Repository navigation
Fix NaN handling in accurateLess/accurateEquals for index range analysis - #98964
Conversation
…ey index contains NaN NaN values in float primary key columns broke the binary search in `markRangesFromPKRange` because `Range::intersectsRange` gives wrong results for IEEE 754 NaN (NaN is incomparable - `x < NaN` returns false for all x). This caused the binary search to incorrectly extend the matching range into NaN territory, leading to a "Inconsistent KeyCondition behavior" exception in debug builds. The fix converts NaN values in the primary key index to `POSITIVE_INFINITY` before passing them to `KeyCondition::checkInRange`, similar to how NULL values are already converted. This is correct because NaN is sorted after all normal float values in ClickHouse, just like `POSITIVE_INFINITY` in `Range` comparison semantics. https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=744485a03e0c78561cc7f820e9b43decf2cea69d&name_0=MasterCI&name_1=AST%20fuzzer%20%28amd_debug%29 Co-Authored-By: Claude Opus 4.6 <[email protected]>
…rkaround Move the fix to the root cause: `FieldVisitorAccurateLess` and `FieldVisitorAccurateEquals` used plain IEEE 754 `<` and `==` for same-type float comparisons, which makes NaN incomparable (all comparisons return false). This broke `Range::intersectsRange` and `Range::containsRange` which rely on these functions. Now use `FloatCompareHelper` with `nan_direction_hint = 1` (NaN at the end), consistent with ClickHouse sort order and `Field::operator<`. Revert the previous workaround in `MergeTreeDataSelectExecutor` that converted NaN index values to `POSITIVE_INFINITY`. Co-Authored-By: Claude Opus 4.6 <[email protected]>
|
Workflow [PR], commit [7429403] Summary: ✅ AI ReviewSummaryThis PR fixes NaN handling for index range analysis by changing Missing context
Findings
Tests
ClickHouse Rules
Final Verdict
|
The change to `accurateLess` makes NaN greater than all normal values (consistent with ClickHouse sort order). This means `ORDER BY nan WITH FILL FROM 1` now correctly generates a fill row at value 1 before the NaN data row. The fill row's non-fill SELECT column gets its default value (0). Co-Authored-By: Claude Opus 4.6 <[email protected]>
|
Closes #98075 ? |
|
Yes. |
…e/ClickHouse into fix-nan-pk-index-binary-search
…curateEquals` The AST fuzzer found that when comparing integer values (e.g. UInt8 from `isNull`) against Float64 NaN values in the primary key index, the cross-type comparison path in `FieldVisitorAccurateLess` and `FieldVisitorAccurateEquals` delegated to `accurate::lessOp` / `accurate::equalsOp` which return false for any NaN comparison. This violates ClickHouse sort order where NaN is greater than all normal values, causing "Inconsistent KeyCondition behavior" exception. The previous fix only handled same-type float comparisons via `FloatCompareHelper`. This commit extends NaN handling to the cross-type arithmetic comparison path, and adds the fuzzer-found `LowCardinality(Nullable(Float32))` test case to the regression test. Co-Authored-By: Claude Opus 4.6 <[email protected]>
…search path The previous fix to `FieldVisitorAccurateLess`/`FieldVisitorAccurateEquals` was insufficient. The root cause: when the key range left bound is NaN, all values in the range are NaN (since NaN sorts last), so no SQL comparison condition can be true. But `Range::intersectsRange` incorrectly returned true for NaN ranges, because NaN has a defined position in ClickHouse's sort order (after +inf) while SQL treats NaN as incomparable. This caused the binary search in `markRangesFromPKRange` to include NaN marks in the result range, then the exact range verification found `can_be_false=true` (from the existing NaN workaround), triggering the "Inconsistent KeyCondition behavior" assertion. Fix: when `key_range.left.isNaN()`, also set `intersects = false` (not just `contains = false`). Also revert the incorrect `03239_nan_with_fill.reference` change since `WITH FILL` uses `Field::operator<`, not `accurateLess`. Add fuzzer-found reproducer: `LowCardinality(Float32)` with identical PREWHERE and WHERE conditions (`0 < col`) that triggers the binary search with exact range verification. Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
The `accurateLess` change makes NaN ordered (greater than all values), which causes `WITH FILL FROM 1` to insert a fill row before the NaN row. Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
LLVM Coverage Report
PR changed lines: PR changed-lines coverage: 100.00% (44/44, 0 noise lines excluded) |
The change is alright. |
…98964) PR ClickHouse#98964 fixed `checkInHyperrectangle` in `KeyCondition.cpp` to handle NaN correctly in index range analysis. The PR's own test (`04031`) uses `index_granularity = 1`, so each value is in its own granule and the `left.isNaN()` branch (first granule boundary) is hit for the NaN granule. With `index_granularity = 8192` all values land in a single granule whose right bound is NaN. This exercises the `key_range.right.isNaN()` branch at `KeyCondition.cpp:4205` that was not covered by the PR's own test. Also adds a `Nullable(Float32)` variant with NaN and NULL in the same granule. Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
…e-coverage Add NaN-in-PK index range test with multi-row granule (PR #98964)
Summary
FieldVisitorAccurateLessandFieldVisitorAccurateEqualsto handle NaN correctly for float comparisons. They used plain IEEE 754<and==which makes NaN incomparable, breakingRange::intersectsRangeandRange::containsRange.FloatCompareHelperwithnan_direction_hint = 1, consistent withField::operator<and ClickHouse's sort order where NaN is placed after all normal values.markRangesFromPKRange.AST fuzzer report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=744485a03e0c78561cc7f820e9b43decf2cea69d&name_0=MasterCI&name_1=AST%20fuzzer%20%28amd_debug%29
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix exception "Inconsistent KeyCondition behavior" in debug builds when primary key contains NaN float values, by making
accurateLessandaccurateEqualshandle NaN consistently with ClickHouse sort order. Closes #98075.Documentation entry for user-facing changes
Note
Medium Risk
Changes comparison semantics for floating-point
Fieldvalues (including cross-type comparisons) used in index range analysis, which can affect primary key range pruning and related query planning. Risk is moderate because it’s localized but touches core comparison logic and NaN edge cases.Overview
Fixes NaN handling in
FieldVisitorAccurateLessandFieldVisitorAccurateEqualsby usingFloatCompareHelper(withnan_direction_hint = 1) so NaNs have a consistent ordering/equality behavior during index range analysis.Adds/updates regression tests to cover NaN values in primary-key indexes (including nullable and
LowCardinality(Nullable(Float32))) and adjusts an existing NaN-with-fill expected output.Written by Cursor Bugbot for commit f8c9aed. This will update automatically on new commits. Configure here.
Version info
26.3.1.725