perf(hash-join): eliminate intermediate array allocations in probe-side collision filter#23209
Draft
LiaCastaneda wants to merge 1 commit into
Draft
perf(hash-join): eliminate intermediate array allocations in probe-side collision filter#23209LiaCastaneda wants to merge 1 commit into
LiaCastaneda wants to merge 1 commit into
Conversation
…be path Replace the take() + eq_dyn_null() + FilterBuilder collision-filter in lookup_join_hashmap with a JoinKeyComparator-based in-place loop. The old equal_rows_arr allocated O(matched_pairs) intermediate arrays per probe batch (one take-gathered key array per key column per side, plus a BooleanArray and FilterBuilder result). At high fanout these dominate: fanout=78 over 136M output rows means ~2 × N_key_cols × 136M array elements allocated and discarded every probe batch. JoinKeyComparator builds make_comparator closures once per (build_batch, probe_batch) column pair, then walks the candidate index pairs with a plain Rust loop — no intermediate arrays, no Arrow kernel dispatch per row. Null semantics (NullEqualsNull / NullEqualsNothing) are baked into the closures at construction time, preserving correctness. Add JoinKeyComparator::for_equality as a convenience constructor for callers that only need equality (no sort options required). Closes apache#12131 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Which issue does this PR close?
Rationale for this change
When DataFusion executes a
HashJoin, for each probe batch it collects all candidate(build_idx, probe_idx)pairs from the hash table, then runs a key equality check to filter out hash collisions. This check existed because two different key values can hash to the same bucket (although it's unusual) -- so a hash match alone is not sufficient to confirm a true join match.The current implementation (equal_rows_arr) does this confirmation by materializing intermediate arrays sized to the total number of matched pairs
take(build_key_col, matched_indices)-- gather all matched build-side key values into a new arraytake(probe_key_col, matched_indices)-- same for the probe sideeq_dyn_null(...)-- compare element-wise into a boolean arrayFilterBuilder-- build a selection bitmask and filter both index arraysThis is O(matched_pairs) in both allocations and comparisons. For a typical star-schema join with fanout ≈ 1, matched_pairs ~ probe_rows and the cost is acceptable. But for joins with high build-side fanout (many build rows per key value), matched_pairs = probe_rows × fanout, and the intermediate arrays grow proportionally. At fanout 78 over 136M output rows, this means allocating and discarding ~4 arrays of 136M elements per probe batch -- the majority of which pass the filter unchanged since true hash collisions are rare.
This cost is most visible when:
What changes are included in this PR?
Are these changes tested?
Are there any user-facing changes?