sqlexpr-rust 1.0.0

A SQL expression parser and evaluator in Rust
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
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
use crate::ast::*;
use crate::parser::{parse, ParseError};

use std::collections::HashMap;
use std::fmt;

// ============================================================================
// PUBLIC API
// ============================================================================

/// User-provided values for variable substitution
#[derive(Debug, Clone, PartialEq)]
pub enum RuntimeValue {
    Integer(i64),
    Float(f64),
    String(String),
    Boolean(bool),
    Null,
}

/// Comprehensive error type for evaluation failures
#[derive(Debug, Clone, PartialEq)]
pub enum EvalError {

    /// Parse error from the parser
    EvalParseError(String),

    /// Variable referenced but not found in value map
    UnboundVariable {
        name: String
    },

    /// Type mismatch in operation
    TypeError {
        operation: String,
        expected: String,
        actual: String,
        context: String,
    },

    /// NULL used in arithmetic or comparison operation (not IS NULL/IS NOT NULL)
    NullInOperation {
        operation: String,
        context: String,
    },

    /// Division by zero
    DivisionByZero {
        expression: String,
    },

    /// Invalid literal format
    InvalidLiteral {
        literal: String,
        literal_type: String,
        error: String,
    },
}

impl fmt::Display for EvalError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            EvalError::EvalParseError(msg) => write!(f, "Parse error: {}", msg),
            EvalError::UnboundVariable { name } => {
                write!(f, "Unbound variable '{}' - not found in value map", name)
            }
            EvalError::TypeError { operation, expected, actual, context } => {
                write!(f, "Type error in {}: expected {}, got {} (context: {})",
                    operation, expected, actual, context)
            }
            EvalError::NullInOperation { operation, context } => {
                write!(f, "NULL value in {} operation (context: {}). NULL is only allowed in IS NULL/IS NOT NULL",
                    operation, context)
            }
            EvalError::DivisionByZero { expression } => {
                write!(f, "Division by zero in expression: {}", expression)
            }
            EvalError::InvalidLiteral { literal, literal_type, error } => {
                write!(f, "Invalid {} literal '{}': {}", literal_type, literal, error)
            }
        }
    }
}

impl std::error::Error for EvalError {}

impl From<String> for EvalError {
    fn from(msg: String) -> Self {
        EvalError::EvalParseError(msg)
    }
}

impl From<&str> for EvalError {
    fn from(msg: &str) -> Self {
        EvalError::EvalParseError(msg.to_string())
    }
}

impl From<ParseError> for EvalError {
    fn from(msg: ParseError) -> Self {
        EvalError::EvalParseError(msg.to_string())
    }
}


/// Public evaluation function - evaluates a SQL boolean expression with variable bindings
///
/// # Arguments
/// * `input` - SQL expression string to evaluate (must be boolean-valued)
/// * `map` - Variable name to value bindings for substitution
///
/// # Returns
/// * `Ok(bool)` - The evaluated boolean result
/// * `Err(EvalError)` - Error during parsing, variable resolution, type checking, or evaluation
///
/// # Examples
/// ```
/// use std::collections::HashMap;
/// use sqlexpr_rust::{evaluate, RuntimeValue};
///
/// let mut map = HashMap::new();
/// map.insert("x".to_string(), RuntimeValue::Integer(42));
///
/// let result = evaluate("x > 10", &map).unwrap();
/// assert_eq!(result, true);
/// ```
pub fn evaluate(input: &str, map: &HashMap<String, RuntimeValue>) -> Result<bool, EvalError> {
    let evaluator = Evaluator::new(input, map)?;
    evaluator.eval_boolean(&evaluator.ast)

}

// ============================================================================
// EVALUATOR
// ============================================================================

/// Private evaluator implementation
struct Evaluator<'a> {
    input: String,
    ast: BooleanExpr,
    value_map: &'a HashMap<String, RuntimeValue>,
}

impl<'a> Evaluator<'a> {
    /// Create new evaluator by parsing input
    fn new(input: &str, value_map: &'a HashMap<String, RuntimeValue>) -> Result<Self, EvalError> {
        let ast = parse(input)?;
        Ok(Evaluator {
            input: input.to_string(),
            ast,
            value_map,
        })
    }

