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


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/DateTimeUtils.scala:
##########
@@ -79,6 +83,29 @@ object DateTimeUtils extends SparkDateTimeUtils {
     }
   }
 
+  /**
+   * Converts seconds stored as a decimal to microseconds since the epoch. 
Sub-microsecond digits
+   * are truncated towards zero, and an [[ArithmeticException]] is thrown if 
the truncated result
+   * does not fit in a Long.
+   */
+  def decimalToTimestamp(d: Decimal): Long = {
+    if (d.isZero) {
+      0L
+    } else {
+      val value = d.toJavaBigDecimal
+      val microsDigits = value.precision().toLong - value.scale().toLong + 
microsPerSecondDigits
+      if (microsDigits <= 0) {
+        0L
+      } else if (microsDigits < maxLongDigits) {
+        value.multiply(microsPerSecondBD).longValue()
+      } else if (microsDigits > maxLongDigits) {
+        throw new ArithmeticException("overflow")

Review Comment:
   **Nit (P3):** Every overflowing row now pays for a stack-trace-filled 
exception that each caller immediately discards. Legacy `CAST` catches it to 
return NULL. `TRY_CAST` evaluates decimal-to-timestamp with ANSI semantics, 
because `canUseLegacyCastForTryCast` only covers string sources, so it also 
builds a formatted `CAST_OVERFLOW` exception per row that its try/catch wrapper 
throws away. The base branch did no exception work on these paths, and the NULL 
result needs only the digit-count comparison this method already makes. 
Overflow-heavy inputs, such as epoch microseconds stored as decimal seconds or 
dirty data under `TRY_CAST`, therefore pay for exception construction on every 
row with no change in results. In-range rows and ANSI `CAST` are unaffected.
   
   A same-result fix: add `(DecimalType, TimestampType)` to 
`canUseLegacyCastForTryCast`, so `TRY_CAST` takes the legacy NULL path without 
the wrapper, and signal overflow here with an exception that skips stack-trace 
capture, such as `new ArithmeticException("overflow") with 
scala.util.control.NoStackTrace`. Use it both for more than 19 digits and, 
after comparing the truncated value against the Long bounds, in the 19-digit 
band instead of relying on `longValueExact`.
   
   **Verification:**
   - **Behavior:** Overflowing decimals, including the fractional Long 
boundaries and extreme legacy negative scales, still yield NULL under legacy 
CAST and TRY_CAST and CAST_OVERFLOW under ANSI CAST, in both interpreted and 
generated evaluation.
   - **Compatibility:** For overflowing decimal Variant values, including 
values read through shredded Parquet, variant_get still raises 
INVALID_VARIANT_CAST and try_variant_get still returns NULL.
   - **Inspection:** Overflow, beyond 19 digits and in the 19-digit band, is 
signalled without stack-trace construction, and TRY_CAST of DECIMAL to 
TIMESTAMP evaluates through the legacy path, without the try/catch wrapper or a 
CAST_OVERFLOW error.



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