dataprof 0.7.1

High-performance data profiler with ISO 8000/25012 quality metrics for CSV, JSON/JSONL, and Parquet files
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
use arrow::array::*;
use arrow::csv::ReaderBuilder;
use arrow::datatypes::*;
use std::fs::File;
use std::path::Path;
use std::sync::Arc;

use crate::core::errors::DataProfilerError;
use crate::core::report_assembler::ReportAssembler;
use crate::parsers::csv::CsvParserConfig;
use crate::types::{
    ColumnProfile, DataSource, DataType, ExecutionMetadata, FileFormat, MetricPack, ProfileReport,
    QualityDimension,
};

/// Sample cap for numeric columns (matches SAMPLE_THRESHOLD in stats::numeric)
const NUMERIC_SAMPLE_CAP: usize = 10_000;

/// Columnar profiler using Apache Arrow for efficient column-oriented processing
pub struct ArrowProfiler {
    batch_size: usize,
    memory_limit_mb: usize,
    quality_dimensions: Option<Vec<QualityDimension>>,
    metric_packs: Option<Vec<MetricPack>>,
    csv_config: Option<CsvParserConfig>,
    locale: Option<String>,
}

impl ArrowProfiler {
    pub fn new() -> Self {
        Self {
            batch_size: 8192, // Default batch size for Arrow
            memory_limit_mb: 512,
            quality_dimensions: None,
            metric_packs: None,
            csv_config: None,
            locale: None,
        }
    }

    pub fn batch_size(mut self, size: usize) -> Self {
        self.batch_size = size;
        self
    }

    pub fn memory_limit_mb(mut self, limit: usize) -> Self {
        self.memory_limit_mb = limit;
        self
    }

    pub fn quality_dimensions(mut self, dims: Vec<QualityDimension>) -> Self {
        self.quality_dimensions = Some(dims);
        self
    }

    pub fn metric_packs(mut self, packs: Vec<MetricPack>) -> Self {
        self.metric_packs = Some(packs);
        self
    }

    pub fn csv_config(mut self, config: CsvParserConfig) -> Self {
        self.csv_config = Some(config);
        self
    }

    pub fn locale(mut self, locale: String) -> Self {
        self.locale = Some(locale);
        self
    }

    pub fn analyze_csv_file(&self, file_path: &Path) -> Result<ProfileReport, DataProfilerError> {
        let start = std::time::Instant::now();
        let file = File::open(file_path)?;
        let file_size_bytes = file.metadata()?.len();
        let _file_size_mb = file_size_bytes as f64 / 1_048_576.0;

        // Read first to infer schema from headers
        let mut header_builder = csv::ReaderBuilder::new();
        header_builder.has_headers(true);
        if let Some(ref config) = self.csv_config {
            if let Some(delim) = config.delimiter {
                header_builder.delimiter(delim);
            }
            header_builder.flexible(config.flexible);
            header_builder.quote(config.quote_char);
            if config.trim_whitespace {
                header_builder.trim(csv::Trim::All);
            }
        }
        let mut reader = header_builder.from_path(file_path)?;

        // Get headers to create Arrow schema
        let headers = reader.headers()?.clone();
        let mut fields = Vec::new();
        for header in headers.iter() {
            // Start with string type, Arrow will convert during processing
            fields.push(Field::new(header, arrow::datatypes::DataType::Utf8, true));
        }
        let schema = Arc::new(Schema::new(fields));

        // Now create Arrow reader with proper schema
        let file = File::open(file_path)?;
        let mut arrow_builder = ReaderBuilder::new(schema.clone())
            .with_header(true)
            .with_batch_size(self.batch_size);
        if let Some(ref config) = self.csv_config
            && let Some(delim) = config.delimiter
        {
            arrow_builder = arrow_builder.with_delimiter(delim);
        }
        let csv_reader = arrow_builder.build(file)?;

        // Process data in columnar batches
        let mut column_analyzers: std::collections::HashMap<String, ColumnAnalyzer> =
            std::collections::HashMap::new();

        for field in schema.fields() {
            column_analyzers.insert(
                field.name().to_string(),
                ColumnAnalyzer::new(field.data_type()),
            );
        }

        let mut total_rows = 0;

        for batch_result in csv_reader {
            let batch = batch_result?;
            total_rows += batch.num_rows();

            // Process each column in the batch
            for (col_idx, column) in batch.columns().iter().enumerate() {
                let schema = batch.schema();
                let field = schema.field(col_idx);

                if let Some(analyzer) = column_analyzers.get_mut(field.name()) {
                    analyzer.process_array(column)?;
                }
            }
        }

        // Convert analyzers to column profiles and extract samples
        // Iterate in header order (from schema) to preserve source column ordering
        let packs = self.metric_packs.as_deref();
        let skip_stats = !MetricPack::include_statistics(packs);
        let skip_patterns = !MetricPack::include_patterns(packs);

        let mut column_profiles = Vec::new();
        let mut sample_columns = std::collections::HashMap::new();

        for header in headers.iter() {
            let name = header.to_string();
            if let Some(analyzer) = column_analyzers.get(&name) {
                let profile = analyzer.to_column_profile(
                    name.clone(),
                    skip_stats,
                    skip_patterns,
                    self.locale.as_deref(),
                );
                column_profiles.push(profile);
                sample_columns.insert(name, analyzer.get_sample_values());
            }
        }

        let scan_time_ms = start.elapsed().as_millis();
        let num_columns = column_profiles.len();

        let mut assembler = ReportAssembler::new(
            DataSource::File {
                path: file_path.display().to_string(),
                format: FileFormat::Csv,
                size_bytes: file_size_bytes,
                modified_at: None,
                parquet_metadata: None,
            },
            ExecutionMetadata::new(total_rows, num_columns, scan_time_ms),
        )
        .columns(column_profiles);

        if MetricPack::include_quality(packs) {
            assembler = assembler.with_quality_data(sample_columns);
            if let Some(ref dims) = self.quality_dimensions {
                assembler = assembler.with_requested_dimensions(dims.clone());
            }
        } else {
            assembler = assembler.skip_quality();
        }

        Ok(assembler.build())
    }
}

