voonhous commented on PR #20895: URL: https://github.com/apache/hudi/pull/20895#issuecomment-7213494208
> On `bb69ac7`, `MERGE ... ON false` writes two rows with the same composite key `(8, NULL)`. The [duplicate check](https://github.com/apache/hudi/blob/bb69ac7fb0fa814678e56ba413eaade0509b153d/hudi-spark-datasource/hudi-spark3-common/src/main/scala/org/apache/spark/sql/hudi/command/MergeIntoHoodieTableCommand.scala#L397-L405) filters out both rows before grouping because `part` is `NULL`. The writer accepts both and gives them the same record key. > > I reproduced this on Spark 3.5.9 with an empty table: > > ```sql > SET hoodie.index.type=SIMPLE; > > CREATE TABLE hudi_20895_repro (id INT, part STRING) > USING hudi TBLPROPERTIES (primaryKey='id,part', type='cow'); > > MERGE INTO hudi_20895_repro t > USING (SELECT 8 AS id, CAST(NULL AS STRING) AS part FROM range(2)) s > ON false WHEN NOT MATCHED THEN INSERT *; > > SELECT _hoodie_record_key, COUNT(*) AS copies > FROM hudi_20895_repro GROUP BY _hoodie_record_key; > ``` > > I expected `HoodieDuplicateKeyException` and an empty table. The `MERGE` succeeded, and the query returned `id:8,part:__null__` with `copies = 2`. > > Could the duplicate check compare the record keys the writer generates so it catches this case? Thanks for the repro! This is fixed in c88752f: the repeated-key check now runs inside the insert on the record keys the key generator produces, so `(8, NULL)` twice is caught as `id:8,part:__null__`. Your exact statement on an empty table now fails with `HoodieDuplicateKeyException: Duplicate key found for insert statement, key is: id:8,part:__null__` and leaves the table empty. The case is covered in `TestMergeIntoTable2` ("... Checks Inserted Records By The Key They Are Written With"), along with empty key fields and -0.0/0.0. -- 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]
