dataprof-metrics 0.10.0

Metrics and statistical analysis engine 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
//! Uniqueness Dimension (ISO 8000-110)
//!
//! Measures duplicate detection and key uniqueness.
//! Key metrics: duplicate rows, key uniqueness, high cardinality warnings.

use super::utils::is_likely_id_column;
use crate::core::config::IsoQualityConfig;
use crate::core::errors::DataProfilerError;
use crate::quality::RowDuplicateSummary;
use crate::types::ColumnProfile;
use std::collections::{HashMap, HashSet};

/// Uniqueness metrics container
#[derive(Debug)]
pub(crate) struct UniquenessMetrics {
    pub duplicate_rows: usize,
    pub key_uniqueness: f64,
    pub high_cardinality_warning: bool,
    pub rows_checked: usize,
    pub key_column: Option<String>,
    pub duplicate_rows_approximate: bool,
}

/// Calculator for uniqueness dimension metrics
pub(crate) struct UniquenessCalculator<'a> {
    thresholds: &'a IsoQualityConfig,
}

impl<'a> UniquenessCalculator<'a> {
    pub fn new(thresholds: &'a IsoQualityConfig) -> Self {
        Self { thresholds }
    }

    /// Calculate uniqueness dimension metrics.
    ///
    /// `row_duplicates` carries full-stream duplicate counts from an
    /// engine's row tracker; when present it supersedes the sample-based
    /// duplicate scan (which only works on provably row-aligned samples).
    pub fn calculate(
        &self,
        data: &HashMap<String, Vec<String>>,
        column_profiles: &[ColumnProfile],
        total_rows: usize,
        identifier_columns: &[String],
        row_duplicates: Option<RowDuplicateSummary>,
    ) -> Result<UniquenessMetrics, DataProfilerError> {
        let (duplicate_rows, rows_checked, duplicate_rows_approximate) = match row_duplicates {
            Some(summary) if summary.rows_checked > 0 => (
                summary.duplicate_rows,
                summary.rows_checked,
                summary.approximate,
            ),
            _ => {
                let (duplicates, rows) = Self::count_exact_duplicate_rows(data, column_profiles)?;
                (duplicates, rows, false)
            }
        };
        let (key_uniqueness, key_column) =
            Self::calculate_key_uniqueness(column_profiles, identifier_columns)?;
        let high_cardinality_warning =
            self.check_high_cardinality(column_profiles, total_rows, identifier_columns);

        Ok(UniquenessMetrics {
            duplicate_rows,
            key_uniqueness,
            high_cardinality_warning,
            rows_checked,
            key_column,
            duplicate_rows_approximate,
        })
    }

    /// Count exact duplicate rows; returns `(duplicates, rows_scanned)`.
    ///
    /// The per-column sample arrays are only row-aligned when every column
    /// kept every value: null-like values never enter the reservoir, and
    /// past its capacity each column is evicted independently. Comparing
    /// misaligned columns fabricates duplicates out of unrelated values, so
    /// when alignment cannot be proven (unequal lengths, or lengths that
    /// don't match the known row count) this returns `(0, 0)` — "not
    /// assessed" — rather than a plausible-looking wrong count.
    fn count_exact_duplicate_rows(
        data: &HashMap<String, Vec<String>>,
        column_profiles: &[ColumnProfile],
    ) -> Result<(usize, usize), DataProfilerError> {
        if data.is_empty() {
            return Ok((0, 0));
        }

        let total_rows = data.values().next().map(|v| v.len()).ok_or_else(|| {
            DataProfilerError::MetricsCalculationError {
                message: "No data columns found".to_string(),
            }
        })?;

        let aligned_lengths = data.values().all(|column| column.len() == total_rows);
        let matches_known_row_count = match column_profiles.first() {
            Some(profile) => profile.total_count == total_rows,
            None => true,
        };
        if !aligned_lengths || !matches_known_row_count {
            return Ok((0, 0));
        }

        let column_names: Vec<&String> = data.keys().collect();

        let mut row_signatures = HashSet::new();
        let mut duplicates = 0;

        for row_idx in 0..total_rows {
            let row_signature: Vec<&String> = column_names
                .iter()
                .filter_map(|col_name| data.get(*col_name)?.get(row_idx))
                .collect();

            if !row_signatures.insert(row_signature) {
                duplicates += 1;
            }
        }

        Ok((duplicates, total_rows))
    }

    /// Calculate key uniqueness percentage and the column it describes.
    ///
    /// Returns `(100.0, None)` when no key column was identified or its
    /// unique count is unavailable — the caller treats `None` as "no key
    /// signal" rather than "perfect key".
    fn calculate_key_uniqueness(
        column_profiles: &[ColumnProfile],
        identifier_columns: &[String],
    ) -> Result<(f64, Option<String>), DataProfilerError> {
        // Explicit semantic hints take precedence and retain user-supplied
        // order. Fall back to the conservative name heuristic when absent.
        let key_column = identifier_columns
            .iter()
            .find_map(|name| {
                column_profiles
                    .iter()
                    .find(|profile| profile.name == name.as_str())
            })
            .or_else(|| {
                column_profiles
                    .iter()
                    .find(|profile| is_likely_id_column(&profile.name))
            });

        if let Some(key_col) = key_column {
            if let Some(unique_count) = key_col.unique_count {
                if key_col.total_count == 0 {
                    Ok((100.0, None))
                } else {
                    Ok((
                        (unique_count as f64 / key_col.total_count as f64) * 100.0,
                        Some(key_col.name.clone()),
                    ))
                }
            } else {
                // unique_count unavailable: no evidence either way
                Ok((100.0, None))
            }
        } else {
            // No key column found: no key signal
            Ok((100.0, None))
        }
    }

