MaxGekk commented on code in PR #58913:
URL: https://github.com/apache/spark/pull/58913#discussion_r4641304379
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/decimalExpressions.scala:
##########
@@ -119,7 +119,9 @@ case class CheckOverflow(
override def nullIntolerant: Boolean = true
- 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
Review Comment:
Under ANSI (`nullOnOverflow = false`) with a non-nullable child,
`CheckOverflow` now reports `nullable = false`, so `NullPropagation` can fold
`IsNull`/`IsNotNull` over it, and `Coalesce` can prune after it, without
evaluating it. For example, `IsNull(CheckOverflow(Literal(Decimal("10.1")),
DecimalType(4, 3), nullOnOverflow = false))` used to throw and would now fold
to `false`. Is that the intended trade-off, as for an ANSI `Cast`, whose
`nullable` is `child.nullable`? Either way, could a test pin down what
`isnull`/`coalesce` over an overflowing `CheckOverflow` do?
Separately, `SerializerBuildHelper` wraps the `BigDecimal`/`BigInteger`
serializers in `CheckOverflow(..., nullOnOverflow = !ansiEnabled)`, and
`ExpressionEncoder.schema` takes the nullability from the serializers. So
`Encoders.row` over a non-nullable `DECIMAL(10, 2)` field now gives `nullable =
false` with ANSI on and `true` with ANSI off, where both were `true` before.
Could we check that no caller compares encoder schemas built under different
settings?
##########
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/DecimalExpressionSuite.scala:
##########
@@ -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"))
Review Comment:
nit: the new test runs the non-nullable branch only on a value that fits.
Could it also check `checkExceptionInExpression` for `CheckOverflow(d,
DecimalType(4, 3), false)` and `checkEvaluation(CheckOverflow(d, DecimalType(4,
3), true), null)`, so that a non-nullable `CheckOverflow` is shown to throw
rather than return null?
--
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]