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/02/20 00:02:23 UTC

incubator-calcite git commit: [CALCITE-600] Use SetOpFactory in rules containing Union operator (Jesus Camacho Rodriguez)

Repository: incubator-calcite
Updated Branches:
  refs/heads/master f9db1ee92 -> 1ae40b005


[CALCITE-600] Use SetOpFactory in rules containing Union operator (Jesus Camacho Rodriguez)

Close apache/incubator-calcite#54


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

Branch: refs/heads/master
Commit: 1ae40b0051bff7100e415e1954d1220cd3ff62f3
Parents: f9db1ee
Author: Jesus Camacho Rodriguez <jc...@hortonworks.com>
Authored: Thu Feb 19 00:45:40 2015 +0000
Committer: julianhyde <jh...@apache.org>
Committed: Thu Feb 19 14:39:35 2015 -0800

----------------------------------------------------------------------
 .../rel/rules/AggregateUnionAggregateRule.java  | 48 ++++++++++++------
 .../rel/rules/AggregateUnionTransposeRule.java  | 53 +++++++++++++-------
 .../calcite/rel/rules/UnionEliminatorRule.java  |  9 ++--
 .../calcite/rel/rules/UnionMergeRule.java       | 31 ++++++++----
 .../calcite/rel/rules/UnionToDistinctRule.java  | 26 +++++++---
 5 files changed, 109 insertions(+), 58 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-calcite/blob/1ae40b00/core/src/main/java/org/apache/calcite/rel/rules/AggregateUnionAggregateRule.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/calcite/rel/rules/AggregateUnionAggregateRule.java b/core/src/main/java/org/apache/calcite/rel/rules/AggregateUnionAggregateRule.java
index c1b5a28..9f64f0f 100644
--- a/core/src/main/java/org/apache/calcite/rel/rules/AggregateUnionAggregateRule.java
+++ b/core/src/main/java/org/apache/calcite/rel/rules/AggregateUnionAggregateRule.java
@@ -20,8 +20,11 @@ import org.apache.calcite.plan.RelOptRule;
 import org.apache.calcite.plan.RelOptRuleCall;
 import org.apache.calcite.rel.RelNode;
 import org.apache.calcite.rel.core.Aggregate;
+import org.apache.calcite.rel.core.RelFactories;
+import org.apache.calcite.rel.core.Union;
 import org.apache.calcite.rel.logical.LogicalAggregate;
 import org.apache.calcite.rel.logical.LogicalUnion;
+import org.apache.calcite.sql.SqlKind;
 
 import com.google.common.collect.ImmutableList;
 
