tinyetl 0.3.0

Fast, zero-config ETL in a single binary for transforming data between formats and databases
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
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::Path;
use rust_decimal::Decimal;
use regex::Regex;
use crate::Result;

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum DataType {
    String,
    Integer,
    Decimal,
    Boolean,
    Date,
    DateTime,
    Null,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SchemaFileColumn {
    pub name: String,
    #[serde(rename = "type")]
    pub data_type: String,
    pub nullable: bool,
    pub pattern: Option<String>,
    pub default: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SchemaFile {
    pub columns: Vec<SchemaFileColumn>,
}

impl SchemaFile {
    pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Self> {
        let content = std::fs::read_to_string(path)?;
        let schema_file: SchemaFile = serde_yaml::from_str(&content)
            .map_err(|e| crate::TinyEtlError::Configuration(format!("Invalid schema file: {}", e)))?;
        
        // Validate the schema file
        schema_file.validate()?;
        
        Ok(schema_file)
    }
    
    pub fn validate(&self) -> Result<()> {
        for column in &self.columns {
            // Validate data type
            match column.data_type.to_lowercase().as_str() {
                "string" | "integer" | "decimal" | "boolean" | "date" | "datetime" => {},
                _ => return Err(crate::TinyEtlError::Configuration(
                    format!("Invalid data type '{}' for column '{}'", column.data_type, column.name)
                )),
            }
            
            // Validate regex pattern if provided
            if let Some(pattern) = &column.pattern {
                Regex::new(pattern).map_err(|e| 
                    crate::TinyEtlError::Configuration(
                        format!("Invalid regex pattern '{}' for column '{}': {}", pattern, column.name, e)
                    )
                )?;
            }
        }
        Ok(())
    }
    
    pub fn to_schema(&self) -> Result<Schema> {
        let columns = self.columns.iter().map(|col| {
            let data_type = match col.data_type.to_lowercase().as_str() {
                "string" => DataType::String,
                "integer" => DataType::Integer,
                "decimal" => DataType::Decimal,
                "boolean" => DataType::Boolean,
                "date" => DataType::Date,
                "datetime" => DataType::DateTime,
                _ => DataType::String, // Already validated, so this shouldn't happen
            };
            
            Column {
                name: col.name.clone(),
                data_type,
                nullable: col.nullable,
            }
        }).collect();
        
        Ok(Schema {
            columns,
            estimated_rows: None,
            primary_key_candidate: None,
        })
    }
    
    pub fn validate_and_transform_row(&self, row: &mut Row) -> Result<()> {
        for schema_col in &self.columns {
            let value = row.get(&schema_col.name);
            
            // Check for required columns
            if !schema_col.nullable && (value.is_none() || matches!(value, Some(Value::Null))) {
                // Apply default if available
                if let Some(default_str) = &schema_col.default {
                    let default_value = self.parse_default_value(default_str, &schema_col.data_type)?;
                    row.insert(schema_col.name.clone(), default_value);
                } else {
                    return Err(crate::TinyEtlError::DataValidation(
                        format!("Required column '{}' is missing or null", schema_col.name)
                    ));
                }
            }
            
            // Validate existing values
            if let Some(val) = row.get(&schema_col.name) {
                self.validate_column_value(val, schema_col)?;
            }
        }
        Ok(())
    }
    
    fn validate_column_value(&self, value: &Value, schema_col: &SchemaFileColumn) -> Result<()> {
        // Skip null values if column is nullable
        if matches!(value, Value::Null) && schema_col.nullable {
            return Ok(());
        }
        
        // Validate data type
        let expected_type = match schema_col.data_type.to_lowercase().as_str() {
            "string" => DataType::String,
            "integer" => DataType::Integer,
            "decimal" => DataType::Decimal,
            "boolean" => DataType::Boolean,
            "date" => DataType::Date,
            "datetime" => DataType::DateTime,
            _ => return Err(crate::TinyEtlError::DataValidation(
                format!("Unknown data type '{}' for column '{}'", schema_col.data_type, schema_col.name)
            )),
        };
        
        let actual_type = SchemaInferer::infer_type(value);
        if actual_type != expected_type && actual_type != DataType::Null {
            return Err(crate::TinyEtlError::DataValidation(
                format!("Column '{}' expected type {:?}, got {:?}", schema_col.name, expected_type, actual_type)
            ));
        }
        
        // Validate pattern for string values
        if let (Some(pattern), Value::String(s)) = (&schema_col.pattern, value) {
            let regex = Regex::new(pattern).unwrap(); // Already validated in validate()
            if !regex.is_match(s) {
                return Err(crate::TinyEtlError::DataValidation(
                    format!("Column '{}' value '{}' does not match pattern '{}'", schema_col.name, s, pattern)
                ));
            }
        }
        
        Ok(())
    }
    
    fn parse_default_value(&self, default_str: &str, data_type: &str) -> Result<Value> {
        match data_type.to_lowercase().as_str() {
            "string" => Ok(Value::String(default_str.to_string())),
            "integer" => {
                let parsed = default_str.parse::<i64>()
                    .map_err(|_| crate::TinyEtlError::Configuration(
                        format!("Invalid default integer value: '{}'", default_str)
                    ))?;
                Ok(Value::Integer(parsed))
            },
            "decimal" => {
                let parsed = default_str.parse::<Decimal>()
                    .map_err(|_| crate::TinyEtlError::Configuration(
                        format!("Invalid default decimal value: '{}'", default_str)
                    ))?;
                Ok(Value::Decimal(parsed))
            },
            "boolean" => {
                let parsed = default_str.parse::<bool>()
                    .map_err(|_| crate::TinyEtlError::Configuration(
                        format!("Invalid default boolean value: '{}'", default_str)
                    ))?;
                Ok(Value::Boolean(parsed))
            },
            "date" | "datetime" => {
                // Try to parse as RFC3339 first, then as date
                let parsed = chrono::DateTime::parse_from_rfc3339(default_str)
                    .or_else(|_| {
                        // Try parsing as date only
                        chrono::NaiveDate::parse_from_str(default_str, "%Y-%m-%d")
                            .map(|d| d.and_hms_opt(0, 0, 0).unwrap().and_utc().fixed_offset())
                    })
                    .map_err(|_| crate::TinyEtlError::Configuration(
                        format!("Invalid default date/datetime value: '{}'", default_str)
                    ))?;
                Ok(Value::Date(parsed.with_timezone(&Utc)))
            },
            _ => Err(crate::TinyEtlError::Configuration(
                format!("Unsupported data type for default value: '{}'", data_type)
            )),
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Column {
    pub name: String,
    pub data_type: DataType,
    pub nullable: bool,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Schema {
    pub columns: Vec<Column>,
    pub estimated_rows: Option<usize>,
    pub primary_key_candidate: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum Value {
    String(String),
    Integer(i64),
    Decimal(Decimal),
    Boolean(bool),
    Date(DateTime<Utc>),
    Null,
}

pub type Row = HashMap<String, Value>;

pub struct SchemaInferer;

impl SchemaInferer {
    pub fn infer_from_rows(rows: &[Row]) -> Result<Schema> {
        if rows.is_empty() {
            return Ok(Schema {
                columns: Vec::new(),
                estimated_rows: Some(0),
                primary_key_candidate: None,
            });
        }

        let mut column_types: HashMap<String, Vec<DataType>> = HashMap::new();
        
        // Collect all unique column names
        let mut all_columns = std::collections::HashSet::new();
        for row in rows {
            for key in row.keys() {
                all_columns.insert(key.clone());
            }
        }

        // Analyze each column's data types
        for col_name in &all_columns {
            let mut types = Vec::new();
            for row in rows {
                let data_type = match row.get(col_name) {
                    Some(value) => Self::infer_type(value),
                    None => DataType::Null,
                };
                types.push(data_type);
            }
            column_types.insert(col_name.clone(), types);
        }

        // Determine final type for each column
        let columns = all_columns
            .iter()
            .map(|col_name| {
                let types = column_types.get(col_name).unwrap();
                let (data_type, nullable) = Self::resolve_column_type(types);
                Column {
                    name: col_name.clone(),
                    data_type,
                    nullable,
                }
            })
            .collect();

        Ok(Schema {
            columns,
            estimated_rows: Some(rows.len()),
            primary_key_candidate: None, // TODO: Implement PK detection
        })
    }
    
    pub fn infer_type(value: &Value) -> DataType {
        match value {
            Value::String(_) => DataType::String,
            Value::Integer(_) => DataType::Integer,
            Value::Decimal(_) => DataType::Decimal,
            Value::Boolean(_) => DataType::Boolean,
            Value::Date(_) => DataType::DateTime,
            Value::Null => DataType::Null,
        }
    }
    
    pub fn resolve_column_type(types: &[DataType]) -> (DataType, bool) {
        let has_null = types.iter().any(|t| matches!(t, DataType::Null));
        
        // Filter out nulls for type determination
        let non_null_types: Vec<&DataType> = types
            .iter()
            .filter(|t| !matches!(t, DataType::Null))
            .collect();
            
        if non_null_types.is_empty() {
            return (DataType::String, true);
        }
        
        // If all non-null values are the same type, use that type
        let first_type = non_null_types[0];
        if non_null_types.iter().all(|t| std::mem::discriminant(*t) == std::mem::discriminant(first_type)) {
            (first_type.clone(), has_null)
        } else {
            // Mixed types default to String
            (DataType::String, has_null)
        }
    }
}

impl std::fmt::Display for DataType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            DataType::String => write!(f, "TEXT"),
            DataType::Integer => write!(f, "INTEGER"),
            DataType::Decimal => write!(f, "DECIMAL"),
            DataType::Boolean => write!(f, "BOOLEAN"),
            DataType::Date => write!(f, "DATE"),
            DataType::DateTime => write!(f, "TIMESTAMP"),
            DataType::Null => write!(f, "NULL"),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use chrono::Utc;
    
    #[test]
    fn test_empty_schema_inference() {
        let rows = Vec::new();
        let schema = SchemaInferer::infer_from_rows(&rows).unwrap();
        assert!(schema.columns.is_empty());
        assert_eq!(schema.estimated_rows, Some(0));
    }
    
    #[test]
    fn test_single_row_schema_inference() {
        let mut row = HashMap::new();
        row.insert("id".to_string(), Value::Integer(1));
        row.insert("name".to_string(), Value::String("test".to_string()));
        row.insert("active".to_string(), Value::Boolean(true));
        
        let schema = SchemaInferer::infer_from_rows(&[row]).unwrap();
        assert_eq!(schema.columns.len(), 3);
        
        let id_col = schema.columns.iter().find(|c| c.name == "id").unwrap();
        assert_eq!(id_col.data_type, DataType::Integer);
        assert!(!id_col.nullable);
    }
    
    #[test]
    fn test_mixed_types_schema_inference() {
        let mut row1 = HashMap::new();
        row1.insert("value".to_string(), Value::Integer(1));
        
        let mut row2 = HashMap::new();
        row2.insert("value".to_string(), Value::String("hello".to_string()));
        
        let schema = SchemaInferer::infer_from_rows(&[row1, row2]).unwrap();
        let value_col = schema.columns.iter().find(|c| c.name == "value").unwrap();
        assert_eq!(value_col.data_type, DataType::String);
    }
    
    #[test]
    fn test_nullable_column_inference() {
        let mut row1 = HashMap::new();
        row1.insert("optional".to_string(), Value::String("present".to_string()));
        
        let mut row2 = HashMap::new();
        row2.insert("optional".to_string(), Value::Null);
        
        let schema = SchemaInferer::infer_from_rows(&[row1, row2]).unwrap();
        let optional_col = schema.columns.iter().find(|c| c.name == "optional").unwrap();
        assert_eq!(optional_col.data_type, DataType::String);
        assert!(optional_col.nullable);
    }
    
    #[test]
    fn test_data_type_display() {
        assert_eq!(DataType::String.to_string(), "TEXT");
        assert_eq!(DataType::Integer.to_string(), "INTEGER");
        assert_eq!(DataType::Decimal.to_string(), "DECIMAL");
        assert_eq!(DataType::Boolean.to_string(), "BOOLEAN");
        assert_eq!(DataType::Date.to_string(), "DATE");
        assert_eq!(DataType::DateTime.to_string(), "TIMESTAMP");
        assert_eq!(DataType::Null.to_string(), "NULL");
    }
    
    #[test]
    fn test_value_type_inference() {
        assert_eq!(SchemaInferer::infer_type(&Value::String("test".to_string())), DataType::String);
        assert_eq!(SchemaInferer::infer_type(&Value::Integer(42)), DataType::Integer);
        assert_eq!(SchemaInferer::infer_type(&Value::Decimal(Decimal::new(314, 2))), DataType::Decimal);
        assert_eq!(SchemaInferer::infer_type(&Value::Boolean(true)), DataType::Boolean);
        assert_eq!(SchemaInferer::infer_type(&Value::Date(Utc::now())), DataType::DateTime);
        assert_eq!(SchemaInferer::infer_type(&Value::Null), DataType::Null);
    }
    
    #[test]
    fn test_column_type_resolution() {
        // All same type
        let types = vec![DataType::Integer, DataType::Integer, DataType::Integer];
        let (resolved_type, nullable) = SchemaInferer::resolve_column_type(&types);
        assert_eq!(resolved_type, DataType::Integer);
        assert!(!nullable);
        
        // With nulls
        let types = vec![DataType::String, DataType::Null, DataType::String];
        let (resolved_type, nullable) = SchemaInferer::resolve_column_type(&types);
        assert_eq!(resolved_type, DataType::String);
        assert!(nullable);
        
        // All nulls
        let types = vec![DataType::Null, DataType::Null];
        let (resolved_type, nullable) = SchemaInferer::resolve_column_type(&types);
        assert_eq!(resolved_type, DataType::String);
        assert!(nullable);
    }
    
    #[test]
    fn test_large_schema_inference() {
        let mut rows = Vec::new();
        for i in 0..1000 {
            let mut row = HashMap::new();
            row.insert("id".to_string(), Value::Integer(i));
            row.insert("name".to_string(), Value::String(format!("user_{}", i)));
            if i % 10 == 0 {
                row.insert("optional".to_string(), Value::Null);
            } else {
                row.insert("optional".to_string(), Value::String("present".to_string()));
            }
            rows.push(row);
        }
        
        let schema = SchemaInferer::infer_from_rows(&rows).unwrap();
        assert_eq!(schema.columns.len(), 3);
        assert_eq!(schema.estimated_rows, Some(1000));
        
        let optional_col = schema.columns.iter().find(|c| c.name == "optional").unwrap();
        assert!(optional_col.nullable);
    }
    
    #[test]
    fn test_missing_columns_in_rows() {
        // Test case where some rows don't have all columns
        let mut row1 = HashMap::new();
        row1.insert("id".to_string(), Value::Integer(1));
        row1.insert("name".to_string(), Value::String("Alice".to_string()));
        row1.insert("email".to_string(), Value::String("alice@example.com".to_string()));
        
        let mut row2 = HashMap::new();
        row2.insert("id".to_string(), Value::Integer(2));
        row2.insert("name".to_string(), Value::String("Bob".to_string()));
        // Missing email column in this row
        
        let schema = SchemaInferer::infer_from_rows(&[row1, row2]).unwrap();
        assert_eq!(schema.columns.len(), 3);
        
        let email_col = schema.columns.iter().find(|c| c.name == "email").unwrap();
        assert_eq!(email_col.data_type, DataType::String);
        assert!(email_col.nullable); // Should be nullable because it's missing in row2
    }
}