Skip to content

client-v2: a negative Time64 value with a fraction is read as a wrong value (-01:02:03.456 becomes -01:02:02.544) #3419

Backlog

Description

@polyglotAI-bot

Description

client-v2 reads a negative Time64(p) value with a fraction (p ≥ 1) as a different value, and reports no error. The fraction is added to the whole seconds instead of subtracted: -01:02:03.456 is read as -01:02:02.544, and -00:00:00.5 loses its sign (+0.5 s). getDuration, getLocalDateTime and getInstant show it, also for Nullable(Time64) columns and Array(Time64) elements. jdbc-v2 ResultSet.getObject(col, LocalDateTime.class) returns the same wrong value. Positive values, and negative values in Time64(0), are correct.

Root cause: DataTypeUtils.localTimeFromTime64Integer (DataTypeUtils.java:165-166) takes Math.abs of the remainder after a truncating division, so for a negative tick count the fraction gets the wrong sign. BinaryStreamReader.readTime64 (BinaryStreamReader.java:1332) uses it for every Time64 value.

The fix is in localTimeFromTime64Integer only (instantFromTime64Integer in the same file already uses floor division). Polyglot AI can open a fix PR if a maintainer assigns this issue to @polyglotAI-bot.

Related:

Steps to reproduce

  1. Run SELECT toTime64('-01:02:03.456', 3) AS t with Client.queryAll.
  2. Read t with getDuration("t") or getLocalDateTime("t").

Error Log or Exception StackTrace

Time64(3) server=-01:02:03.456       getDuration=PT-1H-2M-2.544S        getLocalDateTime=1969-12-31T22:57:57.456
Time64(9) server=-01:02:03.456789123 getDuration=PT-1H-2M-2.543210877S  getLocalDateTime=1969-12-31T22:57:57.456789123
Time64(1) server=-00:00:00.5         getDuration=PT0.5S                 getLocalDateTime=1970-01-01T00:00:00.500
Time64(3) server=01:02:03.456        getDuration=PT1H2M3.456S           getLocalDateTime=1970-01-01T01:02:03.456

jdbc-v2, same query: rs.getObject("t", LocalDateTime.class) returns 1969-12-31T22:57:57.456.

Expected Behaviour

The value that the server stores. In RowBinary, the server writes toTime64('-01:02:03.456', 3) as the Int64 -3723456, that is -3723.456 s:

$ echo "SELECT toTime64('-01:02:03.456', 3) FORMAT RowBinary" | curl -s 'http://localhost:8123/' --data-binary @- | od -A n -t d8
             -3723456

So getDuration must return PT-1H-2M-3.456S and getLocalDateTime must return 1969-12-31T22:57:56.544. For -00:00:00.5: PT-0.5S and 1969-12-31T23:59:59.500.

Code Example

Reproduction program (Java, 17 lines)
import com.clickhouse.client.api.Client;
import com.clickhouse.client.api.query.GenericRecord;

public class Repro {
    public static void main(String[] args) throws Exception {
        try (Client client = new Client.Builder().addEndpoint("http://clickhouse:8123").setUsername("default")
                .setPassword(System.getenv("CLICKHOUSE_PASSWORD")).build()) {
            String[][] cases = {{"-01:02:03.456", "3"}, {"-01:02:03.456789123", "9"},
                    {"-00:00:00.5", "1"}, {"01:02:03.456", "3"}};
            for (String[] c : cases) {
                GenericRecord r = client.queryAll("SELECT toTime64('" + c[0] + "', " + c[1] + ") AS t, toString(t) AS s").get(0);
                System.out.printf("Time64(%s) server=%-19s getDuration=%-22s getLocalDateTime=%s%n",
                        c[1], r.getString("s"), r.getDuration("t"), r.getLocalDateTime("t"));
            }
        }
    }
}

Configuration

Client Configuration

Default (in the program).

Environment

  • Cloud
  • Client version: client-v2 and jdbc-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: none
  • CREATE TABLE statements for tables involved: none (constant values)
  • Sample data for all these tables: none

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


Found by automated analysis (Polyglot AI) while working on ClickHouse/ch-go#1248. Verified against a live server.

Activity

  1. added
    jdbc-v2
    area:data-type
    area:jdbc-read
    area:client-read
    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-readarea:data-typearea:jdbc-readbugclient-api-v2jdbc-v2

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions