Skip to content

Commit fb66657

Browse files
committed
Convert NullType into some accepted type for BinaryOperators.
1 parent 2e22330 commit fb66657

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/HiveTypeCoercion.scala

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,13 +222,19 @@ object HiveTypeCoercion {
222222

223223
case b @ BinaryOperator(left, right) if left.dataType != right.dataType =>
224224
findTightestCommonTypeOfTwo(left.dataType, right.dataType).map { commonType =>
225-
// If the expression accepts the tighest common type, cast to that.
226-
// Otherwise, don't do anything with the expression.
227225
if (b.inputType.acceptsType(commonType)) {
226+
// If the expression accepts the tighest common type, cast to that.
228227
val newLeft = if (left.dataType == commonType) left else Cast(left, commonType)
229228
val newRight = if (right.dataType == commonType) right else Cast(right, commonType)
230229
b.makeCopy(Array(newLeft, newRight))
230+
} else if (commonType == NullType) {
231+
// If the common type is null type (from null literals), cast the null type into the
232+
// first accepted type.
233+
val newLeft = Cast(left, b.inputType.defaultConcreteType)
234+
val newRight = Cast(right, b.inputType.defaultConcreteType)
235+
b.makeCopy(Array(newLeft, newRight))
231236
} else {
237+
// Otherwise, don't do anything with the expression.
232238
b
233239
}
234240
}.getOrElse(b) // If there is no applicable conversion, leave expression unchanged.

sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/analysis/HiveTypeCoercionSuite.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,12 @@ class HiveTypeCoercionSuite extends PlanTest {
194194
Project(Seq(Alias(transformed, "a")()), testRelation))
195195
}
196196

197+
test("null literals handling for binary operators") {
198+
ruleTest(HiveTypeCoercion.WidenTypes,
199+
Add(Literal.create(null, NullType), Literal.create(null, NullType)),
200+
Add(Literal.create(null, DoubleType), Literal.create(null, DoubleType)))
201+
}
202+
197203
test("coalesce casts") {
198204
ruleTest(HiveTypeCoercion.FunctionArgumentConversion,
199205
Coalesce(Literal(1.0)

0 commit comments

Comments
 (0)