stoolap 0.4.0

High-performance embedded SQL database with MVCC, time-travel queries, and full ACID compliance
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
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
// Copyright 2025 Stoolap Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Compiled Expression Operations
//
// These operations form the instruction set for the expression VM.
// Each operation is designed to be:
// - Self-contained (no external dependencies during execution)
// - Fast to dispatch (small enum, good for branch prediction)
// - Zero allocation (all data pre-computed at compile time)

use std::sync::Arc;

use crate::common::CompactArc;
use memchr::memmem;

use crate::core::{DataType, Value, ValueSet};
use crate::functions::{NativeFn1, ScalarFunction};

/// Compiled LIKE/ILIKE pattern for fast matching
#[derive(Debug, Clone)]
pub enum CompiledPattern {
    /// Exact match (no wildcards)
    Exact(String),
    /// Prefix match: "abc%"
    Prefix(String),
    /// Suffix match: "%abc"
    Suffix(String),
    /// Contains match: "%abc%"
    Contains(String),
    /// Prefix + Suffix: "abc%xyz"
    PrefixSuffix(String, String),
    /// Complex pattern requiring regex
    Regex(regex::Regex),
    /// Match all: "%"
    MatchAll,
    /// Single char: "_"
    SingleChar,
}

impl CompiledPattern {
    /// Compile a LIKE pattern into optimized form
    pub fn compile(pattern: &str, case_insensitive: bool) -> Self {
        let pat = if case_insensitive {
            pattern.to_lowercase()
        } else {
            pattern.to_string()
        };

        // Check for simple patterns that don't need regex
        let has_percent = pat.contains('%');
        let has_underscore = pat.contains('_');
        let has_escape = pat.contains('\\');

        if !has_percent && !has_underscore && !has_escape {
            return CompiledPattern::Exact(pat);
        }

        // When backslash escapes are present (e.g. \% for literal %), skip all fast
        // paths — they don't understand escape sequences. Fall through to regex which
        // correctly handles them via like_to_regex().
        if !has_escape {
            if pat == "%" {
                return CompiledPattern::MatchAll;
            }

            if pat == "_" {
                return CompiledPattern::SingleChar;
            }

            // Check for prefix pattern: "abc%"
            if pat.ends_with('%') && !pat[..pat.len() - 1].contains('%') && !has_underscore {
                return CompiledPattern::Prefix(pat[..pat.len() - 1].to_string());
            }

            // Check for suffix pattern: "%abc"
            if pat.starts_with('%') && !pat[1..].contains('%') && !has_underscore {
                return CompiledPattern::Suffix(pat[1..].to_string());
            }

            // Check for contains pattern: "%abc%"
            if pat.starts_with('%') && pat.ends_with('%') && pat.len() > 2 {
                let middle = &pat[1..pat.len() - 1];
                if !middle.contains('%') && !middle.contains('_') {
                    return CompiledPattern::Contains(middle.to_string());
                }
            }

            // Check for prefix+suffix: "abc%xyz"
            if has_percent && !has_underscore {
                let parts: Vec<&str> = pat.split('%').collect();
                if parts.len() == 2 && !parts[0].is_empty() && !parts[1].is_empty() {
                    return CompiledPattern::PrefixSuffix(
                        parts[0].to_string(),
                        parts[1].to_string(),
                    );
                }
            }
        }

        // Fall back to regex for complex patterns
        let regex_pattern = Self::like_to_regex(&pat);
        let regex = if case_insensitive {
            regex::Regex::new(&format!("(?i)^{}$", regex_pattern))
        } else {
            regex::Regex::new(&format!("^{}$", regex_pattern))
        };

        match regex {
            Ok(re) => CompiledPattern::Regex(re),
            Err(_) => CompiledPattern::Exact(pat), // Fallback
        }
    }

    /// Compile a GLOB pattern into optimized form
    /// GLOB uses * for any sequence and ? for single character (case-sensitive)
    pub fn compile_glob(pattern: &str) -> Self {
        let pat = pattern.to_string();

        // Check for simple patterns that don't need regex
        let has_star = pat.contains('*');
        let has_question = pat.contains('?');

        if !has_star && !has_question {
            return CompiledPattern::Exact(pat);
        }

        if pat == "*" {
            return CompiledPattern::MatchAll;
        }

        if pat == "?" {
            return CompiledPattern::SingleChar;
        }

        // Check for prefix pattern: "abc*"
        if pat.ends_with('*') && !pat[..pat.len() - 1].contains('*') && !has_question {
            return CompiledPattern::Prefix(pat[..pat.len() - 1].to_string());
        }

        // Check for suffix pattern: "*abc"
        if pat.starts_with('*') && !pat[1..].contains('*') && !has_question {
            return CompiledPattern::Suffix(pat[1..].to_string());
        }

        // Check for contains pattern: "*abc*"
        if pat.starts_with('*') && pat.ends_with('*') && pat.len() > 2 {
            let middle = &pat[1..pat.len() - 1];
            if !middle.contains('*') && !middle.contains('?') {
                return CompiledPattern::Contains(middle.to_string());
            }
        }

        // Check for prefix+suffix: "abc*xyz"
        if has_star && !has_question {
            let parts: Vec<&str> = pat.split('*').collect();
            if parts.len() == 2 && !parts[0].is_empty() && !parts[1].is_empty() {
                return CompiledPattern::PrefixSuffix(parts[0].to_string(), parts[1].to_string());
            }
        }

        // Fall back to regex for complex patterns
        let regex_pattern = Self::glob_to_regex(&pat);
        let regex = regex::Regex::new(&format!("^{}$", regex_pattern));

        match regex {
            Ok(re) => CompiledPattern::Regex(re),
            Err(_) => CompiledPattern::Exact(pat), // Fallback
        }
    }

    /// Convert GLOB pattern to regex (* -> .*, ? -> .)
    fn glob_to_regex(pattern: &str) -> String {
        let mut result = String::with_capacity(pattern.len() * 2);
        let mut chars = pattern.chars().peekable();

        while let Some(c) = chars.next() {
            match c {
                '*' => result.push_str(".*"),
                '?' => result.push('.'),
                '\\' => {
                    // Escape sequence
                    if let Some(&next) = chars.peek() {
                        if next == '*' || next == '?' || next == '\\' {
                            result.push_str(&regex::escape(&next.to_string()));
                            chars.next();
                        } else {
                            result.push_str(&regex::escape("\\"));
                        }
                    }
                }
                '[' => {
                    // Character class in GLOB - pass through to regex
                    result.push('[');
                    for c in chars.by_ref() {
                        if c == ']' {
                            result.push(']');
                            break;
                        }
                        result.push(c);
                    }
                }
                _ => result.push_str(&regex::escape(&c.to_string())),
            }
        }

        result
    }

    /// Convert LIKE pattern to regex
    fn like_to_regex(pattern: &str) -> String {
        let mut result = String::with_capacity(pattern.len() * 2);
        let mut chars = pattern.chars().peekable();

        while let Some(c) = chars.next() {
            match c {
                '%' => result.push_str(".*"),
                '_' => result.push('.'),
                '\\' => {
                    // Escape sequence: \% → literal %, \_ → literal _, \\ → literal \
                    if let Some(&next) = chars.peek() {
                        if next == '%' || next == '_' || next == '\\' {
                            chars.next();
                            result.push_str(&regex::escape(&next.to_string()));
                        } else {
                            result.push_str(&regex::escape("\\"));
                        }
                    } else {
                        // Trailing backslash: emit literal backslash
                        result.push_str(&regex::escape("\\"));
                    }
                }
                _ => result.push_str(&regex::escape(&c.to_string())),
            }
        }

        result
    }

