World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.
Java ai-catalogdata-catalogdatalakefederated-querylakehousemetadatametalakemodel-catalogopendatacatalogskycomputingstratosphere
[#9736][followup] feat(iceberg-rest): Implement snapshots=refs filtering in loadTable (#10593)
akshay thorat183bec9fcc58
diff --git a/iceberg/iceberg-rest-server/src/main/java/org/apache/gravitino/iceberg/service/rest/IcebergTableOperations.java b/iceberg/iceberg-rest-server/src/main/java/org/apache/gravitino/iceberg/service/rest/IcebergTableOperations.javaindex 78b5381187..7754f1037c 100644--- a/iceberg/iceberg-rest-server/src/main/java/org/apache/gravitino/iceberg/service/rest/IcebergTableOperations.java+++ b/iceberg/iceberg-rest-server/src/main/java/org/apache/gravitino/iceberg/service/rest/IcebergTableOperations.java@@ -28,7 +28,10 @@ import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;import java.util.ArrayList;import java.util.List;+import java.util.Map;import java.util.Optional;+import java.util.Set;+import java.util.stream.Collectors;import javax.inject.Inject;import javax.servlet.http.HttpServletRequest;import javax.ws.rs.Consumes;@@ -67,6 +70,8 @@ import org.apache.gravitino.server.authorization.annotations.IcebergAuthorizatioimport org.apache.gravitino.server.authorization.annotations.IcebergAuthorizationMetadata.RequestType;import org.apache.gravitino.server.authorization.expression.AuthorizationExpressionConstants;import org.apache.gravitino.server.web.Utils;+import org.apache.iceberg.SnapshotRef;+import org.apache.iceberg.TableMetadata;import org.apache.iceberg.catalog.Namespace;import org.apache.iceberg.catalog.TableIdentifier;import org.apache.iceberg.rest.RESTUtil;@@ -94,6 +99,7 @@ public class IcebergTableOperations {@VisibleForTesting public static final String IF_NONE_MATCH = "If-None-Match";@VisibleForTesting static final String DEFAULT_SNAPSHOTS = "all";+ @VisibleForTesting static final String SNAPSHOTS_REFS = "refs";private IcebergMetricsManager icebergMetricsManager;@@ -306,7 +312,6 @@ public class IcebergTableOperations {tableName,accessDelegation,isCredentialVending);- // todo support snapshotstry {return Utils.doAs(httpRequest,@@ -334,6 +339,9 @@ public class IcebergTableOperations {if (etag != null && etagMatches(ifNoneMatch, etag)) {return Response.notModified(etag).build();}+ if (SNAPSHOTS_REFS.equals(snapshots)) {+ loadTableResponse = filterSnapshotsByRefs(loadTableResponse);+ }return buildResponseWithETag(loadTableResponse, etag);});} catch (Exception e) {@@ -536,6 +544,33 @@ public class IcebergTableOperations {}}+ /**+ * Filters the {@link LoadTableResponse} to include only snapshots that are directly referenced by+ * the table's refs (branches and tags). This implements the {@code snapshots=refs} query+ * parameter behavior per the Iceberg REST specification.+ *+ * @param loadTableResponse the original response containing all snapshots+ * @return a new response with only ref-referenced snapshots+ */+ @VisibleForTesting+ static LoadTableResponse filterSnapshotsByRefs(LoadTableResponse loadTableResponse) {+ TableMetadata metadata = loadTableResponse.tableMetadata();+ Map<String, SnapshotRef> refs = metadata.refs();+ Set<Long> referencedSnapshotIds =+ refs.values().stream().map(SnapshotRef::snapshotId).collect(Collectors.toSet());+ // If all snapshots are already referenced by refs, return the original response+ if (metadata.snapshots().stream()+ .allMatch(s -> referencedSnapshotIds.contains(s.snapshotId()))) {+ return loadTableResponse;+ }+ TableMetadata filteredMetadata =+ TableMetadata.buildFrom(metadata).suppressHistoricalSnapshots().build();+ return LoadTableResponse.builder()+ .withTableMetadata(filteredMetadata)+ .addAllConfig(loadTableResponse.config())+ .build();+ }+/*** Builds an OK response with the ETag header derived from the table metadata location. Uses the* default snapshots value to ensure ETags from create/update are consistent with the defaultdiff --git a/iceberg/iceberg-rest-server/src/test/java/org/apache/gravitino/iceberg/service/rest/TestIcebergTableOperations.java b/iceberg/iceberg-rest-server/src/test/java/org/apache/gravitino/iceberg/service/rest/TestIcebergTableOperations.javaindex cd904f14d9..f447d1c492 100644--- a/iceberg/iceberg-rest-server/src/test/java/org/apache/gravitino/iceberg/service/rest/TestIcebergTableOperations.java+++ b/iceberg/iceberg-rest-server/src/test/java/org/apache/gravitino/iceberg/service/rest/TestIcebergTableOperations.java@@ -66,6 +66,7 @@ import org.apache.gravitino.server.authorization.GravitinoAuthorizerProvider;import org.apache.iceberg.MetadataUpdate;import org.apache.iceberg.Schema;import org.apache.iceberg.Snapshot;+import org.apache.iceberg.SnapshotRef;import org.apache.iceberg.TableMetadata;import org.apache.iceberg.UpdateRequirement;import org.apache.iceberg.UpdateRequirements;@@ -981,4 +982,68 @@ public class TestIcebergTableOperations extends IcebergNamespaceTestBase {Assertions.assertEquals(allEtag, allEtag2, "ETag should be consistent for the same snapshots value");}++ @ParameterizedTest+ @MethodSource("org.apache.gravitino.iceberg.service.rest.IcebergRestTestUtil#testNamespaces")+ void testLoadTableSnapshotsRefsFiltering(Namespace namespace) {+ verifyCreateNamespaceSucc(namespace);+ // Create table with data; generatePlanData=true produces two snapshots (create + append),+ // but only the latest snapshot is referenced by the "main" branch.+ verifyCreateTableSucc(namespace, "snapshots_refs_foo1", true);++ // Load with snapshots=all: should return all snapshots+ Response allResponse = doLoadTableWithSnapshots(namespace, "snapshots_refs_foo1", "all");+ Assertions.assertEquals(Status.OK.getStatusCode(), allResponse.getStatus());+ LoadTableResponse allTableResponse = allResponse.readEntity(LoadTableResponse.class);+ List<Snapshot> allSnapshots = allTableResponse.tableMetadata().snapshots();+ Assertions.assertTrue(+ allSnapshots.size() >= 2,+ "Table with data should have at least 2 snapshots, got " + allSnapshots.size());++ // Collect snapshot IDs referenced by refs+ Map<String, SnapshotRef> refs = allTableResponse.tableMetadata().refs();+ Set<Long> referencedSnapshotIds =+ refs.values().stream().map(SnapshotRef::snapshotId).collect(Collectors.toSet());++ // Load with snapshots=refs: should return only ref-referenced snapshots+ Response refsResponse = doLoadTableWithSnapshots(namespace, "snapshots_refs_foo1", "refs");+ Assertions.assertEquals(Status.OK.getStatusCode(), refsResponse.getStatus());+ LoadTableResponse refsTableResponse = refsResponse.readEntity(LoadTableResponse.class);+ List<Snapshot> refsSnapshots = refsTableResponse.tableMetadata().snapshots();++ // The returned snapshots should be exactly the ref-referenced snapshots+ Assertions.assertEquals(+ referencedSnapshotIds,+ refsSnapshots.stream().map(Snapshot::snapshotId).collect(Collectors.toSet()),+ "snapshots=refs should return exactly the ref-referenced snapshots");++ // Refs should be preserved in the filtered response+ Assertions.assertEquals(+ refs.keySet(),+ refsTableResponse.tableMetadata().refs().keySet(),+ "Refs should be preserved in filtered response");+ }++ @ParameterizedTest+ @MethodSource("org.apache.gravitino.iceberg.service.rest.IcebergRestTestUtil#testNamespaces")+ void testLoadTableSnapshotsAllReturnsAllSnapshots(Namespace namespace) {+ verifyCreateNamespaceSucc(namespace);+ verifyCreateTableSucc(namespace, "snapshots_all_foo1", true);++ // Load with default snapshots=all+ Response defaultResponse = doLoadTable(namespace, "snapshots_all_foo1");+ Assertions.assertEquals(Status.OK.getStatusCode(), defaultResponse.getStatus());+ LoadTableResponse defaultTableResponse = defaultResponse.readEntity(LoadTableResponse.class);++ // Load with explicit snapshots=all+ Response allResponse = doLoadTableWithSnapshots(namespace, "snapshots_all_foo1", "all");+ Assertions.assertEquals(Status.OK.getStatusCode(), allResponse.getStatus());+ LoadTableResponse allTableResponse = allResponse.readEntity(LoadTableResponse.class);++ // Both should return the same number of snapshots+ Assertions.assertEquals(+ defaultTableResponse.tableMetadata().snapshots().size(),+ allTableResponse.tableMetadata().snapshots().size(),+ "Default load and snapshots=all should return the same number of snapshots");+ }}