motedb 0.1.6

AI-native embedded multimodal database for embodied intelligence (robots, AR glasses, industrial arms).
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
/// Expression evaluator - evaluates expressions against rows
use super::ast::{Expr, BinaryOperator, UnaryOperator};
use crate::error::{Result, MoteDBError};
use crate::types::{Value, SqlRow};
use crate::database::MoteDB;
use std::sync::Arc;
use std::collections::HashMap;
use std::sync::RwLock;
use std::cell::RefCell;

/// ⚡ Compiled LIKE pattern for fast matching
#[derive(Debug, Clone)]
enum CompiledPattern {
    /// Exact match: "abc" (no wildcards)
    Exact(String),
    /// Prefix match: "abc%" 
    Prefix(String),
    /// Suffix match: "%abc"
    Suffix(String),
    /// Contains match: "%abc%"
    Contains(String),
    /// Complex pattern with multiple wildcards
    Complex(Vec<PatternSegment>),
}

#[derive(Debug, Clone)]
enum PatternSegment {
    Literal(String),
    AnyChar,      // _
    AnyChars,     // %
}

impl CompiledPattern {
    /// Compile LIKE pattern into optimized form
    fn compile(pattern: &str) -> Self {
        // Fast path: no wildcards
        if !pattern.contains('%') && !pattern.contains('_') {
            return CompiledPattern::Exact(pattern.to_string());
        }
        
        // Fast path: prefix match "abc%"
        if pattern.ends_with('%') && !pattern[..pattern.len()-1].contains('%') && !pattern.contains('_') {
            return CompiledPattern::Prefix(pattern[..pattern.len()-1].to_string());
        }
        
        // Fast path: suffix match "%abc"
        if pattern.starts_with('%') && !pattern[1..].contains('%') && !pattern.contains('_') {
            return CompiledPattern::Suffix(pattern[1..].to_string());
        }
        
        // Fast path: contains match "%abc%"
        if pattern.starts_with('%') && pattern.ends_with('%') 
            && pattern.len() > 2
            && !pattern[1..pattern.len()-1].contains('%') 
            && !pattern.contains('_') 
        {
            return CompiledPattern::Contains(pattern[1..pattern.len()-1].to_string());
        }
        
        // Complex pattern: parse into segments
        let mut segments = Vec::new();
        let mut current_literal = String::new();
        
        for ch in pattern.chars() {
            match ch {
                '%' => {
                    if !current_literal.is_empty() {
                        segments.push(PatternSegment::Literal(current_literal.clone()));
                        current_literal.clear();
                    }
                    segments.push(PatternSegment::AnyChars);
                }
                '_' => {
                    if !current_literal.is_empty() {
                        segments.push(PatternSegment::Literal(current_literal.clone()));
                        current_literal.clear();
                    }
                    segments.push(PatternSegment::AnyChar);
                }
                c => {
                    current_literal.push(c);
                }
            }
        }
        
        if !current_literal.is_empty() {
            segments.push(PatternSegment::Literal(current_literal));
        }
        
        CompiledPattern::Complex(segments)
    }
    
    /// Fast matching against compiled pattern
    #[inline]
    fn matches(&self, text: &str) -> bool {
        match self {
            CompiledPattern::Exact(pattern) => text == pattern,
            CompiledPattern::Prefix(prefix) => text.starts_with(prefix),
            CompiledPattern::Suffix(suffix) => text.ends_with(suffix),
            CompiledPattern::Contains(substring) => text.contains(substring),
            CompiledPattern::Complex(segments) => Self::match_complex(text, segments),
        }
    }
    
    /// Match complex pattern with segments
    fn match_complex(text: &str, segments: &[PatternSegment]) -> bool {
        let text_chars: Vec<char> = text.chars().collect();
        Self::match_segments(&text_chars, segments, 0, 0)
    }
    
    fn match_segments(text: &[char], segments: &[PatternSegment], ti: usize, si: usize) -> bool {
        // All segments matched
        if si >= segments.len() {
            return ti >= text.len();
        }
        
        match &segments[si] {
            PatternSegment::AnyChars => {
                // Try matching 0 or more characters
                if Self::match_segments(text, segments, ti, si + 1) {
                    return true;
                }
                if ti < text.len() && Self::match_segments(text, segments, ti + 1, si) {
                    return true;
                }
                false
            }
            PatternSegment::AnyChar => {
                // Match exactly one character
                if ti < text.len() {
                    Self::match_segments(text, segments, ti + 1, si + 1)
                } else {
                    false
                }
            }
            PatternSegment::Literal(literal) => {
                // Match literal string
                let chars: Vec<char> = literal.chars().collect();
                if ti + chars.len() > text.len() {
                    return false;
                }
                for (i, &c) in chars.iter().enumerate() {
                    if text[ti + i] != c {
                        return false;
                    }
                }
                Self::match_segments(text, segments, ti + chars.len(), si + 1)
            }
        }
    }
}

pub struct ExprEvaluator {
    #[allow(dead_code)]
    db: Option<Arc<MoteDB>>,  // Optional database reference for MATCH() function
    /// ⚡ Pattern cache: pattern string -> compiled pattern
    /// RwLock for concurrent read access (common case)
    pattern_cache: Arc<RwLock<HashMap<String, CompiledPattern>>>,
    /// 🆕 Store the last AUTO_INCREMENT value inserted (shared with QueryExecutor)
    pub(crate) last_insert_id: Arc<RefCell<Option<i64>>>,
}

impl ExprEvaluator {
    pub fn new() -> Self {
        Self { 
            db: None,
            pattern_cache: Arc::new(RwLock::new(HashMap::new())),
            last_insert_id: Arc::new(RefCell::new(None)),
        }
    }
    
    pub fn with_db(db: Arc<MoteDB>) -> Self {
        Self { 
            db: Some(db),
            pattern_cache: Arc::new(RwLock::new(HashMap::new())),
            last_insert_id: Arc::new(RefCell::new(None)),
        }
    }
    
