sqlload 0.45.0

SQL data loading tooling.
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
use crate::adapters::contract::constants::CURSOR_TIMESTAMP_FORMAT;
use crate::adapters::contract::constants::STAGING_SUFFIX;
use crate::adapters::contract::main::cursor_text::cursor_text;
use crate::adapters::contract::models::TableName;
use crate::adapters::contract::models::WriteMode;
use crate::adapters::contract::models::WriteStatementsRequest;
use crate::adapters::contract::models::{ChangeApply, ChangeWrite};
use crate::adapters::dispatch::main::dialect::files_destination::files_destination;
use crate::adapters::dispatch::main::dialect::sql_adapter::sql_adapter;
use crate::adapters::dispatch::main::session::execute::execute;
use crate::adapters::dispatch::main::session::execute_ddl::execute_ddl;
use crate::adapters::dispatch::main::session::live_columns::live_columns;
use crate::adapters::dispatch::main::session::single_text::single_text;
use crate::adapters::dispatch::main::session::table_names::table_names;
use crate::adapters::dispatch::main::session::text_values::text_values;
use crate::adapters::dispatch::models::Destination;
use crate::batch_plan::main::window_at::window_at;
use crate::batch_plan::models::CursorWindow;
use crate::cdc::constants::DELETE_OP;
use crate::column_types::constants::ARROW_TYPE_METADATA_KEY;
use crate::column_types::main::arrow_field::arrow_field;
use crate::column_types::main::combined_type::combined_type;
use crate::column_types::main::exact_cast::exact_cast;
use crate::column_types::models::ColumnType;
use crate::load_naming::constants::CHILD_TABLE_SEPARATOR;
use crate::load_naming::main::child_table_name::child_table_name;
use crate::load_naming::main::loaded_name::loaded_name;
use crate::load_naming::main::metadata_name::metadata_name;
use crate::load_naming::models::{MetadataRole, Naming};
use crate::loader::_helpers::publish::replaces_roots;
use crate::loader::_helpers::publish::staged_columns;
use crate::loader::_helpers::resolve::resolve_tables;
use crate::loader::_helpers::run_log::log_empty_run;
use crate::loader::_helpers::run_log::run_entry;
use crate::loader::_helpers::specs::is_child_column;
use crate::loader::_helpers::table_plan::append_problem;
use crate::loader::_helpers::table_plan::replan_columns;
use crate::loader::_helpers::table_plan::schema_statements;
use crate::loader::constants::DROPPED_CHUNK;
use crate::loader::constants::EMPTY_REPLACE_PROBLEM;
use crate::loader::constants::NO_DATA_NOTE;
use crate::loader::constants::NO_STATE;
use crate::loader::constants::PYTHON_COLUMN_TYPE;
use crate::loader::constants::UNKNOWN_CURSOR_TYPE;
use crate::loader::constants::UTC_OFFSET_SUFFIX;
use crate::loader::errors::LoaderError;
use crate::loader::main::_plan_files::plan_files_table;
use crate::loader::models::ChildFeed;
use crate::loader::models::ChildTables;
use crate::loader::models::CommitRange;
use crate::loader::models::CommitRule;
use crate::loader::models::PlanOptions;
use crate::loader::models::Publisher;
use crate::loader::models::PythonFeed;
use crate::loader::models::PythonLoad;
use crate::loader::models::PythonTable;
use crate::loader::models::ReadWindow;
use crate::loader::models::TableLayout;
use crate::loader::models::TableLoaded;
use crate::loader::models::TablePlan;
use crate::loader::models::TableSpec;
use crate::masking::models::MaskRules;
use crate::postgres_source::models::PgType;
use crate::postgres_source::models::SourceColumn;
use crate::postgres_source::models::SourceTable;
use crate::project::constants::FILES_CONNECTION_KINDS;
use crate::project::models::CheckName;
use crate::project::models::OnSchemaChange;
use crate::project::models::ProjectConfig;
use crate::project::models::SourceConfig;
use crate::publish_checks::main::check_column_problems::check_column_problems;
use crate::python_sources::models::CursorRead;
use crate::python_sources::models::TableRead;
use crate::python_sources::types::BatchStream;
use crate::python_sources::types::PythonHost;
use crate::python_sources::types::Streamed;
use crate::row_batches::main::choose_lists::choose_lists;
use crate::row_batches::main::conform_batch::conform_batch;
use crate::row_batches::main::flattened_columns::flattened_columns;
use crate::row_batches::main::json_lists::json_lists;
use crate::row_batches::main::merge_schema::merge_schema;
use crate::row_batches::main::settled_schema::settled_schema;
use crate::row_batches::models::ChildBatch;
use crate::row_batches::models::Unnest;
use crate::row_batches::models::Unnested;
use crate::row_batches::types::ListChoices;
use crate::schema_plan::constants::ROOT_ROW_COLUMN;
use crate::schema_plan::constants::{CDC_LSN_COLUMN, CDC_OP_COLUMN, CDC_UNCHANGED_COLUMN};
use crate::schema_plan::models::DesiredColumn;
use crate::schema_plan::models::LiveColumn;
use crate::schema_plan::models::SchemaPlan;
use crate::schema_plan::models::TableAction;
use crate::similar_names::main::close_name::close_name;
use arrow_array::Array;
use arrow_array::ArrayRef;
use arrow_array::BooleanArray;
use arrow_array::Date32Array;
use arrow_array::RecordBatch;
use arrow_array::Scalar;
use arrow_array::StringArray;
use arrow_array::TimestampMicrosecondArray;
use arrow_array::cast::AsArray;
use arrow_cast::cast::CastOptions;
use arrow_cast::cast::cast_with_options;
use arrow_cast::display::ArrayFormatter;
use arrow_cast::display::FormatOptions;
use arrow_ord::cmp::gt;
use arrow_ord::cmp::gt_eq;
use arrow_ord::cmp::lt;
use arrow_ord::sort::SortOptions;
use arrow_ord::sort::sort_to_indices;
use arrow_schema::DataType;
use arrow_schema::Field;
use arrow_schema::Schema;
use arrow_schema::SchemaRef;
use arrow_schema::TimeUnit;
use arrow_select::filter::filter_record_batch;
use chrono::NaiveDate;
use chrono::NaiveDateTime;
use std::collections::BTreeMap;
use std::collections::BTreeSet;
use std::sync::Arc;

/// A Python source's tables as planning sees them before any data: the declared (`columns`) and masked columns only.
pub(crate) fn python_tables(source: &SourceConfig) -> Vec<SourceTable> {
    let mut tables: Vec<SourceTable> = Vec::new();
    for (name, table) in &source.tables {
        let mut columns: Vec<SourceColumn> = Vec::new();
        for column in table.columns.keys().chain(table.mask.keys()) {
            if !columns.iter().any(|known| known.name == *column) && !is_child_column(column, table)
            {
                columns.push(python_column(column, ColumnType::Text));
            }
        }
        tables.push(SourceTable {
            schema: String::new(),
            name: name.clone(),
            columns,
            primary_key: Vec::new(),
            indexed_columns: Vec::new(),
            estimated_rows: -1,
            estimated_bytes: -1,
            comment: None,
            listed_keys: Vec::new(),
        });
    }
    tables
}

/// What a Python table's spec records about it.
pub(crate) fn python_table(module: &str, table: &SourceTable) -> PythonTable {
    PythonTable {
        module: module.to_owned(),
        declared: table
            .columns
            .iter()
            .map(|column| column.name.clone())
            .collect(),
        lists: ListChoices::new(),
    }
}

/// The child tables `spec`'s table has in the destination (tables named `<table>__...` that have a parent-id column, staging tables and the swap tables named after them, in any case, left out); none without `unnest`. A table named so without that column is another table, and so are the child tables of such a table (`orders__notes__items` beside a table `orders__notes`).
pub(crate) async fn live_child_tables(
    destination: &Destination,
    spec: &TableSpec,
) -> Result<Vec<String>, LoaderError> {
    if spec.unnest == Unnest::None || spec.destination_path.is_some() {
        return Ok(Vec::new());
    }
    let adapter = sql_adapter(spec.warehouse);
    let found = table_names(
        destination,
        adapter.list_child_tables_sql(&spec.destination),
    )
    .await?;
    let (children, others): (Vec<&String>, Vec<&String>) =
        found.iter().partition(|marked| marked.starts_with('1'));
    let others: Vec<&str> = others.iter().filter_map(|marked| marked.get(1..)).collect();
    Ok(children
        .into_iter()
        .filter_map(|marked| marked.get(1..))
        .filter(|name| !name.to_ascii_lowercase().contains(STAGING_SUFFIX))
        .filter(|name| !is_child_of_any(name, &others))
        .map(str::to_owned)
        .collect())
}

/// Whether `table` is named as a child table of one of `parents` is (`orders__notes__items` of `orders__notes`).
fn is_child_of_any(table: &str, parents: &[&str]) -> bool {
    parents.iter().any(|parent| {
        table
            .strip_prefix(parent)
            .is_some_and(|rest| rest.starts_with(CHILD_TABLE_SEPARATOR))
    })
}

/// The dotted path each of `spec`'s child tables among `tables` holds, by its name as the load writes it (`orders__items__parts` holds `items.parts`; Snowflake's `ORDERS__ITEMS` holds `items`; Postgres lists the children of `ORDERS` folded, as `orders__items`): the path of a `child_pinned` column whose table it is, or else the name's parts. Only the pins know a path whose field holds `__` (a list in an object `naming = "dlt"` flattened: `orders__items__dims__tags` holds `items.dims__tags`) or a name dlt shortened.
pub(crate) fn child_paths(spec: &TableSpec, tables: Vec<String>) -> Vec<(String, String)> {
    let pinned = pinned_tables(spec, &tables);
    tables
        .into_iter()
        .filter_map(|table| {
            let path = pinned
                .get(&table)
                .cloned()
                .or_else(|| named_child_path(spec, &table))?;
            Some((path, table))
        })
        .collect()
}

/// Each child table path `spec`'s `child_pinned` keys name: every dotted prefix of a key (`items.dims__tags.value` names `items` and `items.dims__tags`).
fn pinned_child_paths(spec: &TableSpec) -> BTreeSet<&str> {
    let mut paths: BTreeSet<&str> = BTreeSet::new();
    for key in spec.child_pinned.keys() {
        for (at, _) in key.match_indices('.') {
            paths.insert(&key[..at]);
        }
    }
    paths
}

/// Each of `tables` (as the warehouse lists them) that a `child_pinned` path's child table is, with that path: the table spelt exactly as the path's is, or else, when no pin spells it exactly, one spelt so in another case (Postgres lists an upper-case parent's children folded), so `items` and `Items` keep their own paths.
fn pinned_tables(spec: &TableSpec, tables: &[String]) -> BTreeMap<String, String> {
    let adapter = sql_adapter(spec.warehouse);
    let named: Vec<(&str, String)> = pinned_child_paths(spec)
        .into_iter()
        .map(|path| (path, adapter.stored_table_name(&unnested_table(spec, path))))
        .collect();
    let mut found: BTreeMap<String, String> = BTreeMap::new();
    for exact in [true, false] {
        for (path, name) in &named {
            for table in tables {
                let spelt = match exact {
                    true => table == name,
                    false => table.eq_ignore_ascii_case(name),
                };
                if spelt {
                    found
                        .entry(table.clone())
                        .or_insert_with(|| (*path).to_owned());
                }
            }
        }
    }
    found
}

/// The path `table`'s name gives, its parts below `spec`'s table joined by dots; `None` when it is not named as a child of that table.
fn named_child_path(spec: &TableSpec, table: &str) -> Option<String> {
    let prefix = format!("{}{CHILD_TABLE_SEPARATOR}", spec.destination.table);
    if !table.starts_with(&sql_adapter(spec.warehouse).stored_table_name(&prefix)) {
        return None;
    }
    Some(
        source_spelling(table, spec)
            .get(prefix.len()..)?
            .replace(CHILD_TABLE_SEPARATOR, "."),
    )
}

/// `spec` with each top-level field whose child table is among `children` chosen as a list field under `unnest = "all"`: an earlier load chose it, so this one keeps it one whatever its sampled values show (a value that is not a list then fails the load).
pub(crate) fn with_live_children(spec: &TableSpec, children: &[String]) -> TableSpec {
    let mut chosen = spec.clone();
    if spec.unnest != Unnest::All || children.is_empty() {
        return chosen;
    }
    let adapter = sql_adapter(spec.warehouse);
    let live = |field: &str| {
        let table = child_table_name(
            (spec.naming, adapter.max_identifier_length()),
            &spec.destination.table,
            field,
        );
        children.contains(&adapter.stored_table_name(&table))
    };
    if let Some(files) = &mut chosen.files {
        for column in &mut files.table.columns {
            if live(&column.name) {
                column.lists = Some(true);
            }
        }
    }
    if let Some(python) = &mut chosen.python {
        let prefix = adapter.stored_table_name(&format!(
            "{}{CHILD_TABLE_SEPARATOR}",
            spec.destination.table
        ));
        for name in children {
            let field = name
                .strip_prefix(&prefix)
                .filter(|field| !field.contains(CHILD_TABLE_SEPARATOR));
            if let Some(field) = field {
                python.lists.insert(field.to_owned(), true);
            }
        }
    }
    chosen
}

/// Under `unnest = "all"`, the top-level columns the plan chose as list fields or not: from the sampled files, or from a Python table's rows so far.
pub(crate) fn list_choices(spec: &TableSpec) -> ListChoices {
    if spec.unnest != Unnest::All {
        return ListChoices::new();
    }
    let sampled = spec
        .files
        .iter()
        .flat_map(|files| &files.table.columns)
        .filter_map(|column| {
            Some((
                spec.source_table.loaded_as(&column.name).to_owned(),
                column.lists?,
            ))
        });
    let yielded = spec.python.iter().flat_map(|python| python.lists.clone());
    sampled.chain(yielded).collect()
}

/// A Python table's spec with the live table's data columns added (as the source has them, so `plan` shows no change for them), or `None` when there are none to add; its `constant_columns` are never the source's.
pub(crate) fn with_live_columns(
    spec: &TableSpec,
    live: Option<&[LiveColumn]>,
) -> Option<TableSpec> {
    spec.python.as_ref()?;
    let mut added: Vec<SourceColumn> = Vec::new();
    let constant = |name: &str| {
        spec.constants
            .iter()
            .any(|(constant, _, _)| constant.eq_ignore_ascii_case(name))
    };
    for column in live? {
        if !is_metadata(spec.naming, &column.name)
            && !constant(&column.name)
            && !has_column(&spec.source_table, &column.name)
        {
            let name = source_spelling(&column.name, spec);
            added.push(python_column(&name, column.column_type.clone()));
        }
    }
    if added.is_empty() {
        return None;
    }
    let mut augmented = spec.clone();
    augmented.source_table.columns.extend(added);
    Some(augmented)
}

/// A yielded batch with its fields named as they load under `spec`'s naming: as yielded under SQLLoad's, normalised under dlt's; two fields loading as one name fail, naming both.
fn dlt_named(batch: RecordBatch, spec: &TableSpec) -> Result<RecordBatch, LoaderError> {
    if spec.naming == Naming::Sqlload {
        return Ok(batch);
    }
    let limit = sql_adapter(spec.warehouse).max_identifier_length();
    let schema = batch.schema();
    let mut fields: Vec<arrow_schema::FieldRef> = Vec::with_capacity(schema.fields().len());
    for field in schema.fields() {
        let name = loaded_name(spec.naming, field.name(), limit);
        if let Some(other) = fields.iter().find(|taken| *taken.name() == name) {
            let yielded = schema
                .fields()
                .iter()
                .find(|yielded| loaded_name(spec.naming, yielded.name(), limit) == *other.name())
                .map_or(name.clone(), |yielded| yielded.name().clone());
            return Err(LoaderError::Invalid(format!(
                "fields {yielded:?} and {:?} both load as {name} under naming = \"dlt\"; rename one in the table function",
                field.name()
            )));
        }
        fields.push(Arc::new(field.as_ref().clone().with_name(name)));
    }
    RecordBatch::try_new(
        Arc::new(Schema::new_with_metadata(fields, schema.metadata().clone())),
        batch.columns().to_vec(),
    )
    .map_err(|error| LoaderError::Invalid(error.to_string()))
}

/// The name a table function most likely yields for live column `stored`: its lower-case form when the warehouse stores that as `stored` (upper case on Snowflake), else `stored` itself.
pub(crate) fn source_spelling(stored: &str, spec: &TableSpec) -> String {
    let lower = stored.to_lowercase();
    match sql_adapter(spec.warehouse).stored_name(&lower) == stored {
        true => lower,
        false => stored.to_owned(),
    }
}

/// What the yielded data showed: its merged schema, the columns that only ever held NULL, and the rows.
pub(crate) struct YieldedData<'a> {
    pub(crate) schema: &'a Schema,
    pub(crate) null_columns: &'a BTreeSet<String>,
    pub(crate) rows: u64,
}

/// A Python table as its yielded data shows it: every column of the data typed by inference (the live table's type when the inferred one fits it, or when the column only held NULL), plus declared columns no row had.
pub(crate) fn inferred_table(
    (planned, naming): (&SourceTable, Naming),
    declared: &[String],
    data: &YieldedData<'_>,
    live: Option<&[LiveColumn]>,
) -> SourceTable {
    let mut columns: Vec<SourceColumn> = Vec::new();
    for field in data.schema.fields() {
        if !is_metadata(naming, field.name()) {
            let live_type = live_type(live, field.name());
            let column_type = match (data.null_columns.contains(field.name()), live_type) {
                (true, Some(live_type)) => live_type,
                (_, live_type) => fitting_type(field_type(field), live_type),
            };
            columns.push(python_column(field.name(), column_type));
        }
    }
    for name in declared {
        let seen = columns
            .iter()
            .any(|column| column.name.eq_ignore_ascii_case(name));
        let planned_column = planned.columns.iter().find(|column| column.name == *name);
        if let (false, Some(column)) = (seen, planned_column) {
            columns.push(column.clone());
        }
    }
    SourceTable {
        columns,
        ..planned.clone()
    }
}

/// Why the declared `columns` and `mask` keys do not match the data: a key that names no column but is close to one (a different case or a typo), or a masked column the data lacks while it has rows; masking must never miss the real column.
pub(crate) fn declared_problems(spec: &TableSpec, data: &YieldedData<'_>) -> Vec<String> {
    let names: Vec<&str> = data
        .schema
        .fields()
        .iter()
        .map(|field| field.name().as_str())
        .filter(|name| !is_metadata(spec.naming, name))
        .collect();
    let keys = spec
        .pinned
        .keys()
        .filter(|key| !spec.masked.columns.contains_key(*key))
        .map(|key| ("columns", key))
        .chain(spec.masked.columns.keys().map(|key| ("mask", key)));
    let mut problems: Vec<String> = Vec::new();
    for (setting, key) in keys {
        if names.contains(&key.as_str()) {
            continue;
        }
        match (close_name(key, names.iter().copied()), setting) {
            (Some(close), _) => problems.push(format!(
                "{setting} key {key} names no column of the data; did you mean {close}?"
            )),
            (None, "mask") if data.rows > 0 => {
                problems.push(format!("mask {key} names no column of the data"));
            }
            (None, _) => {}
        }
    }
    problems
}

/// The live table's type of column `name`, if it has one.
fn live_type(live: Option<&[LiveColumn]>, name: &str) -> Option<ColumnType> {
    live?
        .iter()
        .find(|column| column.name.eq_ignore_ascii_case(name))
        .map(|column| column.column_type.clone())
}

/// The live type when the inferred values fit it (pinned > live > inferred), else the inferred type, which schema planning then widens or blocks.
pub(crate) fn fitting_type(inferred: ColumnType, live: Option<ColumnType>) -> ColumnType {
    match live {
        Some(live) if combined_type(live.clone(), inferred.clone()) == live => live,
        _ => inferred,
    }
}

fn has_column(table: &SourceTable, name: &str) -> bool {
    table
        .columns
        .iter()
        .any(|column| column.name.eq_ignore_ascii_case(name))
}

/// The planned type of the cursor column (matched ignoring case, as warehouses store names), `unknown` before the table's first load.
pub(crate) fn cursor_type_name(table: &TablePlan, column: &str) -> String {
    match table
        .read_columns
        .iter()
        .find(|read| read.name.eq_ignore_ascii_case(column))
    {
        Some(read) => read.output.to_string(),
        None => UNKNOWN_CURSOR_TYPE.to_owned(),
    }
}

/// The schema changes the load made, one line each, as `apply` would print them.
pub(crate) fn change_lines(table: &TablePlan) -> Vec<String> {
    let name = sql_adapter(table.spec.warehouse).render_display_name(&table.spec.destination);
    match &table.schema.action {
        TableAction::Create => vec![format!(
            "created  {name}  {} columns",
            table.schema.write_columns.len()
        )],
        TableAction::Alter(changes) => changes
            .iter()
            .map(|change| format!("altered  {name}  {change}"))
            .collect(),
        TableAction::Recreate | TableAction::Unchanged => Vec::new(),
    }
}

/// Fail when writing no rows would empty a table that has rows: `replace` or `--reload` without `allow_empty` (an empty result must not wipe a table by accident).
pub(crate) async fn refuse_emptying(
    table: &TablePlan,
    destination: &Destination,
) -> Result<(), LoaderError> {
    let replaces = matches!(table.mode, WriteMode::Replace | WriteMode::Recreate);
    if !replaces || table.spec.allow_empty || !table.exists {
        return Ok(());
    }
    let adapter = sql_adapter(table.spec.warehouse);
    let has_rows = match adapter.runs_sql() {
        false => true,
        true => {
            let query = adapter.render_any_row_query(&table.spec.destination);
            single_text(destination, query).await?.is_some()
        }
    };
    match has_rows {
        true => Err(LoaderError::Invalid(
            EMPTY_REPLACE_PROBLEM.replace("{table}", &table.outcome_name()),
        )),
        false => Ok(()),
    }
}

/// Whether a target's connection `name` may also be a Python source's own: only an s3 or filesystem one (project checks refuse the rest).
pub(crate) fn shareable_connection(project: &ProjectConfig, name: &str) -> bool {
    project
        .connections
        .get(name)
        .is_some_and(|connection| FILES_CONNECTION_KINDS.contains(&connection.kind()))
}

/// The largest non-NULL value of cursor `column` (matched ignoring case) in `batch`, as cursor text; `None` when it has none.
pub(crate) fn batch_cursor_max(
    batch: &RecordBatch,
    column: &str,
    column_type: &ColumnType,
) -> Result<Option<String>, String> {
    let schema = batch.schema();
    let Some((position, _)) = schema
        .fields()
        .iter()
        .enumerate()
        .find(|(_, field)| field.name().eq_ignore_ascii_case(column))
    else {
        return Ok(None);
    };
    let values = batch.column(position);
    let options = SortOptions {
        descending: true,
        nulls_first: false,
    };
    let sorted = sort_to_indices(values.as_ref(), Some(options), Some(1))
        .map_err(|error| error.to_string())?;
    let Some(index) = sorted.values().first().map(|index| *index as usize) else {
        return Ok(None);
    };
    if values.is_null(index) {
        return Ok(None);
    }
    let formatter = ArrayFormatter::try_new(values.as_ref(), &FormatOptions::default())
        .map_err(|error| error.to_string())?;
    cursor_text(&formatter.value(index).to_string(), column_type).map(Some)
}

/// The portable type of a column the crossing produced.
pub(crate) fn field_type(field: &Field) -> ColumnType {
    let json = field
        .metadata()
        .get(ARROW_TYPE_METADATA_KEY)
        .is_some_and(|kind| *kind == ColumnType::Json.to_string());
    match field.data_type() {
        DataType::Boolean => ColumnType::Bool,
        DataType::Int8 | DataType::Int16 | DataType::Int32 | DataType::Int64 => ColumnType::Int,
        DataType::Float16 | DataType::Float32 | DataType::Float64 => ColumnType::Float,
        DataType::Decimal128(precision, scale) => ColumnType::Decimal {
            precision: *precision,
            scale: u8::try_from(*scale).unwrap_or_default(),
        },
        DataType::Date32 | DataType::Date64 => ColumnType::Date,
        DataType::Time32(_) | DataType::Time64(_) => ColumnType::Time,
        DataType::Timestamp(_, Some(_)) => ColumnType::TimestampTz,
        DataType::Timestamp(
            TimeUnit::Microsecond | TimeUnit::Millisecond | TimeUnit::Second | TimeUnit::Nanosecond,
            None,
        ) => ColumnType::TimestampNtz,
        DataType::Binary | DataType::LargeBinary | DataType::BinaryView => ColumnType::Binary,
        DataType::Utf8 if json => ColumnType::Json,
        DataType::List(_) => ColumnType::Json,
        _ => ColumnType::Text,
    }
}

fn python_column(name: &str, column_type: ColumnType) -> SourceColumn {
    SourceColumn {
        name: name.to_owned(),
        postgres_type: PYTHON_COLUMN_TYPE.to_owned(),
        pg_type: PgType::Other,
        column_type: Ok(column_type),
        nullable: true,
        source_spelling: None,
    }
}

/// Whether `name` is a column the load adds under `naming`.
fn is_metadata(naming: Naming, name: &str) -> bool {
    [
        MetadataRole::LoadId,
        MetadataRole::LoadedAt,
        MetadataRole::RowId,
    ]
    .into_iter()
    .any(|role| name.eq_ignore_ascii_case(metadata_name(naming, role)))
}

/// Call the table function on a blocking thread; the host holds the GIL only to start it.
pub(crate) async fn start_read(
    host: Arc<dyn PythonHost>,
    request: TableRead,
) -> Result<Box<dyn BatchStream>, LoaderError> {
    tokio::task::spawn_blocking(move || host.read_table(&request))
        .await
        .map_err(|error| {
            LoaderError::Invalid(format!("the table function's thread failed: {error}"))
        })?
        .map_err(LoaderError::Invalid)
}

/// The position the table function left in `ctx.state` once it finished, as JSON text.
pub(crate) async fn final_state(
    host: Arc<dyn PythonHost>,
    table: String,
) -> Result<Option<String>, LoaderError> {
    tokio::task::spawn_blocking(move || host.table_state(&table))
        .await
        .map_err(|error| {
            LoaderError::Invalid(format!("the table function's thread failed: {error}"))
        })?
        .map_err(LoaderError::Invalid)
}

/// One yielded batch as the plan reads it: in the schema merged so far (`merged`, all-NULL columns as text), each read column converted to its planned type (excluded columns dropped), and only the rows inside `window` when it is given; returns the batch and the rows left outside.
pub(crate) fn typed_batch(
    batch: &RecordBatch,
    table: &TablePlan,
    merged: &SchemaRef,
    window: Option<&ReadWindow>,
) -> Result<(RecordBatch, usize), LoaderError> {
    let conformed = conform_batch(batch, merged).map_err(LoaderError::Invalid)?;
    let typed = read_types(&conformed, &read_schema(table))?;
    let cursor_type = window.and_then(|window| {
        table
            .read_columns
            .iter()
            .find(|column| column.name.eq_ignore_ascii_case(&window.column))
            .map(|column| column.output.clone())
    });
    let kept = match (window, &cursor_type) {
        (Some(window), Some(cursor_type)) => in_window(&typed, window, cursor_type)?,
        _ => typed,
    };
    let outside = batch.num_rows() - kept.num_rows();
    Ok((kept, outside))
}

/// The batches' target schema: each read column in its planned type.
fn read_schema(table: &TablePlan) -> SchemaRef {
    let fields: Vec<Field> = table
        .read_columns
        .iter()
        .map(|column| {
            arrow_field(&column.name, &column.output)
                .unwrap_or_else(|| Field::new(&column.name, arrow_schema::DataType::Utf8, true))
        })
        .collect();
    Arc::new(Schema::new(fields))
}

/// `batch` with the target's columns (excluded columns dropped), each converted to its planned type (an array from the list, or from the JSON text a list arrives as); a value that does not convert is an error naming the column.
fn read_types(batch: &RecordBatch, target: &SchemaRef) -> Result<RecordBatch, LoaderError> {
    let arrays: Vec<ArrayRef> = target
        .fields()
        .iter()
        .map(|field| {
            let column = batch
                .column_by_name(field.name())
                .cloned()
                .unwrap_or_else(|| {
                    arrow_array::new_null_array(field.data_type(), batch.num_rows())
                });
            match (field.data_type(), column.data_type()) {
                (arrow_schema::DataType::List(_), arrow_schema::DataType::Utf8) => {
                    json_lists(&column, field.data_type()).map_err(|error| {
                        LoaderError::Invalid(format!("column {}: {error}", field.name()))
                    })
                }
                _ => exact_cast(&column, field.data_type()).map_err(|error| {
                    LoaderError::Invalid(format!(
                        "column {}: {error}; pin a type for {} in columns that holds every value",
                        field.name(),
                        field.name()
                    ))
                }),
            }
        })
        .collect::<Result<_, LoaderError>>()?;
    let options = arrow_array::RecordBatchOptions::new().with_row_count(Some(batch.num_rows()));
    RecordBatch::try_new_with_options(target.clone(), arrays, &options).map_err(invalid)
}

/// The rows of `batch` whose cursor is inside `window` (a NULL cursor is outside, as a source filter would have it).
fn in_window(
    batch: &RecordBatch,
    window: &ReadWindow,
    cursor_type: &ColumnType,
) -> Result<RecordBatch, LoaderError> {
    let schema = batch.schema();
    let Some((position, _)) = schema
        .fields()
        .iter()
        .enumerate()
        .find(|(_, field)| field.name().eq_ignore_ascii_case(&window.column))
    else {
        return Ok(batch.clone());
    };
    let mut kept = batch.clone();
    if let Some(from) = &window.from {
        let column = kept.column(position).clone();
        let bound = Scalar::new(bound_array(from, cursor_type, column.data_type())?);
        let mask = match window.inclusive {
            true => gt_eq(&column, &bound),
            false => gt(&column, &bound),
        }
        .map_err(invalid)?;
        kept = filter_record_batch(&kept, &mask).map_err(invalid)?;
    }
    if let Some(to) = &window.to {
        let column = kept.column(position).clone();
        let bound = Scalar::new(bound_array(to, cursor_type, column.data_type())?);
        let mask = lt(&column, &bound).map_err(invalid)?;
        kept = filter_record_batch(&kept, &mask).map_err(invalid)?;
    }
    Ok(kept)
}

/// A cursor bound as a one-value array of the cursor column's Arrow type `data_type`, converted from the bound's text (timestamps and dates parsed, every other type cast).
pub(crate) fn bound_array(
    text: &str,
    cursor_type: &ColumnType,
    data_type: &DataType,
) -> Result<ArrayRef, LoaderError> {
    let normal = match text.strip_suffix(UTC_OFFSET_SUFFIX) {
        Some(_) => text.to_owned(),
        None => cursor_text(text, cursor_type).map_err(LoaderError::Invalid)?,
    };
    let unreadable = || {
        LoaderError::Invalid(format!(
            "cursor bound {text:?} does not fit the cursor's type {cursor_type}"
        ))
    };
    let micros = || {
        NaiveDateTime::parse_from_str(
            normal.trim_end_matches(UTC_OFFSET_SUFFIX),
            CURSOR_TIMESTAMP_FORMAT,
        )
        .map(|time| time.and_utc().timestamp_micros())
        .map_err(|_| unreadable())
    };
    let parsed: ArrayRef = match cursor_type {
        ColumnType::TimestampTz => {
            Arc::new(TimestampMicrosecondArray::from(vec![micros()?]).with_timezone("UTC"))
        }
        ColumnType::TimestampNtz => Arc::new(TimestampMicrosecondArray::from(vec![micros()?])),
        ColumnType::Date => {
            let date = NaiveDate::parse_from_str(&normal, "%Y-%m-%d").map_err(|_| unreadable())?;
            let epoch = NaiveDate::from_ymd_opt(1970, 1, 1).unwrap_or_default();
            let days = i32::try_from((date - epoch).num_days()).map_err(|_| unreadable())?;
            Arc::new(Date32Array::from(vec![days]))
        }
        _ => Arc::new(StringArray::from(vec![normal.clone()])),
    };
    let strict = CastOptions {
        safe: false,
        ..CastOptions::default()
    };
    cast_with_options(&parsed, data_type, &strict).map_err(|_| unreadable())
}

fn invalid(error: impl std::fmt::Display) -> LoaderError {
    LoaderError::Invalid(error.to_string())
}

impl PythonFeed {
    /// Call the function for each window (or once), committing each window, then commit the rest.
    pub(crate) async fn load(&mut self, load: &PythonLoad<'_>) -> Result<TableLoaded, LoaderError> {
        let windows = match &load.table.commits {
            CommitRule::Windows(windows) => Some(windows),
            _ => None,
        };
        let calls = windows.map_or(1, |windows| windows.count);
        let table = load.table.spec.source_table.name.clone();
        for index in 0..calls {
            let window: Option<CursorWindow> = windows
                .map(|windows| window_at(windows, index))
                .transpose()
                .map_err(LoaderError::Invalid)?;
            let stream =
                start_read(load.host.clone(), table_read(load, window.as_ref(), self)).await?;
            self.read(load, stream, window.as_ref()).await?;
            if load.table.logged.is_some()
                && let Some(state) = final_state(load.host.clone(), table.clone()).await?
            {
                self.state = Some(state);
            }
            let state = self.state.clone();
            if let Some(window) = window
                && (self.publisher.is_some() || load.table.exists)
            {
                let publisher = self.publisher(load).await?;
                publisher.state = state;
                publisher
                    .cut(CommitRange::Window {
                        from: window.from.clone(),
                        to: window.to.clone(),
                    })
                    .await?;
            }
        }
        if self.outside > 0 {
            tracing::warn!(
                table = %load.table.spec.destination.table,
                rows = self.outside,
                "skipped rows outside the cursor window; use ctx.cursor_value to read only new data"
            );
        }
        let declared = load
            .table
            .spec
            .python
            .as_ref()
            .map_or(0, |python| python.declared.len());
        if self.rows == 0 {
            refuse_emptying(load.table, load.destination).await?;
        }
        if self.publisher.is_none() && self.rows == 0 && !load.table.exists && declared == 0 {
            empty_problem(load.table)?;
            let run = run_entry(
                load,
                load.table,
                (self.state.clone(), self.reconciled.clone()),
            );
            log_empty_run(run.as_ref(), load.table, load.destination).await?;
            return Ok(TableLoaded {
                rows: 0,
                changes: Vec::new(),
                note: Some(NO_DATA_NOTE.to_owned()),
                after_load: None,
            });
        }
        let unsettled = self.publisher.is_none()
            || self
                .merged
                .fields()
                .iter()
                .any(|field| *field.data_type() == arrow_schema::DataType::Null);
        if unsettled {
            self.plan_data(load, true).await?;
        }
        let state = self.state.clone();
        let publisher = self.publisher(load).await?;
        let rows = publisher.finish(state).await?;
        let children = publisher.child_changes();
        self.changes.extend(children);
        Ok(TableLoaded {
            rows,
            changes: std::mem::take(&mut self.changes),
            note: None,
            after_load: None,
        })
    }

