Repository navigation
Set skip index: route NULL-constant atoms to UNKNOWN so granules are not wrongly pruned - #111604
Conversation
Internal second-model review — adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 2 findings; bounded: 1 full pass + 1 fix round + 1 delta recheck).
Severity: ❌ blocker / Session id: cron:clickhouse-author-slot-1:20260723-091900 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-author-slot-1:20260723-091900 |
|
cc @al13n321 @shankar-iyer — could you review this? A function-produced NULL constant (e.g. |
|
Workflow [PR], commit [d100a52] Summary: ❌
AI ReviewSummaryThis PR fixes Final Verdict✅ No new findings. |
A NULL value reaching the set skip index (e.g. `toInt64OrNull('x')`,
`nullIf(1, 1)`, `CAST(NULL AS Nullable(Int64))`, or a nullable comparison
result) is wrapped by `__bitWrapperFunc`, which returns NULL for a NULL
input instead of a BoolMask. That NULL propagates through
`__bitBoolMaskAnd`/`__bitBoolMaskOr`, so a disjunction such as
`(v = 1 AND toInt64OrNull('x')) OR v < 50` evaluates to a NULL granule
mask and the granule is wrongly pruned, silently returning fewer rows
than the same query with `use_skip_indexes = 0`.
When the bit-wrapped atom is nullable, coalesce a NULL mask to
`UNKNOWN_FIELD` (0b11, "can be true or false") with `ifNull`. NULL is
mapped to UNKNOWN, the correct BoolMask meaning, so the granule is no
longer excluded. Unlike inspecting a folded constant column, this also
covers NULLs hidden behind functions that suppress constant folding
(`identity`, `materialize`, ...), without evaluating the atom during index
analysis.
03707_set_index_bad_get_null_bug asserted the old (incorrect) pruning: its
single row matches via the second disjunct, so the correct result is 1 row,
not 0. Its reference is updated to the corrected plan and gains count
assertions. New test 04627 covers the constant/identity/materialize/NOT
shapes across both granule-filtering paths
(`secondary_indices_enable_bulk_filtering` 0 and 1) and confirms legitimate
pruning is preserved.
Closes: ClickHouse#103103
Co-Authored-By: Claude Opus 4.8 <[email protected]>
d91fe67 to
a3e097d
Compare
Internal second-model review (maintenance round, ci-fix for 03707)Adjudication of Gate B findings (click to expand)This round widened the fix (see below) and updated the regression tests. Independent
Both regression tests were confirmed to FAIL on a pristine (pre-fix) build and PASS on the |
CI finish ledger - a3e097dThe earlier PR-caused
Session id: cron:our-pr-ci-monitor:20260723-213000 |
…const-unknown-103103
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 9/9 (100.00%) · Uncovered code |
58a7912
`in` and `equals` on a `Nullable` column return `Nullable(UInt8)`, so the set index evaluates `c IN (803)` to NULL on a stored NULL set element. ClickHouse#111604 wrapped every nullable bit-wrapped atom in `ifNull(mask, UNKNOWN_FIELD)` to stop a constant NULL atom from propagating NULL through `__bitBoolMaskAnd`/`Or` (ClickHouse#103103). That also turned the NULL of a NULL set element into "can be true", so every granule holding a NULL passed `IN`/`=` and nothing was pruned (26.6: 32/448 granules, 26.8 and master: 448/448 on the issue's data). Map a NULL atom mask to "can be false" only (0b10). The mask connectives are only and/or/not/indexHint, i.e. Kleene logic, where a value fixed with a NULL operand stays the same for any definite replacement of it; so a row that makes the condition TRUE also makes it TRUE with every NULL atom read as FALSE, and no granule holding a matching row can be pruned. For a plain atom this is exact (WHERE treats NULL as not passing); under an un-pushed NOT it stays conservative. The ClickHouse#103103 shapes stay fixed: `(x AND NULL) OR y` -> `(x AND false) OR y` = `y`. This also matches how KeyCondition already reads a NULL hint. Closes ClickHouse#123705 Co-Authored-By: Claude Opus 5.5 <[email protected]>
The backport of #111604 used `ColumnConstPtr` and the three-argument `ActionsDAG::addColumn(column, type, name)` overload, neither of which exists in 26.3. Build failed with: error: unknown type name 'ColumnConstPtr' error: no viable conversion from 'ColumnPtr' to 'ColumnConst' Rewrite the NULL-mask mapping to build a `ColumnWithTypeAndName` and call the single-argument `addColumn`, matching the adjacent `else` branch in the same function. Build log: https://s3.amazonaws.com/clickhouse-test-reports/PRs/130735/4dc280f0a6520269e1bc114481c6d319f80319d4/build_amd_debug/build_clickhouse/build_clickhouse.log
Backport #111604 to 26.3: Set skip index: route NULL-constant atoms to UNKNOWN so granules are not wrongly pruned
…o UNKNOWN so granules are not wrongly pruned
The backport of #111604 used `ColumnConstPtr` and the three-argument `ActionsDAG::addColumn(column, type, name)` overload, neither of which exists in 26.3. Build failed with: error: unknown type name 'ColumnConstPtr' error: no viable conversion from 'ColumnPtr' to 'ColumnConst' Rewrite the NULL-mask mapping to build a `ColumnWithTypeAndName` and call the single-argument `addColumn`, matching the adjacent `else` branch in the same function. Build log: https://s3.amazonaws.com/clickhouse-test-reports/PRs/130735/4dc280f0a6520269e1bc114481c6d319f80319d4/build_amd_debug/build_clickhouse/build_clickhouse.log
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed wrong results when a query has a
setskip index and a predicate of the shape(x AND <null-value>) OR y, where the NULL is produced by a function (e.g.toInt64OrNull('x'),nullIf(1, 1),CAST(NULL AS Nullable(Int64))). Such a value was wrapped by__bitWrapperFuncand propagatedNULLinstead of the "unknown" mask, so the set index could prune granules that actually match and the query returned fewer rows than it should.Description
Closes #103103
When building the set-index condition,
MergeTreeIndexConditionSet::traverseDAGwraps each integer atom with__bitWrapperFuncto turn a boolean into aBoolMask(the0b01/0b10/0b11"can be false / can be true / unknown" bits).A NULL value with an integer element type (a
Nullable(Int64)NULL fromtoInt64OrNull('x'),nullIf(1, 1),CAST(NULL AS Nullable(Int64)), or a nullable comparison result such asCAST(NULL, 'Nullable(String)') = 'VALUE1') passes the integer test and gets wrapped.__bitWrapperFuncuses the default NULL implementation, so a NULL input yields a NULL mask instead of the0b11unknown mask. That NULL then propagates through__bitBoolMaskAnd/__bitBoolMaskOr, the result column becomes NULL, and both granule-analysis paths readNULL & 1 == 0and prune granules that should match, producing fewer rows: the per-granulemayBeTrueOnGranuleand the bulkgetPossibleGranules(whoseonlyNull()guard only catches a whole all-NULL mask column, not a NULL mixed into a per-set-element mask).Fix: when the bit-wrapped atom is nullable, coalesce a NULL mask to
UNKNOWN_FIELD(0b11) withifNull. NULL is mapped to UNKNOWN, which is the correctBoolMaskmeaning, so theAND/OR/NOToperators and both granule paths are covered at the source. This handles the NULL regardless of whether it folds to a constant column, so it also covers values hidden behind functions that suppress constant folding (identity,materialize), and it does not evaluate the atom during index analysis. A bareNULLliteral and non-integer typed NULLs were already routed toUNKNOWN_FIELDand are unaffected.03707_set_index_bad_get_null_bugasserted the old (incorrect) pruning for exactly this shape: its single row matches via the second disjunct, so the correct result is 1 row, not 0; its reference is updated to the corrected plan and gains count assertions. New test04627_set_index_null_const_no_pruneasserts that the set index does not change the result for the affected predicate shapes (including theidentity/materializewrappers and aNOTform) across both granule-filtering paths, and that legitimate pruning is preserved.Version info
26.8.1.443-master(included in26.8and later)