Github user rxin commented on a diff in the pull request:
https://github.com/apache/spark/pull/7202#discussion_r33886072
--- Diff:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/HiveTypeCoercion.scala
---
@@ -713,39 +715,68 @@ object HiveTypeCoercion {
case e: ExpectsInputTypes =>
val children: Seq[Expression] = e.children.zip(e.inputTypes).map {
case (in, expected) =>
- implicitCast(in, expected)
+ // If we cannot do the implicit cast, just use the original
input.
+ implicitCast(in, expected).getOrElse(in)
}
e.withNewChildren(children)
}
/**
- * If needed, cast the expression into the expected type.
- * If the implicit cast is not allowed, return the expression itself.
+ * Given an expected data type, try to cast the expression and return
the cast expression.
+ *
+ * If the expression already fits the input type, we simply return the
expression itself.
+ * If the expression has an incompatible type that cannot be
implicitly cast, return None.
*/
- def implicitCast(e: Expression, expectedType: AbstractDataType):
Expression = {
+ def implicitCast(e: Expression, expectedType: AbstractDataType):
Option[Expression] = {
val inType = e.dataType
- (inType, expectedType) match {
+
+ // 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 {
+
+ // If the expected type is already a parent of the input type, no
need to cast.
+ case _ if expectedType.isParentOf(inType) => e
+
// Cast null type (usually from null literals) into target types
- case (NullType, target: DataType) => Cast(e,
target.defaultConcreteType)
+ case (NullType, target) => Cast(e, target.defaultConcreteType)
// Implicit cast among numeric types
+ // If input is decimal, and we expect a decimal type, just use the
input.
+ case (_: DecimalType, DecimalType) => e
+ // If input is a numeric type but not decimal, and we expect a
decimal type,
+ // cast the input to unlimited precision decimal.
+ case (_: NumericType, DecimalType) if
!inType.isInstanceOf[DecimalType] =>
+ Cast(e, DecimalType.Unlimited)
+ // For any other numeric types, implicitly cast to each other,
e.g. long -> int, int -> long
case (_: NumericType, target: NumericType) if e.dataType != target
=> Cast(e, target)
+ case (_: NumericType, target: NumericType) => e
// Implicit cast between date time types
case (DateType, TimestampType) => Cast(e, TimestampType)
case (TimestampType, DateType) => Cast(e, DateType)
// Implicit cast from/to string
- case (StringType, NumericType) => Cast(e, DoubleType)
--- End diff --
there isn't a numeric adt anymore.
we can consider adding it back for expressions like round
---
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]