rdblue commented on code in PR #9332:
URL: https://github.com/apache/iceberg/pull/9332#discussion_r1430721984


##########
spark/v3.5/spark-extensions/src/main/scala/org/apache/spark/sql/catalyst/parser/extensions/IcebergSparkSqlExtensionsParser.scala:
##########
@@ -122,37 +147,132 @@ class IcebergSparkSqlExtensionsParser(delegate: 
ParserInterface) extends ParserI
     if (isIcebergCommand(sqlTextAfterSubstitution)) {
       parse(sqlTextAfterSubstitution) { parser => 
astBuilder.visit(parser.singleStatement()) }.asInstanceOf[LogicalPlan]
     } else {
-      delegate.parsePlan(sqlText)
+      ViewSubstitutionExecutor.execute(delegate.parsePlan(sqlText))
     }
   }
 
-  object UnresolvedIcebergTable {
+  private object ViewSubstitutionExecutor extends RuleExecutor[LogicalPlan] {
+    private val fixedPoint = FixedPoint(
+      maxIterations,
+      errorOnExceed = true,
+      maxIterationsSetting = SQLConf.ANALYZER_MAX_ITERATIONS.key)
 
-    def unapply(plan: LogicalPlan): Option[LogicalPlan] = {
-      EliminateSubqueryAliases(plan) match {
-        case UnresolvedRelation(multipartIdentifier, _, _) if 
isIcebergTable(multipartIdentifier) =>
-          Some(plan)
-        case _ =>
+    override protected def batches: Seq[Batch] = Seq(Batch("pre-substitution", 
fixedPoint, V2ViewSubstitution))
+  }
+
+  private object V2ViewSubstitution extends Rule[LogicalPlan] {
+    import org.apache.spark.sql.connector.catalog.CatalogV2Implicits._
+
+    // the reason for handling these cases here is because 
ResolveSessionCatalog exits early for v2 commands
+    override def apply(plan: LogicalPlan): LogicalPlan = 
plan.resolveOperatorsUp {
+      case u@UnresolvedView(identifier, _, _, _) =>
+        lookupTableOrView(identifier, viewOnly = true).getOrElse(u)
+
+      case u@UnresolvedTableOrView(identifier, _, _) =>
+        lookupTableOrView(identifier).getOrElse(u)
+
+      case CreateView(UnresolvedIdentifier(nameParts, allowTemp), 
userSpecifiedColumns,
+      comment, properties, originalText, query, allowExisting, replace) =>
+        CreateIcebergView(UnresolvedIdentifier(nameParts, allowTemp), 
userSpecifiedColumns,
+          comment, properties, originalText, query, allowExisting, replace)
+
+      case ShowViews(UnresolvedNamespace(multipartIdentifier), pattern, 
output) =>
+        ShowIcebergViews(UnresolvedNamespace(multipartIdentifier), pattern, 
output)
+
+      case DropView(UnresolvedIdentifier(nameParts, allowTemp), ifExists) =>
+        DropIcebergView(UnresolvedIdentifier(nameParts, allowTemp), ifExists)
+    }
+
+    private def expandIdentifier(nameParts: Seq[String]): Seq[String] = {
+      if (!isResolvingView || isReferredTempViewName(nameParts)) return 
nameParts
+
+      if (nameParts.length == 1) {
+        AnalysisContext.get.catalogAndNamespace :+ nameParts.head
+      } else if 
(SparkSession.active.sessionState.catalogManager.isCatalogRegistered(nameParts.head))
 {
+        nameParts
+      } else {
+        AnalysisContext.get.catalogAndNamespace.head +: nameParts
+      }
+    }
+
+    /**
+     * Resolves relations to `ResolvedTable` or 
`Resolved[Temp/Persistent]View`. This is
+     * for resolving DDL and misc commands. Code is copied from Spark's 
Analyzer, but performs
+     * a view lookup before performing a table lookup.
+     */
+    private def lookupTableOrView(
+                                   identifier: Seq[String],
+                                   viewOnly: Boolean = false): 
Option[LogicalPlan] = {
+      lookupTempView(identifier).map { tempView =>
+        ResolvedTempView(identifier.asIdentifier, tempView.tableMeta.schema)
+      }.orElse {
+        val multipartIdent = expandIdentifier(identifier)
+        val catalogAndIdentifier = 
Spark3Util.catalogAndIdentifier(SparkSession.active, multipartIdent.asJava)
+        if (null != catalogAndIdentifier) {
+          
lookupView(SparkSession.active.sessionState.catalogManager.currentCatalog,
+            catalogAndIdentifier.identifier())
+            
.orElse(lookupTable(SparkSession.active.sessionState.catalogManager.currentCatalog,
+              catalogAndIdentifier.identifier()))
+        } else {
           None
+        }
       }
     }
 
-    private def isIcebergTable(multipartIdent: Seq[String]): Boolean = {
-      val catalogAndIdentifier = 
Spark3Util.catalogAndIdentifier(SparkSession.active, multipartIdent.asJava)
-      catalogAndIdentifier.catalog match {
-        case tableCatalog: TableCatalog =>
-          Try(tableCatalog.loadTable(catalogAndIdentifier.identifier))
-            .map(isIcebergTable)
-            .getOrElse(false)
+    private def isResolvingView: Boolean = 
AnalysisContext.get.catalogAndNamespace.nonEmpty
 
-        case _ =>
-          false
+    private def isReferredTempViewName(nameParts: Seq[String]): Boolean = {
+      AnalysisContext.get.referredTempViewNames.exists { n =>
+        (n.length == nameParts.length) && n.zip(nameParts).forall {
+          case (a, b) => resolver(a, b)
+        }
       }
     }
 
-    private def isIcebergTable(table: Table): Boolean = table match {
-      case _: SparkTable => true
-      case _ => false
+    private def lookupTempView(identifier: Seq[String]): 
Option[TemporaryViewRelation] = {
+      // 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
+      
SparkSession.active.sessionState.catalogManager.v1SessionCatalog.getRawLocalOrGlobalTempView(identifier)
+    }
+
+
+    private def lookupView(catalog: CatalogPlugin, ident: Identifier): 
Option[LogicalPlan] =
+      loadView(catalog, ident).map {
+        case view if CatalogV2Util.isSessionCatalog(catalog) =>

Review Comment:
   Why is there a different path for the session catalog? I think it should 
only matter that the view was a v2 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: issues-unsubscr...@iceberg.apache.org

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


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

Reply via email to