{"record":{"id":"31a40ac2001ff641","repo":"apache/iceberg","slug":"cannot-rename-s-to-s-view-already-exists-31a40a","errorCode":null,"errorMessage":"Cannot rename %s to %s. View already exists","messagePattern":"Cannot rename (.+?) to (.+?)\\. View already exists","errorType":"exception","errorClass":"AlreadyExistsException","httpStatus":null,"severity":"error","filePath":"core/src/main/java/org/apache/iceberg/jdbc/JdbcCatalog.java","lineNumber":369,"sourceCode":"        JdbcUtil.namespaceToString(namespace));\n  }\n\n  @Override\n  public void renameTable(TableIdentifier from, TableIdentifier to) {\n    if (from.equals(to)) {\n      return;\n    }\n\n    if (!tableExists(from)) {\n      throw new NoSuchTableException(\"Table does not exist: %s\", from);\n    }\n\n    if (!namespaceExists(to.namespace())) {\n      throw new NoSuchNamespaceException(\"Namespace does not exist: %s\", to.namespace());\n    }\n\n    if (schemaVersion == JdbcUtil.SchemaVersion.V1 && viewExists(to)) {\n      throw new AlreadyExistsException(\"Cannot rename %s to %s. View already exists\", from, to);\n    }\n\n    if (tableExists(to)) {\n      throw new AlreadyExistsException(\"Table already exists: %s\", to);\n    }\n\n    int updatedRecords =\n        execute(\n            err -> {\n              if (JdbcUtil.isConstraintViolation(err)) {\n                throw new AlreadyExistsException(\"Table already exists: %s\", to);\n              }\n            },\n            (schemaVersion == JdbcUtil.SchemaVersion.V1)\n                ? JdbcUtil.V1_RENAME_TABLE_SQL\n                : JdbcUtil.V0_RENAME_TABLE_SQL,\n            JdbcUtil.namespaceToString(to.namespace()),\n            to.name(),","sourceCodeStart":351,"sourceCodeEnd":387,"githubUrl":"https://github.com/apache/iceberg/blob/86d9c8fc543e7c56c9f624eb725f76c9baff9570/core/src/main/java/org/apache/iceberg/jdbc/JdbcCatalog.java#L351-L387","documentation":"JdbcCatalog.renameTable throws AlreadyExistsException when renaming a table to a name that collides with an existing view under the V1 schema. This guards the single-catalog-namespace against name collisions between tables and views in V1-backed JDBC catalogs. The rename is aborted before any SQL update is issued.","triggerScenarios":"Calling catalog.renameTable(from, to) where 'to' names an existing view and the JDBC catalog schemaVersion is V1 (V1_RENAME_TABLE_SQL used, catalog_property 'catalog-table'/'catalog-view' separated).","commonSituations":"Migrating catalogs where a view was created at the target name; upgrading or downgrading JDBC schema versions where V1 separated views and tables but identifiers still overlap; concurrent creation of a view between the caller's exists-check and rename.","solutions":["Choose a different target TableIdentifier that does not collide with an existing view","Drop or rename the existing view at the target name first, then retry renameTable","If view/table sharing a name is intended, migrate the catalog to the V0 (shared-identifier) schema version where this check is not applied"],"exampleFix":"// before\ncatalog.renameTable(from, TableIdentifier.of(ns, \"report\")); // fails: view 'report' exists\n// after\nif (!catalog.listViews(ns).contains(TableIdentifier.of(ns, \"report\"))) {\n  catalog.renameTable(from, TableIdentifier.of(ns, \"report\"));\n}","handlingStrategy":"try-catch","validationCode":"if (catalog instanceof JdbcCatalog && catalog.listViews(to.namespace()).contains(to)) { throw new IllegalStateException(\"target collides with view\"); }","typeGuard":"boolean targetIsFree = !catalog.tableExists(to) && !catalog.listViews(to.namespace()).contains(to);","tryCatchPattern":"try { catalog.renameTable(from, to); } catch (AlreadyExistsException e) { /* pick a new target name or drop the view */ }","preventionTips":["Check both tableExists and listViews for the target identifier before renaming","Avoid giving views and tables identical names in shared namespaces","Document the catalog schema version in use since V0 and V1 differ on this check"],"tags":["jdbc-catalog","already-exists","rename"],"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"}