moraine 0.6.2

A SlateDB-backed catalog for DuckLake
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
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
use std::{
    sync::{
        Arc,
        atomic::{AtomicU64, AtomicUsize, Ordering},
    },
    time::Duration,
};

use arrow::{
    array::{Array, FixedSizeBinaryArray, Int64Array, RecordBatch, StringArray},
    datatypes::{DataType, Field, Schema},
};
use futures::stream::BoxStream;
use object_store::{
    CopyOptions, GetOptions, GetResult, ListResult, MultipartUpload, ObjectMeta, ObjectStore,
    ObjectStoreExt, PutMultipartOptions, PutOptions, PutPayload, PutResult, memory::InMemory,
};
use parquet::arrow::{ArrowWriter, ProjectionMask, arrow_reader::ParquetRecordBatchReaderBuilder};

use super::*;
use crate::{
    data_file::{selection::Ordinals, values::array_value},
    store::index_encoding::IntWidth,
};

/// Derives one [`ScopedReadEntry`] per row of the Parquet file at `path`
/// that `rows` names, fetching only the footer, the columns at
/// `indexed_positions` (the indexed columns, in the index's column order),
/// and the embedded row-id column when the file carries one — byte-range
/// reads, never the whole object. Row ids resolve per `row_id_source`: the
/// field-id-tagged embedded column if present — rewrite files from UPDATE
/// and compaction preserve old ids there — else `row_id_start + ordinal`,
/// else refusal. This test helper deliberately exercises the discovery path;
/// production reads require DuckLake's recorded file and footer sizes.
async fn scoped_read_entries(
    object_store: Arc<dyn ObjectStore>,
    path: &Path,
    indexed_positions: &[usize],
    rows: ScopedRows<'_>,
    row_id_source: RowIdSource,
    file_size: Option<u64>,
) -> Result<Vec<ScopedReadEntry>> {
    let file_size = match file_size {
        Some(file_size) => file_size,
        None => {
            object_store
                .head(path)
                .await
                .map_err(corrupt("scoped read"))?
                .size
        }
    };
    scoped_read_recorded_entries(
        ParquetFile::new(DataStore::new(object_store), path.clone(), file_size, 0),
        indexed_positions,
        rows,
        row_id_source,
    )
    .await
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn index_encoding_workers_share_one_process_bound() {
    const TASKS: usize = INDEX_ENCODING_CONCURRENCY * 2;

    let active = Arc::new(AtomicUsize::new(0));
    let peak = Arc::new(AtomicUsize::new(0));
    let work = (0..TASKS).map(|position| {
        let active = Arc::clone(&active);
        let peak = Arc::clone(&peak);
        async move {
            run_bounded_index_encoding(move || {
                let held = active.fetch_add(1, Ordering::AcqRel) + 1;
                peak.fetch_max(held, Ordering::AcqRel);
                std::thread::sleep(Duration::from_millis(10));
                active.fetch_sub(1, Ordering::AcqRel);
                Ok(position)
            })
            .await
        }
    });
    let mut completed = stream::iter(work)
        .buffer_unordered(TASKS)
        .try_collect::<Vec<_>>()
        .await
        .unwrap();
    completed.sort_unstable();

    assert_eq!(completed, (0..TASKS).collect::<Vec<_>>());
    assert!(peak.load(Ordering::Relaxed) <= INDEX_ENCODING_CONCURRENCY);
    assert!(peak.load(Ordering::Relaxed) > 1);
}

/// Wraps an [`InMemory`] store, counting the payload bytes and requests
/// served by object/range reads — so a test can assert how much of a
/// file the scoped read actually fetched. `head` probes are not counted:
/// they carry no payload.
#[derive(Debug)]
struct CountingStore {
    inner: InMemory,
    fetched_bytes: AtomicU64,
    fetch_requests: AtomicU64,
    fetch_delay: Option<Duration>,
}

impl CountingStore {
    fn new() -> Self {
        Self {
            inner: InMemory::new(),
            fetched_bytes: AtomicU64::new(0),
            fetch_requests: AtomicU64::new(0),
            fetch_delay: None,
        }
    }

    fn with_fetch_delay(fetch_delay: Duration) -> Self {
        Self {
            fetch_delay: Some(fetch_delay),
            ..Self::new()
        }
    }

    fn fetched_bytes(&self) -> u64 {
        self.fetched_bytes.load(Ordering::Relaxed)
    }

    fn fetch_requests(&self) -> u64 {
        self.fetch_requests.load(Ordering::Relaxed)
    }
}

impl std::fmt::Display for CountingStore {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "CountingStore({})", self.inner)
    }
}

#[async_trait::async_trait]
impl ObjectStore for CountingStore {
    async fn put_opts(
        &self,
        location: &Path,
        payload: PutPayload,
        opts: PutOptions,
    ) -> object_store::Result<PutResult> {
        self.inner.put_opts(location, payload, opts).await
    }

    async fn put_multipart_opts(
        &self,
        location: &Path,
        opts: PutMultipartOptions,
    ) -> object_store::Result<Box<dyn MultipartUpload>> {
        self.inner.put_multipart_opts(location, opts).await
    }

    async fn get_opts(
        &self,
        location: &Path,
        options: GetOptions,
    ) -> object_store::Result<GetResult> {
        if let Some(delay) = self.fetch_delay {
            tokio::time::sleep(delay).await;
        }
        let head = options.head;
        let result = self.inner.get_opts(location, options).await?;
        if !head {
            self.fetch_requests.fetch_add(1, Ordering::Relaxed);
            self.fetched_bytes
                .fetch_add(result.range.end - result.range.start, Ordering::Relaxed);
        }
        Ok(result)
    }

    async fn get_ranges(
        &self,
        location: &Path,
        ranges: &[std::ops::Range<u64>],
    ) -> object_store::Result<Vec<Bytes>> {
        if let Some(delay) = self.fetch_delay {
            tokio::time::sleep(delay).await;
        }
        let results = self.inner.get_ranges(location, ranges).await?;
        self.fetch_requests.fetch_add(1, Ordering::Relaxed);
        let total: u64 = results.iter().map(|bytes| bytes.len() as u64).sum();
        self.fetched_bytes.fetch_add(total, Ordering::Relaxed);
        Ok(results)
    }

    fn delete_stream(
        &self,
        locations: BoxStream<'static, object_store::Result<Path>>,
    ) -> BoxStream<'static, object_store::Result<Path>> {
        self.inner.delete_stream(locations)
    }

    fn list(&self, prefix: Option<&Path>) -> BoxStream<'static, object_store::Result<ObjectMeta>> {
        self.inner.list(prefix)
    }

    async fn list_with_delimiter(&self, prefix: Option<&Path>) -> object_store::Result<ListResult> {
        self.inner.list_with_delimiter(prefix).await
    }

    async fn copy_opts(
        &self,
        from: &Path,
        to: &Path,
        options: CopyOptions,
    ) -> object_store::Result<()> {
        self.inner.copy_opts(from, to, options).await
    }
}

