Github user liancheng commented on a diff in the pull request: https://github.com/apache/spark/pull/6405#discussion_r31390775 --- 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 -- The following version might be questionably easier to read: ```scala val maybeCommonType = cw.valueTypes.map(Some(_)).reduce { case (Some(dt1), Some(dt2)) => findTightestCommonType(dt1, dt2) case _ => None } maybeCommonType.map { commonType => val castedBranches = cw.branches.grouped(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 other => other }.reduce(_ ++ _) cw match { case _: CaseWhen => CaseWhen(castedBranches) case CaseKeyWhen(key, _) => CaseKeyWhen(key, castedBranches) } }.getOrElse(cw) ```
--- 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 infrastruct...@apache.org or file a JIRA ticket with INFRA. --- --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org