radixdb-sql 1.1.0

SQL syntax contracts and parser for RadixDB
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
// Copyright 2026 RadixDB 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.

use super::*;

/// Parsed conflict clause (ON DUPLICATE KEY UPDATE or ON CONFLICT)
#[derive(Default)]
struct ConflictClause {
    on_duplicate: bool,
    update_columns: Vec<Identifier>,
    update_expressions: Vec<Expression>,
    do_nothing: bool,
    conflict_target: Vec<Identifier>,
}

impl Parser {
    /// Parse an INSERT statement
    pub(super) fn parse_insert_statement(&mut self) -> Option<InsertStatement> {
        let token = self.cur_token.clone();

        // Expect INTO
        if !self.expect_keyword("INTO") {
            return None;
        }

        // Parse table name
        if !self.expect_peek_identifier_like() {
            return None;
        }
        let table_name = self.parse_relation_identifier_current()?;

        // Parse optional column list
        let mut columns = Vec::new();
        if self.peek_token_is_punctuator("(") {
            self.next_token(); // consume (
            columns = self.parse_identifier_list();
            if self.peek_token_is_punctuator(".") {
                self.add_error(format!(
                    "{}: navigation paths cannot be INSERT target columns",
                    NavigationErrorCode::ReadOnly
                ));
                return None;
            }
            if !self.expect_peek(TokenType::Punctuator) || self.cur_token.literal != ")" {
                self.add_error(format!("expected ')' at {}", self.cur_token.position));
                return None;
            }
        }

        // Check if next is VALUES, SELECT, or WITH (CTE)
        if self.peek_token_is_keyword("SELECT") {
            // INSERT INTO ... SELECT
            self.next_token(); // consume SELECT (we're now on SELECT)
            let select_stmt = self.parse_select_statement()?;

            // Check for ON DUPLICATE KEY UPDATE or ON CONFLICT
            let conflict = self.parse_conflict_clause();

            // Parse optional RETURNING clause
            let returning = self.parse_returning_clause();

            return Some(InsertStatement {
                token,
                table_name,
                columns,
                values: Vec::new(),
                select: Some(Box::new(select_stmt)),
                on_duplicate: conflict.on_duplicate,
                update_columns: conflict.update_columns,
                update_expressions: conflict.update_expressions,
                do_nothing: conflict.do_nothing,
                conflict_target: conflict.conflict_target,
                returning,
            });
        }

        if self.peek_token_is_keyword("WITH") {
            // INSERT INTO ... WITH ... SELECT
            self.next_token(); // consume WITH
            let with_clause = self.parse_with_clause()?;

            // Expect SELECT after WITH clause
            if !self.expect_keyword("SELECT") {
                return None;
            }

            // Parse the SELECT statement
            let mut select_stmt = self.parse_select_statement()?;

            // Attach the WITH clause to the SELECT
            select_stmt.with = Some(with_clause);

            // Check for ON DUPLICATE KEY UPDATE or ON CONFLICT
            let conflict = self.parse_conflict_clause();

            // Parse optional RETURNING clause
            let returning = self.parse_returning_clause();

            return Some(InsertStatement {
                token,
                table_name,
                columns,
                values: Vec::new(),
                select: Some(Box::new(select_stmt)),
                on_duplicate: conflict.on_duplicate,
                update_columns: conflict.update_columns,
                update_expressions: conflict.update_expressions,
                do_nothing: conflict.do_nothing,
                conflict_target: conflict.conflict_target,
                returning,
            });
        }

        // Expect VALUES
        if !self.expect_keyword("VALUES") {
            return None;
        }

        // Parse value lists
        let values = self.parse_value_lists()?;

        // Check for ON DUPLICATE KEY UPDATE or ON CONFLICT
        let conflict = self.parse_conflict_clause();

        // Parse optional RETURNING clause
        let returning = self.parse_returning_clause();

        Some(InsertStatement {
            token,
            table_name,
            columns,
            values,
            select: None,
            on_duplicate: conflict.on_duplicate,
            update_columns: conflict.update_columns,
            update_expressions: conflict.update_expressions,
            do_nothing: conflict.do_nothing,
            conflict_target: conflict.conflict_target,
            returning,
        })
    }

