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


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala:
##########
@@ -3448,13 +3448,32 @@ object Sequence {
         || (estimatedStep == num.zero && start == stop),
       s"Illegal sequence boundaries: $start to $stop by $step")
 
-    val len = if (start == stop) 1L else 1L + (stop.toLong - start.toLong) / 
estimatedStep.toLong
-
-    require(
-      len <= MAX_ROUNDED_ARRAY_LENGTH,
-      s"Too long sequence: $len. Should be <= $MAX_ROUNDED_ARRAY_LENGTH")
-
-    len.toInt
+    try {
+      val delta = Math.subtractExact(stop.toLong, start.toLong)
+      if (delta == Long.MinValue && estimatedStep.toLong == -1L) {
+        // We must special-case division of Long.MinValue by -1 to catch 
potential unchecked
+        // overflow in next operation. Division does not have a builtin 
overflow check. We
+        // previously special-case div-by-zero.
+        throw new ArithmeticException("Long overflow (Long.MinValue / -1)")
+      }
+      val len = if (stop == start) 1L else Math.addExact(1L, (delta / 
estimatedStep.toLong))
+      require(
+        len <= MAX_ROUNDED_ARRAY_LENGTH,
+        s"Too long sequence: $len. Should be <= $MAX_ROUNDED_ARRAY_LENGTH"
+      )
+      len.toInt
+    } catch {
+      // We handle overflows in the previous try block by raising an 
appropriate exception.
+      case _: ArithmeticException =>
+        val safeLen =
+          BigInt(1) + (BigInt(stop.toLong) - BigInt(start.toLong)) / 
BigInt(estimatedStep.toLong)
+        require(
+          safeLen <= MAX_ROUNDED_ARRAY_LENGTH,
+          s"Too long sequence: $safeLen. Should be <= 
$MAX_ROUNDED_ARRAY_LENGTH"
+        )
+        throw new RuntimeException("Unreachable code reached.")

Review Comment:
   if it's not reachable, let's throw `SparkException.internalError`



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala:
##########
@@ -3448,13 +3448,32 @@ object Sequence {
         || (estimatedStep == num.zero && start == stop),
       s"Illegal sequence boundaries: $start to $stop by $step")
 
-    val len = if (start == stop) 1L else 1L + (stop.toLong - start.toLong) / 
estimatedStep.toLong
-
-    require(
-      len <= MAX_ROUNDED_ARRAY_LENGTH,
-      s"Too long sequence: $len. Should be <= $MAX_ROUNDED_ARRAY_LENGTH")
-
-    len.toInt
+    try {
+      val delta = Math.subtractExact(stop.toLong, start.toLong)
+      if (delta == Long.MinValue && estimatedStep.toLong == -1L) {
+        // We must special-case division of Long.MinValue by -1 to catch 
potential unchecked
+        // overflow in next operation. Division does not have a builtin 
overflow check. We
+        // previously special-case div-by-zero.
+        throw new ArithmeticException("Long overflow (Long.MinValue / -1)")
+      }
+      val len = if (stop == start) 1L else Math.addExact(1L, (delta / 
estimatedStep.toLong))
+      require(
+        len <= MAX_ROUNDED_ARRAY_LENGTH,
+        s"Too long sequence: $len. Should be <= $MAX_ROUNDED_ARRAY_LENGTH"
+      )
+      len.toInt
+    } catch {
+      // We handle overflows in the previous try block by raising an 
appropriate exception.
+      case _: ArithmeticException =>
+        val safeLen =
+          BigInt(1) + (BigInt(stop.toLong) - BigInt(start.toLong)) / 
BigInt(estimatedStep.toLong)
+        require(

Review Comment:
   let's use explicit `if` to throw the error.



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