Skip to content

Report SQLState 22003 for floating-point arithmetic and SUM overflow - #574

Merged
perNyfelt merged 3 commits into
mainfrom
fix/323-floating-point-arithmetic-overflow
Nov 18, 2026
Merged

perNyfelt merged 3 commits into
mainfrom
fix/323-floating-point-arithmetic-overflow

Conversation

@perNyfelt

@perNyfelt perNyfelt commented Nov 18, 2026 •

Copy link
Copy Markdown
Member

Problem

Floating-point arithmetic that overflows returned Infinity instead of raising an error. SELECT CAST(1e300 AS DOUBLE) * CAST(1e300 AS DOUBLE) FROM mtcars LIMIT 1 came back as Infinity. Integer arithmetic already raises SQLDataException with SQLState 22003 in the same situation (BIGINT arithmetic overflow), and PR #321 made numeric CAST reject overflow to infinity with 22003 — so arithmetic stayed the inconsistent member: values that CAST rejects could still be produced by *, +, -, / on DOUBLE or REAL operands. The same leak existed in SUM over REAL/DOUBLE: the exact BigDecimal sum was converted with doubleValue(), whose overflow produces Infinity, and the value was returned as-is.

Reproduction (verified on main)

Query Before After
SELECT CAST(1e300 AS DOUBLE) * CAST(1e300 AS DOUBLE) Infinity rejected, 22003
SELECT CAST(1e308 AS DOUBLE) + CAST(1e308 AS DOUBLE) Infinity rejected, 22003
SELECT CAST(1e300 AS DOUBLE) / CAST(1e-300 AS DOUBLE) Infinity rejected, 22003
SELECT CAST(1e38 AS REAL) * CAST(1e300 AS DOUBLE) Infinity rejected, 22003
SELECT SUM(CAST(1e308 AS DOUBLE)) FROM mtcars Infinity rejected, 22003
SELECT SUM(CAST(1e308 AS DOUBLE)) OVER () FROM mtcars Infinity rejected, 22003
SELECT CAST(1e300 AS DOUBLE) + CAST(1e300 AS DOUBLE) 2.0E300 unchanged (in range)
SELECT CAST(1 AS DOUBLE) / CAST(0 AS DOUBLE) NULL unchanged
non-finite operand, e.g. Infinity * 0 NaN (IEEE 754) unchanged

Root cause

The double branch of AggregateFunctions.calculate returned the raw IEEE 754 result of +, -, *, /, % without any range check, so overflow surfaced to the client as ±Infinity. Both SUM paths (aggregate SumAccumulator.result() and window SumComputationState.result()) converted the exact BigDecimal sum to the reported Double result via doubleValue(), which overflows to ±Infinity, and returned it unchanged.

Fix

  • AggregateFunctions.calculate: the zero-divisor NULL for DIV/MOD is hoisted out of the switch; the double result is computed once and rejected with SQLState 22003 (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 22003 IllegalArgumentException classification, keeping the same shape as the existing BIGINT arithmetic overflow failure.
  • SumAccumulator.result() and SumComputationState.result(): the sum.doubleValue() paths are consolidated into a single call to the new shared helper NumericCasts.toFiniteDouble, which rejects a non-finite conversion with 22003 (SUM overflows the DOUBLE range), keeping both SUM paths in lockstep. Integer and BigDecimal result 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 the SUM aggregate entry now state that finite operands producing a non-finite result raise 22003.

AVG needs no guard: the average of finite values is bounded by the largest finite input, so the DECIMAL64 division cannot overflow the DOUBLE result 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): +, -, *, /, and REAL * DOUBLE overflow on finite operands raise 22003 in direct, derived-table, CTE, scalar-subquery, WHERE, and bare-literal forms; SUM overflow in global, grouped, HAVING, ORDER BY, and window forms raises 22003 for both signs (1e308 and -1e308); in-range results (2.0E300, DBL_MAX / 1e300, 10.5 % 2.0), NULL zero-divisors, and the exact ±32 × 1e300 sums pass through unchanged.

Verified: full mvn verify green (0 test failures), 0 Checkstyle/PMD/Spotless violations.

Fixes #323.

@perNyfelt perNyfelt left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Existing feedback

No prior review threads, review bodies, or PR comments exist on this PR, so there is nothing unresolved to check.

Verification

  • CI verify is green on the current head (both the push and pull_request runs).
  • Ran jparq.FloatingPointArithmeticOverflowTest locally: 12/12 pass, plus the adjacent existing suites (RealSumTest, window sum/aggregate and metadata suites, aggregate function suites) with no regressions.
  • Exception classification: SqlExceptions.numericRangeFailure follows the established engine-failure pattern (IllegalArgumentException carrying a classified SQLDataException cause); findSqlStateFailure/wrap translate it to a SQLDataException with 22003 at the JDBC boundary since 22003 is in CLASSIFIED_STATES.
  • The hoisted zero-divisor check in calculate is behaviorally identical to the previous in-switch ternary (right == 0 catches ±0.0 as before). With zero divisors returned early, 0/0 NaN 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 AVG no-guard claim holds: an average of finite values is bounded by the largest finite input (≤ DBL_MAX), and rounding the exact quotient to DECIMAL64's 34 digits cannot produce a value whose doubleValue() overflows.
  • BigDecimal sums are always finite, so doubleValue() 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 SUM entry. Math functions (EXP, POWER) can still yield Infinity for 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)

  1. 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) and SumComputationState.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.)
  2. Test coverage nit — sumOverflowReportsSqlState only covers positive operands (SUM(CAST(1e308 AS DOUBLE))). The arithmetic tests include negative overflow variants; a symmetric SUM(CAST(-1e308 AS DOUBLE)) case would close that gap cheaply.

@perNyfelt perNyfelt left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Existing feedback status

  • Finding 1 (DRY — duplicated SUM overflow guard) — fixed in a4482d23. The guard is now shared as NumericCasts.toFiniteDouble(BigDecimal, String) (src/main/java/se/alipsa/jparq/engine/NumericCasts.java:84), called from both SumAccumulator.result() (AggregateFunctions.java:2802) and SumComputationState.result() (SumComputationState.java:34). The class javadoc was updated accordingly.
  • Finding 2 (negative SUM coverage) — fixed in a4482d23. sumOverflowReportsSqlState now iterates both SUM(CAST(1e308 AS DOUBLE)) and SUM(CAST(-1e308 AS DOUBLE)) across all seven query forms, and the in-range test additionally covers SUM(CAST(-1e300 AS DOUBLE)) in aggregate and window forms via Expectation.
  • 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 verify green on the new head (both push and pull_request runs).
  • Ran jparq.FloatingPointArithmeticOverflowTest locally: 12/12 pass.
  • The refactor is behavior-preserving: both call sites use the identical doubleValue() + isFinite check and message as before, and the new helper is properly documented with @param/@return/@throws.
  • c25ae097 removes the accidentally committed src/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.

@perNyfelt
perNyfelt merged commit a0d5ad3 into main Nov 18, 2026
2 checks passed
@perNyfelt
perNyfelt deleted the fix/323-floating-point-arithmetic-overflow branch November 18, 2026 10:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Floating-point arithmetic overflow returns Infinity instead of SQLState 22003

1 participant