datui-lib 0.4.7

Data Exploration in the Terminal (library)
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
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
//! The row buffer: planning which rows to read around the view, reading them (on a
//! worker, or here for tests), installing them, and the scroll that walks through them.

use super::*;

/// Parameters for a background buffer load. Produced by `prepare_async_collect()`.
pub struct CollectRequest {
    /// LazyFrame to collect (sliced to the buffer range, with column selection applied).
    pub lf: LazyFrame,
    /// Whether to use Polars streaming engine.
    pub polars_streaming: bool,
    /// Buffer start row in the full dataset.
    pub buffer_start: usize,
    /// Buffer end row in the full dataset.
    pub buffer_end: usize,
    /// How the worker fits the rows it reads to the buffer: [`FillPlan::fit`].
    pub plan: FillPlan,
}

/// What the fill-reading worker needs to make it the buffer: the adjoining rows on
/// hand, the view and the caps, as planned. A trim that would copy (see
/// `trim_rows`) runs here, off the UI thread; `apply_async_collect` installs the
/// result as is.
pub struct FillPlan {
    buffer_start: usize,
    buffer_end: usize,
    num_rows: usize,
    count_known: bool,
    /// Lines were still being indexed when the read was planned: a short read ends
    /// where the indexing had got to, not the file.
    indexing: bool,
    /// The rows on hand and their first row, when the fill is planned to be stitched
    /// on to them. Shared, not copied.
    held: Option<(DataFrame, usize)>,
    view_start: usize,
    view_len: usize,
    max_rows: usize,
    max_mb: usize,
}

impl FillPlan {
    /// Make the buffer from `df`, the planned range: stitched to adjoining rows on hand,
    /// then cut to the caps around the view.
    pub fn fit(mut self, df: DataFrame) -> CollectResult {
        let returned = df.height();
        let bytes_per_row = (returned > 0).then(|| (df.estimated_size() / returned).max(1));
        // A shape mismatch (the columns changed underneath) keeps the fetched rows alone.
        let (df, start, seam) = match self.held.take() {
            Some((mut held, held_start)) if held_start + held.height() == self.buffer_start => {
                let seam = held.height();
                match held.vstack_mut(&df) {
                    Ok(_) => (held, held_start, Some(seam)),
                    Err(_) => (df, self.buffer_start, None),
                }
            }
            Some((held, held_start))
                if returned > 0 && self.buffer_start + returned == held_start =>
            {
                match df.vstack(&held) {
                    Ok(joined) => (joined, self.buffer_start, Some(returned)),
                    Err(_) => (df, self.buffer_start, None),
                }
            }
            _ => (df, self.buffer_start, None),
        };
        let (df, start) = self.cut_to_caps(df, start, seam);
        let df = fewer_chunks(df);
        CollectResult {
            df,
            start,
            returned,
            bytes_per_row,
            buffer_start: self.buffer_start,
            buffer_end: self.buffer_end,
            num_rows: self.num_rows,
            count_known: self.count_known,
            indexing: self.indexing,
        }
    }

    /// Cut `df` (rows `[start, start + df.height())`) to the row cap and byte budget,
    /// centered on the view (a head cut would drop a late jump's rows). Returns the kept
    /// rows and their first row. The budget bounds rows held between collects, not the
    /// collect.
    fn cut_to_caps(&self, df: DataFrame, start: usize, seam: Option<usize>) -> (DataFrame, usize) {
        let total = df.height();
        if total == 0 {
            return (df, start);
        }
        // The row cap as well: a row group stitched on to the rows on hand can run over it.
        let mut max_rows = total;
        if self.max_rows > 0 {
            max_rows = max_rows.min(self.max_rows);
        }
        if self.max_mb > 0 {
            let bytes_per_row = (df.estimated_size() / total).max(1);
            max_rows = max_rows.min(self.max_mb * 1024 * 1024 / bytes_per_row);
        }
        let max_rows = max_rows.max(1);
        if max_rows >= total {
            return (df, start);
        }
        let view_off = self.view_start.saturating_sub(start).min(total);
        let view_len = self.view_len.max(1).min(total);
        let view_center = view_off + view_len / 2;
        let mut keep_start = view_center.saturating_sub(max_rows / 2);
        if keep_start + max_rows > total {
            keep_start = total - max_rows;
        }
        let kept = max_rows.min(total - keep_start);
        (trim_rows(df, keep_start, kept, seam), start + keep_start)
    }
}

/// Result of a background buffer load, made by [`FillPlan::fit`] on the worker and
/// installed as it is by `apply_async_collect()`.
pub struct CollectResult {
    /// The buffer: the rows read, stitched and cut to the caps.
    pub(super) df: DataFrame,
    /// The first row of `df`.
    pub(super) start: usize,
    /// Rows the read returned, before the stitch and the cut.
    returned: usize,
    /// Bytes per row of the rows read, to plan the next fill by.
    bytes_per_row: Option<usize>,
    /// The range the read was planned for.
    buffer_start: usize,
    buffer_end: usize,
    /// Row count for the full (unsliced) dataset. Only meaningful when `count_known`.
    num_rows: usize,
    /// Whether `num_rows` is the true total. False for a first buffer read before the
    /// background `len()` count has resolved; `num_rows` is then provisional.
    count_known: bool,
    /// See `FillPlan::indexing`.
    indexing: bool,
}

impl CollectResult {
    /// The rows read, as the buffer will hold them.
    pub(crate) fn rows(&self) -> &DataFrame {
        &self.df
    }
}

/// A string's in-memory width when nothing says otherwise: the view plus a short value.
pub(super) const STRING_BYTES_GUESS: usize = 40;

/// Estimated in-memory bytes per row of `columns`: fixed widths, strings by the footer
/// average in `column_bytes` (or a guess) plus their view, nested by footer average or
/// guess. Binary columns are buffered as a stub (`binary_stub_exprs`).
pub(super) fn estimate_bytes_per_row(
    schema: &Schema,
    columns: &[String],
    column_bytes: &[(String, usize)],
) -> usize {
    let footer_width = |name: &String| {
        column_bytes
            .iter()
            .find(|(n, _)| n == name)
            .map(|(_, w)| *w)
    };
    columns
        .iter()
        .map(|name| match schema.get(name.as_str()) {
            Some(DataType::String) => 16 + footer_width(name).unwrap_or(STRING_BYTES_GUESS - 16),
            Some(DataType::Binary) => 16 + binary_stub().len(),
            Some(DataType::Boolean) => 1,
            Some(DataType::Null) => 0,
            Some(dtype) if dtype.is_primitive_numeric() || dtype.is_temporal() => {
                match dtype.to_physical() {
                    DataType::Int8 | DataType::UInt8 => 1,
                    DataType::Int16 | DataType::UInt16 => 2,
                    DataType::Int32 | DataType::UInt32 | DataType::Float32 => 4,
                    DataType::Int128 => 16,
                    _ => 8,
                }
            }
            Some(DataType::Decimal(..)) => 16,
            _ => footer_width(name).unwrap_or(64),
        })
        .sum::<usize>()
        .max(1)
}

