{"record":{"id":"4c28b02b0f90fe3e","repo":"apache/iceberg","slug":"namespacealreadyexistsexception-namespace-4c28b0","errorCode":null,"errorMessage":"NamespaceAlreadyExistsException: <namespace>","messagePattern":"NamespaceAlreadyExistsException: <namespace>","errorType":"exception","errorClass":"org.apache.spark.sql.catalyst.analysis.NamespaceAlreadyExistsException","httpStatus":null,"severity":"error","filePath":"spark/v4.2/spark/src/main/java/org/apache/iceberg/spark/SparkCatalog.java","lineNumber":491,"sourceCode":"\n    throw new NoSuchNamespaceException(namespace);\n  }\n\n  @Override\n  public void createNamespace(String[] namespace, Map<String, String> metadata)\n      throws NamespaceAlreadyExistsException {\n    if (asNamespaceCatalog != null) {\n      try {\n        if (asNamespaceCatalog instanceof HadoopCatalog\n            && DEFAULT_NS_KEYS.equals(metadata.keySet())) {\n          // Hadoop catalog will reject metadata properties, but Spark automatically adds \"owner\".\n          // If only the automatic properties are present, replace metadata with an empty map.\n          asNamespaceCatalog.createNamespace(Namespace.of(namespace), ImmutableMap.of());\n        } else {\n          asNamespaceCatalog.createNamespace(Namespace.of(namespace), metadata);\n        }\n      } catch (AlreadyExistsException e) {\n        throw new NamespaceAlreadyExistsException(namespace);\n      }\n    } else {\n      throw new UnsupportedOperationException(\n          \"Namespaces are not supported by catalog: \" + catalogName);\n    }\n  }\n\n  @Override\n  public void alterNamespace(String[] namespace, NamespaceChange... changes)\n      throws NoSuchNamespaceException {\n    if (asNamespaceCatalog != null) {\n      Map<String, String> updates = Maps.newHashMap();\n      Set<String> removals = Sets.newHashSet();\n      for (NamespaceChange change : changes) {\n        if (change instanceof NamespaceChange.SetProperty) {\n          NamespaceChange.SetProperty set = (NamespaceChange.SetProperty) change;\n          updates.put(set.property(), set.value());\n        } else if (change instanceof NamespaceChange.RemoveProperty) {","sourceCodeStart":473,"sourceCodeEnd":509,"githubUrl":"https://github.com/apache/iceberg/blob/86d9c8fc543e7c56c9f624eb725f76c9baff9570/spark/v4.2/spark/src/main/java/org/apache/iceberg/spark/SparkCatalog.java#L473-L509","documentation":"createNamespace delegates to the SupportsNamespaces catalog; if the underlying catalog reports AlreadyExistsException, SparkCatalog translates it to Spark's NamespaceAlreadyExistsException. Namespace creation is not idempotent unless the catalog implements createOrExists semantics.","triggerScenarios":"CREATE NAMESPACE without IF NOT EXISTS when the namespace already exists; concurrent creation from two jobs; retrying a failed job that already created the namespace.","commonSituations":"Multi-tenant setups where another team created the namespace; pipeline retries; HadoopCatalog backed by an existing directory with metadata files.","solutions":["Use CREATE NAMESPACE IF NOT EXISTS","Check namespaceExists(namespace) before creating","Catch NamespaceAlreadyExistsException and treat it as success in idempotent pipelines","Remove the conflicting namespace only if it is safe"],"exampleFix":"// before\nspark.sql(\"CREATE NAMESPACE prod.db\");\n// after\nspark.sql(\"CREATE NAMESPACE IF NOT EXISTS prod.db\");","handlingStrategy":"try-catch","validationCode":"if (!spark.catalog().namespaceExists(\"prod.db\")) { spark.sql(\"CREATE NAMESPACE prod.db\"); }","typeGuard":null,"tryCatchPattern":"try { spark.sql(\"CREATE NAMESPACE prod.db\"); } catch (NamespaceAlreadyExistsException e) { /* treat as success */ }","preventionTips":["Prefer CREATE NAMESPACE IF NOT EXISTS in scripts","Make pipeline creation steps idempotent","Coordinate namespace creation across teams/jobs"],"tags":["spark","namespace","already-exists"],"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-23T08:17:48.524Z"}