/// A file wide enough that its first column is a small fraction of its
/// bytes: one `Int64` value plus seven fat `Utf8` payload columns, `rows`
/// rows. Returns the written object's size and footer size.
async fn write_wide_named_fixture_with_footer(
    store: &dyn ObjectStore,
    path: &Path,
    rows: usize,
    first_column: &str,
) -> (u64, u64) {
    let mut fields = vec![Field::new(first_column, DataType::Int64, false)];
    for i in 0..7 {
        fields.push(Field::new(format!("payload{i}"), DataType::Utf8, false));
    }
    let schema = Arc::new(Schema::new(fields));

    let ids: Vec<i64> = (0..i64::try_from(rows).unwrap()).collect();
    let mut columns: Vec<Arc<dyn Array>> = vec![Arc::new(Int64Array::from(ids))];
    for i in 0..7 {
        // Row-unique text so the payload columns stay large on disk.
        let values: Vec<String> = (0..rows)
            .map(|row| format!("payload-{i}-row-{row:08}-abcdefghijklmnopqrstuvwxyz"))
            .collect();
        columns.push(Arc::new(StringArray::from(values)));
    }
    let batch = RecordBatch::try_new(schema, columns).unwrap();

    let mut buffer = Vec::new();
    {
        let mut writer = ArrowWriter::try_new(&mut buffer, batch.schema(), None).unwrap();
        writer.write(&batch).unwrap();
        writer.close().unwrap();
    }
    let object_len = buffer.len() as u64;
    let footer_offset = buffer.len() - 8;
    let footer_size = u64::from(u32::from_le_bytes(
        buffer[footer_offset..footer_offset + 4].try_into().unwrap(),
    ));
    store.put(path, buffer.into()).await.unwrap();
    (object_len, footer_size)
}

async fn write_wide_fixture_with_footer(
    store: &dyn ObjectStore,
    path: &Path,
    rows: usize,
) -> (u64, u64) {
    write_wide_named_fixture_with_footer(store, path, rows, "id").await
}

async fn write_wide_fixture(store: &dyn ObjectStore, path: &Path, rows: usize) -> u64 {
    write_wide_fixture_with_footer(store, path, rows).await.0
}

/// DuckLake's recorded footer size collapses the footer-length probe,
/// and immutable-file metadata is reused on the next read. The second
/// read therefore fetches nothing: its footer and its column ranges are
/// both resident.
#[tokio::test]
async fn recorded_footer_and_metadata_cache_remove_metadata_round_trips() {
    let store = Arc::new(CountingStore::new());
    let data = DataStore::new(store.clone());
    let path = Path::from("cached-wide.parquet");
    let (object_len, footer_size) =
        write_wide_fixture_with_footer(store.as_ref(), &path, 20_000).await;
    let wanted: RowPositions = [7, 19_000].into_iter().collect();

    let first = scoped_read_recorded_entries(
        ParquetFile::new(data.clone(), path.clone(), object_len, footer_size),
        &[0],
        ScopedRows::At(&wanted),
        RowIdSource::Ordinal,
    )
    .await
    .unwrap();
    assert_eq!(first.len(), 2);
    let first_requests = store.fetch_requests();

    let second = scoped_read_recorded_entries(
        ParquetFile::new(data.clone(), path.clone(), object_len, footer_size),
        &[0],
        ScopedRows::At(&wanted),
        RowIdSource::Ordinal,
    )
    .await
    .unwrap();
    assert_eq!(second, first);

    let second_requests = store.fetch_requests() - first_requests;
    assert_eq!(first_requests, 3, "footer, page index, projected columns");
    assert_eq!(
        second_requests, 0,
        "the second read repeats the first's ranges, which are cached"
    );
}

/// A delete-file read fetches only its `pos` column. Both its footer and
/// that column's ranges are retained, so a second pass over the same file
/// fetches nothing.
#[tokio::test]
async fn large_delete_file_reads_only_positions_and_reuses_metadata() {
    let store = Arc::new(CountingStore::new());
    let data = DataStore::new(store.clone());
    let path = Path::from("cached-wide-delete.parquet");
    let (object_len, footer_size) =
        write_wide_named_fixture_with_footer(store.as_ref(), &path, 20_000, "pos").await;
    let first = delete_file_positions(ParquetFile::new(
        data.clone(),
        path.clone(),
        object_len,
        footer_size,
    ))
    .await
    .unwrap();
    assert_eq!(first.len(), 20_000);
    assert_eq!(first[0], 0);
    assert_eq!(first[19_999], 19_999);
    let first_requests = store.fetch_requests();
    let first_bytes = store.fetched_bytes();

    let second = delete_file_positions(ParquetFile::new(
        data.clone(),
        path.clone(),
        object_len,
        footer_size,
    ))
    .await
    .unwrap();
    assert_eq!(second, first);
    let second_requests = store.fetch_requests() - first_requests;

    assert_eq!(first_requests, 2, "footer and projected position column");
    assert_eq!(
        second_requests, 0,
        "the second pass repeats the first's ranges, which are cached"
    );
    assert!(
        first_bytes < object_len / 4,
        "fetched {first_bytes} of {object_len} bytes"
    );
}

/// Two readers missing the same immutable footer at once share one
/// metadata fill. Each still reads its own projected data column, while
/// the footer and page index are fetched only once between them.
#[tokio::test]
async fn concurrent_metadata_misses_share_one_in_flight_fill() {
    let store = Arc::new(CountingStore::with_fetch_delay(Duration::from_millis(10)));
    let data = DataStore::new(store.clone());
    let path = Path::from("concurrent-cached-wide.parquet");
    let (object_len, footer_size) =
        write_wide_fixture_with_footer(store.as_ref(), &path, 20_000).await;
    let wanted: RowPositions = [7, 19_000].into_iter().collect();

    let first = scoped_read_recorded_entries(
        ParquetFile::new(data.clone(), path.clone(), object_len, footer_size),
        &[0],
        ScopedRows::At(&wanted),
        RowIdSource::Ordinal,
    );
    let second = scoped_read_recorded_entries(
        ParquetFile::new(data.clone(), path.clone(), object_len, footer_size),
        &[0],
        ScopedRows::At(&wanted),
        RowIdSource::Ordinal,
    );
    let (first, second) = tokio::join!(first, second);

    assert_eq!(first.unwrap(), second.unwrap());
    assert_eq!(
        store.fetch_requests(),
        4,
        "one footer, one page-index, and two projected-column reads"
    );
}

/// A failed shared fill wakes its callers but is not retained as a cache
/// entry. Immutable objects do not change in production; replacing the
/// fixture here proves a transient read failure can be retried.
#[tokio::test]
async fn failed_metadata_in_flight_fill_is_retryable() {
    let store = Arc::new(CountingStore::new());
    let data = DataStore::new(store.clone());
    let path = Path::from("retry-cached-wide.parquet");
    let (object_len, footer_size) =
        write_wide_fixture_with_footer(store.as_ref(), &path, 20_000).await;
    let valid = store.inner.get(&path).await.unwrap().bytes().await.unwrap();
    let mut corrupt = valid.to_vec();
    *corrupt.last_mut().unwrap() ^= 0xff;
    store.inner.put(&path, corrupt.into()).await.unwrap();
    let wanted: RowPositions = [7].into_iter().collect();

    let failed = scoped_read_recorded_entries(
        ParquetFile::new(data.clone(), path.clone(), object_len, footer_size),
        &[0],
        ScopedRows::At(&wanted),
        RowIdSource::Ordinal,
    )
    .await;
    assert!(failed.is_err());

    store.inner.put(&path, valid.into()).await.unwrap();
    let retried = scoped_read_recorded_entries(
        ParquetFile::new(DataStore::new(store), path, object_len, footer_size),
        &[0],
        ScopedRows::At(&wanted),
        RowIdSource::Ordinal,
    )
    .await
    .unwrap();
    assert_eq!(retried.len(), 1);
}

