dataprof 0.7.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
//! Database table sampling strategies for large datasets

use crate::core::errors::DataProfilerError;
use crate::database::security::{validate_base_query, validate_sql_identifier};
use serde::{Deserialize, Serialize};

/// Configuration for database table sampling
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SamplingConfig {
    /// Sampling strategy to use
    pub strategy: SamplingStrategy,
    /// Target sample size (number of rows)
    pub sample_size: usize,
    /// Random seed for reproducible sampling (optional)
    pub seed: Option<u64>,
    /// Whether to stratify sampling by a column (optional)
    pub stratify_column: Option<String>,
}

/// Available sampling strategies for large databases
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum SamplingStrategy {
    /// Simple random sampling - fastest but may skip patterns
    Random,
    /// Systematic sampling - every nth row
    Systematic,
    /// Reservoir sampling - single pass, memory efficient
    Reservoir,
    /// Stratified sampling - maintains class distribution
    Stratified,
    /// Time-based sampling - for temporal data
    Temporal { column_name: String },
    /// Multi-stage sampling - first sample tables, then rows
    MultiStage,
}

impl Default for SamplingConfig {
    fn default() -> Self {
        Self {
            strategy: SamplingStrategy::Reservoir,
            sample_size: 10000,
            seed: None,
            stratify_column: None,
        }
    }
}

impl SamplingConfig {
    /// Create a new sampling config for quick analysis
    pub fn quick_sample(sample_size: usize) -> Self {
        Self {
            strategy: SamplingStrategy::Random,
            sample_size,
            seed: Some(42), // Fixed seed for reproducibility
            stratify_column: None,
        }
    }

    /// Create a config for representative sampling
    pub fn representative_sample(sample_size: usize, stratify_column: Option<String>) -> Self {
        Self {
            strategy: if stratify_column.is_some() {
                SamplingStrategy::Stratified
            } else {
                SamplingStrategy::Systematic
            },
            sample_size,
            seed: Some(42),
            stratify_column,
        }
    }

    /// Create a config for temporal data sampling
    pub fn temporal_sample(sample_size: usize, time_column: String) -> Self {
        Self {
            strategy: SamplingStrategy::Temporal {
                column_name: time_column,
            },
            sample_size,
            seed: Some(42),
            stratify_column: None,
        }
    }

    /// Generate the appropriate SQL sampling query
    pub fn generate_sample_query(
        &self,
        base_query: &str,
        total_rows: u64,
    ) -> Result<String, DataProfilerError> {
        // Determine if we need sampling
        if total_rows as usize <= self.sample_size {
            return Ok(base_query.to_string());
        }

        let sampling_ratio = self.sample_size as f64 / total_rows as f64;

        match &self.strategy {
            SamplingStrategy::Random => {
                let seed = self.seed.unwrap_or(42);
                if base_query.trim().to_uppercase().starts_with("SELECT") {
                    let validated_query = validate_base_query(base_query)?;
                    Ok(format!(
                        "SELECT * FROM ({}) AS sample_subquery ORDER BY RANDOM({}) LIMIT {}",
                        validated_query, seed, self.sample_size
                    ))
                } else {
                    validate_sql_identifier(base_query)?;
                    Ok(format!(
                        "SELECT * FROM {} ORDER BY RANDOM({}) LIMIT {}",
                        base_query, seed, self.sample_size
                    ))
                }
            }
            SamplingStrategy::Systematic => {
                let step = (total_rows as f64 / self.sample_size as f64).ceil() as u64;
                if base_query.trim().to_uppercase().starts_with("SELECT") {
                    let validated_query = validate_base_query(base_query)?;
                    Ok(format!(
                        "SELECT * FROM (SELECT *, ROW_NUMBER() OVER() as rn FROM ({})) AS numbered WHERE rn % {} = 1",
                        validated_query, step
                    ))
                } else {
                    validate_sql_identifier(base_query)?;
                    Ok(format!(
                        "SELECT * FROM (SELECT *, ROW_NUMBER() OVER() as rn FROM {}) AS numbered WHERE rn % {} = 1",
                        base_query, step
                    ))
                }
            }
            SamplingStrategy::Reservoir => {
                // Reservoir sampling uses TABLESAMPLE if available, otherwise falls back to random
                self.generate_tablesample_query(base_query, sampling_ratio)
            }
            SamplingStrategy::Stratified => {
                if let Some(stratify_col) = &self.stratify_column {
                    validate_sql_identifier(stratify_col)?;
                    self.generate_stratified_query(base_query, stratify_col, total_rows)
                } else {
                    // Fall back to random sampling with current configuration
                    let mut fallback_config = self.clone();
                    fallback_config.strategy = SamplingStrategy::Random;
                    fallback_config.generate_sample_query(base_query, total_rows)
                }
            }
            SamplingStrategy::Temporal { column_name } => {
                validate_sql_identifier(column_name)?;
                self.generate_temporal_query(base_query, column_name, total_rows)
            }
            SamplingStrategy::MultiStage => {
                // For multi-stage, we'll use systematic sampling as base
                let mut config = self.clone();
                config.strategy = SamplingStrategy::Systematic;
                config.generate_sample_query(base_query, total_rows)
            }
        }
    }

