Repository navigation
Conversation
matchesExactContinuousRange gates the exact continuous-range (implicit count projection) optimization and classifies a key column as an exact POINT when its monotonic-function chain is strict. Comparing a key column against a wider-typed constant (e.g. a UInt64 key vs toUInt256(...)) inserts a widening CAST into that chain. The widening cast is genuinely strictly monotonic, so the column was marked POINT. But checkInRange applies the chain to the granule bounds via applyMonotonicFunctionsChainToRange, which forces the bounds closed. For a granule that straddles the key boundary, the closed-bound mapped range only partially matches, so checkInRange honestly reports can_be_false = true. That contradicts the POINT "exact continuous" promise, and the exact range computation in markRangesFromPKRange then finds an interior granule that can_be_false and throws the debug-only "Inconsistent KeyCondition behavior" logical error (the ClickHouse#90461 monotonicity canary, #ifndef NDEBUG). Found by the AST fuzzer mutating 04303_key_condition_ifnull_coalesce_boolean.sql (team_id = 1 -> team_id = toUInt256(1)) in Stress test (arm_debug) on PR ClickHouse#108100 (STID 5182-2f27). There is no behavior change in release builds (the branch is compiled out under NDEBUG); the count result was already correct, the wider-typed comparison just reads a couple more granules. Fix: only promote a key column to POINT when its monotonic-function chain is empty (a native key column). A non-empty chain, even a strict one, cannot promise exact continuity across boundary granules because the chain application forces closed bounds; treat such a column as a RANGE. This mirrors how the function already downgrades non-strict chains and only disables the exact-range fast path for the affected queries (pruning and results are unchanged). Related: ClickHouse#90461 Co-Authored-By: Claude Opus 4.8 <[email protected]>
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-worker-slot-3:20260701-061100 |
|
cc @KochetovNicolai @CurtizJ — could you review this? It fixes the debug-only |
|
Workflow [PR], commit [206a4a2] Summary: ✅
AI ReviewSummaryThis PR narrows Final VerdictStatus: ✅ Approve |
CI finish ledger — 1f4a77bEvery failure below has an owner. Only
Session id: cron:our-pr-ci-monitor:20260701-120000 |
|
@groeneai update branch |
…test Extend the regression test with qoega's ClickHouse#111421 trigger: a composite (x, y, z) key at index_granularity = 1 and a count() combining the wider-typed equality on the leading key (x = toUInt256(3)) with range conditions on the rest, over a multi-x dataset. This exercises the aggregate-projection exact-count path with a mark layout fine enough to disagree with the analyzer's continuous-range claim, which the existing single-key cases did not cover. Without the KeyCondition fix this aborts in debug builds with the Inconsistent KeyCondition behavior logical error; in release the broken exact range is dropped and read normally, so the count stays correct (3). Related: ClickHouse#111421 Co-Authored-By: Claude Opus 4.8 <[email protected]>
…dition-relaxed-exact-range
|
Branch updated (merged current master, CI re-running). The stale Upgrade-check red was the flaky #39174 background-mutation noise from the old base, unrelated to this change. I verified your #111421 repro against this fix on debug, release-emulation (throw disabled), and unfixed builds:
On the release count: it is not a wrong result. Your composite-key / |
The regression only exercises the exact-count projection path (where the debug abort fires) while optimize_use_projections and optimize_use_implicit_projections default on. If either default flips, an unfixed build reads normally and the test silently passes. Pin both settings before the count queries in each clickhouse-local block, matching sibling tests 04506 and 04508. Also assert the wide-typed comparison is actually routed through the exact-count projection with a discriminating SELECT count() > 0 FROM (EXPLAIN ...) WHERE explain ILIKE '%_exact_count_projection%' in each block, so the test fails if the optimizer declines the affected mechanism instead of silently passing on the correct count. Verified the assertion returns 1 with the projection path live and 0 when implicit projections are disabled. Co-Authored-By: Claude Opus 4.8 <[email protected]>
A key column can carry both a native exact-point atom (x = 3) and a redundant widened-cast atom (x = toUInt256(3)) at once when the query is planned with the old analyzer, which does not fold them (the new analyzer does). In that case matchesExactContinuousRange pins the column to POINT via the native equality and keeps it POINT despite the co-present cast atom. This is safe: the native atom is a genuine exact point, and the equivalent widening cast can only report can_be_false on the same boundary granules, so after boundary trimming no interior granule is left non-exact. The exact-count path stays live and returns the correct native count without the debug Inconsistent KeyCondition abort. Add this case to 04493 (enable_analyzer = 0 to keep both atoms) so the interaction is guarded against regression. Also strengthen the existing wide-typed cases so they distinguish the fix from reverted code in release builds too, where the debug abort is compiled out: the fix demotes the leading key from POINT to RANGE, which forces generic-exclusion index search instead of binary search. Assert 'generic exclusion search' via EXPLAIN indexes = 1 for the wide-only equality and add a cast-wrapped IN case (toUInt256(team_id) IN (toUInt256(1))) that actually populates the set-index function chain, exercising the changed non-empty mapping.functions branch that a bare-key IN does not reach. Co-Authored-By: Claude Opus 4.8 <[email protected]>
The style check forbids describing the analyzer as "new" (it is enabled by default since 24.3). Reword the comment to "the analyzer".
CI finish ledger - 7e73034The PR-caused Style failure from the prior head (analyzer-wording rule in the 04493 test comment) is fixed on this head. The sole remaining failure is a pre-existing trunk flake, owned by an open fixing PR.
Session id: cron:our-pr-ci-monitor:20260723-113000 |
…the stress-test failure
…s key columns
`MergeTreeDataSelectExecutor::markRangesFromPKRange` throws the debug-only
`Inconsistent KeyCondition behavior` logical error when `matchesExactContinuousRange`
reports that the filter describes one continuous key range, yet the exact mark range
computed by the binary search still contains rows the filter rejects.
That check silently assumes the in-memory primary index resolves every key column the
filter references. A part can keep only a prefix of the key in memory - see the
`MergeTree` setting `primary_key_ratio_of_unique_prefix_values_to_skip_suffix_columns`
- and the key columns beyond that prefix are then analysed as constant coordinates
over the whole universe, or over the part's partition minmax bound, rather than from
per-mark values. A granule strictly inside the binary-search boundary can therefore
legitimately hold rows the filter rejects even though the condition itself really does
describe one continuous key range. The assertion fires on a plain
`WHERE a = 5 AND b = 3`, with no monotonic function and no reverse-sorted key column
involved:
CREATE TABLE t (a UInt16, b UInt16, c UInt16) ENGINE = MergeTree ORDER BY (a, b, c)
SETTINGS index_granularity = 4,
primary_key_ratio_of_unique_prefix_values_to_skip_suffix_columns = 0.01;
INSERT INTO t SELECT intDiv(number, 100), intDiv(number % 100, 10), number % 10 FROM numbers(1000);
SELECT count() FROM t WHERE a = 5 AND b = 3; -- SIGABRT on a debug build
Only `a` is kept in memory, so the binary search returns the marks for `a = 5`, and the
granules inside that boundary do contain rows with `b != 3`.
Restrict the check to the case where the in-memory index resolved the whole used key
prefix. It keeps firing for the widening-cast manifestation of ClickHouse#90461, which is what it
is actually catching in CI. Release builds are unaffected: the exact range is dropped
there already, and that is the correct outcome here too.
CI reports for this failure (`Logical error: Inconsistent KeyCondition behavior`,
STID 5182-2f27). The first one is the manifestation fixed here - its participating
conditions are `(column 0 in [5, 5]), (column 1 in [3, 3]), and, (column 0 in (-Inf, 100])`,
with no cast - while the other three are the widening-cast class already addressed by
ClickHouse#109023:
https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=b59441bd06c2fcb6b103a30874528cc398afc723&name_0=MasterCI&name_1=Stress%20test%20%28arm_debug%29&name_1=Stress%20test%20%28arm_debug%29
https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=859a9abe2597d0c2c26b898066a32a375ea6b62f&name_0=MasterCI&name_1=Stress%20test%20%28arm_debug%29&name_1=Stress%20test%20%28arm_debug%29
https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=10116111a8db9ece9c215d65d7afa38d9f02ebdf&name_0=MasterCI&name_1=Stress%20test%20%28arm_debug%29&name_1=Stress%20test%20%28arm_debug%29
https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=e7b490a6a1e7b00ceb20d0b23ba43fc64fca87d4&name_0=MasterCI&name_1=Stress%20test%20%28amd_debug%29&name_1=Stress%20test%20%28amd_debug%29
Related: ClickHouse#90461
Related: ClickHouse#109023
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
|
@alexey-milovidov could you apply This fix is on master and on no release branch, so 26.7 and 26.6 keep aborting Master went quiet exactly at the fix boundary. Keying on the message phrase over 14 days, with
The denominator matters, so here is the window where master carried this fix and nothing else for the family (2026-07-27 20:50:49Z to 2026-07-31 16:08:15Z, Effect and risk: the change only downgrades a key column from POINT to RANGE when its monotonic function chain is non-empty. It is conservative, so it can lose some pruning but never changes results. No settings and no serialization, so no I simulated the port with Two things I am deliberately not asking for. 26.5 and 26.3 carry the same unguarded line, but neither has fired: over 30 days they ran 1020 and 87 debug Stress rows across 102 and 87 commits with zero hits. For completeness, #111964 is also unbackported. I am not asking for it here because its condition ( |
Pull in the master fixes for the two unrelated CI failures on the previous head: - ClickHouse#111991: fix test 04627_analyzer_compatibility_final_all_joined_tables with old analyzer - ClickHouse#109023 / ClickHouse#111964: fix logical error `Inconsistent KeyCondition behavior` CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=102194&sha=5086241c961abbe0a7cb89990fa1a0fee4c4939e&name_0=PR PR: ClickHouse#102194 Co-Authored-By: Claude Fable 5 <[email protected]>
Antalya 26.6 Backport of ClickHouse#109023 - Fix Inconsistent KeyCondition behavior for wider-typed key comparisons
Closes: #111421
Related: #90461
Related: #108011
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
...
Description
Fixes the recurring debug-only
Inconsistent KeyCondition behaviorlogical error thrown fromMergeTreeDataSelectExecutor::markRangesFromPKRange(the #90461 monotonicity canary), for the manifestation where the first primary-key column is compared against a wider-typed constant.Requested by @alexey-milovidov on #108100, where the AST fuzzer mutated
04303_key_condition_ifnull_coalesce_boolean.sql'steam_id = 1(aUInt64column) intoteam_id = toUInt256(1)and hit the assertion inStress test (arm_debug). The same class was reported independently in #111421 (a composite(x, y, z)key withx = toUInt256(3)).CI provenance: STID 5182-2f27,
Stress test (arm_debug)on PR #108100, commit1f4cb31a3007c75924ae0790f691c4b549772a08, fuzzed querySELECT count() FROM t_ifnull_keycond WHERE (team_id = toUInt256(1)) AND (k = 0). Report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=108100&sha=1f4cb31a3007c75924ae0790f691c4b549772a08&name_0=PR&name_1=Stress%20test%20%28arm_debug%29Root cause
KeyCondition::matchesExactContinuousRange()gates the exact continuous-range (implicit count projection) optimization. It classifies a key column as an exactPOINTwhen the column's monotonic-function chain is strict.Comparing a
UInt64key column against a wider-typed constant (toUInt256(...),toInt256,toUInt128,toInt128) inserts a wideningCASTinto that chain. The widening cast is genuinely strictly monotonic, somatchesExactContinuousRangemarked the columnPOINT. ButcheckInRangeapplies the chain to the granule bounds viaapplyMonotonicFunctionsChainToRange, which forces the bounds closed. For a granule that straddles the key boundary, the closed-bound mapped range only partially matches, socheckInRangehonestly reportscan_be_false = true. That contradicts thePOINT"exact continuous" promise, and the subsequent exact-range computation inmarkRangesFromPKRangefinds an interior granule thatcan_be_falseand throws the debug-onlyInconsistent KeyCondition behaviorcanary.This is compiled out under
NDEBUG, so there is no behavior change in release builds. In release the broken exact range is dropped and read normally, so the count result stays correct (the wider-typed comparison just reads a couple more granules). Verified for both the #108100 single-key repro and the #111421 composite-key repro: the release count matches the native-typed comparison.Fix
Only promote a key column to
POINTinmatchesExactContinuousRangewhen its monotonic-function chain is empty (a native key column). A non-empty chain, even a strict one, cannot promise exact continuity across boundary granules because the chain application forces closed bounds; treat such a column as aRANGE. This mirrors how the function already downgrades non-strict chains, and only disables the exact-range fast path for the affected queries (the pruning and results are unchanged).Testing
tests/queries/0_stateless/04493_key_condition_wide_type_exact_range.shreproduces the abort on a debug build without this change and passes with it. It covers the single-key= toUInt256/toInt256/toUInt128/toInt128/IN-set variants and the #111421 composite(x, y, z)key atindex_granularity = 1with range conditions; each returns the same count as the native-typed comparison.Version info
26.8.1.235(included in26.8and later)