Skip to content

perf(hash-join): Redesign hash table with contiguous match storage - #27401

Closed
2010YOUY01 wants to merge 3 commits into
apache:mainfrom
2010YOUY01:redesign-physical-hash-table
Closed

2010YOUY01 wants to merge 3 commits into
apache:mainfrom
2010YOUY01:redesign-physical-hash-table

Conversation

@2010YOUY01

@2010YOUY01 2010YOUY01 commented Nov 13, 2026 •

Copy link
Copy Markdown
Member

Important

Fork CI setup: For pull requests from a fork to main, enable workflows in your fork's Actions tab before you push.
See the fork CI setup guide.

Which issue does this PR close?

Step 2 of

This PR builds on top of #27399, commit 3 is for this PR. Needs rebase after 1st PR is merged.

Rationale for this change

See #27398 for details

hj benchmark result:

┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━┳━━━━━━━━━━━┳━━━━━━━━━━━━━━━┓
┃ Query                                                                         ┃      main ┃    vec-ht ┃        Change ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━╇━━━━━━━━━━━╇━━━━━━━━━━━━━━━┩
│ QQuery 1_density=1_prob_hit=1_25*1.5M                                         │   1.07 ms │   1.12 ms │     no change │
│ QQuery 2_density=0.026_prob_hit=1_25*1.5M                                     │   3.32 ms │   4.47 ms │  1.35x slower │
│ QQuery 3_density=1_prob_hit=1_100K*60M                                        │  55.13 ms │  55.42 ms │     no change │
│ QQuery 4_density=1_prob_hit=0.1_100K*60M                                      │ 158.77 ms │ 146.74 ms │ +1.08x faster │
│ QQuery 5_density=0.75_prob_hit=1_100K*60M                                     │ 584.66 ms │ 171.51 ms │ +3.41x faster │
│ QQuery 6_density=0.75_prob_hit=0.1_100K*60M                                   │ 219.85 ms │ 206.06 ms │ +1.07x faster │
│ QQuery 7_density=0.5_prob_hit=1_100K*60M                                      │ 598.32 ms │ 164.35 ms │ +3.64x faster │
│ QQuery 8_density=0.5_prob_hit=0.1_100K*60M                                    │ 208.76 ms │ 197.83 ms │ +1.06x faster │
│ QQuery 9_density=0.2_prob_hit=1_100K*60M                                      │ 582.67 ms │ 168.23 ms │ +3.46x faster │
│ QQuery 10_density=0.2_prob_hit=0.1_100K*60M                                   │ 209.91 ms │ 202.32 ms │     no change │
│ QQuery 11_density=0.1_prob_hit=1_100K*60M                                     │ 584.03 ms │ 171.17 ms │ +3.41x faster │
│ QQuery 12_density=0.1_prob_hit=0.1_100K*60M                                   │ 214.30 ms │ 212.03 ms │     no change │
│ QQuery 13_density=0.01_prob_hit=1_100K*60M                                    │ 608.84 ms │ 185.13 ms │ +3.29x faster │
│ QQuery 14_density=0.01_prob_hit=0.1_100K*60M                                  │ 218.62 ms │ 219.30 ms │     no change │
│ QQuery 15_density=0.2_prob_hit=0.1_100K_(20%_dups)*60M                        │ 197.45 ms │ 166.08 ms │ +1.19x faster │
│ QQuery 16_density=1_prob_hit=1_25*1.5M_RightSemi                              │   1.38 ms │   1.42 ms │     no change │
│ QQuery 17_density=1_prob_hit=1_100K*60M_RightSemi                             │  57.59 ms │  57.41 ms │     no change │
│ QQuery 18_density=1_prob_hit=0.1_100K*60M_RightSemi                           │  71.99 ms │  70.57 ms │     no change │
│ QQuery 19_density=1_prob_hit=1_25*1.5M_RightAnti                              │   1.11 ms │   1.08 ms │     no change │
│ QQuery 20_density=1_prob_hit=1_100K*60M_RightAnti                             │  52.92 ms │  52.65 ms │     no change │
│ QQuery 21_density=1_prob_hit=0.1_100K*60M_RightAnti                           │  77.23 ms │  78.34 ms │     no change │
│ QQuery 22_density=1_prob_hit=0.01_100K_(fanout_100)*60M_RightSemi             │  78.84 ms │  80.95 ms │     no change │
│ QQuery 23_density=1_prob_hit=1_32K_(fanout~78)*2.3M_long_keys_count           │  89.26 ms │  26.34 ms │ +3.39x faster │
│ QQuery 24_density=1_prob_hit=1_3K_(single_hot_bucket)*20K_long_keys_count     │ 281.53 ms │  35.50 ms │ +7.93x faster │
│ QQuery 25_density=1_prob_hit=1_20K_(fanout~78_multi_key)*240K_multi_key_count │ 108.82 ms │  24.79 ms │ +4.39x faster │
│ QQuery 26_density=1_prob_hit=0.027_270K_dense_keys*15M                        │  41.95 ms │  42.67 ms │     no change │
│ QQuery 27_density=0.027_prob_hit=0.027_270K_sparse_keys*15M                   │  48.62 ms │  44.72 ms │ +1.09x faster │
└───────────────────────────────────────────────────────────────────────────────┴───────────┴───────────┴───────────────┘
┏━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━┓
┃ Benchmark Summary      ┃           ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━┩
│ Total Time (main)      │ 5356.92ms │
│ Total Time (vec-ht)    │ 2788.19ms │
│ Average Time (main)    │  198.40ms │
│ Average Time (vec-ht)  │  103.27ms │
│ Queries Faster         │        13 │
│ Queries Slower         │         1 │
│ Queries with No Change │        13 │
│ Queries with Failure   │         0 │
└────────────────────────┴───────────┘

