stevenzwu commented on code in PR #15241: URL: https://github.com/apache/iceberg/pull/15241#discussion_r2771735446
########## core/src/main/java/org/apache/iceberg/SnapshotFileChanges.java: ########## @@ -0,0 +1,268 @@ +/* + * 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 java.io.IOException; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Queue; +import java.util.concurrent.ExecutorService; +import org.apache.iceberg.exceptions.RuntimeIOException; +import org.apache.iceberg.io.FileIO; +import org.apache.iceberg.relocated.com.google.common.base.Preconditions; +import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList; +import org.apache.iceberg.relocated.com.google.common.collect.Iterables; +import org.apache.iceberg.relocated.com.google.common.collect.Lists; +import org.apache.iceberg.relocated.com.google.common.collect.Queues; +import org.apache.iceberg.util.Tasks; + +/** + * Helper class for retrieving file changes in a snapshot with caching. + * + * <p>This class caches the results of file change detection operations, making it efficient to + * query multiple file change types for the same snapshot. By default, manifests are read + * sequentially. Use {@link Builder#executeWith(ExecutorService)} to enable parallel reading. + */ +public class SnapshotFileChanges { Review Comment: can we keep this class as package private to start with? `SnapshotUtil` is in a diff package which prevents this. Not sure if `ChangelogUtil` is a better place. Or maybe add a new `ChangeDetectionUtil`? ########## core/src/test/java/org/apache/iceberg/util/TestSnapshotUtil.java: ########## @@ -248,4 +249,123 @@ public void schemaForTag() { assertThat(table.schema().asStruct()).isEqualTo(expected.asStruct()); assertThat(SnapshotUtil.schemaFor(table, tag).asStruct()).isEqualTo(initialSchema.asStruct()); } + + @Test + public void testAddedDataFiles() { + DataFile addedFile = + DataFiles.builder(SPEC) + .withPath("/path/to/test-data.parquet") + .withFileSizeInBytes(10) + .withRecordCount(1) + .build(); + + table.newFastAppend().appendFile(addedFile).commit(); + Snapshot snapshotWithAddedFile = table.currentSnapshot(); + + // Test the utility method with table + Iterable<DataFile> filesFromTableUtil = Review Comment: nit: the variable name is a bit misleading. it is more like `filesFromTableParam`, because there is actually a class called `TableUtil`. I also made another comment that we may not need to have the overload that takes a `Table` arg. ########## flink/v1.20/flink/src/main/java/org/apache/iceberg/flink/maintenance/operator/TableChange.java: ########## @@ -54,8 +56,33 @@ private TableChange( this.commitCount = commitCount; } - TableChange(Snapshot snapshot, FileIO io) { - this(snapshot.addedDataFiles(io), snapshot.addedDeleteFiles(io)); + TableChange(Snapshot snapshot, Table table) { Review Comment: this code is for the old Flink source, which is deprecated since 1.7.0. It is fine if we copy the code a little bit ########## core/src/main/java/org/apache/iceberg/CherryPickOperation.java: ########## @@ -82,7 +82,9 @@ public CherryPickOperation cherrypick(long snapshotId) { set(SnapshotSummary.SOURCE_SNAPSHOT_ID_PROP, String.valueOf(snapshotId)); // Pick modifications from the snapshot - for (DataFile addedFile : cherrypickSnapshot.addedDataFiles(io)) { + SnapshotFileChanges changes = + SnapshotFileChanges.builder(cherrypickSnapshot, io, specsById).build(); Review Comment: This looks reasonable to me. Cached or not is the internal impl of `SnapshotFileChanges` ########## core/src/test/java/org/apache/iceberg/util/TestSnapshotUtil.java: ########## @@ -248,4 +249,123 @@ public void schemaForTag() { assertThat(table.schema().asStruct()).isEqualTo(expected.asStruct()); assertThat(SnapshotUtil.schemaFor(table, tag).asStruct()).isEqualTo(initialSchema.asStruct()); } + + @Test + public void testAddedDataFiles() { + DataFile addedFile = + DataFiles.builder(SPEC) + .withPath("/path/to/test-data.parquet") + .withFileSizeInBytes(10) + .withRecordCount(1) + .build(); + + table.newFastAppend().appendFile(addedFile).commit(); + Snapshot snapshotWithAddedFile = table.currentSnapshot(); + + // Test the utility method with table + Iterable<DataFile> filesFromTableUtil = + SnapshotUtil.addedDataFiles(table, snapshotWithAddedFile); + assertThat(filesFromTableUtil).hasSize(1); + + // Test the utility method with explicit parameters + Iterable<DataFile> filesFromExplicitParams = + SnapshotUtil.addedDataFiles(snapshotWithAddedFile, table.io(), table.specs()); + assertThat(filesFromExplicitParams).hasSize(1); + + // Test using SnapshotFileChanges object directly (for caching) Review Comment: it doesn't seem like we tested caching here. for caching test, we need to call the `addedDataFiles` method twice and check if the 2nd result is the same object as the 1st result. but maybe, it should be moved to a separate `TestSnapshotFileChanges` ########## core/src/main/java/org/apache/iceberg/SnapshotFileChanges.java: ########## @@ -0,0 +1,276 @@ +/* Review Comment: With V4 metadata change, the change detection can be more complicated with manfest DVs. I like this direction of moving the change detection out of the `Snapshot` class, which can just focus on core data structures. This could be a good foundation for the [change detection](https://docs.google.com/document/d/1k4x8utgh41Sn1tr98eynDKCWq035SV_f75rtNHcerVw/edit?tab=t.rrpksmp8zkb#heading=h.v89930re1yf4) in the V4 adaptive tree. In V4, if we are going to colocate DV (deleted old and added new) and data file, it might make sense to expose a combined result. Otherwise, the associations get split first and then need to be joined again. ########## core/src/main/java/org/apache/iceberg/util/SnapshotUtil.java: ########## @@ -536,4 +540,108 @@ public static Snapshot latestSnapshot(TableMetadata metadata, String branch) { return metadata.snapshot(ref.snapshotId()); } + + /** + * Return all data files added to the table in the given snapshot. + * + * @param table the table that owns the snapshot + * @param snapshot the snapshot to detect changes for + * @return all data files added to the table in this snapshot + */ + public static Iterable<DataFile> addedDataFiles(Table table, Snapshot snapshot) { Review Comment: I am wondering if it is necessary to have this overload of `Table` arg. maybe just have the version that takes `(Snapshot, FileIO, Map<Integer, PartitionSpec>)`. I wouldn't mind if the unit test change add an extra arg. -- 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]
