aboutsummaryrefslogtreecommitdiff
path: root/sql/catalyst
diff options
context:
space:
mode:
authorWenchen Fan <wenchen@databricks.com>2016-09-05 13:09:20 +0800
committerWenchen Fan <wenchen@databricks.com>2016-09-05 13:09:20 +0800
commit3ccb23e445711ea5d9059eb6de7c490c8fc9d112 (patch)
tree0d3d7652b065b16c1fcaaf54c466d0907a00b2c7 /sql/catalyst
parentc1e9a6d274c281ec30e6d022eedfbe3a2988f721 (diff)
downloadspark-3ccb23e445711ea5d9059eb6de7c490c8fc9d112.tar.gz
spark-3ccb23e445711ea5d9059eb6de7c490c8fc9d112.tar.bz2
spark-3ccb23e445711ea5d9059eb6de7c490c8fc9d112.zip
[SPARK-17394][SQL] should not allow specify database in table/view name after RENAME TO
## What changes were proposed in this pull request? It's really weird that we allow users to specify database in both from table name and to table name in `ALTER TABLE RENAME TO`, while logically we can't support rename a table to a different database. Both postgres and MySQL disallow this syntax, it's reasonable to follow them and simply our code. ## How was this patch tested? new test in `DDLCommandSuite` Author: Wenchen Fan <wenchen@databricks.com> Closes #14955 from cloud-fan/rename.
Diffstat (limited to 'sql/catalyst')
-rw-r--r--sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/catalog/SessionCatalog.scala16
-rw-r--r--sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/catalog/SessionCatalogSuite.scala27
2 files changed, 10 insertions, 33 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 e7132cd397..9fb5db573b 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
@@ -349,29 +349,17 @@ class SessionCatalog(
* If a database is specified in `oldName`, this will rename the table in that database.
* If no database is specified, this will first attempt to rename a temporary table with
* the same name, then, if that does not exist, rename the table in the current database.
- *
- * This assumes the database specified in `oldName` matches the one specified in `newName`.
*/
- def renameTable(oldName: TableIdentifier, newName: TableIdentifier): Unit = synchronized {
+ def renameTable(oldName: TableIdentifier, newName: String): Unit = synchronized {
val db = formatDatabaseName(oldName.database.getOrElse(currentDb))
requireDbExists(db)
- val newDb = formatDatabaseName(newName.database.getOrElse(currentDb))
- if (db != newDb) {
- throw new AnalysisException(
- s"RENAME TABLE source and destination databases do not match: '$db' != '$newDb'")
- }
val oldTableName = formatTableName(oldName.table)
- val newTableName = formatTableName(newName.table)
+ val newTableName = formatTableName(newName)
if (oldName.database.isDefined || !tempTables.contains(oldTableName)) {
requireTableExists(TableIdentifier(oldTableName, Some(db)))
requireTableNotExists(TableIdentifier(newTableName, Some(db)))
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")
diff --git a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/catalog/SessionCatalogSuite.scala b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/catalog/SessionCatalogSuite.scala
index c9d4fef805..012df629bb 100644
--- a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/catalog/SessionCatalogSuite.scala
+++ b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/catalog/SessionCatalogSuite.scala
@@ -273,37 +273,27 @@ class SessionCatalogSuite extends SparkFunSuite {
val externalCatalog = newBasicCatalog()
val sessionCatalog = new SessionCatalog(externalCatalog)
assert(externalCatalog.listTables("db2").toSet == Set("tbl1", "tbl2"))
- sessionCatalog.renameTable(
- TableIdentifier("tbl1", Some("db2")), TableIdentifier("tblone", Some("db2")))
+ sessionCatalog.renameTable(TableIdentifier("tbl1", Some("db2")), "tblone")
assert(externalCatalog.listTables("db2").toSet == Set("tblone", "tbl2"))
- sessionCatalog.renameTable(
- TableIdentifier("tbl2", Some("db2")), TableIdentifier("tbltwo", Some("db2")))
+ sessionCatalog.renameTable(TableIdentifier("tbl2", Some("db2")), "tbltwo")
assert(externalCatalog.listTables("db2").toSet == Set("tblone", "tbltwo"))
// Rename table without explicitly specifying database
sessionCatalog.setCurrentDatabase("db2")
- sessionCatalog.renameTable(TableIdentifier("tbltwo"), TableIdentifier("table_two"))
+ sessionCatalog.renameTable(TableIdentifier("tbltwo"), "table_two")
assert(externalCatalog.listTables("db2").toSet == Set("tblone", "table_two"))
- // Renaming "db2.tblone" to "db1.tblones" should fail because databases don't match
- intercept[AnalysisException] {
- sessionCatalog.renameTable(
- TableIdentifier("tblone", Some("db2")), TableIdentifier("tblones", Some("db1")))
- }
// The new table already exists
intercept[TableAlreadyExistsException] {
- sessionCatalog.renameTable(
- TableIdentifier("tblone", Some("db2")), TableIdentifier("table_two", Some("db2")))
+ sessionCatalog.renameTable(TableIdentifier("tblone", Some("db2")), "table_two")
}
}
test("rename table when database/table does not exist") {
val catalog = new SessionCatalog(newBasicCatalog())
intercept[NoSuchDatabaseException] {
- catalog.renameTable(
- TableIdentifier("tbl1", Some("unknown_db")), TableIdentifier("tbl2", Some("unknown_db")))
+ catalog.renameTable(TableIdentifier("tbl1", Some("unknown_db")), "tbl2")
}
intercept[NoSuchTableException] {
- catalog.renameTable(
- TableIdentifier("unknown_table", Some("db2")), TableIdentifier("tbl2", Some("db2")))
+ catalog.renameTable(TableIdentifier("unknown_table", Some("db2")), "tbl2")
}
}
@@ -316,13 +306,12 @@ class SessionCatalogSuite extends SparkFunSuite {
assert(sessionCatalog.getTempTable("tbl1") == Option(tempTable))
assert(externalCatalog.listTables("db2").toSet == Set("tbl1", "tbl2"))
// If database is not specified, temp table should be renamed first
- sessionCatalog.renameTable(TableIdentifier("tbl1"), TableIdentifier("tbl3"))
+ sessionCatalog.renameTable(TableIdentifier("tbl1"), "tbl3")
assert(sessionCatalog.getTempTable("tbl1").isEmpty)
assert(sessionCatalog.getTempTable("tbl3") == Option(tempTable))
assert(externalCatalog.listTables("db2").toSet == Set("tbl1", "tbl2"))
// If database is specified, temp tables are never renamed
- sessionCatalog.renameTable(
- TableIdentifier("tbl2", Some("db2")), TableIdentifier("tbl4", Some("db2")))
+ sessionCatalog.renameTable(TableIdentifier("tbl2", Some("db2")), "tbl4")
assert(sessionCatalog.getTempTable("tbl3") == Option(tempTable))
assert(sessionCatalog.getTempTable("tbl4").isEmpty)
assert(externalCatalog.listTables("db2").toSet == Set("tbl1", "tbl4"))