Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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 @@ -677,7 +677,7 @@ object HiveTypeCoercion {
case e if !e.childrenResolved => e
// 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 =>
findWiderTypeForTwo(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)
If(pred, newLeft, newRight)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,20 @@ class HiveTypeCoercionSuite extends PlanTest {
)
}

test("test for SPARK-13772") {
val rule = HiveTypeCoercion.IfCoercion
ruleTest(rule,
If(Literal(true), Literal(1.0), Cast(Literal(1.0), DecimalType(19, 0))),
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you also check it with other database systems like Hive? Do they allow double type and decimal type in if and else branches?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@cloud-fan It is ok in hive 1.2.1 and spark 1.4.1.
test case:
select if(1=1, cast(1 as double), cast(1 as decimal)) from test

If(Literal(true), Literal(1.0), Cast(Cast(Literal(1.0), DecimalType(19, 0)), DoubleType))
)

ruleTest(rule,
If(Literal(true), Literal(Decimal(1)), Cast(Literal(1.0), DecimalType(19, 9))),
If(Literal(true), Cast(Literal(Decimal(1)), DecimalType(19, 9)),
Cast(Literal(1.0), DecimalType(19, 9)))
)
}

test("type coercion for CaseKeyWhen") {
ruleTest(HiveTypeCoercion.CaseWhenCoercion,
CaseKeyWhen(Literal(1.toShort), Seq(Literal(1), Literal("a"))),
Expand Down