xanderbailey commented on code in PR #2337:
URL: https://github.com/apache/iceberg-rust/pull/2337#discussion_r3143635799


##########
crates/iceberg/src/scan/incremental.rs:
##########
@@ -0,0 +1,802 @@
+// 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.
+
+//! Incremental append scan for reading only newly added data between 
snapshots.
+
+use std::collections::HashSet;
+use std::sync::Arc;
+
+use crate::expr::Predicate;
+use crate::scan::context::{ManifestEntryFilter, ManifestFileFilter};
+use crate::scan::{ScanConfig, TableScan, build_table_scan};
+use crate::spec::{ManifestContentType, ManifestStatus, Operation, 
TableMetadataRef};
+use crate::table::Table;
+use crate::util::available_parallelism;
+use crate::util::snapshot::ancestors_between;
+use crate::{Error, ErrorKind, Result};
+
+/// Represents a validated range of snapshots for incremental scanning.
+///
+/// This struct is used to track which snapshot IDs are included in an 
incremental
+/// scan range, allowing efficient filtering of manifest entries.
+#[derive(Debug, Clone)]
+pub(crate) struct AppendSnapshotSet {
+    /// Snapshot IDs in the range
+    snapshot_ids: HashSet<i64>,
+}
+
+impl AppendSnapshotSet {
+    /// Build a snapshot range by walking the snapshot ancestry chain.
+    ///
+    /// Validates that `from_snapshot_id` is an ancestor of `to_snapshot_id` 
and
+    /// collects all snapshot IDs in between. Also validates that all snapshots
+    /// in the range have APPEND operations.
+    ///
+    /// # Arguments
+    /// * `table_metadata` - The table metadata containing snapshot information
+    /// * `from_snapshot_id` - The starting snapshot ID
+    /// * `to_snapshot_id` - The ending snapshot ID
+    /// * `from_inclusive` - Whether to include the from_snapshot in the range
+    pub(crate) fn build(
+        table_metadata: &TableMetadataRef,
+        from_snapshot_id: i64,
+        to_snapshot_id: i64,
+        from_inclusive: bool,
+    ) -> Result<Self> {
+        // Determine the exclusive stop point for the ancestry walk.
+        // For inclusive mode the from-snapshot must exist so we can look up
+        // its parent. For exclusive mode the snapshot may have been expired
+        // (the parent pointer on its child still references it), so we only
+        // need the ID — matching Java's BaseIncrementalScan semantics.
+        let oldest_exclusive = if from_inclusive {
+            let from_snapshot =
+                table_metadata
+                    .snapshot_by_id(from_snapshot_id)
+                    .ok_or_else(|| {
+                        Error::new(
+                            ErrorKind::DataInvalid,
+                            format!("Snapshot {from_snapshot_id} not found"),
+                        )
+                    })?;
+            from_snapshot.parent_snapshot_id()
+        } else {
+            Some(from_snapshot_id)
+        };
+
+        let snapshots: Vec<_> =
+            ancestors_between(table_metadata, to_snapshot_id, 
oldest_exclusive).collect();
+
+        // ancestors_between silently returns the full chain to root if
+        // oldest_exclusive isn't in the ancestry chain. Detect this:
+        // if we got snapshots but from_snapshot_id wasn't encountered as
+        // the stop point, the chain doesn't connect.
+        if from_snapshot_id == to_snapshot_id {
+            // Edge case: from == to. In exclusive mode, range is empty.
+            // In inclusive mode, we should have exactly one snapshot.
+            if !from_inclusive {
+                return Ok(Self {
+                    snapshot_ids: HashSet::new(),
+                });
+            }
+        } else if snapshots.is_empty() {
+            // to_snapshot_id doesn't exist
+            return Err(Error::new(
+                ErrorKind::DataInvalid,
+                format!(
+                    "from_snapshot {from_snapshot_id} is not an ancestor of 
to_snapshot {to_snapshot_id}",
+                ),
+            ));
+        } else {
+            // Verify the oldest snapshot in our walk is actually connected
+            // to from_snapshot_id. The last snapshot's parent (for exclusive)
+            // or the last snapshot itself (for inclusive) should be 
from_snapshot_id.
+            let oldest_collected = snapshots.last().unwrap();
+            let connects = if from_inclusive {
+                oldest_collected.snapshot_id() == from_snapshot_id
+            } else {
+                oldest_collected.parent_snapshot_id() == Some(from_snapshot_id)
+            };
+            if !connects {
+                return Err(Error::new(
+                    ErrorKind::DataInvalid,
+                    format!(
+                        "from_snapshot {from_snapshot_id} is not an ancestor 
of to_snapshot {to_snapshot_id}",
+                    ),
+                ));
+            }
+        }
+
+        // Collect only APPEND snapshot IDs, silently skipping non-APPEND
+        // snapshots (e.g. replace/compaction, overwrite, delete). This matches
+        // the Java BaseIncrementalAppendScan behavior — only append operations
+        // contribute new data files to an incremental append scan.
+        let mut snapshot_ids = HashSet::with_capacity(snapshots.len());
+        for snapshot in &snapshots {
+            if snapshot.summary().operation == Operation::Append {
+                snapshot_ids.insert(snapshot.snapshot_id());
+            }
+        }
+
+        Ok(Self { snapshot_ids })
+    }
+
+    /// Check if a snapshot_id is within this set
+    pub(crate) fn contains(&self, snapshot_id: i64) -> bool {
+        self.snapshot_ids.contains(&snapshot_id)
+    }
+
+    /// Create a manifest file filter that skips delete manifests and data
+    /// manifests whose `added_snapshot_id` is outside this set.
+    pub(crate) fn manifest_file_filter(self: &Arc<Self>) -> ManifestFileFilter 
{
+        let set = self.clone();
+        Arc::new(move |manifest_file| {
+            manifest_file.content != ManifestContentType::Deletes
+                && set.contains(manifest_file.added_snapshot_id)
+        })
+    }
+
+    /// Create a manifest entry filter that includes only entries with
+    /// status ADDED and a snapshot_id within this set.
+    pub(crate) fn manifest_entry_filter(self: &Arc<Self>) -> 
ManifestEntryFilter {
+        let set = self.clone();
+        Arc::new(move |entry| {
+            entry.status() == ManifestStatus::Added
+                && entry.snapshot_id().is_some_and(|id| set.contains(id))
+        })
+    }
+}
+
+/// Builder to create an incremental append scan between two snapshots.
+///
+/// An incremental append scan returns only data files that were added in
+/// snapshots between `from_snapshot_id` and the target snapshot. Only
+/// snapshots with APPEND operations are supported.
+///
+/// Use [`Table::incremental_append_scan`] or
+/// [`Table::incremental_append_scan_inclusive`] to create an instance.
+pub struct IncrementalAppendScanBuilder<'a> {

Review Comment:
   There is duplication here between the scan builders which I've left for now 
until maybe we have a 3rd variant and we can pick out the common patterns a bit 
better.



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