dataprof-runtime 0.14.0

Shared runtime helpers and composed report types for dataprof
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
//! Structured, prioritized findings over a [`ProfileReport`] (#375).
//!
//! A report carries every metric, and says nothing about which of them
//! deserves attention. Answering that by hand means inventing thresholds at
//! every call site. [`FindingPolicy::evaluate`] (or [`ProfileReport::findings`]
//! with the default thresholds) turns the metrics a report already holds into
//! a short, deterministic list of [`Finding`]s.
//!
//! Findings are interpretation, not data cleaning: each one names what was
//! observed and the evidence behind it, and none carries a raw cell value.
//!
//! # Computed, not stored
//!
//! Findings are derived from the report on demand and are not part of the
//! serialized report. A report read back from its document yields the same
//! findings as the report that wrote it, and changing a threshold does not
//! require re-profiling or rewriting stored reports.
//!
//! # Absence is not a clean result
//!
//! A rule whose input the report does not carry, such as patterns that were
//! never detected or a quality assessment that was not requested, produces no
//! finding, and is listed in [`FindingsResult::not_evaluated`] with the reason.
//! An empty `findings` list means "looked, found nothing" only for the rules
//! that are not listed there.
//!
//! # Scope
//!
//! Findings describe the rows the report read. When the scan stopped early or
//! sampled, a [`FindingCode::PartialScan`] finding says so; the other findings
//! are not withheld, because what was observed is still true of those rows.
//! Use a [`QualityPolicy`](crate::QualityPolicy) for pass/fail decisions about
//! the whole source.

use std::collections::BTreeMap;
use std::fmt;

use dataprof_core::serde_helpers::rounded_2;
use dataprof_core::{
    DataSource, DataType, DelimiterDetection, FileFormat, KeyColumnSource, PatternCategory,
    TypeHomogeneity,
};

use crate::profile_report::ProfileReport;
use crate::quality_gate::{
    Evidence, EvidenceGap, Provenance, quality_status_name, sampled_component_evidence,
};

/// Default for [`FindingPolicy::null_heavy_percentage`]. Matches the
/// `null-heavy` flag in the Python binding's `to_llm_context()`.
pub const DEFAULT_NULL_HEAVY_PERCENTAGE: f64 = 20.0;

/// Default for [`FindingPolicy::mixed_types_percentage`]. Matches the
/// `mixed types` flag in the Python binding's `to_llm_context()`.
pub const DEFAULT_MIXED_TYPES_PERCENTAGE: f64 = 5.0;

/// Pattern confidence a detection needs before a finding reports it: the
/// threshold validity scoring and the report summaries already use.
const MIN_PATTERN_CONFIDENCE: f64 = 0.5;

/// Identifier patterns that name a person. The rest of the identifier
/// category (UUIDs, product codes, VAT numbers) identifies things.
const PERSONAL_IDENTIFIER_PATTERNS: [&str; 2] = ["Codice Fiscale (IT)", "SSN (US)"];

/// How much attention a finding asks for.
///
/// Declared order is priority order: warnings sort first.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, serde::Serialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum Severity {
    /// Likely a defect in the data or in how it was read.
    Warning,
    /// Worth knowing when reading the report; often intended.
    Info,
}

impl fmt::Display for Severity {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Warning => write!(f, "warning"),
            Self::Info => write!(f, "info"),
        }
    }
}

/// Stable identifier for the rule a finding came from.
///
/// Part of the contract: a consumer matches on these, so they do not change
/// when the prose does. Declared in alphabetical order, which is the order
/// findings of equal severity sort in.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, serde::Serialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum FindingCode {
    /// Every value of a column is null.
    AllNull,
    /// Every non-null value of a column is the same one.
    ConstantColumn,
    /// Delimiter detection found a second candidate that read the sampled CSV
    /// records as well as the one it chose.
    DelimiterAmbiguous,
    /// The source holds exact duplicate rows.
    DuplicateRows,
    /// Date values lie after the time the report was produced.
    FutureDates,
    /// No delimiter detection tries splits the CSV header, so the source was
    /// read as one column, while a character outside them splits the header
    /// and every sampled record alike.
    HeaderNotSplit,
    /// The declared key column holds a non-null value more than once.
    KeyDuplicates,
    /// At least half of a column's text values are numbers written with a
    /// decimal comma or digit-group separators, which no numeric statistic
    /// includes.
    LocaleNumbers,
    /// A column's values split across lexical types.
    MixedTypes,
    /// A column's null share is at or above the policy threshold.
    NullHeavy,
    /// The scan stopped early or sampled rows.
    PartialScan,
    /// Rows had a different field count from the header and were recovered.
    RaggedRows,
    /// Errors were counted while reading the source.
    RecordsSkipped,
    /// A column matches a pattern for personal or financial data.
    SensitivePattern,
    /// Start dates fall after their paired end dates.
    TemporalOrderViolations,
    /// A CSV source ended inside a quoted field.
    UnterminatedQuote,
}

impl FindingCode {
    /// Every code, in declared order. A new code is added here too: parsing
    /// a code name, and so `QualityPolicy::forbid_findings` from a binding,
    /// reads this list.
    pub const ALL: [Self; 16] = [
        Self::AllNull,
        Self::ConstantColumn,
        Self::DelimiterAmbiguous,
        Self::DuplicateRows,
        Self::FutureDates,
        Self::HeaderNotSplit,
        Self::KeyDuplicates,
        Self::LocaleNumbers,
        Self::MixedTypes,
        Self::NullHeavy,
        Self::PartialScan,
        Self::RaggedRows,
        Self::RecordsSkipped,
        Self::SensitivePattern,
        Self::TemporalOrderViolations,
        Self::UnterminatedQuote,
    ];

    /// The severity every finding with this code carries.
    pub fn severity(self) -> Severity {
        match self {
            Self::AllNull
            | Self::DelimiterAmbiguous
            | Self::DuplicateRows
            | Self::FutureDates
            | Self::HeaderNotSplit
            | Self::KeyDuplicates
            | Self::LocaleNumbers
            | Self::MixedTypes
            | Self::NullHeavy
            | Self::RaggedRows
            | Self::RecordsSkipped
            | Self::TemporalOrderViolations
            | Self::UnterminatedQuote => Severity::Warning,
            Self::ConstantColumn | Self::PartialScan | Self::SensitivePattern => Severity::Info,
        }
    }

    /// A fixed sentence naming what the rule observed.
    ///
    /// Deliberately free of numbers, as the gate's messages are: the evidence
    /// carries those, at the precision it serializes with.
    fn summary(self) -> &'static str {
        match self {
            Self::AllNull => "every value in this column is null",
            Self::ConstantColumn => "every non-null value in this column is the same",
            Self::DelimiterAmbiguous => {
                "another delimiter reads the sampled CSV records as well as the one detection \
                 chose, so the columns may be split on the wrong one; set csv_delimiter to choose"
            }
            Self::DuplicateRows => "the source contains exact duplicate rows",
            Self::FutureDates => "some date values lie in the future",
            Self::HeaderNotSplit => {
                "no delimiter detection tries (comma, semicolon, tab, pipe) splits the CSV \
                 header, so the source was read as one column, but another character splits the \
                 header and every sampled record alike; set csv_delimiter to read it as a table"
            }
            Self::KeyDuplicates => {
                "some non-null values of the declared key column occur more than once"
            }
            Self::LocaleNumbers => {
                "at least half of the text values in this column are numbers written with a \
                 decimal comma or digit grouping, so no numeric statistic includes them"
            }
            Self::MixedTypes => "this column's values are split across lexical types",
            Self::NullHeavy => "this column's null share is at or above the threshold",
            Self::PartialScan => {
                "only part of the source was read; findings describe the rows that were"
            }
            Self::RaggedRows => {
                "some rows had a different field count from the header and were recovered"
            }
            Self::RecordsSkipped => "errors were counted while reading the source",
            Self::SensitivePattern => {
                "values in this column match a pattern for personal or financial data"
            }
            Self::TemporalOrderViolations => "some start dates fall after their paired end dates",
            Self::UnterminatedQuote => {
                "the source ends inside a quoted field, so its last record may hold several rows"
            }
        }
    }
}

impl fmt::Display for FindingCode {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let name = match self {
            Self::AllNull => "all_null",
            Self::ConstantColumn => "constant_column",
            Self::DelimiterAmbiguous => "delimiter_ambiguous",
            Self::DuplicateRows => "duplicate_rows",
            Self::FutureDates => "future_dates",
            Self::HeaderNotSplit => "header_not_split",
            Self::KeyDuplicates => "key_duplicates",
            Self::LocaleNumbers => "locale_numbers",
            Self::MixedTypes => "mixed_types",
            Self::NullHeavy => "null_heavy",
            Self::PartialScan => "partial_scan",
            Self::RaggedRows => "ragged_rows",
            Self::RecordsSkipped => "records_skipped",
            Self::SensitivePattern => "sensitive_pattern",
            Self::TemporalOrderViolations => "temporal_order_violations",
            Self::UnterminatedQuote => "unterminated_quote",
        };
        write!(f, "{name}")
    }
}