    /// Match a string against this pattern
    #[inline]
    pub fn matches(&self, text: &str, case_insensitive: bool) -> bool {
        // Fast path: case-sensitive matching (no allocation)
        if !case_insensitive {
            return match self {
                CompiledPattern::Exact(p) => text == p,
                CompiledPattern::Prefix(p) => text.starts_with(p),
                CompiledPattern::Suffix(p) => text.ends_with(p),
                // Use memmem::find for SIMD-accelerated substring search
                CompiledPattern::Contains(p) => {
                    memmem::find(text.as_bytes(), p.as_bytes()).is_some()
                }
                CompiledPattern::PrefixSuffix(prefix, suffix) => {
                    text.starts_with(prefix)
                        && text.ends_with(suffix)
                        && text.len() >= prefix.len() + suffix.len()
                }
                CompiledPattern::Regex(re) => re.is_match(text),
                CompiledPattern::MatchAll => true,
                CompiledPattern::SingleChar => text.chars().count() == 1,
            };
        }

        // Case-insensitive: use ASCII fast path when possible, fall back to Unicode
        // NOTE: Pattern is already lowercased at compile time (see compile() method),
        // so we only need to lowercase the input text, not the pattern again.
        match self {
            CompiledPattern::Exact(p) => {
                if text.is_ascii() && p.is_ascii() {
                    text.eq_ignore_ascii_case(p)
                } else {
                    // Pattern already lowercased at compile time
                    text.to_lowercase() == *p
                }
            }
            CompiledPattern::Prefix(p) => {
                if text.len() < p.len() {
                    return false;
                }
                if text.is_ascii() && p.is_ascii() {
                    text[..p.len()].eq_ignore_ascii_case(p)
                } else {
                    // Pattern already lowercased at compile time
                    text.to_lowercase().starts_with(p)
                }
            }
            CompiledPattern::Suffix(p) => {
                if text.len() < p.len() {
                    return false;
                }
                if text.is_ascii() && p.is_ascii() {
                    text[text.len() - p.len()..].eq_ignore_ascii_case(p)
                } else {
                    // Pattern already lowercased at compile time
                    text.to_lowercase().ends_with(p)
                }
            }
            CompiledPattern::Contains(p) => {
                if p.len() > text.len() {
                    return false;
                }
                if text.is_ascii() && p.is_ascii() {
                    // Fast path: ASCII-only, use byte-level comparison without allocation
                    text.as_bytes()
                        .windows(p.len())
                        .any(|window| window.eq_ignore_ascii_case(p.as_bytes()))
                } else {
                    // Unicode path: lowercase text only (pattern already lowercased)
                    text.to_lowercase().contains(p)
                }
            }
            CompiledPattern::PrefixSuffix(prefix, suffix) => {
                if text.len() < prefix.len() + suffix.len() {
                    return false;
                }
                if text.is_ascii() && prefix.is_ascii() && suffix.is_ascii() {
                    text[..prefix.len()].eq_ignore_ascii_case(prefix)
                        && text[text.len() - suffix.len()..].eq_ignore_ascii_case(suffix)
                } else {
                    // Patterns already lowercased at compile time
                    let text_lower = text.to_lowercase();
                    text_lower.starts_with(prefix) && text_lower.ends_with(suffix)
                }
            }
            // Regex already has case-insensitivity compiled in
            CompiledPattern::Regex(re) => re.is_match(text),
            CompiledPattern::MatchAll => true,
            CompiledPattern::SingleChar => text.chars().count() == 1,
        }
    }
}

/// Expression VM Operation
///
/// Each operation is self-contained with all data needed for execution.
/// No external lookups during execution - everything resolved at compile time.
#[derive(Clone)]
pub enum Op {
    // =========================================================================
    // LOAD OPERATIONS - Push values onto stack
    // =========================================================================
    /// Load column value by pre-resolved index
    /// Stack: [] -> [value]
    LoadColumn(u16),

    /// Load column from second row (for joins)
    /// Stack: [] -> [value]
    LoadColumn2(u16),

    /// Load from outer row context (for correlated subqueries)
    /// Uses pre-resolved key
    /// Stack: [] -> [value]
    LoadOuterColumn(CompactArc<str>),

    /// Load constant value (pre-cloned at compile time)
    /// Stack: [] -> [value]
    LoadConst(Value),

    /// Load query parameter by index
    /// Stack: [] -> [value]
    LoadParam(u16),

    /// Load named parameter
    /// Stack: [] -> [value]
    LoadNamedParam(CompactArc<str>),

    /// Load NULL with type hint
    /// Stack: [] -> [null]
    LoadNull(DataType),

    // =========================================================================
    // COMPARISON OPERATIONS - Pop 2, push bool
    // =========================================================================
    /// Equal: a == b
    Eq,
    /// Not equal: a != b
    Ne,
    /// Less than: a < b
    Lt,
    /// Less than or equal: a <= b
    Le,
    /// Greater than: a > b
    Gt,
    /// Greater than or equal: a >= b
    Ge,

    /// IS NULL check
    /// Stack: [value] -> [bool]
    IsNull,

    /// IS NOT NULL check
    /// Stack: [value] -> [bool]
    IsNotNull,

    /// IS DISTINCT FROM (NULL-safe not equal)
    /// Stack: [a, b] -> [bool]
    IsDistinctFrom,

    /// IS NOT DISTINCT FROM (NULL-safe equal)
    /// Stack: [a, b] -> [bool]
    IsNotDistinctFrom,

    // =========================================================================
    // FUSED COMPARISON OPERATIONS - Single instruction for column vs constant
    // These avoid push/pop overhead for the most common filter patterns
    // =========================================================================
    /// Fused: column == constant
    /// Stack: [] -> [bool]
    EqColumnConst(u16, Value),

    /// Fused: column != constant
    /// Stack: [] -> [bool]
    NeColumnConst(u16, Value),

    /// Fused: column < constant
    /// Stack: [] -> [bool]
    LtColumnConst(u16, Value),

    /// Fused: column <= constant
    /// Stack: [] -> [bool]
    LeColumnConst(u16, Value),

    /// Fused: column > constant
    /// Stack: [] -> [bool]
    GtColumnConst(u16, Value),

    /// Fused: column >= constant
    /// Stack: [] -> [bool]
    GeColumnConst(u16, Value),

    /// Fused: column IS NULL
    /// Stack: [] -> [bool]
    IsNullColumn(u16),

    /// Fused: column IS NOT NULL
    /// Stack: [] -> [bool]
    IsNotNullColumn(u16),

    /// Fused: column LIKE pattern
    /// Stack: [] -> [bool]
    LikeColumn(u16, Arc<CompiledPattern>, bool), // col_idx, pattern, case_insensitive

    /// Fused: column IN (constant set with AHash)
    /// Stack: [] -> [bool]
    InSetColumn(u16, CompactArc<ValueSet>, bool), // col_idx, set, has_null

    /// Fused: column BETWEEN low AND high (constants)
    /// Stack: [] -> [bool]
    BetweenColumnConst(u16, Value, Value), // col_idx, low, high

