Skip to content

Commit

Permalink
fix type coercion when promting to StringType
Browse files Browse the repository at this point in the history
  • Loading branch information
jiangxb1987 committed Apr 15, 2018
1 parent 73f2853 commit 803a6a4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,13 @@ object TypeCoercion {
private def findWiderCommonType(types: Seq[DataType]): Option[DataType] = {
types.foldLeft[Option[DataType]](Some(NullType))((r, c) => r match {
case Some(d) => findWiderTypeForTwo(d, c)
case None => None
// Currently we find the wider common type by comparing the two types from left to right,
// this can be a problem when you have two data types which don't have a common type but each
// can be promoted to StringType. For instance, (TimestampType, IntegerType, StringType)
// should have StringType as the wider common type.
case None if types.exists(_ == StringType) &&
types.forall(stringPromotion(_, StringType).nonEmpty) => Some(StringType)
case _ => None
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,11 @@ class TypeCoercionSuite extends AnalysisTest {
Coalesce(Seq(nullLit, floatNullLit, doubleLit, stringLit)),
Coalesce(Seq(Cast(nullLit, StringType), Cast(floatNullLit, StringType),
Cast(doubleLit, StringType), Cast(stringLit, StringType))))

ruleTest(rule,
Coalesce(Seq(timestampLit, intLit, stringLit)),
Coalesce(Seq(Cast(timestampLit, StringType), Cast(intLit, StringType),
Cast(stringLit, StringType))))
}

test("CreateArray casts") {
Expand Down

0 comments on commit 803a6a4

Please sign in to comment.