You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@gravitino.apache.org by ro...@apache.org on 2026/06/01 08:31:27 UTC

(gravitino) branch main updated: [#11285] fix(iceberg-rest): default JDBC catalog to strict mode (#11318)

This is an automated email from the ASF dual-hosted git repository.

roryqi pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/gravitino.git


The following commit(s) were added to refs/heads/main by this push:
     new 012f72d413 [#11285] fix(iceberg-rest): default JDBC catalog to strict mode (#11318)
012f72d413 is described below

commit 012f72d413bd63c0c94d4a864f81174c25094356
Author: roryqi <ro...@datastrato.com>
AuthorDate: Mon Jun 1 16:31:22 2026 +0800

    [#11285] fix(iceberg-rest): default JDBC catalog to strict mode (#11318)
    
    ### What changes were proposed in this pull request?
    
    Default the Iceberg `jdbc.strict-mode` property to `true` for the JDBC
    catalog backend, in `IcebergCatalogUtil#loadJdbcCatalog` (via
    `putIfAbsent`, mirroring the existing `jdbc.schema-version` default).
    The value can still be overridden by an explicit config.
    
    ### Why are the changes needed?
    
    The JDBC backend implicitly created the namespace when a table (or view)
    was created under a non-existent namespace, returning `200 OK` instead
    of the HTTP `404 NoSuchNamespaceError` required by the Iceberg REST
    catalog spec. The memory backend already returns 404. This caused
    namespace proliferation, hid namespace-name typos, and was
    spec-non-compliant. With strict mode,
    `JdbcTableOperations`/`JdbcViewOperations` validate namespace existence
    before commit and throw `NoSuchNamespaceException`.
    
    Fix: #11285
    
    ### Does this PR introduce _any_ user-facing change?
    
    Yes. For the Iceberg REST service backed by the JDBC catalog, creating a
    table or view in a namespace that does not exist now fails with HTTP 404
    instead of implicitly creating the namespace. Operators relying on the
    old implicit behavior can restore it by explicitly setting
    `gravitino.iceberg-rest.jdbc.strict-mode = false`. No property keys were
    added or removed (the property is provided by Iceberg).
    
    ### How was this patch tested?
    
    - Unit tests in `TestIcebergCatalogUtil`: default strict mode rejects
    creating a table in a missing namespace with `NoSuchNamespaceException`;
    an explicit `jdbc.strict-mode=false` is not overridden and allows
    implicit creation.
    - Integration test
    `IcebergRESTServiceIT#testCreateTableInNonExistentNamespace` exercises
    the end-to-end REST path and runs across the memory, hive, and JDBC
    backends.
---
 .../lakehouse/iceberg/IcebergConstants.java        |  2 +
 docs/iceberg-rest-service.md                       |  1 +
 .../iceberg/common/utils/IcebergCatalogUtil.java   |  6 +++
 .../common/utils/TestIcebergCatalogUtil.java       | 51 +++++++++++++++++++++
 .../integration/test/IcebergRESTServiceIT.java     | 52 ++++++++++++++++++++++
 5 files changed, 112 insertions(+)

diff --git a/catalogs/catalog-common/src/main/java/org/apache/gravitino/catalog/lakehouse/iceberg/IcebergConstants.java b/catalogs/catalog-common/src/main/java/org/apache/gravitino/catalog/lakehouse/iceberg/IcebergConstants.java
index cbfe027188..58606f43f3 100644
--- a/catalogs/catalog-common/src/main/java/org/apache/gravitino/catalog/lakehouse/iceberg/IcebergConstants.java
+++ b/catalogs/catalog-common/src/main/java/org/apache/gravitino/catalog/lakehouse/iceberg/IcebergConstants.java
@@ -38,6 +38,8 @@ public class IcebergConstants {
   public static final String GRAVITINO_JDBC_SCHEMA_VERSION = "jdbc-schema-version";
   public static final String ICEBERG_JDBC_SCHEMA_VERSION = "jdbc.schema-version";
 
+  public static final String ICEBERG_JDBC_STRICT_MODE = "jdbc.strict-mode";
+
   public static final String GRAVITINO_JDBC_DRIVER = "jdbc-driver";
   public static final String WAREHOUSE = "warehouse";
   public static final String URI = "uri";
diff --git a/docs/iceberg-rest-service.md b/docs/iceberg-rest-service.md
index d050e6d21e..d25832d6cb 100644
--- a/docs/iceberg-rest-service.md
+++ b/docs/iceberg-rest-service.md
@@ -119,6 +119,7 @@ The Gravitino Iceberg REST catalog service uses the memory catalog backend by de
 | `gravitino.iceberg-rest.jdbc-initialize`      | Whether to initialize the meta tables when creating the JDBC catalog.                                                                                                                                                                                  | `true`                  | No       | 0.2.0         |
 | `gravitino.iceberg-rest.jdbc-driver`          | `com.mysql.jdbc.Driver` or `com.mysql.cj.jdbc.Driver` for MySQL, `org.postgresql.Driver` for PostgreSQL.                                                                                                                                               | (none)                  | Yes      | 0.3.0         |
 | `gravitino.iceberg-rest.jdbc-schema-version`  | The schema version of the JDBC catalog. Defaults to `V1` to enable view support. Set to `V0` only if you need to opt out of view support. Once the underlying database is migrated to V1, this property is no longer required on subsequent restarts.  | `V1`                    | No       | 1.2.0         |
+| `gravitino.iceberg-rest.jdbc.strict-mode`     | Whether the JDBC catalog runs in strict mode. Defaults to `true` so that creating a table or view in a namespace that does not exist fails with `NoSuchNamespace` (HTTP 404), matching the Iceberg REST specification. Set to `false` to restore the legacy behavior of implicitly creating the namespace.  | `true`                  | No       | 1.3.0         |
 
 If you have a JDBC Iceberg catalog prior, you must set `catalog-backend-name` to keep consistent with your Jdbc Iceberg catalog name to operate the prior namespace and tables.
 
diff --git a/iceberg/iceberg-common/src/main/java/org/apache/gravitino/iceberg/common/utils/IcebergCatalogUtil.java b/iceberg/iceberg-common/src/main/java/org/apache/gravitino/iceberg/common/utils/IcebergCatalogUtil.java
index 8935a84d9b..8af0c6325c 100644
--- a/iceberg/iceberg-common/src/main/java/org/apache/gravitino/iceberg/common/utils/IcebergCatalogUtil.java
+++ b/iceberg/iceberg-common/src/main/java/org/apache/gravitino/iceberg/common/utils/IcebergCatalogUtil.java
@@ -115,6 +115,12 @@ public class IcebergCatalogUtil {
     // Default to V1 schema to support view operations; can be overridden by explicit config.
     properties.putIfAbsent(IcebergConstants.ICEBERG_JDBC_SCHEMA_VERSION, "V1");
 
+    // Default to strict mode so that creating a table or view in a non-existent namespace fails
+    // with NoSuchNamespaceException (HTTP 404) instead of implicitly creating the namespace,
+    // matching the Iceberg REST spec and the memory backend behavior. Can be overridden by
+    // explicit config.
+    properties.putIfAbsent(IcebergConstants.ICEBERG_JDBC_STRICT_MODE, "true");
+
     HdfsConfiguration hdfsConfiguration = new HdfsConfiguration();
     properties.forEach(hdfsConfiguration::set);
     jdbcCatalog.setConf(hdfsConfiguration);
diff --git a/iceberg/iceberg-common/src/test/java/org/apache/gravitino/iceberg/common/utils/TestIcebergCatalogUtil.java b/iceberg/iceberg-common/src/test/java/org/apache/gravitino/iceberg/common/utils/TestIcebergCatalogUtil.java
index 6f9cf71194..bf0fbd8e2c 100644
--- a/iceberg/iceberg-common/src/test/java/org/apache/gravitino/iceberg/common/utils/TestIcebergCatalogUtil.java
+++ b/iceberg/iceberg-common/src/test/java/org/apache/gravitino/iceberg/common/utils/TestIcebergCatalogUtil.java
@@ -19,22 +19,31 @@
 
 package org.apache.gravitino.iceberg.common.utils;
 
+import java.nio.file.Path;
 import java.util.HashMap;
 import java.util.Map;
 import org.apache.gravitino.catalog.lakehouse.iceberg.IcebergCatalogBackend;
 import org.apache.gravitino.catalog.lakehouse.iceberg.IcebergConstants;
 import org.apache.gravitino.iceberg.common.IcebergConfig;
 import org.apache.iceberg.CatalogProperties;
+import org.apache.iceberg.Schema;
 import org.apache.iceberg.catalog.Catalog;
+import org.apache.iceberg.catalog.Namespace;
+import org.apache.iceberg.catalog.TableIdentifier;
+import org.apache.iceberg.exceptions.NoSuchNamespaceException;
 import org.apache.iceberg.hive.HiveCatalog;
 import org.apache.iceberg.inmemory.InMemoryCatalog;
 import org.apache.iceberg.jdbc.JdbcCatalog;
 import org.apache.iceberg.jdbc.JdbcCatalogWithMetadataLocationSupport;
+import org.apache.iceberg.types.Types;
 import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
 
 public class TestIcebergCatalogUtil {
 
+  @TempDir private Path warehouse;
+
   @Test
   void testLoadCatalog() {
     Catalog catalog;
@@ -117,6 +126,48 @@ public class TestIcebergCatalogUtil {
         "Explicitly configured V0 schema should not be overridden to V1");
   }
 
+  @Test
+  void testJdbcCatalogDefaultStrictModeRejectsImplicitNamespace() {
+    Map<String, String> properties = new HashMap<>();
+    properties.put(
+        CatalogProperties.URI, "jdbc:sqlite:file:strict_default?mode=memory&cache=shared");
+    properties.put(CatalogProperties.WAREHOUSE_LOCATION, warehouse.toString());
+    properties.put(IcebergConstants.GRAVITINO_JDBC_DRIVER, "org.sqlite.JDBC");
+    properties.put(IcebergConstants.ICEBERG_JDBC_USER, "test");
+    properties.put(IcebergConstants.ICEBERG_JDBC_PASSWORD, "test");
+    // jdbc.strict-mode is intentionally not set; default should be true
+    Catalog catalog =
+        IcebergCatalogUtil.loadCatalogBackend(
+            IcebergCatalogBackend.JDBC, new IcebergConfig(properties));
+
+    Schema schema = new Schema(Types.NestedField.required(1, "id", Types.IntegerType.get()));
+    TableIdentifier identifier = TableIdentifier.of(Namespace.of("absent_ns"), "t");
+    // Strict mode must reject creating a table in a namespace that does not exist.
+    Assertions.assertThrows(
+        NoSuchNamespaceException.class, () -> catalog.createTable(identifier, schema));
+  }
+
+  @Test
+  void testJdbcCatalogExplicitStrictModeDisabledAllowsImplicitNamespace() {
+    Map<String, String> properties = new HashMap<>();
+    properties.put(
+        CatalogProperties.URI, "jdbc:sqlite:file:strict_disabled?mode=memory&cache=shared");
+    properties.put(CatalogProperties.WAREHOUSE_LOCATION, warehouse.toString());
+    properties.put(IcebergConstants.GRAVITINO_JDBC_DRIVER, "org.sqlite.JDBC");
+    properties.put(IcebergConstants.ICEBERG_JDBC_USER, "test");
+    properties.put(IcebergConstants.ICEBERG_JDBC_PASSWORD, "test");
+    // Explicitly disable strict mode; loadJdbcCatalog must not override it back to true.
+    properties.put(IcebergConstants.ICEBERG_JDBC_STRICT_MODE, "false");
+    Catalog catalog =
+        IcebergCatalogUtil.loadCatalogBackend(
+            IcebergCatalogBackend.JDBC, new IcebergConfig(properties));
+
+    Schema schema = new Schema(Types.NestedField.required(1, "id", Types.IntegerType.get()));
+    TableIdentifier identifier = TableIdentifier.of(Namespace.of("implicit_ns"), "t");
+    // With strict mode disabled the namespace is created implicitly, so this must succeed.
+    Assertions.assertNotNull(catalog.createTable(identifier, schema));
+  }
+
   @Test
   void testValidLoadCustomCatalog() {
     Catalog catalog;
diff --git a/iceberg/iceberg-rest-server/src/test/java/org/apache/gravitino/iceberg/integration/test/IcebergRESTServiceIT.java b/iceberg/iceberg-rest-server/src/test/java/org/apache/gravitino/iceberg/integration/test/IcebergRESTServiceIT.java
index 51aca2dc02..9109f9734a 100644
--- a/iceberg/iceberg-rest-server/src/test/java/org/apache/gravitino/iceberg/integration/test/IcebergRESTServiceIT.java
+++ b/iceberg/iceberg-rest-server/src/test/java/org/apache/gravitino/iceberg/integration/test/IcebergRESTServiceIT.java
@@ -305,6 +305,58 @@ public abstract class IcebergRESTServiceIT extends IcebergRESTServiceBaseIT {
         () -> sql(String.format("CREATE TABLE %s.create_foo1", getTestNamespace())));
   }
 
+  @Test
+  void testCreateTableInNonExistentNamespace() {
+    // The child namespace is intentionally never created.
+    String namespaceName = getTestNamespace("absent_create_ns");
+    Throwable thrown =
+        Assertions.assertThrows(
+            Throwable.class,
+            () ->
+                sql(
+                    String.format(
+                        "CREATE TABLE %s.create_in_absent(id bigint) using iceberg",
+                        namespaceName)));
+    // Creating a table in a namespace that does not exist must surface a NoSuchNamespace error
+    // (HTTP 404) instead of implicitly creating the namespace. This holds for the memory and hive
+    // backends, and for the JDBC backend now that strict mode is enabled by default.
+    Assertions.assertTrue(
+        isNoSuchNamespace(thrown), () -> "Expected a NoSuchNamespace error but got: " + thrown);
+  }
+
+  @Test
+  @EnabledIf("isSupportsViewCatalog")
+  void testCreateViewInNonExistentNamespace() {
+    String sourceTable = getTestNamespace() + ".view_source_for_absent_ns";
+    sql(String.format("CREATE TABLE %s (id bigint) using iceberg", sourceTable));
+    // The child namespace is intentionally never created.
+    String namespaceName = getTestNamespace("absent_view_ns");
+    Throwable thrown =
+        Assertions.assertThrows(
+            Throwable.class,
+            () ->
+                sql(
+                    String.format(
+                        "CREATE VIEW %s.view_in_absent AS SELECT * FROM %s",
+                        namespaceName, sourceTable)));
+    // Strict mode must also reject creating a view in a namespace that does not exist, instead of
+    // implicitly creating the namespace.
+    Assertions.assertTrue(
+        isNoSuchNamespace(thrown), () -> "Expected a NoSuchNamespace error but got: " + thrown);
+  }
+
+  // Walks the cause chain because the missing namespace may surface as either Spark's or Iceberg's
+  // NoSuchNamespaceException, and Spark may wrap it.
+  private static boolean isNoSuchNamespace(Throwable thrown) {
+    for (Throwable t = thrown; t != null; t = t.getCause()) {
+      if (t instanceof NoSuchNamespaceException
+          || t instanceof org.apache.iceberg.exceptions.NoSuchNamespaceException) {
+        return true;
+      }
+    }
+    return false;
+  }
+
   @Test
   void testDropTable() {
     sql(