Github user hvanhovell commented on a diff in the pull request:

    https://github.com/apache/spark/pull/16057#discussion_r92440234
  
    --- Diff: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/TypeCoercion.scala
 ---
    @@ -673,48 +673,54 @@ object TypeCoercion {
          * If the expression has an incompatible type that cannot be 
implicitly cast, return None.
          */
         def implicitCast(e: Expression, expectedType: AbstractDataType): 
Option[Expression] = {
    -      val inType = e.dataType
    +      implicitCast(e.dataType, expectedType).map { dt =>
    +        if (dt == e.dataType) e else Cast(e, dt)
    +      }
    +    }
     
    +    private def implicitCast(inType: DataType, expectedType: 
AbstractDataType): Option[DataType] = {
           // Note that ret is nullable to avoid typing a lot of Some(...) in 
this local scope.
           // We wrap immediately an Option after this.
    -      @Nullable val ret: Expression = (inType, expectedType) match {
    -
    +      @Nullable val ret: DataType = (inType, expectedType) match {
             // If the expected type is already a parent of the input type, no 
need to cast.
    -        case _ if expectedType.acceptsType(inType) => e
    +        case _ if expectedType.acceptsType(inType) => inType
     
             // Cast null type (usually from null literals) into target types
    -        case (NullType, target) => Cast(e, target.defaultConcreteType)
    +        case (NullType, target) => target.defaultConcreteType
     
             // If the function accepts any numeric type and the input is a 
string, we follow the hive
             // convention and cast that input into a double
    -        case (StringType, NumericType) => Cast(e, 
NumericType.defaultConcreteType)
    +        case (StringType, NumericType) => NumericType.defaultConcreteType
     
             // Implicit cast among numeric types. When we reach here, input 
type is not acceptable.
     
             // If input is a numeric type but not decimal, and we expect a 
decimal type,
             // cast the input to decimal.
    -        case (d: NumericType, DecimalType) => Cast(e, 
DecimalType.forType(d))
    +        case (d: NumericType, DecimalType) => DecimalType.forType(d)
             // For any other numeric types, implicitly cast to each other, 
e.g. long -> int, int -> long
    -        case (_: NumericType, target: NumericType) => Cast(e, target)
    +        case (_: NumericType, target: NumericType) => target
     
             // Implicit cast between date time types
    -        case (DateType, TimestampType) => Cast(e, TimestampType)
    -        case (TimestampType, DateType) => Cast(e, DateType)
    +        case (DateType, TimestampType) => TimestampType
    +        case (TimestampType, DateType) => DateType
     
             // Implicit cast from/to string
    -        case (StringType, DecimalType) => Cast(e, 
DecimalType.SYSTEM_DEFAULT)
    -        case (StringType, target: NumericType) => Cast(e, target)
    -        case (StringType, DateType) => Cast(e, DateType)
    -        case (StringType, TimestampType) => Cast(e, TimestampType)
    -        case (StringType, BinaryType) => Cast(e, BinaryType)
    +        case (StringType, DecimalType) => DecimalType.SYSTEM_DEFAULT
    +        case (StringType, target: NumericType) => target
    +        case (StringType, DateType) => DateType
    +        case (StringType, TimestampType) => TimestampType
    +        case (StringType, BinaryType) => BinaryType
             // Cast any atomic type to string.
    -        case (any: AtomicType, StringType) if any != StringType => Cast(e, 
StringType)
    +        case (any: AtomicType, StringType) if any != StringType => 
StringType
     
             // When we reach here, input type is not acceptable for any types 
in this type collection,
             // try to find the first one we can implicitly cast.
    -        case (_, TypeCollection(types)) => types.flatMap(implicitCast(e, 
_)).headOption.orNull
    +        case (_, TypeCollection(types)) =>
    +          types.flatMap(implicitCast(inType, _)).headOption.orNull
    +
    +        case (ArrayType(dt1, n1), ArrayType(dt2: DataType, n2)) if n2 || 
!n1 =>
    --- End diff --
    
    Some casts can always produce a null, for example casting `Double.Inf` to 
`Long` will result in a null. We need to check if this nullability does not 
cause any problems with `ArrayType.containsNull`. See @viirya previous comment 
on this.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to