viirya commented on a change in pull request #30774:
URL: https://github.com/apache/spark/pull/30774#discussion_r543157855



##########
File path: 
sql/core/src/main/scala/org/apache/spark/sql/execution/command/tables.scala
##########
@@ -195,16 +195,21 @@ case class AlterTableRenameCommand(
       DDLUtils.verifyAlterTableType(catalog, table, isView)
       // If an exception is thrown here we can just assume the table is 
uncached;
       // this can happen with Hive tables when the underlying catalog is 
in-memory.
-      val wasCached = 
Try(sparkSession.catalog.isCached(oldName.unquotedString)).getOrElse(false)
-      if (wasCached) {
+      // If `optStorageLevel` is defined, the old table was cached.
+      val optStorageLevel = Try {
+        val optCachedData = 
sparkSession.sharedState.cacheManager.lookupCachedData(
+          sparkSession.table(oldName.unquotedString))
+        optCachedData.map(_.cachedRepresentation.cacheBuilder.storageLevel)
+      }.getOrElse(None)
+      optStorageLevel.foreach { _ =>
         CommandUtils.uncacheTableOrView(sparkSession, oldName.unquotedString)
       }
       // Invalidate the table last, otherwise uncaching the table would load 
the logical plan
       // back into the hive metastore cache
       catalog.refreshTable(oldName)
       catalog.renameTable(oldName, newName)
-      if (wasCached) {
-        sparkSession.catalog.cacheTable(newName.unquotedString)
+      optStorageLevel.foreach { storageLevel =>
+        sparkSession.catalog.cacheTable(newName.unquotedString, storageLevel)

Review comment:
       Hm, you can check the change like #30769. Especially how it recaches the 
table. There is a `cacheName` parameter. If the table was cached with a cache 
name, when recaching it, I think we should keep it.
   
   ```scala
       val cache = session.sharedState.cacheManager.lookupCachedData(v2Relation)
       session.sharedState.cacheManager.uncacheQuery(session, v2Relation, 
cascade = true)
       session.sharedState.cacheManager.uncacheQuery(session, v2Relation, 
cascade = true)
       if (recacheTable && cache.isDefined) {
         // save the cache name and cache level for recreation
         val cacheName = cache.get.cachedRepresentation.cacheBuilder.tableName
         val cacheLevel = 
cache.get.cachedRepresentation.cacheBuilder.storageLevel
   
         // recache with the same name and cache level.
         val ds = Dataset.ofRows(session, v2Relation)
         session.sharedState.cacheManager.cacheQuery(ds, cacheName, cacheLevel)
       }
   ```




----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to