Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,7 @@ class SessionCatalog(
requireTableExists(TableIdentifier(oldTableName, Some(db)))
requireTableNotExists(TableIdentifier(newTableName, Some(db)))
validateName(newTableName)
validateNewLocationOfRename(oldName, newName)
Copy link
Member

Choose a reason for hiding this comment

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

Add this new behavior to migration guide?

externalCatalog.renameTable(db, oldTableName, newTableName)
} else {
if (newName.database.isDefined) {
Expand Down Expand Up @@ -1366,4 +1367,23 @@ class SessionCatalog(
// copy over temporary views
tempViews.foreach(kv => target.tempViews.put(kv._1, kv._2))
}

/**
* Validate the new locatoin before renaming a managed table, which should be non-existent.
*/
private def validateNewLocationOfRename(
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') already exists.")
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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")))
Copy link
Member

Choose a reason for hiding this comment

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

nit: remove the space between test and (.

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],
Expand Down