Repository navigation
Description
Description
With beta.row_binary_for_simple_insert=true, a Timestamp or LocalDateTime bound with setObject to a DateTime or DateTime64 column that has no time zone of its own is stored as if its wall clock were UTC. The default text path stores a Timestamp in the server time zone, which is what a DateTime without a time zone means in ClickHouse (DateTime docs). So on a server that isn't UTC, the same value through the same driver is stored hours apart depending on which path is used, and nothing reports an error.
The cause is in SerializerUtils.serializeData (v0.11.2, lines 735-744). When the column has no time zone it falls back to ZoneId.of("UTC"), not the server time zone. Columns that declare a time zone are fine: for DateTime('Europe/Berlin') a Timestamp is stored the same on both paths. The code is unchanged on main at 7506301.
What each path does for a column without a time zone, measured with the program below:
Bound with setObject |
Text path (default) | RowBinary writer |
|---|---|---|
Timestamp |
server time zone | UTC, whatever the server or JVM time zone |
LocalDateTime |
JVM time zone (see #3127) | UTC, whatever the server or JVM time zone |
On a UTC server with a UTC JVM all four agree, which is likely why tests don't catch it.
This matters more once #3328 makes the writer the default. An app on a non-UTC server that inserts Timestamp values into DateTime columns would start storing shifted values after upgrading, without an error. We found it while moving the jdbc-v2 insert arms of an internal benchmark suite to the writer. Our servers are UTC, so our data isn't affected.
Expected: for a column without a time zone, the writer uses the same time zone the text path uses for a Timestamp (the server time zone, or the one set with use_time_zone), so both paths store the same value.
Related: #3127 is about the text path, LocalDateTime and the JVM time zone, which is a different question. Other writer gaps: #3386, #3334, #3326. The client-v2 RowBinary insert path goes through the same SerializerUtils code, so it is probably affected too. I haven't tested that.
Separately, setObject with a String such as "2024-01-15 12:00:00" works on the text path and fails on the writer with Cannot convert 2024-01-15 12:00:00 to DateTime. That looks like #3165, so it isn't covered here.
Steps to reproduce
- jdbc-v2 0.11.2, ClickHouse 26.6.8.7, Java 21.
- Start a server in a non-UTC time zone:
docker run -d --rm -p 28123:8123 -e TZ=Asia/Tokyo -e CLICKHOUSE_SKIP_USER_SETUP=1 clickhouse/clickhouse-server:26.6-alpine - Run
java -Duser.timezone=UTC Repro jdbc:clickhouse:http://localhost:28123/default. Change-Duser.timezoneto see the JVM's effect, or drop-e TZ=...for a UTC server.
import java.sql.*;
import java.time.LocalDateTime;
import java.util.Properties;
public class Repro {
public static void main(String[] args) throws Exception {
String url = args[0];
Properties text = new Properties();
text.setProperty("user", "default");
text.setProperty("password", "");
Properties writer = (Properties) text.clone();
writer.setProperty("beta.row_binary_for_simple_insert", "true");
try (Connection c = DriverManager.getConnection(url, text); Statement s = c.createStatement()) {
ResultSet rs = s.executeQuery("SELECT timezone()");
rs.next();
System.out.println("server time zone=" + rs.getString(1) + ", JVM time zone=" + java.util.TimeZone.getDefault().getID());
s.execute("DROP TABLE IF EXISTS tz_repro");
s.execute("CREATE TABLE tz_repro (label String, t DateTime, t_berlin DateTime('Europe/Berlin'), t64 DateTime64(3))"
+ " ENGINE = MergeTree ORDER BY label");
}
Object[][] values = {
{"Timestamp", Timestamp.valueOf("2024-01-15 12:00:00")},
{"LocalDateTime", LocalDateTime.of(2024, 1, 15, 12, 0, 0)},
};
for (Object[] v : values) {
for (String path : new String[] {"text", "writer"}) {
try (Connection c = DriverManager.getConnection(url, path.equals("text") ? text : writer);
PreparedStatement ps = c.prepareStatement("INSERT INTO tz_repro VALUES (?, ?, ?, ?)")) {
ps.setString(1, path + "/" + v[0]);
ps.setObject(2, v[1]);
ps.setObject(3, v[1]);
ps.setObject(4, v[1]);
ps.executeUpdate();
}
}
}
try (Connection c = DriverManager.getConnection(url, text); Statement s = c.createStatement()) {
ResultSet rs = s.executeQuery("SELECT label, toString(t, 'UTC'), toString(t_berlin, 'UTC'), toString(t64, 'UTC')"
+ " FROM tz_repro ORDER BY label");
System.out.printf(" %-21s %-21s %-21s %s%n", "(stored, shown in UTC)", "DateTime", "DateTime('Europe/Berlin')", "DateTime64(3)");
while (rs.next()) {
System.out.printf(" %-21s %-21s %-21s %s%n", rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4));
}
s.execute("DROP TABLE tz_repro");
}
}
}Output
Verbatim. Every value is shown in UTC so the rows can be compared directly. The text and writer rows should match, and on the Tokyo server they don't.
server time zone=Asia/Tokyo, JVM time zone=UTC
(stored, shown in UTC) DateTime DateTime('Europe/Berlin') DateTime64(3)
text/LocalDateTime 2024-01-15 12:00:00 2024-01-15 12:00:00 2024-01-15 12:00:00.000
text/Timestamp 2024-01-15 03:00:00 2024-01-15 11:00:00 2024-01-15 03:00:00.000
writer/LocalDateTime 2024-01-15 12:00:00 2024-01-15 11:00:00 2024-01-15 12:00:00.000
writer/Timestamp 2024-01-15 12:00:00 2024-01-15 11:00:00 2024-01-15 12:00:00.000
server time zone=Asia/Tokyo, JVM time zone=America/New_York
(stored, shown in UTC) DateTime DateTime('Europe/Berlin') DateTime64(3)
text/LocalDateTime 2024-01-15 17:00:00 2024-01-15 17:00:00 2024-01-15 17:00:00.000
text/Timestamp 2024-01-15 03:00:00 2024-01-15 11:00:00 2024-01-15 03:00:00.000
writer/LocalDateTime 2024-01-15 12:00:00 2024-01-15 11:00:00 2024-01-15 12:00:00.000
writer/Timestamp 2024-01-15 12:00:00 2024-01-15 11:00:00 2024-01-15 12:00:00.000
server time zone=UTC, JVM time zone=UTC
(stored, shown in UTC) DateTime DateTime('Europe/Berlin') DateTime64(3)
text/LocalDateTime 2024-01-15 12:00:00 2024-01-15 12:00:00 2024-01-15 12:00:00.000
text/Timestamp 2024-01-15 12:00:00 2024-01-15 11:00:00 2024-01-15 12:00:00.000
writer/LocalDateTime 2024-01-15 12:00:00 2024-01-15 11:00:00 2024-01-15 12:00:00.000
writer/Timestamp 2024-01-15 12:00:00 2024-01-15 11:00:00 2024-01-15 12:00:00.000
On the Tokyo server, text/Timestamp stores 12:00 Tokyo (03:00 UTC) and writer/Timestamp stores 12:00 UTC (21:00 Tokyo), 9 hours apart. On the UTC server every row matches.
Activity
jdbc-v2: store DateTime in the server time zone in the RowBinary writer