Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ protected AliasNamespace(
rowType.getFieldList().get(0).getType())
.build();
aliasedType = node.getKind() == SqlKind.COLLECTION_TABLE
|| (node.getKind() == SqlKind.UNNEST && isUnnestOfStructArray((SqlCall) node))
? new SingleColumnAliasRelDataType(rowType, singleColumnAlias) : singleColumnAlias;
// If the sub-query is UNNEST with ordinality
// and the sub-query has two columns: data column, ordinality column
Expand Down Expand Up @@ -162,6 +163,22 @@ protected AliasNamespace(
}
}

/**
* Returns whether an UNNEST call's array operand has a struct element type.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

beware that unnest has two semantics, depending on conformance: the Trino semantics does not create a column for each ROW column, and the standard semantics does.

*
* <p>{@link SqlUnnestOperator#inferReturnType} produces a 1-field row for
* both struct arrays and scalar arrays. Only the structs have a real field
* name worth preserving.
*/
private boolean isUnnestOfStructArray(SqlCall unnestCall) {
if (unnestCall.operandCount() != 1) {
return false;
}
final RelDataType operandType = validator.getValidatedNodeType(unnestCall.operand(0));
final RelDataType componentType = operandType.getComponentType();
return componentType != null && componentType.isStruct();
}

private static String getString(RelDataType rowType) {
StringBuilder buf = new StringBuilder();
buf.append("(");
Expand Down
14 changes: 14 additions & 0 deletions core/src/test/java/org/apache/calcite/test/SqlValidatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1201,6 +1201,20 @@ void testDyadicCollateOperator() {
});
}

/** Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-7791">[CALCITE-7791]
* UNNEST of a single-field ROW array loses the field name when aliased</a>. */
@Test void testUnnestSingleFieldRow() {
sql("select d.a\n"
+ "from unnest(cast(array[row(1)] as row(a integer) array)) as d")
.columnType("INTEGER NOT NULL");
// The array's real field name ("A") survives star-expansion too, not just
// the synthetic alias ("D").
sql("select *\n"
+ "from unnest(cast(array[row(1)] as row(a integer) array)) as d")
.type("RecordType(INTEGER NOT NULL A) NOT NULL");
}

@Test void testOverlay() {
expr("overlay('ABCdef' placing 'abc' from 1)").ok();
expr("overlay('ABCdef' placing 'abc' from 1 for 3)").ok();
Expand Down
Loading