Skip to content

Cherry pick #123900 to 26.8: Faster short LowCardinality range inserts from a different dictionary - #124428

Merged
harikrishnan94 merged 11 commits into
backport/26.8/123900from
cherrypick/26.8/123900
Oct 8, 2026
Merged

harikrishnan94 merged 11 commits into
backport/26.8/123900from
cherrypick/26.8/123900

Conversation

@robot-ch-test-poll

Copy link
Copy Markdown
Contributor

Original pull-request #123900

Do not merge this PR manually

This pull-request is a first step of an automated backporting.
It contains changes similar to calling git cherry-pick locally.
If you intend to continue backporting the changes, then resolve all conflicts if any.
Otherwise, if you do not want to backport them, then just close this pull-request.

The check results does not matter at this step - you can safely ignore them.

Before you resolve anything

Conflicts are often caused by a prerequisite change that has not been backported yet, rather than by a real divergence. The bot re-tries this cherry-pick against the release branch on every run, so if that is the case here it will merge itself as soon as the prerequisite lands, and you will see a comment saying so. Manual resolution is only needed while the conflict persists.

Troubleshooting

If the conflicts were resolved in a wrong way

If this cherry-pick PR is completely screwed by a wrong conflicts resolution, and you want to recreate it:

  • delete the pr-cherrypick label from the PR
  • delete this branch from the repository

You also need to check the Original pull-request for pr-backports-created label, and delete if it's presented there

The PR source

The PR is created in the CI job

robot-clickhouse and others added 10 commits October 4, 2026 16:53
Since #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. #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: #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]>
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]>
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]>
…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]>
As requested in review.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ther-dictionary

Faster short `LowCardinality` range inserts from a different dictionary
@robot-ch-test-poll robot-ch-test-poll added pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only! do not test disable testing on pull request labels Oct 7, 2026
@groeneai

groeneai commented Oct 7, 2026

Copy link
Copy Markdown
Collaborator

The only conflict is in the include block of src/Columns/tests/gtest_low_cardinality.cpp. After the 26.8 cut, #115961 added #include <Common/Exception.h> there and #115020 removed the blank line next to it. Neither has a backport label, so this cherry-pick will not clear on its own. #123900 adds DataTypeNullable.h and DataTypeString.h at the same spot. I kept 26.8's include block as it is and added those two includes, so nothing from #115961 or #115020 comes along. The other four files merge cleanly.

What I checked:

  • git diff backport/26.8/123900 <resolution> touches exactly the 5 files of Faster short LowCardinality range inserts from a different dictionary #123900 (254 insertions, 17 deletions), and in each file the added and removed lines are the same as in Faster short LowCardinality range inserts from a different dictionary #123900's own diff.
  • A Debug build of the resolved tree with clang-22, the 26.8 toolset, compiles, and unit_tests_dbms --gtest_filter='*LowCardinality*:*ColumnUnique*' passes 18/18, including the new ColumnLowCardinality.ShortRangeFromDifferentDictionaryMatchesRangePath. When I break the new per-row path, that test fails, so it does exercise the ported code on this branch.
  • The resolution's first parent is 80259c91d3ff, this PR's head, so applying it is a fast-forward.

@harikrishnan94, I have triage but not push on this repository, so I cannot update cherrypick/26.8/123900 myself. Applying it takes two commands:

git fetch https://github.com/groeneai/ClickHouse resolve-26.8-cherrypick-123900
git push origin FETCH_HEAD:cherrypick/26.8/123900

After that the robot merges this PR and opens the 26.8 backport PR itself.

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

Labels

do not test disable testing on pull request pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only!

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants