Skip to content

Commit 3a92da0

Browse files
author
Paultagoras
committed
Adjusting date/time conversion
1 parent 8ca05b3 commit 3a92da0

File tree

2 files changed

+47
-54
lines changed

2 files changed

+47
-54
lines changed

jdbc-v2/src/main/java/com/clickhouse/jdbc/ResultSetImpl.java

Lines changed: 34 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import java.sql.*;
1212
import java.time.LocalDate;
1313
import java.time.LocalDateTime;
14+
import java.time.ZoneId;
15+
import java.time.ZonedDateTime;
1416
import java.util.Calendar;
1517
import java.util.GregorianCalendar;
1618
import java.util.Map;
@@ -179,12 +181,12 @@ public byte[] getBytes(int columnIndex) throws SQLException {
179181

180182
@Override
181183
public Date getDate(int columnIndex) throws SQLException {
182-
return getDate(columnIndexToName(columnIndex));
184+
return getDate(columnIndex, null);
183185
}
184186

185187
@Override
186188
public Time getTime(int columnIndex) throws SQLException {
187-
return getTime(columnIndexToName(columnIndex));
189+
return getTime(columnIndex, null);
188190
}
189191

190192
@Override
@@ -369,37 +371,12 @@ public byte[] getBytes(String columnLabel) throws SQLException {
369371

370372
@Override
371373
public Date getDate(String columnLabel) throws SQLException {
372-
checkClosed();
373-
try {
374-
//TODO: Add this to ClickHouseBinaryFormatReader
375-
LocalDate localDate = reader.getLocalDate(columnLabel);
376-
if (localDate == null) {
377-
wasNull = true;
378-
return null;
379-
}
380-
381-
wasNull = false;
382-
return Date.valueOf(localDate);
383-
} catch (Exception e) {
384-
throw ExceptionUtils.toSqlState(String.format("SQL: [%s]; Method: getDate(%s)", parentStatement.getLastSql(), columnLabel), e);
385-
}
374+
return getDate(columnLabel, null);
386375
}
387376

388377
@Override
389378
public Time getTime(String columnLabel) throws SQLException {
390-
checkClosed();
391-
try {
392-
LocalDateTime localDateTime = reader.getLocalDateTime(columnLabel);
393-
if(localDateTime == null) {
394-
wasNull = true;
395-
return null;
396-
}
397-
398-
wasNull = false;
399-
return Time.valueOf(localDateTime.toLocalTime());
400-
} catch (Exception e) {
401-
throw ExceptionUtils.toSqlState(String.format("SQL: [%s]; Method: getTime(%s)", parentStatement.getLastSql(), columnLabel), e);
402-
}
379+
return getTime(columnLabel, null);
403380
}
404381

405382
@Override
@@ -1068,15 +1045,18 @@ public Date getDate(int columnIndex, Calendar cal) throws SQLException {
10681045
@Override
10691046
public Date getDate(String columnLabel, Calendar cal) throws SQLException {
10701047
checkClosed();
1071-
Date date = getDate(columnLabel);
1072-
if (date == null) {
1073-
return null;
1048+
try {
1049+
ZonedDateTime zdt = reader.getZonedDateTime(columnLabel);
1050+
if (zdt == null) {
1051+
wasNull = true;
1052+
return null;
1053+
}
1054+
wasNull = false;
1055+
ZoneId zoneId = cal != null ? cal.getTimeZone().toZoneId() : zdt.getZone();
1056+
return Date.valueOf(zdt.withZoneSameInstant(zoneId).toLocalDate());
1057+
} catch (Exception e) {
1058+
throw ExceptionUtils.toSqlState(String.format("SQL: [%s]; Method: getDate(%s, %s)", parentStatement.getLastSql(), columnLabel, cal), e);
10741059
}
1075-
LocalDate d = date.toLocalDate();
1076-
Calendar c = (Calendar)( cal != null ? cal : defaultCalendar).clone();
1077-
c.clear();
1078-
c.set(d.getYear(), d.getMonthValue() - 1, d.getDayOfMonth(), 0, 0, 0);
1079-
return new Date(c.getTimeInMillis());
10801060
}
10811061

10821062
@Override
@@ -1087,7 +1067,18 @@ public Time getTime(int columnIndex, Calendar cal) throws SQLException {
10871067
@Override
10881068
public Time getTime(String columnLabel, Calendar cal) throws SQLException {
10891069
checkClosed();
1090-
return getTime(columnLabel);
1070+
try {
1071+
ZonedDateTime zdt = reader.getZonedDateTime(columnLabel);
1072+
if (zdt == null) {
1073+
wasNull = true;
1074+
return null;
1075+
}
1076+
wasNull = false;
1077+
ZoneId zoneId = cal != null ? cal.getTimeZone().toZoneId() : zdt.getZone();
1078+
return Time.valueOf(zdt.withZoneSameInstant(zoneId).toLocalTime());
1079+
} catch (Exception e) {
1080+
throw ExceptionUtils.toSqlState(String.format("SQL: [%s]; Method: getTime(%s, %s)", parentStatement.getLastSql(), columnLabel, cal), e);
1081+
}
10911082
}
10921083

10931084
@Override
@@ -1099,20 +1090,16 @@ public Timestamp getTimestamp(int columnIndex, Calendar cal) throws SQLException
10991090
public Timestamp getTimestamp(String columnLabel, Calendar cal) throws SQLException {
11001091
checkClosed();
11011092
try {
1102-
LocalDateTime localDateTime = reader.getLocalDateTime(columnLabel);
1103-
if (localDateTime == null) {
1093+
ZonedDateTime zdt = reader.getZonedDateTime(columnLabel);
1094+
ZoneId zoneId = cal != null ? cal.getTimeZone().toZoneId() : zdt.getZone();
1095+
if (zdt == null) {
11041096
wasNull = true;
11051097
return null;
11061098
}
1107-
Calendar c = (Calendar) (cal != null ? cal : defaultCalendar).clone();
1108-
c.set(localDateTime.getYear(), localDateTime.getMonthValue() - 1, localDateTime.getDayOfMonth(), localDateTime.getHour(), localDateTime.getMinute(),
1109-
localDateTime.getSecond());
1110-
Timestamp timestamp = new Timestamp(c.getTimeInMillis());
1111-
timestamp.setNanos(localDateTime.getNano());
11121099
wasNull = false;
1113-
return timestamp;
1100+
return Timestamp.valueOf(zdt.withZoneSameInstant(zoneId).toLocalDateTime());
11141101
} catch (Exception e) {
1115-
throw ExceptionUtils.toSqlState(String.format("SQL: [%s]; Method: getTimestamp(%s)", parentStatement.getLastSql(), columnLabel), e);
1102+
throw ExceptionUtils.toSqlState(String.format("SQL: [%s]; Method: getTimestamp(%s, %s)", parentStatement.getLastSql(), columnLabel, cal), e);
11161103
}
11171104

11181105
}

jdbc-v2/src/test/java/com/clickhouse/jdbc/StatementTest.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,13 @@
2020
import java.sql.SQLException;
2121
import java.sql.Statement;
2222
import java.time.LocalDate;
23+
import java.time.ZonedDateTime;
2324
import java.util.Arrays;
25+
import java.util.Calendar;
26+
import java.util.GregorianCalendar;
2427
import java.util.List;
2528
import java.util.Properties;
29+
import java.util.TimeZone;
2630
import java.util.UUID;
2731
import java.util.concurrent.CountDownLatch;
2832
import java.util.concurrent.atomic.AtomicReference;
@@ -123,22 +127,24 @@ public void testExecuteQueryNulls() throws Exception {
123127
public void testExecuteQueryDates() throws Exception {
124128
try (Connection conn = getJdbcConnection()) {
125129
try (Statement stmt = conn.createStatement()) {
126-
try (ResultSet rs = stmt.executeQuery("SELECT toDate('2020-01-01 12:10:07') AS date, toDateTime('2020-01-01 10:11:12', 'Asia/Istanbul') AS datetime")) {
130+
try (ResultSet rs = stmt.executeQuery("SELECT toDate('2020-01-01 12:10:07', 'UTC') AS date, toDateTime('2020-01-01 10:11:12', 'Asia/Istanbul') AS datetime")) {
127131
assertTrue(rs.next());
128132
assertEquals(rs.getDate(1).toString(), "2020-01-01");
129133
assertEquals(rs.getDate("date").toString(), "2020-01-01");
130134
assertEquals(rs.getDate(1).toLocalDate().toString(), "2020-01-01");
131135
assertEquals(rs.getDate("date").toLocalDate().toString(), "2020-01-01");
132-
assertEquals(rs.getDate(1, null).toLocalDate().toString(), "2020-01-01");
133-
assertEquals(rs.getDate("date", null).toLocalDate().toString(), "2020-01-01");
136+
assertEquals(rs.getDate(1, null).toString(), "2020-01-01");
137+
assertEquals(rs.getDate("date", null).toString(), "2020-01-01");
138+
assertEquals(rs.getDate(1, new GregorianCalendar(TimeZone.getTimeZone("America/New_York"))).toString(), "2019-12-31");
139+
assertEquals(rs.getDate("date", new GregorianCalendar(TimeZone.getTimeZone("America/New_York"))).toString(), "2019-12-31");
134140
assertEquals(rs.getString(1), "2020-01-01");
135141
assertEquals(rs.getString("date"), "2020-01-01");
136142
assertEquals(rs.getDate(2).toString(), "2020-01-01");
137143
assertEquals(rs.getDate("datetime").toString(), "2020-01-01");
138-
assertEquals(rs.getDate(2).toLocalDate().toString(), "2020-01-01");
139-
assertEquals(rs.getDate("datetime").toLocalDate().toString(), "2020-01-01");
140-
assertEquals(rs.getDate(2, null).toLocalDate().toString(), "2020-01-01");
141-
assertEquals(rs.getDate("datetime", null).toLocalDate().toString(), "2020-01-01");
144+
assertEquals(rs.getDate(2).toString(), "2020-01-01");
145+
assertEquals(rs.getDate("datetime").toString(), "2020-01-01");
146+
assertEquals(rs.getDate(2, null).toString(), "2020-01-01");
147+
assertEquals(rs.getDate("datetime", new GregorianCalendar(TimeZone.getTimeZone("Asia/Istanbul"))).toString(), "2020-01-01");
142148
assertEquals(rs.getString(2), "2020-01-01T10:11:12+03:00[Asia/Istanbul]");
143149
assertEquals(rs.getString("datetime"), "2020-01-01T10:11:12+03:00[Asia/Istanbul]");
144150
assertFalse(rs.next());

0 commit comments

Comments
 (0)