Repository navigation
Fix key analysis of a Decimal key compared with a float - #126584
Conversation
The deterministic key transform path, which also covers a plain key column for `equals`, `notEquals` and `isNotDistinctFrom`, cast a float constant to the `Decimal` key type and used the result as a single key point. A float and a `Decimal` are compared as `Float64`, where many `Decimal` values equal the same float, and the cast itself can be inexact: `toFloat32(1)` becomes `0.99999998430674944` in `Decimal(38, 18)`. So the primary key and `minmax` indices pruned granules with matching rows. Skip such candidates, as the direct match already does, because `Decimal` and `Float` have no common supertype. Closes ClickHouse#126494
|
Workflow [PR], commit [44133fc] Summary: ✅
AI ReviewSummaryThis PR prevents unsafe floating-point conversions from excluding matching rows during index analysis of decimal-backed keys. No unresolved findings remain at Final VerdictStatus: ✅ Approve |
The previous commit skipped a float constant only when the deterministic key transform casts it to a `Decimal`. The same cast happens in the monotonic chain of a key expression such as `toFloat64(d)`, and `DateTime64` and `Time64` are compared with a float as `Float64` as well (`isColumnedAsDecimal`), so `WHERE d = toFloat32(1)` with `ORDER BY toFloat64(d)`, and `WHERE d = toFloat64(1767225600)` on a `DateTime64(9)` key, still dropped matching rows. A `BFloat16` constant made the same cast throw. Both candidate sources now skip such a constant.
NIKTONIKTO717
left a comment
There was a problem hiding this comment.
Thank you @sz-kristof for another nice PR! I left one comment, and I'd love to hear what you think about it.
The previous commits skipped every float constant over a `Decimal`, `DateTime64` or `Time64` key expression input. That also stopped `d = 20.00` from using the index over `d Decimal(10, 2)`, where the literal is a `Float64`, and made `test_bucket_partition_pruning` fail: it expects `value == 20.00` to prune files of a `bucket(4, value)` partition of a `DECIMAL(10, 2)` column. A `Decimal` of at most 15 digits converts its values to different `Float64` values in the same order, so a float that converts to the type and back unchanged equals exactly one value of it and compares with the others as the cast value does. Such a float uses the index again. `0.29` does not: it is `0.28999999999999998` as a float, which the cast truncates to `0.28`. The elements of a subquery set are cast to the key type in `tryPrepareSetColumnsForIndex`, with the same problem: `d IN (SELECT toFloat32(1))` returned no rows over a `Decimal(38, 18)` key, and a `BFloat16` element threw "Conversion from BFloat16 to Decimal is not implemented". The set path now applies the same check.
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Object file sizes4 object files changed (+24.44 KiB total), 0 added.
719 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only Compile time of recompiled translation units7 translation units recompiled, 24 s compile time in total, 7 of them have a recent master baseline. |
…es in The previous commit let a float use the index over a `Decimal` of at most 15 digits when the float converted to the type and back unchanged. That was not enough in two ways. `IN` converts the column to the type of the set elements (`Set::execute`), so a `Float32` set compares in `Float32`, where every `Decimal(9, 2)` value from `999999.97` to `1000000.03` is `toFloat32(1000000)`. And the precision does not bound the stored values: `INSERT SELECT toDecimal64(9007199254740992 + number, 0)` stores 16 digits in a `Decimal(15, 0)` column, where `9007199254740995` and `9007199254740997` are the same `Float64` as `9007199254740996`. In both cases the primary key, a `minmax` index and a hash partition key pruned matching rows. Now a float uses the index over a `Decimal32` or `Decimal64` input only if the cast value converts back to it, bit for bit, in the type the predicate compares in (`Float64` for a comparison, the element type for `IN`), and the values one step below and above the cast value in the storage type convert to other floats. `DecimalUtils::convertTo` never decreases as the value grows, so the cast value is then the only value that equals the float. The bits matter for `IN`: a set compares them, so `d NOT IN (SELECT -0.0)` holds for `d = 0`, which the previous commit pruned through a `minmax` index.
NIKTONIKTO717
left a comment
There was a problem hiding this comment.
Hi @sz-kristof, one extra "low-hanging" fruit, otherwise good and will be very likely approved after this. Thanks for changes!
The check of float values built a `Field` for every value of a set whose type is not a native float, such as the 5 million values of `ts IN (SELECT toDateTime64(number * 2, 3) FROM numbers(5000000))` over a `DateTime64` key, which took 0.60 s instead of 0.47 s before that check. It now returns at once when the type cannot hold a float: only a float type, `Dynamic`, `Variant` or `JSON` can, and the walk of the type tree finds a float inside `Nullable`, `LowCardinality`, `Array`, `Tuple` and `Map` as well, so the results are the same. The query takes 0.49 s.
NIKTONIKTO717
left a comment
There was a problem hiding this comment.
Thank you @sz-kristof for another such a nice PR!
Backport #126584 to 26.10: Fix key analysis of a `Decimal` key compared with a float
Closes: #126494
Related: #96225
Related: #76867
A float and a
Decimal,DateTime64orTime64value are compared asFloat64(seeFunctionsComparison.h), andINconverts such a value to the type of the set elements (seeSet::execute). Many values of such a type can convert to the same float, and the cast of a float to such a type can be inexact:toFloat32(1)becomes0.99999998430674944inDecimal(38, 18), and0.29becomes0.28inDecimal(9, 2). Key analysis cast the float to the input type of the key expression and used the result as a key point, so the primary key andminmaxindices pruned granules with matching rows. It did so in three places:equals,notEqualsandisNotDistinctFrom. This is the regression from the issue, forDecimalandDateTime64keys; 26.9 is not affected.toFloat64(d). This already happens in 26.9.22.3: withORDER BY toFloat64(d), or aminmaxindex on it,WHERE d = toFloat32(1)returns no rows, and aBFloat16constant fails with "Conversion from BFloat16 to Decimal is not implemented".INsubquery set, intryPrepareSetColumnsForIndex:d IN (SELECT toFloat32(1))returns no rows over aDecimal(38, 18)key, and aBFloat16element fails the same way.All three now skip a float constant or set element when the input type contains a type for which
isColumnedAsDecimalholds (Decimal,DateTime64andTime64), with one exception. Over aDecimal32orDecimal64input, a float still uses the index if the cast value converts back to it, bit for bit, in the type the predicate compares in (Float64, or the element type forIN), and the values one step below and above the cast value in the storage type convert to other floats. The conversion (DecimalUtils::convertTo) never decreases as the value grows, so the cast value is then the only value that equals the float, and it compares with the others as the float does. This does not rely on the precision, which does not bound the stored values:INSERT SELECTcan store 16 digits in aDecimal(15, 0)column. Sod = 20.00still uses the index overd Decimal(10, 2), astest_bucket_partition_pruningexpects for itsbucket(4, value)partition, butd = 0.29does not, and neither doesd IN (SELECT toFloat32(1000000))overDecimal(9, 2), where999999.99is the sameFloat32. The bits matter forIN: a set compares them, sod NOT IN (SELECT -0.0)holds ford = 0. Integer,DecimalandStringconstants still use the index.The new test covers the primary key, the
minmaxcase from the issue, a key expression with both,Dynamicconstants,<=>, aBFloat16constant, aDateTime64(9)key,INandNOT INwith a subquery, exact and inexact floats over aDecimal(10, 2)key and acityHash64(d)key,-0.0in a set through aminmaxindex, and aFloat32set and values stored beyond the precision through the primary key, aminmaxindex and a partition key. It fails without the fix.Checked against
countIfover all rows, with the column andtoFloat64(d)as a primary key and as aminmaxindex,cityHash64(d)as a primary key andcityHash64(d) % 8as a partition key; with=,!=,<,<=,>,>=and<=>, and with a subquery forIN,NOT INand a two-elementIN:Decimaltypes fromDecimal(9, 0)toDecimal(50, 10),DateTime64with 0, 3, 6 and 9 digits andTime64(3), with up to 30 float constants each: 25,500 checks, no differences.Decimaltypes fromDecimal(5, 0)toDecimal(38, 18), with stored values next to where neighbouring decimals become the sameFloat32orFloat64, also beyond the precision, and theFloat64andFloat32images of the stored values and of their neighbours as constants and set elements: 102,240 checks, no differences. c971493, the previous version of this PR, has 3,200 differences there, and master 14,083.The primary key still serves
=andINwith a float that converts exactly, such as20.0,0.1ortoFloat32(0.5)overDecimal(9, 2), and no longer with one that does not, such as0.29or19.99.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed the primary key and
minmaxindices skipping matching rows when aDecimalcolumn was compared with a float constant under a key expression such astoFloat64(d), for exampleWHERE d = toFloat32(1), or with the floats of anINsubquery. Such a comparison with aBFloat16value no longer fails with "Conversion from BFloat16 to Decimal is not implemented".Workflow [PR]
Sync PR [sync-upstream/pr/126584]
Version info
26.11.1.778-master(included in26.11and later)26.10.8.11