Skip to content

Refactor hash join to give each ON disjunct its own HashJoinTable - #120515

Merged
harikrishnan94 merged 6 commits into
masterfrom
pbnp-table-building-blocks
Nov 18, 2026
Merged

harikrishnan94 merged 6 commits into
masterfrom
pbnp-table-building-blocks

Conversation

@harikrishnan94

@harikrishnan94 harikrishnan94 commented Sep 17, 2026 •

Copy link
Copy Markdown
Contributor

Related: #115254

A hash join keeps one map per ON disjunct. On master the maps live in RightTableData::maps, next to one set of slot locks and slot arenas that all the maps share. Every user of a map takes it out of the join itself, after a dispatch on the maps variant and the key type. Among them are the insert, the probe, the non-joined scan, the StorageJoin read, the rerange and the fixed-map conversion.

The partitioned hash join of the related PR needs one place that decides how a table is built and probed. That lets it change how tables are built and probed without touching every user of the maps.

this PR
  RightTableData
    tables    one HashJoinTable per disjunct: maps, key type and range, key sizes, key count, slot locks,
              one arena per build stream
  HashJoinTable
    insert                       the build loop
    probe                        the probe loop for one disjunct
    probeAll                     the probe loop for several disjuncts, or with an ON condition like t1.a < t2.b
    scanNotJoined                the non-joined rows
    forEachCell, forEachMapped   the StorageJoin row count and the rerange
    readCells                    the StorageJoin read, which decodes the keys
    tryConvertToFixedHashMap     the fixed-map conversion
    freezeForProbing             the step between the last insert and the probe
    makeSharedRuntimeFilter      the runtime filter
    parallelClearWork,           the clear of the ASOF maps of a large right side on a thread pool
    clearInParallel

For a user:

  1. Query results do not change, except close to a byte limit. max_bytes_in_join, the SpillingHashJoin threshold and the GraceHashJoin repartition compare the join's byte count with a limit. That count can now differ from master's, because each build stream has one arena instead of one per slot. A join with several disjuncts still returns, for LEFT and INNER ANY and for LEFT SEMI, the row of the first disjunct in ON order that matches. RIGHT ANY and RIGHT SEMI still look up every disjunct.
  2. There is no new setting, profile event or log message.

Reading order: HashJoinTable.h (the class comment and its public members) -> HashJoin::addBlockToJoin and HashJoinTable::insert -> HashJoinMethods::joinRightColumns and HashJoinTable::probe / probeAll -> NotJoinedHash and HashJoinTable::scanNotJoined -> StorageJoin -> ~RightTableData.

The tables:

  1. All the tables of a join share one key type, chosen as on master. HashJoin reads it from its first table. probe and probeAll still take it as a template argument, and the other methods switch on the stored type. The residual-condition path had its own loop on master. It now runs probeAllRows with lazy_row_filter, which checks the NULL key and the mask of each row when the loop visits it, instead of building skip bytes for the whole selector. The hashed and packed String keys still build them.
  2. Each table has its own slot locks, so two threads can fill the same slot number of two different tables at once. A slot lock no longer guards an arena.
  3. An Arena is not thread-safe, but the calls of one build stream never overlap, so an arena needs no lock. A key and the rows of its list can be in different arenas, because neither needs contiguity.
  4. The insert splits the rows of a block by slot and inserts the rows of each slot under that slot's lock. It adds what the slot buffers and the arena grow by to the join's byte count, even when it throws.
  5. As on master, the setup of the probe and its row loop are separate functions: probeAll builds the key getters and the map pointers, and probeAllRows holds the row loop. probe takes one callback for each row, and probeAll one for a match and one for the end of a row, so each join kind keeps its own output handling.

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

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

Version info

  • Merged into: 26.11.1.2689-master (included in 26.11 and later)

@harikrishnan94 harikrishnan94 added pr-not-for-changelog This PR should not be mentioned in the changelog pr-performance Pull request with some performance improvements labels Sep 17, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 17, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [9a336ce]

