EaswarKilari opened a new pull request, #60212:
URL: https://github.com/apache/spark/pull/60212
### What changes were proposed in this pull request?
This PR truncates the results of `to_time`, `try_to_time` and
`time_from_seconds` to the precision of their result type, `TIME(6)`, with
`DateTimeUtils.truncateTimeToPrecision`, the same helper that `CAST` uses.
- `to_time` / `try_to_time`: `ToTimeParser.parse` truncates the parsed
value, with and without a format.
- `time_from_seconds`: the `DECIMAL` and `DOUBLE` overloads of
`DateTimeUtils.timeFromSeconds` truncate after the range check, so a small
negative input like `-0.0000001` is still rejected instead of becoming
`00:00:00`.
### Why are the changes needed?
These functions return `TIME(6)`, but the value could keep up to 9
fractional digits. SPARK-57551 made the default time parser keep digits 7 to 9
and added truncation to `CAST`, but not to `to_time`, and `time_from_seconds`
never truncated. For example:
```sql
SET spark.sql.timeType.enabled=true;
SELECT to_time('00:00:00.1234567') = TIME'00:00:00.123456'; -- false
SELECT time_from_seconds(0.0000001) = TIME'00:00:00'; -- false
```
The result of a query could also change depending on
`UnwrapCastInBinaryComparison`, or after writing the value to Parquet and
reading it back.
### Does this PR introduce _any_ user-facing change?
Yes. These functions now return at most 6 fractional digits. For example,
`to_time('00:00:00.1234567')` returns `00:00:00.123456` instead of
`00:00:00.1234567`, and `time_from_seconds(0.0000001)` returns `00:00:00`. The
TIME type is a preview feature behind `spark.sql.timeType.enabled`.
### How was this patch tested?
Added tests to `TimeExpressionsSuite` and new queries to `time.sql` (golden
files regenerated). The new tests fail without the fix. I also ran
`DateTimeUtilsSuite`, `CastWithAnsiOnSuite`, `CastWithAnsiOffSuite`,
`TimeFunctionsAnsiOnSuite`, `TimeFunctionsAnsiOffSuite`, `ExpressionInfoSuite`,
and the `datetime-parsing*.sql` and `try_datetime_functions.sql` golden files
locally.
### Was this patch authored or co-authored using generative AI tooling?
Generated-by: GitHub Copilot
--
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]