Skip to content
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
2 changes: 2 additions & 0 deletions core/src/main/java/org/apache/iceberg/TableMetadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -1167,6 +1167,7 @@
specs.stream()
.filter(s -> !specIdsToRemove.contains(s.specId()))
.collect(Collectors.toList());
specsById.keySet().removeAll(specIdsToRemove);
changes.add(new MetadataUpdate.RemovePartitionSpecs(specIdsToRemove));
}

Expand All @@ -1183,6 +1184,7 @@
schemas.stream()
.filter(s -> !schemaIdsToRemove.contains(s.schemaId()))
.collect(Collectors.toList());
schemasById.keySet().removeAll(schemaIdsToRemove);
changes.add(new MetadataUpdate.RemoveSchemas(schemaIdsToRemove));
}

Expand Down Expand Up @@ -1846,11 +1848,11 @@
Set<Long> addedSnapshotIds = Sets.newHashSet();
Set<Long> intermediateSnapshotIds = Sets.newHashSet();
for (MetadataUpdate update : changes) {
if (update instanceof MetadataUpdate.AddSnapshot) {

Check warning on line 1851 in core/src/main/java/org/apache/iceberg/TableMetadata.java

View workflow job for this annotation

GitHub Actions / build-checks (17, pull_request)

[PatternMatchingInstanceof] This code can be simplified to use a pattern-matching instanceof.
// adds must always come before set current snapshot
MetadataUpdate.AddSnapshot addSnapshot = (MetadataUpdate.AddSnapshot) update;
addedSnapshotIds.add(addSnapshot.snapshot().snapshotId());
} else if (update instanceof MetadataUpdate.SetSnapshotRef) {

Check warning on line 1855 in core/src/main/java/org/apache/iceberg/TableMetadata.java

View workflow job for this annotation

GitHub Actions / build-checks (17, pull_request)

[PatternMatchingInstanceof] This code can be simplified to use a pattern-matching instanceof.
MetadataUpdate.SetSnapshotRef setRef = (MetadataUpdate.SetSnapshotRef) update;
long snapshotId = setRef.snapshotId();
if (addedSnapshotIds.contains(snapshotId)
Expand Down
68 changes: 68 additions & 0 deletions core/src/test/java/org/apache/iceberg/TestTableMetadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -2071,6 +2071,74 @@ public void testMetadataWithRemoveSchemas() {
assertThat(meta.changes()).anyMatch(u -> u instanceof MetadataUpdate.RemoveSchemas);
}

@Test
public void testRemoveSchemasThenReuseRemovedId() {
TableMetadata base =
TableMetadata.buildFrom(
TableMetadata.newTableMetadata(
TEST_SCHEMA, PartitionSpec.unpartitioned(), TEST_LOCATION, ImmutableMap.of()))
.addSchema(
new Schema(
Types.NestedField.required(1, "x", Types.LongType.get()),
Types.NestedField.optional(4, "unused", Types.LongType.get())))
.build();
assertThat(base.currentSchemaId()).isEqualTo(0);
assertThat(base.schemasById()).containsOnlyKeys(0, 1);

assertThatThrownBy(
() ->
TableMetadata.buildFrom(base).removeSchemas(Sets.newHashSet(1)).setCurrentSchema(1))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("Cannot set current schema to unknown schema: 1");

// the new schema is assigned the id of the removed schema
Schema newSchema =
new Schema(
Types.NestedField.required(1, "x", Types.LongType.get()),
Types.NestedField.required(2, "y", Types.LongType.get(), "comment"),
Types.NestedField.required(3, "z", Types.LongType.get()),
Types.NestedField.optional(5, "w", Types.LongType.get()));
TableMetadata updated =
TableMetadata.buildFrom(base)
.removeSchemas(Sets.newHashSet(1))
.setCurrentSchema(newSchema, 5)
.build();

assertThat(updated.schemasById()).containsOnlyKeys(0, 1);
assertThat(updated.currentSchemaId()).isEqualTo(1);
assertThat(updated.schema().asStruct()).isEqualTo(newSchema.asStruct());
}

@Test
public void testRemoveSpecsThenReuseRemovedId() {
TableMetadata created =
TableMetadata.newTableMetadata(
TEST_SCHEMA, PartitionSpec.unpartitioned(), TEST_LOCATION, ImmutableMap.of());
TableMetadata base =
TableMetadata.buildFrom(created)
.addPartitionSpec(PartitionSpec.builderFor(created.schema()).bucket("y", 16).build())
.build();
assertThat(base.defaultSpecId()).isEqualTo(0);
assertThat(base.specsById()).containsOnlyKeys(0, 1);

// the new spec is assigned the id of the removed spec
PartitionSpec newSpec = PartitionSpec.builderFor(base.schema()).identity("x").build();
TableMetadata updated =
TableMetadata.buildFrom(base)
.removeSpecs(Sets.newHashSet(1))
.setDefaultPartitionSpec(newSpec)
.build();

assertThat(updated.specsById()).containsOnlyKeys(0, 1);
assertThat(updated.defaultSpecId()).isEqualTo(1);
assertThat(updated.spec())
.isEqualTo(
PartitionSpec.builderFor(updated.schema())
.withSpecId(1)
.add(1, 1000, "x", Transforms.identity())
.build());
}

@Test
public void testAddSnapshotWithStaleSequenceNumberIsRetryable() {
TableMetadata base =
Expand Down
66 changes: 66 additions & 0 deletions core/src/test/java/org/apache/iceberg/catalog/CatalogTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -2013,6 +2013,72 @@ public void testRemoveUnusedSchemas(boolean withBranch) {
}
}

@Test
public void testRemoveUnusedSchemaThenAddColumnInTransaction() {
C catalog = catalog();

if (requiresNamespaceCreate()) {
catalog.createNamespace(NS);
}

Table table =
catalog.buildTable(TABLE, SCHEMA).withProperty(TableProperties.GC_ENABLED, "true").create();

// the current schema goes back to id 0 and schema 1 is no longer used
table.updateSchema().addColumn("col_to_delete", Types.IntegerType.get()).commit();
table.updateSchema().deleteColumn("col_to_delete").commit();
assertThat(table.schema().schemaId()).isEqualTo(0);
assertThat(table.schemas().keySet()).containsExactlyInAnyOrder(0, 1);

// schema 1 is removed and the new schema is assigned the freed id
Transaction transaction = table.newTransaction();
transaction.expireSnapshots().cleanExpiredMetadata(true).commit();
transaction.updateSchema().addColumn("new_col", Types.LongType.get()).commit();
transaction.commitTransaction();

Table loaded = catalog.loadTable(TABLE);
assertThat(loaded.schema().schemaId()).isEqualTo(1);
assertThat(loaded.schema().findField("new_col")).isNotNull();
assertThat(loaded.schema().findField("col_to_delete")).isNull();
assertThat(loaded.schemas().keySet()).containsExactlyInAnyOrder(0, 1);
}

@Test
public void testRemoveUnusedSpecThenAddFieldInTransaction() {
C catalog = catalog();

if (requiresNamespaceCreate()) {
catalog.createNamespace(NS);
}

// create a v2 table. otherwise removing the field would produce a new spec with a void
// partition field instead of going back to spec 0
Table table =
catalog
.buildTable(TABLE, SCHEMA)
.withProperty(TableProperties.GC_ENABLED, "true")
.withProperty("format-version", "2")
.create();

// the default spec goes back to id 0 and spec 1 is no longer used
table.updateSpec().addField(Expressions.bucket("data", 16)).commit();
table.updateSpec().removeField(Expressions.bucket("data", 16)).commit();
assertThat(table.spec().specId()).isEqualTo(0);
assertThat(table.specs().keySet()).containsExactlyInAnyOrder(0, 1);

// spec 1 is removed and the new spec is assigned the freed id
Transaction transaction = table.newTransaction();
transaction.expireSnapshots().cleanExpiredMetadata(true).commit();
transaction.updateSpec().addField("id").commit();
transaction.commitTransaction();

Table loaded = catalog.loadTable(TABLE);
assertThat(loaded.spec().specId()).isEqualTo(1);
assertThat(loaded.spec().fields()).hasSize(1);
assertThat(loaded.spec().fields().get(0).name()).isEqualTo("id");
assertThat(loaded.specs().keySet()).containsExactlyInAnyOrder(0, 1);
}

@Test
public void testUpdateTableSortOrder() {
C catalog = catalog();
Expand Down
Loading