    // ========================================================================
    // BOOLEAN EXPRESSION EVALUATION
    // ========================================================================

    /// Evaluate a boolean expression
    fn eval_boolean(&self, expr: &BooleanExpr) -> Result<bool, EvalError> {
        match expr {
            BooleanExpr::Literal(b) => Ok(*b),

            BooleanExpr::Variable(name) => {
                match self.value_map.get(name) {
                    Some(RuntimeValue::Boolean(b)) => Ok(*b),
                    Some(other) => Err(EvalError::TypeError {
                        operation: "boolean variable".to_string(),
                        expected: "boolean".to_string(),
                        actual: Self::runtime_type_name(other),
                        context: format!("variable '{}'", name),
                    }),
                    None => Err(EvalError::UnboundVariable {
                        name: name.clone(),
                    }),
                }
            }

            BooleanExpr::And(left, right) => {
                let l = self.eval_boolean(left)?;
                // Short-circuit: if left is false, don't evaluate right
                if !l {
                    return Ok(false);
                }
                self.eval_boolean(right)
            }

            BooleanExpr::Or(left, right) => {
                let l = self.eval_boolean(left)?;
                // Short-circuit: if left is true, don't evaluate right
                if l {
                    return Ok(true);
                }
                self.eval_boolean(right)
            }

            BooleanExpr::Not(expr) => {
                Ok(!self.eval_boolean(expr)?)
            }

            BooleanExpr::Relational(rel) => {
                self.eval_relational(rel)
            }
        }
    }

    // ========================================================================
    // RELATIONAL EXPRESSION EVALUATION
    // ========================================================================

    /// Evaluate a relational expression to boolean
    fn eval_relational(&self, expr: &RelationalExpr) -> Result<bool, EvalError> {
        match expr {
            RelationalExpr::Equality { left, op, right } => {
                self.eval_equality(left, right, *op)
            }

            RelationalExpr::Comparison { left, op, right } => {
                self.eval_comparison(left, right, *op)
            }

            RelationalExpr::Like { expr, pattern, escape, negated } => {
                self.eval_like(expr, pattern, escape.as_ref(), *negated)
            }

            RelationalExpr::Between { expr, lower, upper, negated } => {
                self.eval_between(expr, lower, upper, *negated)
            }

            RelationalExpr::In { expr, values, negated } => {
                self.eval_in(expr, values, *negated)
            }

            RelationalExpr::IsNull { expr, negated } => {
                self.eval_is_null(expr, *negated)
            }
        }
    }

    /// Evaluate equality/inequality operators
    fn eval_equality(&self, left: &ValueExpr, right: &ValueExpr, op: EqualityOp)
        -> Result<bool, EvalError>
    {
        let l_val = self.eval_value(left)?;
        let r_val = self.eval_value(right)?;

        // NULL handling
        if l_val.is_null() || r_val.is_null() {
            return Err(EvalError::NullInOperation {
                operation: format!("{:?}", op),
                context: "cannot compare NULL values (use IS NULL instead)".to_string(),
            });
        }

        let equal = match (&l_val, &r_val) {
            // Numeric comparisons
            (SubValue::Integer(a), SubValue::Integer(b)) => a == b,
            (SubValue::Float(a), SubValue::Float(b)) => a == b,
            (SubValue::Integer(a), SubValue::Float(b)) => (*a as f64) == *b,
            (SubValue::Float(a), SubValue::Integer(b)) => *a == (*b as f64),

            // String comparisons
            (SubValue::String(a), SubValue::String(b)) => a == b,

            // Boolean comparisons (only for equality)
            (SubValue::Boolean(a), SubValue::Boolean(b)) => a == b,

            // Type mismatch
            _ => return Err(EvalError::TypeError {
                operation: format!("{:?}", op),
                expected: "matching types".to_string(),
                actual: format!("{} vs {}", l_val.type_name(), r_val.type_name()),
                context: "equality comparison".to_string(),
            }),
        };

        Ok(match op {
            EqualityOp::Equal => equal,
            EqualityOp::NotEqual => !equal,
        })
    }

