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
use crate::ast::*;
use crate::error::Result;
use crate::type_checker::{TypeChecker, TypeInfo};
use crate::validator::{
    ValidationConfig, ValidationResult, ValidationError, ValidationWarning,
    ValidationErrorKind, ValidationWarningKind,
};

/// Expression validator that performs semantic validation beyond type checking
pub struct ExpressionValidator {
    config: ValidationConfig,
}

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

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

    /// Validate expressions in a statement using type checker
    pub fn validate(
        &self,
        statement: &Statement,
        type_checker: &mut TypeChecker,
        result: &mut ValidationResult,
    ) -> Result<()> {
        result.metadata.phases_completed.push("expression".to_string());
        
        match statement {
            Statement::Select(select) => self.validate_select_expressions(select, type_checker, result),
            Statement::Insert(insert) => self.validate_insert_expressions(insert, type_checker, result),
            Statement::Update(update) => self.validate_update_expressions(update, type_checker, result),
            Statement::Delete(delete) => self.validate_delete_expressions(delete, type_checker, result),
            Statement::Schema(_schema_op) => {
                // TODO: Implement schema expression validation
                // Validate expressions in calculated fields and cascade configurations
                Ok(())
            },
            Statement::Stream(_stream_op) => {
                // TODO: Implement stream expression validation
                // Validate expressions in stream filters and transformations
                Ok(())
            },
        }
    }

    /// Validate SELECT statement expressions
    fn validate_select_expressions(
        &self,
        select: &SelectStatement,
        type_checker: &mut TypeChecker,
        result: &mut ValidationResult,
    ) -> Result<()> {
        // Validate projection expressions
        for projection in &select.select_list {
            if let SelectItem::Expression { expr, .. } = projection {
                self.validate_expression_semantics(expr, type_checker, result)?;
            }
        }

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

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

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

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

        // Note: LIMIT/OFFSET are now u64 values, not expressions
        // TODO: If they become expressions again, add validation here

        Ok(())
    }

    /// Validate INSERT expressions
    fn validate_insert_expressions(
        &self,
        insert: &InsertStatement,
        type_checker: &mut TypeChecker,
        result: &mut ValidationResult,
    ) -> Result<()> {
        // Validate value expressions (INSERT values are Vec<Vec<Expression>>)
        for value_row in &insert.values {
            for value_expr in value_row {
                self.validate_expression_semantics(value_expr, type_checker, result)?;
            }
        }

        Ok(())
    }

    /// Validate UPDATE expressions
    fn validate_update_expressions(
        &self,
        update: &UpdateStatement,
        type_checker: &mut TypeChecker,
        result: &mut ValidationResult,
    ) -> Result<()> {
        // Validate assignment expressions
        for assignment in &update.assignments {
            self.validate_expression_semantics(&assignment.value, type_checker, result)?;
        }

        // Validate WHERE clause
        if let Some(where_expr) = &update.where_clause {
            self.validate_where_expression(where_expr, type_checker, result)?;
        }

        Ok(())
    }

    /// Validate DELETE expressions
    fn validate_delete_expressions(
        &self,
        delete: &DeleteStatement,
        type_checker: &mut TypeChecker,
        result: &mut ValidationResult,
    ) -> Result<()> {
        // Validate WHERE clause
        if let Some(where_expr) = &delete.where_clause {
            self.validate_where_expression(where_expr, type_checker, result)?;
        }

        Ok(())
    }

    /// Validate general expression semantics
    fn validate_expression_semantics(
        &self,
        expr: &Expression,
        type_checker: &mut TypeChecker,
        result: &mut ValidationResult,
    ) -> Result<()> {
        // First perform type checking
        if let Err(type_error) = type_checker.check_expression_type(expr) {
            result.add_error(
                ValidationError::new(
                    ValidationErrorKind::ExpressionError,
                    format!("Type checking failed: {}", type_error),
                )
            );
            return Ok(()); // Continue with other validations
        }

        // Then perform semantic validation
        match expr {
            Expression::Function { name, args } => {
                self.validate_function_call(name, args, type_checker, result)?;
                // Record function usage
                result.metadata.functions_used.push(name.clone());
            }
            Expression::Binary { left, op, right } => {
                self.validate_binary_expression(left, op, right, type_checker, result)?;
            }
            Expression::Unary { op, expr } => {
                self.validate_unary_expression(op, expr, type_checker, result)?;
            }
            Expression::Between { expr, lower, upper, .. } => {
                self.validate_expression_semantics(expr, type_checker, result)?;
                self.validate_expression_semantics(lower, type_checker, result)?;
                self.validate_expression_semantics(upper, type_checker, result)?;
            }
            Expression::Column(col_ref) => {
                self.validate_column_reference(col_ref, result);
                // Record column usage
                let column_name = match &col_ref.table {
                    Some(table) => format!("{}.{}", table, col_ref.name),
                    None => col_ref.name.clone(),
                };
                result.metadata.columns_referenced.push(column_name);
            }
            Expression::Literal(literal) => {
                self.validate_literal_value(literal, result)?;
            }
            Expression::Geometric(geo_expr) => {
                self.validate_geometric_expression(geo_expr, type_checker, result)?;
            }
            Expression::Vector(vec_expr) => {
                self.validate_vector_expression(vec_expr, type_checker, result)?;
            }
        }

        Ok(())
    }

    /// Validate WHERE clause expressions
    fn validate_where_expression(
        &self,
        expr: &Expression,
        type_checker: &mut TypeChecker,
        result: &mut ValidationResult,
    ) -> Result<()> {
        // WHERE must return boolean
        if let Ok(expr_type) = type_checker.check_expression_type(expr) {
            if !matches!(expr_type, TypeInfo::Bool | TypeInfo::Unknown) {
                result.add_error(
                    ValidationError::new(
                        ValidationErrorKind::ExpressionError,
                        "WHERE clause must evaluate to boolean".to_string(),
                    )
                    .with_suggestions(vec![
                        "Use comparison operators (=, <, >, etc.)".to_string(),
                        "Use logical operators (AND, OR, NOT)".to_string(),
                    ])
                );
            }
        }

        self.validate_expression_semantics(expr, type_checker, result)?;
        
        // Check for common WHERE clause anti-patterns
        self.check_where_antipatterns(expr, result);

        Ok(())
    }

    /// Validate HAVING clause expressions
    fn validate_having_expression(
        &self,
        expr: &Expression,
        type_checker: &mut TypeChecker,
        result: &mut ValidationResult,
    ) -> Result<()> {
        // HAVING must return boolean
        if let Ok(expr_type) = type_checker.check_expression_type(expr) {
            if !matches!(expr_type, TypeInfo::Bool | TypeInfo::Unknown) {
                result.add_error(
                    ValidationError::new(
                        ValidationErrorKind::ExpressionError,
                        "HAVING clause must evaluate to boolean".to_string(),
                    )
                );
            }
        }

        self.validate_expression_semantics(expr, type_checker, result)
    }


    /// Validate function call semantics
    fn validate_function_call(
        &self,
        name: &str,
        args: &[Expression],
        type_checker: &mut TypeChecker,
        result: &mut ValidationResult,
    ) -> Result<()> {
        // Validate argument count for known functions
        match name.to_uppercase().as_str() {
            // Aggregate functions
            "COUNT" => {
                if args.is_empty() || args.len() > 1 {
                    result.add_error(
                        ValidationError::new(
                            ValidationErrorKind::FunctionError,
                            "COUNT() requires exactly one argument".to_string(),
                        )
                    );
                }
            }
            "SUM" | "AVG" | "MIN" | "MAX" => {
                if args.len() != 1 {
                    result.add_error(
                        ValidationError::new(
                            ValidationErrorKind::FunctionError,
                            format!("{}() requires exactly one argument", name),
                        )
                    );
                }
            }
            // String functions
            "SUBSTRING" | "SUBSTR" => {
                if args.len() < 2 || args.len() > 3 {
                    result.add_error(
                        ValidationError::new(
                            ValidationErrorKind::FunctionError,
                            "SUBSTRING() requires 2 or 3 arguments".to_string(),
                        )
                    );
                }
            }
            "LENGTH" | "UPPER" | "LOWER" | "TRIM" => {
                if args.len() != 1 {
                    result.add_error(
                        ValidationError::new(
                            ValidationErrorKind::FunctionError,
                            format!("{}() requires exactly one argument", name),
                        )
                    );
                }
            }
            // Math functions
            "ABS" | "SQRT" | "FLOOR" | "CEIL" => {
                if args.len() != 1 {
                    result.add_error(
                        ValidationError::new(
                            ValidationErrorKind::FunctionError,
                            format!("{}() requires exactly one argument", name),
                        )
                    );
                }
            }
            "POWER" | "MOD" => {
                if args.len() != 2 {
                    result.add_error(
                        ValidationError::new(
                            ValidationErrorKind::FunctionError,
                            format!("{}() requires exactly two arguments", name),
                        )
                    );
                }
            }
            // Date functions
            "NOW" | "CURRENT_TIMESTAMP" => {
                if !args.is_empty() {
                    result.add_error(
                        ValidationError::new(
                            ValidationErrorKind::FunctionError,
                            format!("{}() takes no arguments", name),
                        )
                    );
                }
            }
            _ => {
                // Unknown function - warn but don't error
                result.add_warning(
                    ValidationWarning::new(
                        ValidationWarningKind::UnusualPattern,
                        format!("Unknown function '{}' - cannot validate arguments", name),
                    )
                );
            }
        }

        // Validate each argument
        for arg in args {
            self.validate_expression_semantics(arg, type_checker, result)?;
        }

        Ok(())
    }

    /// Validate binary expression semantics
    fn validate_binary_expression(
        &self,
        left: &Expression,
        op: &BinaryOperator,
        right: &Expression,
        type_checker: &mut TypeChecker,
        result: &mut ValidationResult,
    ) -> Result<()> {
        // Validate sub-expressions
        self.validate_expression_semantics(left, type_checker, result)?;
        self.validate_expression_semantics(right, type_checker, result)?;

        // Check for division by zero
        if matches!(op, BinaryOperator::Divide) {
            match right {
                Expression::Literal(Literal::Int(0)) => {
                    result.add_error(
                        ValidationError::new(
                            ValidationErrorKind::ExpressionError,
                            "Division by zero is not allowed".to_string(),
                        )
                    );
                }
                Expression::Literal(Literal::Float(f)) if *f == 0.0 => {
                    result.add_error(
                        ValidationError::new(
                            ValidationErrorKind::ExpressionError,
                            "Division by zero is not allowed".to_string(),
                        )
                    );
                }
                _ => {}
            }
        }

        // Check for potentially problematic comparisons
        match op {
            BinaryOperator::Equal | BinaryOperator::NotEqual => {
                self.check_equality_comparison(left, right, result);
            }
            BinaryOperator::LessThan | BinaryOperator::LessThanOrEqual |
            BinaryOperator::GreaterThan | BinaryOperator::GreaterThanOrEqual => {
                self.check_ordering_comparison(left, right, result);
            }
            _ => {}
        }

        Ok(())
    }

    /// Validate unary expression semantics
    fn validate_unary_expression(
        &self,
        op: &UnaryOperator,
        expr: &Expression,
        type_checker: &mut TypeChecker,
        result: &mut ValidationResult,
    ) -> Result<()> {
        self.validate_expression_semantics(expr, type_checker, result)?;

        // Validate operator usage
        match op {
            UnaryOperator::Not => {
                // NOT should be applied to boolean expressions
                if let Ok(expr_type) = type_checker.check_expression_type(expr) {
                    if !matches!(expr_type, TypeInfo::Bool | TypeInfo::Unknown) {
                        result.add_warning(
                            ValidationWarning::new(
                                ValidationWarningKind::UnusualPattern,
                                "NOT operator applied to non-boolean expression".to_string(),
                            )
                        );
                    }
                }
            }
            UnaryOperator::Plus => {
                // Plus should be applied to numeric expressions
                if let Ok(expr_type) = type_checker.check_expression_type(expr) {
                    if !expr_type.is_numeric() && !matches!(expr_type, TypeInfo::Unknown) {
                        result.add_error(
                            ValidationError::new(
                                ValidationErrorKind::ExpressionError,
                                "Plus operator can only be applied to numeric expressions".to_string(),
                            )
                        );
                    }
                }
            }
            UnaryOperator::Minus => {
                // Minus should be applied to numeric expressions
                if let Ok(expr_type) = type_checker.check_expression_type(expr) {
                    if !expr_type.is_numeric() && !matches!(expr_type, TypeInfo::Unknown) {
                        result.add_error(
                            ValidationError::new(
                                ValidationErrorKind::ExpressionError,
                                "Minus operator can only be applied to numeric expressions".to_string(),
                            )
                        );
                    }
                }
            }
            UnaryOperator::IsNull | UnaryOperator::IsNotNull => {
                // IS NULL and IS NOT NULL can be applied to any type
            }
        }

        Ok(())
    }

    /// Validate column reference
    fn validate_column_reference(&self, col_ref: &ColumnRef, result: &mut ValidationResult) {
        // Check for potentially problematic column names
        let col_name = &col_ref.name;
        
        if col_name.contains(' ') {
            result.add_warning(
                ValidationWarning::new(
                    ValidationWarningKind::UnusualPattern,
                    format!("Column name '{}' contains spaces", col_name),
                )
                .with_suggestion("Consider using quoted identifiers for names with spaces".to_string())
            );
        }

        if col_name.to_uppercase() == *col_name && col_name.len() > 1 {
            result.add_warning(
                ValidationWarning::new(
                    ValidationWarningKind::UnusualPattern,
                    format!("Column name '{}' is all uppercase", col_name),
                )
                .with_suggestion("Consider using lowercase or mixed case for better readability".to_string())
            );
        }
    }

    /// Validate literal values
    fn validate_literal_value(&self, literal: &Literal, result: &mut ValidationResult) -> Result<()> {
        match literal {
            Literal::Float(f) => {
                if f.is_infinite() {
                    result.add_error(
                        ValidationError::new(
                            ValidationErrorKind::ExpressionError,
                            "Infinite floating point values are not allowed".to_string(),
                        )
                    );
                }
                if f.is_nan() {
                    result.add_error(
                        ValidationError::new(
                            ValidationErrorKind::ExpressionError,
                            "NaN floating point values are not allowed".to_string(),
                        )
                    );
                }
            }
            Literal::String(s) => {
                if s.len() > 1000000 { // 1MB limit
                    result.add_warning(
                        ValidationWarning::new(
                            ValidationWarningKind::PerformanceWarning,
                            "Very large string literal may impact performance".to_string(),
                        )
                        .with_suggestion("Consider using parameters for large strings".to_string())
                    );
                }
            }
            _ => {}
        }

        Ok(())
    }

    /// Validate geometric expressions
    fn validate_geometric_expression(
        &self,
        _geo_expr: &geometric::GeometricExpression,
        _type_checker: &mut TypeChecker,
        result: &mut ValidationResult,
    ) -> Result<()> {
        // TODO: Add specific geometric expression validation
        // For now, just validate as generic expressions
        
        result.metadata.phases_completed.push("geometric_expression".to_string());
        Ok(())
    }

    /// Validate vector expressions
    fn validate_vector_expression(
        &self,
        vec_expr: &VectorExpression,
        _type_checker: &mut TypeChecker,
        result: &mut ValidationResult,
    ) -> Result<()> {
        match vec_expr {
            VectorExpression::Similarity { threshold, .. } => {
                if let Some(thresh) = threshold {
                    if *thresh < 0.0 || *thresh > 1.0 {
                        result.add_error(
                            ValidationError::new(
                                ValidationErrorKind::VectorError,
                                "Similarity threshold must be between 0.0 and 1.0".to_string(),
                            )
                        );
                    }
                }
            }
            VectorExpression::KNN { k, .. } => {
                if *k <= 0 {
                    result.add_error(
                        ValidationError::new(
                            ValidationErrorKind::VectorError,
                            "K in K-NN must be positive".to_string(),
                        )
                    );
                }
                if *k > 1000 {
                    result.add_warning(
                        ValidationWarning::new(
                            ValidationWarningKind::PerformanceWarning,
                            "Large K values may impact performance".to_string(),
                        )
                        .with_suggestion("Consider using smaller K for better performance".to_string())
                    );
                }
            }
        }

        result.metadata.phases_completed.push("vector_expression".to_string());
        Ok(())
    }

    /// Check WHERE clause anti-patterns
    fn check_where_antipatterns(&self, expr: &Expression, result: &mut ValidationResult) {
        match expr {
            Expression::Binary { left, op, right } => {
                // Check for 1=1 or 1=0 patterns
                if matches!(op, BinaryOperator::Equal) {
                    if let (Expression::Literal(Literal::Int(1)), Expression::Literal(Literal::Int(1))) = (left.as_ref(), right.as_ref()) {
                        result.add_warning(
                            ValidationWarning::new(
                                ValidationWarningKind::UnusualPattern,
                                "WHERE 1=1 is always true - consider removing".to_string(),
                            )
                        );
                    }
                    if let (Expression::Literal(Literal::Int(1)), Expression::Literal(Literal::Int(0))) = (left.as_ref(), right.as_ref()) {
                        result.add_warning(
                            ValidationWarning::new(
                                ValidationWarningKind::UnusualPattern,
                                "WHERE 1=0 is always false - query will return no results".to_string(),
                            )
                        );
                    }
                }

                // Recursively check sub-expressions
                self.check_where_antipatterns(left, result);
                self.check_where_antipatterns(right, result);
            }
            Expression::Unary { expr, .. } => {
                self.check_where_antipatterns(expr, result);
            }
            _ => {}
        }
    }

    /// Check equality comparisons
    fn check_equality_comparison(&self, left: &Expression, right: &Expression, result: &mut ValidationResult) {
        // Warn about comparing different literal types
        match (left, right) {
            (Expression::Literal(Literal::String(_)), Expression::Literal(Literal::Int(_))) |
            (Expression::Literal(Literal::Int(_)), Expression::Literal(Literal::String(_))) => {
                result.add_warning(
                    ValidationWarning::new(
                        ValidationWarningKind::UnusualPattern,
                        "Comparing string and integer literals - may not work as expected".to_string(),
                    )
                );
            }
            _ => {}
        }
    }

    /// Check ordering comparisons
    fn check_ordering_comparison(&self, left: &Expression, right: &Expression, result: &mut ValidationResult) {
        // Similar checks for ordering comparisons
        match (left, right) {
            (Expression::Literal(Literal::String(_)), Expression::Literal(Literal::Int(_))) |
            (Expression::Literal(Literal::Int(_)), Expression::Literal(Literal::String(_))) => {
                result.add_warning(
                    ValidationWarning::new(
                        ValidationWarningKind::UnusualPattern,
                        "Ordering comparison between string and integer - may not work as expected".to_string(),
                    )
                );
            }
            _ => {}
        }
    }
}

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

    #[test]
    fn test_division_by_zero_detection() {
        let config = ValidationConfig::default();
        let validator = ExpressionValidator::new(&config);
        let mut type_checker = TypeChecker::new();
        let mut result = ValidationResult::new();

        let expr = Expression::Binary {
            left: Box::new(Expression::Literal(Literal::Int(10))),
            op: BinaryOperator::Divide,
            right: Box::new(Expression::Literal(Literal::Int(0))),
        };

        validator.validate_expression_semantics(&expr, &mut type_checker, &mut result).unwrap();

        assert!(!result.errors.is_empty());
        assert!(result.errors.iter().any(|e| e.message.contains("Division by zero")));
    }

    #[test]
    fn test_function_argument_validation() {
        let config = ValidationConfig::default();
        let validator = ExpressionValidator::new(&config);
        let mut type_checker = TypeChecker::new();
        let mut result = ValidationResult::new();

        let expr = Expression::Function {
            name: "COUNT".to_string(),
            args: vec![], // COUNT requires an argument
        };

        validator.validate_expression_semantics(&expr, &mut type_checker, &mut result).unwrap();

        assert!(!result.errors.is_empty());
        assert!(result.errors.iter().any(|e| e.message.contains("COUNT() requires")));
    }


    #[test]
    fn test_vector_validation() {
        let config = ValidationConfig::default();
        let validator = ExpressionValidator::new(&config);
        let mut type_checker = TypeChecker::new();
        let mut result = ValidationResult::new();

        let vec_expr = VectorExpression::Similarity {
            vector_name: "embeddings".to_string(),
            reference: Box::new(Expression::Literal(Literal::String("test".to_string()))),
            metric: crate::ast::vector::similarity::SimilarityMetric::Cosine,
            threshold: Some(1.5), // Invalid threshold > 1.0
            vector_type: crate::ast::vector::similarity::VectorType::Dense { dimensions: 768 },
        };

        validator.validate_vector_expression(&vec_expr, &mut type_checker, &mut result).unwrap();

        assert!(!result.errors.is_empty());
        assert!(result.errors.iter().any(|e| e.message.contains("Similarity threshold")));
    }

    #[test]
    fn test_where_antipattern_detection() {
        let config = ValidationConfig::default();
        let validator = ExpressionValidator::new(&config);
        let mut result = ValidationResult::new();

        let expr = Expression::Binary {
            left: Box::new(Expression::Literal(Literal::Int(1))),
            op: BinaryOperator::Equal,
            right: Box::new(Expression::Literal(Literal::Int(1))),
        };

        validator.check_where_antipatterns(&expr, &mut result);

        assert!(!result.warnings.is_empty());
        assert!(result.warnings.iter().any(|w| w.message.contains("WHERE 1=1")));
    }
}