motedb 0.5.2

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
/// SQL Lexer - converts SQL string into tokens
use super::token::{Token, TokenType};
use crate::error::{MoteDBError, Result};

pub struct Lexer<'a> {
    input: &'a str,
    bytes: &'a [u8],
    position: usize,
    line: usize,
    column: usize,
}

impl<'a> Lexer<'a> {
    pub fn new(input: &'a str) -> Self {
        Self {
            input,
            bytes: input.as_bytes(),
            position: 0,
            line: 1,
            column: 1,
        }
    }

    pub fn tokenize(&mut self) -> Result<Vec<Token>> {
        // 🚀 P1.2: Pre-allocate tokens based on input size
        let estimated_tokens = self.input.len() / 4 + 10;
        let mut tokens = Vec::with_capacity(estimated_tokens);

        loop {
            let token = self.next_token()?;
            let is_eof = matches!(token.token_type, TokenType::Eof);
            tokens.push(token);
            if is_eof {
                break;
            }
        }

        Ok(tokens)
    }

    pub fn next_token(&mut self) -> Result<Token> {
        self.skip_whitespace();

        let line = self.line;
        let column = self.column;

        if self.is_eof() {
            return Ok(Token::new(TokenType::Eof, line, column));
        }

        let ch = self.current_char();

        // Skip comments
        if ch == '-' && self.peek_char() == Some('-') {
            self.skip_line_comment();
            return self.next_token();
        }

        if ch == '/' && self.peek_char() == Some('*') {
            self.skip_block_comment()?;
            return self.next_token();
        }

        let token_type = match ch {
            // String literals
            '\'' | '"' => self.read_string(ch)?,

            // Numbers
            '0'..='9' => self.read_number()?,

            // Identifiers and keywords
            'a'..='z' | 'A'..='Z' | '_' => self.read_identifier()?,

            // Operators and delimiters
            '=' => {
                self.advance();
                TokenType::Eq
            }
            '!' => {
                self.advance();
                if self.current_char() == '=' {
                    self.advance();
                    TokenType::Ne
                } else {
                    return Err(MoteDBError::ParseError(format!(
                        "Unexpected character '!' at {}:{}",
                        line, column
                    )));
                }
            }
            '<' => {
                self.advance();
                if self.current_char() == '=' {
                    self.advance();
                    // Check for <=> (cosine distance)
                    if self.current_char() == '>' {
                        self.advance();
                        TokenType::CosineDistance
                    } else {
                        TokenType::Le
                    }
                } else if self.current_char() == '>' {
                    self.advance();
                    TokenType::Ne
                } else if self.current_char() == '-' {
                    self.advance();
                    // Check for <-> (L2 distance)
                    if self.current_char() == '>' {
                        self.advance();
                        TokenType::L2Distance
                    } else {
                        return Err(MoteDBError::ParseError(format!(
                            "Unexpected sequence '<-' at {}:{}",
                            line, column
                        )));
                    }
                } else if self.current_char() == '#' {
                    self.advance();
                    // Check for <#> (dot product)
                    if self.current_char() == '>' {
                        self.advance();
                        TokenType::DotProduct
                    } else {
                        return Err(MoteDBError::ParseError(format!(
                            "Unexpected sequence '<#' at {}:{}",
                            line, column
                        )));
                    }
                } else {
                    TokenType::Lt
                }
            }
            '>' => {
                self.advance();
                if self.current_char() == '=' {
                    self.advance();
                    TokenType::Ge
                } else {
                    TokenType::Gt
                }
            }
            '+' => {
                self.advance();
                TokenType::Plus
            }
            '-' => {
                self.advance();
                TokenType::Minus
            }
            '*' => {
                self.advance();
                TokenType::Star
            }
            '/' => {
                self.advance();
                TokenType::Slash
            }
            '%' => {
                self.advance();
                TokenType::Percent
            }
            '(' => {
                self.advance();
                TokenType::LParen
            }
            ')' => {
                self.advance();
                TokenType::RParen
            }
            '[' => {
                self.advance();
                TokenType::LBracket
            }
            ']' => {
                self.advance();
                TokenType::RBracket
            }
            ',' => {
                self.advance();
                TokenType::Comma
            }
            ';' => {
                self.advance();
                TokenType::Semicolon
            }
            '.' => {
                self.advance();
                TokenType::Dot
            }
            '?' => {
                self.advance();
                // Check for ?N (numbered parameter like ?1, ?2)
                if !self.is_eof() && self.current_char().is_ascii_digit() {
                    let mut num = String::new();
                    while !self.is_eof() && self.current_char().is_ascii_digit() {
                        num.push(self.current_char());
                        self.advance();
                    }
                    let idx: usize = num.parse().unwrap_or(1);
                    TokenType::Parameter(idx)
                } else {
                    // Unnamed ? — gets sequential number resolved later
                    TokenType::Parameter(0) // 0 = auto-assign
                }
            }
            _ => {
                return Err(MoteDBError::ParseError(format!(
                    "Unexpected character '{}' at {}:{}",
                    ch, line, column
                )));
            }
        };

        Ok(Token::new(token_type, line, column))
    }

