Repository navigation
Speed up DISTINCT in order, sort-merge joins, LIMIT BY and negative LIMIT BY - #106502
Conversation
|
Workflow [PR], commit [fef062a] Summary: ✅
AI ReviewSummary
Missing context / blind spots
Final Verdict
|
|
📊 Cloud Performance Report ✅ AI verdict: This PR adds a galloping plus binary-search fast path for finding equal-value runs in sorted columns (getEqualRangeEndAssumeSorted), wired only into sort-merge join equal-length detection and the LIMIT BY / negative-LIMIT-BY sorted-stream transforms. ClickBench queries use neither merge joins nor LIMIT BY, so the flagged improvements on Q4, Q28, and Q32 cannot be attributed to this change and read as run-to-run variance across the two builds; they have been downgraded. Notably the tpch_adapted_1_official suite, which is where join and ordering changes would actually surface, showed no per-query shifts, reinforcing that the ClickBench deltas are noise rather than a real effect of this PR. clickbenchFlagged queries (3 of 43)
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
|
DISTINCT in order, sort-merge joins, ORDER BY ... WITH FILL and LIMIT BYDISTINCT in order, sort-merge joins, ORDER BY ... WITH FILL, LIMIT BY and negative LIMIT BY
Algunenano
left a comment
There was a problem hiding this comment.
@nihalzp It seems #106502 (comment) and #106502 (comment) are still unaddressed.
You mentioned Okay, I will keep it safe and not apply it for Float LC. but didn't change it AFAICT.
ColumnLowCardinality::getEqualRangeEndAssumeSorted would need to deal with floats, and nullable(float). And after that uniqueInsertCanonicalFloat is unused and should be removed
I requested review before I made that comment and have not gotten chance to work on it yet :'. I will work on it soon. |
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered by tests: 482/490 (98.37%) | Lost baseline coverage (was covered on master, now uncovered in this PR): 5 line(s) · Uncovered code |
9f7b790
…end-search Speed up `DISTINCT` in order, sort-merge joins, `LIMIT BY` and negative `LIMIT BY`
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 ClickHouse#123700: 18.9 String equality checks per row for 480-row leading runs with a unique second key, versus 2 comparisons per row before ClickHouse#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]>
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.8). Fixes the LIMIT BY regression from #106502 reported in #123700
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
The PR tries to optimize the scenario when we have a sorted
IColumnand we want to find the end row number of equal key in the column. In many places, we do normal linear scan and in some places, we do binary search but have duplicate implementation. This PR unifies this utility it as aIColumnmethod and allows a single optimized binary search implementation for all use cases.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Speed up operators that scan a sorted stream for runs of equal key values —
DISTINCTin order,LIMIT BYin order, negativeLIMIT BYin order,full_sorting_mergeandpartial_mergejoins.