beliefer commented on a change in pull request #35060:
URL: https://github.com/apache/spark/pull/35060#discussion_r783688043
##########
File path:
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/StringExpressionsSuite.scala
##########
@@ -888,6 +889,174 @@ 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'
+ intercept[IllegalArgumentException] {
+ evaluateWithoutCodegen(ToNumber(Literal("454"), Literal("9")))
+ }
+ intercept[IllegalArgumentException] {
+ evaluateWithoutCodegen(ToNumber(Literal("454"), Literal("99")))
+ }
+
+ Seq(
+ ("454", "999") -> Decimal(454),
+ ("054", "999") -> Decimal(54),
+ ("54", "999") -> Decimal(54),
+ ("404", "999") -> Decimal(404),
+ ("450", "999") -> Decimal(450),
+ ("454", "9999") -> Decimal(454),
+ ("054", "9999") -> Decimal(54),
+ ("404", "9999") -> Decimal(404),
+ ("450", "9999") -> Decimal(450)
+ ).foreach { case ((str, format), expected) =>
+ checkEvaluation(ToNumber(Literal(str), Literal(format)), expected)
+ }
+
+ intercept[IllegalArgumentException] {
+ evaluateWithoutCodegen(ToNumber(Literal("454"), Literal("0")))
+ }
+ intercept[IllegalArgumentException] {
+ evaluateWithoutCodegen(ToNumber(Literal("454"), Literal("00")))
+ }
+
+ Seq(
+ ("454", "000") -> Decimal(454),
+ ("054", "000") -> Decimal(54),
+ ("54", "000") -> Decimal(54),
+ ("404", "000") -> Decimal(404),
+ ("450", "000") -> Decimal(450),
+ ("454", "0000") -> Decimal(454),
+ ("054", "0000") -> Decimal(54),
+ ("404", "0000") -> Decimal(404),
+ ("450", "0000") -> Decimal(450)
+ ).foreach { case ((str, format), expected) =>
+ checkEvaluation(ToNumber(Literal(str), Literal(format)), expected)
+ }
+
+ // Test '.' and 'D'
+ intercept[IllegalArgumentException] {
+ evaluateWithoutCodegen(ToNumber(Literal("454.2"), Literal("999")))
+ }
+ intercept[IllegalArgumentException] {
+ evaluateWithoutCodegen(ToNumber(Literal("454.23"), Literal("999.9")))
+ }
+
+ Seq(
+ ("454.2", "999.9") -> Decimal(454.2),
+ ("454.2", "000.0") -> Decimal(454.2),
+ ("454.2", "999D9") -> Decimal(454.2),
+ ("454.2", "000D0") -> Decimal(454.2),
+ ("454.23", "999.99") -> Decimal(454.23),
+ ("454.23", "000.00") -> Decimal(454.23),
+ ("454.23", "999D99") -> Decimal(454.23),
+ ("454.23", "000D00") -> Decimal(454.23),
+ ("454.0", "999.9") -> Decimal(454),
+ ("454.0", "000.0") -> Decimal(454),
+ ("454.0", "999D9") -> Decimal(454),
+ ("454.0", "000D0") -> Decimal(454),
+ ("454.00", "999.99") -> Decimal(454),
+ ("454.00", "000.00") -> Decimal(454),
+ ("454.00", "999D99") -> Decimal(454),
+ ("454.00", "000D00") -> Decimal(454),
+ (".4542", ".9999") -> Decimal(0.4542),
+ (".4542", ".0000") -> Decimal(0.4542),
+ (".4542", "D9999") -> Decimal(0.4542),
+ (".4542", "D0000") -> Decimal(0.4542),
+ ("4542.", "9999.") -> Decimal(4542),
+ ("4542.", "0000.") -> Decimal(4542),
+ ("4542.", "9999D") -> 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"Multiple 'D' or '.' in '$str'"))
+ }
+ }
+
+ // Test ',' and 'G'
+ Seq(
+ ("12,454", "99,999") -> Decimal(12454),
+ ("12,454", "00,000") -> Decimal(12454),
+ ("12,454", "99G999") -> Decimal(12454),
+ ("12,454", "00G000") -> Decimal(12454),
+ ("12,454,367", "99,999,999") -> Decimal(12454367),
+ ("12,454,367", "00,000,000") -> Decimal(12454367),
+ ("12,454,367", "99G999G999") -> Decimal(12454367),
+ ("12,454,367", "00G000G000") -> Decimal(12454367),
+ ("12,454,", "99,999,") -> Decimal(12454),
+ ("12,454,", "00,000,") -> Decimal(12454),
+ ("12,454,", "99G999G") -> Decimal(12454),
+ ("12,454,", "00G000G") -> Decimal(12454),
+ (",454,367", ",999,999") -> Decimal(454367),
Review comment:
`(",454,367", "999,999")` work good! But `("123,456", "9G9")` will
fail!. There exists a check the number of input digit must less or equal than
the number of digit in format string.
--
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]