    /// Parse conflict clause: ON DUPLICATE KEY UPDATE (MySQL) or ON CONFLICT (PostgreSQL)
    /// Returns ConflictClause with all parsed fields.
    fn parse_conflict_clause(&mut self) -> ConflictClause {
        if !self.peek_token_is_keyword("ON") {
            return ConflictClause::default();
        }
        self.next_token(); // consume ON

        // Determine MySQL vs PostgreSQL style
        if self.peek_token_is_keyword("DUPLICATE") {
            // MySQL: ON DUPLICATE KEY UPDATE ...
            self.next_token(); // consume DUPLICATE
            if !self.expect_keyword("KEY") {
                return ConflictClause::default();
            }
            if !self.expect_keyword("UPDATE") {
                return ConflictClause::default();
            }
            let (update_columns, update_expressions) = self.parse_update_assignments();
            ConflictClause {
                on_duplicate: true,
                update_columns,
                update_expressions,
                ..Default::default()
            }
        } else if self.peek_token_is_keyword("CONFLICT") {
            // PostgreSQL: ON CONFLICT [(columns)] DO UPDATE SET ... | DO NOTHING
            self.next_token(); // consume CONFLICT

            // Optional conflict target: (col1, col2, ...)
            let conflict_target = if self.peek_token_is_punctuator("(") {
                self.next_token(); // consume (
                let cols = self.parse_identifier_list();
                if self.peek_token_is_punctuator(".") {
                    self.add_error(format!(
                        "{}: navigation paths cannot be conflict target columns",
                        NavigationErrorCode::ReadOnly
                    ));
                    return ConflictClause::default();
                }
                if !self.expect_peek(TokenType::Punctuator) || self.cur_token.literal != ")" {
                    self.add_error(format!(
                        "expected ')' after conflict target at {}",
                        self.cur_token.position
                    ));
                    return ConflictClause::default();
                }
                cols
            } else {
                Vec::new()
            };

            // Expect DO
            if !self.expect_keyword("DO") {
                return ConflictClause::default();
            }

            // DO NOTHING or DO UPDATE SET
            if self.peek_token_is_keyword("NOTHING") {
                self.next_token(); // consume NOTHING
                ConflictClause {
                    do_nothing: true,
                    conflict_target,
                    ..Default::default()
                }
            } else if self.peek_token_is_keyword("UPDATE") {
                self.next_token(); // consume UPDATE
                if !self.expect_keyword("SET") {
                    return ConflictClause::default();
                }
                let (update_columns, update_expressions) = self.parse_update_assignments();
                ConflictClause {
                    on_duplicate: true,
                    update_columns,
                    update_expressions,
                    conflict_target,
                    do_nothing: false,
                }
            } else {
                self.add_error(format!(
                    "expected NOTHING or UPDATE after DO, got {}",
                    Self::format_token_for_error(&self.peek_token)
                ));
                ConflictClause::default()
            }
        } else {
            self.add_error(format!(
                "expected DUPLICATE or CONFLICT after ON, got {}",
                Self::format_token_for_error(&self.peek_token)
            ));
            ConflictClause::default()
        }
    }

