{"record":{"id":"33eadeaae3cc8e83","repo":"apache/iceberg","slug":"table-with-same-name-already-exists-s-33eade","errorCode":null,"errorMessage":"Table with same name already exists: %s","messagePattern":"Table with same name already exists: (.+?)","errorType":"exception","errorClass":"AlreadyExistsException","httpStatus":409,"severity":"error","filePath":"hive-metastore/src/main/java/org/apache/iceberg/hive/HiveCatalog.java","lineNumber":943,"sourceCode":"  /**\n   * The purpose of this class is to add table detection only for Hive-Specific view. Hive catalog\n   * follows checks at different levels: 1. During refresh, it validates if the view is an iceberg\n   * view or not. 2. During commit, it validates if there is any concurrent commit with view or\n   * view-name already exists. This class helps to do the validation on an early basis.\n   */\n  private class TableAwareViewBuilder extends BaseViewBuilder {\n\n    private final TableIdentifier identifier;\n\n    private TableAwareViewBuilder(TableIdentifier identifier) {\n      super(identifier);\n      this.identifier = identifier;\n    }\n\n    @Override\n    public View createOrReplace() {\n      if (tableExists(identifier)) {\n        throw new AlreadyExistsException(\"Table with same name already exists: %s\", identifier);\n      }\n      return super.createOrReplace();\n    }\n\n    @Override\n    public View create() {\n      if (tableExists(identifier)) {\n        throw new AlreadyExistsException(\"Table with same name already exists: %s\", identifier);\n      }\n      return super.create();\n    }\n  }\n\n  /**\n   * Register a table with the catalog if it does not exist. This is overridden in order to add view\n   * existence detection before registering a table.\n   *\n   * @param identifier a table identifier","sourceCodeStart":925,"sourceCodeEnd":961,"githubUrl":"https://github.com/apache/iceberg/blob/86d9c8fc543e7c56c9f624eb725f76c9baff9570/hive-metastore/src/main/java/org/apache/iceberg/hive/HiveCatalog.java#L925-L961","documentation":"HiveCatalog's table-builder createOrReplace() throws AlreadyExistsException when a table (not a view) already exists under the target identifier. createOrReplace on a view builder refuses to clobber an existing table — replacing a table with a view would destroy data, so it is a hard error. The tableExists check runs before any replacement metadata is committed.","triggerScenarios":"Calling HiveCatalog.buildView(identifier).createOrReplace() when a table with the same namespace/name exists (tableExists(identifier) is true). Typically a name collision where the same logical name was previously created as a table.","commonSituations":"Migrating a dataset from a table to a view without dropping the table first; a naming convention change that collides an old table name with a new view name; automated scripts reusing one identifier for both artifacts.","solutions":["Drop the existing table explicitly: catalog.dropTable(identifier) (consider purge semantics) before creating the view.","Choose a distinct identifier for the view so it doesn't collide with the table.","Catch AlreadyExistsException and surface a clear operator message about the table/view name collision.","Audit the namespace for name collisions before running view-creation migrations."],"exampleFix":"// before\ncatalog.buildView(identifier).createOrReplace(); // collides with existing table\n// after\nif (catalog.tableExists(identifier)) {\n  catalog.dropTable(identifier);\n}\ncatalog.buildView(identifier).createOrReplace();","handlingStrategy":"validation","validationCode":"if (catalog.tableExists(identifier)) {\n  throw new IllegalStateException(\"Name \" + identifier + \" is occupied by a table; cannot create view\");\n}","typeGuard":null,"tryCatchPattern":"try {\n  catalog.buildView(identifier).createOrReplace();\n} catch (AlreadyExistsException e) {\n  // drop the table explicitly after human confirmation, then retry\n}","preventionTips":["Never reuse the same identifier for tables and views","Audit namespaces for name collisions before migrations","Drop the old table explicitly when converting a table to a view"],"tags":["hive","catalog","view","table-conflict","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-14T16:17:12.679Z"}