impl std::str::FromStr for FindingCode {
    type Err = String;

    /// Parse a code from its serialized name, such as `locale_numbers`.
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Self::ALL
            .into_iter()
            .find(|code| code.to_string() == s)
            .ok_or_else(|| {
                let valid: Vec<String> = Self::ALL.iter().map(ToString::to_string).collect();
                format!(
                    "Unknown finding code: {s}. Valid codes: {}",
                    valid.join(", ")
                )
            })
    }
}

/// One value a finding read from the report, or a threshold it applied.
///
/// Percentages follow the report's 2dp serialization convention; counts are
/// whole numbers and are not widened into rounded floats.
#[derive(Debug, Clone, PartialEq, serde::Serialize)]
#[serde(untagged)]
#[non_exhaustive]
pub enum EvidenceValue {
    /// A count of rows or values.
    Count(usize),
    /// A 0..=100 percentage.
    Percentage(#[serde(serialize_with = "dataprof_core::serde_helpers::round_2")] f64),
    /// A name from the report: a lexical type, a pattern, a reason.
    Text(String),
    /// A yes/no property of the evidence.
    Flag(bool),
}

/// Something in the report that deserves attention.
#[derive(Debug, Clone, PartialEq, serde::Serialize)]
#[non_exhaustive]
pub struct Finding {
    /// Which rule produced it.
    pub code: FindingCode,
    /// How much attention it asks for. Always `code.severity()`.
    pub severity: Severity,
    /// The column it concerns, absent for a finding about the whole report.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub column: Option<String>,
    /// The metric values and thresholds that caused it, by name. Never a raw
    /// cell value.
    pub evidence: BTreeMap<String, EvidenceValue>,
    /// A fixed sentence naming what was observed.
    pub summary: String,
}

/// Why a rule produced no finding without having looked.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize)]
#[serde(tag = "reason", rename_all = "snake_case")]
#[non_exhaustive]
pub enum NotEvaluatedReason {
    /// The report carries no quality assessment, for this recorded reason.
    #[non_exhaustive]
    QualityUnavailable {
        /// The report's `quality_status` state, verbatim.
        quality_status: String,
    },
    /// Quality was computed, but the dimension the rule reads had nothing to
    /// assess in this run.
    NotAssessed,
    /// The number the rule reads is an estimate, which witnesses nothing: an
    /// estimated duplicate or distinct count is not evidence that a duplicate
    /// exists.
    Estimated,
    /// The count the rule reads is zero, but it came from the retained quality
    /// sample rather than every scanned row, so it rules nothing out for the
    /// rows the sample left behind. A nonzero count is still reported.
    Sampled,
    /// The report does not record what the rule needs to tell a zero from an
    /// unmeasured value: a document written before dataprof recorded it
    /// (ragged rows before 0.10, quality sample coverage before 0.12, how the
    /// key column was chosen before 0.13), a flat summary that omits it, or a
    /// CSV scan that stopped before the end of its source, where the quote
    /// state at the end was never checked.
    Unrecorded,
    /// The metric the rule reads was not computed for these columns: the
    /// metric pack was not selected, or the column type does not carry it.
    NotComputed,
    /// These columns had no values for the rule to look at.
    NoValues,
}

impl NotEvaluatedReason {
    /// Position in the declared order, which is the order entries sort in.
    fn rank(&self) -> u8 {
        match self {
            Self::QualityUnavailable { .. } => 0,
            Self::NotAssessed => 1,
            Self::Estimated => 2,
            Self::Sampled => 3,
            Self::Unrecorded => 4,
            Self::NotComputed => 5,
            Self::NoValues => 6,
        }
    }
}

/// A rule that could not be evaluated, and why.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize)]
#[non_exhaustive]
pub struct UnevaluatedRule {
    /// The rule.
    pub code: FindingCode,
    /// Why it was not evaluated.
    #[serde(flatten)]
    pub reason: NotEvaluatedReason,
    /// For a column rule, the columns it could not look at, in report order.
    /// Empty for a rule about the whole report.
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub columns: Vec<String>,
}

/// What [`FindingPolicy::evaluate`] found.
#[derive(Debug, Clone, PartialEq, serde::Serialize)]
#[non_exhaustive]
pub struct FindingsResult {
    /// Every finding, most severe first. Within a severity: by code, then
    /// report-level before column-level, then by column position in the
    /// report, then by pattern name.
    pub findings: Vec<Finding>,
    /// Every rule that could not look, by code then reason. A rule listed
    /// here for some columns was still evaluated for the others.
    pub not_evaluated: Vec<UnevaluatedRule>,
}

/// The facts of a report the findings rules read, and nothing else (#909).
///
/// [`FindingPolicy::evaluate`] builds one from a [`ProfileReport`]. A report a
/// binding loaded from a flat summary has no `ProfileReport` behind it: the
/// binding writes these facts from the summary as JSON and reads them with
/// [`FindingsInput::from_json`], so both kinds of report go through one set
/// of rules. A count the summary does not carry reads as zero, as the rules
/// read an absent count.
///
/// A fact about how the source was read is `None` when the report does not
/// record it (#1306). The rule that reads it then lists itself as not
/// evaluated, so an unrecorded fact never reads as a clean one.
#[derive(Debug, Clone, PartialEq, serde::Deserialize)]
#[non_exhaustive]
pub struct FindingsInput {
    rows_processed: usize,
    /// `None` only for a flat summary that omits it.
    source_exhausted: Option<bool>,
    /// Whether the report records a truncation reason.
    truncated: bool,
    /// `None` only for a flat summary that omits it.
    sampling_applied: Option<bool>,
    /// `None` for a document written before 0.10, which did not count ragged
    /// rows and reads back with a zero that was never measured.
    ragged_row_count: Option<usize>,
    /// `None` where the quote state was not checked: a source that is not
    /// CSV, a scan that stopped before the end of its source, or a report
    /// written before 0.12; or where a flat summary omits it.
    unterminated_quote: Option<bool>,
    /// Whether the source was read as CSV, the only format with quoted
    /// fields. `None` when the report does not record the source format, so
    /// an unchecked quote state cannot be told apart from one that does not
    /// apply.
    #[serde(default)]
    csv_source: Option<bool>,
    error_count: usize,
    /// What delimiter detection saw, when the report records that it ran.
    /// A flat summary records no source metadata, so it reads as absent.
    #[serde(default)]
    delimiter_detection: Option<DelimiterDetection>,
    /// A CSV file or bytes source whose report predates the CSV source
    /// metadata (#1100), so the delimiter rules cannot tell a clear choice
    /// from an unrecorded one.
    #[serde(default)]
    csv_source_unrecorded: bool,
    /// How the report chose the column `key_uniqueness` measured. Absent for
    /// a report written before 0.13, whose key may be a guess from column
    /// names (#1065).
    #[serde(default)]
    key_column_source: Option<KeyColumnSource>,
    /// The report's `quality_status` state, read when `quality` is absent.
    quality_status: String,
    quality: Option<QualityFacts>,
    columns: Vec<ColumnFacts>,
}

#[derive(Debug, Clone, PartialEq, serde::Deserialize)]
struct QualityFacts {
    uniqueness: Option<UniquenessFacts>,
    timeliness: Option<TimelinessFacts>,
    /// The sampled component labels, `None` when the report did not record
    /// them.
    sampled_dimensions: Option<Vec<String>>,
    /// Components with signal that neither recorded list names. Empty for a
    /// flat summary written before 0.14, which records no exact list (#1027).
    unlisted_components: Vec<String>,
}

#[derive(Debug, Clone, PartialEq, serde::Deserialize)]
struct UniquenessFacts {
    #[serde(default)]
    rows_checked: usize,
    #[serde(default)]
    duplicate_rows: usize,
    #[serde(default)]
    duplicate_rows_approximate: bool,
    /// The column `key_uniqueness` measured, absent when no key was measured.
    #[serde(default)]
    key_column: Option<String>,
}

#[derive(Debug, Clone, PartialEq, serde::Deserialize)]
struct TimelinessFacts {
    #[serde(default)]
    date_values_checked: usize,
    #[serde(default)]
    future_dates_count: usize,
    #[serde(default)]
    temporal_pairs_checked: usize,
    #[serde(default)]
    temporal_violations: usize,
}

#[derive(Debug, Clone, PartialEq, serde::Deserialize)]
struct ColumnFacts {
    name: String,
    /// Typed as an identifier, which `mixed_types` and `locale_numbers` exempt.
    identifier: bool,
    total_count: usize,
    null_count: usize,
    unique_count: Option<usize>,
    /// Whether `unique_count` is an estimate; absent when not recorded.
    #[serde(default)]
    unique_count_is_approximate: Option<bool>,
    type_homogeneity: Option<TypeHomogeneity>,
    locale_number_count: Option<usize>,
    patterns: Option<Vec<PatternFacts>>,
}

#[derive(Debug, Clone, PartialEq, serde::Deserialize)]
struct PatternFacts {
    name: String,
    /// The category as the report names it; a summary from a later release
    /// may name one this build does not know.
    category: String,
    confidence: f64,
    match_percentage: f64,
}

impl FindingsInput {
    /// Read the facts a binding wrote for a report it holds no
    /// [`ProfileReport`] for.
    pub fn from_json(text: &str) -> serde_json::Result<Self> {
        serde_json::from_str(text)
    }

