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 4b6fb668ef [CALCITE-6870] The FilterToCalcRule/ProjectToCalcRule 
should not convert a Filter/Project to Calc when it contains Subquery
4b6fb668ef is described below

commit 4b6fb668ef46549371669507edfe36f937f3ef0f
Author: Xiong Duan <[email protected]>
AuthorDate: Tue Mar 4 09:54:31 2025 +0800

    [CALCITE-6870] The FilterToCalcRule/ProjectToCalcRule should not convert a 
Filter/Project to Calc when it contains Subquery
---
 .../apache/calcite/rel/rules/FilterToCalcRule.java |  6 ++++-
 .../calcite/rel/rules/ProjectToCalcRule.java       |  3 ++-
 .../org/apache/calcite/test/RelOptRulesTest.java   | 19 ++++++++++++++
 .../org/apache/calcite/test/RelOptRulesTest.xml    | 29 ++++++++++++++++++++++
 4 files changed, 55 insertions(+), 2 deletions(-)

diff --git 
a/core/src/main/java/org/apache/calcite/rel/rules/FilterToCalcRule.java 
b/core/src/main/java/org/apache/calcite/rel/rules/FilterToCalcRule.java
index cdeffc0fa6..e5aff4ac9f 100644
--- a/core/src/main/java/org/apache/calcite/rel/rules/FilterToCalcRule.java
+++ b/core/src/main/java/org/apache/calcite/rel/rules/FilterToCalcRule.java
@@ -25,6 +25,7 @@
 import org.apache.calcite.rex.RexBuilder;
 import org.apache.calcite.rex.RexProgram;
 import org.apache.calcite.rex.RexProgramBuilder;
+import org.apache.calcite.rex.RexUtil;
 import org.apache.calcite.tools.RelBuilderFactory;
 
 import org.immutables.value.Value;
@@ -85,7 +86,10 @@ public FilterToCalcRule(RelBuilderFactory relBuilderFactory) 
{
   public interface Config extends RelRule.Config {
     Config DEFAULT = ImmutableFilterToCalcRule.Config.of()
         .withOperandSupplier(b ->
-            b.operand(LogicalFilter.class).anyInputs());
+            b.operand(LogicalFilter.class)
+                .predicate(filter ->
+                    !RexUtil.SubQueryFinder.containsSubQuery(filter))
+                .anyInputs());
 
     @Override default FilterToCalcRule toRule() {
       return new FilterToCalcRule(this);
diff --git 
a/core/src/main/java/org/apache/calcite/rel/rules/ProjectToCalcRule.java 
b/core/src/main/java/org/apache/calcite/rel/rules/ProjectToCalcRule.java
index f6a9b9eda2..17ea600362 100644
--- a/core/src/main/java/org/apache/calcite/rel/rules/ProjectToCalcRule.java
+++ b/core/src/main/java/org/apache/calcite/rel/rules/ProjectToCalcRule.java
@@ -79,7 +79,8 @@ public interface Config extends RelRule.Config {
     Config DEFAULT = ImmutableProjectToCalcRule.Config.of()
         .withOperandSupplier(b ->
             b.operand(LogicalProject.class)
-                .predicate(RexUtil.M2V_FINDER::notInProject)
+                .predicate(project -> RexUtil.M2V_FINDER.notInProject(project)
+                    && !RexUtil.SubQueryFinder.containsSubQuery(project))
                 .anyInputs());
 
     @Override default ProjectToCalcRule toRule() {
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 cd15f88ac8..baf8fd7183 100644
--- a/core/src/test/java/org/apache/calcite/test/RelOptRulesTest.java
+++ b/core/src/test/java/org/apache/calcite/test/RelOptRulesTest.java
@@ -9302,6 +9302,25 @@ public interface Config extends RelRule.Config {
         .check();
   }
 
+  /**
+   * Test case for
+   * <a 
href="https://issues.apache.org/jira/browse/CALCITE-6870";>[CALCITE-6870]
+   * The FilterToCalcRule/ProjectToCalcRule should not convert a 
Filter/Project to Calc
+   * when it contains Subquery</a>. */
+  @Test void testFilterToCalc() {
+    final String sql = "select ename from emp where sal > all (select comm 
from emp)";
+    sql(sql)
+        .withRule(CoreRules.FILTER_TO_CALC)
+        .checkUnchanged();
+  }
+
+  @Test void testProjectToCalc() {
+    final String sql = "select  sal > all (select comm from emp) from emp";
+    sql(sql)
+        .withRule(CoreRules.PROJECT_TO_CALC)
+        .checkUnchanged();
+  }
+
   @Test void testEnumerableCalcRule() {
     final String sql = "select FNAME, LNAME\n"
         + "from SALES.CUSTOMER\n"
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 1cb6c34679..153864230c 100644
--- a/core/src/test/resources/org/apache/calcite/test/RelOptRulesTest.xml
+++ b/core/src/test/resources/org/apache/calcite/test/RelOptRulesTest.xml
@@ -5323,6 +5323,21 @@ LogicalProject(DEPTNO=[$7])
   Sample(mode=[system], rate=[0.5], repeatableSeed=[10])
     LogicalFilter(condition=[>($7, 10)])
       LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+]]>
+    </Resource>
+  </TestCase>
+  <TestCase name="testFilterToCalc">
+    <Resource name="sql">
+      <![CDATA[select ename from emp where sal > all (select comm from emp)]]>
+    </Resource>
+    <Resource name="planBefore">
+      <![CDATA[
+LogicalProject(ENAME=[$1])
+  LogicalFilter(condition=[NOT(<= SOME($5, {
+LogicalProject(COMM=[$6])
+  LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+}))])
+    LogicalTableScan(table=[[CATALOG, SALES, EMP]])
 ]]>
     </Resource>
   </TestCase>
@@ -9162,6 +9177,20 @@ LogicalProject(JOB=[$0], EXPR$1=[SUM($2) OVER (PARTITION 
BY $1)])
     LogicalProject(JOB=[$2], DEPTNO=[$7], EXPR$0=[+($5, 100)])
       LogicalProject(EMPNO=[$0], ENAME=[$1], JOB=[$2], MGR=[$3], 
HIREDATE=[$4], SAL=[$5], COMM=[$6], DEPTNO=[$7], SLACKER=[$8])
         LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+]]>
+    </Resource>
+  </TestCase>
+  <TestCase name="testProjectToCalc">
+    <Resource name="sql">
+      <![CDATA[select  sal > all (select comm from emp) from emp]]>
+    </Resource>
+    <Resource name="planBefore">
+      <![CDATA[
+LogicalProject(EXPR$0=[NOT(<= SOME($5, {
+LogicalProject(COMM=[$6])
+  LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+}))])
+  LogicalTableScan(table=[[CATALOG, SALES, EMP]])
 ]]>
     </Resource>
   </TestCase>

Reply via email to