Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -94,6 +94,7 @@ object ReplaceNullWithFalseInPredicate extends Rule[LogicalPlan] {
replaceNullWithFalse(cond) -> replaceNullWithFalse(value)
}
val newElseValue = cw.elseValue.map(replaceNullWithFalse)
.orElse(if (newBranches.forall(_._2 == FalseLiteral)) Some(FalseLiteral) else None)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change assumes that other rules can optimize CASE WHEN with false in all branches, which violates the catalyst principle that rules should be orthogonal. Can we think of a better way?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we do need the logic from 2 optimizer rules:

  1. null means false in predicates
  2. eliminate CASE WHEN if all branches return the same value

We can probably include logic 2 in this rule

...
if (newBranches.forall(_._2 == FalseLiteral) && cw.elseValue.isEmpty) {
  FalseLiteral
} else {
  val newElseValue = cw.elseValue.map(replaceNullWithFalse)
  CaseWhen(newBranches, newElseValue)
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. This change assumes CaseWhen has optimized by PushFoldableIntoBranches, SimplifyConditionals and ConstantFolding.

CaseWhen(newBranches, newElseValue)
case i @ If(pred, trueVal, falseVal) if i.dataType == BooleanType =>
If(replaceNullWithFalse(pred), replaceNullWithFalse(trueVal), replaceNullWithFalse(falseVal))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import org.apache.spark.sql.AnalysisException
import org.apache.spark.sql.catalyst.analysis.UnresolvedAttribute
import org.apache.spark.sql.catalyst.dsl.expressions._
import org.apache.spark.sql.catalyst.dsl.plans._
import org.apache.spark.sql.catalyst.expressions.{And, ArrayExists, ArrayFilter, ArrayTransform, CaseWhen, Expression, GreaterThan, If, LambdaFunction, Literal, MapFilter, NamedExpression, Or, UnresolvedNamedLambdaVariable}
import org.apache.spark.sql.catalyst.expressions.{And, ArrayExists, ArrayFilter, ArrayTransform, CaseWhen, EqualTo, Expression, GreaterThan, If, LambdaFunction, Literal, MapFilter, NamedExpression, Or, UnresolvedNamedLambdaVariable}
import org.apache.spark.sql.catalyst.expressions.Literal.{FalseLiteral, TrueLiteral}
import org.apache.spark.sql.catalyst.plans.{Inner, PlanTest}
import org.apache.spark.sql.catalyst.plans.logical.{DeleteFromTable, LocalRelation, LogicalPlan, UpdateTable}
Expand All @@ -38,6 +38,7 @@ class ReplaceNullWithFalseInPredicateSuite extends PlanTest {
ConstantFolding,
BooleanSimplification,
SimplifyConditionals,
PushFoldableIntoBranches,
ReplaceNullWithFalseInPredicate) :: Nil
}

Expand Down Expand Up @@ -222,10 +223,17 @@ class ReplaceNullWithFalseInPredicateSuite extends PlanTest {
Literal(null, IntegerType),
Literal(3)),
FalseLiteral)
testFilter(originalCond = condition, expectedCond = condition)
testJoin(originalCond = condition, expectedCond = condition)
testDelete(originalCond = condition, expectedCond = condition)
testUpdate(originalCond = condition, expectedCond = condition)
val expectedCond = If(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to update the test name. Now it's inability to replace null in non-boolean branches of If

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or we change this test to preserve the original purpose.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed PushFoldableIntoBranches from this test suite, and add integration test to ReplaceNullWithFalseInPredicateEndToEndSuite.

UnresolvedAttribute("i") > Literal(10),
If(
UnresolvedAttribute("i") === Literal(15),
FalseLiteral,
TrueLiteral),
FalseLiteral)
testFilter(originalCond = condition, expectedCond = expectedCond)
testJoin(originalCond = condition, expectedCond = expectedCond)
testDelete(originalCond = condition, expectedCond = expectedCond)
testUpdate(originalCond = condition, expectedCond = expectedCond)
}