    /// Read one call of the table function to its end: each batch is typed and staged as it arrives (the plan and the table grow with new columns first), and a table without a cursor commits about every 1 GB; a logged table's `ctx.state` follows the rows that crossed with it, so its commits fall only there, recording that state.
    pub(crate) async fn read(
        &mut self,
        load: &PythonLoad<'_>,
        mut stream: Box<dyn BatchStream>,
        window: Option<&CursorWindow>,
    ) -> Result<(), LoaderError> {
        loop {
            let (returned, next) = tokio::task::spawn_blocking(move || {
                let next = stream.next_batch();
                (stream, next)
            })
            .await
            .map_err(|error| {
                LoaderError::Invalid(format!("the table function's thread failed: {error}"))
            })?;
            stream = returned;
            match next {
                None => return Ok(()),
                Some(Err(failure)) => return Err(LoaderError::Read(failure)),
                Some(Ok(Streamed::State(state))) => {
                    let state = (state != NO_STATE).then_some(state);
                    let sized = self.table.commits == CommitRule::Size;
                    self.state = state.clone();
                    if let Some(publisher) = &mut self.publisher {
                        publisher.state = state;
                        if sized && publisher.full() {
                            publisher.cut(CommitRange::Planned).await?;
                        }
                    }
                }
                Some(Ok(Streamed::Batch(batch))) => self.batch(load, batch, window).await?,
            }
        }
    }

    /// Refuse a column that one batch has as a field and another made by flattening a listed object.
    fn check_flattened(&mut self, batch: &RecordBatch) -> Result<(), LoaderError> {
        let made = flattened_columns(&batch.schema());
        for field in batch.schema().fields() {
            let flattened = made.contains(field.name());
            let seen = *self
                .flattened
                .entry(field.name().clone())
                .or_insert(flattened);
            if seen != flattened {
                return Err(LoaderError::Invalid(format!(
                    "flatten: column {} comes both from a field of the rows and from flattening a listed object; rename the field or leave the object unflattened",
                    field.name()
                )));
            }
        }
        Ok(())
    }

    /// Stage one batch: when it changes the merged schema, plan the table for the data so far first (one read of the live columns, the schema changes applied once).
    async fn batch(
        &mut self,
        load: &PythonLoad<'_>,
        batch: RecordBatch,
        window: Option<&CursorWindow>,
    ) -> Result<(), LoaderError> {
        self.check_flattened(&batch)?;
        let batch = dlt_named(batch, &self.table.spec)?;
        let merged = merge_schema(&self.merged, &batch.schema());
        self.rows += batch.num_rows() as u64;
        let chosen = match (&self.table.spec.unnest, &mut self.table.spec.python) {
            (Unnest::All, Some(python)) => {
                choose_lists(&batch, &mut python.lists).map_err(LoaderError::Invalid)?
            }
            _ => false,
        };
        if merged != self.merged || self.publisher.is_none() || chosen {
            self.merged = merged;
            self.plan_data(load, false).await?;
        }
        let schema: SchemaRef = Arc::new(settled_schema(&self.merged));
        let read = match window {
            Some(window) => Some(self.read_window(window)),
            None => self.table.window.clone(),
        };
        let (typed, outside) = typed_batch(&batch, &self.table, &schema, read.as_ref())?;
        self.outside += outside as u64;
        let sized = self.table.commits == CommitRule::Size && self.table.logged.is_none();
        let publisher = self.publisher(load).await?;
        publisher.stage(typed, None).await?;
        match (sized, publisher.full()) {
            (true, true) => publisher.cut(CommitRange::Planned).await,
            _ => Ok(()),
        }
    }

    /// Plan the table for the data seen so far and bring the table (and the staging table) in line: created on the first batch, altered as later batches add or widen columns; columns that only held NULL wait for a typed value, or become text at the end (`settle`).
    pub(crate) async fn plan_data(
        &mut self,
        load: &PythonLoad<'_>,
        settle: bool,
    ) -> Result<(), LoaderError> {
        let nulls: BTreeSet<String> = self
            .merged
            .fields()
            .iter()
            .filter(|field| *field.data_type() == DataType::Null)
            .map(|field| field.name().clone())
            .collect();
        let live = self.table.live.as_deref();
        let typed_elsewhere = |name: &str| {
            live.is_some_and(|live| {
                live.iter()
                    .any(|column| column.name.eq_ignore_ascii_case(name))
            }) || load.table.spec.pinned.keys().any(|pinned| pinned == name)
        };
        let fields: Vec<_> = self
            .merged
            .fields()
            .iter()
            .filter(|field| {
                settle || !nulls.contains(field.name()) || typed_elsewhere(field.name())
            })
            .cloned()
            .collect();
        let schema = settled_schema(&Schema::new(fields));
        let data = YieldedData {
            schema: &schema,
            null_columns: &nulls,
            rows: self.rows,
        };
        let destination = match &self.publisher {
            Some(publisher) => &publisher.destination,
            None => load.destination,
        };
        let known = self
            .publisher
            .is_none()
            .then_some(self.table.live.as_deref());
        let replanned = replan(load, &self.table, (&data, known), destination).await?;
        let old = staged_columns(&self.table);
        let changes = bring_in_line(
            (load.destination, self.publisher.as_mut()),
            (&old, &replanned),
        )
        .await?;
        self.changes.extend(changes);
        self.table = TablePlan {
            live: replanned.live.clone().or(self.table.live.clone()),
            ..replanned
        };
        Ok(())
    }

    /// The publisher, opened on first use with the table as planned for the data.
    pub(crate) async fn publisher(
        &mut self,
        load: &PythonLoad<'_>,
    ) -> Result<&mut Publisher, LoaderError> {
        if self.publisher.is_none() {
            let run = run_entry(
                load,
                &self.table,
                (self.state.clone(), self.reconciled.clone()),
            );
            let mut publisher =
                Publisher::open(self.table.clone(), (load.destination, load.options), run).await?;
            publisher.state = self.state.clone();
            self.publisher = Some(publisher);
        }
        self.publisher
            .as_mut()
            .ok_or_else(|| LoaderError::Invalid("the table's load could not start".to_owned()))
    }

    fn read_window(&self, window: &CursorWindow) -> ReadWindow {
        ReadWindow {
            column: self
                .table
                .spec
                .cursor
                .as_ref()
                .map(|cursor| cursor.column.clone())
                .unwrap_or_default(),
            from: window.from.clone(),
            to: window.to.clone(),
            inclusive: window.inclusive,
            maximum: None,
        }
    }
}

/// Fail a table whose function yielded no rows when its `min_rows` check needs some (unless the check only warns).
fn empty_problem(table: &TablePlan) -> Result<(), LoaderError> {
    let minimum = table.spec.checks.as_ref().and_then(|checks| {
        checks
            .min_rows
            .filter(|minimum| *minimum > 0 && !checks.warn.contains(&CheckName::MinRows))
    });
    match minimum {
        Some(minimum) => Err(LoaderError::Invalid(format!(
            "checks.min_rows failed: 0 row(s) staged, fewer than the {minimum} required; nothing was published"
        ))),
        None => Ok(()),
    }
}

/// `planned` with its columns planned again for those `data` has (`known` being the live columns when they need no read); everything else stays as planned before the read, so it matches what the function was asked for.
async fn replan(
    load: &PythonLoad<'_>,
    planned: &TablePlan,
    (data, known): (&YieldedData<'_>, Option<Option<&[LiveColumn]>>),
    destination: &Destination,
) -> Result<TablePlan, LoaderError> {
    let spec = &planned.spec;
    let problems = declared_problems(spec, data);
    if !problems.is_empty() {
        return Err(LoaderError::Invalid(problems.join("; ")));
    }
    let declared = spec
        .python
        .as_ref()
        .map(|python| python.declared.clone())
        .unwrap_or_default();
    let table = inferred_table(
        (&load.table.spec.source_table, load.table.spec.naming),
        &declared,
        data,
        planned.live.as_deref(),
    );
    let mut project = load.project.clone();
    if let Some(source) = project.sources.get_mut(&spec.source_name) {
        source.tables.retain(|name, _| *name == table.name);
    }
    let resolved = resolve_tables(&project, &spec.source_name, &[table]);
    let problems: Vec<String> = resolved
        .problems
        .into_iter()
        .chain(resolved.skipped.into_iter().map(|skipped| skipped.reason))
        .collect();
    let mut inferred = match (problems.is_empty(), resolved.specs.into_iter().next()) {
        (true, Some(inferred)) => inferred,
        _ => return Err(LoaderError::Invalid(problems.join("; "))),
    };
    inferred.python = spec.python.clone();
    inferred.approved = true;
    let columns = replan_columns(
        &inferred,
        (destination, planned.live.as_deref()),
        known,
        load.options,
    )
    .await?;
    let cursor_type = columns.spec.cursor.as_ref().and_then(|cursor| {
        columns
            .schema
            .write_columns
            .iter()
            .find(|column| column.name.eq_ignore_ascii_case(&cursor.column))
            .map(|column| &column.column_type)
    });
    if let Some(problem) = append_problem(&columns.spec, cursor_type) {
        return Err(LoaderError::Invalid(problem));
    }
    let loaded: Vec<String> = columns
        .schema
        .write_columns
        .iter()
        .map(|column| column.name.clone())
        .chain(data.null_columns.iter().cloned())
        .collect();
    let problems: Vec<String> = columns
        .spec
        .checks
        .iter()
        .flat_map(|checks| check_column_problems(checks, &loaded))
        .collect();
    if !problems.is_empty() {
        return Err(LoaderError::Invalid(problems.join("; ")));
    }
    Ok(TablePlan {
        spec: columns.spec,
        read_columns: columns.read_columns,
        live: columns.live,
        schema: columns.schema,
        ..planned.clone()
    })
}

/// What the table function is called with: the window to read (`ctx.cursor_value` / `ctx.cursor_end`) and the latest `ctx.state`; connections that targets write to are withheld, except the source's own s3 or filesystem connection.
fn table_read(
    load: &PythonLoad<'_>,
    window: Option<&CursorWindow>,
    feed: &PythonFeed,
) -> TableRead {
    let table = load.table;
    let spec = &table.spec;
    let connection = load
        .project
        .sources
        .get(&spec.source_name)
        .map(|source| source.connection.clone())
        .filter(|name| !name.is_empty());
    let withheld: Vec<String> = load
        .project
        .targets
        .values()
        .map(|target| target.connection.clone())
        .filter(|name| {
            Some(name) != connection.as_ref() || !shareable_connection(load.project, name)
        })
        .collect();
    let mut connections = load.project.connection_settings.clone();
    connections.retain(|name, _| !withheld.contains(name));
    let cursor = table.window.as_ref().map(|planned| CursorRead {
        column: planned.column.clone(),
        column_type: cursor_type_name(&feed.table, &planned.column),
        start: window.map_or(planned.from.clone(), |window| window.from.clone()),
        end: window.map_or(planned.to.clone(), |window| window.to.clone()),
        inclusive: window.map_or(planned.inclusive, |window| window.inclusive),
    });
    TableRead {
        source: spec.source_name.clone(),
        table: spec.source_table.name.clone(),
        connection,
        connections,
        withheld,
        cursor,
        logged: table.logged.is_some(),
        state: feed.state.clone(),
        is_reload: load.options.reload,
        flatten: spec.flatten.clone(),
        masked: spec.masked.columns.keys().cloned().collect(),
        arrays: spec
            .pinned
            .iter()
            .filter(|(_, column_type)| matches!(column_type, ColumnType::Array(_)))
            .map(|(name, _)| name.clone())
            .collect(),
        naming: (
            spec.naming,
            sql_adapter(spec.warehouse).max_identifier_length(),
        ),
    }
}

/// Bring a table planned from its data in line with `replanned`, through `publisher`'s connection once it is open (`destination` before): its schema changes run, and an open publisher's rows so far are settled and its staging table altered from the `old` staged columns, before it takes the new plan. Returns the `created ...` and `altered ...` lines.
pub(crate) async fn bring_in_line(
    (destination, publisher): (&Destination, Option<&mut Publisher>),
    (old, replanned): (&[DesiredColumn], &TablePlan),
) -> Result<Vec<String>, LoaderError> {
    let mut changes: Vec<String> = Vec::new();
    let destination = match &publisher {
        Some(publisher) => &publisher.destination,
        None => destination,
    };
    if let Some(statements) = schema_statements(replanned) {
        execute_ddl(destination, statements).await?;
        changes.extend(change_lines(replanned));
    }
    if let Some(publisher) = publisher {
        publisher.settle_rows().await?;
        publisher.reopen_rows();
        if let Some(staging) = &publisher.staging {
            let altered = sql_adapter(replanned.spec.warehouse).render_staging_changes(
                staging,
                (old, &staged_columns(replanned)),
                publisher.started_with(),
            );
            execute_ddl(&publisher.destination, altered).await?;
        }
        publisher.table = replanned.clone();
        publisher.widen_files().await?;
    }
    Ok(changes)
}

/// A child table's plan before its first rows: the parent's settings, with columns inferred from its rows as a Python table's are, each row given an id, and no key, cursor, checks or column rules of the parent's; a `replace` replaces it with the parent, every other load appends to it.
fn child_plan(parent: &TablePlan, (path, table): (&str, &str)) -> TablePlan {
    let spec = &parent.spec;
    let child = TableSpec {
        source_table: SourceTable {
            name: table.to_owned(),
            columns: Vec::new(),
            primary_key: Vec::new(),
            indexed_columns: Vec::new(),
            comment: None,
            ..spec.source_table.clone()
        },
        destination: TableName {
            table: table.to_owned(),
            ..spec.destination.clone()
        },
        description: format!(
            "The items of the {path} lists of {}.",
            spec.destination.table
        ),
        unique_key: Vec::new(),
        dedup_sort: None,
        cursor: None,
        batch: None,
        constants: Vec::new(),
        shared_destination: false,
        source_end: None,
        pinned: spec
            .child_pinned
            .iter()
            .filter_map(|(key, column_type)| {
                let column = key.strip_prefix(path)?.strip_prefix('.')?;
                (!column.contains('.')).then(|| (column.to_owned(), column_type.clone()))
            })
            .collect(),
        child_pinned: BTreeMap::new(),
        excluded_columns: Vec::new(),
        masked: MaskRules::default(),
        logged: false,
        files: None,
        flatten: Vec::new(),
        row_id: true,
        root_rows: matches!(parent.mode, WriteMode::Merge { .. }),
        unnest: Unnest::None,
        python: Some(PythonTable {
            module: String::new(),
            declared: Vec::new(),
            lists: ListChoices::new(),
        }),
        sheet: None,
        cdc: None,
        checks: None,
        warehouse_source: None,
        layout: TableLayout::default(),
        ..spec.clone()
    };
    TablePlan {
        spec: child,
        read_columns: Vec::new(),
        live: None,
        schema: SchemaPlan {
            action: TableAction::Create,
            write_columns: Vec::new(),
            warnings: Vec::new(),
            blocked: None,
        },
        mode: match parent.mode {
            WriteMode::Replace => WriteMode::Replace,
            WriteMode::Recreate => WriteMode::Recreate,
            _ => WriteMode::Append,
        },
        source_filter: None,
        source_parameters: Vec::new(),
        cursor_note: None,
        window: None,
        route: parent.route.clone(),
        files: None,
        output: None,
        exists: false,
        logged: None,
        commits: CommitRule::Size,
        stage: parent.stage.clone(),
        planned_stage: None,
    }
}

/// The ids a child batch's rows were given, from its row id column.
fn given_ids(batch: &RecordBatch, naming: Naming) -> Result<Vec<String>, LoaderError> {
    let name = metadata_name(naming, MetadataRole::RowId);
    let ids = batch
        .column_by_name(name)
        .and_then(|column| column.as_any().downcast_ref::<StringArray>())
        .ok_or_else(|| {
            LoaderError::Invalid(format!("a child table's rows have no {name} column"))
        })?;
    Ok(ids
        .iter()
        .map(|id| id.unwrap_or_default().to_owned())
        .collect())
}

/// The child table the items of `spec`'s list field at dotted `path` land in, such as `orders__items__parts` for `items.parts`.
pub(crate) fn unnested_table(spec: &TableSpec, path: &str) -> String {
    let mut table = spec.destination.table.clone();
    for field in path.split('.') {
        table = child_table_name(
            (
                spec.naming,
                sql_adapter(spec.warehouse).max_identifier_length(),
            ),
            &table,
            field,
        );
    }
    table
}

/// The staged ordinal of each child row's root, from the root's row in the parent's batch.
fn root_ordinals_of(root_rows: &[usize], ordinals: &[i64]) -> Vec<i64> {
    root_rows
        .iter()
        .map(|row| ordinals.get(*row).copied().unwrap_or_default())
        .collect()
}

impl ChildTables {
    /// Stage the child rows of one of the parent's batches: a child table is planned (and created or altered) when its rows bring new columns, then they wait in its staging table for the parent's next commit.
    pub(crate) async fn stage(
        &mut self,
        (parent, (load_id, loaded_at)): (&TablePlan, (&str, &str)),
        (batches, ordinals): (Vec<ChildBatch>, Option<&[i64]>),
    ) -> Result<(), LoaderError> {
        for child in batches {
            let roots = ordinals.map(|ordinals| root_ordinals_of(&child.root_rows, ordinals));
            let below =
                |path: &String| child.path == *path || child.path.starts_with(&format!("{path}."));
            if self.ignored.iter().any(below) {
                continue;
            }
            let index = match self.feeds.iter().position(|feed| feed.path == child.path) {
                Some(index) => index,
                None => {
                    let table = self
                        .child_table(parent, (&child.path, &child.table))
                        .await?;
                    if !self.admits((parent, &child, &table)).await? {
                        self.ignored.push(child.path.clone());
                        continue;
                    }
                    self.feeds.push(ChildFeed {
                        table,
                        path: child.path.clone(),
                        merged: Schema::empty(),
                        publisher: None,
                    });
                    self.feeds.len() - 1
                }
            };
            let feed = &mut self.feeds[index];
            let merged = merge_schema(&feed.merged, &child.batch.schema());
            if merged != feed.merged || feed.publisher.is_none() {
                feed.merged = merged;
                let changes = feed.plan((&self.destination, &self.options), false).await?;
                self.changes.extend(changes);
            }
            let ids = given_ids(&child.batch, parent.spec.naming)?;
            let schema: SchemaRef = Arc::new(settled_schema(&feed.merged));
            let (typed, _) = typed_batch(&child.batch, &feed.table, &schema, None)?;
            let publisher = match &mut feed.publisher {
                Some(publisher) => publisher,
                None => {
                    let mut opened = Publisher::open(
                        feed.table.clone(),
                        (&self.destination, &self.options),
                        None,
                    )
                    .await?;
                    opened.load_id = load_id.to_owned();
                    opened.loaded_at = loaded_at.to_owned();
                    feed.publisher.insert(opened)
                }
            };
            publisher.stage_rows(typed, (Some(ids), roots)).await?;
        }
        Ok(())
    }

    /// The plan of the child table at `path` before its first rows (see [`child_plan`]); on a files target with its folder's files, as the target's plan reads them, and refused while another load holds the folder.
    async fn child_table(
        &self,
        parent: &TablePlan,
        (path, table): (&str, &str),
    ) -> Result<TablePlan, LoaderError> {
        let planned = child_plan(parent, (path, table));
        let Some(files) = files_destination(&self.destination) else {
            return Ok(planned);
        };
        let read = plan_files_table(&planned.spec, files, &self.options).await?;
        if let Some(blocked) = &read.schema.blocked {
            return Err(LoaderError::Invalid(format!(
                "child table {table}: {}",
                blocked.reasons.join("; ")
            )));
        }
        Ok(TablePlan {
            live: read.live,
            exists: read.exists,
            output: read.output,
            ..planned
        })
    }

    /// Whether the child table `table` is there already: on a files target, whether its folder holds files.
    async fn exists(&self, table: &TablePlan) -> Result<bool, LoaderError> {
        match files_destination(&self.destination) {
            Some(_) => Ok(table.exists),
            None => Ok(live_columns(&self.destination, &table.spec.destination)
                .await?
                .is_some()),
        }
    }

    /// Whether `child`'s table may be filled: always on the table's first load; after it, a child table that is not there yet is a schema change, which `on_schema_change` decides like a new column: added, left out with its rows and everything below (`"ignore"`, `false`), or refused.
    async fn admits(
        &self,
        (parent, child, table): (&TablePlan, &ChildBatch, &TablePlan),
    ) -> Result<bool, LoaderError> {
        let spec = &parent.spec;
        if !parent.exists || spec.on_schema_change == OnSchemaChange::AppendNewColumns {
            return Ok(true);
        }
        if self.exists(table).await? {
            return Ok(true);
        }
        match spec.on_schema_change {
            OnSchemaChange::Fail => Err(LoaderError::Invalid(format!(
                "new list field {} would make the child table {} and on_schema_change = \"fail\"",
                child.path, child.table
            ))),
            _ => {
                tracing::warn!(table = %child.table, field = %child.path, "new list field is not loaded (on_schema_change = \"ignore\")");
                Ok(false)
            }
        }
    }

    /// Commit every child table's rows so far, before the parent's commit (of `staging`'s rows by `mode`, `None` for a files target's) names them: under a merge or `delete_insert`, once the rows of the roots it replaces are cleared (see [`Self::clear_replaced`]).
    pub(crate) async fn cut(
        &mut self,
        (parent, destination, commit): (&TablePlan, &Destination, Option<(&TableName, &WriteMode)>),
    ) -> Result<(), LoaderError> {
        if let Some(commit) = commit {
            self.clear_replaced((parent, destination, commit)).await?;
        }
        for publisher in self
            .feeds
            .iter_mut()
            .filter_map(|feed| feed.publisher.as_mut())
        {
            publisher.cut(CommitRange::Planned).await?;
        }
        Ok(())
    }

    /// Finish every child table before the parent (`commit`, its last commit's staging table and mode, when one follows): columns that only held NULL become text, and what waits commits; under a `replace` (or `--reload`) an existing child table no rows reached (a listed one, or under `"all"` any named as the table's) is emptied too, so it never keeps an earlier load's items.
    pub(crate) async fn finish(
        &mut self,
        (parent, (load_id, loaded_at)): (&TablePlan, (&str, &str)),
        (destination, commit): (&Destination, Option<(&TableName, &WriteMode)>),
    ) -> Result<(), LoaderError> {
        if let Some(commit) = commit {
            self.clear_replaced((parent, destination, commit)).await?;
        }
        let replaced = matches!(parent.mode, WriteMode::Replace | WriteMode::Recreate);
        let spec = &parent.spec;
        let adapter = sql_adapter(spec.warehouse);
        let candidates: Vec<(String, String)> = match &spec.unnest {
            Unnest::Paths(paths) if replaced => paths
                .iter()
                .map(|path| (path.clone(), unnested_table(spec, path)))
                .collect(),
            Unnest::All if replaced => {
                child_paths(spec, live_child_tables(&self.destination, spec).await?)
            }
            _ => Vec::new(),
        };
        for (path, table) in &candidates {
            let fed = self.feeds.iter().any(|feed| {
                adapter.stored_table_name(&feed.table.spec.destination.table)
                    == adapter.stored_table_name(table)
            });
            if fed {
                continue;
            }
            let mut feed = ChildFeed {
                table: self.child_table(parent, (path, table)).await?,
                path: path.clone(),
                merged: Schema::empty(),
                publisher: None,
            };
            if !self.exists(&feed.table).await? {
                continue;
            }
            feed.plan((&self.destination, &self.options), true).await?;
            let mut opened =
                Publisher::open(feed.table.clone(), (&self.destination, &self.options), None)
                    .await?;
            opened.load_id = load_id.to_owned();
            opened.loaded_at = loaded_at.to_owned();
            feed.publisher = Some(opened);
            self.feeds.push(feed);
        }
        for feed in &mut self.feeds {
            let unsettled = feed
                .merged
                .fields()
                .iter()
                .any(|field| *field.data_type() == DataType::Null);
            if unsettled {
                let changes = feed.plan((&self.destination, &self.options), true).await?;
                self.changes.extend(changes);
            }
            if let Some(publisher) = &mut feed.publisher {
                publisher.finish(None).await?;
            }
        }
        Ok(())
    }

    /// Before the commit of the parent's rows staged in `staging` (on the parent's `destination`) by `mode`, every child table's rows whose root that commit replaces are deleted, so a root's items are replaced by its new ones, an empty or missing list leaving none: under a merge the roots it merges (and the staged child rows of the parent rows it drops, a repeated key's others, leave each child table's staging table), under `delete_insert` the rows in its window or partitions. Each runs on its own, before the child and parent commits: a failure between them leaves a root without its items until a rerun replaces it again.
    async fn clear_replaced(
        &mut self,
        (parent, destination, (staging, mode)): (
            &TablePlan,
            &Destination,
            (&TableName, &WriteMode),
        ),
    ) -> Result<(), LoaderError> {
        if !replaces_roots(mode) {
            return Ok(());
        }
        let spec = &parent.spec;
        let adapter = sql_adapter(spec.warehouse);
        for publisher in self
            .feeds
            .iter_mut()
            .filter_map(|feed| feed.publisher.as_mut())
        {
            publisher.flush_batch().await?;
        }
        let request = WriteStatementsRequest {
            table: &spec.destination,
            columns: &parent.schema.write_columns,
            mode,
            hashed: &[],
            built: None,
        };
        let dropped: Vec<i64> = match adapter.render_dropped_ordinals(&request, staging) {
            Some(query) => text_values(destination, query)
                .await?
                .iter()
                .map(|text| {
                    text.parse::<i64>().map_err(|error| {
                        LoaderError::Invalid(format!("a dropped row's ordinal {text}: {error}"))
                    })
                })
                .collect::<Result<_, _>>()?,
            None => Vec::new(),
        };
        let root_row = adapter.render_identifier(ROOT_ROW_COLUMN);
        for publisher in self
            .feeds
            .iter_mut()
            .filter_map(|feed| feed.publisher.as_mut())
        {
            let Some(child_staging) = publisher.staging.clone() else {
                continue;
            };
            let deletes: Vec<String> = dropped
                .chunks(DROPPED_CHUNK)
                .map(|chunk| {
                    let listed: Vec<String> = chunk.iter().map(i64::to_string).collect();
                    adapter.render_delete_staged(
                        &child_staging,
                        &format!("{root_row} IN ({})", listed.join(", ")),
                    )
                })
                .collect();
            execute(&publisher.destination, deletes).await?;
        }
        let root_name = metadata_name(spec.naming, MetadataRole::RootId);
        let row_id_name = metadata_name(spec.naming, MetadataRole::RowId);
        let row_id = adapter.render_identifier(row_id_name);
        let root = adapter.render_identifier(root_name);
        let mut tables: Vec<String> = self.merged.clone();
        for feed in self.feeds.iter().filter(|feed| feed.publisher.is_some()) {
            let table = &feed.table.spec.destination.table;
            if !tables
                .iter()
                .any(|known| adapter.stored_table_name(known) == adapter.stored_table_name(table))
            {
                tables.push(table.clone());
            }
        }
        let mut rooted: Vec<(usize, TableName)> = Vec::new();
        for (index, table) in tables.into_iter().enumerate() {
            let name = TableName {
                table,
                ..spec.destination.clone()
            };
            let has_root = live_columns(destination, &name)
                .await?
                .is_some_and(|columns| {
                    columns
                        .iter()
                        .any(|column| column.name.eq_ignore_ascii_case(root_name))
                });
            if has_root {
                rooted.push((index, name));
            }
        }
        if rooted.is_empty() {
            return Ok(());
        }
        let mut roots = match mode {
            WriteMode::DeleteInsert { .. } => format!(
                "{root} IN (SELECT {row_id} FROM {}{})",
                adapter.render_qualified_name(&spec.destination),
                adapter
                    .render_replaced_rows(&request)
                    .map(|condition| format!(" WHERE {condition}"))
                    .unwrap_or_default()
            ),
            _ => format!(
                "{root} IN (SELECT {row_id} FROM {})",
                adapter.render_qualified_name(staging)
            ),
        };
        if let Some(rekeyed) = adapter.render_rekeyed_row_ids(&request, staging, row_id_name) {
            let found = text_values(destination, format!("{rekeyed} LIMIT 1")).await?;
            if !found.is_empty() {
                roots = format!("{roots} OR {root} IN ({rekeyed})");
            }
        }
        let load_id = self
            .feeds
            .iter()
            .find_map(|feed| feed.publisher.as_ref())
            .map(|publisher| publisher.load_id.clone());
        let deletes: Vec<String> = rooted
            .iter()
            .flat_map(|(index, name)| {
                let scratch = TableName {
                    table: format!("{}_{index}", staging.table),
                    ..staging.clone()
                };
                let roots = match mode {
                    WriteMode::Changes(change) => changed_roots(
                        (spec, &name.table),
                        (staging, change),
                        (root_name, row_id_name),
                        load_id.as_deref(),
                    ),
                    _ => roots.clone(),
                };
                adapter.render_clear_rows(name, &roots, &scratch)
            })
            .collect();
        execute(destination, deletes).await
    }

    /// Close every child table's publisher; best effort.
    pub(crate) async fn close(&mut self) {
        for publisher in self
            .feeds
            .iter_mut()
            .filter_map(|feed| feed.publisher.as_mut())
        {
            publisher.close().await;
        }
    }
}

/// `unnested` without its deletes' child rows when `table` applies captured changes (a changelog keeps them): with `REPLICA IDENTITY FULL` a delete carries its row's old lists, whose items stay loaded under soft deletes and are cleared under hard ones.
pub(crate) fn without_deleted_items(
    table: &TablePlan,
    batch: &RecordBatch,
    mut unnested: Unnested,
) -> Result<Unnested, String> {
    let WriteMode::Changes(change) = &table.mode else {
        return Ok(unnested);
    };
    let Ok(index) = batch.schema().index_of(CDC_OP_COLUMN) else {
        return Ok(unnested);
    };
    if change.apply == ChangeApply::Changelog {
        return Ok(unnested);
    }
    let ops = cast_with_options(
        batch.column(index),
        &DataType::Utf8,
        &CastOptions::default(),
    )
    .map_err(|error| error.to_string())?;
    let ops = ops.as_string::<i32>();
    let deleted: Vec<bool> = (0..ops.len())
        .map(|row| ops.is_valid(row) && ops.value(row) == DELETE_OP)
        .collect();
    for child in &mut unnested.children {
        let keep: BooleanArray = child
            .root_rows
            .iter()
            .map(|row| Some(!deleted[*row]))
            .collect();
        child.batch =
            filter_record_batch(&child.batch, &keep).map_err(|error| error.to_string())?;
        child.root_rows.retain(|row| !deleted[*row]);
    }
    Ok(unnested)
}

/// Under captured changes, the condition choosing the rows of `table`, a child table of `spec`'s, whose roots the changes in `staging` replace: every changed root (in a changelog, rows a replay wrote before), but under `merge` a root keeps its items when its list field came unchanged (a TOASTed value the change left out) and, under soft deletes, when it is deleted, as its row stays; under hard deletes a truncate also takes the roots it removes. A child table under captured changes appends its rows straight in, so this load's own rows (`load_id`) are never among them. Under `unnest = "all"` the list field is the source column whose child table `table` is or is below, the longest when names overlap (`line__items` beside `line`), as a column name may hold the separator.
fn changed_roots(
    (spec, table): (&TableSpec, &str),
    (staging, change): (&TableName, &ChangeWrite),
    (root_name, row_id_name): (&str, &str),
    load_id: Option<&str>,
) -> String {
    let adapter = sql_adapter(spec.warehouse);
    let id = |name: &str| adapter.render_identifier(name);
    let text = |value: &str| adapter.render_loader_value_literal(value);
    let stored = adapter.stored_table_name(table);
    let field: Option<String> = match &spec.unnest {
        Unnest::Paths(paths) => paths
            .iter()
            .find(|path| adapter.stored_table_name(&unnested_table(spec, path)) == stored)
            .and_then(|path| path.split('.').next())
            .map(str::to_owned),
        _ => spec
            .source_table
            .columns
            .iter()
            .map(|column| column.name.as_str())
            .filter(|column| {
                let child = adapter.stored_table_name(&child_table_name(
                    (spec.naming, adapter.max_identifier_length()),
                    &spec.destination.table,
                    column,
                ));
                stored == child || stored.starts_with(&format!("{child}{CHILD_TABLE_SEPARATOR}"))
            })
            .max_by_key(|column| column.len())
            .map(str::to_owned),
    };
    let mut kept: Vec<String> = Vec::new();
    if let Some(field) = field.filter(|_| change.apply != ChangeApply::Changelog) {
        kept.push(format!(
            "POSITION({} IN COALESCE({}, {})) = 0",
            text(&format!(",{field},")),
            id(CDC_UNCHANGED_COLUMN),
            text("")
        ));
    }
    if change.apply == ChangeApply::SoftDeletes {
        kept.push(format!("{} <> {}", id(CDC_OP_COLUMN), text(DELETE_OP)));
    }
    let filter = match kept.is_empty() {
        true => String::new(),
        false => format!(" WHERE {}", kept.join(" AND ")),
    };
    let root = id(root_name);
    let row_id = id(row_id_name);
    let changed = format!(
        "{root} IN (SELECT {row_id} FROM {}{filter})",
        adapter.render_qualified_name(staging)
    );
    let replaced = match (&change.truncated, change.apply) {
        (Some(truncation), ChangeApply::HardDeletes) => format!(
            "{changed} OR {root} IN (SELECT {row_id} FROM {} WHERE {} < {})",
            adapter.render_qualified_name(&spec.destination),
            id(CDC_LSN_COLUMN),
            truncation.lsn
        ),
        _ => changed,
    };
    match load_id {
        Some(load_id) => format!(
            "({replaced}) AND {} <> {}",
            id(metadata_name(spec.naming, MetadataRole::LoadId)),
            text(load_id)
        ),
        None => replaced,
    }
}

impl ChildFeed {
    /// Plan the child table for its rows so far and bring it in line, as [`PythonFeed::plan_data`] does a Python table; returns the `created ...` and `altered ...` lines.
    async fn plan(
        &mut self,
        (destination, options): (&Destination, &PlanOptions),
        settle: bool,
    ) -> Result<Vec<String>, LoaderError> {
        let nulls: BTreeSet<String> = self
            .merged
            .fields()
            .iter()
            .filter(|field| *field.data_type() == DataType::Null)
            .map(|field| field.name().clone())
            .collect();
        let live = self.table.live.as_deref();
        let fields: Vec<_> = self
            .merged
            .fields()
            .iter()
            .filter(|field| {
                settle || !nulls.contains(field.name()) || live_type(live, field.name()).is_some()
            })
            .cloned()
            .collect();
        let schema = settled_schema(&Schema::new(fields));
        let data = YieldedData {
            schema: &schema,
            null_columns: &nulls,
            rows: 0,
        };
        let spec = TableSpec {
            source_table: inferred_table(
                (&self.table.spec.source_table, self.table.spec.naming),
                &[],
                &data,
                live,
            ),
            ..self.table.spec.clone()
        };
        let reader = match &self.publisher {
            Some(publisher) => &publisher.destination,
            None => destination,
        };
        let columns = replan_columns(&spec, (reader, live), None, options).await?;
        let replanned = TablePlan {
            exists: columns.live.is_some(),
            spec: columns.spec,
            read_columns: columns.read_columns,
            live: columns.live,
            schema: columns.schema,
            ..self.table.clone()
        };
        let old = staged_columns(&self.table);
        let changes =
            bring_in_line((destination, self.publisher.as_mut()), (&old, &replanned)).await?;
        self.table = TablePlan {
            live: replanned.live.clone().or(self.table.live.clone()),
            ..replanned
        };
        Ok(changes)
    }
}