datasynth-core 2.4.0

Core domain models, traits, and distributions for synthetic enterprise data generation
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
//! Post-processor trait for data quality variations and other post-generation transformations.
//!
//! Post-processors modify records after generation to inject data quality issues,
//! format variations, typos, and other realistic flakiness. They produce labels
//! that can be used for ML training.

use crate::error::SynthResult;
use std::collections::HashMap;

/// Context passed to post-processors during processing.
#[derive(Debug, Clone, Default)]
pub struct ProcessContext {
    /// Current record index in the batch
    pub record_index: usize,
    /// Total records in the batch
    pub batch_size: usize,
    /// Current output format (csv, json, parquet)
    pub output_format: Option<String>,
    /// Additional context data
    pub metadata: HashMap<String, String>,
}

impl ProcessContext {
    /// Create a new processing context.
    pub fn new(record_index: usize, batch_size: usize) -> Self {
        Self {
            record_index,
            batch_size,
            output_format: None,
            metadata: HashMap::new(),
        }
    }

    /// Set the output format.
    pub fn with_format(mut self, format: impl Into<String>) -> Self {
        self.output_format = Some(format.into());
        self
    }

    /// Add metadata.
    pub fn with_metadata(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.metadata.insert(key.into(), value.into());
        self
    }

    /// Check if processing first record.
    pub fn is_first(&self) -> bool {
        self.record_index == 0
    }

    /// Check if processing last record.
    pub fn is_last(&self) -> bool {
        self.record_index == self.batch_size.saturating_sub(1)
    }
}

/// Statistics from a post-processor run.
#[derive(Debug, Clone, Default)]
pub struct ProcessorStats {
    /// Number of records processed
    pub records_processed: u64,
    /// Number of records modified
    pub records_modified: u64,
    /// Number of labels generated
    pub labels_generated: u64,
    /// Number of errors encountered
    pub errors_encountered: u64,
    /// Processing time in microseconds
    pub processing_time_us: u64,
}

impl ProcessorStats {
    /// Calculate modification rate.
    pub fn modification_rate(&self) -> f64 {
        if self.records_processed == 0 {
            0.0
        } else {
            self.records_modified as f64 / self.records_processed as f64
        }
    }

    /// Merge stats from another processor.
    pub fn merge(&mut self, other: &ProcessorStats) {
        self.records_processed += other.records_processed;
        self.records_modified += other.records_modified;
        self.labels_generated += other.labels_generated;
        self.errors_encountered += other.errors_encountered;
        self.processing_time_us += other.processing_time_us;
    }
}

/// Core trait for post-processors that modify records and generate labels.
///
/// Post-processors are applied after generation to inject realistic data quality
/// issues. Each processor can modify records in place and generate labels
/// describing the modifications for ML training.
pub trait PostProcessor: Send + Sync {
    /// The type of records this processor modifies.
    type Record;
    /// The type of labels this processor produces.
    type Label;

    /// Process a single record, potentially modifying it and generating labels.
    ///
    /// Returns a vector of labels describing any modifications made.
    fn process(
        &mut self,
        record: &mut Self::Record,
        context: &ProcessContext,
    ) -> SynthResult<Vec<Self::Label>>;

    /// Process a batch of records.
    ///
    /// Default implementation calls process for each record.
    fn process_batch(
        &mut self,
        records: &mut [Self::Record],
        base_context: &ProcessContext,
    ) -> SynthResult<Vec<Self::Label>> {
        let mut all_labels = Vec::new();
        let batch_size = records.len();

        for (i, record) in records.iter_mut().enumerate() {
            let context = ProcessContext {
                record_index: i,
                batch_size,
                output_format: base_context.output_format.clone(),
                metadata: base_context.metadata.clone(),
            };
            let labels = self.process(record, &context)?;
            all_labels.extend(labels);
        }

        Ok(all_labels)
    }

    /// Get the name of this processor.
    fn name(&self) -> &'static str;

    /// Check if this processor is enabled.
    fn is_enabled(&self) -> bool;

    /// Get processing statistics.
    fn stats(&self) -> ProcessorStats;

    /// Reset statistics (for testing or between batches).
    fn reset_stats(&mut self);
}

/// A pipeline of post-processors applied in sequence.
pub struct PostProcessorPipeline<R, L> {
    processors: Vec<Box<dyn PostProcessor<Record = R, Label = L>>>,
    stats: ProcessorStats,
}

impl<R, L> PostProcessorPipeline<R, L> {
    /// Create a new empty pipeline.
    pub fn new() -> Self {
        Self {
            processors: Vec::new(),
            stats: ProcessorStats::default(),
        }
    }

    /// Add a processor to the pipeline.
    pub fn add<P>(&mut self, processor: P)
    where
        P: PostProcessor<Record = R, Label = L> + 'static,
    {
        self.processors.push(Box::new(processor));
    }

