[SPARK-56898] Rewrite COUNT(DISTINCT IF) to COUNT(DISTINCT) FILTER for Expand reduction#55925
Open
xumingming wants to merge 1 commit into
Open
[SPARK-56898] Rewrite COUNT(DISTINCT IF) to COUNT(DISTINCT) FILTER for Expand reduction#55925xumingming wants to merge 1 commit into
xumingming wants to merge 1 commit into
Conversation
cc0a893 to
3bc29c1
Compare
Author
|
@LuciferYang Can you help take a look at this PR? |
… FILTER for Expand reduction Adds RewriteCountDistinctConditional optimizer rule that canonicalizes: COUNT(DISTINCT IF(cond, base, NULL)) COUNT(DISTINCT CASE WHEN cond THEN base END) into: COUNT(DISTINCT base) FILTER (WHERE cond) This reduces the number of distinct groups seen by RewriteDistinctAggregates from N (one per unique conditional expression) down to 1 (all share the same base column), collapsing the Expand factor from Nx to 1x. Gated by spark.sql.optimizer.rewriteCountDistinctConditional.enabled (default: false).
3bc29c1 to
028cf1b
Compare
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.
What changes were proposed in this pull request?
Adds RewriteCountDistinctConditional optimizer rule that canonicalizes:
into:
This reduces the number of distinct groups seen by RewriteDistinctAggregates from N (one per unique conditional expression) down to 1 (all share the same base column), collapsing the Expand factor from Nx to 1x.
Gated by spark.sql.optimizer.rewriteCountDistinctConditional.enabled (default: false).
Includes comprehensive unit tests for rewrite patterns and safety boundaries.
Why are the changes needed?
When a query contains many COUNT(DISTINCT IF(cond_i, col, NULL)) expressions over the same base column, RewriteDistinctAggregates treats each unique IF(...) expression as a distinct group. N conditions → N distinct groups → N× Expand amplification. In production workloads with 25–60 such expressions, this produces multi-terabyte shuffles and hour-long runtimes.
Does this PR introduce any user-facing change?
No.
How was this patch tested?
Unit Test.
Was this patch authored or co-authored using generative AI tooling?
No.