Commit cb2542c
authored
fix: TRY_CAST returns NULL for timestamp/date overflow (#22897)
## Which issue does this PR close?
<!--
We generally require a GitHub issue to be filed for all bug fixes and
enhancements and this helps us generate change logs for our releases.
You can link an issue to this PR using the GitHub syntax. For example
`Closes #123` indicates that this PR will close issue #123.
-->
- Closes #22896.
## Rationale for this change
`TRY_CAST` should return NULL on cast failure, but overflowing
date/timestamp casts returned errors.
<!--
Why are you proposing this change? If this is already explained clearly
in the issue then this section is not needed.
Explaining clearly why changes are proposed helps reviewers understand
your changes and offer better suggestions for fixes.
-->
## 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.
<!--
There is no need to duplicate the description in the issue here but it
is sometimes worth providing a summary of the individual changes in this
PR.
-->
## Are these changes tested?
Yes:
```bash
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
```
<!--
We typically require tests for all PRs in order to:
1. Prevent the code from being accidentally broken by subsequent changes
2. Serve as another way to document the expected behavior of the code
If tests are not included in your PR, please explain why (for example,
are they covered by existing tests)?
-->
## Are there any user-facing changes?
Yes. TRY_CAST for overflowing date/timestamp casts now returns NULL;
regular CAST still errors.
<!--
If there are user-facing changes then we may require documentation to be
updated before approving the PR.
-->
<!--
If there are any breaking changes to public APIs, please add the `api
change` label.
-->
## Known Limitation
This PR does not add Date array-path coverage yet. For example:
```sql
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.1 parent 78033fa commit cb2542c
3 files changed
Lines changed: 94 additions & 2 deletions
File tree
- datafusion
- common/src/scalar
- expr-common/src
- sqllogictest/test_files/datetime
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4292 | 4292 | | |
4293 | 4293 | | |
4294 | 4294 | | |
4295 | | - | |
| 4295 | + | |
| 4296 | + | |
| 4297 | + | |
| 4298 | + | |
| 4299 | + | |
| 4300 | + | |
| 4301 | + | |
| 4302 | + | |
4296 | 4303 | | |
4297 | 4304 | | |
4298 | 4305 | | |
| |||
10190 | 10197 | | |
10191 | 10198 | | |
10192 | 10199 | | |
| 10200 | + | |
| 10201 | + | |
| 10202 | + | |
| 10203 | + | |
| 10204 | + | |
| 10205 | + | |
| 10206 | + | |
| 10207 | + | |
| 10208 | + | |
| 10209 | + | |
| 10210 | + | |
| 10211 | + | |
| 10212 | + | |
| 10213 | + | |
| 10214 | + | |
| 10215 | + | |
| 10216 | + | |
| 10217 | + | |
10193 | 10218 | | |
10194 | 10219 | | |
10195 | 10220 | | |
| |||
10203 | 10228 | | |
10204 | 10229 | | |
10205 | 10230 | | |
| 10231 | + | |
| 10232 | + | |
| 10233 | + | |
| 10234 | + | |
| 10235 | + | |
| 10236 | + | |
| 10237 | + | |
| 10238 | + | |
| 10239 | + | |
| 10240 | + | |
| 10241 | + | |
| 10242 | + | |
| 10243 | + | |
| 10244 | + | |
| 10245 | + | |
| 10246 | + | |
| 10247 | + | |
| 10248 | + | |
10206 | 10249 | | |
10207 | 10250 | | |
10208 | 10251 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
325 | 325 | | |
326 | 326 | | |
327 | 327 | | |
328 | | - | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
329 | 331 | | |
330 | 332 | | |
331 | 333 | | |
| |||
766 | 768 | | |
767 | 769 | | |
768 | 770 | | |
| 771 | + | |
| 772 | + | |
| 773 | + | |
| 774 | + | |
| 775 | + | |
| 776 | + | |
| 777 | + | |
| 778 | + | |
| 779 | + | |
| 780 | + | |
| 781 | + | |
| 782 | + | |
| 783 | + | |
| 784 | + | |
| 785 | + | |
| 786 | + | |
| 787 | + | |
| 788 | + | |
| 789 | + | |
| 790 | + | |
| 791 | + | |
| 792 | + | |
| 793 | + | |
| 794 | + | |
| 795 | + | |
| 796 | + | |
| 797 | + | |
| 798 | + | |
769 | 799 | | |
Lines changed: 19 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5379 | 5379 | | |
5380 | 5380 | | |
5381 | 5381 | | |
| 5382 | + | |
| 5383 | + | |
| 5384 | + | |
| 5385 | + | |
| 5386 | + | |
| 5387 | + | |
| 5388 | + | |
| 5389 | + | |
| 5390 | + | |
| 5391 | + | |
| 5392 | + | |
| 5393 | + | |
| 5394 | + | |
| 5395 | + | |
| 5396 | + | |
| 5397 | + | |
| 5398 | + | |
| 5399 | + | |
| 5400 | + | |
5382 | 5401 | | |
5383 | 5402 | | |
5384 | 5403 | | |
| |||
0 commit comments