vortex-layout 0.90.0

Vortex layouts provide a way to perform lazy push-down scans over abstract storage
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright the Vortex contributors

use std::ops::Range;
use std::pin::Pin;
use std::sync::Arc;
use std::task::Context;
use std::task::Poll;
use std::task::ready;

use futures::Stream;
use futures::future::BoxFuture;
use futures::stream::BoxStream;
use itertools::Itertools;
use vortex_array::ArrayRef;
use vortex_array::dtype::DType;
use vortex_array::dtype::FieldMask;
use vortex_array::expr::BoundExpression;
use vortex_array::expr::analysis::referenced_field_paths;
use vortex_array::expr::transform::find_between;
use vortex_array::iter::ArrayIterator;
use vortex_array::iter::ArrayIteratorAdapter;
use vortex_array::stream::ArrayStream;
use vortex_array::stream::ArrayStreamAdapter;
use vortex_error::VortexExpect;
use vortex_error::VortexResult;
use vortex_io::runtime::BlockingRuntime;
use vortex_io::runtime::Task;
use vortex_io::session::RuntimeSessionExt;
use vortex_metrics::MetricsRegistry;
use vortex_scan::selection::Selection;
use vortex_scan::strict_sorted_buffer::StrictSortedBuffer;
use vortex_session::VortexSession;

use crate::LayoutReader;
use crate::LayoutReaderRef;
use crate::layouts::row_idx::RowIdx;
use crate::layouts::row_idx::RowIdxLayoutReader;
use crate::scan::limit::RowLimit;
use crate::scan::limit::ScanLimit;
use crate::scan::repeated_scan::RepeatedScan;
use crate::scan::split_by::SplitBy;
use crate::scan::splits::Splits;
use crate::scan::splits::attempt_split_ranges;

/// Builder for scanning a [`LayoutReader`] into arrays, streams, iterators, or mapped outputs.
///
/// A scan has three independent row restriction mechanisms:
///
/// - [`with_row_range`](Self::with_row_range) selects a contiguous range before scanning.
/// - [`with_selection`](Self::with_selection) applies a [`Selection`] inside that range.
/// - [`with_filter`](Self::with_filter) evaluates an expression predicate during execution.
///
/// Projection and filter expressions must be bound against the reader dtype. Work is divided by
/// the configured [`SplitBy`] strategy or by explicit selection ranges.
pub struct ScanBuilder<A> {
    session: VortexSession,
    layout_reader: LayoutReaderRef,
    projection: BoundExpression,
    filter: Option<BoundExpression>,
    /// Whether the scan needs to return splits in the order they appear in the file.
    ordered: bool,
    /// Optionally read a subset of the rows in the file.
    row_range: Option<Range<u64>>,
    /// The selection mask to apply to the selected row range.
    // TODO(joe): replace this is usage of row_id selection, see
    selection: Selection,
    /// How to split the file for concurrent processing.
    split_by: SplitBy,
    /// Precomputed full-file natural split boundaries; when set, [`prepare`](Self::prepare)
    /// uses them instead of walking the layout.
    natural_splits: Option<Arc<[u64]>>,
    /// The number of splits to make progress on concurrently **per-thread**.
    concurrency: usize,
    /// Function to apply to each [`ArrayRef`] within the spawned split tasks.
    map_fn: Arc<dyn Fn(ArrayRef) -> VortexResult<A> + Send + Sync>,
    metrics_registry: Option<Arc<dyn MetricsRegistry>>,
    /// Maximal number of rows to read after filtering.
    limit: Option<ScanLimit>,
    /// The row-offset assigned to the first row of the file. Used by the `row_idx` expression,
    /// but not by the scan [`Selection`] which remains relative.
    row_offset: u64,
}

impl ScanBuilder<ArrayRef> {
    /// Create a scan builder over `layout_reader` using `session` for runtime and execution state.
    pub fn new(session: VortexSession, layout_reader: Arc<dyn LayoutReader>) -> Self {
        let projection = BoundExpression::new_root(layout_reader.dtype().clone());
        Self {
            session,
            layout_reader,
            projection,
            filter: None,
            ordered: true,
            row_range: None,
            selection: Default::default(),
            split_by: SplitBy::default(),
            natural_splits: None,
            // We default to four tasks per worker thread, which allows for some I/O lookahead
            // without too much impact on work-stealing.
            concurrency: 4,
            map_fn: Arc::new(Ok),
            metrics_registry: None,
            limit: None,
            row_offset: 0,
        }
    }

    /// Returns an [`ArrayStream`] with tasks spawned onto the session's runtime handle.
    ///
    /// See [`ScanBuilder::into_stream`] for more details.
    pub fn into_array_stream(self) -> VortexResult<impl ArrayStream + Send + 'static> {
        let dtype = self.dtype()?;
        let stream = self.into_stream()?;
        Ok(ArrayStreamAdapter::new(dtype, stream))
    }

    /// Returns an [`ArrayIterator`] using the given blocking runtime.
    pub fn into_array_iter<B: BlockingRuntime>(
        self,
        runtime: &B,
    ) -> VortexResult<impl ArrayIterator + 'static> {
        let stream = self.into_array_stream()?;
        let dtype = stream.dtype().clone();
        Ok(ArrayIteratorAdapter::new(
            dtype,
            runtime.block_on_stream(stream),
        ))
    }
}

