diff --git a/release.md b/release.md index 38ae54b3..de5229c4 100644 --- a/release.md +++ b/release.md @@ -2,6 +2,14 @@ ### 1.4.0 +- **Behavior change:** MIN and MAX now report the argument's declared type, class, and decimal bounds + regardless of row count instead of OTHER or value-inferred bounds, so set operations consistently + reject incompatible branches and merge row-independent bounds (#320). For example, MIN(mpg) over a + filtered-to-empty input reports DOUBLE/Double, MIN(CAST(mpg AS DECIMAL(10,2))) reports the declared + decimal(10,2) bounds with and without rows, MIN(cyl / 3) over empty input reports DECIMAL, and + combining an empty MIN(mpg) with a character literal is rejected with SQLState 42000. + Fixes #320. + - 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/AggregateResultSetMetaData.java b/src/main/java/se/alipsa/jparq/AggregateResultSetMetaData.java index b8c4f209..7849319e 100644 --- a/src/main/java/se/alipsa/jparq/AggregateResultSetMetaData.java +++ b/src/main/java/se/alipsa/jparq/AggregateResultSetMetaData.java @@ -137,6 +137,9 @@ void retainValueSchemas(List schemas) { /** * Applies declared output types and numeric bounds, including for empty groups. + * Unbounded decimal MIN/MAX arguments use their numeric declaration, since + * unbounded decimals carry no value-schema bounds; bounded declarations take + * precedence over bounds inferred from returned rows. * * @param projected * metadata resolved from the source schema and expressions @@ -146,6 +149,9 @@ void retainExpressionTypes(JParqResultSetMetaData projected) { for (int i = 0; i < labels.size(); i++) { int column = i + 1; NumericType numeric = projected.numericType(column); + if (numeric == null) { + numeric = projected.unboundedExtremumArgumentType(column); + } if (numeric != null) { sqlTypes.set(i, numeric.jdbcType()); if (numeric.jdbcType() != Types.DECIMAL || numeric.precision() > 0) { @@ -156,6 +162,10 @@ void retainExpressionTypes(JParqResultSetMetaData projected) { Schema declaration = projected.columnValueSchema(column); if (numeric == null && declaration != null) { sqlTypes.set(i, JdbcTypeMapper.mapSchemaToJdbcType(declaration)); + if (declaration.getLogicalType() instanceof Decimal decimal) { + precisions[i] = decimal.getPrecision(); + scales[i] = decimal.getScale(); + } } schemas.add(numeric != null ? numeric.schema() : declaration != null ? declaration : columnValueSchema(column)); } diff --git a/src/main/java/se/alipsa/jparq/JParqResultSetMetaData.java b/src/main/java/se/alipsa/jparq/JParqResultSetMetaData.java index e4cf6f05..062e1558 100644 --- a/src/main/java/se/alipsa/jparq/JParqResultSetMetaData.java +++ b/src/main/java/se/alipsa/jparq/JParqResultSetMetaData.java @@ -499,8 +499,9 @@ public Schema columnValueSchema(int column) { /** * Resolves literal and CAST declarations independently of returned rows, and - * scalar subqueries against the subquery's own source schema. Non-numeric - * window MIN/MAX retain their operand declarations. + * scalar subqueries against the subquery's own source schema. Window and + * aggregate MIN/MAX retain their operand declarations, so an empty input does + * not erase the argument's declared type (#320). * * @param column * one-based output column @@ -523,6 +524,18 @@ private Schema declaredProjectionSchema(int column) { return null; } } + Expression argument = extremumArgument(expression); + if (argument != null) { + try { + return ArrayExpressionSchemas.resolve(argument, reference -> { + Schema.Field source = resolveSourceField(reference); + return source == null ? null : source.schema(); + }, scalarResultSchemas); + } catch (IllegalArgumentException incompatible) { + // Preserve value inference for mixed DATE/TIMESTAMP operands (#431). + return null; + } + } Schema scalar = ScalarExpressionSchemas.resolve(expressionAtColumn(column), name -> resolveSourceField(new Column(name)) != null); if (scalar != null || !(expression instanceof CaseExpression) && !ExpressionBranches.isCoalesce(expression)) { @@ -544,6 +557,22 @@ private Schema declaredProjectionSchema(int column) { } } + /** + * Identifies a top-level MIN or MAX with exactly one argument. + * + * @param expression + * unwrapped projection expression + * @return the aggregate's argument, or null for other expressions + */ + private static Expression extremumArgument(Expression expression) { + if (expression instanceof net.sf.jsqlparser.expression.Function function + && ("MIN".equalsIgnoreCase(function.getName()) || "MAX".equalsIgnoreCase(function.getName())) + && function.getParameters() != null && function.getParameters().size() == 1) { + return function.getParameters().getFirst(); + } + return null; + } + /** * Resolves a declared CAST or cast-style CONVERT using bound source columns. * @@ -757,6 +786,27 @@ NumericType numericExpressionType(Expression expression) { }, scalarResultSchemas, scalarNumericTypes); } + /** + * Resolves the unbounded decimal argument of a top-level MIN or MAX, which the + * value-schema channel cannot express because unbounded decimals carry no Avro + * bounds (#320). + * + * @param column + * one-based output column + * @return the argument's unbounded decimal type, or null otherwise + */ + NumericType unboundedExtremumArgumentType(int column) { + Expression argument = extremumArgument(unwrap(expressionAtColumn(column))); + if (argument == null) { + return null; + } + NumericType numeric = numericExpressionType(argument); + if (numeric == null || numeric.jdbcType() != Types.DECIMAL || numeric.precision() != 0) { + return null; + } + return numeric; + } + private String canonicalName(int column) { return ColumnNameLookup.canonicalName(canonicalNames, physicalNames, labels, column); } diff --git a/src/test/java/jparq/MinMaxEmptyInputTest.java b/src/test/java/jparq/MinMaxEmptyInputTest.java new file mode 100644 index 00000000..963fea5b --- /dev/null +++ b/src/test/java/jparq/MinMaxEmptyInputTest.java @@ -0,0 +1,337 @@ +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; + +/** + * Verifies that MIN and MAX retain their argument's declared type and decimal + * bounds regardless of row count, so set operations reject incompatible + * branches and merge stable bounds (#320). + */ +class MinMaxEmptyInputTest { + + private static String url() { + return "jdbc:jparq:" + Path.of("src/test/resources").toAbsolutePath(); + } + + /** + * An empty MIN reports the argument's declared numeric type and class. + * + * @throws Exception + * if query execution fails + */ + @Test + void minOverEmptyInputReportsArgumentType() throws Exception { + try (var connection = DriverManager.getConnection(url()); var statement = connection.createStatement()) { + try (var result = statement.executeQuery("SELECT MIN(mpg) FROM mtcars WHERE cyl = 99")) { + var meta = result.getMetaData(); + assertEquals(Types.DOUBLE, meta.getColumnType(1)); + assertEquals("java.lang.Double", meta.getColumnClassName(1)); + assertTrue(result.next()); + assertNull(result.getObject(1)); + assertFalse(result.next()); + } + } + } + + /** + * An empty MAX reports the argument's declared character type and class. + * + * @throws Exception + * if query execution fails + */ + @Test + void maxOverEmptyInputReportsArgumentType() throws Exception { + try (var connection = DriverManager.getConnection(url()); var statement = connection.createStatement()) { + try (var result = statement.executeQuery("SELECT MAX(model) FROM mtcars WHERE cyl = 99")) { + var meta = result.getMetaData(); + assertEquals(Types.VARCHAR, meta.getColumnType(1)); + assertEquals("java.lang.String", meta.getColumnClassName(1)); + assertTrue(result.next()); + assertNull(result.getObject(1)); + assertFalse(result.next()); + } + } + } + + /** + * An empty MIN over an integer argument reports INTEGER. + * + * @throws Exception + * if query execution fails + */ + @Test + void minOverEmptyInputReportsIntegerArgumentType() throws Exception { + try (var connection = DriverManager.getConnection(url()); var statement = connection.createStatement()) { + try (var result = statement.executeQuery("SELECT MIN(cyl) FROM mtcars WHERE cyl = 99")) { + var meta = result.getMetaData(); + assertEquals(Types.INTEGER, meta.getColumnType(1)); + assertEquals("java.lang.Integer", meta.getColumnClassName(1)); + } + } + } + + /** + * Non-empty MIN and MAX keep reporting the observed argument values. + * + * @throws Exception + * if query execution fails + */ + @Test + void minMaxOverNonEmptyInputKeepObservedTypes() throws Exception { + try (var connection = DriverManager.getConnection(url()); var statement = connection.createStatement()) { + try (var result = statement.executeQuery("SELECT MIN(mpg) FROM mtcars")) { + assertEquals(Types.DOUBLE, result.getMetaData().getColumnType(1)); + assertTrue(result.next()); + assertEquals(10.4, result.getDouble(1), 0.0); + } + try (var result = statement.executeQuery("SELECT MAX(model) FROM mtcars")) { + assertEquals(Types.VARCHAR, result.getMetaData().getColumnType(1)); + assertTrue(result.next()); + assertNotNull(result.getString(1)); + } + } + } + + /** + * A group whose values are all NULL reports the argument's declared type, which + * VALUES typing derives from the non-NULL literals. + * + * @throws Exception + * if query execution fails + */ + @Test + void groupedAllNullInputReportsArgumentType() throws Exception { + try (var connection = DriverManager.getConnection(url()); var statement = connection.createStatement()) { + try (var result = statement + .executeQuery("SELECT g, MIN(v) FROM (VALUES (1, 1.5), (2, NULL)) t(g, v) WHERE v IS NULL GROUP BY g")) { + var meta = result.getMetaData(); + assertEquals(Types.DECIMAL, meta.getColumnType(2)); + assertEquals(2, meta.getPrecision(2)); + assertEquals(1, meta.getScale(2)); + assertTrue(result.next()); + assertEquals(2, result.getInt(1)); + assertNull(result.getObject(2)); + assertFalse(result.next()); + } + } + } + + /** + * An empty DECIMAL MIN retains the argument's declared decimal bounds instead + * of collapsing to OTHER. + * + * @throws Exception + * if query execution fails + */ + @Test + void emptyDecimalMinimumRetainsDeclaredBounds() throws Exception { + try (var connection = DriverManager.getConnection(url()); var statement = connection.createStatement()) { + try (var result = statement.executeQuery("SELECT MIN(CAST(mpg AS DECIMAL(10,2))) FROM mtcars WHERE cyl = 99")) { + var meta = result.getMetaData(); + assertEquals(Types.DECIMAL, meta.getColumnType(1)); + assertEquals(10, meta.getPrecision(1)); + assertEquals(2, meta.getScale(1)); + assertTrue(result.next()); + assertNull(result.getObject(1)); + } + } + } + + /** + * A DECIMAL MIN reports the argument's declared decimal bounds with and without + * rows, so the bounds do not depend on the returned values. + * + * @throws Exception + * if query execution fails + */ + @Test + void decimalMinimumBoundsDoNotDependOnRows() throws Exception { + String sql = "SELECT MIN(CAST(mpg AS DECIMAL(10,2))) FROM mtcars"; + try (var connection = DriverManager.getConnection(url()); var statement = connection.createStatement()) { + try (var result = statement.executeQuery(sql)) { + var meta = result.getMetaData(); + assertEquals(Types.DECIMAL, meta.getColumnType(1)); + assertEquals(10, meta.getPrecision(1)); + assertEquals(2, meta.getScale(1)); + assertTrue(result.next()); + assertEquals(new java.math.BigDecimal("10.40"), result.getBigDecimal(1)); + } + try (var result = statement.executeQuery(sql + " WHERE cyl = 99")) { + var meta = result.getMetaData(); + assertEquals(Types.DECIMAL, meta.getColumnType(1)); + assertEquals(10, meta.getPrecision(1)); + assertEquals(2, meta.getScale(1)); + assertTrue(result.next()); + assertNull(result.getObject(1)); + } + } + } + + /** + * A set operation over a DECIMAL MIN merges the declared decimal bounds whether + * or not the MIN branch returns rows. + * + * @throws Exception + * if query execution fails + */ + @Test + void setOperationBoundsDoNotDependOnRows() throws Exception { + String suffix = " UNION ALL SELECT CAST(1 AS DECIMAL(3,0))"; + String emptyBranch = "SELECT MIN(CAST(mpg AS DECIMAL(10,2))) FROM mtcars WHERE cyl = 99" + suffix; + String filledBranch = "SELECT MIN(CAST(mpg AS DECIMAL(10,2))) FROM mtcars" + suffix; + int[] declaredBounds = { + Types.DECIMAL, 10, 2 + }; + try (var connection = DriverManager.getConnection(url())) { + assertArrayEquals(declaredBounds, setBounds(connection, emptyBranch), "merged bounds must not depend on rows"); + assertArrayEquals(declaredBounds, setBounds(connection, filledBranch), "merged bounds must not depend on rows"); + } + } + + /** + * An empty MIN over an unbounded decimal argument reports DECIMAL instead of + * OTHER, matching the argument's plain empty projection. + * + * @throws Exception + * if query execution fails + */ + @Test + void emptyUnboundedDecimalMinimumReportsDecimal() throws Exception { + try (var connection = DriverManager.getConnection(url()); var statement = connection.createStatement()) { + try (var result = statement.executeQuery("SELECT MIN(cyl / 3) FROM mtcars WHERE cyl = 99")) { + var meta = result.getMetaData(); + assertEquals(Types.DECIMAL, meta.getColumnType(1)); + assertEquals(0, meta.getPrecision(1)); + assertEquals(0, meta.getScale(1)); + assertTrue(result.next()); + assertNull(result.getObject(1)); + } + try (var result = statement.executeQuery("SELECT MIN(ROUND(mpg, 1)) FROM mtcars WHERE cyl = 99")) { + assertEquals(Types.DECIMAL, result.getMetaData().getColumnType(1)); + assertTrue(result.next()); + assertNull(result.getObject(1)); + } + try (var result = statement.executeQuery("SELECT MIN(cyl / 3) FROM mtcars")) { + assertEquals(Types.DECIMAL, result.getMetaData().getColumnType(1)); + assertTrue(result.next()); + assertNotNull(result.getBigDecimal(1)); + } + } + } + + /** + * An empty MAX over an unbounded decimal argument reports DECIMAL instead of + * OTHER, matching the argument's plain empty projection. + * + * @throws Exception + * if query execution fails + */ + @Test + void emptyUnboundedDecimalMaximumReportsDecimal() throws Exception { + try (var connection = DriverManager.getConnection(url()); var statement = connection.createStatement()) { + try (var result = statement.executeQuery("SELECT MAX(cyl / 3) FROM mtcars WHERE cyl = 99")) { + var meta = result.getMetaData(); + assertEquals(Types.DECIMAL, meta.getColumnType(1)); + assertEquals(0, meta.getPrecision(1)); + assertEquals(0, meta.getScale(1)); + assertTrue(result.next()); + assertNull(result.getObject(1)); + } + try (var result = statement.executeQuery("SELECT MAX(ROUND(mpg, 1)) FROM mtcars WHERE cyl = 99")) { + assertEquals(Types.DECIMAL, result.getMetaData().getColumnType(1)); + assertTrue(result.next()); + assertNull(result.getObject(1)); + } + try (var result = statement.executeQuery("SELECT MAX(cyl / 3) FROM mtcars")) { + assertEquals(Types.DECIMAL, result.getMetaData().getColumnType(1)); + assertTrue(result.next()); + assertNotNull(result.getBigDecimal(1)); + } + } + } + + /** + * Reads a set operation's first-column JDBC type, precision, and scale. + * + * @param connection + * open connection + * @param sql + * set operation to execute + * @return the column type, precision, and scale + * @throws Exception + * if query execution fails + */ + private static int[] setBounds(java.sql.Connection connection, String sql) throws Exception { + try (var statement = connection.createStatement(); var result = statement.executeQuery(sql)) { + while (result.next()) { + result.getObject(1); + } + var meta = result.getMetaData(); + return new int[]{ + meta.getColumnType(1), meta.getPrecision(1), meta.getScale(1) + }; + } + } + + /** + * A set operation rejects a character branch against an empty numeric MIN. + * + * @throws Exception + * if connection creation fails + */ + @Test + void setOperationRejectsCharacterBranchForEmptyNumericMinimum() throws Exception { + assertSetOperationRejected("SELECT MIN(mpg) FROM mtcars WHERE cyl = 99 UNION ALL SELECT 'a'"); + } + + /** + * A set operation rejects a numeric branch against an empty character MAX. + * + * @throws Exception + * if connection creation fails + */ + @Test + void setOperationRejectsNumericBranchForEmptyCharacterMaximum() throws Exception { + assertSetOperationRejected("SELECT MAX(model) FROM mtcars WHERE cyl = 99 UNION ALL SELECT 1"); + } + + /** + * A set operation merges an empty MIN with a compatible numeric branch. + * + * @throws Exception + * if query execution fails + */ + @Test + void setOperationMergesEmptyMinimumWithCompatibleBranch() throws Exception { + String sql = "SELECT MIN(mpg) FROM mtcars WHERE cyl = 99 UNION ALL SELECT 2.5"; + try (var connection = DriverManager.getConnection(url()); var statement = connection.createStatement()) { + try (var result = statement.executeQuery(sql)) { + assertEquals(Types.DOUBLE, result.getMetaData().getColumnType(1)); + assertTrue(result.next()); + assertNull(result.getObject(1)); + assertTrue(result.next()); + assertEquals(2.5, result.getDouble(1), 0.0); + assertFalse(result.next()); + } + } + } + + private static void assertSetOperationRejected(String sql) throws Exception { + try (var connection = DriverManager.getConnection(url())) { + SQLException error = assertThrows(SQLException.class, () -> { + try (var statement = connection.prepareStatement(sql)) { + statement.executeQuery().close(); + } + }, sql); + assertEquals("42000", error.getSQLState(), sql); + assertTrue(error.getMessage().contains("Set operation components must use compatible column types"), + error.getMessage()); + } + } +}