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 @@ -842,15 +842,26 @@ object TypeCoercion {
* Casts types according to the expected input types for [[Expression]]s.
*/
object ImplicitTypeCasts extends TypeCoercionRule {

private def canHandleTypeCoercion(leftType: DataType, rightType: DataType): Boolean = {
(leftType, rightType) match {
case (_: DecimalType, NullType) => true
case (NullType, _: DecimalType) => true
case _ =>
// If DecimalType operands are involved except for the two cases above,
// DecimalPrecision will handle it.
!leftType.isInstanceOf[DecimalType] && !rightType.isInstanceOf[DecimalType] &&
leftType != rightType
}
}

override protected def coerceTypes(
plan: LogicalPlan): LogicalPlan = plan resolveExpressions {
// Skip nodes who's children have not been resolved yet.
case e if !e.childrenResolved => e

// If DecimalType operands are involved, DecimalPrecision will handle it
case b @ BinaryOperator(left, right) if !left.dataType.isInstanceOf[DecimalType] &&
!right.dataType.isInstanceOf[DecimalType] &&
left.dataType != right.dataType =>
case b @ BinaryOperator(left, right)
if canHandleTypeCoercion(left.dataType, right.dataType) =>
findTightestCommonType(left.dataType, right.dataType).map { commonType =>
if (b.inputType.acceptsType(commonType)) {
// If the expression accepts the tightest common type, cast to that.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1541,6 +1541,24 @@ class TypeCoercionSuite extends AnalysisTest {
Multiply(CaseWhen(Seq((EqualTo(1, 2), Cast(1, DecimalType(34, 24)))),
Cast(100, DecimalType(34, 24))), Cast(1, IntegerType)))
}

test("SPARK-31468: null types should be casted to decimal types in ImplicitTypeCasts") {
Seq(AnyTypeBinaryOperator(_, _), NumericTypeBinaryOperator(_, _)).foreach { binaryOp =>
// binaryOp(decimal, null) case
ruleTest(TypeCoercion.ImplicitTypeCasts,
binaryOp(Literal.create(null, DecimalType.SYSTEM_DEFAULT),
Literal.create(null, NullType)),
binaryOp(Literal.create(null, DecimalType.SYSTEM_DEFAULT),
Cast(Literal.create(null, NullType), DecimalType.SYSTEM_DEFAULT)))

// binaryOp(null, decimal) case
ruleTest(TypeCoercion.ImplicitTypeCasts,
binaryOp(Literal.create(null, NullType),
Literal.create(null, DecimalType.SYSTEM_DEFAULT)),
binaryOp(Cast(Literal.create(null, NullType), DecimalType.SYSTEM_DEFAULT),
Literal.create(null, DecimalType.SYSTEM_DEFAULT)))
}
}
}


Expand Down