FranMorilloAWS commented on code in PR #191:
URL: 
https://github.com/apache/flink-connector-aws/pull/191#discussion_r2052295570


##########
flink-catalog-aws-glue/src/main/java/org/apache/flink/table/catalog/glue/operations/GlueTableOperations.java:
##########
@@ -0,0 +1,255 @@
+/*
+ * 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.CatalogTable;
+import org.apache.flink.table.catalog.ObjectPath;
+import org.apache.flink.table.catalog.exceptions.CatalogException;
+import org.apache.flink.table.catalog.exceptions.TableNotExistException;
+
+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.Column;
+import software.amazon.awssdk.services.glue.model.CreateTableRequest;
+import software.amazon.awssdk.services.glue.model.CreateTableResponse;
+import software.amazon.awssdk.services.glue.model.DeleteTableRequest;
+import software.amazon.awssdk.services.glue.model.EntityNotFoundException;
+import software.amazon.awssdk.services.glue.model.GetTableRequest;
+import software.amazon.awssdk.services.glue.model.GetTablesRequest;
+import software.amazon.awssdk.services.glue.model.GetTablesResponse;
+import software.amazon.awssdk.services.glue.model.GlueException;
+import software.amazon.awssdk.services.glue.model.StorageDescriptor;
+import software.amazon.awssdk.services.glue.model.Table;
+import software.amazon.awssdk.services.glue.model.TableInput;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.regex.Pattern;
+import java.util.stream.Collectors;
+
+/**
+ * Handles all table-related operations for the Glue catalog.
+ * Provides functionality for checking existence, listing, creating, getting, 
and dropping tables in AWS Glue.
+ */
+public class GlueTableOperations extends AbstractGlueOperations {
+
+    /**
+     * Logger for logging table operations.
+     */
+    private static final Logger LOG = 
LoggerFactory.getLogger(GlueTableOperations.class);
+
+    /**
+     * Pattern for validating table 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 GlueTableOperations.
+     * 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 GlueTableOperations(GlueClient glueClient, String catalogName) {
+        super(glueClient, catalogName);
+    }
+
+    /**
+     * Validates that a table name contains only lowercase letters, numbers, 
and underscores.
+     * AWS Glue lowercases all identifiers, which can lead to name conflicts 
if uppercase is used.
+     *
+     * @param tableName The table name to validate
+     * @throws CatalogException if the table name contains uppercase letters 
or invalid characters
+     */
+    private void validateTableName(String tableName) {
+        if (tableName == null || tableName.isEmpty()) {
+            throw new CatalogException("Table name cannot be null or empty");
+        }
+
+        if (!VALID_NAME_PATTERN.matcher(tableName).matches()) {
+            throw new CatalogException(
+                    "Table name can only contain lowercase letters, numbers, 
and underscores. " +
+                    "AWS Glue lowercases all identifiers, which can cause 
identification issues with mixed-case names.");
+        }
+    }
+
+    /**
+     * Checks whether a table exists in the Glue catalog.
+     *
+     * @param databaseName The name of the database where the table should 
exist.
+     * @param tableName    The name of the table to check.
+     * @return true if the table exists, false otherwise.
+     */
+    public boolean glueTableExists(String databaseName, String tableName) {
+        try {
+            glueClient.getTable(builder -> 
builder.databaseName(databaseName).name(tableName));
+            return true;
+        } catch (EntityNotFoundException e) {
+            return false;
+        } catch (GlueException e) {
+            throw new CatalogException("Error checking table existence: " + 
databaseName + "." + tableName, e);
+        }
+    }
+
+    /**
+     * Lists all tables in a given database.
+     *
+     * @param databaseName The name of the database from which to list tables.
+     * @return A list of table names.
+     * @throws CatalogException if there is an error fetching the table list.
+     */
+    public List<String> listTables(String databaseName) {
+        try {
+            List<String> tableNames = new ArrayList<>();
+            String nextToken = null;
+
+            do {

Review Comment:
   Applied



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

Reply via email to