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

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


The following commit(s) were added to refs/heads/main by this push:
     new 426818bfe7 Core: Add property to disable table initialization for 
JdbcCatalog (#10124)
426818bfe7 is described below

commit 426818bfe7fa93e8c677ebf886638d5c50db597b
Author: Marc Cenac <[email protected]>
AuthorDate: Mon Apr 29 05:20:15 2024 -0500

    Core: Add property to disable table initialization for JdbcCatalog (#10124)
---
 .../java/org/apache/iceberg/jdbc/JdbcCatalog.java  |  5 +-
 .../java/org/apache/iceberg/jdbc/JdbcUtil.java     |  3 +
 .../org/apache/iceberg/jdbc/TestJdbcCatalog.java   | 73 ++++++++++++++++++++++
 3 files changed, 80 insertions(+), 1 deletion(-)

diff --git a/core/src/main/java/org/apache/iceberg/jdbc/JdbcCatalog.java 
b/core/src/main/java/org/apache/iceberg/jdbc/JdbcCatalog.java
index dc7352743e..71590e7618 100644
--- a/core/src/main/java/org/apache/iceberg/jdbc/JdbcCatalog.java
+++ b/core/src/main/java/org/apache/iceberg/jdbc/JdbcCatalog.java
@@ -86,7 +86,7 @@ public class JdbcCatalog extends BaseMetastoreViewCatalog
   private Map<String, String> catalogProperties;
   private final Function<Map<String, String>, FileIO> ioBuilder;
   private final Function<Map<String, String>, JdbcClientPool> 
clientPoolBuilder;
-  private final boolean initializeCatalogTables;
+  private boolean initializeCatalogTables;
   private CloseableGroup closeableGroup;
   private JdbcUtil.SchemaVersion schemaVersion = JdbcUtil.SchemaVersion.V0;
 
@@ -137,6 +137,9 @@ public class JdbcCatalog extends BaseMetastoreViewCatalog
       this.connections = new JdbcClientPool(uri, properties);
     }
 
+    this.initializeCatalogTables =
+        PropertyUtil.propertyAsBoolean(
+            properties, JdbcUtil.INIT_CATALOG_TABLES_PROPERTY, 
initializeCatalogTables);
     if (initializeCatalogTables) {
       initializeCatalogTables();
     }
diff --git a/core/src/main/java/org/apache/iceberg/jdbc/JdbcUtil.java 
b/core/src/main/java/org/apache/iceberg/jdbc/JdbcUtil.java
index ffa606a94f..749c2d485f 100644
--- a/core/src/main/java/org/apache/iceberg/jdbc/JdbcUtil.java
+++ b/core/src/main/java/org/apache/iceberg/jdbc/JdbcUtil.java
@@ -39,6 +39,9 @@ final class JdbcUtil {
   static final String STRICT_MODE_PROPERTY = JdbcCatalog.PROPERTY_PREFIX + 
"strict-mode";
   // property to control if view support is added to the existing database
   static final String SCHEMA_VERSION_PROPERTY = JdbcCatalog.PROPERTY_PREFIX + 
"schema-version";
+  // property to control if catalog tables are created during initialization
+  static final String INIT_CATALOG_TABLES_PROPERTY =
+      JdbcCatalog.PROPERTY_PREFIX + "init-catalog-tables";
 
   enum SchemaVersion {
     V0,
diff --git a/core/src/test/java/org/apache/iceberg/jdbc/TestJdbcCatalog.java 
b/core/src/test/java/org/apache/iceberg/jdbc/TestJdbcCatalog.java
index c2f0869d00..985c84f0dc 100644
--- a/core/src/test/java/org/apache/iceberg/jdbc/TestJdbcCatalog.java
+++ b/core/src/test/java/org/apache/iceberg/jdbc/TestJdbcCatalog.java
@@ -31,6 +31,7 @@ import java.nio.charset.StandardCharsets;
 import java.nio.file.Files;
 import java.nio.file.Paths;
 import java.sql.Connection;
+import java.sql.DatabaseMetaData;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.util.List;
@@ -167,6 +168,46 @@ public class TestJdbcCatalog extends 
CatalogTests<JdbcCatalog> {
     jdbcCatalog.initialize("test_jdbc_catalog", properties);
   }
 
+  @Test
+  public void testDisableInitCatalogTablesOverridesDefault() throws Exception {
+    // as this test uses different connections, we can't use memory database 
(as it's per
+    // connection), but a file database instead
+    java.nio.file.Path dbFile = 
Files.createTempFile("icebergInitCatalogTables", "db");
+    String jdbcUrl = "jdbc:sqlite:" + dbFile.toAbsolutePath();
+
+    Map<String, String> properties = Maps.newHashMap();
+    properties.put(CatalogProperties.WAREHOUSE_LOCATION, 
this.tableDir.toAbsolutePath().toString());
+    properties.put(CatalogProperties.URI, jdbcUrl);
+    properties.put(JdbcUtil.INIT_CATALOG_TABLES_PROPERTY, "false");
+
+    JdbcCatalog jdbcCatalog = new JdbcCatalog();
+    jdbcCatalog.initialize("test_jdbc_catalog", properties);
+
+    assertThat(catalogTablesExist(jdbcUrl)).isFalse();
+
+    assertThatThrownBy(() -> jdbcCatalog.listNamespaces())
+        .isInstanceOf(UncheckedSQLException.class)
+        .hasMessage(String.format("Failed to execute query: %s", 
JdbcUtil.LIST_ALL_NAMESPACES_SQL));
+  }
+
+  @Test
+  public void testEnableInitCatalogTablesOverridesDefault() throws Exception {
+    // as this test uses different connections, we can't use memory database 
(as it's per
+    // connection), but a file database instead
+    java.nio.file.Path dbFile = 
Files.createTempFile("icebergInitCatalogTables", "db");
+    String jdbcUrl = "jdbc:sqlite:" + dbFile.toAbsolutePath();
+
+    Map<String, String> properties = Maps.newHashMap();
+    properties.put(CatalogProperties.WAREHOUSE_LOCATION, 
this.tableDir.toAbsolutePath().toString());
+    properties.put(CatalogProperties.URI, jdbcUrl);
+    properties.put(JdbcUtil.INIT_CATALOG_TABLES_PROPERTY, "true");
+
+    JdbcCatalog jdbcCatalog = new JdbcCatalog(null, null, false);
+    jdbcCatalog.initialize("test_jdbc_catalog", properties);
+
+    assertThat(catalogTablesExist(jdbcUrl)).isTrue();
+  }
+
   @Test
   public void testInitSchemaV0() {
     Map<String, String> properties = Maps.newHashMap();
@@ -1123,4 +1164,36 @@ public class TestJdbcCatalog extends 
CatalogTests<JdbcCatalog> {
           .execute();
     }
   }
+
+  private boolean catalogTablesExist(String jdbcUrl) throws SQLException {
+    SQLiteDataSource dataSource = new SQLiteDataSource();
+    dataSource.setUrl(jdbcUrl);
+
+    boolean catalogTableExists = false;
+    boolean namespacePropertiesTableExists = false;
+
+    try (Connection connection = dataSource.getConnection()) {
+      DatabaseMetaData metadata = connection.getMetaData();
+      if (tableExists(metadata, JdbcUtil.CATALOG_TABLE_VIEW_NAME)) {
+        catalogTableExists = true;
+      }
+      if (tableExists(metadata, JdbcUtil.NAMESPACE_PROPERTIES_TABLE_NAME)) {
+        namespacePropertiesTableExists = true;
+      }
+    }
+
+    return catalogTableExists && namespacePropertiesTableExists;
+  }
+
+  private boolean tableExists(DatabaseMetaData metadata, String tableName) 
throws SQLException {
+    ResultSet resultSet = metadata.getTables(null, null, tableName, new 
String[] {"TABLE"});
+
+    while (resultSet.next()) {
+      if (tableName.equals(resultSet.getString("TABLE_NAME"))) {
+        return true;
+      }
+    }
+
+    return false;
+  }
 }

Reply via email to