/// The read fetches only the footer and projected column chunks, and
/// values follow the requested position order.
#[tokio::test]
async fn scoped_read_fetches_only_projected_columns() {
    let store = Arc::new(CountingStore::new());
    let path = Path::from("wide.parquet");
    let object_len = write_wide_fixture(store.as_ref(), &path, 20_000).await;

    let entries = scoped_read_entries(
        store.clone(),
        &path,
        &[1, 0],
        ScopedRows::All,
        RowIdSource::Ordinal,
        None,
    )
    .await
    .unwrap();
    assert_eq!(entries.len(), 20_000);
    assert_eq!(
        entries[19_999].values,
        vec![
            Some(IndexKeyValue::Str(
                "payload-0-row-00019999-abcdefghijklmnopqrstuvwxyz".to_owned()
            )),
            Some(IndexKeyValue::Int {
                value: 19_999,
                width: IntWidth::I64,
            }),
        ],
    );

    let fetched = store.fetched_bytes();
    assert!(
        fetched < object_len * 2 / 5,
        "fetched {fetched} of {object_len} bytes ({} requests) — the scoped read should \
         range-read only the footer and the projected columns",
        store.fetch_requests(),
    );
}

/// A narrow file shaped like DuckLake's small per-insert output: an
/// `Int64` id and one short `Utf8` column, `rows` rows. Returns the
/// written object's size.
async fn write_narrow_fixture(store: &dyn ObjectStore, path: &Path, rows: usize) -> u64 {
    let schema = Arc::new(Schema::new(vec![
        Field::new("id", DataType::Int64, false),
        Field::new("name", DataType::Utf8, false),
    ]));
    let ids: Vec<i64> = (0..i64::try_from(rows).unwrap()).collect();
    let names: Vec<String> = (0..rows).map(|row| format!("name-{row}")).collect();
    let batch = RecordBatch::try_new(
        schema,
        vec![
            Arc::new(Int64Array::from(ids)),
            Arc::new(StringArray::from(names)),
        ],
    )
    .unwrap();

    let mut buffer = Vec::new();
    {
        let mut writer = ArrowWriter::try_new(&mut buffer, batch.schema(), None).unwrap();
        writer.write(&batch).unwrap();
        writer.close().unwrap();
    }
    let object_len = u64::try_from(buffer.len()).unwrap();
    store.put(path, buffer.into()).await.unwrap();
    object_len
}

/// Prints the exact bytes and requests the range reader issues per
/// fixture shape. Run with:
/// `cargo test -p moraine --lib -- --ignored --nocapture
/// prints_fetch_profile`
#[tokio::test]
#[ignore = "fetch-profile probe; run manually with --nocapture"]
async fn prints_fetch_profile() {
    // (label, wide fixture?, rows, indexed positions)
    let shapes: [(&str, bool, usize, &[usize]); 4] = [
        ("wide 8-col x 20k rows, 1 indexed col ", true, 20_000, &[0]),
        (
            "wide 8-col x 20k rows, 2 indexed cols",
            true,
            20_000,
            &[0, 1],
        ),
        ("narrow 2-col x 100 rows, 1 indexed   ", false, 100, &[0]),
        ("narrow 2-col x 10 rows, 1 indexed    ", false, 10, &[0]),
    ];
    for (label, wide, rows, positions) in shapes {
        let store = Arc::new(CountingStore::new());
        let path = Path::from("probe.parquet");
        let object_len = if wide {
            write_wide_fixture(store.as_ref(), &path, rows).await
        } else {
            write_narrow_fixture(store.as_ref(), &path, rows).await
        };
        let entries = scoped_read_entries(
            store.clone(),
            &path,
            positions,
            ScopedRows::All,
            RowIdSource::Ordinal,
            None,
        )
        .await
        .unwrap();
        assert_eq!(entries.len(), rows);
        println!(
            "{label}: object {object_len:>8} B, fetched {:>7} B ({:>2}%), {} requests",
            store.fetched_bytes(),
            store.fetched_bytes() * 100 / object_len,
            store.fetch_requests(),
        );
    }
}

/// Per-request latency a [`LatencyStore`] charges, modelling a remote
/// store round trip.
const REQUEST_LATENCY: std::time::Duration = std::time::Duration::from_millis(30);

/// Wraps an [`InMemory`] store, modelling a remote one: every read
/// request pays [`REQUEST_LATENCY`] plus transfer at ~100 MB/s (10 ns
/// per byte).
#[derive(Debug)]
struct LatencyStore {
    inner: InMemory,
}

impl LatencyStore {
    async fn charge(bytes: u64) {
        tokio::time::sleep(REQUEST_LATENCY + std::time::Duration::from_nanos(bytes * 10)).await;
    }
}

impl std::fmt::Display for LatencyStore {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "LatencyStore({})", self.inner)
    }
}

#[async_trait::async_trait]
impl ObjectStore for LatencyStore {
    async fn put_opts(
        &self,
        location: &Path,
        payload: PutPayload,
        opts: PutOptions,
    ) -> object_store::Result<PutResult> {
        self.inner.put_opts(location, payload, opts).await
    }

    async fn put_multipart_opts(
        &self,
        location: &Path,
        opts: PutMultipartOptions,
    ) -> object_store::Result<Box<dyn MultipartUpload>> {
        self.inner.put_multipart_opts(location, opts).await
    }

    async fn get_opts(
        &self,
        location: &Path,
        options: GetOptions,
    ) -> object_store::Result<GetResult> {
        let head = options.head;
        let result = self.inner.get_opts(location, options).await?;
        let bytes = if head {
            0
        } else {
            result.range.end - result.range.start
        };
        Self::charge(bytes).await;
        Ok(result)
    }

    async fn get_ranges(
        &self,
        location: &Path,
        ranges: &[std::ops::Range<u64>],
    ) -> object_store::Result<Vec<Bytes>> {
        let results = self.inner.get_ranges(location, ranges).await?;
        Self::charge(results.iter().map(|bytes| bytes.len() as u64).sum()).await;
        Ok(results)
    }

    fn delete_stream(
        &self,
        locations: BoxStream<'static, object_store::Result<Path>>,
    ) -> BoxStream<'static, object_store::Result<Path>> {
        self.inner.delete_stream(locations)
    }

    fn list(&self, prefix: Option<&Path>) -> BoxStream<'static, object_store::Result<ObjectMeta>> {
        self.inner.list(prefix)
    }

    async fn list_with_delimiter(&self, prefix: Option<&Path>) -> object_store::Result<ListResult> {
        self.inner.list_with_delimiter(prefix).await
    }

    async fn copy_opts(
        &self,
        from: &Path,
        to: &Path,
        options: CopyOptions,
    ) -> object_store::Result<()> {
        self.inner.copy_opts(from, to, options).await
    }
}

