Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 6 additions & 9 deletions core/src/main/java/org/apache/iceberg/RewriteTablePathUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
import org.apache.iceberg.data.Record;
import org.apache.iceberg.deletes.PositionDelete;
Expand Down Expand Up @@ -225,11 +224,7 @@ public static RewriteResult<ManifestFile> rewriteManifestList(
OutputFile outputFile = io.newOutputFile(outputPath);

List<ManifestFile> manifestFiles = manifestFilesInSnapshot(io, snapshot);
List<ManifestFile> manifestFilesToRewrite =
manifestFiles.stream()
.filter(mf -> manifestsToRewrite.contains(mf.path()))
.collect(Collectors.toList());
manifestFilesToRewrite.forEach(
manifestFiles.forEach(
mf ->
Preconditions.checkArgument(
mf.path().startsWith(sourcePrefix),
Expand All @@ -245,13 +240,15 @@ public static RewriteResult<ManifestFile> rewriteManifestList(
snapshot.parentId(),
snapshot.sequenceNumber())) {

for (ManifestFile file : manifestFilesToRewrite) {
for (ManifestFile file : manifestFiles) {
ManifestFile newFile = file.copy();
((StructLike) newFile).set(0, newPath(newFile.path(), sourcePrefix, targetPrefix));
writer.add(newFile);

result.toRewrite().add(file);
result.copyPlan().add(Pair.of(stagingPath(file.path(), stagingDir), newFile.path()));
if (manifestsToRewrite.contains(file.path())) {
result.toRewrite().add(file);
result.copyPlan().add(Pair.of(stagingPath(file.path(), stagingDir), newFile.path()));
}
}
return result;
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,51 @@ public void testStartVersion() throws Exception {
.isEqualTo(0);
}

@Test
public void testIncrementalRewrite() throws Exception {
String location = newTableLocation();
Table sourceTable =
TABLES.create(SCHEMA, PartitionSpec.unpartitioned(), Maps.newHashMap(), location);
List<ThreeColumnRecord> recordsA =
Lists.newArrayList(new ThreeColumnRecord(1, "AAAAAAAAAA", "AAAA"));
Dataset<Row> dfA = spark.createDataFrame(recordsA, ThreeColumnRecord.class).coalesce(1);

// Write first increment to source table
dfA.select("c1", "c2", "c3").write().format("iceberg").mode("append").save(location);
spark.read().format("iceberg").load(location).show();
Comment thread
barronfuentes marked this conversation as resolved.
Outdated
assertThat(spark.read().format("iceberg").load(location).count()).isEqualTo(1);

// Replicate first increment to target table
RewriteTablePath.Result result =
actions()
.rewriteTablePath(sourceTable)
.rewriteLocationPrefix(sourceTable.location(), targetTableLocation())
.execute();
copyTableFiles(result);
assertThat(spark.read().format("iceberg").load(targetTableLocation()).count()).isEqualTo(1);

// Write second increment to source table
List<ThreeColumnRecord> recordsB =
Lists.newArrayList(new ThreeColumnRecord(1, "BBBBBBBBB", "BBB"));
Dataset<Row> dfB = spark.createDataFrame(recordsB, ThreeColumnRecord.class).coalesce(1);
dfB.select("c1", "c2", "c3").write().format("iceberg").mode("append").save(location);
assertThat(spark.read().format("iceberg").load(location).count()).isEqualTo(2);

// Replicate second increment to target table
Table sourceTableIncrement = TABLES.load(location);
Comment thread
barronfuentes marked this conversation as resolved.
Outdated
Table targetTable = TABLES.load(targetTableLocation());
String targetTableMetadata = currentMetadata(targetTable).metadataFileLocation();
String startVersion = fileName(targetTableMetadata);
RewriteTablePath.Result resultB =
Comment thread
barronfuentes marked this conversation as resolved.
Outdated
actions()
.rewriteTablePath(sourceTableIncrement)
.rewriteLocationPrefix(sourceTable.location(), targetTableLocation())
.startVersion(startVersion)
.execute();
copyTableFiles(resultB);
assertThat(spark.read().format("iceberg").load(targetTableLocation()).count()).isEqualTo(2);
Comment thread
barronfuentes marked this conversation as resolved.
Outdated
}

@Test
public void testTableWith3Snapshots(@TempDir Path location1, @TempDir Path location2)
throws Exception {
Expand Down