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/09/08 17:18:11 UTC

[GitHub] [hudi] minihippo commented on a diff in pull request #4676: [HUDI-3304] support partial update on mor table

minihippo commented on code in PR #4676:
URL: https://github.com/apache/hudi/pull/4676#discussion_r966225565


##########
hudi-common/src/main/java/org/apache/hudi/common/model/PartialUpdateAvroPayload.java:
##########
@@ -0,0 +1,196 @@
+/*
+ * 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.model;
+
+import org.apache.avro.Schema;
+import org.apache.avro.generic.GenericRecord;
+import org.apache.avro.generic.IndexedRecord;
+
+import org.apache.hudi.avro.HoodieAvroUtils;
+import org.apache.hudi.common.util.Option;
+import org.apache.hudi.exception.HoodieException;
+
+import java.io.IOException;
+import java.util.List;
+import java.util.Properties;
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * subclass of OverwriteNonDefaultsWithLatestAvroPayload used for delta streamer.
+ *
+ * <ol>
+ * <li>preCombine - Picks the latest delta record for a key, based on an ordering field;
+ * <li>combineAndGetUpdateValue/getInsertValue - overwrite storage for specified fields
+ * that doesn't equal defaultValue.
+ * </ol>
+ */
+public class PartialUpdateAvroPayload extends OverwriteNonDefaultsWithLatestAvroPayload {
+
+  public static ConcurrentHashMap<String, Schema> schemaRepo = new ConcurrentHashMap<>();
+
+  public PartialUpdateAvroPayload(GenericRecord record, Comparable orderingVal) {
+    super(record, orderingVal);
+  }
+
+  public PartialUpdateAvroPayload(Option<GenericRecord> record) {
+    super(record); // natural order
+  }
+
+  @Override
+  public PartialUpdateAvroPayload preCombine(OverwriteWithLatestAvroPayload oldValue, Properties properties) {
+    String schemaStringIn = properties.getProperty("schema");
+    Schema schemaInstance;
+    if (!schemaRepo.containsKey(schemaStringIn)) {
+      schemaInstance = new Schema.Parser().parse(schemaStringIn);
+      schemaRepo.put(schemaStringIn, schemaInstance);
+    } else {
+      schemaInstance = schemaRepo.get(schemaStringIn);
+    }
+    if (oldValue.recordBytes.length == 0) {
+      // use natural order for delete record
+      return this;
+    }
+
+    try {
+      GenericRecord indexedOldValue = (GenericRecord) oldValue.getInsertValue(schemaInstance).get();
+      Option<IndexedRecord> optValue = combineAndGetUpdateValue(indexedOldValue, schemaInstance, this.orderingVal.toString());
+      // Rebuild ordering value if required
+      String newOrderingVal = rebuildOrderingValMap((GenericRecord) optValue.get(), this.orderingVal.toString());
+      if (optValue.isPresent()) {
+        return new PartialUpdateAvroPayload((GenericRecord) optValue.get(), newOrderingVal);
+      }
+    } catch (Exception ex) {
+      return this;
+    }
+    return this;
+  }
+
+  public Option<IndexedRecord> combineAndGetUpdateValue(
+      IndexedRecord currentValue, Schema schema, String newOrderingValWithMappings) throws IOException {
+    Option<IndexedRecord> recordOption = getInsertValue(schema);
+    if (!recordOption.isPresent()) {
+      return Option.empty();
+    }
+
+    // Perform a deserialization again to prevent resultRecord from sharing the same reference as recordOption
+    GenericRecord resultRecord = (GenericRecord) getInsertValue(schema).get();
+    List<Schema.Field> fields = schema.getFields();
+
+    // newOrderingValWithMappings = _ts1:name1,price1=999;_ts2:name2,price2=;
+    for (String newOrderingFieldMapping : newOrderingValWithMappings.split(";")) {
+      String orderingField = newOrderingFieldMapping.split(":")[0];
+      String newOrderingVal = getNewOrderingVal(orderingField, newOrderingFieldMapping);
+      String oldOrderingVal = HoodieAvroUtils.getNestedFieldValAsString(
+          (GenericRecord) currentValue, orderingField, true, false);
+      if (oldOrderingVal == null) {
+        oldOrderingVal = "";
+      }
+
+      // No update required
+      if (oldOrderingVal.isEmpty() && newOrderingVal.isEmpty()) {
+        continue;
+      }
+
+      // Pick the payload with greatest ordering value as insert record
+      boolean isBaseRecord = false;
+      try {
+        if (Long.parseLong(oldOrderingVal) > Long.parseLong(newOrderingVal)) {
+          isBaseRecord = true;
+        }
+      } catch (NumberFormatException e) {
+        if (oldOrderingVal.compareTo(newOrderingVal) > 0) {
+          isBaseRecord = true;
+        }
+      }
+
+      // Initialise the fields of the sub-tables
+      GenericRecord insertRecord;
+      if (isBaseRecord) {
+        insertRecord = (GenericRecord) currentValue;
+        // resultRecord is already assigned as recordOption
+      } else {
+        insertRecord = (GenericRecord) recordOption.get();
+        GenericRecord currentRecord = (GenericRecord) currentValue;
+        fields.stream()
+            .filter(f -> newOrderingFieldMapping.contains(f.name()))
+            .forEach(f -> resultRecord.put(f.name(), currentRecord.get(f.name())));

Review Comment:
   There is a problem. For example, table with schema (key, col1, tsA, col2, tsB)
   partial write client A with schema: key, col1, tsA
   partial write client B with schema: key, col2, tsB
   current value belong to A with old value belong to B, so the current value should be updated. But here only update the tsA and tsB without update the col1 which doesn't belong to precombine field.
   



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