    /// Whether the rule for `code` read some column's retained value sample
    /// rather than all of its values.
    ///
    /// `mixed_types`, `locale_numbers` and `sensitive_pattern` read a
    /// column's lexical forms, locale numbers and patterns, which the
    /// profiler takes from the same retained sample. A classified count short
    /// of the column's non-null count says the sample did not hold every
    /// value, even on a scan that read the whole source. Columns a rule
    /// exempts (identifiers, for the first two) are not counted. Every other
    /// code reads exact counters, or records its sampling itself.
    pub(crate) fn reads_a_partial_sample(&self, code: FindingCode) -> bool {
        let exempts_identifiers = match code {
            FindingCode::MixedTypes | FindingCode::LocaleNumbers => true,
            FindingCode::SensitivePattern => false,
            _ => return false,
        };
        self.columns
            .iter()
            .filter(|column| !(exempts_identifiers && column.identifier))
            .any(|column| {
                let non_null = column.total_count.saturating_sub(column.null_count);
                column
                    .type_homogeneity
                    .as_ref()
                    .is_some_and(|forms| forms.classified_count() < non_null)
            })
    }
}

impl From<&ProfileReport> for FindingsInput {
    fn from(report: &ProfileReport) -> Self {
        let execution = &report.execution;
        Self {
            rows_processed: execution.rows_processed,
            source_exhausted: Some(execution.source_exhausted),
            truncated: execution.truncation_reason.is_some(),
            sampling_applied: Some(execution.sampling_applied),
            // Ragged rows were first counted by the release that introduced
            // schema versioning (0.10, #452).
            ragged_row_count: (report.schema_version != 0).then_some(execution.ragged_row_count),
            unterminated_quote: execution.unterminated_quote,
            csv_source: csv_source(&report.data_source),
            error_count: execution.error_count,
            delimiter_detection: report
                .data_source
                .csv_metadata()
                .and_then(|metadata| metadata.delimiter_detection.clone()),
            csv_source_unrecorded: csv_source_unrecorded(&report.data_source),
            key_column_source: report
                .metric_semantics
                .as_ref()
                .and_then(|semantics| semantics.key_column_source),
            quality_status: quality_status_name(&report.quality_status).to_string(),
            quality: report.quality.as_ref().map(|quality| QualityFacts {
                uniqueness: quality
                    .metrics
                    .uniqueness
                    .as_ref()
                    .map(|uniqueness| UniquenessFacts {
                        rows_checked: uniqueness.rows_checked,
                        duplicate_rows: uniqueness.duplicate_rows,
                        duplicate_rows_approximate: uniqueness.duplicate_rows_approximate,
                        key_column: uniqueness.key_column.clone(),
                    }),
                timeliness: quality
                    .metrics
                    .timeliness
                    .as_ref()
                    .map(|timeliness| TimelinessFacts {
                        date_values_checked: timeliness.date_values_checked,
                        future_dates_count: timeliness.future_dates_count,
                        temporal_pairs_checked: timeliness.temporal_pairs_checked,
                        temporal_violations: timeliness.temporal_violations,
                    }),
                sampled_dimensions: quality.sampled_dimensions(),
                unlisted_components: quality.unlisted_components(),
            }),
            columns: report
                .column_profiles
                .iter()
                .map(|column| ColumnFacts {
                    name: column.name.clone(),
                    identifier: column.data_type == DataType::Identifier,
                    total_count: column.total_count,
                    null_count: column.null_count,
                    unique_count: column.unique_count,
                    unique_count_is_approximate: column.unique_count_is_approximate,
                    type_homogeneity: column.type_homogeneity,
                    locale_number_count: column.locale_number_count,
                    patterns: column.patterns.as_ref().map(|patterns| {
                        patterns
                            .iter()
                            .map(|pattern| PatternFacts {
                                name: pattern.name.clone(),
                                category: pattern.category.to_string(),
                                confidence: pattern.confidence,
                                match_percentage: pattern.match_percentage,
                            })
                            .collect()
                    }),
                })
                .collect(),
        }
    }
}

/// A finding threshold that cannot be applied as written.
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub enum FindingPolicyError {
    /// A threshold fell outside `(0, 100]` at the 2dp precision it is applied
    /// at, or was not a finite number. Percentages are on the report's 0..100
    /// scale, not 0..1 ratios, and a zero threshold would report every column.
    #[non_exhaustive]
    ThresholdOutOfRange {
        /// The setting that carried it.
        setting: &'static str,
        /// The value as supplied.
        value: f64,
    },
}

impl fmt::Display for FindingPolicyError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::ThresholdOutOfRange { setting, value } => write!(
                f,
                "{setting} must be a percentage above 0 and at most 100 at two decimal \
                 places, got {value}"
            ),
        }
    }
}

impl std::error::Error for FindingPolicyError {}

/// The thresholds findings apply.
///
/// Separate from the profiler's configuration on purpose: findings interpret
/// a finished report, including one read back from disk that no profiler
/// configuration accompanies.
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct FindingPolicy {
    null_heavy_percentage: f64,
    mixed_types_percentage: f64,
}

impl Default for FindingPolicy {
    fn default() -> Self {
        Self {
            null_heavy_percentage: DEFAULT_NULL_HEAVY_PERCENTAGE,
            mixed_types_percentage: DEFAULT_MIXED_TYPES_PERCENTAGE,
        }
    }
}

impl FindingPolicy {
    /// The default thresholds.
    pub fn new() -> Self {
        Self::default()
    }

    /// Report a column as `null_heavy` when its null percentage, at the
    /// report's 2dp precision, is at least `percentage`. The threshold is
    /// applied at 2dp too, so the evidence states exactly what was compared.
    pub fn null_heavy_percentage(mut self, percentage: f64) -> Self {
        self.null_heavy_percentage = percentage;
        self
    }

    /// Report a column as `mixed_types` when the values outside its dominant
    /// lexical type make up at least `percentage` of those classified, both
    /// at 2dp.
    pub fn mixed_types_percentage(mut self, percentage: f64) -> Self {
        self.mixed_types_percentage = percentage;
        self
    }

    /// Check that every threshold can be applied, without needing a report.
    pub fn validate(&self) -> Result<(), FindingPolicyError> {
        for (setting, value) in [
            ("null_heavy_percentage", self.null_heavy_percentage),
            ("mixed_types_percentage", self.mixed_types_percentage),
        ] {
            let applied = rounded_2(value);
            if !(value.is_finite() && applied > 0.0 && applied <= 100.0) {
                return Err(FindingPolicyError::ThresholdOutOfRange { setting, value });
            }
        }
        Ok(())
    }

    /// Derive the findings a report supports.
    ///
    /// The report is not modified and nothing is printed.
    pub fn evaluate(&self, report: &ProfileReport) -> Result<FindingsResult, FindingPolicyError> {
        self.evaluate_input(&FindingsInput::from(report))
    }

    /// [`evaluate`](Self::evaluate) over the facts of a report, for a report
    /// a binding holds no [`ProfileReport`] for.
    pub fn evaluate_input(
        &self,
        input: &FindingsInput,
    ) -> Result<FindingsResult, FindingPolicyError> {
        self.validate()?;
        Ok(self.collect(input))
    }

    /// The thresholds as applied: at the precision a percentage is compared
    /// and reported at.
    fn null_heavy_threshold(&self) -> f64 {
        rounded_2(self.null_heavy_percentage)
    }

    fn mixed_types_threshold(&self) -> f64 {
        rounded_2(self.mixed_types_percentage)
    }

    pub(crate) fn collect(&self, input: &FindingsInput) -> FindingsResult {
        let mut collector = Collector::default();
        scan_findings(input, &mut collector);
        quality_findings(input, &mut collector);
        for (index, column) in input.columns.iter().enumerate() {
            self.column_findings(index, column, &mut collector);
        }
        collector.finish()
    }

