voonhous commented on code in PR #20846:
URL: https://github.com/apache/hudi/pull/20846#discussion_r4696048698


##########
hudi-spark-datasource/hudi-spark3-common/src/main/scala/org/apache/spark/sql/hudi/command/MergeIntoHoodieTableCommand.scala:
##########
@@ -601,7 +635,9 @@ case class MergeIntoHoodieTableCommand(mergeInto: 
MergeIntoTable) extends Hoodie
   }
 
   private def getOperationType(parameters: Map[String, String]) = {
-    if 
(StringUtils.isNullOrEmpty(ConfigUtils.getOrderingFieldsStrDuringWrite(parameters.asJava))
 && updatingActions.isEmpty) {
+    // The cardinality check runs on the records an upsert locates
+    if 
(StringUtils.isNullOrEmpty(ConfigUtils.getOrderingFieldsStrDuringWrite(parameters.asJava))
 && updatingActions.isEmpty
+      && !shouldCheckCardinality(parameters)) {

Review Comment:
   **blocker:** Forcing the upsert here when the check is on loses inserts on 
MOR with SIMPLE/BLOOM: after `delete where id = 1` (log-file delete), `when 
matched and s.op = 'del' then delete when not matched then insert` with source 
ids 1 and 3 leaves id 1 absent with the check on, and inserts it with the check 
off. The index tags key 1 from the base file, so ExpressionPayload takes the 
matched branch (the same class as the comment on #20897). Could the statement 
stay an insert, or skip the check, for MOR tables whose index cannot index log 
files, with a test for a log-deleted key plus a conditional delete and insert?



##########
hudi-client/hudi-client-common/src/main/java/org/apache/hudi/table/action/commit/BaseWriteHelper.java:
##########
@@ -87,6 +97,35 @@ public HoodieWriteMetadata<O> write(String instantTime,
   protected abstract I tag(
       I dedupedRecords, HoodieEngineContext context, HoodieTable<T, I, K, O> 
table);
 
+  private static boolean shouldFailOnMultipleMatches(HoodieTable<?, ?, ?, ?> 
table, WriteOperationType operationType) {
+    // A bucket index locates every record of an existing bucket, including 
one with a new key
+    return operationType == WriteOperationType.UPSERT
+        && table.getConfig().shouldFailUpsertOnMultipleMatches()
+        && table.getIndex().requiresTagging(operationType)
+        && !(table.getIndex() instanceof HoodieBucketIndex);

Review Comment:
   **blocker:** With a bucket index the check is skipped but MERGE INTO still 
forces `COMBINE_BEFORE_UPSERT`, so two source rows for an existing key are 
silently combined by shuffle order even when the user set 
`hoodie.spark.sql.merge.into.cardinality.check=true` (bucket COW table, source 
(1,x,2) and (1,y,3) gives [1,y,3] with no error). Could MERGE INTO fail fast 
when the check is on and the index is a bucket index, and could the exclusion 
be an index capability (e.g. `HoodieIndex#locatesNewKeys`) rather than 
`instanceof HoodieBucketIndex`?



##########
hudi-client/hudi-client-common/src/main/java/org/apache/hudi/table/action/commit/HoodieWriteHelper.java:
##########
@@ -79,4 +89,64 @@ public HoodieData<HoodieRecord<T>> 
deduplicateRecords(HoodieData<HoodieRecord<T>
         (previous, next) -> reduceRecords(props, recordMerger, 
orderingFieldNames, previous, next, schema, recordContext, deleteContext),
         parallelism).map(Pair::getRight);
   }
+
+  @Override
+  protected HoodieData<HoodieRecord<T>> 
failOnMultipleMatches(HoodieData<HoodieRecord<T>> taggedRecords, boolean 
combineNewKeys,
+                                                              HoodieTable<T, 
HoodieData<HoodieRecord<T>>, HoodieData<HoodieKey>, HoodieData<WriteStatus>> 
table) {
+    boolean isIndexingGlobal = table.getIndex().isGlobal();
+    Option<SerializableBiFunction<HoodieRecord<T>, HoodieRecord<T>, 
HoodieRecord<T>>> newKeyCombiner =
+        combineNewKeys ? Option.of(recordCombiner(table)) : Option.empty();
+    HoodieSchema writeSchema = 
HoodieSchema.parse(table.getConfig().getWriteSchema());
+    TypedProperties props = table.getConfig().getProps();
+    return taggedRecords
+        // NOTE: the incoming record may hold an instance of [[InternalRow]] 
pointing into a
+        //       shared, mutable buffer, so it has to be copied before the 
shuffle
+        .mapToPair(record -> Pair.of(isIndexingGlobal ? record.getRecordKey() 
: record.getKey(), record.copy()))

Review Comment:
   **blocker:** With `RECORD_LEVEL_INDEX` (partitioned RLI) the check reports a 
false violation: target rows (1,p1) and (1,p2), source (1,x,p1) and (1,y,p2) 
with `ON t.id = s.id AND t.dt = s.dt` fails here, while SIMPLE gives the 
correct two updates. The RLI is non-global but tags via 
`tagGlobalLocationBackToRecords`, which joins on record key only, so (1,p2) is 
also re-keyed to the p1 location; with the check off the same statement writes 
`y` into both rows, an existing corruption. Could the partitioned RLI be 
excluded from the check (or its join filtered by partition), with a 
partitioned-RLI test and a separate issue for the existing cross-partition 
write?



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