rdblue commented on a change in pull request #1663:
URL: https://github.com/apache/iceberg/pull/1663#discussion_r523316253



##########
File path: core/src/main/java/org/apache/iceberg/io/BaseDeltaWriter.java
##########
@@ -0,0 +1,248 @@
+/*
+ * 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.iceberg.io;
+
+import java.io.IOException;
+import java.util.List;
+import java.util.function.Function;
+import org.apache.iceberg.DataFile;
+import org.apache.iceberg.DeleteFile;
+import org.apache.iceberg.Schema;
+import org.apache.iceberg.StructLike;
+import org.apache.iceberg.deletes.PositionDelete;
+import org.apache.iceberg.relocated.com.google.common.base.MoreObjects;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.relocated.com.google.common.collect.Lists;
+import org.apache.iceberg.relocated.com.google.common.collect.Sets;
+import org.apache.iceberg.types.TypeUtil;
+import org.apache.iceberg.util.StructLikeMap;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class BaseDeltaWriter<T> implements DeltaWriter<T> {
+  private static final Logger LOG = 
LoggerFactory.getLogger(BaseDeltaWriter.class);
+
+  private final RollingContentFileWriter<DataFile, T> dataWriter;
+  private final RollingContentFileWriter<DeleteFile, T> equalityDeleteWriter;
+  private final RollingContentFileWriter<DeleteFile, PositionDelete<T>> 
posDeleteWriter;
+
+  private final PositionDelete<T> positionDelete = new PositionDelete<>();
+  private final StructLikeMap<List<FilePos>> insertedRowMap;
+
+  // Function to convert the generic data to a StructLike.
+  private final Function<T, StructLike> structLikeFun;
+
+  public BaseDeltaWriter(RollingContentFileWriter<DataFile, T> dataWriter) {
+    this(dataWriter, null);
+  }
+
+  public BaseDeltaWriter(RollingContentFileWriter<DataFile, T> dataWriter,
+                         RollingContentFileWriter<DeleteFile, 
PositionDelete<T>> posDeleteWriter) {
+    this(dataWriter, posDeleteWriter, null, null, null, null);
+  }
+
+  public BaseDeltaWriter(RollingContentFileWriter<DataFile, T> dataWriter,
+                         RollingContentFileWriter<DeleteFile, 
PositionDelete<T>> posDeleteWriter,
+                         RollingContentFileWriter<DeleteFile, T> 
equalityDeleteWriter,
+                         Schema tableSchema,
+                         List<Integer> equalityFieldIds,
+                         Function<T, StructLike> structLikeFun) {
+
+    Preconditions.checkNotNull(dataWriter, "Data writer should always not be 
null.");
+
+    if (posDeleteWriter == null) {
+      // Only accept INSERT records.
+      Preconditions.checkArgument(equalityDeleteWriter == null);
+    }
+
+    if (posDeleteWriter != null && equalityDeleteWriter == null) {
+      // Only accept INSERT records and position deletion.
+      Preconditions.checkArgument(tableSchema == null);
+      Preconditions.checkArgument(equalityFieldIds == null);
+    }
+
+    if (equalityDeleteWriter != null) {
+      // Accept insert records, position deletion, equality deletions.
+      Preconditions.checkNotNull(posDeleteWriter,
+          "Position delete writer shouldn't be null when writing equality 
deletions.");
+      Preconditions.checkNotNull(tableSchema, "Iceberg table schema shouldn't 
be null");
+      Preconditions.checkNotNull(equalityFieldIds, "Equality field ids 
shouldn't be null");
+      Preconditions.checkNotNull(structLikeFun, "StructLike function shouldn't 
be null");
+
+      Schema deleteSchema = TypeUtil.select(tableSchema, 
Sets.newHashSet(equalityFieldIds));
+      this.insertedRowMap = StructLikeMap.create(deleteSchema.asStruct());
+      this.structLikeFun = structLikeFun;
+    } else {
+      this.insertedRowMap = null;
+      this.structLikeFun = null;
+    }
+
+    this.dataWriter = dataWriter;
+    this.equalityDeleteWriter = equalityDeleteWriter;
+    this.posDeleteWriter = posDeleteWriter;
+  }
+
+  @Override
+  public void writeRow(T row) throws IOException {
+    if (enableEqualityDelete()) {
+      FilePos filePos = FilePos.create(dataWriter.currentPath(), 
dataWriter.currentPos());
+      insertedRowMap.compute(structLikeFun.apply(row), (k, v) -> {
+        if (v == null) {
+          return Lists.newArrayList(filePos);
+        } else {
+          v.add(filePos);
+          return v;
+        }
+      });
+    }
+
+    dataWriter.write(row);
+  }
+
+  @Override
+  public void writeEqualityDelete(T equalityDelete) throws IOException {
+    if (!enableEqualityDelete()) {
+      throw new UnsupportedOperationException("Could not accept equality 
deletion.");
+    }
+
+    List<FilePos> existing = 
insertedRowMap.get(structLikeFun.apply(equalityDelete));
+
+    if (existing == null) {
+      // Delete the row which have been written by other completed delta 
writer.
+      equalityDeleteWriter.write(equalityDelete);
+    } else {
+      // Delete the rows which was written in current delta writer.
+      for (FilePos filePos : existing) {
+        posDeleteWriter.write(positionDelete.set(filePos.path(), 
filePos.pos(), null));

Review comment:
       I think this strategy will violate the ordering requirements of position 
delete files.
   
   Position delete files need to be ordered by file and position. Imagine 3 
replacements for the same row key. The first creates insert 0, the second 
insert 1, and the third insert 2. The first adds 0 existing, the second adds 1, 
and the third adds 2. And the first time through this loop deletes position 0, 
the second deletes positions 0 and 1, and the third deletes positions 0, 1, and 
2. The result is deleting positions 0, 0, 1, 0, 1, 2, which violates the order 
requirement.
   
   For a given inserted position, we need to track that it is deleted and write 
all of the deletes at one time so that they are all in order.
   
   I think this should change from using `StructLikeMap<List<FilePos>>` to 
`StructLikeMap<FilePos>`. Each time the row is deleted, get the current 
`FilePos` and add it to a `deletePositions` list. Then set the new row's 
`FilePos` in the map so that it is the one deleted the next time.




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

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