Repository navigation
Do not build a key range from a NULL or a NaN nested in a Tuple - #118944
Conversation
`Field::isNull` and `Field::isNaN` only look at the top level of a `Field`, so a `NULL` or a `NaN` nested in a `Tuple` slips past every check built on them. A whole-tuple comparison carries exactly that, and primary key analysis treated such a value as an ordinary point of key order. On the constant side that produced a real key range. At row level `t <= (nan, 3)` is false for every row, but in key order `NaN` sorts above every ordinary value, so every granule was judged wholly inside `(-Inf, (nan, 3)]` and the exact-count projection counted its rows without evaluating the filter. Once a key transform maps the constant into key space (`ORDER BY toString(t)`), a nested `NULL` does the same: `'(NULL,1)' = '(NULL,1)'` matches in key space while `t = (NULL, 1)` is `NULL` for every row. Such a constant no longer produces an atom, so the granules are read and the filter decides. On the bound side a granule whose bound holds a nested `NaN` or `NULL` can never be proven wholly inside a comparison range - `WHERE` rejects a row whose comparison is `NULL` just as it rejects a false one - so only the containment claim is dropped. `intersects` is left as computed, which keeps every pruning decision the over-approximation the two-valued range algebra requires.
|
Workflow [PR], commit [13c3a58] Summary: ✅
AI ReviewSummaryThis PR hardens Missing context / blind spots
Final Verdict✅ No new findings. |
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
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. The official master build is compiled with Compile time of recompiled translation units341 translation units recompiled, 3520 s compile time in total, 341 of them have a recent master baseline. |
|
The one red check, The cause is the text index's virtual-column substitution on a CREATE TABLE tab (id UInt32, str Nullable(String), INDEX idx(str) TYPE text(tokenizer = 'splitByNonAlpha')) ENGINE = MergeTree ORDER BY id;
INSERT INTO tab VALUES (1,'hello world'),(2,NULL),(3,'foo bar'),(4,NULL),(5,'hello foo');
SELECT groupArray(id) FROM (SELECT id FROM tab WHERE NOT hasToken(str, 'foo') ORDER BY id);
-- [1,2,4]
SELECT groupArray(id) FROM (SELECT id FROM tab WHERE NOT hasToken(str, 'foo') ORDER BY id SETTINGS use_skip_indexes = 0);
-- [1] <- correct: hasToken(NULL, 'foo') is NULL, so NOT of it is NULL and `WHERE` rejects the row
|
…-null-118687 # Conflicts: # src/Storages/MergeTree/KeyCondition.cpp
…IN` on a nested `NULL` or `NaN` The key-bound check walks `Field`s read from stored key data, so a recursive descent would map the nesting depth of a value to native stack depth. Reuse `anyFieldSatisfies`, whose explicit worklist keeps it off the stack, with a non-capturing predicate; the constant side and the bound side ask the same question, so the two helpers collapse into one `hasNullOrNaNInside`. The set-index path built by `tryPrepareSetIndexForIn` was suspected of the same wrong-results hole through `IN` / `NOT IN`. It is not: both the set index and the row-level `IN` compare whole tuples by hash, so a nested `NULL` or a `NaN` matches its identical copy on both sides, and an unpacked set literal drops the element whose top-level `Nullable` column is `NULL` on both sides as well. Pin that `count()` agrees with the real read for a literal set, a subquery set, a key transform and partition pruning. Co-Authored-By: Claude Fable 5.1 <[email protected]>
|
🕵 @groeneai, the one red check, AST fuzzer (amd_release, oracle), is the pre-existing text-index bug analysed above ( |
|
Checked: #113332 does cover the I measured it at today's master rather than quoting an older run. Two Debug binaries from one tree: master
It repairs the substitution rather than declining it, which is the interesting part relative to the
One practical note: #113332 has had no new commit since 2026-09-03 and now conflicts with master. The conflict is a single hunk in My earlier fix for this, #107078, was closed as replaced by #113332, so I am not opening a competing PR on my own initiative. If you would rather have the wrong result off master without waiting for the rest of that branch, I can put up a separate PR carrying only the null restoration at the substitution site, credited to #113332, plus the regression test. Your call. |
…-null-118687 # Conflicts: # src/Storages/MergeTree/KeyCondition.cpp
…h ordinary bounds A granule of `Tuple(Int32, Nullable(Int32))` values `(2, 1), (2, NULL), (3, 0)` has the bounds `[(2, 1), (3, 0)]`, so the bound check in `checkInHyperrectangle` does not fire, while the middle row compares as `NULL`. `rangeOfKeyColumnMayHoldNull` now also reports a composite key column (`Tuple`, `Array`, `Map`) whose type may carry a `Nullable` or a floating-point value below its top level, so `mayReadNullKeyValue` drops `can_be_false` for it in both the dense and the sparse path. Pruning is unaffected. The `KeyCondition.cpp` part of this change went into the preceding merge commit by mistake; this commit adds the tests. Without the fix `count()` returns 3 for the `NULL` case while the real read returns 2. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…an claim it The type-based guard added for a `NULL` or a `NaN` hiding inside a granule of a composite key dropped `can_be_false` for every atom over such a key column, so an upper-bounded comparison like `t <= (1000., 0)` over a `Tuple(Float64, Int32)` key lost exact counting. That broke the control in `05136_117819_exact_count_nan_in_tuple_key`, which pins that only right-unbounded range atoms are affected. Key order puts a value with a nested `NULL` or `NaN` above every value sharing its prefix, and at row level the comparison against an ordinary constant is then greater, false, or `NULL`, all of which `WHERE` rejects for an upper-bounded range. So only a range atom that reaches `+inf` (or a negated range, a set, a polygon) can claim such a value as matching; restrict the nested check to those atoms. Pin the upper-bounded cases in `05161_pk_tuple_nested_nan_null`. CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=118944&sha=abadb08e492d5aa9f873191cbe350c63a0b85672&name_0=PR PR: #118944 Co-Authored-By: Claude Opus 5.5 <[email protected]>
…es a `NULL` `canConstantBeWrappedByDeterministicFunctions` marked an atom exact when the key transform is injective, but a transform like `toString` maps a `Tuple` value with a nested `NULL`, e.g. `(NULL, 1)`, to an ordinary key value `'(NULL,1)'`. The row-level comparison of such a value is `NULL`, which `WHERE` rejects, while the index compares the key as unequal to the constant, so a negated atom (`t != (2, 1)`) claimed the granule as wholly matching and `count()` counted the row. `mayReadNullKeyValue` could not see it, because the key type is a plain `String`. Now the atom is not exact when the column the transform consumes may hold a `NULL` that the key type does not reveal: a nested `NULL`, or a top-level `NULL` mapped to a non-`Nullable` key. Only `can_be_false` is affected, pruning is unchanged. #118944 Co-Authored-By: Claude Opus 5.5 <[email protected]>
…le`-only and range-only `Array` and `Map` comparisons are `compareAt`-based, so they never hide a nested `NULL` or `NaN` from the index. Set atoms compare whole tuples by hash on both sides, so `IN` / `NOT IN` keep their exact `can_be_false`. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…` or `NaN` `deterministicTransformMayHideNull` and `hasNullOrNaNInside` walked every nested type and value, so an `Array(Nullable(T))` or a `Map` key lost exact atoms (and the exact-count fast path) for no correctness reason: their comparison is `compareAt`-based and answers a definite value for a nested `NULL`. Both now descend only through `Tuple`s, matching `keyTypeMayHoldNestedNullOrNaN`. Add test cases for `Array` and `Map` keys. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ed `NULL` `<=>` compares a `Tuple` element-wise in the null-safe way, so `t <=> (NULL, 1)` is true for the row `(NULL, 1)`, and key order places a nested `NULL` the same way for the key and the constant. The new early return for a `NULL` nested in the constant and the nested-`NULL` exactness downgrade of a deterministic key transform are about comparisons that answer `NULL`, which `<=>` never does, so skip both for it. A nested `NaN` is still rejected: `(nan, 1) <=> (nan, 1)` is false. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…-null-118687 # Conflicts: # src/Storages/MergeTree/KeyCondition.cpp
…tinctFrom` exception `anyFieldSatisfies` also descends `Array` and `Map`, where a nested `NaN` compares as an ordinary value (`[nan] <=> [nan]` is true). Use a `Tuple`-only walk, shared with `hasNullOrNaNInside`, and add `<=>` cases on `Array(Float64)` and `Tuple(Array(Float64), UInt8)` keys to `05161_pk_tuple_nested_nan_null`. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
🕵 @groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=118944&sha=e0ccbaa4f6ab491cf01dfae3250d3e4af8faa1f4&name_0=PR&name_1=Stateless%20tests%20%28arm_asan_ubsan%2C%20azure%2C%20sequential%29 (the job hit its 9000 s timeout at test 1165 of 1180 with every test passing so far) and provide a fix in a separate PR. If the fix is already in progress, link it here. The other two red tests in that run ( |
|
The fix is already in progress: #123849 splits Your reading of the run at e0ccbaa is right: |
…le-nullable-element ClickHouse#118944 already fixes the exact count over a `Tuple` key with a `Nullable` element: `KeyCondition::mayReadNullKeyValue` now covers a `NULL` nested in a `Tuple` key column through `keyTypeMayHoldNestedNullOrNaN`. Take master's `KeyCondition` and `MergeTreeDataSelectExecutor` and keep the test as a regression test for ClickHouse#124155: it fails before that change and passes after it.
`checkInHyperrectangle` asks `mayReadNullKeyValue` whether a wholly matching range may hold a NULL, and since ClickHouse#118944 that calls `keyTypeMayHoldNestedNullOrNaN` for every such range of a negated range atom (e.g. `WHERE c2` over a `UInt8` key column, analysed as `c2 NOT IN [0, 0]`). The helper unwrapped the key type with `removeLowCardinality` / `removeNullable`, which return `DataTypePtr` by value, so every call made shared_ptr copies of the table's key type objects. All analysis threads share those objects, so the reference-count cache line bounced between cores: `perf` puts 99.6% of the helper's samples on its `lock incq` / `lock xadd` instructions, and on the query of ClickHouse#131715 primary key analysis takes 20.3 s of CPU at 8 threads instead of 7.3 s. The answer depends only on the type, so unwrap through references and raw pointers instead, with the same checks in the same order. At one thread the copies were cheap and the time is unchanged. Add a performance test with many wholly matching ranges in parts analysed in parallel, and three cases to 05161 that each fail when one of the helper's unwrap steps is dropped (a NULL in an inner Tuple, under LowCardinality(Nullable), below a Nullable(Tuple) key). The float check is not covered: the condition already relaxes those atoms. Closes ClickHouse#131715 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Field::isNullandField::isNaNonly look at the top level of aField, so aNULLor aNaNnested in aTupleslips past every check built on them. A whole-tuple comparison carries exactly that, and primary-key analysis then treats such a value as an ordinary point of key order:NaNproduced a real key range. At row levelt <= (nan, 3)is false for every row, but in key orderNaNsorts above every ordinary value, so every granule was judged wholly inside(-Inf, (nan, 3)]and the exact-count projection counted its rows without evaluating the filter:count()answered 8 whereSELECT *returned nothing. Once a key transform maps the constant into key space (ORDER BY toString(t)), a nestedNULLdoes the same:'(NULL,1)' = '(NULL,1)'matches in key space whilet = (NULL, 1)isNULLfor every row.NaNnested in a granule bound is the mirror case: the granule holding it can never be proven wholly inside a comparison range, because the row-level comparison is false there while key order gives the value a definite position.count()answered 6 where the same predicate returned 2 rows.A constant that holds a nested
NULLorNaNno longer produces an atom, so the granules are read and the filter decides. On the bound side only the containment claim is dropped -intersectsis left as computed, so every pruning decision stays the over-approximation the two-valued range algebra requires. The bound side covers a nestedNULLas well as a nestedNaN:WHERErejects a row whose comparison isNULLjust as it rejects a false one, so a granule whose bound holds one cannot be claimed to match wholly either - the same reasoningmayReadNullKeyValuealready applies to a flatNullablekey column.Closes: #118687
Closes: #118837
Closes: #117819
Related: #117501
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed
count()counting rows that the filter rejects when a whole-tuple comparison against aTupleprimary key involves aNULLor aNaNnested in the constant, or aNaNnested in the key data.Workflow [PR]
Sync PR [sync-upstream/pr/118944]
Version info
26.10.1.2174-master(included in26.10and later)