Github user cloud-fan commented on a diff in the pull request:
https://github.com/apache/spark/pull/7175#discussion_r33771063
--- Diff:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/HiveTypeCoercion.scala
---
@@ -704,19 +704,48 @@ 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 = {
+ val inType = e.dataType
+ (inType, expectedType) match {
+ // Cast null type (usually from null literals) into target types
+ case (NullType, target: DataType) => Cast(e,
target.defaultConcreteType)
+
+ // Implicit cast among numeric types
+ case (_: NumericType, target: NumericType) if e.dataType != target
=> Cast(e, target)
+
+ // 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)
+ 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 (any, StringType) if any != StringType => Cast(e, StringType)
--- End diff --
according to hive
https://cwiki.apache.org/confluence/display/Hive/LanguageManual+Types and
discussion in https://github.com/apache/spark/pull/6551,
we should only allow atomic type(except boolean and binary) 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]