Skip to content

Commit

Permalink
Add first dValue is the same as the default format test.
Browse files Browse the repository at this point in the history
  • Loading branch information
wangyum committed May 6, 2018
1 parent e273045 commit 0bc77e8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2140,12 +2140,12 @@ case class FormatNumber(x: Expression, d: Expression)
case IntegerType =>
val pattern = ctx.addMutableState(sb, "pattern", v => s"$v = new $sb();")
val i = ctx.freshName("i")
val lastDIntValue =
val lastDValue =
ctx.addMutableState(CodeGenerator.JAVA_INT, "lastDValue", v => s"$v = -100;")
s"""
if ($d >= 0) {
$pattern.delete(0, $pattern.length());
if ($d != $lastDIntValue) {
if ($d != $lastDValue) {
$pattern.append("$defaultFormat");

if ($d > 0) {
Expand All @@ -2154,7 +2154,7 @@ case class FormatNumber(x: Expression, d: Expression)
$pattern.append("0");
}
}
$lastDIntValue = $d;
$lastDValue = $d;
$numberFormat.applyLocalizedPattern($pattern.toString());
}
${ev.value} = UTF8String.fromString($numberFormat.format(${typeHelper(num)}));
Expand All @@ -2164,13 +2164,12 @@ case class FormatNumber(x: Expression, d: Expression)
}
"""
case StringType =>
val lastDStringValue =
ctx.addMutableState("String", "lastDValue", v => s"""$v = "$defaultFormat";""")
val lastDValue = ctx.addMutableState("String", "lastDValue", v => s"""$v = null;""")
val dValue = ctx.addMutableState("String", "dValue")
s"""
$dValue = $d.toString();
if (!$dValue.equals($lastDStringValue)) {
$lastDStringValue = $dValue;
if (!$dValue.equals($lastDValue)) {
$lastDValue = $dValue;
if ($dValue.isEmpty()) {
$numberFormat.applyLocalizedPattern("$defaultFormat");
} else {
Expand All @@ -2179,6 +2178,11 @@ case class FormatNumber(x: Expression, d: Expression)
}
${ev.value} = UTF8String.fromString($numberFormat.format(${typeHelper(num)}));
"""
case NullType =>
s"""
${ev.value} = null;
${ev.isNull} = true;
"""
}
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,13 @@ class StringExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
Literal("###,###,###,###,###.####")), "15,159,339,180,002,773.2778")
checkEvaluation(FormatNumber(Literal.create(null, IntegerType), Literal("##.###")), null)
assert(FormatNumber(Literal.create(null, NullType), Literal("##.###")).resolved === false)

checkEvaluation(FormatNumber(Literal(12332.123456), Literal("#,###,###,###,###,###,##0")),
"12,332")
checkEvaluation(FormatNumber(
Literal.create(null, IntegerType), Literal.create(null, NullType)), null)
checkEvaluation(FormatNumber(
Literal.create(null, NullType), Literal.create(null, NullType)), null)
}

test("find in set") {
Expand Down

0 comments on commit 0bc77e8

Please sign in to comment.