Skip to content

Sort pushdown: let an Exact answer split into more partitions - #27987

Draft
zhuqi-lucas wants to merge 1 commit into
apache:mainfrom
zhuqi-lucas:qizhu/sort-pushdown-more-partitions
Draft

zhuqi-lucas wants to merge 1 commit into
apache:mainfrom
zhuqi-lucas:qizhu/sort-pushdown-more-partitions

Conversation

@zhuqi-lucas

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Rationale for this change

A source can sometimes satisfy an ordering only by splitting into more partitions, for example one file per partition when the files of a group are each sorted but overlap. Today try_pushdown_sort has no way to say that: in the standalone SortExec path, PushdownSort removes the sort on Exact without checking the partition count, so a source that split would leave several sorted streams with no merge above them.

What changes are included in this PR?

Steps 1 and 2 of the issue:

  • The try_pushdown_sort contract now allows an Exact answer with more partitions than the node it replaces, as long as every partition is in the requested order. An Inexact answer must keep the partitioning.
  • In the standalone SortExec path, when an Exact answer has a different partition count:
    • a sort that needed a single partition becomes a SortPreservingMergeExec with the sort's fetch, over a BufferExec, with the fetch also pushed into every partition;
    • a sort with preserve_partitioning is left alone, since its parent relies on the partitioning.
  • An Inexact answer that changes the partition count is ignored.

For example, a single-partition scan that splits into three sorted partitions:

SortExec: TopK(fetch=10), expr=[a@0 ASC], preserve_partitioning=[false]
  TestScan

becomes

SortPreservingMergeExec: [a@0 ASC], fetch=10
  BufferExec: capacity=357913941
    LocalLimitExec: fetch=10
      TestScan: requested_ordering=[a@0 ASC]

The BufferExec here shares sort_pushdown_buffer_capacity across the partitions instead of giving each partition all of it. The source picks the partition count, which can be large, and when the partitions don't overlap, every partition after the one being merged keeps reading until its buffer is full. Each partition still buffers at least one batch.

Step 3, letting FileScanConfig split overlapping groups itself, is left for a follow-up.

What is the testing strategy for this PR?

Five plan tests in pushdown_sort.rs, using a TestScan option that splits into more partitions on pushdown:

  • Exact with more partitions under a single-partition sort, with and without a fetch: the sort becomes a merge.
  • Exact with more partitions under a per-partition sort with a merge above: the existing merge pattern handles it.
  • Exact with more partitions under a per-partition sort without a merge: unchanged.
  • Inexact with more partitions: unchanged.

Without the change, four of them fail.

Are there any user-facing changes?

The try_pushdown_sort docs now allow Exact with more partitions. No API changes; existing sources keep their behavior.

A source that can only serve an ordering by splitting into more sorted
partitions may now answer Exact with more partitions. PushdownSort turns
a single-partition sort above it into a SortPreservingMergeExec with the
sort's fetch, sharing the buffer budget across the partitions. A
per-partition sort, or an Inexact answer that changes the partitioning,
is left alone.
@github-actions github-actions Bot added optimizer Optimizer rules core Core DataFusion crate physical-plan Changes to the physical-plan crate labels Dec 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core Core DataFusion crate optimizer Optimizer rules physical-plan Changes to the physical-plan crate

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Sort pushdown: let a source split into more partitions to turn a sort into a merge

1 participant