    /// Evaluate an expression against a row
    pub fn eval(&self, expr: &Expr, row: &SqlRow) -> Result<Value> {
        match expr {
            Expr::Column(name) => {
                // 🔧 FIX: Intelligent column name matching
                // Try 1: Direct match (e.g., "id" or "table.id")
                if let Some(val) = row.get(name) {
                    return Ok(val.clone());
                }
                
                // Try 2: Match with table prefix (e.g., "id" matches "test.id")
                for (key, value) in row.iter() {
                    // Skip metadata columns
                    if key.starts_with("__") {
                        continue;
                    }
                    
                    // Match: key ends with ".{name}"
                    if key.ends_with(&format!(".{}", name)) {
                        return Ok(value.clone());
                    }
                }
                
                // Try 3: Case-insensitive match (for robustness)
                let name_lower = name.to_lowercase();
                for (key, value) in row.iter() {
                    if key.to_lowercase() == name_lower {
                        return Ok(value.clone());
                    }
                }
                
                Err(MoteDBError::ColumnNotFound(name.clone()))
            }
            
            Expr::Literal(val) => Ok(val.clone()),
            
            Expr::BinaryOp { left, op, right } => {
                let left_val = self.eval(left, row)?;
                let right_val = self.eval(right, row)?;
                self.eval_binary_op(op, left_val, right_val)
            }
            
            Expr::UnaryOp { op, expr } => {
                let val = self.eval(expr, row)?;
                self.eval_unary_op(op, val)
            }
            
            Expr::FunctionCall { name, args, distinct } => {
                self.eval_function(name, args, *distinct, row)
            }
            
            Expr::In { expr, list, negated } => {
                let val = self.eval(expr, row)?;
                let mut found = false;
                
                // Handle subquery IN: expr IN (SELECT ...)
                // If list contains a single Subquery, it needs special handling
                // But we can't execute subqueries here - they're handled by executor
                // So we'll just evaluate literal lists here
                
                for item in list {
                    let item_val = self.eval(item, row)?;
                    if val == item_val {
                        found = true;
                        break;
                    }
                }
                Ok(Value::Bool(if *negated { !found } else { found }))
            }
            
            Expr::Between { expr, low, high, negated } => {
                let val = self.eval(expr, row)?;
                let low_val = self.eval(low, row)?;
                let high_val = self.eval(high, row)?;
                
                let in_range = val >= low_val && val <= high_val;
                Ok(Value::Bool(if *negated { !in_range } else { in_range }))
            }
            
            Expr::Like { expr, pattern, negated } => {
                let val = self.eval(expr, row)?;
                let pattern_val = self.eval(pattern, row)?;
                
                let matches = if let (Value::Text(s), Value::Text(p)) = (val, pattern_val) {
                    // ⚡ Use compiled pattern cache for fast matching
                    self.like_match_cached(&s, &p)
                } else {
                    false
                };
                
                Ok(Value::Bool(if *negated { !matches } else { matches }))
            }
            
            Expr::IsNull { expr, negated } => {
                let val = self.eval(expr, row)?;
                let is_null = matches!(val, Value::Null);
                Ok(Value::Bool(if *negated { !is_null } else { is_null }))
            }
            
            Expr::Subquery(_) => {
                // Subqueries are handled at executor level, not here
                Err(MoteDBError::Query("Subquery evaluation must be done by executor".into()))
            }
            
            Expr::Match { .. } => {
                // MATCH...AGAINST is handled at executor level (requires index access)
                Err(MoteDBError::Query("MATCH...AGAINST must be evaluated by executor".into()))
            }
            
            Expr::KnnSearch { .. } => {
                // KNN_SEARCH is handled at executor level (requires index access)
                Err(MoteDBError::Query("KNN_SEARCH must be evaluated by executor".into()))
            }
            
            Expr::KnnDistance { .. } => {
                // KNN_DISTANCE is handled at executor level (requires row vector data)
                Err(MoteDBError::Query("KNN_DISTANCE must be evaluated by executor".into()))
            }
            
            Expr::StWithin { .. } => {
                // ST_WITHIN is handled at executor level (requires spatial index access)
                Err(MoteDBError::Query("ST_WITHIN must be evaluated by executor".into()))
            }
            
            Expr::StDistance { .. } => {
                // ST_DISTANCE is handled at executor level (requires row geometry data)
                Err(MoteDBError::Query("ST_DISTANCE must be evaluated by executor".into()))
            }
            
            Expr::StKnn { .. } => {
                // ST_KNN is handled at executor level (requires spatial index access)
                Err(MoteDBError::Query("ST_KNN must be evaluated by executor".into()))
            }
            
            Expr::WindowFunction { .. } => {
                // Window functions are handled at executor level (require partition data)
                Err(MoteDBError::Query("Window functions must be evaluated by executor".into()))
            }
        }
    }
    
    fn eval_binary_op(&self, op: &BinaryOperator, left: Value, right: Value) -> Result<Value> {
        match op {
            BinaryOperator::Eq => Ok(Value::Bool(left == right)),
            BinaryOperator::Ne => Ok(Value::Bool(left != right)),
            BinaryOperator::Lt => {
                // 🐛 DEBUG: Print comparison for debugging
                let result = left < right;
                // eprintln!("DEBUG Lt: {:?} < {:?} = {}", left, right, result);
                Ok(Value::Bool(result))
            }
            BinaryOperator::Gt => {
                // 🐛 DEBUG: Print comparison for debugging  
                let result = left > right;
                // eprintln!("DEBUG Gt: {:?} > {:?} = {}", left, right, result);
                Ok(Value::Bool(result))
            }
            BinaryOperator::Le => Ok(Value::Bool(left <= right)),
            BinaryOperator::Ge => Ok(Value::Bool(left >= right)),
            
            BinaryOperator::And => {
                let left_bool = self.to_bool(&left)?;
                let right_bool = self.to_bool(&right)?;
                Ok(Value::Bool(left_bool && right_bool))
            }
            
            BinaryOperator::Or => {
                let left_bool = self.to_bool(&left)?;
                let right_bool = self.to_bool(&right)?;
                Ok(Value::Bool(left_bool || right_bool))
            }
            
            BinaryOperator::Add => self.add_values(left, right),
            BinaryOperator::Sub => self.sub_values(left, right),
            BinaryOperator::Mul => self.mul_values(left, right),
            BinaryOperator::Div => self.div_values(left, right),
            BinaryOperator::Mod => self.mod_values(left, right),
            
            // E-SQL Vector Distance Operators
            BinaryOperator::L2Distance => self.l2_distance(left, right),
            BinaryOperator::CosineDistance => self.cosine_distance(left, right),
            BinaryOperator::DotProduct => self.dot_product(left, right),
        }
    }
    
    fn eval_unary_op(&self, op: &UnaryOperator, val: Value) -> Result<Value> {
        match op {
            UnaryOperator::Not => {
                let b = self.to_bool(&val)?;
                Ok(Value::Bool(!b))
            }
            UnaryOperator::Minus => {
                match val {
                    Value::Integer(i) => Ok(Value::Integer(-i)),
                    Value::Float(f) => Ok(Value::Float(-f)),
                    _ => Err(MoteDBError::TypeError("Cannot negate non-numeric value".to_string())),
                }
            }
            UnaryOperator::Plus => Ok(val),
        }
    }
    
