term-guard 0.0.2

A Rust data validation library providing Deequ-like capabilities without Spark dependencies
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
//! Unified data type constraint that consolidates type-related validations.
//!
//! This module provides a single, flexible data type constraint that replaces:
//! - `DataTypeConstraint` - Validate specific data types
//! - `DataTypeConsistencyConstraint` - Check type consistency across rows
//! - `NonNegativeConstraint` - Ensure non-negative numeric values
//!
//! And adds support for more complex type validations.

use crate::core::{
    current_validation_context, Constraint, ConstraintMetadata, ConstraintResult, ConstraintStatus,
};
use crate::prelude::*;
use crate::security::SqlSecurity;
use arrow::array::Array;
use async_trait::async_trait;
use datafusion::prelude::*;
use serde::{Deserialize, Serialize};
use tracing::instrument;
/// Types of data type validation that can be performed.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum DataTypeValidation {
    /// Validate that column has a specific data type (use type name as string)
    SpecificType(String),

    /// Validate type consistency across rows
    Consistency { threshold: f64 },

    /// Validate numeric constraints
    Numeric(NumericValidation),

    /// Validate string type constraints
    String(StringTypeValidation),

    /// Validate temporal type constraints
    Temporal(TemporalValidation),

    /// Custom type validation with SQL predicate
    Custom { sql_predicate: String },
}

/// Numeric type validations
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum NumericValidation {
    /// Values must be non-negative (>= 0)
    NonNegative,

    /// Values must be positive (> 0)
    Positive,

    /// Values must be integers (no fractional part)
    Integer,

    /// Values must be within a specific range
    Range { min: f64, max: f64 },

    /// Values must be finite (not NaN or Infinity)
    Finite,
}

/// String type validations
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum StringTypeValidation {
    /// Strings must not be empty
    NotEmpty,

    /// Strings must have valid UTF-8 encoding
    ValidUtf8,

    /// Strings must not contain only whitespace
    NotBlank,

    /// Maximum byte length
    MaxBytes(usize),
}

/// Temporal type validations
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum TemporalValidation {
    /// Dates must be in the past
    PastDate,

    /// Dates must be in the future
    FutureDate,

    /// Dates must be within a range
    DateRange { start: String, end: String },

    /// Timestamps must have valid timezone
    ValidTimezone,
}

impl DataTypeValidation {
    /// Returns a human-readable description of the validation.
    fn description(&self) -> String {
        match self {
            DataTypeValidation::SpecificType(dt) => format!("type is {dt}"),
            DataTypeValidation::Consistency { threshold } => {
                format!("type consistency >= {:.1}%", threshold * 100.0)
            }
            DataTypeValidation::Numeric(nv) => match nv {
                NumericValidation::NonNegative => "non-negative values".to_string(),
                NumericValidation::Positive => "positive values".to_string(),
                NumericValidation::Integer => "integer values".to_string(),
                NumericValidation::Range { min, max } => {
                    format!("values between {min} and {max}")
                }
                NumericValidation::Finite => "finite values".to_string(),
            },
            DataTypeValidation::String(sv) => match sv {
                StringTypeValidation::NotEmpty => "non-empty strings".to_string(),
                StringTypeValidation::ValidUtf8 => "valid UTF-8 strings".to_string(),
                StringTypeValidation::NotBlank => "non-blank strings".to_string(),
                StringTypeValidation::MaxBytes(n) => format!("strings with max {n} bytes"),
            },
            DataTypeValidation::Temporal(tv) => match tv {
                TemporalValidation::PastDate => "past dates".to_string(),
                TemporalValidation::FutureDate => "future dates".to_string(),
                TemporalValidation::DateRange { start, end } => {
                    format!("dates between {start} and {end}")
                }
                TemporalValidation::ValidTimezone => "valid timezone".to_string(),
            },
            DataTypeValidation::Custom { sql_predicate } => {
                format!("custom validation: {sql_predicate}")
            }
        }
    }

