[SPARK-59859][SQL] Fail on overflow in date_add and date_sub instead of wrapping around #59135
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
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
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
stevomitric
What changes were proposed in this pull request?
This PR makes
date_addanddate_sub(DateAdd/DateSub) fail withDATETIME_OVERFLOWwhen the resulting number of days does not fit in anInt, instead of silently wrapping around. It addsDateTimeUtils.dateAddDays/dateSubtractDays, which useMath.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_subtracton dates, which now returnNULLon overflow as documented, andRANGEwindow frames ordered by aDATEcolumn with an integer offset, whose frame boundary is computed withDateAdd.The error message formats the start date in the proleptic Gregorian calendar of
DATEvalues, whateverspark.sql.legacy.timeParserPolicyis. UnderLEGACY,toSQLValue(_, DateType)uses the legacy formatter, whose rebasing to the Julian calendar overflows for the dates within about 44,500 days of the minimumDATE, so building the error would have thrown a rawjava.lang.ArithmeticExceptioninstead (the same issue was found in the review of #60134).Why are the changes needed?
The days were added with plain
Intarithmetic, so an overflowing result wrapped around to a wrong but valid-looking date, with ANSI mode on and off:Similar operations already fail in this case, e.g.
add_months(DATE'2020-01-01', 2147483647)raisesARITHMETIC_OVERFLOWandDATE'2020-01-01' + make_dt_interval(2147483647, 0, 0, 0)raisesINTERVAL_ARITHMETIC_OVERFLOW. The error class followsTIMESTAMPADD, which raisesDATETIME_OVERFLOWsince 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_subdoes not fit in anInt, it now fails instead of returning a wrapped date:try_add/try_subtracton dates returnNULLin that case. ARANGEwindow frame ordered by aDATEcolumn with an integer offset, such asRANGE 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 singleDATE'2020-01-01'row counted 0 rows instead of 1). Results that fit are unchanged, for exampledate_add(DATE'9999-12-31', 1)still returns+10000-01-01.How was this patch tested?
DateExpressionsSuitefor overflow in both directions and for the largest values that still fit, in both interpreted and codegen mode. The existing consistency checks forIntegerTypenow allow both paths to throw. The new tests fail without this change.spark.sql.legacy.timeParserPolicy, for start dates at the minimum and maximumDATE. Without the formatter change it fails underLEGACYwithjava.lang.ArithmeticException: integer overflow.try_add/try_subtractdate overflow cases intry_arithmetic.sql(also imported bynonansi/try_arithmetic.sql). Golden files regenerated (the.java21variant was folded into the default file by SPARK-60894). Without this change the new cases return the wrapped date-5877590-06-22.RANGEwindow frame query with ANSI mode on and off.DateExpressionsSuite,DateTimeUtilsSuite,DateFunctionsSuite, and the date, timestamp, interval and try_arithmetic tests inSQLQueryTestSuite.Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Code (Claude Opus 5.5)