aokolnychyi commented on code in PR #5382: URL: https://github.com/apache/iceberg/pull/5382#discussion_r933705197
########## core/src/test/java/org/apache/iceberg/TestBaseIncrementalChangelogScan.java: ########## @@ -0,0 +1,310 @@ +/* + * 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; + +import static org.apache.iceberg.TableProperties.MANIFEST_MERGE_ENABLED; +import static org.apache.iceberg.TableProperties.MANIFEST_MIN_MERGE_COUNT; + +import java.io.IOException; +import java.util.List; +import org.apache.iceberg.expressions.Expressions; +import org.apache.iceberg.io.CloseableIterable; +import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList; +import org.apache.iceberg.relocated.com.google.common.collect.ImmutableSet; +import org.apache.iceberg.relocated.com.google.common.collect.Iterables; +import org.apache.iceberg.relocated.com.google.common.collect.Lists; +import org.junit.Assert; +import org.junit.Assume; +import org.junit.Test; + +public class TestBaseIncrementalChangelogScan + extends ScanTestBase< + IncrementalChangelogScan, ChangelogScanTask, ScanTaskGroup<ChangelogScanTask>> { + + public TestBaseIncrementalChangelogScan(int formatVersion) { + super(formatVersion); + } + + @Override + protected IncrementalChangelogScan newScan() { + return table.newIncrementalChangelogScan(); + } + + @Test + public void testDataFilters() { + table.newFastAppend().appendFile(FILE_A).commit(); + + Snapshot firstSnapshot = table.currentSnapshot(); + ManifestFile firstSnapshotDataManifest = + Iterables.getOnlyElement(firstSnapshot.dataManifests(table.io())); + + table.newFastAppend().appendFile(FILE_B).commit(); + + Snapshot secondSnapshot = table.currentSnapshot(); + + Assert.assertEquals( + "Must be 2 data manifests", 2, secondSnapshot.dataManifests(table.io()).size()); + + withUnavailableLocations( + ImmutableList.of(firstSnapshotDataManifest.path()), + () -> { + // bucket(k, 16) is 1 which is supposed to match only FILE_B + IncrementalChangelogScan scan = newScan().filter(Expressions.equal("data", "k")); + + List<ChangelogScanTask> tasks = plan(scan); + + Assert.assertEquals("Must have 1 task", 1, tasks.size()); + + AddedRowsScanTask task = (AddedRowsScanTask) Iterables.getOnlyElement(tasks); + Assert.assertEquals("Ordinal must match", 1, task.changeOrdinal()); + Assert.assertEquals( + "Snapshot must match", secondSnapshot.snapshotId(), task.commitSnapshotId()); + Assert.assertEquals("Data file must match", FILE_B.path(), task.file().path()); + Assert.assertTrue("Must be no deletes", task.deletes().isEmpty()); + }); + } + + @Test + public void testOverwrites() { + table.newFastAppend().appendFile(FILE_A).appendFile(FILE_B).commit(); + + Snapshot firstSnapshot = table.currentSnapshot(); + + table.newOverwrite().addFile(FILE_A2).deleteFile(FILE_A).commit(); + + Snapshot secondSnapshot = table.currentSnapshot(); + + IncrementalChangelogScan scan = + newScan() + .fromSnapshotExclusive(firstSnapshot.snapshotId()) + .toSnapshot(secondSnapshot.snapshotId()); + + List<ChangelogScanTask> tasks = plan(scan); + + Assert.assertEquals("Must have 2 tasks", 2, tasks.size()); + + AddedRowsScanTask addedRowsTask = (AddedRowsScanTask) tasks.get(0); + Assert.assertEquals("Ordinal must match", 0, addedRowsTask.changeOrdinal()); + Assert.assertEquals( + "Snapshot must match", secondSnapshot.snapshotId(), addedRowsTask.commitSnapshotId()); + Assert.assertEquals("Data file must match", FILE_A2.path(), addedRowsTask.file().path()); + Assert.assertTrue("Must be no deletes", addedRowsTask.deletes().isEmpty()); + + DeletedDataFileScanTask deletedDataFileTask = (DeletedDataFileScanTask) tasks.get(1); + Assert.assertEquals("Ordinal must match", 0, deletedDataFileTask.changeOrdinal()); + Assert.assertEquals( + "Snapshot must match", secondSnapshot.snapshotId(), deletedDataFileTask.commitSnapshotId()); + Assert.assertEquals("Data file must match", FILE_A.path(), deletedDataFileTask.file().path()); + Assert.assertTrue("Must be no deletes", deletedDataFileTask.existingDeletes().isEmpty()); + } + + @Test + public void testFileDeletes() { + table.newFastAppend().appendFile(FILE_A).appendFile(FILE_B).commit(); + + Snapshot firstSnapshot = table.currentSnapshot(); + + table.newDelete().deleteFile(FILE_A).commit(); + + Snapshot secondSnapshot = table.currentSnapshot(); + + IncrementalChangelogScan scan = + newScan() + .fromSnapshotExclusive(firstSnapshot.snapshotId()) + .toSnapshot(secondSnapshot.snapshotId()); + + List<ChangelogScanTask> tasks = plan(scan); + + Assert.assertEquals("Must have 1 tasks", 1, tasks.size()); + + DeletedDataFileScanTask deletedDataFileTask = + (DeletedDataFileScanTask) Iterables.getOnlyElement(tasks); + Assert.assertEquals("Ordinal must match", 0, deletedDataFileTask.changeOrdinal()); + Assert.assertEquals( + "Snapshot must match", secondSnapshot.snapshotId(), deletedDataFileTask.commitSnapshotId()); + Assert.assertEquals("Data file must match", FILE_A.path(), deletedDataFileTask.file().path()); + Assert.assertTrue("Must be no deletes", deletedDataFileTask.existingDeletes().isEmpty()); + } + + @Test + public void testExistingEntriesInNewDataManifestsAreIgnored() { + table + .updateProperties() + .set(MANIFEST_MIN_MERGE_COUNT, "1") + .set(MANIFEST_MERGE_ENABLED, "true") + .commit(); + + table.newAppend().appendFile(FILE_A).commit(); + + table.newAppend().appendFile(FILE_B).commit(); + + table.newAppend().appendFile(FILE_C).commit(); + + Snapshot thirdSnapshot = table.currentSnapshot(); + + ManifestFile manifest = Iterables.getOnlyElement(thirdSnapshot.dataManifests(table.io())); + Assert.assertTrue("Manifest must have existing files", manifest.hasExistingFiles()); + + IncrementalChangelogScan scan = + newScan() + .fromSnapshotInclusive(thirdSnapshot.snapshotId()) + .toSnapshot(thirdSnapshot.snapshotId()); + + List<ChangelogScanTask> tasks = plan(scan); + + Assert.assertEquals("Must have 1 task", 1, tasks.size()); + + AddedRowsScanTask task = (AddedRowsScanTask) Iterables.getOnlyElement(tasks); + Assert.assertEquals("Ordinal must match", 0, task.changeOrdinal()); + Assert.assertEquals("Snapshot must match", thirdSnapshot.snapshotId(), task.commitSnapshotId()); + Assert.assertEquals("Data file must match", FILE_C.path(), task.file().path()); + Assert.assertTrue("Must be no deletes", task.deletes().isEmpty()); + } + + @Test + public void testManifestRewritesAreIgnored() throws IOException { + table.newAppend().appendFile(FILE_A).commit(); + + Snapshot firstSnapshot = table.currentSnapshot(); + + table.newAppend().appendFile(FILE_B).commit(); + + Snapshot secondSnapshot = table.currentSnapshot(); + + ManifestFile newManifest = + writeManifest( + "manifest-file.avro", + manifestEntry(ManifestEntry.Status.EXISTING, firstSnapshot.snapshotId(), FILE_A), + manifestEntry(ManifestEntry.Status.EXISTING, secondSnapshot.snapshotId(), FILE_B)); + + RewriteManifests rewriteManifests = table.rewriteManifests(); + + for (ManifestFile manifest : secondSnapshot.dataManifests(table.io())) { + rewriteManifests.deleteManifest(manifest); + } + + rewriteManifests.addManifest(newManifest); + + rewriteManifests.commit(); + + table.newAppend().appendFile(FILE_C).commit(); + + Snapshot fourthSnapshot = table.currentSnapshot(); + + List<ChangelogScanTask> tasks = plan(newScan()); + + Assert.assertEquals("Must have 3 tasks", 3, tasks.size()); + + AddedRowsScanTask firstTask = (AddedRowsScanTask) tasks.get(0); Review Comment: I should probably reconsider names here. Maybe, `task1` would be better? Then statements below would fit on one line. -- 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]