    // =========================================================================
    // LOGICAL OPERATIONS
    // =========================================================================
    /// Logical AND with short-circuit
    /// If top of stack is false, jump to target
    /// Stack: [bool] -> [bool] (or jump)
    And(u16), // Jump target if false

    /// Logical OR with short-circuit
    /// If top of stack is true, jump to target
    /// Stack: [bool] -> [bool] (or jump)
    Or(u16), // Jump target if true

    /// Logical NOT
    /// Stack: [bool] -> [bool]
    Not,

    /// Logical XOR
    /// Stack: [a, b] -> [bool]
    Xor,

    /// AND finalize - combine left and right results
    /// Stack: [left_bool, right_bool] -> [bool]
    AndFinalize,

    /// OR finalize - combine left and right results
    /// Stack: [left_bool, right_bool] -> [bool]
    OrFinalize,

    // =========================================================================
    // ARITHMETIC OPERATIONS - Pop 2, push result
    // =========================================================================
    Add,
    Sub,
    Mul,
    Div,
    Mod,

    /// Unary negation
    /// Stack: [value] -> [-value]
    Neg,

    // =========================================================================
    // BITWISE OPERATIONS
    // =========================================================================
    BitAnd,
    BitOr,
    BitXor,
    BitNot,
    Shl,
    Shr,

    // =========================================================================
    // STRING OPERATIONS
    // =========================================================================
    /// String concatenation (binary)
    /// Stack: [a, b] -> [a || b]
    Concat,

    /// Multi-value string concatenation (optimized for chained ||)
    /// Stack: [v1, v2, ..., vN] -> [v1 || v2 || ... || vN]
    /// Pre-calculates total length and allocates once
    ConcatN(u8),

    /// LIKE pattern match (pre-compiled pattern)
    /// Stack: [text] -> [bool]
    Like(Arc<CompiledPattern>, bool), // pattern, case_insensitive

    /// GLOB pattern match
    /// Stack: [text] -> [bool]
    Glob(Arc<CompiledPattern>),

    /// REGEXP match (pre-compiled regex)
    /// Stack: [text] -> [bool]
    Regexp(Arc<regex::Regex>),

    /// LIKE with ESCAPE character
    /// Stack: [text] -> [bool]
    LikeEscape(Arc<CompiledPattern>, bool, char), // pattern, case_insensitive, escape_char

    /// Dynamic LIKE: pattern is on the stack (e.g. from a parameter)
    /// Stack: [text, pattern_text] -> [bool]
    LikeDynamic(bool), // case_insensitive

    /// Dynamic LIKE with ESCAPE: pattern is on the stack, escape char is compiled in
    /// Stack: [text, pattern_text] -> [bool]
    LikeDynamicEscape(bool, char), // case_insensitive, escape_char

    /// Dynamic GLOB: pattern is on the stack
    /// Stack: [text, pattern_text] -> [bool]
    GlobDynamic,

    /// Dynamic REGEXP: pattern is on the stack
    /// Stack: [text, pattern_text] -> [bool]
    RegexpDynamic,

    // =========================================================================
    // JSON OPERATIONS
    // =========================================================================
    /// JSON access: json -> key (returns JSON)
    /// Stack: [json, key] -> [json_value]
    JsonAccess,

    /// JSON access text: json ->> key (returns TEXT)
    /// Stack: [json, key] -> [text_value]
    JsonAccessText,

    // =========================================================================
    // TIMESTAMP OPERATIONS
    // =========================================================================
    /// Add interval to timestamp: timestamp + interval_string
    /// Stack: [timestamp, interval_text] -> [timestamp]
    TimestampAddInterval,

    /// Subtract interval from timestamp: timestamp - interval_string
    /// Stack: [timestamp, interval_text] -> [timestamp]
    TimestampSubInterval,

    /// Subtract timestamps: timestamp - timestamp (returns interval text)
    /// Stack: [timestamp1, timestamp2] -> [interval_text]
    TimestampDiff,

    /// Add days to timestamp: timestamp + integer
    /// Stack: [timestamp, days] -> [timestamp]
    TimestampAddDays,

    /// Subtract days from timestamp: timestamp - integer
    /// Stack: [timestamp, days] -> [timestamp]
    TimestampSubDays,

    // =========================================================================
    // VECTOR DISTANCE OPERATIONS
    // =========================================================================
    /// L2 (Euclidean) distance between two vectors
    /// Stack: [vector1, vector2] -> [float]
    VectorDistanceL2,

    /// Cosine distance between two vectors (1 - cosine_similarity)
    /// Stack: [vector1, vector2] -> [float]
    VectorDistanceCosine,

    /// Negative inner product distance between two vectors
    /// Stack: [vector1, vector2] -> [float]
    VectorDistanceIP,

    // =========================================================================
    // SET OPERATIONS
    // =========================================================================
    /// IN set membership (pre-built FxHashSet for fast lookups)
    /// Stack: [value] -> [bool]
    InSet(CompactArc<ValueSet>, bool), // set, has_null

    /// NOT IN set membership
    /// Stack: [value] -> [bool]
    NotInSet(CompactArc<ValueSet>, bool), // set, has_null

    /// BETWEEN check: value BETWEEN low AND high
    /// Stack: [value, low, high] -> [bool]
    Between,

    /// NOT BETWEEN check
    /// Stack: [value, low, high] -> [bool]
    NotBetween,

    /// Multi-column IN: (a, b) IN ((1, 2), (3, 4))
    /// Stack: [val1, val2, ...valN] -> [bool]
    /// The tuple_values contains pre-evaluated constant tuples
    InTupleSet {
        tuple_size: u8,
        values: Arc<Vec<Vec<Value>>>, // List of tuples
        negated: bool,
    },

    // =========================================================================
    // BOOLEAN CHECKS
    // =========================================================================
    /// IS TRUE check
    /// Stack: [value] -> [bool]
    IsTrue,

    /// IS NOT TRUE check
    /// Stack: [value] -> [bool]
    IsNotTrue,

    /// IS FALSE check
    /// Stack: [value] -> [bool]
    IsFalse,

    /// IS NOT FALSE check
    /// Stack: [value] -> [bool]
    IsNotFalse,

    // =========================================================================
    // FUNCTION CALLS
    // =========================================================================
    /// Call scalar function with N arguments
    /// Stack: [arg1, arg2, ..., argN] -> [result]
    CallScalar {
        func: Arc<dyn ScalarFunction>,
        arg_count: u8,
    },

    /// Special: COALESCE - return first non-null
    /// Stack: [arg1, ..., argN] -> [result]
    Coalesce(u8), // arg count

    /// Special: NULLIF(a, b) - return NULL if a = b
    /// Stack: [a, b] -> [a or null]
    NullIf,

    /// Special: GREATEST - return max of args
    /// Stack: [arg1, ..., argN] -> [result]
    Greatest(u8),

    /// Special: LEAST - return min of args
    /// Stack: [arg1, ..., argN] -> [result]
    Least(u8),

    // =========================================================================
    // NATIVE SCALAR FUNCTIONS (function pointer, no dynamic dispatch)
    // =========================================================================
    /// Native scalar function - single argument, direct function pointer call
    /// Stack: [value] -> [result]
    NativeFn1(NativeFn1),

    // =========================================================================
    // TYPE OPERATIONS
    // =========================================================================
    /// Cast value to target type
    /// Stack: [value] -> [casted_value]
    Cast(DataType),