/// The pre-range-reader read, reproduced for comparison: fetch the whole
/// object, then decode only the projected columns.
async fn whole_file_entries(
    store: &dyn ObjectStore,
    path: &Path,
    indexed_positions: &[usize],
) -> Vec<ScopedReadEntry> {
    let bytes: Bytes = store.get(path).await.unwrap().bytes().await.unwrap();
    let builder = ParquetRecordBatchReaderBuilder::try_new(bytes).unwrap();
    let mask = ProjectionMask::roots(builder.parquet_schema(), indexed_positions.iter().copied());
    let reader = builder.with_projection(mask).build().unwrap();

    let output_positions: Vec<usize> = (0..indexed_positions.len()).collect();
    let mut entries = Vec::new();
    let mut emitted = 0usize;
    for batch in reader {
        let batch = batch.unwrap();
        entries.extend(
            record_batch_entries(&batch, &output_positions, None, 0, Ordinals::Dense, emitted)
                .unwrap(),
        );
        emitted += batch.num_rows();
    }
    entries
}

/// Wall-clock comparison of the whole-object read against the range
/// reader on a simulated remote store (30 ms per request, ~100 MB/s).
/// Run with:
/// `cargo test -p moraine --lib -- --ignored --nocapture simulated_remote`
#[tokio::test]
#[ignore = "timing probe; run manually with --nocapture"]
async fn simulated_remote_store_bench() {
    // (label, wide fixture?, rows) — narrow-small is DuckLake's typical
    // per-insert file; wide-large is the backfill/bulk-maintenance case.
    let shapes = [
        ("narrow 2-col x 100 rows ", false, 100),
        ("wide 8-col x 50k rows   ", true, 50_000),
    ];
    for (label, wide, rows) in shapes {
        let store = Arc::new(LatencyStore {
            inner: InMemory::new(),
        });
        let path = Path::from("bench.parquet");
        let object_len = if wide {
            write_wide_fixture(store.as_ref(), &path, rows).await
        } else {
            write_narrow_fixture(store.as_ref(), &path, rows).await
        };

        let started = std::time::Instant::now();
        let old_entries = whole_file_entries(store.as_ref(), &path, &[0]).await;
        let whole_file = started.elapsed();

        let started = std::time::Instant::now();
        let new_entries = scoped_read_entries(
            store.clone(),
            &path,
            &[0],
            ScopedRows::All,
            RowIdSource::Ordinal,
            None,
        )
        .await
        .unwrap();
        let range_read = started.elapsed();

        assert_eq!(
            old_entries, new_entries,
            "both paths derive the same entries"
        );
        println!(
            "{label} ({object_len:>8} B): whole-file {whole_file:>9.2?}, range-read \
             {range_read:>9.2?}"
        );
    }
}

/// Writes `batch` to `path` as a Parquet object, returning its byte size.
pub(super) async fn write_fixture(
    object_store: &InMemory,
    path: &Path,
    batch: &RecordBatch,
) -> u64 {
    let mut buffer = Vec::new();
    {
        let mut writer = ArrowWriter::try_new(&mut buffer, batch.schema(), None).unwrap();
        writer.write(batch).unwrap();
        writer.close().unwrap();
    }
    let file_size = buffer.len() as u64;
    object_store.put(path, buffer.into()).await.unwrap();
    file_size
}

fn fixture_batch() -> RecordBatch {
    // Columns: id (indexed), name (indexed, one NULL), row_id, and an
    // unindexed `payload` column the read must not touch.
    let schema = Arc::new(Schema::new(vec![
        Field::new("id", DataType::Int64, false),
        Field::new("name", DataType::Utf8, true),
        Field::new("row_id", DataType::Int64, false),
        Field::new("payload", DataType::Utf8, false),
    ]));
    RecordBatch::try_new(
        schema,
        vec![
            Arc::new(Int64Array::from(vec![10, 20, 30])),
            Arc::new(StringArray::from(vec![Some("a"), None, Some("c")])),
            Arc::new(Int64Array::from(vec![100, 101, 102])),
            Arc::new(StringArray::from(vec!["x", "y", "z"])),
        ],
    )
    .unwrap()
}

#[test]
fn sub_microsecond_timestamps_index_by_their_own_count() {
    // A millisecond timestamp indexes by its millisecond count, a
    // nanosecond one by its nanosecond count — not misread as micros.
    let millis = arrow::array::TimestampMillisecondArray::from(vec![1_700_000_000_123i64]);
    assert_eq!(
        array_value(&millis, 0).unwrap(),
        Some(IndexKeyValue::Int {
            value: 1_700_000_000_123,
            width: IntWidth::I64
        }),
    );
    let nanos = arrow::array::TimestampNanosecondArray::from(vec![1_700_000_000_123_456_789i64]);
    assert_eq!(
        array_value(&nanos, 0).unwrap(),
        Some(IndexKeyValue::Int {
            value: 1_700_000_000_123_456_789,
            width: IntWidth::I64
        }),
    );
}

#[test]
fn fixed_size_binary_indexes_as_bytes() {
    // A `UUID` reaches the read as a 16-byte `FixedSizeBinary`.
    let uuid = [0xABu8; 16];
    let array = arrow::array::FixedSizeBinaryArray::try_from_iter([uuid].into_iter()).unwrap();
    assert_eq!(
        array_value(&array, 0).unwrap(),
        Some(IndexKeyValue::Bytes(uuid.to_vec())),
    );
}

