Skip to content
Open
Show file tree
Hide file tree
Changes from all 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 @@ -448,14 +448,32 @@ class DataSourceV2Strategy(session: SparkSession) extends Strategy with Predicat
case _: NoopCommand =>
LocalTableScanExec(Nil, Nil, None) :: Nil

case RenameTable(r @ ResolvedTable(catalog, oldIdent, _, _), newIdent, isView) =>
case RenameTable(r @ ResolvedTable(catalog, oldIdent, _, _), rawNewNameParts, isView) =>
if (isView) {
throw QueryCompilationErrors.cannotRenameTableWithAlterViewError()
}

// Strip catalog prefix if the identifier is catalog-qualified.
val newNameParts =
if (rawNewNameParts.length > 1 && rawNewNameParts.head == catalog.name()) {
rawNewNameParts.tail
} else {
rawNewNameParts
}

val namespace =
if (newNameParts.length == 1) {
oldIdent.namespace()
} else {
newNameParts.dropRight(1).toArray
}

val newIdent = Identifier.of(namespace, newNameParts.last)

RenameTableExec(
catalog,
oldIdent,
newIdent.asIdentifier,
newIdent,
invalidateTableCache(r),
session.sharedState.cacheManager.cacheQuery) :: Nil

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2486,6 +2486,36 @@ class DataSourceV2SQLSuiteV1Filter
ExpectedContext("testcat.ns.tbl", 11, 10 + "testcat.ns.tbl".length))
}

test("SPARK-56611: rename table with catalog-qualified identifier") {
withTable("testcat.ns.t1", "testcat.ns.t1_renamed") {
sql("CREATE TABLE testcat.ns.t1 USING foo AS SELECT id, data FROM source")
checkAnswer(sql("SHOW TABLES FROM testcat.ns"), Seq(Row("ns", "t1", false)))

// rename with just table name, inherits namespace from source
sql("ALTER TABLE testcat.ns.t1 RENAME TO t1_renamed")
checkAnswer(sql("SHOW TABLES FROM testcat.ns"), Seq(Row("ns", "t1_renamed", false)))

// rename with namespace-qualified name
sql("ALTER TABLE testcat.ns.t1_renamed RENAME TO ns.t1")
checkAnswer(sql("SHOW TABLES FROM testcat.ns"), Seq(Row("ns", "t1", false)))

// rename with catalog-qualified target identifier
sql("ALTER TABLE testcat.ns.t1 RENAME TO testcat.ns.t1_renamed")
checkAnswer(sql("SHOW TABLES FROM testcat.ns"), Seq(Row("ns", "t1_renamed", false)))
}
}

test("SPARK-56611: rename table across namespace should work for V2 catalogs") {
withTable("testcat.ns1.t1", "testcat.ns2.t1") {
sql("CREATE TABLE testcat.ns1.t1 USING foo AS SELECT id, data FROM source")
checkAnswer(sql("SHOW TABLES FROM testcat.ns1"), Seq(Row("ns1", "t1", false)))

sql("ALTER TABLE testcat.ns1.t1 RENAME TO ns2.t1")
checkAnswer(sql("SHOW TABLES FROM testcat.ns1"), Nil)
checkAnswer(sql("SHOW TABLES FROM testcat.ns2"), Seq(Row("ns2", "t1", false)))
}
}

test("ANALYZE TABLE") {
val t = "testcat.ns1.ns2.tbl"
withTable(t) {
Expand Down