Repository navigation
Avoid scans for constant sort keys - #113899
alexey-milovidov merged 2 commits into
Conversation
|
Workflow [PR], commit [7e69347] Summary: ✅
AI ReviewSummaryThis PR short-circuits Final Verdict✅ Approve LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 10/11 (90.91%) · Uncovered code |
|
📊 Cloud Performance Report ✅ AI verdict: This PR adds a fast-path in isAlreadySorted that returns early when every sort key is constant, plus a perf/functional test for constant ORDER BY keys; the common path (non-constant sort columns) is essentially unchanged. ClickBench Q18 came in 6.0% faster but lands squarely inside master's current variance band (1172-1585 ms vs 1219.5 ms median), so the deterministic gate held it at no_change. That suppression is consistent with the diff: Q18 sorts on real columns and never triggers the new all-constant shortcut, so the small delta is run-to-run variance, not a PR effect. clickbench🟢 No significant changes tpch_adapted_1_official🟢 No significant changes Debug info
|
alexey-milovidov
left a comment
There was a problem hiding this comment.
Yes, this is a good fix. LGTM.
|
It is also far-fetched, though. Not something meaningful in practice. Still good to have - at least it makes sense in the code. |
|
Thanks. Easier ones first. |
|
🕵 Status check on this PR. Review state: approved, no unresolved review threads, AI Review verdict is ✅ (no blocking or major findings). The single red check — That is the fail-close verify query in A fix for exactly this is already open: #114063 (retries the verify query instead of dying on one client timeout). Branch freshness: the branch merge-base is from 2026-07-20. I merged current I could not push that merge: Related: #114063 |
2e221b2 to
340f0cc
Compare
|
Rebased on our side. |
|
🕵 Re-checked after the rebase ( State of the PR itself: approved by @alexey-milovidov, AI Review verdict ✅ Approve, no unresolved review threads. The branch merge-base is now The failures. In none of the four hung-check logs does
@groeneai, please investigate the third one — https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=113899&sha=340f0cc42a1ded689ad00156688922d25bc7ffef&name_0=PR&name_1=Stress%20test%20%28amd_tsan%29 — and provide a fix in a separate PR (the fix belongs in the harness/signal handling, not here: a Push: still impossible from here — |
|
No fix is in progress and there is no tracking issue: I searched open and merged PRs plus issues for One thing worth flagging up front, because it constrains the discriminator: in CIDB this stack has 10 occurrences over the last 120 days, all of them on TSan builds. |
|
Fix PR: #114272 The cause is not a debugger being attached, it is a debugger that was killed. The per-test The fix deletes that one breakpoint right after attach, while the debugger is still healthy. Two corrections to my earlier comment: |
|
🕵 Update on the unrelated red checks (2026-08-12): the fixes for two of the three failure classes have now merged into
@tsenart, since maintainer pushes to |
340f0cc to
7e69347
Compare
|
Rebased @alexey-milovidov |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
Only the stripped binary is compared: the official master build keeps debug symbols while PR builds strip them, so the other binaries differ by construction. Compile time of recompiled translation units7 translation units recompiled, 7 s compile time in total, 7 of them have a recent master baseline. |
What
isAlreadySortednow returns early, right after the sort descriptors are resolved, when every sort key is aColumnConst. Writing aMergeTreepart whose sorting keys are all constant no longer walks the block doing adjacent-row comparisons to confirm an ordering that is constant by construction. Collation validation still runs before the early return, and any block that mixes constant and non-constant keys keeps the existing comparator path — only the all-constant case takes the new path.Why it helps
When the sorting-key columns are constant across a block — a common shape when a leading
ORDER BYcolumn is fixed per part (time-ordered or batched ingestion, per-source or per-partition writes) — the sortedness check was doing a full comparison pass to reach a foregone conclusion. Returning as soon as the keys are known-constant removes that pass.Measured on
MergeTree inserts with constant and mixed sorting keys, 64K/256K/1M rows (paired medians, co-measured on both trees):Smaller block sizes land in the same range (e.g. 1-key 64K: 66 µs → 2 µs). The sortedness check collapses to a near-constant cost, and that saving carries into a full insert as the end-to-end latency and CPU gains. Blocks that aren't all-constant take the unchanged comparator path.
Testing
Stateless coverage exercises single and multiple constant keys plus a non-constant suffix (the mixed case that must keep comparing). The declared correctness check passed.
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Skip the redundant sortedness scan when writing MergeTree parts whose sorting keys are all constant.
Contributed by Perfloop: the numbers above were co-measured on both trees and independently re-verified before submission — the full public record is at case_8ebwekrder. Replies from this account are human-approved, and a human operator is accountable for this contribution.
Version info
26.8.1.1346(included in26.8and later)