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

    https://github.com/apache/spark/pull/21010#discussion_r180313214
  
    --- Diff: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/stringExpressions.scala
 ---
    @@ -2108,35 +2132,52 @@ case class FormatNumber(x: Expression, d: 
Expression)
           // SPARK-13515: US Locale configures the DecimalFormat object to use 
a dot ('.')
           // as a decimal separator.
           val usLocale = "US"
    -      val i = ctx.freshName("i")
           val dFormat = ctx.freshName("dFormat")
    -      val lastDValue =
    -        ctx.addMutableState(CodeGenerator.JAVA_INT, "lastDValue", v => 
s"$v = -100;")
           val pattern = ctx.addMutableState(sb, "pattern", v => s"$v = new 
$sb();")
           val numberFormat = ctx.addMutableState(df, "numberFormat",
             v => s"""$v = new $df("", new $dfs($l.$usLocale));""")
     
    -      s"""
    -        if ($d >= 0) {
    -          $pattern.delete(0, $pattern.length());
    -          if ($d != $lastDValue) {
    -            $pattern.append("#,###,###,###,###,###,##0");
    -
    -            if ($d > 0) {
    -              $pattern.append(".");
    -              for (int $i = 0; $i < $d; $i++) {
    -                $pattern.append("0");
    +      right.dataType match {
    +        case IntegerType =>
    +          val i = ctx.freshName("i")
    +          val lastDIntValue =
    +            ctx.addMutableState(CodeGenerator.JAVA_INT, "lastDValue", v => 
s"$v = -100;")
    +          s"""
    +            if ($d >= 0) {
    +              $pattern.delete(0, $pattern.length());
    +              if ($d != $lastDIntValue) {
    +                $pattern.append("$defaultFormat");
    +
    +                if ($d > 0) {
    +                  $pattern.append(".");
    +                  for (int $i = 0; $i < $d; $i++) {
    +                    $pattern.append("0");
    +                  }
    +                }
    +                $lastDIntValue = $d;
    +                $numberFormat.applyLocalizedPattern($pattern.toString());
                   }
    +              ${ev.value} = 
UTF8String.fromString($numberFormat.format(${typeHelper(num)}));
    +            } else {
    +              ${ev.value} = null;
    +              ${ev.isNull} = true;
                 }
    -            $lastDValue = $d;
    -            $numberFormat.applyLocalizedPattern($pattern.toString());
    -          }
    -          ${ev.value} = 
UTF8String.fromString($numberFormat.format(${typeHelper(num)}));
    -        } else {
    -          ${ev.value} = null;
    -          ${ev.isNull} = true;
    -        }
    -       """
    +           """
    +        case StringType =>
    +          val lastDStringValue =
    +            ctx.addMutableState("String", "lastDValue", v => s"""$v = 
"$defaultFormat";""")
    +          s"""
    +            if (!$d.toString().equals($lastDStringValue)) {
    --- End diff --
    
    We should only call `$d.toString()` once.


---

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

Reply via email to