Repository navigation
Properly fix primary key index analysis for reverse sorting keys - #111059
Conversation
|
Workflow [PR], commit [39c2c1a] Summary: ✅
AI ReviewSummaryThis PR correctly threads reverse-key direction through planner-time Tests
Final Verdict
|
|
See also #110908 🙂 |
Add a debug `chassert` in `KeyCondition::checkInRange` verifying its contract: the boundary tuples must be physical boundaries in key order, left not after right, with reversed (`DESC`) columns compared inverted. The per-column boundary swap removed by the previous commit violated exactly this contract, so the check would have caught the bug at the API boundary. Add `04612_reverse_key_index_analysis_shapes` with reverse key shapes not covered by the earlier tests: a `DESC` first key column, two `DESC` columns, NULLs (stored physically first on a `DESC` column), a part without a final mark, a `DESC` key column not loaded in the in-memory index, a middle `DESC` column skipped by the sparse analysis, an explicit `PRIMARY KEY` clause, and granule-pruning effectiveness checks. Scenarios ported from #111059; the results match that implementation's reference output exactly. Related: #111059 Co-Authored-By: Claude Fable 5 <[email protected]>
| { | ||
| const auto & primary_key = metadata_snapshot->getPrimaryKey(); | ||
| KeyCondition dynamic_key_condition(filter_dag, context, primary_key.column_names, primary_key.expression); | ||
| KeyCondition dynamic_key_condition(filter_dag, context, primary_key); |
There was a problem hiding this comment.
This is the read-time JOIN runtime-filter pruning path that was still broken for reverse-sorted primary keys, but the new coverage never exercises it. 04612_reverse_key_index_analysis* only proves planner-time PK analysis and replicated-metadata rebuilds, so this call site can regress while the whole new suite still passes.
Please add a focused runtime-filter index-analysis case on a table with ORDER BY (... DESC ...) and compare enable_join_runtime_filters_index_analysis = 0 vs 1 results. The existing 04612_join_runtime_filters_index_analysis_multi_key_anti.sql harness already covers this feature on ascending keys and looks like the natural place to extend.
There was a problem hiding this comment.
This PR will be backported to older versions where this path does not exist.
@groeneai Create a PR with this missing test coverage once this PR is merged.
There was a problem hiding this comment.
Will do. Once this merges I will open a separate PR adding the missing test coverage on master and link it back here.
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 197/231 (85.28%) · Uncovered code |
Backport #111059 to 26.7: Properly fix primary key index analysis for reverse sorting keys
Add issue ClickHouse#111421 alexey-milovidov reverse-key case (ORDER BY (g, r DESC), g = toUInt256(5)) to the 04493 regression. The matchesExactContinuousRange fix is order-independent: a widening CAST leaves a non-empty function chain on the leading key, so it is demoted POINT -> RANGE regardless of any column sort direction. Verified this exact shape aborts in debug on master with ClickHouse#111059 without the fix and returns the native count (10) with it. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Follow-up to ClickHouse#111059 requested by nihalzp: the reverse-key primary-key index-analysis fix is backported to versions without the JOIN runtime-filter read-time pruning path (MergeTreeIndexReadResultPool), so that path is not covered by the backportable test 04612_reverse_key_index_analysis. This adds the missing coverage on master only. The enable_join_runtime_filters_index_analysis path builds a read-time KeyCondition from a join's collected probe keys and prunes the probe-side primary key. It must honor the key sort directions: before ClickHouse#111059 a DESC primary-key column was analyzed as ascending, which inverts the value interval and drops granules holding matching rows (on a set-index probe it aborts with Invalid binary search result in MergeTreeSetIndex). The test joins on reverse-sorted primary keys (DESC second, DESC leading, and a single DESC column) and checks that the result with the feature on equals the result with it off, and that pruning actually engaged (RuntimeFilterGranules Considered and Dropped both greater than zero) so the equality is not vacuous.
Cherry pick #111059 to 26.6: Properly fix primary key index analysis for reverse sorting keys
… reverse sorting keys
Backport #111059 to 26.6: Properly fix primary key index analysis for reverse sorting keys
Add issue ClickHouse#111421 alexey-milovidov reverse-key case (ORDER BY (g, r DESC), g = toUInt256(5)) to the 04493 regression. The matchesExactContinuousRange fix is order-independent: a widening CAST leaves a non-empty function chain on the leading key, so it is demoted POINT -> RANGE regardless of any column sort direction. Verified this exact shape aborts in debug on master with ClickHouse#111059 without the fix and returns the native count (10) with it. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Follow-up to ClickHouse#111059 requested by nihalzp: the reverse-key primary-key index-analysis fix is backported to versions without the JOIN runtime-filter read-time pruning path (MergeTreeIndexReadResultPool), so that path is not covered by the backportable test 04612_reverse_key_index_analysis. This adds the missing coverage on master only. The enable_join_runtime_filters_index_analysis path builds a read-time KeyCondition from a join's collected probe keys and prunes the probe-side primary key. It must honor the key sort directions: before ClickHouse#111059 a DESC primary-key column was analyzed as ascending, which inverts the value interval and drops granules holding matching rows (on a set-index probe it aborts with Invalid binary search result in MergeTreeSetIndex). The test joins on reverse-sorted primary keys (DESC second, DESC leading, and a single DESC column) and checks that the result with the feature on equals the result with it off, and that pruning actually engaged (RuntimeFilterGranules Considered and Dropped both greater than zero) so the equality is not vacuous.
…nalysis-fix Properly fix primary key index analysis for reverse sorting keys
Backport the KeyCondition/KeyOrder portion of ClickHouse/ClickHouse#111059. Add a KeyDescription-based constructor and direction-aware lexicographic range decomposition. Preserve the existing ascending-only constructor; do not change MergeTree or CHYT callers. commit_hash:0635f611128ee391a1e9db18bc5196de1130625f
Backport the KeyCondition/KeyOrder portion of ClickHouse/ClickHouse#111059. Add a KeyDescription-based constructor and direction-aware lexicographic range decomposition. Preserve the existing ascending-only constructor; do not change MergeTree or CHYT callers. commit_hash:0635f611128ee391a1e9db18bc5196de1130625f
Closes: #110275
Continuation of #110288.
The existing approach by swapping the left and right keys if a column is reversed is unsound, leading to incorrect hyperrectangles being created.
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix incorrect pruning from primary key index analysis on tables with a reverse (descending) sorting key (
ORDER BY (g, r DESC)). A granule spanning a change of a leading key column followed by a descending key column could be pruned incorrectly, dropping matching rows.Version info
26.7.2.15,26.6.2.122