From 4d6e99930b29c8191a701506997233e013375226 Mon Sep 17 00:00:00 2001 From: pernyf Date: Tue, 17 Nov 2026 16:15:09 +0100 Subject: [PATCH 1/2] Support SIGNED INTEGER and reject unrecognized CAST targets --- release.md | 8 + .../jparq/engine/NumericCastTarget.java | 30 ++- .../se/alipsa/jparq/engine/SqlParser.java | 6 +- .../engine/function/DateTimeFunctions.java | 42 +++- .../java/jparq/SignedUnsignedCastTest.java | 220 ++++++++++++++++++ 5 files changed, 299 insertions(+), 7 deletions(-) create mode 100644 src/test/java/jparq/SignedUnsignedCastTest.java diff --git a/release.md b/release.md index de5229c4..74937fad 100644 --- a/release.md +++ b/release.md @@ -10,6 +10,14 @@ combining an empty MIN(mpg) with a character literal is rejected with SQLState 42000. Fixes #320. +- **Behavior change:** Support `SIGNED INTEGER` and `SIGNED_INTEGER` as synonyms of `SIGNED` in CAST and + CONVERT, retaining the established 32-bit INTEGER mapping rather than MySQL's 64-bit semantics, and reject + `UNSIGNED`, `UNSIGNED INTEGER`, and other unrecognized CAST targets with SQLState 42000 instead of returning + the input unchanged (#322). For example, `CAST('123' AS SIGNED INTEGER)` returns Integer 123, + `CAST('3000000000' AS SIGNED INTEGER)` fails with 22003 like `SIGNED`, and `CAST('1' AS JSON)` or + `CAST('1' AS UNSIGNED)` fail with 42000. + Fixes #322. + - Derive common numeric CASE types from scalar subquery declarations, including correlated, empty, and NULL results, and the remaining supported numeric scalar functions (#319). Retain unbounded decimal types without executing unselected branches. Keep value classes and metadata consistent through grouping, derived tables, diff --git a/src/main/java/se/alipsa/jparq/engine/NumericCastTarget.java b/src/main/java/se/alipsa/jparq/engine/NumericCastTarget.java index aca8a93d..2be19035 100644 --- a/src/main/java/se/alipsa/jparq/engine/NumericCastTarget.java +++ b/src/main/java/se/alipsa/jparq/engine/NumericCastTarget.java @@ -5,6 +5,7 @@ import java.util.List; import java.util.Locale; import java.util.Map; +import java.util.Set; import se.alipsa.jparq.NumericType; /** @@ -16,8 +17,8 @@ public enum NumericCastTarget { TINYINT(new NumericType(Types.TINYINT, 3, 0), "tinyint"), /** Signed sixteen-bit integer. */ SMALLINT(new NumericType(Types.SMALLINT, 5, 0), "smallint", "int2"), - /** Signed thirty-two-bit integer. */ - INTEGER(NumericType.of(Types.INTEGER, 0, 0), "int", "integer", "signed", "int4"), + /** Signed thirty-two-bit integer, including the SIGNED INTEGER spelling. */ + INTEGER(NumericType.of(Types.INTEGER, 0, 0), "int", "integer", "signed", "signedinteger", "int4"), /** Signed sixty-four-bit integer. */ BIGINT(NumericType.of(Types.BIGINT, 0, 0), "bigint", "int8"), /** Single-precision floating point, including precision-qualified FLOAT. */ @@ -26,6 +27,7 @@ public enum NumericCastTarget { DOUBLE(NumericType.of(Types.DOUBLE, 0, 0), "double", "doubleprecision", "float8"); private static final Map BY_NAME; + private static final Set UNSUPPORTED_ALIASES = Set.of("unsigned", "unsignedinteger"); static { Map names = new HashMap<>(); @@ -61,12 +63,32 @@ public enum NumericCastTarget { * @return the numeric target, or null for an unrecognized name */ public static NumericCastTarget fromName(String name) { + return name == null ? null : fromNormalizedName(normalize(name)); + } + + /** + * Identifies the unsigned integer spellings that JParq does not support as cast + * targets, so cast-style CONVERT arguments resolve to a CAST that fails with + * SQLState 42000 instead of silently returning the operand. Underscores are + * retained so underscored column names keep their value role. + * + * @param name + * the SQL target name, possibly null + * @return whether the name spells an unsupported unsigned integer target + */ + public static boolean isUnsupportedAlias(String name) { if (name == null) { - return null; + return false; } int parameters = name.indexOf('('); String base = parameters < 0 ? name : name.substring(0, parameters); - return fromNormalizedName(base.toLowerCase(Locale.ROOT).trim().replace(" ", "").replace("_", "")); + return UNSUPPORTED_ALIASES.contains(base.toLowerCase(Locale.ROOT).trim().replace(" ", "")); + } + + private static String normalize(String name) { + int parameters = name.indexOf('('); + String base = parameters < 0 ? name : name.substring(0, parameters); + return base.toLowerCase(Locale.ROOT).trim().replace(" ", "").replace("_", ""); } /** diff --git a/src/main/java/se/alipsa/jparq/engine/SqlParser.java b/src/main/java/se/alipsa/jparq/engine/SqlParser.java index 3ebbaf2c..5f8224cb 100644 --- a/src/main/java/se/alipsa/jparq/engine/SqlParser.java +++ b/src/main/java/se/alipsa/jparq/engine/SqlParser.java @@ -62,8 +62,12 @@ static boolean isConvertTypeName(String name) { if (paren > 0) { normalized = normalized.substring(0, paren).trim(); } + // Spaces join multi-word spellings such as SIGNED INTEGER, while underscores + // keep names like big_int in their column role. + String compact = normalized.replace(" ", ""); return CONVERT_TYPE_NAMES.contains(normalized) || ScalarExpressionSchemas.declaredJdbcType(normalized) != null - || NumericCastTarget.fromNormalizedName(normalized) != null || DecimalCasts.isDecimalType(normalized); + || NumericCastTarget.fromNormalizedName(compact) != null || NumericCastTarget.isUnsupportedAlias(normalized) + || DecimalCasts.isDecimalType(normalized); } /** diff --git a/src/main/java/se/alipsa/jparq/engine/function/DateTimeFunctions.java b/src/main/java/se/alipsa/jparq/engine/function/DateTimeFunctions.java index fe73b486..af2ff0c0 100644 --- a/src/main/java/se/alipsa/jparq/engine/function/DateTimeFunctions.java +++ b/src/main/java/se/alipsa/jparq/engine/function/DateTimeFunctions.java @@ -781,7 +781,8 @@ private static Object applyInterval(Object temporal, TemporalInterval interval) * @return the cast result converted to the requested type when possible * @throws IllegalArgumentException * if decimal bounds or binary lengths or source types are invalid - * (SQLState 42000) or the rounded value exceeds the target's range or + * (SQLState 42000) or the target is not a recognized CAST data type + * (42000) or the rounded value exceeds the target's range or * precision (SQLState 22003), or numeric input is invalid or * non-finite (SQLState 22018), or temporal input has invalid format * (22007) or out-of-range fields (22008), or a non-character interval @@ -832,7 +833,44 @@ public static Object castLiteral(CastExpression cast, Object value) { case "text", "clob", "characterlargeobject", "nclob", "nationalcharacterlargeobject" -> SqlText.cast(value); case "binary", "varbinary", "binaryvarying", "blob", "binarylargeobject" -> compileBinaryCast(cast).convert(value); - default -> value; + default -> unsupportedCastTarget(normalized, dataType, value); + }; + } + + /** + * Retains temporal alias targets whose conversion the temporal promotion path + * applies, and rejects declarations that no converter recognizes. + * + * @param normalized + * normalized target name + * @param dataType + * declared type text for diagnostics + * @param value + * evaluated operand, possibly null + * @return value unchanged for temporal promotion targets + * @throws IllegalArgumentException + * carrying SQLState 42000 for an unrecognized target + */ + private static Object unsupportedCastTarget(String normalized, String dataType, Object value) { + if (isTemporalPromotionTarget(normalized)) { + return value; + } + throw SqlExceptions.syntaxFailure("Invalid data type for CAST: " + dataType); + } + + /** + * Identifies temporal CAST aliases converted by the temporal promotion path + * after literal conversion instead of by this converter. + * + * @param normalized + * normalized target name + * @return whether temporal promotion applies the conversion + */ + static boolean isTemporalPromotionTarget(String normalized) { + return switch (normalized) { + case "datetime", "timestampwithouttimezone", "timewithouttimezone", "timewithtimezone", "timestampwithtimezone" -> + true; + default -> false; }; } diff --git a/src/test/java/jparq/SignedUnsignedCastTest.java b/src/test/java/jparq/SignedUnsignedCastTest.java new file mode 100644 index 00000000..7cb2bf4a --- /dev/null +++ b/src/test/java/jparq/SignedUnsignedCastTest.java @@ -0,0 +1,220 @@ +package jparq; + +import static org.junit.jupiter.api.Assertions.*; + +import java.nio.file.Path; +import java.sql.DriverManager; +import java.sql.SQLException; +import java.sql.Types; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +/** + * Verifies that SIGNED INTEGER and SIGNED_INTEGER convert like SIGNED, while + * UNSIGNED, UNSIGNED INTEGER, and other unrecognized CAST targets fail with + * SQLState 42000 instead of returning the input unchanged (#322). + */ +class SignedUnsignedCastTest { + + /** + * The SIGNED, SIGNED INTEGER, and SIGNED_INTEGER spellings declare and convert + * a thirty-two-bit integer. + * + * @param spelling + * signed integer target spelling + * @throws Exception + * if query execution fails + */ + @ParameterizedTest + @ValueSource(strings = { + "SIGNED", "SIGNED INTEGER", "SIGNED_INTEGER" + }) + void signedSpellingsConvertToInteger(String spelling) throws Exception { + String sql = "SELECT CAST('123' AS " + spelling + ")"; + try (var connection = DriverManager.getConnection(url()); var statement = connection.createStatement()) { + try (var result = statement.executeQuery(sql)) { + var meta = result.getMetaData(); + assertEquals(Types.INTEGER, meta.getColumnType(1), sql); + assertEquals("java.lang.Integer", meta.getColumnClassName(1), sql); + assertEquals(10, meta.getPrecision(1), sql); + assertTrue(result.next(), sql); + assertEquals(123, result.getInt(1), sql); + assertFalse(result.next(), sql); + } + } + } + + /** + * Signed integer casts truncate the fractional part and keep the established + * thirty-two-bit range, so oversized values fail like SIGNED. + * + * @throws Exception + * if query execution fails + */ + @Test + void signedIntegerRetainsThirtyTwoBitRange() throws Exception { + try (var connection = DriverManager.getConnection(url()); var statement = connection.createStatement()) { + try (var result = statement.executeQuery("SELECT CAST(mpg AS SIGNED INTEGER) FROM mtcars LIMIT 1")) { + assertEquals(Types.INTEGER, result.getMetaData().getColumnType(1)); + assertTrue(result.next()); + assertEquals(21, result.getInt(1)); + assertFalse(result.next()); + } + try (var result = statement.executeQuery("SELECT CAST('10.4' AS SIGNED INTEGER)")) { + assertTrue(result.next()); + assertEquals(10, result.getInt(1)); + assertFalse(result.next()); + } + } + assertRejected("22003", "value out of range", "SELECT CAST('3000000000' AS SIGNED INTEGER)"); + assertRejected("22003", "value out of range", "SELECT CAST('3000000000' AS SIGNED)"); + } + + /** + * UNSIGNED and UNSIGNED INTEGER name no JParq target, so both literal and + * column operands fail instead of returning the input unchanged. + * + * @param spelling + * unsigned integer target spelling + * @throws Exception + * if connection creation fails + */ + @ParameterizedTest + @ValueSource(strings = { + "UNSIGNED", "UNSIGNED INTEGER" + }) + void unsignedSpellingsAreRejected(String spelling) throws Exception { + assertRejected("42000", "Invalid data type for CAST", "SELECT CAST('123' AS " + spelling + ")"); + assertRejected("42000", "Invalid data type for CAST", "SELECT CAST(cyl AS " + spelling + ") FROM mtcars"); + } + + /** + * Other unrecognized targets fail with the same classification. + * + * @param target + * unrecognized CAST target + * @throws Exception + * if connection creation fails + */ + @ParameterizedTest + @ValueSource(strings = { + "FOOBAR", "JSON", "INTERVAL DAY" + }) + void unrecognizedTargetsAreRejected(String target) throws Exception { + assertRejected("42000", "Invalid data type for CAST", "SELECT CAST('1' AS " + target + ")"); + } + + /** + * A NULL operand retains the signed declaration without failing. + * + * @throws Exception + * if query execution fails + */ + @Test + void nullInputKeepsSignedDeclaration() throws Exception { + try (var connection = DriverManager.getConnection(url()); var statement = connection.createStatement()) { + try (var result = statement.executeQuery("SELECT CAST(NULL AS SIGNED INTEGER)")) { + var meta = result.getMetaData(); + assertEquals(Types.INTEGER, meta.getColumnType(1)); + assertEquals("java.lang.Integer", meta.getColumnClassName(1)); + assertTrue(result.next()); + assertNull(result.getObject(1)); + assertFalse(result.next()); + } + } + } + + /** + * Unsigned targets inside a scalar subquery fail with the same error. + * + * @throws Exception + * if connection creation fails + */ + @Test + void scalarSubqueryUnsignedRejected() throws Exception { + assertRejected("42000", "Invalid data type for CAST", "SELECT (SELECT CAST('1' AS UNSIGNED))"); + } + + /** + * Cast-style CONVERT shares the signed declaration and the unsigned rejection + * for both argument orders. + * + * @throws Exception + * if query execution fails + */ + @Test + void convertSharesSignedAndUnsignedDeclarations() throws Exception { + try (var connection = DriverManager.getConnection(url()); var statement = connection.createStatement()) { + try (var result = statement.executeQuery("SELECT CONVERT(SIGNED INTEGER, '123')")) { + var meta = result.getMetaData(); + assertEquals(Types.INTEGER, meta.getColumnType(1)); + assertEquals("java.lang.Integer", meta.getColumnClassName(1)); + assertTrue(result.next()); + assertEquals(123, result.getInt(1)); + assertFalse(result.next()); + } + } + assertRejected("42000", "Invalid data type for CAST", "SELECT CONVERT(UNSIGNED, '123')"); + assertRejected("42000", "Invalid data type for CAST", "SELECT CONVERT(model, UNSIGNED) FROM mtcars"); + } + + /** + * Set operations see the declared INTEGER branch, so an empty signed branch + * rejects a character peer while a numeric peer still merges. + * + * @throws Exception + * if query execution fails + */ + @Test + void setOperationSeesDeclaredSignedIntegerBranch() throws Exception { + assertRejected("42000", "compatible column types", + "SELECT CAST(cyl AS SIGNED INTEGER) FROM mtcars WHERE 1=0 UNION ALL SELECT 'a'"); + try (var connection = DriverManager.getConnection(url()); var statement = connection.createStatement()) { + try (var result = statement + .executeQuery("SELECT CAST(cyl AS SIGNED INTEGER) FROM mtcars LIMIT 1 UNION ALL SELECT 7")) { + assertEquals(Types.INTEGER, result.getMetaData().getColumnType(1)); + assertTrue(result.next()); + assertEquals(6, result.getInt(1)); + assertTrue(result.next()); + assertEquals(7, result.getInt(1)); + assertFalse(result.next()); + } + } + } + + /** + * Executes a query and asserts the SQLState and message of its failure. + * + * @param sqlState + * expected SQLState + * @param expectedMessage + * expected message fragment + * @param sql + * query expected to fail + * @throws Exception + * if connection creation fails + */ + private static void assertRejected(String sqlState, String expectedMessage, String sql) throws Exception { + try (var connection = DriverManager.getConnection(url())) { + SQLException error = assertThrows(SQLException.class, () -> { + try (var statement = connection.createStatement(); var result = statement.executeQuery(sql)) { + while (result.next()) { + result.getObject(1); + } + } + }, sql); + assertEquals(sqlState, error.getSQLState(), sql); + assertTrue(error.getMessage().contains(expectedMessage), error.getMessage()); + } + } + + /** + * Returns the fixture database URL. + * + * @return JDBC connection URL + */ + private static String url() { + return "jdbc:jparq:" + Path.of("src/test/resources").toAbsolutePath(); + } +} From 41b54a019c0400cd360a30bea6578369c3b5189a Mon Sep 17 00:00:00 2001 From: pernyf Date: Tue, 17 Nov 2026 17:32:16 +0100 Subject: [PATCH 2/2] Reuse DateTimeFunctions type name normalization in NumericCastTarget --- .../java/se/alipsa/jparq/engine/NumericCastTarget.java | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/main/java/se/alipsa/jparq/engine/NumericCastTarget.java b/src/main/java/se/alipsa/jparq/engine/NumericCastTarget.java index 2be19035..56e84c1c 100644 --- a/src/main/java/se/alipsa/jparq/engine/NumericCastTarget.java +++ b/src/main/java/se/alipsa/jparq/engine/NumericCastTarget.java @@ -7,6 +7,7 @@ import java.util.Map; import java.util.Set; import se.alipsa.jparq.NumericType; +import se.alipsa.jparq.engine.function.DateTimeFunctions; /** * Shares numeric CAST aliases and their JDBC types across parsing and @@ -63,7 +64,7 @@ public enum NumericCastTarget { * @return the numeric target, or null for an unrecognized name */ public static NumericCastTarget fromName(String name) { - return name == null ? null : fromNormalizedName(normalize(name)); + return name == null ? null : fromNormalizedName(DateTimeFunctions.normalizeTypeName(name)); } /** @@ -85,12 +86,6 @@ public static boolean isUnsupportedAlias(String name) { return UNSUPPORTED_ALIASES.contains(base.toLowerCase(Locale.ROOT).trim().replace(" ", "")); } - private static String normalize(String name) { - int parameters = name.indexOf('('); - String base = parameters < 0 ? name : name.substring(0, parameters); - return base.toLowerCase(Locale.ROOT).trim().replace(" ", "").replace("_", ""); - } - /** * Looks up an exact alias without treating column names as normalized types. *