yihua commented on code in PR #20857:
URL: https://github.com/apache/hudi/pull/20857#discussion_r4703618669
##########
hudi-client/hudi-client-common/src/main/java/org/apache/hudi/index/HoodieIndexUtils.java:
##########
@@ -558,6 +659,101 @@ public static <R> HoodieData<HoodieRecord<R>>
mergeForPartitionUpdatesAndDeletio
return taggedUpdatingRecords.union(taggedNewRecords);
}
+ /**
+ * Resolves each located record of a Spark SQL MERGE INTO write against the
record it matches, read from the
+ * latest merged file slice of the located file group, so that its matched
clauses see the fields of the target
+ * table. A record the clauses update is replaced with its new version and a
record they delete with a delete
+ * record, both at the location and row position the index tagged, and a
record none of them accepts is dropped.
+ * A record whose location holds no live record with its key, as a deleted
copy in a base file or the bucket of a
+ * bucket index can get it tagged, is replaced with what its not-matched
clauses make of it at the same location,
+ * and dropped when none of them accepts it. The records of any other write
are returned as they are.
+ */
+ public static <R> HoodieData<HoodieRecord<R>>
mergeLocatedExpressionPayloadRecords(HoodieData<HoodieRecord<R>> taggedRecords,
+
HoodieWriteConfig config,
+
HoodieTable hoodieTable,
+
int parallelism) {
+ if (!EXPRESSION_PAYLOAD_CLASS_NAME.equals(config.getPayloadClass())) {
+ return taggedRecords;
+ }
+ HoodieReaderContext<R> readerContext = ((ReaderContextFactory<R>)
hoodieTable.getContext()
+ .getReaderContextFactoryForWrite(hoodieTable.getMetaClient(),
config.getRecordMerger().getRecordType(), config.getProps())).getContext();
+ readerContext.initRecordMergerForIngestion(config.getProps());
+ HoodieSchema writerSchema = HoodieSchema.parse(config.getWriteSchema());
+ ExistingRecordMerger<R> merger = new ExistingRecordMerger<>(config,
hoodieTable, readerContext, writerSchema);
+ TypedProperties notMatchedProps = TypedProperties.copy(config.getProps());
+
notMatchedProps.setProperty(HoodiePayloadProps.PAYLOAD_IS_UPDATE_RECORD_FOR_MOR,
"false");
+ HoodieData<HoodieRecord<R>> locatedRecords =
taggedRecords.filter(HoodieIndexUtils::isLocatedExpressionPayloadRecord);
+ HoodieData<Pair<String, String>> locations = locatedRecords
+ .map(record -> Pair.of(record.getPartitionPath(),
record.getCurrentLocation().getFileId()))
+ .distinct(parallelism);
+ HoodieData<HoodieRecord<R>> resolvedRecords =
locatedRecords.mapToPair(record -> Pair.of(fileIdAndRecordKey(record), record))
+ .groupByKey()
+ .leftOuterJoin(merger.getExistingRecords(locations, hoodieTable, true)
+ .mapToPair(record -> Pair.of(fileIdAndRecordKey(record),
record)).groupByKey())
+ .values()
+ .flatMap(entry -> pairWithExistingRecords(entry.getLeft(),
entry.getRight()).iterator())
+ .flatMap(entry -> {
+ HoodieRecord<R> incoming = entry.getLeft();
+ if (!entry.getRight().isPresent()) {
+ Option<IndexedRecord> inserted = ((HoodieRecordPayload<?>)
incoming.getData()).getInsertValue(writerSchema.toAvroSchema(),
notMatchedProps);
+ if (!inserted.isPresent() ||
inserted.get().equals(HoodieRecord.SENTINEL)) {
+ return Collections.emptyIterator();
+ }
+ HoodieRecord<R> insertRecord = (HoodieRecord<R>) new
HoodieAvroIndexedRecord(incoming.getKey(), inserted.get());
+ return Collections.singletonList(tagRecord(insertRecord,
incoming.getCurrentLocation())).iterator();
+ }
+ Option<HoodieRecord<R>> mergedOpt = merger.merge(incoming,
entry.getRight().get());
+ if (mergedOpt.isPresent() &&
mergedOpt.get().getData().equals(HoodieRecord.SENTINEL)) {
+ return Collections.emptyIterator();
+ }
+ HoodieRecord<R> resolved = mergedOpt.isPresent()
+ ? mergedOpt.get().newInstance(incoming.getKey()) :
createDeleteRecord(config, incoming.getKey());
+ return Collections.singletonList(tagRecord(resolved,
incoming.getCurrentLocation())).iterator();
+ });
+ return taggedRecords.filter(record ->
!isLocatedExpressionPayloadRecord(record)).union(resolvedRecords);
+ }
+
+ /**
+ * Pairs the located records of one record key in one file group with the
records the file group holds for the key.
+ * A file group can hold a key more than once, and an index that reads the
base file locates the incoming record at
+ * each of its rows, so each such row is paired with the located record at
its position. When the positions do not
+ * tell the rows apart, one located record is paired with one record of the
key.
+ */
+ private static <R> List<Pair<HoodieRecord<R>, Option<HoodieRecord<R>>>>
pairWithExistingRecords(
+ Iterable<HoodieRecord<R>> locatedRecords,
Option<Iterable<HoodieRecord<R>>> existingRecords) {
+ HoodieRecord<R> firstLocated = locatedRecords.iterator().next();
+ if (!existingRecords.isPresent()) {
+ return Collections.singletonList(Pair.of(firstLocated, Option.empty()));
+ }
+ Map<Long, HoodieRecord<R>> locatedByPosition = new HashMap<>();
+ for (HoodieRecord<R> located : locatedRecords) {
+ long position = located.getCurrentLocation().getPosition();
+ if (HoodieRecordLocation.isPositionValid(position)) {
+ locatedByPosition.putIfAbsent(position, located);
+ }
+ }
+ List<Pair<HoodieRecord<R>, Option<HoodieRecord<R>>>> pairs = new
ArrayList<>();
+ for (HoodieRecord<R> existing : existingRecords.get()) {
+ HoodieRecord<R> located =
locatedByPosition.get(existing.getCurrentLocation().getPosition());
+ if (located != null) {
+ pairs.add(Pair.of(located, Option.of(existing)));
+ }
+ }
+ if (pairs.isEmpty()) {
+ pairs.add(Pair.of(firstLocated,
Option.of(existingRecords.get().iterator().next())));
+ }
+ return pairs;
+ }
+
+ private static boolean isLocatedExpressionPayloadRecord(HoodieRecord<?>
record) {
+ return record.isCurrentLocationKnown() && record.getData() instanceof
HoodieRecordPayload
+ &&
EXPRESSION_PAYLOAD_CLASS_NAME.equals(record.getData().getClass().getName());
+ }
+
+ private static Pair<String, String> fileIdAndRecordKey(HoodieRecord<?>
record) {
Review Comment:
Fixed, the key now has the partition path, with a bucket table test under
non-blocking concurrency control.
##########
hudi-client/hudi-client-common/src/main/java/org/apache/hudi/index/HoodieIndexUtils.java:
##########
@@ -558,6 +659,101 @@ public static <R> HoodieData<HoodieRecord<R>>
mergeForPartitionUpdatesAndDeletio
return taggedUpdatingRecords.union(taggedNewRecords);
}
+ /**
+ * Resolves each located record of a Spark SQL MERGE INTO write against the
record it matches, read from the
+ * latest merged file slice of the located file group, so that its matched
clauses see the fields of the target
+ * table. A record the clauses update is replaced with its new version and a
record they delete with a delete
+ * record, both at the location and row position the index tagged, and a
record none of them accepts is dropped.
+ * A record whose location holds no live record with its key, as a deleted
copy in a base file or the bucket of a
+ * bucket index can get it tagged, is replaced with what its not-matched
clauses make of it at the same location,
+ * and dropped when none of them accepts it. The records of any other write
are returned as they are.
+ */
+ public static <R> HoodieData<HoodieRecord<R>>
mergeLocatedExpressionPayloadRecords(HoodieData<HoodieRecord<R>> taggedRecords,
+
HoodieWriteConfig config,
+
HoodieTable hoodieTable,
+
int parallelism) {
+ if (!EXPRESSION_PAYLOAD_CLASS_NAME.equals(config.getPayloadClass())) {
+ return taggedRecords;
+ }
+ HoodieReaderContext<R> readerContext = ((ReaderContextFactory<R>)
hoodieTable.getContext()
+ .getReaderContextFactoryForWrite(hoodieTable.getMetaClient(),
config.getRecordMerger().getRecordType(), config.getProps())).getContext();
+ readerContext.initRecordMergerForIngestion(config.getProps());
+ HoodieSchema writerSchema = HoodieSchema.parse(config.getWriteSchema());
+ ExistingRecordMerger<R> merger = new ExistingRecordMerger<>(config,
hoodieTable, readerContext, writerSchema);
+ TypedProperties notMatchedProps = TypedProperties.copy(config.getProps());
+
notMatchedProps.setProperty(HoodiePayloadProps.PAYLOAD_IS_UPDATE_RECORD_FOR_MOR,
"false");
+ HoodieData<HoodieRecord<R>> locatedRecords =
taggedRecords.filter(HoodieIndexUtils::isLocatedExpressionPayloadRecord);
+ HoodieData<Pair<String, String>> locations = locatedRecords
+ .map(record -> Pair.of(record.getPartitionPath(),
record.getCurrentLocation().getFileId()))
+ .distinct(parallelism);
+ HoodieData<HoodieRecord<R>> resolvedRecords =
locatedRecords.mapToPair(record -> Pair.of(fileIdAndRecordKey(record), record))
+ .groupByKey()
+ .leftOuterJoin(merger.getExistingRecords(locations, hoodieTable, true)
+ .mapToPair(record -> Pair.of(fileIdAndRecordKey(record),
record)).groupByKey())
+ .values()
+ .flatMap(entry -> pairWithExistingRecords(entry.getLeft(),
entry.getRight()).iterator())
+ .flatMap(entry -> {
+ HoodieRecord<R> incoming = entry.getLeft();
+ if (!entry.getRight().isPresent()) {
+ Option<IndexedRecord> inserted = ((HoodieRecordPayload<?>)
incoming.getData()).getInsertValue(writerSchema.toAvroSchema(),
notMatchedProps);
Review Comment:
Thanks. Under commit time ordering, the re-insert is written at the location
after a MERGE or SQL delete, with tests. Under event time ordering, the delete
now carries the source's ordering value, so a lower one leaves the row as an
unflagged delete does, and a located record with no live row is not written, as
on master, so the deleted row cannot come back; a bucket index table refuses
such a statement with an insert clause, since a new key in an existing bucket
looks the same.
##########
hudi-client/hudi-spark-client/src/main/java/org/apache/hudi/table/action/deltacommit/SparkUpsertDeltaCommitPartitioner.java:
##########
@@ -53,6 +53,11 @@ public SparkUpsertDeltaCommitPartitioner(WorkloadProfile
profile, HoodieSparkEng
@Override
protected List<SmallFile> getSmallFiles(String partitionPath) {
+ if (config.isSparkSqlMergeIntoReadingTarget()) {
Review Comment:
Done, only the candidates that take an update in the write are dropped,
before the candidate limit.
##########
hudi-client/hudi-client-common/src/main/java/org/apache/hudi/index/HoodieIndexUtils.java:
##########
@@ -558,6 +659,101 @@ public static <R> HoodieData<HoodieRecord<R>>
mergeForPartitionUpdatesAndDeletio
return taggedUpdatingRecords.union(taggedNewRecords);
}
+ /**
+ * Resolves each located record of a Spark SQL MERGE INTO write against the
record it matches, read from the
+ * latest merged file slice of the located file group, so that its matched
clauses see the fields of the target
+ * table. A record the clauses update is replaced with its new version and a
record they delete with a delete
+ * record, both at the location and row position the index tagged, and a
record none of them accepts is dropped.
+ * A record whose location holds no live record with its key, as a deleted
copy in a base file or the bucket of a
+ * bucket index can get it tagged, is replaced with what its not-matched
clauses make of it at the same location,
+ * and dropped when none of them accepts it. The records of any other write
are returned as they are.
+ */
+ public static <R> HoodieData<HoodieRecord<R>>
mergeLocatedExpressionPayloadRecords(HoodieData<HoodieRecord<R>> taggedRecords,
+
HoodieWriteConfig config,
+
HoodieTable hoodieTable,
+
int parallelism) {
+ if (!EXPRESSION_PAYLOAD_CLASS_NAME.equals(config.getPayloadClass())) {
+ return taggedRecords;
+ }
+ HoodieReaderContext<R> readerContext = ((ReaderContextFactory<R>)
hoodieTable.getContext()
+ .getReaderContextFactoryForWrite(hoodieTable.getMetaClient(),
config.getRecordMerger().getRecordType(), config.getProps())).getContext();
+ readerContext.initRecordMergerForIngestion(config.getProps());
+ HoodieSchema writerSchema = HoodieSchema.parse(config.getWriteSchema());
+ ExistingRecordMerger<R> merger = new ExistingRecordMerger<>(config,
hoodieTable, readerContext, writerSchema);
+ TypedProperties notMatchedProps = TypedProperties.copy(config.getProps());
+
notMatchedProps.setProperty(HoodiePayloadProps.PAYLOAD_IS_UPDATE_RECORD_FOR_MOR,
"false");
+ HoodieData<HoodieRecord<R>> locatedRecords =
taggedRecords.filter(HoodieIndexUtils::isLocatedExpressionPayloadRecord);
Review Comment:
Done, the tagged records are persisted and the existing rows are filtered to
the located keys before the shuffle, with a test that counts both.
--
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]