You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hudi.apache.org by GitBox <gi...@apache.org> on 2022/10/21 19:48:30 UTC

[GitHub] [hudi] alexeykudinkin commented on a diff in pull request #6986: [HUDI-5047] Add partition value in HoodieLogRecordReader when hoodie.datasource.write.drop.partition.columns=true

alexeykudinkin commented on code in PR #6986:
URL: https://github.com/apache/hudi/pull/6986#discussion_r1002132914


##########
hudi-common/src/main/java/org/apache/hudi/common/table/log/AbstractHoodieLogRecordReader.java:
##########
@@ -195,6 +203,24 @@ protected AbstractHoodieLogRecordReader(FileSystem fs, String basePath, List<Str
           Pair.of(tableConfig.getRecordKeyFieldProp(), tableConfig.getPartitionFieldProp()));
     }
     this.partitionName = partitionName;
+    this.partitionValues  = getPartitionValues();

Review Comment:
   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.



##########
hudi-common/src/test/java/org/apache/hudi/common/functional/TestHoodieLogRecordReader.java:
##########
@@ -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());

Review Comment:
   I don't think we really need HDFS for this test. What's the idea behind doing it on HDFS?



##########
hudi-common/src/main/java/org/apache/hudi/common/table/log/AbstractHoodieLogRecordReader.java:
##########
@@ -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)

Review Comment:
   Why do we filter on specific length here?



##########
hudi-common/src/main/java/org/apache/hudi/common/table/log/AbstractHoodieLogRecordReader.java:
##########
@@ -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) {

Review Comment:
   This condition should be asserted on during init



##########
hudi-common/src/main/java/org/apache/hudi/common/table/log/AbstractHoodieLogRecordReader.java:
##########
@@ -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) {
+            for (int i = 0; i < partitionValues.length; i++) {
+              record.put(schema.getField(partitionFields[i]).pos(), partitionValues[i]);

Review Comment:
   We can't just blindly inject strings into record, we need to make sure we coercing it to a type of the column



-- 
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: commits-unsubscribe@hudi.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org