vlsi commented on code in PR #5271:
URL: https://github.com/apache/calcite/pull/5271#discussion_r4348471126
##########
core/src/test/java/org/apache/calcite/test/SqlToRelConverterTest.java:
##########
@@ -2314,6 +2314,33 @@ private void checkCallTypesConsistent(String sql) {
sql(sql).ok();
}
+ /**
+ * 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 testUnnestSingleFieldRowAliasKeepsFieldName() {
+ final String sql = "select d, d.a\n"
+ + "from unnest(cast(array[row(1)] as row(a integer) array)) as d";
+ sql(sql).ok();
+ }
+
+ @Test void testUnnestSingleFieldRowWithOrdinality() {
Review Comment:
This test now checks the case where the alias equals `ORDINALITY`, and the
name `testUnnestSingleFieldRowWithOrdinality` still reads as the general `WITH
ORDINALITY` case, which `testUnnestSingleFieldRowWithOrdinalityAllColumns`
covers. A name such as `testUnnestAliasOrdinalityResolvesToOrdinalityColumn`
would say what fails; the `TestCase` entry in `SqlToRelConverterTest.xml` needs
the same rename. This test and
`testUnnestSingleFieldRowWithOrdinalityAllColumns` also still lack the "Test
case for [CALCITE-7791]" Javadoc that
`testUnnestSingleFieldRowAliasKeepsFieldName` has (the second point of the
[2026-10-03
comment](https://github.com/apache/calcite/pull/5271#discussion_r4174190637)).
##########
core/src/main/java/org/apache/calcite/sql/validate/AliasNamespace.java:
##########
@@ -162,6 +170,21 @@ protected AliasNamespace(
}
}
+ /**
+ * Returns whether the data column of an aliased UNNEST is a field of
+ * the collection's ROW element, which keeps its own name, rather than
+ * a scalar column, which the alias renames.
Review Comment:
"rather than a scalar column, which the alias renames" does not cover
`PRESTO` and `BIG_QUERY`: there the method returns `false` and the data column
has the ROW type, as `testUnnestSingleFieldRowUnexpanded` shows. For example:
```java
/**
* 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.
*/
```
##########
core/src/main/java/org/apache/calcite/sql/validate/AliasNamespace.java:
##########
@@ -100,29 +100,37 @@ 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
+ aliasedType = node.getKind() == SqlKind.COLLECTION_TABLE ||
keepsElementFieldName
? 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.
} else if (node.getKind() == SqlKind.UNNEST && rowType.getFieldCount()
== 2
&& ((SqlUnnestOperator) ((SqlBasicCall)
node).getOperator()).withOrdinality) {
- aliasedType = validator.getTypeFactory().builder()
+ // WITH ORDINALITY: data column, ordinality column. The data
+ // column is named after the alias, same as the single-column case.
+ final RelDataType dataColumnAlias =
validator.getTypeFactory().builder()
.kind(rowType.getStructKind())
.add(((SqlIdentifier) operands.get(1)).getSimple(),
rowType.getFieldList().get(0).getType())
- .add(rowType.getFieldList().get(1))
.build();
+ aliasedType = keepsElementFieldName
+ ? new SingleColumnAliasRelDataType(rowType, dataColumnAlias)
+ : validator.getTypeFactory().builder()
Review Comment:
The `false` branch builds the aliased data column again with the same
`.add(...)` call as `dataColumnAlias` at line 123. Reusing it would keep the
two in step:
```java
: validator.getTypeFactory().builder()
.kind(rowType.getStructKind())
.addAll(dataColumnAlias.getFieldList())
.add(rowType.getFieldList().get(1))
.build();
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]