15
15
// specific language governing permissions and limitations
16
16
// under the License.
17
17
18
+ use std:: collections:: HashSet ;
18
19
use std:: sync:: Arc ;
19
20
20
21
use futures:: channel:: mpsc:: Sender ;
21
22
use futures:: { SinkExt , TryFutureExt } ;
23
+ use itertools:: Itertools ;
22
24
23
25
use crate :: delete_file_index:: DeleteFileIndex ;
24
26
use crate :: expr:: { Bind , BoundPredicate , Predicate } ;
@@ -28,11 +30,12 @@ use crate::scan::{
28
30
PartitionFilterCache ,
29
31
} ;
30
32
use crate :: spec:: {
31
- ManifestContentType , ManifestEntryRef , ManifestFile , ManifestList , SchemaRef , SnapshotRef ,
32
- TableMetadataRef ,
33
+ DataContentType , ManifestContentType , ManifestEntryRef , ManifestFile , ManifestList ,
34
+ ManifestStatus , Operation , SchemaRef , SnapshotRef , TableMetadataRef ,
33
35
} ;
34
36
use crate :: { Error , ErrorKind , Result } ;
35
37
38
+ type ManifestEntryFilterFn = dyn Fn ( & ManifestEntryRef ) -> bool + Send + Sync ;
36
39
/// Wraps a [`ManifestFile`] alongside the objects that are needed
37
40
/// to process it in a thread-safe manner
38
41
pub ( crate ) struct ManifestFileContext {
@@ -45,7 +48,11 @@ pub(crate) struct ManifestFileContext {
45
48
object_cache : Arc < ObjectCache > ,
46
49
snapshot_schema : SchemaRef ,
47
50
expression_evaluator_cache : Arc < ExpressionEvaluatorCache > ,
48
- delete_file_index : DeleteFileIndex ,
51
+ delete_file_index : Option < DeleteFileIndex > ,
52
+
53
+ /// filter manifest entries.
54
+ /// Used for different kind of scans, e.g., only scan newly added files without delete files.
55
+ filter_fn : Option < Arc < ManifestEntryFilterFn > > ,
49
56
}
50
57
51
58
/// Wraps a [`ManifestEntryRef`] alongside the objects that are needed
@@ -58,7 +65,7 @@ pub(crate) struct ManifestEntryContext {
58
65
pub bound_predicates : Option < Arc < BoundPredicates > > ,
59
66
pub partition_spec_id : i32 ,
60
67
pub snapshot_schema : SchemaRef ,
61
- pub delete_file_index : DeleteFileIndex ,
68
+ pub delete_file_index : Option < DeleteFileIndex > ,
62
69
}
63
70
64
71
impl ManifestFileContext {
@@ -74,12 +81,13 @@ impl ManifestFileContext {
74
81
mut sender,
75
82
expression_evaluator_cache,
76
83
delete_file_index,
77
- ..
84
+ filter_fn ,
78
85
} = self ;
86
+ let filter_fn = filter_fn. unwrap_or_else ( || Arc :: new ( |_| true ) ) ;
79
87
80
88
let manifest = object_cache. get_manifest ( & manifest_file) . await ?;
81
89
82
- for manifest_entry in manifest. entries ( ) {
90
+ for manifest_entry in manifest. entries ( ) . iter ( ) . filter ( |e| filter_fn ( e ) ) {
83
91
let manifest_entry_context = ManifestEntryContext {
84
92
// TODO: refactor to avoid the expensive ManifestEntry clone
85
93
manifest_entry : manifest_entry. clone ( ) ,
@@ -105,13 +113,16 @@ impl ManifestEntryContext {
105
113
/// consume this `ManifestEntryContext`, returning a `FileScanTask`
106
114
/// created from it
107
115
pub ( crate ) async fn into_file_scan_task ( self ) -> Result < FileScanTask > {
108
- let deletes = self
109
- . delete_file_index
110
- . get_deletes_for_data_file (
111
- self . manifest_entry . data_file ( ) ,
112
- self . manifest_entry . sequence_number ( ) ,
113
- )
114
- . await ;
116
+ let deletes = if let Some ( delete_file_index) = self . delete_file_index {
117
+ delete_file_index
118
+ . get_deletes_for_data_file (
119
+ self . manifest_entry . data_file ( ) ,
120
+ self . manifest_entry . sequence_number ( ) ,
121
+ )
122
+ . await
123
+ } else {
124
+ vec ! [ ]
125
+ } ;
115
126
116
127
Ok ( FileScanTask {
117
128
start : 0 ,
@@ -149,6 +160,11 @@ pub(crate) struct PlanContext {
149
160
pub partition_filter_cache : Arc < PartitionFilterCache > ,
150
161
pub manifest_evaluator_cache : Arc < ManifestEvaluatorCache > ,
151
162
pub expression_evaluator_cache : Arc < ExpressionEvaluatorCache > ,
163
+
164
+ // for incremental scan.
165
+ // If `to_snapshot_id` is set, it means incremental scan. `from_snapshot_id` can be `None`.
166
+ pub from_snapshot_id : Option < i64 > ,
167
+ pub to_snapshot_id : Option < i64 > ,
152
168
}
153
169
154
170
impl PlanContext {
@@ -180,23 +196,84 @@ impl PlanContext {
180
196
Ok ( partition_filter)
181
197
}
182
198
183
- pub ( crate ) fn build_manifest_file_contexts (
199
+ pub ( crate ) async fn build_manifest_file_contexts (
184
200
& self ,
185
- manifest_list : Arc < ManifestList > ,
186
201
tx_data : Sender < ManifestEntryContext > ,
187
- delete_file_idx : DeleteFileIndex ,
188
- delete_file_tx : Sender < ManifestEntryContext > ,
202
+ delete_file_idx_and_tx : Option < ( DeleteFileIndex , Sender < ManifestEntryContext > ) > ,
189
203
) -> Result < Box < impl Iterator < Item = Result < ManifestFileContext > > + ' static > > {
190
- let manifest_files = manifest_list. entries ( ) . iter ( ) ;
204
+ let mut filter_fn: Option < Arc < ManifestEntryFilterFn > > = None ;
205
+ let manifest_files = {
206
+ if let Some ( to_snapshot_id) = self . to_snapshot_id {
207
+ // Incremental scan mode:
208
+ // Get all added files between two snapshots.
209
+ // - data files in `Append` and `Overwrite` snapshots are included.
210
+ // - delete files are ignored
211
+ // - `Replace` snapshots (e.g., compaction) are ignored.
212
+ //
213
+ // `latest_snapshot_id` is inclusive, `oldest_snapshot_id` is exclusive.
214
+
215
+ // prevent misuse
216
+ assert ! (
217
+ delete_file_idx_and_tx. is_none( ) ,
218
+ "delete file is not supported in incremental scan mode"
219
+ ) ;
220
+
221
+ let snapshots =
222
+ ancestors_between ( & self . table_metadata , to_snapshot_id, self . from_snapshot_id )
223
+ . filter ( |snapshot| {
224
+ matches ! (
225
+ snapshot. summary( ) . operation,
226
+ Operation :: Append | Operation :: Overwrite
227
+ )
228
+ } )
229
+ . collect_vec ( ) ;
230
+ let snapshot_ids: HashSet < i64 > = snapshots
231
+ . iter ( )
232
+ . map ( |snapshot| snapshot. snapshot_id ( ) )
233
+ . collect ( ) ;
234
+
235
+ let mut manifest_files = vec ! [ ] ;
236
+ for snapshot in snapshots {
237
+ let manifest_list = self
238
+ . object_cache
239
+ . get_manifest_list ( & snapshot, & self . table_metadata )
240
+ . await ?;
241
+ for entry in manifest_list. entries ( ) {
242
+ if !snapshot_ids. contains ( & entry. added_snapshot_id ) {
243
+ continue ;
244
+ }
245
+ manifest_files. push ( entry. clone ( ) ) ;
246
+ }
247
+ }
248
+
249
+ filter_fn = Some ( Arc :: new ( move |entry : & ManifestEntryRef | {
250
+ matches ! ( entry. status( ) , ManifestStatus :: Added )
251
+ && matches ! ( entry. data_file( ) . content_type( ) , DataContentType :: Data )
252
+ && (
253
+ // Is it possible that the snapshot id here is not contained?
254
+ entry. snapshot_id ( ) . is_none ( )
255
+ || snapshot_ids. contains ( & entry. snapshot_id ( ) . unwrap ( ) )
256
+ )
257
+ } ) ) ;
258
+
259
+ manifest_files
260
+ } else {
261
+ let manifest_list = self . get_manifest_list ( ) . await ?;
262
+ manifest_list. entries ( ) . to_vec ( )
263
+ }
264
+ } ;
191
265
192
266
// TODO: Ideally we could ditch this intermediate Vec as we return an iterator.
193
267
let mut filtered_mfcs = vec ! [ ] ;
194
268
195
- for manifest_file in manifest_files {
196
- let tx = if manifest_file. content == ManifestContentType :: Deletes {
197
- delete_file_tx. clone ( )
269
+ for manifest_file in & manifest_files {
270
+ let ( delete_file_idx, tx) = if manifest_file. content == ManifestContentType :: Deletes {
271
+ let Some ( ( delete_file_idx, tx) ) = delete_file_idx_and_tx. as_ref ( ) else {
272
+ continue ;
273
+ } ;
274
+ ( Some ( delete_file_idx. clone ( ) ) , tx. clone ( ) )
198
275
} else {
199
- tx_data. clone ( )
276
+ ( delete_file_idx_and_tx . as_ref ( ) . map ( | ( idx , _ ) | idx . clone ( ) ) , tx_data. clone ( ) )
200
277
} ;
201
278
202
279
let partition_bound_predicate = if self . predicate . is_some ( ) {
@@ -224,7 +301,8 @@ impl PlanContext {
224
301
manifest_file,
225
302
partition_bound_predicate,
226
303
tx,
227
- delete_file_idx. clone ( ) ,
304
+ delete_file_idx,
305
+ filter_fn. clone ( ) ,
228
306
) ;
229
307
230
308
filtered_mfcs. push ( Ok ( mfc) ) ;
@@ -238,7 +316,8 @@ impl PlanContext {
238
316
manifest_file : & ManifestFile ,
239
317
partition_filter : Option < Arc < BoundPredicate > > ,
240
318
sender : Sender < ManifestEntryContext > ,
241
- delete_file_index : DeleteFileIndex ,
319
+ delete_file_index : Option < DeleteFileIndex > ,
320
+ filter_fn : Option < Arc < ManifestEntryFilterFn > > ,
242
321
) -> ManifestFileContext {
243
322
let bound_predicates =
244
323
if let ( Some ( ref partition_bound_predicate) , Some ( snapshot_bound_predicate) ) =
@@ -261,6 +340,61 @@ impl PlanContext {
261
340
field_ids : self . field_ids . clone ( ) ,
262
341
expression_evaluator_cache : self . expression_evaluator_cache . clone ( ) ,
263
342
delete_file_index,
343
+ filter_fn,
264
344
}
265
345
}
266
346
}
347
+
348
+ struct Ancestors {
349
+ next : Option < SnapshotRef > ,
350
+ get_snapshot : Box < dyn Fn ( i64 ) -> Option < SnapshotRef > + Send > ,
351
+ }
352
+
353
+ impl Iterator for Ancestors {
354
+ type Item = SnapshotRef ;
355
+
356
+ fn next ( & mut self ) -> Option < Self :: Item > {
357
+ let snapshot = self . next . take ( ) ?;
358
+ let result = snapshot. clone ( ) ;
359
+ self . next = snapshot
360
+ . parent_snapshot_id ( )
361
+ . and_then ( |id| ( self . get_snapshot ) ( id) ) ;
362
+ Some ( result)
363
+ }
364
+ }
365
+
366
+ /// Iterate starting from `snapshot` (inclusive) to the root snapshot.
367
+ fn ancestors_of (
368
+ table_metadata : & TableMetadataRef ,
369
+ snapshot : i64 ,
370
+ ) -> Box < dyn Iterator < Item = SnapshotRef > + Send > {
371
+ if let Some ( snapshot) = table_metadata. snapshot_by_id ( snapshot) {
372
+ let table_metadata = table_metadata. clone ( ) ;
373
+ Box :: new ( Ancestors {
374
+ next : Some ( snapshot. clone ( ) ) ,
375
+ get_snapshot : Box :: new ( move |id| table_metadata. snapshot_by_id ( id) . cloned ( ) ) ,
376
+ } )
377
+ } else {
378
+ Box :: new ( std:: iter:: empty ( ) )
379
+ }
380
+ }
381
+
382
+ /// Iterate starting from `snapshot` (inclusive) to `oldest_snapshot_id` (exclusive).
383
+ fn ancestors_between (
384
+ table_metadata : & TableMetadataRef ,
385
+ latest_snapshot_id : i64 ,
386
+ oldest_snapshot_id : Option < i64 > ,
387
+ ) -> Box < dyn Iterator < Item = SnapshotRef > + Send > {
388
+ let Some ( oldest_snapshot_id) = oldest_snapshot_id else {
389
+ return Box :: new ( ancestors_of ( table_metadata, latest_snapshot_id) ) ;
390
+ } ;
391
+
392
+ if latest_snapshot_id == oldest_snapshot_id {
393
+ return Box :: new ( std:: iter:: empty ( ) ) ;
394
+ }
395
+
396
+ Box :: new (
397
+ ancestors_of ( table_metadata, latest_snapshot_id)
398
+ . take_while ( move |snapshot| snapshot. snapshot_id ( ) != oldest_snapshot_id) ,
399
+ )
400
+ }
0 commit comments