    fn current_char(&self) -> char {
        if self.is_eof() {
            '\0'
        } else {
            // 🚀 P1.1: Direct byte access (O(1))
            self.bytes[self.position] as char
        }
    }

    /// Decode the current UTF-8 character (handles multi-byte sequences correctly).
    fn current_utf8_char(&self) -> char {
        if self.is_eof() {
            return '\0';
        }
        // Decode from the raw byte slice at `position`. We must NOT slice
        // `self.input[..]` (a &str) by a byte index — if `position` lands in
        // the middle of a multi-byte UTF-8 sequence (which happens whenever a
        // caller advances past a non-ASCII byte one byte at a time), str
        // slicing panics. Decoding from bytes is panic-free: a lone
        // continuation byte decodes to a replacement char, which is harmless
        // for lexing (it just yields an Unexpected-token error).
        let rest = &self.bytes[self.position..];
        match std::str::from_utf8(rest) {
            Ok(s) => s.chars().next().unwrap_or('\0'),
            Err(_) => {
                // `rest` starts with invalid UTF-8. If the first byte is a
                // leading byte of a valid sequence that's simply truncated,
                // decode just that byte's expected length; otherwise treat the
                // single byte as a byte char.
                let b = rest[0];
                // Decode the first code point from up to 4 bytes.
                decode_first_codepoint(rest).unwrap_or(b as char)
            }
        }
    }

    /// Advance past the current UTF-8 character (1-4 bytes).
    fn advance_utf8(&mut self) {
        if self.is_eof() {
            return;
        }
        // Compute the UTF-8 length of the code point starting at `position`
        // WITHOUT slicing self.input (which panics on intra-character byte
        // offsets). Decode from the byte slice.
        let rest = &self.bytes[self.position..];
        let char_len = decode_first_codepoint(rest)
            .map(|c| c.len_utf8())
            .unwrap_or(1);
        for _ in 0..char_len {
            self.advance();
        }
    }

    fn peek_char(&self) -> Option<char> {
        if self.position + 1 < self.bytes.len() {
            Some(self.bytes[self.position + 1] as char)
        } else {
            None
        }
    }

    fn advance(&mut self) {
        if !self.is_eof() {
            if self.bytes[self.position] == b'\n' {
                self.line += 1;
                self.column = 1;
            } else {
                self.column += 1;
            }
            self.position += 1;
        }
    }

    fn is_eof(&self) -> bool {
        self.position >= self.bytes.len()
    }

    fn skip_whitespace(&mut self) {
        while !self.is_eof() && self.current_char().is_whitespace() {
            self.advance();
        }
    }

    fn skip_line_comment(&mut self) {
        while !self.is_eof() && self.current_char() != '\n' {
            self.advance();
        }
        if !self.is_eof() {
            self.advance(); // skip newline
        }
    }

    fn skip_block_comment(&mut self) -> Result<()> {
        self.advance(); // skip '/'
        self.advance(); // skip '*'

        while !self.is_eof() {
            if self.current_char() == '*' && self.peek_char() == Some('/') {
                self.advance(); // skip '*'
                self.advance(); // skip '/'
                return Ok(());
            }
            self.advance();
        }

        Err(MoteDBError::ParseError(
            "Unterminated block comment".to_string(),
        ))
    }