    /// Evaluate comparison operators (>, <, >=, <=)
    fn eval_comparison(&self, left: &ValueExpr, right: &ValueExpr, op: ComparisonOp)
        -> Result<bool, EvalError>
    {
        let l_val = self.eval_value(left)?;
        let r_val = self.eval_value(right)?;

        // NULL handling
        if l_val.is_null() || r_val.is_null() {
            return Err(EvalError::NullInOperation {
                operation: format!("{:?}", op),
                context: "cannot compare NULL values".to_string(),
            });
        }

        match (&l_val, &r_val) {
            // Numeric comparisons
            (SubValue::Integer(a), SubValue::Integer(b)) => {
                Ok(Self::apply_comparison_op(*a, *b, op))
            }
            (SubValue::Float(a), SubValue::Float(b)) => {
                Ok(Self::apply_comparison_op(*a, *b, op))
            }
            (SubValue::Integer(a), SubValue::Float(b)) => {
                Ok(Self::apply_comparison_op(*a as f64, *b, op))
            }
            (SubValue::Float(a), SubValue::Integer(b)) => {
                Ok(Self::apply_comparison_op(*a, *b as f64, op))
            }

            // String comparisons (lexicographic)
            (SubValue::String(a), SubValue::String(b)) => {
                Ok(Self::apply_comparison_op(a, b, op))
            }

            // Boolean not allowed in comparisons
            (SubValue::Boolean(_), _) | (_, SubValue::Boolean(_)) => {
                Err(EvalError::TypeError {
                    operation: format!("{:?}", op),
                    expected: "numeric or string".to_string(),
                    actual: "boolean".to_string(),
                    context: "comparison operand".to_string(),
                })
            }

            // Type mismatch
            _ => Err(EvalError::TypeError {
                operation: format!("{:?}", op),
                expected: "matching types".to_string(),
                actual: format!("{} vs {}", l_val.type_name(), r_val.type_name()),
                context: "comparison".to_string(),
            }),
        }
    }

    fn apply_comparison_op<T: PartialOrd>(a: T, b: T, op: ComparisonOp) -> bool {
        match op {
            ComparisonOp::GreaterThan => a > b,
            ComparisonOp::GreaterOrEqual => a >= b,
            ComparisonOp::LessThan => a < b,
            ComparisonOp::LessOrEqual => a <= b,
        }
    }

    /// Evaluate LIKE operator with wildcards
    fn eval_like(&self, expr: &ValueExpr, pattern: &str, escape: Option<&String>, negated: bool)
        -> Result<bool, EvalError>
    {
        let val = self.eval_value(expr)?;

        let string_val = match val {
            SubValue::String(s) => s,
            SubValue::Null => {
                return Err(EvalError::NullInOperation {
                    operation: "LIKE".to_string(),
                    context: "cannot apply LIKE to NULL".to_string(),
                });
            }
            _ => {
                return Err(EvalError::TypeError {
                    operation: "LIKE".to_string(),
                    expected: "string".to_string(),
                    actual: val.type_name(),
                    context: "left operand".to_string(),
                });
            }
        };

        let matches = Self::match_pattern(&string_val, pattern, escape)?;
        Ok(if negated { !matches } else { matches })
    }

    /// Pattern matching with SQL wildcards (% = any chars, _ = single char)
    fn match_pattern(s: &str, pattern: &str, escape: Option<&String>) -> Result<bool, EvalError> {
        let escape_char = escape.and_then(|e| e.chars().next());

        // Convert SQL pattern to regex
        let mut regex_pattern = String::from("^");
        let mut chars = pattern.chars().peekable();

        while let Some(ch) = chars.next() {
            if Some(ch) == escape_char {
                // Escaped character - treat next character literally
                if let Some(next) = chars.next() {
                    regex_pattern.push_str(&regex::escape(&next.to_string()));
                }
            } else if ch == '%' {
                regex_pattern.push_str(".*");
            } else if ch == '_' {
                regex_pattern.push('.');
            } else {
                regex_pattern.push_str(&regex::escape(&ch.to_string()));
            }
        }
        regex_pattern.push('$');

        let re = regex::Regex::new(&regex_pattern)
            .map_err(|e| EvalError::InvalidLiteral {
                literal: pattern.to_string(),
                literal_type: "LIKE pattern".to_string(),
                error: format!("{}", e),
            })?;

        Ok(re.is_match(s))
    }

