From 14b8150eada0bb629bfc801304da1e0f44efd6a4 Mon Sep 17 00:00:00 2001 From: Per Nyfelt Date: Mon, 16 Nov 2026 22:38:16 +0100 Subject: [PATCH 1/3] Retain declared MIN/MAX argument type for empty input An empty or all-NULL MIN/MAX reported OTHER/Object because the result type was inferred from the observed value class and the declared-type fallback had no branch for top-level aggregate MIN/MAX. declaredProjectionSchema now resolves the single-argument MIN/MAX argument like the existing window branch, and retainExpressionTypes retains the declared decimal bounds when no numeric type is inferred, so set operations consistently reject incompatible branches. Fixes #320. --- release.md | 6 + .../jparq/AggregateResultSetMetaData.java | 4 + .../alipsa/jparq/JParqResultSetMetaData.java | 18 +- src/test/java/jparq/MinMaxEmptyInputTest.java | 201 ++++++++++++++++++ 4 files changed, 227 insertions(+), 2 deletions(-) create mode 100644 src/test/java/jparq/MinMaxEmptyInputTest.java diff --git a/release.md b/release.md index 38ae54b3..e024648a 100644 --- a/release.md +++ b/release.md @@ -2,6 +2,12 @@ ### 1.4.0 +- **Behavior change:** MIN and MAX now report the argument's declared type, class, and decimal bounds + for empty and all-NULL input instead of OTHER, so set operations consistently reject incompatible + branches (#320). For example, MIN(mpg) over a filtered-to-empty input reports DOUBLE/Double, and + combining that result 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..fe2a4d54 100644 --- a/src/main/java/se/alipsa/jparq/AggregateResultSetMetaData.java +++ b/src/main/java/se/alipsa/jparq/AggregateResultSetMetaData.java @@ -156,6 +156,10 @@ void retainExpressionTypes(JParqResultSetMetaData projected) { Schema declaration = projected.columnValueSchema(column); if (numeric == null && declaration != null) { sqlTypes.set(i, JdbcTypeMapper.mapSchemaToJdbcType(declaration)); + if (precisions[i] == 0 && 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..84020539 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,19 @@ private Schema declaredProjectionSchema(int column) { return null; } } + if (expression instanceof net.sf.jsqlparser.expression.Function function + && ("MIN".equalsIgnoreCase(function.getName()) || "MAX".equalsIgnoreCase(function.getName())) + && function.getParameters() != null && function.getParameters().size() == 1) { + try { + return ArrayExpressionSchemas.resolve(function.getParameters().getFirst(), 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)) { diff --git a/src/test/java/jparq/MinMaxEmptyInputTest.java b/src/test/java/jparq/MinMaxEmptyInputTest.java new file mode 100644 index 00000000..98feee7b --- /dev/null +++ b/src/test/java/jparq/MinMaxEmptyInputTest.java @@ -0,0 +1,201 @@ +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 for empty and + * all-NULL input, so set operations reject incompatible branches (#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(v) FROM (VALUES (CAST(1.5 AS DECIMAL(10,2)))) t(v) WHERE 1 = 0")) { + var meta = result.getMetaData(); + assertEquals(Types.DECIMAL, meta.getColumnType(1)); + assertEquals(3, meta.getPrecision(1)); + assertEquals(2, meta.getScale(1)); + assertTrue(result.next()); + assertNull(result.getObject(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()); + } + } +} From f7df1361a8d20703c800a50d987b8ff2e7270aef Mon Sep 17 00:00:00 2001 From: Per Nyfelt Date: Mon, 16 Nov 2026 23:25:40 +0100 Subject: [PATCH 2/3] Make MIN/MAX decimal bounds row-independent and cover unbounded arguments Address review round 1 on PR #570: - retainExpressionTypes applies a resolved decimal declaration's bounds unconditionally, so declared bounds no longer depend on whether rows came back and set operations merge row-independent bounds. - unboundedExtremumArgumentType exposes the NumericType of an unbounded decimal MIN/MAX argument, which the value-schema channel cannot express because NumericType.schema() is null for precision 0; empty MIN(cyl / 3) and MIN(ROUND(mpg, 1)) now report DECIMAL p=0 s=0, matching the plain empty projection, while value-derived bounds are preserved when rows exist. - emptyDecimalMinimumRetainsDeclaredBounds no longer depends on VALUES literal typing (issue #559) and uses a real source column cast; add row-independence tests for decimal bounds and set-operation merges. Scalar subquery arguments with nonnumeric results stay untyped over empty input; tracked as #572. --- release.md | 8 +- .../jparq/AggregateResultSetMetaData.java | 8 +- .../alipsa/jparq/JParqResultSetMetaData.java | 44 ++++++- src/test/java/jparq/MinMaxEmptyInputTest.java | 114 +++++++++++++++++- 4 files changed, 161 insertions(+), 13 deletions(-) diff --git a/release.md b/release.md index e024648a..de5229c4 100644 --- a/release.md +++ b/release.md @@ -3,9 +3,11 @@ ### 1.4.0 - **Behavior change:** MIN and MAX now report the argument's declared type, class, and decimal bounds - for empty and all-NULL input instead of OTHER, so set operations consistently reject incompatible - branches (#320). For example, MIN(mpg) over a filtered-to-empty input reports DOUBLE/Double, and - combining that result with a character literal is rejected with SQLState 42000. + 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 diff --git a/src/main/java/se/alipsa/jparq/AggregateResultSetMetaData.java b/src/main/java/se/alipsa/jparq/AggregateResultSetMetaData.java index fe2a4d54..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,7 +162,7 @@ void retainExpressionTypes(JParqResultSetMetaData projected) { Schema declaration = projected.columnValueSchema(column); if (numeric == null && declaration != null) { sqlTypes.set(i, JdbcTypeMapper.mapSchemaToJdbcType(declaration)); - if (precisions[i] == 0 && declaration.getLogicalType() instanceof Decimal decimal) { + if (declaration.getLogicalType() instanceof Decimal decimal) { precisions[i] = decimal.getPrecision(); scales[i] = decimal.getScale(); } diff --git a/src/main/java/se/alipsa/jparq/JParqResultSetMetaData.java b/src/main/java/se/alipsa/jparq/JParqResultSetMetaData.java index 84020539..062e1558 100644 --- a/src/main/java/se/alipsa/jparq/JParqResultSetMetaData.java +++ b/src/main/java/se/alipsa/jparq/JParqResultSetMetaData.java @@ -524,11 +524,10 @@ private Schema declaredProjectionSchema(int column) { return null; } } - if (expression instanceof net.sf.jsqlparser.expression.Function function - && ("MIN".equalsIgnoreCase(function.getName()) || "MAX".equalsIgnoreCase(function.getName())) - && function.getParameters() != null && function.getParameters().size() == 1) { + Expression argument = extremumArgument(expression); + if (argument != null) { try { - return ArrayExpressionSchemas.resolve(function.getParameters().getFirst(), reference -> { + return ArrayExpressionSchemas.resolve(argument, reference -> { Schema.Field source = resolveSourceField(reference); return source == null ? null : source.schema(); }, scalarResultSchemas); @@ -558,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. * @@ -771,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 index 98feee7b..936204c7 100644 --- a/src/test/java/jparq/MinMaxEmptyInputTest.java +++ b/src/test/java/jparq/MinMaxEmptyInputTest.java @@ -9,8 +9,9 @@ import org.junit.jupiter.api.Test; /** - * Verifies that MIN and MAX retain their argument's declared type for empty and - * all-NULL input, so set operations reject incompatible branches (#320). + * 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 { @@ -131,11 +132,10 @@ void groupedAllNullInputReportsArgumentType() throws Exception { @Test void emptyDecimalMinimumRetainsDeclaredBounds() throws Exception { try (var connection = DriverManager.getConnection(url()); var statement = connection.createStatement()) { - try (var result = statement - .executeQuery("SELECT MIN(v) FROM (VALUES (CAST(1.5 AS DECIMAL(10,2)))) t(v) WHERE 1 = 0")) { + 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(3, meta.getPrecision(1)); + assertEquals(10, meta.getPrecision(1)); assertEquals(2, meta.getScale(1)); assertTrue(result.next()); assertNull(result.getObject(1)); @@ -143,6 +143,110 @@ void emptyDecimalMinimumRetainsDeclaredBounds() throws Exception { } } + /** + * 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 same 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; + try (var connection = DriverManager.getConnection(url())) { + int[] emptyBounds = setBounds(connection, emptyBranch); + int[] filledBounds = setBounds(connection, filledBranch); + assertArrayEquals(filledBounds, emptyBounds, "merged bounds must not depend on rows"); + assertEquals(Types.DECIMAL, emptyBounds[0]); + } + } + + /** + * 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)); + } + } + } + + /** + * 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. * From 55df396c830ec56e855bb0ba767b194183078e34 Mon Sep 17 00:00:00 2001 From: pernyf Date: Tue, 17 Nov 2026 13:14:22 +0100 Subject: [PATCH 3/3] Pin set operation decimal bounds and cover unbounded decimal MAX --- src/test/java/jparq/MinMaxEmptyInputTest.java | 44 ++++++++++++++++--- 1 file changed, 38 insertions(+), 6 deletions(-) diff --git a/src/test/java/jparq/MinMaxEmptyInputTest.java b/src/test/java/jparq/MinMaxEmptyInputTest.java index 936204c7..963fea5b 100644 --- a/src/test/java/jparq/MinMaxEmptyInputTest.java +++ b/src/test/java/jparq/MinMaxEmptyInputTest.java @@ -174,8 +174,8 @@ void decimalMinimumBoundsDoNotDependOnRows() throws Exception { } /** - * A set operation over a DECIMAL MIN merges the same bounds whether or not the - * MIN branch returns rows. + * 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 @@ -185,11 +185,12 @@ 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())) { - int[] emptyBounds = setBounds(connection, emptyBranch); - int[] filledBounds = setBounds(connection, filledBranch); - assertArrayEquals(filledBounds, emptyBounds, "merged bounds must not depend on rows"); - assertEquals(Types.DECIMAL, emptyBounds[0]); + assertArrayEquals(declaredBounds, setBounds(connection, emptyBranch), "merged bounds must not depend on rows"); + assertArrayEquals(declaredBounds, setBounds(connection, filledBranch), "merged bounds must not depend on rows"); } } @@ -224,6 +225,37 @@ void emptyUnboundedDecimalMinimumReportsDecimal() throws Exception { } } + /** + * 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. *