    /// Truncate timestamp to date (midnight)
    /// Used for CAST(timestamp AS DATE) - truncates time component to 00:00:00
    /// Stack: [value] -> [timestamp_at_midnight]
    TruncateToDate,

    // =========================================================================
    // CASE EXPRESSION
    // =========================================================================
    /// Start of CASE - marks beginning
    CaseStart,

    /// WHEN condition: if top is false, jump to next branch
    /// Stack: [bool] -> [] (condition consumed)
    CaseWhen(u16), // Jump to next WHEN/ELSE/END if false

    /// THEN result: jump to CASE end after pushing result
    /// Stack: [value] -> [value] (then jump)
    CaseThen(u16), // Jump to END

    /// ELSE clause marker
    CaseElse,

    /// End of CASE
    CaseEnd,

    /// Simple CASE: compare value with WHEN value
    /// Stack: [case_value, when_value] -> [bool]
    CaseCompare,

    // =========================================================================
    // CONTROL FLOW
    // =========================================================================
    /// Unconditional jump
    Jump(u16),

    /// Jump if top of stack is true (doesn't pop)
    JumpIfTrue(u16),

    /// Jump if top of stack is false (doesn't pop)
    JumpIfFalse(u16),

    /// Jump if top of stack is NULL (doesn't pop)
    JumpIfNull(u16),

    /// Jump if top of stack is NOT NULL (doesn't pop)
    /// Used for COALESCE short-circuit evaluation
    JumpIfNotNull(u16),

    /// Pop and jump if true
    PopJumpIfTrue(u16),

    /// Pop and jump if false
    PopJumpIfFalse(u16),

    /// Duplicate top of stack
    Dup,

    /// Pop top of stack (discard)
    Pop,

    /// Swap top two stack elements
    Swap,

    // =========================================================================
    // SUBQUERY OPERATIONS (for correlated/scalar subqueries)
    // =========================================================================
    /// Execute scalar subquery and push result
    /// The subquery plan is stored separately and referenced by index
    /// Stack: [] -> [value]
    ExecScalarSubquery(u16), // Subquery plan index

    /// Execute EXISTS subquery
    /// Stack: [] -> [bool]
    ExecExists(u16), // Subquery plan index

    /// Execute IN subquery
    /// Stack: [value] -> [bool]
    ExecInSubquery(u16), // Subquery plan index

    /// Execute ALL comparison subquery
    /// Stack: [value] -> [bool]
    ExecAll(u16, CompareOp), // Subquery index, comparison operator

    /// Execute ANY comparison subquery
    /// Stack: [value] -> [bool]
    ExecAny(u16, CompareOp), // Subquery index, comparison operator

    // =========================================================================
    // AGGREGATE REFERENCES (post-aggregation)
    // =========================================================================
    /// Load pre-computed aggregate result by column index
    /// Used in HAVING clauses where aggregates are already computed
    /// Stack: [] -> [value]
    LoadAggregateResult(u16),

    /// Load current transaction ID
    /// Returns NULL if no transaction is active
    /// Stack: [] -> [value]
    LoadTransactionId,

    // =========================================================================
    // SPECIAL
    // =========================================================================
    /// No operation (placeholder)
    Nop,

    /// Return current top of stack as result
    Return,

    /// Return true immediately
    ReturnTrue,

    /// Return false immediately
    ReturnFalse,

    /// Return NULL immediately
    ReturnNull(DataType),
}

/// Comparison operator for ALL/ANY subqueries
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CompareOp {
    Eq,
    Ne,
    Lt,
    Le,
    Gt,
    Ge,
}

impl CompareOp {
    pub fn compare(&self, a: &Value, b: &Value) -> Option<bool> {
        match (a.partial_cmp(b), self) {
            (Some(std::cmp::Ordering::Equal), CompareOp::Eq) => Some(true),
            (Some(std::cmp::Ordering::Equal), CompareOp::Ne) => Some(false),
            (Some(std::cmp::Ordering::Equal), CompareOp::Le) => Some(true),
            (Some(std::cmp::Ordering::Equal), CompareOp::Ge) => Some(true),
            (Some(std::cmp::Ordering::Equal), CompareOp::Lt) => Some(false),
            (Some(std::cmp::Ordering::Equal), CompareOp::Gt) => Some(false),

            (Some(std::cmp::Ordering::Less), CompareOp::Lt) => Some(true),
            (Some(std::cmp::Ordering::Less), CompareOp::Le) => Some(true),
            (Some(std::cmp::Ordering::Less), CompareOp::Ne) => Some(true),
            (Some(std::cmp::Ordering::Less), CompareOp::Eq) => Some(false),
            (Some(std::cmp::Ordering::Less), CompareOp::Gt) => Some(false),
            (Some(std::cmp::Ordering::Less), CompareOp::Ge) => Some(false),

            (Some(std::cmp::Ordering::Greater), CompareOp::Gt) => Some(true),
            (Some(std::cmp::Ordering::Greater), CompareOp::Ge) => Some(true),
            (Some(std::cmp::Ordering::Greater), CompareOp::Ne) => Some(true),
            (Some(std::cmp::Ordering::Greater), CompareOp::Eq) => Some(false),
            (Some(std::cmp::Ordering::Greater), CompareOp::Lt) => Some(false),
            (Some(std::cmp::Ordering::Greater), CompareOp::Le) => Some(false),

            (None, _) => None, // NULL comparison
        }
    }
}

