Repository navigation
Fix wrapped Time64 values from an overflowing scale conversion in convertFieldToType - #114546
Conversation
… overflow The Time64 <- Decimal64 branch of convertFieldToTypeImpl rescaled the source value with an unchecked multiply, so a value above INT64_MAX / scale_multiplier wrapped. The wrapped product was then handed to decimalFromComponentsWithMultiplier<Time64>(value, 0, 1), whose own mulOverflow check is vacuous at multiplier 1, so the single downstream guard could not catch it and the corrupted value became the field. Values 'x Time64(6)' (253402207200000::Decimal64(0)) returned -999:59:59.722624, a positive input as a negative time, and INSERT persisted it; the IN-set path silently matched nothing. Explicit CAST to Time64 already reported DECIMAL_OVERFLOW for the same input, so the literal path disagreed with CAST. This file carried the same statement twice, for DateTime64 and Time64, both unguarded. 2f482ef guarded the DateTime64 one with Int64 + common::mulOverflow + DECIMAL_OVERFLOW and added test 03797, leaving its Time64 twin below untouched. Mirror that guard here so the two branches agree. The operand also becomes Int64, which the guard requires: common::mulOverflow on an unsigned operand reports overflow for every negative value, so keeping UInt64 would reject in-range negative times. The previous UInt64 was not itself losing information, since the helper takes const Int64 & and the conversion is modular. Only inputs that were producing wrong values change behaviour. The largest value whose rescale still fits, 9223372036854 at scale 6, is still accepted and returns 999:59:59.000000 exactly as before; 9223372036855 is now reported. Verified by building both arms and diffing them: every overflow arm goes from a wrong value to DECIMAL_OVERFLOW while thirty in-range controls across scales 0/3/6/9, both signs and both scale directions are byte-identical. Tests 03797 and 04837 pass unchanged; the deliberately-Null Date32 branches are untouched. Co-Authored-By: Claude Opus 5 <[email protected]>
Internal second-model review (click to expand)Two independent cold reviews plus two second-model gate passes, adjudicated before publishing.
Reviewer never edits source: the tree is byte-identical to what the gate reviewed, verified by Session id: cron:clickhouse-review-slot-10:20260812-205500 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-41:20260812-183700 |
|
Workflow [PR], commit [e74ea7e] Summary: ✅
AI ReviewSummaryThis PR closes the unchecked Final Verdict✅ No new review findings. LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 11/11 (100.00%) · Uncovered code |
|
cc @yariks5s @KochetovNicolai, could you review this? The Time64 <- Decimal64 branch of convertFieldToTypeImpl rescaled with an unchecked multiply, so a value past INT64_MAX/scale_multiplier wrapped and a positive literal became a negative time that INSERT persisted; the one downstream guard is a no-op because that call passes multiplier 1. This mirrors the Int64 + common::mulOverflow guard already applied to the DateTime64 twin in #94537. |
alexey-milovidov
left a comment
There was a problem hiding this comment.
Good. Thanks for the quick fix!
The `insert into t_04883 values (253402207200000::Decimal64(0))` arm expected `serverError DECIMAL_OVERFLOW`, but an inline `VALUES` list of an `INSERT` is parsed by `clickhouse-client`, so the guard fires inside the client and the overflow is reported as a client error. `Fast test` failed with `Expected server error code '407' but got no server error`. Which process performs the conversion is not what this test pins down, so the arm now goes through `format`, like every other arm in the file, which keeps the conversion on the server. Verified against a binary without the guard: the same `INSERT` persists `-999:59:59.722624` there, so the arm still fails on master and still asserts that no wrapped value is persisted. Report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=114546&sha=c06656ca2d3a2ba0618998737a438d1581e3b199&name_0=PR&name_1=Fast%20test
|
🕵 Pushed e74ea7e to make The failing arm was the persistence one: An inline Which side performs the conversion is not stable per type, so it is not worth pinning: against a running server, the same query text targeting insert into t_04883 select * from format(Values, 'x Time64(6)', '(253402207200000::Decimal64(0))'); -- { serverError DECIMAL_OVERFLOW }Both halves verified against a running server: this shape reports The Also added to the description the two
|
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
Only the stripped binary is compared: the official master build keeps debug symbols while PR builds strip them, so the other binaries differ by construction. Compile time of recompiled translation units7 translation units recompiled, 11 s compile time in total, 7 of them have a recent master baseline. |
CI finish ledger - e74ea7eCI has fully finished on this head with no failures. The praktika report is
The earlier Session id: cron:our-pr-ci-monitor:20260813-083000 |
Related: #94537
Related: #111534
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixes incorrect, often sign-flipped,
Time64literals andIN-list constants produced when rescaling a lower-scaleDecimal64overflowedInt64. Such a conversion now reportsDECIMAL_OVERFLOW, matching theDateTime64branch and explicitCAST.Description
convertFieldToTypeImplrescales aDecimal64-backed field into aTime64column atsrc/Interpreters/convertFieldToType.cpp:473-476. The scale-increasing arm multiplied without a range check, sovalue * scale_multiplier_diffcould exceedInt64and wrap. The wrapped product was then handed todecimalFromComponentsWithMultiplier<Time64>(value, 0, 1), whose ownmulOverflowcheck is a no-op at multiplier1, so the corrupted value became the field.Observed on master (
bb1bd307):Values 'x Time64(6)' (253402207200000::Decimal64(0))returned-999:59:59.722624, andINSERTpersisted it. The wrappedInt64of253402207200000 * 1000000is-4852209831933722624, which renders as exactly that; the negated input wraps to+4852209831933722624, so a negative input returned a positive time. ExplicitCAST(... AS Time64(6))already reportedDECIMAL_OVERFLOWhere, so the literal path disagreed withCAST.The file carried this same statement twice, for
DateTime64andTime64, both unguarded. The related PR above guarded theDateTime64one and added test03797; itsTime64twin was left as it was. This change mirrors that guard onto the twin. The operand also becomesInt64, which the guard requires:mulOverflowon an unsigned operand reports overflow for every negative value, which would reject in-range negative times.Reporting rather than returning Null matches the
DateTime64twin, which03797asserts, and explicitCAST. The neighbouringDate32branches keep their Null contract and are untouched.Found by a UBSan signed-overflow report on this line; there is no open issue for it. It keeps reproducing on
master, in bothasan_ubsanstress jobs, assigned integer overflow: 253402239600000 * 1000000 cannot be represented in type 'long'atsrc/Interpreters/convertFieldToType.cpp:475:Only inputs that were producing wrong values change:
9223372036854at scale 6, the largest whose rescale still fits, is still accepted and returns999:59:59.000000identically. Verified by building both arms and diffing: every overflow arm goes from a wrong value toDECIMAL_OVERFLOW, while 30 in-range controls across scales 0/3/6/9, both signs and both scale directions are byte-identical.03797and04837still pass. New test04883fails on master and is green here over 100 randomized runs.