hyperQL 0.1.0

Query language for hyperspatial graph databases with temporal and spatial semantics
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
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
use crate::ast::*;
use crate::error::Result;
use crate::validator::{
    ValidationConfig, ValidationResult, ValidationError, ValidationWarning,
    ValidationErrorKind, ValidationWarningKind,
};
use std::collections::HashMap;

/// Schema-aware validator for table and column references
pub struct SchemaValidator {
    config: ValidationConfig,
    schema: Option<HashMap<String, Vec<String>>>,
}

impl SchemaValidator {
    /// Create a new schema validator
    pub fn new(config: &ValidationConfig) -> Self {
        Self {
            config: config.clone(),
            schema: None,
        }
    }

    /// Update configuration
    pub fn update_config(&mut self, config: &ValidationConfig) {
        self.config = config.clone();
    }

    /// Set schema information for validation
    pub fn set_schema(&mut self, schema: &HashMap<String, Vec<String>>) {
        self.schema = Some(schema.clone());
    }

    /// Clear schema information
    pub fn clear_schema(&mut self) {
        self.schema = None;
    }

    /// Validate a statement against schema
    pub fn validate(&self, statement: &Statement, result: &mut ValidationResult) -> Result<()> {
        result.metadata.phases_completed.push("schema".to_string());
        
        // Only validate if we have schema information
        if self.schema.is_none() {
            return Ok(());
        }

        match statement {
            Statement::Select(select) => self.validate_select_schema(select, result),
            Statement::Insert(insert) => self.validate_insert_schema(insert, result),
            Statement::Update(update) => self.validate_update_schema(update, result),
            Statement::Delete(delete) => self.validate_delete_schema(delete, result),
            Statement::Schema(_schema_op) => {
                // TODO: Implement schema DDL schema validation
                // Validate that schema operations reference valid collections
                Ok(())
            },
            Statement::Stream(_stream_op) => {
                // TODO: Implement stream schema validation
                // Validate stream schemas and field types
                Ok(())
            },
        }
    }

    /// Validate SELECT statement against schema
    fn validate_select_schema(&self, select: &SelectStatement, result: &mut ValidationResult) -> Result<()> {
        let schema = self.schema.as_ref().unwrap();

        // Validate FROM clause table references
        if let Some(from_clause) = &select.from {
            match from_clause {
                FromClause::Table { collection, entity_type, .. } => {
                    // Build table reference name for schema lookup
                    let table_ref = match entity_type {
                        Some(et) => format!("{}.{}", collection, et),
                        None => collection.clone(),
                    };
                    self.validate_table_reference(&table_ref, result, schema);
                }
                FromClause::Subquery { .. } => {
                    // TODO: Handle subquery validation
                }
            }
        }

        // Collect available columns from referenced tables
        let available_columns = if let Some(from_clause) = &select.from {
            self.collect_available_columns(from_clause, schema)
        } else {
            HashMap::new()
        };

        // Validate column references in all clauses
        self.validate_select_columns(select, result, &available_columns)?;

        Ok(())
    }

    /// Validate INSERT statement against schema
    fn validate_insert_schema(&self, insert: &InsertStatement, result: &mut ValidationResult) -> Result<()> {
        let schema = self.schema.as_ref().unwrap();

        // Validate table reference
        self.validate_table_reference(&insert.table, result, schema);

        // Get table columns
        if let Some(table_columns) = schema.get(&insert.table) {
            // Validate specified columns (if any)
            if !insert.columns.is_empty() {
                for column in &insert.columns {
                    if !table_columns.contains(column) {
                        result.add_error(
                            ValidationError::new(
                                ValidationErrorKind::SchemaError,
                                format!("Column '{}' does not exist in table '{}'", column, insert.table),
                            )
                            .with_suggestions(self.suggest_similar_columns(column, table_columns))
                        );
                    }
                }

                // Check if all required columns are provided (simplified - assumes all columns are optional for now)
                // TODO: Add required column validation based on schema constraints
            }
        }

        Ok(())
    }