    /// Evaluate BETWEEN operator
    fn eval_between(&self, expr: &ValueExpr, lower: &ValueExpr, upper: &ValueExpr, negated: bool)
        -> Result<bool, EvalError>
    {
        let val = self.eval_value(expr)?;
        let low = self.eval_value(lower)?;
        let high = self.eval_value(upper)?;

        // Check for NULL
        if val.is_null() || low.is_null() || high.is_null() {
            return Err(EvalError::NullInOperation {
                operation: "BETWEEN".to_string(),
                context: "cannot use NULL in BETWEEN".to_string(),
            });
        }

        // All must be same comparable type
        let in_range = match (&val, &low, &high) {
            (SubValue::Integer(v), SubValue::Integer(l), SubValue::Integer(h)) => {
                v >= l && v <= h
            }
            (SubValue::Float(v), SubValue::Float(l), SubValue::Float(h)) => {
                v >= l && v <= h
            }
            (SubValue::String(v), SubValue::String(l), SubValue::String(h)) => {
                v >= l && v <= h
            }
            // Mixed numeric types need coercion
            _ => {
                // Try numeric comparison with coercion
                let v_num = Self::to_numeric(&val)?;
                let l_num = Self::to_numeric(&low)?;
                let h_num = Self::to_numeric(&high)?;
                v_num >= l_num && v_num <= h_num
            }
        };

        Ok(if negated { !in_range } else { in_range })
    }

    /// Evaluate IN operator
    fn eval_in(&self, expr: &ValueExpr, values: &[ValueLiteral], negated: bool)
        -> Result<bool, EvalError>
    {
        let val = self.eval_value(expr)?;

        if val.is_null() {
            return Err(EvalError::NullInOperation {
                operation: "IN".to_string(),
                context: "cannot use NULL in IN".to_string(),
            });
        }

        // Type consistency of the values list is now guaranteed by the parser,
        // so we only need to check if the left operand is type-compatible with the list
        if !values.is_empty() {
            let first_list_val = SubValue::from_literal(&values[0]);
            let is_compatible = Self::are_types_compatible_for_in(&val, &first_list_val);

            // If list is incompatible with left value, it's a type error
            if !is_compatible {
                return Err(EvalError::TypeError {
                    operation: "IN".to_string(),
                    expected: first_list_val.type_name(),
                    actual: val.type_name(),
                    context: "left operand type doesn't match list element types".to_string(),
                });
            }

            // Note: Type consistency check for list elements removed - now done at parse time
        }

        let mut found = false;
        for lit_val in values {
            let list_val = SubValue::from_literal(lit_val);

            // Check if values match (with type compatibility)
            let matches = match (&val, &list_val) {
                (SubValue::Integer(a), SubValue::Integer(b)) => a == b,
                (SubValue::Float(a), SubValue::Float(b)) => a == b,
                (SubValue::Integer(a), SubValue::Float(b)) => (*a as f64) == *b,
                (SubValue::Float(a), SubValue::Integer(b)) => *a == (*b as f64),
                (SubValue::String(a), SubValue::String(b)) => a == b,
                (SubValue::Boolean(a), SubValue::Boolean(b)) => a == b,
                (SubValue::Null, SubValue::Null) => true,
                _ => false,  // Means no match since type mismatches are caused above
            };

            if matches {
                found = true;
                break;
            }
        }

        Ok(if negated { !found } else { found })
    }

    /// Evaluate IS NULL operator
    fn eval_is_null(&self, expr: &ValueExpr, negated: bool) -> Result<bool, EvalError> {
        let val = self.eval_value(expr)?;
        let is_null = val.is_null();
        Ok(if negated { !is_null } else { is_null })
    }

    // ========================================================================
    // VALUE EXPRESSION EVALUATION
    // ========================================================================

