dataprof-csv 0.13.1

CSV parsing and profiling support 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
use csv::ReaderBuilder;
use std::io::Read;
use std::path::Path;

use dataprof_core::{
    AnalysisOptions, ColumnProfile, DataProfilerError, DataSource, ExecutionMetadata, FileFormat,
    TruncationReason,
};
use dataprof_runtime::{
    ProfileReport, ReportAssembler, StreamingColumnCollection, profile_builder,
};

mod encoding;
mod memmap_reader;
mod quote_state;
#[doc(hidden)]
pub mod row_count;

pub use encoding::{EncodingDiagnostic, diagnose_non_utf8};
pub use memmap_reader::MemoryMappedCsvReader;
pub use quote_state::{QuoteOutcome, QuoteTrackingReader, unterminated_quote_error};

/// Configuration for CSV parsing and analysis.
///
/// Replaces the 7 separate `analyze_csv_*` functions with a single config struct.
/// Supports both file-based and reader-based (streaming) entry points.
#[derive(Debug, Clone)]
pub struct CsvParserConfig {
    /// Allow ragged rows (flexible field counts).
    pub flexible: bool,
    /// Custom delimiter (None = comma).
    pub delimiter: Option<u8>,
    /// Quote character.
    pub quote_char: u8,
    /// Trim whitespace from fields.
    pub trim_whitespace: bool,
    /// Whether the first row contains column headers.
    /// When `false`, columns are named `column_0`, `column_1`, etc.
    pub has_header: bool,
    /// Maximum rows to process (None = all rows).
    pub max_rows: Option<usize>,
}

impl Default for CsvParserConfig {
    fn default() -> Self {
        Self {
            flexible: true,
            delimiter: None,
            quote_char: b'"',
            trim_whitespace: false,
            has_header: true,
            max_rows: None,
        }
    }
}

impl CsvParserConfig {
    /// Create a strict config that rejects ragged rows.
    pub fn strict() -> Self {
        Self {
            flexible: false,
            ..Default::default()
        }
    }

    /// Set the delimiter.
    pub fn with_delimiter(mut self, delimiter: u8) -> Self {
        self.delimiter = Some(delimiter);
        self
    }

    /// Set whether the first row contains column headers.
    pub fn has_header(mut self, yes: bool) -> Self {
        self.has_header = yes;
        self
    }

    /// Set maximum number of rows to evaluate.
    pub fn max_rows(mut self, max: Option<usize>) -> Self {
        self.max_rows = max;
        self
    }
}

/// Count the fields in a record for a given delimiter, respecting quoted
/// sections, and how many of them at the end are empty.
///
/// Returns `(fields, trailing_empty)`. A field is empty when it decodes to
/// nothing, as the readers decode it: nothing stands between its delimiters,
/// or it is a quoted empty string `""`. Judging the raw text instead counted
/// `""` as content, and a row ending in `;""` disagreed with its header
/// (#1102).
fn count_fields(line: &str, delimiter: char) -> (usize, usize) {
    let decodes_empty = |field: &str| field.is_empty() || field == "\"\"";
    let mut fields = 1;
    let mut trailing_empty = 0;
    let mut field_start = 0;
    let mut in_quotes = false;
    let mut chars = line.char_indices().peekable();
    while let Some((index, ch)) = chars.next() {
        if ch == '"' {
            if chars.peek().map(|&(_, next)| next) == Some('"') {
                chars.next();
            } else {
                in_quotes = !in_quotes;
            }
        } else if ch == delimiter && !in_quotes {
            trailing_empty = if decodes_empty(&line[field_start..index]) {
                trailing_empty + 1
            } else {
                0
            };
            fields += 1;
            field_start = index + ch.len_utf8();
        }
    }
    trailing_empty = if decodes_empty(&line[field_start..]) {
        trailing_empty + 1
    } else {
        0
    };
    (fields, trailing_empty)
}

/// Whether a record reads at the header's width without losing or inventing a
/// value: it has exactly that many fields, or more whose extras are all empty,
/// so cutting it to the header drops nothing (#1096).
fn agrees_with_header(fields: usize, trailing_empty: usize, header_fields: usize) -> bool {
    fields == header_fields || (fields > header_fields && fields - trailing_empty <= header_fields)
}