    /// Parse column = expression assignment list (used by both MySQL and PostgreSQL upsert)
    pub(super) fn parse_update_assignments(&mut self) -> (Vec<Identifier>, Vec<Expression>) {
        let mut update_columns = Vec::new();
        let mut update_expressions = Vec::new();

        loop {
            // Accept both identifiers and keywords as column names (e.g., level, key, order)
            if !self.peek_token_is(TokenType::Identifier) && !self.peek_token_is(TokenType::Keyword)
            {
                self.add_error(format!(
                    "expected column name, got {}",
                    Self::format_token_for_error(&self.peek_token)
                ));
                return (update_columns, update_expressions);
            }
            self.next_token();
            update_columns.push(self.cur_token_as_column_identifier());

            if self.peek_token_is_punctuator(".") {
                self.add_error(format!(
                    "{}: navigation paths cannot be assignment targets",
                    NavigationErrorCode::ReadOnly
                ));
                return (update_columns, update_expressions);
            }

            if !self.expect_peek(TokenType::Operator) || self.cur_token.literal != "=" {
                self.add_error(format!("expected '=' at {}", self.cur_token.position));
                return (update_columns, update_expressions);
            }

            self.next_token();
            if let Some(expr) = self.parse_expression(Precedence::Lowest) {
                update_expressions.push(expr);
            } else {
                return (update_columns, update_expressions);
            }

            if !self.peek_token_is_punctuator(",") {
                break;
            }
            self.next_token(); // consume comma
        }

        (update_columns, update_expressions)
    }

    /// Parse RETURNING clause for INSERT/UPDATE/DELETE statements
    pub(super) fn parse_returning_clause(&mut self) -> Vec<Expression> {
        if !self.peek_token_is_keyword("RETURNING") {
            return Vec::new();
        }

        self.next_token(); // consume RETURNING

        // Parse the expression list
        self.parse_expression_list()
    }

    /// Parse value lists for INSERT
    pub(super) fn parse_value_lists(&mut self) -> Option<Vec<Vec<Expression>>> {
        // Pre-allocate for common case (single row INSERT)
        let mut value_lists = Vec::with_capacity(1);

        // Expect (
        if !self.expect_peek(TokenType::Punctuator) || self.cur_token.literal != "(" {
            self.add_error(format!("expected '(' at {}", self.cur_token.position));
            return None;
        }

        // Parse first value list
        let values = self.parse_expression_list();
        value_lists.push(values);

        // Expect )
        if !self.expect_peek(TokenType::Punctuator) || self.cur_token.literal != ")" {
            self.add_error(format!("expected ')' at {}", self.cur_token.position));
            return None;
        }

        // Parse additional value lists
        while self.peek_token_is_punctuator(",") {
            self.next_token(); // consume comma

            if !self.expect_peek(TokenType::Punctuator) || self.cur_token.literal != "(" {
                self.add_error(format!("expected '(' at {}", self.cur_token.position));
                return None;
            }

            let values = self.parse_expression_list();
            value_lists.push(values);

            if !self.expect_peek(TokenType::Punctuator) || self.cur_token.literal != ")" {
                self.add_error(format!("expected ')' at {}", self.cur_token.position));
                return None;
            }
        }

        Some(value_lists)
    }

    /// Parse an UPDATE statement
    pub(super) fn parse_update_statement(&mut self) -> Option<UpdateStatement> {
        let token = self.cur_token.clone();

        // Parse table name
        if !self.expect_peek_identifier_like() {
            return None;
        }
        let table_name = self.parse_relation_identifier_current()?;

        // Expect SET
        if !self.expect_keyword("SET") {
            return None;
        }

        // Parse column-value pairs
        let mut updates = FxHashMap::default();
        loop {
            // Accept both identifiers and keywords as column names (e.g., level, key, order)
            if !self.peek_token_is(TokenType::Identifier) && !self.peek_token_is(TokenType::Keyword)
            {
                self.add_error(format!(
                    "expected column name in SET clause, got {}",
                    Self::format_token_for_error(&self.peek_token)
                ));
                return None;
            }
            self.next_token();
            let column_name = self.cur_token_as_column_identifier();
            let column_name = column_name.value;

            if self.peek_token_is_punctuator(".") {
                self.add_error(format!(
                    "{}: navigation paths cannot be assignment targets",
                    NavigationErrorCode::ReadOnly
                ));
                return None;
            }

            if !self.expect_peek(TokenType::Operator) || self.cur_token.literal != "=" {
                self.add_error(format!("expected '=' at {}", self.cur_token.position));
                return None;
            }

            self.next_token();
            let value_expr = self.parse_expression(Precedence::Lowest)?;
            if updates
                .keys()
                .any(|existing: &SmartString| existing.eq_ignore_ascii_case(&column_name))
            {
                self.add_error(format!(
                    "duplicate column '{}' in UPDATE SET clause",
                    column_name
                ));
                return None;
            }
            updates.insert(column_name, value_expr);

            if !self.peek_token_is_punctuator(",") {
                break;
            }
            self.next_token(); // consume comma
        }

        // Parse WHERE clause
        let where_clause = if self.peek_token_is_keyword("WHERE") {
            self.next_token(); // consume WHERE
            self.current_clause = "WHERE".to_string();
            self.next_token();
            Some(Box::new(self.parse_expression(Precedence::Lowest)?))
        } else {
            None
        };

        self.current_clause.clear();

        // Parse optional RETURNING clause
        let returning = self.parse_returning_clause();

        Some(UpdateStatement {
            token,
            table_name,
            updates,
            where_clause,
            returning,
        })
    }

