Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The real problem is, findWiderTypeForTwo doesn't satisfy the associative law, i.e. (a op b) op c may not equal to a op (b op c). I think StringType is the only exception here, it's more clear to do

val (stringType, nonStringType) = types.partition(_ == StringType)
(stringType.distinct ++ nonStringType).foldLeft...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a behavior change. We need to make it configurable. Add a conf and update the migration guide.

// 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