    fn column_findings(&self, index: usize, column: &ColumnFacts, out: &mut Collector) {
        let name = column.name.as_str();
        let non_null = column.total_count.saturating_sub(column.null_count);

        if column.total_count == 0 {
            out.skip(FindingCode::AllNull, NotEvaluatedReason::NoValues, name);
            out.skip(FindingCode::NullHeavy, NotEvaluatedReason::NoValues, name);
        } else if non_null == 0 {
            out.column(
                FindingCode::AllNull,
                index,
                name,
                [
                    ("null_count", EvidenceValue::Count(column.null_count)),
                    ("total_count", EvidenceValue::Count(column.total_count)),
                ],
            );
        } else {
            // Compared at the serialized precision, so a report read back from
            // its document lands on the same side of the threshold.
            let null_percentage =
                rounded_2(column.null_count as f64 / column.total_count as f64 * 100.0);
            if null_percentage >= self.null_heavy_threshold() {
                out.column(
                    FindingCode::NullHeavy,
                    index,
                    name,
                    [
                        ("null_percentage", percentage(null_percentage)),
                        ("threshold", percentage(self.null_heavy_threshold())),
                    ],
                );
            }
        }

        match column.unique_count {
            None => out.skip(
                FindingCode::ConstantColumn,
                NotEvaluatedReason::NotComputed,
                name,
            ),
            Some(_) if non_null == 0 => out.skip(
                FindingCode::ConstantColumn,
                NotEvaluatedReason::NoValues,
                name,
            ),
            // One value seen once is not a constant, just a single value.
            Some(1) if non_null > 1 => out.column(
                FindingCode::ConstantColumn,
                index,
                name,
                [
                    ("unique_count", EvidenceValue::Count(1)),
                    ("non_null_count", EvidenceValue::Count(non_null)),
                ],
            ),
            Some(_) => {}
        }

        locale_numbers(index, column, out);
        self.mixed_types(index, column, non_null, out);
        sensitive_patterns(index, column, non_null, out);
    }

    fn mixed_types(
        &self,
        index: usize,
        column: &ColumnFacts,
        non_null: usize,
        out: &mut Collector,
    ) {
        let name = column.name.as_str();
        // Mixing forms ("A1", "123") is what an identifier scheme does, not a
        // defect: the same exemption the consistency dimension makes.
        if column.identifier {
            return;
        }
        let Some(homogeneity) = column.type_homogeneity.as_ref() else {
            out.skip(
                FindingCode::MixedTypes,
                NotEvaluatedReason::NotComputed,
                name,
            );
            return;
        };
        let classified = homogeneity.classified_count();
        let Some((dominant, dominant_count)) = homogeneity.dominant() else {
            out.skip(FindingCode::MixedTypes, NotEvaluatedReason::NoValues, name);
            return;
        };
        let outside = classified - dominant_count;
        if outside == 0 {
            return;
        }
        let outside_percentage = rounded_2(outside as f64 / classified as f64 * 100.0);
        if outside_percentage < self.mixed_types_threshold() {
            return;
        }
        out.column(
            FindingCode::MixedTypes,
            index,
            name,
            [
                ("dominant_type", EvidenceValue::Text(dominant.to_string())),
                (
                    "dominant_percentage",
                    percentage(dominant_count as f64 / classified as f64 * 100.0),
                ),
                ("threshold", percentage(self.mixed_types_threshold())),
                // Shares are counted over the values the profiler retained; a
                // classified count short of the non-null count says they were
                // sampled.
                ("classified_count", EvidenceValue::Count(classified)),
                ("non_null_count", EvidenceValue::Count(non_null)),
            ],
        );
    }
}

/// Report a column at least half of whose text values are numbers written in
/// a locale format (#433).
///
/// Measured against the text values rather than the whole column, so it
/// covers both ways these numbers go missing: a column of `1.234,56` typed
/// `string` with no numeric statistics, and a numeric column whose
/// decimal-comma values were counted as invalid. At least half, so that a free
/// text column holding a stray `1,5` is not reported.
fn locale_numbers(index: usize, column: &ColumnFacts, out: &mut Collector) {
    let name = column.name.as_str();
    // The exemption `mixed_types` makes: `1.234.567` in an identifier
    // scheme is a code, not an amount.
    if column.identifier {
        return;
    }
    let Some(homogeneity) = column.type_homogeneity.as_ref() else {
        out.skip(
            FindingCode::LocaleNumbers,
            NotEvaluatedReason::NotComputed,
            name,
        );
        return;
    };
    // Classified, but by a release that did not count these yet.
    let Some(locale_numbers) = column.locale_number_count else {
        out.skip(
            FindingCode::LocaleNumbers,
            NotEvaluatedReason::Unrecorded,
            name,
        );
        return;
    };
    let classified = homogeneity.classified_count();
    if classified == 0 {
        out.skip(
            FindingCode::LocaleNumbers,
            NotEvaluatedReason::NoValues,
            name,
        );
        return;
    }
    let text = homogeneity.text;
    if locale_numbers == 0 || locale_numbers * 2 < text {
        return;
    }
    out.column(
        FindingCode::LocaleNumbers,
        index,
        name,
        [
            ("locale_number_count", EvidenceValue::Count(locale_numbers)),
            ("text_count", EvidenceValue::Count(text)),
            // Counted over the values the profiler retained, as in
            // `mixed_types`.
            ("classified_count", EvidenceValue::Count(classified)),
            (
                "non_null_count",
                EvidenceValue::Count(column.total_count.saturating_sub(column.null_count)),
            ),
        ],
    );
}

impl ProfileReport {
    /// The findings this report supports under the default thresholds.
    ///
    /// Shorthand for `FindingPolicy::default().evaluate(self)`, which cannot
    /// fail because the defaults are in range.
    pub fn findings(&self) -> FindingsResult {
        FindingPolicy::default().collect(&FindingsInput::from(self))
    }
}

fn scan_findings(input: &FindingsInput, out: &mut Collector) {
    // A recorded stop or sample witnesses a partial scan even when the other
    // fact is unrecorded; only "read everything" needs both.
    let partial = if input.source_exhausted == Some(false) || input.truncated {
        Some("truncated")
    } else if input.sampling_applied == Some(true) {
        Some("sampled")
    } else {
        None
    };
    match partial {
        Some(reason) => out.report(
            FindingCode::PartialScan,
            [
                ("reason", EvidenceValue::Text(reason.to_string())),
                ("rows_processed", EvidenceValue::Count(input.rows_processed)),
            ],
        ),
        None if input.source_exhausted.is_none() || input.sampling_applied.is_none() => {
            out.skip_report(FindingCode::PartialScan, NotEvaluatedReason::Unrecorded);
        }
        None => {}
    }
    match input.ragged_row_count {
        None => out.skip_report(FindingCode::RaggedRows, NotEvaluatedReason::Unrecorded),
        Some(0) => {}
        Some(ragged_row_count) => out.report(
            FindingCode::RaggedRows,
            [
                ("ragged_row_count", EvidenceValue::Count(ragged_row_count)),
                ("rows_processed", EvidenceValue::Count(input.rows_processed)),
            ],
        ),
    }
    match (input.unterminated_quote, input.csv_source) {
        (Some(true), _) => out.report(
            FindingCode::UnterminatedQuote,
            [("rows_processed", EvidenceValue::Count(input.rows_processed))],
        ),
        (Some(false), _) => {}
        // Not applicable: a source without quoted fields ends inside none.
        // Decided from the recorded format, never from the absence.
        (None, Some(false)) => {}
        (None, _) => out.skip_report(
            FindingCode::UnterminatedQuote,
            NotEvaluatedReason::Unrecorded,
        ),
    }
    if input.error_count > 0 {
        out.report(
            FindingCode::RecordsSkipped,
            [("error_count", EvidenceValue::Count(input.error_count))],
        );
    }
    if input.csv_source_unrecorded {
        out.skip_report(
            FindingCode::DelimiterAmbiguous,
            NotEvaluatedReason::Unrecorded,
        );
        out.skip_report(FindingCode::HeaderNotSplit, NotEvaluatedReason::Unrecorded);
    }
    // A delimiter the caller set records no detection, so neither rule asks
    // about a choice nobody made.
    if let Some(detection) = &input.delimiter_detection {
        if let Some(runner_up) = &detection.runner_up {
            out.report(
                FindingCode::DelimiterAmbiguous,
                [
                    (
                        "delimiter",
                        EvidenceValue::Text(detection.delimiter.clone()),
                    ),
                    ("runner_up", EvidenceValue::Text(runner_up.clone())),
                ],
            );
        }
        // A one-column source is often intended, so an unsplit header alone
        // is no finding: only a character outside the candidates that splits
        // the header and every sampled record alike makes the reading doubtful.
        if let Some(unlisted) = &detection.unlisted_delimiter {
            out.report(
                FindingCode::HeaderNotSplit,
                [("unlisted_delimiter", EvidenceValue::Text(unlisted.clone()))],
            );
        }
    }
}

