{"record":{"id":"0295954da94d784f","repo":"apache/iceberg","slug":"cannot-create-table-s-as-it-already-exists-029595","errorCode":null,"errorMessage":"Cannot create table %s as it already exists","messagePattern":"Cannot create table (.+?) as it already exists","errorType":"exception","errorClass":"AlreadyExistsException","httpStatus":null,"severity":"error","filePath":"spark/v4.0/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.0/spark/src/main/java/org/apache/iceberg/spark/actions/BaseTableCreationSparkAction.java#L149-L185","documentation":"Failure in BaseTableCreationSparkAction.stageDestTable: staging the destination table creation threw TableAlreadyExistsException (the %s is the destination identifier), rethrown as the action's already-exists error. It fires when CTAS/RTAS-style actions target an existing table without replace semantics.","triggerScenarios":"Calling migrateTable(...).as(existingIdent) (or snapshot to an existing name) where the destination catalog already contains that table name; stageCreate throws TableAlreadyExistsException.","commonSituations":"Re-running a failed/partial migration without cleaning up the staged destination table; a backup table from a previous migrate run already occupying the name.","solutions":["Choose a new destination name via .as(\"db.new_name\") or drop the existing destination table","If it is a leftover backup/staged table from a failed run, drop it or restore it before re-running","Check catalog contents with SHOW TABLES IN <catalog>.<ns> to confirm the name is free"],"exampleFix":"// before\nSparkActions.get(spark).migrateTable(\"db.tbl\").as(\"iceberg_db.tbl\").execute(); // tbl exists\n// after\nSparkActions.get(spark).migrateTable(\"db.tbl\").as(\"iceberg_db.tbl_migrated\").execute();","handlingStrategy":"try-catch","validationCode":"boolean exists = spark.catalog().tableExists(destIdentString);\nif (exists) { dropOrRenameDestFirst(); }","typeGuard":null,"tryCatchPattern":"try { action.execute(); } catch (AlreadyExistsException e) { /* drop backup or pick new .as() name and retry */ }","preventionTips":["Use unique destination names per run","Clean up leftover backup/staged tables after failed runs","Check SHOW TABLES before running"],"tags":["spark","already-exists","staged-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"}