-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-24675][SQL]Rename table: validate existence of new location #21655
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -619,6 +619,7 @@ class SessionCatalog( | |
| requireTableExists(TableIdentifier(oldTableName, Some(db))) | ||
| requireTableNotExists(TableIdentifier(newTableName, Some(db))) | ||
| validateName(newTableName) | ||
| validateLocationOfRename(oldName, newName) | ||
| externalCatalog.renameTable(db, oldTableName, newTableName) | ||
| } else { | ||
| if (newName.database.isDefined) { | ||
|
|
@@ -1366,4 +1367,18 @@ class SessionCatalog( | |
| // copy over temporary views | ||
| tempViews.foreach(kv => target.tempViews.put(kv._1, kv._2)) | ||
| } | ||
|
|
||
| private def validateLocationOfRename(oldName: TableIdentifier, newName: TableIdentifier): Unit = { | ||
| val oldTable = getTableMetadata(oldName) | ||
| if (oldTable.tableType == CatalogTableType.MANAGED) { | ||
| val databaseLocation = | ||
| externalCatalog.getDatabase(oldName.database.getOrElse(currentDb)).locationUri | ||
| val newTableLocation = new Path(new Path(databaseLocation), formatTableName(newName.table)) | ||
| val fs = newTableLocation.getFileSystem(hadoopConf) | ||
| if (fs.exists(newTableLocation)) { | ||
| throw new AnalysisException(s"Can not rename the managed table('${oldName}')" + | ||
|
||
| s". The associated location('${newTableLocation.toString}') already exists.") | ||
|
||
| } | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -441,6 +441,24 @@ abstract class DDLSuite extends QueryTest with SQLTestUtils { | |
| } | ||
| } | ||
|
|
||
| test ("rename a managed table with existing empty directory") { | ||
| val tableLoc = new File(spark.sessionState.catalog.defaultTablePath(TableIdentifier("tab2"))) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: remove the space between |
||
| try { | ||
| withTable("tab1") { | ||
| sql(s"CREATE TABLE tab1 USING ${dataSource} AS SELECT 1, 'a'") | ||
|
||
| tableLoc.mkdir() | ||
| val ex = intercept[AnalysisException] { | ||
| sql("ALTER TABLE tab1 RENAME TO tab2") | ||
| }.getMessage | ||
| val expectedMsg = "Can not rename the managed table('`tab1`'). The associated location" | ||
| assert(ex.contains(expectedMsg)) | ||
| } | ||
| } finally { | ||
| waitForTasksToFinish() | ||
| Utils.deleteRecursively(tableLoc) | ||
| } | ||
| } | ||
|
|
||
| private def checkSchemaInCreatedDataSourceTable( | ||
| path: File, | ||
| userSpecifiedSchema: Option[String], | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add code comments along with
validateName?