dongjoon-hyun opened a new pull request, #60645: URL: https://github.com/apache/spark/pull/60645
### What changes were proposed in this pull request? This PR aims to fix MERGE failing on a `NULLIF` or `BETWEEN` in a clause that the failing row does not select. `RewriteWithExpression` now treats a MERGE instruction (`MergeRows.Keep`, `Discard`, `Split`) like a conditional branch. A `With` in an instruction's condition or outputs stays in the instruction and memoizes its definition there, and only the definitions that gain nothing from memoizing (cheap and deterministic, or referenced once) are inlined. Previously an instruction was handled like any other expression, so a definition referenced more than once and not cheap was pre-evaluated in a `Project` under `MergeRows`. ### Why are the changes needed? `MergeRowsExec` evaluates an instruction only for the rows of its kind (matched, not matched, or not matched by source) that no earlier instruction of that kind took, and its outputs only for the rows its condition selects. `NULLIF` and `BETWEEN` wrap their first operand in a `With`, and the `Project` under `MergeRows` evaluated that operand for every joined row. So a MERGE failed on a row that never selects the clause. ```sql -- ANSI mode; target t(pk, salary, dep) has rows with pk 1 and 2, source s(pk, x) = (1, 0), (2, 20) MERGE INTO t USING s ON t.pk = s.pk WHEN MATCHED AND s.x = 0 THEN DELETE WHEN MATCHED AND (100 DIV s.x) BETWEEN 1 AND 10 THEN UPDATE SET salary = s.x; -- [DIVIDE_BY_ZERO] although the row with x = 0 is deleted by the first clause MERGE INTO t USING s ON t.pk = s.pk WHEN MATCHED AND s.x > 0 THEN UPDATE SET salary = nullif(100 DIV s.x, 0); -- [DIVIDE_BY_ZERO] although the row with x = 0 does not select the clause ``` In a group-based MERGE, the `Project` also evaluated the operand for the target rows that are copied over unchanged. This is a regression in Apache Spark [v4.0.0](https://github.com/apache/spark/releases/tag/v4.0.0) (2025-05-19), where `NULLIF` (SPARK-45760) and `BETWEEN` (SPARK-46366) started to use `With`. Apache Spark [v3.5.0](https://github.com/apache/spark/releases/tag/v3.5.0) (2023-09-09) duplicated the operand inside the clause instead. `With` can be evaluated since SPARK-58818, so a kept `With` is evaluated only where its clause is. ### Does this PR introduce _any_ user-facing change? Yes. The MERGE statements above succeed instead of failing. A `NULLIF` or `BETWEEN` operand in a MERGE clause is now evaluated only for the rows that reach the clause, still at most once per row. ### How was this patch tested? Pass the CIs with the newly added test cases. - `RewriteWithExpressionSuite`: a `With` in a MERGE instruction stays in the instruction instead of a `Project` under `MergeRows`. - `MergeIntoTableSuiteBase`: `BETWEEN` in a clause after a `DELETE` clause, and `NULLIF` in an assignment of a clause that the row does not select, in the group-based and delta-based suites with and without whole-stage codegen. ### Was this patch authored or co-authored using generative AI tooling? Generated-by: 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]
