[SPARK-58503][SQL] Evaluate HAVING before window functions with generators - #57704
Closed
vladimirg-db wants to merge 2 commits into
Closed
[SPARK-58503][SQL] Evaluate HAVING before window functions with generators#57704vladimirg-db wants to merge 2 commits into
vladimirg-db wants to merge 2 commits into
Conversation
Contributor
Author
|
@uros-b could you please help me merge this correctness issue fix? Thanks! |
vladimirg-db
force-pushed
the
fix-having-before-window
branch
from
August 2, 2026 18:22
b39a57b to
28a6b09
Compare
uros-b
approved these changes
Aug 2, 2026
Member
|
Thank you @vladimirg-db! |
uros-b
pushed a commit
that referenced
this pull request
Aug 2, 2026
…ators ### What changes were proposed in this pull request? Place a resolved HAVING filter below the complete Window chain when generator extraction has inserted Project and Generate operators above it. If the condition's attributes are not available below every Window, retain the existing fallback placement. Add regression coverage for HAVING conditions on grouping columns and aggregate aliases, including multiple window specifications, and document the corrected behavior in the SQL migration guide. ### Why are the changes needed? Generator extraction can hide the Window chain from the analyzer's normal HAVING handling. The remaining fallback then places HAVING above Window, so window functions see groups that HAVING removes. ### Does this PR introduce _any_ user-facing change? Yes. Window functions are now evaluated after HAVING for queries that also select generators. For example, the window count from this query changes from 4 to the correct value 3: ```sql SELECT explode(array(a)), count(*) OVER (), a FROM VALUES (1), (2), (3), (NULL) AS t(a) GROUP BY a HAVING a IS NOT NULL; ``` ### How was this patch tested? Added SQL execution and analyzer golden coverage and ran: ``` build/sbt "sql/testOnly org.apache.spark.sql.SQLQueryTestSuite -- -z generators-resolution-edge-cases.sql" ``` ### Was this patch authored or co-authored using generative AI tooling? Generated-by: OpenAI Codex (GPT-5) Closes #57704 from vladimirg-db/fix-having-before-window. Authored-by: Vladimir Golubev <vladimir.golubev@databricks.com> Signed-off-by: Uros Bojanic <221401595+uros-b@users.noreply.github.com> (cherry picked from commit 22bffbb) Signed-off-by: Uros Bojanic <221401595+uros-b@users.noreply.github.com>
Member
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?
Place a resolved HAVING filter below the complete Window chain when generator extraction has
inserted Project and Generate operators above it. If the condition's attributes are not available
below every Window, retain the existing fallback placement.
Add regression coverage for HAVING conditions on grouping columns and aggregate aliases, including
multiple window specifications, and document the corrected behavior in the SQL migration guide.
Why are the changes needed?
Generator extraction can hide the Window chain from the analyzer's normal HAVING handling. The
remaining fallback then places HAVING above Window, so window functions see groups that HAVING
removes.
Does this PR introduce any user-facing change?
Yes. Window functions are now evaluated after HAVING for queries that also select generators. For
example, the window count from this query changes from 4 to the correct value 3:
How was this patch tested?
Added SQL execution and analyzer golden coverage and ran:
Was this patch authored or co-authored using generative AI tooling?
Generated-by: OpenAI Codex (GPT-5)