data-modelling-core 2.4.0

Core SDK library for model operations across platforms
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
//! AVRO schema parser for importing AVRO schemas into data models.
//!
//! # Validation
//!
//! All imported table and column names are validated for:
//! - Valid identifier format
//! - Maximum length limits

use crate::import::odcs_shared::column_to_column_data;
use crate::import::{ImportError, ImportResult, TableData};
use crate::models::{Column, Table, Tag};
use crate::validation::input::{validate_column_name, validate_table_name};
use anyhow::{Context, Result};
use serde_json::{Value, json};
use std::collections::HashMap;
use std::str::FromStr;
use tracing::{info, warn};

/// Parser for AVRO schema format.
#[derive(Default)]
pub struct AvroImporter;

impl AvroImporter {
    /// Create a new AVRO parser instance.
    ///
    /// # Example
    ///
    /// ```rust
    /// use data_modelling_core::import::avro::AvroImporter;
    ///
    /// let importer = AvroImporter::new();
    /// ```
    pub fn new() -> Self {
        Self
    }

    /// Import AVRO schema content and create Table(s) (SDK interface).
    ///
    /// # Arguments
    ///
    /// * `avro_content` - AVRO schema as JSON string (can be a single record or array of records)
    ///
    /// # Returns
    ///
    /// An `ImportResult` containing extracted tables and any parse errors.
    ///
    /// # Example
    ///
    /// ```rust
    /// use data_modelling_core::import::avro::AvroImporter;
    ///
    /// let importer = AvroImporter::new();
    /// let schema = r#"
    /// {
    ///   "type": "record",
    ///   "name": "User",
    ///   "fields": [
    ///     {"name": "id", "type": "long"}
    ///   ]
    /// }
    /// "#;
    /// let result = importer.import(schema).unwrap();
    /// ```
    pub fn import(&self, avro_content: &str) -> Result<ImportResult, ImportError> {
        match self.parse(avro_content) {
            Ok((tables, errors)) => {
                let mut sdk_tables = Vec::new();
                for (idx, table) in tables.iter().enumerate() {
                    sdk_tables.push(TableData {
                        table_index: idx,
                        id: Some(table.id.to_string()),
                        name: Some(table.name.clone()),
                        columns: table.columns.iter().map(column_to_column_data).collect(),
                        ..Default::default()
                    });
                }
                let sdk_errors: Vec<ImportError> = errors
                    .iter()
                    .map(|e| ImportError::ParseError(e.message.clone()))
                    .collect();
                Ok(ImportResult {
                    tables: sdk_tables,
                    tables_requiring_name: Vec::new(),
                    errors: sdk_errors,
                    ai_suggestions: None,
                })
            }
            Err(e) => Err(ImportError::ParseError(e.to_string())),
        }
    }

    /// Parse AVRO schema content and create Table(s) (internal method).
    ///
    /// # Returns
    ///
    /// Returns a tuple of (Tables, list of errors/warnings).
    fn parse(&self, avro_content: &str) -> Result<(Vec<Table>, Vec<ParserError>)> {
        let mut errors = Vec::new();

        // Parse JSON
        let schema: Value =
            serde_json::from_str(avro_content).context("Failed to parse AVRO schema as JSON")?;

        let mut tables = Vec::new();

        // AVRO can be a single record or an array of records
        if let Some(schemas) = schema.as_array() {
            // Multiple schemas
            for (idx, schema_item) in schemas.iter().enumerate() {
                match self.parse_schema(schema_item, &mut errors) {
                    Ok(table) => tables.push(table),
                    Err(e) => {
                        errors.push(ParserError {
                            error_type: "parse_error".to_string(),
                            field: Some(format!("schema[{}]", idx)),
                            message: format!("Failed to parse schema: {}", e),
                        });
                    }
                }
            }
        } else {
            // Single schema
            match self.parse_schema(&schema, &mut errors) {
                Ok(table) => tables.push(table),
                Err(e) => {
                    errors.push(ParserError {
                        error_type: "parse_error".to_string(),
                        field: None,
                        message: format!("Failed to parse schema: {}", e),
                    });
                }
            }
        }

        Ok((tables, errors))
    }

    /// Parse a single AVRO schema record.
    fn parse_schema(&self, schema: &Value, errors: &mut Vec<ParserError>) -> Result<Table> {
        let schema_obj = schema
            .as_object()
            .ok_or_else(|| anyhow::anyhow!("Schema must be an object"))?;

        // Extract record name
        let name = schema_obj
            .get("name")
            .and_then(|v| v.as_str())
            .ok_or_else(|| anyhow::anyhow!("Missing required field: name"))?
            .to_string();

        // Validate table name
        if let Err(e) = validate_table_name(&name) {
            warn!("Table name validation warning for '{}': {}", name, e);
        }

        // Extract namespace (optional)
        let namespace = schema_obj
            .get("namespace")
            .and_then(|v| v.as_str())
            .map(|s| s.to_string());

        // Extract fields
        let fields = schema_obj
            .get("fields")
            .and_then(|v| v.as_array())
            .ok_or_else(|| anyhow::anyhow!("Missing required field: fields"))?;

        let mut columns = Vec::new();
        for (idx, field) in fields.iter().enumerate() {
            match self.parse_field(field, &name, errors) {
                Ok(mut cols) => columns.append(&mut cols),
                Err(e) => {
                    errors.push(ParserError {
                        error_type: "parse_error".to_string(),
                        field: Some(format!("fields[{}]", idx)),
                        message: format!("Failed to parse field: {}", e),
                    });
                }
            }
        }

        // Extract tags from AVRO schema (can be in root or in aliases/metadata)
        let mut tags: Vec<Tag> = Vec::new();
        if let Some(tags_arr) = schema_obj.get("tags").and_then(|v| v.as_array()) {
            for item in tags_arr {
                if let Some(s) = item.as_str() {
                    if let Ok(tag) = Tag::from_str(s) {
                        tags.push(tag);
                    } else {
                        tags.push(Tag::Simple(s.to_string()));
                    }
                }
            }
        }
        // Also check aliases/metadata for tags
        if let Some(aliases_arr) = schema_obj.get("aliases").and_then(|v| v.as_array()) {
            for item in aliases_arr {
                if let Some(s) = item.as_str() {
                    // AVRO aliases can be used as tags
                    if let Ok(tag) = Tag::from_str(s) {
                        if !tags.contains(&tag) {
                            tags.push(tag);
                        }
                    } else {
                        let simple_tag = Tag::Simple(s.to_string());
                        if !tags.contains(&simple_tag) {
                            tags.push(simple_tag);
                        }
                    }
                }
            }
        }

        // Build table metadata
        let mut odcl_metadata = HashMap::new();
        if let Some(ref ns) = namespace {
            odcl_metadata.insert("namespace".to_string(), json!(ns));
        }
        if let Some(doc) = schema_obj.get("doc").and_then(|v| v.as_str()) {
            odcl_metadata.insert("description".to_string(), json!(doc));
        }

        let table = Table {
            id: crate::models::table::Table::generate_id(&name, None, None, namespace.as_deref()),
            name: name.clone(),
            columns,
            database_type: None,
            catalog_name: None,
            schema_name: namespace.clone(),
            medallion_layers: Vec::new(),
            scd_pattern: None,
            data_vault_classification: None,
            modeling_level: None,
            tags,
            odcl_metadata,
            owner: None,
            sla: None,
            contact_details: None,
            infrastructure_type: None,
            notes: None,
            position: None,
            yaml_file_path: None,
            drawio_cell_id: None,
            quality: Vec::new(),
            errors: Vec::new(),
            created_at: chrono::Utc::now(),
            updated_at: chrono::Utc::now(),
        };

        info!(
            "Parsed AVRO schema: {} with {} columns",
            name,
            table.columns.len()
        );
        Ok(table)
    }

