You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@calcite.apache.org by jh...@apache.org on 2015/09/03 00:16:26 UTC

[38/50] incubator-calcite git commit: [CALCITE-826] Use ProjectFactory in AggregateJoinTranposeRule and FilterJoinRule

[CALCITE-826] Use ProjectFactory in AggregateJoinTranposeRule and FilterJoinRule


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

Branch: refs/heads/branch-release
Commit: 0357573f57346e976baac9828759b0a991bd9597
Parents: 17ef593
Author: Jesus Camacho Rodriguez <jc...@apache.org>
Authored: Fri Aug 7 16:27:43 2015 +0300
Committer: Jesus Camacho Rodriguez <jc...@apache.org>
Committed: Fri Aug 7 16:27:43 2015 +0300

----------------------------------------------------------------------
 .../org/apache/calcite/plan/RelOptUtil.java     | 75 ++++++++++++++++++--
 .../apache/calcite/rel/core/RelFactories.java   |  4 +-
 .../calcite/rel/logical/LogicalProject.java     |  8 ++-
 .../rel/rules/AggregateJoinTransposeRule.java   | 38 ++++++++--
 .../calcite/rel/rules/FilterJoinRule.java       |  2 +-
 5 files changed, 112 insertions(+), 15 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-calcite/blob/0357573f/core/src/main/java/org/apache/calcite/plan/RelOptUtil.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/calcite/plan/RelOptUtil.java b/core/src/main/java/org/apache/calcite/plan/RelOptUtil.java
index d2686a0..04733cf 100644
--- a/core/src/main/java/org/apache/calcite/plan/RelOptUtil.java
+++ b/core/src/main/java/org/apache/calcite/plan/RelOptUtil.java
@@ -2497,6 +2497,11 @@ public abstract class RelOptUtil {
     return createProject(child, Mappings.asList(mapping.inverse()));
   }
 
+  public static RelNode createProject(RelNode child, Mappings.TargetMapping mapping,
+          RelFactories.ProjectFactory projectFactory) {
+    return createProject(projectFactory, child, Mappings.asList(mapping.inverse()));
+  }
+
   /** Returns whether relational expression {@code target} occurs within a
    * relational expression {@code ancestor}. */
   public static boolean contains(RelNode ancestor, final RelNode target) {
@@ -2629,7 +2634,23 @@ public abstract class RelOptUtil {
       List<Pair<RexNode, String>> projectList,
       boolean optimize) {
     return createProject(child, Pair.left(projectList), Pair.right(projectList),
-        optimize);
+        optimize, RelFactories.DEFAULT_PROJECT_FACTORY);
+  }
+
+  /**
+   * Creates a relational expression which projects a list of (expression, name)
+   * pairs.
+   *
+   * @param child             input relational expression
+   * @param projectList       list of (expression, name) pairs
+   * @param optimize          Whether to optimize
+   * @param projectFactory    Factory to create project operators
+   */
+  public static RelNode createProject(
+      RelNode child, List<Pair<RexNode, String>> projectList,
+      boolean optimize, RelFactories.ProjectFactory projectFactory) {
+    return createProject(child, Pair.left(projectList), Pair.right(projectList),
+        optimize, projectFactory);
   }
 
   /**
@@ -2669,6 +2690,33 @@ public abstract class RelOptUtil {
       List<? extends RexNode> exprs,
       List<String> fieldNames,
       boolean optimize) {
+    return createProject(child, exprs, fieldNames, optimize,
+            RelFactories.DEFAULT_PROJECT_FACTORY);
+  }
+
+  /**
+   * Creates a relational expression which projects an array of expressions,
+   * and optionally optimizes.
+   *
+   * <p>The result may not be a
+   * {@link org.apache.calcite.rel.logical.LogicalProject}. If the
+   * projection is trivial, <code>child</code> is returned directly; and future
+   * versions may return other formulations of expressions, such as
+   * {@link org.apache.calcite.rel.logical.LogicalCalc}.
+   *
+   * @param child          input relational expression
+   * @param exprs          list of expressions for the input columns
+   * @param fieldNames     aliases of the expressions, or null to generate
+   * @param optimize       Whether to return <code>child</code> unchanged if the
+   *                       projections are trivial.
+   * @param projectFactory Factory to create project operators
+   */
+  public static RelNode createProject(
+      RelNode child,
+      List<? extends RexNode> exprs,
+      List<String> fieldNames,
+      boolean optimize,
+      RelFactories.ProjectFactory projectFactory) {
     final RelOptCluster cluster = child.getCluster();
     final List<String> fieldNames2 =
         fieldNames == null
@@ -2690,7 +2738,7 @@ public abstract class RelOptUtil {
       }
       return child;
     }
