deniskuzZ commented on code in PR #6925:
URL: https://github.com/apache/hive/pull/6925#discussion_r4702268084


##########
iceberg/iceberg-handler/src/main/java/org/apache/iceberg/mr/hive/writer/HiveIcebergDeleteWriter.java:
##########
@@ -20,49 +20,62 @@
 package org.apache.iceberg.mr.hive.writer;
 
 import java.io.IOException;
+import java.nio.ByteBuffer;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 import java.util.function.Supplier;
 import org.apache.hadoop.conf.Configuration;
 import org.apache.hadoop.io.Writable;
 import org.apache.iceberg.DeleteFile;
-import org.apache.iceberg.PartitionKey;
+import org.apache.iceberg.PartitionSpec;
+import org.apache.iceberg.StructLike;
 import org.apache.iceberg.Table;
-import org.apache.iceberg.data.GenericRecord;
 import org.apache.iceberg.data.Record;
-import org.apache.iceberg.deletes.PositionDelete;
 import org.apache.iceberg.io.DeleteWriteResult;
 import org.apache.iceberg.io.OutputFileFactory;
 import org.apache.iceberg.mr.hive.FilesForCommit;
 import org.apache.iceberg.mr.hive.IcebergAcidUtil;
 import org.apache.iceberg.mr.hive.writer.WriterBuilder.Context;
 import org.apache.iceberg.mr.mapred.Container;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.relocated.com.google.common.collect.Maps;
+import org.apache.iceberg.types.Type;
 import org.apache.iceberg.util.DeleteFileSet;
 
 class HiveIcebergDeleteWriter extends HiveIcebergWriterBase {
 
-  private final GenericRecord rowDataTemplate;
-  private final boolean isMergeTask;
+  // deserialized partitions, by spec id and serialized partition
+  private final Map<Integer, Map<ByteBuffer, StructLike>> partitions;
 
   HiveIcebergDeleteWriter(
       Table table, Supplier<Map<String, DeleteFileSet>> rewritableDeletes, 
Configuration conf,
       HiveFileWriterFactory writerFactory, OutputFileFactory deleteFileFactory,
       Context context) {
     super(table, newDeleteWriter(table, rewritableDeletes, conf, 
writerFactory, deleteFileFactory, context));
-
-    this.rowDataTemplate = GenericRecord.create(table.schema());
-    this.isMergeTask = context.isMergeTask();
+    this.partitions = Maps.newHashMapWithExpectedSize(specs.size());
   }
 
   @Override
   public void write(Writable row) throws IOException {
     Record rec = ((Container<Record>) row).get();
-    PositionDelete<Record> positionDelete = 
IcebergAcidUtil.getPositionDelete(rec, rowDataTemplate, isMergeTask);
-    int specId = IcebergAcidUtil.parseSpecId(rec);
-    PartitionKey partitionKey = isMergeTask ? 
IcebergAcidUtil.parsePartitionKey(rec) :
-        partition(rowDataTemplate, specId);
-    writer.write(positionDelete, specs.get(specId), partitionKey);
+    PartitionSpec spec = specs.get(IcebergAcidUtil.parseSpecId(rec));
+    StructLike partition = partitions.computeIfAbsent(spec.specId(), id -> 
newPartitionCache(spec))
+        .computeIfAbsent(IcebergAcidUtil.parseSerializedPartition(rec),
+            serialized -> IcebergAcidUtil.deserializePartition(serialized, 
spec));

Review Comment:
   This doesn't change how memory grows. Until the task commits, the delete 
writer already holds a DeleteFile for every partition it writes, each with its 
own copy of the partition: one per data file for DVs (`BaseDVFileWriter` also 
keeps a copy and a position bitmap per data file in `deletesByPath`), and at 
least one per partition for `ClusteredPositionDeleteWriter` and 
`FanoutPositionOnlyDeleteWriter`. Per distinct partition, the cache adds the 
PARTITION__KEY bytes, a map entry and the decoded record, which is less than 
that DeleteFile. Iceberg's copies are shallow, so they share the decoded 
record's values, such as the strings of identity partitions. Bounding the cache 
would not lower the peak: V3 input is distributed by file but not sorted, so a 
partition decoded again after eviction gives later data files their own copies 
of the values.
   



##########
iceberg/iceberg-handler/src/main/java/org/apache/iceberg/mr/hive/writer/HiveIcebergDeleteWriter.java:
##########
@@ -20,47 +20,62 @@
 package org.apache.iceberg.mr.hive.writer;
 
 import java.io.IOException;
+import java.nio.ByteBuffer;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
+import java.util.function.Supplier;
 import org.apache.hadoop.io.Writable;
 import org.apache.iceberg.DeleteFile;
-import org.apache.iceberg.PartitionKey;
+import org.apache.iceberg.PartitionSpec;
+import org.apache.iceberg.StructLike;
 import org.apache.iceberg.Table;
-import org.apache.iceberg.data.GenericRecord;
 import org.apache.iceberg.data.Record;
-import org.apache.iceberg.deletes.PositionDelete;
 import org.apache.iceberg.io.DeleteWriteResult;
 import org.apache.iceberg.io.OutputFileFactory;
 import org.apache.iceberg.mr.hive.FilesForCommit;
 import org.apache.iceberg.mr.hive.IcebergAcidUtil;
 import org.apache.iceberg.mr.hive.writer.WriterBuilder.Context;
 import org.apache.iceberg.mr.mapred.Container;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.relocated.com.google.common.collect.Maps;
+import org.apache.iceberg.types.Type;
 import org.apache.iceberg.util.DeleteFileSet;
 
 class HiveIcebergDeleteWriter extends HiveIcebergWriterBase {
 
-  private final GenericRecord rowDataTemplate;
-  private final boolean isMergeTask;
+  // the partitions of the deleted records by spec and serialized partition
+  private final Map<Integer, Map<ByteBuffer, StructLike>> partitions;
 
   HiveIcebergDeleteWriter(
-      Table table, Map<String, DeleteFileSet> rewritableDeletes,
+      Table table, Supplier<Map<String, DeleteFileSet>> rewritableDeletes,
       HiveFileWriterFactory writerFactory, OutputFileFactory deleteFileFactory,
       Context context) {
     super(table, newDeleteWriter(table, rewritableDeletes, writerFactory, 
deleteFileFactory, context));
-
-    this.rowDataTemplate = GenericRecord.create(table.schema());
-    this.isMergeTask = context.isMergeTask();
+    this.partitions = Maps.newHashMapWithExpectedSize(specs.size());
   }
 
   @Override
   public void write(Writable row) throws IOException {
     Record rec = ((Container<Record>) row).get();
-    PositionDelete<Record> positionDelete = 
IcebergAcidUtil.getPositionDelete(rec, rowDataTemplate, isMergeTask);
-    int specId = IcebergAcidUtil.parseSpecId(rec);
-    PartitionKey partitionKey = isMergeTask ? 
IcebergAcidUtil.parsePartitionKey(rec) :
-        partition(rowDataTemplate, specId);
-    writer.write(positionDelete, specs.get(specId), partitionKey);
+    PartitionSpec spec = specs.get(IcebergAcidUtil.parseSpecId(rec));
+    StructLike partition = partitions.computeIfAbsent(spec.specId(), id -> 
newPartitionCache(spec))
+        .computeIfAbsent(IcebergAcidUtil.parseSerializedPartition(rec),
+            serialized -> IcebergAcidUtil.deserializePartition(serialized, 
spec));

Review Comment:
   The key can't change while it's in the map. On the DELETE path, the serde 
copies the partition bytes into a new array for every row 
(`WritableBinaryObjectInspector` → `LazyUtils.createByteArray` → 
`Arrays.copyOf`), and `IcebergBinaryObjectInspector.convert` wraps that array. 
In the merge task, the key wraps the array that 
`MergeTaskVirtualColumnAwareIterator` serializes once per file and never writes 
again. Its only reader, `IcebergAcidUtil.deserializePartition`, reads it 
through `ByteBuffers.toByteArray`, which never moves the buffer's position or 
limit. So hashCode and equals stay the same while the key is in the map, and a 
defensive copy would only add an allocation per row.
   



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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to