the-other-tim-brown commented on code in PR #12340:
URL: https://github.com/apache/hudi/pull/12340#discussion_r2047860607


##########
hudi-common/src/test/java/org/apache/hudi/common/table/read/TestSchemaHandler.java:
##########
@@ -0,0 +1,467 @@
+/*
+ * 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.HoodieAvroUtils;
+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.HoodieRecord;
+import org.apache.hudi.common.model.HoodieRecordMerger;
+import org.apache.hudi.common.table.HoodieTableConfig;
+import org.apache.hudi.common.testutils.HoodieTestDataGenerator;
+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.storage.HoodieStorage;
+import org.apache.hudi.storage.StoragePath;
+
+import org.apache.avro.Schema;
+import org.apache.avro.generic.GenericRecord;
+import org.apache.avro.generic.IndexedRecord;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.function.UnaryOperator;
+import java.util.stream.Stream;
+
+import static 
org.apache.hudi.common.config.RecordMergeMode.COMMIT_TIME_ORDERING;
+import static org.apache.hudi.common.config.RecordMergeMode.CUSTOM;
+import static 
org.apache.hudi.common.config.RecordMergeMode.EVENT_TIME_ORDERING;
+import static 
org.apache.hudi.common.table.read.ParquetRowIndexBasedSchemaHandler.addPositionalMergeCol;
+import static 
org.apache.hudi.common.table.read.ParquetRowIndexBasedSchemaHandler.getPositionalMergeField;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public class TestSchemaHandler {
+
+  protected static final Schema DATA_SCHEMA = 
HoodieAvroUtils.addMetadataFields(HoodieTestDataGenerator.AVRO_SCHEMA);
+  protected static final Schema DATA_SCHEMA_NO_DELETE = 
generateProjectionSchema(DATA_SCHEMA.getFields().stream()
+      .map(Schema.Field::name).filter(f -> 
!f.equals("_hoodie_is_deleted")).toArray(String[]::new));
+  protected static final Schema DATA_COLS_ONLY_SCHEMA = 
generateProjectionSchema("begin_lat", "tip_history", "rider");
+  protected static final Schema META_COLS_ONLY_SCHEMA = 
generateProjectionSchema("_hoodie_commit_seqno", "_hoodie_record_key");
+
+  @Test
+  public void testCow() {
+    HoodieReaderContext<String> readerContext = createReaderContext(false, 
false, false, false);
+    HoodieTableConfig hoodieTableConfig = mock(HoodieTableConfig.class);
+    Schema requestedSchema = DATA_SCHEMA;
+    FileGroupReaderSchemaHandler schemaHandler = 
createSchemaHandler(readerContext, DATA_SCHEMA, requestedSchema, 
hoodieTableConfig, false);
+    assertEquals(requestedSchema, schemaHandler.getRequiredSchema());
+
+    //read subset of columns
+    requestedSchema = generateProjectionSchema("begin_lat", "tip_history", 
"rider");
+    schemaHandler = createSchemaHandler(readerContext, DATA_SCHEMA, 
requestedSchema, hoodieTableConfig, false);
+    assertEquals(requestedSchema, schemaHandler.getRequiredSchema());
+    assertFalse(readerContext.getNeedsBootstrapMerge());
+  }
+
+  @Test
+  public void testCowBootstrap() {
+    HoodieReaderContext<String> readerContext = createReaderContext(false, 
false, true, false);
+    HoodieTableConfig hoodieTableConfig = mock(HoodieTableConfig.class);
+    Schema requestedSchema = generateProjectionSchema("begin_lat", 
"tip_history", "_hoodie_record_key", "rider");
+    FileGroupReaderSchemaHandler schemaHandler = 
createSchemaHandler(readerContext, DATA_SCHEMA, requestedSchema, 
hoodieTableConfig, false);
+
+    //meta cols must go first in the required schema
+    Schema expectedRequiredSchema = 
generateProjectionSchema("_hoodie_record_key", "begin_lat", "tip_history", 
"rider");
+    assertEquals(expectedRequiredSchema, schemaHandler.getRequiredSchema());
+    Pair<List<Schema.Field>, List<Schema.Field>> bootstrapFields = 
schemaHandler.getBootstrapRequiredFields();
+    assertEquals(Collections.singletonList(getField("_hoodie_record_key")), 
bootstrapFields.getLeft());
+    assertEquals(Arrays.asList(getField("begin_lat"), getField("tip_history"), 
getField("rider")), bootstrapFields.getRight());
+    assertTrue(readerContext.getNeedsBootstrapMerge());
+  }
+
+  @Test
+  public void testCowBootstrapWithPositionMerge() {
+    HoodieReaderContext<String> readerContext = createReaderContext(true, 
false, true, false);
+    HoodieTableConfig hoodieTableConfig = mock(HoodieTableConfig.class);
+    Schema requestedSchema = generateProjectionSchema("begin_lat", 
"tip_history", "_hoodie_record_key", "rider");
+    FileGroupReaderSchemaHandler schemaHandler = 
createSchemaHandler(readerContext, DATA_SCHEMA, requestedSchema, 
hoodieTableConfig, true);
+    //meta cols must go first in the required schema
+    Schema expectedRequiredSchema = 
generateProjectionSchema("_hoodie_record_key", "begin_lat", "tip_history", 
"rider");
+    assertEquals(expectedRequiredSchema, schemaHandler.getRequiredSchema());
+    Pair<List<Schema.Field>, List<Schema.Field>> bootstrapFields = 
schemaHandler.getBootstrapRequiredFields();
+    assertEquals(Arrays.asList(getField("_hoodie_record_key"), 
getPositionalMergeField()), bootstrapFields.getLeft());
+    assertEquals(Arrays.asList(getField("begin_lat"), getField("tip_history"), 
getField("rider"), getPositionalMergeField()), bootstrapFields.getRight());
+    assertTrue(readerContext.getNeedsBootstrapMerge());
+
+    schemaHandler = createSchemaHandler(readerContext, DATA_SCHEMA, 
DATA_COLS_ONLY_SCHEMA, hoodieTableConfig, true);
+    assertEquals(DATA_COLS_ONLY_SCHEMA, schemaHandler.getRequiredSchema());
+    bootstrapFields = schemaHandler.getBootstrapRequiredFields();
+    assertTrue(bootstrapFields.getLeft().isEmpty());
+    assertEquals(Arrays.asList(getField("begin_lat"), getField("tip_history"), 
getField("rider")), bootstrapFields.getRight());
+    assertFalse(readerContext.getNeedsBootstrapMerge());
+
+    schemaHandler = createSchemaHandler(readerContext, DATA_SCHEMA, 
META_COLS_ONLY_SCHEMA, hoodieTableConfig, true);
+    assertEquals(META_COLS_ONLY_SCHEMA, schemaHandler.getRequiredSchema());
+    bootstrapFields = schemaHandler.getBootstrapRequiredFields();
+    assertEquals(Arrays.asList(getField("_hoodie_commit_seqno"), 
getField("_hoodie_record_key")), bootstrapFields.getLeft());
+    assertTrue(bootstrapFields.getRight().isEmpty());
+    assertFalse(readerContext.getNeedsBootstrapMerge());
+  }
+
+  private static Stream<Arguments> testMorParams() {
+    Stream.Builder<Arguments> b = Stream.builder();
+    for (boolean mergeUseRecordPosition : new boolean[] {true, false}) {
+      for (boolean supportsParquetRowIndex : new boolean[] {true, false}) {
+        for (boolean hasBuiltInDelete : new boolean[] {true, false}) {
+          b.add(Arguments.of(EVENT_TIME_ORDERING, true, false, 
mergeUseRecordPosition, supportsParquetRowIndex, hasBuiltInDelete));
+          b.add(Arguments.of(EVENT_TIME_ORDERING, false, false, 
mergeUseRecordPosition, supportsParquetRowIndex, hasBuiltInDelete));
+          b.add(Arguments.of(COMMIT_TIME_ORDERING, false, false, 
mergeUseRecordPosition, supportsParquetRowIndex, hasBuiltInDelete));
+          b.add(Arguments.of(CUSTOM, false, true, mergeUseRecordPosition, 
supportsParquetRowIndex, hasBuiltInDelete));
+          b.add(Arguments.of(CUSTOM, false, false, mergeUseRecordPosition, 
supportsParquetRowIndex, hasBuiltInDelete));
+        }
+      }
+    }
+    return b.build();
+  }
+
+  @ParameterizedTest
+  @MethodSource("testMorParams")
+  public void testMor(RecordMergeMode mergeMode,
+                      boolean hasPrecombine,
+                      boolean isProjectionCompatible,
+                      boolean mergeUseRecordPosition,
+                      boolean supportsParquetRowIndex,
+                      boolean hasBuiltInDelete) {
+
+    Schema dataSchema = hasBuiltInDelete ? DATA_SCHEMA : DATA_SCHEMA_NO_DELETE;
+    HoodieReaderContext<String> readerContext = 
createReaderContext(supportsParquetRowIndex, true, false, 
mergeUseRecordPosition);
+    HoodieTableConfig hoodieTableConfig = mock(HoodieTableConfig.class);
+    when(hoodieTableConfig.populateMetaFields()).thenReturn(Boolean.TRUE);
+    when(hoodieTableConfig.getRecordMergeMode()).thenReturn(mergeMode);
+    if (hasPrecombine) {
+      when(hoodieTableConfig.getPreCombineField()).thenReturn("timestamp");
+    } else {
+      when(hoodieTableConfig.getPreCombineField()).thenReturn(null);
+    }
+    if (mergeMode == CUSTOM) {
+      when(hoodieTableConfig.getRecordMergeStrategyId()).thenReturn("asdf");

Review Comment:
   You'll want to mock the table version for the config as well. Can you also 
extract the common mock setup for this test and `testMorBootstrap` to resusable 
methods?



##########
hudi-common/src/test/java/org/apache/hudi/common/table/read/TestSchemaHandler.java:
##########
@@ -0,0 +1,467 @@
+/*
+ * 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.HoodieAvroUtils;
+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.HoodieRecord;
+import org.apache.hudi.common.model.HoodieRecordMerger;
+import org.apache.hudi.common.table.HoodieTableConfig;
+import org.apache.hudi.common.testutils.HoodieTestDataGenerator;
+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.storage.HoodieStorage;
+import org.apache.hudi.storage.StoragePath;
+
+import org.apache.avro.Schema;
+import org.apache.avro.generic.GenericRecord;
+import org.apache.avro.generic.IndexedRecord;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.function.UnaryOperator;
+import java.util.stream.Stream;
+
+import static 
org.apache.hudi.common.config.RecordMergeMode.COMMIT_TIME_ORDERING;
+import static org.apache.hudi.common.config.RecordMergeMode.CUSTOM;
+import static 
org.apache.hudi.common.config.RecordMergeMode.EVENT_TIME_ORDERING;
+import static 
org.apache.hudi.common.table.read.ParquetRowIndexBasedSchemaHandler.addPositionalMergeCol;
+import static 
org.apache.hudi.common.table.read.ParquetRowIndexBasedSchemaHandler.getPositionalMergeField;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public class TestSchemaHandler {
+
+  protected static final Schema DATA_SCHEMA = 
HoodieAvroUtils.addMetadataFields(HoodieTestDataGenerator.AVRO_SCHEMA);
+  protected static final Schema DATA_SCHEMA_NO_DELETE = 
generateProjectionSchema(DATA_SCHEMA.getFields().stream()
+      .map(Schema.Field::name).filter(f -> 
!f.equals("_hoodie_is_deleted")).toArray(String[]::new));
+  protected static final Schema DATA_COLS_ONLY_SCHEMA = 
generateProjectionSchema("begin_lat", "tip_history", "rider");
+  protected static final Schema META_COLS_ONLY_SCHEMA = 
generateProjectionSchema("_hoodie_commit_seqno", "_hoodie_record_key");
+
+  @Test
+  public void testCow() {
+    HoodieReaderContext<String> readerContext = createReaderContext(false, 
false, false, false);
+    HoodieTableConfig hoodieTableConfig = mock(HoodieTableConfig.class);
+    Schema requestedSchema = DATA_SCHEMA;
+    FileGroupReaderSchemaHandler schemaHandler = 
createSchemaHandler(readerContext, DATA_SCHEMA, requestedSchema, 
hoodieTableConfig, false);
+    assertEquals(requestedSchema, schemaHandler.getRequiredSchema());
+
+    //read subset of columns
+    requestedSchema = generateProjectionSchema("begin_lat", "tip_history", 
"rider");
+    schemaHandler = createSchemaHandler(readerContext, DATA_SCHEMA, 
requestedSchema, hoodieTableConfig, false);
+    assertEquals(requestedSchema, schemaHandler.getRequiredSchema());
+    assertFalse(readerContext.getNeedsBootstrapMerge());
+  }
+
+  @Test
+  public void testCowBootstrap() {
+    HoodieReaderContext<String> readerContext = createReaderContext(false, 
false, true, false);
+    HoodieTableConfig hoodieTableConfig = mock(HoodieTableConfig.class);
+    Schema requestedSchema = generateProjectionSchema("begin_lat", 
"tip_history", "_hoodie_record_key", "rider");
+    FileGroupReaderSchemaHandler schemaHandler = 
createSchemaHandler(readerContext, DATA_SCHEMA, requestedSchema, 
hoodieTableConfig, false);
+
+    //meta cols must go first in the required schema
+    Schema expectedRequiredSchema = 
generateProjectionSchema("_hoodie_record_key", "begin_lat", "tip_history", 
"rider");
+    assertEquals(expectedRequiredSchema, schemaHandler.getRequiredSchema());
+    Pair<List<Schema.Field>, List<Schema.Field>> bootstrapFields = 
schemaHandler.getBootstrapRequiredFields();
+    assertEquals(Collections.singletonList(getField("_hoodie_record_key")), 
bootstrapFields.getLeft());
+    assertEquals(Arrays.asList(getField("begin_lat"), getField("tip_history"), 
getField("rider")), bootstrapFields.getRight());
+    assertTrue(readerContext.getNeedsBootstrapMerge());
+  }
+
+  @Test
+  public void testCowBootstrapWithPositionMerge() {
+    HoodieReaderContext<String> readerContext = createReaderContext(true, 
false, true, false);
+    HoodieTableConfig hoodieTableConfig = mock(HoodieTableConfig.class);
+    Schema requestedSchema = generateProjectionSchema("begin_lat", 
"tip_history", "_hoodie_record_key", "rider");
+    FileGroupReaderSchemaHandler schemaHandler = 
createSchemaHandler(readerContext, DATA_SCHEMA, requestedSchema, 
hoodieTableConfig, true);
+    //meta cols must go first in the required schema
+    Schema expectedRequiredSchema = 
generateProjectionSchema("_hoodie_record_key", "begin_lat", "tip_history", 
"rider");
+    assertEquals(expectedRequiredSchema, schemaHandler.getRequiredSchema());
+    Pair<List<Schema.Field>, List<Schema.Field>> bootstrapFields = 
schemaHandler.getBootstrapRequiredFields();
+    assertEquals(Arrays.asList(getField("_hoodie_record_key"), 
getPositionalMergeField()), bootstrapFields.getLeft());
+    assertEquals(Arrays.asList(getField("begin_lat"), getField("tip_history"), 
getField("rider"), getPositionalMergeField()), bootstrapFields.getRight());
+    assertTrue(readerContext.getNeedsBootstrapMerge());
+
+    schemaHandler = createSchemaHandler(readerContext, DATA_SCHEMA, 
DATA_COLS_ONLY_SCHEMA, hoodieTableConfig, true);
+    assertEquals(DATA_COLS_ONLY_SCHEMA, schemaHandler.getRequiredSchema());
+    bootstrapFields = schemaHandler.getBootstrapRequiredFields();
+    assertTrue(bootstrapFields.getLeft().isEmpty());
+    assertEquals(Arrays.asList(getField("begin_lat"), getField("tip_history"), 
getField("rider")), bootstrapFields.getRight());
+    assertFalse(readerContext.getNeedsBootstrapMerge());
+
+    schemaHandler = createSchemaHandler(readerContext, DATA_SCHEMA, 
META_COLS_ONLY_SCHEMA, hoodieTableConfig, true);
+    assertEquals(META_COLS_ONLY_SCHEMA, schemaHandler.getRequiredSchema());
+    bootstrapFields = schemaHandler.getBootstrapRequiredFields();
+    assertEquals(Arrays.asList(getField("_hoodie_commit_seqno"), 
getField("_hoodie_record_key")), bootstrapFields.getLeft());
+    assertTrue(bootstrapFields.getRight().isEmpty());
+    assertFalse(readerContext.getNeedsBootstrapMerge());
+  }
+
+  private static Stream<Arguments> testMorParams() {
+    Stream.Builder<Arguments> b = Stream.builder();
+    for (boolean mergeUseRecordPosition : new boolean[] {true, false}) {
+      for (boolean supportsParquetRowIndex : new boolean[] {true, false}) {
+        for (boolean hasBuiltInDelete : new boolean[] {true, false}) {
+          b.add(Arguments.of(EVENT_TIME_ORDERING, true, false, 
mergeUseRecordPosition, supportsParquetRowIndex, hasBuiltInDelete));
+          b.add(Arguments.of(EVENT_TIME_ORDERING, false, false, 
mergeUseRecordPosition, supportsParquetRowIndex, hasBuiltInDelete));
+          b.add(Arguments.of(COMMIT_TIME_ORDERING, false, false, 
mergeUseRecordPosition, supportsParquetRowIndex, hasBuiltInDelete));
+          b.add(Arguments.of(CUSTOM, false, true, mergeUseRecordPosition, 
supportsParquetRowIndex, hasBuiltInDelete));
+          b.add(Arguments.of(CUSTOM, false, false, mergeUseRecordPosition, 
supportsParquetRowIndex, hasBuiltInDelete));
+        }
+      }
+    }
+    return b.build();
+  }
+
+  @ParameterizedTest
+  @MethodSource("testMorParams")
+  public void testMor(RecordMergeMode mergeMode,
+                      boolean hasPrecombine,
+                      boolean isProjectionCompatible,
+                      boolean mergeUseRecordPosition,
+                      boolean supportsParquetRowIndex,
+                      boolean hasBuiltInDelete) {
+
+    Schema dataSchema = hasBuiltInDelete ? DATA_SCHEMA : DATA_SCHEMA_NO_DELETE;
+    HoodieReaderContext<String> readerContext = 
createReaderContext(supportsParquetRowIndex, true, false, 
mergeUseRecordPosition);
+    HoodieTableConfig hoodieTableConfig = mock(HoodieTableConfig.class);
+    when(hoodieTableConfig.populateMetaFields()).thenReturn(Boolean.TRUE);
+    when(hoodieTableConfig.getRecordMergeMode()).thenReturn(mergeMode);
+    if (hasPrecombine) {
+      when(hoodieTableConfig.getPreCombineField()).thenReturn("timestamp");
+    } else {
+      when(hoodieTableConfig.getPreCombineField()).thenReturn(null);
+    }
+    if (mergeMode == CUSTOM) {
+      when(hoodieTableConfig.getRecordMergeStrategyId()).thenReturn("asdf");

Review Comment:
   You'll want to mock the table version for the config as well. Can you also 
extract the common mock setup for this test and `testMorBootstrap` to reusable 
methods?



-- 
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]

Reply via email to