{"record":{"id":"00247965e41db58b","repo":"apache/iceberg","slug":"create-view-column-arity-mismatch-too-many-data-co-002479","errorCode":"CREATE_VIEW_COLUMN_ARITY_MISMATCH.TOO_MANY_DATA_COLUMNS","errorMessage":"[CREATE_VIEW_COLUMN_ARITY_MISMATCH.TOO_MANY_DATA_COLUMNS]","messagePattern":"\\[CREATE_VIEW_COLUMN_ARITY_MISMATCH\\.TOO_MANY_DATA_COLUMNS\\]","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"spark/v4.0/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/v4.0/spark-extensions/src/main/scala/org/apache/spark/sql/catalyst/analysis/CheckViews.scala#L66-L102","documentation":"Mirror case of the NOT_ENOUGH_DATA_COLUMNS check: if a CREATE VIEW statement's explicit column list has fewer names than the query's output columns, CheckViews throws Spark's CREATE_VIEW_COLUMN_ARITY_MISMATCH.TOO_MANY_DATA_COLUMNS. Each declared view column must map to exactly one query output column.","triggerScenarios":"CREATE VIEW v (a) AS SELECT x, y FROM t — the query produces 2 columns but only 1 view column is declared.","commonSituations":"Forgetting to alias extra SELECT columns or omitting them from the column list; schema drift in the underlying query adding columns; templated DDL generated with a stale column list.","solutions":["Add the missing column names to the explicit view column list","Reduce the SELECT to only the columns the view should expose","Drop the explicit column list entirely"],"exampleFix":"// before\nCREATE VIEW v (a) AS SELECT x, y FROM t\n// after\nCREATE VIEW v (a, b) AS SELECT x, y FROM t","handlingStrategy":"validation","validationCode":"val queryOutput = spark.sql(viewQuery).schema.length\nif (declaredColumns.length < queryOutput) {\n  throw new IllegalArgumentException(\"Query output has more columns than declared view columns\")\n}","typeGuard":null,"tryCatchPattern":"try {\n  spark.sql(createViewDdl)\n} catch {\n  case e: AnalysisException if e.getErrorClass.contains(\"TOO_MANY_DATA_COLUMNS\") =>\n    logError(s\"Declared too few view columns: $createViewDdl\", e)\n}","preventionTips":["Alias all SELECT columns explicitly and mirror them in the column list","Recompute the column list whenever the SELECT changes","Prefer omitting the column list and relying on SELECT aliases"],"tags":["spark","iceberg","views","column-arity"],"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-14T16:17:12.679Z"}