This is an automated email from the ASF dual-hosted git repository.

uros-b pushed a commit to branch branch-4.x
in repository https://gitbox.apache.org/repos/asf/spark.git


The following commit(s) were added to refs/heads/branch-4.x by this push:
     new 78ee667ddafe [SPARK-60505][SQL] Extract DROP VIEW lowering and V1 
command extractors from ResolveSessionCatalog
78ee667ddafe is described below

commit 78ee667ddafef5d859eb5764a7696fe346c05604
Author: Vladan Vasić <[email protected]>
AuthorDate: Mon Oct 26 09:06:25 2026 +0000

    [SPARK-60505][SQL] Extract DROP VIEW lowering and V1 command extractors 
from ResolveSessionCatalog
    
    ### What changes were proposed in this pull request?
    
    Jira: [SPARK-60505](https://issues.apache.org/jira/browse/SPARK-60505)
    
    Follow-up to SPARK-60422 (#59646). This moves more of 
`ResolveSessionCatalog` into shared helpers:
    
    - `v1CommandExtractors.scala` (Catalyst) now holds the V1 command target 
extractors that used to be members of `ResolveSessionCatalog`: 
`ResolvedViewIdentifier`, `ResolvedV1TableIdentifier`, 
`ResolvedV1TableIdentifierInSessionCatalog`, `ResolvedV1TableOrViewIdentifier`, 
`ResolvedIdentifierInSessionCatalog`, `ResolvedV1Database`, 
`ResolvedDatabaseInSessionCatalog` and `DatabaseNameInSessionCatalog`. 
`ResolvedV1CommandIdentifier` moves into the same file. Their names and bodies 
are unch [...]
    - `ViewCommandHelper.rewrite(dropView)` (sql/core) now does the 
session-catalog `DROP VIEW` lowering, and `ResolveSessionCatalog` delegates to 
it. The three existing cases keep their order. Any other `DropView` is returned 
as the same instance, so `ViewCatalog` targets still reach 
`DataSourceV2Strategy`.
    
    ### Why are the changes needed?
    
    `ResolveSessionCatalog` keeps its command lowering, and the extractors it 
uses, inside the rule, so other analyzer code can't reuse them. With the 
extractors moved out, every command lowering matches V1 targets through one 
shared place. Extracting the `DROP VIEW` lowering follows the `DropTableHelper` 
pattern from #59646: the single-pass resolver can lower `DROP VIEW` without 
duplicating the rule's logic. `ViewCommandHelper` is meant to take the other 
view commands as they get extracted.
    
    ### Does this PR introduce _any_ user-facing change?
    
    No. This is a refactor only: analyzed plans, errors and command behavior 
are unchanged.
    
    ### How was this patch tested?
    
    Existing tests, run with SBT. All passed:
    
    - `sql`: `PlanResolutionSuite`, v1/v2 `DropViewSuite` and `DropTableSuite`, 
`DataSourceV2MetadataViewSuite`, `GlobalTempViewSuite`, `SimpleSQLViewSuite`, 
`PersistedViewTestSuite`, `LocalTempViewTestSuite`, `GlobalTempViewTestSuite`, 
`MetricViewV2CatalogSuite`, `CreateTableAsSelectSuite`, `CreateTableLikeSuite`, 
and the v1/v2 namespace, SHOW, DESCRIBE, ALTER VIEW and function command suites.
    - `hive`: `org.apache.spark.sql.hive.execution.command.*` and 
`HiveDDLSuite`.
    - Scalastyle for `catalyst` and `sql`.
    
    ### Was this patch authored or co-authored using generative AI tooling?
    
    Generated-by: Claude Code (Claude Opus 5.5)
    
    This pull request and its description were written by Isaac.
    
    Closes #59746 from 
vladanvasi-db/effort/spark-resolve-session-catalog-helpers.
    
    Authored-by: Vladan Vasić <[email protected]>
    Signed-off-by: Uros Bojanic <[email protected]>
    (cherry picked from commit 9d66b43694bca637aab3b8cfcc08bf20ddb91b6a)
    Signed-off-by: Uros Bojanic <[email protected]>
---
 .../analysis/ResolvedV1CommandIdentifier.scala     |  32 -----
 .../catalyst/analysis/v1CommandExtractors.scala    | 160 +++++++++++++++++++++
 .../catalyst/analysis/ResolveSessionCatalog.scala  | 132 +----------------
 .../sql/catalyst/analysis/ViewCommandHelper.scala  |  66 +++++++++
 4 files changed, 228 insertions(+), 162 deletions(-)

diff --git 
a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/ResolvedV1CommandIdentifier.scala
 
b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/ResolvedV1CommandIdentifier.scala
deleted file mode 100644
index e46ee3d7adc7..000000000000
--- 
a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/ResolvedV1CommandIdentifier.scala
+++ /dev/null
@@ -1,32 +0,0 @@
-/*
- * 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.TableIdentifier
-import org.apache.spark.sql.catalyst.analysis.V1CommandUtils.supportsV1Command
-import org.apache.spark.sql.catalyst.plans.logical.LogicalPlan
-import org.apache.spark.sql.connector.catalog.CatalogV2Implicits._
-
-/** Extracts a V1 command identifier, rejecting namespaces that cannot be 
represented in V1. */
-object ResolvedV1CommandIdentifier {
-  def unapply(resolved: LogicalPlan): Option[TableIdentifier] = resolved match 
{
-    case ResolvedIdentifier(catalog, ident) if supportsV1Command(catalog) =>
-      Some(ident.asTableIdentifier.copy(catalog = Some(catalog.name)))
-    case _ => None
-  }
-}
diff --git 
a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/v1CommandExtractors.scala
 
b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/v1CommandExtractors.scala
new file mode 100644
index 000000000000..ce1a49f612bc
--- /dev/null
+++ 
b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/v1CommandExtractors.scala
@@ -0,0 +1,160 @@
+/*
+ * 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.TableIdentifier
+import org.apache.spark.sql.catalyst.analysis.V1CommandUtils.supportsV1Command
+import org.apache.spark.sql.catalyst.plans.logical.LogicalPlan
+import org.apache.spark.sql.connector.catalog.{CatalogManager, V1Table}
+import org.apache.spark.sql.connector.catalog.CatalogV2Implicits._
+import org.apache.spark.sql.connector.catalog.CatalogV2Util.isSessionCatalog
+import org.apache.spark.sql.errors.QueryCompilationErrors
+
+/**
+ * Extracts the V1 identifier of a resolved session-catalog persistent view or 
temporary view.
+ *
+ * Non-session-catalog persistent views produced for `DelegatingTable` are 
handled by dedicated
+ * V2 cases in `DataSourceV2Strategy`. ALTER VIEW AS, SET/UNSET TBLPROPERTIES, 
ALTER VIEW WITH
+ * SCHEMA, RENAME TO, SHOW CREATE TABLE, SHOW TBLPROPERTIES, SHOW COLUMNS and 
DESCRIBE [COLUMN]
+ * use `ResolvedPersistentView.info` directly.
+ *
+ * Temporary views preserve their database so ALTER VIEW and DROP VIEW can 
find global temporary
+ * views. Local temporary views use only the table name. Session-qualified 
temporary views use
+ * the `session` database so `SessionCatalog` resolves them as temporary views.
+ */
+object ResolvedViewIdentifier {
+  def unapply(resolved: LogicalPlan): Option[TableIdentifier] = resolved match 
{
+    case ResolvedPersistentView(catalog, ident, _) if 
isSessionCatalog(catalog) =>
+      Some(ident.asTableIdentifier.copy(catalog = Some(catalog.name)))
+
+    case ResolvedTempView(ident, _) =>
+      val db = 
CatalogManager.databaseForSessionQualifiedViewIdentifier(ident.namespace().toSeq)
+      Some(TableIdentifier(ident.name(), db))
+
+    case _ => None
+  }
+}
+
+/** Extracts the identifier of a resolved [[V1Table]] in a catalog that 
supports V1 commands. */
+object ResolvedV1TableIdentifier {
+  def unapply(resolved: LogicalPlan): Option[TableIdentifier] = resolved match 
{
+    case ResolvedTable(catalog, _, t: V1Table, _) if 
supportsV1Command(catalog) =>
+      Some(t.catalogTable.identifier)
+    case _ => None
+  }
+}
+
+/** Extracts the identifier of a resolved [[V1Table]] in the session catalog. 
*/
+object ResolvedV1TableIdentifierInSessionCatalog {
+  def unapply(resolved: LogicalPlan): Option[TableIdentifier] = resolved match 
{
+    case ResolvedTable(catalog, _, t: V1Table, _) if isSessionCatalog(catalog) 
=>
+      Some(t.catalogTable.identifier)
+    case _ => None
+  }
+}
+
+/** Extracts the V1 identifier of a [[ResolvedV1TableIdentifier]] or 
[[ResolvedViewIdentifier]]. */
+object ResolvedV1TableOrViewIdentifier {
+  def unapply(resolved: LogicalPlan): Option[TableIdentifier] = resolved match 
{
+    case ResolvedV1TableIdentifier(ident) => Some(ident)
+    case ResolvedViewIdentifier(ident) => Some(ident)
+    case _ => None
+  }
+}
+
+/** Extracts a V1 command identifier, rejecting namespaces that cannot be 
represented in V1. */
+object ResolvedV1CommandIdentifier {
+  def unapply(resolved: LogicalPlan): Option[TableIdentifier] = resolved match 
{
+    case ResolvedIdentifier(catalog, ident) if supportsV1Command(catalog) =>
+      Some(ident.asTableIdentifier.copy(catalog = Some(catalog.name)))
+    case _ => None
+  }
+}
+
+/**
+ * Extractor for resolved identifiers in the session catalog.
+ * Rejects multi-part namespaces and builtin namespace targets with 
appropriate errors.
+ * Errors requiring a single-part namespace include the full identifier 
(namespace and name).
+ *
+ * @param statement the SQL statement (e.g. "CREATE", "DROP") for error 
messages
+ * @param objectType the object type (e.g. "FUNCTION", "VIEW") for error 
messages
+ */
+class ResolvedIdentifierInSessionCatalog(statement: String, objectType: 
String) {
+  def unapply(resolved: LogicalPlan): Option[TableIdentifier] = resolved match 
{
+    case ResolvedIdentifier(catalog, ident) if isSessionCatalog(catalog) =>
+      if (ident.namespace().length != 1) {
+        if (ident.namespace().length >= 1 &&
+            
ident.namespace().last.equalsIgnoreCase(CatalogManager.BUILTIN_NAMESPACE)) {
+          throw 
QueryCompilationErrors.operationNotAllowedOnBuiltinNamespaceError(
+            statement, objectType, ident.name())
+        }
+        throw QueryCompilationErrors
+          .requiresSinglePartNamespaceError(ident.namespace().toSeq :+ 
ident.name())
+      }
+      Some(TableIdentifier(ident.name(), Some(ident.namespace().head), 
Some(catalog.name)))
+    case _ => None
+  }
+}
+
+/**
+ * Extracts the database name of a resolved namespace in a catalog that 
supports V1 commands.
+ * Fails if the namespace is empty or has more than one part.
+ */
+object ResolvedV1Database {
+  def unapply(resolved: ResolvedNamespace): Option[String] = resolved match {
+    case ResolvedNamespace(catalog, _, _) if !supportsV1Command(catalog) => 
None
+    case ResolvedNamespace(_, Seq(), _) =>
+      throw 
QueryCompilationErrors.databaseFromV1SessionCatalogNotSpecifiedError()
+    case ResolvedNamespace(_, Seq(dbName), _) => Some(dbName)
+    case _ =>
+      assert(resolved.namespace.length > 1)
+      throw 
QueryCompilationErrors.nestedDatabaseUnsupportedByV1SessionCatalogError(
+        resolved.namespace)
+  }
+}
+
+/**
+ * Extracts the database name of a resolved namespace in the session catalog, 
for commands that do
+ * not have a v2 implementation. Fails if the namespace is empty or has more 
than one part.
+ */
+object ResolvedDatabaseInSessionCatalog {
+  def unapply(resolved: ResolvedNamespace): Option[String] = resolved match {
+    case ResolvedNamespace(catalog, _, _) if !isSessionCatalog(catalog) => None
+    case ResolvedNamespace(_, Seq(), _) =>
+      throw 
QueryCompilationErrors.databaseFromV1SessionCatalogNotSpecifiedError()
+    case ResolvedNamespace(_, Seq(dbName), _) => Some(dbName)
+    case _ =>
+      assert(resolved.namespace.length > 1)
+      throw 
QueryCompilationErrors.nestedDatabaseUnsupportedByV1SessionCatalogError(
+        resolved.namespace)
+  }
+}
+
+/**
+ * Extracts the name of a database to create in a catalog that supports V1 
commands. Fails if the
+ * namespace does not have exactly one part.
+ */
+object DatabaseNameInSessionCatalog {
+  def unapply(resolved: ResolvedNamespace): Option[String] = resolved match {
+    case ResolvedNamespace(catalog, _, _) if !supportsV1Command(catalog) => 
None
+    case ResolvedNamespace(_, Seq(dbName), _) => Some(dbName)
+    case _ =>
+      assert(resolved.namespace.length > 1)
+      throw 
QueryCompilationErrors.requiresSinglePartNamespaceError(resolved.namespace)
+  }
+}
diff --git 
a/sql/core/src/main/scala/org/apache/spark/sql/catalyst/analysis/ResolveSessionCatalog.scala
 
b/sql/core/src/main/scala/org/apache/spark/sql/catalyst/analysis/ResolveSessionCatalog.scala
index 7f4d824dd8c4..a037a2dadc64 100644
--- 
a/sql/core/src/main/scala/org/apache/spark/sql/catalyst/analysis/ResolveSessionCatalog.scala
+++ 
b/sql/core/src/main/scala/org/apache/spark/sql/catalyst/analysis/ResolveSessionCatalog.scala
@@ -356,25 +356,8 @@ class ResolveSessionCatalog(val catalogManager: 
CatalogManager)
     case dropTable @ DropTable(_: ResolvedIdentifier, _, _) =>
       DropTableHelper.rewrite(dropTable)
 
-    // Session-qualified temp view: same execution path as FakeSystemCatalog 
DROP VIEW.
-    case DropView(ResolvedTempView(ident, _), ifExists) =>
-      DropTempViewCommand(ident, ifExists)
-
-    case DropView(DropViewInSessionCatalog(ident), ifExists) =>
-      DropTableCommand(ident, ifExists, isView = true, purge = false)
-
-    // ViewCatalog catalogs fall through to `DataSourceV2Strategy`, which 
routes DROP VIEW to
-    // `ViewCatalog.dropView` (this also covers METRIC_VIEW since metric views 
are persisted
-    // through the same ViewCatalog interface). Other non-session catalogs get
-    // `MISSING_CATALOG_ABILITY.VIEWS`, matching the error raised from 
`CheckViewReferences` for
-    // CREATE/ALTER VIEW and from the analyzer gate on UnresolvedView.
-    case DropView(r @ ResolvedIdentifier(catalog, ident), ifExists)
-        if !catalog.isInstanceOf[ViewCatalog] =>
-      if (catalog == FakeSystemCatalog) {
-        DropTempViewCommand(ident, ifExists)
-      } else {
-        throw QueryCompilationErrors.missingCatalogViewsAbilityError(catalog)
-      }
+    case dropView: DropView =>
+      ViewCommandHelper.rewrite(dropView)
 
     case c @ CreateNamespace(DatabaseNameInSessionCatalog(name), _, _) if 
conf.useV1Command =>
       val comment = c.properties.get(SupportsNamespaces.PROP_COMMENT)
@@ -843,85 +826,11 @@ class ResolveSessionCatalog(val catalogManager: 
CatalogManager)
     )
   }
 
