Github user hvanhovell commented on a diff in the pull request:
https://github.com/apache/spark/pull/16057#discussion_r90012747
--- Diff:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/TypeCoercion.scala
---
@@ -714,6 +714,17 @@ object TypeCoercion {
// try to find the first one we can implicitly cast.
case (_, TypeCollection(types)) => types.flatMap(implicitCast(e,
_)).headOption.orNull
+ case (ArrayType(_, nullable), ArrayType(internalType: DataType,
expectedNullable))
+ if (expectedNullable || nullable == expectedNullable) =>
--- End diff --
This might work:
```scala
def implicitCast(e: Expression, expectedType: AbstractDataType):
Option[Expression] = {
implicitCast(e.dataType, expectedType).collect {
case dt if dt != e.dataType => 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: 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) => inType
// Cast null type (usually from null literals) into target types
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) => 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) => DecimalType.forType(d)
// For any other numeric types, implicitly cast to each other, e.g.
long -> int, int -> long
case (_: NumericType, target: NumericType) => target
// Implicit cast between date time types
case (DateType, TimestampType) => TimestampType
case (TimestampType, DateType) => DateType
// Implicit cast from/to string
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 => 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(inType, _)).headOption.orNull
case (ArrayType(dt1, n1), ArrayType(dt2: DataType, n2)) if n2 || !n1 =>
implicitCast(dt1, dt2).map(ArrayType(_, n2)).orNull
case _ => null
}
Option(ret)
}
```
---
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]