Skip to content
Prev Previous commit
Next Next commit
address comments
  • Loading branch information
wangyum committed Dec 16, 2020
commit 55f4528e7e61629e2194adb055f3ba52ae4a875b
Original file line number Diff line number Diff line change
Expand Up @@ -203,16 +203,17 @@ class SimplifyConditionalSuite extends PlanTest with ExpressionEvalHelper with P
test("SPARK-33798: simplify EqualTo(If, Literal) always false") {
val a = EqualTo(UnresolvedAttribute("a"), Literal(100))
val b = UnresolvedAttribute("b")
val ifExp = If(a === Literal(1), Literal(2), Literal(3))
val ifExp = If(a, Literal(2), Literal(3))

assertEquivalent(EqualTo(ifExp, Literal(4)), FalseLiteral)
assertEquivalent(EqualTo(ifExp, Literal(3)), EqualTo(ifExp, Literal(3)))
assertEquivalent(EqualTo(ifExp, Literal("4")), FalseLiteral)
Copy link
Contributor

Choose a reason for hiding this comment

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

ditto, let's don't test invalid expressions, ifExp.dataType is int, not string.

assertEquivalent(EqualTo(ifExp, Literal("3")), EqualTo(ifExp, Literal(3)))

// Do not simplify if it contains non foldable expressions.
assertEquivalent(EqualTo(If(a === Literal(1), b, Literal(2)), Literal(3)),
EqualTo(If(a === Literal(1), b, Literal(2)), Literal(3)))
assertEquivalent(
EqualTo(If(a, b, Literal(2)), Literal(3)),
EqualTo(If(a, b, Literal(2)), Literal(3)))
val nonFoldable = If(NonFoldableLiteral(true), Literal(1), Literal(2))
Copy link
Contributor

Choose a reason for hiding this comment

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

NonFoldableLiteral is nothing special comparing to 'a == 100, as they are both non-foldable. I think we don't need to test with NonFoldableLiteral

assertEquivalent(EqualTo(nonFoldable, Literal(1)), EqualTo(nonFoldable, Literal(1)))

Expand All @@ -223,20 +224,19 @@ class SimplifyConditionalSuite extends PlanTest with ExpressionEvalHelper with P

// Should not handle Null values.
assertEquivalent(
EqualTo(If(a === Literal(1), Literal(null, IntegerType), Literal(1)), Literal(2)),
EqualTo(If(a === Literal(1), Literal(null, IntegerType), Literal(1)), Literal(2)))
EqualTo(If(a, Literal(null, IntegerType), Literal(1)), Literal(2)),
EqualTo(If(a, Literal(null, IntegerType), Literal(1)), Literal(2)))
assertEquivalent(
EqualTo(If(a =!= Literal(1), Literal(1), Literal(2)), Literal(null, IntegerType)),
EqualTo(If(a =!= Literal(1), Literal(1), Literal(2)), Literal(null, IntegerType)))
EqualTo(If(!a, Literal(1), Literal(2)), Literal(null, IntegerType)),
Copy link
Contributor

Choose a reason for hiding this comment

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

as a unit test, we know that we only care if the condition is foldable or not. I think we don't need to test !a as it doesn't improve test coverage.

EqualTo(If(!a, Literal(1), Literal(2)), Literal(null, IntegerType)))
assertEquivalent(
EqualTo(If(a === Literal(1), Literal(null, IntegerType), Literal(1)), Literal(1)),
EqualTo(If(a === Literal(1), Literal(null, IntegerType), Literal(1)), Literal(1)))
EqualTo(If(a, Literal(null, IntegerType), Literal(1)), Literal(1)),
EqualTo(If(a, Literal(null, IntegerType), Literal(1)), Literal(1)))
assertEquivalent(
EqualTo(If(a =!= Literal(1), Literal(null, IntegerType), Literal(1)), Literal(1)),
EqualTo(If(a =!= Literal(1), Literal(null, IntegerType), Literal(1)), Literal(1)))
EqualTo(If(!a, Literal(null, IntegerType), Literal(1)), Literal(1)),
EqualTo(If(!a, Literal(null, IntegerType), Literal(1)), Literal(1)))
assertEquivalent(
EqualTo(If(a =!= Literal(1), Literal(null, IntegerType), Literal(null, IntegerType)),
Literal(1)),
EqualTo(If(!a, Literal(null, IntegerType), Literal(null, IntegerType)), Literal(1)),
Literal(null, BooleanType))
}

Expand Down