rdblue commented on code in PR #9421: URL: https://github.com/apache/iceberg/pull/9421#discussion_r1452824768
########## spark/v3.5/spark-extensions/src/main/scala/org/apache/spark/sql/catalyst/analysis/HijackViewCommands.scala: ########## @@ -0,0 +1,53 @@ +/* + * 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.plans.logical.DropView +import org.apache.spark.sql.catalyst.plans.logical.LogicalPlan +import org.apache.spark.sql.catalyst.plans.logical.views.DropIcebergView +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.LookupCatalog +import org.apache.spark.sql.connector.catalog.ViewCatalog + +/** + * ResolveSessionCatalog exits early for some v2 View commands, + * thus they are pre-substituted here and then handled in ResolveViews + */ +case class HijackViewCommands(spark: SparkSession) extends Rule[LogicalPlan] with LookupCatalog { + + protected lazy val catalogManager: CatalogManager = spark.sessionState.catalogManager + + override def apply(plan: LogicalPlan): LogicalPlan = plan.resolveOperatorsUp { + case DropView(UnresolvedIdentifier(nameParts, allowTemp), ifExists) + if isViewCatalog(catalogManager.currentCatalog) && !isTempView(nameParts) => Review Comment: I don't think this test is correct. The pattern using `UnresolvedIdentifier` will match any `DROP VIEW` plan and convert it to an Iceberg plan. The `isViewCatalog` check needs to test the catalog responsible for the view, not the current catalog. The current catalog check makes the v1 drop view test work when `USE spark_catalog` was called, but it would have the wrong behavior if an Iceberg catalog were the current. I think the logic should use `CatalogAndIdentifier` from `LookupCatalog` like Spark does in `ResolveCatalogs`: ```scala case d@DropView(UnresolvedIdentifier(nameParts, allowTemp), ifExists) if !isTempView(allowTemp, nameParts) => nameParts match { case CatalogAndIdentifier(catalog, ident) if isViewCatalog(catalog) => DropIcebergView(ResolvedIdentifier(catalog, ident), ifExists) case _ => d } ``` That way we know that the command is only replaced if a v2 `ViewCatalog` is responsible for it. You can also make this easier for the next time by creating a custom pattern with `unapply`: ```scala object ResolvedView { def unapply(unresolved: UnresolvedIdentifier): Option[ResolvedIdentifier] = unresolved match { case UnresolvedIdentifier(nameParts, true) if isTempView(nameParts) => None case UnresolvedIdentifier(CatalogAndIdentifier(catalog, ident), _) if isViewCatalog(catalog) => Some(ResolvedIdentifier(catalog, ident)) case _ => None } } ``` Then the rule is much simpler: ```scala override def apply(plan: LogicalPlan): LogicalPlan = plan.resolveOperatorsUp { case DropView(ResolvedView(resolved), ifExists) => DropIcebergView(resolved, ifExists) } ``` I've opened https://github.com/nastra/iceberg/pull/138 with these changes and test updates to catch this case by changing back to the test catalog. -- 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