This is an automated email from the ASF dual-hosted git repository.

silundong pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/calcite.git


The following commit(s) were added to refs/heads/main by this push:
     new 82e5ae7072 [CALCITE-7482] Wrong variablesSet used when rewriting 
subquery in JOIN ON clause
82e5ae7072 is described below

commit 82e5ae70720de17a010d3f39be1119b86610cdd2
Author: Silun Dong <[email protected]>
AuthorDate: Tue Apr 21 10:43:47 2026 +0800

    [CALCITE-7482] Wrong variablesSet used when rewriting subquery in JOIN ON 
clause
---
 .../java/org/apache/calcite/prepare/Prepare.java    |  4 +++-
 .../calcite/rel/rules/SubQueryRemoveRule.java       |  1 +
 .../apache/calcite/sql2rel/RelDecorrelatorTest.java |  9 ++++-----
 core/src/test/resources/sql/sub-query.iq            | 21 +++++++++++++++++++++
 4 files changed, 29 insertions(+), 6 deletions(-)

diff --git a/core/src/main/java/org/apache/calcite/prepare/Prepare.java 
b/core/src/main/java/org/apache/calcite/prepare/Prepare.java
index 1c6d8875cb..1f2e96688a 100644
--- a/core/src/main/java/org/apache/calcite/prepare/Prepare.java
+++ b/core/src/main/java/org/apache/calcite/prepare/Prepare.java
@@ -297,7 +297,9 @@ public PreparedResult prepareSql(
     // storage.
     root = root.withRel(flattenTypes(root.rel, true));
 
-    if (this.context.config().forceDecorrelate()) {
+    // TopDownGeneralDecorrelator cannot be used until the subquerys are 
completely removed.
+    if (this.context.config().forceDecorrelate()
+        && !this.context.config().topDownGeneralDecorrelationEnabled()) {
       // Sub-query decorrelation.
       root = root.withRel(decorrelate(sqlToRelConverter, sqlQuery, root.rel));
     }
diff --git 
a/core/src/main/java/org/apache/calcite/rel/rules/SubQueryRemoveRule.java 
b/core/src/main/java/org/apache/calcite/rel/rules/SubQueryRemoveRule.java
index 43d5adc27e..f7a6ce552f 100644
--- a/core/src/main/java/org/apache/calcite/rel/rules/SubQueryRemoveRule.java
+++ b/core/src/main/java/org/apache/calcite/rel/rules/SubQueryRemoveRule.java
@@ -1041,6 +1041,7 @@ private static void matchJoin(SubQueryRemoveRule rule, 
RelOptRuleCall call) {
     //
     // In such a case $cor0.DNAME need to be accounted as input form left side.
     final Set<CorrelationId> variablesSet = RelOptUtil.getVariablesUsed(e.rel);
+    variablesSet.retainAll(join.getVariablesSet());
     for (CorrelationId id : variablesSet) {
       ImmutableBitSet requiredColumns = RelOptUtil.correlationColumns(id, 
e.rel);
       inputSet = ImmutableBitSet.union(ImmutableList.of(requiredColumns, 
inputSet));
diff --git 
a/core/src/test/java/org/apache/calcite/sql2rel/RelDecorrelatorTest.java 
b/core/src/test/java/org/apache/calcite/sql2rel/RelDecorrelatorTest.java
index e91b138861..99b7453192 100644
--- a/core/src/test/java/org/apache/calcite/sql2rel/RelDecorrelatorTest.java
+++ b/core/src/test/java/org/apache/calcite/sql2rel/RelDecorrelatorTest.java
@@ -2099,8 +2099,7 @@ public static Frameworks.ConfigBuilder config() {
         + "      LogicalProject(EMPNO=[$0], ENAME=[$1], JOB=[$2], MGR=[$3], 
HIREDATE=[$4],"
         + " SAL=[$5], COMM=[$6], DEPTNO=[$7], DEPTNO0=[$9], DNAME=[$10], 
LOC=[$11])\n"
         + "        LogicalJoin(condition=[true], joinType=[inner])\n"
-        + "          LogicalJoin(condition=[true], joinType=[inner],"
-        + " variablesSet=[[$cor0, $cor1]])\n"
+        + "          LogicalCorrelate(correlation=[$cor0], joinType=[inner], 
requiredColumns=[{0}])\n"
         + "            LogicalTableScan(table=[[scott, EMP]])\n"
         + "            LogicalAggregate(group=[{0}])\n"
         + "              LogicalProject(i=[true])\n"
@@ -2113,11 +2112,11 @@ public static Frameworks.ConfigBuilder config() {
         RelDecorrelator.decorrelateQuery(afterJoinSubqueryCorrelate, builder,
             RuleSets.ofList(Collections.emptyList()), 
RuleSets.ofList(Collections.emptyList()));
     final String planAfterDecorrelation = "LogicalProject(EMPNO=[$0])\n"
-        + "  LogicalJoin(condition=[=($1, $10)], joinType=[inner])\n"
+        + "  LogicalJoin(condition=[=($1, $9)], joinType=[inner])\n"
         + "    LogicalTableScan(table=[[scott, EMP]])\n"
-        + "    LogicalProject(DEPTNO=[$11], EMPNO0=[$8], ENAME0=[$9])\n"
+        + "    LogicalProject(DEPTNO=[$11], ENAME0=[$9])\n"
         + "      LogicalJoin(condition=[true], joinType=[inner])\n"
-        + "        LogicalJoin(condition=[true], joinType=[inner])\n"
+        + "        LogicalJoin(condition=[=($0, $8)], joinType=[inner])\n"
         + "          LogicalTableScan(table=[[scott, EMP]])\n"
         + "          LogicalProject(EMPNO=[$0], ENAME=[$1], $f2=[true])\n"
         + "            LogicalFilter(condition=[IS NOT NULL($1)])\n"
diff --git a/core/src/test/resources/sql/sub-query.iq 
b/core/src/test/resources/sql/sub-query.iq
index b9d4774cef..1a79635e64 100644
--- a/core/src/test/resources/sql/sub-query.iq
+++ b/core/src/test/resources/sql/sub-query.iq
@@ -9183,5 +9183,26 @@ SELECT 1 FROM emp where sal > 200 and 
emp.deptno=dept.deptno);
 +--------+
 (3 rows)
 
+!ok
+
+WITH t1(id, sal) as (VALUES (1, 10), (2, 20), (3, 30)),
+t2(id, sal) as (VALUES (2, 20), (2, 200), (3, 30)),
+t3(id, sal) as (VALUES (3, 30), (4, 40)),
+t4(id, sal) as (VALUES (2, 200), (5, 50))
+select * from
+t1,
+lateral(
+  select t2.id, t2.sal from t2 join t3 on exists(
+    select t4.id from t4 where t4.id=t1.id and t4.sal=t2.sal
+  )
+);
++----+-----+-----+------+
+| ID | SAL | ID0 | SAL0 |
++----+-----+-----+------+
+|  2 |  20 |   2 |  200 |
+|  2 |  20 |   2 |  200 |
++----+-----+-----+------+
+(2 rows)
+
 !ok
 # End sub-query.iq

Reply via email to