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
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
use super::*;
impl Parser {
/// Parse SELECT statement (public entry point)
pub(crate) fn parse_select_statement(&mut self) -> Result<vibesql_ast::SelectStmt, ParseError> {
self.parse_select_statement_internal(true, true, false)
}
/// Parse a SELECT statement used as the source of a DML statement
/// (e.g., INSERT INTO t SELECT ... RETURNING ...).
///
/// Unlike `parse_select_statement`, a trailing RETURNING keyword is accepted
/// as a valid end token because the clause belongs to the outer DML statement
/// (issue #5263). Bare SELECT statements must NOT use this entry point, since
/// SQLite rejects RETURNING outside DML (issue #5271).
pub(crate) fn parse_dml_source_select_statement(
&mut self,
) -> Result<vibesql_ast::SelectStmt, ParseError> {
self.parse_select_statement_internal(true, true, true)
}
/// Parse SELECT or VALUES statement when embedded in another statement (cursor, view, etc.)
/// This allows tokens after SELECT/VALUES that belong to the outer statement.
///
/// Supports:
/// - SELECT ... (standard select statement)
/// - VALUES(expr, ...) [, ...] (table value constructor, e.g., CREATE VIEW dual AS VALUES('x'))
pub(crate) fn parse_embedded_select_statement(
&mut self,
) -> Result<vibesql_ast::SelectStmt, ParseError> {
if self.peek_keyword(Keyword::Values) {
// Handle VALUES clause as view source (e.g., CREATE VIEW dual(dummy) AS VALUES('x'))
self.parse_values_statement_internal(true, false, false)
} else {
self.parse_select_statement_internal(true, false, false)
}
}
/// Internal SELECT parser with control over ORDER BY/LIMIT parsing
///
/// The `allow_order_limit` parameter controls whether ORDER BY, LIMIT, and OFFSET
/// are parsed. This is set to false when parsing the right-hand side of set operations
/// to ensure these clauses only apply to the outermost query.
///
/// The `validate_end_tokens` parameter controls whether to validate that no
/// unexpected tokens follow the SELECT statement. This is set to false when parsing
/// SELECT as part of another statement (cursor, view, etc.) where additional tokens
/// belong to the outer statement.
///
/// The `allow_returning` parameter controls whether a trailing RETURNING keyword
/// is accepted as a valid end token. This is true only when the SELECT/VALUES is
/// the source of a DML statement (INSERT INTO t SELECT ... RETURNING ..., issue
/// #5263). For bare SELECT statements it must be false so that SQLite-incompatible
/// input like `SELECT 1 RETURNING a` is rejected (issue #5271).
fn parse_select_statement_internal(
&mut self,
allow_order_limit: bool,
validate_end_tokens: bool,
allow_returning: bool,
) -> Result<vibesql_ast::SelectStmt, ParseError> {
// Parse optional WITH clause (CTEs)
let with_clause = if self.peek_keyword(Keyword::With) {
self.consume_keyword(Keyword::With)?;
// Check for optional RECURSIVE keyword (SQL:1999, SQLite)
let recursive = if self.peek_keyword(Keyword::Recursive) {
self.consume_keyword(Keyword::Recursive)?;
true
} else {
false
};
Some(self.parse_cte_list(recursive)?)
} else {
None
};
self.expect_keyword(Keyword::Select)?;
// Parse optional set quantifier (DISTINCT or ALL)
// SQL:1999 syntax: SELECT [ALL | DISTINCT] select_list
// ALL is the default (include duplicates), DISTINCT removes duplicates
let distinct = if self.peek_keyword(Keyword::Distinct) {
self.consume_keyword(Keyword::Distinct)?;
true
} else if self.peek_keyword(Keyword::All) {
self.consume_keyword(Keyword::All)?;
false // ALL means include duplicates (same as default)
} else {
false // Default is ALL (include duplicates)
};
// Parse SELECT list
let select_list = self.parse_select_list()?;
// Parse optional INTO clause
// Two forms:
// 1. DDL SELECT INTO: SELECT * INTO new_table FROM source (SQL:1999 Feature E111)
// 2. Procedural SELECT INTO: SELECT col1, col2 INTO @var1, @var2 FROM table
let (into_table, into_variables) = if self.peek_keyword(Keyword::Into) {
self.consume_keyword(Keyword::Into)?;
// Check if this is procedural SELECT INTO (variables) or DDL SELECT INTO (table)
if matches!(self.peek(), Token::UserVariable(_)) {
// Procedural SELECT INTO: parse comma-separated list of user variables
let variables = self.parse_comma_separated_list(|p| match p.peek() {
Token::UserVariable(var_name) => {
let name = var_name.clone();
p.advance();
Ok(name)
}
_ => Err(ParseError {
message: "Expected user variable (@var) in procedural SELECT INTO"
.to_string(),
}),
})?;
(None, Some(variables))
} else {
// DDL SELECT INTO: parse table name
(Some(self.parse_identifier()?), None)
}
} else {
(None, None)
};
// Parse optional FROM clause
let from = if self.peek_keyword(Keyword::From) {
self.consume_keyword(Keyword::From)?;
Some(self.parse_from_clause()?)
} else {
None
};
// Parse optional WHERE clause
let where_clause = if self.peek_keyword(Keyword::Where) {
self.consume_keyword(Keyword::Where)?;
Some(self.parse_expression()?)
} else {
None
};
// Parse optional GROUP BY clause (with ROLLUP, CUBE, GROUPING SETS support)
let group_by = if self.peek_keyword(Keyword::Group) {
self.consume_keyword(Keyword::Group)?;
self.expect_keyword(Keyword::By)?;
Some(self.parse_group_by_clause()?)
} else {
None
};
// Parse optional HAVING clause (only valid with GROUP BY)
let having = if self.peek_keyword(Keyword::Having) {
self.consume_keyword(Keyword::Having)?;
Some(self.parse_expression()?)
} else {
None
};
// Parse optional WINDOW clause (named window definitions)
// Example: WINDOW win AS (PARTITION BY x ORDER BY y)
let window_definitions = if self.peek_keyword(Keyword::Window) {
self.consume_keyword(Keyword::Window)?;
Some(self.parse_window_definitions()?)
} else {
None
};
// Parse set operations (UNION, INTERSECT, EXCEPT) before ORDER BY/LIMIT
// This ensures ORDER BY/LIMIT apply to the entire set operation result
let set_operation = if self.peek_keyword(Keyword::Union)
|| self.peek_keyword(Keyword::Intersect)
|| self.peek_keyword(Keyword::Except)
{
let op = if self.peek_keyword(Keyword::Union) {
self.consume_keyword(Keyword::Union)?;
vibesql_ast::SetOperator::Union
} else if self.peek_keyword(Keyword::Intersect) {
self.consume_keyword(Keyword::Intersect)?;
vibesql_ast::SetOperator::Intersect
} else {
self.consume_keyword(Keyword::Except)?;
vibesql_ast::SetOperator::Except
};
// Check for ALL or DISTINCT quantifier (default is DISTINCT if omitted)
let all = if self.peek_keyword(Keyword::All) {
self.consume_keyword(Keyword::All)?;
true
} else if self.peek_keyword(Keyword::Distinct) {
self.consume_keyword(Keyword::Distinct)?;
false // DISTINCT = remove duplicates (same as default)
} else {
false // Default behavior is DISTINCT
};
// Parse the right-hand side SELECT or VALUES statement
// Don't allow ORDER BY/LIMIT on the right side - they should only apply to the final
// result
//
// Standard SQL allows the right-hand side to be:
// - A SELECT statement: SELECT ... UNION ALL SELECT ...
// - A VALUES clause: SELECT ... UNION VALUES(1)
// - Parenthesized: SELECT ... UNION ALL (SELECT ...) or SELECT ... UNION (VALUES(1))
let right = if matches!(self.peek(), Token::LParen) {
self.advance(); // consume '('
let stmt = if self.peek_keyword(Keyword::Values) {
self.parse_values_statement_internal(false, true, false)?
} else {
self.parse_select_statement_internal(false, true, false)?
};
if !matches!(self.peek(), Token::RParen) {
return Err(ParseError {
message: "Expected ')' after parenthesized statement in set operation"
.to_string(),
});
}
self.advance(); // consume ')'
Box::new(stmt)
} else if self.peek_keyword(Keyword::Values) {
Box::new(self.parse_values_statement_internal(false, true, allow_returning)?)
} else {
Box::new(self.parse_select_statement_internal(false, true, allow_returning)?)
};
Some(vibesql_ast::SetOperation { op, all, right })
} else {
None
};
// Parse ORDER BY, LIMIT, OFFSET after set operations (only if allowed)
// These apply to the entire result (including set operations)
// Parse optional ORDER BY clause
let order_by = if allow_order_limit && self.peek_keyword(Keyword::Order) {
self.consume_keyword(Keyword::Order)?;
self.expect_keyword(Keyword::By)?;
// Parse comma-separated list of order items
let order_items = self.parse_comma_separated_list(|p| {
let expr = p.parse_expression()?;
// Check for optional ASC/DESC
let direction = if p.peek_keyword(Keyword::Asc) {
p.consume_keyword(Keyword::Asc)?;
vibesql_ast::OrderDirection::Asc
} else if p.peek_keyword(Keyword::Desc) {
p.consume_keyword(Keyword::Desc)?;
vibesql_ast::OrderDirection::Desc
} else {
vibesql_ast::OrderDirection::Asc // Default
};
// Parse optional NULLS FIRST/LAST (SQL:2003 extension)
let nulls_order = if p.peek_keyword(Keyword::Nulls) {
p.consume_keyword(Keyword::Nulls)?;
if p.peek_keyword(Keyword::First) {
p.consume_keyword(Keyword::First)?;
Some(vibesql_ast::NullsOrder::First)
} else if p.peek_keyword(Keyword::Last) {
p.consume_keyword(Keyword::Last)?;
Some(vibesql_ast::NullsOrder::Last)
} else {
return Err(ParseError {
message: format!(
"Expected FIRST or LAST after NULLS, found {}",
p.peek().syntax_error()
),
});
}
} else {
None
};
Ok(vibesql_ast::OrderByItem { expr, direction, nulls_order })
})?;
// Check for too many ORDER BY terms (SQLite compatibility)
if order_items.len() > super::super::MAX_ORDER_BY_TERMS {
return Err(ParseError {
message: "too many terms in ORDER BY clause".to_string(),
});
}
Some(order_items)
} else {
None
};
// Check for ORDER BY appearing before a set operation (SQLite-compatible error)
// This catches: SELECT ... ORDER BY ... UNION SELECT ...
if order_by.is_some() {
let op_name = if self.peek_keyword(Keyword::Union) {
// Check for UNION ALL vs UNION
let saved = self.position;
self.consume_keyword(Keyword::Union)?;
let all = self.peek_keyword(Keyword::All);
self.position = saved; // restore position
if all {
Some("UNION ALL")
} else {
Some("UNION")
}
} else if self.peek_keyword(Keyword::Intersect) {
Some("INTERSECT")
} else if self.peek_keyword(Keyword::Except) {
Some("EXCEPT")
} else {
None
};
if let Some(op) = op_name {
return Err(ParseError {
message: format!("ORDER BY clause should come after {} not before", op),
});
}
}
// Parse LIMIT clause
// SQLite allows expressions in LIMIT (e.g., LIMIT 5+3)
// SQLite also allows comma syntax: LIMIT offset,count (equivalent to LIMIT count OFFSET
// offset)
let (limit, offset_from_limit) = if allow_order_limit && self.peek_keyword(Keyword::Limit) {
self.consume_keyword(Keyword::Limit)?;
let first_expr = self.parse_expression()?;
// Check for comma syntax: LIMIT offset,count
if matches!(self.peek(), Token::Comma) {
self.advance(); // consume comma
let second_expr = self.parse_expression()?;
// In comma syntax, first is offset, second is count
(Some(second_expr), Some(first_expr))
} else {
(Some(first_expr), None)
}
} else {
(None, None)
};
// Parse OFFSET clause (only if not already set via comma syntax)
// SQLite allows expressions in OFFSET (e.g., OFFSET 10*2)
let offset = if offset_from_limit.is_some() {
offset_from_limit
} else if allow_order_limit && self.peek_keyword(Keyword::Offset) {
self.consume_keyword(Keyword::Offset)?;
Some(self.parse_expression()?)
} else {
None
};
// Check for LIMIT appearing before a set operation (SQLite-compatible error)
// This catches: SELECT ... LIMIT ... UNION SELECT ...
if limit.is_some() || offset.is_some() {
let op_name = if self.peek_keyword(Keyword::Union) {
// Check for UNION ALL vs UNION
let saved = self.position;
self.consume_keyword(Keyword::Union)?;
let all = self.peek_keyword(Keyword::All);
self.position = saved; // restore position
if all {
Some("UNION ALL")
} else {
Some("UNION")
}
} else if self.peek_keyword(Keyword::Intersect) {
Some("INTERSECT")
} else if self.peek_keyword(Keyword::Except) {
Some("EXCEPT")
} else {
None
};
if let Some(op) = op_name {
return Err(ParseError {
message: format!("LIMIT clause should come after {} not before", op),
});
}
}
// Issue #4448: Reject ORDER BY after LIMIT/OFFSET
// SQLite rejects: SELECT f1 FROM test1 LIMIT 5 OFFSET 1 ORDER BY f2
if (limit.is_some() || offset.is_some()) && self.peek_keyword(Keyword::Order) {
return Err(ParseError { message: self.peek().syntax_error() });
}
// Issue #4448: Validate no unexpected tokens before semicolon/EOF
// This catches incomplete input like: SELECT f1 FROM test1 AS 'hi', test2 AS
// and unexpected keywords like: SELECT f1 FROM test1 ORDER BY f1 desc, f2 where
//
// We only validate when validate_end_tokens is true. When false, we skip
// validation because the SELECT is embedded in another statement (cursor, view, etc.)
// that may have additional tokens.
//
// When allow_order_limit is false, we're nested inside a set operation and
// ORDER/LIMIT/OFFSET tokens belong to the outer statement.
//
// Even when validating, we allow RParen because SELECT can appear in:
// - Subqueries in FROM clause
// - CTEs (WITH ... AS (SELECT ...))
// - Parenthesized subexpressions
if validate_end_tokens {
if allow_order_limit {
// Top-level: only allow semicolon, EOF, or ) (for subqueries/CTEs).
// RETURNING is allowed only when the SELECT is the source of
// INSERT INTO t SELECT ... RETURNING ... (the clause belongs to
// the outer INSERT, issue #5263). For a bare SELECT, a trailing
// RETURNING is a syntax error, matching SQLite (issue #5271).
// ON is likewise allowed only as a DML source so that
// INSERT INTO t SELECT ... ON CONFLICT ... (SQLite upsert,
// issue #5269) and ... ON DUPLICATE KEY UPDATE ... parse,
// while a bare `SELECT 1 ON ...` remains a syntax error.
let valid_end_token = match self.peek() {
Token::Semicolon | Token::Eof | Token::RParen => true,
Token::Keyword { keyword: Keyword::Returning, .. } if allow_returning => true,
Token::Keyword { keyword: Keyword::On, .. } if allow_returning => true,
_ => false,
};
if !valid_end_token {
return Err(ParseError { message: self.peek().syntax_error() });
}
} else {
// Nested in set operation: also allow ORDER/LIMIT/OFFSET for outer statement
match self.peek() {
Token::Semicolon | Token::Eof | Token::RParen => {}
Token::Keyword { keyword: Keyword::Order, .. }
| Token::Keyword { keyword: Keyword::Limit, .. }
| Token::Keyword { keyword: Keyword::Offset, .. } => {}
// RETURNING belongs to an outer DML statement when this SELECT
// is the right side of a set operation in its source, e.g.
// INSERT INTO t SELECT 1 UNION SELECT 2 RETURNING a (issue #5263)
Token::Keyword { keyword: Keyword::Returning, .. } if allow_returning => {}
_ => return Err(ParseError { message: self.peek().syntax_error() }),
}
}
}
// Expect semicolon or EOF
if matches!(self.peek(), Token::Semicolon) {
self.advance();
}
Ok(vibesql_ast::SelectStmt {
with_clause,
distinct,
select_list,
into_table,
into_variables,
from,
where_clause,
group_by,
having,
window_definitions,
order_by,
limit,
offset,
set_operation,
values: None,
})
}
/// Parse a comma-separated list of CTEs
///
/// Syntax: cte_name [(col1, col2, ...)] AS (SELECT ...) [, ...]
///
/// If `recursive` is true, all CTEs in this list are marked as recursive.
/// In SQL:1999/SQLite, the RECURSIVE keyword applies to all CTEs in the WITH clause.
pub(crate) fn parse_cte_list(
&mut self,
recursive: bool,
) -> Result<Vec<vibesql_ast::CommonTableExpr>, ParseError> {
self.parse_comma_separated_list(|p| p.parse_cte(recursive))
}
/// Parse a single CTE definition
///
/// Syntax: cte_name [(col1, col2, ...)] AS (SELECT ...)
///
/// The `recursive` parameter indicates whether this CTE was declared in a
/// WITH RECURSIVE clause. In SQL:1999/SQLite, RECURSIVE applies to all CTEs
/// in the WITH clause, even if they don't actually recurse.
fn parse_cte(&mut self, recursive: bool) -> Result<vibesql_ast::CommonTableExpr, ParseError> {
// Parse CTE name
let name = match self.peek() {
Token::Identifier(id) => {
let name = id.clone();
self.advance();
name
}
// Allow unreserved keywords (like NULLS, TIMESTAMP, etc.) as CTE names
Token::Keyword { keyword: kw, .. } if kw.can_be_identifier() => {
let name = format!("{}", kw).to_lowercase();
self.advance();
name
}
_ => return Err(ParseError { message: "Expected CTE name (identifier)".to_string() }),
};
// Parse optional column list: (col1, col2, ...)
let columns = if matches!(self.peek(), Token::LParen) {
self.advance(); // consume '('
// Check for empty column list
if matches!(self.peek(), Token::RParen) {
return Err(ParseError { message: "CTE column list cannot be empty".to_string() });
}
// Parse comma-separated list of column identifiers
let cols = self.parse_comma_separated_list(|p| match p.peek() {
Token::Identifier(col) | Token::DelimitedIdentifier(col) => {
let name = col.clone();
p.advance();
Ok(name)
}
// Allow unreserved keywords as column names
Token::Keyword { keyword: kw, .. } if kw.can_be_identifier() => {
let name = format!("{}", kw).to_lowercase();
p.advance();
Ok(name)
}
_ => Err(ParseError {
message: "Expected column name in CTE column list".to_string(),
}),
})?;
// Expect closing paren
if !matches!(self.peek(), Token::RParen) {
return Err(ParseError {
message: "Expected ')' after CTE column list".to_string(),
});
}
self.advance(); // consume ')'
Some(cols)
} else {
None
};
// Expect AS keyword
self.expect_keyword(Keyword::As)?;
// Parse optional materialization hint: MATERIALIZED or NOT MATERIALIZED
let materialization = if self.peek_keyword(Keyword::Not) {
self.advance(); // consume NOT
self.expect_keyword(Keyword::Materialized)?;
vibesql_ast::CteMaterialization::NotMaterialized
} else if self.peek_keyword(Keyword::Materialized) {
self.advance(); // consume MATERIALIZED
vibesql_ast::CteMaterialization::Materialized
} else {
vibesql_ast::CteMaterialization::Default
};
// Expect opening paren for subquery
if !matches!(self.peek(), Token::LParen) {
return Err(ParseError {
message: "Expected '(' after AS in CTE definition".to_string(),
});
}
self.advance(); // consume '('
// Parse the SELECT or VALUES statement
// CTE body can be either SELECT or VALUES (both return SelectStmt)
let query = if self.peek_keyword(Keyword::Values) {
Box::new(self.parse_values_statement()?)
} else {
Box::new(self.parse_select_statement()?)
};
// Expect closing paren
if !matches!(self.peek(), Token::RParen) {
return Err(ParseError { message: "Expected ')' after CTE query".to_string() });
}
self.advance(); // consume ')'
Ok(vibesql_ast::CommonTableExpr { name, columns, query, recursive, materialization })
}
/// Parse a VALUES statement (standalone or in set operations)
///
/// Syntax: VALUES(expr, ...) [, (expr, ...), ...] [set_operation] [ORDER BY] [LIMIT] [OFFSET]
///
/// Examples:
/// - VALUES(1);
/// - VALUES(1,2,3);
/// - VALUES(1),(2),(3);
/// - VALUES(1) UNION VALUES(2);
pub(crate) fn parse_values_statement(&mut self) -> Result<vibesql_ast::SelectStmt, ParseError> {
self.parse_values_statement_internal(true, true, false)
}
/// Internal VALUES parser with control over ORDER BY/LIMIT parsing
///
/// The `validate_end_tokens` parameter controls whether to validate that no
/// unexpected tokens follow the statement.
///
/// The `allow_returning` parameter controls whether a trailing RETURNING keyword
/// is accepted as a valid end token (only when the VALUES is the source of a DML
/// statement, issue #5263). For bare VALUES statements it must be false (issue
/// #5271).
///
/// This is used by INSERT parser to handle compound VALUES statements like:
/// `INSERT INTO t VALUES(1) UNION VALUES(2)`
pub(crate) fn parse_values_statement_internal(
&mut self,
allow_order_limit: bool,
validate_end_tokens: bool,
allow_returning: bool,
) -> Result<vibesql_ast::SelectStmt, ParseError> {
// Parse the VALUES rows
let rows = self.parse_values_rows()?;
// Parse set operations (UNION, INTERSECT, EXCEPT)
let set_operation = if self.peek_keyword(Keyword::Union)
|| self.peek_keyword(Keyword::Intersect)
|| self.peek_keyword(Keyword::Except)
{
let op = if self.peek_keyword(Keyword::Union) {
self.consume_keyword(Keyword::Union)?;
vibesql_ast::SetOperator::Union
} else if self.peek_keyword(Keyword::Intersect) {
self.consume_keyword(Keyword::Intersect)?;
vibesql_ast::SetOperator::Intersect
} else {
self.consume_keyword(Keyword::Except)?;
vibesql_ast::SetOperator::Except
};
let all = if self.peek_keyword(Keyword::All) {
self.consume_keyword(Keyword::All)?;
true
} else if self.peek_keyword(Keyword::Distinct) {
self.consume_keyword(Keyword::Distinct)?;
false
} else {
false
};
// Parse the right-hand side (can be SELECT or VALUES)
let right = if matches!(self.peek(), Token::LParen) {
self.advance(); // consume '('
let stmt = if self.peek_keyword(Keyword::Values) {
self.parse_values_statement_internal(false, true, false)?
} else {
self.parse_select_statement_internal(false, true, false)?
};
if !matches!(self.peek(), Token::RParen) {
return Err(ParseError {
message: "Expected ')' after parenthesized statement in set operation"
.to_string(),
});
}
self.advance(); // consume ')'
Box::new(stmt)
} else if self.peek_keyword(Keyword::Values) {
Box::new(self.parse_values_statement_internal(false, true, allow_returning)?)
} else {
Box::new(self.parse_select_statement_internal(false, true, allow_returning)?)
};
Some(vibesql_ast::SetOperation { op, all, right })
} else {
None
};
// Parse ORDER BY (only if allowed)
let order_by = if allow_order_limit && self.peek_keyword(Keyword::Order) {
self.consume_keyword(Keyword::Order)?;
self.expect_keyword(Keyword::By)?;
let order_items = self.parse_comma_separated_list(|p| {
let expr = p.parse_expression()?;
let direction = if p.peek_keyword(Keyword::Asc) {
p.consume_keyword(Keyword::Asc)?;
vibesql_ast::OrderDirection::Asc
} else if p.peek_keyword(Keyword::Desc) {
p.consume_keyword(Keyword::Desc)?;
vibesql_ast::OrderDirection::Desc
} else {
vibesql_ast::OrderDirection::Asc
};
Ok(vibesql_ast::OrderByItem { expr, direction, nulls_order: None })
})?;
// Check for too many ORDER BY terms (SQLite compatibility)
if order_items.len() > super::super::MAX_ORDER_BY_TERMS {
return Err(ParseError {
message: "too many terms in ORDER BY clause".to_string(),
});
}
Some(order_items)
} else {
None
};
// Check for ORDER BY appearing before a set operation (SQLite-compatible error)
// This catches: VALUES(...) ORDER BY ... UNION VALUES(...)
if order_by.is_some() {
let op_name = if self.peek_keyword(Keyword::Union) {
// Check for UNION ALL vs UNION
let saved = self.position;
self.consume_keyword(Keyword::Union)?;
let all = self.peek_keyword(Keyword::All);
self.position = saved; // restore position
if all {
Some("UNION ALL")
} else {
Some("UNION")
}
} else if self.peek_keyword(Keyword::Intersect) {
Some("INTERSECT")
} else if self.peek_keyword(Keyword::Except) {
Some("EXCEPT")
} else {
None
};
if let Some(op) = op_name {
return Err(ParseError {
message: format!("ORDER BY clause should come after {} not before", op),
});
}
}
// Parse LIMIT (supports comma syntax: LIMIT offset,count)
let (limit, offset_from_limit) = if allow_order_limit && self.peek_keyword(Keyword::Limit) {
self.consume_keyword(Keyword::Limit)?;
let first_expr = self.parse_expression()?;
// Check for comma syntax: LIMIT offset,count
if matches!(self.peek(), Token::Comma) {
self.advance(); // consume comma
let second_expr = self.parse_expression()?;
// In comma syntax, first is offset, second is count
(Some(second_expr), Some(first_expr))
} else {
(Some(first_expr), None)
}
} else {
(None, None)
};
// Parse OFFSET (only if not already set via comma syntax)
let offset = if offset_from_limit.is_some() {
offset_from_limit
} else if allow_order_limit && self.peek_keyword(Keyword::Offset) {
self.consume_keyword(Keyword::Offset)?;
Some(self.parse_expression()?)
} else {
None
};
// Issue #4448: Reject ORDER BY after LIMIT/OFFSET
if (limit.is_some() || offset.is_some()) && self.peek_keyword(Keyword::Order) {
return Err(ParseError { message: self.peek().syntax_error() });
}
// Issue #4448: Validate no unexpected tokens before semicolon/EOF
// Only validate when validate_end_tokens is true. When false, the VALUES statement
// is embedded in another statement that may have additional tokens.
if validate_end_tokens {
// Note: RParen is valid because VALUES can appear in subqueries/CTEs
// Note: When allow_order_limit is false (nested in set operation right side),
// ORDER/LIMIT/OFFSET may follow and belong to the outer statement
let valid_end_token = match self.peek() {
Token::Semicolon | Token::Eof | Token::RParen => true,
// RETURNING belongs to an outer INSERT (INSERT INTO t VALUES
// ... RETURNING ..., issue #5263). For a bare VALUES statement a
// trailing RETURNING is a syntax error, matching SQLite (issue
// #5271).
Token::Keyword { keyword: Keyword::Returning, .. } if allow_returning => true,
// When nested, allow ORDER BY/LIMIT/OFFSET for outer statement
Token::Keyword { keyword: Keyword::Order, .. }
| Token::Keyword { keyword: Keyword::Limit, .. }
| Token::Keyword { keyword: Keyword::Offset, .. }
if !allow_order_limit =>
{
true
}
_ => false,
};
if !valid_end_token {
return Err(ParseError { message: self.peek().syntax_error() });
}
}
// Consume optional semicolon
if matches!(self.peek(), Token::Semicolon) {
self.advance();
}
Ok(vibesql_ast::SelectStmt {
with_clause: None,
distinct: false,
select_list: vec![],
into_table: None,
into_variables: None,
from: None,
where_clause: None,
group_by: None,
having: None,
window_definitions: None,
order_by,
limit,
offset,
set_operation,
values: Some(rows),
})
}
}