Skip to content

[SPARK-58503][SQL] Evaluate HAVING before window functions with generators - #57704

Closed
vladimirg-db wants to merge 2 commits into
apache:masterfrom
vladimirg-db:fix-having-before-window
Closed

[SPARK-58503][SQL] Evaluate HAVING before window functions with generators#57704
vladimirg-db wants to merge 2 commits into
apache:masterfrom
vladimirg-db:fix-having-before-window

Conversation

@vladimirg-db

@vladimirg-db vladimirg-db commented Aug 2, 2026

Copy link
Copy Markdown
Contributor

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:

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)

@vladimirg-db

Copy link
Copy Markdown
Contributor Author

@uros-b could you please help me merge this correctness issue fix? Thanks!

@vladimirg-db
vladimirg-db force-pushed the fix-having-before-window branch from b39a57b to 28a6b09 Compare August 2, 2026 18:22
@uros-b

uros-b commented Aug 2, 2026

Copy link
Copy Markdown
Member

Thank you @vladimirg-db!

@uros-b uros-b closed this in 22bffbb Aug 2, 2026
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>
@uros-b

uros-b commented Aug 2, 2026

Copy link
Copy Markdown
Member

Merge Summary:

Posted by merge_spark_pr.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants