jiangxt2 commented on code in PR #58029:
URL: https://github.com/apache/spark/pull/58029#discussion_r3797009135


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala:
##########
@@ -1014,8 +1019,13 @@ case class Cast(
         DateTimeUtils.makeTimestampNTZNanos(currentDate(zoneId), nanos, 
precision))
   }
 
-  private[this] def decimalToTimestamp(d: Decimal): Long = {
-    (d.toBigDecimal * MICROS_PER_SECOND).longValue
+  private[this] def decimalToTimestamp(d: Decimal): Any = {
+    val result = d.toJavaBigDecimal.multiply(MICROS_PER_SECOND_BD)
+    if (result.compareTo(LONG_MAX_BD) > 0 || result.compareTo(LONG_MIN_BD) < 
0) {

Review Comment:
   I changed the conversion order to scale the decimal value to microseconds, 
truncate toward zero with toBigInteger, and only then call longValueExact() for 
the range check. As a result, `9223372036854.7758075BD` converts to 
Long.MaxValue microseconds, and the corresponding negative value converts to 
Long.MinValue. Only values whose truncated result is actually out of range now 
return NULL or raise CAST_OVERFLOW. I also added positive and negative boundary 
tests covering both interpreted and codegen execution.



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