CTTY commented on code in PR #3564:
URL: https://github.com/apache/iceberg-rust/pull/3564#discussion_r4780162274


##########
crates/iceberg/src/transaction/snapshot/manifest_filter.rs:
##########
@@ -0,0 +1,316 @@
+// 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.
+
+//! Rewrites existing manifests to mark files as deleted.
+
+use std::collections::{HashMap, HashSet};
+
+use uuid::Uuid;
+
+use super::{manifest_path, new_manifest_writer};
+use crate::error::{Result, invalid_data};
+use crate::io::object_cache::ManifestKey;
+use crate::spec::{DataFile, DataFileFormat, FormatVersion, 
ManifestContentType, ManifestFile};
+use crate::table::Table;
+
+/// Identifies one entry to mark as `Deleted`, a data file or a delete file.
+///
+/// A path alone is not enough: a Puffin file may hold several deletion
+/// vectors, each its own entry at a distinct blob offset. Data files and
+/// non-DV delete files have no offset, so for them this is the path.
+#[derive(Clone, Debug, PartialEq, Eq, Hash)]
+struct RemovedFileIdentity {
+    /// The absolute path, as stored in the manifest entry. Comparison is
+    /// textual, so a relative path would not match the entry it names.
+    path: String,
+    content_offset: Option<i64>,
+    content_size_in_bytes: Option<i64>,
+}
+
+impl RemovedFileIdentity {
+    fn of(file: &DataFile) -> Self {
+        Self {
+            path: file.file_path().to_string(),
+            content_offset: file.content_offset(),
+            content_size_in_bytes: file.content_size_in_bytes(),
+        }
+    }
+}
+
+impl std::fmt::Display for RemovedFileIdentity {
+    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+        match (self.content_offset, self.content_size_in_bytes) {
+            (Some(offset), Some(size)) => write!(f, "{}@{offset}+{size}", 
self.path),
+            _ => f.write_str(&self.path),
+        }
+    }
+}
+
+/// Removes a fixed set of files from the manifests of one content type.
+///
+/// One manager lives for one logical execution of an action and is driven
+/// once per attempt via [`ManifestFilterManager::filter`]. A manifest that
+/// references none of the files passes through untouched. One that does is
+/// rewritten with those entries marked `Deleted` and the rest `Existing`.
+///
+/// A rewrite is reused across attempts while its inputs are unchanged. Its
+/// entries carry the sequence numbers and row ids the source's entries
+/// inherited from the manifest list when they were read, so the reuse key is
+/// the source's [`ManifestKey`], not its path alone: a transaction-local
+/// source manifest keeps its path across attempts but inherits new values
+/// after a rebase.
+pub(crate) struct ManifestFilterManager {
+    content: ManifestContentType,
+    /// The files to remove, by absolute path (and blob range for deletion
+    /// vectors). Fixed for the execution.
+    files_to_remove: HashSet<RemovedFileIdentity>,
+    /// Whether a file to remove that is not live in any manifest fails the
+    /// attempt. Off lets a retry succeed after a concurrent commit removed the
+    /// same file; the action decides which it needs.
+    fail_missing_delete_paths: bool,
+    /// Absolute paths of the data files the commit removes. A deletion vector
+    /// referencing one of them is dangling and is marked `Deleted` too. Not
+    /// required to be found, unlike `files_to_remove`. Only set on the delete
+    /// filter; see [`Self::with_dangling_dvs_for`].
+    removed_data_file_paths: HashSet<String>,
+    /// Source manifest → rewritten result.
+    filtered: HashMap<ManifestKey, FilteredManifest>,
+    /// Format version the rewrites were written under. Per spec, v1 entries
+    /// cannot inherit sequence numbers, so an upgrade invalidates them.
+    format_version: Option<FormatVersion>,

Review Comment:
   a `RetryContext` or `AttemptContext` sounds pretty clean, but we still have 
to store the format version in the struct to validate against the 
format_version of the current attempt to make sure we invalidate the cache when 
format is being upgraded



##########
crates/iceberg/src/transaction/snapshot/merging.rs:
##########
@@ -0,0 +1,1417 @@
+// 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.
+
+//! Persistent producer for snapshot actions that add and remove data and
+//! delete files.
+
+use std::collections::{HashMap, HashSet};
+
+use typed_builder::TypedBuilder;
+use uuid::Uuid;
+
+use super::manifest_filter::ManifestFilterManager;
+use super::{
+    AddedManifests, SnapshotCommitInput, current_manifests, 
generate_snapshot_id,
+    manifest_list_path, manifest_path, validate_partition_value, 
write_added_manifest,
+    write_snapshot_commit,
+};
+use crate::error::{Result, invalid_data};
+use crate::spec::{
+    DataContentType, DataFile, DataFileFormat, FormatVersion, 
ManifestContentType, ManifestFile,
+    Operation,
+};
+use crate::table::Table;
+use crate::transaction::ActionCommit;
+
+/// What a merging action asks the producer to do.
+///
+/// Fixed for one logical execution; the producer derives everything else from
+/// it and the table of the current attempt. The action validates the
+/// table-independent parts, such as content types, before each attempt. A path
+/// may be both removed and added: the producer marks the old entry `Deleted`
+/// and writes a new `Added` one, so whether to allow it is the action's call.
+#[derive(TypedBuilder)]
+pub(crate) struct MergingIntent {
+    /// Overrides the generated commit UUID.
+    #[builder(default, setter(strip_option))]
+    commit_uuid: Option<Uuid>,
+    operation: Operation,
+    #[builder(default)]
+    snapshot_properties: HashMap<String, String>,
+    #[builder(default)]
+    added_data_files: Vec<DataFile>,
+    #[builder(default)]
+    added_delete_files: Vec<DataFile>,
+    #[builder(default)]
+    removed_data_files: Vec<DataFile>,
+    #[builder(default)]
+    removed_delete_files: Vec<DataFile>,
+    /// Whether a removed file that is no longer live fails the commit.
+    ///
+    /// Off by default: a retry after a concurrent commit removed the same
+    /// file still commits, which is harmless for actions such as a
+    /// merge-on-read delete replacing a position delete file. Actions that
+    /// must not double-remove (a rewrite whose replacement files would
+    /// otherwise duplicate rows) turn it on.
+    #[builder(default)]
+    fail_missing_delete_paths: bool,
+    /// Whether the summary resets table totals (a full-table overwrite).
+    #[builder(default)]
+    truncate_full_table: bool,
+}
+
+impl MergingIntent {
+    pub(crate) fn commit_uuid(&self) -> Option<Uuid> {
+        self.commit_uuid
+    }
+
+    pub(crate) fn operation(&self) -> &Operation {
+        &self.operation
+    }
+
+    pub(crate) fn snapshot_properties(&self) -> &HashMap<String, String> {
+        &self.snapshot_properties
+    }
+
+    pub(crate) fn added_data_files(&self) -> &[DataFile] {
+        &self.added_data_files
+    }
+
+    pub(crate) fn added_delete_files(&self) -> &[DataFile] {
+        &self.added_delete_files
+    }
+
+    pub(crate) fn removed_data_files(&self) -> &[DataFile] {
+        &self.removed_data_files
+    }
+
+    pub(crate) fn removed_delete_files(&self) -> &[DataFile] {
+        &self.removed_delete_files
+    }
+
+    pub(crate) fn fail_missing_delete_paths(&self) -> bool {
+        self.fail_missing_delete_paths
+    }
+
+    pub(crate) fn truncate_full_table(&self) -> bool {
+        self.truncate_full_table
+    }
+}
+
+/// Retry-persistent state for an action that adds and removes data and delete
+/// files.
+///
+/// One producer lives for one logical execution of an action and is driven 
once
+/// per attempt via [`MergingSnapshotProducer::apply`]. Per attempt it takes 
the
+/// current snapshot's manifests, rewrites those that reference a removed file,
+/// appends manifests for the added files, and writes a manifest list. Identity
+/// (commit UUID, snapshot id) is fixed on first use; rewritten and added
+/// manifests are reused across attempts while their inputs are unchanged.
+pub(crate) struct MergingSnapshotProducer {
+    intent: MergingIntent,
+    /// Fixed when the producer is created, so every attempt shares one UUID.
+    commit_uuid: Uuid,
+    /// Resolved against the table on the first attempt, then stable.
+    snapshot_id: Option<i64>,
+    /// Incremented per attempt; distinguishes manifest-list paths.
+    attempt: u64,
+    /// Numbers every manifest this producer writes, including rewrites. Never
+    /// reset, so paths never collide across attempts.
+    manifest_counter: u64,
+    data_filter: ManifestFilterManager,
+    delete_filter: ManifestFilterManager,
+    /// Manifests written for the added files, reused across attempts.
+    added_manifests: Option<AddedManifests>,

Review Comment:
   No, added_manifests should come from manifest writers that live in the 
snapshot producer, not tx actions



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