Skip to content

Commit 4c21e43

Browse files
committed
fix
1 parent b33fa53 commit 4c21e43

File tree

3 files changed

+17
-19
lines changed

3 files changed

+17
-19
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/expressions.scala

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -510,8 +510,9 @@ object SimplifyConditionals extends Rule[LogicalPlan] with PredicateHelper {
510510
val (h, t) = branches.span(_._1 != TrueLiteral)
511511
CaseWhen( h :+ t.head, None)
512512

513-
case e @ CaseWhen(branches, Some(elseValue))
514-
if branches.forall(_._2.semanticEquals(elseValue)) =>
513+
case e @ CaseWhen(branches, elseOpt)
514+
if branches.forall(_._2.semanticEquals(elseOpt.getOrElse(Literal(null, e.dataType)))) =>
515+
val elseValue = elseOpt.getOrElse(Literal(null, e.dataType))
515516
// For non-deterministic conditions with side effect, we can not remove it, or change
516517
// the ordering. As a result, we try to remove the deterministic conditions from the tail.
517518
var hitNonDeterministicCond = false
@@ -527,10 +528,6 @@ object SimplifyConditionals extends Rule[LogicalPlan] with PredicateHelper {
527528
} else {
528529
e.copy(branches = branches.take(i).map(branch => (branch._1, elseValue)))
529530
}
530-
531-
case e @ CaseWhen(branches, None)
532-
if branches.forall(_._2.semanticEquals(Literal(null, e.dataType))) =>
533-
Literal(null, e.dataType)
534531
}
535532
}
536533
}

sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/optimizer/PushFoldableIntoBranchesSuite.scala

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -260,13 +260,12 @@ class PushFoldableIntoBranchesSuite
260260
}
261261

262262
test("SPARK-33847: Remove the CaseWhen if elseValue is empty and other outputs are null") {
263-
Seq(a, LessThan(Rand(1), Literal(0.5))).foreach { condition =>
264-
assertEquivalent(
265-
EqualTo(CaseWhen(Seq((condition, Literal.create(null, IntegerType)))), Literal(2)),
266-
Literal.create(null, BooleanType))
267-
assertEquivalent(
268-
EqualTo(CaseWhen(Seq((condition, Literal("str")))).cast(IntegerType), Literal(2)),
269-
Literal.create(null, BooleanType))
270-
}
263+
assertEquivalent(
264+
EqualTo(CaseWhen(Seq((a, Literal.create(null, IntegerType)))), Literal(2)),
265+
Literal.create(null, BooleanType))
266+
assertEquivalent(
267+
EqualTo(CaseWhen(Seq((LessThan(Rand(1), Literal(0.5)), Literal("str")))).cast(IntegerType),
268+
Literal(2)),
269+
CaseWhen(Seq((LessThan(Rand(1), Literal(0.5)), Literal.create(null, BooleanType)))))
271270
}
272271
}

sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/optimizer/SimplifyConditionalSuite.scala

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -237,10 +237,12 @@ class SimplifyConditionalSuite extends PlanTest with ExpressionEvalHelper with P
237237
}
238238

239239
test("SPARK-33847: Remove the CaseWhen if elseValue is empty and other outputs are null") {
240-
Seq(GreaterThan('a, 1), GreaterThan(Rand(0), 1)).foreach { condition =>
241-
assertEquivalent(
242-
CaseWhen((condition, Literal.create(null, IntegerType)) :: Nil, None),
243-
Literal.create(null, IntegerType))
244-
}
240+
assertEquivalent(
241+
CaseWhen((GreaterThan('a, 1), Literal.create(null, IntegerType)) :: Nil, None),
242+
Literal.create(null, IntegerType))
243+
244+
assertEquivalent(
245+
CaseWhen((GreaterThan(Rand(0), 0.5), Literal.create(null, IntegerType)) :: Nil, None),
246+
CaseWhen((GreaterThan(Rand(0), 0.5), Literal.create(null, IntegerType)) :: Nil, None))
245247
}
246248
}

0 commit comments

Comments
 (0)