rdtr opened a new pull request, #60480:
URL: https://github.com/apache/spark/pull/60480

   ### What changes were proposed in this pull request?
   
   `ConstantFolding` folds the children of a non-foldable conditional 
expression (`If`, `CaseWhen`, `Coalesce`, ...) with `isConditionalBranch = 
true`, but does not check the conditional again afterwards 
([expressions.scala#L87-L88](https://github.com/apache/spark/blob/00ce94fdba0d4800c3c631583436623c127415c9/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/expressions.scala#L87-L88)).
 A child can turn into a literal in that pass, for example a `ScalarSubquery` 
whose plan has `maxRows == 0` (SPARK-44527) or `size(array(a))`. The 
conditional is then foldable but stays as it is.
   
   This PR folds the conditional after its children when it has become 
foldable, like the `case other` branch does since SPARK-53360 
([expressions.scala#L116-L122](https://github.com/apache/spark/blob/00ce94fdba0d4800c3c631583436623c127415c9/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/expressions.scala#L116-L122)).
 The children are still folded with `isConditionalBranch = true`. The 
conditional itself is folded with the caller's `isConditionalBranch`, which is 
what the next run of the rule does today when it meets the foldable 
conditional. So a fold that fails throws only where a second run already threw, 
and inside another conditional it is tagged with `FAILED_TO_EVALUATE` and kept.
   
   ### Why are the changes needed?
   
   Without this, `ConstantFolding` is not idempotent, which breaks a batch that 
runs it with the `Once` strategy, as described in SPARK-53360. For example, one 
run of the rule turns
   
   ```sql
   SELECT coalesce((SELECT 1 FROM range(1) WHERE false), 2) AS c FROM range(3)
   ```
   
   into `coalesce(null, 2)`, and only a second run gives `2`. The operator 
optimization batch runs to a fixed point, so the final plan of this query is 
already `Project [2 AS c]`.
   
   The issue is also tracked as #60479 on GitHub.
   
   ### Does this PR introduce _any_ user-facing change?
   
   No. The optimizer batches that run `ConstantFolding` repeat it until the 
plan stops changing, so they already end with the folded conditional.
   
   ### How was this patch tested?
   
   Added `SPARK-61676: Fold a conditional expression whose children fold to 
literals` to `ConstantFoldingSuite`, whose `Optimize` executor runs 
`ConstantFolding` in a `Once` batch. It checks that `Coalesce`, `If` and 
`CaseWhen` over an empty `ScalarSubquery` and over `size(array(a))` fold to a 
literal. With ANSI on, it checks that a `Coalesce` that fails to fold at the 
top level throws `SparkArithmeticException`, and that the same `Coalesce` 
inside an `If` branch is kept and tagged with `FAILED_TO_EVALUATE`. Without the 
change in `expressions.scala`, the new test fails with:
   
   ```
   org.apache.spark.SparkRuntimeException: Once strategy's idempotence is 
broken for batch ConstantFolding
   !Project [coalesce(null, 2) AS o#5]          Project [2 AS o#5]
    +- LocalRelation <empty>, [a#0, b#1, c#2]   +- LocalRelation <empty>, [a#0, 
b#1, c#2].
   ```
   
   All suites in `org.apache.spark.sql.catalyst.optimizer` (catalyst module) 
pass, including `ConstantFoldingSuite`.
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   Generated-by: Claude Code (Claude Opus 5.5)


-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to