From 7c9fdf4fc2bda25804323826475200211f35e21b Mon Sep 17 00:00:00 2001 From: nk1506 Date: Tue, 9 Jan 2024 15:26:12 +0530 Subject: [PATCH] Core: Purge support on drop view operation --- .palantir/revapi.yml | 5 +++++ .../apache/iceberg/catalog/ViewCatalog.java | 15 ++++++++++++- .../iceberg/catalog/ViewSessionCatalog.java | 10 +++++++++ .../catalog/BaseViewSessionCatalog.java | 9 ++++++++ .../iceberg/inmemory/InMemoryCatalog.java | 22 +++++++++++++++++++ .../org/apache/iceberg/rest/RESTCatalog.java | 5 +++++ .../iceberg/rest/RESTSessionCatalog.java | 17 ++++++++++++++ .../apache/iceberg/nessie/NessieCatalog.java | 7 +++++- 8 files changed, 88 insertions(+), 2 deletions(-) diff --git a/.palantir/revapi.yml b/.palantir/revapi.yml index 990c9ba31afd..5a5feff01a16 100644 --- a/.palantir/revapi.yml +++ b/.palantir/revapi.yml @@ -873,6 +873,11 @@ acceptedBreaks: new: "method void org.apache.iceberg.encryption.Ciphers::()" justification: "Static utility class - should not have public constructor" "1.4.0": + org.apache.iceberg:iceberg-api: + - code: "java.method.addedToInterface" + new: "method boolean org.apache.iceberg.catalog.ViewCatalog::dropView(org.apache.iceberg.catalog.TableIdentifier,\ + \ boolean)" + justification: "Added purge support for drop view" org.apache.iceberg:iceberg-core: - code: "java.class.defaultSerializationChanged" old: "class org.apache.iceberg.mapping.NameMapping" diff --git a/api/src/main/java/org/apache/iceberg/catalog/ViewCatalog.java b/api/src/main/java/org/apache/iceberg/catalog/ViewCatalog.java index ca470eec7171..bf3e40e5d84d 100644 --- a/api/src/main/java/org/apache/iceberg/catalog/ViewCatalog.java +++ b/api/src/main/java/org/apache/iceberg/catalog/ViewCatalog.java @@ -83,7 +83,20 @@ default boolean viewExists(TableIdentifier identifier) { * @param identifier a view identifier * @return true if the view was dropped, false if the view did not exist */ - boolean dropView(TableIdentifier identifier); + default boolean dropView(TableIdentifier identifier) { + return dropView(identifier, true); + } + + /** + * Drop a view; optionally delete metadata files. + * + *

If purge is set to true the implementation should delete metadata files. + * + * @param identifier a table identifier + * @param purge if true, delete metadata files in the view + * @return true if the view was dropped, false if the view did not exist + */ + boolean dropView(TableIdentifier identifier, boolean purge); /** * Rename a view. diff --git a/api/src/main/java/org/apache/iceberg/catalog/ViewSessionCatalog.java b/api/src/main/java/org/apache/iceberg/catalog/ViewSessionCatalog.java index 106e20d3bce1..6fdd8777ca76 100644 --- a/api/src/main/java/org/apache/iceberg/catalog/ViewSessionCatalog.java +++ b/api/src/main/java/org/apache/iceberg/catalog/ViewSessionCatalog.java @@ -85,6 +85,16 @@ default boolean viewExists(SessionCatalog.SessionContext context, TableIdentifie */ boolean dropView(SessionCatalog.SessionContext context, TableIdentifier identifier); + /** + * Drop a view and request that metadata files are immediately deleted. + * + * @param context session context + * @param ident a table identifier + * @return true if the view was dropped and purged, false if the table did not exist + * @throws UnsupportedOperationException if immediate delete is not supported + */ + boolean purgeView(SessionCatalog.SessionContext context, TableIdentifier ident); + /** * Rename a view. * diff --git a/core/src/main/java/org/apache/iceberg/catalog/BaseViewSessionCatalog.java b/core/src/main/java/org/apache/iceberg/catalog/BaseViewSessionCatalog.java index 10895e1de9e6..2fb0ea5cf4d3 100644 --- a/core/src/main/java/org/apache/iceberg/catalog/BaseViewSessionCatalog.java +++ b/core/src/main/java/org/apache/iceberg/catalog/BaseViewSessionCatalog.java @@ -73,6 +73,15 @@ public boolean dropView(TableIdentifier identifier) { return BaseViewSessionCatalog.this.dropView(context, identifier); } + @Override + public boolean dropView(TableIdentifier identifier, boolean purge) { + if (purge) { + return BaseViewSessionCatalog.this.purgeView(context, identifier); + } else { + return BaseViewSessionCatalog.this.dropView(context, identifier); + } + } + @Override public void renameView(TableIdentifier from, TableIdentifier to) { BaseViewSessionCatalog.this.renameView(context, from, to); diff --git a/core/src/main/java/org/apache/iceberg/inmemory/InMemoryCatalog.java b/core/src/main/java/org/apache/iceberg/inmemory/InMemoryCatalog.java index 51d242f93419..5c3a4ea2bb22 100644 --- a/core/src/main/java/org/apache/iceberg/inmemory/InMemoryCatalog.java +++ b/core/src/main/java/org/apache/iceberg/inmemory/InMemoryCatalog.java @@ -332,6 +332,28 @@ public boolean dropView(TableIdentifier identifier) { } } + @Override + public boolean dropView(TableIdentifier identifier, boolean purge) { + TableOperations ops = newTableOps(identifier); + TableMetadata lastMetadata; + if (purge && ops.current() != null) { + lastMetadata = ops.current(); + } else { + lastMetadata = null; + } + + synchronized (this) { + if (null == views.remove(identifier)) { + return false; + } + } + + if (purge && lastMetadata != null) { + CatalogUtil.dropTableData(ops.io(), lastMetadata); + } + return true; + } + @Override public void renameView(TableIdentifier from, TableIdentifier to) { if (from.equals(to)) { diff --git a/core/src/main/java/org/apache/iceberg/rest/RESTCatalog.java b/core/src/main/java/org/apache/iceberg/rest/RESTCatalog.java index 61a7eca272df..fcc96b677c6a 100644 --- a/core/src/main/java/org/apache/iceberg/rest/RESTCatalog.java +++ b/core/src/main/java/org/apache/iceberg/rest/RESTCatalog.java @@ -288,6 +288,11 @@ public boolean dropView(TableIdentifier identifier) { return viewSessionCatalog.dropView(identifier); } + @Override + public boolean dropView(TableIdentifier identifier, boolean purge) { + return viewSessionCatalog.dropView(identifier, purge); + } + @Override public void renameView(TableIdentifier from, TableIdentifier to) { viewSessionCatalog.renameView(from, to); diff --git a/core/src/main/java/org/apache/iceberg/rest/RESTSessionCatalog.java b/core/src/main/java/org/apache/iceberg/rest/RESTSessionCatalog.java index 5f660f0f4fe8..da73a2919ab6 100644 --- a/core/src/main/java/org/apache/iceberg/rest/RESTSessionCatalog.java +++ b/core/src/main/java/org/apache/iceberg/rest/RESTSessionCatalog.java @@ -1081,6 +1081,23 @@ public boolean dropView(SessionContext context, TableIdentifier identifier) { } } + @Override + public boolean purgeView(SessionContext context, TableIdentifier identifier) { + checkViewIdentifierIsValid(identifier); + + try { + client.delete( + paths.view(identifier), + ImmutableMap.of("purgeRequested", "true"), + null, + headers(context), + ErrorHandlers.viewErrorHandler()); + return true; + } catch (NoSuchViewException e) { + return false; + } + } + @Override public void renameView(SessionContext context, TableIdentifier from, TableIdentifier to) { checkViewIdentifierIsValid(from); diff --git a/nessie/src/main/java/org/apache/iceberg/nessie/NessieCatalog.java b/nessie/src/main/java/org/apache/iceberg/nessie/NessieCatalog.java index a242bfeb67fa..ebaa05c3922f 100644 --- a/nessie/src/main/java/org/apache/iceberg/nessie/NessieCatalog.java +++ b/nessie/src/main/java/org/apache/iceberg/nessie/NessieCatalog.java @@ -360,10 +360,15 @@ public List listViews(Namespace namespace) { @Override public boolean dropView(TableIdentifier identifier) { + return dropView(identifier, false); + } + + @Override + public boolean dropView(TableIdentifier identifier, boolean purge) { TableReference tableReference = parseTableReference(identifier); return client .withReference(tableReference.getReference(), tableReference.getHash()) - .dropView(identifierWithoutTableReference(identifier, tableReference), false); + .dropView(identifierWithoutTableReference(identifier, tableReference), purge); } @Override