Norio Akagi created SPARK-61676:
-----------------------------------

             Summary: ConstantFolding does not fold a conditional expression 
whose children fold to literals
                 Key: SPARK-61676
                 URL: https://issues.apache.org/jira/browse/SPARK-61676
             Project: Spark
          Issue Type: Bug
          Components: SQL
    Affects Versions: 4.3.0, 4.2.0, 4.0.1, 4.1.0
            Reporter: Norio Akagi


{\{ConstantFolding}} folds the children of a non-foldable conditional 
expression (\{{If}}, \{{CaseWhen}}, \{{Coalesce}}, ...) but does not check the 
conditional itself again. A child can become a literal in that pass: a 
\{{ScalarSubquery}} whose plan has \{{maxRows == 0}} becomes a null literal 
(SPARK-44527), and \{{size(array(a))}} becomes its length. The conditional is 
then foldable but stays unfolded, so a second run of the rule changes the plan 
again. Other expressions are folded again after their children since 
SPARK-53360.

For example, one run of \{{ConstantFolding}} turns
{code:sql}
SELECT coalesce((SELECT 1 FROM range(1) WHERE false), 2) AS c FROM range(3)
{code}
into \{{coalesce(null, 2)}} instead of \{{2}}. The operator optimization batch 
runs to a fixed point, so the final plan of this query is folded. A batch that 
runs the rule with the \{{Once}} strategy, like the one in 
\{{ConstantFoldingSuite}}, fails instead:
{noformat}
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].
{noformat}

Proposed fix: after folding the children of a conditional expression, fold the 
result when it is foldable, as the other expressions do.



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

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

Reply via email to