Github user gatorsmile commented on a diff in the pull request:

    https://github.com/apache/spark/pull/19086#discussion_r136723572
  
    --- Diff: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/catalog/SessionCatalog.scala
 ---
    @@ -573,28 +573,29 @@ class SessionCatalog(
        * 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 `newName` matches the one in 
`oldName`.
    +   * If the database specified in `newName` is different from the one 
specified in `oldName`,
    +   * It will result in moving table across databases.
        */
       def renameTable(oldName: TableIdentifier, newName: TableIdentifier): 
Unit = synchronized {
    -    val db = formatDatabaseName(oldName.database.getOrElse(currentDb))
    -    newName.database.map(formatDatabaseName).foreach { newDb =>
    -      if (db != newDb) {
    -        throw new AnalysisException(
    -          s"RENAME TABLE source and destination databases do not match: 
'$db' != '$newDb'")
    -      }
    -    }
    +    val oldDb = formatDatabaseName(oldName.database.getOrElse(currentDb))
    +    val newDb = formatDatabaseName(newName.database.getOrElse(currentDb))
     
         val oldTableName = formatTableName(oldName.table)
         val newTableName = formatTableName(newName.table)
    -    if (db == globalTempViewManager.database) {
    +    if (oldDb == globalTempViewManager.database || newDb == 
globalTempViewManager.database) {
    +      if (oldDb != newDb) {
    +        throw new AnalysisException(
    +          s"Cannot change database of table '$oldTableName'")
    +      }
           globalTempViewManager.rename(oldTableName, newTableName)
         } else {
    -      requireDbExists(db)
           if (oldName.database.isDefined || 
!tempTables.contains(oldTableName)) {
    -        requireTableExists(TableIdentifier(oldTableName, Some(db)))
    -        requireTableNotExists(TableIdentifier(newTableName, Some(db)))
    +        requireDbExists(oldDb)
    +        requireDbExists(newDb)
    +        requireTableExists(TableIdentifier(oldTableName, Some(oldDb)))
    +        requireTableNotExists(TableIdentifier(newTableName, Some(newDb)))
             validateName(newTableName)
    -        externalCatalog.renameTable(db, oldTableName, newTableName)
    +        externalCatalog.renameTable(oldDb, oldTableName, newDb, 
newTableName)
           } else {
             if (newName.database.isDefined) {
    --- End diff --
    
    Add a comment 
    > // when the old table is a non-global temporary view


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

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

Reply via email to