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

chunwei 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 603f93d  [CALCITE-3003] AssertionError when GROUP BY nested field 
(Will Yu)
603f93d is described below

commit 603f93d3fd242010a2fd3dd2dd658dbeea0a96fc
Author: Will Yu <[email protected]>
AuthorDate: Sat Apr 27 17:02:59 2019 -0700

    [CALCITE-3003] AssertionError when GROUP BY nested field (Will Yu)
    
    Close apache/calcite#1186
---
 .../calcite/sql/validate/SqlValidatorUtil.java     |  2 +-
 .../apache/calcite/test/SqlToRelConverterTest.java | 60 ++++++++++++++++++++++
 .../org/apache/calcite/test/SqlValidatorTest.java  | 21 ++++++++
 .../apache/calcite/test/SqlToRelConverterTest.xml  | 30 +++++++++++
 4 files changed, 112 insertions(+), 1 deletion(-)

diff --git 
a/core/src/main/java/org/apache/calcite/sql/validate/SqlValidatorUtil.java 
b/core/src/main/java/org/apache/calcite/sql/validate/SqlValidatorUtil.java
index ad0e241..956afab 100644
--- a/core/src/main/java/org/apache/calcite/sql/validate/SqlValidatorUtil.java
+++ b/core/src/main/java/org/apache/calcite/sql/validate/SqlValidatorUtil.java
@@ -836,7 +836,7 @@ public class SqlValidatorUtil {
       SqlIdentifier expr = (SqlIdentifier) expandedGroupExpr;
 
       // column references should be fully qualified.
-      assert expr.names.size() == 2;
+      assert expr.names.size() >= 2;
       String originalRelName = expr.names.get(0);
       String originalFieldName = expr.names.get(1);
 
diff --git 
a/core/src/test/java/org/apache/calcite/test/SqlToRelConverterTest.java 
b/core/src/test/java/org/apache/calcite/test/SqlToRelConverterTest.java
index dbe51d2..74d3625 100644
--- a/core/src/test/java/org/apache/calcite/test/SqlToRelConverterTest.java
+++ b/core/src/test/java/org/apache/calcite/test/SqlToRelConverterTest.java
@@ -2679,6 +2679,66 @@ public class SqlToRelConverterTest extends 
SqlToRelTestBase {
     sql(sql).ok();
   }
 
+  /**
+   * Test case for <a 
href="https://issues.apache.org/jira/browse/CALCITE-3003";>[CALCITE-3003]
+   * AssertionError when GROUP BY nested field</a>.
+   */
+  @Test
+  public void testGroupByNestedColumn() {
+    final String sql =
+        "select\n"
+            + "  coord.x,\n"
+            + "  coord_ne.sub.a,\n"
+            + "  avg(coord.y)\n"
+            + "from\n"
+            + "  customer.contact_peek\n"
+            + "group by\n"
+            + "  coord_ne.sub.a,\n"
+            + "  coord.x";
+    sql(sql).ok();
+  }
+
+  /**
+   * Similar to {@link #testGroupByNestedColumn()},
+   * but with grouping sets.
+   */
+  @Test
+  public void testGroupingSetsWithNestedColumn() {
+    final String sql =
+        "select\n"
+            + "  coord.x,\n"
+            + "  coord.\"unit\",\n"
+            + "  coord_ne.sub.a,\n"
+            + "  avg(coord.y)\n"
+            + "from\n"
+            + "  customer.contact_peek\n"
+            + "group by\n"
+            + "  grouping sets (\n"
+            + "    (coord_ne.sub.a, coord.x, coord.\"unit\"),\n"
+            + "    (coord.x, coord.\"unit\")\n"
+            + "  )";
+    sql(sql).ok();
+  }
+
+  /**
+   * Similar to {@link #testGroupByNestedColumn()},
+   * but with cube.
+   */
+  @Test
+  public void testGroupByCubeWithNestedColumn() {
+    final String sql =
+        "select\n"
+            + "  coord.x,\n"
+            + "  coord.\"unit\",\n"
+            + "  coord_ne.sub.a,\n"
+            + "  avg(coord.y)\n"
+            + "from\n"
+            + "  customer.contact_peek\n"
+            + "group by\n"
+            + "  cube (coord_ne.sub.a, coord.x, coord.\"unit\")";
+    sql(sql).ok();
+  }
+
   @Test public void testDynamicSchemaUnnest() {
     final String sql3 = "select t1.c_nationkey, t3.fake_col3\n"
         + "from SALES.CUSTOMER as t1,\n"
diff --git a/core/src/test/java/org/apache/calcite/test/SqlValidatorTest.java 
b/core/src/test/java/org/apache/calcite/test/SqlValidatorTest.java
index de5c333..37de177 100644
--- a/core/src/test/java/org/apache/calcite/test/SqlValidatorTest.java
+++ b/core/src/test/java/org/apache/calcite/test/SqlValidatorTest.java
@@ -5197,6 +5197,27 @@ public class SqlValidatorTest extends 
SqlValidatorTestCase {
         .fails("(?s)Cannot apply '\\+' to arguments of type.*");
   }
 
+  /**
+   * Test case for <a 
href="https://issues.apache.org/jira/browse/CALCITE-3003";>[CALCITE-3003]
+   * AssertionError when GROUP BY nested field</a>.
+   *
+   * <p>Make sure table name of GROUP BY item with nested field could be
+   * properly validated.
+   */
+  @Test
+  public void testInvalidGroupByWithInvalidTableName() {
+    final String sql =
+        "select\n"
+            + "  coord.x,\n"
+            + "  avg(coord.y)\n"
+            + "from\n"
+            + "  customer.contact_peek\n"
+            + "group by\n"
+            + "  ^unknown_table_alias.coord^.x";
+    sql(sql)
+        .fails("Table 'UNKNOWN_TABLE_ALIAS.COORD' not found");
+  }
+
   /** Test case for
    * <a 
href="https://issues.apache.org/jira/browse/CALCITE-1781";>[CALCITE-1781]
    * Allow expression in CUBE and ROLLUP</a>. */
diff --git 
a/core/src/test/resources/org/apache/calcite/test/SqlToRelConverterTest.xml 
b/core/src/test/resources/org/apache/calcite/test/SqlToRelConverterTest.xml
index a6752d1..843c712 100644
--- a/core/src/test/resources/org/apache/calcite/test/SqlToRelConverterTest.xml
+++ b/core/src/test/resources/org/apache/calcite/test/SqlToRelConverterTest.xml
@@ -882,6 +882,36 @@ LogicalProject(UNIT_LENGTH=[CHAR_LENGTH($3)])
 ]]>
         </Resource>
     </TestCase>
