mxm commented on code in PR #15996:
URL: https://github.com/apache/iceberg/pull/15996#discussion_r3155898006


##########
flink/v2.1/flink/src/main/java/org/apache/iceberg/flink/maintenance/operator/EqualityConvertWorker.java:
##########
@@ -0,0 +1,160 @@
+/*
+ * 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.flink.maintenance.operator;
+
+import java.util.Objects;
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.api.common.functions.OpenContext;
+import org.apache.flink.api.common.state.ListState;
+import org.apache.flink.api.common.state.ListStateDescriptor;
+import org.apache.flink.api.common.state.ValueState;
+import org.apache.flink.api.common.state.ValueStateDescriptor;
+import org.apache.flink.api.common.typeinfo.TypeInformation;
+import org.apache.flink.api.common.typeinfo.Types;
+import org.apache.flink.metrics.Counter;
+import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
+import org.apache.flink.util.Collector;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Parallel worker that maintains a shard of the primary key index. Keyed by 
the full serialized
+ * primary key ({@link SerializedEqualityValues}), each instance handles a 
subset of PK values.
+ *
+ * <p>ADD_DATA_ROW commands with ts &le; resolveTimestamp go directly into the 
resolution index;
+ * commands with ts &gt; resolveTimestamp are staged as pending rows for the 
next cycle. This
+ * handles the race where parallel Readers deliver a later-phase record before 
an earlier-phase
+ * RESOLVE_DELETE fires.
+ *
+ * <p>Detects main branch changes via {@link IndexCommand#mainSnapshotId()} 
and clears stale index
+ * entries when the main snapshot changes.
+ */
+@Internal
+public class EqualityConvertWorker
+    extends KeyedProcessFunction<SerializedEqualityValues, IndexCommand, 
DVPosition> {
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(EqualityConvertWorker.class);
+
+  private static final String RESOLVED_DELETE_NUM_METRIC = "resolvedDeleteNum";
+  private static final String INDEXED_KEY_NUM_METRIC = "indexedKeyNum";
+
+  private transient ValueState<Long> mainSnapshotVersion;
+  private transient ListState<DVPosition> dataRowPositions;
+  private transient ListState<DVPosition> pendingRows;

Review Comment:
   This is because after resolving the equality deletes, we update the index 
with the added data files from the staging snapshot. We need to separate the 
two to ensure that during resolving the equality deletes, the new data isn't 
visible yet.



##########
flink/v2.1/flink/src/main/java/org/apache/iceberg/flink/maintenance/operator/ReadCommand.java:
##########
@@ -0,0 +1,99 @@
+/*
+ * 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.flink.maintenance.operator;
+
+import java.io.Serializable;
+import java.util.Collections;
+import java.util.List;
+import org.apache.flink.annotation.Internal;
+import org.apache.iceberg.FileFormat;
+import org.apache.iceberg.relocated.com.google.common.collect.Lists;
+
+/**
+ * Command from the {@link EqualityConvertPlanner} to the {@link 
EqualityConvertReader}. Describes a
+ * single file to read and index.
+ *
+ * <p>{@code equalityFieldIds} is in the order the planner read it from the 
delete file metadata
+ * (not necessarily sorted). The worker's keyBy uses the canonical sorted 
order separately when
+ * deduplicating field sets.
+ */
+@Internal
+public class ReadCommand implements Serializable {
+
+  public enum Type {
+    DATA_FILE,
+    EQ_DELETE_FILE,
+    POS_DELETE_FILE
+  }
+
+  private final Type type;
+  private final String filePath;
+  private final FileFormat format;
+  private final List<Integer> equalityFieldIds;
+  // Can be null if the main branch does not contain any snapshots
+  private final Long mainSnapshotId;
+
+  private ReadCommand(
+      Type type,
+      String filePath,
+      FileFormat format,
+      List<Integer> equalityFieldIds,
+      Long mainSnapshotId) {
+    this.type = type;
+    this.filePath = filePath;
+    this.format = format;
+    this.equalityFieldIds = Lists.newArrayList(equalityFieldIds);
+    this.mainSnapshotId = mainSnapshotId;
+  }
+
+  public static ReadCommand dataFile(
+      String filePath, FileFormat format, List<Integer> equalityFieldIds, Long 
mainSnapshotId) {
+    return new ReadCommand(Type.DATA_FILE, filePath, format, equalityFieldIds, 
mainSnapshotId);
+  }
+
+  public static ReadCommand eqDeleteFile(
+      String filePath, FileFormat format, List<Integer> equalityFieldIds, Long 
mainSnapshotId) {
+    return new ReadCommand(Type.EQ_DELETE_FILE, filePath, format, 
equalityFieldIds, mainSnapshotId);
+  }
+
+  public static ReadCommand posDeleteFile(String filePath, FileFormat format, 
Long mainSnapshotId) {
+    return new ReadCommand(
+        Type.POS_DELETE_FILE, filePath, format, Collections.emptyList(), 
mainSnapshotId);
+  }
+
+  public Type type() {
+    return type;
+  }
+
+  public String filePath() {
+    return filePath;
+  }
+
+  public FileFormat format() {
+    return format;
+  }
+
+  public List<Integer> equalityFieldIds() {
+    return equalityFieldIds;
+  }
+
+  public long mainSnapshotId() {
+    return mainSnapshotId;
+  }

Review Comment:
   Thanks for spotting this. I had recently converted from boxed to primitive, 
but I decided to revert to boxed because the mainSnapshotId for empty tables 
can be null and I didn't like using a special value in this case.



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