Skip to content

perf: keep sort-merge join pairs as runs and filter outer join pairs first - #27928

Draft
comphead wants to merge 3 commits into
apache:mainfrom
comphead:smj-27910
Draft

comphead wants to merge 3 commits into
apache:mainfrom
comphead:smj-27910

Conversation

@comphead

@comphead comphead commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Rationale for this change

#27278 filters Inner sort-merge join pairs before materializing them. Its profiles showed where the join still spends its time:

  • On TPC-DS q72's shape (inner_1to1000_selective_filtered), gathering the filter's columns through per-pair indices took about 30% of the CPU, and recording those indices 11%.
  • With 64-row buffered batches, allocating and freeing a chunk of indices for every buffered batch took 14%.
  • A Left join whose filter keeps 5% of the pairs spent about 90% of its time materializing every pair and running the deferred filter over them, and 1% evaluating the filter.
  • Buffered batches kept, reserved and spilled columns that the join neither emits nor reads, such as a join key that the projection drops.

What changes are included in this PR?

  • Pairs stay runs until the freeze (items 1 and 2). append_output_pairs records a streamed row and its run of buffered rows as one PairRun, in a vector that the freezes reuse. Before, it stored a u64 pair per match, in chunks allocated for each buffered batch. At the freeze, a side whose rows form a range is still sliced. Long runs are copied: the buffered rows as ranges (MutableArrayData), and the streamed row repeated for primitive columns. Runs shorter than 12 pairs on average are still gathered row by row, as are dictionary columns from several buffered batches, which interleave merges.
  • Outer joins filter first (item 4). A Left, Right or Full join with a filter evaluates it on the filter's columns, then materializes the pairs that pass plus the first pair of each streamed row whose pairs all fail. The deferred filter null-joins that row as before.
  • Buffered batches keep only the columns they need (item 3): the emitted ones and the filter's, once the join keys are computed. Their reservation and spill files shrink to match.
  • New sort_merge_join benchmark cases, inner_10to1 and left_1to1000_selective_filtered, are in a separate first commit.

What is the testing strategy for this PR?

The existing sort-merge join tests cover the new paths. They compare with HashJoinExec and with projected joins, and they cover spilling. A check with 16 mutants of the new code found two gaps, which the first two of these tests close:

  • join_runs_continue_only_within_a_buffered_batch: a key group starts at row 32 of one buffered batch and ends at row 32 of the next, so runs from the two batches line up.
  • join_with_projection_matches_projected_join now also runs with spilling. Its projections drop the buffered key.
  • join_dictionary_column_of_several_buffered_batches: copying ranges would concatenate the batches' dictionaries past Int8 keys, which fails without the interleave fallback.

The join fuzz tests and the sort-merge join sqllogictest files pass.

Benchmarks: cargo bench -p datafusion-physical-plan --features test_utils --bench sort_merge_join, this PR against its base plus the bench commit (the head of #27278, whose sort-merge join code main now has), with thin LTO on an Apple M5 Max. The two builds and a second copy of the base ran alternately for four rounds. The table shows each case's best round.

Benchmark Base This PR Change
inner_1to1000_selective_filtered/20000 3.09 ms 1.91 ms -38%
left_1to1000_selective_filtered/20000 (new) 27.58 ms 3.38 ms -88%
left_1to100_filtered/20000 38.45 ms 22.84 ms -41%
inner_1to100/20000 12.67 ms 9.31 ms -26%
inner_1to100_filtered/20000 9.47 ms 7.44 ms -21%
inner_group_spans_buffered_batches/8 9.83 ms 4.22 ms -57%
inner_group_spans_buffered_batches/64 2.93 ms 2.11 ms -28%
inner_1to1/100000 1.41 ms 1.34 ms -5%
inner_10to1/100000 (new) 1.12 ms 1.14 ms +2%
inner_1to10/100000 7.08 ms 7.32 ms +3%
inner_1to10_high_pass_filtered_narrow/100000 3.36 ms 3.54 ms +5%

The other cases changed by 2% or less, and the second copy of the base stayed within 2% of the first. Runs of about 10 pairs are a few percent slower, because the freeze now expands them into indices that pairing used to write.

For item 3, in a scratch Inner join whose projection drops the buffered key and a string column, with 50,000 buffered rows per key, the peak reservation fell from 2.99 MB to 1.38 MB. With a 1 MB pool, the spilled bytes fell from 4.71 MB to 0.49 MB.

Are there any user-facing changes?

No.

… join

`inner_10to1` joins 10 streamed rows to each buffered row, as a fact table
joins a dimension on its key. `left_1to1000_selective_filtered` runs the
inputs and filter of `inner_1to1000_selective_filtered` as a Left join.
…first

A streamed row and the run of buffered rows it matches are now recorded as
one `PairRun`, in a vector that the freezes reuse, instead of a pair of `u64`
indices per match in chunks allocated for each buffered batch. At the freeze
a side whose rows form a range is sliced as before, long runs of buffered
rows are copied as ranges, and the streamed row of a long run is repeated for
primitive columns. Short runs are still gathered row by row.

Left, Right and Full joins with a filter now evaluate it before
materializing pairs, like Inner joins. They keep the pairs that pass and the
first pair of each streamed row whose pairs all fail, which the deferred
filter null-joins as before.
Buffered batches kept every input column while they waited for their key
group, and spilled them all. Once its join keys are computed, a buffered
batch now keeps only the columns that the join emits or its filter reads, so
its reservation and spill files no longer include, for example, a join key
that the projection drops.
@github-actions github-actions Bot added the physical-plan Changes to the physical-plan crate label Dec 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

physical-plan Changes to the physical-plan crate

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Sort-merge join: follow-ups to filtering pairs before materializing them

1 participant