    /// Evaluate a value expression to a concrete value
    fn eval_value(&self, expr: &ValueExpr) -> Result<SubValue, EvalError> {
        match expr {
            ValueExpr::Literal(lit) => Ok(SubValue::from_literal(lit)),

            ValueExpr::Variable(name) => {
                match self.value_map.get(name) {
                    Some(rv) => Ok(SubValue::from_runtime(rv)),
                    None => Err(EvalError::UnboundVariable {
                        name: name.clone(),
                    }),
                }
            }

            ValueExpr::Add(l, r) => self.eval_arithmetic_add(l, r),
            ValueExpr::Subtract(l, r) => self.eval_arithmetic_subtract(l, r),
            ValueExpr::Multiply(l, r) => self.eval_arithmetic_multiply(l, r),
            ValueExpr::Divide(l, r) => self.eval_arithmetic_divide(l, r),
            ValueExpr::Modulo(l, r) => self.eval_arithmetic_modulo(l, r),

            ValueExpr::UnaryPlus(e) => {
                let val = self.eval_value(e)?;
                match val {
                    SubValue::Integer(i) => Ok(SubValue::Integer(i)),
                    SubValue::Float(f) => Ok(SubValue::Float(f)),
                    SubValue::Null => Err(EvalError::NullInOperation {
                        operation: "unary plus".to_string(),
                        context: "cannot apply unary plus to NULL".to_string(),
                    }),
                    _ => Err(EvalError::TypeError {
                        operation: "unary plus".to_string(),
                        expected: "numeric".to_string(),
                        actual: val.type_name(),
                        context: "operand".to_string(),
                    }),
                }
            }

            ValueExpr::UnaryMinus(e) => {
                let val = self.eval_value(e)?;
                match val {
                    SubValue::Integer(i) => Ok(SubValue::Integer(-i)),
                    SubValue::Float(f) => Ok(SubValue::Float(-f)),
                    SubValue::Null => Err(EvalError::NullInOperation {
                        operation: "unary minus".to_string(),
                        context: "cannot apply unary minus to NULL".to_string(),
                    }),
                    _ => Err(EvalError::TypeError {
                        operation: "unary minus".to_string(),
                        expected: "numeric".to_string(),
                        actual: val.type_name(),
                        context: "operand".to_string(),
                    }),
                }
            }
        }
    }

    /// Arithmetic addition with type checking and coercion
    fn eval_arithmetic_add(&self, l: &ValueExpr, r: &ValueExpr) -> Result<SubValue, EvalError> {
        let left = self.eval_value(l)?;
        let right = self.eval_value(r)?;

        // Check for NULL
        if left.is_null() || right.is_null() {
            return Err(EvalError::NullInOperation {
                operation: "addition".to_string(),
                context: "cannot add NULL values".to_string(),
            });
        }

        match (&left, &right) {
            (SubValue::Integer(a), SubValue::Integer(b)) => {
                Ok(SubValue::Integer(a + b))
            }
            (SubValue::Float(a), SubValue::Float(b)) => {
                Ok(SubValue::Float(a + b))
            }
            // Type coercion: int + float = float
            (SubValue::Integer(a), SubValue::Float(b)) => {
                Ok(SubValue::Float(*a as f64 + b))
            }
            (SubValue::Float(a), SubValue::Integer(b)) => {
                Ok(SubValue::Float(a + *b as f64))
            }
            _ => Err(EvalError::TypeError {
                operation: "addition".to_string(),
                expected: "numeric types".to_string(),
                actual: format!("{} and {}", left.type_name(), right.type_name()),
                context: "arithmetic operation".to_string(),
            }),
        }
    }

    /// Arithmetic subtraction
    fn eval_arithmetic_subtract(&self, l: &ValueExpr, r: &ValueExpr) -> Result<SubValue, EvalError> {
        let left = self.eval_value(l)?;
        let right = self.eval_value(r)?;

        if left.is_null() || right.is_null() {
            return Err(EvalError::NullInOperation {
                operation: "subtraction".to_string(),
                context: "cannot subtract NULL values".to_string(),
            });
        }

        match (&left, &right) {
            (SubValue::Integer(a), SubValue::Integer(b)) => {
                Ok(SubValue::Integer(a - b))
            }
            (SubValue::Float(a), SubValue::Float(b)) => {
                Ok(SubValue::Float(a - b))
            }
            (SubValue::Integer(a), SubValue::Float(b)) => {
                Ok(SubValue::Float(*a as f64 - b))
            }
            (SubValue::Float(a), SubValue::Integer(b)) => {
                Ok(SubValue::Float(a - *b as f64))
            }
            _ => Err(EvalError::TypeError {
                operation: "subtraction".to_string(),
                expected: "numeric types".to_string(),
                actual: format!("{} and {}", left.type_name(), right.type_name()),
                context: "arithmetic operation".to_string(),
            }),
        }
    }

