You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by "lunar-shadow (via GitHub)" <gi...@apache.org> on 2026/04/24 19:58:46 UTC

[PR] [SPARK-56611][SQL] Fix ALTER TABLE RENAME TO with catalog-qualified and table-only targets [spark]

lunar-shadow opened a new pull request, #55547:
URL: https://github.com/apache/spark/pull/55547

   <!--
   Thanks for sending a pull request!  Here are some tips for you:
     1. If this is your first time, please read our contributor guidelines: https://spark.apache.org/contributing.html
     2. Ensure you have added or run the appropriate tests for your PR: https://spark.apache.org/developer-tools.html
     3. If the PR is unfinished, add '[WIP]' in your PR title, e.g., '[WIP][SPARK-XXXX] Your PR title ...'.
     4. Be sure to keep the PR description updated to reflect all changes.
     5. Please write your PR title to summarize what this PR proposes.
     6. If possible, provide a concise example to reproduce the issue for a faster review.
     7. If you want to add a new configuration, please read the guideline first for naming configurations in
        'core/src/main/scala/org/apache/spark/internal/config/ConfigEntry.scala'.
     8. If you want to add or modify an error type or message, please read the guideline first in
        'common/utils/src/main/resources/error/README.md'.
   -->
   
   ### What changes were proposed in this pull request?
   <!--
   Please clarify what changes you are proposing. The purpose of this section is to outline the changes and how this PR fixes the issue. 
   If possible, please consider writing useful notes for better and faster reviews in your PR. See the examples below.
     1. If you refactor some codes with changing classes, showing the class hierarchy will help reviewers.
     2. If you fix some SQL features, you can provide some references of other DBMSes.
     3. If there is design documentation, please add the link.
     4. If there is a discussion in the mailing list, please add the link.
   -->
   
   This fixes `ALTER TABLE ... RENAME TO` in a couple of cases that weren’t handled correctly:
   
   * When the target name is catalog-qualified in V2 catalogs
   * When the target is just a table name (no namespace) in both V1 and V2 catalogs
   
   **Changes:**
   
   * Handle catalog-qualified targets correctly in V2 (strip catalog when it matches source)
   * Inherit source namespace when target is table-only (V1 & V2)
   * Add tests covering the above cases
   
   ### Why are the changes needed?
   <!--
   Please clarify why the changes are needed. For instance,
     1. If you propose a new API, clarify the use case for a new API.
     2. If you fix a bug, you can clarify why it is a bug.
   -->
   
   Right now, rename behaves incorrectly in a few scenarios.
   
   For example:
   
   ```sql
   ALTER TABLE testcat.ns.t1 RENAME TO testcat.ns.t2;
   ```
   
   fails because `asIdentifier` treats everything except the last part as the namespace. That means the catalog name (`testcat`) incorrectly ends up inside the namespace:
   
   ```scala
   Identifier.of(["testcat", "ns"], "t2")
   ```
   
   instead of:
   
   ```scala
   Identifier.of(["ns"], "t2")
   ```
   
   Similarly:
   
   ```sql
   ALTER TABLE testcat.ns.t1 RENAME TO t2;
   ```
   
   creates an identifier with an empty namespace, instead of inheriting `ns` from the source table.
   
   **Steps to reproduce:**
   
   ```sql
   CREATE TABLE testcat.ns.t1 USING foo AS SELECT 1 AS id;
   ALTER TABLE testcat.ns.t1 RENAME TO testcat.ns.t2;
   ```
   
   This fails with a namespace resolution error due to the catalog being treated as part of the namespace.
   
   
   ### Does this PR introduce _any_ user-facing change?
   <!--
   Note that it means *any* user-facing change including all aspects such as new features, bug fixes, or other behavior changes. Documentation-only updates are not considered user-facing changes.
   
   If yes, please clarify the previous behavior and the change this PR proposes - provide the console output, description and/or an example to show the behavior difference if possible.
   If possible, please also clarify if this is a user-facing change compared to the released Spark versions or within the unreleased branches such as master.
   If no, write 'No'.
   -->
   
   Yes.
   
   * Catalog-qualified renames like ALTER TABLE catalog.ns.t1 RENAME TO catalog.ns.t2 now work as expected
   * Renaming to a table-only name (e.g., `RENAME TO t2`) now correctly keeps the original namespace
   * Cross-namespace renames (e.g., RENAME TO other_ns.t2) continue to work for V2 catalogs as before 
   
   ### How was this patch tested?
   <!--
   If tests were added, say they were added here. Please make sure to add some test cases that check the changes thoroughly including negative and positive cases if possible.
   If it was tested in a way different from regular unit tests, please clarify how you tested step by step, ideally copy and paste-able, so that other reviewers can test and check, and descendants can verify in the future.
   If tests were not added, please describe why they were not added and/or why it was difficult to add.
   If benchmark tests were added, please run the benchmarks in GitHub Actions for the consistent environment, and the instructions could accord to: https://spark.apache.org/developer-tools.html#github-workflow-benchmarks.
   -->
   
   Added test coverage in `DataSourceV2SQLSuite`:
   
   * Rename with catalog-qualified identifiers (including table-only and namespace-qualified cases) 
   * Rename across namespaces for V2 catalogs
   
   
   ### Was this patch authored or co-authored using generative AI tooling?
   <!--
   If generative AI tooling has been used in the process of authoring this patch, please include the
   phrase: 'Generated-by: ' followed by the name of the tool and its version.
   If no, write 'No'.
   Please refer to the [ASF Generative Tooling Guidance](https://www.apache.org/legal/generative-tooling.html) for details.
   -->
   No
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-56611][SQL] Fix ALTER TABLE RENAME TO with catalog-qualified and table-only targets [spark]

