Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Core: Allow adding files to multiple partition specs in FastAppend #11771

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
36 changes: 25 additions & 11 deletions core/src/main/java/org/apache/iceberg/FastAppend.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,22 @@
import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
import org.apache.iceberg.relocated.com.google.common.collect.Iterables;
import org.apache.iceberg.relocated.com.google.common.collect.Lists;
import org.apache.iceberg.relocated.com.google.common.collect.Maps;
import org.apache.iceberg.util.DataFileSet;

/** {@link AppendFiles Append} implementation that adds a new manifest file for the write. */
/** {@link AppendFiles Append} implementation that adds new manifest files for writes. */
class FastAppend extends SnapshotProducer<AppendFiles> implements AppendFiles {
private final String tableName;
private final PartitionSpec spec;
private final SnapshotSummary.Builder summaryBuilder = SnapshotSummary.builder();
private final DataFileSet newFiles = DataFileSet.create();
private final Map<Integer, DataFileSet> newDataFilesBySpec = Maps.newHashMap();
private final List<ManifestFile> appendManifests = Lists.newArrayList();
private final List<ManifestFile> rewrittenAppendManifests = Lists.newArrayList();
private List<ManifestFile> newManifests = null;
private final List<ManifestFile> newManifests = Lists.newArrayList();
private boolean hasNewFiles = false;

FastAppend(String tableName, TableOperations ops) {
super(ops);
this.tableName = tableName;
this.spec = ops().current().spec();
}

@Override
Expand Down Expand Up @@ -78,14 +77,28 @@ protected Map<String, String> summary() {
@Override
public FastAppend appendFile(DataFile file) {
Preconditions.checkNotNull(file, "Invalid data file: null");
if (newFiles.add(file)) {
PartitionSpec spec = spec(file.specId());
Preconditions.checkArgument(
spec != null,
"Cannot find partition spec %s for data file: %s",
file.specId(),
file.location());

DataFileSet dataFiles =
newDataFilesBySpec.computeIfAbsent(spec.specId(), ignored -> DataFileSet.create());

if (dataFiles.add(file)) {
this.hasNewFiles = true;
summaryBuilder.addedFile(spec, file);
}

return this;
}

private PartitionSpec spec(int specId) {
return ops().current().spec(specId);
}

@Override
public FastAppend toBranch(String branch) {
targetBranch(branch);
Expand Down Expand Up @@ -176,7 +189,7 @@ protected void cleanUncommitted(Set<ManifestFile> committed) {
}
}
if (hasDeletes) {
this.newManifests = null;
this.newManifests.clear();
}
}

Expand All @@ -200,13 +213,14 @@ protected boolean cleanupAfterCommit() {
}

private List<ManifestFile> writeNewManifests() throws IOException {
if (hasNewFiles && newManifests != null) {
if (hasNewFiles && !newManifests.isEmpty()) {
newManifests.forEach(file -> deleteFile(file.path()));
newManifests = null;
newManifests.clear();
}

if (newManifests == null && !newFiles.isEmpty()) {
this.newManifests = writeDataManifests(newFiles, spec);
if (newManifests.isEmpty() && !newDataFilesBySpec.isEmpty()) {
newDataFilesBySpec.forEach(
(specId, dataFiles) -> newManifests.addAll(writeDataManifests(dataFiles, spec(specId))));
hasNewFiles = false;
}

Expand Down
62 changes: 62 additions & 0 deletions core/src/test/java/org/apache/iceberg/TestFastAppend.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import org.apache.iceberg.ManifestEntry.Status;
import org.apache.iceberg.exceptions.CommitFailedException;
import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap;
import org.apache.iceberg.relocated.com.google.common.collect.Iterables;
import org.apache.iceberg.relocated.com.google.common.collect.Lists;
import org.apache.iceberg.relocated.com.google.common.collect.Sets;
Expand Down Expand Up @@ -61,6 +63,66 @@ public void testAddManyFiles() {
validateTableFiles(table, dataFiles);
}

@TestTemplate
public void testEmptyTableFastAppendFilesWithDifferentSpecs() {
assertThat(listManifestFiles()).as("Table should start empty").isEmpty();

TableMetadata base = readMetadata();
assertThat(base.currentSnapshot()).as("Should not have a current snapshot").isNull();
assertThat(base.lastSequenceNumber()).as("Last sequence number should be 0").isEqualTo(0);

table.updateSpec().addField("id").commit();
PartitionSpec newSpec = table.spec();

assertThat(table.specs()).as("Table should have 2 specs").hasSize(2);

DataFile fileNewSpec =
DataFiles.builder(newSpec)
.withPath("/path/to/data-b.parquet")
.withPartitionPath("data_bucket=0/id=0")
.withFileSizeInBytes(10)
.withRecordCount(1)
.build();

Snapshot committedSnapshot =
commit(
table,
table.newFastAppend().appendFile(FILE_A).appendFile(fileNewSpec),
SnapshotRef.MAIN_BRANCH);

assertThat(committedSnapshot).as("Should create a snapshot").isNotNull();
V1Assert.assertEquals(
"Last sequence number should be 0", 0, table.ops().current().lastSequenceNumber());
V2Assert.assertEquals(
"Last sequence number should be 1", 1, table.ops().current().lastSequenceNumber());

assertThat(committedSnapshot.allManifests(table.io()))
.as("Should create 2 manifests for initial write, 1 manifest per spec")
.hasSize(2);

long snapshotId = committedSnapshot.snapshotId();

ImmutableMap<Integer, DataFile> expectedFileBySpec =
ImmutableMap.of(SPEC.specId(), FILE_A, newSpec.specId(), fileNewSpec);

expectedFileBySpec.forEach(
(specId, expectedDataFile) -> {
ManifestFile manifestFileForSpecId =
committedSnapshot.allManifests(table.io()).stream()
.filter(m -> Objects.equals(m.partitionSpecId(), specId))
.findAny()
.get();

validateManifest(
manifestFileForSpecId,
dataSeqs(1L),
fileSeqs(1L),
ids(snapshotId),
files(expectedDataFile),
statuses(Status.ADDED));
});
}

@TestTemplate
public void appendNullFile() {
assertThatThrownBy(() -> table.newFastAppend().appendFile(null).commit())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1590,13 +1590,15 @@ public void testCompleteCreateTransactionMultipleSchemas() {
updateSchema.commit();

UpdatePartitionSpec updateSpec = create.updateSpec().addField("new_col");
PartitionSpec newSpec = updateSpec.apply();
updateSpec.commit();

ReplaceSortOrder replaceSortOrder = create.replaceSortOrder().asc("new_col");
SortOrder newSortOrder = replaceSortOrder.apply();
replaceSortOrder.commit();

// Get new spec after commit to write new file with new spec
PartitionSpec newSpec = create.table().spec();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test was not using the latest spec. I would fail with newAppend() too.


DataFile anotherFile =
DataFiles.builder(newSpec)
.withPath("/path/to/data-b.parquet")
Expand Down