sbroeder commented on code in PR #5271:
URL: https://github.com/apache/calcite/pull/5271#discussion_r4360515334


##########
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:
   Done



-- 
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]

Reply via email to