Repository navigation
Fix DECIMAL_OVERFLOW in changeYear/changeXxx for nanosecond DateTime64 - #108681
alexey-milovidov merged 3 commits into
Conversation
changeYear, changeMonth, changeDay, changeHour, changeMinute and changeSecond share getChangedDate(), which precomputes the max_date boundary for year 2299 as a DateTime64. At scale 9 that value (2299 * 10^9 seconds) does not fit into Int64, so DecimalUtils::dateTimeFromComponents threw DECIMAL_OVERFLOW before the actual result was ever computed and every scale-9 input failed. Use the non-throwing tryGetDateTimeFromComponents and fall back to std::numeric_limits<Int64>::max() (2262-04-11 23:47:16.854775807, the maximum representable scale-9 DateTime64) when the year-2299 boundary overflows. Behavior is unchanged for scales <= 8. Closes ClickHouse#72019
|
Workflow [PR], commit [c1703cf] Summary: ✅
AI ReviewSummaryThis PR now fixes two PR Metadata
Missing context / blind spots
Final Verdict
|
The previous fix clamped only the precomputed `max_date` upper bound, but
the actual candidate timestamp was still built with the throwing
`DecimalUtils::dateTimeFromComponents`. For an in-range component change
whose result lands past the maximum representable scale-9 value
(`2262-04-11 23:47:16.854775807`) but at or below the `max_year` of 2299,
the early `return max_date` clamp is skipped, so the throwing construction
was reached and `changeYear(toDateTime64('2024-01-01 00:00:00.000000000', 9), 2299)`
still failed with `DECIMAL_OVERFLOW`. The same applied to `changeMonth`,
`changeDay`, `changeHour`, `changeMinute` and `changeSecond` when an
in-range component change moved a value past the boundary.
Build the final candidate with the non-throwing `tryGetDateTimeFromComponents`
and, on overflow, clamp to the nearest representable boundary (the sign of the
whole-seconds value picks `min_date`/`max_date`); the existing min/max checks
finish the clamp. Behavior is unchanged for scales <= 8 and for values that
fit.
Extends the regression test `04413_changeyear_nanosecond_datetime64` with
years 2263-2299 and representable-to-over-boundary component changes.
Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
Pushed a follow-up commit ( What was still wrong: the previous change clamped only the precomputed Fix: build the final candidate with the non-throwing Tests: extended The only red check on the previous commit was |
The `DateTime64` path in `changeDate.cpp` split the stored value into whole
seconds and a fraction with plain `/` and `%`, which truncate toward zero.
For pre-epoch values with a non-zero fraction the whole-seconds part was
rounded toward zero instead of toward negative infinity, so the calendar
fields were extracted from the next calendar boundary. For example,
`changeYear(toDateTime64('1969-12-31 23:59:59.500', 3, 'UTC'), 1969)`
decomposed the input as `1970-01-01 00:00:00` minus 500 ms and returned
`1968-12-31 23:59:59.500` instead of leaving the value untouched. The same
one-boundary shift affected `changeMonth`, `changeDay`, `changeHour`,
`changeMinute` and `changeSecond` for any pre-epoch value in the last
fractional second before the relevant boundary (and `changeSecond` for any
pre-epoch sub-second value).
Round the whole-seconds part toward negative infinity and carry the borrow
into a non-negative fraction before extracting the calendar components. This
matches the additive-fraction convention used by
`DecimalUtils::dateTimeFromComponents`. Positive values and values that are
exactly on a whole second are unaffected.
Extends test `04413_changeyear_nanosecond_datetime64` with pre-epoch
sub-second regression cases at scale 3 and scale 9.
This addresses the AI review finding on
ClickHouse#108681
|
Pushed a follow-up commit ( What was wrong: the Fix: round the whole-seconds part toward negative infinity and carry the borrow into a non-negative fraction before extracting the calendar components (matching the additive-fraction convention of Verified locally on a debug build ( |
|
The two red The bugfix-validation job downloads a prebuilt Evidence that this is infrastructure, not the change:
The rest of CI is still in progress. |
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 30/31 (96.77%) · Uncovered code |
bdc4547
The regression test `04413_changeyear_nanosecond_datetime64` came from `master` (via #108681) through today's merge and still encoded the former `[1900-01-01, 2299-12-31]` clamp boundaries. After this PR widens `DateTime64` to `[0000-01-01, 9999-12-31]`, the out-of-range `changeXxx` clamp results legitimately change: - At scale 9 the range is limited by the `Int64` tick storage to `[1677-09-21 00:12:43.145224192, 2262-04-11 23:47:16.854775807]`, so an invalid or out-of-range component now clamps to the true scale-9 minimum `1677-09-21 00:12:43.145224192` instead of the former `1900-01-01`. This is symmetric with the scale-9 maximum `2262-04-11 23:47:16.854775807` that the reference already accepts. - At scale 3 and 6 the year `2500` is now representable, so `changeYear(..., 2500)` returns `2500-01-01` instead of the former clamp `2299-12-31`. `getChangedDate` in `changeDate.cpp` already computes these boundaries correctly; only the reference was stale (the earlier merge assumed this test still passed). Verified against the deterministic CI output, which failed identically across every stateless configuration: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=107907&sha=2e78058b12f04857839a021b535ec9e11d687b0b&name_0=PR Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
| @@ -0,0 +1,74 @@ | |||
| -- Tags: no-fasttest | |||
The `DateTime64` path in `changeDate.cpp` split the stored value into whole
seconds and a fraction with plain `/` and `%`, which truncate toward zero.
For pre-epoch values with a non-zero fraction the whole-seconds part was
rounded toward zero instead of toward negative infinity, so the calendar
fields were extracted from the next calendar boundary. For example,
`changeYear(toDateTime64('1969-12-31 23:59:59.500', 3, 'UTC'), 1969)`
decomposed the input as `1970-01-01 00:00:00` minus 500 ms and returned
`1968-12-31 23:59:59.500` instead of leaving the value untouched. The same
one-boundary shift affected `changeMonth`, `changeDay`, `changeHour`,
`changeMinute` and `changeSecond` for any pre-epoch value in the last
fractional second before the relevant boundary (and `changeSecond` for any
pre-epoch sub-second value).
Round the whole-seconds part toward negative infinity and carry the borrow
into a non-negative fraction before extracting the calendar components. This
matches the additive-fraction convention used by
`DecimalUtils::dateTimeFromComponents`. Positive values and values that are
exactly on a whole second are unaffected.
Extends test `04413_changeyear_nanosecond_datetime64` with pre-epoch
sub-second regression cases at scale 3 and scale 9.
This addresses the AI review finding on
ClickHouse#108681
…anosecond-datetime64-overflow Fix DECIMAL_OVERFLOW in changeYear/changeXxx for nanosecond DateTime64
The regression test `04413_changeyear_nanosecond_datetime64` came from `master` (via ClickHouse#108681) through today's merge and still encoded the former `[1900-01-01, 2299-12-31]` clamp boundaries. After this PR widens `DateTime64` to `[0000-01-01, 9999-12-31]`, the out-of-range `changeXxx` clamp results legitimately change: - At scale 9 the range is limited by the `Int64` tick storage to `[1677-09-21 00:12:43.145224192, 2262-04-11 23:47:16.854775807]`, so an invalid or out-of-range component now clamps to the true scale-9 minimum `1677-09-21 00:12:43.145224192` instead of the former `1900-01-01`. This is symmetric with the scale-9 maximum `2262-04-11 23:47:16.854775807` that the reference already accepts. - At scale 3 and 6 the year `2500` is now representable, so `changeYear(..., 2500)` returns `2500-01-01` instead of the former clamp `2299-12-31`. `getChangedDate` in `changeDate.cpp` already computes these boundaries correctly; only the reference was stale (the earlier merge assumed this test still passed). Verified against the deterministic CI output, which failed identically across every stateless configuration: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=107907&sha=2e78058b12f04857839a021b535ec9e11d687b0b&name_0=PR Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed
DateTime64bugs inchangeYear,changeMonth,changeDay,changeHour,changeMinuteandchangeSecond: nanosecond-precision (scale 9) inputs no longer throwDECIMAL_OVERFLOW, and pre-epoch sub-second inputs now return the correct calendar second.Summary
changeYear,changeMonth,changeDay,changeHour,changeMinuteandchangeSecondsharegetChangedDate. This PR fixes twoDateTime64issues in that shared path:Overflow on every scale-9 input.
getChangedDateprecomputes themax_dateboundary for year 2299 as aDateTime64. At scale 9 that value (2299 * 10^9seconds) does not fit intoInt64, soDecimalUtils::dateTimeFromComponentsthrewDECIMAL_OVERFLOWbefore the result was ever computed, and every scale-9 input failed. The fix builds both the boundary and the final candidate with the non-throwingtryGetDateTimeFromComponents, and on overflow clamps to the nearest representable boundary (std::numeric_limits<Int64>::max()=2262-04-11 23:47:16.854775807, the maximum representable scale-9DateTime64, or the minimum for negative values).Pre-epoch sub-second split. The
DateTime64input path split the stored value into whole seconds and a fraction with plain/and%, which truncate toward zero. For a pre-epoch value with a non-zero fraction the whole-seconds part was rounded toward zero instead of toward negative infinity, so the calendar fields were read off the next boundary and the resulting second was wrong (e.g.changeSecond('1969-12-31 23:59:58.500', 30)returned...:29.500instead of...:30.500). The fix floor-normalizes the split (adds one degree to a negative fraction and decrements the whole seconds), so the calendar fields match the actual instant. This affects any sub-second scale, including 3 and 9.Closes #72019
Version info
26.7.1.417(included in26.7and later)