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 1, 2024
1 parent f948850 commit ceb6530
Show file tree
Hide file tree
Showing 4 changed files with 50 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
16 changes: 16 additions & 0 deletions core/src/test/java/org/apache/calcite/test/RelMetadataTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3782,6 +3782,22 @@ public void checkAllPredicatesAndTableSetOp(String sql) {
hasToString("=($0, $1)"));
}

@Test void testGetPredicatesForRollup() {
final RelBuilder b = RelBuilderTest.createBuilder();
RelNode r = b
.scan("EMP")
.aggregate(b.groupKey("DEPTNO"),
b.literalAgg(42),
b.literalAgg(null))
.build();
RelMetadataQuery mq = r.getCluster().getMetadataQuery();
final RelOptPredicateList predicateList = mq.getPulledUpPredicates(r);
assertThat(predicateList.pulledUpPredicates,
hasToString("[=($1, 42), IS NULL($2)]"));
assertThat(toSortedStringList(predicateList.constantMap),
hasToString("[$1=42, $2=null:NULL]"));
}

@Test void testGetPredicatesForLiteralAgg() {
final RelBuilder b = RelBuilderTest.createBuilder();
RelNode r = b
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 ceb6530

Please sign in to comment.