impl<A: 'static + Send> ScanBuilder<A> {
    /// Add a filter expression bound against the reader dtype.
    pub fn with_filter(mut self, filter: BoundExpression) -> Self {
        self.filter = Some(filter);
        self
    }

    /// Add or clear a filter expression bound against the reader dtype.
    pub fn with_some_filter(mut self, filter: Option<BoundExpression>) -> Self {
        self.filter = filter;
        self
    }

    /// Set a projection expression bound against the reader dtype.
    pub fn with_projection(mut self, projection: BoundExpression) -> Self {
        self.projection = projection;
        self
    }

    /// Returns whether output chunks are yielded in file order.
    pub fn ordered(&self) -> bool {
        self.ordered
    }

    /// Configure whether output chunks must be yielded in file order.
    pub fn with_ordered(mut self, ordered: bool) -> Self {
        self.ordered = ordered;
        self
    }

    /// Restrict scanning to a contiguous row range.
    pub fn with_row_range(mut self, row_range: Range<u64>) -> Self {
        self.row_range = Some(row_range);
        self
    }

    /// Apply a row selection to the selected row range.
    pub fn with_selection(mut self, selection: Selection) -> Self {
        self.selection = selection;
        self
    }

    /// Select rows by strictly sorted absolute indices relative to the scan input.
    pub fn with_row_indices(mut self, row_indices: StrictSortedBuffer<u64>) -> Self {
        self.selection = Selection::IncludeByIndex(row_indices);
        self
    }

    /// Set the root row offset used by row-index expressions.
    pub fn with_row_offset(mut self, row_offset: u64) -> Self {
        self.row_offset = row_offset;
        self
    }

    /// Configure how natural scan work is split for concurrency.
    pub fn with_split_by(mut self, split_by: SplitBy) -> Self {
        self.split_by = split_by;
        self
    }

    /// Supply precomputed full-file natural split boundaries (see
    /// [`full_file_splits`](Self::full_file_splits)) so [`prepare`](Self::prepare) reuses them
    /// instead of walking the layout. Callers translating external partitions into row ranges
    /// can compute the boundaries once per file and share them across partitions.
    ///
    /// Takes precedence over [`with_split_by`](Self::with_split_by); boundaries outside the
    /// scan's row range are clamped during execution. Boundaries must be strictly increasing.
    pub fn with_natural_splits(mut self, boundaries: Arc<[u64]>) -> Self {
        debug_assert!(
            boundaries.windows(2).all(|w| w[0] < w[1]),
            "natural split boundaries must be strictly increasing"
        );
        self.natural_splits = Some(boundaries);
        self
    }

    /// Compute the full-file natural split boundaries for the fields referenced by this scan's
    /// projection and filter, ignoring any configured row range.
    ///
    /// These are the boundaries [`prepare`](Self::prepare) derives for a whole-file scan; hand
    /// them back via [`with_natural_splits`](Self::with_natural_splits) to skip the layout walk
    /// in `prepare`.
    pub fn full_file_splits(&self) -> VortexResult<Vec<u64>> {
        let field_mask = referenced_field_masks(&self.projection, self.filter.as_ref())?;
        self.split_by.splits(
            self.layout_reader.as_ref(),
            &(0..self.layout_reader.row_count()),
            &field_mask,
        )
    }

    /// Returns the per-worker row-split concurrency.
    pub fn concurrency(&self) -> usize {
        self.concurrency
    }

    /// The number of row splits to make progress on concurrently per-thread, must
    /// be greater than 0.
    pub fn with_concurrency(mut self, concurrency: usize) -> Self {
        assert!(concurrency > 0);
        self.concurrency = concurrency;
        self
    }

    /// Add or clear the metrics registry used by scan execution.
    pub fn with_some_metrics_registry(mut self, metrics: Option<Arc<dyn MetricsRegistry>>) -> Self {
        self.metrics_registry = metrics;
        self
    }

    /// Set the metrics registry used by scan execution.
    pub fn with_metrics_registry(mut self, metrics: Arc<dyn MetricsRegistry>) -> Self {
        self.metrics_registry = Some(metrics);
        self
    }

    /// Add or clear the maximum number of rows returned after filtering.
    ///
    /// A prepared [`RepeatedScan`] applies the limit to each of its executions independently.
    pub fn with_some_limit(mut self, limit: Option<u64>) -> Self {
        self.limit = limit.map(ScanLimit::PerExecution);
        self
    }

    /// Set the maximum number of rows returned after filtering.
    ///
    /// A prepared [`RepeatedScan`] applies the limit to each of its executions independently.
    pub fn with_limit(self, limit: u64) -> Self {
        self.with_some_limit(Some(limit))
    }

    /// Draw rows from a budget shared with sibling partitions instead of a limit of its own.
    pub(crate) fn with_shared_limit(mut self, limit: RowLimit) -> Self {
        self.limit = Some(ScanLimit::Shared(limit));
        self
    }

    /// The [`DType`] returned by the scan, after applying the projection.
    pub fn dtype(&self) -> VortexResult<DType> {
        Ok(self.projection.dtype().clone())
    }

    /// The session used by the scan.
    pub fn session(&self) -> &VortexSession {
        &self.session
    }

    /// Map each split of the scan. The function will be run on the spawned task.
    pub fn map<B: 'static>(
        self,
        map_fn: impl Fn(A) -> VortexResult<B> + 'static + Send + Sync,
    ) -> ScanBuilder<B> {
        let old_map_fn = self.map_fn;
        ScanBuilder {
            session: self.session,
            layout_reader: self.layout_reader,
            projection: self.projection,
            filter: self.filter,
            ordered: self.ordered,
            row_range: self.row_range,
            selection: self.selection,
            split_by: self.split_by,
            natural_splits: self.natural_splits,
            concurrency: self.concurrency,
            metrics_registry: self.metrics_registry,
            limit: self.limit,
            row_offset: self.row_offset,
            map_fn: Arc::new(move |a| old_map_fn(a).and_then(&map_fn)),
        }
    }

    /// Rewrite range comparisons in the filter into betweens, compute split ranges, and return an
    /// executable repeated scan.
    pub fn prepare(self) -> VortexResult<RepeatedScan<A>> {
        let dtype = self.dtype()?;

        // Spin up the root layout reader, and wrap it in a FilterLayoutReader to perform
        // conjunction splitting if a filter is provided.
        let mut layout_reader = self.layout_reader;

        // Enrich the layout reader to support RowIdx expressions if scan uses #row_idx.
        // Note that this is applied below the filter layout reader since it can perform
        // better over individual conjunctions.
        let mut found_row_idx = self.projection.contains::<RowIdx>()?;
        if !found_row_idx && let Some(filter) = self.filter.as_ref() {
            found_row_idx = filter.contains::<RowIdx>()?;
        }
        if found_row_idx {
            layout_reader = Arc::new(RowIdxLayoutReader::new(
                self.row_offset,
                layout_reader,
                self.session.clone(),
            ));
        }

        let bound_projection = self.projection;
        let bound_filter = self.filter.map(find_between).transpose()?;

        // Compute the row splits of the scan.
        let splits =
            if let Some(ranges) = attempt_split_ranges(&self.selection, self.row_range.as_ref()) {
                Splits::Ranges(ranges)
            } else if let Some(boundaries) = self.natural_splits {
                // Caller-supplied full-file boundaries; execution clamps them to the row range.
                Splits::Natural(boundaries)
            } else {
                let field_mask = referenced_field_masks(&bound_projection, bound_filter.as_ref())?;
                let split_range = self
                    .row_range
                    .clone()
                    .unwrap_or_else(|| 0..layout_reader.row_count());
                Splits::Natural(
                    self.split_by
                        .splits(layout_reader.as_ref(), &split_range, &field_mask)?
                        .into(),
                )
            };

        Ok(RepeatedScan::new(
            self.session.clone(),
            layout_reader,
            bound_projection,
            bound_filter,
            self.ordered,
            self.row_range,
            self.selection,
            splits,
            self.concurrency,
            self.map_fn,
            self.limit,
            dtype,
        ))
    }

    /// Constructs a task per row split of the scan, returned as a vector of futures.
    ///
    /// Scans with row limits must use [`Self::into_stream`] or [`Self::into_iter`] so the scan
    /// can coordinate the limit across splits.
    pub fn build(self) -> VortexResult<Vec<BoxFuture<'static, VortexResult<Option<A>>>>> {
        self.prepare()?.execute(None)
    }

    /// Returns a [`Stream`] with tasks spawned onto the session's runtime handle.
    ///
    /// Preparation and initial stream construction begin on the first poll. Errors from either
    /// step are returned as the stream's next item.
    pub fn into_stream(
        self,
    ) -> VortexResult<impl Stream<Item = VortexResult<A>> + Send + 'static + use<A>> {
        Ok(LazyScanStream::new(self))
    }

    /// Returns an [`Iterator`] using the session's runtime.
    pub fn into_iter<B: BlockingRuntime>(
        self,
        runtime: &B,
    ) -> VortexResult<impl Iterator<Item = VortexResult<A>> + 'static> {
        let stream = self.into_stream()?;
        Ok(runtime.block_on_stream(stream))
    }
}

