sunchao opened a new pull request, #27887: URL: https://github.com/apache/datafusion/pull/27887
## Which issue does this PR close? - Part of #20910. ## Rationale for this change The semi/anti/mark sort-merge join stream finds the end of every key group with `find_key_group_end`. It checks the next row and the last row of the batch, and otherwise binary searches the rest of the batch. With 8192-row batches that is about 13 comparisons per group on each side, however short the group is. Join key groups are usually short. For a group of a few rows, those comparisons are a large part of what the join does per group. ## What changes are included in this PR? After the two existing checks (single-row group, group running to the end of the batch), `find_key_group_end` gallops from the start of the group in doubling steps and binary searches only the step that overshot. The number of comparisons then grows with the size of the group rather than the size of the batch: 3 for a 2-row group and 9 for a 10-row group, instead of about 15. A long group that ends inside the batch needs more comparisons than before (about 21 instead of 15 for 1,000 rows), but that cost is spread over the rows of the group. `left_semi_1to1000` is added to the benchmark to cover that case. ## What is the testing strategy for this PR? `bitwise_find_key_group_end_matches_linear_scan` compares the function with a linear scan for runs of 1 to 130 rows, every start row and every batch length. The existing sort-merge join tests do not pin this down: three deliberate off-by-one errors in the new search (the gallop skipping a row, either bound one too low) each pass all 497 of them and fail only the new test. The join fuzz tests and the join `.slt` files pass as before. Benchmarks: `cargo bench -p datafusion-physical-plan --features test_utils --bench sort_merge_join`, main at 34eb0b910 against this branch, medians of two runs (main first, then this branch first), on an AMD Ryzen 9 9950X3D (Linux under WSL2): | Benchmark | main | this PR | main / this PR | | --- | --- | --- | --- | | `left_semi_1to10/100000` (10-row groups) | 449.5, 451.1 µs | 329.8, 325.6 µs | 1.36x, 1.39x | | `left_semi_1to1000/100000` (new, 1,000-row groups) | 69.3, 70.3 µs | 69.9, 68.8 µs | 0.99x, 1.02x | | `left_anti_partial/100000` (single-row groups) | 692.1, 693.2 µs | 690.5, 688.7 µs | 1.00x, 1.01x | | `inner_1to10/100000` (other stream, untouched) | 8.02, 8.02 ms | 8.08, 8.06 ms | 0.99x, 0.99x | Filtered joins over small key groups gain about as much once #27884 is in. On top of that branch, its `left_semi_small_groups_filtered` case (1 to 7 rows per key) goes from 9.3 ms to 7.0 ms and `left_anti_small_groups_filtered` from 6.3 ms to 4.5 ms. Those two benchmarks are not on main yet, so they are not part of this PR. ## Are there any user-facing changes? No. --- Written with AI assistance (Claude Code). -- 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]
