{"record":{"id":"c6c4963e2171ab31","repo":"apache/iceberg","slug":"nosuchnamespaceexception-currentnamespace","errorCode":null,"errorMessage":"NoSuchNamespaceException(currentNamespace)","messagePattern":"NoSuchNamespaceException\\(currentNamespace\\)","errorType":"exception","errorClass":"NoSuchNamespaceException","httpStatus":null,"severity":"error","filePath":"spark/v4.0/spark/src/main/java/org/apache/iceberg/spark/SparkCatalog.java","lineNumber":638,"sourceCode":"        Map<String, String> props =\n            ImmutableMap.<String, String>builder()\n                .putAll(Spark3Util.rebuildCreateProperties(properties))\n                .put(SparkView.QUERY_COLUMN_NAMES, COMMA_JOINER.join(queryColumnNames))\n                .buildKeepingLast();\n\n        org.apache.iceberg.view.View view =\n            asViewCatalog\n                .buildView(buildIdentifier(ident))\n                .withDefaultCatalog(currentCatalog)\n                .withDefaultNamespace(Namespace.of(currentNamespace))\n                .withQuery(\"spark\", sql)\n                .withSchema(icebergSchema)\n                .withLocation(properties.get(\"location\"))\n                .withProperties(props)\n                .create();\n        return new SparkView(catalogName, view);\n      } catch (org.apache.iceberg.exceptions.NoSuchNamespaceException e) {\n        throw new NoSuchNamespaceException(currentNamespace);\n      } catch (AlreadyExistsException e) {\n        throw new ViewAlreadyExistsException(ident);\n      }\n    }\n\n    throw new UnsupportedOperationException(\n        \"Creating a view is not supported by catalog: \" + catalogName);\n  }\n\n  @Override\n  public View replaceView(\n      Identifier ident,\n      String sql,\n      String currentCatalog,\n      String[] currentNamespace,\n      StructType schema,\n      String[] queryColumnNames,\n      String[] columnAliases,","sourceCodeStart":620,"sourceCodeEnd":656,"githubUrl":"https://github.com/apache/iceberg/blob/86d9c8fc543e7c56c9f624eb725f76c9baff9570/spark/v4.0/spark/src/main/java/org/apache/iceberg/spark/SparkCatalog.java#L620-L656","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Create the namespace first: CREATE NAMESPACE catalog.db (or CREATE DATABASE IF NOT EXISTS)","Verify the database name with SHOW DATABASES IN catalog and fix typos","Ensure deployment/migration ordering creates schemas before views","Confirm you are pointed at the right catalog/environment where the namespace exists"],"exampleFix":"// before\nCREATE VIEW prod.analytics.revenue_view AS SELECT ...; // prod.analytics missing\n// after\nCREATE NAMESPACE IF NOT EXISTS prod.analytics;\nCREATE VIEW prod.analytics.revenue_view AS SELECT ...;","handlingStrategy":"validation","validationCode":"boolean nsExists = spark.sql(\"SHOW DATABASES IN \" + catalog).collectAsList().stream()\n    .anyMatch(r -> r.getString(0).equalsIgnoreCase(db));\nif (!nsExists) { spark.sql(\"CREATE NAMESPACE IF NOT EXISTS \" + catalog + \".\" + db); }","typeGuard":null,"tryCatchPattern":"try {\n  spark.sql(\"CREATE VIEW \" + catalog + \".\" + db + \".\" + name + \" AS SELECT ...;\");\n} catch (org.apache.spark.sql.catalyst.analysis.NoSuchNamespaceException e) {\n  spark.sql(\"CREATE NAMESPACE \" + catalog + \".\" + db);\n  spark.sql(\"CREATE VIEW \" + catalog + \".\" + db + \".\" + name + \" AS SELECT ...;\");\n}","preventionTips":["Create namespaces before views in migration scripts (schema-first ordering)","Use CREATE NAMESPACE IF NOT EXISTS in bootstrap jobs","Verify the namespace exists with SHOW DATABASES IN <catalog>","Align namespace names across dev/staging/prod environments"],"tags":["spark","view","namespace","create"],"backgroundTag":"entity-not-found","analyzedSha":"86d9c8fc543e7c56c9f624eb725f76c9baff9570","analyzedAt":"2026-09-12T00:46:39.097Z","contentChangedAt":"2026-09-12T00:46:39.097Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}