Skip to content

Commit

Permalink
fix(clickhouse): handle DateTime with no TZ (#140)
Browse files Browse the repository at this point in the history
close #136
  • Loading branch information
loicmathieu authored Jul 3, 2023
1 parent cdab0f8 commit 13c6be9
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

public class ClickHouseCellConverter extends AbstractCellConverter {
private static final Pattern PATTERN = Pattern.compile("DateTime(64)?\\((.*)'(.*)'\\)");
private static final DateTimeFormatter DATE_TIME_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss[.SSSSSS][.SSSSS][.SSSS][.SSS][.SS][.S]");

public ClickHouseCellConverter(ZoneId zoneId) {
super(zoneId);
Expand All @@ -35,7 +36,14 @@ public Object convertCell(int columnIndex, ResultSet rs, Connection connection)
Object columnVal = rs.getObject(columnIndex);
String columnTypeName = rs.getMetaData().getColumnTypeName(columnIndex);

if (columnTypeName.startsWith("DateTime")) {
if (columnTypeName.equals("DateTime")) {
// date with no TZ, we use the server default one
return LocalDateTime
.parse(
rs.getString(columnIndex),
DATE_TIME_FORMAT
);
} else if (columnTypeName.startsWith("DateTime")) {
Matcher matcher = PATTERN.matcher(columnTypeName);
if (!matcher.find() || matcher.groupCount() < 3) {
throw new IllegalArgumentException("Invalid Column Type '" + columnTypeName + "'");
Expand All @@ -44,7 +52,7 @@ public Object convertCell(int columnIndex, ResultSet rs, Connection connection)
return LocalDateTime
.parse(
rs.getString(columnIndex),
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss[.SSSSSS][.SSSSS][.SSSS][.SSS][.SS][.S]")
DATE_TIME_FORMAT
)
.atZone(ZoneId.of(matcher.group(3)))
.withZoneSameInstant(zoneId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ void select() throws Exception {
assertThat(runOutput.getRow().get("Uuid"), is("6bbf0744-74b4-46b9-bb05-53905d4538e7"));
assertThat(runOutput.getRow().get("Date"), is(LocalDate.parse("2030-12-25")));
assertThat(runOutput.getRow().get("DateTime"), is(LocalDateTime.parse("2004-10-19T08:23:54").atZone(ZoneId.of("Europe/Paris"))));
assertThat(runOutput.getRow().get("DateTimeNoTZ"), is(LocalDateTime.parse("2004-10-19T10:23:54")));
assertThat(runOutput.getRow().get("DateTime64"), is(LocalDateTime.parse("2004-10-19T08:23:54.999").atZone(ZoneId.of("Europe/Paris"))));
assertThat(runOutput.getRow().get("Enum"), is("hello"));
assertThat(runOutput.getRow().get("LowCardinality"), is("four"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ CREATE TABLE clickhouse_types (
Uuid UUID,
Date Date,
DateTime DateTime('Europe/Moscow'),
DateTimeNoTZ DateTime,
DateTime64 DateTime64(3, 'Europe/Moscow'),
Enum Enum('hello' = 1, 'world' = 2),
LowCardinality LowCardinality(String),
Expand Down Expand Up @@ -44,6 +45,7 @@ INSERT INTO clickhouse_types
Uuid,
Date,
DateTime,
DateTimeNoTZ,
DateTime64,
Enum,
LowCardinality,
Expand All @@ -66,6 +68,7 @@ VALUES (
'6bbf0744-74b4-46b9-bb05-53905d4538e7',
'2030-12-25',
'2004-10-19T10:23:54',
'2004-10-19T10:23:54',
'2004-10-19T10:23:54.999999',
'hello',
'four',
Expand Down

0 comments on commit 13c6be9

Please sign in to comment.