vibesql-parser 0.2.0

SQL parser for vibesql database engine
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
//! Function parsing module
//!
//! This module handles parsing of all SQL function types:
//! - Regular functions (e.g., UPPER, LOWER, CONCAT)
//! - Aggregate functions (COUNT, SUM, AVG, MIN, MAX)
//! - Window functions (with OVER clause)
//! - Special SQL:1999 syntax (POSITION, TRIM, SUBSTRING)

use super::*;

mod string_special;
mod window;

impl Parser {
    /// Parse function call expressions (including window functions)
    pub(super) fn parse_function_call(
        &mut self,
    ) -> Result<Option<vibesql_ast::Expression>, ParseError> {
        // Try to match either an identifier or specific keywords that can be function names
        let function_name = match self.peek() {
            Token::Identifier(id) | Token::DelimitedIdentifier(id) => {
                let name = id.clone();
                self.advance();
                // Check if followed by '('
                if !matches!(self.peek(), Token::LParen) {
                    // Not a function call - rewind
                    self.position -= 1;
                    return Ok(None);
                }
                name
            }
            // Allow LEFT, RIGHT, REPLACE, SCHEMA, GROUPING, GROUPING_ID, GLOB, LIKE, MATCH,
            // DATE, and TIME keywords as function names. These are reserved keywords but
            // can also be functions (DATE/TIME: SQLite date('now')/time('now'), #5307).
            Token::Keyword { keyword: Keyword::Left, .. }
            | Token::Keyword { keyword: Keyword::Right, .. }
            | Token::Keyword { keyword: Keyword::Replace, .. }
            | Token::Keyword { keyword: Keyword::Schema, .. }
            | Token::Keyword { keyword: Keyword::Grouping, .. }
            | Token::Keyword { keyword: Keyword::GroupingId, .. }
            | Token::Keyword { keyword: Keyword::Glob, .. }
            | Token::Keyword { keyword: Keyword::Like, .. }
            | Token::Keyword { keyword: Keyword::Match, .. }
            | Token::Keyword { keyword: Keyword::Date, .. }
            | Token::Keyword { keyword: Keyword::Time, .. } => {
                // Peek ahead to see if this is followed by '('
                // Don't consume the keyword unless we're sure it's a function
                // SQL:1999 normalizes unquoted identifiers (including function names) to lowercase
                let keyword_name = match self.peek() {
                    Token::Keyword { keyword: Keyword::Left, .. } => "left",
                    Token::Keyword { keyword: Keyword::Right, .. } => "right",
                    Token::Keyword { keyword: Keyword::Replace, .. } => "replace",
                    Token::Keyword { keyword: Keyword::Schema, .. } => "schema",
                    Token::Keyword { keyword: Keyword::Grouping, .. } => "grouping",
                    Token::Keyword { keyword: Keyword::GroupingId, .. } => "grouping_id",
                    Token::Keyword { keyword: Keyword::Glob, .. } => "glob",
                    Token::Keyword { keyword: Keyword::Like, .. } => "like",
                    Token::Keyword { keyword: Keyword::Match, .. } => "match",
                    Token::Keyword { keyword: Keyword::Date, .. } => "date",
                    Token::Keyword { keyword: Keyword::Time, .. } => "time",
                    _ => unreachable!(),
                };

                // Look ahead to next token
                if self.position + 1 < self.tokens.len() {
                    if matches!(self.tokens[self.position + 1], Token::LParen) {
                        // Yes, it's a function call
                        self.advance(); // consume keyword
                        keyword_name.to_string()
                    } else {
                        // Not a function call, don't consume
                        return Ok(None);
                    }
                } else {
                    return Ok(None);
                }
            }
            _ => return Ok(None),
        };

        self.advance(); // consume '('
        let first = function_name;

        // Special case for RAISE(ABORT|FAIL|ROLLBACK, msg) and RAISE(IGNORE).
        // SQLite's trigger-program error/abort expression. The first argument is
        // one of the conflict-resolution keywords (not a normal expression), so
        // it cannot go through the generic argument loop.
        if first.to_uppercase() == "RAISE" {
            return Ok(Some(self.parse_raise_expression()?));
        }

        // Special case for VALUES(column) - MySQL ON DUPLICATE KEY UPDATE
        // Returns the value that would have been inserted
        if first.to_uppercase() == "VALUES" {
            // Expect a single column name as argument
            let column = match self.peek() {
                Token::Identifier(col) | Token::DelimitedIdentifier(col) => {
                    let column_name = col.clone();
                    self.advance();
                    column_name
                }
                _ => {
                    return Err(ParseError {
                        message: "Expected column name in VALUES() function".to_string(),
                    })
                }
            };
            self.expect_token(Token::RParen)?;
            return Ok(Some(vibesql_ast::Expression::DuplicateKeyValue { column }));
        }

        // Special case for POSITION(substring IN string [USING unit])
        // SQL:1999 standard syntax
        if first.to_uppercase() == "POSITION" {
            return Ok(Some(self.parse_position_function()?));
        }

        // Special case for TRIM([position] [removal_char FROM] string)
        // SQL:1999 standard syntax
        if first.to_uppercase() == "TRIM" {
            return Ok(Some(self.parse_trim_function()?));
        }

        // Special case for SUBSTRING(string FROM start [FOR length] [USING unit])
        // SQL:1999 standard syntax - alternative to comma syntax
        if first.to_uppercase() == "SUBSTRING" {
            return Ok(Some(self.parse_substring_function(first)?));
        }

        // Special case for EXTRACT(field FROM expr)
        // SQL:1999 standard syntax for date/time extraction
        if first.to_uppercase() == "EXTRACT" {
            return Ok(Some(self.parse_extract_function()?));
        }

        // Check if this might be an aggregate function (before we know argument count)
        // Note: MIN/MAX with multiple arguments are scalar functions (like LEAST/GREATEST)
        // while MIN/MAX with a single argument are aggregate functions
        let function_name_upper = first.to_uppercase();
        let might_be_aggregate = matches!(
            function_name_upper.as_str(),
            "COUNT"
                | "SUM"
                | "AVG"
                | "MIN"
                | "MAX"
                | "GROUP_CONCAT"
                | "STRING_AGG"
                | "TOTAL"
                | "JSON_GROUP_ARRAY"
        );

        // Parse optional DISTINCT or ALL for potential aggregate functions
        let distinct = if might_be_aggregate {
            if matches!(self.peek(), Token::Keyword { keyword: Keyword::Distinct, .. }) {
                self.advance(); // consume DISTINCT
                true
            } else if matches!(self.peek(), Token::Keyword { keyword: Keyword::All, .. }) {
                self.advance(); // consume ALL
                false
            } else {
                false // default is ALL
            }
        } else {
            false
        };

        // Parse function arguments
        let mut args = Vec::new();
        let mut character_unit = None;
        let mut order_by = None;

        // Check for empty argument list or '*'
        if matches!(self.peek(), Token::RParen) {
            // No arguments: func()
            self.advance();
        } else if matches!(self.peek(), Token::Symbol('*')) {
            // Special case for COUNT(*)
            self.advance(); // consume '*'
            self.expect_token(Token::RParen)?;
            // Represent * as a special wildcard expression
            args.push(vibesql_ast::Expression::ColumnRef(vibesql_ast::ColumnIdentifier::simple(
                "*", false,
            )));
        } else if might_be_aggregate
            && matches!(self.peek(), Token::Keyword { keyword: Keyword::Order, .. })
        {
            // Zero-arg aggregate with ORDER BY: count(ORDER BY a)
            // This is a special SQL extension allowing ORDER BY without arguments
            self.advance(); // consume ORDER
            self.expect_keyword(Keyword::By)?;

            let mut order_items = Vec::new();
            loop {
                // parse_expression already handles COLLATE as a postfix operator
                let expr = self.parse_expression()?;

                // Check for optional ASC/DESC
                let direction =
                    if matches!(self.peek(), Token::Keyword { keyword: Keyword::Asc, .. }) {
                        self.advance();
                        vibesql_ast::OrderDirection::Asc
                    } else if matches!(self.peek(), Token::Keyword { keyword: Keyword::Desc, .. }) {
                        self.advance();
                        vibesql_ast::OrderDirection::Desc
                    } else {
                        vibesql_ast::OrderDirection::Asc // Default
                    };

                // Check for optional NULLS FIRST/LAST
                let nulls_order =
                    if matches!(self.peek(), Token::Keyword { keyword: Keyword::Nulls, .. }) {
                        self.advance(); // consume NULLS
                        if matches!(self.peek(), Token::Keyword { keyword: Keyword::First, .. }) {
                            self.advance();
                            Some(vibesql_ast::NullsOrder::First)
                        } else if matches!(
                            self.peek(),
                            Token::Keyword { keyword: Keyword::Last, .. }
                        ) {
                            self.advance();
                            Some(vibesql_ast::NullsOrder::Last)
                        } else {
                            return Err(ParseError {
                                message: "Expected FIRST or LAST after NULLS".to_string(),
                            });
                        }
                    } else {
                        None
                    };

                order_items.push(vibesql_ast::OrderByItem { expr, direction, nulls_order });

                if matches!(self.peek(), Token::Comma) {
                    self.advance();
                } else {
                    break;
                }
            }

            // Check for too many ORDER BY terms (SQLite compatibility)
            if order_items.len() > super::super::MAX_ORDER_BY_TERMS {
                return Err(ParseError {
                    message: "too many terms in ORDER BY clause".to_string(),
                });
            }

            order_by = Some(order_items);
            self.expect_token(Token::RParen)?;
        } else {
            // Parse comma-separated argument list
            loop {
                let arg = self.parse_expression()?;
                args.push(arg);

                if matches!(self.peek(), Token::Comma) {
                    self.advance();
                } else {
                    break;
                }
            }

            // Parse optional ORDER BY clause for aggregate functions
            // SQL:2003 syntax: aggregate(expr ORDER BY order_list)
            // Example: GROUP_CONCAT(name ORDER BY name ASC)
            if might_be_aggregate
                && matches!(self.peek(), Token::Keyword { keyword: Keyword::Order, .. })
            {
                self.advance(); // consume ORDER
                self.expect_keyword(Keyword::By)?;

                let mut order_items = Vec::new();
                loop {
                    // parse_expression already handles COLLATE as a postfix operator
                    let expr = self.parse_expression()?;

                    // Check for optional ASC/DESC
                    let direction =
                        if matches!(self.peek(), Token::Keyword { keyword: Keyword::Asc, .. }) {
                            self.advance();
                            vibesql_ast::OrderDirection::Asc
                        } else if matches!(
                            self.peek(),
                            Token::Keyword { keyword: Keyword::Desc, .. }
                        ) {
                            self.advance();
                            vibesql_ast::OrderDirection::Desc
                        } else {
                            vibesql_ast::OrderDirection::Asc // Default
                        };

                    // Check for optional NULLS FIRST/LAST
                    let nulls_order =
                        if matches!(self.peek(), Token::Keyword { keyword: Keyword::Nulls, .. }) {
                            self.advance(); // consume NULLS
                            if matches!(self.peek(), Token::Keyword { keyword: Keyword::First, .. })
                            {
                                self.advance();
                                Some(vibesql_ast::NullsOrder::First)
                            } else if matches!(
                                self.peek(),
                                Token::Keyword { keyword: Keyword::Last, .. }
                            ) {
                                self.advance();
                                Some(vibesql_ast::NullsOrder::Last)
                            } else {
                                return Err(ParseError {
                                    message: "Expected FIRST or LAST after NULLS".to_string(),
                                });
                            }
                        } else {
                            None
                        };

                    order_items.push(vibesql_ast::OrderByItem { expr, direction, nulls_order });

                    if matches!(self.peek(), Token::Comma) {
                        self.advance();
                    } else {
                        break;
                    }
                }

                // Check for too many ORDER BY terms (SQLite compatibility)
                if order_items.len() > super::super::MAX_ORDER_BY_TERMS {
                    return Err(ParseError {
                        message: "too many terms in ORDER BY clause".to_string(),
                    });
                }

                order_by = Some(order_items);
            }

            // Parse optional USING clause for string functions BEFORE closing paren
            if matches!(function_name_upper.as_str(), "CHARACTER_LENGTH" | "CHAR_LENGTH")
                && matches!(self.peek(), Token::Keyword { keyword: Keyword::Using, .. })
            {
                self.advance(); // consume USING
                character_unit = Some(self.parse_character_unit()?);
            }

            self.expect_token(Token::RParen)?;
        }

        // Parse optional FILTER clause (SQL:2003)
        // Syntax: aggregate(...) FILTER (WHERE condition) [OVER (...)]
        let filter = if matches!(self.peek(), Token::Keyword { keyword: Keyword::Filter, .. }) {
            self.advance(); // consume FILTER
            self.expect_token(Token::LParen)?;
            self.expect_keyword(Keyword::Where)?;
            let condition = self.parse_expression()?;
            self.expect_token(Token::RParen)?;
            Some(Box::new(condition))
        } else {
            None
        };

        // Check for OVER clause (window function)
        if matches!(self.peek(), Token::Keyword { keyword: Keyword::Over, .. }) {
            self.advance(); // consume OVER

            // Validate argument count for window functions
            self.validate_window_function_args(&function_name_upper, args.len(), &first)?;

            // Parse window specification
            let window_spec = self.parse_window_spec()?;

            // Determine window function type based on function name
            let function_spec = self.classify_window_function(&first, args, filter);

            return Ok(Some(vibesql_ast::Expression::WindowFunction {
                function: function_spec,
                over: window_spec,
            }));
        }

        // Check if this is a window-only function used without OVER clause
        // These functions REQUIRE an OVER clause - they cannot be used as scalar functions
        if self.is_window_only_function(&function_name_upper) {
            return Err(ParseError {
                message: format!("misuse of window function {}()", first.to_lowercase()),
            });
        }

        // Determine if this is truly an aggregate function
        // MIN/MAX with >1 argument are scalar functions (SQLite compatibility)
        // GROUP_CONCAT accepts 1 or 2 arguments (expr, separator)
        let is_aggregate = match function_name_upper.as_str() {
            "COUNT" | "SUM" | "AVG" | "TOTAL" => true,
            "GROUP_CONCAT" | "STRING_AGG" => args.len() <= 2, // 1 or 2 args
            "JSON_GROUP_ARRAY" => true,                       // JSON aggregate function
            "MIN" | "MAX" => args.len() <= 1 && !distinct, // multi-arg or DISTINCT with >1 arg = scalar
            _ => false,
        };

        // Return appropriate expression type
        // FunctionIdentifier handles SQL:1999 case normalization internally
        // (canonical lowercase for comparison, display preserves original case)
        let func_id = vibesql_ast::FunctionIdentifier::new(&first);
        if is_aggregate {
            // DISTINCT aggregates must have exactly one argument (SQLite compatibility)
            if distinct && args.len() != 1 {
                return Err(ParseError {
                    message: "DISTINCT aggregates must have exactly one argument".to_string(),
                });
            }
            Ok(Some(vibesql_ast::Expression::AggregateFunction {
                name: func_id,
                distinct,
                args,
                order_by,
                filter,
            }))
        } else {
            // ORDER BY and FILTER are only allowed in aggregate functions
            if order_by.is_some() {
                return Err(ParseError {
                    message: format!(
                        "ORDER BY may not be used with non-aggregate {}()",
                        first.to_uppercase()
                    ),
                });
            }
            if filter.is_some() {
                return Err(ParseError {
                    message: format!(
                        "FILTER may not be used with non-aggregate {}()",
                        first.to_uppercase()
                    ),
                });
            }
            Ok(Some(vibesql_ast::Expression::Function { name: func_id, args, character_unit }))
        }
    }

