@@ -58,6 +58,15 @@ object HiveTypeCoercion {
5858 case _ => None
5959 }
6060
61+ /** Similar to [[findTightestCommonType ]], but can promote all the way to StringType. */
62+ private def findTightestCommonTypeToString (left : DataType , right : DataType ): Option [DataType ] = {
63+ findTightestCommonTypeOfTwo(left, right).orElse((left, right) match {
64+ case (StringType , t2 : AtomicType ) if t2 != BinaryType && t2 != BooleanType => Some (StringType )
65+ case (t1 : AtomicType , StringType ) if t1 != BinaryType && t1 != BooleanType => Some (StringType )
66+ case _ => None
67+ })
68+ }
69+
6170 /**
6271 * Find the tightest common type of a set of types by continuously applying
6372 * `findTightestCommonTypeOfTwo` on these types.
@@ -660,7 +669,7 @@ trait HiveTypeCoercion {
660669 def apply (plan : LogicalPlan ): LogicalPlan = plan transformAllExpressions {
661670 // Find tightest common type for If, if the true value and false value have different types.
662671 case i @ If (pred, left, right) if left.dataType != right.dataType =>
663- findTightestCommonTypeOfTwo (left.dataType, right.dataType).map { widestType =>
672+ findTightestCommonTypeToString (left.dataType, right.dataType).map { widestType =>
664673 val newLeft = if (left.dataType == widestType) left else Cast (left, widestType)
665674 val newRight = if (right.dataType == widestType) right else Cast (right, widestType)
666675 i.makeCopy(Array (pred, newLeft, newRight))
0 commit comments