comphead opened a new pull request, #27928: URL: https://github.com/apache/datafusion/pull/27928
## Which issue does this PR close? - Closes https://github.com/apache/datafusion/issues/27910. Stacked on https://github.com/apache/datafusion/pull/27278: only the last three commits are new. ## Rationale for this change https://github.com/apache/datafusion/pull/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 the head of https://github.com/apache/datafusion/pull/27278 plus the bench commit, 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. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
