RowBinary insert stores an Unspecified DateTime in UTC for a timezone-less DateTime/DateTime64 column, while the query-parameter path uses the server timezone

Open
#748 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

@XSY-28 is already working on this.

Since Nov 25, 2026.

  • #772 by @XSY-28 — open

Assessment

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

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-128 and :139-140 send 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.cs coerces an Unspecified value with TimeZoneOrUtc.AtLeniently(...), and TimeZoneOrUtc is TimeZone ?? 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

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.