    /// Validate UPDATE statement against schema
    fn validate_update_schema(&self, update: &UpdateStatement, result: &mut ValidationResult) -> Result<()> {
        let schema = self.schema.as_ref().unwrap();

        // Validate table reference
        self.validate_table_reference(&update.table, result, schema);

        // Get table columns
        if let Some(table_columns) = schema.get(&update.table) {
            // Validate assignment columns
            for assignment in &update.assignments {
                if !table_columns.contains(&assignment.column) {
                    result.add_error(
                        ValidationError::new(
                            ValidationErrorKind::SchemaError,
                            format!("Column '{}' does not exist in table '{}'", assignment.column, update.table),
                        )
                        .with_suggestions(self.suggest_similar_columns(&assignment.column, table_columns))
                    );
                }
            }

            // Validate WHERE clause column references
            if let Some(where_clause) = &update.where_clause {
                let available_columns = [(update.table.clone(), table_columns.clone())].iter().cloned().collect();
                self.validate_expression_columns(where_clause, result, &available_columns)?;
            }
        }

        Ok(())
    }

    /// Validate DELETE statement against schema
    fn validate_delete_schema(&self, delete: &DeleteStatement, result: &mut ValidationResult) -> Result<()> {
        let schema = self.schema.as_ref().unwrap();

        // Validate table reference
        self.validate_table_reference(&delete.table, result, schema);

        // Get table columns
        if let Some(table_columns) = schema.get(&delete.table) {
            // Validate WHERE clause column references
            if let Some(where_clause) = &delete.where_clause {
                let available_columns = [(delete.table.clone(), table_columns.clone())].iter().cloned().collect();
                self.validate_expression_columns(where_clause, result, &available_columns)?;
            }
        }

        Ok(())
    }

    /// Validate table reference against schema
    fn validate_table_reference(
        &self,
        table_name: &str,
        result: &mut ValidationResult,
        schema: &HashMap<String, Vec<String>>,
    ) {
        if !schema.contains_key(table_name) {
            result.add_error(
                ValidationError::new(
                    ValidationErrorKind::SchemaError,
                    format!("Table '{}' does not exist", table_name),
                )
                .with_suggestions(self.suggest_similar_tables(table_name, schema))
            );
        }
    }

    /// Collect available columns from FROM clause tables
    fn collect_available_columns(
        &self,
        from_clause: &FromClause,
        schema: &HashMap<String, Vec<String>>,
    ) -> HashMap<String, Vec<String>> {
        let mut available_columns = HashMap::new();

        match from_clause {
            FromClause::Table { collection, entity_type, .. } => {
                // Build table reference name for schema lookup
                let table_ref = match entity_type {
                    Some(et) => format!("{}.{}", collection, et),
                    None => collection.clone(),
                };
                if let Some(columns) = schema.get(&table_ref) {
                    available_columns.insert(table_ref, columns.clone());
                }
            }
            FromClause::Subquery { .. } => {
                // TODO: Handle subquery columns
            }
        }

        available_columns
    }

    /// Validate column references in SELECT statement
    fn validate_select_columns(
        &self,
        select: &SelectStatement,
        result: &mut ValidationResult,
        available_columns: &HashMap<String, Vec<String>>,
    ) -> Result<()> {
        // Validate projection columns
        for projection in &select.select_list {
            match projection {
                SelectItem::Expression { expr, .. } => {
                    self.validate_expression_columns(expr, result, available_columns)?;
                }
                SelectItem::Wildcard => {
                    // Wildcard is valid if we have any tables
                    if available_columns.is_empty() {
                        result.add_error(
                            ValidationError::new(
                                ValidationErrorKind::SchemaError,
                                "Wildcard (*) cannot be used without FROM clause".to_string(),
                            )
                        );
                    }
                }
            }
        }

        // Validate WHERE clause
        if let Some(where_expr) = &select.where_clause {
            self.validate_expression_columns(where_expr, result, available_columns)?;
        }

        // Validate GROUP BY
        for group_expr in &select.group_by {
            self.validate_expression_columns(group_expr, result, available_columns)?;
        }

        // Validate HAVING
        if let Some(having_expr) = &select.having {
            self.validate_expression_columns(having_expr, result, available_columns)?;
        }

        // Validate ORDER BY
        for order_item in &select.order_by {
            self.validate_expression_columns(&order_item.expr, result, available_columns)?;
        }

        Ok(())
    }

    /// Validate column references in expressions recursively
    fn validate_expression_columns(
        &self,
        expr: &Expression,
        result: &mut ValidationResult,
        available_columns: &HashMap<String, Vec<String>>,
    ) -> Result<()> {
        match expr {
            Expression::Column(col_ref) => {
                self.validate_column_reference(col_ref, result, available_columns);
            }
            Expression::Binary { left, right, .. } => {
                self.validate_expression_columns(left, result, available_columns)?;
                self.validate_expression_columns(right, result, available_columns)?;
            }
            Expression::Unary { expr, .. } => {
                self.validate_expression_columns(expr, result, available_columns)?;
            }
            Expression::Function { args, .. } => {
                for arg in args {
                    self.validate_expression_columns(arg, result, available_columns)?;
                }
            }
            // Other expression types don't contain column references
            _ => {}
        }

        Ok(())
    }

