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_r327427897
 
 

 ##########
 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:
   I think the current namespace should be either 1) reset to default or 2) 
maintained per catalog, but not carried over when a catalog is switched.
   
   For example,
   1) reset to default:
   ```SQL 
   # Starts with session catalog
   SELECT current_database() # default
   USE db
   SELECT current_database() # db
   
   # switch catalog
   USE CATALOG myCat
   SELECT current_database() # [] (default)
   USE ns1.ns2
   SELECT current_database() # ns1.ns2
   
   # switch back to different catalogs. namespace is reset.
   USE CATALOG session
   SELECT current_database() # default
   USE CATALOG myCat
   SELECT current_database() # [] (default)
   ```
   
   2) previous namespace is maintained per catalog:
   ```SQL 
   # Starts with session catalog
   SELECT current_database() # default
   USE db
   SELECT current_database() # db
   
   # switch catalog
   USE CATALOG myCat
   SELECT current_database() # [] (default)
   USE ns1.ns2
   SELECT current_database() # ns1.ns2
   
   # switch back to different catalogs. namespace is preserved.
   USE CATALOG session
   SELECT current_database() # db
   USE CATALOG myCat
   SELECT current_database() # ns1.ns2
   ```
   

----------------------------------------------------------------
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:
us...@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to