Skip to content

refactor: split join selection into two subrules - #27979

Open
2010YOUY01 wants to merge 2 commits into
mainfrom
2010YOUY01/join-selection-subrules
Open

2010YOUY01 wants to merge 2 commits into
mainfrom
2010YOUY01/join-selection-subrules

Conversation

@2010YOUY01

@2010YOUY01 2010YOUY01 commented Dec 3, 2026 •

Copy link
Copy Markdown
Member

Which issue does this PR close?

cleanup for #27980

Rationale for this change

Hash join selection currently makes two decisions in a single optimizer tree traversal:

  • which input should build the hash table
  • 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:

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.

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

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

No behavior change, some query get optimized differently

@github-actions github-actions Bot added optimizer Optimizer rules core Core DataFusion crate sqllogictest SQL Logic Tests (.slt) physical-plan Changes to the physical-plan crate labels Dec 3, 2026
@2010YOUY01
2010YOUY01 added this pull request to stack #27981 December 3, 2026 02:26
@@ -1925,6 +1925,10 @@ DROP TABLE dynamic_filter_recursive;
# recursive iterations (#26641). Each iteration looks up a different key; the
# EXPLAIN ANALYZE below checks that the filter is reset and republished, so the
# probe scan prunes rows on every iteration.
# Keep the work table on the build side so the parquet scan receives the filter.

@2010YOUY01 2010YOUY01 Dec 3, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For all slt changes, see PR description test section.

@2010YOUY01
2010YOUY01 marked this pull request as ready for review December 3, 2026 02:34
@codecov-commenter

codecov-commenter commented Dec 3, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 92.79279% with 8 lines in your changes missing coverage. Please review.
✅ Project coverage is 83.71%. Comparing base (8b376d8) to head (d8d10c6).
⚠️ Report is 66 commits behind head on main.

Files with missing lines Patch % Lines
...atafusion/physical-optimizer/src/join_selection.rs 86.66% 1 Missing and 3 partials ⚠️
...cal-plan/src/joins/hash_join/exec/optimizations.rs 96.82% 0 Missing and 2 partials ⚠️
...tafusion/physical-plan/src/joins/hash_join/exec.rs 93.75% 0 Missing and 1 partial ⚠️
...usion/physical-plan/src/operator_statistics/mod.rs 0.00% 0 Missing and 1 partial ⚠️
Flag Coverage Δ
all-tests 83.71% <92.79%> (+0.27%) ⬆️
sqllogictests 59.77% <87.96%> (-0.76%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

@@            Coverage Diff             @@
##             main   #27979      +/-   ##
==========================================
+ Coverage   83.44%   83.71%   +0.26%     
==========================================
  Files        1060     1061       +1     
  Lines      475307   507452   +32145     
  Branches   475307   507452   +32145     
==========================================
+ Hits       396642   424837   +28195     
- Misses      54124    58110    +3986     
+ Partials    24541    24505      -36     
Files with missing lines Coverage Δ
...fusion/physical-plan/src/joins/hash_join/not_in.rs 94.74% <100.00%> (ø)
...tafusion/physical-plan/src/joins/hash_join/exec.rs 90.50% <93.75%> (+<0.01%) ⬆️
...usion/physical-plan/src/operator_statistics/mod.rs 79.72% <0.00%> (ø)
...cal-plan/src/joins/hash_join/exec/optimizations.rs 96.82% <96.82%> (ø)
...atafusion/physical-optimizer/src/join_selection.rs 93.57% <86.66%> (+2.57%) ⬆️

... and 119 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core Core DataFusion crate optimizer Optimizer rules physical-plan Changes to the physical-plan crate sqllogictest SQL Logic Tests (.slt)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants