skardi 0.5.0

High performance query engine for both offline compute and online serving
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
//! Amazon DynamoDB table provider for DataFusion.
//!
//! DynamoDB is a managed NoSQL document/key-value store. A DynamoDB table maps
//! to one Skardi table: each item becomes a row and each top-level attribute a
//! column. The shape mirrors the MongoDB provider (`super::mongo`) — a sample
//! item drives schema inference, binary filters push down into the backend, and
//! INSERT/UPDATE/DELETE are supported because DynamoDB is a read-write store.
//!
//! DynamoDB has no native full-text or vector search, so no FTS/KNN table
//! functions are registered (unlike Lance or the pgvector-backed Postgres
//! provider).

use std::any::Any;
use std::collections::{HashMap, HashSet};
use std::fmt::{self, Debug, Formatter};
use std::sync::Arc;

use anyhow::{Context, Result};
use arrow::array::{
    ArrayRef, BooleanArray, Float64Array, Int32Array, Int64Array, RecordBatch, RecordBatchOptions,
    StringArray, UInt64Array,
};
use arrow::datatypes::{DataType, Field, Schema, SchemaRef};
use async_trait::async_trait;
use aws_sdk_dynamodb::Client;
use aws_sdk_dynamodb::config::{Credentials, Region};
use aws_sdk_dynamodb::types::{
    AttributeValue, DeleteRequest, KeyType, PutRequest, ScalarAttributeType, Select, WriteRequest,
};
use datafusion::catalog::Session;
use datafusion::datasource::{TableProvider, TableType};
use datafusion::error::{DataFusionError, Result as DFResult};
use datafusion::logical_expr::{Expr, Operator, TableProviderFilterPushDown};
use datafusion::physical_expr::EquivalenceProperties;
use datafusion::physical_plan::execution_plan::{Boundedness, EmissionType};
use datafusion::physical_plan::stream::RecordBatchStreamAdapter;
use datafusion::physical_plan::{
    DisplayAs, DisplayFormatType, ExecutionPlan, Partitioning, PlanProperties,
    SendableRecordBatchStream,
};
use datafusion::prelude::SessionContext;
use futures::StreamExt;
use futures::stream::{self, Stream};

use super::is_pushable_binary_filter;
use crate::sources::DataSourceType;
use crate::sources::hierarchy::{
    HierarchyLevel, SourceLabel, build_catalog_best_effort, build_catalog_with_required_schemas,
    retry_with_timeout,
};

/// Schema name used for DynamoDB catalog mode. DynamoDB has no native schema/database
/// layer; all tables live under a single endpoint/region. We expose them under a fixed
/// schema so SQL references are consistently three-part: `catalog.tables.<table>`.
const DYNAMODB_CATALOG_SCHEMA: &str = "tables";

/// Maximum items sampled to infer a schema when no explicit `columns` option is
/// given. Merging several items (rather than one) makes the inferred column set
/// deterministic and complete enough for most tables.
const SCHEMA_SAMPLE_SIZE: i32 = 50;

/// DynamoDB `BatchWriteItem` accepts at most 25 write requests per call.
const BATCH_WRITE_CHUNK: usize = 25;

/// Maximum tables returned per `ListTables` page. AWS allows up to 100; we use the
/// default and follow `last_evaluated_table_name` when present.
const LIST_TABLES_PAGE_SIZE: i32 = 100;

const DYNAMODB_CATALOG_CONFLICT_OPTIONS: &[&str] =
    &["table", "schema", "partition_key", "sort_key", "columns"];

#[derive(Clone, Debug, PartialEq)]
struct DynamoKeySchema {
    partition_key: String,
    partition_type: DataType,
    sort_key: Option<String>,
    sort_type: Option<DataType>,
}

impl DynamoKeySchema {
    fn new(
        partition_key: String,
        partition_type: DataType,
        sort_key: Option<String>,
        sort_type: Option<DataType>,
    ) -> Self {
        Self {
            partition_key,
            partition_type,
            sort_key,
            sort_type,
        }
    }

    fn fallback(partition_key: String, sort_key: Option<String>) -> Self {
        Self::new(partition_key, DataType::Utf8, sort_key, None)
    }
}

/// A DynamoDB table exposed to DataFusion as a `TableProvider`.
pub struct DynamoTableProvider {
    client: Client,
    table_name: String,
    schema: SchemaRef,
    /// Partition (hash) key attribute name. Always present and not nullable.
    partition_key: String,
    /// Sort (range) key attribute name, if the table has a composite key.
    sort_key: Option<String>,
    /// When false, INSERT/UPDATE/DELETE are rejected at plan time so a
    /// `read_only` source can never mutate a live table.
    read_write: bool,
}

impl Debug for DynamoTableProvider {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        f.debug_struct("DynamoTableProvider")
            .field("table_name", &self.table_name)
            .field("partition_key", &self.partition_key)
            .field("sort_key", &self.sort_key)
            .field("read_write", &self.read_write)
            .field("schema", &self.schema)
            .finish()
    }
}

impl DynamoTableProvider {
    /// Build a provider against an existing DynamoDB table, inferring the schema
    /// from sampled items unless one is supplied. `read_write` gates the DML
    /// methods; a read-only provider rejects INSERT/UPDATE/DELETE at plan time.
    pub async fn new(
        client: Client,
        table_name: &str,
        partition_key: &str,
        sort_key: Option<&str>,
        schema: Option<SchemaRef>,
        read_write: bool,
    ) -> Result<Self> {
        Self::new_with_key_types(
            client,
            table_name,
            partition_key,
            None,
            sort_key,
            None,
            schema,
            read_write,
        )
        .await
    }

    async fn new_with_key_types(
        client: Client,
        table_name: &str,
        partition_key: &str,
        partition_key_type: Option<DataType>,
        sort_key: Option<&str>,
        sort_key_type: Option<DataType>,
        schema: Option<SchemaRef>,
        read_write: bool,
    ) -> Result<Self> {
        let schema = match schema {
            Some(s) => s,
            None => Arc::new(
                Self::infer_schema(
                    &client,
                    table_name,
                    partition_key,
                    partition_key_type,
                    sort_key,
                    sort_key_type,
                )
                .await?,
            ),
        };

        Ok(Self {
            client,
            table_name: table_name.to_string(),
            schema,
            partition_key: partition_key.to_string(),
            sort_key: sort_key.map(str::to_string),
            read_write,
        })
    }

    /// Infer an Arrow schema by sampling several items and merging their
    /// attribute sets. The partition key (then the sort key, if any) are always
    /// emitted first and non-nullable; remaining attributes are inferred from
    /// the samples and marked nullable, since DynamoDB items are schemaless and
    /// any attribute may be absent on other items.
    async fn infer_schema(
        client: &Client,
        table_name: &str,
        partition_key: &str,
        partition_key_type: Option<DataType>,
        sort_key: Option<&str>,
        sort_key_type: Option<DataType>,
    ) -> Result<Schema> {
        let sample = client
            .scan()
            .table_name(table_name)
            .limit(SCHEMA_SAMPLE_SIZE)
            .send()
            .await
            .with_context(|| format!("Failed to sample DynamoDB table '{table_name}'"))?;

        let items = sample.items();
        if items.is_empty() {
            tracing::warn!(
                table = %table_name,
                "DynamoDB table is empty; schema limited to declared key attributes"
            );
        }

        let mut attrs = Vec::new();
        if let Some(dtype) = partition_key_type {
            attrs.push((partition_key.to_string(), dtype));
        }
        if let (Some(sort_key), Some(dtype)) = (sort_key, sort_key_type) {
            attrs.push((sort_key.to_string(), dtype));
        }
        let mut sampled_attrs = merge_sampled_attributes(items);
        sampled_attrs.retain(|(name, _)| {
            name != partition_key && sort_key.map(|sk| name != sk).unwrap_or(true)
        });
        attrs.extend(sampled_attrs);

        Ok(build_schema_fields(partition_key, sort_key, &attrs))
    }

    /// Pick the cheapest physical read strategy the pushable predicates allow.
    fn plan_read(&self, pushable: &[Expr]) -> DFResult<DynamoRead> {
        classify_read(&self.partition_key, self.sort_key.as_deref(), pushable)
    }
}

/// Merge attributes across sampled items; the first observation of an
/// attribute fixes its type. The SDK item maps iterate in arbitrary order, so
/// the merged list is sorted by name to give a deterministic non-key column
/// order across runs (keys are placed explicitly downstream).
fn merge_sampled_attributes(items: &[HashMap<String, AttributeValue>]) -> Vec<(String, DataType)> {
    let mut attrs: Vec<(String, DataType)> = Vec::new();
    let mut seen: HashSet<String> = HashSet::new();
    for item in items {
        for (key, value) in item.iter() {
            if seen.insert(key.clone()) {
                attrs.push((key.clone(), attribute_value_to_arrow_type(value)));
            }
        }
    }
    attrs.sort_by(|(a, _), (b, _)| a.cmp(b));
    attrs
}

/// Build an ordered field list: key attributes first (non-nullable, in key
/// order), then every other attribute (nullable). Shared by schema inference
/// and the explicit `columns` option so both produce identical shapes.
fn build_schema_fields(
    partition_key: &str,
    sort_key: Option<&str>,
    attrs: &[(String, DataType)],
) -> Schema {
    let type_of = |name: &str| {
        attrs
            .iter()
            .find(|(n, _)| n == name)
            .map(|(_, t)| t.clone())
            .unwrap_or(DataType::Utf8)
    };

    let mut fields: Vec<Field> = Vec::new();
    let mut seen: HashSet<&str> = HashSet::new();

    fields.push(Field::new(partition_key, type_of(partition_key), false));
    seen.insert(partition_key);
    if let Some(sk) = sort_key {
        fields.push(Field::new(sk, type_of(sk), false));
        seen.insert(sk);
    }
    for (name, dtype) in attrs {
        if seen.insert(name.as_str()) {
            fields.push(Field::new(name, dtype.clone(), true));
        }
    }
    Schema::new(fields)
}

/// Convert a page of DynamoDB items into an Arrow `RecordBatch` shaped by
/// `schema`. Consumes the items (attribute values are moved, not cloned) and
/// fills missing attributes with NULL.
fn items_to_batch(
    items: Vec<HashMap<String, AttributeValue>>,
    schema: &SchemaRef,
) -> DFResult<RecordBatch> {
    let n_rows = items.len();
    let mut columns: Vec<Vec<Option<AttributeValue>>> = schema
        .fields()
        .iter()
        .map(|_| Vec::with_capacity(n_rows))
        .collect();

    for mut item in items {
        for (idx, field) in schema.fields().iter().enumerate() {
            columns[idx].push(item.remove(field.name()));
        }
    }

    let arrays: Vec<ArrayRef> = schema
        .fields()
        .iter()
        .zip(columns.iter())
        .map(|(field, values)| attribute_values_to_arrow_array(values, field.data_type()))
        .collect();

    let options = RecordBatchOptions::new().with_row_count(Some(n_rows));
    RecordBatch::try_new_with_options(schema.clone(), arrays, &options)
        .map_err(DataFusionError::from)
}

#[async_trait]
impl TableProvider for DynamoTableProvider {
    fn as_any(&self) -> &dyn Any {
        self
    }

    fn schema(&self) -> SchemaRef {
        self.schema.clone()
    }

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

    fn supports_filters_pushdown(
        &self,
        filters: &[&Expr],
    ) -> DFResult<Vec<TableProviderFilterPushDown>> {
        Ok(filters
            .iter()
            .map(|expr| {
                if is_pushable_binary_filter(expr) {
                    // Inexact (not Exact) keeps the filter in the logical plan so
                    // DataFusion's UPDATE/DELETE planner can still hand it to
                    // delete_from/update — matching the MongoDB provider.
                    TableProviderFilterPushDown::Inexact
                } else {
                    TableProviderFilterPushDown::Unsupported
                }
            })
            .collect())
    }

    async fn scan(
        &self,
        _state: &dyn Session,
        projection: Option<&Vec<usize>>,
        filters: &[Expr],
        limit: Option<usize>,
    ) -> DFResult<Arc<dyn ExecutionPlan>> {
        // Only push filters we can actually convert. A pushable-shaped predicate
        // with an inconvertible literal (e.g. a Timestamp) is skipped rather than
        // failing the whole query — safe because pushdown is Inexact, so
        // DataFusion re-applies every predicate after the fetch.
        let pushable: Vec<Expr> = filters
            .iter()
            .filter(|e| is_convertible_pushdown(e))
            .cloned()
            .collect();
        let read = self.plan_read(&pushable)?;

        // Empty projection (e.g. `count(*)`) means nothing above the scan
        // references any column — including filters, so there are none — and we
        // can read counts server-side. A non-empty projection is fetched with a
        // ProjectionExpression so only those attributes cross the wire.
        let (output_schema, projection_expr, count_only) = match projection {
            Some(p) if p.is_empty() => (Arc::new(self.schema.project(&[])?), None, true),
            Some(p) => {
                let ps = Arc::new(self.schema.project(p)?);
                let pe = build_projection_expression(&ps);
                (ps, Some(pe), false)
            }
            None => {
                let pe = build_projection_expression(&self.schema);
                (self.schema.clone(), Some(pe), false)
            }
        };

        let properties = PlanProperties::new(
            EquivalenceProperties::new(output_schema.clone()),
            Partitioning::UnknownPartitioning(1),
            EmissionType::Incremental,
            Boundedness::Bounded,
        );
        Ok(Arc::new(DynamoScanExec {
            client: self.client.clone(),
            table_name: self.table_name.clone(),
            output_schema,
            read,
            projection_expr,
            count_only,
            limit,
            properties,
        }))
    }

    async fn insert_into(
        &self,
        _state: &dyn Session,
        input: Arc<dyn ExecutionPlan>,
        insert_op: datafusion::logical_expr::dml::InsertOp,
    ) -> DFResult<Arc<dyn ExecutionPlan>> {
        if !self.read_write {
            return Err(read_only_error("INSERT", &self.table_name));
        }
        use datafusion::logical_expr::dml::InsertOp;
        // Plain INSERT (Append) must not clobber an existing item — DynamoDB
        // PutItem is an upsert, so a duplicate key would silently replace the
        // whole item. Overwrite/Replace opt into upsert semantics.
        let upsert = matches!(insert_op, InsertOp::Overwrite | InsertOp::Replace);
        Ok(Arc::new(DynamoInsertExec {
            input,
            client: self.client.clone(),
            table_name: self.table_name.clone(),
            schema: self.schema.clone(),
            properties: count_plan_properties(),
            partition_key: self.partition_key.clone(),
            upsert,
        }))
    }

    async fn delete_from(
        &self,
        _state: &dyn Session,
        filters: Vec<Expr>,
    ) -> DFResult<Arc<dyn ExecutionPlan>> {
        if !self.read_write {
            return Err(read_only_error("DELETE", &self.table_name));
        }
        let plan = self.plan_dml(&filters)?;
        Ok(Arc::new(DynamoDmlExec::new(
            self.clone_handle(),
            DynamoDmlOp::Delete { plan },
        )))
    }

    async fn update(
        &self,
        _state: &dyn Session,
        assignments: Vec<(String, Expr)>,
        filters: Vec<Expr>,
    ) -> DFResult<Arc<dyn ExecutionPlan>> {
        if !self.read_write {
            return Err(read_only_error("UPDATE", &self.table_name));
        }
        if assignments.is_empty() {
            return Err(DataFusionError::Plan(
                "UPDATE requires at least one assignment".to_string(),
            ));
        }
        let mut sets: Vec<(String, AttributeValue)> = Vec::with_capacity(assignments.len());
        for (col, expr) in &assignments {
            if col == &self.partition_key || Some(col) == self.sort_key.as_ref() {
                return Err(DataFusionError::Plan(format!(
                    "Cannot modify key column '{col}' — DynamoDB key attributes are immutable"
                )));
            }
            sets.push((col.clone(), expr_to_attribute_value(expr)?));
        }

        let plan = self.plan_dml(&filters)?;
        Ok(Arc::new(DynamoDmlExec::new(
            self.clone_handle(),
            DynamoDmlOp::Update { plan, sets },
        )))
    }
}

impl DynamoTableProvider {
    /// A cheap clone of the connection handle and key metadata for the DML
    /// execution plans (the schema isn't needed there).
    fn clone_handle(&self) -> DynamoHandle {
        DynamoHandle {
            client: self.client.clone(),
            table_name: self.table_name.clone(),
            partition_key: self.partition_key.clone(),
            sort_key: self.sort_key.clone(),
        }
    }

