dramaticlly commented on code in PR #19242:
URL: https://github.com/apache/iceberg/pull/19242#discussion_r4805328300
##########
spark/v4.2/spark/src/main/java/org/apache/iceberg/spark/actions/RewriteTablePathSparkAction.java:
##########
@@ -535,26 +530,11 @@ private RewriteResult<ManifestFile> rewriteManifestList(
return result;
}
- private Set<String> manifestsToRewrite(
- Set<Snapshot> deltaSnapshots, TableMetadata startMetadata) {
+ private void validateEndVersionManifests() {
try {
Table endStaticTable = newStaticTable(endVersionName, table.io());
- Dataset<Row> lastVersionFiles =
manifestDS(endStaticTable).select("path");
- if (startMetadata == null) {
- return
Sets.newHashSet(lastVersionFiles.distinct().as(Encoders.STRING()).collectAsList());
- } else {
- Set<Long> deltaSnapshotIds =
-
deltaSnapshots.stream().map(Snapshot::snapshotId).collect(Collectors.toSet());
- return Sets.newHashSet(
- lastVersionFiles
- .distinct()
- .filter(
- functions
- .column(ManifestFile.SNAPSHOT_ID.name())
- .isInCollection(deltaSnapshotIds))
- .as(Encoders.STRING())
- .collectAsList());
- }
+ // read all manifest lists to fail fast on a missing one, without
collecting to the driver
+ manifestDS(endStaticTable).select("path").count();
Review Comment:
Updated in e1655ce as requested, also I dropped the explicit catch after as
we already have throwFailureWhenFinished when reading manifest lists
##########
core/src/main/java/org/apache/iceberg/RewriteTablePathUtil.java:
##########
@@ -462,9 +615,21 @@ public static RewriteResult<DeleteFile>
rewriteDeleteManifest(
return result;
}
+ private static Predicate<ManifestEntry<?>> committedAfter(
+ long startSequenceNumber, Set<Long> startSnapshotIds) {
+ return entry -> {
+ Long fileSequenceNumber = entry.fileSequenceNumber();
+ if (startSequenceNumber > 0 && fileSequenceNumber != null) {
+ return fileSequenceNumber > startSequenceNumber;
+ }
+
+ return !startSnapshotIds.contains(entry.snapshotId());
Review Comment:
The repeated copy on v1 is intentional conservative when we dont have
sequence numbers guarantees. The start version's snapshots are the only
reliable signal, so files whose snapshot expired before the start version can
be copied again without missing a file.
aa5dbed documents this in the Javadoc and adds v1 coverage:
TestRewriteTablePathsAction now runs on v1, and the core fallback test runs on
v1 as well.
##########
core/src/main/java/org/apache/iceberg/RewriteTablePathUtil.java:
##########
@@ -462,9 +615,21 @@ public static RewriteResult<DeleteFile>
rewriteDeleteManifest(
return result;
}
+ private static Predicate<ManifestEntry<?>> committedAfter(
+ long startSequenceNumber, Set<Long> startSnapshotIds) {
+ return entry -> {
+ Long fileSequenceNumber = entry.fileSequenceNumber();
+ if (startSequenceNumber > 0 && fileSequenceNumber != null) {
+ return fileSequenceNumber > startSequenceNumber;
Review Comment:
I am not sure how concurrent rest replacement result in the same sequence
number is the case need to fix here , the iceberg spec require the file
sequence number to be reassigned on commit failure and retry
> The relative age of data and delete files relies on a sequence number that
is assigned to every successful commit. When a snapshot is created for a
commit, it is optimistically assigned the next sequence number, and it is
written into the snapshot's metadata. If the commit fails and must be retried,
the sequence number is reassigned and written into new snapshot metadata.
I think the fix belongs in the replace retry path, and I'd like to keep it
out of this PR.
##########
core/src/test/java/org/apache/iceberg/TestRewriteTablePathUtil.java:
##########
@@ -476,6 +476,125 @@ public void testFileName() {
assertThat(RewriteTablePathUtil.fileName("file.parquet")).isEqualTo("file.parquet");
}
+ @Test
+ void rewriteDataManifestCopiesEntriesAfterStartSequenceNumber() throws
IOException {
+ assumeThat(formatVersion)
+ .as("Sequence numbers require format version 2+")
+ .isGreaterThanOrEqualTo(2);
+
+ // FILE_B keeps an old data sequence number but was committed after the
start, like a
+ // compaction output
+ ManifestFile manifest =
+ writeManifest(
+ 3L,
+ manifestEntry(ManifestEntry.Status.EXISTING, 1L, 1L, 1L, FILE_A),
+ manifestEntry(ManifestEntry.Status.EXISTING, 2L, 1L, 2L, FILE_B),
+ manifestEntry(ManifestEntry.Status.DELETED, 3L, 3L, 3L, FILE_C));
+
+ RewriteTablePathUtil.RewriteResult<DataFile> result =
+ RewriteTablePathUtil.rewriteDataManifest(
+ manifest,
+ 1L,
+ Set.of(),
+ Files.localOutput(temp.resolve("rewritten.avro").toFile()),
+ table.io(),
+ formatVersion,
+ table.specs(),
+ "/path/to/",
+ "/path/new/");
+
+ assertThat(result.copyPlan())
+ .containsExactly(Pair.of(FILE_B.location(),
"/path/new/data-b.parquet"));
+ }
+
+ @Test
+ void rewriteDataManifestFallsBackToSnapshotIdsWithoutFileSequenceNumber()
throws IOException {
+ assumeThat(formatVersion)
+ .as("Sequence numbers require format version 2+")
+ .isGreaterThanOrEqualTo(2);
+
+ ManifestFile manifest =
+ writeManifest(
+ 2L,
+ manifestEntry(ManifestEntry.Status.EXISTING, 1L, 1L, null, FILE_A),
+ manifestEntry(ManifestEntry.Status.EXISTING, 2L, 1L, null,
FILE_B));
+
+ RewriteTablePathUtil.RewriteResult<DataFile> result =
+ RewriteTablePathUtil.rewriteDataManifest(
+ manifest,
+ 5L,
+ Set.of(1L),
+ Files.localOutput(temp.resolve("rewritten.avro").toFile()),
+ table.io(),
+ formatVersion,
+ table.specs(),
+ "/path/to/",
+ "/path/new/");
+
+ assertThat(result.copyPlan())
+ .containsExactly(Pair.of(FILE_B.location(),
"/path/new/data-b.parquet"));
+ }
+
+ @Test
+ void rewriteDeleteManifestCopiesEntriesAfterStartSequenceNumber() throws
IOException {
+ assumeThat(formatVersion)
+ .as("Delete files require format version 2+")
+ .isGreaterThanOrEqualTo(2);
+
+ ManifestFile manifest =
+ writeManifest(
+ 2L,
+ manifestEntry(ManifestEntry.Status.EXISTING, 1L, 1L, 1L,
FILE_A_DELETES),
+ manifestEntry(ManifestEntry.Status.EXISTING, 2L, 2L, 2L,
FILE_A_EQUALITY_DELETES));
+
+ RewriteTablePathUtil.RewriteResult<DeleteFile> result =
+ RewriteTablePathUtil.rewriteDeleteManifest(
+ manifest,
+ 1L,
+ Set.of(),
+ Files.localOutput(temp.resolve("rewritten.avro").toFile()),
+ table.io(),
+ formatVersion,
+ table.specs(),
+ "/path/to/",
+ "/path/new/",
+ "/staging/",
+ ImmutableMap.of());
+
+ assertThat(result.copyPlan())
+ .containsExactly(
+ Pair.of(
+ FILE_A_EQUALITY_DELETES.location(),
"/path/new/data-a-equality-deletes.parquet"));
+ }
+
+ @Test
+ void rewriteDataManifestWithoutStartSequenceNumberSkipsStartSnapshots()
throws IOException {
+ assumeThat(formatVersion)
+ .as("Uncommitted v1 manifests have no sequence number for entries to
inherit")
+ .isGreaterThanOrEqualTo(2);
Review Comment:
Yes, added in aa5dbed. TestRewriteTablePathsAction now runs on v1, including
an incremental copy after appends, manifest rewrites and expirations on both
sides of the start version, and the core fallback test now runs on v1 too.
--
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]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]