Repository navigation
Fix DateTime64: throw instead of wrapping when a scale 8/9 value does not fit in Int64 - #712
Conversation
… not fit in Int64 The binary write path scaled the 100 ns tick count up with an unchecked multiply. For DateTime64(9) (and DateTime64(8) after 4892-10-07) a value outside the Int64 range at that scale wrapped, and a different instant was stored with no error. WriteChecked now checks the bound before scaling up and throws ArgumentOutOfRangeException with the value, the column type and the range, like the Date/Date32/DateTime writers and the TCP codec. Fixes: #711 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
There was a problem hiding this comment.
🟢 Approval recommended
There are no active review issues for the DateTime64 overflow-validation change.
No open issues 🎉
What changed in this PR
Fixes #711 in the HTTP client by rejecting binary DateTime64(8/9) writes whose scaled timestamp exceeds Int64 limits, preventing silent timestamp corruption. Scales 0–7 remain unchanged.
Changes:
- Adds overflow validation before scaling, with errors reporting the original value, column type, and writable range.
- Adds server-backed tests for boundary values and out-of-range inputs, including timezone and offset conversions.
- Documents the limits and records the behavior change in a changelog fragment.
| File | Description |
|---|---|
docs/http.mdx |
Documents DateTime64 write limits and out-of-range exceptions. |
ClickHouse.Driver/Types/DateTime64Type.cs |
Checks Int64 bounds before scaling and formats range errors. |
ClickHouse.Driver.Tests/Types/DateTimeBinaryRangeTests.cs |
Adds boundary round-trip and rejection tests for scales 8–9. |
changelog.d/711-datetime64-overflow.fixes.md |
Records the overflow fix and new exception behavior. |
Review effort: Balanced
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
There was a problem hiding this comment.
Automated approval by the AI review gate.
The required AI review gate concluded SUCCESS on 232d110, and this change touches no path that requires a human reviewer. This approval is posted by an automated identity (security-ai-gate-reviewer[bot]), not a human, and is bound to the evaluated commit. A new push dismisses it and re-evaluates.
repo=ClickHouse/clickhouse-cs pr=#712 head=232d1100a6d0e3243efad43830d7a23bd0e33306
AI review gate — Human approval in place of AI reviewApproved on the head commit by alex-clickhouse (write access, not the author), standing in for the AI review:
What to do:
Evaluated at |
Summary
Fixes #711.
The HTTP client writes a
DateTime64as an Int64 count of 10^-scale seconds since the epoch.DateTime64Type.ToClickHouseTicksgets that count from the .NET tick count (100 ns) withMathUtils.ShiftDecimalPlaces, which multiplies by 10^(scale-7) with no overflow check. Scales 8 and 9 are finer than a tick, so they reach the Int64 limit inside theDateTimerange:DateTime64(9)before 1677-09-21 or after 2262-04-11 UTC, andDateTime64(8)after 4892-10-07 UTC. For those values the multiply wrapped, and the server stored a different instant with no error (for example, 1500-06-01 was stored as 2084-12-18 at scale 9).DateTime64Type.WriteCheckednow checks the tick count againstlong.MaxValue / factorandlong.MinValue / factorbefore it scales up. Out-of-range values throwArgumentOutOfRangeException. TheDateTimeandDate32writers and the TCP codec (DateTime64ColumnCodec.CountFromDateTimeOffset) already do this. Scales 0–7 are not changed.Changes
ClickHouse.Driver/Types/DateTime64Type.cs: range check inWriteCheckedfor scale > 7. The exception reports the caller's original value (ActualValue), the column type and the range that can be written. The old comment ("No range check: any coerced instant is representable") was wrong and is removed. For scale > 7 the count is computed once (ticks * factor) after the check.ClickHouse.Driver.Tests/Types/DateTimeBinaryRangeTests.cs: DateTime64 cases next to the existing Date/Date32/DateTime range tests.docs/http.mdx: the write type map and the "Out-of-range values" note now includeDateTime64(8)andDateTime64(9).changelog.d/711-datetime64-overflow.fixes.md.Test
The tests insert through
InsertBinaryAsyncinto a real server:DateTime64(9)min and max, theDateTime64(8)max, the max as aDateTimeOffsetat +01:00, and the max as anUnspecifiedDateTimein anAsia/Tokyocolumn. The test asserts the stored count withreinterpretAsInt64(t). It does not usetoString(t), because 26.3 renders an instant before 1900 as 1900-01-01 (checked on 26.3.28.5).ArgumentOutOfRangeException. CoversDateTime(Utc, Local, Unspecified in a non-UTC column),DateTimeOffsetwith an offset,DateOnlyand NodaTimeInstant. The test checks the type name, the range in the message andActualValue.The expected counts and boundary dates come from the Int64 range (±9223372036854775800 at tick precision), not from client output. All past-limit cases fail on
main(no exception); all cases pass with the fix. The fullClickHouse.Driver.Testssuite passes on net10.0 against 26.9.DateTimeBinaryRangeTestsalso passes against 26.3. The HTTP client builds for net6.0, net8.0, net9.0 and net10.0.changelog.cs --checkpasses.Every binary write entry point (
Write(object), the typedWriteValueoverloads used by POCO inserts, and the Nullable/Array/Map/Tuple/Variant/JSON wrappers) goes throughWriteChecked, so the one check covers all of them. The HTTP parameter path sends an ISO string and is not affected.Checklist
Pre-PR validation gate
mainAGENTS.mdandClickHouse.Driver/AGENTS.mdRelated: ClickHouse/clickhouse-java#3292 (the report this issue was relayed from).
🤖 Generated with Claude Code
Note
Medium Risk
Changes core binary serialization for high-scale DateTime64; bounds are conservative and align with existing Date/DateTime checks, but any caller relying on previously wrapped values will now get exceptions.
Overview
Binary inserts for
DateTime64(8)andDateTime64(9)no longer silently store wrong timestamps when scaling .NET ticks to the wire Int64 count overflows.DateTime64Type.WriteCheckednow validates againstlong.MaxValue / factorandlong.MinValue / factorbefore multiply for scale > 7, throwingArgumentOutOfRangeExceptionwith the column type and writable UTC range (scales 0–7 unchanged).Integration tests in
DateTimeBinaryRangeTestscover round-trip at Int64 limits and out-of-range failures for several input shapes (DateTime,DateTimeOffset, NodaTimeInstant,DateOnly, timezone/unspecified cases). Docs and changelog #711 document the accepted ranges and binary write behavior.Reviewed by Cursor Bugbot for commit 232d110. Bugbot is set up for automated code reviews on this repo. Configure here.