datafusion-ducklake 0.8.0

DuckLake query engine for rust, built with datafusion.
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
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
//! DuckLake table provider implementation

use std::collections::{HashMap, HashSet};
use std::sync::{
    Arc,
    atomic::{AtomicUsize, Ordering},
};

use chrono::{DateTime, Utc};

use crate::Result;
use crate::column_rename::ColumnRenameExec;
use crate::delete_filter::DeleteFilterExec;
use crate::inlined_filter::translate_inlined_filters;
use crate::metadata_provider::{
    DuckLakeFileColumnStatistics, DuckLakeFileData, DuckLakeFileMetadata, DuckLakeNameMapping,
    DuckLakeStatistics, DuckLakeTableColumn, DuckLakeTableColumnStatistics, DuckLakeTableField,
    DuckLakeTableFile, FILE_METADATA_BATCH_SIZE, INLINED_DATA_REMEDIATION, MetadataProvider,
};
use crate::nan_pruning_barrier::NanPruningBarrierExec;
use crate::partition::PartitionSpec;
use crate::path_resolver::resolve_path;
use crate::row_id::{
    ROW_ID_PARQUET_FIELD_ID, ROWID_COLUMN_NAME, RowIdExec, SNAPSHOT_ID_PARQUET_FIELD_ID,
    positional_table_schema, positional_table_schema_reserving, rowid_field,
};
use crate::snapshot_filter::SnapshotFilterExec;
use crate::stats_filter::{self, StatsFilter};
use crate::types::{
    DuckLakeDefaultExprAdapterFactory, INITIAL_DEFAULT_METADATA_KEY,
    build_arrow_schema_from_fields, build_read_schema_with_field_id_mapping,
    build_read_schema_with_field_id_mapping_from_schema, build_read_schema_with_name_mapping,
    ducklake_to_arrow_type, extract_parquet_field_ids, parse_ducklake_default_scalar,
    parse_ducklake_scalar,
};

#[cfg(feature = "write")]
use crate::delete_exec::DuckLakeDeleteExec;
#[cfg(feature = "write")]
use crate::insert_exec::DuckLakeInsertExec;
#[cfg(feature = "write")]
use crate::metadata_writer::{MetadataWriter, WriteMode};
#[cfg(feature = "write")]
use crate::update_exec::DuckLakeUpdateExec;
use datafusion::common::DFSchema;
use datafusion::common::pruning::{PrunableStatistics, PruningStatistics};
#[cfg(feature = "write")]
use datafusion::logical_expr::Operator;
use datafusion::physical_expr::PhysicalExpr;
#[cfg(feature = "write")]
use datafusion::physical_expr::expressions::BinaryExpr;
use datafusion::physical_optimizer::pruning::{PruningPredicate, PruningPredicateBuilder};

#[cfg(feature = "encryption")]
use crate::encryption::DuckLakeEncryptionFactory;
use arrow::array::{Array, ArrayRef, BooleanArray, Int64Array};
use arrow::datatypes::{DataType, Field, Schema, SchemaRef};
use arrow::record_batch::RecordBatch;
use async_trait::async_trait;
use datafusion::catalog::{Session, TableProvider};
use datafusion::common::stats::Precision;
use datafusion::common::{Column, ColumnStatistics, ScalarValue, Statistics};
use datafusion::datasource::listing::PartitionedFile;
use datafusion::datasource::memory::MemorySourceConfig;
use datafusion::datasource::physical_plan::parquet::CachedParquetFileReaderFactory;
use datafusion::datasource::physical_plan::{
    FileGroup, FileScanConfigBuilder, FileSource, ParquetFileReaderFactory, ParquetSource,
};
use datafusion::datasource::source::DataSourceExec;
use datafusion::datasource::table_schema::TableSchema;
use datafusion::error::{DataFusionError, Result as DataFusionResult};
use datafusion::execution::object_store::ObjectStoreUrl;
#[cfg(feature = "write")]
use datafusion::logical_expr::dml::InsertOp;
use datafusion::logical_expr::{Expr, TableProviderFilterPushDown, TableType};
use datafusion::physical_plan::ExecutionPlan;
use datafusion_datasource_parquet::metadata::DFParquetMetadata;
use futures::StreamExt;
use object_store::ObjectMeta;
use object_store::ObjectStoreExt;
use object_store::path::Path as ObjectPath;
use percent_encoding::percent_decode_str;

#[cfg(feature = "encryption")]
use datafusion::execution::parquet_encryption::EncryptionFactory;

// Delete file schema constants (public for testing)
pub const DELETE_FILE_PATH_COL: &str = "file_path";
pub const DELETE_POS_COL: &str = "pos";

/// Parquet field-id DuckLake's own `ducklake` extension assigns to a positional
/// delete file's `file_path` column (its `FILENAME` virtual column). We stamp it
/// on the delete files we WRITE so DuckDB can read our deletes back. This is the
/// DuckDB id (`i32::MAX - 1`), NOT Iceberg's positional-delete id `2147483546`.
pub const DELETE_FILE_PATH_FIELD_ID: i32 = 2_147_483_646;
/// Parquet field-id DuckLake assigns to a positional delete file's `pos` column
/// (its `FILE_ROW_NUMBER`/ordinal virtual column) — the DuckDB id (`i32::MAX -
/// 2`), NOT Iceberg's `2147483545`. See [`DELETE_FILE_PATH_FIELD_ID`].
pub const DELETE_POS_FIELD_ID: i32 = 2_147_483_645;

struct FileMetadataPages<'a> {
    provider: &'a dyn MetadataProvider,
    table_id: i64,
    snapshot_id: i64,
    after_data_file_id: Option<i64>,
    /// Predicate the catalog may use to skip files whose statistics prove they
    /// cannot match, lowered by [`crate::stats_filter`].
    ///
    /// It has to be applied inside the provider's query, before its `LIMIT`.
    /// Filtering a page here instead would break the keyset cursor below: a
    /// page whose candidates all failed the filter would come back empty, the
    /// iteration would stop, and every matching file beyond it would never be
    /// visited. That is why this is threaded to the provider rather than
    /// applied to what the provider returns.
    filter: Option<&'a StatsFilter>,
    page_name: &'static str,
    finished: bool,
}

impl Iterator for FileMetadataPages<'_> {
    type Item = Result<Vec<DuckLakeFileMetadata>>;

    fn next(&mut self) -> Option<Self::Item> {
        if self.finished {
            return None;
        }
        let metadata = match self.provider.get_table_file_metadata_page_filtered(
            self.table_id,
            self.snapshot_id,
            self.after_data_file_id,
            FILE_METADATA_BATCH_SIZE,
            self.filter,
        ) {
            Ok(metadata) => metadata,
            Err(error) => {
                self.finished = true;
                return Some(Err(error));
            },
        };
        if metadata.is_empty() {
            self.finished = true;
            return None;
        }
        if metadata.len() > FILE_METADATA_BATCH_SIZE {
            self.finished = true;
            return Some(Err(crate::DuckLakeError::InvalidConfig(format!(
                "metadata provider returned {} files for a {}-file {} page",
                metadata.len(),
                FILE_METADATA_BATCH_SIZE,
                self.page_name,
            ))));
        }

        let next_after = metadata.last().unwrap().file.data_file_id;
        if self
            .after_data_file_id
            .is_some_and(|after| next_after <= after)
        {
            self.finished = true;
            return Some(Err(crate::DuckLakeError::InvalidConfig(
                "metadata provider returned a non-advancing file page".to_string(),
            )));
        }
        self.after_data_file_id = Some(next_after);
        // A short page means the provider ran out of rows, not that its filter
        // rejected some: `filter` narrows the query ahead of its `LIMIT`, so a
        // full page's worth of matches is always returned when one exists.
        self.finished = metadata.len() < FILE_METADATA_BATCH_SIZE;
        Some(Ok(metadata))
    }
}

/// Build a `PARQUET:field_id` field-metadata map for the given reserved id.
fn parquet_field_id_metadata(field_id: i32) -> HashMap<String, String> {
    HashMap::from([("PARQUET:field_id".to_string(), field_id.to_string())])
}

/// Validate and convert file_size_bytes from i64 (as stored in DuckLake metadata) to u64.
///
/// DuckLake stores file sizes as signed integers in SQL. A negative value indicates
/// corrupt or invalid metadata. Without this check, a negative i64 cast to u64 would
/// wrap to a huge value (e.g., -1 becomes u64::MAX), causing confusing downstream errors.
pub(crate) fn validated_file_size(file_size_bytes: i64, file_path: &str) -> DataFusionResult<u64> {
    u64::try_from(file_size_bytes).map_err(|_| {
        DataFusionError::Execution(format!(
            "Invalid file_size_bytes ({}) for file '{}': value must be non-negative",
            file_size_bytes, file_path
        ))
    })
}

/// Validate and convert record_count from i64 (as stored in DuckLake metadata) to u64.
///
/// DuckLake stores record counts as signed integers in SQL. A negative value indicates
/// corrupt or invalid metadata. Without this check, a negative record_count would cause
/// incorrect behavior (e.g., empty ranges in full-file deletes, or incorrect row filtering).
pub(crate) fn validated_record_count(record_count: i64, file_path: &str) -> DataFusionResult<u64> {
    u64::try_from(record_count).map_err(|_| {
        DataFusionError::Execution(format!(
            "Invalid record_count ({}) for file '{}': value must be non-negative",
            record_count, file_path
        ))
    })
}

fn statistic_usize(value: i64, statistic: &str) -> Option<usize> {
    match usize::try_from(value) {
        Ok(value) => Some(value),
        Err(_) => {
            tracing::warn!(
                value,
                statistic,
                "Ignoring invalid negative DuckLake statistic"
            );
            None
        },
    }
}

/// Decode DuckLake's string representation for min/max statistics into a
/// scalar whose type exactly matches the Arrow field.
fn parse_statistic_scalar(
    value: &str,
    column: &DuckLakeTableColumn,
    data_type: &DataType,
) -> Option<ScalarValue> {
    let ducklake_type = column.column_type.trim().to_ascii_lowercase();

    // These types either have no scalar min/max in DuckLake or use
    // `extra_stats`, which DataFusion's ColumnStatistics cannot represent.
    if ducklake_type.starts_with("list")
        || ducklake_type.starts_with("array")
        || ducklake_type.starts_with("struct")
        || ducklake_type.starts_with("map")
        || matches!(
            ducklake_type.as_str(),
            "geometry"
                | "point"
                | "linestring"
                | "polygon"
                | "multipoint"
                | "multilinestring"
                | "multipolygon"
                | "geometrycollection"
                | "linestring z"
                | "timetz"
                | "time with time zone"
                | "interval"
        )
    {
        return None;
    }

    // Arrow has no representation for DuckDB's infinite date/timestamp
    // sentinels, so leave that bound unknown.
    if matches!(
        value.to_ascii_lowercase().as_str(),
        "infinity" | "-infinity"
    ) {
        return None;
    }

    let parsed = parse_ducklake_scalar(value, data_type);

    if parsed.is_none() {
        tracing::debug!(
            column = %column.column_name,
            ducklake_type = %column.column_type,
            value,
            "Ignoring DuckLake statistic that could not be decoded"
        );
    }
    parsed
}

fn validate_column_defaults(columns: &[DuckLakeTableColumn]) -> Result<HashMap<String, Expr>> {
    let mut defaults = HashMap::new();
    for column in columns {
        let data_type = ducklake_to_arrow_type(&column.column_type)?;
        if let Some(value) = &column.initial_default
            && parse_ducklake_default_scalar(value, &data_type).is_none()
        {
            return Err(crate::DuckLakeError::InvalidConfig(format!(
                "Cannot decode initial_default '{value}' for column '{}' as {}",
                column.column_name, data_type
            )));
        }

        let Some(value) = &column.default_value else {
            continue;
        };
        match column.default_value_type.as_deref() {
            None | Some("literal") => {
                let scalar = parse_ducklake_default_scalar(value, &data_type).ok_or_else(|| {
                    crate::DuckLakeError::InvalidConfig(format!(
                        "Cannot decode default_value '{value}' for column '{}' as {}",
                        column.column_name, data_type
                    ))
                })?;
                defaults.insert(column.column_name.clone(), Expr::Literal(scalar, None));
            },
            Some("expression") => {
                tracing::debug!(
                    column = %column.column_name,
                    dialect = column.default_value_dialect.as_deref().unwrap_or("unknown"),
                    "Ignoring unevaluated DuckLake expression default"
                );
            },
            Some(value_type) => {
                return Err(crate::DuckLakeError::Unsupported(format!(
                    "Default value type '{value_type}' for column '{}' is not supported",
                    column.column_name
                )));
            },
        }
    }
    Ok(defaults)
}

pub(crate) fn apply_initial_default_metadata(
    schema: &Schema,
    columns: &[DuckLakeTableColumn],
    name_mapping: &HashMap<String, String>,
) -> Schema {
    let fields = schema
        .fields()
        .iter()
        .map(|field| {
            let logical_name = name_mapping
                .get(field.name())
                .map_or(field.name().as_str(), String::as_str);
            let Some(initial_default) = columns
                .iter()
                .find(|column| column.column_name == logical_name)
                .and_then(|column| column.initial_default.as_deref())
                .filter(|value| !value.eq_ignore_ascii_case("NULL"))
            else {
                return Arc::clone(field);
            };
            let mut metadata = field.metadata().clone();
            metadata.insert(
                INITIAL_DEFAULT_METADATA_KEY.to_string(),
                initial_default.to_string(),
            );
            Arc::new(field.as_ref().clone().with_metadata(metadata))
        })
        .collect::<Vec<_>>();
    Schema::new_with_metadata(fields, schema.metadata().clone())
}

/// Whether a stored float `min_value` / `max_value` is a usable bound.
///
/// Catalog min/max exclude NaN, so a float column whose NaN state is unknown
/// (`None`, e.g. register-by-reference loads) or positive (`Some(true)`, e.g.
/// stats written by official DuckLake's INSERT) may hold values outside its
/// recorded bounds, and pruning on them would wrongly drop rows.
///
/// **Both** bounds need the gate here, which is where this deliberately differs
/// from official DuckLake — an engine-layer difference, not a format one.
///
/// Official gates only the max, and that is correct for DuckDB, which
/// normalizes NaN: `-NaN = NaN` is true and either sign sorts above every
/// value, so NaN can only ever hide above a recorded max. DataFusion compares
/// floats with arrow's `total_cmp`, which is sign-sensitive:
/// `(-CAST('NaN' AS DOUBLE)) < CAST('-Infinity' AS DOUBLE)` is `true` here and
/// `false` in DuckDB. A negative NaN therefore sits *below* a recorded min in
/// this engine, and `contains_nan` does not record the sign — so neither bound
/// can be trusted.
///
/// Gating both is what keeps pruning consistent with the filter semantics this
/// engine actually has. The underlying `total_cmp`-vs-DuckDB ordering
/// difference is inherited from arrow and predates this gate; it is visible in
/// query *results* for `±NaN` comparisons, not only in pruning.
fn float_bound_is_usable(data_type: &DataType, contains_nan: Option<bool>) -> bool {
    !matches!(
        data_type,
        DataType::Float16 | DataType::Float32 | DataType::Float64
    ) || contains_nan == Some(false)
}

fn scalar_precision(
    value: Option<&str>,
    column: &DuckLakeTableColumn,
    data_type: &DataType,
    exact: bool,
) -> Precision<ScalarValue> {
    match value.and_then(|value| parse_statistic_scalar(value, column, data_type)) {
        Some(value) if exact => Precision::Exact(value),
        Some(value) => Precision::Inexact(value),
        None => Precision::Absent,
    }
}

fn file_row_count(
    file: &DuckLakeTableFile,
    file_columns: Option<&HashMap<i64, DuckLakeFileColumnStatistics>>,
) -> Precision<usize> {
    let gross = file.max_row_count.or_else(|| {
        file_columns.and_then(|columns| columns.values().find_map(|stats| stats.value_count))
    });
    let Some(gross) = gross.and_then(|value| statistic_usize(value, "record_count")) else {
        return Precision::Absent;
    };

    if file.delete_file.is_some() {
        let Some(deleted) = file
            .delete_count
            .and_then(|value| statistic_usize(value, "delete_count"))
        else {
            return Precision::Absent;
        };
        gross
            .checked_sub(deleted)
            .map(Precision::Exact)
            .unwrap_or(Precision::Absent)
    } else {
        Precision::Exact(gross)
    }
}

/// This file's live row count taken from `record_count` alone, for the scan
/// SUMMARY rather than for pruning.
///
/// Deliberately does NOT fall back to a column's `value_count` the way
/// [`file_row_count`] does. `value_count` is the count of NON-NULL values
/// (official: "value_count should be the count of non-null values"), so on a
/// nullable column it is smaller than the row count. As a pruning estimate that
/// is harmless; published as a scan's `num_rows` it becomes the answer to
/// `count(*)`, and a table whose files omit `record_count` would report fewer
/// rows than it holds. Absent beats wrong: the aggregate then reads the data.
///
/// DELETE-FREE FILES ONLY. A file carrying deletes reports `Absent`, because
/// `record_count` alone over-reports it.
///
/// Do not "improve" this into a general count by subtracting `delete_count`:
/// rows removed by an INLINED delete leave `delete_file` and `delete_count`
/// both NULL, so such a file would report its gross count and `count(*)` would
/// over-report by exactly the rows that were deleted. Any future with-deletes
/// summary (see the `count(*)`-after-DELETE convergence issue) must take the
/// inlined-delete map, not just this row.
///
/// CALLER PRECONDITION: files removed by INLINED deletes are not excluded here,
/// because this row cannot see them — an inlined delete leaves `delete_file` and
/// `delete_count` NULL. `scan()` routes any file in `inlined_deletes` to the
/// with-deletes exec, so the only consumer never sees one; a new consumer must
/// make the same exclusion itself, or take the inlined-delete map.
fn file_summary_row_count(file: &DuckLakeTableFile) -> Precision<usize> {
    if file.delete_file.is_some() || file.delete_count.is_some_and(|count| count > 0) {
        return Precision::Absent;
    }
    file.max_row_count
        .and_then(|value| statistic_usize(value, "record_count"))
        .map(Precision::Exact)
        .unwrap_or(Precision::Absent)
}

/// Whether a catalog bound on this type may be a widened approximation rather
/// than the value actually present in the file.
///
/// The DuckLake spec requires `min_value`/`max_value` only to bound the column,
/// not to equal its extremes, and writers exercise that latitude: a parquet
/// writer truncates a long BYTE_ARRAY bound to a prefix and rounds it outward,
/// and `ducklake_add_data_files` ingests those footers verbatim. Answering
/// `max(v)` from such a bound returns a string the table does not contain.
///
/// Official refuses string MIN/MAX from statistics for exactly this reason (its
/// `min_max_optimization_basic` test says so in as many words), so the bounds
/// are usable for PRUNING — where a widened bound is safe — and never for the
/// answer. Numeric and temporal bounds are stored whole and stay usable, including
/// the fixed-length ones: parquet explicitly refuses to truncate `Decimal` and
/// `Float16` statistics because their sort order is not the byte-array sort order.
///
/// This is a TYPE ALLOWLIST standing in for a property the catalog does not
/// record. Parquet carries `is_min_value_exact` / `is_max_value_exact` flags for
/// exactly this question; DuckLake has no column for them, and `add_data_files`
/// ingests footer bounds without them. So the list has to be re-checked whenever
/// the type map in `types.rs` grows a new mapping onto a variable-length physical
/// type — a new type that lands on `Utf8View`/`BinaryView` is covered, one that
/// invents its own is not.
fn bound_may_be_widened(data_type: &DataType) -> bool {
    matches!(
        data_type,
        DataType::Utf8
            | DataType::LargeUtf8
            | DataType::Utf8View
            | DataType::Binary
            | DataType::LargeBinary
            | DataType::BinaryView
            | DataType::FixedSizeBinary(_)
    )
}

/// Whether the catalog *proves* this file holds no rows.
///
/// A file with no rows cannot hold a row matching any predicate, so it is safe to
/// drop from a pruning candidate set without consulting its statistics.
///
/// Only `Some(0)` is proof. `record_count` is optional in the catalog and some
/// providers do not surface it at all, so `None` means "unknown", never "zero":
/// treating it as zero would drop files full of live rows, silently losing them
/// from a scan and — through
/// [`files_matching`](DuckLakeTable::files_matching) — from a keyed mutation's
/// view of the table. Unknown keeps the file, in line with the fail-open contract
/// everywhere else in pruning.
fn file_is_known_empty(file: &DuckLakeTableFile) -> bool {
    file.max_row_count == Some(0)
}

fn build_datafusion_statistics(
    schema: &Schema,
    columns: &[DuckLakeTableColumn],
    table_files: &[DuckLakeTableFile],
    catalog: DuckLakeStatistics,
    use_current_table_statistics: bool,
    file_metadata_complete: bool,
) -> (Statistics, HashMap<i64, Arc<Statistics>>) {
    let table_column_rows: HashMap<i64, DuckLakeTableColumnStatistics> = catalog
        .columns
        .into_iter()
        .map(|stats| (stats.column_id, stats))
        .collect();
    let mut file_column_rows: HashMap<i64, HashMap<i64, DuckLakeFileColumnStatistics>> =
        HashMap::new();
    for stats in catalog.files {
        file_column_rows
            .entry(stats.data_file_id)
            .or_default()
            .insert(stats.column_id, stats);
    }

    let mut file_statistics = HashMap::with_capacity(table_files.len());
    for file in table_files {
        let raw_columns = file_column_rows.get(&file.data_file_id);
        let has_deletes = file.delete_file.is_some();
        let mut statistics = Statistics::new_unknown(schema);
        statistics.num_rows = file_row_count(file, raw_columns);
        statistics.total_byte_size =
            statistic_usize(file.file.file_size_bytes, "data_file.file_size_bytes")
                .map(Precision::Inexact)
                .unwrap_or(Precision::Absent);

        for (index, column) in columns.iter().enumerate() {
            let Some(raw) = raw_columns.and_then(|stats| stats.get(&column.column_id)) else {
                continue;
            };
            let field_type = schema.field(index).data_type();
            let exact = !has_deletes;
            let column_statistics = &mut statistics.column_statistics[index];
            column_statistics.null_count = raw
                .null_count
                .and_then(|value| statistic_usize(value, "file_column_stats.null_count"))
                .map(|value| {
                    if exact {
                        Precision::Exact(value)
                    } else {
                        Precision::Inexact(value)
                    }
                })
                .unwrap_or(Precision::Absent);
            let bounds_usable = float_bound_is_usable(field_type, raw.contains_nan);
            column_statistics.min_value = if bounds_usable {
                scalar_precision(raw.min_value.as_deref(), column, field_type, exact)
            } else {
                Precision::Absent
            };
            column_statistics.max_value = if bounds_usable {
                scalar_precision(raw.max_value.as_deref(), column, field_type, exact)
            } else {
                Precision::Absent
            };
            column_statistics.byte_size = raw
                .column_size_bytes
                .and_then(|value| statistic_usize(value, "file_column_stats.column_size_bytes"))
                .map(Precision::Inexact)
                .unwrap_or(Precision::Absent);
        }

        file_statistics.insert(file.data_file_id, Arc::new(statistics));
    }

    let mut table_statistics = Statistics::new_unknown(schema);

    // Per-file row counts are snapshot-aware and exact when all required
    // counts are present. Fall back to the approximate current-table counter.
    let mut row_total = Some(0usize);
    for file in table_files {
        let value = file_row_count(file, file_column_rows.get(&file.data_file_id));
        row_total = match (row_total, value.get_value()) {
            (Some(total), Some(value)) => total.checked_add(*value),
            _ => None,
        };
    }
    table_statistics.num_rows = if file_metadata_complete && let Some(rows) = row_total {
        Precision::Exact(rows)
    } else if use_current_table_statistics {
        catalog
            .table
            .as_ref()
            .and_then(|stats| stats.record_count)
            .and_then(|value| statistic_usize(value, "table_stats.record_count"))
            .map(Precision::Exact)
            .unwrap_or(Precision::Absent)
    } else {
        Precision::Absent
    };

    // DuckLake stores compressed file bytes while DataFusion describes Arrow
    // output bytes, so this value is necessarily an estimate.
    table_statistics.total_byte_size = if use_current_table_statistics {
        catalog
            .table
            .as_ref()
            .and_then(|stats| stats.file_size_bytes)
            .and_then(|value| statistic_usize(value, "table_stats.file_size_bytes"))
            .map(Precision::Inexact)
            .unwrap_or_else(|| fallback_table_byte_size(table_files))
    } else {
        fallback_table_byte_size(table_files)
    };

    let any_deletes = table_files.iter().any(|file| file.delete_file.is_some());
    for (index, column) in columns.iter().enumerate() {
        let field_type = schema.field(index).data_type();
        let output = &mut table_statistics.column_statistics[index];

        // Table-column rows are not snapshot-versioned. Only use them for the
        // current table generation, and mark bounds inexact because deletes can
        // leave conservative (wider) bounds behind.
        if use_current_table_statistics && let Some(raw) = table_column_rows.get(&column.column_id)
        {
            if raw.contains_null == Some(false) {
                output.null_count = Precision::Exact(0);
            }
            let bounds_usable = float_bound_is_usable(field_type, raw.contains_nan);
            output.min_value = if bounds_usable {
                scalar_precision(
                    raw.min_value.as_deref(),
                    column,
                    field_type,
                    raw.bounds_are_exact,
                )
            } else {
                Precision::Absent
            };
            output.max_value = if bounds_usable {
                scalar_precision(
                    raw.max_value.as_deref(),
                    column,
                    field_type,
                    raw.bounds_are_exact,
                )
            } else {
                Precision::Absent
            };
            output.byte_size = raw
                .column_size_bytes
                .and_then(|value| statistic_usize(value, "file_column_stats.column_size_bytes"))
                .map(Precision::Inexact)
                .unwrap_or(Precision::Absent);
        }

        if !file_metadata_complete {
            continue;
        }

        if table_files.is_empty() {
            output.null_count = Precision::Exact(0);
            output.byte_size = Precision::Exact(0);
            continue;
        }

        let mut null_total = Some(0usize);
        let mut byte_total = Some(0usize);
        let mut min_value: Option<ScalarValue> = None;
        let mut max_value: Option<ScalarValue> = None;
        let mut min_complete = true;
        let mut max_complete = true;

        for file in table_files {
            let Some(raw) = file_column_rows
                .get(&file.data_file_id)
                .and_then(|stats| stats.get(&column.column_id))
            else {
                null_total = None;
                byte_total = None;
                min_complete = false;
                max_complete = false;
                continue;
            };

            null_total = match (
                null_total,
                raw.null_count
                    .and_then(|value| statistic_usize(value, "file_column_stats.null_count")),
            ) {
                (Some(total), Some(value)) => total.checked_add(value),
                _ => None,
            };
            byte_total = match (
                byte_total,
                raw.column_size_bytes.and_then(|value| {
                    statistic_usize(value, "file_column_stats.column_size_bytes")
                }),
            ) {
                (Some(total), Some(value)) => total.checked_add(value),
                _ => None,
            };

            let all_null =
                matches!((raw.value_count, raw.null_count), (Some(v), Some(n)) if v == n);
            // An unusable float min (NaN state unknown/positive) is treated the
            // same way as an unusable max below: negative NaN sorts beneath
            // every value, so the recorded min is not a lower bound.
            let usable_min = raw
                .min_value
                .as_deref()
                .filter(|_| float_bound_is_usable(field_type, raw.contains_nan));
            match usable_min.and_then(|value| parse_statistic_scalar(value, column, field_type)) {
                Some(value) => {
                    min_value = match min_value {
                        Some(current) => current.partial_cmp(&value).map(|ordering| {
                            if ordering.is_le() {
                                current
                            } else {
                                value
                            }
                        }),
                        None => Some(value),
                    };
                    min_complete &= min_value.is_some();
                },
                None if all_null => {},
                None => min_complete = false,
            }
            // An unusable float bound is treated as absent: with `all_null` it
            // contributes nothing, otherwise it poisons `max_complete` so the
            // aggregate degrades to unknown.
            let usable_max = raw
                .max_value
                .as_deref()
                .filter(|_| float_bound_is_usable(field_type, raw.contains_nan));
            match usable_max.and_then(|value| parse_statistic_scalar(value, column, field_type)) {
                Some(value) => {
                    max_value = match max_value {
                        Some(current) => current.partial_cmp(&value).map(|ordering| {
                            if ordering.is_ge() {
                                current
                            } else {
                                value
                            }
                        }),
                        None => Some(value),
                    };
                    max_complete &= max_value.is_some();
                },
                None if all_null => {},
                None => max_complete = false,
            }
        }

        if let Some(value) = null_total {
            output.null_count = if any_deletes {
                Precision::Inexact(value)
            } else {
                Precision::Exact(value)
            };
        }
        if let Some(value) = byte_total {
            output.byte_size = Precision::Inexact(value);
        }
        if min_complete && let Some(value) = min_value {
            output.min_value = if any_deletes {
                Precision::Inexact(value)
            } else {
                Precision::Exact(value)
            };
        }
        if max_complete && let Some(value) = max_value {
            output.max_value = if any_deletes {
                Precision::Inexact(value)
            } else {
                Precision::Exact(value)
            };
        }
    }

    (table_statistics, file_statistics)
}

fn fallback_table_byte_size(table_files: &[DuckLakeTableFile]) -> Precision<usize> {
    let data_bytes: i128 = table_files
        .iter()
        .map(|file| i128::from(file.file.file_size_bytes))
        .sum();
    let delete_bytes: i128 = table_files
        .iter()
        .filter_map(|file| file.delete_file.as_ref())
        .map(|file| i128::from(file.file_size_bytes))
        .sum();
    usize::try_from((data_bytes - delete_bytes).max(0))
        .map(Precision::Inexact)
        .unwrap_or(Precision::Absent)
}

/// Returns the expected schema for DuckLake delete files
///
/// Delete files have a standard schema: (file_path: VARCHAR, pos: INT64).
/// The file_path column records which data file the positions belong to (only
/// `pos` is consumed on read; the catalog already maps delete->data file). Both
/// fields carry DuckLake's reserved parquet field-ids
/// ([`DELETE_FILE_PATH_FIELD_ID`], [`DELETE_POS_FIELD_ID`]) so that delete files
/// WE write are readable by DuckDB's `ducklake` extension. Reads match by column
/// name, so the ids are inert on the read path (files without them still read).
pub fn delete_file_schema() -> SchemaRef {
    Arc::new(Schema::new(vec![
        Field::new(DELETE_FILE_PATH_COL, DataType::Utf8, false)
            .with_metadata(parquet_field_id_metadata(DELETE_FILE_PATH_FIELD_ID)),
        Field::new(DELETE_POS_COL, DataType::Int64, false)
            .with_metadata(parquet_field_id_metadata(DELETE_POS_FIELD_ID)),
    ]))
}

/// One catalog metadata page, resolved: its files, the per-file statistics that
/// drive PRUNING (partition-derived bounds folded in), and the narrower per-file
/// statistics that may be published as a scan SUMMARY. See
/// [`DuckLakeTable::page_files_with_both_statistics`] for why the last two differ.
type PagedFileStatistics = (
    Vec<DuckLakeTableFile>,
    HashMap<i64, Arc<Statistics>>,
    HashMap<i64, Arc<Statistics>>,
);

/// Cached schema mapping for renamed and path-derived columns.
type SchemaMapping = (
    SchemaRef,
    HashMap<String, String>,
    HashMap<String, ScalarValue>,
);

/// Per-file read configuration computed for the row-lineage scan path.
///
/// Encapsulates the decision made by `DuckLakeMultiFileReader::GetVirtualColumnExpression`
/// in the C++ extension: either the parquet file embeds a row-id column
/// (UPDATE/compaction case — surviving rowids preserved across file rewrite),
/// or it doesn't (INSERT-only case — synthesize from `row_id_start + position`).
#[derive(Debug, Clone)]
struct FileReadConfig {
    /// Schema we pass to `ParquetSource::new` for this file. When
    /// `embedded_rowid_parquet_name` is `Some`, this schema has the embedded
    /// rowid column appended at the end (under its parquet name).
    read_schema: SchemaRef,
    /// Parquet-name → user-facing-name renames. Includes the rowid rename
    /// (parquet column → `"rowid"`) when the file has an embedded column with
    /// a different name.
    name_mapping: HashMap<String, String>,
    /// Number of row groups in the file. A scan cannot usefully be split into
    /// more partitions than this — see [`DuckLakeTable::split_across_partitions`].
    row_group_count: usize,
    /// Catalog column indices whose physical name in THIS file differs from the
    /// catalog name — a rename, or the placeholder given to a column the file
    /// predates.
    ///
    /// Indices rather than a flag, and derived positionally rather than from
    /// `name_mapping`, for two reasons. `name_mapping` also carries the
    /// synthetic `_ducklake_internal_row_id -> rowid` entry that every
    /// embedded-rowid file gets, so its emptiness says nothing about data
    /// columns. And a whole-file flag would refuse pushdown for every predicate
    /// on a file that has had any `ADD COLUMN`, however unrelated — see
    /// [`DuckLakeTable::predicate_is_prunable`], which rejects only a predicate
    /// that actually references one of these.
    renamed_column_indices: Arc<HashSet<usize>>,
    /// Logical column name -> value synthesized from this file's Hive path.
    constants: HashMap<String, ScalarValue>,
    /// `Some(parquet_column_name)` if the file embeds the rowid column
    /// (tagged with [`ROW_ID_PARQUET_FIELD_ID`]); `None` otherwise.
    embedded_rowid_parquet_name: Option<String>,
    /// `Some(parquet_column_name)` if the file embeds the per-row snapshot-id
    /// column (tagged with [`SNAPSHOT_ID_PARQUET_FIELD_ID`]) — i.e. it is a
    /// merged partial file; `None` otherwise. Not added to `read_schema` (that
    /// would shift the embedded-rowid column off the end, which several call
    /// sites rely on); the partial-file read path appends it explicitly.
    ///
    /// [`SNAPSHOT_ID_PARQUET_FIELD_ID`]: crate::row_id::SNAPSHOT_ID_PARQUET_FIELD_ID
    embedded_snapshot_parquet_name: Option<String>,
    /// True if the file carries a data column (parquet field-id) that is NOT in
    /// the table's CURRENT schema — i.e. a column dropped since the file was
    /// written. Reads null-drop it harmlessly, but compaction must NOT merge such
    /// a file: merged output is written at the current schema, so the dropped
    /// column's data would be lost (and its sources removed). `merge_adjacent_files`
    /// skips any group containing one.
    drops_current_columns: bool,
}

