Repository: calcite
Updated Branches:
  refs/heads/master 97df1acbe -> 5c4554758


[CALCITE-891] When substituting materializations, match TableScan without 
Project (Maryann Xue)


Project: http://git-wip-us.apache.org/repos/asf/calcite/repo
Commit: http://git-wip-us.apache.org/repos/asf/calcite/commit/5c455475
Tree: http://git-wip-us.apache.org/repos/asf/calcite/tree/5c455475
Diff: http://git-wip-us.apache.org/repos/asf/calcite/diff/5c455475

Branch: refs/heads/master
Commit: 5c45547582d8a05fc929c97d0930122a15817fdd
Parents: 97df1ac
Author: maryannxue <[email protected]>
Authored: Thu Oct 29 11:40:00 2015 -0700
Committer: Julian Hyde <[email protected]>
Committed: Thu Oct 29 21:20:11 2015 -0700

----------------------------------------------------------------------
 .../calcite/plan/SubstitutionVisitor.java       | 38 ++++++++++++++++++++
 .../calcite/test/MaterializationTest.java       | 13 +++++++
 2 files changed, 51 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/calcite/blob/5c455475/core/src/main/java/org/apache/calcite/plan/SubstitutionVisitor.java
----------------------------------------------------------------------
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 28d7b32..b52dc8a 100644
--- a/core/src/main/java/org/apache/calcite/plan/SubstitutionVisitor.java
+++ b/core/src/main/java/org/apache/calcite/plan/SubstitutionVisitor.java
@@ -147,6 +147,7 @@ public class SubstitutionVisitor {
   protected static final ImmutableList<UnifyRule> DEFAULT_RULES =
       ImmutableList.<UnifyRule>of(
 //          TrivialRule.INSTANCE,
+          ScanToProjectUnifyRule.INSTANCE,
           ProjectToProjectUnifyRule.INSTANCE,
           FilterToProjectUnifyRule.INSTANCE,
 //          ProjectToFilterUnifyRule.INSTANCE,
@@ -842,6 +843,43 @@ public class SubstitutionVisitor {
   }
 
   /** Implementation of {@link UnifyRule} that matches
+   * {@link org.apache.calcite.rel.logical.LogicalTableScan}. */
+  private static class ScanToProjectUnifyRule extends AbstractUnifyRule {
+    public static final ScanToProjectUnifyRule INSTANCE =
+        new ScanToProjectUnifyRule();
+
+    private ScanToProjectUnifyRule() {
+      super(any(MutableScan.class),
+          any(MutableProject.class), 0);
+    }
+
+    public UnifyResult apply(UnifyRuleCall call) {
+      final MutableProject target = (MutableProject) call.target;
+      final MutableScan query = (MutableScan) call.query;
+      // We do not need to check query's parent type to avoid duplication
+      // of ProjectToProjectUnifyRule or FilterToProjectUnifyRule, since
+      // SubstitutionVisitor performs a top-down match.
+      if (!query.equals(target.getInput())) {
+        return null;
+      }
+      final RexShuttle shuttle = getRexShuttle(target);
+      final RexBuilder rexBuilder = target.cluster.getRexBuilder();
+      final List<RexNode> newProjects;
+      try {
+        newProjects = (List<RexNode>)
+            shuttle.apply(rexBuilder.identityProjects(query.getRowType()));
+      } catch (MatchFailed e) {
+        return null;
+      }
+      final MutableProject newProject =
+          MutableProject.of(
+              query.getRowType(), target, newProjects);
+      final MutableRel newProject2 = MutableRels.strip(newProject);
+      return call.result(newProject2);
+    }
+  }
+
+  /** Implementation of {@link UnifyRule} that matches
    * {@link org.apache.calcite.rel.logical.LogicalProject}. */
   private static class ProjectToProjectUnifyRule extends AbstractUnifyRule {
     public static final ProjectToProjectUnifyRule INSTANCE =

http://git-wip-us.apache.org/repos/asf/calcite/blob/5c455475/core/src/test/java/org/apache/calcite/test/MaterializationTest.java
----------------------------------------------------------------------
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 07d5a44..d65804e 100644
--- a/core/src/test/java/org/apache/calcite/test/MaterializationTest.java
+++ b/core/src/test/java/org/apache/calcite/test/MaterializationTest.java
@@ -790,6 +790,19 @@ public class MaterializationTest {
   }
 
   /** Test case for
+   * <a href="https://issues.apache.org/jira/browse/CALCITE-891";>[CALCITE-891]
+   * TableScan without Project cannot be substituted by any projected
+   * materialization</a>. */
+  @Test public void testJoinMaterialization2() {
+    String q = "select *\n"
+        + "from \"emps\"\n"
+        + "join \"depts\" using (\"deptno\")";
+    final String m = "select \"deptno\", \"empid\", \"name\",\n"
+        + "\"salary\", \"commission\" from \"emps\"";
+    checkMaterialize(m, q);
+  }
+
+  /** Test case for
    * <a href="https://issues.apache.org/jira/browse/CALCITE-761";>[CALCITE-761]
    * Pre-populated materializations</a>. */
   @Test public void testPrePopulated() {

Reply via email to