[SPARK-58235][SQL] Fix Double/Float to Timestamp silent overflow clamping in non-ANSI mode - #57399
Open
jiangxt2 wants to merge 2 commits into
Open
[SPARK-58235][SQL] Fix Double/Float to Timestamp silent overflow clamping in non-ANSI mode#57399jiangxt2 wants to merge 2 commits into
jiangxt2 wants to merge 2 commits into
Conversation
uros-b
reviewed
Jul 21, 2026
uros-b
approved these changes
Jul 21, 2026
uros-b
left a comment
Member
There was a problem hiding this comment.
Thank you @jiangxt2, please address comment before merge - otherwise LGTM! cc @MaxGekk @gengliangwang
Author
|
Thanks for catching this! Moved the entry to the Upgrading from Spark SQL 4.2 to 4.3 section and corrected the version to Since Spark 4.3. @uros-b |
jiangxt2
force-pushed
the
fix/double-to-timestamp-overflow
branch
from
July 22, 2026 08:55
ce0ebe2 to
2e93c6a
Compare
…ping in non-ANSI mode In non-ANSI mode, a Double or Float cast to Timestamp that overflows the Long range is silently clamped to Long.MAX_VALUE or Long.MIN_VALUE instead of returning NULL. This is inconsistent with other overflow casts (e.g., Timestamp to integral types) which return NULL in non-ANSI mode. Fix: add overflow detection in doubleToTimestamp (interpreted path) and castToTimestampCode Double/Float branches (codegen path). In non-ANSI mode, overflow returns NULL; in ANSI mode, it throws CAST_OVERFLOW via doubleToTimestampAnsi. Signed-off-by: jiangxt2 <jiangxt2@vip.qq.com>
Signed-off-by: jiangxt2 <jiangxt2@vip.qq.com>
jiangxt2
force-pushed
the
fix/double-to-timestamp-overflow
branch
from
July 23, 2026 01:39
2e93c6a to
2955922
Compare
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.
What changes were proposed in this pull request?
Add Long range validation for the Double/Float-to-Timestamp cast path in both interpreted and codegen execution.
When a Double or Float value multiplied by
MICROS_PER_SECOND(1,000,000) exceedsLongrange, the unchecked cast tolongsilently produces a clamped value (Long.MAX_VALUEorLong.MIN_VALUE). The fix detects this overflow and either returnsNULL(non-ANSI mode) or throwsCAST_OVERFLOW(ANSI mode).The interpreted path (
Cast.doubleToTimestamp) now checks the multiplication result against Long range before calling.toLong. The codegen path (castToTimestampCodeDouble/Float branches) applies the same range check before the(long)cast.Why are the changes needed?
CAST(1e20 AS TIMESTAMP)silently produces+294247-01-10 04:00:54.775807(clamped toLong.MAX_VALUE) instead ofNULLin non-ANSI mode. This is inconsistent with other overflow behavior:The ANSI mode path (
doubleToTimestampAnsi→DoubleExactNumeric.toLong) already correctly throwsCAST_OVERFLOW. This bug only affects non-ANSI mode.Does this PR introduce any user-facing change?
Yes. Previously,
CAST(large_double AS TIMESTAMP)silently returned an incorrect value caused by numeric overflow. Now:NULL(consistent with other overflow casts)CAST_OVERFLOWerror (unchanged)How was this patch tested?
Added unit tests in
CastWithAnsiOffSuiteandCastWithAnsiOnSuite:Non-ANSI mode (
CastWithAnsiOffSuite):1E20→ null-1E20→ null9223372036854.0→ passes through9223372036855.0→ null-9223372036854.0/-9223372036855.0Double.MaxValue/-Double.MaxValueANSI mode (
CastWithAnsiOnSuite):CAST_OVERFLOWwith correct error parametersCAST_OVERFLOWwith correct error parametersWas this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Code (Opus 4.8)