dtenedor commented on code in PR #36365:
URL: https://github.com/apache/spark/pull/36365#discussion_r861220757
##########
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",
+ (Decimal(12454), "000,0000") -> "001,2454",
+ (Decimal(12454), "0000,0000") -> "0001,2454",
+ (Decimal(12454367), "000,000,000") -> "012,454,367",
+ (Decimal(12454), "000,000") -> "012,454",
+ (Decimal(12454), "999,999") -> " 12,454",
+ (Decimal(12454), "9,9999") -> "1,2454",
+ (Decimal(12454), "99,9999") -> " 1,2454",
+ (Decimal(12454), "999,9999") -> " 1,2454",
+ (Decimal(12454), "9999,9999") -> " 1,2454",
+ (Decimal(12454367), "999,999,999") -> " 12,454,367",
+ (Decimal(12454), "999,999") -> " 12,454"
+ ).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 '$'
+ Seq(
+ (Decimal(78.12), "$99.99") -> "$78.12",
+ (Decimal(78.12), "$00.00") -> "$78.12"
+ ).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 'S'
+ Seq(
+ (Decimal(unscaled = 43100000000L, precision = 38, scale = 10),
+ "9999999999999999.999999999999999S") ->
+ " 4.31 ",
+ (Decimal(0), "9999999999999999.999999999999999S") -> " 0
",
+ (Decimal(-454.8), "99G999.9S") -> " 454.8-",
+ (Decimal(-454.8), "00G000.0S") -> "00,454.8-",
+ (Decimal(-454), "S999") -> "-454",
+ (Decimal(-454), "999S") -> "454-",
+ (Decimal(-12454.8), "99G999D9S") -> "12,454.8-",
+ (Decimal(-83028485), "9999999999999999.999999999999999S") ->
+ " 83028485- "
Review Comment:
SG, done.
##########
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",
+ (Decimal(12454), "000,0000") -> "001,2454",
+ (Decimal(12454), "0000,0000") -> "0001,2454",
+ (Decimal(12454367), "000,000,000") -> "012,454,367",
+ (Decimal(12454), "000,000") -> "012,454",
+ (Decimal(12454), "999,999") -> " 12,454",
+ (Decimal(12454), "9,9999") -> "1,2454",
+ (Decimal(12454), "99,9999") -> " 1,2454",
+ (Decimal(12454), "999,9999") -> " 1,2454",
+ (Decimal(12454), "9999,9999") -> " 1,2454",
+ (Decimal(12454367), "999,999,999") -> " 12,454,367",
+ (Decimal(12454), "999,999") -> " 12,454"
+ ).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 '$'
+ Seq(
+ (Decimal(78.12), "$99.99") -> "$78.12",
+ (Decimal(78.12), "$00.00") -> "$78.12"
+ ).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 'S'
+ Seq(
+ (Decimal(unscaled = 43100000000L, precision = 38, scale = 10),
+ "9999999999999999.999999999999999S") ->
+ " 4.31 ",
+ (Decimal(0), "9999999999999999.999999999999999S") -> " 0
",
+ (Decimal(-454.8), "99G999.9S") -> " 454.8-",
+ (Decimal(-454.8), "00G000.0S") -> "00,454.8-",
+ (Decimal(-454), "S999") -> "-454",
+ (Decimal(-454), "999S") -> "454-",
+ (Decimal(-12454.8), "99G999D9S") -> "12,454.8-",
+ (Decimal(-83028485), "9999999999999999.999999999999999S") ->
+ " 83028485- "
+ ).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 'MI'
+ Seq(
+ (Decimal(4.31), "9999999999999999.999999999999999MI") -> "
4.31 ",
Review Comment:
SG, done.
--
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]