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

未關閉
#3,418 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

維護者通常 1 天內回覆

還沒有人認領這個 Issue。

評估

難度
3/5
預估耗時
1-2 天
新手友好度
68/100
Issue 類型
缺陷
描述清晰度
描述清楚
活躍度
活躍
技術堆疊
java
領域
data

研究方向

Start in serializeTime64 in SerializerUtils.java (around line 845). It does not receive the column scale, so Instant values are always converted to nanoseconds, and BigInteger and Instant values go through BinaryStreamUtils.writeUnsignedInt64, which rejects negative values. Run the 44-line reproduction in the issue against a local server: done means Time64(3) Instant stores 01:02:03.456, negative BigInteger and Instant values store -01:02:03.456, and the Long results stay unchanged.

由索引模型根據 Issue 內容生成。

描述

bug client data-types

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 051a7ecfd
  • 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.

主要語言
Java
星號
1.6k
分支
657
平均合併
2 天 6 小時
30 天內合併 PR
36

環境準備

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

ClickHouse/clickhouse-java 的其他 Issue

查看 ClickHouse/clickhouse-java 的全部 Issue

相似的 Issue

更多 Java Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。