nsivabalan commented on code in PR #13411: URL: https://github.com/apache/hudi/pull/13411#discussion_r2151281331
########## hudi-common/src/main/java/org/apache/hudi/common/table/read/SortedKeyBasedFileGroupRecordBuffer.java: ########## @@ -0,0 +1,100 @@ +/* + * 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.config.RecordMergeMode; +import org.apache.hudi.common.config.TypedProperties; +import org.apache.hudi.common.engine.HoodieReaderContext; +import org.apache.hudi.common.table.HoodieTableMetaClient; +import org.apache.hudi.common.util.Option; +import org.apache.hudi.common.util.ValidationUtils; + +import java.io.IOException; +import java.util.Comparator; +import java.util.PriorityQueue; +import java.util.Queue; +import java.util.stream.Collectors; + +/** + * An extension of {@link KeyBasedFileGroupRecordBuffer} that sorts the log records based on the record key when joining with the base file records. + * This assumes that the base file records are already sorted by the record key. + */ +public class SortedKeyBasedFileGroupRecordBuffer<T> extends KeyBasedFileGroupRecordBuffer<T> { + + // when sorting is enabled, this will hold the base file record if it was not used in the previous iteration + private Option<T> queuedBaseFileRecord = Option.empty(); + private Queue<String> logRecordKeysSorted = null; + + public SortedKeyBasedFileGroupRecordBuffer(HoodieReaderContext<T> readerContext, + HoodieTableMetaClient hoodieTableMetaClient, + RecordMergeMode recordMergeMode, + TypedProperties props, + HoodieReadStats readStats, + Option<String> orderingFieldName, + boolean emitDelete) { + super(readerContext, hoodieTableMetaClient, recordMergeMode, props, readStats, orderingFieldName, emitDelete); + } + + @Override + protected void initializeLogRecordIterator() { + logRecordIterator = records.values().stream().sorted(Comparator.comparing(BufferedRecord::getRecordKey)).iterator(); + } + + @Override + protected boolean hasNextBaseRecord(T baseRecord) throws IOException { + String recordKey = readerContext.getRecordKey(baseRecord, readerSchema); + int comparison; + while (!getLogRecordKeysSorted().isEmpty() && (comparison = getLogRecordKeysSorted().peek().compareTo(recordKey)) <= 0) { + String nextLogRecordKey = getLogRecordKeysSorted().poll(); + if (comparison < 0) { + BufferedRecord<T> nextLogRecord = records.remove(nextLogRecordKey); + if (!nextLogRecord.isDelete() || emitDelete) { + nextRecord = nextLogRecord.getRecord(); Review Comment: can we add java docs here. mainly to callout, here we are handling insert records in log files which are not present in base files. ########## hudi-common/src/main/java/org/apache/hudi/metadata/HoodieMetadataPayload.java: ########## @@ -376,19 +380,44 @@ public Option<IndexedRecord> combineAndGetUpdateValue(IndexedRecord oldRecord, S } @Override - public Option<IndexedRecord> getInsertValue(Schema schemaIgnored, Properties propertiesIgnored) throws IOException { + public Option<IndexedRecord> getInsertValue(Schema schema, Properties propertiesIgnored) throws IOException { if (key == null || this.isDeletedRecord) { return Option.empty(); } - HoodieMetadataRecord record = new HoodieMetadataRecord(key, type, filesystemMetadata, bloomFilterMetadata, - columnStatMetadata, recordIndexMetadata, secondaryIndexMetadata); - return Option.of(record); + if (schema == null || HOODIE_METADATA_SCHEMA == schema) { + // If the schema is same or none is provided, we can return the record directly + HoodieMetadataRecord record = new HoodieMetadataRecord(key, type, filesystemMetadata, bloomFilterMetadata, + columnStatMetadata, recordIndexMetadata, secondaryIndexMetadata); + return Option.of(record); + } else { + // Otherwise, the assumption is that the schema required contains the metadata fields so we construct a new GenericRecord with these fields + GenericData.Record record = new GenericData.Record(schema); + int offset = schema.getField("key").pos(); + record.put(offset, key); + record.put(offset + 1, type); + if (filesystemMetadata != null) { Review Comment: similar to how we have HoodieRecord.HOODIE_META_COLUMNS_NAME_TO_POS can we add similar static mapping for metadata table field positions. and use it here. -- 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]
