|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +use std::collections::HashSet; |
| 19 | +use std::sync::Arc; |
| 20 | + |
| 21 | +use crate::spec::{ManifestContentType, ManifestFile, Operation, SnapshotRef, TableMetadata}; |
| 22 | +use crate::table::Table; |
| 23 | + |
| 24 | +pub(crate) trait SnapshotValidator { |
| 25 | + fn validate(&self, table: &Table, snapshot: Option<&SnapshotRef>) -> () {} |
| 26 | + |
| 27 | + #[allow(dead_code)] |
| 28 | + async fn validation_history( |
| 29 | + &self, |
| 30 | + base: &Table, |
| 31 | + to_snapshot: Option<&SnapshotRef>, |
| 32 | + from_snapshot: Option<&SnapshotRef>, |
| 33 | + matching_operations: HashSet<Operation>, |
| 34 | + manifest_content_type: ManifestContentType, |
| 35 | + ) -> (Vec<ManifestFile>, HashSet<i64>) { |
| 36 | + let mut manifests = vec![]; |
| 37 | + let mut new_snapshots = HashSet::new(); |
| 38 | + let mut last_snapshot: Option<&SnapshotRef> = None; |
| 39 | + |
| 40 | + let snapshots = Self::ancestors_between(to_snapshot, from_snapshot, base.metadata()); |
| 41 | + for current_snapshot in &snapshots { |
| 42 | + last_snapshot = Some(current_snapshot); |
| 43 | + |
| 44 | + if matching_operations.contains(¤t_snapshot.summary().operation) { |
| 45 | + new_snapshots.insert(current_snapshot.snapshot_id().clone()); |
| 46 | + current_snapshot |
| 47 | + .load_manifest_list(base.file_io(), base.metadata()) |
| 48 | + .await |
| 49 | + .expect("Failed to load manifest list!") |
| 50 | + .entries() |
| 51 | + .into_iter() |
| 52 | + .for_each(|manifest| { |
| 53 | + if manifest.content == manifest_content_type |
| 54 | + && manifest.added_snapshot_id == current_snapshot.snapshot_id() |
| 55 | + { |
| 56 | + manifests.push(manifest.clone()); |
| 57 | + } |
| 58 | + }); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + if last_snapshot.is_some() |
| 63 | + && last_snapshot.unwrap().parent_snapshot_id() |
| 64 | + != from_snapshot.map(|snapshot| snapshot.snapshot_id()) |
| 65 | + { |
| 66 | + panic!("Cannot determine history between starting snapshot {} and the last known ancestor {}", |
| 67 | + from_snapshot.map_or_else( |
| 68 | + || "None".to_string(), |
| 69 | + |snapshot| snapshot.snapshot_id().to_string()), |
| 70 | + last_snapshot.map_or_else( |
| 71 | + || "None".to_string(), |
| 72 | + |snapshot| snapshot.parent_snapshot_id().unwrap().to_string())); |
| 73 | + } |
| 74 | + |
| 75 | + (manifests, new_snapshots) |
| 76 | + } |
| 77 | + |
| 78 | + fn ancestors_between( |
| 79 | + to_snapshot: Option<&SnapshotRef>, |
| 80 | + from_snapshot: Option<&SnapshotRef>, |
| 81 | + table_metadata: &TableMetadata, |
| 82 | + ) -> Vec<SnapshotRef> { |
| 83 | + let mut snapshots = Vec::new(); |
| 84 | + let mut current_snapshot = to_snapshot; |
| 85 | + while let Some(snapshot) = current_snapshot { |
| 86 | + snapshots.push(Arc::clone(&snapshot)); |
| 87 | + match snapshot.parent_snapshot_id() { |
| 88 | + Some(parent_snapshot_id) |
| 89 | + if from_snapshot.is_some() |
| 90 | + && parent_snapshot_id == from_snapshot.unwrap().snapshot_id() => |
| 91 | + { |
| 92 | + break |
| 93 | + } |
| 94 | + Some(parent_snapshot_id) => { |
| 95 | + current_snapshot = table_metadata.snapshot_by_id(parent_snapshot_id) |
| 96 | + } |
| 97 | + None => break, |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + snapshots |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +#[cfg(test)] |
| 106 | +mod tests { |
| 107 | + use std::collections::HashSet; |
| 108 | + |
| 109 | + use crate::spec::{ |
| 110 | + DataContentType, DataFileBuilder, DataFileFormat, Literal, ManifestContentType, Operation, |
| 111 | + SnapshotRef, Struct, |
| 112 | + }; |
| 113 | + use crate::transaction::tests::{make_v2_minimal_table, make_v2_table}; |
| 114 | + use crate::transaction::validate::SnapshotValidator; |
| 115 | + use crate::transaction::{Table, Transaction}; |
| 116 | + use crate::TableUpdate; |
| 117 | + |
| 118 | + struct TestValidator {} |
| 119 | + |
| 120 | + impl SnapshotValidator for TestValidator {} |
| 121 | + |
| 122 | + async fn make_v2_table_with_updates() -> (Table, Vec<TableUpdate>) { |
| 123 | + let table = make_v2_minimal_table(); |
| 124 | + let tx = Transaction::new(&table); |
| 125 | + let mut action = tx.fast_append(None, vec![]).unwrap(); |
| 126 | + |
| 127 | + let data_file_1 = DataFileBuilder::default() |
| 128 | + .content(DataContentType::Data) |
| 129 | + .file_path("test/1.parquet".to_string()) |
| 130 | + .file_format(DataFileFormat::Parquet) |
| 131 | + .file_size_in_bytes(100) |
| 132 | + .record_count(1) |
| 133 | + .partition_spec_id(table.metadata().default_partition_spec_id()) |
| 134 | + .partition(Struct::from_iter([Some(Literal::long(300))])) |
| 135 | + .build() |
| 136 | + .unwrap(); |
| 137 | + |
| 138 | + let data_file_2 = DataFileBuilder::default() |
| 139 | + .content(DataContentType::Data) |
| 140 | + .file_path("test/2.parquet".to_string()) |
| 141 | + .file_format(DataFileFormat::Parquet) |
| 142 | + .file_size_in_bytes(100) |
| 143 | + .record_count(1) |
| 144 | + .partition_spec_id(table.metadata().default_partition_spec_id()) |
| 145 | + .partition(Struct::from_iter([Some(Literal::long(300))])) |
| 146 | + .build() |
| 147 | + .unwrap(); |
| 148 | + |
| 149 | + action.add_data_files(vec![data_file_1.clone()]).unwrap(); |
| 150 | + let tx = action.apply().await.unwrap(); |
| 151 | + let mut action = tx.fast_append(None, vec![]).unwrap(); |
| 152 | + action.add_data_files(vec![data_file_2.clone()]).unwrap(); |
| 153 | + let tx = action.apply().await.unwrap(); |
| 154 | + |
| 155 | + (table.clone(), tx.updates) |
| 156 | + } |
| 157 | + |
| 158 | + #[tokio::test] |
| 159 | + async fn test_validation_history() { |
| 160 | + let (table, updates) = make_v2_table_with_updates().await; |
| 161 | + let parent_snapshot = if let TableUpdate::AddSnapshot { snapshot } = &updates[0] { |
| 162 | + SnapshotRef::new(snapshot.clone()) |
| 163 | + } else { |
| 164 | + unreachable!() |
| 165 | + }; |
| 166 | + let current_snapshot = if let TableUpdate::AddSnapshot { snapshot } = &updates[2] { |
| 167 | + SnapshotRef::new(snapshot.clone()) |
| 168 | + } else { |
| 169 | + unreachable!() |
| 170 | + }; |
| 171 | + |
| 172 | + let test_validator = TestValidator {}; |
| 173 | + |
| 174 | + // specifying from_snapshot, validating up to the from_snapshot |
| 175 | + let (manifests, snapshots) = test_validator |
| 176 | + .validation_history( |
| 177 | + &table, |
| 178 | + Some(¤t_snapshot), |
| 179 | + Some(&parent_snapshot), |
| 180 | + HashSet::from([Operation::Append]), |
| 181 | + ManifestContentType::Data, |
| 182 | + ) |
| 183 | + .await; |
| 184 | + |
| 185 | + manifests |
| 186 | + .iter() |
| 187 | + .for_each(|manifest| assert_eq!(manifest.content, ManifestContentType::Data)); |
| 188 | + assert_eq!(snapshots.into_iter().collect::<Vec<_>>(), vec![ |
| 189 | + current_snapshot.snapshot_id() |
| 190 | + ]); |
| 191 | + } |
| 192 | + |
| 193 | + #[test] |
| 194 | + fn test_ancestor_between() { |
| 195 | + let table = make_v2_table(); |
| 196 | + let current_snapshot = table.metadata().current_snapshot(); |
| 197 | + let parent_snapshot_id = current_snapshot.unwrap().parent_snapshot_id().unwrap(); |
| 198 | + let parent_snapshot = table.metadata().snapshot_by_id(parent_snapshot_id); |
| 199 | + |
| 200 | + // not specifying from_snapshot, listing all ancestors |
| 201 | + let all_ancestors = |
| 202 | + TestValidator::ancestors_between(current_snapshot, None, table.metadata()); |
| 203 | + assert_eq!( |
| 204 | + vec![ |
| 205 | + current_snapshot.unwrap().snapshot_id(), |
| 206 | + current_snapshot.unwrap().parent_snapshot_id().unwrap() |
| 207 | + ], |
| 208 | + all_ancestors |
| 209 | + .iter() |
| 210 | + .map(|snapshot| snapshot.snapshot_id()) |
| 211 | + .collect::<Vec<_>>() |
| 212 | + ); |
| 213 | + |
| 214 | + // specifying from_snapshot, listing only 1 snapshot |
| 215 | + let ancestors = |
| 216 | + TestValidator::ancestors_between(current_snapshot, parent_snapshot, table.metadata()); |
| 217 | + assert_eq!( |
| 218 | + vec![current_snapshot.unwrap().snapshot_id()], |
| 219 | + ancestors |
| 220 | + .iter() |
| 221 | + .map(|snapshot| snapshot.snapshot_id()) |
| 222 | + .collect::<Vec<_>>() |
| 223 | + ); |
| 224 | + } |
| 225 | +} |
0 commit comments