-  object ResolvedViewIdentifier {
-    // Only matches session-catalog persistent views. Non-session-catalog 
persistent views
-    // (produced for `DelegatingTable`) fall through and are picked up by 
dedicated v2 strategy
-    // cases in `DataSourceV2Strategy` -- AlterViewAs, SET/UNSET 
TBLPROPERTIES, ALTER VIEW ...
-    // WITH SCHEMA, RENAME TO, SHOW CREATE TABLE, SHOW TBLPROPERTIES, SHOW 
COLUMNS, DESCRIBE
-    // [COLUMN] all dispatch to v2 view execs that consume 
`ResolvedPersistentView.info`
-    // directly.
-    def unapply(resolved: LogicalPlan): Option[TableIdentifier] = resolved 
match {
-      case ResolvedPersistentView(catalog, ident, _) if 
isSessionCatalog(catalog) =>
-        Some(ident.asTableIdentifier.copy(catalog = Some(catalog.name)))
-
-      case ResolvedTempView(ident, _) =>
-        // Global temp views have a namespace (e.g. global_temp); preserve it 
so ALTER VIEW
-        // and DROP VIEW find the view. Local temp views are keyed by table 
name only.
-        // system.session.viewName -> use database "session" so SessionCatalog 
resolves
-        // it as a temp view.
-        val db = 
CatalogManager.databaseForSessionQualifiedViewIdentifier(ident.namespace().toSeq)
-        Some(TableIdentifier(ident.name(), db))
-
-      case _ => None
-    }
-  }
-
-  object ResolvedV1TableIdentifier {
-    def unapply(resolved: LogicalPlan): Option[TableIdentifier] = resolved 
match {
-      case ResolvedTable(catalog, _, t: V1Table, _) if 
supportsV1Command(catalog) =>
-        Some(t.catalogTable.identifier)
-      case _ => None
-    }
-  }
-
-  object ResolvedV1TableIdentifierInSessionCatalog {
-    def unapply(resolved: LogicalPlan): Option[TableIdentifier] = resolved 
match {
-      case ResolvedTable(catalog, _, t: V1Table, _) if 
isSessionCatalog(catalog) =>
-        Some(t.catalogTable.identifier)
-      case _ => None
-    }
-  }
-
-  object ResolvedV1TableOrViewIdentifier {
-    def unapply(resolved: LogicalPlan): Option[TableIdentifier] = resolved 
match {
-      case ResolvedV1TableIdentifier(ident) => Some(ident)
-      case ResolvedViewIdentifier(ident) => Some(ident)
-      case _ => None
-    }
-  }
-
   private object CreateViewInSessionCatalog
     extends ResolvedIdentifierInSessionCatalog("CREATE", "VIEW")
