amaliujia commented on code in PR #36641:
URL: https://github.com/apache/spark/pull/36641#discussion_r897502115


##########
sql/core/src/main/scala/org/apache/spark/sql/internal/CatalogImpl.scala:
##########
@@ -287,16 +298,37 @@ class CatalogImpl(sparkSession: SparkSession) extends 
Catalog {
    * Checks if the database with the specified name exists.
    */
   override def databaseExists(dbName: String): Boolean = {
-    sessionCatalog.databaseExists(dbName)
+    // To maintain backwards compatibility, we first treat the input is a 
simple dbName and check
+    // if sessionCatalog contains it. If no, we try to parse it as 3 part 
name. If the parased
+    // identifier contains both catalog name and database name, we then search 
the database in the
+    // catalog.
+    if (!sessionCatalog.databaseExists(dbName)) {
+      val ident = 
sparkSession.sessionState.sqlParser.parseMultipartIdentifier(dbName)
+      val plan = 
sparkSession.sessionState.executePlan(UnresolvedNamespace(ident)).analyzed
+      plan match {
+        case ResolvedNamespace(catalog: InMemoryCatalog, _) => 
catalog.databaseExists(ident(1))
+        case _ => false
+      }

Review Comment:
   @cloud-fan 
   
   It seems that the `UnresolvedNamespace` will just be converted to 
`ResolvedNamespace` without a further validation: 
https://github.com/apache/spark/blob/7cfc40fff6a2bb4025b29d8dd9eb66734030a901/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/Analyzer.scala#L890
   
   So we cannot rely on whether the plan is resolved to determine if the DB 
exists. In current impl I have to access the catalog and verify it by myself. 
   
   However, `ResolvedNamespace`'s catalog's type is `CatalogPlugin`. I am not 
sure how to write general code to deal with it. Any ideas?



-- 
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.

To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


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

Reply via email to