Skip to content

Batch the hash join probe of several ON disjuncts and of an additional ON condition - #132896

Closed
groeneai wants to merge 4 commits into
ClickHouse:masterfrom
groeneai:hash-join-or-batched-probe
Closed

groeneai wants to merge 4 commits into
ClickHouse:masterfrom
groeneai:hash-join-or-batched-probe

Conversation

@groeneai

@groeneai groeneai commented Nov 19, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #120515
Related: #130344
Related: #132628

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

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

A hash join with several ON disjuncts, or with an additional ON condition, now looks up a batch of left rows in the table of each disjunct in turn, instead of one row at a time in every table.

Description

Draft, as asked in #120515 (comment).

A hash join with several ON disjuncts, or with an additional ON condition such as l.t < r.t, looked each left row up in the table of every disjunct in turn (HashJoinTable::probeAll). Now it looks a batch of left rows up in each table in turn, then adds their matches. A join that stops at its first match looks a later table up only for the rows still unmatched.

The key search (LookupMethod.h, FindKeys.h, FindKeysMaps{All,One,Set}.cpp), KnownRowsHolder.h, the batch driver (JoinRightColumns) and the match steps addMatchesOfDisjuncts and addMatchesWithAdditionalFilter are the code of commit c1ab44bb9559 (#130344). Its one-disjunct probe (search batches, MatchAdder, fused loops, WordsAndKeyOffsets, ASOF key search) is not taken, so a join with one disjunct and no additional condition keeps HashJoinTable::probe.

Performance, ratio PR / master 6483b80 at 16 and 96 threads (x86 release, Xeon 6975P-C, one socket, parallel_hash, 60M left rows, table). Slower: ALL with 2 to 4 OR out of cache 1.04 to 1.13, with Nullable keys 1.08 to 1.12, LEFT SEMI and ANTI with a rarely matching first disjunct in cache 1.03 to 1.11, rows whose disjuncts all match or none 1.18 to 1.30. Faster: LEFT ANY and SEMI with an often matching first disjunct 0.73 to 0.92, ALL with 3 or 4 OR in cache 0.76 to 0.92. #132628 is slower than its merge base in the same cases by about as much (table), so the performance bar of the request is not met yet.

Tests: 05823_hash_join_probe_disjunct_batches runs 40 joins on left blocks of 1000 to 20001 rows and row limits 7 and 1000, each giving master's result. The performance test hash_join_or_disjuncts_batched_probe.xml runs these cases out of cache.


Workflow [PR]
Sync PR [sync-upstream/pr/132896]

groeneai and others added 4 commits November 19, 2026 05:25
…l ON condition

A hash join with several `ON` disjuncts, or with an additional `ON` condition such as `l.x < r.y`,
looked one left row up in the table of each disjunct in turn (`HashJoinTable::probeAll`,
`detail::probeAllRows`). Now it takes a batch of left rows, looks all of them up in the first table,
then in the next one, and then adds the matches of the batch. A join that stops at its first match
looks a later table up only for the rows that no earlier disjunct matched. This removes
`HashJoinTable::probeAll`, `detail::probeAllRows` and `MultiMapLookupSampler`.

The key search (`LookupMethod.h`, `FindKeys.h`, `FindKeysMaps*.cpp`), the batch driver
(`JoinRightColumns.h/.cpp`) and the match steps `addMatchesOfDisjuncts` and
`addMatchesWithAdditionalFilter` (`JoinRightColumnsImpl.h`) are ported from commit c1ab44b of
ClickHouse#130344 by harikrishnan94, cut to these two shapes of join: none of the one-disjunct probe of that
commit is taken (`MatchAdder`, search batches, fused loops), so a join with one disjunct and no
additional condition keeps `HashJoinTable::probe`, and only loses the `flag_per_row` template
parameter of `processMatch` and `joinRightColumns`. `KnownRowsHolder.h` is that commit's version.

Related: ClickHouse#120515, ClickHouse#130344

Co-authored-by: harikrishnan94 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
`joined_block_split_single_row`, which the test runner randomizes, rejects `max_joined_block_size_rows = 0`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A probe column built as `intHash64(number + c)` is the previous column one row later, so the key
of one disjunct predicts the key of the next disjunct one row on, which a loop over the disjuncts
of a row can exploit. Hash `number * 32 + c` instead, so that every column has its own hash. Also
add rows whose disjuncts all match together or none does, with an ALL join over three disjuncts
and an ANTI join over two.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex; 2 findings; 1 full pass).

