{"record":{"id":"418603b19cd31132","repo":"apache/iceberg","slug":"cannot-rename-s-to-s-table-already-exists-418603","errorCode":null,"errorMessage":"Cannot rename %s to %s. Table already exists","messagePattern":"Cannot rename (.+?) to (.+?)\\. Table already exists","errorType":"exception","errorClass":"AlreadyExistsException","httpStatus":409,"severity":"error","filePath":"hive-metastore/src/main/java/org/apache/iceberg/hive/HiveCatalog.java","lineNumber":406,"sourceCode":"        .collect(Collectors.toList());\n  }\n\n  @SuppressWarnings(\"checkstyle:CyclomaticComplexity\")\n  private void renameTableOrView(\n      TableIdentifier from,\n      TableIdentifier originalTo,\n      HiveOperationsBase.ContentType contentType) {\n    Preconditions.checkArgument(isValidIdentifier(from), \"Invalid identifier: %s\", from);\n\n    TableIdentifier to = removeCatalogName(originalTo);\n    Preconditions.checkArgument(isValidIdentifier(to), \"Invalid identifier: %s\", to);\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    if (tableExists(to)) {\n      throw new AlreadyExistsException(\"Cannot rename %s to %s. Table already exists\", from, to);\n    }\n\n    if (viewExists(to)) {\n      throw new AlreadyExistsException(\"Cannot rename %s to %s. View already exists\", from, to);\n    }\n\n    String toDatabase = to.namespace().level(0);\n    String fromDatabase = from.namespace().level(0);\n    String fromName = from.name();\n\n    try {\n      Table table = clients.run(client -> client.getTable(fromDatabase, fromName));\n      validateTableIsIcebergTableOrView(contentType, table, CatalogUtil.fullTableName(name, from));\n\n      table.setDbName(toDatabase);\n      table.setTableName(to.name());\n\n      clients.run(","sourceCodeStart":388,"sourceCodeEnd":424,"githubUrl":"https://github.com/apache/iceberg/blob/86d9c8fc543e7c56c9f624eb725f76c9baff9570/hive-metastore/src/main/java/org/apache/iceberg/hive/HiveCatalog.java#L388-L424","documentation":"renameTableOrView() throws AlreadyExistsException when a table already exists at the rename destination identifier. Hive requires unique table names per database, so renaming onto an existing table would clobber it and is refused.","triggerScenarios":"Calling catalog.renameTable(from, to) (or renameView, which shares this path) where a table named to.name() already exists in to.namespace().","commonSituations":"Renaming to a name that was previously used and never cleaned up; concurrent job created the destination table first; retrying a rename that already partially succeeded elsewhere; case-insensitivity surprises since Hive matches names case-insensitively.","solutions":["Check catalog.tableExists(to) before renaming and pick a free destination name.","Drop or rename the conflicting destination table if it is stale and safe to remove.","Make destination names unique (add timestamp/suffix) in concurrent workflows.","Beware Hive's case-insensitive matching — check variants of the destination name."],"exampleFix":"// before\ncatalog.renameTable(from, TableIdentifier.of(db, \"events\"));\n// after\nTableIdentifier to = TableIdentifier.of(db, \"events\");\nif (catalog.tableExists(to)) {\n  throw new IllegalStateException(\"Destination already exists: \" + to);\n}\ncatalog.renameTable(from, to);","handlingStrategy":"validation","validationCode":"TableIdentifier to = TableIdentifier.of(db, \"events\");\nif (catalog.tableExists(to)) {\n  throw new IllegalStateException(\"Rename target already exists: \" + to);\n}\ncatalog.renameTable(from, to);","typeGuard":null,"tryCatchPattern":"try {\n  catalog.renameTable(from, to);\n} catch (AlreadyExistsException e) {\n  // pick a new destination name or resolve the conflict manually\n  throw e;\n}","preventionTips":["Always check tableExists on the destination before renaming.","Remember Hive name matching is case-insensitive; check name variants.","Use unique suffixes for generated destination names in concurrent pipelines."],"tags":["hive-metastore","rename","already-exists","conflict"],"backgroundTag":"entity-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"}