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

#[test]
fn test_parse_select_with_where() {
    let result = Parser::parse_sql("SELECT name FROM users WHERE id = 1;");
    assert!(result.is_ok());
    let stmt = result.unwrap();

    match stmt {
        vibesql_ast::Statement::Select(select) => {
            assert!(select.where_clause.is_some());
            match &select.where_clause.as_ref().unwrap() {
                vibesql_ast::Expression::BinaryOp { op, left, right } => {
                    assert_eq!(*op, vibesql_ast::BinaryOperator::Equal);
                    match **left {
                        vibesql_ast::Expression::ColumnRef(ref col_id)
                            if col_id.column_canonical() == "id" => {}
                        _ => panic!("Expected id column in WHERE"),
                    }
                    match **right {
                        vibesql_ast::Expression::Literal(vibesql_types::SqlValue::Integer(1)) => {}
                        _ => panic!("Expected Integer(1) in WHERE"),
                    }
                }
                _ => panic!("Expected BinaryOp in WHERE clause"),
            }
        }
        _ => panic!("Expected SELECT statement"),
    }
}

#[test]
fn test_parse_select_with_alias() {
    let result = Parser::parse_sql("SELECT id AS user_id FROM users;");
    assert!(result.is_ok());
    let stmt = result.unwrap();

    match stmt {
        vibesql_ast::Statement::Select(select) => {
            assert_eq!(select.select_list.len(), 1);
            match &select.select_list[0] {
                vibesql_ast::SelectItem::Expression { alias, .. } => {
                    assert_eq!(alias.as_ref().unwrap(), "user_id");
                }
                _ => panic!("Expected Expression select item"),
            }
        }
        _ => panic!("Expected SELECT statement"),
    }
}

#[test]
fn test_parse_select_with_alias_without_as() {
    let result = Parser::parse_sql("SELECT id user_id FROM users;");
    assert!(result.is_ok());
    let stmt = result.unwrap();

    match stmt {
        vibesql_ast::Statement::Select(select) => {
            assert_eq!(select.select_list.len(), 1);
            match &select.select_list[0] {
                vibesql_ast::SelectItem::Expression { alias, .. } => {
                    assert_eq!(alias.as_ref().unwrap(), "user_id");
                }
                _ => panic!("Expected Expression select item"),
            }
        }
        _ => panic!("Expected SELECT statement"),
    }
}

#[test]
fn test_parse_select_mixed_aliases() {
    let result = Parser::parse_sql("SELECT id AS user_id, name username, age FROM users;");
    assert!(result.is_ok());
    let stmt = result.unwrap();

    match stmt {
        vibesql_ast::Statement::Select(select) => {
            assert_eq!(select.select_list.len(), 3);

            // First column: id AS user_id
            match &select.select_list[0] {
                vibesql_ast::SelectItem::Expression { alias, .. } => {
                    assert_eq!(alias.as_ref().unwrap(), "user_id");
                }
                _ => panic!("Expected Expression select item"),
            }

            // Second column: name username (without AS)
            match &select.select_list[1] {
                vibesql_ast::SelectItem::Expression { alias, .. } => {
                    assert_eq!(alias.as_ref().unwrap(), "username");
                }
                _ => panic!("Expected Expression select item"),
            }

            // Third column: age (no alias)
            match &select.select_list[2] {
                vibesql_ast::SelectItem::Expression { alias, .. } => {
                    assert!(alias.is_none());
                }
                _ => panic!("Expected Expression select item"),
            }
        }
        _ => panic!("Expected SELECT statement"),
    }
}

/// SQLite compatibility: single-quoted strings can be used as column aliases
/// e.g., SELECT 1 AS 'a' - the 'a' is treated as an identifier, not a string literal
#[test]
fn test_parse_select_with_single_quoted_alias() {
    let result = Parser::parse_sql("SELECT 1 AS 'a', 'hello' AS 'b', 2 AS 'c';");
    assert!(result.is_ok());
    let stmt = result.unwrap();

    match stmt {
        vibesql_ast::Statement::Select(select) => {
            assert_eq!(select.select_list.len(), 3);

            // First column: 1 AS 'a'
            match &select.select_list[0] {
                vibesql_ast::SelectItem::Expression { alias, .. } => {
                    assert_eq!(alias.as_ref().unwrap(), "a");
                }
                _ => panic!("Expected Expression select item"),
            }

            // Second column: 'hello' AS 'b'
            match &select.select_list[1] {
                vibesql_ast::SelectItem::Expression { alias, .. } => {
                    assert_eq!(alias.as_ref().unwrap(), "b");
                }
                _ => panic!("Expected Expression select item"),
            }

            // Third column: 2 AS 'c'
            match &select.select_list[2] {
                vibesql_ast::SelectItem::Expression { alias, .. } => {
                    assert_eq!(alias.as_ref().unwrap(), "c");
                }
                _ => panic!("Expected Expression select item"),
            }
        }
        _ => panic!("Expected SELECT statement"),
    }
}

