joelrobin18 opened a new pull request, #60218:
URL: https://github.com/apache/spark/pull/60218
### What changes were proposed in this pull request?
This PR makes `to_time`, `try_to_time` and `time_from_seconds` return values
that fit their result type, `TIME(6)`.
- `ToTimeParser.parse` now truncates the parsed value to
`TimeType.DEFAULT_PRECISION` with `DateTimeUtils.truncateTimeToPrecision`.
`CAST` uses the same function. `try_to_time` is `TryEval(ToTime(...))`, thus it
gets the same fix.
- `DateTimeUtils.timeFromSeconds(Decimal)` and
`DateTimeUtils.timeFromSeconds(Double)` now floor the seconds to whole
microseconds. They floor, and do not truncate toward zero, so a negative input
below one microsecond stays out of range and fails as before.
- For a `DOUBLE` input, the conversion uses the shortest decimal form of the
value, as `CAST(double AS DECIMAL)` does. Without this, `3.0E-6` becomes
`2.9999...E-6` after the multiplication, and 2 microseconds after the floor.
### Why are the changes needed?
A `TIME(p)` value must not have more than `p` fractional digits. `CAST`,
`TIME` literals and the Parquet reader obey this rule. The optimizer rule
`UnwrapCastInBinaryComparison` also expects it.
These three functions return `TIME(6)`, but their values can have 7 to 9
fractional digits:
```sql
SET spark.sql.timeType.enabled=true;
SELECT to_time('00:00:00.1234567'); -- 00:00:00.1234567
SELECT try_to_time('00:00:00.123456789'); -- 00:00:00.123456789
SELECT time_from_seconds(0.0000001); -- 00:00:00.0000001
SELECT to_time('00:00:00.1234567') = TIME'00:00:00.123456'; -- false
SELECT time_from_seconds(0.0000001) AS v UNION SELECT TIME'00:00:00'; -- 2
rows
```
Because of this, equal `TIME(6)` values compare as different, `UNION` and
`DISTINCT` do not remove duplicates, and a write to Parquet changes the value.
The result of a comparison can also change when `UnwrapCastInBinaryComparison`
is excluded.
SPARK-57551 changed `stringToTime` to keep the digits 7 to 9. It added
truncation to `CAST`, but not to `to_time`. `time_from_seconds` did not
truncate since SPARK-54442.
### Does this PR introduce _any_ user-facing change?
Yes, when `spark.sql.timeType.enabled` is `true`. The `TIME` type is a
preview feature, and this config is `false` by default.
- `to_time('00:00:00.1234567')` returns `00:00:00.123456`. Before, it
returned `00:00:00.1234567`.
- `try_to_time('00:00:00.123456789')` returns `00:00:00.123456`. Before, it
returned `00:00:00.123456789`.
- `time_from_seconds(0.0000001)` returns `00:00:00`. Before, it returned
`00:00:00.0000001`.
- `time_from_seconds` with a `DECIMAL` that has more than 9 fractional
digits, for example `1.0000000001`, returns a value. Before, it failed with an
overflow error.
- For a negative input below one microsecond, the error stays the same. Only
the number of nanoseconds in the message changes, for example `-1000` instead
of `-100`.
### How was this patch tested?
- Added a test to `TimeExpressionsSuite`. It covers constant and column
inputs, a format with 9 fractional digits, `try_to_time`, and
`time_from_seconds` with `DECIMAL`, `DOUBLE` and negative inputs.
- Added queries to `time.sql`, and regenerated `time.sql.out` in `results`
and `analyzer-results`. The output of the existing queries did not change.
- Ran `TimeExpressionsSuite`, `DateTimeUtilsSuite` and `SQLQueryTestSuite`
for `time.sql`.
```
build/sbt "catalyst/testOnly *TimeExpressionsSuite *DateTimeUtilsSuite"
build/sbt "sql/testOnly *SQLQueryTestSuite -- -z time.sql"
```
### Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Opus 5.5
--
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]