Repository: phoenix
Updated Branches:
  refs/heads/calcite d46eed5e0 -> fe95b66e9


Improve PhoenixMergeSortUnionRule


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

Branch: refs/heads/calcite
Commit: fe95b66e984f97d67d931f83696cfbaa48870f12
Parents: d46eed5
Author: maryannxue <wei....@intel.com>
Authored: Mon Nov 2 15:34:40 2015 -0500
Committer: maryannxue <wei....@intel.com>
Committed: Mon Nov 2 15:34:40 2015 -0500

----------------------------------------------------------------------
 .../calcite/jdbc/PhoenixPrepareImpl.java        |  2 +
 .../calcite/metadata/PhoenixRelMdCollation.java | 56 +-------------------
 .../calcite/rel/PhoenixMergeSortUnion.java      | 13 +++--
 .../calcite/rules/PhoenixConverterRules.java    | 41 --------------
 .../rules/PhoenixMergeSortUnionRule.java        | 45 ++++++++++++++++
 5 files changed, 54 insertions(+), 103 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/phoenix/blob/fe95b66e/phoenix-core/src/main/java/org/apache/phoenix/calcite/jdbc/PhoenixPrepareImpl.java
----------------------------------------------------------------------
diff --git 
a/phoenix-core/src/main/java/org/apache/phoenix/calcite/jdbc/PhoenixPrepareImpl.java
 
b/phoenix-core/src/main/java/org/apache/phoenix/calcite/jdbc/PhoenixPrepareImpl.java
index 3d50429..b0102fe 100644
--- 
a/phoenix-core/src/main/java/org/apache/phoenix/calcite/jdbc/PhoenixPrepareImpl.java
+++ 
b/phoenix-core/src/main/java/org/apache/phoenix/calcite/jdbc/PhoenixPrepareImpl.java
@@ -30,6 +30,7 @@ import 
org.apache.phoenix.calcite.rules.PhoenixCompactClientSortRule;
 import org.apache.phoenix.calcite.rules.PhoenixFilterScanMergeRule;
 import org.apache.phoenix.calcite.rules.PhoenixInnerSortRemoveRule;
 import 
org.apache.phoenix.calcite.rules.PhoenixJoinSingleValueAggregateMergeRule;
+import org.apache.phoenix.calcite.rules.PhoenixMergeSortUnionRule;
 import org.apache.phoenix.calcite.rules.PhoenixOrderedAggregateRule;
 
 import com.google.common.base.Function;
