1515// specific language governing permissions and limitations
1616// under the License.
1717
18+ use std:: collections:: HashSet ;
1819use std:: sync:: Arc ;
1920
2021use futures:: channel:: mpsc:: Sender ;
2122use futures:: { SinkExt , TryFutureExt } ;
23+ use itertools:: Itertools ;
2224
2325use crate :: delete_file_index:: DeleteFileIndex ;
2426use crate :: expr:: { Bind , BoundPredicate , Predicate } ;
@@ -28,11 +30,12 @@ use crate::scan::{
2830 PartitionFilterCache ,
2931} ;
3032use crate :: spec:: {
31- ManifestContentType , ManifestEntryRef , ManifestFile , ManifestList , SchemaRef , SnapshotRef ,
32- TableMetadataRef ,
33+ DataContentType , ManifestContentType , ManifestEntryRef , ManifestFile , ManifestList ,
34+ ManifestStatus , Operation , SchemaRef , SnapshotRef , TableMetadataRef ,
3335} ;
3436use crate :: { Error , ErrorKind , Result } ;
3537
38+ type ManifestEntryFilterFn = dyn Fn ( & ManifestEntryRef ) -> bool + Send + Sync ;
3639/// Wraps a [`ManifestFile`] alongside the objects that are needed
3740/// to process it in a thread-safe manner
3841pub ( crate ) struct ManifestFileContext {
@@ -46,6 +49,10 @@ pub(crate) struct ManifestFileContext {
4649 snapshot_schema : SchemaRef ,
4750 expression_evaluator_cache : Arc < ExpressionEvaluatorCache > ,
4851 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 > > ,
4956}
5057
5158/// Wraps a [`ManifestEntryRef`] alongside the objects that are needed
@@ -74,12 +81,13 @@ impl ManifestFileContext {
7481 mut sender,
7582 expression_evaluator_cache,
7683 delete_file_index,
77- ..
84+ filter_fn ,
7885 } = self ;
86+ let filter_fn = filter_fn. unwrap_or_else ( || Arc :: new ( |_| true ) ) ;
7987
8088 let manifest = object_cache. get_manifest ( & manifest_file) . await ?;
8189
82- for manifest_entry in manifest. entries ( ) {
90+ for manifest_entry in manifest. entries ( ) . iter ( ) . filter ( |e| filter_fn ( e ) ) {
8391 let manifest_entry_context = ManifestEntryContext {
8492 // TODO: refactor to avoid the expensive ManifestEntry clone
8593 manifest_entry : manifest_entry. clone ( ) ,
@@ -153,6 +161,11 @@ pub(crate) struct PlanContext {
153161 pub partition_filter_cache : Arc < PartitionFilterCache > ,
154162 pub manifest_evaluator_cache : Arc < ManifestEvaluatorCache > ,
155163 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 > ,
156169}
157170
158171impl PlanContext {
@@ -184,18 +197,77 @@ impl PlanContext {
184197 Ok ( partition_filter)
185198 }
186199
187- pub ( crate ) fn build_manifest_file_contexts (
200+ pub ( crate ) async fn build_manifest_file_contexts (
188201 & self ,
189- manifest_list : Arc < ManifestList > ,
190202 tx_data : Sender < ManifestEntryContext > ,
191203 delete_file_idx_and_tx : Option < ( DeleteFileIndex , Sender < ManifestEntryContext > ) > ,
192204 ) -> 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+ } ;
194266
195267 // TODO: Ideally we could ditch this intermediate Vec as we return an iterator.
196268 let mut filtered_mfcs = vec ! [ ] ;
197269
198- for manifest_file in manifest_files {
270+ for manifest_file in & manifest_files {
199271 let ( delete_file_idx, tx) = if manifest_file. content == ManifestContentType :: Deletes {
200272 let Some ( ( delete_file_idx, tx) ) = delete_file_idx_and_tx. as_ref ( ) else {
201273 continue ;
@@ -234,6 +306,7 @@ impl PlanContext {
234306 partition_bound_predicate,
235307 tx,
236308 delete_file_idx,
309+ filter_fn. clone ( ) ,
237310 ) ;
238311
239312 filtered_mfcs. push ( Ok ( mfc) ) ;
@@ -248,6 +321,7 @@ impl PlanContext {
248321 partition_filter : Option < Arc < BoundPredicate > > ,
249322 sender : Sender < ManifestEntryContext > ,
250323 delete_file_index : Option < DeleteFileIndex > ,
324+ filter_fn : Option < Arc < ManifestEntryFilterFn > > ,
251325 ) -> ManifestFileContext {
252326 let bound_predicates =
253327 if let ( Some ( ref partition_bound_predicate) , Some ( snapshot_bound_predicate) ) =
@@ -270,6 +344,61 @@ impl PlanContext {
270344 field_ids : self . field_ids . clone ( ) ,
271345 expression_evaluator_cache : self . expression_evaluator_cache . clone ( ) ,
272346 delete_file_index,
347+ filter_fn,
273348 }
274349 }
275350}
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