    /// Generate a TABLESAMPLE query (PostgreSQL/SQL Server)
    fn generate_tablesample_query(
        &self,
        base_query: &str,
        sampling_ratio: f64,
    ) -> Result<String, DataProfilerError> {
        let percentage = (sampling_ratio * 100.0).min(100.0);

        if base_query.trim().to_uppercase().starts_with("SELECT") {
            // Complex query - use subquery approach
            let validated_query = validate_base_query(base_query)?;
            let seed = self.seed.unwrap_or(42);
            Ok(format!(
                "SELECT * FROM ({}) AS sample_subquery ORDER BY RANDOM({}) LIMIT {}",
                validated_query, seed, self.sample_size
            ))
        } else {
            // Simple table - can use TABLESAMPLE
            validate_sql_identifier(base_query)?;
            Ok(format!(
                "SELECT * FROM {} TABLESAMPLE SYSTEM ({:.2}) LIMIT {}",
                base_query, percentage, self.sample_size
            ))
        }
    }

    /// Generate stratified sampling query
    fn generate_stratified_query(
        &self,
        base_query: &str,
        stratify_col: &str,
        _total_rows: u64,
    ) -> Result<String, DataProfilerError> {
        // Validate inputs - stratify_col already validated by caller
        let sample_per_stratum = self.sample_size / 10; // Assume up to 10 strata for simplicity

        if base_query.trim().to_uppercase().starts_with("SELECT") {
            let validated_query = validate_base_query(base_query)?;
            Ok(format!(
                r#"
                SELECT * FROM (
                    SELECT *, ROW_NUMBER() OVER(PARTITION BY {} ORDER BY RANDOM()) as stratum_rn
                    FROM ({}) AS base_query
                ) stratified
                WHERE stratum_rn <= {}
                LIMIT {}
                "#,
                stratify_col, validated_query, sample_per_stratum, self.sample_size
            ))
        } else {
            validate_sql_identifier(base_query)?;
            Ok(format!(
                r#"
                SELECT * FROM (
                    SELECT *, ROW_NUMBER() OVER(PARTITION BY {} ORDER BY RANDOM()) as stratum_rn
                    FROM {}
                ) stratified
                WHERE stratum_rn <= {}
                LIMIT {}
                "#,
                stratify_col, base_query, sample_per_stratum, self.sample_size
            ))
        }
    }

    /// Generate temporal sampling query
    fn generate_temporal_query(
        &self,
        base_query: &str,
        time_col: &str,
        total_rows: u64,
    ) -> Result<String, DataProfilerError> {
        // time_col already validated by caller
        // Sample evenly across time periods
        if base_query.trim().to_uppercase().starts_with("SELECT") {
            let validated_query = validate_base_query(base_query)?;
            Ok(format!(
                r#"
                SELECT * FROM (
                    SELECT *, ROW_NUMBER() OVER(ORDER BY {}) as time_rn
                    FROM ({}) AS base_query
                ) temporal
                WHERE time_rn % {} = 1
                LIMIT {}
                "#,
                time_col,
                validated_query,
                (total_rows as f64 / self.sample_size as f64).ceil() as u64,
                self.sample_size
            ))
        } else {
            validate_sql_identifier(base_query)?;
            Ok(format!(
                r#"
                SELECT * FROM (
                    SELECT *, ROW_NUMBER() OVER(ORDER BY {}) as time_rn
                    FROM {}
                ) temporal
                WHERE time_rn % {} = 1
                LIMIT {}
                "#,
                time_col,
                base_query,
                (total_rows as f64 / self.sample_size as f64).ceil() as u64,
                self.sample_size
            ))
        }
    }
}