    /// Arithmetic multiplication
    fn eval_arithmetic_multiply(&self, l: &ValueExpr, r: &ValueExpr) -> Result<SubValue, EvalError> {
        let left = self.eval_value(l)?;
        let right = self.eval_value(r)?;

        if left.is_null() || right.is_null() {
            return Err(EvalError::NullInOperation {
                operation: "multiplication".to_string(),
                context: "cannot multiply NULL values".to_string(),
            });
        }

        match (&left, &right) {
            (SubValue::Integer(a), SubValue::Integer(b)) => {
                Ok(SubValue::Integer(a * b))
            }
            (SubValue::Float(a), SubValue::Float(b)) => {
                Ok(SubValue::Float(a * b))
            }
            (SubValue::Integer(a), SubValue::Float(b)) => {
                Ok(SubValue::Float(*a as f64 * b))
            }
            (SubValue::Float(a), SubValue::Integer(b)) => {
                Ok(SubValue::Float(a * *b as f64))
            }
            _ => Err(EvalError::TypeError {
                operation: "multiplication".to_string(),
                expected: "numeric types".to_string(),
                actual: format!("{} and {}", left.type_name(), right.type_name()),
                context: "arithmetic operation".to_string(),
            }),
        }
    }

    /// Division with mandatory float coercion
    fn eval_arithmetic_divide(&self, l: &ValueExpr, r: &ValueExpr) -> Result<SubValue, EvalError> {
        let left = self.eval_value(l)?;
        let right = self.eval_value(r)?;

        if left.is_null() || right.is_null() {
            return Err(EvalError::NullInOperation {
                operation: "division".to_string(),
                context: "cannot divide NULL values".to_string(),
            });
        }

        // Convert both to float for division
        let left_float = match left {
            SubValue::Integer(i) => i as f64,
            SubValue::Float(f) => f,
            _ => return Err(EvalError::TypeError {
                operation: "division".to_string(),
                expected: "numeric".to_string(),
                actual: left.type_name(),
                context: "left operand".to_string(),
            }),
        };

        let right_float = match right {
            SubValue::Integer(i) => i as f64,
            SubValue::Float(f) => f,
            _ => return Err(EvalError::TypeError {
                operation: "division".to_string(),
                expected: "numeric".to_string(),
                actual: right.type_name(),
                context: "right operand".to_string(),
            }),
        };

        if right_float == 0.0 {
            return Err(EvalError::DivisionByZero {
                expression: self.input.clone(),
            });
        }

        Ok(SubValue::Float(left_float / right_float))
    }

    /// Arithmetic modulo
    fn eval_arithmetic_modulo(&self, l: &ValueExpr, r: &ValueExpr) -> Result<SubValue, EvalError> {
        let left = self.eval_value(l)?;
        let right = self.eval_value(r)?;

        if left.is_null() || right.is_null() {
            return Err(EvalError::NullInOperation {
                operation: "modulo".to_string(),
                context: "cannot modulo NULL values".to_string(),
            });
        }

        match (&left, &right) {
            (SubValue::Integer(a), SubValue::Integer(b)) => {
                if *b == 0 {
                    return Err(EvalError::DivisionByZero {
                        expression: self.input.clone(),
                    });
                }
                Ok(SubValue::Integer(a % b))
            }
            (SubValue::Float(a), SubValue::Float(b)) => {
                if *b == 0.0 {
                    return Err(EvalError::DivisionByZero {
                        expression: self.input.clone(),
                    });
                }
                Ok(SubValue::Float(a % b))
            }
            (SubValue::Integer(a), SubValue::Float(b)) => {
                if *b == 0.0 {
                    return Err(EvalError::DivisionByZero {
                        expression: self.input.clone(),
                    });
                }
                Ok(SubValue::Float((*a as f64) % b))
            }
            (SubValue::Float(a), SubValue::Integer(b)) => {
                if *b == 0 {
                    return Err(EvalError::DivisionByZero {
                        expression: self.input.clone(),
                    });
                }
                Ok(SubValue::Float(a % (*b as f64)))
            }
            _ => Err(EvalError::TypeError {
                operation: "modulo".to_string(),
                expected: "numeric types".to_string(),
                actual: format!("{} and {}", left.type_name(), right.type_name()),
                context: "arithmetic operation".to_string(),
            }),
        }
    }

