Skip to content

[jdbc-v2] setObject(ZonedDateTime) outside 1677-2262 silently stores a wrong DateTime64 value #3292

Open

Description

@krvikash

Description

JDBC V2 sends a ZonedDateTime (also OffsetDateTime and Instant) as fromUnixTimestamp64Nano(<epoch nanos>). This only fits in Int64 for 1677-09-21 00:12:43.145224192 to 2262-04-11 23:47:16.854775807 UTC. Outside that range the driver stores a wrong value without an error, or the server rejects the query.

The DateTime64 docs say the range is 0000-01-01 to 9999-12-31 for precisions up to 7. The 1677-2262 range applies only to precision 9. The driver applies it to every precision.

Steps to reproduce

  1. Create a DateTime64(7, 'UTC') column.
  2. Insert a ZonedDateTime with PreparedStatement.setObject().
  3. Read it back with toString(dt).

Error Log or Exception StackTrace

Value sent Stored
1677-09-21 00:12:44Z 1677-09-21 00:12:44 (ok)
2262-04-11 23:47:16Z 2262-04-11 23:47:16 (ok)
1500-06-01 00:00:00Z 1677-09-21 00:12:43.1452242 (wrong, no error)
2262-04-11 23:47:17Z 1677-09-21 00:12:43.2904484 (wrong, no error)
3000-01-01 00:00:00Z error

For 3000-01-01 00:00:00Z:

java.sql.SQLException: Code: 43. DB::Exception: The first argument for function fromUnixTimestamp64Nano must be integer: In scope fromUnixTimestamp64Nano(32503680000000000000.): While executing WaitForAsyncInsert. (ILLEGAL_TYPE_OF_ARGUMENT) (version 26.8.34.13 (official build))  (queryId= cafe2b13-6035-4ad8-ad9d-a5e8463c0a56)
	at com.clickhouse.jdbc.internal.ExceptionUtils.toSqlState(ExceptionUtils.java:68)
	at com.clickhouse.jdbc.internal.ExceptionUtils.toSqlState(ExceptionUtils.java:41)
	at com.clickhouse.jdbc.StatementImpl.executeUpdateImpl(StatementImpl.java:416)
	at com.clickhouse.jdbc.PreparedStatementImpl.executeUpdate(PreparedStatementImpl.java:156)
Caused by: com.clickhouse.client.api.ServerException: Code: 43. DB::Exception: The first argument for function fromUnixTimestamp64Nano must be integer: In scope fromUnixTimestamp64Nano(32503680000000000000.): While executing WaitForAsyncInsert. (ILLEGAL_TYPE_OF_ARGUMENT) (version 26.8.34.13 (official build))
	at com.clickhouse.client.api.internal.HttpAPIClientHelper.readClickHouseError(HttpAPIClientHelper.java:534)
	at com.clickhouse.client.api.internal.HttpAPIClientHelper.readError(HttpAPIClientHelper.java:426)
	at com.clickhouse.client.api.internal.HttpAPIClientHelper.doExecuteRequest(HttpAPIClientHelper.java:833)
	at com.clickhouse.client.api.Client.query(Client.java:2081)
	at com.clickhouse.jdbc.StatementImpl.executeUpdateImpl(StatementImpl.java:409)

Expected Behaviour

The value is stored correctly, or the driver throws a clear error. The server handles these values correctly, and so does setObject(index, "1500-06-01 00:00:00") with a String.

Suspected cause, in DataTypeUtils.toUnixTimestampString(long seconds, int nanos): below the range TimeUnit.SECONDS.toNanos(seconds) saturates at Long.MIN_VALUE, and above it the BigInteger value does not fit in Int64.

Code Example

com.clickhouse.jdbc.Driver.load();
try (Connection c = DriverManager.getConnection("jdbc:clickhouse://localhost:8123/default", "default", "");
        Statement s = c.createStatement()) {
    s.execute("CREATE TABLE t (dt DateTime64(7, 'UTC')) ENGINE = Memory");
    try (PreparedStatement ps = c.prepareStatement("INSERT INTO t VALUES (?)")) {
        ps.setObject(1, ZonedDateTime.of(1500, 6, 1, 0, 0, 0, 0, ZoneOffset.UTC));
        ps.executeUpdate();
    }
    ResultSet rs = s.executeQuery("SELECT toString(dt) FROM t");
    rs.next();
    System.out.println(rs.getString(1)); // 1677-09-21 00:12:43.1452242
}

Configuration

Environment

  • Cloud
  • Client version: jdbc-v2 0.11.0 (0.10.0 behaves the same)
  • Language version: Java 26
  • OS: macOS

ClickHouse Server

  • ClickHouse Server version: 26.8.34.13
  • ClickHouse Server non-default settings, if any: none
  • CREATE TABLE statements for tables involved: CREATE TABLE t (dt DateTime64(7, 'UTC')) ENGINE = Memory
  • Sample data for all these tables: see the table above

Related: #3127. The workaround suggested there to avoid the JVM time zone shift, setObject(ZonedDateTime), is affected by this bug outside 1677-2262.

