-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-29162][SQL] Simplify NOT(IsNull(x)) and NOT(IsNotNull(x)) #25878
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 6 commits
fa8b240
a701dca
978a284
f139558
9bf2d7c
a75c2ef
1fdea48
cb9cef2
9f5defa
f71698d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,6 +18,7 @@ | |
| package org.apache.spark.sql.catalyst.optimizer | ||
|
|
||
| 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._ | ||
| import org.apache.spark.sql.catalyst.expressions.Literal.{FalseLiteral, TrueLiteral} | ||
|
|
@@ -40,10 +41,19 @@ class SimplifyConditionalSuite extends PlanTest with PredicateHelper { | |
| comparePlans(actual, correctAnswer) | ||
| } | ||
|
|
||
| private def assertFilter(originalExpr: Expression, expectedExpr: Expression): Unit = { | ||
| val originalPlan = testRelation.where(originalExpr).analyze | ||
| val optimizedPlan = Optimize.execute(originalPlan) | ||
| val expectedPlan = testRelation.where(expectedExpr).analyze | ||
| comparePlans(optimizedPlan, expectedPlan) | ||
| } | ||
|
|
||
| private val trueBranch = (TrueLiteral, Literal(5)) | ||
| private val normalBranch = (NonFoldableLiteral(true), Literal(10)) | ||
| private val unreachableBranch = (FalseLiteral, Literal(20)) | ||
| private val nullBranch = (Literal.create(null, NullType), Literal(30)) | ||
| private val testRelation = | ||
| LocalRelation('i.int, 'b.boolean, 'a.array(IntegerType), 'm.map(IntegerType, IntegerType)) | ||
|
|
||
| val isNotNullCond = IsNotNull(UnresolvedAttribute(Seq("a"))) | ||
| val isNullCond = IsNull(UnresolvedAttribute("b")) | ||
|
|
@@ -166,4 +176,9 @@ class SimplifyConditionalSuite extends PlanTest with PredicateHelper { | |
| Literal(1)) | ||
| ) | ||
| } | ||
|
|
||
| test("simplify NOT(IsNull(x)) and NOT(IsNotNull(x))") { | ||
| assertFilter(Not(IsNotNull(UnresolvedAttribute("b"))), IsNull(UnresolvedAttribute("b"))) | ||
|
||
| assertFilter(Not(IsNull(UnresolvedAttribute("b"))), IsNotNull(UnresolvedAttribute("b"))) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3192,6 +3192,19 @@ class SQLQuerySuite extends QueryTest with SharedSparkSession { | |
| checkAnswer(df3, Array(Row(new java.math.BigDecimal("0.100000000000000000000000100")))) | ||
| } | ||
| } | ||
|
|
||
| test("SPARK-29152: Simplify NOT(IsNull(x)) and NOT(IsNotNull(x))") { | ||
|
||
| withTempView("tbl1") { | ||
| val df: DataFrame = | ||
| Seq[java.lang.Boolean](true, false, true, null, false, null, true).toDF("id") | ||
| df.createOrReplaceTempView("tbl1") | ||
| val query1 = sql("select id from tbl1 where not(isnull(id) or id == false)") | ||
| val query2 = sql("select id from tbl1 where not(isnotnull(id) and id == true) ") | ||
|
|
||
| checkAnswer(query1, Row(true) :: Row(true) :: Row(true) :: Nil) | ||
| checkAnswer(query2, Row(false) :: Row(null) :: Row(false) :: Row(null) :: Nil) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| case class Foo(bar: Option[String]) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit.
expr->e.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Better!