#[test]
fn test_parse_precedence() {
    // Test that 1 + 2 * 3 parses as 1 + (2 * 3)
    let result = Parser::parse_sql("SELECT 1 + 2 * 3;");
    assert!(result.is_ok());
    let stmt = result.unwrap();

    match stmt {
        vibesql_ast::Statement::Select(select) => {
            match &select.select_list[0] {
                vibesql_ast::SelectItem::Expression { expr, .. } => match expr {
                    vibesql_ast::Expression::BinaryOp { op, left, right } => {
                        assert_eq!(*op, vibesql_ast::BinaryOperator::Plus);
                        // Left should be 1
                        match **left {
                            vibesql_ast::Expression::Literal(vibesql_types::SqlValue::Integer(
                                1,
                            )) => {}
                            _ => panic!("Expected left = 1"),
                        }
                        // Right should be 2 * 3
                        match **right {
                            vibesql_ast::Expression::BinaryOp {
                                op: vibesql_ast::BinaryOperator::Multiply,
                                ..
                            } => {}
                            _ => panic!("Expected right = 2 * 3"),
                        }
                    }
                    _ => panic!("Expected BinaryOp"),
                },
                _ => panic!("Expected Expression"),
            }
        }
        _ => panic!("Expected SELECT"),
    }
}

#[test]
fn test_parse_parentheses() {
    // Test that (1 + 2) * 3 parses correctly
    let result = Parser::parse_sql("SELECT (1 + 2) * 3;");
    assert!(result.is_ok());
    let stmt = result.unwrap();

    match stmt {
        vibesql_ast::Statement::Select(select) => {
            match &select.select_list[0] {
                vibesql_ast::SelectItem::Expression { expr, .. } => match expr {
                    vibesql_ast::Expression::BinaryOp { op, left, right } => {
                        assert_eq!(*op, vibesql_ast::BinaryOperator::Multiply);
                        // Left should be (1 + 2)
                        match **left {
                            vibesql_ast::Expression::BinaryOp {
                                op: vibesql_ast::BinaryOperator::Plus,
                                ..
                            } => {}
                            _ => panic!("Expected left = 1 + 2"),
                        }
                        // Right should be 3
                        match **right {
                            vibesql_ast::Expression::Literal(vibesql_types::SqlValue::Integer(
                                3,
                            )) => {}
                            _ => panic!("Expected right = 3"),
                        }
                    }
                    _ => panic!("Expected BinaryOp"),
                },
                _ => panic!("Expected Expression"),
            }
        }
        _ => panic!("Expected SELECT"),
    }
}

#[test]
fn test_parse_and_operator() {
    let result = Parser::parse_sql("SELECT * FROM users WHERE age > 18 AND status = 'active';");
    assert!(result.is_ok());
    let stmt = result.unwrap();

    match stmt {
        vibesql_ast::Statement::Select(select) => {
            assert!(select.where_clause.is_some());
            match select.where_clause.as_ref().unwrap() {
                vibesql_ast::Expression::BinaryOp { op, .. } => {
                    assert_eq!(*op, vibesql_ast::BinaryOperator::And);
                }
                _ => panic!("Expected AND expression"),
            }
        }
        _ => panic!("Expected SELECT"),
    }
}

#[test]
fn test_parse_or_operator() {
    let result =
        Parser::parse_sql("SELECT * FROM users WHERE status = 'active' OR status = 'pending';");
    assert!(result.is_ok());
    let stmt = result.unwrap();

    match stmt {
        vibesql_ast::Statement::Select(select) => {
            assert!(select.where_clause.is_some());
            match select.where_clause.as_ref().unwrap() {
                vibesql_ast::Expression::BinaryOp { op, .. } => {
                    assert_eq!(*op, vibesql_ast::BinaryOperator::Or);
                }
                _ => panic!("Expected OR expression"),
            }
        }
        _ => panic!("Expected SELECT"),
    }
}

#[test]
fn test_parse_complex_where() {
    // Test: age > 18 AND (status = 'active' OR status = 'pending')
    let result = Parser::parse_sql(
        "SELECT * FROM users WHERE age > 18 AND (status = 'active' OR status = 'pending');",
    );
    assert!(result.is_ok());
    let stmt = result.unwrap();

    match stmt {
        vibesql_ast::Statement::Select(select) => {
            assert!(select.where_clause.is_some());
            // Outer should be AND
            match select.where_clause.as_ref().unwrap() {
                vibesql_ast::Expression::BinaryOp { op, right, .. } => {
                    assert_eq!(*op, vibesql_ast::BinaryOperator::And);
                    // Right side should be OR (in parentheses)
                    match **right {
                        vibesql_ast::Expression::BinaryOp {
                            op: vibesql_ast::BinaryOperator::Or,
                            ..
                        } => {} /* Success */
                        _ => panic!("Expected OR in right side"),
                    }
                }
                _ => panic!("Expected AND expression"),
            }
        }
        _ => panic!("Expected SELECT"),
    }
}