test("inability to replace null in non-boolean values of CaseWhen") {
Expand All @@ -238,10 +246,18 @@ class ReplaceNullWithFalseInPredicateSuite extends PlanTest {
FalseLiteral)
val branches = Seq((UnresolvedAttribute("i") > Literal(10)) -> branchValue)
val condition = CaseWhen(branches)
testFilter(originalCond = condition, expectedCond = condition)
testJoin(originalCond = condition, expectedCond = condition)
testDelete(originalCond = condition, expectedCond = condition)
testUpdate(originalCond = condition, expectedCond = condition)

val expectedCond = CaseWhen(Seq((UnresolvedAttribute("i") > Literal(10)) -> If(
CaseWhen(
Seq((UnresolvedAttribute("i") > Literal(20)) -> TrueLiteral),
FalseLiteral),
TrueLiteral,
FalseLiteral)))

testFilter(originalCond = condition, expectedCond = expectedCond)
testJoin(originalCond = condition, expectedCond = expectedCond)
testDelete(originalCond = condition, expectedCond = expectedCond)
testUpdate(originalCond = condition, expectedCond = expectedCond)
}

test("inability to replace null in non-boolean branches of If inside another If") {
Expand All @@ -252,10 +268,17 @@ class ReplaceNullWithFalseInPredicateSuite extends PlanTest {
Literal(3)),
TrueLiteral,
FalseLiteral)
testFilter(originalCond = condition, expectedCond = condition)
testJoin(originalCond = condition, expectedCond = condition)
testDelete(originalCond = condition, expectedCond = condition)
testUpdate(originalCond = condition, expectedCond = condition)
val expectedCond = If(
If(
UnresolvedAttribute("i") === Literal(15),
FalseLiteral,
TrueLiteral),
TrueLiteral,
FalseLiteral)
testFilter(originalCond = condition, expectedCond = expectedCond)
testJoin(originalCond = condition, expectedCond = expectedCond)
testDelete(originalCond = condition, expectedCond = expectedCond)
testUpdate(originalCond = condition, expectedCond = expectedCond)
}

test("replace null in If used as a join condition") {
Expand Down Expand Up @@ -375,6 +398,40 @@ class ReplaceNullWithFalseInPredicateSuite extends PlanTest {
testProjection(originalExpr = column, expectedExpr = column)
}

test("replace None of elseValue inside CaseWhen if all branches are FalseLiteral") {
val allFalseBranches = Seq(
(UnresolvedAttribute("i") < Literal(10)) -> FalseLiteral,
(UnresolvedAttribute("i") > Literal(40)) -> FalseLiteral)
val allFalseCond = CaseWhen(allFalseBranches)

val nonAllFalseBranches = Seq(
(UnresolvedAttribute("i") < Literal(10)) -> FalseLiteral,
(UnresolvedAttribute("i") > Literal(40)) -> TrueLiteral)
val nonAllFalseCond = CaseWhen(nonAllFalseBranches)

testFilter(allFalseCond, FalseLiteral)
testJoin(allFalseCond, FalseLiteral)
testDelete(allFalseCond, FalseLiteral)
testUpdate(allFalseCond, FalseLiteral)

testFilter(nonAllFalseCond, nonAllFalseCond)
testJoin(nonAllFalseCond, nonAllFalseCond)
testDelete(nonAllFalseCond, nonAllFalseCond)
testUpdate(nonAllFalseCond, nonAllFalseCond)
}

test("replace None of elseValue inside CaseWhen with PushFoldableIntoBranches") {
val allFalseBranches = Seq(
(UnresolvedAttribute("i") < Literal(10)) -> Literal("a"),
(UnresolvedAttribute("i") > Literal(40)) -> Literal("b"))
val allFalseCond = EqualTo(CaseWhen(allFalseBranches), "c")

testFilter(allFalseCond, FalseLiteral)
testJoin(allFalseCond, FalseLiteral)
testDelete(allFalseCond, FalseLiteral)
testUpdate(allFalseCond, FalseLiteral)
}

private def testFilter(originalCond: Expression, expectedCond: Expression): Unit = {
test((rel, exp) => rel.where(exp), originalCond, expectedCond)
}
Expand Down