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: Add TableMetadata#buildFromEmpty(formatVersion) API #1

Merged
Merged
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
14 changes: 11 additions & 3 deletions core/src/main/java/org/apache/iceberg/TableMetadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -853,7 +853,11 @@ public static Builder buildFrom(TableMetadata base) {
}

public static Builder buildFromEmpty() {
return new Builder();
return new Builder(DEFAULT_TABLE_FORMAT_VERSION);
}

public static Builder buildFromEmpty(int formatVersion) {
return new Builder(formatVersion);
}

public static class Builder {
Expand Down Expand Up @@ -903,8 +907,12 @@ public static class Builder {
private final Map<Integer, SortOrder> sortOrdersById;

private Builder() {
this(DEFAULT_TABLE_FORMAT_VERSION);
}

public Builder(int formatVersion) {
this.base = null;
this.formatVersion = DEFAULT_TABLE_FORMAT_VERSION;
this.formatVersion = formatVersion;
this.lastSequenceNumber = INITIAL_SEQUENCE_NUMBER;
this.uuid = UUID.randomUUID().toString();
this.schemas = Lists.newArrayList();
Expand Down Expand Up @@ -986,7 +994,7 @@ public Builder assignUUID(String newUuid) {

// it is only safe to set the format version directly while creating tables
// in all other cases, use upgradeFormatVersion
public Builder setInitialFormatVersion(int newFormatVersion) {
private Builder setInitialFormatVersion(int newFormatVersion) {
Preconditions.checkArgument(
newFormatVersion <= SUPPORTED_TABLE_FORMAT_VERSION,
"Unsupported format version: v%s (supported: v%s)",
Expand Down
23 changes: 10 additions & 13 deletions core/src/main/java/org/apache/iceberg/rest/CatalogHandlers.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.time.ZoneOffset;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -374,19 +375,15 @@ private static boolean isCreate(UpdateTableRequest request) {
private static TableMetadata create(TableOperations ops, UpdateTableRequest request) {
// the only valid requirement is that the table will be created
request.requirements().forEach(requirement -> requirement.validate(ops.current()));

TableMetadata.Builder builder = TableMetadata.buildFromEmpty();
request
.updates()
.forEach(
update -> {
if (update instanceof UpgradeFormatVersion) {
builder.setInitialFormatVersion(((UpgradeFormatVersion) update).formatVersion());
} else {
update.applyTo(builder);
}
});

Optional<Integer> formatVersion =
request.updates().stream()
.filter(update -> update instanceof UpgradeFormatVersion)
.map(update -> ((UpgradeFormatVersion) update).formatVersion())
.findFirst();

TableMetadata.Builder builder =
formatVersion.map(TableMetadata::buildFromEmpty).orElseGet(TableMetadata::buildFromEmpty);
request.updates().forEach(update -> update.applyTo(builder));
// create transactions do not retry. if the table exists, retrying is not a solution
ops.commit(null, builder.build());

Expand Down