{"record":{"id":"0ce4a5879314aae8","repo":"apache/iceberg","slug":"alter-view-viewname-as-is-not-supported-use-cre-0ce4a5","errorCode":null,"errorMessage":"ALTER VIEW <viewName> AS is not supported. Use CREATE OR REPLACE VIEW instead","messagePattern":"ALTER VIEW <viewName> AS is not supported\\. Use CREATE OR REPLACE VIEW instead","errorType":"exception","errorClass":"IcebergAnalysisException","httpStatus":null,"severity":"error","filePath":"spark/v4.2/spark-extensions/src/main/scala/org/apache/spark/sql/catalyst/analysis/CheckViews.scala","lineNumber":61,"sourceCode":"          case resolvedIdent @ ResolvedIdentifier(_: ViewCatalog, _) =>\n            val viewIdent: Seq[String] =\n              resolvedIdent.catalog.name() +: resolvedIdent.identifier.asMultipartIdentifier\n            ViewHelper.verifyTemporaryObjectsNotExists(\n              isTemporary = false,\n              viewIdent,\n              c.query,\n              c.referredTempFunctions)\n            ViewHelper.verifyAutoGeneratedAliasesNotExists(c.query, isTemporary = false, viewIdent)\n            verifyColumnCount(resolvedIdent, c.columnAliases, c.query)\n            if (c.replace) {\n              checkCyclicViewReference(viewIdent, c.query, Seq(viewIdent))\n            }\n\n          case _ => // OK\n        }\n\n      case AlterViewAs(ResolvedV2View(_, _, _), _, _, _, _) =>\n        throw new IcebergAnalysisException(\n          \"ALTER VIEW <viewName> AS is not supported. Use CREATE OR REPLACE VIEW instead\")\n\n      case _ => // OK\n    }\n  }\n\n  private def verifyColumnCount(\n      ident: ResolvedIdentifier,\n      columns: Seq[String],\n      query: LogicalPlan): Unit = {\n    if (columns.nonEmpty) {\n      val viewNameParts = ident.catalog.name() +: ident.identifier.asMultipartIdentifier\n      if (columns.length > query.output.length) {\n        throw QueryCompilationErrors.cannotCreateViewNotEnoughColumnsError(\n          viewNameParts,\n          columns,\n          query)\n      } else if (columns.length < query.output.length) {","sourceCodeStart":43,"sourceCodeEnd":79,"githubUrl":"https://github.com/apache/iceberg/blob/86d9c8fc543e7c56c9f624eb725f76c9baff9570/spark/v4.2/spark-extensions/src/main/scala/org/apache/spark/sql/catalyst/analysis/CheckViews.scala#L43-L79","documentation":"The Iceberg view extensions do not implement ALTER VIEW ... AS for Iceberg (v2) views; the CheckViews analysis rule intercepts AlterViewAs on ResolvedV2View and fails fast with this exception. Iceberg views are redefined with CREATE OR REPLACE VIEW instead of being altered in place.","triggerScenarios":"Running `ALTER VIEW <iceberg_view_name> AS <query>` where the view resolves to an Iceberg V2 view (created through the Iceberg catalog with view support enabled).","commonSituations":"Porting scripts written for Spark's built-in views (where ALTER VIEW AS works) to Iceberg views; migration tooling that assumes ALTER VIEW semantics across catalogs.","solutions":["Replace the statement with CREATE OR REPLACE VIEW <viewName> AS <query>.","If you only need to change the definition occasionally, drop and recreate the view with CREATE OR REPLACE in one idempotent statement.","Keep ALTER VIEW usage limited to non-Iceberg (V1/temp) views where Spark supports it."],"exampleFix":"-- before\nALTER VIEW my_view AS SELECT id, ts FROM my_table WHERE ts > '2024-01-01'\n-- after\nCREATE OR REPLACE VIEW my_view AS SELECT id, ts FROM my_table WHERE ts > '2024-01-01'","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  spark.sql(s\"ALTER VIEW $name AS $query\")\n} catch {\n  case e: IcebergAnalysisException if e.getMessage.contains(\"ALTER VIEW\") =>\n    spark.sql(s\"CREATE OR REPLACE VIEW $name AS $query\")\n}","preventionTips":["Use CREATE OR REPLACE VIEW for Iceberg views everywhere","Keep ALTER VIEW only in scripts targeting Spark V1 temp views","Grep migration scripts for 'ALTER VIEW' before pointing them at Iceberg catalogs","Document view lifecycle in your team's DDL conventions"],"tags":["spark","iceberg-views","sql-dialect"],"backgroundTag":"unsupported-operation","analyzedSha":"86d9c8fc543e7c56c9f624eb725f76c9baff9570","analyzedAt":"2026-09-12T00:46:39.097Z","contentChangedAt":"2026-09-12T00:46:39.097Z","schemaVersion":2},"datasetVersion":"2026-09-14T21:17:11.552Z"}