/// What one file's parquet footer says: the field-id → physical-name map, the
/// arrow schema the reader derives from it, and the row-group boundaries.
///
/// This is the ONLY place the crate parses a data file's footer for read
/// planning, so field-id resolution cannot drift between the table scan and the
/// change feeds.
pub(crate) struct ParquetFooterFacts {
    /// `field_id → physical column name`, at every nesting depth.
    pub(crate) field_ids: HashMap<i32, String>,
    /// The arrow schema the parquet reader derives for this file.
    pub(crate) arrow_schema: SchemaRef,
    /// Number of row groups in the file. Bounds how far a scan of it can
    /// usefully be split across partitions.
    pub(crate) row_group_count: usize,
}

/// A file's footer resolved against a table's CURRENT columns: the schema to
/// scan it with (each column under the physical name THIS file gives it, or a
/// guaranteed-absent name when the file predates the column), the renames back
/// to the catalog names, and the reserved embedded columns it carries.
#[derive(Debug, Clone)]
pub(crate) struct ParquetFileLayout {
    /// One field per current column, in catalog order, under its physical name.
    /// Carries no embedded columns — each read path appends the ones it wants.
    pub(crate) read_schema: SchemaRef,
    /// `physical name → catalog name`, for the columns whose names differ.
    pub(crate) name_mapping: HashMap<String, String>,
    /// Logical column values synthesized from Hive path segments.
    pub(crate) constants: HashMap<String, ScalarValue>,
    /// `Some(parquet_column_name)` if the file embeds the rowid column (tagged
    /// with [`ROW_ID_PARQUET_FIELD_ID`]).
    pub(crate) embedded_rowid_parquet_name: Option<String>,
    /// `Some(parquet_column_name)` if the file embeds the per-row snapshot-id
    /// column ([`SNAPSHOT_ID_PARQUET_FIELD_ID`]) — i.e. it is a merged partial
    /// file.
    pub(crate) embedded_snapshot_parquet_name: Option<String>,
    /// True if the file carries a data column that is NOT in the table's current
    /// schema — a column dropped since the file was written.
    pub(crate) drops_current_columns: bool,
    /// Number of row groups in the file.
    pub(crate) row_group_count: usize,
}

/// The [`ObjectMeta`] DuckLake's own scan-time `PartitionedFile::new(path,
/// size)` builds (see [`DuckLakeTable::partitioned_file`]): epoch
/// `last_modified`, no e_tag or version. A plan-time footer probe that builds
/// this exact shape can share the `RuntimeEnv` `FileMetadataCache` entry with
/// the scan's own `CachedParquetFileReaderFactory` read (#1323) —
/// `CachedFileMetadataEntry::is_valid_for` requires an exact `size` AND
/// `last_modified` match (the cache itself keys only by location), so any
/// other `last_modified` would silently miss the entry the scan populates,
/// and vice versa.
fn epoch_object_meta(location: ObjectPath, size: u64) -> ObjectMeta {
    ObjectMeta {
        location,
        last_modified: DateTime::<Utc>::from_timestamp_nanos(0),
        size,
        e_tag: None,
        version: None,
    }
}

/// Read `resolved_path`'s parquet footer once, through `state`'s shared
/// `RuntimeEnv` file-metadata cache (#1323) rather than the uncached
/// one-shot `ParquetObjectReader` this used before: a warm cache serves this
/// from memory, whether the entry was populated by an earlier call here or by
/// the scan's own `CachedParquetFileReaderFactory` (same cache key — see
/// [`epoch_object_meta`]).
///
/// `file_size_bytes` is the catalog's recorded size for the file, needed to
/// build the `ObjectMeta` the cache validates against. It is taken as a
/// parameter rather than resolved via a `head()` call, which would cost
/// exactly the round trip this function exists to avoid.
///
/// `encryption_key` is the file's DuckLake encryption key when it has one; it
/// is only usable with the `encryption` feature, and this function cannot
/// open an encrypted file without it. Note an encrypted footer is never
/// cached — `DFParquetMetadata::fetch_metadata` treats a fetch made with
/// decryption properties as uncacheable (see its `cache_metadata` gate) — so
/// this saves nothing for that path beyond sharing one code path with the
/// unencrypted one.
pub(crate) async fn read_parquet_footer_facts(
    state: &dyn Session,
    object_store_url: &ObjectStoreUrl,
    resolved_path: &str,
    file_size_bytes: Option<i64>,
    encryption_key: Option<&str>,
) -> DataFusionResult<ParquetFooterFacts> {
    let object_store = state.runtime_env().object_store(object_store_url)?;
    let object_path = ObjectPath::from(resolved_path);
    let size = match file_size_bytes {
        Some(size) => validated_file_size(size, resolved_path)?,
        // The caller doesn't have the catalog's recorded size in reach (a
        // `file_layout` lookup, keyed only by path). One `head()` — not a
        // ranged read, so it never shows up as a footer read — resolves the
        // true size so the `ObjectMeta` built below still matches what a
        // scan's own `PartitionedFile` would carry, and the cache entry is
        // still shared rather than silently keyed apart from it.
        None => object_store.head(&object_path).await?.size,
    };
    let object_meta = epoch_object_meta(object_path, size);

    #[cfg(feature = "encryption")]
    let decryption_properties = match encryption_key {
        Some(key) if !key.is_empty() => {
            let key_bytes = crate::encryption::DuckLakeEncryptionFactory::decode_key(key)?;
            let props = parquet::encryption::decrypt::FileDecryptionProperties::builder(key_bytes)
                .build()
                .map_err(|e| {
                    DataFusionError::Execution(format!(
                        "Failed to create decryption properties: {}",
                        e
                    ))
                })?;
            Some(props)
        },
        _ => None,
    };
    #[cfg(not(feature = "encryption"))]
    let decryption_properties = {
        // Without the feature there is no decryption to configure.
        let _ = encryption_key;
        None
    };

    let metadata = DFParquetMetadata::new(object_store.as_ref(), &object_meta)
        .with_file_metadata_cache(Some(
            state.runtime_env().cache_manager.get_file_metadata_cache(),
        ))
        .with_decryption_properties(decryption_properties)
        .fetch_metadata()
        .await?;

    let field_ids = extract_parquet_field_ids(&metadata);
    let arrow_schema = Arc::new(
        parquet::arrow::parquet_to_arrow_schema(
            metadata.file_metadata().schema_descr(),
            metadata.file_metadata().key_value_metadata(),
        )
        .map_err(|e| DataFusionError::External(Box::new(e)))?,
    );

    Ok(ParquetFooterFacts {
        field_ids,
        arrow_schema,
        row_group_count: metadata.row_groups().len(),
    })
}

/// Build a [`CachedParquetFileReaderFactory`] reading `object_store_url`'s
/// store through `state`'s shared file-metadata cache (the `RuntimeEnv`'s
/// `FileMetadataCache`, sized by `with_metadata_cache_limit`), rather than the
/// uncached `DefaultParquetFileReaderFactory` a bare `ParquetSource::new`
/// falls back to (#1323). Every query-time `ParquetSource` this crate builds
/// should attach one, so a footer fetched once (by this scan, another scan, or
/// the caller's own footer sampling) is never re-fetched for the life of the
/// cache.
pub(crate) fn cached_parquet_reader_factory(
    state: &dyn Session,
    object_store_url: &ObjectStoreUrl,
) -> DataFusionResult<Arc<dyn ParquetFileReaderFactory>> {
    let object_store = state.runtime_env().object_store(object_store_url)?;
    let metadata_cache = state.runtime_env().cache_manager.get_file_metadata_cache();
    Ok(Arc::new(CachedParquetFileReaderFactory::new(
        object_store,
        metadata_cache,
    )))
}

/// Resolve one file's columns against `columns` by field id.
///
/// `fallback_schema` is used verbatim for a file that carries no field ids at
/// all (an external or pre-DuckLake parquet file), where names are the only
/// thing to match on.
pub(crate) async fn read_parquet_file_layout(
    state: &dyn Session,
    object_store_url: &ObjectStoreUrl,
    resolved_path: &str,
    file_size_bytes: Option<i64>,
    encryption_key: Option<&str>,
    columns: &[DuckLakeTableColumn],
    fallback_schema: &SchemaRef,
) -> DataFusionResult<Arc<ParquetFileLayout>> {
    let facts = read_parquet_footer_facts(
        state,
        object_store_url,
        resolved_path,
        file_size_bytes,
        encryption_key,
    )
    .await?;

    let (read_schema, name_mapping) = if facts.field_ids.is_empty() {
        (fallback_schema.clone(), HashMap::new())
    } else {
        let (schema, mapping) = build_read_schema_with_field_id_mapping(
            columns,
            &facts.field_ids,
            Some(facts.arrow_schema.as_ref()),
        )
        .map_err(|e| DataFusionError::External(Box::new(e)))?;
        (Arc::new(schema), mapping)
    };

    // Does the file carry a data column no longer in the current schema? Any
    // parquet field-id that is neither a reserved embedded column nor one of the
    // current catalog `column_id`s is a since-dropped column. Compaction uses
    // this to refuse merging a file whose data would be lost.
    let current_column_ids: HashSet<i32> = columns
        .iter()
        .flat_map(|column| {
            std::iter::once(column.column_id).chain(column.nested_column_ids.iter().copied())
        })
        .map(|column_id| column_id as i32)
        .collect();
    let drops_current_columns = facts.field_ids.keys().any(|fid| {
        *fid != ROW_ID_PARQUET_FIELD_ID
            && *fid != SNAPSHOT_ID_PARQUET_FIELD_ID
            && !current_column_ids.contains(fid)
    });

    Ok(Arc::new(ParquetFileLayout {
        read_schema,
        name_mapping,
        constants: HashMap::new(),
        embedded_rowid_parquet_name: facts.field_ids.get(&ROW_ID_PARQUET_FIELD_ID).cloned(),
        embedded_snapshot_parquet_name: facts.field_ids.get(&SNAPSHOT_ID_PARQUET_FIELD_ID).cloned(),
        drops_current_columns,
        row_group_count: facts.row_group_count,
    }))
}

pub(crate) fn apply_name_mapping_to_layout(
    provider: &dyn MetadataProvider,
    table_id: i64,
    snapshot_id: i64,
    columns: &[DuckLakeTableColumn],
    resolved_path: &str,
    mapping_id: Option<i64>,
    layout: Arc<ParquetFileLayout>,
) -> DataFusionResult<Arc<ParquetFileLayout>> {
    let Some(mapping_id) = mapping_id else {
        return Ok(layout);
    };
    let mapping = provider
        .get_name_mapping(mapping_id)
        .map_err(|e| DataFusionError::External(Box::new(e)))?;
    if mapping.table_id != table_id {
        return Err(DataFusionError::Execution(format!(
            "DuckLake name mapping {mapping_id} belongs to table {}, not table {table_id}",
            mapping.table_id
        )));
    }
    let fields = provider
        .get_table_fields(table_id, snapshot_id)
        .map_err(|e| DataFusionError::External(Box::new(e)))?;
    let mapped = build_read_schema_with_name_mapping(&fields, &mapping)
        .map_err(|e| DataFusionError::External(Box::new(e)))?;
    let constants = mapped
        .partitions
        .into_iter()
        .map(|partition| {
            DuckLakeTable::hive_partition_value(
                resolved_path,
                &partition.source_name,
                &partition.data_type,
            )
            .map(|value| (partition.logical_name, value))
        })
        .collect::<DataFusionResult<HashMap<_, _>>>()?;
    let mut layout = layout.as_ref().clone();
    layout.read_schema = Arc::new(apply_initial_default_metadata(
        &mapped.schema,
        columns,
        &mapped.names,
    ));
    layout.name_mapping = mapped.names;
    layout.constants = constants;
    Ok(Arc::new(layout))
}

/// DuckLake table provider
///
/// Represents a table within a DuckLake schema and provides access to data via Parquet files.
/// Caches snapshot_id and uses it to load all metadata atomically.
///
/// `Clone` shares the `file_read_config_cache` (it is `Arc`-wrapped): a clone is
/// a cheap handle over the same cached parquet metadata. `delete_from` clones the
/// table into the returned `DuckLakeDeleteExec` so the delete work runs at
/// `execute` time (never at plan/EXPLAIN time).
#[derive(Clone)]
pub struct DuckLakeTable {
    #[allow(dead_code)]
    table_id: i64,
    table_name: String,
    #[allow(dead_code)]
    provider: Arc<dyn MetadataProvider>,
    /// Snapshot this table was opened at. Threaded to the delete-commit path as
    /// the `base_snapshot` (the generation the resolved positions were read
    /// against) for conflict diagnostics.
    #[cfg_attr(not(feature = "write"), allow(dead_code))]
    snapshot_id: i64,
    /// Object store URL for resolving file paths (e.g., s3://bucket/ or file:///)
    object_store_url: Arc<ObjectStoreUrl>,
    /// Table path for resolving relative file paths
    table_path: String,
    /// User-facing schema. Equals `physical_schema` when row lineage is off, or
    /// `physical_schema` with a `rowid` BIGINT appended at the end when on.
    schema: SchemaRef,
    /// Schema of the physical (parquet-backed) columns only — no rowid.
    physical_schema: SchemaRef,
    /// When true, `schema` includes a trailing `rowid` column and `scan()`
    /// injects it per-file via [`RowIdExec`].
    row_lineage: bool,
    /// Column metadata from DuckLake (needed for field_id mapping)
    columns: Vec<DuckLakeTableColumn>,
    /// Literal defaults used by DataFusion when an INSERT omits a column
    column_defaults: HashMap<String, Expr>,
    /// Complete nested `ducklake_column` tree used by name mappings.
    fields: Vec<DuckLakeTableField>,
    /// Immutable name mappings cached by `mapping_id` across scans and clones.
    name_mapping_cache: Arc<std::sync::Mutex<HashMap<i64, Arc<DuckLakeNameMapping>>>>,
    /// Table-level statistics for the physical schema.
    table_statistics: Statistics,
    /// The table's active partition spec at `snapshot_id`, if any. Loaded once at
    /// construction and used by `scan()` to synthesize per-file min/max bounds
    /// (from each file's partition values) for partition pruning. `None` for an
    /// unpartitioned table, a catalog without partition support, or a table whose
    /// spec has changed (the provider returns `None` in that case to stay safe).
    partition_spec: Option<PartitionSpec>,
    /// Per-file row-lineage read config, populated lazily on the rowid scan
    /// path. Each file requires its own parquet metadata read to detect an
    /// embedded `_ducklake_internal_row_id` column; we memoize so repeated
    /// scans don't re-fetch. `Arc`-wrapped so a cloned table (see `delete_from`)
    /// shares the same memoized configs.
    file_read_config_cache: Arc<std::sync::Mutex<HashMap<String, Arc<FileReadConfig>>>>,
    /// Rows materialized by the most recent inlined scan before residual filters.
    inlined_materialized_row_count: Arc<AtomicUsize>,
    /// Decryption keys for the encrypted files this table has listed, and the
    /// factory built from them. Shared with every clone of the table, so a key
    /// one of them installs is available to all of them.
    #[cfg(feature = "encryption")]
    encryption_keys: Arc<InstalledEncryptionKeys>,
    /// Schema name (needed for write operations)
    #[cfg(feature = "write")]
    schema_name: Option<String>,
    /// Metadata writer for write operations (when write feature is enabled)
    #[cfg(feature = "write")]
    writer: Option<Arc<dyn MetadataWriter>>,
    /// Write-layout options (compression, row-group caps, file-rollover target)
    /// applied to the writer built for an INSERT into this table.
    #[cfg(feature = "write")]
    pub(crate) write_options: crate::table_writer::DuckLakeWriteOptions,
}

impl std::fmt::Debug for DuckLakeTable {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("DuckLakeTable")
            .field("table_id", &self.table_id)
            .field("table_name", &self.table_name)
            .field("table_path", &self.table_path)
            .field("schema", &self.schema)
            .field("columns", &self.columns)
            .finish_non_exhaustive()
    }
}

/// The decryption keys installed on a table, and the factory every reader
/// clones from them ([`DuckLakeTable::create_parquet_source`]).
///
/// Keys only ever accumulate here: [`DuckLakeTable::install_encryption_keys`]
/// merges a listing's keys into the set instead of replacing it. No single
/// listing sees every file — the catalog leaves files unlisted for
/// [`DuckLakeTable::files_matching`] (see [`crate::stats_filter`]) and a scan
/// installs page by page — so replacing would drop the key of a file the caller
/// is still holding from an earlier listing, with no way to get it back short
/// of listing again. The cell is shared with every clone of the table, so that
/// caller need not be the one that listed.
///
/// Accumulating cannot install a wrong key: a path maps to one key for the life
/// of the snapshot, because DuckLake data files are immutable and a rewritten or
/// compacted file is written under a fresh path. A key kept past its file's last
/// read is dead weight, never a wrong answer, and the set is bounded by the
/// encrypted files this table has listed.
///
/// The factory reads the very map that keys are installed into, so the two
/// halves cannot drift apart. Whatever key a path maps to now is the key a
/// reader gets for it, including after a second install of the same path —
/// which nothing here produces, every data and delete file being named by a
/// fresh UUID, but which the factory would otherwise be free to answer from a
/// stale copy.
///
/// Deliberately not `Debug`: the keys are secrets, which is why
/// [`DuckLakeEncryptionFactory`] prints only its file count.
#[cfg(feature = "encryption")]
#[derive(Default)]
struct InstalledEncryptionKeys {
    /// Resolved file path -> decryption key, unioned over every listing. Only
    /// encrypted files get an entry, so an unencrypted table keeps this empty.
    ///
    /// Shared with `factory` instead of copied into it, so installing a
    /// listing's keys costs one insert per path however many the table has
    /// already collected: a scan of P pages installs O(total keys), not
    /// O(keys per page x P).
    keys: Arc<std::sync::RwLock<HashMap<String, String>>>,
    /// The factory a reader clones, reading through `keys`. Unset until an
    /// encrypted file is listed, so an unencrypted table never attaches one;
    /// set once and never replaced, because a factory over the shared map
    /// already resolves the keys of every later listing. A reader holds the
    /// `Arc` it cloned, so nothing here disturbs a read already planned.
    factory: std::sync::OnceLock<Arc<dyn EncryptionFactory>>,
}

/// Record `path`'s decryption key, ignoring the absent-or-empty key an
/// unencrypted file carries.
///
/// These are the entries
/// [`EncryptionFactoryBuilder::add_file`](crate::encryption::EncryptionFactoryBuilder::add_file)
/// would store; they are collected into a plain map so that successive listings
/// can be unioned before a factory is built from the result.
#[cfg(feature = "encryption")]
fn record_encryption_key(keys: &mut HashMap<String, String>, path: &str, key: Option<&str>) {
    if let Some(key) = key.filter(|key| !key.is_empty()) {
        keys.insert(path.to_string(), key.to_string());
    }
}

impl DuckLakeTable {
    /// Create a new DuckLake table
    pub fn new(
        table_id: i64,
        table_name: impl Into<String>,
        provider: Arc<dyn MetadataProvider>,
        snapshot_id: i64, // Received from schema
        object_store_url: Arc<ObjectStoreUrl>,
        table_path: String,
    ) -> Result<Self> {
        // File metadata is deliberately deferred until scan(), where it can be
        // consumed and pruned in bounded pages.
        let fields = provider.get_table_fields(table_id, snapshot_id)?;
        let columns = provider.get_table_structure(table_id, snapshot_id)?;
        let column_defaults = validate_column_defaults(&columns)?;
        // Active partition spec (if any) for pruning. Loaded once at the bound
        // snapshot; `None` for unpartitioned tables or catalogs without partitions.
        let partition_spec = provider.get_partition_spec(table_id, snapshot_id)?;
        let physical_schema = Arc::new(apply_initial_default_metadata(
            &build_arrow_schema_from_fields(&fields)?,
            &columns,
            &HashMap::new(),
        ));
        let schema = physical_schema.clone();
        let catalog_statistics = provider.get_table_summary_statistics(table_id, snapshot_id)?;
        // `ducklake_table_stats` and `ducklake_table_column_stats` describe the
        // current table generation. They must not be applied to an older
        // snapshot if a newer commit landed after the catalog was opened.
        let use_current_table_statistics = provider.get_current_snapshot()? == snapshot_id;
        let (table_statistics, _) = build_datafusion_statistics(
            physical_schema.as_ref(),
            &columns,
            &[],
            catalog_statistics,
            use_current_table_statistics,
            false,
        );

        // File metadata — and with it every encryption key — is deferred to the
        // first listing, so the table opens with no keys installed.
        #[cfg(feature = "encryption")]
        let encryption_keys = Arc::new(InstalledEncryptionKeys::default());

        Ok(Self {
            table_id,
            table_name: table_name.into(),
            provider,
            snapshot_id,
            object_store_url,
            table_path,
            schema,
            physical_schema,
            row_lineage: false,
            columns,
            column_defaults,
            fields,
            name_mapping_cache: Arc::new(std::sync::Mutex::new(HashMap::new())),
            table_statistics,
            partition_spec,
            #[cfg(feature = "encryption")]
            encryption_keys,
            file_read_config_cache: Arc::new(std::sync::Mutex::new(HashMap::new())),
            inlined_materialized_row_count: Arc::new(AtomicUsize::new(0)),
            #[cfg(feature = "write")]
            schema_name: None,
            #[cfg(feature = "write")]
            writer: None,
            #[cfg(feature = "write")]
            write_options: crate::table_writer::DuckLakeWriteOptions::default(),
        })
    }

    /// Enable / disable the row-lineage feature. When enabled, the table's
    /// public schema includes a trailing `rowid` BIGINT column synthesized
    /// from each row's catalog-recorded `row_id_start + position_in_file`.
    pub fn with_row_lineage(mut self, enabled: bool) -> Self {
        self.row_lineage = enabled;
        self.schema = if enabled {
            let mut fields: Vec<Arc<Field>> =
                self.physical_schema.fields().iter().cloned().collect();
            fields.push(Arc::new(rowid_field()));
            Arc::new(Schema::new(fields))
        } else {
            self.physical_schema.clone()
        };
        self
    }

    /// Rows materialized by the most recent metadata-catalog inlined scan.
    #[must_use]
    pub fn inlined_materialized_row_count(&self) -> usize {
        self.inlined_materialized_row_count.load(Ordering::Relaxed)
    }

    /// Index of the synthetic `rowid` column in `self.schema`, when enabled.
    fn rowid_index(&self) -> Option<usize> {
        self.row_lineage
            .then(|| self.physical_schema.fields().len())
    }

    /// The table's live data files (each with its catalog `data_file_id`, any
    /// live delete file, and that delete file's `delete_file_id`) at the snapshot
    /// this table was opened at. The positional-delete flow iterates these: for
    /// each, [`Self::resolve_positions`] finds the rows to delete,
    /// [`Self::read_delete_file_positions`] reads the already-deleted set, and
    /// the union is written back via `set_delete_file` (CAS on `delete_file_id`).
    pub fn files(&self) -> Result<Vec<DuckLakeTableFile>> {
        let files = self
            .provider
            .get_table_files_for_select(self.table_id, self.snapshot_id)?;
        #[cfg(feature = "encryption")]
        self.configure_encryption_factory(&files)?;
        Ok(files)
    }

    /// The subset of [`Self::files`] whose own catalog statistics permit a match
    /// for `predicate` — every file that could hold a matching row, and none that
    /// is provably empty of them.
    ///
    /// This is the file-level pruning a `SELECT` with the same filter performs,
    /// exposed for callers that drive their own per-file work instead of a scan:
    /// a keyed update, an upsert, or a delete that resolves row positions file by
    /// file with [`Self::resolve_positions`]. It prunes *further* than a scan in
    /// one respect. A scan applies delete files, so it treats a delete-bearing
    /// file's recorded bounds as approximate and keeps the file; these callers do
    /// not apply them, so for them the bounds still hold and the file can be
    /// dropped (see `restate_in_physical_row_space`). Every file a scan would
    /// keep for any other reason is kept here too. Without it such a
    /// caller must open every data file to discover which ones contain a key,
    /// which costs the whole table on every mutation. `predicate` is the same
    /// [`PhysicalExpr`] those callers pass to [`Self::resolve_positions`], so one
    /// expression drives both the file list and the row match; it is resolved
    /// against the table's physical (parquet-backed) columns, without the
    /// synthetic `rowid`.
    ///
    /// Pruning is **fail-open**, and deliberately so: a file is dropped only when
    /// its own statistics *prove* it cannot match. A file with no recorded
    /// statistics, an undecodable partition value, a NULL partition value, or a
    /// bound the catalog only knows approximately is always kept, and any error
    /// building or evaluating the pruning predicate discards the pruning done so
    /// far and returns every file in the page — including files an earlier conjunct
    /// had already excluded, since a failure part-way through leaves no basis for
    /// trusting the exclusions that preceded it. Callers may therefore treat the
    /// result as "these files may match" and must still evaluate `predicate`
    /// against the rows themselves; a mutation that skipped that step could
    /// silently miss a row.
    ///
    /// The one file dropped without consulting statistics is one the catalog
    /// records as holding **no rows** (`record_count` of exactly 0): having no
    /// rows, it cannot hold a matching one. That is a proof rather than an
    /// estimate, so it is the one exclusion the error path above does *not* give
    /// back — such a file is absent from the result whether or not pruning ran, and
    /// whether or not it failed. A file whose row count the catalog leaves unset is
    /// *not* treated as empty; unknown keeps the file, like every other unknown
    /// here.
    ///
    /// How much it prunes depends on how completely the catalog describes the
    /// files. A file that carries no usable bound is kept, while usable bounds on
    /// other files can still prune them.
    ///
    /// Partition values contribute bounds only on their own partition column, and
    /// only when the table has never been re-partitioned (after a re-partition a
    /// live file's values may belong to a retired spec generation whose key order
    /// differs, so they are ignored rather than risk mis-pruning). A predicate
    /// that does not reference a partition column is therefore never pruned by
    /// partition values.
    ///
    /// One limit on fail-open comes from the catalog rather than from here. A
    /// partition value whose recorded key index falls outside the range of a
    /// signed 32-bit integer is read as key index 0 by the SQLite, PostgreSQL
    /// and MySQL providers rather than skipped, so on such a catalog a bound can
    /// be attributed to the wrong partition column and a file that could match
    /// may be dropped. Nothing in this crate writes such a value; it would take a
    /// catalog produced by other means.
    ///
    /// Files are read from the catalog in bounded pages and pruned page by page,
    /// so peak memory tracks the size of the *result*, not the size of the table.
    ///
    /// One addition, on an encrypted table: the decryption key of every file
    /// this *lists* joins the table's installed set, one path/key pair each.
    /// That is a superset of what it returns — each page's keys are collected
    /// before the in-memory pruning runs over that page, so a file dropped
    /// there leaves its key behind, which is what keeps it openable for a
    /// caller holding it from an earlier listing. The set accumulates across
    /// listings rather than being replaced, so a file this call never listed
    /// keeps the key an earlier listing installed for it — see
    /// `InstalledEncryptionKeys` for why that matters and what bounds it.
    ///
    /// # Resolving positions on the returned files
    ///
    /// The returned list may include a file rewritten by an UPDATE or by
    /// compaction, and that is fine: [`Self::resolve_positions`] reads a file's
    /// true physical row positions and does not depend on a row's position
    /// matching `rowid - row_id_start`, so it is valid for rewritten files too.
    /// A rewritten file needs no special handling here.
    pub fn files_matching(
        &self,
        predicate: &Arc<dyn PhysicalExpr>,
    ) -> Result<Vec<DuckLakeTableFile>> {
        let conjuncts = datafusion::physical_expr::split_conjunction(predicate)
            .into_iter()
            .cloned();
        // Fail open: an un-prunable predicate means "keep everything", never an
        // error the caller could mistake for "no files match".
        let pruning = match self.pruning_predicates(conjuncts) {
            Ok(pruning) => pruning,
            Err(error) => {
                tracing::debug!(%error, "skipping predicate-based file pruning");
                Vec::new()
            },
        };
        // The same expression the pruning predicates above were built from,
        // lowered to catalog-statistics SQL so a provider that understands it
        // can stop listing files their statistics rule out — the cost of a
        // selective mutation then tracks the result rather than the table.
        // Fail open here too: `None` means "no SQL filter", and the in-memory
        // pruning still runs over every file that does come back.
        let stats_filter =
            stats_filter::lower_predicate(predicate, &self.physical_schema, &self.columns);

        // Decryption keys for the files these pages carry, which is not every
        // file at the snapshot: the catalog filter above leaves files unlisted,
        // and a file that never reaches this loop cannot have its key collected
        // here. The keys that are collected are therefore MERGED into the
        // table's installed set (`install_encryption_keys`), never installed in
        // place of it, so the key of a file an earlier listing did see stays
        // available to whoever holds that file. Readers clone the factory whole
        // (`create_parquet_source`) out of a cell shared with every clone of
        // this table, so replacing it here would strand exactly those keys — a
        // `resolve_positions` on a file `files` returned, or a scan on a clone.
        //
        // Keeping the retained files' keys still costs one path/key pair per
        // encrypted file returned, on top of whatever the table already holds.
        #[cfg(feature = "encryption")]
        let mut encryption_keys = HashMap::new();

        let mut matching = Vec::new();
        for metadata in self.file_metadata_pages("file matching", stats_filter.as_ref()) {
            let (table_files, mut file_statistics) = self.page_files_with_statistics(metadata?);
            restate_in_physical_row_space(&mut file_statistics, &table_files);
            #[cfg(feature = "encryption")]
            self.collect_encryption_keys(&mut encryption_keys, &table_files)?;
            matching.extend(
                self.prune_table_files_iteratively(&pruning, &table_files, &file_statistics)
                    .into_iter()
                    .cloned(),
            );
        }
        #[cfg(feature = "encryption")]
        self.install_encryption_keys(encryption_keys);
        Ok(matching)
    }

    pub(crate) fn inlined_data_with_row_ids(
        &self,
    ) -> Result<Vec<crate::metadata_provider::DuckLakeInlinedData>> {
        self.provider
            .get_inlined_data_with_row_ids(self.table_id, self.snapshot_id, &self.columns)
    }

