Skip to content
Draft
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
11 changes: 10 additions & 1 deletion core/src/main/java/org/apache/iceberg/util/SnapshotUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,16 @@ public static Schema schemaFor(TableMetadata metadata, String ref) {
}

Snapshot snapshot = metadata.snapshot(snapshotRef.snapshotId());
return metadata.schemas().get(snapshot.schemaId());
Integer schemaId = snapshot.schemaId();
// schemaId could be null, if snapshot was created before Iceberg added schema id to snapshot
if (schemaId != null) {
Schema schema = metadata.schemasById().get(schemaId);
Preconditions.checkState(schema != null, "Cannot find schema with schema id %s", schemaId);
return schema;
}

// TODO: recover the schema by reading previous metadata files
return metadata.schema();
}

/**
Expand Down
70 changes: 70 additions & 0 deletions core/src/test/java/org/apache/iceberg/util/TestSnapshotUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@
import java.util.stream.StreamSupport;
import org.apache.iceberg.DataFile;
import org.apache.iceberg.DataFiles;
import org.apache.iceberg.HasTableOperations;
import org.apache.iceberg.MetadataTableType;
import org.apache.iceberg.MetadataTableUtils;
import org.apache.iceberg.PartitionSpec;
import org.apache.iceberg.Schema;
import org.apache.iceberg.Snapshot;
import org.apache.iceberg.SnapshotRef;
import org.apache.iceberg.Table;
import org.apache.iceberg.TableMetadata;
import org.apache.iceberg.TestHelpers;
import org.apache.iceberg.TestTables;
import org.apache.iceberg.types.Types;
Expand Down Expand Up @@ -253,6 +255,74 @@ public void schemaForTag() {
assertThat(SnapshotUtil.schemaFor(table, tag).asStruct()).isEqualTo(initialSchema.asStruct());
}

@Test
public void schemaForTagWithMetadata() {
// Evolve the schema twice and tag the snapshot written under the middle schema.
table.updateSchema().addColumn("col1", Types.IntegerType.get()).commit();
appendFileToMain();
long taggedSnapshotId = table.currentSnapshot().snapshotId();
table.updateSchema().addColumn("col2", Types.IntegerType.get()).commit();
appendFileToMain();

String tag = "tag";
table.manageSnapshots().createTag(tag, taggedSnapshotId).commit();

// Drop the branches set up in before() so the oldest snapshots become unreferenced.
table.manageSnapshots().removeBranch("b1").removeBranch("fork").commit();

// Drop the unreferenced oldest schema so schema ids no longer match list positions:
// the tagged snapshot keeps schema id 1, which now sits at list index 0.
table
.expireSnapshots()
.cleanExpiredMetadata(true)
.expireOlderThan(table.snapshot(taggedSnapshotId).timestampMillis())
.retainLast(2)
.commit();

TableMetadata metadata = ((HasTableOperations) table).operations().current();
Schema expected =
new Schema(
required(1, "id", Types.IntegerType.get()),
required(2, "data", Types.StringType.get()),
optional(3, "col1", Types.IntegerType.get()));

assertThat(metadata.snapshot(taggedSnapshotId).schemaId()).isEqualTo(1);
assertThat(SnapshotUtil.schemaFor(metadata, tag).asStruct()).isEqualTo(expected.asStruct());
}

@Test
public void schemaForTagWithMetadataSchemaIdBeyondListSize() {
// Tag the latest schema, then drop the oldest unreferenced schema so the schemas list shrinks
// while the tagged snapshot keeps the highest schema id, which is now beyond the list size.
table.updateSchema().addColumn("col1", Types.IntegerType.get()).commit();
appendFileToMain();
table.updateSchema().addColumn("col2", Types.IntegerType.get()).commit();
appendFileToMain();
long taggedSnapshotId = table.currentSnapshot().snapshotId();

String tag = "tag";
table.manageSnapshots().createTag(tag, taggedSnapshotId).commit();

table
.expireSnapshots()
.cleanExpiredMetadata(true)
.expireOlderThan(table.snapshot(taggedSnapshotId).timestampMillis())
.retainLast(1)
.commit();

TableMetadata metadata = ((HasTableOperations) table).operations().current();
Schema expected =
new Schema(
required(1, "id", Types.IntegerType.get()),
required(2, "data", Types.StringType.get()),
optional(3, "col1", Types.IntegerType.get()),
optional(4, "col2", Types.IntegerType.get()));

assertThat(metadata.snapshot(taggedSnapshotId).schemaId())
.isGreaterThanOrEqualTo(metadata.schemas().size());
assertThat(SnapshotUtil.schemaFor(metadata, tag).asStruct()).isEqualTo(expected.asStruct());
}

@Test
public void schemaForSnapshotId() {
Schema initialSchema =
Expand Down