Repository navigation
Conversation
`SELECT count()` answered from the implicit exact-count projection asks
KeyCondition for exactly matching mark ranges. A `Nullable` key column
analyses a NULL as the `+inf` stand-in of the `NULLS LAST` order, and an
ordinary comparison is NULL - so `WHERE` drops the row - for such a key,
which the two-valued range algebra cannot express. checkInHyperrectangle
therefore forces `can_be_false` on any atom that answers NULL for a NULL
argument, and excludes `IS NULL` / `IS NOT NULL`, which answer definitely.
A set-membership atom that compares a NULL as an ordinary element answers
definitely too, and deserves the same exclusion. `transform_null_in`
rewrites `x IN (subquery)` to `nullIn`, where `nullIn(NULL, {NULL})` is 1
and `nullIn(5, {NULL})` is 0; `has` needs no rewrite at all, and
`has([NULL], NULL)` is 1 while `has([3], NULL)` is 0. Neither got an
exclusion, because the RPN maps `in`, `nullIn` and `has` onto the same
FUNCTION_IN_SET, so the safeguard could not tell them apart. The set index
already returned the correct exact verdict for the NULL tail; the safeguard
then overrode it, one mark was trimmed that did not need to be, and
matchesExactContinuousRange's claim no longer matched the ranges actually
produced - reported as `Inconsistent KeyCondition behavior` and, on a debug
or sanitizer build, an abort that takes the server down mid-job. In a
release build the exact range is dropped and the optimization declines.
Carry the distinction on the RPN element instead of widening the function
enum, and skip those atoms in both the dense and the sparse
mayReadNullKeyValue. Only `can_be_false` is affected, so no pruning
decision and no query result changes for any other atom.
Exclude them only over a bare key. A set atom also carries the key's
monotonic-function chain, and applyMonotonicFunctionsChainToRange does not
apply that chain to an infinite bound, because the -Inf/+Inf indicator is
itself a `Null`-typed Field. On an all-NULL granule the algebra therefore
compares the set element against untransformed bounds, and so cannot see
that the wrapper redefines the predicate for a NULL row - or refuses it, as
a `CAST` to a non-Nullable type does. Exempting such an atom would let the
projection answer a count for a query that raises
CANNOT_INSERT_NULL_IN_ORDINARY_COLUMN when the rows are read, making the
answer depend on whether the projection was used. Any chain keeps the
safeguard, with no attempt to classify which functions throw:
monotonicChainSupportsNullAtom already resolves the same hazard the same
way for the `IS NULL` atom, and names this `CAST` case in its comment. A
null set index falls through to the switch, so the exclusion fails closed.
Reproduced on a debug build as a deterministic abort on both index
representations: `nullIn` on an ascending, a descending and a
`LowCardinality(Nullable)` key, `globalNullIn`, and `has` over an array
holding a NULL on an ascending, a descending and a
`LowCardinality(Nullable)` key. Every one is silent after the change, and
the counts of the ordinary three-valued `in` and `notIn`, of `notHas`, and
of `has` over an array without a NULL are unchanged. A wrapped key still
raises rather than being answered from the projection, on both index
representations, for a tuple-literal set and for `has`. The regression test
pins `use_lightweight_primary_key_index_analysis` to 0 and to 1 for every
null-set carrier it covers, each against a truth arm that reads the rows, so
neither mirror of mayReadNullKeyValue can regress unobserved; two of its arms
assert through `EXPLAIN projections = 1` that the exact-count projection was
actually used, one per representation, which is what fails without the change
in a release build, where the debug-only consistency check is compiled out.
Co-Authored-By: Claude Opus 5 <[email protected]>
(cherry picked from commit 3c869f046547cc5b15a602e0ddf51890cdc08ecc)
The three FUNCTION_NOT_IN_SET builders the previous commit flags
(`notNullIn`, `notHas`, `globalNotNullIn`) had no arm that reddens when
their flag is removed: every negated arm in the test uses a set that holds
a NULL, and the exemption is not load-bearing there. An all-NULL granule
matches a NULL set element, so the set atom already answers
`{true, false}`, `NOT` flips it, `can_be_true` is false, the granule is
pruned, and the safeguard's `!can_be_false` precondition never holds.
Assert on a set that holds no NULL instead. Over a bare all-NULL key
`nullIn(NULL, {0})` is definitely false, so the negation is definitely true
and every granule is exact; without the exemption nothing is exact,
`exact_count` stays 0 and the projection is declined outright. The arm
reads the projection name out of `EXPLAIN projections = 1` rather than the
`Exact count optimization is applied` description, because a fully exact
read replaces the ReadFromMergeTree node with ReadFromPreparedSource and
that description is only rendered for a partially exact read.
Measured on a debug build: the arm answers 1 on both index
representations with the change, 0 on master, and 0 when only the
`notNullIn` flag statement is removed, which is what ties it to that
builder. 50 randomized repetitions pass, so no setting neuters it.
Co-Authored-By: Claude Opus 5 <[email protected]>
Drop the duplicated reason for the bare-key restriction from the mayReadNullKeyValue doc: the same property is stated at the guard that uses it, a few lines above.
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 2 full passes plus 1 delta recheck over
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-10:20260919-203700 |
|
Workflow [PR], commit [e1c22c8] Summary: ❌
AI ReviewSummaryThis PR fixes the exact-count projection analysis for null-aware set-membership predicates on Final Verdict
|
CI finish ledger - 4db7eb9Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
Session id: cron:our-pr-ci-monitor:20260920-110135 |
`globalNullIn` and `globalNotNullIn` are separate `atom_map` entries from `nullIn` and `notNullIn`, so the exemption is written by four builders and the existing arms could not see a regression in the global pair: with `null_is_ordinary_value` dropped from the `globalNullIn` builder the file still passed 3/3, and the same held for `globalNotNullIn`. One arm per spelling, over the two fixtures the file already creates, in the shapes that were measured to flip. With the `globalNullIn` builder mutated the positive arm raises `Inconsistent KeyCondition behavior` from `markRangesFromPKRange`; with the `globalNotNullIn` builder mutated the negated arm's plan assertion goes 1 to 0 and no other line of the file changes. 20/20 green on the unmutated tree under randomized settings.
`null_is_ordinary_value` is written by six atom_map builders. The file's only `NOT has` arm applies `CAST(x, 'UInt64')` to the key, and `atomIsNullForNullArgument` skips the exemption once the set index carries a monotonic functions chain, so that arm answers the same way with and without the `notHas` flag statement: deleting that one statement left the file green 3/3. The new arm keeps the key bare and takes a constant array holding no NULL, so `NOT has` is definitely true for every NULL key value, every selected range stays exact and the implicit exact-count projection answers the count. Deleting `null_is_ordinary_value` from the `notHas` builder alone now flips exactly one line of the reference, 1 to 0, and no other line; unmutated, the file is 20/20 OK under randomized settings. `HasToInPass` does not rewrite this shape: it bails on a Nullable array element type and on a Nullable needle, so the atom reaches `KeyCondition` as `notHas` and is folded there through `inverse_relations`.
Build profile diff (arm_release)Commit See the job log for details. |
|
📊 Cloud Performance Report
These ClickBench slowdowns are very unlikely to come from this PR, which changes no aggregation code. The PR only changes how KeyCondition decides whether nullIn, has() and related set atoms on a bare Nullable key keep the exact-count projection exact, and ClickBench has no Nullable key columns. Thirteen GROUP BY-heavy ClickBench queries are about 13% to 53% slower with higher CPU time, three of the six source runs shared one host, and no TPC-H query moved. Nine flagged queries are downgraded to not sure; Q16, Q17, Q18 and Q32 (+44.8%, +27.5%, +45.5%, +53.1%) keep their regression verdicts because the deltas are too large to override, but a rerun on separate hosts should be done before acting on them. clickbench🔴 4 regressed · Flagged queries (14 of 43)
Change = percent below ×2; the ratio of medians (×N faster/slower) beyond, where percent understates the scale. q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on. tpch_adapted_1_official🟢 No significant changes Debug info
|
…on-null-set-exactness # Conflicts: # src/Storages/MergeTree/KeyCondition.cpp
CI finish ledger - e1c22c8Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
CIDB has no other Session id: cron:our-pr-ci-monitor:20261013-030009 |
|
@nihalzp maybe you can take a look? |
|
Dear @nihalzp, you haven't been active on this PR for 30 days. You will be unassigned. Will you continue working on it? If so, please feel free to reassign yourself. |
Related: #90461
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
...
Description
On a debug or sanitizer build,
SELECT count()over aNullableprimary key aborts withLogical error: 'Inconsistent KeyCondition behavior'when the filter's set holds a NULL:Reproducer
The #96844 carrier is the same shape at scale. Its
server.logshows the atom isnullInand thetable holds 48 NULL rows, written by two
divideOrNull(number, NULL)inserts that the report'sgenerated reproducer drops, which is why that paste answers
0instead of aborting.It takes the server down mid-job, from
markRangesFromPKRange. Found byAST fuzzer (amd_debug, targeted)on #115919 @a8e3557f(report)
and on #96844 @
8c888f34(report).
Root cause.
count()from the implicit exact-count projection asks KeyCondition for exact markranges. An ordinary comparison is NULL for a NULL key value, so
checkInHyperrectangleforcescan_be_falseon every atom that answers NULL for one, exceptIS NULL/IS NOT NULL, which answerdefinitely. Set membership comparing a NULL as an ordinary element answers definitely too
(
nullIn(NULL, {NULL})is 1;hasalike). But the RPN mapsin,nullInandhasonto oneFUNCTION_IN_SET, so the safeguard could not tell them apart: it overrode the set index'salready-exact verdict, and the ranges stopped matching
matchesExactContinuousRange.Change. Carry the distinction on the RPN element and skip those atoms in both
mayReadNullKeyValueoverloads, but only over a bare key: a monotonic wrapper is not applied to theNULL stand-in bound, so for a wrapped key the algebra cannot describe what the wrapper does to a NULL
row, and the safeguard stays.
can_be_trueis untouched, so pruning is unaffected and every countstays the one a plain read returns.
Validation. The abort is deterministic before and silent after for
nullIn,globalNullInandhasover ascending, descending andLowCardinality(Nullable)keys;in,notIn,notHasandNULL-free
hascounts are unchanged.05083_nullable_key_exact_countcommits thenullIn,has,descending, wrapped-key and negated-set arms on both index representations, plus
EXPLAIN projections = 1assertions that fail without the fix in a release build; the negated-set,globalandnotHasarms each flip on one builder's flag alone. The rest was measured only.Workflow [PR]
Sync PR [sync-upstream/pr/121089]