{"record":{"id":"fbd5506b03a82693","repo":"apache/iceberg","slug":"view-already-exists-toidentifier-fbd550","errorCode":null,"errorMessage":"View already exists: ${toIdentifier}","messagePattern":"View already exists: (.+?)","errorType":"exception","errorClass":"ViewAlreadyExistsException","httpStatus":null,"severity":"error","filePath":"spark/v4.1/spark/src/main/java/org/apache/iceberg/spark/SparkCatalog.java","lineNumber":760,"sourceCode":"  @Override\n  public boolean dropView(Identifier ident) {\n    if (null != asViewCatalog) {\n      return asViewCatalog.dropView(buildIdentifier(ident));\n    }\n\n    return false;\n  }\n\n  @Override\n  public void renameView(Identifier fromIdentifier, Identifier toIdentifier)\n      throws NoSuchViewException, ViewAlreadyExistsException {\n    if (null != asViewCatalog) {\n      try {\n        asViewCatalog.renameView(buildIdentifier(fromIdentifier), buildIdentifier(toIdentifier));\n      } catch (org.apache.iceberg.exceptions.NoSuchViewException e) {\n        throw new NoSuchViewException(fromIdentifier);\n      } catch (org.apache.iceberg.exceptions.AlreadyExistsException e) {\n        throw new ViewAlreadyExistsException(toIdentifier);\n      }\n    } else {\n      throw new UnsupportedOperationException(\n          \"Renaming a view is not supported by catalog: \" + catalogName);\n    }\n  }\n\n  @Override\n  public final void initialize(String name, CaseInsensitiveStringMap options) {\n    super.initialize(name, options);\n\n    boolean cacheEnabled =\n        PropertyUtil.propertyAsBoolean(\n            options, CatalogProperties.CACHE_ENABLED, CatalogProperties.CACHE_ENABLED_DEFAULT);\n\n    boolean cacheCaseSensitive =\n        PropertyUtil.propertyAsBoolean(\n            options,","sourceCodeStart":742,"sourceCodeEnd":778,"githubUrl":"https://github.com/apache/iceberg/blob/86d9c8fc543e7c56c9f624eb725f76c9baff9570/spark/v4.1/spark/src/main/java/org/apache/iceberg/spark/SparkCatalog.java#L742-L778","documentation":"renameView() maps a core AlreadyExistsException to Spark's ViewAlreadyExistsException naming the destination identifier. The rename target already holds a view (or another catalog object) with that name, and the catalog refuses to overwrite it.","triggerScenarios":"Calling SparkCatalog.renameView(fromIdentifier, toIdentifier) when ViewCatalog.renameView throws org.apache.iceberg.exceptions.AlreadyExistsException because buildIdentifier(toIdentifier) already exists.","commonSituations":"Re-running a non-idempotent rename migration script; renaming to a name that already exists in the target namespace; concurrent jobs creating the destination view.","solutions":["Drop or rename the existing destination view first, then retry","Choose a unique destination name for the rename","Make the script idempotent: check existence of the target before renaming"],"exampleFix":"// before\nspark.sql(\"ALTER VIEW analytics.v1 RENAME TO analytics.v2\");\n// after\nspark.sql(\"DROP VIEW IF EXISTS analytics.v2\");\nspark.sql(\"ALTER VIEW analytics.v1 RENAME TO analytics.v2\");","handlingStrategy":"try-catch","validationCode":"// Ensure destination name is free before renaming\nboolean targetExists = ((org.apache.iceberg.catalog.ViewCatalog) catalog)\n    .viewExists(org.apache.iceberg.catalog.TableIdentifier.of(toNs, toName));\nif (targetExists) {\n  throw new IllegalStateException(\"View already exists: \" + toName);\n}","typeGuard":"boolean isFree(org.apache.iceberg.catalog.ViewCatalog vc, Identifier ident) {\n  return !vc.viewExists(org.apache.iceberg.catalog.TableIdentifier.of(ident.namespace().levels(), ident.name()));\n}","tryCatchPattern":"try {\n  catalog.renameView(from, to);\n} catch (ViewAlreadyExistsException e) {\n  logger.error(\"Destination view {} already exists; drop it or pick another name\", to, e);\n}","preventionTips":["Check existence of the destination (viewExists / SHOW VIEWS) before renaming","Guard against re-runs of migration scripts by tracking applied renames","Avoid concurrent jobs renaming into the same destination name; serialize DDL on a namespace level"],"tags":["spark","view","rename","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"}