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


##########
sdks/java/extensions/sql/src/main/java/org/apache/beam/sdk/extensions/sql/impl/CatalogManagerSchema.java:
##########
@@ -0,0 +1,284 @@
+/*
+ * 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.impl;
+
+import static org.apache.beam.sdk.util.Preconditions.checkStateNotNull;
+import static 
org.apache.beam.vendor.calcite.v1_40_0.org.apache.calcite.util.Static.RESOURCE;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+import java.util.stream.Collectors;
+import org.apache.beam.sdk.extensions.sql.impl.parser.SqlDdlNodes;
+import org.apache.beam.sdk.extensions.sql.meta.catalog.Catalog;
+import org.apache.beam.sdk.extensions.sql.meta.catalog.CatalogManager;
+import org.apache.beam.sdk.extensions.sql.meta.provider.TableProvider;
+import org.apache.beam.sdk.extensions.sql.meta.store.MetaStore;
+import 
org.apache.beam.vendor.calcite.v1_40_0.org.apache.calcite.linq4j.tree.Expression;
+import 
org.apache.beam.vendor.calcite.v1_40_0.org.apache.calcite.rel.type.RelProtoDataType;
+import 
org.apache.beam.vendor.calcite.v1_40_0.org.apache.calcite.schema.Function;
+import org.apache.beam.vendor.calcite.v1_40_0.org.apache.calcite.schema.Schema;
+import 
org.apache.beam.vendor.calcite.v1_40_0.org.apache.calcite.schema.SchemaPlus;
+import 
org.apache.beam.vendor.calcite.v1_40_0.org.apache.calcite.schema.SchemaVersion;
+import 
org.apache.beam.vendor.calcite.v1_40_0.org.apache.calcite.schema.Schemas;
+import org.apache.beam.vendor.calcite.v1_40_0.org.apache.calcite.schema.Table;
+import 
org.apache.beam.vendor.calcite.v1_40_0.org.apache.calcite.sql.SqlIdentifier;
+import org.apache.beam.vendor.calcite.v1_40_0.org.apache.calcite.sql.SqlUtil;
+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.base.Preconditions;
+import 
org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.collect.ImmutableSet;
+import org.checkerframework.checker.nullness.qual.Nullable;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * A Calcite {@link Schema} that corresponds to a {@link CatalogManager}. This 
is typically the root
+ * node of a pipeline. Child schemas are of type {@link CatalogSchema}.
+ */
+public class CatalogManagerSchema implements Schema {
+  private static final Logger LOG = 
LoggerFactory.getLogger(CatalogManagerSchema.class);
+  private final JdbcConnection connection;
+  private final CatalogManager catalogManager;
+  private final Map<String, CatalogSchema> catalogSubSchemas = new HashMap<>();
+
+  CatalogManagerSchema(JdbcConnection jdbcConnection, CatalogManager 
catalogManager) {
+    this.connection = jdbcConnection;
+    this.catalogManager = catalogManager;
+  }
+
+  @VisibleForTesting
+  public JdbcConnection connection() {
+    return connection;
+  }
+
+  public void createCatalog(
+      SqlIdentifier catalogIdentifier,
+      String type,
+      Map<String, String> properties,
+      boolean replace,
+      boolean ifNotExists) {
+    String name = SqlDdlNodes.name(catalogIdentifier);
+    if (catalogManager.getCatalog(name) != null) {
+      if (replace) {
+        LOG.info("Replacing existing catalog '{}'", name);
+        catalogManager.dropCatalog(name);
+      } else if (!ifNotExists) {
+        throw SqlUtil.newContextException(
+            catalogIdentifier.getParserPosition(),
+            RESOURCE.internal(String.format("Catalog '%s' already exists.", 
name)));
+      } else {
+        LOG.info("Catalog '{}' already exists", name);
+        return;
+      }
+    }
+
+    // create the catalog
+    catalogManager.createCatalog(name, type, properties);
+    CatalogSchema catalogSchema =
+        new CatalogSchema(connection, 
checkStateNotNull(catalogManager.getCatalog(name)));
+    catalogSubSchemas.put(name, catalogSchema);
+  }
+
+  public void useCatalog(SqlIdentifier catalogIdentifier) {
+    String name = catalogIdentifier.toString();
+    if (catalogManager.getCatalog(catalogIdentifier.toString()) == null) {
+      throw SqlUtil.newContextException(
+          catalogIdentifier.getParserPosition(),
+          RESOURCE.internal(String.format("Cannot use catalog: '%s' not 
found.", name)));
+    }
+
+    if (catalogManager.currentCatalog().name().equals(name)) {
+      LOG.info("Catalog '{}' is already in use.", name);
+      return;
+    }
+
+    catalogManager.useCatalog(name);
+    LOG.info("Switched to catalog '{}' (type: {})", name, 
catalogManager.currentCatalog().type());
+  }
+
+  public void dropCatalog(SqlIdentifier identifier, boolean ifExists) {
+    String name = SqlDdlNodes.name(identifier);
+    if (catalogManager.getCatalog(name) == null) {
+      if (!ifExists) {
+        throw SqlUtil.newContextException(
+            identifier.getParserPosition(),
+            RESOURCE.internal(String.format("Cannot drop catalog: '%s' not 
found.", name)));
+      }
+      LOG.info("Ignoring 'DROP CATALOG` call for non-existent catalog: {}", 
name);
+      return;
+    }
+
+    if (catalogManager.currentCatalog().name().equals(name)) {
+      throw SqlUtil.newContextException(
+          identifier.getParserPosition(),
+          RESOURCE.internal(
+              String.format(
+                  "Unable to drop active catalog '%s'. Please switch to 
another catalog first.",
+                  name)));
+    }
+
+    catalogManager.dropCatalog(name);
+    LOG.info("Successfully dropped catalog '{}'", name);
+    catalogSubSchemas.remove(name);
+  }
+
+  // A BeamCalciteSchema may be used to interact with multiple TableProviders.
+  // If such a TableProvider is not registered in the BeamCalciteSchema, this 
method
+  // will attempt to do so.
+  public void maybeRegisterProvider(TableName path, String type) {
+    CatalogSchema catalogSchema = getCatalogSchema(path);
+    BeamCalciteSchema beamCalciteSchema = 
catalogSchema.getDatabaseSchema(path);
+
+    if (beamCalciteSchema.getTableProvider() instanceof MetaStore) {
+      MetaStore metaStore = (MetaStore) beamCalciteSchema.getTableProvider();
+      if (metaStore.tableProviders().containsKey(type)) {
+        return;
+      }
+
+      // Start with the narrowest scope.
+      // Attempt to fetch provider from Catalog first, then CatalogManager.
+      @Nullable TableProvider provider = 
catalogSchema.getCatalog().tableProviders().get(type);
+      if (provider == null) {
+        provider = catalogManager.tableProviders().get(type);
+      }
+      // register provider
+      if (provider != null) {
+        metaStore.registerProvider(provider);
+      }
+    }
+  }
+
+  @Override
+  public @Nullable Table getTable(String table) {
+    @Nullable
+    CatalogSchema catalogSchema = 
catalogSubSchemas.get(catalogManager.currentCatalog().name());
+    return catalogSchema != null ? catalogSchema.getTable(table) : null;
+  }
+
+  @Override
+  public Set<String> getTableNames() {
+    ImmutableSet.Builder<String> names = ImmutableSet.builder();
+    // TODO: this might be a heavy operation
+    for (CatalogSchema catalogSchema : catalogSubSchemas.values()) {

Review Comment:
   Good idea, done



-- 
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