bowenli86 commented on a change in pull request #8404: [FLINK-11476][table] 
Create CatalogManager to manage multiple catalogs
URL: https://github.com/apache/flink/pull/8404#discussion_r284377603
 
 

 ##########
 File path: 
flink-table/flink-table-planner/src/main/java/org/apache/flink/table/catalog/CatalogManager.java
 ##########
 @@ -0,0 +1,316 @@
+/*
+ * 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.flink.table.catalog;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.table.api.CatalogNotExistException;
+import org.apache.flink.table.api.ExternalCatalogAlreadyExistException;
+import org.apache.flink.table.api.TableSchema;
+import org.apache.flink.table.catalog.exceptions.CatalogException;
+import org.apache.flink.table.catalog.exceptions.TableNotExistException;
+import org.apache.flink.table.operations.CatalogTableOperation;
+import org.apache.flink.util.StringUtils;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Set;
+
+import static java.lang.String.format;
+import static org.apache.flink.util.Preconditions.checkArgument;
+import static org.apache.flink.util.Preconditions.checkNotNull;
+
+/**
+ * A CatalogManager that encapsulates all available catalogs. It also 
implements the logic of
+ * table path resolution. Supports both new API ({@link Catalog} as well as 
{@link ExternalCatalog}).
+ */
+@Internal
+public class CatalogManager {
+       private static final Logger LOG = 
LoggerFactory.getLogger(CatalogManager.class);
+
+       // A map between names and catalogs.
+       private Map<String, Catalog> catalogs;
+
+       // TO BE REMOVED along with ExternalCatalog API
+       private Map<String, ExternalCatalog>  externalCatalogs;
+
+       // The name of the default catalog and schema
+       private String currentCatalogName;
+
+       private String currentDatabaseName;
+
+       public CatalogManager(String defaultCatalogName, Catalog 
defaultCatalog) {
+               checkArgument(
+                       !StringUtils.isNullOrWhitespaceOnly(defaultCatalogName),
+                       "Default catalog name cannot be null or empty");
+               checkNotNull(defaultCatalog, "Default catalog cannot be null");
+               catalogs = new LinkedHashMap<>();
+               externalCatalogs = new LinkedHashMap<>();
+               catalogs.put(defaultCatalogName, defaultCatalog);
+               this.currentCatalogName = defaultCatalogName;
+               this.currentDatabaseName = defaultCatalog.getDefaultDatabase();
+       }
+
+       /**
+        * Registers a catalog under the given name. The catalog name must be 
unique across both
+        * {@link Catalog}s and {@link ExternalCatalog}s.
+        *
+        * @param catalogName name under which to register the given catalog
+        * @param catalog catalog to register
+        * @throws CatalogException if the registration of the catalog under 
the given name failed
+        */
+       public void registerCatalog(String catalogName, Catalog catalog) {
+               checkArgument(!StringUtils.isNullOrWhitespaceOnly(catalogName), 
"Catalog name cannot be null or empty.");
+               checkNotNull(catalog, "Catalog cannot be null");
+
+               if (catalogs.containsKey(catalogName) || 
externalCatalogs.containsKey(catalogName)) {
+                       throw new CatalogException(format("Catalog %s already 
exists.", catalogName));
+               }
+
+               catalogs.put(catalogName, catalog);
+               catalog.open();
+       }
+
+       /**
+        * Gets a catalog by name.
+        *
+        * @param catalogName name of the catalog to retrieve
+        * @return the requested catalog or empty if it does not exist
+        * @see CatalogManager#getExternalCatalog(String)
+        */
+       public Optional<Catalog> getCatalog(String catalogName) {
+               return Optional.ofNullable(catalogs.get(catalogName));
+       }
+
+       /**
+        * Registers an external catalog under the given name. The catalog name 
must be unique across both
+        * {@link Catalog}s and {@link ExternalCatalog}s.
+        *
+        * @param catalogName name under which to register the given catalog
+        * @param catalog catalog to register
+        * @throws ExternalCatalogAlreadyExistException thrown if the name is 
already taken
+        * @deprecated {@link ExternalCatalog} APIs will be dropped
+        */
+       @Deprecated
+       public void registerExternalCatalog(String catalogName, ExternalCatalog 
catalog) {
+               checkArgument(!StringUtils.isNullOrWhitespaceOnly(catalogName), 
"Catalog name cannot be null or empty");
+               checkNotNull(catalog, "Catalog cannot be null");
+
+               if (externalCatalogs.containsKey(catalogName) || 
catalogs.containsKey(catalogName)) {
+                       throw new 
ExternalCatalogAlreadyExistException(catalogName);
+               }
+
+               externalCatalogs.put(catalogName, catalog);
+       }
+
+       /**
+        * Gets an external catalog by name.
+        *
+        * @param externalCatalogName name of the catalog to retrieve
+        * @return the requested external catalog or empty if it does not exist
+        * @see CatalogManager#getCatalog(String)
+        * @deprecated {@link ExternalCatalog} APIs will be dropped
+        */
+       @Deprecated
+       public Optional<ExternalCatalog> getExternalCatalog(String 
externalCatalogName) {
+               return 
Optional.ofNullable(externalCatalogs.get(externalCatalogName));
+       }
+
+       /**
+        * Retrieves names of all registered catalogs. It does not include 
{@link ExternalCatalog}s.
+        *
+        * @return a set of names of registered catalogs
+        * @see CatalogManager#getExternalCatalogs()
+        */
+       public Set<String> getCatalogs() {
+               return catalogs.keySet();
+       }
+
+       /**
+        * Retrieves names of all registered external catalogs. It does not 
include {@link Catalog}s.
+        *
+        * @return a set of names of registered catalogs
+        * @see CatalogManager#getCatalogs()
+        * @deprecated {@link ExternalCatalog} APIs will be dropped
+        */
+       @Deprecated
+       public Set<String> getExternalCatalogs() {
+               return externalCatalogs.keySet();
+       }
+
+       /**
+        * Gets the current default catalog that will be used when resolving 
table path.
+        *
+        * @return the current default catalog
+        * @see CatalogManager#resolveTable(String...)
+        */
+       public String getCurrentCatalog() {
+               return currentCatalogName;
+       }
+
+       /**
+        * Sets the current default catalog name that will be used when 
resolving table path.
+        *
+        * @param catalogName catalog name to set as current default catalog
+        * @throws CatalogNotExistException thrown if the catalog doesn't exist
+        * @see CatalogManager#resolveTable(String...)
+        */
+       public void setCurrentCatalog(String catalogName) throws 
CatalogNotExistException {
+               checkArgument(!StringUtils.isNullOrWhitespaceOnly(catalogName), 
"Catalog name cannot be null or empty.");
+
+               if (externalCatalogs.containsKey(catalogName)) {
+                       throw new CatalogException("An external catalog cannot 
be set as the default one.");
+               }
+
+               Catalog potentialCurrentCatalog = catalogs.get(catalogName);
+               if (potentialCurrentCatalog == null) {
+                       throw new CatalogException(format("Catalog with name %s 
does not exist", catalogName));
+               }
+
+               if (!currentCatalogName.equals(catalogName)) {
+                       currentCatalogName = catalogName;
+                       currentDatabaseName = 
potentialCurrentCatalog.getDefaultDatabase();
+
+                       LOG.info(
+                               "Sets the current default catalog as '{}' and 
the current default database as '{}'",
+                               currentCatalogName,
+                               currentDatabaseName);
+               }
+       }
+
+       /**
+        * Gets the current default database name that will be used when 
resolving table path.
+        *
+        * @return the current default database
+        * @see CatalogManager#resolveTable(String...)
+        */
+       public String getCurrentDatabase() {
+               return currentDatabaseName;
+       }
+
+       /**
+        * Sets the current default database name that will be used when 
resolving a table path.
+        * The database has to exist in the current catalog.
+        *
+        * @param databaseName database name to set as current default database 
name
+        * @throws CatalogException thrown if the database doesn't exist in the 
current catalog
+        * @see CatalogManager#resolveTable(String...)
+        * @see CatalogManager#setCurrentCatalog(String)
+        */
+       public void setCurrentDatabase(String databaseName) {
+               
checkArgument(!StringUtils.isNullOrWhitespaceOnly(databaseName), "The database 
name cannot be null or empty");
+
+               if 
(!catalogs.get(currentCatalogName).databaseExists(databaseName)) {
+                       throw new CatalogException(format(
+                               "A database with with name %s does not exist in 
the catalog: %s",
+                               databaseName,
+                               currentCatalogName));
+               }
+
+               if (!currentDatabaseName.equals(databaseName)) {
+                       currentDatabaseName = databaseName;
+
+                       LOG.info(
+                               "Sets the current default database as '{}' in 
the current default catalog '{}'",
+                               currentCatalogName,
+                               currentDatabaseName);
+               }
+       }
+
+       /**
+        * Tries to resolve a table path to a {@link CatalogTableOperation}. 
First it tries to look for
+        * {@code [default-path].[table-path]} if no table is found assumes the 
table path is a fully qualified one
+        * and looks for {@code [table-path]}.
+        *
+        * @param tablePath table path to look for
+        * @return {@link CatalogTableOperation} containing both fully 
qualified table identifier and its
+        * {@link TableSchema}.
+        */
+       public Optional<CatalogTableOperation> resolveTable(String... 
tablePath) {
+               checkArgument(tablePath != null && tablePath.length != 0, 
"Table path must not be null or empty.");
+
+               List<String> defaultPath = new ArrayList<>();
+               defaultPath.add(currentCatalogName);
+               defaultPath.add(currentDatabaseName);
+
+               List<String> userPath = Arrays.asList(tablePath);
+               defaultPath.addAll(userPath);
+
+               Optional<CatalogTableOperation> inDefaultPath = 
lookupPath(defaultPath);
+
+               if (inDefaultPath.isPresent()) {
+                       return inDefaultPath;
+               } else {
+                       return lookupPath(userPath);
+               }
+       }
+
+       private Optional<CatalogTableOperation> lookupPath(List<String> path) {
+               try {
+                       Optional<TableSchema> potentialTable = 
lookupCatalogTable(path);
+
+                       if (!potentialTable.isPresent()) {
+                               potentialTable = lookupExternalTable(path);
+                       }
+                       return potentialTable.map(schema -> new 
CatalogTableOperation(path, schema));
+               } catch (TableNotExistException e) {
+                       return Optional.empty();
+               }
+       }
+
+       private Optional<TableSchema> lookupCatalogTable(List<String> path) 
throws TableNotExistException {
+               if (path.size() >= 3) {
 
 Review comment:
   I got a bit confused here: why can size of a valid path be larger than 3 for 
catalog table name resolution?
   
   And shall we validate the size of input with checkArgument rather than 
swallowing all kinds of path?

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to