enum LazyScanState<A: 'static + Send> {
    Builder(Option<Box<ScanBuilder<A>>>),
    Preparing(Task<VortexResult<BoxStream<'static, VortexResult<A>>>>),
    Stream(BoxStream<'static, VortexResult<A>>),
    Error(Option<vortex_error::VortexError>),
}

struct LazyScanStream<A: 'static + Send> {
    state: LazyScanState<A>,
}

impl<A: 'static + Send> LazyScanStream<A> {
    fn new(builder: ScanBuilder<A>) -> Self {
        Self {
            state: LazyScanState::Builder(Some(Box::new(builder))),
        }
    }
}

impl<A: 'static + Send> Unpin for LazyScanStream<A> {}

impl<A: 'static + Send> Stream for LazyScanStream<A> {
    type Item = VortexResult<A>;

    fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        loop {
            match &mut self.state {
                LazyScanState::Builder(builder) => {
                    let builder = builder.take().vortex_expect("polled after completion");
                    let handle = builder.session.handle();
                    // IMPORTANT: Building the stream can synchronously walk the layout and
                    // register I/O for every split. Keep it with preparation in this CPU
                    // task: poll_next must only wait for and poll an already-constructed stream.
                    // This also keeps construction errors on the Preparing -> Error path rather
                    // than running construction on the caller's executor.
                    let task = handle.spawn_cpu(move || builder.prepare()?.execute_stream(None));
                    self.state = LazyScanState::Preparing(task);
                }
                LazyScanState::Preparing(task) => match ready!(Pin::new(task).poll(cx)) {
                    Ok(stream) => self.state = LazyScanState::Stream(stream),
                    Err(err) => self.state = LazyScanState::Error(Some(err)),
                },
                LazyScanState::Stream(stream) => return stream.as_mut().poll_next(cx),
                LazyScanState::Error(err) => return Poll::Ready(err.take().map(Err)),
            }
        }
    }
}

/// Compute masks of field paths referenced by the projection and filter in the scan.
///
/// Projection and filter must be pre-simplified and bound against the scan dtype.
pub fn referenced_field_masks(
    projection: &BoundExpression,
    filter: Option<&BoundExpression>,
) -> VortexResult<Vec<FieldMask>> {
    let mut field_paths = referenced_field_paths(projection)?;
    if let Some(filter) = filter {
        field_paths.extend(referenced_field_paths(filter)?);
    }
    Ok(field_paths
        .into_iter()
        .map(|path| {
            if path.is_root() {
                FieldMask::All
            } else {
                FieldMask::Prefix(path)
            }
        })
        .collect_vec())
}

#[cfg(test)]
mod test {
    use std::ops::Range;
    use std::pin::Pin;
    use std::sync::Arc;
    use std::sync::atomic::AtomicUsize;
    use std::sync::atomic::Ordering;
    use std::sync::mpsc;
    use std::task::Context;
    use std::task::Poll;
    use std::time::Duration;

