vibesql-executor 0.2.0

Query execution engine for vibesql SQL database
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
//! Arithmetic operator implementations
//!
//! Handles: +, -, *, /
//! Supports: Integer, Smallint, Bigint, Float, Real, Double, Numeric types
//! Includes: Type coercion, mixed-type arithmetic, division-by-zero handling
//!
//! SQLite compatibility: NaN results are converted to NULL

mod addition;
mod division;
mod modulo;
mod multiplication;
mod subtraction;

pub use addition::Addition;
pub use division::Division;
pub use modulo::Modulo;
pub use multiplication::Multiplication;
pub use subtraction::Subtraction;
use vibesql_types::SqlValue;

/// Convert NaN floating-point results to NULL for SQLite compatibility.
///
/// SQLite converts NaN (Not a Number) results to NULL. This happens when:
/// - Infinity * 0.0
/// - 0.0 / 0.0
/// - Infinity - Infinity
///
/// Example:
/// ```sql
/// -- SQLite behavior:
/// SELECT (1e300 * 1e300) * 0.0;  -- Returns NULL (not NaN)
/// SELECT coalesce((1e300 * 1e300) * 0.0, 99.0);  -- Returns 99.0
/// ```
#[inline]
pub(super) fn nan_to_null(value: SqlValue) -> SqlValue {
    match &value {
        SqlValue::Float(f) if f.is_nan() => SqlValue::Null,
        SqlValue::Real(f) if f.is_nan() => SqlValue::Null,
        SqlValue::Double(f) if f.is_nan() => SqlValue::Null,
        SqlValue::Numeric(f) if f.is_nan() => SqlValue::Null,
        _ => value,
    }
}

use crate::{
    errors::ExecutorError,
    evaluator::casting::{
        boolean_to_i64, is_approximate_numeric, is_exact_numeric, string_to_number, to_f64, to_i64,
    },
};

/// Result of type coercion for arithmetic operations
pub(super) enum CoercedValues {
    ExactNumeric(i64, i64),
    ApproximateNumeric(f64, f64),
    Numeric(f64, f64),
}

/// Helper to convert a value to numeric, with string coercion support
/// Returns (value_as_i64, value_as_f64, is_float)
fn coerce_single_value(value: &SqlValue) -> Option<(i64, f64, bool)> {
    match value {
        SqlValue::Integer(n) => Some((*n, *n as f64, false)),
        SqlValue::Smallint(n) => Some((*n as i64, *n as f64, false)),
        SqlValue::Bigint(n) => Some((*n, *n as f64, false)),
        SqlValue::Unsigned(n) => Some((*n as i64, *n as f64, false)),
        SqlValue::Float(f) => Some((*f as i64, *f as f64, true)),
        SqlValue::Real(f) => Some((*f as i64, *f as f64, true)),
        SqlValue::Double(f) => Some((*f as i64, *f, true)),
        SqlValue::Numeric(f) => Some((*f as i64, *f, true)),
        SqlValue::Boolean(b) => Some((if *b { 1 } else { 0 }, if *b { 1.0 } else { 0.0 }, false)),
        SqlValue::Varchar(s) | SqlValue::Character(s) => {
            let (i, f, is_float) = string_to_number(s);
            Some((i, f, is_float))
        }
        _ => None,
    }
}

