AveryQi115 opened a new pull request, #60361:
URL: https://github.com/apache/spark/pull/60361

   
   ### What changes were proposed in this pull request?
   
   `ResolveGroupingAnalytics` lowers a grouping-analytics `Aggregate` into 
`Aggregate(Expand(...))`.
   The `Expand` mints fresh exprIds for each grouping key (`col1#0` -> 
`col1#9`), so any reference to
   a grouping key that was resolved against the pre-Expand plan must be 
re-pointed at the rebuilt
   plan. The aggregate list already is; correlated subqueries were not. This PR 
adds one shared helper,
   
`GroupingAnalyticsTransformer.rewriteCorrelatedSubqueryGroupingKeys(subquery, 
remap,
   skipAggregateExpressions)`, that rewrites a correlated subquery's 
grouping-key `OuterReference`
   nodes (in the inner plan and in `outerAttrs`, recursing into nested 
subqueries) through a
   caller-supplied mapping, and uses it from three places:
   
   - **SELECT list** (`GroupingAnalyticsTransformer.replaceExpressions`): 
`grouping input -> Expand
     attribute`. A subquery that is itself a grouping key is replaced wholesale 
by its grouping
     attribute, like any other grouping expression, so super-aggregate rows 
show NULL.
   - **HAVING, fixed-point analyzer** 
(`ResolveGroupingAnalytics.tryResolveHavingCondition`): the
     subquery is resolved pre-Expand, so `grouping input -> post-Expand 
aggregate output`; for an
     aliased key `outer(col1#0)` becomes `outer(k#2)`. The mapping is built 
from the SELECT list plus
     the extra aggregate expressions `resolveExprsWithAggregate` pulls up from 
HAVING, so a grouping
     key referenced only in HAVING is covered too. When a key is selected 
several times, its first
     alias wins over a bare occurrence, matching what `UpdateOuterReferences` 
substitutes inside the
     subquery. `AggregateExpression` subtrees are skipped 
(`skipAggregateExpressions = true`) because
     `UpdateOuterReferences` lifts such an aggregate into the outer aggregate, 
which computes it over
     the pre-Expand input.
   - **HAVING, single-pass resolver** (`AggregateResolver`): the aggregate-list 
aliases are collected
     while resolving the aggregate expressions, before 
`GroupingAnalyticsResolver` rebuilds the
     aggregate over `Expand`, so they are registered with pre-Expand children 
(`col1#0 AS k`) and
     `NameScope.tryReplaceOuterReferenceAttributeWithAlias` never matches a 
post-Expand grouping key
     (`col1#9`). For grouping-analytics aggregates the rebuilt aggregate's own 
aliases (matched by
     exprId) are registered instead, so `outer(col1#9)` binds to `outer(k#2)` 
at resolution time,
     consistent with the fixed-point plan.
   
   - **Plan validation** (`ExprUtils.assertValidAggregation`): a 
`SubqueryExpression` in the aggregate
     list is now checked through its `outerAttrs` only. Its other children are 
the `joinConds` that
     `PullupCorrelatedPredicates` adds, which reference the subquery plan's own 
attributes and so can
     never be grouping expressions. Without this, test-mode plan validation
     (`spark.sql.planChangeValidation`) rejects any correlated EXISTS/IN in an 
aggregate's SELECT list
     with MISSING_AGGREGATION after `PullupCorrelatedPredicates`, including the 
plain `GROUP BY` shape
     on master, so the new SELECT-list tests could not run.
   
   Known limitation (unchanged): grouping keys that are expressions rather than 
columns
   (`GROUP BY ROLLUP(col1 + 1)` correlated as `... = col1 + 1`) are not 
remapped by either analyzer;
   such queries fail analysis today as before.
   
   ### Why are the changes needed?
   
   Wrong results and analysis failures for correlated subqueries over 
ROLLUP/CUBE/GROUPING SETS
   grouping keys; see the JIRA for three minimal reproductions (SELECT-list 
EXISTS returning true in
   the grand-total row, a scalar-subquery grouping key showing its value 
instead of NULL in the
   grand-total row, and a HAVING correlated subquery failing in 
PullupCorrelatedPredicates).
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes, bug fix: the queries above now return the correct result (or analyze 
successfully) instead
   of a wrong result (or an analysis error).
   
   ### How was this patch tested?
   
   New golden file `subquery/exists-subquery/exists-grouping-analytics.sql` 
covering the SELECT-list
   and HAVING shapes over ROLLUP/CUBE/GROUPING SETS, aliased and unselected 
grouping keys, a key
   selected twice, a correlation through an aggregate (left untouched), the 
ORDER BY / QUALIFY alias
   binding exercised by the single-pass change, and the negative cases that 
CheckAnalysis rejects
   before any rewrite. New `SubquerySuite` tests per grouping type (SELECT-list 
EXISTS, HAVING NOT
   EXISTS, and an exprId pin that the subquery's `outerAttrs` and inner 
`OuterReference` are the
   post-Expand grouping attribute), plus HAVING over an unselected grouping key.
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   Yes, co-authored with Claude Code (Isaac).
   
   This pull request and its description were written by Isaac.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to