    /// Add a processor and return self for chaining.
    pub fn with<P>(mut self, processor: P) -> Self
    where
        P: PostProcessor<Record = R, Label = L> + 'static,
    {
        self.add(processor);
        self
    }

    /// Process a single record through all processors.
    pub fn process(&mut self, record: &mut R, context: &ProcessContext) -> SynthResult<Vec<L>> {
        let mut all_labels = Vec::new();

        for processor in &mut self.processors {
            if processor.is_enabled() {
                let labels = processor.process(record, context)?;
                all_labels.extend(labels);
            }
        }

        self.stats.records_processed += 1;
        if !all_labels.is_empty() {
            self.stats.records_modified += 1;
        }
        self.stats.labels_generated += all_labels.len() as u64;

        Ok(all_labels)
    }

    /// Process a batch of records through all processors.
    pub fn process_batch(
        &mut self,
        records: &mut [R],
        base_context: &ProcessContext,
    ) -> SynthResult<Vec<L>> {
        let mut all_labels = Vec::new();
        let batch_size = records.len();

        for (i, record) in records.iter_mut().enumerate() {
            let context = ProcessContext {
                record_index: i,
                batch_size,
                output_format: base_context.output_format.clone(),
                metadata: base_context.metadata.clone(),
            };
            let labels = self.process(record, &context)?;
            all_labels.extend(labels);
        }

        Ok(all_labels)
    }

    /// Get aggregate statistics for the pipeline.
    ///
    /// Returns the pipeline's own stats tracking records processed through
    /// the entire pipeline. Use `processor_stats()` to get individual
    /// processor statistics.
    pub fn stats(&self) -> ProcessorStats {
        self.stats.clone()
    }

    /// Get individual processor statistics.
    pub fn processor_stats(&self) -> Vec<(&'static str, ProcessorStats)> {
        self.processors
            .iter()
            .map(|p| (p.name(), p.stats()))
            .collect()
    }

    /// Check if pipeline has any enabled processors.
    pub fn has_enabled_processors(&self) -> bool {
        self.processors.iter().any(|p| p.is_enabled())
    }

    /// Get number of processors in the pipeline.
    pub fn len(&self) -> usize {
        self.processors.len()
    }

    /// Check if pipeline is empty.
    pub fn is_empty(&self) -> bool {
        self.processors.is_empty()
    }

    /// Reset all statistics.
    pub fn reset_stats(&mut self) {
        self.stats = ProcessorStats::default();
        for processor in &mut self.processors {
            processor.reset_stats();
        }
    }
}

impl<R, L> Default for PostProcessorPipeline<R, L> {
    fn default() -> Self {
        Self::new()
    }
}

/// A no-op processor that passes records through unchanged.
pub struct PassthroughProcessor<R, L> {
    enabled: bool,
    stats: ProcessorStats,
    _phantom: std::marker::PhantomData<(R, L)>,
}

impl<R, L> PassthroughProcessor<R, L> {
    /// Create a new passthrough processor.
    pub fn new() -> Self {
        Self {
            enabled: true,
            stats: ProcessorStats::default(),
            _phantom: std::marker::PhantomData,
        }
    }

    /// Create a disabled passthrough processor.
    pub fn disabled() -> Self {
        Self {
            enabled: false,
            stats: ProcessorStats::default(),
            _phantom: std::marker::PhantomData,
        }
    }
}

impl<R, L> Default for PassthroughProcessor<R, L> {
    fn default() -> Self {
        Self::new()
    }
}

impl<R: Send + Sync, L: Send + Sync> PostProcessor for PassthroughProcessor<R, L> {
    type Record = R;
    type Label = L;

    fn process(
        &mut self,
        _record: &mut Self::Record,
        _context: &ProcessContext,
    ) -> SynthResult<Vec<Self::Label>> {
        self.stats.records_processed += 1;
        Ok(Vec::new())
    }

    fn name(&self) -> &'static str {
        "passthrough"
    }

    fn is_enabled(&self) -> bool {
        self.enabled
    }

    fn stats(&self) -> ProcessorStats {
        self.stats.clone()
    }

    fn reset_stats(&mut self) {
        self.stats = ProcessorStats::default();
    }
}

/// Builder for creating post-processor pipelines.
pub struct PipelineBuilder<R, L> {
    pipeline: PostProcessorPipeline<R, L>,
}

impl<R, L> PipelineBuilder<R, L> {
    /// Create a new pipeline builder.
    pub fn new() -> Self {
        Self {
            pipeline: PostProcessorPipeline::new(),
        }
    }