impl Default for ArrowProfiler {
    fn default() -> Self {
        Self::new()
    }
}

/// Column analyzer for Arrow arrays
struct ColumnAnalyzer {
    data_type: arrow::datatypes::DataType,
    total_count: usize,
    null_count: usize,
    unique_values: std::collections::HashSet<String>,
    // Numeric statistics
    min_value: Option<f64>,
    max_value: Option<f64>,
    sum: f64,
    sum_squares: f64,
    // Text statistics
    min_length: usize,
    max_length: usize,
    total_length: usize,
    // Boolean statistics
    true_count: usize,
    false_count: usize,
    // Sample values for pattern detection
    sample_values: Vec<String>,
}

impl ColumnAnalyzer {
    fn new(data_type: &arrow::datatypes::DataType) -> Self {
        Self {
            data_type: data_type.clone(),
            total_count: 0,
            null_count: 0,
            unique_values: std::collections::HashSet::new(),
            min_value: None,
            max_value: None,
            sum: 0.0,
            sum_squares: 0.0,
            min_length: usize::MAX,
            max_length: 0,
            total_length: 0,
            true_count: 0,
            false_count: 0,
            sample_values: Vec::new(),
        }
    }

    fn process_array(&mut self, array: &dyn Array) -> Result<(), DataProfilerError> {
        self.total_count += array.len();
        self.null_count += array.null_count();

        match array.data_type() {
            arrow::datatypes::DataType::Float64 => {
                if let Some(float_array) = array.as_any().downcast_ref::<Float64Array>() {
                    self.process_float64_array(float_array)?;
                } else {
                    return Err(DataProfilerError::ArrowError {
                        message: "Failed to downcast to Float64Array".to_string(),
                    });
                }
            }
            arrow::datatypes::DataType::Float32 => {
                if let Some(float_array) = array.as_any().downcast_ref::<Float32Array>() {
                    self.process_float32_array(float_array)?;
                } else {
                    return Err(DataProfilerError::ArrowError {
                        message: "Failed to downcast to Float32Array".to_string(),
                    });
                }
            }
            arrow::datatypes::DataType::Int64 => {
                if let Some(int_array) = array.as_any().downcast_ref::<Int64Array>() {
                    self.process_int64_array(int_array)?;
                } else {
                    return Err(DataProfilerError::ArrowError {
                        message: "Failed to downcast to Int64Array".to_string(),
                    });
                }
            }
            arrow::datatypes::DataType::Int32 => {
                if let Some(int_array) = array.as_any().downcast_ref::<Int32Array>() {
                    self.process_int32_array(int_array)?;
                } else {
                    return Err(DataProfilerError::ArrowError {
                        message: "Failed to downcast to Int32Array".to_string(),
                    });
                }
            }
            arrow::datatypes::DataType::Utf8 => {
                if let Some(string_array) = array.as_any().downcast_ref::<StringArray>() {
                    self.process_string_array(string_array)?;
                } else {
                    return Err(DataProfilerError::ArrowError {
                        message: "Failed to downcast to StringArray".to_string(),
                    });
                }
            }
            arrow::datatypes::DataType::LargeUtf8 => {
                if let Some(string_array) = array.as_any().downcast_ref::<LargeStringArray>() {
                    self.process_large_string_array(string_array)?;
                } else {
                    return Err(DataProfilerError::ArrowError {
                        message: "Failed to downcast to LargeStringArray".to_string(),
                    });
                }
            }
            arrow::datatypes::DataType::Boolean => {
                if let Some(bool_array) = array.as_any().downcast_ref::<BooleanArray>() {
                    self.process_boolean_array(bool_array)?;
                } else {
                    return Err(DataProfilerError::ArrowError {
                        message: "Failed to downcast to BooleanArray".to_string(),
                    });
                }
            }
            arrow::datatypes::DataType::Date32 => {
                if let Some(date_array) = array.as_any().downcast_ref::<Date32Array>() {
                    self.process_date32_array(date_array)?;
                } else {
                    return Err(DataProfilerError::ArrowError {
                        message: "Failed to downcast to Date32Array".to_string(),
                    });
                }
            }
            arrow::datatypes::DataType::Date64 => {
                if let Some(date_array) = array.as_any().downcast_ref::<Date64Array>() {
                    self.process_date64_array(date_array)?;
                } else {
                    return Err(DataProfilerError::ArrowError {
                        message: "Failed to downcast to Date64Array".to_string(),
                    });
                }
            }
            arrow::datatypes::DataType::Timestamp(_, _) => {
                if let Some(ts_array) = array.as_any().downcast_ref::<TimestampNanosecondArray>() {
                    self.process_timestamp_array(ts_array)?;
                } else if let Some(ts_array) =
                    array.as_any().downcast_ref::<TimestampMicrosecondArray>()
                {
                    self.process_timestamp_micro_array(ts_array)?;
                } else if let Some(ts_array) =
                    array.as_any().downcast_ref::<TimestampMillisecondArray>()
                {
                    self.process_timestamp_milli_array(ts_array)?;
                } else if let Some(ts_array) = array.as_any().downcast_ref::<TimestampSecondArray>()
                {
                    self.process_timestamp_second_array(ts_array)?;
                } else {
                    return Err(DataProfilerError::ArrowError {
                        message: "Failed to downcast Timestamp array".to_string(),
                    });
                }
            }
            arrow::datatypes::DataType::Binary | arrow::datatypes::DataType::LargeBinary => {
                if let Some(bin_array) = array.as_any().downcast_ref::<BinaryArray>() {
                    self.process_binary_array(bin_array)?;
                } else if let Some(bin_array) = array.as_any().downcast_ref::<LargeBinaryArray>() {
                    self.process_large_binary_array(bin_array)?;
                } else {
                    return Err(DataProfilerError::ArrowError {
                        message: "Failed to downcast Binary array".to_string(),
                    });
                }
            }
            _ => {
                // For other types, convert to string and process
                self.process_as_string_array(array)?;
            }
        }

        Ok(())
    }

