linhongliu-db opened a new pull request, #59806:
URL: https://github.com/apache/spark/pull/59806
### What changes were proposed in this pull request?
Fix `date_add` and `date_sub` to preserve a foldable NULL string day count
during
type coercion. Keep the cast result boxed and construct an INT literal with
`Literal.create`, so NULL does not become zero.
### Why are the changes needed?
With ANSI mode disabled, string day counts are evaluated during analysis.
Unboxing
a NULL cast result with `asInstanceOf[Int]` produces zero, causing these
functions
to return the input date instead of NULL.
### Does this PR introduce _any_ user-facing change?
Yes, a bug fix. For example:
```sql
SET spark.sql.ansi.enabled=false;
SELECT date_add(DATE '2020-01-01', CAST(NULL AS STRING)),
date_sub(DATE '2020-01-01', CAST(NULL AS STRING));
```
Previously this returned `2020-01-01, 2020-01-01`; it now returns `NULL,
NULL`.
Valid non-null string day counts and invalid-string errors are unchanged.
### How was this patch tested?
Added coercion and SQL regression coverage for direct NULL strings, foldable
conditional/cast expressions, ANSI mode on and off, valid string day counts,
and
invalid-string errors. The new coercion test fails without the fix because
the
day-count literal is `0` instead of NULL.
Passed on JDK 17:
```bash
build/sbt 'catalyst/testOnly
org.apache.spark.sql.catalyst.analysis.TypeCoercionSuite'
build/sbt 'sql/testOnly org.apache.spark.sql.DateFunctionsSuite -- -z "date
arithmetic" -z "function date_add" -z "function date_sub"'
```
The commands passed 63 Catalyst tests and five SQL tests. Catalyst main/test
and
SQL test Scalastyle checks also passed, along with `git diff --check`.
### Was this patch authored or co-authored using generative AI tooling?
Generated-by: OpenAI Codex (GPT-6)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]