Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ case class Cast(child: Expression, dataType: DataType, timeZoneId: Option[String
if (longValue == longValue.toShort) {
longValue.toShort
} else {
throw new ArithmeticException(s"Casting $t to short causes overflow.")
throw new ArithmeticException(s"Casting $t to short causes overflow")
}
})
case TimestampType =>
Expand All @@ -548,12 +548,12 @@ case class Cast(child: Expression, dataType: DataType, timeZoneId: Option[String
x.exactNumeric.asInstanceOf[Numeric[Any]].toInt(b)
} catch {
case _: ArithmeticException =>
throw new ArithmeticException(s"Casting $b to short causes overflow.")
throw new ArithmeticException(s"Casting $b to short causes overflow")
}
if (intValue == intValue.toShort) {
intValue.toShort
} else {
throw new ArithmeticException(s"Casting $b to short causes overflow.")
throw new ArithmeticException(s"Casting $b to short causes overflow")
}
case x: NumericType =>
b => x.numeric.asInstanceOf[Numeric[Any]].toInt(b).toShort
Expand All @@ -578,7 +578,7 @@ case class Cast(child: Expression, dataType: DataType, timeZoneId: Option[String
if (longValue == longValue.toByte) {
longValue.toByte
} else {
throw new ArithmeticException(s"Casting $t to byte causes overflow.")
throw new ArithmeticException(s"Casting $t to byte causes overflow")
}
})
case TimestampType =>
Expand All @@ -589,12 +589,12 @@ case class Cast(child: Expression, dataType: DataType, timeZoneId: Option[String
x.exactNumeric.asInstanceOf[Numeric[Any]].toInt(b)
} catch {
case _: ArithmeticException =>
throw new ArithmeticException(s"Casting $b to byte causes overflow.")
throw new ArithmeticException(s"Casting $b to byte causes overflow")
}
if (intValue == intValue.toByte) {
intValue.toByte
} else {
throw new ArithmeticException(s"Casting $b to byte causes overflow.")
throw new ArithmeticException(s"Casting $b to byte causes overflow")
}
case x: NumericType =>
b => x.numeric.asInstanceOf[Numeric[Any]].toInt(b).toByte
Expand Down Expand Up @@ -1275,7 +1275,7 @@ case class Cast(child: Expression, dataType: DataType, timeZoneId: Option[String
if ($longValue == ($integralType) $longValue) {
$evPrim = ($integralType) $longValue;
} else {
throw new ArithmeticException("Casting $c to $integralType causes overflow");
throw new ArithmeticException("Casting " + $c + " to $integralType causes overflow");
}
"""
} else {
Expand All @@ -1300,7 +1300,7 @@ case class Cast(child: Expression, dataType: DataType, timeZoneId: Option[String
if ($c == ($integralType) $c) {
$evPrim = ($integralType) $c;
} else {
throw new ArithmeticException("Casting $c to $integralType causes overflow");
throw new ArithmeticException("Casting " + $c + " to $integralType causes overflow");
}
"""
}
Expand Down Expand Up @@ -1335,7 +1335,7 @@ case class Cast(child: Expression, dataType: DataType, timeZoneId: Option[String
if ($mathClass.floor($c) <= $max && $mathClass.ceil($c) >= $min) {
$evPrim = ($integralType) $c;
} else {
throw new ArithmeticException("Casting $c to $integralType causes overflow");
throw new ArithmeticException("Casting " + $c + " to $integralType causes overflow");
}
"""
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,10 @@ abstract class BinaryArithmetic extends BinaryOperator with NullIntolerant {
def calendarIntervalMethod: String =
sys.error("BinaryArithmetics must override either calendarIntervalMethod or genCode")

/** Name of the function for the exact version of this expression in [[Math]]. */
def exactMathMethod: String =
sys.error("BinaryArithmetics must override either exactMathMethod or genCode")
// Name of the function for the exact version of this expression in [[Math]].
// If the option "spark.sql.failOnIntegralTypeOverflow" is enabled and there is corresponding
// function in [[Math]], the exact function will be called instead of evaluation with [[symbol]].
def exactMathMethod: Option[String] = None

override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = dataType match {
case _: DecimalType =>
Expand Down Expand Up @@ -182,9 +183,9 @@ abstract class BinaryArithmetic extends BinaryOperator with NullIntolerant {
})
case IntegerType | LongType =>
nullSafeCodeGen(ctx, ev, (eval1, eval2) => {
val operation = if (checkOverflow) {
val operation = if (checkOverflow && exactMathMethod.isDefined) {
val mathClass = classOf[Math].getName
s"$mathClass.$exactMathMethod($eval1, $eval2)"
s"$mathClass.${exactMathMethod.get}($eval1, $eval2)"
} else {
s"$eval1 $symbol $eval2"
}
Expand Down Expand Up @@ -235,7 +236,7 @@ case class Add(left: Expression, right: Expression) extends BinaryArithmetic {
}
}

override def exactMathMethod: String = "addExact"
override def exactMathMethod: Option[String] = Some("addExact")
}

@ExpressionDescription(
Expand Down Expand Up @@ -265,7 +266,7 @@ case class Subtract(left: Expression, right: Expression) extends BinaryArithmeti
}
}

override def exactMathMethod: String = "subtractExact"
override def exactMathMethod: Option[String] = Some("subtractExact")
}

@ExpressionDescription(
Expand All @@ -286,7 +287,7 @@ case class Multiply(left: Expression, right: Expression) extends BinaryArithmeti

protected override def nullSafeEval(input1: Any, input2: Any): Any = numeric.times(input1, input2)

override def exactMathMethod: String = "multiplyExact"
override def exactMathMethod: Option[String] = Some("multiplyExact")
}

// Common base trait for Divide and Remainder, since these two classes are almost identical
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ final class Decimal extends Ordered[Decimal] with Serializable {
def toByte: Byte = toLong.toByte

private def overflowException(dataType: String) =
throw new ArithmeticException(s"Casting $this to $dataType causes overflow.")
throw new ArithmeticException(s"Casting $this to $dataType causes overflow")

/**
* @return the Byte value that is equal to the rounded decimal.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,13 @@ object LongExactNumeric extends LongIsIntegral with Ordering.LongOrdering {
if (x == x.toInt) {
x.toInt
} else {
throw new ArithmeticException(s"Casting $x to int causes overflow.")
throw new ArithmeticException(s"Casting $x to int causes overflow")
}
}

object FloatExactNumeric extends FloatIsFractional with Ordering.FloatOrdering {
private def overflowException(x: Float, dataType: String) =
throw new ArithmeticException(s"Casting $x to $dataType causes overflow.")
throw new ArithmeticException(s"Casting $x to $dataType causes overflow")

private val intUpperBound = Int.MaxValue.toFloat
private val intLowerBound = Int.MinValue.toFloat
Expand Down Expand Up @@ -152,7 +152,7 @@ object FloatExactNumeric extends FloatIsFractional with Ordering.FloatOrdering {

object DoubleExactNumeric extends DoubleIsFractional with Ordering.DoubleOrdering {
private def overflowException(x: Double, dataType: String) =
throw new ArithmeticException(s"Casting $x to $dataType causes overflow.")
throw new ArithmeticException(s"Casting $x to $dataType causes overflow")

private val intUpperBound = Int.MaxValue.toDouble
private val intLowerBound = Int.MinValue.toDouble
Expand Down
5 changes: 0 additions & 5 deletions sql/core/src/test/resources/sql-tests/inputs/pgSQL/int4.sql
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,6 @@ INSERT INTO INT4_TBL VALUES ('-2147483647');
-- INSERT INTO INT4_TBL(f1) VALUES ('123 5');
-- INSERT INTO INT4_TBL(f1) VALUES ('');

-- We cannot test this when failOnOverFlow=true here
-- because exception happens in the executors and the
-- output stacktrace cannot have an exact match
set spark.sql.arithmeticOperations.failOnOverFlow=false;

SELECT '' AS five, * FROM INT4_TBL;

SELECT '' AS four, i.* FROM INT4_TBL i WHERE i.f1 <> smallint('0');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,9 +334,10 @@ struct<CAST(CAST(-2147483648.5 AS FLOAT) AS INT):int>
-- !query 37
SELECT int(float('-2147483900'))
-- !query 37 schema
struct<CAST(CAST(-2147483900 AS FLOAT) AS INT):int>
struct<>
-- !query 37 output
-2147483648
java.lang.ArithmeticException
Casting -2.1474839E9 to int causes overflow


-- !query 38
Expand Down Expand Up @@ -366,9 +367,10 @@ struct<CAST(CAST(-9223372036854775808.5 AS FLOAT) AS BIGINT):bigint>
-- !query 41
SELECT bigint(float('-9223380000000000000'))
-- !query 41 schema
struct<CAST(CAST(-9223380000000000000 AS FLOAT) AS BIGINT):bigint>
struct<>
-- !query 41 output
-9223372036854775808
java.lang.ArithmeticException
Casting -9.22338E18 to int causes overflow


-- !query 42
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -826,9 +826,10 @@ struct<CAST(CAST(-9223372036854775808.5 AS DOUBLE) AS BIGINT):bigint>
-- !query 93
SELECT bigint(double('-9223372036854780000'))
-- !query 93 schema
struct<CAST(CAST(-9223372036854780000 AS DOUBLE) AS BIGINT):bigint>
struct<>
-- !query 93 output
-9223372036854775808
java.lang.ArithmeticException
Casting -9.22337203685478E18 to long causes overflow


-- !query 94
Expand Down
Loading