Skip to content

[SPARK-16459][SQL] Prevent dropping current database #14115

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed

[SPARK-16459][SQL] Prevent dropping current database #14115

wants to merge 6 commits into from

Conversation

dongjoon-hyun
Copy link
Member

@dongjoon-hyun dongjoon-hyun commented Jul 9, 2016

What changes were proposed in this pull request?

This PR prevents dropping the current database to avoid errors like the followings.

scala> sql("create database delete_db")
scala> sql("use delete_db")
scala> sql("drop database delete_db")
scala> sql("create table t as select 1")
org.apache.spark.sql.catalyst.analysis.NoSuchDatabaseException: Database `delete_db` not found;

How was this patch tested?

Pass the Jenkins tests including an updated testcase.

@SparkQA
Copy link

SparkQA commented Jul 9, 2016

Test build #62022 has finished for PR 14115 at commit 3029821.

  • This patch passes all tests.
  • This patch merges cleanly.
  • This patch adds no public classes.

@rxin
Copy link
Contributor

rxin commented Jul 9, 2016

I think it's scary to do this. It'd be better to forbid dropping the current database. This is Postgres' behavior:

testdb1=# drop database testdb1;
ERROR:  cannot drop the currently open database

@dongjoon-hyun
Copy link
Member Author

Sure. No problem!

@dongjoon-hyun
Copy link
Member Author

I'll fix this one first. This one is easier. :)