/// Split raw CSV sample text into logical records, honoring quoted sections
/// so an embedded newline inside quotes does not start a new record.
///
/// A record ends at LF, CRLF or a lone CR outside quotes, as in the readers.
/// Splitting on LF alone read a CR-only file as one header record (#1097).
///
/// The returned flag is true when the final record was not terminated by a
/// newline outside quotes — i.e. the text ends mid-record (or exactly at a
/// record's end with no trailing newline, which a caller sampling a fixed
/// byte budget cannot distinguish from a cut).
fn split_sample_records(text: &str) -> (Vec<&str>, bool) {
    let bytes = text.as_bytes();
    let mut records = Vec::new();
    let mut in_quotes = false;
    let mut start = 0usize;
    let mut i = 0usize;
    while i < bytes.len() {
        match bytes[i] {
            b'"' => {
                if in_quotes && bytes.get(i + 1) == Some(&b'"') {
                    i += 1; // escaped quote inside a quoted section
                } else {
                    in_quotes = !in_quotes;
                }
            }
            b'\n' if !in_quotes => {
                let end = if i > start && bytes[i - 1] == b'\r' {
                    i - 1
                } else {
                    i
                };
                records.push(&text[start..end]);
                start = i + 1;
            }
            b'\r' if !in_quotes && bytes.get(i + 1) != Some(&b'\n') => {
                records.push(&text[start..i]);
                start = i + 1;
            }
            _ => {}
        }
        i += 1;
    }
    let last_unterminated = start < text.len();
    if last_unterminated {
        records.push(text[start..].trim_end_matches('\r'));
    }
    (records, last_unterminated)
}

/// Detect the most likely CSV delimiter by sampling up to 4 KB of data.
///
/// The sample is split into logical records with quote handling, so quoted
/// fields containing embedded newlines or delimiter characters do not skew
/// detection. Tests comma, semicolon, tab, and pipe over the first 5 non-blank
/// records, measured against the first one, the header: it sets the column
/// count, and the readers cut or pad a wider or narrower record to it, or
/// reject it under strict parsing. A delimiter that splits the header always
/// beats one that does not; then more records agreeing with the header win,
/// then more records at exactly the header's width, then a wider header, and
/// comma wins a tie. A record agrees when it reads at the header's width
/// without losing a value: the same field count, or extra fields that are all
/// empty, as an exporter's trailing delimiter leaves.
///
/// Judging by the data rows alone let a delimiter that never occurs in the
/// header win, and the readers then cut every row back to its first field: a
/// one-column file of `123|456` keys was profiled as `123` with fabricated
/// duplicates (#1092).
pub fn detect_delimiter<R: Read>(reader: R) -> std::io::Result<u8> {
    const SAMPLE_BYTES: u64 = 4096;
    let mut preamble = Vec::new();
    let mut take = reader.take(SAMPLE_BYTES);
    take.read_to_end(&mut preamble)?;
    let truncated = preamble.len() as u64 == SAMPLE_BYTES;

    // A UTF-8 BOM marks the encoding, as in the readers, and is no part of
    // the first record: left in, a BOM followed by a blank line was scored as
    // a one-field header and outvoted the real one.
    let sample = preamble.strip_prefix(b"\xEF\xBB\xBF").unwrap_or(&preamble);
    let preamble_str = String::from_utf8_lossy(sample);
    let (mut records, last_unterminated) = split_sample_records(&preamble_str);
    // A full sample buffer that ends mid-record leaves a fragment; never score
    // it. A sample ending on a clean newline keeps all its records.
    if truncated && last_unterminated && records.len() > 1 {
        records.pop();
    }

    // Blank records carry no delimiter signal: a header followed by trailing
    // blank lines must not outvote the true delimiter with single-field
    // "records" (gh #559).
    let scored: Vec<&str> = records
        .into_iter()
        .filter(|record| !record.trim().is_empty())
        .take(5)
        .collect();
    let Some((header, rows)) = scored.split_first() else {
        return Ok(b',');
    };

    let delimiters = *b",;\t|";
    let mut best_delimiter = b',';
    // (splits the header into >1 field, records agreeing with the header,
    //  records at exactly the header's width, header field count) — compared
    //  lexicographically, first wins ties. Exact width only breaks a tie: a
    //  candidate that reads every record as it stands beats one that needs
    //  trailing empty fields cut to agree.
    let mut best_score = (false, 0usize, 0usize, 0usize);

    for &delimiter in &delimiters {
        let (header_fields, _) = count_fields(header, delimiter as char);
        let (mut agreeing, mut exact) = (1, 1);
        for record in rows {
            let (fields, trailing_empty) = count_fields(record, delimiter as char);
            if agrees_with_header(fields, trailing_empty, header_fields) {
                agreeing += 1;
            }
            if fields == header_fields {
                exact += 1;
            }
        }

        let score = (header_fields > 1, agreeing, exact, header_fields);
        if score > best_score {
            best_score = score;
            best_delimiter = delimiter;
        }
    }

    Ok(best_delimiter)
}

