@@ -39,6 +39,7 @@ use vortex_utils::parallelism::get_available_parallelism;
3939
4040use crate :: LayoutReader ;
4141use crate :: LayoutReaderRef ;
42+ use crate :: layouts:: row_idx:: RowIdx ;
4243use crate :: layouts:: row_idx:: RowIdxLayoutReader ;
4344use crate :: scan:: repeated_scan:: RepeatedScan ;
4445use crate :: scan:: split_by:: SplitBy ;
@@ -273,14 +274,18 @@ impl<A: 'static + Send> ScanBuilder<A> {
273274 // conjunction splitting if a filter is provided.
274275 let mut layout_reader = self . layout_reader ;
275276
276- // Enrich the layout reader to support RowIdx expressions.
277+ // Enrich the layout reader to support RowIdx expressions if scan uses #row_idx .
277278 // Note that this is applied below the filter layout reader since it can perform
278279 // better over individual conjunctions.
279- layout_reader = Arc :: new ( RowIdxLayoutReader :: new (
280- self . row_offset ,
281- layout_reader,
282- self . session . clone ( ) ,
283- ) ) ;
280+ if references_row_idx ( & self . projection )
281+ || self . filter . as_ref ( ) . is_some_and ( references_row_idx)
282+ {
283+ layout_reader = Arc :: new ( RowIdxLayoutReader :: new (
284+ self . row_offset ,
285+ layout_reader,
286+ self . session . clone ( ) ,
287+ ) ) ;
288+ }
284289
285290 // Normalize and simplify the expressions.
286291 let projection = self . projection . optimize_recursive ( layout_reader. dtype ( ) ) ?;
@@ -431,6 +436,10 @@ impl<A: 'static + Send> Stream for LazyScanStream<A> {
431436 }
432437}
433438
439+ fn references_row_idx ( expr : & Expression ) -> bool {
440+ expr. is :: < RowIdx > ( ) || expr. children ( ) . iter ( ) . any ( references_row_idx)
441+ }
442+
434443/// Compute masks of field paths referenced by the projection and filter in the scan.
435444///
436445/// Projection and filter must be pre-simplified.
@@ -494,6 +503,7 @@ mod test {
494503 use crate :: LayoutReader ;
495504 use crate :: RowSplits ;
496505 use crate :: SplitRange ;
506+ use crate :: layouts:: row_idx:: row_idx;
497507 use crate :: scan:: test:: SCAN_SESSION ;
498508 use crate :: scan:: test:: session_with_handle;
499509
@@ -896,4 +906,10 @@ mod test {
896906
897907 Ok ( ( ) )
898908 }
909+
910+ #[ test]
911+ fn references_row_idx ( ) {
912+ assert ! ( super :: references_row_idx( & eq( row_idx( ) , lit( 3u64 ) ) ) ) ;
913+ assert ! ( !super :: references_row_idx( & eq( root( ) , lit( 1i32 ) ) ) ) ;
914+ }
899915}
0 commit comments