You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iceberg.apache.org by et...@apache.org on 2024/03/08 08:29:04 UTC

(iceberg) branch main updated: Core: Add test for renaming table to a non-existing namespace (#9895)

This is an automated email from the ASF dual-hosted git repository.

etudenhoefner pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/iceberg.git


The following commit(s) were added to refs/heads/main by this push:
     new afb200041b Core: Add test for renaming table to a non-existing namespace (#9895)
afb200041b is described below

commit afb200041b17a3f8cce792848ca3abc2bab9bd75
Author: Eduard Tudenhoefner <et...@gmail.com>
AuthorDate: Fri Mar 8 09:28:59 2024 +0100

    Core: Add test for renaming table to a non-existing namespace (#9895)
---
 .../org/apache/iceberg/catalog/CatalogTests.java     | 20 ++++++++++++++++++++
 .../java/org/apache/iceberg/hive/HiveCatalog.java    |  4 ++++
 2 files changed, 24 insertions(+)

diff --git a/core/src/test/java/org/apache/iceberg/catalog/CatalogTests.java b/core/src/test/java/org/apache/iceberg/catalog/CatalogTests.java
index fef4600159..d6b9f6d120 100644
--- a/core/src/test/java/org/apache/iceberg/catalog/CatalogTests.java
+++ b/core/src/test/java/org/apache/iceberg/catalog/CatalogTests.java
@@ -783,6 +783,26 @@ public abstract class CatalogTests<C extends Catalog & SupportsNamespaces> {
         .isFalse();
   }
 
+  @Test
+  public void renameTableNamespaceMissing() {
+    TableIdentifier from = TableIdentifier.of("ns", "table");
+    TableIdentifier to = TableIdentifier.of("non_existing", "renamedTable");
+
+    if (requiresNamespaceCreate()) {
+      catalog().createNamespace(from.namespace());
+    }
+
+    Assertions.assertThat(catalog().tableExists(from)).as("Table should not exist").isFalse();
+
+    catalog().buildTable(from, SCHEMA).create();
+
+    Assertions.assertThat(catalog().tableExists(from)).as("Table should exist").isTrue();
+
+    Assertions.assertThatThrownBy(() -> catalog().renameTable(from, to))
+        .isInstanceOf(NoSuchNamespaceException.class)
+        .hasMessageContaining("Namespace does not exist: non_existing");
+  }
+
   @Test
   public void testRenameTableDestinationTableAlreadyExists() {
     C catalog = catalog();
diff --git a/hive-metastore/src/main/java/org/apache/iceberg/hive/HiveCatalog.java b/hive-metastore/src/main/java/org/apache/iceberg/hive/HiveCatalog.java
index 63e4aad5d8..34f9e1da43 100644
--- a/hive-metastore/src/main/java/org/apache/iceberg/hive/HiveCatalog.java
+++ b/hive-metastore/src/main/java/org/apache/iceberg/hive/HiveCatalog.java
@@ -228,6 +228,10 @@ public class HiveCatalog extends BaseMetastoreCatalog implements SupportsNamespa
 
     TableIdentifier to = removeCatalogName(originalTo);
     Preconditions.checkArgument(isValidIdentifier(to), "Invalid identifier: %s", to);
+    if (!namespaceExists(to.namespace())) {
+      throw new NoSuchNamespaceException(
+          "Cannot rename %s to %s. Namespace does not exist: %s", from, to, to.namespace());
+    }
 
     String toDatabase = to.namespace().level(0);
     String fromDatabase = from.namespace().level(0);