    /// Validate a single column reference
    fn validate_column_reference(
        &self,
        col_ref: &ColumnRef,
        result: &mut ValidationResult,
        available_columns: &HashMap<String, Vec<String>>,
    ) {
        match &col_ref.table {
            Some(table) => {
                // Qualified column reference (table.column)
                if let Some(columns) = available_columns.get(table) {
                    if !columns.contains(&col_ref.name) {
                        result.add_error(
                            ValidationError::new(
                                ValidationErrorKind::SchemaError,
                                format!("Column '{}.{}' does not exist", table, col_ref.name),
                            )
                            .with_suggestions(self.suggest_similar_columns(&col_ref.name, columns))
                        );
                    }
                } else {
                    result.add_error(
                        ValidationError::new(
                            ValidationErrorKind::SchemaError,
                            format!("Table '{}' not found in FROM clause", table),
                        )
                    );
                }
            }
            None => {
                // Unqualified column reference - find in available tables
                let matching_tables: Vec<String> = available_columns
                    .iter()
                    .filter_map(|(table, columns)| {
                        if columns.contains(&col_ref.name) {
                            Some(table.clone())
                        } else {
                            None
                        }
                    })
                    .collect();

                match matching_tables.len() {
                    0 => {
                        // Column not found in any table
                        result.add_error(
                            ValidationError::new(
                                ValidationErrorKind::SchemaError,
                                format!("Column '{}' not found in any referenced table", col_ref.name),
                            )
                            .with_suggestions(self.suggest_columns_from_all_tables(&col_ref.name, available_columns))
                        );
                    }
                    1 => {
                        // Column found in exactly one table - OK
                    }
                    _ => {
                        // Column found in multiple tables - ambiguous
                        if !self.config.allow_ambiguous_columns {
                            result.add_error(
                                ValidationError::new(
                                    ValidationErrorKind::SchemaError,
                                    format!("Column '{}' is ambiguous - found in tables: {}", 
                                           col_ref.name, 
                                           matching_tables.join(", ")),
                                )
                                .with_suggestions(vec![
                                    "Use qualified column names (table.column)".to_string(),
                                    "Use table aliases to disambiguate".to_string(),
                                ])
                            );
                        } else {
                            result.add_warning(
                                ValidationWarning::new(
                                    ValidationWarningKind::AmbiguousReference,
                                    format!("Column '{}' found in multiple tables: {}", 
                                           col_ref.name, 
                                           matching_tables.join(", ")),
                                )
                                .with_suggestion("Consider using qualified column names for clarity".to_string())
                            );
                        }
                    }
                }
            }
        }
    }

    /// Suggest similar table names
    fn suggest_similar_tables(&self, table_name: &str, schema: &HashMap<String, Vec<String>>) -> Vec<String> {
        let mut suggestions = Vec::new();
        
        for existing_table in schema.keys() {
            if self.is_similar_name(table_name, existing_table) {
                suggestions.push(format!("Did you mean '{}'?", existing_table));
            }
        }

        if suggestions.is_empty() {
            suggestions.push("Available tables:".to_string());
            for table in schema.keys().take(5) {
                suggestions.push(format!("  {}", table));
            }
            if schema.len() > 5 {
                suggestions.push("  ...".to_string());
            }
        }

        suggestions
    }

    /// Suggest similar column names
    fn suggest_similar_columns(&self, column_name: &str, columns: &[String]) -> Vec<String> {
        let mut suggestions = Vec::new();
        
        for existing_column in columns {
            if self.is_similar_name(column_name, existing_column) {
                suggestions.push(format!("Did you mean '{}'?", existing_column));
            }
        }

        if suggestions.is_empty() && !columns.is_empty() {
            suggestions.push("Available columns:".to_string());
            for column in columns.iter().take(5) {
                suggestions.push(format!("  {}", column));
            }
            if columns.len() > 5 {
                suggestions.push("  ...".to_string());
            }
        }

        suggestions
    }

