You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by "averyqi-db (via GitHub)" <gi...@apache.org> on 2024/08/26 19:50:50 UTC

[PR] [SPARK-49396][SQL] remain nullability while simplifyConditional [spark]

averyqi-db opened a new pull request, #47877:
URL: https://github.com/apache/spark/pull/47877

   <!--
   Thanks for sending a pull request!  Here are some tips for you:
     1. If this is your first time, please read our contributor guidelines: https://spark.apache.org/contributing.html
     2. Ensure you have added or run the appropriate tests for your PR: https://spark.apache.org/developer-tools.html
     3. If the PR is unfinished, add '[WIP]' in your PR title, e.g., '[WIP][SPARK-XXXX] Your PR title ...'.
     4. Be sure to keep the PR description updated to reflect all changes.
     5. Please write your PR title to summarize what this PR proposes.
     6. If possible, provide a concise example to reproduce the issue for a faster review.
     7. If you want to add a new configuration, please read the guideline first for naming configurations in
        'core/src/main/scala/org/apache/spark/internal/config/ConfigEntry.scala'.
     8. If you want to add or modify an error type or message, please read the guideline first in
        'common/utils/src/main/resources/error/README.md'.
   -->
   
   ### What changes were proposed in this pull request?
   `SimplifyConditionals` has a simplification code where if any of the (not first) branches in `CaseWhen` is `TrueLiteral` we remove all the remaining branches including the elseValue.
   
   ```
   case CaseWhen(branches, ) if branches.exists(._1 == TrueLiteral) => // a branch with a true condition eliminates all following branches,
   // these branches can be pruned away
   val (h, t) = branches.span(_._1 != TrueLiteral)
   CaseWhen( h :+ t.head, None)
   ```
   Now, the nullability check of CaseWhen checks that
   (1) either of the branches including elseValue is nullable or
   (2) elseValue is None.
   
   The above simplification makes the elseValue as None. Combined with this nullability check makes the CaseWhen switch from non-nullable to nullable.
   
   This ticket aims to fix this issue and remain the nullability of the expression by replacing the elsevalue to the value of the TrueLiteral expression.
   
   ### Why are the changes needed?
   See the section above 
   
   ### Does this PR introduce _any_ user-facing change?
   <!--
   Note that it means *any* user-facing change including all aspects such as the documentation fix.
   If yes, please clarify the previous behavior and the change this PR proposes - provide the console output, description and/or an example to show the behavior difference if possible.
   If possible, please also clarify if this is a user-facing change compared to the released Spark versions or within the unreleased branches such as master.
   If no, write 'No'.
   -->
   no
   
   ### How was this patch tested?
   <!--
   If tests were added, say they were added here. Please make sure to add some test cases that check the changes thoroughly including negative and positive cases if possible.
   If it was tested in a way different from regular unit tests, please clarify how you tested step by step, ideally copy and paste-able, so that other reviewers can test and check, and descendants can verify in the future.
   If tests were not added, please describe why they were not added and/or why it was difficult to add.
   If benchmark tests were added, please run the benchmarks in GitHub Actions for the consistent environment, and the instructions could accord to: https://spark.apache.org/developer-tools.html#github-workflow-benchmarks.
   -->
   unit tests
   
   ### Was this patch authored or co-authored using generative AI tooling?
   <!--
   If generative AI tooling has been used in the process of authoring this patch, please include the
   phrase: 'Generated-by: ' followed by the name of the tool and its version.
   If no, write 'No'.
   Please refer to the [ASF Generative Tooling Guidance](https://www.apache.org/legal/generative-tooling.html) for details.
   -->
   no
   


-- 
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.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-49396][SQL] remain nullability while simplifyConditional [spark]

Posted by "averyqi-db (via GitHub)" <gi...@apache.org>.
averyqi-db closed pull request #47877: [SPARK-49396][SQL] remain nullability while simplifyConditional
URL: https://github.com/apache/spark/pull/47877


