Skip to content
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

[client-v2] Fixes BinaryStreamReader parsing DateTime64 values. Using a column defined scale now. #1863

Merged
merged 1 commit into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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");

}
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ public <T> 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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Supplier<String>> 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<Consumer<ClickHouseBinaryFormatReader>> verifiers = new ArrayList<>();
Expand All @@ -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);
}
Expand Down
Loading