siddharthteotia commented on a change in pull request #1033: [CALCITE-2820] 
Avoid reducing certain aggregate functions when it is not necessary
URL: https://github.com/apache/calcite/pull/1033#discussion_r264339127
 
 

 ##########
 File path: core/src/test/java/org/apache/calcite/test/RelOptRulesTest.java
 ##########
 @@ -4292,6 +4294,86 @@ private Sql checkSubQuery(String sql) {
     checkPlanning(program, sql);
   }
 
+  @Test public void testReduceAverageWithNoReduceSum() {
+    final EnumSet<SqlKind> functionsToReduce = EnumSet.of(SqlKind.AVG);
+    checkPlanning(
+        new AggregateReduceFunctionsRule(
+          LogicalAggregate.class, RelFactories.LOGICAL_BUILDER,
+            functionsToReduce),
+                  "select name, max(name), avg(deptno), min(name)"
+                          + " from sales.dept group by name");
+  }
+
+  @Test public void testNoReduceAverage() {
+    final EnumSet<SqlKind> functionsToReduce = EnumSet.noneOf(SqlKind.class);
+    HepProgram program = new HepProgramBuilder()
+        .addRuleInstance(
+          new AggregateReduceFunctionsRule(LogicalAggregate.class,
+            RelFactories.LOGICAL_BUILDER, functionsToReduce))
+        .build();
+    String sql = "select name, max(name), avg(deptno), min(name)"
+        + " from sales.dept group by name";
+    sql(sql).with(program).checkUnchanged();
+  }
+
+  @Test public void testNoReduceSum() {
+    final EnumSet<SqlKind> functionsToReduce = EnumSet.noneOf(SqlKind.class);
+    HepProgram program = new HepProgramBuilder()
+            .addRuleInstance(
+              new AggregateReduceFunctionsRule(LogicalAggregate.class,
+                RelFactories.LOGICAL_BUILDER, functionsToReduce))
+            .build();
+    String sql = "select name, sum(deptno)"
+            + " from sales.dept group by name";
+    sql(sql).with(program).checkUnchanged();
+  }
+
+  @Test public void testReduceAverageAndVarWithNoReduceStddev() {
+    // configure rule to reduce AVG and VAR_POP functions
+    // other functions like SUM, STDDEV won't be reduced
+    final EnumSet<SqlKind> functionsToReduce = EnumSet.of(SqlKind.AVG, 
SqlKind.VAR_POP);
+    HepProgram program = new HepProgramBuilder()
+        .addRuleInstance(
+         new AggregateReduceFunctionsRule(LogicalAggregate.class,
+           RelFactories.LOGICAL_BUILDER, functionsToReduce))
+        .build();
+    final String sql = "select name, stddev_pop(deptno), avg(deptno),"
+        + " var_pop(deptno)\n"
+        + "from sales.dept group by name";
+    sql(sql).with(program).check();
+  }
+
+  @Test public void testReduceAverageAndSumWithNoReduceStddevAndVar() {
 
 Review comment:
   Yes, the use of AVG will implicitly introduce SUM and the latter will be 
reduced to SUM0

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


With regards,
Apache Git Services

Reply via email to