dataprof 0.10.0

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
use std::time::Duration;

use dataprof::{
    ChunkSize, CsvParserConfig, DataSource, DataType, EngineType, FileFormat, JsonFormat,
    JsonParserConfig, MetricPack, OutputFormat, Profiler, ProfilerConfig, ProgressSink,
    QualityDimension, SamplingStrategy, StopCondition, analyze_column_fast, analyze_structure,
    calculate_numeric_stats, calculate_text_stats, detect_patterns, infer_type,
};

#[test]
fn stable_facade_builder_surface_compiles() {
    let _profiler = Profiler::with_config(ProfilerConfig::default())
        .engine(EngineType::Auto)
        .chunk_size(ChunkSize::Fixed(128))
        .sampling(SamplingStrategy::None)
        .memory_limit_mb(32)
        .stop_when(StopCondition::MaxRows(1_000))
        .format(FileFormat::Csv)
        .csv_delimiter(b';')
        .csv_flexible(true)
        .quality_dimensions(vec![QualityDimension::Completeness])
        .metric_packs(vec![MetricPack::Schema, MetricPack::Quality])
        .locale("IT")
        .progress_interval(Duration::from_millis(25))
        .progress_sink(ProgressSink::None);

    let source = DataSource::File {
        path: "sample.csv".to_string(),
        format: FileFormat::Csv,
        size_bytes: 42,
        modified_at: None,
        parquet_metadata: None,
    };

    assert!(source.is_file());
    assert_eq!(FileFormat::Csv.to_string(), "csv");
    assert!(matches!(OutputFormat::Json, OutputFormat::Json));
}

#[test]
fn parser_and_metrics_reexports_compile() {
    let numeric_values = vec!["1".to_string(), "2".to_string(), "3".to_string()];
    let text_values = vec![
        "alice@example.com".to_string(),
        "bob@example.com".to_string(),
    ];

    assert_eq!(infer_type(&numeric_values), DataType::Integer);
    assert_eq!(analyze_column_fast("n", &numeric_values).name, "n");
    let _numeric_stats = calculate_numeric_stats(&numeric_values);
    let _text_stats = calculate_text_stats(&text_values);
    let _patterns = detect_patterns(&text_values, Some("US"));

    let csv_config = CsvParserConfig::strict()
        .with_delimiter(b';')
        .has_header(true)
        .max_rows(Some(10));
    assert_eq!(csv_config.delimiter, Some(b';'));

    let jsonl_config = JsonParserConfig::jsonl().with_max_rows(10);
    assert_eq!(jsonl_config.format, Some(JsonFormat::Jsonl));
    assert_eq!(
        JsonParserConfig::json_array().format,
        Some(JsonFormat::JsonArray)
    );
}

#[cfg(feature = "parquet")]
#[test]
fn parquet_facade_reexports_compile() {
    use dataprof::ParquetConfig;

    let config = ParquetConfig::batch_size(1_024);
    assert_eq!(config.batch_size, 1_024);
    assert_eq!(ParquetConfig::adaptive_batch_size(0), 1_024);
}

#[cfg(feature = "database")]
#[test]
fn database_facade_reexports_compile() {
    use dataprof::{DataProfilerError, DatabaseConfig, DatabaseConnector, create_connector};

    let _config = DatabaseConfig {
        connection_string: "sqlite::memory:".to_string(),
        load_credentials_from_env: false,
        ..Default::default()
    };

    let _factory: fn(DatabaseConfig) -> Result<Box<dyn DatabaseConnector>, DataProfilerError> =
        create_connector;
}

/// Regression: `Profiler::format()` must override extension-based detection,
/// even on the auto engine path that previously short-circuited to Parquet
/// based on the `.parquet` file extension.
#[test]
fn format_override_beats_extension() {
    use std::io::Write;

    let mut tmp = tempfile::Builder::new()
        .suffix(".parquet")
        .tempfile()
        .expect("tmpfile");
    writeln!(tmp, "city,population").unwrap();
    writeln!(tmp, "Rome,2873").unwrap();
    writeln!(tmp, "Milan,1352").unwrap();
    tmp.flush().unwrap();

    let report = Profiler::new()
        .format(FileFormat::Csv)
        .analyze_file(tmp.path())
        .expect("forced CSV parse of .parquet-named file should succeed");

    assert_eq!(report.execution.columns_detected, 2);
}

/// Regression: the default `EngineType::Auto` silently ignored `stop_when`,
/// scanning the whole file and reporting `source_exhausted` with no truncation
/// reason. Auto must route to an engine that honours the stop condition.
#[test]
fn auto_engine_honours_stop_condition() {
    use std::io::Write;

    let mut tmp = tempfile::Builder::new()
        .suffix(".csv")
        .tempfile()
        .expect("tmpfile");
    writeln!(tmp, "n").unwrap();
    for i in 0..500 {
        writeln!(tmp, "{i}").unwrap();
    }
    tmp.flush().unwrap();

    let report = Profiler::new()
        .engine(EngineType::Auto)
        .stop_when(StopCondition::MaxRows(50))
        .analyze_file(tmp.path())
        .expect("auto engine should profile the csv");

    assert!(
        report.execution.rows_processed < 500,
        "auto engine ignored max_rows: processed {} rows",
        report.execution.rows_processed
    );
    assert!(
        report.execution.truncation_reason.is_some(),
        "early stop must record a truncation reason"
    );
    assert!(
        !report.execution.source_exhausted,
        "a truncated scan must not claim the source was exhausted"
    );
}

