Repository navigation
fix(optimizer): rewrite single distinct aggregates with Unsupported distinct handling - #27657
Open
mohitgurav20 wants to merge 1 commit into
Open
mohitgurav20 wants to merge 1 commit into
mohitgurav20 wants to merge 1 commit into
Conversation
mohitgurav20
marked this pull request as ready for review
November 23, 2026 13:59
Contributor
Author
|
@mkleen @jayzhan211 sir pls free to reiview this PR as per requested. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Which issue does this PR close?
Follow-up split from #26000 per review feedback from @jayzhan211.
Rationale for this change
On
main, single-distinct queries combining a distinct aggregate with non-distinct companion aggregates (such ascount(*)) checkrewrite_pays_for_countto decide whether to rewrite to two-phaseGROUP BY.For aggregates declaring
DistinctHandling::Unsupported(such asstddev),groups_accumulator_supported_for_typesreturnsNonerather thanSome(false). As a result,rewrite_pays_for_countconcludes the rewrite does not pay and leaves the query untouched. At execution time, the engine fails with an error because the accumulator cannot handle DISTINCT natively:What changes are included in this PR?
SingleDistinctToGroupBy::is_single_distinct_aggto ensure aggregates withDistinctHandling::Unsupportedare always rewritten into the two-phaseGROUP BYplan, where deduplication happens in the inner aggregation and the outer function runs non-distinct.stddev(DISTINCT)with companioncount(*)insingle_distinct_to_groupby.slt.Are these changes tested?
Yes:
datafusion/optimizer/src/single_distinct_to_groupby.rs.single_distinct_to_groupby.slt.