julianhyde commented on a change in pull request #1078: [CALCITE-896] Remove 
Aggregate if grouping columns are unique and all functions are splittable
URL: https://github.com/apache/calcite/pull/1078#discussion_r261429146
 
 

 ##########
 File path: 
core/src/main/java/org/apache/calcite/rel/rules/AggregateRemoveRule.java
 ##########
 @@ -58,37 +68,63 @@ public AggregateRemoveRule(Class<? extends Aggregate> 
aggregateClass,
     // distinct to make it correct up-front, we can get rid of the reference
     // to the child here.
     super(
-        operand(aggregateClass,
+        operandJ(aggregateClass, null, agg -> isAggregateSupported(agg),
             operand(RelNode.class, any())),
         relBuilderFactory, null);
   }
 
+  private static boolean isAggregateSupported(Aggregate aggregate) {
+    if (aggregate.indicator
+        || aggregate.getGroupType() != Aggregate.Group.SIMPLE) {
+      return false;
+    }
+    // If any aggregate functions do not support splitting, bail out
+    for (AggregateCall aggregateCall : aggregate.getAggCallList()) {
+      if (aggregateCall.filterArg >= 0
+          || aggregateCall.getAggregation()
+              .unwrap(SqlSplittableAggFunction.class) == null) {
+        return false;
+      }
+    }
+    return true;
+  }
+
   //~ Methods ----------------------------------------------------------------
 
   public void onMatch(RelOptRuleCall call) {
     final Aggregate aggregate = call.rel(0);
     final RelNode input = call.rel(1);
-    if (!aggregate.getAggCallList().isEmpty() || aggregate.indicator) {
-      return;
-    }
     final RelMetadataQuery mq = call.getMetadataQuery();
     if (!SqlFunctions.isTrue(mq.areColumnsUnique(input, 
aggregate.getGroupSet()))) {
       return;
     }
-    // Distinct is "GROUP BY c1, c2" (where c1, c2 are a set of columns on
-    // which the input is unique, i.e. contain a key) and has no aggregate
-    // functions. It can be removed.
-    final RelNode newInput = convert(input, 
aggregate.getTraitSet().simplify());
 
-    // If aggregate was projecting a subset of columns, add a project for the
-    // same effect.
+    final RexBuilder rexBuilder = aggregate.getCluster().getRexBuilder();
 
 Review comment:
   an easier way to get rexBuilder is from relBuilder; just initialize 
relBuilder a few lines earlier

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


With regards,
Apache Git Services

Reply via email to