Github user cloud-fan commented on a diff in the pull request:

    https://github.com/apache/spark/pull/17075#discussion_r103588721
  
    --- Diff: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala
 ---
    @@ -339,36 +339,34 @@ case class Cast(child: Expression, dataType: 
DataType, timeZoneId: Option[String
       }
     
       /**
    -   * Change the precision / scale in a given decimal to those set in 
`decimalType` (if any),
    -   * returning null if it overflows or modifying `value` in-place and 
returning it if successful.
    +   * Create new `Decimal` with precision and scale given in `decimalType` 
(if any),
    +   * returning null if it overflows or creating a new `value` and 
returning it if successful.
        *
    -   * NOTE: this modifies `value` in-place, so don't call it on external 
data.
        */
    -  private[this] def changePrecision(value: Decimal, decimalType: 
DecimalType): Decimal = {
    -    if (value.changePrecision(decimalType.precision, decimalType.scale)) 
value else null
    -  }
    +  private[this] def toPrecision(value: Decimal, decimalType: DecimalType): 
Decimal =
    +    value.toPrecision(decimalType.precision, decimalType.scale).orNull
     
       private[this] def castToDecimal(from: DataType, target: DecimalType): 
Any => Any = from match {
         case StringType =>
           buildCast[UTF8String](_, s => try {
    -        changePrecision(Decimal(new JavaBigDecimal(s.toString)), target)
    +        toPrecision(Decimal(new JavaBigDecimal(s.toString)), target)
           } catch {
             case _: NumberFormatException => null
           })
         case BooleanType =>
    -      buildCast[Boolean](_, b => changePrecision(if (b) Decimal.ONE else 
Decimal.ZERO, target))
    +      buildCast[Boolean](_, b => toPrecision(if (b) Decimal.ONE else 
Decimal.ZERO, target))
         case DateType =>
           buildCast[Int](_, d => null) // date can't cast to decimal in Hive
         case TimestampType =>
           // Note that we lose precision here.
    -      buildCast[Long](_, t => 
changePrecision(Decimal(timestampToDouble(t)), target))
    +      buildCast[Long](_, t => toPrecision(Decimal(timestampToDouble(t)), 
target))
         case dt: DecimalType =>
    -      b => changePrecision(b.asInstanceOf[Decimal].clone(), target)
    --- End diff --
    
    I think this is the only case we need `toPewcision`


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to