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

   ### Describe the issue this Pull Request addresses
   
   closes #20889
   
   A Spark SQL `INSERT INTO`, or a datasource write with the `insert` 
operation, keeps rows that share a record key on a table with a record key. 
`hoodie.datasource.insert.dup.policy` defaults to `none`, and `fail` only looks 
the incoming keys up in the table, so two incoming rows with one key pass even 
then.
   
   ### Summary and Changelog
   
   An insert into a table of a non-native table format (`hoodie.table.format` 
other than `native`) with a record key that sets no duplicate policy now fails 
with `HoodieDuplicateKeyException` when a key repeats among the incoming 
records, or, with a built record level index, is already in the table; `fail` 
checks the incoming records the same way, and the message names the settings 
that lift the check. The check runs in jobs of its own before the write (a 
keys-only shuffle, plus an index lookup on a view of the persisted input), so 
the write gets the records in their original order and the source is read once. 
Without a policy the table is looked up only through a built record level 
index, so the default adds no index lookup of the table with other indexes. 
`TestInsertDedupPolicy` and `TestInsertTable5` cover SQL and datasource inserts 
on COW and MOR, duplicates within a batch and against the table, the explicit 
policies, upsert, keyless tables and the record order in the base files;
  native tables keep master's default, which a test pins.
   
   ### Impact
   
   Behaviour change for tables of a non-native table format only: an insert 
with a record key and no duplicate policy fails when a key repeats within the 
batch, through Spark SQL `INSERT INTO`, CTAS, datasource writes and structured 
streaming with `operation=insert` (`HoodieStreamingSink`, where a micro-batch 
that repeats a key fails). A key the table already holds fails by default only 
with the record level index once it is built; with SIMPLE, BLOOM, BUCKET or a 
record level index not yet built it is written as a duplicate unless the writer 
opts in with `hoodie.datasource.insert.dup.policy=fail`, which looks the table 
up on every index. Native tables keep the duplicates by default, as before. An 
explicit policy always wins: `none` keeps duplicates, `drop` drops existing 
keys, and `fail` now also fails on a key repeated within the batch (on native 
tables too) and surfaces as `HoodieInsertException` caused by 
`HoodieDuplicateKeyException` (it threw `HoodieUpsertException` before). `ho
 odie.combine.before.insert=true` still combines the incoming records first. 
Not covered: tables without a record key, `upsert`, `bulk_insert`, 
`insert_overwrite`, the legacy `hoodie.sql.insert.mode` flow, and writers that 
do not go through `HoodieSparkSqlWriter`. Cost, where the check runs: one 
keys-only shuffle per keyed insert plus persisting the input (read once); the 
table lookup runs only with a built record level index or the `fail` policy.
   
   ### Risk Level
   
   low. The default changes only for tables of a non-native format; the error 
surfaces before the write starts, and `none` restores the old behaviour.
   
   ### Documentation Update
   
   Needs a release note for the new default on non-native tables and the 
exception type of the `fail` policy (see Impact). The 
`hoodie.datasource.insert.dup.policy` description is updated in this PR.
   
   ### 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