// Make Op Debug-printable (without showing full function pointers)
impl std::fmt::Debug for Op {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Op::LoadColumn(idx) => write!(f, "LoadColumn({})", idx),
            Op::LoadColumn2(idx) => write!(f, "LoadColumn2({})", idx),
            Op::LoadOuterColumn(name) => write!(f, "LoadOuterColumn({})", name),
            Op::LoadConst(v) => write!(f, "LoadConst({:?})", v),
            Op::LoadParam(idx) => write!(f, "LoadParam({})", idx),
            Op::LoadNamedParam(name) => write!(f, "LoadNamedParam({})", name),
            Op::LoadNull(dt) => write!(f, "LoadNull({:?})", dt),
            Op::Eq => write!(f, "Eq"),
            Op::Ne => write!(f, "Ne"),
            Op::Lt => write!(f, "Lt"),
            Op::Le => write!(f, "Le"),
            Op::Gt => write!(f, "Gt"),
            Op::Ge => write!(f, "Ge"),
            Op::IsNull => write!(f, "IsNull"),
            Op::IsNotNull => write!(f, "IsNotNull"),
            Op::IsDistinctFrom => write!(f, "IsDistinctFrom"),
            Op::IsNotDistinctFrom => write!(f, "IsNotDistinctFrom"),
            Op::EqColumnConst(col, val) => write!(f, "EqColumnConst({}, {:?})", col, val),
            Op::NeColumnConst(col, val) => write!(f, "NeColumnConst({}, {:?})", col, val),
            Op::LtColumnConst(col, val) => write!(f, "LtColumnConst({}, {:?})", col, val),
            Op::LeColumnConst(col, val) => write!(f, "LeColumnConst({}, {:?})", col, val),
            Op::GtColumnConst(col, val) => write!(f, "GtColumnConst({}, {:?})", col, val),
            Op::GeColumnConst(col, val) => write!(f, "GeColumnConst({}, {:?})", col, val),
            Op::IsNullColumn(col) => write!(f, "IsNullColumn({})", col),
            Op::IsNotNullColumn(col) => write!(f, "IsNotNullColumn({})", col),
            Op::LikeColumn(col, _, ci) => write!(f, "LikeColumn({}, case_insensitive={})", col, ci),
            Op::InSetColumn(col, set, has_null) => {
                write!(
                    f,
                    "InSetColumn({}, len={}, has_null={})",
                    col,
                    set.len(),
                    has_null
                )
            }
            Op::BetweenColumnConst(col, low, high) => {
                write!(f, "BetweenColumnConst({}, {:?}, {:?})", col, low, high)
            }
            Op::And(target) => write!(f, "And(jump={})", target),
            Op::Or(target) => write!(f, "Or(jump={})", target),
            Op::Not => write!(f, "Not"),
            Op::Xor => write!(f, "Xor"),
            Op::AndFinalize => write!(f, "AndFinalize"),
            Op::OrFinalize => write!(f, "OrFinalize"),
            Op::Add => write!(f, "Add"),
            Op::Sub => write!(f, "Sub"),
            Op::Mul => write!(f, "Mul"),
            Op::Div => write!(f, "Div"),
            Op::Mod => write!(f, "Mod"),
            Op::Neg => write!(f, "Neg"),
            Op::BitAnd => write!(f, "BitAnd"),
            Op::BitOr => write!(f, "BitOr"),
            Op::BitXor => write!(f, "BitXor"),
            Op::BitNot => write!(f, "BitNot"),
            Op::Shl => write!(f, "Shl"),
            Op::Shr => write!(f, "Shr"),
            Op::Concat => write!(f, "Concat"),
            Op::Like(_, ci) => write!(f, "Like(case_insensitive={})", ci),
            Op::Glob(_) => write!(f, "Glob"),
            Op::Regexp(_) => write!(f, "Regexp"),
            Op::LikeEscape(_, ci, esc) => {
                write!(f, "LikeEscape(case_insensitive={}, escape='{}')", ci, esc)
            }
            Op::LikeDynamic(ci) => write!(f, "LikeDynamic(case_insensitive={})", ci),
            Op::LikeDynamicEscape(ci, esc) => {
                write!(
                    f,
                    "LikeDynamicEscape(case_insensitive={}, escape='{}')",
                    ci, esc
                )
            }
            Op::GlobDynamic => write!(f, "GlobDynamic"),
            Op::RegexpDynamic => write!(f, "RegexpDynamic"),
            Op::JsonAccess => write!(f, "JsonAccess"),
            Op::JsonAccessText => write!(f, "JsonAccessText"),
            Op::TimestampAddInterval => write!(f, "TimestampAddInterval"),
            Op::TimestampSubInterval => write!(f, "TimestampSubInterval"),
            Op::TimestampDiff => write!(f, "TimestampDiff"),
            Op::TimestampAddDays => write!(f, "TimestampAddDays"),
            Op::TimestampSubDays => write!(f, "TimestampSubDays"),
            Op::VectorDistanceL2 => write!(f, "VectorDistanceL2"),
            Op::VectorDistanceCosine => write!(f, "VectorDistanceCosine"),
            Op::VectorDistanceIP => write!(f, "VectorDistanceIP"),
            Op::InSet(set, has_null) => {
                write!(f, "InSet(len={}, has_null={})", set.len(), has_null)
            }
            Op::NotInSet(set, has_null) => {
                write!(f, "NotInSet(len={}, has_null={})", set.len(), has_null)
            }
            Op::Between => write!(f, "Between"),
            Op::NotBetween => write!(f, "NotBetween"),
            Op::InTupleSet {
                tuple_size,
                values,
                negated,
            } => {
                write!(
                    f,
                    "InTupleSet(size={}, tuples={}, negated={})",
                    tuple_size,
                    values.len(),
                    negated
                )
            }
            Op::IsTrue => write!(f, "IsTrue"),
            Op::IsNotTrue => write!(f, "IsNotTrue"),
            Op::IsFalse => write!(f, "IsFalse"),
            Op::IsNotFalse => write!(f, "IsNotFalse"),
            Op::CallScalar { arg_count, .. } => write!(f, "CallScalar(args={})", arg_count),
            Op::Coalesce(n) => write!(f, "Coalesce({})", n),
            Op::NullIf => write!(f, "NullIf"),
            Op::Greatest(n) => write!(f, "Greatest({})", n),
            Op::Least(n) => write!(f, "Least({})", n),
            Op::Cast(dt) => write!(f, "Cast({:?})", dt),
            Op::TruncateToDate => write!(f, "TruncateToDate"),
            Op::CaseStart => write!(f, "CaseStart"),
            Op::CaseWhen(target) => write!(f, "CaseWhen(jump={})", target),
            Op::CaseThen(target) => write!(f, "CaseThen(jump={})", target),
            Op::CaseElse => write!(f, "CaseElse"),
            Op::CaseEnd => write!(f, "CaseEnd"),
            Op::CaseCompare => write!(f, "CaseCompare"),
            Op::Jump(target) => write!(f, "Jump({})", target),
            Op::JumpIfTrue(target) => write!(f, "JumpIfTrue({})", target),
            Op::JumpIfFalse(target) => write!(f, "JumpIfFalse({})", target),
            Op::JumpIfNull(target) => write!(f, "JumpIfNull({})", target),
            Op::JumpIfNotNull(target) => write!(f, "JumpIfNotNull({})", target),
            Op::PopJumpIfTrue(target) => write!(f, "PopJumpIfTrue({})", target),
            Op::PopJumpIfFalse(target) => write!(f, "PopJumpIfFalse({})", target),
            Op::Dup => write!(f, "Dup"),
            Op::Pop => write!(f, "Pop"),
            Op::Swap => write!(f, "Swap"),
            Op::ExecScalarSubquery(idx) => write!(f, "ExecScalarSubquery({})", idx),
            Op::ExecExists(idx) => write!(f, "ExecExists({})", idx),
            Op::ExecInSubquery(idx) => write!(f, "ExecInSubquery({})", idx),
            Op::ExecAll(idx, op) => write!(f, "ExecAll({}, {:?})", idx, op),
            Op::ExecAny(idx, op) => write!(f, "ExecAny({}, {:?})", idx, op),
            Op::LoadAggregateResult(idx) => write!(f, "LoadAggregateResult({})", idx),
            Op::LoadTransactionId => write!(f, "LoadTransactionId"),
            Op::Nop => write!(f, "Nop"),
            Op::Return => write!(f, "Return"),
            Op::ReturnTrue => write!(f, "ReturnTrue"),
            Op::ReturnFalse => write!(f, "ReturnFalse"),
            Op::ReturnNull(dt) => write!(f, "ReturnNull({:?})", dt),
            Op::NativeFn1(_) => write!(f, "NativeFn1(...)"),
            Op::ConcatN(n) => write!(f, "ConcatN({})", n),
        }
    }
}

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

    #[test]
    fn test_op_size() {
        let size = mem::size_of::<Op>();
        println!("Op enum size: {} bytes", size);
        println!("Op alignment: {} bytes", mem::align_of::<Op>());

        // The Op enum contains large variants like:
        // - InTupleSet { tuple_size: u8, values: Arc<Vec<Vec<Value>>>, negated: bool }
        // - CallScalar { func: Arc<dyn ScalarFunction>, arg_count: u8 }
        // - GtColumnConst(u16, Value) where Value is 24 bytes

        // We want to keep Op small for cache efficiency
        // Ideally under 32 bytes, definitely under 64 bytes
        assert!(size <= 64, "Op enum is too large: {} bytes", size);
    }

    // =========================================================================
    // CompiledPattern::compile() tests - LIKE patterns
    // =========================================================================

    #[test]
    fn test_pattern_exact() {
        let pattern = CompiledPattern::compile("hello", false);
        assert!(matches!(pattern, CompiledPattern::Exact(ref s) if s == "hello"));
        assert!(pattern.matches("hello", false));
        assert!(!pattern.matches("Hello", false));
        assert!(!pattern.matches("hello world", false));
    }

    #[test]
    fn test_pattern_exact_case_insensitive() {
        let pattern = CompiledPattern::compile("Hello", true);
        assert!(matches!(pattern, CompiledPattern::Exact(ref s) if s == "hello"));
        assert!(pattern.matches("hello", true));
        assert!(pattern.matches("HELLO", true));
        assert!(pattern.matches("HeLLo", true));
    }

    #[test]
    fn test_pattern_match_all() {
        let pattern = CompiledPattern::compile("%", false);
        assert!(matches!(pattern, CompiledPattern::MatchAll));
        assert!(pattern.matches("", false));
        assert!(pattern.matches("anything", false));
        assert!(pattern.matches("with spaces and 123", false));
    }

    #[test]
    fn test_pattern_single_char() {
        let pattern = CompiledPattern::compile("_", false);
        assert!(matches!(pattern, CompiledPattern::SingleChar));
        assert!(pattern.matches("a", false));
        assert!(pattern.matches("Z", false));
        assert!(!pattern.matches("", false));
        assert!(!pattern.matches("ab", false));
    }

    #[test]
    fn test_pattern_prefix() {
        let pattern = CompiledPattern::compile("hello%", false);
        assert!(matches!(pattern, CompiledPattern::Prefix(ref s) if s == "hello"));
        assert!(pattern.matches("hello", false));
        assert!(pattern.matches("hello world", false));
        assert!(pattern.matches("hellooooo", false));
        assert!(!pattern.matches("Hello", false));
        assert!(!pattern.matches("say hello", false));
    }

    #[test]
    fn test_pattern_prefix_case_insensitive() {
        let pattern = CompiledPattern::compile("Hello%", true);
        assert!(pattern.matches("hello world", true));
        assert!(pattern.matches("HELLO WORLD", true));
        assert!(!pattern.matches("say hello", true));
    }

    #[test]
    fn test_pattern_suffix() {
        let pattern = CompiledPattern::compile("%world", false);
        assert!(matches!(pattern, CompiledPattern::Suffix(ref s) if s == "world"));
        assert!(pattern.matches("world", false));
        assert!(pattern.matches("hello world", false));
        assert!(!pattern.matches("World", false));
        assert!(!pattern.matches("world!", false));
    }

    #[test]
    fn test_pattern_suffix_case_insensitive() {
        let pattern = CompiledPattern::compile("%World", true);
        assert!(pattern.matches("hello world", true));
        assert!(pattern.matches("HELLO WORLD", true));
        assert!(!pattern.matches("world!", true));
    }

    #[test]
    fn test_pattern_contains() {
        let pattern = CompiledPattern::compile("%ello%", false);
        assert!(matches!(pattern, CompiledPattern::Contains(ref s) if s == "ello"));
        assert!(pattern.matches("hello", false));
        assert!(pattern.matches("yellow", false));
        assert!(pattern.matches("hello world", false));
        assert!(!pattern.matches("HELLO", false));
    }

    #[test]
    fn test_pattern_contains_case_insensitive() {
        let pattern = CompiledPattern::compile("%ELLO%", true);
        assert!(pattern.matches("hello", true));
        assert!(pattern.matches("YELLOW", true));
        assert!(!pattern.matches("hi", true));
    }

    #[test]
    fn test_pattern_prefix_suffix() {
        let pattern = CompiledPattern::compile("hello%world", false);
        assert!(
            matches!(pattern, CompiledPattern::PrefixSuffix(ref p, ref s) if p == "hello" && s == "world")
        );
        assert!(pattern.matches("helloworld", false));
        assert!(pattern.matches("hello world", false));
        assert!(pattern.matches("hello beautiful world", false));
        assert!(!pattern.matches("hello", false));
        assert!(!pattern.matches("world", false));
    }

    #[test]
    fn test_pattern_prefix_suffix_case_insensitive() {
        let pattern = CompiledPattern::compile("Hello%World", true);
        assert!(pattern.matches("helloworld", true));
        assert!(pattern.matches("HELLO WORLD", true));
        assert!(!pattern.matches("hello", true));
    }

    #[test]
    fn test_pattern_prefix_suffix_too_short() {
        let pattern = CompiledPattern::compile("abc%xyz", false);
        // Text must be at least prefix.len() + suffix.len()
        assert!(!pattern.matches("abcxy", false)); // too short
        assert!(pattern.matches("abcxyz", false)); // exact length
        assert!(pattern.matches("abc123xyz", false));
    }

    #[test]
    fn test_pattern_complex_regex() {
        // Pattern with multiple % or _ that requires regex
        let pattern = CompiledPattern::compile("a%b%c", false);
        assert!(matches!(pattern, CompiledPattern::Regex(_)));
        assert!(pattern.matches("abc", false));
        assert!(pattern.matches("aXbYc", false));
        assert!(pattern.matches("aXXXbYYYc", false));
        assert!(!pattern.matches("ac", false));
    }

    #[test]
    fn test_pattern_underscore_regex() {
        let pattern = CompiledPattern::compile("a_c", false);
        assert!(matches!(pattern, CompiledPattern::Regex(_)));
        assert!(pattern.matches("abc", false));
        assert!(pattern.matches("aXc", false));
        assert!(!pattern.matches("ac", false));
        assert!(!pattern.matches("abbc", false));
    }

    #[test]
    fn test_pattern_mixed_wildcards() {
        let pattern = CompiledPattern::compile("a_%b", false);
        assert!(matches!(pattern, CompiledPattern::Regex(_)));
        assert!(pattern.matches("aXb", false));
        assert!(pattern.matches("aXYZb", false));
        assert!(!pattern.matches("ab", false));
    }

    // =========================================================================
    // CompiledPattern::compile_glob() tests - GLOB patterns
    // =========================================================================

    #[test]
    fn test_glob_exact() {
        let pattern = CompiledPattern::compile_glob("hello");
        assert!(matches!(pattern, CompiledPattern::Exact(ref s) if s == "hello"));
        assert!(pattern.matches("hello", false));
        assert!(!pattern.matches("Hello", false));
    }

    #[test]
    fn test_glob_match_all() {
        let pattern = CompiledPattern::compile_glob("*");
        assert!(matches!(pattern, CompiledPattern::MatchAll));
        assert!(pattern.matches("anything", false));
    }

    #[test]
    fn test_glob_single_char() {
        let pattern = CompiledPattern::compile_glob("?");
        assert!(matches!(pattern, CompiledPattern::SingleChar));
        assert!(pattern.matches("a", false));
        assert!(!pattern.matches("ab", false));
    }

    #[test]
    fn test_glob_prefix() {
        let pattern = CompiledPattern::compile_glob("hello*");
        assert!(matches!(pattern, CompiledPattern::Prefix(ref s) if s == "hello"));
        assert!(pattern.matches("hello", false));
        assert!(pattern.matches("hello world", false));
    }

    #[test]
    fn test_glob_suffix() {
        let pattern = CompiledPattern::compile_glob("*world");
        assert!(matches!(pattern, CompiledPattern::Suffix(ref s) if s == "world"));
        assert!(pattern.matches("world", false));
        assert!(pattern.matches("hello world", false));
    }

    #[test]
    fn test_glob_contains() {
        let pattern = CompiledPattern::compile_glob("*ello*");
        assert!(matches!(pattern, CompiledPattern::Contains(ref s) if s == "ello"));
        assert!(pattern.matches("hello", false));
        assert!(pattern.matches("yellow", false));
    }

    #[test]
    fn test_glob_prefix_suffix() {
        let pattern = CompiledPattern::compile_glob("hello*world");
        assert!(
            matches!(pattern, CompiledPattern::PrefixSuffix(ref p, ref s) if p == "hello" && s == "world")
        );
        assert!(pattern.matches("helloworld", false));
        assert!(pattern.matches("hello beautiful world", false));
    }

    #[test]
    fn test_glob_complex_regex() {
        let pattern = CompiledPattern::compile_glob("a*b*c");
        assert!(matches!(pattern, CompiledPattern::Regex(_)));
        assert!(pattern.matches("abc", false));
        assert!(pattern.matches("aXbYc", false));
    }

    #[test]
    fn test_glob_question_mark() {
        let pattern = CompiledPattern::compile_glob("a?c");
        assert!(matches!(pattern, CompiledPattern::Regex(_)));
        assert!(pattern.matches("abc", false));
        assert!(!pattern.matches("ac", false));
    }

    #[test]
    fn test_glob_character_class() {
        // Character class alone without * or ? is treated as exact match
        let pattern = CompiledPattern::compile_glob("a[bc]d");
        assert!(matches!(pattern, CompiledPattern::Exact(_)));

        // Pattern with * at end is treated as Prefix
        let pattern2 = CompiledPattern::compile_glob("a[bc]*");
        assert!(matches!(pattern2, CompiledPattern::Prefix(ref s) if s == "a[bc]"));

        // *[bc]* is Contains since middle has no wildcards
        let pattern3 = CompiledPattern::compile_glob("*[bc]*");
        assert!(matches!(pattern3, CompiledPattern::Contains(ref s) if s == "[bc]"));
    }

    #[test]
    fn test_glob_escape_edge_cases() {
        // `a\\*b` splits on * giving ["a\\", "b"] - treated as PrefixSuffix
        let pattern1 = CompiledPattern::compile_glob("a\\*b");
        assert!(matches!(pattern1, CompiledPattern::PrefixSuffix(_, _)));

        // Complex escapes with multiple wildcards go to regex
        let pattern2 = CompiledPattern::compile_glob("*a*b*");
        assert!(matches!(pattern2, CompiledPattern::Regex(_)));
        assert!(pattern2.matches("XaYbZ", false));
        assert!(pattern2.matches("ab", false));
    }

    // =========================================================================
    // CompareOp tests
    // =========================================================================

    #[test]
    fn test_compare_op_eq() {
        let op = CompareOp::Eq;
        assert_eq!(
            op.compare(&Value::Integer(5), &Value::Integer(5)),
            Some(true)
        );
        assert_eq!(
            op.compare(&Value::Integer(5), &Value::Integer(6)),
            Some(false)
        );
        assert_eq!(
            op.compare(&Value::Integer(6), &Value::Integer(5)),
            Some(false)
        );
    }

    #[test]
    fn test_compare_op_ne() {
        let op = CompareOp::Ne;
        assert_eq!(
            op.compare(&Value::Integer(5), &Value::Integer(5)),
            Some(false)
        );
        assert_eq!(
            op.compare(&Value::Integer(5), &Value::Integer(6)),
            Some(true)
        );
        assert_eq!(
            op.compare(&Value::Integer(6), &Value::Integer(5)),
            Some(true)
        );
    }

    #[test]
    fn test_compare_op_lt() {
        let op = CompareOp::Lt;
        assert_eq!(
            op.compare(&Value::Integer(5), &Value::Integer(6)),
            Some(true)
        );
        assert_eq!(
            op.compare(&Value::Integer(5), &Value::Integer(5)),
            Some(false)
        );
        assert_eq!(
            op.compare(&Value::Integer(6), &Value::Integer(5)),
            Some(false)
        );
    }

    #[test]
    fn test_compare_op_le() {
        let op = CompareOp::Le;
        assert_eq!(
            op.compare(&Value::Integer(5), &Value::Integer(6)),
            Some(true)
        );
        assert_eq!(
            op.compare(&Value::Integer(5), &Value::Integer(5)),
            Some(true)
        );
        assert_eq!(
            op.compare(&Value::Integer(6), &Value::Integer(5)),
            Some(false)
        );
    }

    #[test]
    fn test_compare_op_gt() {
        let op = CompareOp::Gt;
        assert_eq!(
            op.compare(&Value::Integer(6), &Value::Integer(5)),
            Some(true)
        );
        assert_eq!(
            op.compare(&Value::Integer(5), &Value::Integer(5)),
            Some(false)
        );
        assert_eq!(
            op.compare(&Value::Integer(5), &Value::Integer(6)),
            Some(false)
        );
    }

    #[test]
    fn test_compare_op_ge() {
        let op = CompareOp::Ge;
        assert_eq!(
            op.compare(&Value::Integer(6), &Value::Integer(5)),
            Some(true)
        );
        assert_eq!(
            op.compare(&Value::Integer(5), &Value::Integer(5)),
            Some(true)
        );
        assert_eq!(
            op.compare(&Value::Integer(5), &Value::Integer(6)),
            Some(false)
        );
    }

    #[test]
    fn test_compare_op_with_booleans() {
        // Test boolean comparisons
        let op = CompareOp::Eq;
        assert_eq!(
            op.compare(&Value::Boolean(true), &Value::Boolean(true)),
            Some(true)
        );
        assert_eq!(
            op.compare(&Value::Boolean(true), &Value::Boolean(false)),
            Some(false)
        );
        assert_eq!(
            op.compare(&Value::Boolean(false), &Value::Boolean(false)),
            Some(true)
        );

        let op_ne = CompareOp::Ne;
        assert_eq!(
            op_ne.compare(&Value::Boolean(true), &Value::Boolean(false)),
            Some(true)
        );
    }

    #[test]
    fn test_compare_op_strings() {
        let op = CompareOp::Lt;
        assert_eq!(
            op.compare(&Value::Text("apple".into()), &Value::Text("banana".into())),
            Some(true)
        );
        assert_eq!(
            op.compare(&Value::Text("banana".into()), &Value::Text("apple".into())),
            Some(false)
        );
    }

    #[test]
    fn test_compare_op_floats() {
        let op = CompareOp::Ge;
        assert_eq!(
            op.compare(&Value::Float(2.5), &Value::Float(2.5)),
            Some(true)
        );
        assert_eq!(
            op.compare(&Value::Float(2.6), &Value::Float(2.5)),
            Some(true)
        );
        assert_eq!(
            op.compare(&Value::Float(2.4), &Value::Float(2.5)),
            Some(false)
        );
    }

    // =========================================================================
    // Op Debug format tests
    // =========================================================================

    #[test]
    fn test_op_debug_format() {
        // Test that Debug formatting works for various Op variants
        assert_eq!(format!("{:?}", Op::LoadColumn(5)), "LoadColumn(5)");
        assert_eq!(format!("{:?}", Op::LoadColumn2(3)), "LoadColumn2(3)");
        assert_eq!(format!("{:?}", Op::Eq), "Eq");
        assert_eq!(format!("{:?}", Op::Ne), "Ne");
        assert_eq!(format!("{:?}", Op::Lt), "Lt");
        assert_eq!(format!("{:?}", Op::Le), "Le");
        assert_eq!(format!("{:?}", Op::Gt), "Gt");
        assert_eq!(format!("{:?}", Op::Ge), "Ge");
        assert_eq!(format!("{:?}", Op::Add), "Add");
        assert_eq!(format!("{:?}", Op::Sub), "Sub");
        assert_eq!(format!("{:?}", Op::Mul), "Mul");
        assert_eq!(format!("{:?}", Op::Div), "Div");
        assert_eq!(format!("{:?}", Op::Mod), "Mod");
        assert_eq!(format!("{:?}", Op::Not), "Not");
        assert_eq!(format!("{:?}", Op::Neg), "Neg");
        assert_eq!(format!("{:?}", Op::IsNull), "IsNull");
        assert_eq!(format!("{:?}", Op::IsNotNull), "IsNotNull");
        assert_eq!(format!("{:?}", Op::Return), "Return");
        assert_eq!(format!("{:?}", Op::ReturnTrue), "ReturnTrue");
        assert_eq!(format!("{:?}", Op::ReturnFalse), "ReturnFalse");
        assert_eq!(format!("{:?}", Op::Nop), "Nop");
        assert_eq!(format!("{:?}", Op::Dup), "Dup");
        assert_eq!(format!("{:?}", Op::Pop), "Pop");
        assert_eq!(format!("{:?}", Op::Swap), "Swap");
    }

    #[test]
    fn test_op_debug_format_with_values() {
        assert_eq!(
            format!("{:?}", Op::LoadConst(Value::Integer(42))),
            "LoadConst(Integer(42))"
        );
        assert_eq!(
            format!("{:?}", Op::EqColumnConst(0, Value::Integer(10))),
            "EqColumnConst(0, Integer(10))"
        );
        assert_eq!(format!("{:?}", Op::And(5)), "And(jump=5)");
        assert_eq!(format!("{:?}", Op::Or(10)), "Or(jump=10)");
        assert_eq!(format!("{:?}", Op::Jump(15)), "Jump(15)");
        assert_eq!(format!("{:?}", Op::JumpIfTrue(20)), "JumpIfTrue(20)");
        assert_eq!(format!("{:?}", Op::JumpIfFalse(25)), "JumpIfFalse(25)");
    }

    #[test]
    fn test_op_debug_format_special() {
        assert_eq!(format!("{:?}", Op::Coalesce(3)), "Coalesce(3)");
        assert_eq!(format!("{:?}", Op::NullIf), "NullIf");
        assert_eq!(format!("{:?}", Op::Greatest(2)), "Greatest(2)");
        assert_eq!(format!("{:?}", Op::Least(4)), "Least(4)");
        assert_eq!(format!("{:?}", Op::Between), "Between");
        assert_eq!(format!("{:?}", Op::NotBetween), "NotBetween");
        assert_eq!(format!("{:?}", Op::Concat), "Concat");
        assert_eq!(format!("{:?}", Op::JsonAccess), "JsonAccess");
        assert_eq!(format!("{:?}", Op::JsonAccessText), "JsonAccessText");
    }

    #[test]
    fn test_op_debug_bitwise() {
        assert_eq!(format!("{:?}", Op::BitAnd), "BitAnd");
        assert_eq!(format!("{:?}", Op::BitOr), "BitOr");
        assert_eq!(format!("{:?}", Op::BitXor), "BitXor");
        assert_eq!(format!("{:?}", Op::BitNot), "BitNot");
        assert_eq!(format!("{:?}", Op::Shl), "Shl");
        assert_eq!(format!("{:?}", Op::Shr), "Shr");
    }

    #[test]
    fn test_op_debug_case() {
        assert_eq!(format!("{:?}", Op::CaseStart), "CaseStart");
        assert_eq!(format!("{:?}", Op::CaseWhen(5)), "CaseWhen(jump=5)");
        assert_eq!(format!("{:?}", Op::CaseThen(10)), "CaseThen(jump=10)");
        assert_eq!(format!("{:?}", Op::CaseElse), "CaseElse");
        assert_eq!(format!("{:?}", Op::CaseEnd), "CaseEnd");
        assert_eq!(format!("{:?}", Op::CaseCompare), "CaseCompare");
    }

    #[test]
    fn test_op_debug_timestamp() {
        assert_eq!(
            format!("{:?}", Op::TimestampAddInterval),
            "TimestampAddInterval"
        );
        assert_eq!(
            format!("{:?}", Op::TimestampSubInterval),
            "TimestampSubInterval"
        );
        assert_eq!(format!("{:?}", Op::TimestampDiff), "TimestampDiff");
        assert_eq!(format!("{:?}", Op::TimestampAddDays), "TimestampAddDays");
        assert_eq!(format!("{:?}", Op::TimestampSubDays), "TimestampSubDays");
    }

    #[test]
    fn test_op_debug_boolean_checks() {
        assert_eq!(format!("{:?}", Op::IsTrue), "IsTrue");
        assert_eq!(format!("{:?}", Op::IsNotTrue), "IsNotTrue");
        assert_eq!(format!("{:?}", Op::IsFalse), "IsFalse");
        assert_eq!(format!("{:?}", Op::IsNotFalse), "IsNotFalse");
        assert_eq!(format!("{:?}", Op::IsDistinctFrom), "IsDistinctFrom");
        assert_eq!(format!("{:?}", Op::IsNotDistinctFrom), "IsNotDistinctFrom");
    }

    #[test]
    fn test_op_debug_subquery() {
        assert_eq!(
            format!("{:?}", Op::ExecScalarSubquery(0)),
            "ExecScalarSubquery(0)"
        );
        assert_eq!(format!("{:?}", Op::ExecExists(1)), "ExecExists(1)");
        assert_eq!(format!("{:?}", Op::ExecInSubquery(2)), "ExecInSubquery(2)");
        assert_eq!(
            format!("{:?}", Op::ExecAll(3, CompareOp::Gt)),
            "ExecAll(3, Gt)"
        );
        assert_eq!(
            format!("{:?}", Op::ExecAny(4, CompareOp::Lt)),
            "ExecAny(4, Lt)"
        );
    }

    #[test]
    fn test_op_debug_sets() {
        use crate::core::ValueSet;
        let set = CompactArc::new(ValueSet::default());
        assert!(format!("{:?}", Op::InSet(set.clone(), false)).contains("InSet"));
        assert!(format!("{:?}", Op::NotInSet(set.clone(), true)).contains("NotInSet"));
        assert!(format!("{:?}", Op::InSetColumn(0, set, false)).contains("InSetColumn"));
    }

    #[test]
    fn test_op_debug_like_glob() {
        let pattern = Arc::new(CompiledPattern::compile("test%", false));
        assert!(format!("{:?}", Op::Like(pattern.clone(), false)).contains("Like"));
        assert!(format!("{:?}", Op::Glob(pattern.clone())).contains("Glob"));
        assert!(format!("{:?}", Op::LikeColumn(0, pattern, false)).contains("LikeColumn"));
    }
}