shivadarshan-devadiga opened a new pull request, #60749:
URL: https://github.com/apache/spark/pull/60749
### What changes were proposed in this pull request?
`ResolveQualify` resolves the `QUALIFY` condition against the `Project` that
holds the `SELECT` list, starting from that `Project`'s children. By then
`ExtractWindowExpressions` has moved the window functions of the `SELECT` list
below it, into a `Window` node and a `Project` that passes its results up. So
an unqualified name that is both an input column and the alias of a window
function resolves to the window function, and the input column, which the
`SELECT` list doesn't keep, is never considered:
```
Filter (rn#7 = 1) <- the SELECT alias, not the input
column rn#6
+- Project [id#4, rn#7]
+- Project [id#4, score#5, rn#7, rn#7]
+- Window [row_number() ... AS rn#7]
+- Project [id#4, score#5]
+- SubqueryAlias t
+- LocalRelation [id#4, score#5, rn#6]
```
This PR makes the `Project` case of `resolveQualifyCondition` leave out the
attributes that the `Window` nodes of the query block define (`windowOutputs`,
which stops at the block's `SubqueryAlias`) when it resolves against the
children. A name that is also an input column then recurses down to the input,
and the existing code carries the column up through the intermediate nodes. A
name that only matches a window alias is still resolved against the `SELECT`
list afterwards, as before. Aliases of aggregates are not window outputs, so
they resolve as before.
### Why are the changes needed?
`QUALIFY` docs say: "When an alias in the `SELECT` list has the same name as
an input column, the input column takes precedence." Instead, the alias wins,
and the filter keeps different rows, without an error:
```sql
SELECT id, ROW_NUMBER() OVER (ORDER BY score DESC) AS rn
FROM VALUES (1, 30, 0), (2, 20, 0), (3, 10, 1) AS t(id, score, rn)
QUALIFY rn = 1
ORDER BY id;
```
It returns `(1, 1)`, where `QUALIFY t.rn = 1` returns `(3, 3)`.
### Does this PR introduce _any_ user-facing change?
Yes. In `QUALIFY`, an unqualified name that is both an input column and the
`SELECT` alias of a window function now refers to the input column, as
documented. `QUALIFY` is new in 5.0.0, so no released version is affected.
### How was this patch tested?
New queries in `sql-tests/inputs/qualify.sql`:
- The query above, which now returns `(3, 3)`, and its `t.rn` control.
- An aggregate alias with the name of an input column (`SUM(b) AS b ...
QUALIFY b > 2`), which still refers to the aggregate.
- A window alias of a subquery, which is an input column of the outer query.
Without the change, only the first query differs in the golden files (`1 1`
instead of `3 3`). `SQLQueryTestSuite` for `qualify.sql` and the window
queries, `AnalysisSuite`, `DataFrameWindowFunctionsSuite` and
`SparkSqlParserSuite` pass, and `scalastyle` is clean for `catalyst` and `sql`.
### Was this patch authored or co-authored using generative AI tooling?
Co-authored by: Claude Sonnet 4.5
--
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]