    /// Plan a DELETE/UPDATE against the key schema.
    ///
    /// DynamoDB cannot mutate by arbitrary predicate, so we first resolve the
    /// matching keys. Every WHERE predicate must be a convertible pushable
    /// comparison: otherwise the residual predicate could not be applied
    /// server-side and we would mutate rows it should have excluded. We refuse
    /// rather than silently over-delete (`DELETE WHERE a = 1 OR b = 2` must not
    /// wipe the table).
    fn plan_dml(&self, filters: &[Expr]) -> DFResult<DmlKeyPlan> {
        if let Some(bad) = filters.iter().find(|e| !is_convertible_pushdown(e)) {
            return Err(DataFusionError::Plan(format!(
                "DynamoDB DELETE/UPDATE requires every WHERE predicate to be a pushable comparison \
                 (a column compared to a literal via =, <>, <, <=, >, >=). Unsupported predicate: {bad}. \
                 Refusing to run to avoid mutating rows the predicate should have excluded."
            )));
        }
        classify_dml(&self.partition_key, self.sort_key.as_deref(), filters)
    }
}

/// Error returned when a write is attempted against a read-only source.
fn read_only_error(op: &str, table: &str) -> DataFusionError {
    DataFusionError::Plan(format!(
        "{op} not allowed on DynamoDB table '{table}': the data source is configured read_only. \
         Set access_mode: read_write to enable write operations."
    ))
}

/// Connection handle plus key metadata shared with DML execution plans.
#[derive(Clone)]
struct DynamoHandle {
    client: Client,
    table_name: String,
    partition_key: String,
    sort_key: Option<String>,
}

impl DynamoHandle {
    fn key_of(
        &self,
        item: &HashMap<String, AttributeValue>,
    ) -> Result<HashMap<String, AttributeValue>> {
        let mut key = HashMap::new();
        let pk = item.get(&self.partition_key).cloned().ok_or_else(|| {
            anyhow::anyhow!("item missing partition key '{}'", self.partition_key)
        })?;
        key.insert(self.partition_key.clone(), pk);
        if let Some(sk) = &self.sort_key {
            let sk_val = item
                .get(sk)
                .cloned()
                .ok_or_else(|| anyhow::anyhow!("item missing sort key '{sk}'"))?;
            key.insert(sk.clone(), sk_val);
        }
        Ok(key)
    }

    /// A `ProjectionExpression` (plus its name bindings) that fetches only the
    /// key attributes — all the DML paths need, since they mutate by key.
    fn key_projection(&self) -> (String, HashMap<String, String>) {
        let mut names = HashMap::new();
        let mut parts = vec!["#p0".to_string()];
        names.insert("#p0".to_string(), self.partition_key.clone());
        if let Some(sk) = &self.sort_key {
            names.insert("#p1".to_string(), sk.clone());
            parts.push("#p1".to_string());
        }
        (parts.join(", "), names)
    }

    /// Resolve the keys of items matching a DML plan, routing to Query when the
    /// partition key is pinned (any residual predicate is applied server-side as
    /// a `FilterExpression`) and falling back to a full Scan otherwise — instead
    /// of always scanning the whole table.
    async fn matching_keys(
        &self,
        plan: &DmlKeyPlan,
    ) -> Result<Vec<HashMap<String, AttributeValue>>> {
        let source = match plan {
            DmlKeyPlan::Query {
                key_condition,
                residual,
            } => PageSource::Query {
                key_condition: key_condition.clone(),
                filter: residual.clone(),
            },
            DmlKeyPlan::Scan { filter } => PageSource::Scan {
                filter: filter.clone(),
            },
        };
        let key_projection = self.key_projection();

        let mut keys = Vec::new();
        let mut start_key: Option<HashMap<String, AttributeValue>> = None;
        loop {
            let (items, last) = fetch_page(
                &self.client,
                &self.table_name,
                &source,
                Some(&key_projection),
                start_key.take(),
                None,
            )
            .await?;
            for item in &items {
                keys.push(self.key_of(item)?);
            }
            match last {
                Some(k) => start_key = Some(k),
                None => break,
            }
        }
        Ok(keys)
    }

    /// Delete a set of keys via `BatchWriteItem` (25 per request), retrying any
    /// unprocessed keys. Returns the number of keys submitted (all of which
    /// correspond to matched items).
    async fn batch_delete(&self, keys: Vec<HashMap<String, AttributeValue>>) -> Result<u64> {
        let total = keys.len() as u64;
        for chunk in keys.chunks(BATCH_WRITE_CHUNK) {
            let requests: Vec<WriteRequest> = chunk
                .iter()
                .map(|k| {
                    let del = DeleteRequest::builder()
                        .set_key(Some(k.clone()))
                        .build()
                        .expect("DeleteRequest requires only a key, which is set");
                    WriteRequest::builder().delete_request(del).build()
                })
                .collect();
            batch_write(&self.client, &self.table_name, requests).await?;
        }
        Ok(total)
    }
}

/// Submit one chunk of write requests, retrying `UnprocessedItems` (which
/// DynamoDB returns under throttling) up to a bounded number of times.
async fn batch_write(client: &Client, table: &str, requests: Vec<WriteRequest>) -> Result<()> {
    let mut pending: HashMap<String, Vec<WriteRequest>> =
        HashMap::from([(table.to_string(), requests)]);
    for _ in 0..8 {
        let out = client
            .batch_write_item()
            .set_request_items(Some(pending))
            .send()
            .await
            .with_context(|| format!("DynamoDB batch_write_item failed for '{table}'"))?;
        match out.unprocessed_items {
            Some(u) if !u.is_empty() => pending = u,
            _ => return Ok(()),
        }
    }
    anyhow::bail!("DynamoDB batch_write_item left items unprocessed after retries for '{table}'")
}

// ─── Schema inference & type mapping ────────────────────────────────────────

/// Map a sampled DynamoDB attribute to an Arrow type. Numbers are always
/// inferred as `Float64`: a single sampled item can't prove a column is
/// integer-only, and a later fractional value would otherwise be silently
/// truncated (or drop the row via the Inexact re-filter). Everything non-scalar
/// (maps, lists, sets, binary) falls back to `Utf8`.
pub(crate) fn attribute_value_to_arrow_type(value: &AttributeValue) -> DataType {
    match value {
        AttributeValue::S(_) => DataType::Utf8,
        AttributeValue::Bool(_) => DataType::Boolean,
        AttributeValue::N(_) => DataType::Float64,
        AttributeValue::Null(_) => DataType::Utf8,
        _ => DataType::Utf8,
    }
}

/// Build a typed Arrow array from a column of optional DynamoDB attributes.
pub(crate) fn attribute_values_to_arrow_array(
    values: &[Option<AttributeValue>],
    data_type: &DataType,
) -> ArrayRef {
    match data_type {
        DataType::Utf8 => {
            let arr: StringArray = values
                .iter()
                .map(|v| v.as_ref().and_then(av_to_string))
                .collect();
            Arc::new(arr)
        }
        DataType::Int32 => {
            let arr: Int32Array = values
                .iter()
                .map(|v| v.as_ref().and_then(av_to_i64).map(|n| n as i32))
                .collect();
            Arc::new(arr)
        }
        DataType::Int64 => {
            let arr: Int64Array = values
                .iter()
                .map(|v| v.as_ref().and_then(av_to_i64))
                .collect();
            Arc::new(arr)
        }
        DataType::Float64 => {
            let arr: Float64Array = values
                .iter()
                .map(|v| v.as_ref().and_then(av_to_f64))
                .collect();
            Arc::new(arr)
        }
        DataType::Boolean => {
            let arr: BooleanArray = values
                .iter()
                .map(|v| v.as_ref().and_then(av_to_bool))
                .collect();
            Arc::new(arr)
        }
        _ => {
            let arr: StringArray = values
                .iter()
                .map(|v| v.as_ref().and_then(av_to_string))
                .collect();
            Arc::new(arr)
        }
    }
}

/// Render a scalar attribute as the string form for a Utf8 column. An explicit
/// DynamoDB `NULL` maps to `None` (Arrow null) rather than an empty string, so a
/// SQL NULL written via `UPDATE ... SET col = NULL` round-trips as NULL instead
/// of `""`.
fn av_to_string(v: &AttributeValue) -> Option<String> {
    match v {
        AttributeValue::S(s) => Some(s.clone()),
        AttributeValue::N(n) => Some(n.clone()),
        AttributeValue::Bool(b) => Some(b.to_string()),
        AttributeValue::Null(_) => None,
        other => Some(format!("{other:?}")),
    }
}

/// Coerce to `i64` only from an exactly-integer `N`. A fractional value
/// (`N("7.9")`) or a string (`S("7")`) yields `None` (SQL NULL) rather than a
/// silently truncated or cross-type value — which would violate the Inexact
/// pushdown superset contract, since DynamoDB's server-side filter is type- and
/// value-strict.
fn av_to_i64(v: &AttributeValue) -> Option<i64> {
    match v {
        AttributeValue::N(n) => n.parse::<i64>().ok(),
        _ => None,
    }
}

/// Coerce to `f64` only from a numeric `N`. A string is not coerced (see
/// `av_to_i64` for why cross-type coercion is unsafe under Inexact pushdown).
fn av_to_f64(v: &AttributeValue) -> Option<f64> {
    match v {
        AttributeValue::N(n) => n.parse::<f64>().ok(),
        _ => None,
    }
}

fn av_to_bool(v: &AttributeValue) -> Option<bool> {
    match v {
        AttributeValue::Bool(b) => Some(*b),
        _ => None,
    }
}

/// Convert one Arrow cell into a DynamoDB attribute. Returns `None` for NULLs so
/// the caller can omit the attribute (DynamoDB has no typed NULL columns).
fn arrow_value_to_attribute(
    array: &ArrayRef,
    row: usize,
    data_type: &DataType,
) -> Result<Option<AttributeValue>> {
    if array.is_null(row) {
        return Ok(None);
    }
    let value = match data_type {
        DataType::Utf8 => {
            let arr = array
                .as_any()
                .downcast_ref::<StringArray>()
                .with_context(|| "expected StringArray for DataType::Utf8")?;
            AttributeValue::S(arr.value(row).to_string())
        }
        DataType::Int32 => {
            let arr = array
                .as_any()
                .downcast_ref::<Int32Array>()
                .with_context(|| "expected Int32Array for DataType::Int32")?;
            AttributeValue::N(arr.value(row).to_string())
        }
        DataType::Int64 => {
            let arr = array
                .as_any()
                .downcast_ref::<Int64Array>()
                .with_context(|| "expected Int64Array for DataType::Int64")?;
            AttributeValue::N(arr.value(row).to_string())
        }
        DataType::Float64 => {
            let arr = array
                .as_any()
                .downcast_ref::<Float64Array>()
                .with_context(|| "expected Float64Array for DataType::Float64")?;
            AttributeValue::N(arr.value(row).to_string())
        }
        DataType::Boolean => {
            let arr = array
                .as_any()
                .downcast_ref::<BooleanArray>()
                .with_context(|| "expected BooleanArray for DataType::Boolean")?;
            AttributeValue::Bool(arr.value(row))
        }
        _ => {
            let arr = array
                .as_any()
                .downcast_ref::<StringArray>()
                .with_context(|| "unsupported Arrow type for DynamoDB write")?;
            AttributeValue::S(arr.value(row).to_string())
        }
    };
    Ok(Some(value))
}

fn record_batch_to_items(
    batch: &RecordBatch,
    schema: &Schema,
) -> Result<Vec<HashMap<String, AttributeValue>>> {
    let mut items = Vec::with_capacity(batch.num_rows());
    for row in 0..batch.num_rows() {
        let mut item = HashMap::new();
        for (idx, field) in schema.fields().iter().enumerate() {
            let array = batch.column(idx);
            if let Some(v) = arrow_value_to_attribute(array, row, field.data_type())? {
                item.insert(field.name().clone(), v);
            }
        }
        items.push(item);
    }
    Ok(items)
}

// ─── Filter pushdown ────────────────────────────────────────────────────────

/// A DynamoDB `FilterExpression` plus its attribute-name/value placeholder maps.
#[derive(Clone, Debug)]
struct DynamoFilter {
    expression: String,
    names: HashMap<String, String>,
    values: HashMap<String, AttributeValue>,
}

/// A pushable-shaped filter (see `is_pushable_binary_filter`) whose literal can
/// also be converted to a DynamoDB attribute value. The shape check alone is not
/// enough: a comparison against e.g. a Timestamp literal is pushable-shaped but
/// not convertible, and pushing it would fail the request.
fn is_convertible_pushdown(expr: &Expr) -> bool {
    is_pushable_binary_filter(expr) && normalize_binary(expr).is_some()
}

/// Convert a list of pushable binary filters into one ANDed DynamoDB
/// `FilterExpression`. Attribute names and values are passed as `#n`/`:v`
/// placeholders so reserved words and types are handled safely.
fn build_filter_expression(filters: &[Expr]) -> DFResult<Option<DynamoFilter>> {
    if filters.is_empty() {
        return Ok(None);
    }
    let mut parts = Vec::with_capacity(filters.len());
    let mut names = HashMap::new();
    let mut values = HashMap::new();

    for (i, expr) in filters.iter().enumerate() {
        let Expr::BinaryExpr(binary) = expr else {
            return Err(DataFusionError::Plan(format!(
                "Unsupported DynamoDB filter expression: {expr}"
            )));
        };
        let (col, value_expr, flipped) = match (binary.left.as_ref(), binary.right.as_ref()) {
            (Expr::Column(c), v) => (c.name.clone(), v, false),
            (v, Expr::Column(c)) => (c.name.clone(), v, true),
            _ => {
                return Err(DataFusionError::Plan(format!(
                    "DynamoDB filter must compare a column to a literal, got: {expr}"
                )));
            }
        };
        // If the literal is on the left (`5 < col`), invert the operator so the
        // emitted expression keeps `#name <op> :val` form.
        let op = if flipped {
            flip_operator(binary.op)
        } else {
            binary.op
        };
        let name_ph = format!("#n{i}");
        let val_ph = format!(":v{i}");
        names.insert(name_ph.clone(), col);
        values.insert(val_ph.clone(), expr_to_attribute_value(value_expr)?);
        parts.push(format!("{name_ph} {} {val_ph}", operator_symbol(op)?));
    }

    Ok(Some(DynamoFilter {
        expression: parts.join(" AND "),
        names,
        values,
    }))
}

fn flip_operator(op: Operator) -> Operator {
    match op {
        Operator::Lt => Operator::Gt,
        Operator::LtEq => Operator::GtEq,
        Operator::Gt => Operator::Lt,
        Operator::GtEq => Operator::LtEq,
        other => other, // Eq / NotEq are symmetric
    }
}

fn operator_symbol(op: Operator) -> DFResult<&'static str> {
    match op {
        Operator::Eq => Ok("="),
        Operator::NotEq => Ok("<>"),
        Operator::Lt => Ok("<"),
        Operator::LtEq => Ok("<="),
        Operator::Gt => Ok(">"),
        Operator::GtEq => Ok(">="),
        other => Err(DataFusionError::Plan(format!(
            "Unsupported DynamoDB filter operator: {other}"
        ))),
    }
}

/// Build a `ProjectionExpression` naming exactly the fields in `schema`, using a
/// `#p` placeholder namespace (distinct from filter `#n`/`:v` and key-condition
/// `#k`/`:k`) so it can be merged onto the same request without collision.
fn build_projection_expression(schema: &Schema) -> (String, HashMap<String, String>) {
    let mut names = HashMap::new();
    let mut parts = Vec::with_capacity(schema.fields().len());
    for (i, field) in schema.fields().iter().enumerate() {
        let ph = format!("#p{i}");
        names.insert(ph.clone(), field.name().clone());
        parts.push(ph);
    }
    (parts.join(", "), names)
}

// ─── Key-aware read planning ────────────────────────────────────────────────

/// How a `scan()` should physically read DynamoDB given its pushable filters.
#[derive(Clone, Debug)]
enum DynamoRead {
    /// Full primary key pinned by equality — a single-item `GetItem`.
    GetItem {
        key: HashMap<String, AttributeValue>,
    },
    /// Partition key pinned by equality, with an optional sort-key condition —
    /// a `Query` against the key schema.
    Query { key_condition: DynamoFilter },
    /// No usable key constraint — a full `Scan` with an optional filter.
    Scan { filter: Option<DynamoFilter> },
}

/// Operators DynamoDB accepts in a `KeyConditionExpression` on the sort key.
/// `NotEq` (`<>`) is intentionally excluded — it is pushable as a regular filter
/// but illegal on a key.
fn is_key_condition_op(op: Operator) -> bool {
    matches!(
        op,
        Operator::Eq | Operator::Lt | Operator::LtEq | Operator::Gt | Operator::GtEq
    )
}