    /// Page the catalog's file metadata, optionally handing the provider a
    /// lowered predicate so it can leave provably non-matching files unlisted.
    ///
    /// `filter` is only ever a pre-filter. Every caller still runs the
    /// in-memory [`PruningPredicate`]s over what comes back, because a provider
    /// may ignore the filter entirely (the trait method defaults to doing so)
    /// and because the in-memory pass prunes on things the catalog cannot see —
    /// partition-derived bounds among them.
    fn file_metadata_pages<'a>(
        &'a self,
        page_name: &'static str,
        filter: Option<&'a StatsFilter>,
    ) -> FileMetadataPages<'a> {
        FileMetadataPages {
            provider: self.provider.as_ref(),
            table_id: self.table_id,
            snapshot_id: self.snapshot_id,
            after_data_file_id: None,
            filter,
            page_name,
            finished: false,
        }
    }
    /// Resolve a file path (data or delete file) to its absolute path
    fn resolve_file_path(&self, file: &DuckLakeFileData) -> DataFusionResult<String> {
        resolve_path(&self.table_path, &file.path, file.path_is_relative)
            .map_err(|e| DataFusionError::External(Box::new(e)))
    }

    fn hive_partition_raw_value<'a>(resolved_path: &'a str, source_name: &str) -> Option<&'a str> {
        // Match DuckDB v1.5.5 HivePartitioning::Parse: scan the raw object key,
        // compare raw keys, and leave URL decoding to the value conversion
        // https://github.com/duckdb/duckdb/blob/v1.5.5/src/common/hive_partitioning.cpp
        let mut partition_start = 0;
        let mut equality_sign = 0;
        let mut candidate_partition = true;
        for (index, byte) in resolved_path.bytes().enumerate() {
            if matches!(byte, b'?' | b'\n') {
                candidate_partition = false;
            }
            if matches!(byte, b'/' | b'\\') {
                if candidate_partition && equality_sign > partition_start {
                    let key = &resolved_path[partition_start..equality_sign];
                    if key == source_name {
                        return Some(&resolved_path[equality_sign + 1..index]);
                    }
                }
                partition_start = index + 1;
                candidate_partition = true;
            } else if byte == b'=' {
                if equality_sign > partition_start {
                    candidate_partition = false;
                }
                equality_sign = index;
            }
        }
        None
    }

    fn normalize_hive_hex_integer(value: &str, data_type: &DataType) -> Option<String> {
        if !matches!(
            data_type,
            DataType::Int8
                | DataType::Int16
                | DataType::Int32
                | DataType::Int64
                | DataType::UInt8
                | DataType::UInt16
                | DataType::UInt32
                | DataType::UInt64
        ) {
            return None;
        }
        let digits = value
            .strip_prefix("0x")
            .or_else(|| value.strip_prefix("0X"))?;
        if digits.is_empty()
            || digits
                .split('_')
                .any(|part| part.is_empty() || !part.bytes().all(|byte| byte.is_ascii_hexdigit()))
        {
            return None;
        }
        let digits = digits.replace('_', "");
        u128::from_str_radix(&digits, 16)
            .ok()
            .map(|value| value.to_string())
    }

    fn normalize_hive_exponent_decimal(value: &str, data_type: &DataType) -> Option<String> {
        let (precision, scale) = match data_type {
            DataType::Decimal128(precision, scale) | DataType::Decimal256(precision, scale) => {
                (*precision, *scale)
            },
            _ => return None,
        };
        let exponent_index = value.find(['e', 'E'])?;
        if value[exponent_index + 1..].contains(['e', 'E']) {
            return None;
        }
        let exponent = value[exponent_index + 1..].parse::<i32>().ok()?;
        let mantissa = &value[..exponent_index];
        let (negative, mantissa) = if let Some(mantissa) = mantissa.strip_prefix('-') {
            (true, mantissa)
        } else if let Some(mantissa) = mantissa.strip_prefix('+') {
            (false, mantissa)
        } else {
            (false, mantissa)
        };
        let mut components = mantissa.split('.');
        let whole = components.next()?;
        let fraction = components.next().unwrap_or_default();
        if components.next().is_some()
            || (whole.is_empty() && fraction.is_empty())
            || !whole
                .bytes()
                .chain(fraction.bytes())
                .all(|byte| byte.is_ascii_digit())
        {
            return None;
        }
        let digits = format!("{whole}{fraction}");
        let decimal_index = i64::try_from(whole.len()).ok()? + i64::from(exponent);
        let max_expansion = usize::from(precision)
            .saturating_add(usize::from(scale.unsigned_abs()))
            .saturating_add(digits.len())
            .saturating_add(2);
        let leading_zeros = usize::try_from((-decimal_index).max(0)).ok()?;
        let trailing_zeros =
            usize::try_from((decimal_index - i64::try_from(digits.len()).ok()?).max(0)).ok()?;
        if leading_zeros > max_expansion || trailing_zeros > max_expansion {
            return None;
        }

        let mut normalized = String::with_capacity(digits.len() + max_expansion.min(32));
        if negative {
            normalized.push('-');
        }
        if decimal_index <= 0 {
            normalized.push_str("0.");
            normalized.extend(std::iter::repeat_n('0', leading_zeros));
            normalized.push_str(&digits);
        } else if usize::try_from(decimal_index).ok()? >= digits.len() {
            normalized.push_str(&digits);
            normalized.extend(std::iter::repeat_n('0', trailing_zeros));
        } else {
            let decimal_index = usize::try_from(decimal_index).ok()?;
            normalized.push_str(&digits[..decimal_index]);
            normalized.push('.');
            normalized.push_str(&digits[decimal_index..]);
        }
        Some(normalized)
    }

    pub(crate) fn hive_partition_value(
        resolved_path: &str,
        source_name: &str,
        data_type: &DataType,
    ) -> DataFusionResult<ScalarValue> {
        let raw_value = Self::hive_partition_raw_value(resolved_path, source_name).ok_or_else(|| {
            DataFusionError::Execution(format!(
                "column '{source_name}' uses a DuckLake Hive name mapping but is absent from file path '{resolved_path}'"
            ))
        })?;
        let value = percent_decode_str(raw_value).decode_utf8().map_err(|e| {
            DataFusionError::Execution(format!(
                "invalid percent encoding in Hive partition value '{raw_value}': {e}"
            ))
        })?;
        let value = value.into_owned();
        let is_varchar = matches!(
            data_type,
            DataType::Utf8 | DataType::LargeUtf8 | DataType::Utf8View
        );
        if value == "__HIVE_DEFAULT_PARTITION__"
            || value.eq_ignore_ascii_case("NULL")
            || (value.is_empty() && !is_varchar)
        {
            return ScalarValue::try_from(data_type);
        }
        let scalar_input = Self::normalize_hive_hex_integer(&value, data_type)
            .or_else(|| Self::normalize_hive_exponent_decimal(&value, data_type))
            .unwrap_or_else(|| value.clone());
        ScalarValue::try_from_string(scalar_input, data_type).map_err(|e| {
            DataFusionError::Execution(format!(
                "failed to parse Hive partition '{source_name}={value}' as {data_type}: {e}"
            ))
        })
    }

    fn mapped_schema(
        &self,
        mapping_id: i64,
        resolved_path: &str,
    ) -> DataFusionResult<SchemaMapping> {
        let mapping = {
            let cached = self.name_mapping_cache.lock().unwrap();
            cached.get(&mapping_id).cloned()
        };
        let mapping = match mapping {
            Some(mapping) => mapping,
            None => {
                let mapping = Arc::new(
                    self.provider
                        .get_name_mapping(mapping_id)
                        .map_err(|e| DataFusionError::External(Box::new(e)))?,
                );
                self.name_mapping_cache
                    .lock()
                    .unwrap()
                    .insert(mapping_id, Arc::clone(&mapping));
                mapping
            },
        };
        if mapping.table_id != self.table_id {
            return Err(DataFusionError::Execution(format!(
                "DuckLake name mapping {mapping_id} belongs to table {}, not table {}",
                mapping.table_id, self.table_id
            )));
        }
        let mapped = build_read_schema_with_name_mapping(&self.fields, mapping.as_ref())
            .map_err(|e| DataFusionError::External(Box::new(e)))?;
        let constants = mapped
            .partitions
            .into_iter()
            .map(|partition| {
                Self::hive_partition_value(
                    resolved_path,
                    &partition.source_name,
                    &partition.data_type,
                )
                .map(|value| (partition.logical_name, value))
            })
            .collect::<DataFusionResult<HashMap<_, _>>>()?;
        let schema = apply_initial_default_metadata(&mapped.schema, &self.columns, &mapped.names);
        Ok((Arc::new(schema), mapped.names, constants))
    }

    /// Build a DataFusion file descriptor and attach the catalog's file-level
    /// statistics. `include_rowid` adds an unknown trailing statistic for an
    /// embedded rowid column so the vector still matches the scan schema.
    /// A [`PartitionedFile`] for `file`: its resolved path, validated size, and
    /// the catalog's footer-size hint when it has one.
    fn partitioned_file(&self, file: &DuckLakeFileData) -> DataFusionResult<PartitionedFile> {
        let resolved_path = self.resolve_file_path(file)?;
        let mut pf = PartitionedFile::new(
            &resolved_path,
            validated_file_size(file.file_size_bytes, &resolved_path)?,
        );
        if let Some(footer_size) = file.footer_size
            && footer_size > 0
            && let Ok(hint) = usize::try_from(footer_size)
        {
            pf = pf.with_metadata_size_hint(hint);
        }
        Ok(pf)
    }

    fn partitioned_data_file(
        &self,
        table_file: &DuckLakeTableFile,
        include_rowid: bool,
        file_statistics: &HashMap<i64, Arc<Statistics>>,
    ) -> DataFusionResult<PartitionedFile> {
        let mut file = self.partitioned_file(&table_file.file)?;
        if let Some(statistics) = file_statistics.get(&table_file.data_file_id) {
            let statistics = if include_rowid {
                let mut statistics = statistics.as_ref().clone();
                statistics
                    .column_statistics
                    .push(ColumnStatistics::new_unknown());
                Arc::new(statistics)
            } else {
                Arc::clone(statistics)
            };
            file = file.with_statistics(statistics);
        }
        Ok(file)
    }

    /// Split one catalog metadata page into its files and the per-file DataFusion
    /// statistics that drive pruning, with partition-derived bounds folded in.
    ///
    /// Every path that prunes files — planning a scan, and
    /// [`Self::files_matching`] — builds its statistics here, so they all prune
    /// against identical inputs.
    fn page_files_with_statistics(
        &self,
        metadata: Vec<DuckLakeFileMetadata>,
    ) -> (Vec<DuckLakeTableFile>, HashMap<i64, Arc<Statistics>>) {
        // Summary statistics are deliberately NOT built here. This is the
        // keyed-mutation path (`files_matching`), which pages over the whole
        // table; `summary_statistics_from` deep-clones every file's
        // `ColumnStatistics` including each `ScalarValue` bound, so building a
        // map this caller drops immediately would cost one wasted clone per file
        // per mutation.
        let (table_files, pruning, _) = self.page_files_resolved(metadata, false);
        (table_files, pruning)
    }

    /// As [`Self::page_files_with_statistics`], and additionally the per-file
    /// statistics that may be published as the SCAN SUMMARY.
    ///
    /// Two maps rather than one because `Precision` carries two meanings here
    /// that do not coincide. For pruning, `Exact` means "act on this bound"; a
    /// widened bound is still sound because pruning only ever drops a file that
    /// provably cannot match. For the summary, `Exact` means "this IS the
    /// answer", and `AggregateStatistics` returns it verbatim. Bounds that are
    /// safe to prune on are therefore not all safe to answer from, and the
    /// summary map drops the ones that are not:
    ///
    /// - **partition-derived bounds** are omitted, because the summary is built
    ///   before [`Self::apply_partition_bounds`] runs. Official never derives a
    ///   column bound from a partition value, and a bound synthesised from
    ///   whatever a writer stamped into `ducklake_file_partition_value` has no
    ///   business answering `max(col)`.
    /// - **bounds on types a writer may widen** are dropped — see
    ///   [`bound_may_be_widened`].
    /// - **every bound on a file carrying deletes** is dropped, because the
    ///   catalog's bounds are never tightened when rows are removed, so the
    ///   extreme may name a row that is gone. This mirrors official's
    ///   `min_max_exact` gate.
    /// - **`num_rows`** comes from [`file_summary_row_count`], which subtracts
    ///   deletes and refuses the `value_count` fallback, so it stays exact
    ///   through a DELETE exactly as official's `count(*)` does.
    fn page_files_with_both_statistics(
        &self,
        metadata: Vec<DuckLakeFileMetadata>,
    ) -> PagedFileStatistics {
        self.page_files_resolved(metadata, true)
    }

    /// Shared body of the two page resolvers; `want_summary` decides whether the
    /// (cloning) summary map is built at all.
    fn page_files_resolved(
        &self,
        metadata: Vec<DuckLakeFileMetadata>,
        want_summary: bool,
    ) -> PagedFileStatistics {
        let mut catalog_file_statistics = Vec::new();
        let mut table_files = Vec::with_capacity(metadata.len());
        for DuckLakeFileMetadata {
            file,
            column_statistics,
        } in metadata
        {
            table_files.push(file);
            catalog_file_statistics.extend(column_statistics);
        }
        let (_, mut file_statistics) = build_datafusion_statistics(
            self.physical_schema.as_ref(),
            &self.columns,
            &table_files,
            DuckLakeStatistics {
                files: catalog_file_statistics,
                ..Default::default()
            },
            false,
            true,
        );
        // Derived from the PRISTINE per-file statistics, before partition bounds
        // are folded in below.
        let summary_statistics = if want_summary {
            self.summary_statistics_from(&table_files, &file_statistics)
        } else {
            HashMap::new()
        };
        // Synthesize per-file bounds from partition values so partition columns
        // prune even when a file carries no parquet-derived column statistics.
        self.apply_partition_bounds(&table_files, &mut file_statistics);
        (table_files, file_statistics, summary_statistics)
    }

    /// Narrow the pruning statistics of each file to what may be published as a
    /// scan summary. See [`Self::page_files_with_both_statistics`] for why the
    /// two differ.
    fn summary_statistics_from(
        &self,
        table_files: &[DuckLakeTableFile],
        file_statistics: &HashMap<i64, Arc<Statistics>>,
    ) -> HashMap<i64, Arc<Statistics>> {
        let mut summary = HashMap::with_capacity(table_files.len());
        for file in table_files {
            let Some(pruning) = file_statistics.get(&file.data_file_id) else {
                continue;
            };
            let mut statistics = pruning.as_ref().clone();
            statistics.num_rows = file_summary_row_count(file);
            let has_deletes = file.delete_file.is_some();
            for (index, column) in statistics.column_statistics.iter_mut().enumerate() {
                let widened = self
                    .physical_schema
                    .fields()
                    .get(index)
                    .is_none_or(|field| bound_may_be_widened(field.data_type()));
                if has_deletes || widened {
                    column.min_value = Precision::Absent;
                    column.max_value = Precision::Absent;
                }
            }
            summary.insert(file.data_file_id, Arc::new(statistics));
        }
        summary
    }

    /// Inject partition-derived min/max bounds into per-file statistics so the
    /// existing pruning path ([`Self::prune_table_files_iteratively`]) can drop
    /// partition files that cannot match a predicate on a partition column.
    ///
    /// For each file's `partition_values`, map the value through the active spec to
    /// a source-column min/max envelope ([`crate::partition::PartitionTransform::source_bounds`]
    /// — `identity` exact, `year` a range; other transforms contribute nothing and
    /// the file is kept). Only fills a column bound the catalog left `Absent`, so
    /// real parquet-derived statistics (tighter, and already prunable) are always
    /// preserved. A NULL partition value or an unmappable column is skipped. A bound
    /// is marked `Exact` only when `min == max` (a genuine single-value extreme);
    /// a widened envelope stays `Inexact` so it can never corrupt MIN/MAX-from-
    /// statistics. Either way the envelope satisfies `min <= every row <= max`, so a
    /// file is never wrongly dropped.
    fn apply_partition_bounds(
        &self,
        table_files: &[DuckLakeTableFile],
        file_statistics: &mut HashMap<i64, Arc<Statistics>>,
    ) {
        let Some(spec) = self.partition_spec.as_ref() else {
            return;
        };
        // Only prune when the spec's key→column mapping is known to apply to every
        // live file (a single spec generation ever). After a re-partition a file's
        // values could belong to a retired generation with a different key order,
        // so mapping them through the current spec could mis-prune — skip pruning
        // then (the write path still uses the live spec via `insert_into`).
        if !spec.prune_safe {
            return;
        }
        for file in table_files {
            if file.partition_values.is_empty() {
                continue;
            }
            let mut updates: Vec<(usize, ScalarValue, ScalarValue)> = Vec::new();
            for (key_index, value) in &file.partition_values {
                let Some(value) = value.as_deref() else {
                    continue; // NULL partition value: cannot bound, keep the file.
                };
                let Some(column) = spec
                    .columns
                    .iter()
                    .find(|c| c.partition_key_index == *key_index)
                else {
                    continue;
                };
                let Some(index) = self
                    .columns
                    .iter()
                    .position(|c| c.column_id == column.column_id)
                else {
                    continue;
                };
                let data_type = self.physical_schema.field(index).data_type();
                if let Some((min, max)) = column.transform.source_bounds(value, data_type) {
                    updates.push((index, min, max));
                }
            }
            if updates.is_empty() {
                continue;
            }
            let Some(stats) = file_statistics.get_mut(&file.data_file_id) else {
                continue;
            };
            let stats = Arc::make_mut(stats);
            for (index, min, max) in updates {
                let Some(column_statistics) = stats.column_statistics.get_mut(index) else {
                    continue;
                };
                // Mark the bound `Exact` ONLY when the file holds a single value for
                // the column (`min == max`, e.g. `identity` or an integer `year`
                // column) — then min/max ARE the true extremes, safe both for pruning
                // (`PrunableStatistics` prunes only on `Exact`) and for MIN/MAX-from-
                // statistics. A widened envelope (e.g. `year` on a timestamp) is
                // `Inexact`: it must never be treated as the exact extreme, or
                // `SELECT max(ts)` could be answered from the year boundary. `Inexact`
                // does not prune, so widened partition bounds rely on real column
                // statistics (which DuckLake writers produce) for pruning.
                let exact = min == max;
                if matches!(column_statistics.min_value, Precision::Absent) {
                    column_statistics.min_value = if exact {
                        Precision::Exact(min)
                    } else {
                        Precision::Inexact(min)
                    };
                }
                if matches!(column_statistics.max_value, Precision::Absent) {
                    column_statistics.max_value = if exact {
                        Precision::Exact(max)
                    } else {
                        Precision::Inexact(max)
                    };
                }
            }
        }
    }

    /// Return the files whose catalog column statistics prove they may contain
    /// rows matching every predicate.
    ///
    /// This complements the parquet opener's execution-time `FilePruner`, which
    /// uses the same statistics to skip reading non-matching files. Pruning here
    /// shrinks the physical plan itself, including its file count and aggregate
    /// estimates, so downstream planning sees only the relevant files.
    ///
    /// A file with a live delete file has `Inexact` statistics because its
    /// recorded min/max may cover deleted rows, and such a file is kept — unless
    /// the caller has already re-stated those bounds for a reader that does not
    /// apply deletes, which [`DuckLakeTable::files_matching`] does. A file is
    /// only removed when its own exact statistics prove it cannot match, and any
    /// pruning error abandons the whole attempt: it returns `candidates` — every
    /// input file bar the proven-empty ones — and so gives back files an earlier
    /// conjunct had already excluded, rather than trusting exclusions made before
    /// the failure.
    ///
    /// Files the catalog records as holding no rows are dropped up front by
    /// [`file_is_known_empty`], before any statistics are consulted. That
    /// exclusion rests on a proof rather than on statistics, which is why
    /// `candidates` (not `table_files`) is what both the no-predicates and the
    /// error path return: it is the one narrowing no return path here gives back.
    fn prune_table_files_iteratively<'a>(
        &self,
        predicates: &[PruningPredicate],
        table_files: &'a [DuckLakeTableFile],
        file_statistics: &HashMap<i64, Arc<Statistics>>,
    ) -> Vec<&'a DuckLakeTableFile> {
        // The candidate set every path below falls back to: the input minus the
        // files proven empty. Dropping those is not a pruning decision that can be
        // wrong, so it holds even when pruning is skipped entirely.
        let candidates: Vec<&'a DuckLakeTableFile> = table_files
            .iter()
            .filter(|file| !file_is_known_empty(file))
            .collect();
        let mut retained = candidates.clone();
        if predicates.is_empty() {
            return retained;
        }

        loop {
            let count_before = retained.len();
            for predicate in predicates {
                let mask = match self.file_pruning_mask_for(predicate, &retained, file_statistics) {
                    Ok(mask) => mask,
                    Err(e) => {
                        tracing::debug!(error = %e, "skipping plan-time file pruning");
                        return candidates;
                    },
                };
                debug_assert_eq!(mask.len(), retained.len());
                retained = retained
                    .into_iter()
                    .zip(mask)
                    .filter_map(|(file, keep)| keep.then_some(file))
                    .collect();
            }
            if retained.len() == count_before {
                return retained;
            }
        }
    }

    /// Convert scan filters into physical conjuncts against `physical_schema`.
    ///
    /// The single place `scan`'s logical filters become physical expressions.
    /// Both plan-time pruning paths read the result — the catalog-side
    /// statistics filter that narrows the listing query and the in-memory
    /// [`PruningPredicate`]s that run over what it returns — so building them
    /// once is what stops the two disagreeing about what the predicate is.
    fn physical_conjuncts(
        &self,
        state: &dyn Session,
        filters: &[Expr],
    ) -> DataFusionResult<Vec<Arc<dyn PhysicalExpr>>> {
        let df_schema = DFSchema::try_from(self.physical_schema.as_ref().clone())?;
        filters
            .iter()
            .flat_map(datafusion::logical_expr::utils::split_conjunction)
            .map(|expr| state.create_physical_expr(expr.clone(), &df_schema))
            .collect()
    }

    /// Build one [`PruningPredicate`] per conjunct against `physical_schema` (the
    /// parquet-backed columns, excluding the synthetic rowid) — the schema the
    /// per-file statistics are indexed by. The single place pruning predicates
    /// are constructed, whether the conjuncts came from scan filters or from a
    /// caller's own expression.
    fn pruning_predicates(
        &self,
        conjuncts: impl IntoIterator<Item = Arc<dyn PhysicalExpr>>,
    ) -> DataFusionResult<Vec<PruningPredicate>> {
        conjuncts
            .into_iter()
            .map(|conjunct| {
                PruningPredicateBuilder::new()
                    .with_file_schema(Arc::clone(&self.physical_schema))
                    .try_build(conjunct)
            })
            .collect()
    }

    /// Build a `PruningPredicate` from `filters` and evaluate it against every
    /// file's catalog statistics, returning a keep/drop mask 1:1 with
    /// `self.table_files` (`true` = keep). Filters and statistics are both keyed
    /// to `physical_schema` (the parquet-backed columns, excluding the synthetic
    /// rowid), matching how `file_statistics` is indexed.
    fn file_pruning_mask_for(
        &self,
        pruning: &PruningPredicate,
        table_files: &[&DuckLakeTableFile],
        file_statistics: &HashMap<i64, Arc<Statistics>>,
    ) -> DataFusionResult<Vec<bool>> {
        // A file lacking recorded statistics (e.g. written before statistics
        // were produced) contributes `new_unknown`, which the predicate treats
        // as "cannot prune" — the file is kept.
        let per_file: Vec<Arc<Statistics>> = table_files
            .iter()
            .map(|tf| {
                file_statistics
                    .get(&tf.data_file_id)
                    .map(Arc::clone)
                    .unwrap_or_else(|| Arc::new(Statistics::new_unknown(&self.physical_schema)))
            })
            .collect();
        let stats = FilePruningStatistics::new(per_file, Arc::clone(&self.physical_schema));
        pruning.prune(&stats)
    }

    /// Create a ParquetSource with encryption support if enabled and needed
    /// Build the parquet source for a scan, with the table's encryption factory
    /// attached when one is installed, and a [`CachedParquetFileReaderFactory`]
    /// attached so the scan reads footers through `state`'s shared
    /// file-metadata cache instead of re-fetching them per file per query
    /// (#1323).
    ///
    /// Accepts either a plain file schema or a [`TableSchema`] — the latter is
    /// how a *positional* scan asks the reader for the physical-position virtual
    /// column (see [`positional_table_schema`]).
    fn create_parquet_source(
        &self,
        state: &dyn Session,
        schema: impl Into<TableSchema>,
    ) -> DataFusionResult<ParquetSource> {
        let reader_factory = cached_parquet_reader_factory(state, self.object_store_url.as_ref())?;
        #[cfg(feature = "encryption")]
        if let Some(factory) = self.encryption_keys.factory.get() {
            return Ok(ParquetSource::new(schema)
                .with_encryption_factory(Arc::clone(factory))
                .with_parquet_file_reader_factory(reader_factory));
        }
        Ok(ParquetSource::new(schema).with_parquet_file_reader_factory(reader_factory))
    }

    fn scan_config_builder(&self, source: Arc<dyn FileSource>) -> FileScanConfigBuilder {
        FileScanConfigBuilder::new(self.object_store_url.as_ref().clone(), source)
            .with_expr_adapter(Some(Arc::new(DuckLakeDefaultExprAdapterFactory)))
    }

    /// Add each file's decryption key — and that of its live delete file — to
    /// `keys`, resolving paths the same way the readers do. Separate from
    /// installation so a caller reading the catalog in pages can accumulate keys
    /// across every page and install the whole set once.
    #[cfg(feature = "encryption")]
    fn collect_encryption_keys(
        &self,
        keys: &mut HashMap<String, String>,
        table_files: &[DuckLakeTableFile],
    ) -> Result<()> {
        for table_file in table_files {
            let resolved_path = resolve_path(
                &self.table_path,
                &table_file.file.path,
                table_file.file.path_is_relative,
            )?;
            record_encryption_key(
                keys,
                &resolved_path,
                table_file.file.encryption_key.as_deref(),
            );
            if let Some(delete_file) = &table_file.delete_file {
                let path = resolve_path(
                    &self.table_path,
                    &delete_file.path,
                    delete_file.path_is_relative,
                )?;
                record_encryption_key(keys, &path, delete_file.encryption_key.as_deref());
            }
        }
        Ok(())
    }

    /// Merge one listing's keys into the table's installed set.
    ///
    /// Merging, not replacing: a listing covers only the files it was given —
    /// one page of a scan, or the files the catalog filter left in a
    /// [`Self::files_matching`] page — and a reader may still be holding a file
    /// from an earlier one. See [`InstalledEncryptionKeys`].
    ///
    /// Cost is one insert per path. The factory looks keys up in the same map,
    /// so it is attached once — on the first encrypted file the table lists —
    /// and never rebuilt. A factory owning a copy would instead have every page
    /// of a scan clone every key collected so far, which over P pages of k keys
    /// is k*P*(P+1)/2 entry clones: a 245-page listing of a 1M-file encrypted
    /// table would copy 1.2x10^8 path/key pairs to install 1x10^6.
    ///
    /// A listing that carries no keys does not even take the lock, which is
    /// every page of the unencrypted tables that make up the common case.
    #[cfg(feature = "encryption")]
    fn install_encryption_keys(&self, keys: HashMap<String, String>) {
        if keys.is_empty() {
            return;
        }
        self.encryption_keys.keys.write().unwrap().extend(keys);
        // The first encrypted file this table has listed. Every listing after
        // it reuses this factory, which resolves paths through the map just
        // extended — so the keys it serves and the keys installed are one set,
        // not two that could disagree.
        self.encryption_keys.factory.get_or_init(|| {
            Arc::new(DuckLakeEncryptionFactory::shared(Arc::clone(
                &self.encryption_keys.keys,
            ))) as Arc<dyn EncryptionFactory>
        });
    }

    /// Collect and install the decryption keys of the files in one listing.
    #[cfg(feature = "encryption")]
    fn configure_encryption_factory(&self, table_files: &[DuckLakeTableFile]) -> Result<()> {
        let mut keys = HashMap::new();
        self.collect_encryption_keys(&mut keys, table_files)?;
        self.install_encryption_keys(keys);
        Ok(())
    }

    /// Compute the field_id -> physical-name read schema and rename mapping for a
    /// SINGLE file. Physical column names can differ across files (e.g. a column
    /// renamed after some files were written), so this is resolved per file.
    async fn file_schema_mapping(
        &self,
        state: &dyn Session,
        file: &DuckLakeFileData,
    ) -> DataFusionResult<SchemaMapping> {
        let resolved_path = self.resolve_file_path(file)?;

        // A file with a physical rename mapping resolves entirely from the
        // mapping row (`mapped_schema`) — never from the file's own footer
        // field ids. Checked FIRST, before any I/O: a renamed file used to pay
        // a footer fetch here on every `scan()` whose result this branch then
        // discarded outright (the dominant plan-time footer cost, worse than
        // the execute-time read #1323 originally fixed, since it ran even for
        // files this method never needed to open at all).
        if let Some(mapping_id) = file.mapping_id {
            return self.mapped_schema(mapping_id, &resolved_path);
        }

        let object_store = state
            .runtime_env()
            .object_store(self.object_store_url.as_ref())?;
        let object_path = ObjectPath::from(resolved_path.as_str());
        let object_meta = epoch_object_meta(
            object_path,
            validated_file_size(file.file_size_bytes, &resolved_path)?,
        );
        let metadata_size_hint = file
            .footer_size
            .filter(|&size| size > 0)
            .and_then(|size| usize::try_from(size).ok());

        // Resolve decryption properties (if any) before touching the store, the
        // same way `read_parquet_footer_facts` does.
        #[cfg(feature = "encryption")]
        let decryption_properties = match file.encryption_key.as_deref() {
            Some(key) if !key.is_empty() => {
                let key_bytes = crate::encryption::DuckLakeEncryptionFactory::decode_key(key)?;
                let props =
                    parquet::encryption::decrypt::FileDecryptionProperties::builder(key_bytes)
                        .build()
                        .map_err(|e| {
                            DataFusionError::Execution(format!(
                                "Failed to create decryption properties: {}",
                                e
                            ))
                        })?;
                Some(props)
            },
            _ => None,
        };
        #[cfg(not(feature = "encryption"))]
        let decryption_properties = None;

        // Read the footer through `state`'s shared `RuntimeEnv` file-metadata
        // cache (#1323) — the same cache key a scan's own
        // `CachedParquetFileReaderFactory` read uses (see `epoch_object_meta`),
        // so whichever of the two runs first for a given file populates the
        // entry the other then hits for free. Note an encrypted footer is
        // never cached either way (`DFParquetMetadata::fetch_metadata` treats
        // a fetch made with decryption properties as uncacheable), so this
        // saves nothing new for that path beyond sharing the code with the
        // unencrypted one.
        let metadata = DFParquetMetadata::new(object_store.as_ref(), &object_meta)
            .with_metadata_size_hint(metadata_size_hint)
            .with_file_metadata_cache(Some(
                state.runtime_env().cache_manager.get_file_metadata_cache(),
            ))
            .with_decryption_properties(decryption_properties)
            .fetch_metadata()
            .await?;

        let field_id_map = extract_parquet_field_ids(&metadata);

        // No field_ids means external file - use current schema directly
        if field_id_map.is_empty() {
            return Ok((self.schema.clone(), HashMap::new(), HashMap::new()));
        }

        let arrow_schema = parquet::arrow::parquet_to_arrow_schema(
            metadata.file_metadata().schema_descr(),
            metadata.file_metadata().key_value_metadata(),
        )
        .map_err(|e| DataFusionError::External(Box::new(e)))?;

        let (read_schema, name_mapping) = build_read_schema_with_field_id_mapping_from_schema(
            &self.columns,
            self.physical_schema.as_ref(),
            &field_id_map,
            Some(&arrow_schema),
        )
        .map_err(|e| DataFusionError::External(Box::new(e)))?;

        let read_schema =
            apply_initial_default_metadata(&read_schema, &self.columns, &name_mapping);
        Ok((Arc::new(read_schema), name_mapping, HashMap::new()))
    }

    fn present_file_read_config(
        &self,
        plan: Arc<dyn ExecutionPlan>,
        file_cfg: &FileReadConfig,
        physical_len: usize,
    ) -> Arc<dyn ExecutionPlan> {
        let mut output_fields: Vec<Arc<Field>> =
            self.physical_schema.fields().iter().cloned().collect();
        output_fields.extend(plan.schema().fields().iter().skip(physical_len).cloned());
        Arc::new(ColumnRenameExec::new_with_constants(
            plan,
            Arc::new(Schema::new(output_fields)),
            file_cfg.name_mapping.clone(),
            file_cfg.constants.clone(),
        ))
    }

    /// Scan `data_file` and return the physical positions of rows matching
    /// `predicate`, without applying delete files. These are the positions used
    /// by a delete file's `pos` column and
    /// [`crate::metadata_writer::MetadataWriter::set_delete_file`].
    ///
    /// `predicate` is pushed into the reader for row-group/bloom pruning only
    /// when `predicate_is_prunable` allows: never for a float predicate
    /// (footer bounds exclude NaN, and a hidden NaN row is one a DELETE fails to
    /// delete), and never for a file carrying a physical rename. Otherwise the
    /// whole file is scanned.
    ///
    /// Valid for every data file, including one rewritten by an UPDATE or by
    /// compaction. A delete file's `pos` is the row's **physical** index in the
    /// data file it targets, and that is the space this method returns and the
    /// space [`crate::delete_filter::DeleteFilterExec`] filters in — neither
    /// consults `row_id_start` nor a rowid. A rewritten file's rowids are
    /// therefore irrelevant here: they may be non-contiguous, or ordered
    /// differently from the rows they sit on, without affecting which physical
    /// row a resolved position names.
    pub async fn resolve_positions(
        &self,
        state: &dyn Session,
        data_file: &DuckLakeFileData,
        predicate: Arc<dyn datafusion::physical_expr::PhysicalExpr>,
    ) -> DataFusionResult<HashSet<i64>> {
        // Positional scan of the data file: read the physical data columns and the
        // reader-produced physical row position, WITHOUT applying any delete
        // files. Then evaluate `predicate` per batch and collect the physical
        // positions of matching rows — exactly the `pos` values a positional
        // delete file records.
        //
        // `predicate` is expressed against the table's logical column order
        // (column index i = the i-th logical/data field); `Column::evaluate` is
        // index-based, so it resolves against the read batch regardless of any
        // physical rename. `ROW_POS_COLUMN_NAME` is appended last and is never
        // referenced by the predicate.
        //
        // The projection is the physical data columns only. On a file that
        // embeds a rowid column that column sits last in `read_schema`, so
        // `0..physical_len` excludes it and the predicate's column indices still
        // line up with the table's logical order.
        let file_cfg = self.build_file_read_config(state, data_file).await?;

        // Physical data columns only (logical order), then the reader-produced
        // position column. Embedded/rowid columns are not needed to evaluate the
        // predicate or read positions.
        let (table_schema, pos_table_idx, _pos_name) = positional_table_schema_reserving(
            file_cfg.read_schema.clone(),
            self.physical_schema
                .fields()
                .iter()
                .map(|field| field.name().as_str()),
        );
        let mut proj: Vec<usize> = (0..self.physical_schema.fields().len()).collect();
        proj.push(pos_table_idx);
        let pos_idx = proj.len() - 1;

        // Hand the predicate to the reader when it is safe to prune on, so a
        // DELETE/UPDATE reads only the row groups that can contain matches
        // instead of every row of the file. Positions stay true under pruning
        // because the reader derives them from row-group offsets, and a row the
        // reader drops could not have matched anyway.
        let mut source = self.create_parquet_source(state, table_schema)?;
        if self.predicate_is_prunable(&predicate, &file_cfg) {
            source = source.with_predicate(Arc::clone(&predicate));
        }

        let plan: Arc<dyn ExecutionPlan> = DataSourceExec::from_data_source(
            self.scan_config_builder(Arc::new(source))
                .with_file_group(FileGroup::new(vec![self.partitioned_file(data_file)?]))
                .with_projection_indices(Some(proj))?
                .build(),
        );
        let plan = self.split_across_partitions(plan, state, &file_cfg)?;

        let plan =
            self.present_file_read_config(plan, &file_cfg, self.physical_schema.fields().len());

        let batches = datafusion::physical_plan::collect(plan, state.task_ctx()).await?;

        let mut positions = HashSet::new();
        for batch in &batches {
            let mask = predicate.evaluate(batch)?.into_array(batch.num_rows())?;
            let mask = mask
                .as_any()
                .downcast_ref::<BooleanArray>()
                .ok_or_else(|| {
                    DataFusionError::Execution(
                        "resolve_positions: predicate did not evaluate to a boolean".to_string(),
                    )
                })?;
            let pos = batch
                .column(pos_idx)
                .as_any()
                .downcast_ref::<Int64Array>()
                .ok_or_else(|| {
                    DataFusionError::Internal("physical-position column is not Int64".to_string())
                })?;
            for i in 0..batch.num_rows() {
                // A NULL predicate result is treated as non-match (SQL semantics).
                if mask.is_valid(i) && mask.value(i) {
                    positions.insert(pos.value(i));
                }
            }
        }
        Ok(positions)
    }

    /// Turn a failed delete-file scan into a caller-facing error, replacing the
    /// raw parquet failure with a "delete file is missing" message when the file
    /// really is absent from the object store.
    ///
    /// The absence cannot be read off the error alone: DataFusion's parquet
    /// reader flattens the metadata-fetch failure into a `ParquetError::General`
    /// string, so the underlying `object_store::Error::NotFound` is no longer in
    /// the source chain. Probing the store is only done once a scan has already
    /// failed, so the happy path pays nothing.
    async fn classify_delete_file_read_error(
        &self,
        state: &dyn Session,
        resolved_delete_path: &str,
        err: DataFusionError,
    ) -> DataFusionError {
        let missing = if is_object_store_not_found(&err) {
            true
        } else {
            match state
                .runtime_env()
                .object_store(self.object_store_url.as_ref())
            {
                Ok(store) => matches!(
                    store.head(&ObjectPath::from(resolved_delete_path)).await,
                    Err(object_store::Error::NotFound { .. })
                ),
                Err(_) => false,
            }
        };
        if missing {
            DataFusionError::Execution(format!(
                "Delete file '{resolved_delete_path}' referenced in catalog metadata was not found. This may indicate catalog corruption or that the file was deleted outside of DuckLake."
            ))
        } else {
            err
        }
    }

    /// Read a delete file and return the set of physical row positions it marks
    /// deleted (the `pos` column). Callers use this to form the cumulative
    /// (prior ∪ new) position set when superseding a data file's live delete
    /// file via [`crate::metadata_writer::MetadataWriter::set_delete_file`].
    ///
    /// The delete file is already associated with a specific data file via
    /// metadata; only `pos` is read (the `file_path` column is documentation).
    pub async fn read_delete_file_positions(
        &self,
        state: &dyn Session,
        delete_file: &DuckLakeFileData,
    ) -> DataFusionResult<HashSet<i64>> {
        // Get the standard delete file schema
        let delete_schema = delete_file_schema();

        // Resolve the delete file path
        let resolved_delete_path = self.resolve_file_path(delete_file)?;

        // Create PartitionedFile with footer size hint if available
        let mut pf = PartitionedFile::new(
            &resolved_delete_path,
            validated_file_size(delete_file.file_size_bytes, &resolved_delete_path)?,
        );
        if let Some(footer_size) = delete_file.footer_size
            && footer_size > 0
            && let Ok(hint) = usize::try_from(footer_size)
        {
            pf = pf.with_metadata_size_hint(hint);
        }

        // Create file scan config for the delete file
        let file_scan_config = FileScanConfigBuilder::new(
            self.object_store_url.as_ref().clone(),
            Arc::new(self.create_parquet_source(state, delete_schema)?),
        )
        .with_file_group(FileGroup::new(vec![pf]))
        .build();

        // Use DataSourceExec directly to preserve our ParquetSource with encryption factory
        let exec = DataSourceExec::from_data_source(file_scan_config);

        // Execute and collect all batches
        let task_ctx = state.task_ctx();
        let stream = exec.execute(0, task_ctx)?;

        let collected = stream
            .collect::<Vec<_>>()
            .await
            .into_iter()
            .collect::<DataFusionResult<Vec<_>>>();
        let batches: Vec<RecordBatch> = match collected {
            Ok(batches) => batches,
            Err(e) => {
                return Err(self
                    .classify_delete_file_read_error(state, &resolved_delete_path, e)
                    .await);
            },
        };

        // Extract all positions from all batches
        let mut positions = HashSet::new();
        for batch in batches {
            extract_deleted_positions_from_batch(&batch, &mut positions)?;
        }

        Ok(positions)
    }

    pub(crate) fn inlined_deletes_by_file(&self) -> DataFusionResult<HashMap<i64, HashSet<i64>>> {
        let deletes = self
            .provider
            .get_inlined_deletes(self.table_id, self.snapshot_id)
            .map_err(|error| DataFusionError::External(Box::new(error)))?;
        let mut by_file: HashMap<i64, HashSet<i64>> = HashMap::new();
        for delete in deletes {
            if delete.data_file_id < 0 || delete.row_id < 0 {
                return Err(DataFusionError::Execution(format!(
                    "inlined delete has invalid file_id {} and row_id {}",
                    delete.data_file_id, delete.row_id
                )));
            }
            by_file
                .entry(delete.data_file_id)
                .or_default()
                .insert(delete.row_id);
        }
        Ok(by_file)
    }

    async fn deleted_positions_for_file(
        &self,
        state: &dyn Session,
        table_file: &DuckLakeTableFile,
        inlined_positions: Option<&HashSet<i64>>,
    ) -> DataFusionResult<HashSet<i64>> {
        let mut positions = inlined_positions.cloned().unwrap_or_default();
        if let Some(delete_file) = &table_file.delete_file {
            positions.extend(self.read_delete_file_positions(state, delete_file).await?);
        }
        // Positions are NOT filtered against the file's recorded `record_count`,
        // for two reasons that point the same way.
        //
        // Dropping one would un-delete its row whenever `record_count`
        // under-states the file — the position disappears, nothing matches the
        // row, and a deleted row comes back. And dropping the only position
        // empties the set, which routes the file to the plain-scan branch below,
        // where no delete filtering happens at all.
        //
        // Official does not filter them either: its `SetMaxRowCount` clamp is
        // unreachable, since `DuckLakeFileListEntry::max_row_count` is never
        // assigned anywhere in its tree. It reads every physical row, applies
        // deletes, and lets `count(*)` disagree with its own scan on a corrupt
        // count.
        //
        // The statistics side handles the mismatch instead, without touching
        // execution — see `DeleteFilterExec::deleted_in_range`.
        Ok(positions)
    }

    /// Whether `file` was rewritten — by an UPDATE or by compaction — rather
    /// than only ever inserted. A rewritten file embeds its rows' original row
    /// ids as a reserved parquet column (tagged with
    /// [`ROW_ID_PARQUET_FIELD_ID`]); an insert-only file does not.
    ///
    /// This answers where a row's *rowid* comes from — the embedded column when
    /// there is one, `row_id_start + physical position` otherwise — and nothing
    /// more. It is **not** a precondition for [`Self::resolve_positions`] or for a
    /// keyed mutation. A delete file's `pos` is a row's physical index in the data
    /// file, and a rewrite leaves that meaningful: the rewritten file's rows sit
    /// at `0..n-1` exactly as any other file's do. What a rewrite does disturb is
    /// the rowid *sequence* — `rewrite_data_files` drops deleted rows, so the
    /// surviving rowids carry holes and no longer satisfy
    /// `rowid = row_id_start + position` (the catalog records no `row_id_start`
    /// for such a file at all). That is precisely why positions, not rowids, are
    /// what a delete file records.
    ///
    /// Reads the file's parquet footer once and memoizes the answer.
    pub async fn file_has_embedded_rowid(
        &self,
        state: &dyn Session,
        file: &DuckLakeFileData,
    ) -> DataFusionResult<bool> {
        let cfg = self.build_file_read_config(state, file).await?;
        Ok(cfg.embedded_rowid_parquet_name.is_some())
    }

    /// Roll one scan group's per-file catalog statistics up into the group- and
    /// scan-level [`Statistics`] DataFusion's cost-based rules read.
    ///
    /// Without this a scan reports [`Statistics::new_unknown`], because
    /// `FileScanConfigBuilder` defaults to it and nothing here overrode it. The
    /// per-file statistics were already attached (`partitioned_data_file`), so
    /// pruning and row-group filtering always worked; what was missing is the
    /// summary, and `AggregateStatistics` reads only the summary. The visible
    /// cost was that an unfiltered `count(*)` / `min(col)` / `max(col)` read the
    /// data to recompute numbers the catalog already records — on a 1.4-billion-row
    /// table, two minutes to return one row.
    ///
    /// Official DuckLake answers the same aggregates from its catalog
    /// (`DuckLakeGetPartitionStats` publishes an exact row count, and exact column
    /// bounds when no row has ever been deleted), so this converges on it rather
    /// than adding behaviour of our own.
    ///
    /// The inputs are the SUMMARY statistics from
    /// [`Self::page_files_with_both_statistics`], never the pruning ones. That
    /// distinction is the whole safety argument: the summary map has already
    /// dropped partition-derived bounds, bounds on types a writer may widen, and
    /// every bound on a file carrying deletes, so whatever remains `Exact` here
    /// is a value the table actually holds.
    ///
    /// A *filtered* scan needs no special handling. `supports_filters_pushdown`
    /// declares our filters `Inexact`, so DataFusion keeps a `FilterExec` above
    /// the scan and the aggregate never sees the scan node directly. That coupling
    /// is load-bearing and not obvious: `AggregateStatistics` runs BEFORE
    /// `FilterPushdown` in DataFusion's pipeline, so `FileScanConfig`'s own
    /// filter-downgrade has not happened yet when the fold is decided, and the
    /// published count is post-prune but pre-filter. Declaring any filter class
    /// `Exact` would remove that `FilterExec` and let a filtered `count(*)` fold
    /// to the unfiltered count.
    ///
    /// One place we are deliberately AHEAD of official: official gates MIN/MAX on
    /// a table-wide accumulated `record_count == net_count`, so a TRUNCATE,
    /// overwrite or compaction disables its fold permanently. We compute from the
    /// live file set instead, so we keep folding after those — and correctly,
    /// because our bounds come from the visible files rather than from a global
    /// high-water mark that is never tightened.
    ///
    /// Returns `None` — leaving the scan reporting unknown statistics, exactly as
    /// it did before this existed — when the rollup cannot be trusted:
    ///
    /// - statistics collection is switched off;
    /// - any file's `num_rows` is not `Exact`. Merging a group where one file's
    ///   count is unknown would publish a total lower than the scan returns, and
    ///   `count(*)` would be answered with it. `Precision::Absent` does propagate
    ///   through the merge, so this is belt-and-braces rather than the only
    ///   defence — but it is checked explicitly because a wrong count is the worst
    ///   failure this code can produce.
    ///
    /// When this group's read schema is not IDENTICAL to the table's physical
    /// schema, the row count is still published and every column bound is
    /// suppressed — see the comment at the publish site. Bounds are positional
    /// against the physical schema; the count is not.
    fn roll_up_scan_statistics(
        &self,
        state: &dyn Session,
        file_group: FileGroup,
        read_schema: &SchemaRef,
        summary_statistics: &[Arc<Statistics>],
    ) -> (FileGroup, Option<Statistics>) {
        if !state.config_options().execution.collect_statistics {
            return (file_group, None);
        }
        if summary_statistics.is_empty() || summary_statistics.len() != file_group.len() {
            return (file_group, None);
        }
        if summary_statistics
            .iter()
            .any(|statistics| !matches!(statistics.num_rows, Precision::Exact(_)))
        {
            return (file_group, None);
        }
        let merged = Statistics::try_merge_iter(
            summary_statistics.iter().map(Arc::as_ref),
            self.physical_schema.as_ref(),
        );
        match merged {
            Ok(summary) => {
                // `num_rows` comes from `record_count` and has no relationship to
                // column positions, so it survives a schema this scan reads under a
                // different shape (a nested child field rebuilt by the read-schema
                // mapping is enough to make the two unequal). The BOUNDS do not:
                // they are positional against the table's physical schema. So when
                // the schemas are not identical, publish the count and suppress
                // every bound rather than publishing nothing — otherwise a table
                // with one struct or list column loses the `count(*)` fold it had
                // every right to.
                let summary = if read_schema.as_ref() == self.physical_schema.as_ref() {
                    summary
                } else {
                    let mut unknown = Statistics::new_unknown(read_schema);
                    unknown.num_rows = summary.num_rows;
                    unknown.total_byte_size = summary.total_byte_size;
                    unknown
                };
                let file_group = file_group.with_statistics(Arc::new(summary.clone()));
                (file_group, Some(summary))
            },
            // A merge failure is never a reason to fail the scan: unknown statistics
            // are what this path produced before, and they cost optimisation only,
            // never correctness.
            Err(error) => {
                tracing::debug!(%error, "skipping scan statistics rollup");
                (file_group, None)
            },
        }
    }

    /// Build a single execution plan for all files without delete files
    ///
    /// Groups multiple files into a single efficient execution plan since they don't
    /// need delete filtering.
    async fn build_exec_for_files_without_deletes(
        &self,
        state: &dyn Session,
        files: &[&DuckLakeTableFile],
        file_statistics: &HashMap<i64, Arc<Statistics>>,
        summary_statistics: &HashMap<i64, Arc<Statistics>>,
        projection: Option<&Vec<usize>>,
        limit: Option<usize>,
    ) -> DataFusionResult<Arc<dyn ExecutionPlan>> {
        // Physical column names can differ across files (e.g. a column renamed
        // after some files were written), so the field_id -> physical-name read
        // schema must be resolved PER FILE. Group files that share the same
        // physical schema into one ParquetSource and union the groups; the common
        // case (no schema evolution) stays a single group / single scan.
        let mut groups: Vec<(SchemaMapping, Vec<PartitionedFile>, Vec<Arc<Statistics>>)> =
            Vec::new();
        let mut group_index: HashMap<String, usize> = HashMap::new();

        for table_file in files {
            let mapping = self.file_schema_mapping(state, &table_file.file).await?;
            let pf = self.partitioned_data_file(table_file, false, file_statistics)?;

            // Group key: physical field names + types, then the rename mapping.
            let (read_schema, name_mapping, constants) = &mapping;
            let mut key = String::new();
            for f in read_schema.fields() {
                key.push_str(f.name());
                key.push('\u{1}');
                key.push_str(&format!("{:?}", f.data_type()));
                key.push('\u{2}');
            }
            let mut pairs: Vec<(&String, &String)> = name_mapping.iter().collect();
            pairs.sort();
            for (k, v) in pairs {
                key.push_str(k);
                key.push('\u{3}');
                key.push_str(v);
                key.push('\u{4}');
            }
            let mut constants: Vec<_> = constants.iter().collect();
            constants.sort_by_key(|(name, _)| *name);
            for (name, value) in constants {
                key.push_str(name);
                key.push('\u{5}');
                key.push_str(&format!("{value:?}"));
                key.push('\u{6}');
            }

            // Kept positionally alongside the group's files so the rollup can
            // refuse a group where any file lacks a summary, rather than
            // silently merging a subset.
            let summary = summary_statistics.get(&table_file.data_file_id).cloned();
            match group_index.get(&key) {
                Some(&gi) => {
                    groups[gi].1.push(pf);
                    if let Some(summary) = summary {
                        groups[gi].2.push(summary);
                    }
                },
                None => {
                    group_index.insert(key, groups.len());
                    groups.push((mapping, vec![pf], summary.into_iter().collect()));
                },
            }
        }

        let output_schema = match projection {
            Some(indices) => Arc::new(self.schema.project(indices)?),
            None => self.schema.clone(),
        };

        // Float columns whose NaN state isn't known false for every scanned
        // file: predicates on them must not reach the parquet reader's
        // row-group/page pruning (footer bounds exclude NaN).
        let nan_unsafe_columns = self.nan_unsafe_float_columns(files, file_statistics);

        // Build one scan per physical-schema group; ColumnRenameExec coerces each
        // group to the catalog schema (renamed columns or a differing Arrow type).
        let mut execs: Vec<Arc<dyn ExecutionPlan>> = Vec::with_capacity(groups.len());
        for ((read_schema, name_mapping, constants), partitioned_files, group_summary) in groups {
            let (file_group, summary) = self.roll_up_scan_statistics(
                state,
                FileGroup::new(partitioned_files),
                &read_schema,
                &group_summary,
            );
            let mut builder = self
                .scan_config_builder(Arc::new(
                    self.create_parquet_source(state, read_schema.clone())?,
                ))
                .with_limit(limit)
                .with_file_group(file_group);
            if let Some(summary) = summary {
                builder = builder.with_statistics(summary);
            }

            if let Some(proj) = projection {
                builder = builder.with_projection_indices(Some(proj.clone()))?;
            }

            let parquet_exec: Arc<dyn ExecutionPlan> =
                DataSourceExec::from_data_source(builder.build());

            let mut exec = if !name_mapping.is_empty() || parquet_exec.schema() != output_schema {
                Arc::new(ColumnRenameExec::new_with_constants(
                    parquet_exec,
                    output_schema.clone(),
                    name_mapping,
                    constants,
                )) as Arc<dyn ExecutionPlan>
            } else {
                parquet_exec
            };
            if !nan_unsafe_columns.is_empty() {
                exec = Arc::new(NanPruningBarrierExec::new(
                    exec,
                    Arc::clone(&nan_unsafe_columns),
                ));
            }
            execs.push(exec);
        }

        combine_execution_plans(execs)
    }

    /// Float columns whose stored max is unusable for at least one of `files`
    /// — the NaN state is unknown or positive, so parquet footer bounds (which
    /// exclude NaN) must not drive row-group/page pruning for predicates on
    /// them. Detected via the already-gated per-file statistics: a float
    /// column with an `Absent` max is exactly one whose `contains_nan` isn't
    /// known false (see `float_bound_is_usable`); a file with no statistics entry
    /// is unknown across the board.
    fn nan_unsafe_float_columns(
        &self,
        files: &[&DuckLakeTableFile],
        file_statistics: &HashMap<i64, Arc<Statistics>>,
    ) -> Arc<HashSet<String>> {
        let mut unsafe_columns = HashSet::new();
        for (index, field) in self.physical_schema.fields().iter().enumerate() {
            if !matches!(
                field.data_type(),
                DataType::Float16 | DataType::Float32 | DataType::Float64
            ) {
                continue;
            }
            let any_unsafe = files.iter().any(|file| {
                file_statistics
                    .get(&file.data_file_id)
                    .is_none_or(|statistics| {
                        matches!(
                            statistics.column_statistics[index].max_value,
                            Precision::Absent
                        )
                    })
            });
            if any_unsafe {
                unsafe_columns.insert(field.name().clone());
            }
        }
        Arc::new(unsafe_columns)
    }

    /// Split a freshly built scan across `target_partitions` byte ranges.
    ///
    /// The read paths get this from the physical optimizer's
    /// `repartition_file_scans` rule. The paths that build a plan and run it
    /// themselves — `resolve_positions`, the UPDATE source scan, the compaction
    /// rewrite — never reach that rule (they go straight to
    /// `physical_plan::collect` / `execute`), so they ask for the split here.
    ///
    /// Safe because the physical row position comes from the reader, not from
    /// arrival order: every consumer on these paths reads positions out of the
    /// column, so a byte-range split cannot move a row's position. The scan this
    /// replaced did its own row-group-aligned splitting for the same reason, and
    /// dropping it without this would have made these paths single-threaded.
    ///
    /// Capped at the file's row-group count, because DataFusion splits purely by
    /// byte size and has no idea how many row groups a file has: asking for 8
    /// partitions of a 2-row-group file yields 6 that read nothing and 8 that
    /// each fetch the footer, since these scans install no shared metadata
    /// cache. The partitioning this replaced capped the same way.
    ///
    /// Skipped entirely when `optimizer.repartition_file_scans` is off, so these
    /// paths honour the setting the read paths get from the optimizer rule.
    ///
    /// Falls back to `plan` unchanged if the source declines to split.
    fn split_across_partitions(
        &self,
        plan: Arc<dyn ExecutionPlan>,
        state: &dyn Session,
        file_cfg: &FileReadConfig,
    ) -> DataFusionResult<Arc<dyn ExecutionPlan>> {
        let options = state.config_options();
        if !options.optimizer.repartition_file_scans {
            return Ok(plan);
        }
        let target = state
            .config()
            .target_partitions()
            .min(file_cfg.row_group_count.max(1));
        if target <= 1 {
            return Ok(plan);
        }
        Ok(plan.repartitioned(target, options)?.unwrap_or(plan))
    }

    /// Whether `predicate` may be pushed into the parquet reader for a
    /// [`Self::resolve_positions`] scan of a file described by `file_cfg`.
    ///
    /// Deliberately stricter than the read path's `NanPruningBarrierExec`, on
    /// two counts:
    ///
    /// - **Any float reference disqualifies the predicate**, not just one whose
    ///   file is not known NaN-free. Parquet footer float bounds exclude NaN
    ///   while `NaN > C` evaluates true, so pruning on them can hide a matching
    ///   NaN row — and here a hidden row is not a missing result but a row a
    ///   DELETE silently fails to delete. The read path can afford the precise
    ///   test because it has the catalog's per-file statistics; this path is
    ///   handed a bare [`DuckLakeFileData`] and has none. Plumbing them through
    ///   would let float predicates prune here too.
    ///
    /// - **A predicate referencing a column this file names differently is
    ///   disqualified.** The predicate carries catalog column *names*, and the
    ///   reader resolves a pushed predicate by name against the file's schema,
    ///   which uses the physical names. There is no `ColumnRenameExec` on this
    ///   path to rewrite them (`resolve_positions` evaluates positionally
    ///   instead), so such a column would fail to bind — and an unbindable
    ///   column folds the predicate to false, which prunes every row group and
    ///   deletes nothing.
    ///
    ///   Only the columns the predicate actually references matter, hence
    ///   [`FileReadConfig::renamed_column_indices`] rather than a per-file flag:
    ///   a file older than any `ADD COLUMN` names that column with a
    ///   placeholder, and refusing every predicate on such a file would give up
    ///   pruning on the whole history of a table that has ever gained a column.
    ///
    /// Both rejections fall back to scanning the whole file, which is what this
    /// path always did — never to a wrong answer.
    fn predicate_is_prunable(
        &self,
        predicate: &Arc<dyn datafusion::physical_expr::PhysicalExpr>,
        file_cfg: &FileReadConfig,
    ) -> bool {
        use datafusion::common::tree_node::{TreeNode, TreeNodeRecursion};
        use datafusion::physical_expr::expressions::Column;

        let mut prunable = true;
        predicate
            .apply(|expr| {
                let Some(column) = expr.downcast_ref::<Column>() else {
                    return Ok(TreeNodeRecursion::Continue);
                };
                // An index outside the physical schema cannot be resolved, so
                // treat it as unsafe rather than assuming it is fine.
                let Some(field) = self.physical_schema.fields().get(column.index()) else {
                    prunable = false;
                    return Ok(TreeNodeRecursion::Stop);
                };
                let is_float = matches!(
                    field.data_type(),
                    DataType::Float16 | DataType::Float32 | DataType::Float64
                );
                if is_float || file_cfg.renamed_column_indices.contains(&column.index()) {
                    prunable = false;
                    return Ok(TreeNodeRecursion::Stop);
                }
                Ok(TreeNodeRecursion::Continue)
            })
            .expect("scanning a predicate for column references cannot fail");
        prunable
    }

    /// Configure this table for write operations.
    ///
    /// This method enables write support by attaching a metadata writer and data path.
    /// Once configured, the table can handle INSERT INTO operations.
    ///
    /// # Arguments
    /// * `schema_name` - Name of the schema this table belongs to
    /// * `writer` - Metadata writer for catalog operations
    #[cfg(feature = "write")]
    pub fn with_writer(mut self, schema_name: String, writer: Arc<dyn MetadataWriter>) -> Self {
        self.schema_name = Some(schema_name);
        self.writer = Some(writer);
        self
    }

    /// Set the write-layout options applied to this table's INSERT path
    /// (compression, row-group caps, file-rollover target). Propagated from the
    /// catalog's [`with_write_options`](crate::DuckLakeCatalog::with_write_options).
    #[cfg(feature = "write")]
    pub fn with_write_options(
        mut self,
        options: crate::table_writer::DuckLakeWriteOptions,
    ) -> Self {
        self.write_options = options;
        self
    }

    /// The GROSS row count of one file — every row it holds, deleted rows
    /// included — with all column bounds unknown, sized to `schema`.
    ///
    /// This is what the scan UNDER a [`DeleteFilterExec`] reports, because that
    /// scan really does emit the deleted rows; the exec above it subtracts them
    /// (see its `partition_statistics`). Publishing the net count in both places
    /// would subtract twice.
    ///
    /// Bounds are unknown rather than inherited: the scan's own bounds describe
    /// rows that are about to be filtered out, and the exec above cannot tighten
    /// them, so letting them through would risk answering `max(col)` with a
    /// deleted row's value.
    ///
    /// Keys on `record_count` and nothing else, and that is load-bearing:
    /// `deleted_positions_for_file` bounds the delete set against the same field,
    /// so a file without one publishes no count AND keeps every position. Give
    /// this a fallback (`file_row_count` has a `value_count` one) and the pairing
    /// breaks silently — a count would be published against an unbounded set.
    fn gross_scan_statistics(
        &self,
        state: &dyn Session,
        table_file: &DuckLakeTableFile,
        schema: &SchemaRef,
    ) -> Option<Statistics> {
        if !state.config_options().execution.collect_statistics {
            return None;
        }
        let rows = table_file
            .max_row_count
            .and_then(|value| statistic_usize(value, "record_count"))?;
        let mut statistics = Statistics::new_unknown(schema);
        statistics.num_rows = Precision::Exact(rows);
        Some(statistics)
    }

    /// Build an execution plan for a single file with delete filtering
    ///
    /// Creates a Parquet scan wrapped with a delete filter to exclude deleted rows.
    async fn build_exec_for_file_with_deletes(
        &self,
        state: &dyn Session,
        table_file: &DuckLakeTableFile,
        inlined_positions: Option<&HashSet<i64>>,
        file_statistics: &HashMap<i64, Arc<Statistics>>,
        projection: Option<&Vec<usize>>,
        limit: Option<usize>,
    ) -> DataFusionResult<Arc<dyn ExecutionPlan>> {
        let file_cfg = self.build_file_read_config(state, &table_file.file).await?;

        // Deletes filter by physical row position, so this is a positional path:
        // the scan must also read the reader-produced position column.
        let deleted_positions = self
            .deleted_positions_for_file(state, table_file, inlined_positions)
            .await?;
        let deleted_positions = (!deleted_positions.is_empty()).then_some(deleted_positions);

        let output_schema = match projection {
            Some(indices) => Arc::new(self.schema.project(indices)?),
            None => self.schema.clone(),
        };

        // Explicit parquet projection over `read_schema`. rowid is never
        // projected on this path, so always read only the physical columns —
        // for an embedded-rowid file, `read_schema` has a trailing embedded
        // column we must NOT read here. With `projection = None` that means the
        // physical columns `0..physical_len` (not "all of read_schema").
        let proj_indices: Vec<usize> = match projection {
            Some(indices) => indices.clone(),
            None => (0..self.physical_schema.fields().len()).collect(),
        };

        let exec_after_delete: Arc<dyn ExecutionPlan> = if let Some(positions) = deleted_positions {
            // Positional path: read the physical position alongside the data so
            // deletes are matched by position. No scan-level limit (it would drop
            // rows before the delete filter); DataFusion enforces LIMIT above.
            let (table_schema, pos_table_idx, _pos_name) =
                positional_table_schema(file_cfg.read_schema.clone());
            let gross = self.gross_scan_statistics(state, table_file, table_schema.table_schema());
            let mut proj = proj_indices.clone();
            proj.push(pos_table_idx);
            let pos_index = proj.len() - 1;

            let pf = self.partitioned_data_file(
                table_file,
                file_cfg.embedded_rowid_parquet_name.is_some(),
                file_statistics,
            )?;
            let mut builder = self
                .scan_config_builder(Arc::new(self.create_parquet_source(state, table_schema)?))
                .with_file_group(FileGroup::new(vec![pf]));
            builder = builder.with_projection_indices(Some(proj))?;
            if let Some(gross) = gross {
                builder = builder.with_statistics(gross);
            }
            let scan = DataSourceExec::from_data_source(builder.build());

            Arc::new(DeleteFilterExec::try_new(
                scan,
                table_file.file.path.clone(),
                Arc::new(positions),
                pos_index,
            )?)
        } else {
            // No actual deletes for this file: plain scan, scan-level limit OK.
            let pf = self.partitioned_data_file(
                table_file,
                file_cfg.embedded_rowid_parquet_name.is_some(),
                file_statistics,
            )?;
            let mut builder = self
                .scan_config_builder(Arc::new(
                    self.create_parquet_source(state, file_cfg.read_schema.clone())?,
                ))
                .with_limit(limit)
                .with_file_group(FileGroup::new(vec![pf]));
            builder = builder.with_projection_indices(Some(proj_indices.clone()))?;
            DataSourceExec::from_data_source(builder.build())
        };

        // ColumnRenameExec presents the catalog schema and, on the positional
        // path, drops the internal physical-position column.
        let mut exec: Arc<dyn ExecutionPlan> = if !file_cfg.name_mapping.is_empty()
            || !file_cfg.constants.is_empty()
            || exec_after_delete.schema() != output_schema
        {
            Arc::new(ColumnRenameExec::new_with_constants(
                exec_after_delete,
                output_schema,
                file_cfg.name_mapping.clone(),
                file_cfg.constants.clone(),
            ))
        } else {
            exec_after_delete
        };

        // Both branches above can let a predicate reach the parquet reader's
        // pruning, so both need the NaN barrier: footer float bounds exclude
        // NaN, and pruning on them would silently drop NaN rows. The plain-scan
        // branch (a file whose delete file resolves to no positions) has always
        // been pushdown-capable and was missing this.
        let nan_unsafe_columns =
            self.nan_unsafe_float_columns(std::slice::from_ref(&table_file), file_statistics);
        if !nan_unsafe_columns.is_empty() {
            exec = Arc::new(NanPruningBarrierExec::new(exec, nan_unsafe_columns));
        }
        Ok(exec)
    }

    /// Inspect a single file's parquet metadata for the row-lineage scan
    /// path. Mirrors the per-file logic in `DuckLakeMultiFileReader::
    /// GetVirtualColumnExpression` (ducklake C++): if the file embeds a
    /// column tagged with [`ROW_ID_PARQUET_FIELD_ID`], project that column;
    /// otherwise synthesize rowid from `row_id_start + position`.
    async fn build_file_read_config(
        &self,
        state: &dyn Session,
        file: &DuckLakeFileData,
    ) -> DataFusionResult<Arc<FileReadConfig>> {
        let resolved_path = self.resolve_file_path(file)?;

        {
            let cache = self.file_read_config_cache.lock().unwrap();
            if let Some(cfg) = cache.get(&resolved_path) {
                return Ok(cfg.clone());
            }
        }

        #[cfg(feature = "encryption")]
        let encryption_key = file.encryption_key.as_deref();
        #[cfg(not(feature = "encryption"))]
        let encryption_key = None;

        let layout = read_parquet_file_layout(
            state,
            self.object_store_url.as_ref(),
            &resolved_path,
            Some(file.file_size_bytes),
            encryption_key,
            &self.columns,
            &self.physical_schema,
        )
        .await?;

        let (physical_read_schema, mut name_mapping, constants) =
            if let Some(mapping_id) = file.mapping_id {
                self.mapped_schema(mapping_id, &resolved_path)?
            } else {
                (
                    layout.read_schema.clone(),
                    layout.name_mapping.clone(),
                    HashMap::new(),
                )
            };
        let read_schema = if let Some(ref parquet_name) = layout.embedded_rowid_parquet_name {
            // Append the embedded rowid column to read_schema under its
            // parquet name; ParquetExec will project it by name from the
            // file. We add a `parquet_name → "rowid"` rename so the user
            // sees the column as `rowid` (only needed if the names differ).
            let mut fields: Vec<Arc<Field>> =
                physical_read_schema.fields().iter().cloned().collect();
            fields.push(Arc::new(Field::new(
                parquet_name.clone(),
                DataType::Int64,
                true,
            )));
            if parquet_name != ROWID_COLUMN_NAME {
                name_mapping.insert(parquet_name.clone(), ROWID_COLUMN_NAME.to_string());
            }
            Arc::new(Schema::new(fields))
        } else {
            physical_read_schema.clone()
        };

        // A catalog column reads under a different physical name when the file
        // renamed it or predates it. `physical_read_schema` is one field per
        // current column in catalog order, so the comparison is positional.
        let renamed_column_indices: HashSet<usize> = self
            .physical_schema
            .fields()
            .iter()
            .zip(physical_read_schema.fields())
            .enumerate()
            .filter(|(_, (catalog, physical))| catalog.name() != physical.name())
            .map(|(index, _)| index)
            .collect();

        let cfg = Arc::new(FileReadConfig {
            read_schema,
            row_group_count: layout.row_group_count,
            renamed_column_indices: Arc::new(renamed_column_indices),
            name_mapping,
            constants,
            embedded_rowid_parquet_name: layout.embedded_rowid_parquet_name.clone(),
            embedded_snapshot_parquet_name: layout.embedded_snapshot_parquet_name.clone(),
            drops_current_columns: layout.drops_current_columns,
        });

        {
            let mut cache = self.file_read_config_cache.lock().unwrap();
            cache.entry(resolved_path).or_insert_with(|| cfg.clone());
        }

        Ok(cfg)
    }

    /// Build a plan for a single file when the synthetic `rowid` column is in
    /// the projection. Always uses per-file scans because each file may have a
    /// different layout (embedded rowid vs. synthesized) and a distinct
    /// `row_id_start`.
    ///
    /// Order on the positional path (non-embedded, or any file with deletes):
    ///   DataSourceExec → DeleteFilterExec(?) → RowIdExec(?) → ColumnRenameExec,
    ///   with the position column produced by the scan itself. Embedded-rowid
    ///   files with no deletes keep a plain DataSourceExec → ColumnRenameExec
    ///   (rowid read from the file).
    #[allow(clippy::too_many_arguments, reason = "per-file row-lineage scan inputs stay explicit")]
    async fn build_exec_for_file_with_rowid(
        &self,
        state: &dyn Session,
        table_file: &DuckLakeTableFile,
        inlined_positions: Option<&HashSet<i64>>,
        file_statistics: &HashMap<i64, Arc<Statistics>>,
        user_proj: &[usize],
        rowid_idx: usize,
        limit: Option<usize>,
    ) -> DataFusionResult<Arc<dyn ExecutionPlan>> {
        let file_cfg = self.build_file_read_config(state, &table_file.file).await?;
        let has_embedded = file_cfg.embedded_rowid_parquet_name.is_some();

        // Physical columns to read (everything the user asked for except rowid).
        let physical_proj: Vec<usize> = user_proj
            .iter()
            .filter(|&&i| i != rowid_idx)
            .copied()
            .collect();

        // Match the C++ extension: if the file embeds no rowid column AND the
        // catalog didn't record a `row_id_start`, lineage cannot be
        // reconstructed. Hard-error rather than silently emit NULL/garbage.
        if !has_embedded && table_file.row_id_start.is_none() {
            return Err(DataFusionError::Execution(format!(
                "File \"{}\" has no embedded `_ducklake_internal_row_id` column and no \
                 `row_id_start` set in the catalog — row lineage cannot be reconstructed",
                table_file.file.path
            )));
        }

        // Resolve deletes once.
        let deleted_positions = self
            .deleted_positions_for_file(state, table_file, inlined_positions)
            .await?;
        let deleted_positions = (!deleted_positions.is_empty()).then_some(deleted_positions);
        let has_deletes = deleted_positions.is_some();

        // We need synthesized physical positions when rowid must be synthesized
        // (non-embedded) or when positional deletes must be applied. Embedded-
        // rowid files with no deletes keep the legacy plain scan (rowid read from
        // the file; reader-side pruning and scan-level limit are safe there).
        let needs_position = !has_embedded || has_deletes;

        // Parquet read projection. For embedded files, also read the embedded
        // rowid column; `ColumnRenameExec` later maps it to `rowid` by name, so
        // its position in the read projection is irrelevant.
        let parquet_projection: Vec<usize> = if has_embedded {
            let rowid_col_in_read_schema = file_cfg.read_schema.fields().len() - 1;
            let mut p = physical_proj.clone();
            p.push(rowid_col_in_read_schema);
            p
        } else {
            physical_proj.clone()
        };

        let after_deletes: Arc<dyn ExecutionPlan> = if needs_position {
            // Positional path: the reader produces the physical position, which
            // feeds delete filtering and/or rowid synthesis. No scan-level limit
            // (it would drop rows before delete filtering); DataFusion enforces
            // LIMIT above.
            let (table_schema, pos_table_idx, _pos_name) =
                positional_table_schema(file_cfg.read_schema.clone());
            let mut proj = parquet_projection;
            proj.push(pos_table_idx);
            let pos_index = proj.len() - 1;

            let pf = self.partitioned_data_file(table_file, has_embedded, file_statistics)?;
            let mut builder = self
                .scan_config_builder(Arc::new(self.create_parquet_source(state, table_schema)?))
                .with_file_group(FileGroup::new(vec![pf]));
            builder = builder.with_projection_indices(Some(proj))?;
            let mut plan: Arc<dyn ExecutionPlan> =
                DataSourceExec::from_data_source(builder.build());

            // Filter deletes by position, then (for non-embedded files)
            // synthesize rowid from it.
            if let Some(p) = deleted_positions {
                plan = Arc::new(DeleteFilterExec::try_new(
                    plan,
                    table_file.file.path.clone(),
                    Arc::new(p),
                    pos_index,
                )?);
            }
            if !has_embedded {
                plan = Arc::new(RowIdExec::try_new(
                    plan,
                    table_file.row_id_start,
                    pos_index,
                )?);
            }
            plan
        } else {
            // Embedded rowid, no deletes: legacy plain scan (cardinality-
            // preserving). Keep scan-level limit and reader pruning.
            let pf = self.partitioned_data_file(table_file, true, file_statistics)?;
            let mut builder = self
                .scan_config_builder(Arc::new(
                    self.create_parquet_source(state, file_cfg.read_schema.clone())?,
                ))
                .with_limit(limit)
                .with_file_group(FileGroup::new(vec![pf]));
            builder = builder.with_projection_indices(Some(parquet_projection))?;
            DataSourceExec::from_data_source(builder.build())
        };

        // Wrap with ColumnRenameExec to present the catalog schema. Required when
        // a physical column was renamed in the catalog, when the embedded rowid
        // column's parquet name differs from `"rowid"` (the common case — it's
        // `_ducklake_internal_row_id`), or when the file's physical Arrow type
        // differs from the catalog type (e.g. a DuckDB ARRAY read as
        // FixedSizeList vs the catalog's List). Coerces each column to
        // `output_schema`.
        let output_schema = self.output_schema_for_projection(user_proj, rowid_idx);
        let mut exec = if !file_cfg.name_mapping.is_empty()
            || !file_cfg.constants.is_empty()
            || after_deletes.schema() != output_schema
        {
            Arc::new(ColumnRenameExec::new_with_constants(
                after_deletes,
                output_schema,
                file_cfg.name_mapping.clone(),
                file_cfg.constants.clone(),
            )) as Arc<dyn ExecutionPlan>
        } else {
            after_deletes
        };
        // Every branch here can now let a predicate reach the parquet reader's
        // row-group/page/bloom pruning: the positional branch no longer blocks
        // pushdown, and `DeleteFilterExec` / `RowIdExec` / `ColumnRenameExec`
        // forward it. So the NaN barrier is unconditional — footer float bounds
        // exclude NaN, and pruning on them would silently drop NaN rows.
        let nan_unsafe_columns =
            self.nan_unsafe_float_columns(std::slice::from_ref(&table_file), file_statistics);
        if !nan_unsafe_columns.is_empty() {
            exec = Arc::new(NanPruningBarrierExec::new(exec, nan_unsafe_columns));
        }
        Ok(exec)
    }

    /// Output schema for the rowid-projected per-file plan: physical fields
    /// (using their user-facing renamed names from `self.schema`) interleaved
    /// with the synthetic `rowid` field at `rowid_idx`.
    fn output_schema_for_projection(&self, user_proj: &[usize], rowid_idx: usize) -> SchemaRef {
        let mut fields: Vec<Arc<Field>> = Vec::with_capacity(user_proj.len());
        for &i in user_proj {
            if i == rowid_idx {
                fields.push(Arc::new(rowid_field()));
            } else {
                fields.push(self.schema.fields()[i].clone());
            }
        }
        Arc::new(Schema::new(fields))
    }

    /// Whether `table_file` is a merged partial file being read at a snapshot
    /// BELOW its `partial_max` — i.e. some of its rows originate from snapshots
    /// newer than the read snapshot and must be dropped per-row. When false (the
    /// common case: an ordinary file, or a partial file read at or after
    /// `partial_max`), the file is read by the existing paths with no filtering.
    fn needs_snapshot_filter(&self, table_file: &DuckLakeTableFile) -> bool {
        table_file
            .partial_max
            .is_some_and(|partial_max| self.snapshot_id < partial_max)
    }

    /// Build a plan for a single merged **partial file** read at a snapshot below
    /// its `partial_max`. Reads every column (data + embedded rowid + embedded
    /// snapshot-id), drops rows whose embedded origin snapshot exceeds the read
    /// snapshot via [`SnapshotFilterExec`], then presents `output_schema` (which
    /// also projects away the embedded snapshot-id and any unrequested embedded
    /// rowid column). Used only on the cold time-travel path, so it reads all
    /// columns and lets `LIMIT` apply above rather than pushing it into the scan.
    async fn build_exec_for_partial_file(
        &self,
        state: &dyn Session,
        table_file: &DuckLakeTableFile,
        inlined_positions: Option<&HashSet<i64>>,
        output_schema: SchemaRef,
    ) -> DataFusionResult<Arc<dyn ExecutionPlan>> {
        // This path applies no delete filtering, and that has been safe only
        // because the combination cannot arise: a delete on a merged partial file
        // is authored in a snapshot later than the merge that produced it, which
        // is itself later than `partial_max`, while this path is chosen only when
        // reading BELOW `partial_max` (see `needs_snapshot_filter`) — and the
        // metadata provider attaches a delete file only from its own
        // `begin_snapshot` onward. So no delete file can be live at a snapshot
        // this path serves.
        //
        // Nothing has been observed to violate that; the check exists because the
        // assumption is newly load-bearing. Positional deletes on rewritten and
        // partial files used to be refused outright, so this path could not meet
        // one; now that it can in principle, an invariant that was safely
        // implicit is worth failing loudly on rather than silently returning rows
        // a delete file says are gone.
        if table_file.delete_file.is_some() {
            return Err(DataFusionError::Internal(format!(
                "partial file \"{}\" has a live delete file at read snapshot {}, which the \
                 snapshot-filtered read path cannot apply",
                table_file.file.path, self.snapshot_id
            )));
        }

        let file_cfg = self.build_file_read_config(state, &table_file.file).await?;

        let snap_name = file_cfg
            .embedded_snapshot_parquet_name
            .clone()
            .ok_or_else(|| {
                DataFusionError::Internal(format!(
                    "partial file \"{}\" is missing its embedded snapshot-id column",
                    table_file.file.path
                ))
            })?;

        // Same contract as every other rowid path, and as the C++ extension: a
        // file with neither an embedded rowid nor a catalog `row_id_start` has no
        // reconstructable lineage. Gated on the output actually carrying `rowid`,
        // because this function serves two callers: the rowid-projected read and
        // an ordinary time-travel read, which projects the embedded column away
        // and needs no lineage at all. A merged partial file always carries the
        // embedded column, so this is a guard against a foreign catalog rather
        // than a live case — but without it the rowid caller's failure is a
        // confusing "no field named rowid" from the rename layer instead of a
        // statement of what is actually missing.
        if output_schema.field_with_name(ROWID_COLUMN_NAME).is_ok()
            && file_cfg.embedded_rowid_parquet_name.is_none()
            && table_file.row_id_start.is_none()
        {
            return Err(DataFusionError::Execution(format!(
                "File \"{}\" has no embedded `_ducklake_internal_row_id` column and no \
                 `row_id_start` set in the catalog — row lineage cannot be reconstructed",
                table_file.file.path
            )));
        }

        // Append the embedded snapshot-id column to the file's read schema so the
        // scan materializes it. It is deliberately absent from the cached
        // `read_schema` (that would shift the embedded-rowid column off the end,
        // which other read paths rely on), so we append it here.
        let mut fields: Vec<Arc<Field>> = file_cfg.read_schema.fields().iter().cloned().collect();
        fields.push(Arc::new(Field::new(&snap_name, DataType::Int64, true)));
        let read_schema = Arc::new(Schema::new(fields));
        let projection: Vec<usize> = (0..read_schema.fields().len()).collect();

        let deleted_positions = self
            .deleted_positions_for_file(state, table_file, inlined_positions)
            .await?;
        let scan: Arc<dyn ExecutionPlan> = if deleted_positions.is_empty() {
            let resolved_path = self.resolve_file_path(&table_file.file)?;
            let mut pf = PartitionedFile::new(
                &resolved_path,
                validated_file_size(table_file.file.file_size_bytes, &resolved_path)?,
            );
            if let Some(footer_size) = table_file.file.footer_size
                && footer_size > 0
                && let Ok(hint) = usize::try_from(footer_size)
            {
                pf = pf.with_metadata_size_hint(hint);
            }
            let builder = self
                .scan_config_builder(Arc::new(
                    self.create_parquet_source(state, read_schema.clone())?,
                ))
                .with_file_group(FileGroup::new(vec![pf]))
                .with_projection_indices(Some(projection))?;
            DataSourceExec::from_data_source(builder.build())
        } else {
            // Positional: the reader appends the physical row position, which the
            // delete filter matches the delete set against.
            let (table_schema, pos_table_idx, _pos_name) =
                positional_table_schema(read_schema.clone());
            let mut proj = projection;
            proj.push(pos_table_idx);
            let pos_index = proj.len() - 1;
            let builder = self
                .scan_config_builder(Arc::new(self.create_parquet_source(state, table_schema)?))
                .with_file_group(FileGroup::new(vec![
                    self.partitioned_file(&table_file.file)?,
                ]))
                .with_projection_indices(Some(proj))?;
            let scan = DataSourceExec::from_data_source(builder.build());
            Arc::new(DeleteFilterExec::try_new(
                scan,
                table_file.file.path.clone(),
                Arc::new(deleted_positions),
                pos_index,
            )?)
        };

        // No `NanPruningBarrierExec` here: `SnapshotFilterExec` implements neither
        // `gather_filters_for_pushdown` nor `try_pushdown_sort`, so DataFusion's
        // defaults bar every parent filter and every sort, and nothing can reach
        // the reader's float pruning. Give `SnapshotFilterExec` either one and
        // this path needs the barrier — see `build_exec_for_file_with_deletes`
        // for the shape. Sort pushdown needs it for the same reason filters do:
        // it reorders files and row groups from the same NaN-blind statistics
        // (see `NanPruningBarrierExec::try_pushdown_sort`).
        //
        // Drop rows newer than the read snapshot, then present the catalog schema.
        let filtered: Arc<dyn ExecutionPlan> = Arc::new(SnapshotFilterExec::try_new(
            scan,
            snap_name,
            self.snapshot_id,
        )?);
        Ok(Arc::new(ColumnRenameExec::new_with_constants(
            filtered,
            output_schema,
            file_cfg.name_mapping.clone(),
            file_cfg.constants.clone(),
        )))
    }

    /// A read-only clone of this table (no writer, no rowid projection, fresh
    /// per-file read-config cache) carrying exactly the metadata a scan needs.
    /// [`DuckLakeUpdateExec`] holds one so it can drive the per-file update
    /// scans ([`Self::compute_file_update`]) at execute time — `update()` only
    /// has `&self`, so it cannot hand the exec an `Arc<Self>` directly.
    #[cfg(feature = "write")]
    fn read_only_clone(&self) -> DuckLakeTable {
        DuckLakeTable {
            table_id: self.table_id,
            table_name: self.table_name.clone(),
            provider: Arc::clone(&self.provider),
            snapshot_id: self.snapshot_id,
            object_store_url: self.object_store_url.clone(),
            table_path: self.table_path.clone(),
            schema: self.physical_schema.clone(),
            physical_schema: self.physical_schema.clone(),
            row_lineage: false,
            columns: self.columns.clone(),
            column_defaults: self.column_defaults.clone(),
            fields: self.fields.clone(),
            name_mapping_cache: Arc::clone(&self.name_mapping_cache),
            table_statistics: self.table_statistics.clone(),
            partition_spec: self.partition_spec.clone(),
            // `snapshot_id`/cache match the post-#163 struct (Arc-wrapped cache,
            // pinned snapshot). A read-only clone starts with an empty cache.
            file_read_config_cache: Arc::new(std::sync::Mutex::new(HashMap::new())),
            inlined_materialized_row_count: Arc::clone(&self.inlined_materialized_row_count),
            #[cfg(feature = "encryption")]
            encryption_keys: Arc::clone(&self.encryption_keys),
            schema_name: None,
            writer: None,
            write_options: self.write_options.clone(),
        }
    }

    /// Physical (data-column) schema this table reads/writes, without the
    /// synthetic `rowid`. Used by [`DuckLakeUpdateExec`] to author the rewritten
    /// data file.
    #[cfg(feature = "write")]
    pub(crate) fn physical_schema(&self) -> SchemaRef {
        self.physical_schema.clone()
    }

    /// The metadata writer, when this table was opened writable
    /// (`DuckLakeCatalog::with_writer`). Used by the compaction ops.
    #[cfg(feature = "write")]
    pub(crate) fn writer(&self) -> Option<&Arc<dyn MetadataWriter>> {
        self.writer.as_ref()
    }

    /// Resolved catalog settings with explicit Rust overrides applied. Used by
    /// maintenance writes so their Parquet layout matches ordinary inserts.
    #[cfg(feature = "write")]
    pub(crate) fn write_options(&self) -> &crate::table_writer::DuckLakeWriteOptions {
        &self.write_options
    }

    /// The schema name, when this table was opened writable. Used by the
    /// compaction ops to author output file paths.
    #[cfg(feature = "write")]
    pub(crate) fn schema_name(&self) -> Option<&str> {
        self.schema_name.as_deref()
    }

    /// This table's name. Used by the compaction ops for output file paths.
    #[cfg(feature = "write")]
    pub(crate) fn table_name(&self) -> &str {
        &self.table_name
    }

    /// This table's catalog `table_id`. Used by the compaction commit.
    #[cfg(feature = "write")]
    pub(crate) fn table_id(&self) -> i64 {
        self.table_id
    }

    /// The snapshot this table was opened at — the base the compaction sources
    /// are read against, threaded to the commit as its conflict base.
    #[cfg(feature = "write")]
    pub(crate) fn base_snapshot(&self) -> i64 {
        self.snapshot_id
    }

    /// The object store URL for resolving this table's file paths.
    #[cfg(feature = "write")]
    pub(crate) fn object_store_url(&self) -> &Arc<ObjectStoreUrl> {
        &self.object_store_url
    }

    /// The table's live sort spec at the current catalog head, if any. The write
    /// and compaction paths use it to order rows before writing (tightening
    /// per-file min/max). Read at the head, not the pinned read snapshot, so a
    /// `SET SORTED BY` applied after this provider was opened is honored.
    #[cfg(feature = "write")]
    pub(crate) fn live_sort_spec(&self) -> crate::Result<Option<crate::sort::SortSpec>> {
        let head = self.provider.get_current_snapshot()?;
        self.provider.get_sort_spec(self.table_id, head)
    }

    /// The table's partition spec live at the CURRENT catalog head (not the
    /// snapshot this provider is pinned to) — the generation any write committing
    /// now must agree with. Mirrors [`Self::live_sort_spec`]; the pinned
    /// `self.partition_spec` remains the one used for read pruning, which is
    /// snapshot-bound.
    #[cfg(feature = "write")]
    pub(crate) fn live_partition_spec(&self) -> crate::Result<Option<PartitionSpec>> {
        let head = self.provider.get_current_snapshot()?;
        self.provider.get_partition_spec(self.table_id, head)
    }

    /// The live columns' catalog `column_id`s in `column_order` — the parquet
    /// field-ids a compaction output must bake in so its data columns map back
    /// to the catalog on read.
    #[cfg(feature = "write")]
    pub(crate) fn column_ids(&self) -> Vec<i64> {
        let mut ids = Vec::new();
        for column in &self.columns {
            ids.push(column.column_id);
            ids.extend_from_slice(&column.nested_column_ids);
        }
        ids
    }

    #[cfg(feature = "write")]
    pub(crate) fn top_level_column_ids(&self) -> Vec<i64> {
        self.columns.iter().map(|column| column.column_id).collect()
    }

    /// What `merge_adjacent_files` needs from one candidate's PARQUET FOOTER.
    /// Both facts come from the same footer read (memoized in the per-file
    /// read-config cache), so asking for them together costs one lookup.
    #[cfg(feature = "write")]
    pub(crate) async fn merge_source_facts(
        &self,
        state: &dyn Session,
        file: &DuckLakeFileData,
    ) -> DataFusionResult<MergeSourceFacts> {
        let cfg = self.build_file_read_config(state, file).await?;
        Ok(MergeSourceFacts {
            drops_current_columns: cfg.drops_current_columns,
            has_embedded_snapshot: cfg.embedded_snapshot_parquet_name.is_some(),
        })
    }

    /// Build the positional read plan (and the metadata needed to interpret it)
    /// for one source file of an `UPDATE`. Runs at PLAN time: it reads the
    /// parquet footer (field-ids, row-group layout) and the file's live delete
    /// positions — the same plan-time reads `scan()` performs — but executes NO
    /// data scan and mutates nothing. The returned [`UpdateSourceScan::scan`]
    /// yields the physical data columns (logical order), the embedded rowid
    /// column when the file has one, and the internal physical-position column;
    /// [`Self::apply_update_to_batches`] turns its collected batches into the
    /// rewritten rows at execute time.
    ///
    /// Errors if the file has neither an embedded `_ducklake_internal_row_id`
    /// column nor a catalog `row_id_start`: its lineage cannot be reconstructed,
    /// so rewriting it would fabricate rowids.
    #[cfg(feature = "write")]
    pub(crate) async fn build_update_scan(
        &self,
        state: &dyn Session,
        table_file: &DuckLakeTableFile,
        inlined_positions: Option<&HashSet<i64>>,
    ) -> DataFusionResult<UpdateSourceScan> {
        self.build_update_scan_with_snapshot(state, table_file, false, inlined_positions)
            .await
    }

    /// [`Self::build_update_scan`], additionally projecting the file's embedded
    /// per-row `_ducklake_internal_snapshot_id` column when `want_snapshot_id`.
    ///
    /// Only a merge re-reading a source that PHYSICALLY carries that column sets
    /// it: such a source's rows each hold their own origin snapshot, so the
    /// merged output must copy the column through rather than stamp one origin
    /// on every row. Errors when the file carries no such column — the caller
    /// decides from the footer, so asking for a column that is not there is a
    /// bug, and inventing origins would silently re-attribute history.
    ///
    /// The column is appended to a LOCAL read schema rather than to the cached
    /// [`FileReadConfig::read_schema`], which ends with the embedded rowid that
    /// the other read paths index from the end.
    #[cfg(feature = "write")]
    pub(crate) async fn build_update_scan_with_snapshot(
        &self,
        state: &dyn Session,
        table_file: &DuckLakeTableFile,
        want_snapshot_id: bool,
        inlined_positions: Option<&HashSet<i64>>,
    ) -> DataFusionResult<UpdateSourceScan> {
        let file_cfg = self.build_file_read_config(state, &table_file.file).await?;
        let has_embedded = file_cfg.embedded_rowid_parquet_name.is_some();

        if !has_embedded && table_file.row_id_start.is_none() {
            return Err(DataFusionError::Execution(format!(
                "File \"{}\" has no embedded `_ducklake_internal_row_id` column and no \
                 `row_id_start` in the catalog — cannot preserve row lineage through UPDATE",
                table_file.file.path
            )));
        }

        // Both delete sources mask the scan, but only prior Parquet positions are
        // carried into the replacement Parquet delete file. Inlined positions
        // remain owned by their metadata table until an explicit flush.
        let existing_parquet_deleted = match &table_file.delete_file {
            Some(delete_file) => self.read_delete_file_positions(state, delete_file).await?,
            None => HashSet::new(),
        };
        let mut deleted_positions = existing_parquet_deleted.clone();
        if let Some(inlined) = inlined_positions {
            deleted_positions.extend(inlined);
        }

        // The scan schema is the cached per-file one, plus the embedded
        // snapshot-id column when this caller asked for it. Appending here (and
        // not in `build_file_read_config`) keeps the cached schema's invariant
        // that the embedded rowid, when present, is its LAST field.
        let read_schema = if want_snapshot_id {
            let snap_name = file_cfg
                .embedded_snapshot_parquet_name
                .clone()
                .ok_or_else(|| {
                    DataFusionError::Internal(format!(
                        "File \"{}\" carries no embedded `_ducklake_internal_snapshot_id` \
                         column, so its rows' origin snapshots cannot be read",
                        table_file.file.path
                    ))
                })?;
            let mut fields: Vec<Arc<Field>> =
                file_cfg.read_schema.fields().iter().cloned().collect();
            fields.push(Arc::new(Field::new(&snap_name, DataType::Int64, true)));
            Arc::new(Schema::new(fields))
        } else {
            Arc::clone(&file_cfg.read_schema)
        };

        // Positional scan: the reader produces the physical position. Project the
        // physical columns (logical order), then for an embedded file the
        // embedded rowid column, then the position column.
        let physical_len = self.physical_schema.fields().len();
        let (table_schema, pos_table_idx, _pos_name) = positional_table_schema_reserving(
            read_schema.clone(),
            self.physical_schema
                .fields()
                .iter()
                .map(|field| field.name().as_str()),
        );
        // Built by pushing, so each appended column records the index it actually
        // landed at. Arithmetic off `physical_len` would silently misalign the
        // appended columns for a file that has one but not another, and all are
        // Int64 — nothing downstream would fail to downcast.
        let mut proj: Vec<usize> = (0..physical_len).collect();
        let embedded_batch_idx = has_embedded.then(|| {
            proj.push(file_cfg.read_schema.fields().len() - 1);
            proj.len() - 1
        });
        let embedded_snapshot_batch_idx = want_snapshot_id.then(|| {
            proj.push(read_schema.fields().len() - 1);
            proj.len() - 1
        });
        proj.push(pos_table_idx);
        let pos_index = proj.len() - 1;

        let scan: Arc<dyn ExecutionPlan> = DataSourceExec::from_data_source(
            self.scan_config_builder(Arc::new(self.create_parquet_source(state, table_schema)?))
                .with_file_group(FileGroup::new(vec![
                    self.partitioned_file(&table_file.file)?,
                ]))
                .with_projection_indices(Some(proj))?
                .build(),
        );
        let mut plan = self.split_across_partitions(scan, state, &file_cfg)?;
        // Mask with BOTH delete sources (#262): inlined positions and prior
        // Parquet ones. Only `existing_parquet_deleted` is carried forward into
        // the replacement delete file.
        if !deleted_positions.is_empty() {
            plan = Arc::new(DeleteFilterExec::try_new(
                plan,
                table_file.file.path.clone(),
                Arc::new(deleted_positions),
                pos_index,
            )?);
        }

        plan = self.present_file_read_config(plan, &file_cfg, physical_len);

        Ok(UpdateSourceScan {
            scan: plan,
            physical_len,
            embedded_batch_idx,
            embedded_snapshot_batch_idx,
            pos_index,
            row_id_start: table_file.row_id_start,
            existing_parquet_deleted,
            data_file_id: table_file.data_file_id,
            delete_file_id: table_file.delete_file_id,
            source_path: table_file.file.path.clone(),
        })
    }

    /// Turn the batches collected from an [`UpdateSourceScan`] into the rewritten
    /// row versions for one source file: select the rows matching `predicate`
    /// (or every live row when it is `None`), apply `assignments`, and RETAIN
    /// each row's original rowid so lineage survives the rewrite. Pure and
    /// synchronous — the exec runs it at execute time after `collect`ing the
    /// scan, so no [`Session`] is required.
    ///
    /// `assignments` are `(physical_column_index, new_value_expr)`; unlisted
    /// columns carry through unchanged. Returned batches are
    /// `[physical columns (catalog types)..., rowid]`, ready for
    /// [`DuckLakeTableWriter::begin_write_with_embedded_rowid`](crate::table_writer::DuckLakeTableWriter::begin_write_with_embedded_rowid).
    /// The original rowid is the embedded column when the file has one, else
    /// `row_id_start + physical_position`.
    ///
    /// The per-batch work lives in [`rewrite_scanned_batch`], which compaction
    /// applies inside its execution plan rather than after a `collect`, so both
    /// paths produce byte-identical rows.
    #[cfg(feature = "write")]
    pub(crate) fn apply_update_to_batches(
        &self,
        scan: &UpdateSourceScan,
        batches: &[RecordBatch],
        predicate: Option<&Arc<dyn PhysicalExpr>>,
        assignments: &[(usize, Arc<dyn PhysicalExpr>)],
    ) -> DataFusionResult<FileUpdateOutput> {
        let out_schema = rewrite_output_schema(&self.physical_schema);

        let mut updated_batches: Vec<RecordBatch> = Vec::new();
        let mut new_positions: Vec<i64> = Vec::new();

        for batch in batches {
            let Some(rewritten) = rewrite_scanned_batch(
                &self.physical_schema,
                &out_schema,
                scan,
                batch,
                predicate,
                assignments,
            )?
            else {
                continue;
            };
            updated_batches.push(rewritten.batch);
            new_positions.extend(rewritten.positions);
        }

        let matched_count = new_positions.len();
        let mut cumulative = scan.existing_parquet_deleted.clone();
        cumulative.extend(new_positions);
        let mut cumulative_positions: Vec<i64> = cumulative.into_iter().collect();
        cumulative_positions.sort_unstable();

        Ok(FileUpdateOutput {
            updated_batches,
            matched_count,
            cumulative_positions,
        })
    }
}

