Skip to content

Commit

Permalink
Restore createNamespace call and behavior.
Browse files Browse the repository at this point in the history
  • Loading branch information
rdblue committed Aug 3, 2019
1 parent 4bd3dd1 commit 14de5b6
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ default boolean namespaceExists(String[] namespace) {
* @throws NamespaceAlreadyExistsException If the namespace already exists
* @throws UnsupportedOperationException If create is not a supported operation
*/
void createNamespaceMetadata(
void createNamespace(
String[] namespace,
Map<String, String> metadata) throws NamespaceAlreadyExistsException;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,7 @@ class TableCatalogSuite extends SparkFunSuite {

test("listNamespaces: list namespaces from metadata") {
val catalog = newCatalog()
catalog.createNamespaceMetadata(Array("ns1"), Map("property" -> "value").asJava)
catalog.createNamespace(Array("ns1"), Map("property" -> "value").asJava)

assert(catalog.listNamespaces === Array(Array("ns1")))
assert(catalog.listNamespaces(Array()) === Array(Array("ns1")))
Expand All @@ -684,7 +684,7 @@ class TableCatalogSuite extends SparkFunSuite {
val ident1 = Identifier.of(Array("ns1", "ns2"), "test_table_1")
val ident2 = Identifier.of(Array("ns1", "ns2"), "test_table_2")

catalog.createNamespaceMetadata(Array("ns1"), Map("property" -> "value").asJava)
catalog.createNamespace(Array("ns1"), Map("property" -> "value").asJava)
catalog.createTable(ident1, schema, Array.empty, emptyProps)
catalog.createTable(ident2, schema, Array.empty, emptyProps)

Expand Down Expand Up @@ -717,7 +717,7 @@ class TableCatalogSuite extends SparkFunSuite {
test("loadNamespaceMetadata: metadata exists, no tables") {
val catalog = newCatalog()

catalog.createNamespaceMetadata(testNs, Map("property" -> "value").asJava)
catalog.createNamespace(testNs, Map("property" -> "value").asJava)

val metadata = catalog.loadNamespaceMetadata(testNs)

Expand All @@ -727,49 +727,52 @@ class TableCatalogSuite extends SparkFunSuite {
test("loadNamespaceMetadata: metadata and table exist") {
val catalog = newCatalog()

catalog.createNamespace(testNs, Map("property" -> "value").asJava)
catalog.createTable(testIdent, schema, Array.empty, emptyProps)
catalog.createNamespaceMetadata(testNs, Map("property" -> "value").asJava)

val metadata = catalog.loadNamespaceMetadata(testNs)

assert(metadata.asScala === Map("property" -> "value"))
}

test("createNamespaceMetadata: basic behavior") {
test("createNamespace: basic behavior") {
val catalog = newCatalog()

catalog.createNamespaceMetadata(testNs, Map("property" -> "value").asJava)
catalog.createNamespace(testNs, Map("property" -> "value").asJava)

assert(catalog.namespaceExists(testNs) === true)
assert(catalog.loadNamespaceMetadata(testNs).asScala === Map("property" -> "value"))
}

test("createNamespaceMetadata: fail if metadata already exists") {
test("createNamespace: fail if metadata already exists") {
val catalog = newCatalog()

catalog.createNamespaceMetadata(testNs, Map("property" -> "value").asJava)
catalog.createNamespace(testNs, Map("property" -> "value").asJava)

val exc = intercept[NamespaceAlreadyExistsException] {
catalog.createNamespaceMetadata(testNs, Map("property" -> "value").asJava)
catalog.createNamespace(testNs, Map("property" -> "value").asJava)
}

assert(exc.getMessage.contains(testNs.quoted))
assert(catalog.namespaceExists(testNs) === true)
assert(catalog.loadNamespaceMetadata(testNs).asScala === Map("property" -> "value"))
}

test("createNamespaceMetadata: table exists") {
test("createNamespace: fail if namespace already exists from table") {
val catalog = newCatalog()

catalog.createTable(testIdent, schema, Array.empty, emptyProps)

assert(catalog.namespaceExists(testNs) === true)
assert(catalog.loadNamespaceMetadata(testNs).asScala === Map.empty)

catalog.createNamespaceMetadata(testNs, Map("property" -> "value").asJava)
val exc = intercept[NamespaceAlreadyExistsException] {
catalog.createNamespace(testNs, Map("property" -> "value").asJava)
}

assert(exc.getMessage.contains(testNs.quoted))
assert(catalog.namespaceExists(testNs) === true)
assert(catalog.loadNamespaceMetadata(testNs).asScala === Map("property" -> "value"))
assert(catalog.loadNamespaceMetadata(testNs).asScala === Map.empty)
}

test("dropNamespace: drop missing namespace") {
Expand All @@ -785,7 +788,7 @@ class TableCatalogSuite extends SparkFunSuite {
test("dropNamespace: drop empty namespace") {
val catalog = newCatalog()

catalog.createNamespaceMetadata(testNs, Map("property" -> "value").asJava)
catalog.createNamespace(testNs, Map("property" -> "value").asJava)

assert(catalog.namespaceExists(testNs) === true)
assert(catalog.loadNamespaceMetadata(testNs).asScala === Map("property" -> "value"))
Expand All @@ -799,8 +802,8 @@ class TableCatalogSuite extends SparkFunSuite {
test("dropNamespace: fail if not empty") {
val catalog = newCatalog()

catalog.createNamespace(testNs, Map("property" -> "value").asJava)
catalog.createTable(testIdent, schema, Array.empty, emptyProps)
catalog.createNamespaceMetadata(testNs, Map("property" -> "value").asJava)

val exc = intercept[IllegalStateException] {
catalog.dropNamespace(testNs)
Expand All @@ -814,7 +817,7 @@ class TableCatalogSuite extends SparkFunSuite {
test("alterNamespace: basic behavior") {
val catalog = newCatalog()

catalog.createNamespaceMetadata(testNs, Map("property" -> "value").asJava)
catalog.createNamespace(testNs, Map("property" -> "value").asJava)

catalog.alterNamespace(testNs, NamespaceChange.setProperty("property2", "value2"))
assert(catalog.loadNamespaceMetadata(testNs).asScala === Map(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,7 @@ class TestTableCatalog extends TableCatalog with SupportsNamespaces {
allNamespaces.map(_.head).distinct.map(Array(_)).toArray
}

override def listNamespaces(
namespace: Array[String]): Array[Array[String]] = {
override def listNamespaces(namespace: Array[String]): Array[Array[String]] = {
allNamespaces
.filter(_.size > namespace.length)
.filter(_.startsWith(namespace))
Expand All @@ -127,14 +126,18 @@ class TestTableCatalog extends TableCatalog with SupportsNamespaces {
}
}

override def createNamespaceMetadata(
override def createNamespace(
namespace: Array[String],
metadata: util.Map[String, String]): Unit = {
if (namespaceExists(namespace)) {
throw new NamespaceAlreadyExistsException(namespace)
}

Option(namespaces.putIfAbsent(namespace.toList, metadata.asScala.toMap)) match {
case Some(_) =>
throw new NamespaceAlreadyExistsException(namespace)
case _ =>
// created successfully
// created successfully
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ class V2SessionCatalog(sessionState: SessionState) extends TableCatalog with Sup
}
}

override def createNamespaceMetadata(
override def createNamespace(
namespace: Array[String],
metadata: util.Map[String, String]): Unit = namespace match {
case Array(db) if !catalog.databaseExists(db) =>
Expand Down

0 comments on commit 14de5b6

Please sign in to comment.