[SPARK-58757][SQL] Allow CollapseWindow to merge windows with an empty order spec - #57986
Open
ulysses-you wants to merge 1 commit into
Open
[SPARK-58757][SQL] Allow CollapseWindow to merge windows with an empty order spec#57986ulysses-you wants to merge 1 commit into
ulysses-you wants to merge 1 commit into
Conversation
…y order spec
Two adjacent Window operators can currently be collapsed only when their
order specs are identical. This relaxes CollapseWindow to also merge a
window whose order spec is empty into a sibling window with a non-empty
order spec, provided every window expression of the empty-order window is
order-insensitive.
A window expression is order-insensitive when its frame is the whole
partition (UNBOUNDED PRECEDING to UNBOUNDED FOLLOWING): the frame always
covers every row of the partition regardless of ordering, so aggregates
like count or sum give the same value under any ordering, and functions
whose result depends on the row order (e.g. collect_list, first) are
non-deterministic when the order spec is empty, so evaluating them under
any ordering yields a valid result. A bounded frame is order-sensitive and
is therefore never merged.
The merged window keeps the non-empty order spec of the other window, so
a query like
SELECT c2, c1,
row_number() OVER (PARTITION BY c1 ORDER BY c2) AS rk,
count(1) OVER (PARTITION BY c1)
FROM t3
now runs with a single window operator instead of two, saving one
WindowExec pass (the sort on [c1, c2] is shared in both plans).
Co-Authored-By: Claude <noreply@anthropic.com>
uros-b
approved these changes
Aug 13, 2026
Member
|
LGTM, thank you @ulysses-you! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What changes were proposed in this pull request?
Currently,
CollapseWindowcollapses two adjacentWindowoperators only when their partition specs and order specs are identical. This PR relaxes it to also merge two windows with the same partition spec when one of them has an empty order spec, as long as every window expression of the empty-order window is order-insensitive.A window expression is treated as order-insensitive when its frame is the whole partition (
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING): such a frame always covers every row of the partition regardless of the ordering, so aggregates likecount/sum/min/maxgive the same value under any ordering, and functions whose result does depend on the row order (e.g.collect_list,first) are non-deterministic when the order spec is empty, so evaluating them under any ordering yields a valid result. Windows with a bounded frame (e.g.ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) are order-sensitive and are never merged.The merged window keeps the non-empty order spec of the other window.
Why are the changes needed?
For queries that mix an ordered window function with an unordered aggregate over the same partition, e.g.:
the current rule keeps two
Windowoperators even though they sharePARTITION BY c1. After this change they are collapsed into a single window operator, saving oneWindowExecpass (the[c1, c2]sort is already shared in both plans). A local benchmark on 4M rows showed roughly 16% faster runtime in the non-spill case and 20% in the spill case.Does this PR introduce any user-facing change?
The query result is unchanged for the common shape (the empty-order window written after the ordered one), where the merge does not change the input order of any window expression. When the empty-order window appears before an ordered sibling in the SELECT list, the merge evaluates its expressions under the sibling's order; for functions documented as non-deterministic without an order (
first,last,collect_list) the value may differ, which is already allowed by their contract. FPsum/avgmay also differ at the bit level, consistent with Spark's existing treatment of FP aggregation as order-sensitive (EliminateSorts.isOrderIrrelevantAggs).How was this patch tested?
Added tests to
CollapseWindowSuitecovering:row_number+count), including the case where it is the inner window;firstover the whole partition;Projectbetween the windows;Ran
CollapseWindowSuite(13 tests) andTransposeWindowSuite(8 tests), all pass.Was this patch authored or co-authored using generative AI tooling?
Yes, developed with assistance from Claude Code.
Generated-by: Claude Code