    /// Parse a DELETE statement
    /// DELETE FROM table [AS alias] [WHERE condition] [RETURNING ...]
    pub(super) fn parse_delete_statement(&mut self) -> Option<DeleteStatement> {
        let token = self.cur_token.clone();

        // Expect FROM
        if !self.expect_keyword("FROM") {
            return None;
        }

        // Parse table name
        if !self.peek_token_is_identifier_like() {
            self.add_error(format!(
                "expected table name after DELETE FROM, got {}",
                Self::format_token_for_error(&self.peek_token)
            ));
            return None;
        }
        self.next_token();
        let table_name = self.parse_relation_identifier_current()?;

        // Parse optional alias (AS alias or just alias)
        let alias = if self.peek_token_is_keyword("AS") {
            self.next_token(); // consume AS
            if !self.expect_peek_identifier_like() {
                return None;
            }
            Some(Identifier::new(
                self.cur_token.clone(),
                self.cur_token.literal.clone(),
            ))
        } else if self.peek_token_is_identifier_like()
            && !self.peek_token_is_keyword("WHERE")
            && !self.peek_token_is_keyword("RETURNING")
        {
            // Alias without AS keyword (e.g., DELETE FROM users u WHERE ...)
            self.next_token();
            Some(Identifier::new(
                self.cur_token.clone(),
                self.cur_token.literal.clone(),
            ))
        } else {
            None
        };

        // Parse WHERE clause
        let where_clause = if self.peek_token_is_keyword("WHERE") {
            self.next_token(); // consume WHERE
            self.current_clause = "WHERE".to_string();
            self.next_token();
            Some(Box::new(self.parse_expression(Precedence::Lowest)?))
        } else {
            None
        };

        self.current_clause.clear();

        // Parse optional RETURNING clause
        let returning = self.parse_returning_clause();

        Some(DeleteStatement {
            token,
            table_name,
            alias,
            where_clause,
            returning,
        })
    }

    /// Parse a TRUNCATE statement
    /// TRUNCATE TABLE table_name or TRUNCATE table_name
    pub(super) fn parse_truncate_statement(&mut self) -> Option<TruncateStatement> {
        let token = self.cur_token.clone();

        // Optional TABLE keyword
        if self.peek_token_is_keyword("TABLE") {
            self.next_token(); // consume TABLE
        }

        // Parse table name
        if !self.expect_peek_identifier_like() {
            return None;
        }
        let table_name = self.parse_relation_identifier_current()?;

        Some(TruncateStatement { token, table_name })
    }

    /// Parse a VACUUM statement
    /// VACUUM [table_name]
    pub(super) fn parse_vacuum_statement(&mut self) -> Option<VacuumStatement> {
        let token = self.cur_token.clone();

        // Optional table name
        let table_name = if self.peek_token_is_identifier_like() {
            self.next_token();
            Some(Identifier::new(
                self.cur_token.clone(),
                self.cur_token.literal.clone(),
            ))
        } else {
            None
        };

        Some(VacuumStatement { token, table_name })
    }
}