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

xiong 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 1204f4231a [CALCITE-7355] RelToSqlConverter throws exception when the 
join condition contains a correlated subquery
1204f4231a is described below

commit 1204f4231a70cf8d0521534899dc0f0a32321010
Author: Xiong Duan <[email protected]>
AuthorDate: Tue Jan 6 16:15:41 2026 +0800

    [CALCITE-7355] RelToSqlConverter throws exception when the join condition 
contains a correlated subquery
---
 .../calcite/rel/rel2sql/RelToSqlConverter.java     | 15 ++++++++++++++
 .../calcite/rel/rel2sql/RelToSqlConverterTest.java | 23 ++++++++++++++++++++++
 2 files changed, 38 insertions(+)

diff --git 
a/core/src/main/java/org/apache/calcite/rel/rel2sql/RelToSqlConverter.java 
b/core/src/main/java/org/apache/calcite/rel/rel2sql/RelToSqlConverter.java
index 54b60c63d7..47834ea9d0 100644
--- a/core/src/main/java/org/apache/calcite/rel/rel2sql/RelToSqlConverter.java
+++ b/core/src/main/java/org/apache/calcite/rel/rel2sql/RelToSqlConverter.java
@@ -236,6 +236,7 @@ public Result visit(Join e) {
     final Result rightResult = visitInput(e, 1).resetAlias();
     final Context leftContext = leftResult.qualifiedContext();
     final Context rightContext = rightResult.qualifiedContext();
+    parseCorrelTable(e, leftContext.implementor().joinContext(leftContext, 
rightContext));
     final SqlNode sqlCondition;
     final JoinConditionType condType;
     JoinType joinType = joinType(e.getJoinType());
@@ -1437,6 +1438,20 @@ private void parseCorrelTable(RelNode relNode, Result x) 
{
     }
   }
 
+  /**
+   * Populate correlation table information and stores it in the correlation 
table map.
+   * Iterates through all variables in the relational node and maps each 
{@link CorrelationId}
+   * to its corresponding {@link Context} information.
+   *
+   * @param relNode The relational node containing the variable set to be 
parsed
+   * @param context The current context information used to establish mapping 
with correlation IDs
+   */
+  private void parseCorrelTable(RelNode relNode, Context context) {
+    for (CorrelationId id : relNode.getVariablesSet()) {
+      correlTableMap.put(id, context);
+    }
+  }
+
   /** Stack frame. */
   private static class Frame {
     private final RelNode parent;
diff --git 
a/core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java 
b/core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java
index 2c34707258..fb975386c0 100644
--- 
a/core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java
+++ 
b/core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java
@@ -11175,6 +11175,29 @@ private void checkLiteral2(String expression, String 
expected) {
     sql(sql).ok(expected);
   }
 
+  /** Test case of
+   * <a 
href="https://issues.apache.org/jira/browse/CALCITE-7355";>[CALCITE-7355]
+   * RelToSqlConverter throws exception
+   * when the join condition contains a correlated subquery</a>. */
+  @Test void testCorrelateInJoinCondition() {
+    final String sql = "SELECT E.EMPNO\n"
+        + "FROM EMP E\n"
+        + "JOIN DEPT D ON E.DEPTNO = D.DEPTNO\n"
+        + "AND D.DEPTNO = (\n"
+        + "  SELECT MIN(D_INNER.DEPTNO)\n"
+        + "  FROM DEPT D_INNER\n"
+        + "  WHERE D_INNER.DEPTNO = E.DEPTNO)";
+    final String expected = "SELECT \"EMP\".\"EMPNO\"\n"
+        + "FROM \"SCOTT\".\"EMP\"\n"
+        + "INNER JOIN \"SCOTT\".\"DEPT\" ON \"EMP\".\"DEPTNO\" = 
\"DEPT\".\"DEPTNO\""
+        + " AND \"DEPT\".\"DEPTNO\" = (SELECT MIN(\"DEPTNO\")\n"
+        + "FROM \"SCOTT\".\"DEPT\"\nWHERE \"DEPTNO\" = \"EMP\".\"DEPTNO\")";
+    sql(sql)
+        .schema(CalciteAssert.SchemaSpec.JDBC_SCOTT)
+        .withCalcite()
+        .ok(expected);
+  }
+
   /** Fluid interface to run tests. */
   static class Sql {
     private final CalciteAssert.SchemaSpec schemaSpec;

Reply via email to