apache/iceberg · error · NoSuchNamespaceException

NoSuchNamespaceException(currentNamespace)

Error message

NoSuchNamespaceException(currentNamespace)

What it means

createView catches Iceberg's NoSuchNamespaceException raised while creating the view (e.g. the target database does not exist) and rethrows Spark's NoSuchNamespaceException for currentNamespace. The view CREATE cannot proceed because its parent namespace is missing in the catalog.

Solutions

  1. Create the namespace first: CREATE NAMESPACE catalog.db (or CREATE DATABASE IF NOT EXISTS)
  2. Verify the database name with SHOW DATABASES IN catalog and fix typos
  3. Ensure deployment/migration ordering creates schemas before views
  4. Confirm you are pointed at the right catalog/environment where the namespace exists

Example fix

// before
CREATE VIEW prod.analytics.revenue_view AS SELECT ...; // prod.analytics missing
// after
CREATE NAMESPACE IF NOT EXISTS prod.analytics;
CREATE VIEW prod.analytics.revenue_view AS SELECT ...;
Defensive patterns

Strategy: validation

Validate before calling

boolean nsExists = spark.sql("SHOW DATABASES IN " + catalog).collectAsList().stream()
    .anyMatch(r -> r.getString(0).equalsIgnoreCase(db));
if (!nsExists) { spark.sql("CREATE NAMESPACE IF NOT EXISTS " + catalog + "." + db); }

Try / catch

try {
  spark.sql("CREATE VIEW " + catalog + "." + db + "." + name + " AS SELECT ...;");
} catch (org.apache.spark.sql.catalyst.analysis.NoSuchNamespaceException e) {
  spark.sql("CREATE NAMESPACE " + catalog + "." + db);
  spark.sql("CREATE VIEW " + catalog + "." + db + "." + name + " AS SELECT ...;");
}

Prevention

When it happens

Trigger: CREATE VIEW catalog.db.name (via createView) where the ViewBuilder .create() throws org.apache.iceberg.exceptions.NoSuchNamespaceException because namespace `db` does not exist; also when asViewCatalog is null — though that path instead throws UnsupportedOperationException (see 2647).

Common situations: Creating a view in a database/namespace that was never created (or was dropped); typo in the database part of the qualified name; migration scripts that create views before creating the schema; cross-environment drift where the namespace exists in dev but not prod.

Understand the failure class

Background: "Not found" and "does not exist" errors: why "Task not found", "No such folder", and "Can't find" fire when a lookup comes back empty — this error's family across 14 libraries.

Related errors


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

Appendix: source

Thrown at spark/v4.0/spark/src/main/java/org/apache/iceberg/spark/SparkCatalog.java:638

        Map<String, String> props =
            ImmutableMap.<String, String>builder()
                .putAll(Spark3Util.rebuildCreateProperties(properties))
                .put(SparkView.QUERY_COLUMN_NAMES, COMMA_JOINER.join(queryColumnNames))
                .buildKeepingLast();

        org.apache.iceberg.view.View view =
            asViewCatalog
                .buildView(buildIdentifier(ident))
                .withDefaultCatalog(currentCatalog)
                .withDefaultNamespace(Namespace.of(currentNamespace))
                .withQuery("spark", sql)
                .withSchema(icebergSchema)
                .withLocation(properties.get("location"))
                .withProperties(props)
                .create();
        return new SparkView(catalogName, view);
      } catch (org.apache.iceberg.exceptions.NoSuchNamespaceException e) {
        throw new NoSuchNamespaceException(currentNamespace);
      } catch (AlreadyExistsException e) {
        throw new ViewAlreadyExistsException(ident);
      }
    }

    throw new UnsupportedOperationException(
        "Creating a view is not supported by catalog: " + catalogName);
  }

  @Override
  public View replaceView(
      Identifier ident,
      String sql,
      String currentCatalog,
      String[] currentNamespace,
      StructType schema,
      String[] queryColumnNames,
      String[] columnAliases,

View on GitHub (pinned to 86d9c8fc54)