zhuqi-lucas commented on code in PR #16641: URL: https://github.com/apache/datafusion/pull/16641#discussion_r2179151489
########## datafusion/physical-optimizer/src/enforce_sorting/sort_pushdown.rs: ########## @@ -668,6 +668,15 @@ fn handle_hash_join( plan: &HashJoinExec, parent_required: OrderingRequirements, ) -> Result<Option<Vec<Option<OrderingRequirements>>>> { + // Anti-joins (LeftAnti or RightAnti) do not preserve meaningful input order, + // so sorting beforehand cannot be relied on. Bail out early for both flavors: + match plan.join_type() { + JoinType::LeftAnti | JoinType::RightAnti => { + return Ok(None); + } + _ => {} + } Review Comment: Updated, i checked the code, we will not push down limit for anti join: https://github.com/apache/datafusion/blob/25c2a079fc1baacc038e304cb2bcae671338c2d8/datafusion/optimizer/src/push_down_limit.rs#L256 And example: ```rust CREATE TABLE t1 AS VALUES (1, 'a'), (2, 'b'), (3, 'c'); 0 row(s) fetched. Elapsed 0.004 seconds. > CREATE TABLE t2 AS VALUES (1, 'a'); 0 row(s) fetched. Elapsed 0.003 seconds. > -- LIMIT potentially pushed down (depends on optimizer) SELECT * FROM t1 LEFT ANTI JOIN t2 ON t1.column1 = t2.column1 LIMIT 2; +---------+---------+ | column1 | column2 | +---------+---------+ | 2 | b | | 3 | c | +---------+---------+ 2 row(s) fetched. Elapsed 0.003 seconds. > -- LIMIT applied after anti join (expected) SELECT * FROM ( SELECT * FROM t1 LEFT ANTI JOIN t2 ON t1.column1 = t2.column1 ) LIMIT 2; +---------+---------+ | column1 | column2 | +---------+---------+ | 2 | b | | 3 | c | +---------+---------+ 2 row(s) fetched. Elapsed 0.002 seconds. > EXPLAIN SELECT * FROM t1 LEFT ANTI JOIN t2 ON t1.column1 = t2.column1 LIMIT 2; +---------------+------------------------------------------------------------+ | plan_type | plan | +---------------+------------------------------------------------------------+ | physical_plan | ┌───────────────────────────┐ | | | │ CoalesceBatchesExec │ | | | │ -------------------- │ | | | │ limit: 2 │ | | | │ │ | | | │ target_batch_size: │ | | | │ 8192 │ | | | └─────────────┬─────────────┘ | | | ┌─────────────┴─────────────┐ | | | │ HashJoinExec │ | | | │ -------------------- │ | | | │ join_type: RightAnti │ | | | │ ├──────────────┐ | | | │ on: │ │ | | | │ (column1 = column1) │ │ | | | └─────────────┬─────────────┘ │ | | | ┌─────────────┴─────────────┐┌─────────────┴─────────────┐ | | | │ DataSourceExec ││ DataSourceExec │ | | | │ -------------------- ││ -------------------- │ | | | │ bytes: 1336 ││ bytes: 1336 │ | | | │ format: memory ││ format: memory │ | | | │ rows: 1 ││ rows: 1 │ | | | └───────────────────────────┘└───────────────────────────┘ | | | | +---------------+------------------------------------------------------------+ 1 row(s) fetched. Elapsed 0.003 seconds. ``` -- 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: github-unsubscr...@datafusion.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org For additional commands, e-mail: github-h...@datafusion.apache.org