@@ -29,45 +32,57 @@ import java.util.List;
 
 /**
  * Planner rule that matches
- * {@link org.apache.calcite.rel.logical.LogicalAggregate}s beneath a
- * {@link org.apache.calcite.rel.logical.LogicalUnion} and pulls them up, so
+ * {@link org.apache.calcite.rel.core.Aggregate}s beneath a
+ * {@link org.apache.calcite.rel.core.Union} and pulls them up, so
  * that a single
- * {@link org.apache.calcite.rel.logical.LogicalAggregate} removes duplicates.
+ * {@link org.apache.calcite.rel.core.Aggregate} removes duplicates.
  *
  * <p>This rule only handles cases where the
- * {@link org.apache.calcite.rel.logical.LogicalUnion}s
+ * {@link org.apache.calcite.rel.core.Union}s
  * still have only two inputs.
  */
 public class AggregateUnionAggregateRule extends RelOptRule {
   public static final AggregateUnionAggregateRule INSTANCE =
-      new AggregateUnionAggregateRule();
+      new AggregateUnionAggregateRule(LogicalAggregate.class,
+          RelFactories.DEFAULT_AGGREGATE_FACTORY,
+          LogicalUnion.class,
+          RelFactories.DEFAULT_SET_OP_FACTORY);
+
+  private final RelFactories.AggregateFactory aggregateFactory;
+
+  private final RelFactories.SetOpFactory setOpFactory;
 
   //~ Constructors -----------------------------------------------------------
 
   /**
    * Creates a AggregateUnionAggregateRule.
    */
-  private AggregateUnionAggregateRule() {
+  public AggregateUnionAggregateRule(Class<? extends Aggregate> aggregateClass,
+      RelFactories.AggregateFactory aggregateFactory,
+      Class<? extends Union> unionClass,
+      RelFactories.SetOpFactory setOpFactory) {
     super(
-        operand(LogicalAggregate.class, null, Aggregate.IS_SIMPLE,
-            operand(LogicalUnion.class,
+        operand(aggregateClass, null, Aggregate.IS_SIMPLE,
+            operand(unionClass,
                 operand(RelNode.class, any()),
                 operand(RelNode.class, any()))));
+    this.aggregateFactory = aggregateFactory;
+    this.setOpFactory = setOpFactory;
   }
 
   //~ Methods ----------------------------------------------------------------
 
   // implement RelOptRule
   public void onMatch(RelOptRuleCall call) {
-    LogicalUnion union = call.rel(1);
+    Union union = call.rel(1);
 
     // If distincts haven't been removed yet, defer invoking this rule
     if (!union.all) {
       return;
     }
 
-    LogicalAggregate topAggRel = call.rel(0);
-    LogicalAggregate bottomAggRel;
+    Aggregate topAggRel = call.rel(0);
+    Aggregate bottomAggRel;
 
     // We want to apply this rule on the pattern where the LogicalAggregate
     // is the second input into the Union first.  Hence, that's why the
@@ -75,12 +90,12 @@ public class AggregateUnionAggregateRule extends RelOptRule {
     // UnionRels.  By doing so, and firing this rule in a bottom-up order,
     // it allows us to only specify a single pattern for this rule.
     List<RelNode> unionInputs;
-    if (call.rel(3) instanceof LogicalAggregate) {
+    if (call.rel(3) instanceof Aggregate) {
       bottomAggRel = call.rel(3);
       unionInputs = ImmutableList.of(
           call.rel(2),
           call.rel(3).getInput(0));
-    } else if (call.rel(2) instanceof LogicalAggregate) {
+    } else if (call.rel(2) instanceof Aggregate) {
       bottomAggRel = call.rel(2);
       unionInputs = ImmutableList.of(
           call.rel(2).getInput(0),
@@ -95,10 +110,11 @@ public class AggregateUnionAggregateRule extends RelOptRule {
       return;
     }
 
-    LogicalUnion newUnion = LogicalUnion.create(unionInputs, true);
+    RelNode newUnion = setOpFactory.createSetOp(SqlKind.UNION,
+        unionInputs, true);
 
-    LogicalAggregate newAggRel =
-        LogicalAggregate.create(newUnion, false,
+    RelNode newAggRel =
+        aggregateFactory.createAggregate(newUnion, false,
             topAggRel.getGroupSet(), null, topAggRel.getAggCallList());
 
     call.transformTo(newAggRel);

http://git-wip-us.apache.org/repos/asf/incubator-calcite/blob/1ae40b00/core/src/main/java/org/apache/calcite/rel/rules/AggregateUnionTransposeRule.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/calcite/rel/rules/AggregateUnionTransposeRule.java b/core/src/main/java/org/apache/calcite/rel/rules/AggregateUnionTransposeRule.java
index eb72876..5f78489 100644
--- a/core/src/main/java/org/apache/calcite/rel/rules/AggregateUnionTransposeRule.java
+++ b/core/src/main/java/org/apache/calcite/rel/rules/AggregateUnionTransposeRule.java
@@ -17,16 +17,19 @@
 package org.apache.calcite.rel.rules;
 
 import org.apache.calcite.linq4j.Ord;
-import org.apache.calcite.plan.RelOptCluster;
 import org.apache.calcite.plan.RelOptRule;
 import org.apache.calcite.plan.RelOptRuleCall;
 import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.core.Aggregate;
 import org.apache.calcite.rel.core.AggregateCall;
+import org.apache.calcite.rel.core.RelFactories;
+import org.apache.calcite.rel.core.Union;
 import org.apache.calcite.rel.logical.LogicalAggregate;
 import org.apache.calcite.rel.logical.LogicalUnion;
 import org.apache.calcite.rel.metadata.RelMdUtil;
 import org.apache.calcite.rel.type.RelDataType;
 import org.apache.calcite.sql.SqlAggFunction;
+import org.apache.calcite.sql.SqlKind;
 import org.apache.calcite.sql.fun.SqlCountAggFunction;
 import org.apache.calcite.sql.fun.SqlMinMaxAggFunction;
 import org.apache.calcite.sql.fun.SqlStdOperatorTable;
@@ -43,15 +46,23 @@ import java.util.Map;
 
 /**
  * Planner rule that pushes an
- * {@link org.apache.calcite.rel.logical.LogicalAggregate}
- * past a non-distinct {@link org.apache.calcite.rel.logical.LogicalUnion}.
+ * {@link org.apache.calcite.rel.core.Aggregate}
+ * past a non-distinct {@link org.apache.calcite.rel.core.Union}.
  */
 public class AggregateUnionTransposeRule extends RelOptRule {
   public static final AggregateUnionTransposeRule INSTANCE =
-      new AggregateUnionTransposeRule();
+      new AggregateUnionTransposeRule(LogicalAggregate.class,
+          RelFactories.DEFAULT_AGGREGATE_FACTORY,
+          LogicalUnion.class,
+          RelFactories.DEFAULT_SET_OP_FACTORY);
 
-  private static final Map<Class, Boolean> SUPPORTED_AGGREGATES =
-      new IdentityHashMap<Class, Boolean>();
+  private final RelFactories.AggregateFactory aggregateFactory;
+
+  private final RelFactories.SetOpFactory setOpFactory;
+
+  private static final Map<Class<? extends SqlAggFunction>, Boolean>
+  SUPPORTED_AGGREGATES =
+      new IdentityHashMap<Class<? extends SqlAggFunction>, Boolean>();
 
   static {
     SUPPORTED_AGGREGATES.put(SqlMinMaxAggFunction.class, true);
@@ -61,17 +72,22 @@ public class AggregateUnionTransposeRule extends RelOptRule {
   }
 
   /**
-   * Private constructor.
+   * Constructor.
    */
-  private AggregateUnionTransposeRule() {
+  public AggregateUnionTransposeRule(Class<? extends Aggregate> aggregateClass,
+      RelFactories.AggregateFactory aggregateFactory,
+      Class<? extends Union> unionClass,
+      RelFactories.SetOpFactory setOpFactory) {
     super(
-        operand(LogicalAggregate.class,
-            operand(LogicalUnion.class, any())));
+        operand(aggregateClass,
+            operand(unionClass, any())));
+    this.aggregateFactory = aggregateFactory;
+    this.setOpFactory = setOpFactory;
   }
 
   public void onMatch(RelOptRuleCall call) {
-    LogicalAggregate aggRel = call.rel(0);
-    LogicalUnion union = call.rel(1);
+    Aggregate aggRel = call.rel(0);
+    Union union = call.rel(1);
 
     if (!union.all) {
       // This transformation is only valid for UNION ALL.
@@ -86,8 +102,6 @@ public class AggregateUnionTransposeRule extends RelOptRule {
       return;
     }
 
-    RelOptCluster cluster = union.getCluster();
-
     int groupCount = aggRel.getGroupSet().cardinality();
 
     List<AggregateCall> transformedAggCalls =
@@ -116,8 +130,8 @@ public class AggregateUnionTransposeRule extends RelOptRule {
       } else {
         anyTransformed = true;
         newUnionInputs.add(
-            LogicalAggregate.create(input, false, aggRel.getGroupSet(),
-                null, aggRel.getAggCallList()));
+            aggregateFactory.createAggregate(input, false,
+                aggRel.getGroupSet(), null, aggRel.getAggCallList()));
       }
     }
 
@@ -129,10 +143,11 @@ public class AggregateUnionTransposeRule extends RelOptRule {
     }
 
     // create a new union whose children are the aggregates created above
-    LogicalUnion newUnion = LogicalUnion.create(newUnionInputs, true);
+    RelNode newUnion = setOpFactory.createSetOp(SqlKind.UNION,
+        newUnionInputs, true);
 
-    LogicalAggregate newTopAggRel =
-        LogicalAggregate.create(newUnion, aggRel.indicator,
+    RelNode newTopAggRel =
+        aggregateFactory.createAggregate(newUnion, aggRel.indicator,
             aggRel.getGroupSet(), aggRel.getGroupSets(), transformedAggCalls);
 
     call.transformTo(newTopAggRel);

http://git-wip-us.apache.org/repos/asf/incubator-calcite/blob/1ae40b00/core/src/main/java/org/apache/calcite/rel/rules/UnionEliminatorRule.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/calcite/rel/rules/UnionEliminatorRule.java b/core/src/main/java/org/apache/calcite/rel/rules/UnionEliminatorRule.java
index f78704b..4e908ec 100644
--- a/core/src/main/java/org/apache/calcite/rel/rules/UnionEliminatorRule.java
+++ b/core/src/main/java/org/apache/calcite/rel/rules/UnionEliminatorRule.java
@@ -18,6 +18,7 @@ package org.apache.calcite.rel.rules;
 
 import org.apache.calcite.plan.RelOptRule;
 import org.apache.calcite.plan.RelOptRuleCall;
+import org.apache.calcite.rel.core.Union;
 import org.apache.calcite.rel.logical.LogicalUnion;
 
 /**
@@ -27,21 +28,21 @@ import org.apache.calcite.rel.logical.LogicalUnion;
  */
 public class UnionEliminatorRule extends RelOptRule {
   public static final UnionEliminatorRule INSTANCE =
-      new UnionEliminatorRule();
+      new UnionEliminatorRule(LogicalUnion.class);
 
   //~ Constructors -----------------------------------------------------------
 
   /**
    * Creates a UnionEliminatorRule.
    */
-  private UnionEliminatorRule() {
-    super(operand(LogicalUnion.class, any()));
+  private UnionEliminatorRule(Class<? extends Union> clazz) {
+    super(operand(clazz, any()));
   }
 
   //~ Methods ----------------------------------------------------------------
 
   public void onMatch(RelOptRuleCall call) {
-    LogicalUnion union = call.rel(0);
+    Union union = call.rel(0);
     if (union.getInputs().size() != 1) {
       return;
     }

http://git-wip-us.apache.org/repos/asf/incubator-calcite/blob/1ae40b00/core/src/main/java/org/apache/calcite/rel/rules/UnionMergeRule.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/calcite/rel/rules/UnionMergeRule.java b/core/src/main/java/org/apache/calcite/rel/rules/UnionMergeRule.java
index bda281c..1d4cfcc 100644
--- a/core/src/main/java/org/apache/calcite/rel/rules/UnionMergeRule.java
+++ b/core/src/main/java/org/apache/calcite/rel/rules/UnionMergeRule.java
@@ -19,7 +19,10 @@ package org.apache.calcite.rel.rules;
 import org.apache.calcite.plan.RelOptRule;
 import org.apache.calcite.plan.RelOptRuleCall;
 import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.core.RelFactories;
+import org.apache.calcite.rel.core.Union;
 import org.apache.calcite.rel.logical.LogicalUnion;
+import org.apache.calcite.sql.SqlKind;
 import org.apache.calcite.util.Util;
 
 import java.util.ArrayList;
@@ -27,41 +30,46 @@ import java.util.List;
 
 /**
  * UnionMergeRule implements the rule for combining two
- * non-distinct {@link org.apache.calcite.rel.logical.LogicalUnion}s
- * into a single {@link org.apache.calcite.rel.logical.LogicalUnion}.
+ * non-distinct {@link org.apache.calcite.rel.core.Union}s
+ * into a single {@link org.apache.calcite.rel.core.Union}.
  */
 public class UnionMergeRule extends RelOptRule {
   public static final UnionMergeRule INSTANCE =
-      new UnionMergeRule();
+      new UnionMergeRule(LogicalUnion.class,
+          RelFactories.DEFAULT_SET_OP_FACTORY);
+
+  private final RelFactories.SetOpFactory setOpFactory;
 
   //~ Constructors -----------------------------------------------------------
 
   /**
    * Creates a UnionMergeRule.
    */
-  private UnionMergeRule() {
+  public UnionMergeRule(Class<? extends Union> clazz,
+      RelFactories.SetOpFactory setOpFactory) {
     super(
         operand(
-            LogicalUnion.class,
+            clazz,
             operand(RelNode.class, any()),
             operand(RelNode.class, any())));
+    this.setOpFactory = setOpFactory;
   }
 
   //~ Methods ----------------------------------------------------------------
 
   // implement RelOptRule
   public void onMatch(RelOptRuleCall call) {
-    LogicalUnion topUnion = call.rel(0);
-    LogicalUnion bottomUnion;
+    Union topUnion = call.rel(0);
+    Union bottomUnion;
 
     // We want to combine the Union that's in the second input first.
     // Hence, that's why the rule pattern matches on generic RelNodes
     // rather than explicit UnionRels.  By doing so, and firing this rule
     // in a bottom-up order, it allows us to only specify a single
     // pattern for this rule.
-    if (call.rel(2) instanceof LogicalUnion) {
+    if (call.rel(2) instanceof Union) {
       bottomUnion = call.rel(2);
-    } else if (call.rel(1) instanceof LogicalUnion) {
+    } else if (call.rel(1) instanceof Union) {
       bottomUnion = call.rel(1);
     } else {
       return;
@@ -75,7 +83,7 @@ public class UnionMergeRule extends RelOptRule {
     // Combine the inputs from the bottom union with the other inputs from
     // the top union
     List<RelNode> unionInputs = new ArrayList<RelNode>();
-    if (call.rel(2) instanceof LogicalUnion) {
+    if (call.rel(2) instanceof Union) {
       assert topUnion.getInputs().size() == 2;
       unionInputs.add(topUnion.getInput(0));
       unionInputs.addAll(bottomUnion.getInputs());
@@ -87,7 +95,8 @@ public class UnionMergeRule extends RelOptRule {
         == bottomUnion.getInputs().size()
         + topUnion.getInputs().size()
         - 1;
-    LogicalUnion newUnion = LogicalUnion.create(unionInputs, true);
+    RelNode newUnion = setOpFactory.createSetOp(SqlKind.UNION,
+        unionInputs, true);
     call.transformTo(newUnion);
   }
 }

http://git-wip-us.apache.org/repos/asf/incubator-calcite/blob/1ae40b00/core/src/main/java/org/apache/calcite/rel/rules/UnionToDistinctRule.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/calcite/rel/rules/UnionToDistinctRule.java b/core/src/main/java/org/apache/calcite/rel/rules/UnionToDistinctRule.java
index c688170..5bb9562 100644
--- a/core/src/main/java/org/apache/calcite/rel/rules/UnionToDistinctRule.java
+++ b/core/src/main/java/org/apache/calcite/rel/rules/UnionToDistinctRule.java
@@ -19,37 +19,47 @@ package org.apache.calcite.rel.rules;
 import org.apache.calcite.plan.RelOptRule;
 import org.apache.calcite.plan.RelOptRuleCall;
 import org.apache.calcite.plan.RelOptUtil;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.core.RelFactories;
+import org.apache.calcite.rel.core.Union;
 import org.apache.calcite.rel.logical.LogicalUnion;
+import org.apache.calcite.sql.SqlKind;
 
 /**
  * Planner rule that translates a distinct
- * {@link org.apache.calcite.rel.logical.LogicalUnion}
+ * {@link org.apache.calcite.rel.core.Union}
  * (<code>all</code> = <code>false</code>)
- * into an {@link org.apache.calcite.rel.logical.LogicalAggregate}
- * on top of a non-distinct {@link org.apache.calcite.rel.logical.LogicalUnion}
+ * into an {@link org.apache.calcite.rel.core.Aggregate}
+ * on top of a non-distinct {@link org.apache.calcite.rel.core.Union}
  * (<code>all</code> = <code>true</code>).
  */
 public class UnionToDistinctRule extends RelOptRule {
   public static final UnionToDistinctRule INSTANCE =
-      new UnionToDistinctRule();
+      new UnionToDistinctRule(LogicalUnion.class,
+          RelFactories.DEFAULT_SET_OP_FACTORY);
+
+  private final RelFactories.SetOpFactory setOpFactory;
 
   //~ Constructors -----------------------------------------------------------
 
   /**
    * Creates a UnionToDistinctRule.
    */
-  private UnionToDistinctRule() {
-    super(operand(LogicalUnion.class, any()));
+  public UnionToDistinctRule(Class<? extends Union> clazz,
+      RelFactories.SetOpFactory setOpFactory) {
+    super(operand(clazz, any()));
+    this.setOpFactory = setOpFactory;
   }
 
   //~ Methods ----------------------------------------------------------------
 
   public void onMatch(RelOptRuleCall call) {
-    LogicalUnion union = call.rel(0);
+    Union union = call.rel(0);
     if (union.all) {
       return; // nothing to do
     }
-    LogicalUnion unionAll = LogicalUnion.create(union.getInputs(), true);
+    RelNode unionAll = setOpFactory.createSetOp(SqlKind.UNION,
+        union.getInputs(), true);
     call.transformTo(RelOptUtil.createDistinctRel(unionAll));
   }
 }