stoolap 0.4.0

High-performance embedded SQL database with MVCC, time-travel queries, and full ACID compliance
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
// Copyright 2025 Stoolap Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! SQL Parser - Main Parser struct and core parsing logic

use std::sync::LazyLock;

use rustc_hash::FxHashSet;

use super::ast::*;
use super::error::{ParseError, ParseErrors};
use super::lexer::Lexer;
use super::precedence::Precedence;
use super::token::{Token, TokenType};

/// Reserved SQL keywords that cannot be used as identifiers (O(1) lookup)
static RESERVED_KEYWORDS: LazyLock<FxHashSet<&'static str>> = LazyLock::new(|| {
    [
        // Core SQL keywords that should never be identifiers
        "SELECT",
        "FROM",
        "WHERE",
        "AND",
        "OR",
        "NOT",
        "INSERT",
        "INTO",
        "VALUES",
        "UPDATE",
        "SET",
        "DELETE",
        "CREATE",
        "DROP",
        "TABLE",
        "INDEX",
        "VIEW",
        "ALTER",
        "ADD",
        "PRIMARY",
        "KEY",
        "FOREIGN",
        "REFERENCES",
        "NULL",
        "TRUE",
        "FALSE",
        "AS",
        "ON",
        "JOIN",
        // LEFT and RIGHT are handled specially - they can be function names
        // or column names when not followed by JOIN
        "INNER",
        "OUTER",
        "FULL",
        "CROSS",
        "GROUP",
        "BY",
        "ORDER",
        "HAVING",
        "LIMIT",
        "OFFSET",
        "UNION",
        "INTERSECT",
        "EXCEPT",
        "CASE",
        "WHEN",
        "THEN",
        "ELSE",
        "END",
        "DISTINCT",
        "ALL",
        "EXISTS",
        "IN",
        "BETWEEN",
        "LIKE",
        "GLOB",
        "REGEXP",
        "RLIKE",
        "IS",
        "ASC",
        "DESC",
        "NULLS",
        // FIRST and LAST are handled specially - they can be function names
        // or column names, or ORDER BY modifiers (NULLS FIRST/LAST)
        "BEGIN",
        "COMMIT",
        "ROLLBACK",
        "SAVEPOINT",
        "IF",
        "WITH",
        "RECURSIVE",
    ]
    .into_iter()
    .collect()
});

/// SQL Parser using Pratt parsing algorithm
pub struct Parser {
    /// The lexer providing tokens
    lexer: Lexer,
    /// Current token being examined
    pub(crate) cur_token: Token,
    /// Next token (peek)
    pub(crate) peek_token: Token,
    /// Collected errors
    errors: Vec<ParseError>,
    /// Current clause context (for error messages and parameter tracking)
    pub(crate) current_clause: String,
    /// Current statement ID (for multi-statement queries)
    current_statement_id: usize,
    /// Parameter counter within current statement
    parameter_counter: usize,
}

impl Parser {
    /// Create a new parser for the given input
    pub fn new(input: &str) -> Self {
        let mut lexer = Lexer::new(input);
        let cur_token = lexer.next_token();
        let peek_token = lexer.next_token();

        Parser {
            lexer,
            cur_token,
            peek_token,
            errors: Vec::new(),
            current_clause: String::new(),
            current_statement_id: 0,
            parameter_counter: 1,
        }
    }

    /// Parse the input and return a Program
    pub fn parse_program(&mut self) -> Result<Program, ParseErrors> {
        // Pre-allocate for common case (most queries have 1 statement)
        let mut statements = Vec::with_capacity(1);

        while !self.cur_token_is(TokenType::Eof) {
            // Skip comments
            if self.cur_token_is(TokenType::Comment) {
                self.next_token();
                continue;
            }

            if let Some(stmt) = self.parse_statement() {
                statements.push(stmt);
            }

            // Skip optional semicolons (handles trailing `;;` etc.)
            while self.peek_token_is_punctuator(";") {
                self.next_token();
            }

            self.next_token();
            self.current_statement_id += 1;
            self.parameter_counter = 1;
        }

        if !self.errors.is_empty() {
            return Err(ParseErrors::from_errors(self.errors.clone()));
        }

        Ok(Program { statements })
    }

