Repository navigation
Fix toString of integer keys skipping rows across digit counts and use the key for toString(key) IN (subquery) again - #130537
Conversation
Since ClickHouse#118957, `toString(k) IN (SELECT <integers>)` over an integer key `k` no longer uses the primary key (ClickHouse#130315): `EXPLAIN indexes = 1` shows `Condition: true` and every granule is read, where 26.8 pruned with `toString(k) in N-element set`. The same holds for `CAST(k AS String)`, `k::String`, a `String ALIAS k` column (`_CAST`), `Nullable`/`LowCardinality` integer keys and a `Date` key with a `Date` set. ClickHouse#118957 made `tryPrepareSetColumnsForIndex` decline a non-`String` set element over a string predicate column, because `Set::execute` casts the column into the element type by parsing, where many spellings name one value (`'02'` and `'2'`), while the index renders the element into a single spelling. That premise does not hold when the string column is the text of a native integer (or of a `Date`) that the key's monotonic chain itself computes: the column then holds only canonical numerals, `accurateOrNull` of a canonical numeral into a native integer type gives that number or NULL, and an integer element renders to the canonical numeral of its own value, so the set and the index agree exactly. Keep the decline, except when the chain ends with `toString`/`CAST`/`_CAST` to `String` of a native integer and the element is a native integer, or both are `Date`. Types with a custom serialization are excluded on either side: `Bool` is a `UInt8` that renders as `true` while the key's `'1'` parses to `true` (also inside `SimpleAggregateFunction(any, Bool)`, which keeps `SerializationBool` under another name). A `String` key, `FixedString` targets, `DateTime` and the other types keep the decline. Reverting ClickHouse#118957 would bring back the wrong results it fixed (for example `s IN (SELECT toUInt8(2))` over a `String` key holding `'02'`). Closes ClickHouse#130315 Co-Authored-By: Claude Opus 5.5 <[email protected]>
The EXPLAIN lines show only that the condition is built; a condition can be printed while `MergeTreeSetIndex::checkInRange` prunes nothing. Read the `UInt64` cell under `max_rows_to_read = 100`: the key reads 21 of the 200 rows, a full scan fails with `TOO_MANY_ROWS`. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The read-limit check covered only `toString` of a `UInt64` key. `CAST`, a `String ALIAS` column (reached as `_CAST`), `Nullable` and `LowCardinality` keys and a `Date` key evaluate ranges through different code, so a printed condition alone did not prove they prune. Each now has its own `max_rows_to_read = 100` check over 200 rows; master fails all six with `TOO_MANY_ROWS`. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review (rounds 1-3, head 4140b8a)
|
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-5:20261110-140200 |
|
cc @nihalzp @KochetovNicolai, could you review this and add the |
|
Workflow [PR], commit [a55b1fd] Summary: ✅
AI ReviewSummaryThis PR fixes incorrect Final VerdictStatus: ✅ Approve. |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Slowest function optimization changes (ThinLTO)Median per-function time ratio to the master baseline:
Compile time of recompiled translation units41 translation units recompiled, 291 s compile time in total, 41 of them have a recent master baseline. |
`ToStringMonotonicity` compared `floor(log10(x))` computed in `double`. From 15 digits up the logarithm of `10^n - 1` rounds to `n` (and from 16 digits up `10^n - 1` itself rounds to `10^n`), so a range such as [999999999999999999, 1000000000000000000] was reported as monotonic although its texts are in the opposite order. Index analysis then mapped the range to an inverted string range and skipped matching rows: on 26.3, 26.8 and 26.10 `toString(k) = '999999999999999999'` returns 0 instead of 1 when one granule (or a minmax skip index granule) holds both values, and the `toString(k) IN (subquery)` pruning restored by the previous commits reaches the same path (found by the automated review of ClickHouse#130537). Use `common::digits10`. The zero handling is unchanged: 0 stays apart from 1..9, because a `Bool` renders 0 and 1 as the configurable `bool_false_representation` and `bool_true_representation`, so counting 0 as one digit would let `toString(b) = 'z'` skip the `false` row when false is rendered as 'z' and true as 'a'. Co-Authored-By: Claude Opus 5.5 <[email protected]>
toString(key) IN (subquery) over an integer key againtoString of integer keys skipping rows across digit counts and use the key for toString(key) IN (subquery) again
Backport #130537 to 26.10: Fix `toString` of integer keys skipping rows across digit counts and use the key for `toString(key) IN (subquery)` again
…ws across digit counts and use the key for `toString(key) IN (subquery)` again (cherry picked from commit d8d6d32)
…-explain-tests Resolve the conflict in KeyCondition.cpp: master added isTextOfElementValues (ClickHouse#130537, 79950f0) at the place where this branch adds keyToSetConversionMayCollapse; keep both. keyToSetConversionMayCollapse reads the chain's result type, so for `toString(k) IN (SELECT <integers>)` it sees a String key against an integer set and would decline the set again, undoing ClickHouse#130537. Apply the same isTextOfElementValues exemption to it: the chain computes only canonical numerals (or `Date` text), which the set's cast parses back to one value. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed filters on
toStringof an integer key, such astoString(key) = '999999999999999999', skipping matching rows when one granule (or oneminmaxskip index granule) held integers of 15 or more digits with different numbers of digits.toString(key) IN (subquery)(alsoCAST(key AS String)) over an integer orDatekey uses the primary key again when the subquery returns integers (or dates), as before 26.10. Closes #130315.Description
Closes: #130315
Related: #118957
On 26.3, 26.8 and 26.10,
toString(k) = '999999999999999999'over aUInt64key returns 0 instead of 1 when one granule holds 999999999999999999 and 1000000000000000000.ToStringMonotonicitycomparedfloor(log10(x))indouble, which givesnforx = 10^n - 1from 15 digits on, so such a range counted as monotonic although its texts are in the opposite order. The digit count is now exact (common::digits10); 0 stays apart from 1..9 as before, because aBoolrenders them as configurable strings.Since #118957 (26.10),
toString(k) IN (SELECT <integers>)over an integer sorting key reads every granule, where 26.8 pruned (#130315). #118957 declines a non-Stringset element over a string column, becauseINparses the column ('02'and'2'are both2) while the index renders the element. When the string is the text that the key's own chain computes from a native integer (toString,CAST/_CASTtoString), it holds only canonical numerals, so the set and the index agree; the same holds forDate. The decline now exempts exactly that case; types with a custom serialization (Bool, also insideSimpleAggregateFunction) stay declined. This path needs the exact digit count: 26.8 pruned it with the same wrong result. Reverting #118957 would bring back the wrong results it fixed (s IN (SELECT toUInt8(2))over aStringkey holding'02').Validation: on the issue's setup the first parent of #118957 and this PR select 1186/3668 granules, master 3668/3668, 39 rows on all. Both tests fail on master; every indexed count matches a full-scan
countIf, andmax_rows_to_readchecks pin the pruning, also for 19-digit keys. 50 randomized runs pass.Workflow [PR]
Sync PR [sync-upstream/pr/130537]
Version info
26.11.1.2150-master(included in26.11and later)