fmorillo7694 commented on code in PR #206:
URL: 
https://github.com/apache/flink-connector-aws/pull/206#discussion_r4125195886


##########
flink-catalog-aws/flink-catalog-aws-glue/src/main/java/org/apache/flink/table/catalog/glue/util/GlueCatalogConstants.java:
##########
@@ -0,0 +1,86 @@
+/*
+ * 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.annotation.Internal;
+
+import java.util.regex.Pattern;
+
+/**
+ * Constants used throughout the Glue catalog implementation. Includes string 
literals, patterns,
+ * and configuration keys.
+ */
+@Internal
+public class GlueCatalogConstants {

Review Comment:
   Done — twelve unused constants removed; every remaining constant has at 
least one reference.



##########
flink-catalog-aws/flink-catalog-aws-glue/src/main/java/org/apache/flink/table/catalog/glue/util/ConnectorRegistry.java:
##########
@@ -0,0 +1,71 @@
+/*
+ * 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.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * This class is responsible for storing and retrieving location-specific keys 
for different
+ * connectors. It maps connector types to their corresponding location keys 
(e.g., Kinesis, Kafka).
+ */
+public class ConnectorRegistry {
+
+    /** Logger for logging connector registry actions. */
+    private static final Logger LOG = 
LoggerFactory.getLogger(ConnectorRegistry.class);
+
+    /** Map to store connector types and their corresponding location-specific 
keys. */
+    private static final Map<String, String> connectorLocationKeys = new 
HashMap<>();

Review Comment:
   Renamed to `CONNECTOR_LOCATION_KEYS` (and the class is `@Internal`).



##########
flink-catalog-aws/flink-catalog-aws-glue/src/main/java/org/apache/flink/table/catalog/glue/util/ConnectorRegistry.java:
##########
@@ -0,0 +1,71 @@
+/*
+ * 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.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * This class is responsible for storing and retrieving location-specific keys 
for different
+ * connectors. It maps connector types to their corresponding location keys 
(e.g., Kinesis, Kafka).
+ */
+public class ConnectorRegistry {

Review Comment:
   Added.



##########
flink-catalog-aws/flink-catalog-aws-glue/src/main/java/org/apache/flink/table/catalog/glue/util/ConnectorRegistry.java:
##########
@@ -0,0 +1,71 @@
+/*
+ * 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.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * This class is responsible for storing and retrieving location-specific keys 
for different
+ * connectors. It maps connector types to their corresponding location keys 
(e.g., Kinesis, Kafka).
+ */
+public class ConnectorRegistry {
+
+    /** Logger for logging connector registry actions. */
+    private static final Logger LOG = 
LoggerFactory.getLogger(ConnectorRegistry.class);
+
+    /** Map to store connector types and their corresponding location-specific 
keys. */
+    private static final Map<String, String> connectorLocationKeys = new 
HashMap<>();
+
+    // Static block to initialize the connector keys mapping.
+    static {
+        connectorLocationKeys.put("kinesis", "stream.arn");
+        connectorLocationKeys.put("kafka", "properties.bootstrap.servers");
+        connectorLocationKeys.put("jdbc", "url");
+        connectorLocationKeys.put("filesystem", "path");
+        connectorLocationKeys.put("elasticsearch", "hosts");
+        connectorLocationKeys.put("opensearch", "hosts");
+        connectorLocationKeys.put("hbase", "zookeeper.quorum");
+        connectorLocationKeys.put("dynamodb", "table.name");
+        connectorLocationKeys.put("mongodb", "uri");
+        connectorLocationKeys.put("hive", "hive-conf-dir");
+        // Additional connectors can be added here as needed.
+    }
+
+    /**
+     * Retrieves the location-specific key for a given connector type.
+     *
+     * @param connectorType The type of the connector (e.g., "kinesis", 
"kafka").
+     * @return The location-specific key corresponding to the connector type, 
or null if not found.
+     */
+    public static String getLocationKey(String connectorType) {
+        // Log the lookup request.
+        LOG.debug("Looking up location key for connector type: {}", 
connectorType);
+
+        // Check if the connector type exists and return the corresponding key.
+        String locationKey = connectorLocationKeys.get(connectorType);
+        if (locationKey == null) {
+            LOG.warn("No location key found for connector type: {}", 
connectorType);

Review Comment:
   Agreed — it is `debug` now; an unknown connector is normal, it just means no 
storage location is recorded.



##########
flink-catalog-aws/flink-catalog-aws-glue/src/main/java/org/apache/flink/table/catalog/glue/util/GlueFlinkSchemaProperties.java:
##########
@@ -0,0 +1,338 @@
+/*
+ * 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.annotation.Internal;
+import org.apache.flink.table.api.DataTypes;
+import org.apache.flink.table.api.Schema;
+import org.apache.flink.table.catalog.Column;
+import org.apache.flink.table.catalog.ResolvedSchema;
+import org.apache.flink.table.catalog.WatermarkSpec;
+import org.apache.flink.table.catalog.exceptions.CatalogException;
+import org.apache.flink.table.types.logical.LogicalType;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.TreeMap;
+import java.util.function.Consumer;
+
+/**
+ * Persists the parts of a Flink schema that AWS Glue columns cannot represent 
- computed columns,
+ * metadata columns, watermarks, and the primary key - as Glue table 
parameters, and restores them
+ * when a table is read back.
+ *
+ * <p>Physical columns are stored as native Glue columns (so other engines can 
read the table),
+ * while everything else round-trips through {@code flink.schema.*} 
parameters. Without this, {@code
+ * CREATE TABLE} statements with watermarks or primary keys would silently 
lose them on read back,
+ * and computed or metadata columns would be corrupted into physical columns.
+ */
+@Internal
+public final class GlueFlinkSchemaProperties {
+
+    /** Prefix shared by all schema-fidelity parameters written by this class. 
*/
+    public static final String SCHEMA_PARAMETER_PREFIX = "flink.schema.";
+
+    private static final String COLUMN_PREFIX = SCHEMA_PARAMETER_PREFIX + 
"column.";
+    private static final String COMPUTED_NAME_SUFFIX = ".computed.name";
+    private static final String COMPUTED_EXPR_SUFFIX = ".computed.expr";
+    private static final String METADATA_NAME_SUFFIX = ".metadata.name";
+    private static final String METADATA_DATA_TYPE_SUFFIX = 
".metadata.data-type";
+    private static final String METADATA_KEY_SUFFIX = ".metadata.key";
+    private static final String METADATA_VIRTUAL_SUFFIX = ".metadata.virtual";
+    private static final String PHYSICAL_DATA_TYPE_SUFFIX = ".data-type";
+
+    private static final String WATERMARK_PREFIX = SCHEMA_PARAMETER_PREFIX + 
"watermark.";
+    private static final String WATERMARK_ROWTIME_SUFFIX = ".rowtime";
+    private static final String WATERMARK_STRATEGY_EXPR_SUFFIX = 
".strategy.expr";
+
+    private static final String PRIMARY_KEY_NAME = SCHEMA_PARAMETER_PREFIX + 
"primary-key.name";
+    private static final String PRIMARY_KEY_COLUMNS =
+            SCHEMA_PARAMETER_PREFIX + "primary-key.columns";
+
+    /**
+     * Comma-joined names of physical columns declared NOT NULL. Glue type 
strings carry no
+     * nullability, so without this the restored primary key would fail schema 
resolution ("Invalid
+     * primary key: column is nullable").
+     */
+    private static final String NOT_NULL_COLUMNS = SCHEMA_PARAMETER_PREFIX + 
"not-null-columns";
+
+    private static final GlueTypeConverter TYPE_CONVERTER = new 
GlueTypeConverter();
+
+    private GlueFlinkSchemaProperties() {}
+
+    /**
+     * Serializes the non-physical parts of the given schema into the target 
parameter map.
+     *
+     * @param resolvedSchema the resolved Flink schema
+     * @param targetParameters the mutable Glue table parameter map to write 
into
+     * @throws CatalogException if an expression cannot be serialized to SQL
+     */
+    public static void serializeNonPhysicalSchema(
+            ResolvedSchema resolvedSchema, Map<String, String> 
targetParameters) {
+        List<Column> columns = resolvedSchema.getColumns();
+        List<String> notNullColumns = new ArrayList<>();
+        for (int i = 0; i < columns.size(); i++) {
+            Column column = columns.get(i);
+            if (column instanceof Column.PhysicalColumn) {
+                if (!column.getDataType().getLogicalType().isNullable()) {
+                    notNullColumns.add(column.getName());
+                }
+                // Glue's Hive-style type strings are lossy for some types 
(for example
+                // TIMESTAMP(3) has no Glue representation and would come back 
as
+                // TIMESTAMP(6)). When the Glue round-trip does not reproduce 
the declared
+                // type, record the original so the read path can restore it 
exactly.
+                LogicalType declared = column.getDataType().getLogicalType();
+                LogicalType glueRoundTrip =
+                        TYPE_CONVERTER
+                                .toFlinkDataType(
+                                        
TYPE_CONVERTER.toGlueDataType(column.getDataType()))
+                                .getLogicalType();
+                if (!declared.copy(true).equals(glueRoundTrip.copy(true))) {
+                    targetParameters.put(
+                            COLUMN_PREFIX + i + PHYSICAL_DATA_TYPE_SUFFIX,
+                            declared.asSerializableString());
+                }
+            }
+            if (column instanceof Column.ComputedColumn) {
+                Column.ComputedColumn computed = (Column.ComputedColumn) 
column;
+                targetParameters.put(COLUMN_PREFIX + i + COMPUTED_NAME_SUFFIX, 
column.getName());
+                targetParameters.put(
+                        COLUMN_PREFIX + i + COMPUTED_EXPR_SUFFIX,
+                        serializeExpression(column.getName(), 
computed.getExpression()));
+            } else if (column instanceof Column.MetadataColumn) {
+                Column.MetadataColumn metadata = (Column.MetadataColumn) 
column;
+                final int position = i;
+                targetParameters.put(COLUMN_PREFIX + i + METADATA_NAME_SUFFIX, 
column.getName());
+                targetParameters.put(
+                        COLUMN_PREFIX + i + METADATA_DATA_TYPE_SUFFIX,
+                        
column.getDataType().getLogicalType().asSerializableString());
+                metadata.getMetadataKey()
+                        .ifPresent(
+                                key ->
+                                        targetParameters.put(
+                                                COLUMN_PREFIX + position + 
METADATA_KEY_SUFFIX,
+                                                key));
+                targetParameters.put(
+                        COLUMN_PREFIX + i + METADATA_VIRTUAL_SUFFIX,
+                        String.valueOf(metadata.isVirtual()));
+            }
+        }
+
+        if (!notNullColumns.isEmpty()) {
+            targetParameters.put(NOT_NULL_COLUMNS, String.join(",", 
notNullColumns));
+        }
+
+        List<WatermarkSpec> watermarkSpecs = 
resolvedSchema.getWatermarkSpecs();
+        for (int i = 0; i < watermarkSpecs.size(); i++) {
+            WatermarkSpec spec = watermarkSpecs.get(i);
+            targetParameters.put(
+                    WATERMARK_PREFIX + i + WATERMARK_ROWTIME_SUFFIX, 
spec.getRowtimeAttribute());
+            targetParameters.put(
+                    WATERMARK_PREFIX + i + WATERMARK_STRATEGY_EXPR_SUFFIX,
+                    serializeExpression(spec.getRowtimeAttribute(), 
spec.getWatermarkExpression()));
+        }
+
+        resolvedSchema
+                .getPrimaryKey()
+                .ifPresent(
+                        primaryKey -> {
+                            targetParameters.put(PRIMARY_KEY_NAME, 
primaryKey.getName());
+                            targetParameters.put(
+                                    PRIMARY_KEY_COLUMNS, String.join(",", 
primaryKey.getColumns()));
+                        });
+    }
+
+    /**
+     * Returns whether the given Glue table parameter key was written by {@link
+     * #serializeNonPhysicalSchema} and must therefore be hidden from the 
table options exposed to
+     * users.
+     *
+     * @param parameterKey the Glue table parameter key
+     * @return true when the key is an internal schema-fidelity parameter
+     */
+    public static boolean isSchemaParameter(String parameterKey) {
+        return parameterKey.startsWith(SCHEMA_PARAMETER_PREFIX);
+    }
+
+    /**
+     * Restores computed and metadata columns from the Glue table parameters, 
merging them with the
+     * physical columns at their originally declared positions, then 
re-applies watermarks and the
+     * primary key.
+     *
+     * @param parameters the Glue table parameters (may be null)
+     * @param physicalColumnAdders ordered adders for the physical columns 
read from Glue

Review Comment:
   Renamed as part of the rewrite (`parameters` / `glueColumns` / `comments`).



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