@@ -76,6 +77,7 @@ public class PhoenixPrepareImpl extends CalcitePrepareImpl {
         planner.addRule(PhoenixFilterScanMergeRule.INSTANCE);
         planner.addRule(PhoenixCompactClientSortRule.SORT_SERVERAGGREGATE);
         planner.addRule(PhoenixJoinSingleValueAggregateMergeRule.INSTANCE);
+        planner.addRule(PhoenixMergeSortUnionRule.INSTANCE);
         planner.addRule(PhoenixInnerSortRemoveRule.INSTANCE);
         planner.addRule(PhoenixOrderedAggregateRule.INSTANCE);
         

http://git-wip-us.apache.org/repos/asf/phoenix/blob/fe95b66e/phoenix-core/src/main/java/org/apache/phoenix/calcite/metadata/PhoenixRelMdCollation.java
----------------------------------------------------------------------
diff --git 
a/phoenix-core/src/main/java/org/apache/phoenix/calcite/metadata/PhoenixRelMdCollation.java
 
b/phoenix-core/src/main/java/org/apache/phoenix/calcite/metadata/PhoenixRelMdCollation.java
index cb6b232..90606cd 100644
--- 
a/phoenix-core/src/main/java/org/apache/phoenix/calcite/metadata/PhoenixRelMdCollation.java
+++ 
b/phoenix-core/src/main/java/org/apache/phoenix/calcite/metadata/PhoenixRelMdCollation.java
@@ -1,8 +1,6 @@
 package org.apache.phoenix.calcite.metadata;
 
 import java.util.List;
-import java.util.Set;
-
 import org.apache.calcite.rel.RelCollation;
 import org.apache.calcite.rel.RelCollations;
 import org.apache.calcite.rel.RelNode;
@@ -21,7 +19,6 @@ import org.apache.phoenix.calcite.rel.PhoenixMergeSortUnion;
 import org.apache.phoenix.calcite.rel.PhoenixServerJoin;
 
 import com.google.common.collect.ImmutableList;
-import com.google.common.collect.Sets;
 
 public class PhoenixRelMdCollation {
     public static final RelMetadataProvider SOURCE =
@@ -47,7 +44,7 @@ public class PhoenixRelMdCollation {
     }
 
     public ImmutableList<RelCollation> collations(PhoenixMergeSortUnion union) 
{
-        return 
ImmutableList.copyOf(PhoenixRelMdCollation.mergeSortUnion(union.getInputs(), 
union.all));
+        return ImmutableList.of(union.collation);
     }
     
     /** Helper method to determine a {@link PhoenixCorrelate}'s collation. */
@@ -82,56 +79,5 @@ public class PhoenixRelMdCollation {
         }
         return builder.build();
     }
-    
-    public static List<RelCollation> mergeSortUnion(List<RelNode> inputs, 
boolean all) {
-       if (!all) {
-               return ImmutableList.of(RelCollations.EMPTY);
-       }
-       
-       Set<RelCollation> mergedCollations = null;
-       for (RelNode input : inputs) {
-               final ImmutableList<RelCollation> inputCollations = 
RelMetadataQuery.collations(input);
-               Set<RelCollation> nonEmptyInputCollations = Sets.newHashSet();
-                       for (RelCollation collation : inputCollations) {
-                               if (!collation.getFieldCollations().isEmpty()) {
-                                       nonEmptyInputCollations.add(collation);
-                               }
-                       }
-               
-                       if (nonEmptyInputCollations.isEmpty() || 
mergedCollations == null) {
-                       mergedCollations = nonEmptyInputCollations;
-               } else {
-                       Set<RelCollation> newCollations = Sets.newHashSet();
-                       for (RelCollation m : mergedCollations) {
-                               for (RelCollation n : nonEmptyInputCollations) {
-                                       if (n.satisfies(m)) {
-                                               newCollations.add(m);
-                                               break;
-                                       }
-                               }
-                       }
-                       for (RelCollation n : nonEmptyInputCollations) {
-                               for (RelCollation m : mergedCollations) {
-                                       if (m.satisfies(n)) {
-                                               newCollations.add(n);
-                                               break;
-                                       }
-                               }
-                       }
-                       mergedCollations = newCollations;
-               }
-                       
-               if (mergedCollations.isEmpty()) {
-                       break;
-               }
-       }
-       
-       // We only return the simplified collation here because 
PhoenixMergeSortUnion
-       // needs a definite way for implement().
-               if (mergedCollations.size() != 1) {
-                       return ImmutableList.of(RelCollations.EMPTY);
-               }
-        return ImmutableList.of(mergedCollations.iterator().next());
-    }
 
 }

http://git-wip-us.apache.org/repos/asf/phoenix/blob/fe95b66e/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixMergeSortUnion.java
----------------------------------------------------------------------
diff --git 
a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixMergeSortUnion.java
 
b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixMergeSortUnion.java
index f3e162b..05b90ad 100644
--- 
a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixMergeSortUnion.java
+++ 
b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixMergeSortUnion.java
@@ -11,7 +11,7 @@ import org.apache.calcite.rel.RelCollation;
 import org.apache.calcite.rel.RelCollationTraitDef;
 import org.apache.calcite.rel.RelNode;
 import org.apache.calcite.rel.core.Union;
-import org.apache.phoenix.calcite.metadata.PhoenixRelMdCollation;
+import org.apache.calcite.rel.metadata.RelMetadataQuery;
 import org.apache.phoenix.compile.QueryPlan;
 import org.apache.phoenix.compile.RowProjector;
 import org.apache.phoenix.compile.GroupByCompiler.GroupBy;