/// The columnar engine enforces a row cap, slicing the batch that straddles the
/// limit so the row count is exact rather than rounded to a batch boundary.
///
/// The columnar CSV path is the `ArrowProfiler`, so this needs the `arrow` feature.
#[cfg(feature = "arrow")]
#[test]
fn columnar_engine_honours_max_rows() {
    use std::io::Write;

    let mut csv = tempfile::Builder::new()
        .suffix(".csv")
        .tempfile()
        .expect("tmpfile");
    writeln!(csv, "n").unwrap();
    for i in 0..500 {
        writeln!(csv, "{i}").unwrap();
    }
    csv.flush().unwrap();

    let report = Profiler::new()
        .engine(EngineType::Columnar)
        .stop_when(StopCondition::MaxRows(50))
        .analyze_file(csv.path())
        .expect("columnar should profile the csv");

    assert_eq!(report.execution.rows_processed, 50);
    assert!(report.execution.truncation_reason.is_some());
    assert!(!report.execution.source_exhausted);
}

/// The JSON parser enforces a row cap on every engine that routes to it.
#[test]
fn json_honours_max_rows() {
    use std::io::Write;

    let mut jsonl = tempfile::Builder::new()
        .suffix(".jsonl")
        .tempfile()
        .expect("tmpfile");
    for i in 0..500 {
        writeln!(jsonl, "{{\"a\": {i}}}").unwrap();
    }
    jsonl.flush().unwrap();

    for engine in [
        EngineType::Auto,
        EngineType::Incremental,
        EngineType::Columnar,
    ] {
        let report = Profiler::new()
            .engine(engine)
            .stop_when(StopCondition::MaxRows(50))
            .analyze_file(jsonl.path())
            .unwrap_or_else(|e| panic!("json profile failed for {engine:?}: {e}"));

        assert_eq!(
            report.execution.rows_processed, 50,
            "json ignored max_rows on {engine:?}"
        );
        assert!(
            report.execution.truncation_reason.is_some(),
            "no truncation reason on {engine:?}"
        );
        assert!(
            !report.execution.source_exhausted,
            "exhausted on {engine:?}"
        );
    }
}

/// Parquet enforces a row cap via the reader's `with_limit`.
#[cfg(feature = "parquet")]
#[test]
fn parquet_honours_max_rows() {
    let path = std::path::Path::new("examples/test_data/sensors.parquet");
    assert!(path.exists(), "fixture missing: {}", path.display());

    let full = Profiler::new()
        .analyze_file(path)
        .expect("parquet profile should succeed");
    assert_eq!(full.execution.rows_processed, 20);
    assert!(full.execution.truncation_reason.is_none());

    for engine in [EngineType::Auto, EngineType::Columnar] {
        let report = Profiler::new()
            .engine(engine)
            .stop_when(StopCondition::MaxRows(5))
            .analyze_file(path)
            .unwrap_or_else(|e| panic!("parquet profile failed for {engine:?}: {e}"));

        assert_eq!(
            report.execution.rows_processed, 5,
            "parquet ignored max_rows on {engine:?}"
        );
        assert!(
            report.execution.truncation_reason.is_some(),
            "no truncation reason on {engine:?}"
        );
        assert!(
            !report.execution.source_exhausted,
            "exhausted on {engine:?}"
        );
    }
}

