diff --git a/client-v2/src/main/java/com/clickhouse/client/api/DataTypeUtils.java b/client-v2/src/main/java/com/clickhouse/client/api/DataTypeUtils.java index 1e01f6cd4..49e8b30a5 100644 --- a/client-v2/src/main/java/com/clickhouse/client/api/DataTypeUtils.java +++ b/client-v2/src/main/java/com/clickhouse/client/api/DataTypeUtils.java @@ -4,7 +4,19 @@ public class DataTypeUtils { - public static DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); + /** + * Formatter for the DateTime type. + */ + public static DateTimeFormatter DATETIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); + /** + * Formatter for the Date type. + */ public static DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd"); + + /** + * Formatter for the DateTime type with nanoseconds. + */ + public static DateTimeFormatter DATETIME_WITH_NANOS_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.nnnnnnnnn"); + } diff --git a/client-v2/src/main/java/com/clickhouse/client/api/data_formats/internal/BinaryStreamReader.java b/client-v2/src/main/java/com/clickhouse/client/api/data_formats/internal/BinaryStreamReader.java index ecb309315..303162296 100644 --- a/client-v2/src/main/java/com/clickhouse/client/api/data_formats/internal/BinaryStreamReader.java +++ b/client-v2/src/main/java/com/clickhouse/client/api/data_formats/internal/BinaryStreamReader.java @@ -171,7 +171,7 @@ public T readValue(ClickHouseColumn column, Class typeHint) throws IOExce return convertDateTime(readDateTime32(column.getTimeZone() == null ? timeZone : column.getTimeZone()), typeHint); case DateTime64: - return convertDateTime(readDateTime64(3, column.getTimeZone() == null ? timeZone : + return convertDateTime(readDateTime64(column.getScale(), column.getTimeZone() == null ? timeZone : column.getTimeZone()), typeHint); case IntervalYear: diff --git a/client-v2/src/test/java/com/clickhouse/client/query/QueryTests.java b/client-v2/src/test/java/com/clickhouse/client/query/QueryTests.java index ae8baf129..e5711212e 100644 --- a/client-v2/src/test/java/com/clickhouse/client/query/QueryTests.java +++ b/client-v2/src/test/java/com/clickhouse/client/query/QueryTests.java @@ -706,18 +706,36 @@ public void testDateTimeDataTypes() { "min_date Date", "max_date Date", "min_dateTime DateTime", - "max_dateTime DateTime" + "max_dateTime DateTime", + "min_dateTime64 DateTime64", + "max_dateTime64 DateTime64", + "min_dateTime64_6 DateTime64(6)", + "max_dateTime64_6 DateTime64(6)", + "min_dateTime64_9 DateTime64(9)", + "max_dateTime64_9 DateTime64(9)" ); final LocalDate minDate = LocalDate.parse("1970-01-01"); final LocalDate maxDate = LocalDate.parse("2149-06-06"); - final LocalDateTime minDateTime = LocalDateTime.parse("1970-01-01T00:00:00"); + final LocalDateTime minDateTime = LocalDateTime.parse("1970-01-01T01:02:03"); final LocalDateTime maxDateTime = LocalDateTime.parse("2106-02-07T06:28:15"); + final LocalDateTime minDateTime64 = LocalDateTime.parse("1970-01-01T01:02:03.123"); + final LocalDateTime maxDateTime64 = LocalDateTime.parse("2106-02-07T06:28:15.123"); + final LocalDateTime minDateTime64_6 = LocalDateTime.parse("1970-01-01T01:02:03.123456"); + final LocalDateTime maxDateTime64_6 = LocalDateTime.parse("2106-02-07T06:28:15.123456"); + final LocalDateTime minDateTime64_9 = LocalDateTime.parse("1970-01-01T01:02:03.123456789"); + final LocalDateTime maxDateTime64_9 = LocalDateTime.parse("2106-02-07T06:28:15.123456789"); final List> valueGenerators = Arrays.asList( () -> sq(minDate.toString()), () -> sq(maxDate.toString()), - () -> sq(minDateTime.format(DataTypeUtils.DATE_TIME_FORMATTER)), - () -> sq(maxDateTime.format(DataTypeUtils.DATE_TIME_FORMATTER)) + () -> sq(minDateTime.format(DataTypeUtils.DATETIME_FORMATTER)), + () -> sq(maxDateTime.format(DataTypeUtils.DATETIME_FORMATTER)), + () -> sq(minDateTime64.format(DataTypeUtils.DATETIME_WITH_NANOS_FORMATTER)), + () -> sq(maxDateTime64.format(DataTypeUtils.DATETIME_WITH_NANOS_FORMATTER)), + () -> sq(minDateTime64_6.format(DataTypeUtils.DATETIME_WITH_NANOS_FORMATTER)), + () -> sq(maxDateTime64_6.format(DataTypeUtils.DATETIME_WITH_NANOS_FORMATTER)), + () -> sq(minDateTime64_9.format(DataTypeUtils.DATETIME_WITH_NANOS_FORMATTER)), + () -> sq(maxDateTime64_9.format(DataTypeUtils.DATETIME_WITH_NANOS_FORMATTER)) ); final List> verifiers = new ArrayList<>(); @@ -741,6 +759,36 @@ public void testDateTimeDataTypes() { Assert.assertEquals(r.getLocalDateTime("max_dateTime"), maxDateTime); Assert.assertEquals(r.getLocalDateTime(4), maxDateTime); }); + verifiers.add(r -> { + Assert.assertTrue(r.hasValue("min_dateTime64"), "No value for column min_dateTime64 found"); + Assert.assertEquals(r.getLocalDateTime("min_dateTime64"), minDateTime64); + Assert.assertEquals(r.getLocalDateTime(5), minDateTime64); + }); + verifiers.add(r -> { + Assert.assertTrue(r.hasValue("max_dateTime64"), "No value for column max_dateTime64 found"); + Assert.assertEquals(r.getLocalDateTime("max_dateTime64"), maxDateTime64); + Assert.assertEquals(r.getLocalDateTime(6), maxDateTime64); + }); + verifiers.add(r -> { + Assert.assertTrue(r.hasValue("min_dateTime64_6"), "No value for column min_dateTime64_6 found"); + Assert.assertEquals(r.getLocalDateTime("min_dateTime64_6"), minDateTime64_6); + Assert.assertEquals(r.getLocalDateTime(7), minDateTime64_6); + }); + verifiers.add(r -> { + Assert.assertTrue(r.hasValue("max_dateTime64_6"), "No value for column max_dateTime64_6 found"); + Assert.assertEquals(r.getLocalDateTime("max_dateTime64_6"), maxDateTime64_6); + Assert.assertEquals(r.getLocalDateTime(8), maxDateTime64_6); + }); + verifiers.add(r -> { + Assert.assertTrue(r.hasValue("min_dateTime64_9"), "No value for column min_dateTime64_9 found"); + Assert.assertEquals(r.getLocalDateTime("min_dateTime64_9"), minDateTime64_9); + Assert.assertEquals(r.getLocalDateTime(9), minDateTime64_9); + }); + verifiers.add(r -> { + Assert.assertTrue(r.hasValue("max_dateTime64_9"), "No value for column max_dateTime64_9 found"); + Assert.assertEquals(r.getLocalDateTime("max_dateTime64_9"), maxDateTime64_9); + Assert.assertEquals(r.getLocalDateTime(10), maxDateTime64_9); + }); testDataTypes(columns, valueGenerators, verifiers); }