aokolnychyi commented on code in PR #57799:
URL: https://github.com/apache/spark/pull/57799#discussion_r3776560425


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/RelationResolution.scala:
##########
@@ -476,23 +493,61 @@ class RelationResolution(
 
   def resolveReference(ref: V2TableReference): LogicalPlan = {
     val relation = if (ref.context.cacheable) {
-      getOrLoadRelation(ref)
+      // A temporary view may contain a relation pinned by CacheManager, so 
its re-resolution
+      // consults sharedRelationCache to preserve that Table. Transaction 
references use the
+      // Table loaded through the transaction catalog instead.
+      val useSharedRelationCache =
+        ref.context.isInstanceOf[V2TableReference.TemporaryViewContext]
+      getOrLoadRelation(ref, useSharedRelationCache)
     } else {
       loadRelation(ref)
     }
     val planId = ref.getTagValue(LogicalPlan.PLAN_ID_TAG)
     cloneWithPlanId(relation, planId)
   }
 
-  private def getOrLoadRelation(ref: V2TableReference): LogicalPlan = {
+  private def getOrLoadRelation(
+      ref: V2TableReference,
+      useSharedRelationCache: Boolean): LogicalPlan = {
     val key = toCacheKey(ref.catalog, ref.identifier, None, ref.options)
     relationCache.get(key) match {
       case Some(cached) =>
         adaptCachedRelation(cached, ref)
       case None =>
-        val relation = loadRelation(ref)
-        relationCache.update(key, relation)
-        relation
+        val resolvedCatalog = 
catalogManager.catalog(ref.catalog.name).asTableCatalog

Review Comment:
   I wonder whether we can simplify this a bit:
   
   ```
     private def getOrLoadRelation(ref: V2TableReference): LogicalPlan = {
       val key = toCacheKey(ref.catalog, ref.identifier, None, ref.options)
       relationCache.get(key) match {
         case Some(cached) =>
           adaptCachedRelation(cached, ref)
         case None =>
           val catalog = catalogManager.catalog(ref.catalog.name).asTableCatalog
           val tableKey = toTableCacheKey(catalog, ref.identifier, None, 
ref.options)
           val relation = tableCache.get(tableKey) match {
             case Some(pinnedTable) =>
               createRelation(ref, catalog, pinnedTable)
             case None =>
               val table = CatalogV2Util.getTable(catalog, ref.identifier, 
options = ref.options)
               val sharedCacheMatch = if (ref.context.sharedCacheable) {
                 lookupSharedRelationCache(catalog, ref.identifier, table, 
ref.options)
               } else {
                 None
               }
               sharedCacheMatch match {
                 case Some(cached) =>
                   tableCache.update(tableKey, cached.table)
                   adaptCachedRelation(cached, ref)
                 case None =>
                   tableCache.update(tableKey, table)
                   createRelation(ref, catalog, table)
               }
           }
           relationCache.update(key, relation)
           relation
       }
     }
   ```



-- 
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: [email protected]

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