stevomitric commented on code in PR #58029:
URL: https://github.com/apache/spark/pull/58029#discussion_r3795551232
##########
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:
what happens at the boundary, i.e. `CAST(9223372036854.7758075BD AS
TIMESTAMP)`? truncation towards zero would produce exactly `Long.MaxValue` but
it will throw an exception here
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala:
##########
@@ -684,6 +685,10 @@ case class Cast(
with ToStringBase
with SupportQueryContext
with QueryErrorsBase {
+ private val MICROS_PER_SECOND_BD =
java.math.BigDecimal.valueOf(MICROS_PER_SECOND)
+ private val LONG_MAX_BD = java.math.BigDecimal.valueOf(Long.MaxValue)
+ private val LONG_MIN_BD = java.math.BigDecimal.valueOf(Long.MinValue)
Review Comment:
is this the best place for max/min value constants (not related to the cast
class)? I'm surprised these are not defined at some other place already
--
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]