cloud-fan commented on code in PR #48203:
URL: https://github.com/apache/spark/pull/48203#discussion_r1771019997


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/stringExpressions.scala:
##########
@@ -3039,16 +3039,20 @@ object Encode {
       legacyCharsets: Boolean,
       legacyErrorAction: Boolean): Array[Byte] = {
     val toCharset = charset.toString
-    if (input.numBytes == 0 || "UTF-8".equalsIgnoreCase(toCharset)) {
-      return input.getBytes
-    }
-    val encoder = CharsetProvider.newEncoder(toCharset, legacyCharsets, 
legacyErrorAction)
-    try {
-      val bb = encoder.encode(CharBuffer.wrap(input.toString))
-      JavaUtils.bufferToArray(bb)
-    } catch {
-      case _: CharacterCodingException =>
-        throw QueryExecutionErrors.malformedCharacterCoding("encode", 
toCharset)
+    if ("UTF-8".equalsIgnoreCase(toCharset) && input.isValid) {

Review Comment:
   Personally I feel code with shortcuts is easier to read as there are less 
indentations. How about
   ```
   if ("UTF-8".equalsIgnoreCase(toCharset) && input.isValid) return 
input.getBytes
   val encoder = CharsetProvider.newEncoder(toCharset, legacyCharsets, 
legacyErrorAction)
   if (input.numBytes == 0) return input.getBytes
   ...
   ```



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