zhuqi-lucas opened a new pull request, #27987:
URL: https://github.com/apache/datafusion/pull/27987

   ## Which issue does this PR close?
   
   - Closes #27908.
   
   ## 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:
   
   ```text
   SortExec: TopK(fetch=10), expr=[a@0 ASC], preserve_partitioning=[false]
     TestScan
   ```
   
   becomes
   
   ```text
   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.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to