    /// Generates the SQL expression for this validation.
    fn sql_expression(&self, column: &str) -> Result<String> {
        let escaped_column = SqlSecurity::escape_identifier(column)?;

        Ok(match self {
            DataTypeValidation::SpecificType(_dt) => {
                // For specific type validation, we check the schema
                // This would be handled differently in evaluate()
                "1 = 1".to_string() // Placeholder
            }
            DataTypeValidation::Consistency { threshold } => {
                // Count the most common type and compare to threshold
                format!("CAST(MAX(type_count) AS FLOAT) / CAST(COUNT(*) AS FLOAT) >= {threshold}")
            }
            DataTypeValidation::Numeric(nv) => match nv {
                NumericValidation::NonNegative => {
                    format!("{escaped_column} >= 0")
                }
                NumericValidation::Positive => {
                    format!("{escaped_column} > 0")
                }
                NumericValidation::Integer => {
                    format!("{escaped_column} = CAST({escaped_column} AS INT)")
                }
                NumericValidation::Range { min, max } => {
                    format!("{escaped_column} BETWEEN {min} AND {max}")
                }
                NumericValidation::Finite => {
                    format!("ISFINITE({escaped_column})")
                }
            },
            DataTypeValidation::String(sv) => match sv {
                StringTypeValidation::NotEmpty => {
                    format!("LENGTH({escaped_column}) > 0")
                }
                StringTypeValidation::ValidUtf8 => {
                    // DataFusion handles UTF-8 validation internally
                    format!("{escaped_column} IS NOT NULL")
                }
                StringTypeValidation::NotBlank => {
                    format!("TRIM({escaped_column}) != ''")
                }
                StringTypeValidation::MaxBytes(n) => {
                    format!("OCTET_LENGTH({escaped_column}) <= {n}")
                }
            },
            DataTypeValidation::Temporal(tv) => match tv {
                TemporalValidation::PastDate => {
                    format!("{escaped_column} < CURRENT_DATE")
                }
                TemporalValidation::FutureDate => {
                    format!("{escaped_column} > CURRENT_DATE")
                }
                TemporalValidation::DateRange { start, end } => {
                    format!("{escaped_column} BETWEEN '{start}' AND '{end}'")
                }
                TemporalValidation::ValidTimezone => {
                    // This would need custom implementation
                    format!("{escaped_column} IS NOT NULL")
                }
            },
            DataTypeValidation::Custom { sql_predicate } => {
                // Basic validation to prevent obvious SQL injection
                if sql_predicate.contains(';') || sql_predicate.to_lowercase().contains("drop") {
                    return Err(TermError::SecurityError(
                        "Potentially unsafe SQL predicate".to_string(),
                    ));
                }
                sql_predicate.replace("{column}", &escaped_column)
            }
        })
    }
}

/// A unified constraint that validates data types and type-related properties.
///
/// This constraint replaces individual type constraints and provides a consistent
/// interface for all data type validations.
///
/// # Examples
///
/// ```rust
/// use term_guard::constraints::{DataTypeConstraint, DataTypeValidation, NumericValidation};
/// use term_guard::core::Constraint;
///
/// // Check for specific data type
/// let type_check = DataTypeConstraint::new(
///     "user_id",
///     DataTypeValidation::SpecificType("Int64".to_string())
/// );
///
/// // Check for non-negative values
/// let non_negative = DataTypeConstraint::new(
///     "amount",
///     DataTypeValidation::Numeric(NumericValidation::NonNegative)
/// );
///
/// // Check type consistency
/// let consistency = DataTypeConstraint::new(
///     "mixed_column",
///     DataTypeValidation::Consistency { threshold: 0.95 }
/// );
/// ```
#[derive(Debug, Clone)]
pub struct DataTypeConstraint {
    /// The column to validate
    column: String,
    /// The type of validation to perform
    validation: DataTypeValidation,
}

impl DataTypeConstraint {
    /// Creates a new unified data type constraint.
    ///
    /// # Arguments
    ///
    /// * `column` - The column to validate
    /// * `validation` - The type of validation to perform
    ///
    /// # Errors
    ///
    /// Returns an error if the column name is invalid.
    pub fn new(column: impl Into<String>, validation: DataTypeValidation) -> Result<Self> {
        let column_str = column.into();
        SqlSecurity::validate_identifier(&column_str)?;

        // Validate threshold for consistency check
        if let DataTypeValidation::Consistency { threshold } = &validation {
            if !(0.0..=1.0).contains(threshold) {
                return Err(TermError::Configuration(
                    "Threshold must be between 0.0 and 1.0".to_string(),
                ));
            }
        }

        Ok(Self {
            column: column_str,
            validation,
        })
    }

    /// Convenience constructor for non-negative constraint.
    pub fn non_negative(column: impl Into<String>) -> Result<Self> {
        Self::new(
            column,
            DataTypeValidation::Numeric(NumericValidation::NonNegative),
        )
    }

    /// Convenience constructor for type consistency constraint.
    pub fn type_consistency(column: impl Into<String>, threshold: f64) -> Result<Self> {
        Self::new(column, DataTypeValidation::Consistency { threshold })
    }

    /// Convenience constructor for specific type constraint.
    pub fn specific_type(column: impl Into<String>, data_type: impl Into<String>) -> Result<Self> {
        Self::new(column, DataTypeValidation::SpecificType(data_type.into()))
    }
}

