PavithranRick commented on code in PR #17604: URL: https://github.com/apache/hudi/pull/17604#discussion_r2734413281
########## hudi-common/src/main/java/org/apache/hudi/common/table/read/PartialMergerWithKeepValues.java: ########## @@ -0,0 +1,161 @@ +/* + * 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.io.Serializable; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +/** + * 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 PartialMergerWithKeepValues<T> implements Serializable { + private final Map<HoodieSchema, Set<String>> + fieldNameCache = new HashMap<>(); + private final Map<Pair<Pair<HoodieSchema, HoodieSchema>, HoodieSchema>, HoodieSchema> + mergedSchemaCache = new HashMap<>(); + + /** + * 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 = + getCachedFieldNames(highOrderSchema); + // 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) { + String fieldName = mergedSchemaField.name(); + if (fieldNamesInNewRecord.contains(fieldName)) { // field present in newer record. + fieldVals[idx++] = recordContext.getValue(highOrderRecord.getRecord(), highOrderSchema, fieldName); + } else { // if not present in newer record pick from old record + fieldVals[idx++] = recordContext.getValue(lowOrderRecord.getRecord(), lowOrderSchema, fieldName); + } + } + // Build merged record. + T engineRecord = recordContext.constructEngineRecord(mergedSchema, fieldVals); + BufferedRecord<T> mergedRecord = new BufferedRecord<>( + highOrderRecord.getRecordKey(), + highOrderRecord.getOrderingValue(), + engineRecord, + recordContext.encodeSchema(mergedSchema), + highOrderRecord.getHoodieOperation()); + return Pair.of(mergedRecord, mergedSchema); + } + + /** + * @param hoodieSchema Hoodie schema. + * @return The set of field names. + */ + Set<String> getCachedFieldNames(HoodieSchema hoodieSchema) { + return fieldNameCache.computeIfAbsent(hoodieSchema, schema -> { + Set<String> fieldNames = new HashSet<>(); + for (HoodieSchemaField field : schema.getFields()) { + fieldNames.add(field.name()); + } + return fieldNames; + }); + } + + /** + * Merges the two schemas so the merged schema contains all the fields from the two schemas, + * with the same ordering of fields based on the provided reader schema. + * + * @param oldSchema Old schema. + * @param newSchema New schema. + * @param readerSchema Reader schema containing all the fields to read. + * @return The merged Avro schema. + */ + HoodieSchema getCachedMergedSchema(HoodieSchema oldSchema, Review Comment: Used in test failed -- 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]
