Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CALCITE-6332] Optimization CoreRules.AGGREGATE_EXPAND_DISTINCT_AGGREGATES_TO_JOIN... #3749

Merged
merged 1 commit into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading