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
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
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
use vibesql_ast::{GroupByClause, GroupingElement, MixedGroupingItem};

use super::*;

// ========================================================================
// GROUP BY and HAVING Tests
// ========================================================================

#[test]
fn test_parse_group_by_single_column() {
    let result = Parser::parse_sql("SELECT name, COUNT(*) FROM users GROUP BY name;");
    assert!(result.is_ok());
    let stmt = result.unwrap();

    match stmt {
        vibesql_ast::Statement::Select(select) => {
            assert!(select.group_by.is_some());
            let group_by = select.group_by.unwrap();
            match group_by {
                GroupByClause::Simple(exprs) => {
                    assert_eq!(exprs.len(), 1);
                    match &exprs[0] {
                        vibesql_ast::Expression::ColumnRef(col_id)
                            if col_id.column_canonical() == "name" => {}
                        _ => panic!("Expected column reference 'name'"),
                    }
                }
                _ => panic!("Expected simple GROUP BY"),
            }
        }
        _ => panic!("Expected SELECT"),
    }
}

#[test]
fn test_parse_group_by_multiple_columns() {
    let result =
        Parser::parse_sql("SELECT dept, user_role, COUNT(*) FROM users GROUP BY dept, user_role;");
    assert!(result.is_ok());
    let stmt = result.unwrap();

    match stmt {
        vibesql_ast::Statement::Select(select) => {
            assert!(select.group_by.is_some());
            let group_by = select.group_by.unwrap();
            match group_by {
                GroupByClause::Simple(exprs) => {
                    assert_eq!(exprs.len(), 2);
                    match &exprs[0] {
                        vibesql_ast::Expression::ColumnRef(col_id)
                            if col_id.column_canonical() == "dept" => {}
                        _ => panic!("Expected column reference 'dept'"),
                    }
                    match &exprs[1] {
                        vibesql_ast::Expression::ColumnRef(col_id)
                            if col_id.column_canonical() == "user_role" => {}
                        _ => panic!("Expected column reference 'user_role'"),
                    }
                }
                _ => panic!("Expected simple GROUP BY"),
            }
        }
        _ => panic!("Expected SELECT"),
    }
}

#[test]
fn test_parse_having_clause() {
    let result =
        Parser::parse_sql("SELECT name, COUNT(*) FROM users GROUP BY name HAVING COUNT(*) > 5;");
    assert!(result.is_ok());
    let stmt = result.unwrap();

    match stmt {
        vibesql_ast::Statement::Select(select) => {
            assert!(select.group_by.is_some());
            assert!(select.having.is_some());

            // HAVING should contain a comparison expression
            match select.having.as_ref().unwrap() {
                vibesql_ast::Expression::BinaryOp { op, .. } => {
                    assert_eq!(*op, vibesql_ast::BinaryOperator::GreaterThan);
                }
                _ => panic!("Expected comparison in HAVING clause"),
            }
        }
        _ => panic!("Expected SELECT"),
    }
}

#[test]
fn test_parse_group_by_with_where_and_having() {
    let result = Parser::parse_sql(
        "SELECT dept, COUNT(*) FROM users WHERE active = true GROUP BY dept HAVING COUNT(*) > 10;",
    );
    assert!(result.is_ok());
    let stmt = result.unwrap();

    match stmt {
        vibesql_ast::Statement::Select(select) => {
            // Should have WHERE, GROUP BY, and HAVING
            assert!(select.where_clause.is_some());
            assert!(select.group_by.is_some());
            assert!(select.having.is_some());
        }
        _ => panic!("Expected SELECT"),
    }
}

#[test]
fn test_parse_group_by_qualified_columns() {
    let result = Parser::parse_sql("SELECT u.dept, COUNT(*) FROM users u GROUP BY u.dept;");
    assert!(result.is_ok());
    let stmt = result.unwrap();

    match stmt {
        vibesql_ast::Statement::Select(select) => {
            assert!(select.group_by.is_some());
            let group_by = select.group_by.unwrap();
            match group_by {
                GroupByClause::Simple(exprs) => {
                    assert_eq!(exprs.len(), 1);
                    match &exprs[0] {
                        vibesql_ast::Expression::ColumnRef(col_id) => {
                            let table = col_id.table_canonical();
                            let column = col_id.column_canonical();
                            assert_eq!(table.unwrap(), "u");
                            assert_eq!(column, "dept");
                        }
                        _ => panic!("Expected qualified column reference"),
                    }
                }
                _ => panic!("Expected simple GROUP BY"),
            }
        }
        _ => panic!("Expected SELECT"),
    }
}

#[test]
fn test_parse_group_by_with_order_by() {
    let result = Parser::parse_sql(
        "SELECT name, COUNT(*) as cnt FROM users GROUP BY name ORDER BY cnt DESC;",
    );
    assert!(result.is_ok());
    let stmt = result.unwrap();

    match stmt {
        vibesql_ast::Statement::Select(select) => {
            // Should have both GROUP BY and ORDER BY
            assert!(select.group_by.is_some());
            assert!(select.order_by.is_some());
        }
        _ => panic!("Expected SELECT"),
    }
}

// ========================================================================
// ROLLUP, CUBE, GROUPING SETS Tests (SQL:1999 OLAP Extensions)
// ========================================================================

#[test]
fn test_parse_group_by_rollup() {
    let result = Parser::parse_sql(
        "SELECT year, quarter, SUM(sales) FROM data GROUP BY ROLLUP(year, quarter);",
    );
    assert!(result.is_ok(), "Failed to parse ROLLUP: {:?}", result);
    let stmt = result.unwrap();

    match stmt {
        vibesql_ast::Statement::Select(select) => {
            assert!(select.group_by.is_some());
            let group_by = select.group_by.unwrap();
            match group_by {
                GroupByClause::Rollup(elements) => {
                    assert_eq!(elements.len(), 2);
                    match &elements[0] {
                        GroupingElement::Single(vibesql_ast::Expression::ColumnRef(col_id)) => {
                            assert_eq!(col_id.column_canonical(), "year")
                        }
                        _ => panic!("Expected column reference 'year'"),
                    }
                    match &elements[1] {
                        GroupingElement::Single(vibesql_ast::Expression::ColumnRef(col_id)) => {
                            assert_eq!(col_id.column_canonical(), "quarter")
                        }
                        _ => panic!("Expected column reference 'quarter'"),
                    }
                }
                _ => panic!("Expected ROLLUP clause"),
            }
        }
        _ => panic!("Expected SELECT"),
    }
}

#[test]
fn test_parse_group_by_cube() {
    let result = Parser::parse_sql(
        "SELECT region, product, SUM(sales) FROM data GROUP BY CUBE(region, product);",
    );
    assert!(result.is_ok(), "Failed to parse CUBE: {:?}", result);
    let stmt = result.unwrap();

    match stmt {
        vibesql_ast::Statement::Select(select) => {
            assert!(select.group_by.is_some());
            let group_by = select.group_by.unwrap();
            match group_by {
                GroupByClause::Cube(elements) => {
                    assert_eq!(elements.len(), 2);
                    match &elements[0] {
                        GroupingElement::Single(vibesql_ast::Expression::ColumnRef(col_id)) => {
                            assert_eq!(col_id.column_canonical(), "region")
                        }
                        _ => panic!("Expected column reference 'region'"),
                    }
                    match &elements[1] {
                        GroupingElement::Single(vibesql_ast::Expression::ColumnRef(col_id)) => {
                            assert_eq!(col_id.column_canonical(), "product")
                        }
                        _ => panic!("Expected column reference 'product'"),
                    }
                }
                _ => panic!("Expected CUBE clause"),
            }
        }
        _ => panic!("Expected SELECT"),
    }
}

#[test]
fn test_parse_group_by_grouping_sets() {
    let result = Parser::parse_sql(
        "SELECT year, quarter, SUM(sales) FROM data GROUP BY GROUPING SETS((year, quarter), (year), ());",
    );
    assert!(result.is_ok(), "Failed to parse GROUPING SETS: {:?}", result);
    let stmt = result.unwrap();

    match stmt {
        vibesql_ast::Statement::Select(select) => {
            assert!(select.group_by.is_some());
            let group_by = select.group_by.unwrap();
            match group_by {
                GroupByClause::GroupingSets(sets) => {
                    assert_eq!(sets.len(), 3);
                    // First set: (year, quarter)
                    assert_eq!(sets[0].columns.len(), 2);
                    // Second set: (year)
                    assert_eq!(sets[1].columns.len(), 1);
                    // Third set: () - grand total
                    assert_eq!(sets[2].columns.len(), 0);
                }
                _ => panic!("Expected GROUPING SETS clause"),
            }
        }
        _ => panic!("Expected SELECT"),
    }
}

#[test]
fn test_parse_group_by_rollup_with_composite() {
    // ROLLUP((a, b), c) - (a, b) treated as single grouping unit
    let result = Parser::parse_sql("SELECT a, b, c, SUM(d) FROM data GROUP BY ROLLUP((a, b), c);");
    assert!(result.is_ok(), "Failed to parse ROLLUP with composite: {:?}", result);
    let stmt = result.unwrap();

    match stmt {
        vibesql_ast::Statement::Select(select) => {
            assert!(select.group_by.is_some());
            let group_by = select.group_by.unwrap();
            match group_by {
                GroupByClause::Rollup(elements) => {
                    assert_eq!(elements.len(), 2);
                    // First element should be composite (a, b)
                    match &elements[0] {
                        GroupingElement::Composite(exprs) => {
                            assert_eq!(exprs.len(), 2);
                        }
                        _ => panic!("Expected composite element (a, b)"),
                    }
                    // Second element should be single c
                    match &elements[1] {
                        GroupingElement::Single(_) => {}
                        _ => panic!("Expected single element c"),
                    }
                }
                _ => panic!("Expected ROLLUP clause"),
            }
        }
        _ => panic!("Expected SELECT"),
    }
}

#[test]
fn test_parse_grouping_function() {
    let result = Parser::parse_sql(
        "SELECT year, GROUPING(year), SUM(sales) FROM data GROUP BY ROLLUP(year);",
    );
    assert!(result.is_ok(), "Failed to parse GROUPING function: {:?}", result);
    let stmt = result.unwrap();

    match stmt {
        vibesql_ast::Statement::Select(select) => {
            // Second select item should be GROUPING(year)
            assert!(select.select_list.len() >= 2);
            match &select.select_list[1] {
                vibesql_ast::SelectItem::Expression { expr, .. } => match expr {
                    vibesql_ast::Expression::Function { name, args, .. } => {
                        assert_eq!(name, "grouping");
                        assert_eq!(args.len(), 1);
                    }
                    _ => panic!("Expected GROUPING function"),
                },
                _ => panic!("Expected expression"),
            }
        }
        _ => panic!("Expected SELECT"),
    }
}

#[test]
fn test_group_by_clause_len() {
    // Test the len() helper method
    let simple = GroupByClause::Simple(vec![
        vibesql_ast::Expression::ColumnRef(vibesql_ast::ColumnIdentifier::simple("a", false)),
        vibesql_ast::Expression::ColumnRef(vibesql_ast::ColumnIdentifier::simple("b", false)),
    ]);
    assert_eq!(simple.len(), 2);

    let rollup = GroupByClause::Rollup(vec![
        GroupingElement::Single(vibesql_ast::Expression::ColumnRef(
            vibesql_ast::ColumnIdentifier::simple("a", false),
        )),
        GroupingElement::Composite(vec![
            vibesql_ast::Expression::ColumnRef(vibesql_ast::ColumnIdentifier::simple("b", false)),
            vibesql_ast::Expression::ColumnRef(vibesql_ast::ColumnIdentifier::simple("c", false)),
        ]),
    ]);
    assert_eq!(rollup.len(), 3); // a, b, c = 3 total expressions
}

#[test]
fn test_group_by_clause_is_simple() {
    let simple = GroupByClause::Simple(vec![]);
    assert!(simple.is_simple());

    let rollup = GroupByClause::Rollup(vec![]);
    assert!(!rollup.is_simple());

    let cube = GroupByClause::Cube(vec![]);
    assert!(!cube.is_simple());

    let grouping_sets = GroupByClause::GroupingSets(vec![]);
    assert!(!grouping_sets.is_simple());
}

// ========================================================================
// Mixed GROUP BY Tests (SQL:1999 - combining simple columns with ROLLUP/CUBE)
// ========================================================================

#[test]
fn test_parse_group_by_mixed_simple_with_rollup() {
    // GROUP BY region, ROLLUP(year, quarter)
    let result = Parser::parse_sql(
        "SELECT region, year, quarter, SUM(sales) FROM data GROUP BY region, ROLLUP(year, quarter);",
    );
    assert!(result.is_ok(), "Failed to parse mixed GROUP BY: {:?}", result);
    let stmt = result.unwrap();

    match stmt {
        vibesql_ast::Statement::Select(select) => {
            assert!(select.group_by.is_some());
            let group_by = select.group_by.unwrap();
            match group_by {
                GroupByClause::Mixed(items) => {
                    assert_eq!(items.len(), 2);
                    // First item should be simple expression 'region'
                    match &items[0] {
                        MixedGroupingItem::Simple(vibesql_ast::Expression::ColumnRef(col_id)) => {
                            assert_eq!(col_id.column_canonical(), "region")
                        }
                        _ => panic!("Expected simple column 'region'"),
                    }
                    // Second item should be ROLLUP(year, quarter)
                    match &items[1] {
                        MixedGroupingItem::Rollup(elements) => {
                            assert_eq!(elements.len(), 2);
                        }
                        _ => panic!("Expected ROLLUP item"),
                    }
                }
                _ => panic!("Expected Mixed GROUP BY clause, got {:?}", group_by),
            }
        }
        _ => panic!("Expected SELECT"),
    }
}

#[test]
fn test_parse_group_by_mixed_simple_with_cube() {
    // GROUP BY department, CUBE(category, brand)
    let result = Parser::parse_sql(
        "SELECT department, category, brand, SUM(sales) FROM products GROUP BY department, CUBE(category, brand);",
    );
    assert!(result.is_ok(), "Failed to parse mixed GROUP BY with CUBE: {:?}", result);
    let stmt = result.unwrap();

    match stmt {
        vibesql_ast::Statement::Select(select) => {
            assert!(select.group_by.is_some());
            let group_by = select.group_by.unwrap();
            match group_by {
                GroupByClause::Mixed(items) => {
                    assert_eq!(items.len(), 2);
                    // First item should be simple expression 'department'
                    match &items[0] {
                        MixedGroupingItem::Simple(_) => {}
                        _ => panic!("Expected simple column"),
                    }
                    // Second item should be CUBE
                    match &items[1] {
                        MixedGroupingItem::Cube(elements) => {
                            assert_eq!(elements.len(), 2);
                        }
                        _ => panic!("Expected CUBE item"),
                    }
                }
                _ => panic!("Expected Mixed GROUP BY clause"),
            }
        }
        _ => panic!("Expected SELECT"),
    }
}

#[test]
fn test_parse_group_by_mixed_simple_with_grouping_sets() {
    // GROUP BY region, GROUPING SETS((year), (quarter))
    let result = Parser::parse_sql(
        "SELECT region, year, quarter, SUM(sales) FROM data GROUP BY region, GROUPING SETS((year), (quarter));",
    );
    assert!(result.is_ok(), "Failed to parse mixed GROUP BY with GROUPING SETS: {:?}", result);
    let stmt = result.unwrap();

    match stmt {
        vibesql_ast::Statement::Select(select) => {
            assert!(select.group_by.is_some());
            let group_by = select.group_by.unwrap();
            match group_by {
                GroupByClause::Mixed(items) => {
                    assert_eq!(items.len(), 2);
                    // First item should be simple expression 'region'
                    match &items[0] {
                        MixedGroupingItem::Simple(_) => {}
                        _ => panic!("Expected simple column"),
                    }
                    // Second item should be GROUPING SETS
                    match &items[1] {
                        MixedGroupingItem::GroupingSets(sets) => {
                            assert_eq!(sets.len(), 2);
                        }
                        _ => panic!("Expected GROUPING SETS item"),
                    }
                }
                _ => panic!("Expected Mixed GROUP BY clause"),
            }
        }
        _ => panic!("Expected SELECT"),
    }
}

#[test]
fn test_parse_group_by_mixed_multiple_simple_with_rollup() {
    // GROUP BY a, b, ROLLUP(c, d)
    let result =
        Parser::parse_sql("SELECT a, b, c, d, SUM(e) FROM data GROUP BY a, b, ROLLUP(c, d);");
    assert!(result.is_ok(), "Failed to parse mixed GROUP BY with multiple simple: {:?}", result);
    let stmt = result.unwrap();

    match stmt {
        vibesql_ast::Statement::Select(select) => {
            assert!(select.group_by.is_some());
            let group_by = select.group_by.unwrap();
            match group_by {
                GroupByClause::Mixed(items) => {
                    assert_eq!(items.len(), 3); // a, b, ROLLUP(c, d)
                                                // First two should be simple
                    assert!(matches!(&items[0], MixedGroupingItem::Simple(_)));
                    assert!(matches!(&items[1], MixedGroupingItem::Simple(_)));
                    // Third should be ROLLUP
                    assert!(matches!(&items[2], MixedGroupingItem::Rollup(_)));
                }
                _ => panic!("Expected Mixed GROUP BY clause"),
            }
        }
        _ => panic!("Expected SELECT"),
    }
}

#[test]
fn test_parse_group_by_rollup_then_simple() {
    // ROLLUP at the beginning, then simple columns
    // GROUP BY ROLLUP(a), b
    let result = Parser::parse_sql("SELECT a, b, SUM(c) FROM data GROUP BY ROLLUP(a), b;");
    assert!(result.is_ok(), "Failed to parse ROLLUP then simple: {:?}", result);
    let stmt = result.unwrap();

    match stmt {
        vibesql_ast::Statement::Select(select) => {
            assert!(select.group_by.is_some());
            let group_by = select.group_by.unwrap();
            match group_by {
                GroupByClause::Mixed(items) => {
                    assert_eq!(items.len(), 2);
                    // First should be ROLLUP
                    assert!(matches!(&items[0], MixedGroupingItem::Rollup(_)));
                    // Second should be simple
                    assert!(matches!(&items[1], MixedGroupingItem::Simple(_)));
                }
                _ => panic!("Expected Mixed GROUP BY clause"),
            }
        }
        _ => panic!("Expected SELECT"),
    }
}

#[test]
fn test_parse_group_by_multiple_rollup() {
    // Multiple ROLLUP in one GROUP BY
    // GROUP BY ROLLUP(a), ROLLUP(b)
    let result = Parser::parse_sql("SELECT a, b, SUM(c) FROM data GROUP BY ROLLUP(a), ROLLUP(b);");
    assert!(result.is_ok(), "Failed to parse multiple ROLLUP: {:?}", result);
    let stmt = result.unwrap();

    match stmt {
        vibesql_ast::Statement::Select(select) => {
            assert!(select.group_by.is_some());
            let group_by = select.group_by.unwrap();
            match group_by {
                GroupByClause::Mixed(items) => {
                    assert_eq!(items.len(), 2);
                    assert!(matches!(&items[0], MixedGroupingItem::Rollup(_)));
                    assert!(matches!(&items[1], MixedGroupingItem::Rollup(_)));
                }
                _ => panic!("Expected Mixed GROUP BY clause"),
            }
        }
        _ => panic!("Expected SELECT"),
    }
}

#[test]
fn test_parse_group_by_mixed_rollup_and_cube() {
    // Mix of ROLLUP and CUBE
    // GROUP BY a, ROLLUP(b), CUBE(c)
    let result =
        Parser::parse_sql("SELECT a, b, c, SUM(d) FROM data GROUP BY a, ROLLUP(b), CUBE(c);");
    assert!(result.is_ok(), "Failed to parse mixed ROLLUP and CUBE: {:?}", result);
    let stmt = result.unwrap();

    match stmt {
        vibesql_ast::Statement::Select(select) => {
            assert!(select.group_by.is_some());
            let group_by = select.group_by.unwrap();
            match group_by {
                GroupByClause::Mixed(items) => {
                    assert_eq!(items.len(), 3);
                    assert!(matches!(&items[0], MixedGroupingItem::Simple(_)));
                    assert!(matches!(&items[1], MixedGroupingItem::Rollup(_)));
                    assert!(matches!(&items[2], MixedGroupingItem::Cube(_)));
                }
                _ => panic!("Expected Mixed GROUP BY clause"),
            }
        }
        _ => panic!("Expected SELECT"),
    }
}