-- 
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.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-49396][SQL] remain nullability while simplifyConditional [spark]

Posted by "averyqi-db (via GitHub)" <gi...@apache.org>.
averyqi-db commented on PR #47877:
URL: https://github.com/apache/spark/pull/47877#issuecomment-2333069404

   Closed as alternative method https://github.com/apache/spark/pull/47981 is merged.


-- 
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.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-49396][SQL] remain nullability while simplifyConditional [spark]

Posted by "averyqi-db (via GitHub)" <gi...@apache.org>.
averyqi-db commented on PR #47877:
URL: https://github.com/apache/spark/pull/47877#issuecomment-2319152250

   cc: @cloud-fan for review and merge


-- 
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.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-49396][SQL] remain nullability while simplifyConditional [spark]

Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #47877:
URL: https://github.com/apache/spark/pull/47877#discussion_r1740486656


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/expressions.scala:
##########
@@ -598,7 +598,9 @@ object SimplifyConditionals extends Rule[LogicalPlan] {
         // a branch with a true condition eliminates all following branches,
         // these branches can be pruned away
         val (h, t) = branches.span(_._1 != TrueLiteral)
-        CaseWhen( h :+ t.head, None)
+        // Use the value of TrueLiteral expression as elseValue to maintain
+        // nullability of CaseWhen expressions
+        CaseWhen(h :+ t.head, t.head._2)

Review Comment:
   shall we update `CaseWhen#nullable` to consider conditions that are true literal? My point is we should keep the expression tree small for the good of optimizer, and the else value will never be used here.



-- 
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.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-49396][SQL] remain nullability while simplifyConditional [spark]

Posted by "kelvinjian-db (via GitHub)" <gi...@apache.org>.
kelvinjian-db commented on code in PR #47877:
URL: https://github.com/apache/spark/pull/47877#discussion_r1731808873


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/expressions.scala:
##########
@@ -598,7 +598,7 @@ object SimplifyConditionals extends Rule[LogicalPlan] {
         // a branch with a true condition eliminates all following branches,
         // these branches can be pruned away
         val (h, t) = branches.span(_._1 != TrueLiteral)
-        CaseWhen( h :+ t.head, None)

Review Comment:
   also, can we add a comment explaining why we need to populate the else value here? 



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/expressions.scala:
##########
@@ -598,7 +598,7 @@ object SimplifyConditionals extends Rule[LogicalPlan] {
         // a branch with a true condition eliminates all following branches,
         // these branches can be pruned away
         val (h, t) = branches.span(_._1 != TrueLiteral)
-        CaseWhen( h :+ t.head, None)

Review Comment:
   nit: fix extra space?



-- 
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.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-49396][SQL] remain nullability while simplifyConditional [spark]

Posted by "averyqi-db (via GitHub)" <gi...@apache.org>.
averyqi-db commented on PR #47877:
URL: https://github.com/apache/spark/pull/47877#issuecomment-2310962947

   cc: @nikhilsheoran-db @kelvinjian-db for review


-- 
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.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-49396][SQL] remain nullability while simplifyConditional [spark]

Posted by "averyqi-db (via GitHub)" <gi...@apache.org>.
averyqi-db commented on code in PR #47877:
URL: https://github.com/apache/spark/pull/47877#discussion_r1742547158


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/expressions.scala:
##########
@@ -598,7 +598,9 @@ object SimplifyConditionals extends Rule[LogicalPlan] {
         // a branch with a true condition eliminates all following branches,
         // these branches can be pruned away
         val (h, t) = branches.span(_._1 != TrueLiteral)
-        CaseWhen( h :+ t.head, None)
+        // Use the value of TrueLiteral expression as elseValue to maintain
+        // nullability of CaseWhen expressions
+        CaseWhen(h :+ t.head, t.head._2)

Review Comment:
   Got it. Yes, it is a good idea to change null check instead. I'll investigate this path.



-- 
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.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org