Skip to content

Fix key analysis of a Decimal key compared with a float - #126584

Merged
NIKTONIKTO717 merged 7 commits into
ClickHouse:masterfrom
sz-kristof:fix-key-condition-float-decimal
Oct 25, 2026
Merged

NIKTONIKTO717 merged 7 commits into
ClickHouse:masterfrom
sz-kristof:fix-key-condition-float-decimal

Conversation

@sz-kristof

@sz-kristof sz-kristof commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

Closes: #126494
Related: #96225
Related: #76867

A float and a Decimal, DateTime64 or Time64 value are compared as Float64 (see FunctionsComparison.h), and IN converts such a value to the type of the set elements (see Set::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) becomes 0.99999998430674944 in Decimal(38, 18), and 0.29 becomes 0.28 in Decimal(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 and minmax indices pruned granules with matching rows. It did so in three places:

  • The deterministic constant transform from Use many key columns per condition for pruning #96225, which also covers a plain key column for equals, notEquals and isNotDistinctFrom. This is the regression from the issue, for Decimal and DateTime64 keys; 26.9 is not affected.
  • The monotonic chain of a key expression such as toFloat64(d). This already happens in 26.9.22.3: with ORDER BY toFloat64(d), or a minmax index on it, WHERE d = toFloat32(1) returns no rows, and a BFloat16 constant fails with "Conversion from BFloat16 to Decimal is not implemented".
  • The elements of an IN subquery set, in tryPrepareSetColumnsForIndex: d IN (SELECT toFloat32(1)) returns no rows over a Decimal(38, 18) key, and a BFloat16 element fails the same way.

All three now skip a float constant or set element when the input type contains a type for which isColumnedAsDecimal holds (Decimal, DateTime64 and Time64), with one exception. Over a Decimal32 or Decimal64 input, 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 for IN), 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 SELECT can store 16 digits in a Decimal(15, 0) column. So d = 20.00 still uses the index over d Decimal(10, 2), as test_bucket_partition_pruning expects for its bucket(4, value) partition, but d = 0.29 does not, and neither does d IN (SELECT toFloat32(1000000)) over Decimal(9, 2), where 999999.99 is the same Float32. The bits matter for IN: a set compares them, so d NOT IN (SELECT -0.0) holds for d = 0. Integer, Decimal and String constants still use the index.

The new test covers the primary key, the minmax case from the issue, a key expression with both, Dynamic constants, <=>, a BFloat16 constant, a DateTime64(9) key, IN and NOT IN with a subquery, exact and inexact floats over a Decimal(10, 2) key and a cityHash64(d) key, -0.0 in a set through a minmax index, and a Float32 set and values stored beyond the precision through the primary key, a minmax index and a partition key. It fails without the fix.

Checked against countIf over all rows, with the column and toFloat64(d) as a primary key and as a minmax index, cityHash64(d) as a primary key and cityHash64(d) % 8 as a partition key; with =, !=, <, <=, >, >= and <=>, and with a subquery for IN, NOT IN and a two-element IN:

  • 12 Decimal types from Decimal(9, 0) to Decimal(50, 10), DateTime64 with 0, 3, 6 and 9 digits and Time64(3), with up to 30 float constants each: 25,500 checks, no differences.
  • 14 Decimal types from Decimal(5, 0) to Decimal(38, 18), with stored values next to where neighbouring decimals become the same Float32 or Float64, also beyond the precision, and the Float64 and Float32 images 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 = and IN with a float that converts exactly, such as 20.0, 0.1 or toFloat32(0.5) over Decimal(9, 2), and no longer with one that does not, such as 0.29 or 19.99.

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Fixed the primary key and minmax indices skipping matching rows when a Decimal column was compared with a float constant under a key expression such as toFloat64(d), for example WHERE d = toFloat32(1), or with the floats of an IN subquery. Such a comparison with a BFloat16 value no longer fails with "Conversion from BFloat16 to Decimal is not implemented".


Workflow [PR]
Sync PR [sync-upstream/pr/126584]

Version info

  • Merged into: 26.11.1.778-master (included in 26.11 and later)
  • Backported to: 26.10.8.11

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
@CLAassistant

CLAassistant commented Oct 18, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@clickhouse-gh clickhouse-gh Bot added comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. external Author is not a member of the ClickHouse organization labels Oct 18, 2026
@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Oct 19, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [44133fc]

Summary: ✅


AI Review

Summary

This PR prevents unsafe floating-point conversions from excluding matching rows during index analysis of decimal-backed keys. No unresolved findings remain at 44133fc01165. The new regression test matches its reference on the PR binary; all 4,760 additional comparisons of indexed results against full scans passed.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label Oct 19, 2026
Comment thread src/Storages/MergeTree/KeyCondition.cpp Resolved
Comment thread src/Storages/MergeTree/KeyCondition.cpp Resolved
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.
@clickhouse-gh clickhouse-gh Bot added pr-bugfix Pull request with bugfix, not backported by default and removed pr-not-for-changelog This PR should not be mentioned in the changelog labels Oct 20, 2026
@NIKTONIKTO717 NIKTONIKTO717 self-assigned this Oct 22, 2026

@NIKTONIKTO717 NIKTONIKTO717 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @sz-kristof for another nice PR! I left one comment, and I'd love to hear what you think about it.

Comment thread src/Storages/MergeTree/KeyCondition.cpp Resolved
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.
Comment thread src/Storages/MergeTree/KeyCondition.cpp Resolved
Comment thread src/Storages/MergeTree/KeyCondition.cpp Resolved
@clickhouse-gh

clickhouse-gh Bot commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 44133fc01 with master 733ba5305 (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Object file sizes

4 object files changed (+24.44 KiB total), 0 added.

Object file Master PR Δ
src/CMakeFiles/dbms.dir/Storages/MergeTree/KeyCondition.cpp.o 1.72 MiB 1.75 MiB +24.43 KiB (+1.38%)

719 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only clickhouse-bundle) and not compared.

Compile time of recompiled translation units

7 translation units recompiled, 24 s compile time in total, 7 of them have a recent master baseline.

Job report

…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 NIKTONIKTO717 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @sz-kristof, one extra "low-hanging" fruit, otherwise good and will be very likely approved after this. Thanks for changes!

Comment thread src/Storages/MergeTree/KeyCondition.cpp Resolved
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 NIKTONIKTO717 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @sz-kristof for another such a nice PR!

@NIKTONIKTO717
NIKTONIKTO717 added this pull request to the merge queue Oct 25, 2026
Merged via the queue into ClickHouse:master with commit a2228ac Oct 25, 2026
178 checks passed
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 25, 2026
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Nov 19, 2026
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Nov 19, 2026
clickhouse-gh Bot added a commit that referenced this pull request Nov 19, 2026
Backport #126584 to 26.10: Fix key analysis of a `Decimal` key compared with a float
@clickhouse-gh clickhouse-gh Bot added comp-key-condition Analysis of a filter against a key. KeyCondition turns a WHERE condition into ranges over the pri... and removed comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. labels Nov 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-key-condition Analysis of a filter against a key. KeyCondition turns a WHERE condition into ranges over the pri... external Author is not a member of the ClickHouse organization pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-bugfix Pull request with bugfix, not backported by default pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-synced-to-cloud The PR is synced to the cloud repo v26.10-must-backport

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Key analysis drops rows for Decimal key compared with a Float32 held by Dynamic

8 participants