Repository navigation
[CALCITE-7791] UNNEST of a single-field ROW array loses the field name when aliased #5271
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
802dca9
f407a58
bd11b19
33a6826
7c6b0c0
953b947
07ea9f1
1e5e6f9
c0aac83
f00353c
ae62c82
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,6 +28,7 @@ | |
| import org.apache.calcite.sql.SqlNode; | ||
| import org.apache.calcite.sql.SqlNodeList; | ||
| import org.apache.calcite.sql.SqlUnnestOperator; | ||
| import org.apache.calcite.sql.SqlUtil; | ||
| import org.apache.calcite.sql.fun.SqlStdOperatorTable; | ||
| import org.apache.calcite.sql.parser.SqlParserPos; | ||
| import org.apache.calcite.util.Pair; | ||
|
|
@@ -103,29 +104,30 @@ protected AliasNamespace( | |
| final RelDataType aliasedType; | ||
| if (operands.size() == 2) { | ||
| final SqlNode node = operands.get(0); | ||
| // Alias is 'AS t' (no column list). | ||
| // If the sub-query is UNNEST or VALUES, | ||
| // and the sub-query has one column, | ||
| // then the namespace's sole column is named after the alias. | ||
| // 'AS t' on a single-column result, or on UNNEST WITH ORDINALITY with one | ||
| // data column: the data column is named after the alias. A ROW element's | ||
| // field keeps its own name for star expansion, and the alias names it as | ||
| // well. COLLECTION_TABLE handles its column the same way. | ||
| final boolean keepsElementFieldName = node.getKind() == SqlKind.UNNEST | ||
| && keepsElementFieldName((SqlCall) node); | ||
| if (rowType.getFieldCount() == 1) { | ||
| final RelDataType singleColumnAlias = validator.getTypeFactory().builder() | ||
| .kind(rowType.getStructKind()) | ||
| .add(((SqlIdentifier) operands.get(1)).getSimple(), | ||
| rowType.getFieldList().get(0).getType()) | ||
| .build(); | ||
| aliasedType = node.getKind() == SqlKind.COLLECTION_TABLE | ||
| ? new SingleColumnAliasRelDataType(rowType, singleColumnAlias) : singleColumnAlias; | ||
| // If the sub-query is UNNEST with ordinality | ||
| // and the sub-query has two columns: data column, ordinality column | ||
| // then the namespace's sole column is named after the alias. | ||
| final RelDataType dataColumnAlias = | ||
| aliasDataColumn(rowType, (SqlIdentifier) operands.get(1)); | ||
| aliasedType = node.getKind() == SqlKind.COLLECTION_TABLE || keepsElementFieldName | ||
| ? new SingleColumnAliasRelDataType(rowType, dataColumnAlias) : dataColumnAlias; | ||
| } else if (node.getKind() == SqlKind.UNNEST && rowType.getFieldCount() == 2 | ||
| && ((SqlUnnestOperator) ((SqlBasicCall) node).getOperator()).withOrdinality) { | ||
| aliasedType = validator.getTypeFactory().builder() | ||
| .kind(rowType.getStructKind()) | ||
| .add(((SqlIdentifier) operands.get(1)).getSimple(), | ||
| rowType.getFieldList().get(0).getType()) | ||
| .add(rowType.getFieldList().get(1)) | ||
| .build(); | ||
| // WITH ORDINALITY: data column, ordinality column. The data | ||
| // column is named after the alias, same as the single-column case. | ||
| final RelDataType dataColumnAlias = | ||
| aliasDataColumn(rowType, (SqlIdentifier) operands.get(1)); | ||
| aliasedType = keepsElementFieldName | ||
| ? new SingleColumnAliasRelDataType(rowType, dataColumnAlias) | ||
|
Comment on lines
+116
to
+125
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. F1 major An aliased UNNEST of a single-field ROW array on the null-generating side of a LEFT or RIGHT JOIN now validates its column as NOT NULL, and SqlToRelConverter rejects the query, which converted on the base. Problem: The validator's outer-join nullability does not reach a SingleColumnAliasRelDataType, so a column reference through it ( Reproducer// SqlToRelConverterTest
@Test void testUnnestSingleFieldRowAliasOnNullableSide() {
sql("select d, d.a from (values 1) as t (x)\n"
+ "left join unnest(cast(array[row(1)] as row(a integer) array)) as d on true").ok();
sql("select d, d.ordinality from (values 1) as t (x)\n"
+ "left join unnest(cast(array[row(1)] as row(a integer) array))\n"
+ " with ordinality as d on true").ok();
sql("select d from unnest(cast(array[row(1)] as row(a integer) array)) as d\n"
+ "right join (values 1) as t (x) on true").ok();
// head: AssertionError: Conversion to relational algebra failed to preserve datatypes
// validated type: RecordType(INTEGER NOT NULL D) NOT NULL
// converted type: RecordType(INTEGER D) NOT NULL
// expected (base 9634752f for `d`): validated RecordType(INTEGER D) NOT NULL, plan converts
}Proposed solution: Make the nullable copy of a SingleColumnAliasRelDataType a SingleColumnAliasRelDataType over nullable copies of both wrapped types, for example in RelDataTypeFactoryImpl.createTypeWithNullability, which fixes TABLE(...) AS alias with it. Add SqlToRelConverterTest cases, with their validator types in testUnnestAlias, for the aliased UNNEST on the null-generating side of a LEFT and a RIGHT JOIN, plain and WITH ORDINALITY, selecting the alias and the field name.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great catch. fixed in c0aac83 |
||
| : validator.getTypeFactory().builder() | ||
| .kind(rowType.getStructKind()) | ||
| .addAll(dataColumnAlias.getFieldList()) | ||
| .add(rowType.getFieldList().get(1)) | ||
| .build(); | ||
| } else { | ||
| aliasedType = rowType; | ||
| } | ||
|
|
@@ -165,6 +167,39 @@ protected AliasNamespace( | |
| } | ||
| } | ||
|
|
||
| /** Returns a single-field struct holding the first column of | ||
| * {@code rowType}, named after the alias. */ | ||
| private RelDataType aliasDataColumn(RelDataType rowType, SqlIdentifier alias) { | ||
| return validator.getTypeFactory().builder() | ||
| .kind(rowType.getStructKind()) | ||
| .add(alias.getSimple(), rowType.getFieldList().get(0).getType()) | ||
| .build(); | ||
| } | ||
|
|
||
| /** | ||
| * Returns whether UNNEST expands the collection's ROW element into one | ||
| * column per field. Such a column keeps its field name, and the alias | ||
| * names it as well; any other data column takes the alias as its name. | ||
| */ | ||
| private boolean keepsElementFieldName(SqlCall unnestCall) { | ||
| final RelDataType operandType = | ||
| SqlUnnestOperator.unwrapOperandType( | ||
| validator.getValidatedNodeType(unnestCall.operand(0))); | ||
| final RelDataType componentType = operandType.getComponentType(); | ||
| return componentType != null | ||
| && SqlUnnestOperator.expandsStructIntoColumns(componentType, | ||
| validator.config().conformance().allowAliasUnnestItems()) | ||
| && !hasGeneratedFirstFieldName(componentType); | ||
| } | ||
|
|
||
| /** Returns whether the first field of a ROW element has a generated name, | ||
| * such as {@code EXPR$0} for {@code ROW(1)}. The alias then names the | ||
| * column, because a generated name is not worth keeping. */ | ||
| private static boolean hasGeneratedFirstFieldName(RelDataType componentType) { | ||
| final List<RelDataTypeField> fields = componentType.getFieldList(); | ||
| return !fields.isEmpty() && SqlUtil.isGeneratedAlias(fields.get(0).getName()); | ||
| } | ||
|
Comment on lines
+184
to
+201
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. F2 major An aliased UNNEST of an anonymous single-field ROW array now exposes the generated field name EXPR$0 instead of the alias, so Problem: keepsElementFieldName is true for every single-field ROW element, including select * from unnest(array[row(1)]) as d;
-- head: RecordType(INTEGER NOT NULL EXPR$0) base: RecordType(INTEGER NOT NULL D)
select * from unnest(array[row(1)]) as a natural join unnest(array[row(2)]) as b;
-- head: (EXPR$0), LogicalJoin(condition=[=($0, $1)]), no rows
-- base: (A, B), LogicalJoin(condition=[true])
select * from unnest(array[row(1)]) with ordinality as d;
-- head: (EXPR$0, ORDINALITY) base: (D, ORDINALITY)Proposed solution: Keep the base behavior, the alias as the column name, when the element's single field name is generated, for example by also requiring
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great catch. Fixed in c0aac83 |
||
|
|
||
| private static String getString(RelDataType rowType) { | ||
| StringBuilder buf = new StringBuilder(); | ||
| buf.append("("); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
F5 minor No test observes SingleColumnAliasRelDataType.getFieldCount(): the mutant that returns 0 survives, though a wrong count binds a correlated reference to the wrong column.
Problem: The change makes getFieldCount() report the wrapped type's count so that WITH ORDINALITY works through the wrapper. SqlToRelConverter adds each namespace's field count to the offset of the correlation fields after it, so a FROM item to the right of an aliased ROW-element UNNEST WITH ORDINALITY that a correlated sub-query references is where the count shows.
Proposed solution: Add a SqlToRelConverterTest case with an aliased ROW-element UNNEST WITH ORDINALITY followed by another table in the same FROM, and a correlated sub-query that references that table, so the plan pins the correlation field.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed in c0aac83