Skip to content

minmax skip index prunes NaN granule under negated float range predicate (NOT ((val >= a) AND (val <= b))) #106948

Closed

Description

@qoega

Wrong result: minmax skip index prunes NaN rows for a negated float range condition

Summary

With a minmax skip index on a Float64 (here Nullable(Float64)) column,
a granule containing only NaN values is pruned for the predicate
NOT (val >= 0. AND val <= 3.) — but row-wise evaluation says the NaN rows
match (NaN >= 0. is false, so the negation is true). The negated
range analysis treats the granule's [nan, nan] minmax range as disjoint
from the complement of [0, 3], which is only valid inside the total order;
NaN compares false against everything, so it always satisfies the negation
of any comparison-based range.

Found by the AST-fuzzer correctness-oracle suite from PR
#99980 (TLP WHERE oracle:
the p / NOT p / p IS NULL partitions lost all NaN rows; two fuzz
instances independently hit the same family via t_minmax_float and
test_negate_nan_ne_float seeds).

Minimal repro

CREATE TABLE t_minmax_float
(
    id UInt64,
    val Nullable(Float64),
    INDEX idx_val val TYPE minmax GRANULARITY 1
)
ENGINE = MergeTree ORDER BY id SETTINGS index_granularity = 3;

INSERT INTO t_minmax_float VALUES (1, NULL), (2, NULL), (3, NULL);
INSERT INTO t_minmax_float VALUES (4, nan), (5, nan), (6, nan);
INSERT INTO t_minmax_float VALUES (7, 1.0), (8, 2.0), (9, 3.0);

SELECT count() FROM t_minmax_float WHERE NOT ((val >= 0.) AND (val <= 3.));
-- index ON (default):          0   (WRONG — rows 4,5,6 have val = NaN, the predicate is true)
-- SETTINGS use_skip_indexes=0: 3   (correct)

TLP view (partitions must cover the table exactly once):

SELECT count() FROM t_minmax_float WHERE (val >= 0.) AND (val <= 3.);          -- 3
SELECT count() FROM t_minmax_float WHERE NOT ((val >= 0.) AND (val <= 3.));    -- 0, should be 3
SELECT count() FROM t_minmax_float WHERE isNull((val >= 0.) AND (val <= 3.));  -- 3
-- 6 of 9 rows with the index, 9 of 9 without.

Reproduced on 26.6.1.1 (master 76c2edca2fd, release build), default
settings.

Fix area

Minmax index condition / KeyCondition range complement logic for floating
point columns: when negating a comparison-based range, a granule whose
min/max are NaN (or whose range may contain NaN — note min/max
aggregation itself may ignore NaN, so a mixed granule's range can look
normal while containing NaN rows) must be treated as "may match". The seed
tests (t_minmax_float from the NaN-ordering test, test_negate_nan_ne_float)
suggest recent work on adjacent cases — this negation path is still open.
Note the all-NaN granule case may extend to mixed granules: if minmax
stats skip NaN, a granule [1.0, 3.0] containing a NaN row would also be
wrongly pruned for NOT (val >= 0 AND val <= 3) — worth covering both in
the fix and tests.

Original fuzzer hits (for reference)

2026-06-10 03:47:00 (cycle 9f instance 01, release, fuzzed query
SELECT id FROM t_minmax_float WHERE (val >= 0.) AND (val <= 3.) ORDER BY id DESC)
and 03:46:02 (instance 06, debug, test_negate_nan_ne_float), run dir
tmp/fuzz_lab/runs/20260610-0322-8d62cab0551/.

Version info

