sarutak commented on code in PR #60119:
URL: https://github.com/apache/spark/pull/60119#discussion_r4496400594
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/ExprUtils.scala:
##########
@@ -266,13 +294,71 @@ object ExprUtils extends EvalHelper with QueryErrorsBase {
e.deterministic && !e.containsPattern(PLAN_EXPRESSION) &&
canEvaluateUnconditionallyInternal(e)
+ // Only the numeric pairs are taken from `Cast.canUpCast`, which also covers
e.g. DATE to
+ // TIMESTAMP, which can overflow, and casts between complex types. Among the
numeric ones, a
+ // decimal to an integral type can overflow in ANSI mode, e.g. DECIMAL(19,
0) to BIGINT, as
+ // `Cast.nullable` notes.
+ private def isNumericUpCast(from: DataType, to: DataType): Boolean = (from,
to) match {
+ case (_: DecimalType, _: IntegralType) => false
+ case (_: NumericType, _: NumericType) => Cast.canUpCast(from, to)
+ case _ => false
+ }
+
+ // Whether a cast can not fail. A cast of a date to a timestamp is left out
also in LEGACY mode,
+ // as converting a date far from the epoch to microseconds overflows there.
+ private def cannotFailCast(c: Cast): Boolean = (c.child.dataType,
c.dataType) match {
+ case (from, to) if isNumericUpCast(from, to) => true
+ case (TimestampType | TimestampNTZType, DateType) => true
+ // A CHAR or VARCHAR target checks the length of the string.
+ case (_: NumericType | BooleanType, s: StringType) if
StringHelper.isPlainString(s) => true
+ case _ if c.evalMode == EvalMode.TRY => true
+ // `Cast.isNoThrowLegacyCast` covers the numbers, booleans and strings. A
string to a date or
+ // a timestamp does not raise in LEGACY mode either: the parsers catch the
errors and return
+ // None, which becomes NULL.
+ case (_: StringType, DateType | TimestampType | TimestampNTZType)
+ if c.evalMode == EvalMode.LEGACY => true
+ case (from, to) if c.evalMode == EvalMode.LEGACY =>
Cast.isNoThrowLegacyCast(from, to)
+ case _ => false
+ }
+
+ // Whether numeric arithmetic can not fail, in its own eval mode.
+ private def cannotFailArithmetic(b: BinaryArithmetic): Boolean = {
+ val decimal = b.left.dataType.isInstanceOf[DecimalType]
+ b.children.forall(_.dataType.isInstanceOf[NumericType]) && (b.evalMode
match {
+ case EvalMode.LEGACY => b match {
+ case _: Add | _: Subtract | _: Multiply | _: Divide => true
+ // `Decimal.quot` and `Decimal.%` raise when the integral quotient is
too long.
+ case _: IntegralDivide | _: Remainder | _: Pmod => !decimal
+ case _ => false
+ }
+ // `DivModLike` and Pmod override `eval` without the catch of TRY mode,
and IntegralDivide
+ // and Pmod check a zero divisor there as in ANSI mode.
+ case EvalMode.TRY => b match {
Review Comment:
Done, thanks: `cannotFailArithmetic` now matches the operator first, with
the TRY-mode restriction only on `IntegralDivide` and `Pmod`.
--
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]