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
use super::*;

// ========================================================================
// INSERT Statement Tests
// ========================================================================

#[test]
fn test_parse_insert_basic() {
    let result = Parser::parse_sql("INSERT INTO users (id, name) VALUES (1, 'Alice');");
    assert!(result.is_ok());
    let stmt = result.unwrap();

    match stmt {
        vibesql_ast::Statement::Insert(insert) => {
            assert_eq!(insert.table_name, "users");
            assert_eq!(insert.columns.len(), 2);
            assert_eq!(insert.columns[0], "id");
            assert_eq!(insert.columns[1], "name");
            match &insert.source {
                vibesql_ast::InsertSource::Values(values) => {
                    assert_eq!(values.len(), 1); // One row
                    assert_eq!(values[0].len(), 2); // Two values
                }
                _ => panic!("Expected VALUES source"),
            }
        }
        _ => panic!("Expected INSERT statement"),
    }
}

#[test]
fn test_insert_with_default() {
    let result = Parser::parse_sql("INSERT INTO users (id, name) VALUES (DEFAULT, 'Alice');");
    assert!(result.is_ok());
    let stmt = result.unwrap();

    match stmt {
        vibesql_ast::Statement::Insert(insert) => {
            assert_eq!(insert.table_name, "users");
            match &insert.source {
                vibesql_ast::InsertSource::Values(values) => {
                    assert_eq!(values.len(), 1); // One row
                    assert_eq!(values[0].len(), 2); // Two values
                                                    // Check that first value is DEFAULT
                    match &values[0][0] {
                        vibesql_ast::Expression::Default => {
                            // Success - parsed as Default expression
                        }
                        _ => panic!("Expected DEFAULT expression, got {:?}", values[0][0]),
                    }
                    // Check that second value is a string literal
                    match &values[0][1] {
                        vibesql_ast::Expression::Literal(vibesql_types::SqlValue::Varchar(s)) => {
                            assert_eq!(s.as_str(), "Alice");
                        }
                        _ => panic!("Expected string literal 'Alice', got {:?}", values[0][1]),
                    }
                }
                _ => panic!("Expected VALUES source"),
            }
        }
        _ => panic!("Expected INSERT statement"),
    }
}

#[test]
fn test_insert_multiple_defaults() {
    let result = Parser::parse_sql("INSERT INTO t VALUES (DEFAULT, DEFAULT);");
    assert!(result.is_ok());
    let stmt = result.unwrap();

    match stmt {
        vibesql_ast::Statement::Insert(insert) => {
            assert_eq!(insert.table_name, "t");
            match &insert.source {
                vibesql_ast::InsertSource::Values(values) => {
                    assert_eq!(values.len(), 1); // One row
                    assert_eq!(values[0].len(), 2); // Two values
                                                    // Check that both values are DEFAULT
                    match &values[0][0] {
                        vibesql_ast::Expression::Default => {
                            // Success - parsed as Default expression
                        }
                        _ => panic!(
                            "Expected DEFAULT expression for first value, got {:?}",
                            values[0][0]
                        ),
                    }
                    match &values[0][1] {
                        vibesql_ast::Expression::Default => {
                            // Success - parsed as Default expression
                        }
                        _ => panic!(
                            "Expected DEFAULT expression for second value, got {:?}",
                            values[0][1]
                        ),
                    }
                }
                _ => panic!("Expected VALUES source"),
            }
        }
        _ => panic!("Expected INSERT statement"),
    }
}

#[test]
fn test_parse_insert_multiple_rows() {
    let result = Parser::parse_sql("INSERT INTO users (id, name) VALUES (1, 'Alice'), (2, 'Bob');");
    assert!(result.is_ok());
    let stmt = result.unwrap();

    match stmt {
        vibesql_ast::Statement::Insert(insert) => {
            assert_eq!(insert.table_name, "users");
            match &insert.source {
                vibesql_ast::InsertSource::Values(values) => {
                    assert_eq!(values.len(), 2); // Two rows
                }
                _ => panic!("Expected VALUES source"),
            }
        }
        _ => panic!("Expected INSERT statement"),
    }
}

