Repository navigation
Cherry pick #123900 to 26.8: Faster short LowCardinality range inserts from a different dictionary - #124428
Merged
Conversation
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]>
…rt-from-other-dictionary
…rt-from-other-dictionary
…ther-dictionary Faster short `LowCardinality` range inserts from a different dictionary
Collaborator
|
The only conflict is in the include block of What I checked:
@harikrishnan94, I have After that the robot merges this PR and opens the 26.8 backport PR itself. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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-picklocally.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:
pr-cherrypicklabel from the PRYou also need to check the Original pull-request for
pr-backports-createdlabel, and delete if it's presented thereThe PR source
The PR is created in the CI job