Repository navigation
Description
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:
getStringandgetLocalTimeofGenericRecord(queryAll) and ofClickHouseBinaryFormatReader, alsogetStringof anArray(Time)value ([22:57:57]); - jdbc-v2:
ResultSet.getString,getObject(col)(the default mappingjava.sql.Time, which holds only the time of day: 82677000 ms for-01:02:03), andgetObject(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
- Start a ClickHouse server.
- Run the program in "Code Example" in a JVM with the time zone UTC, with the
clickhouse-jdbc-alljar andslf4j-apion 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.timeToStringformats the value with the time-of-day patternHH:mm:ss(DataTypeConverter.java:156-168), andAbstractBinaryFormatReader.getStringconverts the value toLocalTimefirst (AbstractBinaryFormatReader.java:381).- client-v2
getLocalTimereturnsdt.toLocalTime()of the epoch-basedLocalDateTimethat the reader makes forTime(AbstractBinaryFormatReader.java:1015). - jdbc-v2
JdbcUtils.convertmakes thejava.sql.TimewithTime.valueOf(LocalTime.from(value))(JdbcUtils.java:426), and theLocalTimewithLocalTime.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:
- 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 (open): a negative
Time64with a fraction is decoded wrong, in a different function. tests/clickhouse-client/allowlist.txt, groupjdbc-time-as-sql-time(from [tests] Run the ClickHouse core SQL tests through client-v2 and jdbc-v2 nightly in CI #3389): lists 43 jdbc-v2 core SQL tests for thegetObjectpart ("Issue: not filed yet").
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, passwordEnvironment
- Cloud
- Client version:
main(8480ca001): client-v2, and theclickhouse-jdbc-alljar (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 TABLEstatements 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