    fn read_string(&mut self, quote: char) -> Result<TokenType> {
        self.advance(); // skip opening quote
        let mut value = String::with_capacity(32);
        const MAX_STRING_LEN: usize = 16 * 1024 * 1024; // 16 MiB limit prevents OOM

        while !self.is_eof() {
            if value.len() >= MAX_STRING_LEN {
                return Err(MoteDBError::ParseError(
                    "String literal exceeds maximum length (16 MiB)".to_string(),
                ));
            }
            let ch = self.current_utf8_char();

            if ch == quote {
                // SQL standard: doubled quote escapes the quote ('it''s' → it's)
                // Check if next char is also a quote
                let after_quote = self.position + 1;
                if after_quote < self.bytes.len() && self.bytes[after_quote] == quote as u8 {
                    value.push(quote);
                    self.advance(); // skip first quote
                    self.advance(); // skip second quote
                    continue;
                }
                // Single quote = end of string
                break;
            }

            if ch == '\\' {
                self.advance();
                if self.is_eof() {
                    return Err(MoteDBError::ParseError("Unterminated string".to_string()));
                }
                let escaped = match self.current_char() {
                    'n' => '\n',
                    't' => '\t',
                    'r' => '\r',
                    '\\' => '\\',
                    '\'' => '\'',
                    '"' => '"',
                    c => c,
                };
                value.push(escaped);
                self.advance();
            } else {
                value.push(ch);
                self.advance_utf8();
            }
        }

        if self.is_eof() {
            return Err(MoteDBError::ParseError("Unterminated string".to_string()));
        }

        self.advance(); // skip closing quote
        Ok(TokenType::String(value))
    }

    fn read_number(&mut self) -> Result<TokenType> {
        let mut value = String::with_capacity(16);

        while !self.is_eof() && (self.current_char().is_numeric() || self.current_char() == '.') {
            value.push(self.current_char());
            self.advance();
        }

        // Handle scientific notation (e.g., 1.5e10)
        if !self.is_eof() && (self.current_char() == 'e' || self.current_char() == 'E') {
            value.push(self.current_char());
            self.advance();
            if !self.is_eof() && (self.current_char() == '+' || self.current_char() == '-') {
                value.push(self.current_char());
                self.advance();
            }
            while !self.is_eof() && self.current_char().is_numeric() {
                value.push(self.current_char());
                self.advance();
            }
        }

        let num = value
            .parse::<f64>()
            .map_err(|_| MoteDBError::ParseError(format!("Invalid number: {}", value)))?;
        if num.is_infinite() || num.is_nan() {
            return Err(MoteDBError::ParseError(format!(
                "Number out of range: {}",
                value
            )));
        }
        Ok(TokenType::Number(num))
    }

    fn read_identifier(&mut self) -> Result<TokenType> {
        let start = self.position;

        while !self.is_eof() {
            let ch = self.current_utf8_char();
            if ch.is_alphanumeric() || ch == '_' {
                self.advance_utf8();
            } else {
                break;
            }
        }

        let word = &self.input[start..self.position];

        // Guard against DoS via extremely long identifiers (4KB limit)
        if word.len() > 4096 {
            return Err(MoteDBError::ParseError("Identifier too long".into()));
        }

        // Zero-allocation keyword check (from_keyword uses stack buffer)
        Ok(
            TokenType::from_keyword(word)
                .unwrap_or_else(|| TokenType::Identifier(word.to_string())),
        )
    }
}