// ========================================================================
// TYPE and SQL as Column Names in SELECT (SQLite compatibility - Issue #4187)
// ========================================================================

#[test]
fn test_select_type_column_unquoted() {
    // This is the main use case from issue #4187: querying sqlite_master.type
    let result = Parser::parse_sql("SELECT name FROM sqlite_master WHERE type = 'table';");
    assert!(result.is_ok(), "Should parse unquoted 'type' column: {:?}", result.err());
    let stmt = result.unwrap();

    match stmt {
        vibesql_ast::Statement::Select(select) => {
            assert!(select.where_clause.is_some());
            match select.where_clause.as_ref().unwrap() {
                vibesql_ast::Expression::BinaryOp { op, left, .. } => {
                    assert_eq!(*op, vibesql_ast::BinaryOperator::Equal);
                    match **left {
                        vibesql_ast::Expression::ColumnRef(ref col_id) => {
                            assert_eq!(col_id.column_canonical(), "type");
                        }
                        _ => panic!("Expected type column in WHERE, got {:?}", left),
                    }
                }
                _ => panic!("Expected BinaryOp in WHERE clause"),
            }
        }
        _ => panic!("Expected SELECT statement"),
    }
}

#[test]
fn test_select_sql_column_unquoted() {
    // Querying sqlite_master.sql column
    let result = Parser::parse_sql("SELECT sql FROM sqlite_master WHERE name = 'users';");
    assert!(result.is_ok(), "Should parse unquoted 'sql' column: {:?}", result.err());
    let stmt = result.unwrap();

    match stmt {
        vibesql_ast::Statement::Select(select) => {
            assert_eq!(select.select_list.len(), 1);
            match &select.select_list[0] {
                vibesql_ast::SelectItem::Expression { expr, .. } => match expr {
                    vibesql_ast::Expression::ColumnRef(col_id) => {
                        let column = col_id.column_canonical();
                        assert_eq!(column, "sql");
                    }
                    _ => panic!("Expected ColumnRef for sql"),
                },
                _ => panic!("Expected Expression select item"),
            }
        }
        _ => panic!("Expected SELECT statement"),
    }
}

#[test]
fn test_select_type_and_sql_columns_together() {
    // Full sqlite_master query pattern
    let result =
        Parser::parse_sql("SELECT type, name, sql FROM sqlite_master WHERE type = 'table';");
    assert!(result.is_ok(), "Should parse type and sql columns: {:?}", result.err());
    let stmt = result.unwrap();

    match stmt {
        vibesql_ast::Statement::Select(select) => {
            assert_eq!(select.select_list.len(), 3);

            // First column: type
            match &select.select_list[0] {
                vibesql_ast::SelectItem::Expression { expr, .. } => match expr {
                    vibesql_ast::Expression::ColumnRef(col_id) => {
                        let column = col_id.column_canonical();
                        assert_eq!(column, "type");
                    }
                    _ => panic!("Expected ColumnRef for type"),
                },
                _ => panic!("Expected Expression select item"),
            }

            // Third column: sql
            match &select.select_list[2] {
                vibesql_ast::SelectItem::Expression { expr, .. } => match expr {
                    vibesql_ast::Expression::ColumnRef(col_id) => {
                        let column = col_id.column_canonical();
                        assert_eq!(column, "sql");
                    }
                    _ => panic!("Expected ColumnRef for sql"),
                },
                _ => panic!("Expected Expression select item"),
            }
        }
        _ => panic!("Expected SELECT statement"),
    }
}

#[test]
fn test_select_qualified_type_column() {
    // Qualified column reference: table.type
    let result = Parser::parse_sql("SELECT sqlite_master.type FROM sqlite_master;");
    assert!(result.is_ok(), "Should parse qualified type column: {:?}", result.err());
    let stmt = result.unwrap();

    match stmt {
        vibesql_ast::Statement::Select(select) => match &select.select_list[0] {
            vibesql_ast::SelectItem::Expression { expr, .. } => match expr {
                vibesql_ast::Expression::ColumnRef(col_id) => {
                    let table = col_id.table_canonical();
                    let column = col_id.column_canonical();
                    assert_eq!(table, Some("sqlite_master"));
                    assert_eq!(column, "type");
                }
                _ => panic!("Expected ColumnRef"),
            },
            _ => panic!("Expected Expression select item"),
        },
        _ => panic!("Expected SELECT statement"),
    }
}