Skip to content
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class CatalogManager(
defaultSessionCatalog: CatalogPlugin,
val v1SessionCatalog: SessionCatalog) extends Logging {
import CatalogManager.SESSION_CATALOG_NAME
import CatalogV2Util._

private val catalogs = mutable.HashMap.empty[String, CatalogPlugin]

Expand Down Expand Up @@ -106,13 +107,15 @@ class CatalogManager(
}

def setCurrentNamespace(namespace: Array[String]): Unit = synchronized {
if (currentCatalog.name() == SESSION_CATALOG_NAME) {
if (namespace.length != 1) {
currentCatalog match {
case _ if isSessionCatalog(currentCatalog) && namespace.length == 1 =>
v1SessionCatalog.setCurrentDatabase(namespace.head)
case _ if isSessionCatalog(currentCatalog) =>
throw new NoSuchNamespaceException(namespace)
}
v1SessionCatalog.setCurrentDatabase(namespace.head)
} else {
_currentNamespace = Some(namespace)
case catalog: SupportsNamespaces if !catalog.namespaceExists(namespace) =>
throw new NoSuchNamespaceException(namespace)
Comment thread
cloud-fan marked this conversation as resolved.
Outdated
case _ =>
_currentNamespace = Some(namespace)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@ package org.apache.spark.sql.connector.catalog

import java.net.URI

import scala.collection.JavaConverters._

import org.apache.spark.SparkFunSuite
import org.apache.spark.sql.catalyst.analysis.{EmptyFunctionRegistry, FakeV2SessionCatalog, NoSuchNamespaceException}
import org.apache.spark.sql.catalyst.catalog.{CatalogDatabase, InMemoryCatalog, SessionCatalog}
import org.apache.spark.sql.connector.InMemoryTableCatalog
import org.apache.spark.sql.internal.SQLConf
import org.apache.spark.sql.util.CaseInsensitiveStringMap

Expand Down Expand Up @@ -108,6 +111,19 @@ class CatalogManagerSuite extends SparkFunSuite {
assert(v1SessionCatalog.getCurrentDatabase == "default")
catalogManager.setCurrentNamespace(Array("test2"))
assert(v1SessionCatalog.getCurrentDatabase == "default")

// Check namespace existence if currentCatalog implements SupportsNamespaces.
conf.setConfString("spark.sql.catalog.testCatalog", classOf[InMemoryTableCatalog].getName)
catalogManager.setCurrentCatalog("testCatalog")
catalogManager.currentCatalog.asInstanceOf[InMemoryTableCatalog]
.createNamespace(Array("test3"), Map.empty[String, String].asJava)
assert(v1SessionCatalog.getCurrentDatabase == "default")
catalogManager.setCurrentNamespace(Array("test3"))
assert(v1SessionCatalog.getCurrentDatabase == "default")

intercept[NoSuchNamespaceException] {
catalogManager.setCurrentNamespace(Array("ns1", "ns2"))
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1394,6 +1394,7 @@ class DataSourceV2SQLSuite

test("Use: v2 catalog is used and namespace does not exist") {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

we should update this test with catalogs that don't implement SupportsNamespace

// Namespaces are not required to exist for v2 catalogs.
sql("create namespace testcat.ns1.ns2")
sql("USE testcat.ns1.ns2")
val catalogManager = spark.sessionState.catalogManager
assert(catalogManager.currentNamespace === Array("ns1", "ns2"))
Expand All @@ -1418,6 +1419,7 @@ class DataSourceV2SQLSuite

sql("USE testcat")
testShowCurrentNamespace("testcat", "")
sql("create namespace testcat.ns1.ns2")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

please upper case the SQL keywords

sql("USE testcat.ns1.ns2")
testShowCurrentNamespace("testcat", "ns1.ns2")
}
Expand Down Expand Up @@ -2257,6 +2259,7 @@ class DataSourceV2SQLSuite
spark.conf.unset(V2_SESSION_CATALOG_IMPLEMENTATION.key)
val sessionCatalogName = CatalogManager.SESSION_CATALOG_NAME

sql("create namespace testcat.ns1.ns2")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

ditto

sql("USE testcat.ns1.ns2")
sql("CREATE TABLE t USING foo AS SELECT 1 col")
checkAnswer(spark.table("t"), Row(1))
Expand Down