fix: apply single_distinct_to_groupby to aliased distinct aggregates#23486
Open
gmhelmold wants to merge 1 commit into
Open
fix: apply single_distinct_to_groupby to aliased distinct aggregates#23486gmhelmold wants to merge 1 commit into
gmhelmold wants to merge 1 commit into
Conversation
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?
single_distinct_to_groupbynot applied to DataFrame API count(DISTINCT) #23401.Rationale for this change
SingleDistinctToGroupByrewrites a groupedAGG(DISTINCT col)into a cheaper nested double aggregate. Today the rule only recognizes a bareExpr::AggregateFunctioninAggregate.aggr_expr, so it fires for plans from the SQL planner but silently skips the logically-identical plan built through the DataFrame API — that path runs materially slower for the same query.The divergence is purely in where each front-end places the output alias:
count(DISTINCT v) AS n): the planner hoistsAS ninto an outerProjection, leaving a bareExpr::AggregateFunctioninaggr_expr.count(col("v")).distinct().alias("n")):.alias(..)wraps the aggregate directly, soaggr_exprholdsExpr::Alias(AggregateFunction).is_single_distinct_aggmatched only the bare shape and returnedfalseforAlias(AggregateFunction), so the rule never fired for DataFrame-API distinct aggregates.What changes are included in this PR?
Expr::Aliaslayer in the matcher (is_single_distinct_agg) and in the rewrite, mirroring the one-layerunalias()idiom already used in the optimizer (e.g.decorrelate.rs), so both front-end shapes are recognized identically.Projectionfrom the originalAggregateschema (schema.qualified_field(idx)), soAS nsurvives.Are these changes tested?
Yes — new snapshot tests in
single_distinct_to_groupby.rs:single_distinct_aliased— the DataFrame-APIAlias(AggregateFunction)shape is now rewritten, and the output column stays namedn.single_distinct_aliased_and_groupby— same, alongside a realGROUP BYcolumn.non_distinct_aliasedandtwo_distinct_aliased_and_groupby— negative cases: an aliased non-distinct aggregate, and two aliased distinct aggregates on different fields, are still (correctly) left untouched.Reverting the production change makes the two positive tests fail (the rule no longer fires) while every other test stays green, confirming the tests are wired to the fix.
Are there any user-facing changes?
No API changes. DataFrame-API queries using
count(distinct …)(and other single-distinct aggregates) now receive the same optimized plan as their SQL equivalents — a performance improvement, with no change to results or output column names.