wangyum opened a new pull request, #60519:
URL: https://github.com/apache/spark/pull/60519

   ### What changes were proposed in this pull request?
   
   `WindowGroupLimitExec` did not implement `CodegenSupport` at all, so every 
top-k window query (`ROW_NUMBER`/`RANK`/`DENSE_RANK` filtered by a rank limit) 
forced a whole-stage codegen boundary around the operator. This adds codegen 
for all three rank-like functions, mirroring the interpreted iterators 
(`BaseLimitIterator`/`RankLimitIterator`/`DenseRankLimitIterator`) exactly:
   
   - **RowNumber**: a per-group `int` counter, reset on a partition-key change 
via the same group-boundary detection `SortAggregateExec` uses for its 
sort-based grouping.
   - **Rank/DenseRank**: a narrow `UnsafeRow` projection of just the order-by 
columns, compared through a narrow `Ordering` built over that projection's own 
ordinals. Admission uses the rank as it stood after the *previous* row, 
matching how `BaseLimitIterator.next()` checks `hasNext` before calling 
`increaseRank()` -- so the row that pushes a group's rank to the limit can 
still be admitted; harmless, since the exact `Filter` after the real 
`WindowExec` trims it if needed.
   - Once a group's rank has passed the limit, **all** order-key work (not just 
its result) is skipped for the rest of that group, mirroring 
`GroupedLimitIterator.skipRemainingRows`'s short-circuit, instead of projecting 
and comparing on every already-rejected row.
   - Columns stay lazy except the ones actually needed (`usedInputs` is empty, 
`FilterExec`-style); the partition/order key columns are claimed via 
`evaluateRequiredVariables` *before* any `consume()` call, so a column shared 
between the output and the order-by expression -- or a non-deterministic 
order-by expression -- is evaluated exactly once per row, not twice.
   
   TPC-DS plan-stability golden files are updated mechanically: 
`WindowGroupLimit` now fuses into the surrounding `WholeStageCodegenExec` stage 
for `rank()`-based queries (q44, q67, q70, and their `.sf100`/`v2_7` variants); 
the diffs are purely the node moving inside the stage boundary, with no other 
structural change.
   
   ### Why are the changes needed?
   
   Top-k-per-group (`ROW_NUMBER`/`RANK`/`DENSE_RANK() ... WHERE rn <= k`) is a 
common SQL idiom, and `InferWindowGroupLimit` already pushes a 
`WindowGroupLimitExec` below `WindowExec` for it by default 
(`spark.sql.optimizer.windowGroupLimitThreshold` defaults to 1000). Without 
codegen, this operator forced every such query onto the interpreted, 
iterator-based execution path for this part of the plan, giving up whole-stage 
codegen's usual per-row speedup.
   
   ### Does this PR introduce _any_ user-facing change?
   
   No. Query results are unchanged (verified by comparing codegen output 
against both the interpreted path and, for `RANK`/`DENSE_RANK`, a ground truth 
with the `WindowGroupLimit` optimization disabled entirely -- a structurally 
different plan). The only visible difference is in `EXPLAIN`/plan-stability 
output, where `WindowGroupLimit` now appears nested inside a 
`WholeStageCodegen` stage instead of as a sibling node.
   
   ### How was this patch tested?
   
   - Extended `DataFrameWindowFunctionsSuite`.
   - Regenerated and manually diffed the TPC-DS plan-stability golden files.
   - Added a "Top-K with Skewed Partition" case to `TopKBenchmark`.
   
   ### 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]

Reply via email to