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
//! Tests for window function parsing (OVER clause)

use super::*;

#[test]
fn test_empty_over_clause() {
    let sql = "SELECT ROW_NUMBER() OVER () FROM t";
    let result = Parser::parse_sql(sql);
    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 { expr, .. } => match expr {
                    vibesql_ast::Expression::WindowFunction { function, over } => {
                        match function {
                            vibesql_ast::WindowFunctionSpec::Ranking { name, .. } => {
                                assert_eq!(name, "ROW_NUMBER");
                            }
                            _ => panic!("Expected Ranking window function"),
                        }
                        assert!(over.partition_by.is_none());
                        assert!(over.order_by.is_none());
                        assert!(over.frame.is_none());
                    }
                    _ => panic!("Expected WindowFunction expression"),
                },
                _ => panic!("Expected Expression select item"),
            }
        }
        _ => panic!("Expected SELECT statement"),
    }
}

#[test]
fn test_partition_by_single() {
    let sql = "SELECT RANK() OVER (PARTITION BY dept) FROM t";
    let result = Parser::parse_sql(sql);
    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::WindowFunction { over, .. } => {
                    assert!(over.partition_by.is_some());
                    let partition = over.partition_by.as_ref().unwrap();
                    assert_eq!(partition.len(), 1);
                }
                _ => panic!("Expected WindowFunction"),
            },
            _ => panic!("Expected Expression"),
        },
        _ => panic!("Expected SELECT"),
    }
}

#[test]
fn test_partition_by_multiple() {
    let sql = "SELECT RANK() OVER (PARTITION BY dept, region) FROM t";
    let result = Parser::parse_sql(sql);
    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::WindowFunction { over, .. } => {
                    assert!(over.partition_by.is_some());
                    let partition = over.partition_by.as_ref().unwrap();
                    assert_eq!(partition.len(), 2);
                }
                _ => panic!("Expected WindowFunction"),
            },
            _ => panic!("Expected Expression"),
        },
        _ => panic!("Expected SELECT"),
    }
}

#[test]
fn test_order_by_single() {
    let sql = "SELECT RANK() OVER (ORDER BY salary DESC) FROM t";
    let result = Parser::parse_sql(sql);
    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::WindowFunction { over, .. } => {
                    assert!(over.order_by.is_some());
                    let order = over.order_by.as_ref().unwrap();
                    assert_eq!(order.len(), 1);
                    assert_eq!(order[0].direction, vibesql_ast::OrderDirection::Desc);
                }
                _ => panic!("Expected WindowFunction"),
            },
            _ => panic!("Expected Expression"),
        },
        _ => panic!("Expected SELECT"),
    }
}

#[test]
fn test_order_by_multiple() {
    let sql = "SELECT RANK() OVER (ORDER BY dept ASC, salary DESC) FROM t";
    let result = Parser::parse_sql(sql);
    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::WindowFunction { over, .. } => {
                    assert!(over.order_by.is_some());
                    let order = over.order_by.as_ref().unwrap();
                    assert_eq!(order.len(), 2);
                    assert_eq!(order[0].direction, vibesql_ast::OrderDirection::Asc);
                    assert_eq!(order[1].direction, vibesql_ast::OrderDirection::Desc);
                }
                _ => panic!("Expected WindowFunction"),
            },
            _ => panic!("Expected Expression"),
        },
        _ => panic!("Expected SELECT"),
    }
}

#[test]
fn test_partition_and_order() {
    let sql = "SELECT RANK() OVER (PARTITION BY dept ORDER BY salary DESC) FROM t";
    let result = Parser::parse_sql(sql);
    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::WindowFunction { over, .. } => {
                    assert!(over.partition_by.is_some());
                    assert!(over.order_by.is_some());
                }
                _ => panic!("Expected WindowFunction"),
            },
            _ => panic!("Expression"),
        },
        _ => panic!("Expected SELECT"),
    }
}

#[test]
fn test_rows_unbounded_preceding() {
    let sql = "SELECT SUM(x) OVER (ROWS UNBOUNDED PRECEDING) FROM t";
    let result = Parser::parse_sql(sql);
    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::WindowFunction { over, .. } => {
                    assert!(over.frame.is_some());
                    let frame = over.frame.as_ref().unwrap();
                    assert_eq!(frame.unit, vibesql_ast::FrameUnit::Rows);
                    assert_eq!(frame.start, vibesql_ast::FrameBound::UnboundedPreceding);
                }
                _ => panic!("Expected WindowFunction"),
            },
            _ => panic!("Expected Expression"),
        },
        _ => panic!("Expected SELECT"),
    }
}

#[test]
fn test_rows_between_and() {
    let sql = "SELECT SUM(x) OVER (ROWS BETWEEN 2 PRECEDING AND CURRENT ROW) FROM t";
    let result = Parser::parse_sql(sql);
    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::WindowFunction { over, .. } => {
                    assert!(over.frame.is_some());
                    let frame = over.frame.as_ref().unwrap();
                    assert_eq!(frame.unit, vibesql_ast::FrameUnit::Rows);

                    match &frame.start {
                        vibesql_ast::FrameBound::Preceding(_) => {}
                        _ => panic!("Expected Preceding frame start"),
                    }

                    assert!(frame.end.is_some());
                    assert_eq!(frame.end.as_ref().unwrap(), &vibesql_ast::FrameBound::CurrentRow);
                }
                _ => panic!("Expected WindowFunction"),
            },
            _ => panic!("Expected Expression"),
        },
        _ => panic!("Expected SELECT"),
    }
}

#[test]
fn test_range_frame() {
    let sql = "SELECT SUM(x) OVER (RANGE UNBOUNDED PRECEDING) FROM t";
    let result = Parser::parse_sql(sql);
    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::WindowFunction { over, .. } => {
                    assert!(over.frame.is_some());
                    let frame = over.frame.as_ref().unwrap();
                    assert_eq!(frame.unit, vibesql_ast::FrameUnit::Range);
                }
                _ => panic!("Expected WindowFunction"),
            },
            _ => panic!("Expected Expression"),
        },
        _ => panic!("Expected SELECT"),
    }
}

#[test]
fn test_aggregate_as_window_function() {
    let sql = "SELECT SUM(salary) OVER (PARTITION BY dept) FROM t";
    let result = Parser::parse_sql(sql);
    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::WindowFunction { function, .. } => match function {
                    vibesql_ast::WindowFunctionSpec::Aggregate { name, args, .. } => {
                        assert_eq!(name, "SUM");
                        assert_eq!(args.len(), 1);
                    }
                    _ => panic!("Expected Aggregate window function"),
                },
                _ => panic!("Expected WindowFunction"),
            },
            _ => panic!("Expected Expression"),
        },
        _ => panic!("Expected SELECT"),
    }
}

#[test]
fn test_ranking_functions() {
    let sql = "SELECT ROW_NUMBER() OVER (ORDER BY id) FROM t";
    let result = Parser::parse_sql(sql);
    assert!(result.is_ok());

    let sql = "SELECT RANK() OVER (ORDER BY score DESC) FROM t";
    let result = Parser::parse_sql(sql);
    assert!(result.is_ok());

    let sql = "SELECT DENSE_RANK() OVER (ORDER BY score DESC) FROM t";
    let result = Parser::parse_sql(sql);
    assert!(result.is_ok());

    let sql = "SELECT NTILE(4) OVER (ORDER BY score DESC) FROM t";
    let result = Parser::parse_sql(sql);
    assert!(result.is_ok());
}

#[test]
fn test_value_functions() {
    let sql = "SELECT LAG(salary, 1) OVER (ORDER BY hire_date) FROM t";
    let result = Parser::parse_sql(sql);
    assert!(result.is_ok());

    let sql = "SELECT LEAD(salary, 2) OVER (ORDER BY hire_date) FROM t";
    let result = Parser::parse_sql(sql);
    assert!(result.is_ok());
}