    /// Check for high cardinality warning
    /// Uses configurable high_cardinality_threshold from ISO thresholds (default: 95%)
    fn check_high_cardinality(
        &self,
        column_profiles: &[ColumnProfile],
        total_rows: usize,
        identifier_columns: &[String],
    ) -> bool {
        if total_rows == 0 {
            return false;
        }

        let threshold = self.thresholds.high_cardinality_threshold / 100.0;

        column_profiles.iter().any(|profile| {
            if let Some(unique_count) = profile.unique_count {
                let cardinality_ratio = unique_count as f64 / total_rows as f64;
                // Flag if a column exceeds threshold (excluding obvious ID columns)
                let is_identifier = identifier_columns.contains(&profile.name)
                    || is_likely_id_column(&profile.name);
                cardinality_ratio > threshold && !is_identifier
            } else {
                false
            }
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::types::{ColumnStats, DataType};

    fn profile(name: &str, total: usize, unique: Option<usize>) -> ColumnProfile {
        ColumnProfile {
            name: name.to_string(),
            data_type: DataType::String,
            null_count: 0,
            total_count: total,
            unique_count: unique,
            unique_count_is_approximate: unique.map(|_| false),
            invalid_count: None,
            stats: ColumnStats::None,
            patterns: Some(vec![]),
        }
    }

    fn column(values: &[&str]) -> Vec<String> {
        values.iter().map(|v| v.to_string()).collect()
    }

    fn cardinality_column(total: usize, unique: usize) -> Vec<String> {
        (0..total).map(|i| (i % unique).to_string()).collect()
    }

    #[test]
    fn test_uniqueness_case_table() {
        struct Case {
            name: &'static str,
            data: HashMap<String, Vec<String>>,
            profiles: Vec<ColumnProfile>,
            total_rows: usize,
            duplicate_rows: usize,
            rows_checked: usize,
            key_uniqueness: f64,
            key_column: Option<&'static str>,
            high_cardinality_warning: bool,
        }

        let cases = [
            Case {
                name: "all rows distinct",
                data: HashMap::from([("city".to_string(), column(&["a", "b", "c"]))]),
                profiles: vec![profile("city", 3, Some(3))],
                total_rows: 3,
                duplicate_rows: 0,
                rows_checked: 3,
                key_uniqueness: 100.0,
                key_column: None,
                high_cardinality_warning: true, // 3/3 unique, non-id column
            },
            Case {
                name: "one exact duplicate row",
                data: HashMap::from([("city".to_string(), column(&["a", "b", "a"]))]),
                profiles: vec![profile("city", 3, Some(2))],
                total_rows: 3,
                duplicate_rows: 1,
                rows_checked: 3,
                key_uniqueness: 100.0,
                key_column: None,
                high_cardinality_warning: false,
            },
            Case {
                name: "all rows identical",
                data: HashMap::from([("city".to_string(), column(&["a", "a", "a", "a"]))]),
                profiles: vec![profile("city", 4, Some(1))],
                total_rows: 4,
                duplicate_rows: 3,
                rows_checked: 4,
                key_uniqueness: 100.0,
                key_column: None,
                high_cardinality_warning: false,
            },
            Case {
                name: "fully unique identifier column",
                data: HashMap::from([("user_id".to_string(), column(&["1", "2", "3"]))]),
                profiles: vec![profile("user_id", 3, Some(3))],
                total_rows: 3,
                duplicate_rows: 0,
                rows_checked: 3,
                key_uniqueness: 100.0,
                key_column: Some("user_id"),
                high_cardinality_warning: false, // id columns are excluded
            },
            Case {
                name: "partially unique identifier column",
                data: HashMap::from([("user_id".to_string(), column(&["1", "2", "2", "3"]))]),
                profiles: vec![profile("user_id", 4, Some(3))],
                total_rows: 4,
                duplicate_rows: 1,
                rows_checked: 4,
                key_uniqueness: 75.0,
                key_column: Some("user_id"),
                high_cardinality_warning: false,
            },
            Case {
                name: "high cardinality just below threshold",
                data: HashMap::from([("note".to_string(), cardinality_column(100, 94))]),
                profiles: vec![profile("note", 100, Some(94))],
                total_rows: 100,
                duplicate_rows: 6,
                rows_checked: 100,
                key_uniqueness: 100.0,
                key_column: None,
                high_cardinality_warning: false,
            },
            Case {
                name: "high cardinality at threshold (not above)",
                data: HashMap::from([("note".to_string(), cardinality_column(100, 95))]),
                profiles: vec![profile("note", 100, Some(95))],
                total_rows: 100,
                duplicate_rows: 5,
                rows_checked: 100,
                key_uniqueness: 100.0,
                key_column: None,
                // 95/100 == threshold exactly; the check requires strictly above
                high_cardinality_warning: false,
            },
            Case {
                name: "high cardinality above threshold",
                data: HashMap::from([("note".to_string(), cardinality_column(100, 96))]),
                profiles: vec![profile("note", 100, Some(96))],
                total_rows: 100,
                duplicate_rows: 4,
                rows_checked: 100,
                key_uniqueness: 100.0,
                key_column: None,
                high_cardinality_warning: true,
            },
            Case {
                name: "empty input",
                data: HashMap::new(),
                profiles: vec![],
                total_rows: 0,
                duplicate_rows: 0,
                rows_checked: 0,
                key_uniqueness: 100.0,
                key_column: None,
                high_cardinality_warning: false,
            },
            Case {
                name: "single row",
                data: HashMap::from([("city".to_string(), column(&["a"]))]),
                profiles: vec![profile("city", 1, Some(1))],
                total_rows: 1,
                duplicate_rows: 0,
                rows_checked: 1,
                key_uniqueness: 100.0,
                key_column: None,
                high_cardinality_warning: true, // 1/1 unique, non-id column
            },
        ];

        let thresholds = IsoQualityConfig::default();
        let calculator = UniquenessCalculator::new(&thresholds);

        for case in cases {
            let metrics = calculator
                .calculate(&case.data, &case.profiles, case.total_rows, &[], None)
                .unwrap_or_else(|e| panic!("{}: calculation failed: {e}", case.name));

            assert_eq!(
                metrics.duplicate_rows, case.duplicate_rows,
                "{}: duplicate_rows",
                case.name
            );
            assert_eq!(
                metrics.rows_checked, case.rows_checked,
                "{}: rows_checked",
                case.name
            );
            assert!(
                (metrics.key_uniqueness - case.key_uniqueness).abs() < 0.01,
                "{}: key_uniqueness {} != {}",
                case.name,
                metrics.key_uniqueness,
                case.key_uniqueness
            );
            assert_eq!(
                metrics.key_column.as_deref(),
                case.key_column,
                "{}: key_column",
                case.name
            );
            assert_eq!(
                metrics.high_cardinality_warning, case.high_cardinality_warning,
                "{}: high_cardinality_warning",
                case.name
            );
            assert!(
                !metrics.duplicate_rows_approximate,
                "{}: sample-scan duplicates are never approximate",
                case.name
            );
        }
    }

    #[test]
    fn test_row_duplicate_summary_supersedes_sample_scan() {
        let thresholds = IsoQualityConfig::default();
        let calculator = UniquenessCalculator::new(&thresholds);
        // Misaligned sample (2 vs 3 values): the scan alone would refuse it.
        let data = HashMap::from([
            ("a".to_string(), column(&["x", "y"])),
            ("b".to_string(), column(&["1", "2", "3"])),
        ]);
        let profiles = vec![profile("a", 1000, Some(2)), profile("b", 1000, Some(3))];

        let without = calculator
            .calculate(&data, &profiles, 1000, &[], None)
            .expect("scan-only path");
        assert_eq!(without.rows_checked, 0, "misaligned sample must not scan");

        let summary = RowDuplicateSummary {
            duplicate_rows: 40,
            rows_checked: 1000,
            approximate: true,
        };
        let with = calculator
            .calculate(&data, &profiles, 1000, &[], Some(summary))
            .expect("summary path");
        assert_eq!(with.duplicate_rows, 40);
        assert_eq!(with.rows_checked, 1000);
        assert!(with.duplicate_rows_approximate);
    }

    #[test]
    fn test_misaligned_sample_lengths_are_not_scanned() {
        let thresholds = IsoQualityConfig::default();
        let calculator = UniquenessCalculator::new(&thresholds);
        let data = HashMap::from([
            ("a".to_string(), column(&["x", "x", "x"])),
            ("b".to_string(), column(&["1", "1"])),
        ]);
        let profiles = vec![profile("a", 3, Some(1)), profile("b", 3, Some(1))];

        let metrics = calculator
            .calculate(&data, &profiles, 3, &[], None)
            .expect("metrics");
        assert_eq!(metrics.duplicate_rows, 0);
        assert_eq!(metrics.rows_checked, 0);
    }
}