MaxGekk opened a new pull request, #56641:
URL: https://github.com/apache/spark/pull/56641
### What changes were proposed in this pull request?
When a string cannot be parsed while casting to `TIME(p)` in ANSI mode, the
`CAST_INVALID_INPUT` error always reported the target type as `"TIME(6)"`,
regardless of the actual target precision. This happened because
`SparkDateTimeUtils.stringToTimeAnsi` hardcoded the default `TimeType()`
(precision 6) when constructing the error, and the cast's target precision
never reached the error builder.
This PR:
- Adds a precision-aware overload `stringToTimeAnsi(s, precision, context)`
in `SparkDateTimeUtils` that reports `TimeType(precision)` in the error. The
existing no-arg/default overload is kept (delegating to
`TimeType.DEFAULT_PRECISION`) for non-cast callers such as
`DefaultTimeFormatter.parse`.
- Updates `Cast.castToTime` (interpreted) and `Cast.castToTimeCode`
(codegen) to pass the cast's target `to.precision`.
This mirrors the existing nanosecond `TIMESTAMP` casts, which already pass
the real target type, e.g. `invalidInputInCastToDatetimeError(s,
TimestampLTZNanosType(precision), ...)`.
### Why are the changes needed?
The `CAST_INVALID_INPUT` error message was inaccurate for any TIME precision
other than 6. For example:
```sql
SET spark.sql.ansi.enabled=true;
SELECT CAST('not a time' AS TIME(2));
```
reported `targetType = "TIME(6)"` instead of the requested `"TIME(2)"`.
### Does this PR introduce _any_ user-facing change?
Yes. With ANSI mode enabled, `CAST('x' AS TIME(p))` on invalid input now
reports `targetType = "TIME(p)"` (the requested precision) instead of always
`"TIME(6)"`. This is a change only within the unreleased master/branch lines.
### How was this patch tested?
- Extended `DateTimeUtilsSuite."stringToTimeAnsi"` to assert the error
reports the requested precision across the supported range
`[TimeType.MIN_PRECISION, TimeType.MAX_PRECISION]`, in addition to the existing
default `TIME(6)` cases.
- Added `CastWithAnsiOnSuite."SPARK-57588: cast invalid string input to time
reports the target precision"`, which exercises both the interpreted and
codegen paths via the standard cast eval harness.
Ran `build/sbt "catalyst/testOnly *DateTimeUtilsSuite *CastWithAnsiOnSuite"`
(199 tests passed) and scalastyle on `sql-api` and `catalyst` (0 errors).
### Was this patch authored or co-authored using generative AI tooling?
Generated-by: Cursor (Claude Opus 4.8)
--
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]