Summary: ✅


AI Review

Summary

Refactors HashJoin so each ON disjunct owns a HashJoinTable, centralizing build, probe, scan, conversion, and cleanup operations while moving arenas to build streams. No actionable findings at commit 9a336cecaa53b0fdd77ef9a19652f8a4d8ec62b3; prior findings no longer apply to the current implementation. All four added regression tests passed in the Fast test report. No builds or tests were run locally.

Final Verdict

Status: ✅ Approve

@harikrishnan94 harikrishnan94 removed the pr-performance Pull request with some performance improvements label Sep 17, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-joins JOINs end-to-end (planning hooks + runtime join operators/algorithms). Single bucket to avoid pla... label Sep 17, 2026
Comment thread src/Interpreters/IJoin.h Resolved
@clickhouse-gh

clickhouse-gh Bot commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

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

⚠️ Significant changes: object file sizes, compile time of recompiled translation units.

Binary sizes
Binary Master PR Δ
programs/clickhouse-stripped 831.35 MiB 830.94 MiB -419.23 KiB (-0.05%)

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.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Object file sizes ⚠️

44 object files changed (+5.43 MiB total), 2 added.

Object file Master PR Δ
src/CMakeFiles/dbms.dir/Interpreters/HashJoin/HashJoin.cpp.o 4.45 MiB 1.07 MiB -3.38 MiB (-76.00%)
src/CMakeFiles/dbms.dir/Interpreters/HashJoin/HashJoinTable.cpp.o new 1.95 MiB +1.95 MiB
src/CMakeFiles/dbms.dir/Interpreters/HashJoin/HashJoinTableScan.cpp.o new 947.38 KiB +947.38 KiB
src/CMakeFiles/dbms.dir/Storages/StorageJoin.cpp.o 2.77 MiB 1.94 MiB -850.32 KiB (-29.99%)
src/CMakeFiles/dbms.dir/Interpreters/HashJoin/FullHashJoinAny.cpp.o 2.37 MiB 2.64 MiB +271.38 KiB (+11.17%)
src/CMakeFiles/dbms.dir/Interpreters/HashJoin/FullHashJoinAll.cpp.o 2.67 MiB 2.94 MiB +270.61 KiB (+9.88%)
src/CMakeFiles/dbms.dir/Interpreters/HashJoin/InnerHashJoinAll.cpp.o 2.58 MiB 2.85 MiB +270.21 KiB (+10.22%)
src/CMakeFiles/dbms.dir/Interpreters/HashJoin/FullHashJoinRightAny.cpp.o 2.48 MiB 2.74 MiB +268.75 KiB (+10.59%)
src/CMakeFiles/dbms.dir/Interpreters/HashJoin/RightHashJoinAll.cpp.o 2.65 MiB 2.91 MiB +268.09 KiB (+9.90%)
src/CMakeFiles/dbms.dir/Interpreters/HashJoin/RightHashJoinAny.cpp.o 2.60 MiB 2.87 MiB +267.81 KiB (+10.04%)
src/CMakeFiles/dbms.dir/Interpreters/HashJoin/RightHashJoinSemi.cpp.o 2.60 MiB 2.87 MiB +267.79 KiB (+10.04%)
src/CMakeFiles/dbms.dir/Interpreters/HashJoin/LeftHashJoinAnyMapsAll.cpp.o 2.42 MiB 2.67 MiB +259.70 KiB (+10.48%)
src/CMakeFiles/dbms.dir/Interpreters/HashJoin/LeftHashJoinRightAnyMapsAll.cpp.o 2.42 MiB 2.67 MiB +258.88 KiB (+10.44%)
src/CMakeFiles/dbms.dir/Interpreters/HashJoin/LeftHashJoinAll.cpp.o 2.61 MiB 2.87 MiB +258.84 KiB (+9.67%)
src/CMakeFiles/dbms.dir/Interpreters/HashJoin/InnerHashJoinAnyMapsAll.cpp.o 2.24 MiB 2.48 MiB +242.46 KiB (+10.57%)
src/CMakeFiles/dbms.dir/Interpreters/HashJoin/RightHashJoinAnti.cpp.o 2.17 MiB 2.40 MiB +239.04 KiB (+10.77%)
src/CMakeFiles/dbms.dir/Interpreters/HashJoin/RightHashJoinRightAny.cpp.o 2.26 MiB 2.48 MiB +234.46 KiB (+10.15%)
src/CMakeFiles/dbms.dir/Interpreters/HashJoin/InnerHashJoinRightAnyMapsAll.cpp.o 2.20 MiB 2.43 MiB +232.05 KiB (+10.30%)
src/CMakeFiles/dbms.dir/Interpreters/HashJoin/LeftHashJoinSemiMapsAll.cpp.o 2.20 MiB 2.43 MiB +231.96 KiB (+10.29%)
src/CMakeFiles/dbms.dir/Interpreters/HashJoin/LeftHashJoinAntiMapsAll.cpp.o 2.18 MiB 2.41 MiB +230.21 KiB (+10.30%)