    /// Parse an AVRO field (which can be a simple field or nested record).
    fn parse_field(
        &self,
        field: &Value,
        _parent_name: &str,
        errors: &mut Vec<ParserError>,
    ) -> Result<Vec<Column>> {
        let field_obj = field
            .as_object()
            .ok_or_else(|| anyhow::anyhow!("Field must be an object"))?;

        let field_name = field_obj
            .get("name")
            .and_then(|v| v.as_str())
            .ok_or_else(|| anyhow::anyhow!("Field missing name"))?
            .to_string();

        // Validate column name
        if let Err(e) = validate_column_name(&field_name) {
            warn!("Column name validation warning for '{}': {}", field_name, e);
        }

        let field_type = field_obj
            .get("type")
            .ok_or_else(|| anyhow::anyhow!("Field missing type"))?;

        let description = field_obj
            .get("doc")
            .and_then(|v| v.as_str())
            .map(|s| s.to_string())
            .unwrap_or_default();

        // Handle union types (e.g., ["null", "string"] for nullable)
        let (avro_type, nullable) = if let Some(types) = field_type.as_array() {
            if types.len() == 2 && types.iter().any(|t| t.as_str() == Some("null")) {
                // Nullable type
                let non_null_type = types
                    .iter()
                    .find(|t| t.as_str() != Some("null"))
                    .ok_or_else(|| anyhow::anyhow!("Invalid union type"))?;
                (non_null_type, true)
            } else {
                // Complex union with multiple non-null types - use first non-null type
                // and mark as nullable since union implies optionality
                let first_non_null = types
                    .iter()
                    .find(|t| t.as_str() != Some("null"))
                    .unwrap_or(field_type);
                (first_non_null, true)
            }
        } else {
            (field_type, false)
        };

        // Parse the actual type
        let mut columns = Vec::new();
        if let Some(type_str) = avro_type.as_str() {
            // Simple type
            let data_type = self.map_avro_type_to_sql(type_str);
            columns.push(Column {
                name: field_name,
                data_type,
                nullable,
                description,
                ..Default::default()
            });
        } else if let Some(type_obj) = avro_type.as_object() {
            // Complex type (record, array, map)
            if type_obj.get("type").and_then(|v| v.as_str()) == Some("record") {
                // Nested record - create nested columns with dot notation
                let nested_name = type_obj
                    .get("name")
                    .and_then(|v| v.as_str())
                    .unwrap_or(&field_name);
                let nested_fields = type_obj
                    .get("fields")
                    .and_then(|v| v.as_array())
                    .ok_or_else(|| anyhow::anyhow!("Nested record missing fields"))?;

                for nested_field in nested_fields {
                    match self.parse_field(nested_field, nested_name, errors) {
                        Ok(mut nested_cols) => {
                            // Prefix nested columns with parent field name
                            for col in nested_cols.iter_mut() {
                                col.name = format!("{}.{}", field_name, col.name);
                            }
                            columns.append(&mut nested_cols);
                        }
                        Err(e) => {
                            errors.push(ParserError {
                                error_type: "parse_error".to_string(),
                                field: Some(format!("{}.{}", field_name, nested_name)),
                                message: format!("Failed to parse nested field: {}", e),
                            });
                        }
                    }
                }
            } else if type_obj.get("type").and_then(|v| v.as_str()) == Some("array") {
                // Array type
                let items = type_obj
                    .get("items")
                    .ok_or_else(|| anyhow::anyhow!("Array type missing items"))?;

                let data_type = if let Some(items_str) = items.as_str() {
                    format!("ARRAY<{}>", self.map_avro_type_to_sql(items_str))
                } else if let Some(items_obj) = items.as_object() {
                    if items_obj.get("type").and_then(|v| v.as_str()) == Some("record") {
                        // Array of records - create nested columns
                        let nested_name = items_obj
                            .get("name")
                            .and_then(|v| v.as_str())
                            .unwrap_or(&field_name);
                        let nested_fields = items_obj
                            .get("fields")
                            .and_then(|v| v.as_array())
                            .ok_or_else(|| anyhow::anyhow!("Array record missing fields"))?;

                        for nested_field in nested_fields {
                            match self.parse_field(nested_field, nested_name, errors) {
                                Ok(mut nested_cols) => {
                                    for col in nested_cols.iter_mut() {
                                        col.name = format!("{}.{}", field_name, col.name);
                                    }
                                    columns.append(&mut nested_cols);
                                }
                                Err(e) => {
                                    errors.push(ParserError {
                                        error_type: "parse_error".to_string(),
                                        field: Some(format!("{}.{}", field_name, nested_name)),
                                        message: format!("Failed to parse array item field: {}", e),
                                    });
                                }
                            }
                        }
                        return Ok(columns);
                    } else {
                        format!("ARRAY<{}>", "STRUCT")
                    }
                } else {
                    "ARRAY<STRING>".to_string()
                };

                columns.push(Column {
                    name: field_name,
                    data_type,
                    nullable,
                    description,
                    ..Default::default()
                });
            } else {
                // Other complex types - default to STRUCT
                columns.push(Column {
                    name: field_name,
                    data_type: "STRUCT".to_string(),
                    nullable,
                    description,
                    ..Default::default()
                });
            }
        } else {
            return Err(anyhow::anyhow!("Unsupported field type format"));
        }

        Ok(columns)
    }

    /// Map AVRO type to SQL/ODCL data type.
    fn map_avro_type_to_sql(&self, avro_type: &str) -> String {
        match avro_type {
            "int" => "INTEGER".to_string(),
            "long" => "BIGINT".to_string(),
            "float" => "FLOAT".to_string(),
            "double" => "DOUBLE".to_string(),
            "boolean" => "BOOLEAN".to_string(),
            "bytes" => "BYTES".to_string(),
            "string" => "STRING".to_string(),
            "null" => "NULL".to_string(),
            _ => "STRING".to_string(), // Default fallback
        }
    }
}

/// Parser error structure (matches ODCL parser format).
#[derive(Debug, Clone)]
pub struct ParserError {
    pub error_type: String,
    pub field: Option<String>,
    pub message: String,
}