From 0bc77e87689edbe252e23d322b1a64d317b19c67 Mon Sep 17 00:00:00 2001 From: Yuming Wang Date: Sun, 6 May 2018 10:10:21 +0800 Subject: [PATCH] Add first dValue is the same as the default format test. --- .../expressions/stringExpressions.scala | 18 +++++++++++------- .../expressions/StringExpressionsSuite.scala | 7 +++++++ 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/stringExpressions.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/stringExpressions.scala index c97c5b3c5735d..230c0a7591904 100755 --- a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/stringExpressions.scala +++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/stringExpressions.scala @@ -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) { @@ -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)})); @@ -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 { @@ -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; + """ } }) } diff --git a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/StringExpressionsSuite.scala b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/StringExpressionsSuite.scala index b9a6a3f58f54a..b782b4879fe1f 100644 --- a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/StringExpressionsSuite.scala +++ b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/StringExpressionsSuite.scala @@ -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") {