diff --git a/clickhouse-data/src/main/java/com/clickhouse/data/format/BinaryDataProcessor.java b/clickhouse-data/src/main/java/com/clickhouse/data/format/BinaryDataProcessor.java index c7ecb4f1d..fc9b1e35f 100644 --- a/clickhouse-data/src/main/java/com/clickhouse/data/format/BinaryDataProcessor.java +++ b/clickhouse-data/src/main/java/com/clickhouse/data/format/BinaryDataProcessor.java @@ -289,17 +289,11 @@ public ClickHouseValue deserialize(ClickHouseValue ref, ClickHouseInputStream in @Override public void serialize(ClickHouseValue value, ClickHouseOutputStream output) throws IOException { LocalDateTime dt = value.asDateTime(scale); - long v = ClickHouseChecker.between( - ClickHouseValues.UTC_ZONE.equals(zoneId) ? dt.toEpochSecond(ZoneOffset.UTC) - : dt.atZone(zoneId).toEpochSecond(), - ClickHouseValues.TYPE_DATE_TIME, BinaryStreamUtils.DATETIME64_MIN, - BinaryStreamUtils.DATETIME64_MAX); - if (ClickHouseChecker.between(scale, ClickHouseValues.PARAM_SCALE, 0, 9) > 0) { - v *= BASES[scale]; - int nanoSeconds = dt.getNano(); - if (nanoSeconds > 0L) { - v += nanoSeconds / BASES[9 - scale]; - } + long v = dt.toEpochSecond(ZoneOffset.UTC); + v *= BASES[scale]; + int nanoSeconds = dt.getNano(); + if (nanoSeconds > 0L) { + v += nanoSeconds / BASES[9 - scale]; } BinaryStreamUtils.writeInt64(output, v); @@ -393,10 +387,7 @@ public ClickHouseValue deserialize(ClickHouseValue ref, ClickHouseInputStream in @Override public void serialize(ClickHouseValue value, ClickHouseOutputStream output) throws IOException { BigDecimal v = value.asBigDecimal(); - BinaryStreamUtils.writeInt64(output, - ClickHouseChecker.between(scale == 0 ? v : v.multiply(BigDecimal.TEN.pow(scale)), - ClickHouseValues.TYPE_BIG_DECIMAL, BinaryStreamUtils.DECIMAL64_MIN, - BinaryStreamUtils.DECIMAL64_MAX).longValue()); + BinaryStreamUtils.writeDecimal64(output, v, scale); } } @@ -425,10 +416,7 @@ public ClickHouseValue deserialize(ClickHouseValue ref, ClickHouseInputStream in @Override public void serialize(ClickHouseValue value, ClickHouseOutputStream output) throws IOException { BigDecimal v = value.asBigDecimal(); - BinaryStreamUtils.writeInt128(output, - ClickHouseChecker.between(scale == 0 ? v : v.multiply(BigDecimal.TEN.pow(scale)), - ClickHouseValues.TYPE_BIG_DECIMAL, BinaryStreamUtils.DECIMAL128_MIN, - BinaryStreamUtils.DECIMAL128_MAX).toBigInteger()); + BinaryStreamUtils.writeDecimal128(output, v, scale); } } diff --git a/clickhouse-data/src/main/java/com/clickhouse/data/format/BinaryStreamUtils.java b/clickhouse-data/src/main/java/com/clickhouse/data/format/BinaryStreamUtils.java index bccdce7ae..27d763ec1 100644 --- a/clickhouse-data/src/main/java/com/clickhouse/data/format/BinaryStreamUtils.java +++ b/clickhouse-data/src/main/java/com/clickhouse/data/format/BinaryStreamUtils.java @@ -69,9 +69,9 @@ public final class BinaryStreamUtils { public static final BigDecimal DECIMAL256_MIN = new BigDecimal( "-10000000000000000000000000000000000000000000000000000000000000000000000000000"); - public static final long DATETIME64_MAX = LocalDateTime.of(LocalDate.of(2283, 11, 11), LocalTime.MAX) + public static final long DATETIME64_MAX = LocalDateTime.of(LocalDate.of(2299, 12, 31), LocalTime.MAX) .toEpochSecond(ZoneOffset.UTC); - public static final long DATETIME64_MIN = LocalDateTime.of(LocalDate.of(1925, 1, 1), LocalTime.MIN) + public static final long DATETIME64_MIN = LocalDateTime.of(LocalDate.of(1900, 1, 1), LocalTime.MIN) .toEpochSecond(ZoneOffset.UTC); public static final long MILLIS_IN_DAY = TimeUnit.DAYS.toMillis(1); diff --git a/clickhouse-data/src/test/java/com/clickhouse/data/format/BinaryStreamUtilsTest.java b/clickhouse-data/src/test/java/com/clickhouse/data/format/BinaryStreamUtilsTest.java index 4f7df70f5..9bc1dd502 100644 --- a/clickhouse-data/src/test/java/com/clickhouse/data/format/BinaryStreamUtilsTest.java +++ b/clickhouse-data/src/test/java/com/clickhouse/data/format/BinaryStreamUtilsTest.java @@ -1256,12 +1256,12 @@ public void testWriteDateTime64() throws IOException { Assert.assertThrows(IllegalArgumentException.class, () -> getWrittenBytes(o -> BinaryStreamUtils.writeDateTime64(o, - LocalDateTime.of(LocalDate.of(1925, 1, 1).minus(1L, ChronoUnit.DAYS), + LocalDateTime.of(LocalDate.of(1900, 1, 1).minus(1L, ChronoUnit.DAYS), LocalTime.MAX), null))); Assert.assertThrows(IllegalArgumentException.class, () -> getWrittenBytes(o -> BinaryStreamUtils.writeDateTime64(o, - LocalDateTime.of(LocalDate.of(2283, 11, 11).plus(1L, ChronoUnit.DAYS), + LocalDateTime.of(LocalDate.of(2299, 12, 31).plus(1L, ChronoUnit.DAYS), LocalTime.MIN), null))); } @@ -1325,7 +1325,7 @@ public void testWriteDateTime64WithTimeZone(String timeZoneId) throws IOExceptio () -> getWrittenBytes( o -> BinaryStreamUtils.writeDateTime64(o, LocalDateTime.of( - LocalDate.of(1925, 1, 1).minus(1L, + LocalDate.of(1900, 1, 1).minus(1L, ChronoUnit.DAYS), LocalTime.MAX) .atOffset(ZoneOffset.UTC) @@ -1336,7 +1336,7 @@ public void testWriteDateTime64WithTimeZone(String timeZoneId) throws IOExceptio () -> getWrittenBytes( o -> BinaryStreamUtils.writeDateTime64(o, LocalDateTime.of( - LocalDate.of(2283, 11, 11).plus(1L, + LocalDate.of(2299, 12, 31).plus(1L, ChronoUnit.DAYS), LocalTime.MIN) .atOffset(ZoneOffset.UTC)