    fn process_float64_array(&mut self, array: &Float64Array) -> Result<(), DataProfilerError> {
        for i in 0..array.len() {
            if let Some(value) = array.value(i).into() {
                self.update_numeric_stats(value);

                // Add to unique values (with limit)
                if self.unique_values.len() < 1000 {
                    self.unique_values.insert(value.to_string());
                }

                // Keep samples for pattern detection
                if self.sample_values.len() < NUMERIC_SAMPLE_CAP {
                    self.sample_values.push(value.to_string());
                }
            }
        }
        Ok(())
    }

    fn process_float32_array(&mut self, array: &Float32Array) -> Result<(), DataProfilerError> {
        for i in 0..array.len() {
            if let Some(value) = array.value(i).into() {
                let value_f64 = value as f64;
                self.update_numeric_stats(value_f64);

                if self.unique_values.len() < 1000 {
                    self.unique_values.insert(value.to_string());
                }

                if self.sample_values.len() < NUMERIC_SAMPLE_CAP {
                    self.sample_values.push(value.to_string());
                }
            }
        }
        Ok(())
    }

    fn process_int64_array(&mut self, array: &Int64Array) -> Result<(), DataProfilerError> {
        for i in 0..array.len() {
            if let Some(value) = array.value(i).into() {
                let value_f64 = value as f64;
                self.update_numeric_stats(value_f64);

                if self.unique_values.len() < 1000 {
                    self.unique_values.insert(value.to_string());
                }

                if self.sample_values.len() < NUMERIC_SAMPLE_CAP {
                    self.sample_values.push(value.to_string());
                }
            }
        }
        Ok(())
    }

