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


##########
spark/v3.5/spark-extensions/src/main/scala/org/apache/spark/sql/catalyst/analysis/ResolveViews.scala:
##########
@@ -0,0 +1,156 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.spark.sql.catalyst.analysis
+
+import org.apache.spark.sql.AnalysisException
+import org.apache.spark.sql.SparkSession
+import org.apache.spark.sql.catalyst.analysis.SimpleAnalyzer.checkAnalysis
+import org.apache.spark.sql.catalyst.analysis.SimpleAnalyzer.execute
+import org.apache.spark.sql.catalyst.parser.ParseException
+import org.apache.spark.sql.catalyst.plans.logical.CatalogTableViewDescription
+import org.apache.spark.sql.catalyst.plans.logical.IcebergView
+import org.apache.spark.sql.catalyst.plans.logical.LogicalPlan
+import org.apache.spark.sql.catalyst.plans.logical.SubqueryAlias
+import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.connector.catalog.{View => V2View}
+import org.apache.spark.sql.connector.catalog.CatalogManager
+import org.apache.spark.sql.connector.catalog.CatalogPlugin
+import org.apache.spark.sql.connector.catalog.Identifier
+import org.apache.spark.sql.connector.catalog.LookupCatalog
+import org.apache.spark.sql.connector.catalog.View
+import org.apache.spark.sql.connector.catalog.ViewCatalog
+import org.apache.spark.sql.errors.QueryCompilationErrors
+import org.apache.spark.sql.internal.SQLConf
+
+case class ResolveViews(spark: SparkSession) extends Rule[LogicalPlan] with 
LookupCatalog {
+
+  import org.apache.spark.sql.connector.catalog.CatalogV2Implicits._
+
+  protected lazy val catalogManager: CatalogManager = 
spark.sessionState.catalogManager
+
+  override def apply(plan: LogicalPlan): LogicalPlan = plan resolveOperators {
+    case u@UnresolvedRelation(nameParts, _, _) if 
catalogManager.v1SessionCatalog.isTempView(nameParts) =>
+      u
+
+    case u@UnresolvedRelation(
+    parts@NonSessionCatalogAndIdentifier(catalog, ident), _, _) if 
!isSQLOnFile(parts) =>
+      loadView(catalog, ident)
+        .map(createViewRelation(parts.quoted, _))
+        .getOrElse(u)
+
+    case p@SubqueryAlias(_, view: IcebergView) =>
+      p.copy(child = resolveViews(view))
+
+  }
+
+  def loadView(catalog: CatalogPlugin, ident: Identifier): Option[View] = 
catalog match {
+    case viewCatalog: ViewCatalog =>
+      try {
+        Option(viewCatalog.loadView(ident))
+      } catch {
+        case _: NoSuchViewException => None
+      }
+    case _ => None
+  }
+
+  // The current catalog and namespace may be different from when the view was 
created, we must
+  // resolve the view logical plan here, with the catalog and namespace stored 
in view metadata.
+  // This is done by keeping the catalog and namespace in `AnalysisContext`, 
and analyzer will
+  // look at `AnalysisContext.catalogAndNamespace` when resolving relations 
with single-part name.
+  // If `AnalysisContext.catalogAndNamespace` is non-empty, analyzer will 
expand single-part names
+  // with it, instead of current catalog and namespace.
+  def resolveViews(plan: LogicalPlan): LogicalPlan = plan match {
+    // The view's child should be a logical plan parsed from the 
`desc.viewText`, the variable
+    // `viewText` should be defined, or else we throw an error on the 
generation of the View
+    // operator.
+    case view@IcebergView(CatalogTableViewDescription(desc), isTempView, 
child) if !child.resolved =>
+      // Resolve all the UnresolvedRelations and Views in the child.
+      val newChild = AnalysisContext.withAnalysisContext(desc) {
+        val nestedViewDepth = AnalysisContext.get.nestedViewDepth
+        val maxNestedViewDepth = AnalysisContext.get.maxNestedViewDepth
+        if (nestedViewDepth > maxNestedViewDepth) {
+          throw QueryCompilationErrors.viewDepthExceedsMaxResolutionDepthError(
+            desc.identifier, maxNestedViewDepth, view)
+        }
+        
SQLConf.withExistingConf(IcebergView.effectiveSQLConf(desc.viewSQLConfigs, 
isTempView)) {
+          execute(child)
+        }
+      }
+      // Fail the analysis eagerly because outside AnalysisContext, the 
unresolved operators
+      // inside a view maybe resolved incorrectly.
+      checkAnalysis(newChild)
+      view.copy(child = newChild)
+    case p@SubqueryAlias(_, view: IcebergView) =>
+      p.copy(child = resolveViews(view))

Review Comment:
   Rules should not make recursive calls. If the rule needs to be applied more 
than once, it should happen by executing the rule multiple times.



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