-  private object DropViewInSessionCatalog
-    extends ResolvedIdentifierInSessionCatalog("DROP", "VIEW")
   private object CreateFunctionInSessionCatalog
     extends ResolvedIdentifierInSessionCatalog("CREATE", "FUNCTION")
 
-  /**
-   * Extractor for resolved identifiers in the session catalog.
-   * Rejects multi-part namespaces and builtin namespace targets with 
appropriate errors.
-   *
-   * @param statement the SQL statement (e.g. "CREATE", "DROP") for error 
messages
-   * @param objectType the object type (e.g. "FUNCTION", "VIEW") for error 
messages
-   */
-  class ResolvedIdentifierInSessionCatalog(statement: String, objectType: 
String) {
-    def unapply(resolved: LogicalPlan): Option[TableIdentifier] = resolved 
match {
-      case ResolvedIdentifier(catalog, ident) if isSessionCatalog(catalog) =>
-        if (ident.namespace().length != 1) {
-          if (ident.namespace().length >= 1 &&
-              
ident.namespace().last.equalsIgnoreCase(CatalogManager.BUILTIN_NAMESPACE)) {
-            throw 
QueryCompilationErrors.operationNotAllowedOnBuiltinNamespaceError(
-              statement, objectType, ident.name())
-          }
-          // Pass full identifier (namespace + name) so error shows e.g. 
"`a`.`b`.`c`"
-          throw QueryCompilationErrors
-            .requiresSinglePartNamespaceError(ident.namespace().toSeq :+ 
ident.name())
-        }
-        Some(TableIdentifier(ident.name(), Some(ident.namespace().head), 
Some(catalog.name)))
-      case _ => None
-    }
-  }
-
   private def isV2Provider(provider: String): Boolean = {
     DataSourceV2Utils.getTableProvider(provider, conf).isDefined
   }