    /// Advance to the next token
    pub(crate) fn next_token(&mut self) {
        self.cur_token = std::mem::replace(&mut self.peek_token, self.lexer.next_token());
    }

    /// Check if the current token is of the given type
    pub(crate) fn cur_token_is(&self, t: TokenType) -> bool {
        self.cur_token.token_type == t
    }

    /// Check if the peek token is of the given type
    pub(crate) fn peek_token_is(&self, t: TokenType) -> bool {
        self.peek_token.token_type == t
    }

    /// Check if the current token can be used as an identifier
    /// This allows keywords like TIMESTAMP, DATE, etc. to be used as column/table names
    pub(crate) fn cur_token_is_identifier_like(&self) -> bool {
        match self.cur_token.token_type {
            TokenType::Identifier => true,
            TokenType::Keyword => {
                // Allow non-reserved keywords as identifiers
                // Reserved keywords that cannot be used as identifiers
                !Self::is_reserved_keyword(&self.cur_token.literal)
            }
            _ => false,
        }
    }

    /// Create an Identifier from the current token.
    /// Identifier::new automatically lowercases keyword tokens.
    pub(crate) fn cur_token_as_column_identifier(&self) -> Identifier {
        Identifier::new(self.cur_token.clone(), self.cur_token.literal.clone())
    }

    /// Check if a keyword is truly reserved and cannot be used as an identifier
    /// Note: Some keywords like LEFT, RIGHT, FIRST, LAST are handled specially in
    /// parse_keyword_prefix() where they can be functions or identifiers.
    /// Uses O(1) HashSet lookup instead of O(n) match chain.
    pub(crate) fn is_reserved_keyword(keyword: &str) -> bool {
        // Use uppercase for case-insensitive comparison
        // Note: Keywords are typically already uppercase from the lexer
        RESERVED_KEYWORDS.contains(keyword.to_uppercase().as_str())
    }

    /// Check if the current token is a specific keyword
    pub(crate) fn cur_token_is_keyword(&self, keyword: &str) -> bool {
        self.cur_token.token_type == TokenType::Keyword
            && self.cur_token.literal.eq_ignore_ascii_case(keyword)
    }

    /// Check if the peek token is a specific keyword
    pub(crate) fn peek_token_is_keyword(&self, keyword: &str) -> bool {
        self.peek_token.token_type == TokenType::Keyword
            && self.peek_token.literal.eq_ignore_ascii_case(keyword)
    }

    /// Check if the current token is a specific punctuator
    pub(crate) fn cur_token_is_punctuator(&self, punc: &str) -> bool {
        self.cur_token.token_type == TokenType::Punctuator && self.cur_token.literal == punc
    }

    /// Check if the peek token is a specific punctuator
    pub(crate) fn peek_token_is_punctuator(&self, punc: &str) -> bool {
        self.peek_token.token_type == TokenType::Punctuator && self.peek_token.literal == punc
    }

    /// Check if the peek token is a specific operator
    pub(crate) fn peek_token_is_operator(&self, op: &str) -> bool {
        self.peek_token.token_type == TokenType::Operator && self.peek_token.literal == op
    }

    /// Expect the peek token to be of a specific type and advance
    pub(crate) fn expect_peek(&mut self, t: TokenType) -> bool {
        if self.peek_token_is(t) {
            self.next_token();
            true
        } else {
            self.peek_error(t);
            false
        }
    }

    /// Expect the peek token to be a specific keyword and advance
    pub(crate) fn expect_keyword(&mut self, keyword: &str) -> bool {
        if self.peek_token_is_keyword(keyword) {
            self.next_token();
            true
        } else {
            self.add_error(format!(
                "expected {} after {}, got {}",
                keyword,
                self.cur_token.literal,
                Self::format_token_for_error(&self.peek_token)
            ));
            false
        }
    }