#[test]
fn test_parse_insert_with_default() {
    let result = Parser::parse_sql("INSERT INTO users (id, name) VALUES (DEFAULT, 'Alice');");
    assert!(result.is_ok());
    let stmt = result.unwrap();

    match stmt {
        vibesql_ast::Statement::Insert(insert) => {
            assert_eq!(insert.table_name, "users");
            assert_eq!(insert.columns.len(), 2);
            match &insert.source {
                vibesql_ast::InsertSource::Values(values) => {
                    assert_eq!(values.len(), 1);
                    assert_eq!(values[0].len(), 2);
                    // First value should be DEFAULT
                    assert!(matches!(values[0][0], vibesql_ast::Expression::Default));
                    // Second value should be a literal
                    assert!(matches!(values[0][1], vibesql_ast::Expression::Literal(_)));
                }
                _ => panic!("Expected VALUES source"),
            }
        }
        _ => panic!("Expected INSERT statement"),
    }
}

#[test]
fn test_parse_insert_all_defaults() {
    let result = Parser::parse_sql("INSERT INTO users (id, name) VALUES (DEFAULT, DEFAULT);");
    assert!(result.is_ok());
    let stmt = result.unwrap();

    match stmt {
        vibesql_ast::Statement::Insert(insert) => {
            match &insert.source {
                vibesql_ast::InsertSource::Values(values) => {
                    assert_eq!(values.len(), 1);
                    assert_eq!(values[0].len(), 2);
                    // Both values should be DEFAULT
                    assert!(matches!(values[0][0], vibesql_ast::Expression::Default));
                    assert!(matches!(values[0][1], vibesql_ast::Expression::Default));
                }
                _ => panic!("Expected VALUES source"),
            }
        }
        _ => panic!("Expected INSERT statement"),
    }
}

/// Test that INSERT statement can include rowid in column list (SQLite compatibility)
#[test]
fn test_parse_insert_with_rowid_column() {
    let result = Parser::parse_sql("INSERT INTO t1 (rowid, a, b) VALUES (100, 1, 'hello');");
    assert!(result.is_ok(), "Failed to parse INSERT with rowid: {:?}", result.err());
    let stmt = result.unwrap();

    match stmt {
        vibesql_ast::Statement::Insert(insert) => {
            assert_eq!(insert.table_name, "t1");
            assert_eq!(insert.columns.len(), 3);
            assert_eq!(insert.columns[0], "rowid");
            assert_eq!(insert.columns[1], "a");
            assert_eq!(insert.columns[2], "b");
            match &insert.source {
                vibesql_ast::InsertSource::Values(values) => {
                    assert_eq!(values.len(), 1); // One row
                    assert_eq!(values[0].len(), 3); // Three values
                }
                _ => panic!("Expected VALUES source"),
            }
        }
        _ => panic!("Expected INSERT statement"),
    }
}

/// Test that REPLACE statement can include rowid in column list (SQLite compatibility)
#[test]
fn test_parse_replace_with_rowid_column() {
    let result = Parser::parse_sql("REPLACE INTO t1 (rowid, a) VALUES (100, 1);");
    assert!(result.is_ok(), "Failed to parse REPLACE with rowid: {:?}", result.err());
    let stmt = result.unwrap();

    match stmt {
        vibesql_ast::Statement::Insert(insert) => {
            assert_eq!(insert.table_name, "t1");
            assert_eq!(insert.columns.len(), 2);
            assert_eq!(insert.columns[0], "rowid");
            assert_eq!(insert.columns[1], "a");
        }
        _ => panic!("Expected INSERT statement"),
    }
}

// ========================================================================
// RETURNING Clause Tests (SQLite 3.35.0+, issue #5263)
// ========================================================================

