dtenedor commented on code in PR #36365: URL: https://github.com/apache/spark/pull/36365#discussion_r870545641
########## sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/StringExpressionsSuite.scala: ########## @@ -1108,6 +1125,366 @@ 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 ", + // There are no digits after the decimal point. + (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(12), + "000,000") -> + "000,012", + (Decimal(1245436), + "0,000,000") -> + "1,245,436", + (Decimal(12454367), + "00,000,000") -> + "12,454,367" + ).foreach { case ((decimal, format), expected) => + val format2 = 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 = 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(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), Review Comment: Good catch, removed :) -- 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