Repository navigation
Cherry pick #123808 to 26.9: Speed up in-order LIMIT BY, DISTINCT, window functions, merge joins and FINAL on multi-column keys with long leading-key runs - #124726
Merged
robot-clickhouse-ci-2 merged 11 commits intoOct 9, 2026
Conversation
…eSorted overloads Checks the three multi-column overloads (columns, positions, SortDescription) against a row-by-row whole-key oracle for every begin and two range ends, on 2- and 3-column sorted keys with leading runs of 1, 7, 8, 9 and 600 rows and trailing runs of 1, 2, 7, 8, 9, 300 rows or unique values, over UInt64, UInt32, String, Nullable(String) with arbitrary nested values at NULLs, Float64 with NaN and -0.0/+0.0, and LowCardinality(String) with dictionary indices out of value order. Also the single-column and no-column paths. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The sorted-stream LIMIT BY, negative LIMIT BY and full_sorting_merge JOIN find every group with per-column narrowing: from the group's first row, column 0's run end is searched, then column 1 within it, and so on. They call it once per group, each call starting where the previous group ended, and nothing was remembered between calls. So when column 0 has long runs and a later column ends the groups early, column 0's whole remaining run was searched again for every group inside it (issue #123700: 18.9 String equality checks per row for 480-row leading runs with a unique second key, versus 2 comparisons per row before #106502). `SortedKeyRuns` remembers, for each key prefix, the last run found. Within a sorted range a search from a row inside that run ends the prefix's run at the same row, so column `i` is searched again only when the search leaves the run of columns `0..i`. Every per-column call it makes has exactly the arguments the stateless narrowing passes for that column from the same row; it only skips the calls for leading columns whose remembered run still contains the row, so results are identical and the per-column work is a subset of today's. A reuse checks both bounds of the remembered run, so any call order is correct. Keys of fewer than two columns take the stateless path unchanged. In debug and sanitizer builds every call is cross-checked against the full narrowing from the same row, so the sampled contiguity checks inside the per-column searches keep firing on the inputs they fire on today. The LIMIT BY transforms use one instance per chunk; the join cursor resets its instance in setChunk, the only writer of its key columns and row count. The stateless multi-column helper in SortCursor.h now shares the same narrowing loop. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The remembered runs use VectorWithMemoryTracking, as the style check requires for standard containers. Two perf-test arms cover the shape of issue #123700: LIMIT BY on a String key read in primary key order, and a full_sorting_merge join on a two-column UInt64 key, both with long runs of the leading column and every group ended by the second column. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…gative LIMIT BY perf arm The walk test now counts the per-column searches `findRunEnd` makes: column 0 must be searched once per run of column 0, and the walk must never search more than the stateless narrowing from the same rows. Debug and sanitizer builds cross-check every result through the same callback, so those calls are subtracted there. A `findRunEnd` that keeps nothing returns the same boundaries, so only the counts catch it. The five multi-column oracle tests become one loop over the shared list of key types. Performance tests: a negative `LIMIT BY` over two `String` key columns with long runs of the leading one, and the two-column `LIMIT BY` query reads a single sorted stream (`ORDER BY a, b`), so it measures the sorted-stream transform rather than the final hash `LIMIT BY`. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…eyRuns walk test In a forward walk, column `i` is searched only from the first row of a run of columns `0..i`, and the search stops after the first column whose run from that row is one row long. The walk test now derives the expected count of every column from the data and compares it with the searches `findRunEnd` makes, instead of checking column 0 only. A `findRunEnd` that reuses only column 0's run returns the same boundaries and passed before; it fails now on the 3-column keys, where the middle prefix's run is the one reused. The debug cross-check runs only for two or more columns, so its calls are subtracted only there. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Extends SortedKeyRuns to the other callers that find the runs of one sorted range one after another with the multi-column narrowing search, where every group restarted the search on the leading key column (a gallop over the rest of its run): - DistinctSortedStreamTransform (DISTINCT in order) - WindowTransform peer groups: the RANGE/GROUPS frame end at CURRENT ROW and findPeerGroupEnd (GROUPS offset frames), one cache keyed on (block, end) shared by the frame boundaries; a single-column ORDER BY keeps the stateless search - SlidingIndexes::markKeyChanges (multi-column PARTITION BY boundaries) - MergeJoinCursor::getEqualLength (partial_merge join) - ReplacingSortedAlgorithm run skipping, one cache per source, invalidated on a new chunk or a new batch end The callers use new SortedKeyRuns-aware overloads of the multi-column getEqualRangeEndAssumeSorted in SortCursor.h; the LIMIT BY transforms switch to them too. SortedKeyRuns::findRunEnd is ALWAYS_INLINE: out of line it cost a few percent on keys with one-row leading runs. Measured on 20M rows sorted by (a String with runs of 20000, b String with runs of 4), RelWithDebInfo, interleaved, identical results: ReplacingMergeTree FINAL with enable_vertical_final = 0 0.44x, window ORDER BY a, b 0.73x, PARTITION BY a, b 0.81x, GROUPS frame 0.88x, DISTINCT a, b 0.83x, partial_merge join USING (a, b) 0.74x. With one-row or four-row leading runs the same queries stay within 0.99x-1.06x. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…Runs DISTINCT in order, window peer groups (RANGE and GROUPS frames), PARTITION BY boundaries, partial_merge join and ReplacingMergeTree FINAL without vertical FINAL, on a two-column String key with long runs of the leading column. Locally (5M rows, RelWithDebInfo, same results): 0.72x, 0.60x, 0.46x, 0.69x, 0.94x and 0.37x. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…-runs # Conflicts: # src/Processors/Transforms/Window/SlidingIndexes.cpp
parallel_full_sorting_merge_join #1 (full_sorting_merge on one UInt64 key) and window_function_with_order #3 got 10-20% slower: per row, the join called an out-of-line findKeyRangeEndAssumeSorted, and the window code called out-of-line copies of the SortedKeyRuns overloads and WindowTransform::findPeerRunEnd, where master inlined the stateless search. - force-inline findKeyRangeEndAssumeSorted, the SortedKeyRuns overloads in SortCursor.h and findPeerRunEnd; - the SortedKeyRuns overloads take the stateless search for one-column keys; - the peer-group marking of SlidingIndexes (new in master) uses one SortedKeyRuns per block instead of resetting it for every partition, which called vector::assign per partition. The partitions are searched in order and none starts inside an earlier one, so no stale run can be reused; the reset contract is stated precisely. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Speed up in-order LIMIT BY, DISTINCT, window functions, merge joins and FINAL on multi-column keys with long leading-key runs
Collaborator
|
The only conflicts are in What I checked:
@nihalzp, I cannot push to After that the robot merges this PR and opens the 26.9 backport PR itself. |
Take the multi-column sorted key run reuse for LIMIT BY, negative LIMIT BY, DISTINCT in order, full_sorting_merge and partial_merge JOIN. Skip the WindowTransform changes (reverted on master by #124640) and the ReplacingSortedAlgorithm run skipping (needs the batch sorting queue, absent in 26.9). Fixes the LIMIT BY regression from #106502 reported in #123700
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 #123808
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