Github user marmbrus commented on a diff in the pull request:
https://github.com/apache/spark/pull/6754#discussion_r32278918
--- Diff:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/HiveTypeCoercion.scala
---
@@ -653,6 +663,26 @@ trait HiveTypeCoercion {
}
/**
+ * Coerces the type of different branches of If statement to a common
type.
+ */
+ object IfCoercion extends Rule[LogicalPlan] {
+ def apply(plan: LogicalPlan): LogicalPlan = plan
transformAllExpressions {
+ // Find tightest common type for If, if the true value and false
value have different types.
+ case i @ If(pred, left, right) if left.dataType != right.dataType =>
+ findTightestCommonTypeToString(left.dataType, right.dataType).map
{ widestType =>
+ val newLeft = if (left.dataType == widestType) left else
Cast(left, widestType)
+ val newRight = if (right.dataType == widestType) right else
Cast(right, widestType)
+ i.makeCopy(Array(pred, newLeft, newRight))
+ }.getOrElse(i) // If there is no applicable conversion, leave
expression unchanged.
+
+ // Convert If(null literal, _, _) into boolean type.
+ // In the optimizer, we should short-circuit this directly into
false value.
+ case i @ If(pred, left, right) if pred.dataType == NullType =>
+ i.makeCopy(Array(Literal.create(null, BooleanType), left, right))
--- End diff --
Why this special case? Also, I'd just use `If`instead of makeCopy here and
above. Make copy is nice when you are matching on different but structurally
similar expression, but looses compile time checks for arguments.
---
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]