/// Normalize a pushable binary filter into `(column, operator, value)` with the
/// column on the left (operator flipped if the literal was on the left).
fn normalize_binary(expr: &Expr) -> Option<(String, Operator, AttributeValue)> {
    let Expr::BinaryExpr(binary) = expr else {
        return None;
    };
    let (col, value_expr, flipped) = match (binary.left.as_ref(), binary.right.as_ref()) {
        (Expr::Column(c), v) => (c.name.clone(), v, false),
        (v, Expr::Column(c)) => (c.name.clone(), v, true),
        _ => return None,
    };
    let op = if flipped {
        flip_operator(binary.op)
    } else {
        binary.op
    };
    Some((col, op, expr_to_attribute_value(value_expr).ok()?))
}

/// Build a DynamoDB `KeyConditionExpression` (`#k0 = :k0 [AND #k1 <op> :k1]`).
/// Uses a `#k`/`:k` placeholder namespace distinct from the `#n`/`:v` used by
/// filter expressions, so the two never collide if combined on one request.
fn build_key_condition(
    partition_key: &str,
    partition_value: AttributeValue,
    sort_key: &str,
    sort_cond: Option<(Operator, AttributeValue)>,
) -> DynamoFilter {
    let mut names = HashMap::new();
    let mut values = HashMap::new();
    names.insert("#k0".to_string(), partition_key.to_string());
    values.insert(":k0".to_string(), partition_value);
    let mut expression = "#k0 = :k0".to_string();

    if let Some((op, value)) = sort_cond {
        names.insert("#k1".to_string(), sort_key.to_string());
        values.insert(":k1".to_string(), value);
        let symbol =
            operator_symbol(op).expect("sort condition op validated by is_key_condition_op");
        expression.push_str(&format!(" AND #k1 {symbol} :k1"));
    }

    DynamoFilter {
        expression,
        names,
        values,
    }
}

/// Classify pushable filters into the cheapest DynamoDB access pattern.
///
/// Only the *key* portion of the predicate set drives the choice; any other
/// predicate is left for DataFusion to re-apply (filters are pushed `Inexact`),
/// so the result is always correct regardless of which path is chosen.
fn classify_read(
    partition_key: &str,
    sort_key: Option<&str>,
    pushable: &[Expr],
) -> DFResult<DynamoRead> {
    let mut partition_value: Option<AttributeValue> = None;
    let mut sort_cond: Option<(Operator, AttributeValue)> = None;

    for expr in pushable {
        let Some((col, op, value)) = normalize_binary(expr) else {
            continue;
        };
        if col == partition_key {
            // The partition key only narrows the access pattern under equality.
            if op == Operator::Eq && partition_value.is_none() {
                partition_value = Some(value);
            }
        } else if Some(col.as_str()) == sort_key && is_key_condition_op(op) && sort_cond.is_none() {
            sort_cond = Some((op, value));
        }
    }

    let Some(partition_value) = partition_value else {
        // Partition key not pinned by equality → must Scan.
        return Ok(DynamoRead::Scan {
            filter: build_filter_expression(pushable)?,
        });
    };

    match (sort_key, sort_cond) {
        // Single-key table: the partition key IS the full primary key → GetItem.
        (None, _) => {
            let mut key = HashMap::new();
            key.insert(partition_key.to_string(), partition_value);
            Ok(DynamoRead::GetItem { key })
        }
        // Composite key fully pinned by equality → GetItem.
        (Some(sk), Some((Operator::Eq, sort_value))) => {
            let mut key = HashMap::new();
            key.insert(partition_key.to_string(), partition_value);
            key.insert(sk.to_string(), sort_value);
            Ok(DynamoRead::GetItem { key })
        }
        // Composite key with a sort-key range (or no sort constraint) → Query.
        (Some(sk), sort_cond) => Ok(DynamoRead::Query {
            key_condition: build_key_condition(partition_key, partition_value, sk, sort_cond),
        }),
    }
}

/// How a DELETE/UPDATE should resolve the keys it will mutate. Unlike the read
/// path (whose residual predicates DataFusion re-applies via Inexact pushdown),
/// DML must apply *every* predicate server-side, so any residual non-key
/// predicate travels as a `FilterExpression`.
#[derive(Clone, Debug)]
enum DmlKeyPlan {
    /// Partition key pinned by equality → `Query` (with an optional sort-key
    /// condition folded in), plus a residual `FilterExpression` over non-key
    /// attributes. Far cheaper than scanning the whole table.
    Query {
        key_condition: DynamoFilter,
        residual: Option<DynamoFilter>,
    },
    /// Partition key not pinned (or an inexpressible sort predicate) → full
    /// `Scan` carrying every predicate as a `FilterExpression`.
    Scan { filter: Option<DynamoFilter> },
}

/// Plan the key-resolution strategy for a DELETE/UPDATE. Callers must have
/// already ensured every predicate is a convertible pushable comparison (see
/// `plan_dml`), so nothing is silently dropped.
///
/// We can drive a `Query` only when the partition key is pinned by equality and
/// every sort-key predicate is expressible on the key: a `Query`'s
/// `FilterExpression` may not reference key attributes, and its
/// `KeyConditionExpression` allows at most one sort-key condition using a legal
/// operator (so `sk <> …`, or two sort predicates, force a `Scan`). A `Scan`'s
/// filter, by contrast, may reference keys, so it always expresses the full
/// predicate set.
fn classify_dml(
    partition_key: &str,
    sort_key: Option<&str>,
    filters: &[Expr],
) -> DFResult<DmlKeyPlan> {
    let mut partition_value: Option<AttributeValue> = None;
    let mut partition_query_ok = true;
    let mut sort_conds: Vec<(Operator, AttributeValue)> = Vec::new();
    let mut sort_key_expressible = true;
    let mut residual: Vec<Expr> = Vec::new();

    for expr in filters {
        let Some((col, op, value)) = normalize_binary(expr) else {
            // Unreachable after plan_dml's guard, but stay safe: force a Scan.
            partition_query_ok = false;
            residual.push(expr.clone());
            continue;
        };
        if col == partition_key {
            if op == Operator::Eq && partition_value.is_none() {
                partition_value = Some(value);
            } else {
                // A non-equality (or duplicate) partition predicate can't drive
                // a Query and can't live in a Query filter → must Scan.
                partition_query_ok = false;
            }
        } else if Some(col.as_str()) == sort_key {
            if !is_key_condition_op(op) {
                sort_key_expressible = false;
            }
            sort_conds.push((op, value));
        } else {
            residual.push(expr.clone());
        }
    }

    let can_query = partition_query_ok
        && partition_value.is_some()
        && sort_conds.len() <= 1
        && sort_key_expressible;

    if can_query {
        let partition_value = partition_value.expect("checked by can_query");
        let sort_cond = sort_conds.into_iter().next();
        let key_condition = build_key_condition(
            partition_key,
            partition_value,
            sort_key.unwrap_or(""),
            sort_cond,
        );
        Ok(DmlKeyPlan::Query {
            key_condition,
            residual: build_filter_expression(&residual)?,
        })
    } else {
        // Scan carries every predicate; a Scan filter may reference key columns.
        Ok(DmlKeyPlan::Scan {
            filter: build_filter_expression(filters)?,
        })
    }
}

/// Convert a DataFusion literal expression into a DynamoDB attribute value.
pub(crate) fn expr_to_attribute_value(expr: &Expr) -> DFResult<AttributeValue> {
    match expr {
        Expr::Literal(scalar, _) => scalar_to_attribute_value(scalar),
        _ => Err(DataFusionError::Plan(format!(
            "Unsupported expression for DynamoDB value: {expr}"
        ))),
    }
}

fn scalar_to_attribute_value(scalar: &datafusion::common::ScalarValue) -> DFResult<AttributeValue> {
    use datafusion::common::ScalarValue;
    let av = match scalar {
        ScalarValue::Utf8(Some(s)) | ScalarValue::LargeUtf8(Some(s)) => {
            AttributeValue::S(s.clone())
        }
        ScalarValue::Int8(Some(v)) => AttributeValue::N(v.to_string()),
        ScalarValue::Int16(Some(v)) => AttributeValue::N(v.to_string()),
        ScalarValue::Int32(Some(v)) => AttributeValue::N(v.to_string()),
        ScalarValue::Int64(Some(v)) => AttributeValue::N(v.to_string()),
        ScalarValue::UInt8(Some(v)) => AttributeValue::N(v.to_string()),
        ScalarValue::UInt16(Some(v)) => AttributeValue::N(v.to_string()),
        ScalarValue::UInt32(Some(v)) => AttributeValue::N(v.to_string()),
        ScalarValue::UInt64(Some(v)) => AttributeValue::N(v.to_string()),
        ScalarValue::Float32(Some(v)) => AttributeValue::N(v.to_string()),
        ScalarValue::Float64(Some(v)) => AttributeValue::N(v.to_string()),
        ScalarValue::Boolean(Some(v)) => AttributeValue::Bool(*v),
        ScalarValue::Null => AttributeValue::Null(true),
        _ => {
            return Err(DataFusionError::Plan(format!(
                "Unsupported scalar type for DynamoDB: {scalar}"
            )));
        }
    };
    Ok(av)
}

// ─── Page fetching & streaming ──────────────────────────────────────────────

/// One page source for the shared scan/query paginator.
#[derive(Clone, Debug)]
enum PageSource {
    Scan {
        filter: Option<DynamoFilter>,
    },
    Query {
        key_condition: DynamoFilter,
        filter: Option<DynamoFilter>,
    },
}

/// Fetch a single page from a Scan or Query, merging any filter, key-condition
/// and projection expressions onto one request (their `#n`/`#k`/`#p` namespaces
/// never collide). Returns the page's items — moved, not cloned — and the next
/// `ExclusiveStartKey` (`None` when the table is exhausted).
async fn fetch_page(
    client: &Client,
    table: &str,
    source: &PageSource,
    projection: Option<&(String, HashMap<String, String>)>,
    start_key: Option<HashMap<String, AttributeValue>>,
    page_limit: Option<i32>,
) -> Result<(
    Vec<HashMap<String, AttributeValue>>,
    Option<HashMap<String, AttributeValue>>,
)> {
    let mut names: HashMap<String, String> = HashMap::new();
    if let Some((_, pnames)) = projection {
        names.extend(pnames.clone());
    }

    let (items, last_key) = match source {
        PageSource::Scan { filter } => {
            let mut req = client.scan().table_name(table);
            if let Some(f) = filter {
                req = req
                    .filter_expression(&f.expression)
                    .set_expression_attribute_values(Some(f.values.clone()));
                names.extend(f.names.clone());
            }
            if let Some((expr, _)) = projection {
                req = req.projection_expression(expr);
            }
            if !names.is_empty() {
                req = req.set_expression_attribute_names(Some(names));
            }
            if let Some(l) = page_limit {
                req = req.limit(l);
            }
            if let Some(sk) = start_key {
                req = req.set_exclusive_start_key(Some(sk));
            }
            let out = req
                .send()
                .await
                .with_context(|| format!("DynamoDB scan failed for '{table}'"))?;
            (out.items.unwrap_or_default(), out.last_evaluated_key)
        }
        PageSource::Query {
            key_condition,
            filter,
        } => {
            let mut req = client
                .query()
                .table_name(table)
                .key_condition_expression(&key_condition.expression);
            let mut values = key_condition.values.clone();
            names.extend(key_condition.names.clone());
            if let Some(f) = filter {
                req = req.filter_expression(&f.expression);
                values.extend(f.values.clone());
                names.extend(f.names.clone());
            }
            if let Some((expr, _)) = projection {
                req = req.projection_expression(expr);
            }
            req = req
                .set_expression_attribute_names(Some(names))
                .set_expression_attribute_values(Some(values));
            if let Some(l) = page_limit {
                req = req.limit(l);
            }
            if let Some(sk) = start_key {
                req = req.set_exclusive_start_key(Some(sk));
            }
            let out = req
                .send()
                .await
                .with_context(|| format!("DynamoDB query failed for '{table}'"))?;
            (out.items.unwrap_or_default(), out.last_evaluated_key)
        }
    };

    Ok((items, last_key.filter(|k| !k.is_empty())))
}

/// State carried between paginator steps.
struct PageState {
    start_key: Option<HashMap<String, AttributeValue>>,
    remaining: Option<usize>,
}

/// Stream a Scan/Query one page at a time as projected `RecordBatch`es. Memory
/// is bounded to a single page (~1MB) instead of buffering the whole result,
/// and a SQL `LIMIT` caps both the rows returned and the per-page request size.
fn stream_pages(
    client: Client,
    table: String,
    source: PageSource,
    projection: Option<(String, HashMap<String, String>)>,
    schema: SchemaRef,
    limit: Option<usize>,
) -> impl Stream<Item = DFResult<RecordBatch>> {
    let initial = Some(PageState {
        start_key: None,
        remaining: limit,
    });
    stream::unfold(initial, move |maybe_state| {
        let client = client.clone();
        let table = table.clone();
        let source = source.clone();
        let projection = projection.clone();
        let schema = schema.clone();
        async move {
            let state = maybe_state?;
            let page_limit = state.remaining.map(|r| r.min(i32::MAX as usize) as i32);
            let fetched = fetch_page(
                &client,
                &table,
                &source,
                projection.as_ref(),
                state.start_key,
                page_limit,
            )
            .await
            .map_err(|e| DataFusionError::External(e.into()));

            let (mut items, last_key) = match fetched {
                Ok(v) => v,
                Err(e) => return Some((Err(e), None)),
            };
            if let Some(r) = state.remaining {
                items.truncate(r);
            }
            let got = items.len();
            let batch = items_to_batch(items, &schema);
            let next_remaining = state.remaining.map(|r| r.saturating_sub(got));
            let stop = next_remaining == Some(0) || last_key.is_none();
            let next = if stop {
                None
            } else {
                Some(PageState {
                    start_key: last_key,
                    remaining: next_remaining,
                })
            };
            Some((batch, next))
        }
    })
}

/// Count items server-side via `Scan` with `Select=COUNT`, emitting a single
/// zero-column batch whose row count is the total (drives `count(*)`). Only
/// reached when the projection is empty, i.e. no predicate references any
/// column, so an unfiltered count is exact.
async fn count_scan(client: &Client, table: &str, schema: SchemaRef) -> DFResult<RecordBatch> {
    let mut total = 0usize;
    let mut start: Option<HashMap<String, AttributeValue>> = None;
    loop {
        let mut req = client.scan().table_name(table).select(Select::Count);
        if let Some(sk) = start.take() {
            req = req.set_exclusive_start_key(Some(sk));
        }
        let out = req
            .send()
            .await
            .map_err(|e| DataFusionError::Execution(format!("DynamoDB count scan failed: {e}")))?;
        total += out.count() as usize;
        match out.last_evaluated_key {
            Some(k) if !k.is_empty() => start = Some(k),
            _ => break,
        }
    }
    let options = RecordBatchOptions::new().with_row_count(Some(total));
    RecordBatch::try_new_with_options(schema, vec![], &options).map_err(DataFusionError::from)
}

// ─── Scan execution plan ────────────────────────────────────────────────────

/// Leaf plan that streams a DynamoDB read, page by page, on execution.
#[derive(Debug)]
struct DynamoScanExec {
    client: Client,
    table_name: String,
    output_schema: SchemaRef,
    read: DynamoRead,
    /// `ProjectionExpression` (and its `#p` bindings) for the projected columns,
    /// or `None` for a `count(*)` (empty projection).
    projection_expr: Option<(String, HashMap<String, String>)>,
    count_only: bool,
    limit: Option<usize>,
    properties: PlanProperties,
}

impl DisplayAs for DynamoScanExec {
    fn fmt_as(&self, _t: DisplayFormatType, f: &mut Formatter) -> fmt::Result {
        write!(f, "DynamoScanExec")
    }
}

impl ExecutionPlan for DynamoScanExec {
    fn name(&self) -> &str {
        "DynamoScanExec"
    }
    fn as_any(&self) -> &dyn Any {
        self
    }
    fn properties(&self) -> &PlanProperties {
        &self.properties
    }
    fn children(&self) -> Vec<&Arc<dyn ExecutionPlan>> {
        vec![]
    }
    fn with_new_children(
        self: Arc<Self>,
        _children: Vec<Arc<dyn ExecutionPlan>>,
    ) -> DFResult<Arc<dyn ExecutionPlan>> {
        Ok(self)
    }
    fn execute(
        &self,
        _partition: usize,
        _context: Arc<datafusion::execution::TaskContext>,
    ) -> DFResult<SendableRecordBatchStream> {
        let schema = self.output_schema.clone();
        let client = self.client.clone();
        let table = self.table_name.clone();

        if self.count_only {
            let count_schema = schema.clone();
            let fut = async move { count_scan(&client, &table, count_schema).await };
            return Ok(Box::pin(RecordBatchStreamAdapter::new(
                schema,
                stream::once(fut),
            )));
        }

        match self.read.clone() {
            DynamoRead::GetItem { key } => {
                let projection = self.projection_expr.clone();
                let item_schema = schema.clone();
                let fut = async move {
                    let mut req = client.get_item().table_name(&table).set_key(Some(key));
                    if let Some((expr, names)) = projection {
                        req = req
                            .projection_expression(expr)
                            .set_expression_attribute_names(Some(names));
                    }
                    let out = req.send().await.map_err(|e| {
                        DataFusionError::Execution(format!("DynamoDB get_item failed: {e}"))
                    })?;
                    let items: Vec<_> = out.item.into_iter().collect();
                    items_to_batch(items, &item_schema)
                };
                Ok(Box::pin(RecordBatchStreamAdapter::new(
                    schema,
                    stream::once(fut),
                )))
            }
            DynamoRead::Query { key_condition } => {
                let src = PageSource::Query {
                    key_condition,
                    filter: None,
                };
                let s = stream_pages(
                    client,
                    table,
                    src,
                    self.projection_expr.clone(),
                    schema.clone(),
                    self.limit,
                );
                Ok(Box::pin(RecordBatchStreamAdapter::new(schema, s)))
            }
            DynamoRead::Scan { filter } => {
                let src = PageSource::Scan { filter };
                let s = stream_pages(
                    client,
                    table,
                    src,
                    self.projection_expr.clone(),
                    schema.clone(),
                    self.limit,
                );
                Ok(Box::pin(RecordBatchStreamAdapter::new(schema, s)))
            }
        }
    }
}

