Skip to content

Commit 3aa4e11

Browse files
wangyumHyukjinKwon
authored andcommitted
[SPARK-33861][SQL][FOLLOWUP] Simplify conditional in predicate should consider deterministic
### What changes were proposed in this pull request? This pr address apache#30865 (review) to fix simplify conditional in predicate should consider deterministic. ### Why are the changes needed? Fix bug. ### Does this PR introduce _any_ user-facing change? No. ### How was this patch tested? Unit test. Closes apache#31067 from wangyum/SPARK-33861-2. Authored-by: Yuming Wang <[email protected]> Signed-off-by: HyukjinKwon <[email protected]>
1 parent 26b6039 commit 3aa4e11

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,7 @@ import org.apache.spark.sql.types.BooleanType
3939
* - CASE WHEN cond THEN trueVal ELSE null END => AND(cond, trueVal)
4040
* - CASE WHEN cond THEN trueVal ELSE true END => OR(NOT(cond), trueVal)
4141
* - CASE WHEN cond THEN false ELSE elseVal END => AND(NOT(cond), elseVal)
42-
* - CASE WHEN cond THEN false END => false
4342
* - CASE WHEN cond THEN true ELSE elseVal END => OR(cond, elseVal)
44-
* - CASE WHEN cond THEN true END => cond
4543
*/
4644
object SimplifyConditionalsInPredicate extends Rule[LogicalPlan] {
4745

@@ -64,12 +62,8 @@ object SimplifyConditionalsInPredicate extends Rule[LogicalPlan] {
6462
And(cond, trueValue)
6563
case CaseWhen(Seq((cond, trueValue)), Some(TrueLiteral)) =>
6664
Or(Not(cond), trueValue)
67-
case CaseWhen(Seq((_, FalseLiteral)), Some(FalseLiteral) | None) =>
68-
FalseLiteral
6965
case CaseWhen(Seq((cond, FalseLiteral)), Some(elseValue)) =>
7066
And(Not(cond), elseValue)
71-
case CaseWhen(Seq((cond, TrueLiteral)), Some(FalseLiteral) | None) =>
72-
cond
7367
case CaseWhen(Seq((cond, TrueLiteral)), Some(elseValue)) =>
7468
Or(cond, elseValue)
7569
case e if e.dataType == BooleanType => e

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import org.apache.spark.sql.AnalysisException
2121
import org.apache.spark.sql.catalyst.analysis.UnresolvedAttribute
2222
import org.apache.spark.sql.catalyst.dsl.expressions._
2323
import org.apache.spark.sql.catalyst.dsl.plans._
24-
import org.apache.spark.sql.catalyst.expressions.{And, CaseWhen, Expression, If, IsNotNull, Literal, Or}
24+
import org.apache.spark.sql.catalyst.expressions.{And, CaseWhen, Expression, If, IsNotNull, Literal, Or, Rand}
2525
import org.apache.spark.sql.catalyst.expressions.Literal.{FalseLiteral, TrueLiteral}
2626
import org.apache.spark.sql.catalyst.plans.{Inner, PlanTest}
2727
import org.apache.spark.sql.catalyst.plans.logical.{DeleteFromTable, LocalRelation, LogicalPlan, UpdateTable}
@@ -158,6 +158,15 @@ class SimplifyConditionalsInPredicateSuite extends PlanTest {
158158
testProjection(originalCond, expectedExpr = originalCond)
159159
}
160160

161+
test("CASE WHEN non-deterministic-cond THEN false END") {
162+
val originalCond =
163+
CaseWhen(Seq((UnresolvedAttribute("i") > Rand(0), FalseLiteral)))
164+
val expectedCond = And(UnresolvedAttribute("i") > Rand(0), FalseLiteral)
165+
// nondeterministic expressions are only allowed in Project, Filter, Aggregate or Window,
166+
testFilter(originalCond, expectedCond = FalseLiteral)
167+
testProjection(originalCond, expectedExpr = originalCond)
168+
}
169+
161170
test("CASE WHEN cond THEN true ELSE elseVal END => OR(cond, elseVal)") {
162171
val originalCond = CaseWhen(
163172
Seq((UnresolvedAttribute("i") > Literal(10), TrueLiteral)),

0 commit comments

Comments
 (0)