-    return LogicalProject.create(child, exprs, fieldNames2);
+    return projectFactory.createProject(child, exprs, fieldNames2);
   }
 
   /**
@@ -2945,6 +2993,23 @@ public abstract class RelOptUtil {
    * @param originalJoin Join whose condition is to be pushed down
    */
   public static RelNode pushDownJoinConditions(Join originalJoin) {
+    return pushDownJoinConditions(originalJoin, RelFactories.DEFAULT_PROJECT_FACTORY);
+  }
+
+  /**
+   * Pushes down expressions in "equal" join condition.
+   *
+   * <p>For example, given
+   * "emp JOIN dept ON emp.deptno + 1 = dept.deptno", adds a project above
+   * "emp" that computes the expression
+   * "emp.deptno + 1". The resulting join condition is a simple combination
+   * of AND, equals, and input fields, plus the remaining non-equal conditions.
+   *
+   * @param originalJoin Join whose condition is to be pushed down
+   * @param projectFactory Factory to create project operator
+   */
+  public static RelNode pushDownJoinConditions(Join originalJoin,
+          RelFactories.ProjectFactory projectFactory) {
     RexNode joinCond = originalJoin.getCondition();
     final JoinRelType joinType = originalJoin.getJoinType();
     RelNode leftRel = originalJoin.getLeft();
@@ -2980,7 +3045,7 @@ public abstract class RelOptUtil {
               }
             }
           },
-          true);
+          true, projectFactory);
     }
     if (!extraRightExprs.isEmpty()) {
       final List<RelDataTypeField> fields =
@@ -3008,7 +3073,7 @@ public abstract class RelOptUtil {
               }
             }
           },
-          true);
+          true, projectFactory);
     }
 
     RelNode join = originalJoin.copy(originalJoin.getTraitSet(),
@@ -3021,7 +3086,7 @@ public abstract class RelOptUtil {
                   + rightCount + extraRightExprs.size(),
               0, 0, leftCount,
               leftCount, leftCount + extraLeftExprs.size(), rightCount);
-      return RelOptUtil.createProject(join, mapping);
+      return RelOptUtil.createProject(join, mapping, projectFactory);
     }
     return join;
   }

http://git-wip-us.apache.org/repos/asf/incubator-calcite/blob/0357573f/core/src/main/java/org/apache/calcite/rel/core/RelFactories.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/calcite/rel/core/RelFactories.java b/core/src/main/java/org/apache/calcite/rel/core/RelFactories.java
index 787ab1f..e8cbd3c 100644
--- a/core/src/main/java/org/apache/calcite/rel/core/RelFactories.java
+++ b/core/src/main/java/org/apache/calcite/rel/core/RelFactories.java
@@ -20,7 +20,6 @@ package org.apache.calcite.rel.core;
 import org.apache.calcite.plan.Contexts;
 import org.apache.calcite.plan.RelOptCluster;
 import org.apache.calcite.plan.RelOptTable;
-import org.apache.calcite.plan.RelOptUtil;
 import org.apache.calcite.plan.RelTraitSet;
 import org.apache.calcite.rel.RelCollation;
 import org.apache.calcite.rel.RelNode;
@@ -29,6 +28,7 @@ import org.apache.calcite.rel.logical.LogicalFilter;
 import org.apache.calcite.rel.logical.LogicalIntersect;
 import org.apache.calcite.rel.logical.LogicalJoin;
 import org.apache.calcite.rel.logical.LogicalMinus;