// ─── Insert execution plan ──────────────────────────────────────────────────

struct DynamoInsertExec {
    input: Arc<dyn ExecutionPlan>,
    client: Client,
    table_name: String,
    /// Target-table schema, used to shape input batches into items to write.
    schema: SchemaRef,
    /// Properties of this node's own output (`{ count }`), distinct from
    /// `input`'s schema — `execute` streams a count, not the inserted rows.
    properties: PlanProperties,
    partition_key: String,
    /// True for INSERT OVERWRITE/REPLACE (upsert via `BatchWriteItem`); false
    /// for plain INSERT (Append), which uses a conditional `PutItem` so a
    /// duplicate key errors instead of silently replacing the item.
    upsert: bool,
}

impl Debug for DynamoInsertExec {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        f.debug_struct("DynamoInsertExec")
            .field("table_name", &self.table_name)
            .field("upsert", &self.upsert)
            .finish()
    }
}

impl DisplayAs for DynamoInsertExec {
    fn fmt_as(&self, _t: DisplayFormatType, f: &mut Formatter) -> fmt::Result {
        write!(f, "DynamoInsertExec")
    }
}

/// Append INSERT: `PutItem` guarded by `attribute_not_exists` on the partition
/// key so an existing item is not silently overwritten.
async fn put_conditional(
    client: &Client,
    table: &str,
    partition_key: &str,
    item: HashMap<String, AttributeValue>,
) -> DFResult<()> {
    let names = HashMap::from([("#pk".to_string(), partition_key.to_string())]);
    client
        .put_item()
        .table_name(table)
        .set_item(Some(item))
        .condition_expression("attribute_not_exists(#pk)")
        .set_expression_attribute_names(Some(names))
        .send()
        .await
        .map_err(|e| {
            let svc = e.into_service_error();
            if svc.is_conditional_check_failed_exception() {
                DataFusionError::Execution(format!(
                    "DynamoDB INSERT: an item with this key already exists in '{table}'. \
                     Use INSERT OVERWRITE to replace it."
                ))
            } else {
                DataFusionError::Execution(format!("DynamoDB put_item failed: {svc}"))
            }
        })?;
    Ok(())
}

/// Upsert INSERT (Overwrite/Replace): batch the items via `BatchWriteItem`
/// (25 per request), cutting a large insert from one round-trip per row.
async fn batch_put(
    client: &Client,
    table: &str,
    items: Vec<HashMap<String, AttributeValue>>,
) -> DFResult<()> {
    for chunk in items.chunks(BATCH_WRITE_CHUNK) {
        let requests: Vec<WriteRequest> = chunk
            .iter()
            .map(|it| {
                let put = PutRequest::builder()
                    .set_item(Some(it.clone()))
                    .build()
                    .expect("PutRequest requires only an item, which is set");
                WriteRequest::builder().put_request(put).build()
            })
            .collect();
        batch_write(client, table, requests)
            .await
            .map_err(|e| DataFusionError::External(e.into()))?;
    }
    Ok(())
}

impl ExecutionPlan for DynamoInsertExec {
    fn name(&self) -> &str {
        "DynamoInsertExec"
    }
    fn as_any(&self) -> &dyn Any {
        self
    }
    fn properties(&self) -> &PlanProperties {
        &self.properties
    }
    fn children(&self) -> Vec<&Arc<dyn ExecutionPlan>> {
        vec![&self.input]
    }
    fn with_new_children(
        self: Arc<Self>,
        children: Vec<Arc<dyn ExecutionPlan>>,
    ) -> DFResult<Arc<dyn ExecutionPlan>> {
        Ok(Arc::new(DynamoInsertExec {
            input: children[0].clone(),
            client: self.client.clone(),
            table_name: self.table_name.clone(),
            schema: self.schema.clone(),
            properties: self.properties.clone(),
            partition_key: self.partition_key.clone(),
            upsert: self.upsert,
        }))
    }
    fn execute(
        &self,
        partition: usize,
        context: Arc<datafusion::execution::TaskContext>,
    ) -> DFResult<SendableRecordBatchStream> {
        let mut input_stream = self.input.execute(partition, context)?;
        let client = self.client.clone();
        let table_name = self.table_name.clone();
        let schema = self.schema.clone();
        let partition_key = self.partition_key.clone();
        let upsert = self.upsert;
        let output_schema = count_schema();

        let future = async move {
            let mut count: u64 = 0;
            // Bound memory for upsert batching: flush every full chunk instead of
            // buffering the entire input.
            let mut buf: Vec<HashMap<String, AttributeValue>> = Vec::new();
            while let Some(batch) = input_stream.next().await {
                let batch = batch?;
                let items = record_batch_to_items(&batch, &schema)
                    .map_err(|e| DataFusionError::External(e.into()))?;
                for item in items {
                    if upsert {
                        buf.push(item);
                        if buf.len() >= BATCH_WRITE_CHUNK {
                            let chunk = std::mem::take(&mut buf);
                            count += chunk.len() as u64;
                            batch_put(&client, &table_name, chunk).await?;
                        }
                    } else {
                        put_conditional(&client, &table_name, &partition_key, item).await?;
                        count += 1;
                    }
                }
            }
            if !buf.is_empty() {
                count += buf.len() as u64;
                batch_put(&client, &table_name, buf).await?;
            }
            count_batch(count)
        };

        Ok(Box::pin(RecordBatchStreamAdapter::new(
            output_schema,
            stream::once(future),
        )))
    }
}

// ─── DELETE / UPDATE execution plan ─────────────────────────────────────────

#[derive(Clone)]
enum DynamoDmlOp {
    Delete {
        plan: DmlKeyPlan,
    },
    Update {
        plan: DmlKeyPlan,
        sets: Vec<(String, AttributeValue)>,
    },
}

/// Leaf plan that runs a key-based DELETE or UPDATE and returns `{ count }`.
///
/// DynamoDB cannot delete or update by arbitrary predicate, so the matching
/// keys are resolved first (via Query or Scan) and then mutated: DELETE batches
/// them through `BatchWriteItem`, UPDATE issues one `UpdateItem` per key
/// (`BatchWriteItem` cannot express updates).
struct DynamoDmlExec {
    handle: DynamoHandle,
    op: DynamoDmlOp,
    schema: SchemaRef,
    properties: PlanProperties,
}

impl DynamoDmlExec {
    fn new(handle: DynamoHandle, op: DynamoDmlOp) -> Self {
        Self {
            handle,
            op,
            schema: count_schema(),
            properties: count_plan_properties(),
        }
    }
}

impl Debug for DynamoDmlExec {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        write!(f, "DynamoDmlExec")
    }
}

impl DisplayAs for DynamoDmlExec {
    fn fmt_as(&self, _t: DisplayFormatType, f: &mut Formatter) -> fmt::Result {
        write!(f, "DynamoDmlExec")
    }
}

impl ExecutionPlan for DynamoDmlExec {
    fn name(&self) -> &str {
        "DynamoDmlExec"
    }
    fn as_any(&self) -> &dyn Any {
        self
    }
    fn properties(&self) -> &PlanProperties {
        &self.properties
    }
    fn children(&self) -> Vec<&Arc<dyn ExecutionPlan>> {
        vec![]
    }
    fn with_new_children(
        self: Arc<Self>,
        _children: Vec<Arc<dyn ExecutionPlan>>,
    ) -> DFResult<Arc<dyn ExecutionPlan>> {
        Ok(self)
    }
    fn execute(
        &self,
        _partition: usize,
        _context: Arc<datafusion::execution::TaskContext>,
    ) -> DFResult<SendableRecordBatchStream> {
        let handle = self.handle.clone();
        let op = self.op.clone();
        let future = async move {
            let affected = match op {
                DynamoDmlOp::Delete { plan } => {
                    let keys = handle
                        .matching_keys(&plan)
                        .await
                        .map_err(|e| DataFusionError::External(e.into()))?;
                    handle
                        .batch_delete(keys)
                        .await
                        .map_err(|e| DataFusionError::External(e.into()))?
                }
                DynamoDmlOp::Update { plan, sets } => {
                    let keys = handle
                        .matching_keys(&plan)
                        .await
                        .map_err(|e| DataFusionError::External(e.into()))?;
                    let (expr, mut names, values) = build_update_expression(&sets);
                    // Guard against resurrecting a row deleted between key
                    // resolution and this update: DynamoDB's UpdateItem creates
                    // the item if absent, so without a condition a concurrently
                    // deleted row would come back as key attributes plus the SET
                    // fields. `attribute_exists` on the partition key (which can
                    // never be a SET target, so `#pk` won't collide with `#s*`)
                    // makes the update a no-op for a vanished row.
                    names.insert("#pk".to_string(), handle.partition_key.clone());
                    let mut n = 0u64;
                    for key in keys {
                        let result = handle
                            .client
                            .update_item()
                            .table_name(&handle.table_name)
                            .set_key(Some(key))
                            .update_expression(&expr)
                            .condition_expression("attribute_exists(#pk)")
                            .set_expression_attribute_names(Some(names.clone()))
                            .set_expression_attribute_values(Some(values.clone()))
                            .send()
                            .await;
                        match result {
                            Ok(_) => n += 1,
                            // Row disappeared after matching_keys(); skip it
                            // rather than recreating a partial item or failing
                            // the whole statement.
                            Err(e)
                                if e.as_service_error().is_some_and(|se| {
                                    se.is_conditional_check_failed_exception()
                                }) => {}
                            Err(e) => {
                                return Err(DataFusionError::Execution(format!(
                                    "DynamoDB update failed: {e}"
                                )));
                            }
                        }
                    }
                    n
                }
            };
            count_batch(affected)
        };

        Ok(Box::pin(RecordBatchStreamAdapter::new(
            self.schema.clone(),
            stream::once(future),
        )))
    }
}

/// Build a DynamoDB `SET` update expression from column→value assignments.
fn build_update_expression(
    sets: &[(String, AttributeValue)],
) -> (
    String,
    HashMap<String, String>,
    HashMap<String, AttributeValue>,
) {
    let mut names = HashMap::new();
    let mut values = HashMap::new();
    let mut parts = Vec::with_capacity(sets.len());
    for (i, (col, av)) in sets.iter().enumerate() {
        let name_ph = format!("#s{i}");
        let val_ph = format!(":s{i}");
        names.insert(name_ph.clone(), col.clone());
        values.insert(val_ph.clone(), av.clone());
        parts.push(format!("{name_ph} = {val_ph}"));
    }
    (format!("SET {}", parts.join(", ")), names, values)
}

fn count_schema() -> SchemaRef {
    Arc::new(Schema::new(vec![Field::new(
        "count",
        DataType::UInt64,
        false,
    )]))
}

/// `PlanProperties` for an insert/DML leaf whose execution emits a single
/// `{ count }` row. Kept in sync with the schema returned by `execute` so
/// planning/introspection see the real output shape, not the input's.
fn count_plan_properties() -> PlanProperties {
    PlanProperties::new(
        EquivalenceProperties::new(count_schema()),
        Partitioning::UnknownPartitioning(1),
        EmissionType::Final,
        Boundedness::Bounded,
    )
}

fn count_batch(count: u64) -> DFResult<RecordBatch> {
    let array: UInt64Array = vec![count].into();
    RecordBatch::try_new(count_schema(), vec![Arc::new(array)]).map_err(DataFusionError::from)
}

// ─── Registration ───────────────────────────────────────────────────────────

/// Register a DynamoDB table or a whole account/endpoint (catalog) into a DataFusion
/// [`SessionContext`].
///
/// Single-table mode (default) registers one table under `name`. Catalog mode discovers all
/// accessible DynamoDB tables via `ListTables` and registers each one under
/// `name.tables.<table_name>`.
///
/// # Arguments
/// * `session_ctx` - session context to register the table(s) into.
/// * `name` - the SQL table name (table mode) or catalog name (catalog mode) to expose.
/// * `connection_string` - the DynamoDB endpoint URL. For Amazon DynamoDB use
///   the regional endpoint (e.g. `https://dynamodb.us-east-1.amazonaws.com`);
///   for DynamoDB Local use `http://localhost:8000`.
/// * `options` - configuration options (see below).
/// * `read_write` - gates DML for every table registered.
/// * `hierarchy_level` - [`HierarchyLevel::Table`] (default) or [`HierarchyLevel::Catalog`].
///
/// # Options
/// * `table` - DynamoDB table name (required in table mode; not allowed in catalog mode).
/// * `partition_key` - partition (hash) key attribute name. Optional in table mode: the key
///   schema is read authoritatively via `DescribeTable`; this is only a fallback
///   used when `DescribeTable` is unavailable (e.g. restricted IAM permissions).
///   Ignored in catalog mode.
/// * `sort_key` - sort (range) key attribute name. Optional in table mode and likewise
///   auto-detected from `DescribeTable`; ignored in catalog mode.
/// * `region` - AWS region (optional, default `us-east-1`).
/// * `access_key_env` / `secret_key_env` - names of environment variables
///   holding static AWS credentials (optional). When omitted, the default AWS
///   credential provider chain is used.
/// * `columns` - explicit column schema as `name:type[,name:type…]` (types:
///   `string`, `int`, `float`, `bool`). Optional in table mode; ignored in catalog
///   mode because each DynamoDB table has its own attribute set.
/// * `allowed_tables` - Comma-separated table allow-list (catalog mode only). When
///   present, Skardi registers only those tables and skips `ListTables`.
pub async fn register_dynamodb_tables(
    session_ctx: &mut SessionContext,
    name: &str,
    connection_string: &str,
    options: Option<&HashMap<String, String>>,
    read_write: bool,
    hierarchy_level: HierarchyLevel,
) -> Result<()> {
    let mode_str = if read_write {
        "read-write"
    } else {
        "read-only"
    };
    validate_dynamodb_mode_options(name, hierarchy_level, options)?;
    match hierarchy_level {
        HierarchyLevel::Catalog => {
            register_dynamodb_catalog(
                session_ctx,
                name,
                connection_string,
                options,
                read_write,
                mode_str,
            )
            .await
        }
        HierarchyLevel::Table => {
            register_single_dynamodb_table(
                session_ctx,
                name,
                connection_string,
                options,
                read_write,
                mode_str,
            )
            .await
        }
    }
}

fn validate_dynamodb_mode_options(
    name: &str,
    hierarchy_level: HierarchyLevel,
    options: Option<&HashMap<String, String>>,
) -> Result<()> {
    let Some(opts) = options else {
        return Ok(());
    };

    match hierarchy_level {
        HierarchyLevel::Catalog => {
            for option in DYNAMODB_CATALOG_CONFLICT_OPTIONS {
                if opts.contains_key(*option) {
                    anyhow::bail!(
                        "DynamoDB catalog data source '{name}' cannot use option '{option}'; catalog mode supports endpoint/credential options plus optional 'allowed_tables'"
                    );
                }
            }
        }
        HierarchyLevel::Table => {
            if opts.contains_key("allowed_tables") {
                anyhow::bail!(
                    "DynamoDB table data source '{name}' cannot use catalog-only option 'allowed_tables'"
                );
            }
        }
    }

    Ok(())
}

