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


##########
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.
+   */
+  @Override
+  public Row next() {
+    // return the cached row if there is one
+    if (postImage != null) {
+      Row currentRow = postImage;
+      postImage = null;
+      return currentRow;
+    }
+
+    // return the first row if there is no more rows
+    if (!getRowIterator().hasNext() && firstRow != null) {
+      Row currentRow = firstRow;
+      firstRow = null;
+      return currentRow;
+    }
+
+    if (firstRow == null) {
+      firstRow = getRowIterator().next();
+      return null;
+    }
+
+    Row lastRow = null;
+
+    while (getRowIterator().hasNext()) {
+      Row currentRow = getRowIterator().next();
+      // if current row is not the same logical as the first row
+      if (sameLogicalRow(firstRow, currentRow)) {
+        lastRow = currentRow;
+      } else {
+        // if current row is not the same logical row as the first row, 
compute the results and
+        // set the current row as the first row, and return the result
+        Row result = getNetChanges(firstRow, lastRow);
+        firstRow = currentRow;
+        return result;
+      }
+    }
+
+    Row current = getNetChanges(firstRow, lastRow);
+    firstRow = null;
+    return current;
+  }
+
+  /**
+   * This method gets the net changes between the first version and the last 
version of a logical
+   * row. Here are the algorithm:
+   *
+   * <ol>
+   *   <li>If the first version is an insert, and last one is an insert, then 
return an insert with
+   *       the last version.
+   *   <li>If the first version is an insert, and last one is a delete, then 
return null.
+   *   <li>If the first version is a delete, and last one is a delete, then 
return a delete.
+   *   <li>If the first version is a delete, and last one is an insert, if 
they are the exactly
+   *       same, they are carry-over rows, return null, otherwise return an 
update pair.
+   * </ol>
+   */
+  private Row getNetChanges(Row first, Row last) {
+    if (first == last || last == null) {
+      return first;
+    }
+
+    if (first.getString(getChangeTypeIndex()).equals(INSERT)) {
+      if (last.getString(getChangeTypeIndex()).equals(INSERT)) {
+        return last;
+      }
+
+      if (last.getString(getChangeTypeIndex()).equals(DELETE)) {
+        return null;
+      }
+    }
+
+    if (first.getString(getChangeTypeIndex()).equals(DELETE)) {
+      if (last.getString(getChangeTypeIndex()).equals(DELETE)) {
+        return first;
+      }
+
+      if (last.getString(getChangeTypeIndex()).equals(INSERT)) {
+        if (isCarryoverRecord(first, last)) {
+          return null;
+        } else {
+          // update
+          Row updatedFirstRow = modify(first, getChangeTypeIndex(), 
UPDATE_BEFORE);
+          postImage = modify(last, getChangeTypeIndex(), UPDATE_AFTER);
+          return updatedFirstRow;
+        }
+      }
+    }
+
+    return null;

Review Comment:
   What condition is this?



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