rdblue commented on code in PR #4448:
URL: https://github.com/apache/iceberg/pull/4448#discussion_r863914937


##########
core/src/test/java/org/apache/iceberg/catalog/CatalogTests.java:
##########
@@ -562,6 +563,87 @@ public void testLoadMissingTable() {
         () -> catalog.loadTable(ident));
   }
 
+  @Test
+  public void testRenameTable() {
+    C catalog = catalog();
+
+    if (requiresNamespaceCreate()) {
+      catalog.createNamespace(NS);
+    }
+
+    Assert.assertFalse("Source table should not exist before create", 
catalog.tableExists(TABLE));
+
+    catalog.buildTable(TABLE, SCHEMA).create();
+    Assert.assertTrue("Table should exist after create", 
catalog.tableExists(TABLE));
+
+    Assert.assertFalse("Destination table should not exist before rename", 
catalog.tableExists(RENAMED_TABLE));
+
+    catalog.renameTable(TABLE, RENAMED_TABLE);
+    Assert.assertTrue("Table should exist with new name", 
catalog.tableExists(RENAMED_TABLE));
+    Assert.assertFalse("Original table should no longer exist", 
catalog.tableExists(TABLE));
+
+    catalog.dropTable(RENAMED_TABLE);
+    assertEmpty("Should not contain table after drop", catalog, NS);
+  }
+
+  @Test
+  public void testRenameTableMissingSourceTable() {
+    C catalog = catalog();
+
+    if (requiresNamespaceCreate()) {
+      catalog.createNamespace(NS);
+    }
+
+    Assert.assertFalse("Source table should not exist before create", 
catalog.tableExists(TABLE));
+    Assert.assertFalse("Destination table should not exist before rename", 
catalog.tableExists(RENAMED_TABLE));
+
+    AssertHelpers.assertThrows("Should reject renaming a table that does not 
exist",
+        NoSuchTableException.class,
+        "Table does not exist",
+        () -> catalog.renameTable(TABLE, RENAMED_TABLE));
+
+    Assert.assertFalse("Destination table should not exist after failed 
rename", catalog.tableExists(RENAMED_TABLE));
+  }
+
+  // WIP - This needs to throw AlreadyExistsException but its giving 
UncheckedSQLException
+  @Test
+  public void testRenameTableDestinationTableAlreadyExists() {
+    C catalog = catalog();
+
+    if (requiresNamespaceCreate()) {
+      catalog.createNamespace(NS);
+    }
+
+    Assert.assertFalse("Source table should not exist before create", 
catalog.tableExists(TABLE));
+    catalog.buildTable(TABLE, SCHEMA).create();
+    Assert.assertTrue("Source table should exist after create", 
catalog.tableExists(TABLE));
+
+    Assert.assertFalse("Destination table should not exist before create", 
catalog.tableExists(RENAMED_TABLE));
+    catalog.buildTable(RENAMED_TABLE, SCHEMA).create();
+    Assert.assertTrue("Destination table should exist after create", 
catalog.tableExists(TABLE));
+
+    /**
+     * TODO - This throws UncheckedSqlException, which is what the JdbcCatalog 
also throws according to its tests.
+     *
+     *   org.apache.iceberg.exceptions.ServiceFailureException: Server error: 
UncheckedSQLException:
+     *   Failed to execute: UPDATE iceberg_tables SET table_namespace = ? , 
table_name = ?
+     *   WHERE catalog_name = ? AND table_namespace = ? AND table_name = ?
+     *
+     *   Updating JDBC catalog seems to have fixed it, but it's pretty hacky 
and probably pretty SQLite specific.
+     */
+    AssertHelpers.assertThrows("Should reject renaming a table if the new name 
already exists",
+        AlreadyExistsException.class,
+        "Table already exists",
+        () -> catalog.renameTable(TABLE, RENAMED_TABLE));
+
+    Assert.assertTrue("Source table should still exist after failed rename", 
catalog.tableExists(TABLE));
+    Assert.assertTrue("Destination table should still exist after failed 
rename", catalog.tableExists(RENAMED_TABLE));

Review Comment:
   It would be nice to have a validation that the destination table is distinct 
from the source table. Maybe checking UUID or making it a partitioned table and 
ensuring it is still partitioned.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to