/// The most chunks a buffer column keeps: each fill stitched on adds one, and a column
/// of many small chunks is slower to read from.
pub(super) const MAX_BUFFER_CHUNKS: usize = 16;

/// `df` in one chunk per column once fills stitched on have left it in more than
/// [`MAX_BUFFER_CHUNKS`]. A copy, on the worker, once every so many fills.
fn fewer_chunks(df: DataFrame) -> DataFrame {
    let chunks = df
        .columns()
        .iter()
        .filter_map(Column::as_series)
        .map(|s| s.chunks().len())
        .max()
        .unwrap_or(0);
    if chunks <= MAX_BUFFER_CHUNKS {
        return df;
    }
    let rows = df.height();
    compact_rows(df, 0, rows, None)
}

/// Rows `[offset, offset + len)` of `df`, copied when a slice would keep much more
/// allocated (a single-chunk fill, a stitched union, shared string data), keeping a
/// `seam` as a chunk boundary (see [`compact_rows`]). A chunk that is itself a slice is
/// not seen through.
pub(super) fn trim_rows(
    df: DataFrame,
    offset: usize,
    len: usize,
    seam: Option<usize>,
) -> DataFrame {
    if backing_rows(&df, offset, len) > len + len / 4 {
        compact_rows(df, offset, len, seam)
    } else {
        df.slice(offset as i64, len)
    }
}

/// The most rows any column of `df` keeps allocated behind the slice `[offset, offset
/// + len)`: every chunk the slice touches, whole.
pub(super) fn backing_rows(df: &DataFrame, offset: usize, len: usize) -> usize {
    let end = offset + len;
    df.columns()
        .iter()
        .filter_map(Column::as_series)
        .map(|s| {
            let mut start = 0;
            let mut touched = 0;
            for chunk in s.chunks() {
                let chunk_end = start + chunk.len();
                if start < end && offset < chunk_end {
                    touched += chunk.len();
                }
                start = chunk_end;
            }
            touched
        })
        .max()
        .unwrap_or(len)
}

/// Rows `[offset, offset + len)` in their own storage: one chunk per column, two when
/// `seam` falls inside, so a later cut to one side lets the other go. Neither `rechunk`
/// nor `take` reliably releases the parent; builders with `ShareStrategy::Never` copy
/// everything. Constant columns stay one value. Each source column is released once
/// copied, bounding the extra memory to about one column.
pub(super) fn compact_rows(
    df: DataFrame,
    offset: usize,
    len: usize,
    seam: Option<usize>,
) -> DataFrame {
    use polars::series::builder::SeriesBuilder;
    use polars_arrow::array::builder::ShareStrategy;
    #[cfg(test)]
    tests::COMPACTIONS.with(|count| count.set(count.get() + 1));
    let len = len.min(df.height().saturating_sub(offset));
    let pieces = match seam.filter(|&seam| offset < seam && seam < offset + len) {
        Some(seam) => vec![(offset, seam - offset), (seam, offset + len - seam)],
        None => vec![(offset, len)],
    };
    let copy = |series: &Series, (offset, len): (usize, usize)| {
        let mut builder = SeriesBuilder::new(series.dtype().clone());
        builder.reserve(len);
        builder.subslice_extend(series, offset, len, ShareStrategy::Never);
        builder.freeze(series.name().clone())
    };
    let columns = df
        .into_columns()
        .into_iter()
        .map(|column| match column {
            Column::Scalar(constant) => {
                Column::new_scalar(constant.name().clone(), constant.scalar().clone(), len)
            }
            Column::Series(series) => {
                let mut kept = copy(&series, pieces[0]);
                for &piece in &pieces[1..] {
                    if kept.append_owned(copy(&series, piece)).is_err() {
                        kept = copy(&series, (offset, len));
                        break;
                    }
                }
                kept.into_column()
            }
        })
        .collect();
    // Cannot fail: the names are one frame's and every column was built to `len` rows.
    DataFrame::new(len, columns).unwrap_or_else(|_| DataFrame::empty_with_height(len))
}

/// Shrink `[buffer_start, buffer_end)` to at most `max_len` rows, kept around the view
/// `[view_start, view_end)` and inside `[floor, ceil)`.
pub(super) fn shrink_around_view(
    view_start: usize,
    view_end: usize,
    max_len: usize,
    floor: usize,
    ceil: usize,
    buffer_start: &mut usize,
    buffer_end: &mut usize,
) {
    if buffer_end.saturating_sub(*buffer_start) <= max_len {
        return;
    }
    let view_len = view_end.saturating_sub(view_start);
    if view_len >= max_len {
        *buffer_start = view_start;
        *buffer_end = (view_start + max_len).min(ceil);
        return;
    }
    let half = (max_len - view_len) / 2;
    *buffer_end = (view_end + half).min(ceil);
    *buffer_start = buffer_end.saturating_sub(max_len).max(floor);
    if *buffer_start > view_start {
        *buffer_start = view_start;
    }
    *buffer_end = (*buffer_start + max_len).min(ceil);
}

/// The most files one buffer read opens, beyond those the view itself spans.
pub(super) const MAX_FILES_PER_BUFFER: usize = 16;

/// Narrow `[start, end)` to at most `max_files` files, keeping every file the view
/// `[view_start, view_end)` lies in and adding the ones after it first.
pub(super) fn limit_files(
    offsets: &[usize],
    view_start: usize,
    view_end: usize,
    start: usize,
    end: usize,
    max_files: usize,
) -> (usize, usize) {
    let (Some((first, last)), Some((view_first, view_last))) = (
        files_holding(offsets, start, end.saturating_sub(start)),
        files_holding(
            offsets,
            view_start,
            view_end.saturating_sub(view_start).max(1),
        ),
    ) else {
        return (start, end);
    };
    // An empty file is not opened (see `window_of`), so it costs nothing to reach past.
    let opened = |from: usize, to: usize| (from..=to).filter(|&i| holds_rows(offsets, i)).count();
    if opened(first, last) <= max_files {
        return (start, end);
    }
    let (mut lo, mut hi) = (view_first.max(first), view_last.min(last));
    let mut files = opened(lo, hi);
    while files < max_files && (hi < last || lo > first) {
        if hi < last {
            hi += 1;
            files += usize::from(holds_rows(offsets, hi));
        }
        if files < max_files && lo > first {
            lo -= 1;
            files += usize::from(holds_rows(offsets, lo));
        }
    }
    (start.max(offsets[lo]), end.min(offsets[hi + 1]))
}

/// Whether file `i` has any rows, given where each file's rows start.
pub(super) fn holds_rows(offsets: &[usize], i: usize) -> bool {
    offsets[i + 1] > offsets[i]
}

/// The files from `first` to `last` that hold rows: a window reads these and passes
/// over the empty ones, which a dataset written a file a day can be mostly made of.
pub(super) fn files_with_rows(offsets: &[usize], first: usize, last: usize) -> Vec<usize> {
    (first..=last).filter(|&i| holds_rows(offsets, i)).collect()
}

