EaswarKilari commented on PR #60212: URL: https://github.com/apache/spark/pull/60212#issuecomment-6719971427
> ## Review summary > This review compared each changed conversion with the base branch. `to_time` and `try_to_time` now truncate on all three parse routes (no format, constant format and per-row format). The DECIMAL and DOUBLE overloads of `time_from_seconds` truncate only after the range check, so `-0.0000001` is still rejected with an error that reports the original -100 nanoseconds. The new tests reach each of these routes. One inline comment covers avoidable work in the DECIMAL overload of `time_from_seconds`, which now truncates inputs with scale 6 or less even though their values are already microsecond-aligned; results are unaffected. A question about which microsecond DOUBLE inputs should land on follows. > > ## Findings > **1 total: 0 P0, 0 P1, 0 P2, 1 P3.** > > ### Nit (P3) > * **time_from_seconds truncates DECIMAL values that are already microsecond-aligned** — `sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/DateTimeUtils.scala:1181` — see inline. > > ## Decision challenges > ### time_from_seconds(8.2D) now returns 00:00:08.199999: truncate the DOUBLE product, or round it to the nearest nanosecond first? > `timeFromSeconds(Double)` still computes `(seconds * NANOS_PER_SECOND).toLong`, which floors the inexact floating-point product at nanoseconds, and the new `truncateTimeToPrecision` call then floors again at microseconds. When the product for a double written with six fractional digits lands just below the microsecond boundary, the second floor turns a sub-nanosecond representation error into a whole lost microsecond: > > * `time_from_seconds(CAST(8.2 AS DOUBLE))` was 8199999999 ns (`00:00:08.199999999`) on the base branch and is now `00:00:08.199999`. `time_to_seconds` of the result was `8.200000`, because it rounds the extra digits back up, and is now `8.199999`. > * Likewise, `1.000001D` now gives `00:00:01`, `17548.741022D` gives `04:52:28.741021`, and `0.000065D` gives `00:00:00.000064`. > > In an IEEE-754 model of this code over 200,000 random doubles written with six fractional digits, about 2.6% land one microsecond low. The rest return the written microsecond, and the PR's own DOUBLE tests (`1e-7` and `52200.1234567`) are unaffected. Equality with the matching `TIME` literal was already false for the affected inputs before this PR, so what changes for them is the displayed and cast value and the `time_to_seconds` round trip. For every DOUBLE input, the PR still removes the sub-microsecond digits it targets. > > There seem to be two reasonable options: > > 1. Keep truncating the floating-point product. This stays close to `timestamp_seconds`, which computes `(d * MICROS_PER_SECOND).toLong` and lands low for about 1.8% of the same inputs, 8.2D included. The two-step floor here is lower than `timestamp_seconds` for about 0.9% of them, for example `17548.741022D` and `0.000065D`. Neither function truncates the double's exact value, which would be low for about half of these inputs. > 2. Round the product to the nearest nanosecond, for example with `Math.round`, before truncating. In the same model this returns the written microsecond for every input, still rejects `-0.0000001`, and keeps the new tests passing, but it departs from `timestamp_seconds` for inputs such as 8.2D. If the range check then sees the rounded value, inputs within half a nanosecond below 86400, such as `86399.99999999999D` (now `23:59:59.999999`), would start failing as out of range. > > Which behavior do you intend for DOUBLE input? Either way, a test with an exact-microsecond double such as 8.2D would pin it down. > > _Generated by Omnigent on Databricks_ > Which behavior do you intend for DOUBLE input? Neither of the two options, in the end. The latest commit converts the `DOUBLE` through its shortest decimal form, the same digits `CAST(double AS DECIMAL)` produces, and then floors to microseconds. So `time_from_seconds(d)` now matches `time_from_seconds(CAST(d AS DECIMAL))`. - `8.2D`, `1.000001D`, `0.000065D` and `17548.741022D` return the written microsecond. - `-0.0000001` is still rejected. - `86399.99999999999D` stays valid as `23:59:59.999999`. That's the boundary case that rounding to the nearest nanosecond would break. Like option 2, this differs from `timestamp_seconds` for inputs such as `8.2D`. I left `timestamp_seconds` unchanged since it's outside this JIRA and changing it would change released behavior. I added tests for `8.2D`, `1.000001D`, `3.0E-6` and `86399.99999999999D`. -- 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]
