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


##########
core/src/test/java/org/apache/calcite/test/SqlValidatorTest.java:
##########
@@ -10101,6 +10101,72 @@ void testGroupExpressionEquivalenceParams() {
     sql("select * from (SELECT ARRAY['banana'] as fruits) as t, 
UNNEST(t.fruits)")
         .type("RecordType(CHAR(6) NOT NULL ARRAY NOT NULL FRUITS, "
             + "CHAR(6) NOT NULL EXPR$0) NOT NULL").ok();
+
+    // Test case for [CALCITE-7791] UNNEST of a single-field ROW array loses
+    // the field name when aliased. The field name "A" survives
+    // star-expansion. The alias "D" still names the column and
+    // qualifies the field.
+    sql("select d, d.a\n"
+        + "from unnest(cast(array[row(1)] as row(a integer) array)) as d")
+        .type("RecordType(INTEGER NOT NULL D, INTEGER NOT NULL A) NOT NULL");
+
+    // A sub-query operand keeps the field name as well.
+    sql("select *\n"
+        + "from unnest(\n"
+        + "  (select cast(array[row(1)] as row(a integer) array)\n"
+        + "   from (values (1)))) as d")
+        .type("RecordType(INTEGER NOT NULL A) NOT NULL");
+
+    // WITH ORDINALITY keeps the field name and the alias still
+    // names the data column.
+    sql("select *\n"
+        + "from unnest(cast(array[row(1)] as row(a integer) array))\n"
+        + "  with ordinality as d")
+        .type("RecordType(INTEGER NOT NULL A, INTEGER NOT NULL ORDINALITY) NOT 
NULL");
+    sql("select d, d.a\n"
+        + "from unnest(cast(array[row(1)] as row(a integer) array))\n"
+        + "  with ordinality as d")
+        .type("RecordType(INTEGER NOT NULL D, INTEGER NOT NULL A) NOT NULL");
+
+    // UNNEST MULTISET gets the same treatment.
+    sql("select *\n"
+        + "from unnest(cast(multiset[row(1)] as row(a integer) multiset)) as 
d")
+        .type("RecordType(INTEGER NOT NULL A) NOT NULL");
+
+    // NATURAL JOIN and USING match the field name "A", not the alias "D".
+    // A column named after the alias does not count as a match.

Review Comment:
   Ack.  Should be addressed in 7c6b0c09



##########
core/src/test/java/org/apache/calcite/test/SqlValidatorTest.java:
##########
@@ -10101,6 +10101,72 @@ void testGroupExpressionEquivalenceParams() {
     sql("select * from (SELECT ARRAY['banana'] as fruits) as t, 
UNNEST(t.fruits)")
         .type("RecordType(CHAR(6) NOT NULL ARRAY NOT NULL FRUITS, "
             + "CHAR(6) NOT NULL EXPR$0) NOT NULL").ok();
+
+    // Test case for [CALCITE-7791] UNNEST of a single-field ROW array loses
+    // the field name when aliased. The field name "A" survives
+    // star-expansion. The alias "D" still names the column and
+    // qualifies the field.
+    sql("select d, d.a\n"
+        + "from unnest(cast(array[row(1)] as row(a integer) array)) as d")
+        .type("RecordType(INTEGER NOT NULL D, INTEGER NOT NULL A) NOT NULL");
+
+    // A sub-query operand keeps the field name as well.
+    sql("select *\n"
+        + "from unnest(\n"
+        + "  (select cast(array[row(1)] as row(a integer) array)\n"
+        + "   from (values (1)))) as d")
+        .type("RecordType(INTEGER NOT NULL A) NOT NULL");
+
+    // WITH ORDINALITY keeps the field name and the alias still
+    // names the data column.
+    sql("select *\n"
+        + "from unnest(cast(array[row(1)] as row(a integer) array))\n"
+        + "  with ordinality as d")
+        .type("RecordType(INTEGER NOT NULL A, INTEGER NOT NULL ORDINALITY) NOT 
NULL");
+    sql("select d, d.a\n"
+        + "from unnest(cast(array[row(1)] as row(a integer) array))\n"
+        + "  with ordinality as d")
+        .type("RecordType(INTEGER NOT NULL D, INTEGER NOT NULL A) NOT NULL");
+
+    // UNNEST MULTISET gets the same treatment.
+    sql("select *\n"
+        + "from unnest(cast(multiset[row(1)] as row(a integer) multiset)) as 
d")
+        .type("RecordType(INTEGER NOT NULL A) NOT NULL");
+
+    // NATURAL JOIN and USING match the field name "A", not the alias "D".
+    // A column named after the alias does not count as a match.
+    sql("select *\n"
+        + "from unnest(cast(array[row(1)] as row(a integer) array)) as d\n"
+        + "natural join (select 1 as a) as t")
+        .type("RecordType(INTEGER NOT NULL A) NOT NULL");
+    sql("select *\n"
+        + "from unnest(cast(array[row(1)] as row(a integer) array)) as d\n"
+        + "natural join (select 1 as d) as t")
+        .type("RecordType(INTEGER NOT NULL A, INTEGER NOT NULL D) NOT NULL");
+    sql("select *\n"
+        + "from unnest(cast(array[row(1)] as row(a integer) array)) as d\n"
+        + "join (values (1)) as t (a) using (a)")
+        .type("RecordType(INTEGER NOT NULL A) NOT NULL");
+    // USING also accepts the alias, which resolves through the field.

Review Comment:
   Ack.  Should be addressed in 7c6b0c09



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