#[test]
fn test_parse_insert_returning_star() {
    let result = Parser::parse_sql("INSERT INTO t1(b) VALUES ('x') RETURNING *;");
    assert!(result.is_ok(), "RETURNING * should parse: {:?}", result.err());
    match result.unwrap() {
        vibesql_ast::Statement::Insert(insert) => {
            let returning = insert.returning.expect("returning clause should be captured");
            assert_eq!(returning.len(), 1);
            assert!(matches!(returning[0], vibesql_ast::SelectItem::Wildcard { .. }));
        }
        _ => panic!("Expected INSERT statement"),
    }
}

#[test]
fn test_parse_insert_returning_expression_list() {
    let result = Parser::parse_sql("INSERT INTO t1 VALUES (1, 2) RETURNING a, b + 1;");
    assert!(result.is_ok(), "RETURNING expr list should parse: {:?}", result.err());
    match result.unwrap() {
        vibesql_ast::Statement::Insert(insert) => {
            let returning = insert.returning.expect("returning clause should be captured");
            assert_eq!(returning.len(), 2);
            assert!(matches!(returning[0], vibesql_ast::SelectItem::Expression { .. }));
            assert!(matches!(returning[1], vibesql_ast::SelectItem::Expression { .. }));
        }
        _ => panic!("Expected INSERT statement"),
    }
}

#[test]
fn test_parse_insert_returning_with_alias() {
    let result =
        Parser::parse_sql("INSERT INTO t1 VALUES (1) RETURNING a AS new_a, a + 1 incremented;");
    assert!(result.is_ok(), "RETURNING aliases should parse: {:?}", result.err());
    match result.unwrap() {
        vibesql_ast::Statement::Insert(insert) => {
            let returning = insert.returning.expect("returning clause should be captured");
            assert_eq!(returning.len(), 2);
            match &returning[0] {
                vibesql_ast::SelectItem::Expression { alias, .. } => {
                    assert_eq!(alias.as_deref(), Some("new_a"));
                }
                other => panic!("Expected expression item, got {:?}", other),
            }
            match &returning[1] {
                vibesql_ast::SelectItem::Expression { alias, .. } => {
                    assert_eq!(alias.as_deref(), Some("incremented"));
                }
                other => panic!("Expected expression item, got {:?}", other),
            }
        }
        _ => panic!("Expected INSERT statement"),
    }
}

#[test]
fn test_parse_insert_returning_after_on_conflict() {
    let result = Parser::parse_sql(
        "INSERT INTO t1(a, b) VALUES (1, 2) ON CONFLICT(a) DO UPDATE SET b = 3 RETURNING *;",
    );
    assert!(result.is_ok(), "RETURNING after ON CONFLICT should parse: {:?}", result.err());
    match result.unwrap() {
        vibesql_ast::Statement::Insert(insert) => {
            assert!(insert.on_conflict.is_some());
            let returning = insert.returning.expect("returning clause should be captured");
            assert_eq!(returning.len(), 1);
        }
        _ => panic!("Expected INSERT statement"),
    }
}

#[test]
fn test_parse_insert_returning_with_cte() {
    let result = Parser::parse_sql(
        "WITH src AS (SELECT 1 AS x) INSERT INTO t1 SELECT x FROM src RETURNING *;",
    );
    assert!(result.is_ok(), "RETURNING with CTE should parse: {:?}", result.err());
    match result.unwrap() {
        vibesql_ast::Statement::Insert(insert) => {
            assert!(insert.with_clause.is_some());
            let returning = insert.returning.expect("returning clause should be captured");
            assert_eq!(returning.len(), 1);
        }
        _ => panic!("Expected INSERT statement"),
    }
}

#[test]
fn test_parse_replace_into_returning() {
    let result = Parser::parse_sql("REPLACE INTO t1(a, b) VALUES (1, 'x') RETURNING a, b;");
    assert!(result.is_ok(), "REPLACE INTO RETURNING should parse: {:?}", result.err());
    match result.unwrap() {
        vibesql_ast::Statement::Insert(insert) => {
            assert_eq!(
                insert.conflict_clause,
                Some(vibesql_ast::ConflictClause::Replace),
                "REPLACE INTO should set the Replace conflict clause"
            );
            let returning = insert.returning.expect("returning clause should be captured");
            assert_eq!(returning.len(), 2);
        }
        _ => panic!("Expected INSERT statement"),
    }
}