@@ -965,43 +874,6 @@ class ResolveSessionCatalog(val catalogManager: 
CatalogManager)
     constantFolded.sql
   }
 
-  private object ResolvedV1Database {
-    def unapply(resolved: ResolvedNamespace): Option[String] = resolved match {
-      case ResolvedNamespace(catalog, _, _) if !supportsV1Command(catalog) => 
None
-      case ResolvedNamespace(_, Seq(), _) =>
-        throw 
QueryCompilationErrors.databaseFromV1SessionCatalogNotSpecifiedError()
-      case ResolvedNamespace(_, Seq(dbName), _) => Some(dbName)
-      case _ =>
-        assert(resolved.namespace.length > 1)
-        throw 
QueryCompilationErrors.nestedDatabaseUnsupportedByV1SessionCatalogError(
-          resolved.namespace)
-    }
-  }
-
-  // Use this object to help match commands that do not have a v2 
implementation.
-  private object ResolvedDatabaseInSessionCatalog {
-    def unapply(resolved: ResolvedNamespace): Option[String] = resolved match {
-      case ResolvedNamespace(catalog, _, _) if !isSessionCatalog(catalog) => 
None
-      case ResolvedNamespace(_, Seq(), _) =>
-        throw 
QueryCompilationErrors.databaseFromV1SessionCatalogNotSpecifiedError()
-      case ResolvedNamespace(_, Seq(dbName), _) => Some(dbName)
-      case _ =>
-        assert(resolved.namespace.length > 1)
-        throw 
QueryCompilationErrors.nestedDatabaseUnsupportedByV1SessionCatalogError(
-          resolved.namespace)
-    }
-  }
-
-  private object DatabaseNameInSessionCatalog {
-    def unapply(resolved: ResolvedNamespace): Option[String] = resolved match {
-      case ResolvedNamespace(catalog, _, _) if !supportsV1Command(catalog) => 
None
-      case ResolvedNamespace(_, Seq(dbName), _) => Some(dbName)
-      case _ =>
-        assert(resolved.namespace.length > 1)
-        throw 
QueryCompilationErrors.requiresSinglePartNamespaceError(resolved.namespace)
-    }
-  }
-
   // True when the ALTER COLUMN specs only change comments (set or drop, no 
