Skip to content

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

Merged
andygrove merged 4 commits into
apache:mainfrom
dwsmith1983:fix/7553-doubted-window-full-order
Nov 14, 2026
Merged

andygrove merged 4 commits 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

@andygrove andygrove left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PR backlog is huge and the maintainers don't have the bandwidth to review them all, so here is an automated review. Feel free to ignore it.

I found no blocking concerns. Sorting by the whole order the window's input reports is the right shape for this fix. Spark's WindowExec passes child.outputOrdering on as its own, so the operators Spark planned above it can rely on more than requiredChildOrdering, and a sort by the window's own keys loses that. I checked that the new sortDoubtedWindowInputs branch behaves sensibly in the cases I could construct. When the child reports an order that satisfies the window's keys, the sort keeps the longer order. When Spark had to plan its own sort, mayReportFalseInputOrder stops at the SortExec and the rule does not fire. Because the rule is transformUp, a second Spark window above the first sees a SortExec below it and is not sorted again.

Against branch-1.2 the rule does not exist at all, so this only refines unreleased code from #7256 and changes nothing a user on the release sees. The contributor guide paragraph in adding_a_new_operator.md matches the new behavior.

The tests exercise both ways the order can be lost, and assertOnlySortUnderLowerWindow pins the plan shape the test depends on, which keeps the tests from passing for the wrong reason.

@andygrove andygrove left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks @dwsmith1983

@andygrove
andygrove enabled auto-merge November 13, 2026 18:00
@dwsmith1983

Copy link
Copy Markdown
Contributor Author

LGTM. Thanks @dwsmith1983

It seems to be stuck sitting in queuing. Not sure why.

@dwsmith1983

Copy link
Copy Markdown
Contributor Author

@andygrove CI on the earlier merge commit failed before starting any jobs, so Required Checks never reported and auto-merge never queued the PR. I merged main to get a new run, but the workflows are waiting on approval. Could you approve them? Auto-merge is still on, so it should queue once they pass.

@andygrove
andygrove added this pull request to the merge queue Nov 14, 2026
Merged via the queue into apache:main with commit fd82542 Nov 14, 2026
23 of 24 checks passed
dwsmith1983 added a commit to dwsmith1983/datafusion-comet that referenced this pull request Nov 14, 2026
…anner-strategy

A native nested loop join over a sorted cache now stays in Spark, so the
two window tests from apache#7555 move to a broadcast outer hash join over a
native filter, which a Spark window above still doubts.
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

2 participants