LuciferYang commented on code in PR #47001:
URL: https://github.com/apache/spark/pull/47001#discussion_r1643643929


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/mathExpressions.scala:
##########
@@ -1032,9 +1032,19 @@ object Hex {
     array
   }
 
+  private def maxArraySize(length: Long): Int = {
+    if (length > Int.MaxValue) {
+      throw QueryExecutionErrors.tooManyArrayElementsError(length, 
Int.MaxValue)
+    }
+    length.toInt
+  }
+
   def hex(bytes: Array[Byte]): UTF8String = {
     val length = bytes.length
-    val value = new Array[Byte](length * 2)
+    if (length == 0) {
+      return UTF8String.EMPTY_UTF8
+    }
+    val value = new Array[Byte](maxArraySize(length * 2L))

Review Comment:
   "Is it also feasible to check if `length <= Int.MaxValue/2`? Which is 
better, a single `division operation` or `multiple type conversions`?"



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/mathExpressions.scala:
##########
@@ -1032,9 +1032,19 @@ object Hex {
     array
   }
 
+  private def maxArraySize(length: Long): Int = {
+    if (length > Int.MaxValue) {
+      throw QueryExecutionErrors.tooManyArrayElementsError(length, 
Int.MaxValue)
+    }
+    length.toInt
+  }
+
   def hex(bytes: Array[Byte]): UTF8String = {
     val length = bytes.length
-    val value = new Array[Byte](length * 2)
+    if (length == 0) {
+      return UTF8String.EMPTY_UTF8
+    }
+    val value = new Array[Byte](maxArraySize(length * 2L))

Review Comment:
   Is it also feasible to check if `length <= Int.MaxValue/2`? Which is better, 
a single `division operation` or `multiple type conversions`?



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

Reply via email to