    fn process_int32_array(&mut self, array: &Int32Array) -> Result<(), DataProfilerError> {
        for i in 0..array.len() {
            if let Some(value) = array.value(i).into() {
                let value_f64 = value as f64;
                self.update_numeric_stats(value_f64);

                if self.unique_values.len() < 1000 {
                    self.unique_values.insert(value.to_string());
                }

                if self.sample_values.len() < NUMERIC_SAMPLE_CAP {
                    self.sample_values.push(value.to_string());
                }
            }
        }
        Ok(())
    }

    fn process_string_array(&mut self, array: &StringArray) -> Result<(), DataProfilerError> {
        for i in 0..array.len() {
            if !array.is_null(i) {
                let value = array.value(i);
                self.update_text_stats(value);

                if self.unique_values.len() < 1000 {
                    self.unique_values.insert(value.to_string());
                }

                if self.sample_values.len() < NUMERIC_SAMPLE_CAP {
                    self.sample_values.push(value.to_string());
                }
            }
        }
        Ok(())
    }

    fn process_large_string_array(
        &mut self,
        array: &LargeStringArray,
    ) -> Result<(), DataProfilerError> {
        for i in 0..array.len() {
            if !array.is_null(i) {
                let value = array.value(i);
                self.update_text_stats(value);

                if self.unique_values.len() < 1000 {
                    self.unique_values.insert(value.to_string());
                }

                if self.sample_values.len() < NUMERIC_SAMPLE_CAP {
                    self.sample_values.push(value.to_string());
                }
            }
        }
        Ok(())
    }

    fn process_boolean_array(&mut self, array: &BooleanArray) -> Result<(), DataProfilerError> {
        for i in 0..array.len() {
            if !array.is_null(i) {
                let value = array.value(i);
                if value {
                    self.true_count += 1;
                } else {
                    self.false_count += 1;
                }
                let value_str = if value { "True" } else { "False" };

                if self.unique_values.len() < 1000 {
                    self.unique_values.insert(value_str.to_string());
                }

                if self.sample_values.len() < NUMERIC_SAMPLE_CAP {
                    self.sample_values.push(value_str.to_string());
                }
            }
        }
        Ok(())
    }

    fn process_date32_array(&mut self, array: &Date32Array) -> Result<(), DataProfilerError> {
        for i in 0..array.len() {
            if let Some(value) = array.value(i).into() {
                // Date32 represents days since epoch
                let date_str = format!("date32:{}", value);
                self.update_text_stats(&date_str);

                if self.unique_values.len() < 1000 {
                    self.unique_values.insert(date_str.clone());
                }

                if self.sample_values.len() < NUMERIC_SAMPLE_CAP {
                    self.sample_values.push(date_str);
                }
            }
        }
        Ok(())
    }

    fn process_date64_array(&mut self, array: &Date64Array) -> Result<(), DataProfilerError> {
        for i in 0..array.len() {
            if let Some(value) = array.value(i).into() {
                // Date64 represents milliseconds since epoch
                let date_str = format!("date64:{}", value);
                self.update_text_stats(&date_str);

                if self.unique_values.len() < 1000 {
                    self.unique_values.insert(date_str.clone());
                }

                if self.sample_values.len() < NUMERIC_SAMPLE_CAP {
                    self.sample_values.push(date_str);
                }
            }
        }
        Ok(())
    }