/// Information about the sampling process
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SampleInfo {
    /// Total rows in the original table/query
    pub total_rows: u64,
    /// Number of rows in the sample
    pub sampled_rows: u64,
    /// Sampling ratio (0.0 - 1.0)
    pub sampling_ratio: f64,
    /// Sampling strategy used
    pub strategy: SamplingStrategy,
    /// Whether the sample is representative
    pub is_representative: bool,
    /// Estimated confidence interval for statistics
    pub confidence_margin: f64,
}

impl SampleInfo {
    /// Create new sample info
    pub fn new(total_rows: u64, sampled_rows: u64, strategy: SamplingStrategy) -> Self {
        let sampling_ratio = if total_rows > 0 {
            sampled_rows as f64 / total_rows as f64
        } else {
            1.0
        };

        // Estimate representativeness based on sample size and strategy
        let is_representative = match strategy {
            SamplingStrategy::Systematic | SamplingStrategy::Stratified => sampled_rows >= 1000,
            SamplingStrategy::Random | SamplingStrategy::Reservoir => sampled_rows >= 500,
            SamplingStrategy::Temporal { .. } => sampled_rows >= 2000, // Temporal needs more samples
            SamplingStrategy::MultiStage => sampled_rows >= 1500,
        };

        // Calculate confidence margin (simplified)
        let confidence_margin = if sampled_rows > 0 {
            1.96 / (sampled_rows as f64).sqrt() // 95% confidence interval
        } else {
            1.0
        };

        Self {
            total_rows,
            sampled_rows,
            sampling_ratio,
            strategy,
            is_representative,
            confidence_margin,
        }
    }

    /// Get a warning message if the sample might not be representative
    pub fn get_warning(&self) -> Option<String> {
        if !self.is_representative {
            Some(format!(
                "Sample size ({}) may be too small for reliable analysis. \
                Consider increasing sample size for better representation.",
                self.sampled_rows
            ))
        } else if self.confidence_margin > 0.1 {
            Some(format!(
                "Large confidence margin ({:.2}). \
                Statistics may have high uncertainty.",
                self.confidence_margin
            ))
        } else {
            None
        }
    }

    /// Get recommended actions for improving sample quality
    pub fn get_recommendations(&self) -> Vec<String> {
        let mut recommendations = Vec::new();

        if self.sampled_rows < 1000 {
            recommendations.push(
                "Increase sample size to at least 1000 rows for better reliability".to_string(),
            );
        }

        if self.sampling_ratio < 0.01 && self.total_rows > 100000 {
            recommendations.push(
                "Consider stratified sampling for large datasets to ensure representativeness"
                    .to_string(),
            );
        }

        match &self.strategy {
            SamplingStrategy::Random if self.total_rows > 1000000 => {
                recommendations.push(
                    "For very large datasets, consider systematic or reservoir sampling"
                        .to_string(),
                );
            }
            SamplingStrategy::Temporal { .. } if self.sampled_rows < 2000 => {
                recommendations.push(
                    "Temporal sampling requires larger samples to capture time patterns"
                        .to_string(),
                );
            }
            _ => {}
        }

        recommendations
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_generate_random_sample_query() {
        let config = SamplingConfig::quick_sample(1000);
        let query = config
            .generate_sample_query("users", 10000)
            .expect("Failed to generate sample query");

        assert!(query.contains("RANDOM"));
        assert!(query.contains("LIMIT 1000"));
    }

    #[test]
    fn test_generate_systematic_sample_query() {
        let config = SamplingConfig {
            strategy: SamplingStrategy::Systematic,
            sample_size: 1000,
            seed: Some(42),
            stratify_column: None,
        };

        let query = config
            .generate_sample_query("orders", 10000)
            .expect("Failed to generate sample query");

        assert!(query.contains("ROW_NUMBER()"));
        assert!(query.contains("% 10 = 1")); // Every 10th row
    }

    #[test]
    fn test_sample_info_calculations() {
        let info = SampleInfo::new(10000, 1000, SamplingStrategy::Random);

        assert_eq!(info.sampling_ratio, 0.1);
        assert!(info.is_representative);
        assert!(info.confidence_margin < 0.1);
    }

    #[test]
    fn test_small_sample_warning() {
        let info = SampleInfo::new(10000, 100, SamplingStrategy::Random);

        assert!(!info.is_representative);
        assert!(info.get_warning().is_some());
        assert!(!info.get_recommendations().is_empty());
    }
}