sqlload 0.52.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
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
use crate::load_naming::models::Naming;
use crate::loader::constants::{
    FIX_LABEL, HOURS_OFFSET_LENGTH, REASON_LABEL, ROUTE_LABEL, SECOND_TIMESTAMP_LENGTH,
    SOURCE_LABEL,
};
use crate::loader::models::UnreadSource;
use crate::project::models::CheckName;
use crate::run_log::models::RunEvent;
use std::collections::HashMap;
use std::time::Duration;

use chrono::SecondsFormat;

use crate::adapters::contract::models::{ChangeApply, MergeDedupe, WriteMode};
use crate::adapters::dispatch::main::dialect::sql_adapter::sql_adapter;
use crate::adapters::dispatch::main::route::sends_rows::sends_rows;
use crate::adapters::files::models::{PendingLoad, PendingPhase};
use crate::adapters::snowflake::models::PlannedStage;
use crate::batch_plan::constants::{BATCH_BYTES, MAX_BATCH_FILES};
use crate::column_types::models::ColumnType;
use crate::load_naming::main::is_load_column::is_load_column;
use crate::loader::_helpers::publish::records_files;
use crate::loader::_helpers::python::list_choices;
use crate::loader::_helpers::python::unnested_table;
use crate::loader::_helpers::resolve::display_name;
use crate::loader::_helpers::sources::{source_readers, unarchived};
use crate::loader::_helpers::table_plan::{
    archived_reason, is_direct, publishes_once, pulls_unloaded,
};
use crate::loader::constants::{
    CONSTANT_NOTE, DRAINED_REASON, LABEL_WIDTH, NULL_KEYS_NOTE, PARALLEL_READERS,
    PLAN_DETAIL_COLUMN, PRIMARY_KEY_PLACEHOLDER, RESULT_SCHEMA_VERSION,
};
use crate::loader::models::{
    CommitRule, FilesOutput, ObjectChange, PlanDetail, PlanItem, PlanView, ProjectPlan,
    PythonTable, RunReport, RunResult, RunSummary, SourceEnd, TableOutcome, TablePlan, TableResult,
    TallyLine, WarehouseRoute, WarehouseSource, WholeReason,
};
use crate::number_text::main::thousands::thousands;
use crate::project::models::BatchStep;
use crate::project::models::MaskMethod;
use crate::project::models::NullCursor;
use crate::project::models::{FilesFormat, OutputCompression};
use crate::routing::models::{Route, Step, Store};
use crate::row_batches::models::Unnest;
use crate::run_log::main::describe_state::describe_state;
use crate::run_log::main::runs_table::runs_table;
use crate::schema_plan::models::{ColumnChange, ColumnOrigin, TableAction};
use crate::store_paths::models::StoreKind;

/// How many renamed columns the one-table view's `columns` line names before `...`.
const SHOWN_RENAMES: usize = 2;

/// Details only a plan of one table shows: why its rows travel the way they do.
const ONE_TABLE_LABELS: [&str; 1] = ["why"];

/// Details the project view leaves out where a table has the text most of its tables have.
const SHARED_LABELS: [&str; 2] = ["batch", "schema"];

/// Details any table can have, which a plan of several tables leaves out even when no other table shares them: the batches of a table read whole.
const DEFAULT_DETAILS: [(&str, &str); 2] = [
    ("batch", "about every 1 GB"),
    (
        "batch",
        "about 1 GB at a time into a new table, swapped in once at the end",
    ),
];

/// The details an incremental table's `reads` and `commits` stand in for.
const REPLACED_LABELS: [&str; 3] = ["cursor", "state", "batch"];

/// Words in a source's error that say it could not be reached, as opposed to read: such an error is headed `can't connect` and gets a fix.
const CONNECTION_WORDS: [&str; 6] = [
    "cannot connect",
    "connection refused",
    "login failed",
    "password authentication failed",
    "timed out",
    "could not connect",
];

/// What an error's reason says, and the headline `plan` gives it.
const HEADLINES: [(&str, &str); 2] = [
    ("the source reports no primary key", "no key to merge on"),
    ("no write_strategy is set", "no write_strategy"),
];

/// A section: a heading with an optional right-aligned note, then each item as a tree branch; nothing when it has no items.
pub(crate) fn tree(heading: &str, note: &str, items: &[PlanItem]) -> Vec<String> {
    if items.is_empty() {
        return Vec::new();
    }
    let mut lines = vec![aligned(&format!("{heading} ({})", items.len()), note)];
    for (index, item) in items.iter().enumerate() {
        let last = index + 1 == items.len();
        let (branch, indent) = match last {
            true => ("└── ", "    "),
            false => ("├── ", "│   "),
        };
        lines.push(match item.action.is_empty() {
            true => format!("{branch}{}", item.name),
            false => format!("{branch}{}", row(&item.name, &item.action)),
        });
        for (detail_index, detail) in item.details.iter().enumerate() {
            let (sub, continued) = match detail_index + 1 == item.details.len() {
                true => ("└── ", "    "),
                false => ("├── ", "│   "),
            };
            let label = match detail.label.is_empty() {
                true => String::new(),
                false => format!("{:<width$} ", detail.label, width = LABEL_WIDTH - 1),
            };
            for (line_index, text) in detail.text.split('\n').enumerate() {
                lines.push(match line_index {
                    0 => format!("{indent}{sub}{label}{text}"),
                    _ => format!(
                        "{indent}{continued}{:width$}{text}",
                        "",
                        width = label.len()
                    ),
                });
            }
        }
    }
    lines.push(String::new());
    lines
}

fn aligned(left: &str, right: &str) -> String {
    match right.is_empty() {
        true => left.to_owned(),
        false => format!("{left:<width$}{right}", width = PLAN_DETAIL_COLUMN),
    }
}

/// `name` padded so `detail` starts in the plan's detail column (after a branch prefix), or two spaces after a name too long for it.
fn row(name: &str, detail: &str) -> String {
    format!("{name:<width$}  {detail}", width = PLAN_DETAIL_COLUMN - 6)
}

fn detail(label: &str, text: impl Into<String>) -> PlanDetail {
    PlanDetail {
        label: label.to_owned(),
        text: text.into(),
    }
}

/// A finding as an `Errors` item: the reason, and the fix where the text gives one after `Fix:`.
pub(crate) fn error_item(name: &str, reasons: &[String]) -> PlanItem {
    let mut details: Vec<PlanDetail> = Vec::new();
    for reason in reasons {
        match reason.split_once(" Fix: ") {
            Some((why, fix)) => {
                details.push(detail("reason", why.trim_end_matches('.')));
                details.push(detail("fix", fix));
            }
            None => details.push(detail("reason", reason.as_str())),
        }
    }
    PlanItem {
        name: name.to_owned(),
        action: String::new(),
        details,
    }
}

/// `errors` with items of exactly the same details merged into one that names each, so a reason shared by many tables is printed once.
pub(crate) fn merged_errors(errors: Vec<PlanItem>) -> Vec<PlanItem> {
    let mut merged: Vec<PlanItem> = Vec::new();
    for item in errors {
        match merged.iter_mut().find(|seen| seen.details == item.details) {
            Some(seen) => seen.name = format!("{}, {}", seen.name, item.name),
            None => merged.push(item),
        }
    }
    merged
}

/// A project problem, `subject: reason`, as an `Errors` item.
pub(crate) fn problem_item(problem: &str) -> PlanItem {
    match problem.split_once(": ") {
        Some((subject, reason)) if !subject.contains(' ') || subject.starts_with("connection ") => {
            error_item(subject, &[reason.to_owned()])
        }
        _ => error_item("project", &[problem.to_owned()]),
    }
}

pub(crate) fn object_item(table: &TablePlan) -> PlanItem {
    let name = sql_adapter(table.spec.warehouse).render_display_name(&table.spec.destination);
    let action = match &table.schema.action {
        TableAction::Recreate => "recreate table (--reload)".to_owned(),
        TableAction::Create if table.spec.python.is_some() => python_create(table),
        TableAction::Create => "create table".to_owned(),
        _ if table.spec.python.is_some() => "alter table at load".to_owned(),
        _ => "alter table".to_owned(),
    };
    let mut details: Vec<PlanDetail> = Vec::new();
    match &table.schema.action {
        TableAction::Create | TableAction::Recreate
            if table.spec.python.is_none()
                || table.schema.write_columns.iter().any(|column| {
                    !matches!(
                        column.origin,
                        ColumnOrigin::Metadata | ColumnOrigin::Constant
                    )
                }) =>
        {
            details.extend(column_details(table));
        }
        TableAction::Alter(changes) => {
            details.extend(
                changes
                    .iter()
                    .map(|change| detail("", format!("{change}{}", change_note(table, change)))),
            );
        }
        _ => {}
    }
    details.extend(
        table
            .spec
            .excluded_columns
            .iter()
            .map(|(column, rule)| detail("", format!("excluded  {column} ({rule})"))),
    );
    details.extend(
        table
            .spec
            .masked
            .columns
            .iter()
            .map(|(column, method)| detail("", format!("masked  {column} ({method})"))),
    );
    PlanItem {
        name,
        action,
        details,
    }
}

/// What the type an added or widened column ends up with loses, as `  (...)`, or nothing, as a new table's column lines note it.
fn change_note(table: &TablePlan, change: &ColumnChange) -> String {
    let column_type = match change {
        ColumnChange::Add { column, .. } => &column.column_type,
        ColumnChange::Widen { to, .. } | ColumnChange::Rebuild { to, .. } => to,
    };
    let rendered = sql_adapter(table.spec.warehouse).render_loader_logical_type(column_type);
    let losses: Vec<&str> = rendered.lossy.as_deref().into_iter().collect();
    match losses.is_empty() {
        true => String::new(),
        false => format!("  ({})", losses.join("; ")),
    }
}

/// A new table's columns: name, portable type and the warehouse's type, then the `_sqlload_*` columns on one line.
fn column_details(table: &TablePlan) -> Vec<PlanDetail> {
    let adapter = sql_adapter(table.spec.warehouse);
    let (metadata, data): (Vec<_>, Vec<_>) = table
        .schema
        .write_columns
        .iter()
        .partition(|column| is_load_column(table.spec.naming, &column.name));
    let name_width = data
        .iter()
        .map(|column| column.name.len())
        .max()
        .unwrap_or(0)
        + 1;
    let type_width = data
        .iter()
        .map(|column| column.column_type.to_string().len())
        .max()
        .unwrap_or(0)
        + 3;
    let from_path = |name: &str| {
        let name = table.spec.source_table.spelling(name);
        table.spec.files.as_ref().map_or("", |files| {
            match (
                files.constants.iter().any(|constant| constant == name),
                files
                    .partition_types
                    .iter()
                    .any(|(partition, _)| partition == name),
            ) {
                (true, _) => CONSTANT_NOTE,
                (false, true) => "  (from the path)",
                (false, false) => "",
            }
        })
    };
    let mut details: Vec<PlanDetail> = data
        .iter()
        .map(|column| {
            let rendered = adapter.render_loader_logical_type(&column.column_type);
            let losses: Vec<&str> = rendered.lossy.as_deref().into_iter().collect();
            let note = match (from_path(&column.name), losses.is_empty()) {
                _ if column.origin == ColumnOrigin::Constant => CONSTANT_NOTE.to_owned(),
                (path, _) if !path.is_empty() => path.to_owned(),
                (_, false) => format!("  ({})", losses.join("; ")),
                (_, true) => String::new(),
            };
            detail(
                "",
                format!(
                    "{:<name_width$}{:<type_width$}-> {}{note}",
                    column.name,
                    column.column_type.to_string(),
                    rendered.sql
                ),
            )
        })
        .collect();
    let names: Vec<String> = metadata
        .iter()
        .enumerate()
        .map(|(index, column)| match index {
            0 => column.name.clone(),
            _ => column
                .name
                .strip_prefix("_sqlload")
                .unwrap_or(&column.name)
                .to_owned(),
        })
        .collect();
    if !names.is_empty() {
        details.push(detail("", format!("+ {}", names.join(", "))));
    }
    details
}

/// The `_sqlload_runs` tables `apply` creates, one per schema, where the commits of tables with a `transform` record its version.
fn run_log_items(tables: &[&TablePlan]) -> Vec<PlanItem> {
    let mut items: Vec<PlanItem> = Vec::new();
    for table in tables {
        let spec = &table.spec;
        let name = display_name(spec.warehouse, &runs_table(&spec.destination));
        if spec
            .transform
            .as_ref()
            .is_some_and(|transform| transform.creates_log)
            && !items.iter().any(|item| item.name == name)
        {
            items.push(PlanItem {
                name,
                action: "create table (run log)".to_owned(),
                details: Vec::new(),
            });
        }
    }
    items
}

pub(crate) fn stage_item(stage: &PlannedStage) -> PlanItem {
    PlanItem {
        name: stage.name.clone(),
        action: format!("create stage on {}", stage.url),
        details: Vec::new(),
    }
}

/// The plan line for loading a table. A captured table whose destination does not exist yet is shown as copied first, as on its source's first load or when it joins a source whose slot already streams.
pub(crate) fn load_item(table: &TablePlan) -> PlanItem {
    let spec = &table.spec;
    let action = match &table.mode {
        WriteMode::Merge {
            unique_key, dedupe, ..
        } => {
            let repeats = match dedupe {
                None => "; repeated keys fail".to_owned(),
                Some(MergeDedupe {
                    sort: Some((column, true)),
                    ..
                }) => format!("; repeated keys: highest {column} wins"),
                Some(MergeDedupe {
                    sort: Some((column, false)),
                    ..
                }) => format!("; repeated keys: lowest {column} wins"),
                Some(_) => String::new(),
            };
            let origin = match spec.key_from_primary_key {
                true => format!(" ← {PRIMARY_KEY_PLACEHOLDER}"),
                false => String::new(),
            };
            let nulls = match table
                .mode
                .matched_keys()
                .iter()
                .any(|(_, nullable)| *nullable)
            {
                true => NULL_KEYS_NOTE,
                false => "",
            };
            let rebuilt = match table.schema.action {
                TableAction::Recreate => " into a new table, swapped in at the end (--reload)",
                _ => "",
            };
            format!(
                "merge (unique_key: {}{origin}{repeats}{nulls}){rebuilt}",
                unique_key.join(", ")
            )
        }
        WriteMode::DeleteInsert {
            cursor_column,
            replace_key,
            ..
        } => {
            let nulls = match table
                .mode
                .matched_keys()
                .iter()
                .any(|(_, nullable)| *nullable)
            {
                true => NULL_KEYS_NOTE,
                false => "",
            };
            let parts: Vec<String> = [
                cursor_column
                    .as_ref()
                    .map(|column| format!("cursor: {column}")),
                (!replace_key.is_empty())
                    .then(|| format!("replace_key: {}{nulls}", replace_key.join(", "))),
            ]
            .into_iter()
            .flatten()
            .collect();
            format!("delete_insert ({})", parts.join(", "))
        }
        WriteMode::Append => match &spec.cursor {
            Some(cursor) => format!("append (cursor: {})", cursor.label()),
            None => "append".to_owned(),
        },
        WriteMode::Replace => "replace".to_owned(),
        WriteMode::Recreate => "recreate from the whole source (--reload)".to_owned(),
        WriteMode::Changes(change) => {
            let changes = match change.apply {
                ChangeApply::SoftDeletes => format!(
                    "apply changes, soft deletes (key: {})",
                    change.unique_key.join(", ")
                ),
                ChangeApply::HardDeletes => format!(
                    "apply changes, hard deletes (key: {})",
                    change.unique_key.join(", ")
                ),
                ChangeApply::Changelog => "append changes (changelog)".to_owned(),
            };
            match table.exists {
                true => changes,
                false => format!("snapshot copy, then {changes}"),
            }
        }
    };
    let source = match (&spec.files, &spec.python) {
        (Some(files), _) => match &files.archive {
            Some(archive) => format!(
                "files  {} and {archive}  {}",
                files.location, files.encoding
            ),
            None => format!("files  {}  {}", files.location, files.encoding),
        },
        (None, Some(python)) => format!("python  {} ({})", python.module, spec.source_name),
        (None, None) if spec.warehouse_source.is_some() => {
            let read = spec
                .warehouse_source
                .as_ref()
                .map_or("", |source| source.system);
            let what = match spec
                .warehouse_source
                .as_ref()
                .and_then(|source| source.sql.as_ref())
            {
                Some(_) => format!("query {}", spec.source_table.name),
                None => spec.source_table.qualified_name(),
            };
            format!("{read}  {}  {what}", spec.source_name)
        }
        (None, None) if spec.sheet.is_some() => {
            let range = spec
                .sheet
                .as_ref()
                .and_then(|sheet| sheet.range.clone())
                .unwrap_or_else(|| format!("tab {}", spec.source_table.name));
            format!(
                "google_sheets  {}  {range}  {} rows",
                spec.source_name, spec.source_table.estimated_rows
            )
        }
        (None, None) => match &spec.cdc {
            Some(cdc) => format!(
                "postgres_cdc  {}  {}  slot {}",
                spec.source_name,
                spec.source_table.qualified_name(),
                cdc.slot
            ),
            None => format!(
                "postgres  {}  {}",
                spec.source_name,
                spec.source_table.qualified_name()
            ),
        },
    };
    let source = match (&spec.tunnel, &spec.transform) {
        (Some(route), _) => format!("{source}  via {route}"),
        (None, Some(transform)) => format!("{source}  transform {}", transform.function),
        (None, None) => source,
    };
    let mut details = vec![detail("source", source)];
    details.extend(
        spec.python
            .as_ref()
            .map(|python| detail("schema", python_columns_line(table, python))),
    );
    let unnested: Option<Vec<String>> = match &spec.unnest {
        Unnest::Paths(paths) => Some(paths.clone()),
        Unnest::All => Some(
            list_choices(spec)
                .into_iter()
                .filter_map(|(field, chosen)| chosen.then_some(field))
                .collect(),
        ),
        Unnest::None => None,
    };
    if let Some(paths) = unnested {
        let tables: Vec<String> = paths
            .iter()
            .map(|path| unnested_table(spec, path))
            .collect();
        let text = match (&spec.unnest, tables.is_empty()) {
            (Unnest::All, true) => format!(
                "every list field, {} levels down at most, into child tables as lists appear; columns inferred at load",
                spec.max_depth
            ),
            (Unnest::All, false) => format!(
                "every list field, {} levels down at most: {} and more as lists appear; columns inferred at load",
                spec.max_depth,
                tables.join(", ")
            ),
            _ => format!(
                "{}  child tables, columns inferred at load",
                tables.join(", ")
            ),
        };
        details.push(detail("unnest", text));
    }
    details.extend(
        spec.files
            .as_ref()
            .zip(table.files.as_ref())
            .map(|(source, read)| {
                let skipped = match source.table.skipped {
                    0 => String::new(),
                    count => format!(
                        ", {count} skipped (names that do not fit {})",
                        source.encoding
                    ),
                };
                let unmatched = match source.table.unmatched {
                    0 => String::new(),
                    count => format!(", {count} skipped (paths that do not match path_columns)"),
                };
                let fresh = match (&table.mode, &table.schema.action) {
                    (WriteMode::Recreate | WriteMode::Replace, _) | (_, TableAction::Recreate) => {
                        "to read"
                    }
                    _ => "new",
                };
                let folders = match &source.archive {
                    Some(archive) => {
                        let archived = read.files.len() - unarchived(table, &read.files).len();
                        format!(
                            ": {} in {}, {} in {archive}",
                            thousands((read.files.len() - archived) as u64),
                            source.location,
                            thousands(archived as u64)
                        )
                    }
                    None => String::new(),
                };
                detail(
                    "files",
                    format!(
                        "{} {fresh} of {} listed{folders}{skipped}{unmatched}",
                        thousands(read.files.len() as u64),
                        thousands(source.table.files.len() as u64)
                    ),
                )
            }),
    );
    details.extend(
        spec.files
            .iter()
            .flat_map(|source| source.notes.iter())
            .map(|note| detail("note", note.as_str())),
    );
    details.extend(table.output.as_ref().map(files_line));
    details.extend(
        table
            .output
            .iter()
            .filter(|output| output.footers_read > 0)
            .map(|output| {
                detail(
                    "footers",
                    format!(
                        "{} read from the newest partition ({} files listed, max_footer_reads {})",
                        output.footers_read, output.listed, output.max_footer_reads
                    ),
                )
            }),
    );
    if table.output.is_some() {
        details.extend(table.schema.action.summary().into_iter().map(|change| {
            detail(
                "schema",
                match change.as_str() {
                    "create" => {
                        format!("first files, {} columns", table.schema.write_columns.len())
                    }
                    "recreate" => {
                        "every file rewritten with the current columns (--reload)".to_owned()
                    }
                    "unchanged" => "as the newest files".to_owned(),
                    _ => format!("new files {change}"),
                },
            )
        }));
    }
    if reload_moves_columns(table) {
        details.push(detail(
            "schema",
            "changed columns move to the end of the column order",
        ));
    }
    let why_shown =
        (table.stage.is_some() && spec.files.is_some() && !is_direct(table.route.as_ref()))
            || spec.warehouse_source.is_some();
    let archived = archived_reason(spec, table.files.as_ref()).filter(|_| !why_shown);
    details.extend(table.route.as_ref().map(|route| {
        let text = route_text(table, route);
        detail(
            "route",
            archived
                .as_ref()
                .map_or_else(|| text.clone(), |why| format!("{text} ({why})")),
        )
    }));
    if let Some(WarehouseSource {
        route: WarehouseRoute::Unload { stage, .. },
        ..
    }) = &spec.warehouse_source
    {
        details.push(detail("stage", stage.as_str()));
    }
    if let Some(reason) = table
        .route
        .as_ref()
        .filter(|_| why_shown)
        .map(|route| route.reason.clone())
    {
        details.push(detail("why", reason));
    }
    if table.route.as_ref().is_some_and(|route| {
        is_direct(Some(route))
            && spec
                .masked
                .columns
                .values()
                .any(|method| *method == MaskMethod::Hash)
    }) {
        details.push(detail(
            "note",
            "hashed columns reach Snowflake in plain text in a temporary table, gone with its session; mask = \"hmac\" keeps them out, through SQLLoad (much slower)",
        ));
    }
    details.push(detail("batch", batch_line(table)));
    details.extend(
        spec.python
            .iter()
            .filter_map(|python| python.literal_paths.as_ref()?.checked_at_load())
            .map(|text| detail("paths", text)),
    );
    if let Some(fetch) = &table.fetch_memory {
        details.push(detail("fetch", fetch.memory.as_str()));
        details.push(detail("buffer", fetch.budget_note.as_str()));
    }
    if matches!(table.commits, CommitRule::Windows(_))
        && spec.python.is_none()
        && spec.files.is_none()
        && spec.warehouse_source.is_none()
    {
        details.push(detail(
            "note",
            "windows are read in one REPEATABLE READ snapshot, held open on the source for the whole table load (mind idle_in_transaction_session_timeout and vacuum)",
        ));
    }
    if source_readers(table) > 1 {
        details.push(detail(
            "note",
            format!("reads over {PARALLEL_READERS} connections, in one snapshot"),
        ));
    }
    details.extend(table.cursor_note.iter().map(|note| {
        let text = match null_cursor_rule(table) {
            Some(rule) => format!("{note} ({rule})"),
            None => note.clone(),
        };
        detail("cursor", text)
    }));
    details.extend(table.logged.as_ref().map(|logged| {
        detail(
            "state",
            format!("logged: {}", describe_state(logged.current.as_ref())),
        )
    }));
    details.extend(
        spec.transform
            .iter()
            .filter(|transform| transform.older > 0)
            .map(|transform| {
                let unit = match spec.files.is_some() {
                    true => ("file", "files"),
                    false => ("range", "ranges"),
                };
                let older = counted(transform.older, unit);
                let every = unit.0;
                detail("", format!("transform  changed: {older} loaded under an earlier version (--reload transforms every {every} again)"))
            }),
    );
    details.extend(
        table
            .output
            .iter()
            .flat_map(|output| output.pending.iter())
            .map(pending_line),
    );
    details.extend(
        table
            .output
            .iter()
            .flat_map(|output| output.notes.iter())
            .map(|note| detail("note", note.as_str())),
    );
    PlanItem {
        name: spec.destination.table.clone(),
        action,
        details,
    }
}

