apache/iceberg · error · ViewAlreadyExistsException

View already exists: toIdentifier

Error message

View already exists: toIdentifier

What it means

Thrown by SparkCatalog.renameView when the underlying view catalog throws AlreadyExistsException for the target identifier, meaning a view (or conflicting object) named toIdentifier already exists. The source view is left unchanged and the rename fails atomically.

Source

Thrown at spark/v3.5/spark/src/main/java/org/apache/iceberg/spark/SparkCatalog.java:757

  @Override
  public boolean dropView(Identifier ident) {
    if (null != asViewCatalog) {
      return asViewCatalog.dropView(buildIdentifier(ident));
    }

    return false;
  }

  @Override
  public void renameView(Identifier fromIdentifier, Identifier toIdentifier)
      throws NoSuchViewException, ViewAlreadyExistsException {
    if (null != asViewCatalog) {
      try {
        asViewCatalog.renameView(buildIdentifier(fromIdentifier), buildIdentifier(toIdentifier));
      } catch (org.apache.iceberg.exceptions.NoSuchViewException e) {
        throw new NoSuchViewException(fromIdentifier);
      } catch (org.apache.iceberg.exceptions.AlreadyExistsException e) {
        throw new ViewAlreadyExistsException(toIdentifier);
      }
    } else {
      throw new UnsupportedOperationException(
          "Renaming a view is not supported by catalog: " + catalogName);
    }
  }

  @Override
  public final void initialize(String name, CaseInsensitiveStringMap options) {
    super.initialize(name, options);

    this.cacheEnabled =
        PropertyUtil.propertyAsBoolean(
            options, CatalogProperties.CACHE_ENABLED, CatalogProperties.CACHE_ENABLED_DEFAULT);

    boolean cacheCaseSensitive =
        PropertyUtil.propertyAsBoolean(
            options,

View on GitHub (pinned to 86d9c8fc54)

Solutions

  1. Drop or rename the existing target view first, or choose a different target name.
  2. Check existence with SHOW VIEWS / viewExists(toIdentifier) before renaming.
  3. If the collision is unintended (stale artifact), drop the stale view and retry.
  4. For idempotent pipelines, wrap the rename in logic that treats 'target already equals source' as success.

Example fix

// before
spark.sql("ALTER VIEW analytics.daily RENAME TO analytics.backup");
// after
spark.sql("DROP VIEW IF EXISTS analytics.backup");
spark.sql("ALTER VIEW analytics.daily RENAME TO analytics.backup");
Defensive patterns

Strategy: validation

Validate before calling

if (catalog.viewExists(toIdentifier)) { throw new IllegalStateException("target view already exists: " + toIdentifier); }

Try / catch

try { catalog.renameView(from, to); } catch (ViewAlreadyExistsException e) { catalog.dropView(to); catalog.renameView(from, to); }

Prevention

When it happens

Trigger: Calling renameView(fromIdentifier, toIdentifier) where a view named toIdentifier already exists in the view catalog; the catalog's renameView rejects the name collision and SparkCatalog maps it to ViewAlreadyExistsException.

Common situations: Renaming a view to a name that already exists (e.g. daily -> daily_backup where daily_backup exists); rerunning an idempotent rename script after a previous partial success; case-insensitive filesystems or catalogs where the names collide after normalization.

Understand the failure class

Background: "already exists" / EEXIST / FileAlreadyExistsException: what the 'file already exists' error means and how to fix it — this error's family across 37 libraries.

Related errors


AI-assisted analysis of apache/iceberg@86d9c8fc54 (2026-09-12). Data as JSON: /api/errors/1930f50f472a4ee2. Report an issue: GitHub.