    fn eval_function(&self, name: &str, args: &[Expr], _distinct: bool, row: &SqlRow) -> Result<Value> {
        // Note: distinct parameter is only used for aggregate functions like COUNT(DISTINCT)
        // It's ignored for non-aggregate functions
        
        match name.to_lowercase().as_str() {
            // 🆕 LAST_INSERT_ID() - returns the last AUTO_INCREMENT value
            "last_insert_id" => {
                if !args.is_empty() {
                    return Err(MoteDBError::InvalidArgument("last_insert_id() takes no arguments".to_string()));
                }
                let last_id = *self.last_insert_id.borrow();
                Ok(Value::Integer(last_id.unwrap_or(0)))
            }
            
            // Aggregate functions (will be handled by executor for now)
            "count" | "sum" | "avg" | "min" | "max" => {
                Err(MoteDBError::NotImplemented(format!("Aggregate function {} not yet implemented", name)))
            }
            
            // String functions
            "lower" => {
                if args.len() != 1 {
                    return Err(MoteDBError::InvalidArgument("lower() takes 1 argument".to_string()));
                }
                let val = self.eval(&args[0], row)?;
                if let Value::Text(s) = val {
                    Ok(Value::Text(s.to_lowercase()))
                } else {
                    Err(MoteDBError::TypeError("lower() requires text argument".to_string()))
                }
            }
            
            "upper" => {
                if args.len() != 1 {
                    return Err(MoteDBError::InvalidArgument("upper() takes 1 argument".to_string()));
                }
                let val = self.eval(&args[0], row)?;
                if let Value::Text(s) = val {
                    Ok(Value::Text(s.to_uppercase()))
                } else {
                    Err(MoteDBError::TypeError("upper() requires text argument".to_string()))
                }
            }
            
            "length" | "len" => {
                if args.len() != 1 {
                    return Err(MoteDBError::InvalidArgument("length() takes 1 argument".to_string()));
                }
                let val = self.eval(&args[0], row)?;
                if let Value::Text(s) = val {
                    Ok(Value::Integer(s.len() as i64))
                } else {
                    Err(MoteDBError::TypeError("length() requires text argument".to_string()))
                }
            }
            
            // 🆕 String manipulation functions
            "concat" => {
                // CONCAT(str1, str2, ...) - concatenate strings
                // ✅ 优化:预估容量,减少重新分配
                let estimated_capacity = args.len() * 20; // 每个参数估计 20 字节
                let mut result = String::with_capacity(estimated_capacity);
                for arg in args {
                    let val = self.eval(arg, row)?;
                    // ✅ 优化:直接 push_str,避免中间 String 分配
                    match val {
                        Value::Text(s) => result.push_str(&s),
                        Value::Integer(i) => { use std::fmt::Write; let _ = write!(result, "{}", i); }
                        Value::Float(f) => { use std::fmt::Write; let _ = write!(result, "{}", f); }
                        Value::Bool(b) => result.push_str(if b { "true" } else { "false" }),
                        Value::Null => result.push_str("NULL"),
                        _ => { use std::fmt::Write; let _ = write!(result, "{:?}", val); }
                    };
                }
                Ok(Value::Text(result))
            }
            
            "substr" | "substring" => {
                // SUBSTR(text, start [, length])
                if args.len() < 2 || args.len() > 3 {
                    return Err(MoteDBError::InvalidArgument(
                        "substr() takes 2 or 3 arguments (text, start, [length])".to_string()
                    ));
                }
                let text = match self.eval(&args[0], row)? {
                    Value::Text(s) => s,
                    _ => return Err(MoteDBError::TypeError("substr() first argument must be text".to_string())),
                };
                let start = match self.eval(&args[1], row)? {
                    Value::Integer(i) => i.max(0) as usize, // 1-indexed in SQL
                    _ => return Err(MoteDBError::TypeError("substr() start must be integer".to_string())),
                };
                
                // SQL uses 1-based indexing
                let start_idx = if start > 0 { start - 1 } else { 0 };
                
                let result = if args.len() == 3 {
                    let length = match self.eval(&args[2], row)? {
                        Value::Integer(i) => i.max(0) as usize,
                        _ => return Err(MoteDBError::TypeError("substr() length must be integer".to_string())),
                    };
                    text.chars().skip(start_idx).take(length).collect()
                } else {
                    text.chars().skip(start_idx).collect()
                };
                Ok(Value::Text(result))
            }
            
            "trim" => {
                // TRIM(text) - remove leading and trailing whitespace
                if args.len() != 1 {
                    return Err(MoteDBError::InvalidArgument("trim() takes 1 argument".to_string()));
                }
                let text = match self.eval(&args[0], row)? {
                    Value::Text(s) => s,
                    _ => return Err(MoteDBError::TypeError("trim() requires text argument".to_string())),
                };
                // ✅ 优化:如果没有前后空格,直接返回原字符串
                if text.trim() == text.as_str() {
                    return Ok(Value::Text(text));
                }
                // 否则才创建新字符串
                Ok(Value::Text(text.trim().to_string()))
            }
            
            "ltrim" => {
                if args.len() != 1 {
                    return Err(MoteDBError::InvalidArgument("ltrim() takes 1 argument".to_string()));
                }
                let text = match self.eval(&args[0], row)? {
                    Value::Text(s) => s,
                    _ => return Err(MoteDBError::TypeError("ltrim() requires text argument".to_string())),
                };
                // ✅ 优化:如果没有前导空格,直接返回
                if text.trim_start() == text.as_str() {
                    return Ok(Value::Text(text));
                }
                Ok(Value::Text(text.trim_start().to_string()))
            }
            
            "rtrim" => {
                if args.len() != 1 {
                    return Err(MoteDBError::InvalidArgument("rtrim() takes 1 argument".to_string()));
                }
                let text = match self.eval(&args[0], row)? {
                    Value::Text(s) => s,
                    _ => return Err(MoteDBError::TypeError("rtrim() requires text argument".to_string())),
                };
                // ✅ 优化:如果没有尾随空格,直接返回
                if text.trim_end() == text.as_str() {
                    return Ok(Value::Text(text));
                }
                Ok(Value::Text(text.trim_end().to_string()))
            }
            
            "replace" => {
                // REPLACE(text, from, to) - replace all occurrences
                if args.len() != 3 {
                    return Err(MoteDBError::InvalidArgument(
                        "replace() takes 3 arguments (text, from, to)".to_string()
                    ));
                }
                let text = match self.eval(&args[0], row)? {
                    Value::Text(s) => s,
                    _ => return Err(MoteDBError::TypeError("replace() first argument must be text".to_string())),
                };
                let from = match self.eval(&args[1], row)? {
                    Value::Text(s) => s,
                    _ => return Err(MoteDBError::TypeError("replace() second argument must be text".to_string())),
                };
                let to = match self.eval(&args[2], row)? {
                    Value::Text(s) => s,
                    _ => return Err(MoteDBError::TypeError("replace() third argument must be text".to_string())),
                };
                Ok(Value::Text(text.replace(&from, &to)))
            }
            
            "reverse" => {
                if args.len() != 1 {
                    return Err(MoteDBError::InvalidArgument("reverse() takes 1 argument".to_string()));
                }
                let text = match self.eval(&args[0], row)? {
                    Value::Text(s) => s,
                    _ => return Err(MoteDBError::TypeError("reverse() requires text argument".to_string())),
                };
                Ok(Value::Text(text.chars().rev().collect()))
            }
            
            "leftstr" | "str_left" => {
                // LEFTSTR(text, length) - get leftmost N characters
                // Renamed to avoid SQL keyword conflict with LEFT JOIN
                if args.len() != 2 {
                    return Err(MoteDBError::InvalidArgument("leftstr() takes 2 arguments".to_string()));
                }
                let text = match self.eval(&args[0], row)? {
                    Value::Text(s) => s,
                    _ => return Err(MoteDBError::TypeError("leftstr() first argument must be text".to_string())),
                };
                let length = match self.eval(&args[1], row)? {
                    Value::Integer(i) => i.max(0) as usize,
                    _ => return Err(MoteDBError::TypeError("leftstr() second argument must be integer".to_string())),
                };
                Ok(Value::Text(text.chars().take(length).collect()))
            }
            
            "rightstr" | "str_right" => {
                // RIGHTSTR(text, length) - get rightmost N characters
                // Renamed to avoid SQL keyword conflict with RIGHT JOIN
                if args.len() != 2 {
                    return Err(MoteDBError::InvalidArgument("rightstr() takes 2 arguments".to_string()));
                }
                let text = match self.eval(&args[0], row)? {
                    Value::Text(s) => s,
                    _ => return Err(MoteDBError::TypeError("rightstr() first argument must be text".to_string())),
                };
                let length = match self.eval(&args[1], row)? {
                    Value::Integer(i) => i.max(0) as usize,
                    _ => return Err(MoteDBError::TypeError("rightstr() second argument must be integer".to_string())),
                };
                let char_vec: Vec<char> = text.chars().collect();
                let start_idx = char_vec.len().saturating_sub(length);
                Ok(Value::Text(char_vec[start_idx..].iter().collect()))
            }
            
            "repeat" => {
                // REPEAT(text, n) - repeat text N times
                if args.len() != 2 {
                    return Err(MoteDBError::InvalidArgument("repeat() takes 2 arguments".to_string()));
                }
                let text = match self.eval(&args[0], row)? {
                    Value::Text(s) => s,
                    _ => return Err(MoteDBError::TypeError("repeat() first argument must be text".to_string())),
                };
                let count = match self.eval(&args[1], row)? {
                    Value::Integer(i) => i.max(0) as usize,
                    _ => return Err(MoteDBError::TypeError("repeat() second argument must be integer".to_string())),
                };
                Ok(Value::Text(text.repeat(count)))
            }
            
            // Math functions
            "abs" => {
                if args.len() != 1 {
                    return Err(MoteDBError::InvalidArgument("abs() takes 1 argument".to_string()));
                }
                let val = self.eval(&args[0], row)?;
                match val {
                    Value::Integer(i) => Ok(Value::Integer(i.abs())),
                    Value::Float(f) => Ok(Value::Float(f.abs())),
                    _ => Err(MoteDBError::TypeError("abs() requires numeric argument".to_string())),
                }
            }
            
            "round" => {
                // ROUND(number [, decimals])
                if args.is_empty() || args.len() > 2 {
                    return Err(MoteDBError::InvalidArgument("round() takes 1 or 2 arguments".to_string()));
                }
                let val = self.eval(&args[0], row)?;
                let decimals = if args.len() == 2 {
                    match self.eval(&args[1], row)? {
                        Value::Integer(i) => i as i32,
                        _ => return Err(MoteDBError::TypeError("round() decimals must be integer".to_string())),
                    }
                } else {
                    0
                };
                
                match val {
                    Value::Float(f) => {
                        let multiplier = 10_f64.powi(decimals);
                        Ok(Value::Float((f * multiplier).round() / multiplier))
                    }
                    Value::Integer(i) => Ok(Value::Integer(i)),
                    _ => Err(MoteDBError::TypeError("round() requires numeric argument".to_string())),
                }
            }
            
            "floor" => {
                if args.len() != 1 {
                    return Err(MoteDBError::InvalidArgument("floor() takes 1 argument".to_string()));
                }
                let val = self.eval(&args[0], row)?;
                match val {
                    Value::Float(f) => Ok(Value::Integer(f.floor() as i64)),
                    Value::Integer(i) => Ok(Value::Integer(i)),
                    _ => Err(MoteDBError::TypeError("floor() requires numeric argument".to_string())),
                }
            }
            
            "ceil" | "ceiling" => {
                if args.len() != 1 {
                    return Err(MoteDBError::InvalidArgument("ceil() takes 1 argument".to_string()));
                }
                let val = self.eval(&args[0], row)?;
                match val {
                    Value::Float(f) => Ok(Value::Integer(f.ceil() as i64)),
                    Value::Integer(i) => Ok(Value::Integer(i)),
                    _ => Err(MoteDBError::TypeError("ceil() requires numeric argument".to_string())),
                }
            }
            
            "power" | "pow" => {
                // POWER(base, exponent)
                if args.len() != 2 {
                    return Err(MoteDBError::InvalidArgument("power() takes 2 arguments".to_string()));
                }
                let base = self.to_float(&self.eval(&args[0], row)?)?;
                let exp = self.to_float(&self.eval(&args[1], row)?)?;
                Ok(Value::Float(base.powf(exp)))
            }
            
            "sqrt" => {
                if args.len() != 1 {
                    return Err(MoteDBError::InvalidArgument("sqrt() takes 1 argument".to_string()));
                }
                let val = self.to_float(&self.eval(&args[0], row)?)?;
                if val < 0.0 {
                    return Err(MoteDBError::InvalidArgument("sqrt() of negative number".to_string()));
                }
                Ok(Value::Float(val.sqrt()))
            }
            
            "exp" => {
                if args.len() != 1 {
                    return Err(MoteDBError::InvalidArgument("exp() takes 1 argument".to_string()));
                }
                let val = self.to_float(&self.eval(&args[0], row)?)?;
                Ok(Value::Float(val.exp()))
            }
            
            "ln" | "log" => {
                if args.len() != 1 {
                    return Err(MoteDBError::InvalidArgument("ln() takes 1 argument".to_string()));
                }
                let val = self.to_float(&self.eval(&args[0], row)?)?;
                if val <= 0.0 {
                    return Err(MoteDBError::InvalidArgument("ln() of non-positive number".to_string()));
                }
                Ok(Value::Float(val.ln()))
            }
            
            "log10" => {
                if args.len() != 1 {
                    return Err(MoteDBError::InvalidArgument("log10() takes 1 argument".to_string()));
                }
                let val = self.to_float(&self.eval(&args[0], row)?)?;
                if val <= 0.0 {
                    return Err(MoteDBError::InvalidArgument("log10() of non-positive number".to_string()));
                }
                Ok(Value::Float(val.log10()))
            }
            
            "mod" => {
                // MOD(dividend, divisor)
                if args.len() != 2 {
                    return Err(MoteDBError::InvalidArgument("mod() takes 2 arguments".to_string()));
                }
                let dividend = self.eval(&args[0], row)?;
                let divisor = self.eval(&args[1], row)?;
                match (&dividend, &divisor) {
                    (Value::Integer(a), Value::Integer(b)) => {
                        if *b == 0 {
                            return Err(MoteDBError::InvalidArgument("Division by zero in mod()".to_string()));
                        }
                        Ok(Value::Integer(a % b))
                    }
                    _ => {
                        let a = self.to_float(&dividend)?;
                        let b = self.to_float(&divisor)?;
                        if b == 0.0 {
                            return Err(MoteDBError::InvalidArgument("Division by zero in mod()".to_string()));
                        }
                        Ok(Value::Float(a % b))
                    }
                }
            }
            
            "sign" => {
                if args.len() != 1 {
                    return Err(MoteDBError::InvalidArgument("sign() takes 1 argument".to_string()));
                }
                let val = self.eval(&args[0], row)?;
                match val {
                    Value::Integer(i) => Ok(Value::Integer(i.signum())),
                    Value::Float(f) => Ok(Value::Integer(if f > 0.0 { 1 } else if f < 0.0 { -1 } else { 0 })),
                    _ => Err(MoteDBError::TypeError("sign() requires numeric argument".to_string())),
                }
            }
            
            "random" | "rand" => {
                if !args.is_empty() {
                    return Err(MoteDBError::InvalidArgument("random() takes no arguments".to_string()));
                }
                use std::collections::hash_map::RandomState;
                use std::hash::{BuildHasher, Hasher};
                let s = RandomState::new();
                let mut hasher = s.build_hasher();
                hasher.write_u64(std::time::SystemTime::now()
                    .duration_since(std::time::UNIX_EPOCH)
                    .unwrap_or_else(|_| std::time::Duration::from_secs(0))
                    .as_nanos() as u64);
                let hash = hasher.finish();
                // Convert to [0, 1) range
                Ok(Value::Float((hash as f64) / (u64::MAX as f64)))
            }
            
            // 🆕 Conditional functions
            "if" => {
                // IF(condition, true_value, false_value)
                if args.len() != 3 {
                    return Err(MoteDBError::InvalidArgument(
                        "if() takes 3 arguments (condition, true_value, false_value)".to_string()
                    ));
                }
                let condition = self.eval(&args[0], row)?;
                if self.to_bool(&condition)? {
                    self.eval(&args[1], row)
                } else {
                    self.eval(&args[2], row)
                }
            }
            
            "coalesce" => {
                // COALESCE(val1, val2, ...) - return first non-NULL value
                if args.is_empty() {
                    return Err(MoteDBError::InvalidArgument(
                        "coalesce() requires at least 1 argument".to_string()
                    ));
                }
                for arg in args {
                    let val = self.eval(arg, row)?;
                    if !matches!(val, Value::Null) {
                        return Ok(val);
                    }
                }
                Ok(Value::Null)
            }
            
            "ifnull" | "nvl" => {
                // IFNULL(value, default) - return default if value is NULL
                if args.len() != 2 {
                    return Err(MoteDBError::InvalidArgument(
                        "ifnull() takes 2 arguments (value, default)".to_string()
                    ));
                }
                let val = self.eval(&args[0], row)?;
                if matches!(val, Value::Null) {
                    self.eval(&args[1], row)
                } else {
                    Ok(val)
                }
            }
            
            "nullif" => {
                // NULLIF(val1, val2) - return NULL if val1 == val2, otherwise val1
                if args.len() != 2 {
                    return Err(MoteDBError::InvalidArgument(
                        "nullif() takes 2 arguments".to_string()
                    ));
                }
                let val1 = self.eval(&args[0], row)?;
                let val2 = self.eval(&args[1], row)?;
                if val1 == val2 {
                    Ok(Value::Null)
                } else {
                    Ok(val1)
                }
            }
            
            // E-SQL Spatial Functions
            "st_distance" => {
                if args.len() != 2 {
                    return Err(MoteDBError::InvalidArgument("ST_Distance() takes 2 arguments".to_string()));
                }
                let p1 = self.eval(&args[0], row)?;
                let p2 = self.eval(&args[1], row)?;
                self.st_distance(p1, p2)
            }
            
            "within_radius" => {
                if args.len() != 3 {
                    return Err(MoteDBError::InvalidArgument("WITHIN_RADIUS() takes 3 arguments (point, center, radius)".to_string()));
                }
                let point = self.eval(&args[0], row)?;
                let center = self.eval(&args[1], row)?;
                let radius = self.eval(&args[2], row)?;
                self.within_radius(point, center, radius)
            }
            
            "st_ontopof" => {
                if args.len() != 2 {
                    return Err(MoteDBError::InvalidArgument("ST_OnTopOf() takes 2 arguments".to_string()));
                }
                let p1 = self.eval(&args[0], row)?;
                let p2 = self.eval(&args[1], row)?;
                self.st_ontopof(p1, p2)
            }
            
            // E-SQL Text Functions
            "match" => {
                // MATCH(column, query_text)
                if args.len() != 2 {
                    return Err(MoteDBError::InvalidArgument(
                        "MATCH() takes 2 arguments (column, query_text)".to_string()
                    ));
                }
                
                // Get column name (must be a column reference, not expression)
                let column_name = match &args[0] {
                    Expr::Column(name) => name.clone(),
                    _ => return Err(MoteDBError::InvalidArgument(
                        "MATCH() first argument must be a column name".to_string()
                    )),
                };
                
                // Get query text
                let query_text = match self.eval(&args[1], row)? {
                    Value::Text(s) => s,
                    _ => return Err(MoteDBError::TypeError(
                        "MATCH() query must be text".to_string()
                    )),
                };
                
                // Get column value
                let column_value = row.get(&column_name)
                    .ok_or_else(|| MoteDBError::ColumnNotFound(column_name.clone()))?;
                
                // Simple text matching: check if all query terms appear in column
                // (This is a simplified version; real implementation should use BM25 from text_fts index)
                let matches = match column_value {
                    Value::Text(text) => {
                        let text_lower = text.to_lowercase();
                        let query_lower = query_text.to_lowercase();
                        
                        // Split query into terms
                        let terms: Vec<&str> = query_lower.split_whitespace().collect();
                        
                        // Check if all terms appear in text
                        terms.iter().all(|term| text_lower.contains(term))
                    }
                    _ => false,
                };
                
                Ok(Value::Bool(matches))
            }
            
            "fts_match" | "fts_search" => {
                Err(MoteDBError::NotImplemented("Full-text search functions not yet implemented".to_string()))
            }
            
            // Timestamp functions
            "now" => {
                // NOW() - current timestamp
                if !args.is_empty() {
                    return Err(MoteDBError::InvalidArgument("NOW() takes no arguments".to_string()));
                }
                use crate::types::Timestamp;
                let ts = Timestamp::now();
                Ok(Value::Timestamp(ts))
            }
            
            "timestamp_micros" => {
                // TIMESTAMP_MICROS(value) - convert integer to timestamp
                if args.len() != 1 {
                    return Err(MoteDBError::InvalidArgument("TIMESTAMP_MICROS() takes 1 argument".to_string()));
                }
                let val = self.eval(&args[0], row)?;
                match val {
                    Value::Integer(micros) => {
                        use crate::types::Timestamp;
                        Ok(Value::Timestamp(Timestamp::from_micros(micros)))
                    }
                    _ => Err(MoteDBError::TypeError("TIMESTAMP_MICROS() requires integer argument".to_string())),
                }
            }
            
            "timestamp_millis" => {
                // TIMESTAMP_MILLIS(value) - convert integer milliseconds to timestamp
                if args.len() != 1 {
                    return Err(MoteDBError::InvalidArgument("TIMESTAMP_MILLIS() takes 1 argument".to_string()));
                }
                let val = self.eval(&args[0], row)?;
                match val {
                    Value::Integer(millis) => {
                        use crate::types::Timestamp;
                        Ok(Value::Timestamp(Timestamp::from_millis(millis)))
                    }
                    _ => Err(MoteDBError::TypeError("TIMESTAMP_MILLIS() requires integer argument".to_string())),
                }
            }
            
            "timestamp_secs" => {
                // TIMESTAMP_SECS(value) - convert integer seconds to timestamp
                if args.len() != 1 {
                    return Err(MoteDBError::InvalidArgument("TIMESTAMP_SECS() takes 1 argument".to_string()));
                }
                let val = self.eval(&args[0], row)?;
                match val {
                    Value::Integer(secs) => {
                        use crate::types::Timestamp;
                        Ok(Value::Timestamp(Timestamp::from_secs(secs)))
                    }
                    _ => Err(MoteDBError::TypeError("TIMESTAMP_SECS() requires integer argument".to_string())),
                }
            }
            
            "to_micros" => {
                // TO_MICROS(timestamp) - extract microseconds from timestamp
                if args.len() != 1 {
                    return Err(MoteDBError::InvalidArgument("TO_MICROS() takes 1 argument".to_string()));
                }
                let val = self.eval(&args[0], row)?;
                match val {
                    Value::Timestamp(ts) => Ok(Value::Integer(ts.as_micros())),
                    _ => Err(MoteDBError::TypeError("TO_MICROS() requires timestamp argument".to_string())),
                }
            }
            
            // 🆕 P1 Date/Time extraction functions
            "year" => {
                // YEAR(timestamp) - extract year
                if args.len() != 1 {
                    return Err(MoteDBError::InvalidArgument("YEAR() takes 1 argument".to_string()));
                }
                let val = self.eval(&args[0], row)?;
                match val {
                    Value::Timestamp(ts) => {
                        // Convert microseconds to seconds for chrono-like calculation
                        let secs = ts.as_micros() / 1_000_000;
                        // Days since epoch: divide by seconds per day (86400)
                        let days = secs / 86400;
                        // Approximate year: 1970 + days/365.25
                        let year = 1970 + (days as f64 / 365.25) as i64;
                        Ok(Value::Integer(year))
                    }
                    _ => Err(MoteDBError::TypeError("YEAR() requires timestamp argument".to_string())),
                }
            }
            
            "month" => {
                // MONTH(timestamp) - extract month (1-12)
                if args.len() != 1 {
                    return Err(MoteDBError::InvalidArgument("MONTH() takes 1 argument".to_string()));
                }
                let val = self.eval(&args[0], row)?;
                match val {
                    Value::Timestamp(ts) => {
                        let secs = ts.as_micros() / 1_000_000;
                        let days = secs / 86400;
                        // Simplified month calculation (approximate)
                        let days_in_year = days % 365;
                        let month = ((days_in_year / 30) + 1).min(12);
                        Ok(Value::Integer(month))
                    }
                    _ => Err(MoteDBError::TypeError("MONTH() requires timestamp argument".to_string())),
                }
            }
            
            "day" | "day_of_month" => {
                // DAY(timestamp) - extract day of month (1-31)
                if args.len() != 1 {
                    return Err(MoteDBError::InvalidArgument("DAY() takes 1 argument".to_string()));
                }
                let val = self.eval(&args[0], row)?;
                match val {
                    Value::Timestamp(ts) => {
                        let secs = ts.as_micros() / 1_000_000;
                        let days = secs / 86400;
                        // Day within month (approximation)
                        let day = (days % 30) + 1;
                        Ok(Value::Integer(day))
                    }
                    _ => Err(MoteDBError::TypeError("DAY() requires timestamp argument".to_string())),
                }
            }
            
            "hour" => {
                // HOUR(timestamp) - extract hour (0-23)
                if args.len() != 1 {
                    return Err(MoteDBError::InvalidArgument("HOUR() takes 1 argument".to_string()));
                }
                let val = self.eval(&args[0], row)?;
                match val {
                    Value::Timestamp(ts) => {
                        let secs = ts.as_micros() / 1_000_000;
                        let hour = (secs % 86400) / 3600;
                        Ok(Value::Integer(hour))
                    }
                    _ => Err(MoteDBError::TypeError("HOUR() requires timestamp argument".to_string())),
                }
            }
            
            "minute" => {
                // MINUTE(timestamp) - extract minute (0-59)
                if args.len() != 1 {
                    return Err(MoteDBError::InvalidArgument("MINUTE() takes 1 argument".to_string()));
                }
                let val = self.eval(&args[0], row)?;
                match val {
                    Value::Timestamp(ts) => {
                        let secs = ts.as_micros() / 1_000_000;
                        let minute = (secs % 3600) / 60;
                        Ok(Value::Integer(minute))
                    }
                    _ => Err(MoteDBError::TypeError("MINUTE() requires timestamp argument".to_string())),
                }
            }
            
            "second" => {
                // SECOND(timestamp) - extract second (0-59)
                if args.len() != 1 {
                    return Err(MoteDBError::InvalidArgument("SECOND() takes 1 argument".to_string()));
                }
                let val = self.eval(&args[0], row)?;
                match val {
                    Value::Timestamp(ts) => {
                        let secs = ts.as_micros() / 1_000_000;
                        let second = secs % 60;
                        Ok(Value::Integer(second))
                    }
                    _ => Err(MoteDBError::TypeError("SECOND() requires timestamp argument".to_string())),
                }
            }
            
            "date_add" | "dateadd" => {
                // DATE_ADD(timestamp, interval_seconds) - add seconds to timestamp
                if args.len() != 2 {
                    return Err(MoteDBError::InvalidArgument(
                        "DATE_ADD() takes 2 arguments (timestamp, seconds)".to_string()
                    ));
                }
                let ts = match self.eval(&args[0], row)? {
                    Value::Timestamp(ts) => ts,
                    _ => return Err(MoteDBError::TypeError("DATE_ADD() first argument must be timestamp".to_string())),
                };
                let seconds = match self.eval(&args[1], row)? {
                    Value::Integer(i) => i,
                    _ => return Err(MoteDBError::TypeError("DATE_ADD() second argument must be integer".to_string())),
                };
                
                use crate::types::Timestamp;
                let new_micros = ts.as_micros() + (seconds * 1_000_000);
                Ok(Value::Timestamp(Timestamp::from_micros(new_micros)))
            }
            
            "date_diff" | "datediff" => {
                // DATE_DIFF(timestamp1, timestamp2) - difference in seconds
                if args.len() != 2 {
                    return Err(MoteDBError::InvalidArgument(
                        "DATE_DIFF() takes 2 arguments (timestamp1, timestamp2)".to_string()
                    ));
                }
                let ts1 = match self.eval(&args[0], row)? {
                    Value::Timestamp(ts) => ts,
                    _ => return Err(MoteDBError::TypeError("DATE_DIFF() first argument must be timestamp".to_string())),
                };
                let ts2 = match self.eval(&args[1], row)? {
                    Value::Timestamp(ts) => ts,
                    _ => return Err(MoteDBError::TypeError("DATE_DIFF() second argument must be timestamp".to_string())),
                };
                
                let diff_micros = ts1.as_micros() - ts2.as_micros();
                let diff_seconds = diff_micros / 1_000_000;
                Ok(Value::Integer(diff_seconds))
            }
            
            "day_of_week" | "dow" => {
                // DAY_OF_WEEK(timestamp) - extract day of week (1=Monday, 7=Sunday)
                if args.len() != 1 {
                    return Err(MoteDBError::InvalidArgument("DAY_OF_WEEK() takes 1 argument".to_string()));
                }
                let val = self.eval(&args[0], row)?;
                match val {
                    Value::Timestamp(ts) => {
                        let secs = ts.as_micros() / 1_000_000;
                        let days = secs / 86400;
                        // Unix epoch (1970-01-01) was Thursday (day 4)
                        // Calculate day of week: (days + 4) % 7, then map to 1-7
                        let dow = ((days + 3) % 7) + 1; // +3 because epoch was Thursday (4-1=3)
                        Ok(Value::Integer(dow))
                    }
                    _ => Err(MoteDBError::TypeError("DAY_OF_WEEK() requires timestamp argument".to_string())),
                }
            }
            
            // 🆕 Type conversion function
            "cast" => {
                // CAST(value AS type) - NOTE: In SQL this is special syntax, but we handle as function
                // Usage: CAST(column, 'INTEGER') or CAST(column, 'TEXT')
                if args.len() != 2 {
                    return Err(MoteDBError::InvalidArgument(
                        "CAST() takes 2 arguments (value, target_type)".to_string()
                    ));
                }
                let val = self.eval(&args[0], row)?;
                let target_type = match self.eval(&args[1], row)? {
                    Value::Text(s) => s.to_uppercase(),
                    _ => return Err(MoteDBError::TypeError("CAST() target type must be text".to_string())),
                };
                
                match target_type.as_str() {
                    "INTEGER" | "INT" => {
                        match val {
                            Value::Integer(i) => Ok(Value::Integer(i)),
                            Value::Float(f) => Ok(Value::Integer(f as i64)),
                            Value::Text(s) => s.parse::<i64>()
                                .map(Value::Integer)
                                .map_err(|_| MoteDBError::TypeError("Cannot parse integer".to_string())),
                            Value::Bool(b) => Ok(Value::Integer(if b { 1 } else { 0 })),
                            Value::Timestamp(ts) => Ok(Value::Integer(ts.as_micros())),
                            _ => Err(MoteDBError::TypeError(format!("Cannot cast {:?} to INTEGER", val))),
                        }
                    }
                    "FLOAT" | "REAL" | "DOUBLE" => {
                        match val {
                            Value::Float(f) => Ok(Value::Float(f)),
                            Value::Integer(i) => Ok(Value::Float(i as f64)),
                            Value::Text(s) => s.parse::<f64>()
                                .map(Value::Float)
                                .map_err(|_| MoteDBError::TypeError("Cannot parse float".to_string())),
                            _ => Err(MoteDBError::TypeError(format!("Cannot cast {:?} to FLOAT", val))),
                        }
                    }
                    "TEXT" | "VARCHAR" | "STRING" => {
                        let text = match val {
                            Value::Text(s) => s,
                            Value::Integer(i) => i.to_string(),
                            Value::Float(f) => f.to_string(),
                            Value::Bool(b) => b.to_string(),
                            Value::Null => "NULL".to_string(),
                            _ => format!("{:?}", val),
                        };
                        Ok(Value::Text(text))
                    }
                    "BOOLEAN" | "BOOL" => {
                        let b = self.to_bool(&val)?;
                        Ok(Value::Bool(b))
                    }
                    "TIMESTAMP" => {
                        match val {
                            Value::Timestamp(ts) => Ok(Value::Timestamp(ts)),
                            Value::Integer(micros) => {
                                use crate::types::Timestamp;
                                Ok(Value::Timestamp(Timestamp::from_micros(micros)))
                            }
                            _ => Err(MoteDBError::TypeError(format!("Cannot cast {:?} to TIMESTAMP", val))),
                        }
                    }
                    _ => Err(MoteDBError::TypeError(format!("Unknown target type: {}", target_type))),
                }
            }
            
            _ => Err(MoteDBError::UnknownFunction(name.to_string())),
        }
    }
    
