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


##########
flink/v2.1/flink/src/test/java/org/apache/iceberg/flink/maintenance/operator/TestEqualityConvertReader.java:
##########
@@ -0,0 +1,385 @@
+/*
+ * 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 static org.apache.iceberg.flink.SimpleDataUtil.createRecord;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Path;
+import java.util.List;
+import org.apache.flink.streaming.util.OneInputStreamOperatorTestHarness;
+import org.apache.flink.streaming.util.ProcessFunctionTestHarnesses;
+import org.apache.iceberg.DataFile;
+import org.apache.iceberg.DataFiles;
+import org.apache.iceberg.DeleteFile;
+import org.apache.iceberg.FileFormat;
+import org.apache.iceberg.Files;
+import org.apache.iceberg.ManifestFile;
+import org.apache.iceberg.ManifestFiles;
+import org.apache.iceberg.ManifestReader;
+import org.apache.iceberg.PartitionData;
+import org.apache.iceberg.PartitionSpec;
+import org.apache.iceberg.Table;
+import org.apache.iceberg.data.FileHelpers;
+import org.apache.iceberg.data.GenericAppenderHelper;
+import org.apache.iceberg.data.GenericRecord;
+import org.apache.iceberg.deletes.PositionDelete;
+import org.apache.iceberg.relocated.com.google.common.collect.Lists;
+import org.apache.iceberg.relocated.com.google.common.collect.Sets;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+
+class TestEqualityConvertReader extends OperatorTestBase {
+
+  @TempDir private Path tempDir;
+
+  @Test
+  void emitsAddStagingDataRowForDataFile() throws Exception {
+    Table table = createTableWithDelete(3);
+    insert(table, 1, "a");
+
+    DataFile dataFile = getFirstDataFile(table);
+    PartitionSpec spec = table.specs().get(dataFile.specId());
+    List<Integer> fieldIds = Lists.newArrayList(1);
+
+    try (OneInputStreamOperatorTestHarness<ReadCommand, IndexCommand> harness =
+        createHarness(fieldIds)) {
+      harness.open();
+
+      ReadCommand cmd =
+          ReadCommand.stagingDataFile(new FlinkAddedRowsScanTask(dataFile, 
spec), 0L, 0L, 0L);
+      harness.processElement(cmd, 0);
+
+      List<IndexCommand> output = harness.extractOutputValues();
+      assertThat(output).hasSize(1);
+      // stagingDataFile marks a staging snapshot's new data, so the reader 
emits
+      // ADD_STAGING_DATA_ROW.
+      
assertThat(output.get(0).type()).isEqualTo(IndexCommand.Type.ADD_STAGING_DATA_ROW);
+      
assertThat(output.get(0).rowPosition().dataFilePath()).isEqualTo(dataFile.location());
+      assertThat(output.get(0).rowPosition().position()).isZero();
+    }
+  }
+
+  @Test
+  void emitsAddDataRowWhenNotStaging() throws Exception {
+    Table table = createTableWithDelete(3);
+    insert(table, 1, "a");
+
+    DataFile dataFile = getFirstDataFile(table);
+    PartitionSpec spec = table.specs().get(dataFile.specId());
+    List<Integer> fieldIds = Lists.newArrayList(1);
+
+    try (OneInputStreamOperatorTestHarness<ReadCommand, IndexCommand> harness =
+        createHarness(fieldIds)) {
+      harness.open();
+
+      // Same task type as the staging test: the command's staging flag, not 
the task type, decides
+      // that main reindex data is indexed immediately as ADD_DATA_ROW.
+      ReadCommand cmd =
+          ReadCommand.dataFile(new FlinkAddedRowsScanTask(dataFile, spec), 0L, 
0L, 0L);
+      harness.processElement(cmd, 0);
+
+      List<IndexCommand> output = harness.extractOutputValues();
+      assertThat(output).hasSize(1);
+      
assertThat(output.get(0).type()).isEqualTo(IndexCommand.Type.ADD_DATA_ROW);
+    }
+  }
+
+  @Test
+  void emitsResolveDeleteForEqDeleteFile() throws Exception {
+    Table table = createTableWithDelete(3);
+    DeleteFile eqDelete = writeEqualityDelete(table, 1, "a");
+    PartitionSpec spec = table.specs().get(eqDelete.specId());
+    List<Integer> fieldIds = eqDelete.equalityFieldIds();
+
+    try (OneInputStreamOperatorTestHarness<ReadCommand, IndexCommand> harness =
+        createHarness(fieldIds)) {
+      harness.open();
+
+      ReadCommand cmd = ReadCommand.eqDeleteFile(eqDelete, spec, 0L, 0L, 0L);
+      harness.processElement(cmd, 0);
+
+      List<IndexCommand> output = harness.extractOutputValues();
+      assertThat(output).hasSize(1);
+      
assertThat(output.get(0).type()).isEqualTo(IndexCommand.Type.RESOLVE_DELETE);
+      assertThat(output.get(0).rowPosition()).isNull();
+    }
+  }
+
+  @Test
+  void tracksPositionsAcrossRecords() throws Exception {
+    Table table = createTableWithDelete(3);
+
+    DataFile dataFile =
+        new GenericAppenderHelper(table, FileFormat.PARQUET, tempDir)
+            .writeFile(Lists.newArrayList(createRecord(1, "a"), 
createRecord(2, "b")));
+    table.newAppend().appendFile(dataFile).commit();
+    table.refresh();
+
+    PartitionSpec spec = table.specs().get(dataFile.specId());
+    List<Integer> fieldIds = Lists.newArrayList(1);
+
+    try (OneInputStreamOperatorTestHarness<ReadCommand, IndexCommand> harness =
+        createHarness(fieldIds)) {
+      harness.open();
+
+      ReadCommand cmd =
+          ReadCommand.stagingDataFile(new FlinkAddedRowsScanTask(dataFile, 
spec), 0L, 0L, 0L);
+      harness.processElement(cmd, 0);
+
+      List<IndexCommand> output = harness.extractOutputValues();
+      assertThat(output).hasSize(2);
+      assertThat(output.get(0).rowPosition().position()).isZero();
+      assertThat(output.get(1).rowPosition().position()).isEqualTo(1);
+    }
+  }
+
+  @Test
+  void skipsDeletedPositionsWhenCreatingIndexFromMain() throws Exception {
+    Table table = createTableWithDelete(3);
+
+    DataFile dataFile =
+        new GenericAppenderHelper(table, FileFormat.PARQUET, tempDir)
+            .writeFile(
+                Lists.newArrayList(
+                    createRecord(1, "a"), createRecord(2, "b"), 
createRecord(3, "c")));
+    table.newAppend().appendFile(dataFile).commit();
+    table.refresh();
+
+    // DV marks position 1 (id=2) as deleted on main. When the reader creates 
the worker's index
+    // from this data file, it must skip position 1 and emit rows only for 
positions 0 and 2.
+    DeleteFile dv = writeDV(table, dataFile.location(), 1L);
+
+    PartitionSpec spec = table.specs().get(dataFile.specId());
+    List<Integer> fieldIds = Lists.newArrayList(1);
+
+    try (OneInputStreamOperatorTestHarness<ReadCommand, IndexCommand> harness =
+        createHarness(fieldIds)) {
+      harness.open();
+
+      ReadCommand cmd =
+          ReadCommand.stagingDataFile(
+              new FlinkAddedRowsScanTask(dataFile, spec, 
Lists.newArrayList(dv)), 0L, 0L, 0L);
+      harness.processElement(cmd, 0);
+
+      List<IndexCommand> output = harness.extractOutputValues();
+      assertThat(output).hasSize(2);
+      assertThat(output.get(0).rowPosition().position()).isZero();
+      assertThat(output.get(1).rowPosition().position()).isEqualTo(2);
+    }
+  }
+
+  @Test
+  void sameKeyFromDataAndDeleteProducesEqualSerializedValues() throws 
Exception {
+    Table table = createTableWithDelete(3);
+
+    DataFile dataFile =
+        new GenericAppenderHelper(table, FileFormat.PARQUET, tempDir)
+            .writeFile(Lists.newArrayList(createRecord(1, "a")));
+    table.newAppend().appendFile(dataFile).commit();
+    table.refresh();
+
+    DeleteFile eqDelete = writeEqualityDelete(table, 1, "a");
+    PartitionSpec dataSpec = table.specs().get(dataFile.specId());
+    PartitionSpec deleteSpec = table.specs().get(eqDelete.specId());
+    List<Integer> fieldIds = eqDelete.equalityFieldIds();
+
+    try (OneInputStreamOperatorTestHarness<ReadCommand, IndexCommand> harness =
+        createHarness(fieldIds)) {
+      harness.open();
+
+      harness.processElement(
+          ReadCommand.stagingDataFile(new FlinkAddedRowsScanTask(dataFile, 
dataSpec), 0L, 0L, 0L),
+          0);
+      harness.processElement(ReadCommand.eqDeleteFile(eqDelete, deleteSpec, 
0L, 0L, 0L), 1);
+
+      List<IndexCommand> output = harness.extractOutputValues();
+      assertThat(output).hasSize(2);
+      
assertThat(output.get(0).type()).isEqualTo(IndexCommand.Type.ADD_STAGING_DATA_ROW);
+      
assertThat(output.get(1).type()).isEqualTo(IndexCommand.Type.RESOLVE_DELETE);
+      assertThat(output.get(0).key()).isEqualTo(output.get(1).key());
+    }
+  }
+
+  @Test
+  void propagatesMainSnapshotId() throws Exception {
+    Table table = createTableWithDelete(3);
+    insert(table, 1, "a");
+
+    DataFile dataFile = getFirstDataFile(table);
+    PartitionSpec spec = table.specs().get(dataFile.specId());
+    List<Integer> fieldIds = Lists.newArrayList(1);
+    long mainSnapshotId = 42L;
+    long mainSequenceNumber = 7L;
+    long dataSequenceNumber = 9L;
+
+    try (OneInputStreamOperatorTestHarness<ReadCommand, IndexCommand> harness =
+        createHarness(fieldIds)) {
+      harness.open();
+
+      ReadCommand cmd =
+          ReadCommand.stagingDataFile(
+              new FlinkAddedRowsScanTask(dataFile, spec),
+              mainSnapshotId,
+              mainSequenceNumber,
+              dataSequenceNumber);
+      harness.processElement(cmd, 0);
+
+      List<IndexCommand> output = harness.extractOutputValues();
+      assertThat(output).hasSize(1);
+      assertThat(output.get(0).mainSnapshotId()).isEqualTo(mainSnapshotId);
+      
assertThat(output.get(0).mainSequenceNumber()).isEqualTo(mainSequenceNumber);
+      
assertThat(output.get(0).rowPosition().dataSequenceNumber()).isEqualTo(dataSequenceNumber);
+    }
+  }
+
+  @Test
+  void routesExceptionToErrorStream() throws Exception {
+    Table table = createTableWithDelete(3);
+    insert(table, 1, "a");
+
+    DataFile dataFile = getFirstDataFile(table);
+    DataFile missing =
+        DataFiles.builder(table.spec())
+            .copy(dataFile)
+            .withPath(dataFile.location() + ".missing")
+            .build();
+    PartitionSpec spec = table.specs().get(dataFile.specId());
+    List<Integer> fieldIds = Lists.newArrayList(1);
+
+    try (OneInputStreamOperatorTestHarness<ReadCommand, IndexCommand> harness =
+        createHarness(fieldIds)) {
+      harness.open();
+
+      // Non-existent file path causes the reader to throw.
+      ReadCommand cmd =
+          ReadCommand.stagingDataFile(new FlinkAddedRowsScanTask(missing, 
spec), 0L, 0L, 0L);
+      harness.processElement(cmd, 0);
+
+      assertThat(harness.extractOutputValues()).isEmpty();
+      
assertThat(harness.getSideOutput(TaskResultAggregator.ERROR_STREAM)).hasSize(1);

Review Comment:
   Thanks. Added!



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