wuchong commented on a change in pull request #10213: [FLINK-12846][table-common] Carry primary key information in TableSchema URL: https://github.com/apache/flink/pull/10213#discussion_r347136245
########## File path: flink-table/flink-table-common/src/main/java/org/apache/flink/table/utils/TableSchemaValidation.java ########## @@ -0,0 +1,168 @@ +/* + * 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.utils; + +import org.apache.flink.table.api.TableColumn; +import org.apache.flink.table.api.ValidationException; +import org.apache.flink.table.api.WatermarkSpec; +import org.apache.flink.table.types.DataType; +import org.apache.flink.table.types.FieldsDataType; +import org.apache.flink.table.types.logical.LogicalType; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.function.Function; +import java.util.stream.Collectors; + +import static org.apache.flink.table.types.logical.LogicalTypeRoot.TIMESTAMP_WITHOUT_TIME_ZONE; + +/** + * Utilities for {@link org.apache.flink.table.api.TableSchema} validation. + */ +public final class TableSchemaValidation { + + /** + * Validate the field names {@code fieldNames} and field types {@code fieldTypes} + * have equal number. + * + * @param fieldNames Field names + * @param fieldTypes Field data types + */ + public static void validateNameTypeNumberEqual(String[] fieldNames, DataType[] fieldTypes) { + if (fieldNames.length != fieldTypes.length) { + throw new ValidationException( + "Number of field names and field data types must be equal.\n" + + "Number of names is " + fieldNames.length + + ", number of data types is " + fieldTypes.length + ".\n" + + "List of field names: " + Arrays.toString(fieldNames) + "\n" + + "List of field data types: " + Arrays.toString(fieldTypes)); + } + } + + /** + * Validate table columns, watermark specification, primary key and unique keys. + */ + public static void validateSchema( + List<TableColumn> columns, + List<WatermarkSpec> watermarkSpecs, + List<String> primaryKey, + List<List<String>> uniqueKeys) { + // validate and create nested field name to type mapping. + // we need nested field name to type mapping because the row time attribute + // field can be nested. + + // this also check duplicate fields. + final Map<String, DataType> fieldNameToType = new HashMap<>(); + for (TableColumn column : columns) { + validateAndBuildNameToTypeMapping(fieldNameToType, + column.getName(), + column.getType(), + ""); + } + + // primary key and unique key requires the key field is top-level (non-nested) + Map<String, TableColumn> nameToColumn = columns.stream() + .collect(Collectors.toMap(TableColumn::getName, Function.identity())); + + // validate primary key + for (String key : primaryKey) { Review comment: Yes. I think we should validate the key fields are unique. ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