/// A row cap must not fire when the source is smaller than the cap.
#[test]
fn max_rows_above_row_count_is_not_truncation() {
    use std::io::Write;

    let mut jsonl = tempfile::Builder::new()
        .suffix(".jsonl")
        .tempfile()
        .expect("tmpfile");
    writeln!(jsonl, "{{\"a\": 1}}").unwrap();
    writeln!(jsonl, "{{\"a\": 2}}").unwrap();
    jsonl.flush().unwrap();

    let report = Profiler::new()
        .stop_when(StopCondition::MaxRows(1_000))
        .analyze_file(jsonl.path())
        .expect("json profile should succeed");

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

/// Row-capped parsers cannot evaluate richer conditions. Rejecting is correct;
/// silently returning a full scan marked `source_exhausted` is not.
#[test]
fn non_row_limit_stop_condition_is_rejected_not_ignored() {
    use std::io::Write;

    let mut jsonl = tempfile::Builder::new()
        .suffix(".jsonl")
        .tempfile()
        .expect("tmpfile");
    writeln!(jsonl, "{{\"a\": 1}}").unwrap();
    writeln!(jsonl, "{{\"a\": 2}}").unwrap();
    jsonl.flush().unwrap();

    let err = Profiler::new()
        .stop_when(StopCondition::MaxBytes(16))
        .analyze_file(jsonl.path())
        .expect_err("json must reject a byte-cap it cannot honour");
    assert!(
        err.to_string().contains("row-limit"),
        "unexpected error: {err}"
    );
}

/// The rejection must only fire when a stop condition is actually set.
#[test]
fn unsupported_combinations_still_profile_without_stop_condition() {
    use std::io::Write;

    let mut jsonl = tempfile::Builder::new()
        .suffix(".jsonl")
        .tempfile()
        .expect("tmpfile");
    writeln!(jsonl, "{{\"a\": 1}}").unwrap();
    writeln!(jsonl, "{{\"a\": 2}}").unwrap();
    jsonl.flush().unwrap();

    let report = Profiler::new()
        .analyze_file(jsonl.path())
        .expect("json without a stop condition must still profile");
    assert_eq!(report.execution.rows_processed, 2);
}

/// Auto must keep using the adaptive engine when no stop condition is set.
#[test]
fn auto_engine_without_stop_condition_reads_everything() {
    use std::io::Write;

    let mut tmp = tempfile::Builder::new()
        .suffix(".csv")
        .tempfile()
        .expect("tmpfile");
    writeln!(tmp, "n").unwrap();
    for i in 0..100 {
        writeln!(tmp, "{i}").unwrap();
    }
    tmp.flush().unwrap();

    let report = Profiler::new()
        .engine(EngineType::Auto)
        .analyze_file(tmp.path())
        .expect("auto engine should profile the csv");

    assert_eq!(report.execution.rows_processed, 100);
    assert_eq!(report.execution.engine.as_deref(), Some("incremental"));
    assert!(report.execution.truncation_reason.is_none());
    assert!(report.execution.source_exhausted);
}

#[test]
fn explicit_incremental_engine_is_recorded() {
    use std::io::Write;

    let mut tmp = tempfile::Builder::new()
        .suffix(".csv")
        .tempfile()
        .expect("tmpfile");
    writeln!(tmp, "n").unwrap();
    writeln!(tmp, "1").unwrap();
    tmp.flush().unwrap();

    let report = Profiler::new()
        .engine(EngineType::Incremental)
        .analyze_file(tmp.path())
        .expect("incremental engine should profile the csv");

    assert_eq!(report.execution.engine.as_deref(), Some("incremental"));
}

#[cfg(feature = "arrow")]
#[test]
fn explicit_columnar_engine_is_recorded() {
    use std::io::Write;

    let mut tmp = tempfile::Builder::new()
        .suffix(".csv")
        .tempfile()
        .expect("tmpfile");
    writeln!(tmp, "n").unwrap();
    writeln!(tmp, "1").unwrap();
    tmp.flush().unwrap();

    let report = Profiler::new()
        .engine(EngineType::Columnar)
        .analyze_file(tmp.path())
        .expect("columnar engine should profile the csv");

    assert_eq!(report.execution.engine.as_deref(), Some("columnar"));
}

#[test]
fn analyze_structure_facade_compiles() {
    use std::io::Write;

    let mut tmp = tempfile::Builder::new()
        .suffix(".csv")
        .tempfile()
        .expect("tmpfile");
    writeln!(tmp, "name,age").unwrap();
    writeln!(tmp, "Alice,30").unwrap();
    writeln!(tmp, "Bob,").unwrap();
    tmp.flush().unwrap();

    let report = analyze_structure(tmp.path(), Some(10)).expect("free function");
    assert_eq!(report.columns.len(), 2);
    assert_eq!(report.rows_sampled, 2);

    let via_profiler = Profiler::new()
        .format(FileFormat::Csv)
        .analyze_structure(tmp.path(), Some(1))
        .expect("profiler method");
    assert!(via_profiler.truncated);
    assert_eq!(
        via_profiler.truncation_reason.as_deref(),
        Some("max_rows(1)")
    );
}

#[cfg(feature = "async-streaming")]
#[test]
fn async_streaming_facade_reexports_compile() {
    use dataprof::{AsyncSourceInfo, AsyncStreamingProfiler, BytesSource};
    use dataprof_engines::streaming::{IncrementalProfiler, MemoryMappedCsvReader};

    let info = AsyncSourceInfo::new("inline", FileFormat::Csv).size_hint(Some(4));
    let _source = BytesSource::new(bytes::Bytes::from_static(b"a\n1\n"), info);
    let _profiler = AsyncStreamingProfiler::new();
    let _incremental = IncrementalProfiler::new()
        .chunk_size(ChunkSize::Fixed(256))
        .sampling(SamplingStrategy::None)
        .stop_condition(StopCondition::Never);
    let _reader_type_size = std::mem::size_of::<MemoryMappedCsvReader>();
}