aokolnychyi commented on code in PR #7326:
URL: https://github.com/apache/iceberg/pull/7326#discussion_r1166143503


##########
spark/v3.3/spark/src/main/java/org/apache/iceberg/spark/NetChangelogIterator.java:
##########
@@ -0,0 +1,138 @@
+/*
+ * 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.spark;
+
+import java.util.Iterator;
+import org.apache.spark.sql.Row;
+import org.apache.spark.sql.types.StructType;
+
+public class NetChangelogIterator extends ChangelogIterator {
+
+  private Row firstRow = null;
+  private Row postImage = null;
+
+  protected NetChangelogIterator(
+      Iterator<Row> rowIterator, StructType rowType, String[] 
identifierFields) {
+    super(rowIterator, rowType, identifierFields);
+  }
+
+  @Override
+  public boolean hasNext() {
+    if (postImage != null || firstRow != null) {
+      return true;
+    }
+    return getRowIterator().hasNext();
+  }
+
+  /**
+   * This method iterates all versions of the same logical row across multiple 
snapshots. It caches
+   * the row's first version, and removes every version in between, then 
compares the last version
+   * with the first version to get the net change. Please note that it also 
removes any invalid
+   * data. For example, if there are two identical records, it will remove one 
of them.

Review Comment:
   I am not sure this behavior is correct. We know our identity columns are not 
enforced and MERGE does not require strict uniqueness. For instance, if we have 
multiple rows with ID = 1, then all of them will be updated in a row-level 
operation. 
   
   I like the idea of consuming all changes for the same logical row and then 
acting on them based on what we got. Is there a way for us to adjust this logic 
so that we can correctly handle these use cases:
   
   ```
   s1, DELETE
   s1, DELETE
   s1, INSERT
   s1, INSERT
   ```
   
   Should produce UPDATE_BEFORE, UPDATE_AFTER, UPDATE_BEFORE, UPDATE_AFTER.
   
   ```
   s1, INSERT
   s2, INSERT
   ```
   
   Should still remain as two INSERTs when computing net changes.
   
   ```
   s1, DELETE
   s1, DELETE
   ```
   
   Should still remain as two DELETEs when computing net changes.
   
   



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