711 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only clickhouse-bundle) and not compared.

Slowest function optimization changes (ThinLTO)

Median per-function time ratio to the master baseline: programs/clickhouse ×0.81, programs/clickhouse-keeper ×1.05 (each binary is linked separately, on a different machine and with different build flags); deltas below are relative to it.

Binary Function Master PR Δ vs median
programs/clickhouse DB::registerFunctionConversion(DB::FunctionFactory&) 7.0 s 15.3 s +9.6 s (+168%)
programs/clickhouse-keeper unsigned long DB::HashJoinMethods<(DB::JoinKind)2, (DB::JoinStrictness)4, DB::HashJoin::MapsTemplat… 6.5 s gone -6.8 s (-100%)
programs/clickhouse-keeper unsigned long DB::HashJoinMethods<(DB::JoinKind)0, (DB::JoinStrictness)4, DB::HashJoin::MapsTemplat… 6.4 s gone -6.8 s (-100%)
programs/clickhouse-keeper unsigned long DB::HashJoinMethods<(DB::JoinKind)1, (DB::JoinStrictness)6, DB::HashJoin::MapsTemplat… 6.3 s gone -6.6 s (-100%)
programs/clickhouse-keeper unsigned long DB::HashJoinMethods<(DB::JoinKind)0, (DB::JoinStrictness)5, DB::HashJoin::MapsTemplat… 6.2 s gone -6.5 s (-100%)
programs/clickhouse-keeper unsigned long DB::HashJoinMethods<(DB::JoinKind)1, (DB::JoinStrictness)6, DB::HashJoin::MapsTemplat… 5.9 s gone -6.2 s (-100%)
programs/clickhouse-keeper unsigned long DB::HashJoinMethods<(DB::JoinKind)1, (DB::JoinStrictness)5, DB::HashJoin::MapsTemplat… 5.9 s gone -6.2 s (-100%)
programs/clickhouse-keeper unsigned long DB::HashJoinMethods<(DB::JoinKind)0, (DB::JoinStrictness)6, DB::HashJoin::MapsTemplat… 5.3 s gone -5.6 s (-100%)
programs/clickhouse-keeper unsigned long DB::HashJoinMethods<(DB::JoinKind)0, (DB::JoinStrictness)1, DB::HashJoin::MapsTemplat… 4.9 s gone -5.2 s (-100%)
programs/clickhouse unsigned long DB::HashJoinMethods<(DB::JoinKind)2, (DB::JoinStrictness)4, DB::HashJoin::MapsTemplat… 6.3 s gone -5.1 s (-100%)
programs/clickhouse unsigned long DB::HashJoinMethods<(DB::JoinKind)0, (DB::JoinStrictness)4, DB::HashJoin::MapsTemplat… 6.2 s gone -5.1 s (-100%)
programs/clickhouse-keeper unsigned long DB::HashJoinMethods<(DB::JoinKind)1, (DB::JoinStrictness)5, DB::HashJoin::MapsTemplat… 4.8 s gone -5.0 s (-100%)
programs/clickhouse unsigned long DB::HashJoinMethods<(DB::JoinKind)1, (DB::JoinStrictness)6, DB::HashJoin::MapsTemplat… 6.2 s gone -5.0 s (-100%)
Compile time of recompiled translation units ⚠️

