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
8 changes: 8 additions & 0 deletions release.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/se/alipsa/jparq/AggregateResultSetMetaData.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ void retainValueSchemas(List<Schema> 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
Expand All @@ -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) {
Expand All @@ -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));
}
Expand Down
54 changes: 52 additions & 2 deletions src/main/java/se/alipsa/jparq/JParqResultSetMetaData.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)) {
Expand All @@ -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.
*
Expand Down Expand Up @@ -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);
}
Expand Down
Loading
Loading