imback82 commented on a change in pull request #25903: [SPARK-29215][SQL] 
current namespace should be tracked in SessionCatalog if the current catalog is 
session catalog
URL: https://github.com/apache/spark/pull/25903#discussion_r327713725
 
 

 ##########
 File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/connector/catalog/CatalogManager.scala
 ##########
 @@ -85,33 +91,53 @@ class CatalogManager(conf: SQLConf, defaultSessionCatalog: 
TableCatalog) extends
 
   def currentNamespace: Array[String] = synchronized {
     _currentNamespace.getOrElse {
-      currentCatalog.map { catalogName =>
-        getDefaultNamespace(catalog(catalogName))
-      }.getOrElse(Array("default")) // The builtin catalog use "default" as 
the default database.
+      // For session catalog, the current namespace is kept in 
`SessionCatalog`. There are many
+      // commands that do not support v2 catalog API. They ignore the current 
catalog and blindly
+      // go to `SessionCatalog`. This means, we must keep track of the current 
namespace of session
+      // catalog even if the current catalog is not session catalog. 
`CatalogManager` only tracks
+      // the current namespace of the current catalog.
+      if (currentCatalog.name() == SESSION_CATALOG_NAME) {
+        Array(v1SessionCatalog.getCurrentDatabase)
+      } else {
+        getDefaultNamespace(currentCatalog)
+      }
     }
   }
 
   def setCurrentNamespace(namespace: Array[String]): Unit = synchronized {
-    _currentNamespace = Some(namespace)
+    // For session catalog, the current namespace is kept in `SessionCatalog`. 
There are many
+    // commands that do not support v2 catalog API. They ignore the current 
catalog and blindly
+    // go to `SessionCatalog`. This means, we must keep track of the current 
namespace of session
+    // catalog even if the current catalog is not session catalog. 
`CatalogManager` only tracks
+    // the current namespace of the current catalog.
+    if (currentCatalog.name() == SESSION_CATALOG_NAME) {
+      if (namespace.length != 1) {
+        throw new NoSuchNamespaceException(namespace)
+      }
+      v1SessionCatalog.setCurrentDatabase(namespace.head)
+    } else {
+      _currentNamespace = Some(namespace)
+    }
   }
 
-  private var _currentCatalog: Option[String] = None
+  private var _currentCatalogName: Option[String] = None
 
-  // Returns the name of current catalog. None means the current catalog is 
the builtin catalog.
-  def currentCatalog: Option[String] = synchronized {
-    _currentCatalog.orElse(conf.defaultV2Catalog)
+  def currentCatalog: CatalogPlugin = synchronized {
+    _currentCatalogName.map(catalogName => catalog(catalogName))
+      .orElse(defaultCatalog)
+      .getOrElse(v2SessionCatalog)
   }
 
   def setCurrentCatalog(catalogName: String): Unit = synchronized {
-    _currentCatalog = Some(catalogName)
+    _currentCatalogName = Some(catalogName)
     _currentNamespace = None
 
 Review comment:
   +1 for Option 1. I think we can make the behavior consistent when we switch 
catalogs (reset to default). But if Option 1 is a lot of work, we can go with 
Option 2 (`default.t`) until we create rules for all commands that don't 
support v2 APIs (I can help with this).

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to