Skip to content

[branch-56] fix: preserve local TopK limits during exact sort pushdown (backport #26736) - #27784

Merged
zhuqi-lucas merged 1 commit into
apache:branch-56from
zhuqi-lucas:backport-56-26736
Nov 27, 2026
Merged

zhuqi-lucas merged 1 commit into
apache:branch-56from
zhuqi-lucas:backport-56-26736

Conversation

@zhuqi-lucas

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Rationale for this change

On 56.0.0, an ORDER BY ... LIMIT query can fail plan validation with a SinglePartition distribution error when a source accepts an exact sort pushdown but cannot apply the limit itself: the sort is removed and replaced by a GlobalLimitExec over several partitions. #26736 fixed this on main.

What changes are included in this PR?

A cherry-pick of #26736 (8ad8490) with -x. It applied cleanly.

What is the testing strategy for this PR?

The regression tests from #26736 (exact_sort_pushdown test context and its sqllogictest) pass on this branch, along with sort_pushdown*.slt and limit.slt.

Are there any user-facing changes?

Affected queries run instead of failing plan validation. No public API changes.

)

## Which issue does this PR close?

- Closes apache#26739.

## Rationale for this change

Sort pushdown can make an `ORDER BY ... LIMIT` query fail plan
validation when a source guarantees the requested ordering but cannot
apply the row limit itself.

A partition-preserving TopK keeps up to `k` rows **per partition**.
Removing its sort must retain that local limit. The standalone
exact-pushdown path always inserted `GlobalLimitExec` instead, which
requires a single input partition. With a four-partition source, the
resulting plan fails with a `SinglePartition` distribution error.

The direct merge/sort path already uses a local fallback. The standalone
path needs to preserve the same limit scope.

## What changes are included in this PR?

When the source cannot absorb the fetch, choose `LocalLimitExec` if the
removed sort preserves partitions, and `GlobalLimitExec` otherwise.

## What is the testing strategy for this PR?

The SQL regression uses a native running-sum window over a
four-partition source, with an explicit `ORDER BY group_id, value LIMIT
3`. The custom source only supplies data and accepts exact sort
pushdown; it cannot apply a limit.

The window is needed to reach the affected optimization: `PARTITION BY`
requires redistributing this source's rows by group, and the outer
`LIMIT` lets the optimizer add a local TopK before that redistribution.
Without the window, this query takes the already-correct merge/sort
path.

The test checks the three expected rows and uses `EXPLAIN` to verify
that `LocalLimitExec` remains below the repartition. Without the fix,
the query fails plan validation. Removing the limit entirely would fail
the plan assertion.

[Two direct optimizer
tests](https://github.com/apache/datafusion/blob/a7f447f4831d4d906d82968b287dc72dea72aca2/datafusion/sqllogictest/src/test_context/exact_sort_pushdown.rs#L134)
reuse the same source: one checks the global-limit fallback and three
returned rows; the other checks sort elimination without a limit and all
16 rows.

## Are there any user-facing changes?

Affected queries execute successfully while preserving the per-partition
and overall row limits. No public API changes.

Developed with AI assistance.

(cherry picked from commit 8ad8490)
Copilot AI balanced review requested due to automatic review settings November 27, 2026 03:14

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@codecov-commenter

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 84.55882% with 21 lines in your changes missing coverage. Please review.
✅ Project coverage is 83.30%. Comparing base (69d40d1) to head (b5b26d4).
⚠️ Report is 1 commits behind head on branch-56.

Files with missing lines Patch % Lines
...llogictest/src/test_context/exact_sort_pushdown.rs 83.96% 11 Missing and 10 partials ⚠️
Flag Coverage Δ
all-tests 83.30% <84.55%> (-0.01%) ⬇️
sqllogictests 60.86% <79.48%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

@@              Coverage Diff              @@
##           branch-56   #27784      +/-   ##
=============================================
- Coverage      83.30%   83.30%   -0.01%     
=============================================
  Files           1170     1171       +1     
  Lines         476209   476344     +135     
  Branches      476209   476344     +135     
=============================================
+ Hits          396694   396806     +112     
- Misses         55487    55499      +12     
- Partials       24028    24039      +11     
Files with missing lines Coverage Δ
datafusion/physical-optimizer/src/pushdown_sort.rs 94.50% <100.00%> (+0.12%) ⬆️
datafusion/sqllogictest/src/test_context.rs 94.66% <100.00%> (+0.01%) ⬆️
...llogictest/src/test_context/exact_sort_pushdown.rs 83.96% <83.96%> (ø)

... and 7 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@zhuqi-lucas
zhuqi-lucas merged commit 9e9163e into apache:branch-56 Nov 27, 2026
38 checks passed
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.

5 participants