#[test]
fn test_complex_window_spec() {
    let sql = "SELECT RANK() OVER (PARTITION BY dept ORDER BY salary DESC ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) FROM t";
    let result = Parser::parse_sql(sql);
    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::WindowFunction { over, .. } => {
                    assert!(over.partition_by.is_some());
                    assert!(over.order_by.is_some());
                    assert!(over.frame.is_some());

                    let frame = over.frame.as_ref().unwrap();
                    assert!(frame.end.is_some());
                }
                _ => panic!("Expected WindowFunction"),
            },
            _ => panic!("Expected Expression"),
        },
        _ => panic!("Expected SELECT"),
    }
}

#[test]
fn test_multiple_window_functions() {
    let sql = "SELECT ROW_NUMBER() OVER (ORDER BY id), RANK() OVER (ORDER BY score DESC) FROM t";
    let result = Parser::parse_sql(sql);
    assert!(result.is_ok());

    let stmt = result.unwrap();
    match stmt {
        vibesql_ast::Statement::Select(select) => {
            assert_eq!(select.select_list.len(), 2);

            for item in &select.select_list {
                match item {
                    vibesql_ast::SelectItem::Expression { expr, .. } => match expr {
                        vibesql_ast::Expression::WindowFunction { .. } => {}
                        _ => panic!("Expected WindowFunction"),
                    },
                    _ => panic!("Expected Expression"),
                }
            }
        }
        _ => panic!("Expected SELECT"),
    }
}

#[test]
fn test_unbounded_following() {
    let sql = "SELECT SUM(x) OVER (ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) FROM t";
    let result = Parser::parse_sql(sql);
    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::WindowFunction { over, .. } => {
                    let frame = over.frame.as_ref().unwrap();
                    assert_eq!(frame.start, vibesql_ast::FrameBound::CurrentRow);
                    assert_eq!(
                        frame.end.as_ref().unwrap(),
                        &vibesql_ast::FrameBound::UnboundedFollowing
                    );
                }
                _ => panic!("Expected WindowFunction"),
            },
            _ => panic!("Expected Expression"),
        },
        _ => panic!("Expected SELECT"),
    }
}

#[test]
fn test_n_following() {
    let sql = "SELECT AVG(price) OVER (ROWS BETWEEN 1 PRECEDING AND 2 FOLLOWING) FROM t";
    let result = Parser::parse_sql(sql);
    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::WindowFunction { over, .. } => {
                    let frame = over.frame.as_ref().unwrap();

                    match &frame.start {
                        vibesql_ast::FrameBound::Preceding(_) => {}
                        _ => panic!("Expected Preceding"),
                    }

                    match &frame.end {
                        Some(vibesql_ast::FrameBound::Following(_)) => {}
                        _ => panic!("Expected Following"),
                    }
                }
                _ => panic!("Expected WindowFunction"),
            },
            _ => panic!("Expected Expression"),
        },
        _ => panic!("Expected SELECT"),
    }
}

#[test]
fn test_window_function_without_over_clause_errors() {
    // Window-only functions MUST have an OVER clause
    // They cannot be used as regular scalar functions

    // Ranking functions
    let sql = "SELECT row_number() FROM t";
    let result = Parser::parse_sql(sql);
    assert!(result.is_err(), "row_number() without OVER should error");
    assert!(result.unwrap_err().message.contains("misuse of window function"));

    let sql = "SELECT rank() FROM t";
    let result = Parser::parse_sql(sql);
    assert!(result.is_err(), "rank() without OVER should error");

    let sql = "SELECT dense_rank() FROM t";
    let result = Parser::parse_sql(sql);
    assert!(result.is_err(), "dense_rank() without OVER should error");

    let sql = "SELECT ntile(4) FROM t";
    let result = Parser::parse_sql(sql);
    assert!(result.is_err(), "ntile() without OVER should error");

    // Value functions
    let sql = "SELECT nth_value(x, 1) FROM t";
    let result = Parser::parse_sql(sql);
    assert!(result.is_err(), "nth_value() without OVER should error");

    let sql = "SELECT lag(x, 1) FROM t";
    let result = Parser::parse_sql(sql);
    assert!(result.is_err(), "lag() without OVER should error");

    let sql = "SELECT lead(x, 1) FROM t";
    let result = Parser::parse_sql(sql);
    assert!(result.is_err(), "lead() without OVER should error");

    let sql = "SELECT first_value(x) FROM t";
    let result = Parser::parse_sql(sql);
    assert!(result.is_err(), "first_value() without OVER should error");

    let sql = "SELECT last_value(x) FROM t";
    let result = Parser::parse_sql(sql);
    assert!(result.is_err(), "last_value() without OVER should error");
}