    // Helper functions
    
    fn to_bool(&self, val: &Value) -> Result<bool> {
        match val {
            Value::Bool(b) => Ok(*b),
            Value::Integer(i) => Ok(*i != 0),
            Value::Float(f) => Ok(*f != 0.0 && !f.is_nan()),  // 🔧 Support Float: non-zero and non-NaN is true
            Value::Null => Ok(false),
            _ => Err(MoteDBError::TypeError("Cannot convert to boolean".to_string())),
        }
    }
    
    fn to_float(&self, val: &Value) -> Result<f64> {
        match val {
            Value::Float(f) => Ok(*f),
            Value::Integer(i) => Ok(*i as f64),
            _ => Err(MoteDBError::TypeError("Cannot convert to float".to_string())),
        }
    }
    
    fn add_values(&self, left: Value, right: Value) -> Result<Value> {
        match (left, right) {
            (Value::Integer(l), Value::Integer(r)) => Ok(Value::Integer(l + r)),
            (Value::Float(l), Value::Float(r)) => Ok(Value::Float(l + r)),
            (Value::Integer(l), Value::Float(r)) => Ok(Value::Float(l as f64 + r)),
            (Value::Float(l), Value::Integer(r)) => Ok(Value::Float(l + r as f64)),
            (Value::Text(l), Value::Text(r)) => Ok(Value::Text(format!("{}{}", l, r))),
            _ => Err(MoteDBError::TypeError("Cannot add these types".to_string())),
        }
    }
    
