@@ -17,10 +17,12 @@ use vortex_array::iter::ArrayIterator;
1717use vortex_array:: iter:: ArrayIteratorAdapter ;
1818use vortex_array:: stream:: ArrayStream ;
1919use vortex_array:: stream:: ArrayStreamAdapter ;
20+ use vortex_error:: VortexExpect ;
2021use vortex_error:: VortexResult ;
2122use vortex_io:: runtime:: BlockingRuntime ;
2223use vortex_io:: session:: RuntimeSessionExt ;
2324use vortex_scan:: selection:: Selection ;
25+ use vortex_scan:: selection:: selection_roaring_intersects;
2426use vortex_session:: VortexSession ;
2527use vortex_utils:: parallelism:: get_available_parallelism;
2628
@@ -130,7 +132,17 @@ impl<A: 'static + Send> RepeatedScan<A> {
130132 mapper : Arc :: clone ( & self . map_fn ) ,
131133 } ) ;
132134
135+ let selection_range: Option < Range < u64 > > = match & self . selection {
136+ Selection :: IncludeByIndex ( buf) if !buf. is_empty ( ) => {
137+ Some ( buf[ 0 ] ..buf[ buf. len ( ) - 1 ] + 1 )
138+ }
139+ Selection :: IncludeRoaring ( roaring) if !roaring. is_empty ( ) => {
140+ Some ( roaring. min ( ) . vortex_expect ( "empty" ) ..roaring. max ( ) . vortex_expect ( "empty" ) + 1 )
141+ }
142+ _ => None ,
143+ } ;
133144 let row_range = intersect_ranges ( self . row_range . as_ref ( ) , row_range) ;
145+ let row_range = intersect_ranges ( row_range. as_ref ( ) , selection_range) ;
134146
135147 let ranges = match & self . splits {
136148 Splits :: Natural ( btree_set) => {
@@ -167,17 +179,33 @@ impl<A: 'static + Send> RepeatedScan<A> {
167179
168180 let mut limit = self . limit ;
169181 let mut tasks = Vec :: new ( ) ;
182+ let mut cursor = 0usize ;
170183
171184 for range in ranges {
172185 if range. start >= range. end {
173186 continue ;
174187 }
175188
176- if limit. is_some_and ( |l| l == 0 ) {
177- break ;
189+ let selection_intersects = match & self . selection {
190+ Selection :: IncludeByIndex ( buf) => {
191+ // "ranges" and "buf" are sorted so we can use two pointers
192+ // to intersect.
193+ while cursor < buf. len ( ) && buf[ cursor] < range. start {
194+ cursor += 1 ;
195+ }
196+ cursor < buf. len ( ) && buf[ cursor] < range. end
197+ }
198+ Selection :: IncludeRoaring ( roaring) => selection_roaring_intersects ( roaring, & range) ,
199+ _ => true ,
200+ } ;
201+ if !selection_intersects {
202+ continue ;
178203 }
179204
180205 tasks. push ( split_exec ( Arc :: clone ( & ctx) , range, limit. as_mut ( ) ) ?) ;
206+ if limit. is_some_and ( |l| l == 0 ) {
207+ break ;
208+ }
181209 }
182210
183211 Ok ( tasks)
0 commit comments