Skip to content
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 @@ -33,6 +33,7 @@
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
Expand All @@ -50,14 +51,9 @@ public abstract class AbstractBinaryFormatReader implements ClickHouseBinaryForm
protected BinaryStreamReader binaryStreamReader;

private TableSchema schema;

private ClickHouseColumn[] columns;

private Map[] convertions;

private volatile boolean hasNext = true;


private volatile boolean initialState = true; // reader is in initial state, no records have been read yet

protected AbstractBinaryFormatReader(InputStream inputStream, QuerySettings querySettings, TableSchema schema,
Expand Down Expand Up @@ -279,6 +275,13 @@ public String getString(String colName) {
return null;
} else if (value instanceof String) {
return (String) value;
} else if (value instanceof ZonedDateTime) {
ClickHouseDataType dataType = schema.getColumnByName(colName).getDataType();
ZonedDateTime zdt = (ZonedDateTime) value;
if (dataType == ClickHouseDataType.Date) {
return zdt.format(com.clickhouse.client.api.DataTypeUtils.DATE_FORMATTER).toString();
}
return value.toString();
} else {
ClickHouseDataType dataType = schema.getColumnByName(colName).getDataType();
if (dataType == ClickHouseDataType.Enum8 || dataType == ClickHouseDataType.Enum16) {
Expand Down
14 changes: 13 additions & 1 deletion jdbc-v2/src/main/java/com/clickhouse/jdbc/ResultSetImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Map;
import java.util.TimeZone;

import com.clickhouse.client.api.data_formats.ClickHouseBinaryFormatReader;
import com.clickhouse.client.api.metadata.TableSchema;
Expand All @@ -33,6 +35,7 @@ public class ResultSetImpl implements ResultSet, JdbcV2Wrapper {
private boolean closed;
private final StatementImpl parentStatement;
private boolean wasNull;
private final Calendar defaultCalendar;

public ResultSetImpl(StatementImpl parentStatement, QueryResponse response, ClickHouseBinaryFormatReader reader) {
this.parentStatement = parentStatement;
Expand All @@ -41,6 +44,7 @@ public ResultSetImpl(StatementImpl parentStatement, QueryResponse response, Clic
this.metaData = new com.clickhouse.jdbc.metadata.ResultSetMetaData(this);
this.closed = false;
this.wasNull = false;
this.defaultCalendar = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is a feature to use server timezone for date. So it may be applicable here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will check the JDBC v1 implementation to how we deal with that in the past

}

private void checkClosed() throws SQLException {
Expand Down Expand Up @@ -1234,7 +1238,15 @@ public Date getDate(int columnIndex, Calendar cal) throws SQLException {
@Override
public Date getDate(String columnLabel, Calendar cal) throws SQLException {
checkClosed();
return getDate(columnLabel);
Date date = getDate(columnLabel);
if (date == null) {
return null;
}
LocalDate d = date.toLocalDate();
Calendar c = (Calendar)( cal != null ? cal : defaultCalendar).clone();
c.clear();
c.set(d.getYear(), d.getMonthValue() - 1, d.getDayOfMonth(), 0, 0, 0);
return new Date(c.getTimeInMillis());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -888,7 +888,7 @@ public void testTypeConversions() throws Exception {
assertEquals(rs.getDate(4), Date.valueOf("2024-12-01"));
assertTrue(rs.getObject(4) instanceof Date);
assertEquals(rs.getObject(4), Date.valueOf("2024-12-01"));
assertEquals(rs.getString(4), "2024-12-01T00:00Z[UTC]");//Underlying object is ZonedDateTime
assertEquals(rs.getString(4), "2024-12-01");//Underlying object is ZonedDateTime
assertEquals(rs.getObject(4, LocalDate.class), LocalDate.of(2024, 12, 1));
assertEquals(rs.getObject(4, ZonedDateTime.class), ZonedDateTime.of(2024, 12, 1, 0, 0, 0, 0, ZoneId.of("UTC")));
assertEquals(String.valueOf(rs.getObject(4, new HashMap<String, Class<?>>(){{put(JDBCType.DATE.getName(), LocalDate.class);}})), "2024-12-01");
Expand Down
4 changes: 2 additions & 2 deletions jdbc-v2/src/test/java/com/clickhouse/jdbc/StatementTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ public void testExecuteQueryDates() throws Exception {
assertEquals(rs.getDate("date").toLocalDate().toString(), "2020-01-01");
assertEquals(rs.getDate(1, null).toLocalDate().toString(), "2020-01-01");
assertEquals(rs.getDate("date", null).toLocalDate().toString(), "2020-01-01");
assertEquals(rs.getString(1), "2020-01-01T00:00Z[UTC]");
assertEquals(rs.getString("date"), "2020-01-01T00:00Z[UTC]");
assertEquals(rs.getString(1), "2020-01-01");
assertEquals(rs.getString("date"), "2020-01-01");
assertEquals(rs.getDate(2).toString(), "2020-01-01");
assertEquals(rs.getDate("datetime").toString(), "2020-01-01");
assertEquals(rs.getDate(2).toLocalDate().toString(), "2020-01-01");
Expand Down
Loading