{"record":{"id":"ba73c4bf4c2894bb","repo":"apache/iceberg","slug":"cannot-rename-s-to-s-table-already-exists","errorCode":null,"errorMessage":"Cannot rename %s to %s. Table already exists","messagePattern":"Cannot rename (.+?) to (.+?)\\. Table already exists","errorType":"exception","errorClass":"AlreadyExistsException","httpStatus":null,"severity":"error","filePath":"core/src/main/java/org/apache/iceberg/inmemory/InMemoryCatalog.java","lineNumber":182,"sourceCode":"  @Override\n  public void renameTable(TableIdentifier from, TableIdentifier to) {\n    if (from.equals(to)) {\n      return;\n    }\n\n    synchronized (this) {\n      if (!namespaceExists(to.namespace())) {\n        throw new NoSuchNamespaceException(\n            \"Cannot rename %s to %s. Namespace does not exist: %s\", from, to, to.namespace());\n      }\n\n      String fromLocation = tables.get(from);\n      if (null == fromLocation) {\n        throw new NoSuchTableException(\"Cannot rename %s to %s. Table does not exist\", from, to);\n      }\n\n      if (tables.containsKey(to)) {\n        throw new AlreadyExistsException(\"Cannot rename %s to %s. Table already exists\", from, to);\n      }\n\n      if (views.containsKey(to)) {\n        throw new AlreadyExistsException(\"Cannot rename %s to %s. View already exists\", from, to);\n      }\n\n      tables.put(to, fromLocation);\n      tables.remove(from);\n    }\n  }\n\n  @Override\n  public void createNamespace(Namespace namespace) {\n    createNamespace(namespace, Collections.emptyMap());\n  }\n\n  @Override\n  public void createNamespace(Namespace namespace, Map<String, String> metadata) {","sourceCodeStart":164,"sourceCodeEnd":200,"githubUrl":"https://github.com/apache/iceberg/blob/86d9c8fc543e7c56c9f624eb725f76c9baff9570/core/src/main/java/org/apache/iceberg/inmemory/InMemoryCatalog.java#L164-L200","documentation":"InMemoryCatalog.renameTable throws AlreadyExistsException when a table already exists at the destination identifier. Renaming is not a merge; the catalog refuses to overwrite an existing table at 'to'.","triggerScenarios":"Calling catalog.renameTable(from, to) where tables already contains 'to'. E.g. renaming 'events' to 'events_backup' when 'events_backup' already exists from a previous rename.","commonSituations":"Cron jobs producing colliding backup names; retrying a partially completed rename workflow; test fixtures reusing names.","solutions":["Drop or rename the existing destination table first","Choose a unique destination identifier (e.g. append timestamp)","Check catalog.tableExists(to) before renaming and handle collisions","Catch AlreadyExistsException to skip if the destination already has the data"],"exampleFix":"// before\ncatalog.renameTable(from, to);\n// after\nif (!catalog.tableExists(to)) {\n  catalog.renameTable(from, to);\n} else {\n  catalog.dropTable(to);\n  catalog.renameTable(from, to);\n}","handlingStrategy":"validation","validationCode":"if (catalog.tableExists(to)) { catalog.dropTable(to); }\ncatalog.renameTable(from, to);","typeGuard":null,"tryCatchPattern":"try { catalog.renameTable(from, to); } catch (AlreadyExistsException e) { /* resolve collision: drop or pick new name */ }","preventionTips":["Generate unique destination names (timestamp/suffix)","Check tableExists(to) before renaming","Make backup/rotation jobs collision-aware","Clean up stale destination tables in teardown"],"tags":["inmemory-catalog","rename","table-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-14T21:17:11.552Z"}