Skip to content

Commit 2572fcc

Browse files
committed
add ut
1 parent 795789f commit 2572fcc

File tree

1 file changed

+84
-3
lines changed

1 file changed

+84
-3
lines changed

crates/iceberg/src/transaction/update_statistics.rs

Lines changed: 84 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,19 @@ pub struct UpdateStatistics<'a> {
2626

2727
impl<'a> UpdateStatistics<'a> {
2828
pub fn new(tx: Transaction<'a>) -> Self {
29-
UpdateStatistics {
29+
Self {
3030
tx,
3131
updates: Vec::new(),
3232
}
3333
}
3434

35-
pub fn set_statistics(&mut self, statistics: StatisticsFile) -> Result<&mut Self, Error> {
35+
pub fn set_statistics(mut self, statistics: StatisticsFile) -> Result<Self, Error> {
3636
self.updates.push(TableUpdate::SetStatistics { statistics });
3737

3838
Ok(self)
3939
}
4040

41-
pub fn remove_statistics(&mut self, snapshot_id: i64) -> Result<&mut Self, Error> {
41+
pub fn remove_statistics(mut self, snapshot_id: i64) -> Result<Self, Error> {
4242
self.updates
4343
.push(TableUpdate::RemoveStatistics { snapshot_id });
4444

@@ -51,3 +51,84 @@ impl<'a> UpdateStatistics<'a> {
5151
Ok(self.tx)
5252
}
5353
}
54+
55+
#[cfg(test)]
56+
mod tests {
57+
use std::collections::HashMap;
58+
59+
use crate::TableUpdate;
60+
use crate::spec::{BlobMetadata, StatisticsFile};
61+
use crate::transaction::Transaction;
62+
use crate::transaction::tests::make_v2_table;
63+
64+
#[test]
65+
fn test_update_statistics() {
66+
let table = make_v2_table();
67+
let tx = Transaction::new(&table);
68+
69+
let statistics_file_1 = StatisticsFile {
70+
snapshot_id: 3055729675574597004i64,
71+
statistics_path: "s3://a/b/stats.puffin".to_string(),
72+
file_size_in_bytes: 413,
73+
file_footer_size_in_bytes: 42,
74+
key_metadata: None,
75+
blob_metadata: vec![BlobMetadata {
76+
r#type: "ndv".to_string(),
77+
snapshot_id: 3055729675574597004i64,
78+
sequence_number: 1,
79+
fields: vec![1],
80+
properties: HashMap::new(),
81+
}],
82+
};
83+
84+
let statistics_file_2 = StatisticsFile {
85+
snapshot_id: 3366729675595277004i64,
86+
statistics_path: "s3://a/b/stats.puffin".to_string(),
87+
file_size_in_bytes: 413,
88+
file_footer_size_in_bytes: 42,
89+
key_metadata: None,
90+
blob_metadata: vec![BlobMetadata {
91+
r#type: "ndv".to_string(),
92+
snapshot_id: 3366729675595277004i64,
93+
sequence_number: 1,
94+
fields: vec![1],
95+
properties: HashMap::new(),
96+
}],
97+
};
98+
99+
// set stats1
100+
let tx = tx
101+
.update_statistics()
102+
.set_statistics(statistics_file_1.clone())
103+
.unwrap()
104+
.apply()
105+
.unwrap();
106+
107+
let TableUpdate::SetStatistics { statistics } = tx.updates.get(0).unwrap().clone() else {
108+
panic!("The update should be a TableUpdate::SetStatistics!");
109+
};
110+
assert_eq!(statistics, statistics_file_1);
111+
112+
// start a new update, remove stat1 and set stat2
113+
let tx = tx
114+
.update_statistics()
115+
.remove_statistics(3055729675574597004i64)
116+
.unwrap()
117+
.set_statistics(statistics_file_2.clone())
118+
.unwrap()
119+
.apply()
120+
.unwrap();
121+
122+
assert_eq!(tx.updates.len(), 3);
123+
let TableUpdate::RemoveStatistics { snapshot_id } = tx.updates.get(1).unwrap().clone()
124+
else {
125+
panic!("The update should be a TableUpdate::RemoveStatistics!");
126+
};
127+
assert_eq!(snapshot_id, 3055729675574597004i64);
128+
129+
let TableUpdate::SetStatistics { statistics } = tx.updates.get(2).unwrap().clone() else {
130+
panic!("The update should be a TableUpdate::SetStatistics!");
131+
};
132+
assert_eq!(statistics, statistics_file_2);
133+
}
134+
}

0 commit comments

Comments
 (0)