    /// Suggest column names from all available tables
    fn suggest_columns_from_all_tables(
        &self,
        column_name: &str,
        available_columns: &HashMap<String, Vec<String>>,
    ) -> Vec<String> {
        let mut suggestions = Vec::new();
        
        for (table, columns) in available_columns {
            for existing_column in columns {
                if self.is_similar_name(column_name, existing_column) {
                    suggestions.push(format!("Did you mean '{}.{}'?", table, existing_column));
                }
            }
        }

        if suggestions.is_empty() {
            suggestions.push("Available columns:".to_string());
            let mut count = 0;
            for (table, columns) in available_columns {
                for column in columns {
                    if count >= 5 { break; }
                    suggestions.push(format!("  {}.{}", table, column));
                    count += 1;
                }
                if count >= 5 { break; }
            }
            if count >= 5 {
                suggestions.push("  ...".to_string());
            }
        }

        suggestions
    }

    /// Check if two names are similar (simple edit distance)
    fn is_similar_name(&self, name1: &str, name2: &str) -> bool {
        if name1 == name2 {
            return true;
        }

        // Simple similarity check - case insensitive and edit distance
        let name1_lower = name1.to_lowercase();
        let name2_lower = name2.to_lowercase();
        
        // Check for case-insensitive match
        if name1_lower == name2_lower {
            return true;
        }

        // Check for simple edit distance (insertions, deletions, substitutions)
        let distance = self.edit_distance(&name1_lower, &name2_lower);
        let max_len = name1.len().max(name2.len());
        
        // Consider similar if edit distance is small relative to length
        distance <= 2 && max_len > 2 && (distance as f64 / max_len as f64) < 0.5
    }

    /// Calculate edit distance between two strings
    fn edit_distance(&self, s1: &str, s2: &str) -> usize {
        let chars1: Vec<char> = s1.chars().collect();
        let chars2: Vec<char> = s2.chars().collect();
        let len1 = chars1.len();
        let len2 = chars2.len();

        // Create matrix for dynamic programming
        let mut matrix = vec![vec![0; len2 + 1]; len1 + 1];

        // Initialize first row and column
        for i in 0..=len1 {
            matrix[i][0] = i;
        }
        for j in 0..=len2 {
            matrix[0][j] = j;
        }

        // Fill the matrix
        for i in 1..=len1 {
            for j in 1..=len2 {
                if chars1[i - 1] == chars2[j - 1] {
                    matrix[i][j] = matrix[i - 1][j - 1];
                } else {
                    matrix[i][j] = 1 + matrix[i - 1][j].min(matrix[i][j - 1]).min(matrix[i - 1][j - 1]);
                }
            }
        }

        matrix[len1][len2]
    }

    /// Check if schema validation is enabled
    pub fn is_enabled(&self) -> bool {
        self.config.validate_schema && self.schema.is_some()
    }

    /// Get available tables
    pub fn get_tables(&self) -> Vec<String> {
        self.schema
            .as_ref()
            .map(|schema| schema.keys().cloned().collect())
            .unwrap_or_default()
    }