/// What one merge candidate's parquet footer says about it, from
/// [`DuckLakeTable::merge_source_facts`].
#[cfg(feature = "write")]
#[derive(Clone, Copy)]
pub(crate) struct MergeSourceFacts {
    /// The file carries a data column that is no longer in the table's current
    /// schema (dropped since it was written). Merged output is written at the
    /// current schema, so merging such a file would lose that column's data —
    /// and remove the file that still holds it.
    pub(crate) drops_current_columns: bool,
    /// The file physically embeds the per-row `_ducklake_internal_snapshot_id`
    /// column, so its rows' origin snapshots are read from it rather than taken
    /// as the file's single catalog `begin_snapshot`. This is the ONLY thing
    /// that decides where a merge source's origins come from — the catalog's
    /// `partial_max` can be NULL on a file that carries the column (providers
    /// that do not read the field, catalogs predating it), and keying off it
    /// would re-stamp every row with one origin and drop the column.
    pub(crate) has_embedded_snapshot: bool,
}

/// Output schema of a rewritten source file: the table's physical columns (in
/// catalog types) followed by the preserved `rowid`. `UPDATE` and compaction
/// share it so both emit exactly the same shape.
#[cfg(feature = "write")]
pub(crate) fn rewrite_output_schema(physical_schema: &SchemaRef) -> SchemaRef {
    let mut fields: Vec<Arc<Field>> = physical_schema.fields().iter().cloned().collect();
    fields.push(Arc::new(rowid_field()));
    Arc::new(Schema::new(fields))
}