/// How the load commits: by cursor window, in cursor order, by size, by files batch, or as one commit; with a hint to set `batch_size` for a large first load that would be one commit.
pub(crate) fn batch_line(table: &TablePlan) -> String {
    let spec = &table.spec;
    let at_end = matches!(table.mode, WriteMode::Replace | WriteMode::Recreate);
    let described = match (&table.commits, at_end) {
        (CommitRule::Windows(windows), _) => {
            let step = match spec.batch {
                Some(BatchStep::Duration(step)) => humantime::format_duration(step).to_string(),
                Some(BatchStep::Integer(step)) => step.to_string(),
                Some(BatchStep::Whole) | None => String::new(),
            };
            let end = match &spec.source_end {
                Some(SourceEnd::Maximum { maximum, .. }) => {
                    format!(", up to the source's maximum {maximum} (read at plan)")
                }
                Some(SourceEnd::Empty) => ", none: the source has no cursor values".to_owned(),
                None => String::new(),
            };
            let windows = match windows.count {
                1 => "1 window".to_owned(),
                count => format!("{count} windows"),
            };
            let commits = match (at_end, publishes_once(table)) {
                (true, _) => "read one after another into a new table, swapped in once at the end",
                (false, true) => "staged one after another and published in one commit",
                (false, false) => "one commit each",
            };
            format!("{windows} of {step}, {commits}{end}")
        }
        (CommitRule::FileGroups(groups), _) if records_files(table) => {
            format!(
                "{}, one commit each, oldest modified first (ties by path), each recorded in the run log",
                match groups.len() {
                    1 => "1 file".to_owned(),
                    count => format!("{count} files"),
                }
            )
        }
        (CommitRule::FileGroups(groups), _) => {
            format!(
                "{}, up to {} files or about 1 GB per commit",
                match groups.len() {
                    1 => "1 batch".to_owned(),
                    count => format!("{count} batches"),
                },
                thousands(MAX_BATCH_FILES as u64)
            )
        }
        (_, true) if pulls_unloaded(table.route.as_ref()) => {
            "each unload loaded at once into a new table, swapped in once at the end".to_owned()
        }
        (_, true) if sql_adapter(spec.warehouse).keeps_tables() => {
            "about 1 GB at a time into a staging table, then the table emptied and refilled in one transaction".to_owned()
        }
        (_, true) if sends_rows(table.stage.as_ref()) => {
            "about 1 GB at a time into a staging table, copied and swapped in once at the end"
                .to_owned()
        }
        (_, true) => "about 1 GB at a time into a new table, swapped in once at the end".to_owned(),
        (CommitRule::Ordered { column, .. }, false) if publishes_once(table) => format!(
            "in {column} order (an index leads with it, NULLs first), staged about 1 GB at a time and published in one commit replacing the whole range"
        ),
        (CommitRule::Ordered { column, .. }, false) => format!(
            "about every 1 GB, in {column} order (an index leads with it, NULLs first), between distinct values{}",
            match table.mode {
                WriteMode::DeleteInsert { .. } => "; each commit replaces its own cursor range",
                _ => "",
            }
        ),
        (CommitRule::Size, false) if pulls_unloaded(table.route.as_ref()) => {
            "one commit: the destination loads the whole unload at once".to_owned()
        }
        (CommitRule::Size, false) => "about every 1 GB".to_owned(),
        (CommitRule::Whole(reason), false) => {
            let estimated = u64::try_from(spec.source_table.estimated_bytes).unwrap_or_default();
            let large = !table.exists && (spec.python.is_some() || estimated > BATCH_BYTES as u64);
            let why = match reason {
                WholeReason::Chosen => "batch_size = \"none\"",
                WholeReason::SingleFile => "single_file writes each file once, whole",
                WholeReason::TextCursor => {
                    "a text cursor, which the source and destination may sort differently"
                }
                WholeReason::AppendWithoutCursor => {
                    "append without a cursor: a rerun after a partial commit would duplicate it"
                }
                WholeReason::Unordered | WholeReason::Blocked => {
                    "committing part of an unordered cursor read could skip rows"
                }
                WholeReason::ReplaceKey => {
                    "delete_insert by replace_key: each commit replaces every row of its key values, so a value's rows commit together"
                }
            };
            let once = matches!(table.mode, WriteMode::DeleteInsert { .. })
                && sql_adapter(spec.warehouse).publishes_ranges_once();
            let hint = match (large, reason, once) {
                (true, WholeReason::Unordered, false) => {
                    "; for a large first load, set batch_size (such as batch_size = \"1d\") to commit it window by window"
                }
                (true, WholeReason::Unordered, true) => {
                    "; for a large first load, set batch_size (such as batch_size = \"1d\") to read it window by window, or split it into runs with --end-cursor"
                }
                _ => "",
            };
            format!("one commit for the whole read ({why}){hint}")
        }
    };
    let logged = match (&table.logged, publishes_once(table)) {
        (Some(_), false) => ", and only when ctx.state has changed",
        _ => "",
    };
    format!("{described}{logged}")
}

fn pending_line(load: &PendingLoad) -> PlanDetail {
    let (stopped, repair) = match load.phase {
        PendingPhase::Writing => ("while writing", "deletes its"),
        PendingPhase::Replacing => ("while deleting the files it replaced", "finishes deleting"),
    };
    detail(
        "cleanup",
        format!(
            "load {} stopped {stopped}; load {repair} {} files first",
            load.load_id,
            load.deletes.len()
        ),
    )
}

fn files_line(output: &FilesOutput) -> PlanDetail {
    let partition = match &output.path.partition {
        Some(partition) => format!("  partitions {partition}"),
        None => String::new(),
    };
    let encoding = &output.encoding;
    let mut format = match (encoding.format, encoding.compression) {
        (FilesFormat::Parquet, _) => "parquet (zstd)".to_owned(),
        (FilesFormat::Csv, OutputCompression::None) => "csv".to_owned(),
        (FilesFormat::Csv, OutputCompression::Gzip) => "csv (gzip)".to_owned(),
        (FilesFormat::Csv, OutputCompression::Zstd) => "csv (zstd)".to_owned(),
    };
    if encoding.single_file {
        format.push_str(", single file");
    }
    detail(
        "files",
        format!("{}  {format}{partition}", output.path.folder),
    )
}

/// Whether a Snowflake `--reload` re-adds any column, as its build table starts `LIKE` the table to keep the grants.
fn reload_moves_columns(table: &TablePlan) -> bool {
    let warehouse = table.spec.warehouse;
    let Some(live) = &table.live else {
        return false;
    };
    if table.mode != WriteMode::Recreate || !sql_adapter(warehouse).builds_like_target() {
        return false;
    }
    let sql = |column_type: &ColumnType| {
        sql_adapter(warehouse)
            .render_loader_logical_type(column_type)
            .sql
    };
    for column in &table.schema.write_columns {
        let name = sql_adapter(warehouse).stored_name(&column.name);
        let known = live.iter().find(|known| known.name == name);
        if known.is_none_or(|known| sql(&known.column_type) != sql(&column.column_type)) {
            return true;
        }
    }
    false
}

/// How a table's data moves, in short: the warehouse's own read of the files where it reads them, otherwise each step.
fn route_text(table: &TablePlan, route: &Route) -> String {
    let warehouse = table.spec.warehouse;
    let (verb, load) = sql_adapter(warehouse).render_read_words();
    let real = table
        .spec
        .files
        .as_ref()
        .map(|files| files.encoding.format.to_string());
    let over_sftp = table
        .spec
        .files
        .as_ref()
        .is_some_and(|files| matches!(files.location.store, StoreKind::Sftp { .. }));
    match route.steps.as_slice() {
        [Step::UseFiles(files), Step::DestinationPulls(_)] => {
            format!(
                "{warehouse} {verb} directly from {} (nothing through the loader)",
                files.store
            )
        }
        [
            Step::UseFiles(files),
            Step::CopyFiles(Store::InternalStage(stage)),
            Step::DestinationPulls(_),
        ] => format!(
            "{} files from {} uploaded unchanged to {stage} -> {load}",
            real.unwrap_or_else(|| files.format.to_string()),
            files.store
        ),
        [Step::Unload(store), Step::DestinationPulls(_)] => {
            format!("unload Parquet to {store} -> {warehouse} {load} (nothing through the loader)")
        }
        [
            Step::Unload(store),
            Step::CopyFiles(Store::Local(folder)),
            Step::DestinationPulls(_),
        ] => format!(
            "unload Parquet to {store} -> download unchanged to {folder} -> {warehouse} {load} (nothing through the loader)"
        ),
        steps => over_sftp
            .then(|| "read over SFTP".to_owned())
            .into_iter()
            .chain(
                steps
                    .iter()
                    .filter(|step| !matches!(step, Step::UseFiles(_)))
                    .map(|step| match step {
                        Step::ReadArrow => "read as Arrow".to_owned(),
                        Step::ParquetWriter(Store::InternalStage(stage)) => {
                            format!("Parquet -> PUT to {stage}")
                        }
                        Step::ParquetWriter(store) => format!("Parquet -> {store}"),
                        Step::DestinationPulls(_) => load.to_owned(),
                        Step::DestinationTakesArrow => format!("{warehouse} takes Arrow"),
                        other => other.to_string(),
                    }),
            )
            .collect::<Vec<_>>()
            .join(" -> "),
    }
}

/// Each destination `tables` load into, as `Snowflake analytics`, in order.
fn destinations(tables: &[&TablePlan]) -> Vec<String> {
    let mut names: Vec<String> = Vec::new();
    for table in tables {
        let name = format!("{} {}", table.spec.warehouse, table.spec.target);
        if !names.contains(&name) {
            names.push(name);
        }
    }
    names
}

