Skip to content

fix: TRY_CAST returns NULL for timestamp/date overflow#22897

Open
fengys1996 wants to merge 2 commits into
apache:mainfrom
fengys1996:fix/try_cast_temporal_overflow
Open

fix: TRY_CAST returns NULL for timestamp/date overflow#22897
fengys1996 wants to merge 2 commits into
apache:mainfrom
fengys1996:fix/try_cast_temporal_overflow

Conversation

@fengys1996

@fengys1996 fengys1996 commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Rationale for this change

TRY_CAST should return NULL on cast failure, but overflowing date/timestamp casts returned errors.

What changes are included in this PR?

  • Make scalar temporal overflow checks respect CastOptions.safe.
  • Skip DataFusion’s array pre-check for safe casts so Arrow can return NULLs.
  • Add regression tests.

Are these changes tested?

Yes:

cargo test -p datafusion-common timestamp_overflow_returns
cargo test -p datafusion-expr-common timestamp_array_to_timestamp_overflow
cargo test --test sqllogictests -- datetime/timestamps.slt

Are there any user-facing changes?

Yes. TRY_CAST for overflowing date/timestamp casts now returns NULL; regular CAST still errors.

Known Limitation

This PR does not add Date array-path coverage yet. For example:

SELECT TRY_CAST(d AS TIMESTAMP(9))
FROM (VALUES (DATE '3000-01-01')) t(d);

This depends on the upstream Arrow fix in apache/arrow-rs#9825. Once DataFusion updates to an Arrow version containing that fix, we can add this regression test.

@github-actions github-actions Bot added logical-expr Logical plan and expressions sqllogictest SQL Logic Tests (.slt) common Related to common crate labels Jun 11, 2026
@fengys1996 fengys1996 marked this pull request as ready for review June 11, 2026 03:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

common Related to common crate logical-expr Logical plan and expressions sqllogictest SQL Logic Tests (.slt)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TRY_CAST returns an error instead of NULL for timestamp/date overflow

1 participant