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
72 changes: 47 additions & 25 deletions src/main/java/se/alipsa/jparq/JParqResultSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -1715,23 +1715,28 @@ public byte[] getBytes(int columnIndex) throws SQLException {
* one-based column index
* @return JDBC date, or null for SQL NULL
* @throws SQLException
* if the value is unavailable or cannot be converted to DATE
* if the value is unavailable or cannot be converted to DATE, with
* SQLState 22007 for an invalid format or 22008 for out-of-range
* fields
*/
@Override
public Date getDate(int columnIndex) throws SQLException {
Object v = value(columnIndex);
return switch (v) {
case null -> null;
case Date date -> date;
case Timestamp ts -> new Date(ts.getTime());
case OffsetDateTime timestamp -> new Date(timestamp.toInstant().toEpochMilli());
case String s -> Date.valueOf(s);
case Long l -> new Date(l);
case Double d -> new Date(d.longValue());
case LocalDateTime l -> Date.valueOf(l.toLocalDate());
case LocalDate localDate -> Date.valueOf(localDate);
default -> throw new SQLException("Unsupported date type: " + v.getClass().getName());
};
if (v == null) {
return null;
}
if (v instanceof OffsetDateTime timestamp) {
v = new Date(timestamp.toInstant().toEpochMilli());
}
if (!(v instanceof Date || v instanceof Timestamp || v instanceof String || v instanceof Long || v instanceof Double
|| v instanceof LocalDateTime || v instanceof LocalDate)) {
throw new SQLException("Unsupported date type: " + v.getClass().getName());
}
try {
return DateTimeFunctions.toDate(v);
} catch (RuntimeException exception) {
throw SqlExceptions.wrap("Failed to convert column " + columnIndex + " to DATE", exception);
}
}

@Override
Expand All @@ -1747,7 +1752,9 @@ public Date getDate(String columnLabel) throws SQLException {
* one-based column index
* @return JDBC time, or null for SQL NULL
* @throws SQLException
* if the value is unavailable or cannot be converted to TIME
* if the value is unavailable or cannot be converted to TIME, with
* SQLState 22007 for an invalid format or 22008 for out-of-range
* fields
*/
@Override
public Time getTime(int columnIndex) throws SQLException {
Expand Down Expand Up @@ -1777,20 +1784,35 @@ public Time getTime(String columnLabel) throws SQLException {
return getTime(findColumn(columnLabel));
}

/**
* Returns the JVM-local timestamp, converting offset timestamp instants first.
*
* @param columnIndex
* one-based column index
* @return JDBC timestamp, or null for SQL NULL
* @throws SQLException
* if the value is unavailable or cannot be converted to TIMESTAMP,
* with SQLState 22007 for an invalid format or 22008 for out-of-range
* fields
*/
@Override
public Timestamp getTimestamp(int columnIndex) throws SQLException {
Object v = value(columnIndex);
return switch (v) {
case null -> null;
case Timestamp t -> t;
case OffsetDateTime timestamp -> Timestamp.from(timestamp.toInstant());
case Date d -> new Timestamp(d.getTime());
case String s -> Timestamp.valueOf(s);
case Long l -> new Timestamp(l);
case Double d -> new Timestamp(d.longValue());
case LocalDateTime l -> Timestamp.valueOf(l);
default -> throw new SQLException("Unsupported timestamp type: " + v.getClass().getName());
};
if (v == null) {
return null;
}
if (v instanceof OffsetDateTime timestamp) {
v = Timestamp.from(timestamp.toInstant());
}
if (!(v instanceof Timestamp || v instanceof Date || v instanceof String || v instanceof Long || v instanceof Double
|| v instanceof LocalDateTime)) {
throw new SQLException("Unsupported timestamp type: " + v.getClass().getName());
}
try {
return DateTimeFunctions.toTimestamp(v);
} catch (RuntimeException exception) {
throw SqlExceptions.wrap("Failed to convert column " + columnIndex + " to TIMESTAMP", exception);
}
}

@Override
Expand Down
99 changes: 99 additions & 0 deletions src/test/java/jparq/TemporalGetStringConversionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package jparq;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.nio.file.Path;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.SQLDataException;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

/**
* Verifies that temporal getters convert VARCHAR values strictly and report
* classified SQL failures instead of leaking parser exceptions.
*/
class TemporalGetStringConversionTest {

/**
* Supplies invalid VARCHAR literals with their accessor and expected state.
*
* @return invalid temporal string cases
*/
static Stream<Arguments> invalidTemporalStrings() {
return Stream.of(Arguments.of("'bad'", "date", "22007"), Arguments.of("'bad'", "time", "22007"),
Arguments.of("'bad'", "timestamp", "22007"), Arguments.of("'2024-02-30'", "date", "22008"),
Arguments.of("'2024-02-30'", "timestamp", "22008"), Arguments.of("'2024-13-01'", "date", "22008"),
Arguments.of("'2024-13-01'", "timestamp", "22008"), Arguments.of("'25:00:00'", "time", "22008"),
Arguments.of("'25:00:00'", "timestamp", "22007"), Arguments.of("'2024-01-01 12:60:00'", "timestamp", "22008"));
}

/**
* Rejects VARCHAR values that no supported temporal format accepts.
*
* @param value
* the invalid VARCHAR literal
* @param getter
* the temporal accessor
* @param state
* expected data SQLState
* @throws Exception
* if fixture lookup or connection setup fails
*/
@ParameterizedTest
@MethodSource("invalidTemporalStrings")
void gettersRejectInvalidTemporalStrings(String value, String getter, String state) throws Exception {
String url = "jdbc:jparq:"
+ Path.of(TemporalGetStringConversionTest.class.getResource("/mtcars.parquet").toURI()).getParent();
try (var connection = DriverManager.getConnection(url);
var statement = connection.createStatement();
var result = statement.executeQuery("SELECT " + value + " AS x FROM mtcars LIMIT 1")) {
assertTrue(result.next());
SQLDataException failure = assertThrows(SQLDataException.class, () -> {
switch (getter) {
case "date" -> result.getDate(1);
case "time" -> result.getTime(1);
default -> result.getTimestamp(1);
}
});
assertEquals(state, failure.getSQLState());
assertTrue(failure.getMessage().contains(getter.toUpperCase()), failure::getMessage);
}
}

/**
* Retains valid VARCHAR temporal values, including unpadded SQL fields, and
* keeps numeric epoch values on the millisecond path.
*
* @throws Exception
* if fixture lookup or connection setup fails
*/
@Test
void gettersRetainValidTemporalStrings() throws Exception {
String url = "jdbc:jparq:"
+ Path.of(TemporalGetStringConversionTest.class.getResource("/mtcars.parquet").toURI()).getParent();
try (var connection = DriverManager.getConnection(url);
var statement = connection.createStatement();
var result = statement.executeQuery(
"SELECT '2024-2-29' AS d, '10:20:30' AS t, '2024-02-29T10:20:30' AS ts FROM mtcars " + "LIMIT 1")) {
assertTrue(result.next());
assertEquals(Date.valueOf("2024-02-29"), result.getDate(1));
assertEquals(Time.valueOf("10:20:30"), result.getTime(2));
assertEquals(Timestamp.valueOf("2024-02-29 10:20:30"), result.getTimestamp(3));
}
try (var connection = DriverManager.getConnection(url);
var statement = connection.createStatement();
var result = statement.executeQuery("SELECT 1700000000000 AS x FROM mtcars LIMIT 1")) {
assertTrue(result.next());
assertEquals(new Date(1700000000000L), result.getDate(1));
assertEquals(new Timestamp(1700000000000L), result.getTimestamp(1));
}
}
}
Loading