gguptp commented on code in PR #206: URL: https://github.com/apache/flink-connector-aws/pull/206#discussion_r4110358832
########## 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, Review Comment: following my comment on GlueTableUtils about reordering, that also leads to schema corruption, because parameter is keyed by declared position and addPhysicalColumn reads it back by index in the reordered list. For `region STRING, ts TIMESTAMP(3), id INT PARTITIONED BY (region)`: we write `column.1.data-type = TIMESTAMP(3)` meaning ts, but the rebuilt list is [ts, id, region] so index 1 is id. It reads back as ts TIMESTAMP(6), id TIMESTAMP(3), region STRING. id is an INT column returning as a timestamp. -- 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]
