Github user maropu commented on a diff in the pull request:
https://github.com/apache/spark/pull/21586#discussion_r196281780
--- Diff:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala
---
@@ -116,22 +116,23 @@ object Cast {
}
/**
- * Return true iff we may truncate during casting `from` type to `to`
type. e.g. long -> int,
- * timestamp -> date.
+ * Returns true iff we can safely cast the `from` type to `to` type
without any truncating or
+ * precision lose, e.g. int -> long, date -> timestamp.
*/
- def mayTruncate(from: DataType, to: DataType): Boolean = (from, to)
match {
- case (from: NumericType, to: DecimalType) if !to.isWiderThan(from) =>
true
- case (from: DecimalType, to: NumericType) if !from.isTighterThan(to)
=> true
- case (from, to) if illegalNumericPrecedence(from, to) => true
- case (TimestampType, DateType) => true
- case (StringType, to: NumericType) => true
+ def canSafeCast(from: DataType, to: DataType): Boolean = (from, to)
match {
+ case _ if from == to => true
+ case (from: NumericType, to: DecimalType) if to.isWiderThan(from) =>
true
+ case (from: DecimalType, to: NumericType) if from.isTighterThan(to) =>
true
+ case (from, to) if legalNumericPrecedence(from, to) => true
+ case (DateType, TimestampType) => true
+ case (_, StringType) => true
case _ => false
}
- private def illegalNumericPrecedence(from: DataType, to: DataType):
Boolean = {
+ private def legalNumericPrecedence(from: DataType, to: DataType):
Boolean = {
val fromPrecedence = TypeCoercion.numericPrecedence.indexOf(from)
val toPrecedence = TypeCoercion.numericPrecedence.indexOf(to)
- toPrecedence > 0 && fromPrecedence > toPrecedence
+ fromPrecedence >= 0 && fromPrecedence < toPrecedence
--- End diff --
super nit: `fromPrecedence != -1 && fromPrecedence < toPrecedence` is more
easy-to-understand?
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]