/// The first and last files holding rows `[start, start + len)`, given where each file's
/// rows start (`offsets`, with the total last). `None` when the rows lie past the end.
pub(super) fn files_holding(offsets: &[usize], start: usize, len: usize) -> Option<(usize, usize)> {
    let files = offsets.len().checked_sub(1)?;
    let total = *offsets.last()?;
    if files == 0 || len == 0 || start >= total {
        return None;
    }
    let end = (start + len).min(total);
    // The file a row is in: the last one starting at or before it. Empty files start
    // where the next one does and are skipped over.
    let file_of = |row: usize| offsets.partition_point(|&o| o <= row).saturating_sub(1);
    Some((file_of(start), file_of(end - 1).min(files - 1)))
}

/// Where a window of a view reads its rows from, besides the frame itself.
#[derive(Clone, Copy, Default)]
pub(super) struct Sources<'a> {
    /// Counted remote files: a scan of only the files holding the window.
    pub(super) files: Option<&'a RemoteFiles>,
    /// Records read straight from the source.
    pub(super) records: Option<&'a dyn crate::formats::pushdown::Windowed>,
    /// Where the rows of a CSV start: the window reads on from the mark before it.
    pub(super) csv: Option<&'a Arc<super::csv_marks::CsvMarks>>,
}

/// Rows `[start, start + len)` of `lf` as `all_columns`, read from the narrowest of
/// `sources` that has them.
pub(super) fn window_of(
    lf: &LazyFrame,
    sources: Sources<'_>,
    read_as_text: &[PlSmallStr],
    start: usize,
    len: usize,
    all_columns: Vec<Expr>,
) -> PolarsResult<LazyFrame> {
    let Sources {
        files,
        records,
        csv,
    } = sources;
    // Polars gives an anonymous scan no row offset, so a slice deep in the view would
    // read every row before it; the source starts the window there instead.
    if let Some(records) = records {
        return Ok(records.window(start, len)?.select(all_columns));
    }
    if let Some(window) = csv.and_then(|marks| marks.window(lf, start, len)) {
        return Ok(window.select(all_columns));
    }
    if let Some((files, offsets)) = files.and_then(|f| f.offsets.as_ref().map(|o| (f, o)))
        && let Some((first, last)) = files_holding(offsets, start, len)
    {
        // The window's first file holds its first row, so leaving out the empty files
        // after it does not move the slice.
        let urls: Vec<String> = files_with_rows(offsets, first, last)
            .into_iter()
            .map(|i| files.urls[i].clone())
            .collect();
        let lf = (files.scan)(&urls, read_as_text)?;
        return Ok(lf
            .select(all_columns)
            .slice((start - offsets[first]) as i64, len as u32));
    }
    Ok(lf
        .clone()
        .select(all_columns)
        .slice(start as i64, len as u32))
}

/// The rows of a view, for a reader off the UI thread: read a window at a time as a
/// page is, or from the buffer the table already holds.
#[derive(Clone)]
pub(crate) struct ViewRows {
    lf: LazyFrame,
    files: Option<RemoteFiles>,
    /// See [`DataTableState::window_now`].
    records: Option<Arc<dyn crate::formats::pushdown::Windowed>>,
    /// See [`DataTableState::csv_window`].
    csv: Option<Arc<super::csv_marks::CsvMarks>>,
    read_as_text: Vec<PlSmallStr>,
    /// The buffer on hand and the view row it starts at.
    pub(crate) buffer: Option<(DataFrame, usize)>,
    /// The view's row count, when it is known.
    pub(crate) num_rows: Option<usize>,
    pub(crate) streaming: bool,
    /// Any window of the view reads all of it: see [`sees_every_row_first`].
    pub(crate) whole: bool,
    /// A window of the view reads every row before it: see [`reads_up_to_a_window`].
    pub(crate) reads_up_to: bool,
}

/// Whether `lf` has to see every row before it gives its first: a sort, a group by or a
/// pivot under it. Then a window of it costs as much as all of it.
pub(crate) fn sees_every_row_first(lf: &LazyFrame) -> bool {
    use polars::lazy::dsl::DslPlan;
    lf.logical_plan.into_iter().any(|node| {
        matches!(
            node,
            DslPlan::Sort { .. } | DslPlan::GroupBy { .. } | DslPlan::Pivot { .. }
        )
    })
}

/// Whether a window of `lf` reads every row before it: a filter (to find the first
/// match) or a scan with no row index to skip by (CSV). Parquet and IPC skip.
pub(crate) fn reads_up_to_a_window(lf: &LazyFrame) -> bool {
    use polars::lazy::dsl::{DslPlan, FileScanDsl};
    lf.logical_plan.into_iter().any(|node| match node {
        DslPlan::Filter { .. } => true,
        DslPlan::Scan { scan_type, .. } => !matches!(
            **scan_type,
            FileScanDsl::Parquet { .. } | FileScanDsl::Ipc { .. }
        ),
        _ => false,
    })
}

impl ViewRows {
    /// Rows `[start, start + len)` of the view as `exprs`.
    pub(crate) fn window(
        &self,
        start: usize,
        len: usize,
        exprs: Vec<Expr>,
    ) -> PolarsResult<LazyFrame> {
        window_of(
            &self.lf,
            Sources {
                files: self.files.as_ref(),
                records: self.records.as_deref(),
                csv: self.csv.as_ref(),
            },
            &self.read_as_text,
            start,
            len,
            exprs,
        )
    }

    /// The view `lf`, with `buffer` on hand from row `buffer_start`.
    #[cfg(test)]
    pub(crate) fn of(lf: LazyFrame, buffer: Option<(DataFrame, usize)>) -> Self {
        Self {
            whole: sees_every_row_first(&lf),
            reads_up_to: reads_up_to_a_window(&lf),
            lf,
            files: None,
            records: None,
            csv: None,
            read_as_text: Vec::new(),
            buffer,
            num_rows: None,
            streaming: false,
        }
    }
}

/// Rows a fill reads ahead of the view, at most, when a read decodes whole data pages:
/// a read of this many costs about what one page does.
pub(super) const PAGES_DECODED_AHEAD: usize = 8192;

/// Bytes of rows a fill reads ahead of the view, at most, when a read decodes whole data
/// pages: rows of long text read ahead fewer.
pub(super) const BYTES_DECODED_AHEAD: usize = 8 << 20;

/// Whether `lf` reads local Parquet files: a read of it decodes whole data pages.
pub(super) fn decodes_pages(lf: &LazyFrame) -> bool {
    use polars::lazy::dsl::{DslPlan, FileScanDsl, ScanSources};
    let remote = |path: &str| crate::cloud::source::is_remote_url(Path::new(path));
    (&lf.logical_plan).into_iter().any(|node| match node {
        DslPlan::Scan {
            sources: ScanSources::Paths(paths),
            scan_type,
            ..
        } => {
            matches!(**scan_type, FileScanDsl::Parquet { .. })
                && !paths.iter().any(|path| remote(path.as_str()))
        }
        _ => false,
    })
}

