nastra commented on code in PR #9332: URL: https://github.com/apache/iceberg/pull/9332#discussion_r1431095931
########## spark/v3.5/spark-extensions/src/main/scala/org/apache/spark/sql/catalyst/analysis/CreateViewAnalysis.scala: ########## @@ -0,0 +1,272 @@ +/* + * 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.FunctionIdentifier +import org.apache.spark.sql.catalyst.expressions.Alias +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.CreateView +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.Project +import org.apache.spark.sql.catalyst.plans.logical.views.CreateIcebergView +import org.apache.spark.sql.catalyst.plans.logical.views.CreateV2View +import org.apache.spark.sql.catalyst.plans.logical.views.ResolvedV2View +import org.apache.spark.sql.catalyst.rules.Rule +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.ViewCatalog +import org.apache.spark.sql.errors.QueryCompilationErrors +import org.apache.spark.sql.errors.QueryCompilationErrors.toSQLId +import org.apache.spark.sql.execution.CommandExecutionMode +import org.apache.spark.sql.execution.QueryExecution +import org.apache.spark.sql.internal.SQLConf +import org.apache.spark.sql.types.MetadataBuilder +import org.apache.spark.sql.util.SchemaUtils + +/** + * Resolve views in CREATE VIEW and ALTER VIEW AS plans and convert them to logical plans. + */ +case class CreateViewAnalysis(spark: SparkSession) + extends Rule[LogicalPlan] with LookupCatalog { + + protected lazy val catalogManager: CatalogManager = spark.sessionState.catalogManager + + import org.apache.spark.sql.connector.catalog.CatalogV2Implicits._ + + private lazy val isTempView = + (nameParts: Seq[String]) => catalogManager.v1SessionCatalog.isTempView(nameParts) + private lazy val isTemporaryFunction = catalogManager.v1SessionCatalog.isTemporaryFunction _ + + def asViewCatalog(plugin: CatalogPlugin): ViewCatalog = plugin match { + case viewCatalog: ViewCatalog => + viewCatalog + case _ => + throw QueryCompilationErrors.missingCatalogAbilityError(plugin, "views") + } + + def apply(plan: LogicalPlan): LogicalPlan = plan resolveOperators { + case CreateIcebergView(ResolvedIdentifier(catalog, nameParts), userSpecifiedColumns, comment, + properties, originalText, child, allowExisting, replace) => + convertCreateView( + catalog = asViewCatalog(catalog), + viewInfo = ViewInfo( + ident = nameParts, + userSpecifiedColumns = userSpecifiedColumns, + comment = comment, + properties = properties, + originalText = originalText), + child = child, + allowExisting = allowExisting, + replace = replace) + + case AlterViewAs(ResolvedV2View(catalog, ident, _), originalText, query) => + convertCreateView( + catalog = catalog, + viewInfo = ViewInfo( + ident = ident, + userSpecifiedColumns = Seq.empty, + comment = None, + properties = Map.empty, + originalText = Option(originalText)), + child = query, + allowExisting = false, + replace = true) + } + + private case class ViewInfo(ident: Identifier, + userSpecifiedColumns: Seq[(String, Option[String])], + comment: Option[String], + properties: Map[String, String], + originalText: Option[String]) Review Comment: I've fixed the indentation now -- 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