Skip to content

Commit

Permalink
add renameTableTargetAlreadyExistsAsView() test
Browse files Browse the repository at this point in the history
  • Loading branch information
nastra committed Oct 18, 2023
1 parent fd04d41 commit b518ab2
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,10 @@ public void renameTable(TableIdentifier from, TableIdentifier to) {
throw new AlreadyExistsException("Cannot rename %s to %s. Table already exists", from, to);
}

if (views.containsKey(to)) {
throw new AlreadyExistsException("Cannot rename %s to %s. View already exists", from, to);
}

tables.put(to, fromLocation);
tables.remove(from);
}
Expand Down
35 changes: 35 additions & 0 deletions core/src/test/java/org/apache/iceberg/view/ViewCatalogTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,41 @@ public void renameViewTargetAlreadyExistsAsTable() {
.hasMessageContaining("Cannot rename ns.view to ns.table. Table already exists");
}

@Test
public void renameTableTargetAlreadyExistsAsView() {
Assumptions.assumeThat(tableCatalog())
.as("Only valid for catalogs that support tables")
.isNotNull();

TableIdentifier viewIdentifier = TableIdentifier.of("ns", "view");
TableIdentifier tableIdentifier = TableIdentifier.of("ns", "table");

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

assertThat(tableCatalog().tableExists(tableIdentifier)).as("Table should not exist").isFalse();

tableCatalog().buildTable(tableIdentifier, SCHEMA).create();

assertThat(tableCatalog().tableExists(tableIdentifier)).as("Table should exist").isTrue();

assertThat(catalog().viewExists(viewIdentifier)).as("View should not exist").isFalse();

catalog()
.buildView(viewIdentifier)
.withSchema(SCHEMA)
.withDefaultNamespace(viewIdentifier.namespace())
.withQuery("spark", "select * from ns.tbl")
.create();

assertThat(catalog().viewExists(viewIdentifier)).as("View should exist").isTrue();

assertThatThrownBy(() -> tableCatalog().renameTable(tableIdentifier, viewIdentifier))
.isInstanceOf(AlreadyExistsException.class)
.hasMessageContaining("Cannot rename ns.table to ns.view. View already exists");
}

@Test
public void listViews() {
Namespace ns1 = Namespace.of("ns1");
Expand Down

0 comments on commit b518ab2

Please sign in to comment.