/// Snap `[start, end)` outward to whole row groups (`offsets`, total last). The groups
/// the view lies in are always whole (Polars fetches whole groups, so paging inside is
/// free); others the window reaches are added within `cap` rows (0: none), ahead of the
/// view first.
pub(super) fn align_to_row_groups(
    offsets: &[usize],
    view_start: usize,
    view_end: usize,
    start: usize,
    end: usize,
    cap: usize,
) -> (usize, usize) {
    let Some(groups) = offsets.len().checked_sub(1).filter(|n| *n > 0) else {
        return (start, end);
    };
    let group_of = |row: usize| {
        offsets
            .partition_point(|&o| o <= row)
            .saturating_sub(1)
            .min(groups - 1)
    };
    let last_row = |s: usize, e: usize| e.saturating_sub(1).max(s);
    let (mut lo, mut hi) = (
        group_of(view_start),
        group_of(last_row(view_start, view_end)),
    );
    let (want_lo, want_hi) = (group_of(start), group_of(last_row(start, end)));
    let fits = |lo: usize, hi: usize| cap == 0 || offsets[hi + 1] - offsets[lo] <= cap;
    loop {
        if hi < want_hi && fits(lo, hi + 1) {
            hi += 1;
        } else if lo > want_lo && fits(lo - 1, hi) {
            lo -= 1;
        } else {
            break;
        }
    }
    (offsets[lo], offsets[hi + 1])
}

impl DataTableState {
    /// Returns true if a scroll by `rows` would trigger a collect (view would leave the buffer).
    /// Used so the UI only shows the throbber when actual data loading will occur.
    pub fn scroll_would_trigger_collect(&self, rows: i64) -> bool {
        if rows < 0 && self.view.start_row == 0 {
            return false;
        }
        let new_start_row = if self.view.start_row as i64 + rows <= 0 {
            0
        } else {
            if let Some(df) = self.view.df.as_ref()
                && rows > 0
                && df.shape().0 <= self.visible_rows
            {
                return false;
            }
            let unclamped = (self.view.start_row as i64 + rows) as usize;
            if rows > 0 {
                unclamped.min(self.view.num_rows.saturating_sub(self.visible_rows))
            } else {
                unclamped
            }
        };
        if new_start_row == self.view.start_row {
            return false;
        }
        let view_end = new_start_row
            + self
                .visible_rows
                .min(self.view.num_rows.saturating_sub(new_start_row));
        let within_buffer = new_start_row >= self.view.buffered_start_row
            && view_end <= self.view.buffered_end_row
            && self.view.buffered_end_row > 0;
        !within_buffer
    }

    /// Scroll by `rows`: within the buffer, re-slice the display; outside it, set the
    /// position and return true so the caller collects.
    pub fn slide_table(&mut self, rows: i64) -> bool {
        if rows < 0 && self.view.start_row == 0 {
            return false;
        }

        let new_start_row = if self.view.start_row as i64 + rows <= 0 {
            0
        } else {
            if let Some(df) = self.view.df.as_ref()
                && rows > 0
                && df.shape().0 <= self.visible_rows
            {
                return false;
            }
            let unclamped = (self.view.start_row as i64 + rows) as usize;
            if rows > 0 {
                // Keep a screen of data in view: otherwise held PageDown at the bottom pushes past
                // `num_rows`, repeatedly asking for no-op collects.
                unclamped.min(self.view.num_rows.saturating_sub(self.visible_rows))
            } else {
                unclamped
            }
        };

        if new_start_row == self.view.start_row {
            return false;
        }

        let view_end = new_start_row
            + self
                .visible_rows
                .min(self.view.num_rows.saturating_sub(new_start_row));
        let within_buffer = new_start_row >= self.view.buffered_start_row
            && view_end <= self.view.buffered_end_row
            && self.view.buffered_end_row > 0;

        self.view.start_row = new_start_row;

        if within_buffer {
            if self.table_state.selected().is_none() {
                self.table_state.select(Some(0));
            }
            false
        } else {
            true // caller must collect
        }
    }

    /// Read the view's rows synchronously, as a job does for the app; for tests only.
    #[cfg(test)]
    pub fn collect(&mut self) {
        if self.defer_collect {
            return;
        }
        if !self.view.num_rows_valid {
            // A count that fails means the frame itself is broken: say so rather than
            // draw it as empty.
            match collect_lazy(row_count_lf(&self.view.lf), self.polars_streaming) {
                Ok(df) => {
                    self.error = None;
                    let n = match df.get(0).as_deref().and_then(|row| row.first()) {
                        Some(AnyValue::UInt64(len)) => *len as usize,
                        _ => 0,
                    };
                    self.set_num_rows(n);
                }
                Err(e) => {
                    self.error = Some(e);
                    self.set_num_rows(0);
                }
            }
        }
        let Some(request) = self.prepare_async_collect(None) else {
            return;
        };
        match collect_lazy(request.lf, request.polars_streaming) {
            Ok(df) => self.apply_async_collect(request.plan.fit(df)),
            Err(e) => self.error = Some(e),
        }
    }

    /// In the app a mutation asks for its rows: the event loop reads them on a job
    /// after the next frame. Under [`Self::deferred`] the caller reads them itself.
    #[cfg(not(test))]
    pub(super) fn collect(&mut self) {
        if !self.defer_collect {
            self.needs_recollect = true;
        }
    }

    /// Expressions for every column in `column_order`, binary columns replaced by a stub
    /// ([`binary_stub`]) so blobs are never read, for the display buffer and analysis
    /// (multi-GB blobs would exhaust memory). `lf` keeps the bytes for export.
    pub(crate) fn binary_stub_exprs(&self) -> Vec<Expr> {
        self.view
            .column_order
            .iter()
            .map(|name| {
                if matches!(self.view.schema.get(name.as_str()), Some(DataType::Binary)) {
                    lit(binary_stub()).alias(name.as_str())
                } else {
                    col(name.as_str())
                }
            })
            .collect()
    }

