{"record":{"id":"118b08b9085bd1f8","repo":"apache/iceberg","slug":"tablealreadyexistsexception-to-118b08","errorCode":null,"errorMessage":"TableAlreadyExistsException: <to>","messagePattern":"TableAlreadyExistsException: <to>","errorType":"exception","errorClass":"org.apache.spark.sql.catalyst.analysis.TableAlreadyExistsException","httpStatus":null,"severity":"error","filePath":"spark/v4.2/spark/src/main/java/org/apache/iceberg/spark/SparkCatalog.java","lineNumber":383,"sourceCode":"  private boolean catalogDropTable(Identifier ident) {\n    if (isPathIdentifier(ident)) {\n      return tables.dropTable(((PathIdentifier) ident).location(), false /* don't purge data */);\n    } else {\n      return icebergCatalog.dropTable(buildIdentifier(ident), false /* don't purge data */);\n    }\n  }\n\n  @Override\n  public void renameTable(Identifier from, Identifier to)\n      throws NoSuchTableException, TableAlreadyExistsException {\n    try {\n      checkNotPathIdentifier(from, \"renameTable\");\n      checkNotPathIdentifier(to, \"renameTable\");\n      icebergCatalog.renameTable(buildIdentifier(from), buildIdentifier(to));\n    } catch (org.apache.iceberg.exceptions.NoSuchTableException e) {\n      throw new NoSuchTableException(from);\n    } catch (AlreadyExistsException e) {\n      throw new TableAlreadyExistsException(to);\n    }\n  }\n\n  @Override\n  public void invalidateTable(Identifier ident) {\n    if (!isPathIdentifier(ident)) {\n      icebergCatalog.invalidateTable(buildIdentifier(ident));\n    }\n  }\n\n  @Override\n  public Identifier[] listTables(String[] namespace) {\n    return icebergCatalog.listTables(Namespace.of(namespace)).stream()\n        .map(ident -> Identifier.of(ident.namespace().levels(), ident.name()))\n        .toArray(Identifier[]::new);\n  }\n\n  @Override","sourceCodeStart":365,"sourceCodeEnd":401,"githubUrl":"https://github.com/apache/iceberg/blob/86d9c8fc543e7c56c9f624eb725f76c9baff9570/spark/v4.2/spark/src/main/java/org/apache/iceberg/spark/SparkCatalog.java#L365-L401","documentation":"When the rename target identifier already exists, the Iceberg catalog throws AlreadyExistsException and SparkCatalog translates it to Spark's TableAlreadyExistsException for the target name. Renames in Iceberg catalogs are fail-fast and never overwrite an existing table.","triggerScenarios":"ALTER TABLE ... RENAME TO where the destination table already exists in the same namespace; retrying a rename that partially appeared to succeed.","commonSituations":"Job retries re-attempting a completed rename; staging table naming collisions; automated pipelines generating deterministic target names.","solutions":["Drop or rename the existing target table first","Choose a unique target name (e.g. append a timestamp)","Check for the target's existence before renaming","Handle TableAlreadyExistsException and skip the rename on retries"],"exampleFix":"// before\nspark.sql(\"ALTER TABLE db.t RENAME TO db.archive\") // db.archive exists\n// after\nspark.sql(\"ALTER TABLE db.t RENAME TO db.archive_\" + System.currentTimeMillis())","handlingStrategy":"validation","validationCode":"if (spark.catalog().tableExists(toIdent)) { throw new IllegalStateException(\"target already exists\"); }","typeGuard":null,"tryCatchPattern":"try { spark.sql(\"ALTER TABLE db.t RENAME TO db.t2\"); } catch (TableAlreadyExistsException e) { /* pick another target or skip */ }","preventionTips":["Generate unique target names (timestamp/uuid suffix)","Check target existence before renaming","Handle retries by treating AlreadyExists as success"],"tags":["spark","rename","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"}