RowBinary insert stores an Unspecified DateTime in UTC for a timezone-less DateTime/DateTime64 column, while the query-parameter path uses the server timezone
Maintainers usually reply within 1 day
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 45/100
Research direction
Start with ClickHouse.Driver/Types/AbstractDateTimeType.cs and trace how TypeSettings reaches DateTimeType.Parse and DateTime64Type.Parse. Compare the timezone handling in Formats/HttpParameterFormatter.cs with the binary insert and read paths, then run the provided reproduction against a server using session_timezone = Asia/Tokyo. Done means timezone-less DateTime and DateTime64 values agree across parameter and binary paths on write and read.
Written by the indexing model from the issue text.
Description
Description
For a DateTime / DateTime64 column declared without a timezone, the two write paths of the HTTP client disagree about what wall-clock a DateTime with Kind == Unspecified means:
- Query-parameter (text) path —
Formats/HttpParameterFormatter.cs:127-128and:139-140send the wall clock verbatim (dt.ToString("s")). The server then interprets it in the column's timezone, which for a timezone-less column is the server (or session) timezone. That matches the DateTime docs. - RowBinary insert path (
InsertBinaryAsync,ClickHouseBulkCopy) —Types/AbstractDateTimeType.cscoerces anUnspecifiedvalue withTimeZoneOrUtc.AtLeniently(...), andTimeZoneOrUtcisTimeZone ?? DateTimeZone.Utc. With no timezone on the column it resolves the wall clock in UTC.
On a server whose timezone is not UTC, the same DateTime value through the same driver is stored hours apart depending on which path you use, and nothing reports an error. Columns that declare a timezone (DateTime('Europe/Berlin')) are fine — both paths agree there.
The read path has the same assumption: AbstractDateTimeType.ToDateTime returns instant.InZone(DateTimeZone.Utc).ToDateTimeUnspecified() when TimeZone == null, so a value written through the binary path round-trips self-consistently but does not agree with what the server itself renders for that column.
This is the .NET counterpart of ClickHouse/clickhouse-java#3394.
ClickHouse server version
26.10.4.207 (the test server runs in UTC; the non-UTC server is simulated with the session_timezone setting, which makes the server resolve timezone-less DateTime columns in Asia/Tokyo while still reporting the column type as a bare DateTime).
Reproduction
ClickHouse.Driver.Tests, net9.0, against a real server:
using System;
using System.Threading.Tasks;
using ClickHouse.Driver.ADO.Parameters;
using ClickHouse.Driver.Utility;
using NUnit.Framework;
namespace ClickHouse.Driver.Tests;
public class DateTimeServerTimezoneTests
{
[Test]
public async Task InsertUnspecifiedDateTime_TimezoneLessColumn_BinaryAndParameterPathsAgree()
{
var settings = TestUtilities.GetTestClickHouseClientSettings();
settings.CustomSettings["session_timezone"] = "Asia/Tokyo"; // stand-in for a non-UTC server
using var client = new ClickHouseClient(settings);
var table = TestUtilities.CreateTableName();
await client.ExecuteNonQueryAsync($"CREATE TABLE {table} (label String, t DateTime, t64 DateTime64(3)) ENGINE = Memory");
var value = new DateTime(2024, 1, 15, 12, 0, 0, DateTimeKind.Unspecified);
var parameters = new ClickHouseParameterCollection();
parameters.AddParameter("t", value);
parameters.AddParameter("t64", value);
await client.ExecuteNonQueryAsync($"INSERT INTO {table} VALUES ('param', {{t:DateTime}}, {{t64:DateTime64(3)}})", parameters);
await client.InsertBinaryAsync(table, new[] { "label", "t", "t64" },
new[] { new object[] { "binary", value, value } });
using var reader = await client.ExecuteReaderAsync(
$"SELECT label, toString(t, 'UTC'), toString(t64, 'UTC') FROM {table} ORDER BY label");
while (reader.Read())
Console.WriteLine($"{reader.GetString(0),-8} t={reader.GetString(1)} t64={reader.GetString(2)}");
await client.ExecuteNonQueryAsync($"DROP TABLE IF EXISTS {table}");
}
}
Actual output (values shown in UTC so the two rows are comparable):
binary t=2024-01-15 12:00:00 t64=2024-01-15 12:00:00.000
param t=2024-01-15 03:00:00 t64=2024-01-15 03:00:00.000
Expected: the two rows are identical. The parameter row is correct — 12:00 Asia/Tokyo is 03:00 UTC. The binary row stored 12:00 UTC, which is 21:00 Asia/Tokyo, nine hours off.
Suggested fix
ClickHouse.Driver/Types/AbstractDateTimeType.cs: TimeZoneOrUtc (TimeZone ?? DateTimeZone.Utc) is the fallback used both by CoerceToDateTimeOffset(DateTime) for the Unspecified case and by ToDateTime/ToDateTimeOffset on the read path. For a column with no timezone of its own the fallback should be the server timezone (the value of timezone(), overridable by session_timezone or a client setting), not UTC. That value is already available to the connection; TypeSettings passed into Parse would be the natural place to carry it, so DateTimeType.Parse / DateTime64Type.Parse can attach the server zone when the type has no explicit one. Changing the default would also need a note in docs/http.mdx and a changelog fragment, since it changes stored values for existing users on non-UTC servers.
The same TimeZoneOrUtc value is used on the read path, so a fix should keep the write and read sides consistent.
Link
Relayed from https://github.com/ClickHouse/clickhouse-java/issues/3394
- 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