    fn process_timestamp_array(
        &mut self,
        array: &TimestampNanosecondArray,
    ) -> Result<(), DataProfilerError> {
        for i in 0..array.len() {
            if let Some(value) = array.value(i).into() {
                let ts_str = format!("timestamp_ns:{}", value);
                self.update_text_stats(&ts_str);

                if self.unique_values.len() < 1000 {
                    self.unique_values.insert(ts_str.clone());
                }

                if self.sample_values.len() < NUMERIC_SAMPLE_CAP {
                    self.sample_values.push(ts_str);
                }
            }
        }
        Ok(())
    }

    fn process_timestamp_micro_array(
        &mut self,
        array: &TimestampMicrosecondArray,
    ) -> Result<(), DataProfilerError> {
        for i in 0..array.len() {
            if let Some(value) = array.value(i).into() {
                let ts_str = format!("timestamp_us:{}", value);
                self.update_text_stats(&ts_str);

                if self.unique_values.len() < 1000 {
                    self.unique_values.insert(ts_str.clone());
                }

                if self.sample_values.len() < NUMERIC_SAMPLE_CAP {
                    self.sample_values.push(ts_str);
                }
            }
        }
        Ok(())
    }

    fn process_timestamp_milli_array(
        &mut self,
        array: &TimestampMillisecondArray,
    ) -> Result<(), DataProfilerError> {
        for i in 0..array.len() {
            if let Some(value) = array.value(i).into() {
                let ts_str = format!("timestamp_ms:{}", value);
                self.update_text_stats(&ts_str);

                if self.unique_values.len() < 1000 {
                    self.unique_values.insert(ts_str.clone());
                }

                if self.sample_values.len() < NUMERIC_SAMPLE_CAP {
                    self.sample_values.push(ts_str);
                }
            }
        }
        Ok(())
    }

    fn process_timestamp_second_array(
        &mut self,
        array: &TimestampSecondArray,
    ) -> Result<(), DataProfilerError> {
        for i in 0..array.len() {
            if let Some(value) = array.value(i).into() {
                let ts_str = format!("timestamp_s:{}", value);
                self.update_text_stats(&ts_str);

                if self.unique_values.len() < 1000 {
                    self.unique_values.insert(ts_str.clone());
                }

                if self.sample_values.len() < NUMERIC_SAMPLE_CAP {
                    self.sample_values.push(ts_str);
                }
            }
        }
        Ok(())
    }

    fn process_binary_array(&mut self, array: &BinaryArray) -> Result<(), DataProfilerError> {
        for i in 0..array.len() {
            if !array.is_null(i) {
                let value = array.value(i);
                // Convert to hex string manually (first 8 bytes)
                let sample_bytes = &value[..value.len().min(8)];
                let hex_str = format!(
                    "0x{}",
                    sample_bytes
                        .iter()
                        .map(|b| format!("{:02x}", b))
                        .collect::<String>()
                );
                self.update_text_stats(&hex_str);

                if self.unique_values.len() < 1000 {
                    self.unique_values.insert(hex_str.clone());
                }

                if self.sample_values.len() < NUMERIC_SAMPLE_CAP {
                    self.sample_values.push(hex_str);
                }
            }
        }
        Ok(())
    }

    fn process_large_binary_array(
        &mut self,
        array: &LargeBinaryArray,
    ) -> Result<(), DataProfilerError> {
        for i in 0..array.len() {
            if !array.is_null(i) {
                let value = array.value(i);
                // Convert to hex string manually (first 8 bytes)
                let sample_bytes = &value[..value.len().min(8)];
                let hex_str = format!(
                    "0x{}",
                    sample_bytes
                        .iter()
                        .map(|b| format!("{:02x}", b))
                        .collect::<String>()
                );
                self.update_text_stats(&hex_str);

                if self.unique_values.len() < 1000 {
                    self.unique_values.insert(hex_str.clone());
                }

                if self.sample_values.len() < NUMERIC_SAMPLE_CAP {
                    self.sample_values.push(hex_str);
                }
            }
        }
        Ok(())
    }

