Skip to content

[SPARK-58757][SQL] Allow CollapseWindow to merge windows with an empty order spec - #57986

Open
ulysses-you wants to merge 1 commit into
apache:masterfrom
ulysses-you:collapse-window-empty-order
Open

[SPARK-58757][SQL] Allow CollapseWindow to merge windows with an empty order spec#57986
ulysses-you wants to merge 1 commit into
apache:masterfrom
ulysses-you:collapse-window-empty-order

Conversation

@ulysses-you

Copy link
Copy Markdown
Contributor

What changes were proposed in this pull request?

Currently, CollapseWindow collapses two adjacent Window operators 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 like count/sum/min/max give 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.:

SELECT c2, c1,
       row_number() OVER (PARTITION BY c1 ORDER BY c2) AS rk,
       count(1)     OVER (PARTITION BY c1)
FROM t3

the current rule keeps two Window operators even though they share PARTITION BY c1. After this change they are collapsed into a single window operator, saving one WindowExec pass (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. FP sum/avg may 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 CollapseWindowSuite covering:

  • collapse when the empty-order window has a whole-partition frame (row_number + count), including the case where it is the inner window;
  • collapse when the empty-order window has multiple window expressions;
  • collapse when the empty-order window has first over the whole partition;
  • the SPARK-34565 shape with a Project between the windows;
  • no collapse when the empty-order window has a bounded frame.

Ran CollapseWindowSuite (13 tests) and TransposeWindowSuite (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

…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

uros-b commented Aug 13, 2026

Copy link
Copy Markdown
Member

LGTM, thank you @ulysses-you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants