yihua commented on code in PR #13532: URL: https://github.com/apache/hudi/pull/13532#discussion_r2207712201
########## hudi-common/src/test/java/org/apache/hudi/common/table/read/TestKeyBasedFileGroupRecordBuffer.java: ########## @@ -0,0 +1,322 @@ +/* + * 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.avro.HoodieAvroReaderContext; +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.model.BaseAvroPayload; +import org.apache.hudi.common.model.DeleteRecord; +import org.apache.hudi.common.model.HoodieAvroIndexedRecord; +import org.apache.hudi.common.model.HoodieAvroRecordMerger; +import org.apache.hudi.common.model.HoodieKey; +import org.apache.hudi.common.model.HoodieRecord; +import org.apache.hudi.common.model.HoodieRecordMerger; +import org.apache.hudi.common.model.HoodieRecordPayload; +import org.apache.hudi.common.table.HoodieTableConfig; +import org.apache.hudi.common.table.HoodieTableMetaClient; +import org.apache.hudi.common.table.log.block.HoodieDataBlock; +import org.apache.hudi.common.table.log.block.HoodieDeleteBlock; +import org.apache.hudi.common.util.Option; +import org.apache.hudi.common.util.collection.ClosableIterator; +import org.apache.hudi.common.util.collection.Pair; +import org.apache.hudi.internal.schema.InternalSchema; +import org.apache.hudi.storage.StorageConfiguration; + +import org.apache.avro.Schema; +import org.apache.avro.generic.GenericData; +import org.apache.avro.generic.GenericRecord; +import org.apache.avro.generic.IndexedRecord; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.Mockito.RETURNS_DEEP_STUBS; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +class TestKeyBasedFileGroupRecordBuffer { + private static final Schema SCHEMA = Schema.createRecord("test_record", null, "namespace", false, + Arrays.asList( + new Schema.Field("record_key", Schema.create(Schema.Type.STRING)), + new Schema.Field("counter", Schema.create(Schema.Type.INT)), + new Schema.Field("ts", Schema.create(Schema.Type.LONG)))); + private final IndexedRecord testRecord1 = createTestRecord("1", 1, 1L); + private final IndexedRecord testRecord1UpdateWithSameTime = createTestRecord("1", 2, 1L); + private final IndexedRecord testRecord2 = createTestRecord("2", 1, 1L); + private final IndexedRecord testRecord2Update = createTestRecord("2", 1, 2L); + private final IndexedRecord testRecord2EarlierUpdate = createTestRecord("2", 1, 0L); + private final IndexedRecord testRecord2Delete = createTestRecord("2", 2, 3L); + private final IndexedRecord testRecord3 = createTestRecord("3", 1, 1L); + private final IndexedRecord testRecord3Update = createTestRecord("3", 1, 2L); + private final IndexedRecord testRecord3DeleteByFieldValue = createTestRecord("3", 3, 1L); + private final IndexedRecord testRecord4 = createTestRecord("4", 2, 1L); + private final IndexedRecord testRecord4Update = createTestRecord("4", 1, 2L); + + @Test + void readWithEventTimeOrdering() throws IOException { + HoodieReadStats readStats = new HoodieReadStats(); + HoodieTableConfig tableConfig = mock(HoodieTableConfig.class); + when(tableConfig.getPayloadClass()).thenReturn(CustomPayload.class.getName()); + when(tableConfig.getRecordKeyFields()).thenReturn(Option.of(new String[]{"record_key"})); + StorageConfiguration<?> storageConfiguration = mock(StorageConfiguration.class); + HoodieReaderContext<IndexedRecord> readerContext = new HoodieAvroReaderContext(storageConfiguration, tableConfig, Option.empty(), Option.empty()); + KeyBasedFileGroupRecordBuffer<IndexedRecord> fileGroupRecordBuffer = buildSortedKeyBasedFileGroupRecordBuffer(readerContext, tableConfig, readStats, null, + RecordMergeMode.EVENT_TIME_ORDERING, Option.of("ts"), Option.of(Pair.of("counter", "3"))); + + fileGroupRecordBuffer.setBaseFileIterator(ClosableIterator.wrap(Arrays.asList(testRecord1, testRecord2, testRecord3).iterator())); + + HoodieDataBlock dataBlock = mock(HoodieDataBlock.class); + when(dataBlock.getSchema()).thenReturn(SCHEMA); + when(dataBlock.getEngineRecordIterator(readerContext)).thenReturn(ClosableIterator.wrap(Arrays.asList(testRecord1UpdateWithSameTime, testRecord2Update, testRecord2EarlierUpdate, + testRecord3Update, testRecord3DeleteByFieldValue).iterator())); + + fileGroupRecordBuffer.processDataBlock(dataBlock, Option.empty()); + + List<IndexedRecord> actualRecords = getActualRecords(fileGroupRecordBuffer); + // delete for record3 is ignored due to event time ordering + assertEquals(Arrays.asList(testRecord1UpdateWithSameTime, testRecord2Update, testRecord3Update), actualRecords); + } + + @Test + void readWithEventTimeOrderingAndDeleteBlock() throws IOException { + HoodieReadStats readStats = new HoodieReadStats(); + HoodieTableConfig tableConfig = mock(HoodieTableConfig.class); + when(tableConfig.getPayloadClass()).thenReturn(CustomPayload.class.getName()); + when(tableConfig.getRecordKeyFields()).thenReturn(Option.of(new String[]{"record_key"})); + StorageConfiguration<?> storageConfiguration = mock(StorageConfiguration.class); + HoodieReaderContext<IndexedRecord> readerContext = new HoodieAvroReaderContext(storageConfiguration, tableConfig, Option.empty(), Option.empty()); + KeyBasedFileGroupRecordBuffer<IndexedRecord> fileGroupRecordBuffer = buildSortedKeyBasedFileGroupRecordBuffer(readerContext, tableConfig, readStats, null, Review Comment: nit: the `Sorted` in the method name seems unnecessary. ########## hudi-common/src/test/java/org/apache/hudi/common/table/read/TestKeyBasedFileGroupRecordBuffer.java: ########## @@ -0,0 +1,322 @@ +/* + * 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.avro.HoodieAvroReaderContext; +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.model.BaseAvroPayload; +import org.apache.hudi.common.model.DeleteRecord; +import org.apache.hudi.common.model.HoodieAvroIndexedRecord; +import org.apache.hudi.common.model.HoodieAvroRecordMerger; +import org.apache.hudi.common.model.HoodieKey; +import org.apache.hudi.common.model.HoodieRecord; +import org.apache.hudi.common.model.HoodieRecordMerger; +import org.apache.hudi.common.model.HoodieRecordPayload; +import org.apache.hudi.common.table.HoodieTableConfig; +import org.apache.hudi.common.table.HoodieTableMetaClient; +import org.apache.hudi.common.table.log.block.HoodieDataBlock; +import org.apache.hudi.common.table.log.block.HoodieDeleteBlock; +import org.apache.hudi.common.util.Option; +import org.apache.hudi.common.util.collection.ClosableIterator; +import org.apache.hudi.common.util.collection.Pair; +import org.apache.hudi.internal.schema.InternalSchema; +import org.apache.hudi.storage.StorageConfiguration; + +import org.apache.avro.Schema; +import org.apache.avro.generic.GenericData; +import org.apache.avro.generic.GenericRecord; +import org.apache.avro.generic.IndexedRecord; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.Mockito.RETURNS_DEEP_STUBS; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +class TestKeyBasedFileGroupRecordBuffer { + private static final Schema SCHEMA = Schema.createRecord("test_record", null, "namespace", false, + Arrays.asList( + new Schema.Field("record_key", Schema.create(Schema.Type.STRING)), + new Schema.Field("counter", Schema.create(Schema.Type.INT)), + new Schema.Field("ts", Schema.create(Schema.Type.LONG)))); + private final IndexedRecord testRecord1 = createTestRecord("1", 1, 1L); + private final IndexedRecord testRecord1UpdateWithSameTime = createTestRecord("1", 2, 1L); + private final IndexedRecord testRecord2 = createTestRecord("2", 1, 1L); + private final IndexedRecord testRecord2Update = createTestRecord("2", 1, 2L); + private final IndexedRecord testRecord2EarlierUpdate = createTestRecord("2", 1, 0L); + private final IndexedRecord testRecord2Delete = createTestRecord("2", 2, 3L); + private final IndexedRecord testRecord3 = createTestRecord("3", 1, 1L); + private final IndexedRecord testRecord3Update = createTestRecord("3", 1, 2L); + private final IndexedRecord testRecord3DeleteByFieldValue = createTestRecord("3", 3, 1L); + private final IndexedRecord testRecord4 = createTestRecord("4", 2, 1L); + private final IndexedRecord testRecord4Update = createTestRecord("4", 1, 2L); + + @Test + void readWithEventTimeOrdering() throws IOException { + HoodieReadStats readStats = new HoodieReadStats(); + HoodieTableConfig tableConfig = mock(HoodieTableConfig.class); + when(tableConfig.getPayloadClass()).thenReturn(CustomPayload.class.getName()); Review Comment: Is this required? -- 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]
