viirya commented on code in PR #60469:
URL: https://github.com/apache/spark/pull/60469#discussion_r4569620880
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala:
##########
@@ -615,6 +615,108 @@ object Cast extends QueryErrorsBase {
case _ => false
}
+ /**
+ * Returns true if a cast from `from` type to `to` type in `evalMode` is
total, as
+ * [[Expression.isTotal]] defines it: it never fails at runtime, and it
gives equal results for
+ * equal values. The patterns matched reflect the current implementation in
the Cast node and
+ * must stay in sync with it.
+ *
+ * A cast of a value with a FLOAT or DOUBLE in it to a type with a STRING or
a VARIANT in it is
+ * left out whether or not it can fail, as it tells -0.0 from 0.0, which are
equal. So is every
+ * cast from or to a type with a UDT in it, which renders its value with its
own
+ * `stringifyValue`, or a Python UDT as its `sqlType`, which can tell apart
values that are
+ * equal, and the checks here do not look inside it. The target is checked
too, defensively,
+ * although only a UDT or a NULL can be cast to one.
+ *
+ * Among the other casts, the result is the union of the ones that can not
fail in any eval mode
+ * and of the ones that can not fail in `evalMode`, so it does not depend on
the order of the
+ * checks.
+ */
+ def isTotalCast(from: DataType, to: DataType, evalMode: EvalMode.Value):
Boolean =
+ !tellsSignedZeros(from, to) && !hasUserDefinedType(from) &&
!hasUserDefinedType(to) &&
+ (cannotFailInAnyMode(from, to) || cannotFailInMode(from, to, evalMode))
+
+ // Whether the source type has a FLOAT or DOUBLE and the target type a
STRING or VARIANT in it,
+ // so that the cast may keep the sign of a zero, e.g. -0.0 as '-0.0'. This
is conservative: it
+ // does not match the fields of the types.
+ private def tellsSignedZeros(from: DataType, to: DataType): Boolean =
+ from.existsRecursively(t => t == FloatType || t == DoubleType) &&
+ to.existsRecursively(t => t.isInstanceOf[StringType] || t == VariantType)
+
+ // `existsRecursively` does not look inside a UDT, so neither does this.
+ private def hasUserDefinedType(dt: DataType): Boolean =
+ dt.existsRecursively(_.isInstanceOf[UserDefinedType[_]])
+
+ /**
+ * Returns true if the type has a decimal of a negative scale in it
+ * (`spark.sql.legacy.allowNegativeScaleOfDecimal`).
+ */
+ private[expressions] def hasNegativeScale(dt: DataType): Boolean =
dt.existsRecursively {
Review Comment:
Moved to `TypeUtils`.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/arithmetic.scala:
##########
@@ -215,6 +233,21 @@ abstract class BinaryArithmetic extends BinaryOperator
with SupportQueryContext
def evalMode: EvalMode.Value = evalContext.evalMode
+ /**
+ * Whether both operands are numbers of a non-negative scale. This is what
the subclasses that
+ * override [[isTotal]] require first: arithmetic over a DAY TO SECOND or
YEAR TO MONTH interval
+ * can overflow in every eval mode, and over a decimal of a negative scale
+ * (`spark.sql.legacy.allowNegativeScaleOfDecimal`) `Decimal.%` raises for a
very negative one.
+ * A subclass that does not override [[isTotal]] is not taken as total.
+ */
+ protected def hasNumericOperands: Boolean = children.forall { e =>
Review Comment:
Renamed to `hasTotalNumericOperands`.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala:
##########
@@ -741,6 +843,8 @@ case class Cast(
override protected def withNewChildInternal(newChild: Expression): Cast =
copy(child = newChild)
+ override def isTotal: Boolean = Cast.isTotalCast(child.dataType, dataType,
evalMode)
Review Comment:
Added to the scaladoc of `Expression.isTotal` that it is meant for resolved
expressions.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala:
##########
@@ -615,6 +615,108 @@ object Cast extends QueryErrorsBase {
case _ => false
}
+ /**
+ * Returns true if a cast from `from` type to `to` type in `evalMode` is
total, as
+ * [[Expression.isTotal]] defines it: it never fails at runtime, and it
gives equal results for
+ * equal values. The patterns matched reflect the current implementation in
the Cast node and
+ * must stay in sync with it.
+ *
+ * A cast of a value with a FLOAT or DOUBLE in it to a type with a STRING or
a VARIANT in it is
+ * left out whether or not it can fail, as it tells -0.0 from 0.0, which are
equal. So is every
+ * cast from or to a type with a UDT in it, which renders its value with its
own
+ * `stringifyValue`, or a Python UDT as its `sqlType`, which can tell apart
values that are
+ * equal, and the checks here do not look inside it. The target is checked
too, defensively,
+ * although only a UDT or a NULL can be cast to one.
+ *
+ * Among the other casts, the result is the union of the ones that can not
fail in any eval mode
+ * and of the ones that can not fail in `evalMode`, so it does not depend on
the order of the
+ * checks.
+ */
+ def isTotalCast(from: DataType, to: DataType, evalMode: EvalMode.Value):
Boolean =
+ !tellsSignedZeros(from, to) && !hasUserDefinedType(from) &&
!hasUserDefinedType(to) &&
+ (cannotFailInAnyMode(from, to) || cannotFailInMode(from, to, evalMode))
+
+ // Whether the source type has a FLOAT or DOUBLE and the target type a
STRING or VARIANT in it,
+ // so that the cast may keep the sign of a zero, e.g. -0.0 as '-0.0'. This
is conservative: it
+ // does not match the fields of the types.
+ private def tellsSignedZeros(from: DataType, to: DataType): Boolean =
+ from.existsRecursively(t => t == FloatType || t == DoubleType) &&
+ to.existsRecursively(t => t.isInstanceOf[StringType] || t == VariantType)
+
+ // `existsRecursively` does not look inside a UDT, so neither does this.
+ private def hasUserDefinedType(dt: DataType): Boolean =
+ dt.existsRecursively(_.isInstanceOf[UserDefinedType[_]])
+
+ /**
+ * Returns true if the type has a decimal of a negative scale in it
+ * (`spark.sql.legacy.allowNegativeScaleOfDecimal`).
+ */
+ private[expressions] def hasNegativeScale(dt: DataType): Boolean =
dt.existsRecursively {
+ case d: DecimalType => d.scale < 0
+ case _ => false
+ }
+
+ // The casts that can not fail in any eval mode, also in ANSI mode.
+ private def cannotFailInAnyMode(from: DataType, to: DataType): Boolean =
+ // A numeric up-cast holds also from or to a decimal of a negative scale,
as a widening between
+ // decimals never calls `Decimal.toLong`; a change to a negative scale
only checks that
+ // `spark.sql.legacy.allowNegativeScaleOfDecimal` is on. So does a decimal
to a FLOAT or a
+ // DOUBLE. The other ones do not: `Decimal.toLong` raises for a decimal of
a negative scale.
+ isNumericUpCast(from, to) || isDecimalToFloatOrDouble(from, to) ||
+ (!hasNegativeScale(from) && !hasNegativeScale(to) &&
+ (isTimestampToDate(from, to) || isToPlainString(from, to)))
+
+ // The casts that can not fail in the given eval mode but can in another
one: outside ANSI mode,
+ // the casts that return NULL, wrap or clamp for a value they can not
convert.
+ private def cannotFailInMode(from: DataType, to: DataType, evalMode:
EvalMode.Value): Boolean = {
Review Comment:
`isTotalCast` now computes it once and passes it down. I did not make it a
`lazy val`, to not add a field to every `Cast`.
--
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]