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


##########
hudi-client/hudi-spark-client/src/main/java/org/apache/hudi/table/action/commit/BaseSparkCommitActionExecutor.java:
##########
@@ -228,11 +229,18 @@ public HoodieWriteMetadata<HoodieData<WriteStatus>> 
execute(HoodieData<HoodieRec
       log.info("RDD PreppedRecords was persisted at: {}", 
inputRDD.getStorageLevel());
     }
 
+    HoodieData<HoodieRecord<T>> positionedRecords = 
HoodieIndexUtils.tagMissingRecordPositionsIfNeeded(inputRecords, operationType, 
config, table);

Review Comment:
   Thanks, agreed that it belongs next to `tagLocation`. To let this PR land as 
the fix, I will move it into the write and delete helpers in a follow-up, 
together with running the pending-compaction check first and the narrower 
caching, so `execute` stays as here for now.



##########
hudi-client/hudi-java-client/src/main/java/org/apache/hudi/table/action/commit/BaseJavaCommitActionExecutor.java:
##########
@@ -90,11 +92,15 @@ public HoodieWriteMetadata<List<WriteStatus>> 
execute(List<HoodieRecord<T>> inpu
   }
 
   @Override
-  public HoodieWriteMetadata<List<WriteStatus>> execute(List<HoodieRecord<T>> 
inputRecords, Option<HoodieTimer> sourceReadAndIndexTimer) {
+  public HoodieWriteMetadata<List<WriteStatus>> execute(List<HoodieRecord<T>> 
taggedRecords, Option<HoodieTimer> sourceReadAndIndexTimer) {
     HoodieWriteMetadata<List<WriteStatus>> result = new 
HoodieWriteMetadata<>();
+    HoodieData<HoodieRecord<T>> taggedData = 
HoodieListData.eager(taggedRecords);
+    HoodieData<HoodieRecord<T>> positionedData = 
HoodieIndexUtils.tagMissingRecordPositionsIfNeeded(taggedData, operationType, 
config, table);

Review Comment:
   Will move it into `JavaWriteHelper` and `JavaDeleteHelper` in the same 
follow-up as the Spark executor.



##########
hudi-client/hudi-client-common/src/main/java/org/apache/hudi/index/HoodieIndexUtils.java:
##########
@@ -628,6 +868,77 @@ public static <R> HoodieData<HoodieRecord<R>> 
tagGlobalLocationBackToRecords(
         : incomingRecordsAndLocations.map(Pair::getLeft);
   }
 
+  /**
+   * Sets the row position of each located record of an update or delete that 
has none, for a merge-on-read write in
+   * the update-as-delete-insert write mode, which writes the delete of a 
located record as a positional delete. Only
+   * the indexes that read the keys of base files to locate records tag their 
positions, so this reads them for the
+   * records a global index or the record index located.
+   */
+  public static <R> HoodieData<HoodieRecord<R>> 
tagMissingRecordPositionsIfNeeded(HoodieData<HoodieRecord<R>> taggedRecords,
+                                                                               
   WriteOperationType operationType,
+                                                                               
   HoodieWriteConfig config,
+                                                                               
   HoodieTable table) {
+    boolean updatesOrDeletes = operationType == WriteOperationType.UPSERT || 
operationType == WriteOperationType.UPSERT_PREPPED

Review Comment:
   Done, dropped the two prepped branches from the gate since `preWrite` 
refuses those writes in this mode.



##########
hudi-common/src/main/java/org/apache/hudi/common/model/HoodieRecordGlobalLocation.java:
##########
@@ -53,7 +53,7 @@ public HoodieRecordGlobalLocation(String partitionPath, 
String instantTime, Stri
    * Returns the global record location from local.
    */
   public static HoodieRecordGlobalLocation fromLocal(String partitionPath, 
HoodieRecordLocation localLoc) {
-    return new HoodieRecordGlobalLocation(partitionPath, 
localLoc.getInstantTime(), localLoc.getFileId());
+    return new HoodieRecordGlobalLocation(partitionPath, 
localLoc.getInstantTime(), localLoc.getFileId(), localLoc.getPosition());

Review Comment:
   Done, added under Impact with a release-note line. One nuance: on a 
partitioned table with `hoodie.bloom.index.update.partition.path` on (the 
default), the merged lookup still tags without positions, so the change reaches 
the direct tagging path only. Added `TestUpdatesAsDeletesAndInsertsWithSql` 
cases for GLOBAL_BLOOM and GLOBAL_SIMPLE outside the mode (partition move, 
snapshot read, compaction, read again) and a GLOBAL_BLOOM case outside the mode 
in `testUpdatesAndDeletesCarryRecordPositions` that checks every log position.



##########
hudi-client/hudi-client-common/src/main/java/org/apache/hudi/table/action/commit/BaseWriteHelper.java:
##########
@@ -84,9 +116,196 @@ public HoodieWriteMetadata<O> write(String instantTime,
     }
   }
 
+  /**
+   * Index types that tag a record with its row position in the base file and 
write merge-on-read
+   * inserts to base files, which the update-as-delete-insert write mode needs 
to keep log files to
+   * positional deletes. The write reads the position of a record the index 
locates without one from
+   * the base file of the located file group, see {@link 
HoodieIndexUtils#tagMissingRecordPositionsIfNeeded}.
+   */
+  private static final EnumSet<HoodieIndex.IndexType> 
POSITION_PRODUCING_INDEX_TYPES = EnumSet.of(
+      HoodieIndex.IndexType.BLOOM, HoodieIndex.IndexType.SIMPLE,
+      HoodieIndex.IndexType.GLOBAL_BLOOM, HoodieIndex.IndexType.GLOBAL_SIMPLE,
+      HoodieIndex.IndexType.RECORD_INDEX, 
HoodieIndex.IndexType.GLOBAL_RECORD_LEVEL_INDEX);
+
   protected abstract I tag(
       I dedupedRecords, HoodieEngineContext context, HoodieTable<T, I, K, O> 
table);
 
+  /**
+   * Replaces each located record of a Spark SQL MERGE INTO with what its 
matched clauses make of the record it
+   * matches, for a write that appends updates to log files, which evaluates 
the clauses of a located record
+   * without that record. See {@link 
HoodieWriteConfig#SPARK_SQL_MERGE_INTO_READS_TARGET_KEY}.
+   */
+  protected I mergeLocatedSqlMergeIntoRecords(I taggedRecords, String 
instantTime, HoodieEngineContext context,
+                                              HoodieTable<T, I, K, O> table, 
int configuredShuffleParallelism) {
+    throw new HoodieNotSupportedException(
+        HoodieWriteConfig.SPARK_SQL_MERGE_INTO_READS_TARGET_KEY + " is not 
supported by " + this.getClass().getName());
+  }
+
+  private boolean shouldWriteUpdatesAsDeletesAndInserts(HoodieTable<T, I, K, 
O> table, WriteOperationType operationType) {
+    if (operationType != WriteOperationType.UPSERT
+        || table.getMetaClient().getTableType() != 
HoodieTableType.MERGE_ON_READ
+        || !table.getConfig().shouldWriteUpdatesAsDeletesAndInserts()) {
+      return false;
+    }
+    validateUpdatesAsDeletesAndInserts(table.getConfig(), 
table.getMetaClient(), operationType);
+    return true;
+  }
+
+  /**
+   * Validates the requirements of {@link 
HoodieWriteConfig#WRITE_UPDATES_AS_DELETES_AND_INSERTS} for a
+   * write that can append to the log files of a merge-on-read table.
+   */
+  public static void validateUpdatesAsDeletesAndInserts(HoodieWriteConfig 
config, HoodieTableMetaClient metaClient,
+                                                        WriteOperationType 
operationType) {
+    HoodieTableVersion writeVersion = config.getWriteVersion();
+    if (writeVersion.lesserThan(HoodieTableVersion.EIGHT)) {
+      // Log files carry record positions only from write table version 8 on
+      throw new HoodieNotSupportedException(
+          HoodieWriteConfig.WRITE_UPDATES_AS_DELETES_AND_INSERTS.key()
+              + " requires write table version 8 or higher to write record 
positions but the write table version is "
+              + writeVersion.versionCode());
+    }
+    RecordMergeMode mergeMode = 
metaClient.getTableConfig().getRecordMergeMode();
+    if (mergeMode != RecordMergeMode.COMMIT_TIME_ORDERING) {
+      // The decomposed delete unconditionally tombstones the current version, 
so a late-arriving
+      // update with a lower ordering value would incorrectly win under 
event-time ordering
+      throw new HoodieNotSupportedException(
+          HoodieWriteConfig.WRITE_UPDATES_AS_DELETES_AND_INSERTS.key()
+              + " requires commit-time ordering merge semantics but the table 
uses " + mergeMode);
+    }
+    if (!StringUtils.isNullOrEmpty(config.getIndexClass())) {
+      // A custom index class takes precedence over the index type checked 
below
+      throw new HoodieNotSupportedException(
+          HoodieWriteConfig.WRITE_UPDATES_AS_DELETES_AND_INSERTS.key() + " 
does not support a custom index class but "
+              + HoodieIndexConfig.INDEX_CLASS_NAME.key() + " is " + 
config.getIndexClass());
+    }
+    HoodieIndex.IndexType indexType = config.getIndexType();
+    if (!POSITION_PRODUCING_INDEX_TYPES.contains(indexType)) {
+      // Without a base file position a delete cannot be represented as a 
positional delete, and an
+      // index that routes by key hash or can index log files appends inserts 
to logs
+      throw new HoodieNotSupportedException(
+          HoodieWriteConfig.WRITE_UPDATES_AS_DELETES_AND_INSERTS.key()
+              + " requires an index that produces record positions " + 
POSITION_PRODUCING_INDEX_TYPES
+              + " but the index type is " + indexType);
+    }
+    if (!config.shouldWriteRecordPositions()) {
+      throw new HoodieNotSupportedException(
+          HoodieWriteConfig.WRITE_UPDATES_AS_DELETES_AND_INSERTS.key()
+              + " requires " + HoodieWriteConfig.WRITE_RECORD_POSITIONS.key() 
+ " to be enabled");
+    }
+    HoodieFileFormat baseFileFormat = 
metaClient.getTableConfig().getBaseFileFormat();
+    if (baseFileFormat == HoodieFileFormat.HFILE) {

Review Comment:
   Fixed, the mode now refuses a table with 
`hoodie.table.multiple.base.file.formats.enable` on, with a test.



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