{"record":{"id":"6af940dd8daff7b4","repo":"apache/iceberg","slug":"create-view-column-arity-mismatch-too-many-data-co","errorCode":"CREATE_VIEW_COLUMN_ARITY_MISMATCH.TOO_MANY_DATA_COLUMNS","errorMessage":"CREATE_VIEW_COLUMN_ARITY_MISMATCH.TOO_MANY_DATA_COLUMNS (viewName: %s, viewColumns: %s, dataColumns: %s)","messagePattern":"CREATE_VIEW_COLUMN_ARITY_MISMATCH\\.TOO_MANY_DATA_COLUMNS \\(viewName: (.+?), viewColumns: (.+?), dataColumns: (.+?)\\)","errorType":"error_code","errorClass":"AnalysisException","httpStatus":null,"severity":"error","filePath":"spark/v3.5/spark-extensions/src/main/scala/org/apache/spark/sql/catalyst/analysis/CheckViews.scala","lineNumber":84,"sourceCode":"\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(\", \")))\n      } else if (columns.length < query.output.length) {\n        throw new AnalysisException(\n          errorClass = \"CREATE_VIEW_COLUMN_ARITY_MISMATCH.TOO_MANY_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(\", \")))\n      }\n    }\n  }\n\n  private def checkCyclicViewReference(\n      viewIdent: Seq[String],\n      plan: LogicalPlan,\n      cyclePath: Seq[Seq[String]]): Unit = {\n    plan match {\n      case sub @ SubqueryAlias(_, Project(_, _)) =>\n        val currentViewIdent: Seq[String] = sub.identifier.qualifier :+ sub.identifier.name\n        checkIfRecursiveView(viewIdent, currentViewIdent, cyclePath, sub.children)\n      case v1View: View =>","sourceCodeStart":66,"sourceCodeEnd":102,"githubUrl":"https://github.com/apache/iceberg/blob/86d9c8fc543e7c56c9f624eb725f76c9baff9570/spark/v3.5/spark-extensions/src/main/scala/org/apache/spark/sql/catalyst/analysis/CheckViews.scala#L66-L102","documentation":"Thrown by Iceberg's CheckViews rule when CREATE VIEW lists a different number of column names than the view's query produces. Specifically, the query outputs MORE columns than the explicit column list provides (columns.length < query.output.length). Spark/Iceberg requires an exact 1:1 mapping between declared view columns and query output columns.","triggerScenarios":"Executing CREATE VIEW catalog.db.v (col1) AS SELECT a, b, c FROM tbl — the SELECT outputs 3 columns but only 1 view column name was declared.","commonSituations":"Typos or omissions when renaming view columns; copying a view definition across table versions where the source table gained columns; hand-written DDL where the SELECT list was widened later.","solutions":["Add or remove column aliases so the number of declared view columns exactly equals the number of SELECT output columns","Drop the explicit column list entirely and let the view inherit the query's column names","If the source table gained columns unintentionally, replace SELECT * with an explicit column list matching the intended view columns"],"exampleFix":"// before\nCREATE VIEW catalog.db.v (id) AS SELECT id, name, ts FROM catalog.db.t\n// after\nCREATE VIEW catalog.db.v (id, name, ts) AS SELECT id, name, ts FROM catalog.db.t","handlingStrategy":"validation","validationCode":"val queryOutput = df.queryExecution.analyzed.output.map(_.name)\nrequire(viewColumns.length == queryOutput.length,\n  s\"view declares ${viewColumns.length} columns but query outputs ${queryOutput.length}\")","typeGuard":null,"tryCatchPattern":"try { spark.sql(createViewSql) } catch { case e: AnalysisException if e.getErrorClass == \"CREATE_VIEW_COLUMN_ARITY_MISMATCH\" => // fix column list }","preventionTips":["Omit the explicit column list unless you need to rename view columns","Count SELECT output columns against declared view columns before submitting DDL","Avoid SELECT * in view definitions of tables whose schema may evolve"],"tags":["spark","sql","view-ddl","analysis"],"backgroundTag":"schema-validation-failed","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"}