Skip to content

Fix missing rows in EmbeddedRocksDB, KeeperMap and Redis key lookups for a float compared with a Decimal key - #128086

Merged
antonio2368 merged 4 commits into
ClickHouse:masterfrom
groeneai:fix-kv-key-lookup-decimal-float-constant
Nov 3, 2026
Merged

antonio2368 merged 4 commits into
ClickHouse:masterfrom
groeneai:fix-kv-key-lookup-decimal-float-constant

Conversation

@groeneai

@groeneai groeneai commented Oct 28, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #126584
Related: #127676

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 EmbeddedRocksDB, KeeperMap and Redis tables with a Decimal, DateTime64 or Time64 primary key returning no rows for a float constant such as WHERE key = 19.99 or key IN (SELECT 19.99) when the key holds the matching value, and ALTER TABLE ... DELETE / UPDATE with such a condition changing nothing. Such a condition now reads the whole table unless it is a Float32 or Float64 constant proven to match only its cast to a Decimal32 or Decimal64 key (key = 12.5); a constant written in the key's type (key = '19.99') still uses the key lookup.

Description

On 26.8.28.14 and 26.9.19.9, SELECT count() FROM t WHERE k = 19.99 over an EmbeddedRocksDB table with a Decimal(10, 2) key holding 19.99 returns 0, while the full scan not(not(k = 19.99)) returns 1.

= compares a float with a Decimal-like value as Float64, and IN compares the key cast to the set element type, so a float can equal keys other than its cast. The key filter (getFilterKeys) cast the float into the key type, which truncates (19.99 is 19.98999..., cast to 19.98), and looked up only that key. At a high scale (Decimal(38, 30), DateTime64(9)) several stored values share one float image, and only one was looked up.

