Repository navigation
Add only the required right keys when a join has several disjuncts - #115465
Conversation
…sjuncts With one disjunct the right keys are split out of the block the join adds to the result and put back only where the query needs them - a value restored from the left key column, which is the same thing under an equality. With several disjuncts that restoration is invalid, because a row matched through another clause has a right key of its own, so the whole right header became the block the join adds: `sample_block_with_columns_to_add`. Needing to *read* a key when it is asked for is not a reason to *add* it when it is not, and the plan drops those columns right after the join with a `ColumnPermuteTransform` anyway. Keep the keys the query asks for - `requiredRightKeys` already tracks them, the planner fills it in `setUsedColumns` - and add nothing else. Without the analyzer nothing fills it, so there every key is kept, as before. `right_table_keys` stays whole either way, so the stored right blocks and the mixed `ON` condition evaluated against them are untouched. For a `count()` over `... ON a = a OR b = b` the join used to emit `__table2.a` and `__table2.b` for every result row and the permutation threw them away; now it emits neither. That is not measurable on a 20M x 5M join - 892 vs 903 ms with four disjuncts - so it is worth having for what it removes rather than for what it saves. It also settles the `LEFT SEMI` half of the key-only join hash tables: `canUseSetMaps` asks whether the join has to produce any right value at all, which with several disjuncts could never be false. Related: #115301 Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_016QJvAjMfovbKtDsMW9mDbF
|
Workflow [PR], commit [aaa145a] Summary: ✅
AI ReviewSummaryThis PR teaches PR Metadata
Exact replacement: Findings💡 Nits [src/Interpreters/HashJoin/HashJoin.cpp:142] The implementation still bails out to the pre-PR behavior under Suggested fix: either implement the legacy-planner branch too, or narrow the contract to Tests
Final VerdictComment. One user-facing contract mismatch needs tightening: the optimization is analyzer-only today, so the PR text and changelog should say that explicitly unless the legacy planner path is going to be optimized as well. LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 14/14 (100.00%) · Uncovered code |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
Only the stripped binary is compared: the official master build keeps debug symbols while PR builds strip them, so the other binaries differ by construction. Compile time of recompiled translation units7 translation units recompiled, 22 s compile time in total, 7 of them have a recent master baseline. |
…nger adds The previous commit claimed the pruning was not measurable. That was measured on the wrong shape: a 20M x 5M join where every left row matches one right row is dominated by the probe, and the columns that go away are a small part of the work. The win scales with the number of result rows, and CI found it on the shapes that have many - `join_nullable_keys_small_table` #5 and #9 and `join_main_loop` #8, all of them `count()` over a join with disjuncts, went -30%, -34% and -16% on ARM. Reproduced locally against a build of the parent commit, min of five runs, `max_threads = 8`: 200M left rows, 1000 right, two `UInt64` keys, count only 424 -> 243 ms 20M left rows, `String` keys 102 -> 83 ms, 31 -> 1 MiB one of the two keys selected, so only the other one goes 432 -> 390 ms RIGHT, which also emits the non-joined right rows 134 -> 138 ms (unchanged) The test covers those four shapes. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_016QJvAjMfovbKtDsMW9mDbF
| /// those are, so without it every key is kept, as before. | ||
| static Block rightColumnsToAddWithSeveralDisjuncts(const TableJoin & table_join, const Block & right_columns) | ||
| { | ||
| if (!table_join.enableAnalyzer()) |
There was a problem hiding this comment.
This optimization still returns all right columns when enable_analyzer = 0, but the PR title/changelog/test wording currently reads as if every multi-disjunct join got the improvement. Since the old analyzer is still a supported compatibility path, please either narrow the contract to enable_analyzer = 1 or teach the legacy path to compute the required right keys too.
`LEFT ANTI` had five shapes in the test and `LEFT SEMI` one. Add the three that were missing: `parallel_hash`, the one where spilling keeps the right blocks so only the smaller cell is left to win, and one with several disjuncts. Measured with `enable_join_key_only_hash_tables` off and on, 20M left rows joined to 5M right rows, min of five runs: SEMI, parallel_hash 54 -> 45 ms, 307 -> 188 MiB SEMI, right blocks kept 148 -> 124 ms, 470 -> 316 MiB SEMI, several disjuncts 388 -> 408 ms, 577 -> 577 MiB (unchanged) The last one does not run on a set map yet: with several disjuncts every right key column is added to the result whether or not the query asks for it, so `canUseSetMaps` refuses. It qualifies as soon as #115465 stops adding the ones nobody asked for - with both changes in place the same query goes 298 -> 218 ms and 577 -> 289 MiB - and the query is here to measure that. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_016QJvAjMfovbKtDsMW9mDbF
…ld analyzer too `rightColumnsToAddWithSeveralDisjuncts` was gated on `enableAnalyzer()`, so on the legacy planner a join with several disjuncts still adds every right key column to its result. `canUseSetMaps` asks whether the join produces any right value at all, so with `enable_analyzer = 0` a query like `SELECT count() FROM l SEMI LEFT JOIN r ON l.k = r.k OR l.s = r.s` stayed on the mapped tables while the same query on the analyzer got the key-only ones. The gate is not needed: `requiredRightKeys` works on both paths, because `TreeRewriter::collectJoinedColumns` fills `columns_added_by_join` through `addJoinedColumn` exactly as the planner fills it through `setUsedColumns`. Checked with `enable_analyzer = 0`: the results of seven `OR` join shapes - `ALL`/`SEMI`/`ANTI`, `LEFT`/`RIGHT`/`FULL`, with and without a right key or payload column selected - are the same as with the analyzer; `04926_join_key_only_hash_tables` and `05023_join_several_disjuncts_right_keys` pass; and the `join` tests have the same six reference mismatches as the build without this change, all of them tests that read differently on the legacy planner (`00826_cross_to_inner_join`, `01115_join_with_dictionary`, `02220_array_join_format`, `02784_parallel_replicas_automatic_decision_join`, `04653_distributed_plan_auto_rewrite_in_to_join`, `04670_any_join_take_last_row_no_swap`). Related: #115465 Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_016QJvAjMfovbKtDsMW9mDbF
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
A
JOINwhoseONsection has severalOR-ed equalities no longer materializes the right-hand key columns that the query does not select.Version info
26.8.1.1795(included in26.8and later)