-
Notifications
You must be signed in to change notification settings - Fork 28.7k
[SPARK-26218][SQL] Overflow on arithmetic operations returns incorrect result #21599
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
5c662f6
[SPARK-24598][SQL] Overflow on airthmetic operation returns incorrect…
mgaido91 fad75fa
fix scalastyle
mgaido91 8591417
fix ut failures
mgaido91 9c3df7d
use larger intermediate buffer for sum
mgaido91 ebdaf61
fix UT error
mgaido91 a0b862e
allow precision loss when converting decimal to long
mgaido91 7bba22f
Merge branch 'master' into SPARK-24598
mgaido91 77f26f2
Merge branch 'master' of github.com:apache/spark into SPARK-24598
mgaido91 74cd0a4
Handle NaN
mgaido91 2cfd946
Add conf flag for checking overflow
mgaido91 25c853c
fix
mgaido91 ff02dca
Merge branch 'master' of github.com:apache/spark into SPARK-24598
mgaido91 00fae1d
fix tests
mgaido91 8e9715c
change default value and fix tests
mgaido91 1dff779
Merge branch 'master' into SPARK-24598
mgaido91 38fc1f4
fix typo
mgaido91 0d5e510
Merge branch 'SPARK-24598' of github.com:mgaido91/spark into SPARK-24598
mgaido91 37e19ce
fix
mgaido91 eb37ee7
Merge branch 'master' of github.com:apache/spark into SPARK-24598
mgaido91 98bbf83
address comments
mgaido91 650ea79
fix
mgaido91 1d20f73
address comments
mgaido91 538e332
address comments
mgaido91 3de4bfb
fix
mgaido91 3baecbc
fixes
mgaido91 a247f9f
fix unaryminus
mgaido91 582d148
address comments
mgaido91 b809a3f
fix
mgaido91 ce3ed2b
address comments
mgaido91 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
sql/catalyst/src/main/scala/org/apache/spark/sql/types/numerics.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.spark.sql.types | ||
|
||
import scala.math.Numeric.{ByteIsIntegral, IntIsIntegral, LongIsIntegral, ShortIsIntegral} | ||
import scala.math.Ordering | ||
|
||
|
||
object ByteExactNumeric extends ByteIsIntegral with Ordering.ByteOrdering { | ||
private def checkOverflow(res: Int, x: Byte, y: Byte, op: String): Unit = { | ||
if (res > Byte.MaxValue || res < Byte.MinValue) { | ||
throw new ArithmeticException(s"$x $op $y caused overflow.") | ||
} | ||
} | ||
|
||
override def plus(x: Byte, y: Byte): Byte = { | ||
val tmp = x + y | ||
checkOverflow(tmp, x, y, "+") | ||
tmp.toByte | ||
} | ||
|
||
override def minus(x: Byte, y: Byte): Byte = { | ||
val tmp = x - y | ||
checkOverflow(tmp, x, y, "-") | ||
tmp.toByte | ||
} | ||
|
||
override def times(x: Byte, y: Byte): Byte = { | ||
val tmp = x * y | ||
checkOverflow(tmp, x, y, "*") | ||
tmp.toByte | ||
} | ||
|
||
override def negate(x: Byte): Byte = { | ||
if (x == Byte.MinValue) { // if and only if x is Byte.MinValue, overflow can happen | ||
throw new ArithmeticException(s"- $x caused overflow.") | ||
} | ||
(-x).toByte | ||
} | ||
} | ||
|
||
|
||
object ShortExactNumeric extends ShortIsIntegral with Ordering.ShortOrdering { | ||
private def checkOverflow(res: Int, x: Short, y: Short, op: String): Unit = { | ||
if (res > Short.MaxValue || res < Short.MinValue) { | ||
throw new ArithmeticException(s"$x $op $y caused overflow.") | ||
} | ||
} | ||
|
||
override def plus(x: Short, y: Short): Short = { | ||
val tmp = x + y | ||
checkOverflow(tmp, x, y, "+") | ||
tmp.toShort | ||
} | ||
|
||
override def minus(x: Short, y: Short): Short = { | ||
val tmp = x - y | ||
checkOverflow(tmp, x, y, "-") | ||
tmp.toShort | ||
} | ||
|
||
override def times(x: Short, y: Short): Short = { | ||
val tmp = x * y | ||
checkOverflow(tmp, x, y, "*") | ||
tmp.toShort | ||
} | ||
|
||
override def negate(x: Short): Short = { | ||
if (x == Short.MinValue) { // if and only if x is Byte.MinValue, overflow can happen | ||
throw new ArithmeticException(s"- $x caused overflow.") | ||
} | ||
(-x).toShort | ||
} | ||
} | ||
|
||
|
||
object IntegerExactNumeric extends IntIsIntegral with Ordering.IntOrdering { | ||
override def plus(x: Int, y: Int): Int = Math.addExact(x, y) | ||
|
||
override def minus(x: Int, y: Int): Int = Math.subtractExact(x, y) | ||
|
||
override def times(x: Int, y: Int): Int = Math.multiplyExact(x, y) | ||
|
||
override def negate(x: Int): Int = Math.negateExact(x) | ||
} | ||
|
||
object LongExactNumeric extends LongIsIntegral with Ordering.LongOrdering { | ||
override def plus(x: Long, y: Long): Long = Math.addExact(x, y) | ||
|
||
override def minus(x: Long, y: Long): Long = Math.subtractExact(x, y) | ||
|
||
override def times(x: Long, y: Long): Long = Math.multiplyExact(x, y) | ||
|
||
override def negate(x: Long): Long = Math.negateExact(x) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.