PavithranRick commented on code in PR #17604: URL: https://github.com/apache/hudi/pull/17604#discussion_r2723222105
########## hudi-common/src/main/java/org/apache/hudi/common/table/read/PartialMergerWithKeepValues.java: ########## @@ -0,0 +1,163 @@ +/* + * 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.hudi.common.table.read; + +import org.apache.hudi.common.engine.RecordContext; +import org.apache.hudi.common.schema.HoodieSchema; +import org.apache.hudi.common.schema.HoodieSchemaField; +import org.apache.hudi.common.schema.HoodieSchemaType; +import org.apache.hudi.common.util.VisibleForTesting; +import org.apache.hudi.common.util.collection.Pair; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; + +/** + * Class to assist with merging two versions of the record that may contain partial updates using + * {@link org.apache.hudi.common.table.PartialUpdateMode#KEEP_VALUES} mode. + */ +public class KeepValuesPartialMergingUtils<T> { + static KeepValuesPartialMergingUtils INSTANCE = new KeepValuesPartialMergingUtils(); + private static final Map<HoodieSchema, Map<String, Integer>> + FIELD_NAME_TO_ID_MAPPING_CACHE = new ConcurrentHashMap<>(); + private static final Map<Pair<Pair<HoodieSchema, HoodieSchema>, HoodieSchema>, HoodieSchema> + MERGED_SCHEMA_CACHE = new ConcurrentHashMap<>(); + + /** + * Merges records which can contain partial updates. + * + * @param lowOrderRecord record with lower commit time or lower ordering value + * @param lowOrderSchema The schema of the older record + * @param highOrderRecord record with higher commit time or higher ordering value + * @param highOrderSchema The schema of highOrderRecord + * @param newSchema The schema of the new incoming record + * @return The merged record and schema. + */ + Pair<BufferedRecord<T>, HoodieSchema> mergePartialRecords(BufferedRecord<T> lowOrderRecord, + HoodieSchema lowOrderSchema, + BufferedRecord<T> highOrderRecord, + HoodieSchema highOrderSchema, + HoodieSchema newSchema, + RecordContext<T> recordContext) { + // The merged schema contains fields that only appear in either older and/or newer record. + HoodieSchema mergedSchema = + getCachedMergedSchema(lowOrderSchema, highOrderSchema, newSchema); + boolean isNewerPartial = isPartial(highOrderSchema, mergedSchema); + if (!isNewerPartial) { + return Pair.of(highOrderRecord, highOrderSchema); + } + Set<String> fieldNamesInNewRecord = + getCachedFieldNameToIdMapping(highOrderSchema).keySet(); + // Collect field values. + List<HoodieSchemaField> fields = mergedSchema.getFields(); + Object[] fieldVals = new Object[fields.size()]; + int idx = 0; + List<HoodieSchemaField> mergedSchemaFields = mergedSchema.getFields(); + for (HoodieSchemaField mergedSchemaField : mergedSchemaFields) { Review Comment: sure will create a separate issue for follow up -- 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]
