szehon-ho commented on PR #60559:
URL: https://github.com/apache/spark/pull/60559#issuecomment-6978626543
Thanks for the review. I'll go back to the original approach: make the
single-clause insert-only decision in `RewriteMergeIntoTable`, keeping the
existing projection path when its constants fold safely.
- Run `ReplaceExpressions`, `ComputeCurrentTime`, `SpecialDatetimeValues`,
and `ConstantFolding` on copies of the INSERT action expressions.
- Keep the existing `Project` and source-side `Filter` when the check
succeeds. If folding fails, use `MergeRows` so the action is evaluated only for
unmatched rows. Failed folds retained inside a conditional clause condition
also need this fallback, because a pushed filter would evaluate that condition
for matched source rows.
The check leaves the original planned expressions intact. This preserves the
existing analysis path for subqueries and window functions, and keeps normal
source-side pushdown without the new optimizer rule.
For example, with ANSI mode enabled and both tables containing only `id = 1`:
```sql
MERGE INTO target t
USING source s ON t.id = s.id
WHEN NOT MATCHED THEN
INSERT (id, amount) VALUES (s.id, 1 DIV 0)
```
`WHEN NOT MATCHED` is itself the conditional guard: the source row matches,
so the INSERT should never run. Rewriting it as `Project(1 DIV 0,
LeftAntiJoin(...))` lets constant folding evaluate the division before the join
removes that row. Ordinary projections also fold constants eagerly; the MERGE
action needs its conditional evaluation preserved.
The early check only sees constants already present in the action. I'll
document the remaining limitation from #5, where later optimizer rewrites
introduce failing constants.
---
<!-- GITHUB_MCP_FOOTER: This attribution is automatically appended by GitHub
MCP. -->
_This comment was generated with [GitHub MCP](http://go/mcps)._
--
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]