Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nessie: Support views for NessieCatalog #8909

Merged
merged 6 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
98 changes: 76 additions & 22 deletions nessie/src/main/java/org/apache/iceberg/nessie/NessieCatalog.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@

import java.io.IOException;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.function.Function;
import org.apache.iceberg.BaseMetastoreCatalog;
import org.apache.iceberg.CatalogProperties;
import org.apache.iceberg.CatalogUtil;
import org.apache.iceberg.TableOperations;
Expand All @@ -41,19 +41,22 @@
import org.apache.iceberg.relocated.com.google.common.base.Joiner;
import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap;
import org.apache.iceberg.view.BaseMetastoreViewCatalog;
import org.apache.iceberg.view.ViewOperations;
import org.projectnessie.client.NessieClientBuilder;
import org.projectnessie.client.NessieConfigConstants;
import org.projectnessie.client.api.NessieApiV1;
import org.projectnessie.client.api.NessieApiV2;
import org.projectnessie.client.config.NessieClientConfigSource;
import org.projectnessie.client.config.NessieClientConfigSources;
import org.projectnessie.model.Content;
import org.projectnessie.model.ContentKey;
import org.projectnessie.model.TableReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/** Nessie implementation of Iceberg Catalog. */
public class NessieCatalog extends BaseMetastoreCatalog
public class NessieCatalog extends BaseMetastoreViewCatalog
implements AutoCloseable, SupportsNamespaces, Configurable<Object> {

private static final Logger LOG = LoggerFactory.getLogger(NessieCatalog.class);
Expand Down Expand Up @@ -203,8 +206,7 @@ protected TableOperations newTableOps(TableIdentifier tableIdentifier) {
org.projectnessie.model.Namespace.of(tableIdentifier.namespace().levels()),
tr.getName()),
client.withReference(tr.getReference(), tr.getHash()),
fileIO,
catalogOptions);
fileIO);
}

@Override
Expand Down Expand Up @@ -239,33 +241,24 @@ public boolean dropTable(TableIdentifier identifier, boolean purge) {
TableReference tableReference = parseTableReference(identifier);
return client
.withReference(tableReference.getReference(), tableReference.getHash())
.dropTable(identifierWithoutTableReference(identifier, tableReference), purge);
.dropTable(identifierWithoutTableReference(identifier, tableReference), false);
Copy link
Contributor

Choose a reason for hiding this comment

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

@ajantha-bhat shouldn't this keep the purge flag?

}

@Override
public void renameTable(TableIdentifier from, TableIdentifier to) {
TableReference fromTableReference = parseTableReference(from);
nastra marked this conversation as resolved.
Show resolved Hide resolved
TableReference toTableReference = parseTableReference(to);
String fromReference =
fromTableReference.hasReference()
? fromTableReference.getReference()
: client.getRef().getName();
String toReference =
toTableReference.hasReference()
? toTableReference.getReference()
: client.getRef().getName();
Preconditions.checkArgument(
fromReference.equalsIgnoreCase(toReference),
"from: %s and to: %s reference name must be same",
fromReference,
toReference);

validateReferenceForRename(fromTableReference, toTableReference, Content.Type.ICEBERG_TABLE);

TableIdentifier fromIdentifier =
NessieUtil.removeCatalogName(
identifierWithoutTableReference(from, fromTableReference), name());
TableIdentifier toIdentifier =
NessieUtil.removeCatalogName(identifierWithoutTableReference(to, toTableReference), name());
client
.withReference(fromTableReference.getReference(), fromTableReference.getHash())
.renameTable(
identifierWithoutTableReference(from, fromTableReference),
NessieUtil.removeCatalogName(
identifierWithoutTableReference(to, toTableReference), name()));
.renameTable(fromIdentifier, toIdentifier);
}

@Override
Expand Down Expand Up @@ -347,4 +340,65 @@ private TableIdentifier identifierWithoutTableReference(
protected Map<String, String> properties() {
return catalogOptions;
}

@Override
protected ViewOperations newViewOps(TableIdentifier identifier) {
TableReference tr = parseTableReference(identifier);
return new NessieViewOperations(
ContentKey.of(
org.projectnessie.model.Namespace.of(identifier.namespace().levels()), tr.getName()),
client.withReference(tr.getReference(), tr.getHash()),
fileIO);
}

@Override
public List<TableIdentifier> listViews(Namespace namespace) {
return client.listViews(namespace);
}

@Override
public boolean dropView(TableIdentifier identifier) {
TableReference tableReference = parseTableReference(identifier);
return client
.withReference(tableReference.getReference(), tableReference.getHash())
.dropView(identifierWithoutTableReference(identifier, tableReference), false);
}

@Override
public void renameView(TableIdentifier from, TableIdentifier to) {
TableReference fromTableReference = parseTableReference(from);
TableReference toTableReference = parseTableReference(to);

validateReferenceForRename(fromTableReference, toTableReference, Content.Type.ICEBERG_VIEW);

TableIdentifier fromIdentifier =
NessieUtil.removeCatalogName(
identifierWithoutTableReference(from, fromTableReference), name());
TableIdentifier toIdentifier =
NessieUtil.removeCatalogName(identifierWithoutTableReference(to, toTableReference), name());
client
.withReference(fromTableReference.getReference(), fromTableReference.getHash())
.renameView(fromIdentifier, toIdentifier);
}

private void validateReferenceForRename(
TableReference fromTableReference, TableReference toTableReference, Content.Type type) {
String fromReference =
fromTableReference.hasReference()
? fromTableReference.getReference()
: client.getRef().getName();
String toReference =
toTableReference.hasReference()
? toTableReference.getReference()
: client.getRef().getName();
Preconditions.checkArgument(
fromReference.equalsIgnoreCase(toReference),
"Cannot rename %s '%s' on reference '%s' to '%s' on reference '%s':"
+ " source and target references must be the same.",
NessieUtil.contentTypeString(type).toLowerCase(Locale.ENGLISH),
fromTableReference.getName(),
fromReference,
toTableReference.getName(),
toReference);
}
}
Loading