Repository navigation
A NULL key value is not a definite match of a negated comparison - #118214
Conversation
`SELECT count()` answered from the implicit exact-count projection reads a
granule's key range instead of its rows. A NULL key value is analysed as the
`+inf` stand-in of the `NULLS LAST` order, and a comparison against a NULL is
NULL - so `WHERE` rejects the row - which the two-valued range algebra of
`KeyCondition::checkInHyperrectangle` cannot express: "no NULL row lies inside
the range" turns, under negation, into "every row lies outside it". A granule
of NULLs therefore looked like a definite match of `id != ''`, `id NOT IN ('')`
or `id NOT LIKE 'z%'`, and `count()` counted rows that `SELECT *` under the same
`WHERE` does not return. At default settings, on a `Nullable` primary key.
The whole analysis now gives up its `can_be_false` claim as soon as an atom
that answers NULL for a NULL argument reads a `Nullable` key column whose range
reaches the NULL stand-in. `IS NULL` and `IS NOT NULL` are excluded: they answer
true or false for a NULL as well, so the algebra describes them exactly, and the
exact count of `04318_sparse_pk_index_analysis_last_mark_null` keeps working.
`can_be_true`, and with it every pruning decision, is untouched.
Closes: #112641
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
|
Workflow [PR], commit [d9be209] Summary: ✅
AI ReviewSummaryThis PR fixes wrong Findings
Final VerdictLLVM Coverage ReportMeasured on commit d9be209.
Changed lines: Changed C/C++ lines covered: 44/45 (97.78%) · Uncovered code |
| { | ||
| switch (function) | ||
| { | ||
| case KeyCondition::RPNElement::FUNCTION_IN_RANGE: |
There was a problem hiding this comment.
atomIsNullForNullArgument is too coarse here. RPNElement::Function already conflates atom families with different NULL semantics:
FUNCTION_IN_SET/FUNCTION_NOT_IN_SETare used not only for ordinaryIN/NOT IN, but also forhas/notHasandnullIn/notNullIn.FUNCTION_IN_RANGEis also used forisNotDistinctFromwith a non-NULLconstant.
Those surfaces are two-valued on NULL, not NULL-producing. We already rely on that elsewhere: 01280_null_in shows nullIn(NULL, ...) / notNullIn(NULL, ...) return a non-NULL boolean, and 04510_not_has_function_explain checks notHas([NULL], x) on a nullable key.
With the current switch, any nullable-key range that reaches the NULL stand-in now forces can_be_false = true even for queries whose semantics stay exact on NULL rows, so _exact_count_projection / exact ranges stop working for cases like SELECT count() FROM ... WHERE notHas([1], k) or ... WHERE k NOT IN (...) SETTINGS transform_null_in = 1.
This needs per-atom NULL behavior recorded in RPNElement (or another way to distinguish these sources) instead of inferring it from the shared enum.
There was a problem hiding this comment.
This is suboptimal, but still correct.
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units334 translation units recompiled, 3182 s compile time in total, 334 of them have a recent master baseline. |
|
The wrong count reproduces on the released 26.8 LTS (v26.8.2.7-lts), not just master: CREATE TABLE t (id Nullable(String), s Nullable(String)) ENGINE = MergeTree ORDER BY (id, s) SETTINGS allow_nullable_key = 1;
INSERT INTO t VALUES ('s','s'), (NULL,'s1'), (NULL,NULL);
SELECT count() FROM t WHERE id != ''; -- 3 on 26.8.2.7, correct answer is 1so it may be worth a |
… a negated comparison
Backport #118214 to 26.8: A NULL key value is not a definite match of a negated comparison
Cherry pick #118214 to 26.3: A NULL key value is not a definite match of a negated comparison
… a negated comparison
Cherry pick #118214 to 26.6: A NULL key value is not a definite match of a negated comparison
… a negated comparison
Cherry pick #118214 to 26.7: A NULL key value is not a definite match of a negated comparison
… a negated comparison
Backport #118214 to 26.6: A NULL key value is not a definite match of a negated comparison
Backport #118214 to 26.7: A NULL key value is not a definite match of a negated comparison
Backport #118214 to 26.3: A NULL key value is not a definite match of a negated comparison
…ight PK analysis With `use_lightweight_primary_key_index_analysis = 1` (the default), a key column that is neither held by the part's in-memory primary index (it follows a unique key prefix, see `primary_key_ratio_of_unique_prefix_values_to_skip_suffix_columns`) nor bounded by the partition minmax is not tracked by the sparse analysis. A set atom over such a column still evaluates it as `[-inf, +inf]`, so `b NOT IN (NULL)` / `b NOT IN (<empty subquery>)` / `NOT (b IN ...)` answers `(true, false)`. The NULL safeguard `mayReadNullKeyValue` (ClickHouse#118214) skipped every untracked column, so the granule was reported as an exact match and the exact-count projection counted rows whose `b` is NULL, which `WHERE` rejects: CREATE TABLE t (a UInt64, b Nullable(UInt64)) ENGINE = MergeTree ORDER BY (a, b) SETTINGS index_granularity = 1, allow_nullable_key = 1; INSERT INTO t SELECT number, if(number % 7 = 0, NULL, number) FROM numbers(100); SELECT count() FROM t WHERE a >= 10 AND b NOT IN (NULL); -- 90, must be 77 The non-sparse analysis analyses an unloaded column as the whole universe with its real type, so its safeguard fires and the result is correct. The sparse safeguard now checks an untracked key column as the whole universe with its real type, taken from the full primary key types that the caller passes down. Range atoms are skipped there: they answer `(true, true)` for an untracked column, which claims nothing, so e.g. `a >= 10 OR b < 5` keeps its exact count. Pruning (`can_be_true`) is unchanged. Co-Authored-By: Claude Opus 5.5 <[email protected]>
SELECT count()answered from the implicit exact-count projection reads a granule's key range instead of its rows. A NULL key value is analysed as the+infstand-in of theNULLS LASTorder, and a comparison against a NULL is NULL - soWHERErejects the row - which the two-valued range algebra ofKeyCondition::checkInHyperrectanglecannot express: "no NULL row lies inside the range" turns, under negation, into "every row lies outside it". A granule of NULLs therefore looked like a definite match ofid != '',id NOT IN ('')orid NOT LIKE 'z%', andcount()counted rows thatSELECT *under the sameWHEREdoes not return - at default settings, on aNullableprimary key:The whole analysis now gives up its
can_be_falseclaim as soon as an atom that answers NULL for a NULL argument reads aNullablekey column whose range reaches the NULL stand-in.IS NULLandIS NOT NULLare excluded: they answer true or false for a NULL as well, so the algebra describes them exactly, and the exact count that04318_sparse_pk_index_analysis_last_mark_nullpins keeps working.can_be_true, and with it every pruning decision, is untouched.Closes: #112641
Related: #112242
Related: #115152
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes to CHANGELOG.md):
Fixed
SELECT count()returning too many rows for a negated comparison (!=,NOT IN,NOT LIKE) over aNullableprimary key column: the exact-count optimization counted the NULL-key rows that theWHEREcondition rejects.Workflow [PR]
Sync PR [sync-upstream/pr/118214]
Version info
26.8.3.45,26.7.7.41,26.6.5.67,26.3.33.27