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
//! Predicate evaluation methods (BETWEEN, LIKE, IN, POSITION, EXTRACT)

use chrono::{Datelike, Timelike};

use super::super::{
    casting::cast_value,
    core::ExpressionEvaluator,
    pattern::{glob_match, like_match},
};
use crate::errors::ExecutorError;

impl ExpressionEvaluator<'_> {
    /// Evaluate BETWEEN predicate: expr BETWEEN low AND high
    /// Standard BETWEEN: returns false if low > high (per SQLite behavior)
    /// BETWEEN SYMMETRIC: swaps low and high if low > high before evaluation
    /// If negated: expr < low OR expr > high
    ///
    /// NULL handling (SQLite/SQL:1999 three-valued logic):
    /// - NULL propagates through comparisons naturally (x < NULL → NULL, x > NULL → NULL)
    /// - For BETWEEN: (expr >= low) AND (expr <= high)
    ///   - If any comparison is NULL: NULL AND ... → NULL or FALSE (per three-valued AND)
    /// - For NOT BETWEEN: (expr < low) OR (expr > high)
    ///   - If any comparison is NULL: NULL OR ... → TRUE or NULL (per three-valued OR)
    ///   - Example: 93 NOT BETWEEN NULL AND 10 = (NULL) OR (TRUE) = TRUE
    #[inline]
    pub(super) fn eval_between(
        &self,
        expr: &vibesql_ast::Expression,
        low: &vibesql_ast::Expression,
        high: &vibesql_ast::Expression,
        negated: bool,
        symmetric: bool,
        row: &vibesql_storage::Row,
    ) -> Result<vibesql_types::SqlValue, ExecutorError> {
        use vibesql_types::SqlValue;

        // Handle row value constructor BETWEEN
        // (a, b) BETWEEN (low_a, low_b) AND (high_a, high_b)
        // is equivalent to: (a, b) >= (low_a, low_b) AND (a, b) <= (high_a, high_b)
        if let (
            vibesql_ast::Expression::RowValueConstructor(expr_exprs),
            vibesql_ast::Expression::RowValueConstructor(low_exprs),
            vibesql_ast::Expression::RowValueConstructor(high_exprs),
        ) = (expr, low, high)
        {
            return self.eval_row_value_between(
                expr_exprs, low_exprs, high_exprs, negated, symmetric, row,
            );
        }

        let sql_mode = self.database.map(|db| db.sql_mode()).unwrap_or_default();

        // Get effective collation: explicit COLLATE or column-level collation
        let collation = self.get_expression_collation(expr);

        let expr_val = self.eval(expr, row)?;
        let low_val = self.eval(low, row)?;
        let high_val = self.eval(high, row)?;

        // Apply collation transformation if needed
        // NOCASE: uppercase all strings for case-insensitive comparison
        // RTRIM: trim trailing whitespace from all strings
        let transform_for_collation = |val: SqlValue, collation_name: &str| -> SqlValue {
            if collation_name.eq_ignore_ascii_case("nocase") {
                match val {
                    SqlValue::Varchar(s) => {
                        SqlValue::Varchar(arcstr::ArcStr::from(s.to_uppercase()))
                    }
                    SqlValue::Character(s) => {
                        SqlValue::Character(arcstr::ArcStr::from(s.to_uppercase()))
                    }
                    other => other,
                }
            } else if collation_name.eq_ignore_ascii_case("rtrim") {
                match val {
                    SqlValue::Varchar(s) => SqlValue::Varchar(arcstr::ArcStr::from(s.trim_end())),
                    SqlValue::Character(s) => {
                        SqlValue::Character(arcstr::ArcStr::from(s.trim_end()))
                    }
                    other => other,
                }
            } else {
                val
            }
        };

        let (expr_val, low_val, high_val) = if let Some(ref collation_name) = collation {
            (
                transform_for_collation(expr_val, collation_name),
                transform_for_collation(low_val, collation_name),
                transform_for_collation(high_val, collation_name),
            )
        } else {
            (expr_val, low_val, high_val)
        };

        // Apply SQLite type affinity rules for BETWEEN comparisons
        // TEXT column vs INTEGER literal → convert INTEGER to TEXT, string compare
        //
        // IMPORTANT: Apply affinity transformations independently for each comparison.
        // The expr value may be transformed differently when compared to low vs high.
        // Example: '-1' BETWEEN 0 AND col where col is INTEGER:
        //   - For '-1' >= 0: string vs integer → use type ordering (TEXT > INTEGER → TRUE)
        //   - For '-1' <= col: string vs INTEGER column → coerce '-1' to -1 → -1 <= col
        let (expr_val_for_low, mut low_val) =
            self.apply_affinity_for_comparison(expr, expr_val.clone(), low, low_val);
        let (expr_val_for_high, mut high_val) =
            self.apply_affinity_for_comparison(expr, expr_val.clone(), high, high_val);

        // Check if bounds are reversed (low > high)
        let gt_result = Self::eval_binary_op_static(
            &low_val,
            &vibesql_ast::BinaryOperator::GreaterThan,
            &high_val,
            sql_mode.clone(),
        )?;

        if let vibesql_types::SqlValue::Boolean(true) = gt_result {
            if symmetric {
                // For SYMMETRIC: swap bounds to normalize range
                std::mem::swap(&mut low_val, &mut high_val);
            } else {
                // For standard BETWEEN with reversed bounds: return empty set
                // Per SQLite behavior: BETWEEN 57.93 AND 43.23 returns no rows
                // - BETWEEN with reversed bounds returns false
                // - NOT BETWEEN with reversed bounds returns true
                //
                // However, if expr is NULL, we must preserve NULL semantics:
                // - NULL BETWEEN reversed_bounds = NULL (not FALSE)
                // - NOT NULL BETWEEN reversed_bounds = NULL (not TRUE)
                if matches!(expr_val, vibesql_types::SqlValue::Null) {
                    return Ok(vibesql_types::SqlValue::Null);
                }
                return Ok(vibesql_types::SqlValue::Boolean(negated));
            }
        }

        let ge_low = Self::eval_binary_op_static(
            &expr_val_for_low,
            &vibesql_ast::BinaryOperator::GreaterThanOrEqual,
            &low_val,
            sql_mode.clone(),
        )?;

        let le_high = Self::eval_binary_op_static(
            &expr_val_for_high,
            &vibesql_ast::BinaryOperator::LessThanOrEqual,
            &high_val,
            sql_mode.clone(),
        )?;

        if negated {
            let lt_low = Self::eval_binary_op_static(
                &expr_val_for_low,
                &vibesql_ast::BinaryOperator::LessThan,
                &low_val,
                sql_mode.clone(),
            )?;
            let gt_high = Self::eval_binary_op_static(
                &expr_val_for_high,
                &vibesql_ast::BinaryOperator::GreaterThan,
                &high_val,
                sql_mode.clone(),
            )?;
            Self::eval_binary_op_static(
                &lt_low,
                &vibesql_ast::BinaryOperator::Or,
                &gt_high,
                sql_mode,
            )
        } else {
            Self::eval_binary_op_static(
                &ge_low,
                &vibesql_ast::BinaryOperator::And,
                &le_high,
                sql_mode,
            )
        }
    }

    /// Evaluate CAST expression
    #[inline]
    pub(super) fn eval_cast(
        &self,
        expr: &vibesql_ast::Expression,
        data_type: &vibesql_types::DataType,
        row: &vibesql_storage::Row,
    ) -> Result<vibesql_types::SqlValue, ExecutorError> {
        let value = self.eval(expr, row)?;
        let sql_mode = self.database.map(|db| db.sql_mode()).unwrap_or_default();
        cast_value(&value, data_type, &sql_mode)
    }

    /// Evaluate POSITION expression
    #[inline]
    pub(super) fn eval_position(
        &self,
        substring: &vibesql_ast::Expression,
        string: &vibesql_ast::Expression,
        row: &vibesql_storage::Row,
    ) -> Result<vibesql_types::SqlValue, ExecutorError> {
        let substring_val = self.eval(substring, row)?;
        let string_val = self.eval(string, row)?;

        match (&substring_val, &string_val) {
            (vibesql_types::SqlValue::Null, _) | (_, vibesql_types::SqlValue::Null) => {
                Ok(vibesql_types::SqlValue::Null)
            }
            (
                vibesql_types::SqlValue::Varchar(needle)
                | vibesql_types::SqlValue::Character(needle),
                vibesql_types::SqlValue::Varchar(haystack)
                | vibesql_types::SqlValue::Character(haystack),
            ) => match haystack.find(&**needle) {
                Some(pos) => Ok(vibesql_types::SqlValue::Integer((pos + 1) as i64)),
                None => Ok(vibesql_types::SqlValue::Integer(0)),
            },
            _ => Err(ExecutorError::TypeMismatch {
                left: substring_val.clone(),
                op: "POSITION".to_string(),
                right: string_val.clone(),
            }),
        }
    }

    /// Evaluate TRIM expression
    /// TRIM([position] [removal_char FROM] string)
    #[inline]
    pub(super) fn eval_trim(
        &self,
        position: &Option<vibesql_ast::TrimPosition>,
        removal_char: &Option<Box<vibesql_ast::Expression>>,
        string: &vibesql_ast::Expression,
        row: &vibesql_storage::Row,
    ) -> Result<vibesql_types::SqlValue, ExecutorError> {
        let string_val = self.eval(string, row)?;

        // Handle NULL string
        if matches!(string_val, vibesql_types::SqlValue::Null) {
            return Ok(vibesql_types::SqlValue::Null);
        }

        // Extract the string value
        let s = match &string_val {
            vibesql_types::SqlValue::Varchar(s) | vibesql_types::SqlValue::Character(s) => &**s,
            _ => {
                return Err(ExecutorError::TypeMismatch {
                    left: string_val.clone(),
                    op: "TRIM".to_string(),
                    right: vibesql_types::SqlValue::Null,
                })
            }
        };

        // Determine the character(s) to remove
        // Need to extract owned String to avoid lifetime issues
        let char_to_remove: String = if let Some(removal_expr) = removal_char {
            let removal_val = self.eval(removal_expr, row)?;

            // Handle NULL removal character
            if matches!(removal_val, vibesql_types::SqlValue::Null) {
                return Ok(vibesql_types::SqlValue::Null);
            }

            match removal_val {
                vibesql_types::SqlValue::Varchar(c) | vibesql_types::SqlValue::Character(c) => {
                    c.to_string()
                }
                _ => {
                    return Err(ExecutorError::TypeMismatch {
                        left: removal_val.clone(),
                        op: "TRIM".to_string(),
                        right: string_val.clone(),
                    })
                }
            }
        } else {
            " ".to_string() // Default to space
        };

        let char_to_remove_str = char_to_remove.as_str();

        // Apply trimming based on position (default is Both)
        let result = match position.as_ref().unwrap_or(&vibesql_ast::TrimPosition::Both) {
            vibesql_ast::TrimPosition::Both => {
                // Trim from both sides
                let mut result = s;
                while result.starts_with(char_to_remove_str) && !result.is_empty() {
                    result = &result[char_to_remove_str.len()..];
                }
                while result.ends_with(char_to_remove_str) && !result.is_empty() {
                    result = &result[..result.len() - char_to_remove_str.len()];
                }
                result.to_string()
            }
            vibesql_ast::TrimPosition::Leading => {
                // Trim from start only
                let mut result = s;
                while result.starts_with(char_to_remove_str) && !result.is_empty() {
                    result = &result[char_to_remove_str.len()..];
                }
                result.to_string()
            }
            vibesql_ast::TrimPosition::Trailing => {
                // Trim from end only
                let mut result = s;
                while result.ends_with(char_to_remove_str) && !result.is_empty() {
                    result = &result[..result.len() - char_to_remove_str.len()];
                }
                result.to_string()
            }
        };

        Ok(vibesql_types::SqlValue::Varchar(arcstr::ArcStr::from(result)))
    }

    /// Evaluate EXTRACT expression
    /// EXTRACT(field FROM expr) - extracts date/time component from datetime expression
    #[inline]
    pub(super) fn eval_extract(
        &self,
        field: &vibesql_ast::IntervalUnit,
        expr: &vibesql_ast::Expression,
        row: &vibesql_storage::Row,
    ) -> Result<vibesql_types::SqlValue, ExecutorError> {
        let datetime_val = self.eval(expr, row)?;

        // Handle NULL
        if matches!(datetime_val, vibesql_types::SqlValue::Null) {
            return Ok(vibesql_types::SqlValue::Null);
        }

        // Extract component based on field type
        use vibesql_ast::IntervalUnit;

        match &datetime_val {
            vibesql_types::SqlValue::Date(d) => {
                let result = match field {
                    IntervalUnit::Year => d.year as i64,
                    IntervalUnit::Month => d.month as i64,
                    IntervalUnit::Day => d.day as i64,
                    IntervalUnit::Quarter => ((d.month - 1) / 3 + 1) as i64,
                    IntervalUnit::Week => {
                        // Calculate ISO week number using chrono
                        let chrono_date =
                            chrono::NaiveDate::from_ymd_opt(d.year, d.month as u32, d.day as u32)
                                .unwrap_or_else(|| {
                                    chrono::NaiveDate::from_ymd_opt(1970, 1, 1).unwrap()
                                });
                        chrono_date.iso_week().week() as i64
                    }
                    _ => {
                        return Err(ExecutorError::InvalidExtractField {
                            field: format!("{:?}", field),
                            value_type: "DATE".to_string(),
                        })
                    }
                };
                Ok(vibesql_types::SqlValue::Integer(result))
            }

            vibesql_types::SqlValue::Time(t) => {
                let result = match field {
                    IntervalUnit::Hour => t.hour as i64,
                    IntervalUnit::Minute => t.minute as i64,
                    IntervalUnit::Second => t.second as i64,
                    IntervalUnit::Microsecond => (t.nanosecond / 1000) as i64,
                    _ => {
                        return Err(ExecutorError::InvalidExtractField {
                            field: format!("{:?}", field),
                            value_type: "TIME".to_string(),
                        })
                    }
                };
                Ok(vibesql_types::SqlValue::Integer(result))
            }

            vibesql_types::SqlValue::Timestamp(ts) => {
                let result = match field {
                    IntervalUnit::Year => ts.date.year as i64,
                    IntervalUnit::Month => ts.date.month as i64,
                    IntervalUnit::Day => ts.date.day as i64,
                    IntervalUnit::Hour => ts.time.hour as i64,
                    IntervalUnit::Minute => ts.time.minute as i64,
                    IntervalUnit::Second => ts.time.second as i64,
                    IntervalUnit::Microsecond => (ts.time.nanosecond / 1000) as i64,
                    IntervalUnit::Quarter => ((ts.date.month - 1) / 3 + 1) as i64,
                    IntervalUnit::Week => {
                        // Calculate ISO week number using chrono
                        let chrono_date = chrono::NaiveDate::from_ymd_opt(
                            ts.date.year,
                            ts.date.month as u32,
                            ts.date.day as u32,
                        )
                        .unwrap_or_else(|| chrono::NaiveDate::from_ymd_opt(1970, 1, 1).unwrap());
                        chrono_date.iso_week().week() as i64
                    }
                    _ => {
                        return Err(ExecutorError::InvalidExtractField {
                            field: format!("{:?}", field),
                            value_type: "TIMESTAMP".to_string(),
                        })
                    }
                };
                Ok(vibesql_types::SqlValue::Integer(result))
            }

            // For string values, try to parse as date/timestamp
            vibesql_types::SqlValue::Varchar(s) | vibesql_types::SqlValue::Character(s) => {
                // Try to parse as date first
                if let Ok(date) = chrono::NaiveDate::parse_from_str(s, "%Y-%m-%d") {
                    let result = match field {
                        IntervalUnit::Year => date.year() as i64,
                        IntervalUnit::Month => date.month() as i64,
                        IntervalUnit::Day => date.day() as i64,
                        IntervalUnit::Quarter => ((date.month() - 1) / 3 + 1) as i64,
                        IntervalUnit::Week => date.iso_week().week() as i64,
                        _ => {
                            return Err(ExecutorError::InvalidExtractField {
                                field: format!("{:?}", field),
                                value_type: "DATE".to_string(),
                            })
                        }
                    };
                    return Ok(vibesql_types::SqlValue::Integer(result));
                }

                // Try to parse as timestamp
                if let Ok(ts) = chrono::NaiveDateTime::parse_from_str(s, "%Y-%m-%d %H:%M:%S") {
                    let result = match field {
                        IntervalUnit::Year => ts.year() as i64,
                        IntervalUnit::Month => ts.month() as i64,
                        IntervalUnit::Day => ts.day() as i64,
                        IntervalUnit::Hour => ts.hour() as i64,
                        IntervalUnit::Minute => ts.minute() as i64,
                        IntervalUnit::Second => ts.second() as i64,
                        IntervalUnit::Microsecond => ts.nanosecond() as i64 / 1000,
                        IntervalUnit::Quarter => ((ts.month() - 1) / 3 + 1) as i64,
                        IntervalUnit::Week => ts.iso_week().week() as i64,
                        _ => {
                            return Err(ExecutorError::InvalidExtractField {
                                field: format!("{:?}", field),
                                value_type: "TIMESTAMP".to_string(),
                            })
                        }
                    };
                    return Ok(vibesql_types::SqlValue::Integer(result));
                }

                Err(ExecutorError::TypeMismatch {
                    left: datetime_val.clone(),
                    op: "EXTRACT".to_string(),
                    right: vibesql_types::SqlValue::Null,
                })
            }

            _ => Err(ExecutorError::TypeMismatch {
                left: datetime_val.clone(),
                op: "EXTRACT".to_string(),
                right: vibesql_types::SqlValue::Null,
            }),
        }
    }

    /// Evaluate LIKE predicate
    #[inline]
    pub(super) fn eval_like(
        &self,
        expr: &vibesql_ast::Expression,
        pattern: &vibesql_ast::Expression,
        escape: &Option<Box<vibesql_ast::Expression>>,
        negated: bool,
        row: &vibesql_storage::Row,
    ) -> Result<vibesql_types::SqlValue, ExecutorError> {
        let expr_val = self.eval(expr, row)?;
        let pattern_val = self.eval(pattern, row)?;

        // Issue #4913: SQLite coerces numeric types to strings for LIKE comparison
        let text = match &expr_val {
            vibesql_types::SqlValue::Varchar(s) | vibesql_types::SqlValue::Character(s) => {
                s.clone()
            }
            vibesql_types::SqlValue::Null => return Ok(vibesql_types::SqlValue::Null),
            // SQLite coerces numeric types to strings for LIKE comparison
            vibesql_types::SqlValue::Integer(i) => arcstr::ArcStr::from(i.to_string()),
            vibesql_types::SqlValue::Bigint(i) => arcstr::ArcStr::from(i.to_string()),
            vibesql_types::SqlValue::Float(f) => arcstr::ArcStr::from(f.to_string()),
            vibesql_types::SqlValue::Double(f) => arcstr::ArcStr::from(f.to_string()),
            vibesql_types::SqlValue::Real(f) => arcstr::ArcStr::from(f.to_string()),
            // SQLite treats blob bytes as raw text for LIKE comparison
            vibesql_types::SqlValue::Blob(b) => {
                arcstr::ArcStr::from(String::from_utf8_lossy(b).into_owned())
            }
            _ => {
                return Err(ExecutorError::TypeMismatch {
                    left: expr_val,
                    op: "LIKE".to_string(),
                    right: pattern_val,
                })
            }
        };

        let pattern_str = match &pattern_val {
            vibesql_types::SqlValue::Varchar(s) | vibesql_types::SqlValue::Character(s) => {
                s.clone()
            }
            vibesql_types::SqlValue::Null => return Ok(vibesql_types::SqlValue::Null),
            // SQLite coerces numeric types to strings for LIKE comparison
            vibesql_types::SqlValue::Integer(i) => arcstr::ArcStr::from(i.to_string()),
            vibesql_types::SqlValue::Bigint(i) => arcstr::ArcStr::from(i.to_string()),
            vibesql_types::SqlValue::Float(f) => arcstr::ArcStr::from(f.to_string()),
            vibesql_types::SqlValue::Double(f) => arcstr::ArcStr::from(f.to_string()),
            vibesql_types::SqlValue::Real(f) => arcstr::ArcStr::from(f.to_string()),
            _ => {
                return Err(ExecutorError::TypeMismatch {
                    left: expr_val,
                    op: "LIKE".to_string(),
                    right: pattern_val,
                })
            }
        };

        // Evaluate the escape character if provided
        let escape_char = if let Some(escape_expr) = escape {
            let escape_val = self.eval(escape_expr, row)?;
            match escape_val {
                vibesql_types::SqlValue::Varchar(ref s)
                | vibesql_types::SqlValue::Character(ref s) => {
                    let mut chars = s.chars();
                    match (chars.next(), chars.next()) {
                        (Some(c), None) => Some(c), // Exactly one character
                        _ => {
                            // Empty string or multi-character string: error per SQLite
                            return Err(ExecutorError::SqliteCompatError(
                                "ESCAPE expression must be a single character".to_string(),
                            ));
                        }
                    }
                }
                vibesql_types::SqlValue::Null => return Ok(vibesql_types::SqlValue::Null),
                vibesql_types::SqlValue::Integer(n) => {
                    // Allow single-digit integers as escape character (SQLite compatibility)
                    let s = n.to_string();
                    let mut chars = s.chars();
                    match (chars.next(), chars.next()) {
                        (Some(c), None) => Some(c), // Exactly one character
                        _ => {
                            return Err(ExecutorError::SqliteCompatError(
                                "ESCAPE expression must be a single character".to_string(),
                            ))
                        }
                    }
                }
                _ => {
                    return Err(ExecutorError::TypeMismatch {
                        left: escape_val,
                        op: "ESCAPE".to_string(),
                        right: vibesql_types::SqlValue::Null,
                    })
                }
            }
        } else {
            None
        };

        // Get case_sensitive_like setting from database (default: false = case-insensitive)
        let case_sensitive = self.database.map(|db| db.case_sensitive_like()).unwrap_or(false);
        let matches = like_match(&text, &pattern_str, case_sensitive, escape_char);
        let result = if negated { !matches } else { matches };

        Ok(vibesql_types::SqlValue::Boolean(result))
    }

    /// Evaluate GLOB predicate (SQLite)
    #[inline]
    pub(super) fn eval_glob(
        &self,
        expr: &vibesql_ast::Expression,
        pattern: &vibesql_ast::Expression,
        negated: bool,
        row: &vibesql_storage::Row,
    ) -> Result<vibesql_types::SqlValue, ExecutorError> {
        let expr_val = self.eval(expr, row)?;
        let pattern_val = self.eval(pattern, row)?;

        // Issue #4913: SQLite coerces numeric types to strings for GLOB comparison
        let text = match &expr_val {
            vibesql_types::SqlValue::Varchar(s) | vibesql_types::SqlValue::Character(s) => {
                s.clone()
            }
            vibesql_types::SqlValue::Null => return Ok(vibesql_types::SqlValue::Null),
            // SQLite coerces numeric types to strings for GLOB comparison
            vibesql_types::SqlValue::Integer(i) => arcstr::ArcStr::from(i.to_string()),
            vibesql_types::SqlValue::Bigint(i) => arcstr::ArcStr::from(i.to_string()),
            vibesql_types::SqlValue::Float(f) => arcstr::ArcStr::from(f.to_string()),
            vibesql_types::SqlValue::Double(f) => arcstr::ArcStr::from(f.to_string()),
            vibesql_types::SqlValue::Real(f) => arcstr::ArcStr::from(f.to_string()),
            // SQLite treats blob bytes as raw text for GLOB comparison
            vibesql_types::SqlValue::Blob(b) => {
                arcstr::ArcStr::from(String::from_utf8_lossy(b).into_owned())
            }
            _ => {
                return Err(ExecutorError::TypeMismatch {
                    left: expr_val,
                    op: "GLOB".to_string(),
                    right: pattern_val,
                })
            }
        };

        let pattern_str = match &pattern_val {
            vibesql_types::SqlValue::Varchar(s) | vibesql_types::SqlValue::Character(s) => {
                s.clone()
            }
            vibesql_types::SqlValue::Null => return Ok(vibesql_types::SqlValue::Null),
            // SQLite coerces numeric types to strings for GLOB comparison
            vibesql_types::SqlValue::Integer(i) => arcstr::ArcStr::from(i.to_string()),
            vibesql_types::SqlValue::Bigint(i) => arcstr::ArcStr::from(i.to_string()),
            vibesql_types::SqlValue::Float(f) => arcstr::ArcStr::from(f.to_string()),
            vibesql_types::SqlValue::Double(f) => arcstr::ArcStr::from(f.to_string()),
            vibesql_types::SqlValue::Real(f) => arcstr::ArcStr::from(f.to_string()),
            _ => {
                return Err(ExecutorError::TypeMismatch {
                    left: expr_val,
                    op: "GLOB".to_string(),
                    right: pattern_val,
                })
            }
        };

        let matches = glob_match(&text, &pattern_str);
        let result = if negated { !matches } else { matches };

        Ok(vibesql_types::SqlValue::Boolean(result))
    }

    /// Evaluate IN list predicate with HashSet optimization
    /// For lists > 3 items, uses HashSet for O(1) lookup instead of O(n) linear scan
    #[inline]
    pub(super) fn eval_in_list(
        &self,
        expr: &vibesql_ast::Expression,
        values: &[vibesql_ast::Expression],
        negated: bool,
        row: &vibesql_storage::Row,
    ) -> Result<vibesql_types::SqlValue, ExecutorError> {
        // Handle empty IN list: returns false for IN, true for NOT IN
        // This is per SQLite behavior (SQL:1999 extension, not standard SQL)
        if values.is_empty() {
            return Ok(vibesql_types::SqlValue::Boolean(negated));
        }

        let expr_val = self.eval(expr, row)?;

        // SQL standard behavior for NULL IN (list):
        // - NULL IN (empty list) → FALSE (already handled above)
        // - NULL IN (non-empty list) → NULL (three-valued logic)
        // The IN operator returns NULL when comparing NULL to any value
        if matches!(expr_val, vibesql_types::SqlValue::Null) {
            return Ok(vibesql_types::SqlValue::Null);
        }

        // For small lists (≤3 items), use linear search to avoid HashSet overhead
        // For larger lists, use HashSet for O(1) lookup performance
        if values.len() <= 3 {
            // Original linear search implementation for small lists
            let mut found_null = false;

            for value_expr in values {
                let value = self.eval(value_expr, row)?;

                if matches!(value, vibesql_types::SqlValue::Null) {
                    found_null = true;
                    continue;
                }

                // Apply SQLite type affinity rules for IN comparisons
                // Note: IN expressions have different affinity rules than regular comparisons
                let (expr_coerced, value_coerced) = self.apply_affinity_for_in_comparison(
                    expr,
                    expr_val.clone(),
                    value_expr,
                    value,
                );

                let eq_result = self.eval_binary_op(
                    &expr_coerced,
                    &vibesql_ast::BinaryOperator::Equal,
                    &value_coerced,
                )?;

                if matches!(eq_result, vibesql_types::SqlValue::Boolean(true)) {
                    return Ok(vibesql_types::SqlValue::Boolean(!negated));
                }
            }

            if found_null {
                Ok(vibesql_types::SqlValue::Null)
            } else {
                Ok(vibesql_types::SqlValue::Boolean(negated))
            }
        } else {
            // HashSet optimization for larger lists
            // Evaluate each IN list value once (instead of per row) and collect into HashSet
            // Apply affinity coercion before adding to the HashSet
            let mut value_set = std::collections::HashSet::new();
            let mut found_null = false;

            // Evaluate all values once, apply affinity, and build the HashSet
            for value_expr in values {
                let value = self.eval(value_expr, row)?;

                if matches!(value, vibesql_types::SqlValue::Null) {
                    found_null = true;
                } else {
                    // Apply SQLite type affinity rules for IN comparisons
                    let (_, value_coerced) = self.apply_affinity_for_in_comparison(
                        expr,
                        expr_val.clone(),
                        value_expr,
                        value,
                    );
                    value_set.insert(value_coerced);
                }
            }

            // O(n) lookup with SQL type coercion (where n = unique values in list)
            // This preserves correctness while still benefiting from single evaluation of IN list
            for value in &value_set {
                let eq_result =
                    self.eval_binary_op(&expr_val, &vibesql_ast::BinaryOperator::Equal, value)?;

                if matches!(eq_result, vibesql_types::SqlValue::Boolean(true)) {
                    return Ok(vibesql_types::SqlValue::Boolean(!negated));
                }
            }

            // No match found: return NULL if list contained NULL, else FALSE
            if found_null {
                Ok(vibesql_types::SqlValue::Null)
            } else {
                Ok(vibesql_types::SqlValue::Boolean(negated))
            }
        }
    }

    /// Evaluate row value BETWEEN predicate
    ///
    /// `(a, b) BETWEEN (low_a, low_b) AND (high_a, high_b)`
    /// is equivalent to: `(a, b) >= (low_a, low_b) AND (a, b) <= (high_a, high_b)`
    ///
    /// NULL handling follows the standard row value comparison semantics.
    fn eval_row_value_between(
        &self,
        expr_exprs: &[vibesql_ast::Expression],
        low_exprs: &[vibesql_ast::Expression],
        high_exprs: &[vibesql_ast::Expression],
        negated: bool,
        symmetric: bool,
        row: &vibesql_storage::Row,
    ) -> Result<vibesql_types::SqlValue, ExecutorError> {
        use vibesql_types::SqlValue;

        // Row values must have the same number of elements
        if expr_exprs.len() != low_exprs.len() || expr_exprs.len() != high_exprs.len() {
            return Err(ExecutorError::UnsupportedExpression(format!(
                "Row value constructor size mismatch in BETWEEN: expr has {} elements, low has {}, high has {}",
                expr_exprs.len(),
                low_exprs.len(),
                high_exprs.len()
            )));
        }

        if expr_exprs.is_empty() {
            return Err(ExecutorError::UnsupportedExpression(
                "Empty row value constructors are not allowed".to_string(),
            ));
        }

        // Determine effective bounds (swap if symmetric and low > high)
        let (effective_low, effective_high) = if symmetric {
            let gt_op = vibesql_ast::BinaryOperator::GreaterThan;
            let low_gt_high = self.eval_row_value_comparison(low_exprs, &gt_op, high_exprs, row)?;

            if matches!(low_gt_high, SqlValue::Boolean(true)) {
                (high_exprs, low_exprs) // Swap
            } else {
                (low_exprs, high_exprs)
            }
        } else {
            (low_exprs, high_exprs)
        };

        if negated {
            // NOT BETWEEN: expr < low OR expr > high
            let lt_op = vibesql_ast::BinaryOperator::LessThan;
            let gt_op = vibesql_ast::BinaryOperator::GreaterThan;

            let lt_low = self.eval_row_value_comparison(expr_exprs, &lt_op, effective_low, row)?;
            let gt_high =
                self.eval_row_value_comparison(expr_exprs, &gt_op, effective_high, row)?;

            // OR logic with three-valued semantics
            match (&lt_low, &gt_high) {
                (SqlValue::Boolean(true), _) | (_, SqlValue::Boolean(true)) => {
                    Ok(SqlValue::Boolean(true))
                }
                (SqlValue::Boolean(false), SqlValue::Boolean(false)) => {
                    Ok(SqlValue::Boolean(false))
                }
                _ => Ok(SqlValue::Null),
            }
        } else {
            // BETWEEN: expr >= low AND expr <= high
            let ge_op = vibesql_ast::BinaryOperator::GreaterThanOrEqual;
            let le_op = vibesql_ast::BinaryOperator::LessThanOrEqual;

            let ge_low = self.eval_row_value_comparison(expr_exprs, &ge_op, effective_low, row)?;
            let le_high =
                self.eval_row_value_comparison(expr_exprs, &le_op, effective_high, row)?;

            // AND logic with three-valued semantics
            match (&ge_low, &le_high) {
                (SqlValue::Boolean(true), SqlValue::Boolean(true)) => Ok(SqlValue::Boolean(true)),
                (SqlValue::Boolean(false), _) | (_, SqlValue::Boolean(false)) => {
                    Ok(SqlValue::Boolean(false))
                }
                _ => Ok(SqlValue::Null),
            }
        }
    }
}