{"record":{"id":"8f81ca6491632339","repo":"apache/iceberg","slug":"view-already-exists-ident","errorCode":null,"errorMessage":"View already exists: ident","messagePattern":"View already exists: ident","errorType":"exception","errorClass":"ViewAlreadyExistsException","httpStatus":409,"severity":"error","filePath":"spark/v3.5/spark/src/main/java/org/apache/iceberg/spark/SparkCatalog.java","lineNumber":641,"sourceCode":"                .putAll(Spark3Util.rebuildCreateProperties(properties))\n                .put(SparkView.QUERY_COLUMN_NAMES, COMMA_JOINER.join(queryColumnNames))\n                .buildKeepingLast();\n\n        org.apache.iceberg.view.View view =\n            asViewCatalog\n                .buildView(buildIdentifier(ident))\n                .withDefaultCatalog(currentCatalog)\n                .withDefaultNamespace(Namespace.of(currentNamespace))\n                .withQuery(\"spark\", sql)\n                .withSchema(icebergSchema)\n                .withLocation(properties.get(\"location\"))\n                .withProperties(props)\n                .create();\n        return new SparkView(catalogName, view);\n      } catch (org.apache.iceberg.exceptions.NoSuchNamespaceException e) {\n        throw new NoSuchNamespaceException(currentNamespace);\n      } catch (AlreadyExistsException e) {\n        throw new ViewAlreadyExistsException(ident);\n      }\n    }\n\n    throw new UnsupportedOperationException(\n        \"Creating a view is not supported by catalog: \" + catalogName);\n  }\n\n  @Override\n  public View replaceView(\n      Identifier ident,\n      String sql,\n      String currentCatalog,\n      String[] currentNamespace,\n      StructType schema,\n      String[] queryColumnNames,\n      String[] columnAliases,\n      String[] columnComments,\n      Map<String, String> properties)","sourceCodeStart":623,"sourceCodeEnd":659,"githubUrl":"https://github.com/apache/iceberg/blob/86d9c8fc543e7c56c9f624eb725f76c9baff9570/spark/v3.5/spark/src/main/java/org/apache/iceberg/spark/SparkCatalog.java#L623-L659","documentation":"SparkCatalog.createView throws ViewAlreadyExistsException when the underlying Iceberg catalog already contains a view (or, depending on catalog, an entry) at the target identifier. The underlying create() raises AlreadyExistsException and it is translated to ViewAlreadyExistsException with the Spark identifier. The existing view is left untouched.","triggerScenarios":"Running CREATE VIEW <ident> (SparkCatalog.java:606 createView) when a view with the same identifier already exists in the catalog; re-running a provisioning/seed script without IF NOT EXISTS; a concurrent job created the same view between the existence check and the create.","commonSituations":"Idempotent deployment scripts re-executed; race between two Spark jobs creating the same view; developer assumes tables and views can share a name (they cannot in the same identifier space here); leftover view from a previous experiment.","solutions":["Use CREATE VIEW IF NOT EXISTS, or drop the existing view first: DROP VIEW <ident>.","Check the existing view with DESCRIBE VIEW <ident> to decide whether to keep or replace it; use CREATE OR REPLACE VIEW to overwrite intentionally.","For programmatic use, guard with asViewCatalog.viewExists(ident) before createView, or catch ViewAlreadyExistsException.","In CI/seed scripts, make creation idempotent (IF NOT EXISTS or drop-then-create)."],"exampleFix":"-- before\nCREATE VIEW analytics.daily_metrics AS SELECT ...;   -- fails on rerun\n\n-- after\nCREATE VIEW IF NOT EXISTS analytics.daily_metrics AS SELECT ...;","handlingStrategy":"try-catch","validationCode":"if (asViewCatalog.viewExists(ident)) {\n  // decide: keep, replace, or skip\n}","typeGuard":null,"tryCatchPattern":"try {\n  spark.sql(\"CREATE VIEW \" + ident + \" AS \" + sqlText);\n} catch (ViewAlreadyExistsException e) {\n  // treat as success for idempotent runs, or use CREATE OR REPLACE VIEW\n}","preventionTips":["Use CREATE VIEW IF NOT EXISTS in all provisioning/seed scripts.","Use CREATE OR REPLACE VIEW when overwriting is intended.","Make CI view creation idempotent to survive re-runs and races.","Remember views and tables share no name slot — check both when a name seems free."],"tags":["spark","view-catalog","already-exists"],"backgroundTag":"file-already-exists","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"}