+import org.apache.calcite.rel.logical.LogicalProject;
 import org.apache.calcite.rel.logical.LogicalSort;
 import org.apache.calcite.rel.logical.LogicalTableScan;
 import org.apache.calcite.rel.logical.LogicalUnion;
@@ -113,7 +113,7 @@ public class RelFactories {
   private static class ProjectFactoryImpl implements ProjectFactory {
     public RelNode createProject(RelNode input,
         List<? extends RexNode> childExprs, List<String> fieldNames) {
-      return RelOptUtil.createProject(input, childExprs, fieldNames);
+      return LogicalProject.create(input, childExprs, fieldNames);
     }
   }
 

http://git-wip-us.apache.org/repos/asf/incubator-calcite/blob/0357573f/core/src/main/java/org/apache/calcite/rel/logical/LogicalProject.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/calcite/rel/logical/LogicalProject.java b/core/src/main/java/org/apache/calcite/rel/logical/LogicalProject.java
index 8b46f79..cce5e79 100644
--- a/core/src/main/java/org/apache/calcite/rel/logical/LogicalProject.java
+++ b/core/src/main/java/org/apache/calcite/rel/logical/LogicalProject.java
@@ -30,6 +30,7 @@ import org.apache.calcite.rel.metadata.RelMdCollation;
 import org.apache.calcite.rel.type.RelDataType;
 import org.apache.calcite.rex.RexNode;
 import org.apache.calcite.rex.RexUtil;
+import org.apache.calcite.sql.validate.SqlValidatorUtil;
 import org.apache.calcite.util.Util;
 
 import com.google.common.base.Supplier;
@@ -95,9 +96,14 @@ public final class LogicalProject extends Project {
   public static LogicalProject create(final RelNode input,
       final List<? extends RexNode> projects, List<String> fieldNames) {
     final RelOptCluster cluster = input.getCluster();
+    final List<String> fieldNames2 =
+        fieldNames == null
+            ? null
+            : SqlValidatorUtil.uniquify(fieldNames,
+                SqlValidatorUtil.F_SUGGESTER);
     final RelDataType rowType =
         RexUtil.createStructType(cluster.getTypeFactory(), projects,
-            fieldNames);
+            fieldNames2);
     return create(input, projects, rowType);
   }
 

http://git-wip-us.apache.org/repos/asf/incubator-calcite/blob/0357573f/core/src/main/java/org/apache/calcite/rel/rules/AggregateJoinTransposeRule.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/calcite/rel/rules/AggregateJoinTransposeRule.java b/core/src/main/java/org/apache/calcite/rel/rules/AggregateJoinTransposeRule.java
index 673d579..8dfe4aa 100644
--- a/core/src/main/java/org/apache/calcite/rel/rules/AggregateJoinTransposeRule.java
+++ b/core/src/main/java/org/apache/calcite/rel/rules/AggregateJoinTransposeRule.java
@@ -63,27 +63,51 @@ public class AggregateJoinTransposeRule extends RelOptRule {
       new AggregateJoinTransposeRule(LogicalAggregate.class,
           RelFactories.DEFAULT_AGGREGATE_FACTORY,
           LogicalJoin.class,
-          RelFactories.DEFAULT_JOIN_FACTORY);
+          RelFactories.DEFAULT_JOIN_FACTORY,
+          RelFactories.DEFAULT_PROJECT_FACTORY);
 
   /** Extended instance of the rule that can push down aggregate functions. */
   public static final AggregateJoinTransposeRule EXTENDED =
       new AggregateJoinTransposeRule(LogicalAggregate.class,
           RelFactories.DEFAULT_AGGREGATE_FACTORY,
           LogicalJoin.class,
-          RelFactories.DEFAULT_JOIN_FACTORY, true);
+          RelFactories.DEFAULT_JOIN_FACTORY,
+          RelFactories.DEFAULT_PROJECT_FACTORY, true);
 
   private final RelFactories.AggregateFactory aggregateFactory;
 
   private final RelFactories.JoinFactory joinFactory;
 
+  private final RelFactories.ProjectFactory projectFactory;
+
   private final boolean allowFunctions;
 
