zhuqi-lucas opened a new issue, #27909:
URL: https://github.com/apache/datafusion/issues/27909

   ### Is your feature request related to a problem or challenge?
   
   `UnionExec` doesn't implement `try_pushdown_sort`, so `PushdownSort` stops 
at it and the sources below never see the requested ordering. For example, 
`ORDER BY ts DESC LIMIT 10` over a `UNION ALL` of two tables stored `ts ASC` 
keeps a TopK per partition, with this plan shape:
   
   ```text
   SortPreservingMergeExec: [ts DESC], fetch=10
     SortExec: TopK(fetch=10), expr=[ts DESC], preserve_partitioning=[true]
       UnionExec
         DataSourceExec: output_ordering=[ts ASC]
         DataSourceExec: output_ordering=[ts ASC]
   ```
   
   If each source could answer for itself, both would be reversed and the 
per-partition sort removed, as for a single `DataSourceExec`.
   
   ### Describe the solution you'd like
   
   Implement `try_pushdown_sort` on `UnionExec` by asking every child:
   
   - every child `Exact`: rebuild the union around the new children and return 
`Exact`;
   - otherwise, if any child is `Exact` or `Inexact`: rebuild with the new 
children, keep the others, and return `Inexact`;
   - every child `Unsupported`: `Unsupported`.
   
   Each output partition of `UnionExec` comes from exactly one child partition, 
so `Exact` from every child means every output partition is in the order. A 
multi-partition `UnionExec` only sits below a per-partition sort under a 
`SortPreservingMergeExec`, or below a `CoalescePartitionsExec`, which already 
downgrades `Exact` to `Inexact`, so no merge has to be added.
   
   `InterleaveExec` combines partitions of several children into one, so it 
should stay `Unsupported`.
   
   ### Describe alternatives you've considered
   
   A custom rule that reverses the union's children itself. That works for one 
shape, but not for unions that other rules create.
   
   ### Additional context
   
   Part of #19394 and #24270.
   


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