{"record":{"id":"c5d0914cd445ae2e","repo":"apache/iceberg","slug":"create-view-column-arity-mismatch-not-enough-data-c5d091","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":null,"httpStatus":null,"severity":"error","filePath":"spark/v4.0/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.0/spark-extensions/src/main/scala/org/apache/spark/sql/catalyst/analysis/CheckViews.scala#L59-L95","documentation":"When a CREATE VIEW statement specifies an explicit column list, Iceberg's CheckViews verifies the column count matches the query output. If the user names more view columns than the query produces, Spark's CREATE_VIEW_COLUMN_ARITY_MISMATCH.NOT_ENOUGH_DATA_COLUMNS error is raised with the view and query column lists in the message.","triggerScenarios":"CREATE VIEW v (a, b, c) AS SELECT x, y FROM t — the explicit column list (3) exceeds query output columns (2).","commonSituations":"Typos or stale column lists after the underlying SELECT changed; copying view DDL from another table with a wider schema; aliased-column refactors.","solutions":["Match the explicit column list length to the query's output column count","Remove the explicit column list to let view columns inherit query output names","Adjust the SELECT to produce as many columns as the view declares"],"exampleFix":"// before\nCREATE VIEW v (a, b, c) 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(\"Declared view columns exceed query output columns\")\n}","typeGuard":null,"tryCatchPattern":"try {\n  spark.sql(createViewDdl)\n} catch {\n  case e: AnalysisException if e.getErrorClass.exists(_.startsWith(\"CREATE_VIEW_COLUMN_ARITY_MISMATCH\")) =>\n    logError(s\"Column list mismatch in: $createViewDdl\", e)\n}","preventionTips":["Generate column lists from the SELECT's schema rather than hardcoding","Align column count before executing CREATE VIEW DDL","Re-check view DDL after changing the underlying query"],"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"}