SEPURI-SAI-KRISHNA commented on code in PR #56746:
URL: https://github.com/apache/spark/pull/56746#discussion_r3748542942


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala:
##########
@@ -2053,16 +2053,19 @@ case class Slice(x: Expression, start: Expression, 
length: Expression)
     val lengthInt = lengthVal.asInstanceOf[Int]
     val arr = xVal.asInstanceOf[ArrayData]
     val startIndex = ArrayExpressionUtils.sliceStartIndex(startInt, 
arr.numElements(), prettyName)
-    if (lengthInt < 0) {
-      throw 
QueryExecutionErrors.unexpectedValueForLengthInFunctionError(prettyName, 
lengthInt)
-    }
+    // Resolve (and validate) the result length via the shared helper, 
mirroring the codegen path.
+    // Besides rejecting a negative length, this clamps the length to the 
elements remaining after
+    // `startIndex`, so `startIndex + resLength` cannot overflow `Int` for a 
large length -- the

Review Comment:
   Good catch, thanks. You're right: with a large negative start the helper's 
own numElements - startIndex wraps (e.g. numElements = 6, start = Int.MinValue 
gives startIndex = -2147483642, and 6 - startIndex overflows to Int.MinValue). 
The result is still correct only because the startIndex < 0 guard returns 
before resLength is read. I've qualified the claim to in-range start indexes 
and called out the guard explicitly.



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