CTTY commented on code in PR #1353: URL: https://github.com/apache/iceberg-rust/pull/1353#discussion_r2186284135
########## crates/iceberg/src/transaction/validate.rs: ########## @@ -0,0 +1,232 @@ +// 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. + +use std::collections::HashSet; +use std::sync::Arc; + +use crate::spec::{ManifestContentType, ManifestFile, Operation, SnapshotRef, TableMetadata}; +use crate::table::Table; + +pub(crate) trait SnapshotValidator { + #[allow(dead_code)] + fn validate(&self, _table: &Table, _snapshot: Option<&SnapshotRef>) {} + + #[allow(dead_code)] + async fn validation_history( + &self, + base: &Table, + to_snapshot: &SnapshotRef, + from_snapshot: Option<&SnapshotRef>, + matching_operations: HashSet<Operation>, + manifest_content_type: ManifestContentType, + ) -> (Vec<ManifestFile>, HashSet<i64>) { + let mut manifests = vec![]; + let mut new_snapshots = HashSet::new(); + let mut last_snapshot: Option<&SnapshotRef> = None; + + let snapshots = Self::ancestors_between(to_snapshot, from_snapshot, base.metadata()); + for current_snapshot in &snapshots { + last_snapshot = Some(current_snapshot); + + if matching_operations.contains(¤t_snapshot.summary().operation) { + new_snapshots.insert(current_snapshot.snapshot_id()); + current_snapshot + .load_manifest_list(base.file_io(), base.metadata()) + .await + .expect("Failed to load manifest list!") + .entries() + .iter() + .for_each(|manifest| { + if manifest.content == manifest_content_type + && manifest.added_snapshot_id == current_snapshot.snapshot_id() + { + manifests.push(manifest.clone()); + } + }); + } + } + + if last_snapshot.is_some() + && last_snapshot.unwrap().parent_snapshot_id() + != from_snapshot.map(|snapshot| snapshot.snapshot_id()) + { + panic!( + "Cannot determine history between starting snapshot {} and the last known ancestor {}", + from_snapshot.map_or_else( + || "None".to_string(), + |snapshot| snapshot.snapshot_id().to_string() + ), + last_snapshot.map_or_else( + || "None".to_string(), + |snapshot| snapshot.parent_snapshot_id().unwrap().to_string() + ) + ); + } + + (manifests, new_snapshots) + } + + /// find ancestors in (from_snapshot, to_snapshot] + /// TODO: Return an iterator instead of a vector + fn ancestors_between( + to_snapshot: &SnapshotRef, + from_snapshot: Option<&SnapshotRef>, Review Comment: Yes, I believe the validation should be a part of snapshot producing that happens when committing certain transaction actions. This work has been paused and we are trying to make iceberg-rust able to write data first: https://github.com/apache/iceberg-rust/issues/1382 I'll certain check out pyiceberg when resuming this! -- 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]
