{"record":{"id":"cb39d6af6fbf3ebf","repo":"apache/iceberg","slug":"alter-view-viewname-as-is-not-supported-use-cre-cb39d6","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.1/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.1/spark-extensions/src/main/scala/org/apache/spark/sql/catalyst/analysis/CheckViews.scala#L46-L82","documentation":"Iceberg's view support (CheckViews extension) does not implement ALTER VIEW ... AS ... for session-catalog v2 views. Since Iceberg views are replaced via CREATE OR REPLACE VIEW, ALTER VIEW AS is explicitly rejected with this IcebergAnalysisException.","triggerScenarios":"Executing `ALTER VIEW <iceberg view name> AS SELECT ...` against a view registered through the Iceberg catalog/session catalog (ResolvedV2View), while the Iceberg Spark extensions are loaded.","commonSituations":"Migrating Spark scripts written for the built-in Spark views (where ALTER VIEW AS works) to Iceberg views; automated migration tools generating ALTER VIEW statements.","solutions":["Use `CREATE OR REPLACE VIEW <viewName> AS SELECT ...` instead of ALTER VIEW ... AS","Update migration scripts/templates to translate ALTER VIEW AS into CREATE OR REPLACE VIEW","If only metadata changes are needed (not the query), use ALTER VIEW RENAME TO or view properties commands as appropriate"],"exampleFix":"// before\nALTER VIEW my_view AS SELECT id, name FROM my_table\n// after\nCREATE OR REPLACE VIEW my_view AS SELECT id, name FROM my_table","handlingStrategy":"validation","validationCode":"// rewrite DDL before execution\nval rewritten = sqlText.replaceAll(\"(?i)\\\\bALTER\\\\s+VIEW\\\\s+(\\\\S+)\\\\s+AS\\\\b\", \"CREATE OR REPLACE VIEW $1 AS\")","typeGuard":null,"tryCatchPattern":"try { spark.sql(sqlText) } catch { case e: IcebergAnalysisException if e.getMessage.contains(\"ALTER VIEW\") => spark.sql(toCreateOrReplace(sqlText)) }","preventionTips":["Use CREATE OR REPLACE VIEW for redefinition of Iceberg views","Update migration scripts that emit ALTER VIEW AS","Document that Iceberg views don't support ALTER VIEW AS"],"tags":["spark","views","sql-extensions"],"backgroundTag":"unsupported-operation","analyzedSha":"86d9c8fc543e7c56c9f624eb725f76c9baff9570","analyzedAt":"2026-09-12T00:46:39.097Z","contentChangedAt":"2026-09-12T00:46:39.097Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}