From 24ef703a7f2ece52be8763e36a7ce9d75ce8fdf8 Mon Sep 17 00:00:00 2001 From: utvaxling Date: Fri, 27 Nov 2026 11:56:53 -0800 Subject: [PATCH 1/3] [client-v2] Use one constant UTC ZoneId when writing Date and DateTime values SerializerUtils.serializePrimitiveData called ZoneId.of("UTC") for each Date and Date32 value, and for each DateTime and DateTime64 value of a column that has no time zone. On JDK 17 each call makes a new ZoneRegion with new ZoneRules, and the ZoneRules make a ConcurrentHashMap and a ZoneOffset array. The zone is now a constant of the class. It is the same ZoneId value as before, so the bytes that the serializer writes do not change. Measured with the JMH benchmark Serializers.SerializerOutputStreamV2 of the performance module on JDK 17: 100,000 rows of 12 columns, with one Date, one DateTime and one DateTime64 column, 5 forks before and 5 forks after. Allocation for each operation goes from 41.20 MB to 28.99 MB (-29.6%) in the forks on the lowest allocation level (4 of 5 forks on each side; the level depends on what escape analysis removes in a fork). The mean time of these forks is 12.0 ms before and 11.5 ms after, which is not more than the noise of this benchmark. Co-Authored-By: Claude Fable 5.2 --- .../api/data_formats/internal/SerializerUtils.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/client-v2/src/main/java/com/clickhouse/client/api/data_formats/internal/SerializerUtils.java b/client-v2/src/main/java/com/clickhouse/client/api/data_formats/internal/SerializerUtils.java index 50f9136e2..cbeb98492 100644 --- a/client-v2/src/main/java/com/clickhouse/client/api/data_formats/internal/SerializerUtils.java +++ b/client-v2/src/main/java/com/clickhouse/client/api/data_formats/internal/SerializerUtils.java @@ -62,6 +62,8 @@ @SuppressWarnings("deprecation") public class SerializerUtils { + private static final ZoneId UTC_ZONE_ID = ZoneId.of("UTC"); + 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()) { @@ -727,18 +729,18 @@ private static void serializePrimitiveData(OutputStream stream, Object value, Cl } break; case Date: - writeDate(stream, value, ZoneId.of("UTC")); // TODO: check + writeDate(stream, value, UTC_ZONE_ID); // TODO: check break; case Date32: - writeDate32(stream, value, ZoneId.of("UTC")); // TODO: check + writeDate32(stream, value, UTC_ZONE_ID); // TODO: check break; case DateTime: { - ZoneId zoneId = column.getTimeZone() == null ? ZoneId.of("UTC") : column.getTimeZone().toZoneId(); + ZoneId zoneId = column.getTimeZone() == null ? UTC_ZONE_ID : 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 ? UTC_ZONE_ID : column.getTimeZone().toZoneId(); writeDateTime64(stream, value, column.getScale(), zoneId); break; } From b4857a6a62d3f50d27cd5968930012029ad27a15 Mon Sep 17 00:00:00 2001 From: utvaxling Date: Mon, 30 Nov 2026 10:57:32 -0800 Subject: [PATCH 2/3] [client-v2] Reuse the existing UTC zone constant for Date and DateTime conversions The first commit of this change added a private UTC ZoneId constant to SerializerUtils. This commit uses the constant that already exists, ClickHouseValues.UTC_ZONE, and removes the new one. Two more places made ZoneId.of("UTC") for each call, both in the binary readers: getInstant on a Date or Date32 column, and the conversion of a LocalDateTime of a Dynamic or Variant column to an Instant. They use the same constant now. ClickHouseValues.UTC_ZONE is equal to ZoneId.of("UTC"), so the written bytes and the returned Instant values do not change. ZoneOffset.UTC of the JDK is not used here on purpose. When a ZonedDateTime or an OffsetDateTime is written to a Date column, java.time asks the zone for its rules, and ZoneOffset.getRules() makes new ZoneRules for each call on JDK 17 and earlier. A probe of the conversions on JDK 17 gave, for a ZonedDateTime in zone UTC that is written to a Date column: 224 bytes for each value with ZoneOffset.UTC, 152 bytes with ZoneId.of("UTC") for each value, and 0 bytes with a constant ZoneId.of("UTC"). For LocalDate and LocalDateTime values both constants gave 0 bytes. For the two reader places the probe gave 152 bytes less for each call. Measured with the JMH benchmark Serializers.SerializerOutputStreamV2 of the performance module on JDK 17 (100,000 rows of 12 columns, one Date column). With escape analysis off, where allocation does not depend on the JIT compiler: 45.78 MB for each operation, the same as with the first commit, and 14.49 MB less than before the change (60.27 MB). With the default JVM the lowest allocation level is 28.99 MB, the same as with the first commit (41.20 MB before the change). Co-Authored-By: Claude Fable 5.2 --- .../internal/AbstractBinaryFormatReader.java | 6 +++--- .../api/data_formats/internal/SerializerUtils.java | 11 +++++------ 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/client-v2/src/main/java/com/clickhouse/client/api/data_formats/internal/AbstractBinaryFormatReader.java b/client-v2/src/main/java/com/clickhouse/client/api/data_formats/internal/AbstractBinaryFormatReader.java index a9fcc7c6e..25bff717b 100644 --- a/client-v2/src/main/java/com/clickhouse/client/api/data_formats/internal/AbstractBinaryFormatReader.java +++ b/client-v2/src/main/java/com/clickhouse/client/api/data_formats/internal/AbstractBinaryFormatReader.java @@ -14,6 +14,7 @@ import com.clickhouse.client.api.serde.POJOFieldDeserializer; import com.clickhouse.data.ClickHouseColumn; import com.clickhouse.data.ClickHouseDataType; +import com.clickhouse.data.ClickHouseValues; import com.clickhouse.data.value.ClickHouseBitmap; import com.clickhouse.data.value.ClickHouseGeoMultiPolygonValue; import com.clickhouse.data.value.ClickHouseGeoPointValue; @@ -39,7 +40,6 @@ import java.time.LocalDateTime; import java.time.LocalTime; import java.time.OffsetDateTime; -import java.time.ZoneId; import java.time.ZoneOffset; import java.time.ZonedDateTime; import java.time.temporal.TemporalAmount; @@ -664,7 +664,7 @@ public Instant getInstant(int index) { case Date: case Date32: LocalDate date = getLocalDate(index); - return date == null ? null : date.atStartOfDay(ZoneId.of("UTC")).toInstant(); + return date == null ? null : date.atStartOfDay(ClickHouseValues.UTC_ZONE).toInstant(); case Time: case Time64: LocalDateTime dt = getLocalDateTime(index); @@ -707,7 +707,7 @@ public static byte[] stringLikeToBytes(Object value) { static Instant objectToInstant(Object value) { if (value instanceof LocalDateTime) { LocalDateTime dateTime = (LocalDateTime) value; - return Instant.from(dateTime.atZone(ZoneId.of("UTC"))); + return Instant.from(dateTime.atZone(ClickHouseValues.UTC_ZONE)); } else if (value instanceof ZonedDateTime) { ZonedDateTime dateTime = (ZonedDateTime) value; return dateTime.toInstant(); diff --git a/client-v2/src/main/java/com/clickhouse/client/api/data_formats/internal/SerializerUtils.java b/client-v2/src/main/java/com/clickhouse/client/api/data_formats/internal/SerializerUtils.java index cbeb98492..784c7c37e 100644 --- a/client-v2/src/main/java/com/clickhouse/client/api/data_formats/internal/SerializerUtils.java +++ b/client-v2/src/main/java/com/clickhouse/client/api/data_formats/internal/SerializerUtils.java @@ -7,6 +7,7 @@ import com.clickhouse.data.ClickHouseColumn; import com.clickhouse.data.ClickHouseDataType; import com.clickhouse.data.ClickHouseEnum; +import com.clickhouse.data.ClickHouseValues; import com.clickhouse.data.format.BinaryStreamUtils; import com.clickhouse.data.value.ClickHouseBitmap; import com.clickhouse.data.value.ClickHouseGeoMultiPolygonValue; @@ -62,8 +63,6 @@ @SuppressWarnings("deprecation") public class SerializerUtils { - private static final ZoneId UTC_ZONE_ID = ZoneId.of("UTC"); - 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()) { @@ -729,18 +728,18 @@ private static void serializePrimitiveData(OutputStream stream, Object value, Cl } break; case Date: - writeDate(stream, value, UTC_ZONE_ID); // TODO: check + writeDate(stream, value, ClickHouseValues.UTC_ZONE); // TODO: check break; case Date32: - writeDate32(stream, value, UTC_ZONE_ID); // TODO: check + writeDate32(stream, value, ClickHouseValues.UTC_ZONE); // TODO: check break; case DateTime: { - ZoneId zoneId = column.getTimeZone() == null ? UTC_ZONE_ID : column.getTimeZone().toZoneId(); + ZoneId zoneId = column.getTimeZone() == null ? ClickHouseValues.UTC_ZONE : column.getTimeZone().toZoneId(); writeDateTime(stream, value, zoneId); break; } case DateTime64: { - ZoneId zoneId = column.getTimeZone() == null ? UTC_ZONE_ID : column.getTimeZone().toZoneId(); + ZoneId zoneId = column.getTimeZone() == null ? ClickHouseValues.UTC_ZONE : column.getTimeZone().toZoneId(); writeDateTime64(stream, value, column.getScale(), zoneId); break; } From 1c5117cfdf217ddb3a2d3e868f96b4a0dd2bc9d2 Mon Sep 17 00:00:00 2001 From: utvaxling Date: Mon, 30 Nov 2026 14:53:05 -0800 Subject: [PATCH 3/3] [client-v2] Use DataTypeConverter.UTC_ZONE_ID for Date and DateTime conversions The six places of this change used ClickHouseValues.UTC_ZONE of clickhouse-data as the UTC zone. They now use DataTypeConverter.UTC_ZONE_ID, the UTC zone constant of client-v2 itself: four places in SerializerUtils.serializePrimitiveData and two in AbstractBinaryFormatReader (getInstant on a Date or Date32 column, and the conversion of a LocalDateTime of a Dynamic or Variant column to an Instant). The two classes no longer import ClickHouseValues. Both constants are equal to ZoneId.of("UTC"), so the written bytes and the returned Instant values do not change. Measured with the JMH benchmark Serializers.SerializerOutputStreamV2 of the performance module on JDK 17 (100,000 rows of 12 columns, one Date column), against the commit before this one in the same session. With escape analysis off: 45.78 MB for each operation with both commits. With the default JVM, 5 forks each: the lowest allocation level is 28.99 MB and the highest is 37.39 MB with both commits, and the mean time of the forks on the lowest level is 11.6 ms with both. Co-Authored-By: Claude Fable 5.2 --- .../internal/AbstractBinaryFormatReader.java | 5 ++--- .../api/data_formats/internal/SerializerUtils.java | 10 +++++----- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/client-v2/src/main/java/com/clickhouse/client/api/data_formats/internal/AbstractBinaryFormatReader.java b/client-v2/src/main/java/com/clickhouse/client/api/data_formats/internal/AbstractBinaryFormatReader.java index 25bff717b..c3efac515 100644 --- a/client-v2/src/main/java/com/clickhouse/client/api/data_formats/internal/AbstractBinaryFormatReader.java +++ b/client-v2/src/main/java/com/clickhouse/client/api/data_formats/internal/AbstractBinaryFormatReader.java @@ -14,7 +14,6 @@ import com.clickhouse.client.api.serde.POJOFieldDeserializer; import com.clickhouse.data.ClickHouseColumn; import com.clickhouse.data.ClickHouseDataType; -import com.clickhouse.data.ClickHouseValues; import com.clickhouse.data.value.ClickHouseBitmap; import com.clickhouse.data.value.ClickHouseGeoMultiPolygonValue; import com.clickhouse.data.value.ClickHouseGeoPointValue; @@ -664,7 +663,7 @@ public Instant getInstant(int index) { case Date: case Date32: LocalDate date = getLocalDate(index); - return date == null ? null : date.atStartOfDay(ClickHouseValues.UTC_ZONE).toInstant(); + return date == null ? null : date.atStartOfDay(DataTypeConverter.UTC_ZONE_ID).toInstant(); case Time: case Time64: LocalDateTime dt = getLocalDateTime(index); @@ -707,7 +706,7 @@ public static byte[] stringLikeToBytes(Object value) { static Instant objectToInstant(Object value) { if (value instanceof LocalDateTime) { LocalDateTime dateTime = (LocalDateTime) value; - return Instant.from(dateTime.atZone(ClickHouseValues.UTC_ZONE)); + return Instant.from(dateTime.atZone(DataTypeConverter.UTC_ZONE_ID)); } else if (value instanceof ZonedDateTime) { ZonedDateTime dateTime = (ZonedDateTime) value; return dateTime.toInstant(); diff --git a/client-v2/src/main/java/com/clickhouse/client/api/data_formats/internal/SerializerUtils.java b/client-v2/src/main/java/com/clickhouse/client/api/data_formats/internal/SerializerUtils.java index 784c7c37e..94d19e77f 100644 --- a/client-v2/src/main/java/com/clickhouse/client/api/data_formats/internal/SerializerUtils.java +++ b/client-v2/src/main/java/com/clickhouse/client/api/data_formats/internal/SerializerUtils.java @@ -2,12 +2,12 @@ import com.clickhouse.client.api.Client; import com.clickhouse.client.api.ClientException; +import com.clickhouse.client.api.internal.DataTypeConverter; import com.clickhouse.client.api.serde.POJOFieldDeserializer; import com.clickhouse.data.ClickHouseAggregateFunction; import com.clickhouse.data.ClickHouseColumn; import com.clickhouse.data.ClickHouseDataType; import com.clickhouse.data.ClickHouseEnum; -import com.clickhouse.data.ClickHouseValues; import com.clickhouse.data.format.BinaryStreamUtils; import com.clickhouse.data.value.ClickHouseBitmap; import com.clickhouse.data.value.ClickHouseGeoMultiPolygonValue; @@ -728,18 +728,18 @@ private static void serializePrimitiveData(OutputStream stream, Object value, Cl } break; case Date: - writeDate(stream, value, ClickHouseValues.UTC_ZONE); // TODO: check + writeDate(stream, value, DataTypeConverter.UTC_ZONE_ID); // TODO: check break; case Date32: - writeDate32(stream, value, ClickHouseValues.UTC_ZONE); // TODO: check + writeDate32(stream, value, DataTypeConverter.UTC_ZONE_ID); // TODO: check break; case DateTime: { - ZoneId zoneId = column.getTimeZone() == null ? ClickHouseValues.UTC_ZONE : column.getTimeZone().toZoneId(); + ZoneId zoneId = column.getTimeZone() == null ? DataTypeConverter.UTC_ZONE_ID : column.getTimeZone().toZoneId(); writeDateTime(stream, value, zoneId); break; } case DateTime64: { - ZoneId zoneId = column.getTimeZone() == null ? ClickHouseValues.UTC_ZONE : column.getTimeZone().toZoneId(); + ZoneId zoneId = column.getTimeZone() == null ? DataTypeConverter.UTC_ZONE_ID : column.getTimeZone().toZoneId(); writeDateTime64(stream, value, column.getScale(), zoneId); break; }