{"record":{"id":"a437fdda2f290e07","repo":"apache/iceberg","slug":"view-already-exists-ident-a437fd","errorCode":null,"errorMessage":"View already exists: ${ident}","messagePattern":"View already exists: (.+?)","errorType":"exception","errorClass":"ViewAlreadyExistsException","httpStatus":null,"severity":"error","filePath":"spark/v4.1/spark/src/main/java/org/apache/iceberg/spark/SparkCatalog.java","lineNumber":644,"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":626,"sourceCodeEnd":662,"githubUrl":"https://github.com/apache/iceberg/blob/86d9c8fc543e7c56c9f624eb725f76c9baff9570/spark/v4.1/spark/src/main/java/org/apache/iceberg/spark/SparkCatalog.java#L626-L662","documentation":"Thrown by SparkCatalog.createView when the underlying view catalog already contains a view with the same identifier. The Iceberg AlreadyExistsException is translated into a Spark ViewAlreadyExistsException. CREATE VIEW without IF NOT EXISTS always fails on an existing name.","triggerScenarios":"CREATE VIEW on an identifier that already exists as a view; programmatic catalog.createView(...) without checking existence; a race where two sessions create the same view simultaneously.","commonSituations":"Re-running idempotency-naive migration/deployment scripts; name collision between views in the same namespace; residual view left by a partially rolled-back pipeline.","solutions":["Use CREATE VIEW IF NOT EXISTS, or drop/rename the existing view first","In code, check catalog.viewExists(ident) before createView, or catch ViewAlreadyExistsException","Switch to replaceView (CREATE OR REPLACE VIEW) when overwriting is intended","Choose a distinct view name if the collision is accidental"],"exampleFix":"// before\nspark.sql(\"CREATE VIEW prod.db.report AS SELECT ...\") // View already exists\n// after\nspark.sql(\"CREATE OR REPLACE VIEW prod.db.report AS SELECT ...\")","handlingStrategy":"try-catch","validationCode":"Identifier ident = Identifier.of(new String[]{\"db\"}, \"report\");\nif (viewCatalog.viewExists(ident)) {\n  viewCatalog.dropView(ident); // or use replaceView\n}","typeGuard":"boolean viewIsFree(ViewCatalog catalog, Identifier ident) {\n  return !catalog.viewExists(ident);\n}","tryCatchPattern":"try {\n  spark.sql(\"CREATE VIEW \" + ident + \" AS \" + sql);\n} catch (ViewAlreadyExistsException e) {\n  LOG.info(\"View {} exists, replacing\", ident);\n  spark.sql(\"CREATE OR REPLACE VIEW \" + ident + \" AS \" + sql);\n}","preventionTips":["Prefer CREATE OR REPLACE VIEW when overwriting is acceptable","Use IF NOT EXISTS when the create is idempotent provisioning","Generate unique view names per run in ephemeral/CI environments","Check viewExists before createView in programmatic pipelines"],"tags":["spark","view","duplicate","create"],"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"}