Repository navigation
Conversation
…) sorting key A granule that spans a change in a leading key column, followed by a descending key column, was pruned incorrectly. The per-column left/right swap in MergeTreeDataSelectExecutor is correct only when a single column varies; across a leading-column change it inverts the head/tail decomposition in forAnyHyperrectangle, so an equality/range on the descending column could prune a matching granule (wrong result). Thread reverse_flags into KeyCondition::checkInRange and forAnyHyperrectangle (both dense and sparse overloads), feed the raw storage-order begin/end tuples unswapped, and build the value-space range per column honouring its sort direction. Reproduces without any JOIN/subquery/filter-push-down; the issue's push-down + ANY JOIN + CTE + NOT IN shape merely relocates the filter onto the reverse-keyed table. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-worker-slot-4:20260713-162100 |
|
cc @CurtizJ @KochetovNicolai — could you review? This makes MergeTree primary index analysis ( |
|
Workflow [PR], commit [280f646] AI ReviewSummaryThis PR fixes reverse-sorting-key primary-index pruning by making Final VerdictStatus: ✅ Approve |
The reverse (DESC) sorting-key index-analysis fix widens the pruned range for the reverse-keyed generic exclusion search (scenario E), so the pruning-off case now reads 9/100 granules instead of 8/100. Reading one more granule is the safe direction: the fix corrects the over-pruning class from ClickHouse#110275 and loses no rows (count() stays 1 with pruning on, off, and no index). Co-Authored-By: Claude Opus 4.8 <[email protected]>
|
Fixed the Fast test failure on |
CI finish ledger — 0597a7eEvery failure below has an owner. Only
Finish Workflow and Mergeable Check are green; all functional/integration/stress checks pass on this head. The prior Fast test 04356 reference difference (PR-caused) was resolved in this commit (0597a7e). Session id: cron:our-pr-ci-monitor:20260715-010000 |
…rk range
In the last mark range of a part without a final mark, the sparse path of
markRangesFromPKRange set equal_boundaries_mask[i] = isNullAt(range.begin),
assuming the right boundary is always +inf. For a descending (reverse) key
column the storage-order right endpoint is -inf, so the boundaries are never
equal there. Marking them equal collapsed the column to the point {NULL},
which caused both a false exclusion (WHERE n = 5 returned 0) and a wrongly
exact range (WHERE n IS NULL returned 4 out of 4 rows because the filter was
eliminated). Requires Nullable reverse key column, allow_nullable_key = 1 and
index_granularity_bytes = 0.
Also add test coverage for parts without a final mark, where the last-mark
+inf/-inf endpoints are actually exercised (adaptive parts have an index
entry at the end of the last granule, so the previous test never hit this
branch).
Related: ClickHouse#110288
Related: ClickHouse#110275
|
Pushed 1f1d4a5 with a fix for a residual wrong result in the code this PR touches, found while reviewing. In the last mark range of a part without a final mark, the sparse path set CREATE TABLE tn (n Nullable(Int32)) ENGINE = MergeTree ORDER BY n DESC
SETTINGS allow_nullable_key = 1, index_granularity = 2, index_granularity_bytes = 0;
INSERT INTO tn VALUES (NULL), (NULL), (NULL), (5);
SELECT count() FROM tn WHERE n = 5; -- returned 0, correct is 1 (false exclusion)
SELECT count() FROM tn WHERE n IS NULL; -- returned 4, correct is 3 (range wrongly exact, filter eliminated)Both symptoms come from the same collapse (the point range makes |
The review flagged a possible empty Range(+Inf, value) for a descending Nullable key when a mark boundary lands on NULL. NULL maps to +Inf and sorts first for a descending column, so storage order is NULL, values..., i.e. no interior mark can begin at a value and end at NULL. The empty range cannot form. Add tests for the exact reported case (ORDER BY ts DESC, rows 4, 3, NULL, index_granularity = 2) and a composite (a, n DESC) case with a NULL mark boundary, covering both dense and sparse index analysis. Related: ClickHouse#110288 Related: ClickHouse#110275 Co-Authored-By: Claude Opus 4.8 <[email protected]>
index_granularity_bytes=0 with the default min_bytes_for_wide_part emits a stderr warning that Fast test rejects. Force Wide format by also setting min_bytes_for_wide_part=0 (consistent with non-adaptive granularity); mark boundaries and results are unchanged. Co-Authored-By: Claude Opus 4.8 <[email protected]>
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 122/124 (98.39%) · Uncovered code |
|
Thanks @fm4v, verified 1f1d4a5. Gating the last-mark equal-boundaries shortcut on CI finish ledger - b612e41Every failure below has an owner. Only
Finish Workflow and Mergeable Check green; all functional/integration/stress/fuzzer checks pass on this head. The earlier PR-caused Fast test failure on |
|
@nihalzp checked: #109901 does not fix this. They are complementary fixes to different code paths. Built a binary with only #109901's source changes (its 2-file CREATE TABLE t_r (g String, r Enum8('poor'=1,'ok'=2,'great'=3))
ENGINE = MergeTree ORDER BY (g, r DESC)
SETTINGS allow_experimental_reverse_key = 1, index_granularity = 8192;
INSERT INTO t_r VALUES ('manual','ok'),('manual','poor'),('novel','great'),('novel','great');
SELECT count() FROM t_r WHERE g='novel' AND r='great'; -- primary key
SELECT count() FROM t_r WHERE g='novel' AND r='great' SETTINGS use_primary_key=0; -- reference
Why they don't overlap: #110288 fixes the value-space range construction in So #110288 is still needed; it and #109901 fix disjoint reverse-key pruning bugs. |
|
|
|
The Concrete case from #110275 (
I built exactly the contract you describe (caller normalizes the storage endpoints so vs this PR: The reason no caller-side normalization fixes it: within one decomposition the trailing column appears in BOTH the head sub-range (lower-bounded, anchored at the begin value) and the tail sub-range (upper-bounded, anchored at the end value). For a descending column both of those need to flip direction, and they draw from different endpoints, so a single ordered If the specific thing you want removed is the per-call |
Resolve the conflict in `MergeTreeDataSelectExecutor` while preserving the PR storage-order boundary handling and the `reverse_flags` handoff to `KeyCondition`. ClickHouse#110288
|
A more proper and cleaner fix here: |
|
📊 Cloud Performance Report ✅ AI verdict: This PR fixes a correctness bug in MergeTree primary-key index analysis for reverse (descending) sorting keys, moving the reverse-column handling into KeyCondition; for ascending keys the logic is behavior-preserving. The two flagged ClickBench improvements (Q4 change_factor -21.6%, Q15 -19.3%) both fall inside master's current-era variance band and were correctly kept as no_change: ClickBench tables use only ascending keys, so the touched code path is never hit, and CPU time is essentially unchanged (Q4 2682.7 vs 2678.3 ms, Q15 2595.2 vs 2621.5 ms) even as the wall-clock median dropped. The envelope suppressions are consistent with the diff — no real performance impact from this change. clickbench🟢 No significant changes tpch_adapted_1_official🟢 No significant changes Debug info
|
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix a wrong result from primary key index analysis on tables with a reverse (descending) sorting key (
allow_experimental_reverse_key). A granule spanning a change of a leading key column followed by a descending key column could be pruned incorrectly, dropping matching rows.Description
Closes: #110275
Reported wrong result (returns 0 rows with
query_plan_filter_push_down=1, 25 with=0). The push-down/ANY JOIN/CTE/NOT IN shape only relocates the filter; the actual bug is in primary index analysis over a reverse sorting key and reproduces with plain equality, no join or subquery:Root cause:
MergeTreeDataSelectExecutorswaps the left/right boundary values per reverse column before callingKeyCondition::checkInRange. That swap is correct only when a single key column varies within a mark range. When the range spans a change of a leading column,forAnyHyperrectangle's head/tail decomposition ([x1] x [y1..+inf)and[x2] x (-inf..y2]) needs the trailing value at the range begin for the head and at the range end for the tail; the swap routes them the wrong way, so the range on the descending column is inverted and a matching granule is pruned.Fix: pass
reverse_flagsintoKeyCondition::checkInRangeandforAnyHyperrectangle(dense and sparse overloads), feed the raw storage-order begin/end tuples without swapping, and construct the value-spaceRangeper column honouring its sort direction. Bothuse_lightweight_primary_key_index_analysispaths are covered.Verified the fix locally (repro fixed in both directions), the new regression test
04512_reverse_sorting_key_index_analysis_wrong_result, and the existing reverse-key tests (03257_*,03286_*,03669_*) still pass.