    use futures::Stream;
    use futures::StreamExt;
    use futures::task::noop_waker_ref;
    use parking_lot::Mutex;
    use rstest::rstest;
    use vortex_array::IntoArray;
    use vortex_array::MaskFuture;
    use vortex_array::VortexSessionExecute;
    use vortex_array::array_session;
    use vortex_array::arrays::PrimitiveArray;
    use vortex_array::dtype::DType;
    use vortex_array::dtype::FieldMask;
    use vortex_array::dtype::FieldPath;
    use vortex_array::dtype::Nullability;
    use vortex_array::dtype::PType;
    use vortex_array::dtype::StructFields;
    use vortex_array::expr::BoundExpression;
    use vortex_array::expr::ExactBoundExpr;
    use vortex_array::expr::and;
    use vortex_array::expr::between;
    use vortex_array::expr::col;
    use vortex_array::expr::eq;
    use vortex_array::expr::get_item;
    use vortex_array::expr::gt_eq;
    use vortex_array::expr::is_not_null;
    use vortex_array::expr::lit;
    use vortex_array::expr::lt;
    use vortex_array::expr::root;
    use vortex_array::scalar_fn::fns::between::BetweenOptions;
    use vortex_array::scalar_fn::fns::between::StrictComparison;
    use vortex_error::VortexResult;
    use vortex_error::vortex_bail;
    use vortex_error::vortex_err;
    use vortex_io::runtime::BlockingRuntime;
    use vortex_io::runtime::single::SingleThreadRuntime;
    use vortex_io::runtime::tokio::TokioRuntime;
    use vortex_mask::Mask;

    use super::ScanBuilder;
    use super::referenced_field_masks;
    use crate::ArrayFuture;
    use crate::LayoutReader;
    use crate::RowSplits;
    use crate::SplitRange;
    use crate::scan::limit::RowLimit;
    use crate::scan::test::SCAN_SESSION;
    use crate::scan::test::TestLayoutReader;
    use crate::scan::test::collect_scan_values;
    use crate::scan::test::drain_runtime;
    use crate::scan::test::keep_all;
    use crate::scan::test::keep_odd;
    use crate::scan::test::session_with_handle;

    fn nested_dtype() -> DType {
        DType::Struct(
            StructFields::from_iter([
                (
                    "a",
                    DType::Struct(
                        StructFields::from_iter([
                            ("1", DType::Primitive(PType::I32, Nullability::NonNullable)),
                            ("2", DType::Primitive(PType::I32, Nullability::NonNullable)),
                        ]),
                        Nullability::NonNullable,
                    ),
                ),
                ("b", DType::Primitive(PType::I32, Nullability::NonNullable)),
            ]),
            Nullability::NonNullable,
        )
    }

    #[test]
    fn bound_setters_preserve_identity() -> VortexResult<()> {
        let dtype = DType::Primitive(PType::I32, Nullability::NonNullable);
        let projection = eq(root(), lit(1_i32)).bind(&dtype)?;
        let filter = eq(root(), lit(2_i32)).bind(&dtype)?;
        let expected_projection = ExactBoundExpr(projection.clone());
        let expected_filter = ExactBoundExpr(filter.clone());
        let reader = Arc::new(CountingLayoutReader::new(Arc::new(AtomicUsize::new(0))));

        let builder = ScanBuilder::new(SCAN_SESSION.clone(), reader)
            .with_projection(projection)
            .with_filter(filter);

        assert_eq!(ExactBoundExpr(builder.projection), expected_projection);
        assert_eq!(builder.filter.map(ExactBoundExpr), Some(expected_filter));
        Ok(())
    }

    #[test]
    fn root_projection_produces_all_mask() -> VortexResult<()> {
        let dtype = DType::Primitive(PType::I32, Nullability::NonNullable);
        let projection = root().bind(&dtype)?;

        assert_eq!(referenced_field_masks(&projection, None)?, [FieldMask::All]);
        Ok(())
    }

    #[test]
    fn nested_projection_preserves_field_path_in_split_mask() -> VortexResult<()> {
        let dtype = nested_dtype();
        let projection = get_item("1", get_item("a", root())).bind(&dtype)?;
        let filter = eq(get_item("2", get_item("a", root())), lit(0_i32)).bind(&dtype)?;

        let field_masks = referenced_field_masks(&projection, Some(&filter))?;

        assert_eq!(field_masks.len(), 2);
        assert!(field_masks.contains(&FieldMask::Prefix(FieldPath::from_name("a").push("1"))));
        assert!(field_masks.contains(&FieldMask::Prefix(FieldPath::from_name("a").push("2"))));
        Ok(())
    }

    #[test]
    fn filter_path_covers_nested_projection_path() -> VortexResult<()> {
        let dtype = nested_dtype();
        let projection = get_item("1", get_item("a", root())).bind(&dtype)?;
        let filter = is_not_null(get_item("a", root())).bind(&dtype)?;

        let field_masks = referenced_field_masks(&projection, Some(&filter))?;

        assert_eq!(field_masks, [FieldMask::Prefix(FieldPath::from_name("a"))]);
        Ok(())
    }

    #[test]
    fn parent_projection_path_covers_nested_filter_path() -> VortexResult<()> {
        let dtype = nested_dtype();
        let projection = get_item("a", root()).bind(&dtype)?;
        let filter = is_not_null(get_item("1", get_item("a", root()))).bind(&dtype)?;

        let field_masks = referenced_field_masks(&projection, Some(&filter))?;

        assert_eq!(field_masks, [FieldMask::Prefix(FieldPath::from_name("a"))]);
        Ok(())
    }

    #[derive(Debug)]
    struct CountingLayoutReader {
        name: Arc<str>,
        dtype: DType,
        row_count: u64,
        register_splits_calls: Arc<AtomicUsize>,
    }

    impl CountingLayoutReader {
        fn new(register_splits_calls: Arc<AtomicUsize>) -> Self {
            Self {
                name: Arc::from("counting"),
                dtype: DType::Primitive(PType::I32, Nullability::NonNullable),
                row_count: 1,
                register_splits_calls,
            }
        }
    }

