Github user viirya commented on a diff in the pull request:

    https://github.com/apache/spark/pull/21010#discussion_r180311991
  
    --- Diff: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/stringExpressions.scala
 ---
    @@ -2050,33 +2058,49 @@ case class FormatNumber(x: Expression, d: 
Expression)
       private lazy val numberFormat = new DecimalFormat("", new 
DecimalFormatSymbols(Locale.US))
     
       override protected def nullSafeEval(xObject: Any, dObject: Any): Any = {
    -    val dValue = dObject.asInstanceOf[Int]
    -    if (dValue < 0) {
    -      return null
    -    }
    -
    -    lastDValue match {
    -      case Some(last) if last == dValue =>
    -        // use the current pattern
    -      case _ =>
    -        // construct a new DecimalFormat only if a new dValue
    -        pattern.delete(0, pattern.length)
    -        pattern.append("#,###,###,###,###,###,##0")
    -
    -        // decimal place
    -        if (dValue > 0) {
    -          pattern.append(".")
    -
    -          var i = 0
    -          while (i < dValue) {
    -            i += 1
    -            pattern.append("0")
    -          }
    +    right.dataType match {
    +      case IntegerType =>
    +        val dValue = dObject.asInstanceOf[Int]
    +        if (dValue < 0) {
    +          return null
             }
     
    -        lastDValue = Some(dValue)
    +        lastDIntValue match {
    +          case Some(last) if last == dValue =>
    +          // use the current pattern
    +          case _ =>
    +            // construct a new DecimalFormat only if a new dValue
    +            pattern.delete(0, pattern.length)
    +            pattern.append(defaultFormat)
    +
    +            // decimal place
    +            if (dValue > 0) {
    +              pattern.append(".")
    +
    +              var i = 0
    +              while (i < dValue) {
    +                i += 1
    +                pattern.append("0")
    +              }
    +            }
    +
    +            lastDIntValue = Some(dValue)
     
    -        numberFormat.applyLocalizedPattern(pattern.toString)
    +            numberFormat.applyLocalizedPattern(pattern.toString)
    +        }
    +      case StringType =>
    +        val dValue = dObject.asInstanceOf[UTF8String].toString
    +        lastDStringValue match {
    +          case Some(last) if last == dValue =>
    +          case _ =>
    +            pattern.delete(0, pattern.length)
    +            lastDStringValue = Some(dValue)
    +            if (dValue.toString.isEmpty) {
    --- End diff --
    
    `dValue` is already a string.


---

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

Reply via email to