Repository navigation
Fix DECIMAL_OVERFLOW when a decimal is compared with a constant that does not fit its scale - #126603
Conversation
`canBeSafelyCast` reported `Decimal(P1, S1) -> Decimal(P2, S2)` as safe whenever `P1 <= P2` and `S1 <= S2`. Raising the scale multiplies the stored integer by `10^(S2 - S1)`, so `Decimal(38, 0)` holding `10^9` does not fit `Decimal(38, 30)`, and declared precision does not bound stored values. Index analysis (`KeyCondition` for sorting/partition keys and minmax/set skip indexes, and the KV storage key `IN` set) trusts "safe" and takes a throwing cast, so a valid filter such as `d = toDecimal128(1000000000, 0)` on a `Decimal(38, 30)` key failed with `DECIMAL_OVERFLOW` while the same filter on an unindexed column returns the right answer. Since ClickHouse#124680 the overflow is detected for Decimal128/256 too, which turned the wrapped key value into an exception; Decimal32/64 `IN (subquery)` threw already on released versions. The pair is now safe only when every value of the source native integer, scaled up, fits the target native integer; other pairs take the callers' existing `accurateOrNull` probe, which declines an unrepresentable constant. Closes ClickHouse#126339 Co-Authored-By: Claude Opus 5.5 <[email protected]>
…a decimal constant that does not fit The strict `Field` conversion that builds a literal `IN` set rescaled a decimal with the throwing `convertDecimals`, so a member that does not fit the column's type at its scale failed the query instead of being skipped. It now uses `tryConvertDecimals` and returns Null, and in strict mode a Null element of a `Tuple`, `Array` or `Map` that comes from a non-NULL source makes the whole value unconvertible, so such a member is never kept as a NULL one. `isNotDistinctFrom` / `isDistinctFrom` cast both operands to their common type even when the result was only used for `Nullable` operands; the cast now happens only in that branch, and other operands are compared exactly by `FunctionComparison`. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 0 findings on
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-8:20261018-182600 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-4:20261018-161400 |
|
cc @Algunenano @yariks5s, could you review this and add the |
|
Workflow [PR], commit [5f7528a] Summary: ✅
AI ReviewSummaryThe PR prevents Final VerdictStatus: ✅ Approve. |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units15 translation units recompiled, 109 s compile time in total, 15 of them have a recent master baseline. |
|
Backporting to v26.10 as #129166 needs it |
Cherry pick #126603 to 26.10: Fix `DECIMAL_OVERFLOW` when a decimal is compared with a constant that does not fit its scale
…ompared with a constant that does not fit its scale
Backport #126603 to 26.10: Fix `DECIMAL_OVERFLOW` when a decimal is compared with a constant that does not fit its scale
…ompared with a constant that does not fit its scale
Closes: #126339
Related: #124680
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed
DECIMAL_OVERFLOWthrown by=,!=,IN,NOT INand non-NullableIS [NOT] DISTINCT FROMwhen a decimal is compared with a decimal constant that does not fit its type at its scale, e.g.d IN (toDecimal128(1000000000, 0))ford Decimal(38, 30), on sorting and partition keys,minmaxandsetindexes,EmbeddedRocksDBkeys and unindexed columns. Such a constant now equals no value of the column instead of throwing.Description
On 26.9.19.9,
d IN (SELECT toDecimal32(300000000, 0))on aDecimal(9, 1)sorting key throwsDECIMAL_OVERFLOW, while the same filter on an unindexed column returns0. Since #124680 the overflow is detected forDecimal128/Decimal256as well, so the queries of #126339 now throw on master.Three places rescale a constant only to compare it:
canBeSafelyCastcalledDecimal(P1, S1) -> Decimal(P2, S2)safe wheneverP1 <= P2 && S1 <= S2, although raising the scale shrinks the range. Key analysis then took a throwing cast. A pair is now safe only when the scaled native range fits; other pairs take the existingaccurateOrNullprobe.Fieldconversion that builds a literalINset used the throwingconvertDecimals. It now returns Null, and an unrepresentable element of aTuple/Array/Mapmakes the whole value Null instead of aNULLelement. This also fixestuple(NULL::Nullable(Decimal64(1))) IN (tuple(33.33::Decimal64(2)))withtransform_null_in = 1, which returned1.isNotDistinctFromcast both operands to the common type although only theNullablebranch uses it.Not changed: a
Nullableoperand of<=>,CASEandtransformstill cast and throw like[a] = [b](#124680); an integer literal still throwsARGUMENT_OUT_OF_BOUND, as on releases.Side effects: an
INSERToftuple(1000)intoVariant(Tuple(Nullable(Int8)), Tuple(String))now keeps the value instead of storing(NULL), and aMongoDBINlist element that matches nothing is dropped instead of disabling the pushdown.Index analysis of a 1M-element rescaled
IN (SELECT ...)set now takes the probe: 1254 ms vs 980 ms (debug build). It must travel with any backport of #124680.The new test lines fail on master.
Workflow [PR]
Sync PR [sync-upstream/pr/126603]
Version info
26.11.1.241-master(included in26.11and later)