Posted by "lunar-shadow (via GitHub)" <gi...@apache.org>.
lunar-shadow commented on code in PR #55547:
URL: https://github.com/apache/spark/pull/55547#discussion_r3152967915


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/DataSourceV2Strategy.scala:
##########
@@ -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()) {

Review Comment:
   Thanks @dongjoon-hyun for catching this. 
   
   Fixed, now using `SQLConf.get.resolver` for case-insensitive comparison. Added a test case: `ALTER TABLE testcat.ns.t1_renamed RENAME TO TesTcaT.ns.t1` 



##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/DataSourceV2Strategy.scala:
##########
@@ -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) {

Review Comment:
   Fixed, thanks.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-56611][SQL] Fix ALTER TABLE RENAME TO with catalog-qualified and table-only targets [spark]

Posted by "lunar-shadow (via GitHub)" <gi...@apache.org>.
lunar-shadow commented on code in PR #55547:
URL: https://github.com/apache/spark/pull/55547#discussion_r3152977226


##########
sql/core/src/test/scala/org/apache/spark/sql/connector/DataSourceV2SQLSuite.scala:
##########
@@ -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")

Review Comment:
   Cross-catalog rename is not addressed as part of this PR. I noticed it currently silently treats the other catalog name as a namespace. I'll file a follow-up JIRA for proper validation.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-56611][SQL] Fix ALTER TABLE RENAME TO with catalog-qualified targets [spark]

Posted by "mervyn-lobo (via GitHub)" <gi...@apache.org>.
mervyn-lobo closed pull request #55547: [SPARK-56611][SQL] Fix ALTER TABLE RENAME TO with catalog-qualified targets
URL: https://github.com/apache/spark/pull/55547


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-56611][SQL] Fix ALTER TABLE RENAME TO with catalog-qualified and table-only targets [spark]

Posted by "lunar-shadow (via GitHub)" <gi...@apache.org>.
lunar-shadow commented on code in PR #55547:
URL: https://github.com/apache/spark/pull/55547#discussion_r3152975045


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/DataSourceV2Strategy.scala:
##########
@@ -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

Review Comment:
   Fixed, thanks. 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-56611][SQL] Fix ALTER TABLE RENAME TO with catalog-qualified targets [spark]

Posted by "peter-toth (via GitHub)" <gi...@apache.org>.
peter-toth commented on PR #55547:
URL: https://github.com/apache/spark/pull/55547#issuecomment-4395487561

   @cloud-fan , while the consistent behaviour also makes sense, I'm sure that it will cause lots of confusion.
   
   Shall we call out the difference between the 2 `table_identifier` at https://spark.apache.org/docs/latest/sql-ref-syntax-ddl-alter-table.html#rename and https://spark.apache.org/docs/latest/sql-ref-syntax-ddl-alter-view.html#rename-view?
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-56611][SQL] Fix ALTER TABLE RENAME TO with catalog-qualified targets [spark]

Posted by "dongjoon-hyun (via GitHub)" <gi...@apache.org>.
dongjoon-hyun commented on PR #55547:
URL: https://github.com/apache/spark/pull/55547#issuecomment-4406109466

   To @mervyn-lobo , could you reply for [the above review comment](https://github.com/apache/spark/pull/55547#pullrequestreview-4241607663).


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-56611][SQL] Fix ALTER TABLE RENAME TO with catalog-qualified and table-only targets [spark]