+    <TestCase name="testGroupByNestedColumn">
+        <Resource name="plan">
+            <![CDATA[
+LogicalProject(X=[$1], A=[$0], EXPR$2=[$2])
+  LogicalAggregate(group=[{0, 1}], EXPR$2=[AVG($2)])
+    LogicalProject(A=[$5.SUB.A], X=[$4.X], $f2=[$4.Y])
+      LogicalTableScan(table=[[CATALOG, CUSTOMER, CONTACT_PEEK]])
+]]>
+        </Resource>
+    </TestCase>
+    <TestCase name="testGroupingSetsWithNestedColumn">
+        <Resource name="plan">
+            <![CDATA[
+LogicalProject(X=[$1], unit=[$2], A=[$0], EXPR$3=[$3])
+  LogicalAggregate(group=[{0, 1, 2}], groups=[[{0, 1, 2}, {1, 2}]], 
EXPR$3=[AVG($3)])
+    LogicalProject(A=[$5.SUB.A], X=[$4.X], unit=[$4.unit], $f3=[$4.Y])
+      LogicalTableScan(table=[[CATALOG, CUSTOMER, CONTACT_PEEK]])
+]]>
+        </Resource>
+    </TestCase>
+    <TestCase name="testGroupByCubeWithNestedColumn">
+    <Resource name="plan">
+    <![CDATA[
+LogicalProject(X=[$1], unit=[$2], A=[$0], EXPR$3=[$3])
+  LogicalAggregate(group=[{0, 1, 2}], groups=[[{0, 1, 2}, {0, 1}, {0, 2}, {0}, 
{1, 2}, {1}, {2}, {}]], EXPR$3=[AVG($3)])
+    LogicalProject(A=[$5.SUB.A], X=[$4.X], unit=[$4.unit], $f3=[$4.Y])
+      LogicalTableScan(table=[[CATALOG, CUSTOMER, CONTACT_PEEK]])
+]]>
+    </Resource>
+    </TestCase>
     <TestCase name="testDynamicSchemaUnnest">
         <Resource name="sql">
             <![CDATA[select t1.c_nationkey, t3.fake_col3

Reply via email to