61 translation units recompiled, 890 s compile time in total, 59 of them have a recent master baseline.

Median compile-time ratio to the baselines is ×1.11 (machine-speed difference or a change affecting every TU); per-TU deltas below are relative to that ratio.
The matched translation units cost +40.4 s (+5%) in total before that adjustment.

Translation unit Master PR Δ vs median
src/Interpreters/HashJoin/HashJoin.cpp 34.7 s 14.3 s -24.0 s (-63%)

Translation units without a recent master baseline:

  • src/Interpreters/HashJoin/HashJoinTable.cpp: 15.6 s
  • src/Interpreters/HashJoin/HashJoinTableScan.cpp: 11.4 s
Symbol sizes
Binary Symbol Master PR Δ
programs/clickhouse unsigned long DB::HashJoinMethods<(DB::JoinKind)0, (DB::JoinStrictness)4, DB::HashJoin::MapsTemplat… 277.36 KiB removed -277.36 KiB (-100.00%)
programs/clickhouse unsigned long DB::HashJoinMethods<(DB::JoinKind)2, (DB::JoinStrictness)4, DB::HashJoin::MapsTemplat… 277.36 KiB removed -277.36 KiB (-100.00%)
programs/clickhouse-keeper unsigned long DB::HashJoinMethods<(DB::JoinKind)2, (DB::JoinStrictness)4, DB::HashJoin::MapsTemplat… 277.36 KiB removed -277.36 KiB (-100.00%)
programs/clickhouse-keeper unsigned long DB::HashJoinMethods<(DB::JoinKind)0, (DB::JoinStrictness)4, DB::HashJoin::MapsTemplat… 277.36 KiB removed -277.36 KiB (-100.00%)
programs/clickhouse-keeper unsigned long DB::HashJoinMethods<(DB::JoinKind)1, (DB::JoinStrictness)6, DB::HashJoin::MapsTemplat… 276.38 KiB removed -276.38 KiB (-100.00%)
programs/clickhouse unsigned long DB::HashJoinMethods<(DB::JoinKind)1, (DB::JoinStrictness)6, DB::HashJoin::MapsTemplat… 276.38 KiB removed -276.38 KiB (-100.00%)
programs/clickhouse-keeper unsigned long DB::HashJoinMethods<(DB::JoinKind)1, (DB::JoinStrictness)6, DB::HashJoin::MapsTemplat… 275.52 KiB removed -275.52 KiB (-100.00%)
programs/clickhouse unsigned long DB::HashJoinMethods<(DB::JoinKind)1, (DB::JoinStrictness)6, DB::HashJoin::MapsTemplat… 275.52 KiB removed -275.52 KiB (-100.00%)
programs/clickhouse unsigned long DB::HashJoinMethods<(DB::JoinKind)0, (DB::JoinStrictness)5, DB::HashJoin::MapsTemplat… 270.35 KiB removed -270.35 KiB (-100.00%)
programs/clickhouse-keeper unsigned long DB::HashJoinMethods<(DB::JoinKind)0, (DB::JoinStrictness)5, DB::HashJoin::MapsTemplat… 270.35 KiB removed -270.35 KiB (-100.00%)
programs/clickhouse unsigned long DB::HashJoinMethods<(DB::JoinKind)1, (DB::JoinStrictness)5, DB::HashJoin::MapsTemplat… 269.33 KiB removed -269.33 KiB (-100.00%)
programs/clickhouse-keeper unsigned long DB::HashJoinMethods<(DB::JoinKind)1, (DB::JoinStrictness)5, DB::HashJoin::MapsTemplat… 269.33 KiB removed -269.33 KiB (-100.00%)
programs/clickhouse unsigned long DB::HashJoinMethods<(DB::JoinKind)0, (DB::JoinStrictness)6, DB::HashJoin::MapsTemplat… 253.43 KiB removed -253.43 KiB (-100.00%)
programs/clickhouse-keeper unsigned long DB::HashJoinMethods<(DB::JoinKind)0, (DB::JoinStrictness)6, DB::HashJoin::MapsTemplat… 253.43 KiB removed -253.43 KiB (-100.00%)
programs/clickhouse unsigned long DB::HashJoinMethods<(DB::JoinKind)0, (DB::JoinStrictness)1, DB::HashJoin::MapsTemplat… 229.50 KiB removed -229.50 KiB (-100.00%)
programs/clickhouse-keeper unsigned long DB::HashJoinMethods<(DB::JoinKind)1, (DB::JoinStrictness)5, DB::HashJoin::MapsTemplat… 229.50 KiB removed -229.50 KiB (-100.00%)
programs/clickhouse unsigned long DB::HashJoinMethods<(DB::JoinKind)1, (DB::JoinStrictness)5, DB::HashJoin::MapsTemplat… 229.50 KiB removed -229.50 KiB (-100.00%)
programs/clickhouse-keeper unsigned long DB::HashJoinMethods<(DB::JoinKind)0, (DB::JoinStrictness)1, DB::HashJoin::MapsTemplat… 229.50 KiB removed -229.50 KiB (-100.00%)
programs/clickhouse unsigned long DB::HashJoinMethods<(DB::JoinKind)2, (DB::JoinStrictness)1, DB::HashJoin::MapsTemplat… new 227.99 KiB +227.99 KiB
programs/clickhouse-keeper unsigned long DB::HashJoinMethods<(DB::JoinKind)2, (DB::JoinStrictness)1, DB::HashJoin::MapsTemplat… new 227.99 KiB +227.99 KiB