    fn sub_values(&self, left: Value, right: Value) -> Result<Value> {
        match (left, right) {
            (Value::Integer(l), Value::Integer(r)) => Ok(Value::Integer(l - r)),
            (Value::Float(l), Value::Float(r)) => Ok(Value::Float(l - r)),
            (Value::Integer(l), Value::Float(r)) => Ok(Value::Float(l as f64 - r)),
            (Value::Float(l), Value::Integer(r)) => Ok(Value::Float(l - r as f64)),
            _ => Err(MoteDBError::TypeError("Cannot subtract these types".to_string())),
        }
    }
    
    fn mul_values(&self, left: Value, right: Value) -> Result<Value> {
        match (left, right) {
            (Value::Integer(l), Value::Integer(r)) => Ok(Value::Integer(l * r)),
            (Value::Float(l), Value::Float(r)) => Ok(Value::Float(l * r)),
            (Value::Integer(l), Value::Float(r)) => Ok(Value::Float(l as f64 * r)),
            (Value::Float(l), Value::Integer(r)) => Ok(Value::Float(l * r as f64)),
            _ => Err(MoteDBError::TypeError("Cannot multiply these types".to_string())),
        }
    }
    
    fn div_values(&self, left: Value, right: Value) -> Result<Value> {
        match (left, right) {
            (Value::Integer(l), Value::Integer(r)) => {
                if r == 0 {
                    return Err(MoteDBError::DivisionByZero);
                }
                Ok(Value::Integer(l / r))
            }
            (Value::Float(l), Value::Float(r)) => {
                if r == 0.0 {
                    return Err(MoteDBError::DivisionByZero);
                }
                Ok(Value::Float(l / r))
            }
            (Value::Integer(l), Value::Float(r)) => {
                if r == 0.0 {
                    return Err(MoteDBError::DivisionByZero);
                }
                Ok(Value::Float(l as f64 / r))
            }
            (Value::Float(l), Value::Integer(r)) => {
                if r == 0 {
                    return Err(MoteDBError::DivisionByZero);
                }
                Ok(Value::Float(l / r as f64))
            }
            _ => Err(MoteDBError::TypeError("Cannot divide these types".to_string())),
        }
    }
    