#[test]
#[allow(clippy::too_many_lines)]
fn fused_arrow_encoding_matches_owned_path_for_every_indexable_type() {
    use arrow::array::{
        ArrayRef, BinaryArray, BooleanArray, Date32Array, Date64Array, Float32Array, Float64Array,
        Int8Array, Int16Array, Int32Array, LargeBinaryArray, LargeStringArray,
        TimestampMicrosecondArray, TimestampMillisecondArray, TimestampNanosecondArray,
        TimestampSecondArray, UInt8Array, UInt16Array, UInt32Array, UInt64Array,
    };

    let fixed = FixedSizeBinaryArray::try_from_sparse_iter_with_size(
        [Some(vec![0, 1, 2, 3]), None].into_iter(),
        4,
    )
    .unwrap();
    let columns: Vec<(&str, ArrayRef)> = vec![
        ("i8", Arc::new(Int8Array::from(vec![Some(-8), None]))),
        ("i16", Arc::new(Int16Array::from(vec![Some(-16), None]))),
        ("i32", Arc::new(Int32Array::from(vec![Some(-32), None]))),
        ("i64", Arc::new(Int64Array::from(vec![Some(-64), None]))),
        ("u8", Arc::new(UInt8Array::from(vec![Some(8), None]))),
        ("u16", Arc::new(UInt16Array::from(vec![Some(16), None]))),
        ("u32", Arc::new(UInt32Array::from(vec![Some(32), None]))),
        ("u64", Arc::new(UInt64Array::from(vec![Some(64), None]))),
        (
            "f32",
            Arc::new(Float32Array::from(vec![
                Some(f32::from_bits(0xffc0_0001)),
                None,
            ])),
        ),
        (
            "f64",
            Arc::new(Float64Array::from(vec![
                Some(f64::from_bits(0x7ff8_0000_0000_0001)),
                None,
            ])),
        ),
        ("bool", Arc::new(BooleanArray::from(vec![Some(true), None]))),
        (
            "utf8",
            Arc::new(StringArray::from(vec![Some("a\0\u{1}z"), None])),
        ),
        (
            "large_utf8",
            Arc::new(LargeStringArray::from(vec![Some("large"), None])),
        ),
        (
            "binary",
            Arc::new(BinaryArray::from(vec![Some(&[0_u8, 1, 0xff][..]), None])),
        ),
        (
            "large_binary",
            Arc::new(LargeBinaryArray::from(vec![Some(&[1_u8, 0, 2][..]), None])),
        ),
        ("fixed_binary", Arc::new(fixed)),
        ("date32", Arc::new(Date32Array::from(vec![Some(12), None]))),
        ("date64", Arc::new(Date64Array::from(vec![Some(13), None]))),
        (
            "timestamp_s",
            Arc::new(TimestampSecondArray::from(vec![Some(14), None])),
        ),
        (
            "timestamp_ms",
            Arc::new(TimestampMillisecondArray::from(vec![Some(15), None])),
        ),
        (
            "timestamp_us",
            Arc::new(TimestampMicrosecondArray::from(vec![Some(16), None])),
        ),
        (
            "timestamp_ns",
            Arc::new(TimestampNanosecondArray::from(vec![Some(17), None])),
        ),
    ];
    let batch = RecordBatch::try_from_iter(columns).unwrap();
    let all_positions: Vec<_> = (0..batch.num_columns()).collect();
    let projections = vec![
        IndexProjection {
            index_id: 37,
            unique: true,
            directions: all_positions
                .iter()
                .enumerate()
                .map(|(position, _)| {
                    if position % 2 == 0 {
                        Direction::Ascending
                    } else {
                        Direction::Descending
                    }
                })
                .collect(),
            nulls: all_positions
                .iter()
                .enumerate()
                .map(|(position, _)| {
                    if position % 3 == 0 {
                        NullOrder::First
                    } else {
                        NullOrder::Last
                    }
                })
                .collect(),
            positions: all_positions,
        },
        // Overlapping text/blob/composite use verifies a shared Arrow
        // column can feed several final keys without an owned scalar.
        IndexProjection {
            index_id: 38,
            unique: false,
            positions: vec![11, 13, 11],
            directions: vec![
                Direction::Descending,
                Direction::Ascending,
                Direction::Ascending,
            ],
            nulls: vec![NullOrder::Last, NullOrder::First, NullOrder::Last],
        },
    ];

    let actual = record_batch_index_entries(
        &batch,
        &projections,
        None,
        100,
        Ordinals::Dense,
        0,
        None,
        None,
    )
    .unwrap();
    let mut expected = Vec::new();
    for row in 0..batch.num_rows() {
        for (index, projection) in projections.iter().enumerate() {
            let values = projection
                .positions
                .iter()
                .map(|&position| array_value(batch.column(position).as_ref(), row))
                .collect::<Result<Vec<_>>>()
                .unwrap();
            let row_id = 100 + u64::try_from(row).unwrap();
            let (key, unique) = crate::store::index_encoding::encode_ordered_index_entry(
                &values,
                &projection.directions,
                &projection.nulls,
                projection.index_id,
                projection.unique,
                row_id,
            )
            .unwrap();
            expected.push((index, row_id, key, unique));
        }
    }

    assert_eq!(actual.len(), expected.len());
    for (actual, expected) in actual.iter().zip(expected) {
        assert_eq!(actual.index, expected.0);
        assert_eq!(actual.row_id, expected.1);
        assert_eq!(actual.key, expected.2);
        assert_eq!(actual.unique, expected.3);
    }
}

#[test]
fn fused_arrow_encoding_normalizes_signed_zero_identically() {
    use arrow::array::{ArrayRef, Float32Array, Float64Array};

    let batch = RecordBatch::try_from_iter(vec![
        (
            "f32",
            Arc::new(Float32Array::from(vec![-0.0, 0.0])) as ArrayRef,
        ),
        (
            "f64",
            Arc::new(Float64Array::from(vec![-0.0, 0.0])) as ArrayRef,
        ),
    ])
    .unwrap();
    let projections = [IndexProjection {
        index_id: 39,
        unique: true,
        positions: vec![0, 1],
        directions: vec![Direction::Descending, Direction::Ascending],
        nulls: vec![NullOrder::First, NullOrder::Last],
    }];
    let entries = record_batch_index_entries(
        &batch,
        &projections,
        None,
        0,
        Ordinals::Dense,
        0,
        None,
        None,
    )
    .unwrap();

    assert_eq!(entries[0].key, entries[1].key);
    for (row, entry) in entries.iter().enumerate() {
        let values = [
            array_value(batch.column(0).as_ref(), row).unwrap(),
            array_value(batch.column(1).as_ref(), row).unwrap(),
        ];
        assert_eq!(
            entry.key,
            crate::store::index_encoding::encode_ordered_index_entry(
                &values,
                &projections[0].directions,
                &projections[0].nulls,
                projections[0].index_id,
                projections[0].unique,
                u64::try_from(row).unwrap(),
            )
            .unwrap()
            .0
        );
    }
}

/// The row-id column DuckLake's rewrite and flush writers append:
/// BIGINT, tagged with the reserved field id — at any position.
pub(super) fn tagged_row_id_field(nullable: bool) -> Field {
    Field::new("_ducklake_internal_row_id", DataType::Int64, nullable).with_metadata(
        std::collections::HashMap::from([(
            parquet::arrow::PARQUET_FIELD_ID_META_KEY.to_string(),
            "2147483540".to_string(),
        )]),
    )
}

/// `fixture_batch` with the row-id column carrying the field id, so
/// discovery finds it (at position 2, not trailing).
fn tagged_fixture_batch() -> RecordBatch {
    let schema = Arc::new(Schema::new(vec![
        Field::new("id", DataType::Int64, false),
        Field::new("name", DataType::Utf8, true),
        tagged_row_id_field(false),
        Field::new("payload", DataType::Utf8, false),
    ]));
    RecordBatch::try_new(
        schema,
        vec![
            Arc::new(Int64Array::from(vec![10, 20, 30])),
            Arc::new(StringArray::from(vec![Some("a"), None, Some("c")])),
            Arc::new(Int64Array::from(vec![100, 101, 102])),
            Arc::new(StringArray::from(vec!["x", "y", "z"])),
        ],
    )
    .unwrap()
}

#[tokio::test]
async fn reads_indexed_columns_and_embedded_row_ids() {
    let store = Arc::new(InMemory::new());
    let path = Path::from("data.parquet");
    write_fixture(&store, &path, &tagged_fixture_batch()).await;

    // Index over (id, name); the file carries the field-id-tagged
    // row-id column at position 2, found without a caller hint.
    let entries = scoped_read_entries(
        store.clone(),
        &path,
        &[0, 1],
        ScopedRows::All,
        RowIdSource::Resolve { row_id_start: None },
        None,
    )
    .await
    .unwrap();
    assert_eq!(entries.len(), 3);
    assert_eq!(
        entries[0],
        ScopedReadEntry {
            ordinal: 0,
            row_id: 100,
            values: vec![
                Some(IndexKeyValue::Int {
                    value: 10,
                    width: IntWidth::I64,
                }),
                Some(IndexKeyValue::Str("a".to_owned())),
            ],
        }
    );
    // Row 1's name is NULL → no value for that component.
    assert_eq!(entries[1].row_id, 101);
    assert_eq!(entries[1].values[1], None);
    assert_eq!(entries[2].row_id, 102);
}

