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

Ouverte
#3,418 0 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Les mainteneurs répondent en général sous 1 jour

Personne n'a encore pris cette issue.

Évaluation

Difficulté
3/5
Temps estimé
1-2 jours
Accessibilité débutants
68/100
Type d'issue
Bug
Clarté
Clairement spécifiée
Activité
Active
Stack technique
java
Domaine
data

Piste de recherche

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.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Description

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.

Langage dominant
Java
Étoiles
1.6k
Forks
657
Merge moyen
2 j 6 h
PR mergées (30 j)
36

Préparer son environnement

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Autres issues de ClickHouse/clickhouse-java

Toutes les issues de ClickHouse/clickhouse-java

Issues similaires

Plus d'issues Java

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.