You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@calcite.apache.org by "mihaibudiu (via GitHub)" <gi...@apache.org> on 2026/01/01 02:27:36 UTC
Re: [PR] [CALCITE-7348] Remove redundant extraction correlation variables when… [calcite]
mihaibudiu commented on code in PR #4714:
URL: https://github.com/apache/calcite/pull/4714#discussion_r2656046064
##########
core/src/test/resources/sql/scalar.iq:
##########
@@ -310,4 +310,97 @@ select
!ok
+# [CALCITE-7348] Remove redundant extraction correlation variables when Trim Project Fields
+
+!set trimfields true
+
+SELECT empno, (SELECT COUNT(*) AS c
+FROM "scott".emp
+WHERE mgr < "t".mgr) AS "$f1"
+FROM "scott".emp as "t"
+WHERE sal > 10;
++-------+-----+
+| EMPNO | $f1 |
++-------+-----+
+| 7369 | 12 |
+| 7499 | 2 |
+| 7521 | 2 |
+| 7566 | 9 |
+| 7654 | 2 |
+| 7698 | 9 |
+| 7782 | 9 |
+| 7788 | 0 |
+| 7839 | 0 |
+| 7844 | 2 |
+| 7876 | 8 |
+| 7900 | 2 |
+| 7902 | 0 |
+| 7934 | 7 |
++-------+-----+
+(14 rows)
+
+!ok
+EnumerableCalc(expr#0..3=[{inputs}], expr#4=[IS NULL($t3)], expr#5=[0:BIGINT], expr#6=[CASE($t4, $t5, $t3)], EMPNO=[$t0], $f1=[$t6])
Review Comment:
Reading these plans requires superhuman attention.
Are they really necessary?
##########
core/src/main/java/org/apache/calcite/sql2rel/RelFieldTrimmer.java:
##########
@@ -589,14 +573,14 @@ public TrimResult trimFields(
final List<RexNode> newProjects = new ArrayList<>();
final RexVisitor<RexNode> shuttle;
- if (!correlationIds.isEmpty()) {
- assert correlationIds.size() == 1;
+ if (!project.getVariablesSet().isEmpty()) {
shuttle = new RexPermuteInputsShuttle(inputMapping, newInput) {
@Override public RexNode visitSubQuery(RexSubQuery subQuery) {
subQuery = (RexSubQuery) super.visitSubQuery(subQuery);
return RelOptUtil.remapCorrelatesInSuqQuery(relBuilder.getRexBuilder(),
- subQuery, correlationIds.iterator().next(), newInput.getRowType(), inputMapping);
+ subQuery, project.getVariablesSet().iterator().next(),
Review Comment:
There is an implicit assumption that the project has exactly one variable.
Maybe this pattern to extract the variable could be factored into a method of the Project, which also asserts that there is exactly one such variable.
--
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: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org