JingsongLi commented on code in PR #1678: URL: https://github.com/apache/incubator-paimon/pull/1678#discussion_r1278754288
########## 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 { Review Comment: Maybe we can merge this class to `SchemaManager`? And introduce a method to `mergeSchema(RowType rowType)` to `FileStore`? ########## paimon-common/src/main/java/org/apache/paimon/types/ILength.java: ########## @@ -0,0 +1,25 @@ +/* + * 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.types; + +/** An interface with a getLength method. */ +public interface ILength { Review Comment: Do you think https://github.com/apache/incubator-paimon/pull/1685 works for you? ########## 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) { + List<DataField> baseFields = ((RowType) base).getFields(); + List<DataField> updateFields = ((RowType) update).getFields(); + Map<String, DataField> updateFieldMap = + updateFields.stream() + .collect(Collectors.toMap(DataField::name, Function.identity())); + List<DataField> updatedFields = + baseFields.stream() + .map( + baseField -> { + if (updateFieldMap.containsKey(baseField.name())) { + DataField updateField = + updateFieldMap.get(baseField.name()); + DataType updatedDataType = + merge( + baseField.type(), + updateField.type(), + highestFieldId); + return new DataField( + baseField.id(), + baseField.name(), + updatedDataType, + baseField.description()); + } else { + return baseField; + } + }) + .collect(Collectors.toList()); + + Map<String, DataField> baseFieldMap = + baseFields.stream() + .collect(Collectors.toMap(DataField::name, Function.identity())); + List<DataField> newFields = + updateFields.stream() + .filter(field -> !baseFieldMap.containsKey(field.name())) + .map(field -> assignIdForNewField(field, highestFieldId)) + .collect(Collectors.toList()); + + updatedFields.addAll(newFields); + return new RowType(base.isNullable(), updatedFields); + } else if (base instanceof MapType && update instanceof MapType) { + return new MapType( + base.isNullable(), + merge( + ((MapType) base).getKeyType(), + ((MapType) update).getKeyType(), + highestFieldId), + merge( + ((MapType) base).getValueType(), + ((MapType) update).getValueType(), + highestFieldId)); + } else if (base instanceof ArrayType && update instanceof ArrayType) { + return new ArrayType( + base.isNullable(), + merge( + ((ArrayType) base).getElementType(), + ((ArrayType) update).getElementType(), + highestFieldId)); + } else if (base instanceof MultisetType && update instanceof MultisetType) { + return new MultisetType( + base.isNullable(), + merge( + ((MultisetType) base).getElementType(), + ((MultisetType) update).getElementType(), + highestFieldId)); + } else if (base instanceof DecimalType && update instanceof DecimalType) { + if (base.equals(update)) { + return base0; + } else { + throw new UnsupportedOperationException( + String.format( + "Failed to merge decimal types with different precision or scale: %s and %s", + base, update)); + } + } else if (DataTypeCasts.supportsImplicitCast(base, update)) { Review Comment: Currently, we have using `supportsExplicitCast` to manually casting (From DDL alter table). Do you think we should use `supportsExplicitCast`? Or provide an option to let user to choose? ########## 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: Maybe here can use `DataTypeDefaultVisitor`? -- 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]
