Repository navigation
Repair an inverted key boundary pair holding a nested NULL - #118945
Conversation
A boundary mark of a `Tuple` key column can hold a `NULL` inside the tuple (`Tuple(Nullable(Float64), Int32)` with `allow_nullable_key = 1`). The key stores NULLs last, while `Field` order puts `Null` below every value, so where a granule spans the boundary between non-`NULL` and `NULL` values the mark that follows compared *below* the mark that precedes it. The granule looked empty and primary key analysis dropped it - together with the matching rows it held: `SELECT * FROM tn WHERE t >= (5., 3)` returned nothing while two rows match, with `EXPLAIN indexes = 1` reporting `Granules: 0/2`. Only such an inverted pair is replaced, by the extremes of its own sides, which claim nothing about that column. A pair that is still ordered keeps its exact bounds, because for it `Field` order and the key's order agree on everything the range algebra asks - a table whose every key value holds a `NULL` in one element keeps its granule count. A flat `Nullable` column is untouched: its `NULL` bound is already mapped to `+inf`, which is where the key stores it. In the sparse key representation the replacement also clears `equal_boundaries_mask` for the column, because a replaced pair no longer stands for the equal boundaries the mask reports.
|
Workflow [PR], commit [ab9da1b] Summary: ✅
AI ReviewSummaryThis PR fixes the false-negative pruning that happened when a primary-key Findings❌ Blockers
Final Verdict
LLVM Coverage ReportMeasured on commit ab9da1b.
Changed lines: Changed C/C++ lines covered: 75/83 (90.36%) · Uncovered code |
The `t <= (5., 3)` count is answered by the implicit projection, which claims a granule whose bound holds a NULL nested in the tuple matches wholly - a separate defect of the range algebra, fixed by the `KeyCondition` change in #118944, not by the boundary repair here. Turn the projection off for that one query so the test witnesses only what this change fixes.
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units337 translation units recompiled, 3351 s compile time in total, 337 of them have a recent master baseline. |
The debug-only cross-check after the binary search asserts that a condition describing one continuous key range analyses every interior granule as wholly matching. A repaired boundary pair breaks that by construction: the granule between two marks that `Field` order cannot compare is analysed as the whole universe, so it may hold rows the filter rejects. That is the intended outcome - the pair claims nothing about the column, which can only widen `can_be_true`, and the mark range the binary search returns stays sound - but the assertion has no way to know, and the AST fuzzer found it on a `Tuple(Nullable(...))` key. Only the exactness of the analysis is affected. A release build already drops the exact range in the same situation, and the exact range is dropped here too; the check is simply skipped for a part whose boundaries had to be replaced.
…lement-bounds-117820
…d `NULL` The review asked whether the partition-minmax bound of the same key column can arrive at `KeyCondition` inverted as `[non-NULL, nested-NULL]` the way a per-mark pair can. It cannot: `ColumnTuple::getExtremes` takes each element's extremes with the NULLs filtered out (all-NULL gives `+inf`), so the bound is ordered and contains every value without a NULL - it prunes nothing a comparison matches. The test now pins that for a key column that also feeds the partition key, both when the column is loaded in the in-memory index and when it is a dropped suffix column whose partition-minmax bound is the only information the analysis has. The test is also renumbered: master gained other `05162_` tests meanwhile. Co-Authored-By: Claude Fable 5.1 <[email protected]>
…lement-bounds-117820
|
🕵 @groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=118945&sha=3ce4d8c45c7d9d989edb21cb26cfcb3f00d943bf&name_0=PR&name_1=Stress%20test%20%28amd_tsan%29 ( |
|
Investigated. Same root cause as #119814 (yours, open), so there is no new PR from me. The abort on SELECT k, sum(v) FROM remote('127.0.0.{2,3}', currentDatabase(), t_shuffle_bucket_order) GROUP BY k FORMAT Null
SETTINGS distributed_plan_force_shuffle_aggregation = 1;Its recorded settings carry That is what #119814 fixes, and its test change lands on this exact file: it adds Fix-presence check on the failing head: #119814's tip |
Address review: a boundary pair that stays ordered in `Field` order can still exclude rows the key stores inside the granule. For `Tuple(UInt8, Nullable(UInt8))` the granule `(1, 5)` .. `(2, NULL)` holds `(2, 3)`, but in `Field` order `(2, 3)` is above `(2, NULL)`, so `t = (2, 3)` pruned the granule. A nested `NULL` is stored above every value of its element, while `Field` order puts it below them, so only the upper bound in value space comes out too low; a lower bound holding such a `NULL` only widens the range. `KeyCondition::checkInHyperrectangle` now widens such an upper bound to `+inf` for `Range`-based atoms and never claims the granule matches wholly. Sets are compared in the key's own order (`IColumn::compareAt` with NULLs last), so they keep the exact bounds and `03733_has_function_key_condition_explain` keeps its granule count. The inverted-pair replacement in `markRangesFromPKRange` stays for a granule whose range would otherwise look empty before any comparison, and the debug continuity check is skipped whenever an upper bound holds a nested `NULL`. The test covers a `NULL` in a later tuple element and a descending key column. Co-Authored-By: Claude Opus 5.5 <[email protected]>
| /// rows the key stores under it: `(2, 3)` is above `(2, NULL)`. The upper bound is widened to | ||
| /// `+inf`, which claims nothing about the column. A lower bound holding such a NULL only | ||
| /// comes out lower and needs no widening. | ||
| const bool upper_bound_widened = fieldHasNullInside(key_range_ptr->right); |
There was a problem hiding this comment.
This fixes the false-negative pruning path for mark bounds, but the exact-range side is still blind to nested NULLs that arrive through a getExtremes()-derived tuple bound.
mayReadNullKeyValue() still decides solely from isNullableOrLowCardinalityNullable(key_type), so a sparse / partition-minmax-only key Tuple(Nullable(Float64), Int32) can still be analyzed through a bound like [(+inf, 2), (+inf, 5)] when the first element is all NULL (ColumnNullable::getExtremes() does exactly that). On a count() query, a predicate such as t >= (0., 0) can then still come back with can_be_false = false even though every row actually evaluates to NULL and must be filtered out.
That flows into exact_ranges / trivial count optimization, so we can still over-count without reading rows. Please extend the hidden-NULL exactness guard to tuple/minmax bounds too, and add a regression for the dropped-suffix / partition-minmax path.
A boundary mark of a
Tuplekey column can hold aNULLinside the tuple (Tuple(Nullable(Float64), Int32)withallow_nullable_key = 1). The key stores NULLs last, whileFieldorder putsNullbelow every value, so the mark that follows a run of non-NULLrows compared below the mark that precedes it. The granule between the two looked empty, and primary-key analysis dropped it - together with the matching rows it held.SELECT * FROM tn WHERE t >= (5., 3)returned nothing while two rows match, withEXPLAIN indexes = 1showingParts: 0/1, Granules: 0/2. A flatNullablekey is exact here: itsNULLbound is mapped to+inf, which is where the key stores it.A nested
NULLputs a bound below where the key stores it inFieldorder. For the lower bound that only widens the range. For the upper bound it does not, even when the pair stays ordered: inTuple(UInt8, Nullable(UInt8))the granule(1, 5)..(2, NULL)holds(2, 3), butFieldorder puts(2, 3)above(2, NULL). SoKeyCondition::checkInHyperrectanglewidens an upper bound holding a nestedNULLto+infforRange-based atoms, and never claims that such a granule matches wholly. Sets are compared in the key's own order (IColumn::compareAtwith NULLs last), so they keep the exact bounds and pruning is unaffected:03733_has_function_key_condition_explainkeeps its granule count. A flatNullablecolumn is untouched and keeps its exact+infmapping.Where a granule spans the boundary between non-
NULLandNULLvalues, the pair comes out inverted, and the range would look empty before any atom is evaluated.markRangesFromPKRangereplaces such a pair with the extremes of its own sides. In the sparse key representation this also clearsequal_boundaries_maskfor the column. The debug-only exact-continuity check is skipped when an upper bound holds a nestedNULL.Closes: #117820
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed rows silently missing from a query over a table whose primary key is a
Tuplewith aNullableelement holdingNULL: primary-key analysis dropped whole granules of matching rows.Workflow [PR]
Sync PR [sync-upstream/pr/118945]
Version info
26.10.1.1349-master(included in26.10and later)