wangyum opened a new pull request, #60517:
URL: https://github.com/apache/spark/pull/60517
### What changes were proposed in this pull request?
`InferWindowGroupLimit` already rewrites
```
Filter(rn = 1)
Window(row_number() AS rn, partitionSpec, orderSpec)
child
```
into
```
Filter(rn = 1)
Window(row_number() AS rn, partitionSpec, orderSpec)
WindowGroupLimit(partitionSpec, orderSpec, row_number(), limit = 1)
child
```
This PR extends that same rule so that, once the limit is exactly `1` and the
`Window` computes nothing besides that one `row_number()`, it goes one step
further and eliminates the `Window`/`Filter` entirely:
```
Project(..., 1 AS rn)
WindowGroupLimit(partitionSpec, orderSpec, row_number(), limit = 1)
child
```
`WindowGroupLimit(limit = 1)` already guarantees at most one row survives per
partition group, so a `row_number()` recomputed over that single-row group
can
only ever be `1`, and the `rn = 1`-style filter on top is therefore always
true — both nodes are dead computation.
The rewrite only fires when all of the following hold, to keep it provably
safe:
- `limit == 1` and the rank-like function is `RowNumber` specifically (not
`Rank`/`DenseRank`, since ties could let more than one row through
`WindowGroupLimit` even at `limit = 1`);
- the `Window` has exactly one window expression (so nothing else computed by
the `Window` is lost by replacing it with a `Project`);
- the `Filter` condition is *exactly* a top-1 predicate (`rn = 1`, `1 = rn`,
`rn <= 1`, `1 >= rn`, `rn < 2`, or `2 > rn`) and not a larger conjunction
(e.g. `rn = 1 AND other_pred`), so no other predicate is silently dropped;
and
- the `Window` carries no `WindowHint` (a bare `Project` has nowhere to carry
one).
### Why are the changes needed?
`SELECT * FROM (SELECT *, ROW_NUMBER() OVER (PARTITION BY k ORDER BY ts) rn
FROM t) WHERE rn = 1` (dedup / "latest row per key") is a very common query
shape. Once `WindowGroupLimit` has already done the work of truncating each
partition to one row, recomputing `row_number()` and then filtering on it
again is pure wasted computation on a hot optimizer path — this change
removes that redundant work.
### Does this PR introduce _any_ user-facing change?
No. Query results are unchanged; this only affects the optimized plan shape
(and `EXPLAIN` output) for the specific top-1 `row_number()` pattern
described
above.
### How was this patch tested?
Added unit tests in `InferWindowGroupLimitSuite`:
- the elimination fires for all 6 recognized top-1 predicate forms, crossed
with ascending/descending order, a multi-column partition/order spec, and
an empty `partitionSpec` (with `topKSortFallbackThreshold` lowered to force
past the pre-existing `Limit`+`Sort` fallback path);
- the rewritten output keeps the original attribute's `exprId`, `qualifier`,
and `metadata`;
- the elimination is correctly *not* applied when: the `Window` computes more
than the one `row_number()`; the rank-like function is `Rank`/`DenseRank`;
the filter has an extra conjunct beyond the top-1 predicate; or the
`Window` carries a hint.
Also ran the full `InferWindowGroupLimitSuite` (22 tests, all passing) and
confirmed `sql/catalyst` and `sql/core` compile cleanly.
### Was this patch authored or co-authored using generative AI tooling?
Generated-by: Cursor (GLM 5.3)
--
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]