cloud-fan commented on a change in pull request #35060:
URL: https://github.com/apache/spark/pull/35060#discussion_r786115746
##########
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
Review comment:
Can we test the mixed case as I proposed before? `val format3 =
0.until(input.length + 2).map(i => i % 2).mkString`
It's usually not very useful to go deeper into one factor in the test. To
test format string that is longer than the value, the `+ 1` case should be
sufficient, we don't need to test `+ 2`.
--
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]