/// One batch of [`DuckLakeTable::apply_update_to_batches`] output.
#[cfg(feature = "write")]
pub(crate) struct RewrittenBatch {
    /// `[physical columns (catalog types)..., rowid]` for the matched rows.
    pub(crate) batch: RecordBatch,
    /// Physical positions of the matched rows within the source file, in batch
    /// order — the positions the source file's new cumulative delete must mask.
    pub(crate) positions: Vec<i64>,
    /// The matched rows' embedded per-row origin snapshots, present exactly when
    /// the scan projected them ([`UpdateSourceScan::embedded_snapshot_batch_idx`]).
    /// Filtered with the same mask as the data columns, so each value still
    /// travels with its own row.
    pub(crate) origin_snapshots: Option<ArrayRef>,
}

/// Rewrite ONE batch read from an [`UpdateSourceScan`] into its
/// `[physical columns..., rowid]` form, retaining each row's original rowid.
///
/// `Ok(None)` means the batch contributed nothing (it was empty, or `predicate`
/// matched no row); the caller skips it. `out_schema` must be
/// [`rewrite_output_schema`] of `physical_schema` — it is passed in so a
/// streaming caller builds it once rather than per batch.
///
/// Row-order independent: every value it reads (the physical columns, the
/// embedded rowid, the physical-position column) travels with its own row, so
/// the result is the same however the scan is partitioned or merged.
#[cfg(feature = "write")]
pub(crate) fn rewrite_scanned_batch(
    physical_schema: &SchemaRef,
    out_schema: &SchemaRef,
    scan: &UpdateSourceScan,
    batch: &RecordBatch,
    predicate: Option<&Arc<dyn PhysicalExpr>>,
    assignments: &[(usize, Arc<dyn PhysicalExpr>)],
) -> DataFusionResult<Option<RewrittenBatch>> {
    let physical_len = scan.physical_len;
    let n = batch.num_rows();
    if n == 0 {
        return Ok(None);
    }

    // Coerce physical columns to the catalog types the assignment /
    // predicate exprs (and the writer) expect.
    let mut phys_cols: Vec<ArrayRef> = Vec::with_capacity(physical_len);
    for i in 0..physical_len {
        phys_cols.push(crate::column_rename::coerce_column(
            batch.column(i),
            physical_schema.field(i).data_type(),
        )?);
    }
    let phys_batch = RecordBatch::try_new(Arc::clone(physical_schema), phys_cols.clone())?;

    let row_pos = batch
        .column(scan.pos_index)
        .as_any()
        .downcast_ref::<Int64Array>()
        .ok_or_else(|| {
            DataFusionError::Internal("physical-position column is not Int64".to_string())
        })?;

    // Predicate mask (all rows when there is no WHERE). A NULL predicate
    // result is a non-match (SQL semantics).
    let mask: BooleanArray = match predicate {
        Some(p) => {
            let arr = p.evaluate(&phys_batch)?.into_array(n)?;
            let b = arr.as_any().downcast_ref::<BooleanArray>().ok_or_else(|| {
                DataFusionError::Execution(
                    "UPDATE predicate did not evaluate to a boolean".to_string(),
                )
            })?;
            BooleanArray::from(
                (0..n)
                    .map(|i| b.is_valid(i) && b.value(i))
                    .collect::<Vec<bool>>(),
            )
        },
        None => BooleanArray::from(vec![true; n]),
    };
    if mask.true_count() == 0 {
        return Ok(None);
    }

    // Keep only matched rows, then apply the assignments to them.
    let matched_phys: Vec<ArrayRef> = phys_cols
        .iter()
        .enumerate()
        .map(|(i, column)| {
            let filtered = arrow::compute::filter(column.as_ref(), &mask)
                .map_err(|e| DataFusionError::ArrowError(Box::new(e), None))?;
            crate::column_rename::coerce_column(&filtered, physical_schema.field(i).data_type())
        })
        .collect::<DataFusionResult<_>>()?;
    let matched_batch = RecordBatch::try_new(Arc::clone(physical_schema), matched_phys.clone())?;
    let matched_rows = matched_batch.num_rows();

    let mut out_cols = matched_phys;
    for (col_idx, expr) in assignments {
        let val = expr.evaluate(&matched_batch)?.into_array(matched_rows)?;
        out_cols[*col_idx] =
            crate::column_rename::coerce_column(&val, physical_schema.field(*col_idx).data_type())?;
    }

    // Original rowids: embedded column when present, else synthesized.
    let matched_pos = arrow::compute::filter(row_pos, &mask)
        .map_err(|e| DataFusionError::ArrowError(Box::new(e), None))?;
    let matched_pos = matched_pos
        .as_any()
        .downcast_ref::<Int64Array>()
        .expect("filtered Int64Array");
    let rowid_col: ArrayRef = if let Some(idx) = scan.embedded_batch_idx {
        let embedded = batch
            .column(idx)
            .as_any()
            .downcast_ref::<Int64Array>()
            .ok_or_else(|| {
                DataFusionError::Internal("embedded rowid column is not Int64".to_string())
            })?;
        let embedded: ArrayRef = Arc::new(embedded.clone());
        arrow::compute::filter(embedded.as_ref(), &mask)
            .map_err(|e| DataFusionError::ArrowError(Box::new(e), None))?
    } else {
        let start = scan
            .row_id_start
            .expect("row_id_start checked in build_update_scan");
        Arc::new(Int64Array::from(
            matched_pos
                .values()
                .iter()
                .map(|p| start + p)
                .collect::<Vec<i64>>(),
        ))
    };
    out_cols.push(rowid_col);

    // Carried through under the SAME mask as the data columns: taking it from
    // the unfiltered batch would hand each surviving row a different row's
    // origin whenever a predicate selected a subset.
    let origin_snapshots = match scan.embedded_snapshot_batch_idx {
        Some(idx) => Some(
            arrow::compute::filter(batch.column(idx).as_ref(), &mask)
                .map_err(|e| DataFusionError::ArrowError(Box::new(e), None))?,
        ),
        None => None,
    };

    Ok(Some(RewrittenBatch {
        batch: RecordBatch::try_new(Arc::clone(out_schema), out_cols)?,
        positions: matched_pos.values().to_vec(),
        origin_snapshots,
    }))
}

