dwsmith1983 commented on code in PR #59159:
URL: https://github.com/apache/spark/pull/59159#discussion_r4640776117
##########
sql/api/src/main/scala/org/apache/spark/sql/types/Decimal.scala:
##########
@@ -264,63 +270,57 @@ final class Decimal extends Ordered[Decimal] with
Serializable {
/**
* @return
- * the Byte value that is equal to the rounded decimal.
+ * the Byte value of the decimal truncated toward zero.
* @throws ArithmeticException
* if the decimal is too big to fit in Byte type.
*/
private[sql] def roundToByte(): Byte =
- roundToNumeric[Byte](ByteType, Byte.MaxValue,
Byte.MinValue)(_.toByte)(_.toByte)
+ roundToNumeric[Byte](ByteType)(_.toByte)
/**
* @return
- * the Short value that is equal to the rounded decimal.
+ * the Short value of the decimal truncated toward zero.
* @throws ArithmeticException
* if the decimal is too big to fit in Short type.
*/
private[sql] def roundToShort(): Short =
- roundToNumeric[Short](ShortType, Short.MaxValue,
Short.MinValue)(_.toShort)(_.toShort)
+ roundToNumeric[Short](ShortType)(_.toShort)
/**
* @return
- * the Int value that is equal to the rounded decimal.
+ * the Int value of the decimal truncated toward zero.
* @throws ArithmeticException
* if the decimal too big to fit in Int type.
*/
private[sql] def roundToInt(): Int =
- roundToNumeric[Int](IntegerType, Int.MaxValue,
Int.MinValue)(_.toInt)(_.toInt)
+ roundToNumeric[Int](IntegerType)(_.toInt)
private def toSqlValue: String = this.toString + "BD"
- private def roundToNumeric[T <: AnyVal](
- integralType: IntegralType,
- maxValue: Int,
- minValue: Int)(f1: Long => T)(f2: Double => T): T = {
- if (decimalVal.eq(null)) {
- val numericVal = f1(actualLongVal)
- if (actualLongVal == numericVal) {
- numericVal
- } else {
- throw DataTypeErrors.castingCauseOverflowError(
- toSqlValue,
- DecimalType(this.precision, this.scale),
- integralType)
- }
+ private def roundToNumeric[T <: AnyVal](integralType: IntegralType)(f: Long
=> T): T = {
+ val integralVal = if (decimalVal.eq(null)) {
+ actualLongVal
Review Comment:
Good catch, thanks. It's on master too, since `toLong` goes through the same
`actualLongVal`, so I opened SPARK-62122 to fix it on its own: #60751.
--
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]