Repository navigation
Refactor hash join to give each ON disjunct its own HashJoinTable - #120515
Conversation
|
Workflow [PR], commit [9a336ce] Summary: ✅
AI ReviewSummaryRefactors Final VerdictStatus: ✅ Approve |
8c6b0f9 to
4e05476
Compare
2a1766f to
4327787
Compare
4e05476 to
4dbcbc5
Compare
7ca766e to
38ae471
Compare
3c48983 to
043f608
Compare
Build profile diff (arm_release)Comparing 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. The official master build is compiled with Object file sizes
|
| 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 ssrc/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 |
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]>
HashJoinTableON disjunct its own HashJoinTable
…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]>
|
@groeneai can you pick this up? Please start now, while #120515 is in the merge queue. Don't wait for the merge.
|
|
Acknowledged. I'm investigating the batched probe for multi-disjunct |
|
#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 |
|
@groeneai |
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]>
|
#132628 contains this change. Its match steps for several disjuncts and for an additional filter ( 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,
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 Where it goes, from One caveat for benchmarks of this path: a key column built as |
…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]>
@groeneai #132628 doesn't show any slowdown. So, your port is not faithful. Do two things:
|
|
Acknowledged. I'm running the same matrix on #132628's head |
|
Same matrix, host and method as my last table (Xeon 6975P-C, both servers on the 96 physical cores of socket 1,
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
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 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 The draft PR follows in this thread: the port, rebased on latest master. |
|
Draft PR: #132896, the port on master The key search ( |
…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]>
Related: #115254
A hash join keeps one map per
ONdisjunct. On master the maps live inRightTableData::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, theStorageJoinread, 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.
For a user:
max_bytes_in_join, theSpillingHashJointhreshold and theGraceHashJoinrepartition 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, forLEFTandINNERANYand forLEFT SEMI, the row of the first disjunct inONorder that matches.RIGHT ANYandRIGHT SEMIstill look up every disjunct.Reading order:
HashJoinTable.h(the class comment and its public members) ->HashJoin::addBlockToJoinandHashJoinTable::insert->HashJoinMethods::joinRightColumnsandHashJoinTable::probe/probeAll->NotJoinedHashandHashJoinTable::scanNotJoined->StorageJoin->~RightTableData.The tables:
HashJoinreads it from its first table.probeandprobeAllstill 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 runsprobeAllRowswithlazy_row_filter, which checks theNULLkey and the mask of each row when the loop visits it, instead of building skip bytes for the whole selector. Thehashedand packedStringkeys still build them.Arenais 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.probeAllbuilds the key getters and the map pointers, andprobeAllRowsholds the row loop.probetakes one callback for each row, andprobeAllone for a match and one for the end of a row, so each join kind keeps its own output handling.Changelog category (leave one):
Workflow [PR]
Sync PR [sync-upstream/pr/120515]
Version info
26.11.1.2689-master(included in26.11and later)