/// Decode the first Unicode code point from the start of `bytes`, tolerating
/// invalid/truncated UTF-8. Used by the lexer to inspect the current character
/// WITHOUT slicing a `&str` (which would panic if the byte offset lands inside
/// a multi-byte sequence). Returns `None` only if `bytes` is empty.
///
/// For valid UTF-8 this matches `str::chars().next()`. For invalid sequences
/// (lone continuation bytes, truncated leaders) it falls back to the first
/// byte as a `char` — the lexer then yields an Unexpected-token error, which
/// is the correct behavior for malformed input.
fn decode_first_codepoint(bytes: &[u8]) -> Option<char> {
    if bytes.is_empty() {
        return None;
    }
    let b0 = bytes[0];
    // ASCII fast path.
    if b0 < 0x80 {
        return Some(b0 as char);
    }
    // Determine expected length from the leading byte.
    let len = if b0 & 0xE0 == 0xC0 {
        2
    } else if b0 & 0xF0 == 0xE0 {
        3
    } else if b0 & 0xF8 == 0xF0 {
        4
    } else {
        // Lone continuation byte (10xxxxxx) or invalid leader.
        return Some(b0 as char);
    };
    if bytes.len() < len {
        // Truncated sequence — not enough bytes. Decode what we have as a
        // single byte so the lexer advances one byte and recovers.
        return Some(b0 as char);
    }
    // Try to decode the full sequence; if any continuation byte is invalid,
    // fall back to the leading byte.
    let mut code = (b0 as u32)
        & match len {
            2 => 0x1F,
            3 => 0x0F,
            4 => 0x07,
            _ => 0x7F,
        };
    for i in 1..len {
        let b = bytes[i];
        if b & 0xC0 != 0x80 {
            return Some(b0 as char);
        }
        code = (code << 6) | (b as u32 & 0x3F);
    }
    char::from_u32(code).or(Some(b0 as char))
}

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

    #[test]
    fn test_lexer_simple_select() {
        let mut lexer = Lexer::new("SELECT * FROM users");
        let tokens = lexer.tokenize().unwrap();

        assert_eq!(tokens.len(), 5); // SELECT, *, FROM, users, EOF
        assert!(matches!(tokens[0].token_type, TokenType::Select));
        assert!(matches!(tokens[1].token_type, TokenType::Star));
        assert!(matches!(tokens[2].token_type, TokenType::From));
        assert!(matches!(tokens[3].token_type, TokenType::Identifier(_)));
        assert!(matches!(tokens[4].token_type, TokenType::Eof));
    }

    /// Regression: the lexer must not panic on bytes that contain multi-byte
    /// UTF-8 characters. Found by the fuzz_sql_parser target (the byte sequence
    /// `]]'Ü\©\g` crashed current_utf8_char via a str slice on an
    /// intra-character byte offset). Tokenizing may succeed or return an error,
    /// but it must never panic.
    #[test]
    fn test_lexer_multibyte_utf8_no_panic() {
        // The exact crashing input from the fuzzer.
        let crashing: &[u8] = &[93, 93, 39, 220, 92, 169, 92, 103];
        let sql = String::from_utf8_lossy(crashing);
        let mut lexer = Lexer::new(&sql);
        // Must not panic — we ignore the result (Ok or Err both fine).
        let _ = lexer.tokenize();

        // A few more multi-byte sequences that previously risked panicking
        // when position landed inside a code point.
        for s in &["café", "日本語", "a\0b", "表情🎓", "\u{1F916}robot"] {
            let _ = Lexer::new(s).tokenize();
        }
    }

    #[test]
    fn test_lexer_with_where() {
        let mut lexer = Lexer::new("SELECT id FROM users WHERE age > 18");
        let tokens = lexer.tokenize().unwrap();

        // SELECT, id, FROM, users, WHERE, age, >, 18, EOF
        assert_eq!(tokens.len(), 9);
        assert!(matches!(tokens[5].token_type, TokenType::Identifier(_)));
        assert!(matches!(tokens[6].token_type, TokenType::Gt));
        assert!(matches!(tokens[7].token_type, TokenType::Number(_)));
    }

    #[test]
    fn test_lexer_string_literal() {
        let mut lexer = Lexer::new("SELECT * FROM users WHERE name = 'John'");
        let tokens = lexer.tokenize().unwrap();

        // SELECT, *, FROM, users, WHERE, name, =, 'John', EOF
        // Index: 0,1,2,3,4,5,6,7,8
        assert!(matches!(tokens[7].token_type, TokenType::String(ref s) if s == "John"));
    }

    #[test]
    fn test_lexer_operators() {
        let mut lexer = Lexer::new("= != < > <= >= + - * /");
        let tokens = lexer.tokenize().unwrap();

        assert!(matches!(tokens[0].token_type, TokenType::Eq));
        assert!(matches!(tokens[1].token_type, TokenType::Ne));
        assert!(matches!(tokens[2].token_type, TokenType::Lt));
        assert!(matches!(tokens[3].token_type, TokenType::Gt));
        assert!(matches!(tokens[4].token_type, TokenType::Le));
        assert!(matches!(tokens[5].token_type, TokenType::Ge));
    }

    #[test]
    fn test_lexer_comment() {
        let mut lexer = Lexer::new("SELECT * -- this is a comment\nFROM users");
        let tokens = lexer.tokenize().unwrap();

        assert_eq!(tokens.len(), 5); // Comment should be skipped
        assert!(matches!(tokens[2].token_type, TokenType::From));
    }
}