JingsongLi commented on code in PR #1678: URL: https://github.com/apache/incubator-paimon/pull/1678#discussion_r1279013433
########## paimon-core/src/main/java/org/apache/paimon/schema/SchemaMergingUtils.java: ########## @@ -0,0 +1,196 @@ +/* + * 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.paimon.schema; + +import org.apache.paimon.types.ArrayType; +import org.apache.paimon.types.DataField; +import org.apache.paimon.types.DataType; +import org.apache.paimon.types.DataTypeCasts; +import org.apache.paimon.types.DecimalType; +import org.apache.paimon.types.ILength; +import org.apache.paimon.types.IPrecision; +import org.apache.paimon.types.MapType; +import org.apache.paimon.types.MultisetType; +import org.apache.paimon.types.ReassignFieldId; +import org.apache.paimon.types.RowType; + +import java.util.List; +import java.util.Map; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.function.Function; +import java.util.stream.Collectors; + +/** The util class for merging the schemas. */ +public class SchemaMergingUtils { + + public static TableSchema mergeSchemas(TableSchema currentTableSchema, RowType dataFields) { + if (currentTableSchema.logicalRowType().equals(dataFields)) { + return currentTableSchema; + } + + AtomicInteger highestFieldId = new AtomicInteger(currentTableSchema.highestFieldId()); + RowType newRowType = + mergeSchemas(currentTableSchema.logicalRowType(), dataFields, highestFieldId); + return new TableSchema( + currentTableSchema.id() + 1, + newRowType.getFields(), + highestFieldId.get(), + currentTableSchema.partitionKeys(), + currentTableSchema.primaryKeys(), + currentTableSchema.options(), + currentTableSchema.comment()); + } + + public static RowType mergeSchemas( + RowType tableSchema, RowType dataSchema, AtomicInteger highestFieldId) { + return (RowType) merge(tableSchema, dataSchema, highestFieldId); + } + + /** + * Merge the base data type and the update data type if possible. + * + * <p>For RowType, find the fields which exists in both the base schema and the update schema, + * and try to merge them by calling the method iteratively; remain those fields that are only in + * the base schema and append those fields that are only in the update schema. + * + * <p>For other complex type, try to merge the element types. + * + * <p>For primitive data type, we treat that's compatible if the original type can be safely + * cast to the new type. + */ + static DataType merge(DataType base0, DataType update0, AtomicInteger highestFieldId) { + // Here we try t0 merge the base0 and update0 without regard to the nullability, + // and set the base0's nullability to the return's. + DataType base = base0.copy(true); + DataType update = update0.copy(true); + + if (base.equals(update)) { + return base0; + } else if (base instanceof RowType && update instanceof RowType) { Review Comment: Sounds reasonable to me. -- 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]