The filter now declines the lookup (a full scan; the condition still applies) when floatValuesMissKeyValues, the MergeTree key analysis check for the same comparison (#126584), says the float can match a key other than its cast: for =, IN sets, and per element of a tuple = / IN over a composite key. It proves uniqueness only for a Float32 / Float64 value over a Decimal32 / Decimal64 key (k = 12.5 keeps the lookup); other floats (wider Decimal, DateTime64, Time64 keys, or inside Dynamic / Variant) read the whole table, as in MergeTree.

Validation: the new test fails on master and every reference line equals the full-scan answer; a 684-case sweep over 7 key types finds 80 lookup/scan mismatches on master and 0 with the fix; 50/50 randomized runs; the existing KV engine tests and 05408_key_condition_decimal_float_constant behave as on master.

🤖 Generated with Claude Code


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

Version info

  • Merged into: 26.11.1.1215-master (included in 26.11 and later)
  • Backported to: 26.10.8.26

groeneai and others added 2 commits October 28, 2026 17:27
`=` compares a float with a `Decimal`, `DateTime64` or `Time64` value as
`Float64`, and `IN` compares the key cast to the set element type, so a
float can equal keys other than its cast to the key type. `getFilterKeys`
cast the float into the key type, which truncates (`19.99` becomes
`19.98`), and looked up only that key, so `EmbeddedRocksDB`, `KeeperMap`
and `Redis` returned no rows for `k = 19.99` over a `Decimal(10, 2)` key
holding `19.99`, and `ALTER DELETE` with that condition deleted nothing.
At a high scale several stored values share one float image.

Reuse `floatValuesMissKeyValues`, which the MergeTree key analysis uses
for the same comparison, to decline the key filter (full scan, the
condition is still applied) for `=`, for `IN` sets and per element of a
tuple `=` / `IN` over a composite key. An exact float over a `Decimal32`
or `Decimal64` key keeps the lookup.

A constant node of an `ActionsDAG` holds a `ColumnConst` of 0 rows, and
the helper walks the rows of its column, so the constant is passed as a
1-row column (`cloneResized(1)`), the value `getField()` reads.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A scalar subquery returns a `Nullable(Tuple(...))` constant, so the tuple
`=` branch did not see a `ColumnTuple` and checked the whole tuple against
every key column: any float element declined the lookup, also for an exact
`(a, b) = (SELECT (12.5, 1))` over a `Decimal` key and for a composite key
with no float compared with a `Decimal`. Unwrap one outer `Nullable` of
the column and the type before the per-element check.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai

Copy link
Copy Markdown
Collaborator Author
Internal second-model review (2 rounds): 2 major resolved, 1 nit noted
Finding Verdict
❌ A Nullable(Tuple) constant from a scalar subquery skipped the per-element check of a composite-key tuple =, so an exact (a, b) = (SELECT (12.5, 1)) lost its key lookup AGREE, fixed: one outer Nullable is unwrapped before the per-element check; test covers it
⚠️ An exact float inside Dynamic/Variant (k = CAST(12.5 AS Dynamic)) reads the whole table, so "unless the float matches exactly one key" overclaimed AGREE on the wording, fixed in the description: the shared check proves uniqueness only for a Float32/Float64 value over a Decimal32/Decimal64 key, as in MergeTree since #126584; results stay correct
💡 #127676 edits the same lines of traverseDAGFilter noted: the later of the two PRs resolves a textual conflict

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. Decimal(10, 2) key holding 19.99: k = 19.99 returns 0 rows, not(not(k = 19.99)) returns 1, on every run (master, 26.8.28.14, 26.9.19.9).
b Root cause explained? = compares the float and the key as Float64, IN in the set element type; the key filter cast the float into the key type (truncating 19.99 to 19.98) and looked up only that key. At a high scale several keys share one float image.
c Fix matches root cause? Yes. The lookup is declined when the float can equal a key other than its cast (the MergeTree check from #126584); a Float32 / Float64 value exact over a Decimal32 / Decimal64 key keeps the lookup.
d Test intent preserved / new tests added? New test 05457_kv_key_lookup_decimal_float_constant; every reference line equals the full-scan answer, plus GetKeys controls for kept lookups.
e Both directions demonstrated? Yes. The test fails on master and passes with the fix; a sweep over 7 key types and 684 float constants: 80 lookup/scan mismatches on master, 0 with the fix.
f Fix is general across code paths? All four filter branches (=, IN, tuple =, tuple IN; AND / OR recurse into them), all three storages via getFilterKeys, and mutations (ALTER DELETE is in the test).
g Fix generalizes across inputs (params/datatypes/wrappers)? Decimal32/64/128/256, DateTime64, Time64, Tuple / Array keys, composite keys; Float64, Float32, Dynamic, Variant, Nullable / LowCardinality sets, Nullable(Tuple) scalar-subquery constants; nan, -0., out-of-range floats.
h Backward compatible? (maintainer-approved exception only) Yes. No setting or format change; inexact floats and every float over a wider Decimal, DateTime64 or Time64 key or inside Dynamic / Variant now read the table, which returns the rows the condition matches.
i Invariants and contracts preserved? The looked-up keys include every key the condition matches; a declined filter returns false like any other non-key condition. MergeTree behaviour is unchanged.

Session id: cron:clickhouse-impl-self-slot-57:20261028-155600

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @GrigoryPervakov @tiandiwonder, could you review this and add the can be tested label so CI runs? The KV key filter (getFilterKeys) cast a float constant into a Decimal-like key and looked up only that key while = compares the two as Float64, so k = 19.99 on a Decimal(10, 2) key returned no rows; it now declines the lookup unless the MergeTree check floatValuesMissKeyValues proves the cast is the only match.

@clickhouse-gh

clickhouse-gh Bot commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [0df1333]

Summary: ✅


AI Review

Summary

The PR prevents incomplete key lookups in EmbeddedRocksDB, KeeperMap, and Redis when floating-point predicates can match additional Decimal, DateTime64, or Time64 keys. At 0df1333fe5d562fd9c472e3517c5ba3236e5cda3, no unresolved findings remain. The earlier Float32 composite IN finding is addressed by matching the guard's conversion to the lookup's conversion through Field, with regression coverage.

Missing context / blind spots
  • ⚠️ CI for the latest merge is still running, and no local build was available. The preceding revision's targeted regression report records 50 successful runs of 05457_kv_key_lookup_decimal_float_constant, and its bugfix validation passed. Completed regression jobs on the current head would close the remaining validation gap.
Final Verdict

Status: ✅ Approve. No code changes requested.

@clickhouse-gh clickhouse-gh Bot added pr-bugfix Pull request with bugfix, not backported by default comp-foreign-db Connectivity to external databases (ODBC/JDBC, MySQL, PostgreSQL, etc.). labels Oct 28, 2026
@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Oct 28, 2026
Comment thread src/Storages/KVStorageUtils.cpp Resolved
@clickhouse-gh

clickhouse-gh Bot commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 0df1333fe with master 4741b0dc4 (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.

Compile time of recompiled translation units

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

Job report

The tuple IN branch builds each lookup key from a `Field`, and a `Field`
holds a `Float32` as `Float64`, so `tryConvertFieldToType` scales the
widened value in `Float64`. `floatValuesMissKeyValues` cast the original
`Float32` column instead, in `Float32` arithmetic. The two can disagree:
`toFloat32(0.29)` is `0.28999999165534973`; times 100 in `Float32` that
rounds to 29 (key `0.29`, which the check proves is the only match),
while in `Float64` it is `28.999999165534973` and truncates to 28, so the
lookup probed `0.28` and `(a, b) IN (SELECT toFloat32(0.29), 1)` missed
the stored `(0.29, 1)` (also inside `Nullable`/`LowCardinality`).

Widen a `Float32` set column to `Float64` before the check, so it proves
the key the lookup actually builds. The comparison type stays the set
column's own type. A lookup whose `Float64` cast is exact is kept
(`toFloat32(12.5)` still reads `GetKeys`).

The `=` branches compare in `Float64`. An exhaustive search over the
keys the check can accept there (every `Decimal32`/`Decimal64` key at
scales 1-18 that a `Float32` product can produce exactly) found none
where the two casts disagree, so they stay as they are. The
single-column IN branch already converts with the same typed cast as
the check.

Found by the automated review on ClickHouse#128086.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finish ledger - 8f2da1d

Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
whose fixing-PR link will be posted here when it opens. Only CH Inc sync is exempt.

Check / test Reason Owner / fixing PR
Fast test (arm_darwin) / 05397_clickhouse_local_locked_path_untouched not from this PR (its diff changes only the KV key filter and 05457): the test calls flock(1), which macOS does not have, so the expected "Another server instance in same directory is already running" line is missing #128088 (mine, open)
Mergeable Check, PR praktika rollups of the row above #128088 (the owner of the row above)

The new test 05457 passed 50 of 50 runs in the flaky check, and both Bugfix validation (functional tests) jobs confirmed it fails without the fix. CH Inc sync is green. The earlier Config Workflow failures were the attempts before the can be tested label.

Session id: cron:our-pr-ci-monitor:20261029-043003

@antonio2368 antonio2368 self-assigned this Nov 2, 2026
@antonio2368

Copy link
Copy Markdown
Member

@groeneai merge latest master

@groeneai

groeneai commented Nov 2, 2026

Copy link
Copy Markdown
Collaborator Author

Done: merged master (77fcf30) in 0df1333, no conflicts. 05457_kv_key_lookup_decimal_float_constant passes on the merged build.

@antonio2368
antonio2368 added this pull request to the merge queue Nov 3, 2026
Merged via the queue into ClickHouse:master with commit 1843e9c Nov 3, 2026
179 checks passed
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 3, 2026
groeneai added a commit to groeneai/ClickHouse that referenced this pull request Nov 12, 2026
…ixed-string-variant-constant

Resolve the conflict in src/Storages/KVStorageUtils.cpp with master's
float/Decimal key checks (ClickHouse#128086): keep master's floatValuesMissKeyValues
checks before the constant-type resolution, and extend master's tuple
column extraction with the Dynamic/Variant unwrapping of this PR.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@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 added a commit that referenced this pull request Nov 19, 2026
Cherry pick #128086 to 26.10: Fix missing rows in `EmbeddedRocksDB`, `KeeperMap` and `Redis` key lookups for a float compared with a `Decimal` key
robot-clickhouse added a commit that referenced this pull request Nov 19, 2026
…eperMap` and `Redis` key lookups for a float compared with a `Decimal` key
robot-clickhouse added a commit that referenced this pull request Nov 19, 2026
@robot-ch-test-poll4 robot-ch-test-poll4 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 #128086 to 26.10: Fix missing rows in `EmbeddedRocksDB`, `KeeperMap` and `Redis` key lookups for a float compared with a `Decimal` key
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-foreign-db Connectivity to external databases (ODBC/JDBC, MySQL, PostgreSQL, etc.). 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.

7 participants