/// `item` with its `route` naming `table`'s destination when the run loads into several.
fn routed_item(table: &TablePlan, mut item: PlanItem, several: bool) -> PlanItem {
    if several {
        let name = format!("{} {}", table.spec.warehouse, table.spec.target);
        for detail in item
            .details
            .iter_mut()
            .filter(|detail| detail.label == ROUTE_LABEL)
        {
            detail.text = format!("{name}: {}", detail.text);
        }
    }
    item
}

/// A table that cannot load, as an `Errors` item, with the flag that unblocks it where one does.
pub(crate) fn blocked_item(table: &TablePlan) -> Option<PlanItem> {
    let blocked = table.schema.blocked.as_ref()?;
    let mut item = error_item(&table.spec.destination.table, &blocked.reasons);
    item.details.extend(blocked.fix_flag.iter().map(|flag| {
        detail(
            "fix",
            format!("sqlload load {} {flag}", table.spec.destination.table),
        )
    }));
    Some(item)
}

/// A table's warnings, as one `Warnings` item.
pub(crate) fn warning_item(table: &TablePlan) -> Option<PlanItem> {
    (!table.schema.warnings.is_empty()).then(|| PlanItem {
        name: table.spec.destination.table.clone(),
        action: String::new(),
        details: table
            .schema
            .warnings
            .iter()
            .map(|warning| detail("", warning.as_str()))
            .collect(),
    })
}

/// A Python table's object line before its first load.
fn python_create(table: &TablePlan) -> String {
    let declared = table
        .schema
        .write_columns
        .iter()
        .filter(|column| {
            !matches!(
                column.origin,
                ColumnOrigin::Metadata | ColumnOrigin::Constant
            )
        })
        .count();
    match declared {
        0 => "create table on first load (columns inferred from the data)".to_owned(),
        count => format!(
            "create table on first load ({count} declared columns, the rest inferred from the data)"
        ),
    }
}

/// Where a Python table's columns come from.
fn python_columns_line(table: &TablePlan, python: &PythonTable) -> String {
    let declared = match python.declared.len() {
        0 => String::new(),
        count => format!(", {count} declared"),
    };
    let flattened = match table.spec.flatten.is_empty() {
        true => String::new(),
        false => format!(", {} flattened one level", table.spec.flatten.join(", ")),
    };
    match table.exists {
        true => format!(
            "columns inferred at load and checked against the live table{declared}{flattened}"
        ),
        false => format!("columns inferred on first load{declared}{flattened}"),
    }
}

/// `count` and the noun for it, as `1 object` or `2 objects`.
fn counted(count: usize, (one, many): (&str, &str)) -> String {
    match count {
        1 => format!("1 {one}"),
        _ => format!("{count} {many}"),
    }
}

/// The plan's sections: what `apply` creates or changes, what `load` moves, and what stops or should be known; one `Errors` item per table or problem.
pub(crate) fn plan_view(plan: &ProjectPlan) -> PlanView {
    sections(plan, false)
}

/// The plan as `plan` prints it: as `plan_view`, with tables named as they land and sorted by that name, and each load's details sharpened by `text_item`.
pub(crate) fn text_view(plan: &ProjectPlan) -> PlanView {
    sections(plan, true)
}

/// Why a files table that exists and has no new file to read is skipped, as `up to date: 0 new of 3 files in drop/stock_counts/ (csv in ZIP archives)`; `None` for any other table, for one with a schema change, an unfinished load to repair or a `transform` changed since its last load, and for a `delete_insert` over a `--start-cursor`/`--end-cursor` range (`ranged`), which clears that range even with no file in it.
pub(crate) fn up_to_date(table: &TablePlan, ranged: bool) -> Option<String> {
    let (source, read) = table.spec.files.as_ref().zip(table.files.as_ref())?;
    let incremental =
        table.exists && !matches!(table.mode, WriteMode::Replace | WriteMode::Recreate);
    let settled = table.schema.action == TableAction::Unchanged
        && table
            .output
            .as_ref()
            .is_none_or(|output| output.pending.is_empty());
    let transform_changed = table
        .spec
        .transform
        .as_ref()
        .is_some_and(|transform| transform.older > 0);
    if !settled
        || transform_changed
        || ranged && matches!(table.mode, WriteMode::DeleteInsert { .. })
    {
        return None;
    }
    (incremental && read.files.is_empty()).then(|| {
        let files = format!(
            "{} files in {} ({})",
            thousands(source.table.files.len() as u64),
            source.location,
            source.encoding
        );
        match (ranged, reads_line(table, false)) {
            (true, Some(reads)) => format!("nothing in range: 0 of {files} for {reads}"),
            _ => format!("up to date: 0 new of {files}"),
        }
    })
}

fn sections(plan: &ProjectPlan, text: bool) -> PlanView {
    let ready: Vec<_> = plan
        .tables
        .iter()
        .filter(|table| table.schema.blocked.is_none())
        .collect();
    let mut stages: Vec<&PlannedStage> = Vec::new();
    for table in &ready {
        if let Some(stage) = &table.planned_stage
            && !stages.iter().any(|seen| seen.name == stage.name)
        {
            stages.push(stage);
        }
    }
    let changed: Vec<_> = ready
        .iter()
        .filter(|table| table.schema.action != TableAction::Unchanged && table.output.is_none())
        .collect();
    let logs = run_log_items(&ready);
    let creates = stages.len()
        + logs.len()
        + changed
            .iter()
            .filter(|table| {
                matches!(
                    table.schema.action,
                    TableAction::Create | TableAction::Recreate
                )
            })
            .count();
    let alters = stages.len() + logs.len() + changed.len() - creates;
    let mut errors: Vec<PlanItem> = plan
        .tables
        .iter()
        .filter_map(|table| {
            let item = blocked_item(table)?;
            Some(match (text, &table.schema.blocked) {
                (true, Some(blocked)) => {
                    let name = display_name(table.spec.warehouse, &table.spec.destination);
                    let problem = blocked.reasons.join(" ");
                    let at = table.spec.settings_at.as_deref();
                    text_error(plan, (&problem, at), PlanItem { name, ..item })
                }
                _ => item,
            })
        })
        .collect();
    for (index, problem) in plan.problems.iter().enumerate() {
        errors.push(match text {
            true => {
                let nth = plan.problems[..index]
                    .iter()
                    .filter(|earlier| *earlier == problem)
                    .count();
                let item = problem_item(problem);
                let item = PlanItem {
                    name: nth_about(&plan.problem_names, problem, nth).unwrap_or(item.name),
                    ..item
                };
                let at = nth_about(&plan.problem_places, problem, nth).flatten();
                text_error(plan, (problem, at.as_deref()), item)
            }
            false => problem_item(problem),
        });
    }
    let clauses = [
        (creates, ("object to create", "objects to create")),
        (alters, ("to alter", "to alter")),
        (ready.len(), ("to load", "to load")),
        (plan.skipped.len(), ("skipped", "skipped")),
    ];
    let selected = plan.tables.len();
    let one = plan.tables.len() == 1 && plan.skipped.is_empty() && errors.is_empty();
    let named = match text && !one {
        true => destinations(&ready),
        false => Vec::new(),
    };
    let several = named.len() > 1;
    let mut summary = vec![format!("{selected} selected")];
    summary.extend(
        clauses
            .into_iter()
            .filter(|(count, _)| *count > 0)
            .map(|(count, words)| counted(count, words)),
    );
    PlanView {
        ok: errors.is_empty(),
        retryable: !errors.is_empty() && plan.only_retryable_problems(),
        summary: summary.join(", "),
        selected,
        destinations: named,
        objects: stages
            .iter()
            .map(|stage| stage_item(stage))
            .chain(changed.iter().map(|table| object_item(table)))
            .chain(logs)
            .collect(),
        loads: match text {
            false => ready.iter().map(|table| load_item(table)).collect(),
            true => {
                let items: Vec<PlanItem> = ready.iter().map(|table| load_item(table)).collect();
                let usual = usual_details(&ready, &items);
                sorted(
                    ready
                        .iter()
                        .zip(items)
                        .map(|(table, item)| {
                            let item = routed_item(table, item, several);
                            text_item(table, item, one, &usual)
                        })
                        .collect(),
                )
            }
        },
        skipped: match text {
            false => plan
                .skipped
                .iter()
                .map(|skipped| skipped_item(skipped.source_table.clone(), skipped.reason.clone()))
                .collect(),
            true => sorted(
                plan.skipped
                    .iter()
                    .map(|skipped| {
                        let reason = match skipped.reason.strip_suffix(DRAINED_REASON) {
                            Some(drained) => {
                                drained.replacen("no files to load in", "no new files in", 1)
                            }
                            None => skipped.reason.clone(),
                        };
                        skipped_item(skipped.destination.clone(), reason)
                    })
                    .collect(),
            ),
        },
        errors,
        warnings: plan
            .warnings
            .iter()
            .filter(|warning| !(text && folded_warning(plan, warning)))
            .map(|warning| error_item(warning, &[]))
            .chain(ready.iter().filter_map(|table| warning_item(table)))
            .collect(),
    }
}

