Skip to content

Commit 614ec11

Browse files
pan3793Jack Ye
authored andcommitted
Spark: Fix create table in Hadoop catalog root namespace (#4024)
1 parent 5d599e1 commit 614ec11

File tree

6 files changed

+51
-0
lines changed

6 files changed

+51
-0
lines changed

spark/v3.0/spark/src/main/java/org/apache/iceberg/spark/SparkCatalog.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,10 @@ private Pair<Table, Long> load(Identifier ident) {
488488
return Pair.of(icebergCatalog.loadTable(buildIdentifier(ident)), null);
489489

490490
} catch (org.apache.iceberg.exceptions.NoSuchTableException e) {
491+
if (ident.namespace().length == 0) {
492+
throw e;
493+
}
494+
491495
// if the original load didn't work, the identifier may be extended and include a snapshot selector
492496
TableIdentifier namespaceAsIdent = buildIdentifier(namespaceToIdentifier(ident.namespace()));
493497
Table table;
@@ -567,6 +571,8 @@ private Pair<Table, Long> loadFromPathIdentifier(PathIdentifier ident) {
567571
}
568572

569573
private Identifier namespaceToIdentifier(String[] namespace) {
574+
Preconditions.checkArgument(namespace.length > 0,
575+
"Cannot convert empty namespace to identifier");
570576
String[] ns = Arrays.copyOf(namespace, namespace.length - 1);
571577
String name = namespace[ns.length];
572578
return Identifier.of(ns, name);

spark/v3.0/spark/src/test/java/org/apache/iceberg/spark/sql/TestCreateTable.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,17 @@ public void testCreateTable() {
6767
table.properties().get(TableProperties.DEFAULT_FILE_FORMAT));
6868
}
6969

70+
@Test
71+
public void testCreateTableInRootNamespace() {
72+
Assume.assumeTrue("Hadoop has no default namespace configured", "testhadoop".equals(catalogName));
73+
74+
try {
75+
sql("CREATE TABLE %s.table (id bigint) USING iceberg", catalogName);
76+
} finally {
77+
sql("DROP TABLE IF EXISTS %s.table", catalogName);
78+
}
79+
}
80+
7081
@Test
7182
public void testCreateTableUsingParquet() {
7283
Assume.assumeTrue(

spark/v3.1/spark/src/main/java/org/apache/iceberg/spark/SparkCatalog.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,10 @@ private Pair<Table, Long> load(Identifier ident) {
488488
return Pair.of(icebergCatalog.loadTable(buildIdentifier(ident)), null);
489489

490490
} catch (org.apache.iceberg.exceptions.NoSuchTableException e) {
491+
if (ident.namespace().length == 0) {
492+
throw e;
493+
}
494+
491495
// if the original load didn't work, the identifier may be extended and include a snapshot selector
492496
TableIdentifier namespaceAsIdent = buildIdentifier(namespaceToIdentifier(ident.namespace()));
493497
Table table;
@@ -567,6 +571,8 @@ private Pair<Table, Long> loadFromPathIdentifier(PathIdentifier ident) {
567571
}
568572

569573
private Identifier namespaceToIdentifier(String[] namespace) {
574+
Preconditions.checkArgument(namespace.length > 0,
575+
"Cannot convert empty namespace to identifier");
570576
String[] ns = Arrays.copyOf(namespace, namespace.length - 1);
571577
String name = namespace[ns.length];
572578
return Identifier.of(ns, name);

spark/v3.1/spark/src/test/java/org/apache/iceberg/spark/sql/TestCreateTable.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,17 @@ public void testCreateTable() {
6767
table.properties().get(TableProperties.DEFAULT_FILE_FORMAT));
6868
}
6969

70+
@Test
71+
public void testCreateTableInRootNamespace() {
72+
Assume.assumeTrue("Hadoop has no default namespace configured", "testhadoop".equals(catalogName));
73+
74+
try {
75+
sql("CREATE TABLE %s.table (id bigint) USING iceberg", catalogName);
76+
} finally {
77+
sql("DROP TABLE IF EXISTS %s.table", catalogName);
78+
}
79+
}
80+
7081
@Test
7182
public void testCreateTableUsingParquet() {
7283
Assume.assumeTrue(

spark/v3.2/spark/src/main/java/org/apache/iceberg/spark/SparkCatalog.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,10 @@ private Pair<Table, Long> load(Identifier ident) {
488488
return Pair.of(icebergCatalog.loadTable(buildIdentifier(ident)), null);
489489

490490
} catch (org.apache.iceberg.exceptions.NoSuchTableException e) {
491+
if (ident.namespace().length == 0) {
492+
throw e;
493+
}
494+
491495
// if the original load didn't work, the identifier may be extended and include a snapshot selector
492496
TableIdentifier namespaceAsIdent = buildIdentifier(namespaceToIdentifier(ident.namespace()));
493497
Table table;
@@ -567,6 +571,8 @@ private Pair<Table, Long> loadFromPathIdentifier(PathIdentifier ident) {
567571
}
568572

569573
private Identifier namespaceToIdentifier(String[] namespace) {
574+
Preconditions.checkArgument(namespace.length > 0,
575+
"Cannot convert empty namespace to identifier");
570576
String[] ns = Arrays.copyOf(namespace, namespace.length - 1);
571577
String name = namespace[ns.length];
572578
return Identifier.of(ns, name);

spark/v3.2/spark/src/test/java/org/apache/iceberg/spark/sql/TestCreateTable.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,17 @@ public void testCreateTable() {
6767
table.properties().get(TableProperties.DEFAULT_FILE_FORMAT));
6868
}
6969

70+
@Test
71+
public void testCreateTableInRootNamespace() {
72+
Assume.assumeTrue("Hadoop has no default namespace configured", "testhadoop".equals(catalogName));
73+
74+
try {
75+
sql("CREATE TABLE %s.table (id bigint) USING iceberg", catalogName);
76+
} finally {
77+
sql("DROP TABLE IF EXISTS %s.table", catalogName);
78+
}
79+
}
80+
7081
@Test
7182
public void testCreateTableUsingParquet() {
7283
Assume.assumeTrue(

0 commit comments

Comments
 (0)