    /// Parse a `RAISE(...)` trigger-program expression.
    ///
    /// The opening `RAISE` identifier and the `(` have already been consumed.
    /// Accepts SQLite's four forms:
    /// - `RAISE(ABORT, error-message)`
    /// - `RAISE(FAIL, error-message)`
    /// - `RAISE(ROLLBACK, error-message)`
    /// - `RAISE(IGNORE)`
    ///
    /// The error-message is required for ABORT/FAIL/ROLLBACK and forbidden for
    /// IGNORE (matching SQLite, which reports a `near ","`/`near ")"` syntax
    /// error for the mismatched forms).
    fn parse_raise_expression(
        &mut self,
    ) -> Result<vibesql_ast::Expression, ParseError> {
        use vibesql_ast::RaiseAction;

        // SQLite only permits RAISE() inside a trigger-program (a
        // `CREATE TRIGGER` body) and rejects it at prepare/parse time
        // everywhere else, e.g. `SELECT raise(ABORT, 'x')` ->
        // `RAISE() may only be used within a trigger-program`. Match that:
        // reject at parse time unless we are parsing a trigger body.
        if !self.in_trigger_body {
            return Err(ParseError {
                message: "RAISE() may only be used within a trigger-program".to_string(),
            });
        }

        // First argument: one of the conflict-resolution keywords.
        let action = match self.peek() {
            Token::Keyword { keyword: Keyword::Abort, .. } => RaiseAction::Abort,
            Token::Keyword { keyword: Keyword::Fail, .. } => RaiseAction::Fail,
            Token::Keyword { keyword: Keyword::Rollback, .. } => RaiseAction::Rollback,
            Token::Keyword { keyword: Keyword::Ignore, .. } => RaiseAction::Ignore,
            other => {
                return Err(ParseError {
                    message: format!(
                        "near \"{}\": syntax error (expected ABORT, FAIL, ROLLBACK, or IGNORE in RAISE())",
                        other
                    ),
                });
            }
        };
        self.advance(); // consume the action keyword

        let error_message = if matches!(action, RaiseAction::Ignore) {
            // RAISE(IGNORE) takes no message.
            self.expect_token(Token::RParen)?;
            None
        } else {
            // RAISE(ABORT|FAIL|ROLLBACK, error-message) requires a message.
            self.expect_token(Token::Comma)?;
            let message = self.parse_expression()?;
            self.expect_token(Token::RParen)?;
            Some(Box::new(message))
        };

        Ok(vibesql_ast::Expression::Raise { action, error_message })
    }
}