mxm commented on code in PR #16858: URL: https://github.com/apache/iceberg/pull/16858#discussion_r3434366995
########## flink/v2.1/flink/src/test/java/org/apache/iceberg/flink/maintenance/operator/TestEqualityConvertDVWriter.java: ########## @@ -0,0 +1,431 @@ +/* + * 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 java.util.List; +import org.apache.flink.streaming.api.watermark.Watermark; +import org.apache.flink.streaming.runtime.streamrecord.StreamRecord; +import org.apache.flink.streaming.util.TwoInputStreamOperatorTestHarness; +import org.apache.iceberg.DataFile; +import org.apache.iceberg.ManifestFile; +import org.apache.iceberg.ManifestFiles; +import org.apache.iceberg.ManifestReader; +import org.apache.iceberg.PartitionSpec; +import org.apache.iceberg.RowDelta; +import org.apache.iceberg.SnapshotRef; +import org.apache.iceberg.Table; +import org.apache.iceberg.relocated.com.google.common.collect.Lists; +import org.junit.jupiter.api.Test; + +class TestEqualityConvertDVWriter extends OperatorTestBase { + + private static final byte[] EMPTY_PARTITION = new byte[0]; + + @Test + void writesDVFileForSinglePosition() throws Exception { + Table table = createTableWithDelete(3); + insert(table, 1, "a"); + + String dataFilePath = getFirstDataFilePath(table); + + try (TwoInputStreamOperatorTestHarness<DVPosition, EqualityConvertPlan, DVWriteResult> harness = + createHarness()) { + harness.open(); + + long time = System.currentTimeMillis(); + harness.processElement1( + new StreamRecord<>(new DVPosition(dataFilePath, 0, 0, EMPTY_PARTITION, 0L), time)); + harness.processElement2(new StreamRecord<>(emptyEqualityConvertPlan(), time)); + + harness.processBothWatermarks(new Watermark(time)); + + List<DVWriteResult> output = harness.extractOutputValues(); + assertThat(output).hasSize(1); + assertThat(output.get(0).hasError()).isFalse(); + assertThat(output.get(0).dvFiles()).hasSize(1); + } + } + + @Test + void writesSingleDVFileForMultiplePositionsOnSameDataFile() throws Exception { + Table table = createTableWithDelete(3); + insert(table, 1, "a"); + insert(table, 2, "b"); + insert(table, 3, "c"); + + String dataFilePath = getFirstDataFilePath(table); + + try (TwoInputStreamOperatorTestHarness<DVPosition, EqualityConvertPlan, DVWriteResult> harness = + createHarness()) { + harness.open(); + + long time = System.currentTimeMillis(); + harness.processElement1( + new StreamRecord<>(new DVPosition(dataFilePath, 0, 0, EMPTY_PARTITION, 0L), time)); + harness.processElement1( + new StreamRecord<>(new DVPosition(dataFilePath, 1, 0, EMPTY_PARTITION, 0L), time)); + harness.processElement1( + new StreamRecord<>(new DVPosition(dataFilePath, 2, 0, EMPTY_PARTITION, 0L), time)); + harness.processElement2(new StreamRecord<>(emptyEqualityConvertPlan(), time)); + + harness.processBothWatermarks(new Watermark(time)); + + List<DVWriteResult> output = harness.extractOutputValues(); + assertThat(output).hasSize(1); + assertThat(output.get(0).hasError()).isFalse(); + assertThat(output.get(0).dvFiles()).hasSize(1); + assertThat(output.get(0).dvFiles().get(0).recordCount()).isEqualTo(3); + assertThat(output.get(0).rewrittenDvFiles()).isEmpty(); + } + } + + @Test + void emptyPositionsProducesNoOutput() throws Exception { + createTableWithDelete(3); + + try (TwoInputStreamOperatorTestHarness<DVPosition, EqualityConvertPlan, DVWriteResult> harness = + createHarness()) { + harness.open(); + + long time = System.currentTimeMillis(); + harness.processElement2(new StreamRecord<>(emptyEqualityConvertPlan(), time)); + + harness.processBothWatermarks(new Watermark(time)); + + assertThat(harness.extractOutputValues()).isEmpty(); + } + } + + @Test + void noOutputWithoutPlanResult() throws Exception { + Table table = createTableWithDelete(3); + insert(table, 1, "a"); + + String dataFilePath = getFirstDataFilePath(table); + + try (TwoInputStreamOperatorTestHarness<DVPosition, EqualityConvertPlan, DVWriteResult> harness = + createHarness()) { + harness.open(); + + long time = System.currentTimeMillis(); + harness.processElement1( + new StreamRecord<>(new DVPosition(dataFilePath, 0, 0, EMPTY_PARTITION, 0L), time)); + + harness.processBothWatermarks(new Watermark(time)); + + assertThat(harness.extractOutputValues()).isEmpty(); + } + } + + @Test + void writesDVFilesForMultipleDataFiles() throws Exception { + Table table = createTableWithDelete(3); + insert(table, 1, "a"); + insert(table, 2, "b"); + + List<String> dataFilePaths = getDataFilePaths(table); + assertThat(dataFilePaths).hasSize(2); + + try (TwoInputStreamOperatorTestHarness<DVPosition, EqualityConvertPlan, DVWriteResult> harness = + createHarness()) { + harness.open(); + + long time = System.currentTimeMillis(); + harness.processElement1( + new StreamRecord<>( + new DVPosition(dataFilePaths.get(0), 0, 0, EMPTY_PARTITION, 0L), time)); + harness.processElement1( + new StreamRecord<>( + new DVPosition(dataFilePaths.get(1), 0, 0, EMPTY_PARTITION, 0L), time)); + harness.processElement2(new StreamRecord<>(emptyEqualityConvertPlan(), time)); + + harness.processBothWatermarks(new Watermark(time)); + + List<DVWriteResult> output = harness.extractOutputValues(); + assertThat(output).hasSize(1); + assertThat(output.get(0).dvFiles()).hasSize(2); + } + } + + @Test + void routesErrorToErrorStream() throws Exception { + Table table = createTableWithDelete(3); + insert(table, 1, "a"); + + String dataFilePath = getFirstDataFilePath(table); + + try (TwoInputStreamOperatorTestHarness<DVPosition, EqualityConvertPlan, DVWriteResult> harness = + createHarness()) { + harness.open(); + + long time = System.currentTimeMillis(); + harness.processElement1( + new StreamRecord<>(new DVPosition(dataFilePath, 0, 0, EMPTY_PARTITION, 0L), time)); + harness.processElement2(new StreamRecord<>(emptyEqualityConvertPlan(), time)); + + dropTable(); + + harness.processBothWatermarks(new Watermark(time)); + + assertThat(harness.extractOutputValues()).hasSize(1); + assertThat(harness.extractOutputValues().get(0).hasError()).isTrue(); + assertThat(harness.getSideOutput(TaskResultAggregator.ERROR_STREAM)).hasSize(1); + } + } + + @Test + void writesDVForStagingDataFile() throws Exception { + Table table = createTableWithDelete(3); + insert(table, 1, "a"); + + String dataFilePath = getFirstDataFilePath(table); + DataFile stagingDataFile = getFirstDataFile(table); + + try (TwoInputStreamOperatorTestHarness<DVPosition, EqualityConvertPlan, DVWriteResult> harness = + createHarness()) { + harness.open(); + + long time = System.currentTimeMillis(); + EqualityConvertPlan planResult = + new EqualityConvertPlan( + Lists.newArrayList(stagingDataFile), Lists.newArrayList(), 1L, null, 0L, 0L); Review Comment: Thanks! I modified the test to verify that the merging works as expected. -- 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]
