fix: preserve ANSI errors for rejected TIMESTAMP_NTZ casts - #5752
Open
peterxcli wants to merge 1 commit into
Open
fix: preserve ANSI errors for rejected TIMESTAMP_NTZ casts#5752peterxcli wants to merge 1 commit into
peterxcli wants to merge 1 commit into
Conversation
manuzhang
approved these changes
Sep 7, 2026
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.
Which issue does this PR close?
Closes #5749.
Rationale for this change
CAST(string AS TIMESTAMP_NTZ)can silently return NULL under ANSI mode when the input matches a supported timestamp pattern but the timestamp decoder rejects it. For example, casting a Parquet-backed string column containing294249-01-01raisesCAST_INVALID_INPUTin Spark 4.1.3, while Comet previously returned NULL. The equivalent Comet cast toTIMESTAMPalready raises an error.The discrepancy comes from
timestamp_ntz_parser_inner: aNonereturned byparse_to_timestamp_infowas mapped directly toOk(None), bypassing the evaluation-mode check. Six-digit years outside the decoder's allowed range reach this branch because they pass the timestamp pattern check.What changes are included in this PR?
The NTZ parser now returns immediately only when decoding and conversion to microseconds both succeed. If either rejects a recognized input, parsing stops and falls through to the existing evaluation-mode handling, which produces
InvalidInputInCastToDatetimefor ANSI mode and NULL for legacy and try modes. Unrecognized input continues through the same handling. This removes the duplicated ANSI error construction and ensures decoder rejection and conversion rejection follow the same policy without changing the accepted timestamp formats or year limits.Regression coverage includes
294249-01-01,294249-01-01 00:00:00, and-290310-01-01. Native tests check the ANSI error's value and source/target types, plus NULL results in legacy and try modes. Spark comparison tests exercise bothTIMESTAMPandTIMESTAMP_NTZusing Parquet-backed columns to prevent constant folding. Each malformed value is checked separately so an exception from one value cannot hide another case.How are these changes tested?
git diff --checkpassed.