Skip to content
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

[SPARK-14338][SQL] Improve SimplifyConditionals rule to handle null in IF/CASEWHEN #12122

Closed
wants to merge 2 commits into from
Closed

Conversation

dongjoon-hyun
Copy link
Member

What changes were proposed in this pull request?

Currently, SimplifyConditionals handles true and false to optimize branches. This PR improves SimplifyConditionals to take advantage of null conditions for if and CaseWhen expressions, too.

Before

scala> sql("SELECT IF(null, 1, 0)").explain()
== Physical Plan ==
WholeStageCodegen
:  +- Project [if (null) 1 else 0 AS (IF(CAST(NULL AS BOOLEAN), 1, 0))#4]
:     +- INPUT
+- Scan OneRowRelation[]
scala> sql("select case when cast(null as boolean) then 1 else 2 end").explain()
== Physical Plan ==
WholeStageCodegen
:  +- Project [CASE WHEN null THEN 1 ELSE 2 END AS CASE WHEN CAST(NULL AS BOOLEAN) THEN 1 ELSE 2 END#14]
:     +- INPUT
+- Scan OneRowRelation[]

After

scala> sql("SELECT IF(null, 1, 0)").explain()
== Physical Plan ==
WholeStageCodegen
:  +- Project [0 AS (IF(CAST(NULL AS BOOLEAN), 1, 0))#4]
:     +- INPUT
+- Scan OneRowRelation[]
scala> sql("select case when cast(null as boolean) then 1 else 2 end").explain()
== Physical Plan ==
WholeStageCodegen
:  +- Project [2 AS CASE WHEN CAST(NULL AS BOOLEAN) THEN 1 ELSE 2 END#4]
:     +- INPUT
+- Scan OneRowRelation[]

Hive

hive> select if(null,1,2);
OK
2
hive> select case when cast(null as boolean) then 1 else 2 end;
OK
2

How was this patch tested?

Pass the Jenkins tests (including new extended test cases).

@dongjoon-hyun
Copy link
Member Author

deep-review this please

@@ -33,6 +33,8 @@ object Literal {

val FalseLiteral: Literal = Literal(false, BooleanType)

val NullLiteral: Literal = Literal(null, NullType)

Choose a reason for hiding this comment

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

Detected duplicate pattern.

We should replace Literal(null, NullType) in L55 with NullLiteral.

@DeepSparkBot
Copy link

@dongjoon-hyun Review complete. No major issues found.

This matches Hive semantics, where SELECT if(null, 1, 2) returns 2. However, this prevents a future optimization where if(anything, TrueLiteral, FalseLiteral) can be simplified to anything.

This requires human decision. @JoshRosen @sameeragarwal @yhuai Please advise.

@SparkQA
Copy link

SparkQA commented Apr 2, 2016

Test build #54743 has finished for PR 12122 at commit 22bb31f.

  • This patch fails Spark unit tests.
  • This patch merges cleanly.
  • This patch adds no public classes.

@SparkQA
Copy link

SparkQA commented Apr 2, 2016

Test build #54747 has finished for PR 12122 at commit 512a45f.

  • This patch fails Spark unit tests.
  • This patch merges cleanly.
  • This patch adds no public classes.

@dongjoon-hyun
Copy link
Member Author

Rebased and squashed.

@SparkQA
Copy link

SparkQA commented Apr 2, 2016

Test build #54758 has finished for PR 12122 at commit dcbb0ea.

  • This patch fails Spark unit tests.
  • This patch merges cleanly.
  • This patch adds no public classes.

@dongjoon-hyun
Copy link
Member Author

It's strange.
org.apache.spark.sql.execution.streaming.state.StateStoreSuite.maintenance test fails here and at my other PR frequently.

@SparkQA
Copy link

SparkQA commented Apr 2, 2016

Test build #2729 has finished for PR 12122 at commit dcbb0ea.

  • This patch fails Spark unit tests.
  • This patch merges cleanly.
  • This patch adds no public classes.

@@ -773,17 +773,24 @@ object BooleanSimplification extends Rule[LogicalPlan] with PredicateHelper {
* Simplifies conditional expressions (if / case).
*/
object SimplifyConditionals extends Rule[LogicalPlan] with PredicateHelper {
def falseOrNullLiteral(e: Expression): Boolean = e match {
Copy link
Contributor

Choose a reason for hiding this comment

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

private def

@rxin
Copy link
Contributor

rxin commented Apr 2, 2016

LGTM pending Jenkins

@dongjoon-hyun
Copy link
Member Author

Thank you, @rxin .
I fixed that to private def, too.

@SparkQA
Copy link

SparkQA commented Apr 2, 2016

Test build #2730 has finished for PR 12122 at commit dcbb0ea.

  • This patch passes all tests.
  • This patch merges cleanly.
  • This patch adds no public classes.

@SparkQA
Copy link

SparkQA commented Apr 2, 2016

Test build #54768 has finished for PR 12122 at commit d8c3f38.

  • This patch fails Spark unit tests.
  • This patch merges cleanly.
  • This patch adds no public classes.

@dongjoon-hyun
Copy link
Member Author

The last failure is also org.apache.spark.sql.execution.streaming.state.StateStoreSuite.maintenance. It's irrelevant to this PR.

@dongjoon-hyun
Copy link
Member Author

Rebased.

@SparkQA
Copy link

SparkQA commented Apr 2, 2016

Test build #54780 has finished for PR 12122 at commit 2870d4e.

  • This patch fails Spark unit tests.
  • This patch merges cleanly.
  • This patch adds no public classes.

@SparkQA
Copy link

SparkQA commented Apr 2, 2016

Test build #2733 has finished for PR 12122 at commit 2870d4e.

  • This patch passes all tests.
  • This patch merges cleanly.
  • This patch adds no public classes.

@rxin
Copy link
Contributor

rxin commented Apr 3, 2016

Thanks - merging in master.

@asfgit asfgit closed this in f705037 Apr 3, 2016
@yhuai
Copy link
Contributor

yhuai commented Apr 3, 2016

@dongjoon-hyun Can you take a look at the scala 2.10 build?

@rxin
Copy link
Contributor

rxin commented Apr 3, 2016

I think I fixed it just now.

@dongjoon-hyun
Copy link
Member Author

Oh, sorry for breaking Scala 2.10 build and late response, @yhuai and @rxin .
Thank you so much for hot fix, @rxin !

@dongjoon-hyun dongjoon-hyun deleted the SPARK-14338 branch May 12, 2016 00:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
5 participants