/// Helper function to coerce two values to a common numeric type
pub(super) fn coerce_numeric_values(
    left: &SqlValue,
    right: &SqlValue,
    op: &str,
) -> Result<CoercedValues, ExecutorError> {
    use SqlValue::*;

    // Handle Boolean coercion first
    if matches!(left, Boolean(_)) || matches!(right, Boolean(_)) {
        let left_i64 = boolean_to_i64(left).or_else(|| to_i64(left).ok()).ok_or_else(|| {
            ExecutorError::TypeMismatch {
                left: left.clone(),
                op: op.to_string(),
                right: right.clone(),
            }
        })?;

        let right_i64 = boolean_to_i64(right).or_else(|| to_i64(right).ok()).ok_or_else(|| {
            ExecutorError::TypeMismatch {
                left: left.clone(),
                op: op.to_string(),
                right: right.clone(),
            }
        })?;

        return Ok(CoercedValues::ExactNumeric(left_i64, right_i64));
    }

    // Handle string coercion (SQLite compatibility)
    // When either operand is a string, coerce it to a number
    if matches!(left, Varchar(_) | Character(_)) || matches!(right, Varchar(_) | Character(_)) {
        let left_coerced =
            coerce_single_value(left).ok_or_else(|| ExecutorError::TypeMismatch {
                left: left.clone(),
                op: op.to_string(),
                right: right.clone(),
            })?;

        let right_coerced =
            coerce_single_value(right).ok_or_else(|| ExecutorError::TypeMismatch {
                left: left.clone(),
                op: op.to_string(),
                right: right.clone(),
            })?;

        // If either value is a float, use ApproximateNumeric
        if left_coerced.2 || right_coerced.2 {
            return Ok(CoercedValues::ApproximateNumeric(left_coerced.1, right_coerced.1));
        } else {
            return Ok(CoercedValues::ExactNumeric(left_coerced.0, right_coerced.0));
        }
    }

    // Mixed exact numeric types - integer arithmetic returns Integer type in both modes
    // This matches MySQL behavior where integer arithmetic produces integers
    if is_exact_numeric(left) && is_exact_numeric(right) {
        let left_i64 = to_i64(left)?;
        let right_i64 = to_i64(right)?;
        return Ok(CoercedValues::ExactNumeric(left_i64, right_i64));
    }

    // Approximate numeric types - promote to f64
    if is_approximate_numeric(left) && is_approximate_numeric(right) {
        let left_f64 = to_f64(left)?;
        let right_f64 = to_f64(right)?;
        return Ok(CoercedValues::ApproximateNumeric(left_f64, right_f64));
    }

    // Mixed Float/Integer - promote to f64
    if (matches!(left, Float(_) | Real(_) | Double(_))
        && matches!(right, Integer(_) | Smallint(_) | Bigint(_)))
        || (matches!(left, Integer(_) | Smallint(_) | Bigint(_))
            && matches!(right, Float(_) | Real(_) | Double(_)))
    {
        let left_f64 = to_f64(left)?;
        let right_f64 = to_f64(right)?;
        return Ok(CoercedValues::ApproximateNumeric(left_f64, right_f64));
    }

    // NUMERIC with any numeric type - preserve Numeric type
    if (matches!(left, Numeric(_))
        && matches!(
            right,
            Integer(_) | Smallint(_) | Bigint(_) | Float(_) | Real(_) | Double(_) | Numeric(_)
        ))
        || (matches!(left, Integer(_) | Smallint(_) | Bigint(_) | Float(_) | Real(_) | Double(_))
            && matches!(right, Numeric(_)))
    {
        let left_f64 = to_f64(left)?;
        let right_f64 = to_f64(right)?;
        return Ok(CoercedValues::Numeric(left_f64, right_f64));
    }

    // Type mismatch
    Err(ExecutorError::TypeMismatch {
        left: left.clone(),
        op: op.to_string(),
        right: right.clone(),
    })
}

pub(crate) struct ArithmeticOps;

impl ArithmeticOps {
    /// Addition operator (+)
    #[inline]
    pub fn add(left: &SqlValue, right: &SqlValue) -> Result<SqlValue, ExecutorError> {
        Addition::add(left, right)
    }

    /// Subtraction operator (-)
    #[inline]
    pub fn subtract(left: &SqlValue, right: &SqlValue) -> Result<SqlValue, ExecutorError> {
        Subtraction::subtract(left, right)
    }

    /// Multiplication operator (*)
    #[inline]
    pub fn multiply(left: &SqlValue, right: &SqlValue) -> Result<SqlValue, ExecutorError> {
        Multiplication::multiply(left, right)
    }