    fn process_as_string_array(&mut self, array: &dyn Array) -> Result<(), DataProfilerError> {
        // Convert any array type to string for processing using Arrow's display functionality
        use arrow::util::display::array_value_to_string;

        for i in 0..array.len() {
            if !array.is_null(i) {
                // Use Arrow's built-in conversion to string
                let value = array_value_to_string(array, i)
                    .unwrap_or_else(|_| format!("<type:{}>", array.data_type()));
                self.update_text_stats(&value);

                if self.sample_values.len() < NUMERIC_SAMPLE_CAP {
                    self.sample_values.push(value.clone());
                }

                if self.unique_values.len() < 1000 {
                    self.unique_values.insert(value);
                }
            }
        }
        Ok(())
    }

    fn update_numeric_stats(&mut self, value: f64) {
        self.sum += value;
        self.sum_squares += value * value;

        self.min_value = Some(match self.min_value {
            Some(min) => min.min(value),
            None => value,
        });

        self.max_value = Some(match self.max_value {
            Some(max) => max.max(value),
            None => value,
        });
    }

    fn update_text_stats(&mut self, value: &str) {
        let len = value.len();
        self.min_length = self.min_length.min(len);
        self.max_length = self.max_length.max(len);
        self.total_length += len;
    }

    fn to_column_profile(
        &self,
        name: String,
        skip_statistics: bool,
        skip_patterns: bool,
        locale: Option<&str>,
    ) -> ColumnProfile {
        let data_type = self.infer_data_type();
        let avg_length = if self.total_count > self.null_count {
            self.total_length as f64 / (self.total_count - self.null_count) as f64
        } else {
            0.0
        };

        crate::core::profile_builder::build_column_profile(
            crate::core::profile_builder::ColumnProfileInput {
                name,
                data_type,
                total_count: self.total_count,
                null_count: self.null_count,
                unique_count: Some(self.unique_values.len()),
                sample_values: &self.sample_values,
                text_lengths: Some(crate::core::profile_builder::TextLengths {
                    min_length: self.min_length,
                    max_length: self.max_length,
                    avg_length,
                }),
                boolean_counts: if matches!(self.data_type, arrow::datatypes::DataType::Boolean) {
                    Some((self.true_count, self.false_count))
                } else {
                    None
                },
                skip_statistics,
                skip_patterns,
                locale,
            },
        )
    }

    fn infer_data_type(&self) -> DataType {
        match &self.data_type {
            arrow::datatypes::DataType::Float64 | arrow::datatypes::DataType::Float32 => {
                DataType::Float
            }
            arrow::datatypes::DataType::Int64
            | arrow::datatypes::DataType::Int32
            | arrow::datatypes::DataType::Int16
            | arrow::datatypes::DataType::Int8 => DataType::Integer,
            arrow::datatypes::DataType::Boolean => DataType::Boolean,
            arrow::datatypes::DataType::Utf8 | arrow::datatypes::DataType::LargeUtf8 => {
                // Reuse the shared inference logic for consistent type detection
                // across all engines (dates before numerics, 100% match threshold)
                crate::analysis::inference::infer_type(&self.sample_values)
            }
            _ => DataType::String,
        }
    }

