aboutsummaryrefslogtreecommitdiff
path: root/sql
diff options
context:
space:
mode:
authorgatorsmile <gatorsmile@gmail.com>2016-05-09 13:05:18 +0800
committerWenchen Fan <wenchen@databricks.com>2016-05-09 13:05:18 +0800
commita59ab594cac5189ecf4158fc0ada200eaa874158 (patch)
tree803c91dbb9af0a9ab9e6ecd2ed3d9d1a9667d45e /sql
parente9131ec277731de4a73026f2fb4559182c236f84 (diff)
downloadspark-a59ab594cac5189ecf4158fc0ada200eaa874158.tar.gz
spark-a59ab594cac5189ecf4158fc0ada200eaa874158.tar.bz2
spark-a59ab594cac5189ecf4158fc0ada200eaa874158.zip
[SPARK-15184][SQL] Fix Silent Removal of An Existent Temp Table by Rename Table
#### What changes were proposed in this pull request? Currently, if we rename a temp table `Tab1` to another existent temp table `Tab2`. `Tab2` will be silently removed. This PR is to detect it and issue an exception message. In addition, this PR also detects another issue in the rename table command. When the destination table identifier does have database name, we should not ignore them. That might mean users could rename a regular table. #### How was this patch tested? Added two related test cases Author: gatorsmile <gatorsmile@gmail.com> Closes #12959 from gatorsmile/rewriteTable.
Diffstat (limited to 'sql')
-rw-r--r--sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/catalog/SessionCatalog.scala9
-rw-r--r--sql/core/src/test/scala/org/apache/spark/sql/execution/command/DDLSuite.scala60
2 files changed, 69 insertions, 0 deletions
diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/catalog/SessionCatalog.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/catalog/SessionCatalog.scala
index 9918bce455..18524e4118 100644
--- a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/catalog/SessionCatalog.scala
+++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/catalog/SessionCatalog.scala
@@ -301,6 +301,15 @@ class SessionCatalog(
if (oldName.database.isDefined || !tempTables.contains(oldTableName)) {
externalCatalog.renameTable(db, oldTableName, newTableName)
} else {
+ if (newName.database.isDefined) {
+ throw new AnalysisException(
+ s"RENAME TEMPORARY TABLE from '$oldName' to '$newName': cannot specify database " +
+ s"name '${newName.database.get}' in the destination table")
+ }
+ if (tempTables.contains(newTableName)) {
+ throw new AnalysisException(
+ s"RENAME TEMPORARY TABLE from '$oldName' to '$newName': destination table already exists")
+ }
val table = tempTables(oldTableName)
tempTables.remove(oldTableName)
tempTables.put(newTableName, table)
diff --git a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/DDLSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/DDLSuite.scala
index 6085098a70..f72325b5ce 100644
--- a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/DDLSuite.scala
+++ b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/DDLSuite.scala
@@ -396,6 +396,66 @@ class DDLSuite extends QueryTest with SharedSQLContext with BeforeAndAfterEach {
}
}
+ test("rename temporary table - destination table with database name") {
+ withTempTable("tab1") {
+ sql(
+ """
+ |CREATE TEMPORARY TABLE tab1
+ |USING org.apache.spark.sql.sources.DDLScanSource
+ |OPTIONS (
+ | From '1',
+ | To '10',
+ | Table 'test1'
+ |)
+ """.stripMargin)
+
+ val e = intercept[AnalysisException] {
+ sql("ALTER TABLE tab1 RENAME TO default.tab2")
+ }
+ assert(e.getMessage.contains(
+ "RENAME TEMPORARY TABLE from '`tab1`' to '`default`.`tab2`': " +
+ "cannot specify database name 'default' in the destination table"))
+
+ val catalog = sqlContext.sessionState.catalog
+ assert(catalog.listTables("default") == Seq(TableIdentifier("tab1")))
+ }
+ }
+
+ test("rename temporary table - destination table already exists") {
+ withTempTable("tab1", "tab2") {
+ sql(
+ """
+ |CREATE TEMPORARY TABLE tab1
+ |USING org.apache.spark.sql.sources.DDLScanSource
+ |OPTIONS (
+ | From '1',
+ | To '10',
+ | Table 'test1'
+ |)
+ """.stripMargin)
+
+ sql(
+ """
+ |CREATE TEMPORARY TABLE tab2
+ |USING org.apache.spark.sql.sources.DDLScanSource
+ |OPTIONS (
+ | From '1',
+ | To '10',
+ | Table 'test1'
+ |)
+ """.stripMargin)
+
+ val e = intercept[AnalysisException] {
+ sql("ALTER TABLE tab1 RENAME TO tab2")
+ }
+ assert(e.getMessage.contains(
+ "RENAME TEMPORARY TABLE from '`tab1`' to '`tab2`': destination table already exists"))
+
+ val catalog = sqlContext.sessionState.catalog
+ assert(catalog.listTables("default") == Seq(TableIdentifier("tab1"), TableIdentifier("tab2")))
+ }
+ }
+
test("alter table: set location") {
testSetLocation(isDatasourceTable = false)
}