← Back to Pull requests

[SPARK-59859][SQL] Fail on overflow in date_add and date_sub instead of wrapping around #59135

Open
unknown opened this pull request with 509 lines diff across 6 files
View on GitHub
unknown unknown commented on Sep 29, 2026 13:01 UTC
Author

What changes were proposed in this pull request?

This PR makes date_add and date_sub (DateAdd / DateSub) fail with DATETIME_OVERFLOW when the resulting number of days does not fit in an Int, instead of silently wrapping around. It adds DateTimeUtils.dateAddDays / dateSubtractDays, which use Math.addExact / Math.subtractExact, and uses them in both the interpreted and the generated code.

This also covers the operators and functions that resolve to these expressions: date + int, date - int, dateadd, try_add / try_subtract on dates, which now return NULL on overflow as documented, and RANGE window frames ordered by a DATE column with an integer offset, whose frame boundary is computed with DateAdd.

The error message formats the start date in the proleptic Gregorian calendar of DATE values, whatever spark.sql.legacy.timeParserPolicy is. Under LEGACY, toSQLValue(_, DateType) uses the legacy formatter, whose rebasing to the Julian calendar overflows for the dates within about 44,500 days of the minimum DATE, so building the error would have thrown a raw java.lang.ArithmeticException instead (the same issue was found in the review of #60134).

Why are the changes needed?

The days were added with plain Int arithmetic, so an overflowing result wrapped around to a wrong but valid-looking date, with ANSI mode on and off:

SELECT date_add(DATE'2020-01-01', 2147483647);   -- -5877591-06-21
SELECT date_sub(DATE'1900-01-01', 2147483647);   -- +5881510-07-14
SELECT try_add(DATE'2020-01-01', 2147483647);    -- -5877591-06-21 instead of NULL

Similar operations already fail in this case, e.g. add_months(DATE'2020-01-01', 2147483647) raises ARITHMETIC_OVERFLOW and DATE'2020-01-01' + make_dt_interval(2147483647, 0, 0, 0) raises INTERVAL_ARITHMETIC_OVERFLOW. The error class follows TIMESTAMPADD, which raises DATETIME_OVERFLOW since SPARK-38481.

Does this PR introduce _any_ user-facing change?

Yes. When the number of days since 1970-01-01 in the result of date_add / date_sub does not fit in an Int, it now fails instead of returning a wrapped date:

[DATETIME_OVERFLOW] Datetime operation overflow: add 2147483647 days to DATE '2020-01-01'. SQLSTATE: 22008

try_add / try_subtract on dates return NULL in that case. A RANGE window frame ordered by a DATE column with an integer offset, such as RANGE BETWEEN CURRENT ROW AND 2147483647 FOLLOWING, now fails the same way instead of using a wrapped frame boundary (before this change, that frame over a single DATE'2020-01-01' row counted 0 rows instead of 1). Results that fit are unchanged, for example date_add(DATE'9999-12-31', 1) still returns +10000-01-01.

How was this patch tested?

  • New tests in DateExpressionsSuite for overflow in both directions and for the largest values that still fit, in both interpreted and codegen mode. The existing consistency checks for IntegerType now allow both paths to throw. The new tests fail without this change.

  • A test that the error message is the same under every spark.sql.legacy.timeParserPolicy, for start dates at the minimum and maximum DATE. Without the formatter change it fails under LEGACY with java.lang.ArithmeticException: integer overflow.

  • New try_add / try_subtract date overflow cases in try_arithmetic.sql (also imported by nonansi/try_arithmetic.sql). Golden files regenerated (the .java21 variant was folded into the default file by SPARK-60894). Without this change the new cases return the wrapped date -5877590-06-22.

  • Ran the queries above on a table (not constant folded) with whole-stage codegen and interpreted mode, with ANSI mode on and off, and the RANGE window frame query with ANSI mode on and off.

  • Ran DateExpressionsSuite, DateTimeUtilsSuite, DateFunctionsSuite, and the date, timestamp, interval and try_arithmetic tests in SQLQueryTestSuite.
  • Was this patch authored or co-authored using generative AI tooling?

    Generated-by: Claude Code (Claude Opus 5.5)

    Diff: 509 lines (+381 / -128) across 6 files Reviewer: stevomitric Review duration: 70676m
    All Vouch review confidence checks passed
    Residual risk 0.30 = Change Risk (0.55) × (1 − Review Confidence (0.45)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.55 · 509 lines diff across 6 files
    0.55
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    44%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.373
    Shallow Review Depth shallow_review_depth
    0.344
    Real PR Conversation Timeline
    1 comments
    "Review active on #59135"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 44.7% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 54.6% (MEDIUM) · Higher = larger blast radius
    54.6%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 44.7% · Lower = rushed or shallow review
    44.7%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 30.2% · Safe to merge
    30.2%
    Repository