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


##########
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:
   `UPSERT_PREPPED` and `DELETE_PREPPED` can't reach here in this mode, since 
`preWrite` refuses prepped writes. If tagging moves into the helpers, which 
prepped writes skip, these can be dropped.



##########
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:
   Same as the Spark executor: suggest moving this into `JavaWriteHelper` and 
`JavaDeleteHelper`, after `tagLocation`.



##########
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:
   See point 1 in the summary: could this move into the write and delete 
helpers, right after tagging (and after the split for upserts), so that 
`execute` stays as on master? That also lets the pending-compaction check run 
before the base files are read, and makes the second persist here unnecessary.



##########
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:
   This only checks the table's main base file format. With multiple base file 
formats enabled, a file group can still have an HFile base file. See point 3.



##########
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:
   This applies to every write, not just the mode: global-index MoR tables on 
v8+ will now write `RECORD_POSITIONS` headers by default, and readers will 
merge by position for them. See point 2 in the summary, which asks for an 
Impact or release note and a test with a partition-path update followed by 
compaction.



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