Skip to content

Commit

Permalink
Merge pull request #1020 from zhicwu/develop
Browse files Browse the repository at this point in the history
apply timezone to datetime default value
  • Loading branch information
zhicwu authored Jul 28, 2022
2 parents 0e0a214 + d4a0b61 commit a3a8995
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.TimeZone;
Expand All @@ -24,7 +25,8 @@ public class ClickHouseDateTimeValue extends ClickHouseObjectValue<LocalDateTime
/**
* Default value.
*/
public static final LocalDateTime DEFAULT = ClickHouseDateValue.DEFAULT.atStartOfDay();
public static final LocalDateTime DEFAULT = ClickHouseInstantValue.DEFAULT.atOffset(ZoneOffset.UTC)
.toLocalDateTime();

static final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

Expand Down Expand Up @@ -111,11 +113,15 @@ public static ClickHouseDateTimeValue of(ClickHouseValue ref, String value, int

private final int scale;
private final TimeZone tz;
private final LocalDateTime defaultValue;

protected ClickHouseDateTimeValue(LocalDateTime value, int scale, TimeZone tz) {
super(value);
this.scale = ClickHouseChecker.between(scale, ClickHouseValues.PARAM_SCALE, 0, 9);
this.tz = tz != null ? tz : ClickHouseValues.UTC_TIMEZONE;
this.defaultValue = this.tz.equals(ClickHouseValues.UTC_TIMEZONE) ? DEFAULT
: ClickHouseOffsetDateTimeValue.DEFAULT.toZonedDateTime().withZoneSameInstant(this.tz.toZoneId())
.toLocalDateTime();
}

public int getScale() {
Expand Down Expand Up @@ -237,7 +243,7 @@ public String asString(int length, Charset charset) {

@Override
public ClickHouseDateTimeValue resetToDefault() {
set(DEFAULT);
set(defaultValue);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class ClickHouseDateValue extends ClickHouseObjectValue<LocalDate> {
/**
* Default date.
*/
public static final LocalDate DEFAULT = LocalDate.of(1970, 1, 1);
public static final LocalDate DEFAULT = ClickHouseOffsetDateTimeValue.DEFAULT.toLocalDate();

/**
* Create a new instance representing null value.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,15 @@ public static ClickHouseOffsetDateTimeValue of(ClickHouseValue ref, LocalDateTim

private final int scale;
private final TimeZone tz;
private final OffsetDateTime defaultValue;

protected ClickHouseOffsetDateTimeValue(OffsetDateTime value, int scale, TimeZone tz) {
super(value);
this.scale = ClickHouseChecker.between(scale, ClickHouseValues.PARAM_SCALE, 0, 9);
this.tz = tz == null || tz.equals(ClickHouseValues.UTC_TIMEZONE) ? ClickHouseValues.UTC_TIMEZONE : tz;
this.defaultValue = this.tz.equals(ClickHouseValues.UTC_TIMEZONE) ? DEFAULT
: ClickHouseInstantValue.DEFAULT
.atOffset(tz.toZoneId().getRules().getOffset(ClickHouseInstantValue.DEFAULT));
}

public int getScale() {
Expand Down Expand Up @@ -221,9 +225,7 @@ public String asString(int length, Charset charset) {

@Override
public ClickHouseOffsetDateTimeValue resetToDefault() {
set(tz == null ? DEFAULT
: ClickHouseInstantValue.DEFAULT
.atOffset(tz.toZoneId().getRules().getOffset(ClickHouseInstantValue.DEFAULT)));
set(defaultValue);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Calendar;
import java.util.Collections;
import java.util.Properties;
import java.util.TimeZone;
Expand All @@ -36,7 +37,9 @@
import com.clickhouse.client.ClickHouseProtocol;
import com.clickhouse.client.config.ClickHouseClientOption;
import com.clickhouse.client.data.ClickHouseBitmap;
import com.clickhouse.client.data.ClickHouseDateTimeValue;
import com.clickhouse.client.data.ClickHouseExternalTable;
import com.clickhouse.client.data.ClickHouseOffsetDateTimeValue;
import com.clickhouse.jdbc.internal.InputBasedPreparedStatement;
import com.clickhouse.jdbc.internal.SqlBasedPreparedStatement;

Expand Down Expand Up @@ -1480,6 +1483,39 @@ public void testInsertWithMultipleValues() throws Exception {
}
}

@Test(groups = "integration")
public void testInsertWithNullDateTime() throws Exception {
Properties props = new Properties();
props.setProperty(JdbcConfig.PROP_NULL_AS_DEFAULT, "2");
try (ClickHouseConnection conn = newConnection(props);
Statement s = conn.createStatement()) {
s.execute("drop table if exists test_insert_with_null_datetime; "
+ "CREATE TABLE test_insert_with_null_datetime(a Int32, "
+ "b01 DateTime32, b02 DateTime32('America/Los_Angeles'), "
+ "b11 DateTime32, b12 DateTime32('America/Los_Angeles'), "
+ "c01 DateTime64(3), c02 DateTime64(6, 'Asia/Shanghai'), "
+ "c11 DateTime64(3), c12 DateTime64(6, 'Asia/Shanghai')) ENGINE=Memory");
try (PreparedStatement ps = conn
.prepareStatement("INSERT INTO test_insert_with_null_datetime values(?, ? ,? ,?,?)")) {
ps.setInt(1, 1);
ps.setObject(2, LocalDateTime.now());
ps.setObject(3, LocalDateTime.now());
ps.setTimestamp(4, null);
ps.setNull(5, Types.TIMESTAMP);
ps.setObject(6, LocalDateTime.now());
ps.setObject(7, LocalDateTime.now());
ps.setObject(8, null);
ps.setTimestamp(9, null, Calendar.getInstance());
ps.executeUpdate();
}

try (ResultSet rs = s.executeQuery("select * from test_insert_with_null_datetime order by a")) {
Assert.assertTrue(rs.next());
Assert.assertFalse(rs.next());
}
}
}

@Test(groups = "integration")
public void testGetParameterMetaData() throws Exception {
try (Connection conn = newConnection(new Properties());
Expand Down

0 comments on commit a3a8995

Please sign in to comment.