@@ -26,10 +26,8 @@ import com.google.common.collect.Lists;
 public class PhoenixMergeSortUnion extends Union implements PhoenixRel {
        public final RelCollation collation;
     
-    public static PhoenixMergeSortUnion create(final List<RelNode> inputs, 
final boolean all) {
-       final List<RelCollation> collationList = 
PhoenixRelMdCollation.mergeSortUnion(inputs, all);
-       assert collationList.size() == 1;
-       final RelCollation collation = collationList.get(0);
+    public static PhoenixMergeSortUnion create(final List<RelNode> inputs,
+            final boolean all, final RelCollation collation) {
         RelOptCluster cluster = inputs.get(0).getCluster();
         RelTraitSet traits = 
                        cluster.traitSetOf(PhoenixRel.CLIENT_CONVENTION)
@@ -49,13 +47,14 @@ public class PhoenixMergeSortUnion extends Union implements 
PhoenixRel {
 
     @Override
     public PhoenixMergeSortUnion copy(RelTraitSet traits, List<RelNode> 
inputs, boolean all) {
-        return create(inputs, all);
+        return create(inputs, all, collation);
     }
 
     @Override
     public RelOptCost computeSelfCost(RelOptPlanner planner) {
         for (RelNode input : getInputs()) {
-            if (input.getConvention() != PhoenixRel.CLIENT_CONVENTION) {
+            if (input.getConvention() != PhoenixRel.CLIENT_CONVENTION
+                    || 
!RelMetadataQuery.collations(input).contains(collation)) {
                 return planner.getCostFactory().makeInfiniteCost();
             }
         }

http://git-wip-us.apache.org/repos/asf/phoenix/blob/fe95b66e/phoenix-core/src/main/java/org/apache/phoenix/calcite/rules/PhoenixConverterRules.java
----------------------------------------------------------------------
diff --git 
a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rules/PhoenixConverterRules.java
 
b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rules/PhoenixConverterRules.java
index 6ad544d..5e2342a 100644
--- 
a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rules/PhoenixConverterRules.java
+++ 
b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rules/PhoenixConverterRules.java
@@ -39,7 +39,6 @@ import org.apache.calcite.rel.logical.LogicalValues;
 import org.apache.calcite.rex.RexNode;
 import org.apache.calcite.util.trace.CalciteTrace;
 import org.apache.phoenix.calcite.CalciteUtils;
-import org.apache.phoenix.calcite.metadata.PhoenixRelMdCollation;
 import org.apache.phoenix.calcite.rel.PhoenixAbstractAggregate;
 import org.apache.phoenix.calcite.rel.PhoenixClientAggregate;
 import org.apache.phoenix.calcite.rel.PhoenixClientJoin;
@@ -49,7 +48,6 @@ import org.apache.phoenix.calcite.rel.PhoenixClientSort;
 import org.apache.phoenix.calcite.rel.PhoenixCorrelate;
 import org.apache.phoenix.calcite.rel.PhoenixFilter;
 import org.apache.phoenix.calcite.rel.PhoenixLimit;
-import org.apache.phoenix.calcite.rel.PhoenixMergeSortUnion;
 import org.apache.phoenix.calcite.rel.PhoenixRel;
 import org.apache.phoenix.calcite.rel.PhoenixServerAggregate;
 import org.apache.phoenix.calcite.rel.PhoenixServerJoin;
@@ -91,7 +89,6 @@ public class PhoenixConverterRules {
         PhoenixServerAggregateRule.SERVER,
         PhoenixServerAggregateRule.SERVERJOIN,
         PhoenixUnionRule.INSTANCE,
-        PhoenixMergeSortUnionRule.INSTANCE,
         PhoenixClientJoinRule.INSTANCE,
         PhoenixServerJoinRule.INSTANCE,
         PhoenixClientSemiJoinRule.INSTANCE,
@@ -116,7 +113,6 @@ public class PhoenixConverterRules {
         PhoenixServerAggregateRule.CONVERTIBLE_SERVER,
         PhoenixServerAggregateRule.CONVERTIBLE_SERVERJOIN,
         PhoenixUnionRule.CONVERTIBLE,
-        PhoenixMergeSortUnionRule.CONVERTIBLE,
         PhoenixClientJoinRule.CONVERTIBLE,
         PhoenixServerJoinRule.CONVERTIBLE,
         PhoenixClientSemiJoinRule.INSTANCE,
@@ -488,43 +484,6 @@ public class PhoenixConverterRules {
     }
 
     /**
-     * Rule to convert a {@link org.apache.calcite.rel.core.Union} to a
-     * {@link PhoenixMergeSortUnion}.
-     */
-    public static class PhoenixMergeSortUnionRule extends PhoenixConverterRule 
{
-        private static Predicate<LogicalUnion> IS_CONVERTIBLE = new 
Predicate<LogicalUnion>() {
-            @Override
-            public boolean apply(LogicalUnion input) {
-                return isConvertible(input);
-            }            
-        };
-        
-        private static Predicate<LogicalUnion> NON_EMPTY_COLLATION = new 
Predicate<LogicalUnion>() {
-                       @Override
-                       public boolean apply(LogicalUnion input) {
-                               List<RelCollation> collations = 
PhoenixRelMdCollation.mergeSortUnion(input.getInputs(), input.all);
-                               return collations.size() == 1 && 
!collations.get(0).getFieldCollations().isEmpty();
-                       }
-        };
-        
-        public static final PhoenixMergeSortUnionRule INSTANCE = new 
PhoenixMergeSortUnionRule(NON_EMPTY_COLLATION);
-        
-        public static final PhoenixMergeSortUnionRule CONVERTIBLE = new 
PhoenixMergeSortUnionRule(Predicates.and(IS_CONVERTIBLE, NON_EMPTY_COLLATION));
-
-        private PhoenixMergeSortUnionRule(Predicate<LogicalUnion> predicate) {
-            super(LogicalUnion.class, predicate, Convention.NONE, 
-                    PhoenixRel.CLIENT_CONVENTION, "PhoenixMergeSortUnionRule");
-        }
-
-        public RelNode convert(RelNode rel) {
-            final LogicalUnion union = (LogicalUnion) rel;
-            return PhoenixMergeSortUnion.create(
-                    convertList(union.getInputs(), out),
-                    union.all);
-        }
-    }
-
-    /**
      * Rule to convert a {@link org.apache.calcite.rel.core.Join} to a
      * {@link PhoenixClientJoin}.
      */

http://git-wip-us.apache.org/repos/asf/phoenix/blob/fe95b66e/phoenix-core/src/main/java/org/apache/phoenix/calcite/rules/PhoenixMergeSortUnionRule.java
----------------------------------------------------------------------
diff --git 
a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rules/PhoenixMergeSortUnionRule.java
 
b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rules/PhoenixMergeSortUnionRule.java
new file mode 100644
index 0000000..43a2771
--- /dev/null
+++ 
b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rules/PhoenixMergeSortUnionRule.java
@@ -0,0 +1,45 @@
+package org.apache.phoenix.calcite.rules;
+
+import org.apache.calcite.plan.RelOptRule;
+import org.apache.calcite.plan.RelOptRuleCall;
+import org.apache.calcite.rel.RelCollation;
+import org.apache.phoenix.calcite.rel.PhoenixClientSort;
+import org.apache.phoenix.calcite.rel.PhoenixMergeSortUnion;
+import org.apache.phoenix.calcite.rel.PhoenixUnion;
+
+import com.google.common.base.Predicate;
+
+/**
+ * Rule that converts a {@link PhoenixClientSort} over a {@link PhoenixUnion}
+ * into a {@link PhoenixMergeSortUnion}.
+ */
+public class PhoenixMergeSortUnionRule extends RelOptRule {
+    private static final Predicate<PhoenixUnion> IS_UNION_ALL =
+            new Predicate<PhoenixUnion>() {
+        @Override
+        public boolean apply(PhoenixUnion input) {
+            return input.all;
+        }
+    };
+    
+    public static final PhoenixMergeSortUnionRule INSTANCE =
+            new PhoenixMergeSortUnionRule();
+
+    public PhoenixMergeSortUnionRule() {
+        super(operand(PhoenixClientSort.class, 
+                operand(PhoenixUnion.class, null, IS_UNION_ALL, any())));
+    }
+
+    @Override
+    public void onMatch(RelOptRuleCall call) {
+        PhoenixClientSort sort = call.rel(0);
+        PhoenixUnion union = call.rel(1);        
+        assert union.all;
+        RelCollation collation = sort.getCollation();
+        call.transformTo(
+                PhoenixMergeSortUnion.create(
+                        convertList(union.getInputs(), collation),
+                        union.all, collation));
+    }
+
+}

Reply via email to