Repository navigation
Conversation
`FunctionIfBase::scaleLiftCanOverflow` exempted every result wider than 64 bits, so `multiIf` / `CASE` with a `Decimal128` result was compiled even when a branch had to be lifted to a larger scale. The compiled lift is an unchecked 128-bit multiply (`nativeCastWithDecimalScale`), so a value that does not fit wraps around. Since ClickHouse#124680 the interpreted rescale throws `DECIMAL_OVERFLOW` for the same input, so the result depended on whether the expression had been compiled yet (`min_count_to_compile_expression`). For a 16-byte result the gate now checks the branch's lowest storage value against `DecimalScaleBounds<Int128>` (the helper the interpreter uses; `common::mulOverflow` does not detect an `Int128` overflow) and declines compilation when the lift can overflow. Same-scale `Decimal128` and lifts that cannot overflow (`Decimal64` by up to 19 digits, `Decimal32` by up to 28) stay compiled. `Decimal256` is never compiled, and `if` with two decimal branches of different scales is rejected before JIT. ClickHouse#126516 Co-Authored-By: Claude Opus 5.5 <[email protected]>
…iled The test checked the Decimal32 gap-28 lift by value only, which the interpreter returns identically, so dropping the Int32 arm of the 16-byte block in scaleLiftCanOverflow (or giving a Decimal32 branch the Int64 lowest) over-declined every Decimal32 lift with a gap of 20..28 and the test stayed green. Assert the compile state of that shape against the control. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review (1 finding, fixed)
Re-review of the fix: no further findings. |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-7:20261104-030400 |
|
Workflow [PR], commit [5f76d2a] Summary: ✅
AI ReviewSummaryThe change prevents JIT compilation of potentially overflowing Missing context / blind spots
Final VerdictStatus: ✅ Approve. No code changes requested; the source analysis, focused regression coverage, and author's reported before/after results support the fix. |
|
cc @Manerone @george-larionov, could you review this and add the |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units8 translation units recompiled, 37 s compile time in total, 8 of them have a recent master baseline. |
|
@groeneai ping me for JIT compilation related issues and PRs |
|
Noted, I'll cc you on JIT compilation issues and PRs from now on. |
22cb845
Backport #129166 to 26.10: Do not JIT-compile a `multiIf` `Decimal128` scale lift that can overflow
…` scale lift that can overflow
Closes: #126516
Related: #124680
Related: #126979
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed JIT-compiled
multiIfandCASEreturning a wrapped, wrong value instead of throwingDECIMAL_OVERFLOWwhen aDecimalbranch is lifted to a larger scale of aDecimal128result. Closes #126516.Description
On 26.10.2.103,
SELECT multiIf(number = 0, materialize(toDecimal128(1000000000, 0)), number = 1, toDecimal128(1, 0), toDecimal128(1, 30)) FROM numbers(1)returns-20847100.762815390390123822295304634368instead of throwingDECIMAL_OVERFLOW; on master #124680 made the interpreted cast throw, but a JIT-compiledmultiIf/CASE(compile_expressions, on by default) still returns the wrapped value, so the result depends on how often the expression already ran.Root cause:
FunctionIfBase::scaleLiftCanOverflowexempted every result wider than 64 bits, so the compiled code lifted a branch intoDecimal128with an unchecked 128-bit multiply.Change: for a 16-byte result the gate now declines a branch whose lowest storage value, multiplied by
10^(result scale - branch scale), does not fitInt128(DecimalScaleBounds, the check the interpreter uses). Such expressions run interpreted and raise the error. Same-scaleDecimal128and lifts that cannot overflow (e.g.Decimal64by up to 19 digits) stay compiled.Other compiled lifts:
ifwith two decimal scales is rejected before JIT (NOT_IMPLEMENTED), compiledCAST/arithmetic/comparison exclude decimals, andDecimal256is never compiled.Price (debug build, 100M rows): a lift that is now declined went from 1.26 s compiled to 2.81 s interpreted with
short_circuit_function_evaluation = 'disable'; at default settings that shape is not compiled on either binary (6.68 s vs 6.69 s); a same-scale control is unchanged.Validation: new test
05497_jit_multiif_decimal128_scale_lift_overflowfails on master and passes 50/50 with randomized settings; it also asserts which shapes stay compiled viaCompiledFunctionExecute.Workflow [PR]
Sync PR [sync-upstream/pr/129166]
Version info
26.11.1.1310-master(included in26.11and later)