Github user concretevitamin commented on a diff in the pull request:
https://github.com/apache/spark/pull/1785#discussion_r15828596
--- Diff:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/HiveTypeCoercion.scala
---
@@ -336,28 +338,33 @@ trait HiveTypeCoercion {
}
/**
- * Ensures that NullType gets casted to some other types under certain
circumstances.
+ * Coerces the type of different branches of a CASE WHEN statement to a
common type.
*/
- object CastNulls extends Rule[LogicalPlan] {
+ object CaseWhenCoercion extends Rule[LogicalPlan] with TypeWidening {
def apply(plan: LogicalPlan): LogicalPlan = plan
transformAllExpressions {
- case cw @ CaseWhen(branches) =>
+ case cw @ CaseWhen(branches) if !cw.resolved &&
!branches.exists(!_.resolved) =>
val valueTypes = branches.sliding(2, 2).map {
- case Seq(_, value) if value.resolved => Some(value.dataType)
- case Seq(elseVal) if elseVal.resolved => Some(elseVal.dataType)
- case _ => None
+ case Seq(_, value) => value.dataType
+ case Seq(elseVal) => elseVal.dataType
}.toSeq
- if (valueTypes.distinct.size == 2 && valueTypes.exists(_ ==
Some(NullType))) {
- val otherType = valueTypes.filterNot(_ == Some(NullType))(0).get
+
+ logDebug(s"Input values for null casting
${valueTypes.mkString(",")}")
+
+ if (valueTypes.distinct.size > 1) {
+ val commonType = valueTypes.reduce { (v1, v2) =>
+ findTightestCommonType(v1, v2)
+ .getOrElse(sys.error(s"Invalid types in CASE WHEN. $v1,
$v2"))
--- End diff --
Perhaps "Value types in CASE WHEN must be the same, or can be coerced to a
tightest upper bound." to be more precise about the semantics, in case users
run into this.
---
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]