    fn mod_values(&self, left: Value, right: Value) -> Result<Value> {
        match (left, right) {
            (Value::Integer(l), Value::Integer(r)) => {
                if r == 0 {
                    return Err(MoteDBError::DivisionByZero);
                }
                Ok(Value::Integer(l % r))
            }
            _ => Err(MoteDBError::TypeError("Modulo only works on integers".to_string())),
        }
    }
    
    /// ⚡ LIKE pattern matching with compilation cache (5-10x faster)
    /// Caches compiled patterns for repeated use
    #[inline]
    fn like_match_cached(&self, text: &str, pattern: &str) -> bool {
        // Fast path: check read-only cache first
        {
            if let Ok(cache) = self.pattern_cache.read() {
                if let Some(compiled) = cache.get(pattern) {
                    return compiled.matches(text);
                }
            }
        }
        
        // Slow path: compile and cache pattern
        let compiled = CompiledPattern::compile(pattern);
        let result = compiled.matches(text);
        
        // Insert into cache (write lock)
        {
            if let Ok(mut cache) = self.pattern_cache.write() {
                // Limit cache size to prevent memory bloat
                if cache.len() < 1000 {
                    cache.insert(pattern.to_string(), compiled);
                }
            }
        }
        
        result
    }
    
    /// Simple LIKE pattern matching (fallback for non-cached cases)
    /// Supports % (any characters) and _ (single character)
    #[allow(dead_code)]
    fn like_match(&self, text: &str, pattern: &str) -> bool {
        let text_chars: Vec<char> = text.chars().collect();
        let pattern_chars: Vec<char> = pattern.chars().collect();
        
        self.like_match_recursive(&text_chars, &pattern_chars, 0, 0)
    }
    
