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

   ### Is your feature request related to a problem or challenge?
   
   `HashJoinExec` doesn't implement `try_pushdown_sort`, so `PushdownSort` 
stops at a join even when the join keeps the order of its probe (right) side. A 
sort above the join then stays a TopK over a scan that could have been read in 
the requested order.
   
   For example, with `t` stored `id ASC`, `enable_sort_pushdown` and 
`enable_exact_reverse_scan` on:
   
   ```sql
   SELECT t.* FROM dim RIGHT SEMI JOIN t ON dim.category = t.category
   ORDER BY t.id DESC LIMIT 3;
   ```
   
   ```text
   SortExec: TopK(fetch=3), expr=[id@0 DESC], preserve_partitioning=[false]
     HashJoinExec: mode=CollectLeft, join_type=RightSemi, on=[(category@0, 
category@1)]
       DataSourceExec: partitions=1, partition_sizes=[1]
       DataSourceExec: ..., output_ordering=[id@0 ASC NULLS LAST], 
file_type=parquet, ...
   ```
   
   The same query without the join reverses the scan and removes the sort. 
`RightAnti` and `Inner` joins give the same plan as above.
   
   ### Describe the solution you'd like
   
   Implement `try_pushdown_sort` for `HashJoinExec` on the probe side, when the 
output follows the probe order:
   
   - `mode=CollectLeft` (the probe partitions pass through unchanged);
   - a join type for which `maintains_input_order` is true on the right 
(`Inner`, `Right`, `RightSemi`, `RightAnti`, `RightMark`);
   - every sort expression refers to probe-side columns, mapped through the 
join's output schema and projection.
   
   Then ask the right input, rebuild the join around the answer, and return 
`Exact` when the right input is `Exact` and the rebuilt join reports the 
ordering, `Inexact` when the right input could only be improved. The fetch 
stays above the join, since the join can drop or repeat probe rows.
   
   `SortMergeJoinExec`, `NestedLoopJoinExec` and `SymmetricHashJoinExec` could 
follow later.
   
   ### Describe alternatives you've considered
   
   Leaving the TopK above the join. The TopK dynamic filter already reaches the 
probe scan and prunes row groups, but with an exact answer the sort goes away 
and the probe stops after enough rows reach the limit.
   
   ### Additional context
   
   Part of #19394 and the sort pushdown epic #24270. #27982 adds the same for 
`UnionExec`; together they let an ordering reach scans below `UNION ALL` plus 
anti/semi join shapes (for example, merging a materialized table with a small 
set of overrides).
   


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