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
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,7 @@ public static LoadTableResponse loadTable(
TableMetadata.buildFrom(loadedMetadata)
.withMetadataLocation(loadedMetadata.metadataFileLocation())
.suppressHistoricalSnapshots()
.discardChanges()
.build();
break;
default:
Expand Down
71 changes: 71 additions & 0 deletions core/src/test/java/org/apache/iceberg/rest/TestRESTCatalog.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@
import org.apache.iceberg.CatalogProperties;
import org.apache.iceberg.DataFile;
import org.apache.iceberg.DataFiles;
import org.apache.iceberg.GenericBlobMetadata;
import org.apache.iceberg.GenericStatisticsFile;
import org.apache.iceberg.HasTableOperations;
import org.apache.iceberg.HistoryEntry;
import org.apache.iceberg.MetadataUpdate;
Expand Down Expand Up @@ -1123,6 +1125,75 @@ public void testTableSnapshotLoading() {
any());
}

@Test
public void testTableSnapshotLoadingWithStatisticsOnHistoricalSnapshot() {
RESTCatalogAdapter adapter = Mockito.spy(new RESTCatalogAdapter(backendCatalog));

RESTCatalog catalog =
new RESTCatalog(SessionCatalog.SessionContext.createEmpty(), (config) -> adapter);
catalog.initialize(
"test",
ImmutableMap.of(
CatalogProperties.URI,
"ignored",
CatalogProperties.FILE_IO_IMPL,
"org.apache.iceberg.inmemory.InMemoryFileIO",
RESTCatalogProperties.SNAPSHOT_LOADING_MODE,
SnapshotMode.REFS.name()));

if (requiresNamespaceCreate()) {
catalog.createNamespace(TABLE.namespace());
}

Table table = catalog.createTable(TABLE, SCHEMA);
table
.newFastAppend()
.appendFile(
DataFiles.builder(PartitionSpec.unpartitioned())
.withPath("/path/to/data-a.parquet")
.withFileSizeInBytes(10)
.withRecordCount(2)
.build())
.commit();

long firstSnapshotId = table.currentSnapshot().snapshotId();

// add a statistics file to the first snapshot
table
.updateStatistics()
.setStatistics(
new GenericStatisticsFile(
firstSnapshotId,
"/path/to/stats.puffin",
100,
42,
ImmutableList.of(
new GenericBlobMetadata(
"some-stats", firstSnapshotId, 1, ImmutableList.of(1), ImmutableMap.of()))))
.commit();

// second snapshot makes the first snapshot historical (unreferenced)
table
.newFastAppend()
.appendFile(
DataFiles.builder(PartitionSpec.unpartitioned())
.withPath("/path/to/data-b.parquet")
.withFileSizeInBytes(10)
.withRecordCount(2)
.build())
.commit();

Table refsTable = catalog.loadTable(TABLE);

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This line throws an exception if we revert CatalogHandlers's change:

Cannot set metadata location with changes to table metadata: 1 changes
java.lang.IllegalArgumentException: Cannot set metadata location with changes to table metadata: 1 changes
	at org.apache.iceberg.rest.ErrorHandlers$DefaultErrorHandler.accept(ErrorHandlers.java:339)
	at org.apache.iceberg.rest.ErrorHandlers$TableErrorHandler.accept(ErrorHandlers.java:160)
	at org.apache.iceberg.rest.ErrorHandlers$TableErrorHandler.accept(ErrorHandlers.java:142)
	at org.apache.iceberg.rest.RESTCatalogAdapter.execute(RESTCatalogAdapter.java:649)
	at java.base/java.lang.invoke.MethodHandle.invokeWithArguments(MethodHandle.java:732)
	at org.apache.iceberg.rest.RESTCatalogAdapter.execute(RESTCatalogAdapter.java:624)
	at java.base/java.lang.invoke.MethodHandle.invokeWithArguments(MethodHandle.java:732)
	at org.apache.iceberg.rest.RESTCatalogAdapter.execute(RESTCatalogAdapter.java:613)
	at org.apache.iceberg.rest.BaseHTTPClient.get(BaseHTTPClient.java:101)
	at java.base/java.lang.invoke.MethodHandle.invokeWithArguments(MethodHandle.java:732)
	at org.apache.iceberg.rest.BaseHTTPClient.get(BaseHTTPClient.java:100)
	at org.apache.iceberg.rest.RESTSessionCatalog.loadInternal(RESTSessionCatalog.java:453)
	at org.apache.iceberg.rest.RESTSessionCatalog.loadTable(RESTSessionCatalog.java:483)
	at org.apache.iceberg.catalog.BaseSessionCatalog$AsCatalog.loadTable(BaseSessionCatalog.java:105)
	at org.apache.iceberg.rest.RESTCatalog.loadTable(RESTCatalog.java:124)
	at org.apache.iceberg.rest.TestRESTCatalog.testTableSnapshotLoadingWithStatisticsOnHistoricalSnapshot(TestRESTCatalog.java:1187)
	at java.base/java.lang.reflect.Method.invoke(Method.java:569)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)


assertThat(((BaseTable) refsTable).operations().current())
.extracting("snapshots")
.asInstanceOf(InstanceOfAssertFactories.list(Snapshot.class))
.hasSize(1);

assertThat(refsTable.currentSnapshot()).isEqualTo(table.currentSnapshot());
Comment thread
ebyhr marked this conversation as resolved.
assertThat(refsTable.statisticsFiles()).isEmpty();
}

@ParameterizedTest
@ValueSource(strings = {"1", "2"})
public void testTableSnapshotLoadingWithDivergedBranches(String formatVersion) {
Expand Down
Loading