    impl LayoutReader for CountingLayoutReader {
        fn name(&self) -> &Arc<str> {
            &self.name
        }

        fn dtype(&self) -> &DType {
            &self.dtype
        }

        fn row_count(&self) -> u64 {
            self.row_count
        }

        fn register_splits(
            &self,
            _field_mask: &[FieldMask],
            split_range: &SplitRange,
            splits: &mut RowSplits,
        ) -> VortexResult<()> {
            self.register_splits_calls.fetch_add(1, Ordering::Relaxed);
            splits.push(split_range.root_row_range().end);
            Ok(())
        }

        fn pruning_evaluation(
            &self,
            _row_range: &Range<u64>,
            _expr: &BoundExpression,
            _mask: Mask,
        ) -> VortexResult<MaskFuture> {
            vortex_bail!("not needed for this test");
        }

        fn filter_evaluation(
            &self,
            _row_range: &Range<u64>,
            _expr: &BoundExpression,
            _mask: MaskFuture,
        ) -> VortexResult<MaskFuture> {
            vortex_bail!("not needed for this test");
        }

        fn projection_evaluation(
            &self,
            _row_range: &Range<u64>,
            _expr: &BoundExpression,
            _mask: MaskFuture,
        ) -> VortexResult<ArrayFuture> {
            Ok(Box::pin(async move {
                unreachable!("scan should not be polled in this test")
            }))
        }

        fn as_any(&self) -> &dyn std::any::Any {
            self
        }
    }

    #[test]
    fn into_stream_is_lazy() {
        let calls = Arc::new(AtomicUsize::new(0));
        let reader = Arc::new(CountingLayoutReader::new(Arc::clone(&calls)));

        let session = SCAN_SESSION.clone();

        let _stream = ScanBuilder::new(session, reader).into_stream().unwrap();

        assert_eq!(calls.load(Ordering::Relaxed), 0);
    }

    #[derive(Debug)]
    struct SplittingLayoutReader {
        name: Arc<str>,
        dtype: DType,
        row_count: u64,
        register_splits_calls: Arc<AtomicUsize>,
        blocking_projection: Option<BlockingProjection>,
    }

    #[derive(Debug)]
    struct BlockingProjection {
        started: mpsc::Sender<()>,
        gate: Arc<Mutex<()>>,
    }

    impl SplittingLayoutReader {
        fn new(register_splits_calls: Arc<AtomicUsize>) -> Self {
            Self {
                name: Arc::from("splitting"),
                dtype: DType::Primitive(PType::I32, Nullability::NonNullable),
                row_count: 4,
                register_splits_calls,
                blocking_projection: None,
            }
        }

        fn with_blocking_projection(
            register_splits_calls: Arc<AtomicUsize>,
            gate: Arc<Mutex<()>>,
            started: mpsc::Sender<()>,
        ) -> Self {
            let mut reader = Self::new(register_splits_calls);
            reader.blocking_projection = Some(BlockingProjection { started, gate });
            reader
        }
    }

    impl LayoutReader for SplittingLayoutReader {
        fn name(&self) -> &Arc<str> {
            &self.name
        }

        fn dtype(&self) -> &DType {
            &self.dtype
        }

        fn row_count(&self) -> u64 {
            self.row_count
        }

        fn register_splits(
            &self,
            _field_mask: &[FieldMask],
            split_range: &SplitRange,
            splits: &mut RowSplits,
        ) -> VortexResult<()> {
            self.register_splits_calls.fetch_add(1, Ordering::Relaxed);
            for split in (split_range.row_range().start + 1)..=split_range.row_range().end {
                splits.push(split_range.row_offset() + split);
            }
            Ok(())
        }

        fn pruning_evaluation(
            &self,
            _row_range: &Range<u64>,
            _expr: &BoundExpression,
            mask: Mask,
        ) -> VortexResult<MaskFuture> {
            Ok(MaskFuture::ready(mask))
        }

        fn filter_evaluation(
            &self,
            _row_range: &Range<u64>,
            _expr: &BoundExpression,
            mask: MaskFuture,
        ) -> VortexResult<MaskFuture> {
            Ok(mask)
        }

        fn projection_evaluation(
            &self,
            row_range: &Range<u64>,
            _expr: &BoundExpression,
            _mask: MaskFuture,
        ) -> VortexResult<ArrayFuture> {
            if let Some(blocking_projection) = &self.blocking_projection {
                blocking_projection
                    .started
                    .send(())
                    .map_err(|_| vortex_err!("test projection-start receiver dropped"))?;
                let _guard = blocking_projection.gate.lock();
            }

            let start = usize::try_from(row_range.start)
                .map_err(|_| vortex_err!("row_range.start must fit in usize"))?;
            let end = usize::try_from(row_range.end)
                .map_err(|_| vortex_err!("row_range.end must fit in usize"))?;

            let values: VortexResult<Vec<i32>> = (start..end)
                .map(|v| i32::try_from(v).map_err(|_| vortex_err!("split value must fit in i32")))
                .collect();

            let array = PrimitiveArray::from_iter(values?).into_array();
            Ok(Box::pin(async move { Ok(array) }))
        }

        fn as_any(&self) -> &dyn std::any::Any {
            self
        }
    }

    #[test]
    fn into_stream_executes_after_prepare() -> VortexResult<()> {
        let mut ctx = array_session().create_execution_ctx();
        let calls = Arc::new(AtomicUsize::new(0));
        let reader = Arc::new(SplittingLayoutReader::new(Arc::clone(&calls)));

        let runtime = SingleThreadRuntime::default();
        let session = session_with_handle(runtime.handle());

        let stream = ScanBuilder::new(session, reader).into_stream()?;
        let mut iter = runtime.block_on_stream(stream);

        let mut values = Vec::new();
        for chunk in &mut iter {
            let prim = chunk?.execute::<PrimitiveArray>(&mut ctx)?;
            values.extend(prim.into_buffer::<i32>().iter().copied());
        }

        assert_eq!(calls.load(Ordering::Relaxed), 1);
        assert_eq!(values.as_ref(), [0, 1, 2, 3]);

        Ok(())
    }

