Repository navigation
Conversation
…undary MergeTreeSetIndex::checkInRange stores each analysed range boundary by inserting it into a clone of the set's own typed column. ColumnVector::insert is data.push_back(static_cast<T>(...)), so a boundary pushed outside that column's domain was silently truncated back into it, where it could coincide with a set element and make the atom report can_be_true for a hyperrectangle containing no matching row. The boundary leaves the domain in Range::shrinkToIncludedIfPossible, which turns an excluded bound into an included one by incrementing the left or decrementing the right. It guards only against the Field's Int64/UInt64 limits, not against the eventual column's, which is correct for its own contract: it knows nothing about the column type. So for a UInt8 key holding 255, the excluded left bound (255, +inf) becomes [256, +inf), and 256 stored into a ColumnUInt8 becomes 0. With b IN (0) that is exactly the set element, so the binary search yields begin=0, end=1 and the atom answers can_be_true. The equivalent FUNCTION_IN_RANGE atom is immune because it compares Fields directly and never round-trips a bound through a typed column. That asymmetry made the set atom select 5 of 20 granules where the range atom selected 1, and the resulting over-wide range is what the exactness re-check in MergeTreeDataSelectExecutor observed as a contradiction, aborting debug and sanitizer builds with "Inconsistent KeyCondition behavior". FieldValue already carries +/-Infinity in its `value` field as a marker while `column` holds ordinary values, and both comparators already honour it. A bound above the domain is, for comparison purposes, exactly +infinity, and one below it -infinity, so this reuses that path instead of adding a concept. The direction is derived from the value's position relative to the domain, because the right-bound mirror exists too: a signed key at its minimum yields a bound one below the domain (-129 for Int8, truncating to +127). Unsigned types are immune on that side, since their minimum is 0, which equals the guard value in shrinkToIncludedIfPossible. Only Int64/UInt64 Fields are classified, which is exactly the set shrinkToIncludedIfPossible adjusts, so Float and Decimal keys cannot reach this state and are unaffected by construction. A Nullable key column keeps its values in the nested column, whose type bounds the domain, so it is unwrapped first; LowCardinality is already converted to a full column before the set index is built. The check is #ifndef NDEBUG, so release builds returned correct results and the damage was CI signal loss: the abort killed the server mid-Stress-test, so the rest of that job never ran, on pull requests unrelated to MergeTree. NOT IN was measured not to drop rows: inverting the mask turns a too-permissive can_be_true into a too-permissive can_be_false, and an exact can_be_false is only produced by the at_most_one_element_range branch, which requires a degenerate range while the truncation requires a non-degenerate one. The regression test covers both bound directions, UInt8/UInt16/Int8/Int16, Date, DateTime, Enum8, Nullable and LowCardinality wrappers, has(), tuple sets, and both checkInRange overloads, with controls one below each type's maximum, for a present set element, for the equivalent range atom, for a two-element set, and for a single-column key whose exact-count optimization must be preserved.
The exactness check the test targets is #ifndef NDEBUG, so where it is compiled out the thirteen count() witnesses cannot distinguish a fixed from a broken set index: the defect only widens the analysed mark range while row filtering still returns the right rows. The row cap is the only assertion left that can redden there, and three randomized settings each defeat it. use_query_condition_cache defaults on and is randomized 50/50. That cache prunes granules before primary-key analysis, and the same predicate already runs twice earlier in the test, so a warm entry satisfies the cap on cache pruning alone. Measured on a build with the fix reverted: with the cache enabled the row cap passes while the cache grows to three entries, with it disabled the query correctly fails with TOO_MANY_ROWS. parallel_replicas_index_analysis_only_on_coordinator defaults on, and under it a follower skips index analysis and keeps all marks, which can exceed the cap even with a correct set index. The runner enables parallel replicas for plain MergeTree tables often enough for that to be a real flake rather than a hypothetical one. Twenty-four other index tests combine a tight row cap with exactly this pin. optimize_use_projections and optimize_use_implicit_projections are randomized at five percent each. Without the implicit count() projection nothing requests exact ranges, so every witness reads no marks at all and passes whichever way the set index behaves. Before this pin the reverted build passed two runs in eight; after it, ten of ten runs fail as they should, and the fix passes ten of ten plus fifty randomized runs. The right-bound mirror had no assertion that could redden in a release build, since its count() answers are correct either way. It now gets the same row-cap pair as the above-domain witness, with the equivalent range atom beside it as the control: on the reverted build the set form exceeds the cap while the range form does not.
Internal second-model review (2 rounds, 3 findings adjudicated)I ran an independent cold review plus a second-model review pass over this change before opening Round 1: 2 findings, both AGREED and fixed❌ The single release-capable oracle was defeatable by the query-condition cache. The canary ❌ The row cap was not pinned against parallel-replica followers. Under the default
Round 2: 1 finding, DISAGREED💡 Requested: also pin
So those three pins would be inert wherever the affected assertions are live. Unlike the four
|
Pre-PR validation gate (a-i)
Regression evidence: 50 randomized runs of the new test plus |
|
cc @al13n321 @rschu1ze, could you review this? |
|
Workflow [PR], commit [1f32988] Summary: ❌
AI ReviewSummaryThis PR fixes PR Metadata
Tests
Final VerdictNeeds minor follow-up. The code change itself looks sound, but the PR should fix the changelog metadata and keep one focused regression for the remaining new branches before merge. LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 30/34 (88.24%) · Uncovered code |
Build profile diff (arm_release)No arm_release build profile data for commit 1f32988 - the build was skipped, reused from cache, or predates profile upload. |
CI finish ledger - 934f7156ab5e2a35b3b3cca7f13c1d02b8fbe4d3CI is fully finished on this head and there are no failing checks.
Session id: cron:our-pr-ci-monitor:20260809-013000 |
CI finish ledger - 934f715CI is fully finished on this head and there are no failing checks.
Correction to my previous comment: it printed the head as Session id: cron:our-pr-ci-monitor:20260809-013000 |
| @@ -0,0 +1,147 @@ | |||
| -- { echo } | |||
There was a problem hiding this comment.
@groeneai Change the test into a single minimal reproducible example. Do not use term "witness". Do not reference C++ identifiers, e.g. function names in comments.
There was a problem hiding this comment.
Done in 1f32988. The test is now one table, one query, 22 lines instead of 147; "witness" is gone and no comment names a C++ identifier.
Two things I found while reducing it, both measured on this test:
The pins are load bearing. Column statistics record that the key column holds a single value, so statistics pruning rejects the predicate before the primary key index runs, and the runner materializes statistics on insert in 95% of runs. Without that pin a pre-fix debug build passed the test in 10 of 12 runs. With it: pre-fix debug fails 12 of 12, pre-fix release fails 20 of 20, fixed build passes 50 of 50 under full randomization. The other pins (query condition cache, parallel replica index analysis, implicit projection) are each the same kind of earlier-pruning or no-index-analysis path.
The row limit is now 1 rather than 4, so the test asserts the index selects exactly the one granule it should, not merely fewer than five.
One tradeoff worth your call: the single case covers the unsigned above-domain path only. The fix also handles the below-domain direction (a signed key at its minimum) and unwraps Nullable, and those two branches are no longer pinned by any test, so a later change could break them silently. I left it at one case because you asked for one. Say the word and I will add two more short queries for those branches.
|
@groeneai The repro worked just fine. Describe which problem you are trying to fix exactly. |
|
@rschu1ze The abort needs a debug or sanitizer build: the consistency check it trips is under
So "worked just fine" is expected on a release build. That precondition was missing from my description and I am fixing that. The problem itself is observable without a debug build, and this is what I am fixing: the primary key index selects granules that cannot contain a match when the set element is compared against a boundary that was pushed outside the key column's type. On official release master, 1M rows, Both predicates are equivalent and both answer 0, so results stay correct, but the Mechanism: for I will reduce the test to a single minimal example as you asked and restate the description in these terms. |
Requested by rschu1ze on the PR. The test covered thirteen type and wrapper cases; one case is enough to guard the fix, and the extra cases made it hard to read. The kept case is the UInt8 maximum with a row limit. It is the strongest single assertion available: on a debug build it trips the index consistency check, and on a release build, where that check is compiled out, it still exceeds max_rows_to_read because the primary key index selects five granules instead of one. The four pinned settings are load bearing, each measured on a pre-fix build: without them the assertion passes whether or not the index is right. index_granularity_bytes and min_bytes_for_wide_part are pinned so the merge tree setting randomizer cannot collapse the twenty rows into a single granule. Comments no longer name C++ identifiers.
Column statistics record that the key column holds a single value, so statistics pruning rejects the predicate before the primary key index runs. The runner materializes statistics on insert in 95% of runs, which made the assertion pass on a pre-fix build in 10 of 12 measured runs. With the pin, a pre-fix debug build fails 12 of 12 and a pre-fix release build 20 of 20, while the fixed build passes 50 of 50. The row limit is also lowered from 4 to 1, so the test asserts the index selects exactly the one granule it should rather than merely fewer than five.
|
|
||
| -- Every value of b is 255, so no granule can match: the index must select a single granule of one | ||
| -- row. Before the fix it selected five: a debug build aborted, a release build read too many rows. | ||
| SELECT count() FROM t_setidx WHERE a = 1 AND b IN (0) SETTINGS max_rows_to_read = 1; |
There was a problem hiding this comment.
Reducing this to one query dropped the only CI coverage for the other two new branches in FieldValue::update: the position < 0 mirror and the ColumnNullable unwrap in compareToColumnDomain. The existing gtest_merge_tree_set_index cases do not exercise out-of-domain bounds at all, so with the current diff a future regression in either branch would still leave CI green.
Could you keep one additional focused check for either Int8 at its minimum or Nullable(UInt8)? That would keep the test small without bringing back the full matrix.
There was a problem hiding this comment.
Agreed, and I checked rather than assuming: neither existing unit test case uses a bound anywhere near a type's limit, so you are right that with the current diff a regression in either of those two branches would leave CI green.
I raised the same gap in the thread above at 15:08Z and left the decision to @rschu1ze, since he asked for the reduction 24 minutes before your comment and adding cases back is his call. I would rather not re-expand the test while that question is open.
If he wants one more case, I would pick the signed key at its minimum over the Nullable one. It is the exact mirror of the direction already covered, and it needs nothing unusual from the table, whereas a Nullable key requires a non-default table setting to be created at all, so it would only pin that branch under an opt-in configuration.
Whichever is added has to carry the same settings as the existing query and be shown to fail without the fix. Without those pins the check is decorative: statistics pruning alone rejects the predicate before the index runs, which took a pre-fix build from failing 12 of 12 runs to failing 2 of 12.
Related: #102866
Related: #90461
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
...
Description
The primary key index selects granules that cannot contain a match when an
IN-set element is comparedagainst a range boundary pushed outside the key column's type.
On official release master, 1M rows,
balways 255, default granularity:Both predicates are equivalent and answer 0, so results stay correct, but the
INform reads 25x moregranules. With
balways 254 it is1/123, identical to=: only a value at the key column type'smaximum is affected. A debug or sanitizer build also aborts, on an index consistency check that is
#ifndef NDEBUG. Found by CI, STID5182-2f27,Stress test (amd_debug)(report),
from
MergeTreeDataSelectExecutor.cpp:2497.Root cause.
MergeTreeSetIndex::checkInRangestores each analysed boundary into a clone of the set'sown typed column, where
ColumnVector::insertispush_back(static_cast<T>(...)).Range::shrinkToIncludedIfPossiblecloses an excluded bound by incrementing the left (or decrementing theright), guarding only against the
Field's limits, not the column's. For aUInt8key holding 255 theexcluded left bound
(255, +inf)becomes[256, +inf), and 256 in aColumnUInt8becomes 0 - exactlythe set element.
FUNCTION_IN_RANGEcomparesFields directly and is immune.Fix.
FieldValuealready carries+/-Infinityas a marker honoured by both comparators, soFieldValue::updatestores it instead of truncating. The direction follows the value's position: theright-bound mirror exists too, a signed key at its minimum giving a bound below the domain.
NOT INdoesnot drop rows.
Validation. Selected granules fall 5/20 to 1/20. The regression test fails 12 of 12 runs on a pre-fix
debug build and 20 of 20 on a pre-fix release build, passes 50 of 50 on the fixed build, and four mutants
redden it.
Type-relative signature (pre-fix): only a boundary at the column type's own limit wraps
UInt8UInt8UInt8UInt16UInt16Int8Int8Int16UInt8Also reproduced pre-fix and fixed:
Date,DateTime,Enum8,Nullable(UInt8),LowCardinality(UInt8),has([0], b),(a,b) IN ((1,0)),(a,b,c) IN ((1,0,'1')), and bothcheckInRangeoverloads.The four mutants: revert the guard; handle only the above-domain direction; make the fit test always
report "fits"; drop the
Nullableunwrap.