/// What the `nth` (from 0) of `pairs` about `problem` holds: one plan problem's, as the `nth` of the plan's problems with that text.
fn nth_about<T: Clone>(pairs: &[(String, T)], problem: &str, nth: usize) -> Option<T> {
    pairs
        .iter()
        .filter(|(about, _)| about == problem)
        .nth(nth)
        .map(|(_, value)| value.clone())
}

fn skipped_item(name: String, reason: String) -> PlanItem {
    PlanItem {
        name,
        action: String::new(),
        details: vec![detail("reason", reason)],
    }
}

/// `items` sorted by name, which groups them by schema.
fn sorted(mut items: Vec<PlanItem>) -> Vec<PlanItem> {
    items.sort_by(|left, right| left.name.cmp(&right.name));
    items
}

/// The lines `plan` prints for `view`: `Plan ready`, each section with items, then whether the plan is complete.
pub(crate) fn view_lines(view: &PlanView) -> Vec<String> {
    let mut lines = match view.selected {
        0 if !view.errors.is_empty() => Vec::new(),
        _ => vec![format!("Plan ready  {}", view.summary)],
    };
    lines.extend(view.destinations.iter().map(|name| format!("to  {name}")));
    if !lines.is_empty() {
        lines.push(String::new());
    }
    lines.extend(tree("Objects", "sqlload apply", &view.objects));
    lines.extend(tree("Loads", "sqlload load", &view.loads));
    lines.extend(tree("Skipped", "", &view.skipped));
    let summary = match lines.is_empty() {
        true => String::new(),
        false => format!("  {}", view.summary),
    };
    lines.extend(tree("Errors", "", &view.errors));
    lines.extend(tree("Warnings", "", &view.warnings));
    lines.push(match view.ok {
        true => format!("✓ Plan complete{summary}"),
        false => format!("✗ Plan failed with errors{summary}"),
    });
    lines
}

/// `created  name  detail`, as `apply` and a Python table's load report a change, as a tally line.
pub(crate) fn change_line(line: &str) -> TallyLine {
    let mut parts = line.splitn(3, "  ");
    let verb = match parts.next().unwrap_or_default() {
        "created" => "created",
        "recreated" => "recreated",
        _ => "altered",
    };
    TallyLine {
        verb,
        name: parts.next().unwrap_or_default().to_owned(),
        detail: parts.next().unwrap_or_default().to_owned(),
    }
}

/// What `run` did after `plan`: the plan, each change it made, each table's outcome, and counts (a table altered by several changes counts once).
pub(crate) fn run_result(plan: &ProjectPlan, run: &RunReport<'_>) -> RunResult {
    let RunReport {
        command,
        applied,
        outcomes,
        started,
        elapsed,
        peak_memory_bytes,
    } = *run;
    let changes = outcomes
        .iter()
        .flat_map(|outcome| outcome.loaded.iter())
        .flat_map(|loaded| &loaded.changes);
    let objects: Vec<ObjectChange> = applied
        .iter()
        .chain(changes)
        .map(|line| {
            let change = change_line(line);
            ObjectChange {
                action: change.verb,
                name: change.name,
                detail: change.detail,
            }
        })
        .collect();
    let tables: Vec<TableResult> = outcomes.iter().map(table_result).collect();
    let actions = |action: &str| objects.iter().filter(|o| o.action == action).count();
    let distinct = |action: &str| {
        objects
            .iter()
            .filter(|object| object.action == action)
            .map(|object| object.name.as_str())
            .collect::<std::collections::BTreeSet<&str>>()
            .len()
    };
    let statuses = |status: &str| tables.iter().filter(|t| t.status == status).count();
    let summary = RunSummary {
        created: actions("created"),
        altered: distinct("altered"),
        recreated: actions("recreated"),
        loaded: statuses("loaded"),
        failed: statuses("failed"),
        skipped: statuses("skipped"),
        rows: tables.iter().filter_map(|table| table.rows).sum(),
        peak_memory_bytes,
    };
    let view = plan_view(plan);
    let finished = started + chrono::Duration::from_std(elapsed).unwrap_or_default();
    let ok = view.ok && summary.failed == 0;
    RunResult {
        schema_version: RESULT_SCHEMA_VERSION,
        command,
        ok,
        retryable: !ok
            && (view.ok || view.retryable)
            && outcomes.iter().all(TableOutcome::passes_on_rerun),
        started_at: started.to_rfc3339_opts(SecondsFormat::Millis, true),
        finished_at: finished.to_rfc3339_opts(SecondsFormat::Millis, true),
        duration_seconds: seconds(elapsed),
        summary,
        plan: view,
        objects,
        tables,
    }
}

/// `outcome` as `--json` reports it.
fn table_result(outcome: &TableOutcome) -> TableResult {
    let (status, rows, note, error) = match &outcome.loaded {
        Ok(loaded) => ("loaded", Some(loaded.rows), loaded.note.clone(), None),
        Err(reason) if outcome.skipped => ("skipped", None, None, Some(reason.clone())),
        Err(reason) => ("failed", None, None, Some(reason.clone())),
    };
    TableResult {
        table: outcome.table.clone(),
        source: outcome.origin.as_ref().map(|origin| origin.source.clone()),
        target: outcome.origin.as_ref().map(|origin| origin.target.clone()),
        write_strategy: outcome
            .origin
            .as_ref()
            .map(|origin| origin.strategy.to_string()),
        status,
        rows,
        duration_seconds: seconds(outcome.elapsed),
        note,
        error,
        retryable: outcome.retryable,
        after_load: match &outcome.loaded {
            Ok(loaded) => loaded.after_load.clone(),
            Err(_) => None,
        },
        replaced_keys: match &outcome.loaded {
            Ok(loaded) => loaded.replaced_keys.clone(),
            Err(_) => None,
        },
        transforms: match &outcome.loaded {
            Ok(loaded) => loaded.transforms.clone(),
            Err(_) => Vec::new(),
        },
        peak_memory_bytes: outcome.peak_memory_bytes,
    }
}

/// `elapsed` in seconds, to the millisecond.
fn seconds(elapsed: Duration) -> f64 {
    (elapsed.as_secs_f64() * 1000.0).round() / 1000.0
}

/// For each of `SHARED_LABELS`, the text most of `items` show, when at least two share it; ties go to the text first in order. An incremental table's `batch`, which `reads` and `commits` replace, isn't counted.
pub(crate) fn usual_details(tables: &[&TablePlan], items: &[PlanItem]) -> HashMap<String, String> {
    let mut counts: Vec<(&str, &str, usize)> = Vec::new();
    for (table, item) in tables.iter().zip(items) {
        for detail in &item.details {
            let label = detail.label.as_str();
            if incremental(table) && REPLACED_LABELS.contains(&label)
                || !SHARED_LABELS.contains(&label)
            {
                continue;
            }
            match counts
                .iter_mut()
                .find(|(label, text, _)| *label == detail.label && *text == detail.text)
            {
                Some((_, _, count)) => *count += 1,
                None => counts.push((&detail.label, &detail.text, 1)),
            }
        }
    }
    let mut usual: HashMap<String, (String, usize)> = HashMap::new();
    for (label, text, count) in counts {
        let best = usual.get(label).map_or(1, |(_, best)| *best);
        if count > best {
            usual.insert(label.to_owned(), (text.to_owned(), count));
        }
    }
    usual
        .into_iter()
        .map(|(label, (text, _))| (label, text))
        .collect()
}

/// Whether `table` reads from a cursor or a logged state, so `reads` and `commits` describe it.
fn incremental(table: &TablePlan) -> bool {
    table.window.is_some() || table.logged.is_some()
}

