{"record":{"id":"fe09a05129b3fc56","repo":"apache/iceberg","slug":"cannot-create-table-s-as-the-namespace-does-not-e","errorCode":null,"errorMessage":"Cannot create table %s as the namespace does not exist","messagePattern":"Cannot create table (.+?) as the namespace does not exist","errorType":"exception","errorClass":"NoSuchNamespaceException","httpStatus":null,"severity":"error","filePath":"spark/v3.5/spark/src/main/java/org/apache/iceberg/spark/actions/BaseTableCreationSparkAction.java","lineNumber":164,"sourceCode":"        \"Cannot create Iceberg table in non-Iceberg Catalog. \"\n            + \"Catalog '%s' was of class '%s' but '%s' or '%s' are required\",\n        catalog.name(),\n        catalog.getClass().getName(),\n        SparkSessionCatalog.class.getName(),\n        SparkCatalog.class.getName());\n\n    return (StagingTableCatalog) catalog;\n  }\n\n  protected StagedSparkTable stageDestTable() {\n    try {\n      Map<String, String> props = destTableProps();\n      StructType schema = sourceTable.schema();\n      Transform[] partitioning = sourceTable.partitioning();\n      return (StagedSparkTable)\n          destCatalog().stageCreate(destTableIdent(), schema, partitioning, props);\n    } catch (org.apache.spark.sql.catalyst.analysis.NoSuchNamespaceException e) {\n      throw new NoSuchNamespaceException(\n          \"Cannot create table %s as the namespace does not exist\", destTableIdent());\n    } catch (org.apache.spark.sql.catalyst.analysis.TableAlreadyExistsException e) {\n      throw new AlreadyExistsException(\n          \"Cannot create table %s as it already exists\", destTableIdent());\n    }\n  }\n\n  protected void ensureNameMappingPresent(Table table) {\n    if (!table.properties().containsKey(TableProperties.DEFAULT_NAME_MAPPING)) {\n      NameMapping nameMapping = MappingUtil.create(table.schema());\n      String nameMappingJson = NameMappingParser.toJson(nameMapping);\n      table.updateProperties().set(TableProperties.DEFAULT_NAME_MAPPING, nameMappingJson).commit();\n    }\n  }\n\n  protected String getMetadataLocation(Table table) {\n    String defaultValue =\n        LocationUtil.stripTrailingSlash(table.location()) + \"/\" + ICEBERG_METADATA_FOLDER;","sourceCodeStart":146,"sourceCodeEnd":182,"githubUrl":"https://github.com/apache/iceberg/blob/86d9c8fc543e7c56c9f624eb725f76c9baff9570/spark/v3.5/spark/src/main/java/org/apache/iceberg/spark/actions/BaseTableCreationSparkAction.java#L146-L182","documentation":"In stageDestTable, when destCatalog().stageCreate fails with Spark's NoSuchNamespaceException, BaseTableCreationSparkAction translates it into Iceberg's NoSuchNamespaceException stating the destination namespace does not exist. Iceberg does not auto-create namespaces during staged table creation.","triggerScenarios":"Executing snapshot/migrate/addFiles with a destination identifier whose namespace (database) has not been created in the destination catalog.","commonSituations":"Creating the destination in a fresh catalog where the namespace was never created; typo in destination namespace; Hadoop/Hive catalog pointing at a filesystem location without the namespace directory.","solutions":["Create the destination namespace first, e.g. spark.sql(\"CREATE NAMESPACE iceberg_catalog.ns\")","Verify the namespace name spelling and catalog in the destination identifier","If using a Hadoop catalog, ensure the base warehouse location is writable so the namespace can be created"],"exampleFix":"// before\nSparkActions.get(spark).snapshot(\"spark_catalog\", \"db.src\", \"iceberg.newns.dest\").execute();\n// after\nspark.sql(\"CREATE NAMESPACE IF NOT EXISTS iceberg_catalog.newns\");\nSparkActions.get(spark).snapshot(\"spark_catalog\", \"db.src\", \"iceberg_catalog.newns.dest\").execute();","handlingStrategy":"validation","validationCode":"boolean nsExists = spark.sql(\"SHOW NAMESPACES IN \" + destCatalog)\n    .collectAsList().stream().anyMatch(r -> r.getString(0).equalsIgnoreCase(namespace));\nif (!nsExists) spark.sql(\"CREATE NAMESPACE \" + destCatalog + \".\" + namespace);","typeGuard":null,"tryCatchPattern":"try { action.execute(); } catch (NoSuchNamespaceException e) { spark.sql(\"CREATE NAMESPACE IF NOT EXISTS \" + destIdent.namespace()); action.execute(); }","preventionTips":["Always create the destination namespace before snapshot/migrate/addFiles","Script namespace creation into your provisioning step","Validate the destination identifier's namespace spelling against the catalog"],"tags":["spark","namespace","table-creation"],"backgroundTag":"directory-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"}