ulysses-you commented on PR #60517:
URL: https://github.com/apache/spark/pull/60517#issuecomment-6905664271
Thank you @wangyum for working on this. Dedup via `ROW_NUMBER() ... WHERE rn
= 1` is one of the most common query shapes we see, and once
`WindowGroupLimit(limit = 1)` has already done the work, dropping the redundant
`Window`/`Filter` is a clean, worthwhile optimization. I ran some end-to-end
probes against the PR head; one issue needs fixing before merge, and there is
one suggestion that I think would extend the win to more queries.
**1. [Blocking] Wrong results with a collated partition key**
(`InferWindowGroupLimit.scala:165`)
The rewrite makes `WindowGroupLimitExec` the only operator that enforces one
row per group. But it doesn't group rows the same way `WindowExec` does:
- `GroupedLimitIterator` compares group keys by raw bytes (`nextGroup ==
currentGroup`).
- `WindowExec` uses a collation-aware comparison for keys that aren't binary
stable (`groupEqualityCheck` in `WindowEvaluatorFactory`, SPARK-47443 (4.0.0)).
On master today this mismatch is harmless: `WindowGroupLimit` only ever
keeps *more* rows than needed, and the `Window`/`Filter` above re-ranks them
and drops the extras. With this PR those two nodes are gone, so the extra rows
reach the result.
```sql
CREATE TEMP VIEW t3 AS SELECT * FROM VALUES
('a' COLLATE UTF8_LCASE, 1), ('A' COLLATE UTF8_LCASE, 2), ('a' COLLATE
UTF8_LCASE, 3),
('b' COLLATE UTF8_LCASE, 4), ('B' COLLATE UTF8_LCASE, 5) AS t(k, v);
SELECT * FROM (
SELECT *, row_number() OVER (PARTITION BY k ORDER BY v) AS rn FROM t3
) WHERE rn = 1;
```
| | rows |
|---|---|
| master | 2: `[a,1,1], [b,4,1]` |
| this PR | 4: `[A,2,1], [B,5,1], [a,1,1],
[b,4,1]` |
| `InferWindowGroupLimit` excluded | 2 |
Same with AQE on and off. Float keys are fine: `NormalizeFloatingNumbers`
runs before this batch, and `WindowGroupLimit` copies the already-normalized
`partitionSpec`.
Suggested fix: only eliminate when `partitionSpec.forall(e =>
UnsafeRowUtils.isBinaryStable(e.dataType))`. Making `GroupedLimitIterator`
collation-aware would fix it at the root, but that seems better as a separate
ticket.
**2. An end-to-end test would help**
All the new tests compare catalyst plans, which is why the case above isn't
caughnswer` test in `DataFrameWindowFunctionsSuite` with tied order keys, NULL
keys and a `UTF8_LCASE` key, comparing against the result with the rule
excluded, and asserting that the executed plan has no `WindowExec`?
**3. Suggestion: also cover `rn = 1 AND <predicate on another column>`**
`isTop1` passes the whole condition to `extractLimit`, so `rn = 1 AND v > 6`
gets `WindowGroupLimit` inserted (through `extractLimits`) but keeps the
`Window`. Predicates on partition
columns aren't affected, since they are already pushed below the `Window`.
What rates that must run after ranking: on the ORDER BY column or on other
columns.Those are common in practice, e.g. "latest row per key, if it is not
deleted".
Splitting the conjuncts would cover them: drop the top-1 conjunct and keep
the rest as a `Filter` over the new `Project`:
```
Filter (v > 6)
Project (k, v, s, 1 AS rn)
WindowGroupLimit(k, v, row_number, 1)
child
```
This is equivalent because the `WindowGroupLimit(1)` output is exactly the
`rn = predicate is evaluated on the same row with `rn` bound to 1. It needs
two guards: every remaining conjunct deterministic (so `rand()` isn't evaluated
a different number of times), plus the binary-stable check from (1).
I prototyped this locally:
- Results match the rule-excluded plan, with AQE on and off, for predicates
on thr columns, `IS NOT NULL`, mixed `OR`, and a leftover that reads `rn`.
Theprototype correctly does not fire for a nondeterministic leftover or a
`UTF8_LCASE` key.
- On 20M rows / 5M keys with `rn = 1 AND m <> 0`: 1890 ms with the `Window`
kept (about 13%), with identical results.
**Nits**
- The test "preserves rn's qualifier and metadata" also passes when the
`Window` is kept. Adding `assert(!result.exists(_.isInstanceOf[Window]))` would
make it test the rewrite.
--
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]