/// `item`, `load_item`'s view of `table`, as `plan` prints it: named as it lands, `route` (and `why`) directly under `source`, and, for an incremental table, `reads` and `commits` in place of `cursor`, `state` and `batch`. With `one` (one table selected) every detail stays, with times to the millisecond; otherwise a detail with its `usual` text is left out, and `commits` only shows when it says more than the usual.
pub(crate) fn text_item(
    table: &TablePlan,
    item: PlanItem,
    one: bool,
    usual: &HashMap<String, String>,
) -> PlanItem {
    let incremental = incremental(table);
    let mut details: Vec<PlanDetail> = item
        .details
        .into_iter()
        .filter(|detail| match detail.label.as_str() {
            label if incremental && REPLACED_LABELS.contains(&label) => false,
            label if !one && ONE_TABLE_LABELS.contains(&label) => false,
            label => {
                one || (usual.get(label) != Some(&detail.text)
                    && !DEFAULT_DETAILS.contains(&(label, detail.text.as_str())))
            }
        })
        .collect();
    let routed_at = details
        .iter()
        .position(|detail| detail.label == ROUTE_LABEL)
        .unwrap_or(details.len());
    if let Some(text) = incremental.then(|| commits_line(table, one)).flatten() {
        details.insert(routed_at, detail("commits", text));
    }
    let (mut added, mut details): (Vec<PlanDetail>, Vec<PlanDetail>) = details
        .into_iter()
        .partition(|detail| matches!(detail.label.as_str(), "route" | "why"));
    let after_source = details
        .iter()
        .position(|detail| detail.label == SOURCE_LABEL)
        .map_or(0, |index| index + 1);
    if one {
        added.extend(key_line(table).map(|text| detail("key", text)));
    }
    if incremental {
        added.extend(reads_line(table, one).map(|text| detail("reads", text)));
    }
    if one {
        added.extend(columns_line(table).map(|text| detail("columns", text)));
        added.extend(checks_line(table).map(|text| detail("checks", text)));
    }
    details.splice(after_source..after_source, added);
    PlanItem {
        name: display_name(table.spec.warehouse, &table.spec.destination),
        action: item.action,
        details,
    }
}

/// How many columns the table loads from its source, and under dlt's naming the first names it changes: `6, named by naming = "dlt": company-no -> company_no, item-no -> item_no, ...`; `None` when they are only known at load.
fn columns_line(table: &TablePlan) -> Option<String> {
    let count = table
        .schema
        .write_columns
        .iter()
        .filter(|column| column.origin != ColumnOrigin::Metadata)
        .count();
    if count == 0 {
        return None;
    }
    let renamed: Vec<String> = table
        .spec
        .source_table
        .columns
        .iter()
        .filter(|_| table.spec.naming == Naming::Dlt)
        .filter_map(|column| {
            let spelled = column.source_spelling.as_deref()?;
            (spelled != column.name).then(|| format!("{spelled} -> {}", column.name))
        })
        .collect();
    Some(match renamed.as_slice() {
        [] => thousands(count as u64),
        shown => format!(
            "{}, named by naming = \"dlt\": {}{}",
            thousands(count as u64),
            shown
                .iter()
                .take(SHOWN_RENAMES)
                .cloned()
                .collect::<Vec<_>>()
                .join(", "),
            match shown.len() > SHOWN_RENAMES {
                true => ", ...",
                false => "",
            }
        ),
    })
}

/// The checks its staged rows must pass, as `not_null company_no, item_no; min_rows 1,000 (warns)`.
fn checks_line(table: &TablePlan) -> Option<String> {
    let checks = table.spec.checks.as_ref()?;
    let warns = |name: CheckName| match checks.warn.contains(&name) {
        true => " (warns)",
        false => "",
    };
    let parts: Vec<String> = [
        (!checks.not_null.is_empty()).then(|| {
            format!(
                "not_null {}{}",
                checks.not_null.join(", "),
                warns(CheckName::NotNull)
            )
        }),
        checks
            .unique
            .as_ref()
            .map(|columns| format!("unique {}{}", columns.join(", "), warns(CheckName::Unique))),
        checks
            .min_rows
            .map(|rows| format!("min_rows {}{}", thousands(rows), warns(CheckName::MinRows))),
        checks
            .max_row_drop
            .map(|percent| format!("max_row_drop {percent}%{}", warns(CheckName::MaxRowDrop))),
    ]
    .into_iter()
    .flatten()
    .collect();
    (!parts.is_empty()).then(|| parts.join("; "))
}

/// Where a merge's key comes from: `unique_key`, or the primary key the source reports.
fn key_line(table: &TablePlan) -> Option<String> {
    let WriteMode::Merge { unique_key, .. } = &table.mode else {
        return None;
    };
    Some(match table.spec.key_from_primary_key {
        true => format!(
            "{}, the primary key the source reports (unique_key = \"{PRIMARY_KEY_PLACEHOLDER}\"{})",
            unique_key.join(", "),
            table
                .spec
                .unique_key_at
                .as_deref()
                .map(|at| format!(", {at}"))
                .unwrap_or_default()
        ),
        false => {
            let at = table
                .spec
                .unique_key_at
                .as_deref()
                .map(|at| format!(" ({at})"))
                .unwrap_or_default();
            let primary_key = &table.spec.source_table.primary_key;
            let agrees = !primary_key.is_empty()
                && primary_key
                    .iter()
                    .collect::<std::collections::BTreeSet<_>>()
                    == unique_key.iter().collect();
            match agrees {
                true => format!("from unique_key{at}; the source reports the same primary key"),
                false => format!("from unique_key{at}"),
            }
        }
    })
}

/// Where the read starts: `updated_at >= 2026-11-14 07:49:41 (warehouse maximum minus lookback 5s)`, or the logged state; times to the second, or to the millisecond when `precise`; a cursor read followed by what it does with rows whose cursor is NULL, when [`null_cursor_rule`] says.
pub(crate) fn reads_line(table: &TablePlan, precise: bool) -> Option<String> {
    let line = reads_text(table, precise)?;
    Some(
        match null_cursor_rule(table).filter(|_| table.logged.is_none()) {
            Some(rule) => format!("{line} ({rule})"),
            None => line,
        },
    )
}

/// What a cursor read does with source rows whose cursor is NULL, for `plan`: `NULL cursors: included` or `skipped` as `null_cursor` says, and by default `fail the load` when the source reports a cursor column nullable; `None` when it reports one NOT NULL or reports nothing (a Python table), and for a files source, whose cursor comes from each file's path.
pub(crate) fn null_cursor_rule(table: &TablePlan) -> Option<&'static str> {
    let spec = &table.spec;
    let cursor = spec.cursor.as_ref().filter(|_| spec.files.is_none())?;
    let nullable = cursor.columns().all(|name| {
        spec.source_table
            .column(name)
            .is_some_and(|column| column.nullable)
    });
    match cursor.null_cursor {
        NullCursor::Include => Some("NULL cursors: included"),
        NullCursor::Ignore => Some("NULL cursors: skipped"),
        NullCursor::Fail if nullable => Some("NULL cursors: fail the load"),
        NullCursor::Fail => None,
    }
}

/// [`reads_line`] without the NULL cursor rule.
fn reads_text(table: &TablePlan, precise: bool) -> Option<String> {
    if let Some(logged) = &table.logged {
        return Some(match &logged.current {
            Some(record) => {
                let cursor = record
                    .cursor_column
                    .as_ref()
                    .map(|column| {
                        let at = record.cursor_after.as_deref().unwrap_or("not set");
                        format!("{column} {}, ", trimmed_time(at, precise))
                    })
                    .unwrap_or_default();
                let origin = match record.event {
                    RunEvent::Reset => "state reset",
                    RunEvent::Reconcile => "reconciled",
                    RunEvent::Run | RunEvent::Backfill => "run",
                };
                format!(
                    "logged {cursor}state {} ({origin} {}, {})",
                    record.state.as_deref().unwrap_or("not set"),
                    record.run_id.chars().take(8).collect::<String>(),
                    clock_time(&record.recorded_at)
                )
            }
            None => "everything: no run is logged yet".to_owned(),
        });
    }
    let window = table.window.as_ref()?;
    let note = table.cursor_note.as_deref().unwrap_or_default();
    let said = note
        .strip_prefix(window.column.as_str())
        .unwrap_or(note)
        .trim();
    let Some(from) = &window.from else {
        return Some(format!("{}: {said}", window.column));
    };
    let (bound, after_bound) = match (said.strip_prefix("from "), said.strip_prefix("after ")) {
        (Some(rest), _) => (">=", rest.strip_prefix(from.as_str())),
        (None, Some(rest)) => (">", rest.strip_prefix(from.as_str())),
        (None, None) => (
            match window.inclusive {
                true => ">=",
                false => ">",
            },
            None,
        ),
    };
    let origin = match after_bound {
        Some(rest) => match rest
            .trim()
            .strip_prefix('(')
            .and_then(|o| o.strip_suffix(')'))
        {
            Some(origin) if !origin.contains('(') => {
                origin.replace("destination maximum", "warehouse maximum")
            }
            _ if rest.trim().is_empty() => String::new(),
            _ => return Some(note.to_owned()),
        },
        None => match said.strip_suffix(from.as_str()) {
            Some(before) if !before.contains('(') => before.trim_end().to_owned(),
            _ => return Some(note.to_owned()),
        },
    };
    let origin = match origin.is_empty() {
        true => String::new(),
        false => format!(" ({origin})"),
    };
    Some(format!(
        "{} {bound} {}{origin}",
        window.column,
        trimmed_time(from, precise)
    ))
}

