Repository navigation
Do not treat a key range as exact when the constant is a signed zero - #117722
Conversation
When the primary key is a transform of a column, `KeyCondition` pushes the constant of an equality through the transform and treats the resulting key atom as exact if the transform is injective. IEEE equality does not distinguish `-0.0` from `+0.0`, but an injective transform does: `toString(-0.0)` is `'-0'` and `reinterpretAsUInt64(-0.0)` is not zero. The key atom then covers only one of the two zeros, so `f = 0.0` skipped the granules holding the other one, and `f != 0.0` counted its rows without filtering them. The rewrite is now kept only when the transform maps both zeros to the same value, or when the constant does not reach the transform as a zero, so a non-zero constant keeps its exact single-granule lookup. Closes: #117543
|
Workflow [PR], commit [37d40a0] Summary: ✅
AI ReviewSummaryThis PR fixes wrong results when deterministic key rewrites treat Final Verdict
LLVM Coverage ReportMeasured on commit 37d40a0.
Changed lines: Changed C/C++ lines covered: 74/82 (90.24%) · Uncovered code |
|
The scalar fix looks right, including declining the rewrite entirely (a merely non-exact transformed point atom would still fail to intersect the CREATE TABLE t3 (k Tuple(Float64, Float64)) ENGINE = MergeTree ORDER BY toString(k) SETTINGS index_granularity = 1;
INSERT INTO t3 VALUES ((-0.0, 1.0)), ((-0.5, 1.0)), ((0.0, 1.0)), ((2.0, 1.0));
SELECT count() FROM t3 WHERE k = (0.0, 1.0); -- 1, should be 2; k != (0.0, 1.0) gives 3, should be 2
The same top-level-only blindness applies to NaN inside a tuple, which escapes the |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units7 translation units recompiled, 20 s compile time in total, 7 of them have a recent master baseline. |
…igned-zero-exactness-117543
Address review: the bailout was keyed on the transformed image colliding with one of the zero images, so a non-zero constant lost the previously safe pruning whenever its image collided with a zero image under a non-injective transform. For `ORDER BY notEquals(reinterpretAsUInt64(f), 0)`, `WHERE f = 1.0` transforms to the same key value as `-0.0` and fell back to a full scan. The signed-zero ambiguity exists only when the constant itself is a zero: an equality against any other constant matches a single float value, which the transformed key identifies just as well as before. The check is now keyed on that, and only a constant whose value cannot be compared against zero here (a `String`, which the transform converts to a float itself) still uses the image check. Note that this is not only about exactness - a relaxed atom is also not allowed to prune a granule that the original predicate matches - so the bailout does not depend on injectivity. The test now covers the non-injective transform in both directions: `f = 1.0` keeps its pruning (verified to be load-bearing against a control binary with the previous check), and `f = 0.0` reads every granule. Also fix the test failure in CI: the `marks` estimate for the non-zero constant is `1` with implicit projections and `2` without, and `optimize_use_implicit_projections` is randomized, so the assertion is now that the estimate stays below the number of granules in the table, which is what distinguishes index pruning from a full scan. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
…igned-zero-exactness-117543
…a `Tuple` constant The signed-zero bailout in `KeyCondition::canConstantBeWrappedByDeterministicFunctions` was gated on `isFloat(dag.input_type)`, so it only covered a scalar float key input. For `k Tuple(Float64, Float64)` with `ORDER BY toString(k)`, `WHERE k = (0.0, 1.0)` still built an exact atom on `'(0,1)'` and pruned the granule holding `(-0.0, 1.0)`, which tuple equality matches, so the query returned 1 row instead of 2 (and `k != (0.0, 1.0)` counted the row without filtering it). Walk the key input type and the constant `Field` together, and decline the rewrite when a zero reaches a float element inside a `Tuple`. The scalar path keeps probing whether the transform distinguishes `+0.0` from `-0.0`; for a tuple the probe would have to enumerate every combination of signs of the zero elements, so the index simply does not answer the predicate. A zero at a non-float element, and any tuple constant without a zero, keep their pruning. The tuple-carried NaN case raised in the same review is already handled by the recursive NaN check in `KeyCondition::tryParseAtomFromAST`; the new test pins it as well. Addresses the review threads and the AI review verdict on #117722. Co-Authored-By: Claude Fable 5.1 <[email protected]>
|
🕵 Thanks, both halves are now covered. The tuple-carried zero is handled in c7c1fa9 exactly as you suggest: a recursive gate that declines the rewrite whenever the constant holds a float zero at a float position of the |
…igned-zero-exactness-117543
…m check
The previous version declined every deterministic key rewrite for an `Array`
or `Map` key holding floats, even when the constant has no zero, so
`WHERE a = [2.0]` with `ORDER BY toString(a)` fell back to a full scan.
`constantMayHoldFloatZero` now walks `Array` and `Map` element by element like
`Tuple`, and bails out only for an actual zero at a float position, a shape
mismatch, or another float carrier.
Reuse the `typeContainsFloat` helper that master added for the `NaN` set check
instead of keeping a second copy of it.
The `IN` path does not need the same guard: `IN` compares floats bit-exactly
(`-0.0 IN (0.0)` is 0), so the transformed set `{'0'}` selects exactly the rows
`f IN (0.0)` matches. The new test pins the agreement of the index with a full
scan for `IN`/`NOT IN`, and the `Array`/`Map` cases.
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
|
🕵 The only red check on the previous head New head |
…key transform check `typeContainsFloat` relies on `IDataType::forEachChild`, which already walks nested types recursively, so `Array(Tuple(Float64))` and `Map(String, Array(Nullable(Float64)))` are recognized as float carriers. The test pins this down, as asked in the review. Co-Authored-By: Claude Opus 5.5 <[email protected]>
alexey-milovidov
left a comment
There was a problem hiding this comment.
This is weird. LGTM.
When the primary key is a transform of a column,
KeyCondition::canConstantBeWrappedByDeterministicFunctionspushes the constant of an equality through the transform and treats the resulting key atom as exact if the transform is injective. IEEE equality does not distinguish-0.0from+0.0, but an injective transform does:toString(-0.0)is'-0'andreinterpretAsUInt64(-0.0)is not zero. The key atom then covers only one of the two zeros:f != 0.0was wrong in the other direction: the exact atom let the-0.0row be counted without being filtered, returning 3 instead of 2.The rewrite is now kept only when the transform maps both zeros to the same value, or when the constant does not reach the transform as one of them, so a non-zero constant keeps its exact single-granule lookup (asserted by the test).
Closes: #117543
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes to CHANGELOG.md):
Fixed wrong results for
f = 0.0andf != 0.0over a float column when the primary key is an injective transform of it (for exampleORDER BY toString(f)): the index treated the transformed constant as exact, although the transform distinguishes-0.0from+0.0while the comparison does not.Workflow [PR]
Sync PR [sync-upstream/pr/117722]
Version info
26.10.1.1355-master(included in26.10and later)