{"record":{"id":"7a23a2fc57d2f3d2","repo":"apache/iceberg","slug":"cannot-rename-s-to-s-view-already-exists-7a23a2","errorCode":null,"errorMessage":"Cannot rename %s to %s. View already exists","messagePattern":"Cannot rename (.+?) to (.+?)\\. View already exists","errorType":"exception","errorClass":"AlreadyExistsException","httpStatus":409,"severity":"error","filePath":"hive-metastore/src/main/java/org/apache/iceberg/hive/HiveCatalog.java","lineNumber":410,"sourceCode":"  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(\n          client -> {\n            MetastoreUtil.alterTable(client, fromDatabase, fromName, table);\n            return null;\n          });","sourceCodeStart":392,"sourceCodeEnd":428,"githubUrl":"https://github.com/apache/iceberg/blob/86d9c8fc543e7c56c9f624eb725f76c9baff9570/hive-metastore/src/main/java/org/apache/iceberg/hive/HiveCatalog.java#L392-L428","documentation":"renameTableOrView() throws AlreadyExistsException when a view already exists at the rename destination identifier. Since Iceberg tables and views share the Hive Metastore namespace (distinguished by table type), a view at the destination also blocks a rename.","triggerScenarios":"Calling catalog.renameTable(from, to) or catalog.renameView(from, to) where to passes the tableExists check but a Hive view (ICEBERG_VIEW type) named to.name() exists in to.namespace().","commonSituations":"A view with the intended table name exists (views and tables share the HMS name space); leftover views from earlier experiments; concurrent creation of a view at the destination; case-insensitive name collisions.","solutions":["Check catalog.viewExists(to) before renaming and choose a different destination name.","Drop the conflicting view first if it is no longer needed.","In scripts, check both tableExists and viewExists for the destination.","Use unique destination names to avoid races with concurrent view creation."],"exampleFix":"// before\ncatalog.renameView(from, TableIdentifier.of(db, \"summary\"));\n// after\nTableIdentifier to = TableIdentifier.of(db, \"summary\");\nif (catalog.viewExists(to) || catalog.tableExists(to)) {\n  throw new IllegalStateException(\"Destination name in use: \" + to);\n}\ncatalog.renameView(from, to);","handlingStrategy":"validation","validationCode":"TableIdentifier to = TableIdentifier.of(db, \"summary\");\nif (catalog.viewExists(to) || catalog.tableExists(to)) {\n  throw new IllegalStateException(\"Destination name in use: \" + to);\n}\ncatalog.renameView(from, to);","typeGuard":null,"tryCatchPattern":"try {\n  catalog.renameView(from, to);\n} catch (AlreadyExistsException e) {\n  // resolve view/table name collision, then retry\n  throw e;\n}","preventionTips":["Check both viewExists and tableExists — tables and views share the HMS name space.","Avoid reuse of historical names for tables/views without cleaning up first.","Serialize rename workflows to prevent concurrent creations at the destination."],"tags":["hive-metastore","rename","views","already-exists"],"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"}