mihaibudiu commented on code in PR #5409:
URL: https://github.com/apache/calcite/pull/5409#discussion_r4580381485
##########
core/src/test/java/org/apache/calcite/test/SqlValidatorTest.java:
##########
@@ -9318,6 +9318,176 @@ void
testOrderByAliasPreservesValidatedExpressions(String query) {
+ "reference to 'X' from enclosing scope");
}
+ /** Test case for
+ * <a
href="https://issues.apache.org/jira/browse/CALCITE-7933">[CALCITE-7933]
+ * Correlated subqueries with lambdas that capture columns are incorrectly
+ * compiled</a>. A lambda body is evaluated for each element of an array,
+ * so it cannot contain an aggregate or a windowed aggregate. */
+ @Test void testAggregateInLambda() {
+ final SqlValidatorFixture s = fixture()
+ .withOperatorTable(operatorTableFor(SqlLibrary.SPARK));
+ final String aggError =
+ "Aggregate expression is illegal in lambda expression";
+ s.withSql("select \"EXISTS\"(array[1, 2], e -> e = ^max(deptno)^)\n"
+ + "from emp")
+ .fails(aggError);
+ s.withSql("select deptno,\n"
+ + " \"EXISTS\"(array[1, 2], e -> e = ^grouping(deptno)^)\n"
+ + "from emp group by deptno")
+ .fails(aggError);
+ s.withSql("select deptno from emp group by deptno\n"
+ + "having \"EXISTS\"(array[1, 2], e -> e = ^count(*)^)")
+ .fails(aggError);
+ // The aggregate is in the inner lambda of a nested lambda
+ s.withSql("select \"EXISTS\"(array[1, 2],\n"
+ + " e -> \"EXISTS\"(array[3], f -> f = ^min(deptno)^))\n"
+ + "from emp")
+ .fails(aggError);
+ s.withSql("select \"EXISTS\"(array[1, 2],\n"
+ + " e -> e = ^sum(deptno) over ()^)\n"
+ + "from emp")
+ .fails("Windowed aggregate expression is illegal in lambda
expression");
+
+ // The lambda is in the argument of an aggregate
+ s.withSql("select max(\"EXISTS\"(array[deptno], e -> e = empno)) from emp")
+ .ok();
+ // In an aggregate query, the parameter is a row, and the lambda reads
+ // one of its fields
+ s.withSql("select deptno,\n"
+ + " \"EXISTS\"(array[row(1, 2)], r -> r.expr$0 = 1)\n"
+ + "from emp group by deptno")
+ .ok();
+ }
+
+ /** Test case for
+ * <a
href="https://issues.apache.org/jira/browse/CALCITE-7933">[CALCITE-7933]
+ * Correlated subqueries with lambdas that capture columns are incorrectly
+ * compiled</a>. A lambda body cannot contain a sub-query. */
+ @Test void testSubQueryInLambda() {
+ final SqlValidatorFixture s = fixture()
+ .withOperatorTable(operatorTableFor(SqlLibrary.SPARK));
+ final String error = "Sub-query is illegal in lambda expression";
+ s.withSql("select \"EXISTS\"(array[1, 2],\n"
+ + " e -> e = ^(select max(deptno) from dept)^)\n"
+ + "from emp")
+ .fails(error);
+ s.withSql("select \"EXISTS\"(array[1, 2],\n"
+ + " e -> e in (^select deptno from dept^))\n"
+ + "from emp")
+ .fails(error);
+ s.withSql("select \"EXISTS\"(array[1, 2],\n"
+ + " e -> e in (^select deptno from dept union select 1^))\n"
+ + "from emp")
+ .fails(error);
+ // The sub-query reads the lambda parameter
+ s.withSql("select \"EXISTS\"(array[1, 2],\n"
+ + " e -> exists ^(select 1 from dept where deptno = e)^)\n"
+ + "from emp")
+ .fails(error);
+ s.withSql("select \"EXISTS\"(array[1, 2],\n"
+ + " e -> ^(select count(*) from dept where deptno = e)^ = 2)\n"
+ + "from emp")
+ .fails(error);
+ // The sub-query reads a column of the enclosing query
+ s.withSql("select \"EXISTS\"(array[1, 2],\n"
+ + " e -> e = ^(select max(deptno) from dept"
+ + " where deptno <= emp.deptno)^)\n"
+ + "from emp")
+ .fails(error);
+ // The sub-query is in the array argument of the inner lambda
+ s.withSql("select \"EXISTS\"(array[1, 2],\n"
+ + " e -> \"EXISTS\"(array(^select deptno from dept^), f -> f =
e))\n"
+ + "from emp")
+ .fails(error);
+ s.withSql("select empno from emp\n"
+ + "order by \"EXISTS\"(array[1, 2],"
+ + " e -> e in (^select deptno from dept^))")
+ .fails(error);
+
+ // The sub-query is in the array argument, not in the lambda body
+ s.withSql("select \"EXISTS\"(array(select deptno from dept), e -> e =
10)\n"
+ + "from emp")
+ .ok();
+ // The lambda is in a sub-query
+ s.withSql("select (select count(*) from dept\n"
+ + " where \"EXISTS\"(array[deptno], e -> e = emp.deptno))\n"
+ + "from emp")
+ .ok();
+ }
+
+ /** Test case for
+ * <a
href="https://issues.apache.org/jira/browse/CALCITE-7933">[CALCITE-7933]
+ * Correlated subqueries with lambdas that capture columns are incorrectly
+ * compiled</a>. A lambda in an ORDER BY item can read only lambda
+ * parameters, but a lambda in a select item that ORDER BY references
+ * can read columns. */
+ @Test void testLambdaInOrderBy() {
+ final SqlValidatorFixture s = fixture()
+ .withOperatorTable(operatorTableFor(SqlLibrary.SPARK));
+ final String error =
+ "Lambda expression in ORDER BY clause can read only lambda parameters";
+ s.withSql("select empno from emp\n"
+ + "order by \"EXISTS\"(array[1, 2], e -> e = ^deptno^)")
+ .fails(error);
+ s.withSql("select empno from emp\n"
+ + "order by \"EXISTS\"(array[1, 2], e -> e = ^emp.deptno^)")
+ .fails(error);
+ // The lambda reads a select item by alias
+ s.withSql("select empno as x from emp\n"
+ + "order by \"EXISTS\"(array[1, 2], e -> e = ^x^)")
+ .fails(error);
+ // The inner lambda reads a column
+ s.withSql("select empno from emp\n"
+ + "order by \"EXISTS\"(array[array[1]],"
+ + " a -> \"EXISTS\"(a, e -> e = ^deptno^))")
+ .fails(error);
+ s.withSql("select deptno from emp group by deptno\n"
Review Comment:
I have filed https://issues.apache.org/jira/browse/CALCITE-7941
--
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]