This is an automated email from the ASF dual-hosted git repository.
xuzifu666 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 1653c27149 [CALCITE-2136] SubQueryRemoveRule.Project generated error
plan
1653c27149 is described below
commit 1653c27149a4b5b204afc1e7f652de0fb0826a16
Author: Yu Xu <[email protected]>
AuthorDate: Tue Nov 17 23:09:32 2026 +0800
[CALCITE-2136] SubQueryRemoveRule.Project generated error plan
---
.../org/apache/calcite/test/RelOptRulesTest.java | 17 +++++++++++++
.../org/apache/calcite/test/RelOptRulesTest.xml | 27 +++++++++++++++++++++
core/src/test/resources/sql/sub-query.iq | 28 ++++++++++++++++++++++
3 files changed, 72 insertions(+)
diff --git a/core/src/test/java/org/apache/calcite/test/RelOptRulesTest.java
b/core/src/test/java/org/apache/calcite/test/RelOptRulesTest.java
index 307a4fc4e6..e5f1370c46 100644
--- a/core/src/test/java/org/apache/calcite/test/RelOptRulesTest.java
+++ b/core/src/test/java/org/apache/calcite/test/RelOptRulesTest.java
@@ -10314,6 +10314,23 @@ private void checkSemiJoinRuleOnAntiJoin(RelOptRule
rule) {
sql(sql).withRule(subQueryFilterRule).withLateDecorrelate(true).check();
}
+ /** Test case for
+ * <a
href="https://issues.apache.org/jira/browse/CALCITE-2136">[CALCITE-2136]
+ * SubQueryRemoveRule.Project generated error plan</a>.
+ *
+ * <p>Verifies that a correlated scalar sub-query with an aggregate in the
+ * SELECT list is rewritten into a join that declares the correlation
+ * variable. It used to be referenced by the right input but declared by no
+ * relational expression, and execution failed with "Correlation variable
+ * $cor0 should be defined". */
+ @Test void testCorrelatedScalarSubQueryInProject() {
+ final String query = "SELECT t1.ename, "
+ + "(SELECT count(1) FROM emp t2 WHERE t2.ename = t1.ename) "
+ + "FROM emp t1";
+ sql(query).withRule(CoreRules.PROJECT_SUB_QUERY_TO_CORRELATE)
+ .check();
+ }
+
/** Test case for <a
href="https://issues.apache.org/jira/browse/CALCITE-7391">[CALCITE-7391]
* FILTER_REDUCE_EXPRESSIONS crashes on expression where 123 in (SELECT NULL
FROM emps)</a>. */
@Test void testNullSelect2() {
diff --git
a/core/src/test/resources/org/apache/calcite/test/RelOptRulesTest.xml
b/core/src/test/resources/org/apache/calcite/test/RelOptRulesTest.xml
index cac85ed86d..3b60ef695e 100644
--- a/core/src/test/resources/org/apache/calcite/test/RelOptRulesTest.xml
+++ b/core/src/test/resources/org/apache/calcite/test/RelOptRulesTest.xml
@@ -2646,6 +2646,33 @@ LogicalProject(DEPTNO=[$0], DNAME=[$1], LOC=[$2])
LogicalProject(i=[true])
LogicalFilter(condition=[AND(=($cor0.DEPTNO, $7), >($5, 100))])
LogicalTableScan(table=[[scott, EMP]])
+]]>
+ </Resource>
+ </TestCase>
+ <TestCase name="testCorrelatedScalarSubQueryInProject">
+ <Resource name="sql">
+ <![CDATA[SELECT t1.ename, (SELECT count(1) FROM emp t2 WHERE t2.ename =
t1.ename) FROM emp t1]]>
+ </Resource>
+ <Resource name="planBefore">
+ <![CDATA[
+LogicalProject(variablesSet=[[$cor0]], ENAME=[$1], EXPR$1=[$SCALAR_QUERY({
+LogicalAggregate(group=[{}], EXPR$0=[COUNT()])
+ LogicalProject($f0=[1])
+ LogicalFilter(condition=[=($1, $cor0.ENAME)])
+ LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+})])
+ LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+]]>
+ </Resource>
+ <Resource name="planAfter">
+ <![CDATA[
+LogicalProject(ENAME=[$1], EXPR$1=[$9])
+ LogicalCorrelate(correlation=[$cor0], joinType=[left], requiredColumns=[{1}])
+ LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+ LogicalAggregate(group=[{}], EXPR$0=[COUNT()])
+ LogicalProject($f0=[1])
+ LogicalFilter(condition=[=($1, $cor0.ENAME)])
+ LogicalTableScan(table=[[CATALOG, SALES, EMP]])
]]>
</Resource>
</TestCase>
diff --git a/core/src/test/resources/sql/sub-query.iq
b/core/src/test/resources/sql/sub-query.iq
index 82673b67cf..e37b5f2dfe 100644
--- a/core/src/test/resources/sql/sub-query.iq
+++ b/core/src/test/resources/sql/sub-query.iq
@@ -11297,6 +11297,34 @@ order by e.ename;
!ok
+# [CALCITE-2136] SubQueryRemoveRule.Project generated error plan
+# This sql program was validated in PostgreSQL
+select t1.ename as name,
+ (select count(1) from emp t2 where t2.ename = t1.ename) as c
+from emp t1
+order by t1.ename;
++--------+---+
+| NAME | C |
++--------+---+
+| ADAMS | 1 |
+| ALLEN | 1 |
+| BLAKE | 1 |
+| CLARK | 1 |
+| FORD | 1 |
+| JAMES | 1 |
+| JONES | 1 |
+| KING | 1 |
+| MARTIN | 1 |
+| MILLER | 1 |
+| SCOTT | 1 |
+| SMITH | 1 |
+| TURNER | 1 |
+| WARD | 1 |
++--------+---+
+(14 rows)
+
+!ok
+
# [CALCITE-7930] Correlated sub-query with ROLLUP or GROUPING SETS loses the
# () group when the sub-query input is empty
# The next 4 queries belong to CALCITE-7930: 2 queries, each run with