Skip to content

Commit 5e2874c

Browse files
authored
Tableau fix getDate with calendar (#2062)
* Tableau fix getDate with calendar * Use date fromat from a Util class * Fix date tests
1 parent 33b1fad commit 5e2874c

File tree

4 files changed

+24
-9
lines changed

4 files changed

+24
-9
lines changed

client-v2/src/main/java/com/clickhouse/client/api/data_formats/internal/AbstractBinaryFormatReader.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.time.LocalDateTime;
3434
import java.time.ZoneOffset;
3535
import java.time.ZonedDateTime;
36+
import java.time.format.DateTimeFormatter;
3637
import java.time.temporal.ChronoUnit;
3738
import java.util.*;
3839
import java.util.concurrent.ConcurrentHashMap;
@@ -50,14 +51,9 @@ public abstract class AbstractBinaryFormatReader implements ClickHouseBinaryForm
5051
protected BinaryStreamReader binaryStreamReader;
5152

5253
private TableSchema schema;
53-
5454
private ClickHouseColumn[] columns;
55-
5655
private Map[] convertions;
57-
5856
private volatile boolean hasNext = true;
59-
60-
6157
private volatile boolean initialState = true; // reader is in initial state, no records have been read yet
6258

6359
protected AbstractBinaryFormatReader(InputStream inputStream, QuerySettings querySettings, TableSchema schema,
@@ -279,6 +275,13 @@ public String getString(String colName) {
279275
return null;
280276
} else if (value instanceof String) {
281277
return (String) value;
278+
} else if (value instanceof ZonedDateTime) {
279+
ClickHouseDataType dataType = schema.getColumnByName(colName).getDataType();
280+
ZonedDateTime zdt = (ZonedDateTime) value;
281+
if (dataType == ClickHouseDataType.Date) {
282+
return zdt.format(com.clickhouse.client.api.DataTypeUtils.DATE_FORMATTER).toString();
283+
}
284+
return value.toString();
282285
} else {
283286
ClickHouseDataType dataType = schema.getColumnByName(colName).getDataType();
284287
if (dataType == ClickHouseDataType.Enum8 || dataType == ClickHouseDataType.Enum16) {

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
import java.time.LocalDate;
1414
import java.time.LocalDateTime;
1515
import java.util.Calendar;
16+
import java.util.GregorianCalendar;
1617
import java.util.Map;
18+
import java.util.TimeZone;
1719

1820
import com.clickhouse.client.api.data_formats.ClickHouseBinaryFormatReader;
1921
import com.clickhouse.client.api.metadata.TableSchema;
@@ -33,6 +35,7 @@ public class ResultSetImpl implements ResultSet, JdbcV2Wrapper {
3335
private boolean closed;
3436
private final StatementImpl parentStatement;
3537
private boolean wasNull;
38+
private final Calendar defaultCalendar;
3639

3740
public ResultSetImpl(StatementImpl parentStatement, QueryResponse response, ClickHouseBinaryFormatReader reader) {
3841
this.parentStatement = parentStatement;
@@ -41,6 +44,7 @@ public ResultSetImpl(StatementImpl parentStatement, QueryResponse response, Clic
4144
this.metaData = new com.clickhouse.jdbc.metadata.ResultSetMetaData(this);
4245
this.closed = false;
4346
this.wasNull = false;
47+
this.defaultCalendar = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
4448
}
4549

4650
private void checkClosed() throws SQLException {
@@ -1234,7 +1238,15 @@ public Date getDate(int columnIndex, Calendar cal) throws SQLException {
12341238
@Override
12351239
public Date getDate(String columnLabel, Calendar cal) throws SQLException {
12361240
checkClosed();
1237-
return getDate(columnLabel);
1241+
Date date = getDate(columnLabel);
1242+
if (date == null) {
1243+
return null;
1244+
}
1245+
LocalDate d = date.toLocalDate();
1246+
Calendar c = (Calendar)( cal != null ? cal : defaultCalendar).clone();
1247+
c.clear();
1248+
c.set(d.getYear(), d.getMonthValue() - 1, d.getDayOfMonth(), 0, 0, 0);
1249+
return new Date(c.getTimeInMillis());
12381250
}
12391251

12401252
@Override

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -888,7 +888,7 @@ public void testTypeConversions() throws Exception {
888888
assertEquals(rs.getDate(4), Date.valueOf("2024-12-01"));
889889
assertTrue(rs.getObject(4) instanceof Date);
890890
assertEquals(rs.getObject(4), Date.valueOf("2024-12-01"));
891-
assertEquals(rs.getString(4), "2024-12-01T00:00Z[UTC]");//Underlying object is ZonedDateTime
891+
assertEquals(rs.getString(4), "2024-12-01");//Underlying object is ZonedDateTime
892892
assertEquals(rs.getObject(4, LocalDate.class), LocalDate.of(2024, 12, 1));
893893
assertEquals(rs.getObject(4, ZonedDateTime.class), ZonedDateTime.of(2024, 12, 1, 0, 0, 0, 0, ZoneId.of("UTC")));
894894
assertEquals(String.valueOf(rs.getObject(4, new HashMap<String, Class<?>>(){{put(JDBCType.DATE.getName(), LocalDate.class);}})), "2024-12-01");

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,8 @@ public void testExecuteQueryDates() throws Exception {
131131
assertEquals(rs.getDate("date").toLocalDate().toString(), "2020-01-01");
132132
assertEquals(rs.getDate(1, null).toLocalDate().toString(), "2020-01-01");
133133
assertEquals(rs.getDate("date", null).toLocalDate().toString(), "2020-01-01");
134-
assertEquals(rs.getString(1), "2020-01-01T00:00Z[UTC]");
135-
assertEquals(rs.getString("date"), "2020-01-01T00:00Z[UTC]");
134+
assertEquals(rs.getString(1), "2020-01-01");
135+
assertEquals(rs.getString("date"), "2020-01-01");
136136
assertEquals(rs.getDate(2).toString(), "2020-01-01");
137137
assertEquals(rs.getDate("datetime").toString(), "2020-01-01");
138138
assertEquals(rs.getDate(2).toLocalDate().toString(), "2020-01-01");

0 commit comments

Comments
 (0)