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 {
@@ -46,6 +49,10 @@ pub(crate) struct ManifestFileContext {
46
49
snapshot_schema : SchemaRef ,
47
50
expression_evaluator_cache : Arc < ExpressionEvaluatorCache > ,
48
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
@@ -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 ( ) ,
@@ -153,6 +161,11 @@ pub(crate) struct PlanContext {
153
161
pub partition_filter_cache : Arc < PartitionFilterCache > ,
154
162
pub manifest_evaluator_cache : Arc < ManifestEvaluatorCache > ,
155
163
pub expression_evaluator_cache : Arc < ExpressionEvaluatorCache > ,
164
+
165
+ // for incremental scan.
166
+ // If `to_snapshot_id` is set, it means incremental scan. `from_snapshot_id` can be `None`.
167
+ pub from_snapshot_id : Option < i64 > ,
168
+ pub to_snapshot_id : Option < i64 > ,
156
169
}
157
170
158
171
impl PlanContext {
@@ -184,18 +197,77 @@ impl PlanContext {
184
197
Ok ( partition_filter)
185
198
}
186
199
187
- pub ( crate ) fn build_manifest_file_contexts (
200
+ pub ( crate ) async fn build_manifest_file_contexts (
188
201
& self ,
189
- manifest_list : Arc < ManifestList > ,
190
202
tx_data : Sender < ManifestEntryContext > ,
191
203
delete_file_idx_and_tx : Option < ( DeleteFileIndex , Sender < ManifestEntryContext > ) > ,
192
204
) -> Result < Box < impl Iterator < Item = Result < ManifestFileContext > > + ' static > > {
193
- let manifest_files = manifest_list. entries ( ) . iter ( ) ;
205
+ let mut filter_fn: Option < Arc < ManifestEntryFilterFn > > = None ;
206
+ let manifest_files = {
207
+ if let Some ( to_snapshot_id) = self . to_snapshot_id {
208
+ // Incremental scan mode:
209
+ // Get all added files between two snapshots.
210
+ // - data files in `Append` and `Overwrite` snapshots are included.
211
+ // - delete files are ignored
212
+ // - `Replace` snapshots (e.g., compaction) are ignored.
213
+ //
214
+ // `latest_snapshot_id` is inclusive, `oldest_snapshot_id` is exclusive.
215
+
216
+ // prevent misuse
217
+ assert ! (
218
+ delete_file_idx_and_tx. is_none( ) ,
219
+ "delete file is not supported in incremental scan mode"
220
+ ) ;
221
+
222
+ let snapshots =
223
+ ancestors_between ( & self . table_metadata , to_snapshot_id, self . from_snapshot_id )
224
+ . filter ( |snapshot| {
225
+ matches ! (
226
+ snapshot. summary( ) . operation,
227
+ Operation :: Append | Operation :: Overwrite
228
+ )
229
+ } )
230
+ . collect_vec ( ) ;
231
+ let snapshot_ids: HashSet < i64 > = snapshots
232
+ . iter ( )
233
+ . map ( |snapshot| snapshot. snapshot_id ( ) )
234
+ . collect ( ) ;
235
+
236
+ let mut manifest_files = vec ! [ ] ;
237
+ for snapshot in snapshots {
238
+ let manifest_list = self
239
+ . object_cache
240
+ . get_manifest_list ( & snapshot, & self . table_metadata )
241
+ . await ?;
242
+ for entry in manifest_list. entries ( ) {
243
+ if !snapshot_ids. contains ( & entry. added_snapshot_id ) {
244
+ continue ;
245
+ }
246
+ manifest_files. push ( entry. clone ( ) ) ;
247
+ }
248
+ }
249
+
250
+ filter_fn = Some ( Arc :: new ( move |entry : & ManifestEntryRef | {
251
+ matches ! ( entry. status( ) , ManifestStatus :: Added )
252
+ && matches ! ( entry. data_file( ) . content_type( ) , DataContentType :: Data )
253
+ && (
254
+ // Is it possible that the snapshot id here is not contained?
255
+ entry. snapshot_id ( ) . is_none ( )
256
+ || snapshot_ids. contains ( & entry. snapshot_id ( ) . unwrap ( ) )
257
+ )
258
+ } ) ) ;
259
+
260
+ manifest_files
261
+ } else {
262
+ let manifest_list = self . get_manifest_list ( ) . await ?;
263
+ manifest_list. entries ( ) . to_vec ( )
264
+ }
265
+ } ;
194
266
195
267
// TODO: Ideally we could ditch this intermediate Vec as we return an iterator.
196
268
let mut filtered_mfcs = vec ! [ ] ;
197
269
198
- for manifest_file in manifest_files {
270
+ for manifest_file in & manifest_files {
199
271
let ( delete_file_idx, tx) = if manifest_file. content == ManifestContentType :: Deletes {
200
272
let Some ( ( delete_file_idx, tx) ) = delete_file_idx_and_tx. as_ref ( ) else {
201
273
continue ;
@@ -234,6 +306,7 @@ impl PlanContext {
234
306
partition_bound_predicate,
235
307
tx,
236
308
delete_file_idx,
309
+ filter_fn. clone ( ) ,
237
310
) ;
238
311
239
312
filtered_mfcs. push ( Ok ( mfc) ) ;
@@ -248,6 +321,7 @@ impl PlanContext {
248
321
partition_filter : Option < Arc < BoundPredicate > > ,
249
322
sender : Sender < ManifestEntryContext > ,
250
323
delete_file_index : Option < DeleteFileIndex > ,
324
+ filter_fn : Option < Arc < ManifestEntryFilterFn > > ,
251
325
) -> ManifestFileContext {
252
326
let bound_predicates =
253
327
if let ( Some ( ref partition_bound_predicate) , Some ( snapshot_bound_predicate) ) =
@@ -270,6 +344,61 @@ impl PlanContext {
270
344
field_ids : self . field_ids . clone ( ) ,
271
345
expression_evaluator_cache : self . expression_evaluator_cache . clone ( ) ,
272
346
delete_file_index,
347
+ filter_fn,
273
348
}
274
349
}
275
350
}
351
+
352
+ struct Ancestors {
353
+ next : Option < SnapshotRef > ,
354
+ get_snapshot : Box < dyn Fn ( i64 ) -> Option < SnapshotRef > + Send > ,
355
+ }
356
+
357
+ impl Iterator for Ancestors {
358
+ type Item = SnapshotRef ;
359
+
360
+ fn next ( & mut self ) -> Option < Self :: Item > {
361
+ let snapshot = self . next . take ( ) ?;
362
+ let result = snapshot. clone ( ) ;
363
+ self . next = snapshot
364
+ . parent_snapshot_id ( )
365
+ . and_then ( |id| ( self . get_snapshot ) ( id) ) ;
366
+ Some ( result)
367
+ }
368
+ }
369
+
370
+ /// Iterate starting from `snapshot` (inclusive) to the root snapshot.
371
+ fn ancestors_of (
372
+ table_metadata : & TableMetadataRef ,
373
+ snapshot : i64 ,
374
+ ) -> Box < dyn Iterator < Item = SnapshotRef > + Send > {
375
+ if let Some ( snapshot) = table_metadata. snapshot_by_id ( snapshot) {
376
+ let table_metadata = table_metadata. clone ( ) ;
377
+ Box :: new ( Ancestors {
378
+ next : Some ( snapshot. clone ( ) ) ,
379
+ get_snapshot : Box :: new ( move |id| table_metadata. snapshot_by_id ( id) . cloned ( ) ) ,
380
+ } )
381
+ } else {
382
+ Box :: new ( std:: iter:: empty ( ) )
383
+ }
384
+ }
385
+
386
+ /// Iterate starting from `snapshot` (inclusive) to `oldest_snapshot_id` (exclusive).
387
+ fn ancestors_between (
388
+ table_metadata : & TableMetadataRef ,
389
+ latest_snapshot_id : i64 ,
390
+ oldest_snapshot_id : Option < i64 > ,
391
+ ) -> Box < dyn Iterator < Item = SnapshotRef > + Send > {
392
+ let Some ( oldest_snapshot_id) = oldest_snapshot_id else {
393
+ return Box :: new ( ancestors_of ( table_metadata, latest_snapshot_id) ) ;
394
+ } ;
395
+
396
+ if latest_snapshot_id == oldest_snapshot_id {
397
+ return Box :: new ( std:: iter:: empty ( ) ) ;
398
+ }
399
+
400
+ Box :: new (
401
+ ancestors_of ( table_metadata, latest_snapshot_id)
402
+ . take_while ( move |snapshot| snapshot. snapshot_id ( ) != oldest_snapshot_id) ,
403
+ )
404
+ }
0 commit comments