Repository: calcite
Updated Branches:
  refs/heads/master 4e93a82c6 -> 312ab8122


[CALCITE-2391] Aggregate query with UNNEST or LATERAL fails with 
ClassCastException


Project: http://git-wip-us.apache.org/repos/asf/calcite/repo
Commit: http://git-wip-us.apache.org/repos/asf/calcite/commit/312ab812
Tree: http://git-wip-us.apache.org/repos/asf/calcite/tree/312ab812
Diff: http://git-wip-us.apache.org/repos/asf/calcite/diff/312ab812

Branch: refs/heads/master
Commit: 312ab81227d0b6edfdad80bb3dcc6d1f1c771e75
Parents: 4e93a82
Author: Julian Hyde <[email protected]>
Authored: Sun Jul 1 22:11:53 2018 -0700
Committer: Julian Hyde <[email protected]>
Committed: Sun Jul 1 22:11:53 2018 -0700

----------------------------------------------------------------------
 .../calcite/sql/validate/SqlValidatorScope.java |  4 +++
 .../java/org/apache/calcite/test/JdbcTest.java  | 11 ++++++
 core/src/test/resources/sql/lateral.iq          | 38 ++++++++++++++++++++
 3 files changed, 53 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/calcite/blob/312ab812/core/src/main/java/org/apache/calcite/sql/validate/SqlValidatorScope.java
----------------------------------------------------------------------
diff --git 
a/core/src/main/java/org/apache/calcite/sql/validate/SqlValidatorScope.java 
b/core/src/main/java/org/apache/calcite/sql/validate/SqlValidatorScope.java
index b32ed36..21c1a5a 100644
--- a/core/src/main/java/org/apache/calcite/sql/validate/SqlValidatorScope.java
+++ b/core/src/main/java/org/apache/calcite/sql/validate/SqlValidatorScope.java
@@ -298,6 +298,10 @@ public interface SqlValidatorScope {
       if (scope instanceof TableScope) {
         scope = scope.getValidator().getSelectScope((SqlSelect) 
scope.getNode());
       }
+      if (scope instanceof AggregatingSelectScope) {
+        scope = ((AggregatingSelectScope) scope).parent;
+        assert scope instanceof SelectScope;
+      }
       resolves.add(
           new Resolve(namespace, nullable, scope, path, remainingNames));
     }

http://git-wip-us.apache.org/repos/asf/calcite/blob/312ab812/core/src/test/java/org/apache/calcite/test/JdbcTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/calcite/test/JdbcTest.java 
b/core/src/test/java/org/apache/calcite/test/JdbcTest.java
index 40d2f37..4af601b 100644
--- a/core/src/test/java/org/apache/calcite/test/JdbcTest.java
+++ b/core/src/test/java/org/apache/calcite/test/JdbcTest.java
@@ -2092,6 +2092,17 @@ public class JdbcTest {
             "empid=150; deptno=10; name=Sebastian; salary=7000.0; 
commission=null");
   }
 
+  /** Test case for
+   * <a 
href="https://issues.apache.org/jira/browse/CALCITE-2381";>[CALCITE-2391]
+   * Aggregate query with UNNEST or LATERAL fails with
+   * ClassCastException</a>. */
+  @Test public void testAggUnnestColumn() {
+    final String sql = "select count(d.\"name\") as c\n"
+        + "from \"hr\".\"depts\" as d,\n"
+        + " UNNEST(d.\"employees\") as e";
+    CalciteAssert.hr().query(sql).returnsUnordered("C=3");
+  }
+
   @Test public void testArrayElement() {
     CalciteAssert.that()
         .with(CalciteAssert.Config.REGULAR)

http://git-wip-us.apache.org/repos/asf/calcite/blob/312ab812/core/src/test/resources/sql/lateral.iq
----------------------------------------------------------------------
diff --git a/core/src/test/resources/sql/lateral.iq 
b/core/src/test/resources/sql/lateral.iq
index 556b4d2..046b509 100644
--- a/core/src/test/resources/sql/lateral.iq
+++ b/core/src/test/resources/sql/lateral.iq
@@ -108,4 +108,42 @@ where e.deptno = 10;
 
 !ok
 
+select *
+from "scott".dept,
+  lateral (select * from "scott".emp where emp.deptno = dept.deptno) as e;
++--------+------------+----------+-------+--------+-----------+------+------------+---------+---------+---------+
+| DEPTNO | DNAME      | LOC      | EMPNO | ENAME  | JOB       | MGR  | 
HIREDATE   | SAL     | COMM    | DEPTNO0 |
++--------+------------+----------+-------+--------+-----------+------+------------+---------+---------+---------+
+|     10 | ACCOUNTING | NEW YORK |  7782 | CLARK  | MANAGER   | 7839 | 
1981-06-09 | 2450.00 |         |      10 |
+|     10 | ACCOUNTING | NEW YORK |  7839 | KING   | PRESIDENT |      | 
1981-11-17 | 5000.00 |         |      10 |
+|     10 | ACCOUNTING | NEW YORK |  7934 | MILLER | CLERK     | 7782 | 
1982-01-23 | 1300.00 |         |      10 |
+|     20 | RESEARCH   | DALLAS   |  7369 | SMITH  | CLERK     | 7902 | 
1980-12-17 |  800.00 |         |      20 |
+|     20 | RESEARCH   | DALLAS   |  7566 | JONES  | MANAGER   | 7839 | 
1981-02-04 | 2975.00 |         |      20 |
+|     20 | RESEARCH   | DALLAS   |  7788 | SCOTT  | ANALYST   | 7566 | 
1987-04-19 | 3000.00 |         |      20 |
+|     20 | RESEARCH   | DALLAS   |  7876 | ADAMS  | CLERK     | 7788 | 
1987-05-23 | 1100.00 |         |      20 |
+|     20 | RESEARCH   | DALLAS   |  7902 | FORD   | ANALYST   | 7566 | 
1981-12-03 | 3000.00 |         |      20 |
+|     30 | SALES      | CHICAGO  |  7499 | ALLEN  | SALESMAN  | 7698 | 
1981-02-20 | 1600.00 |  300.00 |      30 |
+|     30 | SALES      | CHICAGO  |  7521 | WARD   | SALESMAN  | 7698 | 
1981-02-22 | 1250.00 |  500.00 |      30 |
+|     30 | SALES      | CHICAGO  |  7654 | MARTIN | SALESMAN  | 7698 | 
1981-09-28 | 1250.00 | 1400.00 |      30 |
+|     30 | SALES      | CHICAGO  |  7698 | BLAKE  | MANAGER   | 7839 | 
1981-01-05 | 2850.00 |         |      30 |
+|     30 | SALES      | CHICAGO  |  7844 | TURNER | SALESMAN  | 7698 | 
1981-09-08 | 1500.00 |    0.00 |      30 |
+|     30 | SALES      | CHICAGO  |  7900 | JAMES  | CLERK     | 7698 | 
1981-12-03 |  950.00 |         |      30 |
++--------+------------+----------+-------+--------+-----------+------+------------+---------+---------+---------+
+(14 rows)
+
+!ok
+
+# [CALCITE-2391] Aggregate query with UNNEST or LATERAL fails with 
ClassCastException
+select count(*) as c
+from "scott".dept,
+  lateral (select * from "scott".emp where emp.deptno = dept.deptno) as e;
++----+
+| C  |
++----+
+| 14 |
++----+
+(1 row)
+
+!ok
+
 # End lateral.iq

Reply via email to