Repository navigation
Description
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:
- [tests] Run the ClickHouse core SQL tests through client-v2 and jdbc-v2 nightly in CI #3389 (open): lists this defect as known-failure group
time64-negative-fraction(12 core SQL tests). - client-v2: an Instant written to Time64(3) stores 999:59:59.000, and negative BigInteger/Instant Time64 values fail #3418 (open): negative
Time64writes, a different function.
Steps to reproduce
- Run
SELECT toTime64('-01:02:03.456', 3) AS twithClient.queryAll. - Read
twithgetDuration("t")orgetLocalDateTime("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,mainat 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 TABLEstatements 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