Posted by "peter-toth (via GitHub)" <gi...@apache.org>.
peter-toth commented on code in PR #55547:
URL: https://github.com/apache/spark/pull/55547#discussion_r3153227871


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/DataSourceV2Strategy.scala:
##########
@@ -448,14 +448,31 @@ 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 &&
+          SQLConf.get.resolver(rawNewNameParts.head, catalog.name())) {
+        rawNewNameParts.tail
+      } else {
+        rawNewNameParts
+      }
+
+      val namespace = if (newNameParts.length == 1) {
+        oldIdent.namespace()
+      } else {
+        newNameParts.init.toArray
+      }
+
+      val newIdent = Identifier.of(namespace, newNameParts.last)

Review Comment:
   Minor nit:
   ```suggestion
         val newIdent = if (newNameParts.length == 1) {
           Identifier.of(oldIdent.namespace(), newNameParts.last)
         } else {
           newNameParts.asIdentifier
         }
   ```



##########
sql/core/src/test/scala/org/apache/spark/sql/connector/DataSourceV2SQLSuite.scala:
##########
@@ -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")

Review Comment:
   Shouldn't the above be interpreted as move (rename) `t1` table from `ns1` namespace to `othercat.ns2` namespace in `testcat` catalog? So doesn't the current PR work as expected?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-56611][SQL] Fix ALTER TABLE RENAME TO with catalog-qualified and table-only targets [spark]

Posted by "peter-toth (via GitHub)" <gi...@apache.org>.
peter-toth commented on PR #55547:
URL: https://github.com/apache/spark/pull/55547#issuecomment-4334333119

   cc @cloud-fan, @gengliangwang 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-56611][SQL] Fix ALTER TABLE RENAME TO with catalog-qualified and table-only targets [spark]

Posted by "dongjoon-hyun (via GitHub)" <gi...@apache.org>.
dongjoon-hyun commented on code in PR #55547:
URL: https://github.com/apache/spark/pull/55547#discussion_r3195738724


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/DataSourceV2Strategy.scala:
##########
@@ -448,14 +448,29 @@ 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 &&
+          SQLConf.get.resolver(rawNewNameParts.head, catalog.name())) {

Review Comment:
   `session.sessionState.conf.resolver` will be better in this case.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-56611][SQL] Fix ALTER TABLE RENAME TO with catalog-qualified and table-only targets [spark]

Posted by "lunar-shadow (via GitHub)" <gi...@apache.org>.
lunar-shadow commented on code in PR #55547:
URL: https://github.com/apache/spark/pull/55547#discussion_r3152971776


##########
sql/core/src/test/scala/org/apache/spark/sql/connector/DataSourceV2SQLSuite.scala:
##########
@@ -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") {

Review Comment:
   Done, renamed to "rename table across namespaces"  



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-56611][SQL] Fix ALTER TABLE RENAME TO with catalog-qualified and table-only targets [spark]

Posted by "lunar-shadow (via GitHub)" <gi...@apache.org>.
lunar-shadow commented on code in PR #55547:
URL: https://github.com/apache/spark/pull/55547#discussion_r3159690324


##########
sql/core/src/test/scala/org/apache/spark/sql/connector/DataSourceV2SQLSuite.scala:
##########
@@ -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")

Review Comment:
   Right, cross-catalog rename may not be a valid operation as RENAME operates within a single catalog. So `othercat.ns2.t1` is correctly treated as a namespace path within `testcat`. Added a test case to verify this. No follow-up needed, thanks for the clarification.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-56611][SQL] Fix ALTER TABLE RENAME TO with catalog-qualified and table-only targets [spark]

Posted by "lunar-shadow (via GitHub)" <gi...@apache.org>.
lunar-shadow commented on code in PR #55547:
URL: https://github.com/apache/spark/pull/55547#discussion_r3159681088


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/DataSourceV2Strategy.scala:
##########
@@ -448,14 +448,31 @@ 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 &&
+          SQLConf.get.resolver(rawNewNameParts.head, catalog.name())) {
+        rawNewNameParts.tail
+      } else {
+        rawNewNameParts
+      }
+
+      val namespace = if (newNameParts.length == 1) {
+        oldIdent.namespace()
+      } else {
+        newNameParts.init.toArray
+      }
+
+      val newIdent = Identifier.of(namespace, newNameParts.last)

Review Comment:
   adopted this change, thanks!  



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-56611][SQL] Fix ALTER TABLE RENAME TO with catalog-qualified targets [spark]

