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 a68bfc9  [CALCITE-3214] Add UnionToUnionRule for materialization 
matching (Jin Xing)
a68bfc9 is described below

commit a68bfc96bacc95b4b8ab5d185bb5cc1991113769
Author: jinxing <[email protected]>
AuthorDate: Fri Jul 26 12:34:38 2019 +0800

    [CALCITE-3214] Add UnionToUnionRule for materialization matching (Jin Xing)
---
 .../plan/MaterializedViewSubstitutionVisitor.java  | 34 ++++++++++++++++++++++
 .../apache/calcite/test/MaterializationTest.java   |  6 ++++
 2 files changed, 40 insertions(+)

diff --git 
a/core/src/main/java/org/apache/calcite/plan/MaterializedViewSubstitutionVisitor.java
 
b/core/src/main/java/org/apache/calcite/plan/MaterializedViewSubstitutionVisitor.java
index a2ff5f4..a0333d5 100644
--- 
a/core/src/main/java/org/apache/calcite/plan/MaterializedViewSubstitutionVisitor.java
+++ 
b/core/src/main/java/org/apache/calcite/plan/MaterializedViewSubstitutionVisitor.java
@@ -21,6 +21,7 @@ import org.apache.calcite.rel.mutable.MutableFilter;
 import org.apache.calcite.rel.mutable.MutableProject;
 import org.apache.calcite.rel.mutable.MutableRel;
 import org.apache.calcite.rel.mutable.MutableRels;
+import org.apache.calcite.rel.mutable.MutableUnion;
 import org.apache.calcite.rel.type.RelDataTypeField;
 import org.apache.calcite.rex.RexInputRef;
 import org.apache.calcite.rex.RexNode;
@@ -41,6 +42,7 @@ public class MaterializedViewSubstitutionVisitor extends 
SubstitutionVisitor {
           .add(ProjectToProjectUnifyRule1.INSTANCE)
           .add(FilterToFilterUnifyRule1.INSTANCE)
           .add(FilterToProjectUnifyRule1.INSTANCE)
+          .add(UnionToUnionRule.INSTANCE)
           .build();
 
   public MaterializedViewSubstitutionVisitor(RelNode target_, RelNode query_) {
@@ -182,6 +184,38 @@ public class MaterializedViewSubstitutionVisitor extends 
SubstitutionVisitor {
 
   /**
    * Implementation of {@link SubstitutionVisitor.UnifyRule} that matches a
+   * {@link MutableUnion} to a {@link MutableUnion} where the query and target
+   * have the same inputs but might not have the same order.
+   */
+  private static class UnionToUnionRule extends AbstractUnifyRule {
+    public static final UnionToUnionRule INSTANCE = new UnionToUnionRule();
+
+    private UnionToUnionRule() {
+      super(any(MutableUnion.class), any(MutableUnion.class), 0);
+    }
+
+    public UnifyResult apply(UnifyRuleCall call) {
+      final MutableUnion query = (MutableUnion) call.query;
+      final MutableUnion target = (MutableUnion) call.target;
+      List<MutableRel> queryInputs = query.getInputs();
+      List<MutableRel> targetInputs = target.getInputs();
+      if (queryInputs.size() == targetInputs.size()) {
+        for (MutableRel rel: queryInputs) {
+          int index = targetInputs.indexOf(rel);
+          if (index == -1) {
+            return null;
+          } else {
+            targetInputs.remove(index);
+          }
+        }
+        return call.result(target);
+      }
+      return null;
+    }
+  }
+
+  /**
+   * Implementation of {@link SubstitutionVisitor.UnifyRule} that matches a
    * {@link MutableFilter} to a
    * {@link MutableProject} on top of a
    * {@link MutableFilter} where the condition of the target
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 76e09cc..6fc37c8 100644
--- a/core/src/test/java/org/apache/calcite/test/MaterializationTest.java
+++ b/core/src/test/java/org/apache/calcite/test/MaterializationTest.java
@@ -2450,6 +2450,12 @@ public class MaterializationTest {
     checkMaterialize(sql, sql);
   }
 
+  @Test public void testUnionToUnion() {
+    String sql0 = "select * from \"emps\" where \"empid\" < 300";
+    String sql1 = "select * from \"emps\" where \"empid\" > 200";
+    checkMaterialize(sql0 + " union all " + sql1, sql1 + " union all " + sql0);
+  }
+
   private static <E> List<List<List<E>>> list3(E[][][] as) {
     final ImmutableList.Builder<List<List<E>>> builder =
         ImmutableList.builder();

Reply via email to