ajantha-bhat commented on code in PR #8857: URL: https://github.com/apache/iceberg/pull/8857#discussion_r1364768700
########## nessie/src/main/java/org/apache/iceberg/nessie/NessieIcebergClient.java: ########## @@ -181,21 +182,76 @@ public IcebergTable table(TableIdentifier tableIdentifier) { } public void createNamespace(Namespace namespace, Map<String, String> metadata) { + getRef().checkMutable(); + + if (namespace.isEmpty()) { + throw new IllegalArgumentException("Creating empty namespaces is not supported"); + } + + ContentKey key = ContentKey.of(namespace.levels()); + org.projectnessie.model.Namespace content = + org.projectnessie.model.Namespace.of(key.getElements(), metadata); + try { - getRef().checkMutable(); - withReference( - getApi() - .createNamespace() - .namespace(org.projectnessie.model.Namespace.of(namespace.levels())) - .properties(metadata)) - .create(); - refresh(); - } catch (NessieNamespaceAlreadyExistsException e) { - throw new AlreadyExistsException(e, "Namespace already exists: %s", namespace); + + // First, check that the namespace doesn't exist at the current hash; + // this will avoid a NessieBadRequestException when committing the namespace creation + Reference ref = getReference(); + Map<ContentKey, Content> contentMap = api.getContent().reference(ref).key(key).get(); + Content existing = contentMap.get(key); + if (existing != null) { + if (existing instanceof org.projectnessie.model.Namespace) { + throw new AlreadyExistsException("Namespace already exists: '%s'", namespace); + } else { + throw new AlreadyExistsException( Review Comment: nit: we can check the instance of view or table and make the error message to be clear as Table or view already exist. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org For additional commands, e-mail: issues-h...@iceberg.apache.org