    fn like_match_recursive(&self, text: &[char], pattern: &[char], ti: usize, pi: usize) -> bool {
        // End of pattern
        if pi >= pattern.len() {
            return ti >= text.len();
        }
        
        // End of text
        if ti >= text.len() {
            // Pattern must be all % to match
            return pattern[pi..].iter().all(|&c| c == '%');
        }
        
        match pattern[pi] {
            '%' => {
                // Try matching 0 or more characters
                self.like_match_recursive(text, pattern, ti, pi + 1) ||
                self.like_match_recursive(text, pattern, ti + 1, pi)
            }
            '_' => {
                // Match exactly one character
                self.like_match_recursive(text, pattern, ti + 1, pi + 1)
            }
            c => {
                // Exact match
                if text[ti] == c {
                    self.like_match_recursive(text, pattern, ti + 1, pi + 1)
                } else {
                    false
                }
            }
        }
    }
    
    // E-SQL Vector Distance Functions
    
    /// L2 Distance (Euclidean): <->
    fn l2_distance(&self, left: Value, right: Value) -> Result<Value> {
        let (v1, v2) = self.extract_vectors(left, right)?;
        
        if v1.len() != v2.len() {
            return Err(MoteDBError::TypeError(format!(
                "Vector dimension mismatch: {} vs {}", v1.len(), v2.len()
            )));
        }
        
        let dist: f32 = v1.iter().zip(v2.iter())
            .map(|(a, b)| (a - b).powi(2))
            .sum::<f32>()
            .sqrt();
        
        Ok(Value::Float(dist as f64))
    }
    
