cloud-fan commented on a change in pull request #35060: URL: https://github.com/apache/spark/pull/35060#discussion_r783643127
########## 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), Review comment: some suggestions for the tests: 1. We know that some pattern chars are identical after normalization, and we don't need to test it everywhere. Just put a few tests to verify it. 2. We should test more factors, e.g. the number of `0` or `0` is equal to, smaller than, or larger than the actual number of digits in the value. -- 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: reviews-unsubscr...@spark.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org