Skip to content

Add only the required right keys when a join has several disjuncts - #115465

Merged
nickitat merged 2 commits into
masterfrom
multi-disjunct-required-right-keys
Aug 20, 2026
Merged

nickitat merged 2 commits into
masterfrom
multi-disjunct-required-right-keys

Conversation

@nickitat

@nickitat nickitat commented Aug 19, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Performance Improvement

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

A JOIN whose ON section has several OR-ed equalities no longer materializes the right-hand key columns that the query does not select.


Screenshot 2026-08-20 at 12 50 00 Screenshot 2026-08-20 at 12 51 28

Version info

  • Merged into: 26.8.1.1795 (included in 26.8 and later)

…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
@clickhouse-gh

clickhouse-gh Bot commented Aug 19, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [aaa145a]

Summary: ✅


AI Review

Summary

This PR teaches HashJoin to stop carrying unused right-hand key columns for multi-disjunct JOIN ... ON ... OR ... cases and adds focused semantic and performance coverage. The implementation itself looks consistent, but the advertised contract is broader than the code: the optimization is currently analyzer-only, while the PR text reads as if it applied to every supported planner path.

PR Metadata

Changelog category: Performance Improvement is correct.

Changelog entry: narrow it to the analyzer-enabled path, because the code explicitly keeps the old behavior when enable_analyzer = 0.

Exact replacement:
With enable_analyzer = 1, a JOIN whose ON section has several OR-ed equalities no longer materializes the right-hand key columns that the query does not select.

Findings

💡 Nits

[src/Interpreters/HashJoin/HashJoin.cpp:142] The implementation still bails out to the pre-PR behavior under enable_analyzer = 0, but the PR title/changelog/test naming describe the optimization as unconditional. That leaves a supported compatibility path without the promised improvement.

Suggested fix: either implement the legacy-planner branch too, or narrow the contract to enable_analyzer = 1.

Tests
  • ⚠️ The fetched PR reports only include Build profile diff; Performance Comparison jobs were skipped, so there is no machine-readable before/after evidence for the Performance Improvement claim. A labeled perf run or textual benchmark numbers in the PR body would close that gap.
Final Verdict

Comment. 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

Metric Baseline Current Δ
Lines 86.90% 86.90% +0.00%
Functions 91.90% 91.90% +0.00%
Branches 79.20% 79.30% +0.10%

Changed lines: Changed C/C++ lines covered: 14/14 (100.00%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label Aug 19, 2026
@nickitat
nickitat marked this pull request as draft August 19, 2026 15:11
@nickitat nickitat changed the title Add only the right keys the query asks for when a join has several disjuncts Add only the required right keys when a join has several disjuncts Aug 19, 2026
@nickitat
nickitat requested a review from vdimir August 19, 2026 17:16
@nickitat
nickitat marked this pull request as ready for review August 19, 2026 17:16
@clickhouse-gh

clickhouse-gh Bot commented Aug 19, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing aaa145a65 with master e0669f5aa (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes
Binary Master PR Δ
programs/clickhouse-stripped 704.37 MiB 701.32 MiB -3.05 MiB (-0.43%)

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 units

7 translation units recompiled, 22 s compile time in total, 7 of them have a recent master baseline.

Job report

@vdimir vdimir self-assigned this Aug 20, 2026
…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
@nickitat nickitat added pr-performance Pull request with some performance improvements and removed pr-not-for-changelog This PR should not be mentioned in the changelog labels Aug 20, 2026
@nickitat
nickitat enabled auto-merge August 20, 2026 10:47
/// 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())

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

nickitat added a commit that referenced this pull request Aug 20, 2026
`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
@nickitat
nickitat added this pull request to the merge queue Aug 20, 2026
Merged via the queue into master with commit 7c48f2e Aug 20, 2026
178 checks passed
@nickitat
nickitat deleted the multi-disjunct-required-right-keys branch August 20, 2026 21:10
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Aug 20, 2026
nickitat added a commit that referenced this pull request Aug 21, 2026
…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-performance Pull request with some performance improvements pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants