You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@calcite.apache.org by za...@apache.org on 2019/12/29 13:29:30 UTC

[calcite] branch master updated: [CALCITE-3632] Add IntersectToIntersectUnify Rule in SubstitutionVisitor (xy2953396112)

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

zabetak 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 06b0bb4  [CALCITE-3632] Add IntersectToIntersectUnify Rule in SubstitutionVisitor (xy2953396112)
06b0bb4 is described below

commit 06b0bb4c1239ef31c9898e3acb13ff667166c35c
Author: dz <95...@qq.com>
AuthorDate: Thu Dec 26 14:53:49 2019 +0800

    [CALCITE-3632] Add IntersectToIntersectUnify Rule in SubstitutionVisitor (xy2953396112)
    
    Close apache/calicte#1692
---
 .../apache/calcite/plan/SubstitutionVisitor.java   | 30 +++++++++++++++++++++-
 .../apache/calcite/test/MaterializationTest.java   | 25 ++++++++++++++++++
 2 files changed, 54 insertions(+), 1 deletion(-)

diff --git a/core/src/main/java/org/apache/calcite/plan/SubstitutionVisitor.java b/core/src/main/java/org/apache/calcite/plan/SubstitutionVisitor.java
index 389f4f5..5398fcc 100644
--- a/core/src/main/java/org/apache/calcite/plan/SubstitutionVisitor.java
+++ b/core/src/main/java/org/apache/calcite/plan/SubstitutionVisitor.java
@@ -27,6 +27,7 @@ import org.apache.calcite.rel.mutable.Holder;
 import org.apache.calcite.rel.mutable.MutableAggregate;
 import org.apache.calcite.rel.mutable.MutableCalc;
 import org.apache.calcite.rel.mutable.MutableFilter;
+import org.apache.calcite.rel.mutable.MutableIntersect;
 import org.apache.calcite.rel.mutable.MutableJoin;
 import org.apache.calcite.rel.mutable.MutableRel;
 import org.apache.calcite.rel.mutable.MutableRelVisitor;
@@ -134,7 +135,8 @@ public class SubstitutionVisitor {
           AggregateToAggregateUnifyRule.INSTANCE,
           AggregateOnCalcToAggregateUnifyRule.INSTANCE,
           UnionToUnionUnifyRule.INSTANCE,
-          UnionOnCalcsToUnionUnifyRule.INSTANCE);
+          UnionOnCalcsToUnionUnifyRule.INSTANCE,
+          IntersectToIntersectUnifyRule.INSTANCE);
 
   /**
    * Factory for a builder for relational expressions.
@@ -1630,6 +1632,32 @@ public class SubstitutionVisitor {
     }
   }
 
+  /**
+   * A {@link SubstitutionVisitor.UnifyRule} that matches a
+   * {@link MutableIntersect} to a {@link MutableIntersect} where the query and target
+   * have the same inputs but might not have the same order.
+   */
+  private static class IntersectToIntersectUnifyRule extends AbstractUnifyRule {
+    public static final IntersectToIntersectUnifyRule INSTANCE =
+        new IntersectToIntersectUnifyRule();
+
+    private IntersectToIntersectUnifyRule() {
+      super(any(MutableIntersect.class), any(MutableIntersect.class), 0);
+    }
+
+    public UnifyResult apply(UnifyRuleCall call) {
+      final MutableIntersect query = (MutableIntersect) call.query;
+      final MutableIntersect target = (MutableIntersect) call.target;
+      final List<MutableRel> queryInputs = new ArrayList<>(query.getInputs());
+      final List<MutableRel> targetInputs = new ArrayList<>(target.getInputs());
+      if (query.isAll() == target.isAll()
+          && sameRelCollectionNoOrderConsidered(queryInputs, targetInputs)) {
+        return call.result(target);
+      }
+      return null;
+    }
+  }
+
   /** Check if list0 and list1 contains the same nodes -- order is not considered. */
   private static boolean sameRelCollectionNoOrderConsidered(
       List<MutableRel> list0, List<MutableRel> list1) {
diff --git a/core/src/test/java/org/apache/calcite/test/MaterializationTest.java b/core/src/test/java/org/apache/calcite/test/MaterializationTest.java
index 6cab20a..7aa0a8c 100644
--- a/core/src/test/java/org/apache/calcite/test/MaterializationTest.java
+++ b/core/src/test/java/org/apache/calcite/test/MaterializationTest.java
@@ -2837,6 +2837,7 @@ public class MaterializationTest {
     checkNoMaterialize(sql0 + " union " + sql1, sql0 + " union all " + sql1,
         HR_FKUK_MODEL);
   }
+
   @Test public void testUnionOnCalcsToUnion() {
     String mv = ""
         + "select \"deptno\", \"salary\"\n"
@@ -2857,6 +2858,30 @@ public class MaterializationTest {
     checkMaterialize(mv, query);
   }
 
+  @Test public void testIntersectToIntersect0() {
+    final String mv = ""
+        + "select \"deptno\" from \"emps\"\n"
+        + "intersect\n"
+        + "select \"deptno\" from \"depts\"";
+    final String query = ""
+        + "select \"deptno\" from \"depts\"\n"
+        + "intersect\n"
+        + "select \"deptno\" from \"emps\"";
+    checkMaterialize(mv, query, true);
+  }
+
+  @Test public void testIntersectToIntersect1() {
+    final String mv = ""
+        + "select \"deptno\" from \"emps\"\n"
+        + "intersect all\n"
+        + "select \"deptno\" from \"depts\"";
+    final String query = ""
+        + "select \"deptno\" from \"depts\"\n"
+        + "intersect all\n"
+        + "select \"deptno\" from \"emps\"";
+    checkMaterialize(mv, query, true);
+  }
+
   private static <E> List<List<List<E>>> list3(E[][][] as) {
     final ImmutableList.Builder<List<List<E>>> builder =
         ImmutableList.builder();