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

    https://github.com/apache/spark/pull/7175#discussion_r33749314
  
    --- 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
    +        case (in @ StringType(), target: AtomicType) if target != 
StringType =>
    +          Cast(in, target.defaultConcreteType)
    +        case (in, StringType) if in.dataType != StringType =>
    --- End diff --
    
    I don't think that makes sense w.r.t. "semantic context".
    
    As a matter of fact, most mature databases have well defined semantics for 
implicit type casting. If we make numeric type -> stringtype an implicit cast 
rule, we should have that for all expressions. If we don't want that implicit 
cast rule, then we should never do the implicit cast. I'd argue that having 
some expressions doing implicit casting and some not doing it is extremely 
confusing. 



---
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