{"record":{"id":"936cafc980c2558f","repo":"apache/iceberg","slug":"alter-view-viewname-as-is-not-supported-use-cre-936caf","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.0/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 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      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/v4.0/spark-extensions/src/main/scala/org/apache/spark/sql/catalyst/analysis/CheckViews.scala#L46-L82","documentation":"Iceberg's view support in Spark 4.0 does not implement ALTER VIEW ... AS (redefining a view in place). When CheckViews sees AlterViewAs applied to a resolved Iceberg V2 view, it throws this IcebergAnalysisException and directs users to CREATE OR REPLACE VIEW, which Iceberg supports for full view redefinition.","triggerScenarios":"Running ALTER VIEW catalog.db.v AS SELECT ... against an Iceberg view (a ResolvedV2View) via the Iceberg Spark extensions.","commonSituations":"Scripts written for the built-in Spark session catalog ported to Iceberg catalogs where ALTER VIEW AS is unavailable; schema-evolution workflows attempting to redefine a view in place.","solutions":["Rewrite the statement as CREATE OR REPLACE VIEW catalog.db.v AS SELECT ...","Drop and recreate the view if replace semantics are unacceptable","Use ALTER VIEW only for operations Iceberg supports (e.g. rename is via ALTER VIEW RENAME TO)"],"exampleFix":"// before\nALTER VIEW my_view AS SELECT id, name FROM t\n// after\nCREATE OR REPLACE VIEW my_view AS SELECT id, name FROM t","handlingStrategy":"validation","validationCode":"// static check: replace any occurrence in your SQL pipeline\nval fixed = sql.replaceAll(?i\"ALTER\\s+VIEW\\s+(\\S+)\\s+AS\", \"CREATE OR REPLACE VIEW $1 AS\")","typeGuard":null,"tryCatchPattern":"try {\n  spark.sql(stmt)\n} catch {\n  case e: Exception if e.getMessage.contains(\"ALTER VIEW\") && e.getMessage.contains(\"CREATE OR REPLACE VIEW\") =>\n    val rewritten = rewriteAlterViewAs(stmt)\n    spark.sql(rewritten)\n}","preventionTips":["Use CREATE OR REPLACE VIEW as the standard redefinition idiom for Iceberg views","Lint SQL scripts for ALTER VIEW ... AS against Iceberg catalogs","Review migration scripts from the built-in session catalog for unsupported DDL"],"tags":["spark","iceberg","views","sql"],"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"}