Skip to content

[client-v2, jdbc-v2] A Time value that is negative or 24 h or more is read as a time of day (-01:02:03 becomes 22:57:57) #3447

Backlog

Description

@polyglotAI-bot

Description

A ClickHouse Time or Time64 value can be from -999:59:59 to 999:59:59. client-v2 and jdbc-v2 read a value that is negative or 24 hours or more as its time of day, and report no error: -01:02:03 becomes 22:57:57, and 25:00:00 becomes 01:00:00. Values from 00:00:00 to 23:59:59 are correct.

These reads are affected:

  • client-v2: getString and getLocalTime of GenericRecord (queryAll) and of ClickHouseBinaryFormatReader, also getString of an Array(Time) value ([22:57:57]);
  • jdbc-v2: ResultSet.getString, getObject(col) (the default mapping java.sql.Time, which holds only the time of day: 82677000 ms for -01:02:03), and getObject(col, LocalTime.class).

jdbc-v2 getTime(col) keeps the full value (-3723000 ms for -01:02:03). For Time64, getObject(col) also drops the fraction (01:00:00 for 25:00:00.5).

The same defect was reported for ch-go in ClickHouse/ch-go#1249 and for clickhouse-odbc in ClickHouse/clickhouse-odbc#643.

Steps to reproduce

  1. Start a ClickHouse server.
  2. Run the program in "Code Example" in a JVM with the time zone UTC, with the clickhouse-jdbc -all jar and slf4j-api on the classpath.

Error Log or Exception StackTrace

toTime(-3723)              server=-01:02:03   client-v2: getString=22:57:57 getLocalTime=22:57:57 reader.getString=22:57:57 reader.getLocalTime=22:57:57
toTime(90000)              server=25:00:00    client-v2: getString=01:00:00 getLocalTime=01:00 reader.getString=01:00:00 reader.getLocalTime=01:00
toTime64('25:00:00.5', 1)  server=25:00:00.5  client-v2: getString=01:00:00.5 getLocalTime=01:00:00.500 reader.getString=01:00:00.5 reader.getLocalTime=01:00:00.500
toTime(3723)               server=01:02:03    client-v2: getString=01:02:03 getLocalTime=01:02:03 reader.getString=01:02:03 reader.getLocalTime=01:02:03
[toTime(-3723)]            server=['-01:02:03'] client-v2: getString=[22:57:57]
toTime(-3723)              server=-01:02:03   jdbc-v2: getString=22:57:57 getObject=22:57:57 (82677000 ms) getObject(LocalTime)=22:57:57 | getTime=-3723000 ms
toTime(90000)              server=25:00:00    jdbc-v2: getString=01:00:00 getObject=01:00:00 (3600000 ms) getObject(LocalTime)=01:00 | getTime=90000000 ms
toTime64('25:00:00.5', 1)  server=25:00:00.5  jdbc-v2: getString=01:00:00.5 getObject=01:00:00 (3600000 ms) getObject(LocalTime)=01:00:00.500 | getTime=90000500 ms
toTime(3723)               server=01:02:03    jdbc-v2: getString=01:02:03 getObject=01:02:03 (3723000 ms) getObject(LocalTime)=01:02:03 | getTime=3723000 ms
[toTime(-3723)]            server=['-01:02:03'] jdbc-v2: getString=[22:57:57]

Expected Behaviour

The value that the server gives (toString(t), the server= column above; curl with FORMAT TSV gives the same text): -01:02:03, 25:00:00, 25:00:00.5, ['-01:02:03']. getString must return this text. A LocalTime cannot hold these values, and a java.sql.Time made from a LocalTime cannot either: these reads must return the full value (as getTime(col) does), or throw an error that says that the value does not fit. They must not return a different time.

Root cause:

  • DataTypeConverter.timeToString formats the value with the time-of-day pattern HH:mm:ss (DataTypeConverter.java:156-168), and AbstractBinaryFormatReader.getString converts the value to LocalTime first (AbstractBinaryFormatReader.java:381).
  • client-v2 getLocalTime returns dt.toLocalTime() of the epoch-based LocalDateTime that the reader makes for Time (AbstractBinaryFormatReader.java:1015).
  • jdbc-v2 JdbcUtils.convert makes the java.sql.Time with Time.valueOf(LocalTime.from(value)) (JdbcUtils.java:426), and the LocalTime with LocalTime.from(value) (JdbcUtils.java:416).