# Sev Finding Verdict Evidence / action
1 ❌ The batched path is slower in some cases (up to 1.30x), so it should be optimized before the PR is opened DISAGREE The request is a draft with a high-fidelity port of c1ab44bb9559 (comment): addMatchesOfDisjuncts is byte-identical to it, addMatchesWithAdditionalFilter one line apart, so optimizing would move it away from that code. The slower cases and the unmet bar are in the description; #132628 measures the same.
2 ⚠️ 05823 stays green if the batching is reverted; add a profile event or a driver test DISAGREE The change keeps results, so the test checks results against master at every batch and row-limit boundary, as c1ab44bb9559's own 05622 does. It turned red under three mutations of the new code (wrong filter index, wrong unmatched row, no known_rows.clear()), and a probe showed all its joins take the new path. A profile event added only for a test costs every query; the performance test measures what the batching is for.

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are terminal per finding.

Session id: cron:clickhouse-review-slot-9:20261119-064200

@groeneai
groeneai marked this pull request as draft November 19, 2026 07:11
@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Not a bug fix: a port the code owner asked for. The deterministic oracle is 05823_hash_join_probe_disjunct_batches (40 joins x 7 block/limit shapes): the same result as a master binary, on this branch's debug build and on master 1d0af83c.
b Root cause explained? What changes: the probe of several disjuncts or an additional condition goes from one row in every table to a batch per table. Slower cases (posted tables): one more unpredictable branch per row and disjunct in the match step, and lookups thrown away at max_joined_block_size_rows for ALL with 3-4 OR.
c Fix matches root cause? The code of these two paths is commit c1ab44bb9559's, as requested; no lever or redesign added. The slower cases are stated in the description.
d Test intent preserved / new tests added? New stateless test 05823 and performance test hash_join_or_disjuncts_batched_probe.xml (an independent hash per key column); no existing test changed.
e Both directions demonstrated? 05823 turns red under three mutations of the new code (wrong filter index, wrong unmatched row, no known_rows.clear()), and a routing probe showed that all 280 of its joins take the new path; green unmutated and on master.
f Fix is general across code paths? Every join kind that reaches the new path is in 05823 (INNER/LEFT/RIGHT/FULL x ALL/ANY/SEMI/ANTI, with and without an additional condition). A join with one disjunct and no additional condition, ASOF included, keeps HashJoinTable::probe.
g Fix generalizes across inputs (params/datatypes/wrappers)? UInt64, String, LowCardinality, two-column and Nullable keys; left blocks of 1000, 8191, 8192, 8193 and 20001 rows; row limits 7, 1000 and 65409; serial and parallel layouts; key-only tables on and off; 50/50 randomized runs pass.
h Backward compatible? (maintainer-approved exception only) No setting, default, format or result changes.
i Invariants and contracts preserved? The first batch of a call is at most max_joined_block_size_rows rows, so a call stops at the limit as before; used flags and the right rows claimed by ANY joins are set as before (one-thread RIGHT SEMI and INNER ANY cases in 05823); a missing key's offset is never read (read only for a matched row).

Session id: cron:clickhouse-impl-slot-5:20261119-052300

@clickhouse-gh

clickhouse-gh Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [b0bf917]

Summary: ❌

job_name test_name status info comment
Finish Workflow FAIL
python3 ./ci/jobs/scripts/workflow_hooks/can_be_merged.py FAIL

@clickhouse-gh clickhouse-gh Bot added pr-not-for-changelog This PR should not be mentioned in the changelog can be tested Allows running workflows for external contributors comp-joins JOINs end-to-end (planning hooks + runtime join operators/algorithms). Single bucket to avoid pla... labels Nov 19, 2026
@harikrishnan94 harikrishnan94 self-assigned this Nov 19, 2026
@harikrishnan94

Copy link
Copy Markdown
Contributor

Not needed. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-joins JOINs end-to-end (planning hooks + runtime join operators/algorithms). Single bucket to avoid pla... pr-not-for-changelog This PR should not be mentioned in the changelog

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants