Skip to content

Fix DateTime64: throw instead of wrapping when a scale 8/9 value does not fit in Int64 - #712

Merged
alex-clickhouse merged 1 commit into
mainfrom
polyglot/datetime64-scale-overflow
Nov 16, 2026
Merged

alex-clickhouse merged 1 commit into
mainfrom
polyglot/datetime64-scale-overflow

Conversation

@polyglotAI-bot

@polyglotAI-bot polyglotAI-bot commented Nov 7, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Fixes #711.

The HTTP client writes a DateTime64 as an Int64 count of 10^-scale seconds since the epoch. DateTime64Type.ToClickHouseTicks gets that count from the .NET tick count (100 ns) with MathUtils.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 the DateTime range: DateTime64(9) before 1677-09-21 or after 2262-04-11 UTC, and DateTime64(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.WriteChecked now checks the tick count against long.MaxValue / factor and long.MinValue / factor before it scales up. Out-of-range values throw ArgumentOutOfRangeException. The DateTime and Date32 writers and the TCP codec (DateTime64ColumnCodec.CountFromDateTimeOffset) already do this. Scales 0–7 are not changed.

Changes

  • ClickHouse.Driver/Types/DateTime64Type.cs: range check in WriteChecked for 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 include DateTime64(8) and DateTime64(9).
  • changelog.d/711-datetime64-overflow.fixes.md.

Test

The tests insert through InsertBinaryAsync into a real server:

  • At the Int64 limit, the value round-trips. Covers the DateTime64(9) min and max, the DateTime64(8) max, the max as a DateTimeOffset at +01:00, and the max as an Unspecified DateTime in an Asia/Tokyo column. The test asserts the stored count with reinterpretAsInt64(t). It does not use toString(t), because 26.3 renders an instant before 1900 as 1900-01-01 (checked on 26.3.28.5).
  • One tick past the limit, the write throws ArgumentOutOfRangeException. Covers DateTime (Utc, Local, Unspecified in a non-UTC column), DateTimeOffset with an offset, DateOnly and NodaTime Instant. The test checks the type name, the range in the message and ActualValue.

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 full ClickHouse.Driver.Tests suite passes on net10.0 against 26.9. DateTimeBinaryRangeTests also passes against 26.3. The HTTP client builds for net6.0, net8.0, net9.0 and net10.0. changelog.cs --check passes.

Every binary write entry point (Write(object), the typed WriteValue overloads used by POCO inserts, and the Nullable/Array/Map/Tuple/Variant/JSON wrappers) goes through WriteChecked, so the one check covers all of them. The HTTP parameter path sends an ISO string and is not affected.

Checklist

  • Unit and integration tests covering the common scenarios were added
  • A human-readable description of the changes was provided to include in CHANGELOG

Pre-PR validation gate

  • Deterministic repro confirmed on main
  • Root cause documented above
  • Fix targets the root cause (bound check before the scale-up multiply)
  • Tests fail without the fix and pass with it
  • No existing tests changed or broken
  • Convention compliance verified per AGENTS.md and ClickHouse.Driver/AGENTS.md

Related: 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) and DateTime64(9) no longer silently store wrong timestamps when scaling .NET ticks to the wire Int64 count overflows. DateTime64Type.WriteChecked now validates against long.MaxValue / factor and long.MinValue / factor before multiply for scale > 7, throwing ArgumentOutOfRangeException with the column type and writable UTC range (scales 0–7 unchanged).

Integration tests in DateTimeBinaryRangeTests cover round-trip at Int64 limits and out-of-range failures for several input shapes (DateTime, DateTimeOffset, NodaTime Instant, 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.

… 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>
Copilot AI balanced review requested due to automatic review settings November 7, 2026 03:23

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 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

codecov Bot commented Nov 7, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@security-ai-gate-reviewer security-ai-gate-reviewer Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@alex-clickhouse
alex-clickhouse enabled auto-merge (squash) November 16, 2026 05:08
@github-actions

Copy link
Copy Markdown

AI review gate — Human approval in place of AI review

Approved on the head commit by alex-clickhouse (write access, not the author), standing in for the AI review:

  • Cursor Bugbot is still reviewing this commit.

What to do:

  • Nothing to do yet if the review is running. The gate turns green when it finishes clean.
  • No review showing up? Post bugbot run as a new top-level PR comment. If that does nothing, Bugbot is probably not enabled for this repository: ask a repository administrator.

Evaluated at 232d1100. This required workflow does not re-run on its own once the AI review later concludes — check the Cursor Bugbot check on this commit directly for its current state.

@alex-clickhouse
alex-clickhouse merged commit 3ad7df0 into main Nov 16, 2026
39 of 42 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

HTTP client: DateTime64(9) silently stores a wrapped (wrong) value for dates outside 1677-2262

3 participants