    #[test]
    fn supplied_natural_splits_skip_layout_walk() -> VortexResult<()> {
        let mut ctx = array_session().create_execution_ctx();
        let calls = Arc::new(AtomicUsize::new(0));
        let reader = Arc::new(SplittingLayoutReader::new(Arc::clone(&calls)));

        let runtime = SingleThreadRuntime::default();
        let session = session_with_handle(runtime.handle());

        let stream = ScanBuilder::new(session, reader)
            .with_natural_splits(vec![0u64, 2, 4].into())
            .with_row_range(1..4)
            .into_stream()?;
        let mut iter = runtime.block_on_stream(stream);

        let mut chunks = Vec::new();
        for chunk in &mut iter {
            let prim = chunk?.execute::<PrimitiveArray>(&mut ctx)?;
            chunks.push(prim.into_buffer::<i32>().to_vec());
        }

        assert_eq!(calls.load(Ordering::Relaxed), 0);
        // Supplied full-file boundaries [0, 2, 4] clamped to rows 1..4.
        assert_eq!(chunks, [vec![1], vec![2, 3]]);

        Ok(())
    }

    #[rstest]
    #[case::eager(false)]
    #[case::filtered_limit(true)]
    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn scan_constructs_tasks_off_the_poller(#[case] filtered: bool) -> VortexResult<()> {
        let gate = Arc::new(Mutex::new(()));
        let guard = gate.lock();
        let calls = Arc::new(AtomicUsize::new(0));
        let (started_send, started_recv) = mpsc::channel();
        let reader = Arc::new(SplittingLayoutReader::with_blocking_projection(
            Arc::clone(&calls),
            Arc::clone(&gate),
            started_send,
        ));

        let runtime = TokioRuntime::new(tokio::runtime::Handle::current());
        let session = session_with_handle(runtime.handle());
        let filter = is_not_null(root()).bind(reader.dtype())?;
        let builder = ScanBuilder::new(session, reader);
        let mut stream = if filtered {
            // Drive the prepared stream directly so its first poll must construct a split.
            builder
                .with_filter(filter)
                .with_limit(1)
                .prepare()?
                .execute_stream(None)?
        } else {
            builder.into_stream()?.boxed()
        };

        let (poll_send, poll_recv) = mpsc::channel();
        let (release_send, release_recv) = mpsc::channel();
        let join = std::thread::spawn(move || {
            let waker = noop_waker_ref();
            let mut cx = Context::from_waker(waker);
            let poll = Pin::new(&mut stream).poll_next(&mut cx);
            let _ = poll_send.send(matches!(poll, Poll::Pending));
            let _ = release_recv.recv();
        });

        let poll_result = poll_recv.recv_timeout(Duration::from_secs(1));
        let projection_started = started_recv.recv_timeout(Duration::from_secs(1));

        // Release the task and join its caller before reporting a failed assertion.
        drop(guard);
        let _ = release_send.send(());
        drop(join.join());

        assert!(
            poll_result.is_ok_and(|poll_pending| poll_pending),
            "first poll must return while scan task construction is blocked"
        );
        projection_started
            .map_err(|_| vortex_err!("stream construction did not begin in the background"))?;
        assert_eq!(calls.load(Ordering::Relaxed), 1);

        Ok(())
    }

    #[tokio::test]
    async fn into_stream_reports_stream_construction_errors() -> VortexResult<()> {
        let range_start = i32::MAX as u64 + 1;
        let reader = Arc::new(SplittingLayoutReader::new(Arc::new(AtomicUsize::new(0))));
        let session = session_with_handle(TokioRuntime::current());
        let mut stream = ScanBuilder::new(session, reader)
            .with_row_range(range_start..range_start + 1)
            .into_stream()?;

        assert!(matches!(stream.next().await, Some(Err(_))));
        assert!(stream.next().await.is_none());

        Ok(())
    }

    #[rstest]
    #[case::limit_below_matches(8, keep_all, 3, &[0, 1, 2])]
    #[case::limit_zero(8, keep_all, 0, &[])]
    #[case::limit_exceeds_matches(8, keep_odd, 100, &[1, 3, 5, 7])]
    #[case::empty_input(0, keep_all, 3, &[])]
    fn filtered_limit_yields_expected_rows(
        #[case] row_count: u64,
        #[case] keep_row: fn(u64) -> bool,
        #[case] limit: u64,
        #[case] expected: &[i32],
    ) -> VortexResult<()> {
        let runtime = SingleThreadRuntime::default();
        let session = session_with_handle(runtime.handle());
        let reader = Arc::new(
            TestLayoutReader::new(row_count)
                .with_split_size(2)
                .with_keep_row(keep_row),
        );
        let filter = root().bind(reader.dtype())?;

        let stream = ScanBuilder::new(session, reader)
            .with_filter(filter)
            .with_limit(limit)
            .into_stream()?;
        let values = collect_scan_values(runtime.block_on_stream(stream))?;
        drain_runtime(&runtime);

        assert_eq!(values.as_slice(), expected);
        Ok(())
    }

    /// An ordered filtered limit takes rows in split order, so it returns the earliest matching
    /// rows even when later splits finish first.
    #[test]
    fn ordered_filtered_limit_returns_the_earliest_rows() -> VortexResult<()> {
        let runtime = SingleThreadRuntime::default();
        let session = session_with_handle(runtime.handle());
        let reader = Arc::new(TestLayoutReader::new(12).with_split_size(4));
        let filter = root().bind(reader.dtype())?;

        let stream = ScanBuilder::new(session, reader)
            .with_filter(filter)
            .with_limit(6)
            .into_stream()?;
        let values = collect_scan_values(runtime.block_on_stream(stream))?;
        drain_runtime(&runtime);

        assert_eq!(values, [0, 1, 2, 3, 4, 5]);
        Ok(())
    }

