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


##########
flink-catalog-aws-glue/src/main/java/org/apache/flink/table/catalog/glue/util/GlueTypeConverter.java:
##########
@@ -0,0 +1,311 @@
+/*
+ * 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.util;
+
+import org.apache.flink.table.api.DataTypes;
+import org.apache.flink.table.types.DataType;
+import org.apache.flink.table.types.logical.ArrayType;
+import org.apache.flink.table.types.logical.DecimalType;
+import org.apache.flink.table.types.logical.LogicalType;
+import org.apache.flink.table.types.logical.LogicalTypeRoot;
+import org.apache.flink.table.types.logical.MapType;
+import org.apache.flink.table.types.logical.RowType;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ * Utility class for converting Flink types to Glue types and vice versa.
+ * Supports the conversion of common primitive, array, map, and struct types.
+ */
+public class GlueTypeConverter {
+
+    /** Logger for tracking Glue type conversions. */
+    private static final Logger LOG = 
LoggerFactory.getLogger(GlueTypeConverter.class);
+
+    /** Regular expressions for handling specific Glue types. */
+    private static final Pattern DECIMAL_PATTERN = 
Pattern.compile("decimal\\((\\d+),(\\d+)\\)");
+    private static final Pattern ARRAY_PATTERN = 
Pattern.compile("array<(.+)>");
+    private static final Pattern MAP_PATTERN = 
Pattern.compile("map<(.+),(.+)>");
+    private static final Pattern STRUCT_PATTERN = 
Pattern.compile("struct<(.+)>");
+
+    /**
+     * Converts a Flink DataType to its corresponding Glue type as a string.
+     *
+     * @param flinkType The Flink DataType to be converted.
+     * @return The Glue type as a string.
+     */
+    public String toGlueType(DataType flinkType) {
+        LogicalType logicalType = flinkType.getLogicalType();
+        LogicalTypeRoot typeRoot = logicalType.getTypeRoot();
+
+        // Handle various Flink types and map them to corresponding Glue types
+        switch (typeRoot) {
+            case CHAR:
+            case VARCHAR:
+                return "string";
+            case BOOLEAN:
+                return "boolean";
+            case BINARY:
+            case VARBINARY:
+                return "binary";
+            case DECIMAL:
+                DecimalType decimalType = (DecimalType) logicalType;
+                return String.format("decimal(%d,%d)", 
decimalType.getPrecision(), decimalType.getScale());
+            case TINYINT:
+                return "tinyint";
+            case SMALLINT:
+                return "smallint";
+            case INTEGER:
+                return "int";
+            case BIGINT:
+                return "bigint";
+            case FLOAT:
+                return "float";
+            case DOUBLE:
+                return "double";
+            case DATE:
+                return "date";
+            case TIME_WITHOUT_TIME_ZONE:
+                return "string"; // Glue doesn't have a direct time type, use 
string
+            case TIMESTAMP_WITHOUT_TIME_ZONE:
+            case TIMESTAMP_WITH_LOCAL_TIME_ZONE:
+                return "timestamp";
+            case ARRAY:
+                ArrayType arrayType = (ArrayType) logicalType;
+                return "array<" + 
toGlueType(DataTypes.of(arrayType.getElementType())) + ">";
+            case MAP:
+                MapType mapType = (MapType) logicalType;
+                return String.format("map<%s,%s>",
+                        toGlueType(DataTypes.of(mapType.getKeyType())),
+                        toGlueType(DataTypes.of(mapType.getValueType())));
+            case ROW:
+                RowType rowType = (RowType) logicalType;
+                StringBuilder structBuilder = new StringBuilder("struct<");
+                for (int i = 0; i < rowType.getFieldCount(); i++) {
+                    if (i > 0) {
+                        structBuilder.append(",");
+                    }
+                    // Keep original field name for nested structs
+                    structBuilder.append(rowType.getFieldNames().get(i))
+                            .append(":")
+                            
.append(toGlueType(DataTypes.of(rowType.getChildren().get(i))));
+                }
+                structBuilder.append(">");
+                return structBuilder.toString();
+            default:
+                throw new UnsupportedOperationException("Unsupported Flink 
type: " + logicalType.getTypeRoot());
+        }
+    }
+
+    /**
+     * Converts a Glue type (as a string) to the corresponding Flink DataType.
+     *
+     * @param glueType The Glue type as a string.
+     * @return The corresponding Flink DataType.
+     * @throws IllegalArgumentException if the Glue type is invalid or unknown.
+     */
+    public DataType toFlinkType(String glueType) {
+        if (glueType == null || glueType.trim().isEmpty()) {
+            throw new IllegalArgumentException("Glue type cannot be null or 
empty");
+        }
+
+        // Trim but don't lowercase - we'll handle case-insensitivity per type

Review Comment:
   This approach is necessary because:
   Glue types like "string", "int", "boolean" should be matched 
case-insensitively (i.e., "STRING" and "string" are the same type)
   It matches AWS Glue's behavior, where type names are case-insensitive



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