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



##########
File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/Analyzer.scala
##########
@@ -1211,82 +1064,124 @@ class Analyzer(override val catalogManager: 
CatalogManager)
           case table => table
         }.getOrElse(u)
 
-      case u @ UnresolvedView(identifier, cmd, _, relationTypeMismatchHint) =>
+      case u @ UnresolvedView(identifier, cmd, allowTemp, 
relationTypeMismatchHint) =>
         lookupTableOrView(identifier).map {
+          case v: ResolvedView if v.isTemp && !allowTemp =>
+            val name = identifier.quoted
+            u.failAnalysis(s"$name is a temp view. '$cmd' expects a permanent 
view.")
           case t: ResolvedTable =>
             throw QueryCompilationErrors.expectViewNotTableError(
               t, cmd, relationTypeMismatchHint, u)
-          case view => view
+          case other => other
         }.getOrElse(u)
 
-      case u @ UnresolvedTableOrView(identifier, _, _) =>
-        lookupTableOrView(identifier).getOrElse(u)
+      case u @ UnresolvedTableOrView(identifier, cmd, allowTempView) =>
+        lookupTableOrView(identifier).map {
+          case v: ResolvedView if v.isTemp && !allowTempView =>
+            throw 
QueryCompilationErrors.expectTableOrPermanentViewNotTempViewError(
+              identifier.quoted, cmd, u)
+          case other => other
+        }.getOrElse(u)
     }
 
-    private def lookupTableOrView(identifier: Seq[String]): 
Option[LogicalPlan] = {
-      expandIdentifier(identifier) match {
-        case SessionCatalogAndIdentifier(catalog, ident) =>
-          CatalogV2Util.loadTable(catalog, ident).map {
-            case v1Table: V1Table if v1Table.v1Table.tableType == 
CatalogTableType.VIEW =>
-              ResolvedView(ident, isTemp = false)
-            case table =>
-              ResolvedTable.create(catalog.asTableCatalog, ident, table)
-          }
+    private def lookupTempView(
+        identifier: Seq[String],
+        isStreaming: Boolean = false): Option[LogicalPlan] = {
+      // We are resolving a view and this name is not a temp view when that 
view was created. We
+      // return None earlier here.
+      if (isResolvingView && !isReferredTempViewName(identifier)) return None
+
+      val tmpView = identifier match {
+        case Seq(part1) => v1SessionCatalog.lookupTempView(part1)
+        case Seq(part1, part2) => v1SessionCatalog.lookupGlobalTempView(part1, 
part2)
         case _ => None
       }
+
+      if (isStreaming && tmpView.nonEmpty && !tmpView.get.isStreaming) {
+        throw 
QueryCompilationErrors.readNonStreamingTempViewError(identifier.quoted)
+      }
+      tmpView
     }
 
-    // Look up a relation from the session catalog with the following logic:
-    // 1) If the resolved catalog is not session catalog, return None.
-    // 2) If a relation is not found in the catalog, return None.
-    // 3) If a v1 table is found, create a v1 relation. Otherwise, create a v2 
relation.
-    private def lookupRelation(
-        identifier: Seq[String],
+    private def lookupTableOrView(identifier: Seq[String]): 
Option[LogicalPlan] = {
+      lookupTempView(identifier).map { _ =>
+        ResolvedView(identifier.asIdentifier, isTemp = true)
+      }.orElse {
+        expandIdentifier(identifier) match {
+          case CatalogAndIdentifier(catalog, ident) =>
+            CatalogV2Util.loadTable(catalog, ident).map {
+              case v1Table: V1Table if CatalogV2Util.isSessionCatalog(catalog) 
&&
+                v1Table.v1Table.tableType == CatalogTableType.VIEW =>
+                ResolvedView(ident, isTemp = false)
+              case table =>
+                ResolvedTable.create(catalog.asTableCatalog, ident, table)
+            }
+          case _ => None
+        }
+      }
+    }
+
+    private def createRelation(
+        catalog: CatalogPlugin,
+        ident: Identifier,
+        table: Option[Table],
         options: CaseInsensitiveStringMap,
         isStreaming: Boolean): Option[LogicalPlan] = {
-      expandIdentifier(identifier) match {
-        case SessionCatalogAndIdentifier(catalog, ident) =>
-          lazy val loaded = CatalogV2Util.loadTable(catalog, ident).map {
-            case v1Table: V1Table =>
-              if (isStreaming) {
-                if (v1Table.v1Table.tableType == CatalogTableType.VIEW) {
-                  throw 
QueryCompilationErrors.permanentViewNotSupportedByStreamingReadingAPIError(
-                    identifier.quoted)
-                }
-                SubqueryAlias(
-                  catalog.name +: ident.asMultipartIdentifier,
-                  UnresolvedCatalogRelation(v1Table.v1Table, options, 
isStreaming = true))
-              } else {
-                v1SessionCatalog.getRelation(v1Table.v1Table, options)
-              }
-            case table =>
-              if (isStreaming) {
-                val v1Fallback = table match {
-                  case withFallback: V2TableWithV1Fallback =>
-                    Some(UnresolvedCatalogRelation(withFallback.v1Table, 
isStreaming = true))
-                  case _ => None
-                }
-                SubqueryAlias(
-                  catalog.name +: ident.asMultipartIdentifier,
-                  StreamingRelationV2(None, table.name, table, options, 
table.schema.toAttributes,
-                    Some(catalog), Some(ident), v1Fallback))
-              } else {
-                SubqueryAlias(
-                  catalog.name +: ident.asMultipartIdentifier,
-                  DataSourceV2Relation.create(table, Some(catalog), 
Some(ident), options))
-              }
+      table.map {
+        case v1Table: V1Table if CatalogV2Util.isSessionCatalog(catalog) =>

Review comment:
       Is it possible `catalog` not a session catalog? Previously 
`lookupRelation` returns `None` for that. Now it is not handled?




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