[CALCITE-1524] Add a project to the planner root so that rules know which 
output fields are used


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

Branch: refs/heads/master
Commit: 751e2b0c6d6e19bef7f07b1e0acf1ce491b59311
Parents: 46654ad
Author: Julian Hyde <jh...@apache.org>
Authored: Thu Dec 1 17:51:24 2016 -0800
Committer: Julian Hyde <jh...@apache.org>
Committed: Thu Dec 1 20:10:18 2016 -0800

----------------------------------------------------------------------
 .../org/apache/calcite/plan/RelOptMaterialization.java  |  2 ++
 .../main/java/org/apache/calcite/prepare/Prepare.java   |  7 +++++--
 core/src/main/java/org/apache/calcite/rel/RelRoot.java  | 12 +++++++++++-
 3 files changed, 18 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/calcite/blob/751e2b0c/core/src/main/java/org/apache/calcite/plan/RelOptMaterialization.java
----------------------------------------------------------------------
diff --git 
a/core/src/main/java/org/apache/calcite/plan/RelOptMaterialization.java 
b/core/src/main/java/org/apache/calcite/plan/RelOptMaterialization.java
index df3fac5..7759e7b 100644
--- a/core/src/main/java/org/apache/calcite/plan/RelOptMaterialization.java
+++ b/core/src/main/java/org/apache/calcite/plan/RelOptMaterialization.java
@@ -31,6 +31,7 @@ import org.apache.calcite.rel.rules.FilterJoinRule;
 import org.apache.calcite.rel.rules.JoinProjectTransposeRule;
 import org.apache.calcite.rel.rules.ProjectFilterTransposeRule;
 import org.apache.calcite.rel.rules.ProjectMergeRule;
+import org.apache.calcite.rel.rules.ProjectRemoveRule;
 import org.apache.calcite.rex.RexNode;
 import org.apache.calcite.rex.RexUtil;
 import org.apache.calcite.schema.Table;
@@ -272,6 +273,7 @@ public class RelOptMaterialization {
             JoinProjectTransposeRule.RIGHT_PROJECT,
             JoinProjectTransposeRule.LEFT_PROJECT,
             FilterJoinRule.FilterIntoJoinRule.FILTER_ON_JOIN,
+            ProjectRemoveRule.INSTANCE,
             ProjectMergeRule.INSTANCE),
         false,
         DefaultRelMetadataProvider.INSTANCE);

http://git-wip-us.apache.org/repos/asf/calcite/blob/751e2b0c/core/src/main/java/org/apache/calcite/prepare/Prepare.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/calcite/prepare/Prepare.java 
b/core/src/main/java/org/apache/calcite/prepare/Prepare.java
index 2d97bb9..2064567 100644
--- a/core/src/main/java/org/apache/calcite/prepare/Prepare.java
+++ b/core/src/main/java/org/apache/calcite/prepare/Prepare.java
@@ -122,12 +122,15 @@ public abstract class Prepare {
    * @param lattices Lattices
    * @return an equivalent optimized relational expression
    */
-  protected RelRoot optimize(final RelRoot root,
+  protected RelRoot optimize(RelRoot root,
       final List<Materialization> materializations,
       final List<CalciteSchema.LatticeEntry> lattices) {
     final RelOptPlanner planner = root.rel.getCluster().getPlanner();
 
-    planner.setRoot(root.rel);
+    // Add a project to the root. Even if the project is trivial, it informs
+    // rules firing on the relational expression below it which of the fields
+    // are used. SemiJoinRule, for instance.
+    planner.setRoot(root.project(true));
 
     final RelTraitSet desiredTraits = getDesiredRootTraitSet(root);
     final Program program = getProgram();

http://git-wip-us.apache.org/repos/asf/calcite/blob/751e2b0c/core/src/main/java/org/apache/calcite/rel/RelRoot.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/calcite/rel/RelRoot.java 
b/core/src/main/java/org/apache/calcite/rel/RelRoot.java
index 82506d2..a2fa71a 100644
--- a/core/src/main/java/org/apache/calcite/rel/RelRoot.java
+++ b/core/src/main/java/org/apache/calcite/rel/RelRoot.java
@@ -140,7 +140,17 @@ public class RelRoot {
   /** Returns the root relational expression, creating a {@link LogicalProject}
    * if necessary to remove fields that are not needed. */
   public RelNode project() {
-    if (isRefTrivial()) {
+    return project(false);
+  }
+
+  /** Returns the root relational expression as a {@link LogicalProject}.
+   *
+   * @param force Create a Project even if all fields are used */
+  public RelNode project(boolean force) {
+    if (isRefTrivial()
+        && (SqlKind.DML.contains(kind)
+            || !force
+            || rel instanceof LogicalProject)) {
       return rel;
     }
     final List<RexNode> projects = new ArrayList<>();

Reply via email to