    /// Cosine Distance: <=>
    /// Returns 1 - cosine_similarity, range [0, 2]
    fn cosine_distance(&self, left: Value, right: Value) -> Result<Value> {
        let (v1, v2) = self.extract_vectors(left, right)?;
        
        if v1.len() != v2.len() {
            return Err(MoteDBError::TypeError(format!(
                "Vector dimension mismatch: {} vs {}", v1.len(), v2.len()
            )));
        }
        
        let dot: f32 = v1.iter().zip(v2.iter()).map(|(a, b)| a * b).sum();
        let norm1: f32 = v1.iter().map(|x| x * x).sum::<f32>().sqrt();
        let norm2: f32 = v2.iter().map(|x| x * x).sum::<f32>().sqrt();
        
        if norm1 == 0.0 || norm2 == 0.0 {
            return Ok(Value::Float(1.0)); // Maximum distance for zero vectors
        }
        
        let cosine_sim = dot / (norm1 * norm2);
        let dist = 1.0 - cosine_sim; // Range: [0, 2]
        
        Ok(Value::Float(dist as f64))
    }
    
    /// Dot Product (Inner Product): <#>
    fn dot_product(&self, left: Value, right: Value) -> Result<Value> {
        let (v1, v2) = self.extract_vectors(left, right)?;
        
        if v1.len() != v2.len() {
            return Err(MoteDBError::TypeError(format!(
                "Vector dimension mismatch: {} vs {}", v1.len(), v2.len()
            )));
        }
        
        let dot: f32 = v1.iter().zip(v2.iter()).map(|(a, b)| a * b).sum();
        
        Ok(Value::Float(dot as f64))
    }
    
    /// Extract vectors from Value types
    fn extract_vectors(&self, left: Value, right: Value) -> Result<(Vec<f32>, Vec<f32>)> {
        let v1 = match left {
            Value::Vector(v) => v.to_vec(),
            Value::Tensor(t) => {
                t.as_f32().to_vec()
            }
            _ => return Err(MoteDBError::TypeError(
                format!("Left operand is not a vector: {:?}", left)
            )),
        };
        
        let v2 = match right {
            Value::Vector(v) => v.to_vec(),
            Value::Tensor(t) => {
                t.as_f32().to_vec()
            }
            _ => return Err(MoteDBError::TypeError(
                format!("Right operand is not a vector: {:?}", right)
            )),
        };
        
        Ok((v1, v2))
    }
    
    // E-SQL Spatial Functions
    
    /// ST_Distance: Compute 3D Euclidean distance between two spatial points
    fn st_distance(&self, p1: Value, p2: Value) -> Result<Value> {
        use crate::types::Geometry;
        
        let point1 = match p1 {
            Value::Spatial(Geometry::Point(p)) => p,
            _ => return Err(MoteDBError::TypeError("ST_Distance requires spatial point arguments".to_string())),
        };
        
        let point2 = match p2 {
            Value::Spatial(Geometry::Point(p)) => p,
            _ => return Err(MoteDBError::TypeError("ST_Distance requires spatial point arguments".to_string())),
        };
        
        let dist = ((point1.x - point2.x).powi(2) + (point1.y - point2.y).powi(2)).sqrt();
        Ok(Value::Float(dist))
    }
    
    /// WITHIN_RADIUS: Check if a point is within radius of a center point
    fn within_radius(&self, point: Value, center: Value, radius: Value) -> Result<Value> {
        use crate::types::Geometry;
        
        let p = match point {
            Value::Spatial(Geometry::Point(p)) => p,
            _ => return Err(MoteDBError::TypeError("WITHIN_RADIUS requires spatial point for first argument".to_string())),
        };
        
        let c = match center {
            Value::Spatial(Geometry::Point(c)) => c,
            _ => return Err(MoteDBError::TypeError("WITHIN_RADIUS requires spatial point for center".to_string())),
        };
        
        let r = match radius {
            Value::Float(r) => r,
            Value::Integer(i) => i as f64,
            _ => return Err(MoteDBError::TypeError("WITHIN_RADIUS requires numeric radius".to_string())),
        };
        
        let dist = ((p.x - c.x).powi(2) + (p.y - c.y).powi(2)).sqrt();
        Ok(Value::Bool(dist <= r))
    }
    
    /// ST_OnTopOf: Check if point p1 is on top of point p2 (p1.y > p2.y)
    fn st_ontopof(&self, p1: Value, p2: Value) -> Result<Value> {
        use crate::types::Geometry;
        
        let point1 = match p1 {
            Value::Spatial(Geometry::Point(p)) => p,
            _ => return Err(MoteDBError::TypeError("ST_OnTopOf requires spatial point arguments".to_string())),
        };
        
        let point2 = match p2 {
            Value::Spatial(Geometry::Point(p)) => p,
            _ => return Err(MoteDBError::TypeError("ST_OnTopOf requires spatial point arguments".to_string())),
        };
        
        // In 2D, "on top of" means higher Y coordinate
        // Also check if X coordinates are close (within same vertical region)
        let same_region = (point1.x - point2.x).abs() < 1.0; // Within 1 unit horizontally
        let above = point1.y > point2.y;
        
        Ok(Value::Bool(same_region && above))
    }
}

impl Default for ExprEvaluator {
    fn default() -> Self {
        Self::new()
    }
}