/// Register one DynamoDB table under `name` in the default catalog.
async fn register_single_dynamodb_table(
    session_ctx: &mut SessionContext,
    name: &str,
    connection_string: &str,
    options: Option<&HashMap<String, String>>,
    read_write: bool,
    mode_str: &str,
) -> Result<()> {
    tracing::info!(
        source = %name,
        endpoint = %connection_string,
        read_write,
        "Registering DynamoDB table"
    );

    let opts = options.ok_or_else(|| {
        anyhow::anyhow!("DynamoDB data source '{name}' requires options (table, partition_key)")
    })?;

    let table = opts
        .get("table")
        .ok_or_else(|| anyhow::anyhow!("DynamoDB data source '{name}' requires 'table' option"))?;
    let region = opts
        .get("region")
        .cloned()
        .unwrap_or_else(|| "us-east-1".to_string());

    let client = build_client(connection_string, &region, opts).await?;

    // Prefer the table's authoritative key schema (this also auto-detects the
    // sort key). Fall back to the configured options if DescribeTable is
    // unavailable, e.g. under restricted IAM permissions.
    let key_schema = match describe_keys(&client, table).await {
        Ok(schema) => schema,
        Err(e) => {
            tracing::warn!(
                source = %name,
                error = %e,
                "DescribeTable failed; falling back to configured key options"
            );
            let pk = opts.get("partition_key").cloned().ok_or_else(|| {
                anyhow::anyhow!(
                    "DynamoDB data source '{name}': DescribeTable failed ({e}) and no 'partition_key' option was provided"
                )
            })?;
            DynamoKeySchema::fallback(pk, opts.get("sort_key").cloned())
        }
    };

    // An explicit `columns` option pins the schema; otherwise it is inferred by
    // sampling inside `DynamoTableProvider::new`.
    let declared_schema = match opts.get("columns") {
        Some(spec) => Some(
            parse_columns_option(
                spec,
                &key_schema.partition_key,
                key_schema.sort_key.as_deref(),
            )
            .with_context(|| format!("DynamoDB data source '{name}': invalid 'columns'"))?,
        ),
        None => None,
    };

    let provider = DynamoTableProvider::new_with_key_types(
        client,
        table,
        &key_schema.partition_key,
        Some(key_schema.partition_type.clone()),
        key_schema.sort_key.as_deref(),
        key_schema.sort_type.clone(),
        declared_schema,
        read_write,
    )
    .await
    .with_context(|| format!("Failed to create DynamoDB table provider for '{name}'"))?;

    session_ctx
        .register_table(name, Arc::new(provider))
        .with_context(|| format!("Failed to register DynamoDB table '{name}' with DataFusion"))?;

    tracing::info!(
        source = %name,
        table = %table,
        "Successfully registered DynamoDB table '{}' as '{}' ({})",
        table,
        name,
        mode_str
    );
    Ok(())
}

/// Register DynamoDB tables as a named DataFusion catalog.
///
/// Tables are discovered via `ListTables` unless `allowed_tables` is set. Registered
/// tables live under the fixed schema `tables`, so they are addressable as
/// `catalog.tables.<table_name>`.
///
/// Tables whose key schema or sampled schema cannot be determined are skipped with a
/// warning rather than failing the entire catalog registration, because DynamoDB
/// permissions or table states can vary across a large account.
async fn register_dynamodb_catalog(
    session_ctx: &mut SessionContext,
    catalog_name: &str,
    connection_string: &str,
    options: Option<&HashMap<String, String>>,
    read_write: bool,
    mode_str: &str,
) -> Result<()> {
    tracing::info!(
        catalog = %catalog_name,
        endpoint = %connection_string,
        read_write,
        "Registering DynamoDB catalog"
    );

    let opts = options.cloned().unwrap_or_default();
    let region = opts
        .get("region")
        .cloned()
        .unwrap_or_else(|| "us-east-1".to_string());

    let label = SourceLabel::new(
        DataSourceType::Dynamodb,
        HierarchyLevel::Catalog,
        catalog_name,
    );
    let client = retry_with_timeout(label, "DynamoDB client creation", || async {
        build_client(connection_string, &region, &opts).await
    })
    .await
    .with_context(|| format!("Failed to build DynamoDB client for catalog '{catalog_name}'"))?;

    let allowed_tables = parse_allowed_tables(Some(&opts))?;
    let explicit_allowlist = allowed_tables.is_some();
    let table_names = match allowed_tables {
        Some(allowed) => {
            tracing::info!(
                catalog = %catalog_name,
                allowed_tables = ?allowed,
                "Using DynamoDB catalog table allow-list"
            );
            allowed
        }
        None => list_dynamodb_tables(&client, label)
            .await
            .with_context(|| {
                format!("Failed to list DynamoDB tables for catalog '{catalog_name}'")
            })?,
    };

    if table_names.is_empty() {
        tracing::warn!(catalog = %catalog_name, "No DynamoDB tables found for catalog registration");
    }

    let discovered_count = table_names.len();
    let client = Arc::new(client);
    let schema_tables = table_names
        .into_iter()
        .map(|table_name| (DYNAMODB_CATALOG_SCHEMA.to_string(), table_name))
        .collect::<Vec<_>>();
    let required_schemas = vec![DYNAMODB_CATALOG_SCHEMA.to_string()];

    let build_table = |_: String, table_name: String| {
        let client = Arc::clone(&client);
        let catalog_name = catalog_name.to_string();
        async move {
            let op_name = format!("DynamoDB table provider build '{table_name}'");
            retry_with_timeout(label, &op_name, || {
                let client = Arc::clone(&client);
                let table_name = table_name.clone();
                let catalog_name = catalog_name.clone();
                async move {
                    build_dynamodb_table_provider(client, &table_name, read_write, &catalog_name)
                        .await
                        .map(|provider| Arc::new(provider) as Arc<dyn TableProvider>)
                }
            })
            .await
        }
    };

    let report = if explicit_allowlist {
        build_catalog_with_required_schemas(
            session_ctx,
            catalog_name,
            schema_tables,
            required_schemas,
            build_table,
        )
        .await
    } else {
        build_catalog_best_effort(
            session_ctx,
            catalog_name,
            schema_tables,
            required_schemas,
            build_table,
        )
        .await
    }
    .with_context(|| format!("Failed to build DynamoDB catalog '{catalog_name}'"))?;

    tracing::info!(
        "Successfully registered DynamoDB catalog '{}' with {} table(s), skipped {} of {} discovered ({})",
        catalog_name,
        report.registered,
        report.skipped,
        discovered_count,
        mode_str
    );
    Ok(())
}

/// Parse the comma-separated `allowed_tables` option.
fn parse_allowed_tables(options: Option<&HashMap<String, String>>) -> Result<Option<Vec<String>>> {
    let Some(value) = options.and_then(|opts| opts.get("allowed_tables")) else {
        return Ok(None);
    };
    let mut tables = value
        .split(',')
        .map(str::trim)
        .filter(|s| !s.is_empty())
        .map(str::to_string)
        .collect::<Vec<_>>();
    tables.sort();
    tables.dedup();
    if tables.is_empty() {
        anyhow::bail!(
            "DynamoDB catalog option 'allowed_tables' must be omitted or contain at least one table name"
        );
    } else {
        Ok(Some(tables))
    }
}

/// Build a [`DynamoTableProvider`] for `table_name`.
async fn build_dynamodb_table_provider(
    client: Arc<Client>,
    table_name: &str,
    read_write: bool,
    catalog_name: &str,
) -> Result<DynamoTableProvider> {
    let key_schema = describe_keys(&client, table_name).await.with_context(|| {
        format!("DynamoDB catalog '{catalog_name}': DescribeTable failed for '{table_name}'")
    })?;

    DynamoTableProvider::new_with_key_types(
        (*client).clone(),
        table_name,
        &key_schema.partition_key,
        Some(key_schema.partition_type.clone()),
        key_schema.sort_key.as_deref(),
        key_schema.sort_type.clone(),
        None,
        read_write,
    )
    .await
    .with_context(|| {
        format!("DynamoDB catalog '{catalog_name}': failed to create provider for '{table_name}'")
    })
}

/// List all DynamoDB table names accessible through `client`, following pagination.
async fn list_dynamodb_tables(client: &Client, label: SourceLabel<'_>) -> Result<Vec<String>> {
    let mut table_names = Vec::new();
    let mut last_evaluated: Option<String> = None;

    loop {
        let exclusive_start = last_evaluated.take();
        let resp = retry_with_timeout(label, "ListTables page", || {
            let exclusive_start = exclusive_start.clone();
            async move {
                let mut req = client.list_tables().limit(LIST_TABLES_PAGE_SIZE);
                if let Some(exclusive_start) = exclusive_start {
                    req = req.exclusive_start_table_name(exclusive_start);
                }
                req.send().await.with_context(|| "ListTables failed")
            }
        })
        .await?;
        table_names.extend(resp.table_names().iter().cloned());

        match resp.last_evaluated_table_name() {
            Some(name) if !name.is_empty() => last_evaluated = Some(name.to_string()),
            _ => break,
        }
    }

    table_names.sort();
    Ok(table_names)
}

/// Build a DynamoDB client from the endpoint, region, and credential options.
async fn build_client(
    endpoint: &str,
    region: &str,
    opts: &HashMap<String, String>,
) -> Result<Client> {
    let mut loader = aws_config::defaults(aws_config::BehaviorVersion::latest())
        .region(Region::new(region.to_string()))
        .endpoint_url(endpoint);

    if let (Some(ak_env), Some(sk_env)) = (opts.get("access_key_env"), opts.get("secret_key_env")) {
        let access_key = std::env::var(ak_env).with_context(|| {
            format!("Environment variable '{ak_env}' not found for DynamoDB access key")
        })?;
        let secret_key = std::env::var(sk_env).with_context(|| {
            format!("Environment variable '{sk_env}' not found for DynamoDB secret key")
        })?;
        let creds = Credentials::new(access_key, secret_key, None, None, "skardi-dynamodb");
        loader = loader.credentials_provider(creds);
    }

    let config = loader.load().await;
    Ok(Client::new(&config))
}

/// Map a DynamoDB key scalar type from `DescribeTable` to the Arrow type used by Skardi.
fn scalar_attribute_type_to_arrow_type(attribute_type: &ScalarAttributeType) -> DataType {
    match attribute_type {
        ScalarAttributeType::S => DataType::Utf8,
        ScalarAttributeType::N => DataType::Float64,
        ScalarAttributeType::B => DataType::Utf8,
        _ => DataType::Utf8,
    }
}

/// Read the table's authoritative key schema via `DescribeTable`, returning key
/// names and scalar types. This drives key-aware read planning without trusting
/// (possibly mismatched) configured key names, and auto-detects the sort key for
/// composite-key tables.
async fn describe_keys(client: &Client, table: &str) -> Result<DynamoKeySchema> {
    let out = client
        .describe_table()
        .table_name(table)
        .send()
        .await
        .with_context(|| format!("DescribeTable failed for '{table}'"))?;
    let table_desc = out
        .table()
        .ok_or_else(|| anyhow::anyhow!("DescribeTable for '{table}' returned no table metadata"))?;
    let key_schema = table_desc.key_schema();
    let attr_types = table_desc
        .attribute_definitions()
        .iter()
        .map(|attr| {
            (
                attr.attribute_name().to_string(),
                scalar_attribute_type_to_arrow_type(attr.attribute_type()),
            )
        })
        .collect::<HashMap<_, _>>();

    let mut partition_key: Option<String> = None;
    let mut sort_key: Option<String> = None;
    for element in key_schema {
        match element.key_type() {
            KeyType::Hash => partition_key = Some(element.attribute_name().to_string()),
            KeyType::Range => sort_key = Some(element.attribute_name().to_string()),
            _ => {}
        }
    }

    let partition_key = partition_key
        .ok_or_else(|| anyhow::anyhow!("table '{table}' has no HASH key in its key schema"))?;
    let partition_type = attr_types
        .get(&partition_key)
        .cloned()
        .unwrap_or(DataType::Utf8);
    let sort_type = sort_key
        .as_ref()
        .and_then(|sort_key| attr_types.get(sort_key).cloned());

    Ok(DynamoKeySchema::new(
        partition_key,
        partition_type,
        sort_key,
        sort_type,
    ))
}