    /// Get columns for a specific table
    pub fn get_table_columns(&self, table: &str) -> Option<&Vec<String>> {
        self.schema.as_ref().and_then(|schema| schema.get(table))
    }
}

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

    fn create_test_schema() -> HashMap<String, Vec<String>> {
        let mut schema = HashMap::new();
        schema.insert(
            "users".to_string(),
            vec!["id".to_string(), "name".to_string(), "email".to_string(), "age".to_string()],
        );
        schema.insert(
            "orders".to_string(),
            vec!["id".to_string(), "user_id".to_string(), "amount".to_string(), "created_at".to_string()],
        );
        schema
    }

    #[test]
    fn test_valid_table_reference() {
        let config = ValidationConfig::default();
        let mut validator = SchemaValidator::new(&config);
        let schema = create_test_schema();
        validator.set_schema(&schema);

        let mut result = ValidationResult::new();

        let select = SelectStatement {
            select_list: vec![SelectItem::Wildcard],
            from: Some(FromClause::Table {
                collection: "users".to_string(),
                entity_type: None,
                alias: None,
            }),
            joins: Vec::new(),
            where_clause: None,
            group_by: vec![],
            having: None,
            order_by: vec![],
            limit: None,
            offset: None,
            distinct: false,
            traverse_clause: None,
        };

        validator.validate_select_schema(&select, &mut result).unwrap();

        assert!(result.errors.is_empty());
    }

    #[test]
    fn test_invalid_table_reference() {
        let config = ValidationConfig::default();
        let mut validator = SchemaValidator::new(&config);
        let schema = create_test_schema();
        validator.set_schema(&schema);

        let mut result = ValidationResult::new();

        let select = SelectStatement {
            select_list: vec![SelectItem::Wildcard],
            from: Some(FromClause::Table {
                collection: "nonexistent".to_string(),
                entity_type: None,
                alias: None,
            }),
            joins: Vec::new(),
            where_clause: None,
            group_by: vec![],
            having: None,
            order_by: vec![],
            limit: None,
            offset: None,
            distinct: false,
            traverse_clause: None,
        };

        validator.validate_select_schema(&select, &mut result).unwrap();

        assert!(!result.errors.is_empty());
        assert!(result.errors.iter().any(|e| e.message.contains("Table 'nonexistent' does not exist")));
    }

    #[test]
    fn test_valid_column_reference() {
        let config = ValidationConfig::default();
        let mut validator = SchemaValidator::new(&config);
        let schema = create_test_schema();
        validator.set_schema(&schema);

        let mut result = ValidationResult::new();

        let select = SelectStatement {
            select_list: vec![SelectItem::Expression {
                expr: Expression::Column(ColumnRef {
                    table: None,
                    name: "name".to_string(),
                }),
                alias: None,
            }],
            from: Some(FromClause::Table {
                collection: "users".to_string(),
                entity_type: None,
                alias: None,
            }),
            joins: Vec::new(),
            where_clause: None,
            group_by: vec![],
            having: None,
            order_by: vec![],
            limit: None,
            offset: None,
            distinct: false,
            traverse_clause: None,
        };

        validator.validate_select_schema(&select, &mut result).unwrap();

        assert!(result.errors.is_empty());
    }

    #[test]
    fn test_invalid_column_reference() {
        let config = ValidationConfig::default();
        let mut validator = SchemaValidator::new(&config);
        let schema = create_test_schema();
        validator.set_schema(&schema);

        let mut result = ValidationResult::new();

        let select = SelectStatement {
            select_list: vec![SelectItem::Expression {
                expr: Expression::Column(ColumnRef {
                    table: None,
                    name: "nonexistent".to_string(),
                }),
                alias: None,
            }],
            from: Some(FromClause::Table {
                collection: "users".to_string(),
                entity_type: None,
                alias: None,
            }),
            joins: Vec::new(),
            where_clause: None,
            group_by: vec![],
            having: None,
            order_by: vec![],
            limit: None,
            offset: None,
            distinct: false,
            traverse_clause: None,
        };

        validator.validate_select_schema(&select, &mut result).unwrap();

        assert!(!result.errors.is_empty());
        assert!(result.errors.iter().any(|e| e.message.contains("Column 'nonexistent' not found")));
    }

    #[test]
    fn test_ambiguous_column_reference() {
        // TODO: Re-enable when multi-table FROM clause is supported
        // Currently FROM only supports single table, so no ambiguity can occur

        let config = ValidationConfig {
            allow_ambiguous_columns: false,
            ..ValidationConfig::default()
        };
        let mut validator = SchemaValidator::new(&config);
        let schema = create_test_schema();
        validator.set_schema(&schema);

        let mut result = ValidationResult::new();

        let select = SelectStatement {
            select_list: vec![SelectItem::Expression {
                expr: Expression::Column(ColumnRef {
                    table: None,
                    name: "id".to_string(), // Both users and orders have 'id'
                }),
                alias: None,
            }],
            from: Some(FromClause::Table {
                collection: "users".to_string(),
                entity_type: None,
                alias: None,
            }),
            joins: Vec::new(),
            where_clause: None,
            group_by: vec![],
            having: None,
            order_by: vec![],
            limit: None,
            offset: None,
            distinct: false,
            traverse_clause: None,
        };

        validator.validate_select_schema(&select, &mut result).unwrap();

        // With single table FROM clause, there should be no ambiguity
        assert!(result.errors.is_empty());
    }

    #[test]
    fn test_edit_distance() {
        let config = ValidationConfig::default();
        let validator = SchemaValidator::new(&config);

        assert_eq!(validator.edit_distance("test", "test"), 0);
        assert_eq!(validator.edit_distance("test", "best"), 1);
        assert_eq!(validator.edit_distance("test", "tests"), 1);
        assert_eq!(validator.edit_distance("test", "tes"), 1);
        assert_eq!(validator.edit_distance("test", "best1"), 2);
    }

    #[test]
    fn test_similar_name_detection() {
        let config = ValidationConfig::default();
        let validator = SchemaValidator::new(&config);

        assert!(validator.is_similar_name("user", "users"));
        assert!(validator.is_similar_name("User", "user"));
        assert!(validator.is_similar_name("test", "best"));
        assert!(!validator.is_similar_name("test", "completely_different"));
    }
}