Repository navigation
Faster short LowCardinality range inserts from a different dictionary - #123900
harikrishnan94 merged 9 commits into
Conversation
Since ClickHouse#107189 the hash join emits consecutive same-block rows of a key with one `insertRangeFrom` instead of one `insertFrom` per row. `RowRefList` keeps a key's rows in nodes of up to 6 refs, so these runs are 2-6 rows long. When the source block's dictionary differs from the output column's, `ColumnLowCardinality::insertRangeFrom` builds about five temporary columns per call whatever the length (`cut`, `mapUniqueIndex`, `index` of the used keys, `uniqueInsertRangeFrom`, `index` of the positions), so such a run costs ~25x per row what `insertFrom` costs. ClickHouse#115961 avoids it only when the right side has a single shared dictionary; several parts, `FINAL` or a `LowCardinality` value computed per block still hit it on every run. Ranges of at most 32 rows from a different dictionary are now translated row by row with the per-key rules of `uniqueInsertRangeImpl`: the source NULL maps to the destination NULL, a key that compares equal to the default (so `-0.0` too) maps to the default slot, anything else goes through `uniqueInsertFrom`, which canonicalizes NaN. Positions are computed before the indexes change and the range is bounds-checked first, so a throw leaves the rows untouched, as on the range path. The threshold is the largest power of two at which the per-row path is not slower than the range path in a microbenchmark of ns per call (RelWithDebInfo, one core, median of 7): range/per-row at 32 rows is 1.99x for 7 short keys, 2.52x for 10000 random keys, 1.48x for two 200-byte keys and 3.61x for 1000 UInt64 keys; at 64 the 200-byte case drops to 0.87x. A/B, RelWithDebInfo, n = 10 interleaved, medians (26.6.8.7 / parent / this): issue repro 4 with `compatibility = '23.8'` 2.31 / 8.93 / 2.30 s; repro 1 with the table in 4 parts 1.33 / 5.12 / 0.79 s, at default settings 1.00 / 4.40 / 0.50 s; a table-free `hash` join 0.18 / 0.75 / 0.15 s. Shared-dictionary and `String` controls unchanged. Repro 4 at default settings does not reach this path (the join sides are swapped and the time is in the row-store emit). The gtest compares a short range against a 600-row range of the same rows for every length 1-256; it fails when the default fold, the bounds check or the NULL branch is removed. Closes: ClickHouse#123874 Co-Authored-By: Claude Opus 5.5 <[email protected]>
…eFrom wrap `start + length` can overflow `size_t` (e.g. `start = SIZE_MAX, length = 1` sums to 0), so the guard passed and the per-row loop read outside the source indexes. Compare `start` and `length` separately, as `ColumnIndex::insertIndexesRange` does, and cover `start = SIZE_MAX` in the gtest. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; round 1: 2 findings, round 2 on the fix: 0), plus my own review.
Severity: ❌ blocker / |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-4:20261004-185300 |
|
Workflow [PR], commit [34ac297] Summary: ✅
AI ReviewSummaryThis PR teaches Final VerdictStatus: ✅ Approve LLVM Coverage ReportMeasured on commit 34ac297.
Changed lines: Changed C/C++ lines covered: 50/52 (96.15%) · Uncovered code |
Build (arm_tidy) failed with cppcoreguidelines-pro-type-member-init /
hicpp-member-init on the `positions` stack array of the short-range path
in ColumnLowCardinality::insertRangeFrom. Only the first `length` entries
are written, all before they are read, so the array does not need to be
initialized.
Value-initializing it (`positions{}`, the tidy fix-it) zeroes 256 bytes on
every call of this hot path: RelWithDebInfo A/B, n=11 interleaved medians
on the table-free hash-join repro, 0.146 -> 0.148 s (q7) and
0.146 -> 0.149 s (q7p), identical results. Use the in-tree NOLINT idiom
for write-before-read stack buffers instead (AggregateFunctionUniq.h,
UniquesHashSet.h), which leaves codegen unchanged.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
📊 Cloud Performance Report ✅ AI verdict: No query changed in a way this PR can explain. The PR adds a per-row path to ColumnLowCardinality::insertRangeFrom, used only for inserts of up to 64 rows that come from a different dictionary. That situation mainly occurs when a hash join outputs right-side LowCardinality columns. The only flagged row is ClickBench Q34 (+6.5%), which stayed suppressed because it falls within master's normal variance. That decision fits the diff: ClickBench stores its data in plain String and numeric columns, so Q34 never reaches the changed code, and CPU time moved only about 1.5%. Neither ClickBench nor tpch_adapted_1_official exercises the new path, so the hash-join performance test this PR adds is the right place to measure its effect. clickbenchFlagged queries (1 of 43)
Change = percent below ×2; the ratio of medians (×N faster/slower) beyond, where percent understates the scale. q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on. tpch_adapted_1_official🟢 No significant changes Debug info
|
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Object file sizes12 object files changed (-13.09 KiB total), 0 added.
719 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only Compile time of recompiled translation units1241 translation units recompiled, 9383 s compile time in total, 1241 of them have a recent master baseline. |
harikrishnan94
left a comment
There was a problem hiding this comment.
Could you benchmark a few more lengths between 32 and 1024 to choose a reasonable crossover? Use that cutoff for max_rows_to_translate_individually and add a short comment explaining how it was chosen.
Requested in review: pick the crossover of `insertRangeFrom` from a different dictionary from measurements between 32 and 1024 rows. Microbenchmark (RelWithDebInfo, one pinned core, median of 7, two runs on different cores agree within 1%). Ratio = range path time / per-row time, above 1 means per-row translation is faster: source keys 32 48 64 96 128 256 512 1024 7 short strings 1.96 1.37 1.07 0.76 0.61 0.38 0.26 0.20 10 8-byte strings 2.11 1.48 1.14 0.81 0.63 0.37 0.25 0.19 10 UInt64 2.35 1.65 1.28 0.91 0.72 0.43 0.28 0.22 2 200-byte strings 1.45 1.06 0.86 0.65 0.54 0.39 0.30 0.26 100 16-byte strings 3.33 2.80 2.49 2.11 1.22 0.71 0.39 0.21 1000 UInt64 3.61 3.12 2.90 2.71 2.56 2.82 2.48 0.96 10000 strings 2.49 2.23 2.05 1.91 1.85 1.99 1.80 1.93 With a few distinct short keys the range path overtakes at 66-85 rows, so 64 is the largest power of two that does not lose there; with 100 or more distinct keys per-row translation keeps winning well past 64. Two distinct 1000-byte keys favour the range path at every length from 16 (hashing each row dominates), as they already did at 32. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Thanks. I benchmarked both paths at 16-1024 rows (RelWithDebInfo, one pinned core, median of 7; two runs on different cores agree within 1%). Ratio = range path time / per-row time, above 1 means per-row is faster:
With a few distinct short keys the range path overtakes at 66-85 rows, so I set |
CI finish ledger - d52cce6Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
CIDB has no other Session id: cron:our-pr-ci-monitor:20261005-223244 |
|
@groeneai Could you move the |
…olumnUnique Requested in review: keep the NULL and default-value rules of `uniqueInsertRangeFrom` in one place. `ColumnUnique::getSpecialValueIndex` now holds them and is used by `uniqueInsertRangeImpl` and by the new `uniqueInsertRowsFrom`, which translates the rows of a short range from a different dictionary. `ColumnLowCardinality::insertRangeFrom` only gathers the source indexes and hands them over. `uniqueInsertFrom` is unchanged. A per-row virtual call (one `uniqueInsertFrom`-like call per row) was measured first and cost 14% on the table-free join below, because the source column was unwrapped on every row; taking the whole range unwraps it once. `getSpecialValueIndex` is `ALWAYS_INLINE`: without it the call is not inlined in `uniqueInsertRowsFrom` and the join is 5% slower. Microbenchmark (RelWithDebInfo, one pinned core, ns per insertRangeFrom call, 2 runs on different cores): on the per-row path (2-64 rows) this commit takes 0.91-0.99x the time of the previous head for every key set except two 1000-byte strings (1.00-1.01x); on the range path (96-1024 rows) 0.99-1.01x. At 2-6 rows it is 3-25x faster than the parent of the PR. End to end (median of 7, base = parent of the PR / previous head / this): table-free hash join 0.757 / 0.143 / 0.146 s, parallel_hash 0.756 / 0.144 / 0.147 s, issue repro 1 with 4 parts (compatibility 24.8) 3.755 / 0.773 / 0.784 s. Results identical. The gtest now also checks the NULL and default rules against the source, since both paths share them; removing either rule turns it red. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Thanks. Done in 4a9f718: I tried a per-row virtual call first: it was 14% slower on the table-free join because it unwrapped the source column on every row, so the method takes the whole range and unwraps it once. The helper is Microbenchmark (RelWithDebInfo, one pinned core, two runs on different cores), time of this commit / previous head, by range length:
The range path (96-1024 rows) is 0.99-1.01x. At 2-6 rows this is 3-25x faster than the parent of the PR. End to end, median of 7, within 2% of the previous head:
Since both paths now share the rules, the gtest also checks that NULL and default rows land in their slots; removing either rule turns it red. For merge readiness: the branch merges cleanly with current master and CI is running on the new head. The only failure on the previous head, the Stress test (arm_release) hung check, is a fuzzed |
|
Replace With |
As requested in review. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Done in ad338f1: replaced the comment with your line (comment-only change). |
…rt-from-other-dictionary
CI finish ledger - ad338f1Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
Session id: cron:clickhouse-maint-slot-13:20261006-155500 |
CI finish ledger - 38a9e21Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
Session id: cron:clickhouse-maint-slot-14:20261006-232900 |
|
@groeneai Make this Merge ready |
…rt-from-other-dictionary
|
Merged current master in 34ac297. The PR diff itself is unchanged, and the LowCardinality / ColumnUnique gtests pass on the new head. The only red on the previous head was |
Backport #123900 to 26.9: Faster short `LowCardinality` range inserts from a different dictionary
Cherry pick #123900 to 26.8: Faster short `LowCardinality` range inserts from a different dictionary
… from a different dictionary
Backport #123900 to 26.8: Faster short `LowCardinality` range inserts from a different dictionary
…ge inserts from a different dictionary
Closes: #123874
Related: #114870
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Hash joins with many matches per key are faster when they output a
LowCardinalitycolumn whose blocks have different dictionaries (several parts,FINAL, or aLowCardinalityvalue computed per block): short ranges from another dictionary are now inserted key by key instead of through temporary columns. Such joins run 4-9x faster, at the 26.6 level or better.Description
Since #107189 the hash join emits consecutive same-block rows of a key with one
insertRangeFrom; such runs are 2-6 rows long. When the source block's dictionary differs from the output column's,ColumnLowCardinality::insertRangeFrombuilds about five temporary columns per call, so a run costs ~25x per row whatinsertFromcosts. #115961 avoids it only when the right side has a single shared dictionary.Ranges of at most 64 rows from a different dictionary are now translated row by row by
ColumnUnique::uniqueInsertRowsFrom, which shares the NULL and default-value rules (-0.0folds to the default) withuniqueInsertRangeFromthrough one helper; results are unchanged. A microbenchmark over 16-1024 rows puts the crossover at 66-85 rows for a few distinct short keys (two 200-byte keys: 0.86x at 64) and past 128 rows for 100+ distinct keys. Longer ranges and same-dictionary paths are untouched.Medians of 10 interleaved runs, RelWithDebInfo, base = parent commit, official 26.6.8.7 for reference:
FINAL)hashShared-dictionary and
Stringcontrols are unchanged. Repro 4 at default settings takes a different path (the planner swaps the join sides) and is not affected.A new gtest checks that lengths 1-256 give the same indexes and dictionary as the range path and that NULL and default rows land in their slots (
String,Nullable,Float64with-0.0and NaN payloads, nullable promotion, a shared source, index width growth, out-of-bound ranges).🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/123900]
Version info
26.10.1.1741-master(included in26.10and later)26.9.13.11,26.8.21.9