{"record":{"id":"57f17f482e604b1d","repo":"apache/iceberg","slug":"view-with-same-name-already-exists-s-57f17f","errorCode":null,"errorMessage":"View with same name already exists: %s","messagePattern":"View 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":911,"sourceCode":"  /**\n   * The purpose of this class is to add view detection only for Hive-Specific tables. Hive catalog\n   * follows checks at different levels: 1. During refresh, it validates if the table is an iceberg\n   * table or not. 2. During commit, it validates if there is any concurrent commit with table or\n   * table-name already exists. This class helps to do the validation on an early basis.\n   */\n  private class ViewAwareTableBuilder extends BaseMetastoreViewCatalogTableBuilder {\n\n    private final TableIdentifier identifier;\n\n    private ViewAwareTableBuilder(TableIdentifier identifier, Schema schema) {\n      super(identifier, schema);\n      this.identifier = identifier;\n    }\n\n    @Override\n    public Transaction createOrReplaceTransaction() {\n      if (viewExists(identifier)) {\n        throw new AlreadyExistsException(\"View with same name already exists: %s\", identifier);\n      }\n      return super.createOrReplaceTransaction();\n    }\n\n    @Override\n    public org.apache.iceberg.Table create() {\n      if (viewExists(identifier)) {\n        throw new AlreadyExistsException(\"View with same name already exists: %s\", identifier);\n      }\n      return super.create();\n    }\n  }\n\n  /**\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.","sourceCodeStart":893,"sourceCodeEnd":929,"githubUrl":"https://github.com/apache/iceberg/blob/86d9c8fc543e7c56c9f624eb725f76c9baff9570/hive-metastore/src/main/java/org/apache/iceberg/hive/HiveCatalog.java#L893-L929","documentation":"HiveCatalog's view-builder createOrReplaceTransaction() throws AlreadyExistsException when a view already exists under the target identifier. Because the requested operation is a create-or-replace transaction, an existing view of the same name is a hard conflict: replace transactions refuse to silently overwrite a pre-existing view. The check runs before the transaction is opened so no partial metadata is written.","triggerScenarios":"Calling HiveCatalog.buildView(identifier).createOrReplaceTransaction() (or createOrReplace()) when a view with the same namespace-qualified name already exists in the Hive Metastore. The viewExists(identifier) check inside HiveCatalog's ViewBuilder returns true immediately before super.createOrReplaceTransaction() runs.","commonSituations":"Double-submitting a view-creation job; concurrent pipelines racing to create the same view name; re-running idempotent-expected deployment scripts that don't first check existence; a table and view name collision where an older view was left behind after a migration.","solutions":["Delete or rename the existing view first: catalog.dropView(identifier), then re-run createOrReplaceTransaction().","Guard the call with if (!catalog.viewExists(identifier)) before creating, or wrap in try-catch for AlreadyExistsException and treat it as a no-op.","If the intent is to replace a table with a view (or vice versa), drop the conflicting object explicitly since create-or-replace only replaces same-type objects.","Use locking or a single writer for concurrent view creation jobs to avoid races."],"exampleFix":"// before\ncatalog.buildView(identifier).createOrReplaceTransaction();\n// after\nif (catalog.viewExists(identifier)) {\n  catalog.dropView(identifier); // or skip creation if the view is current\n}\ncatalog.buildView(identifier).createOrReplaceTransaction();","handlingStrategy":"validation","validationCode":"if (catalog.viewExists(identifier)) {\n  // handle existing view before createOrReplaceTransaction()\n}","typeGuard":null,"tryCatchPattern":"try {\n  catalog.buildView(identifier).createOrReplaceTransaction();\n} catch (AlreadyExistsException e) {\n  // view already exists: skip, drop, or reconcile\n}","preventionTips":["Check viewExists before any create-or-replace call","Make deployment scripts idempotent with explicit drop-or-skip logic","Serialize concurrent view creation with external locking or single-writer jobs"],"tags":["hive","catalog","view","already-exists","conflict"],"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"}