Skip to content

Commit

Permalink
[CALCITE-6332] Optimization CoreRules.AGGREGATE_EXPAND_DISTINCT_AGGRE…
Browse files Browse the repository at this point in the history
…GATES_TO_JOIN produces incorrect results for aggregates with groupSets

Signed-off-by: Mihai Budiu <[email protected]>
  • Loading branch information
mihaibudiu committed Apr 8, 2024
1 parent 1f5ba75 commit 06fa8c8
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,13 @@ public AggregateExpandDistinctAggregatesRule(
return;
}

if (!config.isUsingGroupingSets()
&& aggregate.groupSets.size() > 1) {
// Grouping sets are not handled correctly
// when generating joins.
return;
}

// Find all of the agg expressions. We use a LinkedHashSet to ensure determinism.
final List<AggregateCall> aggCalls = aggregate.getAggCallList();
// Find all aggregate calls with distinct
Expand Down
12 changes: 12 additions & 0 deletions core/src/test/java/org/apache/calcite/test/RelOptRulesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1990,6 +1990,18 @@ private void checkSemiOrAntiJoinProjectTranspose(JoinRelType type) {
.check();
}

/** Test case for <a href="https://issues.apache.org/jira/projects/CALCITE/issues/CALCITE-6332">
* [CALCITE-6332] Optimization CoreRules.AGGREGATE_EXPAND_DISTINCT_AGGREGATES_TO_JOIN
* produces incorrect results for aggregates with groupSets</a>. */
@Test void testIssue6332() {
final String sql = "select count(distinct deptno) as cd, count(*) as c\n"
+ "from emp\n"
+ "group by cube(deptno)";
sql(sql)
.withRule(CoreRules.AGGREGATE_EXPAND_DISTINCT_AGGREGATES_TO_JOIN)
.checkUnchanged();
}

@Test void testDistinctCountMixed() {
final String sql = "select deptno, count(distinct deptno, job) as cddj,\n"
+ " sum(sal) as s\n"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5163,6 +5163,21 @@ LogicalProject(EMPNO=[$0], DEPTNO=[$1], W_COUNT=[$2])
LogicalProject(EMPNO=[$0], DEPTNO=[$7], $2=[$9])
LogicalWindow(window#0=[window(aggs [COUNT($0)])])
LogicalTableScan(table=[[CATALOG, SALES, EMP]])
]]>
</Resource>
</TestCase>
<TestCase name="testIssue6332">
<Resource name="sql">
<![CDATA[select count(distinct deptno) as cd, count(*) as c
from emp
group by cube(deptno)]]>
</Resource>
<Resource name="planBefore">
<![CDATA[
LogicalProject(CD=[$1], C=[$2])
LogicalAggregate(group=[{0}], groups=[[{0}, {}]], CD=[COUNT(DISTINCT $0)], C=[COUNT()])
LogicalProject(DEPTNO=[$7])
LogicalTableScan(table=[[CATALOG, SALES, EMP]])
]]>
</Resource>
</TestCase>
Expand Down

0 comments on commit 06fa8c8

Please sign in to comment.