Skip to content

Commit

Permalink
Store formatVersion as field in SerializableTable
Browse files Browse the repository at this point in the history
  • Loading branch information
nastra committed Dec 6, 2024
1 parent aa990b6 commit 1d8ef05
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 6 deletions.
18 changes: 17 additions & 1 deletion core/src/main/java/org/apache/iceberg/SerializableTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,14 @@ public class SerializableTable implements Table, HasTableOperations, Serializabl
private final FileIO io;
private final EncryptionManager encryption;
private final Map<String, SnapshotRef> refs;
private final UUID uuid;
private final int formatVersion;

private transient volatile LocationProvider lazyLocationProvider = null;
private transient volatile Table lazyTable = null;
private transient volatile Schema lazySchema = null;
private transient volatile Map<Integer, PartitionSpec> lazySpecs = null;
private transient volatile SortOrder lazySortOrder = null;
private final UUID uuid;

protected SerializableTable(Table table) {
this.name = table.name();
Expand All @@ -85,6 +86,11 @@ protected SerializableTable(Table table) {
this.encryption = table.encryption();
this.refs = SerializableMap.copyOf(table.refs());
this.uuid = table.uuid();

// formatVersion=-1 will never be used/returned, because
// SerializableMetadataTable#formatVersion() will throw an UOE when the format version is
// retrieved
this.formatVersion = table instanceof BaseMetadataTable ? -1 : TableUtil.formatVersion(table);
}

/**
Expand Down Expand Up @@ -158,6 +164,10 @@ public Map<String, String> properties() {
return properties;
}

public int formatVersion() {
return formatVersion;
}

@Override
public Schema schema() {
if (lazySchema == null) {
Expand Down Expand Up @@ -428,6 +438,12 @@ public StaticTableOperations operations() {
this.getClass().getName() + " does not support operations()");
}

@Override
public int formatVersion() {
throw new UnsupportedOperationException(
this.getClass().getName() + " does not have a format version");
}

public MetadataTableType type() {
return type;
}
Expand Down
6 changes: 4 additions & 2 deletions core/src/main/java/org/apache/iceberg/TableUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ public static int formatVersion(Table table) {

// SerializableMetadataTable is a subclass of SerializableTable but does not support
// operations()
if (table instanceof HasTableOperations
&& !(table instanceof SerializableTable.SerializableMetadataTable)) {
if (table instanceof SerializableTable) {
SerializableTable serializableTable = (SerializableTable) table;
return serializableTable.formatVersion();
} else if (table instanceof HasTableOperations) {
return ((HasTableOperations) table).operations().current().formatVersion();
} else {
throw new IllegalArgumentException(
Expand Down
4 changes: 2 additions & 2 deletions core/src/test/java/org/apache/iceberg/TestTableUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ public void formatVersionForMetadataTables() {
"%s does not have a format version", metadataTable.getClass().getSimpleName());

assertThatThrownBy(() -> TableUtil.formatVersion(SerializableTable.copyOf(metadataTable)))
.isInstanceOf(IllegalArgumentException.class)
.isInstanceOf(UnsupportedOperationException.class)
.hasMessage(
"%s does not have a format version",
SerializableTable.SerializableMetadataTable.class.getSimpleName());
SerializableTable.SerializableMetadataTable.class.getName());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public void testSerializableMetadataTable() throws IOException, ClassNotFoundExc
.isInstanceOf(UnsupportedOperationException.class)
.hasMessageEndingWith("does not support operations()");
assertThatThrownBy(() -> TableUtil.formatVersion(serializableTable))
.isInstanceOf(IllegalArgumentException.class)
.isInstanceOf(UnsupportedOperationException.class)
.hasMessageEndingWith("does not have a format version");
}
}
Expand Down

0 comments on commit 1d8ef05

Please sign in to comment.