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
Maintainers usually reply within 1 day
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 48/100
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
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 topdigits. - 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
- Run the code example (net10.0, project references to
ClickHouse.Driver,ClickHouse.Driver.CommonandClickHouse.Driver.Tcp). - 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¶m_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 byWriteValueandFormatTime64String) callsMath.Round(seconds, Scale).ClickHouse.Driver/Types/TimeType.cs:39,:53-54and:65callMath.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:
mainat 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
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from ClickHouse/clickhouse-cs
-
bug documentation queries
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
ClickHouse/clickhouse-cs#815 ·
Maintainers usually reply within 1 day
-
bug data-types native-format
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
ClickHouse/clickhouse-cs#813 ·
Maintainers usually reply within 1 day
-
bug data-types documentation
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
ClickHouse/clickhouse-cs#811 ·
Maintainers usually reply within 1 day
-
bug data-types
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
ClickHouse/clickhouse-cs#809 ·
Maintainers usually reply within 1 day
-
bug data-types
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
ClickHouse/clickhouse-cs#806 · 1 comment ·
Maintainers usually reply within 1 day
All issues in ClickHouse/clickhouse-cs
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
-
XML grammar: only the closing --> of a comment gets the scope comment.block.xmlPossibly taken @alexeyfala claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
danipen/TextMateSharp#145 · 2 comments ·
-
good first issue help wanted
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
Klyroo/klyroo-dashboard#12 ·
Maintainers usually reply within 1 day
-
CONTRIBUTING rules Node out in step 0, then tells client-side contributors to run node --test, which is what CI gates onPossibly taken @yadavraj2001 claimed this today. Opendocumentation good first issue
Difficulty 1/5 1-3 hours Newbie friendliness 82/100
juandresrodca/AdminForge#73 · 1 comment ·
Maintainers usually reply within 1 day
-
[辞書] VIVANTOpen要トリアージ 辞書
Difficulty 1/5 Under an hour Newbie friendliness 70/100
Maintainers usually reply within 1 day