    /// Division operator (/)
    #[inline]
    pub fn divide(
        left: &SqlValue,
        right: &SqlValue,
        sql_mode: vibesql_types::SqlMode,
    ) -> Result<SqlValue, ExecutorError> {
        Division::divide(left, right, sql_mode)
    }

    /// Integer division operator (DIV) - MySQL-specific
    /// Returns integer result, truncating fractional part (truncates toward zero)
    #[inline]
    pub fn integer_divide(left: &SqlValue, right: &SqlValue) -> Result<SqlValue, ExecutorError> {
        Division::integer_divide(left, right)
    }

    /// Modulo operator (%)
    /// Returns the remainder of division
    #[inline]
    pub fn modulo(left: &SqlValue, right: &SqlValue) -> Result<SqlValue, ExecutorError> {
        Modulo::modulo(left, right)
    }
}

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

    #[test]
    fn test_integer_addition() {
        let result = ArithmeticOps::add(&SqlValue::Integer(5), &SqlValue::Integer(3)).unwrap();
        assert_eq!(result, SqlValue::Integer(8));
    }

    #[test]
    fn test_integer_subtraction() {
        let result = ArithmeticOps::subtract(&SqlValue::Integer(5), &SqlValue::Integer(3)).unwrap();
        assert_eq!(result, SqlValue::Integer(2));
    }

    #[test]
    fn test_integer_multiplication() {
        let result = ArithmeticOps::multiply(&SqlValue::Integer(5), &SqlValue::Integer(3)).unwrap();
        assert_eq!(result, SqlValue::Integer(15));
    }

    #[test]
    fn test_integer_division_sqlite() {
        // SQLite mode: Division returns Integer for integer operands (truncated)
        let sqlite_mode = vibesql_types::SqlMode::SQLite;
        let result = ArithmeticOps::divide(
            &SqlValue::Integer(15),
            &SqlValue::Integer(3),
            sqlite_mode.clone(),
        )
        .unwrap();
        assert_eq!(result, SqlValue::Integer(5));

        // Test truncation behavior
        let result =
            ArithmeticOps::divide(&SqlValue::Integer(15), &SqlValue::Integer(4), sqlite_mode)
                .unwrap();
        assert_eq!(result, SqlValue::Integer(3)); // 15/4 = 3.75 truncates to 3
    }

    #[test]
    fn test_integer_division_mysql() {
        // MySQL mode: Division returns Numeric for integer operands (exact decimal arithmetic)
        let mysql_mode = vibesql_types::SqlMode::MySQL { flags: Default::default() };
        let result = ArithmeticOps::divide(
            &SqlValue::Integer(15),
            &SqlValue::Integer(3),
            mysql_mode.clone(),
        )
        .unwrap();
        assert_eq!(result, SqlValue::Numeric(5.0));

        let result =
            ArithmeticOps::divide(&SqlValue::Integer(15), &SqlValue::Integer(4), mysql_mode)
                .unwrap();
        assert_eq!(result, SqlValue::Numeric(3.75));
    }

    #[test]
    fn test_division_by_zero() {
        // Division by zero should return NULL (SQL standard behavior)
        let result = ArithmeticOps::divide(
            &SqlValue::Integer(5),
            &SqlValue::Integer(0),
            vibesql_types::SqlMode::default(),
        );
        assert_eq!(result.unwrap(), SqlValue::Null);
    }

    #[test]
    fn test_mixed_exact_numeric() {
        let result = ArithmeticOps::add(&SqlValue::Smallint(5), &SqlValue::Bigint(3)).unwrap();
        assert_eq!(result, SqlValue::Integer(8));
    }

    #[test]
    fn test_float_arithmetic() {
        let result = ArithmeticOps::add(&SqlValue::Float(5.5), &SqlValue::Float(3.2)).unwrap();
        match result {
            SqlValue::Float(f) => assert!((f - 8.7).abs() < 0.01),
            _ => panic!("Expected Float result"),
        }
    }

    #[test]
    fn test_mixed_float_integer() {
        let result = ArithmeticOps::add(&SqlValue::Float(5.5), &SqlValue::Integer(3)).unwrap();
        match result {
            SqlValue::Float(f) => assert!((f - 8.5).abs() < 0.01),
            _ => panic!("Expected Float result"),
        }
    }

    // Boolean coercion tests
    #[test]
    fn test_boolean_true_addition() {
        // TRUE + 40 = 41
        let result = ArithmeticOps::add(&SqlValue::Boolean(true), &SqlValue::Integer(40)).unwrap();
        assert_eq!(result, SqlValue::Integer(41));
    }

    #[test]
    fn test_boolean_false_addition() {
        // FALSE + 40 = 40
        let result = ArithmeticOps::add(&SqlValue::Boolean(false), &SqlValue::Integer(40)).unwrap();
        assert_eq!(result, SqlValue::Integer(40));
    }

    #[test]
    fn test_integer_plus_boolean() {
        // 40 + TRUE = 41
        let result = ArithmeticOps::add(&SqlValue::Integer(40), &SqlValue::Boolean(true)).unwrap();
        assert_eq!(result, SqlValue::Integer(41));
    }

    #[test]
    fn test_boolean_multiplication() {
        // 97 * TRUE = 97
        let result =
            ArithmeticOps::multiply(&SqlValue::Integer(97), &SqlValue::Boolean(true)).unwrap();
        assert_eq!(result, SqlValue::Integer(97));

        // 97 * FALSE = 0
        let result =
            ArithmeticOps::multiply(&SqlValue::Integer(97), &SqlValue::Boolean(false)).unwrap();
        assert_eq!(result, SqlValue::Integer(0));
    }

    #[test]
    fn test_boolean_subtraction() {
        // TRUE - FALSE = 1
        let result =
            ArithmeticOps::subtract(&SqlValue::Boolean(true), &SqlValue::Boolean(false)).unwrap();
        assert_eq!(result, SqlValue::Integer(1));

        // 5 - TRUE = 4
        let result =
            ArithmeticOps::subtract(&SqlValue::Integer(5), &SqlValue::Boolean(true)).unwrap();
        assert_eq!(result, SqlValue::Integer(4));
    }

    #[test]
    fn test_boolean_division() {
        // SQLite mode: 10 / TRUE = 10 (integer division)
        let sqlite_mode = vibesql_types::SqlMode::SQLite;
        let result = ArithmeticOps::divide(
            &SqlValue::Integer(10),
            &SqlValue::Boolean(true),
            sqlite_mode.clone(),
        )
        .unwrap();
        assert_eq!(result, SqlValue::Integer(10));

        // TRUE / TRUE = 1
        let result =
            ArithmeticOps::divide(&SqlValue::Boolean(true), &SqlValue::Boolean(true), sqlite_mode)
                .unwrap();
        assert_eq!(result, SqlValue::Integer(1));
    }

    #[test]
    fn test_boolean_division_mysql() {
        // MySQL mode: 10 / TRUE = 10.0 (exact decimal division)
        let mysql_mode = vibesql_types::SqlMode::MySQL { flags: Default::default() };
        let result = ArithmeticOps::divide(
            &SqlValue::Integer(10),
            &SqlValue::Boolean(true),
            mysql_mode.clone(),
        )
        .unwrap();
        assert_eq!(result, SqlValue::Numeric(10.0));

        // TRUE / TRUE = 1.0
        let result =
            ArithmeticOps::divide(&SqlValue::Boolean(true), &SqlValue::Boolean(true), mysql_mode)
                .unwrap();
        assert_eq!(result, SqlValue::Numeric(1.0));
    }

    #[test]
    fn test_boolean_division_by_false() {
        // 10 / FALSE should return NULL (SQL standard behavior)
        let result = ArithmeticOps::divide(
            &SqlValue::Integer(10),
            &SqlValue::Boolean(false),
            vibesql_types::SqlMode::default(),
        );
        assert_eq!(result.unwrap(), SqlValue::Null);
    }

    #[test]
    fn test_boolean_modulo() {
        // 10 % TRUE = 0 (10 % 1 = 0)
        let result =
            ArithmeticOps::modulo(&SqlValue::Integer(10), &SqlValue::Boolean(true)).unwrap();
        assert_eq!(result, SqlValue::Integer(0));

        // TRUE % TRUE = 0 (1 % 1 = 0)
        let result =
            ArithmeticOps::modulo(&SqlValue::Boolean(true), &SqlValue::Boolean(true)).unwrap();
        assert_eq!(result, SqlValue::Integer(0));
    }

    #[test]
    fn test_boolean_integer_divide() {
        // 10 DIV TRUE = 10
        let result =
            ArithmeticOps::integer_divide(&SqlValue::Integer(10), &SqlValue::Boolean(true))
                .unwrap();
        assert_eq!(result, SqlValue::Integer(10));

        // TRUE DIV TRUE = 1
        let result =
            ArithmeticOps::integer_divide(&SqlValue::Boolean(true), &SqlValue::Boolean(true))
                .unwrap();
        assert_eq!(result, SqlValue::Integer(1));
    }

    // Tests for Numeric type preservation
    #[test]
    fn test_numeric_multiply_integer() {
        // Numeric * Integer should preserve Numeric type
        let result =
            ArithmeticOps::multiply(&SqlValue::Numeric(1.0), &SqlValue::Integer(85)).unwrap();
        assert!(matches!(result, SqlValue::Numeric(_)));
        if let SqlValue::Numeric(n) = result {
            assert_eq!(n, 85.0);
        }
    }

    #[test]
    fn test_numeric_add_integer() {
        // Numeric + Integer should preserve Numeric type
        let result = ArithmeticOps::add(&SqlValue::Numeric(10.0), &SqlValue::Integer(5)).unwrap();
        assert!(matches!(result, SqlValue::Numeric(_)));
        if let SqlValue::Numeric(n) = result {
            assert_eq!(n, 15.0);
        }
    }

    #[test]
    fn test_numeric_subtract_integer() {
        // Numeric - Integer should preserve Numeric type
        let result =
            ArithmeticOps::subtract(&SqlValue::Numeric(10.0), &SqlValue::Integer(3)).unwrap();
        assert!(matches!(result, SqlValue::Numeric(_)));
        if let SqlValue::Numeric(n) = result {
            assert_eq!(n, 7.0);
        }
    }

    #[test]
    fn test_numeric_divide_integer() {
        // Numeric / Integer should preserve Numeric type
        let result = ArithmeticOps::divide(
            &SqlValue::Numeric(10.0),
            &SqlValue::Integer(2),
            vibesql_types::SqlMode::default(),
        )
        .unwrap();
        assert!(matches!(result, SqlValue::Numeric(_)));
        if let SqlValue::Numeric(n) = result {
            assert_eq!(n, 5.0);
        }
    }

    #[test]
    fn test_numeric_chain_operations() {
        // Test complex expression: 1.0 * 85 * -28 * 83
        let step1 =
            ArithmeticOps::multiply(&SqlValue::Numeric(1.0), &SqlValue::Integer(85)).unwrap();
        assert!(matches!(step1, SqlValue::Numeric(_)));

        let step2 = ArithmeticOps::multiply(&step1, &SqlValue::Integer(-28)).unwrap();
        assert!(matches!(step2, SqlValue::Numeric(_)));

        let step3 = ArithmeticOps::multiply(&step2, &SqlValue::Integer(83)).unwrap();
        assert!(matches!(step3, SqlValue::Numeric(_)));

        if let SqlValue::Numeric(n) = step3 {
            assert_eq!(n, -197540.0);
        }
    }

    #[test]
    fn test_integer_add_numeric() {
        // Integer + Numeric should preserve Numeric type (commutative)
        let result = ArithmeticOps::add(&SqlValue::Integer(5), &SqlValue::Numeric(10.0)).unwrap();
        assert!(matches!(result, SqlValue::Numeric(_)));
        if let SqlValue::Numeric(n) = result {
            assert_eq!(n, 15.0);
        }
    }

    #[test]
    fn test_numeric_add_numeric() {
        // Numeric + Numeric should preserve Numeric type
        let result = ArithmeticOps::add(&SqlValue::Numeric(10.0), &SqlValue::Numeric(5.0)).unwrap();
        assert!(matches!(result, SqlValue::Numeric(_)));
        if let SqlValue::Numeric(n) = result {
            assert_eq!(n, 15.0);
        }
    }

    #[test]
    fn test_numeric_modulo_integer() {
        // Numeric % Integer should preserve Numeric type
        let result =
            ArithmeticOps::modulo(&SqlValue::Numeric(10.0), &SqlValue::Integer(3)).unwrap();
        assert!(matches!(result, SqlValue::Numeric(_)));
        if let SqlValue::Numeric(n) = result {
            assert_eq!(n, 1.0);
        }
    }

    // SQL Mode Tests
    #[test]
    fn test_sql_mode_standard_integer_arithmetic() {
        // Standard mode: Integer + Integer → Integer
        let result = ArithmeticOps::add(&SqlValue::Integer(1), &SqlValue::Integer(2)).unwrap();
        assert_eq!(result, SqlValue::Integer(3));

        // Standard mode: Integer - Integer → Integer
        let result =
            ArithmeticOps::subtract(&SqlValue::Integer(91), &SqlValue::Integer(0)).unwrap();
        assert_eq!(result, SqlValue::Integer(91));

        // Standard mode: Integer * Integer → Integer
        let result = ArithmeticOps::multiply(&SqlValue::Integer(5), &SqlValue::Integer(7)).unwrap();
        assert_eq!(result, SqlValue::Integer(35));
    }

    #[test]
    fn test_sql_mode_mysql_integer_arithmetic() {
        // MySQL mode: Integer + Integer → Integer (matches actual MySQL behavior)
        let result = ArithmeticOps::add(&SqlValue::Integer(1), &SqlValue::Integer(2)).unwrap();
        assert_eq!(result, SqlValue::Integer(3));

        // MySQL mode: Integer - Integer → Integer (unary negation case)
        let result =
            ArithmeticOps::subtract(&SqlValue::Integer(0), &SqlValue::Integer(-91)).unwrap();
        assert_eq!(result, SqlValue::Integer(91));

        // MySQL mode: Integer * Integer → Integer
        let result = ArithmeticOps::multiply(&SqlValue::Integer(5), &SqlValue::Integer(7)).unwrap();
        assert_eq!(result, SqlValue::Integer(35));
    }

    #[test]
    fn test_sql_mode_comparison() {
        // Both modes now return Integer for integer arithmetic
        let int1 = SqlValue::Integer(100);
        let int2 = SqlValue::Integer(50);

        // Standard mode returns Integer
        let standard_result = ArithmeticOps::add(&int1, &int2).unwrap();
        assert_eq!(standard_result, SqlValue::Integer(150));

        // MySQL mode also returns Integer (matches actual MySQL behavior)
        let mysql_result = ArithmeticOps::add(&int1, &int2).unwrap();
        assert_eq!(mysql_result, SqlValue::Integer(150));
    }

    // NULL propagation tests for issue #1728
    #[test]
    fn test_null_addition() {
        assert_eq!(
            ArithmeticOps::add(&SqlValue::Null, &SqlValue::Integer(23)).unwrap(),
            SqlValue::Null
        );
        assert_eq!(
            ArithmeticOps::add(&SqlValue::Integer(23), &SqlValue::Null).unwrap(),
            SqlValue::Null
        );
        assert_eq!(ArithmeticOps::add(&SqlValue::Null, &SqlValue::Null).unwrap(), SqlValue::Null);
    }

    #[test]
    fn test_null_subtraction() {
        assert_eq!(
            ArithmeticOps::subtract(&SqlValue::Null, &SqlValue::Integer(23)).unwrap(),
            SqlValue::Null
        );
        assert_eq!(
            ArithmeticOps::subtract(&SqlValue::Integer(23), &SqlValue::Null).unwrap(),
            SqlValue::Null
        );
    }

    #[test]
    fn test_null_multiplication() {
        assert_eq!(
            ArithmeticOps::multiply(&SqlValue::Null, &SqlValue::Integer(23)).unwrap(),
            SqlValue::Null
        );
        assert_eq!(
            ArithmeticOps::multiply(&SqlValue::Integer(23), &SqlValue::Null).unwrap(),
            SqlValue::Null
        );
    }

    #[test]
    fn test_null_division() {
        assert_eq!(
            ArithmeticOps::divide(
                &SqlValue::Null,
                &SqlValue::Integer(23),
                vibesql_types::SqlMode::default()
            )
            .unwrap(),
            SqlValue::Null
        );
        assert_eq!(
            ArithmeticOps::divide(
                &SqlValue::Integer(23),
                &SqlValue::Null,
                vibesql_types::SqlMode::default()
            )
            .unwrap(),
            SqlValue::Null
        );
    }

    #[test]
    fn test_null_modulo() {
        assert_eq!(
            ArithmeticOps::modulo(&SqlValue::Null, &SqlValue::Integer(23)).unwrap(),
            SqlValue::Null
        );
        assert_eq!(
            ArithmeticOps::modulo(&SqlValue::Integer(23), &SqlValue::Null).unwrap(),
            SqlValue::Null
        );
    }

    #[test]
    fn test_cast_null_arithmetic() {
        // Simulate CAST(NULL AS SIGNED) + 23
        let null_bigint = SqlValue::Null; // Result of CAST(NULL AS SIGNED)
        let result = ArithmeticOps::add(&null_bigint, &SqlValue::Integer(23)).unwrap();
        assert_eq!(result, SqlValue::Null);

        // Test chained operations with NULL
        let step1 = ArithmeticOps::multiply(&SqlValue::Null, &SqlValue::Integer(100)).unwrap();
        assert_eq!(step1, SqlValue::Null);

        let step2 = ArithmeticOps::add(&step1, &SqlValue::Integer(-23)).unwrap();
        assert_eq!(step2, SqlValue::Null);
    }

    // String-to-number coercion tests (SQLite compatibility)
    #[test]
    fn test_string_integer_addition() {
        // '123' + 1 = 124
        let result = ArithmeticOps::add(
            &SqlValue::Varchar(arcstr::ArcStr::from("123")),
            &SqlValue::Integer(1),
        )
        .unwrap();
        assert_eq!(result, SqlValue::Integer(124));
    }

    #[test]
    fn test_integer_string_addition() {
        // 1 + '2' = 3
        let result = ArithmeticOps::add(
            &SqlValue::Integer(1),
            &SqlValue::Varchar(arcstr::ArcStr::from("2")),
        )
        .unwrap();
        assert_eq!(result, SqlValue::Integer(3));
    }

    #[test]
    fn test_string_float_addition() {
        // 1 + '2.5' = 3.5
        let result = ArithmeticOps::add(
            &SqlValue::Integer(1),
            &SqlValue::Varchar(arcstr::ArcStr::from("2.5")),
        )
        .unwrap();
        match result {
            SqlValue::Float(f) => assert!((f - 3.5).abs() < 0.01),
            _ => panic!("Expected Float result, got {:?}", result),
        }
    }

    #[test]
    fn test_non_numeric_string() {
        // 'abc' + 1 = 1 (non-numeric string converts to 0)
        let result = ArithmeticOps::add(
            &SqlValue::Varchar(arcstr::ArcStr::from("abc")),
            &SqlValue::Integer(1),
        )
        .unwrap();
        assert_eq!(result, SqlValue::Integer(1));
    }

    #[test]
    fn test_whitespace_string() {
        // '  42  ' + 0 = 42 (whitespace trimmed)
        let result = ArithmeticOps::add(
            &SqlValue::Varchar(arcstr::ArcStr::from("  42  ")),
            &SqlValue::Integer(0),
        )
        .unwrap();
        assert_eq!(result, SqlValue::Integer(42));
    }

    #[test]
    fn test_numeric_prefix_string() {
        // '3.14abc' + 0 = 3.14 (numeric prefix extracted)
        let result = ArithmeticOps::add(
            &SqlValue::Varchar(arcstr::ArcStr::from("3.15abc")),
            &SqlValue::Integer(0),
        )
        .unwrap();
        match result {
            SqlValue::Float(f) => assert!((f - 3.15).abs() < 0.01),
            _ => panic!("Expected Float result, got {:?}", result),
        }
    }

    #[test]
    fn test_string_multiplication() {
        // '3' * 4 = 12
        let result = ArithmeticOps::multiply(
            &SqlValue::Varchar(arcstr::ArcStr::from("3")),
            &SqlValue::Integer(4),
        )
        .unwrap();
        assert_eq!(result, SqlValue::Integer(12));
    }

    #[test]
    fn test_string_subtraction() {
        // '10' - 3 = 7
        let result = ArithmeticOps::subtract(
            &SqlValue::Varchar(arcstr::ArcStr::from("10")),
            &SqlValue::Integer(3),
        )
        .unwrap();
        assert_eq!(result, SqlValue::Integer(7));
    }

    #[test]
    fn test_string_division() {
        // '20' / 4 = 5 (SQLite mode)
        let result = ArithmeticOps::divide(
            &SqlValue::Varchar(arcstr::ArcStr::from("20")),
            &SqlValue::Integer(4),
            vibesql_types::SqlMode::SQLite,
        )
        .unwrap();
        assert_eq!(result, SqlValue::Integer(5));
    }

    #[test]
    fn test_string_modulo() {
        // '17' % 5 = 2
        let result = ArithmeticOps::modulo(
            &SqlValue::Varchar(arcstr::ArcStr::from("17")),
            &SqlValue::Integer(5),
        )
        .unwrap();
        assert_eq!(result, SqlValue::Integer(2));
    }

    #[test]
    fn test_empty_string() {
        // '' + 1 = 1 (empty string converts to 0)
        let result =
            ArithmeticOps::add(&SqlValue::Varchar(arcstr::ArcStr::from("")), &SqlValue::Integer(1))
                .unwrap();
        assert_eq!(result, SqlValue::Integer(1));
    }

    #[test]
    fn test_negative_string() {
        // '-5' + 10 = 5
        let result = ArithmeticOps::add(
            &SqlValue::Varchar(arcstr::ArcStr::from("-5")),
            &SqlValue::Integer(10),
        )
        .unwrap();
        assert_eq!(result, SqlValue::Integer(5));
    }

    #[test]
    fn test_string_with_exponent() {
        // '1e2' + 0 = 100.0
        let result = ArithmeticOps::add(
            &SqlValue::Varchar(arcstr::ArcStr::from("1e2")),
            &SqlValue::Integer(0),
        )
        .unwrap();
        match result {
            SqlValue::Float(f) => assert!((f - 100.0).abs() < 0.01),
            _ => panic!("Expected Float result, got {:?}", result),
        }
    }

    #[test]
    fn test_string_string_addition() {
        // '10' + '5' = 15 (both strings coerced)
        let result = ArithmeticOps::add(
            &SqlValue::Varchar(arcstr::ArcStr::from("10")),
            &SqlValue::Varchar(arcstr::ArcStr::from("5")),
        )
        .unwrap();
        assert_eq!(result, SqlValue::Integer(15));
    }

    #[test]
    fn test_float_string_addition() {
        // '1.5' + 0.5 = 2.0
        let result = ArithmeticOps::add(
            &SqlValue::Varchar(arcstr::ArcStr::from("1.5")),
            &SqlValue::Float(0.5),
        )
        .unwrap();
        match result {
            SqlValue::Float(f) => assert!((f - 2.0).abs() < 0.01),
            _ => panic!("Expected Float result, got {:?}", result),
        }
    }
}