@dongjoon-hyun dongjoon-hyun changed the title [SPARK-16459][SQL] Curren database should become default when current database is dropped [SPARK-16459][SQL] Prevent dropping current database Jul 9, 2016
if (dbName == "default") {
throw new AnalysisException(s"Can not drop default database")
if (dbName == DEFAULT_DATABASE || dbName == getCurrentDatabase) {
throw new AnalysisException(s"Can not drop `${DEFAULT_DATABASE}` or current database")
Copy link
Contributor

Choose a reason for hiding this comment

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

hm i think we should break this into two messages rather than a single one.

Copy link
Member Author

Choose a reason for hiding this comment

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

I see. No problem.

@dongjoon-hyun
Copy link
Member Author

Now, it preserves the previous behavior for dropping default db, and throws new exception message for new cases only.

throw new AnalysisException(s"Can not drop default database")
} else if (dbName == getCurrentDatabase) {
Copy link
Contributor

Choose a reason for hiding this comment

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

do we need to check case sensitivity?

Copy link
Member Author

Choose a reason for hiding this comment

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

Oops. formatDatabaseName returns the raw string for case sensitive case.
Hm. That is the same situation with "default" database, isn't?

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll add two testcases for both and fix this.

Copy link
Member Author

@dongjoon-hyun dongjoon-hyun Jul 9, 2016

Choose a reason for hiding this comment

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

Oh, I was confused. For case sensitive case, we don't need to handle that.
Dropping with different names will fails due to lookup failure.

@SparkQA
Copy link

SparkQA commented Jul 9, 2016

Test build #62032 has finished for PR 14115 at commit ac5f3ea.

  • This patch fails Spark unit tests.
  • This patch merges cleanly.
  • This patch adds no public classes.

@SparkQA
Copy link

SparkQA commented Jul 9, 2016

Test build #62034 has finished for PR 14115 at commit 8aa1c2d.

  • This patch fails Spark unit tests.
  • This patch merges cleanly.
  • This patch adds no public classes.

@SparkQA
Copy link

SparkQA commented Jul 10, 2016

Test build #62037 has finished for PR 14115 at commit 805b2f7.

  • This patch passes all tests.
  • This patch merges cleanly.
  • This patch adds no public classes.

@@ -49,6 +49,8 @@ class SessionCatalog(
hadoopConf: Configuration) extends Logging {
import CatalogTypes.TablePartitionSpec

val DEFAULT_DATABASE = "default"
Copy link
Member

Choose a reason for hiding this comment

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

Nit: this should be in an object SessionCatalog?

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh, you're right. object is the best to keep this one. Thank you for review, @srowen .

Copy link
Member Author

@dongjoon-hyun dongjoon-hyun Jul 10, 2016

Choose a reason for hiding this comment

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

Oh, I remember why I didn't do that. In fact, we don't have object SessionCatalog, yet.
So, I was not sure I could introduce new object for just one constant variable. Is it enough valuable, @srowen and @rxin ?

Copy link
Member Author

Choose a reason for hiding this comment

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

Anyway, I'll make that first.
It's easy to revert one commit.
And, if possible, I prefer to have object SessionCatalog for the future

@dongjoon-hyun
Copy link
Member Author

Thank you, @srowen . It's done!

@SparkQA
Copy link

SparkQA commented Jul 10, 2016

Test build #62062 has finished for PR 14115 at commit 1702c7e.

  • This patch passes all tests.
  • This patch merges cleanly.
  • This patch adds no public classes.

@dongjoon-hyun
Copy link
Member Author

Hi, @rxin .
Could you review this again?

@dongjoon-hyun
Copy link
Member Author

After merging this, I'll add the same prevention for information_schema, too.

@rxin
Copy link
Contributor

rxin commented Jul 11, 2016

This looks alright.

cc @hvanhovell can you review and merge?

@dongjoon-hyun
Copy link
Member Author

Hi, @hvanhovell .
Could you review this PR, too?

@@ -878,7 +885,8 @@ class SessionCatalog(
* This is mainly used for tests.
*/
private[sql] def reset(): Unit = synchronized {
val default = "default"
val default = DEFAULT_DATABASE
Copy link
Contributor

@hvanhovell hvanhovell Jul 11, 2016

Choose a reason for hiding this comment

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

NIT: Shouldn't we just replace the val default by DEFAULT_DATABASE?

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh, sure!

@dongjoon-hyun
Copy link
Member Author

Thank you for review, @hvanhovell .
I replaced the local variable.

@hvanhovell
Copy link
Contributor

LGTM - pending jenkins.

@SparkQA
Copy link

SparkQA commented Jul 11, 2016

Test build #3177 has finished for PR 14115 at commit 19a3160.

  • This patch fails PySpark unit tests.
  • This patch merges cleanly.
  • This patch adds no public classes.

@SparkQA
Copy link

SparkQA commented Jul 11, 2016

Test build #62086 has finished for PR 14115 at commit 19a3160.

  • This patch passes all tests.
  • This patch merges cleanly.
  • This patch adds no public classes.

@asfgit asfgit closed this in 7ac79da Jul 11, 2016
@dongjoon-hyun
Copy link
Member Author

Thank you for review and merging, @rxin and @hvanhovell .

@rxin
Copy link
Contributor

rxin commented Jul 11, 2016

@hvanhovell please leave a message on the pr next time when you merge one -- and also indicate the branch this was merged in (master in this case).

@rxin
Copy link
Contributor

rxin commented Jul 11, 2016

Should this be in 2.0?

@dongjoon-hyun
Copy link
Member Author

dongjoon-hyun commented Jul 11, 2016

If possible, I hope. :)

asfgit pushed a commit that referenced this pull request Jul 11, 2016
This PR prevents dropping the current database to avoid errors like the followings.

```scala
scala> sql("create database delete_db")
scala> sql("use delete_db")
scala> sql("drop database delete_db")
scala> sql("create table t as select 1")
org.apache.spark.sql.catalyst.analysis.NoSuchDatabaseException: Database `delete_db` not found;
```

Pass the Jenkins tests including an updated testcase.

Author: Dongjoon Hyun <dongjoon@apache.org>

Closes #14115 from dongjoon-hyun/SPARK-16459.

(cherry picked from commit 7ac79da)
Signed-off-by: Herman van Hovell <hvanhovell@databricks.com>
@hvanhovell
Copy link
Contributor

Cherry picked this into 2.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants