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

    https://github.com/apache/spark/pull/21010#discussion_r185691861
  
    --- Diff: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/stringExpressions.scala
 ---
    @@ -2108,35 +2133,53 @@ 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 pattern = ctx.addMutableState(sb, "pattern", v => s"$v = new 
$sb();")
    +          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";""")
    +          val dValue = ctx.addMutableState("String", "dValue")
    +          s"""
    +            $dValue = $d.toString();
    +            if (!$dValue.equals($lastDStringValue)) {
    --- End diff --
    
    What if the first `dValue` is the same as the default format? Can you add 
the test case?


---

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

Reply via email to