Skip to content

Commit 7d6c01b

Browse files
committed
Tableau fix getDate with calendar
1 parent 33b1fad commit 7d6c01b

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

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

Lines changed: 9 additions & 1 deletion
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;
@@ -57,7 +58,7 @@ public abstract class AbstractBinaryFormatReader implements ClickHouseBinaryForm
5758

5859
private volatile boolean hasNext = true;
5960

60-
61+
private DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
6162
private volatile boolean initialState = true; // reader is in initial state, no records have been read yet
6263

6364
protected AbstractBinaryFormatReader(InputStream inputStream, QuerySettings querySettings, TableSchema schema,
@@ -279,6 +280,13 @@ public String getString(String colName) {
279280
return null;
280281
} else if (value instanceof String) {
281282
return (String) value;
283+
} else if (value instanceof ZonedDateTime) {
284+
ClickHouseDataType dataType = schema.getColumnByName(colName).getDataType();
285+
ZonedDateTime zdt = (ZonedDateTime) value;
286+
if (dataType == ClickHouseDataType.Date) {
287+
return zdt.format(dateTimeFormatter).toString();
288+
}
289+
return value.toString();
282290
} else {
283291
ClickHouseDataType dataType = schema.getColumnByName(colName).getDataType();
284292
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

0 commit comments

Comments
 (0)