roczei commented on PR #37679:
URL: https://github.com/apache/spark/pull/37679#issuecomment-1250958346

   > Is this a common behavior in other databases?
   
   @cloud-fan Good question. The reason that we cannot delete the user 
specified default database because we have the following if statement in the 
actual code:
   
   ```
   if (dbName == defaultDatabase)
   ```
   
   and this is the latest state of master:
   
   
https://github.com/apache/spark/blob/master/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/catalog/SessionCatalog.scala#L286
   
   ```
     def dropDatabase(db: String, ignoreIfNotExists: Boolean, cascade: 
Boolean): Unit = {
       val dbName = format(db)
       if (dbName == DEFAULT_DATABASE) {
         throw QueryCompilationErrors.cannotDropDefaultDatabaseError
       }
   ```
   
   As you can see that I am just using the same logic.
   
   If you think that we should only deny the database drop for "default" and 
allow for the value of spark.sql.catalog.spark_catalog.defaultDatabase, it is 
ok for me. The change is very simple:
   
   ```
   +++ 
b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/catalog/SessionCatalog.scala
   @@ -284,7 +284,7 @@ class SessionCatalog(
    
      def dropDatabase(db: String, ignoreIfNotExists: Boolean, cascade: 
Boolean): Unit = {
        val dbName = format(db)
   -    if (dbName == defaultDatabase) {
   +    if (dbName == "default") {
          throw QueryCompilationErrors.cannotDropDefaultDatabaseError
        }
        if (!ignoreIfNotExists) {
   ```
   
   and here is the validation:
   
   ```
    $ ./spark-shell --conf spark.sql.catalogImplementation=hive --conf 
spark.sql.catalog.spark_catalog.defaultDatabase=xyz
   Setting default log level to "WARN".
   To adjust logging level use sc.setLogLevel(newLevel). For SparkR, use 
setLogLevel(newLevel).
   22/09/19 14:21:07 WARN NativeCodeLoader: Unable to load native-hadoop 
library for your platform... using builtin-java classes where applicable
   Spark context Web UI available at http://localhost:4040
   Spark context available as 'sc' (master = local[*], app id = 
local-1663590068068).
   Spark session available as 'spark'.
   Welcome to
         ____              __
        / __/__  ___ _____/ /__
       _\ \/ _ \/ _ `/ __/  '_/
      /___/ .__/\_,_/_/ /_/\_\   version 3.4.0-SNAPSHOT
         /_/
            
   Using Scala version 2.12.17 (OpenJDK 64-Bit Server VM, Java 1.8.0_345)
   Type in expressions to have them evaluated.
   Type :help for more information.
   
   scala> spark.sql("show databases").show()
   +---------+
   |namespace|
   +---------+
   |      abc|
   |  default|
   |      xyz|
   +---------+
   
   
   scala> spark.sql("use database abc")
   res1: org.apache.spark.sql.DataFrame = []
   
   scala> spark.sql("SELECT current_database() AS db").show()
   +---+
   | db|
   +---+
   |abc|
   +---+
   
   
   scala> 
   
   scala> spark.sql("drop database xyz")
   res3: org.apache.spark.sql.DataFrame = []
   
   scala> spark.sql("show databases").show()
   +---------+
   |namespace|
   +---------+
   |      abc|
   |  default|
   +---------+
   
   
   scala> 
   ```
   Is this solution acceptable for you?
   


-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


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

Reply via email to