Activity

  1. qoega commented on Jun 10, 2026

    @qoega
    MemberAuthor

    @groeneai could you please reproduce and fix this one? Repro is in the issue body — deterministic on master 76c2edca2fd, default settings.

  2. added
    fuzz
    sqlancer
    on Jun 10, 2026
  3. PedroTadim commented on Jun 10, 2026

    @PedroTadim
    Member

    May be a duplicate of #106533

  4. groeneai commented on Jun 10, 2026

    @groeneai
    Collaborator

    Reproduced on master fc73f46 and fixed in #107074.

    @PedroTadim was right that this is related to #106533, but it needs two fixes, not one:

    • All-NaN granule (your exact repro: rows 4,5,6) is fixed by the KeyCondition negation guard from Skip not-comparison rewrite for float operands (issue #106533) #106568. Without it, NOT ((val >= 0) AND (val <= 3)) is rewritten via inverse_relations to (val < 0) OR (val > 3), which prunes the [NaN, NaN] granule.
    • Mixed granule [1.0, nan, 3.0] (the case you flagged) is a distinct bug in the minmax index itself. MergeTreeIndexAggregatorMinMax::update computes extremes with ColumnVector::getExtremes, which deliberately skips NaN, so the granule stores the clean range [1.0, 3.0] and the NaN is invisible to range analysis. Since NaN sorts after +inf in ClickHouse order, the fix records NaN as the granule max when any row is NaN, which trips the existing NaN guard in KeyCondition::checkInHyperrectangle. Widening a stored minmax range only causes conservative keeps, never wrong skips, so positive-range pruning is unaffected (a NaN granule is still pruned for val < 0, val > 500, etc.).

    #107074 supersedes #106568 and contains both fixes, closing #106533 and #106948. Regression tests cover the all-NaN granule, the mixed granule, and that positive-range pruning still works.

  5. clickgapai commented on Jun 13, 2026

    @clickgapai
    Contributor

    Found via ClickGap automated review. Please close or comment if this is incorrect or needs adjustment.

    TL;DR

    minmax skip index prunes NaN granule under negated float range predicate (NOT ((val >= a) AND (val <= b)))

    ClickGap verified: reproduced it with the script below on master; tested 6 version(s) — 4 affected, 2 clean (see Affects); bisected the regression to a single PR (#98964).

    Reproduction

    CREATE TABLE t_minmax_float
    (
        id UInt64,
        val Nullable(Float64),
        INDEX idx_val val TYPE minmax GRANULARITY 1
    )
    ENGINE = MergeTree ORDER BY id SETTINGS index_granularity = 3;
    
    INSERT INTO t_minmax_float VALUES (1, NULL), (2, NULL), (3, NULL);
    INSERT INTO t_minmax_float VALUES (4, nan), (5, nan), (6, nan);
    INSERT INTO t_minmax_float VALUES (7, 1.0), (8, 2.0), (9, 3.0);
    
    -- WRONG: returns 0 with the skip index (default). The 3 NaN rows match the
    -- negation (NaN >= 0. is false, so NOT(...) is true), so the answer must be 3.
    SELECT count() FROM t_minmax_float WHERE NOT ((val >= 0.) AND (val <= 3.));
    
    -- Correct (3) when the index is disabled:
    SELECT count() FROM t_minmax_float WHERE NOT ((val >= 0.) AND (val <= 3.)) SETTINGS use_skip_indexes = 0;
    

    Bisect

    Affects

    • Reproduces on: master, 26.5, 26.4, 26.3
    • Does NOT reproduce on: 26.2, 26.1

    Suggested next step

    Review PR #98964 for the introducing change in comp-skip-index. If a forward-fix or revert is intended, the bisect data above narrows the search space.

    Component: comp-skip-index
    Severity: P2

    cc introducing PR author: @alexey-milovidov
    cc introducing PR reviewer: @clickhouse-gh[bot]


    ClickGap · Finding: h_issue_106948

    Triage metadata

  6. zlareb1 commented on Aug 28, 2026

    @zlareb1
    Member

    The 25.12 default flip use_skip_indexes_for_disjunctions = 1 widens this bug's reach: it drops NaN rows in queries where the pre-flip index analysis was still correct, at pure default settings. The trigger is the mixed granule (the second bullet in the comment above), so no all-NaN granule is needed.

    CREATE TABLE m2
    (
        f Float64, g Int32,
        INDEX idx_f f TYPE minmax GRANULARITY 1,
        INDEX idx_g g TYPE minmax GRANULARITY 1
    )
    ENGINE = MergeTree ORDER BY tuple() SETTINGS index_granularity = 4;
    
    INSERT INTO m2 VALUES (1.0,0),(2.0,1),(nan,2),(3.0,3),(10.0,4),(11.0,5),(12.0,6),(13.0,7);
    
    SELECT count() FROM m2 WHERE NOT (f < 5 AND g < 100);
    -- default:                                       4  (WRONG — the nan row is dropped)
    -- SETTINGS use_skip_indexes_for_disjunctions=0:  5  (correct)
    -- SETTINGS use_skip_indexes=0:                   5  (correct)

    NOT (nan < 5 AND ...) is NOT (false) = true, so the nan row must be returned. Both the old and the new analyzer return 4. Reproduced on 26.9.1.1 and on 26.4.1.1 (the flip is default-on in both).

    Mechanism: with the flip off, each skip index evaluates the whole WHERE; for idx_f the g < 100 conjunct is FUNCTION_UNKNOWN, and NOT (unknown AND f < 5) stays may-be-true, so the granule survives — the pre-flip correctness here is accidental masking, not soundness. With the flip on, MergeTreeDataSelectExecutor::mergePartialResultsForDisjunctions recombines per-atom can_be_true bits by the real boolean structure, so the masking is gone and the isolated NOT (f < 5) atom is checked against the granule's NaN-free minmax range [1, 3] from getExtremes: the range is fully contained in (-inf, 5), so can_be_false comes out definitely-false although the NaN row does not satisfy f < 5; the negation flips it to a definite cannot-match, and false OR false drops granule 0. EXPLAIN indexes = 1 shows the drop under <Combined skip indexes>: Granules: 1/2 with the flip on vs 2/2 off.

    So use_skip_indexes_for_disjunctions = 0 is only a partial mitigation (a single-column NOT (f < 5) over a mixed granule still loses the row, per the mixed-granule bullet above), and since 25.12 the default-on disjunction path extends the loss to NOT (a AND b) and OR-of-conditions queries over multiple indexed columns.

    The mixed-granule fix in #107074 (record NaN as the granule max in MergeTreeIndexAggregatorMinMax::update) heals this arm as well, because the disjunction path consumes the same KeyCondition::checkInHyperrectangle verdicts that the NaN bound guard makes conservative.

  7. added
    comp-skip-index
    on Sep 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

clickgap-analyzedcomp-skip-indexculprit-pr-pinnedfuzzsqlancer

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions