{"record":{"id":"e765c008da22e48a","repo":"apache/iceberg","slug":"cannot-create-or-replace-table-s-a-view-with-the","errorCode":null,"errorMessage":"Cannot create or replace table %s: a view with the same name already exists","messagePattern":"Cannot create or replace table (.+?): a view with the same name already exists","errorType":"exception","errorClass":"AlreadyExistsException","httpStatus":409,"severity":"error","filePath":"spark/v4.2/spark/src/main/java/org/apache/iceberg/spark/SparkSessionCatalog.java","lineNumber":316,"sourceCode":"    try {\n      // create the table with the session catalog, then wrap it in a staged table that will delete\n      // to roll back\n      Table table = catalog.createTable(ident, schema, partitions, properties);\n      return new RollbackStagedTable(catalog, ident, table);\n\n    } catch (TableAlreadyExistsException e) {\n      // the table was deleted, but now already exists again. retry the replace.\n      return stageReplace(ident, schema, partitions, properties);\n    }\n  }\n\n  @Override\n  public StagedTable stageCreateOrReplace(\n      Identifier ident, StructType schema, Transform[] partitions, Map<String, String> properties)\n      throws NoSuchNamespaceException {\n    if (viewExists(ident)) {\n      // StagingTableCatalog does not declare a checked collision exception for create-or-replace.\n      throw new AlreadyExistsException(\n          \"Cannot create or replace table %s: a view with the same name already exists\", ident);\n    }\n\n    String provider = properties.get(\"provider\");\n    TableCatalog catalog;\n    if (useIceberg(provider)) {\n      if (asStagingCatalog != null) {\n        return asStagingCatalog.stageCreateOrReplace(ident, schema, partitions, properties);\n      }\n      catalog = icebergCatalog;\n    } else {\n      catalog = getSessionCatalog();\n    }\n\n    // drop the table if it exists\n    catalog.dropTable(ident);\n\n    try {","sourceCodeStart":298,"sourceCodeEnd":334,"githubUrl":"https://github.com/apache/iceberg/blob/86d9c8fc543e7c56c9f624eb725f76c9baff9570/spark/v4.2/spark/src/main/java/org/apache/iceberg/spark/SparkSessionCatalog.java#L298-L334","documentation":"When staging a create-or-replace, SparkSessionCatalog checks whether a view occupies the target identifier; if so it throws AlreadyExistsException. This explicit guard is needed because StagingTableCatalog's stageCreateOrReplace contract does not declare a checked collision exception, so the conflict must be surfaced eagerly.","triggerScenarios":"Executing CREATE OR REPLACE TABLE ident when a view named ident already exists in either the Iceberg view catalog or the delegated session catalog; viewExists(ident) returns true during stageCreateOrReplace.","commonSituations":"Namespace shared between views and tables where a view was created earlier; migration from a view to a table; scripts assuming table-only namespaces.","solutions":["Drop the conflicting view first (DROP VIEW ident) then retry the create-or-replace","Choose a different table name if the view is still needed","Move views and tables into separate namespaces to avoid identifier collisions"],"exampleFix":"-- before\nCREATE OR REPLACE TABLE prod.db.report (...)\n-- after\nDROP VIEW prod.db.report;\nCREATE OR REPLACE TABLE prod.db.report (...)","handlingStrategy":"validation","validationCode":"// ensure no view occupies the identifier before create-or-replace\nif (catalog.viewExists(ident)) {\n  spark.sql(\"DROP VIEW \" + ident);\n}","typeGuard":null,"tryCatchPattern":"try {\n  spark.sql(\"CREATE OR REPLACE TABLE \" + ident + \" (...)\");\n} catch (AlreadyExistsException e) {\n  // a view with the same name exists; resolve manually\n  throw e;\n}","preventionTips":["Keep views and tables in separate namespaces","List existing views (SHOW VIEWS) in shared namespaces before replacing","Watch for legacy views left over from migrations"],"tags":["spark","name-collision","view"],"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"}