What changes are included in this PR?

To read the diff, there are only 3 key points (all explained in #27398)

  • The idea of hash table physical redesign
  • Two key abstraction layers: BatchedJoinProcessor and JoinIndexMap

Then read in order:

  1. entry point at regular_join.rs, into_stream()
  2. Next see the key struct BatchedJoinProcessor
  3. This PR changes BatchedJoinProcessor inner probing implementation with JoinIndexMap
  4. JoinIndexMap implements the new contiguous match storage HT layout

What is the testing strategy for this PR?

Are there any user-facing changes?

@2010YOUY01
2010YOUY01 marked this pull request as draft November 13, 2026 08:23
@github-actions github-actions Bot added documentation Improvements or additions to documentation core Core DataFusion crate sqllogictest SQL Logic Tests (.slt) common Related to common crate physical-plan Changes to the physical-plan crate labels Nov 13, 2026
Port the hash-table change from PR apache#27401 onto the current regular-stream refactor. Preserve its limit/coalescing behavior and prepared-build ownership, and retain the legacy index for existence joins.
@2010YOUY01
2010YOUY01 force-pushed the redesign-physical-hash-table branch from ffea489 to e04da7b Compare November 16, 2026 12:06
2010YOUY01 added a commit to 2010YOUY01/arrow-datafusion that referenced this pull request Nov 20, 2026
…th (apache#27399)

## Which issue does this PR close?

Step 1 toward:
- apache#27398

Part of:
- apache#26225
- apache#26361

## Rationale for this change

This PR is step 1 of apache#27398, a structural cleanup of Hash Join to
prepare for apache#27401 (physical HT redesign, with 8 queries in the `hj`
benchmark achieving >3x speedups).

The goal is to make Hash Join easier to understand, review, and
optimize. This PR focuses on the execution structure without changing
the physical hash table representation.

### Why the cleanup?

The existing Hash Join implementation has two major architectural
issues:

1. **Regular and existence joins share the same execution path.** It's
better to split them to support logic/optimizations specific to each.

2. **Everything is mixed into one layer.** Low-level physical operations
are mixed with high-level execution control flow. This makes PRs hard to
review: even a change that only touches one layer can appear to be a
deep architectural change.

The ideal design has three layers (see
[apache#27398](apache#27398)):

```text
RegularHashJoinStream   (execution state machine)
          |
BatchedJoinProcessor   (lookup -> filter -> output)
          |
JoinIndexMap            (physical hash table / index)
```

This PR establishes the first two layers. The `JoinIndexMap` abstraction
and physical redesign are deferred to
[apache#27401](apache#27401).

For deeper rationale, see:

- [apache#26225: Refactor Operators for
Maintainability](apache#26225)
- [apache#26361: Split Hash Join into regular/existence
streams](apache#26361)

## What changes are included in this PR?

There are only **two key points** to understand this PR:

1. **Split out the regular Hash Join execution path**
(Inner/Left/Right/Full joins).
- This is an incremental migration: the existing implementation is
preserved as a fallback.
- Existence joins continue using the existing path. Once both paths have
been migrated, the old implementation can be removed.

2. **Establish `BatchedJoinProcessor` as the main execution layer.**
   - See the code comments for details.
- This separation makes execution control flow easier to reason about
independently of the low-level lookup implementation.

### Suggested review order

It's easier to read the diff in this order:

1. **Migration configuration:** `enable_migration_hash_join`. When
`true`, regular joins use the new path; otherwise, they use the existing
implementation. Existence joins always use the existing path. The option
defaults to `true`, since the new path covers the existing regular-join
functionality.

It's planned to keep it for just one release to guard against major
bugs, as we did in
[apache#22710](apache#22710).

2. **Execution state machine:**
`datafusion/physical-plan/src/joins/hash_join/regular_join.rs`. Start
with `RegularHashJoinStream::into_stream`, which is the entry point for
the entire execution.

3. **Main execution layer:**
`datafusion/physical-plan/src/joins/hash_join/batched_join_processor.rs`.
Start with the documentation on `BatchedJoinProcessor`, which explains
its responsibilities and input/output contract with a concrete example.

4. **...and follow along.**

## What is the testing strategy for this PR?

Existing tests are reused. Major join fuzz tests are exercised with both
`enable_migration_hash_join=true` and `enable_migration_hash_join=false`
to cover the new and legacy implementations during migration.

## Are there any user-facing changes?

This PR adds `datafusion.execution.enable_migration_hash_join` (default:
`true`). It enables the refactored implementation for inner, left,
right, and full hash joins. Setting it to `false` restores the legacy
implementation as a temporary workaround for bugs.

See issue for details: apache#26361

The option and legacy fallback will be removed once the migration is
complete; the current plan is to retain them for one release.

---------

Co-authored-by: Matt Butrovich <mbutrovich@users.noreply.github.com>
@2010YOUY01

Copy link
Copy Markdown
Member Author

replaced by #27746

@2010YOUY01 2010YOUY01 closed this Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

common Related to common crate core Core DataFusion crate documentation Improvements or additions to documentation 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.

1 participant