    /// `map` applies to every emitted split, including the trimmed output of a filtered limit.
    #[rstest]
    #[case::unlimited(None, 8)]
    #[case::limited(Some(3), 3)]
    fn map_applies_to_every_emitted_split(
        #[case] limit: Option<u64>,
        #[case] expected_rows: usize,
    ) -> VortexResult<()> {
        let runtime = SingleThreadRuntime::default();
        let session = session_with_handle(runtime.handle());
        let reader = Arc::new(TestLayoutReader::new(8).with_split_size(2));
        let filter = root().bind(reader.dtype())?;

        let rows = ScanBuilder::new(session, reader)
            .with_filter(filter)
            .with_some_limit(limit)
            .map(|array| Ok(array.len()))
            .into_iter(&runtime)?
            .sum::<VortexResult<usize>>()?;
        drain_runtime(&runtime);

        assert_eq!(rows, expected_rows);
        Ok(())
    }

    #[rstest]
    #[case::filter_error(true, None)]
    #[case::filter_error_with_limit(true, Some(2))]
    #[case::projection_error(false, None)]
    #[case::projection_error_with_limit(false, Some(2))]
    fn errors_end_the_scan(
        #[case] fail_filter: bool,
        #[case] limit: Option<u64>,
    ) -> VortexResult<()> {
        let runtime = SingleThreadRuntime::default();
        let session = session_with_handle(runtime.handle());
        let reader = TestLayoutReader::new(2).with_split_size(1);
        let reader = Arc::new(if fail_filter {
            reader.with_fail_first_filter()
        } else {
            reader.with_fail_first_projection()
        });
        let filter = root().bind(reader.dtype())?;
        let stream = ScanBuilder::new(session, reader)
            .with_filter(filter)
            .with_some_limit(limit)
            .into_stream()?;
        let mut iter = runtime.block_on_stream(stream);

        assert!(matches!(iter.next(), Some(Err(_))));
        assert!(iter.next().is_none());
        drop(iter);
        drain_runtime(&runtime);
        Ok(())
    }

    #[test]
    fn full_file_splits_ignore_row_range() -> VortexResult<()> {
        let calls = Arc::new(AtomicUsize::new(0));
        let reader = Arc::new(SplittingLayoutReader::new(Arc::clone(&calls)));

        let splits = ScanBuilder::new(SCAN_SESSION.clone(), reader)
            .with_row_range(1..3)
            .full_file_splits()?;

        assert_eq!(splits, [0, 1, 2, 3, 4]);

        Ok(())
    }

    #[test]
    fn projection_errors_are_stream_items() -> VortexResult<()> {
        let runtime = SingleThreadRuntime::default();
        let session = session_with_handle(runtime.handle());
        let projection_masks = Arc::new(Mutex::new(Vec::new()));
        let reader = Arc::new(
            TestLayoutReader::new(1)
                .with_projection_masks(Arc::clone(&projection_masks))
                .with_projection_error(),
        );
        let filter = root().bind(reader.dtype())?;
        let stream = ScanBuilder::new(session, reader)
            .with_filter(filter)
            .with_limit(1)
            .into_stream()?;
        let mut iter = runtime.block_on_stream(stream);

        assert!(matches!(iter.next(), Some(Err(_))));
        assert!(iter.next().is_none());
        assert_eq!(projection_masks.lock().as_slice(), [1]);
        Ok(())
    }

    #[test]
    fn prepared_scan_limits_filtered_results() -> VortexResult<()> {
        let runtime = SingleThreadRuntime::default();
        let session = session_with_handle(runtime.handle());
        let reader = Arc::new(
            TestLayoutReader::new(8)
                .with_split_size(2)
                .with_keep_row(keep_odd),
        );
        let filter = root().bind(reader.dtype())?;

        let scan = ScanBuilder::new(session, reader)
            .with_filter(filter)
            .with_limit(3)
            .prepare()?;
        let values = collect_scan_values(scan.execute_array_iter(None, &runtime)?)?;
        drain_runtime(&runtime);

        assert_eq!(values, [1, 3, 5]);
        Ok(())
    }

    #[test]
    fn scan_evaluates_filter_ranges_as_between() -> VortexResult<()> {
        let runtime = SingleThreadRuntime::default();
        let session = session_with_handle(runtime.handle());
        let filter_exprs = Arc::new(Mutex::new(Vec::new()));
        let dtype = nested_dtype();
        let reader = Arc::new(
            TestLayoutReader::new(4)
                .with_dtype(dtype.clone())
                .with_filter_exprs(Arc::clone(&filter_exprs)),
        );
        let filter = and(gt_eq(col("b"), lit(2_i32)), lt(col("b"), lit(5_i32))).bind(&dtype)?;

        let values = collect_scan_values(
            ScanBuilder::new(session, reader)
                .with_projection(col("b").bind(&dtype)?)
                .with_filter(filter)
                .into_array_iter(&runtime)?,
        )?;
        drain_runtime(&runtime);

        assert_eq!(values, [0, 1, 2, 3]);
        let expected = between(
            col("b"),
            lit(2_i32),
            lit(5_i32),
            BetweenOptions {
                lower_strict: StrictComparison::NonStrict,
                upper_strict: StrictComparison::Strict,
            },
        )
        .bind(&dtype)?;
        assert_eq!(filter_exprs.lock().as_slice(), [expected]);
        Ok(())
    }