/// Whether `source` was read as CSV, `None` for a stream that records no
/// format (written before 0.14, #1042). A query or dataframe source has no
/// text to quote.
fn csv_source(source: &DataSource) -> Option<bool> {
    match source {
        DataSource::Stream { format: None, .. } => None,
        _ => Some(matches!(source.format(), Some(FileFormat::Csv))),
    }
}

/// Whether `source` is a CSV source that records no CSV source metadata: a
/// report written before 0.14 recorded it. A stream written before 0.14
/// records no format either (#1042), so it cannot be told apart.
fn csv_source_unrecorded(source: &DataSource) -> bool {
    match source {
        DataSource::File { format, .. }
        | DataSource::Bytes { format, .. }
        | DataSource::Stream {
            format: Some(format),
            ..
        } => matches!(format, FileFormat::Csv) && source.csv_metadata().is_none(),
        _ => false,
    }
}

fn quality_findings(input: &FindingsInput, out: &mut Collector) {
    const QUALITY_RULES: [FindingCode; 4] = [
        FindingCode::DuplicateRows,
        FindingCode::FutureDates,
        FindingCode::KeyDuplicates,
        FindingCode::TemporalOrderViolations,
    ];
    let Some(quality) = input.quality.as_ref() else {
        for code in QUALITY_RULES {
            out.skip_report(
                code,
                NotEvaluatedReason::QualityUnavailable {
                    quality_status: input.quality_status.clone(),
                },
            );
        }
        return;
    };

    match quality
        .uniqueness
        .as_ref()
        .filter(|uniqueness| uniqueness.rows_checked > 0)
    {
        None => out.skip_report(FindingCode::DuplicateRows, NotEvaluatedReason::NotAssessed),
        Some(uniqueness) if uniqueness.duplicate_rows_approximate => {
            out.skip_report(FindingCode::DuplicateRows, NotEvaluatedReason::Estimated)
        }
        Some(uniqueness) if uniqueness.duplicate_rows > 0 => out.report(
            FindingCode::DuplicateRows,
            [
                (
                    "duplicate_rows",
                    EvidenceValue::Count(uniqueness.duplicate_rows),
                ),
                (
                    "rows_checked",
                    EvidenceValue::Count(uniqueness.rows_checked),
                ),
            ],
        ),
        Some(_) => skip_unless_complete(out, FindingCode::DuplicateRows, quality, "duplicate_rows"),
    }

    key_duplicates(input, quality, out);

    let timeliness = quality.timeliness.as_ref();
    match timeliness.filter(|timeliness| timeliness.date_values_checked > 0) {
        None => out.skip_report(FindingCode::FutureDates, NotEvaluatedReason::NotAssessed),
        Some(timeliness) if timeliness.future_dates_count > 0 => out.report(
            FindingCode::FutureDates,
            [
                (
                    "future_dates_count",
                    EvidenceValue::Count(timeliness.future_dates_count),
                ),
                (
                    "date_values_checked",
                    EvidenceValue::Count(timeliness.date_values_checked),
                ),
            ],
        ),
        Some(_) => skip_unless_complete(out, FindingCode::FutureDates, quality, "timeliness"),
    }
    match timeliness.filter(|timeliness| timeliness.temporal_pairs_checked > 0) {
        None => out.skip_report(
            FindingCode::TemporalOrderViolations,
            NotEvaluatedReason::NotAssessed,
        ),
        Some(timeliness) if timeliness.temporal_violations > 0 => out.report(
            FindingCode::TemporalOrderViolations,
            [
                (
                    "temporal_violations",
                    EvidenceValue::Count(timeliness.temporal_violations),
                ),
                (
                    "temporal_pairs_checked",
                    EvidenceValue::Count(timeliness.temporal_pairs_checked),
                ),
            ],
        ),
        Some(_) => skip_unless_complete(
            out,
            FindingCode::TemporalOrderViolations,
            quality,
            "timeliness",
        ),
    }
}

/// Report repeated values in the declared key column (#1068).
///
/// Reads the key the uniqueness dimension measured, which is only ever a
/// declared one (#1065), and the key column's own distinct count over its
/// non-null values: a null key is completeness's finding, not a repeat. Whole
/// duplicate rows are `duplicate_rows`; this finds the same key on rows that
/// differ elsewhere, which no row comparison sees.
///
/// The rule needs an exact distinct count. An estimate witnesses no repeat,
/// and a report that does not record whether its count was exact, or how its
/// key was chosen, is listed rather than judged.
fn key_duplicates(input: &FindingsInput, quality: &QualityFacts, out: &mut Collector) {
    const CODE: FindingCode = FindingCode::KeyDuplicates;
    let Some(key) = quality
        .uniqueness
        .as_ref()
        .and_then(|uniqueness| uniqueness.key_column.as_deref())
    else {
        out.skip_report(CODE, NotEvaluatedReason::NotAssessed);
        return;
    };
    // Before 0.13 an undeclared key was the first id-like column name, so
    // the column may be a foreign key that repeats by design.
    let Some(source) = input.key_column_source else {
        out.skip(CODE, NotEvaluatedReason::Unrecorded, key);
        return;
    };
    let Some((index, column)) = input
        .columns
        .iter()
        .enumerate()
        .find(|(_, column)| column.name == key)
    else {
        out.skip(CODE, NotEvaluatedReason::NotComputed, key);
        return;
    };
    let Some(unique_count) = column.unique_count else {
        out.skip(CODE, NotEvaluatedReason::NotComputed, key);
        return;
    };
    let values = column.total_count.saturating_sub(column.null_count);
    if values == 0 {
        out.skip(CODE, NotEvaluatedReason::NoValues, key);
        return;
    }
    let coverage = sampled_component_evidence(
        quality.sampled_dimensions.as_deref(),
        &quality.unlisted_components,
        &[],
        false,
        Provenance::Component("key_uniqueness"),
    );
    // The key component is filed as sampled exactly when the key's distinct
    // count is an estimate.
    let estimated = column.unique_count_is_approximate == Some(true)
        || matches!(
            coverage,
            Evidence::Incomplete {
                reason: EvidenceGap::QualitySampled
            }
        );
    if estimated {
        out.skip(CODE, NotEvaluatedReason::Estimated, key);
        return;
    }
    let exact =
        column.unique_count_is_approximate == Some(false) || matches!(coverage, Evidence::Complete);
    if !exact {
        out.skip(CODE, NotEvaluatedReason::Unrecorded, key);
        return;
    }
    let repeated = values.saturating_sub(unique_count);
    if repeated == 0 {
        return;
    }
    let source = match source {
        KeyColumnSource::Declared => "declared",
    };
    out.column(
        CODE,
        index,
        key,
        [
            ("key_values_checked", EvidenceValue::Count(values)),
            ("repeated_values", EvidenceValue::Count(repeated)),
            ("key_column_source", EvidenceValue::Text(source.to_string())),
        ],
    );
}

/// A zero count is a clean result only when it covers every scanned row.
///
/// A count from the retained quality sample, or from a report that does not
/// record where its counts came from, rules nothing out for the rows it did
/// not see, so the rule is listed rather than read as clean. A nonzero count
/// never reaches here: rows already witnessed stay witnessed. Coverage is
/// resolved by the quality gate's rule, so a finding and a check never
/// disagree about one component.
fn skip_unless_complete(
    out: &mut Collector,
    code: FindingCode,
    quality: &QualityFacts,
    component: &'static str,
) {
    // A component's coverage does not depend on the dimensions the overall
    // score weighs, so none are passed.
    let coverage = sampled_component_evidence(
        quality.sampled_dimensions.as_deref(),
        &quality.unlisted_components,
        &[],
        false,
        Provenance::Component(component),
    );
    match coverage {
        Evidence::Complete => {}
        Evidence::Incomplete {
            reason: EvidenceGap::QualitySampled,
        } => out.skip_report(code, NotEvaluatedReason::Sampled),
        Evidence::Incomplete { .. } => out.skip_report(code, NotEvaluatedReason::Unrecorded),
    }
}

/// Whether a detected pattern names personal or financial data.
fn is_sensitive(category: &str, name: &str) -> bool {
    [PatternCategory::Contact, PatternCategory::Financial]
        .iter()
        .any(|sensitive| sensitive.to_string() == category)
        || PERSONAL_IDENTIFIER_PATTERNS.contains(&name)
}

fn sensitive_patterns(index: usize, column: &ColumnFacts, non_null: usize, out: &mut Collector) {
    let Some(patterns) = column.patterns.as_ref() else {
        out.skip(
            FindingCode::SensitivePattern,
            NotEvaluatedReason::NotComputed,
            &column.name,
        );
        return;
    };
    if non_null == 0 {
        // Detection ran over nothing, which is no evidence either way.
        out.skip(
            FindingCode::SensitivePattern,
            NotEvaluatedReason::NoValues,
            &column.name,
        );
        return;
    }
    for pattern in patterns {
        if pattern.confidence < MIN_PATTERN_CONFIDENCE
            || !is_sensitive(&pattern.category, &pattern.name)
        {
            continue;
        }
        out.push(
            Finding {
                code: FindingCode::SensitivePattern,
                severity: FindingCode::SensitivePattern.severity(),
                column: Some(column.name.clone()),
                evidence: evidence([
                    ("pattern", EvidenceValue::Text(pattern.name.clone())),
                    ("category", EvidenceValue::Text(pattern.category.clone())),
                    ("match_percentage", percentage(pattern.match_percentage)),
                ]),
                summary: FindingCode::SensitivePattern.summary().to_string(),
            },
            Some(index),
            pattern.name.clone(),
        );
    }
}

