Skip to content

Commit 0bc77e8

Browse files
committed
Add first dValue is the same as the default format test.
1 parent e273045 commit 0bc77e8

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/stringExpressions.scala

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2140,12 +2140,12 @@ case class FormatNumber(x: Expression, d: Expression)
21402140
case IntegerType =>
21412141
val pattern = ctx.addMutableState(sb, "pattern", v => s"$v = new $sb();")
21422142
val i = ctx.freshName("i")
2143-
val lastDIntValue =
2143+
val lastDValue =
21442144
ctx.addMutableState(CodeGenerator.JAVA_INT, "lastDValue", v => s"$v = -100;")
21452145
s"""
21462146
if ($d >= 0) {
21472147
$pattern.delete(0, $pattern.length());
2148-
if ($d != $lastDIntValue) {
2148+
if ($d != $lastDValue) {
21492149
$pattern.append("$defaultFormat");
21502150

21512151
if ($d > 0) {
@@ -2154,7 +2154,7 @@ case class FormatNumber(x: Expression, d: Expression)
21542154
$pattern.append("0");
21552155
}
21562156
}
2157-
$lastDIntValue = $d;
2157+
$lastDValue = $d;
21582158
$numberFormat.applyLocalizedPattern($pattern.toString());
21592159
}
21602160
${ev.value} = UTF8String.fromString($numberFormat.format(${typeHelper(num)}));
@@ -2164,13 +2164,12 @@ case class FormatNumber(x: Expression, d: Expression)
21642164
}
21652165
"""
21662166
case StringType =>
2167-
val lastDStringValue =
2168-
ctx.addMutableState("String", "lastDValue", v => s"""$v = "$defaultFormat";""")
2167+
val lastDValue = ctx.addMutableState("String", "lastDValue", v => s"""$v = null;""")
21692168
val dValue = ctx.addMutableState("String", "dValue")
21702169
s"""
21712170
$dValue = $d.toString();
2172-
if (!$dValue.equals($lastDStringValue)) {
2173-
$lastDStringValue = $dValue;
2171+
if (!$dValue.equals($lastDValue)) {
2172+
$lastDValue = $dValue;
21742173
if ($dValue.isEmpty()) {
21752174
$numberFormat.applyLocalizedPattern("$defaultFormat");
21762175
} else {
@@ -2179,6 +2178,11 @@ case class FormatNumber(x: Expression, d: Expression)
21792178
}
21802179
${ev.value} = UTF8String.fromString($numberFormat.format(${typeHelper(num)}));
21812180
"""
2181+
case NullType =>
2182+
s"""
2183+
${ev.value} = null;
2184+
${ev.isNull} = true;
2185+
"""
21822186
}
21832187
})
21842188
}

sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/StringExpressionsSuite.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,13 @@ class StringExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
723723
Literal("###,###,###,###,###.####")), "15,159,339,180,002,773.2778")
724724
checkEvaluation(FormatNumber(Literal.create(null, IntegerType), Literal("##.###")), null)
725725
assert(FormatNumber(Literal.create(null, NullType), Literal("##.###")).resolved === false)
726+
727+
checkEvaluation(FormatNumber(Literal(12332.123456), Literal("#,###,###,###,###,###,##0")),
728+
"12,332")
729+
checkEvaluation(FormatNumber(
730+
Literal.create(null, IntegerType), Literal.create(null, NullType)), null)
731+
checkEvaluation(FormatNumber(
732+
Literal.create(null, NullType), Literal.create(null, NullType)), null)
726733
}
727734

728735
test("find in set") {

0 commit comments

Comments
 (0)