diff --git a/crates/iceberg/public-api.txt b/crates/iceberg/public-api.txt index 18157216cd..46c9bf786a 100644 --- a/crates/iceberg/public-api.txt +++ b/crates/iceberg/public-api.txt @@ -2655,6 +2655,7 @@ pub fn iceberg::spec::PartitionSpec::is_compatible_with(&self, other: &iceberg:: pub fn iceberg::spec::PartitionSpec::is_unpartitioned(&self) -> bool pub fn iceberg::spec::PartitionSpec::partition_to_path(&self, data: &iceberg::spec::Struct, schema: iceberg::spec::SchemaRef) -> alloc::string::String pub fn iceberg::spec::PartitionSpec::partition_type(&self, schema: &iceberg::spec::Schema) -> iceberg::Result +pub fn iceberg::spec::PartitionSpec::resolve(&self, schema: &iceberg::spec::Schema, partition: iceberg::spec::Struct) -> iceberg::Result pub fn iceberg::spec::PartitionSpec::spec_id(&self) -> i32 pub fn iceberg::spec::PartitionSpec::unpartition_spec() -> Self pub fn iceberg::spec::PartitionSpec::with_spec_id(self, spec_id: i32) -> Self diff --git a/crates/iceberg/src/io/object_cache.rs b/crates/iceberg/src/io/object_cache.rs index e7152096cf..4c60027a7c 100644 --- a/crates/iceberg/src/io/object_cache.rs +++ b/crates/iceberg/src/io/object_cache.rs @@ -53,6 +53,10 @@ pub(crate) enum CachedObjectKey { Manifest(ManifestKey), } +/// Identifies a parsed manifest: its path plus every manifest-list value the +/// reader inherits onto its entries. Two references to one physical manifest +/// with different inherited values parse to different entries, so anything +/// derived from parsed entries must be keyed by this, not by path alone. #[derive(Clone, Debug, Hash, Eq, PartialEq)] pub(crate) struct ManifestKey { path: String, @@ -62,7 +66,7 @@ pub(crate) struct ManifestKey { } impl ManifestKey { - fn new(manifest_file: &ManifestFile) -> Self { + pub(crate) fn new(manifest_file: &ManifestFile) -> Self { Self { path: manifest_file.manifest_path.clone(), added_snapshot_id: manifest_file.added_snapshot_id, diff --git a/crates/iceberg/src/spec/manifest/writer.rs b/crates/iceberg/src/spec/manifest/writer.rs index 4fb7330146..006936c088 100644 --- a/crates/iceberg/src/spec/manifest/writer.rs +++ b/crates/iceberg/src/spec/manifest/writer.rs @@ -316,10 +316,6 @@ impl ManifestWriter { /// Add a delete manifest entry. This method will update following status of the entry: /// - Update the entry status to `Deleted` /// - Set the snapshot id to the current snapshot id - /// - /// # TODO - /// Remove this allow later - #[allow(dead_code)] pub(crate) fn add_delete_entry(&mut self, mut entry: ManifestEntry) -> Result<()> { self.check_data_file(&entry.data_file)?; entry.status = ManifestStatus::Deleted; @@ -351,10 +347,6 @@ impl ManifestWriter { /// Add an existing manifest entry. This method will update following status of the entry: /// - Update the entry status to `Existing` - /// - /// # TODO - /// Remove this allow later - #[allow(dead_code)] pub(crate) fn add_existing_entry(&mut self, mut entry: ManifestEntry) -> Result<()> { self.check_data_file(&entry.data_file)?; entry.status = ManifestStatus::Existing; diff --git a/crates/iceberg/src/spec/partition.rs b/crates/iceberg/src/spec/partition.rs index bd6ed35429..78b6d7c29d 100644 --- a/crates/iceberg/src/spec/partition.rs +++ b/crates/iceberg/src/spec/partition.rs @@ -28,7 +28,7 @@ use super::transform::Transform; use super::{NestedField, PrimitiveType, Schema, SchemaRef, StructType}; use crate::Result; use crate::error::invalid_data; -use crate::spec::Struct; +use crate::spec::{Literal, Struct}; pub(crate) const UNPARTITIONED_LAST_ASSIGNED_ID: i32 = 999; pub(crate) const DEFAULT_PARTITION_SPEC_ID: i32 = 0; @@ -180,6 +180,30 @@ impl PartitionSpec { true } + /// Resolves `partition` against the partition type this spec has under + /// `schema`, widening a value whose source column was promoted since it was + /// written (`int` to `long`, `float` to `double`). + /// + /// A partition value read from a manifest keeps the literal type it was + /// written with. Java reads manifests against the current partition type + /// and so never sees the difference; a caller that renders or compares a + /// stored value under the current schema resolves it first. + pub fn resolve(&self, schema: &Schema, partition: Struct) -> Result { + let partition_type = self.partition_type(schema)?; + Ok(partition + .iter() + .zip(partition_type.fields()) + .map(|(value, field)| { + value + .and_then(Literal::as_primitive_literal) + .zip(field.field_type.as_primitive_type()) + .and_then(|(literal, target)| literal.promote_to(target)) + .map(Literal::Primitive) + .or_else(|| value.cloned()) + }) + .collect()) + } + /// Returns partition path string containing partition type and partition /// value as key-value pairs. pub fn partition_to_path(&self, data: &Struct, schema: SchemaRef) -> String { @@ -2169,6 +2193,86 @@ mod tests { ); } + #[test] + fn test_resolve_widens_only_promoted_values() { + let field = |id, name, ty| NestedField::required(id, name, Type::Primitive(ty)).into(); + let written = Schema::builder() + .with_fields(vec![ + field(1, "i", PrimitiveType::Int), + field(2, "f", PrimitiveType::Float), + field(3, "s", PrimitiveType::String), + ]) + .build() + .unwrap(); + let spec = PartitionSpec::builder(written) + .add_partition_field("i", "i", Transform::Identity) + .unwrap() + .add_partition_field("f", "f", Transform::Identity) + .unwrap() + .add_partition_field("s", "s", Transform::Identity) + .unwrap() + .build() + .unwrap(); + let promoted = Schema::builder() + .with_fields(vec![ + field(1, "i", PrimitiveType::Long), + field(2, "f", PrimitiveType::Double), + field(3, "s", PrimitiveType::String), + ]) + .build() + .unwrap(); + + let stale = Struct::from_iter([Some(Literal::int(7)), Some(Literal::float(0.1_f32)), None]); + assert_eq!( + spec.resolve(&promoted, stale).unwrap(), + Struct::from_iter([ + Some(Literal::long(7)), + Some(Literal::double(f64::from(0.1_f32))), + None, + ]) + ); + + let current = Struct::from_iter([ + Some(Literal::long(7)), + Some(Literal::double(0.1)), + Some(Literal::string("s")), + ]); + assert_eq!(spec.resolve(&promoted, current.clone()).unwrap(), current); + } + + /// A value written before its source column was promoted renders under + /// the promoted type once resolved: a `float` widened to `double` prints + /// as Java does. + #[test] + fn test_resolve_then_partition_to_path_matches_java() { + let written = Schema::builder() + .with_fields(vec![ + NestedField::required(1, "f", Type::Primitive(PrimitiveType::Float)).into(), + ]) + .build() + .unwrap(); + let spec = PartitionSpec::builder(written) + .add_partition_field("f", "f", Transform::Identity) + .unwrap() + .build() + .unwrap(); + let promoted: SchemaRef = Schema::builder() + .with_fields(vec![ + NestedField::required(1, "f", Type::Primitive(PrimitiveType::Double)).into(), + ]) + .build() + .unwrap() + .into(); + + let stale = Struct::from_iter([Some(Literal::float(0.1_f32))]); + let resolved = spec.resolve(&promoted, stale).unwrap(); + // Not exact in f32: the widened double is rendered. + assert_eq!( + spec.partition_to_path(&resolved, promoted), + "f=0.10000000149011612" + ); + } + #[test] fn test_partition_to_path_float_matches_java() { let schema = Schema::builder() diff --git a/crates/iceberg/src/test_utils/mod.rs b/crates/iceberg/src/test_utils/mod.rs index 94310dd379..3124b8c6d6 100644 --- a/crates/iceberg/src/test_utils/mod.rs +++ b/crates/iceberg/src/test_utils/mod.rs @@ -32,4 +32,4 @@ pub(crate) use delete_vector::encode_dv_blob; pub(crate) use encryption::{make_encrypted_table, make_encryption_manager}; pub(crate) use record_batch::check_record_batches; pub(crate) use runtime::test_runtime; -pub(crate) use transaction::commit_with_fresh_state; +pub(crate) use transaction::{append_to, commit_with_fresh_state, data_file, with_snapshot}; diff --git a/crates/iceberg/src/test_utils/transaction.rs b/crates/iceberg/src/test_utils/transaction.rs index 292848359b..fead63bc53 100644 --- a/crates/iceberg/src/test_utils/transaction.rs +++ b/crates/iceberg/src/test_utils/transaction.rs @@ -15,9 +15,17 @@ // specific language governing permissions and limitations // under the License. +use std::collections::HashMap; +use std::sync::Arc; + use crate::error::Result; +use crate::spec::{ + DataContentType, DataFile, DataFileBuilder, DataFileFormat, Literal, MAIN_BRANCH, SnapshotRef, + SnapshotReference, SnapshotRetention, Struct, +}; use crate::table::Table; -use crate::transaction::{ActionCommit, TransactionAction}; +use crate::transaction::snapshot::simple::{AppendIntent, SimpleSnapshotProducer}; +use crate::transaction::{ActionCommit, Transaction, TransactionAction}; /// Runs a single commit attempt of an action with fresh state. pub(crate) async fn commit_with_fresh_state( @@ -27,3 +35,51 @@ pub(crate) async fn commit_with_fresh_state( let mut state = action.new_state(); action.commit(&mut state, table).await } + +pub(crate) fn data_file(table: &Table, path: &str, record_count: u64) -> DataFile { + DataFileBuilder::default() + .content(DataContentType::Data) + .file_path(path.to_string()) + .file_format(DataFileFormat::Parquet) + .file_size_in_bytes(100) + .record_count(record_count) + .partition_spec_id(table.metadata().default_partition_spec_id()) + .partition(Struct::from_iter([Some(Literal::long(300))])) + .build() + .unwrap() +} + +/// Commits `data_files` to `table` through a fresh producer, returning the +/// table with every update applied, as `TableCommit::apply` would. +pub(crate) async fn append_to(table: &Table, data_files: Vec) -> Table { + let mut producer = SimpleSnapshotProducer::new(AppendIntent { + commit_uuid: None, + snapshot_properties: HashMap::new(), + data_files, + }); + let updates = producer.apply(table).await.unwrap().take_updates(); + Transaction::update_table_metadata(table.clone(), &updates).unwrap() +} + +/// Returns `table` with `snapshot` added and set as current, as a local +/// `TableCommit::apply` would. +pub(crate) fn with_snapshot(table: &Table, snapshot: SnapshotRef) -> Table { + let metadata = table + .metadata() + .clone() + .into_builder(None) + .add_snapshot(snapshot.as_ref().clone()) + .unwrap() + .set_ref( + MAIN_BRANCH, + SnapshotReference::new( + snapshot.snapshot_id(), + SnapshotRetention::branch(None, None, None), + ), + ) + .unwrap() + .build() + .unwrap() + .metadata; + table.clone().with_metadata(Arc::new(metadata)) +} diff --git a/crates/iceberg/src/transaction/mod.rs b/crates/iceberg/src/transaction/mod.rs index 650912a96f..a0c4eb32df 100644 --- a/crates/iceberg/src/transaction/mod.rs +++ b/crates/iceberg/src/transaction/mod.rs @@ -55,7 +55,7 @@ mod action; pub use action::*; mod append; mod expire_snapshots; -mod snapshot; +pub(crate) mod snapshot; mod sort_order; mod update_location; mod update_properties; @@ -98,7 +98,7 @@ impl Transaction { } } - fn update_table_metadata(table: Table, updates: &[TableUpdate]) -> Result { + pub(crate) fn update_table_metadata(table: Table, updates: &[TableUpdate]) -> Result
{ let mut metadata_builder = table.metadata().clone().into_builder(None); for update in updates { metadata_builder = update.clone().apply(metadata_builder)?; diff --git a/crates/iceberg/src/transaction/snapshot/manifest_filter.rs b/crates/iceberg/src/transaction/snapshot/manifest_filter.rs new file mode 100644 index 0000000000..f59613c3ba --- /dev/null +++ b/crates/iceberg/src/transaction/snapshot/manifest_filter.rs @@ -0,0 +1,333 @@ +// 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 typed_builder::TypedBuilder; + +use super::{AttemptContext, new_manifest_writer}; +use crate::error::{Result, invalid_data}; +use crate::io::object_cache::ManifestKey; +use crate::spec::{DataFile, DataFileFormat, FormatVersion, ManifestContentType, ManifestFile}; + +/// 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)] +pub(crate) enum RemovedFileIdentity { + /// A data file, or a delete file that is not a deletion vector. The + /// absolute path, as stored in the manifest entry. Comparison is textual, + /// so a relative path would not match the entry it names. + File(String), + /// A deletion vector: one blob within a Puffin file. + DeletionVector { + path: String, + content_offset: i64, + content_size_in_bytes: i64, + }, +} + +impl RemovedFileIdentity { + /// Fails for a file that carries a blob range but is not a Puffin file, or + /// carries only half a range: it cannot be the deletion vector the range + /// says it is, and would otherwise match no entry. + pub(crate) fn try_of(file: &DataFile) -> Result { + match (file.content_offset(), file.content_size_in_bytes()) { + (None, None) => Ok(Self::File(file.file_path().to_string())), + (Some(content_offset), Some(content_size_in_bytes)) => { + if file.file_format() != DataFileFormat::Puffin { + return Err(invalid_data!( + "Only a deletion vector in a Puffin file has a blob range, not a {:?} file: {}", + file.file_format(), + file.file_path() + )); + } + Ok(Self::DeletionVector { + path: file.file_path().to_string(), + content_offset, + content_size_in_bytes, + }) + } + _ => Err(invalid_data!( + "content_offset and content_size_in_bytes must both be set or both be absent: {}", + file.file_path() + )), + } + } +} + +impl std::fmt::Display for RemovedFileIdentity { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Self::File(path) => f.write_str(path), + Self::DeletionVector { + path, + content_offset, + content_size_in_bytes, + } => write!(f, "{path}@{content_offset}+{content_size_in_bytes}"), + } + } +} + +/// 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. +/// +/// The removal conditions are fixed by the builder; after `build` the only +/// way to change a manager is to drive it with `filter`. +#[derive(TypedBuilder)] +pub(crate) struct ManifestFilterManager { + content: ManifestContentType, + /// The files to remove, as the action named them. Fixed for the + /// execution. Converted to identities on every attempt, so a malformed + /// file is reported from [`Self::filter`] rather than matching nothing. + #[builder(setter(transform = |files: &[DataFile]| files.to_vec()))] + files_to_remove: Vec, + /// 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. + #[builder(default)] + fail_missing_delete_paths: bool, + /// Also marks `Deleted` any deletion vector whose referenced data file is + /// among these, as Java's `removeDanglingDeletesFor` does. Only deletion + /// vectors qualify: a position delete file's `referenced_data_file` is a + /// hint, not a guarantee of its contents. Not required to be found, unlike + /// `files_to_remove`. Only meaningful on the delete filter. + #[builder(default, setter(transform = |removed_data_files: &[DataFile]| { + removed_data_files.iter().map(|f| f.file_path().to_string()).collect() + }))] + dangling_dvs_for: HashSet, + /// Source manifest → rewritten result. + #[builder(default, setter(skip))] + filtered: HashMap, + /// Format version the rewrites were written under. Per spec, v1 entries + /// cannot inherit sequence numbers, so an upgrade invalidates them. + #[builder(default, setter(skip))] + format_version: Option, +} + +/// One rewritten manifest and the files it marked as deleted. +#[derive(Clone)] +pub(crate) struct FilteredManifest { + manifest: ManifestFile, + removed_files: Vec, +} + +/// The result of one filter pass. +#[derive(TypedBuilder)] +pub(crate) struct Filtered { + /// The resulting manifests, in input order. + manifests: Vec, + /// The files that were live and are now marked deleted, including their + /// metrics as read from the source manifests. + #[builder(default)] + removed_files: Vec, +} + +impl Filtered { + pub(crate) fn manifests(&self) -> &[ManifestFile] { + &self.manifests + } + + pub(crate) fn removed_files(&self) -> &[DataFile] { + &self.removed_files + } + + /// Splits into the manifests and the removed files, so the manifests can + /// feed the next filter while the removed files are kept for the summary. + pub(crate) fn into_parts(self) -> (Vec, Vec) { + (self.manifests, self.removed_files) + } +} + +impl ManifestFilterManager { + /// Applies the removals to `manifests`, rewriting those of this manager's + /// content type that reference a file to remove. + /// + /// With `fail_missing_delete_paths`, fails if a file to remove is not live + /// in any manifest: it was never in the table, or a concurrent commit + /// already removed it. Otherwise such files are skipped. + /// + /// Rewritten manifests take their paths from `ctx`, which records them as + /// owned before they are written, so a failed write still leaves its path + /// tracked for cleanup. + pub(crate) async fn filter( + &mut self, + ctx: &mut AttemptContext<'_>, + manifests: Vec, + ) -> Result { + debug_assert!( + self.dangling_dvs_for.is_empty() || self.content == ManifestContentType::Deletes, + "dangling deletion vectors are only removed by the delete filter" + ); + let format_version = ctx.format_version(); + // The delete filter must still scan when no delete file is named but a + // removed data file may leave a DV dangling. DVs exist from v3, so on + // older tables nothing can dangle. + let may_have_dangling_dvs = + !matches!(format_version, FormatVersion::V1 | FormatVersion::V2) + && !self.dangling_dvs_for.is_empty(); + if self.files_to_remove.is_empty() && !may_have_dangling_dvs { + return Ok(Filtered::builder().manifests(manifests).build()); + } + let to_remove: HashSet = self + .files_to_remove + .iter() + .map(RemovedFileIdentity::try_of) + .collect::>()?; + + if self.format_version != Some(format_version) { + self.filtered.clear(); + self.format_version = Some(format_version); + } + + let mut out = Vec::with_capacity(manifests.len()); + let mut removed_files: Vec = Vec::new(); + + // TODO: every manifest of this content type is read, one at a time. + // Skip manifests with no live files and prune by partition summary + // before reading, as Java's ManifestFilterManager does, and read the + // rest concurrently as `validate_files_not_live` does. + for source in manifests { + if source.content != self.content { + out.push(source); + continue; + } + + let key = ManifestKey::new(&source); + let result = match self.filtered.get(&key) { + Some(cached) => cached.clone(), + None => { + let result = self.rewrite(ctx, &source, &to_remove).await?; + self.filtered.insert(key, result.clone()); + result + } + }; + + removed_files.extend(result.removed_files); + out.push(result.manifest); + } + + if self.fail_missing_delete_paths { + let found: HashSet = removed_files + .iter() + .map(RemovedFileIdentity::try_of) + .collect::>()?; + let mut missing: Vec = to_remove + .iter() + .filter(|f| !found.contains(f)) + .map(ToString::to_string) + .collect(); + if !missing.is_empty() { + missing.sort_unstable(); + return Err(invalid_data!( + "Cannot remove files that are not live in the table: {}", + missing.join(", ") + )); + } + } + + Ok(Filtered::builder() + .manifests(out) + .removed_files(removed_files) + .build()) + } + + /// Reads `source` and, if it references any file to remove, writes a copy + /// with those entries marked `Deleted` and the rest `Existing`. + /// + /// An entry that cannot be identified (a blob range on a non-Puffin file) + /// is invalid manifest metadata and fails the attempt. + async fn rewrite( + &self, + ctx: &mut AttemptContext<'_>, + source: &ManifestFile, + to_remove: &HashSet, + ) -> Result { + let manifest = ctx.table.object_cache().get_manifest(source).await?; + + let mut removed_files: Vec = Vec::new(); + for entry in manifest.entries() { + if entry.is_alive() && self.removes(to_remove, entry.data_file())? { + removed_files.push(entry.data_file().clone()); + } + } + if removed_files.is_empty() { + return Ok(FilteredManifest { + manifest: source.clone(), + removed_files, + }); + } + + let path = ctx.next_manifest_path()?; + + // Write under the source manifest's own schema and spec: its entries' + // partition values and column bounds were encoded for that schema's + // types, which the table's current schema may since have promoted. + let source_metadata = manifest.metadata(); + let mut writer = new_manifest_writer( + ctx.table, + path, + ctx.snapshot_id, + self.content, + source_metadata.schema().clone(), + source_metadata.partition_spec().clone().into(), + )?; + for entry in manifest.entries() { + if !entry.is_alive() { + continue; + } + let entry = (**entry).clone(); + if self.removes(to_remove, entry.data_file())? { + writer.add_delete_entry(entry)?; + } else { + writer.add_existing_entry(entry)?; + } + } + let manifest = writer.write_manifest_file().await?; + + Ok(FilteredManifest { + manifest, + removed_files, + }) + } + + fn removes(&self, to_remove: &HashSet, file: &DataFile) -> Result { + Ok(to_remove.contains(&RemovedFileIdentity::try_of(file)?) || self.is_dangling_dv(file)) + } + + fn is_dangling_dv(&self, file: &DataFile) -> bool { + file.file_format() == DataFileFormat::Puffin + && file + .referenced_data_file() + .is_some_and(|data| self.dangling_dvs_for.contains(&data)) + } +} diff --git a/crates/iceberg/src/transaction/snapshot/merging.rs b/crates/iceberg/src/transaction/snapshot/merging.rs new file mode 100644 index 0000000000..5e628cb0ed --- /dev/null +++ b/crates/iceberg/src/transaction/snapshot/merging.rs @@ -0,0 +1,1421 @@ +// 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, AttemptContext, SnapshotCommitInput, current_manifests, generate_snapshot_id, + manifest_list_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, + operation: Operation, + #[builder(default)] + snapshot_properties: HashMap, + #[builder(default)] + added_data_files: Vec, + #[builder(default)] + added_delete_files: Vec, + #[builder(default)] + removed_data_files: Vec, + #[builder(default)] + removed_delete_files: Vec, + /// 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 { + self.commit_uuid + } + + pub(crate) fn operation(&self) -> &Operation { + &self.operation + } + + pub(crate) fn snapshot_properties(&self) -> &HashMap { + &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, + /// 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, + /// Metadata files written by this producer (manifests and every attempt's + /// manifest list). + // TODO(#3417): consumed by terminal cleanup once it is wired up. + owned_artifacts: HashSet, +} + +impl MergingSnapshotProducer { + pub(crate) fn new(intent: MergingIntent) -> Self { + Self { + commit_uuid: intent.commit_uuid().unwrap_or_else(Uuid::now_v7), + data_filter: ManifestFilterManager::builder() + .content(ManifestContentType::Data) + .files_to_remove(intent.removed_data_files()) + .fail_missing_delete_paths(intent.fail_missing_delete_paths()) + .build(), + delete_filter: ManifestFilterManager::builder() + .content(ManifestContentType::Deletes) + .files_to_remove(intent.removed_delete_files()) + .fail_missing_delete_paths(intent.fail_missing_delete_paths()) + .dangling_dvs_for(intent.removed_data_files()) + .build(), + intent, + snapshot_id: None, + attempt: 0, + manifest_counter: 0, + added_manifests: None, + owned_artifacts: HashSet::new(), + } + } + + /// Produces the resulting manifest set for the current table and writes + /// this attempt's snapshot. + pub(crate) async fn apply(&mut self, table: &Table) -> Result { + let snapshot_id = self.snapshot_id(table); + // Built by hand rather than by a method on `self`, so the filters and + // the added-manifest cache stay borrowable alongside it. + let mut ctx = AttemptContext { + table, + snapshot_id, + commit_uuid: self.commit_uuid, + manifest_counter: &mut self.manifest_counter, + owned_artifacts: &mut self.owned_artifacts, + }; + + let manifests = current_manifests(table).await?; + + // Removals first: filtering only touches the current snapshot's + // manifests, never the ones this producer adds. + let data = self.data_filter.filter(&mut ctx, manifests).await?; + let (manifests, removed_data_files) = data.into_parts(); + // TODO: also drop delete files whose data sequence number is below the + // smallest `min_sequence_number` left in `manifests`, as Java's + // `dropDeleteFilesOlderThan` does; they can no longer match any row. + let deletes = self.delete_filter.filter(&mut ctx, manifests).await?; + let (manifests, removed_delete_files) = deletes.into_parts(); + // A manifest from an earlier snapshot whose entries are all `Deleted` + // has nothing left to contribute; dropping it keeps the manifest list + // from growing with every commit. This commit's own rewrites stay even + // when delete-only, since they record what it removed. + let mut manifests: Vec = manifests + .into_iter() + .filter(|m| { + m.has_added_files() || m.has_existing_files() || m.added_snapshot_id == snapshot_id + }) + .collect(); + + manifests.extend( + Self::added_manifests(&self.intent, &mut self.added_manifests, &mut ctx).await?, + ); + + let manifest_list_path = self.next_manifest_list_path(table, snapshot_id)?; + + let added_files: Vec = self + .intent + .added_data_files() + .iter() + .chain(self.intent.added_delete_files()) + .cloned() + .collect(); + + write_snapshot_commit(table, SnapshotCommitInput { + snapshot_id, + manifest_list_path, + operation: self.intent.operation().clone(), + properties: self.intent.snapshot_properties().clone(), + added_files: &added_files, + removed_files: &removed_data_files, + removed_delete_files: &removed_delete_files, + manifests, + truncate_full_table: self.intent.truncate_full_table(), + }) + .await + } + + /// Checks that every added file is written under the table's default + /// partition spec with a compatible partition value, and that every added + /// delete file is in a format the table's version allows. + /// + /// Runs against the table the manifests are written for; a later rebase + /// does not invalidate manifests already written under a spec the table + /// still has. + fn validate_added_files(intent: &MergingIntent, table: &Table) -> Result<()> { + let metadata = table.metadata(); + let format_version = metadata.format_version(); + for file in intent.added_delete_files() { + validate_delete_file_format(file, format_version)?; + } + for file in intent + .added_data_files() + .iter() + .chain(intent.added_delete_files()) + { + // TODO(#3500): accept any spec known to the table and group the + // files by spec when writing manifests. + if metadata.default_partition_spec_id() != file.partition_spec_id { + return Err(invalid_data!( + "File partition spec id does not match table default partition spec id" + )); + } + validate_partition_value(file.partition(), metadata.default_partition_type())?; + } + Ok(()) + } + + fn snapshot_id(&mut self, table: &Table) -> i64 { + *self + .snapshot_id + .get_or_insert_with(|| generate_snapshot_id(table)) + } + + fn next_manifest_list_path(&mut self, table: &Table, snapshot_id: i64) -> Result { + let path = manifest_list_path(table, snapshot_id, self.attempt, self.commit_uuid)?; + self.attempt += 1; + self.owned_artifacts.insert(path.clone()); + Ok(path) + } + + /// Returns the manifests for the added files, writing them only if no + /// reusable set exists for the current format version. Data and delete + /// files go into separate manifests. + async fn added_manifests( + intent: &MergingIntent, + cache: &mut Option, + ctx: &mut AttemptContext<'_>, + ) -> Result> { + let format_version = ctx.format_version(); + + if let Some(cached) = cache + && cached.format_version() == format_version + { + cached.check_partition_specs(ctx.table)?; + return Ok(cached.manifests().to_vec()); + } + + Self::validate_added_files(intent, ctx.table)?; + + let mut manifests = Vec::new(); + for (content, files) in [ + (ManifestContentType::Data, intent.added_data_files()), + (ManifestContentType::Deletes, intent.added_delete_files()), + ] { + if files.is_empty() { + continue; + } + // TODO(#3500): group by partition spec. TODO(#3499): roll at the + // target size and write concurrently. + let path = ctx.next_manifest_path()?; + manifests.push( + write_added_manifest( + ctx.table, + path, + ctx.snapshot_id, + content, + ctx.table.metadata().default_partition_spec_id(), + files, + ) + .await?, + ); + } + + *cache = Some( + AddedManifests::builder() + .manifests(manifests.clone()) + .format_version(format_version) + .build(), + ); + Ok(manifests) + } +} + +/// Checks that a delete file's format is allowed by the table's version: v1 +/// has no deletes, v2 has no deletion vectors, and v3 position deletes must be +/// deletion vectors. +fn validate_delete_file_format(file: &DataFile, format_version: FormatVersion) -> Result<()> { + let is_dv = file.file_format() == DataFileFormat::Puffin; + match format_version { + FormatVersion::V1 => Err(invalid_data!( + "Cannot add delete files to a format version 1 table" + )), + FormatVersion::V2 if is_dv => Err(invalid_data!( + "Cannot add deletion vector {} to a format version 2 table", + file.file_path() + )), + FormatVersion::V3 if file.content_type() == DataContentType::PositionDeletes && !is_dv => { + Err(invalid_data!( + "Position deletes must be deletion vectors in a format version 3 table: {}", + file.file_path() + )) + } + _ => Ok(()), + } +} + +#[cfg(test)] +mod tests { + use std::collections::HashMap; + + use super::{MergingIntent, MergingSnapshotProducer}; + use crate::spec::{ + DataContentType, DataFile, DataFileBuilder, DataFileFormat, Literal, ManifestContentType, + ManifestStatus, Operation, SnapshotRef, Struct, + }; + use crate::table::Table; + use crate::test_utils::{append_to, data_file}; + use crate::transaction::tests::{ + make_v1_table, make_v2_minimal_table, with_bucket_partition_spec, + }; + use crate::{ErrorKind, TableUpdate}; + + fn delete_file(table: &Table, path: &str) -> DataFile { + DataFileBuilder::default() + .content(DataContentType::PositionDeletes) + .file_path(path.to_string()) + .file_format(DataFileFormat::Parquet) + .file_size_in_bytes(50) + .record_count(1) + .partition_spec_id(table.metadata().default_partition_spec_id()) + .partition(Struct::from_iter([Some(Literal::long(300))])) + .build() + .unwrap() + } + + fn intent(operation: Operation) -> MergingIntent { + MergingIntent::builder() + .operation(operation) + .fail_missing_delete_paths(true) + .build() + } + + fn added_snapshot(commit: &mut crate::transaction::ActionCommit) -> SnapshotRef { + commit + .take_updates() + .into_iter() + .find_map(|u| match u { + TableUpdate::AddSnapshot { snapshot } => Some(SnapshotRef::new(snapshot)), + _ => None, + }) + .expect("apply should add a snapshot") + } + + /// Reads every (status, path) pair in the manifest at `manifest_path`. + async fn entries_of( + table: &Table, + manifest: &crate::spec::ManifestFile, + ) -> Vec<(ManifestStatus, String)> { + let mut entries: Vec<_> = table + .manifest_reader() + .read(manifest) + .await + .unwrap() + .entries() + .iter() + .map(|e| (e.status(), e.file_path().to_string())) + .collect(); + entries.sort_by(|a, b| a.1.cmp(&b.1)); + entries + } + + /// Removing one of two files in a manifest rewrites it with one `Deleted` + /// and one `Existing` entry; a manifest without removed files is carried + /// forward by path. + #[tokio::test] + async fn test_remove_data_file_rewrites_only_its_manifest() { + let base = make_v2_minimal_table(); + // Two commits so the current snapshot has two data manifests. + let table = append_to(&base, vec![ + data_file(&base, "test/a.parquet", 1), + data_file(&base, "test/b.parquet", 1), + ]) + .await; + let table = append_to(&table, vec![data_file(&base, "test/c.parquet", 1)]).await; + let current = table.metadata().current_snapshot().unwrap(); + let before = table.manifest_list_reader(current).load().await.unwrap(); + assert_eq!(before.entries().len(), 2); + let untouched = before + .entries() + .iter() + .find(|m| m.added_files_count == Some(1)) + .unwrap() + .manifest_path + .clone(); + + let mut i = intent(Operation::Delete); + i.removed_data_files = vec![data_file(&base, "test/a.parquet", 1)]; + let mut producer = MergingSnapshotProducer::new(i); + let mut commit = producer.apply(&table).await.unwrap(); + let snapshot = added_snapshot(&mut commit); + + let after = table.manifest_list_reader(&snapshot).load().await.unwrap(); + assert_eq!(after.entries().len(), 2); + assert!(after.entries().iter().any(|m| m.manifest_path == untouched)); + let rewritten = after + .entries() + .iter() + .find(|m| m.added_snapshot_id == snapshot.snapshot_id()) + .expect("a rewritten manifest belongs to the new snapshot"); + assert_eq!(rewritten.deleted_files_count, Some(1)); + assert_eq!(rewritten.existing_files_count, Some(1)); + assert_eq!(entries_of(&table, rewritten).await, vec![ + (ManifestStatus::Deleted, "test/a.parquet".to_string()), + (ManifestStatus::Existing, "test/b.parquet".to_string()), + ]); + + let summary = &snapshot.summary().additional_properties; + assert_eq!(summary.get("deleted-data-files").unwrap(), "1"); + assert_eq!(summary.get("total-data-files").unwrap(), "2"); + } + + /// Added data and delete files go into separate manifests of the right + /// content type, and the summary counts both. + #[tokio::test] + async fn test_add_data_and_delete_files() { + let table = make_v2_minimal_table(); + let mut i = intent(Operation::Overwrite); + i.added_data_files = vec![data_file(&table, "test/d.parquet", 3)]; + i.added_delete_files = vec![delete_file(&table, "test/d-deletes.parquet")]; + let mut producer = MergingSnapshotProducer::new(i); + let mut commit = producer.apply(&table).await.unwrap(); + let snapshot = added_snapshot(&mut commit); + + let list = table.manifest_list_reader(&snapshot).load().await.unwrap(); + let mut contents: Vec<_> = list.entries().iter().map(|m| m.content).collect(); + contents.sort_by_key(|c| *c as i32); + assert_eq!(contents, vec![ + ManifestContentType::Data, + ManifestContentType::Deletes + ]); + + let summary = &snapshot.summary().additional_properties; + assert_eq!(summary.get("added-data-files").unwrap(), "1"); + assert_eq!(summary.get("added-delete-files").unwrap(), "1"); + assert_eq!(summary.get("added-records").unwrap(), "3"); + } + + /// A retry reuses both the rewritten and the added manifests; only the + /// manifest list is written again. + #[tokio::test] + async fn test_retry_reuses_rewritten_and_added_manifests() { + let base = make_v2_minimal_table(); + let table = append_to(&base, vec![ + data_file(&base, "test/a.parquet", 1), + data_file(&base, "test/b.parquet", 1), + ]) + .await; + + let mut i = intent(Operation::Overwrite); + i.removed_data_files = vec![data_file(&base, "test/a.parquet", 1)]; + i.added_data_files = vec![data_file(&base, "test/x.parquet", 1)]; + let mut producer = MergingSnapshotProducer::new(i); + + let mut first = producer.apply(&table).await.unwrap(); + let mut second = producer.apply(&table).await.unwrap(); + let first = added_snapshot(&mut first); + let second = added_snapshot(&mut second); + + assert_eq!(first.snapshot_id(), second.snapshot_id()); + assert_ne!(first.manifest_list(), second.manifest_list()); + + let paths = |list: crate::spec::ManifestList| { + let mut p: Vec<_> = list + .entries() + .iter() + .map(|m| m.manifest_path.clone()) + .collect(); + p.sort(); + p + }; + let first_paths = paths(table.manifest_list_reader(&first).load().await.unwrap()); + let second_paths = paths(table.manifest_list_reader(&second).load().await.unwrap()); + assert_eq!(first_paths, second_paths); + // One rewrite plus one added manifest. + assert_eq!(producer.manifest_counter, 2); + assert_eq!(producer.attempt, 2); + } + + /// A retry rebased onto a table that dropped the spec the added manifest + /// was written under cannot reuse that manifest. + #[tokio::test] + async fn test_retry_rejects_removed_partition_spec() { + let base = with_bucket_partition_spec(make_v2_minimal_table()); + let mut file = data_file(&base, "test/x.parquet", 1); + file.partition = Struct::from_iter([Some(Literal::long(300)), Some(Literal::int(3))]); + + let mut i = intent(Operation::Append); + i.added_data_files = vec![file]; + let mut producer = MergingSnapshotProducer::new(i); + producer.apply(&base).await.unwrap(); + let cached = &producer.added_manifests.as_ref().unwrap().manifests()[0]; + let expected_message = format!( + "Cannot find partition spec {} for cached manifest {}", + cached.partition_spec_id, cached.manifest_path + ); + + let metadata = base + .metadata() + .clone() + .into_builder(None) + .set_default_partition_spec(0) + .unwrap() + .remove_partition_specs(&[base.metadata().default_partition_spec_id()]) + .unwrap() + .build() + .unwrap() + .metadata; + let rebased = base.with_metadata(std::sync::Arc::new(metadata)); + let err = producer + .apply(&rebased) + .await + .err() + .expect("cached manifest's spec must still exist"); + assert_eq!( + (err.kind(), err.message()), + (ErrorKind::DataInvalid, expected_message.as_str()) + ); + } + + /// A blob range belongs to a deletion vector; a Parquet delete file that + /// carries one is rejected rather than matched against nothing. + #[tokio::test] + async fn test_remove_non_puffin_file_with_blob_range_fails() { + let base = make_v2_minimal_table(); + let table = append_to(&base, vec![data_file(&base, "test/a.parquet", 1)]).await; + + let mut bad = delete_file(&table, "test/d-deletes.parquet"); + bad.content_offset = Some(4); + bad.content_size_in_bytes = Some(8); + let mut i = intent(Operation::Delete); + i.removed_delete_files = vec![bad]; + let err = MergingSnapshotProducer::new(i) + .apply(&table) + .await + .err() + .expect("a blob range on a Parquet file must be rejected"); + assert_eq!(err.kind(), ErrorKind::DataInvalid); + assert!(err.message().contains("test/d-deletes.parquet"), "{err}"); + assert!(err.message().contains("Puffin"), "{err}"); + } + + /// Removing a file that is not live anywhere fails and names the file. + #[tokio::test] + async fn test_remove_missing_file_fails() { + let base = make_v2_minimal_table(); + let table = append_to(&base, vec![data_file(&base, "test/a.parquet", 1)]).await; + + let mut i = intent(Operation::Delete); + i.removed_data_files = vec![data_file(&base, "test/nope.parquet", 1)]; + let err = MergingSnapshotProducer::new(i) + .apply(&table) + .await + .err() + .expect("removing a file that is not live must fail"); + assert_eq!(err.kind(), ErrorKind::DataInvalid); + assert!(err.message().contains("test/nope.parquet")); + } + + /// A file removed by a concurrent commit is no longer live on the rebased + /// table, so the retry fails rather than silently succeeding. + #[tokio::test] + async fn test_rebase_onto_table_without_removed_file_fails() { + let base = make_v2_minimal_table(); + let s1 = append_to(&base, vec![data_file(&base, "test/a.parquet", 1)]).await; + + let mut i = intent(Operation::Delete); + i.removed_data_files = vec![data_file(&base, "test/a.parquet", 1)]; + let mut producer = MergingSnapshotProducer::new(i); + producer.apply(&s1).await.unwrap(); + + // Concurrent writer removed `a` first; our retry rebases onto that. + let mut other = intent(Operation::Delete); + other.removed_data_files = vec![data_file(&base, "test/a.parquet", 1)]; + let mut other_commit = MergingSnapshotProducer::new(other) + .apply(&s1) + .await + .unwrap(); + let s2_snapshot = added_snapshot(&mut other_commit); + let s2 = { + use crate::spec::{MAIN_BRANCH, SnapshotReference, SnapshotRetention}; + let metadata = s1 + .metadata() + .clone() + .into_builder(None) + .add_snapshot(s2_snapshot.as_ref().clone()) + .unwrap() + .set_ref( + MAIN_BRANCH, + SnapshotReference::new( + s2_snapshot.snapshot_id(), + SnapshotRetention::branch(None, None, None), + ), + ) + .unwrap() + .build() + .unwrap() + .metadata; + s1.clone().with_metadata(std::sync::Arc::new(metadata)) + }; + + let err = producer + .apply(&s2) + .await + .err() + .expect("file already removed concurrently must not be removed twice"); + assert_eq!(err.kind(), ErrorKind::DataInvalid); + } + + /// Multi-action replay (RFC §5.2): action A's manifest keeps its path across + /// attempts, but after a rebase the manifest list assigns it a new sequence + /// number. Action B's rewrite of that manifest bakes the inherited sequence + /// numbers into its `Existing`/`Deleted` entries, so B must not reuse a + /// rewrite made under the previous sequence number. + #[tokio::test] + async fn test_rewrite_is_not_reused_when_source_manifest_sequence_number_changes() { + use super::super::simple::{AppendIntent, SimpleSnapshotProducer}; + use crate::test_utils::with_snapshot; + + let s0 = make_v2_minimal_table(); + + // Action A appends x and y; action B removes x from A's manifest. + let mut a = SimpleSnapshotProducer::new(AppendIntent { + commit_uuid: None, + snapshot_properties: HashMap::new(), + data_files: vec![ + data_file(&s0, "test/x.parquet", 1), + data_file(&s0, "test/y.parquet", 1), + ], + }); + let mut b_intent = intent(Operation::Delete); + b_intent.removed_data_files = vec![data_file(&s0, "test/x.parquet", 1)]; + let mut b = MergingSnapshotProducer::new(b_intent); + + // Attempt 0 on S0. + let sa0 = added_snapshot(&mut a.apply(&s0).await.unwrap()); + let after_a0 = with_snapshot(&s0, sa0.clone()); + let sb0 = added_snapshot(&mut b.apply(&after_a0).await.unwrap()); + + // A concurrent commit lands on S0, taking the next sequence number. The + // retry rebases onto it: A reuses its manifest but it now inherits a new + // sequence number, and B sees the same manifest path under it. + let s1 = append_to(&s0, vec![data_file(&s0, "test/other.parquet", 1)]).await; + let sa1 = added_snapshot(&mut a.apply(&s1).await.unwrap()); + let after_a1 = with_snapshot(&s1, sa1.clone()); + let sb1 = added_snapshot(&mut b.apply(&after_a1).await.unwrap()); + assert_eq!(sa1.sequence_number(), sa0.sequence_number() + 1); + + // B's rewritten manifest in each attempt. + let rewritten_in = |table: Table, snapshot: SnapshotRef| async move { + let list = table.manifest_list_reader(&snapshot).load().await.unwrap(); + let rewritten = list + .entries() + .iter() + .find(|m| m.added_snapshot_id == snapshot.snapshot_id()) + .cloned() + .unwrap(); + let manifest = table.manifest_reader().read(&rewritten).await.unwrap(); + let y = manifest + .entries() + .iter() + .find(|e| e.file_path() == "test/y.parquet") + .unwrap(); + (rewritten.manifest_path.clone(), y.sequence_number()) + }; + let (path0, y_seq0) = rewritten_in(after_a0, sb0).await; + let (path1, y_seq1) = rewritten_in(after_a1, sb1).await; + + // `y` stays live under A's snapshot, whose sequence number moved; the + // rewrite that keeps it as `Existing` must carry the new one. + assert_eq!(y_seq0, Some(sa0.sequence_number())); + assert_eq!(y_seq1, Some(sa1.sequence_number())); + assert_ne!( + path0, path1, + "a rewrite under a different sequence number is a new file" + ); + } + + /// Removing a data file from a v1 table: v1 entries read with sequence + /// number 0, which satisfies the writer, and v1 serialization drops the + /// field again. The rewritten manifest is readable and `b` stays live. + #[tokio::test] + async fn test_remove_data_file_from_v1_table() { + let base = make_v1_table(); + let table = append_to(&base, vec![ + data_file(&base, "test/a.parquet", 1), + data_file(&base, "test/b.parquet", 1), + ]) + .await; + + let mut i = intent(Operation::Delete); + i.removed_data_files = vec![data_file(&base, "test/a.parquet", 1)]; + let mut commit = MergingSnapshotProducer::new(i).apply(&table).await.unwrap(); + let snapshot = added_snapshot(&mut commit); + + let list = table.manifest_list_reader(&snapshot).load().await.unwrap(); + let rewritten = list + .entries() + .iter() + .find(|m| m.added_snapshot_id == snapshot.snapshot_id()) + .unwrap(); + let manifest = table.manifest_reader().read(rewritten).await.unwrap(); + let mut live: Vec<_> = manifest + .entries() + .iter() + .filter(|e| e.is_alive()) + .map(|e| e.file_path().to_string()) + .collect(); + live.sort(); + assert_eq!(live, vec!["test/b.parquet".to_string()]); + // Both the rewritten entries and the snapshot stay at v1's sequence number 0. + assert!( + manifest + .entries() + .iter() + .all(|e| e.sequence_number() == Some(0)) + ); + assert_eq!(snapshot.sequence_number(), 0); + assert_eq!( + snapshot + .summary() + .additional_properties + .get("deleted-data-files") + .unwrap(), + "1" + ); + } + + /// A manifest written under an earlier schema is rewritten under the + /// current one; its partition spec still binds because partition source + /// columns cannot be dropped. + #[tokio::test] + async fn test_rewrite_manifest_written_under_earlier_schema() { + use crate::spec::{NestedField, PrimitiveType, Type}; + + let base = make_v2_minimal_table(); + let table = append_to(&base, vec![ + data_file(&base, "test/a.parquet", 1), + data_file(&base, "test/b.parquet", 1), + ]) + .await; + + // Evolve the schema after the manifest was written. + let new_schema = table + .metadata() + .current_schema() + .as_ref() + .clone() + .into_builder() + .with_fields([ + NestedField::optional(100, "added", Type::Primitive(PrimitiveType::Int)).into(), + ]) + .build() + .unwrap(); + let metadata = table + .metadata() + .clone() + .into_builder(None) + .add_current_schema(new_schema) + .unwrap() + .build() + .unwrap() + .metadata; + let evolved = table.clone().with_metadata(std::sync::Arc::new(metadata)); + assert_ne!( + evolved.metadata().current_schema_id(), + table.metadata().current_schema_id() + ); + + let mut i = intent(Operation::Delete); + i.removed_data_files = vec![data_file(&base, "test/a.parquet", 1)]; + let mut commit = MergingSnapshotProducer::new(i) + .apply(&evolved) + .await + .unwrap(); + let snapshot = added_snapshot(&mut commit); + + let list = evolved + .manifest_list_reader(&snapshot) + .load() + .await + .unwrap(); + let rewritten = list + .entries() + .iter() + .find(|m| m.added_snapshot_id == snapshot.snapshot_id()) + .unwrap(); + assert_eq!( + rewritten.partition_spec_id, + table.metadata().default_partition_spec_id() + ); + assert_eq!(entries_of(&evolved, rewritten).await, vec![ + (ManifestStatus::Deleted, "test/a.parquet".to_string()), + (ManifestStatus::Existing, "test/b.parquet".to_string()), + ]); + // Partition values survive the rewrite under the new schema. + let manifest = evolved.manifest_reader().read(rewritten).await.unwrap(); + for entry in manifest.entries() { + assert_eq!( + entry.data_file().partition(), + &Struct::from_iter([Some(Literal::long(300))]) + ); + } + } + + /// A manifest's partition values are encoded for the partition type its + /// spec had when it was written. Promoting the source column (`int` to + /// `long`) changes that type under the current schema; the rewrite must + /// keep the entries readable and their partition values intact. + #[tokio::test] + async fn test_rewrite_manifest_after_partition_source_type_promotion() { + use crate::TableIdent; + use crate::io::FileIO; + use crate::spec::{ + FormatVersion, NestedField, PartitionSpec, PrimitiveType, Schema, SortOrder, + TableMetadataBuilder, Transform, Type, + }; + use crate::test_utils::test_runtime; + + let int_schema = Schema::builder() + .with_fields(vec![ + NestedField::required(1, "x", Type::Primitive(PrimitiveType::Int)).into(), + ]) + .build() + .unwrap(); + let spec = PartitionSpec::builder(int_schema.clone()) + .add_partition_field("x", "x", Transform::Identity) + .unwrap() + .build() + .unwrap(); + let metadata = TableMetadataBuilder::new( + int_schema, + spec, + SortOrder::unsorted_order(), + "memory:///table".to_string(), + FormatVersion::V2, + HashMap::new(), + ) + .unwrap() + .build() + .unwrap() + .metadata; + let base = Table::builder() + .metadata(metadata) + .metadata_location("memory:///table/metadata/v1.json") + .identifier(TableIdent::from_strs(["ns", "t"]).unwrap()) + .file_io(FileIO::new_with_memory()) + .runtime(test_runtime()) + .build() + .unwrap(); + + let int_file = |path: &str| { + DataFileBuilder::default() + .content(DataContentType::Data) + .file_path(path.to_string()) + .file_format(DataFileFormat::Parquet) + .file_size_in_bytes(100) + .record_count(1) + .partition_spec_id(base.metadata().default_partition_spec_id()) + .partition(Struct::from_iter([Some(Literal::int(7))])) + .build() + .unwrap() + }; + let table = append_to(&base, vec![ + int_file("test/a.parquet"), + int_file("test/b.parquet"), + ]) + .await; + + // Promote x: int -> long. + let long_schema = Schema::builder() + .with_schema_id(1) + .with_fields(vec![ + NestedField::required(1, "x", Type::Primitive(PrimitiveType::Long)).into(), + ]) + .build() + .unwrap(); + let metadata = table + .metadata() + .clone() + .into_builder(None) + .add_current_schema(long_schema) + .unwrap() + .build() + .unwrap() + .metadata; + let promoted = table.clone().with_metadata(std::sync::Arc::new(metadata)); + + let mut i = intent(Operation::Delete); + i.removed_data_files = vec![int_file("test/a.parquet")]; + let mut commit = MergingSnapshotProducer::new(i) + .apply(&promoted) + .await + .unwrap(); + let snapshot = added_snapshot(&mut commit); + + let list = promoted + .manifest_list_reader(&snapshot) + .load() + .await + .unwrap(); + let rewritten = list + .entries() + .iter() + .find(|m| m.added_snapshot_id == snapshot.snapshot_id()) + .unwrap(); + assert_eq!(entries_of(&promoted, rewritten).await, vec![ + (ManifestStatus::Deleted, "test/a.parquet".to_string()), + (ManifestStatus::Existing, "test/b.parquet".to_string()), + ]); + let manifest = promoted.manifest_reader().read(rewritten).await.unwrap(); + for entry in manifest.entries() { + assert_eq!( + entry.data_file().partition(), + &Struct::from_iter([Some(Literal::int(7))]) + ); + } + } + + /// On v3, existing entries keep the row ids they were assigned when first + /// committed, and the rewritten manifest gets its own range from the list + /// writer. + #[tokio::test] + async fn test_rewrite_preserves_first_row_ids_on_v3() { + use crate::transaction::tests::make_v3_minimal_table; + + let base = make_v3_minimal_table(); + let table = append_to(&base, vec![ + data_file(&base, "test/a.parquet", 5), + data_file(&base, "test/b.parquet", 7), + ]) + .await; + let before = table.metadata().current_snapshot().unwrap(); + let before_list = table.manifest_list_reader(before).load().await.unwrap(); + let before_manifest = table + .manifest_reader() + .read(&before_list.entries()[0]) + .await + .unwrap(); + let b_row_id = before_manifest + .entries() + .iter() + .find(|e| e.file_path() == "test/b.parquet") + .unwrap() + .data_file() + .first_row_id(); + assert_eq!(b_row_id, Some(5)); + + let mut i = intent(Operation::Delete); + i.removed_data_files = vec![data_file(&base, "test/a.parquet", 5)]; + let mut commit = MergingSnapshotProducer::new(i).apply(&table).await.unwrap(); + let snapshot = added_snapshot(&mut commit); + + let list = table.manifest_list_reader(&snapshot).load().await.unwrap(); + let rewritten = list + .entries() + .iter() + .find(|m| m.added_snapshot_id == snapshot.snapshot_id()) + .unwrap(); + let manifest = table.manifest_reader().read(rewritten).await.unwrap(); + let b = manifest + .entries() + .iter() + .find(|e| e.file_path() == "test/b.parquet") + .unwrap(); + assert_eq!(b.data_file().first_row_id(), b_row_id); + // The new snapshot starts its range where the table's next_row_id was. + assert_eq!( + snapshot.first_row_id(), + Some(table.metadata().next_row_id()) + ); + } + + /// Every manifest the producer writes, including rewrites, is recorded as + /// owned before the write starts. + #[tokio::test] + async fn test_owned_artifacts_cover_rewrites_and_added_manifests() { + let base = make_v2_minimal_table(); + let table = append_to(&base, vec![data_file(&base, "test/a.parquet", 1)]).await; + + let mut i = intent(Operation::Overwrite); + i.removed_data_files = vec![data_file(&base, "test/a.parquet", 1)]; + i.added_data_files = vec![data_file(&base, "test/x.parquet", 1)]; + let mut producer = MergingSnapshotProducer::new(i); + let mut commit = producer.apply(&table).await.unwrap(); + let snapshot = added_snapshot(&mut commit); + + let list = table.manifest_list_reader(&snapshot).load().await.unwrap(); + for m in list.entries() { + if m.added_snapshot_id == snapshot.snapshot_id() { + assert!(producer.owned_artifacts.contains(&m.manifest_path)); + } + } + assert!(producer.owned_artifacts.contains(snapshot.manifest_list())); + // One rewrite, one added manifest, one manifest list. + assert_eq!(producer.owned_artifacts.len(), 3); + } + + #[tokio::test] + async fn test_delete_files_rejected_on_v1_table() { + let table = make_v1_table(); + let mut i = intent(Operation::Overwrite); + i.added_delete_files = vec![delete_file(&table, "test/d-deletes.parquet")]; + let err = MergingSnapshotProducer::new(i) + .apply(&table) + .await + .err() + .expect("v1 tables cannot hold delete files"); + assert_eq!(err.kind(), ErrorKind::DataInvalid); + } + + /// A deletion vector for `data_path`: one blob of a Puffin file, located + /// by offset and size. + fn deletion_vector(table: &Table, path: &str, data_path: &str, offset: i64) -> DataFile { + DataFileBuilder::default() + .content(DataContentType::PositionDeletes) + .file_path(path.to_string()) + .file_format(DataFileFormat::Puffin) + .file_size_in_bytes(200) + .record_count(1) + .partition_spec_id(table.metadata().default_partition_spec_id()) + .partition(Struct::from_iter([Some(Literal::long(300))])) + .referenced_data_file(Some(data_path.to_string())) + .content_offset(Some(offset)) + .content_size_in_bytes(Some(10)) + .build() + .unwrap() + } + + fn equality_delete_file(table: &Table, path: &str) -> DataFile { + DataFileBuilder::default() + .content(DataContentType::EqualityDeletes) + .file_path(path.to_string()) + .file_format(DataFileFormat::Parquet) + .file_size_in_bytes(50) + .record_count(1) + .partition_spec_id(table.metadata().default_partition_spec_id()) + .partition(Struct::from_iter([Some(Literal::long(300))])) + .equality_ids(Some(vec![1])) + .build() + .unwrap() + } + + #[tokio::test] + async fn test_deletion_vector_rejected_on_v2_table() { + let table = make_v2_minimal_table(); + let mut i = intent(Operation::Overwrite); + i.added_delete_files = vec![deletion_vector( + &table, + "test/dv.puffin", + "test/d.parquet", + 4, + )]; + let err = MergingSnapshotProducer::new(i) + .apply(&table) + .await + .err() + .expect("v2 tables cannot hold deletion vectors"); + assert_eq!(err.kind(), ErrorKind::DataInvalid); + assert!(err.message().contains("test/dv.puffin")); + } + + #[tokio::test] + async fn test_position_delete_file_rejected_on_v3_table() { + use crate::transaction::tests::make_v3_minimal_table; + + let table = make_v3_minimal_table(); + let mut i = intent(Operation::Overwrite); + i.added_delete_files = vec![delete_file(&table, "test/d-deletes.parquet")]; + let err = MergingSnapshotProducer::new(i) + .apply(&table) + .await + .err() + .expect("v3 position deletes must be deletion vectors"); + assert_eq!(err.kind(), ErrorKind::DataInvalid); + assert!(err.message().contains("test/d-deletes.parquet")); + } + + /// v3 still accepts equality delete files and deletion vectors. + #[tokio::test] + async fn test_v3_accepts_equality_deletes_and_deletion_vectors() { + use crate::transaction::tests::make_v3_minimal_table; + + let table = make_v3_minimal_table(); + let mut i = intent(Operation::Overwrite); + i.added_delete_files = vec![ + equality_delete_file(&table, "test/eq.parquet"), + deletion_vector(&table, "test/dv.puffin", "test/d.parquet", 4), + ]; + let mut commit = MergingSnapshotProducer::new(i).apply(&table).await.unwrap(); + let snapshot = added_snapshot(&mut commit); + assert_eq!( + snapshot + .summary() + .additional_properties + .get("added-delete-files") + .unwrap(), + "2" + ); + } + + /// Two deletion vectors share one Puffin file. Removing one marks only + /// that blob's entry `Deleted`; the other stays `Existing`. + #[tokio::test] + async fn test_remove_one_deletion_vector_keeps_others_in_same_puffin_file() { + use crate::transaction::tests::make_v3_minimal_table; + + let base = make_v3_minimal_table(); + let dv_a = deletion_vector(&base, "test/dvs.puffin", "test/a.parquet", 4); + let dv_b = deletion_vector(&base, "test/dvs.puffin", "test/b.parquet", 14); + + let mut add = intent(Operation::Overwrite); + add.added_data_files = vec![data_file(&base, "test/a.parquet", 1)]; + add.added_delete_files = vec![dv_a.clone(), dv_b.clone()]; + let mut commit = MergingSnapshotProducer::new(add) + .apply(&base) + .await + .unwrap(); + let table = crate::test_utils::with_snapshot(&base, added_snapshot(&mut commit)); + + let mut remove = intent(Operation::Delete); + remove.removed_delete_files = vec![dv_a.clone()]; + let mut commit = MergingSnapshotProducer::new(remove) + .apply(&table) + .await + .unwrap(); + let snapshot = added_snapshot(&mut commit); + + let list = table.manifest_list_reader(&snapshot).load().await.unwrap(); + let rewritten = list + .entries() + .iter() + .find(|m| { + m.added_snapshot_id == snapshot.snapshot_id() + && m.content == ManifestContentType::Deletes + }) + .expect("the delete manifest is rewritten"); + let manifest = table.manifest_reader().read(rewritten).await.unwrap(); + let mut entries: Vec<_> = manifest + .entries() + .iter() + .map(|e| (e.status(), e.data_file().content_offset())) + .collect(); + entries.sort_by_key(|(_, offset)| *offset); + assert_eq!(entries, vec![ + (ManifestStatus::Deleted, dv_a.content_offset()), + (ManifestStatus::Existing, dv_b.content_offset()), + ]); + let summary = &snapshot.summary().additional_properties; + assert_eq!(summary.get("removed-delete-files").unwrap(), "1"); + assert_eq!(summary.get("total-delete-files").unwrap(), "1"); + } + + /// Removing a data file on v3 also marks its deletion vector `Deleted`, + /// even though the intent names no delete files. A DV for another data + /// file in the same Puffin file stays `Existing`, and a removed data file + /// without a DV is fine under `fail_missing_delete_paths`. + #[tokio::test] + async fn test_remove_data_file_also_removes_its_deletion_vector() { + use crate::test_utils::with_snapshot; + use crate::transaction::tests::make_v3_minimal_table; + + let base = make_v3_minimal_table(); + let dv_a = deletion_vector(&base, "test/dvs.puffin", "test/a.parquet", 4); + let dv_b = deletion_vector(&base, "test/dvs.puffin", "test/b.parquet", 14); + + let mut add = intent(Operation::Overwrite); + add.added_data_files = vec![ + data_file(&base, "test/a.parquet", 1), + data_file(&base, "test/b.parquet", 1), + data_file(&base, "test/c.parquet", 1), + ]; + add.added_delete_files = vec![dv_a.clone(), dv_b.clone()]; + let mut commit = MergingSnapshotProducer::new(add) + .apply(&base) + .await + .unwrap(); + let table = with_snapshot(&base, added_snapshot(&mut commit)); + + let mut remove = intent(Operation::Delete); + remove.removed_data_files = vec![ + data_file(&base, "test/a.parquet", 1), + data_file(&base, "test/c.parquet", 1), + ]; + let mut commit = MergingSnapshotProducer::new(remove) + .apply(&table) + .await + .unwrap(); + let snapshot = added_snapshot(&mut commit); + + let list = table.manifest_list_reader(&snapshot).load().await.unwrap(); + let rewritten_deletes = list + .entries() + .iter() + .find(|m| { + m.added_snapshot_id == snapshot.snapshot_id() + && m.content == ManifestContentType::Deletes + }) + .expect("the delete manifest is rewritten for the dangling DV"); + let manifest = table + .manifest_reader() + .read(rewritten_deletes) + .await + .unwrap(); + let mut entries: Vec<_> = manifest + .entries() + .iter() + .map(|e| (e.status(), e.data_file().referenced_data_file())) + .collect(); + entries.sort_by_key(|(_, data)| data.clone()); + assert_eq!(entries, vec![ + (ManifestStatus::Deleted, Some("test/a.parquet".to_string())), + (ManifestStatus::Existing, Some("test/b.parquet".to_string())), + ]); + + let summary = &snapshot.summary().additional_properties; + assert_eq!(summary.get("deleted-data-files").unwrap(), "2"); + assert_eq!(summary.get("removed-delete-files").unwrap(), "1"); + assert_eq!(summary.get("total-data-files").unwrap(), "1"); + assert_eq!(summary.get("total-delete-files").unwrap(), "1"); + } + + /// With missing-file failures off, a file removed by a concurrent commit + /// is skipped and the attempt commits; nothing is rewritten for it. + #[tokio::test] + async fn test_missing_removed_file_is_skipped_when_not_failing() { + let base = make_v2_minimal_table(); + let table = append_to(&base, vec![data_file(&base, "test/a.parquet", 1)]).await; + + let mut i = intent(Operation::Delete); + i.removed_data_files = vec![data_file(&base, "test/nope.parquet", 1)]; + i.fail_missing_delete_paths = false; + let mut producer = MergingSnapshotProducer::new(i); + let mut commit = producer.apply(&table).await.unwrap(); + let snapshot = added_snapshot(&mut commit); + + let list = table.manifest_list_reader(&snapshot).load().await.unwrap(); + assert!( + list.entries() + .iter() + .all(|m| m.added_snapshot_id != snapshot.snapshot_id()) + ); + assert_eq!(producer.manifest_counter, 0); + let summary = &snapshot.summary().additional_properties; + assert_eq!(summary.get("total-data-files").unwrap(), "1"); + assert!(!summary.contains_key("deleted-data-files")); + } + + /// A concurrent format upgrade invalidates both caches: the rewrite, whose + /// entries were written without v2 sequence numbers, and the added + /// manifest, whose entries must now inherit one. + #[tokio::test] + async fn test_rewrite_and_added_manifest_are_redone_when_format_version_changes() { + use crate::spec::FormatVersion; + + let base = make_v1_table(); + let table = append_to(&base, vec![ + data_file(&base, "test/a.parquet", 1), + data_file(&base, "test/b.parquet", 1), + ]) + .await; + + let mut i = intent(Operation::Overwrite); + i.removed_data_files = vec![data_file(&base, "test/a.parquet", 1)]; + i.added_data_files = vec![data_file(&base, "test/x.parquet", 1)]; + let mut producer = MergingSnapshotProducer::new(i); + let mut first = producer.apply(&table).await.unwrap(); + let first = added_snapshot(&mut first); + assert_eq!(producer.manifest_counter, 2); + + let metadata = table + .metadata() + .clone() + .into_builder(None) + .upgrade_format_version(FormatVersion::V2) + .unwrap() + .build() + .unwrap() + .metadata; + let upgraded = table.clone().with_metadata(std::sync::Arc::new(metadata)); + + let mut second = producer.apply(&upgraded).await.unwrap(); + let second = added_snapshot(&mut second); + assert_eq!(producer.manifest_counter, 4); + + let own_paths = |list: crate::spec::ManifestList, snapshot_id: i64| { + let mut p: Vec<_> = list + .entries() + .iter() + .filter(|m| m.added_snapshot_id == snapshot_id) + .map(|m| m.manifest_path.clone()) + .collect(); + p.sort(); + p + }; + let first_list = table.manifest_list_reader(&first).load().await.unwrap(); + let second_list = upgraded.manifest_list_reader(&second).load().await.unwrap(); + let first_paths = own_paths(first_list, first.snapshot_id()); + let second_paths = own_paths(second_list.clone(), second.snapshot_id()); + assert_eq!(first_paths.len(), 2); + assert_eq!(second_paths.len(), 2); + assert!(first_paths.iter().all(|p| !second_paths.contains(p))); + + // Under v2 the added entry inherits the new sequence number, while the + // rewritten entries keep the v1 data's sequence number 0. + for m in second_list + .entries() + .iter() + .filter(|m| m.added_snapshot_id == second.snapshot_id()) + { + let manifest = upgraded.manifest_reader().read(m).await.unwrap(); + for entry in manifest.entries() { + let expected = if entry.file_path() == "test/x.parquet" { + second.sequence_number() + } else { + 0 + }; + assert_eq!( + entry.sequence_number(), + Some(expected), + "{}", + entry.file_path() + ); + } + } + } + + /// A manifest left with only `Deleted` entries by an earlier snapshot is + /// dropped by the next commit; the one this commit writes is kept. + #[tokio::test] + async fn test_delete_only_manifest_from_earlier_snapshot_is_dropped() { + use crate::test_utils::with_snapshot; + + let base = make_v2_minimal_table(); + let table = append_to(&base, vec![data_file(&base, "test/a.parquet", 1)]).await; + + let mut remove = intent(Operation::Delete); + remove.removed_data_files = vec![data_file(&base, "test/a.parquet", 1)]; + let mut commit = MergingSnapshotProducer::new(remove) + .apply(&table) + .await + .unwrap(); + let s1 = added_snapshot(&mut commit); + let s1_list = table.manifest_list_reader(&s1).load().await.unwrap(); + assert_eq!(s1_list.entries().len(), 1); + assert_eq!(s1_list.entries()[0].deleted_files_count, Some(1)); + assert_eq!(s1_list.entries()[0].existing_files_count, Some(0)); + let table = with_snapshot(&table, s1); + + let mut add = intent(Operation::Append); + add.added_data_files = vec![data_file(&base, "test/b.parquet", 1)]; + let mut commit = MergingSnapshotProducer::new(add) + .apply(&table) + .await + .unwrap(); + let s2 = added_snapshot(&mut commit); + let s2_list = table.manifest_list_reader(&s2).load().await.unwrap(); + assert_eq!(s2_list.entries().len(), 1); + assert_eq!(s2_list.entries()[0].added_snapshot_id, s2.snapshot_id()); + assert_eq!( + s2.summary() + .additional_properties + .get("total-data-files") + .unwrap(), + "1" + ); + } +} diff --git a/crates/iceberg/src/transaction/snapshot/mod.rs b/crates/iceberg/src/transaction/snapshot/mod.rs index bd6c1276ad..0633d3634a 100644 --- a/crates/iceberg/src/transaction/snapshot/mod.rs +++ b/crates/iceberg/src/transaction/snapshot/mod.rs @@ -23,20 +23,28 @@ //! Iceberg metadata and files, never select operation policy, and never cache //! across calls. +// TODO(#1607): remove the allows once `RowDeltaAction` consumes the merging +// producer. +#[allow(dead_code)] +pub(crate) mod manifest_filter; +#[allow(dead_code)] +pub(crate) mod merging; pub(crate) mod simple; use std::collections::{HashMap, HashSet}; use futures::TryStreamExt; use futures::stream::FuturesUnordered; +use typed_builder::TypedBuilder; use uuid::Uuid; use crate::error::{Result, invalid_data}; use crate::spec::{ - DataFile, DataFileFormat, FormatVersion, MAIN_BRANCH, ManifestContentType, ManifestFile, - ManifestListWriter, ManifestWriter, ManifestWriterBuilder, Operation, PartitionSpecRef, - SchemaRef, Snapshot, SnapshotReference, SnapshotRetention, SnapshotSummaryCollector, Struct, - StructType, Summary, TableProperties, set_iceberg_version, update_snapshot_summaries, + DataFile, DataFileFormat, FormatVersion, MAIN_BRANCH, ManifestContentType, ManifestEntry, + ManifestFile, ManifestListWriter, ManifestStatus, ManifestWriter, ManifestWriterBuilder, + Operation, PartitionSpecRef, SchemaRef, Snapshot, SnapshotReference, SnapshotRetention, + SnapshotSummaryCollector, Struct, StructType, Summary, TableProperties, set_iceberg_version, + update_snapshot_summaries, }; use crate::table::Table; use crate::transaction::ActionCommit; @@ -173,6 +181,126 @@ pub(crate) async fn current_manifests(table: &Table) -> Result .collect()) } +/// What one attempt writes metadata files against: the table the attempt +/// commits to, the identity every path carries, and the counters that keep +/// paths unique across attempts. +/// +/// Borrowed from the producer for one `apply`, so a helper cannot hold a stale +/// table across attempts, and every file written is recorded as owned by the +/// helper that allocates its path. +pub(crate) struct AttemptContext<'a> { + pub(crate) table: &'a Table, + pub(crate) snapshot_id: i64, + pub(crate) commit_uuid: Uuid, + /// Numbers every manifest the producer writes. Never reset, so paths never + /// collide across attempts. + pub(crate) manifest_counter: &'a mut u64, + /// Metadata files the producer wrote; consumed by terminal cleanup. + pub(crate) owned_artifacts: &'a mut HashSet, +} + +impl AttemptContext<'_> { + pub(crate) fn format_version(&self) -> FormatVersion { + self.table.metadata().format_version() + } + + /// Allocates the next manifest path and records it as owned before + /// anything is written, so a partial write is still cleaned up. + pub(crate) fn next_manifest_path(&mut self) -> Result { + let path = manifest_path(self.table, self.commit_uuid, *self.manifest_counter)?; + *self.manifest_counter += 1; + self.owned_artifacts.insert(path.clone()); + Ok(path) + } +} + +/// The manifests a producer wrote for one execution's added files. +/// +/// Reusable while the table's format version is unchanged. Per spec, sequence +/// number inheritance does not apply to v1 manifests, so one written before a +/// concurrent upgrade cannot carry the v2 sequence number a retry assigns and +/// must be rewritten. The schema is not a guard: the manifest's embedded schema +/// describes the files it was written for, which do not change. Nor is the +/// default partition spec: the files name their own spec, which stays valid as +/// long as the table still has it; see [`Self::check_partition_specs`]. +#[derive(TypedBuilder)] +pub(crate) struct AddedManifests { + /// One per content type today. Several once added files are grouped by + /// partition spec (#3500) or rolled over at a target size (#3499). + manifests: Vec, + format_version: FormatVersion, +} + +impl AddedManifests { + pub(crate) fn manifests(&self) -> &[ManifestFile] { + &self.manifests + } + + pub(crate) fn format_version(&self) -> FormatVersion { + self.format_version + } + + /// Fails if a partition spec these manifests were written under is no + /// longer in `table`: a concurrent commit removed it, so a retry rebased + /// onto that table cannot carry them. + pub(crate) fn check_partition_specs(&self, table: &Table) -> Result<()> { + for manifest in &self.manifests { + if table + .metadata() + .partition_spec_by_id(manifest.partition_spec_id) + .is_none() + { + return Err(invalid_data!( + "Cannot find partition spec {} for cached manifest {}", + manifest.partition_spec_id, + manifest.manifest_path + )); + } + } + Ok(()) + } +} + +/// Writes one manifest at `path` holding `files` as `Added` entries, all of +/// which must belong to `partition_spec_id`, and returns its manifest-list entry. +pub(crate) async fn write_added_manifest<'a>( + table: &Table, + path: String, + snapshot_id: i64, + content: ManifestContentType, + partition_spec_id: i32, + files: impl IntoIterator, +) -> Result { + let metadata = table.metadata(); + let partition_spec = metadata + .partition_spec_by_id(partition_spec_id) + .ok_or_else(|| invalid_data!("Unknown partition spec id {partition_spec_id}"))?; + + let mut writer = new_manifest_writer( + table, + path, + snapshot_id, + content, + metadata.current_schema().clone(), + partition_spec.clone(), + )?; + let format_version = metadata.format_version(); + for file in files { + let builder = ManifestEntry::builder() + .status(ManifestStatus::Added) + .data_file(file.clone()); + let entry = if format_version == FormatVersion::V1 { + builder.snapshot_id(snapshot_id).build() + } else { + // For format version > 1, the snapshot id is inherited from the + // manifest list, so a rebase does not require rewriting the manifest. + builder.build() + }; + writer.add_entry(entry)?; + } + writer.write_manifest_file().await +} + /// Fails if any of `data_files` is already live in the table's current snapshot. /// /// Must run against each attempt's base, since a concurrent commit may have @@ -238,6 +366,7 @@ fn build_summary( mut properties: HashMap, added_files: &[DataFile], removed_files: &[DataFile], + removed_delete_files: &[DataFile], truncate_full_table: bool, ) -> Result { let metadata = table.metadata(); @@ -265,7 +394,7 @@ fn build_summary( } let mut schemas_by_spec = HashMap::new(); - for data_file in removed_files { + for data_file in removed_files.iter().chain(removed_delete_files) { let partition_spec = metadata .partition_spec_by_id(data_file.partition_spec_id) .ok_or_else(|| { @@ -286,7 +415,16 @@ fn build_summary( .unwrap_or(metadata.current_schema()) .clone() }); - collector.remove_file(data_file, schema.clone(), partition_spec.clone()); + // The partition value keeps the literal type it was written with; the + // summary key renders it under the type the schema declares now. + let partition = partition_spec.resolve(schema, data_file.partition().clone())?; + if &partition == data_file.partition() { + collector.remove_file(data_file, schema.clone(), partition_spec.clone()); + } else { + let mut resolved = data_file.clone(); + resolved.partition = partition; + collector.remove_file(&resolved, schema.clone(), partition_spec.clone()); + } } properties.extend(collector.build()); @@ -314,9 +452,12 @@ pub(crate) struct SnapshotCommitInput<'a> { pub manifest_list_path: String, pub operation: Operation, pub properties: HashMap, + /// Data and delete files added by this commit. pub added_files: &'a [DataFile], /// Must match the data files marked Deleted by this commit, including their metrics. pub removed_files: &'a [DataFile], + /// Must match the delete files marked Deleted by this commit, including their metrics. + pub removed_delete_files: &'a [DataFile], pub manifests: Vec, /// Whether the summary resets table totals (a full-table overwrite). pub truncate_full_table: bool, @@ -335,20 +476,29 @@ pub(crate) async fn write_snapshot_commit( properties, added_files, removed_files, + removed_delete_files, manifests, truncate_full_table, } = input; - debug_assert_eq!( + let deleted_count = |content: ManifestContentType| { manifests .iter() - .filter(|m| m.added_snapshot_id == snapshot_id && m.content == ManifestContentType::Data) + .filter(|m| m.added_snapshot_id == snapshot_id && m.content == content) .filter_map(|m| m.deleted_files_count) .map(u64::from) - .sum::(), + .sum::() + }; + debug_assert_eq!( + deleted_count(ManifestContentType::Data), removed_files.len() as u64, "deleted files must match this commit's manifest entries" ); + debug_assert_eq!( + deleted_count(ManifestContentType::Deletes), + removed_delete_files.len() as u64, + "deleted delete files must match this commit's manifest entries" + ); let metadata = table.metadata(); let parent_snapshot_id = metadata.current_snapshot_id(); @@ -361,6 +511,7 @@ pub(crate) async fn write_snapshot_commit( properties, added_files, removed_files, + removed_delete_files, truncate_full_table, ) .map_err(|err| { @@ -472,45 +623,19 @@ mod tests { use uuid::Uuid; - use super::simple::{AppendIntent, SimpleSnapshotProducer}; use super::*; use crate::ErrorKind; use crate::spec::{ DataContentType, DataFileBuilder, Literal, Manifest, NestedField, PartitionSpec, PrimitiveType, Schema, SortOrder, TableMetadataBuilder, Transform, Type, }; - use crate::transaction::Transaction; + use crate::test_utils::{append_to, data_file}; use crate::transaction::tests::make_v2_minimal_table; - fn data_file(table: &Table, path: &str, record_count: u64) -> DataFile { - DataFileBuilder::default() - .content(DataContentType::Data) - .file_path(path.to_string()) - .file_format(DataFileFormat::Parquet) - .file_size_in_bytes(100) - .record_count(record_count) - .partition_spec_id(table.metadata().default_partition_spec_id()) - .partition(Struct::from_iter([Some(Literal::long(300))])) - .build() - .unwrap() - } - fn deleted_file(table: &Table) -> DataFile { data_file(table, "test/deleted.parquet", 2) } - /// Commits `data_files` to `table` through a fresh producer, returning the - /// table with that snapshot as current. - async fn append_to(table: &Table, data_files: Vec) -> Table { - let mut producer = SimpleSnapshotProducer::new(AppendIntent { - commit_uuid: None, - snapshot_properties: HashMap::new(), - data_files, - }); - let updates = producer.apply(table).await.unwrap().take_updates(); - Transaction::update_table_metadata(table.clone(), &updates).unwrap() - } - #[tokio::test] async fn test_commit_keeps_main_branch_retention() { let table = make_v2_minimal_table(); @@ -587,6 +712,7 @@ mod tests { HashMap::new(), &[], &[deleted], + &[], false, ) .unwrap(); @@ -597,6 +723,70 @@ mod tests { assert_eq!(properties.get("removed-files-size").unwrap(), "100"); } + /// A partition value written before its source column was promoted keeps + /// the old literal type. The summary key renders it under the promoted + /// type: a `float` widened to `double` prints as Java does. + #[test] + fn test_summary_renders_partition_value_under_promoted_type() { + let f = |ty| Arc::new(NestedField::required(1, "f", Type::Primitive(ty))); + let schema = |fields: Vec<_>| Schema::builder().with_fields(fields).build().unwrap(); + let base = schema(vec![f(PrimitiveType::Float)]); + let spec = PartitionSpec::builder(base.clone()) + .add_partition_field("f", "f", Transform::Identity) + .unwrap() + .build() + .unwrap(); + let table = make_v2_minimal_table(); + let metadata = TableMetadataBuilder::new( + base, + spec, + SortOrder::unsorted_order(), + table.metadata().location().to_string(), + FormatVersion::V2, + HashMap::from([( + TableProperties::PROPERTY_WRITE_PARTITION_SUMMARY_LIMIT.to_string(), + "10".to_string(), + )]), + ) + .unwrap() + .add_current_schema(schema(vec![f(PrimitiveType::Double)])) + .unwrap() + .build() + .unwrap() + .metadata; + let table = table.with_metadata(Arc::new(metadata)); + let deleted = DataFileBuilder::default() + .content(DataContentType::Data) + .file_path("test/deleted.parquet".to_string()) + .file_format(DataFileFormat::Parquet) + .file_size_in_bytes(100) + .record_count(2) + .partition_spec_id(0) + .partition(Struct::from_iter([Some(Literal::float(0.1_f32))])) + .build() + .unwrap(); + + let summary = build_summary( + &table, + Operation::Delete, + HashMap::new(), + &[], + &[deleted], + &[], + false, + ) + .unwrap(); + + // Not exact in f32: the widened double is rendered, as Java does. + assert!( + summary + .additional_properties + .contains_key("partitions.f=0.10000000149011612"), + "{:?}", + summary.additional_properties + ); + } + #[test] fn test_summary_keeps_decimal_scale_of_dropped_partition_source() { let decimal = PrimitiveType::Decimal { @@ -657,6 +847,7 @@ mod tests { HashMap::new(), &[], &[deleted], + &[], false, ) .unwrap(); @@ -683,6 +874,7 @@ mod tests { properties: HashMap::new(), added_files: &[], removed_files: &[deleted], + removed_delete_files: &[], manifests: vec![], truncate_full_table: false, }) diff --git a/crates/iceberg/src/transaction/snapshot/simple.rs b/crates/iceberg/src/transaction/snapshot/simple.rs index 91766e1a87..a601e8dfac 100644 --- a/crates/iceberg/src/transaction/snapshot/simple.rs +++ b/crates/iceberg/src/transaction/snapshot/simple.rs @@ -22,14 +22,11 @@ use std::collections::{BTreeMap, HashMap, HashSet}; use uuid::Uuid; use super::{ - SnapshotCommitInput, current_manifests, generate_snapshot_id, manifest_list_path, - manifest_path, new_manifest_writer, validate_partition_value, write_snapshot_commit, + AddedManifests, AttemptContext, SnapshotCommitInput, current_manifests, generate_snapshot_id, + manifest_list_path, validate_partition_value, write_added_manifest, write_snapshot_commit, }; use crate::error::{Result, invalid_data}; -use crate::spec::{ - DataFile, FormatVersion, ManifestContentType, ManifestEntry, ManifestFile, ManifestStatus, - Operation, PartitionSpecRef, -}; +use crate::spec::{DataFile, ManifestContentType, ManifestFile, Operation, PartitionSpecRef}; use crate::table::Table; use crate::transaction::ActionCommit; @@ -76,18 +73,6 @@ pub(crate) struct SimpleSnapshotProducer { owned_artifacts: HashSet, } -/// The manifests written for one append's added files. -/// -/// Reusable while the table's format version is unchanged. Per spec, sequence -/// number inheritance does not apply to v1 manifests, so one written before a -/// concurrent upgrade cannot carry the v2 sequence number a retry assigns and -/// must be rewritten. The schema is not a guard: the manifest's embedded schema -/// describes the files it was written for, which do not change. -struct AddedManifests { - manifests: Vec, - format_version: FormatVersion, -} - struct DataFileGroup<'a> { partition_spec: PartitionSpecRef, data_files: Vec<&'a DataFile>, @@ -113,7 +98,17 @@ impl SimpleSnapshotProducer { let mut manifests = current_manifests(table).await?; if !self.intent.data_files.is_empty() { - manifests.extend(self.added_manifests(table, snapshot_id).await?); + let mut ctx = AttemptContext { + table, + snapshot_id, + commit_uuid: self.commit_uuid, + manifest_counter: &mut self.manifest_counter, + owned_artifacts: &mut self.owned_artifacts, + }; + manifests.extend( + Self::added_manifests(&self.intent.data_files, &mut self.added_manifests, &mut ctx) + .await?, + ); } let manifest_list_path = self.next_manifest_list_path(table, snapshot_id)?; @@ -125,6 +120,7 @@ impl SimpleSnapshotProducer { properties: self.intent.snapshot_properties.clone(), added_files: &self.intent.data_files, removed_files: &[], + removed_delete_files: &[], manifests, truncate_full_table: false, }) @@ -147,57 +143,45 @@ impl SimpleSnapshotProducer { /// Returns the manifests for the added files, writing them only if no /// reusable set exists for the current format version. async fn added_manifests( - &mut self, - table: &Table, - snapshot_id: i64, + data_files: &[DataFile], + cache: &mut Option, + ctx: &mut AttemptContext<'_>, ) -> Result> { - let format_version = table.metadata().format_version(); + let format_version = ctx.format_version(); - if let Some(cached) = &self.added_manifests - && cached.format_version == format_version + if let Some(cached) = cache + && cached.format_version() == format_version { - for manifest in &cached.manifests { - if table - .metadata() - .partition_spec_by_id(manifest.partition_spec_id) - .is_none() - { - return Err(invalid_data!( - "Cannot find partition spec {} for cached manifest {}", - manifest.partition_spec_id, - manifest.manifest_path - )); - } - } - return Ok(cached.manifests.clone()); + cached.check_partition_specs(ctx.table)?; + return Ok(cached.manifests().to_vec()); } - let data_files_by_spec = validated_added_data_files(table, &self.intent.data_files)?; + let data_files_by_spec = validated_added_data_files(ctx.table, data_files)?; // TODO(#3499): roll a group over at the target manifest size, and // write the resulting manifests concurrently. let mut manifests = Vec::with_capacity(data_files_by_spec.len()); for group in data_files_by_spec { - let path = manifest_path(table, self.commit_uuid, self.manifest_counter)?; - self.manifest_counter += 1; - // Record ownership before writing: a partial file is an orphan to clean up too. - self.owned_artifacts.insert(path.clone()); + let path = ctx.next_manifest_path()?; manifests.push( write_added_manifest( - table, + ctx.table, path, - snapshot_id, - group.partition_spec, - &group.data_files, + ctx.snapshot_id, + ManifestContentType::Data, + group.partition_spec.spec_id(), + group.data_files.iter().copied(), ) .await?, ); } - self.added_manifests = Some(AddedManifests { - manifests: manifests.clone(), - format_version, - }); + *cache = Some( + AddedManifests::builder() + .manifests(manifests.clone()) + .format_version(format_version) + .build(), + ); Ok(manifests) } } @@ -243,41 +227,6 @@ fn validated_added_data_files<'a>( .collect() } -/// Writes one data manifest at `path` for `data_files`, all of which must -/// belong to `partition_spec`, and returns its manifest-list entry. -async fn write_added_manifest( - table: &Table, - path: String, - snapshot_id: i64, - partition_spec: PartitionSpecRef, - data_files: &[&DataFile], -) -> Result { - let metadata = table.metadata(); - let mut writer = new_manifest_writer( - table, - path, - snapshot_id, - ManifestContentType::Data, - metadata.current_schema().clone(), - partition_spec, - )?; - let format_version = metadata.format_version(); - for data_file in data_files { - let builder = ManifestEntry::builder() - .status(ManifestStatus::Added) - .data_file((*data_file).clone()); - let entry = if format_version == FormatVersion::V1 { - builder.snapshot_id(snapshot_id).build() - } else { - // For format version > 1, the snapshot id is inherited from the - // manifest list, so a rebase does not require rewriting the manifest. - builder.build() - }; - writer.add_entry(entry)?; - } - writer.write_manifest_file().await -} - #[cfg(test)] mod tests { use std::collections::HashMap;