cloud-fan commented on a change in pull request #35060:
URL: https://github.com/apache/spark/pull/35060#discussion_r786120191
##########
File path:
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/StringExpressionsSuite.scala
##########
@@ -888,6 +889,179 @@ class StringExpressionsSuite extends SparkFunSuite with
ExpressionEvalHelper {
Literal.create(null, IntegerType), Literal.create(null, IntegerType)),
null)
}
+ test("ToNumber") {
+ ToNumber(Literal("454"), Literal("")).checkInputDataTypes() match {
+ case TypeCheckResult.TypeCheckFailure(msg) =>
+ assert(msg.contains("Number format cannot be empty"))
+ }
+ ToNumber(Literal("454"), NonFoldableLiteral.create("999", StringType))
+ .checkInputDataTypes() match {
+ case TypeCheckResult.TypeCheckFailure(msg) =>
+ assert(msg.contains("Format expression must be foldable"))
+ }
+
+ // Test '0' and '9'
+
+ Seq("454", "054", "54", "450").foreach { input =>
+ val invalidFormat1 = 0.until(input.length - 1).map(_ => '0').mkString
+ val invalidFormat2 = 0.until(input.length - 2).map(_ => '0').mkString
+ val invalidFormat3 = 0.until(input.length - 1).map(_ => '9').mkString
+ val invalidFormat4 = 0.until(input.length - 2).map(_ => '9').mkString
+ Seq(invalidFormat1, invalidFormat2, invalidFormat3, invalidFormat4)
+ .filter(_.nonEmpty).foreach { format =>
+ checkExceptionInExpression[IllegalArgumentException](
+ ToNumber(Literal("454"), Literal(format)), s"Format '$format' used
for" +
+ " parsing string to number or formatting number to string is
invalid")
+ }
+
+ val format1 = 0.until(input.length).map(_ => '0').mkString
+ val format2 = 0.until(input.length).map(_ => '9').mkString
+ val format3 = 0.until(input.length + 1).map(_ => '0').mkString
+ val format4 = 0.until(input.length + 1).map(_ => '9').mkString
+ val format5 = 0.until(input.length + 2).map(_ => '0').mkString
+ val format6 = 0.until(input.length + 2).map(_ => '9').mkString
+ Seq(format1, format2, format3, format4, format5, format6).foreach {
format =>
+ checkEvaluation(ToNumber(Literal(input), Literal(format)),
Decimal(input))
+ }
+ }
+
+ // Test '.' and 'D'
+ checkExceptionInExpression[IllegalArgumentException](
+ ToNumber(Literal("454.2"), Literal("999")),
+ "Format '999' used for parsing string to number or formatting number to
string is invalid")
+ checkExceptionInExpression[IllegalArgumentException](
+ ToNumber(Literal("454.23"), Literal("999.9")),
+ "Format '999.9' used for parsing string to number or formatting number
to string is invalid")
+ checkExceptionInExpression[IllegalArgumentException](
+ ToNumber(Literal("454.23"), Literal("000.0")),
+ "Format '000.0' used for parsing string to number or formatting number
to string is invalid")
+ checkExceptionInExpression[IllegalArgumentException](
+ ToNumber(Literal("454.23"), Literal("000D0")),
+ "Format '000D0' used for parsing string to number or formatting number
to string is invalid")
+ checkExceptionInExpression[IllegalArgumentException](
+ ToNumber(Literal("454.23"), Literal("00.00")),
+ "Format '00.00' used for parsing string to number or formatting number
to string is invalid")
+ checkExceptionInExpression[IllegalArgumentException](
+ ToNumber(Literal("454.23"), Literal("00D00")),
+ "Format '00D00' used for parsing string to number or formatting number
to string is invalid")
+ checkExceptionInExpression[IllegalArgumentException](
+ ToNumber(Literal("454.23"), Literal("0000.0")),
+ "Format '0000.0' used for parsing string to number or formatting number
to string is invalid")
+ checkExceptionInExpression[IllegalArgumentException](
+ ToNumber(Literal("454.23"), Literal("0000D0")),
+ "Format '0000D0' used for parsing string to number or formatting number
to string is invalid")
+ checkExceptionInExpression[IllegalArgumentException](
+ ToNumber(Literal("454.23"), Literal("00.000")),
+ "Format '00.000' used for parsing string to number or formatting number
to string is invalid")
+ checkExceptionInExpression[IllegalArgumentException](
+ ToNumber(Literal("454.23"), Literal("00D000")),
+ "Format '00D000' used for parsing string to number or formatting number
to string is invalid")
+
+ Seq(
+ ("454.2", "000.0") -> Decimal(454.2),
+ ("454.2", "000D0") -> Decimal(454.2),
+ ("454.23", "000.00") -> Decimal(454.23),
+ ("454.23", "000D00") -> Decimal(454.23),
+ ("454.2", "000.00") -> Decimal(454.2),
+ ("454.2", "000D00") -> Decimal(454.2),
+ ("454.0", "000.0") -> Decimal(454),
+ ("454.0", "000D0") -> Decimal(454),
+ ("454.00", "000.00") -> Decimal(454),
+ ("454.00", "000D00") -> Decimal(454),
+ (".4542", ".0000") -> Decimal(0.4542),
+ (".4542", "D0000") -> Decimal(0.4542),
+ ("4542.", "0000.") -> Decimal(4542),
+ ("4542.", "0000D") -> Decimal(4542)
+ ).foreach { case ((str, format), expected) =>
+ checkEvaluation(ToNumber(Literal(str), Literal(format)), expected)
+ }
+
+ Seq("999.9.9", "999D9D9", "999.9D9", "999D9.9").foreach { str =>
+ ToNumber(Literal("454.3.2"), Literal(str)).checkInputDataTypes() match {
+ case TypeCheckResult.TypeCheckFailure(msg) =>
+ assert(msg.contains(s"At most one 'D' or '.' is allowed in the
number format: '$str'"))
+ }
+ }
+
+ // Test ',' and 'G'
+ checkExceptionInExpression[IllegalArgumentException](
+ ToNumber(Literal("123,456"), Literal("9G9")),
+ "Format '9G9' used for parsing string to number or formatting number to
string is invalid")
+
+ Seq(
+ ("12,454", "99,999") -> Decimal(12454),
+ ("12,454", "00,000") -> Decimal(12454),
+ ("12,454", "99G999") -> Decimal(12454),
+ ("12,454", "00G000") -> Decimal(12454),
Review comment:
same problem here, please make the tests more readable.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]