    /// Get collected sample values for quality metrics calculation
    fn get_sample_values(&self) -> Vec<String> {
        self.sample_values.clone()
    }
}

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

    #[test]
    fn test_arrow_profiler() -> Result<(), DataProfilerError> {
        // Create a test CSV file
        let mut temp_file = NamedTempFile::new()?;
        writeln!(temp_file, "name,age,salary,active")?;
        writeln!(temp_file, "Alice,25,50000.0,true")?;
        writeln!(temp_file, "Bob,30,60000.5,false")?;
        writeln!(temp_file, "Charlie,35,70000.0,true")?;
        temp_file.flush()?;

        // Test Arrow profiler
        let profiler = ArrowProfiler::new();
        let report = profiler.analyze_csv_file(temp_file.path())?;

        assert_eq!(report.column_profiles.len(), 4);

        // Find age column and verify it's detected as numeric
        let age_column = report
            .column_profiles
            .iter()
            .find(|p| p.name == "age")
            .expect("Age column should exist");

        assert_eq!(age_column.total_count, 3);
        assert_eq!(
            age_column.data_type,
            DataType::Integer,
            "age column should be detected as Integer"
        );

        Ok(())
    }

    #[test]
    fn test_arrow_profiler_csv_with_mixed_columns() -> Result<(), DataProfilerError> {
        use crate::types::ColumnStats;

        // The Arrow CSV profiler reads all columns as Utf8 and then infers types.
        // Numeric-looking Utf8 columns that also have float-typed Arrow data
        // get properly typed. Test with a mixed CSV.
        let mut temp_file = NamedTempFile::new()?;
        writeln!(temp_file, "name,score")?;
        for i in 1..=20 {
            writeln!(temp_file, "Person{},{}", i, i * 10)?;
        }
        temp_file.flush()?;

        let profiler = ArrowProfiler::new();
        let report = profiler.analyze_csv_file(temp_file.path())?;

        assert_eq!(report.column_profiles.len(), 2);

        let score_col = report
            .column_profiles
            .iter()
            .find(|p| p.name == "score")
            .expect("score column should exist");

        assert_eq!(score_col.total_count, 20);

        // Numeric Utf8 columns should now be detected as Integer
        assert_eq!(
            score_col.data_type,
            DataType::Integer,
            "score column should be detected as Integer"
        );

        // Verify numeric stats are computed
        match &score_col.stats {
            ColumnStats::Numeric(n) => {
                assert!((n.min - 10.0).abs() < 0.01, "min should be 10");
                assert!((n.max - 200.0).abs() < 0.01, "max should be 200");
                assert!((n.mean - 105.0).abs() < 0.01, "mean should be 105");
                assert!(n.skewness.is_some(), "skewness should be computed");
                assert!(n.kurtosis.is_some(), "kurtosis should be computed");
            }
            other => panic!("score column should have Numeric stats, got {:?}", other),
        }

        // Verify name column is still String
        let name_col = report
            .column_profiles
            .iter()
            .find(|p| p.name == "name")
            .expect("name column should exist");
        assert_eq!(name_col.data_type, DataType::String);

        Ok(())
    }

    #[test]
    fn test_arrow_profiler_numeric_inference_float() -> Result<(), DataProfilerError> {
        use crate::types::ColumnStats;

        let mut temp_file = NamedTempFile::new()?;
        writeln!(temp_file, "label,value")?;
        for i in 1..=20 {
            writeln!(temp_file, "item{},{:.2}", i, i as f64 * 1.5)?;
        }
        temp_file.flush()?;

        let profiler = ArrowProfiler::new();
        let report = profiler.analyze_csv_file(temp_file.path())?;

        let value_col = report
            .column_profiles
            .iter()
            .find(|p| p.name == "value")
            .expect("value column should exist");

        assert_eq!(
            value_col.data_type,
            DataType::Float,
            "value column with decimals should be detected as Float"
        );
        assert!(
            matches!(&value_col.stats, ColumnStats::Numeric(..)),
            "Float column should have Numeric stats"
        );

        Ok(())
    }

    #[test]
    fn test_boolean_column_detection_and_stats() {
        use crate::types::ColumnStats;
        use arrow::array::BooleanArray;
        use arrow::datatypes::DataType as ArrowDataType;

        // Create a ColumnAnalyzer for a Boolean column
        let mut analyzer = ColumnAnalyzer::new(&ArrowDataType::Boolean);

        // Construct a BooleanArray with some nulls
        let array = BooleanArray::from(vec![
            Some(true),
            Some(false),
            Some(true),
            None,
            Some(true),
            Some(false),
        ]);

        analyzer
            .process_array(&array)
            .expect("should process boolean array");

        assert_eq!(analyzer.total_count, 6);
        assert_eq!(analyzer.null_count, 1);
        assert_eq!(analyzer.true_count, 3);
        assert_eq!(analyzer.false_count, 2);

        let profile = analyzer.to_column_profile("flag".to_string(), false, false, None);
        assert_eq!(profile.data_type, DataType::Boolean);
        assert_eq!(profile.total_count, 6);
        assert_eq!(profile.null_count, 1);

        match &profile.stats {
            ColumnStats::Boolean(b) => {
                assert_eq!(b.true_count, 3);
                assert_eq!(b.false_count, 2);
                assert!((b.true_ratio - 0.6).abs() < 0.001);
            }
            other => panic!("expected Boolean stats, got {:?}", other),
        }
    }
}