/// Detect the most likely CSV delimiter from a file path.
pub fn detect_delimiter_from_path(path: &Path) -> std::io::Result<u8> {
    let file = std::fs::File::open(path)?;
    detect_delimiter(std::io::BufReader::new(file))
}

/// Analyze CSV data from any `Read` source using streaming statistics.
///
/// This is the primary generic entry point for both file-based and stream-based
/// CSV profiling. Data is processed incrementally through [`StreamingColumnCollection`]
/// with bounded memory, rather than accumulating all rows in a `HashMap`.
///
/// Returns `(column_profiles, streaming_stats, rows_read)`.
pub fn analyze_csv_from_reader<R: Read>(
    reader: R,
    config: &CsvParserConfig,
) -> Result<
    (
        Vec<ColumnProfile>,
        StreamingColumnCollection,
        usize,
        Vec<String>,
    ),
    DataProfilerError,
> {
    let scan = scan_csv(reader, config)?;
    let profiles = profile_builder::profiles_from_streaming(&scan.column_stats, false, false, None);

    Ok((
        profiles,
        scan.column_stats,
        scan.rows_read,
        scan.header_names,
    ))
}

/// What one pass over CSV input read, and how much of the input that was.
struct CsvScan {
    column_stats: StreamingColumnCollection,
    header_names: Vec<String>,
    rows_read: usize,
    /// Records whose field count differs from the header's, padded or cut to
    /// the header width before profiling.
    ragged_rows: usize,
    /// Set when `max_rows` stopped the pass with records left in the input.
    truncation: Option<TruncationReason>,
}

fn scan_csv<R: Read>(reader: R, config: &CsvParserConfig) -> Result<CsvScan, DataProfilerError> {
    let mut csv_builder = ReaderBuilder::new();
    csv_builder.has_headers(config.has_header);
    csv_builder.flexible(config.flexible);
    csv_builder.quote(config.quote_char);
    if config.trim_whitespace {
        csv_builder.trim(csv::Trim::All);
    }
    let mut actual_delimiter = config.delimiter;

    let boxed_reader: Box<dyn std::io::Read + '_> = if config.delimiter.is_none() {
        let mut preamble = Vec::new();
        let mut take = reader.take(4096);
        take.read_to_end(&mut preamble)?;

        let detected = detect_delimiter(std::io::Cursor::new(&preamble)).unwrap_or(b',');
        actual_delimiter = Some(detected);

        Box::new(std::io::Cursor::new(preamble).chain(take.into_inner()))
    } else {
        Box::new(reader)
    };

    if let Some(delimiter) = actual_delimiter {
        csv_builder.delimiter(delimiter);
    }

    let mut csv_reader = csv_builder.from_reader(boxed_reader);

    let header_names: Vec<String> = if config.has_header {
        let headers = csv_reader.headers()?;
        let names: Vec<String> = headers.iter().map(|header| header.to_string()).collect();
        // Reject duplicate headers before profiling: process_record keys on name,
        // so a repeat would merge two source columns into one inflated profile.
        dataprof_core::validate_unique_column_names(&names, "CSV header")?;
        names
    } else {
        let headers = csv_reader.headers()?;
        (0..headers.len())
            .map(|index| format!("column_{index}"))
            .collect()
    };

    let mut column_stats = StreamingColumnCollection::new();
    // A header declares the columns even when no data records follow it.
    column_stats.init_columns(&header_names);
    let mut rows_read = 0;
    let mut ragged_rows = 0;
    let mut truncation = None;

    for result in csv_reader.records() {
        if let Some(max_rows) = config.max_rows
            && rows_read >= max_rows
        {
            // A record past the cap means the cap, not the end of the input,
            // stopped the pass. That record is neither profiled nor judged.
            truncation = Some(TruncationReason::MaxRows(max_rows as u64));
            break;
        }

        let record = result?;
        let mut values: Vec<String> = record.iter().map(|value| value.to_string()).collect();

        let header_len = header_names.len();
        if values.len() != header_len {
            ragged_rows += 1;
        }
        if values.len() < header_len {
            values.resize(header_len, String::new());
        } else if values.len() > header_len {
            values.truncate(header_len);
        }

        column_stats.process_record(&header_names, values);
        rows_read += 1;
    }

    Ok(CsvScan {
        column_stats,
        header_names,
        rows_read,
        ragged_rows,
        truncation,
    })
}

