Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.apache.iceberg.spark.extensions;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.atIndex;

import java.util.List;
Expand Down Expand Up @@ -82,4 +83,32 @@ public void testRegisterTable() throws NoSuchTableException, ParseException {
.as("Should have the right datafile count in the procedure result")
.contains(originalFileCount, atIndex(2));
}

@TestTemplate
public void testRegisterTableToNonexistentNamespace()
throws NoSuchTableException, ParseException {
String targetNameWithNonexistentNamespace =
(catalogName.equals("spark_catalog") ? "" : catalogName + ".")
+ "missing_namespace."
+ "register_table";
long numRows = 1000;

sql("CREATE TABLE %s (id int, data string) using ICEBERG", tableName);
spark
.range(0, numRows)
.withColumn("data", functions.col("id").cast(DataTypes.StringType))
.writeTo(tableName)
.append();

Table table = Spark3Util.loadIcebergTable(spark, tableName);
String metadataJson = TableUtil.metadataFileLocation(table);

assertThatThrownBy(
() ->
sql(
"CALL %s.system.register_table('%s', '%s')",
catalogName, targetNameWithNonexistentNamespace, metadataJson))
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("Cannot register table to nonexistent target namespace");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.apache.iceberg.SnapshotSummary;
import org.apache.iceberg.Table;
import org.apache.iceberg.catalog.Catalog;
import org.apache.iceberg.catalog.SupportsNamespaces;
import org.apache.iceberg.catalog.TableIdentifier;
import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
import org.apache.iceberg.spark.Spark3Util;
Expand Down Expand Up @@ -86,6 +87,12 @@ public InternalRow[] call(InternalRow args) {
"Cannot handle an empty argument metadata_file");

Catalog icebergCatalog = ((HasIcebergCatalog) tableCatalog()).icebergCatalog();
if (tableName.hasNamespace() && icebergCatalog instanceof SupportsNamespaces) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this check should be part of the procedure. Instead, such a check should be added in registerTable() in BaseMetastoreCatalog / RestSessionCatalog. Tests can then be added to CatalogTests for RestSessionCatalog. Tests for registerTable in BaseMetastoreCatalog are spread across different catalog implementations, so tests should be added there as well

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nastra Thank you for your feedback, I think it makes sense to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nastra I've addressed your feedback. Please take another look when you have time, thanks.

Preconditions.checkArgument(
((SupportsNamespaces) icebergCatalog).namespaceExists(tableName.namespace()),
"Cannot register table to nonexistent target namespace %s",
tableName.namespace());
}
Table table = icebergCatalog.registerTable(tableName, metadataFile);
Long currentSnapshotId = null;
Long totalDataFiles = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,12 @@ public enum SparkCatalogConfig {
"testhive",
SparkCatalog.class.getName(),
ImmutableMap.of(
"type", "hive",
"default-namespace", "default")),
"type",
"hive",
"default-namespace",
"default",
CatalogProperties.CACHE_ENABLED,
"false")),
HADOOP(
"testhadoop",
SparkCatalog.class.getName(),
Expand Down
Loading