    /// Plan an async collect without blocking: clamp the start row, then a `CollectRequest`
    /// if the rows on screen need a new buffer, or `None` (display slices updated). Without
    /// `num_rows_override`, `Self::num_rows_bound` lets the first rows skip the count.
    pub fn prepare_async_collect(
        &mut self,
        num_rows_override: Option<usize>,
    ) -> Option<CollectRequest> {
        if self.visible_rows > 0 {
            self.proximity_threshold = self.proximity();
        }

        if let Some(n) = num_rows_override {
            self.view.num_rows = n;
            self.view.num_rows_valid = true;
        }

        // `bound` is the total, or `usize::MAX` while `len()` runs, planning a top-of-data
        // window without waiting. See `num_rows_bound`.
        let count_known = self.view.num_rows_valid;
        let bound = self.num_rows_bound();

        if count_known {
            if self.view.num_rows > 0 {
                let max_start = self.view.num_rows.saturating_sub(1);
                if self.view.start_row > max_start {
                    self.view.start_row = max_start;
                }
            } else {
                // Confirmed-empty dataset: clear everything.
                self.view.start_row = 0;
                self.drop_buffer();
                self.view.df = None;
                self.view.locked_df = None;
                return None;
            }
        }

        // No column shown: there are no rows to read, and a read of none would come
        // back empty and ask again.
        if self.view.column_order.is_empty() {
            self.drop_buffer();
            self.view.df = None;
            self.view.locked_df = None;
            return None;
        }

        let view_start = self.view.start_row;
        let view_end = self.view.start_row + self.visible_rows.min(bound - self.view.start_row);
        let within_buffer = view_start >= self.view.buffered_start_row
            && view_end <= self.view.buffered_end_row
            && self.view.buffered_end_row > 0;

        // Compute the buffer range using the same logic as collect().
        let (new_buffer_start, new_buffer_end) = if within_buffer {
            let dist_to_start = view_start.saturating_sub(self.view.buffered_start_row);
            let dist_to_end = self.view.buffered_end_row.saturating_sub(view_end);
            let needs_expansion_back =
                dist_to_start <= self.proximity_behind() && self.view.buffered_start_row > 0;
            let needs_expansion_forward =
                dist_to_end <= self.proximity_threshold && self.view.buffered_end_row < bound;

            if !needs_expansion_back && !needs_expansion_forward {
                // Buffer is fine, just re-slice display.
                (self.view.buffered_start_row, self.view.buffered_end_row)
            } else {
                let mut s = if needs_expansion_back {
                    view_start.saturating_sub(self.reach_behind())
                } else {
                    self.view.buffered_start_row
                };
                let mut e = if needs_expansion_forward {
                    (view_end + self.reach_ahead()).min(bound)
                } else {
                    self.view.buffered_end_row
                };
                self.fit_window(view_start, view_end, &mut s, &mut e);
                (s, e)
            }
        } else {
            let had_buffer = self.view.buffered_end_row > 0;
            let scrolled_past_end = had_buffer && view_start >= self.view.buffered_end_row;
            let scrolled_past_start = had_buffer && view_end <= self.view.buffered_start_row;
            let extend_forward_ok = scrolled_past_end
                && (view_start - self.view.buffered_end_row) <= self.reach_ahead();
            let extend_backward_ok = scrolled_past_start
                && (self.view.buffered_start_row - view_end) <= self.reach_behind();

            let mut s;
            let mut e;
            if extend_forward_ok {
                s = self.view.buffered_start_row;
                e = (view_end + self.reach_ahead()).min(bound);
            } else if extend_backward_ok {
                s = view_start.saturating_sub(self.reach_behind());
                e = self.view.buffered_end_row;
            } else {
                s = view_start.saturating_sub(self.reach_behind());
                e = (view_end + self.reach_ahead()).min(bound);
                let min_initial_len = self.min_buffer_len();
                let current_len = e.saturating_sub(s);
                if current_len < min_initial_len {
                    let need = min_initial_len.saturating_sub(current_len);
                    let can_extend_end = bound.saturating_sub(e);
                    let can_extend_start = s;
                    if can_extend_end >= need {
                        e = (e + need).min(bound);
                    } else if can_extend_start >= need {
                        s = s.saturating_sub(need);
                    } else {
                        e = (e + can_extend_end).min(bound);
                        s = s.saturating_sub(need.saturating_sub(can_extend_end));
                    }
                }
            }
            self.fit_window(view_start, view_end, &mut s, &mut e);
            (s, e)
        };

        let buffer_size = new_buffer_end.saturating_sub(new_buffer_start);
        if buffer_size == 0 {
            return None;
        }
        // Already held: the view fits, or fitting the expansion to whole row groups
        // gave back the group on hand.
        if self.holds_buffer(new_buffer_start, new_buffer_end) {
            self.slice_buffer_into_display();
            if self.table_state.selected().is_none() {
                self.table_state.select(Some(0));
            }
            return None;
        }

        let lf = match self.buffer_lf(new_buffer_start, buffer_size) {
            Ok(lf) => lf,
            Err(e) => {
                self.error = Some(e);
                return None;
            }
        };

        // Unknown count: `num_rows` is provisional (this buffer's end); the background `len()`
        // corrects it unless a short read reveals the end.
        let num_rows = if count_known {
            self.view.num_rows
        } else {
            new_buffer_end
        };
        Some(CollectRequest {
            lf,
            polars_streaming: self.polars_streaming,
            buffer_start: new_buffer_start,
            buffer_end: new_buffer_end,
            plan: self.fill_plan(new_buffer_start, new_buffer_end, num_rows, count_known),
        })
    }

    /// How a fill of `[buffer_start, buffer_end)` is to be made the buffer, from what
    /// is held and shown now. See [`FillPlan`].
    pub(super) fn fill_plan(
        &self,
        buffer_start: usize,
        buffer_end: usize,
        num_rows: usize,
        count_known: bool,
    ) -> FillPlan {
        let held = self
            .abuts_buffer(buffer_start, buffer_end.saturating_sub(buffer_start))
            .then(|| self.view.buffered_df.clone())
            .flatten()
            .map(|df| (df, self.view.buffered_start_row));
        FillPlan {
            buffer_start,
            buffer_end,
            num_rows,
            count_known,
            indexing: self.indexing().is_some(),
            held,
            view_start: self.view.start_row,
            view_len: self.visible_rows,
            max_rows: self.max_buffered_rows,
            max_mb: self.max_buffered_mb,
        }
    }

    /// Apply the result of a background buffer load. The worker has already stitched
    /// and cut it ([`FillPlan::fit`]): installing it copies nothing.
    pub fn apply_async_collect(&mut self, result: CollectResult) {
        let CollectResult {
            df,
            start,
            returned: returned_rows,
            bytes_per_row,
            buffer_start,
            buffer_end,
            num_rows,
            count_known,
            indexing,
        } = result;
        let requested_rows = buffer_end.saturating_sub(buffer_start);

        if count_known {
            self.view.num_rows = num_rows;
            self.view.num_rows_valid = true;
        } else if returned_rows < requested_rows
            && (buffer_start == 0 || returned_rows > 0)
            // Lines still being indexed end where the indexing has got to, not the file.
            && !indexing
            && self.indexing().is_none()
        {
            // A short read that began inside the data gives the exact total; an empty slice deep
            // in the frame may lie past the data, so only the count can say.
            self.view.num_rows = buffer_start + returned_rows;
            self.view.num_rows_valid = true;
        } else if !self.view.num_rows_valid {
            // A full buffer with the count pending: a provisional total (at least this buffer's
            // end), corrected by `count_landed()`.
            self.view.num_rows = self.view.num_rows.max(buffer_end);
        }
        // else: the background len() already resolved the exact count between this
        // buffer being requested and applied — keep it; don't downgrade to provisional.
        self.error = None;
        self.remember_pristine_count();

        if bytes_per_row.is_some() {
            self.view.observed_bytes_per_row = bytes_per_row;
        }
        // A fill without the view's first row was planned for replaced rows: keep what is held
        // and replan. One holding the first row but not the whole view (a resize) is kept and
        // the rest fetched. After a short read, a view past the end shows only rows up to it.
        let end = start + df.height();
        let view_end = self.view.start_row + self.visible_rows.max(1);
        let reaches_end = end >= buffer_start + returned_rows;
        let shows_view = start <= self.view.start_row
            && (self.view.start_row < end || (returned_rows < requested_rows && reaches_end));
        if !shows_view {
            self.needs_recollect = true;
            return;
        }
        self.release_display_buffer();
        self.view.buffered_start_row = start;
        self.view.buffered_end_row = end;
        self.view.buffered_df = Some(df);
        // Slice the buffered DataFrame into display DataFrames (locked + scroll columns).
        self.slice_buffer_into_display();
        if self.table_state.selected().is_none() {
            self.table_state.select(Some(0));
        }
        if view_end > end && end < self.view.num_rows {
            self.needs_recollect = true;
        }
    }