/// Fused index encoding yields one Arrow batch at a time; a full-file
/// upkeep read does not retain every encoded entry first.
#[tokio::test]
async fn streams_fused_index_entries_in_bounded_batches() {
    let store = Arc::new(InMemory::new());
    let path = Path::from("streamed-index.parquet");
    let (file_size, footer_size) =
        write_wide_fixture_with_footer(store.as_ref(), &path, 20_000).await;
    let projections = vec![IndexProjection {
        index_id: 7,
        unique: false,
        positions: vec![0],
        directions: vec![Direction::Ascending],
        nulls: vec![NullOrder::First],
    }];
    let batches = scoped_read_index_entry_batches(
        ParquetFile::new(DataStore::new(store), path, file_size, footer_size),
        projections,
        ScopedRows::All,
        RowIdSource::Resolve {
            row_id_start: Some(100),
        },
        None,
    )
    .await
    .unwrap()
    .try_collect::<Vec<_>>()
    .await
    .unwrap();

    assert_eq!(batches.len(), 3);
    assert!(
        batches
            .iter()
            .all(|batch| batch.len() <= BUILD_READ_BATCH_ROWS)
    );
    assert_eq!(batches.iter().map(Vec::len).sum::<usize>(), 20_000);
    assert_eq!(batches[0][0].row_id, 100);
    assert_eq!(batches[2].last().unwrap().row_id, 20_099);
}

#[test]
fn row_positions_sort_and_deduplicate_once() {
    let positions = RowPositions::from_unsorted(vec![9, 2, 9, 4, 2]);

    assert_eq!(positions.as_slice(), &[2, 4, 9]);
}

/// Selected-row reads use the same bounded fused pipeline as full-file
/// additions instead of collecting the target's entries first.
#[tokio::test]
async fn streams_selected_fused_index_entries() {
    let store = Arc::new(InMemory::new());
    let path = Path::from("streamed-selected-index.parquet");
    let (file_size, footer_size) =
        write_wide_fixture_with_footer(store.as_ref(), &path, 20_000).await;
    let projections = vec![IndexProjection {
        index_id: 7,
        unique: false,
        positions: vec![0],
        directions: vec![Direction::Ascending],
        nulls: vec![NullOrder::First],
    }];
    let selected = RowPositions::from_unsorted(vec![19_999, 8_193, 1, 8_193]);
    let batches = scoped_read_index_entry_batches(
        ParquetFile::new(DataStore::new(store), path, file_size, footer_size),
        projections,
        ScopedRows::At(&selected),
        RowIdSource::Resolve {
            row_id_start: Some(100),
        },
        None,
    )
    .await
    .unwrap()
    .try_collect::<Vec<_>>()
    .await
    .unwrap();

    let row_ids: Vec<_> = batches
        .into_iter()
        .flatten()
        .map(|entry| entry.row_id)
        .collect();
    assert_eq!(row_ids, vec![101, 8_293, 20_099]);
}

/// Values come back ordered as the requested positions — duplicates and
/// all. The merged multi-index read (one fetch per file, split back per
/// index) relies on exactly this.
#[tokio::test]
async fn values_follow_requested_position_order() {
    let store = Arc::new(InMemory::new());
    let path = Path::from("data.parquet");
    write_fixture(&store, &path, &fixture_batch()).await;

    let entries = scoped_read_entries(
        store.clone(),
        &path,
        &[1, 0, 0],
        ScopedRows::All,
        RowIdSource::Ordinal,
        None,
    )
    .await
    .unwrap();
    assert_eq!(
        entries[0].values,
        vec![
            Some(IndexKeyValue::Str("a".to_owned())),
            Some(IndexKeyValue::Int {
                value: 10,
                width: IntWidth::I64,
            }),
            Some(IndexKeyValue::Int {
                value: 10,
                width: IntWidth::I64,
            }),
        ],
    );
}

#[tokio::test]
async fn derives_row_ids_from_start_plus_ordinal_when_absent() {
    let store = Arc::new(InMemory::new());
    let path = Path::from("data.parquet");
    write_fixture(&store, &path, &fixture_batch()).await;

    // `fixture_batch`'s "row_id" column carries no field id, so it is
    // not the embedded column — names mean nothing to discovery — and
    // ids fall back to row_id_start (500) + ordinal.
    let entries = scoped_read_entries(
        store.clone(),
        &path,
        &[0],
        ScopedRows::All,
        RowIdSource::Resolve {
            row_id_start: Some(500),
        },
        None,
    )
    .await
    .unwrap();
    assert_eq!(
        entries.iter().map(|e| e.row_id).collect::<Vec<_>>(),
        vec![500, 501, 502]
    );
}

/// The embedded column wins even when a dense start is recorded:
/// flushed files carry both, and their ids may hold gaps.
#[tokio::test]
async fn embedded_row_id_column_wins_over_dense_start() {
    let store = Arc::new(InMemory::new());
    let path = Path::from("rewrite.parquet");
    let schema = Arc::new(Schema::new(vec![
        Field::new("a", DataType::Int64, true),
        tagged_row_id_field(false),
    ]));
    let batch = RecordBatch::try_new(
        schema,
        vec![
            Arc::new(Int64Array::from(vec![10, 20, 30])),
            Arc::new(Int64Array::from(vec![5, 9, 12])),
        ],
    )
    .unwrap();
    write_fixture(&store, &path, &batch).await;

    let entries = scoped_read_entries(
        store.clone(),
        &path,
        &[0],
        ScopedRows::All,
        RowIdSource::Resolve {
            row_id_start: Some(100),
        },
        None,
    )
    .await
    .unwrap();
    assert_eq!(
        entries.iter().map(|e| e.row_id).collect::<Vec<_>>(),
        vec![5, 9, 12],
        "ids come from the column, not 100 + ordinal"
    );
}

/// A per-row-id catalog row over a file lacking the column is a
/// disagreement between catalog and file.
#[tokio::test]
async fn resolve_with_neither_source_fails_corruption() {
    let store = Arc::new(InMemory::new());
    let path = Path::from("plain.parquet");
    write_fixture(&store, &path, &fixture_batch()).await;

    let err = scoped_read_entries(
        store.clone(),
        &path,
        &[0],
        ScopedRows::All,
        RowIdSource::Resolve { row_id_start: None },
        None,
    )
    .await
    .unwrap_err();
    assert!(matches!(err, Error::Corruption(_)), "{err}");
}

/// Ordinal mode refuses a file that already carries row ids —
/// renumbering its rows would fork their identity.
#[tokio::test]
async fn ordinal_mode_refuses_an_embedded_row_id_column() {
    let store = Arc::new(InMemory::new());
    let path = Path::from("rewrite.parquet");
    write_fixture(&store, &path, &tagged_fixture_batch()).await;

    let err = scoped_read_entries(
        store.clone(),
        &path,
        &[0],
        ScopedRows::All,
        RowIdSource::Ordinal,
        None,
    )
    .await
    .unwrap_err();
    assert!(matches!(err, Error::Constraint(_)), "{err}");
}

