@@ -26,19 +26,19 @@ pub struct UpdateStatistics<'a> {
26
26
27
27
impl < ' a > UpdateStatistics < ' a > {
28
28
pub fn new ( tx : Transaction < ' a > ) -> Self {
29
- UpdateStatistics {
29
+ Self {
30
30
tx,
31
31
updates : Vec :: new ( ) ,
32
32
}
33
33
}
34
34
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 > {
36
36
self . updates . push ( TableUpdate :: SetStatistics { statistics } ) ;
37
37
38
38
Ok ( self )
39
39
}
40
40
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 > {
42
42
self . updates
43
43
. push ( TableUpdate :: RemoveStatistics { snapshot_id } ) ;
44
44
@@ -51,3 +51,84 @@ impl<'a> UpdateStatistics<'a> {
51
51
Ok ( self . tx )
52
52
}
53
53
}
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