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
2 changes: 1 addition & 1 deletion src/main/java/org/duckdb/DuckDBResultSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -1356,7 +1356,7 @@ public <T> T getObject(int columnIndex, Class<T> type) throws SQLException {
", SQL type: " + sqlType);
}
} else if (type == LocalDateTime.class) {
if (isTimestamp(sqlType)) {
if (isTimestamp(sqlType) || sqlType == DuckDBColumnType.DATE) {
return type.cast(getLocalDateTime(columnIndex));
} else {
throw new SQLException("Can't convert value to LocalDateTime, Java type: " + type +
Expand Down
22 changes: 20 additions & 2 deletions src/main/java/org/duckdb/DuckDBVector.java
Original file line number Diff line number Diff line change
Expand Up @@ -632,11 +632,24 @@ private boolean isType(DuckDBColumnType columnType) {
return duckdb_type == columnType;
}

private LocalDateTime getLocalDateTimeFromDate(int idx) throws SQLException {
LocalDate ld = getLocalDate(idx);
if (ld == null) {
return null;
}
return ld.atStartOfDay();
}

Timestamp getTimestamp(int idx, Calendar calNullable) throws SQLException {
if (check_and_null(idx)) {
return null;
}
LocalDateTime ldt = getLocalDateTimeFromTimestamp(idx, calNullable);
final LocalDateTime ldt;
if (duckdb_type == DuckDBColumnType.DATE) {
ldt = getLocalDateTimeFromDate(idx);
} else {
ldt = getLocalDateTimeFromTimestamp(idx, calNullable);
}
if (ldt != null) {
return Timestamp.valueOf(ldt);
}
Expand All @@ -648,7 +661,12 @@ Timestamp getTimestamp(int idx, Calendar calNullable) throws SQLException {
}

LocalDateTime getLocalDateTime(int idx) throws SQLException {
LocalDateTime ldt = getLocalDateTimeFromTimestamp(idx, null);
final LocalDateTime ldt;
if (duckdb_type == DuckDBColumnType.DATE) {
ldt = getLocalDateTimeFromDate(idx);
} else {
ldt = getLocalDateTimeFromTimestamp(idx, null);
}
if (ldt != null) {
return ldt;
}
Expand Down
32 changes: 32 additions & 0 deletions src/test/java/org/duckdb/TestTimestamp.java
Original file line number Diff line number Diff line change
Expand Up @@ -581,4 +581,36 @@ public static void test_timestamp_before_epoch() throws Exception {
TimeZone.setDefault(defaultTimeZone);
}
}

public static void test_timestamp_read_ts_from_date() throws Exception {
try (Connection conn = DriverManager.getConnection(JDBC_URL); Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT '2020-01-02'::DATE")) {
assertTrue(rs.next());
assertEquals(rs.getTimestamp(1).toLocalDateTime().toLocalDate(), LocalDate.of(2020, 1, 2));
assertFalse(rs.next());
}
try (Connection conn = DriverManager.getConnection(JDBC_URL); Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT NULL::DATE")) {
assertTrue(rs.next());
assertNull(rs.getTimestamp(1));
assertTrue(rs.wasNull());
assertFalse(rs.next());
}
}

public static void test_timestamp_read_ldt_from_date() throws Exception {
try (Connection conn = DriverManager.getConnection(JDBC_URL); Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT '2020-01-02'::DATE")) {
assertTrue(rs.next());
assertEquals(rs.getObject(1, LocalDateTime.class).toLocalDate(), LocalDate.of(2020, 1, 2));
assertFalse(rs.next());
}
try (Connection conn = DriverManager.getConnection(JDBC_URL); Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT NULL::DATE")) {
assertTrue(rs.next());
assertNull(rs.getObject(1, LocalDateTime.class));
assertTrue(rs.wasNull());
assertFalse(rs.next());
}
}
}