From e3c7eef8042b0c48447a7934dcea397c55ff68b6 Mon Sep 17 00:00:00 2001 From: pernyf Date: Wed, 18 Nov 2026 16:02:12 +0100 Subject: [PATCH 1/2] Classify VARCHAR failures in the temporal ResultSet getters getDate and getTimestamp converted String values with java.sql Date/Time Timestamp.valueOf, which leaked unchecked IllegalArgumentException without a SQLState and accepted values such as 2024-02-30 or 25:00:00 that roll over silently. Route the conversion through DateTimeFunctions.toDate/toTimestamp, the same strict helpers getTime already uses, and wrap failures through SqlExceptions.wrap so they surface as SQLDataException with 22007 for an invalid format or 22008 for out-of-range fields. --- .../java/se/alipsa/jparq/JParqResultSet.java | 68 ++++++++----- .../TemporalGetStringConversionTest.java | 99 +++++++++++++++++++ 2 files changed, 143 insertions(+), 24 deletions(-) create mode 100644 src/test/java/jparq/TemporalGetStringConversionTest.java diff --git a/src/main/java/se/alipsa/jparq/JParqResultSet.java b/src/main/java/se/alipsa/jparq/JParqResultSet.java index acfccbc1..44062c2f 100644 --- a/src/main/java/se/alipsa/jparq/JParqResultSet.java +++ b/src/main/java/se/alipsa/jparq/JParqResultSet.java @@ -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 @@ -1777,20 +1782,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 diff --git a/src/test/java/jparq/TemporalGetStringConversionTest.java b/src/test/java/jparq/TemporalGetStringConversionTest.java new file mode 100644 index 00000000..ecb7baa6 --- /dev/null +++ b/src/test/java/jparq/TemporalGetStringConversionTest.java @@ -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 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)); + } + } +} From 131ba401b9a71d9df3b715199d3ee44e67cc34c3 Mon Sep 17 00:00:00 2001 From: pernyf Date: Wed, 18 Nov 2026 16:50:03 +0100 Subject: [PATCH 2/2] Document the temporal SQLStates in the getTime getter javadoc --- src/main/java/se/alipsa/jparq/JParqResultSet.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/se/alipsa/jparq/JParqResultSet.java b/src/main/java/se/alipsa/jparq/JParqResultSet.java index 44062c2f..141fa08a 100644 --- a/src/main/java/se/alipsa/jparq/JParqResultSet.java +++ b/src/main/java/se/alipsa/jparq/JParqResultSet.java @@ -1752,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 {