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


##########
spark/v3.5/spark-extensions/src/main/scala/org/apache/spark/sql/catalyst/analysis/ResolveViews.scala:
##########
@@ -0,0 +1,105 @@
+/*
+ * 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.SparkSession
+import org.apache.spark.sql.catalyst.parser.ParseException
+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
+
+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(Seq(nameParts.asIdentifier.name())) 
=>
+      u
+
+    case u@UnresolvedRelation(parts@CatalogAndIdentifier(catalog, ident), _, 
_) =>
+      loadView(catalog, ident)
+        .map(createViewRelation(parts.quoted, _))
+        .getOrElse(u)
+  }
+
+  def loadView(catalog: CatalogPlugin, ident: Identifier): Option[View] = 
catalog match {
+    case viewCatalog: ViewCatalog =>
+      try {
+        Option(viewCatalog.loadView(ident))
+      } catch {
+        case _: NoSuchViewException => None
+      }
+    case _ => None
+  }
+
+  private def createViewRelation(name: String, view: V2View): LogicalPlan = {
+    val child = parseViewText(name, view.query)
+    val desc = V2ViewDescription(name, view)
+    val qualifiedChild = desc.viewCatalogAndNamespace match {
+      case Seq() =>
+        // Views from Spark 2.2 or prior do not store catalog or namespace,
+        // however its sql text should already be fully qualified.
+        child
+      case catalogAndNamespace =>
+        // Substitute CTEs within the view before qualifying table identifiers
+        qualifyTableIdentifiers(CTESubstitution.apply(child), 
catalogAndNamespace)
+    }
+
+    // The relation is a view, so we wrap the relation by:
+    // 1. Add a [[View]] operator over the relation to keep track of the view 
desc;

Review Comment:
   I don't understand why we would do this. I don't see a big benefit to having 
`IcebergView` as a node in the logical plan and it creates risk that 
`IcebergView` doesn't correctly pass something through in the analysis phase. I 
think we should skip the `IcebergView` part and just add the `SubqueryAlias`.
   
   @aokolnychyi might want to look at this as well.



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