/// A selective read decodes only the named rows, and their ids follow
/// the file ordinal each row actually sits at — not its position in the
/// selected output.
#[tokio::test]
async fn selective_read_returns_only_the_named_rows() {
    let store = Arc::new(InMemory::new());
    let path = Path::from("narrow.parquet");
    write_narrow_fixture(store.as_ref(), &path, 10).await;

    let wanted: RowPositions = [2, 5, 7].into_iter().collect();
    let entries = scoped_read_entries(
        store.clone(),
        &path,
        &[0],
        ScopedRows::At(&wanted),
        RowIdSource::Resolve {
            row_id_start: Some(500),
        },
        None,
    )
    .await
    .unwrap();

    assert_eq!(
        entries.iter().map(|entry| entry.row_id).collect::<Vec<_>>(),
        vec![502, 505, 507],
    );
    assert_eq!(
        entries
            .iter()
            .map(|entry| entry.values[0].clone())
            .collect::<Vec<_>>(),
        [2, 5, 7]
            .into_iter()
            .map(|value| Some(IndexKeyValue::Int {
                value,
                width: IntWidth::I64,
            }))
            .collect::<Vec<_>>(),
    );
}

/// With an embedded row-id column the ids come from the column, so a
/// selection must carry each selected row's own id.
#[tokio::test]
async fn selective_read_resolves_embedded_row_ids() {
    let store = Arc::new(InMemory::new());
    let path = Path::from("tagged.parquet");
    write_fixture(&store, &path, &tagged_fixture_batch()).await;

    let wanted: RowPositions = [0, 2].into_iter().collect();
    let entries = scoped_read_entries(
        store.clone(),
        &path,
        &[0],
        ScopedRows::At(&wanted),
        RowIdSource::Resolve { row_id_start: None },
        None,
    )
    .await
    .unwrap();

    assert_eq!(
        entries.iter().map(|entry| entry.row_id).collect::<Vec<_>>(),
        vec![100, 102],
    );
}

/// Selecting nothing reads nothing: a commit whose delete names no
/// surviving position must not touch the store at all.
#[tokio::test]
async fn an_empty_selection_reads_nothing() {
    let store = Arc::new(CountingStore::new());
    let path = Path::from("wide.parquet");
    write_wide_fixture(store.as_ref(), &path, 20_000).await;

    let entries = scoped_read_entries(
        store.clone(),
        &path,
        &[0],
        ScopedRows::At(&RowPositions::default()),
        RowIdSource::Ordinal,
        None,
    )
    .await
    .unwrap();

    assert!(entries.is_empty());
    assert_eq!(store.fetch_requests(), 0, "no selection, no read");
}

/// The delete-path regression: deriving the entries of a handful of rows
/// must not cost what deriving the whole file costs. Before the read
/// took a selection, a delete decoded every row of its target and threw
/// all but the killed ones away — cost set by the file's size rather
/// than the delete's.
///
/// Bytes are the observable proxy. They do not fall as far as the decode
/// does: a selective read still pays for the page index it needs to skip
/// pages with, and for the column's dictionary page, which any page of
/// the chunk needs to decode. Measured ≈3x here; the assertion leaves
/// room, since the point is that the selection reaches the reader at all.
#[tokio::test]
async fn selective_read_costs_a_fraction_of_a_full_read() {
    const ROWS: usize = 400_000;
    let path = Path::from("paged.parquet");
    let wanted: RowPositions = [7, 200_000, 399_999].into_iter().collect();

    let full_store = Arc::new(CountingStore::new());
    write_paged_fixture(full_store.as_ref(), &path, ROWS).await;
    let full = scoped_read_entries(
        full_store.clone(),
        &path,
        &[0],
        ScopedRows::All,
        RowIdSource::Ordinal,
        None,
    )
    .await
    .unwrap();
    assert_eq!(full.len(), ROWS);
    let full_bytes = full_store.fetched_bytes();

    let few_store = Arc::new(CountingStore::new());
    write_paged_fixture(few_store.as_ref(), &path, ROWS).await;
    let few = scoped_read_entries(
        few_store.clone(),
        &path,
        &[0],
        ScopedRows::At(&wanted),
        RowIdSource::Ordinal,
        None,
    )
    .await
    .unwrap();

    // The selected rows, and only those, with the values the full read
    // derived for the same ordinals.
    assert_eq!(
        few,
        wanted
            .as_slice()
            .iter()
            .map(|&position| full[usize::try_from(position).unwrap()].clone())
            .collect::<Vec<_>>(),
    );

    let few_bytes = few_store.fetched_bytes();
    assert!(
        few_bytes * 2 < full_bytes,
        "selecting 3 of {ROWS} rows fetched {few_bytes} B against {full_bytes} B for the \
         whole file — the selection is not reaching the reader",
    );
}

/// A narrow file written in small data pages: the indexed column is most
/// of the object and a row selection has pages to skip. The writer's
/// default would put every row in one page, leaving nothing to skip.
async fn write_paged_fixture(store: &dyn ObjectStore, path: &Path, rows: usize) -> u64 {
    let properties = parquet::file::properties::WriterProperties::builder()
        .set_data_page_row_count_limit(512)
        .build();
    let schema = Arc::new(Schema::new(vec![
        Field::new("id", DataType::Int64, false),
        Field::new("name", DataType::Utf8, false),
    ]));
    let ids: Vec<i64> = (0..i64::try_from(rows).unwrap()).collect();
    let names: Vec<String> = (0..rows).map(|row| format!("name-{row}")).collect();
    let batch = RecordBatch::try_new(
        schema,
        vec![
            Arc::new(Int64Array::from(ids)),
            Arc::new(StringArray::from(names)),
        ],
    )
    .unwrap();

    let mut buffer = Vec::new();
    {
        let mut writer =
            ArrowWriter::try_new(&mut buffer, batch.schema(), Some(properties)).unwrap();
        writer.write(&batch).unwrap();
        writer.close().unwrap();
    }
    let object_len = u64::try_from(buffer.len()).unwrap();
    store.put(path, buffer.into()).await.unwrap();
    object_len
}

/// A NULL embedded row id has no dense fallback to hide behind.
#[tokio::test]
async fn null_embedded_row_id_fails_corruption() {
    let store = Arc::new(InMemory::new());
    let path = Path::from("null-id.parquet");
    let schema = Arc::new(Schema::new(vec![
        Field::new("a", DataType::Int64, true),
        tagged_row_id_field(true),
    ]));
    let batch = RecordBatch::try_new(
        schema,
        vec![
            Arc::new(Int64Array::from(vec![10, 20])),
            Arc::new(Int64Array::from(vec![Some(5), None])),
        ],
    )
    .unwrap();
    write_fixture(&store, &path, &batch).await;

    let err = scoped_read_entries(
        store.clone(),
        &path,
        &[0],
        ScopedRows::All,
        RowIdSource::Resolve { row_id_start: None },
        None,
    )
    .await
    .unwrap_err();
    assert!(matches!(err, Error::Corruption(_)), "{err}");
}

