Yang Jie created SPARK-62323:
--------------------------------

             Summary: Fix wrong results from NULLIF or BETWEEN in a lambda 
function
                 Key: SPARK-62323
                 URL: https://issues.apache.org/jira/browse/SPARK-62323
             Project: Spark
          Issue Type: Bug
          Components: SQL
    Affects Versions: 5.0.0
            Reporter: Yang Jie


NULLIF and BETWEEN build a `With`. `RewriteWithExpression` either evaluates its 
definition in a `Project` under the operator, or, when no child can evaluate 
it, inlines it into every reference. In a lambda function both give wrong 
results:

- A definition that reads a lambda variable is inlined, so a nondeterministic 
one is evaluated once per reference. `SELECT count_if(v) FROM (SELECT 
explode(transform(sequence(1, 2000), x -> nullif(rand() + x * 0 > 0.5, true))) 
AS v)` returns about 500, although `nullif(_, true)` never returns true. With 
`rand() + x * 0 BETWEEN 0.6 AND 0.4` it returns about 300 instead of 0.
- A definition that reads no lambda variable is evaluated once per row, not 
once per element, and also for an empty array. `SELECT 
size(array_distinct(transform(sequence(1, 100), x -> nullif(rand(), 2))))` 
returns 1 instead of 100, and `transform(arr, x -> nullif(a / b, 1))` fails 
with DIVIDE_BY_ZERO on a row where `arr` is empty and `b` is 0, as 
`transform(arr, x -> a / b)` does not.

A lambda function is evaluated once per element, or not at all, so a `With` in 
it should stay where it is and memoize its definition per evaluation, as one in 
a conditional branch does since SPARK-58818.




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