calcite-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jh...@apache.org
Subject calcite git commit: [CALCITE-2124] AggregateExpandDistinctAggregatesRule should make SUM nullable if there is no GROUP BY (Godfrey He)
Date Thu, 11 Jan 2018 07:33:22 GMT
Repository: calcite
Updated Branches:
  refs/heads/master efef4e244 -> 1c7f690d3


[CALCITE-2124] AggregateExpandDistinctAggregatesRule should make SUM nullable if there is
no GROUP BY (Godfrey He)

Close apache/calcite#598


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

Branch: refs/heads/master
Commit: 1c7f690d391d81ff251253277944b07e410243da
Parents: efef4e2
Author: xiaoling.hxl <xiaoling.hxl@alibaba-inc.com>
Authored: Sat Jan 6 17:52:04 2018 +0800
Committer: Julian Hyde <jhyde@apache.org>
Committed: Wed Jan 10 23:32:40 2018 -0800

----------------------------------------------------------------------
 .../AggregateExpandDistinctAggregatesRule.java  |  6 +++++-
 .../apache/calcite/test/RelOptRulesTest.java    | 10 ++++++++++
 .../org/apache/calcite/test/RelOptRulesTest.xml | 21 ++++++++++++++++++++
 3 files changed, 36 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/calcite/blob/1c7f690d/core/src/main/java/org/apache/calcite/rel/rules/AggregateExpandDistinctAggregatesRule.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/calcite/rel/rules/AggregateExpandDistinctAggregatesRule.java
b/core/src/main/java/org/apache/calcite/rel/rules/AggregateExpandDistinctAggregatesRule.java
index 019259c..918a4db 100644
--- a/core/src/main/java/org/apache/calcite/rel/rules/AggregateExpandDistinctAggregatesRule.java
+++ b/core/src/main/java/org/apache/calcite/rel/rules/AggregateExpandDistinctAggregatesRule.java
@@ -392,7 +392,11 @@ public final class AggregateExpandDistinctAggregatesRule extends RelOptRule
{
     final List<AggregateCall> distinctAggCalls = new ArrayList<>();
     for (Pair<AggregateCall, String> aggCall : aggregate.getNamedAggCalls()) {
       if (!aggCall.left.isDistinct()) {
-        distinctAggCalls.add(aggCall.left.rename(aggCall.right));
+        AggregateCall newAggCall =
+            aggCall.left.adaptTo(aggregate.getInput(),
+                aggCall.left.getArgList(), aggCall.left.filterArg,
+                aggregate.getGroupCount(), fullGroupSet.cardinality());
+        distinctAggCalls.add(newAggCall.rename(aggCall.right));
       }
     }
 

http://git-wip-us.apache.org/repos/asf/calcite/blob/1c7f690d/core/src/test/java/org/apache/calcite/test/RelOptRulesTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/calcite/test/RelOptRulesTest.java b/core/src/test/java/org/apache/calcite/test/RelOptRulesTest.java
index d76babe..2873531 100644
--- a/core/src/test/java/org/apache/calcite/test/RelOptRulesTest.java
+++ b/core/src/test/java/org/apache/calcite/test/RelOptRulesTest.java
@@ -763,6 +763,16 @@ public class RelOptRulesTest extends RelOptTestBase {
     sql(sql).with(program).check();
   }
 
+  @Test public void testDistinctCountWithoutGroupBy() {
+    final HepProgram program = HepProgram.builder()
+        .addRuleInstance(AggregateExpandDistinctAggregatesRule.INSTANCE)
+        .addRuleInstance(AggregateProjectMergeRule.INSTANCE)
+        .build();
+    final String sql = "select max(deptno), count(distinct ename)\n"
+        + "from sales.emp";
+    sql(sql).with(program).check();
+  }
+
   @Test public void testDistinctCount1() {
     final HepProgram program = HepProgram.builder()
         .addRuleInstance(AggregateExpandDistinctAggregatesRule.INSTANCE)

http://git-wip-us.apache.org/repos/asf/calcite/blob/1c7f690d/core/src/test/resources/org/apache/calcite/test/RelOptRulesTest.xml
----------------------------------------------------------------------
diff --git a/core/src/test/resources/org/apache/calcite/test/RelOptRulesTest.xml b/core/src/test/resources/org/apache/calcite/test/RelOptRulesTest.xml
index 17041ef..70e54c8 100644
--- a/core/src/test/resources/org/apache/calcite/test/RelOptRulesTest.xml
+++ b/core/src/test/resources/org/apache/calcite/test/RelOptRulesTest.xml
@@ -5255,6 +5255,27 @@ LogicalProject(ENAME=[$0], R=[$1])
 ]]>
         </Resource>
     </TestCase>
+    <TestCase name="testDistinctCountWithoutGroupBy">
+        <Resource name="sql">
+            <![CDATA[select max(deptno), count(distinct ename)
+from sales.emp]]>
+        </Resource>
+        <Resource name="planBefore">
+            <![CDATA[
+LogicalAggregate(group=[{}], EXPR$0=[MAX($0)], EXPR$1=[COUNT(DISTINCT $1)])
+  LogicalProject(DEPTNO=[$7], ENAME=[$1])
+    LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+]]>
+        </Resource>
+        <Resource name="planAfter">
+            <![CDATA[
+LogicalAggregate(group=[{}], EXPR$0=[MIN($1) FILTER $3], EXPR$1=[COUNT($0) FILTER $2])
+  LogicalProject(ENAME=[$0], EXPR$0=[$1], $g_0=[=($2, 0)], $g_1=[=($2, 1)])
+    LogicalAggregate(group=[{1}], groups=[[{1}, {}]], EXPR$0=[MAX($7)], $g=[GROUPING($1)])
+      LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+]]>
+        </Resource>
+    </TestCase>
     <TestCase name="testDistinctCount1">
         <Resource name="sql">
             <![CDATA[select deptno, count(distinct ename) from sales.emp group by deptno]]>


Mime
View raw message