Skip to content
Open
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 @@ -117,7 +117,9 @@ case class CheckOverflow(
dataType: DecimalType,
nullOnOverflow: Boolean) extends UnaryExpression with SupportQueryContext {

override def nullable: Boolean = true
// When `nullOnOverflow` is false, an overflow throws instead of producing null, so the result is
// null only when the input is. When true, an overflow yields null regardless of the input.
override def nullable: Boolean = child.nullable || nullOnOverflow

override def nullSafeEval(input: Any): Any =
input.asInstanceOf[Decimal].toPrecision(
Expand All @@ -130,11 +132,13 @@ case class CheckOverflow(
override protected def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = {
val errorContextCode = getContextOrNullCode(ctx, !nullOnOverflow)
nullSafeCodeGen(ctx, ev, eval => {
// Only assign isNull when nullable; nullSafeCodeGen makes it a literal otherwise.
val setIsNull = if (nullable) s"${ev.isNull} = ${ev.value} == null;" else ""
// scalastyle:off line.size.limit
s"""
|${ev.value} = $eval.toPrecision(
| ${dataType.precision}, ${dataType.scale}, Decimal.ROUND_HALF_UP(), $nullOnOverflow, $errorContextCode);
|${ev.isNull} = ${ev.value} == null;
|$setIsNull
""".stripMargin
// scalastyle:on line.size.limit
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,16 @@ class DecimalExpressionSuite extends SparkFunSuite with ExpressionEvalHelper {
Literal.create(null, DecimalType(2, 1)), DecimalType(3, 2), false), null)
}

test("SPARK-59643: CheckOverflow nullability") {
val d = Literal(Decimal("10.1")) // non-nullable
val n = Literal.create(null, DecimalType(3, 1)) // nullable
assert(CheckOverflow(d, DecimalType(4, 1), nullOnOverflow = true).nullable)
assert(!CheckOverflow(d, DecimalType(4, 1), nullOnOverflow = false).nullable)
assert(CheckOverflow(n, DecimalType(4, 1), nullOnOverflow = true).nullable)
assert(CheckOverflow(n, DecimalType(4, 1), nullOnOverflow = false).nullable)
checkEvaluation(CheckOverflow(d, DecimalType(4, 1), nullOnOverflow = false), Decimal("10.1"))
}

test("SPARK-39208: CheckOverflow & CheckOverflowInSum support query context in runtime errors") {
val d = Decimal(101, 3, 1)
val query = "select cast(d as decimal(4, 3)) from t"
Expand Down