/// A delete file's positions are decoded once per object. The byte-range
/// cache already spares the second pass its fetches; the decode is what
/// is left, and it is the whole cost of a warm read.
#[tokio::test]
async fn a_delete_files_positions_are_decoded_once() {
    let store = Arc::new(CountingStore::new());
    let data = DataStore::new(store.clone());
    let path = Path::from("decoded-once-delete.parquet");
    let (object_len, footer_size) =
        write_wide_named_fixture_with_footer(store.as_ref(), &path, 4_000, "pos").await;

    let metrics = Arc::new(ScopedReadMetrics::default());
    let file = || {
        ParquetFile::new(data.clone(), path.clone(), object_len, footer_size)
            .with_metrics(Arc::clone(&metrics))
    };

    let first = delete_file_positions(file()).await.unwrap();
    assert_eq!(first.len(), 4_000);
    assert_eq!(metrics.tally().parquet_files, 1);

    let second = delete_file_positions(file()).await.unwrap();
    assert_eq!(second, first);
    assert_eq!(
        metrics.tally().parquet_files,
        1,
        "the second pass answers from the memoized positions"
    );
    assert_eq!(store.fetch_requests(), 2, "footer and the position column");
}

/// A memoized position set is keyed by the object, so a delete file at
/// another path decodes on its own.
#[tokio::test]
async fn each_delete_file_memoizes_its_own_positions() {
    let store = Arc::new(CountingStore::new());
    let data = DataStore::new(store.clone());
    let metrics = Arc::new(ScopedReadMetrics::default());

    let mut lengths = Vec::new();
    for (index, rows) in [(0usize, 100usize), (1, 200)] {
        let path = Path::from(format!("per-object-delete-{index}.parquet"));
        let (object_len, footer_size) =
            write_wide_named_fixture_with_footer(store.as_ref(), &path, rows, "pos").await;
        let positions = delete_file_positions(
            ParquetFile::new(data.clone(), path, object_len, footer_size)
                .with_metrics(Arc::clone(&metrics)),
        )
        .await
        .unwrap();
        lengths.push(positions.len());
    }

    assert_eq!(lengths, [100, 200]);
    assert_eq!(metrics.tally().parquet_files, 2);
}

/// A store that answers its first `failures` reads with a body that
/// yields one chunk and then dies, the shape a dropped connection takes
/// mid-read.
#[derive(Debug)]
struct DroppedBodies {
    inner: InMemory,
    failures: AtomicUsize,
    reads: AtomicU64,
}

impl DroppedBodies {
    fn new(failures: usize) -> Self {
        Self {
            inner: InMemory::new(),
            failures: AtomicUsize::new(failures),
            reads: AtomicU64::new(0),
        }
    }

    fn reads(&self) -> u64 {
        self.reads.load(Ordering::Relaxed)
    }
}

impl std::fmt::Display for DroppedBodies {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "DroppedBodies({})", self.inner)
    }
}

#[async_trait::async_trait]
impl ObjectStore for DroppedBodies {
    async fn put_opts(
        &self,
        location: &Path,
        payload: PutPayload,
        opts: PutOptions,
    ) -> object_store::Result<PutResult> {
        self.inner.put_opts(location, payload, opts).await
    }

    async fn put_multipart_opts(
        &self,
        location: &Path,
        opts: PutMultipartOptions,
    ) -> object_store::Result<Box<dyn MultipartUpload>> {
        self.inner.put_multipart_opts(location, opts).await
    }

    async fn get_opts(
        &self,
        location: &Path,
        options: GetOptions,
    ) -> object_store::Result<GetResult> {
        self.reads.fetch_add(1, Ordering::Relaxed);
        let result = self.inner.get_opts(location, options).await?;
        let dropping = self
            .failures
            .fetch_update(Ordering::Relaxed, Ordering::Relaxed, |left| {
                left.checked_sub(1)
            })
            .is_ok();
        if !dropping {
            return Ok(result);
        }

        let body = futures::stream::iter([
            Ok(bytes::Bytes::from_static(b"h")),
            Err(object_store::Error::Generic {
                store: "DroppedBodies",
                source: "connection closed before the body ended".into(),
            }),
        ]);
        Ok(GetResult {
            payload: object_store::GetResultPayload::Stream(Box::pin(body)),
            ..result
        })
    }

    fn delete_stream(
        &self,
        locations: BoxStream<'static, object_store::Result<Path>>,
    ) -> BoxStream<'static, object_store::Result<Path>> {
        self.inner.delete_stream(locations)
    }

    fn list(&self, prefix: Option<&Path>) -> BoxStream<'static, object_store::Result<ObjectMeta>> {
        self.inner.list(prefix)
    }

    async fn list_with_delimiter(&self, prefix: Option<&Path>) -> object_store::Result<ListResult> {
        self.inner.list_with_delimiter(prefix).await
    }

    async fn copy_opts(
        &self,
        from: &Path,
        to: &Path,
        options: CopyOptions,
    ) -> object_store::Result<()> {
        self.inner.copy_opts(from, to, options).await
    }
}

/// A data-file read whose body dies partway is taken again, whole: the
/// transport under a Parquet read fails without failing the read.
#[tokio::test(start_paused = true)]
async fn a_data_file_read_whose_body_dies_partway_is_taken_again() {
    let flaky = Arc::new(DroppedBodies::new(2));
    let path = Path::from("dropped.parquet");
    flaky
        .inner
        .put(&path, PutPayload::from_static(b"parquet bytes"))
        .await
        .unwrap();
    let data = DataStore::new(Arc::clone(&flaky) as Arc<dyn ObjectStore>);

    let bytes = data.read_range(&path, 0..7).await.unwrap();

    assert_eq!(bytes, bytes::Bytes::from_static(b"parquet"));
    assert_eq!(flaky.reads(), 3, "two dropped bodies, then the whole range");
}

/// A body that never settles surfaces its own error, once the read's
/// budget is spent.
#[tokio::test(start_paused = true)]
async fn a_data_file_body_that_never_settles_surfaces_its_error() {
    let flaky = Arc::new(DroppedBodies::new(usize::MAX));
    let path = Path::from("never-settles.parquet");
    flaky
        .inner
        .put(&path, PutPayload::from_static(b"parquet bytes"))
        .await
        .unwrap();
    let data = DataStore::new(Arc::clone(&flaky) as Arc<dyn ObjectStore>);

    let error = data.read_range(&path, 0..7).await.unwrap_err();

    assert!(
        error.to_string().contains("connection closed"),
        "the store's own error must survive the retries: {error}"
    );
    assert_eq!(flaky.reads(), 6, "the read spends its whole budget");
}

/// A missing data file is an answer, not a failure: it is not retried.
#[tokio::test(start_paused = true)]
async fn a_missing_data_file_is_reported_without_a_retry() {
    let flaky = Arc::new(DroppedBodies::new(0));
    let data = DataStore::new(Arc::clone(&flaky) as Arc<dyn ObjectStore>);

    let error = data
        .read_range(&Path::from("absent.parquet"), 0..7)
        .await
        .unwrap_err();

    assert!(
        matches!(error, object_store::Error::NotFound { .. }),
        "a missing file must surface as such: {error}"
    );
    assert_eq!(flaky.reads(), 1, "a terminal answer is not retried");
}