dgd-contributor commented on a change in pull request #33459:
URL: https://github.com/apache/spark/pull/33459#discussion_r676704901



##########
File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/NumberConverter.scala
##########
@@ -49,12 +49,14 @@ object NumberConverter {
    */
   private def encode(radix: Int, fromPos: Int, value: Array[Byte]): Long = {
     var v: Long = 0L
-    val bound = java.lang.Long.divideUnsigned(-1 - radix, radix) // Possible 
overflow once
+    // bound will always positive since radix >= 2
+    val bound = java.lang.Long.divideUnsigned(-1 - radix, radix)
     var i = fromPos
     while (i < value.length && value(i) >= 0) {
-      if (v >= bound) {
+      // check if v greater than bound
+      if (v >= bound || v < 0) {
         // Check for overflow
-        if (java.lang.Long.divideUnsigned(-1 - value(i), radix) < v) {
+        if (java.lang.Long.divideUnsigned(-1 - value(i), radix) < v || v < 0) {

Review comment:
       this condition is to check that v * radix + value(i) will be smaller 
than the max value in unsigned long type ( which is equal to -1 in long type). 
Because radix is >= 2, java.lang.Long.divideUnsigned(-1 - value(i), radix) will 
be a positive value (with its bit presentation start with 0), so we can compare 
 it with v as  java.lang.Long.divideUnsigned(-1 - value(i), radix) < v || v < 
0. Note that if v < 0, its bit presentation is start with 1 and will be greater 
than a positive value.




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