fix(proto): store far-future DateTime64 without int64 overflow - #1184
Open
dogacid wants to merge 1 commit into
Open
fix(proto): store far-future DateTime64 without int64 overflow#1184dogacid wants to merge 1 commit into
dogacid wants to merge 1 commit into
Conversation
ToDateTime64 and DateTime64.Time convert through t.UnixNano() / an int64 nanosecond count, which overflows just past 2262-04-11 and corrupts any later timestamp on both the write and read paths (e.g. ClickHouse 26.7's 9999-12-31 ceiling for DateTime64(3) was written back as ~1900). Compute the tick count at the column's own scale instead, so each precision reaches exactly ClickHouse's DateTime64(precision) range. Nanosecond precision still caps near 2262 where an int64 count of nanoseconds runs out, matching ClickHouse itself; the result is identical for all in-range values. Add far-future round-trip tests covering the 2262 boundary and the 9999 ceiling across precisions. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
proto.ToDateTime64andDateTime64.Timeconvert a timestamp through anint64nanosecondcount (
t.UnixNano()on write,int64(d) * p.Scale()on read). That intermediate overflowsint64just past 2262-04-11 23:47:16 UTC, so any later timestamp is silently corrupted — on the way in
and out — even for precisions whose stored tick would fit
int64across a far wider range.ClickHouse 26.7 extended the
DateTime64range (millisecond precision now stores up to9999-12-31), so this is reachable in ordinary use. Concretely, writing an open-ended sentinel suchas
9999-12-31 23:59:59.999into aDateTime64(3)column currently round-trips as1900-…garbage.Root cause
A
DateTime64(P)value is stored as anint64count of ticks of10^-Pseconds. The conversionsfirst scale the whole timestamp to nanoseconds:
t.UnixNano()(and the* p.Scale()on read) overflowint64at ~2262 — even though the final tickcount at, say, millisecond precision would not overflow until roughly the year 292 million.
Fix
Compute at the column's own tick scale instead of via nanoseconds, so no oversized intermediate is
ever formed:
intermediate never exceeds the tick magnitude.
int64-tickDateTime64(P)supports. Coarser precisions reach much further; nanosecond precision (9) stillcaps near 2262, which is the genuine
int64-nanosecond limit and matches ClickHouse's ownDateTime64(9).t.IsZero()short-circuit is preserved.Tests
Adds
TestDateTime64_FarFuture: round-trips the 2262 boundary,2999, and the9999-12-31ceilingacross precisions 0/1/3/6/7, plus a millisecond-fraction case. These fail on
maintoday(e.g.
9999-12-31 → 1816-03-30) and pass with the fix. Existing tests are unchanged.Notes
No measurable performance impact (a couple of extra integer operations; no allocation).