[SPARK-60873][SQL] Evaluate HAVING before nested window functions #60090
All Vouch review confidence checks passed
Residual risk 0.24 = Change Risk (0.45) × (1 − Review Confidence (0.47)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.45 · 420 lines diff across 5 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.362
Shallow Review Depth
shallow_review_depth
0.344
Real PR Conversation Timeline
1 comments
"Review active on #60090"
general
weight: 0.35
vladimirg-db
What changes were proposed in this pull request?
This change places
HAVINGbelow every nested SELECT-listWindowas an all-or-noneoperation. The window-chain walk is bounded to its
Project/Generate/Windowspine,and window-output classification inspects expression trees without traversing subquery plans.
For grouping analytics (
ROLLUP,CUBE, andGROUPING SETS), an unresolvedDeferredHavingmarker preserves the aggregate output only while window extraction builds thewindow chain. The marker is converted to the placed
Filterimmediately afterExtractWindowExpressionsin the main resolution batch, so QUALIFY resolution,nondeterministic-expression handling, commands, and later analyzer batches see the normal plan
shape. Lateral column aliases are lifted to a fixed point before deferral. Conditions that
reference a window output, use a grouping function, or cannot complete the lateral-alias lift
retain their previous analysis path.
It adds the internal session config
spark.sql.legacy.evaluateHavingAfterInnerWindow, defaulting tofalse, to restore theprevious behavior when needed. The SQL migration guide documents the correction, grouping
analytics behavior, and fallback.
SQL execution and analyzer goldens cover nested window specifications, three nesting levels,
mixed nesting depths, generators over windowed inputs and subqueries, lateral aliases, grouping
functions, HAVING-only aggregates, scalar subqueries, QUALIFY, seeded nondeterministic predicates,
and view/cache/CTAS/RTAS command paths. The regenerated goldens and command outputs show
HAVINGbelow the complete SELECT-list window chain by default, while the legacy configurationretains the prior plans and results.
Why are the changes needed?
The fixed-point analyzer previously extracted one window layer per iteration and immediately
converted
HAVINGto aFilter. A later iteration could insert an innerWindowbelow thatfilter. Generator extraction produced a similar non-contiguous window chain, and grouping
analytics followed a separate path that could place the filter after a window. In each case, a
window function could see groups that
HAVINGshould have removed and produce an incorrectresult.
See SPARK-60873.
Does this PR introduce _any_ user-facing change?
Yes. Nested window functions in grouped queries, including grouping analytics, are now evaluated
after
HAVINGwhen the condition does not depend on a window output or grouping function. Forthe regression query, the surviving
keepgroup previously producedrank_sum = 2; it nowcorrectly produces
rank_sum = 1because the removed group is excluded from every window layer.Set
spark.sql.legacy.evaluateHavingAfterInnerWindowtotrueto restore the previousbehavior.
How was this patch tested?
Added SQL execution and analyzer golden coverage. On
b970dec8938b:window-resolution-edge-cases.sql,generators-resolution-edge-cases.sql, andwindow-with-aggregates.sqlpassed (6/6).git diff --check, changed-Scala line-length, and non-ASCII checks passed.Was this patch authored or co-authored using generative AI tooling?
Generated-by: OpenAI Codex (GPT-5)