Github user yhuai commented on a diff in the pull request:
https://github.com/apache/spark/pull/7175#discussion_r33748126
--- Diff:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/HiveTypeCoercion.scala
---
@@ -704,19 +704,46 @@ object HiveTypeCoercion {
/**
* Casts types according to the expected input types for Expressions
that have the trait
- * [[AutoCastInputTypes]].
+ * [[ExpectsInputTypes]].
*/
object ImplicitTypeCasts extends Rule[LogicalPlan] {
def apply(plan: LogicalPlan): LogicalPlan = plan
transformAllExpressions {
// Skip nodes who's children have not been resolved yet.
case e if !e.childrenResolved => e
- case e: AutoCastInputTypes if e.children.map(_.dataType) !=
e.inputTypes =>
- val newC = (e.children, e.children.map(_.dataType),
e.inputTypes).zipped.map {
- case (child, actual, expected) =>
- if (actual == expected) child else Cast(child, expected)
+ case e: ExpectsInputTypes =>
+ val children: Seq[Expression] = e.children.zip(e.inputTypes).map {
case (in, expected) =>
+ implicitCast(in, expected)
}
- e.withNewChildren(newC)
+ e.withNewChildren(children)
+ }
+
+ /**
+ * If needed, cast the expression into the expected type.
+ * If the implicit cast is not allowed, return the expression itself.
+ */
+ def implicitCast(e: Expression, expectedType: AbstractDataType):
Expression = {
+ (e, expectedType) match {
+ // Cast null type (usually from null literals) into target types
+ case (in @ NullType(), target: DataType) => Cast(in,
target.defaultConcreteType)
+
+ // Implicit cast among numeric types
+ case (in @ NumericType(), target: NumericType) if in.dataType !=
target =>
+ Cast(in, target)
+
+ // Implicit cast between date time types
+ case (in @ DateType(), TimestampType) => Cast(in, TimestampType)
+ case (in @ TimestampType(), DateType) => Cast(in, DateType)
+
+ // Implicit from string to atomic types, and vice versa
--- End diff --
Seems this comment is not very consistent with the code below? For `case
(in, StringType) if in.dataType != StringType`, we also cast complex types to
string?
---
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]