2010YOUY01 opened a new pull request, #27979: URL: https://github.com/apache/datafusion/pull/27979
## Which issue does this PR close? <!-- Add the related issue, if any. --> Related work: #27771 addresses preserving already-satisfied distribution requirements. This PR focuses on separating join swapping from partition-mode selection. ## Rationale for this change Hash join selection currently makes two decisions in a single optimizer tree traversal: which input should build the hash table, and which partitioning mode to use. These decisions are spread across tightly coupled utility functions in the operator and optimizer. (I think the existing implementation reflects patches accumulated over time rather than a deliberate design. The two-pass approach is already implicit in the logic, but making it explicit produces a simpler implementation.) This PR simplifies the implementation by giving each layer a clear responsibility: - The optimizer runs separate subrules for join swapping and partition-mode selection. - `HashJoinExec::choose_partition_mode` encapsulates the partition-mode decision in a single, self-contained function on the operator. For example: ```sql SELECT * FROM large_table JOIN small_table USING (id); ``` The two passes handle this in order: 1. Choose the input order, placing the smaller table on the left as the build side. 2. Choose a partitioning mode for each join, such as a broadcast join when the build side is small. Separating the two also also provides a clearer extension point for future cost-based join optimization (CBO): CBO could focus on join ordering, while partition-mode selection remains a separate, heuristic-based subrule. This would make join CBO easier to implement and extend. Related: [#18249](https://github.com/apache/datafusion/issues/18249). ## What changes are included in this PR? Suggested reading order: 1. `datafusion/physical-optimizer/src/join_selection.rs` — `JoinSelection` runs separate bottom-up passes for input swapping and partition-mode selection, after handling unbounded inputs. 2. `datafusion/physical-plan/src/joins/hash_join/exec/optimizations.rs` — atomic `HashJoinExec` mutator for swapping input, and choosing partition mode. ## What is the testing strategy for this PR? Existing tests. Notes on changes on `slt`s in this PR, the diffs are all improving the plan: ``` left: unkwown stat right: 3 rows (known-small) ``` This case it's better to swap the side, to let knwon-small right side broadcast, but some plan shape missed this optimization in the existing implementation. For 3 updated files: ````markdown `explain_tree.slt` ``` left: CSV, unknown statistics right: Parquet, 3 rows ``` Swap to build on the known-small right input. The expected plan changes from `LeftAnti` to `RightAnti`. `joins.slt` ``` left: CSV, unknown statistics right: LIMIT 2, estimated 2 rows ``` The new rule would swap. Disable reordering here to preserve coverage of physical right-join limit pushdown. `cte.slt` ``` left: recursive WorkTable, unknown statistics right: Parquet, 100 rows ``` The new rule would swap. Disable reordering here to keep Parquet on the probe side, where the test verifies dynamic-filter pruning. ```` ## Are there any user-facing changes? SQL results are unchanged, but some physical plans can change when statistics are incomplete: build-side selection can favor a known-small input when the input sizes cannot be compared. The two new optimizer hooks and the changed `swap_inputs` signature are hidden APIs for internal use. -- 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]