Posted by "mervyn-lobo (via GitHub)" <gi...@apache.org>.
mervyn-lobo commented on PR #55547:
URL: https://github.com/apache/spark/pull/55547#issuecomment-4412345175

   > To @mervyn-lobo , could you reply for [the above review comment](https://github.com/apache/spark/pull/55547#pullrequestreview-4241607663)?
   
   Thanks @peter-toth, I agree this behavior can be confusing from a user perspective, especially given how catalog-qualified identifiers are typically interpreted.
   
   That said, I understand @cloud-fan’s point that the current behavior is consistent with multipart namespace semantics and changing it would introduce ambiguity and break existing expectations.
   
   Calling this out in the docs (and clarifying any differences with ALTER VIEW) sounds like the right direction. I’ll go ahead and close this PR.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-56611][SQL] Fix ALTER TABLE RENAME TO with catalog-qualified targets [spark]

Posted by "mervyn-lobo (via GitHub)" <gi...@apache.org>.
mervyn-lobo commented on PR #55547:
URL: https://github.com/apache/spark/pull/55547#issuecomment-4412331837

   > To @mervyn-lobo , could you reply for [the above review comment](https://github.com/apache/spark/pull/55547#pullrequestreview-4241607663)?
   
   Thanks @peter-toth, I agree this behavior can be confusing from a user perspective, especially given how catalog-qualified identifiers are typically interpreted.
   
   That said, I understand @cloud-fan’s point that the current behavior is consistent with multipart namespace semantics and changing it would introduce ambiguity and break existing expectations.
   
   Calling this out in the docs (and clarifying any differences with ALTER VIEW) sounds like the right direction. I’ll go ahead and close this PR.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-56611][SQL] Fix ALTER TABLE RENAME TO with catalog-qualified and table-only targets [spark]

Posted by "dongjoon-hyun (via GitHub)" <gi...@apache.org>.
dongjoon-hyun commented on code in PR #55547:
URL: https://github.com/apache/spark/pull/55547#discussion_r3148475962


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/DataSourceV2Strategy.scala:
##########
@@ -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()) {

Review Comment:
   This looks like case-sensitive for catalog names which is incorrect, doesn't it? Please add a unit test case like the following?
   
   ```
   ALTER TABLE testcat.ns.t1 RENAME TO TesTcaT.ns.t1_renamed
   ```



##########
sql/core/src/test/scala/org/apache/spark/sql/connector/DataSourceV2SQLSuite.scala:
##########
@@ -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") {

Review Comment:
   We don't need to mention `for V2 catalogs` phrase because this is inside `DataSourceV2SQLSuite`.



##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/DataSourceV2Strategy.scala:
##########
@@ -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

Review Comment:
   Do we have a test case for this?



##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/DataSourceV2Strategy.scala:
##########
@@ -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

Review Comment:
   In this case, we can use `init.toArray` like `asIdentifier`.
   
   https://github.com/apache/spark/blob/e51baaee89484867775747bc20d46d796a0c0deb/sql/catalyst/src/main/scala/org/apache/spark/sql/connector/catalog/CatalogV2Implicits.scala#L210
   
   ```
   - newNameParts.dropRight(1).toArray
   + newNameParts.init.toArray
   ```



##########
sql/core/src/test/scala/org/apache/spark/sql/connector/DataSourceV2SQLSuite.scala:
##########
@@ -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")

Review Comment:
   Dose this PR aim to support `ALTER TABLE testcat.ns1.t1 RENAME TO othercat.ns2.t1`? Could you add a test case?



##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/DataSourceV2Strategy.scala:
##########
@@ -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) {

Review Comment:
   Please follow the community Scala coding style for `val namespace = if (...) {`.
   - https://spark.apache.org/contributing.html
   - https://github.com/databricks/scala-style-guide
   
   ```
   val ret = if (condition) {
     Seq(1, 2, 3)
   } else {
     Seq(1, 2, 3)
   }
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-56611][SQL] Fix ALTER TABLE RENAME TO with catalog-qualified and table-only targets [spark]

Posted by "dongjoon-hyun (via GitHub)" <gi...@apache.org>.
dongjoon-hyun commented on PR #55547:
URL: https://github.com/apache/spark/pull/55547#issuecomment-4328564611

   cc @peter-toth , too.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-56611][SQL] Fix ALTER TABLE RENAME TO with catalog-qualified and table-only targets [spark]

Posted by "lunar-shadow (via GitHub)" <gi...@apache.org>.
lunar-shadow commented on code in PR #55547:
URL: https://github.com/apache/spark/pull/55547#discussion_r3152979824


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/DataSourceV2Strategy.scala:
##########
@@ -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

Review Comment:
    Yes, both `RENAME TO testcat.ns.t1_renamed` and `RENAME TO TesTcaT.ns.t1` in the test hit this code path. 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org