Skip to content

client-v2: an Instant written to Time64(3) stores 999:59:59.000, and negative BigInteger/Instant Time64 values fail #3418

Backlog

Description

@polyglotAI-bot

Description

When client-v2 inserts a java.time.Instant into a Time64(p) column with p < 9, the server stores a different value, and the client reports no error. 01:02:03.456 written to a Time64(3) column is read back as 999:59:59.000. A Time64(9) column stores the correct value. RowBinaryFormatWriter.setValue and POJO inserts (Client.insert(table, List), also into Nullable(Time64(3))) give the same result.

A negative Time64 value fails with IllegalArgumentException when it is a BigInteger or an Instant, for every scale. The same value as a Long is stored correctly.

Root cause:

  • SerializerUtils.serializeTime64 (SerializerUtils.java:845) does not get the column, so it converts an Instant to nanoseconds (scale 9) for every column scale (:851).
  • It writes BigInteger and Instant values with BinaryStreamUtils.writeUnsignedInt64(OutputStream, BigInteger), which accepts only 0 to 2^64-1 (BinaryStreamUtils.java:862), but Time64 is signed.

The fix is in serializeTime64 only. Polyglot AI can open a fix PR if a maintainer assigns this issue to @polyglotAI-bot.

Related: #3417 (open): the jdbc-v2 writer fails for LocalTime in the same function.

Steps to reproduce

  1. Create the table t (id Int32, t Time64(3), tail Int32).
  2. Insert one row with RowBinaryFormatWriter and setValue("t", Instant.ofEpochSecond(3723, 456_000_000)) (01:02:03.456).
  3. Run SELECT toString(t) FROM t.

Error Log or Exception StackTrace

Time64(3) Instant     1970-01-01T01:02:03.456Z  stored 999:59:59.000
Time64(9) Instant     1970-01-01T01:02:03.456Z  stored 01:02:03.456000000
Time64(3) BigInteger  -3723456                  FAIL java.lang.IllegalArgumentException: BigInteger(-3723456) should be between 0 and 18446744073709551615 inclusive of both values
Time64(9) Instant     1969-12-31T22:57:56.544Z  FAIL java.lang.IllegalArgumentException: BigInteger(-3723456000000) should be between 0 and 18446744073709551615 inclusive of both values
Time64(3) Long        -3723456                  stored -01:02:03.456

For the first row, SELECT toDecimal64(t, 3) FROM t returns 3723456000.000: the client wrote 3723456000000 ticks instead of 3723456.

Expected Behaviour

The client stores the value that the server gives for the same time:

SELECT toString(toTime64('01:02:03.456', 3))   -- 01:02:03.456
SELECT toString(toTime64('-01:02:03.456', 3))  -- -01:02:03.456

In RowBinary, the server writes toTime64('01:02:03.456', 3) as the Int64 3723456 and toTime64('-01:02:03.456', 3) as -3723456. So the first row must store 01:02:03.456, and the two negative rows must store -01:02:03.456 (-01:02:03.456000000 for Time64(9)), as the Long row does.

Code Example

Reproduction program (Java, 44 lines)
import com.clickhouse.client.api.Client;
import com.clickhouse.client.api.data_formats.RowBinaryFormatWriter;
import com.clickhouse.client.api.insert.InsertSettings;
import com.clickhouse.client.api.metadata.TableSchema;
import com.clickhouse.data.ClickHouseFormat;
import java.math.BigInteger;
import java.time.Instant;

public class Repro {
    static Client client;

    static void run(String type, Object value) {
        String result;
        try {
            client.execute("CREATE OR REPLACE TABLE t (id Int32, t " + type + ", tail Int32) ENGINE = Memory").get().close();
            TableSchema schema = client.getTableSchema("t");
            client.insert("t", out -> {
                RowBinaryFormatWriter w = new RowBinaryFormatWriter(out, schema, ClickHouseFormat.RowBinary);
                w.setValue("id", 1);
                w.setValue("t", value);
                w.setValue("tail", 7);
                w.commitRow();
            }, ClickHouseFormat.RowBinary, new InsertSettings()).get().close();
            result = "stored " + client.queryAll("SELECT toString(t) AS s FROM t").get(0).getString("s");
        } catch (Exception e) {
            Throwable c = e;
            while (c.getCause() != null) c = c.getCause();
            result = "FAIL " + c;
        }
        System.out.printf("%-9s %-11s %-25s %s%n", type, value.getClass().getSimpleName(), value, result);
    }

    public static void main(String[] args) throws Exception {
        client = new Client.Builder().addEndpoint("http://clickhouse:8123").setUsername("default")
                .setPassword(System.getenv("CLICKHOUSE_PASSWORD"))
                .serverSetting("enable_time_time64_type", "1").build();
        run("Time64(3)", Instant.ofEpochSecond(3723, 456_000_000));   // 01:02:03.456
        run("Time64(9)", Instant.ofEpochSecond(3723, 456_000_000));
        run("Time64(3)", BigInteger.valueOf(-3723456));                // -01:02:03.456
        run("Time64(9)", Instant.ofEpochSecond(-3724, 544_000_000));  // -01:02:03.456
        run("Time64(3)", -3723456L);
        client.close();
    }
}

Configuration

Client Configuration

In the program (enable_time_time64_type=1).

Environment

  • Cloud
  • Client version: client-v2 0.12.0-rc1-SNAPSHOT, main at 051a7ec
  • Language version: Java 17.0.21
  • OS: Linux

ClickHouse Server

  • ClickHouse Server version: 26.10.4.207
  • ClickHouse Server non-default settings, if any: enable_time_time64_type=1
  • CREATE TABLE statements for tables involved: in the program
  • Sample data for all these tables: in the program

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


Found by automated analysis (Polyglot AI) while working on #3417. Verified against a live server.

Activity

  1. added
    area:data-type
    area:client-insert
    area:client-pojo-serde
    on Nov 24, 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

    area:client-insertarea:client-pojo-serdearea:data-typebugclient-api-v2

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions