You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@calcite.apache.org by hy...@apache.org on 2019/04/05 04:50:07 UTC

[calcite] branch master updated: [CALCITE-2954] SubQueryJoinRemoveRule and SubQueryProjectRemoveRule passing on empty set instead of set of correlation id (Vineet Garg)

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 6d57ff9  [CALCITE-2954] SubQueryJoinRemoveRule and SubQueryProjectRemoveRule passing on empty set instead of set of correlation id (Vineet Garg)
6d57ff9 is described below

commit 6d57ff983cc8e1bc0db4b8422a5140184dba3053
Author: Vineet Garg <vg...@apache.org>
AuthorDate: Tue Mar 26 22:36:51 2019 -0700

    [CALCITE-2954] SubQueryJoinRemoveRule and SubQueryProjectRemoveRule passing on empty set instead of set of correlation id (Vineet Garg)
    
    Close #1135
---
 .../calcite/rel/rules/SubQueryRemoveRule.java      |  9 +++--
 .../org/apache/calcite/test/RelOptRulesTest.java   |  9 +++++
 .../org/apache/calcite/test/RelOptRulesTest.xml    | 39 ++++++++++++++++++++++
 3 files changed, 54 insertions(+), 3 deletions(-)

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 9eebeb3..501e46f 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
@@ -47,7 +47,6 @@ import org.apache.calcite.util.Pair;
 import org.apache.calcite.util.Util;
 
 import com.google.common.collect.ImmutableList;
-import com.google.common.collect.ImmutableSet;
 
 import java.util.ArrayList;
 import java.util.List;
@@ -510,7 +509,9 @@ public abstract class SubQueryRemoveRule extends RelOptRule {
               project.getProjects(), e);
       builder.push(project.getInput());
       final int fieldCount = builder.peek().getRowType().getFieldCount();
-      final RexNode target = apply(e, ImmutableSet.of(),
+      final Set<CorrelationId>  variablesSet =
+          RelOptUtil.getVariablesUsed(e.rel);
+      final RexNode target = apply(e, variablesSet,
           logic, builder, 1, fieldCount);
       final RexShuttle shuttle = new ReplaceSubQueryShuttle(e, target);
       builder.project(shuttle.apply(project.getProjects()),
@@ -577,7 +578,9 @@ public abstract class SubQueryRemoveRule extends RelOptRule {
       builder.push(join.getLeft());
       builder.push(join.getRight());
       final int fieldCount = join.getRowType().getFieldCount();
-      final RexNode target = apply(e, ImmutableSet.of(),
+      final Set<CorrelationId>  variablesSet =
+          RelOptUtil.getVariablesUsed(e.rel);
+      final RexNode target = apply(e, variablesSet,
           logic, builder, 2, fieldCount);
       final RexShuttle shuttle = new ReplaceSubQueryShuttle(e, target);
       builder.join(join.getJoinType(), shuttle.apply(join.getCondition()));
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 341b891..5e87fc8 100644
--- a/core/src/test/java/org/apache/calcite/test/RelOptRulesTest.java
+++ b/core/src/test/java/org/apache/calcite/test/RelOptRulesTest.java
@@ -4071,6 +4071,15 @@ public class RelOptRulesTest extends RelOptTestBase {
     checkSubQuery(sql).check();
   }
 
+  @Test public void testSelectNotInCorrelated() {
+    final String sql = "select sal, \n"
+        + " empno NOT IN (\n"
+        + " select deptno from dept \n"
+        + "   where emp.job=dept.name) \n"
+        + " from emp";
+    checkSubQuery(sql).withLateDecorrelation(true).check();
+  }
+
   /** Test case for
    * <a href="https://issues.apache.org/jira/browse/CALCITE-1493">[CALCITE-1493]
    * Wrong plan for NOT IN correlated queries</a>. */
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 39835de..582ab00 100644
--- a/core/src/test/resources/org/apache/calcite/test/RelOptRulesTest.xml
+++ b/core/src/test/resources/org/apache/calcite/test/RelOptRulesTest.xml
@@ -7287,6 +7287,45 @@ LogicalProject(EMPNO=[$0], D=[$9])
 ]]>
         </Resource>
     </TestCase>
+    <TestCase name="testSelectNotInCorrelated">
+        <Resource name="sql">
+            <![CDATA[select sal,
+ empno NOT IN (
+ select deptno from dept
+   where emp.job=dept.name)
+ from emp]]>
+        </Resource>
+        <Resource name="planBefore">
+            <![CDATA[
+LogicalProject(SAL=[$5], EXPR$1=[NOT(IN($0, {
+LogicalProject(DEPTNO=[$0])
+  LogicalFilter(condition=[=($cor0.JOB, $1)])
+    LogicalTableScan(table=[[CATALOG, SALES, DEPT]])
+}))])
+  LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+]]>
+        </Resource>
+        <Resource name="planMid">
+            <![CDATA[
+LogicalProject(SAL=[$5], EXPR$1=[IS NULL($10)])
+  LogicalCorrelate(correlation=[$cor0], joinType=[left], requiredColumns=[{2}])
+    LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+    LogicalFilter(condition=[=($cor0.EMPNO, $0)])
+      LogicalProject(DEPTNO=[$0], i=[true])
+        LogicalFilter(condition=[=($cor0.JOB, $1)])
+          LogicalTableScan(table=[[CATALOG, SALES, DEPT]])
+]]>
+        </Resource>
+        <Resource name="planAfter">
+            <![CDATA[
+LogicalProject(SAL=[$5], EXPR$1=[IS NULL($10)])
+  LogicalJoin(condition=[AND(=($0, $9), =($2, $11))], joinType=[left])
+    LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+    LogicalProject(DEPTNO=[$0], i=[true], NAME=[$1])
+      LogicalTableScan(table=[[CATALOG, SALES, DEPT]])
+]]>
+        </Resource>
+    </TestCase>
     <TestCase name="testSome">
         <Resource name="sql">
             <![CDATA[select * from emp e1