vitaliili-db commented on code in PR #37483:
URL: https://github.com/apache/spark/pull/37483#discussion_r951966342


##########
core/src/main/resources/error/error-classes.json:
##########
@@ -70,6 +70,16 @@
       "Another instance of this query was just started by a concurrent 
session."
     ]
   },
+  "CONVERSION_INVALID_FORMAT" : {

Review Comment:
   Good point, fixed.



##########
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/StringExpressionsSuite.scala:
##########
@@ -452,6 +452,90 @@ class StringExpressionsSuite extends SparkFunSuite with 
ExpressionEvalHelper {
     // scalastyle:on
   }
 
+  test("to_binary") {
+    // Base64 - Positive cases
+    val base64Literal = Literal("base64")
+    Seq(
+      ("", ""),
+      ("  \r\n  \t", ""),
+      ("ag", "ag=="),
+      ("abc", "abc="),
+      ("abcd", "abcd"),
+      ("ab\r\n\t cd", "abcd"),
+      ("ab cd ef", "abcdeQ=="),
+      ("+ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789/",
+        "+ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789/"),
+      
("b25lIHR3byB0aHJlZSBmb3VyIGZpdmUgc2l4IHNldmVuIGVpZ2h0IG5pbmUgdGVuIGVsZXZlbiB0\r\n"
 +
+        
"d2VsdmUgdGhpcnRlZW4gZm91cnRlZW4gZml2dGVlbiBzaXh0ZWVuIHNldmVudGVlbiBlaWdodGVl\r\n"
 +
+        "biBuaW5ldGVlbiB0d2VudHkgdHdlbnR5LW9uZQ==",
+       
"b25lIHR3byB0aHJlZSBmb3VyIGZpdmUgc2l4IHNldmVuIGVpZ2h0IG5pbmUgdGVuIGVsZXZlbiB0\r\n"
 +
+        
"d2VsdmUgdGhpcnRlZW4gZm91cnRlZW4gZml2dGVlbiBzaXh0ZWVuIHNldmVudGVlbiBlaWdodGVl\r\n"
 +
+        "biBuaW5ldGVlbiB0d2VudHkgdHdlbnR5LW9uZQ==")
+    ).foreach {
+      case (input: String, expected: String) =>
+        checkEvaluation(Base64(ToBinary(Literal(input), base64Literal)), 
expected)
+    }
+    checkEvaluation(ToBinary(Literal.create(null, StringType), base64Literal), 
null)
+
+    // Base64 - Negative cases
+    Seq(
+      "a", // Too short
+      "abc==", // Invalid padding
+      "abcdef==a", // String should end with padding
+      "[email protected]" // Invalid character
+    ).foreach { input =>
+      val errMsg = "[CONVERSION_INVALID_INPUT]"
+      checkExceptionInExpression[Exception](ToBinary(Literal(input), 
base64Literal), errMsg)
+    }
+
+    // UTF-8/UTF8
+    val utf8Literal = Literal("Utf8")
+    val utf_8Literal = Literal("uTf-8")
+    // scalastyle:off
+    Seq(
+      "∮ E⋅da = Q,  n → ∞, ∑ f(i) = ∏ g(i), ∀x∈ℝ: ⌈x⌉ = −⌊−x⌋, α ∧ ¬β = ¬(¬α ∨ 
β)",
+      "大千世界",
+      ""
+    ).foreach {
+      input =>
+        checkEvaluation(StringDecode(ToBinary(Literal(input), utf8Literal), 
utf_8Literal), input)
+        checkEvaluation(StringDecode(ToBinary(Literal(input), utf_8Literal), 
utf_8Literal), input)
+    }
+    // scalastyle:on
+    checkEvaluation(ToBinary(Literal.create(null, StringType), utf8Literal), 
null)
+    checkEvaluation(ToBinary(Literal.create(null, StringType), utf_8Literal), 
null)
+
+    // HEX - Positive cases
+    val hexLiteral = Literal("hEx")
+    Seq(
+      ("737472696E67", "737472696E67"),
+      ("", ""),
+      ("F", "0F"),
+      ("ff", "FF"),
+      ("00", "00")
+    ).foreach {
+      case (input, expected) =>
+        checkEvaluation(Hex(ToBinary(Literal(input), hexLiteral)), expected)
+    }
+    checkEvaluation(ToBinary(Literal.create(null, StringType), hexLiteral), 
null)
+
+    // HEX - Negative cases
+    Seq(
+      "GG",
+      "FF 01"
+    ).foreach { input =>
+      val errMsg = "[CONVERSION_INVALID_INPUT]"
+      checkExceptionInExpression[Exception](ToBinary(Literal(input), 
hexLiteral), errMsg)
+    }
+
+    // Invalid format
+    val errMsg = "[CONVERSION_INVALID_FORMAT]"

Review Comment:
   Done, thank you



-- 
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]

Reply via email to