cloud-fan commented on code in PR #36365:
URL: https://github.com/apache/spark/pull/36365#discussion_r860474163
##########
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/StringExpressionsSuite.scala:
##########
@@ -1108,6 +1125,238 @@ class StringExpressionsSuite extends SparkFunSuite with
ExpressionEvalHelper {
}
}
+ test("ToCharacter: positive tests") {
+ // Test '0' and '9'
+ Seq(
+ (Decimal(454), "9999") -> " 454",
+ (Decimal(454), "99999") -> " 454",
+ (Decimal(4), "0") -> "4",
+ (Decimal(45), "00") -> "45",
+ (Decimal(454), "000") -> "454",
+ (Decimal(454), "0000") -> "0454",
+ (Decimal(454), "00000") -> "00454"
+ ).foreach { case ((decimal, format), expected) =>
+ var expr: Expression = ToCharacter(Literal(decimal), Literal(format))
+ assert(expr.checkInputDataTypes() == TypeCheckResult.TypeCheckSuccess)
+ checkEvaluation(expr, expected)
+
+ expr = TryToCharacter(Literal(decimal), Literal(format))
+ assert(expr.checkInputDataTypes() == TypeCheckResult.TypeCheckSuccess)
+ checkEvaluation(expr, expected)
+ }
+
+ // Test '.' and 'D'
+ Seq(
+ (Decimal(0.4542), ".00000") -> ".4542 ",
+ (Decimal(454.2), "000.0") -> "454.2",
+ (Decimal(454), "000.0") -> "454 ",
+ (Decimal(454.2), "000.00") -> "454.2 ",
+ (Decimal(454), "000.00") -> "454 ",
+ (Decimal(0.4542), ".0000") -> ".4542",
+ (Decimal(4542), "0000.") -> "4542 "
+ ).foreach { case ((decimal, format), expected) =>
+ val format2 = format.replace('.', 'D')
+ var expr: Expression = ToCharacter(Literal(decimal), Literal(format))
+ assert(expr.checkInputDataTypes() == TypeCheckResult.TypeCheckSuccess)
+ checkEvaluation(expr, expected)
+
+ expr = ToCharacter(Literal(decimal), Literal(format2))
+ assert(expr.checkInputDataTypes() == TypeCheckResult.TypeCheckSuccess)
+ checkEvaluation(expr, expected)
+
+ expr = TryToCharacter(Literal(decimal), Literal(format))
+ assert(expr.checkInputDataTypes() == TypeCheckResult.TypeCheckSuccess)
+ checkEvaluation(expr, expected)
+
+ expr = TryToCharacter(Literal(decimal), Literal(format2))
+ assert(expr.checkInputDataTypes() == TypeCheckResult.TypeCheckSuccess)
+ checkEvaluation(expr, expected)
+ }
+
+ Seq(
+ (Decimal(454.2), "0000.00") -> "0454.2 ",
+ (Decimal(454), "0000.00") -> "0454 ",
+ (Decimal(4542), "00000.") -> "04542 ",
+ (Decimal(454.2), "9999.99") -> " 454.2 ",
+ (Decimal(454), "9999.99") -> " 454 ",
+ (Decimal(4542), "99999.") -> " 4542 "
+ ).foreach { case ((decimal, format), expected) =>
+ var expr: Expression = ToCharacter(Literal(decimal), Literal(format))
+ assert(expr.checkInputDataTypes() == TypeCheckResult.TypeCheckSuccess)
+ checkEvaluation(expr, expected)
+
+ expr = TryToCharacter(Literal(decimal), Literal(format))
+ assert(expr.checkInputDataTypes() == TypeCheckResult.TypeCheckSuccess)
+ checkEvaluation(expr, expected)
+ }
+
+ // Test ',' and 'G'
+ Seq(
+ (Decimal(12454), "0,0000") -> "1,2454",
+ (Decimal(12454), "00,000") -> "12,454",
+ (Decimal(124543), "000,000") -> "124,543",
+ (Decimal(1245436), "0,000,000") -> "1,245,436",
+ (Decimal(12454367), "00,000,000") -> "12,454,367",
+ (Decimal(12454), "0,0000") -> "1,2454",
+ (Decimal(12454), "00,000") -> "12,454",
+ (Decimal(454367), "000,000") -> "454,367"
+ ).foreach { case ((decimal, format), expected) =>
+ val format2 = format.replace('0', '9')
+ val format3 = format.replace(',', 'G')
+ var expr: Expression = ToCharacter(Literal(decimal), Literal(format))
+ assert(expr.checkInputDataTypes() == TypeCheckResult.TypeCheckSuccess)
+ checkEvaluation(expr, expected)
+
+ expr = ToCharacter(Literal(decimal), Literal(format2))
+ assert(expr.checkInputDataTypes() == TypeCheckResult.TypeCheckSuccess)
+ checkEvaluation(expr, expected)
+
+ expr = ToCharacter(Literal(decimal), Literal(format3))
+ assert(expr.checkInputDataTypes() == TypeCheckResult.TypeCheckSuccess)
+ checkEvaluation(expr, expected)
+
+ expr = TryToCharacter(Literal(decimal), Literal(format))
+ assert(expr.checkInputDataTypes() == TypeCheckResult.TypeCheckSuccess)
+ checkEvaluation(expr, expected)
+
+ expr = TryToCharacter(Literal(decimal), Literal(format2))
+ assert(expr.checkInputDataTypes() == TypeCheckResult.TypeCheckSuccess)
+ checkEvaluation(expr, expected)
+
+ expr = TryToCharacter(Literal(decimal), Literal(format3))
+ assert(expr.checkInputDataTypes() == TypeCheckResult.TypeCheckSuccess)
+ checkEvaluation(expr, expected)
+ }
+
+ Seq(
+ (Decimal(12454), "000,000") -> "012,454",
+ (Decimal(12454), "00,0000") -> "01,2454",
+ (Decimal(12454), "000,0000") -> "001,2454",
+ (Decimal(12454), "0000,0000") -> "0001,2454",
+ (Decimal(12454), "00,0000") -> "01,2454",
Review Comment:
can we test that the format pattern has more groups than the input? `e.g.
`Decimal(12), "000,000"`
--
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]