/// How the read commits: once at its end, or after each window; with `precise`, any other rule too, as `batch` says it.
fn commits_line(table: &TablePlan, precise: bool) -> Option<String> {
    match &table.commits {
        CommitRule::Whole(WholeReason::Unordered | WholeReason::Blocked) => Some(format!(
            "once, at the end of the read ({} isn't read in order)",
            table.window.as_ref()?.column
        )),
        CommitRule::Whole(_) => Some(batch_line(table).replacen(
            "one commit for the whole read",
            "once, at the end of the read",
            1,
        )),
        CommitRule::Windows(_) if precise => Some(batch_line(table)),
        CommitRule::Windows(_) => Some(match table.spec.batch {
            Some(BatchStep::Duration(step)) => format!("after each {} window", window_length(step)),
            Some(BatchStep::Integer(step)) => format!("after each window of {step}"),
            Some(BatchStep::Whole) | None => batch_line(table),
        }),
        CommitRule::Ordered { .. } | CommitRule::Size | CommitRule::FileGroups(_) => {
            precise.then(|| batch_line(table))
        }
    }
}

/// A window's length as people say it: `30-day`, `6-hour`, or as written when it isn't whole days, hours or minutes.
fn window_length(step: std::time::Duration) -> String {
    let seconds = step.as_secs();
    let units = [(86_400, "day"), (3_600, "hour"), (60, "minute")];
    units
        .iter()
        .find(|(size, _)| seconds.is_multiple_of(*size) && seconds >= *size)
        .map_or_else(
            || humantime::format_duration(step).to_string(),
            |(size, unit)| format!("{}-{unit}", seconds / size),
        )
}

/// `value` with a timestamp's fraction of a second cut to the second, or to the millisecond when `precise`, and an offset of whole hours shortened (`+00:00` to `+00`); anything else as it is.
fn trimmed_time(value: &str, precise: bool) -> String {
    let bytes = value.as_bytes();
    let is_timestamp = bytes.len() >= SECOND_TIMESTAMP_LENGTH
        && bytes[4] == b'-'
        && bytes[7] == b'-'
        && matches!(bytes[10], b' ' | b'T')
        && bytes[13] == b':'
        && bytes[16] == b':'
        && value.is_char_boundary(SECOND_TIMESTAMP_LENGTH);
    if !is_timestamp {
        return value.to_owned();
    }
    let (seconds, rest) = value.split_at(SECOND_TIMESTAMP_LENGTH);
    let digits = match rest.strip_prefix('.') {
        Some(fraction) => fraction.bytes().take_while(u8::is_ascii_digit).count(),
        None => 0,
    };
    let (fraction, offset) = match digits {
        0 => ("", rest),
        _ => rest.split_at(digits + 1),
    };
    let fraction = match precise {
        true => fraction.get(..4).unwrap_or(fraction),
        false => "",
    };
    let offset = offset
        .strip_suffix(":00")
        .filter(|hours| hours.len() == HOURS_OFFSET_LENGTH)
        .unwrap_or(offset);
    format!("{seconds}{fraction}{offset}")
}

/// A recorded time's clock part: `2026-11-14T07:54:11.123456Z` as `07:54:11Z`.
fn clock_time(recorded_at: &str) -> String {
    let time = recorded_at
        .split_once('T')
        .map_or(recorded_at, |(_, time)| time);
    let clock = time.get(..8).unwrap_or(time);
    match time.ends_with('Z') {
        true => format!("{clock}Z"),
        false => clock.to_owned(),
    }
}

/// `item`, an `Errors` item for `problem` (a project problem, or a blocked table's reason), as `plan` prints it: a source that could not be read named by the source, with a headline, its driver's message, a fix and what it holds up (`blocks`); a table's problem named as its table lands, with a headline when its reason has a known one, and a fix that changes a setting starting with where the table's settings are written (`at`, as `sqlload.toml:93`).
pub(crate) fn text_error(
    plan: &ProjectPlan,
    (problem, at): (&str, Option<&str>),
    item: PlanItem,
) -> PlanItem {
    let source = item
        .name
        .strip_prefix("[sources.")
        .and_then(|name| name.strip_suffix(']'));
    let unread = source.and_then(|source| Some((source, plan.unread_sources.get(source)?)));
    match unread {
        Some((source, unread)) => {
            let mut details: Vec<PlanDetail> = item
                .details
                .into_iter()
                .map(|detail| match detail.label.as_str() {
                    "reason" => PlanDetail {
                        text: driver_message(&detail.text),
                        ..detail
                    },
                    _ => detail,
                })
                .collect();
            let reason = details
                .iter()
                .find(|detail| detail.label == REASON_LABEL)
                .map_or_else(String::new, |detail| detail.text.to_lowercase());
            let connects = CONNECTION_WORDS.iter().any(|word| reason.contains(word));
            if connects && !details.iter().any(|detail| detail.label == FIX_LABEL) {
                let check = match unread.variables.as_slice() {
                    [] => format!(
                        "check connection {}'s settings and credentials",
                        unread.connection
                    ),
                    [one] => format!("check {one} (connection {})", unread.connection),
                    [rest @ .., last] => format!(
                        "check {} and {last} (connection {})",
                        rest.join(", "),
                        unread.connection
                    ),
                };
                details.push(detail(
                    "fix",
                    format!("{check}; sqlload debug tests every connection"),
                ));
            }
            details.extend(blocks_line(plan, source, unread).map(|text| detail("blocks", text)));
            PlanItem {
                name: source.to_owned(),
                action: match connects {
                    true => "can't connect".to_owned(),
                    false => item.action,
                },
                details,
            }
        }
        None => PlanItem {
            name: item.name,
            action: HEADLINES
                .iter()
                .find(|(says, _)| problem.contains(says))
                .map_or(item.action, |(_, headline)| (*headline).to_owned()),
            details: item
                .details
                .into_iter()
                .map(|detail| match (detail.label.as_str(), at) {
                    ("fix", Some(at)) if detail.text.contains(" = ") => PlanDetail {
                        text: format!("{at} {}", detail.text),
                        ..detail
                    },
                    _ => detail,
                })
                .collect(),
        },
    }
}

/// Whether `warning` is one `text_error` folds into an unread source's `blocks`.
pub(crate) fn folded_warning(plan: &ProjectPlan, warning: &str) -> bool {
    for sharing in &plan.unread_sharing {
        if sharing.warning == warning
            && sharing
                .sources
                .iter()
                .all(|source| plan.unread_sources.contains_key(source))
        {
            return true;
        }
    }
    false
}

/// What `source`, which could not be read, holds up: its tables, any its include patterns match, and the shared-table check on other tables.
fn blocks_line(plan: &ProjectPlan, source: &str, unread: &UnreadSource) -> Option<String> {
    let mut parts: Vec<String> = unread.tables.clone();
    if unread.patterns {
        parts.push("any table its include patterns match".to_owned());
    }
    for sharing in &plan.unread_sharing {
        if sharing.sources.iter().any(|named| named == source) {
            parts.push(format!(
                "the shared-table check on {}",
                listed(&sharing.tables)
            ));
        }
    }
    match parts.as_slice() {
        [] => None,
        [one] => Some(one.clone()),
        [first, second] => Some(format!("{first} and {second}")),
        [rest @ .., last] => Some(format!("{}, and {last}", rest.join(", "))),
    }
}

/// `names` as `a`, `a and b`, or `a, b and c`.
fn listed(names: &[String]) -> String {
    match names {
        [] => String::new(),
        [only] => only.clone(),
        [rest @ .., last] => format!("{} and {last}", rest.join(", ")),
    }
}

/// An ODBC error as its driver's message with its state and native error, `Login failed for user 'sa' (ODBC state 28000, native error 18456)`; other text as it is.
fn driver_message(text: &str) -> String {
    let field = |name: &str| -> Option<&str> {
        let (_, after) = text.split_once(name)?;
        Some(after.split([',', '\n']).next().unwrap_or(after).trim())
    };
    let message = text.split_once("Message: ").map(|(_, message)| {
        message
            .rsplit(']')
            .next()
            .unwrap_or(message)
            .trim()
            .trim_end_matches('.')
    });
    match (message, field("State: "), field("Native error: ")) {
        (Some(message), Some(state), Some(native)) => {
            format!("{message} (ODBC state {state}, native error {native})")
        }
        _ => text.to_owned(),
    }
}