-
Notifications
You must be signed in to change notification settings - Fork 2.5k
[HUDI-5047] Add partition value in HoodieLogRecordReader when hoodie.datasource.write.drop.partition.columns=true #6986
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 7 commits
8b09437
6c3f427
970ad79
df2c48f
088cfee
22f7b31
ab5cfec
29919d7
74787e6
240b1a4
1d34a26
350592c
599f8a2
cd21d98
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -151,6 +151,11 @@ public abstract class AbstractHoodieLogRecordReader { | |
| // Use scanV2 method. | ||
| private boolean useScanV2; | ||
|
|
||
| private Option<String[]> partitionFields; | ||
| private Option<String[]> partitionValues; | ||
| private boolean dropPartitions = false; | ||
| private boolean hiveStylePartition = false; | ||
|
|
||
| protected AbstractHoodieLogRecordReader(FileSystem fs, String basePath, List<String> logFilePaths, | ||
| Schema readerSchema, | ||
| String latestInstantTime, boolean readBlocksLazily, boolean reverseReader, | ||
|
|
@@ -172,6 +177,9 @@ protected AbstractHoodieLogRecordReader(FileSystem fs, String basePath, List<Str | |
| HoodieTableConfig tableConfig = this.hoodieTableMetaClient.getTableConfig(); | ||
| this.payloadClassFQN = tableConfig.getPayloadClass(); | ||
| this.preCombineField = tableConfig.getPreCombineField(); | ||
| this.dropPartitions = tableConfig.getBooleanOrDefault(HoodieTableConfig.DROP_PARTITION_COLUMNS); | ||
| this.partitionFields = tableConfig.getPartitionFields(); | ||
| this.hiveStylePartition = tableConfig.getBooleanOrDefault(HoodieTableConfig.HIVE_STYLE_PARTITIONING_ENABLE); | ||
| if (this.preCombineField != null) { | ||
| this.payloadProps.setProperty(HoodiePayloadProps.PAYLOAD_ORDERING_FIELD_PROP_KEY, this.preCombineField); | ||
| } | ||
|
|
@@ -195,6 +203,24 @@ protected AbstractHoodieLogRecordReader(FileSystem fs, String basePath, List<Str | |
| Pair.of(tableConfig.getRecordKeyFieldProp(), tableConfig.getPartitionFieldProp())); | ||
| } | ||
| this.partitionName = partitionName; | ||
| this.partitionValues = getPartitionValues(); | ||
| } | ||
|
|
||
| private Option<String[]> getPartitionValues() { | ||
| if (!this.partitionName.isPresent()) { | ||
| return Option.empty(); | ||
| } | ||
|
|
||
| String[] partitionValues = this.partitionName.get().split("/"); | ||
| if (this.hiveStylePartition) { | ||
| return Option.of(Arrays.stream(partitionValues) | ||
| .map(partition -> partition.split("=")) | ||
| .filter(partition -> partition.length == 2) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we filter on specific length here? |
||
| .map(partition -> partition[1]) | ||
| .toArray(String[]::new)); | ||
| } else { | ||
| return Option.of(partitionValues); | ||
| } | ||
| } | ||
|
|
||
| protected String getKeyField() { | ||
|
|
@@ -632,6 +658,19 @@ private void processDataBlock(HoodieDataBlock dataBlock, Option<KeySpec> keySpec | |
| while (recordIterator.hasNext()) { | ||
| IndexedRecord currentRecord = recordIterator.next(); | ||
| IndexedRecord record = schemaOption.isPresent() ? HoodieAvroUtils.rewriteRecordWithNewSchema(currentRecord, schemaOption.get(), Collections.emptyMap()) : currentRecord; | ||
|
|
||
| Schema schema = record.getSchema(); | ||
| if (this.dropPartitions && this.partitionFields.isPresent() && this.partitionValues.isPresent()) { | ||
| String[] partitionFields = this.partitionFields.get(); | ||
| String[] partitionValues = this.partitionValues.get(); | ||
|
|
||
| if (partitionFields.length == partitionValues.length) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This condition should be asserted on during init |
||
| for (int i = 0; i < partitionValues.length; i++) { | ||
| record.put(schema.getField(partitionFields[i]).pos(), partitionValues[i]); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can't just blindly inject strings into record, we need to make sure we coercing it to a type of the column |
||
| } | ||
| } | ||
| } | ||
|
|
||
| processNextRecord(createHoodieRecord(record, this.hoodieTableMetaClient.getTableConfig(), this.payloadClassFQN, | ||
| this.preCombineField, this.withOperationField, this.simpleKeyGenFields, this.partitionName)); | ||
| totalLogRecords.incrementAndGet(); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,197 @@ | ||
| /* | ||
| * 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.functional; | ||
|
|
||
| import org.apache.hudi.avro.HoodieAvroUtils; | ||
| import org.apache.hudi.common.fs.FSUtils; | ||
| import org.apache.hudi.common.model.HoodieAvroPayload; | ||
| import org.apache.hudi.common.model.HoodieLogFile; | ||
| import org.apache.hudi.common.model.HoodieRecord; | ||
| import org.apache.hudi.common.model.HoodieTableType; | ||
| import org.apache.hudi.common.table.HoodieTableConfig; | ||
| import org.apache.hudi.common.table.log.HoodieLogFormat; | ||
| import org.apache.hudi.common.table.log.HoodieMergedLogRecordScanner; | ||
| import org.apache.hudi.common.table.log.block.HoodieAvroDataBlock; | ||
| import org.apache.hudi.common.table.log.block.HoodieDataBlock; | ||
| import org.apache.hudi.common.table.log.block.HoodieLogBlock; | ||
| import org.apache.hudi.common.testutils.FileCreateUtils; | ||
| import org.apache.hudi.common.testutils.HoodieTestUtils; | ||
| import org.apache.hudi.common.testutils.SchemaTestUtil; | ||
|
|
||
| import org.apache.avro.Schema; | ||
| import org.apache.avro.generic.GenericRecord; | ||
| import org.apache.avro.generic.IndexedRecord; | ||
| import org.apache.hadoop.conf.Configuration; | ||
| import org.apache.hadoop.fs.FileSystem; | ||
| import org.apache.hadoop.fs.FileUtil; | ||
| import org.apache.hadoop.fs.Path; | ||
| import org.apache.hadoop.hdfs.DFSConfigKeys; | ||
| import org.apache.hadoop.hdfs.MiniDFSCluster; | ||
| import org.junit.jupiter.api.AfterAll; | ||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.BeforeAll; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.io.File; | ||
| import java.io.IOException; | ||
| import java.net.URISyntaxException; | ||
| import java.util.ArrayList; | ||
| import java.util.HashMap; | ||
| import java.util.HashSet; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Properties; | ||
| import java.util.Set; | ||
| import java.util.UUID; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| import static org.apache.hudi.common.testutils.SchemaTestUtil.getSimpleSchema; | ||
| import static org.apache.hudi.common.util.collection.ExternalSpillableMap.DiskMapType.BITCASK; | ||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
|
||
| public class TestHoodieLogRecordReader { | ||
|
|
||
| private static File dfsBaseDir; | ||
| private static MiniDFSCluster cluster; | ||
|
|
||
| private FileSystem fs; | ||
| private String basePath; | ||
| private Path partitionPath; | ||
| private String partitionValue = "my_partition"; | ||
| private String partitionName = "partition"; | ||
|
|
||
| @BeforeAll | ||
| public static void setUpClass() throws IOException, InterruptedException { | ||
| dfsBaseDir = new File("/tmp/" + UUID.randomUUID().toString()); | ||
| FileUtil.fullyDelete(dfsBaseDir); | ||
| // Append is not supported in LocalFileSystem. HDFS needs to be setup. | ||
| Configuration conf = new Configuration(); | ||
| // lower heartbeat interval for fast recognition of DN | ||
| conf.set(MiniDFSCluster.HDFS_MINIDFS_BASEDIR, dfsBaseDir.getAbsolutePath()); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we really need HDFS for this test. What's the idea behind doing it on HDFS? |
||
| conf.setInt(DFSConfigKeys.DFS_NAMENODE_HEARTBEAT_RECHECK_INTERVAL_KEY, 1000); | ||
| conf.setInt(DFSConfigKeys.DFS_HEARTBEAT_INTERVAL_KEY, 1); | ||
| conf.setInt(DFSConfigKeys.DFS_CLIENT_SOCKET_TIMEOUT_KEY, 3000); | ||
| cluster = new MiniDFSCluster.Builder(conf).checkExitOnShutdown(true).numDataNodes(4).build(); | ||
| } | ||
|
|
||
| @AfterAll | ||
| public static void tearDownClass() { | ||
| cluster.shutdown(true); | ||
| FileUtil.fullyDelete(dfsBaseDir); | ||
| } | ||
|
|
||
| @BeforeEach | ||
| public void setUp() throws IOException, InterruptedException { | ||
| this.fs = cluster.getFileSystem(); | ||
|
|
||
| this.partitionPath = new Path(dfsBaseDir.toString(), partitionValue); | ||
| this.basePath = dfsBaseDir.toString(); | ||
| } | ||
|
|
||
| @AfterEach | ||
| public void tearDown() throws IOException { | ||
| fs.delete(partitionPath, true); | ||
| } | ||
|
|
||
| @Test | ||
| public void testLogRecordReaderExtractPartitionFromPath() throws IOException, URISyntaxException, InterruptedException { | ||
|
|
||
| Properties properties = new Properties(); | ||
| properties.setProperty(HoodieTableConfig.DROP_PARTITION_COLUMNS.key(), "true"); | ||
| properties.setProperty(HoodieTableConfig.PARTITION_FIELDS.key(), partitionName); | ||
| HoodieTestUtils.init(fs.getConf(), basePath, HoodieTableType.MERGE_ON_READ, properties); | ||
|
|
||
| Schema schema = HoodieAvroUtils.addMetadataFields(getSimpleSchema()); | ||
| HoodieLogFormat.Writer writer = | ||
| HoodieLogFormat.newWriterBuilder().onParentPath(partitionPath).withFileExtension(HoodieLogFile.DELTA_EXTENSION) | ||
| .withFileId("test-fileid1").overBaseCommit("100").withFs(fs).withSizeThreshold(500).build(); | ||
| // Write 1 | ||
| List<IndexedRecord> records1 = SchemaTestUtil.generateHoodieTestRecords(0, 100); | ||
| List<IndexedRecord> copyOfRecords1 = records1.stream() | ||
| .map(record -> HoodieAvroUtils.rewriteRecord((GenericRecord) record, schema)).collect(Collectors.toList()); | ||
|
|
||
| Map<HoodieLogBlock.HeaderMetadataType, String> header = new HashMap<>(); | ||
| header.put(HoodieLogBlock.HeaderMetadataType.INSTANT_TIME, "100"); | ||
| header.put(HoodieLogBlock.HeaderMetadataType.SCHEMA, schema.toString()); | ||
| HoodieDataBlock dataBlock = new HoodieAvroDataBlock(records1, header, HoodieRecord.RECORD_KEY_METADATA_FIELD); | ||
| writer.appendBlock(dataBlock); | ||
|
|
||
| // Write 2 | ||
| List<IndexedRecord> records2 = SchemaTestUtil.generateHoodieTestRecords(0, 100); | ||
| List<IndexedRecord> copyOfRecords2 = records2.stream() | ||
| .map(record -> HoodieAvroUtils.rewriteRecord((GenericRecord) record, schema)).collect(Collectors.toList()); | ||
| header.put(HoodieLogBlock.HeaderMetadataType.SCHEMA, schema.toString()); | ||
| dataBlock = new HoodieAvroDataBlock(records2, header, HoodieRecord.RECORD_KEY_METADATA_FIELD); | ||
| writer.appendBlock(dataBlock); | ||
| writer.close(); | ||
|
|
||
| List<String> allLogFiles = | ||
| FSUtils.getAllLogFiles(fs, partitionPath, "test-fileid1", HoodieLogFile.DELTA_EXTENSION, "100") | ||
| .map(s -> s.getPath().toString()).collect(Collectors.toList()); | ||
|
|
||
| FileCreateUtils.createDeltaCommit(basePath, "100", fs); | ||
|
|
||
| // generate reader schema | ||
| List<Schema.Field> fields = new ArrayList<>(); | ||
| fields.add(new Schema.Field(partitionName, Schema.create(Schema.Type.STRING), null, "")); | ||
| for (Schema.Field field : schema.getFields()) { | ||
| Schema.Field newField = new Schema.Field(field.name(), field.schema(), field.doc(), field.defaultVal()); | ||
| for (Map.Entry<String, Object> prop : field.getObjectProps().entrySet()) { | ||
| newField.addProp(prop.getKey(), prop.getValue()); | ||
| } | ||
| fields.add(newField); | ||
| } | ||
| Schema readerSchema = Schema.createRecord(schema.getName(), schema.getDoc(), schema.getNamespace(), false); | ||
| readerSchema.setFields(fields); | ||
|
|
||
| HoodieMergedLogRecordScanner scanner = HoodieMergedLogRecordScanner.newBuilder() | ||
| .withFileSystem(fs) | ||
| .withBasePath(basePath) | ||
| .withLogFilePaths(allLogFiles) | ||
| .withReaderSchema(readerSchema) | ||
| .withLatestInstantTime("102") | ||
| .withMaxMemorySizeInBytes(10240L) | ||
| .withReverseReader(false) | ||
| .withBufferSize(4096) | ||
| .withDiskMapType(BITCASK) | ||
| .withPartition(partitionValue) | ||
| .build(); | ||
| assertEquals(200, scanner.getTotalLogRecords()); | ||
| Set<String> readKeys = new HashSet<>(200); | ||
| scanner.forEach(s -> { | ||
| readKeys.add(s.getKey().getRecordKey()); | ||
| try { | ||
| IndexedRecord record = ((HoodieAvroPayload)s.getData()).getInsertValue(readerSchema, new Properties()).get(); | ||
| String partition = record.get(record.getSchema().getField(partitionName).pos()).toString(); | ||
| // The value of field "partition" should be "my_partition" passed to scanner | ||
| assertEquals(partitionValue, partition); | ||
| } catch (IOException e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
| }); | ||
|
|
||
| assertEquals(200, readKeys.size(), "Stream collect should return all 200 records"); | ||
| copyOfRecords1.addAll(copyOfRecords2); | ||
| Set<String> originalKeys = | ||
| copyOfRecords1.stream().map(s -> ((GenericRecord) s).get(HoodieRecord.RECORD_KEY_METADATA_FIELD).toString()) | ||
| .collect(Collectors.toSet()); | ||
| assertEquals(originalKeys, readKeys, "CompositeAvroLogReader should return 200 records from 2 versions"); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not a good practice to call non-static methods from the ctor, since it relies implicitly on the ordering of the initialization. Let's instead make this method static and pass it all values it needs.