    #[test]
    fn prepared_scan_split_futures_can_be_polled_in_reverse() -> VortexResult<()> {
        let runtime = SingleThreadRuntime::default();
        let session = session_with_handle(runtime.handle());
        let projection_masks = Arc::new(Mutex::new(Vec::new()));
        let reader = Arc::new(
            TestLayoutReader::new(8)
                .with_split_size(2)
                .with_keep_row(keep_odd)
                .with_projection_masks(Arc::clone(&projection_masks)),
        );
        let filter = root().bind(reader.dtype())?;
        let scan = ScanBuilder::new(session, reader)
            .with_filter(filter)
            .prepare()?;
        let tasks = scan.execute(Some(1..7))?;
        assert!(projection_masks.lock().is_empty());

        let mut arrays = Vec::new();
        for task in tasks.into_iter().rev() {
            if let Some(array) = runtime.block_on(task)? {
                arrays.push(Ok(array));
            }
        }
        assert_eq!(collect_scan_values(arrays)?, [5, 3, 1]);
        Ok(())
    }

    #[rstest]
    #[case(false)]
    #[case(true)]
    fn prepared_scan_split_futures_reject_limits(#[case] shared: bool) -> VortexResult<()> {
        let reader = Arc::new(TestLayoutReader::new(8).with_split_size(2));
        let builder = ScanBuilder::new(SCAN_SESSION.clone(), reader);
        let builder = if shared {
            builder.with_shared_limit(RowLimit::new(1))
        } else {
            builder.with_limit(1)
        };

        assert!(builder.prepare()?.execute(None).is_err());
        Ok(())
    }

    #[derive(Debug)]
    struct BlockingSplitsLayoutReader {
        name: Arc<str>,
        dtype: DType,
        row_count: u64,
        register_splits_calls: Arc<AtomicUsize>,
        gate: Arc<Mutex<()>>,
    }

    impl BlockingSplitsLayoutReader {
        fn new(gate: Arc<Mutex<()>>, register_splits_calls: Arc<AtomicUsize>) -> Self {
            Self {
                name: Arc::from("blocking-splits"),
                dtype: DType::Primitive(PType::I32, Nullability::NonNullable),
                row_count: 1,
                register_splits_calls,
                gate,
            }
        }
    }

    impl LayoutReader for BlockingSplitsLayoutReader {
        fn name(&self) -> &Arc<str> {
            &self.name
        }

        fn dtype(&self) -> &DType {
            &self.dtype
        }

        fn row_count(&self) -> u64 {
            self.row_count
        }

        fn register_splits(
            &self,
            _field_mask: &[FieldMask],
            split_range: &SplitRange,
            splits: &mut RowSplits,
        ) -> VortexResult<()> {
            self.register_splits_calls.fetch_add(1, Ordering::Relaxed);
            let _guard = self.gate.lock();
            splits.push(split_range.root_row_range().end);
            Ok(())
        }

        fn pruning_evaluation(
            &self,
            _row_range: &Range<u64>,
            _expr: &BoundExpression,
            _mask: Mask,
        ) -> VortexResult<MaskFuture> {
            vortex_bail!("not needed for this test");
        }

        fn filter_evaluation(
            &self,
            _row_range: &Range<u64>,
            _expr: &BoundExpression,
            _mask: MaskFuture,
        ) -> VortexResult<MaskFuture> {
            vortex_bail!("not needed for this test");
        }

        fn projection_evaluation(
            &self,
            _row_range: &Range<u64>,
            _expr: &BoundExpression,
            _mask: MaskFuture,
        ) -> VortexResult<ArrayFuture> {
            Ok(Box::pin(async move {
                unreachable!("scan should not be polled in this test")
            }))
        }

        fn as_any(&self) -> &dyn std::any::Any {
            self
        }
    }

    #[test]
    fn into_stream_first_poll_does_not_block() {
        let gate = Arc::new(Mutex::new(()));
        let guard = gate.lock();

        let calls = Arc::new(AtomicUsize::new(0));
        let reader = Arc::new(BlockingSplitsLayoutReader::new(
            Arc::clone(&gate),
            Arc::clone(&calls),
        ));

        let runtime = SingleThreadRuntime::default();
        let session = session_with_handle(runtime.handle());

        let mut stream = ScanBuilder::new(session, reader).into_stream().unwrap();

        let (send, recv) = mpsc::channel::<bool>();
        let join = std::thread::spawn(move || {
            let waker = noop_waker_ref();
            let mut cx = Context::from_waker(waker);
            let poll = Pin::new(&mut stream).poll_next(&mut cx);
            let _ = send.send(matches!(poll, Poll::Pending));
        });

        let polled_pending = recv.recv_timeout(Duration::from_secs(1)).ok();

        // Always release the gate and join the thread so failures don't hang the test process.
        drop(guard);
        drop(join.join());

        let polled_pending = polled_pending.expect("poll_next blocked; expected quick return");
        assert!(
            polled_pending,
            "expected Poll::Pending while prepare is blocked"
        );
        assert_eq!(calls.load(Ordering::Relaxed), 0);

        drop(runtime);
    }

    #[test]
    fn into_stream_with_row_range() -> VortexResult<()> {
        let mut ctx = array_session().create_execution_ctx();
        let calls = Arc::new(AtomicUsize::new(0));
        let reader = Arc::new(SplittingLayoutReader::new(Arc::clone(&calls)));

        let runtime = SingleThreadRuntime::default();
        let session = session_with_handle(runtime.handle());

        let stream = ScanBuilder::new(session, reader)
            .with_row_range(1..3)
            .into_stream()?;
        let mut iter = runtime.block_on_stream(stream);

        let mut values = Vec::new();
        for chunk in &mut iter {
            let prim = chunk?.execute::<PrimitiveArray>(&mut ctx)?;
            values.extend(prim.into_buffer::<i32>().iter().copied());
        }

        assert_eq!(calls.load(Ordering::Relaxed), 1);
        assert_eq!(values.as_ref(), [1, 2]);

        Ok(())
    }
}