/// Per-source-file read plan + metadata for an `UPDATE`, produced by
/// [`DuckLakeTable::build_update_scan`] at plan time and consumed by
/// [`DuckLakeUpdateExec`] at execute time.
#[cfg(feature = "write")]
#[derive(Clone, Debug)]
pub(crate) struct UpdateSourceScan {
    /// Positional read plan yielding `[physical columns..., (embedded rowid),
    /// __ducklake_row_pos]` for the source file, already masking rows removed by
    /// its live delete file.
    pub(crate) scan: Arc<dyn ExecutionPlan>,
    /// Number of physical (data) columns at the front of each scanned batch.
    pub(crate) physical_len: usize,
    /// Column index of the embedded rowid in each scanned batch, or `None` when
    /// the file has no embedded rowid (rowids are synthesized from
    /// `row_id_start + position`).
    pub(crate) embedded_batch_idx: Option<usize>,
    /// Column index of the embedded per-row `_ducklake_internal_snapshot_id` in
    /// each scanned batch, or `None` when the scan did not project it. Set only
    /// by [`DuckLakeTable::build_update_scan_with_snapshot`], for a merge source
    /// whose per-row origins must survive into the merged output.
    pub(crate) embedded_snapshot_batch_idx: Option<usize>,
    /// Column index of the internal physical-position column in each batch.
    pub(crate) pos_index: usize,
    /// The source file's catalog `row_id_start` (used to synthesize rowids for a
    /// non-embedded file).
    pub(crate) row_id_start: Option<i64>,
    /// Positions already masked by the live Parquet delete file, carried into
    /// the replacement Parquet delete file.
    pub(crate) existing_parquet_deleted: HashSet<i64>,
    /// Catalog id of the source data file (the positional delete's target).
    pub(crate) data_file_id: i64,
    /// Catalog id of the file's currently-live delete file (compare-and-swap
    /// guard when superseding it), or `None`.
    pub(crate) delete_file_id: Option<i64>,
    /// The source data file's catalog path (records the delete's provenance).
    pub(crate) source_path: String,
}

/// The rewrite produced for one source data file by
/// [`DuckLakeTable::apply_update_to_batches`].
#[cfg(feature = "write")]
pub(crate) struct FileUpdateOutput {
    /// Rewritten row versions, `[physical columns..., rowid]`, carrying each
    /// row's original rowid. Empty when no rows matched.
    pub(crate) updated_batches: Vec<RecordBatch>,
    /// Number of rows this update rewrote in the source file.
    pub(crate) matched_count: usize,
    /// Physical positions to mask on the source file afterwards: the rows this
    /// update supersedes unioned with any already-deleted rows (sorted).
    pub(crate) cumulative_positions: Vec<i64>,
}

#[async_trait]
impl TableProvider for DuckLakeTable {
    fn schema(&self) -> SchemaRef {
        Arc::clone(&self.schema)
    }

    fn table_type(&self) -> TableType {
        TableType::Base
    }

    fn get_column_default(&self, column: &str) -> Option<&Expr> {
        self.column_defaults.get(column)
    }

    fn statistics(&self) -> Option<Statistics> {
        let mut statistics = self.table_statistics.clone();
        if self.row_lineage {
            statistics
                .column_statistics
                .push(ColumnStatistics::new_unknown());
        }
        Some(statistics)
    }

    fn supports_filters_pushdown(
        &self,
        filters: &[&Expr],
    ) -> DataFusionResult<Vec<TableProviderFilterPushDown>> {
        // Inexact here is ALSO what keeps a filtered `count(*)` correct: it is the
        // retained `FilterExec` that stops `AggregateStatistics` folding the scan's
        // published (post-prune, pre-filter) row count into the answer. That rule
        // runs before `FilterPushdown`, so nothing downstream would catch it.
        // Before declaring any filter class Exact, read `roll_up_scan_statistics`.
        //
        // Mark all filters as Inexact because we apply delete filters after the scan.
        // DataFusion will reapply these filters after DeleteFilterExec to ensure
        // correctness, but Parquet can still use them for:
        // - Row group pruning via statistics
        // - Page-level filtering with late materialization
        // - Bloom filter lookups (if available)
        Ok(filters
            .iter()
            .map(|_| TableProviderFilterPushDown::Inexact)
            .collect())
    }

    async fn scan(
        &self,
        state: &dyn Session,
        projection: Option<&Vec<usize>>,
        // Filters drive plan-time file pruning below: iterative conjunct pruning drops
        // files whose catalog statistics prove they cannot match. They are also
        // pushed down to the parquet scanner by DataFusion's optimizer for row
        // group / page-level filtering. We declare them Inexact in
        // `supports_filters_pushdown`, so DataFusion reapplies them after our
        // scan — pruning here only ever removes provably non-matching files.
        filters: &[Expr],
        limit: Option<usize>,
    ) -> DataFusionResult<Arc<dyn ExecutionPlan>> {
        let inlined_filter = translate_inlined_filters(filters);

        // Row-lineage detour: when the synthetic `rowid` column is projected,
        // every file needs its own scan because each has a distinct
        // `row_id_start`. `projection == None` with row lineage on means "all
        // columns including rowid", which also routes through this path.
        let rowid_idx = self.rowid_index();
        let rowid_in_proj = match (rowid_idx, projection) {
            (Some(r), Some(p)) => p.contains(&r),
            (Some(_), None) => true,
            (None, _) => false,
        };

        // The rowid plan below unions per-file scans only, so visible inlined
        // rows would silently vanish from a row-lineage read. Refuse loudly
        // instead until inlined rowid scans are supported.
        if rowid_in_proj {
            let inlined = self.provider.scan_inlined_data(
                self.table_id,
                self.snapshot_id,
                &self.columns,
                inlined_filter.as_ref(),
            )?;
            self.inlined_materialized_row_count
                .store(inlined.materialized_row_count, Ordering::Relaxed);
            if inlined.batches.iter().any(|batch| batch.num_rows() > 0) {
                return Err(crate::DuckLakeError::Unsupported(format!(
                    "row-lineage (rowid) scan on a table with inlined rows is not supported; \
                     {INLINED_DATA_REMEDIATION}"
                ))
                .into());
            }
        }

        let mut execs: Vec<Arc<dyn ExecutionPlan>> = Vec::new();
        let inlined_deletes = self.inlined_deletes_by_file()?;
        // One physical form of the filters feeds both pruning paths; see
        // `physical_conjuncts`. A failure to convert them means "prune
        // nothing", never an error — the filters are re-applied above this scan
        // regardless (`supports_filters_pushdown` declares them Inexact).
        let conjuncts = match self.physical_conjuncts(state, filters) {
            Ok(conjuncts) => conjuncts,
            Err(error) => {
                tracing::debug!(%error, "skipping plan-time file pruning");
                Vec::new()
            },
        };
        // Catalog-side pre-filter: the provider may leave files unlisted whose
        // statistics prove they cannot match, which is what stops planning cost
        // scaling with the table. Purely additive — `prune_table_files_iteratively`
        // still runs on every file that comes back, and a predicate that will
        // not lower simply yields no filter.
        let stats_filter = datafusion::physical_expr::conjunction_opt(conjuncts.iter().cloned())
            .and_then(|predicate| {
                stats_filter::lower_predicate(&predicate, &self.physical_schema, &self.columns)
            });
        let pruning = match self.pruning_predicates(conjuncts) {
            Ok(pruning) => pruning,
            Err(error) => {
                tracing::debug!(%error, "skipping plan-time file pruning");
                Vec::new()
            },
        };
        for metadata in self.file_metadata_pages("planning", stats_filter.as_ref()) {
            let (table_files, file_statistics, summary_statistics) =
                self.page_files_with_both_statistics(metadata?);
            #[cfg(feature = "encryption")]
            self.configure_encryption_factory(&table_files)?;

            let table_files =
                self.prune_table_files_iteratively(&pruning, &table_files, &file_statistics);

            if rowid_in_proj {
                let rowid_idx = rowid_idx.unwrap();
                let user_proj: Vec<usize> = projection
                    .cloned()
                    .unwrap_or_else(|| (0..self.schema.fields().len()).collect());
                for table_file in table_files {
                    let exec = if self.needs_snapshot_filter(table_file) {
                        let output_schema =
                            self.output_schema_for_projection(&user_proj, rowid_idx);
                        self.build_exec_for_partial_file(
                            state,
                            table_file,
                            inlined_deletes.get(&table_file.data_file_id),
                            output_schema,
                        )
                        .await?
                    } else {
                        self.build_exec_for_file_with_rowid(
                            state,
                            table_file,
                            inlined_deletes.get(&table_file.data_file_id),
                            &file_statistics,
                            &user_proj,
                            rowid_idx,
                            limit,
                        )
                        .await?
                    };
                    execs.push(exec);
                }
                continue;
            }

            let (needs_filter, rest): (Vec<_>, Vec<_>) = table_files
                .into_iter()
                .partition(|table_file| self.needs_snapshot_filter(table_file));
            let (files_with_deletes, files_without_deletes): (Vec<_>, Vec<_>) =
                rest.into_iter().partition(|table_file| {
                    table_file.delete_file.is_some()
                        || inlined_deletes.contains_key(&table_file.data_file_id)
                });

            if !files_without_deletes.is_empty() {
                execs.push(
                    self.build_exec_for_files_without_deletes(
                        state,
                        &files_without_deletes,
                        &file_statistics,
                        &summary_statistics,
                        projection,
                        limit,
                    )
                    .await?,
                );
            }
            for table_file in files_with_deletes {
                execs.push(
                    self.build_exec_for_file_with_deletes(
                        state,
                        table_file,
                        inlined_deletes.get(&table_file.data_file_id),
                        &file_statistics,
                        projection,
                        limit,
                    )
                    .await?,
                );
            }
            for table_file in needs_filter {
                let output_schema = match projection {
                    Some(indices) => Arc::new(self.schema.project(indices)?),
                    None => self.schema.clone(),
                };
                execs.push(
                    self.build_exec_for_partial_file(
                        state,
                        table_file,
                        inlined_deletes.get(&table_file.data_file_id),
                        output_schema,
                    )
                    .await?,
                );
            }
        }

        if rowid_in_proj {
            if execs.is_empty() {
                use datafusion::physical_plan::empty::EmptyExec;
                let rowid_idx = rowid_idx.unwrap();
                let user_proj: Vec<usize> = projection
                    .cloned()
                    .unwrap_or_else(|| (0..self.schema.fields().len()).collect());
                let schema = self.output_schema_for_projection(&user_proj, rowid_idx);
                return Ok(Arc::new(EmptyExec::new(schema)));
            }
            return combine_execution_plans(execs);
        }

        // Inlined data: rows DuckDB's data-inlining optimization stored directly
        // in the catalog (not in Parquet). Union them in so SELECT / COUNT(*)
        // include them. Providers without inlined data — or that don't implement
        // the read — return empty, so this is a no-op for ordinary catalogs.
        let inlined = self.provider.scan_inlined_data(
            self.table_id,
            self.snapshot_id,
            &self.columns,
            inlined_filter.as_ref(),
        )?;
        self.inlined_materialized_row_count
            .store(inlined.materialized_row_count, Ordering::Relaxed);
        if inlined.batches.iter().any(|b| b.num_rows() > 0) {
            let exec = MemorySourceConfig::try_new_exec(
                &[inlined.batches],
                self.physical_schema.clone(),
                projection.cloned(),
            )?;
            execs.push(exec);
        }

        // Handle empty tables (no data files)
        if execs.is_empty() {
            use datafusion::physical_plan::empty::EmptyExec;
            let projected_schema = match projection {
                Some(indices) => Arc::new(self.schema.project(indices)?),
                None => self.schema.clone(),
            };
            return Ok(Arc::new(EmptyExec::new(projected_schema)));
        }

        // Combine execution plans
        combine_execution_plans(execs)
    }

    #[cfg(feature = "write")]
    async fn insert_into(
        &self,
        _state: &dyn Session,
        input: Arc<dyn ExecutionPlan>,
        insert_op: InsertOp,
    ) -> DataFusionResult<Arc<dyn ExecutionPlan>> {
        let writer = self.writer.as_ref().ok_or_else(|| {
            DataFusionError::Plan(
                "Table is read-only. Use DuckLakeCatalog::with_writer() to enable writes."
                    .to_string(),
            )
        })?;

        let schema_name = self.schema_name.as_ref().ok_or_else(|| {
            DataFusionError::Internal("Schema name not set for writable table".to_string())
        })?;
        self.write_options
            .validate()
            .map_err(|e| DataFusionError::External(Box::new(e)))?;

        let write_mode = match insert_op {
            InsertOp::Append => WriteMode::Append,
            InsertOp::Overwrite | InsertOp::Replace => WriteMode::Replace,
        };

        // Resolve the partition spec at the CURRENT catalog head, NOT the snapshot
        // this table provider was pinned to when it was opened. A write always
        // commits at the head, so it must honor the spec live there; using the
        // pinned `self.partition_spec` would ignore a spec set/reset applied after
        // this provider was created (e.g. `execute_ducklake_sql(SET PARTITIONED BY)`
        // then `INSERT` in the same session) and could stamp a retired partition_id.
        // (`self.partition_spec`, pinned, is still used for read pruning, which is
        // snapshot-bound.)
        //
        // A transform we cannot PRODUCE (bucket/unknown) makes a partitioned INSERT
        // unsupported — reject rather than silently writing unpartitioned files that
        // would violate the spec.
        let head_snapshot = self
            .provider
            .get_current_snapshot()
            .map_err(|e| DataFusionError::External(Box::new(e)))?;
        let live_spec = self
            .provider
            .get_partition_spec(self.table_id, head_snapshot)
            .map_err(|e| DataFusionError::External(Box::new(e)))?;
        let partition = match live_spec.as_ref() {
            None => None,
            Some(spec) => {
                let column_ids: Vec<i64> = self.columns.iter().map(|c| c.column_id).collect();
                Some(
                    crate::partition::PartitionWriteSpec::resolve(
                        spec,
                        &column_ids,
                        self.physical_schema.as_ref(),
                    )
                    .map_err(|e| DataFusionError::External(Box::new(e)))?,
                )
            },
        };

        // Resolve the live sort spec (also at the head, for the same reason as the
        // partition spec). When it is producible — every key is a bare column
        // present in the write schema — wrap the input in a global SortExec so each
        // written file's rows are ordered, tightening per-file min/max statistics
        // for range pruning. Sorting is applied before the partition split, so each
        // per-partition file remains a sorted subsequence. An unsupported expression
        // or missing sort column is rejected before execution rather than silently
        // producing files that violate the active sort contract.
        let ordering = if self.write_options.sort_on_insert.unwrap_or(true) {
            let live_sort = self
                .provider
                .get_sort_spec(self.table_id, head_snapshot)
                .map_err(|e| DataFusionError::External(Box::new(e)))?;
            sort_ordering_for(&input.schema(), live_sort.as_ref())?
        } else {
            None
        };
        // Wrap the input in a SortExec now AND declare the same requirement on
        // DuckLakeInsertExec, so DataFusion's EnforceSorting keeps the ordering
        // (a plain SortExec with no downstream ordering requirement would be
        // optimized away, silently dropping the sort).
        let input = match ordering.clone() {
            Some(ordering) => Arc::new(datafusion::physical_plan::sorts::sort::SortExec::new(
                ordering, input,
            )) as Arc<dyn ExecutionPlan>,
            None => input,
        };

        Ok(Arc::new(DuckLakeInsertExec::new(
            input,
            Arc::clone(writer),
            schema_name.clone(),
            self.table_name.clone(),
            self.schema(),
            write_mode,
            self.object_store_url.clone(),
            partition,
            self.write_options.clone(),
            ordering,
        )))
    }

    /// Plan an `UPDATE t SET col = expr [, ...] [WHERE ...]`.
    ///
    /// `assignments` are `(column_name, new_value_expr)` for each SET (identity
    /// `c = c` assignments are already dropped by the planner). `filters` are the
    /// unqualified, AND-conjunctive WHERE predicates; an empty `filters` updates
    /// every live row. The returned [`DuckLakeUpdateExec`] performs the update at
    /// execute time and yields a single `count: UInt64` row — planning here is
    /// side-effect-free (no scans, no writes), so `EXPLAIN` never mutates data.
    #[cfg(feature = "write")]
    async fn update(
        &self,
        state: &dyn Session,
        assignments: Vec<(String, Expr)>,
        filters: Vec<Expr>,
    ) -> DataFusionResult<Arc<dyn ExecutionPlan>> {
        let writer = self.writer.as_ref().ok_or_else(|| {
            DataFusionError::Plan(
                "Table is read-only. Use DuckLakeCatalog::with_writer() to enable writes."
                    .to_string(),
            )
        })?;
        let schema_name = self.schema_name.as_ref().ok_or_else(|| {
            DataFusionError::Internal("Schema name not set for writable table".to_string())
        })?;
        self.write_options
            .validate()
            .map_err(|e| DataFusionError::External(Box::new(e)))?;

        // DuckDB / MySQL metadata writers do not implement the atomic
        // append-with-deletes commit UPDATE needs. Reject up front rather than
        // rewriting files and only failing at commit.
        if !writer.supports_update() {
            return Err(DataFusionError::NotImplemented(
                "UPDATE not supported on this metadata backend".to_string(),
            ));
        }

        // UPDATE rewrites Parquet-resident rows only; visible inlined rows would
        // be silently skipped. Refuse loudly instead (same detection DELETE uses).
        let inlined = self.provider.get_inlined_data(
            self.table_id,
            self.provider.get_current_snapshot()?,
            &self.columns,
        )?;
        if inlined.iter().any(|batch| batch.num_rows() > 0) {
            return Err(crate::DuckLakeError::Unsupported(format!(
                "UPDATE on a table with inlined rows is not supported; \
                 {INLINED_DATA_REMEDIATION}"
            ))
            .into());
        }

        // Assignment / filter expressions reference the table's DATA columns
        // (unqualified), never the synthetic `rowid`. Plan them against the
        // physical schema so column indices line up with the scanned batches.
        let df_schema = DFSchema::try_from(self.physical_schema.as_ref().clone())
            .map_err(|e| DataFusionError::External(Box::new(e)))?;

        let mut phys_assignments: Vec<(usize, Arc<dyn PhysicalExpr>)> =
            Vec::with_capacity(assignments.len());
        for (col_name, expr) in assignments {
            let idx = self.physical_schema.index_of(&col_name).map_err(|_| {
                DataFusionError::Plan(format!(
                    "UPDATE assignment targets unknown column '{col_name}'"
                ))
            })?;
            let pexpr = state.create_physical_expr(expr, &df_schema)?;
            phys_assignments.push((idx, pexpr));
        }

        // AND the WHERE predicates into one physical expression; empty => update
        // all rows (represented as `None`).
        let mut predicate: Option<Arc<dyn PhysicalExpr>> = None;
        for f in filters {
            let pe = state.create_physical_expr(f, &df_schema)?;
            predicate = Some(match predicate {
                None => pe,
                Some(prev) => Arc::new(BinaryExpr::new(prev, Operator::And, pe)),
            });
        }

        // Build the per-file positional read plans now (plan time). This reads
        // parquet footers + live delete positions — the same plan-time reads
        // `scan()` does — but no data scan and no mutation happen here; the exec
        // collects each scan and performs the rewrite + atomic commit at execute
        // time.
        let table_files = self
            .files()
            .map_err(|error| DataFusionError::External(Box::new(error)))?;
        let inlined_deletes = self.inlined_deletes_by_file()?;
        let mut scans = Vec::with_capacity(table_files.len());
        for tf in &table_files {
            scans.push(
                self.build_update_scan(state, tf, inlined_deletes.get(&tf.data_file_id))
                    .await?,
            );
        }

        Ok(Arc::new(DuckLakeUpdateExec::new(
            Arc::new(self.read_only_clone()),
            Arc::clone(writer),
            schema_name.clone(),
            self.table_name.clone(),
            self.snapshot_id,
            scans,
            phys_assignments,
            predicate,
            self.object_store_url.clone(),
        )))
    }

    /// Plan a `DELETE FROM <table> [WHERE ...]`.
    ///
    /// `filters` are the already-analyzed, unqualified, AND-conjunctive
    /// predicates over this table's own columns (DataFusion strips qualifiers and
    /// dedups them). An empty `filters` means no `WHERE` => delete ALL rows.
    ///
    /// Returns a [`DuckLakeDeleteExec`] that performs the delete when executed
    /// (positional-delete files + one atomic metadata commit, or a metadata-only
    /// truncate for delete-all) and yields a single `count: UInt64` row. All
    /// mutation happens at execute time, so planning (e.g. `EXPLAIN`) is
    /// side-effect free.
    ///
    /// The catalog pins its snapshot at creation, so a session sees one
    /// generation for its lifetime: re-open the catalog between mutating
    /// statements. See the [`delete_exec`](crate::delete_exec) module docs
    /// ("Session lifecycle") for why a second in-session `DELETE` can conflict.
    #[cfg(feature = "write")]
    async fn delete_from(
        &self,
        state: &dyn Session,
        filters: Vec<Expr>,
    ) -> DataFusionResult<Arc<dyn ExecutionPlan>> {
        use datafusion::logical_expr::utils::conjunction;

        let writer = self.writer.as_ref().ok_or_else(|| {
            DataFusionError::Plan(
                "Table is read-only. Use DuckLakeCatalog::with_writer() to enable writes."
                    .to_string(),
            )
        })?;
        let schema_name = self.schema_name.as_ref().ok_or_else(|| {
            DataFusionError::Internal("Schema name not set for writable table".to_string())
        })?;

        self.write_options
            .validate()
            .map_err(|e| DataFusionError::External(Box::new(e)))?;

        // Build the physical predicate. Empty `filters` (no WHERE) => delete ALL,
        // signalled by `None` and handled as a metadata-only truncate. We resolve
        // column references against the PHYSICAL schema (no synthetic `rowid`):
        // `resolve_positions` evaluates the predicate index-based against the
        // physically-read columns in logical order, so the physical expression's
        // column indices must line up with `physical_schema`. A predicate that
        // references a column absent from `physical_schema` (e.g. the synthetic
        // `rowid`) fails here rather than mis-deleting.
        let predicate = match conjunction(filters) {
            None => None,
            Some(expr) => {
                let df_schema =
                    datafusion::common::DFSchema::try_from(self.physical_schema.as_ref().clone())?;
                Some(state.create_physical_expr(expr, &df_schema)?)
            },
        };

        // The delete work (positional reads, delete-file writes, atomic commit)
        // MUST run at execute time — planning a DELETE (e.g. `EXPLAIN`) must not
        // mutate. `DuckLakeDeleteExec` captures the concrete `SessionState` to
        // drive the positional reads at execute time (a bare `TaskContext` cannot
        // build physical exprs / sub-plans), plus a clone of this table for its
        // reader methods.
        let session_state = state
            .as_any()
            .downcast_ref::<datafusion::execution::SessionState>()
            .ok_or_else(|| {
                DataFusionError::NotImplemented(
                    "DELETE on a DuckLake table requires a DataFusion SessionState session"
                        .to_string(),
                )
            })?
            .clone();

        Ok(Arc::new(DuckLakeDeleteExec::new(
            Arc::new(self.clone()),
            session_state,
            predicate,
            Arc::clone(writer),
            schema_name.clone(),
            self.table_name.clone(),
            self.table_id,
            self.snapshot_id,
            self.object_store_url.clone(),
        )))
    }
}

/// Combines multiple execution plans into a single plan
fn combine_execution_plans(
    execs: Vec<Arc<dyn ExecutionPlan>>,
) -> DataFusionResult<Arc<dyn ExecutionPlan>> {
    if execs.len() == 1 {
        Ok(execs.into_iter().next().unwrap())
    } else {
        use datafusion::physical_plan::union::UnionExec;
        UnionExec::try_new(execs)
    }
}

/// Extract deleted row positions from a delete file RecordBatch
///
/// Delete files have schema: (file_path: VARCHAR, pos: INT64)
/// We only extract the "pos" column - the "file_path" column is metadata/documentation
/// only (for Iceberg compatibility). The metadata catalog already tells us which delete
/// file is associated with which data file.
fn extract_deleted_positions_from_batch(
    batch: &RecordBatch,
    positions: &mut HashSet<i64>,
) -> DataFusionResult<()> {
    // Get the pos column index by name (not magic number)
    let schema = batch.schema();
    let pos_idx = schema.index_of(DELETE_POS_COL)?;

    // Get the pos column
    let pos_array = batch
        .column(pos_idx)
        .as_any()
        .downcast_ref::<Int64Array>()
        .ok_or_else(|| {
            DataFusionError::Internal(format!("{} column not found or wrong type", DELETE_POS_COL))
        })?;

    // Extract all non-null positions
    for i in 0..batch.num_rows() {
        if !pos_array.is_null(i) {
            positions.insert(pos_array.value(i));
        }
    }

    Ok(())
}

/// Check if a DataFusion error is caused by an object store NotFound error.
fn is_object_store_not_found(err: &DataFusionError) -> bool {
    if let DataFusionError::ObjectStore(os_err) = err {
        return matches!(&**os_err, object_store::Error::NotFound { .. });
    }
    let mut source = std::error::Error::source(err);
    while let Some(e) = source {
        if let Some(os_err) = e.downcast_ref::<object_store::Error>() {
            return matches!(os_err, object_store::Error::NotFound { .. });
        }
        source = e.source();
    }
    false
}

/// The `LexOrdering` for a table's live sort spec against `schema`, or `None` when
/// the table has no sort order. Unsupported expressions and missing sort columns
/// fail the write before data is committed.
#[cfg(feature = "write")]
fn sort_ordering_for(
    schema: &arrow::datatypes::Schema,
    sort_spec: Option<&crate::sort::SortSpec>,
) -> datafusion::common::Result<Option<datafusion::physical_expr::LexOrdering>> {
    let Some(sort_spec) = sort_spec else {
        return Ok(None);
    };
    let keys = sort_spec.producible_columns().ok_or_else(|| {
        DataFusionError::NotImplemented(format!(
            "DuckLake sort order {} contains an unsupported expression; \
             datafusion-ducklake can write only bare-column sort keys",
            sort_spec.sort_id
        ))
    })?;
    if keys.is_empty() {
        return Ok(None);
    }
    let mut sort_exprs = Vec::with_capacity(keys.len());
    for (name, direction, null_order) in &keys {
        let index = schema.index_of(name).map_err(|_| {
            DataFusionError::Plan(format!(
                "DuckLake sort key '{name}' is not present in the write schema"
            ))
        })?;
        let column = Arc::new(datafusion::physical_expr::expressions::Column::new(
            name, index,
        ));
        sort_exprs.push(
            datafusion::physical_expr_common::sort_expr::PhysicalSortExpr::new(
                column,
                arrow::compute::SortOptions {
                    descending: matches!(direction, crate::sort::SortDirection::Desc),
                    nulls_first: null_order.nulls_first(),
                },
            ),
        );
    }
    Ok(datafusion::physical_expr::LexOrdering::new(sort_exprs))
}

/// Re-state a page's per-file statistics for a caller that reads a data file
/// **without applying its delete file**.
///
/// [`build_datafusion_statistics`] marks a delete-bearing file's recorded bounds
/// [`Precision::Inexact`], and [`FilePruningStatistics`] prunes only on `Exact`
/// ones. So the first mutation that writes a delete file also stops that file
/// contributing usable bounds, and every later call opens it whatever its key
/// range. Nothing restores them: compaction skips delete-bearing files.
///
/// Those bounds stay usable for this caller. [`DuckLakeTable::resolve_positions`]
/// scans physical rows and does not apply delete files — it exists to compute
/// the positions a delete file records — so a recorded bound still contains
/// every row it can see, and `null_count`, harvested from the parquet footer,
/// still counts them.
///
/// `Exact` here is DataFusion's word for "usable", not a claim of a true
/// extreme. The DuckLake spec requires `min_value`/`max_value` only to be a
/// lower and upper bound ("does not have to be exact"), and this promotes them
/// no further than that: a bound over the physical rows is still a bound over
/// any subset of them, which is what makes it valid for a delete-applying
/// reader too.
///
/// This promotes every `Inexact` bound on such a file, including the widened
/// envelopes [`DuckLakeTable::apply_partition_bounds`] synthesises, which it
/// leaves `Inexact` so they cannot be mistaken for real extrema. That is
/// deliberate and safe for pruning — an envelope is a true bound — and safe
/// only because the map this returns lives and dies inside
/// [`DuckLakeTable::files_matching`]. It must not reach scan or aggregate
/// statistics, where an envelope presented as an extreme could answer a
/// MIN/MAX-from-statistics query wrongly.
///
/// The live row count is deliberately left out. [`file_row_count`] subtracts
/// `delete_count`, making it the one figure deletes genuinely change. Promoting
/// it alongside a physical `null_count` would be unsound rather than imprecise:
/// DataFusion guards a comparison with `null_count != row_count`, so a file with
/// two physical rows — one NULL, one matching, the NULL deleted — would present
/// `null_count == row_count`, be judged entirely null, and be dropped while
/// still physically holding the row the caller must find. Withholding the count
/// leaves that rewrite inert.
fn restate_in_physical_row_space(
    statistics: &mut HashMap<i64, Arc<Statistics>>,
    files: &[DuckLakeTableFile],
) {
    fn physical<T: Clone + std::fmt::Debug + Eq + PartialOrd>(
        value: &Precision<T>,
    ) -> Precision<T> {
        match value {
            Precision::Inexact(value) => Precision::Exact(value.clone()),
            other => other.clone(),
        }
    }

    for file in files.iter().filter(|f| f.delete_file.is_some()) {
        let Some(current) = statistics.get(&file.data_file_id) else {
            continue;
        };
        let mut restated = current.as_ref().clone();
        // Not physical: this is live rows, deletes already subtracted.
        restated.num_rows = Precision::Absent;
        for column in &mut restated.column_statistics {
            column.min_value = physical(&column.min_value);
            column.max_value = physical(&column.max_value);
            column.null_count = physical(&column.null_count);
        }
        statistics.insert(file.data_file_id, Arc::new(restated));
    }
}

struct FilePruningStatistics {
    base: PrunableStatistics,
    statistics: Vec<Arc<Statistics>>,
    schema: SchemaRef,
}

impl FilePruningStatistics {
    fn new(statistics: Vec<Arc<Statistics>>, schema: SchemaRef) -> Self {
        let base = PrunableStatistics::new(statistics.clone(), Arc::clone(&schema));
        Self {
            base,
            statistics,
            schema,
        }
    }

    fn exact_values(
        &self,
        column: &Column,
        get_statistic: impl Fn(&ColumnStatistics) -> &Precision<ScalarValue>,
    ) -> Option<ArrayRef> {
        let index = self.schema.index_of(column.name()).ok()?;
        // DataFusion 54 substitutes an untyped null for an unavailable bound,
        // which cannot share an Arrow array with typed scalar bounds.
        let typed_null = ScalarValue::try_new_null(self.schema.field(index).data_type()).ok()?;
        let mut has_value = false;
        let values = self.statistics.iter().map(|statistics| {
            statistics
                .column_statistics
                .get(index)
                .and_then(|statistics| match get_statistic(statistics) {
                    Precision::Exact(value) => {
                        has_value = true;
                        Some(value.clone())
                    },
                    _ => None,
                })
                .unwrap_or_else(|| typed_null.clone())
        });
        ScalarValue::iter_to_array(values)
            .ok()
            .filter(|_| has_value)
    }
}

impl PruningStatistics for FilePruningStatistics {
    fn min_values(&self, column: &Column) -> Option<ArrayRef> {
        self.exact_values(column, |statistics| &statistics.min_value)
    }

    fn max_values(&self, column: &Column) -> Option<ArrayRef> {
        self.exact_values(column, |statistics| &statistics.max_value)
    }

    fn num_containers(&self) -> usize {
        self.base.num_containers()
    }

    fn null_counts(&self, column: &Column) -> Option<ArrayRef> {
        self.base.null_counts(column)
    }

    fn row_counts(&self) -> Option<ArrayRef> {
        self.base.row_counts()
    }

