{"record":{"id":"751a47128a279f00","repo":"apache/iceberg","slug":"cannot-create-table-s-as-it-already-exists-751a47","errorCode":null,"errorMessage":"Cannot create table %s as it already exists","messagePattern":"Cannot create table (.+?) as it already exists","errorType":"exception","errorClass":"org.apache.iceberg.exceptions.AlreadyExistsException","httpStatus":null,"severity":"error","filePath":"spark/v4.2/spark/src/main/java/org/apache/iceberg/spark/actions/BaseTableCreationSparkAction.java","lineNumber":167,"sourceCode":"        catalog.getClass().getName(),\n        SparkSessionCatalog.class.getName(),\n        SparkCatalog.class.getName());\n\n    return (StagingTableCatalog) catalog;\n  }\n\n  protected StagedSparkTable stageDestTable() {\n    try {\n      Map<String, String> props = destTableProps();\n      StructType schema = sourceTable.schema();\n      Transform[] partitioning = sourceTable.partitioning();\n      return (StagedSparkTable)\n          destCatalog().stageCreate(destTableIdent(), schema, partitioning, props);\n    } catch (org.apache.spark.sql.catalyst.analysis.NoSuchNamespaceException e) {\n      throw new NoSuchNamespaceException(\n          \"Cannot create table %s as the namespace does not exist\", destTableIdent());\n    } catch (org.apache.spark.sql.catalyst.analysis.TableAlreadyExistsException e) {\n      throw new AlreadyExistsException(\n          \"Cannot create table %s as it already exists\", destTableIdent());\n    }\n  }\n\n  protected void ensureNameMappingPresent(Table table) {\n    if (!table.properties().containsKey(TableProperties.DEFAULT_NAME_MAPPING)) {\n      NameMapping nameMapping = MappingUtil.create(table.schema());\n      String nameMappingJson = NameMappingParser.toJson(nameMapping);\n      table.updateProperties().set(TableProperties.DEFAULT_NAME_MAPPING, nameMappingJson).commit();\n    }\n  }\n\n  protected String getMetadataLocation(Table table) {\n    String defaultValue =\n        LocationUtil.stripTrailingSlash(table.location()) + \"/\" + ICEBERG_METADATA_FOLDER;\n    return LocationUtil.stripTrailingSlash(\n        table.properties().getOrDefault(TableProperties.WRITE_METADATA_LOCATION, defaultValue));\n  }","sourceCodeStart":149,"sourceCodeEnd":185,"githubUrl":"https://github.com/apache/iceberg/blob/86d9c8fc543e7c56c9f624eb725f76c9baff9570/spark/v4.2/spark/src/main/java/org/apache/iceberg/spark/actions/BaseTableCreationSparkAction.java#L149-L185","documentation":"During staging of the destination table, destCatalog().stageCreate throws Spark's TableAlreadyExistsException when a table with the destination identifier already exists. The action converts it into Iceberg's AlreadyExistsException with this message.","triggerScenarios":"Running MIGRATE or SNAPSHOT where the destination table name already exists in the destination catalog; re-running a previously failed/partial migrate after the destination was already created.","commonSituations":"Re-running a failed migration without cleaning up the staged destination table; accidentally reusing an existing table name; OR REPLACE not supported for this procedure path.","solutions":["Choose a unique destination table name","Drop or rename the existing destination table before re-running the action","If re-running after failure, remove the leftover staged table from the destination catalog"],"exampleFix":"// before\nCALL iceberg.system.snapshot('source_tbl', 'db.existing_tbl')\n// after\nDROP TABLE db.existing_tbl; -- or pick a new name\nCALL iceberg.system.snapshot('source_tbl', 'db.new_tbl');","handlingStrategy":"validation","validationCode":"// Check destination does not exist before running\nspark.sql(\"DESCRIBE TABLE db.dest_tbl\"); // throws if absent -> safe to proceed","typeGuard":null,"tryCatchPattern":"try { ... } catch (AlreadyExistsException e) { /* pick a new name or drop existing table */ }","preventionTips":["Check table existence (DESCRIBE/SHOW TABLES) before choosing the destination name","Clean up leftover staged/destination tables after failed migration runs","Use unique, timestamped destination names for one-off migrations"],"tags":["spark","table-already-exists","catalog","table-creation"],"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"}