-  /** Creates an AggregateJoinTransposeRule. */
+  @Deprecated
   public AggregateJoinTransposeRule(Class<? extends Aggregate> aggregateClass,
       RelFactories.AggregateFactory aggregateFactory,
       Class<? extends Join> joinClass,
       RelFactories.JoinFactory joinFactory) {
-    this(aggregateClass, aggregateFactory, joinClass, joinFactory, false);
+    this(aggregateClass, aggregateFactory, joinClass, joinFactory,
+            RelFactories.DEFAULT_PROJECT_FACTORY, false);
+  }
+
+  @Deprecated
+  public AggregateJoinTransposeRule(Class<? extends Aggregate> aggregateClass,
+      RelFactories.AggregateFactory aggregateFactory,
+      Class<? extends Join> joinClass,
+      RelFactories.JoinFactory joinFactory,
+      boolean allowFunctions) {
+    this(aggregateClass, aggregateFactory, joinClass, joinFactory,
+            RelFactories.DEFAULT_PROJECT_FACTORY, allowFunctions);
+  }
+
+  /** Creates an AggregateJoinTransposeRule. */
+  public AggregateJoinTransposeRule(Class<? extends Aggregate> aggregateClass,
+      RelFactories.AggregateFactory aggregateFactory,
+      Class<? extends Join> joinClass,
+      RelFactories.JoinFactory joinFactory,
+      RelFactories.ProjectFactory projectFactory) {
+    this(aggregateClass, aggregateFactory, joinClass, joinFactory, projectFactory, false);
   }
 
   /** Creates an AggregateJoinTransposeRule that may push down functions. */
@@ -91,12 +115,14 @@ public class AggregateJoinTransposeRule extends RelOptRule {
       RelFactories.AggregateFactory aggregateFactory,
       Class<? extends Join> joinClass,
       RelFactories.JoinFactory joinFactory,
+      RelFactories.ProjectFactory projectFactory,
       boolean allowFunctions) {
     super(
         operand(aggregateClass, null, Aggregate.IS_SIMPLE,
             operand(joinClass, any())));
     this.aggregateFactory = aggregateFactory;
     this.joinFactory = joinFactory;
+    this.projectFactory = projectFactory;
     this.allowFunctions = allowFunctions;
   }
 
@@ -279,7 +305,7 @@ public class AggregateJoinTransposeRule extends RelOptRule {
     if (allColumnsInAggregate && newAggCalls.isEmpty()) {
       // no need to aggregate
     } else {
-      r = RelOptUtil.createProject(r, projects, null, true);
+      r = RelOptUtil.createProject(r, projects, null, true, projectFactory);
       if (allColumnsInAggregate) {
         // let's see if we can convert
         List<RexNode> projects2 = new ArrayList<>();
@@ -298,7 +324,7 @@ public class AggregateJoinTransposeRule extends RelOptRule {
         if (projects2.size()
             == aggregate.getGroupSet().cardinality() + newAggCalls.size()) {
           // We successfully converted agg calls into projects.
-          r = RelOptUtil.createProject(r, projects2, null, true);
+          r = RelOptUtil.createProject(r, projects2, null, true, projectFactory);
           break b;
         }
       }

http://git-wip-us.apache.org/repos/asf/incubator-calcite/blob/0357573f/core/src/main/java/org/apache/calcite/rel/rules/FilterJoinRule.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/calcite/rel/rules/FilterJoinRule.java b/core/src/main/java/org/apache/calcite/rel/rules/FilterJoinRule.java
index 12eadb8..844980a 100644
--- a/core/src/main/java/org/apache/calcite/rel/rules/FilterJoinRule.java
+++ b/core/src/main/java/org/apache/calcite/rel/rules/FilterJoinRule.java
@@ -250,7 +250,7 @@ public abstract class FilterJoinRule extends RelOptRule {
     // Push expression in join condition into Project below Join.
     if (newJoinRel instanceof Join) {
       newJoinRel = RelOptUtil.pushDownJoinConditions(
-          (Join) newJoinRel);
+          (Join) newJoinRel, projectFactory);
     }
 
     // create a FilterRel on top of the join if needed