Repository navigation
Report an integer operand that does not fit the decimal result width - #118721
Conversation
```sql SELECT toDecimal32(1.5, 4) * 9223372036854775807; -- -1.5 (the multiplier narrowed to -1) SELECT toDecimal32(1.5, 4) + 9223372036854775807; -- 0.5 (the addend narrowed to -1) SELECT intDiv(toDecimal32(1.5, 4), -9223372036854775807); -- 1 ``` `DecimalBinaryOperation` computes in the native signed width of the decimal result (`Int32` for `Decimal(5, 4)`) and `static_cast`s both operands into it, so an integer operand beyond that width wrapped and every row of the result was silently wrong - for constants and for columns alike. Comparing the same pair already reports `Can't compare decimal number due to overflow`, and the documented contract of `Decimal` is that excessive digits in the integer part raise an exception. The narrowing is checked now, under the existing `decimal_check_overflow` switch: the operation template already has a `check_overflow` parameter chosen from that setting, so `decimal_check_overflow = 0` keeps the previous behaviour. Only the conversions that can lose information are checked, so a narrower integer into a wider native type costs nothing, and a wider decimal (`toDecimal128(1.5, 4) * 9223372036854775807`) still computes the product. Closes: #117237 Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
|
Workflow [PR], commit [acba390] Summary: ✅
AI ReviewSummaryThis PR makes mixed Final Verdict✅ No blocking issues found in the current PR head. LLVM Coverage ReportMeasured on commit acba390.
Changed lines: Changed C/C++ lines covered: 89/101 (88.12%) · Uncovered code |
…rand-overflow-117237
…mal_check_overflow = 0` `04340_intdiv_variable_constant_monotonicity` and `04651_intdiv_const_variable_monotonicity_decimal_float` deliberately feed an integer operand that does not fit the native width of the `Decimal` result, to check that key analysis does not claim monotonicity for the wrapped quotient. Under the default `decimal_check_overflow = 1` that operand now raises `DECIMAL_OVERFLOW` instead of wrapping, so the probes failed in Fast test. The wrap still exists with the check disabled and the monotonicity guard must still hold there, so the probes keep their coverage under `decimal_check_overflow = 0`. CI: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=118721&sha=6188c9929e6cbd74f903cb756974f401f61a348b&name_0=PR&name_1=Fast%20test PR: #118721 Co-Authored-By: Claude Fable 5.1 <[email protected]>
…d in subtraction `executeDateTime64Subtraction` and `executeTime64Subtraction` stored constant operands in `UInt64`, so a pre-epoch value was widened to a huge unsigned number and the new narrowing check raised `DECIMAL_OVERFLOW`. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…rand-overflow-117237
…erflow = 0` These tests (added on master) rely on an integer operand silently wrapping when narrowed into the native width of a `Decimal32`, which now raises `DECIMAL_OVERFLOW` by default. Also add the reference for the pre-epoch `DateTime64` / `Time64` subtraction cases of `05158`. Fast test: https://github.com/ClickHouse/ClickHouse/actions/runs/35801691935/job/106993726178 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Build profile diff (arm_release)Commit See the job log for details. |
…onstants In the constant-constant decimal path, the operands were narrowed with `castConstantToNative` before the `right_nullmap` check, so `intDiv(9223372036854775807, nullIf(toDecimal32(2, 0), toDecimal32(2, 0)))` raised `DECIMAL_OVERFLOW` instead of returning `NULL`. Narrow them only when the divisor is not `NULL`. In `executeTime64Subtraction`, a plain `Time` constant was read as `UInt32`, but `Time` is backed by `Int32`, so a negative value turned into a huge positive one in mixed `Time` / `Time64` subtraction. Read it as `Int32`. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…r row is `NULL` With a constant left operand and a `Nullable` divisor column (or the reverse), the constant was narrowed into the decimal result width before the divisor null map was consulted, so `intDiv(9223372036854775807, materialize(CAST(NULL, 'Nullable(Decimal32(0))')))` raised `DECIMAL_OVERFLOW` instead of returning `NULL`. Fill the result with defaults when all divisor rows are `NULL`, as the const/const branch already does. Co-Authored-By: Claude Opus 5.5 <[email protected]>
parseDateTimeBestEffortImpl dereferences in.position() in the num_digits == 0 branch with no eof() check. The enclosing while (!in.eof()) loop only guarantees a readable byte at the top of an iteration, and the alphabetical-month branch both leaves num_digits == 0 and can consume the last byte of the value, so a value such as '1-Jan' reaches that read with the position already at the end of the buffer. Since 0bd3c24 ("Remove zero byte", ClickHouse#85063) ColumnString no longer stores a trailing terminator, so getDataAt() returns a span ending exactly at the value. The byte read is therefore the first byte of the next row, or the uninitialized right padding of the chars array when the value is the last row. When that byte is one of ' ', 'Z', '.', '+' or '-', the code advances the position past working_buffer.end(); BufferBase::available() is an unclamped size_t(end - pos), so it wraps, eof() never becomes true again, and the parser keeps consuming the neighbouring rows. The consequence differs by caller. The conversion family re-checks isAllRead(), so parseDateTimeBestEffort*, parseDateTime64BestEffort* and toDateTime with date_time_input_format = 'best_effort' reject a valid value (NULL, zero, or CANNOT_PARSE_TEXT). addMonths and the rest of the add-interval family over a String argument do not re-check, so they return date and time fields taken from the following row. Guarding the read with eof() restores the control flow the terminator used to produce: '\0' fell through to readAlpha, which returned zero characters and broke out of the loop. eof() attempts a refill first, so a value straddling a chunk boundary in a refillable ReadBuffer keeps parsing; a plain pointer comparison would have stopped early there. Reported by the memory sanitizer as use-of-uninitialized-value src/IO/parseDateTimeBestEffort.cpp:703:26 in Unit tests (msan, function_prop_fuzzer) on PR ClickHouse#116042 (914a539) and PR ClickHouse#118721 (906e40f), and at the same column in 2024 as issue ClickHouse#61771. PR ClickHouse#61694 added this same guard to the comma branch of this loop; the num_digits == 0 branch was not part of that change. Related: ClickHouse#61771
…rand-overflow-117237
|
🕵 Merged The two red jobs on
@groeneai, please investigate these failures and fix them in a separate PR. If a fix is already in progress, link it here. |
|
Reproduced on a master debug build ( CREATE TABLE t (a Nullable(Int64)) ENGINE = MergeTree ORDER BY a
SETTINGS index_granularity = 2, allow_nullable_key = 1;
INSERT INTO t SELECT NULL FROM numbers(10);
SELECT count() FROM t WHERE intDiv(a, -3) = -2; -- aborts in debugIt is neither
|
The test inserted 1,000,000 rows of 18 wide computed columns (Int256, UInt256, Decimal256 defaults, about 79 MiB compressed) and scanned them four times. Under tsan with s3 storage it exceeded the 180 s budget of the targeted lane (192 s on ClickHouse#116101, 248 s on ClickHouse#118721) and ran over 180 s 20 times on master in 30 days in `amd_tsan, s3 storage, parallel`. Insert 250,000 rows instead and regenerate reference lines 2-5. The assertions stay exact averages over the same expressions and types: 8 and 16 bit columns still wrap, and a local-disk read still uses at least 2 streams (32 marks, above the 24-mark concurrent-read minimum). The new values match a direct recomputation from numbers(250000). Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
The fix for the The AST fuzzer failure on |
… key endpoint
Index analysis evaluates a key expression at the endpoints of the key ranges, both in
the `plus`/`minus` monotonicity probe and when `applyMonotonicFunctionsChainToRange`
applies the chain (the latter evaluates the whole index column at once and is reached
without a probe for a single-point range). With the narrowing check of this PR, mixed
`Decimal`/integer arithmetic raises `DECIMAL_OVERFLOW` for an endpoint beyond the native
width of the result, which failed the whole query even when another condition
restricted it to a safe slice of the key:
SELECT * FROM t WHERE a < 2 AND a + toDecimal32(1, 0) BETWEEN 1 AND 3
Treat such an overflow in `applyMonotonicFunctionsChainToRange` as "this range cannot be
analyzed", the same as a non-monotonic function. The cached whole-column result in
`applyFunction` used to be left as a null column when the function threw, so the next
lookup dereferenced it; now the failure is cached explicitly and reported again.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
🕵 Pushed Thanks @groeneai for #124111 and #124000. The remaining red job on @groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=118721&sha=050cdee89076393df1eb4fe8bed1243f20993df8&name_0=PR&name_1=AST%20fuzzer%20%28amd_debug%2C%20targeted%29 and provide a fix in a separate PR. If the fix is already in progress, link it here. |
|
CREATE TABLE t (a UInt64) ENGINE = MergeTree ORDER BY a SETTINGS index_granularity = 2;
INSERT INTO t SELECT number FROM numbers(10);
SELECT count() FROM t WHERE divide(a, nan) = 0; -- Inconsistent KeyCondition behavior
The fix is already in progress: #118720. Its |
…nction` `ColumnWithTypeAndName` takes the column by const reference, so `std::move` had no effect. https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=118721&sha=fc621ef09e51e1262be48d06c4bf92adad403459&name_0=PR&name_1=Build%20%28arm_tidy%29 #118721 Co-Authored-By: Claude Opus 5.5 <[email protected]>
…rand-overflow-117237
|
🕵 Pushed Thanks @groeneai for tracking down |
…overflow_key_analysis` With parallel replicas the plan does not show the `Granules: N/M` line of the local read, so the test failed in the `ParallelReplicas` job. https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=118721&sha=latest&name_0=PR&name_1=Stateless%20tests%20(amd_llvm_coverage%2C%20ParallelReplicas%2C%20s3%20storage%2C%20parallel) PR: #118721 Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
🕵 Pushed: merged @groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=118721&sha=4b39c168e50772946cab8542f2a6071a471ec270&name_0=PR&name_1=AST%20fuzzer%20%28amd_debug%2C%20targeted%2C%20old_compatibility%29 ( |
|
The trigger in this run is not a CREATE TABLE t (a UInt16) ENGINE = MergeTree ORDER BY a;
INSERT INTO t SELECT number FROM numbers(65536);
SELECT count() FROM t WHERE intDiv(CAST(a, 'UInt64'), -2) = -100; -- 0, expected 2The fix is in progress: #116699. The other runs have different triggers:
|
|
🕵 Thanks @groeneai. So the |
…rand-overflow-117237 Conflicts in `KeyCondition.cpp` resolved in favor of `master`: #123535 and #117355 made `KeyCondition::applyMonotonicFunctionsChainToRange` treat any non-fatal exception at a range endpoint as "cannot analyze" and `applyFunction` cache a failure sentinel for every exception. That subsumes this PR's `DECIMAL_OVERFLOW`-only handling, so the PR's `applyMonotonicFunctionsChainToRangeImpl` split is dropped from `KeyCondition.cpp` and `KeyCondition.h`. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
🕵 Merged |
Since ClickHouse#118721 an integer constant that does not fit the native width of a Decimal result raises DECIMAL_OVERFLOW by default, so the `Array(Decimal(9, 0)) * 9223372036854775807` lines failed in the merge queue with Code 407 instead of comparing the hoisted and the original form. The wrap that makes the hoist wrong is still reachable with `decimal_check_overflow = 0`, which is how ClickHouse#118721 adjusted the sibling test 05227; do the same here. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Closes: #117237
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Arithmetic between a
Decimaland an integer that does not fit the native width of the decimal result now raisesDECIMAL_OVERFLOWinstead of silently wrapping the integer operand.Description
DecimalBinaryOperationcomputes in the native signed width of the decimal result (Int32forDecimal(5, 4)) andstatic_casts both operands into it, so an integer operand beyond that width wrapped and every row of the result was silently wrong - for constants and for columns alike. Comparing the same pair already reportsCan't compare decimal number due to overflow, and the documented contract ofDecimalis that excessive digits in the integer part raise an exception.The narrowing is checked now, under the existing
decimal_check_overflowswitch: the operation template already has acheck_overflowparameter, chosen from that setting, sodecimal_check_overflow = 0keeps the previous behaviour. Only the conversions that can lose information are checked, so a narrower integer into a wider native type costs nothing, and a wider decimal (toDecimal128(1.5, 4) * 9223372036854775807) still computes the product.Index analysis evaluates key expressions at the endpoints of key ranges, which may lie outside what the query reads. A
DECIMAL_OVERFLOWthere must only make the range unanalyzable, not fail the query.masternow handles this generally (#123535, #117355):KeyCondition::applyMonotonicFunctionsChainToRangetreats any non-fatal exception at an endpoint as "cannot analyze", so this PR no longer changesKeyConditionand only keeps the regression test05331_decimal_integer_overflow_key_analysis.Workflow [PR]
Sync PR [sync-upstream/pr/118721]
Version info
26.10.1.1903-master(included in26.10and later)