DataTypeUtils.durationToTimeString already writes the signed text. The fix needs a maintainer decision for the time-of-day types: return the full value in the java.sql.Time (as getTime does) and throw for LocalTime, or throw for both.
Polyglot AI can open a fix PR if a maintainer assigns this issue to @polyglotAI-bot.

Related:

Code Example

Reproduction program (Java, 49 lines)
import com.clickhouse.client.api.Client;
import com.clickhouse.client.api.data_formats.ClickHouseBinaryFormatReader;
import com.clickhouse.client.api.query.GenericRecord;
import com.clickhouse.client.api.query.QueryResponse;
import java.sql.*;
import java.time.LocalTime;
import java.util.Properties;

public class Repro {
    static final String[] CASES = {"toTime(-3723)", "toTime(90000)", "toTime64('25:00:00.5', 1)", "toTime(3723)"};

    public static void main(String[] args) throws Exception {
        String pw = System.getenv("CLICKHOUSE_PASSWORD");
        try (Client client = new Client.Builder().addEndpoint("http://clickhouse:8123")
                .setUsername("default").setPassword(pw).build()) {
            for (String c : CASES) {
                String sql = "SELECT " + c + " AS t, toString(t) AS s";
                GenericRecord r = client.queryAll(sql).get(0);
                try (QueryResponse resp = client.query(sql).get();
                     ClickHouseBinaryFormatReader rd = client.newBinaryFormatReader(resp)) {
                    rd.next();
                    System.out.printf("%-26s server=%-11s client-v2: getString=%s getLocalTime=%s reader.getString=%s reader.getLocalTime=%s%n",
                            c, r.getString("s"), r.getString("t"), r.getLocalTime("t"), rd.getString("t"), rd.getLocalTime("t"));
                }
            }
            GenericRecord a = client.queryAll("SELECT [toTime(-3723)] AS a, toString(a) AS s").get(0);
            System.out.printf("%-26s server=%-11s client-v2: getString=%s%n", "[toTime(-3723)]", a.getString("s"), a.getString("a"));
        }
        Properties p = new Properties();
        p.setProperty("user", "default");
        p.setProperty("password", pw);
        try (Connection cn = DriverManager.getConnection("jdbc:clickhouse://clickhouse:8123/default", p);
             Statement st = cn.createStatement()) {
            for (String c : CASES) {
                try (ResultSet rs = st.executeQuery("SELECT " + c + " AS t, toString(t) AS s")) {
                    rs.next();
                    Time t = (Time) rs.getObject("t");
                    System.out.printf("%-26s server=%-11s jdbc-v2: getString=%s getObject=%s (%d ms) getObject(LocalTime)=%s | getTime=%d ms%n",
                            c, rs.getString("s"), rs.getString("t"), t, t.getTime(), rs.getObject("t", LocalTime.class),
                            rs.getTime("t").getTime());
                }
            }
            try (ResultSet rs = st.executeQuery("SELECT [toTime(-3723)] AS a, toString(a) AS s")) {
                rs.next();
                System.out.printf("%-26s server=%-11s jdbc-v2: getString=%s%n", "[toTime(-3723)]", rs.getString("s"), rs.getString("a"));
            }
        }
    }
}

Configuration

Client Configuration

new Client.Builder().addEndpoint("http://clickhouse:8123").setUsername("default").setPassword(...).build();
DriverManager.getConnection("jdbc:clickhouse://clickhouse:8123/default", props); // user, password

Environment

  • Cloud
  • Client version: main (8480ca001): client-v2, and the clickhouse-jdbc -all jar (jdbc-v2)
  • Language version: Java 17.0.21
  • OS: Linux

ClickHouse Server

  • ClickHouse Server version: 26.11.2.4
  • ClickHouse Server non-default settings, if any: none
  • CREATE TABLE statements for tables involved: none
  • Sample data for all these tables: none

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


Found by automated analysis (Polyglot AI) while checking ch-go#1249 and clickhouse-odbc#643 against the other client repos. Verified against a live server.

Activity

  1. added
    client
    jdbc
    data-types
    on Nov 26, 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

    bugclientdata-typesjdbc

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions