Skip to content

Commit

Permalink
[core] Support rename view (apache#4429)
Browse files Browse the repository at this point in the history
  • Loading branch information
herefree authored Nov 1, 2024
1 parent 38fc717 commit f9b61d4
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 0 deletions.
13 changes: 13 additions & 0 deletions paimon-core/src/main/java/org/apache/paimon/catalog/Catalog.java
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,19 @@ default List<String> listViews(String databaseName) throws DatabaseNotExistExcep
return Collections.emptyList();
}

/**
* Rename a view.
*
* @param fromView identifier of the view to rename
* @param toView new view identifier
* @throws ViewNotExistException if the fromView does not exist
* @throws ViewAlreadyExistException if the toView already exists
*/
default void renameView(Identifier fromView, Identifier toView, boolean ignoreIfNotExists)
throws ViewNotExistException, ViewAlreadyExistException {
throw new UnsupportedOperationException();
}

/** Return a boolean that indicates whether this catalog allow upper case. */
boolean allowUpperCase();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,12 @@ public List<String> listViews(String databaseName) throws DatabaseNotExistExcept
return wrapped.listViews(databaseName);
}

@Override
public void renameView(Identifier fromView, Identifier toView, boolean ignoreIfNotExists)
throws ViewNotExistException, ViewAlreadyExistException {
wrapped.renameView(fromView, toView, ignoreIfNotExists);
}

@Override
public Path getTableLocation(Identifier identifier) {
return wrapped.getTableLocation(identifier);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1083,6 +1083,15 @@ public final void renameTable(
ObjectPath tablePath, String newTableName, boolean ignoreIfNotExists)
throws CatalogException, TableNotExistException, TableAlreadyExistException {
ObjectPath toTable = new ObjectPath(tablePath.getDatabaseName(), newTableName);
if (catalog.viewExists(toIdentifier(tablePath))) {
try {
catalog.renameView(
toIdentifier(tablePath), toIdentifier(toTable), ignoreIfNotExists);
return;
} catch (Catalog.ViewNotExistException | Catalog.ViewAlreadyExistException e) {
throw new RuntimeException("Unexpected exception.", e);
}
}
try {
catalog.renameTable(toIdentifier(tablePath), toIdentifier(toTable), ignoreIfNotExists);
} catch (Catalog.TableNotExistException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,34 @@ public List<String> listViews(String databaseName) throws DatabaseNotExistExcept
}
}

@Override
public void renameView(Identifier fromView, Identifier toView, boolean ignoreIfNotExists)
throws ViewNotExistException, ViewAlreadyExistException {
if (!viewExists(fromView)) {
if (ignoreIfNotExists) {
return;
}
throw new ViewNotExistException(fromView);
}
if (viewExists(toView)) {
throw new ViewAlreadyExistException(toView);
}
try {
String fromDB = fromView.getDatabaseName();
String fromViewName = fromView.getTableName();
Table table = clients.run(client -> client.getTable(fromDB, fromViewName));
table.setDbName(toView.getDatabaseName());
table.setTableName(toView.getTableName());
clients.execute(client -> client.alter_table(fromDB, fromViewName, table));
} catch (TException e) {
throw new RuntimeException("Failed to rename view " + fromView.getFullName(), e);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException(
"Interrupted in call to rename view " + fromView.getFullName(), e);
}
}

@Override
public org.apache.paimon.table.Table getDataOrFormatTable(Identifier identifier)
throws TableNotExistException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1938,6 +1938,22 @@ public void testView() throws Exception {
collect("DROP VIEW hive_v");
}

@Test
public void renameView() throws Exception {
tEnv.executeSql("CREATE TABLE t ( a INT, b STRING ) WITH ( 'file.format' = 'avro' )")
.await();
tEnv.executeSql("INSERT INTO t VALUES (1, 'Hi'), (2, 'Hello')").await();

tEnv.executeSql("CREATE VIEW flink_v AS SELECT a + 1, b FROM t").await();
tEnv.executeSql("ALTER VIEW flink_v rename to flink_v_rename").await();
assertThat(collect("SHOW VIEWS")).containsExactlyInAnyOrder(Row.of("flink_v_rename"));

hiveShell.executeQuery("CREATE VIEW hive_v AS SELECT a + 1, b FROM t");
tEnv.executeSql("ALTER VIEW hive_v rename to hive_v_rename").await();
assertThat(collect("SHOW VIEWS"))
.containsExactlyInAnyOrder(Row.of("flink_v_rename"), Row.of("hive_v_rename"));
}

/** Prepare to update a paimon table with a custom path in the paimon file system. */
private void alterTableInFileSystem(TableEnvironment tEnv) throws Exception {
tEnv.executeSql(
Expand Down

0 comments on commit f9b61d4

Please sign in to comment.