Time/Time64: a TimeSpan is rounded to the column scale by the HTTP insert and both parameter formatters, but truncated by the TCP insert and the server

Open
#767 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

@XSY-28 is already working on this.

Since Nov 25, 2026.

  • #771 by @XSY-28 — open

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
48/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
csharp
Domain
databases

Research direction

Start with the reproduction program in the issue against a live server to see the six diverging rows, then read ClickHouse.Driver/Types/Time64Type.cs (ToClickHouseDecimal, WriteValue, FormatTime64String), ClickHouse.Driver/Types/TimeType.cs and ClickHouse.Driver.Tcp/Parameters/TcpParameterFormatter.cs (FormatTime64, FormatTime). Each calls Math.Round where the server truncates, so the open decision is whether to align every path on truncation and update the two tests that assert rounding (Time64TypeTests.cs:367, TimeTypeTests.cs:101). Done means all write paths, including TimeOnly.MaxValue at Time64(3), store what the server's own conversion stores. Note the related open issues #666, #674 and #677 already rework the TCP write conversion, so coordinate before editing that path.

Written by the indexing model from the issue text.

Description

bug data-types
Describe the bug

A TimeSpan or TimeOnly with more fractional digits than a Time64(p) column is stored as two different values, depending on the write path, with no error:

  • The HTTP insert (InsertBinaryAsync), the HTTP query parameter and the TCP query parameter round the value to p digits.
  • The TCP insert (ClickHouseTcpClient.InsertAsync) truncates it toward zero, as the server does.

For example, 01:02:03.4567891 in Time64(3) is stored as 01:02:03.457 or as 01:02:03.456. Negative values split the same way (-01:02:03.457 against -01:02:03.456). On a sweep of scales 0 to 9, every scale below 7 shows the split when the dropped digits round up. Scales 7 to 9 hold every digit of a TimeSpan. Time splits at whole seconds: 01:02:03.7 is stored as 01:02:04 over HTTP and both parameters, and as 01:02:03 by the TCP insert.

Rounding can also give a value above every digit that the caller wrote. TimeOnly.MaxValue (23:59:59.9999999) written over HTTP to Time64(3) is stored as 24:00:00.000, which is not a time of day.

Steps to reproduce
  1. Run the code example (net10.0, project references to ClickHouse.Driver, ClickHouse.Driver.Common and ClickHouse.Driver.Tcp).
  2. Compare the stored values with the server's own conversion of the same text (below).
Expected behaviour

All write paths store the value that the server stores for the same text. The server truncates toward zero when it reduces the scale of a Time64: a query parameter, toTime64, CAST, and text in VALUES, TSV and JSONEachRow all give 01:02:03.456:

$ curl 'http://localhost:8123/?enable_time_time64_type=1&param_p=01:02:03.4567891' \
  --data-binary "SELECT toString({p:Time64(3)}), toString(toTime64('01:02:03.4567891', 3)), toString(CAST(toTime64('01:02:03.4567891', 7) AS Time64(3)))"
01:02:03.456	01:02:03.456	01:02:03.456

The TCP insert of Time and Time64 truncates too, and so do the HTTP insert and both parameters for DateTime64 (tested at scales 0 to 6).

Code example
Reproduction program (C#, 23 lines)
using ClickHouse.Driver;
using ClickHouse.Driver.ADO.Parameters;
using ClickHouse.Driver.Tcp;
using ClickHouse.Driver.Utility;

var http = new ClickHouseClient("Host=clickhouse;Port=8123;set_enable_time_time64_type=1");
var tcp = new ClickHouseTcpClient("Host=clickhouse;Port=9000;set_enable_time_time64_type=1");
await http.ExecuteNonQueryAsync("CREATE OR REPLACE TABLE t (k String, v Time64(3)) ENGINE=Memory");

var v = TimeSpan.Parse("01:02:03.4567891");
await http.InsertBinaryAsync("t", ["k", "v"], [["http insert", v]]);
await tcp.InsertAsync("INSERT INTO t (k, v) VALUES",
    [ClickHouseTcpColumn.Create("k", ["tcp insert"]), ClickHouseTcpColumn.Create("v", [v])]);
var hp = new ClickHouseParameterCollection(); hp.AddParameter("p", v);
await http.ExecuteNonQueryAsync("INSERT INTO t SELECT 'http parameter', {p:Time64(3)}", hp);
await tcp.ExecuteAsync("INSERT INTO t SELECT 'tcp parameter', {p:Time64(3)}",
    new ClickHouseTcpQueryOptions { Parameters = new() { { "p", v } } });
await http.InsertBinaryAsync("t", ["k", "v"], [["http insert TimeOnly.MaxValue", TimeOnly.MaxValue]]);
await tcp.InsertAsync("INSERT INTO t (k, v) VALUES",
    [ClickHouseTcpColumn.Create("k", ["tcp insert TimeOnly.MaxValue"]), ClickHouseTcpColumn.Create("v", [TimeOnly.MaxValue])]);

using var r = await http.ExecuteReaderAsync("SELECT k, toString(v) FROM t ORDER BY k");
while (r.Read()) Console.WriteLine($"{r.GetString(0),-30} {r.GetString(1)}");
Error log

No error. Output:

http insert                    01:02:03.457
http insert TimeOnly.MaxValue  24:00:00.000
http parameter                 01:02:03.457
tcp insert                     01:02:03.456
tcp insert TimeOnly.MaxValue   23:59:59.999
tcp parameter                  01:02:03.457
Root cause
  • HTTP insert and HTTP parameter: ClickHouse.Driver/Types/Time64Type.cs:95 (ToClickHouseDecimal, used by WriteValue and FormatTime64String) calls Math.Round(seconds, Scale). ClickHouse.Driver/Types/TimeType.cs:39, :53-54 and :65 call Math.Round(TotalSeconds).
  • TCP parameter: ClickHouse.Driver.Tcp/Parameters/TcpParameterFormatter.cs:611 (FormatTime64) and :590 (FormatTime) round too.

The fix needs one decision: which rule all paths use. Truncation matches the server and the TCP insert. Two HTTP unit tests require rounding: Time64TypeTests.ToClickHouseDecimal_Scale0_NoDecimalPlaces (ClickHouse.Driver.Tests/Types/Time64TypeTests.cs:367) and TimeTypeTests.Write_TimeSpanWithMilliseconds_RoundsToNearestSecond (TimeTypeTests.cs:101).

Polyglot AI can open a fix PR if a maintainer assigns this issue to @polyglotAI-bot.

Related: #666, #674 and #677 (open) rewrite the TCP write conversion of Time and Time64, and keep truncation toward zero.

Configuration
Environment
  • Client version: main at 743e442f
  • .NET version: net10.0 (SDK 10.0.404)
  • OS: Linux (container)
ClickHouse server
  • ClickHouse Server version: 26.10.4.207
  • ClickHouse Server non-default settings, if any: enable_time_time64_type=1

Classification: impact: Data integrity · area: Data types & formats (Time, Time64)


Found by automated analysis (Polyglot AI) while working on ClickHouse/ch-go#1248. Verified against a live server.

Dominant language
C#
Stars
102
Forks
31
Avg merge
2d 13h
Merged PRs (30d)
40

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from ClickHouse/clickhouse-cs

All issues in ClickHouse/clickhouse-cs

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.