Skip to content

fix: sort a Spark window's doubted input by the whole order it reports - #7555

Open
dwsmith1983 wants to merge 1 commit into
apache:mainfrom
dwsmith1983:fix/7553-doubted-window-full-order
Open

dwsmith1983 wants to merge 1 commit into
apache:mainfrom
dwsmith1983:fix/7553-doubted-window-full-order

Conversation

@dwsmith1983

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Closes #7553.

#6785 should merge after this one: it restores the whole reported order the same way for windows that run natively, and its Spark fallback relies on this rule.

Rationale for this change

When a window stays in Spark over an input whose order Comet may report falsely, sortDoubtedWindowInputs sorts that input again, but only by the window's own keys. Spark had already planned the operators above the window around the longer order the input reported, so a second window ordered by more keys, or a sortWithinPartitions Spark removed as redundant, sees rows out of order. That happens when the input really is out of order past the window's keys (a native nested loop join with more than one build row), and also when it is in order but the inserted sort spills and the merge reorders ties.

What changes are included in this PR?

  • The inserted sort uses the whole order the window's input reports when that order covers the window's keys, and the window's own keys otherwise.
  • The contributor guide's note on this rule says the same.

How are these changes tested?

Two tests in CometInMemoryCacheSuite run a Spark window ordered by v under one ordered by (v, w) over a sorted cache joined through a native nested loop join, and check the rows against Spark, the order of a sortWithinPartitions("v", "w") above the lower window, and that the only sort in the plan is the one this rule adds, by (v, w). One uses two build rows, the other one build row with a forced spill. Both fail without the change.

sortDoubtedWindowInputs sorted the input of a window that stays in
Spark by the window's own keys only. Spark had already planned the
operators above around the longer order the input reported, so a
second window ordered by more keys, or a sortWithinPartitions it had
removed, saw rows out of order. The sort now uses the whole reported
order when it covers the window's keys.

Closes apache#7553.
@github-actions github-actions Bot added the bug Something isn't working label Nov 7, 2026
@dwsmith1983
dwsmith1983 marked this pull request as ready for review November 7, 2026 12:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Spark window over a doubted input order gets a sort by its own keys only, and operators above it return wrong rows

1 participant