    /// True when `rows` rows fetched from `start` run on from the rows on hand or up to
    /// them, so a fill of them is planned to be stitched on (see [`FillPlan`]).
    fn abuts_buffer(&self, start: usize, rows: usize) -> bool {
        self.stitches_buffer()
            && (start == self.view.buffered_end_row || start + rows == self.view.buffered_start_row)
    }

    /// A view of `sample`, drawn from `source` into `rows`, with `schema`'s columns. It
    /// starts empty and grows via [`Self::sample_grew`]. `through`: drawn from the view's
    /// query or filters rather than the source.
    pub(crate) fn sampled_from(
        source: DataTableState,
        sample: crate::analysis::sampling::Sample,
        schema: &Schema,
        rows: Arc<crate::analysis::table_sample::SampleRows>,
        through: bool,
        path: Option<crate::analysis::table_sample::DrawPath>,
    ) -> Result<Self> {
        let mut view = source.sample_view(DataFrame::empty_with_schema(schema))?;
        let frame = scanned_frame(&view.original_lf)
            .ok_or_else(|| color_eyre::eyre::eyre!("a sample's frame has no rows to scan"))?;
        view.sampled = Some(Box::new(Sampled {
            source: Box::new(source),
            sample,
            rows,
            frame,
            through,
            drawn: None,
            path,
        }));
        Ok(view)
    }

    /// The view's sample, while it has one.
    pub fn sampled(&self) -> Option<&Sampled> {
        self.sampled.as_deref()
    }

    /// The view the sample was drawn from, or this one when it has none: where a new
    /// sample is drawn from.
    pub fn unsampled(&self) -> &DataTableState {
        self.sampled
            .as_ref()
            .map_or(self, |sampled| sampled.source.as_ref())
    }

    /// The view the sample was drawn from, putting the sample down; `self` when it
    /// has none.
    pub(crate) fn into_unsampled(mut self) -> DataTableState {
        match self.sampled.take() {
            Some(sampled) => *sampled.source,
            None => self,
        }
    }

    /// Take the chunks drawn since the last call into every frame (so query, filters and
    /// sort run over them). `None` if none; otherwise whether the rows on hand still stand
    /// (they do while nothing reorders, since new rows come after).
    pub(crate) fn sample_grew(&mut self) -> Option<bool> {
        let sampled = self.sampled.as_ref()?;
        let chunks = sampled.rows.take_new();
        if chunks.is_empty() {
            return None;
        }
        // On the same buffers: each column takes the chunks' arrays, nothing copied.
        let mut frame = (*sampled.frame).clone();
        for chunk in &chunks {
            frame.vstack_mut(chunk).ok()?;
        }
        Some(self.rebind_sample(Arc::new(frame), false))
    }

    /// The draw ended, having read what `drawn` says: the rows go into the order the
    /// source holds them, once.
    pub(crate) fn sample_drawn(&mut self, drawn: crate::analysis::table_sample::Drawn) {
        let Some(sampled) = self.sampled.as_mut() else {
            return;
        };
        // One chunk per column from here: the many the draw left would slow every
        // read, and the chunks are let go so the rows are held once.
        let ordered = sampled.rows.take_in_source_order().ok().flatten();
        // A seeded read of one file needs no path; it was not one, then.
        sampled.path = drawn.path;
        sampled.drawn = Some(drawn);
        if let Some(frame) = ordered {
            self.rebind_sample(Arc::new(frame), true);
        }
    }

    /// Every frame scans `frame` in place of the sample's last one. `reordered` when
    /// the rows already shown changed places. Returns whether the rows on hand stand.
    fn rebind_sample(&mut self, frame: Arc<DataFrame>, reordered: bool) -> bool {
        let Some(old) = self.sampled.as_ref().map(|sampled| sampled.frame.clone()) else {
            return false;
        };
        let rows_stand = !reordered
            && self.view.sort_columns.is_empty()
            && self.view.sort_ascending
            && self.scan_is_the_root();
        let rows = frame.height();
        self.each_frame(|lf| {
            crate::analysis::table_sample::rebind(&mut lf.logical_plan, &old, &frame)
        });
        if let Some(sampled) = self.sampled.as_mut() {
            sampled.frame = frame;
        }
        self.invalidate_num_rows();
        if self.is_pristine() {
            self.set_num_rows(rows);
        } else if self.scan_is_the_root() {
            self.pristine_rows = Some(rows);
        }
        if !rows_stand {
            self.drop_buffer();
        }
        self.needs_recollect = true;
        rows_stand
    }

    /// Bytes per row of a sample of this view: every column of the source (from source) or
    /// the view, shown or not.
    pub(crate) fn sample_row_bytes(&self, from_source: bool) -> usize {
        let schema = if from_source {
            &self.original_schema
        } else {
            &self.view.schema
        };
        let columns: Vec<String> = schema
            .iter_names()
            .filter(|name| name.as_str() != crate::formats::schema_union::DRIFT_COLUMN)
            .map(|name| name.to_string())
            .collect();
        // What the table measured, when it measured these columns.
        if !from_source && columns.len() == self.view.column_order.len() {
            return self.bytes_per_row();
        }
        estimate_bytes_per_row(schema, &columns, &self.column_bytes)
    }

    /// How many files a page at `start` would read: only for a windowed remote scan;
    /// `None` (not zero) when Polars reads the whole scan as it decides.
    pub fn files_a_page_reads(&self, start: usize, len: usize) -> Option<usize> {
        let offsets = self.files_window().and_then(|f| f.offsets.as_ref())?;
        let (first, last) = files_holding(offsets, start, len)?;
        Some(files_with_rows(offsets, first, last).len())
    }

    /// The frame for buffer rows `[start, start + len)`, columns in display order. For a
    /// remote dataset whose files are counted, a scan of only the files holding them.
    pub(super) fn buffer_lf(&self, start: usize, len: usize) -> PolarsResult<LazyFrame> {
        let mut all_columns = self.binary_stub_exprs();
        if self.carries_source_rows() {
            all_columns.push(col(crate::formats::schema_union::DRIFT_COLUMN));
        }
        self.window_lf(start, len, all_columns)
    }

    /// Whether rows carry their source position for `#`: file-traced rows or lines while
    /// the frame is the scan's (query, reshape and group rows do not).
    pub(crate) fn carries_source_rows(&self) -> bool {
        self.view.drift_column_present
            || (self.scan_is_the_root() && (self.source_rows_at_open || self.view.view_numbered))
    }

