dongjoon-hyun commented on a change in pull request #30953:
URL: https://github.com/apache/spark/pull/30953#discussion_r549528900
##########
File path:
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/optimizer/SimplifyConditionalSuite.scala
##########
@@ -201,19 +201,39 @@ class SimplifyConditionalSuite extends PlanTest with
ExpressionEvalHelper with P
}
test("SPARK-33845: remove unnecessary if when the outputs are boolean type")
{
- assertEquivalent(
- If(IsNotNull(UnresolvedAttribute("a")), TrueLiteral, FalseLiteral),
- IsNotNull(UnresolvedAttribute("a")))
- assertEquivalent(
- If(IsNotNull(UnresolvedAttribute("a")), FalseLiteral, TrueLiteral),
- IsNull(UnresolvedAttribute("a")))
+ // verify the boolean equivalence of all transformations involved
+ val fields = Seq(
+ 'cond.boolean.notNull,
+ 'cond_nullable.boolean,
+ 'a.boolean,
+ 'b.boolean
+ )
+ val Seq(cond, cond_nullable, a, b) = fields.zipWithIndex.map { case (f, i)
=> f.at(i) }
+
+ val exprs = Seq(
+ // actual expressions of the transformations: original -> transformed
+ If(cond, true, false) -> cond,
+ If(cond, false, true) -> !cond,
+ If(cond_nullable, true, false) -> (cond_nullable <=> true),
+ If(cond_nullable, false, true) -> (!(cond_nullable <=> true)))
+
+ // check plans
+ for ((originalExpr, expectedExpr) <- exprs) {
+ assertEquivalent(originalExpr, expectedExpr)
+ }
- assertEquivalent(
- If(GreaterThan(Rand(0), UnresolvedAttribute("a")), TrueLiteral,
FalseLiteral),
- GreaterThan(Rand(0), UnresolvedAttribute("a")))
- assertEquivalent(
- If(GreaterThan(Rand(0), UnresolvedAttribute("a")), FalseLiteral,
TrueLiteral),
- LessThanOrEqual(Rand(0), UnresolvedAttribute("a")))
+ // check evaluation
+ val binaryBooleanValues = Seq(true, false)
+ val ternaryBooleanValues = Seq(true, false, null)
+ for (condVal <- binaryBooleanValues;
+ condNullableVal <- ternaryBooleanValues;
+ aVal <- ternaryBooleanValues;
+ bVal <- ternaryBooleanValues;
+ (originalExpr, expectedExpr) <- exprs) {
+ val inputRow = create_row(condVal, condNullableVal, aVal, bVal)
+ val optimizedVal = evaluateWithoutCodegen(expectedExpr, inputRow)
+ checkEvaluation(originalExpr, optimizedVal, inputRow)
Review comment:
Thanks!
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]