/// Analyze a CSV file, returning a full [`ProfileReport`].
///
/// Opens the file, reads it as [`analyze_csv_from_reader`] does, and wraps the
/// result with file metadata, ISO 8000/25012 data quality metrics, and the
/// scan's own record: how many rows were ragged, and whether `max_rows` left
/// rows unread.
pub fn analyze_csv_file(
    file_path: &Path,
    config: &CsvParserConfig,
) -> Result<ProfileReport, DataProfilerError> {
    let metadata = std::fs::metadata(file_path).map_err(|error| map_io_error(file_path, error))?;
    dataprof_core::binary_input::reject_binary_input(file_path)?;
    let start = std::time::Instant::now();

    let file = std::fs::File::open(file_path).map_err(|error| map_io_error(file_path, error))?;
    let buf_reader = std::io::BufReader::new(file);

    let scan = scan_csv(buf_reader, config)?;
    let column_stats = &scan.column_stats;
    let column_profiles =
        profile_builder::profiles_from_streaming(column_stats, false, false, None);

    let file_source = DataSource::File {
        path: file_path.display().to_string(),
        format: FileFormat::Csv,
        size_bytes: metadata.len(),
        modified_at: None,
        parquet_metadata: None,
    };

    let sample_columns = profile_builder::quality_check_samples(column_stats);
    let scan_time_ms = start.elapsed().as_millis();
    let num_columns = column_profiles.len();

    let mut execution = ExecutionMetadata::new(scan.rows_read, num_columns, scan_time_ms)
        .with_engine("csv")
        .with_ragged_row_count(scan.ragged_rows);
    if let Some(reason) = scan.truncation {
        execution = execution.with_truncation(reason);
    }

    // This path takes no analysis options: it profiles with the defaults,
    // so it records them rather than leaving its settings unknown.
    Ok(ReportAssembler::new(file_source, execution)
        .with_analysis_options(&AnalysisOptions::default())
        .columns(column_profiles)
        .with_quality_data(sample_columns)
        .with_row_duplicates(column_stats.row_duplicate_summary())
        .with_row_completeness(column_stats.row_completeness_summary())
        .build())
}

