Repository navigation
Fix toUTCTimestamp next to a daylight saving time change - #128744
Conversation
`toUTCTimestamp` reads its argument as a wall clock in the given time zone, but took the offset with `DateLUTImpl::timezoneOffset`, which is the offset at a UTC instant. The instant it looked at was the local time read as UTC, which is |offset| away from the real one, so in the hours around each offset change the offset from the other side of the change was applied (one hour off in Europe/Berlin and America/New_York, both for DateTime and DateTime64). `parseDateTime` had the same bug, fixed in ClickHouse#126704. Add `DateLUTImpl::timezoneOffsetAtLocalTime`, the offset at a local time, resolved as `makeDateTime` does (a repeated local time is its first occurrence, a skipped one is read with the offset after the change), and use it in the `toUTCTimestamp` direction. The result is now the same as `toDateTime(s, time_zone)`. `fromUTCTimestamp` converts from a UTC instant and keeps using `timezoneOffset`. `03891_utc_timestamp_datetime64_overflow` asserted the old value for New York local 1969-10-26 05:59:59.999 (EST, so 10:59:59.999 UTC); the floor of negative fractions is now checked at the 01:59:59.999 boundary instead. Closes ClickHouse#128266 Co-Authored-By: Claude Opus 5.5 <[email protected]>
…t have one With `t == day * 86400 + time_of_day`, the previous return value `t - (date + (time_of_day >= time_at ? time_of_day - amount : time_of_day))` equals `day * 86400 - date + (time_of_day >= time_at ? amount : 0)`, and adding `amount` when it is 0 is a no-op, so guarding with `amount != 0 &&` changes nothing. This is the shape of `timezoneOffset`, and it keeps `toUTCTimestamp` from decoding the offset-change fields on every row. Also test local times outside the years 1900-2299 (every 15 minutes of 2300 in Europe/Berlin and America/New_York, and of 1883 in America/New_York), where the conversion goes through `timezoneOffsetAtLocalTimeOutOfRange`. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Remove a comment in timezoneOffsetAtLocalTimeOutOfRange that restated the code below it. 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; 2 rounds, 3 findings) plus my own review (3 rounds).
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-10:20261102-191000 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-5:20261102-140100 |
|
cc @alexey-milovidov, could you review this and add the |
|
Workflow [PR], commit [47a30c4] Summary: ✅
AI ReviewSummaryThe change fixes Final VerdictStatus: ✅ Approve |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Slowest function optimization changes (ThinLTO)Median per-function time ratio to the master baseline:
Compile time of recompiled translation units2918 translation units recompiled, 17294 s compile time in total, 2918 of them have a recent master baseline. |
Use `%` for the time of day, and look the out-of-range local time up with cctz directly instead of decomposing it for makeDateTimeOutOfRange. Both are behaviour-preserving: `t - (t / 86400) * 86400 == t % 86400`, and the clamped civil time is already within makeDateTimeOutOfRange's year range, so its year clamp was a no-op here. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Backport #128744 to 26.9: Fix `toUTCTimestamp` next to a daylight saving time change
Backport #128744 to 26.8: Fix `toUTCTimestamp` next to a daylight saving time change
Backport #128744 to 26.10: Fix `toUTCTimestamp` next to a daylight saving time change
Closes: #128266
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed
toUTCTimestamp(to_utc_timestamp) returning a time one hour off for local times in the hours around a daylight saving time change, for bothDateTimeandDateTime64. It now converts a local time astoDateTime(s, time_zone)does, also a local time that happens twice or does not exist. Closes #128266.Description
On 26.8.21.12 and 26.10.2.103,
toUTCTimestamp(toDateTime('2026-10-25 01:00:00'), 'Europe/Berlin')returns2026-10-25 00:00:00instead of2026-10-24 23:00:00(session time zoneUTC). The window is about 3 hours a year in Berlin, 9 in New York and 21 in Sydney.toUTCTimestampreads its argument as a wall clock intime_zone, but took the offset withDateLUTImpl::timezoneOffset, the offset at a UTC instant. It looked at the local time read as UTC, which is |offset| away from the real instant, so next to a change it applied the offset from the other side.parseDateTimehad the same bug (#126704).The fix adds
DateLUTImpl::timezoneOffsetAtLocalTime, the offset at a local time resolved asmakeDateTimeresolves it, and uses it fortoUTCTimestamponly;fromUTCTimestampgets a UTC instant and is unchanged. Behaviour change by design: Berlin2026-10-25 02:30(happens twice) was01:30UTC and is now00:30, astoDateTimeand Spark; New York2026-03-08 02:30(does not exist) was07:30UTC and is now06:30, astoDateTimeandparseDateTime(Spark moves a skipped time forward by the gap, which gives07:30).03891_utc_timestamp_datetime64_overflowasserted the old value at one New York boundary; it is corrected and gains a line that checks the floor of negative fractions.Validation: the new test fails on master and passes with the fix. Over all 598 time zones (every 30 minutes,
DateTime1970-2106,DateTime641900-1970, plus 20 zones over 1900-2299) the result equalsmakeDateTimefor all 2.3 billion local times (master: 688917 mismatches). Cost on the shapes oftests/performance/utc_timestamp_transform.xml(10M rows, one thread, four runs on two cores):toUTCTimestampDateTime-9% to -15%,DateTime64-1% to -9%;fromUTCTimestampDateTime-1% to +4%,DateTime64+3% to +11% (its lookup is unchanged; compiling each direction separately was slower still).Workflow [PR]
Sync PR [sync-upstream/pr/128744]
Version info
26.11.1.2207-master(included in26.11and later)26.10.11.13,26.9.37.7,26.8.45.6