Repository navigation
Report SQLState 22003 for floating-point arithmetic and SUM overflow - #574
Merged
Merged
Conversation
perNyfelt
commented
Nov 18, 2026
perNyfelt
left a comment
Member
Author
There was a problem hiding this comment.
Existing feedback
No prior review threads, review bodies, or PR comments exist on this PR, so there is nothing unresolved to check.
Verification
- CI
verifyis green on the current head (both the push and pull_request runs). - Ran
jparq.FloatingPointArithmeticOverflowTestlocally: 12/12 pass, plus the adjacent existing suites (RealSumTest, window sum/aggregate and metadata suites, aggregate function suites) with no regressions. - Exception classification:
SqlExceptions.numericRangeFailurefollows the established engine-failure pattern (IllegalArgumentExceptioncarrying a classifiedSQLDataExceptioncause);findSqlStateFailure/wraptranslate it to aSQLDataExceptionwith22003at the JDBC boundary since22003is inCLASSIFIED_STATES. - The hoisted zero-divisor check in
calculateis behaviorally identical to the previous in-switch ternary (right == 0catches±0.0as before). With zero divisors returned early,0/0NaN cannot reach the finiteness guard, and for finite operands with a non-zero divisor the only non-finite outcome is overflow, so the guard is precise. Non-finite operands keep IEEE 754 semantics, matching the updated doc. - The
AVGno-guard claim holds: an average of finite values is bounded by the largest finite input (≤DBL_MAX), and rounding the exact quotient toDECIMAL64's 34 digits cannot produce a value whosedoubleValue()overflows. BigDecimalsums are always finite, sodoubleValue()is non-finite only on overflow; the "SUM overflows the DOUBLE range" message is accurate, and all-NULL input still returns NULL.- Doc changes are consistent across the arithmetic table, the floating-point values section, and the
SUMentry. Math functions (EXP,POWER) can still yieldInfinityfor finite inputs, which the PR explicitly scopes out; the SQL standard treats out-of-range mathematical-function results as indeterminate, and the doc statement is limited to arithmetic operators, so this is not filed as a deviation.
Findings (non-blocking)
- DRY nit — the identical SUM overflow guard now exists in two places:
SumAccumulator.result()(src/main/java/se/alipsa/jparq/engine/function/AggregateFunctions.java:2802) andSumComputationState.result()(src/main/java/se/alipsa/jparq/engine/window/SumComputationState.java:34). A small shared helper would keep the message and behavior in lockstep if either path ever needs to change. (The two classes already mirror each other's type-widening logic, so this is a soft suggestion.) - Test coverage nit —
sumOverflowReportsSqlStateonly covers positive operands (SUM(CAST(1e308 AS DOUBLE))). The arithmetic tests include negative overflow variants; a symmetricSUM(CAST(-1e308 AS DOUBLE))case would close that gap cheaply.
perNyfelt
commented
Nov 18, 2026
perNyfelt
left a comment
Member
Author
There was a problem hiding this comment.
Existing feedback status
- Finding 1 (DRY — duplicated SUM overflow guard) — fixed in
a4482d23. The guard is now shared asNumericCasts.toFiniteDouble(BigDecimal, String)(src/main/java/se/alipsa/jparq/engine/NumericCasts.java:84), called from bothSumAccumulator.result()(AggregateFunctions.java:2802) andSumComputationState.result()(SumComputationState.java:34). The class javadoc was updated accordingly. - Finding 2 (negative SUM coverage) — fixed in
a4482d23.sumOverflowReportsSqlStatenow iterates bothSUM(CAST(1e308 AS DOUBLE))andSUM(CAST(-1e308 AS DOUBLE))across all seven query forms, and the in-range test additionally coversSUM(CAST(-1e300 AS DOUBLE))in aggregate and window forms viaExpectation. - No other feedback exists: no review threads, no PR or issue comments, and no Copilot/bot activity. Nothing else outstanding.
Verification of updated head (c25ae09)
- CI
verifygreen on the new head (both push and pull_request runs). - Ran
jparq.FloatingPointArithmeticOverflowTestlocally: 12/12 pass. - The refactor is behavior-preserving: both call sites use the identical
doubleValue()+isFinitecheck and message as before, and the new helper is properly documented with@param/@return/@throws. c25ae097removes the accidentally committedsrc/test/.DS_Store.
Remaining note (non-blocking)
.gitignore has no entry for .DS_Store, which is what allowed src/test/.DS_Store to be committed in the first place. Adding .DS_Store to .gitignore alongside the removal would prevent recurrence on macOS checkouts.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Floating-point arithmetic that overflows returned
Infinityinstead of raising an error.SELECT CAST(1e300 AS DOUBLE) * CAST(1e300 AS DOUBLE) FROM mtcars LIMIT 1came back asInfinity. Integer arithmetic already raisesSQLDataExceptionwith SQLState22003in the same situation (BIGINT arithmetic overflow), and PR #321 made numericCASTreject overflow to infinity with22003— so arithmetic stayed the inconsistent member: values thatCASTrejects could still be produced by*,+,-,/onDOUBLEorREALoperands. The same leak existed inSUMoverREAL/DOUBLE: the exactBigDecimalsum was converted withdoubleValue(), whose overflow producesInfinity, and the value was returned as-is.Reproduction (verified on main)
SELECT CAST(1e300 AS DOUBLE) * CAST(1e300 AS DOUBLE)InfinitySELECT CAST(1e308 AS DOUBLE) + CAST(1e308 AS DOUBLE)InfinitySELECT CAST(1e300 AS DOUBLE) / CAST(1e-300 AS DOUBLE)InfinitySELECT CAST(1e38 AS REAL) * CAST(1e300 AS DOUBLE)InfinitySELECT SUM(CAST(1e308 AS DOUBLE)) FROM mtcarsInfinitySELECT SUM(CAST(1e308 AS DOUBLE)) OVER () FROM mtcarsInfinitySELECT CAST(1e300 AS DOUBLE) + CAST(1e300 AS DOUBLE)2.0E300SELECT CAST(1 AS DOUBLE) / CAST(0 AS DOUBLE)NULLInfinity * 0NaN(IEEE 754)Root cause
The double branch of
AggregateFunctions.calculatereturned the raw IEEE 754 result of+,-,*,/,%without any range check, so overflow surfaced to the client as±Infinity. BothSUMpaths (aggregateSumAccumulator.result()and windowSumComputationState.result()) converted the exactBigDecimalsum to the reportedDoubleresult viadoubleValue(), which overflows to±Infinity, and returned it unchanged.Fix
AggregateFunctions.calculate: the zero-divisor NULL forDIV/MODis hoisted out of the switch; the double result is computed once and rejected with SQLState22003(DOUBLE arithmetic overflow) when both operands are finite and the result is not. Non-finite operands keep IEEE 754 semantics (Infinity * 0→NaN,NaN + 1→NaN), as the floating-point values section documents.SqlExceptions.numericRangeFailure(String): new shared factory producing the 22003IllegalArgumentExceptionclassification, keeping the same shape as the existingBIGINT arithmetic overflowfailure.SumAccumulator.result()andSumComputationState.result(): thesum.doubleValue()paths are consolidated into a single call to the new shared helperNumericCasts.toFiniteDouble, which rejects a non-finite conversion with22003(SUM overflows the DOUBLE range), keeping both SUM paths in lockstep. Integer andBigDecimalresult paths are unchanged, and an all-NULL input still returns NULL.doc/sql-reference.md: the numeric arithmetic table, the floating-point values section, and theSUMaggregate entry now state that finite operands producing a non-finite result raise22003.AVGneeds no guard: the average of finite values is bounded by the largest finite input, so theDECIMAL64division cannot overflow theDOUBLEresult for finite inputs. Math functions (EXP,POWER, …) that can produce infinity are a separate concern and out of scope here.Tests
jparq.FloatingPointArithmeticOverflowTest(12 tests, predicate/projection pushdown on and off):+,-,*,/, andREAL * DOUBLEoverflow on finite operands raise 22003 in direct, derived-table, CTE, scalar-subquery,WHERE, and bare-literal forms;SUMoverflow in global, grouped,HAVING,ORDER BY, and window forms raises 22003 for both signs (1e308and-1e308); in-range results (2.0E300,DBL_MAX / 1e300,10.5 % 2.0), NULL zero-divisors, and the exact ±32 ×1e300sums pass through unchanged.Verified: full
mvn verifygreen (0 test failures), 0 Checkstyle/PMD/Spotless violations.Fixes #323.