fn map_io_error(file_path: &Path, error: std::io::Error) -> DataProfilerError {
    if error.kind() == std::io::ErrorKind::NotFound {
        DataProfilerError::FileNotFound {
            path: file_path.display().to_string(),
        }
    } else {
        DataProfilerError::from(error)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use dataprof_core::DataType;
    use std::io::{Cursor, Write};
    use tempfile::NamedTempFile;

    fn write_csv(content: &str) -> NamedTempFile {
        let mut file = NamedTempFile::new().unwrap();
        write!(file, "{}", content).unwrap();
        file.flush().unwrap();
        file
    }

    #[test]
    fn test_analyze_csv_from_reader_basic() {
        let data = b"name,age\nAlice,25\nBob,30\n";
        let cursor = Cursor::new(data.as_ref());
        let config = CsvParserConfig::default();

        let (profiles, _stats, rows, _) = analyze_csv_from_reader(cursor, &config).unwrap();
        assert_eq!(rows, 2);
        assert_eq!(profiles.len(), 2);

        let age = profiles
            .iter()
            .find(|profile| profile.name == "age")
            .unwrap();
        assert_eq!(age.total_count, 2);
        assert_eq!(age.data_type, DataType::Integer);
    }

    #[test]
    fn test_analyze_csv_from_reader_with_max_rows() {
        let data = b"val\n1\n2\n3\n4\n5\n";
        let cursor = Cursor::new(data.as_ref());
        let config = CsvParserConfig::default().max_rows(Some(3));

        let (_profiles, _stats, rows, _) = analyze_csv_from_reader(cursor, &config).unwrap();
        assert_eq!(rows, 3);
    }

    #[test]
    fn test_analyze_csv_from_reader_strict_rejects_ragged() {
        let data = b"a,b\n1,2\n3,4,5\n";
        let cursor = Cursor::new(data.as_ref());
        let config = CsvParserConfig::strict();

        assert!(analyze_csv_from_reader(cursor, &config).is_err());
    }

    #[test]
    fn test_analyze_csv_from_reader_flexible_handles_ragged() {
        let data = b"a,b\n1,2\n3,4,5\n6\n";
        let cursor = Cursor::new(data.as_ref());
        let config = CsvParserConfig::default();

        let (profiles, _stats, rows, _) = analyze_csv_from_reader(cursor, &config).unwrap();
        assert_eq!(rows, 3);
        assert!(!profiles.is_empty());
    }

    #[test]
    fn test_analyze_csv_from_reader_custom_delimiter() {
        let data = b"a;b\n1;2\n3;4\n";
        let cursor = Cursor::new(data.as_ref());
        let config = CsvParserConfig::default().with_delimiter(b';');

        let (profiles, _stats, rows, _) = analyze_csv_from_reader(cursor, &config).unwrap();
        assert_eq!(rows, 2);
        assert_eq!(profiles.len(), 2);
    }

    #[test]
    fn test_analyze_csv_file_returns_quality_report() {
        let csv = write_csv("x,y\n1,a\n2,b\n3,c\n");
        let config = CsvParserConfig::default();
        let report = analyze_csv_file(csv.path(), &config).unwrap();

        assert_eq!(report.column_profiles.len(), 2);
        assert_eq!(report.execution.rows_processed, 3);
        assert!(report.quality_score().unwrap() >= 0.0);
        assert!(report.quality_score().unwrap() <= 100.0);
    }

    #[test]
    fn test_csv_auto_detects_delimiter_from_reader() {
        let data = b"name;age\nAlice;25\nBob;30\n";
        let cursor = Cursor::new(data.as_ref());
        let config = CsvParserConfig::default();

        let (profiles, _stats, rows, _) = analyze_csv_from_reader(cursor, &config).unwrap();
        assert_eq!(rows, 2);
        assert_eq!(profiles.len(), 2);

        let age = profiles
            .iter()
            .find(|profile| profile.name == "age")
            .unwrap();
        assert_eq!(age.total_count, 2);
    }

    #[test]
    fn test_analyze_csv_basic() {
        let csv = write_csv("name,age\nAlice,25\nBob,30\n");
        let config = CsvParserConfig::default();
        let report = analyze_csv_file(csv.path(), &config).unwrap();
        let profiles = &report.column_profiles;
        assert_eq!(profiles.len(), 2);

        let names: Vec<&str> = profiles
            .iter()
            .map(|profile| profile.name.as_str())
            .collect();
        assert!(names.contains(&"name"));
        assert!(names.contains(&"age"));

        let age = profiles
            .iter()
            .find(|profile| profile.name == "age")
            .unwrap();
        assert_eq!(age.total_count, 2);
        assert_eq!(age.null_count, 0);
    }

    #[test]
    fn test_analyze_csv_with_nulls() {
        let csv = write_csv("name,age,email\nAlice,25,a@b.com\nBob,,\nCharlie,30,c@d.com\n");
        let config = CsvParserConfig::default();
        let report = analyze_csv_file(csv.path(), &config).unwrap();
        let profiles = &report.column_profiles;

        let age = profiles
            .iter()
            .find(|profile| profile.name == "age")
            .unwrap();
        assert_eq!(age.total_count, 3);
        assert_eq!(age.null_count, 1);

        let email = profiles
            .iter()
            .find(|profile| profile.name == "email")
            .unwrap();
        assert_eq!(email.null_count, 1);
    }

    #[test]
    fn test_analyze_csv_file_returns_quality_report_legacy() {
        let csv = write_csv("x,y\n1,a\n2,b\n3,c\n");
        let config = CsvParserConfig::default();
        let report = analyze_csv_file(csv.path(), &config).unwrap();

        assert_eq!(report.column_profiles.len(), 2);
        assert_eq!(report.execution.rows_processed, 3);
        assert_eq!(report.execution.columns_detected, 2);
        assert!(report.quality_score().unwrap() >= 0.0);
        assert!(report.quality_score().unwrap() <= 100.0);
    }

    #[test]
    fn test_analyze_csv_file_header_only_preserves_declared_columns() {
        let csv = write_csv("name,age\n");
        let config = CsvParserConfig::default();
        let report = analyze_csv_file(csv.path(), &config).unwrap();

        assert_eq!(
            report
                .column_profiles
                .iter()
                .map(|column| (column.name.as_str(), column.data_type.clone()))
                .collect::<Vec<_>>(),
            vec![("name", DataType::String), ("age", DataType::String)]
        );
        assert_eq!(report.execution.rows_processed, 0);
        assert!(report.quality.is_some());
    }

    #[test]
    fn test_analyze_csv_file_empty_file_has_no_columns() {
        let csv = write_csv("");
        let config = CsvParserConfig::default();
        let report = analyze_csv_file(csv.path(), &config).unwrap();

        assert!(report.column_profiles.is_empty());
        assert_eq!(report.execution.rows_processed, 0);
        assert_eq!(report.execution.columns_detected, 0);
        assert_eq!(
            report.quality_status,
            dataprof_runtime::QualityAnalysisStatus::Computed
        );
        assert!(report.quality.is_some());
        assert!(report.quality_score().is_none());
    }

    #[test]
    fn test_analyze_csv_with_max_rows_small_file() {
        let csv = write_csv("val\n1\n2\n3\n4\n5\n");
        let config = CsvParserConfig::default();
        let report = analyze_csv_file(csv.path(), &config).unwrap();

        assert_eq!(report.execution.rows_processed, 5);
        assert_eq!(report.execution.columns_detected, 1);
        assert!((report.execution.sampling_ratio.unwrap_or(1.0) - 1.0).abs() < 0.01);
    }

    #[test]
    fn a_row_cap_that_leaves_rows_unread_is_a_truncation() {
        let csv = write_csv("val\n1\n2\n3\n4\n5\n");
        let config = CsvParserConfig::default().max_rows(Some(2));
        let report = analyze_csv_file(csv.path(), &config).unwrap();

        assert_eq!(report.execution.rows_processed, 2);
        assert!(!report.execution.source_exhausted);
        assert_eq!(
            report.execution.truncation_reason,
            Some(TruncationReason::MaxRows(2))
        );
    }

    #[test]
    fn a_row_cap_at_the_last_row_is_a_complete_scan() {
        let csv = write_csv("val\n1\n2\n");
        let config = CsvParserConfig::default().max_rows(Some(2));
        let report = analyze_csv_file(csv.path(), &config).unwrap();

        assert_eq!(report.execution.rows_processed, 2);
        assert!(report.execution.source_exhausted);
        assert_eq!(report.execution.truncation_reason, None);
    }

    #[test]
    fn short_and_long_rows_are_counted_as_ragged() {
        let csv = write_csv("a,b,c\n1,2,3\n4,5\n6,7,8,9\n10,11,12\n");
        let report = analyze_csv_file(csv.path(), &CsvParserConfig::default()).unwrap();

        assert_eq!(report.execution.rows_processed, 4);
        assert_eq!(report.execution.ragged_row_count, 2);
    }

    #[test]
    fn a_ragged_row_past_the_row_cap_is_not_counted() {
        let csv = write_csv("a,b,c\n1,2,3\n4,5\n");
        let config = CsvParserConfig::default().max_rows(Some(1));
        let report = analyze_csv_file(csv.path(), &config).unwrap();

        assert_eq!(report.execution.ragged_row_count, 0);
        assert_eq!(
            report.execution.truncation_reason,
            Some(TruncationReason::MaxRows(1))
        );
    }

    #[test]
    fn test_analyze_csv_numeric_types_detected() {
        let csv = write_csv("int_col,float_col,str_col\n1,1.5,hello\n2,2.5,world\n3,3.5,foo\n");
        let config = CsvParserConfig::default();
        let report = analyze_csv_file(csv.path(), &config).unwrap();
        let profiles = &report.column_profiles;

        let int_col = profiles
            .iter()
            .find(|profile| profile.name == "int_col")
            .unwrap();
        assert!(matches!(int_col.data_type, DataType::Integer));

        let float_col = profiles
            .iter()
            .find(|profile| profile.name == "float_col")
            .unwrap();
        assert!(matches!(float_col.data_type, DataType::Float));

        let str_col = profiles
            .iter()
            .find(|profile| profile.name == "str_col")
            .unwrap();
        assert!(matches!(str_col.data_type, DataType::String));
    }

    #[test]
    fn test_analyze_csv_strict_rejects_ragged_rows() {
        let csv = write_csv("a,b\n1,2\n3,4,5\n");
        let config = CsvParserConfig::strict();
        assert!(analyze_csv_file(csv.path(), &config).is_err());
    }

    #[test]
    fn test_analyze_csv_file_handles_ragged_rows() {
        let csv = write_csv("a,b\n1,2\n3,4,5\n6\n");
        let config = CsvParserConfig::default();
        let report = analyze_csv_file(csv.path(), &config).unwrap();
        assert!(!report.column_profiles.is_empty());
    }

    #[test]
    fn test_analyze_csv_with_quality_metrics() {
        let csv = write_csv(
            "name,age\n\
             Alice,25\nBob,\nCharlie,30\nDave,\nEve,28\n\
             Frank,\nGrace,35\nHeidi,40\nIvan,\nJudy,22\n",
        );
        let config = CsvParserConfig::default();
        let report = analyze_csv_file(csv.path(), &config).unwrap();

        let age = report
            .column_profiles
            .iter()
            .find(|profile| profile.name == "age")
            .unwrap();
        assert_eq!(age.null_count, 4);
        assert_eq!(age.total_count, 10);
        assert!(report.quality_score().unwrap() >= 0.0);
    }

    #[test]
    fn test_detect_delimiter_comma() {
        let data = b"name,age,city\nAlice,25,NYC\nBob,30,London\n";
        assert_eq!(detect_delimiter(Cursor::new(data.as_ref())).unwrap(), b',');
    }

    #[test]
    fn test_detect_delimiter_semicolon() {
        let data = b"name;age;city\nAlice;25;NYC\nBob;30;London\n";
        assert_eq!(detect_delimiter(Cursor::new(data.as_ref())).unwrap(), b';');
    }

    #[test]
    fn test_detect_delimiter_pipe() {
        let data = b"name|age|city\nAlice|25|NYC\nBob|30|London\n";
        assert_eq!(detect_delimiter(Cursor::new(data.as_ref())).unwrap(), b'|');
    }

    #[test]
    fn test_detect_delimiter_tab() {
        let data = b"name\tage\tcity\nAlice\t25\tNYC\nBob\t30\tLondon\n";
        assert_eq!(detect_delimiter(Cursor::new(data.as_ref())).unwrap(), b'\t');
    }

    #[test]
    fn test_detect_delimiter_from_path_semicolon() {
        let csv = write_csv("id;name;salary\n1;Alice;50000\n2;Bob;60000\n");
        assert_eq!(detect_delimiter_from_path(csv.path()).unwrap(), b';');
    }

    #[test]
    fn test_detect_delimiter_blank_records_do_not_outvote_header() {
        // A header-only file with trailing blank lines: the blank records are
        // single-field for every candidate, so counting them made `;` win on
        // perfect consistency and collapse the whole header into one column
        // (gh #559).
        let data = b"name,age\n\n\n";
        assert_eq!(detect_delimiter(Cursor::new(data.as_ref())).unwrap(), b',');

        let semicolon = b"name;age\n\n";
        assert_eq!(
            detect_delimiter(Cursor::new(semicolon.as_ref())).unwrap(),
            b';'
        );
    }

    #[test]
    fn test_detect_delimiter_quoted_fields() {
        let data = b"name;desc;val\n\"hello;world\";foo;1\nbar;baz;2\n";
        assert_eq!(detect_delimiter(Cursor::new(data.as_ref())).unwrap(), b';');
    }

    #[test]
    fn test_detect_delimiter_quoted_embedded_newline() {
        // A quoted cell containing a newline must not break record splitting:
        // before the quote-aware sniffer this file was detected as semicolon
        // (zero occurrences, "perfectly consistent" single-field records) and
        // profiled as a single column named "id,comment".
        let data = b"id,comment\n1,\"line one\nline two\"\n2,\"normal\"\n3,\"has \"\"quotes\"\" inside\"\n";
        assert_eq!(detect_delimiter(Cursor::new(data.as_ref())).unwrap(), b',');
    }

    #[test]
    fn test_detect_delimiter_absent_candidate_never_beats_real_one() {
        // One ragged record makes comma imperfect (consistency 3/4); an absent
        // delimiter is uniformly single-field (4/4) and must still lose.
        let data = b"a,b,c\n1,2,3\nragged line without delimiters\n4,5,6\n";
        assert_eq!(detect_delimiter(Cursor::new(data.as_ref())).unwrap(), b',');
    }

    #[test]
    fn test_detect_delimiter_single_column_falls_back_to_comma() {
        let data = b"value\n1\n2\n3\n";
        assert_eq!(detect_delimiter(Cursor::new(data.as_ref())).unwrap(), b',');
    }

    #[test]
    fn test_detect_delimiter_two_column_semicolon_with_commas_in_text() {
        // Free text containing commas must not outvote the true delimiter.
        let data = b"id;note\n1;hello, world\n2;foo, bar, baz\n3;plain\n";
        assert_eq!(detect_delimiter(Cursor::new(data.as_ref())).unwrap(), b';');
    }

    #[test]
    fn test_detect_delimiter_never_splits_rows_the_header_does_not() {
        // Pipe splits both data rows and not the header; the readers cut rows
        // to the header's width, so choosing it kept only `123` (gh #1092).
        let data = b"key\n123|456\n123|789\n";
        assert_eq!(detect_delimiter(Cursor::new(data.as_ref())).unwrap(), b',');
    }

    #[test]
    fn test_detect_delimiter_header_split_beats_uniform_rows() {
        // A trailing delimiter on every data row: comma splits the header,
        // which no other candidate does.
        let data = b"a,b\n1,2,\n3,4,\n";
        assert_eq!(detect_delimiter(Cursor::new(data.as_ref())).unwrap(), b',');
    }

    #[test]
    fn test_detect_delimiter_trailing_empty_fields_still_agree() {
        // Semicolon rows carry a trailing empty field, comma occurs inside the
        // header name and a decimal. Counting the empty extras against
        // semicolon let comma win and invent a null column (gh #1096).
        let data = b"id;amount,EUR\n1;10,50;\n2;20;\n";
        assert_eq!(detect_delimiter(Cursor::new(data.as_ref())).unwrap(), b';');
    }

    #[test]
    fn test_detect_delimiter_quoted_empty_trailing_field_still_agrees() {
        // `""` decodes to an empty field in every reader; judged on its raw
        // text it counted as content and comma won again (gh #1102).
        let data = b"id;amount,EUR\n1;10,50;\"\"\n2;20;\"\"\n";
        assert_eq!(detect_delimiter(Cursor::new(data.as_ref())).unwrap(), b';');
    }

    #[test]
    fn test_detect_delimiter_exact_width_beats_cut_trailing_fields() {
        // Both candidates split the header and keep every value; semicolon
        // reads each row as it stands, comma only after cutting an empty
        // trailing field. Comma won the tie and renamed the columns.
        let data = b"id,part;value\nx;y,z,\nu;v,w,\n";
        assert_eq!(detect_delimiter(Cursor::new(data.as_ref())).unwrap(), b';');
    }

    #[test]
    fn test_detect_delimiter_skips_a_utf8_bom() {
        // A BOM then a blank line: the BOM alone was scored as a one-field
        // header, and comma won over the real semicolon header.
        let data = b"\xEF\xBB\xBF\nid;value\n1;2\n3;4\n";
        assert_eq!(detect_delimiter(Cursor::new(data.as_ref())).unwrap(), b';');
        let no_blank = b"\xEF\xBB\xBFid;value\n1;2\n";
        assert_eq!(
            detect_delimiter(Cursor::new(no_blank.as_ref())).unwrap(),
            b';'
        );
    }

    #[test]
    fn test_detect_delimiter_lone_cr_ends_a_record() {
        // CR-only line endings: read as one header record, pipe split it and
        // won, and the keys were cut to `123` (gh #1097).
        let data = b"key\r123|456\r123|789\r";
        assert_eq!(detect_delimiter(Cursor::new(data.as_ref())).unwrap(), b',');
        let semicolon = b"id;value\r1;10\r2;20\r";
        assert_eq!(
            detect_delimiter(Cursor::new(semicolon.as_ref())).unwrap(),
            b';'
        );
    }

    #[test]
    fn test_count_fields_reports_trailing_empty_fields() {
        assert_eq!(count_fields("1;10,50;", ';'), (3, 1));
        assert_eq!(count_fields("a;;", ';'), (3, 2));
        assert_eq!(count_fields("a;\"\"", ';'), (2, 1));
        assert_eq!(count_fields("a;\"\"\"\"", ';'), (2, 0));
        assert_eq!(count_fields("a; ", ';'), (2, 0));
        assert_eq!(count_fields("a;b", ';'), (2, 0));
        assert_eq!(count_fields("\"x;y\";", ';'), (2, 1));
    }

    #[test]
    fn test_split_sample_records_quote_aware() {
        let (records, last_unterminated) = split_sample_records("a,b\r\n1,\"x\ny\"\n2,z\n");
        assert_eq!(records, vec!["a,b", "1,\"x\ny\"", "2,z"]);
        assert!(!last_unterminated);

        let (records, last_unterminated) = split_sample_records("a,b\n1,\"cut mid-fie");
        assert_eq!(records, vec!["a,b", "1,\"cut mid-fie"]);
        assert!(last_unterminated);

        let (records, last_unterminated) = split_sample_records("a,b\r1,\"x\ry\"\r2,z\r");
        assert_eq!(records, vec!["a,b", "1,\"x\ry\"", "2,z"]);
        assert!(!last_unterminated);

        let (records, _) = split_sample_records("a\rb\r\nc\n");
        assert_eq!(records, vec!["a", "b", "c"]);
    }

    #[test]
    fn test_detect_delimiter_full_sample_ending_on_record_boundary() {
        // Exactly 4096 bytes ending with a clean newline: the final record is
        // complete and must be scored, not dropped as a fragment.
        let mut data = Vec::from(&b"a;b;c\n"[..]);
        while data.len() < 4090 {
            data.extend_from_slice(b"1;2;3\n");
        }
        while data.len() < 4095 {
            data.push(b'#');
        }
        data.push(b'\n');
        assert_eq!(data.len(), 4096);
        assert_eq!(
            detect_delimiter(Cursor::new(data.as_slice())).unwrap(),
            b';'
        );
    }
}