You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@calcite.apache.org by GitBox <gi...@apache.org> on 2019/10/24 03:00:29 UTC

[GitHub] [calcite] jinxing64 commented on a change in pull request #1451: [CALCITE-3334] Refinement for Substitution-Based MV Matching

jinxing64 commented on a change in pull request #1451: [CALCITE-3334] Refinement for Substitution-Based MV Matching
URL: https://github.com/apache/calcite/pull/1451#discussion_r338362543
 
 

 ##########
 File path: core/src/main/java/org/apache/calcite/plan/SubstitutionVisitor.java
 ##########
 @@ -997,233 +1022,520 @@ public UnifyResult apply(UnifyRuleCall call) {
     }
   }
 
-  /** Implementation of {@link UnifyRule} that matches
-   * {@link org.apache.calcite.rel.core.TableScan}. */
-  private static class ScanToProjectUnifyRule extends AbstractUnifyRule {
-    public static final ScanToProjectUnifyRule INSTANCE =
-        new ScanToProjectUnifyRule();
+  /**
+   * A {@link SubstitutionVisitor.UnifyRule} that matches a
+   * {@link MutableScan} to a {@link MutableCalc} which has {@link MutableScan} as child.
+   */
+  private static class ScanToCalcUnifyRule extends AbstractUnifyRule {
+
+    public static final ScanToCalcUnifyRule INSTANCE = new ScanToCalcUnifyRule();
 
-    private ScanToProjectUnifyRule() {
-      super(any(MutableScan.class),
-          any(MutableProject.class), 0);
+    private ScanToCalcUnifyRule() {
+      super(
+          any(MutableScan.class),
+          operand(MutableCalc.class,
+              any(MutableScan.class)),
+          0);
     }
 
-    public UnifyResult apply(UnifyRuleCall call) {
-      final MutableProject target = (MutableProject) call.target;
+    @Override protected UnifyResult apply(UnifyRuleCall call) {
+
       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.rowType));
-      } catch (MatchFailed e) {
-        return null;
-      }
-      final MutableProject newProject =
-          MutableProject.of(query.rowType, target, newProjects);
-      final MutableRel newProject2 = MutableRels.strip(newProject);
-      return call.result(newProject2);
-    }
-  }
 
-  /** Implementation of {@link UnifyRule} that matches
-   * {@link org.apache.calcite.rel.core.Project}. */
-  private static class ProjectToProjectUnifyRule extends AbstractUnifyRule {
-    public static final ProjectToProjectUnifyRule INSTANCE =
-        new ProjectToProjectUnifyRule();
+      final MutableCalc target = (MutableCalc) call.target;
+      final MutableScan targetInput = (MutableScan) target.getInput();
+      final Pair<RexNode, List<RexNode>> targetExplained = explainCalc(target);
+      final RexNode targetCond = targetExplained.left;
+      final List<RexNode> targetProjs = targetExplained.right;
 
-    private ProjectToProjectUnifyRule() {
-      super(operand(MutableProject.class, query(0)),
-          operand(MutableProject.class, target(0)), 1);
-    }
+      final RexBuilder rexBuilder = call.getCluster().getRexBuilder();
 
-    public UnifyResult apply(UnifyRuleCall call) {
-      final MutableProject target = (MutableProject) call.target;
-      final MutableProject query = (MutableProject) call.query;
-      final RexShuttle shuttle = getRexShuttle(target);
-      final List<RexNode> newProjects;
-      try {
-        newProjects = shuttle.apply(query.projects);
-      } catch (MatchFailed e) {
-        return null;
+      if (query.equals(targetInput) && targetCond.isAlwaysTrue()) {
 
 Review comment:
   Sure ~ that would be better ~

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services