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

   ## Which issue does this PR close?
   
   - Closes #27909.
   
   ## Rationale for this change
   
   `UnionExec` doesn't implement `try_pushdown_sort`, so `PushdownSort` stops 
at it and the sources below a `UNION ALL` never see the requested ordering. A 
`DESC LIMIT` over a union of two scans stored `ASC` keeps a TopK per partition 
instead of reading each scan in reverse.
   
   ## What changes are included in this PR?
   
   `UnionExec::try_pushdown_sort` asks every input:
   
   - every input `Exact`: the union is `Exact`, if the rebuilt union reports 
the ordering;
   - otherwise, if any input is `Exact` or `Inexact`: `Inexact`, with the 
inputs that answered rewritten and the others kept;
   - every input `Unsupported`: `Unsupported`.
   
   Each output partition of a union is one input partition, so `Exact` from 
every input means every output partition is in order. A union with several 
partitions sits below a per-partition sort under a `SortPreservingMergeExec`, 
or below a `CoalescePartitionsExec`, which already downgrades `Exact` to 
`Inexact`. So the merge across partitions is already in place and nothing is 
added above the union.
   
   For example, with `enable_exact_reverse_scan`:
   
   ```text
   SortPreservingMergeExec: [id@0 DESC], fetch=4
     BufferExec: capacity=1073741824
       UnionExec
         DataSourceExec: ..., limit=4, output_ordering=[id@0 DESC], 
reverse_row_groups=true, reverse_rows=true
         DataSourceExec: ..., limit=4, output_ordering=[id@0 DESC], 
reverse_row_groups=true, reverse_rows=true
   ```
   
   ## What is the testing strategy for this PR?
   
   - 4 plan tests in `pushdown_sort.rs`: every input exact, one input inexact, 
one input unsupported, and every input unsupported (plan unchanged).
   - `sort_pushdown_exact_reverse.slt` Test 17: plan and rows for a `UNION ALL` 
of two parquet tables ordered `DESC LIMIT 4`.
   
   Without the change, the first three plan tests and the slt case fail: the 
sort stays above the union.
   
   ## Are there any user-facing changes?
   
   No API changes. Plans for an `ORDER BY` over a `UNION ALL` can now push the 
ordering into the inputs.
   


-- 
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