dongjoon-hyun opened a new pull request, #60742:
URL: https://github.com/apache/spark/pull/60742

   ### What changes were proposed in this pull request?
   
   This PR fixes `MERGE INTO` on a DSv2 table failing analysis when its only 
clause is `WHEN NOT MATCHED` with a `NULL` condition.
   
   `RewriteMergeIntoTable` rewrites such a `MERGE` into an append of the source 
rows that match no target row, and puts the clause condition into a `Filter` on 
the source. A `NULL` literal has the `VOID` type, and `CheckAnalysis` rejects a 
`Filter` whose condition is not `BOOLEAN`. Now a `VOID` condition is cast to 
`BOOLEAN` before it goes into the `Filter`. So the `Filter` drops every row, as 
`MergeRows` does for a `NULL` condition in every other form of `MERGE`.
   
   A `NULL` `ON` condition is not changed. Every form of `MERGE` rejects it, as 
`JOIN ... ON NULL` does.
   
   ### Why are the changes needed?
   
   Every other form of `MERGE` accepts a `NULL` clause condition and selects no 
rows with it, as the existing test `merge with NULL clause conditions` checks. 
Only a `MERGE` whose single clause is `WHEN NOT MATCHED` fails. This has been 
the case since SPARK-43885 added this rewrite in Apache Spark 
[v3.5.0](https://github.com/apache/spark/releases/tag/v3.5.0) (2023-09-09).
   
   ```sql
   -- t (pk INT NOT NULL, salary BIGINT, dep STRING) has (1, 100, 'hr')
   -- source (pk, salary, dep) has (1, 10, 'hr') and (2, 20, 'hr')
   
   -- Issue: It should leave t unchanged, but it fails with 
DATATYPE_MISMATCH.FILTER_NOT_BOOLEAN.
   MERGE INTO t USING source s ON t.pk = s.pk
   WHEN NOT MATCHED AND NULL THEN INSERT *;
   
   -- With another clause, the same NULL condition is accepted, and t is 
unchanged.
   MERGE INTO t USING source s ON t.pk = s.pk
   WHEN NOT MATCHED AND NULL THEN INSERT *
   WHEN NOT MATCHED AND s.pk < 0 THEN INSERT *;
   ```
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes. A `MERGE` whose only clause is `WHEN NOT MATCHED` with a `NULL` 
condition now succeeds and inserts no rows, instead of failing with 
`DATATYPE_MISMATCH.FILTER_NOT_BOOLEAN`.
   
   ### How was this patch tested?
   
   Pass the CIs with the newly added test case, which fails without this fix.
   
   ### 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]

Reply via email to