Skip to content

Commit

Permalink
Nessie: Support views for NessieCatalog
Browse files Browse the repository at this point in the history
  • Loading branch information
ajantha-bhat committed Oct 26, 2023
1 parent e2b56da commit 89b3a21
Show file tree
Hide file tree
Showing 13 changed files with 1,457 additions and 128 deletions.
22 changes: 18 additions & 4 deletions core/src/test/java/org/apache/iceberg/view/ViewCatalogTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -400,8 +400,15 @@ public void replaceTableViaTransactionThatAlreadyExistsAsView() {
.buildTable(viewIdentifier, SCHEMA)
.replaceTransaction()
.commitTransaction())
.isInstanceOf(NoSuchTableException.class)
.hasMessageStartingWith("Table does not exist: ns.view");
.satisfiesAnyOf(
throwable ->
assertThat(throwable)
.isInstanceOf(NoSuchTableException.class)
.hasMessageStartingWith("Table does not exist: ns.view"),
throwable ->
assertThat(throwable)
.isInstanceOf(AlreadyExistsException.class)
.hasMessageContaining("View with same name already exists: ns.view"));
}

@Test
Expand Down Expand Up @@ -465,8 +472,15 @@ public void replaceViewThatAlreadyExistsAsTable() {
.withDefaultNamespace(tableIdentifier.namespace())
.withQuery("spark", "select * from ns.tbl")
.replace())
.isInstanceOf(NoSuchViewException.class)
.hasMessageStartingWith("View does not exist: ns.table");
.satisfiesAnyOf(
throwable ->
assertThat(throwable)
.isInstanceOf(NoSuchViewException.class)
.hasMessageStartingWith("View does not exist: ns.table"),
throwable ->
assertThat(throwable)
.isInstanceOf(AlreadyExistsException.class)
.hasMessageContaining("Table with same name already exists: ns.table"));
}

@Test
Expand Down
55 changes: 53 additions & 2 deletions nessie/src/main/java/org/apache/iceberg/nessie/NessieCatalog.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
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,6 +40,8 @@
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;
Expand All @@ -53,7 +54,7 @@
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 @@ -347,4 +348,54 @@ 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,
catalogOptions);
}

@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);
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);

client
.withReference(fromTableReference.getReference(), fromTableReference.getHash())
.renameView(
identifierWithoutTableReference(from, fromTableReference),
NessieUtil.removeCatalogName(
identifierWithoutTableReference(to, toTableReference), name()));
}
}
Loading

0 comments on commit 89b3a21

Please sign in to comment.