#[test]
fn test_parse_insert_returning_arena_parser() {
    // The CLI path goes through parse_with_arena_fallback; make sure the
    // arena parser captures RETURNING too.
    let result = crate::parse_with_arena_fallback("INSERT INTO t1(b) VALUES ('x') RETURNING a, b");
    assert!(result.is_ok(), "arena parser should handle RETURNING: {:?}", result.err());
    match result.unwrap() {
        vibesql_ast::Statement::Insert(insert) => {
            let returning = insert.returning.expect("returning clause should be captured");
            assert_eq!(returning.len(), 2);
        }
        _ => panic!("Expected INSERT statement"),
    }
}

#[test]
fn test_parse_replace_returning_arena_parser() {
    let result =
        crate::parse_with_arena_fallback("REPLACE INTO t1(a, b) VALUES (1, 'x') RETURNING *");
    assert!(result.is_ok(), "arena parser should handle REPLACE RETURNING: {:?}", result.err());
    match result.unwrap() {
        vibesql_ast::Statement::Insert(insert) => {
            let returning = insert.returning.expect("returning clause should be captured");
            assert_eq!(returning.len(), 1);
        }
        _ => panic!("Expected INSERT statement"),
    }
}

#[test]
fn test_parse_insert_without_returning_is_none() {
    let result = Parser::parse_sql("INSERT INTO t1 VALUES (1);");
    match result.unwrap() {
        vibesql_ast::Statement::Insert(insert) => assert!(insert.returning.is_none()),
        _ => panic!("Expected INSERT statement"),
    }
}

#[test]
fn test_parse_insert_select_returning() {
    // The RETURNING keyword terminates the embedded SELECT source and belongs
    // to the outer INSERT (issue #5263); this must keep working after the
    // bare-SELECT RETURNING fix (issue #5271).
    let result = Parser::parse_sql("INSERT INTO t1 SELECT a FROM t2 RETURNING a;");
    assert!(result.is_ok(), "INSERT ... SELECT ... RETURNING should parse: {:?}", result.err());
    match result.unwrap() {
        vibesql_ast::Statement::Insert(insert) => {
            assert!(matches!(insert.source, vibesql_ast::InsertSource::Select(_)));
            let returning = insert.returning.expect("returning clause should be captured");
            assert_eq!(returning.len(), 1);
        }
        _ => panic!("Expected INSERT statement"),
    }
}

#[test]
fn test_parse_insert_compound_select_returning() {
    // RETURNING after a compound SELECT source: the keyword follows the right
    // side of the set operation, so the allow_returning flag must propagate
    // through nested set-operation parsing (issue #5271).
    let result = Parser::parse_sql("INSERT INTO t1 SELECT 1 UNION SELECT 2 RETURNING a;");
    assert!(result.is_ok(), "compound SELECT source + RETURNING should parse: {:?}", result.err());
    match result.unwrap() {
        vibesql_ast::Statement::Insert(insert) => {
            let returning = insert.returning.expect("returning clause should be captured");
            assert_eq!(returning.len(), 1);
        }
        _ => panic!("Expected INSERT statement"),
    }
}

#[test]
fn test_parse_insert_compound_values_returning() {
    let result = Parser::parse_sql("INSERT INTO t1 VALUES (1) UNION VALUES (2) RETURNING a;");
    assert!(result.is_ok(), "compound VALUES source + RETURNING should parse: {:?}", result.err());
    match result.unwrap() {
        vibesql_ast::Statement::Insert(insert) => {
            let returning = insert.returning.expect("returning clause should be captured");
            assert_eq!(returning.len(), 1);
        }
        _ => panic!("Expected INSERT statement"),
    }
}