#[test]
fn test_window_function_argument_count_validation() {
    // Zero-argument functions should reject any arguments
    let sql = "SELECT row_number(x) OVER () FROM t";
    let result = Parser::parse_sql(sql);
    assert!(result.is_err(), "row_number(x) should error");
    assert!(result.unwrap_err().message.contains("wrong number of arguments"));

    let sql = "SELECT rank(x) OVER () FROM t";
    let result = Parser::parse_sql(sql);
    assert!(result.is_err(), "rank(x) should error");

    let sql = "SELECT dense_rank(x) OVER () FROM t";
    let result = Parser::parse_sql(sql);
    assert!(result.is_err(), "dense_rank(x) should error");

    // Single-argument functions should require exactly one argument
    let sql = "SELECT ntile() OVER () FROM t";
    let result = Parser::parse_sql(sql);
    assert!(result.is_err(), "ntile() with no args should error");

    let sql = "SELECT ntile(1, 2) OVER () FROM t";
    let result = Parser::parse_sql(sql);
    assert!(result.is_err(), "ntile(1, 2) should error");

    let sql = "SELECT first_value() OVER () FROM t";
    let result = Parser::parse_sql(sql);
    assert!(result.is_err(), "first_value() with no args should error");

    // NTH_VALUE requires exactly 2 arguments
    let sql = "SELECT nth_value(x) OVER () FROM t";
    let result = Parser::parse_sql(sql);
    assert!(result.is_err(), "nth_value(x) with 1 arg should error");

    let sql = "SELECT nth_value(x, 1, 2) OVER () FROM t";
    let result = Parser::parse_sql(sql);
    assert!(result.is_err(), "nth_value(x, 1, 2) with 3 args should error");

    // LAG/LEAD accept 1-3 arguments
    let sql = "SELECT lag() OVER () FROM t";
    let result = Parser::parse_sql(sql);
    assert!(result.is_err(), "lag() with no args should error");

    let sql = "SELECT lag(x, 1, 0, extra) OVER () FROM t";
    let result = Parser::parse_sql(sql);
    assert!(result.is_err(), "lag() with 4 args should error");

    // Valid calls should succeed
    let sql = "SELECT row_number() OVER () FROM t";
    assert!(Parser::parse_sql(sql).is_ok(), "row_number() should work");

    let sql = "SELECT ntile(4) OVER () FROM t";
    assert!(Parser::parse_sql(sql).is_ok(), "ntile(4) should work");

    let sql = "SELECT nth_value(x, 1) OVER () FROM t";
    assert!(Parser::parse_sql(sql).is_ok(), "nth_value(x, 1) should work");

    let sql = "SELECT lag(x) OVER () FROM t";
    assert!(Parser::parse_sql(sql).is_ok(), "lag(x) should work");

    let sql = "SELECT lag(x, 1) OVER () FROM t";
    assert!(Parser::parse_sql(sql).is_ok(), "lag(x, 1) should work");

    let sql = "SELECT lag(x, 1, 0) OVER () FROM t";
    assert!(Parser::parse_sql(sql).is_ok(), "lag(x, 1, 0) should work");
}