FranMorilloAWS commented on code in PR #191: URL: https://github.com/apache/flink-connector-aws/pull/191#discussion_r2052176250
########## flink-catalog-aws-glue/src/main/java/org/apache/flink/table/catalog/glue/operations/GlueDatabaseOperations.java: ########## @@ -0,0 +1,240 @@ +/* + * 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.glue.operations; + +import org.apache.flink.table.catalog.CatalogDatabase; +import org.apache.flink.table.catalog.CatalogDatabaseImpl; +import org.apache.flink.table.catalog.exceptions.CatalogException; +import org.apache.flink.table.catalog.exceptions.DatabaseAlreadyExistException; +import org.apache.flink.table.catalog.exceptions.DatabaseNotExistException; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import software.amazon.awssdk.services.glue.GlueClient; +import software.amazon.awssdk.services.glue.model.AlreadyExistsException; +import software.amazon.awssdk.services.glue.model.Database; +import software.amazon.awssdk.services.glue.model.DeleteDatabaseRequest; +import software.amazon.awssdk.services.glue.model.EntityNotFoundException; +import software.amazon.awssdk.services.glue.model.GetDatabaseRequest; +import software.amazon.awssdk.services.glue.model.GetDatabaseResponse; +import software.amazon.awssdk.services.glue.model.GetDatabasesRequest; +import software.amazon.awssdk.services.glue.model.GetDatabasesResponse; +import software.amazon.awssdk.services.glue.model.GlueException; +import software.amazon.awssdk.services.glue.model.InvalidInputException; +import software.amazon.awssdk.services.glue.model.OperationTimeoutException; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.regex.Pattern; +import java.util.stream.Collectors; + +/** + * Handles all database-related operations for the Glue catalog. + * Provides functionality for listing, retrieving, creating, and deleting databases in AWS Glue. + */ +public class GlueDatabaseOperations extends AbstractGlueOperations { + + /** Logger for logging database operations. */ + private static final Logger LOG = LoggerFactory.getLogger(GlueDatabaseOperations.class); + + /** + * Pattern for validating database names. + * AWS Glue lowercases all names, so we enforce lowercase to avoid identification issues. + */ + private static final Pattern VALID_NAME_PATTERN = Pattern.compile("^[a-z0-9_]+$"); + + /** + * Constructor for GlueDatabaseOperations. + * Initializes the Glue client and catalog name. + * + * @param glueClient The Glue client to interact with AWS Glue. + * @param catalogName The name of the catalog. + */ + public GlueDatabaseOperations(GlueClient glueClient, String catalogName) { + super(glueClient, catalogName); + } + + /** + * Validates that a database name contains only lowercase letters, numbers, and underscores. + * AWS Glue lowercases all identifiers, which can lead to name conflicts if uppercase is used. + * + * @param databaseName The database name to validate + * @throws CatalogException if the database name contains uppercase letters or invalid characters + */ + private void validateDatabaseName(String databaseName) { + if (databaseName == null || databaseName.isEmpty()) { + throw new CatalogException("Database name cannot be null or empty"); + } + + if (!VALID_NAME_PATTERN.matcher(databaseName).matches()) { + throw new CatalogException( + "Database name can only contain lowercase letters, numbers, and underscores. " + + "AWS Glue lowercases all identifiers, which can cause identification issues with mixed-case names."); + } + } + + /** + * Lists all the databases in the Glue catalog. + * + * @return A list of database names. + * @throws CatalogException if there is an error fetching the list of databases. + */ + public List<String> listDatabases() throws CatalogException { + try { + LOG.debug("Listing databases from Glue catalog"); + List<String> databaseNames = new ArrayList<>(); + String nextToken = null; + do { + GetDatabasesRequest.Builder requestBuilder = GetDatabasesRequest.builder(); + // Add the next token if we have one + if (nextToken != null) { + requestBuilder.nextToken(nextToken); + } + GetDatabasesResponse response = glueClient.getDatabases(requestBuilder.build()); + // Add all found databases to our list + databaseNames.addAll(response.databaseList().stream() + .map(Database::name) + .collect(Collectors.toList())); + // Update the next token + nextToken = response.nextToken(); + // Continue until no more pages (nextToken is null) + } while (nextToken != null); + LOG.debug("Found {} databases in Glue catalog", databaseNames.size()); + return databaseNames; + } catch (GlueException e) { + LOG.error("Failed to list databases in Glue", e); + throw new CatalogException("Failed to list databases: " + e.getMessage(), e); + } + } + + /** + * Retrieves the specified database from the Glue catalog. + * + * @param databaseName The name of the database to fetch. + * @return The CatalogDatabase object representing the Glue database. + * @throws DatabaseNotExistException If the database does not exist in the Glue catalog. + * @throws CatalogException If there is any error retrieving the database. + */ + public CatalogDatabase getDatabase(String databaseName) throws DatabaseNotExistException, CatalogException { + try { + GetDatabaseResponse response = glueClient.getDatabase( + GetDatabaseRequest.builder() + .name(databaseName) + .build() + ); Review Comment: I added the validateDatabaseName which is called when trying to create the database -- 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: issues-unsubscr...@flink.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org