← Back to Pull requests

[SPARK-60873][SQL] Evaluate HAVING before nested window functions #60090

Open
unknown opened this pull request with 420 lines diff across 5 files
View on GitHub
unknown unknown commented on Nov 04, 2026 12:16 UTC
Author

What changes were proposed in this pull request?

This change places HAVING below every nested SELECT-list Window as an all-or-none
operation. The window-chain walk is bounded to its Project/Generate/Window spine,
and window-output classification inspects expression trees without traversing subquery plans.

For grouping analytics (ROLLUP, CUBE, and GROUPING SETS), an unresolved
DeferredHaving marker preserves the aggregate output only while window extraction builds the
window chain. The marker is converted to the placed Filter immediately after
ExtractWindowExpressions in 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 to false, to restore the
previous 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
HAVING below the complete SELECT-list window chain by default, while the legacy configuration
retains the prior plans and results.

Why are the changes needed?

The fixed-point analyzer previously extracted one window layer per iteration and immediately
converted HAVING to a Filter. A later iteration could insert an inner Window below that
filter. 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 HAVING should have removed and produce an incorrect
result.

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 HAVING when the condition does not depend on a window output or grouping function. For
the regression query, the surviving keep group previously produced rank_sum = 2; it now
correctly produces rank_sum = 1 because the removed group is excluded from every window layer.
Set spark.sql.legacy.evaluateHavingAfterInnerWindow to true to restore the previous
behavior.

How was this patch tested?

Added SQL execution and analyzer golden coverage. On b970dec8938b:

  • The execution and analyzer tests for window-resolution-edge-cases.sql,

  • generators-resolution-edge-cases.sql, and window-with-aggregates.sql passed (6/6).
  • Catalyst Scalastyle passed (741 source files, no findings).

  • 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)

    Diff: 420 lines (+315 / -105) across 5 files Reviewer: vladimirg-db Review duration: 37768m
    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
    0.45
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    46%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    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
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 46.7% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 45% (MEDIUM) · Higher = larger blast radius
    45%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 46.7% · Lower = rushed or shallow review
    46.7%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 24.0% · Safe to merge
    24.0%
    Repository