manuzhang commented on code in PR #14984:
URL: https://github.com/apache/iceberg/pull/14984#discussion_r3809816459


##########
spark/v4.2/spark-extensions/src/main/scala/org/apache/spark/sql/catalyst/analysis/CheckViews.scala:
##########
@@ -0,0 +1,122 @@
+/*
+ * 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.catalyst.expressions.SubqueryExpression
+import org.apache.spark.sql.catalyst.plans.logical.AlterViewAs
+import org.apache.spark.sql.catalyst.plans.logical.LogicalPlan
+import org.apache.spark.sql.catalyst.plans.logical.View
+import org.apache.spark.sql.catalyst.plans.logical.views.CreateIcebergView
+import org.apache.spark.sql.catalyst.plans.logical.views.ResolvedV2View
+import org.apache.spark.sql.connector.catalog.ViewCatalog
+import org.apache.spark.sql.errors.QueryCompilationErrors
+import org.apache.spark.sql.execution.command.ViewHelper
+
+object CheckViews extends (LogicalPlan => Unit) {
+
+  import org.apache.spark.sql.connector.catalog.CatalogV2Implicits._
+
+  override def apply(plan: LogicalPlan): Unit = {
+    plan foreach {
+      // RewriteViewCommands replaces Spark's CreateView before 
CheckViewReferences runs, so apply
+      // Spark's shared post-analysis checks to the Iceberg node here. Spark 
expands referenced
+      // views into logical View nodes, which are also traversed below for 
cycle detection. Spark's
+      // V2CreateViewPreparation still checks duplicate query column names 
during execution.
+      case c: CreateIcebergView if c.isAnalyzed =>
+        c.child match {
+          case resolvedIdent @ ResolvedIdentifier(_: ViewCatalog, _) =>
+            val viewIdent: Seq[String] =
+              resolvedIdent.catalog.name() +: 
resolvedIdent.identifier.asMultipartIdentifier
+            ViewHelper.verifyTemporaryObjectsNotExists(
+              isTemporary = false,
+              viewIdent,
+              c.query,
+              c.referredTempFunctions)
+            ViewHelper.verifyAutoGeneratedAliasesNotExists(c.query, 
isTemporary = false, viewIdent)
+            verifyColumnCount(resolvedIdent, c.columnAliases, c.query)
+            if (c.replace) {
+              checkCyclicViewReference(viewIdent, c.query, Seq(viewIdent))
+            }
+
+          case _ => // OK
+        }
+
+      case AlterViewAs(ResolvedV2View(_, _, _), _, _, _, _) =>
+        throw new IcebergAnalysisException(
+          "ALTER VIEW <viewName> AS is not supported. Use CREATE OR REPLACE 
VIEW instead")
+
+      case _ => // OK
+    }
+  }
+
+  private def verifyColumnCount(
+      ident: ResolvedIdentifier,
+      columns: Seq[String],
+      query: LogicalPlan): Unit = {
+    if (columns.nonEmpty) {
+      val viewNameParts = ident.catalog.name() +: 
ident.identifier.asMultipartIdentifier
+      if (columns.length > query.output.length) {
+        throw QueryCompilationErrors.cannotCreateViewNotEnoughColumnsError(
+          viewNameParts,
+          columns,
+          query)
+      } else if (columns.length < query.output.length) {
+        throw QueryCompilationErrors.cannotCreateViewTooManyColumnsError(
+          viewNameParts,
+          columns,
+          query)
+      }
+    }
+  }
+
+  private def checkCyclicViewReference(

Review Comment:
   Added comments to explain why this is needed.



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