spark-test commented on code in PR #60212:
URL: https://github.com/apache/spark/pull/60212#discussion_r4504396871


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/DateTimeUtils.scala:
##########
@@ -1171,23 +1171,29 @@ object DateTimeUtils extends SparkDateTimeUtils {
   /**
    * Creates a TIME value from seconds since midnight (decimal type).
    * @param seconds Seconds (0 to 86399.999999)
-   * @return Nanoseconds since midnight
+   * @return Nanoseconds since midnight, truncated to microsecond precision
    */
-  def timeFromSeconds(seconds: Decimal): Long = 
withTimeConversionErrorHandling {
-    val operand = new java.math.BigDecimal(NANOS_PER_SECOND)
-    seconds.toJavaBigDecimal.multiply(operand).longValueExact()
+  def timeFromSeconds(seconds: Decimal): Long = {
+    val nanos = withTimeConversionErrorHandling {
+      val operand = new java.math.BigDecimal(NANOS_PER_SECOND)
+      seconds.toJavaBigDecimal.multiply(operand).longValueExact()
+    }
+    truncateTimeToPrecision(nanos, TimeType.MICROS_PRECISION)

Review Comment:
   **Nit (P3):** For DECIMAL input with scale 6 or less, this call can never 
change the result. The exact product `unscaled * 10^(9 - scale)` is already a 
multiple of 1000 ns, so these inputs already produced valid `TIME(6)` values 
before this PR; only a scale above `TimeType.MICROS_PRECISION` can carry 
sub-microsecond digits. Every non-null row of such a column, for example 
`DECIMAL(11,6)`, which matches the documented 0 to 86399.999999 range, now also 
pays for the helper's assert, its factor-table lookup, and a 64-bit divide and 
multiply, and then gets its input back unchanged. The cost is small (a few 
nanoseconds per row, and literal inputs are constant-folded once), but the fix 
does not need it for these inputs: skipping the call when `seconds.scale <= 
TimeType.MICROS_PRECISION` gives identical results for every input, including 
the scale-7 and scale-9 cases in the new tests. The DOUBLE overload and 
`to_time` do need their truncation, because there alignment is only known after 
t
 he value is computed. `CAST` to `TIME` also truncates already-aligned sources 
unconditionally; the difference here is that the decimal's scale makes the 
no-op case cheap to detect.
   
   **Verification:**
   - **Behavior:** time_from_seconds over DECIMAL inputs with six or fewer 
fractional digits (for example 52200.5, 86399.999999 and a whole-second DECIMAL 
value) returns the same nanoseconds as before, when evaluated interpreted, 
through generated code and after constant folding.
   - **Regression:** DECIMAL inputs with 7 to 9 fractional digits still 
truncate: 0.0000001 returns 00:00:00 and 52200.123456789 returns 
14:30:00.123456. -0.0000001 still fails with the out-of-range TIME error that 
reports -100 nanoseconds.
   - **Inspection:** The DECIMAL conversion path calls the microsecond 
truncation only when the decimal's scale exceeds six fractional digits, and 
that call still follows the range check.



-- 
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]

Reply via email to