type / nullability /
   // position / default changes) and, taken together, touch multiple columns 
or a nested field.
   // Such changes come from `COMMENT ON TABLE ... COLUMN (...)` and cannot be 
expressed by the
diff --git 
a/sql/core/src/main/scala/org/apache/spark/sql/catalyst/analysis/ViewCommandHelper.scala
 
b/sql/core/src/main/scala/org/apache/spark/sql/catalyst/analysis/ViewCommandHelper.scala
new file mode 100644
index 000000000000..ee65c8b88ef6
--- /dev/null
+++ 
b/sql/core/src/main/scala/org/apache/spark/sql/catalyst/analysis/ViewCommandHelper.scala
@@ -0,0 +1,66 @@
+/*
+ * 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.plans.logical.{DropView, LogicalPlan}
+import org.apache.spark.sql.connector.catalog.ViewCatalog
+import org.apache.spark.sql.errors.QueryCompilationErrors
+import org.apache.spark.sql.execution.command.{DropTableCommand, 
DropTempViewCommand}
+
+/**
+ * Rewrites resolved view commands to their session-catalog commands in 
[[ResolveSessionCatalog]].
+ * Views in a non-session [[ViewCatalog]] keep the V2 command.
+ */
+object ViewCommandHelper {
+
+  /**
+   * Rewrites a resolved [[DropView]] to [[DropTempViewCommand]] for a 
temporary view or to
+   * [[DropTableCommand]] for a session-catalog view, matching temporary views 
first. Fails if a
+   * session-catalog target's namespace does not have exactly one part.
+   *
+   * For remaining resolved targets outside a [[ViewCatalog]], 
[[FakeSystemCatalog]] uses
+   * [[DropTempViewCommand]], sharing the execution path for session-qualified 
temporary views.
+   * Other catalogs fail with `MISSING_CATALOG_ABILITY.VIEWS`, matching 
`CheckViewReferences` for
+   * CREATE/ALTER VIEW and the analyzer gate on [[UnresolvedView]].
+   *
+   * Returns `dropView` itself for non-session [[ViewCatalog]] targets or 
unresolved targets.
+   * For non-session view catalogs, `DataSourceV2Strategy` routes DROP VIEW to
+   * `ViewCatalog.dropView`, including metric views persisted through the same 
interface.
+   */
+  def rewrite(dropView: DropView): LogicalPlan = dropView match {
+    case DropView(ResolvedTempView(ident, _), ifExists) =>
+      DropTempViewCommand(ident, ifExists)
+
+    case DropView(DropViewInSessionCatalog(ident), ifExists) =>
+      DropTableCommand(ident, ifExists, isView = true, purge = false)
+
+    case DropView(ResolvedIdentifier(catalog, ident), ifExists)
+        if !catalog.isInstanceOf[ViewCatalog] =>
+      if (catalog == FakeSystemCatalog) {
+        DropTempViewCommand(ident, ifExists)
+      } else {
+        throw QueryCompilationErrors.missingCatalogViewsAbilityError(catalog)
+      }
+
+    case _ =>
+      dropView
+  }
+
+  private object DropViewInSessionCatalog
+    extends ResolvedIdentifierInSessionCatalog("DROP", "VIEW")
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to