mihaibudiu commented on code in PR #4637:
URL: https://github.com/apache/calcite/pull/4637#discussion_r2548080919


##########
core/src/test/resources/sql/sub-query.iq:
##########
@@ -5443,4 +5443,1121 @@ ORDER BY deptno;
 
 !ok
 
+# [CALCITE-7272] Subqueries cannot be decorrelated if have set op
+SELECT ename,
+    (SELECT sum(c)
+    FROM
+        (SELECT deptno AS c
+        FROM dept
+        WHERE dept.deptno = emp.deptno
+        UNION ALL
+        SELECT 2 AS c
+        FROM bonus
+        WHERE bonus.job = emp.job) AS union_subquery
+    ) AS correlated_sum
+FROM emp
+ORDER BY ename;
++--------+----------------+
+| ENAME  | CORRELATED_SUM |
++--------+----------------+
+| ADAMS  |             20 |
+| ALLEN  |             30 |
+| BLAKE  |             30 |
+| CLARK  |             10 |
+| FORD   |             20 |
+| JAMES  |             30 |
+| JONES  |             20 |
+| KING   |             10 |
+| MARTIN |             30 |
+| MILLER |             10 |
+| SCOTT  |             20 |
+| SMITH  |             20 |
+| TURNER |             30 |
+| WARD   |             30 |
++--------+----------------+
+(14 rows)
+
+!ok
+
+# [CALCITE-7272] Subqueries cannot be decorrelated if have set op
+SELECT *,
+  (SELECT COUNT(*)
+   FROM (
+     SELECT * FROM emp WHERE emp.deptno = dept.deptno
+     UNION ALL
+     SELECT * FROM emp) AS sub
+   GROUP BY deptno) AS num_dept_groups
+FROM dept;
+more than one value in agg SINGLE_VALUE
+!error
+
+EnumerableCalc(expr#0..4=[{inputs}], proj#0..2=[{exprs}], $f0=[$t4])
+  EnumerableHashJoin(condition=[=($0, $3)], joinType=[left])
+    EnumerableTableScan(table=[[scott, DEPT]])
+    EnumerableAggregate(group=[{0}], agg#0=[SINGLE_VALUE($2)])
+      EnumerableAggregate(group=[{0, 1}], EXPR$0=[COUNT()])
+        EnumerableUnion(all=[true])
+          EnumerableCalc(expr#0..3=[{inputs}], DEPTNO=[$t3], DEPTNO0=[$t1])
+            EnumerableNestedLoopJoin(condition=[IS NOT DISTINCT FROM($3, $2)], 
joinType=[inner])
+              EnumerableCalc(expr#0..7=[{inputs}], expr#8=[IS NOT NULL($t7)], 
EMPNO=[$t0], DEPTNO=[$t7], DEPTNO8=[$t7], $condition=[$t8])
+                EnumerableTableScan(table=[[scott, EMP]])
+              EnumerableCalc(expr#0..2=[{inputs}], DEPTNO=[$t0])
+                EnumerableTableScan(table=[[scott, DEPT]])
+          EnumerableCalc(expr#0..2=[{inputs}], DEPTNO=[$t2], DEPTNO0=[$t1])
+            EnumerableNestedLoopJoin(condition=[true], joinType=[inner])
+              EnumerableCalc(expr#0..7=[{inputs}], EMPNO=[$t0], DEPTNO=[$t7])
+                EnumerableTableScan(table=[[scott, EMP]])
+              EnumerableCalc(expr#0..2=[{inputs}], DEPTNO=[$t0])
+                EnumerableTableScan(table=[[scott, DEPT]])
+!plan
+
+# [CALCITE-7272] Subqueries cannot be decorrelated if have set op

Review Comment:
   I have double-checked all these queries against postgres, and the result is 
good.
   You should add this information as a comment, because I have seen incorrect 
test results previously.



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