Repository navigation
Do not prune a partition when a function of the partition key returns NULL for it - #124041
PedroTadim merged 4 commits into
Conversation
Partition pruning builds its KeyCondition with single_point = true, so any deterministic function of the key may enter the monotonic chain, including functions that return NULL for a non-NULL argument (moduloOrNull, positiveModuloOrNull, nullIf, ...). For such a partition applyMonotonicFunctionsChainToRange produced a range with a real NULL bound. A real NULL has no position in the Range order (accurateLess and accurateEquals are false against it), so both intersectsRange and containsRange returned true and the atom was reported always true for the partition. That was harmless while every range atom was NULL for a NULL argument. The isNotDistinctFrom atom (ClickHouse#110006) is a definite 0 for NULL <=> c and is not in inverse_relations, so NOT turned "always true" into "always false" and the partition was pruned although its rows match: CREATE TABLE t (x Int32) ENGINE = MergeTree PARTITION BY x ORDER BY tuple(); INSERT INTO t SELECT number FROM numbers(15); SELECT count() FROM t WHERE NOT (-5 <=> moduloOrNull(7, x)); -- 14, expected 15 Return {} (cannot analyze) when a chain function returns NULL for an applied bound, the same rule applyFunctionChainToColumn already applies to the constant side. All callers map {} to an unknown mask, so only the partition whose value maps to NULL loses its analysis. Found by the AST fuzzer TLP WHERE oracle on master: https://github.com/ClickHouse/ClickHouse/actions/runs/37295117105/job/111742340948 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 0 findings) plus my own cold review (2 nits).
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-10:20261005-142800 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-5:20261005-135000 |
|
Workflow [PR], commit [44b8c8e] Summary: ✅
AI ReviewSummaryThis PR fixes a real partition-pruning correctness bug in Final Verdict
|
Build profile diff (arm_release)Commit See the job log for details. |
# Conflicts: # src/Storages/MergeTree/KeyCondition.cpp
CI finish ledger - 234db3eEvery failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
All 152 check runs completed, Session id: cron:our-pr-ci-monitor:20261008-210014 |
… NULL for it Port of #124041, a prerequisite of #121154 on this branch: when a link of the monotonic chain turns a non-NULL bound into NULL, the bound has no position in the `Range` order, so the range is left unanalyzed instead of compared. Without it the `NOT (coalesce(nullIf(c0, 5), 0) = 1)` case of `05233_assume_not_null_nullable_partition_key_pruning` still prunes the partition of 5 (4 rows expected, 3 returned), as seen on the 26.9 backport. #124041 is written against the try/catch chain loop of #117355, which this branch does not have, so this applies only its check to the branch's loop. #130788 Co-Authored-By: Claude Opus 5.5 <[email protected]>
… NULL for it Port of #124041, a prerequisite of #121154 on this branch: when a link of the monotonic chain turns a non-NULL bound into NULL, the bound has no position in the `Range` order, so the range is left unanalyzed instead of compared. Without it the `NOT (coalesce(nullIf(c0, 5), 0) = 1)` case of `05233_assume_not_null_nullable_partition_key_pruning` still prunes the partition of 5 (4 rows expected, 3 returned), as seen on the 26.9 backport. #124041 is written against the try/catch chain loop of #117355, which this branch does not have, so this applies only its check to the branch's loop. #130788 Co-Authored-By: Claude Opus 5.5 <[email protected]>
… NULL for it Port of #124041, a prerequisite of #121154 on this branch: when a link of the monotonic chain turns a non-NULL bound into NULL, the bound has no position in the `Range` order, so the range is left unanalyzed instead of compared. Without it the `NOT (coalesce(nullIf(c0, 5), 0) = 1)` case of `05233_assume_not_null_nullable_partition_key_pruning` still prunes the partition of 5 (4 rows expected, 3 returned), as seen on the 26.9 backport. #124041 is written against the try/catch chain loop of #117355, which this branch does not have, so this applies only its check to the branch's loop. #130788 Co-Authored-By: Claude Opus 5.5 <[email protected]>
… NULL for it Port of #124041, a prerequisite of #121154 on this branch: when a link of the monotonic chain turns a non-NULL bound into NULL, the bound has no position in the `Range` order, so the range is left unanalyzed instead of compared. Without it the `NOT (coalesce(nullIf(c0, 5), 0) = 1)` case of `05233_assume_not_null_nullable_partition_key_pruning` still prunes the partition of 5 (4 rows expected, 3 returned), as seen on the 26.9 backport. #124041 is written against the try/catch chain loop of #117355, which this branch does not have, so this applies only its check to the branch's loop. #130788 Co-Authored-By: Claude Opus 5.5 <[email protected]>
… NULL for it Port of #124041, a prerequisite of #121154 on this branch: when a link of the monotonic chain turns a non-NULL bound into NULL, the bound has no position in the `Range` order, so the range is left unanalyzed instead of compared. Without it the `NOT (coalesce(nullIf(c0, 5), 0) = 1)` case of `05233_assume_not_null_nullable_partition_key_pruning` still prunes the partition of 5 (4 rows expected, 3 returned), as seen on the 26.9 backport. #124041 is written against the try/catch chain loop of #117355, which this branch does not have, so this applies only its check to the branch's loop. #130788 Co-Authored-By: Claude Opus 5.5 <[email protected]>
… NULL for it Port of #124041, a prerequisite of #121154 on this branch: when a link of the monotonic chain turns a non-NULL bound into NULL, the bound has no position in the `Range` order, so the range is left unanalyzed instead of compared. Without it the `NOT (coalesce(nullIf(c0, 5), 0) = 1)` case of `05233_assume_not_null_nullable_partition_key_pruning` still prunes the partition of 5 (4 rows expected, 3 returned), as seen on the 26.9 backport. #124041 is written against the try/catch chain loop of #117355, which this branch does not have, so this applies only its check to the branch's loop. #130788 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Related: #110006
Related: #121154
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed partition pruning skipping a partition whose rows match a filter like
NOT (c <=> f(partition_key))whenfreturnsNULLfor that partition's key value (for examplemoduloOrNullornullIf). Such queries returned fewer rows than withuse_partition_pruning = 0.Description
On 26.8.2.7 a partitioned table returns one row less than the same filter without partition pruning:
26.3.33.24 returns 15; 26.6.2.158, 26.7.6.57, 26.8.2.7 and master return 14. Found by the AST fuzzer on master: https://github.com/ClickHouse/ClickHouse/actions/runs/37295117105/job/111742340948
Root cause. Partition pruning analyzes a single point, so any deterministic key function enters the chain, including one returning
NULLfor a non-NULLargument. Forx = 0,applyMonotonicFunctionsChainToRangeturned the point into the range[NULL, NULL]. A realNULLhas no position in theRangeorder, so that range both intersects and is contained in[-5, -5]: the atom was reported always true. This was harmless while every atom wasNULLfor aNULLargument;isNotDistinctFrom(#110006) is a definite0there, soNOTturned "always true" into "always false" and the partition was skipped.Fix. When a chain function returns
NULLfor a bound, the range is reported as not analyzable, the ruleapplyFunctionChainToColumnalready applies to the constant side. Only the partition whose value maps toNULLloses pruning. The primary key, minmax and set-index analysis and the Iceberg, Delta Lake and Paimon partition pruners share this function.Validation. New stateless test: the
<=>,IS NOT DISTINCT FROM,nullIfandIS TRUEspellings against an unpartitioned copy, the pruned parts inEXPLAIN, and the fuzzer's TLP identity; red before, green after. #121154 covers the neighbouring case of a bound that already enters the loop as aNULLstand-in.Workflow [PR]
Sync PR [sync-upstream/pr/124041]
Version info
26.10.1.2009-master(included in26.10and later)