Repository navigation
jdbc-v2: store DateTime in the server time zone in the RowBinary writer #3425
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -62,6 +62,20 @@ | |
| @SuppressWarnings("deprecation") | ||
| public class SerializerUtils { | ||
|
|
||
| // Default time zone for DateTime/DateTime64 columns that declare no zone of their own. | ||
| // The RowBinary writer sets it to the server time zone so stored values match the text path | ||
| // and the reader; it stays UTC for every other caller. See clickhouse-java#3394. | ||
| private static final ThreadLocal<ZoneId> WRITER_DEFAULT_TIME_ZONE = | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is very ugly way to make SerializerUtils (static by nature) as stateful class. |
||
| ThreadLocal.withInitial(() -> ZoneId.of("UTC")); | ||
|
|
||
| public static void setWriterDefaultTimeZone(ZoneId zoneId) { | ||
| WRITER_DEFAULT_TIME_ZONE.set(zoneId == null ? ZoneId.of("UTC") : zoneId); | ||
| } | ||
|
|
||
| public static void resetWriterDefaultTimeZone() { | ||
| WRITER_DEFAULT_TIME_ZONE.remove(); | ||
| } | ||
|
|
||
| public static void serializeData(OutputStream stream, Object value, ClickHouseColumn column) throws IOException { | ||
| //Serialize the value to the stream based on the data type | ||
| switch (column.getDataType()) { | ||
|
|
@@ -733,12 +747,12 @@ private static void serializePrimitiveData(OutputStream stream, Object value, Cl | |
| writeDate32(stream, value, ZoneId.of("UTC")); // TODO: check | ||
| break; | ||
| case DateTime: { | ||
| ZoneId zoneId = column.getTimeZone() == null ? ZoneId.of("UTC") : column.getTimeZone().toZoneId(); | ||
| ZoneId zoneId = column.getTimeZone() == null ? WRITER_DEFAULT_TIME_ZONE.get() : column.getTimeZone().toZoneId(); | ||
| writeDateTime(stream, value, zoneId); | ||
| break; | ||
| } | ||
| case DateTime64: { | ||
| ZoneId zoneId = column.getTimeZone() == null ? ZoneId.of("UTC") : column.getTimeZone().toZoneId(); | ||
| ZoneId zoneId = column.getTimeZone() == null ? WRITER_DEFAULT_TIME_ZONE.get() : column.getTimeZone().toZoneId(); | ||
| writeDateTime64(stream, value, column.getScale(), zoneId); | ||
| break; | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| - **[jdbc-v2]** The RowBinary writer (`beta.row_binary_for_simple_insert`) now stores | ||
| `DateTime`/`DateTime64` values for columns without an explicit time zone in the **server** | ||
| time zone, matching the text path and the reader, instead of UTC. Behaviour change on | ||
| non-UTC servers for the beta writer path; pin the zone with `use_time_zone` / | ||
| `use_server_time_zone` to keep the old behaviour. | ||
| (https://github.com/ClickHouse/clickhouse-java/issues/3394) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| package com.clickhouse.jdbc; | ||
|
|
||
| import com.clickhouse.client.api.Client; | ||
| import java.time.ZoneId; | ||
| import com.clickhouse.client.api.ClientConfigProperties; | ||
| import com.clickhouse.client.api.data_formats.JsonParserFactory; | ||
| import com.clickhouse.client.api.metadata.TableSchema; | ||
|
|
@@ -52,6 +53,7 @@ public class ConnectionImpl implements Connection, JdbcV2Wrapper { | |
|
|
||
| protected final String url; | ||
| private final Client client; // this member is private to force using getClient() | ||
| private volatile ZoneId effectiveServerTimeZone; | ||
| protected final JdbcConfiguration config; | ||
|
|
||
| private boolean closed = false; | ||
|
|
@@ -750,6 +752,32 @@ public Client getClient() throws SQLException { | |
| return client; | ||
| } | ||
|
|
||
| /** | ||
| * Resolves the server time zone once per connection and caches it. The RowBinary writer uses it | ||
| * so DateTime/DateTime64 columns without their own zone are stored in the server zone, matching | ||
| * the text path and the reader (clickhouse-java#3394). Falls back to UTC. | ||
| */ | ||
| public ZoneId getEffectiveServerTimeZone() { | ||
| ZoneId tz = effectiveServerTimeZone; | ||
| if (tz != null) { | ||
| return tz; | ||
| } | ||
| String zoneId = null; | ||
| try { | ||
| zoneId = client.getServerTimeZone(); | ||
| if (zoneId == null || "UTC".equalsIgnoreCase(zoneId)) { | ||
| // The configured default is UTC and the real zone is only detected lazily; load it once. | ||
| client.loadServerInfo(); | ||
| zoneId = client.getServerTimeZone(); | ||
| } | ||
| } catch (Exception e) { | ||
| // fall back to UTC below | ||
| } | ||
| tz = ZoneId.of(zoneId == null ? "UTC" : zoneId); | ||
| effectiveServerTimeZone = tz; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. UTC timezone always reloads from serverMedium Severity
Additional Locations (1)Reviewed by Cursor Bugbot for commit 42ba5fe. Configure here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Failed timezone lookup caches UTC foreverMedium Severity When Reviewed by Cursor Bugbot for commit 42ba5fe. Configure here. |
||
| return tz; | ||
| } | ||
|
|
||
| private void ensureOpenForClientInfo() throws SQLClientInfoException { | ||
| if (isClosed()) { | ||
| throw new SQLClientInfoException("Connection is closed", | ||
|
|
||


There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we are trying to fix UTC and setting UTC in this place. This kind of wrong. It should all be aligned.