{"record":{"id":"ba40b824c648eb86","repo":"apache/iceberg","slug":"tablealreadyexistsexception-ident-ba40b8","errorCode":null,"errorMessage":"TableAlreadyExistsException(ident)","messagePattern":"TableAlreadyExistsException\\(ident\\)","errorType":"exception","errorClass":"TableAlreadyExistsException","httpStatus":409,"severity":"error","filePath":"spark/v4.2/spark/src/main/java/org/apache/iceberg/spark/SparkSessionCatalog.java","lineNumber":664,"sourceCode":"    if (null != asViewCatalog && asViewCatalog.viewExists(fromIdentifier)) {\n      asViewCatalog.renameView(fromIdentifier, toIdentifier);\n    } else if (isViewCatalog()) {\n      getSessionCatalog().renameView(fromIdentifier, toIdentifier);\n    } else {\n      throw new UnsupportedOperationException(\n          \"Renaming a view is not supported by catalog: \" + catalogName);\n    }\n  }\n\n  private void checkTableNotExists(Identifier ident) throws ViewAlreadyExistsException {\n    if (tableExists(ident)) {\n      throw new ViewAlreadyExistsException(ident);\n    }\n  }\n\n  private void checkViewNotExists(Identifier ident) throws TableAlreadyExistsException {\n    if (viewExists(ident)) {\n      throw new TableAlreadyExistsException(ident);\n    }\n  }\n}\n","sourceCodeStart":646,"sourceCodeEnd":668,"githubUrl":"https://github.com/apache/iceberg/blob/86d9c8fc543e7c56c9f624eb725f76c9baff9570/spark/v4.2/spark/src/main/java/org/apache/iceberg/spark/SparkSessionCatalog.java#L646-L668","documentation":"checkViewNotExists throws TableAlreadyExistsException when creating or renaming a table whose target identifier is already occupied by a view. This guards the reverse collision of checkTableNotExists: a view exists where a table is being created. Iceberg treats views and tables as sharing one identifier space in the Spark session catalog.","triggerScenarios":"createTable/stageCreate targeting an identifier where a view exists; renameTable moving a table onto an existing view's name.","commonSituations":"Replacing a former view with a table without dropping it first; automated pipelines generating colliding names; restore/migration tooling that recreates tables where DDL drift left views.","solutions":["Drop the view at that identifier before creating/renaming the table","Use a different table name","Check catalog.viewExists(ident) first and handle the collision explicitly","Ensure catalog names are unique across your table and view provisioning scripts"],"exampleFix":"// before\ncatalog.createTable(ident, schema); // TableAlreadyExistsException if ident is a view\n// after\nif (catalog.viewExists(ident)) {\n  catalog.dropView(ident);\n}\ncatalog.createTable(ident, schema);","handlingStrategy":"validation","validationCode":"if (catalog.viewExists(ident)) {\n  throw new IllegalStateException(\"View \" + ident + \" blocks table creation at this name\");\n}","typeGuard":"boolean nameFreeForTable = !catalog.viewExists(ident);","tryCatchPattern":"try {\n  catalog.createTable(ident, schema, spec);\n} catch (org.apache.iceberg.exceptions.TableAlreadyExistsException e) {\n  // drop the view or pick another identifier\n}","preventionTips":["Check viewExists before createTable/renameTable","Keep provisioning scripts aware that tables and views share a namespace","Run collision checks in migration/restore tooling","Use CREATE OR REPLACE semantics deliberately, not accidentally"],"tags":["spark","catalog","tables","views","name-collision"],"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"}