Repository navigation
Retain declared MIN/MAX argument type for empty input - #570
Conversation
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.
…ents 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.
|
All four addressed in the new commit; verified against your exact repro queries. 1. Decimal bounds no longer depend on rows. Dropped the
2. The VALUES-based test is replaced. 3. Unbounded decimal arguments report DECIMAL when empty. One wrinkle that shaped the implementation:
Empty 4. Scalar subquery argument — tracked as #572. While investigating I found it's narrower and broader than it looks. Narrower: the numeric case already works on this branch — Full |
perNyfelt
left a comment
There was a problem hiding this comment.
Reviewed at head f7df136.
Existing feedback. No GitHub reviews, inline review comments, or review threads exist on this PR (REST pulls/570/reviews, pulls/570/comments and GraphQL reviewThreads are all empty; no Copilot/bot comments). The only feedback record is the issue comment from the prior local review round listing four points; each is addressed at this head, verified against the repro queries:
- Row-independent decimal bounds — verified:
MIN(CAST(mpg AS DECIMAL(10,2)))reports p=10 s=2 with rows, without rows, and in the UNION merge in both row states (previously p=4 s=2 / p=5 s=2 with rows on main). - VALUES-based test replaced — verified:
emptyDecimalMinimumRetainsDeclaredBoundsnow uses a real source-column CAST. - Unbounded decimal arguments — verified: empty
MIN(cyl / 3)andMIN(ROUND(mpg, 1))report DECIMAL p=0 s=0, matching the plain empty projection, while non-emptyMIN(cyl / 3)keeps its value-derived p=16 s=15. - Scalar subquery argument — tracked as #572 (open, with both repros).
Verification.
- Full
mvn verify(no skip flags, Java 21): BUILD SUCCESS, 4034 tests, 0 failures, checkstyle/PMD/spotless clean. - Ran a JDBC repro (18 queries) against builds of
mainand this branch. Every row in the PR's before/after table matches, including: emptyMIN(mpg)→ DOUBLE/Double, emptyMAX(model)→ VARCHAR/String, both incompatible-branch UNIONs rejected with 42000, the SUM control still rejected,MIN(mpg) UNION ALL SELECT 2.5now DOUBLE (was DECIMAL(2,1) on main), and non-empty values/classes unchanged. - Traced the metadata flow (
JParqResultSet.getMetaData→retainExpressionTypes,executeSetOperationfamily validation): the fix is metadata-side only,AggregateFunctionsuntouched, andretainedExpressionTypes's new decimal-bounds assignment can only fire for MIN/MAX columns becausenumericTypereturns null for function projections and the newdeclaredProjectionSchemabranch is the only changed declaration channel.
Minor findings (non-blocking):
- PR body: the "Before" cell for standalone
SELECT MIN(CAST(mpg AS DECIMAL(10,2))) FROM mtcars WHERE cyl = 99says "DECIMAL p=10 s=2 (unchanged)", but onmainthat query measures OTHER p=0 s=0. The table's Before column appears to mix the pre-first-commit state (this row) with the pre-PR state (the rest). Consider correcting the cell or noting the baseline. setOperationBoundsDoNotDependOnRowsasserts row-independence (equality) but not the exact expected bounds; pinning{DECIMAL, 10, 2}would anchor the declared-bounds behavior directly.- Only MIN is exercised for unbounded decimal arguments over empty input;
extremumArgumenthandles both, so a symmetric MAX case would be cheap to add.
No out-of-scope bugs found to file as issues; the remaining scalar-subquery gap is already #572.
perNyfelt
left a comment
There was a problem hiding this comment.
Re-reviewed at head 55df396 (new commit: "Pin set operation decimal bounds and cover unbounded decimal MAX"). LGTM — approving (formal approval blocked because the reviewer is the PR author).
Previous feedback status (all resolved):
- My review of f7df136, finding 1 (PR body "Before" baseline for standalone empty
MIN(CAST(mpg AS DECIMAL(10,2)))): fixed — the body now showsOTHER/java.lang.Objectas the pre-PR state for that row, matching the measurement onmain. - Finding 2 (pin exact merged bounds): fixed —
setOperationBoundsDoNotDependOnRowsnow asserts{DECIMAL, 10, 2}for both the empty and filled MIN branch instead of only asserting row-independence. - Finding 3 (MAX coverage for unbounded decimal arguments): fixed — new
emptyUnboundedDecimalMaximumReportsDecimalmirrors the MIN test, covering emptyMAX(cyl / 3)(DECIMAL p=0 s=0), emptyMAX(ROUND(mpg, 1)), and the non-empty value case. - The four points from the prior local round (author's issue comment) remain addressed; 55df396 is a test-only delta, so the previously verified behavior is unchanged.
- No review threads, Copilot, or bot comments on the PR.
Verification at 55df396:
- Full
mvn verify(no skip flags, Java 21): BUILD SUCCESS, 4035 tests, 0 failures (13 inMinMaxEmptyInputTest), checkstyle/PMD/spotless clean. - Delta inspected: test-only changes to
src/test/java/jparq/MinMaxEmptyInputTest.javaplus the corrected PR-body row; no main-code changes, so no re-verification of the metadata flow was needed.
No open findings and no out-of-scope bugs found; nothing new to file as issues.
Problem
MIN/MAXresult metadata was inferred from the returned value. Over empty input (or an all-NULL group) the column reportedOTHER/java.lang.Objecteven though the argument has a declared type, and set operations then treated such a branch as an untyped NULL and accepted incompatible branch types. Decimal bounds also depended on the returned rows:MIN(CAST(mpg AS DECIMAL(10,2)))reported value-inferreddecimal(4,2)with rows andOTHERwithout.Reproduction (mtcars fixture:
mpgDOUBLE,modelVARCHAR)SELECT MIN(mpg) FROM mtcars WHERE cyl = 99OTHER/java.lang.ObjectDOUBLE/java.lang.DoubleSELECT MAX(model) FROM mtcars WHERE cyl = 99OTHER/java.lang.ObjectVARCHAR/java.lang.StringSELECT MIN(CAST(mpg AS DECIMAL(10,2))) FROM mtcarsDECIMALp=4 s=2 (value-inferred)DECIMALp=10 s=2 (declared)SELECT MIN(CAST(mpg AS DECIMAL(10,2))) FROM mtcars WHERE cyl = 99OTHER/java.lang.ObjectDECIMALp=10 s=2 (declared)… UNION ALL SELECT CAST(1 AS DECIMAL(3,0))(with rows / empty branch)SELECT MIN(cyl / 3) FROM mtcars WHERE cyl = 99OTHER/java.lang.ObjectDECIMALp=0 s=0 (unbounded)SELECT MIN(ROUND(mpg, 1)) FROM mtcars WHERE cyl = 99OTHER/java.lang.ObjectDECIMALp=0 s=0 (unbounded)SELECT MIN(mpg) FROM mtcars WHERE cyl = 99 UNION ALL SELECT 'a'OTHER)SELECT MAX(model) FROM mtcars WHERE cyl = 99 UNION ALL SELECT 1INTEGER)SELECT SUM(cyl) FROM mtcars WHERE cyl = 99 UNION ALL SELECT 'a'(control)Non-empty observed values are unchanged:
MIN(mpg)still returns10.4asDouble, and unbounded decimal arguments keep their value-derived bounds when rows exist (MIN(cyl / 3)staysdecimal(16,15)).Root cause
Aggregate result types come from the accumulator's observed value class: an empty or all-NULL
ExtremumAccumulatorreportssqlTypeForClass(null)=OTHER, and the rows-based constructor infers decimal bounds only from materialized values. The declared-type fallback,JParqResultSetMetaData.declaredProjectionSchema, resolved window MIN/MAX but had no branch for top-level aggregate MIN/MAX, so nothing replaced theOTHERin the metadata. Set operations read the branch type throughResultSetMetaData/columnValueSchema, classified the branch as untypedNULL, and merged it with anything.Fix (metadata side, no value-side changes)
JParqResultSetMetaData.declaredProjectionSchema: a top-level single-argumentMIN/MAXnow resolves its argument's declared schema viaArrayExpressionSchemas.resolve, mirroring the existing window (AnalyticExpression) branch. The existingAggregateResultSetMetaData.retainExpressionTypesmechanism then applies the declaration, which also fixes set-operation rejection becauseexecuteSetOperationreads branch types from the same metadata. Value-side inference inAggregateFunctionsis intentionally untouched.JParqResultSetMetaData.unboundedExtremumArgumentType(new): arguments whose declaration is an unbounded decimal (cyl / 3,ROUND(mpg, 1)) have no Avro value schema (NumericType.schema()is null for precision 0), so theirNumericTypeis exposed directly.AggregateResultSetMetaData.retainExpressionTypes: uses the unbounded-decimal numeric declaration when the value-schema channel cannot express it (fixing emptyMIN(cyl / 3)→DECIMAL p=0 s=0, matching the plain empty projection), and applies a resolved decimal declaration's bounds unconditionally, so declared bounds no longer depend on whether rows came back. Unbounded declarations (precision 0) still never override value-derived bounds.Tests
jparq.MinMaxEmptyInputTest(13 tests): emptyMIN/MAXreport the argument's declared type and class (DOUBLE/Double, VARCHAR/String, INTEGER/Integer); non-empty MIN/MAX keep observed values and types; an all-NULL group reports the argument's declared type; an empty DECIMAL(10,2) MIN retains the declared bounds and a set operation over it merges the declareddecimal(10,2)bounds whether or not the branch returns rows; empty unbounded-decimal MIN/MAX (cyl / 3,ROUND(mpg, 1)) reportDECIMAL p=0 s=0; both incompatible-branch UNION variants are rejected with 42000 and the existing message; a compatible numeric branch still merges.Out of scope, tracked as #572: a scalar subquery argument with a nonnumeric result (e.g.
MIN((SELECT MAX(model) FROM mtcars))) still reportsOTHERover empty input; the same gap pre-exists for top-level scalar subquery projections over empty outer input.Verified: checkstyle/PMD/spotless clean (no new violations vs main baseline), full
mvn -o verifygreen.Fixes #320.