{"record":{"id":"79079d1a5e39acbe","repo":"apache/iceberg","slug":"create-view-column-arity-mismatch-not-enough-data-79079d","errorCode":"CREATE_VIEW_COLUMN_ARITY_MISMATCH.NOT_ENOUGH_DATA_COLUMNS","errorMessage":"CREATE_VIEW_COLUMN_ARITY_MISMATCH.NOT_ENOUGH_DATA_COLUMNS","messagePattern":"CREATE_VIEW_COLUMN_ARITY_MISMATCH\\.NOT_ENOUGH_DATA_COLUMNS","errorType":"error_code","errorClass":"AnalysisException","httpStatus":null,"severity":"error","filePath":"spark/v4.1/spark-extensions/src/main/scala/org/apache/spark/sql/catalyst/analysis/CheckViews.scala","lineNumber":77,"sourceCode":"            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(\", \")))\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],","sourceCodeStart":59,"sourceCodeEnd":95,"githubUrl":"https://github.com/apache/iceberg/blob/86d9c8fc543e7c56c9f624eb725f76c9baff9570/spark/v4.1/spark-extensions/src/main/scala/org/apache/spark/sql/catalyst/analysis/CheckViews.scala#L59-L95","documentation":"When creating an Iceberg view, the declared column list has MORE names than the view's query produces. Iceberg's CheckViews throws Spark's standard CREATE_VIEW_COLUMN_ARITY_MISMATCH.NOT_ENOUGH_DATA_COLUMNS AnalysisException, naming the view, its declared columns, and the query's actual output columns.","triggerScenarios":"`CREATE VIEW v (a, b, c) AS SELECT x, y FROM ...` — the explicit column list length exceeds the number of columns output by the SELECT query on an Iceberg view (v2 view path in CheckViews.verifyColumnCount).","commonSituations":"Hand-written CREATE VIEW where the column list was edited without updating the query; schema drift where the source query lost columns after a refactor.","solutions":["Match the column list length to the query output: reduce the declared column names to the number of query columns","Add/adjust expressions in the SELECT so it returns exactly as many columns as declared","Drop the explicit column list entirely and rely on query output names, optionally using aliases in the SELECT"],"exampleFix":"// before\nCREATE VIEW v (a, b, c) AS SELECT id, name FROM t\n// after\nCREATE VIEW v (a, b) AS SELECT id, name FROM t","handlingStrategy":"validation","validationCode":"val q = spark.sql(viewQuery); require(declaredCols.length == q.schema.length, s\"view declares ${declaredCols.length} columns but query outputs ${q.schema.length}\")","typeGuard":null,"tryCatchPattern":"try { spark.sql(createViewDdl) } catch { case e: AnalysisException if e.getErrorClass.exists(_.startsWith(\"CREATE_VIEW_COLUMN_ARITY_MISMATCH\")) => /* fix column list and retry */ }","preventionTips":["Count SELECT output columns before writing the column list","Prefer aliases in the SELECT over an explicit column list","Test view DDL against the actual query schema"],"tags":["spark","views","column-arity","sql-extensions"],"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"}