    /// Add a processor to the pipeline.
    #[allow(clippy::should_implement_trait)]
    pub fn add<P>(mut self, processor: P) -> Self
    where
        P: PostProcessor<Record = R, Label = L> + 'static,
    {
        self.pipeline.add(processor);
        self
    }

    /// Conditionally add a processor.
    pub fn add_if<P>(mut self, condition: bool, processor: P) -> Self
    where
        P: PostProcessor<Record = R, Label = L> + 'static,
    {
        if condition {
            self.pipeline.add(processor);
        }
        self
    }

    /// Build the pipeline.
    pub fn build(self) -> PostProcessorPipeline<R, L> {
        self.pipeline
    }
}

impl<R, L> Default for PipelineBuilder<R, L> {
    fn default() -> Self {
        Self::new()
    }
}

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

    // Simple test record type
    #[derive(Debug, Clone)]
    struct TestRecord {
        value: String,
    }

    // Test label type — fields are written but not read; the struct serves as
    // a type parameter for PostProcessor and Pipeline tests.
    #[allow(dead_code)]
    #[derive(Debug, Clone)]
    struct TestLabel {
        field: String,
        change: String,
    }

    // Test processor that uppercases strings
    struct UppercaseProcessor {
        enabled: bool,
        stats: ProcessorStats,
    }

    impl UppercaseProcessor {
        fn new() -> Self {
            Self {
                enabled: true,
                stats: ProcessorStats::default(),
            }
        }
    }

    impl PostProcessor for UppercaseProcessor {
        type Record = TestRecord;
        type Label = TestLabel;

        fn process(
            &mut self,
            record: &mut Self::Record,
            _context: &ProcessContext,
        ) -> SynthResult<Vec<Self::Label>> {
            self.stats.records_processed += 1;
            let original = record.value.clone();
            record.value = record.value.to_uppercase();
            if record.value != original {
                self.stats.records_modified += 1;
                self.stats.labels_generated += 1;
                Ok(vec![TestLabel {
                    field: "value".to_string(),
                    change: format!("{} -> {}", original, record.value),
                }])
            } else {
                Ok(vec![])
            }
        }

        fn name(&self) -> &'static str {
            "uppercase"
        }

        fn is_enabled(&self) -> bool {
            self.enabled
        }

        fn stats(&self) -> ProcessorStats {
            self.stats.clone()
        }

        fn reset_stats(&mut self) {
            self.stats = ProcessorStats::default();
        }
    }

    #[test]
    fn test_pipeline_basic() {
        let mut pipeline = PostProcessorPipeline::new();
        pipeline.add(UppercaseProcessor::new());

        let mut record = TestRecord {
            value: "hello".to_string(),
        };
        let context = ProcessContext::new(0, 1);

        let labels = pipeline.process(&mut record, &context).unwrap();

        assert_eq!(record.value, "HELLO");
        assert_eq!(labels.len(), 1);
        assert_eq!(labels[0].field, "value");
    }

    #[test]
    fn test_pipeline_batch() {
        let mut pipeline = PostProcessorPipeline::new();
        pipeline.add(UppercaseProcessor::new());

        let mut records = vec![
            TestRecord {
                value: "a".to_string(),
            },
            TestRecord {
                value: "b".to_string(),
            },
            TestRecord {
                value: "c".to_string(),
            },
        ];
        let context = ProcessContext::new(0, 3);

        let labels = pipeline.process_batch(&mut records, &context).unwrap();

        assert_eq!(records[0].value, "A");
        assert_eq!(records[1].value, "B");
        assert_eq!(records[2].value, "C");
        assert_eq!(labels.len(), 3);
    }

    #[test]
    fn test_pipeline_stats() {
        let mut pipeline = PostProcessorPipeline::new();
        pipeline.add(UppercaseProcessor::new());

        let context = ProcessContext::new(0, 1);

        for _ in 0..5 {
            let mut record = TestRecord {
                value: "test".to_string(),
            };
            let _ = pipeline.process(&mut record, &context);
        }

        let stats = pipeline.stats();
        assert_eq!(stats.records_processed, 5);
        assert_eq!(stats.records_modified, 5);
    }

    #[test]
    fn test_passthrough_processor() {
        let mut processor = PassthroughProcessor::<TestRecord, TestLabel>::new();
        let mut record = TestRecord {
            value: "unchanged".to_string(),
        };
        let context = ProcessContext::new(0, 1);

        let labels = processor.process(&mut record, &context).unwrap();

        assert_eq!(record.value, "unchanged");
        assert!(labels.is_empty());
    }

    #[test]
    fn test_pipeline_builder() {
        let pipeline: PostProcessorPipeline<TestRecord, TestLabel> = PipelineBuilder::new()
            .add(UppercaseProcessor::new())
            .add_if(false, PassthroughProcessor::new())
            .build();

        assert_eq!(pipeline.len(), 1);
    }
}