sunchao opened a new pull request, #27884: URL: https://github.com/apache/datafusion/pull/27884
## Which issue does this PR close? - Closes #25923. The memory accounting half of that issue was fixed in #25933; this is the filter evaluation half. Related: #25584 summarizes the inner key group for a single cross-side comparison once the group has at least seven inner rows. It does not reach smaller groups or compound filters, which is what this PR covers. The two changes are independent but touch the same file, so whichever lands second needs a rebase. ## Rationale for this change A semi, anti or mark sort-merge join with a join filter evaluates the filter once per inner row of each key group. Every call extracts a scalar, broadcasts it to the outer group's length, builds a `RecordBatch` and evaluates the expression. That fixed cost is about 400 ns per call here (including buffering the group), and it is most of the join when key groups hold only a few rows. That is the shape of the correlated `EXISTS` / `NOT EXISTS` subqueries in TPC-H q21, which join `lineitem` to itself on the order key (1 to 7 rows per key) with a `<>` filter on the supplier key. Comet runs those as filtered semi/anti sort-merge joins, which is where #25923 came from. ## What changes are included in this PR? For a key group that ends inside the current batch on both sides and forms at most 64 `(outer, inner)` row pairs, the join no longer buffers the inner group. It queues the row pairs as two `u32` index vectors and moves on. The filter then runs once over the queued pairs of many key groups: `take` each filter column by the pair indices, evaluate the expression once, and set the matched bit of the outer row of every passing pair. The queue is evaluated when a batch (`batch_size`) of pairs has built up, and always before the current outer or inner batch is replaced, since the pairs index those batches. These groups also stay on the synchronous fast path that unfiltered joins already use: no inner key buffer, no slicing, no reservation traffic and no async call per group. Everything else keeps the existing buffered path, with its spilling and its early exit once every outer row of the group has matched: groups that reach the end of a batch or continue into the next one, and groups forming more than 64 pairs. Why 64 pairs: the queued pairs of a group are all evaluated, while the buffered path can stop after the first inner row. On the benchmark below a queued pair costs about 4 ns and a buffered filter call about 400 ns, so 64 pairs keeps a group's queued work below one buffered call. `left_semi_square_groups_filtered/8` is the buffered path's best case (it settles each group after two inner rows) at exactly 64 pairs, and the queued path is still faster there. Things to know when reviewing: - The limit was chosen from measurements with `Int64` filter columns. For wide string columns both the per-pair cost and the per-call cost (the scalar broadcast) go up; I have not measured where the break-even moves. - The queued indices and the filter input built from them (at most `batch_size + 63` rows of the filter columns) are not tracked by the memory reservation. The existing path's transient filter input is not tracked either, but it is one outer group long rather than up to a batch. - A filtered group above the limit now locates both group ends before falling back to the buffered path, which then locates them again. `left_semi_square_groups_filtered/9` below measures that case. ## What is the testing strategy for this PR? New unit tests in `sort_merge_join/tests.rs`, each checked against a nested-loop evaluation of the same join over the input rows, for LeftSemi, LeftAnti, RightSemi, RightAnti, LeftMark and RightMark: - `bitwise_small_key_groups_skip_inner_key_buffer`: 1 to 5 rows per key with NULLs in the filter column, laid out so that no matching key group reaches a batch end. It runs with a 1-byte memory pool and spilling disabled, so it fails as soon as any inner key group is buffered. Batch sizes 1, 16 and 8192 put the evaluation after every group, mid-batch, and only at batch ends. - `bitwise_small_key_groups_across_batch_boundaries`: the same rows cut into input batches of several sizes, so groups that reach or cross a batch boundary (buffered path) are interleaved with groups that do not (queued path). - `bitwise_key_group_above_deferred_pair_limit_is_buffered`: 8x8 and 1x64 groups are queued; 5x13 and 65x1 groups are buffered. `bitwise_small_key_groups_charged_by_row_share` used 2-row and 3-row groups, which no longer reach the inner key buffer it is testing. It now uses 8-row and 12-row groups. `sort_merge_join.slt` gains a q21-shaped `EXISTS` / `NOT EXISTS` case with a `<>` filter and NULLs, at the default batch size and at batch size 2. I also broke the new code on purpose in five ways (queued path disabled, no evaluation before the inner batch is replaced, none before the outer batch is emitted, NULL results treated as true, limit off by one). The new tests failed for each. Benchmarks: `cargo bench -p datafusion-physical-plan --features test_utils --bench sort_merge_join`, main at a56631d71 against this branch, on an AMD Ryzen 9 9950X3D (Linux under WSL2). Each cell is the range of the medians of two runs, one with main first and one with this branch first. | Benchmark | main | this PR | main / this PR | | --- | --- | --- | --- | | `left_semi_small_groups_filtered/100000` (new) | 68.1 - 73.2 ms | 9.9 - 10.0 ms | 6.8x - 7.4x | | `left_anti_small_groups_filtered/100000` (new) | 71.1 - 76.6 ms | 6.6 - 6.8 ms | 10.5x - 11.5x | | `left_semi_square_groups_filtered/8` (new, 64 pairs, queued) | 8.4 - 9.1 ms | 2.3 - 2.4 ms | 3.6x - 3.8x | | `left_semi_square_groups_filtered/9` (new, 81 pairs, buffered) | 7.4 - 8.0 ms | 7.8 ms | 0.95x - 1.02x | | `left_semi_1to10/100000` (no filter) | 503 - 528 µs | 480 - 489 µs | 1.03x - 1.10x | | `left_anti_partial/100000` (no filter) | 742 - 815 µs | 747 µs | 0.99x - 1.09x | | `inner_1to10/100000` (other stream, untouched) | 8.3 - 8.5 ms | 8.7 - 9.0 ms | 0.95x | The first two cases are shaped like the issue's data: 100K keys, 1 to 7 rows per key, 60% of the rows on the outer side, filter `left.data <> right.data`. The inner join does not run any code this PR changes (it uses the materializing stream), so its 0.95x in both runs is the difference between the two builds on this machine; the 9x9 case and the unfiltered cases are within that band. Not run: TPC-H q21 end to end, and the `dfbench smj` queries. The filtered semi join queries I read there (Q11-Q13) use key groups of 10x100 rows, which are above the limit and stay on the buffered path. ## Are there any user-facing changes? No. There are no API or configuration changes; filtered semi, anti and mark sort-merge joins over small key groups get faster. --- 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]
