mihaibudiu commented on code in PR #4637:
URL: https://github.com/apache/calcite/pull/4637#discussion_r2548297872
##########
core/src/main/java/org/apache/calcite/sql2rel/RelDecorrelator.java:
##########
@@ -1184,6 +1185,160 @@ private static void shiftMapping(Map<Integer, Integer>
mapping, int startIndex,
return null;
}
+ /**
+ * Given the SQL:
+ * 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;
+ *
+ * <p>from:
+ * LogicalProject(ENAME=[$1], CORRELATED_SUM=[$8])
+ * LogicalCorrelate(correlation=[$cor0], joinType=[left],
requiredColumns=[{2, 7}])
+ * LogicalTableScan(table=[[scott, EMP]])
+ * LogicalAggregate(group=[{}], EXPR$0=[SUM($0)])
+ * LogicalUnion(all=[true])
+ * LogicalProject(C=[CAST($0):INTEGER NOT NULL])
+ * LogicalFilter(condition=[=($0, $cor0.DEPTNO)])
+ * LogicalTableScan(table=[[scott, DEPT]])
+ * LogicalProject(C=[2])
+ * LogicalFilter(condition=[=($1, $cor0.JOB)])
+ * LogicalTableScan(table=[[scott, BONUS]])
+ *
+ * <p>to:
+ * LogicalProject(ENAME=[$1], CORRELATED_SUM=[$10])
+ * LogicalJoin(condition=[AND(IS NOT DISTINCT FROM($2, $8),
+ * IS NOT DISTINCT FROM($7, $9))],
joinType=[left])
+ * LogicalTableScan(table=[[scott, EMP]])
+ * LogicalAggregate(group=[{0, 1}], EXPR$0=[SUM($2)])
+ * LogicalProject(JOB=[$0], DEPTNO=[$1], C=[$2])
+ * LogicalUnion(all=[true])
+ * LogicalProject(JOB=[$0], DEPTNO=[$1], C=[$2])
+ * LogicalJoin(condition=[IS NOT DISTINCT FROM($1, $3)],
joinType=[inner])
+ * LogicalAggregate(group=[{2, 7}])
+ * LogicalTableScan(table=[[scott, EMP]])
+ * LogicalProject(C=[CAST($0):INTEGER NOT NULL], DEPTNO=[$0])
+ * LogicalTableScan(table=[[scott, DEPT]])
+ * LogicalProject(JOB=[$0], DEPTNO=[$1], C=[$2])
+ * LogicalJoin(condition=[IS NOT DISTINCT FROM($0, $3)],
joinType=[inner])
+ * LogicalAggregate(group=[{2, 7}])
+ * LogicalTableScan(table=[[scott, EMP]])
+ * LogicalProject(C=[2], JOB=[$1])
+ * LogicalFilter(condition=[IS NOT NULL($1)])
+ * LogicalTableScan(table=[[scott, BONUS]])
+ */
+ public @Nullable Frame decorrelateRel(SetOp rel, boolean isCorVarDefined,
+ boolean parentPropagatesNullValues) {
+ if (this.frameStack.peek() == null) {
+ return null;
+ }
+
+ final Pair<CorrelationId, Frame> outerFramePair =
requireNonNull(this.frameStack.peek());
+ final CorrelationId outFrameCorrId = outerFramePair.left;
+ final Frame outFrame = outerFramePair.right;
+
+ // Collect CorDef from all inputs
+ ImmutableBitSet.Builder corFieldBuilder = ImmutableBitSet.builder();
+ List<Frame> frames = new ArrayList<>();
+ for (RelNode oldInput : rel.getInputs()) {
+ if (!(oldInput instanceof Project)) {
Review Comment:
This is a strange requirement; in principle you could always insert an
identity Project on the inputs to make it work.
##########
core/src/main/java/org/apache/calcite/sql2rel/RelDecorrelator.java:
##########
@@ -1184,6 +1185,160 @@ private static void shiftMapping(Map<Integer, Integer>
mapping, int startIndex,
return null;
}
+ /**
+ * Given the SQL:
+ * 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;
+ *
+ * <p>from:
+ * LogicalProject(ENAME=[$1], CORRELATED_SUM=[$8])
+ * LogicalCorrelate(correlation=[$cor0], joinType=[left],
requiredColumns=[{2, 7}])
+ * LogicalTableScan(table=[[scott, EMP]])
+ * LogicalAggregate(group=[{}], EXPR$0=[SUM($0)])
+ * LogicalUnion(all=[true])
+ * LogicalProject(C=[CAST($0):INTEGER NOT NULL])
+ * LogicalFilter(condition=[=($0, $cor0.DEPTNO)])
+ * LogicalTableScan(table=[[scott, DEPT]])
+ * LogicalProject(C=[2])
+ * LogicalFilter(condition=[=($1, $cor0.JOB)])
+ * LogicalTableScan(table=[[scott, BONUS]])
+ *
+ * <p>to:
+ * LogicalProject(ENAME=[$1], CORRELATED_SUM=[$10])
+ * LogicalJoin(condition=[AND(IS NOT DISTINCT FROM($2, $8),
Review Comment:
this left join is not created in this function, and this code does not seem
to check that the parent of the setop is an aggregate.
so maybe you can emphasize in these two plans just the actual part that is
being rewritten by this function?
are there any constraints on the parent operation of the setop?
--
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]