    /// What `#` shows for `rows` rows from `start`: source positions where carried, else
    /// view positions from `row_start_index` (the same when pristine).
    pub fn row_numbers_from(&self, start: usize, rows: usize) -> Vec<usize> {
        let view = |i: usize| start + i + self.row_start_index;
        let places = self
            .view
            .buffered_df
            .as_ref()
            .filter(|_| self.carries_source_rows())
            .and_then(|df| df.column(crate::formats::schema_union::DRIFT_COLUMN).ok())
            .and_then(|column| {
                let offset = start.checked_sub(self.view.buffered_start_row)?;
                let len = rows.min(column.len().saturating_sub(offset));
                let slice = column.slice(offset as i64, len);
                let places = slice.u32().ok()?;
                // Several files' lines are numbered in their own file.
                let place = |p: usize| {
                    self.numbering
                        .as_ref()
                        .and_then(|lines| lines.line_in_file(p))
                        .unwrap_or(p)
                };
                Some(
                    places
                        .iter()
                        .map(|p| p.map(|p| place(p as usize) + self.row_start_index))
                        .collect::<Vec<_>>(),
                )
            });
        (0..rows)
            .map(|i| {
                places
                    .as_ref()
                    .and_then(|p| p.get(i).copied().flatten())
                    .unwrap_or_else(|| view(i))
            })
            .collect()
    }

    /// The frame for rows `[start, start + len)` of the view, as `all_columns`. For a
    /// remote dataset whose files are counted, a scan of only the files holding them.
    pub(super) fn window_lf(
        &self,
        start: usize,
        len: usize,
        all_columns: Vec<Expr>,
    ) -> PolarsResult<LazyFrame> {
        let records = self.window_now();
        window_of(
            &self.view.lf,
            Sources {
                files: self.files_window(),
                records: records.as_deref(),
                csv: self.csv_window(),
            },
            &self.read_as_text,
            start,
            len,
            all_columns,
        )
    }

    /// The view's rows as a find reads them: a window at a time, the way a page is
    /// read, and the buffer already on hand.
    pub(crate) fn view_rows(&self) -> ViewRows {
        ViewRows {
            lf: self.view.lf.clone(),
            files: self.files_window().cloned(),
            // A find reads every row it can reach: lines still being indexed are read
            // through the frame, which waits for them, not the window of those so far.
            records: self.window_now().filter(|_| self.indexing().is_none()),
            csv: self.csv_window().cloned(),
            read_as_text: self.read_as_text.clone(),
            buffer: self
                .view
                .buffered_df
                .as_ref()
                .filter(|_| self.buffer_on_hand())
                .map(|df| (df.clone(), self.view.buffered_start_row)),
            num_rows: self.view.num_rows_valid.then_some(self.view.num_rows),
            streaming: self.polars_streaming,
            whole: sees_every_row_first(&self.view.lf),
            reads_up_to: reads_up_to_a_window(&self.view.lf),
        }
    }

    /// Center the cursor on view row `row`, where a find matched; true if a collect is
    /// needed. A row past a provisional total extends it until the count lands.
    pub(crate) fn go_to_found_row(&mut self, row: usize) -> bool {
        if !self.view.num_rows_valid && self.view.num_rows <= row {
            self.view.num_rows = row + 1;
        }
        self.scroll_to_row_centered(row)
    }

    /// The view row the cursor is on.
    pub(crate) fn cursor_row(&self) -> usize {
        self.view.start_row + self.table_state.selected().unwrap_or(0)
    }

    /// Bytes a buffered row takes: measured on the last buffer collected, or until
    /// then estimated from the schema.
    pub(super) fn bytes_per_row(&self) -> usize {
        self.view.observed_bytes_per_row.unwrap_or_else(|| {
            estimate_bytes_per_row(
                &self.view.schema,
                &self.view.column_order,
                &self.column_bytes,
            )
        })
    }

    /// The best in-memory width estimate per row, shared by buffer planning and Data
    /// Quality's (approximate) preflight so they agree.
    pub fn estimated_row_bytes(&self) -> usize {
        self.bytes_per_row()
    }

    /// Number of source files known to participate in the pristine dataset scan.
    /// Returns `None` after a query or reshape has broken the row-to-file mapping.
    pub fn source_file_count(&self) -> Option<usize> {
        self.is_pristine().then(|| self.loaded_file_count())
    }

    /// Files the dataset was loaded from, whatever the view does with their rows.
    pub(crate) fn loaded_file_count(&self) -> usize {
        if !self.drift_files.is_empty() {
            self.drift_files.len()
        } else if let Some(remote) = &self.remote_files {
            remote.urls.len()
        } else {
            1
        }
    }

    /// Rows the `max_buffered_mb` budget allows, at least a screen; 0 for none. Planned to,
    /// so a wide window is never materialized only to be cut.
    pub(super) fn byte_cap_rows(&self) -> usize {
        if self.max_buffered_mb == 0 {
            return 0;
        }
        let max_bytes = self.max_buffered_mb * 1024 * 1024;
        (max_bytes / self.bytes_per_row()).max(self.visible_rows.max(1))
    }

    /// Whether the buffer is a remote window: a pristine object-store scan. With anything
    /// applied, `slice(0, N)` stops at N matches, so page windows cost a row group, not
    /// forty.
    pub(super) fn remote_window(&self) -> bool {
        self.remote_source && self.is_pristine()
    }

    /// The marks a window reads on from, while the frame is the scan as loaded and that
    /// is one CSV scan: a filter, sort or query reads every row before its window.
    fn csv_window(&self) -> Option<&Arc<super::csv_marks::CsvMarks>> {
        if !self.is_pristine() {
            return None;
        }
        self.csv_marks
            .get_or_init(|| super::csv_marks::CsvMarks::of(&self.original_lf))
            .as_ref()
    }

    /// The files a page reads by, while the frame is the scan as loaded: a filter or
    /// sort reads every file before its window, so it goes through the whole scan.
    fn files_window(&self) -> Option<&RemoteFiles> {
        self.remote_files.as_ref().filter(|_| self.is_pristine())
    }

    /// Rows the buffer reaches past the view one way: `pages` of it locally, for remote
    /// scans with something applied, or many-file datasets (where cost is files opened, so
    /// a few at a time); half the window for a pristine remote object (`fit_window` trims
    /// to the cap).
    fn reach_rows(&self, pages: usize) -> usize {
        if !self.remote_window() || self.remote_files.is_some() {
            return pages * self.visible_rows.max(1);
        }
        let window = if self.max_buffered_rows > 0 {
            self.max_buffered_rows
        } else {
            DEFAULT_MAX_BUFFERED_ROWS
        };
        window / 2
    }

    /// The smallest buffer worth filling: a page plus the reach either side.
    fn min_buffer_len(&self) -> usize {
        self.visible_rows.max(1) + self.reach_ahead() + self.reach_behind()
    }

    /// True when the view already shows the last page, so End has nothing to load.
    pub fn at_end(&self) -> bool {
        self.view.start_row == self.view.num_rows.saturating_sub(self.visible_rows)
    }