Activity

  1. polyglotAI-bot commented on Nov 27, 2026

    @polyglotAI-bot
    Collaborator

    Recommendation: Keep open — fix needed
    setObject(LocalDateTime) has the same defect. No open PR covers it.

    • Category: Bug
    • Classification: impact: Data integrity · area: Data types & formats (DateTime64)
    • Status on main: Reproduced on main — the values of the issue's table give the stored values and the error that the issue shows; LocalDateTime 1500-06-01 is also stored as 1677-09-21 00:12:43.1452242.
    • Breaking change: No — the fix makes the driver store the given value.
    • Duplicates / related: No others

    Analysis: The issue's suspected cause is correct: below the range, TimeUnit.SECONDS.toNanos in DataTypeUtils.toUnixTimestampString saturates at Long.MIN_VALUE, and above it the BigInteger value does not fit in Int64 (client-v2/src/main/java/com/clickhouse/client/api/DataTypeUtils.java:393-399). PreparedStatementImpl.encodeObject sends LocalDateTime through the same function, not only OffsetDateTime, ZonedDateTime, and Instant, for every DateTime64 precision (jdbc-v2/src/main/java/com/clickhouse/jdbc/PreparedStatementImpl.java:782-789).

    Reproduction: complete program and output

    Changed from the issue's steps: one case for each value of the issue's table, a LocalDateTime case, and the issue's String case as a contrast. The JVM time zone is UTC.

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.Statement;
    import java.time.LocalDateTime;
    import java.time.ZoneOffset;
    import java.time.ZonedDateTime;
    import java.util.Properties;
    import java.util.TimeZone;
    
    public class Repro {
        static final String URL = System.getenv().getOrDefault("CLICKHOUSE_URL", "http://localhost:8123");
        static final String PASSWORD = System.getenv().getOrDefault("CLICKHOUSE_PASSWORD", "");
    
        public static void main(String[] args) throws Exception {
            TimeZone.setDefault(TimeZone.getTimeZone("UTC")); // LocalDateTime is sent in the JVM time zone
            Properties props = new Properties();
            props.setProperty("user", "default");
            props.setProperty("password", PASSWORD);
            String jdbcUrl = "jdbc:clickhouse:" + URL.replaceFirst("^https?:", "") + "/default";
            Object[] values = {
                ZonedDateTime.of(1677, 9, 21, 0, 12, 44, 0, ZoneOffset.UTC),
                ZonedDateTime.of(2262, 4, 11, 23, 47, 16, 0, ZoneOffset.UTC),
                ZonedDateTime.of(1500, 6, 1, 0, 0, 0, 0, ZoneOffset.UTC),
                ZonedDateTime.of(2262, 4, 11, 23, 47, 17, 0, ZoneOffset.UTC),
                ZonedDateTime.of(3000, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC),
                LocalDateTime.of(1500, 6, 1, 0, 0, 0),
                "1500-06-01 00:00:00",
            };
            try (Connection c = DriverManager.getConnection(jdbcUrl, props);
                 Statement s = c.createStatement()) {
                s.execute("DROP TABLE IF EXISTS triage_3292");
                s.execute("CREATE TABLE triage_3292 (dt DateTime64(7, 'UTC')) ENGINE = Memory");
                for (Object v : values) {
                    s.execute("TRUNCATE TABLE triage_3292");
                    String label = v.getClass().getSimpleName() + " " + v;
                    try (PreparedStatement ps = c.prepareStatement("INSERT INTO triage_3292 VALUES (?)")) {
                        ps.setObject(1, v);
                        ps.executeUpdate();
                        try (ResultSet rs = s.executeQuery("SELECT toString(dt) FROM triage_3292")) {
                            rs.next();
                            System.out.println(label + " -> " + rs.getString(1));
                        }
                    } catch (Exception e) {
                        String m = String.valueOf(e.getMessage());
                        System.out.println(label + " -> " + e.getClass().getName() + ": " + m.substring(0, Math.min(m.length(), 140)));
                    }
                }
                s.execute("DROP TABLE triage_3292");
            }
        }
    }

    Output on main (8480ca001), ClickHouse 26.11.2.4:

    ZonedDateTime 1677-09-21T00:12:44Z -> 1677-09-21 00:12:44.0000000
    ZonedDateTime 2262-04-11T23:47:16Z -> 2262-04-11 23:47:16.0000000
    ZonedDateTime 1500-06-01T00:00Z -> 1677-09-21 00:12:43.1452242
    ZonedDateTime 2262-04-11T23:47:17Z -> 1677-09-21 00:12:43.2904484
    ZonedDateTime 3000-01-01T00:00Z -> java.sql.SQLException: Code: 43. DB::Exception: The first argument for function fromUnixTimestamp64Nano must be integer: …
    LocalDateTime 1500-06-01T00:00 -> 1677-09-21 00:12:43.1452242
    String 1500-06-01 00:00:00 -> 1500-06-01 00:00:00.0000000
    

    Automated triage by Polyglot AI. A maintainer makes the final decision.

  2. added
    data-types
    jdbc
    on Nov 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugdata-typesjdbc

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions