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


##########
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:
   Partly addressed: setting the check to true now fails for this statement on 
MOR with SIMPLE or BLOOM. With the default on a non-native table it still 
upserts and misses a key deleted in a log until #20897 lands, so this PR should 
land after it.



##########
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:
   Fixed, the check does not run with the partitioned record level index, and 
setting it to true fails before the write, with a test of one key in two 
partitions. The cross-partition write is on master too, so I will track it 
separately.



##########
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:
   Fixed, the exclusion is now `HoodieIndex#locatesOnlyMatchingRecords`, and 
setting the check to true with a bucket index or an index class fails before 
the write.



##########
hudi-spark-datasource/hudi-spark3-common/src/main/scala/org/apache/spark/sql/hudi/command/MergeIntoHoodieTableCommand.scala:
##########
@@ -581,6 +599,22 @@ case class MergeIntoHoodieTableCommand(mergeInto: 
MergeIntoTable) extends Hoodie
     }
   }
 
+  /**
+   * Whether more than one source row matching the same target row fails the 
statement. As in Spark, that applies
+   * when a matched clause other than a single unconditional delete can act on 
the target row. Unless set, the check
+   * is on only for a table of a non-native table format, as a native table 
combined such source rows before it. The
+   * format is the one the table loads, which is native for a name that no 
table format implementation answers to.
+   */
+  private def shouldCheckCardinality(parameters: Map[String, String]): Boolean 
= {
+    
parameters.get(MERGE_INTO_CARDINALITY_CHECK.key).map(_.toBoolean).getOrElse(
+      hoodieCatalogTable.metaClient.getTableFormat.getName != 
NativeTableFormat.TABLE_FORMAT) &&

Review Comment:
   Agreed, I will share one `isNative` helper with #20890 in a follow-up.



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