yihua opened a new pull request, #20846:
URL: https://github.com/apache/hudi/pull/20846

   ### Describe the issue this Pull Request addresses
   
   closes #20845
   
   A Spark SQL `MERGE INTO` never fails when more than one source row matches 
the same target row. Hudi combines those rows into one update, and without an 
ordering field the surviving row depends on the shuffle order, while the SQL 
standard and Spark raise `MERGE_CARDINALITY_VIOLATION`.
   
   ### Summary and Changelog
   
   `MERGE INTO` now fails when several source rows match one target row, with 
Spark's `MERGE_CARDINALITY_VIOLATION` error on Spark 3.5+.
   
   - New write config `hoodie.upsert.fail.on.multiple.matches` (default false): 
the upsert checks after the index lookup that no two incoming records with one 
key are located at the same row (file group and position), and combines the 
records of keys the table does not hold yet there instead of before the lookup. 
The merged lookup of a global index, which keeps one located record per key, 
does the same check. The bucket index is excluded since it locates new keys too.
   - New SQL config `hoodie.spark.sql.merge.into.cardinality.check` (default 
true) turns the write config on for statements with a WHEN MATCHED clause other 
than a single unconditional DELETE.
   - `SparkAdapter#mergeCardinalityViolation` raises Spark's error: as the 
cause on Spark 3.5, as the thrown error on Spark 4, as Spark does for its own 
tables. Spark 3.3/3.4 keep the `HoodieMultipleMatchesException`.
   - `TestMergeIntoCardinalityCheck` covers COW and MOR, with and without a 
primary key and ordering field, and SIMPLE and GLOBAL_SIMPLE indexes. Three 
existing tests that relied on the combining now turn the check off.
   
   ### Impact
   
   A `MERGE INTO` whose source has several rows for one target row now fails by 
default. Set `hoodie.spark.sql.merge.into.cardinality.check=false` to keep 
combining them. A key deleted in a log file but still in a base file is located 
by the SIMPLE and BLOOM indexes, so duplicate source rows for it also fail 
until compaction.
   
   ### Risk Level
   
   medium. The check is on by default for MERGE INTO and adds a shuffle of the 
tagged records by key; the suites that run MERGE INTO pass.
   
   ### Documentation Update
   
   The two new configs carry their documentation. The MERGE INTO page could 
mention the new default.
   
   ### Contributor's checklist
   
   - [ ] Read through [contributor's 
guide](https://hudi.apache.org/contribute/how-to-contribute)
   - [ ] Enough context is provided in the sections above
   - [ ] Adequate tests were added if applicable
   


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

Reply via email to