Skip to content

Commit 360d124

Browse files
committed
Fixed the rule.
1 parent fb66657 commit 360d124

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -220,19 +220,21 @@ object HiveTypeCoercion {
220220
// Skip nodes who's children have not been resolved yet.
221221
case e if !e.childrenResolved => e
222222

223+
case b @ BinaryOperator(left, right)
224+
if left.dataType == NullType && right.dataType == NullType =>
225+
// If both inputs are null type (from null literals), cast the null type into some
226+
// specific type the expression expects, so expressions don't need to handle NullType
227+
val newLeft = Cast(left, b.inputType.defaultConcreteType)
228+
val newRight = Cast(right, b.inputType.defaultConcreteType)
229+
b.makeCopy(Array(newLeft, newRight))
230+
223231
case b @ BinaryOperator(left, right) if left.dataType != right.dataType =>
224232
findTightestCommonTypeOfTwo(left.dataType, right.dataType).map { commonType =>
225233
if (b.inputType.acceptsType(commonType)) {
226234
// If the expression accepts the tighest common type, cast to that.
227235
val newLeft = if (left.dataType == commonType) left else Cast(left, commonType)
228236
val newRight = if (right.dataType == commonType) right else Cast(right, commonType)
229237
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))
236238
} else {
237239
// Otherwise, don't do anything with the expression.
238240
b

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,9 @@ class HiveTypeCoercionSuite extends PlanTest {
197197
test("null literals handling for binary operators") {
198198
ruleTest(HiveTypeCoercion.WidenTypes,
199199
Add(Literal.create(null, NullType), Literal.create(null, NullType)),
200-
Add(Literal.create(null, DoubleType), Literal.create(null, DoubleType)))
200+
Add(
201+
Cast(Literal.create(null, NullType), DoubleType),
202+
Cast(Literal.create(null, NullType), DoubleType)))
201203
}
202204

203205
test("coalesce casts") {

0 commit comments

Comments
 (0)