Job report

@harikrishnan94
harikrishnan94 removed this pull request from stack #124822 October 21, 2026 12:32
@harikrishnan94
harikrishnan94 changed the base branch from pbnp-scatter-foundations to master November 6, 2026 10:37
harikrishnan94 and others added 2 commits November 18, 2026 09:04
The loop read the clause and the table of the same index. Since the probe
takes the key sizes from the table, it reads only the clause, and
clang-tidy `modernize-loop-convert` asks for a range-based for loop.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The sentence after the one about key getters that build their keys ahead
began with "It", which read as the key getter. It now names
`lazy_row_filter`. The comment also no longer compares the code with
master, and says "instead of building skip bytes" for "builds no skip
bytes".

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@harikrishnan94 harikrishnan94 changed the title Refactor hash join to give each ON disjunct its own HashJoinTable Refactor hash join to give each ON disjunct its own HashJoinTable Nov 18, 2026
…yLayout`

`StorageJoin` was the only caller of `HashJoinTable::keySizes`, and its
check repeated the `chassert` of `makeKeyLayout`. `makeKeyLayout` now
throws that `LOGICAL_ERROR`, and the check and the accessor are gone from
`StorageJoin`.

`HashJoinTable::tryConvertToFixedHashMap` takes the `key64` branch for
every key type other than `key32`. A `chassert` now says that only
`key32` and `key64` reach it, which `HashJoin::canConvertToFixedHashMap`
guarantees.

Comment fixes: wrap the 150-character line above the `HashJoinTable`
constructor, put `ON` and `NULL` in backticks in the comments that the PR
adds, and list in the header of test 05515 the joins it covers (`LEFT ANY`,
`INNER ANY` and `LEFT SEMI`).

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
@harikrishnan94
harikrishnan94 added this pull request to the merge queue Nov 18, 2026
@harikrishnan94

Copy link
Copy Markdown
Contributor Author

@groeneai can you pick this up? Please start now, while #120515 is in the merge queue. Don't wait for the merge.

  1. Today, a join with several ON disjuncts (ON l.a = r.a OR l.b = r.b) takes one left row and looks it up in each disjunct's table in turn. So does a join with an extra ON condition like l.x < r.y. Change both. Take a batch of left rows, look them all up in the first table, then in the next table, and so on. Then handle the matches.

  2. Delete HashJoinTable::probeAll, probeAllRows and MultiMapLookupSampler. No loop that handles one row at a time over several tables should remain.

  3. The implementation already exists in PR Hash join: split probe and build, column scatter kernels and AMAC ring #130344, commit c1ab44bb9559 (branch half-radix-join-stack in harikrishnan94/ClickHouse). Take it from these files: FindKeys.h, FindKeysMaps*.cpp, LookupMethod.h, JoinRightColumns.h, JoinRightColumnsImpl.h, and the test 05622_hash_join_probe_search_batches. That commit changes more than you need, so take only what these two kinds of join use.

  4. The change goes into HashJoinTable.h and HashJoinMethodsImpl.h (joinRightColumns) of Refactor hash join to give each ON disjunct its own HashJoinTable #120515 (pbnp-table-building-blocks). Base your branch on that head, and rebase on master once it merges. Open a new PR.

  5. Perf bar: no regression versus today, on ARM and x86, with parallel_hash, at 1/16/32/64/96 threads. Cover these cases: ALL joins with 2 to 4 OR disjuncts; LEFT ANY, SEMI and ANTI joins where the first disjunct matches rarely and where it matches often; the l.x < r.y kind of condition; tables that fit in cache and tables much larger than the L2 cache; nullable keys. If a case gets slower, fix the loop. Don't revert.

@groeneai

Copy link
Copy Markdown
Collaborator

Acknowledged. I'm investigating the batched probe for multi-disjunct ON and for the extra ON condition (porting the needed parts of c1ab44bb9559 onto this PR's head 9a336ce); I'll follow up here with a PR (or my findings) shortly.

Merged via the queue into master with commit 8b9d512 Nov 18, 2026
193 checks passed
@harikrishnan94
harikrishnan94 deleted the pbnp-table-building-blocks branch November 18, 2026 09:12
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 18, 2026
@groeneai

Copy link
Copy Markdown
Collaborator

#120515 merged before I branched, so the new branch is based on master. Item 5 on ARM: this host is x86 only (2 x 96-core Xeon 6975P), so I can run the 1/16/32/64/96-thread parallel_hash matrix on x86 only. The PR will add a perf test with the listed cases, so its Performance Comparison (arm_release, ...) jobs measure ARM too, but that runner is an m8g.4xlarge with max_threads = 12. For ARM at 16/32/64/96 threads: is that CI comparison enough, or will you run the sweep on the PR's build?

@harikrishnan94

Copy link
Copy Markdown
Contributor Author

@groeneai
For ARM at 16/32/64/96 threads: I'll run the sweep on the PR's build also.
Go ahead with latest master for raising the PR.

groeneai added a commit to groeneai/ClickHouse that referenced this pull request Nov 18, 2026
The hash join moved to `HashJoinTable.h` (ClickHouse#120515), so the rows that the join key getters are asked about are passed
there now: `createJoinKeyGetter` takes the `RowRange`, built by `joinKeyRows` for the probe, the build and the slot
scatter. `min_rows_to_batch_pack_sub_range` is the namespace constant of master.

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

Copy link
Copy Markdown
Collaborator

#132628 contains this change. Its match steps for several disjuncts and for an additional filter (addMatchesOfDisjuncts, addMatchesWithAdditionalFilter) and their batch loop in JoinRightColumns are the code I ported from c1ab44bb9559 (the first byte-identical, the second one line apart), and it removes probeAll, probeAllRows and MultiMapLookupSampler. So I am not opening a separate PR.

Item 5 does not hold yet for that code. Measured with local x86 release builds (no ThinLTO) of master 6483b80 and of the port: Xeon 6975P-C, both servers on the same 96 physical cores of socket 1, parallel_hash, Memory tables with 60M left rows, right keys 0..2M (out of cache) or 0..10K (in cache), each OR key matching about half of the left rows, a "rare" / "often" first disjunct matching 5% / 95%. Median of 9 alternating runs, ratio = port / master, same-server A/A in brackets:

Case 16 threads 96 threads
ALL, 2 OR, out of cache 1.068 (0.999) 1.042 (1.002)
ALL, 3 OR, out of cache 1.131 (0.998) 1.067 (0.989)
ALL, 4 OR, out of cache 1.073 (0.902) 1.128 (0.989)
ALL, 2 OR, Nullable keys, out of cache 1.116 (0.996) 1.079 (0.998)
LEFT SEMI, first rare, in cache 1.076 (1.004) 1.101 (1.006)
LEFT ANTI, first rare, in cache 1.033 (1.023) 1.107 (0.986)
ALL, 3 OR, a row matches all its disjuncts or none 1.205 (1.027) 1.178 (0.996)
LEFT ANTI, 2 OR, same rows 1.284 (0.989) 1.298 (1.004)

Faster: LEFT ANY and SEMI with a first disjunct that matches often (0.73 to 0.92), ALL with 3 or 4 OR in cache (0.76 to 0.92). The l.t < r.t cases are within noise.

Where it goes, from perf record -e branch-misses at 16 threads: for ALL 2 OR out of cache (max_joined_block_size_rows = 0), the two searches miss about as often as master's whole probeAllRows (44.8K samples vs 41.0K), and the match step adds as many again (44.9K), nearly all on its if (!word) test per row and disjunct. For LEFT ANTI 2 OR on rows whose disjuncts match together, the searches themselves miss more: 25.4K samples vs 4.0K in master's probeAllRows. So a change to the match step alone does not close that case.

One caveat for benchmarks of this path: a key column built as intHash64(number + c) is the previous column shifted by one row, which a loop that visits a row's disjuncts in turn can predict. The OR queries in your list on #132628 have that shape (k2 = intHash64(number + 1) % 2000000 next to k_1m = intHash64(number) % 2000000). The numbers above use an independent hash per column (intHash64(number * 32 + c)).

alexey-milovidov added a commit that referenced this pull request Nov 18, 2026
…ression

Resolved the conflicts in `HashJoin.cpp` after `master` gave each `ON`
disjunct its own `HashJoinTable` (#120515):
- includes: drop `<any>` as on `master`, keep `<algorithm>` from this PR;
- `addBlockToJoin`: keep this PR's conditional `shrinkToFit` (skipped when
  compressing on insertion), using `build_layout.num_slots` instead of the
  removed `data->slots`;
- `releaseJoinedBlocks`: keep this PR's streaming `ReleasedJoinedBlocks`;
  it drops `data` without touching the maps, so `master`'s
  `HashJoinTable::releaseMaps` loop there is not needed.

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

Copy link
Copy Markdown
Contributor Author

Item 5 does not hold yet for that code

@groeneai #132628 doesn't show any slowdown. So, your port is not faithful. Do two things:

  1. Run the same tests on HEAD of Separate the hash join key lookup from the match handling of the join kinds #132628 vs it's merge base
  2. Raise a draft PR (mandatory) with a high fidelity port of this change.

@groeneai

Copy link
Copy Markdown
Collaborator

Acknowledged. I'm running the same matrix on #132628's head 0cb94f1 vs its merge base 4564d47, and preparing a draft PR with a high-fidelity port of c1ab44bb9559; I'll post both here.

@groeneai

Copy link
Copy Markdown
Collaborator

Same matrix, host and method as my last table (Xeon 6975P-C, both servers on the 96 physical cores of socket 1, parallel_hash, Memory tables with 60M left rows, an independent hash per key column, median of 9 alternating runs), on two pairs of builds:

Ratio = #132628 / base; same-server A/A was 0.98 to 1.06 on these rows. The last two columns are the port vs master 6483b80 from my previous comment.

Case local, 16 thr local, 96 thr CI, 16 thr CI, 96 thr port, 16 thr port, 96 thr
ALL, 2 OR, out of cache 1.085 1.098 1.092 1.085 1.068 1.042
ALL, 3 OR, out of cache 1.152 1.132 1.110 1.121 1.131 1.067
ALL, 4 OR, out of cache 1.170 1.141 1.152 1.131 1.073 1.128
ALL, 2 OR, Nullable keys, out of cache 1.081 1.098 1.101 1.115 1.116 1.079
LEFT SEMI, first rare, in cache 1.031 1.039 1.078 1.016 1.076 1.101
LEFT ANTI, first rare, in cache 1.039 1.042 1.072 1.006 1.033 1.107
ALL, 3 OR, a row matches all its disjuncts or none 1.175 1.132 1.176 1.161 1.205 1.178
LEFT ANTI, 2 OR, same rows 1.261 1.194 1.330 1.306 1.284 1.298

Faster with #132628, as with the port: LEFT ANY and SEMI with a first disjunct that matches often (0.74 to 0.89), ALL with 3 or 4 OR in cache (0.77 to 0.88). The l.t < r.t cases are 0.90 to 1.08. All 34 queries return the same result on both builds of each pair.

So on this matrix #132628 is slower than its base in the same cases as the port, by about as much. The benchmark I ran on #132628 had two OR queries, LEFT ANY and INNER ALL at max_joined_block_size_rows = 1000, so it did not cover these cases.

The draft PR follows in this thread: the port, rebased on latest master.

@groeneai

Copy link
Copy Markdown
Collaborator

Draft PR: #132896, the port on master 92fdcb7.

The key search (LookupMethod.h, FindKeys.h, FindKeysMaps{All,One,Set}.cpp), the batch driver JoinRightColumns and the match steps are the code of c1ab44bb9559: addMatchesOfDisjuncts byte-identical, addMatchesWithAdditionalFilter one line apart (master's appendsWholeKey). Its one-disjunct probe (search batches, MatchAdder, fused loops, ASOF key search) is not taken, so a join with one disjunct and no additional condition keeps HashJoinTable::probe. The performance test uses an independent hash per key column, so CI's comparison runs the out-of-cache cases of my table, the slower ones included.

alexey-milovidov added a commit that referenced this pull request Nov 21, 2026
…ash-keys

Conflicts are in the hash join, which was reworked on master around `HashJoinTable`
(#120515):

- `createJoinKeyGetter` moved to `HashJoinTable.h`, so its `negative_zeros_canonicalized`
  argument is carried over there. The probe paths (`HashJoinTable::probe` and
  `HashJoinTable::prepareProbeAll`) pass `true`, because the keys of `JoinOnKeyColumns`
  are canonicalized once per source block (`CanonicalJoinKeys`), and the build paths keep
  the default, so the key getter canonicalizes the keys itself.
- `JoinOnKeyColumns` no longer has `key_sizes` (they live in `HashJoinTable`), and
  `joinBlockImpl` no longer takes the maps; the `CanonicalJoinKeys` arguments are kept.
- `Aggregator::mergeStreamsImpl` takes the state cache as an argument on master; the
  `negative_zeros_canonicalized` flag of the row range is kept.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci-relocation-headroom-ok Accept an expected decrease of the relocation headroom in the build check 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 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.

5 participants