/// Percentage evidence at the precision it serializes with, so a finding
/// compares equal to one rebuilt from its own document.
fn percentage(value: f64) -> EvidenceValue {
    EvidenceValue::Percentage(rounded_2(value))
}

fn evidence<const N: usize>(
    entries: [(&'static str, EvidenceValue); N],
) -> BTreeMap<String, EvidenceValue> {
    entries
        .into_iter()
        .map(|(name, value)| (name.to_string(), value))
        .collect()
}

/// A finding with the keys it sorts by.
struct Ranked {
    finding: Finding,
    /// `None` for a report-level finding, which sorts before column findings.
    column_index: Option<usize>,
    /// Breaks ties between findings of one code on one column.
    detail: String,
}

#[derive(Default)]
struct Collector {
    findings: Vec<Ranked>,
    not_evaluated: Vec<UnevaluatedRule>,
}

impl Collector {
    fn push(&mut self, finding: Finding, column_index: Option<usize>, detail: String) {
        self.findings.push(Ranked {
            finding,
            column_index,
            detail,
        });
    }

    fn report<const N: usize>(
        &mut self,
        code: FindingCode,
        entries: [(&'static str, EvidenceValue); N],
    ) {
        self.push(
            Finding {
                code,
                severity: code.severity(),
                column: None,
                evidence: evidence(entries),
                summary: code.summary().to_string(),
            },
            None,
            String::new(),
        );
    }

    fn column<const N: usize>(
        &mut self,
        code: FindingCode,
        index: usize,
        name: &str,
        entries: [(&'static str, EvidenceValue); N],
    ) {
        self.push(
            Finding {
                code,
                severity: code.severity(),
                column: Some(name.to_string()),
                evidence: evidence(entries),
                summary: code.summary().to_string(),
            },
            Some(index),
            String::new(),
        );
    }

    fn skip_report(&mut self, code: FindingCode, reason: NotEvaluatedReason) {
        self.not_evaluated.push(UnevaluatedRule {
            code,
            reason,
            columns: Vec::new(),
        });
    }

    /// Record a column a rule could not look at, joining the entry for the
    /// same rule and reason so each pair is listed once.
    fn skip(&mut self, code: FindingCode, reason: NotEvaluatedReason, column: &str) {
        match self
            .not_evaluated
            .iter_mut()
            .find(|entry| entry.code == code && entry.reason == reason)
        {
            Some(entry) => entry.columns.push(column.to_string()),
            None => self.not_evaluated.push(UnevaluatedRule {
                code,
                reason,
                columns: vec![column.to_string()],
            }),
        }
    }

    fn finish(mut self) -> FindingsResult {
        self.findings.sort_by(|left, right| {
            (left.finding.severity, left.finding.code)
                .cmp(&(right.finding.severity, right.finding.code))
                .then_with(|| left.column_index.cmp(&right.column_index))
                .then_with(|| left.detail.cmp(&right.detail))
        });
        // Stable, so the columns inside one entry keep report order.
        self.not_evaluated.sort_by(|left, right| {
            (left.code, left.reason.rank()).cmp(&(right.code, right.reason.rank()))
        });
        FindingsResult {
            findings: self
                .findings
                .into_iter()
                .map(|ranked| ranked.finding)
                .collect(),
            not_evaluated: self.not_evaluated,
        }
    }
}

#[cfg(test)]
mod tests {
    use dataprof_core::{
        ColumnProfile, ColumnStats, CsvSourceMetadata, DataSource, ExecutionMetadata, FileFormat,
        Pattern, TruncationReason, TypeHomogeneity,
    };
    use dataprof_metrics::{
        MetricConfidence, QualityAssessment, QualityMetrics, TimelinessMetrics, UniquenessMetrics,
    };

    use super::*;

    fn source() -> DataSource {
        DataSource::File {
            path: "drop.csv".to_string(),
            format: FileFormat::Csv,
            size_bytes: 256,
            modified_at: None,
            parquet_metadata: None,
            csv_metadata: None,
        }
    }

    fn column(name: &str, total: usize, nulls: usize) -> ColumnProfile {
        ColumnProfile {
            name: name.to_string(),
            data_type: DataType::String,
            null_count: nulls,
            total_count: total,
            unique_count: None,
            unique_count_is_approximate: None,
            unique_count_lower_bound: None,
            invalid_count: None,
            type_homogeneity: None,
            locale_number_count: None,
            native_type: None,
            stats: ColumnStats::None,
            patterns: None,
        }
    }

    fn pattern(name: &str, category: PatternCategory, confidence: f64) -> Pattern {
        Pattern {
            name: name.to_string(),
            regex: String::new(),
            match_count: 10,
            match_percentage: 100.0,
            category,
            confidence,
        }
    }

    fn report(columns: Vec<ColumnProfile>, execution: ExecutionMetadata) -> ProfileReport {
        ProfileReport::new(source(), columns, execution, None)
    }

    fn codes(result: &FindingsResult) -> Vec<(FindingCode, Option<&str>)> {
        result
            .findings
            .iter()
            .map(|finding| (finding.code, finding.column.as_deref()))
            .collect()
    }

    #[test]
    fn an_estimated_duplicate_count_witnesses_nothing() {
        let metrics = QualityMetrics {
            uniqueness: Some(UniquenessMetrics {
                duplicate_rows: 1_200,
                key_uniqueness: 100.0,
                high_cardinality_warning: false,
                rows_checked: 5_000_000,
                key_column: None,
                key_values_checked: None,
                duplicate_rows_approximate: true,
                duplicate_rows_exact_limit: None,
            }),
            ..QualityMetrics::default()
        };
        let report = ProfileReport::new(
            source(),
            vec![],
            ExecutionMetadata::new(5_000_000, 0, 10),
            Some(QualityAssessment::exact(metrics)),
        );

        let result = report.findings();

        assert!(
            !codes(&result).contains(&(FindingCode::DuplicateRows, None)),
            "an HLL estimate reported as a duplicate: {:?}",
            result.findings
        );
        assert!(result.not_evaluated.contains(&UnevaluatedRule {
            code: FindingCode::DuplicateRows,
            reason: NotEvaluatedReason::Estimated,
            columns: vec![],
        }));
    }

    #[test]
    fn the_threshold_is_compared_at_the_serialized_precision() {
        // 4,999 of 25,000 is 19.996%, which the report serializes as 20.0. A
        // full-precision comparison would miss it here and report it once the
        // report was read back from its document.
        let report = report(
            vec![column("near", 25_000, 4_999)],
            ExecutionMetadata::new(25_000, 1, 10),
        );

        let finding = report
            .findings()
            .findings
            .into_iter()
            .find(|finding| finding.code == FindingCode::NullHeavy)
            .expect("reported at the rounded value");
        assert_eq!(
            finding.evidence["null_percentage"],
            EvidenceValue::Percentage(20.0)
        );
    }

    #[test]
    fn only_confident_personal_or_financial_patterns_are_sensitive() {
        let mut profile = column("mixed_ids", 10, 0);
        profile.patterns = Some(vec![
            pattern("UUID", PatternCategory::Identifier, 0.9),
            pattern("SSN (US)", PatternCategory::Identifier, 0.9),
            pattern("IBAN", PatternCategory::Financial, 0.4),
            pattern("Email", PatternCategory::Contact, 0.8),
            pattern("IPv4", PatternCategory::Network, 0.9),
        ]);
        let result = report(vec![profile], ExecutionMetadata::new(10, 1, 10)).findings();

        let reported: Vec<_> = result
            .findings
            .iter()
            .filter(|finding| finding.code == FindingCode::SensitivePattern)
            .map(|finding| finding.evidence["pattern"].clone())
            .collect();
        // Ordered by pattern name within the column, not by detection order.
        assert_eq!(
            reported,
            vec![
                EvidenceValue::Text("Email".to_string()),
                EvidenceValue::Text("SSN (US)".to_string()),
            ]
        );
    }

    #[test]
    fn evidence_holds_the_value_it_serializes() {
        // A percentage kept at full precision in memory and rounded only on
        // the way out would make two findings that serialize identically
        // compare unequal, depending on who wrote the document they came from.
        let mut profile = column("email", 3, 0);
        profile.type_homogeneity = Some(TypeHomogeneity {
            numeric: 1,
            date: 0,
            boolean: 0,
            text: 2,
        });
        let mut email = pattern("Email", PatternCategory::Contact, 0.8);
        email.match_percentage = 200.0 / 3.0;
        profile.patterns = Some(vec![email]);
        let result = report(vec![profile], ExecutionMetadata::new(3, 1, 10)).findings();

        let serialized: FindingsResult = {
            let value = serde_json::to_value(&result).expect("serializes");
            let mut rebuilt = result.clone();
            for (finding, document) in rebuilt
                .findings
                .iter_mut()
                .zip(value["findings"].as_array().unwrap())
            {
                for (name, evidence) in finding.evidence.iter_mut() {
                    if let EvidenceValue::Percentage(_) = evidence {
                        *evidence =
                            EvidenceValue::Percentage(document["evidence"][name].as_f64().unwrap());
                    }
                }
            }
            rebuilt
        };
        assert_eq!(result, serialized);
    }

    fn quality_report(future_dates: usize, confidence: MetricConfidence) -> ProfileReport {
        let metrics = QualityMetrics {
            uniqueness: Some(UniquenessMetrics {
                duplicate_rows: 0,
                key_uniqueness: 100.0,
                high_cardinality_warning: false,
                rows_checked: 60_000,
                key_column: None,
                key_values_checked: None,
                duplicate_rows_approximate: false,
                duplicate_rows_exact_limit: None,
            }),
            timeliness: Some(TimelinessMetrics {
                future_dates_count: future_dates,
                stale_data_ratio: 0.0,
                temporal_violations: 0,
                invalid_date_values: 0,
                date_values_checked: 10_000,
                temporal_pairs_checked: 10_000,
                clock: None,
            }),
            ..QualityMetrics::default()
        };
        ProfileReport::new(
            source(),
            vec![],
            ExecutionMetadata::new(60_000, 0, 10),
            Some(QualityAssessment::new(metrics, confidence)),
        )
    }

    fn skipped(result: &FindingsResult, code: FindingCode) -> Option<&NotEvaluatedReason> {
        result
            .not_evaluated
            .iter()
            .find(|entry| entry.code == code)
            .map(|entry| &entry.reason)
    }

    #[test]
    fn a_zero_count_from_the_quality_sample_rules_nothing_out() {
        // The ordinary large-file shape: every row scanned, timeliness computed
        // over the retained reservoir, duplicates over the full stream.
        let sampled = || MetricConfidence::Mixed {
            exact_dimensions: vec!["duplicate_rows".to_string()],
            sampled_dimensions: vec!["timeliness".to_string()],
            sample_size: 10_000,
        };

        let clean = quality_report(0, sampled()).findings();
        assert_eq!(
            skipped(&clean, FindingCode::FutureDates),
            Some(&NotEvaluatedReason::Sampled)
        );
        assert_eq!(
            skipped(&clean, FindingCode::TemporalOrderViolations),
            Some(&NotEvaluatedReason::Sampled)
        );
        // Counted over every row, so its zero is a clean result.
        assert_eq!(skipped(&clean, FindingCode::DuplicateRows), None);

        // A witnessed future date stays witnessed, sample or not.
        let witnessed = quality_report(3, sampled()).findings();
        assert!(codes(&witnessed).contains(&(FindingCode::FutureDates, None)));
        assert_eq!(skipped(&witnessed, FindingCode::FutureDates), None);
    }

    #[test]
    fn unrecorded_coverage_does_not_read_as_clean() {
        let result = quality_report(0, MetricConfidence::Unrecorded).findings();
        for code in [
            FindingCode::DuplicateRows,
            FindingCode::FutureDates,
            FindingCode::TemporalOrderViolations,
        ] {
            assert_eq!(
                skipped(&result, code),
                Some(&NotEvaluatedReason::Unrecorded),
                "{code}"
            );
        }
    }

    #[test]
    fn a_report_from_before_ragged_counting_does_not_read_as_clean() {
        let mut legacy = report(vec![], ExecutionMetadata::new(5, 0, 10));
        legacy.schema_version = 0;
        assert_eq!(
            skipped(&legacy.findings(), FindingCode::RaggedRows),
            Some(&NotEvaluatedReason::Unrecorded)
        );

        let current = report(vec![], ExecutionMetadata::new(5, 0, 10)).findings();
        assert_eq!(skipped(&current, FindingCode::RaggedRows), None);
    }

    /// A CSV report from before the CSV source metadata cannot say whether
    /// detection was clear, so both delimiter rules are listed as unrecorded
    /// rather than passed. A recorded clear detection, a configured delimiter
    /// and a non-CSV source list neither (#1100).
    #[test]
    fn a_csv_report_without_source_metadata_does_not_read_as_a_clear_delimiter() {
        let rules = [FindingCode::DelimiterAmbiguous, FindingCode::HeaderNotSplit];
        let with_source = |data_source: DataSource| {
            ProfileReport::new(data_source, vec![], ExecutionMetadata::new(5, 0, 10), None)
                .findings()
        };
        let mut wrong = Vec::new();

        let legacy = with_source(source());
        for code in rules {
            if skipped(&legacy, code) != Some(&NotEvaluatedReason::Unrecorded) {
                wrong.push(format!("legacy csv: {code} not listed as unrecorded"));
            }
        }

        let json = DataSource::File {
            path: "drop.json".to_string(),
            format: FileFormat::Json,
            size_bytes: 256,
            modified_at: None,
            parquet_metadata: None,
            csv_metadata: None,
        };
        let recorded = [
            (
                "clear detection",
                source().with_csv_metadata(CsvSourceMetadata::detected(DelimiterDetection::new(
                    b',', 3, None,
                ))),
            ),
            (
                "configured",
                source().with_csv_metadata(CsvSourceMetadata::configured(b';')),
            ),
            ("json", json),
        ];
        for (case, data_source) in recorded {
            let result = with_source(data_source);
            for code in rules {
                if skipped(&result, code).is_some()
                    || result.findings.iter().any(|finding| finding.code == code)
                {
                    wrong.push(format!("{case}: {code} skipped or reported"));
                }
            }
        }
        assert!(wrong.is_empty(), "{}", wrong.join("\n"));
    }

    /// A classified column of `numeric` numbers and `text` text values,
    /// `locale` of them locale-formatted numbers.
    fn classified(name: &str, numeric: usize, text: usize, locale: Option<usize>) -> ColumnProfile {
        let mut profile = column(name, numeric + text, 0);
        profile.type_homogeneity = Some(TypeHomogeneity {
            numeric,
            date: 0,
            boolean: 0,
            text,
        });
        profile.locale_number_count = locale;
        profile
    }

    fn locale_number_columns(result: &FindingsResult) -> Vec<&str> {
        result
            .findings
            .iter()
            .filter(|finding| finding.code == FindingCode::LocaleNumbers)
            .filter_map(|finding| finding.column.as_deref())
            .collect()
    }

    #[test]
    fn locale_numbers_are_reported_from_half_of_the_text_values() {
        let result = report(
            vec![
                classified("half", 0, 4, Some(2)),
                classified("under_half", 0, 5, Some(2)),
                // A float column whose decimal-comma values were invalid.
                classified("float_with_commas", 9, 1, Some(1)),
                classified("clean", 0, 4, Some(0)),
            ],
            ExecutionMetadata::new(10, 0, 10),
        )
        .findings();

        assert_eq!(
            locale_number_columns(&result),
            ["half", "float_with_commas"]
        );
        let half = &result.findings[0];
        assert_eq!(half.code, FindingCode::LocaleNumbers);
        assert_eq!(
            half.evidence,
            BTreeMap::from([
                ("classified_count".to_string(), EvidenceValue::Count(4)),
                ("locale_number_count".to_string(), EvidenceValue::Count(2)),
                ("non_null_count".to_string(), EvidenceValue::Count(4)),
                ("text_count".to_string(), EvidenceValue::Count(4)),
            ])
        );
        assert_eq!(skipped(&result, FindingCode::LocaleNumbers), None);
    }

    #[test]
    fn an_identifier_column_is_exempt_from_locale_numbers() {
        let mut codes = classified("codes", 0, 4, Some(4));
        codes.data_type = DataType::Identifier;

        let result = report(vec![codes], ExecutionMetadata::new(4, 0, 10)).findings();

        assert!(locale_number_columns(&result).is_empty());
        assert_eq!(skipped(&result, FindingCode::LocaleNumbers), None);
    }

    #[test]
    fn a_report_from_before_locale_number_counting_does_not_read_as_clean() {
        // Classified by a release that did not count locale numbers yet.
        let result = report(
            vec![classified("legacy", 0, 4, None)],
            ExecutionMetadata::new(4, 0, 10),
        )
        .findings();
        assert_eq!(
            skipped(&result, FindingCode::LocaleNumbers),
            Some(&NotEvaluatedReason::Unrecorded)
        );

        // Not classified at all: nothing was computed to count from.
        let result = report(
            vec![column("unclassified", 4, 0)],
            ExecutionMetadata::new(4, 0, 10),
        )
        .findings();
        assert_eq!(
            skipped(&result, FindingCode::LocaleNumbers),
            Some(&NotEvaluatedReason::NotComputed)
        );
    }

    #[test]
    fn thresholds_apply_at_the_precision_they_are_reported_at() {
        // 20.004 is applied as 20.00, so a 20.00% column is reported and the
        // evidence names the threshold that was compared.
        let result = FindingPolicy::new()
            .null_heavy_percentage(20.004)
            .evaluate(&report(
                vec![column("fifth", 5, 1)],
                ExecutionMetadata::new(5, 1, 10),
            ))
            .expect("in range");
        let finding = result
            .findings
            .iter()
            .find(|finding| finding.code == FindingCode::NullHeavy)
            .expect("reported at the applied threshold");
        assert_eq!(
            finding.evidence["threshold"],
            EvidenceValue::Percentage(20.0)
        );

        // A threshold that rounds to zero would report every column.
        for value in [1e-9, 0.004] {
            assert!(
                FindingPolicy::new()
                    .null_heavy_percentage(value)
                    .validate()
                    .is_err(),
                "{value} was accepted"
            );
        }
        assert!(
            FindingPolicy::new()
                .null_heavy_percentage(0.005)
                .validate()
                .is_ok()
        );
    }

    #[test]
    fn a_nested_column_is_assessed_by_its_counts_only() {
        // Containers carry counts and nothing else (#637); the value-level
        // rules say they did not look rather than reporting a clean column.
        let mut nested = column("payload", 4, 4);
        nested.data_type = DataType::Nested;
        let result = report(vec![nested], ExecutionMetadata::new(4, 1, 10)).findings();

        assert_eq!(
            codes(&result),
            vec![(FindingCode::AllNull, Some("payload"))]
        );
        for code in [
            FindingCode::ConstantColumn,
            FindingCode::MixedTypes,
            FindingCode::SensitivePattern,
        ] {
            assert!(
                result.not_evaluated.contains(&UnevaluatedRule {
                    code,
                    reason: NotEvaluatedReason::NotComputed,
                    columns: vec!["payload".to_string()],
                }),
                "{code} was not listed: {:?}",
                result.not_evaluated
            );
        }
    }

    #[test]
    fn a_sampled_scan_names_its_reason_and_truncation_wins() {
        let sampled = report(
            vec![],
            ExecutionMetadata::new(100, 0, 10).with_sampling(0.1),
        );
        let both = report(
            vec![],
            ExecutionMetadata::new(100, 0, 10)
                .with_sampling(0.1)
                .with_truncation(TruncationReason::MaxRows(100)),
        );

        for (report, expected) in [(sampled, "sampled"), (both, "truncated")] {
            let finding = report
                .findings()
                .findings
                .into_iter()
                .find(|finding| finding.code == FindingCode::PartialScan)
                .expect("a partial scan is disclosed");
            assert_eq!(
                finding.evidence["reason"],
                EvidenceValue::Text(expected.to_string())
            );
        }
    }

    /// A report whose declared key `order_id` holds ten non-null values and
    /// one null, `unique` of them distinct.
    fn key_report(
        unique: usize,
        approximate: Option<bool>,
        confidence: MetricConfidence,
    ) -> ProfileReport {
        let values = 10;
        let mut key = column("order_id", values + 1, 1);
        key.data_type = DataType::Identifier;
        key.unique_count = Some(unique);
        key.unique_count_is_approximate = approximate;
        let metrics = QualityMetrics {
            uniqueness: Some(UniquenessMetrics {
                duplicate_rows: 0,
                key_uniqueness: unique as f64 / values as f64 * 100.0,
                high_cardinality_warning: false,
                rows_checked: values + 1,
                key_column: Some("order_id".to_string()),
                key_values_checked: Some(values),
                duplicate_rows_approximate: false,
                duplicate_rows_exact_limit: None,
            }),
            ..QualityMetrics::default()
        };
        ProfileReport::new(
            source(),
            vec![column("amount", values + 1, 0), key],
            ExecutionMetadata::new(values + 1, 0, 10),
            Some(QualityAssessment::new(metrics, confidence)),
        )
    }

    fn key_sampled() -> MetricConfidence {
        MetricConfidence::Mixed {
            exact_dimensions: vec!["duplicate_rows".to_string()],
            sampled_dimensions: vec!["key_uniqueness".to_string()],
            sample_size: 10,
        }
    }

    /// What the key rule did: its finding's evidence, or its skip reason.
    fn key_outcome(result: &FindingsResult) -> String {
        let found: Vec<_> = result
            .findings
            .iter()
            .filter(|finding| finding.code == FindingCode::KeyDuplicates)
            .collect();
        match (
            found.as_slice(),
            skipped(result, FindingCode::KeyDuplicates),
        ) {
            ([finding], None) => format!(
                "found on {:?}: {}",
                finding.column,
                serde_json::to_string(&finding.evidence).unwrap()
            ),
            ([], Some(reason)) => format!("skipped: {}", serde_json::to_string(reason).unwrap()),
            ([], None) => "clean".to_string(),
            _ => format!("inconsistent: {found:?}"),
        }
    }

    #[test]
    fn a_repeated_declared_key_is_reported_only_from_an_exact_count() {
        let found = r#"found on Some("order_id"): {"key_column_source":"declared","key_values_checked":10,"repeated_values":2}"#;
        let skip = |reason: &str| format!(r#"skipped: {{"reason":"{reason}"}}"#);
        let mut legacy_key = key_report(8, Some(false), MetricConfidence::Exact);
        legacy_key
            .metric_semantics
            .as_mut()
            .expect("a current report records its semantics")
            .key_column_source = None;
        let mut no_key = key_report(8, Some(false), MetricConfidence::Exact);
        no_key
            .quality
            .as_mut()
            .and_then(|quality| quality.metrics.uniqueness.as_mut())
            .expect("uniqueness assessed")
            .key_column = None;
        let mut all_null_key = key_report(0, Some(false), MetricConfidence::Exact);
        all_null_key.column_profiles[1].null_count = 11;

        let cases = [
            (
                "exact count with repeats",
                key_report(8, Some(false), MetricConfidence::Exact),
                found.to_string(),
            ),
            (
                // One null and ten distinct values: a null is not a repeat.
                "exact count, distinct, one null",
                key_report(10, Some(false), MetricConfidence::Exact),
                "clean".to_string(),
            ),
            (
                "estimated distinct count",
                key_report(8, Some(true), MetricConfidence::Exact),
                skip("estimated"),
            ),
            (
                "key component filed as sampled",
                key_report(8, Some(false), key_sampled()),
                skip("estimated"),
            ),
            (
                "exactness recorded on the column only",
                key_report(8, Some(false), MetricConfidence::Unrecorded),
                found.to_string(),
            ),
            (
                "exactness recorded in the coverage only",
                key_report(8, None, MetricConfidence::Exact),
                found.to_string(),
            ),
            (
                "exactness recorded nowhere",
                key_report(8, None, MetricConfidence::Unrecorded),
                skip("unrecorded"),
            ),
            ("key chosen before 0.13", legacy_key, skip("unrecorded")),
            ("no key measured", no_key, skip("not_assessed")),
            ("key without values", all_null_key, skip("no_values")),
        ];
        let mut wrong = Vec::new();
        for (case, report, expected) in cases {
            let outcome = key_outcome(&report.findings());
            if outcome != expected {
                wrong.push(format!("{case}: {outcome}, expected {expected}"));
            }
        }
        assert!(wrong.is_empty(), "{}", wrong.join("\n"));
    }

    #[test]
    fn warnings_sort_before_info_then_by_code_then_column_position() {
        let mut constant = column("z_constant", 5, 0);
        constant.unique_count = Some(1);
        let mut mixed = column("a_mixed", 5, 0);
        mixed.type_homogeneity = Some(TypeHomogeneity {
            numeric: 3,
            date: 0,
            boolean: 0,
            text: 2,
        });
        let result = report(
            vec![
                constant,
                column("sparse", 5, 4),
                mixed,
                column("empty", 5, 5),
            ],
            ExecutionMetadata::new(5, 4, 10).with_ragged_row_count(1),
        )
        .findings();

        assert_eq!(
            codes(&result),
            vec![
                (FindingCode::AllNull, Some("empty")),
                (FindingCode::MixedTypes, Some("a_mixed")),
                (FindingCode::NullHeavy, Some("sparse")),
                (FindingCode::RaggedRows, None),
                (FindingCode::ConstantColumn, Some("z_constant")),
            ]
        );
        assert!(
            result
                .findings
                .iter()
                .all(|finding| finding.severity == finding.code.severity())
        );
    }
}