Skip to content

perf: prefer existing co-partitioning for hash joins - #27980

Draft
2010YOUY01 wants to merge 1 commit into
2010YOUY01/join-selection-subrulesfrom
2010YOUY01/join-selection-copartitioning
Draft

2010YOUY01 wants to merge 1 commit into
2010YOUY01/join-selection-subrulesfrom
2010YOUY01/join-selection-copartitioning

Conversation

@2010YOUY01

@2010YOUY01 2010YOUY01 commented Dec 3, 2026 •

Copy link
Copy Markdown
Member

Which issue does this PR close?

This PR demonstrates how #25304 by @stuhood can be ported after the refactor in #27979, showing how the refactor makes similar changes easier to implement in the future.

Rationale for this change

A small build input currently makes an Auto hash join choose CollectLeft, even when both inputs are already compatibly partitioned on the join keys. This unnecessarily gathers the build partitions. Prefer Partitioned when the existing layout already satisfies the join's distribution requirements.

For example, join a small dimension table with a fact table when both expose matching range partitions on the join key:

SELECT * FROM dimension d JOIN fact f ON d.d_dkey = f.f_dkey;

Even below the broadcast threshold, the join can use the existing partitions:

Before:                            After:
HashJoinExec: CollectLeft           HashJoinExec: Partitioned
  CoalescePartitionsExec             dimension: Range(key, [B, C], 3)
    dimension: Range(key, [B, C], 3)  fact:      Range(key, [B, C], 3)
  fact: Range(key, [B, C], 3)

What changes are included in this PR?

Suggested reading order:

  1. datafusion/physical-plan/src/joins/hash_join/exec/optimizations.rs — HashJoinExec::choose_partition_mode checks existing co-partitioning before the broadcast threshold, using the existing distribution compatibility check.
  2. datafusion/core/tests/physical_optimizer/join_selection.rs — PartitionCase and the co-partitioned join tests show which layouts qualify and how this interacts with build-side swapping.
  3. datafusion/sqllogictest/test_files/repartition_subset_satisfaction.slt — updated plans show a range-partitioned join retaining its partitions and removing CoalescePartitionsExec.

What is the testing strategy for this PR?

UTs

Are there any user-facing changes?

No behavior change.

@github-actions github-actions Bot added 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
@codecov-commenter

codecov-commenter commented Dec 3, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 89.47368% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 83.72%. Comparing base (d8d10c6) to head (6388255).

Files with missing lines Patch % Lines
...cal-plan/src/joins/hash_join/exec/optimizations.rs 89.47% 1 Missing and 1 partial ⚠️
Flag Coverage Δ
all-tests 83.72% <89.47%> (+0.27%) ⬆️
sqllogictests 59.77% <89.47%> (-0.75%) ⬇️

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

@@                          Coverage Diff                           @@
##           2010YOUY01/join-selection-subrules   #27980      +/-   ##
======================================================================
+ Coverage                               83.44%   83.72%   +0.27%     
======================================================================
  Files                                    1061     1061              
  Lines                                  475300   507471   +32171     
  Branches                               475300   507471   +32171     
======================================================================
+ Hits                                   396629   424865   +28236     
- Misses                                  54132    58101    +3969     
+ Partials                                24539    24505      -34     
Files with missing lines Coverage Δ
...cal-plan/src/joins/hash_join/exec/optimizations.rs 95.12% <89.47%> (-1.71%) ⬇️

... and 121 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.

@2010YOUY01
2010YOUY01 force-pushed the 2010YOUY01/join-selection-copartitioning branch from df55318 to 6388255 Compare December 3, 2026 03:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core Core DataFusion crate 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