{"record":{"id":"79dc5151cdbd7d27","repo":"apache/iceberg","slug":"alter-view-viewname-as-is-not-supported-use-cre","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":"AnalysisException","httpStatus":null,"severity":"error","filePath":"spark/v3.5/spark-extensions/src/main/scala/org/apache/spark/sql/catalyst/analysis/CheckViews.scala","lineNumber":64,"sourceCode":"            _,\n            _,\n            _,\n            _,\n            replace,\n            _,\n            _) =>\n        verifyColumnCount(resolvedIdent, columnAliases, query)\n        SchemaUtils.checkColumnNameDuplication(\n          query.schema.fieldNames.toIndexedSeq,\n          SQLConf.get.resolver)\n        if (replace) {\n          val viewIdent: Seq[String] =\n            resolvedIdent.catalog.name() +: resolvedIdent.identifier.asMultipartIdentifier\n          checkCyclicViewReference(viewIdent, query, Seq(viewIdent))\n        }\n\n      case AlterViewAs(ResolvedV2View(_, _), _, _) =>\n        throw new AnalysisException(\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      if (columns.length > query.output.length) {\n        throw new AnalysisException(\n          errorClass = \"CREATE_VIEW_COLUMN_ARITY_MISMATCH.NOT_ENOUGH_DATA_COLUMNS\",\n          messageParameters = Map(\n            \"viewName\" -> String.format(\"%s.%s\", ident.catalog.name(), ident.identifier),\n            \"viewColumns\" -> columns.mkString(\", \"),\n            \"dataColumns\" -> query.output.map(c => c.name).mkString(\", \")))","sourceCodeStart":46,"sourceCodeEnd":82,"githubUrl":"https://github.com/apache/iceberg/blob/86d9c8fc543e7c56c9f624eb725f76c9baff9570/spark/v3.5/spark-extensions/src/main/scala/org/apache/spark/sql/catalyst/analysis/CheckViews.scala#L46-L82","documentation":"Iceberg's v3.5 Spark-extensions view checker rejects ALTER VIEW ... AS for Iceberg views (ResolvedV2View). Iceberg views do not support in-place query replacement via ALTER VIEW; the SQL dialect requires CREATE OR REPLACE VIEW to change a view's definition, so an AnalysisException is thrown during analysis.","triggerScenarios":"Executing `ALTER VIEW catalog.db.view AS SELECT ...` where the view is an Iceberg view registered through the Iceberg Spark catalog (matched by AlterViewAs(ResolvedV2View(_, _), _, _) in CheckViews.checkViews).","commonSituations":"Migrating scripts from built-in Spark views (where ALTER VIEW AS works) to Iceberg views; ORM/migration tools emitting ALTER VIEW; refactoring view SQL that previously targeted non-Iceberg catalogs.","solutions":["Rewrite the statement as CREATE OR REPLACE VIEW <viewName> AS <query>.","If you truly need ALTER VIEW semantics, use a Spark built-in (session-catalog) view instead of an Iceberg view.","Update migration tooling/ORM templates to emit CREATE OR REPLACE VIEW for Iceberg catalogs."],"exampleFix":"// before\nALTER VIEW db.events AS SELECT * FROM events_v2\n// after\nCREATE OR REPLACE VIEW db.events AS SELECT * FROM events_v2","handlingStrategy":"validation","validationCode":"// Databricks/JDBC preflight: SHOW VIEWS IN db; treat Iceberg-catalog views as CREATE OR REPLACE only","typeGuard":"def isIcebergView(ident: String): Boolean = ident.startsWith(\"iceberg_catalog.\")  // route these to CREATE OR REPLACE VIEW, never ALTER VIEW","tryCatchPattern":"try {\n  spark.sql(s\"ALTER VIEW $viewName AS $query\")\n} catch {\n  case e: AnalysisException if e.getMessage.contains(\"Use CREATE OR REPLACE VIEW\") =>\n    spark.sql(s\"CREATE OR REPLACE VIEW $viewName AS $query\")\n}","preventionTips":["Standardize on CREATE OR REPLACE VIEW for Iceberg views in SQL scripts and migration tools.","Scrub legacy ALTER VIEW statements when migrating to the Iceberg catalog.","Remember: session-catalog Spark views allow ALTER VIEW; Iceberg views do not."],"tags":["spark","sql","views","analysis-exception"],"backgroundTag":"unsupported-operation","analyzedSha":"86d9c8fc543e7c56c9f624eb725f76c9baff9570","analyzedAt":"2026-09-12T00:46:39.097Z","contentChangedAt":"2026-09-12T00:46:39.097Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}