ahmedabu98 commented on code in PR #35787:
URL: https://github.com/apache/beam/pull/35787#discussion_r2377045583


##########
sdks/java/extensions/sql/src/main/java/org/apache/beam/sdk/extensions/sql/meta/provider/iceberg/IcebergMetastore.java:
##########
@@ -0,0 +1,154 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.beam.sdk.extensions.sql.meta.provider.iceberg;
+
+import static org.apache.beam.sdk.util.Preconditions.checkStateNotNull;
+import static 
org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.base.Preconditions.checkArgument;
+
+import java.util.HashMap;
+import java.util.Map;
+import org.apache.beam.sdk.extensions.sql.TableUtils;
+import org.apache.beam.sdk.extensions.sql.impl.TableName;
+import org.apache.beam.sdk.extensions.sql.meta.BeamSqlTable;
+import org.apache.beam.sdk.extensions.sql.meta.Table;
+import org.apache.beam.sdk.extensions.sql.meta.provider.TableProvider;
+import org.apache.beam.sdk.extensions.sql.meta.store.InMemoryMetaStore;
+import org.apache.beam.sdk.io.iceberg.IcebergCatalogConfig;
+import org.apache.beam.sdk.io.iceberg.IcebergCatalogConfig.IcebergTableInfo;
+import org.apache.beam.sdk.io.iceberg.TableAlreadyExistsException;
+import 
org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.annotations.VisibleForTesting;
+import 
org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.collect.ImmutableMap;
+import org.checkerframework.checker.nullness.qual.Nullable;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class IcebergMetastore extends InMemoryMetaStore {
+  private static final Logger LOG = 
LoggerFactory.getLogger(IcebergMetastore.class);
+  @VisibleForTesting final IcebergCatalogConfig catalogConfig;
+  private final Map<String, Table> cachedTables = new HashMap<>();
+  private final String database;
+
+  public IcebergMetastore(String db, IcebergCatalogConfig catalogConfig) {
+    this.database = db;
+    this.catalogConfig = catalogConfig;
+  }
+
+  @Override
+  public String getTableType() {
+    return "iceberg";
+  }
+
+  @Override
+  public void createTable(Table table) {
+    if (!table.getType().equals("iceberg")) {
+      getProvider(table.getType()).createTable(table);
+    } else {
+      String identifier = getIdentifier(table);
+      try {
+        catalogConfig.createTable(identifier, table.getSchema(), 
table.getPartitionFields());
+      } catch (TableAlreadyExistsException e) {
+        LOG.info(
+            "Iceberg table '{}' already exists at location '{}'.", 
table.getName(), identifier);
+      }
+    }
+    cachedTables.put(table.getName(), table);
+  }
+
+  @Override
+  public void dropTable(String tableName) {
+    String identifier = getIdentifier(tableName);
+    if (catalogConfig.dropTable(identifier)) {
+      LOG.info("Dropped table '{}' (path: '{}').", tableName, identifier);
+    } else {
+      LOG.info(
+          "Ignoring DROP TABLE call for '{}' (path: '{}') because it does not 
exist.",
+          tableName,
+          identifier);
+    }
+    cachedTables.remove(tableName);
+  }
+
+  @Override
+  public Map<String, Table> getTables() {
+    for (String id : catalogConfig.listTables(database)) {

Review Comment:
   But yeah it can be pretty inefficient for large databases. I was thinking of 
placing a cache to be utilized for subsequent calls, but I'm not sure if that's 
be a better user experience, as that would produce inaccurate results.
   
   I think it makes sense that if a user executes a `SHOW TABLES` on a large 
database, they should expect a long wait time



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to