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

    https://github.com/apache/spark/pull/6405#discussion_r31390713
  
    --- Diff: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/HiveTypeCoercion.scala
 ---
    @@ -633,25 +618,26 @@ trait HiveTypeCoercion {
         import HiveTypeCoercion._
     
         def apply(plan: LogicalPlan): LogicalPlan = plan 
transformAllExpressions {
    -      case cw: CaseWhenLike if !cw.resolved && cw.childrenResolved && 
!cw.valueTypesEqual =>
    +      case cw: CaseWhenLike if cw.childrenResolved && 
cw.checkInputDataTypes().hasError =>
             logDebug(s"Input values for null casting 
${cw.valueTypes.mkString(",")}")
    -        val commonType = cw.valueTypes.reduce { (v1, v2) =>
    -          findTightestCommonType(v1, v2).getOrElse(sys.error(
    -            s"Types in CASE WHEN must be the same or coercible to a common 
type: $v1 != $v2"))
    -        }
    -        val transformedBranches = cw.branches.sliding(2, 2).map {
    -          case Seq(when, value) if value.dataType != commonType =>
    -            Seq(when, Cast(value, commonType))
    -          case Seq(elseVal) if elseVal.dataType != commonType =>
    -            Seq(Cast(elseVal, commonType))
    -          case s => s
    -        }.reduce(_ ++ _)
    -        cw match {
    -          case _: CaseWhen =>
    -            CaseWhen(transformedBranches)
    -          case CaseKeyWhen(key, _) =>
    -            CaseKeyWhen(key, transformedBranches)
    -        }
    +        cw.valueTypes.foldLeft[Option[DataType]](Some(NullType))((r, c) => 
r match {
    +          case None => None
    +          case Some(d) => findTightestCommonType(d, c)
    +        }).map { commonType =>
    +          val transformedBranches = cw.branches.sliding(2, 2).map {
    +            case Seq(when, value) if value.dataType != commonType =>
    +              Seq(when, Cast(value, commonType))
    +            case Seq(elseVal) if elseVal.dataType != commonType =>
    +              Seq(Cast(elseVal, commonType))
    +            case s => s
    +          }.reduce(_ ++ _)
    +          cw match {
    +            case _: CaseWhen =>
    +              CaseWhen(transformedBranches)
    +            case CaseKeyWhen(key, _) =>
    +              CaseKeyWhen(key, transformedBranches)
    +          }
    +        }.getOrElse(cw)
    --- End diff --
    
    Oh I see, `CheckAnalysis` catches all malformed expressions for us at the 
end of the analysis phase.


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