This is an automated email from the ASF dual-hosted git repository.

danny0405 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/calcite.git


The following commit(s) were added to refs/heads/master by this push:
     new 2d6e57f  [CALCITE-4033] Does not produce parenthesized table 
expressions for UNNEST (Rui Wang)
2d6e57f is described below

commit 2d6e57f00e6ade5ef534bb4404184a44c2f149f8
Author: amaliujia <amaliu...@163.com>
AuthorDate: Sat Jun 13 12:07:06 2020 -0700

    [CALCITE-4033] Does not produce parenthesized table expressions for UNNEST 
(Rui Wang)
    
    Before this change, Calcite parser produces "(UNEST(...))". However, 
Calcite parser fails to parse UNNEST as parenthesized table expressions: "JOIN 
(SELECT * FROM ^(UNNEST(...))^)". This change stops produces parenthesized 
table expressions for UNNEST to fix this problem.
    
    close apache/calcite#2025
---
 .../main/java/org/apache/calcite/sql/SqlKind.java  |  3 ++-
 .../apache/calcite/sql/parser/SqlParserTest.java   | 29 +++++++++++++++++-----
 2 files changed, 25 insertions(+), 7 deletions(-)

diff --git a/core/src/main/java/org/apache/calcite/sql/SqlKind.java 
b/core/src/main/java/org/apache/calcite/sql/SqlKind.java
index f98a0c2..f1cfb08 100644
--- a/core/src/main/java/org/apache/calcite/sql/SqlKind.java
+++ b/core/src/main/java/org/apache/calcite/sql/SqlKind.java
@@ -1240,6 +1240,7 @@ public enum SqlKind {
    * {@link #ORDER_BY},
    * {@link #COLLECTION_TABLE},
    * {@link #TABLESAMPLE},
+   * {@link #UNNEST}
    * or an aggregate function, DML or DDL.
    */
   public static final Set<SqlKind> EXPRESSION =
@@ -1254,7 +1255,7 @@ public enum SqlKind {
                   LITERAL_CHAIN, JDBC_FN, PRECEDING, FOLLOWING, ORDER_BY,
                   NULLS_FIRST, NULLS_LAST, COLLECTION_TABLE, TABLESAMPLE,
                   VALUES, WITH, WITH_ITEM, ITEM, SKIP_TO_FIRST, SKIP_TO_LAST,
-                  JSON_VALUE_EXPRESSION),
+                  JSON_VALUE_EXPRESSION, UNNEST),
               AGGREGATE, DML, DDL));
 
   /**
diff --git 
a/core/src/test/java/org/apache/calcite/sql/parser/SqlParserTest.java 
b/core/src/test/java/org/apache/calcite/sql/parser/SqlParserTest.java
index e5d3cec..19cd942 100644
--- a/core/src/test/java/org/apache/calcite/sql/parser/SqlParserTest.java
+++ b/core/src/test/java/org/apache/calcite/sql/parser/SqlParserTest.java
@@ -7155,10 +7155,10 @@ public class SqlParserTest {
   @Test void testUnnest() {
     sql("select*from unnest(x)")
         .ok("SELECT *\n"
-            + "FROM (UNNEST(`X`))");
+            + "FROM UNNEST(`X`)");
     sql("select*from unnest(x) AS T")
         .ok("SELECT *\n"
-            + "FROM (UNNEST(`X`)) AS `T`");
+            + "FROM UNNEST(`X`) AS `T`");
 
     // UNNEST cannot be first word in query
     sql("^unnest^(x)")
@@ -7169,24 +7169,41 @@ public class SqlParserTest {
         + "unnest(dept.employees, dept.managers)";
     final String expected = "SELECT *\n"
         + "FROM `DEPT`,\n"
-        + "(UNNEST(`DEPT`.`EMPLOYEES`, `DEPT`.`MANAGERS`))";
+        + "UNNEST(`DEPT`.`EMPLOYEES`, `DEPT`.`MANAGERS`)";
     sql(sql).ok(expected);
 
     // LATERAL UNNEST is not valid
     sql("select * from dept, lateral ^unnest^(dept.employees)")
         .fails("(?s)Encountered \"unnest\" at .*");
+
+    // Does not generate extra parentheses around UNNEST because UNNEST is
+    // a table expression.
+    final String sql1 = ""
+        + "SELECT\n"
+        + "  item.name,\n"
+        + "  relations.*\n"
+        + "FROM dfs.tmp item\n"
+        + "JOIN (\n"
+        + "  SELECT * FROM UNNEST(item.related) i(rels)\n"
+        + ") relations\n"
+        + "ON TRUE";
+    final String expected1 = "SELECT `ITEM`.`NAME`, `RELATIONS`.*\n"
+        + "FROM `DFS`.`TMP` AS `ITEM`\n"
+        + "INNER JOIN (SELECT *\n"
+        + "FROM UNNEST(`ITEM`.`RELATED`) AS `I` (`RELS`)) AS `RELATIONS` ON 
TRUE";
+    sql(sql1).ok(expected1);
   }
 
   @Test void testUnnestWithOrdinality() {
     sql("select * from unnest(x) with ordinality")
         .ok("SELECT *\n"
-            + "FROM (UNNEST(`X`) WITH ORDINALITY)");
+            + "FROM UNNEST(`X`) WITH ORDINALITY");
     sql("select*from unnest(x) with ordinality AS T")
         .ok("SELECT *\n"
-            + "FROM (UNNEST(`X`) WITH ORDINALITY) AS `T`");
+            + "FROM UNNEST(`X`) WITH ORDINALITY AS `T`");
     sql("select*from unnest(x) with ordinality AS T(c, o)")
         .ok("SELECT *\n"
-            + "FROM (UNNEST(`X`) WITH ORDINALITY) AS `T` (`C`, `O`)");
+            + "FROM UNNEST(`X`) WITH ORDINALITY AS `T` (`C`, `O`)");
     sql("select*from unnest(x) as T ^with^ ordinality")
         .fails("(?s)Encountered \"with\" at .*");
   }

Reply via email to