    fn contained(&self, column: &Column, values: &HashSet<ScalarValue>) -> Option<BooleanArray> {
        self.base.contained(column, values)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::metadata_provider::{
        ColumnWithTable, DataFileChange, DeleteFileChange, FileWithTable, SchemaMetadata,
        SnapshotMetadata, TableMetadata, TableWithSchema,
    };
    use crate::partition::{PartitionSpecColumn, PartitionTransform};
    use crate::types::build_arrow_schema;
    #[cfg(feature = "write")]
    use datafusion::datasource::physical_plan::FileScanConfig;
    use datafusion::prelude::{SessionContext, col, lit};
    #[cfg(feature = "write")]
    use object_store::ObjectStore;
    use rstest::rstest;
    use std::sync::atomic::{AtomicUsize, Ordering};

    #[rstest]
    #[case("NULL", DataType::Utf8, ScalarValue::Utf8(None))]
    #[case("null", DataType::Int32, ScalarValue::Int32(None))]
    #[case("", DataType::Int32, ScalarValue::Int32(None))]
    #[case("", DataType::Utf8, ScalarValue::Utf8(Some(String::new())))]
    #[case("__HIVE_DEFAULT_PARTITION__", DataType::Int32, ScalarValue::Int32(None))]
    #[case("0x10", DataType::Int32, ScalarValue::Int32(Some(16)))]
    #[case("1e2", DataType::Decimal128(10, 2), ScalarValue::Decimal128(Some(10_000), 10, 2))]
    #[case("1.25e-1", DataType::Decimal128(10, 3), ScalarValue::Decimal128(Some(125), 10, 3))]
    fn hive_partition_values_match_duckdb(
        #[case] encoded: &str,
        #[case] data_type: DataType,
        #[case] expected: ScalarValue,
    ) {
        let path = format!("file:///tmp/p={encoded}/data.parquet");

        let actual = DuckLakeTable::hive_partition_value(&path, "p", &data_type).unwrap();

        assert_eq!(actual, expected);
    }

    #[rstest]
    #[case(
        "s3://bucket/a=one/../p=7/file.parquet",
        "a",
        DataType::Utf8,
        ScalarValue::Utf8(Some("one".to_string()))
    )]
    #[case(
        "s3://bucket/p=a#b/file.parquet",
        "p",
        DataType::Utf8,
        ScalarValue::Utf8(Some("a#b".to_string()))
    )]
    #[case(
        "s3://bucket/not=a?b/p=7/file.parquet",
        "p",
        DataType::Int32,
        ScalarValue::Int32(Some(7))
    )]
    #[case(r"s3:\bucket\p=7\file.parquet", "p", DataType::Int32, ScalarValue::Int32(Some(7)))]
    #[case(
        "s3://bucket/p%20q=7/file.parquet",
        "p%20q",
        DataType::Int32,
        ScalarValue::Int32(Some(7))
    )]
    #[case(
        "s3://bucket/p=a%20b/file.parquet",
        "p",
        DataType::Utf8,
        ScalarValue::Utf8(Some("a b".to_string()))
    )]
    fn hive_partition_path_parsing_matches_duckdb(
        #[case] path: &str,
        #[case] source_name: &str,
        #[case] data_type: DataType,
        #[case] expected: ScalarValue,
    ) {
        let actual = DuckLakeTable::hive_partition_value(path, source_name, &data_type).unwrap();

        assert_eq!(actual, expected);
    }

    #[rstest]
    #[case("s3://bucket/p=7=8/file.parquet", "p")]
    #[case("s3://bucket/p%20q=7/file.parquet", "p q")]
    fn hive_partition_path_rejects_non_candidates(#[case] path: &str, #[case] source_name: &str) {
        let error =
            DuckLakeTable::hive_partition_value(path, source_name, &DataType::Int32).unwrap_err();

        assert!(error.to_string().contains("absent from file path"));
    }

    #[derive(Debug, Default)]
    struct LazyMillionFileProvider {
        eager_file_reads: AtomicUsize,
        max_page: AtomicUsize,
        page_calls: AtomicUsize,
    }

    impl MetadataProvider for LazyMillionFileProvider {
        fn get_current_snapshot(&self) -> Result<i64> {
            Ok(1)
        }

        fn get_data_path(&self) -> Result<String> {
            Ok("memory:///".to_string())
        }

        fn list_snapshots(&self) -> Result<Vec<SnapshotMetadata>> {
            unimplemented!()
        }

        fn list_schemas(&self, _snapshot_id: i64) -> Result<Vec<SchemaMetadata>> {
            unimplemented!()
        }

        fn list_tables(&self, _schema_id: i64, _snapshot_id: i64) -> Result<Vec<TableMetadata>> {
            unimplemented!()
        }

        fn get_table_structure(
            &self,
            table_id: i64,
            _snapshot_id: i64,
        ) -> Result<Vec<DuckLakeTableColumn>> {
            if table_id == 2 {
                return Ok(vec![
                    DuckLakeTableColumn::new(
                        1,
                        "range_value".to_string(),
                        "bigint".to_string(),
                        false,
                    ),
                    DuckLakeTableColumn::new(
                        2,
                        "partition_key".to_string(),
                        "bigint".to_string(),
                        false,
                    ),
                ]);
            }
            Ok(vec![DuckLakeTableColumn::new(
                1,
                "id".to_string(),
                "bigint".to_string(),
                false,
            )])
        }

        fn get_table_files_for_select(
            &self,
            _table_id: i64,
            _snapshot_id: i64,
        ) -> Result<Vec<DuckLakeTableFile>> {
            self.eager_file_reads.fetch_add(1, Ordering::Relaxed);
            panic!("the eager file API must not be used while planning a scan")
        }

        fn get_table_summary_statistics(
            &self,
            table_id: i64,
            _snapshot_id: i64,
        ) -> Result<DuckLakeStatistics> {
            if table_id == 2 {
                return Ok(DuckLakeStatistics::default());
            }
            Ok(DuckLakeStatistics {
                table: Some(crate::metadata_provider::DuckLakeTableStatistics {
                    record_count: Some(1_000_000),
                    file_size_bytes: Some(1_000_000),
                }),
                columns: vec![DuckLakeTableColumnStatistics {
                    column_id: 1,
                    contains_null: Some(false),
                    min_value: Some("1".to_string()),
                    max_value: Some("1000000".to_string()),
                    contains_nan: None,
                    column_size_bytes: Some(1_000_000),
                    bounds_are_exact: true,
                }],
                ..Default::default()
            })
        }

        fn get_table_file_metadata_page(
            &self,
            _table_id: i64,
            snapshot_id: i64,
            after_data_file_id: Option<i64>,
            limit: usize,
        ) -> Result<Vec<DuckLakeFileMetadata>> {
            self.page_calls.fetch_add(1, Ordering::Relaxed);
            let start = after_data_file_id.unwrap_or(0) + 1;
            let end = (start + i64::try_from(limit).unwrap()).min(1_000_001);
            let page: Vec<_> = (start..end)
                .map(|data_file_id| DuckLakeFileMetadata {
                    file: DuckLakeTableFile {
                        data_file_id,
                        file: DuckLakeFileData::new(
                            format!("file-{data_file_id}.parquet"),
                            true,
                            1,
                        ),
                        delete_file_id: None,
                        delete_file: None,
                        row_id_start: Some(data_file_id - 1),
                        snapshot_id: Some(snapshot_id),
                        begin_snapshot: Some(1),
                        schema_version: Some(0),
                        partial_max: None,
                        max_row_count: Some(1),
                        delete_count: None,
                        partition_id: None,
                        partition_values: Vec::new(),
                    },
                    column_statistics: vec![DuckLakeFileColumnStatistics {
                        data_file_id,
                        column_id: 1,
                        column_size_bytes: Some(1),
                        value_count: Some(1),
                        null_count: Some(0),
                        min_value: Some(data_file_id.to_string()),
                        max_value: Some(data_file_id.to_string()),
                        contains_nan: None,
                    }],
                })
                .collect();
            self.max_page.fetch_max(page.len(), Ordering::Relaxed);
            Ok(page)
        }

        fn get_schema_by_name(
            &self,
            _name: &str,
            _snapshot_id: i64,
        ) -> Result<Option<SchemaMetadata>> {
            unimplemented!()
        }

        fn get_table_by_name(
            &self,
            _schema_id: i64,
            _name: &str,
            _snapshot_id: i64,
        ) -> Result<Option<TableMetadata>> {
            unimplemented!()
        }

        fn table_exists(&self, _schema_id: i64, _name: &str, _snapshot_id: i64) -> Result<bool> {
            unimplemented!()
        }

        fn list_all_tables(&self, _snapshot_id: i64) -> Result<Vec<TableWithSchema>> {
            unimplemented!()
        }

        fn list_all_columns(&self, _snapshot_id: i64) -> Result<Vec<ColumnWithTable>> {
            unimplemented!()
        }

        fn list_all_files(&self, _snapshot_id: i64) -> Result<Vec<FileWithTable>> {
            unimplemented!()
        }

        fn get_data_files_added_between_snapshots(
            &self,
            _table_id: i64,
            _start_snapshot: i64,
            _end_snapshot: i64,
        ) -> Result<Vec<DataFileChange>> {
            unimplemented!()
        }

        fn get_delete_files_added_between_snapshots(
            &self,
            _table_id: i64,
            _start_snapshot: i64,
            _end_snapshot: i64,
        ) -> Result<Vec<DeleteFileChange>> {
            unimplemented!()
        }
    }

    #[test]
    fn planning_prunes_a_lazy_million_file_fixture_in_bounded_pages() -> Result<()> {
        let provider = Arc::new(LazyMillionFileProvider::default());
        let table = DuckLakeTable::new(
            1,
            "events",
            provider.clone(),
            1,
            Arc::new(ObjectStoreUrl::parse("memory://").unwrap()),
            String::new(),
        )?;
        assert_eq!(provider.eager_file_reads.load(Ordering::Relaxed), 0);
        let statistics = table.statistics().unwrap();
        assert_eq!(statistics.num_rows, Precision::Exact(1_000_000));
        assert_eq!(
            statistics.column_statistics[0].min_value,
            Precision::Exact(ScalarValue::Int64(Some(1)))
        );
        assert_eq!(
            statistics.column_statistics[0].max_value,
            Precision::Exact(ScalarValue::Int64(Some(1_000_000)))
        );
        assert_eq!(
            statistics.column_statistics[0].byte_size,
            Precision::Inexact(1_000_000)
        );

        let state = SessionContext::new().state();
        let filters = [col("id").eq(lit(999_999_i64))];
        let conjuncts = table.physical_conjuncts(&state, &filters).unwrap();
        let pruning = table.pruning_predicates(conjuncts).unwrap();
        let mut after = None;
        let mut retained = Vec::new();
        loop {
            let metadata =
                provider.get_table_file_metadata_page(1, 1, after, FILE_METADATA_BATCH_SIZE)?;
            if metadata.is_empty() {
                break;
            }
            after = metadata.last().map(|entry| entry.file.data_file_id);
            let files: Vec<_> = metadata.iter().map(|entry| entry.file.clone()).collect();
            let catalog_statistics = metadata
                .into_iter()
                .flat_map(|entry| entry.column_statistics)
                .collect();
            let (_, file_statistics) = build_datafusion_statistics(
                table.physical_schema.as_ref(),
                &table.columns,
                &files,
                DuckLakeStatistics {
                    files: catalog_statistics,
                    ..Default::default()
                },
                false,
                true,
            );
            retained.extend(
                table
                    .prune_table_files_iteratively(&pruning, &files, &file_statistics)
                    .into_iter()
                    .map(|file| file.data_file_id),
            );
        }

        assert_eq!(provider.max_page.load(Ordering::Relaxed), 4_096);
        assert_eq!(provider.page_calls.load(Ordering::Relaxed), 246);
        assert_eq!(retained, vec![999_999]);
        assert_eq!(provider.eager_file_reads.load(Ordering::Relaxed), 0);
        Ok(())
    }

    #[test]
    fn iterative_pruning_revisits_range_conjunct_after_partition_conjunct() -> Result<()> {
        let table = DuckLakeTable::new(
            2,
            "events",
            Arc::new(LazyMillionFileProvider::default()),
            1,
            Arc::new(ObjectStoreUrl::parse("memory://").unwrap()),
            String::new(),
        )?;
        let state = SessionContext::new().state();
        let filters = [col("range_value").eq(lit(1_i64)), col("partition_key").eq(lit(1_i64))];
        let predicates = table.pruning_predicates(table.physical_conjuncts(&state, &filters)?)?;
        let file = |data_file_id| DuckLakeTableFile {
            data_file_id,
            file: DuckLakeFileData::new(format!("file-{data_file_id}.parquet"), true, 1),
            delete_file_id: None,
            delete_file: None,
            row_id_start: None,
            snapshot_id: Some(1),
            begin_snapshot: Some(1),
            schema_version: Some(0),
            partial_max: None,
            max_row_count: Some(1),
            delete_count: None,
            partition_id: None,
            partition_values: Vec::new(),
        };
        let files = vec![file(1), file(2), file(3)];
        let statistics = |range_value: Option<i64>, partition_key: i64| {
            let mut statistics = Statistics::new_unknown(table.physical_schema.as_ref());
            if let Some(value) = range_value {
                statistics.column_statistics[0].min_value =
                    Precision::Exact(ScalarValue::Int64(Some(value)));
                statistics.column_statistics[0].max_value =
                    Precision::Exact(ScalarValue::Int64(Some(value)));
            }
            statistics.column_statistics[1].min_value =
                Precision::Exact(ScalarValue::Int64(Some(partition_key)));
            statistics.column_statistics[1].max_value =
                Precision::Exact(ScalarValue::Int64(Some(partition_key)));
            Arc::new(statistics)
        };
        let file_statistics = HashMap::from([
            (1, statistics(Some(1), 1)),
            (2, statistics(Some(2), 1)),
            (3, statistics(None, 2)),
        ]);

        let retained = table
            .prune_table_files_iteratively(&predicates, &files, &file_statistics)
            .into_iter()
            .map(|file| file.data_file_id)
            .collect::<Vec<_>>();

        assert_eq!(retained, vec![1]);
        Ok(())
    }

    // ==================== `files_matching` ====================
    //
    // Fixture shape: a two-column table `(id BIGINT, region BIGINT)` optionally
    // partitioned by `region` with the `identity` transform. `id` stands in for a
    // mutation's key column and `region` for the partition column, so a predicate
    // can name one without the other.

    const ID_COLUMN_ID: i64 = 1;
    const REGION_COLUMN_ID: i64 = 2;

    /// A catalog of exactly the files a test hands it, served through the paged
    /// metadata API the pruning paths read.
    #[derive(Debug)]
    struct FixedFileProvider {
        files: Vec<DuckLakeFileMetadata>,
        partition_spec: Option<PartitionSpec>,
        /// Which files a page carries once a caller pushes a filter down, by
        /// `data_file_id`. `None` ignores the filter and lists everything, which
        /// is what the trait's default method does and what every test that does
        /// not name this field exercises.
        ///
        /// A real catalog decides this by evaluating the lowered filter against
        /// `ducklake_file_column_stats`; naming the surviving files outright says
        /// the same thing without a second implementation of those semantics.
        /// What matters here is that the other files are never listed at all, so
        /// nothing downstream — including key collection — can see them.
        visible_when_filtered: Option<Vec<i64>>,
    }

    impl MetadataProvider for FixedFileProvider {
        fn get_current_snapshot(&self) -> Result<i64> {
            Ok(1)
        }

        fn get_data_path(&self) -> Result<String> {
            Ok("memory:///".to_string())
        }

        fn get_table_structure(
            &self,
            _table_id: i64,
            _snapshot_id: i64,
        ) -> Result<Vec<DuckLakeTableColumn>> {
            Ok(vec![
                DuckLakeTableColumn::new(
                    ID_COLUMN_ID,
                    "id".to_string(),
                    "bigint".to_string(),
                    false,
                ),
                DuckLakeTableColumn::new(
                    REGION_COLUMN_ID,
                    "region".to_string(),
                    "bigint".to_string(),
                    false,
                ),
            ])
        }

        fn get_partition_spec(
            &self,
            _table_id: i64,
            _snapshot_id: i64,
        ) -> Result<Option<PartitionSpec>> {
            Ok(self.partition_spec.clone())
        }

        fn get_table_files_for_select(
            &self,
            _table_id: i64,
            _snapshot_id: i64,
        ) -> Result<Vec<DuckLakeTableFile>> {
            Ok(self.files.iter().map(|entry| entry.file.clone()).collect())
        }

        fn get_table_file_metadata_page(
            &self,
            _table_id: i64,
            _snapshot_id: i64,
            after_data_file_id: Option<i64>,
            limit: usize,
        ) -> Result<Vec<DuckLakeFileMetadata>> {
            Ok(self
                .files
                .iter()
                .filter(|entry| {
                    after_data_file_id.is_none_or(|after| entry.file.data_file_id > after)
                })
                .take(limit)
                .cloned()
                .collect())
        }

        fn get_table_file_metadata_page_filtered(
            &self,
            table_id: i64,
            snapshot_id: i64,
            after_data_file_id: Option<i64>,
            limit: usize,
            filter: Option<&crate::stats_filter::StatsFilter>,
        ) -> Result<Vec<DuckLakeFileMetadata>> {
            let Some(visible) = self
                .visible_when_filtered
                .as_ref()
                .filter(|_| filter.is_some())
            else {
                return self.get_table_file_metadata_page(
                    table_id,
                    snapshot_id,
                    after_data_file_id,
                    limit,
                );
            };
            // Narrowed inside the "query", ahead of the "limit", exactly as the
            // real providers must: the keyset cursor still advances over the
            // surviving files only.
            Ok(self
                .files
                .iter()
                .filter(|entry| visible.contains(&entry.file.data_file_id))
                .filter(|entry| {
                    after_data_file_id.is_none_or(|after| entry.file.data_file_id > after)
                })
                .take(limit)
                .cloned()
                .collect())
        }

        fn get_table_summary_statistics(
            &self,
            _table_id: i64,
            _snapshot_id: i64,
        ) -> Result<DuckLakeStatistics> {
            Ok(DuckLakeStatistics::default())
        }

        fn list_snapshots(&self) -> Result<Vec<SnapshotMetadata>> {
            unimplemented!()
        }

        fn list_schemas(&self, _snapshot_id: i64) -> Result<Vec<SchemaMetadata>> {
            unimplemented!()
        }

        fn list_tables(&self, _schema_id: i64, _snapshot_id: i64) -> Result<Vec<TableMetadata>> {
            unimplemented!()
        }

        fn get_schema_by_name(
            &self,
            _name: &str,
            _snapshot_id: i64,
        ) -> Result<Option<SchemaMetadata>> {
            unimplemented!()
        }

        fn get_table_by_name(
            &self,
            _schema_id: i64,
            _name: &str,
            _snapshot_id: i64,
        ) -> Result<Option<TableMetadata>> {
            unimplemented!()
        }

        fn table_exists(&self, _schema_id: i64, _name: &str, _snapshot_id: i64) -> Result<bool> {
            unimplemented!()
        }

        fn list_all_tables(&self, _snapshot_id: i64) -> Result<Vec<TableWithSchema>> {
            unimplemented!()
        }

        fn list_all_columns(&self, _snapshot_id: i64) -> Result<Vec<ColumnWithTable>> {
            unimplemented!()
        }

        fn list_all_files(&self, _snapshot_id: i64) -> Result<Vec<FileWithTable>> {
            unimplemented!()
        }

        fn get_data_files_added_between_snapshots(
            &self,
            _table_id: i64,
            _start_snapshot: i64,
            _end_snapshot: i64,
        ) -> Result<Vec<DataFileChange>> {
            unimplemented!()
        }

        fn get_delete_files_added_between_snapshots(
            &self,
            _table_id: i64,
            _start_snapshot: i64,
            _end_snapshot: i64,
        ) -> Result<Vec<DeleteFileChange>> {
            unimplemented!()
        }
    }

    /// The partition value a file records for `region`: `None` = the file records
    /// no partition value at all, `Some(None)` = it records SQL NULL.
    type RegionValue = Option<Option<&'static str>>;

    /// One data file. `id_bounds` become exact catalog min/max statistics for
    /// `id`; `None` leaves the file without any statistics, the shape a file
    /// written before statistics collection has. `region` never carries column
    /// statistics, so anything that prunes it must come from the partition value.
    fn fixture_file(
        data_file_id: i64,
        region: RegionValue,
        id_bounds: Option<(i64, i64)>,
    ) -> DuckLakeFileMetadata {
        let mut file = DuckLakeTableFile::new(DuckLakeFileData::new(
            format!("file-{data_file_id}.parquet"),
            true,
            1,
        ));
        file.data_file_id = data_file_id;
        file.snapshot_id = Some(1);
        file.begin_snapshot = Some(1);
        file.max_row_count = Some(1);
        if let Some(value) = region {
            file.partition_id = Some(1);
            file.partition_values = vec![(0, value.map(str::to_string))];
        }
        let column_statistics = id_bounds
            .map(|(min, max)| DuckLakeFileColumnStatistics {
                data_file_id,
                column_id: ID_COLUMN_ID,
                column_size_bytes: Some(8),
                value_count: Some(1),
                null_count: Some(0),
                min_value: Some(min.to_string()),
                max_value: Some(max.to_string()),
                contains_nan: None,
            })
            .into_iter()
            .collect();
        DuckLakeFileMetadata {
            file,
            column_statistics,
        }
    }

    /// A spec partitioning by `region` with the `identity` transform.
    ///
    /// The partition key index (0) deliberately differs from `region`'s position
    /// in the schema (1): a bound written at the key index instead of the column
    /// index would land on `id`, the very column a keyed mutation filters on.
    fn region_spec(prune_safe: bool) -> PartitionSpec {
        PartitionSpec {
            partition_id: 1,
            columns: vec![PartitionSpecColumn {
                partition_key_index: 0,
                column_id: REGION_COLUMN_ID,
                transform: PartitionTransform::Identity,
            }],
            prune_safe,
        }
    }

    fn fixed_table(
        files: Vec<DuckLakeFileMetadata>,
        partition_spec: Option<PartitionSpec>,
    ) -> Result<DuckLakeTable> {
        DuckLakeTable::new(
            1,
            "events",
            Arc::new(FixedFileProvider {
                files,
                partition_spec,
                visible_when_filtered: None,
            }),
            1,
            Arc::new(ObjectStoreUrl::parse("memory://").unwrap()),
            String::new(),
        )
    }

    /// A table whose catalog honours a pushed-down filter, listing only
    /// `visible_when_filtered` for a page that carries one. An unfiltered
    /// listing — [`DuckLakeTable::files`] among them — still sees every file.
    fn catalog_filtering_table(
        files: Vec<DuckLakeFileMetadata>,
        visible_when_filtered: Vec<i64>,
    ) -> Result<DuckLakeTable> {
        DuckLakeTable::new(
            1,
            "events",
            Arc::new(FixedFileProvider {
                files,
                partition_spec: None,
                visible_when_filtered: Some(visible_when_filtered),
            }),
            1,
            Arc::new(ObjectStoreUrl::parse("memory://").unwrap()),
            String::new(),
        )
    }

    /// The physical expression a caller would build for a scan, and hand to both
    /// `files_matching` and `resolve_positions`.
    /// `predicate_is_prunable` must distinguish a REAL physical rename from the
    /// synthetic `_ducklake_internal_row_id -> rowid` entry that every file
    /// written by `UPDATE` or compaction carries.
    ///
    /// This is asserted here rather than end-to-end because the guard is read
    /// only inside `resolve_positions`, whose plan is built and executed by
    /// `DuckLakeDeleteExec` and never surfaces in `EXPLAIN ANALYZE`. An
    /// integration test can observe that a `DELETE` is correct, but not that it
    /// pruned.
    #[test]
    fn predicate_is_prunable_ignores_the_synthetic_rowid_rename() -> Result<()> {
        let table = DuckLakeTable::new(
            1,
            "events",
            Arc::new(LazyMillionFileProvider::default()),
            1,
            Arc::new(ObjectStoreUrl::parse("memory://").unwrap()),
            String::new(),
        )?;
        let predicate = physical_predicate(&table, col("id").eq(lit(5_i64)));

        let cfg = |renamed: Vec<usize>, mapping: Vec<(&str, &str)>| FileReadConfig {
            read_schema: table.physical_schema.clone(),
            name_mapping: mapping
                .into_iter()
                .map(|(a, b)| (a.to_string(), b.to_string()))
                .collect(),
            constants: HashMap::new(),
            row_group_count: 4,
            renamed_column_indices: Arc::new(renamed.into_iter().collect()),
            embedded_rowid_parquet_name: None,
            embedded_snapshot_parquet_name: None,
            drops_current_columns: false,
        };
        let id_index = table.physical_schema.index_of("id").unwrap();

        // A file written by UPDATE / compaction: one mapping entry, but no data
        // column is renamed. Must still prune.
        assert!(
            table.predicate_is_prunable(
                &predicate,
                &cfg(
                    vec![],
                    vec![(
                        crate::row_id::EMBEDDED_ROW_ID_COLUMN_NAME,
                        ROWID_COLUMN_NAME
                    )]
                )
            ),
            "the synthetic rowid rename must not disable pruning"
        );

        // The predicate's own column reads under a different physical name, so
        // it cannot bind in the reader and must not be pushed.
        assert!(
            !table.predicate_is_prunable(&predicate, &cfg(vec![id_index], vec![])),
            "a predicate on a renamed column must disable pruning"
        );

        // A DIFFERENT column is renamed or absent — the shape a file older than
        // an `ADD COLUMN` has. The predicate never references it, so pruning
        // stands. (This fixture's table has one column, so the unrelated index
        // is synthetic; what matters is that it is not the one the predicate
        // references.)
        assert!(
            table.predicate_is_prunable(&predicate, &cfg(vec![id_index + 1], vec![])),
            "an unrelated renamed/absent column must not disable pruning"
        );

        // Nothing renamed: prunable.
        assert!(table.predicate_is_prunable(&predicate, &cfg(vec![], vec![])));
        Ok(())
    }

    fn physical_predicate(table: &DuckLakeTable, expr: Expr) -> Arc<dyn PhysicalExpr> {
        let df_schema = DFSchema::try_from(table.physical_schema.as_ref().clone()).unwrap();
        SessionContext::new()
            .state()
            .create_physical_expr(expr, &df_schema)
            .unwrap()
    }

    fn matching_ids(table: &DuckLakeTable, predicate: &Arc<dyn PhysicalExpr>) -> Result<Vec<i64>> {
        Ok(table
            .files_matching(predicate)?
            .into_iter()
            .map(|file| file.data_file_id)
            .collect())
    }

    #[test]
    fn files_matching_does_not_judge_a_delete_bearing_file_all_null() -> Result<()> {
        // The hazard the restatement is built around, and the one the simpler
        // test above cannot see because its files have no nulls.
        //
        // `null_count` is physical (parquet footer) while `file_row_count` is
        // live (deletes subtracted). Present both as usable and DataFusion's
        // `null_count != row_count` guard reads two physical rows -- one NULL,
        // one matching -- with the NULL deleted as "one null, one row, so
        // entirely null", and prunes a file that still physically holds the row
        // the caller has to find. Withholding the row count is what stops that,
        // so this fails if a future change promotes it.
        let mut entry = fixture_file(1, None, Some((5, 5)));
        entry.file.max_row_count = Some(2); // physical: [NULL, 5]
        entry.file.delete_count = Some(1); // the NULL is deleted -> 1 live row
        entry.file.delete_file_id = Some(1);
        entry.file.delete_file = Some(DuckLakeFileData::new(
            "delete-1.parquet".to_string(),
            true,
            1,
        ));
        for stats in &mut entry.column_statistics {
            stats.null_count = Some(1);
            stats.value_count = Some(2);
        }

        let table = fixed_table(vec![entry], None)?;
        let predicate = physical_predicate(&table, col("id").eq(lit(5_i64)));
        assert_eq!(
            matching_ids(&table, &predicate)?,
            vec![1],
            "a physical null_count equal to the LIVE row count must not make the \
             file look entirely null -- the matching physical row is still there"
        );
        Ok(())
    }

    #[test]
    fn files_matching_prunes_a_delete_bearing_file_by_its_partition_bound() -> Result<()> {
        // Same fixture and predicate as
        // `files_matching_prunes_a_partition_column_without_column_statistics`,
        // with one variable changed: the file that must be pruned carries a
        // delete file. Its only bound is the one `apply_partition_bounds`
        // synthesises, which the restatement also promotes -- so this pins that
        // a delete-bearing file is still prunable when partition values are all
        // it has, which is the shape a partitioned keyed mutation actually hits.
        let delete_bearing = |data_file_id: i64, value: &'static str| {
            let mut entry = fixture_file(data_file_id, Some(Some(value)), None);
            entry.file.max_row_count = Some(10);
            entry.file.delete_count = Some(1);
            entry.file.delete_file_id = Some(data_file_id);
            entry.file.delete_file = Some(DuckLakeFileData::new(
                format!("delete-{data_file_id}.parquet"),
                true,
                1,
            ));
            entry
        };

        let table = fixed_table(
            vec![fixture_file(1, Some(Some("10")), None), delete_bearing(2, "9999")],
            Some(region_spec(true)),
        )?;

        let predicate = physical_predicate(&table, col("region").eq(lit(10_i64)));
        assert_eq!(
            matching_ids(&table, &predicate)?,
            vec![1],
            "a delete-bearing file whose partition bound excludes the value must \
             still prune"
        );
        Ok(())
    }

    #[test]
    fn files_matching_still_prunes_a_file_that_carries_deletes() -> Result<()> {
        // `build_datafusion_statistics` marks a delete-bearing file's bounds
        // Inexact, and the pruning view surfaces only Exact ones. So without the
        // physical-space restatement, the first mutation that writes a delete
        // file makes that file unprunable for every mutation after it — and
        // permanently, because compaction skips delete-bearing files. Both
        // directions are asserted: the file must still be dropped when its
        // bounds exclude the value, and still kept when they do not.
        let carrying_deletes = |data_file_id: i64, id_bounds: (i64, i64)| {
            let mut entry = fixture_file(data_file_id, None, Some(id_bounds));
            // Leave live rows > 0, so the separate known-empty exclusion is not
            // what drops the file.
            entry.file.max_row_count = Some(10);
            entry.file.delete_count = Some(1);
            entry.file.delete_file_id = Some(data_file_id);
            entry.file.delete_file = Some(DuckLakeFileData::new(
                format!("delete-{data_file_id}.parquet"),
                true,
                1,
            ));
            entry
        };

        let table = fixed_table(
            vec![
                fixture_file(1, None, Some((1, 10))),
                carrying_deletes(2, (100, 110)),
                fixture_file(3, None, Some((200, 210))),
            ],
            None,
        )?;

        let inside = physical_predicate(&table, col("id").eq(lit(105_i64)));
        assert_eq!(
            matching_ids(&table, &inside)?,
            vec![2],
            "a delete-bearing file whose bounds admit the value must be kept"
        );

        let outside = physical_predicate(&table, col("id").eq(lit(5_i64)));
        assert_eq!(
            matching_ids(&table, &outside)?,
            vec![1],
            "a delete-bearing file whose bounds exclude the value must still prune"
        );
        Ok(())
    }

    #[test]
    fn files_matching_prunes_unpartitioned_files_by_column_statistics() -> Result<()> {
        let table = fixed_table(
            vec![
                fixture_file(1, None, Some((1, 10))),
                fixture_file(2, None, Some((100, 110))),
                fixture_file(3, None, Some((200, 210))),
            ],
            None,
        )?;

        let predicate = physical_predicate(&table, col("id").eq(lit(105_i64)));

        assert_eq!(matching_ids(&table, &predicate)?, vec![2]);
        Ok(())
    }

    /// Hex-encoded 16-byte AES key, standing in for a catalog-recorded one.
    #[cfg(feature = "encryption")]
    const FIXTURE_ENCRYPTION_KEY: &str = "0123456789abcdef0123456789abcdef";

    /// A file the catalog records an encryption key for.
    #[cfg(feature = "encryption")]
    fn encrypted_fixture_file(data_file_id: i64, id_bounds: (i64, i64)) -> DuckLakeFileMetadata {
        let mut entry = fixture_file(data_file_id, None, Some(id_bounds));
        entry.file.file.encryption_key = Some(FIXTURE_ENCRYPTION_KEY.to_string());
        entry
    }

    /// The key `path` resolves to through the table's installed factory, or
    /// `None` when the factory holds no key for it. `None` is also what an
    /// unencrypted file gets, so only ever ask this about an encrypted one.
    #[cfg(feature = "encryption")]
    async fn installed_decryption_properties(
        table: &DuckLakeTable,
        path: &str,
    ) -> Option<Arc<parquet::encryption::decrypt::FileDecryptionProperties>> {
        let factory = table
            .encryption_keys
            .factory
            .get()
            .cloned()
            .expect("an encrypted table installs a factory");
        factory
            .get_file_decryption_properties(&Default::default(), &ObjectPath::from(path))
            .await
            .expect("key lookup succeeds")
    }

    /// Pruning a file must not take its decryption key with it. This catalog
    /// lists every file — the filter reaches a provider that ignores it, as the
    /// trait's default method does — so the in-memory pass prunes files whose
    /// metadata, keys included, was collected on the way past.
    /// `files_matching_keeps_keys_the_catalog_filter_left_unlisted` covers the
    /// other half, where the file never reaches the loop at all.
    #[cfg(feature = "encryption")]
    #[tokio::test]
    async fn files_matching_keeps_decryption_keys_for_pruned_files() {
        let table = fixed_table(
            vec![encrypted_fixture_file(1, (1, 10)), encrypted_fixture_file(2, (100, 110))],
            None,
        )
        .expect("table opens");

        let predicate = physical_predicate(&table, col("id").eq(lit(105_i64)));
        let matching = table.files_matching(&predicate).expect("pruning succeeds");
        assert_eq!(
            matching.len(),
            1,
            "file 1's statistics exclude the key, so it is pruned"
        );

        assert!(
            installed_decryption_properties(&table, "file-1.parquet")
                .await
                .is_some(),
            "the pruned file's key must stay installed for other readers",
        );
    }

    /// A file the catalog filter leaves unlisted must keep the key an earlier
    /// listing installed for it.
    ///
    /// Catalog-side filtering is not the same as pruning: the file never reaches
    /// the page loop, so nothing there can collect its key. Installing the keys
    /// of the listed files *in place of* the table's set would therefore strand
    /// the key of every file the mutation did not touch — including the ones the
    /// caller is holding from its own `files()` listing and is about to read
    /// through `resolve_positions`, which opens them with whatever factory is
    /// installed. Merging is what keeps those readable.
    #[cfg(feature = "encryption")]
    #[tokio::test]
    async fn files_matching_keeps_keys_the_catalog_filter_left_unlisted() {
        let table = catalog_filtering_table(
            vec![encrypted_fixture_file(1, (1, 10)), encrypted_fixture_file(2, (100, 110))],
            // Only file 2 survives the filter this predicate lowers to.
            vec![2],
        )
        .expect("table opens");

        // How a keyed mutation gets its files: an unfiltered listing first, which
        // sees the whole snapshot and installs both keys.
        assert_eq!(table.files().expect("listing succeeds").len(), 2);

        let predicate = physical_predicate(&table, col("id").eq(lit(105_i64)));
        let matching = table.files_matching(&predicate).expect("pruning succeeds");
        assert_eq!(
            matching
                .iter()
                .map(|file| file.data_file_id)
                .collect::<Vec<_>>(),
            vec![2],
            "the catalog leaves file 1 out of the listing entirely"
        );

        assert!(
            installed_decryption_properties(&table, "file-1.parquet")
                .await
                .is_some(),
            "a file the catalog never listed must keep the key an earlier listing installed",
        );
        assert!(
            installed_decryption_properties(&table, "file-2.parquet")
                .await
                .is_some(),
            "the returned file's own key must be installed",
        );
    }

    /// A second install of the same path, carrying a different key, must be the
    /// key the factory then serves.
    ///
    /// Nothing in this crate produces one: every data and delete file is named
    /// by a fresh `Uuid::new_v4()`, compaction and partitioned and streaming
    /// writes included. The guarantee is still load-bearing, because the
    /// alternative is silent and permanent — a factory answering out of its own
    /// copy of the map would hand every reader the superseded key for the rest
    /// of the table's life, with the installed set and the served set
    /// disagreeing and no way to tell from either side.
    #[cfg(feature = "encryption")]
    #[tokio::test]
    async fn installing_a_second_key_for_a_path_supersedes_the_first() {
        /// A different valid key, so the two are told apart by what the factory
        /// returns rather than by one of them failing to decode.
        const SUPERSEDED_KEY: &str = "fedcba9876543210fedcba9876543210";

        let table = fixed_table(Vec::new(), None).expect("table opens");
        let install = |key: &str| {
            table.install_encryption_keys(HashMap::from([(
                "file-1.parquet".to_string(),
                key.to_string(),
            )]));
        };

        install(SUPERSEDED_KEY);
        // The same path with a new key: `HashMap::extend` overwrites the entry
        // and leaves the length alone, so "did the length change" is not a test
        // of whether anything was installed.
        install(FIXTURE_ENCRYPTION_KEY);

        let properties = installed_decryption_properties(&table, "file-1.parquet")
            .await
            .expect("the path has a key installed");
        assert_eq!(
            properties
                .footer_key(None)
                .expect("a footer key")
                .as_slice(),
            DuckLakeEncryptionFactory::decode_key(FIXTURE_ENCRYPTION_KEY)
                .expect("the fixture key decodes")
                .as_slice(),
            "the factory must serve the key installed last, not the superseded one",
        );
    }

    /// One factory serves every page of a listing, and serves keys installed
    /// after it was handed out.
    ///
    /// Both halves matter. A reader that cloned the factory while planning
    /// page 1 must be able to open a file listed on page 50 — that is the
    /// guarantee accumulation exists for. And because the factory reads the
    /// installed map rather than a copy of it, no page rebuilds it: rebuilding
    /// per page would clone every key collected so far and make a P-page
    /// listing quadratic in P.
    #[cfg(feature = "encryption")]
    #[tokio::test]
    async fn one_factory_serves_the_keys_of_every_page() {
        let table = fixed_table(Vec::new(), None).expect("table opens");
        let install = |path: &str| {
            table.install_encryption_keys(HashMap::from([(
                path.to_string(),
                FIXTURE_ENCRYPTION_KEY.to_string(),
            )]));
        };
        let installed_factory = || {
            table
                .encryption_keys
                .factory
                .get()
                .cloned()
                .expect("an encrypted table installs a factory")
        };

        install("file-1.parquet");
        let from_first_page = installed_factory();
        install("file-2.parquet");

        assert!(
            Arc::ptr_eq(&from_first_page, &installed_factory()),
            "a later page must reuse the installed factory, not rebuild it",
        );
        assert!(
            from_first_page
                .get_file_decryption_properties(
                    &Default::default(),
                    &ObjectPath::from("file-2.parquet"),
                )
                .await
                .expect("key lookup succeeds")
                .is_some(),
            "the factory handed out on page 1 must resolve a key installed on page 2",
        );
    }

    /// A file with no recorded statistics is always kept, while exact bounds can
    /// still prune the files beside it.
    #[test]
    fn files_matching_keeps_files_without_statistics() -> Result<()> {
        let table = fixed_table(
            vec![
                fixture_file(1, None, Some((1, 10))),
                fixture_file(2, None, None),
                fixture_file(3, None, Some((200, 210))),
            ],
            None,
        )?;

        let predicate = physical_predicate(&table, col("id").eq(lit(105_i64)));

        assert_eq!(matching_ids(&table, &predicate)?, vec![2]);
        Ok(())
    }

    /// A file the catalog records as holding no rows, and — the case that matters —
    /// carrying no per-column statistics row at all.
    ///
    /// A statistics harvest failure
    /// ([`crate::stats_collect::collect_column_stats`] yields no rows) and a column
    /// added by later DDL both produce this shape.
    fn empty_file_without_statistics(data_file_id: i64) -> DuckLakeFileMetadata {
        let mut entry = fixture_file(data_file_id, None, None);
        entry.file.max_row_count = Some(0);
        entry
    }

    /// A file whose `record_count` the catalog leaves unset — what a provider that
    /// does not surface one produces. Unknown, never empty.
    fn file_with_unknown_row_count(
        data_file_id: i64,
        id_bounds: (i64, i64),
    ) -> DuckLakeFileMetadata {
        let mut entry = fixture_file(data_file_id, None, Some(id_bounds));
        entry.file.max_row_count = None;
        entry
    }

    /// A 0-row file with no statistics row of its own, alongside files whose
    /// statistics are ideal.
    ///
    /// The control is
    /// `files_matching_prunes_unpartitioned_files_by_column_statistics`: the same
    /// three row-bearing files without the empty one, same predicate, same result.
    #[test]
    fn files_matching_prunes_around_an_empty_file_without_statistics() -> Result<()> {
        let table = fixed_table(
            vec![
                fixture_file(1, None, Some((1, 10))),
                fixture_file(2, None, Some((100, 110))),
                fixture_file(3, None, Some((200, 210))),
                empty_file_without_statistics(4),
            ],
            None,
        )?;

        let predicate = physical_predicate(&table, col("id").eq(lit(105_i64)));

        assert_eq!(matching_ids(&table, &predicate)?, vec![2]);
        Ok(())
    }

    /// Only a recorded row count of exactly 0 may drop a file. A catalog that
    /// leaves `record_count` unset must never be read as "empty": that would
    /// withhold a file holding live rows, and a keyed mutation that never sees the
    /// file holding its key inserts a duplicate instead of superseding it, with no
    /// error anywhere. Pinned because the mis-implementation is a one-token slip
    /// (`max_row_count.unwrap_or(0) == 0`) that nothing else here would catch.
    ///
    /// File 2 carries the unknown count and is the only file whose bounds admit the
    /// key, so reading unknown as empty collapses this result to nothing.
    #[test]
    fn files_matching_keeps_a_file_whose_row_count_is_unknown() -> Result<()> {
        let table = fixed_table(
            vec![
                fixture_file(1, None, Some((1, 10))),
                file_with_unknown_row_count(2, (100, 110)),
                empty_file_without_statistics(3),
            ],
            None,
        )?;

        let predicate = physical_predicate(&table, col("id").eq(lit(105_i64)));

        assert_eq!(matching_ids(&table, &predicate)?, vec![2]);
        Ok(())
    }

    /// A NULL partition value yields no bound, so that file must survive a
    /// predicate on its own partition column. Exact bounds can still prune the
    /// files beside it.
    ///
    /// The control is
    /// `files_matching_prunes_a_partition_column_without_column_statistics`:
    /// files 1 and 2 alone, same predicate, returns file 1.
    #[test]
    fn files_matching_keeps_a_file_with_a_null_partition_value() -> Result<()> {
        let table = fixed_table(
            vec![
                fixture_file(1, Some(Some("10")), None),
                fixture_file(2, Some(Some("9999")), None),
                fixture_file(3, Some(None), None),
            ],
            Some(region_spec(true)),
        )?;

        let predicate = physical_predicate(&table, col("region").eq(lit(10_i64)));

        assert_eq!(matching_ids(&table, &predicate)?, vec![1, 3]);
        Ok(())
    }

    /// A partition value that does not decode to the column type yields no
    /// bound, with the same consequences as a NULL one. Deliberately kept apart
    /// from the NULL case so either mistake fails independently.
    #[test]
    fn files_matching_keeps_a_file_with_an_undecodable_partition_value() -> Result<()> {
        let table = fixed_table(
            vec![
                fixture_file(1, Some(Some("10")), None),
                fixture_file(2, Some(Some("9999")), None),
                fixture_file(3, Some(Some("not-a-region")), None),
            ],
            Some(region_spec(true)),
        )?;

        let predicate = physical_predicate(&table, col("region").eq(lit(10_i64)));

        assert_eq!(matching_ids(&table, &predicate)?, vec![1, 3]);
        Ok(())
    }

    /// A predicate on a non-partition column must never be pruned by partition
    /// bounds. Getting this wrong would drop a file that holds the key, and a
    /// keyed mutation would then insert a second copy of that key instead of
    /// superseding it — silently, with no error anywhere.
    ///
    /// `files_matching_prunes_a_partition_column_without_column_statistics` is the
    /// counterpart that proves partition bounds really are live in this fixture,
    /// so this test cannot pass merely because nothing prunes.
    #[test]
    fn files_matching_never_prunes_a_non_partition_predicate_by_partition_bounds() -> Result<()> {
        let table = fixed_table(
            vec![
                // Partition value far outside the predicate's range and no `id`
                // statistics: only a partition bound leaking onto `id` could drop it.
                fixture_file(1, Some(Some("9999")), None),
                // Same distant partition value, but `id` statistics that admit the
                // key — the file genuinely holds a matching row.
                fixture_file(2, Some(Some("9999")), Some((100, 110))),
            ],
            Some(region_spec(true)),
        )?;

        let predicate = physical_predicate(&table, col("id").eq(lit(105_i64)));

        assert_eq!(matching_ids(&table, &predicate)?, vec![1, 2]);
        Ok(())
    }

    #[test]
    fn files_matching_prunes_a_partition_column_without_column_statistics() -> Result<()> {
        let table = fixed_table(
            vec![
                fixture_file(1, Some(Some("10")), None),
                fixture_file(2, Some(Some("9999")), None),
            ],
            Some(region_spec(true)),
        )?;

        let predicate = physical_predicate(&table, col("region").eq(lit(10_i64)));

        assert_eq!(matching_ids(&table, &predicate)?, vec![1]);
        Ok(())
    }

    /// Same fixture and predicate as
    /// `files_matching_prunes_a_partition_column_without_column_statistics`, with
    /// the single difference that the spec is not prune-safe — the table has been
    /// re-partitioned, so a file's values may belong to a retired generation whose
    /// key order differs. Nothing may be dropped.
    #[test]
    fn files_matching_ignores_partition_bounds_when_the_spec_is_not_prune_safe() -> Result<()> {
        let table = fixed_table(
            vec![
                fixture_file(1, Some(Some("10")), None),
                fixture_file(2, Some(Some("9999")), None),
            ],
            Some(region_spec(false)),
        )?;

        let predicate = physical_predicate(&table, col("region").eq(lit(10_i64)));

        assert_eq!(matching_ids(&table, &predicate)?, vec![1, 2]);
        Ok(())
    }

    #[test]
    fn files_matching_applies_every_conjunct() -> Result<()> {
        let table = fixed_table(
            vec![
                fixture_file(1, Some(Some("10")), Some((100, 110))),
                // Excluded by the partition conjunct alone.
                fixture_file(2, Some(Some("9999")), Some((100, 110))),
                // Excluded by the key conjunct alone.
                fixture_file(3, Some(Some("10")), Some((1, 10))),
            ],
            Some(region_spec(true)),
        )?;

        let predicate = physical_predicate(
            &table,
            col("id")
                .eq(lit(105_i64))
                .and(col("region").eq(lit(10_i64))),
        );

        assert_eq!(matching_ids(&table, &predicate)?, vec![1]);
        Ok(())
    }

    #[test]
    fn files_matching_returns_every_file_for_a_trivially_true_predicate() -> Result<()> {
        let table = fixed_table(
            vec![
                fixture_file(1, None, Some((1, 10))),
                fixture_file(2, None, Some((100, 110))),
                fixture_file(3, None, None),
            ],
            None,
        )?;

        let predicate = physical_predicate(&table, lit(true));

        assert_eq!(matching_ids(&table, &predicate)?, vec![1, 2, 3]);
        Ok(())
    }

    /// A million-file table read in bounded pages, with one match in the first
    /// page and one in the last: a result assembled from a single page, or from
    /// an eager whole-table file read, cannot produce both.
    #[test]
    fn files_matching_prunes_across_every_metadata_page() -> Result<()> {
        let provider = Arc::new(LazyMillionFileProvider::default());
        let table = DuckLakeTable::new(
            1,
            "events",
            provider.clone(),
            1,
            Arc::new(ObjectStoreUrl::parse("memory://").unwrap()),
            String::new(),
        )?;

        let predicate = physical_predicate(
            &table,
            col("id").eq(lit(5_i64)).or(col("id").eq(lit(999_999_i64))),
        );

        assert_eq!(matching_ids(&table, &predicate)?, vec![5, 999_999]);
        assert!(provider.page_calls.load(Ordering::Relaxed) > 1);
        assert_eq!(provider.eager_file_reads.load(Ordering::Relaxed), 0);
        Ok(())
    }

    #[test]
    fn summary_bounds_with_live_deletes_remain_inexact() -> Result<()> {
        let columns =
            vec![DuckLakeTableColumn::new(1, "id".to_string(), "integer".to_string(), false)];
        let schema = build_arrow_schema(&columns)?;
        let (statistics, _) = build_datafusion_statistics(
            &schema,
            &columns,
            &[],
            DuckLakeStatistics {
                columns: vec![DuckLakeTableColumnStatistics {
                    column_id: 1,
                    contains_null: Some(false),
                    min_value: Some("0".to_string()),
                    max_value: Some("9".to_string()),
                    contains_nan: None,
                    column_size_bytes: Some(40),
                    bounds_are_exact: false,
                }],
                ..Default::default()
            },
            true,
            false,
        );

        let column = &statistics.column_statistics[0];
        assert_eq!(
            column.min_value,
            Precision::Inexact(ScalarValue::Int32(Some(0)))
        );
        assert_eq!(
            column.max_value,
            Precision::Inexact(ScalarValue::Int32(Some(9)))
        );
        assert_eq!(column.byte_size, Precision::Inexact(40));
        Ok(())
    }

    #[test]
    fn float_file_max_gated_by_contains_nan() -> Result<()> {
        let columns =
            vec![DuckLakeTableColumn::new(1, "x".to_string(), "double".to_string(), false)];
        let schema = build_arrow_schema(&columns)?;
        let mut file =
            DuckLakeTableFile::new(DuckLakeFileData::new("f.parquet".to_string(), true, 1));
        file.data_file_id = 7;

        for (contains_nan, max_usable) in [(None, false), (Some(true), false), (Some(false), true)]
        {
            let (table_stats, file_stats) = build_datafusion_statistics(
                &schema,
                &columns,
                std::slice::from_ref(&file),
                DuckLakeStatistics {
                    files: vec![DuckLakeFileColumnStatistics {
                        data_file_id: 7,
                        column_id: 1,
                        column_size_bytes: Some(16),
                        value_count: Some(2),
                        null_count: Some(0),
                        min_value: Some("1.0".to_string()),
                        max_value: Some("2.0".to_string()),
                        contains_nan,
                    }],
                    ..Default::default()
                },
                false,
                true,
            );

            // Neither bound survives an unknown/positive NaN state: DataFusion
            // orders floats with `total_cmp`, under which negative NaN sits
            // below every value (below -Infinity included), so a recorded min is
            // no more trustworthy than a recorded max. `contains_nan` does not
            // record the sign, so both must go.
            let per_file = &file_stats[&7].column_statistics[0];
            let table_col = &table_stats.column_statistics[0];
            let expected_min = if contains_nan == Some(false) {
                Precision::Exact(ScalarValue::Float64(Some(1.0)))
            } else {
                Precision::Absent
            };
            assert_eq!(
                per_file.min_value, expected_min,
                "contains_nan={contains_nan:?}"
            );
            assert_eq!(
                table_col.min_value, expected_min,
                "contains_nan={contains_nan:?}"
            );

            let expected_max = if max_usable {
                Precision::Exact(ScalarValue::Float64(Some(2.0)))
            } else {
                Precision::Absent
            };
            assert_eq!(
                per_file.max_value, expected_max,
                "contains_nan={contains_nan:?}"
            );
            assert_eq!(
                table_col.max_value, expected_max,
                "contains_nan={contains_nan:?}"
            );
        }
        Ok(())
    }

    #[test]
    fn float_rollup_max_gated_by_contains_nan() -> Result<()> {
        let columns =
            vec![DuckLakeTableColumn::new(1, "x".to_string(), "double".to_string(), false)];
        let schema = build_arrow_schema(&columns)?;

        for (contains_nan, max_usable) in [(None, false), (Some(true), false), (Some(false), true)]
        {
            let (statistics, _) = build_datafusion_statistics(
                &schema,
                &columns,
                &[],
                DuckLakeStatistics {
                    columns: vec![DuckLakeTableColumnStatistics {
                        column_id: 1,
                        contains_null: Some(false),
                        min_value: Some("1.0".to_string()),
                        max_value: Some("2.0".to_string()),
                        contains_nan,
                        column_size_bytes: Some(16),
                        bounds_are_exact: true,
                    }],
                    ..Default::default()
                },
                true,
                false,
            );

            let column = &statistics.column_statistics[0];
            // Both bounds are gated: negative NaN sits below a recorded min just
            // as positive NaN sits above a recorded max, and `contains_nan`
            // does not record the sign.
            let expected_min = if max_usable {
                Precision::Exact(ScalarValue::Float64(Some(1.0)))
            } else {
                Precision::Absent
            };
            assert_eq!(
                column.min_value, expected_min,
                "contains_nan={contains_nan:?}"
            );
            let expected_max = if max_usable {
                Precision::Exact(ScalarValue::Float64(Some(2.0)))
            } else {
                Precision::Absent
            };
            assert_eq!(
                column.max_value, expected_max,
                "contains_nan={contains_nan:?}"
            );
        }
        Ok(())
    }

    #[test]
    fn float_table_aggregate_max_needs_all_files_nan_free() -> Result<()> {
        let columns =
            vec![DuckLakeTableColumn::new(1, "x".to_string(), "double".to_string(), false)];
        let schema = build_arrow_schema(&columns)?;
        let mut file_a =
            DuckLakeTableFile::new(DuckLakeFileData::new("a.parquet".to_string(), true, 1));
        file_a.data_file_id = 1;
        let mut file_b =
            DuckLakeTableFile::new(DuckLakeFileData::new("b.parquet".to_string(), true, 1));
        file_b.data_file_id = 2;
        let table_files = vec![file_a, file_b];

        let stat =
            |data_file_id, min: &str, max: &str, contains_nan| DuckLakeFileColumnStatistics {
                data_file_id,
                column_id: 1,
                column_size_bytes: Some(16),
                value_count: Some(2),
                null_count: Some(0),
                min_value: Some(min.to_string()),
                max_value: Some(max.to_string()),
                contains_nan,
            };
        let build = |nan_b| {
            build_datafusion_statistics(
                &schema,
                &columns,
                &table_files,
                DuckLakeStatistics {
                    files: vec![stat(1, "1.0", "2.0", Some(false)), stat(2, "0.5", "3.0", nan_b)],
                    ..Default::default()
                },
                false,
                true,
            )
            .0
        };

        // Both files known NaN-free: bounds fold across the files.
        let statistics = build(Some(false));
        assert_eq!(
            statistics.column_statistics[0].min_value,
            Precision::Exact(ScalarValue::Float64(Some(0.5)))
        );
        assert_eq!(
            statistics.column_statistics[0].max_value,
            Precision::Exact(ScalarValue::Float64(Some(3.0)))
        );

        // One file NaN-unknown: NEITHER of its bounds is trusted, so both
        // aggregates degrade to unknown. A negative NaN in that file would sit
        // below the folded min exactly as a positive one would sit above the
        // folded max.
        let statistics = build(None);
        assert_eq!(statistics.column_statistics[0].min_value, Precision::Absent);
        assert_eq!(statistics.column_statistics[0].max_value, Precision::Absent);
        Ok(())
    }

    #[test]
    fn test_validated_file_size_positive() {
        assert_eq!(validated_file_size(0, "test.parquet").unwrap(), 0);
        assert_eq!(validated_file_size(1024, "test.parquet").unwrap(), 1024);
        assert_eq!(
            validated_file_size(i64::MAX, "test.parquet").unwrap(),
            i64::MAX as u64
        );
    }

    #[test]
    fn test_validated_file_size_negative() {
        let err = validated_file_size(-1, "data/test.parquet").unwrap_err();
        let msg = err.to_string();
        assert!(
            msg.contains("-1"),
            "Error should contain the negative value: {}",
            msg
        );
        assert!(
            msg.contains("data/test.parquet"),
            "Error should contain the file path: {}",
            msg
        );
    }

    #[test]
    fn test_validated_file_size_large_negative() {
        let err = validated_file_size(i64::MIN, "bad.parquet").unwrap_err();
        let msg = err.to_string();
        assert!(msg.contains("bad.parquet"));
        assert!(msg.contains(&i64::MIN.to_string()));
    }

    #[test]
    fn test_validated_record_count_positive() {
        assert_eq!(validated_record_count(0, "test.parquet").unwrap(), 0);
        assert_eq!(validated_record_count(100, "test.parquet").unwrap(), 100);
        assert_eq!(
            validated_record_count(i64::MAX, "test.parquet").unwrap(),
            i64::MAX as u64
        );
    }

    #[test]
    fn test_validated_record_count_negative() {
        let err = validated_record_count(-1, "data/test.parquet").unwrap_err();
        let msg = err.to_string();
        assert!(
            msg.contains("-1"),
            "Error should contain the negative value: {}",
            msg
        );
        assert!(
            msg.contains("data/test.parquet"),
            "Error should contain the file path: {}",
            msg
        );
        assert!(
            msg.contains("record_count"),
            "Error should mention record_count: {}",
            msg
        );
    }

    #[test]
    fn test_validated_record_count_large_negative() {
        let err = validated_record_count(i64::MIN, "bad.parquet").unwrap_err();
        let msg = err.to_string();
        assert!(msg.contains("bad.parquet"));
        assert!(msg.contains(&i64::MIN.to_string()));
    }

    #[test]
    fn test_parse_ducklake_statistic_encodings() {
        let boolean = DuckLakeTableColumn::new(1, "flag".to_string(), "boolean".to_string(), true);
        assert_eq!(
            parse_statistic_scalar("1", &boolean, &DataType::Boolean),
            Some(ScalarValue::Boolean(Some(true)))
        );
        assert_eq!(
            parse_statistic_scalar("FALSE", &boolean, &DataType::Boolean),
            Some(ScalarValue::Boolean(Some(false)))
        );

        let blob = DuckLakeTableColumn::new(2, "bytes".to_string(), "blob".to_string(), true);
        assert_eq!(
            parse_statistic_scalar("68656C6C6F", &blob, &DataType::BinaryView),
            Some(ScalarValue::BinaryView(Some(b"hello".to_vec())))
        );
        assert_eq!(
            parse_statistic_scalar("\\x68656C6C6F", &blob, &DataType::BinaryView),
            Some(ScalarValue::BinaryView(Some(b"hello".to_vec())))
        );

        let uuid = DuckLakeTableColumn::new(3, "id".to_string(), "uuid".to_string(), true);
        assert_eq!(
            parse_statistic_scalar(
                "550e8400-e29b-41d4-a716-446655440000",
                &uuid,
                &DataType::FixedSizeBinary(16),
            ),
            Some(ScalarValue::FixedSizeBinary(
                16,
                Some(vec![
                    0x55, 0x0e, 0x84, 0x00, 0xe2, 0x9b, 0x41, 0xd4, 0xa7, 0x16, 0x44, 0x66, 0x55,
                    0x44, 0x00, 0x00,
                ]),
            ))
        );

        let decimal =
            DuckLakeTableColumn::new(4, "amount".to_string(), "decimal(10,2)".to_string(), true);
        assert_eq!(
            parse_statistic_scalar("123.45", &decimal, &DataType::Decimal128(10, 2)),
            Some(ScalarValue::Decimal128(Some(12_345), 10, 2))
        );
    }

    #[test]
    fn expression_column_default_does_not_block_table_reads() {
        let column =
            DuckLakeTableColumn::new(1, "created_at".to_string(), "timestamp".to_string(), false)
                .with_defaults(
                    None,
                    Some("current_timestamp".to_string()),
                    Some("expression".to_string()),
                    Some("duckdb".to_string()),
                );

        assert!(validate_column_defaults(&[column]).unwrap().is_empty());
    }

    #[test]
    fn null_default_sentinel_does_not_create_an_operational_default() {
        let column = DuckLakeTableColumn::new(1, "id".to_string(), "int32".to_string(), false)
            .with_defaults(
                None,
                Some("NULL".to_string()),
                Some("literal".to_string()),
                Some("duckdb".to_string()),
            );

        assert!(column.default_value.is_none());
        assert!(validate_column_defaults(&[column]).unwrap().is_empty());
    }

    // `sort_ordering_for` is `#[cfg(feature = "write")]`, so the test that exercises
    // it needs the same gate — otherwise a read-only feature combination fails to
    // compile the test target.
    #[cfg(feature = "write")]
    #[test]
    fn sort_ordering_rejects_expression_sort_key() {
        let schema = Schema::new(vec![Field::new("id", DataType::Int64, false)]);
        let sort_spec = crate::sort::SortSpec {
            sort_id: 9,
            fields: vec![crate::sort::SortField {
                sort_key_index: 0,
                expression: "lower(id)".to_string(),
                dialect: crate::sort::DUCKDB_DIALECT.to_string(),
                direction: crate::sort::SortDirection::Asc,
                null_order: crate::sort::NullOrder::NullsLast,
            }],
        };

        let err = sort_ordering_for(&schema, Some(&sort_spec)).unwrap_err();

        assert_eq!(
            err.to_string(),
            "This feature is not implemented: DuckLake sort order 9 contains an unsupported \
             expression; datafusion-ducklake can write only bare-column sort keys",
        );
    }

    // --- #1323: the DuckLake scan attaches the footer metadata cache ---

    /// Real parquet bytes for a single row group matching `fixed_table`'s
    /// `id`/`region` (Int64, Int64) schema, so a scan can actually read a
    /// footer instead of only exercising catalog-side planning.
    fn footer_cache_fixture_bytes() -> Vec<u8> {
        let schema = Arc::new(Schema::new(vec![
            Field::new("id", DataType::Int64, false),
            Field::new("region", DataType::Int64, false),
        ]));
        let batch = RecordBatch::try_new(
            Arc::clone(&schema),
            vec![
                Arc::new(Int64Array::from((0..10).collect::<Vec<i64>>())) as ArrayRef,
                Arc::new(Int64Array::from(vec![1i64; 10])) as ArrayRef,
            ],
        )
        .expect("build fixture batch");
        let mut buf = Vec::new();
        {
            let mut writer =
                parquet::arrow::ArrowWriter::try_new(&mut buf, schema, None).expect("open writer");
            writer.write(&batch).expect("write batch");
            writer.close().expect("close writer");
        }
        buf
    }

    /// An `ObjectStore` wrapper that counts range reads whose end lands
    /// exactly on `file_len` — the footer/page-index suffix a cold
    /// `DFParquetMetadata::fetch_metadata_from_store` fetches
    /// (`datafusion-datasource-parquet`'s `metadata.rs`), which is the read a
    /// warm [`CachedParquetFileReaderFactory`] must not repeat. Covers every
    /// method a footer fetch could arrive through: `get_ranges` directly
    /// (`DFParquetMetadata`'s own call, and the only one this crate's footer
    /// reads use now that both the scan and `file_schema_mapping` route
    /// through it — #1323), `get_opts` (a single bounded range there would
    /// also qualify), and `get_range`, whose `ObjectStoreExt` default
    /// implementation calls `get_opts` on `self` and so is covered by the
    /// same override. Delegates every other `ObjectStore` method to `inner`
    /// untouched.
    #[derive(Debug)]
    struct FooterCountingStore {
        inner: Arc<dyn object_store::ObjectStore>,
        file_len: u64,
        footer_reads: AtomicUsize,
    }

    impl FooterCountingStore {
        fn new(inner: Arc<dyn object_store::ObjectStore>, file_len: u64) -> Self {
            Self {
                inner,
                file_len,
                footer_reads: AtomicUsize::new(0),
            }
        }

        fn footer_reads(&self) -> usize {
            self.footer_reads.load(Ordering::SeqCst)
        }

        fn count_footer_ranges(&self, ranges: &[std::ops::Range<u64>]) {
            for range in ranges {
                if range.end == self.file_len {
                    self.footer_reads.fetch_add(1, Ordering::SeqCst);
                }
            }
        }
    }

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

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

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

        async fn get_opts(
            &self,
            location: &ObjectPath,
            options: object_store::GetOptions,
        ) -> object_store::Result<object_store::GetResult> {
            // Not exercised by `DFParquetMetadata` today (it always calls
            // `get_ranges` below, cold or warm, execution-time or plan-time —
            // see the struct doc), but counted for completeness: `get_range`
            // (built on this) and any future reader that fetches the footer
            // as a single `get_opts` range would otherwise go uncounted.
            if let Some(range) = &options.range
                && let Ok(resolved) = range.as_range(self.file_len)
                && resolved.end == self.file_len
            {
                self.footer_reads.fetch_add(1, Ordering::SeqCst);
            }
            self.inner.get_opts(location, options).await
        }

        async fn get_ranges(
            &self,
            location: &ObjectPath,
            ranges: &[std::ops::Range<u64>],
        ) -> object_store::Result<Vec<bytes::Bytes>> {
            self.count_footer_ranges(ranges);
            self.inner.get_ranges(location, ranges).await
        }

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

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

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

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

    /// `create_parquet_source` must attach a [`CachedParquetFileReaderFactory`]
    /// — reading through `state`'s shared file-metadata cache — rather than
    /// leaving DataFusion to fall back to the uncached
    /// `DefaultParquetFileReaderFactory` (#1323). Builds a real
    /// `DataSourceExec`/`FileScanConfig` scan plan, exactly as a caller of
    /// `create_parquet_source` does, and downcasts down to the `ParquetSource`
    /// to inspect the attached factory.
    // Reads the table's object-store URL, which is only compiled with `write`.
    #[cfg(feature = "write")]
    #[tokio::test]
    async fn ducklake_scan_attaches_the_cached_parquet_reader_factory() -> Result<()> {
        let table = fixed_table(vec![], None)?;

        let store: Arc<dyn object_store::ObjectStore> =
            Arc::new(object_store::memory::InMemory::new());
        let store_url = url::Url::parse(table.object_store_url().as_str()).expect("valid url");
        let ctx = SessionContext::new();
        ctx.runtime_env().register_object_store(&store_url, store);
        let state = ctx.state();

        let source = table.create_parquet_source(&state, Arc::clone(&table.physical_schema))?;
        let plan: Arc<dyn ExecutionPlan> = DataSourceExec::from_data_source(
            table
                .scan_config_builder(Arc::new(source))
                .with_file_group(FileGroup::new(vec![]))
                .build(),
        );

        let found_source = plan
            .downcast_ref::<DataSourceExec>()
            .and_then(|exec| exec.data_source().downcast_ref::<FileScanConfig>())
            .and_then(|config| config.file_source().downcast_ref::<ParquetSource>())
            .expect("plan is a DataSourceExec wrapping a parquet FileScanConfig");

        let factory = found_source
            .parquet_file_reader_factory()
            .expect("a query-time ParquetSource must attach a reader factory (#1323)");
        let debug = format!("{factory:?}");
        assert!(
            debug.contains("CachedParquetFileReaderFactory"),
            "expected the metadata-cache-backed reader factory, got: {debug}",
        );

        Ok(())
    }

    /// End-to-end: scanning the same file twice on one `SessionContext` must
    /// issue the footer/page-index read only on the cold run. The warm run's
    /// `ParquetSource` reads through the same `RuntimeEnv` file-metadata cache
    /// the cold run just populated (#1323), so it performs zero range reads
    /// whose end lands on the file's length.
    // Reads the table's object-store URL, which is only compiled with `write`.
    #[cfg(feature = "write")]
    #[tokio::test]
    async fn ducklake_parquet_source_reuses_cached_footer_metadata_across_scans() -> Result<()> {
        let table = fixed_table(vec![], None)?;

        let bytes = footer_cache_fixture_bytes();
        let file_len = bytes.len() as u64;

        let inner: Arc<dyn object_store::ObjectStore> =
            Arc::new(object_store::memory::InMemory::new());
        let object_path = ObjectPath::from("footer-cache.parquet");
        inner
            .put(&object_path, bytes.into())
            .await
            .expect("seed the fixture file");
        let counting = Arc::new(FooterCountingStore::new(inner, file_len));

        let store_url = url::Url::parse(table.object_store_url().as_str()).expect("valid url");
        let ctx = SessionContext::new();
        ctx.runtime_env()
            .register_object_store(&store_url, Arc::clone(&counting) as Arc<dyn ObjectStore>);
        let state = ctx.state();

        let build_plan = |state: &datafusion::execution::SessionState| -> DataFusionResult<Arc<dyn ExecutionPlan>> {
            let source = table.create_parquet_source(state, Arc::clone(&table.physical_schema))?;
            let pf = PartitionedFile::new("footer-cache.parquet", file_len);
            Ok(DataSourceExec::from_data_source(
                table
                    .scan_config_builder(Arc::new(source))
                    .with_file_group(FileGroup::new(vec![pf]))
                    .build(),
            ))
        };

        let plan1 = build_plan(&state)?;
        let batches1 = datafusion::physical_plan::collect(plan1, state.task_ctx())
            .await
            .expect("first scan executes");
        assert_eq!(
            batches1.iter().map(|b| b.num_rows()).sum::<usize>(),
            10,
            "sanity: the fixture file's rows come back"
        );
        let footer_reads_after_cold_scan = counting.footer_reads();
        assert!(
            footer_reads_after_cold_scan >= 1,
            "the cold scan must read the footer at least once"
        );

        let plan2 = build_plan(&state)?;
        let batches2 = datafusion::physical_plan::collect(plan2, state.task_ctx())
            .await
            .expect("second scan executes");
        assert_eq!(batches2.iter().map(|b| b.num_rows()).sum::<usize>(), 10);

        assert_eq!(
            counting.footer_reads(),
            footer_reads_after_cold_scan,
            "a warm scan must issue zero additional footer reads once the reader factory \
             shares the session's metadata cache (#1323)",
        );

        Ok(())
    }
}