MaxGekk commented on code in PR #58910:
URL: https://github.com/apache/spark/pull/58910#discussion_r4641304005
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/arithmetic.scala:
##########
@@ -1152,7 +1168,9 @@ case class Pmod(
override def inputType: AbstractDataType = NumericType
- override def nullable: Boolean = true
+ // See DivModLike.nullable: pmod-by-zero (and decimal precision overflow)
throw under ANSI rather
+ // than returning null, so the result is null iff a child is; non-ANSI stays
nullable.
+ override def nullable: Boolean = left.nullable || right.nullable ||
!failOnError
Review Comment:
nit: the comment says TRY mode stays nullable, but that holds only for
`Divide` and `Remainder`. `IntegralDivide` and `Pmod` don't override
`failOnError`, whose base version is true for ANSI and TRY, and the new
override replaces `BinaryArithmetic.nullable`, which included `evalMode ==
EvalMode.TRY`. So `IntegralDivide(nonNullLong, nonNullLong,
EvalMode.TRY).nullable` changes from true to false, and so does `Pmod`'s. Could
we keep the TRY clause, or reword the comments, and add TRY cases for `div` and
`pmod` to the test?
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/arithmetic.scala:
##########
@@ -682,7 +682,11 @@ trait DivModLike extends BinaryArithmetic {
// Whether we should check overflow or not in ANSI mode.
protected def checkDivideOverflow: Boolean = false
- override def nullable: Boolean = true
+ // The result is null only when an input is, unless a
divide/remainder-by-zero (or, for decimals,
+ // a precision overflow) can produce a null without failing. Under ANSI
(`failOnError`) those
+ // conditions throw instead of returning null, so the result is null iff a
child is. Non-ANSI /
+ // TRY stay conservatively nullable (zero -> null; decimal overflow -> null).
+ override def nullable: Boolean = left.nullable || right.nullable ||
!failOnError
Review Comment:
With `nullable = false` under ANSI, `NullPropagation` can now remove a
division before it runs: it rewrites `count(e)` to `count(1)`, `e IS NULL` to
`false` and `e IS NOT NULL` to `true` whenever `!e.nullable`
(`optimizer/expressions.scala`, around L1173-1178). So with ANSI on and `t(a
INT NOT NULL, b INT NOT NULL)` holding a row with `b = 0`, `SELECT count(a / b)
FROM t` and `SELECT * FROM t WHERE (a % b) IS NULL` used to fail with
`DIVIDE_BY_ZERO` / `REMAINDER_BY_ZERO`, and would now return the row count and
no rows, without an error. `Pmod` (L1173) is the same. Is that intended? If
not, could these operators stay nullable, or the rewrites skip an expression
that can fail? If it is, could we add a test and a migration-guide note?
--
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]