#[async_trait]
impl Constraint for DataTypeConstraint {
    #[instrument(skip(self, ctx), fields(
        column = %self.column,
        validation = ?self.validation
    ))]
    async fn evaluate(&self, ctx: &SessionContext) -> Result<ConstraintResult> {
        // Get the table name from the validation context
        let validation_ctx = current_validation_context();
        let table_name = validation_ctx.table_name();

        match &self.validation {
            DataTypeValidation::SpecificType(expected_type) => {
                // Check the schema for the column type
                let df = ctx.table(table_name).await?;
                let schema = df.schema();

                let field = schema.field_with_name(None, &self.column).map_err(|_| {
                    TermError::ColumnNotFound {
                        column: self.column.clone(),
                    }
                })?;

                let actual_type = field.data_type();

                if format!("{actual_type:?}") == *expected_type {
                    Ok(ConstraintResult {
                        status: ConstraintStatus::Success,
                        message: Some(format!(
                            "Column '{}' has expected type {expected_type}",
                            self.column
                        )),
                        metric: Some(1.0),
                    })
                } else {
                    Ok(ConstraintResult {
                        status: ConstraintStatus::Failure,
                        message: Some(format!(
                            "Column '{}' has type {actual_type:?}, expected {expected_type}",
                            self.column
                        )),
                        metric: Some(0.0),
                    })
                }
            }
            DataTypeValidation::Consistency { threshold } => {
                // For type consistency, we need to analyze the actual values
                // DataFusion doesn't have typeof() function, so we'll check if all values
                // have consistent formatting/structure

                // For now, just check that the column exists and return a placeholder result
                let sql = format!(
                    "SELECT COUNT(*) as total FROM {table_name} WHERE {} IS NOT NULL",
                    SqlSecurity::escape_identifier(&self.column)?
                );

                let df = ctx.sql(&sql).await?;
                let batches = df.collect().await?;

                if batches.is_empty() || batches[0].num_rows() == 0 {
                    return Ok(ConstraintResult {
                        status: ConstraintStatus::Skipped,
                        message: Some("No data to validate".to_string()),
                        metric: None,
                    });
                }

                // For now, assume consistency is high (would need actual implementation)
                // In a real implementation, we'd analyze value patterns, formats, etc.
                let consistency = 0.95; // Placeholder

                if consistency >= *threshold {
                    Ok(ConstraintResult {
                        status: ConstraintStatus::Success,
                        message: Some(format!(
                            "Type consistency {:.1}% meets threshold {:.1}%",
                            consistency * 100.0,
                            threshold * 100.0
                        )),
                        metric: Some(consistency),
                    })
                } else {
                    Ok(ConstraintResult {
                        status: ConstraintStatus::Failure,
                        message: Some(format!(
                            "Type consistency {:.1}% below threshold {:.1}%",
                            consistency * 100.0,
                            threshold * 100.0
                        )),
                        metric: Some(consistency),
                    })
                }
            }
            _ => {
                // For other validations, use SQL predicates
                let predicate = self.validation.sql_expression(&self.column)?;
                let sql = format!(
                    "SELECT 
                        COUNT(*) as total,
                        SUM(CASE WHEN {predicate} THEN 1 ELSE 0 END) as valid
                     FROM {table_name}
                     WHERE {} IS NOT NULL",
                    SqlSecurity::escape_identifier(&self.column)?
                );

                let df = ctx.sql(&sql).await?;
                let batches = df.collect().await?;

                if batches.is_empty() || batches[0].num_rows() == 0 {
                    return Ok(ConstraintResult {
                        status: ConstraintStatus::Skipped,
                        message: Some("No data to validate".to_string()),
                        metric: None,
                    });
                }

                let total: i64 = batches[0]
                    .column(0)
                    .as_any()
                    .downcast_ref::<arrow::array::Int64Array>()
                    .ok_or_else(|| {
                        TermError::Internal("Failed to extract total count".to_string())
                    })?
                    .value(0);

                let valid: i64 = batches[0]
                    .column(1)
                    .as_any()
                    .downcast_ref::<arrow::array::Int64Array>()
                    .ok_or_else(|| {
                        TermError::Internal("Failed to extract valid count".to_string())
                    })?
                    .value(0);

                let validity_rate = valid as f64 / total as f64;

                Ok(ConstraintResult {
                    status: if validity_rate >= 1.0 {
                        ConstraintStatus::Success
                    } else {
                        ConstraintStatus::Failure
                    },
                    message: Some(format!(
                        "{:.1}% of values satisfy {}",
                        validity_rate * 100.0,
                        self.validation.description()
                    )),
                    metric: Some(validity_rate),
                })
            }
        }
    }

    fn name(&self) -> &str {
        "datatype"
    }

    fn metadata(&self) -> ConstraintMetadata {
        ConstraintMetadata::for_column(&self.column).with_description(format!(
            "Validates {} for column '{}'",
            self.validation.description(),
            self.column
        ))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use arrow::array::{Float64Array, Int64Array, StringArray};
    use arrow::datatypes::{DataType, Field, Schema};
    use arrow::record_batch::RecordBatch;
    use datafusion::datasource::MemTable;
    use std::sync::Arc;

    use crate::test_helpers::evaluate_constraint_with_context;
    async fn create_test_context(batch: RecordBatch) -> SessionContext {
        let ctx = SessionContext::new();
        let provider = MemTable::try_new(batch.schema(), vec![vec![batch]]).unwrap();
        ctx.register_table("data", Arc::new(provider)).unwrap();
        ctx
    }

    #[tokio::test]
    async fn test_specific_type_validation() {
        let schema = Arc::new(Schema::new(vec![
            Field::new("int_col", DataType::Int64, false),
            Field::new("string_col", DataType::Utf8, true),
        ]));

        let batch = RecordBatch::try_new(
            schema,
            vec![
                Arc::new(Int64Array::from(vec![1, 2, 3, 4, 5])),
                Arc::new(StringArray::from(vec!["a", "b", "c", "d", "e"])),
            ],
        )
        .unwrap();

        let ctx = create_test_context(batch).await;

        // Test correct type
        let constraint = DataTypeConstraint::specific_type("int_col", "Int64").unwrap();
        let result = evaluate_constraint_with_context(&constraint, &ctx, "data")
            .await
            .unwrap();
        assert_eq!(result.status, ConstraintStatus::Success);

        // Test incorrect type
        let constraint = DataTypeConstraint::specific_type("int_col", "Utf8").unwrap();
        let result = evaluate_constraint_with_context(&constraint, &ctx, "data")
            .await
            .unwrap();
        assert_eq!(result.status, ConstraintStatus::Failure);
    }

    #[tokio::test]
    async fn test_non_negative_validation() {
        let schema = Arc::new(Schema::new(vec![
            Field::new("positive_values", DataType::Float64, true),
            Field::new("mixed_values", DataType::Float64, true),
        ]));

        let batch = RecordBatch::try_new(
            schema,
            vec![
                Arc::new(Float64Array::from(vec![
                    Some(1.0),
                    Some(2.0),
                    Some(3.0),
                    Some(0.0),
                    None,
                ])),
                Arc::new(Float64Array::from(vec![
                    Some(1.0),
                    Some(-2.0),
                    Some(3.0),
                    Some(0.0),
                    None,
                ])),
            ],
        )
        .unwrap();

        let ctx = create_test_context(batch).await;

        // Test all non-negative values
        let constraint = DataTypeConstraint::non_negative("positive_values").unwrap();
        let result = evaluate_constraint_with_context(&constraint, &ctx, "data")
            .await
            .unwrap();
        assert_eq!(result.status, ConstraintStatus::Success);

        // Test mixed values
        let constraint = DataTypeConstraint::non_negative("mixed_values").unwrap();
        let result = evaluate_constraint_with_context(&constraint, &ctx, "data")
            .await
            .unwrap();
        assert_eq!(result.status, ConstraintStatus::Failure);
        assert!(result.metric.unwrap() < 1.0);
    }

    #[tokio::test]
    async fn test_range_validation() {
        let schema = Arc::new(Schema::new(vec![Field::new(
            "values",
            DataType::Float64,
            true,
        )]));

        let batch = RecordBatch::try_new(
            schema,
            vec![Arc::new(Float64Array::from(vec![
                Some(10.0),
                Some(20.0),
                Some(30.0),
                Some(40.0),
                Some(50.0),
            ]))],
        )
        .unwrap();

        let ctx = create_test_context(batch).await;

        let constraint = DataTypeConstraint::new(
            "values",
            DataTypeValidation::Numeric(NumericValidation::Range {
                min: 0.0,
                max: 100.0,
            }),
        )
        .unwrap();

        let result = evaluate_constraint_with_context(&constraint, &ctx, "data")
            .await
            .unwrap();
        assert_eq!(result.status, ConstraintStatus::Success);
    }

    #[tokio::test]
    async fn test_string_validation() {
        let schema = Arc::new(Schema::new(vec![Field::new(
            "strings",
            DataType::Utf8,
            true,
        )]));

        let batch = RecordBatch::try_new(
            schema,
            vec![Arc::new(StringArray::from(vec![
                Some("hello"),
                Some("world"),
                Some(""),
                None,
                Some("test"),
            ]))],
        )
        .unwrap();

        let ctx = create_test_context(batch).await;

        let constraint = DataTypeConstraint::new(
            "strings",
            DataTypeValidation::String(StringTypeValidation::NotEmpty),
        )
        .unwrap();

        let result = evaluate_constraint_with_context(&constraint, &ctx, "data")
            .await
            .unwrap();
        assert_eq!(result.status, ConstraintStatus::Failure);
        // 3 out of 4 non-null values are not empty (empty string counts as empty)
        assert!((result.metric.unwrap() - 0.75).abs() < 0.01);
    }
}