    /// Get the precedence of the peek token
    pub(crate) fn peek_precedence(&self) -> Precedence {
        match self.peek_token.token_type {
            TokenType::Operator => Precedence::for_operator(&self.peek_token.literal),
            TokenType::Keyword => Precedence::for_operator(&self.peek_token.literal),
            TokenType::Punctuator => {
                if self.peek_token.literal == "." {
                    Precedence::Dot
                } else if self.peek_token.literal == "(" {
                    Precedence::Call
                } else if self.peek_token.literal == "[" {
                    Precedence::Index
                } else {
                    Precedence::Lowest
                }
            }
            _ => Precedence::Lowest,
        }
    }

    /// Get the precedence of the current token
    pub(crate) fn cur_precedence(&self) -> Precedence {
        match self.cur_token.token_type {
            TokenType::Operator => Precedence::for_operator(&self.cur_token.literal),
            TokenType::Keyword => Precedence::for_operator(&self.cur_token.literal),
            TokenType::Punctuator => {
                if self.cur_token.literal == "." {
                    Precedence::Dot
                } else if self.cur_token.literal == "(" {
                    Precedence::Call
                } else if self.cur_token.literal == "[" {
                    Precedence::Index
                } else {
                    Precedence::Lowest
                }
            }
            _ => Precedence::Lowest,
        }
    }

    /// Add an error for unexpected peek token type
    pub(crate) fn peek_error(&mut self, expected: TokenType) {
        let expected_desc = match expected {
            TokenType::Identifier => "identifier (name)",
            TokenType::Keyword => "keyword",
            TokenType::Punctuator => "'(' or ')'",
            TokenType::String => "string literal",
            TokenType::Integer => "integer",
            TokenType::Float => "number",
            _ => "token",
        };

        if self.peek_token.token_type == TokenType::Eof {
            if !self.current_clause.is_empty() {
                self.add_error(format!(
                    "expected {} after {}",
                    expected_desc, self.current_clause
                ));
            } else {
                self.add_error(format!(
                    "unexpected end of input, expected {}",
                    expected_desc
                ));
            }
        } else if expected == TokenType::Identifier
            && self.peek_token.token_type == TokenType::Keyword
            && Self::is_reserved_keyword(&self.peek_token.literal)
        {
            self.add_error(format!(
                "'{}' is a reserved keyword and cannot be used as an identifier. \
                 Use double quotes to escape it: \"{}\"",
                self.peek_token.literal.to_uppercase(),
                self.peek_token.literal
            ));
        } else {
            self.add_error(format!(
                "expected {}, got {}",
                expected_desc,
                Self::format_token_for_error(&self.peek_token)
            ));
        }
    }

    /// Format a token for display in error messages (shows "end of input" for EOF)
    pub(crate) fn format_token_for_error(token: &Token) -> String {
        if token.token_type == TokenType::Eof {
            "end of input".to_string()
        } else {
            format!("'{}'", token.literal)
        }
    }

    /// Add an error message
    pub(crate) fn add_error(&mut self, msg: String) {
        self.errors
            .push(ParseError::new(msg, self.cur_token.position));
    }

    /// Get collected errors
    pub fn errors(&self) -> &[ParseError] {
        &self.errors
    }

    /// Get the next parameter index
    pub(crate) fn next_parameter_index(&mut self) -> usize {
        let idx = self.parameter_counter;
        self.parameter_counter += 1;
        idx
    }

    /// Get current statement ID
    #[allow(dead_code)]
    pub(crate) fn current_statement_id(&self) -> usize {
        self.current_statement_id
    }
}

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

    #[test]
    fn test_parser_creation() {
        let parser = Parser::new("SELECT * FROM users");
        assert!(parser.cur_token_is_keyword("SELECT"));
    }

    #[test]
    fn test_next_token() {
        let mut parser = Parser::new("SELECT * FROM users");
        assert!(parser.cur_token_is_keyword("SELECT"));
        parser.next_token();
        assert!(parser.cur_token_is(TokenType::Operator));
        assert_eq!(parser.cur_token.literal, "*");
    }

    #[test]
    fn test_peek_token() {
        let parser = Parser::new("SELECT * FROM users");
        assert!(parser.cur_token_is_keyword("SELECT"));
        assert!(parser.peek_token_is_operator("*"));
    }
}