    /// Fit `[buffer_start, buffer_end)` to the caps around the view, then for a remote
    /// object with a known footer to the view's row groups, cut back to the caps inside.
    fn fit_window(
        &self,
        view_start: usize,
        view_end: usize,
        buffer_start: &mut usize,
        buffer_end: &mut usize,
    ) {
        let byte_cap = self.byte_cap_rows();
        let cap = match (self.max_buffered_rows, byte_cap) {
            (0, cap) | (cap, 0) => cap,
            (rows, bytes) => rows.min(bytes),
        };
        if cap > 0 {
            shrink_around_view(
                view_start,
                view_end,
                cap,
                0,
                self.num_rows_bound(),
                buffer_start,
                buffer_end,
            );
        }
        let Some(offsets) = self
            .row_group_offsets
            .as_deref()
            .filter(|_| self.remote_window())
        else {
            if !self.remote_source {
                self.read_past_the_rows_on_hand(buffer_start, buffer_end);
            }
            return;
        };
        (*buffer_start, *buffer_end) = align_to_row_groups(
            offsets,
            view_start,
            view_end,
            *buffer_start,
            *buffer_end,
            cap,
        );
        // Caps hold inside a group: an oversized group is read a window at a time, never
        // pulling the next group early.
        if cap > 0 {
            let (floor, ceil) = (*buffer_start, *buffer_end);
            shrink_around_view(
                view_start,
                view_end,
                cap,
                floor,
                ceil,
                buffer_start,
                buffer_end,
            );
        }
        // Over many files, at most a few of them, around the view's.
        if let Some(file_offsets) = self.remote_files.as_ref().and_then(|f| f.offsets.as_ref()) {
            (*buffer_start, *buffer_end) = limit_files(
                file_offsets,
                view_start,
                view_end,
                *buffer_start,
                *buffer_end,
                MAX_FILES_PER_BUFFER,
            );
        }
        // A view straddling two groups needs both, but one is on hand: fetch the other
        // alone and stitch it on (see `apply_async_collect`).
        self.read_past_the_rows_on_hand(buffer_start, buffer_end);
    }

    /// Narrow `[buffer_start, buffer_end)` to the rows past the ones on hand, on the side it
    /// grows, when a fill is stitched to them: rows read once are not read again.
    fn read_past_the_rows_on_hand(&self, buffer_start: &mut usize, buffer_end: &mut usize) {
        if !self.stitches_buffer() {
            return;
        }
        let (held_start, held_end) = (self.view.buffered_start_row, self.view.buffered_end_row);
        if held_start <= *buffer_start && *buffer_start < held_end && held_end < *buffer_end {
            *buffer_start = held_end;
        } else if *buffer_start < held_start && held_start < *buffer_end && *buffer_end <= held_end
        {
            *buffer_end = held_start;
        }
    }

    /// Release the replaced buffer and its display frames (a stitch already took what it
    /// keeps); a requested relearn applies to the next rows.
    fn release_display_buffer(&mut self) {
        self.widths.rows_arrived();
        self.view.buffered_df = None;
        self.view.locked_df = None;
        self.view.df = None;
    }

    /// Recompute locked_df and df from the cached full buffer. Used when only termcol_index (or locked columns) changed.
    pub(super) fn slice_buffer_into_display(&mut self) {
        let full_df = match self.view.buffered_df.as_ref() {
            Some(df) => df,
            None => return,
        };

        if self.view.locked_columns_count > 0 {
            let locked_names: Vec<&str> = self
                .view
                .column_order
                .iter()
                .take(self.view.locked_columns_count)
                .map(|s| s.as_str())
                .collect();
            if let Ok(locked_df) = full_df.select(locked_names) {
                self.view.locked_df = Some(locked_df);
            }
        } else {
            self.view.locked_df = None;
        }

        let scroll_names: Vec<&str> = self
            .view
            .column_order
            .iter()
            .skip(self.frozen_shown() + self.termcol_index)
            .map(|s| s.as_str())
            .collect();
        if scroll_names.is_empty() {
            self.view.df = None;
        } else {
            if let Ok(scroll_df) = full_df.select(scroll_names) {
                self.view.df = Some(scroll_df);
            }
        }
    }

    /// Whether the view is inside the buffer within a page of an end with more data past
    /// it: where to grow the buffer ahead, before an in-buffer scroll leaves it blank.
    pub fn wants_to_load_ahead(&self) -> bool {
        if self.visible_rows == 0
            || self.view.buffered_df.is_none()
            || !self.page_on_hand(self.view.start_row)
        {
            return false;
        }
        let near = self.proximity();
        let view_end = self.view.start_row
            + self
                .visible_rows
                .min(self.num_rows_bound().saturating_sub(self.view.start_row));
        let behind = self.view.start_row - self.view.buffered_start_row <= self.proximity_behind()
            && self.view.buffered_start_row > 0;
        let ahead = self.view.buffered_end_row - view_end <= near
            && self.view.buffered_end_row < self.num_rows_bound();
        behind || ahead
    }

    /// How near its end the view comes before the buffer grows ahead: half the pages
    /// ahead, at least a page (a cloud fetch outlasts a PageDown). Pages, not
    /// [`Self::reach_ahead`]: a view that has not moved reads nothing ahead.
    fn proximity(&self) -> usize {
        (self.reach_rows(self.pages_lookahead) / 2).max(self.visible_rows)
    }

    /// As [`Self::proximity`], behind the view.
    fn proximity_behind(&self) -> usize {
        (self.reach_behind() / 2).max(self.visible_rows)
    }

    /// Rows the buffer reaches ahead of the view: see [`Self::reach_rows`]. A read that
    /// decodes whole data pages reads on through them, once a read has measured the rows:
    /// the rows past the page cost little now, and paging through them reads nothing.
    fn reach_ahead(&self) -> usize {
        let reach = self.reach_rows(self.pages_lookahead);
        let measured = self
            .view
            .observed_bytes_per_row
            .filter(|_| self.decodes_pages && !self.remote_source && self.is_pristine());
        match measured {
            Some(bytes) => reach.max((BYTES_DECODED_AHEAD / bytes).min(PAGES_DECODED_AHEAD)),
            None => reach,
        }
    }

    /// Rows the buffer reaches behind the view: see [`Self::reach_rows`].
    fn reach_behind(&self) -> usize {
        self.reach_rows(self.pages_lookback)
    }

    /// Where the view and the buffer are, to tell one load-ahead attempt from the next.
    pub fn buffer_position(&self) -> (u64, usize, usize, usize) {
        (
            self.len_generation(),
            self.view.start_row,
            self.view.buffered_start_row,
            self.view.buffered_end_row,
        )
    }

    /// Whether every row of the page starting at `start` is in the buffer.
    pub(crate) fn page_on_hand(&self, start: usize) -> bool {
        let bound = self.num_rows_bound();
        let end = start + self.visible_rows.min(bound.saturating_sub(start));
        self.view.buffered_df.is_some()
            && self.view.buffered_end_row > 0
            && start >= self.view.buffered_start_row
            && end <= self.view.buffered_end_row
    }

    /// The first row to draw: the view's own once its rows are on hand, else the last page
    /// drawn whole, so a pending fetch never shows half a page of nothing.
    pub(crate) fn start_to_draw(&mut self) -> usize {
        if self.page_on_hand(self.view.start_row) {
            self.view.drawn_start = self.view.start_row;
            self.view.start_row
        } else if self.page_on_hand(self.view.drawn_start) {
            self.view.drawn_start
        } else {
            self.view.start_row
        }
    }
}