diff --git a/clickhouse-jdbc/src/main/java/com/clickhouse/jdbc/internal/InputBasedPreparedStatement.java b/clickhouse-jdbc/src/main/java/com/clickhouse/jdbc/internal/InputBasedPreparedStatement.java index 14e5d261a..e782c7820 100644 --- a/clickhouse-jdbc/src/main/java/com/clickhouse/jdbc/internal/InputBasedPreparedStatement.java +++ b/clickhouse-jdbc/src/main/java/com/clickhouse/jdbc/internal/InputBasedPreparedStatement.java @@ -434,7 +434,7 @@ public void setTimestamp(int parameterIndex, Timestamp x, Calendar cal) throws S } else { Calendar c = (Calendar) cal.clone(); c.setTime(x); - dt = c.toInstant().atZone(tz).withZoneSameInstant(timeZoneForTs).toLocalDateTime(); + dt = c.toInstant().atZone(tz).withNano(x.getNanos()).withZoneSameInstant(timeZoneForTs).toLocalDateTime(); } values[idx].update(dt); } else { diff --git a/clickhouse-jdbc/src/main/java/com/clickhouse/jdbc/internal/SqlBasedPreparedStatement.java b/clickhouse-jdbc/src/main/java/com/clickhouse/jdbc/internal/SqlBasedPreparedStatement.java index 45eb10968..05e3c61ba 100644 --- a/clickhouse-jdbc/src/main/java/com/clickhouse/jdbc/internal/SqlBasedPreparedStatement.java +++ b/clickhouse-jdbc/src/main/java/com/clickhouse/jdbc/internal/SqlBasedPreparedStatement.java @@ -562,7 +562,7 @@ public void setTimestamp(int parameterIndex, Timestamp x, Calendar cal) throws S } else { Calendar c = (Calendar) cal.clone(); c.setTime(x); - dt = c.toInstant().atZone(tz).withZoneSameInstant(timeZoneForTs).toLocalDateTime(); + dt = c.toInstant().atZone(tz).withNano(x.getNanos()).withZoneSameInstant(timeZoneForTs).toLocalDateTime(); } ClickHouseValue value = templates[idx]; diff --git a/clickhouse-jdbc/src/test/java/com/clickhouse/jdbc/ClickHouseStatementTest.java b/clickhouse-jdbc/src/test/java/com/clickhouse/jdbc/ClickHouseStatementTest.java index 9349cf9dc..cccc23993 100644 --- a/clickhouse-jdbc/src/test/java/com/clickhouse/jdbc/ClickHouseStatementTest.java +++ b/clickhouse-jdbc/src/test/java/com/clickhouse/jdbc/ClickHouseStatementTest.java @@ -611,6 +611,33 @@ public void testTimestamp() throws SQLException { } } + @Test(groups = "integration") + public void testTimestampWithNanoSeconds() throws SQLException { + Properties props = new Properties(); + try (ClickHouseConnection conn = newConnection(props); + ClickHouseStatement stmt = conn.createStatement()) { + stmt.execute("drop table if exists test_timetamp_with_nanos;" + + "create table test_timetamp_with_nanos(d DateTime64(9))engine=Memory"); + Instant instant = Instant.now(); + Timestamp now = new Timestamp(instant.toEpochMilli()); + now.setNanos(instant.getNano()); + try (PreparedStatement ps1 = conn.prepareStatement("insert into test_timetamp_with_nanos"); + PreparedStatement ps2 = conn + .prepareStatement( + "insert into test_timetamp_with_nanos values(toDateTime64(?, 9))")) { + ps1.setTimestamp(1, now); + ps1.executeUpdate(); + + ps2.setTimestamp(1, now); + ps2.executeUpdate(); + } + ResultSet rs = stmt.executeQuery("select distinct * from test_timetamp_with_nanos"); + Assert.assertTrue(rs.next()); + Assert.assertEquals(rs.getObject(1, Instant.class), instant); + Assert.assertFalse(rs.next()); + } + } + // @Test(groups = "integration") // public void testAggregateFunction() throws SQLException { // Properties props = new Properties();