    // ========================================================================
    // HELPER FUNCTIONS
    // ========================================================================

    fn to_numeric(val: &SubValue) -> Result<f64, EvalError> {
        match val {
            SubValue::Integer(i) => Ok(*i as f64),
            SubValue::Float(f) => Ok(*f),
            _ => Err(EvalError::TypeError {
                operation: "numeric comparison".to_string(),
                expected: "numeric".to_string(),
                actual: val.type_name(),
                context: "operand".to_string(),
            }),
        }
    }

    /// Check if two types are compatible for IN operator
    /// (allows int/float mixing, but not string/numeric, etc.)
    fn are_types_compatible_for_in(left: &SubValue, right: &SubValue) -> bool {
        match (left, right) {
            // Exact matches
            (SubValue::Integer(_), SubValue::Integer(_)) => true,
            (SubValue::Float(_), SubValue::Float(_)) => true,
            (SubValue::String(_), SubValue::String(_)) => true,
            (SubValue::Boolean(_), SubValue::Boolean(_)) => true,
            (SubValue::Null, SubValue::Null) => true,
            // Numeric type mixing is allowed
            (SubValue::Integer(_), SubValue::Float(_)) => true,
            (SubValue::Float(_), SubValue::Integer(_)) => true,
            // Everything else is incompatible
            _ => false,
        }
    }

    fn runtime_type_name(rv: &RuntimeValue) -> String {
        match rv {
            RuntimeValue::Integer(_) => "integer".to_string(),
            RuntimeValue::Float(_) => "float".to_string(),
            RuntimeValue::String(_) => "string".to_string(),
            RuntimeValue::Boolean(_) => "boolean".to_string(),
            RuntimeValue::Null => "NULL".to_string(),
        }
    }
}

// ============================================================================
// INTERNAL TYPES
// ============================================================================

/// Substituted values - what AST nodes become after variable substitution
#[derive(Debug, Clone, PartialEq)]
enum SubValue {
    Integer(i64),
    Float(f64),
    String(String),
    Boolean(bool),
    Null,
}

impl SubValue {
    /// Convert from RuntimeValue
    fn from_runtime(rv: &RuntimeValue) -> Self {
        match rv {
            RuntimeValue::Integer(i) => SubValue::Integer(*i),
            RuntimeValue::Float(f) => SubValue::Float(*f),
            RuntimeValue::String(s) => SubValue::String(s.clone()),
            RuntimeValue::Boolean(b) => SubValue::Boolean(*b),
            RuntimeValue::Null => SubValue::Null,
        }
    }

    /// Convert from ValueLiteral
    fn from_literal(lit: &ValueLiteral) -> Self {
        match lit {
            ValueLiteral::Integer(i) => SubValue::Integer(*i),
            ValueLiteral::Float(f) => SubValue::Float(*f),
            ValueLiteral::String(s) => SubValue::String(s.clone()),
            ValueLiteral::Boolean(b) => SubValue::Boolean(*b),
            ValueLiteral::Null => SubValue::Null,
        }
    }

    fn type_name(&self) -> String {
        match self {
            SubValue::Integer(_) => "integer".to_string(),
            SubValue::Float(_) => "float".to_string(),
            SubValue::String(_) => "string".to_string(),
            SubValue::Boolean(_) => "boolean".to_string(),
            SubValue::Null => "NULL".to_string(),
        }
    }

    fn is_null(&self) -> bool {
        matches!(self, SubValue::Null)
    }
}