{"record":{"id":"07c7abf98f5c3c4e","repo":"apache/iceberg","slug":"viewalreadyexistsexception-toidentifier","errorCode":null,"errorMessage":"ViewAlreadyExistsException(toIdentifier)","messagePattern":"ViewAlreadyExistsException\\(toIdentifier\\)","errorType":"exception","errorClass":"ViewAlreadyExistsException","httpStatus":null,"severity":"error","filePath":"spark/v4.0/spark/src/main/java/org/apache/iceberg/spark/SparkCatalog.java","lineNumber":756,"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    this.cacheEnabled =\n        PropertyUtil.propertyAsBoolean(\n            options, CatalogProperties.CACHE_ENABLED, CatalogProperties.CACHE_ENABLED_DEFAULT);\n\n    boolean cacheCaseSensitive =\n        PropertyUtil.propertyAsBoolean(\n            options,","sourceCodeStart":738,"sourceCodeEnd":774,"githubUrl":"https://github.com/apache/iceberg/blob/86d9c8fc543e7c56c9f624eb725f76c9baff9570/spark/v4.0/spark/src/main/java/org/apache/iceberg/spark/SparkCatalog.java#L738-L774","documentation":"In renameView, when the underlying ViewCatalog throws AlreadyExistsException for the destination identifier, SparkCatalog translates it to a Spark ViewAlreadyExistsException carrying toIdentifier. The rename fails because a view with the target name already exists; catalogs refuse to silently overwrite it.","triggerScenarios":"ALTER VIEW src RENAME TO dst where 'dst' already names an existing view, or a rename colliding with a name created concurrently.","commonSituations":"Re-running an idempotent migration script; rename target chosen without checking existing views; two pipelines renaming to the same destination name.","solutions":["Choose a unique target name after checking SHOW VIEWS in the target namespace.","Drop the existing destination view explicitly if replacement is intended, then rename.","Add existence checks to make migration scripts idempotent."],"exampleFix":"// before\nspark.sql(\"ALTER VIEW analytics.v RENAME TO analytics.old\") // old exists\n// after\nspark.sql(\"DROP VIEW IF EXISTS analytics.old\")\nspark.sql(\"ALTER VIEW analytics.v RENAME TO analytics.old\")","handlingStrategy":"try-catch","validationCode":"if (sparkCatalog.viewExists(toIdent)) {\n  throw new IllegalStateException(\"Destination view already exists: \" + toIdent);\n}","typeGuard":null,"tryCatchPattern":"try {\n  sparkCatalog.renameView(from, to);\n} catch (ViewAlreadyExistsException e) {\n  LOG.error(\"Rename target {} already exists\", to, e); // pick new name or drop first\n}","preventionTips":["Check destination name availability before renaming.","Make migration scripts idempotent with DROP VIEW IF EXISTS.","Serialize renames across concurrent pipelines to avoid races."],"tags":["spark","view","rename","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"}