Skip to content

Convert a key-filter literal with its own type in the KV storages - #118689

Merged
alexey-milovidov merged 3 commits into
masterfrom
kv-key-filter-date-family-118397
Sep 8, 2026
Merged

alexey-milovidov merged 3 commits into
masterfrom
kv-key-filter-date-family-118397

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Sep 8, 2026 •

Copy link
Copy Markdown
Member

Closes: #118397

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):

Fix EmbeddedRocksDB, KeeperMap and Redis returning no rows for WHERE key = <literal of another date-family type>, such as a Date key compared with a DateTime literal.

Description

getFilterKeys converted an equality literal with the two-argument form of convertFieldToType, so the source type was unknown and the date-family conversion branches never fired. The raw-UInt64 fallback then returned the DateTime literal's seconds as a Date field (and a Date literal's day number as a DateTime field), the point lookup probed a key that does not exist, and the query silently returned no rows - while the same predicate returns the row on MergeTree, on the same table once the pushdown is neutralized, and through the IN branch, which casts the set with its real type.

The literal is now converted with its own type. tryConvertFieldToType is used, so a combination the conversion rejects outright (a DateTime64 literal against a Date key used to raise TYPE_MISMATCH) no longer fails the query.

A conversion that yields Null was also treated as "a key filter with zero candidate keys", which answers no rows for a literal that is merely not representable in the key type. Such a condition is no longer a key filter, so it is evaluated over a scan, like the IN branch already does when a set element cannot be cast.

The multi-column tuple-equality path converted its elements the same way and now passes the element types of the tuple it reads them from.


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

Version info

  • Merged into: 26.9.1.992 (included in 26.9 and later)

`getFilterKeys` converted an equality literal with the two-argument form of
`convertFieldToType`, so the source type was unknown and the date-family
conversion branches never fired. The raw-`UInt64` fallback then returned a
`DateTime` literal's seconds as a `Date` field (and a `Date` literal's day
number as a `DateTime` field), the point lookup probed a key that does not
exist, and `EmbeddedRocksDB`, `KeeperMap` and `Redis` silently returned no rows
- while the same predicate returns the row on `MergeTree`, on the same table
once the pushdown is neutralized, and through the `IN` branch, which casts the
set with its real type.

The literal is now converted with its own type, using
`tryConvertFieldToType`, so a combination the conversion rejects outright (a
`DateTime64` literal against a `Date` key used to raise `TYPE_MISMATCH`) no
longer fails the query.

A conversion that yields Null was also treated as "a key filter with zero
candidate keys", which answers no rows for a literal that is merely not
representable in the key type. Such a condition is not a key filter any more, so
it is evaluated over a scan - the way the `IN` branch already behaves when a set
element cannot be cast.

The multi-column tuple-equality path converted its elements the same way and now
passes the element types of the tuple it reads them from.

Closes: #118397

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Sep 8, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [7170f9d]

Summary: ✅


AI Review

Summary

This PR fixes KVStorageUtils date-family key-filter conversion for the shared KV engines by preserving the literal's source-type information for scalar and tuple equality, and by avoiding false-empty point lookups when a literal is not representable in the key type. The current head also covers the previously reported wrapped-literal and tuple-IN carriers, and I did not find remaining correctness, metadata, or evidence gaps in the diff.

Final Verdict

✅ No blocking findings in the current diff.

LLVM Coverage Report

Measured on commit 7170f9d.

Metric Baseline Current Δ
Lines 88.80% 88.80% +0.00%
Functions 91.80% 91.80% +0.00%
Branches 81.20% 81.20% +0.00%

Changed lines: Changed C/C++ lines covered: 30/35 (85.71%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Sep 8, 2026
Comment thread src/Storages/KVStorageUtils.cpp Resolved
Comment thread src/Storages/KVStorageUtils.cpp Resolved
@clickhouse-gh clickhouse-gh Bot added the comp-foreign-db Connectivity to external databases (ODBC/JDBC, MySQL, PostgreSQL, etc.). label Sep 8, 2026
Comment thread src/Storages/KVStorageUtils.cpp Resolved
@antonio2368 antonio2368 self-assigned this Sep 8, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 8, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 7170f9d48 with master 6bdba314e (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

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

Job report

…ements

`tryConvertFieldToType` chooses its conversion branch by the source type as it
is, so a `Nullable(Date)` or `LowCardinality(Date)` literal missed the
date-family branches and had its day number reinterpreted as a number of
seconds. The wrappers are stripped now, for the single-column equality and for
each element of a tuple equality.

The tuple `IN` path converted its elements without their types as well, so a
date-family element probed the wrong key there too; it now carries the set's
element types, with the same unwrapping, and uses `tryConvertFieldToType`, so an
element the key type cannot represent drops the candidate instead of raising.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member Author

Addressed both carriers.

The source type is stripped of Nullable/LowCardinality before it is passed as the hint, for the single-column equality and for every element of a tuple equality, so key = CAST(toDate('2024-01-02'), 'Nullable(Date)') against a DateTime key finds the row now (covered, together with the LowCardinality form and the composite key).

The tuple IN path carries the set's element types with the same unwrapping and uses tryConvertFieldToType, so a date-family element probes the right key and an element the key type cannot represent drops the candidate instead of raising.

One correction on the DateTime64 half of the second point: (dt, id) IN ((toDateTime64('2024-01-02 00:00:00', 3, 'UTC'), 1)) never reaches the key filter. The set itself is refused while it is built, in Analyzer/SetUtils.cpp through convertColumnToTypeOrNull, and the identical query on a MergeTree table fails the same way, so it is not something getFilterKeys can fall back from. The test pins that shared behaviour instead.

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Trivial. LGTM.

@clickgapai

Copy link
Copy Markdown
Contributor

@alexey-milovidov @antonio2368 — ClickGap found the following in this PR:

Close anything that's wrong or already addressed.

leshikus pushed a commit to leshikus/ClickHouse that referenced this pull request Oct 13, 2026
…element

`traverseDAGFilter` (multi-column overload, tuple-equality branch) bound a
raw `const DataTypeTuple *` to the temporary returned by
`removeNullable(recursiveRemoveLowCardinality(right->result_type))`.
`recursiveRemoveLowCardinality` allocates a new `DataTypeNullable` /
`DataTypeTuple` when a tuple element is `LowCardinality`, so that temporary
was the only owner and was destroyed at the end of the declaration, while
the per-element loop below kept reading `getElements()` from it.

A scalar subquery such as `(SELECT (lc_col, id) FROM t LIMIT 1)` yields
`Nullable(Tuple(LowCardinality(String), UInt64))`, which takes the
allocating branch, so `WHERE (k, id) = (SELECT ...)` on an
`EmbeddedRocksDB` table with a composite `PRIMARY KEY` read freed memory
(ASan: heap-use-after-free). Introduced by ClickHouse#118689.

Keep the stripped type in a named `DataTypePtr`, the shape the sibling call
sites in the same file (single-column `equals`, tuple `IN`) already use.

Closes ClickHouse#120527

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-foreign-db Connectivity to external databases (ODBC/JDBC, MySQL, PostgreSQL, etc.). pr-bugfix Pull request with bugfix, not backported by default pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

4 participants