/// Parse the `columns` option (`name:type[,name:type…]`) into an explicit
/// schema, ordered with the key attributes first (see `build_schema_fields`).
fn parse_columns_option(
    spec: &str,
    partition_key: &str,
    sort_key: Option<&str>,
) -> Result<SchemaRef> {
    let mut attrs: Vec<(String, DataType)> = Vec::new();
    for part in spec.split(',') {
        let part = part.trim();
        if part.is_empty() {
            continue;
        }
        let (name, ty) = part
            .split_once(':')
            .ok_or_else(|| anyhow::anyhow!("column '{part}' must be 'name:type'"))?;
        let dtype = match ty.trim().to_ascii_lowercase().as_str() {
            "string" | "str" | "utf8" | "text" => DataType::Utf8,
            "int" | "integer" | "bigint" | "int64" | "long" => DataType::Int64,
            "float" | "double" | "float64" | "number" | "num" => DataType::Float64,
            "bool" | "boolean" => DataType::Boolean,
            other => anyhow::bail!(
                "column '{}' has unknown type '{other}' (use string, int, float, or bool)",
                name.trim()
            ),
        };
        attrs.push((name.trim().to_string(), dtype));
    }
    if attrs.is_empty() {
        anyhow::bail!("'columns' option is empty");
    }
    Ok(Arc::new(build_schema_fields(
        partition_key,
        sort_key,
        &attrs,
    )))
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::sources::hierarchy::HierarchyLevel;
    use arrow::array::Array;
    use datafusion::common::ScalarValue;
    use datafusion::logical_expr::dml::InsertOp;
    use datafusion::logical_expr::{BinaryExpr, col, lit};
    use datafusion::physical_plan::empty::EmptyExec;

    fn n(s: &str) -> AttributeValue {
        AttributeValue::N(s.to_string())
    }

    #[test]
    fn attribute_type_inference() {
        assert_eq!(
            attribute_value_to_arrow_type(&AttributeValue::S("x".into())),
            DataType::Utf8
        );
        // Numbers always infer as Float64 — a single sampled whole number can't
        // prove a column is integer-only, and later fractional values would be
        // truncated or dropped by the Inexact re-filter.
        assert_eq!(attribute_value_to_arrow_type(&n("42")), DataType::Float64);
        assert_eq!(attribute_value_to_arrow_type(&n("4.5")), DataType::Float64);
        assert_eq!(
            attribute_value_to_arrow_type(&AttributeValue::Bool(true)),
            DataType::Boolean
        );
        assert_eq!(
            attribute_value_to_arrow_type(&AttributeValue::Null(true)),
            DataType::Utf8
        );
    }

    #[test]
    fn number_coercions() {
        assert_eq!(av_to_i64(&n("7")), Some(7));
        // Fractional N does not silently truncate into an Int64 column; it
        // becomes NULL so the row can't disappear via the Inexact re-filter.
        assert_eq!(av_to_i64(&n("7.9")), None);
        // Cross-type coercion is refused: a string is never parsed into a
        // numeric column (would violate the pushdown superset contract).
        assert_eq!(av_to_i64(&AttributeValue::S("7".into())), None);
        assert_eq!(av_to_f64(&n("2.5")), Some(2.5));
        assert_eq!(av_to_f64(&AttributeValue::S("2.5".into())), None);
        assert_eq!(av_to_bool(&AttributeValue::Bool(false)), Some(false));
        assert_eq!(av_to_bool(&n("1")), None);
    }

    #[test]
    fn null_bearing_column_builds_nullable_array() {
        let values = vec![
            Some(AttributeValue::S("a".into())),
            None,
            Some(AttributeValue::S("c".into())),
        ];
        let arr = attribute_values_to_arrow_array(&values, &DataType::Utf8);
        assert_eq!(arr.len(), 3);
        assert!(arr.is_null(1));
        assert!(!arr.is_null(0));
    }

    #[test]
    fn empty_column_builds_empty_array() {
        let arr = attribute_values_to_arrow_array(&[], &DataType::Int64);
        assert_eq!(arr.len(), 0);
    }

    #[test]
    fn pushable_filter_detection() {
        let pushable = col("a").eq(lit(1i64));
        assert!(is_pushable_binary_filter(&pushable));
        // column-to-column is not pushable
        let not_pushable = col("a").eq(col("b"));
        assert!(!is_pushable_binary_filter(&not_pushable));
    }

    #[test]
    fn filter_expression_emits_placeholders() {
        let filters = vec![
            col("category").eq(lit("Electronics")),
            col("price").gt(lit(100i64)),
        ];
        let f = build_filter_expression(&filters)
            .expect("buildable")
            .expect("non-empty");
        assert!(f.expression.contains(" AND "));
        assert_eq!(f.names.len(), 2);
        assert_eq!(f.values.len(), 2);
        // every #name placeholder in the expression has a binding
        for ph in f.names.keys() {
            assert!(f.expression.contains(ph.as_str()));
        }
    }

    #[test]
    fn literal_on_left_flips_operator() {
        // 100 < price  ⇒  #name > :val
        let expr = Expr::BinaryExpr(BinaryExpr::new(
            Box::new(lit(100i64)),
            Operator::Lt,
            Box::new(col("price")),
        ));
        let f = build_filter_expression(&[expr])
            .expect("buildable")
            .expect("non-empty");
        assert!(f.expression.contains('>'), "got: {}", f.expression);
    }

    #[test]
    fn empty_filter_list_is_none() {
        assert!(build_filter_expression(&[]).expect("ok").is_none());
    }

    #[test]
    fn scalar_conversions() {
        assert!(matches!(
            scalar_to_attribute_value(&ScalarValue::Utf8(Some("hi".into()))).unwrap(),
            AttributeValue::S(s) if s == "hi"
        ));
        assert!(matches!(
            scalar_to_attribute_value(&ScalarValue::Int64(Some(5))).unwrap(),
            AttributeValue::N(s) if s == "5"
        ));
        assert!(matches!(
            scalar_to_attribute_value(&ScalarValue::Boolean(Some(true))).unwrap(),
            AttributeValue::Bool(true)
        ));
    }

    #[test]
    fn update_expression_shape() {
        let sets = vec![
            ("price".to_string(), n("9.99")),
            ("in_stock".to_string(), AttributeValue::Bool(true)),
        ];
        let (expr, names, values) = build_update_expression(&sets);
        assert!(expr.starts_with("SET "));
        assert!(expr.contains(", "));
        assert_eq!(names.len(), 2);
        assert_eq!(values.len(), 2);
    }

    #[test]
    fn update_expression_placeholders_cannot_collide_with_pk_guard() {
        // UPDATE execution reserves "#pk" for its attribute_exists() resurrect
        // guard, so SET placeholders must stay in the #s/:s namespace — even
        // for a column literally named "pk".
        let sets = vec![("pk".to_string(), n("1")), ("note".to_string(), n("2"))];
        let (expr, names, values) = build_update_expression(&sets);
        assert_eq!(expr, "SET #s0 = :s0, #s1 = :s1");
        assert!(!names.contains_key("#pk"));
        assert!(names.keys().all(|k| k.starts_with("#s")));
        assert!(values.keys().all(|k| k.starts_with(":s")));
    }

    #[test]
    fn classify_single_key_eq_is_get_item() {
        let filters = vec![col("product_id").eq(lit("PROD001"))];
        match classify_read("product_id", None, &filters).unwrap() {
            DynamoRead::GetItem { key } => {
                assert_eq!(key.len(), 1);
                assert!(key.contains_key("product_id"));
            }
            _ => panic!("expected GetItem for a full single-key lookup"),
        }
    }

    #[test]
    fn classify_composite_full_key_is_get_item() {
        let filters = vec![col("pk").eq(lit("A")), col("sk").eq(lit("B"))];
        match classify_read("pk", Some("sk"), &filters).unwrap() {
            DynamoRead::GetItem { key } => assert_eq!(key.len(), 2),
            _ => panic!("expected GetItem when both key parts are pinned"),
        }
    }

    #[test]
    fn classify_partition_eq_sort_range_is_query() {
        let filters = vec![col("pk").eq(lit("A")), col("sk").gt(lit(5i64))];
        match classify_read("pk", Some("sk"), &filters).unwrap() {
            DynamoRead::Query { key_condition } => {
                assert!(key_condition.expression.contains("#k0 = :k0"));
                assert!(key_condition.expression.contains(" AND "));
                assert!(key_condition.expression.contains('>'));
                assert_eq!(key_condition.names.len(), 2);
            }
            _ => panic!("expected Query for partition-eq + sort-range"),
        }
    }

    #[test]
    fn classify_partition_eq_only_on_composite_is_query() {
        let filters = vec![col("pk").eq(lit("A"))];
        match classify_read("pk", Some("sk"), &filters).unwrap() {
            DynamoRead::Query { key_condition } => {
                assert_eq!(key_condition.expression, "#k0 = :k0");
                assert_eq!(key_condition.names.len(), 1);
            }
            _ => panic!("expected partition-only Query on a composite-key table"),
        }
    }

    #[test]
    fn classify_partition_non_eq_is_scan() {
        // A non-equality predicate on the partition key cannot drive Query/GetItem.
        let filters = vec![col("product_id").gt(lit("PROD000"))];
        assert!(matches!(
            classify_read("product_id", None, &filters).unwrap(),
            DynamoRead::Scan { .. }
        ));
    }

    #[test]
    fn classify_non_key_filter_is_scan() {
        let filters = vec![col("category").eq(lit("Electronics"))];
        match classify_read("product_id", None, &filters).unwrap() {
            DynamoRead::Scan { filter } => assert!(filter.is_some()),
            _ => panic!("expected Scan for a non-key filter"),
        }
    }

    #[test]
    fn classify_sort_key_noteq_is_not_a_key_condition() {
        // `sk <> B` is pushable as a filter but illegal in a KeyConditionExpression,
        // so it must NOT become a key condition. With the partition key pinned this
        // yields a partition-only Query (the `<>` is left for DataFusion).
        let filters = vec![col("pk").eq(lit("A")), col("sk").not_eq(lit("B"))];
        match classify_read("pk", Some("sk"), &filters).unwrap() {
            DynamoRead::Query { key_condition } => {
                assert_eq!(key_condition.expression, "#k0 = :k0");
            }
            _ => panic!("expected partition-only Query; `<>` must not become a key condition"),
        }
    }

    // ─── DML planning (key-aware routing + guard) ───────────────────────────

    #[test]
    fn classify_dml_partition_eq_routes_query_with_residual() {
        // pk pinned + a non-key predicate → Query, with the non-key predicate
        // carried as a residual FilterExpression (a Query filter can't touch keys).
        let filters = vec![col("pk").eq(lit("A")), col("category").eq(lit("x"))];
        match classify_dml("pk", None, &filters).unwrap() {
            DmlKeyPlan::Query {
                key_condition,
                residual,
            } => {
                assert_eq!(key_condition.expression, "#k0 = :k0");
                let residual = residual.expect("non-key predicate becomes a residual filter");
                assert!(residual.expression.contains("#n0"));
            }
            _ => panic!("expected Query when the partition key is pinned"),
        }
    }

    #[test]
    fn classify_dml_no_partition_routes_scan() {
        // Partition key not pinned → Scan carrying the full predicate set.
        let filters = vec![col("category").eq(lit("x"))];
        match classify_dml("pk", None, &filters).unwrap() {
            DmlKeyPlan::Scan { filter } => assert!(filter.is_some()),
            _ => panic!("expected Scan when the partition key is not pinned"),
        }
    }

    #[test]
    fn classify_dml_sort_noteq_forces_scan() {
        // `sk <> B` can live in neither a KeyCondition nor a Query filter, so the
        // whole DML must Scan (a Scan filter may reference key attributes).
        let filters = vec![col("pk").eq(lit("A")), col("sk").not_eq(lit("B"))];
        match classify_dml("pk", Some("sk"), &filters).unwrap() {
            DmlKeyPlan::Scan { filter } => {
                let f = filter.expect("filter present");
                // Both predicates are expressed (two placeholders).
                assert_eq!(f.names.len(), 2);
            }
            _ => panic!("expected Scan when a sort predicate is inexpressible on a Query"),
        }
    }

    #[test]
    fn convertible_pushdown_excludes_inconvertible_literal() {
        // Pushable-shaped but with an inconvertible literal → not convertible.
        let ok = col("a").eq(lit(1i64));
        assert!(is_convertible_pushdown(&ok));
        let ts = Expr::BinaryExpr(BinaryExpr::new(
            Box::new(col("a")),
            Operator::Eq,
            Box::new(Expr::Literal(
                ScalarValue::TimestampNanosecond(Some(0), None),
                None,
            )),
        ));
        assert!(is_pushable_binary_filter(&ts));
        assert!(!is_convertible_pushdown(&ts));
    }

    #[test]
    fn parse_columns_option_orders_keys_first() {
        let schema = parse_columns_option(
            "price:float, name:string, product_id:string",
            "product_id",
            None,
        )
        .expect("valid columns");
        // Key comes first and is non-nullable; declared cols follow, nullable.
        assert_eq!(schema.field(0).name(), "product_id");
        assert!(!schema.field(0).is_nullable());
        assert_eq!(schema.fields().len(), 3);
        assert_eq!(schema.field(1).data_type(), &DataType::Float64);
    }

    #[test]
    fn parse_columns_option_rejects_bad_type() {
        assert!(parse_columns_option("x:widget", "id", None).is_err());
        assert!(parse_columns_option("noColon", "id", None).is_err());
    }

    #[tokio::test]
    async fn delete_with_non_pushable_predicate_is_rejected() {
        // The guard must fire at plan time (no network) rather than silently
        // dropping the OR and deleting every row. Uses an explicit schema so
        // registration doesn't touch DynamoDB.
        let schema = Arc::new(Schema::new(vec![
            Field::new("id", DataType::Utf8, false),
            Field::new("name", DataType::Utf8, true),
        ]));
        let mut opts = HashMap::new();
        opts.insert("region".to_string(), "us-east-1".to_string());
        let client = build_client("http://localhost:8000", "us-east-1", &opts)
            .await
            .expect("client");
        let provider = DynamoTableProvider::new(client, "t", "id", None, Some(schema), true)
            .await
            .expect("provider");
        let ctx = SessionContext::new();
        ctx.register_table("t", Arc::new(provider))
            .expect("register");

        let err = ctx
            .sql("DELETE FROM t WHERE id = 'A' OR name = 'x'")
            .await
            .expect("logical plan")
            .collect()
            .await
            .expect_err("non-pushable DELETE predicate must be rejected");
        assert!(
            err.to_string().contains("pushable comparison"),
            "unexpected error: {err}"
        );
    }

    #[test]
    fn missing_options_errors() {
        let rt = tokio::runtime::Runtime::new().unwrap();
        rt.block_on(async {
            let mut ctx = SessionContext::new();
            let err = register_dynamodb_tables(
                &mut ctx,
                "ddb",
                "http://localhost:8000",
                None,
                false,
                HierarchyLevel::Table,
            )
            .await
            .unwrap_err();
            assert!(err.to_string().contains("requires options"));
        });
    }

    #[test]
    fn missing_table_option_errors() {
        let rt = tokio::runtime::Runtime::new().unwrap();
        rt.block_on(async {
            let mut ctx = SessionContext::new();
            let mut opts = HashMap::new();
            opts.insert("partition_key".to_string(), "id".to_string());
            let err = register_dynamodb_tables(
                &mut ctx,
                "ddb",
                "http://localhost:8000",
                Some(&opts),
                false,
                HierarchyLevel::Table,
            )
            .await
            .unwrap_err();
            assert!(err.to_string().contains("'table'"));
        });
    }

    #[test]
    fn table_mode_explicitly_requires_options() {
        // Explicit table mode (the default) still requires options so the provider
        // can read the `table` name and credentials.
        let rt = tokio::runtime::Runtime::new().unwrap();
        rt.block_on(async {
            let mut ctx = SessionContext::new();
            let err = register_dynamodb_tables(
                &mut ctx,
                "ddb",
                "http://localhost:8000",
                None,
                false,
                HierarchyLevel::Table,
            )
            .await
            .unwrap_err();
            assert!(err.to_string().contains("requires options"));
        });
    }

    #[test]
    fn parse_allowed_tables_absent_means_all_tables() {
        assert!(parse_allowed_tables(None).unwrap().is_none());
        assert!(
            parse_allowed_tables(Some(&HashMap::new()))
                .unwrap()
                .is_none()
        );
    }

    #[test]
    fn parse_allowed_tables_trims_sorts_and_deduplicates() {
        let mut opts = HashMap::new();
        opts.insert(
            "allowed_tables".to_string(),
            " orders, products,orders , inventory ".to_string(),
        );

        let tables = parse_allowed_tables(Some(&opts))
            .expect("parse allowed_tables")
            .expect("allowed tables");
        assert_eq!(tables, vec!["inventory", "orders", "products"]);
    }

    #[test]
    fn parse_allowed_tables_empty_segments_error() {
        let mut opts = HashMap::new();
        opts.insert("allowed_tables".to_string(), " , , ".to_string());

        let err = parse_allowed_tables(Some(&opts)).unwrap_err();
        assert!(err.to_string().contains("allowed_tables"));
    }

    #[test]
    fn catalog_mode_rejects_table_scoped_options_at_provider_boundary() {
        for option in DYNAMODB_CATALOG_CONFLICT_OPTIONS {
            let mut opts = HashMap::new();
            opts.insert((*option).to_string(), "value".to_string());

            let err = validate_dynamodb_mode_options("ddb", HierarchyLevel::Catalog, Some(&opts))
                .unwrap_err();
            assert!(err.to_string().contains(option), "got: {err}");
        }
    }

    #[test]
    fn table_mode_rejects_catalog_only_allowed_tables() {
        let mut opts = HashMap::new();
        opts.insert("allowed_tables".to_string(), "products".to_string());

        let err =
            validate_dynamodb_mode_options("ddb", HierarchyLevel::Table, Some(&opts)).unwrap_err();
        assert!(err.to_string().contains("allowed_tables"), "got: {err}");
    }

    fn mem_table_provider() -> Arc<dyn TableProvider> {
        let schema: SchemaRef = Arc::new(Schema::new(vec![Field::new("id", DataType::Utf8, true)]));
        let batch = RecordBatch::new_empty(schema.clone());
        Arc::new(datafusion::datasource::MemTable::try_new(schema, vec![vec![batch]]).unwrap())
            as Arc<dyn TableProvider>
    }

    #[tokio::test]
    async fn catalog_registration_skips_failed_tables_and_uses_fixed_schema() {
        let ctx = SessionContext::new();
        let report = build_catalog_best_effort(
            &ctx,
            "ddb",
            vec![
                (DYNAMODB_CATALOG_SCHEMA.to_string(), "orders".to_string()),
                (DYNAMODB_CATALOG_SCHEMA.to_string(), "broken".to_string()),
            ],
            vec![DYNAMODB_CATALOG_SCHEMA.to_string()],
            |_, table_name| async move {
                if table_name == "broken" {
                    Err(anyhow::anyhow!("DescribeTable failed"))
                } else {
                    Ok(mem_table_provider())
                }
            },
        )
        .await
        .expect("register catalog");
        assert_eq!(report.registered, 1);
        assert_eq!(report.skipped, 1);

        let df = ctx
            .sql("SELECT * FROM ddb.tables.orders")
            .await
            .expect("orders is registered under fixed schema");
        let batches = df.collect().await.expect("collect orders");
        assert_eq!(batches.iter().map(|b| b.num_rows()).sum::<usize>(), 0);

        let err = ctx
            .sql("SELECT * FROM ddb.tables.broken")
            .await
            .expect_err("failed provider must be skipped");
        assert!(err.to_string().contains("broken"), "got: {err}");
    }

    #[tokio::test]
    async fn catalog_registration_all_failed_is_still_best_effort() {
        let ctx = SessionContext::new();
        let report = build_catalog_best_effort(
            &ctx,
            "ddb",
            vec![
                (DYNAMODB_CATALOG_SCHEMA.to_string(), "products".to_string()),
                (DYNAMODB_CATALOG_SCHEMA.to_string(), "orders".to_string()),
            ],
            vec![DYNAMODB_CATALOG_SCHEMA.to_string()],
            |_, table_name| async move { Err(anyhow::anyhow!("{table_name} unavailable")) },
        )
        .await
        .expect("register catalog");
        assert_eq!(report.registered, 0);
        assert_eq!(report.skipped, 2);
        assert!(ctx.catalog("ddb").is_some(), "catalog should still exist");

        let err = ctx
            .sql("SELECT * FROM ddb.tables.orders")
            .await
            .expect_err("all failed tables should be absent");
        assert!(
            err.to_string().contains("orders") && !err.to_string().contains("schema not found"),
            "got: {err}"
        );
    }

    #[tokio::test]
    async fn catalog_registration_empty_input_registers_empty_catalog() {
        let ctx = SessionContext::new();
        let report = build_catalog_best_effort(
            &ctx,
            "ddb",
            Vec::new(),
            vec![DYNAMODB_CATALOG_SCHEMA.to_string()],
            |_, _| async { Ok(mem_table_provider()) },
        )
        .await
        .expect("register catalog");

        assert_eq!(report.registered, 0);
        assert_eq!(report.skipped, 0);
        assert!(ctx.catalog("ddb").is_some(), "empty catalog should exist");
        assert!(
            ctx.catalog("ddb")
                .and_then(|catalog| catalog.schema(DYNAMODB_CATALOG_SCHEMA))
                .is_some(),
            "fixed schema should exist even without tables"
        );
    }

    #[tokio::test]
    async fn catalog_registration_fail_fast_reports_allowlist_failures() {
        let ctx = SessionContext::new();
        let err = build_catalog_with_required_schemas(
            &ctx,
            "ddb",
            vec![(DYNAMODB_CATALOG_SCHEMA.to_string(), "ordersx".to_string())],
            vec![DYNAMODB_CATALOG_SCHEMA.to_string()],
            |_, table_name| async move { Err(anyhow::anyhow!("{table_name} unavailable")) },
        )
        .await
        .expect_err("explicit allow-list should fail fast");

        assert!(err.to_string().contains("ordersx"), "got: {err}");
    }

    #[tokio::test]
    #[ignore = "requires DynamoDB Local on :8000"]
    async fn catalog_mode_accepts_empty_options() {
        // Catalog mode discovers tables via ListTables, so it does not need a
        // `table` option. This should reach the network layer without failing
        // option validation.
        let mut ctx = SessionContext::new();
        register_dynamodb_tables(
            &mut ctx,
            "ddb",
            "http://localhost:8000",
            None,
            false,
            HierarchyLevel::Catalog,
        )
        .await
        .expect("catalog registration should not fail due to missing options");
    }

    async fn ensure_catalog_orders_table(client: &Client) {
        use aws_sdk_dynamodb::types::{
            AttributeDefinition, BillingMode, KeySchemaElement, ScalarAttributeType,
        };

        if client
            .describe_table()
            .table_name("orders")
            .send()
            .await
            .is_err()
        {
            client
                .create_table()
                .table_name("orders")
                .attribute_definitions(
                    AttributeDefinition::builder()
                        .attribute_name("order_id")
                        .attribute_type(ScalarAttributeType::S)
                        .build()
                        .unwrap(),
                )
                .key_schema(
                    KeySchemaElement::builder()
                        .attribute_name("order_id")
                        .key_type(KeyType::Hash)
                        .build()
                        .unwrap(),
                )
                .billing_mode(BillingMode::PayPerRequest)
                .send()
                .await
                .expect("create orders table");
        }

        let put = |order_id: &str, product_id: &str, quantity: i64, status: &str| {
            let client = client.clone();
            let order_id = order_id.to_string();
            let product_id = product_id.to_string();
            let status = status.to_string();
            async move {
                client
                    .put_item()
                    .table_name("orders")
                    .item("order_id", AttributeValue::S(order_id))
                    .item("product_id", AttributeValue::S(product_id))
                    .item("quantity", AttributeValue::N(quantity.to_string()))
                    .item("status", AttributeValue::S(status))
                    .send()
                    .await
                    .expect("put order");
            }
        };

        put("ORD001", "PROD001", 2, "paid").await;
        put("ORD002", "PROD002", 1, "pending").await;
    }

    #[tokio::test]
    #[ignore = "requires DynamoDB Local on :8000 with `products` and `orders` tables"]
    async fn catalog_mode_registers_tables_under_fixed_schema() {
        let mut ctx = SessionContext::new();
        let mut client_opts = HashMap::new();
        client_opts.insert("region".to_string(), "us-east-1".to_string());

        let client = build_client("http://localhost:8000", "us-east-1", &client_opts)
            .await
            .expect("client");
        ensure_catalog_orders_table(&client).await;

        register_dynamodb_tables(
            &mut ctx,
            "ddb",
            "http://localhost:8000",
            None,
            false,
            HierarchyLevel::Catalog,
        )
        .await
        .expect("register catalog");

        // DynamoDB has no native schema layer; Skardi exposes every discovered
        // table under the fixed `tables` schema.
        let df = ctx
            .sql("SELECT * FROM ddb.tables.products LIMIT 1")
            .await
            .expect("plan products");
        let batches = df.collect().await.expect("collect products");
        assert!(
            batches.iter().map(|b| b.num_rows()).sum::<usize>() >= 1,
            "expected products table under ddb.tables"
        );

        let df = ctx
            .sql("SELECT * FROM ddb.tables.orders LIMIT 1")
            .await
            .expect("plan orders");
        let batches = df.collect().await.expect("collect orders");
        assert!(
            batches.iter().map(|b| b.num_rows()).sum::<usize>() >= 1,
            "expected orders table under ddb.tables"
        );
    }

    // ─── Schema shaping (build_schema_fields) ───────────────────────────────

    #[test]
    fn schema_fields_put_keys_first_and_nullable_rest() {
        // Attributes are given out of order and include the keys; the builder must
        // emit partition key, then sort key (both non-nullable), then the rest in
        // declared order (nullable), deduping any attribute that repeats a key.
        let attrs = vec![
            ("name".to_string(), DataType::Utf8),
            ("sk".to_string(), DataType::Int64),
            ("price".to_string(), DataType::Float64),
            ("pk".to_string(), DataType::Utf8),
        ];
        let schema = build_schema_fields("pk", Some("sk"), &attrs);
        let names: Vec<&str> = schema.fields().iter().map(|f| f.name().as_str()).collect();
        assert_eq!(names, vec!["pk", "sk", "name", "price"]);
        assert!(!schema.field(0).is_nullable(), "partition key non-nullable");
        assert!(!schema.field(1).is_nullable(), "sort key non-nullable");
        assert!(schema.field(2).is_nullable(), "non-key column nullable");
        // The sort key's declared type is honored even though it came from attrs.
        assert_eq!(schema.field(1).data_type(), &DataType::Int64);
    }

    #[test]
    fn schema_fields_default_key_type_is_utf8_when_unsampled() {
        // A key never seen among the sampled attributes falls back to Utf8 rather
        // than being dropped (an empty table still needs its key columns).
        let schema = build_schema_fields("pk", None, &[]);
        assert_eq!(schema.fields().len(), 1);
        assert_eq!(schema.field(0).name(), "pk");
        assert_eq!(schema.field(0).data_type(), &DataType::Utf8);
    }

    #[test]
    fn schema_fields_preserve_described_key_types_when_unsampled() {
        let key_schema = DynamoKeySchema::new(
            "pk".to_string(),
            scalar_attribute_type_to_arrow_type(&ScalarAttributeType::N),
            Some("sk".to_string()),
            Some(scalar_attribute_type_to_arrow_type(&ScalarAttributeType::S)),
        );
        let attrs = vec![
            (
                key_schema.partition_key.clone(),
                key_schema.partition_type.clone(),
            ),
            (
                key_schema.sort_key.clone().expect("sort key"),
                key_schema.sort_type.clone().expect("sort key type"),
            ),
        ];
        let schema = build_schema_fields("pk", Some("sk"), &attrs);

        assert_eq!(schema.fields().len(), 2);
        assert_eq!(schema.field(0).name(), "pk");
        assert_eq!(schema.field(0).data_type(), &DataType::Float64);
        assert!(!schema.field(0).is_nullable());
        assert_eq!(schema.field(1).name(), "sk");
        assert_eq!(schema.field(1).data_type(), &DataType::Utf8);
        assert!(!schema.field(1).is_nullable());
    }

    #[test]
    fn sampled_attribute_merge_is_sorted_and_first_type_wins() {
        let items = vec![
            HashMap::from([
                ("zeta".to_string(), AttributeValue::S("x".into())),
                ("alpha".to_string(), n("1")),
            ]),
            HashMap::from([
                // Repeats `alpha` with a different type: the first observation
                // fixed it, so this one must not flip the column type.
                ("alpha".to_string(), AttributeValue::S("later".into())),
                ("mid".to_string(), AttributeValue::Bool(true)),
            ]),
        ];
        let attrs = merge_sampled_attributes(&items);
        let names: Vec<&str> = attrs.iter().map(|(name, _)| name.as_str()).collect();
        // HashMap iteration order is arbitrary, so the merged list must come
        // back sorted for the inferred schema to be identical across runs.
        assert_eq!(names, vec!["alpha", "mid", "zeta"]);
        assert_eq!(attrs[0].1, DataType::Float64, "first-seen type wins");
        assert_eq!(attrs[1].1, DataType::Boolean);
        assert_eq!(attrs[2].1, DataType::Utf8);
    }

    // ─── Item ⇄ RecordBatch conversion ──────────────────────────────────────

    #[test]
    fn items_to_batch_fills_missing_attributes_with_null() {
        let schema: SchemaRef = Arc::new(Schema::new(vec![
            Field::new("id", DataType::Utf8, false),
            Field::new("price", DataType::Float64, true),
        ]));
        // Second item omits `price` entirely — it must become a NULL cell, not a
        // dropped row or a shifted column.
        let items = vec![
            HashMap::from([
                ("id".to_string(), AttributeValue::S("A".into())),
                ("price".to_string(), n("9.5")),
            ]),
            HashMap::from([("id".to_string(), AttributeValue::S("B".into()))]),
        ];
        let batch = items_to_batch(items, &schema).expect("batch");
        assert_eq!(batch.num_rows(), 2);
        let ids = batch
            .column(0)
            .as_any()
            .downcast_ref::<StringArray>()
            .unwrap();
        let prices = batch
            .column(1)
            .as_any()
            .downcast_ref::<Float64Array>()
            .unwrap();
        assert_eq!(ids.value(0), "A");
        assert_eq!(ids.value(1), "B");
        assert_eq!(prices.value(0), 9.5);
        assert!(prices.is_null(1), "missing attribute reads as NULL");
    }

    #[test]
    fn record_batch_to_items_round_trips_and_omits_nulls() {
        // A batch with a NULL cell must produce an item that simply lacks that
        // attribute (DynamoDB has no typed NULL columns), while typed cells map
        // back to the matching AttributeValue variant.
        let schema = Schema::new(vec![
            Field::new("id", DataType::Utf8, false),
            Field::new("qty", DataType::Int64, true),
            Field::new("active", DataType::Boolean, true),
        ]);
        let ids: StringArray = vec![Some("A"), Some("B")].into_iter().collect();
        let qty: Int64Array = vec![Some(7i64), None].into_iter().collect();
        let active: BooleanArray = vec![Some(true), Some(false)].into_iter().collect();
        let batch = RecordBatch::try_new(
            Arc::new(schema.clone()),
            vec![Arc::new(ids), Arc::new(qty), Arc::new(active)],
        )
        .unwrap();

        let items = record_batch_to_items(&batch, &schema).expect("items");
        assert_eq!(items.len(), 2);
        assert!(matches!(items[0].get("id"), Some(AttributeValue::S(s)) if s == "A"));
        assert!(matches!(items[0].get("qty"), Some(AttributeValue::N(n)) if n == "7"));
        assert!(matches!(
            items[0].get("active"),
            Some(AttributeValue::Bool(true))
        ));
        // Row 1 had a NULL qty → the attribute is absent, not a typed NULL.
        assert!(
            !items[1].contains_key("qty"),
            "NULL cell omits the attribute"
        );
        assert!(matches!(
            items[1].get("active"),
            Some(AttributeValue::Bool(false))
        ));
    }

    #[test]
    fn arrow_array_builders_cover_numeric_and_bool_types() {
        // Only the Utf8 path was covered before; exercise the Int64/Float64/Boolean
        // arms (including a NULL passthrough) so a regression in any is caught.
        let int_vals = vec![Some(n("3")), None];
        let arr = attribute_values_to_arrow_array(&int_vals, &DataType::Int64);
        let ints = arr.as_any().downcast_ref::<Int64Array>().unwrap();
        assert_eq!(ints.value(0), 3);
        assert!(ints.is_null(1));

        let bool_vals = vec![Some(AttributeValue::Bool(true)), Some(n("1"))];
        let arr = attribute_values_to_arrow_array(&bool_vals, &DataType::Boolean);
        let bools = arr.as_any().downcast_ref::<BooleanArray>().unwrap();
        assert!(bools.value(0));
        // A non-bool attribute in a Boolean column coerces to NULL, never `true`.
        assert!(
            bools.is_null(1),
            "non-bool attribute is NULL in a Boolean column"
        );
    }

    #[test]
    fn av_to_string_formats_scalars_and_maps_null_to_none() {
        assert_eq!(
            av_to_string(&AttributeValue::S("hi".into())).as_deref(),
            Some("hi")
        );
        assert_eq!(av_to_string(&n("9.99")).as_deref(), Some("9.99"));
        assert_eq!(
            av_to_string(&AttributeValue::Bool(true)).as_deref(),
            Some("true")
        );
        // An explicit DynamoDB NULL is None (an Arrow null), not "".
        assert_eq!(av_to_string(&AttributeValue::Null(true)), None);
    }

    #[test]
    fn explicit_null_attribute_is_arrow_null_in_string_columns() {
        // `UPDATE ... SET col = NULL` stores AttributeValue::Null; reading it
        // back must yield a NULL cell, not the empty string "".
        let values = vec![
            Some(AttributeValue::S("a".into())),
            Some(AttributeValue::Null(true)),
        ];
        let arr = attribute_values_to_arrow_array(&values, &DataType::Utf8);
        let strs = arr.as_any().downcast_ref::<StringArray>().unwrap();
        assert_eq!(strs.value(0), "a");
        assert!(strs.is_null(1), "explicit NULL reads as NULL, not \"\"");

        // The fallback arm (unsupported column types render as strings)
        // applies the same rule.
        let arr = attribute_values_to_arrow_array(&values, &DataType::Date32);
        assert!(!arr.is_null(0));
        assert!(arr.is_null(1));
    }

    // ─── Projection / key-condition expression builders ─────────────────────

    #[test]
    fn projection_expression_names_every_field() {
        let schema = Schema::new(vec![
            Field::new("id", DataType::Utf8, false),
            Field::new("price", DataType::Float64, true),
        ]);
        let (expr, names) = build_projection_expression(&schema);
        // Uses the #p namespace so it can share a request with #n/#k/:v bindings.
        assert_eq!(expr, "#p0, #p1");
        assert_eq!(names.len(), 2);
        assert_eq!(names.get("#p0").map(String::as_str), Some("id"));
        assert_eq!(names.get("#p1").map(String::as_str), Some("price"));
    }

    #[test]
    fn key_condition_uses_k_namespace_and_folds_sort_cond() {
        // Partition-only.
        let f = build_key_condition("pk", AttributeValue::S("A".into()), "sk", None);
        assert_eq!(f.expression, "#k0 = :k0");
        assert_eq!(f.names.get("#k0").map(String::as_str), Some("pk"));
        assert!(f.values.contains_key(":k0"));

        // Partition + sort range → second clause on the #k1/:k1 pair.
        let f = build_key_condition(
            "pk",
            AttributeValue::S("A".into()),
            "sk",
            Some((Operator::Gt, n("5"))),
        );
        assert_eq!(f.expression, "#k0 = :k0 AND #k1 > :k1");
        assert_eq!(f.names.get("#k1").map(String::as_str), Some("sk"));
        assert!(f.values.contains_key(":k1"));
    }

    // ─── Operator / normalization helpers ───────────────────────────────────

    #[test]
    fn normalize_binary_flips_left_literal_and_rejects_unusable() {
        // `5 < price` normalizes to `(price, >, 5)` with the column on the left.
        let expr = Expr::BinaryExpr(BinaryExpr::new(
            Box::new(lit(5i64)),
            Operator::Lt,
            Box::new(col("price")),
        ));
        let (col_name, op, _) = normalize_binary(&expr).expect("normalizable");
        assert_eq!(col_name, "price");
        assert_eq!(op, Operator::Gt);

        // Column-to-column has no literal → None.
        assert!(normalize_binary(&col("a").eq(col("b"))).is_none());
        // Pushable-shaped but with an inconvertible literal → None (not a panic).
        let ts = Expr::BinaryExpr(BinaryExpr::new(
            Box::new(col("a")),
            Operator::Eq,
            Box::new(Expr::Literal(
                ScalarValue::TimestampNanosecond(Some(0), None),
                None,
            )),
        ));
        assert!(normalize_binary(&ts).is_none());
    }

    #[test]
    fn flip_operator_is_a_mirror() {
        assert_eq!(flip_operator(Operator::Lt), Operator::Gt);
        assert_eq!(flip_operator(Operator::LtEq), Operator::GtEq);
        assert_eq!(flip_operator(Operator::Gt), Operator::Lt);
        assert_eq!(flip_operator(Operator::GtEq), Operator::LtEq);
        // Symmetric comparisons are unchanged.
        assert_eq!(flip_operator(Operator::Eq), Operator::Eq);
        assert_eq!(flip_operator(Operator::NotEq), Operator::NotEq);
    }

    #[test]
    fn is_key_condition_op_excludes_not_eq() {
        // `<>` is a legal filter but illegal in a KeyConditionExpression.
        assert!(is_key_condition_op(Operator::Eq));
        assert!(is_key_condition_op(Operator::Gt));
        assert!(is_key_condition_op(Operator::LtEq));
        assert!(!is_key_condition_op(Operator::NotEq));
    }

    #[test]
    fn operator_symbol_rejects_non_comparison() {
        assert_eq!(operator_symbol(Operator::Eq).unwrap(), "=");
        assert_eq!(operator_symbol(Operator::NotEq).unwrap(), "<>");
        // A logical connective is not a DynamoDB comparison operator.
        assert!(operator_symbol(Operator::And).is_err());
    }

    // ─── Value / count builders ─────────────────────────────────────────────

    #[test]
    fn expr_to_attribute_value_rejects_non_literal() {
        // A bare column reference is not a value.
        assert!(expr_to_attribute_value(&col("x")).is_err());
        assert!(matches!(
            expr_to_attribute_value(&lit(3.5f64)).unwrap(),
            AttributeValue::N(s) if s == "3.5"
        ));
    }

    #[test]
    fn scalar_to_attribute_value_maps_float_and_null() {
        assert!(matches!(
            scalar_to_attribute_value(&ScalarValue::Float64(Some(1.5))).unwrap(),
            AttributeValue::N(s) if s == "1.5"
        ));
        assert!(matches!(
            scalar_to_attribute_value(&ScalarValue::Null).unwrap(),
            AttributeValue::Null(true)
        ));
    }

    #[test]
    fn count_batch_is_single_uint64_row() {
        let batch = count_batch(42).expect("count batch");
        assert_eq!(batch.num_rows(), 1);
        assert_eq!(batch.schema().field(0).name(), "count");
        assert_eq!(batch.schema().field(0).data_type(), &DataType::UInt64);
        let counts = batch
            .column(0)
            .as_any()
            .downcast_ref::<UInt64Array>()
            .unwrap();
        assert_eq!(counts.value(0), 42);
    }

    #[test]
    fn count_plan_properties_describe_single_count_row() {
        // Must stay in lockstep with count_batch()/count_schema(): one bounded,
        // final partition whose schema is the `{count}` row.
        let props = count_plan_properties();
        assert_eq!(
            props.equivalence_properties().schema().as_ref(),
            count_schema().as_ref()
        );
        assert_eq!(props.output_partitioning().partition_count(), 1);
        assert_eq!(props.emission_type, EmissionType::Final);
        assert_eq!(props.boundedness, Boundedness::Bounded);
    }

    #[test]
    fn parse_columns_option_accepts_int_and_bool_aliases() {
        let schema =
            parse_columns_option("qty:integer, active:boolean", "id", None).expect("valid columns");
        assert_eq!(schema.field(1).data_type(), &DataType::Int64);
        assert_eq!(schema.field(2).data_type(), &DataType::Boolean);
    }

    // ─── Key extraction / projection (provider methods) ─────────────────────

    /// Build a provider with an explicit schema (no DynamoDB round-trip needed).
    async fn test_provider(
        partition_key: &str,
        sort_key: Option<&str>,
        read_write: bool,
    ) -> DynamoTableProvider {
        let mut fields = vec![Field::new(partition_key, DataType::Utf8, false)];
        if let Some(sk) = sort_key {
            fields.push(Field::new(sk, DataType::Utf8, false));
        }
        fields.push(Field::new("name", DataType::Utf8, true));
        let schema = Arc::new(Schema::new(fields));
        let mut opts = HashMap::new();
        opts.insert("region".to_string(), "us-east-1".to_string());
        let client = build_client("http://localhost:8000", "us-east-1", &opts)
            .await
            .expect("client");
        DynamoTableProvider::new(
            client,
            "t",
            partition_key,
            sort_key,
            Some(schema),
            read_write,
        )
        .await
        .expect("provider")
    }

    #[tokio::test]
    async fn key_of_extracts_full_composite_key_and_drops_non_key_attrs() {
        let handle = test_provider("pk", Some("sk"), false).await.clone_handle();
        let item = HashMap::from([
            ("pk".to_string(), AttributeValue::S("A".into())),
            ("sk".to_string(), AttributeValue::S("B".into())),
            ("name".to_string(), AttributeValue::S("ignored".into())),
        ]);
        let key = handle.key_of(&item).expect("key");
        assert_eq!(key.len(), 2, "only the two key attributes are kept");
        assert!(key.contains_key("pk") && key.contains_key("sk"));
    }

    #[tokio::test]
    async fn key_of_errors_when_sort_key_missing() {
        let handle = test_provider("pk", Some("sk"), false).await.clone_handle();
        // Composite-key table but the item lacks the sort key.
        let item = HashMap::from([("pk".to_string(), AttributeValue::S("A".into()))]);
        let err = handle
            .key_of(&item)
            .expect_err("missing sort key must error");
        assert!(err.to_string().contains("sort key"), "got: {err}");
    }

    #[tokio::test]
    async fn key_projection_covers_partition_and_sort() {
        let single = test_provider("pk", None, false).await.clone_handle();
        let (expr, names) = single.key_projection();
        assert_eq!(expr, "#p0");
        assert_eq!(names.get("#p0").map(String::as_str), Some("pk"));

        let composite = test_provider("pk", Some("sk"), false).await.clone_handle();
        let (expr, names) = composite.key_projection();
        assert_eq!(expr, "#p0, #p1");
        assert_eq!(names.get("#p1").map(String::as_str), Some("sk"));
    }

    // ─── Plan-time write guards (no network) ────────────────────────────────

    async fn register_provider(provider: DynamoTableProvider) -> SessionContext {
        let ctx = SessionContext::new();
        ctx.register_table("t", Arc::new(provider))
            .expect("register");
        ctx
    }

    #[tokio::test]
    async fn read_only_source_rejects_delete_at_plan_time() {
        // access_mode enforcement: a read-only source must block DML before any
        // request reaches DynamoDB.
        let ctx = register_provider(test_provider("id", None, false).await).await;
        let err = ctx
            .sql("DELETE FROM t WHERE id = 'A'")
            .await
            .expect("logical plan")
            .collect()
            .await
            .expect_err("read-only DELETE must be rejected");
        assert!(err.to_string().contains("read_only"), "got: {err}");
    }

    #[tokio::test]
    async fn update_of_key_column_is_rejected_at_plan_time() {
        // DynamoDB key attributes are immutable; the guard must fire during
        // planning rather than issuing an UpdateItem that would fail server-side.
        let ctx = register_provider(test_provider("id", None, true).await).await;
        let err = ctx
            .sql("UPDATE t SET id = 'Z' WHERE id = 'A'")
            .await
            .expect("logical plan")
            .collect()
            .await
            .expect_err("updating the key column must be rejected");
        assert!(err.to_string().contains("immutable"), "got: {err}");
    }

    #[tokio::test]
    async fn insert_plan_reports_count_output_not_input_schema() {
        // DynamoInsertExec's execute() streams a single `{count}` batch, so its
        // advertised plan properties must describe that shape — not the input's
        // schema (which is what `input.properties()` would leak).
        let provider = test_provider("id", None, true).await;
        let input: Arc<dyn ExecutionPlan> = Arc::new(EmptyExec::new(provider.schema()));
        let ctx = SessionContext::new();
        let plan = provider
            .insert_into(&ctx.state(), input, InsertOp::Append)
            .await
            .expect("insert plan");
        assert_eq!(plan.schema().as_ref(), count_schema().as_ref());
        assert_eq!(plan.properties().output_partitioning().partition_count(), 1);

        // Optimizer passes rebuild nodes via with_new_children; the count
        // shape must survive the rewrite.
        let rebuilt = plan
            .with_new_children(vec![Arc::new(EmptyExec::new(provider.schema()))])
            .expect("with_new_children");
        assert_eq!(rebuilt.schema().as_ref(), count_schema().as_ref());
    }

    // ─── Integration tests (require DynamoDB Local) ─────────────────────────
    //
    // Run a local endpoint first:
    //   docker run -d -p 8000:8000 amazon/dynamodb-local:2.5.2
    // then seed the `products` table (see docs/dynamodb/README.md) and run:
    //   AWS_ACCESS_KEY_ID=dummy AWS_SECRET_ACCESS_KEY=dummy \
    //     cargo nextest run --all-features -- --ignored

    async fn ci_provider() -> DynamoTableProvider {
        let mut opts = HashMap::new();
        opts.insert("region".to_string(), "us-east-1".to_string());
        let client = build_client("http://localhost:8000", "us-east-1", &opts)
            .await
            .expect("client");
        DynamoTableProvider::new(client, "products", "product_id", None, None, false)
            .await
            .expect("provider")
    }

    async fn query_rows(sql: &str) -> usize {
        let mut ctx = SessionContext::new();
        let mut opts = HashMap::new();
        opts.insert("table".to_string(), "products".to_string());
        opts.insert("partition_key".to_string(), "product_id".to_string());
        opts.insert("region".to_string(), "us-east-1".to_string());
        register_dynamodb_tables(
            &mut ctx,
            "products",
            "http://localhost:8000",
            Some(&opts),
            false,
            HierarchyLevel::Table,
        )
        .await
        .expect("register");
        let df = ctx.sql(sql).await.expect("sql");
        let batches = df.collect().await.expect("collect");
        batches.iter().map(|b| b.num_rows()).sum()
    }

    #[tokio::test]
    #[ignore = "requires DynamoDB Local on :8000 with seeded `products` table"]
    async fn integration_scan_all() {
        let provider = ci_provider().await;
        assert!(!provider.schema.fields().is_empty());
        let rows = query_rows("SELECT * FROM products").await;
        assert!(rows >= 3, "expected seeded rows, got {rows}");
    }

    #[tokio::test]
    #[ignore = "requires DynamoDB Local on :8000 with seeded `products` table"]
    async fn integration_filter_pushdown() {
        let rows = query_rows("SELECT * FROM products WHERE category = 'Electronics'").await;
        assert!(rows >= 1, "expected at least one Electronics row");
    }

    #[tokio::test]
    #[ignore = "requires DynamoDB Local on :8000 with seeded `products` table"]
    async fn integration_count_star() {
        let rows = query_rows("SELECT count(*) FROM products").await;
        // count(*) returns a single aggregate row
        assert_eq!(rows, 1);
    }

    /// Create a throwaway single-key table for write tests (idempotent).
    async fn ensure_dml_table(client: &Client, table: &str) {
        use aws_sdk_dynamodb::types::{
            AttributeDefinition, KeySchemaElement, KeyType, ScalarAttributeType,
        };
        if client
            .describe_table()
            .table_name(table)
            .send()
            .await
            .is_ok()
        {
            return;
        }
        client
            .create_table()
            .table_name(table)
            .attribute_definitions(
                AttributeDefinition::builder()
                    .attribute_name("id")
                    .attribute_type(ScalarAttributeType::S)
                    .build()
                    .unwrap(),
            )
            .key_schema(
                KeySchemaElement::builder()
                    .attribute_name("id")
                    .key_type(KeyType::Hash)
                    .build()
                    .unwrap(),
            )
            .billing_mode(aws_sdk_dynamodb::types::BillingMode::PayPerRequest)
            .send()
            .await
            .expect("create dml table");
    }

    #[tokio::test]
    #[ignore = "requires DynamoDB Local on :8000"]
    async fn integration_insert_update_delete_round_trip() {
        let table = "skardi_dml_roundtrip";
        let mut opts = HashMap::new();
        opts.insert("table".to_string(), table.to_string());
        opts.insert("partition_key".to_string(), "id".to_string());
        opts.insert("region".to_string(), "us-east-1".to_string());

        let client = build_client("http://localhost:8000", "us-east-1", &opts)
            .await
            .expect("client");
        ensure_dml_table(&client, table).await;

        // Provider declares an explicit schema so writes have stable types even
        // when the table is empty (inference can't see absent attributes).
        let schema = Arc::new(Schema::new(vec![
            Field::new("id", DataType::Utf8, false),
            Field::new("name", DataType::Utf8, true),
            Field::new("price", DataType::Float64, true),
        ]));

        async fn ctx_with(table: &str, schema: SchemaRef, client: Client) -> SessionContext {
            let provider = DynamoTableProvider::new(client, table, "id", None, Some(schema), true)
                .await
                .expect("provider");
            let ctx = SessionContext::new();
            ctx.register_table(table, Arc::new(provider))
                .expect("register");
            ctx
        }

        let run = |sql: String| {
            let schema = schema.clone();
            let client = client.clone();
            async move {
                let ctx = ctx_with(table, schema, client).await;
                ctx.sql(&sql)
                    .await
                    .expect("sql")
                    .collect()
                    .await
                    .expect("collect")
            }
        };

        // Clean slate
        run(format!("DELETE FROM {table}")).await;

        // INSERT
        run(format!(
            "INSERT INTO {table} (id, name, price) VALUES ('A1', 'Widget', 9.99)"
        ))
        .await;
        let after_insert: usize = run(format!("SELECT * FROM {table} WHERE id = 'A1'"))
            .await
            .iter()
            .map(|b| b.num_rows())
            .sum();
        assert_eq!(after_insert, 1, "row should exist after insert");

        // UPDATE
        run(format!("UPDATE {table} SET price = 19.99 WHERE id = 'A1'")).await;
        let batches = run(format!("SELECT price FROM {table} WHERE id = 'A1'")).await;
        let price = batches[0]
            .column(0)
            .as_any()
            .downcast_ref::<Float64Array>()
            .expect("price is f64")
            .value(0);
        assert_eq!(price, 19.99, "price should be updated");

        // DELETE
        run(format!("DELETE FROM {table} WHERE id = 'A1'")).await;
        let after_delete: usize = run(format!("SELECT * FROM {table} WHERE id = 'A1'"))
            .await
            .iter()
            .map(|b| b.num_rows())
            .sum();
        assert_eq!(after_delete, 0, "row should be gone after delete");
    }

    /// Create a composite-key (HASH + RANGE) table for Query-path tests.
    async fn ensure_composite_table(client: &Client, table: &str) {
        use aws_sdk_dynamodb::types::{AttributeDefinition, KeySchemaElement, ScalarAttributeType};
        if client
            .describe_table()
            .table_name(table)
            .send()
            .await
            .is_ok()
        {
            return;
        }
        client
            .create_table()
            .table_name(table)
            .attribute_definitions(
                AttributeDefinition::builder()
                    .attribute_name("pk")
                    .attribute_type(ScalarAttributeType::S)
                    .build()
                    .unwrap(),
            )
            .attribute_definitions(
                AttributeDefinition::builder()
                    .attribute_name("sk")
                    .attribute_type(ScalarAttributeType::N)
                    .build()
                    .unwrap(),
            )
            .key_schema(
                KeySchemaElement::builder()
                    .attribute_name("pk")
                    .key_type(KeyType::Hash)
                    .build()
                    .unwrap(),
            )
            .key_schema(
                KeySchemaElement::builder()
                    .attribute_name("sk")
                    .key_type(KeyType::Range)
                    .build()
                    .unwrap(),
            )
            .billing_mode(aws_sdk_dynamodb::types::BillingMode::PayPerRequest)
            .send()
            .await
            .expect("create composite table");
    }

    /// End-to-end coverage of the Query path (partition-only predicate) and the
    /// composite-key GetItem path (pk + sk equality) against DynamoDB Local. Also
    /// exercises DescribeTable-based sort-key auto-detection via registration.
    #[tokio::test]
    #[ignore = "requires DynamoDB Local on :8000"]
    async fn integration_composite_key_query_and_get() {
        let table = "skardi_composite";
        let mut opts = HashMap::new();
        opts.insert("table".to_string(), table.to_string());
        opts.insert("region".to_string(), "us-east-1".to_string());

        let client = build_client("http://localhost:8000", "us-east-1", &opts)
            .await
            .expect("client");
        ensure_composite_table(&client, table).await;

        // Seed three items in one partition + one in another.
        let put = |pk: &str, sk: i64| {
            let client = client.clone();
            let table = table.to_string();
            let pk = pk.to_string();
            async move {
                client
                    .put_item()
                    .table_name(&table)
                    .item("pk", AttributeValue::S(pk))
                    .item("sk", AttributeValue::N(sk.to_string()))
                    .send()
                    .await
                    .expect("put");
            }
        };
        put("A", 1).await;
        put("A", 2).await;
        put("A", 3).await;
        put("B", 1).await;

        // Register via the public path so DescribeTable auto-detects the sort key
        // (note: no partition_key/sort_key options supplied).
        let mut ctx = SessionContext::new();
        register_dynamodb_tables(
            &mut ctx,
            table,
            "http://localhost:8000",
            Some(&opts),
            false,
            HierarchyLevel::Table,
        )
        .await
        .expect("register");

        let rows = |sql: String| {
            let ctx = &ctx;
            async move {
                ctx.sql(&sql)
                    .await
                    .expect("sql")
                    .collect()
                    .await
                    .expect("collect")
                    .iter()
                    .map(|b| b.num_rows())
                    .sum::<usize>()
            }
        };

        // Partition-only predicate → Query path → all 3 items in partition A.
        assert_eq!(
            rows(format!("SELECT * FROM {table} WHERE pk = 'A'")).await,
            3
        );
        // Full composite key by equality → GetItem path → exactly one item.
        assert_eq!(
            rows(format!("SELECT * FROM {table} WHERE pk = 'A' AND sk = 2")).await,
            1
        );
        // Partition + sort range → Query path with a sort condition → 2 items.
        assert_eq!(
            rows(format!("SELECT * FROM {table} WHERE pk = 'A' AND sk >= 2")).await,
            2
        );
    }
}