Skip to main content

spg_sql/
parser.rs

1//! Recursive-descent parser with a Pratt (precedence-climbing) sub-parser for
2//! expressions.
3//!
4//! Precedence (lowest → highest binding):
5//! `OR` (1) `<` `AND` (2) `<` `NOT` unary (3) `<`
6//! comparisons `=` `<>` `<` `<=` `>` `>=` (4) `<`
7//! `+` `-` (5) `<` `*` `/` (6) `<` unary `-` (7) `<` parens / atom.
8//!
9//! This matches PG's behaviour for the operators we support — e.g. `NOT a = b`
10//! parses as `NOT (a = b)` and `-a * b` as `(-a) * b`.
11
12use alloc::boxed::Box;
13use alloc::format;
14use alloc::string::{String, ToString};
15use alloc::vec;
16use alloc::vec::Vec;
17use core::fmt;
18use core::mem;
19
20use crate::ast::{
21    AssignTarget, BinOp, CastTarget, Collation, ColumnDef, ColumnName, ColumnTypeName,
22    CreateFunctionStatement, CreateIndexStatement, CreatePublicationStatement,
23    CreateSubscriptionStatement, CreateTableStatement, CreateTriggerStatement, DiscardTarget, Expr,
24    ExtractField, FkAction, ForeignKeyConstraint, FrameBound, FrameExclusion, FrameKind,
25    FromClause, FromJoin, FunctionArg, FunctionArgMode, FunctionArgType, FunctionAttrs,
26    FunctionBody, FunctionParallel, FunctionReturn, FunctionVolatility, GrantObject, GrantPriv,
27    GrantStatement, IndexMethod, InsertStatement, IsolationLevel, JoinKind, Literal, MysqlIntWidth,
28    NullTreatment, OrderBy, Overriding, PlPgSqlBlock, PlPgSqlDeclare, PlPgSqlStmt,
29    PublicationScope, RaiseLevel, RangeKindAst, ReturnTarget, SelectItem, SelectStatement,
30    Statement, TableRef, TriggerEvent, TriggerForEach, TriggerTiming, UnOp, UnionKind, VecEncoding,
31    WindowFrame,
32};
33use crate::lexer::{self, LexError, Token};
34
35/// v7.38 — a `WINDOW w AS (…)` definition body:
36/// `(PARTITION BY exprs, ORDER BY (expr, desc, nulls_first), frame)`.
37type WindowDef = (
38    Vec<Expr>,
39    Vec<(Expr, bool, Option<bool>)>,
40    Option<WindowFrame>,
41);
42
43/// v7.14.0 — true when the leading keyword of a top-level
44/// statement is one of the dump-emitted DDL forms SPG accepts
45/// as a no-op (no behavioural effect on the single-schema /
46/// single-database model). These statements are consumed up to
47/// the next `;` / EOF and returned as `Statement::Empty`.
48/// v7.39 (read01 round 57) — wrap a parsed GRANT body in the right statement.
49fn finish_grant(grant: bool, g: GrantStatement) -> Statement {
50    if grant {
51        Statement::Grant(g)
52    } else {
53        Statement::Revoke(g)
54    }
55}
56
57fn is_dump_noise_statement(lc: &str) -> bool {
58    matches!(
59        lc,
60        // v7.39 (read01 round 50): "comment" moved OUT — COMMENT ON is now a
61        // real statement with a real store. v7.39 (read01 round 57): "grant" /
62        // "revoke" moved OUT — table privileges are now REAL (stored in
63        // `relacl`, enforced against the session role); a grant on any other
64        // object class still parses and no-ops so dumps restore.
65        // MySQL bulk-load brackets.
66        "unlock"
67            // MySQL OPTIMIZE / ANALYZE TABLE / CHECK TABLE
68            // diagnostics that pg_dump-style tools also emit
69            // post-restore.
70            | "optimize"
71            | "check"
72            // PG psql backslash meta-commands that newer
73            // pg_dump versions emit unescaped (\restrict /
74            // \unrestrict). Real psql intercepts these; SPG's
75            // PG-wire sees them as raw text.
76            | "\\restrict"
77            | "\\unrestrict"
78            // v7.17.0 Phase 4.1 — MySQL `DELIMITER //` and
79            // `DELIMITER ;` directives. Technically client-side
80            // (the `mysql` CLI uses them to set the statement
81            // terminator), not SQL — but mysqldump and stored-
82            // procedure scripts emit them inline. SPG's parser
83            // sees one statement at a time and doesn't care
84            // about the terminator, so consume DELIMITER lines
85            // as Empty.
86            | "delimiter"
87            // v7.37.17 (17.6 siblings) — additional PG maintenance /
88            // session-state statements pg_dump + application startup
89            // scripts emit. SPG has no matching session-state to
90            // discard (no prepared-plan cache surface, no temp
91            // sequences), no matching security-label / storage-
92            // option to apply, no separate CREATE/DROP CAST that
93            // affects execution.
94            // v7.37.17 (17.6 siblings) — PG role-cleanup statements
95            // pg_dump / pg_dumpall emit around DROP ROLE:
96            //   REASSIGN OWNED BY <role> [, ...] TO <newrole>
97            //   DROP OWNED BY <role> [, ...] [CASCADE | RESTRICT]
98            // Both operate on the role's owned objects; SPG has no
99            // role-owner model, so accept-and-no-op.
100            // v7.37.17 (17.6 sibling) — LOAD '<library>'. pg_dump
101            // + extension scripts use LOAD to preload shared
102            // libraries. SPG doesn't have a shared-library extension
103            // point today (extensions ship as first-class crates
104            // linked at build time); accept as a no-op.
105            | "load"
106    )
107}
108
109/// v7.37.43-T4 — PG-unreserved keywords that are legal identifiers
110/// per `pg_get_keywords()`. SPG tokenizes these as named variants
111/// so the parser can dispatch on them in their owning contexts
112/// (`RELEASE SAVEPOINT`, `SHOW name`, `BEGIN`/`COMMIT`/`ROLLBACK`,
113/// `CREATE INDEX`, etc.), but they MUST stay usable as table /
114/// column / alias names — that's the PG contract for unreserved
115/// keywords (see PG docs Appendix C.1).
116///
117/// Before this generalisation, sentori migration 0001_init.sql
118/// `release TEXT NOT NULL` blew up the parser with "expected
119/// identifier, got Release", and the same gap stalked every
120/// SPG drop-in user whose schema had a column / alias named
121/// `release` / `index` / `tables` / `show` / `savepoint` /
122/// `begin` / `commit` / `rollback` / `drop` / `insert` / `values`
123/// / `limit` / `partition`. PG accepts all of them as identifiers
124/// when unquoted, so SPG must too.
125///
126/// Returns the canonical lowercase identifier text when the token
127/// belongs to PG's unreserved class, `None` otherwise. Used by
128/// `expect_ident_like` (column / table / alias names) so the
129/// generalisation applies everywhere an identifier may appear,
130/// not just in the contexts these tokens were introduced for.
131fn unreserved_keyword_text(tok: &Token) -> Option<String> {
132    let s = match tok {
133        // PG keyword class: unreserved or col_name.
134        //
135        Token::Release => "release",
136        Token::Savepoint => "savepoint",
137        Token::Show => "show",
138        Token::Index => "index",
139        Token::Begin => "begin",
140        Token::Commit => "commit",
141        Token::Rollback => "rollback",
142        Token::Drop => "drop",
143        Token::Insert => "insert",
144        Token::Values => "values",
145        Token::Limit => "limit",
146        Token::Partition => "partition",
147        Token::Tables => "tables",
148        Token::Connection => "connection",
149        Token::Publication => "publication",
150        Token::Subscription => "subscription",
151        Token::Interval => "interval",
152        // `extract` is non-reserved in PG too (it's a function the
153        // parser dispatches via context — outside that context it's
154        // a plain identifier).
155        Token::Extract => "extract",
156        Token::Offset => "offset",
157        // `to` is reserved in PG (used in many "AS … TO …" forms), so
158        // it is NOT relaxed here. Same for `from`, `where`, `as`,
159        // `select`, `not`, `and`, `or`, `null`, `true`, `false`,
160        // `create`, `table`, `into`, `on`, `order`, `by`, `having`,
161        // `group`, `distinct`, `union`, `all`, `join`, `inner`,
162        // `left`, `cross`, `outer`, `default`, `is`, `between`,
163        // `in`, `like`, `for`, `except`, `desc`, `asc`, `partition`
164        // (partial — keep partition as unreserved per modern PG).
165        _ => return None,
166    };
167    Some(s.to_string())
168}
169
170/// v7.9.22 — recognise pgvector / SPG vector-index opclass names
171/// in CREATE INDEX. SPG's HNSW already routes by query operator;
172/// the opclass is accepted for `pg_dump` compatibility (mailrs
173/// migration follow-up G5).
174/// v7.13.0 — extended to recognise PG built-in / pg_trgm opclasses
175/// (mailrs round-5 G5). These are tokens-only acceptance — SPG
176/// doesn't change index behaviour based on them.
177/// v7.37.17 (17.6 siblings) — the four PG `each` SRFs share one
178/// FROM-clause pipeline; the stored name tells the executor whether
179/// the value column keeps JSON rendering (`jsonb_each` / `json_each`)
180/// or unwraps to text (`*_each_text`).
181fn is_json_each_name(s: &str) -> bool {
182    s.eq_ignore_ascii_case("jsonb_each_text")
183        || s.eq_ignore_ascii_case("jsonb_each")
184        || s.eq_ignore_ascii_case("json_each_text")
185        || s.eq_ignore_ascii_case("json_each")
186}
187
188/// v7.38 (read01, T14) — resolve named function arguments (`argname => value`)
189/// to positional order for the `make_*` family (the AST stays positional).
190/// Positional args fill slots left-to-right; a named arg goes to its registered
191/// slot; unfilled slots default to integer 0 (PG's optional make_interval
192/// fields — the make_date/time arity is still checked at eval time).
193fn reorder_named_args(
194    fname: &str,
195    args: Vec<Expr>,
196    names: &[Option<String>],
197) -> Result<Vec<Expr>, String> {
198    let params: &[&str] = match fname.to_ascii_lowercase().as_str() {
199        "make_date" => &["year", "month", "day"],
200        "make_time" => &["hour", "min", "sec"],
201        "make_timestamp" | "make_timestamptz" => &["year", "month", "mday", "hour", "min", "sec"],
202        "make_interval" => &["years", "months", "weeks", "days", "hours", "mins", "secs"],
203        other => {
204            return Err(alloc::format!(
205                "function {other}(...) does not support named arguments"
206            ));
207        }
208    };
209    let mut slots: Vec<Option<Expr>> = (0..params.len()).map(|_| None).collect();
210    let mut next_positional = 0usize;
211    for (arg, name) in args.into_iter().zip(names.iter()) {
212        let idx = match name {
213            Some(n) => params
214                .iter()
215                .position(|p| p.eq_ignore_ascii_case(n))
216                .ok_or_else(|| alloc::format!("{fname}(...) has no argument named \"{n}\""))?,
217            None => {
218                let i = next_positional;
219                next_positional += 1;
220                i
221            }
222        };
223        if idx >= slots.len() {
224            return Err(alloc::format!("too many arguments for {fname}(...)"));
225        }
226        if slots[idx].is_some() {
227            return Err(alloc::format!(
228                "argument \"{}\" specified more than once",
229                params[idx]
230            ));
231        }
232        slots[idx] = Some(arg);
233    }
234    Ok(slots
235        .into_iter()
236        .map(|s| s.unwrap_or(Expr::Literal(Literal::Integer(0))))
237        .collect())
238}
239
240/// v7.38 (read01) — parse a lexer `Token::Numeric` source string (digits with
241/// an optional single `.`, no sign, no exponent) into `(unscaled, scale)` for
242/// `Literal::Numeric`. Returns `None` if the mantissa overflows i128.
243/// v7.39 (read01 numeric.c) — the result of expanding an `1.5e3`-style
244/// scientific literal into PG's plain NUMERIC decimal form.
245#[derive(Debug)]
246pub enum SciExpanded {
247    /// Plain decimal string ("1.5e3" → "1500", "1e-5" → "0.00001").
248    Expanded(String),
249    /// Exponent pushes the value outside PG's numeric format
250    /// (more than 131072 integer digits or 16383 fractional digits).
251    Overflow,
252    /// Not a `[±]digits[.digits]e[±]digits` literal at all.
253    NotScientific,
254}
255
256/// Expand scientific notation into a plain decimal string by moving the
257/// decimal point — no float round-trip, so the value stays exact. PG treats
258/// such literals as NUMERIC; the digit-count caps mirror PG's numeric format
259/// limits ("value overflows numeric format").
260pub fn expand_scientific_literal(s: &str) -> SciExpanded {
261    let s = s.trim();
262    let Some(epos) = s.find(['e', 'E']) else {
263        return SciExpanded::NotScientific;
264    };
265    let (mant, exp_str) = (&s[..epos], &s[epos + 1..]);
266    let Ok(exp) = exp_str.parse::<i64>() else {
267        return SciExpanded::NotScientific;
268    };
269    let (neg, mant) = match mant.strip_prefix('-') {
270        Some(r) => (true, r),
271        None => (false, mant.strip_prefix('+').unwrap_or(mant)),
272    };
273    let (int_part, frac_part) = match mant.split_once('.') {
274        Some((i, f)) => (i, f),
275        None => (mant, ""),
276    };
277    if (int_part.is_empty() && frac_part.is_empty())
278        || !int_part.bytes().all(|b| b.is_ascii_digit())
279        || !frac_part.bytes().all(|b| b.is_ascii_digit())
280    {
281        return SciExpanded::NotScientific;
282    }
283    let mut digits = String::with_capacity(int_part.len() + frac_part.len());
284    digits.push_str(int_part);
285    digits.push_str(frac_part);
286    // Decimal point position within `digits` after applying the exponent.
287    let new_point = int_part.len() as i64 + exp;
288    // PG's numeric format: up to 131072 digits before the point, 16383 after.
289    if new_point > 131_072 {
290        return SciExpanded::Overflow;
291    }
292    if (digits.len() as i64 - new_point) > 16_383 {
293        return SciExpanded::Overflow;
294    }
295    let sign = if neg { "-" } else { "" };
296    let plain = if new_point <= 0 {
297        let mut out = String::with_capacity(digits.len() + 2 + (-new_point) as usize);
298        out.push_str("0.");
299        for _ in 0..(-new_point) {
300            out.push('0');
301        }
302        out.push_str(&digits);
303        out
304    } else if (new_point as usize) >= digits.len() {
305        let mut out = digits;
306        for _ in 0..(new_point as usize - out.len()) {
307            out.push('0');
308        }
309        out
310    } else {
311        let mut out = String::with_capacity(digits.len() + 1);
312        out.push_str(&digits[..new_point as usize]);
313        out.push('.');
314        out.push_str(&digits[new_point as usize..]);
315        out
316    };
317    SciExpanded::Expanded(alloc::format!("{sign}{plain}"))
318}
319
320/// v7.39 (round 367, M20) — lower a MySQL hexadecimal binary-string
321/// literal (`0x…` / `X'…'`) onto the existing bytea cast. The hex digits
322/// are left-padded to an even count (`0x123` → byte string `01 23`, per
323/// MariaDB) and handed to the PG bytea input format (`\x…`).
324#[inline(never)]
325fn hex_literal_to_bytea_expr(hex: &str) -> Expr {
326    let padded = if hex.len() % 2 == 1 {
327        alloc::format!("0{hex}")
328    } else {
329        hex.to_string()
330    };
331    Expr::Cast {
332        expr: alloc::boxed::Box::new(Expr::Literal(Literal::String(alloc::format!(
333            "\\x{padded}"
334        )))),
335        target: CastTarget::Named("bytea".to_string()),
336    }
337}
338
339/// v7.39 (round 367, M20) — lower a MySQL bit-value literal (`b'1010'`)
340/// onto the bytea cast. The bits are read big-endian and left-padded to a
341/// whole number of bytes (`b'1010'` → one byte `0x0A`, per MariaDB).
342#[inline(never)]
343fn bits_literal_to_bytea_expr(bits: &str) -> Expr {
344    let pad = (8 - bits.len() % 8) % 8;
345    let mut hex = String::with_capacity((bits.len() + pad).div_ceil(4));
346    let padded: String = core::iter::repeat_n('0', pad).chain(bits.chars()).collect();
347    for nibble in padded.as_bytes().chunks(4) {
348        let mut v = 0u8;
349        for &b in nibble {
350            v = (v << 1) | (b - b'0');
351        }
352        hex.push(char::from_digit(u32::from(v), 16).unwrap_or('0'));
353    }
354    hex_literal_to_bytea_expr(&hex)
355}
356
357/// Resolve a lexer `Token::Numeric` into its literal. PG semantics: a dotted
358/// or over-i64 literal is exact NUMERIC; scientific notation is NUMERIC too
359/// (expanded to the plain decimal form); only a fractional depth beyond SPG's
360/// scale width (u8) falls back to double precision.
361///
362/// v7.38.19 — that sentence used to end "(recorded delta)". Measured
363/// against PG 18.4: a literal with 300 fractional digits round-trips
364/// identically on both engines, so whatever the note described is gone.
365/// It is RD-7 in `docs/RECORDED_DELTAS.md`, under "corrected by
366/// measurement" rather than under "open".
367/// Kept out of the parse_expr recursion frame — see the call site.
368#[inline(never)]
369fn numeric_token_to_literal(s: String) -> Result<Literal, String> {
370    match parse_decimal_literal(&s) {
371        Some((unscaled, scale)) => Ok(Literal::Numeric { unscaled, scale }),
372        // v7.38 (read01, T3.C3) — a plain decimal too wide for i128 keeps
373        // its exact value as a NumericBig.
374        None if !s.contains(['e', 'E']) => Ok(Literal::NumericBig(s)),
375        // v7.39 (read01 numeric.c) — expand the exponent form.
376        None => match expand_scientific_literal(&s) {
377            SciExpanded::Expanded(plain) => match parse_decimal_literal(&plain) {
378                Some((unscaled, scale)) => Ok(Literal::Numeric { unscaled, scale }),
379                None if plain
380                    .split_once('.')
381                    .is_none_or(|(_, f)| u8::try_from(f.len()).is_ok()) =>
382                {
383                    Ok(Literal::NumericBig(plain))
384                }
385                None => s
386                    .parse::<f64>()
387                    .map(Literal::Float)
388                    .map_err(|_| format!("invalid numeric literal {s:?}")),
389            },
390            SciExpanded::Overflow => Err("value overflows numeric format".to_string()),
391            SciExpanded::NotScientific => s
392                .parse::<f64>()
393                .map(Literal::Float)
394                .map_err(|_| format!("invalid numeric literal {s:?}")),
395        },
396    }
397}
398
399fn parse_decimal_literal(s: &str) -> Option<(i128, u16)> {
400    let (int_part, frac_part) = match s.split_once('.') {
401        Some((i, f)) => (i, f),
402        None => (s, ""),
403    };
404    // v7.39 (round 271) — was u8::MAX. A literal with 256 decimal
405    // places fell out of the numeric path here, which is why
406    // `pg_typeof(1e-256)` answered double precision and a plain
407    // 256-place decimal aborted the query in the big-decimal converter.
408    if frac_part.len() > u16::MAX as usize {
409        return None;
410    }
411    let mut digits = String::with_capacity(int_part.len() + frac_part.len());
412    digits.push_str(int_part);
413    digits.push_str(frac_part);
414    let mantissa: i128 = digits.parse().ok()?;
415    #[allow(clippy::cast_possible_truncation)]
416    Some((mantissa, frac_part.len() as u16))
417}
418
419/// `jsonb_to_record` / `jsonb_to_recordset` (+ `json_` variants) — the
420/// record-returning JSON functions that take a `AS alias(col type, …)`
421/// column-definition list in FROM position.
422fn is_json_to_record_name(s: &str) -> bool {
423    s.eq_ignore_ascii_case("jsonb_to_recordset")
424        || s.eq_ignore_ascii_case("jsonb_to_record")
425        // v7.39 (read01 jsonfuncs.c) — the populate family with an AS
426        // column-definition list desugars identically (the record base
427        // argument only carries the type; a non-NULL base's field
428        // defaults are a recorded delta, RD-6).
429        || s.eq_ignore_ascii_case("json_populate_record")
430        || s.eq_ignore_ascii_case("jsonb_populate_record")
431        || s.eq_ignore_ascii_case("json_populate_recordset")
432        || s.eq_ignore_ascii_case("jsonb_populate_recordset")
433        || s.eq_ignore_ascii_case("json_to_recordset")
434        || s.eq_ignore_ascii_case("json_to_record")
435}
436
437impl Parser {
438    /// Whether what follows an identifier ends an index key, which is how
439    /// an operator class is told from anything else in that position.
440    fn opclass_position_follows(next: Option<&Token>) -> bool {
441        match next {
442            // `ASC` / `DESC` have their own tokens; matching them as
443            // identifiers named "asc" / "desc" — which the first version of
444            // this did — never fires, and `(c text_pattern_ops DESC)` (which
445            // PG18.4 accepts, verified) went on failing to parse.
446            Some(Token::Comma | Token::RParen | Token::Asc | Token::Desc) => true,
447            Some(Token::Ident(w)) => {
448                w.eq_ignore_ascii_case("nulls") || w.eq_ignore_ascii_case("collate")
449            }
450            _ => false,
451        }
452    }
453}
454
455fn is_vector_opclass_name(name: &str) -> bool {
456    let lc = name.to_ascii_lowercase();
457    matches!(
458        lc.as_str(),
459        "vector_cosine_ops"
460            | "vector_l2_ops"
461            | "vector_ip_ops"
462            | "halfvec_cosine_ops"
463            | "halfvec_l2_ops"
464            | "halfvec_ip_ops"
465            | "sq8_cosine_ops"
466            | "sq8_l2_ops"
467            | "sq8_ip_ops"
468            // pg_trgm — trigram operator class. SPG's GIN index
469            // already uses tsvector tokens; trigram-style LIKE
470            // pattern matching still routes through a sequential
471            // scan, but the opclass name is accepted so PG schemas
472            // load.
473            | "gin_trgm_ops"
474            | "gist_trgm_ops"
475            // PG built-in btree opclasses occasionally appear in
476            // pg_dump output for column types with multiple
477            // sort orders (text_pattern_ops, varchar_pattern_ops,
478            // bpchar_pattern_ops).
479            | "text_pattern_ops"
480            | "varchar_pattern_ops"
481            | "bpchar_pattern_ops"
482            | "int4_ops"
483            | "int8_ops"
484            | "text_ops"
485    )
486}
487
488#[derive(Debug, Clone, PartialEq, Eq)]
489pub struct ParseError {
490    pub message: String,
491    /// Index into the token stream where parsing tripped. Not a byte offset.
492    /// v7.39 (read01 round 95) — the byte/char position is NOT stored here: a
493    /// field would grow every `Result<_, ParseError>` slot on the deeply
494    /// recursive parse stack and tip the nesting-budget frame cliff. PG's
495    /// 1-based char position is recovered on the cold error path by
496    /// [`syntax_error_position`], which re-tokenizes to map this token index.
497    pub token_pos: usize,
498}
499
500impl fmt::Display for ParseError {
501    /// v7.39 (round 322/V24) — the message ALONE. It used to be prefixed
502    /// with `parse error at token #N: `, which PG has no equivalent of:
503    /// the message bodies are already PG's verbatim (`LIMIT must not be
504    /// negative`, `invalid input syntax for type bigint: "abc"`), and the
505    /// prefix was SPG's internal token index leaking into every one of
506    /// them. `token_pos` stays a field — the wire recovers PG's 1-based
507    /// character position from it for the ErrorResponse `P`.
508    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
509        f.write_str(&self.message)
510    }
511}
512
513impl From<LexError> for ParseError {
514    fn from(e: LexError) -> Self {
515        Self {
516            message: format!("lex: {e}"),
517            token_pos: 0,
518        }
519    }
520}
521
522/// v7.9.30 — parse a single expression (no trailing junk). Used by
523/// the engine to re-hydrate stored partial-index / unique-index
524/// predicates from their canonical Display form. The same Pratt
525/// parser the statement path uses; this entry point just skips the
526/// statement dispatch.
527pub fn parse_expression(input: &str) -> Result<Expr, ParseError> {
528    let (tokens, offsets) = lexer::tokenize_with_offsets(input, lexer::Dialect::PG)
529        .map_err(|e| shape_lex_error(&e, input))?;
530    let mut p = Parser::new(tokens);
531    let expr = p
532        .parse_expr(0)
533        .and_then(|e| p.expect_eof().map(|()| e))
534        .map_err(|e| shape_syntax_error(e, input, &offsets))?;
535    Ok(expr)
536}
537
538/// Parse exactly one statement, swallow an optional trailing `;`, and require
539/// the token stream to end there. PG string semantics.
540pub fn parse_statement(input: &str) -> Result<Statement, ParseError> {
541    parse_statement_with(input, lexer::Dialect::PG)
542}
543
544/// v7.22 (round-13 T3) — dialect-aware entry: `backslash_escapes`
545/// selects MySQL-style string lexing (see `lexer::tokenize_with`).
546/// The engine threads its session flag through here.
547pub fn parse_statement_with(input: &str, dialect: lexer::Dialect) -> Result<Statement, ParseError> {
548    let (tokens, offsets, merges) =
549        lexer::tokenize_with_merges(input, dialect).map_err(|e| shape_lex_error(&e, input))?;
550    // v7.39.2 — the grammar follows "is this MySQL", the lexer follows
551    // "does backslash escape". They used to be one flag, and a session
552    // that turned escapes off lost the grammar with them.
553    let mut p = Parser::new_with_dialect(tokens, dialect.speaks_mysql)
554        .with_source(input, &offsets)
555        .with_merges(merges);
556    let stmt = (|| {
557        let stmt = p.parse_one_statement()?;
558        if matches!(p.peek(), Token::Semicolon) {
559            p.advance();
560        }
561        p.expect_eof()?;
562        Ok(stmt)
563    })()
564    .map_err(|e: ParseError| shape_syntax_error(e, input, &offsets))?;
565    Ok(stmt)
566}
567
568/// v7.39 (round 340, V56) — PG has exactly two syntax-error wordings:
569/// `syntax error at or near "<token>"` and `syntax error at end of input`
570/// (measured on 18.4 across a dozen shapes). SPG wrote its own per-site
571/// prose — `expected identifier, got Eof`, `unexpected token From in
572/// expression`, `expected end of input, got Ident("with")` — which named
573/// internal token types and, in the Debug forms, leaked the parser's own
574/// enum into a message clients read.
575///
576/// Applied once on the way out, so every construction site is covered and
577/// the token named is the one the error itself points at. Messages whose
578/// bodies are already PG's verbatim (`LIMIT must not be negative`,
579/// `invalid input syntax for type bigint: "abc"`) are left alone — those
580/// are PG's own errors, not its syntax error.
581fn shape_syntax_error(e: ParseError, input: &str, offsets: &[usize]) -> ParseError {
582    if !(e.message.starts_with("expected ") || e.message.starts_with("unexpected token ")) {
583        return e;
584    }
585    let message = match offending_lexeme(input, offsets, e.token_pos) {
586        Some(tok) => alloc::format!("syntax error at or near \"{tok}\""),
587        None => "syntax error at end of input".into(),
588    };
589    ParseError {
590        message,
591        token_pos: e.token_pos,
592    }
593}
594
595/// v7.39 (round 340, V56) — a lexer-level failure the way PG words it.
596/// Measured on 18.4: `unterminated quoted string at or near "'abc"`,
597/// `unterminated quoted identifier at or near ""abc"`, `unterminated /*
598/// comment at or near "/* x"` — the quoted part runs from the opening
599/// delimiter to the end of the input. SPG reported its own internal
600/// shape instead (`unterminated string literal at byte 7`), which named
601/// a byte offset no client can use.
602fn shape_lex_error(e: &lexer::LexError, input: &str) -> ParseError {
603    use lexer::LexErrorKind as K;
604    let from_here = input.get(e.pos..).map(str::trim_end).unwrap_or("");
605    let message = match &e.kind {
606        K::UnterminatedString => {
607            alloc::format!("unterminated quoted string at or near \"{from_here}\"")
608        }
609        K::UnterminatedQuotedIdent => {
610            alloc::format!("unterminated quoted identifier at or near \"{from_here}\"")
611        }
612        K::UnterminatedBlockComment => {
613            alloc::format!("unterminated /* comment at or near \"{from_here}\"")
614        }
615        // PG has no "unknown character" error of its own — the character
616        // is skipped and the parser reports the next token. SPG stops at
617        // the character itself and names it, which is the same shape.
618        K::UnknownChar(c) => alloc::format!("syntax error at or near \"{c}\""),
619        // The number-literal kinds already carry PG's `at or near` form.
620        other => alloc::format!(
621            "{}",
622            lexer::LexError {
623                kind: other.clone(),
624                pos: e.pos,
625            }
626        ),
627    };
628    ParseError {
629        message,
630        token_pos: 0,
631    }
632}
633
634/// The offending token exactly as it appears in the input, or `None` at
635/// end of input. PG echoes the source spelling — a lower-case `frm`
636/// reports as `frm`, not as a canonicalised keyword.
637fn offending_lexeme<'a>(input: &'a str, offsets: &[usize], token_pos: usize) -> Option<&'a str> {
638    let start = *offsets.get(token_pos)?;
639    if start >= input.len() {
640        return None;
641    }
642    let end = offsets
643        .get(token_pos + 1)
644        .copied()
645        .unwrap_or(input.len())
646        .min(input.len());
647    let seg = input.get(start..end)?.trim();
648    if seg.is_empty() {
649        return None;
650    }
651    // A quoted literal / identifier keeps its inner spaces; anything else
652    // ends at the first whitespace (the segment runs to the NEXT token's
653    // start, which may swallow a comment).
654    if seg.starts_with('\'') || seg.starts_with('"') || seg.starts_with('`') {
655        Some(seg)
656    } else {
657        seg.split_whitespace().next()
658    }
659}
660
661/// v7.39 (read01 round 95) — recover PG's 1-based CHARACTER error position for
662/// a [`ParseError::token_pos`]. Kept off the `ParseError` struct (and so off
663/// every recursive `Result` slot) to protect the nesting-budget frame cliff:
664/// this re-tokenizes `input` on the cold error path to map the failing token
665/// index to its start byte, then to a character offset. The dialect
666/// must match the parse that produced `token_pos` (it barely shifts offsets,
667/// but stay consistent). Returns `None` when the index has no offset or the
668/// byte isn't a char boundary. The wire attaches it as the ErrorResponse `P`.
669#[must_use]
670pub fn syntax_error_position(
671    input: &str,
672    dialect: lexer::Dialect,
673    token_pos: usize,
674) -> Option<usize> {
675    let (_, offsets) = lexer::tokenize_with_offsets(input, dialect).ok()?;
676    let byte_off = *offsets.get(token_pos)?;
677    if byte_off > input.len() || !input.is_char_boundary(byte_off) {
678        return None;
679    }
680    Some(input[..byte_off].chars().count() + 1)
681}
682
683struct Parser {
684    tokens: Vec<Token>,
685    pos: usize,
686    /// v7.39 (round 274) — the session's dialect, carried by the same
687    /// signal that drives string-literal escaping: `SET sql_mode` (only
688    /// MySQL clients and mysqldump preambles emit it) turns it on,
689    /// `SET standard_conforming_strings` (every pg_dump preamble) turns
690    /// it off. Needed here because the two dialects disagree about what
691    /// `REAL` means — see the type mapping below.
692    mysql_dialect: bool,
693    /// v7.30.2 (mailrs round-25 ask 2) — live nesting depth of the
694    /// mutually recursive expr/select parsers. Bounded so a deeply
695    /// nested input returns a parse error instead of overflowing
696    /// the stack (embed hosts die on overflow — it is an abort,
697    /// not a catchable error).
698    nest_depth: usize,
699    /// TABLESAMPLE lowering channel: the table-ref parser pushes a
700    /// `random() < p/100` predicate here; the enclosing SELECT
701    /// drains the list after its WHERE parses and ANDs the
702    /// predicates in. parse_bare_select save/restores around its
703    /// FROM+WHERE so nested selects only drain their own.
704    pending_sample_preds: Vec<Expr>,
705    /// v7.38.19 — the target of a `SELECT … INTO <table>`, carried out
706    /// of `parse_bare_select` (which returns a `SelectStatement` and has
707    /// nowhere to put it) to the caller that lowers the pair to the CTAS
708    /// node. `bool` is `TEMP`.
709    pending_select_into: Option<(String, bool)>,
710    /// v7.39 (round 691) — collation lowering channel, the same shape as
711    /// `pending_sample_preds` above. `expr COLLATE "name"` is ORDERING
712    /// information, and `ast::OrderBy` is where this parser keeps ordering
713    /// information (`desc`, `nulls_first`); the alternative — a new `Expr`
714    /// variant — puts a new arm on `eval_expr`, which this repo has
715    /// measured to overflow the debug stack. So while an ORDER BY KEY is
716    /// being parsed the postfix loop drops the name here instead of
717    /// refusing it, and the key's parser takes it.
718    ///
719    /// Only inside an ORDER BY key: everywhere else an unperformable
720    /// collation still errors, because accepting one at a COMPARISON and
721    /// ignoring it is the defect F36 exists to close.
722    in_order_by_key: bool,
723    order_key_collation: Option<String>,
724    /// POSITION(sub IN str) — while parsing the needle, the IN
725    /// keyword is the argument separator, not a membership test.
726    /// The postfix loop leaves IN unconsumed when this is set.
727    suppress_in_tail: bool,
728    /// Index of the token the last `advance()` returned — see
729    /// [`Parser::consumed_pos`].
730    last_consumed: usize,
731    /// v7.39 (round 506) — the statement's own text and the byte each token
732    /// starts at, so a MySQL projection item can report the SOURCE TEXT
733    /// MariaDB reports: `SELECT a  +  b` names its column `a  +  b`,
734    /// spacing and all. Only filled for a MySQL session — a PG one names
735    /// columns from the parsed shape and pays nothing for this.
736    src: Option<(String, Vec<usize>)>,
737    /// v7.39.3 — (token index, first-segment byte length) for every
738    /// string literal the lexer built by implicit concatenation.
739    merges: Vec<(usize, usize)>,
740}
741
742/// Max expr/select parser nesting (parens, subqueries, CASE, …).
743/// Real SQL nests a few dozen levels at the extreme. Each nesting level
744/// costs a parse_expr→parse_unary→parse_atom frame chain, so the budget
745/// exists to turn a deep statement into a catchable parse ERROR: a stack
746/// overflow is an abort, and in the server it does not fail one query, it
747/// takes the process down and every other connection with it.
748///
749/// v7.39 (round 507) — measured, because the figure here used to be a
750/// guess ("over 10 KiB in debug … comfortably inside a 2 MiB worker stack
751/// in BOTH debug and release"), and the debug half of that is wrong by
752/// more than an order of magnitude:
753///
754///   * RELEASE, on a 2 MiB worker stack: every recursive shape reaches
755///     this budget and errors. Verified against a live server for nested
756///     derived tables, parens, calls, CASE, IN-subqueries, scalar
757///     subqueries, NOT and unary minus — the server stayed up through all
758///     of them. This is the contract that matters, and it holds.
759///   * DEBUG: nested derived tables cost roughly 235 KiB of stack PER
760///     LEVEL, so parsing alone aborts around 35 levels on an 8 MiB stack
761///     and executing aborts around 8 inside a test thread. The budget is
762///     simply unreachable there, which is why a deep-nesting test has to
763///     ask for a large stack of its own — see `nesting_budget_errors_at`
764///     in the parser tests.
765/// v7.39 (round 541) — the pg_catalog relations SPG synthesises, in
766/// one place.
767///
768/// There were two copies of this fact: a curated list, used for BARE
769/// names, and — in `try_peek_meta_qualified` — no list at all, which
770/// rewrote `pg_catalog.<anything>` to `__spg_pg_<anything>` and left
771/// the engine to complain about a view it could not materialise. So
772/// writing the schema qualifier CHANGED THE ANSWER: `pg_stat_activity`
773/// had rows, `pg_catalog.pg_stat_activity` was an error.
774///
775/// PG puts `pg_catalog` at the implicit front of every search_path, so
776/// the two spellings name the same relation and must resolve the same
777/// way. Names NOT here (`pg_stat_activity`, `pg_locks`,
778/// `pg_stat_statements`, `pg_statio_user_tables`) route through the
779/// meta_view_result path under their own names and must not be
780/// rewritten; a name that is neither reaches the ordinary resolver,
781/// which reports that the relation does not exist — PG's answer.
782const SYNTHESISED_PG_CATALOGS: &[&str] = &[
783    "pg_am",
784    "pg_attrdef",
785    "pg_attribute",
786    "pg_cast",
787    "pg_db_role_setting",
788    "pg_conversion",
789    "pg_default_acl",
790    "pg_shadow",
791    "pg_sequences",
792    "pg_range",
793    "pg_partitioned_table",
794    "pg_language",
795    "pg_group",
796    "pg_authid",
797    "pg_class",
798    "pg_collation",
799    "pg_constraint",
800    "pg_database",
801    "pg_depend",
802    "pg_amop",
803    "pg_amproc",
804    "pg_opclass",
805    "pg_opfamily",
806    // v7.39 (read01 round 50) — COMMENT ON store, PG's pg_description.
807    "pg_description",
808    "pg_enum",
809    "pg_extension",
810    // v7.39 (round 541) — pg_dump reads it for every relation of kind
811    // 'f'. SPG has no foreign tables, so it is empty, which is also
812    // what PG reports on a database that has none.
813    "pg_foreign_table",
814    // v7.39 (round 541) — the empty-by-truth family; see
815    // EMPTY_PG_CATALOGS in spg-engine::system_catalog.
816    "pg_event_trigger",
817    "pg_file_settings",
818    "pg_foreign_data_wrapper",
819    "pg_foreign_server",
820    "pg_hba_file_rules",
821    "pg_ident_file_mappings",
822    "pg_init_privs",
823    "pg_parameter_acl",
824    "pg_prepared_xacts",
825    "pg_publication_namespace",
826    "pg_publication_rel",
827    "pg_publication_tables",
828    "pg_replication_origin",
829    "pg_replication_origin_status",
830    "pg_seclabel",
831    "pg_seclabels",
832    "pg_shdepend",
833    "pg_shdescription",
834    "pg_shmem_allocations",
835    "pg_shmem_allocations_numa",
836    "pg_shseclabel",
837    "pg_statistic_ext_data",
838    "pg_stats_ext",
839    "pg_stats_ext_exprs",
840    "pg_subscription_rel",
841    "pg_transform",
842    "pg_user_mapping",
843    "pg_user_mappings",
844    "pg_index",
845    "pg_indexes",
846    "pg_inherits",
847    // v7.39 (round 650) — the text-search catalogs SPG can fill
848    // honestly. `pg_ts_config_map` is deliberately NOT here: it maps
849    // token types to dictionaries and SPG has no token-type model,
850    // the same gap that leaves `ts_token_type` / `ts_debug` unbuilt.
851    "pg_ts_config",
852    "pg_ts_config_map",
853    "pg_ts_dict",
854    "pg_ts_parser",
855    "pg_ts_template",
856    "pg_matviews",
857    "pg_namespace",
858    // v7.39 (round 621)
859    "pg_operator",
860    "pg_policies",
861    "pg_policy",
862    "pg_proc",
863    "pg_publication",
864    "pg_replication_slots",
865    "pg_roles",
866    // v7.39 (round 143) — the rewrite-rule listing view.
867    // v7.39 (round 312) — and the rule catalogue itself, which
868    // `pg_get_ruledef(oid)` resolves against.
869    "pg_rewrite",
870    "pg_rules",
871    "pg_sequence",
872    "pg_settings",
873    "pg_stat_archiver",
874    "pg_stat_bgwriter",
875    "pg_stat_checkpointer",
876    "pg_stat_database",
877    "pg_stat_io",
878    "pg_stat_progress_analyze",
879    "pg_auth_members",
880    "pg_stat_progress_create_index",
881    "pg_stat_progress_vacuum",
882    "pg_stat_replication",
883    "pg_stat_slru",
884    "pg_stat_subscription_stats",
885    "pg_stat_user_functions",
886    "pg_stat_user_indexes",
887    "pg_stat_user_tables",
888    "pg_stat_wal",
889    "pg_prepared_statements",
890    "pg_largeobject",
891    "pg_largeobject_metadata",
892    "pg_statistic",
893    "pg_statistic_ext",
894    // v7.38.18 — the readable view over pg_statistic.
895    "pg_stats",
896    "pg_subscription",
897    "pg_tables",
898    "pg_tablespace",
899    // v7.39 (round 502) — the timezone catalogues. SPG resolved
900    // named zones correctly but could not list them, so a client
901    // populating a timezone picker got "relation does not exist".
902    "pg_timezone_abbrevs",
903    "pg_timezone_names",
904    "pg_trigger",
905    "pg_type",
906    "pg_user",
907    "pg_views",
908];
909
910const MAX_NEST_DEPTH: usize = 64;
911
912/// Stack accounting for the nesting budget, test-only.
913///
914/// `MAX_NEST_DEPTH` is a fixed count calibrated against a frame size
915/// that MOVES: a compiler upgrade grew the parser's debug frames and
916/// silently ate the margin until `nesting_budget_errors_cleanly` went
917/// from erroring cleanly to aborting on a stack overflow. A count
918/// cannot notice that on its own, so the budget is measured here and
919/// held to a ceiling.
920///
921/// The reading has to come from a helper whose OWN frame is the same at
922/// every call: debug slot placement does not follow source order, so a
923/// local's address inside the function under test is not that
924/// function's frame boundary. Two earlier probes were wrong that way —
925/// one read `&self.nest_depth`, which is the `Parser`'s address and
926/// never moves at all.
927#[cfg(test)]
928mod frame_meter {
929    extern crate std;
930    use std::cell::Cell;
931
932    // Per-THREAD, not global. `cargo test` runs tests in parallel and
933    // plenty of them parse nested expressions, so shared statics get
934    // stack addresses from several threads at once and the subtraction
935    // below turns into noise — it read 229,772 bytes per level that way,
936    // while passing when the test was run on its own.
937    std::thread_local! {
938        static AT_LO: Cell<usize> = const { Cell::new(0) };
939        static AT_HI: Cell<usize> = const { Cell::new(0) };
940    }
941
942    pub(super) const SAMPLE_LO: usize = 4;
943    pub(super) const SAMPLE_HI: usize = 24;
944
945    #[inline(never)]
946    pub(super) fn record(depth: usize) {
947        let anchor = 0u8;
948        let at = core::ptr::from_ref(&anchor) as usize;
949        if depth == SAMPLE_LO {
950            AT_LO.with(|c| c.set(at));
951        } else if depth == SAMPLE_HI {
952            AT_HI.with(|c| c.set(at));
953        }
954    }
955
956    /// Bytes of stack one nesting level costs, averaged over the span.
957    pub(super) fn bytes_per_level() -> usize {
958        let lo = AT_LO.with(Cell::get);
959        let hi = AT_HI.with(Cell::get);
960        assert!(lo > 0 && hi > 0, "meter never sampled: lo={lo} hi={hi}");
961        assert!(lo > hi, "stack grew upwards? lo={lo} hi={hi}");
962        (lo - hi) / (SAMPLE_HI - SAMPLE_LO)
963    }
964
965    pub(super) fn reset() {
966        AT_LO.with(|c| c.set(0));
967        AT_HI.with(|c| c.set(0));
968    }
969}
970
971/// v7.39 (read01 geo_ops.c) — prefix `@@` desugar target, out-of-line so
972/// the constructor's temporaries stay off `parse_unary`'s recursion frame.
973#[inline(never)]
974fn build_center_call(e: Expr) -> Expr {
975    Expr::FunctionCall {
976        name: alloc::string::String::from("center"),
977        args: alloc::vec![e],
978    }
979}
980
981/// Max consecutive binary operators at ONE precedence level
982/// (`a OR b OR c …`, `1+1+1…`). The chain builds iteratively at
983/// parse time but evaluates and drops recursively — depth beyond
984/// this overflows 2 MiB worker stacks (debug eval frames run
985/// multiple KiB). `IN (…)` lists are flat and unaffected.
986const MAX_BINARY_CHAIN: usize = 256;
987
988/// v7.22 (round-13 gap 5) — the kind keyword after `CONSTRAINT
989/// <name>` in a CREATE TABLE column list. FOREIGN KEY is not here:
990/// it keeps its dedicated path (`parse_table_level_fk`).
991enum NamedTableConstraintKind {
992    Check,
993    Unique,
994    PrimaryKey,
995    Exclude,
996}
997
998impl Parser {
999    fn new(tokens: Vec<Token>) -> Self {
1000        Self::new_with_dialect(tokens, false)
1001    }
1002
1003    fn new_with_dialect(tokens: Vec<Token>, mysql_dialect: bool) -> Self {
1004        Self {
1005            tokens,
1006            mysql_dialect,
1007            in_order_by_key: false,
1008            order_key_collation: None,
1009            pos: 0,
1010            nest_depth: 0,
1011            pending_sample_preds: Vec::new(),
1012            pending_select_into: None,
1013            suppress_in_tail: false,
1014            last_consumed: 0,
1015            src: None,
1016            merges: Vec::new(),
1017        }
1018    }
1019
1020    /// Hand the parser the text it is parsing, for [`Parser::source_span`].
1021    fn with_source(mut self, input: &str, offsets: &[usize]) -> Self {
1022        if self.mysql_dialect {
1023            self.src = Some((input.to_string(), offsets.to_vec()));
1024        }
1025        self
1026    }
1027
1028    /// v7.39.3 — the implicit-concatenation log from the lexer, so a
1029    /// merged literal can still be LABELLED by its first segment the way
1030    /// MySQL 9.7.2 labels it.
1031    fn with_merges(mut self, merges: Vec<(usize, usize)>) -> Self {
1032        if self.mysql_dialect {
1033            self.merges = merges;
1034        }
1035        self
1036    }
1037
1038    /// The byte length of the first segment of the literal at `tok`, when
1039    /// that literal was built by implicit concatenation.
1040    fn merged_first_len(&self, tok: usize) -> Option<usize> {
1041        self.merges
1042            .iter()
1043            .find(|(k, _)| *k == tok)
1044            .map(|(_, len)| *len)
1045    }
1046
1047    /// The source text spanning tokens `start ..= end`, trimmed.
1048    ///
1049    /// The offsets are token STARTS, so the span runs to the start of the
1050    /// token after `end` and gives back the whitespace between them —
1051    /// trimming is what makes `a + b FROM t` end at `b`.
1052    fn source_span(&self, start: usize, end: usize) -> Option<&str> {
1053        let (text, offsets) = self.src.as_ref()?;
1054        let from = *offsets.get(start)?;
1055        let to = *offsets.get(end + 1)?;
1056        text.get(from..to).map(str::trim_end)
1057    }
1058
1059    /// v7.30.2 (mailrs round-25 ask 2) — bump the expr/select
1060    /// nesting depth, erroring out cleanly past the budget.
1061    fn enter_nested(&mut self) -> Result<(), ParseError> {
1062        self.nest_depth += 1;
1063        #[cfg(test)]
1064        frame_meter::record(self.nest_depth);
1065        if self.nest_depth > MAX_NEST_DEPTH {
1066            self.nest_depth -= 1;
1067            return Err(self.err(alloc::format!(
1068                "statement nests deeper than {MAX_NEST_DEPTH} levels"
1069            )));
1070        }
1071        Ok(())
1072    }
1073
1074    fn peek(&self) -> &Token {
1075        // tokens always ends with Eof; pos is clamped in advance().
1076        &self.tokens[self.pos]
1077    }
1078
1079    fn advance(&mut self) -> Token {
1080        let t = mem::replace(&mut self.tokens[self.pos], Token::Eof);
1081        self.last_consumed = self.pos;
1082        if self.pos + 1 < self.tokens.len() {
1083            self.pos += 1;
1084        }
1085        t
1086    }
1087
1088    /// v7.39 (round 340, V56) — the index of the token `advance()` just
1089    /// returned. It was computed as `pos - 1`, which is wrong at both
1090    /// ends: `advance()` parks on the final Eof rather than running off
1091    /// the end (so `SELECT * FROM` named `FROM` where PG says `at end of
1092    /// input`), and after backtracking `pos` is no longer one past the
1093    /// token that failed. Recorded by `advance()` itself instead.
1094    const fn consumed_pos(&self) -> usize {
1095        self.last_consumed
1096    }
1097
1098    fn err(&self, message: String) -> ParseError {
1099        ParseError {
1100            message,
1101            token_pos: self.pos,
1102        }
1103    }
1104
1105    /// v7.39.3 — like [`Parser::err`] but pointing at a token the caller
1106    /// names rather than at the current one.
1107    ///
1108    /// The position is not decoration on the MySQL wire: its syntax-error
1109    /// sentence quotes the source from there to the end of the statement,
1110    /// so an error raised after the construct it is about quotes nothing.
1111    fn err_at(&self, token_pos: usize, message: String) -> ParseError {
1112        ParseError { message, token_pos }
1113    }
1114
1115    fn expect_eof(&self) -> Result<(), ParseError> {
1116        if matches!(self.peek(), Token::Eof) {
1117            Ok(())
1118        } else {
1119            Err(self.err(format!("expected end of input, got {:?}", self.peek())))
1120        }
1121    }
1122
1123    /// v7.14.0 — swallow every token up to (but not including) the
1124    /// next semicolon / EOF. Used by the dump-noise dispatcher
1125    /// to consume `COMMENT ON …`, `GRANT …`, `LOCK TABLES …`,
1126    /// etc. without modeling each grammar.
1127    /// v7.39 (read01 round 50) — `COMMENT ON <kind> <name> IS { 'text' | NULL }`.
1128    /// Kinds SPG stores by name: TABLE / COLUMN / INDEX / VIEW / SEQUENCE /
1129    /// SCHEMA / TYPE / DATABASE / FUNCTION. Anything else (and the multi-word
1130    /// `CONSTRAINT c ON t` / `MATERIALIZED VIEW` forms) keeps the old
1131    /// swallow-as-no-op behaviour so a pg_dump tail still loads.
1132    fn parse_comment_on(&mut self) -> Result<Statement, ParseError> {
1133        let start = self.pos;
1134        self.advance(); // COMMENT
1135        if !matches!(self.peek(), Token::On) {
1136            self.pos = start;
1137            self.consume_until_statement_boundary();
1138            return Ok(Statement::Empty);
1139        }
1140        self.advance(); // ON
1141        let kind = match self.peek() {
1142            Token::Ident(s) | Token::QuotedIdent(s) => s.to_ascii_lowercase(),
1143            Token::Table => "table".into(),
1144            _ => {
1145                self.consume_until_statement_boundary();
1146                return Ok(Statement::Empty);
1147            }
1148        };
1149        if !matches!(
1150            kind.as_str(),
1151            "table"
1152                | "column"
1153                | "index"
1154                | "view"
1155                | "sequence"
1156                | "schema"
1157                | "type"
1158                | "database"
1159                | "function"
1160        ) {
1161            self.consume_until_statement_boundary();
1162            return Ok(Statement::Empty);
1163        }
1164        self.advance(); // the kind keyword
1165        // The object name. ⚠️ `expect_ident_like` strips a leading
1166        // `<schema>.` qualifier and returns only the trailing ident (SPG is
1167        // single-schema), which would turn `COMMENT ON COLUMN t.c` into just
1168        // `c`. Read the dotted parts from raw tokens instead, then let a
1169        // 3-part `schema.t.c` drop its leading schema like everywhere else.
1170        let mut parts: alloc::vec::Vec<String> = alloc::vec::Vec::new();
1171        loop {
1172            match self.advance() {
1173                Token::Ident(s) | Token::QuotedIdent(s) => parts.push(s),
1174                other if unreserved_keyword_text(&other).is_some() => {
1175                    parts.push(unreserved_keyword_text(&other).unwrap());
1176                }
1177                other => {
1178                    return Err(ParseError {
1179                        message: alloc::format!("expected identifier, got {other:?}"),
1180                        token_pos: self.consumed_pos(),
1181                    });
1182                }
1183            }
1184            if matches!(self.peek(), Token::Dot) {
1185                self.advance();
1186            } else {
1187                break;
1188            }
1189        }
1190        // COLUMN wants `table.column`; every other kind wants a bare name.
1191        let want = if kind == "column" { 2 } else { 1 };
1192        while parts.len() > want {
1193            parts.remove(0);
1194        }
1195        let name = parts.join(".");
1196        // v7.39 (round 710) — `COMMENT ON FUNCTION f(int, text) IS …`.
1197        // pg_dump writes the SIGNATURE, and the paren list was a syntax
1198        // error here — a dump carrying one function comment failed to
1199        // restore. The list is consumed (the comment store keys by name;
1200        // overload-precise comments are the function-predicate follow-up).
1201        if matches!(self.peek(), Token::LParen)
1202            && matches!(
1203                kind.as_str(),
1204                "function" | "procedure" | "aggregate" | "routine"
1205            )
1206        {
1207            let mut depth = 0usize;
1208            loop {
1209                match self.advance() {
1210                    Token::LParen => depth += 1,
1211                    Token::RParen => {
1212                        depth -= 1;
1213                        if depth == 0 {
1214                            break;
1215                        }
1216                    }
1217                    Token::Eof => {
1218                        return Err(self.err(alloc::string::String::from(
1219                            "unterminated argument list in COMMENT ON",
1220                        )));
1221                    }
1222                    _ => {}
1223                }
1224            }
1225        }
1226        // `IS`
1227        if !matches!(self.peek(), Token::Is) {
1228            self.expect_keyword_ident("is")?;
1229        } else {
1230            self.advance();
1231        }
1232        let comment = match self.peek() {
1233            Token::Null => {
1234                self.advance();
1235                None
1236            }
1237            _ => Some(self.expect_string_literal()?),
1238        };
1239        Ok(Statement::CommentOn {
1240            kind,
1241            name,
1242            comment,
1243        })
1244    }
1245
1246    /// v7.39 (read01 round 57) — `GRANT <privs> ON <obj> TO <roles> [WITH GRANT
1247    /// OPTION]` / `REVOKE [GRANT OPTION FOR] <privs> ON <obj> FROM <roles>
1248    /// [CASCADE|RESTRICT]`.
1249    ///
1250    /// TABLE privileges are the real ones (stored, enforced, introspectable).
1251    /// Every other object class — SCHEMA / SEQUENCE / DATABASE / FUNCTION / …,
1252    /// and the no-ON `GRANT role TO role` membership form — parses into
1253    /// `GrantObject::Other` and no-ops in the engine, so a pg_dump that grants
1254    /// on them still restores.
1255    fn parse_grant_or_revoke(&mut self, grant: bool) -> Result<Statement, ParseError> {
1256        self.advance(); // GRANT / REVOKE
1257        // REVOKE's optional `GRANT OPTION FOR` prefix.
1258        let mut grant_option = false;
1259        if !grant
1260            && self.peek_keyword_ident("grant")
1261            && matches!(self.tokens.get(self.pos + 1), Some(Token::Ident(s)) if s.eq_ignore_ascii_case("option"))
1262        {
1263            self.advance(); // GRANT
1264            self.advance(); // OPTION
1265            self.expect_keyword_ident("for")?;
1266            grant_option = true;
1267        }
1268        // The privilege list: `ALL [PRIVILEGES] [(cols)]`, or comma-separated
1269        // words each with an optional COLUMN list.
1270        let mut privileges: Vec<GrantPriv> = Vec::new();
1271        if matches!(self.peek(), Token::All) {
1272            self.advance();
1273            if self.peek_keyword_ident("privileges") {
1274                self.advance();
1275            }
1276            // `GRANT ALL (col) ON t TO r` — every column privilege, on that
1277            // column only.
1278            if matches!(self.peek(), Token::LParen) {
1279                let columns = self.parse_grant_column_list()?;
1280                privileges.push(GrantPriv {
1281                    word: "ALL".into(),
1282                    columns,
1283                });
1284            }
1285        } else {
1286            loop {
1287                // SELECT and INSERT lex as reserved tokens, so they never
1288                // reach `expect_ident_like` as plain idents; the rest
1289                // (UPDATE / DELETE / TRUNCATE / REFERENCES / TRIGGER /
1290                // MAINTAIN) are ordinary identifiers.
1291                let w = match self.peek() {
1292                    Token::Select => {
1293                        self.advance();
1294                        "SELECT".to_string()
1295                    }
1296                    Token::Insert => {
1297                        self.advance();
1298                        "INSERT".to_string()
1299                    }
1300                    // v7.39 (read01 round 60) — CREATE is a privilege word on a
1301                    // schema / database, and it lexes as a reserved token.
1302                    Token::Create => {
1303                        self.advance();
1304                        "CREATE".to_string()
1305                    }
1306                    // NOT upper-cased: in the no-ON shape (`GRANT devs TO
1307                    // alice`) these "privilege words" are ROLE NAMES, and a
1308                    // role name is case-sensitive. `priv_from_word` folds case
1309                    // itself when they really are privileges.
1310                    _ => self.expect_ident_like()?,
1311                };
1312                // v7.39 (read01 round 59) — the optional per-privilege COLUMN
1313                // list: `GRANT SELECT (a, b), INSERT (c) ON t TO dan`.
1314                let columns = if matches!(self.peek(), Token::LParen) {
1315                    self.parse_grant_column_list()?
1316                } else {
1317                    Vec::new()
1318                };
1319                privileges.push(GrantPriv { word: w, columns });
1320                if matches!(self.peek(), Token::Comma) {
1321                    self.advance();
1322                } else {
1323                    break;
1324                }
1325            }
1326        }
1327        // v7.39 (read01 round 58) — no ON clause at all = `GRANT devs TO alice`:
1328        // role MEMBERSHIP. The words parsed as "privileges" are the role names.
1329        if !matches!(self.peek(), Token::On) {
1330            let roles: Vec<String> = core::mem::take(&mut privileges)
1331                .into_iter()
1332                .map(|p| p.word)
1333                .collect();
1334            let grantees = self.parse_grantee_list(grant)?;
1335            // `WITH ADMIN OPTION` / `GRANTED BY x` — accepted, ignored (SPG has
1336            // no admin-option layer: a member cannot re-grant).
1337            self.consume_until_statement_boundary();
1338            return Ok(finish_grant(
1339                grant,
1340                GrantStatement {
1341                    privileges: Vec::new(),
1342                    object: GrantObject::Roles(roles),
1343                    grantees,
1344                    grant_option,
1345                },
1346            ));
1347        }
1348        self.advance(); // ON
1349        // An optional object-class keyword. `TABLE` (or no keyword at all) is
1350        // the enforced case; anything else parses and no-ops.
1351        let mut class = "TABLE";
1352        match self.peek() {
1353            Token::Table => {
1354                self.advance();
1355            }
1356            Token::All => {
1357                // v7.39 (read01 round 61) — `ALL TABLES IN SCHEMA x` expands to
1358                // every table at GRANT time, like PG. `ALL SEQUENCES/FUNCTIONS
1359                // IN SCHEMA` stay no-ops and keep their own object class.
1360                self.advance(); // ALL
1361                let kind = match self.peek() {
1362                    Token::Ident(w) | Token::QuotedIdent(w) => w.to_ascii_lowercase(),
1363                    // TABLES has its own token (SHOW TABLES owns it).
1364                    Token::Tables | Token::Table => "tables".to_string(),
1365                    _ => String::new(),
1366                };
1367                if !kind.is_empty() {
1368                    self.advance();
1369                }
1370                // `IN SCHEMA <name>`
1371                if matches!(self.peek(), Token::In) {
1372                    self.advance();
1373                    if self.peek_keyword_ident("schema") {
1374                        self.advance();
1375                        let _schema = self.expect_ident_like()?;
1376                    }
1377                }
1378                if kind != "tables" {
1379                    self.consume_until_statement_boundary();
1380                    return Ok(finish_grant(
1381                        grant,
1382                        GrantStatement {
1383                            privileges,
1384                            object: GrantObject::Other("ALL … IN SCHEMA".into()),
1385                            grantees: Vec::new(),
1386                            grant_option,
1387                        },
1388                    ));
1389                }
1390                let grantees = self.parse_grantee_list(grant)?;
1391                self.consume_until_statement_boundary();
1392                return Ok(finish_grant(
1393                    grant,
1394                    GrantStatement {
1395                        privileges,
1396                        object: GrantObject::AllTablesInSchema,
1397                        grantees,
1398                        grant_option,
1399                    },
1400                ));
1401            }
1402            Token::Ident(w) | Token::QuotedIdent(w) => {
1403                let lc = w.to_ascii_lowercase();
1404                // v7.39 (read01 round 60) — SEQUENCE / SCHEMA / DATABASE are
1405                // real objects with real ACLs now.
1406                if matches!(lc.as_str(), "sequence" | "schema" | "database") {
1407                    self.advance();
1408                    let mut names: Vec<String> = Vec::new();
1409                    loop {
1410                        let mut parts: Vec<String> = Vec::new();
1411                        loop {
1412                            parts.push(self.expect_ident_like()?);
1413                            if matches!(self.peek(), Token::Dot) {
1414                                self.advance();
1415                            } else {
1416                                break;
1417                            }
1418                        }
1419                        names.push(parts.pop().expect("at least one part"));
1420                        if matches!(self.peek(), Token::Comma) {
1421                            self.advance();
1422                        } else {
1423                            break;
1424                        }
1425                    }
1426                    let grantees = self.parse_grantee_list(grant)?;
1427                    let mut grant_option = grant_option;
1428                    if grant && self.peek_keyword_ident("with") {
1429                        self.advance();
1430                        self.expect_keyword_ident("grant")?;
1431                        self.expect_keyword_ident("option")?;
1432                        grant_option = true;
1433                    }
1434                    self.consume_until_statement_boundary();
1435                    let object = match lc.as_str() {
1436                        "sequence" => GrantObject::Sequences(names),
1437                        "schema" => GrantObject::Schemas(names),
1438                        _ => GrantObject::Databases(names),
1439                    };
1440                    return Ok(finish_grant(
1441                        grant,
1442                        GrantStatement {
1443                            privileges,
1444                            object,
1445                            grantees,
1446                            grant_option,
1447                        },
1448                    ));
1449                }
1450                // v7.39 (read01 round 61) — `ON FUNCTION f(int)` is real. SPG
1451                // keys functions by NAME, so the argument list parses and is
1452                // dropped (an overload set shares one ACL — recorded residual).
1453                if matches!(lc.as_str(), "function" | "procedure" | "routine") {
1454                    self.advance();
1455                    let mut names: Vec<(String, Option<Vec<String>>)> = Vec::new();
1456                    loop {
1457                        let mut parts: Vec<String> = Vec::new();
1458                        loop {
1459                            parts.push(self.expect_ident_like()?);
1460                            if matches!(self.peek(), Token::Dot) {
1461                                self.advance();
1462                            } else {
1463                                break;
1464                            }
1465                        }
1466                        let fname = parts.pop().expect("at least one part");
1467                        // v7.39 (read01 round 62) — the signature picks the
1468                        // overload, so it is captured.
1469                        let sig = if matches!(self.peek(), Token::LParen) {
1470                            Some(self.parse_function_signature_types()?)
1471                        } else {
1472                            None
1473                        };
1474                        names.push((fname, sig));
1475                        if matches!(self.peek(), Token::Comma) {
1476                            self.advance();
1477                        } else {
1478                            break;
1479                        }
1480                    }
1481                    let grantees = self.parse_grantee_list(grant)?;
1482                    self.consume_until_statement_boundary();
1483                    return Ok(finish_grant(
1484                        grant,
1485                        GrantStatement {
1486                            privileges,
1487                            object: GrantObject::Functions(names),
1488                            grantees,
1489                            grant_option,
1490                        },
1491                    ));
1492                }
1493                if matches!(
1494                    lc.as_str(),
1495                    "type"
1496                        | "domain"
1497                        | "language"
1498                        | "tablespace"
1499                        | "large"
1500                        | "foreign"
1501                        | "parameter"
1502                ) {
1503                    self.consume_until_statement_boundary();
1504                    return Ok(finish_grant(
1505                        grant,
1506                        GrantStatement {
1507                            privileges,
1508                            object: GrantObject::Other(lc.to_ascii_uppercase()),
1509                            grantees: Vec::new(),
1510                            grant_option,
1511                        },
1512                    ));
1513                }
1514                class = "TABLE";
1515            }
1516            _ => {}
1517        }
1518        let _ = class;
1519        // The table list. Schema-qualified names drop their qualifier (SPG is
1520        // single-schema) — but read the dotted parts from raw tokens, since
1521        // `expect_ident_like` would silently swallow the leading part.
1522        let mut tables: Vec<String> = Vec::new();
1523        loop {
1524            let mut parts: Vec<String> = Vec::new();
1525            loop {
1526                parts.push(self.expect_ident_like()?);
1527                if matches!(self.peek(), Token::Dot) {
1528                    self.advance();
1529                } else {
1530                    break;
1531                }
1532            }
1533            tables.push(parts.pop().expect("at least one part"));
1534            if matches!(self.peek(), Token::Comma) {
1535                self.advance();
1536            } else {
1537                break;
1538            }
1539        }
1540        let grantees = self.parse_grantee_list(grant)?;
1541        if grant && self.peek_keyword_ident("with") {
1542            self.advance();
1543            self.expect_keyword_ident("grant")?;
1544            self.expect_keyword_ident("option")?;
1545            grant_option = true;
1546        }
1547        // REVOKE's trailing CASCADE / RESTRICT — SPG has no dependent grants
1548        // to cascade to (no re-granting), so both are accepted and ignored.
1549        if !grant && (self.peek_keyword_ident("cascade") || self.peek_keyword_ident("restrict")) {
1550            self.advance();
1551        }
1552        Ok(finish_grant(
1553            grant,
1554            GrantStatement {
1555                privileges,
1556                object: GrantObject::Tables(tables),
1557                grantees,
1558                grant_option,
1559            },
1560        ))
1561    }
1562
1563    /// v7.39 (read01 round 62) — the argument TYPES in a function signature:
1564    /// `(int, text)` or `(x int, y text)` (PG accepts either). Returns the type
1565    /// words; the caller normalises them into a signature key.
1566    fn parse_function_signature_types(&mut self) -> Result<Vec<String>, ParseError> {
1567        self.advance(); // (
1568        let mut types: Vec<String> = Vec::new();
1569        if matches!(self.peek(), Token::RParen) {
1570            self.advance();
1571            return Ok(types);
1572        }
1573        loop {
1574            // Collect the words of one argument up to a comma / close paren.
1575            let mut words: Vec<String> = Vec::new();
1576            loop {
1577                match self.peek() {
1578                    Token::Comma | Token::RParen | Token::Eof => break,
1579                    _ => {}
1580                }
1581                let tok = self.advance();
1582                match tok {
1583                    Token::Ident(w) | Token::QuotedIdent(w) => words.push(w),
1584                    other => {
1585                        if let Some(w) = unreserved_keyword_text(&other) {
1586                            words.push(w);
1587                        }
1588                    }
1589                }
1590            }
1591            // `name TYPE` or a bare `TYPE`. Several of PG's type names are
1592            // themselves several words (`double precision`, `character
1593            // varying`, `timestamp with time zone`), so "two words means the
1594            // first is a parameter name" reads the type off `f(double
1595            // precision)` as `precision`. v7.39 (round 282): recognise the
1596            // multi-word spellings first — a leading word that STARTS one of
1597            // them is part of the type, not a name.
1598            let joined = words.join(" ");
1599            let ty = if words.len() >= 2 && is_multiword_type_phrase(&joined) {
1600                joined
1601            } else if words.len() >= 2 {
1602                words[1..].join(" ")
1603            } else {
1604                words.first().cloned().unwrap_or_default()
1605            };
1606            types.push(ty);
1607            if matches!(self.peek(), Token::Comma) {
1608                self.advance();
1609            } else {
1610                break;
1611            }
1612        }
1613        if matches!(self.peek(), Token::RParen) {
1614            self.advance();
1615        }
1616        Ok(types)
1617    }
1618
1619    /// v7.39 (read01 round 59) — `( col, col, … )` after a privilege word.
1620    fn parse_grant_column_list(&mut self) -> Result<Vec<String>, ParseError> {
1621        self.advance(); // (
1622        let mut cols = Vec::new();
1623        loop {
1624            cols.push(self.expect_ident_like()?);
1625            if matches!(self.peek(), Token::Comma) {
1626                self.advance();
1627            } else {
1628                break;
1629            }
1630        }
1631        if !matches!(self.peek(), Token::RParen) {
1632            return Err(self.err(alloc::format!(
1633                "expected ')' to close the column list, got {:?}",
1634                self.peek()
1635            )));
1636        }
1637        self.advance(); // )
1638        Ok(cols)
1639    }
1640
1641    /// `TO <roles>` (grant) / `FROM <roles>` (revoke). An empty-string entry is
1642    /// PUBLIC.
1643    fn parse_grantee_list(&mut self, grant: bool) -> Result<Vec<String>, ParseError> {
1644        if grant {
1645            if matches!(self.peek(), Token::To) {
1646                self.advance();
1647            } else {
1648                self.expect_keyword_ident("to")?;
1649            }
1650        } else if matches!(self.peek(), Token::From) {
1651            self.advance();
1652        } else {
1653            self.expect_keyword_ident("from")?;
1654        }
1655        let mut grantees: Vec<String> = Vec::new();
1656        loop {
1657            // `GROUP name` is the legacy spelling of a plain role name.
1658            if self.peek_keyword_ident("group") {
1659                self.advance();
1660            }
1661            if self.peek_keyword_ident("public") {
1662                self.advance();
1663                grantees.push(String::new()); // PUBLIC
1664            } else {
1665                grantees.push(self.expect_ident_like()?);
1666            }
1667            if matches!(self.peek(), Token::Comma) {
1668                self.advance();
1669            } else {
1670                break;
1671            }
1672        }
1673        Ok(grantees)
1674    }
1675
1676    /// v7.39 (round 277) — `PREPARE <name> [(type, …)] AS <statement>`.
1677    /// The body keeps its `$N` placeholders; substitution happens at
1678    /// EXECUTE. The declared types are recorded for
1679    /// `pg_prepared_statements.parameter_types` but are not enforced —
1680    /// PG infers when the list is omitted, and SPG resolves the values
1681    /// at substitution time either way.
1682    fn parse_prepare(&mut self) -> Result<Statement, ParseError> {
1683        let start = self.pos;
1684        self.advance(); // PREPARE
1685        // v7.39 (round 278) — `PREPARE TRANSACTION '<gid>'` is 2PC, a
1686        // different statement that happens to share the keyword. PG
1687        // ships with `max_prepared_transactions = 0` and reports it
1688        // this way; SPG has no prepared-transaction registry, so the
1689        // same wording is the accurate answer rather than a dodge.
1690        // Round 277 turned this from a silent no-op into a confusing
1691        // "expected AS in PREPARE" parse error.
1692        if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("transaction")) {
1693            self.advance();
1694            let gid = match self.advance() {
1695                Token::String(g) => g,
1696                other => {
1697                    return Err(self.err(alloc::format!(
1698                        "expected a transaction identifier after PREPARE TRANSACTION, got {other:?}"
1699                    )));
1700                }
1701            };
1702            return Ok(Statement::PrepareTransaction(gid));
1703        }
1704        let name = self.expect_ident_like()?;
1705        let mut param_types = Vec::new();
1706        if matches!(self.peek(), Token::LParen) {
1707            self.advance();
1708            loop {
1709                let mut ty = self.expect_ident_like()?;
1710                // A parameterised type name (`numeric(10,2)`,
1711                // `varchar(20)`) keeps its argument list in the text.
1712                if matches!(self.peek(), Token::LParen) {
1713                    let mut depth = 0usize;
1714                    let mut buf = String::from("(");
1715                    loop {
1716                        match self.advance() {
1717                            Token::LParen => {
1718                                depth += 1;
1719                                if depth > 1 {
1720                                    buf.push('(');
1721                                }
1722                            }
1723                            Token::RParen => {
1724                                depth -= 1;
1725                                buf.push(')');
1726                                if depth == 0 {
1727                                    break;
1728                                }
1729                            }
1730                            Token::Comma => buf.push(','),
1731                            Token::Integer(n) => buf.push_str(&alloc::format!("{n}")),
1732                            Token::Eof => break,
1733                            _ => {}
1734                        }
1735                    }
1736                    ty.push_str(&buf);
1737                }
1738                // r1049 — `PREPARE p(bigint[]) AS …`: the sixth `[]`
1739                // position, same family as the parameter list above.
1740                let array_suffix = self.consume_array_suffix();
1741                ty.push_str(&array_suffix);
1742                param_types.push(ty);
1743                match self.peek() {
1744                    Token::Comma => {
1745                        self.advance();
1746                    }
1747                    Token::RParen => {
1748                        self.advance();
1749                        break;
1750                    }
1751                    other => {
1752                        return Err(self.err(alloc::format!(
1753                            "expected ',' or ')' in PREPARE parameter list, got {other:?}"
1754                        )));
1755                    }
1756                }
1757            }
1758        }
1759        if !matches!(self.peek(), Token::As) {
1760            return Err(self.err(alloc::format!(
1761                "expected AS in PREPARE, got {:?}",
1762                self.peek()
1763            )));
1764        }
1765        self.advance();
1766        let body = self.parse_one_statement()?;
1767        // The Parser holds tokens, not the source text, so the
1768        // statement PG reports in `pg_prepared_statements.statement`
1769        // is rebuilt from the AST rather than sliced from the input.
1770        let _ = start;
1771        let mut source = alloc::format!("PREPARE {}", crate::ast::quote_ident(&name));
1772        if !param_types.is_empty() {
1773            source.push_str(" (");
1774            source.push_str(&param_types.join(", "));
1775            source.push(')');
1776        }
1777        source.push_str(" AS ");
1778        source.push_str(&alloc::format!("{body}"));
1779        Ok(Statement::Prepare {
1780            name,
1781            param_types,
1782            body: alloc::boxed::Box::new(body),
1783            source,
1784        })
1785    }
1786
1787    /// v7.39 (round 277) — `EXECUTE <name> [(<expr>, …)]`.
1788    fn parse_execute(&mut self) -> Result<Statement, ParseError> {
1789        self.advance(); // EXECUTE
1790        let name = self.expect_ident_like()?;
1791        let mut args = Vec::new();
1792        if matches!(self.peek(), Token::LParen) {
1793            self.advance();
1794            if matches!(self.peek(), Token::RParen) {
1795                self.advance();
1796            } else {
1797                loop {
1798                    args.push(self.parse_expr(0)?);
1799                    match self.advance() {
1800                        Token::Comma => {}
1801                        Token::RParen => break,
1802                        other => {
1803                            return Err(self.err(alloc::format!(
1804                                "expected ',' or ')' in EXECUTE arguments, got {other:?}"
1805                            )));
1806                        }
1807                    }
1808                }
1809            }
1810        }
1811        Ok(Statement::Execute { name, args })
1812    }
1813
1814    /// v7.39 (round 277) — `DEALLOCATE {[PREPARE] <name> | ALL}`.
1815    /// v7.39 (round 278) — `CALL <proc>([args])`. There is no
1816    /// procedure catalog yet, so this reports PG's not-found error
1817    /// (with its HINT) rather than pretending the call ran.
1818    /// v7.39 (round 320, V53) — `DISCARD { ALL | PLANS | SEQUENCES | TEMP }`.
1819    /// Bare `DISCARD` is a syntax error in PG; so it is here.
1820    fn parse_discard(&mut self) -> Result<Statement, ParseError> {
1821        self.advance(); // DISCARD
1822        let target = match self.advance() {
1823            Token::All => DiscardTarget::All,
1824            Token::Ident(w) | Token::QuotedIdent(w) => match w.to_ascii_lowercase().as_str() {
1825                "all" => DiscardTarget::All,
1826                "plans" => DiscardTarget::Plans,
1827                "sequences" => DiscardTarget::Sequences,
1828                "temp" | "temporary" => DiscardTarget::Temp,
1829                other => {
1830                    return Err(self.err(format!(
1831                        "expected ALL / PLANS / SEQUENCES / TEMP after DISCARD, got {other:?}"
1832                    )));
1833                }
1834            },
1835            other => {
1836                return Err(self.err(format!(
1837                    "expected ALL / PLANS / SEQUENCES / TEMP after DISCARD, got {other:?}"
1838                )));
1839            }
1840        };
1841        Ok(Statement::Discard(target))
1842    }
1843
1844    /// v7.39 (round 318, V51) — MySQL `KILL [HARD|SOFT] [CONNECTION|QUERY]
1845    /// <expr>`. MariaDB accepts an expression for the id (its own docs use
1846    /// `KILL connection_id()`), and the HARD / SOFT prefixes only pick how
1847    /// aggressively the server interrupts, which SPG does not distinguish.
1848    /// Bare `KILL <id>` means CONNECTION.
1849    fn parse_kill(&mut self) -> Result<Statement, ParseError> {
1850        self.advance(); // KILL
1851        let mut query_only = false;
1852        loop {
1853            // CONNECTION is a reserved keyword token (it also opens
1854            // `CREATE SUBSCRIPTION … CONNECTION '…'`), so it arrives as
1855            // `Token::Connection` rather than a bare ident.
1856            if matches!(self.peek(), Token::Connection) {
1857                self.advance();
1858                break;
1859            }
1860            let (Token::Ident(w) | Token::QuotedIdent(w)) = self.peek() else {
1861                break;
1862            };
1863            match w.to_ascii_lowercase().as_str() {
1864                "hard" | "soft" => {
1865                    self.advance();
1866                }
1867                "query" => {
1868                    self.advance();
1869                    query_only = true;
1870                    break;
1871                }
1872                _ => break,
1873            }
1874        }
1875        let id = self.parse_expr(0)?;
1876        Ok(Statement::Kill {
1877            query_only,
1878            id: Box::new(id),
1879        })
1880    }
1881
1882    fn parse_call(&mut self) -> Result<Statement, ParseError> {
1883        self.advance(); // CALL
1884        let name = self.expect_ident_like()?;
1885        self.consume_until_statement_boundary();
1886        Ok(Statement::Call(name))
1887    }
1888
1889    fn parse_deallocate(&mut self) -> Result<Statement, ParseError> {
1890        self.advance(); // DEALLOCATE
1891        // PG accepts an optional noise `PREPARE` keyword here.
1892        if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("prepare")) {
1893            self.advance();
1894        }
1895        if matches!(self.peek(), Token::All) {
1896            self.advance();
1897            return Ok(Statement::Deallocate(None));
1898        }
1899        if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("all")) {
1900            self.advance();
1901            return Ok(Statement::Deallocate(None));
1902        }
1903        let name = self.expect_ident_like()?;
1904        Ok(Statement::Deallocate(Some(name)))
1905    }
1906
1907    fn consume_until_statement_boundary(&mut self) {
1908        loop {
1909            match self.peek() {
1910                Token::Semicolon | Token::Eof => return,
1911                _ => self.advance(),
1912            };
1913        }
1914    }
1915
1916    /// v7.38.19 — the database name a `CREATE DATABASE` names, skipping
1917    /// an `IF NOT EXISTS`. Consumes only the name; the collation scanner
1918    /// runs after it and eats the rest.
1919    fn scan_database_name(&mut self) -> Option<String> {
1920        // The caller has only PEEKED at `DATABASE`; step past it, or the
1921        // first identifier found is the keyword itself. It was, and
1922        // `pg_database` listed a database called `database`.
1923        if matches!(self.peek(), Token::Ident(w) | Token::QuotedIdent(w) if w.eq_ignore_ascii_case("database"))
1924        {
1925            self.advance();
1926        }
1927        for kw in ["if", "not", "exists"] {
1928            if matches!(self.peek(), Token::Ident(w) | Token::QuotedIdent(w) if w.eq_ignore_ascii_case(kw))
1929            {
1930                self.advance();
1931            }
1932        }
1933        match self.peek().clone() {
1934            Token::Ident(w) | Token::QuotedIdent(w) => {
1935                self.advance();
1936                Some(w)
1937            }
1938            _ => None,
1939        }
1940    }
1941
1942    /// v7.38.18 — consume to the statement boundary like
1943    /// `consume_until_statement_boundary`, but pick out the collation a
1944    /// `CREATE DATABASE` asked for on the way.
1945    ///
1946    /// `LC_COLLATE 'de_DE.utf8'` and `LOCALE 'de_DE.utf8'` both count;
1947    /// `LC_CTYPE` does not, because SPG has no separate ctype and
1948    /// pretending to honour it would be the more misleading answer. An
1949    /// `=` between the keyword and the value is optional, as in PG.
1950    ///
1951    /// The whole statement used to be thrown away. Being single-database
1952    /// makes the NAME a no-op; it does not make the collation one.
1953    fn scan_database_collation_until_boundary(&mut self) -> Option<String> {
1954        let mut want_value = false;
1955        let mut found: Option<String> = None;
1956        loop {
1957            let tok = self.peek().clone();
1958            match &tok {
1959                Token::Semicolon | Token::Eof => break,
1960                Token::Ident(w) | Token::QuotedIdent(w)
1961                    if w.eq_ignore_ascii_case("lc_collate") || w.eq_ignore_ascii_case("locale") =>
1962                {
1963                    want_value = true;
1964                }
1965                Token::Eq if want_value => {}
1966                Token::String(v) if want_value => {
1967                    found = Some(v.clone());
1968                    want_value = false;
1969                }
1970                Token::Ident(v) | Token::QuotedIdent(v) if want_value => {
1971                    found = Some(v.clone());
1972                    want_value = false;
1973                }
1974                _ => want_value = false,
1975            }
1976            self.advance();
1977        }
1978        found
1979    }
1980
1981    /// v7.22 (round-13 T2) — consume to the statement boundary like
1982    /// `consume_until_statement_boundary`, but pick out the sequence
1983    /// name on the way: either `SEQUENCE NAME <ident>` (identity
1984    /// columns) or the first string literal (`nextval('<seq>')`).
1985    /// Schema qualifiers and `::regclass` casts are stripped.
1986    fn scan_sequence_name_until_boundary(&mut self) -> Option<String> {
1987        let mut seq: Option<String> = None;
1988        let mut after_sequence_kw = false;
1989        let mut after_name_kw = false;
1990        loop {
1991            match self.peek().clone() {
1992                Token::Semicolon | Token::Eof => break,
1993                Token::Ident(s) | Token::QuotedIdent(s) => {
1994                    if after_name_kw && seq.is_none() {
1995                        self.advance();
1996                        let mut name = s;
1997                        // `SEQUENCE NAME public.groups_id_seq` — keep
1998                        // the bare name, drop qualifiers.
1999                        while matches!(self.peek(), Token::Dot) {
2000                            self.advance();
2001                            if let Token::Ident(n) | Token::QuotedIdent(n) = self.advance() {
2002                                name = n;
2003                            }
2004                        }
2005                        seq = Some(name);
2006                        after_name_kw = false;
2007                        continue;
2008                    }
2009                    if after_sequence_kw && s.eq_ignore_ascii_case("name") {
2010                        after_name_kw = true;
2011                        after_sequence_kw = false;
2012                    } else {
2013                        after_sequence_kw = s.eq_ignore_ascii_case("sequence");
2014                    }
2015                    self.advance();
2016                }
2017                Token::String(s) => {
2018                    if seq.is_none() {
2019                        // `nextval('public.groups_id_seq'::regclass)`
2020                        let bare = s
2021                            .rsplit_once('.')
2022                            .map_or_else(|| s.clone(), |(_, b)| b.to_string());
2023                        seq = Some(bare);
2024                    }
2025                    self.advance();
2026                }
2027                _ => {
2028                    after_sequence_kw = false;
2029                    after_name_kw = false;
2030                    self.advance();
2031                }
2032            }
2033        }
2034        seq
2035    }
2036
2037    /// v7.39 (round 621) — is the next token the keyword `BY`?
2038    ///
2039    /// `pg_get_keywords()` classes `by` as `U` (unreserved), so it is a legal
2040    /// column, table and alias name — and SPG lexed it into a dedicated
2041    /// `Token::By`, which made it unusable as a name ANYWHERE. Of the seven
2042    /// two-letter keywords the lexer knew, this was the only one PG leaves
2043    /// unreserved (`as`, `in`, `on`, `or`, `to` are reserved and `is` is `T`).
2044    ///
2045    /// The token is gone; the three clauses that own the word — GROUP BY,
2046    /// ORDER BY, PARTITION BY — and the handful of other places that expect it
2047    /// ask this instead. Adding it to the unreserved-identifier table was not
2048    /// enough on its own: identifier positions that match the token shape
2049    /// directly (an index's column list, a table alias) never consult that
2050    /// table, so `CREATE INDEX … ON t(by)` and `FROM t AS by` still failed.
2051    /// Not lexing it as a keyword closes the whole class rather than the two
2052    /// positions that happened to be noticed.
2053    fn peek_is_by(&self) -> bool {
2054        matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("by"))
2055    }
2056
2057    /// v7.39 (round 621) — the optional `CASCADE` / `RESTRICT` trailer a DROP
2058    /// takes. Accepted and dropped: SPG tracks no dependents to cascade to,
2059    /// which is what `DROP TABLE` and `DROP INDEX` have done since v7.14.
2060    fn consume_drop_behaviour(&mut self) {
2061        if matches!(
2062            self.peek(),
2063            Token::Ident(s) if s.eq_ignore_ascii_case("cascade") || s.eq_ignore_ascii_case("restrict")
2064        ) {
2065            self.advance();
2066        }
2067    }
2068
2069    fn expect_ident_like(&mut self) -> Result<String, ParseError> {
2070        let first = match self.advance() {
2071            Token::Ident(s) | Token::QuotedIdent(s) => s,
2072            // v7.37.43-T4 — PG-unreserved keywords are legal identifiers
2073            // per PG's `pg_get_keywords()` classification. SPG tokenizes
2074            // these as named variants for parsing leverage in the
2075            // contexts that own them (`RELEASE SAVEPOINT`, `SHOW name`,
2076            // `BEGIN`, etc.), but they MUST still be usable as table /
2077            // column / alias names in DDL+DML. Sentori migrations like
2078            // 0001_init.sql ship `release TEXT NOT NULL` in the events
2079            // table — the `events.release` column carries the release
2080            // identifier string. Pre-T4 this triggered "expected
2081            // identifier, got Release" and blocked every drop-in user
2082            // whose schema had a column / alias with one of these names.
2083            other if unreserved_keyword_text(&other).is_some() => {
2084                unreserved_keyword_text(&other).unwrap()
2085            }
2086            other => {
2087                return Err(ParseError {
2088                    message: format!("expected identifier, got {other:?}"),
2089                    token_pos: self.consumed_pos(),
2090                });
2091            }
2092        };
2093        // v7.14.0 — strip optional `<schema>.` prefix. PG dumps
2094        // qualify every name with `public.` (and pg_catalog.* for
2095        // functions); SPG is single-schema so we discard the
2096        // prefix and return only the trailing ident. Same shape
2097        // also handles MySQL `db.tbl` cross-database refs (SPG
2098        // ignores the db part).
2099        if matches!(self.peek(), Token::Dot) {
2100            self.advance();
2101            match self.advance() {
2102                Token::Ident(s) | Token::QuotedIdent(s) => return Ok(s),
2103                other if unreserved_keyword_text(&other).is_some() => {
2104                    return Ok(unreserved_keyword_text(&other).unwrap());
2105                }
2106                other => {
2107                    return Err(ParseError {
2108                        message: format!("expected identifier after '{first}.', got {other:?}"),
2109                        token_pos: self.consumed_pos(),
2110                    });
2111                }
2112            }
2113        }
2114        Ok(first)
2115    }
2116
2117    #[allow(clippy::too_many_lines)]
2118    fn parse_one_statement(&mut self) -> Result<Statement, ParseError> {
2119        // v7.14.0 — empty / comment-only / semicolon-only input
2120        // (after the lexer strips line + block + MySQL
2121        // conditional comments) lands as Statement::Empty.
2122        // pg_dump and mysqldump emit several wrappers that
2123        // collapse to nothing after stripping (`/*!40101 SET …
2124        // */;`, blank lines between statements); the engine
2125        // returns CommandOk no-op so the dump loads cleanly.
2126        if matches!(self.peek(), Token::Eof | Token::Semicolon) {
2127            return Ok(Statement::Empty);
2128        }
2129        // v7.14.0 — pg_dump / mysqldump "noise" statements:
2130        // catalog / metadata DDL that has no behavioural effect
2131        // on SPG's single-schema, single-database, single-user
2132        // model. Consume the whole statement up to the next
2133        // semicolon / EOF and return Empty. This is broader than
2134        // the per-keyword DROP / SET / COMMENT arms but lets the
2135        // long tail of `LOCK TABLES`, `UNLOCK TABLES`, `GRANT`,
2136        // `REVOKE`, `ALTER OWNER TO`, `\restrict`, `\unrestrict`,
2137        // `BEGIN; COMMIT;` wrappers, etc. all pass through.
2138        if let Token::Ident(s) | Token::QuotedIdent(s) = self.peek() {
2139            let lc = s.to_ascii_lowercase();
2140            // v7.39 (read01 round 50) — COMMENT ON is a real statement now.
2141            if lc == "comment" {
2142                return self.parse_comment_on();
2143            }
2144            // v7.39 (read01 round 57) — so is GRANT / REVOKE.
2145            if lc == "grant" || lc == "revoke" {
2146                return self.parse_grant_or_revoke(lc == "grant");
2147            }
2148            // v7.39 (round 277) — the SQL-level prepared-statement
2149            // surface is REAL now. It used to be accepted and dropped
2150            // on the theory that "real execution still happens via the
2151            // extended-query flow" — true only for a driver that uses
2152            // that flow; a plain SQL PREPARE / EXECUTE returned no
2153            // rows at all.
2154            if lc == "prepare" {
2155                return self.parse_prepare();
2156            }
2157            if lc == "execute" {
2158                return self.parse_execute();
2159            }
2160            if lc == "deallocate" {
2161                return self.parse_deallocate();
2162            }
2163            // v7.39 (round 278) — `CALL <proc>(<args>)` used to be
2164            // accepted and dropped, so an application's stored-procedure
2165            // invocation reported success and did nothing. SPG has no
2166            // procedure catalog, so every CALL names a procedure that
2167            // does not exist — which is exactly what PG says.
2168            if lc == "call" {
2169                return self.parse_call();
2170            }
2171            // v7.39 (round 318, V51) — MySQL `KILL`. Not dump noise: it
2172            // names one connection and acts on it.
2173            if lc == "kill" {
2174                return self.parse_kill();
2175            }
2176            if lc == "discard" {
2177                return self.parse_discard();
2178            }
2179            // v7.39 (round 696) — REASSIGN OWNED BY <role> [, …] TO <role>.
2180            // Still performs nothing; the roles are carried out so a name
2181            // that does not exist is refused, as PG18 refuses it.
2182            if lc == "reassign" {
2183                self.advance();
2184                if matches!(self.peek(), Token::Ident(k) if k.eq_ignore_ascii_case("owned")) {
2185                    self.advance();
2186                }
2187                if self.peek_is_by() {
2188                    self.advance();
2189                }
2190                // Only the roles BEFORE the TO are the ones that must
2191                // exist — `TO` names the new owner, which PG checks as
2192                // well, so both lists are collected.
2193                let mut names = self.take_comma_separated_names();
2194                if matches!(self.peek(), Token::To) {
2195                    self.advance();
2196                    names.extend(self.take_comma_separated_names());
2197                }
2198                self.consume_until_statement_boundary();
2199                return Ok(Statement::ValidateOnly {
2200                    kind: crate::ast::ValidateOnlyKind::RoleName,
2201                    names,
2202                });
2203            }
2204            // v7.39 (round 696) — SECURITY LABEL. PG18 refuses it
2205            // unconditionally with `no security label providers have been
2206            // loaded`, whatever object it names, because none is loaded.
2207            // SPG has none either; accepting it told the caller a label had
2208            // been applied when nothing anywhere records one.
2209            if lc == "security" {
2210                self.consume_until_statement_boundary();
2211                return Ok(Statement::ValidateOnly {
2212                    kind: crate::ast::ValidateOnlyKind::SecurityLabel,
2213                    names: Vec::new(),
2214                });
2215            }
2216            // v7.39.2 — `USE <db>` is a real statement now, and only in
2217            // the MySQL dialect. It used to be swallowed here with the
2218            // dump noise, so `USE myapp; SELECT DATABASE()` answered the
2219            // same constant it answered before — MySQL 9.7.2 answers
2220            // `myapp`. PostgreSQL has no USE at all, and pg_dump does not
2221            // emit one, but the swallow stays on that side: it was put
2222            // there for restores and taking it away is not this defect.
2223            if lc == "use" {
2224                if self.mysql_dialect {
2225                    self.advance();
2226                    let name = self.expect_ident_like()?;
2227                    return Ok(Statement::UseDatabase(name));
2228                }
2229                self.consume_until_statement_boundary();
2230                return Ok(Statement::Empty);
2231            }
2232            if is_dump_noise_statement(&lc) {
2233                self.consume_until_statement_boundary();
2234                return Ok(Statement::Empty);
2235            }
2236        }
2237        match self.peek() {
2238            Token::Select => self.parse_select_stmt(),
2239            // v7.37.17 (17.6 siblings) — a statement opening with a
2240            // parenthesized query group: `(SELECT … UNION …)
2241            // INTERSECT …`. parse_bare_select's group arm consumes
2242            // the parens; the select parser handles the outer chain
2243            // and tail.
2244            Token::LParen
2245                if matches!(
2246                    self.tokens.get(self.pos + 1),
2247                    Some(Token::Select | Token::LParen | Token::Values)
2248                ) =>
2249            {
2250                self.parse_select_stmt()
2251            }
2252            // v7.37.17 (17.6 siblings) — top-level bare VALUES
2253            // statement (`VALUES (1), (2) [ORDER BY …] [LIMIT …]`).
2254            // Lowers to the same UNION ALL chain the FROM-position
2255            // form uses, then reuses the shared SELECT tail.
2256            Token::Values => {
2257                self.advance(); // VALUES
2258                let mut head = self.parse_values_rows_body()?;
2259                self.parse_select_tail_into(&mut head)?;
2260                Ok(Statement::Select(head))
2261            }
2262            // SQL-standard `TABLE name` shorthand for
2263            // `SELECT * FROM name` — pg_dump never emits it, but
2264            // psql users and PG docs use it constantly. Set-op
2265            // chains and the ORDER BY/LIMIT tail compose like any
2266            // SELECT head.
2267            Token::Table
2268                if matches!(
2269                    self.tokens.get(self.pos + 1),
2270                    Some(Token::Ident(_) | Token::QuotedIdent(_))
2271                ) =>
2272            {
2273                let mut head = self.parse_table_shorthand()?;
2274                self.parse_setop_chain_into(&mut head)?;
2275                self.parse_select_tail_into(&mut head)?;
2276                Ok(Statement::Select(head))
2277            }
2278            // v7.9.27 — `DO $$ … $$ [LANGUAGE plpgsql]`. The
2279            // body is a dollar-quoted plpgsql block (lexer already
2280            // collapsed `$$…$$` into a single Token::String).
2281            // v7.16.2 — mailrs round-10 A.2: parse the body as a
2282            // real PlPgSqlBlock so the engine can EXECUTE it at
2283            // top level instead of silently swallowing. Pre-
2284            // v7.16.2 the parser threw the body away and the
2285            // engine returned CommandOk for the entire DO; that
2286            // turned `DO BEGIN … IF EXISTS ... THEN ALTER …; END
2287            // $$` into a SEV-1 silent no-op (the IF + the rename
2288            // were both invisible — mailrs's migrate-042 didn't
2289            // actually run). Now the body parses + executes;
2290            // EmbeddedSql inside the block runs immediately
2291            // against the engine (not deferred — we're at top
2292            // level, not inside a trigger row-write loop).
2293            Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("do") => {
2294                self.advance();
2295                let body_text = match self.advance() {
2296                    Token::String(s) => s,
2297                    other => {
2298                        return Err(self.err(alloc::format!(
2299                            "expected dollar-quoted body after DO, got {other:?}"
2300                        )));
2301                    }
2302                };
2303                // Optional `LANGUAGE <name>` trailer (idents only).
2304                if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("language")) {
2305                    self.advance();
2306                    let _ = self.expect_ident_like()?;
2307                }
2308                // Parse the body — same shape CREATE FUNCTION
2309                // uses for trigger function bodies. If the body
2310                // doesn't parse cleanly we surface the error
2311                // (better than silent no-op).
2312                let block = parse_plpgsql_body(&body_text)?;
2313                Ok(Statement::DoBlock(block))
2314            }
2315            // v4.11: `WITH name AS (SELECT ...) [, ...] SELECT ...`.
2316            // WITH isn't a reserved token in our lexer — comes through
2317            // as `Token::Ident("with")` (case-insensitive).
2318            Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("with") => {
2319                self.advance();
2320                self.parse_with_cte_then_select()
2321            }
2322            // v4.26: `EXPLAIN [ANALYZE] <select>`. Comes through as
2323            // an identifier — not a reserved keyword.
2324            Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("explain") => {
2325                self.advance();
2326                let mut analyze = false;
2327                let mut suggest = false;
2328                let mut costs_off = false;
2329                let mut buffers = false;
2330                let mut timing_off = false;
2331                let mut settings = false;
2332                let mut wal = false;
2333                let mut summary_off = false;
2334                let mut format = crate::ast::ExplainFormat::Text;
2335                // v6.8.3 + v7.37.7 — `EXPLAIN (option [, option…])`
2336                // syntax accepts SUGGEST + COSTS ON|OFF. Multiple
2337                // options are comma-separated. Booleans default to ON
2338                // when the value token is omitted (matches PG).
2339                if matches!(self.peek(), Token::LParen) {
2340                    self.advance();
2341                    loop {
2342                        let opt = match self.peek().clone() {
2343                            Token::Ident(s) | Token::QuotedIdent(s) => s,
2344                            other => {
2345                                return Err(self.err(format!(
2346                                    "expected option keyword inside EXPLAIN (…), got {other:?}"
2347                                )));
2348                            }
2349                        };
2350                        self.advance();
2351                        if opt.eq_ignore_ascii_case("suggest") {
2352                            suggest = true;
2353                            // SUGGEST takes no explicit value today.
2354                        } else if opt.eq_ignore_ascii_case("costs") {
2355                            // PG syntax: `COSTS [ON | OFF]`. Default
2356                            // when value omitted is ON, so plain
2357                            // `COSTS` is a no-op. `COSTS OFF` flips.
2358                            // `ON` lexes to `Token::On` (reserved
2359                            // keyword in JOIN ... ON contexts); accept
2360                            // it alongside the bare Ident form so the
2361                            // grammar matches PG verbatim.
2362                            let value = match self.peek().clone() {
2363                                Token::On => {
2364                                    self.advance();
2365                                    true
2366                                }
2367                                Token::Ident(v) | Token::QuotedIdent(v)
2368                                    if v.eq_ignore_ascii_case("off") =>
2369                                {
2370                                    self.advance();
2371                                    false
2372                                }
2373                                Token::Ident(v) | Token::QuotedIdent(v)
2374                                    if v.eq_ignore_ascii_case("true") =>
2375                                {
2376                                    self.advance();
2377                                    true
2378                                }
2379                                _ => true,
2380                            };
2381                            costs_off = !value;
2382                        } else if opt.eq_ignore_ascii_case("analyze")
2383                            || opt.eq_ignore_ascii_case("analyse")
2384                        {
2385                            // v7.37.22 — `EXPLAIN (ANALYZE [ON|OFF]) <S>`.
2386                            // Same default-ON rule as ANALYZE keyword form.
2387                            let value = match self.peek().clone() {
2388                                Token::On => {
2389                                    self.advance();
2390                                    true
2391                                }
2392                                Token::Ident(v) | Token::QuotedIdent(v)
2393                                    if v.eq_ignore_ascii_case("off") =>
2394                                {
2395                                    self.advance();
2396                                    false
2397                                }
2398                                Token::Ident(v) | Token::QuotedIdent(v)
2399                                    if v.eq_ignore_ascii_case("true") =>
2400                                {
2401                                    self.advance();
2402                                    true
2403                                }
2404                                _ => true,
2405                            };
2406                            analyze = value;
2407                        } else if opt.eq_ignore_ascii_case("buffers") {
2408                            // v7.37.22 — `BUFFERS [ON|OFF]`.
2409                            let value = match self.peek().clone() {
2410                                Token::On => {
2411                                    self.advance();
2412                                    true
2413                                }
2414                                Token::Ident(v) | Token::QuotedIdent(v)
2415                                    if v.eq_ignore_ascii_case("off") =>
2416                                {
2417                                    self.advance();
2418                                    false
2419                                }
2420                                Token::Ident(v) | Token::QuotedIdent(v)
2421                                    if v.eq_ignore_ascii_case("true") =>
2422                                {
2423                                    self.advance();
2424                                    true
2425                                }
2426                                _ => true,
2427                            };
2428                            buffers = value;
2429                        } else if opt.eq_ignore_ascii_case("timing") {
2430                            // v7.37.22 — `TIMING [ON|OFF]`. OFF strips
2431                            // the measured wall-clock annotation.
2432                            let value = match self.peek().clone() {
2433                                Token::On => {
2434                                    self.advance();
2435                                    true
2436                                }
2437                                Token::Ident(v) | Token::QuotedIdent(v)
2438                                    if v.eq_ignore_ascii_case("off") =>
2439                                {
2440                                    self.advance();
2441                                    false
2442                                }
2443                                Token::Ident(v) | Token::QuotedIdent(v)
2444                                    if v.eq_ignore_ascii_case("true") =>
2445                                {
2446                                    self.advance();
2447                                    true
2448                                }
2449                                _ => true,
2450                            };
2451                            timing_off = !value;
2452                        } else if opt.eq_ignore_ascii_case("settings") {
2453                            settings = true;
2454                        } else if opt.eq_ignore_ascii_case("wal") {
2455                            wal = true;
2456                        } else if opt.eq_ignore_ascii_case("summary") {
2457                            // v7.39 (round 227) — `SUMMARY [ON|OFF]` really
2458                            // gates the trailing Planning/Execution Time
2459                            // lines now (was accept-and-no-op).
2460                            let value = match self.peek().clone() {
2461                                Token::On => {
2462                                    self.advance();
2463                                    true
2464                                }
2465                                Token::Ident(v) | Token::QuotedIdent(v)
2466                                    if v.eq_ignore_ascii_case("off") =>
2467                                {
2468                                    self.advance();
2469                                    false
2470                                }
2471                                Token::Ident(v) | Token::QuotedIdent(v)
2472                                    if v.eq_ignore_ascii_case("true") =>
2473                                {
2474                                    self.advance();
2475                                    true
2476                                }
2477                                _ => true,
2478                            };
2479                            summary_off = !value;
2480                        } else if opt.eq_ignore_ascii_case("verbose")
2481                            || opt.eq_ignore_ascii_case("format")
2482                        {
2483                            // v7.37.22 — accept-but-no-op the remaining
2484                            // PG options so EXPLAIN-using clients
2485                            // (pgAdmin / DataGrip) don't see syntax
2486                            // errors. FORMAT takes a value (text /
2487                            // json / yaml / xml); skip the next token
2488                            // if it's an ident.
2489                            if opt.eq_ignore_ascii_case("format") {
2490                                if let Token::Ident(v) | Token::QuotedIdent(v) = self.peek().clone()
2491                                {
2492                                    self.advance();
2493                                    format = match v.to_ascii_lowercase().as_str() {
2494                                        "text" => crate::ast::ExplainFormat::Text,
2495                                        "json" => crate::ast::ExplainFormat::Json,
2496                                        "xml" => crate::ast::ExplainFormat::Xml,
2497                                        "yaml" => crate::ast::ExplainFormat::Yaml,
2498                                        other => {
2499                                            return Err(self.err(format!(
2500                                                "EXPLAIN (FORMAT …): unknown format {other:?}; \
2501                                                 supports text, json, xml, yaml"
2502                                            )));
2503                                        }
2504                                    };
2505                                }
2506                            } else {
2507                                // VERBOSE / SUMMARY take optional ON/OFF;
2508                                // consume if present.
2509                                if matches!(self.peek(), Token::On) {
2510                                    self.advance();
2511                                } else if let Token::Ident(v) | Token::QuotedIdent(v) =
2512                                    self.peek().clone()
2513                                    && (v.eq_ignore_ascii_case("off")
2514                                        || v.eq_ignore_ascii_case("true"))
2515                                {
2516                                    self.advance();
2517                                    let _ = v;
2518                                }
2519                            }
2520                        } else {
2521                            return Err(self.err(format!(
2522                                "unknown EXPLAIN option {opt:?}; supports ANALYZE, COSTS, BUFFERS, TIMING, SETTINGS, WAL, SUGGEST, VERBOSE, FORMAT, SUMMARY"
2523                            )));
2524                        }
2525                        if matches!(self.peek(), Token::Comma) {
2526                            self.advance();
2527                            continue;
2528                        }
2529                        break;
2530                    }
2531                    if !matches!(self.peek(), Token::RParen) {
2532                        return Err(self.err(format!(
2533                            "expected ')' after EXPLAIN options, got {:?}",
2534                            self.peek()
2535                        )));
2536                    }
2537                    self.advance();
2538                } else if let Token::Ident(s) | Token::QuotedIdent(s) = self.peek()
2539                    && (s.eq_ignore_ascii_case("analyze") || s.eq_ignore_ascii_case("analyse"))
2540                {
2541                    self.advance();
2542                    analyze = true;
2543                }
2544                // v7.39 (round 224) — the body may open with WITH (CTEs);
2545                // route through the same CTE-then-SELECT path the top-level
2546                // WITH statement uses. v7.39 (round 225) — DML bodies parse
2547                // too (PG explains INSERT / UPDATE / DELETE).
2548                let inner = match self.peek().clone() {
2549                    Token::Ident(s) if s.eq_ignore_ascii_case("with") => {
2550                        self.advance();
2551                        self.parse_with_cte_then_select()?
2552                    }
2553                    Token::Insert => self.parse_insert_stmt(false)?,
2554                    Token::Ident(s) if s.eq_ignore_ascii_case("update") => {
2555                        self.advance();
2556                        self.parse_update_after_keyword()?
2557                    }
2558                    Token::Ident(s) if s.eq_ignore_ascii_case("delete") => {
2559                        self.advance();
2560                        self.parse_delete_after_keyword()?
2561                    }
2562                    _ => self.parse_select_stmt()?,
2563                };
2564                if !matches!(
2565                    inner,
2566                    Statement::Select(_)
2567                        | Statement::Insert(_)
2568                        | Statement::Update(_)
2569                        | Statement::Delete(_)
2570                ) {
2571                    return Err(self.err(format!(
2572                        "EXPLAIN body must be SELECT / INSERT / UPDATE / DELETE, got {inner:?}"
2573                    )));
2574                }
2575                Ok(Statement::Explain(crate::ast::ExplainStatement {
2576                    analyze,
2577                    inner: Box::new(inner),
2578                    suggest,
2579                    costs_off,
2580                    buffers,
2581                    timing_off,
2582                    settings,
2583                    wal,
2584                    summary_off,
2585                    format,
2586                }))
2587            }
2588            Token::Create => self.parse_create_stmt(),
2589            Token::Insert => self.parse_insert_stmt(false),
2590            // MySQL `DESCRIBE t` / `DESC t` — the SHOW COLUMNS
2591            // spelling; route to the same handler. DESC is the
2592            // reserved ORDER BY token, so it gets its own arm.
2593            Token::Ident(s)
2594                if s.eq_ignore_ascii_case("describe")
2595                    && matches!(
2596                        self.tokens.get(self.pos + 1),
2597                        Some(Token::Ident(_) | Token::QuotedIdent(_))
2598                    ) =>
2599            {
2600                self.advance();
2601                let table = self.expect_ident_like()?;
2602                Ok(Statement::ShowColumns(table))
2603            }
2604            Token::Desc
2605                if matches!(
2606                    self.tokens.get(self.pos + 1),
2607                    Some(Token::Ident(_) | Token::QuotedIdent(_))
2608                ) =>
2609            {
2610                self.advance();
2611                let table = self.expect_ident_like()?;
2612                Ok(Statement::ShowColumns(table))
2613            }
2614            // `COPY table [(cols)] TO STDOUT` — the export half of
2615            // pg_dump's COPY pair (the FROM stdin half rides the
2616            // embed import path). Options need a format design and
2617            // error honestly.
2618            Token::Ident(s)
2619                if s.eq_ignore_ascii_case("copy")
2620                    && matches!(
2621                        self.tokens.get(self.pos + 1),
2622                        Some(Token::Ident(_) | Token::QuotedIdent(_))
2623                    ) =>
2624            {
2625                self.advance(); // COPY
2626                let table = self.expect_ident_like()?;
2627                let columns = if matches!(self.peek(), Token::LParen) {
2628                    self.advance();
2629                    let mut cols = alloc::vec![self.expect_ident_like()?];
2630                    while matches!(self.peek(), Token::Comma) {
2631                        self.advance();
2632                        cols.push(self.expect_ident_like()?);
2633                    }
2634                    if !matches!(self.peek(), Token::RParen) {
2635                        return Err(self.err(format!(
2636                            "expected ')' after COPY column list, got {:?}",
2637                            self.peek()
2638                        )));
2639                    }
2640                    self.advance();
2641                    Some(cols)
2642                } else {
2643                    None
2644                };
2645                // v7.39 (round 249) — `COPY t FROM '<path>'`: the file
2646                // endpoint. (FROM STDIN still rides the wire/import path —
2647                // its data arrives out of band.)
2648                if matches!(self.peek(), Token::From)
2649                    && matches!(self.tokens.get(self.pos + 1), Some(Token::String(_)))
2650                {
2651                    self.advance(); // FROM
2652                    let Token::String(path) = self.advance() else {
2653                        unreachable!()
2654                    };
2655                    let options = self.parse_copy_to_options()?;
2656                    return Ok(Statement::CopyFromFile {
2657                        table,
2658                        columns,
2659                        path,
2660                        options,
2661                    });
2662                }
2663                if !matches!(self.peek(), Token::To) {
2664                    return Err(self.err(format!(
2665                        "COPY: only TO STDOUT is supported here (FROM stdin \
2666                         rides the import path); got {:?}",
2667                        self.peek()
2668                    )));
2669                }
2670                self.advance();
2671                if matches!(self.peek(), Token::String(_)) {
2672                    let Token::String(path) = self.advance() else { unreachable!() };
2673                    let options = self.parse_copy_to_options()?;
2674                    return Ok(Statement::CopyToFile {
2675                        table,
2676                        columns,
2677                        query: None,
2678                        path,
2679                        options,
2680                    });
2681                }
2682                if !matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("stdout")) {
2683                    return Err(self.err(format!(
2684                        "COPY TO supports STDOUT only (no file endpoints), got {:?}",
2685                        self.peek()
2686                    )));
2687                }
2688                self.advance();
2689                let options = self.parse_copy_to_options()?;
2690                Ok(Statement::CopyTo {
2691                    table,
2692                    columns,
2693                    query: None,
2694                    options,
2695                })
2696            }
2697            // v7.39 (read01 round 94) — `COPY (<query>) TO STDOUT [WITH (…)]`.
2698            // The parens directly after COPY wrap a SELECT/VALUES/CTE whose
2699            // result set is streamed in COPY format (PG's query form).
2700            Token::Ident(s)
2701                if s.eq_ignore_ascii_case("copy")
2702                    && matches!(self.tokens.get(self.pos + 1), Some(Token::LParen)) =>
2703            {
2704                self.advance(); // COPY
2705                self.advance(); // (
2706                let query = self.parse_select_stmt()?;
2707                if !matches!(self.peek(), Token::RParen) {
2708                    return Err(self.err(format!(
2709                        "expected ')' after COPY query, got {:?}",
2710                        self.peek()
2711                    )));
2712                }
2713                self.advance(); // )
2714                if !matches!(self.peek(), Token::To) {
2715                    return Err(self.err(format!(
2716                        "COPY (query): only TO STDOUT is supported, got {:?}",
2717                        self.peek()
2718                    )));
2719                }
2720                self.advance();
2721                if matches!(self.peek(), Token::String(_)) {
2722                    let Token::String(path) = self.advance() else { unreachable!() };
2723                    let options = self.parse_copy_to_options()?;
2724                    return Ok(Statement::CopyToFile {
2725                        table: String::new(),
2726                        columns: None,
2727                        query: Some(alloc::boxed::Box::new(query)),
2728                        path,
2729                        options,
2730                    });
2731                }
2732                if !matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("stdout")) {
2733                    return Err(self.err(format!(
2734                        "COPY (query): TO supports STDOUT only, got {:?}",
2735                        self.peek()
2736                    )));
2737                }
2738                self.advance();
2739                let options = self.parse_copy_to_options()?;
2740                Ok(Statement::CopyTo {
2741                    table: String::new(),
2742                    columns: None,
2743                    query: Some(alloc::boxed::Box::new(query)),
2744                    options,
2745                })
2746            }
2747            // MySQL `REPLACE INTO t …` — delete-then-insert upsert.
2748            // Shares the INSERT body; the replace flag lowers it
2749            // onto ON CONFLICT DO UPDATE with an empty assignment
2750            // list (engine: replace the whole row).
2751            Token::Ident(s)
2752                if s.eq_ignore_ascii_case("replace")
2753                    && matches!(self.tokens.get(self.pos + 1), Some(Token::Into)) =>
2754            {
2755                self.parse_insert_stmt(true)
2756            }
2757            Token::Begin => {
2758                self.advance();
2759                // v7.38 轴 4 / v7.39 (read01 round 118, B3) — PG-standard
2760                // `BEGIN [WORK|TRANSACTION] [ISOLATION LEVEL …] [READ ONLY|WRITE]
2761                // [[NOT] DEFERRABLE]`. The optional WORK/TRANSACTION noise word
2762                // is consumed first, then the trailing modes — including the
2763                // case where `ISOLATION LEVEL …` follows BEGIN directly (no
2764                // WORK/TRANSACTION). The explicit level, when present, rides the
2765                // statement so `exec_begin` applies it for this transaction.
2766                if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("work") || s.eq_ignore_ascii_case("transaction"))
2767                {
2768                    self.advance();
2769                }
2770                let iso = self.parse_isolation_level_clauses()?;
2771                Ok(Statement::Begin(iso))
2772            }
2773            // v7.38 轴 4 — PG-standard `START TRANSACTION …` synonym
2774            // for BEGIN. START is contextual in PG too; pattern-match
2775            // on the ident here. Iso clauses are parse-and-ignored,
2776            // same as BEGIN above.
2777            Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("start") => {
2778                self.advance();
2779                if !matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("transaction"))
2780                {
2781                    return Err(self.err(alloc::format!(
2782                        "expected TRANSACTION after START, got {:?}",
2783                        self.peek()
2784                    )));
2785                }
2786                self.advance();
2787                let iso = self.parse_isolation_level_clauses()?;
2788                Ok(Statement::Begin(iso))
2789            }
2790            Token::Commit => {
2791                self.advance();
2792                // PG: `COMMIT [WORK | TRANSACTION]`.
2793                if let Token::Ident(w) = self.peek()
2794                    && (w.eq_ignore_ascii_case("work") || w.eq_ignore_ascii_case("transaction"))
2795                {
2796                    self.advance();
2797                }
2798                Ok(Statement::Commit)
2799            }
2800            // r1066 (7.38 S5.1) — `END [WORK | TRANSACTION]` is PG's
2801            // COMMIT synonym; pgbench's builtin tpcb-like script closes
2802            // every transaction with `END;` and the drop-in aborted on
2803            // it. Only reachable at statement start (CASE … END lives
2804            // inside expressions), so no ambiguity.
2805            Token::Ident(s) if s.eq_ignore_ascii_case("end") => {
2806                self.advance();
2807                if let Token::Ident(w) = self.peek()
2808                    && (w.eq_ignore_ascii_case("work") || w.eq_ignore_ascii_case("transaction"))
2809                {
2810                    self.advance();
2811                }
2812                Ok(Statement::Commit)
2813            }
2814            Token::Rollback => {
2815                self.advance();
2816                // `ROLLBACK TO [SAVEPOINT] <name>` returns to that
2817                // savepoint without ending the transaction. Bare
2818                // `ROLLBACK` drops the whole TX.
2819                if matches!(self.peek(), Token::To) {
2820                    self.advance();
2821                    if matches!(self.peek(), Token::Savepoint) {
2822                        self.advance();
2823                    }
2824                    let name = self.expect_ident_like()?;
2825                    Ok(Statement::RollbackToSavepoint(name))
2826                } else {
2827                    Ok(Statement::Rollback)
2828                }
2829            }
2830            Token::Savepoint => {
2831                self.advance();
2832                let name = self.expect_ident_like()?;
2833                Ok(Statement::Savepoint(name))
2834            }
2835            Token::Release => {
2836                self.advance();
2837                // `RELEASE [SAVEPOINT] <name>` — the `SAVEPOINT` keyword
2838                // is optional in standard SQL.
2839                if matches!(self.peek(), Token::Savepoint) {
2840                    self.advance();
2841                }
2842                let name = self.expect_ident_like()?;
2843                Ok(Statement::ReleaseSavepoint(name))
2844            }
2845            Token::Show => {
2846                self.advance();
2847                // `SHOW TABLES` / `SHOW USERS` / `SHOW COLUMNS FROM <table>`.
2848                // v6.1.2 promoted TABLES to a reserved keyword (for
2849                // `CREATE PUBLICATION … FOR ALL TABLES`), so it now
2850                // arrives as `Token::Tables` rather than a bare ident.
2851                // USERS / COLUMNS remain bare idents.
2852                let target = match self.advance() {
2853                    Token::Tables => "tables".to_string(),
2854                    // v7.17.0 Phase 3.P0-59 — CREATE is a reserved
2855                    // keyword token; recognise it as the SHOW CREATE
2856                    // dispatch keyword too.
2857                    Token::Create => "create".to_string(),
2858                    // v7.17.0 Phase 3.P0-60 — INDEX is a reserved
2859                    // keyword too; let SHOW INDEX FROM parse.
2860                    Token::Index => "index".to_string(),
2861                    // v7.37.17 (17.6 sibling) — SHOW ALL. ALL is
2862                    // reserved (used in aggregate function calls);
2863                    // recognise it here so the parser dispatches
2864                    // to ShowParameter("all") — the engine returns
2865                    // the curated parameter inventory.
2866                    Token::All => "all".to_string(),
2867                    // v7.38.18 (C12) — `SHOW COUNT(*) WARNINGS`, MySQL's
2868                    // spelling for the size of the diagnostics area.
2869                    // MySQL-dialect only: PostgreSQL 18.4 answers this
2870                    // phrase with `syntax error at or near "("`, and a
2871                    // PG session must keep getting exactly that rather
2872                    // than a message about an unknown parameter.
2873                    // `COUNT` arrives as a bare ident; the `(*)` and the
2874                    // trailing keyword are consumed here so the whole
2875                    // form reaches the engine as one parameter name.
2876                    Token::Ident(ref c)
2877                        if self.mysql_dialect
2878                            && c.eq_ignore_ascii_case("count")
2879                            && matches!(self.peek(), Token::LParen) =>
2880                    {
2881                        self.advance();
2882                        if matches!(self.peek(), Token::Star) {
2883                            self.advance();
2884                        }
2885                        if matches!(self.peek(), Token::RParen) {
2886                            self.advance();
2887                        }
2888                        match self.advance() {
2889                            Token::Ident(w) if w.eq_ignore_ascii_case("warnings") => {
2890                                return Ok(Statement::ShowParameter(
2891                                    "count(*) warnings".to_string(),
2892                                ));
2893                            }
2894                            other => {
2895                                return Err(self.err(format!(
2896                                    "expected WARNINGS after SHOW COUNT(*), got {other:?}"
2897                                )));
2898                            }
2899                        }
2900                    }
2901                    Token::Ident(s) | Token::QuotedIdent(s) => s.to_ascii_lowercase(),
2902                    other => {
2903                        return Err(self.err(format!(
2904                            "expected SHOW target, got {other:?}"
2905                        )));
2906                    }
2907                };
2908                match target.as_str() {
2909                    "tables" => Ok(Statement::ShowTables),
2910                    "users" => Ok(Statement::ShowUsers),
2911                    // v7.38 轴 4 — `SHOW transaction_isolation`
2912                    // returns the currently-selected isolation level.
2913                    "transaction_isolation" => Ok(Statement::ShowParameter(
2914                        "transaction_isolation".to_string(),
2915                    )),
2916                    // v7.17.0 Phase 3.P0-59 — MySQL `SHOW CREATE
2917                    // TABLE <t>` returns a 2-column row: (Table,
2918                    // Create Table). mysqldump emits this for every
2919                    // table at scrape time; without it the dump
2920                    // round-trip stalls.
2921                    // v7.17.0 Phase 3.P0-60 — MySQL `SHOW INDEXES
2922                    // FROM <t>` (also spelled `SHOW INDEX` and
2923                    // `SHOW KEYS`). admin / mysqldump probes use
2924                    // it to list per-table indexes.
2925                    "indexes" | "index" | "keys" => {
2926                        if !matches!(self.peek(), Token::From) {
2927                            return Err(self.err(format!(
2928                                "expected FROM after SHOW INDEXES, got {:?}",
2929                                self.peek()
2930                            )));
2931                        }
2932                        self.advance();
2933                        let table = self.expect_ident_like()?;
2934                        Ok(Statement::ShowIndexes(table))
2935                    }
2936                    // v7.17.0 Phase 3.P0-61 — MySQL `SHOW STATUS` /
2937                    // `SHOW VARIABLES`. Both return a 2-column row
2938                    // set listing server-side state; clients probe
2939                    // them at connect time.
2940                    "status" => Ok(Statement::ShowStatus),
2941                    "variables" => {
2942                        // r1067 — `SHOW VARIABLES LIKE 'pat'`.
2943                        if matches!(self.peek(), Token::Like) {
2944                            self.advance();
2945                            let pat = match self.advance() {
2946                                Token::String(p) => p,
2947                                other => {
2948                                    return Err(self.err(format!(
2949                                        "SHOW VARIABLES LIKE expects a quoted pattern, got {other:?}"
2950                                    )));
2951                                }
2952                            };
2953                            return Ok(Statement::ShowVariablesLike(pat));
2954                        }
2955                        Ok(Statement::ShowVariables)
2956                    }
2957                    // v7.17.0 Phase 3.P0-62 — MySQL `SHOW PROCESSLIST`.
2958                    "processlist" => Ok(Statement::ShowProcesslist),
2959                    "create" => {
2960                        // SHOW CREATE TABLE / VIEW / DATABASE — only
2961                        // TABLE is supported in v7.17.
2962                        let kind = match self.advance() {
2963                            Token::Ident(s) | Token::QuotedIdent(s) => s,
2964                            Token::Table => "table".to_string(),
2965                            other => {
2966                                return Err(self.err(format!(
2967                                    "expected TABLE after SHOW CREATE, got {other:?}"
2968                                )));
2969                            }
2970                        };
2971                        if !kind.eq_ignore_ascii_case("table") {
2972                            return Err(self.err(format!(
2973                                "unsupported SHOW CREATE {kind:?}; v7.17 supports TABLE only"
2974                            )));
2975                        }
2976                        let name = self.expect_ident_like()?;
2977                        Ok(Statement::ShowCreateTable(name))
2978                    }
2979                    // v7.17.0 Phase 3.P0-58 — MySQL `SHOW DATABASES`
2980                    // (and `SHOW SCHEMAS` alias). The mysql client uses
2981                    // it to populate the database selector at connect
2982                    // time; without it `mysql -p` errors before the
2983                    // first user query.
2984                    "databases" | "schemas" => Ok(Statement::ShowDatabases),
2985                    // v6.1.3 — PUBLICATIONS plural is NOT a reserved
2986                    // keyword on its own; it lands here as a bare
2987                    // ident. Returning all publications + their
2988                    // scope summary.
2989                    "publications" => Ok(Statement::ShowPublications),
2990                    // v6.1.4 — same shape for SUBSCRIPTIONS plural.
2991                    "subscriptions" => Ok(Statement::ShowSubscriptions),
2992                    "columns" => {
2993                        if !matches!(self.peek(), Token::From) {
2994                            return Err(self.err(format!(
2995                                "expected FROM after SHOW COLUMNS, got {:?}",
2996                                self.peek()
2997                            )));
2998                        }
2999                        self.advance();
3000                        let table = self.expect_ident_like()?;
3001                        Ok(Statement::ShowColumns(table))
3002                    }
3003                    // v7.38 轴 4 surface — `SHOW <param>` for any
3004                    // remaining session / preset parameter name
3005                    // (server_version, search_path, client_encoding,
3006                    // …). The engine's ShowParameter handler does the
3007                    // dispatch; unrecognised names error there with
3008                    // a pointer to pg_settings, not at parse time —
3009                    // so a driver that issues `SHOW spam_setting`
3010                    // gets a clear runtime error instead of a
3011                    // confusing "unknown SHOW target".
3012                    other => {
3013                        // v7.38 (read01 P3.20) — a custom namespaced GUC
3014                        // (`SHOW app.foo`) arrives as `app` + `.` + `foo`;
3015                        // consume the dotted tail so it round-trips with
3016                        // `SET app.foo` / `current_setting('app.foo')`.
3017                        let mut full = other.to_string();
3018                        while matches!(self.peek(), Token::Dot) {
3019                            self.advance();
3020                            let seg = self.expect_ident_like()?;
3021                            full.push('.');
3022                            full.push_str(&seg.to_ascii_lowercase());
3023                        }
3024                        Ok(Statement::ShowParameter(full))
3025                    }
3026                }
3027            }
3028            // v6.1.2: `DROP` is now a reserved keyword (it dispatches
3029            // to DROP USER and DROP PUBLICATION today; DROP TABLE /
3030            // DROP INDEX are still SHOW-shaped admin ops). Pre-6.1.2
3031            // arrived as a bare ident; tokenising it dedicatedly
3032            // keeps the dispatch tree small.
3033            Token::Drop => {
3034                self.advance();
3035                match self.peek() {
3036                    // v7.37.17 (17.6 sibling) — DROP OWNED BY <role>
3037                    // [, ...] [CASCADE | RESTRICT]. pg_dumpall emits
3038                    // around DROP ROLE cleanup. SPG has no role-owner
3039                    // model, so consume to boundary as a no-op.
3040                    Token::Ident(s) | Token::QuotedIdent(s)
3041                        if s.eq_ignore_ascii_case("owned") =>
3042                    {
3043                        // v7.39 (round 696) — still a no-op (SPG has no
3044                        // role-owner model), but the ROLE is carried out so
3045                        // the engine can refuse one that does not exist,
3046                        // which is what PG18 does.
3047                        self.advance();
3048                        if self.peek_is_by() {
3049                            self.advance();
3050                        }
3051                        let names = self.take_comma_separated_names();
3052                        self.consume_until_statement_boundary();
3053                        Ok(Statement::ValidateOnly {
3054                            kind: crate::ast::ValidateOnlyKind::RoleName,
3055                            names,
3056                        })
3057                    }
3058                    // v7.39 (round 436) — MySQL's `DROP TEMPORARY TABLE t`.
3059                    // It drops only a TEMPORARY table, and name resolution
3060                    // already prefers the session's own, so the keyword is
3061                    // consumed and the ordinary DROP TABLE path runs.
3062                    Token::Ident(s) | Token::QuotedIdent(s)
3063                        if s.eq_ignore_ascii_case("temporary") || s.eq_ignore_ascii_case("temp") =>
3064                    {
3065                        self.advance();
3066                        if !matches!(self.peek(), Token::Table) {
3067                            return Err(self.err(alloc::format!(
3068                                "expected TABLE after DROP TEMPORARY, got {:?}",
3069                                self.peek()
3070                            )));
3071                        }
3072                        self.parse_drop_table_after_keyword()
3073                    }
3074                    Token::Publication => {
3075                        self.advance();
3076                        // v7.39 (round 754, F31-B4) — the round-753
3077                        // audit probe tripped over the missing
3078                        // `IF EXISTS` here (syntax error).
3079                        let if_exists = self.consume_if_exists();
3080                        let name = self.expect_ident_or_string()?;
3081                        Ok(Statement::DropPublication { name, if_exists })
3082                    }
3083                    Token::Subscription => {
3084                        self.advance();
3085                        let if_exists = self.consume_if_exists();
3086                        let name = self.expect_ident_or_string()?;
3087                        Ok(Statement::DropSubscription { name, if_exists })
3088                    }
3089                    Token::Ident(s) | Token::QuotedIdent(s)
3090                        if s.eq_ignore_ascii_case("user") || s.eq_ignore_ascii_case("role") =>
3091                    {
3092                        self.advance();
3093                        // v7.39 (read01 round 58) — DROP ROLE is DROP USER: a
3094                        // login user IS a role in PG, and SPG's store holds
3095                        // both. `IF EXISTS` is accepted on either spelling.
3096                        let if_exists = self.consume_if_exists();
3097                        let name = self.expect_ident_or_string()?;
3098                        Ok(Statement::DropUser { name, if_exists })
3099                    }
3100                    // v7.39 (round 806) — DROP DATABASE [IF EXISTS] <name>.
3101                    // CREATE DATABASE has parsed since v7.14 and this did
3102                    // not, so `DROP DATABASE IF EXISTS x` — what every
3103                    // teardown script and pg_dumpall preamble opens with —
3104                    // came back as a syntax error, which IF EXISTS cannot
3105                    // soften. The name is carried so the engine can answer
3106                    // the way PG does; PG never lets this succeed on a
3107                    // single-database server, since the name is either
3108                    // unknown ("database … does not exist", or a notice
3109                    // under IF EXISTS) or the one you are connected to
3110                    // ("cannot drop the currently open database").
3111                    Token::Ident(s) | Token::QuotedIdent(s)
3112                        if s.eq_ignore_ascii_case("database") =>
3113                    {
3114                        self.advance();
3115                        let if_exists = self.consume_if_exists();
3116                        let name = self.expect_ident_or_string()?;
3117                        self.consume_until_statement_boundary();
3118                        Ok(Statement::DropDatabase { name, if_exists })
3119                    }
3120                    // v7.12.4 — DROP TRIGGER [IF EXISTS] name ON table.
3121                    Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("trigger") => {
3122                        self.advance();
3123                        let if_exists = self.consume_if_exists();
3124                        let name = self.expect_ident_like()?;
3125                        // ON <table>
3126                        if !matches!(self.peek(), Token::On) {
3127                            return Err(self.err(alloc::format!(
3128                                "expected ON <table> after DROP TRIGGER {name:?}, got {:?}",
3129                                self.peek()
3130                            )));
3131                        }
3132                        self.advance();
3133                        let table = self.expect_ident_like()?;
3134                        Ok(Statement::DropTrigger {
3135                            name,
3136                            table,
3137                            if_exists,
3138                        })
3139                    }
3140                    // v7.39 (round 139) — DROP RULE [IF EXISTS] name ON table
3141                    // [CASCADE|RESTRICT]. Mirrors DROP TRIGGER's shape.
3142                    Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("rule") => {
3143                        self.advance();
3144                        let if_exists = self.consume_if_exists();
3145                        let name = self.expect_ident_like()?;
3146                        if !matches!(self.peek(), Token::On) {
3147                            return Err(self.err(alloc::format!(
3148                                "expected ON <table> after DROP RULE {name:?}, got {:?}",
3149                                self.peek()
3150                            )));
3151                        }
3152                        self.advance();
3153                        let table = self.expect_ident_like()?;
3154                        // Optional CASCADE / RESTRICT — accepted, no effect.
3155                        self.consume_until_statement_boundary();
3156                        Ok(Statement::DropRule {
3157                            name,
3158                            table,
3159                            if_exists,
3160                        })
3161                    }
3162                    // v7.12.4 — DROP FUNCTION [IF EXISTS] name [(args)].
3163                    // v7.12.4 ignores any optional arg-list (signature-
3164                    // based overload disambiguation lands in v7.12.5+).
3165                    Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("function") => {
3166                        self.advance();
3167                        let if_exists = self.consume_if_exists();
3168                        let name = self.expect_ident_like()?;
3169                        // v7.39 (read01 round 62) — the argument list identifies
3170                        // WHICH overload to drop, so it is captured, not
3171                        // discarded. `DROP FUNCTION f` (no list) is legal when
3172                        // the name is unambiguous; the engine enforces that.
3173                        let args = if matches!(self.peek(), Token::LParen) {
3174                            Some(self.parse_function_signature_types()?)
3175                        } else {
3176                            None
3177                        };
3178                        // v7.39 (round 621) — the `CASCADE` / `RESTRICT`
3179                        // trailer, which `DROP TABLE` and `DROP INDEX` have
3180                        // accepted since v7.14 and this one refused outright.
3181                        // pg_dump writes it, so refusing was a parse error in
3182                        // the middle of a restore. SPG drops the function
3183                        // either way — it tracks no dependents to cascade to —
3184                        // which is the same reading the other two give it.
3185                        self.consume_drop_behaviour();
3186                        Ok(Statement::DropFunction {
3187                            name,
3188                            args,
3189                            if_exists,
3190                        })
3191                    }
3192                    // v7.14.0 — DROP TABLE [IF EXISTS] name [, name…]
3193                    // [CASCADE|RESTRICT]. pg_dump and mysqldump both
3194                    // emit DROP TABLE IF EXISTS at the head of every
3195                    // CREATE TABLE block so re-importing a dump
3196                    // overwrites prior state. SPG accepts and removes
3197                    // matching tables; CASCADE/RESTRICT trailers
3198                    // accepted silently.
3199                    Token::Table => self.parse_drop_table_after_keyword(),
3200                    // v7.14.0 — DROP INDEX [IF EXISTS] name
3201                    // [CASCADE|RESTRICT]. PG / mysqldump emit this
3202                    // for partial-index renames and pgvector
3203                    // migrations. SPG removes the matching index;
3204                    // IF EXISTS makes the drop idempotent.
3205                    Token::Index => {
3206                        self.advance();
3207                        let if_exists_at = self.pos;
3208                        let if_exists = self.consume_if_exists();
3209                        let name = self.expect_ident_like()?;
3210                        // v7.39.7 — MySQL's own spelling, which SPG
3211                        // refused.
3212                        //
3213                        // `DROP INDEX i ON t` is how MySQL drops an
3214                        // index; its names live inside a table, so the
3215                        // statement names the table. Measured against
3216                        // MySQL 9.7.2: the form above works, and the
3217                        // bare `DROP INDEX i` PostgreSQL uses is a 1064
3218                        // there. SPG had it exactly backwards on the
3219                        // MySQL wire — the bare form accepted, MySQL's
3220                        // own a syntax error — so a migration that drops
3221                        // an index failed against the drop-in and not
3222                        // against the thing it replaces.
3223                        let table = if matches!(self.peek(), Token::On) {
3224                            self.advance();
3225                            Some(self.expect_ident_like()?)
3226                        } else {
3227                            None
3228                        };
3229                        if self.mysql_dialect {
3230                            // MySQL has no `IF EXISTS` here either:
3231                            // `DROP INDEX IF EXISTS i ON t` is a 1064.
3232                            if if_exists {
3233                                return Err(self.err_at(
3234                                    if_exists_at,
3235                                    "MySQL has no IF EXISTS on DROP INDEX".into(),
3236                                ));
3237                            }
3238                            if table.is_none() {
3239                                return Err(self.err("expected ON after the index name".into()));
3240                            }
3241                        }
3242                        if matches!(
3243                            self.peek(),
3244                            Token::Ident(s) if s.eq_ignore_ascii_case("cascade")
3245                                || s.eq_ignore_ascii_case("restrict")
3246                        ) {
3247                            self.advance();
3248                        }
3249                        Ok(Statement::DropIndex {
3250                            name,
3251                            if_exists,
3252                            table,
3253                        })
3254                    }
3255                    // v7.14.0 — DROP SCHEMA [IF EXISTS] name
3256                    // [CASCADE|RESTRICT]. SPG is single-database;
3257                    // v7.17.0 Phase 1.6 — DROP SCHEMA [IF EXISTS]
3258                    // name [, name…] [CASCADE | RESTRICT]. Real
3259                    // unregister (was silent no-op pre-v7.17).
3260                    Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("schema") => {
3261                        self.advance();
3262                        let if_exists = self.consume_if_exists();
3263                        let mut names = vec![self.expect_ident_like()?];
3264                        while matches!(self.peek(), Token::Comma) {
3265                            self.advance();
3266                            names.push(self.expect_ident_like()?);
3267                        }
3268                        if matches!(
3269                            self.peek(),
3270                            Token::Ident(s) if s.eq_ignore_ascii_case("cascade")
3271                                || s.eq_ignore_ascii_case("restrict")
3272                        ) {
3273                            self.advance();
3274                        }
3275                        Ok(Statement::DropSchema { names, if_exists })
3276                    }
3277                    // v7.17.0 Phase 1.4 — DROP TYPE [IF EXISTS]
3278                    // name [, name…] [CASCADE|RESTRICT].
3279                    Token::Ident(s) | Token::QuotedIdent(s)
3280                        if s.eq_ignore_ascii_case("type") =>
3281                    {
3282                        self.advance();
3283                        let if_exists = self.consume_if_exists();
3284                        let mut names = vec![self.expect_ident_like()?];
3285                        while matches!(self.peek(), Token::Comma) {
3286                            self.advance();
3287                            names.push(self.expect_ident_like()?);
3288                        }
3289                        if matches!(
3290                            self.peek(),
3291                            Token::Ident(s) if s.eq_ignore_ascii_case("cascade")
3292                                || s.eq_ignore_ascii_case("restrict")
3293                        ) {
3294                            self.advance();
3295                        }
3296                        Ok(Statement::DropType { names, if_exists })
3297                    }
3298                    // v7.17.0 Phase 1.5 — DROP DOMAIN [IF EXISTS]
3299                    // name [, name…] [CASCADE|RESTRICT].
3300                    Token::Ident(s) | Token::QuotedIdent(s)
3301                        if s.eq_ignore_ascii_case("domain") =>
3302                    {
3303                        self.advance();
3304                        let if_exists = self.consume_if_exists();
3305                        let mut names = vec![self.expect_ident_like()?];
3306                        while matches!(self.peek(), Token::Comma) {
3307                            self.advance();
3308                            names.push(self.expect_ident_like()?);
3309                        }
3310                        if matches!(
3311                            self.peek(),
3312                            Token::Ident(s) if s.eq_ignore_ascii_case("cascade")
3313                                || s.eq_ignore_ascii_case("restrict")
3314                        ) {
3315                            self.advance();
3316                        }
3317                        Ok(Statement::DropDomain { names, if_exists })
3318                    }
3319                    // v7.17.0 Phase 1.3 — DROP MATERIALIZED VIEW
3320                    // [IF EXISTS] name [, name…] [CASCADE|RESTRICT].
3321                    Token::Ident(s) | Token::QuotedIdent(s)
3322                        if s.eq_ignore_ascii_case("materialized") =>
3323                    {
3324                        self.advance();
3325                        let nxt = self.peek().clone();
3326                        if !matches!(&nxt, Token::Ident(s2) | Token::QuotedIdent(s2) if s2.eq_ignore_ascii_case("view"))
3327                        {
3328                            return Err(self.err(alloc::format!(
3329                                "expected VIEW after DROP MATERIALIZED, got {nxt:?}"
3330                            )));
3331                        }
3332                        self.advance();
3333                        let if_exists = self.consume_if_exists();
3334                        let mut names = vec![self.expect_ident_like()?];
3335                        while matches!(self.peek(), Token::Comma) {
3336                            self.advance();
3337                            names.push(self.expect_ident_like()?);
3338                        }
3339                        if matches!(
3340                            self.peek(),
3341                            Token::Ident(s) if s.eq_ignore_ascii_case("cascade")
3342                                || s.eq_ignore_ascii_case("restrict")
3343                        ) {
3344                            self.advance();
3345                        }
3346                        Ok(Statement::DropMaterializedView { names, if_exists })
3347                    }
3348                    // v7.17.0 Phase 1.2 — DROP VIEW [IF EXISTS]
3349                    // name [, name…] [CASCADE|RESTRICT].
3350                    Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("view") => {
3351                        self.advance();
3352                        let if_exists = self.consume_if_exists();
3353                        let mut names = vec![self.expect_ident_like()?];
3354                        while matches!(self.peek(), Token::Comma) {
3355                            self.advance();
3356                            names.push(self.expect_ident_like()?);
3357                        }
3358                        if matches!(
3359                            self.peek(),
3360                            Token::Ident(s) if s.eq_ignore_ascii_case("cascade")
3361                                || s.eq_ignore_ascii_case("restrict")
3362                        ) {
3363                            self.advance();
3364                        }
3365                        Ok(Statement::DropView { names, if_exists })
3366                    }
3367                    // v7.17.0 — DROP SEQUENCE [IF EXISTS] name [,name…]
3368                    // [CASCADE|RESTRICT]. Real removal from catalog
3369                    // (was a silent no-op pre-v7.17).
3370                    Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("sequence") => {
3371                        self.advance();
3372                        let if_exists = self.consume_if_exists();
3373                        let mut names = vec![self.expect_ident_like()?];
3374                        while matches!(self.peek(), Token::Comma) {
3375                            self.advance();
3376                            names.push(self.expect_ident_like()?);
3377                        }
3378                        if matches!(
3379                            self.peek(),
3380                            Token::Ident(s) if s.eq_ignore_ascii_case("cascade")
3381                                || s.eq_ignore_ascii_case("restrict")
3382                        ) {
3383                            self.advance();
3384                        }
3385                        Ok(Statement::DropSequence { names, if_exists })
3386                    }
3387                    // v7.39 (RLS) — DROP POLICY [IF EXISTS] name ON table.
3388                    Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("policy") => {
3389                        self.advance();
3390                        self.parse_drop_policy_after_keyword()
3391                    }
3392                    // v7.37.17 (17.6 siblings) — DROP <target> for
3393                    // targets SPG doesn't natively track. pg_dump
3394                    // emits DROP EXTENSION / DROP TYPE / DROP DOMAIN
3395                    // / DROP AGGREGATE / DROP OPERATOR / DROP CAST /
3396                    // DROP COLLATION / DROP LANGUAGE / DROP CONVERSION
3397                    // / DROP TEXT SEARCH / DROP FOREIGN * / DROP
3398                    // SERVER / DROP MATERIALIZED VIEW / DROP EVENT
3399                    // TRIGGER / DROP TABLESPACE / DROP RULE / DROP
3400                    // POLICY / DROP LARGE OBJECT / DROP ROLE / DROP
3401                    // ACCESS METHOD / DROP OPERATOR CLASS/FAMILY /
3402                    // etc. — accept + Empty-return so pg_dump tails
3403                    // load through. Materialized-view drop dispatches
3404                    // to the existing DropTable path when the token
3405                    // is Materialized-View-shaped (elsewhere in
3406                    // this parser).
3407                    Token::Ident(s) | Token::QuotedIdent(s)
3408                        if s.eq_ignore_ascii_case("text")
3409                            // The DROP dispatch matches on PEEK — `text` is
3410                            // not yet consumed, so SEARCH/CONFIGURATION sit
3411                            // at pos+1/pos+2 (the round-695 trap's mirror).
3412                            && matches!(self.tokens.get(self.pos + 1), Some(Token::Ident(k)) if k.eq_ignore_ascii_case("search"))
3413                            && matches!(self.tokens.get(self.pos + 2), Some(Token::Ident(k)) if k.eq_ignore_ascii_case("configuration")) =>
3414                    {
3415                        // v7.39 (round 709) — DROP TEXT SEARCH CONFIGURATION
3416                        // validates the name; DICTIONARY / PARSER / TEMPLATE
3417                        // stay in the noise arm below.
3418                        self.advance(); // TEXT
3419                        self.advance(); // SEARCH
3420                        self.advance(); // CONFIGURATION
3421                        let if_exists = self.consume_if_exists();
3422                        let names = self.take_comma_separated_names();
3423                        self.consume_until_statement_boundary();
3424                        if if_exists {
3425                            return Ok(Statement::Empty);
3426                        }
3427                        Ok(Statement::ValidateOnly {
3428                            kind: crate::ast::ValidateOnlyKind::TsConfigName,
3429                            names,
3430                        })
3431                    }
3432                    Token::Ident(s) | Token::QuotedIdent(s)
3433                        if matches!(
3434                            s.to_ascii_lowercase().as_str(),
3435                            "type"
3436                                | "domain"
3437                                | "operator"
3438                                | "cast"
3439                                // `text` = TEXT SEARCH DICTIONARY / PARSER /
3440                                // TEMPLATE (CONFIGURATION intercepted above).
3441                                | "text"
3442                                | "materialized"
3443                                | "large"
3444                                | "role"
3445                                | "access"
3446                                | "procedure"
3447                                | "routine"
3448                        ) =>
3449                    {
3450                        self.consume_until_statement_boundary();
3451                        Ok(Statement::Empty)
3452                    }
3453                    // v7.39 (round 709) — DROP COLLATION / EVENT TRIGGER /
3454                    // TABLESPACE / TEXT SEARCH CONFIGURATION validate their
3455                    // NAME; DROP SERVER / DROP FOREIGN TABLE join the
3456                    // foreign-data warning family (round 706) so a
3457                    // CREATE→DROP sequence in a dump stays consistent.
3458                    Token::Ident(s) | Token::QuotedIdent(s)
3459                        if s.eq_ignore_ascii_case("server")
3460                            || s.eq_ignore_ascii_case("foreign") =>
3461                    {
3462                        self.advance();
3463                        self.consume_until_statement_boundary();
3464                        Ok(Statement::ValidateOnly {
3465                            kind: crate::ast::ValidateOnlyKind::ForeignInfra,
3466                            names: Vec::new(),
3467                        })
3468                    }
3469                    Token::Ident(s) | Token::QuotedIdent(s)
3470                        if s.eq_ignore_ascii_case("collation")
3471                            || s.eq_ignore_ascii_case("tablespace") =>
3472                    {
3473                        let kind = if s.eq_ignore_ascii_case("collation") {
3474                            crate::ast::ValidateOnlyKind::CollationName
3475                        } else {
3476                            crate::ast::ValidateOnlyKind::TablespaceName
3477                        };
3478                        self.advance();
3479                        let if_exists = self.consume_if_exists();
3480                        let names = self.take_comma_separated_names();
3481                        self.consume_until_statement_boundary();
3482                        if if_exists {
3483                            return Ok(Statement::Empty);
3484                        }
3485                        Ok(Statement::ValidateOnly { kind, names })
3486                    }
3487                    Token::Ident(s) | Token::QuotedIdent(s)
3488                        if s.eq_ignore_ascii_case("event") =>
3489                    {
3490                        self.advance();
3491                        if matches!(self.peek(), Token::Ident(k) if k.eq_ignore_ascii_case("trigger"))
3492                        {
3493                            self.advance();
3494                        }
3495                        let if_exists = self.consume_if_exists();
3496                        let names = self.take_comma_separated_names();
3497                        self.consume_until_statement_boundary();
3498                        if if_exists {
3499                            return Ok(Statement::Empty);
3500                        }
3501                        Ok(Statement::ValidateOnly {
3502                            kind: crate::ast::ValidateOnlyKind::EventTriggerName,
3503                            names,
3504                        })
3505                    }
3506                    // v7.39 (round 708) — DROP CONVERSION / DROP LANGUAGE
3507                    // leave the noise list; see the ValidateOnly kinds.
3508                    Token::Ident(s) | Token::QuotedIdent(s)
3509                        if s.eq_ignore_ascii_case("conversion")
3510                            || s.eq_ignore_ascii_case("language")
3511                            // `DROP PROCEDURAL LANGUAGE` puts the modifier
3512                            // FIRST — the first draft looked for it after.
3513                            || s.eq_ignore_ascii_case("procedural") =>
3514                    {
3515                        let kind = if s.eq_ignore_ascii_case("conversion") {
3516                            crate::ast::ValidateOnlyKind::ConversionName
3517                        } else {
3518                            crate::ast::ValidateOnlyKind::LanguageName
3519                        };
3520                        self.advance();
3521                        if matches!(self.peek(), Token::Ident(k) if k.eq_ignore_ascii_case("language"))
3522                        {
3523                            self.advance();
3524                        }
3525                        let if_exists = self.consume_if_exists();
3526                        let names = self.take_comma_separated_names();
3527                        self.consume_until_statement_boundary();
3528                        if if_exists {
3529                            return Ok(Statement::Empty);
3530                        }
3531                        Ok(Statement::ValidateOnly { kind, names })
3532                    }
3533                    // v7.39 (round 707) — `DROP AGGREGATE [IF EXISTS]
3534                    // name(argtypes)[, …]`. Parsed for real so the engine
3535                    // can answer as PG does; see Statement::DropAggregate.
3536                    Token::Ident(s) | Token::QuotedIdent(s)
3537                        if s.eq_ignore_ascii_case("aggregate") =>
3538                    {
3539                        self.advance();
3540                        let if_exists = self.consume_if_exists();
3541                        let mut items: Vec<(String, Option<Vec<String>>)> = Vec::new();
3542                        loop {
3543                            let name = self.expect_ident_like()?;
3544                            if !matches!(self.peek(), Token::LParen) {
3545                                return Err(self.err(alloc::format!(
3546                                    "expected argument list after DROP AGGREGATE {name}"
3547                                )));
3548                            }
3549                            self.advance();
3550                            let mut args: Vec<String> = Vec::new();
3551                            let mut star = false;
3552                            loop {
3553                                match self.peek().clone() {
3554                                    Token::RParen => {
3555                                        self.advance();
3556                                        break;
3557                                    }
3558                                    Token::Star => {
3559                                        self.advance();
3560                                        star = true;
3561                                    }
3562                                    Token::Comma => {
3563                                        self.advance();
3564                                    }
3565                                    _ => {
3566                                        // A type name may be multi-token
3567                                        // (`double precision`); glue idents
3568                                        // until , or ).
3569                                        let mut t = self.expect_ident_like()?;
3570                                        while let Token::Ident(nx) = self.peek() {
3571                                            let nx = nx.clone();
3572                                            self.advance();
3573                                            t.push(' ');
3574                                            t.push_str(&nx);
3575                                        }
3576                                        args.push(t);
3577                                    }
3578                                }
3579                            }
3580                            items.push((name, if star { None } else { Some(args) }));
3581                            if matches!(self.peek(), Token::Comma) {
3582                                self.advance();
3583                            } else {
3584                                break;
3585                            }
3586                        }
3587                        self.consume_until_statement_boundary();
3588                        Ok(Statement::DropAggregate { if_exists, items })
3589                    }
3590                    // v7.39 (round 697) — `DROP EXTENSION [IF EXISTS] <e>
3591                    // [, …] [CASCADE|RESTRICT]`. PG refuses one that is not
3592                    // installed; `IF EXISTS` is the spelling that says do
3593                    // not, and it keeps the no-op.
3594                    Token::Ident(s) | Token::QuotedIdent(s)
3595                        if s.eq_ignore_ascii_case("extension") =>
3596                    {
3597                        self.advance();
3598                        let if_exists = self.consume_if_exists();
3599                        let names = self.take_comma_separated_names();
3600                        self.consume_until_statement_boundary();
3601                        if if_exists {
3602                            return Ok(Statement::Empty);
3603                        }
3604                        Ok(Statement::ValidateOnly {
3605                            kind: crate::ast::ValidateOnlyKind::ExtensionInstalled,
3606                            names,
3607                        })
3608                    }
3609                    Token::Ident(s) | Token::QuotedIdent(s)
3610                        if s.eq_ignore_ascii_case("statistics") =>
3611                    {
3612                        self.parse_drop_statistics_after_drop()
3613                    }
3614                    other => Err(self.err(format!(
3615                        "expected TABLE / INDEX / SCHEMA / SEQUENCE / USER / PUBLICATION / \
3616                         SUBSCRIPTION / TRIGGER / FUNCTION / STATISTICS after DROP, got {other:?}"
3617                    ))),
3618                }
3619            }
3620            // v7.17.0 Phase 1.3 — REFRESH MATERIALIZED VIEW name [WITH [NO] DATA].
3621            // v7.37.19 (19.8) — `CONCURRENTLY` modifier (PG 9.4+) parsed
3622            // and accepted before the view name. SPG materialised
3623            // views re-evaluate on read (always-fresh semantics), so
3624            // the CONCURRENTLY-vs-serial distinction has no runtime
3625            // effect — the refresh body does not block readers either
3626            // way. Same accept-and-no-op pattern as DETACH PARTITION
3627            // CONCURRENTLY (16.5).
3628            Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("refresh") => {
3629                self.advance();
3630                let nxt = self.peek().clone();
3631                if !matches!(&nxt, Token::Ident(s2) | Token::QuotedIdent(s2) if s2.eq_ignore_ascii_case("materialized"))
3632                {
3633                    return Err(self.err(alloc::format!(
3634                        "expected MATERIALIZED after REFRESH, got {nxt:?}"
3635                    )));
3636                }
3637                self.advance();
3638                let nxt2 = self.peek().clone();
3639                if !matches!(&nxt2, Token::Ident(s2) | Token::QuotedIdent(s2) if s2.eq_ignore_ascii_case("view"))
3640                {
3641                    return Err(self.err(alloc::format!(
3642                        "expected VIEW after REFRESH MATERIALIZED, got {nxt2:?}"
3643                    )));
3644                }
3645                self.advance();
3646                // Optional CONCURRENTLY noise word — consumed without
3647                // changing semantics.
3648                if matches!(self.peek(), Token::Ident(s2) | Token::QuotedIdent(s2) if s2.eq_ignore_ascii_case("concurrently"))
3649                {
3650                    self.advance();
3651                }
3652                let name = self.expect_ident_like()?;
3653                let with_data = self.parse_optional_with_data(true)?;
3654                Ok(Statement::RefreshMaterializedView { name, with_data })
3655            }
3656            Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("update") => {
3657                self.advance();
3658                self.parse_update_after_keyword()
3659            }
3660            // v7.37.17 (17.6 sibling) — TRUNCATE [TABLE] [ONLY]
3661            // <name> [, ...] [RESTART IDENTITY | CONTINUE IDENTITY]
3662            // [CASCADE | RESTRICT]. Clears every row from each named
3663            // table. Parses at the top level; the engine dispatcher
3664            // walks Statement::Truncate.
3665            // v7.39.9 — MySQL's top-level `RENAME TABLE a TO b [, c TO d]`.
3666            //
3667            // PostgreSQL renames a table through `ALTER TABLE … RENAME
3668            // TO`, which SPG already had, so this spelling answered 1064
3669            // — and it is what a MySQL migration writes. Measured on
3670            // 9.7.2: several pairs in one statement are accepted, and
3671            // renaming onto a name that exists is 1050.
3672            Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("rename") => {
3673                self.advance();
3674                if matches!(self.peek(), Token::Table) {
3675                    self.advance();
3676                }
3677                let mut pairs: Vec<(String, String)> = Vec::new();
3678                loop {
3679                    let from = self.expect_ident_like()?;
3680                    if matches!(self.peek(), Token::To) {
3681                        self.advance();
3682                    } else {
3683                        self.expect_keyword_ident("to")?;
3684                    }
3685                    let to = self.expect_ident_like()?;
3686                    pairs.push((from, to));
3687                    if matches!(self.peek(), Token::Comma) {
3688                        self.advance();
3689                    } else {
3690                        break;
3691                    }
3692                }
3693                Ok(Statement::RenameTables(pairs))
3694            }
3695            Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("truncate") => {
3696                self.advance();
3697                // Optional TABLE noise word — PG accepts both the reserved
3698                // token and the bare identifier spelling.
3699                if matches!(self.peek(), Token::Table)
3700                    || matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("table"))
3701                {
3702                    self.advance();
3703                }
3704                // v7.39 (round 647) — `TRUNCATE ONLY t` is carried now,
3705                // not absorbed. The lookahead keeps a table genuinely
3706                // called `only` working: the keyword is a keyword only
3707                // when a name follows it.
3708                let only = matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s)
3709                    if s.eq_ignore_ascii_case("only"))
3710                    && matches!(
3711                        self.tokens.get(self.pos + 1),
3712                        Some(Token::Ident(_) | Token::QuotedIdent(_))
3713                    );
3714                if only {
3715                    self.advance();
3716                }
3717                // Table names (comma-separated).
3718                let mut tables = Vec::new();
3719                loop {
3720                    tables.push(self.expect_ident_like()?);
3721                    if matches!(self.peek(), Token::Comma) {
3722                        self.advance();
3723                        continue;
3724                    }
3725                    break;
3726                }
3727                // Optional RESTART IDENTITY / CONTINUE IDENTITY.
3728                let mut restart_identity = false;
3729                if matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("restart"))
3730                {
3731                    self.advance();
3732                    if matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("identity"))
3733                    {
3734                        self.advance();
3735                        restart_identity = true;
3736                    }
3737                } else if matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("continue"))
3738                {
3739                    self.advance();
3740                    if matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("identity"))
3741                    {
3742                        self.advance();
3743                    }
3744                }
3745                // Optional CASCADE / RESTRICT.
3746                let mut cascade = false;
3747                if matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("cascade"))
3748                {
3749                    self.advance();
3750                    cascade = true;
3751                } else if matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("restrict"))
3752                {
3753                    self.advance();
3754                }
3755                Ok(Statement::Truncate {
3756                    tables,
3757                    restart_identity,
3758                    cascade,
3759                    only,
3760                })
3761            }
3762            // v7.37.17 (17.6 sibling) — REINDEX [(OPTION [, ...])]
3763            // [CONCURRENTLY] { INDEX | TABLE | SCHEMA | DATABASE |
3764            // SYSTEM } [IF EXISTS] <name>. SPG rebuilds indexes as
3765            // rows change so the index tree is always up-to-date;
3766            // REINDEX is a strict no-op. Accept the whole statement
3767            // shape to boundary for pg_dump round-trip compatibility.
3768            Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("reindex") => {
3769                // v7.39 (round 535) — the target is CARRIED now. SPG has no
3770                // index bloat to rebuild, so the work stays a no-op, but PG
3771                // validates what it was pointed at and this swallowed the
3772                // name at parse time — `REINDEX TABLE typo` reported
3773                // success. Measured on PG18: INDEX / TABLE name a relation,
3774                // SCHEMA a schema, SYSTEM nothing.
3775                self.advance();
3776                self.parse_reindex_tail()
3777            }
3778            // v7.37.17 (17.6 sibling) — VACUUM [(OPTION [, ...])]
3779            // [FULL] [FREEZE] [VERBOSE] [ANALYZE] [<table> [(cols)]].
3780            // SPG has no MVCC bloat today (Phase D visibility map
3781            // queues with v7.38); the freezer collapses hot-tier
3782            // rows into cold segments automatically. VACUUM is a
3783            // no-op — pg_dump maintenance scripts and Discourse's
3784            // periodic-maintenance path both emit it.
3785            // v7.39 (round 169) — VACUUM is REAL now: with the in-place
3786            // MVCC gate ON (v7.37.15 flip), tombstoned versions are
3787            // actual bloat, so the pre-MVCC accept-and-ignore posture
3788            // became a silent no-op on a customer's manual reclaim.
3789            // Grammar: VACUUM [(opts)] [FULL] [FREEZE] [VERBOSE]
3790            // [ANALYZE] [<table> [(cols)]] — option words are absorbed,
3791            // ANALYZE is captured, the optional table name is captured.
3792            Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("vacuum") => {
3793                self.advance();
3794                // Parenthesised option list: absorb it.
3795                if matches!(self.peek(), Token::LParen) {
3796                    let mut depth = 0usize;
3797                    loop {
3798                        match self.advance() {
3799                            Token::LParen => depth += 1,
3800                            Token::RParen => {
3801                                depth -= 1;
3802                                if depth == 0 {
3803                                    break;
3804                                }
3805                            }
3806                            Token::Eof => break,
3807                            _ => {}
3808                        }
3809                    }
3810                }
3811                let mut analyze = false;
3812                let mut table: Option<String> = None;
3813                loop {
3814                    match self.peek() {
3815                        // v7.39 (round 535) — `FULL` lexes as a keyword, not
3816                        // an identifier, so the loop below broke out on it and
3817                        // dropped the table name: `VACUUM FULL nosuch` was
3818                        // accepted where `VACUUM nosuch` was refused.
3819                        Token::Full => {
3820                            self.advance();
3821                        }
3822                        Token::Ident(w) | Token::QuotedIdent(w) => {
3823                            let wl = w.to_ascii_lowercase();
3824                            match wl.as_str() {
3825                                "full" | "freeze" | "verbose" => {
3826                                    self.advance();
3827                                }
3828                                "analyze" | "analyse" => {
3829                                    analyze = true;
3830                                    self.advance();
3831                                }
3832                                _ => {
3833                                    table = Some(self.expect_ident_like()?);
3834                                    break;
3835                                }
3836                            }
3837                        }
3838                        _ => break,
3839                    }
3840                }
3841                // Optional trailing column list / anything else to the
3842                // statement boundary (PG accepts per-column ANALYZE).
3843                self.consume_until_statement_boundary();
3844                Ok(Statement::Vacuum { table, analyze })
3845            }
3846            // v7.37.17 (17.6 sibling) — CLUSTER [VERBOSE] <table>
3847            // [USING <index>] / CLUSTER (VERBOSE) <table> USING
3848            // <index>. PG stores rows in physical order matching
3849            // an index; SPG's hot-tier is append-only + cold-tier
3850            // is segment-frozen, so clustering has no persistent
3851            // effect. Accept-and-no-op for pg_dump compat.
3852            Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("cluster") => {
3853                // v7.39 (round 535) — same as REINDEX above: the relation is
3854                // carried so the engine can refuse one that does not exist.
3855                // A bare `CLUSTER [VERBOSE]` names nothing and is accepted.
3856                self.advance();
3857                self.parse_cluster_tail()
3858            }
3859            // v7.39 (round 222) — LISTEN / NOTIFY / UNLISTEN with real
3860            // delivery (was accept-and-drop since v7.37.17). NOTIFY takes an
3861            // optional string payload; UNLISTEN takes a channel or `*`.
3862            Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("listen") => {
3863                self.advance();
3864                let ch = match self.advance() {
3865                    Token::Ident(c) | Token::QuotedIdent(c) => c,
3866                    other => {
3867                        return Err(self.err(format!(
3868                            "expected channel name after LISTEN, got {other:?}"
3869                        )));
3870                    }
3871                };
3872                Ok(Statement::Listen(ch))
3873            }
3874            Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("notify") => {
3875                self.advance();
3876                let channel = match self.advance() {
3877                    Token::Ident(c) | Token::QuotedIdent(c) => c,
3878                    other => {
3879                        return Err(self.err(format!(
3880                            "expected channel name after NOTIFY, got {other:?}"
3881                        )));
3882                    }
3883                };
3884                let payload = if matches!(self.peek(), Token::Comma) {
3885                    self.advance();
3886                    match self.advance() {
3887                        Token::String(p) => Some(p),
3888                        other => {
3889                            return Err(self.err(format!(
3890                                "expected string payload after NOTIFY <channel>, got {other:?}"
3891                            )));
3892                        }
3893                    }
3894                } else {
3895                    None
3896                };
3897                Ok(Statement::Notify { channel, payload })
3898            }
3899            Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("unlisten") => {
3900                self.advance();
3901                match self.advance() {
3902                    Token::Star => Ok(Statement::Unlisten(None)),
3903                    Token::Ident(c) | Token::QuotedIdent(c) => Ok(Statement::Unlisten(Some(c))),
3904                    other => Err(self.err(format!(
3905                        "expected channel name or * after UNLISTEN, got {other:?}"
3906                    ))),
3907                }
3908            }
3909            // v7.37.17 (17.6 sibling) — LOCK [TABLE] [ONLY] <table>
3910            // [IN <mode> MODE] [NOWAIT]. SPG's engine holds a
3911            // process-wide write lock today; explicit LOCK has no
3912            // effect. Accept-and-no-op for pg_dump / migration
3913            // compat.
3914            Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("lock") => {
3915                self.advance();
3916                // v7.39 (round 696) — the LOCK still has no effect (SPG's
3917                // engine holds a process-wide write lock), but the TABLE
3918                // NAME is now carried out so the engine can refuse one that
3919                // does not exist, as PG18 does. MySQL's `LOCK TABLES …
3920                // READ|WRITE` is a different statement with the same first
3921                // word; it keeps the old no-op, because a MySQL dump's
3922                // bracket names tables it is about to create.
3923                let mysql_tables = matches!(self.peek(), Token::Ident(k)
3924                    if k.eq_ignore_ascii_case("tables"));
3925                if mysql_tables {
3926                    self.consume_until_statement_boundary();
3927                    return Ok(Statement::Empty);
3928                }
3929                if matches!(self.peek(), Token::Table) {
3930                    self.advance();
3931                }
3932                let names = self.take_comma_separated_names();
3933                self.consume_until_statement_boundary();
3934                Ok(Statement::ValidateOnly {
3935                    kind: crate::ast::ValidateOnlyKind::LockTable,
3936                    names,
3937                })
3938            }
3939            // v7.37.17 (17.6 sibling) — CHECKPOINT. Forces a WAL
3940            // durability marker + snapshot in PG. SPG has WAL
3941            // checkpointing on a byte / time schedule (v7.37.10
3942            // 60s / 4 MiB defaults). The bare statement parses to
3943            // `Statement::Empty` here (the no_std engine owns no
3944            // WAL / snapshot); v7.37 Epic Du wires the HOST
3945            // (embedded `Database::execute_buffered`, via
3946            // `sql_is_checkpoint`) to force an immediate synchronous
3947            // checkpoint through `Database::checkpoint` — a real
3948            // durability barrier, matching PG.
3949            Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("checkpoint") => {
3950                self.advance();
3951                self.consume_until_statement_boundary();
3952                Ok(Statement::Empty)
3953            }
3954            Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("delete") => {
3955                self.advance();
3956                self.parse_delete_after_keyword()
3957            }
3958            // v6.0.4: ALTER INDEX <name> REBUILD [WITH (encoding = ...)].
3959            // ALTER is not a reserved keyword in the lexer — handled
3960            // as a bare ident here.
3961            Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("alter") => {
3962                self.advance();
3963                self.parse_alter_after_keyword()
3964            }
3965            // v6.1.7: WAIT FOR WAL POSITION <pos> [WITH TIMEOUT <ms>].
3966            // WAIT / POSITION / TIMEOUT are bare idents — no lexer
3967            // additions needed.
3968            Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("wait") => {
3969                self.advance();
3970                self.parse_wait_after_keyword()
3971            }
3972            // v6.2.0: ANALYZE [<table>]. ANALYZE is a bare ident.
3973            // Bare ANALYZE → analyse every user table; ANALYZE
3974            // <name> → re-stats one. The argument is an optional
3975            // ident (or quoted ident); anything else is a parse
3976            // error.
3977            // v6.7.3 — `COMPACT COLD SEGMENTS`. No arguments, no
3978            // `WHERE` filter (carved out per V6_7_DESIGN.md
3979            // STABILITY). Lex order: identifier "compact" → "cold"
3980            // → "segments". Anything else after `COMPACT` is a
3981            // parse error.
3982            Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("compact") => {
3983                self.advance();
3984                let next = self.peek().clone();
3985                let cold = match next {
3986                    Token::Ident(s) | Token::QuotedIdent(s) => s,
3987                    _ => {
3988                        return Err(
3989                            self.err(format!("expected COLD after COMPACT, got {:?}", self.peek()))
3990                        );
3991                    }
3992                };
3993                if !cold.eq_ignore_ascii_case("cold") {
3994                    return Err(self.err(format!("expected COLD after COMPACT, got {cold:?}")));
3995                }
3996                self.advance();
3997                let next = self.peek().clone();
3998                let segments = match next {
3999                    Token::Ident(s) | Token::QuotedIdent(s) => s,
4000                    _ => {
4001                        return Err(self.err(format!(
4002                            "expected SEGMENTS after COMPACT COLD, got {:?}",
4003                            self.peek()
4004                        )));
4005                    }
4006                };
4007                if !segments.eq_ignore_ascii_case("segments") {
4008                    return Err(self.err(format!(
4009                        "expected SEGMENTS after COMPACT COLD, got {segments:?}"
4010                    )));
4011                }
4012                self.advance();
4013                Ok(Statement::CompactColdSegments)
4014            }
4015            // v7.17.0 Phase 3.P0-42 — SQL:2003 / PG 15+ MERGE.
4016            // Parsed as a case-insensitive identifier since MERGE
4017            // isn't a reserved lexer keyword (collides with the
4018            // mysqldump `ALGORITHM = MERGE` view clause if it
4019            // were); the inner parser drives the rest of the
4020            // surface (USING / ON / WHEN [NOT] MATCHED / THEN).
4021            Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("merge") => {
4022                self.advance();
4023                self.parse_merge_after_keyword()
4024            }
4025            Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("analyze") => {
4026                self.advance();
4027                // v7.39.9 — MySQL spells it `ANALYZE TABLE t`. The
4028                // keyword is noise to the parse; what differs is the
4029                // ANSWER, which MySQL returns as a result set — see the
4030                // executor.
4031                let mysql_table_kw = matches!(self.peek(), Token::Table);
4032                if mysql_table_kw {
4033                    self.advance();
4034                }
4035                let target = match self.peek() {
4036                    Token::Eof | Token::Semicolon => None,
4037                    Token::Ident(_) | Token::QuotedIdent(_) => {
4038                        Some(self.expect_ident_like()?)
4039                    }
4040                    other => {
4041                        return Err(self.err(format!(
4042                            "expected table name or end of statement after ANALYZE, got {other:?}"
4043                        )));
4044                    }
4045                };
4046                // v7.39 (round 776, F31 J7) — the per-column form
4047                // (`ANALYZE t (x, y)`, PG-accepted) was a syntax error
4048                // here while the VACUUM arm already consumed it; SPG
4049                // analyzes whole tables, so the list parses and is
4050                // accepted like the VACUUM path's.
4051                if target.is_some() && matches!(self.peek(), Token::LParen) {
4052                    self.advance();
4053                    loop {
4054                        let _ = self.expect_ident_like()?;
4055                        match self.peek() {
4056                            Token::Comma => {
4057                                self.advance();
4058                            }
4059                            Token::RParen => {
4060                                self.advance();
4061                                break;
4062                            }
4063                            other => {
4064                                return Err(self.err(format!(
4065                                    "expected ',' or ')' in ANALYZE column list, got {other:?}"
4066                                )));
4067                            }
4068                        }
4069                    }
4070                }
4071                Ok(Statement::Analyze(target))
4072            }
4073            // v7.12.1 — `SET <name> [TO|=] <value>`. The
4074            // `default_text_search_config` parameter is consumed
4075            // by the FTS function dispatcher; other parameter
4076            // names are recorded but treated as a no-op so PG
4077            // dump output loads.
4078            Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("set") => {
4079                self.advance();
4080                // PG allows `SET LOCAL` / `SET SESSION` qualifiers; MySQL
4081                // adds `SET GLOBAL` too (and the alias `SET @@global.name =
4082                // …` which the SessionVar path handles). `LOCAL` is the only
4083                // one that changes semantics — it scopes the change to the
4084                // current transaction — so capture it; SESSION / GLOBAL are
4085                // accepted and treated as the default session scope.
4086                let mut set_local = false;
4087                if let Token::Ident(s) | Token::QuotedIdent(s) = self.peek() {
4088                    let q = s.to_ascii_lowercase();
4089                    if q == "local" || q == "session" || q == "global" {
4090                        set_local = q == "local";
4091                        self.advance();
4092                    }
4093                }
4094                // 7.38.1 S5.2 — PG `SET [SESSION] AUTHORIZATION
4095                // { DEFAULT | <role> }`. pg_dump's ACL section switches
4096                // to the object owner with it. SPG maps it onto the
4097                // session-role machinery (recorded delta RD-10: PG moves
4098                // session_user too; SPG moves the effective role).
4099                if matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s)
4100                    if s.eq_ignore_ascii_case("authorization"))
4101                {
4102                    self.advance(); // AUTHORIZATION
4103                    let role = match self.peek().clone() {
4104                        Token::Default => {
4105                            self.advance();
4106                            None
4107                        }
4108                        Token::String(s) | Token::Ident(s) | Token::QuotedIdent(s) => {
4109                            self.advance();
4110                            Some(s)
4111                        }
4112                        _ => None,
4113                    };
4114                    return Ok(Statement::SetRole(role));
4115                }
4116                // v7.14.0 — MySQL `SET NAMES <charset> [COLLATE
4117                // <collation>]` — change the connection client
4118                // charset. SPG stores UTF-8 always and orders
4119                // bytewise; accept as a no-op.
4120                if matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("names"))
4121                {
4122                    self.advance();
4123                    // v7.39 — this used to parse the clause and throw it
4124                    // away ("SPG stores UTF-8 always and orders
4125                    // bytewise; accept as a no-op"). That sentence
4126                    // stopped being true when collations arrived, and
4127                    // once `collation_connection` began driving literal
4128                    // comparison, dropping the COLLATE clause became a
4129                    // silently wrong answer: `SET NAMES utf8mb4 COLLATE
4130                    // utf8mb4_general_ci` reported back
4131                    // `utf8mb4_0900_ai_ci` and compared as NO PAD.
4132                    //
4133                    // The charset name is emitted as `names` and the
4134                    // ENGINE expands it, because which collation a
4135                    // charset defaults to is MySQL semantics and belongs
4136                    // beside the rest of them, not in the parser.
4137                    let mut pairs = alloc::vec::Vec::new();
4138                    if matches!(
4139                        self.peek(),
4140                        Token::Ident(_) | Token::QuotedIdent(_) | Token::String(_)
4141                    ) {
4142                        let charset = match self.advance() {
4143                            Token::Ident(s) | Token::QuotedIdent(s) | Token::String(s) => s,
4144                            _ => unreachable!("peeked an ident-or-string"),
4145                        };
4146                        pairs.push((String::from("names"), crate::ast::SetValue::Ident(charset)));
4147                    }
4148                    // Optional `COLLATE <name>` — emitted AFTER `names`
4149                    // so it overrides the charset's default, which is
4150                    // what MySQL does.
4151                    if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("collate"))
4152                    {
4153                        self.advance();
4154                        if matches!(
4155                            self.peek(),
4156                            Token::Ident(_) | Token::QuotedIdent(_) | Token::String(_)
4157                        ) {
4158                            let coll = match self.advance() {
4159                                Token::Ident(s) | Token::QuotedIdent(s) | Token::String(s) => s,
4160                                _ => unreachable!("peeked an ident-or-string"),
4161                            };
4162                            pairs.push((
4163                                String::from("collation_connection"),
4164                                crate::ast::SetValue::Ident(coll),
4165                            ));
4166                        }
4167                    }
4168                    if pairs.is_empty() {
4169                        return Ok(Statement::Empty);
4170                    }
4171                    return Ok(Statement::SetParameterList(pairs));
4172                }
4173                // v7.37.17 (17.6 sibling) — PG `SET ROLE
4174                // { NONE | DEFAULT | <role_name> }`. pg_dump preamble
4175                // uses this to switch to the object owner before
4176                // recreating tables. SPG has no role system so this
4177                // is a no-op.
4178                if matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("role"))
4179                {
4180                    self.advance(); // ROLE
4181                    // v7.39 (RLS) — real session-role switch. NONE / DEFAULT
4182                    // reset to the login identity; a name / string sets the
4183                    // effective role that drives current_user + RLS.
4184                    let role = match self.peek().clone() {
4185                        Token::Default => {
4186                            self.advance();
4187                            None
4188                        }
4189                        Token::Ident(s) | Token::QuotedIdent(s)
4190                            if s.eq_ignore_ascii_case("none") =>
4191                        {
4192                            self.advance();
4193                            None
4194                        }
4195                        Token::String(s) | Token::Ident(s) | Token::QuotedIdent(s) => {
4196                            self.advance();
4197                            Some(s)
4198                        }
4199                        _ => None,
4200                    };
4201                    return Ok(Statement::SetRole(role));
4202                }
4203                // v7.37.17 (17.6 sibling) — PG `SET SESSION
4204                // CHARACTERISTICS AS TRANSACTION <mode>` (per PG
4205                // ISO SQL surface). pg_dump prepends this to fix
4206                // the isolation level for the restore session. SPG
4207                // defaults to READ COMMITTED and doesn't yet honor
4208                // session-set isolation across statements — accept
4209                // and no-op. SET (LOCAL/SESSION) TRANSACTION AS ...
4210                // per-tx form is handled elsewhere.
4211                if matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("characteristics"))
4212                {
4213                    self.advance(); // CHARACTERISTICS
4214                    if matches!(self.peek(), Token::As) {
4215                        self.advance();
4216                    }
4217                    if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("transaction")) {
4218                        self.advance();
4219                    }
4220                    // v7.39 — no longer a no-op. The note above said SPG
4221                    // "doesn't yet honor session-set isolation across
4222                    // statements"; it does now, through
4223                    // `default_transaction_isolation`, and measured on
4224                    // PG 18.6 this statement is exactly a way to set it:
4225                    //
4226                    //   SET SESSION CHARACTERISTICS AS TRANSACTION
4227                    //       ISOLATION LEVEL REPEATABLE READ;
4228                    //   current_setting('default_transaction_isolation')
4229                    //       -> repeatable read
4230                    //
4231                    // pg_dump prepends this to fix the level for a
4232                    // restore session, so accepting it and doing nothing
4233                    // meant the restore ran at a level nobody chose.
4234                    //
4235                    // The trailing READ ONLY / [NOT] DEFERRABLE modes are
4236                    // still consumed and dropped. `default_transaction_read_only`
4237                    // exists in the GUC inventory but nothing enforces it,
4238                    // and setting a value no code honours is the very
4239                    // defect this version is about — a session told it
4240                    // holds a guarantee it does not.
4241                    let modes = self.parse_isolation_level_clauses()?;
4242                    self.consume_until_statement_boundary();
4243                    let mut pairs: alloc::vec::Vec<(
4244                        alloc::string::String,
4245                        crate::ast::SetValue,
4246                    )> = alloc::vec::Vec::new();
4247                    if let Some(level) = modes.isolation {
4248                        pairs.push((
4249                            alloc::string::String::from("default_transaction_isolation"),
4250                            crate::ast::SetValue::String(alloc::string::String::from(
4251                                level.as_pg_str(),
4252                            )),
4253                        ));
4254                    }
4255                    if let Some(ro) = modes.read_only {
4256                        pairs.push((
4257                            alloc::string::String::from("default_transaction_read_only"),
4258                            crate::ast::SetValue::Ident(alloc::string::String::from(if ro {
4259                                "on"
4260                            } else {
4261                                "off"
4262                            })),
4263                        ));
4264                    }
4265                    return Ok(if pairs.is_empty() {
4266                        Statement::Empty
4267                    } else {
4268                        Statement::SetParameterList(pairs)
4269                    });
4270                }
4271                // v7.37.17 (17.6 sibling) — PG `SET CONSTRAINTS
4272                // { ALL | <name>[, ...] } { DEFERRED | IMMEDIATE }`.
4273                // pg_dump emits this to control the deferrability of
4274                // FK / UNIQUE constraints across a bulk restore. SPG
4275                // has no deferrable-constraint machinery today; the
4276                // FK checker is strict-immediate. Accept-and-no-op
4277                // for pg_dump round-trip compatibility.
4278                if matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("constraints"))
4279                {
4280                    self.advance(); // CONSTRAINTS
4281                    // v7.39 (round 288) — no longer a no-op: the trailing
4282                    // DEFERRED / IMMEDIATE sets the transaction's timing.
4283                    // v7.39 (round 308, V29) — and the names are kept.
4284                    // They used to be skipped over on the way to the
4285                    // DEFERRED keyword, so a named form silently behaved
4286                    // as ALL: `SET CONSTRAINTS fk_a DEFERRED` deferred
4287                    // every deferrable constraint in the transaction.
4288                    let mut names: alloc::vec::Vec<alloc::string::String> =
4289                        alloc::vec::Vec::new();
4290                    if matches!(self.peek(), Token::All) {
4291                        self.advance();
4292                    } else {
4293                        loop {
4294                            let mut n = self.expect_ident_like()?;
4295                            // A schema-qualified name (`public.fk_a`)
4296                            // identifies the same constraint; PG resolves
4297                            // it by the trailing segment.
4298                            while matches!(self.peek(), Token::Dot) {
4299                                self.advance();
4300                                n = self.expect_ident_like()?;
4301                            }
4302                            names.push(n);
4303                            if matches!(self.peek(), Token::Comma) {
4304                                self.advance();
4305                            } else {
4306                                break;
4307                            }
4308                        }
4309                    }
4310                    let deferred = match self.peek() {
4311                        Token::Ident(s) | Token::QuotedIdent(s)
4312                            if s.eq_ignore_ascii_case("deferred") =>
4313                        {
4314                            true
4315                        }
4316                        Token::Ident(s) | Token::QuotedIdent(s)
4317                            if s.eq_ignore_ascii_case("immediate") =>
4318                        {
4319                            false
4320                        }
4321                        other => {
4322                            return Err(self.err(alloc::format!(
4323                                "expected DEFERRED or IMMEDIATE after SET CONSTRAINTS, got {other:?}"
4324                            )));
4325                        }
4326                    };
4327                    self.advance();
4328                    return Ok(Statement::SetConstraints { names, deferred });
4329                }
4330                // v7.16.2 — PG `SET [SESSION] AUTHORIZATION
4331                // { DEFAULT | '<role>' | <ident> }` (mailrs
4332                // round-10 A.1). pg_dump preamble emits the
4333                // `DEFAULT` form to reset session authorization.
4334                //
4335                // v7.39 (round 697) — this said "SPG has no role system so
4336                // this is a strict no-op". SPG has had one since round 58;
4337                // the comment outlived it, and with it the reason a name
4338                // that is not a role was accepted here. It still switches
4339                // no authorization — what it does now is refuse a role
4340                // that does not exist, as PG18 does. PG also accepts `RESET SESSION
4341                // AUTHORIZATION` (handled by the RESET parser
4342                // elsewhere). Reference:
4343                // <https://www.postgresql.org/docs/current/sql-set-session-authorization.html>
4344                if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("authorization"))
4345                {
4346                    self.advance(); // AUTHORIZATION
4347                    match self.peek().clone() {
4348                        Token::Default => {
4349                            self.advance();
4350                        }
4351                        Token::String(r) | Token::Ident(r) | Token::QuotedIdent(r) => {
4352                            self.advance();
4353                            return Ok(Statement::ValidateOnly {
4354                                kind: crate::ast::ValidateOnlyKind::RoleName,
4355                                names: alloc::vec![r],
4356                            });
4357                        }
4358                        other => {
4359                            return Err(self.err(alloc::format!(
4360                                "expected DEFAULT / '<role>' / <ident> after SET SESSION AUTHORIZATION, got {other:?}"
4361                            )));
4362                        }
4363                    }
4364                    return Ok(Statement::Empty);
4365                }
4366                // v7.38 轴 4 — `SET [SESSION] TRANSACTION
4367                // ISOLATION LEVEL { READ COMMITTED | READ
4368                // UNCOMMITTED | REPEATABLE READ | SERIALIZABLE }
4369                // [, READ {ONLY|WRITE}] [, [NOT] DEFERRABLE]`.
4370                // PG-standard surface. v7.37.8 accepts the syntax
4371                // and tracks the selected level on
4372                // `Engine::current_isolation_level()`; the actual
4373                // MVCC / SSI semantics implementation lands in
4374                // the 轴 4 isolation framework (separate train).
4375                // PG itself maps READ UNCOMMITTED to READ COMMITTED
4376                // internally; SPG behaves the same (effectively
4377                // READ COMMITTED at every level today).
4378                if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("transaction"))
4379                {
4380                    self.advance(); // TRANSACTION
4381                    let modes = self.parse_isolation_level_clauses()?;
4382                    return Ok(Statement::SetTransaction { modes });
4383                }
4384                // v7.14.0 — MySQL `SET CHARACTER SET <charset>`
4385                // alias — same accept-as-no-op as SET NAMES.
4386                if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("character"))
4387                    && matches!(self.tokens.get(self.pos + 1), Some(Token::Ident(s)) if s.eq_ignore_ascii_case("set"))
4388                {
4389                    self.advance(); // CHARACTER
4390                    self.advance(); // SET
4391                    if matches!(
4392                        self.peek(),
4393                        Token::Ident(_) | Token::QuotedIdent(_) | Token::String(_)
4394                    ) {
4395                        self.advance();
4396                    }
4397                    return Ok(Statement::Empty);
4398                }
4399                // v7.39 (GUC) — PG spells the timezone GUC as two
4400                // keywords: `SET [LOCAL|SESSION] TIME ZONE <value>`,
4401                // where <value> is a string/ident or the LOCAL /
4402                // DEFAULT keyword (both mean "back to the default").
4403                if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("time"))
4404                    && matches!(self.tokens.get(self.pos + 1), Some(Token::Ident(s)) if s.eq_ignore_ascii_case("zone"))
4405                {
4406                    self.advance(); // TIME
4407                    self.advance(); // ZONE
4408                    let value = match self.peek().clone() {
4409                        Token::Ident(s)
4410                            if s.eq_ignore_ascii_case("local")
4411                                || s.eq_ignore_ascii_case("default") =>
4412                        {
4413                            self.advance();
4414                            crate::ast::SetValue::Default
4415                        }
4416                        Token::Default => {
4417                            self.advance();
4418                            crate::ast::SetValue::Default
4419                        }
4420                        _ => self.parse_set_value()?,
4421                    };
4422                    return Ok(Statement::SetParameter {
4423                        name: "timezone".into(),
4424                        value,
4425                        local: set_local,
4426                    });
4427                }
4428                // v7.39 (round 430) — `SET @x = <expr> [, @y := <expr>]` is a
4429                // MySQL USER-variable assignment: its own per-session
4430                // namespace, an arbitrary expression on the right, and `:=`
4431                // as a second spelling of `=`. It used to fall into the
4432                // session-PARAMETER list below, whose values are literals and
4433                // whose store nothing reads back under a `@` name — so the
4434                // assignment reported success and vanished.
4435                //
4436                // A `@@`-prefixed LHS is a real engine setting and keeps the
4437                // old path.
4438                if matches!(self.peek(), Token::SessionVar(s) if !s.starts_with("@@")) {
4439                    return self.parse_set_user_vars();
4440                }
4441                // v7.14.0 — multi-assignment form
4442                // `SET a = 1, b = 2, …`. Single-assignment is the
4443                // 1-element case. Each LHS may be a regular ident
4444                // or a SessionVar (`@VAR` / `@@VAR`).
4445                let mut pairs: Vec<(String, crate::ast::SetValue)> = Vec::new();
4446                loop {
4447                    let lhs = match self.peek().clone() {
4448                        Token::SessionVar(s) => {
4449                            self.advance();
4450                            s
4451                        }
4452                        Token::Ident(_) | Token::QuotedIdent(_) => self.parse_set_param_name()?,
4453                        other => {
4454                            return Err(self.err(format!(
4455                                "expected parameter name after SET, got {other:?}"
4456                            )));
4457                        }
4458                    };
4459                    // Accept either `=` or the bare `TO` keyword.
4460                    match self.peek() {
4461                        Token::Eq => {
4462                            self.advance();
4463                        }
4464                        Token::To => {
4465                            self.advance();
4466                        }
4467                        other => {
4468                            return Err(self.err(format!(
4469                                "expected `=` or TO after SET {lhs}, got {other:?}"
4470                            )));
4471                        }
4472                    }
4473                    let mut value = self.parse_set_value()?;
4474                    // v7.39 (GUC) — disambiguate the comma: `, name =` /
4475                    // `, name TO` continues a MySQL-style multi-assign,
4476                    // anything else is a PG list VALUE
4477                    // (`SET search_path = myschema, public`) folded into
4478                    // one comma-joined string.
4479                    while matches!(self.peek(), Token::Comma) {
4480                        let is_assign = matches!(
4481                            self.tokens.get(self.pos + 1),
4482                            Some(Token::Ident(_) | Token::QuotedIdent(_) | Token::SessionVar(_))
4483                        ) && matches!(
4484                            self.tokens.get(self.pos + 2),
4485                            Some(Token::Eq | Token::To)
4486                        );
4487                        if is_assign {
4488                            break;
4489                        }
4490                        self.advance(); // comma
4491                        let next = self.parse_set_value()?;
4492                        let joined = alloc::format!(
4493                            "{}, {}",
4494                            set_value_text(&value),
4495                            set_value_text(&next)
4496                        );
4497                        value = crate::ast::SetValue::String(joined);
4498                    }
4499                    pairs.push((lhs, value));
4500                    if matches!(self.peek(), Token::Comma) {
4501                        self.advance();
4502                        continue;
4503                    }
4504                    break;
4505                }
4506                if pairs.len() == 1 {
4507                    let (name, value) = pairs.into_iter().next().unwrap();
4508                    Ok(Statement::SetParameter {
4509                        name,
4510                        value,
4511                        local: set_local,
4512                    })
4513                } else {
4514                    Ok(Statement::SetParameterList(pairs))
4515                }
4516            }
4517            // v7.12.1 — `RESET <name>` / `RESET ALL`.
4518            Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("reset") => {
4519                self.advance();
4520                match self.peek().clone() {
4521                    Token::All => {
4522                        self.advance();
4523                        Ok(Statement::ResetParameter(None))
4524                    }
4525                    Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("all") => {
4526                        self.advance();
4527                        Ok(Statement::ResetParameter(None))
4528                    }
4529                    // v7.39 (RLS) — `RESET ROLE` clears the session role.
4530                    Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("role") => {
4531                        self.advance();
4532                        Ok(Statement::SetRole(None))
4533                    }
4534                    // 7.38.1 S5.2 — `RESET SESSION AUTHORIZATION`
4535                    // (pg_dump's return from the owner switch).
4536                    Token::Ident(s) | Token::QuotedIdent(s)
4537                        if s.eq_ignore_ascii_case("session")
4538                            && matches!(
4539                                self.tokens.get(self.pos + 1),
4540                                Some(Token::Ident(a) | Token::QuotedIdent(a))
4541                                    if a.eq_ignore_ascii_case("authorization")
4542                            ) =>
4543                    {
4544                        self.advance(); // SESSION
4545                        self.advance(); // AUTHORIZATION
4546                        Ok(Statement::SetRole(None))
4547                    }
4548                    _ => {
4549                        let name = self.parse_set_param_name()?;
4550                        Ok(Statement::ResetParameter(Some(name)))
4551                    }
4552                }
4553            }
4554            // v7.39 (round 218) — server-side cursors.
4555            Token::Ident(s) if s.eq_ignore_ascii_case("declare") => self.parse_declare_cursor(),
4556            Token::Ident(s) if s.eq_ignore_ascii_case("fetch") => self.parse_fetch_or_move(false),
4557            Token::Ident(s) if s.eq_ignore_ascii_case("move") => self.parse_fetch_or_move(true),
4558            Token::Ident(s) if s.eq_ignore_ascii_case("close") => {
4559                self.advance();
4560                match self.peek().clone() {
4561                    Token::All => {
4562                        self.advance();
4563                        Ok(Statement::CloseCursor { name: None })
4564                    }
4565                    Token::Ident(s) if s.eq_ignore_ascii_case("all") => {
4566                        self.advance();
4567                        Ok(Statement::CloseCursor { name: None })
4568                    }
4569                    Token::Ident(n) | Token::QuotedIdent(n) => {
4570                        self.advance();
4571                        Ok(Statement::CloseCursor { name: Some(n) })
4572                    }
4573                    other => Err(self.err(format!(
4574                        "expected cursor name or ALL after CLOSE, got {other:?}"
4575                    ))),
4576                }
4577            }
4578            other => Err(self.err(format!(
4579                "expected SELECT / CREATE / DROP / INSERT / UPDATE / DELETE / ALTER / BEGIN / COMMIT / \
4580                 ROLLBACK / SAVEPOINT / RELEASE / SHOW at start of statement, got {other:?}"
4581            ))),
4582        }
4583    }
4584
4585    /// v7.39 (round 218) — `DECLARE <name> [BINARY] [INSENSITIVE] [ASENSITIVE]
4586    /// [[NO] SCROLL] CURSOR [{WITH|WITHOUT} HOLD] FOR <select>`. BINARY /
4587    /// (IN|A)SENSITIVE are accepted and ignored (SPG cursors materialize at
4588    /// DECLARE, which is INSENSITIVE — PG's only actual behaviour too).
4589    fn parse_declare_cursor(&mut self) -> Result<Statement, ParseError> {
4590        self.advance(); // DECLARE
4591        let name = match self.advance() {
4592            Token::Ident(n) | Token::QuotedIdent(n) => n,
4593            other => {
4594                return Err(self.err(format!("expected cursor name after DECLARE, got {other:?}")));
4595            }
4596        };
4597        let mut scroll: Option<bool> = None;
4598        loop {
4599            match self.peek() {
4600                Token::Ident(s)
4601                    if s.eq_ignore_ascii_case("binary")
4602                        || s.eq_ignore_ascii_case("insensitive")
4603                        || s.eq_ignore_ascii_case("asensitive") =>
4604                {
4605                    self.advance();
4606                }
4607                Token::Ident(s) if s.eq_ignore_ascii_case("scroll") => {
4608                    self.advance();
4609                    scroll = Some(true);
4610                }
4611                Token::Not | Token::Ident(_) if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("no")) =>
4612                {
4613                    self.advance(); // NO
4614                    if !matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("scroll")) {
4615                        return Err(self.err(format!(
4616                            "expected SCROLL after NO in DECLARE, got {:?}",
4617                            self.peek()
4618                        )));
4619                    }
4620                    self.advance();
4621                    scroll = Some(false);
4622                }
4623                _ => break,
4624            }
4625        }
4626        if !matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("cursor")) {
4627            return Err(self.err(format!("expected CURSOR in DECLARE, got {:?}", self.peek())));
4628        }
4629        self.advance();
4630        let mut hold = false;
4631        if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("with")) {
4632            self.advance();
4633            if !matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("hold")) {
4634                return Err(self.err(format!(
4635                    "expected HOLD after WITH in DECLARE, got {:?}",
4636                    self.peek()
4637                )));
4638            }
4639            self.advance();
4640            hold = true;
4641        } else if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("without")) {
4642            self.advance();
4643            if !matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("hold")) {
4644                return Err(self.err(format!(
4645                    "expected HOLD after WITHOUT in DECLARE, got {:?}",
4646                    self.peek()
4647                )));
4648            }
4649            self.advance();
4650        }
4651        if !matches!(self.peek(), Token::For) {
4652            return Err(self.err(format!(
4653                "expected FOR before the cursor query, got {:?}",
4654                self.peek()
4655            )));
4656        }
4657        self.advance();
4658        let query = self.parse_one_statement()?;
4659        Ok(Statement::DeclareCursor {
4660            name,
4661            scroll,
4662            hold,
4663            query: alloc::boxed::Box::new(query),
4664        })
4665    }
4666
4667    /// v7.39 (round 218) — `FETCH`/`MOVE` `[<direction>] [FROM|IN] <name>`.
4668    /// Direction: NEXT | PRIOR | FIRST | LAST | ABSOLUTE n | RELATIVE n | n |
4669    /// ALL | FORWARD [n|ALL] | BACKWARD [n|ALL]; bare `FETCH <name>` = NEXT.
4670    fn parse_fetch_or_move(&mut self, is_move: bool) -> Result<Statement, ParseError> {
4671        use crate::ast::CursorDirection as D;
4672        self.advance(); // FETCH / MOVE
4673        let mut signed_count = |this: &mut Self| -> Result<i64, ParseError> {
4674            let neg = if matches!(this.peek(), Token::Minus) {
4675                this.advance();
4676                true
4677            } else {
4678                false
4679            };
4680            match this.advance() {
4681                Token::Integer(v) => Ok(if neg { -v } else { v }),
4682                other => Err(this.err(format!("expected count, got {other:?}"))),
4683            }
4684        };
4685        let direction = match self.peek().clone() {
4686            Token::Ident(s) if s.eq_ignore_ascii_case("next") => {
4687                self.advance();
4688                D::Next
4689            }
4690            Token::Ident(s) if s.eq_ignore_ascii_case("prior") => {
4691                self.advance();
4692                D::Prior
4693            }
4694            Token::Ident(s) if s.eq_ignore_ascii_case("first") => {
4695                self.advance();
4696                D::First
4697            }
4698            Token::Ident(s) if s.eq_ignore_ascii_case("last") => {
4699                self.advance();
4700                D::Last
4701            }
4702            Token::Ident(s) if s.eq_ignore_ascii_case("absolute") => {
4703                self.advance();
4704                D::Absolute(signed_count(self)?)
4705            }
4706            Token::Ident(s) if s.eq_ignore_ascii_case("relative") => {
4707                self.advance();
4708                D::Relative(signed_count(self)?)
4709            }
4710            Token::Ident(s) if s.eq_ignore_ascii_case("forward") => {
4711                self.advance();
4712                match self.peek().clone() {
4713                    Token::All => {
4714                        self.advance();
4715                        D::All
4716                    }
4717                    Token::Ident(s) if s.eq_ignore_ascii_case("all") => {
4718                        self.advance();
4719                        D::All
4720                    }
4721                    Token::Integer(_) | Token::Minus => D::Count(signed_count(self)?),
4722                    _ => D::Next, // bare FORWARD = FORWARD 1
4723                }
4724            }
4725            Token::Ident(s) if s.eq_ignore_ascii_case("backward") => {
4726                self.advance();
4727                match self.peek().clone() {
4728                    Token::All => {
4729                        self.advance();
4730                        D::BackwardAll
4731                    }
4732                    Token::Ident(s) if s.eq_ignore_ascii_case("all") => {
4733                        self.advance();
4734                        D::BackwardAll
4735                    }
4736                    Token::Integer(_) | Token::Minus => D::Backward(signed_count(self)?),
4737                    _ => D::Backward(1), // bare BACKWARD = BACKWARD 1
4738                }
4739            }
4740            Token::All => {
4741                self.advance();
4742                D::All
4743            }
4744            Token::Ident(s) if s.eq_ignore_ascii_case("all") => {
4745                self.advance();
4746                D::All
4747            }
4748            Token::Integer(_) | Token::Minus => D::Count(signed_count(self)?),
4749            // Bare `FETCH <name>` — direction defaults to NEXT.
4750            _ => D::Next,
4751        };
4752        // Optional FROM / IN.
4753        if matches!(self.peek(), Token::From)
4754            || matches!(self.peek(), Token::In)
4755            || matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("in"))
4756        {
4757            self.advance();
4758        }
4759        let name = match self.advance() {
4760            Token::Ident(n) | Token::QuotedIdent(n) => n,
4761            other => {
4762                return Err(self.err(format!("expected cursor name, got {other:?}")));
4763            }
4764        };
4765        Ok(if is_move {
4766            Statement::MoveCursor { name, direction }
4767        } else {
4768            Statement::FetchCursor { name, direction }
4769        })
4770    }
4771
4772    /// v7.39 (round 280) — `CREATE STATISTICS [IF NOT EXISTS] <name>
4773    /// [(kind, …)] ON <col>, … FROM <table>`.
4774    fn parse_create_statistics_after_create(&mut self) -> Result<Statement, ParseError> {
4775        self.advance(); // STATISTICS
4776        // `IF` / `EXISTS` lex as plain identifiers; only NOT is a keyword.
4777        let mut if_not_exists = false;
4778        if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("if"))
4779            && matches!(self.tokens.get(self.pos + 1), Some(Token::Not))
4780        {
4781            self.advance();
4782            self.advance();
4783            if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("exists")) {
4784                self.advance();
4785                if_not_exists = true;
4786            }
4787        }
4788        let name = self.expect_ident_like()?;
4789        let mut kinds = Vec::new();
4790        if matches!(self.peek(), Token::LParen) {
4791            self.advance();
4792            loop {
4793                let k = self.expect_ident_like()?;
4794                // PG stores the single letters; accept the spelled-out
4795                // names the SQL uses and record what PG records.
4796                kinds.push(match k.to_ascii_lowercase().as_str() {
4797                    "ndistinct" => String::from("d"),
4798                    "dependencies" => String::from("f"),
4799                    "mcv" => String::from("m"),
4800                    other => {
4801                        return Err(
4802                            self.err(alloc::format!("unrecognized statistics kind \"{other}\""))
4803                        );
4804                    }
4805                });
4806                match self.advance() {
4807                    Token::Comma => {}
4808                    Token::RParen => break,
4809                    other => {
4810                        return Err(self.err(alloc::format!(
4811                            "expected ',' or ')' in statistics kind list, got {other:?}"
4812                        )));
4813                    }
4814                }
4815            }
4816        }
4817        if !matches!(self.peek(), Token::On) {
4818            return Err(self.err(alloc::format!(
4819                "expected ON in CREATE STATISTICS, got {:?}",
4820                self.peek()
4821            )));
4822        }
4823        self.advance();
4824        let mut columns = Vec::new();
4825        loop {
4826            columns.push(self.expect_ident_like()?);
4827            if matches!(self.peek(), Token::Comma) {
4828                self.advance();
4829            } else {
4830                break;
4831            }
4832        }
4833        if !matches!(self.peek(), Token::From) {
4834            return Err(self.err(alloc::format!(
4835                "expected FROM in CREATE STATISTICS, got {:?}",
4836                self.peek()
4837            )));
4838        }
4839        self.advance();
4840        let table = self.expect_ident_like()?;
4841        Ok(Statement::CreateStatistics {
4842            name,
4843            if_not_exists,
4844            kinds,
4845            columns,
4846            table,
4847        })
4848    }
4849
4850    /// v7.39 (round 280) — `DROP STATISTICS [IF EXISTS] <name>`.
4851    /// v7.39 (round 436) — the body of `DROP TABLE [IF EXISTS] a[, b] …`,
4852    /// entered with the `TABLE` keyword still unconsumed. Extracted so
4853    /// `DROP TEMPORARY TABLE` (MySQL) runs the identical grammar instead of
4854    /// a second copy — the parser cannot rewind, so re-dispatch has to be a
4855    /// forward call.
4856    fn parse_drop_table_after_keyword(&mut self) -> Result<Statement, ParseError> {
4857        self.advance(); // TABLE
4858        let if_exists = self.consume_if_exists();
4859        let mut names: Vec<String> = Vec::new();
4860        loop {
4861            names.push(self.expect_ident_like()?);
4862            if matches!(self.peek(), Token::Comma) {
4863                self.advance();
4864                continue;
4865            }
4866            break;
4867        }
4868        if matches!(
4869            self.peek(),
4870            Token::Ident(s) if s.eq_ignore_ascii_case("cascade")
4871                || s.eq_ignore_ascii_case("restrict")
4872        ) {
4873            self.advance();
4874        }
4875        Ok(Statement::DropTable { names, if_exists })
4876    }
4877
4878    fn parse_drop_statistics_after_drop(&mut self) -> Result<Statement, ParseError> {
4879        self.advance(); // STATISTICS
4880        let mut if_exists = false;
4881        if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("if"))
4882            && matches!(self.tokens.get(self.pos + 1),
4883                        Some(Token::Ident(e)) if e.eq_ignore_ascii_case("exists"))
4884        {
4885            self.advance();
4886            self.advance();
4887            if_exists = true;
4888        }
4889        let name = self.expect_ident_like()?;
4890        Ok(Statement::DropStatistics { name, if_exists })
4891    }
4892
4893    fn parse_create_stmt(&mut self) -> Result<Statement, ParseError> {
4894        debug_assert!(matches!(self.peek(), Token::Create));
4895        self.advance();
4896        match self.peek() {
4897            Token::Table => self.parse_create_table_stmt_after_create(),
4898            Token::Index => self.parse_create_index_stmt_after_create(false),
4899            // v7.39 (round 280) — CREATE STATISTICS is a real catalog
4900            // object now. It used to be consumed by the CREATE-noise
4901            // arm, so a pg_dump that declares extended statistics
4902            // restored silently without them and reflection showed
4903            // nothing.
4904            Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("statistics") => {
4905                self.parse_create_statistics_after_create()
4906            }
4907            // v7.9.29 — `CREATE UNIQUE INDEX … [WHERE pred]`.
4908            // The `UNIQUE` modifier turns a partial index into a
4909            // partial-uniqueness invariant (only rows matching the
4910            // WHERE predicate are checked for duplicates). mailrs
4911            // K1 (3 hits: email_templates default, calendar_events
4912            // master, calendar_events instance).
4913            Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("unique") => {
4914                self.advance();
4915                if !matches!(self.peek(), Token::Index) {
4916                    return Err(self.err(alloc::format!(
4917                        "expected INDEX after CREATE UNIQUE, got {:?}",
4918                        self.peek()
4919                    )));
4920                }
4921                self.parse_create_index_stmt_after_create(true)
4922            }
4923            Token::Publication => {
4924                self.advance();
4925                self.parse_create_publication_after_keyword()
4926            }
4927            Token::Subscription => {
4928                self.advance();
4929                self.parse_create_subscription_after_keyword()
4930            }
4931            // v4.1: CREATE USER 'name' WITH PASSWORD 'pw' [ROLE 'role'].
4932            // USER isn't a reserved keyword — we look for the bare
4933            // identifier so the lexer doesn't have to grow a token.
4934            Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("user") => {
4935                self.advance();
4936                self.parse_create_user_after_keyword(true)
4937            }
4938            // v7.39 (read01 round 58) — `CREATE ROLE name [WITH] [options]`.
4939            // PG's CREATE USER *is* CREATE ROLE … LOGIN; the only difference is
4940            // the default of the LOGIN attribute.
4941            Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("role") => {
4942                self.advance();
4943                self.parse_create_user_after_keyword(false)
4944            }
4945            // v7.39 (RLS) — `CREATE POLICY name ON table …`.
4946            Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("policy") => {
4947                self.advance();
4948                self.parse_create_policy_after_keyword()
4949            }
4950            // v7.9.15 — `CREATE EXTENSION [IF NOT EXISTS] <name>
4951            // [WITH SCHEMA …] [VERSION '…'] [CASCADE]` as a
4952            // no-op. mailrs follow-up F3.
4953            Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("extension") => {
4954                self.advance();
4955                self.parse_create_extension_after_keyword()
4956            }
4957            // v7.12.4 — `CREATE [OR REPLACE] FUNCTION …` and
4958            // `CREATE [OR REPLACE] TRIGGER …`. `OR REPLACE` is
4959            // optional; absorb it here and forward to the
4960            // per-kind parsers with the flag. OR is a reserved
4961            // keyword token.
4962            Token::Or => {
4963                self.advance();
4964                let next = self.peek();
4965                let (Token::Ident(s2) | Token::QuotedIdent(s2)) = next else {
4966                    return Err(self.err(alloc::format!(
4967                        "expected REPLACE after CREATE OR, got {next:?}"
4968                    )));
4969                };
4970                if !s2.eq_ignore_ascii_case("replace") {
4971                    return Err(self.err(alloc::format!(
4972                        "expected REPLACE after CREATE OR, got {s2:?}"
4973                    )));
4974                }
4975                self.advance();
4976                self.parse_create_function_or_trigger_after_or_replace(true)
4977            }
4978            Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("function") => {
4979                self.advance();
4980                self.parse_create_function_after_keyword(false)
4981            }
4982            Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("trigger") => {
4983                self.advance();
4984                self.parse_create_trigger_after_keyword(false)
4985            }
4986            // v7.39 (round 139) — CREATE RULE …
4987            Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("rule") => {
4988                self.advance();
4989                self.parse_create_rule_after_keyword(false)
4990            }
4991            // v7.39 (read01 round 82) — CREATE CONSTRAINT TRIGGER. A constraint
4992            // trigger is a row-level AFTER trigger that additionally carries
4993            // DEFERRABLE / INITIALLY DEFERRED timing; the `parse_create_trigger`
4994            // path already tolerates and skips those clauses, so consuming the
4995            // CONSTRAINT keyword and reusing it makes the statement parse and the
4996            // trigger fire. (The deferral timing itself is not yet honoured —
4997            // SPG fires it as a plain AFTER trigger, which is correct behaviour
4998            // for every non-deferred use.)
4999            Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("constraint") => {
5000                self.advance();
5001                if !matches!(self.peek(), Token::Ident(t) | Token::QuotedIdent(t)
5002                    if t.eq_ignore_ascii_case("trigger"))
5003                {
5004                    return Err(self.err(alloc::format!(
5005                        "expected TRIGGER after CREATE CONSTRAINT, got {:?}",
5006                        self.peek()
5007                    )));
5008                }
5009                self.advance();
5010                self.parse_create_trigger_after_keyword(false)
5011            }
5012            // v7.17.0 — CREATE [TEMPORARY] SEQUENCE …
5013            Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("sequence") => {
5014                self.advance();
5015                self.parse_create_sequence_after_keyword(false)
5016            }
5017            // v7.17.0 Phase 1.2 — CREATE [TEMPORARY] VIEW …
5018            Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("view") => {
5019                self.advance();
5020                self.parse_create_view_after_keyword(false, false, false)
5021            }
5022            // v7.17.0 Phase 2.6 — MySQL view prefix clauses
5023            // `ALGORITHM = {UNDEFINED|MERGE|TEMPTABLE}` /
5024            // `DEFINER = <user>` / `SQL SECURITY {DEFINER|INVOKER}`
5025            // appear (in any order) between `CREATE` and `VIEW` in
5026            // every mysqldump-emitted view. Pre-2.6 the parser
5027            // rejected the prefix and the customer's whole view
5028            // backup failed on the first view. The hints are pure
5029            // planner / permission metadata; SPG's view-rewrite
5030            // path is semantically equivalent for all three
5031            // algorithms in v7.17 (TEMPTABLE differs only in
5032            // perf for huge views — out of v7.17 scope), and
5033            // DEFINER / SQL SECURITY are pure single-user
5034            // permissioning that SPG ignores by design.
5035            Token::Ident(s) | Token::QuotedIdent(s)
5036                if s.eq_ignore_ascii_case("algorithm")
5037                    || s.eq_ignore_ascii_case("definer")
5038                    || s.eq_ignore_ascii_case("sql") =>
5039            {
5040                self.consume_mysql_view_prefix()?;
5041                // After absorbing ALGORITHM / DEFINER / SQL SECURITY
5042                // (in any order, in any combination), the next
5043                // keyword must be VIEW. mysqldump never emits these
5044                // prefixes on non-view statements.
5045                let next = self.peek().clone();
5046                if matches!(&next, Token::Ident(s2) | Token::QuotedIdent(s2)
5047                    if s2.eq_ignore_ascii_case("view"))
5048                {
5049                    self.advance();
5050                    self.parse_create_view_after_keyword(false, false, false)
5051                } else {
5052                    Err(self.err(alloc::format!(
5053                        "expected VIEW after MySQL view prefix (ALGORITHM/DEFINER/SQL SECURITY), got {next:?}"
5054                    )))
5055                }
5056            }
5057            // v7.17.0 Phase 1.4 — CREATE TYPE name AS ENUM (…).
5058            Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("type") => {
5059                self.advance();
5060                self.parse_create_type_after_keyword()
5061            }
5062            // v7.17.0 Phase 1.5 — CREATE DOMAIN name AS base
5063            // [DEFAULT expr] [NOT NULL] [CHECK (expr)]*.
5064            Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("domain") => {
5065                self.advance();
5066                self.parse_create_domain_after_keyword()
5067            }
5068            // v7.17.0 Phase 1.6 — CREATE SCHEMA [IF NOT EXISTS]
5069            // name [AUTHORIZATION user]. Real catalog registry
5070            // (was silent-no-op'd pre-v7.17).
5071            Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("schema") => {
5072                self.advance();
5073                let if_not_exists = self.parse_if_not_exists();
5074                let name = self.expect_ident_like()?;
5075                // Optional `AUTHORIZATION <user>` trailer — accepted,
5076                // ignored (single-user catalog).
5077                if matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s)
5078                    if s.eq_ignore_ascii_case("authorization"))
5079                {
5080                    self.advance();
5081                    let _ = self.expect_ident_like()?;
5082                }
5083                Ok(Statement::CreateSchema { name, if_not_exists })
5084            }
5085            // v7.17.0 Phase 1.3 — CREATE MATERIALIZED VIEW …
5086            Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("materialized") => {
5087                self.advance();
5088                let next = self.peek().clone();
5089                if matches!(&next, Token::Ident(s2) | Token::QuotedIdent(s2) if s2.eq_ignore_ascii_case("view"))
5090                {
5091                    self.advance();
5092                    self.parse_create_materialized_view_after_keyword()
5093                } else {
5094                    Err(self.err(alloc::format!(
5095                        "expected VIEW after CREATE MATERIALIZED, got {next:?}"
5096                    )))
5097                }
5098            }
5099            // v7.38 (read01 P6.57) — CREATE UNLOGGED TABLE. Unlike TEMP (a
5100            // no-op below), an UNLOGGED table is a real, fully-usable table in
5101            // PG — it only skips WAL. SPG creates a normal table (the WAL-skip
5102            // durability optimisation is a follow-up), so a dump / app that
5103            // declares UNLOGGED tables works instead of failing to parse.
5104            Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("unlogged") => {
5105                self.advance(); // UNLOGGED
5106                if matches!(self.peek(), Token::Table) {
5107                    self.parse_create_table_stmt_after_create()
5108                } else {
5109                    Err(self.err(format!(
5110                        "expected TABLE after CREATE UNLOGGED, got {:?}",
5111                        self.peek()
5112                    )))
5113                }
5114            }
5115            Token::Ident(s) | Token::QuotedIdent(s)
5116                if s.eq_ignore_ascii_case("temporary") || s.eq_ignore_ascii_case("temp") =>
5117            {
5118                self.advance();
5119                // TEMPORARY/TEMP followed by SEQUENCE / VIEW.
5120                let next = self.peek().clone();
5121                if matches!(&next, Token::Ident(s2) | Token::QuotedIdent(s2) if s2.eq_ignore_ascii_case("sequence"))
5122                {
5123                    self.advance();
5124                    self.parse_create_sequence_after_keyword(true)
5125                } else if matches!(&next, Token::Ident(s2) | Token::QuotedIdent(s2) if s2.eq_ignore_ascii_case("view"))
5126                {
5127                    self.advance();
5128                    self.parse_create_view_after_keyword(false, false, true)
5129                } else {
5130                    // v7.39 (round 436) — `CREATE TEMPORARY TABLE` used to be
5131                    // consumed and answered OK while creating nothing, so
5132                    // every statement that touched the table afterwards failed
5133                    // with "table not found" — the DDL itself lied. It is a
5134                    // real CREATE TABLE now, marked temporary so the executor
5135                    // puts it in the session's own namespace. An optional
5136                    // TABLE keyword may or may not be present (`CREATE TEMP t`
5137                    // is not legal, but the keyword is consumed by the
5138                    // CREATE TABLE parser itself).
5139                    let stmt = self.parse_create_table_stmt_after_create()?;
5140                    match stmt {
5141                        Statement::CreateTable(mut c) => {
5142                            c.temporary = true;
5143                            Ok(Statement::CreateTable(c))
5144                        }
5145                        // `CREATE TEMPORARY TABLE x AS <select>` lowers to the
5146                        // CTAS node, which needs the same session namespace.
5147                        Statement::CreateMaterializedView(mut m) if m.as_plain_table => {
5148                            m.temporary = true;
5149                            Ok(Statement::CreateMaterializedView(m))
5150                        }
5151                        other => Ok(other),
5152                    }
5153                }
5154            }
5155            // v7.17.0 Phase 4.2 — MySQL `CREATE PROCEDURE name (…)
5156            // BEGIN <body> END`. The body may reference `@var`
5157            // session variables, SET statements, internal `;`
5158            // terminators, etc. SPG has no procedure runtime, so
5159            // consume the whole `CREATE PROCEDURE … END` block as
5160            // a no-op so mysqldump scripts that include stored
5161            // routines load through. The matching-END consumer
5162            // tracks BEGIN/END nesting depth to handle nested
5163            // BEGIN blocks correctly.
5164            Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("procedure") => {
5165                self.consume_mysql_routine_body();
5166                Ok(Statement::Empty)
5167            }
5168            // v7.14.0 — pg_dump / mysqldump emit
5169            // `CREATE SCHEMA / VIEW / MATERIALIZED VIEW /
5170            // TYPE / DOMAIN / DATABASE / ROLE / POLICY / OPERATOR`.
5171            // SPG is single-schema / single-database; these have
5172            // no behavioural effect, so consume + return Empty.
5173            // v7.17.0 NOTE: SEQUENCE / VIEW / MATERIALIZED VIEW /
5174            // TYPE / DOMAIN / SCHEMA were here pre-v7.17; all
5175            // moved up to real parser branches. DATABASE / ROLE /
5176            // POLICY / OPERATOR stay no-op forever
5177            // (single-database, hardcoded roles).
5178            Token::Ident(s) | Token::QuotedIdent(s)
5179                if matches!(
5180                    s.to_ascii_lowercase().as_str(),
5181                    "database"
5182                        | "role"
5183                        | "operator"
5184                        | "cast"
5185                        | "aggregate"
5186                        | "language"
5187                        | "collation"
5188                        | "conversion"
5189                        // v7.17.0 Phase 8 (audit N6) — rarely-
5190                        // emitted pg_dump shapes that should
5191                        // load through without a parser error.
5192                        // SPG has no planner statistics catalog,
5193                        // no event-trigger hooks, no foreign-
5194                        // data-wrapper infrastructure; consume
5195                        // + return Empty.
5196                        | "statistics"
5197                        | "event"
5198                        // v7.37.17 (17.6 siblings) — additional CREATE
5199                        // targets pg_dump / operator install scripts
5200                        // may emit that SPG has no matching machinery
5201                        // for. Consume + Empty-return.
5202                        | "text"
5203                        | "tablespace"
5204                        | "access"
5205                        | "large"
5206                ) =>
5207            {
5208                // DATABASE is the one member of this list PG refuses
5209                // inside a transaction block; the rest (ROLE, CAST,
5210                // TABLESPACE, …) it runs there quite happily, so only
5211                // this one is named. Still a no-op otherwise — SPG is
5212                // single-database.
5213                let is_database = s.eq_ignore_ascii_case("database");
5214                // The name is the first token after DATABASE, past an
5215                // `IF NOT EXISTS`.
5216                let name = if is_database {
5217                    self.scan_database_name()
5218                } else {
5219                    None
5220                };
5221                let collation = if is_database {
5222                    self.scan_database_collation_until_boundary()
5223                } else {
5224                    self.consume_until_statement_boundary();
5225                    None
5226                };
5227                if is_database {
5228                    return Ok(Statement::NoOpPreventedInTransaction {
5229                        what: String::from("CREATE DATABASE"),
5230                        collation,
5231                        name,
5232                    });
5233                }
5234                Ok(Statement::Empty)
5235            }
5236            // v7.39 (round 706) — the foreign-data family leaves the silent
5237            // list: `CREATE SERVER …`, `CREATE FOREIGN TABLE …`, `CREATE
5238            // FOREIGN DATA WRAPPER …` are still consumed whole (SPG has no
5239            // FDW machinery), but the ENGINE now warns, so a restore log
5240            // says what will not function instead of reporting success.
5241            Token::Ident(s) | Token::QuotedIdent(s)
5242                if s.eq_ignore_ascii_case("server") || s.eq_ignore_ascii_case("foreign") =>
5243            {
5244                self.consume_until_statement_boundary();
5245                Ok(Statement::ValidateOnly {
5246                    kind: crate::ast::ValidateOnlyKind::ForeignInfra,
5247                    names: Vec::new(),
5248                })
5249            }
5250            other => Err(self.err(format!(
5251                "expected TABLE / INDEX / USER / EXTENSION / PUBLICATION / SUBSCRIPTION / FUNCTION / TRIGGER / SEQUENCE / SCHEMA / VIEW / TYPE / DOMAIN [OR REPLACE …] after CREATE, got {other:?}"
5252            ))),
5253        }
5254    }
5255
5256    /// v7.12.4 — `CREATE OR REPLACE` already consumed; the next
5257    /// keyword decides whether we parse a function or trigger
5258    /// body. PG accepts other `OR REPLACE`-able objects (VIEW,
5259    /// PROCEDURE) — those land in later releases.
5260    fn parse_create_function_or_trigger_after_or_replace(
5261        &mut self,
5262        or_replace: bool,
5263    ) -> Result<Statement, ParseError> {
5264        let tok = self.peek();
5265        let (Token::Ident(s) | Token::QuotedIdent(s)) = tok else {
5266            return Err(self.err(alloc::format!(
5267                "expected FUNCTION / TRIGGER / RULE / VIEW after CREATE OR REPLACE, got {tok:?}"
5268            )));
5269        };
5270        if s.eq_ignore_ascii_case("function") {
5271            self.advance();
5272            self.parse_create_function_after_keyword(or_replace)
5273        } else if s.eq_ignore_ascii_case("trigger") {
5274            self.advance();
5275            self.parse_create_trigger_after_keyword(or_replace)
5276        } else if s.eq_ignore_ascii_case("rule") {
5277            // v7.39 (round 143) — CREATE OR REPLACE RULE name AS ON …
5278            self.advance();
5279            self.parse_create_rule_after_keyword(or_replace)
5280        } else if s.eq_ignore_ascii_case("view") {
5281            // v7.17.0 Phase 1.2 — CREATE OR REPLACE VIEW name AS SELECT …
5282            self.advance();
5283            self.parse_create_view_after_keyword(or_replace, false, false)
5284        } else if s.eq_ignore_ascii_case("temporary") || s.eq_ignore_ascii_case("temp") {
5285            // CREATE OR REPLACE TEMPORARY VIEW … (rare but legal).
5286            self.advance();
5287            let nxt = self.peek().clone();
5288            if matches!(&nxt, Token::Ident(n) | Token::QuotedIdent(n) if n.eq_ignore_ascii_case("view"))
5289            {
5290                self.advance();
5291                self.parse_create_view_after_keyword(or_replace, false, true)
5292            } else {
5293                Err(self.err(alloc::format!(
5294                    "expected VIEW after CREATE OR REPLACE TEMPORARY, got {nxt:?}"
5295                )))
5296            }
5297        } else {
5298            Err(self.err(alloc::format!(
5299                "expected FUNCTION / TRIGGER / RULE / VIEW after CREATE OR REPLACE, got {s:?}"
5300            )))
5301        }
5302    }
5303
5304    /// v7.9.15 — accept and discard `CREATE EXTENSION` DDL.
5305    /// SPG doesn't have a registry; pgvector / similar are
5306    /// either builtin (VECTOR(N) ↔ pgvector) or n/a. Parsing
5307    /// the syntax lets dual-target schemas keep the line.
5308    fn parse_create_extension_after_keyword(&mut self) -> Result<Statement, ParseError> {
5309        // Optional `IF NOT EXISTS`.
5310        self.consume_if_not_exists();
5311        let name = self.expect_ident_like()?;
5312        // Drain optional WITH SCHEMA <ident> / VERSION '<v>' /
5313        // CASCADE / FROM '<v>' clauses; we don't model them.
5314        loop {
5315            match self.peek() {
5316                Token::Ident(s) if s.eq_ignore_ascii_case("with") => {
5317                    self.advance();
5318                    continue;
5319                }
5320                Token::Ident(s) if s.eq_ignore_ascii_case("schema") => {
5321                    self.advance();
5322                    let _ = self.expect_ident_like()?;
5323                    continue;
5324                }
5325                Token::Ident(s) if s.eq_ignore_ascii_case("version") => {
5326                    self.advance();
5327                    // String or ident literal.
5328                    let _ = self.advance();
5329                    continue;
5330                }
5331                Token::Ident(s) if s.eq_ignore_ascii_case("from") => {
5332                    self.advance();
5333                    let _ = self.advance();
5334                    continue;
5335                }
5336                Token::Ident(s) if s.eq_ignore_ascii_case("cascade") => {
5337                    self.advance();
5338                    continue;
5339                }
5340                _ => break,
5341            }
5342        }
5343        // v7.39 (round 697) — the NAME is checked now. `CREATE EXTENSION
5344        // nosuch` reported success and `pg_extension` then did not list it,
5345        // which is the accept-and-do-nothing shape F31 exists to find.
5346        Ok(Statement::ValidateOnly {
5347            kind: crate::ast::ValidateOnlyKind::ExtensionAvailable,
5348            names: alloc::vec![name],
5349        })
5350    }
5351
5352    /// v7.12.4 — body of `CREATE [OR REPLACE] FUNCTION`. The
5353    /// `[OR REPLACE]` flag (and the `FUNCTION` keyword) have
5354    /// already been consumed by the caller. Grammar accepted:
5355    ///
5356    ///   name `(` arg-list `)`
5357    ///   `RETURNS` return-type
5358    ///   [ `LANGUAGE` ident ]
5359    ///   `AS` $$ body $$
5360    ///   [ `LANGUAGE` ident ]
5361    ///
5362    /// Either `LANGUAGE` position is allowed; PG accepts both.
5363    fn parse_create_function_after_keyword(
5364        &mut self,
5365        or_replace: bool,
5366    ) -> Result<Statement, ParseError> {
5367        let name = self.expect_ident_like()?;
5368        // Argument list. v7.12.4 commonly sees the empty `()`
5369        // (trigger functions); typed args parse and round-trip
5370        // but the executor only invokes nullary functions.
5371        if !matches!(self.peek(), Token::LParen) {
5372            return Err(self.err(alloc::format!(
5373                "expected '(' after function name {name:?}, got {:?}",
5374                self.peek()
5375            )));
5376        }
5377        self.advance();
5378        let args = self.parse_function_arg_list()?;
5379        // RETURNS clause.
5380        let tok = self.peek();
5381        let (Token::Ident(s) | Token::QuotedIdent(s)) = tok else {
5382            return Err(self.err(alloc::format!(
5383                "expected RETURNS after function arg list, got {tok:?}"
5384            )));
5385        };
5386        if !s.eq_ignore_ascii_case("returns") {
5387            return Err(self.err(alloc::format!(
5388                "expected RETURNS after function arg list, got {s:?}"
5389            )));
5390        }
5391        self.advance();
5392        let returns = self.parse_function_return()?;
5393        // Optional LANGUAGE clause (PG also accepts after AS — we'll
5394        // re-check after the body too).
5395        let mut language: Option<String> = self.parse_optional_language()?;
5396        // v7.39 (round 322, V46) — attribute clauses. PG allows them on
5397        // either side of the body and in any order, interleaved with
5398        // LANGUAGE; `CREATE FUNCTION f() RETURNS int LANGUAGE sql
5399        // IMMUTABLE STRICT AS $$…$$` used to be a parse error, which meant
5400        // PG's own pg_dump output did not restore.
5401        let mut attrs = FunctionAttrs::default();
5402        loop {
5403            let before = self.pos;
5404            self.parse_function_attrs_into(&mut attrs)?;
5405            if language.is_none() {
5406                language = self.parse_optional_language()?;
5407            }
5408            if self.pos == before {
5409                break;
5410            }
5411        }
5412        // `AS` followed by a $$-quoted body (lexer already
5413        // collapses both `$$…$$` and `$tag$…$tag$` to a single
5414        // Token::String). AS is a reserved keyword (Token::As).
5415        if !matches!(self.peek(), Token::As) {
5416            return Err(self.err(alloc::format!(
5417                "expected AS before function body, got {:?}",
5418                self.peek()
5419            )));
5420        }
5421        self.advance();
5422        let body_text = match self.peek() {
5423            Token::String(s) => {
5424                let body = s.clone();
5425                self.advance();
5426                body
5427            }
5428            other => {
5429                return Err(self.err(alloc::format!(
5430                    "expected $$-quoted function body after AS, got {other:?}"
5431                )));
5432            }
5433        };
5434        // Trailing clauses — PG's other accepted position for both the
5435        // LANGUAGE and the attributes.
5436        loop {
5437            let before = self.pos;
5438            self.parse_function_attrs_into(&mut attrs)?;
5439            if language.is_none() {
5440                language = self.parse_optional_language()?;
5441            }
5442            if self.pos == before {
5443                break;
5444            }
5445        }
5446        let language = language.unwrap_or_else(|| String::from("sql"));
5447        // PL/pgSQL bodies get structure-parsed. Other languages
5448        // (or PL/pgSQL bodies the v7.12.4 parser doesn't yet
5449        // recognise) round-trip as Raw text — the executor errors
5450        // when invoked with a clear unsupported message.
5451        let body = if language.eq_ignore_ascii_case("plpgsql") {
5452            match parse_plpgsql_body(&body_text) {
5453                Ok(block) => FunctionBody::PlPgSql(block),
5454                // Best-effort: if the body parser doesn't yet
5455                // support a construct used inside, fall back to
5456                // raw — keeps `CREATE FUNCTION` itself working
5457                // (catalogue accepts), executor errors on
5458                // invocation only.
5459                Err(_) => FunctionBody::Raw(body_text),
5460            }
5461        } else {
5462            FunctionBody::Raw(body_text)
5463        };
5464        Ok(Statement::CreateFunction(CreateFunctionStatement {
5465            name,
5466            or_replace,
5467            args,
5468            returns,
5469            language,
5470            body,
5471            attrs,
5472        }))
5473    }
5474
5475    /// v7.39 (round 322, V46) — consume any run of `CREATE FUNCTION`
5476    /// attribute clauses into `attrs`, stopping at the first token that
5477    /// is not one. Measured against PG 18.4, which accepts them in any
5478    /// order and on either side of the body.
5479    fn parse_function_attrs_into(&mut self, attrs: &mut FunctionAttrs) -> Result<(), ParseError> {
5480        loop {
5481            let word = match self.peek() {
5482                Token::Ident(w) | Token::QuotedIdent(w) => w.to_ascii_lowercase(),
5483                // NOT LEAKPROOF — NOT is a reserved keyword token.
5484                Token::Not
5485                    if matches!(
5486                        self.tokens.get(self.pos + 1),
5487                        Some(Token::Ident(w)) if w.eq_ignore_ascii_case("leakproof")
5488                    ) =>
5489                {
5490                    self.advance();
5491                    self.advance();
5492                    attrs.leakproof = false;
5493                    continue;
5494                }
5495                _ => return Ok(()),
5496            };
5497            match word.as_str() {
5498                "immutable" => {
5499                    self.advance();
5500                    attrs.volatility = FunctionVolatility::Immutable;
5501                }
5502                "stable" => {
5503                    self.advance();
5504                    attrs.volatility = FunctionVolatility::Stable;
5505                }
5506                "volatile" => {
5507                    self.advance();
5508                    attrs.volatility = FunctionVolatility::Volatile;
5509                }
5510                "strict" => {
5511                    self.advance();
5512                    attrs.strict = true;
5513                }
5514                "leakproof" => {
5515                    self.advance();
5516                    attrs.leakproof = true;
5517                }
5518                // RETURNS NULL ON NULL INPUT / CALLED ON NULL INPUT — the
5519                // spelled-out forms of STRICT and its opposite.
5520                "returns" | "called" => {
5521                    let strict = word == "returns";
5522                    let mut probe = self.pos + 1;
5523                    if strict {
5524                        // RETURNS *NULL* ON NULL INPUT; a bare RETURNS here
5525                        // is not ours.
5526                        match self.tokens.get(probe) {
5527                            Some(Token::Null) => probe += 1,
5528                            Some(Token::Ident(w)) if w.eq_ignore_ascii_case("null") => probe += 1,
5529                            _ => return Ok(()),
5530                        }
5531                    }
5532                    let ok = matches!(self.tokens.get(probe), Some(Token::On))
5533                        || matches!(self.tokens.get(probe), Some(Token::Ident(w)) if w.eq_ignore_ascii_case("on"));
5534                    if !ok {
5535                        return Ok(());
5536                    }
5537                    probe += 1;
5538                    match self.tokens.get(probe) {
5539                        Some(Token::Null) => probe += 1,
5540                        Some(Token::Ident(w)) if w.eq_ignore_ascii_case("null") => probe += 1,
5541                        _ => return Ok(()),
5542                    }
5543                    match self.tokens.get(probe) {
5544                        Some(Token::Ident(w)) if w.eq_ignore_ascii_case("input") => probe += 1,
5545                        _ => return Ok(()),
5546                    }
5547                    self.pos = probe;
5548                    attrs.strict = strict;
5549                }
5550                "security" | "external" => {
5551                    // [EXTERNAL] SECURITY { INVOKER | DEFINER }
5552                    let mut probe = self.pos + 1;
5553                    if word == "external" {
5554                        match self.tokens.get(probe) {
5555                            Some(Token::Ident(w)) if w.eq_ignore_ascii_case("security") => {
5556                                probe += 1;
5557                            }
5558                            _ => return Ok(()),
5559                        }
5560                    }
5561                    let definer = match self.tokens.get(probe) {
5562                        Some(Token::Ident(w)) if w.eq_ignore_ascii_case("definer") => true,
5563                        Some(Token::Ident(w)) if w.eq_ignore_ascii_case("invoker") => false,
5564                        _ => return Ok(()),
5565                    };
5566                    self.pos = probe + 1;
5567                    attrs.security_definer = definer;
5568                }
5569                "parallel" => {
5570                    let level = match self.tokens.get(self.pos + 1) {
5571                        Some(Token::Ident(w)) if w.eq_ignore_ascii_case("safe") => {
5572                            FunctionParallel::Safe
5573                        }
5574                        Some(Token::Ident(w)) if w.eq_ignore_ascii_case("restricted") => {
5575                            FunctionParallel::Restricted
5576                        }
5577                        Some(Token::Ident(w)) if w.eq_ignore_ascii_case("unsafe") => {
5578                            FunctionParallel::Unsafe
5579                        }
5580                        _ => return Ok(()),
5581                    };
5582                    self.pos += 2;
5583                    attrs.parallel = level;
5584                }
5585                "cost" | "rows" => {
5586                    let Some(n) = self.peek_number_at(self.pos + 1) else {
5587                        return Ok(());
5588                    };
5589                    self.pos += 2;
5590                    if word == "cost" {
5591                        attrs.cost = Some(n);
5592                    } else {
5593                        attrs.rows = Some(n);
5594                    }
5595                }
5596                _ => return Ok(()),
5597            }
5598        }
5599    }
5600
5601    /// The numeric literal at `idx`, if there is one.
5602    fn peek_number_at(&self, idx: usize) -> Option<f64> {
5603        match self.tokens.get(idx)? {
5604            Token::Integer(n) => Some(*n as f64),
5605            Token::Float(f) => Some(*f),
5606            Token::Numeric(t) => t.parse::<f64>().ok(),
5607            _ => None,
5608        }
5609    }
5610
5611    /// Closing `)`-terminated argument list. v7.12.4 commonly
5612    /// sees the empty `()`; typed args round-trip but the
5613    /// executor (yet) doesn't invoke them.
5614    /// v7.39 (round 344) — consume a `( n [, m] )` type modifier and throw
5615    /// it away, which is what PG does with one on a function parameter.
5616    fn skip_type_modifier(&mut self) {
5617        if !matches!(self.peek(), Token::LParen) {
5618            return;
5619        }
5620        // Only a numeric modifier — anything else is not one, and eating
5621        // it would swallow real grammar.
5622        let mut i = self.pos + 1;
5623        let mut seen_number = false;
5624        loop {
5625            match self.tokens.get(i) {
5626                Some(Token::Integer(_)) => seen_number = true,
5627                Some(Token::Comma) => {}
5628                Some(Token::RParen) => break,
5629                _ => return,
5630            }
5631            i += 1;
5632        }
5633        if !seen_number {
5634            return;
5635        }
5636        while self.pos <= i {
5637            self.advance();
5638        }
5639    }
5640
5641    fn parse_function_arg_list(&mut self) -> Result<Vec<FunctionArg>, ParseError> {
5642        let mut args: Vec<FunctionArg> = Vec::new();
5643        if matches!(self.peek(), Token::RParen) {
5644            self.advance();
5645            return Ok(args);
5646        }
5647        loop {
5648            // Optional `IN` / `OUT` / `INOUT` mode keyword. IN is
5649            // a reserved token; OUT / INOUT are bare idents.
5650            let mode = if matches!(self.peek(), Token::In) {
5651                self.advance();
5652                FunctionArgMode::In
5653            } else if matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("out"))
5654            {
5655                self.advance();
5656                FunctionArgMode::Out
5657            } else if matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("inout"))
5658            {
5659                self.advance();
5660                FunctionArgMode::InOut
5661            } else {
5662                FunctionArgMode::In
5663            };
5664            // Optional name. The next token is either a name
5665            // (followed by a type ident) or the type itself.
5666            // Disambiguate by peeking ahead: if the token after
5667            // the next ident is also an ident, we treat the
5668            // first as the name.
5669            // v7.39 (round 315, V19) — take EVERY ident-like word up to
5670            // the comma or paren, then decide. Reading at most two of
5671            // them could not spell `x double precision` at all, and
5672            // silently mis-read the bare `double precision` as a
5673            // parameter named "double" — which is what made the same
5674            // signature key two different ways.
5675            let (name, ty_token) = {
5676                let mut words: Vec<String> = alloc::vec![self.expect_ident_like()?];
5677                while matches!(self.peek(), Token::Ident(_) | Token::QuotedIdent(_)) {
5678                    words.push(self.expect_ident_like()?);
5679                }
5680                // v7.39 (round 344) — a length / precision modifier on the
5681                // type: `f(character varying(9))`, `f(numeric(10,2))`. PG
5682                // accepts it and DROPS it — `pg_get_function_arguments`
5683                // reports plain `character varying` / `numeric`, measured on
5684                // 18.4 — but SPG raised `syntax error at or near "("`,
5685                // because the modifier's parens were never consumed.
5686                self.skip_type_modifier();
5687                // r1049 — `f(v bigint[])`. The array suffix parsed in
5688                // the column position, the cast position and (r1038)
5689                // the RETURNS position, but not here: the fifth
5690                // member of the same family, reported by sentori as
5691                // presumably the same code. It is now.
5692                let array_suffix = self.consume_array_suffix();
5693                let whole = words.join(" ");
5694                let (name, mut ty_token) = if words.len() >= 2 && !is_multiword_type_phrase(&whole)
5695                {
5696                    (Some(words[0].clone()), words[1..].join(" "))
5697                } else {
5698                    (None, whole)
5699                };
5700                ty_token.push_str(&array_suffix);
5701                (name, ty_token)
5702            };
5703            // Type — try to map to ColumnTypeName, else Raw.
5704            let ty = match map_type_ident_to_column_type_name(&ty_token) {
5705                Some(t) => FunctionArgType::Typed(t),
5706                None => FunctionArgType::Raw(ty_token),
5707            };
5708            args.push(FunctionArg { mode, name, ty });
5709            match self.peek() {
5710                Token::Comma => {
5711                    self.advance();
5712                    continue;
5713                }
5714                Token::RParen => {
5715                    self.advance();
5716                    return Ok(args);
5717                }
5718                other => {
5719                    return Err(self.err(alloc::format!(
5720                        "expected , or ) in function arg list, got {other:?}"
5721                    )));
5722                }
5723            }
5724        }
5725    }
5726
5727    fn parse_function_return(&mut self) -> Result<FunctionReturn, ParseError> {
5728        // v7.39 (read01 round 65) — `RETURNS TABLE(col type, …)`: a set-returning
5729        // function whose row shape is named inline.
5730        if matches!(self.peek(), Token::Table)
5731            && matches!(self.tokens.get(self.pos + 1), Some(Token::LParen))
5732        {
5733            self.advance(); // TABLE
5734            self.advance(); // (
5735            let mut cols: Vec<String> = Vec::new();
5736            loop {
5737                let cname = self.expect_ident_like()?;
5738                let mut ty: Vec<String> = Vec::new();
5739                loop {
5740                    match self.peek() {
5741                        Token::Comma | Token::RParen | Token::Eof => break,
5742                        _ => {}
5743                    }
5744                    match self.advance() {
5745                        Token::Ident(w) | Token::QuotedIdent(w) => ty.push(w),
5746                        other => {
5747                            if let Some(w) = unreserved_keyword_text(&other) {
5748                                ty.push(w);
5749                            }
5750                        }
5751                    }
5752                }
5753                cols.push(alloc::format!("{cname} {}", ty.join(" ")));
5754                if matches!(self.peek(), Token::Comma) {
5755                    self.advance();
5756                } else {
5757                    break;
5758                }
5759            }
5760            if matches!(self.peek(), Token::RParen) {
5761                self.advance();
5762            }
5763            return Ok(FunctionReturn::Other(alloc::format!(
5764                "TABLE({})",
5765                cols.join(", ")
5766            )));
5767        }
5768        let ident = self.expect_ident_like()?;
5769        // v7.39 (read01 round 65) — `RETURNS SETOF <type>`.
5770        if ident.eq_ignore_ascii_case("setof") {
5771            let inner = self.expect_ident_like()?;
5772            let inner = alloc::format!("{inner}{}", self.consume_array_suffix());
5773            return Ok(FunctionReturn::Other(alloc::format!("SETOF {inner}")));
5774        }
5775        if ident.eq_ignore_ascii_case("trigger") {
5776            return Ok(FunctionReturn::Trigger);
5777        }
5778        if ident.eq_ignore_ascii_case("void") {
5779            return Ok(FunctionReturn::Void);
5780        }
5781        // r1038 — `RETURNS bigint[]`. An array COLUMN type parsed; the
5782        // RETURN position did not, so the `[` was a syntax error and the
5783        // whole migration stopped. sentori worked around it by returning
5784        // zero-padded text.
5785        let suffix = self.consume_array_suffix();
5786        if !suffix.is_empty() {
5787            return Ok(FunctionReturn::Other(alloc::format!("{ident}{suffix}")));
5788        }
5789        match map_type_ident_to_column_type_name(&ident) {
5790            Some(t) => Ok(FunctionReturn::Type(t)),
5791            None => Ok(FunctionReturn::Other(ident)),
5792        }
5793    }
5794
5795    /// Consume any `[]` / `[N]` array markers after a type name and give
5796    /// back their text. Empty when there are none.
5797    fn consume_array_suffix(&mut self) -> String {
5798        let mut out = String::new();
5799        while matches!(self.peek(), Token::LBracket) {
5800            self.advance();
5801            // `[N]` is accepted and, as in PG, the length is not enforced.
5802            if let Token::Integer(n) = self.peek().clone() {
5803                self.advance();
5804                out.push_str(&alloc::format!("[{n}]"));
5805            } else {
5806                out.push_str("[]");
5807            }
5808            if matches!(self.peek(), Token::RBracket) {
5809                self.advance();
5810            }
5811        }
5812        out
5813    }
5814
5815    fn parse_optional_language(&mut self) -> Result<Option<String>, ParseError> {
5816        match self.peek() {
5817            Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("language") => {
5818                self.advance();
5819                let lang = self.expect_ident_like()?;
5820                Ok(Some(lang.to_ascii_lowercase()))
5821            }
5822            _ => Ok(None),
5823        }
5824    }
5825
5826    /// v7.17.0 Phase 1.5 — body of `CREATE DOMAIN name AS
5827    /// base_type [DEFAULT expr] [NOT NULL | NULL] [CHECK
5828    /// (expr)]*`. The `DOMAIN` keyword has already been
5829    /// consumed. PG allows the trailing constraints in any
5830    /// order; we approximate with a small loop.
5831    fn parse_create_domain_after_keyword(&mut self) -> Result<Statement, ParseError> {
5832        let name = self.expect_ident_like()?;
5833        // Optional `AS`.
5834        if matches!(self.peek(), Token::As) {
5835            self.advance();
5836        }
5837        // v7.39 (round 259) — keep the raw type NAME when the base is not
5838        // a builtin: it is how `CREATE DOMAIN child AS parent` records its
5839        // parent domain.
5840        let (base_type, _, _, base_user_ref, _, _, _, _, _, _, _, _, _, _) =
5841            self.parse_type_with_implied_flags()?;
5842        let mut default: Option<Expr> = None;
5843        let mut not_null = false;
5844        let mut checks: Vec<Expr> = Vec::new();
5845        loop {
5846            match self.peek() {
5847                Token::Default => {
5848                    if default.is_some() {
5849                        return Err(self.err("DOMAIN DEFAULT specified twice".into()));
5850                    }
5851                    self.advance();
5852                    default = Some(self.parse_expr(0)?);
5853                }
5854                Token::Not => {
5855                    self.advance();
5856                    if !matches!(self.peek(), Token::Null) {
5857                        return Err(self.err(alloc::format!(
5858                            "expected NULL after NOT in DOMAIN, got {:?}",
5859                            self.peek()
5860                        )));
5861                    }
5862                    self.advance();
5863                    not_null = true;
5864                }
5865                Token::Null => {
5866                    self.advance();
5867                    // v7.39 (round 761, F31 tranche 2 #31) — bare NULL
5868                    // is the default-nullable marker (PG accepts it),
5869                    // but AFTER a NOT NULL it is a conflict PG refuses
5870                    // (`conflicting NULL/NOT NULL constraints`,
5871                    // PG18-measured); the old arm no-opped both ways.
5872                    if not_null {
5873                        return Err(self.err(alloc::string::String::from(
5874                            "conflicting NULL/NOT NULL constraints",
5875                        )));
5876                    }
5877                }
5878                Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("check") => {
5879                    self.advance();
5880                    if !matches!(self.peek(), Token::LParen) {
5881                        return Err(self.err(alloc::format!(
5882                            "expected '(' after CHECK in DOMAIN, got {:?}",
5883                            self.peek()
5884                        )));
5885                    }
5886                    self.advance();
5887                    let expr = self.parse_expr(0)?;
5888                    if !matches!(self.peek(), Token::RParen) {
5889                        return Err(self.err(alloc::format!(
5890                            "expected ')' after CHECK expr, got {:?}",
5891                            self.peek()
5892                        )));
5893                    }
5894                    self.advance();
5895                    checks.push(expr);
5896                }
5897                // CONSTRAINT <name> CHECK (…) — PG accepts a name
5898                // prefix on the constraint; we drop the name and
5899                // recurse into the constraint parsing.
5900                Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("constraint") => {
5901                    self.advance();
5902                    let _ = self.expect_ident_like()?;
5903                }
5904                _ => break,
5905            }
5906        }
5907        Ok(Statement::CreateDomain(crate::ast::CreateDomainStatement {
5908            name,
5909            base_type,
5910            base_domain: base_user_ref,
5911            default,
5912            not_null,
5913            checks,
5914        }))
5915    }
5916
5917    /// v7.17.0 Phase 1.4 — body of `CREATE TYPE name AS ENUM
5918    /// ('a', 'b', …)`. The `TYPE` keyword has already been
5919    /// consumed.
5920    fn parse_create_type_after_keyword(&mut self) -> Result<Statement, ParseError> {
5921        let name = self.expect_ident_like()?;
5922        // Required `AS`.
5923        if !matches!(self.peek(), Token::As) {
5924            return Err(self.err(alloc::format!(
5925                "expected AS after CREATE TYPE {name:?}, got {:?}",
5926                self.peek()
5927            )));
5928        }
5929        self.advance();
5930        // v7.37.x (ζ-B composite Phase 1) — `AS (` is the composite-
5931        // type shape: `CREATE TYPE foo AS (a INT, b TEXT)`. Branch
5932        // on the next token: `(` = composite, ident `ENUM` = enum.
5933        if matches!(self.peek(), Token::LParen) {
5934            self.advance();
5935            let mut fields: Vec<(String, ColumnTypeName)> = Vec::new();
5936            let mut field_user_types: Vec<Option<String>> = Vec::new();
5937            // v7.39 (round 769, F31 tranche 5 #140) — `CREATE TYPE x AS ()`
5938            // is legal PG (an attribute-less composite; measured — the old
5939            // e2e note claimed PG requires at least one attribute).
5940            if matches!(self.peek(), Token::RParen) {
5941                self.advance();
5942                return Ok(Statement::CreateType(crate::ast::CreateTypeStatement {
5943                    name,
5944                    kind: crate::ast::TypeKind::Composite {
5945                        fields,
5946                        field_user_types,
5947                    },
5948                }));
5949            }
5950            loop {
5951                let field_name = self.expect_ident_like()?;
5952                // v7.39 (round 264) — keep the raw type name when it is not
5953                // a builtin: that is how a NESTED composite field records
5954                // which composite it holds.
5955                let (field_type, _, _, field_user_ref, _, _, _, _, _, _, _, _, _, _) =
5956                    self.parse_type_with_implied_flags()?;
5957                fields.push((field_name, field_type));
5958                field_user_types.push(field_user_ref);
5959                if matches!(self.peek(), Token::Comma) {
5960                    self.advance();
5961                    continue;
5962                }
5963                if matches!(self.peek(), Token::RParen) {
5964                    self.advance();
5965                    break;
5966                }
5967                return Err(self.err(alloc::format!(
5968                    "expected , or ) in composite field list, got {:?}",
5969                    self.peek()
5970                )));
5971            }
5972            if fields.is_empty() {
5973                return Err(self.err("CREATE TYPE … AS (…) must declare at least one field".into()));
5974            }
5975            return Ok(Statement::CreateType(crate::ast::CreateTypeStatement {
5976                name,
5977                kind: crate::ast::TypeKind::Composite {
5978                    fields,
5979                    field_user_types,
5980                },
5981            }));
5982        }
5983        // Required `ENUM` ident.
5984        let kind_ident = match self.peek().clone() {
5985            Token::Ident(s) | Token::QuotedIdent(s) => s,
5986            other => {
5987                return Err(self.err(alloc::format!(
5988                    "expected ENUM or '(' after CREATE TYPE {name:?} AS, got {other:?}"
5989                )));
5990            }
5991        };
5992        if !kind_ident.eq_ignore_ascii_case("enum") {
5993            return Err(self.err(alloc::format!(
5994                "Phase 1.4 only supports ENUM or composite '(…)'; got {kind_ident:?}"
5995            )));
5996        }
5997        self.advance();
5998        if !matches!(self.peek(), Token::LParen) {
5999            return Err(self.err(alloc::format!(
6000                "expected '(' after ENUM, got {:?}",
6001                self.peek()
6002            )));
6003        }
6004        self.advance();
6005        let mut labels: Vec<String> = Vec::new();
6006        loop {
6007            match self.peek().clone() {
6008                Token::String(s) => {
6009                    self.advance();
6010                    labels.push(s);
6011                }
6012                other => {
6013                    return Err(
6014                        self.err(alloc::format!("expected enum label string, got {other:?}"))
6015                    );
6016                }
6017            }
6018            if matches!(self.peek(), Token::Comma) {
6019                self.advance();
6020                continue;
6021            }
6022            if matches!(self.peek(), Token::RParen) {
6023                self.advance();
6024                break;
6025            }
6026            return Err(self.err(alloc::format!(
6027                "expected , or ) in ENUM label list, got {:?}",
6028                self.peek()
6029            )));
6030        }
6031        if labels.is_empty() {
6032            return Err(self.err("CREATE TYPE … AS ENUM must declare at least one label".into()));
6033        }
6034        Ok(Statement::CreateType(crate::ast::CreateTypeStatement {
6035            name,
6036            kind: crate::ast::TypeKind::Enum { labels },
6037        }))
6038    }
6039
6040    /// v7.17.0 Phase 1.3 — body of `CREATE MATERIALIZED VIEW
6041    /// [IF NOT EXISTS] name [(col, …)] AS <SELECT …> [WITH [NO] DATA]`.
6042    /// The `CREATE MATERIALIZED VIEW` keywords have already been
6043    /// consumed.
6044    fn parse_create_materialized_view_after_keyword(&mut self) -> Result<Statement, ParseError> {
6045        let if_not_exists = self.parse_if_not_exists();
6046        let name = self.expect_ident_like()?;
6047        let mut columns: Vec<String> = Vec::new();
6048        if matches!(self.peek(), Token::LParen) {
6049            self.advance();
6050            loop {
6051                let c = self.expect_ident_like()?;
6052                columns.push(c);
6053                if matches!(self.peek(), Token::Comma) {
6054                    self.advance();
6055                    continue;
6056                }
6057                if matches!(self.peek(), Token::RParen) {
6058                    self.advance();
6059                    break;
6060                }
6061                return Err(self.err(alloc::format!(
6062                    "expected , or ) in MATERIALIZED VIEW column list, got {:?}",
6063                    self.peek()
6064                )));
6065            }
6066        }
6067        if !matches!(self.peek(), Token::As) {
6068            return Err(self.err(alloc::format!(
6069                "expected AS <SELECT …> after CREATE MATERIALIZED VIEW {name:?}, got {:?}",
6070                self.peek()
6071            )));
6072        }
6073        self.advance();
6074        // v7.39 (round 151) — a WITH-headed body is legal (read-only
6075        // CTEs only; the engine rejects data-modifying ones with PG's
6076        // message). A trailing `WITH [NO] DATA` can't START the body,
6077        // so WITH here heads the query.
6078        let body = if self.peek_is_with_kw() {
6079            self.advance();
6080            self.parse_nested_with_select()?
6081        } else {
6082            let body_stmt = self.parse_select_stmt()?;
6083            let Statement::Select(body) = body_stmt else {
6084                return Err(self.err(alloc::format!(
6085                    "CREATE MATERIALIZED VIEW body must be a SELECT, got {body_stmt:?}"
6086                )));
6087            };
6088            body
6089        };
6090        // Optional trailing `WITH [NO] DATA`.
6091        let with_data = self.parse_optional_with_data(true)?;
6092        Ok(Statement::CreateMaterializedView(
6093            crate::ast::CreateMaterializedViewStatement {
6094                temporary: false,
6095                name,
6096                if_not_exists,
6097                columns,
6098                body,
6099                with_data,
6100                as_plain_table: false,
6101            },
6102        ))
6103    }
6104
6105    /// v7.17.0 Phase 1.3 — `WITH [NO] DATA` trailer.
6106    /// `default_when_absent` is what to return if the tail is
6107    /// missing (CREATE defaults to WITH DATA, REFRESH defaults to
6108    /// WITH DATA).
6109    fn parse_optional_with_data(&mut self, default_when_absent: bool) -> Result<bool, ParseError> {
6110        let save = self.pos;
6111        // `WITH` is an Ident (not reserved in the lexer).
6112        let is_with = match self.peek() {
6113            Token::Ident(s) | Token::QuotedIdent(s) => s.eq_ignore_ascii_case("with"),
6114            _ => false,
6115        };
6116        if !is_with {
6117            return Ok(default_when_absent);
6118        }
6119        self.advance();
6120        // Optional `NO`.
6121        let mut with_data = true;
6122        let is_no = match self.peek() {
6123            Token::Ident(s) | Token::QuotedIdent(s) => s.eq_ignore_ascii_case("no"),
6124            _ => false,
6125        };
6126        if is_no {
6127            self.advance();
6128            with_data = false;
6129        }
6130        // Required `DATA` ident.
6131        let is_data = match self.peek() {
6132            Token::Ident(s) | Token::QuotedIdent(s) => s.eq_ignore_ascii_case("data"),
6133            _ => false,
6134        };
6135        if is_data {
6136            self.advance();
6137            Ok(with_data)
6138        } else {
6139            // Caller's WITH wasn't WITH-DATA — rewind so the outer
6140            // parser can interpret it.
6141            self.pos = save;
6142            Ok(default_when_absent)
6143        }
6144    }
6145
6146    /// v7.17.0 Phase 1.2 — body of `CREATE [OR REPLACE]
6147    /// [TEMPORARY] VIEW [IF NOT EXISTS] name [(col, …)] AS <SELECT>`.
6148    /// All keyword prefixes have already been consumed; the flags
6149    /// say which were present.
6150    fn parse_create_view_after_keyword(
6151        &mut self,
6152        or_replace: bool,
6153        _materialized_unused: bool,
6154        temporary: bool,
6155    ) -> Result<Statement, ParseError> {
6156        let if_not_exists = self.parse_if_not_exists();
6157        let name = self.expect_ident_like()?;
6158        // Optional `(col, col, …)` rename list.
6159        let mut columns: Vec<String> = Vec::new();
6160        if matches!(self.peek(), Token::LParen) {
6161            self.advance();
6162            loop {
6163                let c = self.expect_ident_like()?;
6164                columns.push(c);
6165                if matches!(self.peek(), Token::Comma) {
6166                    self.advance();
6167                    continue;
6168                }
6169                if matches!(self.peek(), Token::RParen) {
6170                    self.advance();
6171                    break;
6172                }
6173                return Err(self.err(alloc::format!(
6174                    "expected , or ) in VIEW column list, got {:?}",
6175                    self.peek()
6176                )));
6177            }
6178        }
6179        // Required `AS`.
6180        if !matches!(self.peek(), Token::As) {
6181            return Err(self.err(alloc::format!(
6182                "expected AS <SELECT …> after CREATE VIEW {name:?}, got {:?}",
6183                self.peek()
6184            )));
6185        }
6186        self.advance();
6187        // Body: a regular SELECT statement. v7.39 (round 151) — a
6188        // WITH-headed body is legal too (read-only CTEs only; the
6189        // engine rejects data-modifying ones with PG's message).
6190        // Disambiguation vs `WITH CHECK OPTION`: a body can't START
6191        // with the check-option clause, so WITH here heads the query.
6192        let body = if self.peek_is_with_kw() {
6193            self.advance();
6194            self.parse_nested_with_select()?
6195        } else {
6196            let body_stmt = self.parse_select_stmt()?;
6197            let Statement::Select(body) = body_stmt else {
6198                return Err(self.err(alloc::format!(
6199                    "CREATE VIEW body must be a SELECT statement, got {body_stmt:?}"
6200                )));
6201            };
6202            body
6203        };
6204        // v7.39 (round 132) — optional `WITH [ LOCAL | CASCADED ] CHECK OPTION`.
6205        // The SELECT parser stops before a trailing WITH, so it lands here.
6206        let check_option = if matches!(self.peek(),
6207            Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("with"))
6208        {
6209            self.advance(); // WITH
6210            let opt = match self.peek() {
6211                Token::Ident(s) if s.eq_ignore_ascii_case("local") => {
6212                    self.advance();
6213                    crate::ast::ViewCheckOption::Local
6214                }
6215                Token::Ident(s) if s.eq_ignore_ascii_case("cascaded") => {
6216                    self.advance();
6217                    crate::ast::ViewCheckOption::Cascaded
6218                }
6219                // Bare `WITH CHECK OPTION` defaults to CASCADED (PG).
6220                _ => crate::ast::ViewCheckOption::Cascaded,
6221            };
6222            if !matches!(self.peek(),
6223                Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("check"))
6224            {
6225                return Err(self.err(alloc::format!(
6226                    "expected CHECK in CREATE VIEW … WITH [LOCAL|CASCADED] CHECK OPTION, got {:?}",
6227                    self.peek()
6228                )));
6229            }
6230            self.advance(); // CHECK
6231            if !matches!(self.peek(),
6232                Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("option"))
6233            {
6234                return Err(self.err(alloc::format!(
6235                    "expected OPTION after WITH CHECK in CREATE VIEW, got {:?}",
6236                    self.peek()
6237                )));
6238            }
6239            self.advance(); // OPTION
6240            Some(opt)
6241        } else {
6242            None
6243        };
6244        Ok(Statement::CreateView(crate::ast::CreateViewStatement {
6245            name,
6246            or_replace,
6247            if_not_exists,
6248            temporary,
6249            columns,
6250            body,
6251            check_option,
6252        }))
6253    }
6254
6255    /// v7.17.0 — body of `CREATE [TEMPORARY] SEQUENCE`. The
6256    /// `[TEMPORARY]` and `SEQUENCE` tokens have already been
6257    /// consumed; `temporary` carries whether TEMPORARY was seen.
6258    fn parse_create_sequence_after_keyword(
6259        &mut self,
6260        temporary: bool,
6261    ) -> Result<Statement, ParseError> {
6262        let if_not_exists = self.parse_if_not_exists();
6263        let name = self.expect_ident_like()?;
6264        // Optional `AS data_type`.
6265        let data_type = if matches!(self.peek(), Token::As) {
6266            self.advance();
6267            Some(self.parse_sequence_data_type()?)
6268        } else {
6269            None
6270        };
6271        let options = self.parse_sequence_options(/* allow_restart = */ false)?;
6272        Ok(Statement::CreateSequence(
6273            crate::ast::CreateSequenceStatement {
6274                name,
6275                if_not_exists,
6276                temporary,
6277                data_type,
6278                options,
6279            },
6280        ))
6281    }
6282
6283    /// v7.17.0 — body of `ALTER SEQUENCE`. The `ALTER` keyword has
6284    /// already been consumed; this is reached after `SEQUENCE`.
6285    /// v7.39 (round 260) — `ALTER DOMAIN name <action>`.
6286    fn parse_alter_domain_after_keyword(&mut self) -> Result<Statement, ParseError> {
6287        use crate::ast::AlterDomainAction as A;
6288        let name = self.expect_ident_like()?;
6289        // DROP / SET / ADD lex as reserved keyword tokens, not idents.
6290        let kw = match self.peek() {
6291            Token::Ident(s) | Token::QuotedIdent(s) => s.to_ascii_lowercase(),
6292            Token::Drop => alloc::string::String::from("drop"),
6293            Token::Default => alloc::string::String::from("default"),
6294            other => {
6295                return Err(self.err(alloc::format!(
6296                    "expected an ALTER DOMAIN action, got {other:?}"
6297                )));
6298            }
6299        };
6300        let action = match kw.as_str() {
6301            "add" => {
6302                self.advance();
6303                let cname = if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("constraint"))
6304                {
6305                    self.advance();
6306                    Some(self.expect_ident_like()?)
6307                } else {
6308                    None
6309                };
6310                if !matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("check")) {
6311                    return Err(self.err(alloc::format!(
6312                        "ALTER DOMAIN ADD supports CHECK only, got {:?}",
6313                        self.peek()
6314                    )));
6315                }
6316                self.advance();
6317                if !matches!(self.peek(), Token::LParen) {
6318                    return Err(self.err("expected '(' after CHECK".into()));
6319                }
6320                self.advance();
6321                let check = self.parse_expr(0)?;
6322                if !matches!(self.peek(), Token::RParen) {
6323                    return Err(self.err("expected ')' after CHECK expression".into()));
6324                }
6325                self.advance();
6326                A::AddConstraint { name: cname, check }
6327            }
6328            "drop" => {
6329                self.advance();
6330                match self.peek() {
6331                    Token::Ident(s) if s.eq_ignore_ascii_case("constraint") => {
6332                        self.advance();
6333                        let if_exists = if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("if"))
6334                        {
6335                            self.advance();
6336                            if !matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("exists"))
6337                            {
6338                                return Err(self.err("expected EXISTS after IF".into()));
6339                            }
6340                            self.advance();
6341                            true
6342                        } else {
6343                            false
6344                        };
6345                        let cn = self.expect_ident_like()?;
6346                        A::DropConstraint {
6347                            name: cn,
6348                            if_exists,
6349                        }
6350                    }
6351                    Token::Default => {
6352                        self.advance();
6353                        A::DropDefault
6354                    }
6355                    Token::Not => {
6356                        self.advance();
6357                        if !matches!(self.peek(), Token::Null) {
6358                            return Err(self.err("expected NULL after NOT".into()));
6359                        }
6360                        self.advance();
6361                        A::DropNotNull
6362                    }
6363                    other => {
6364                        return Err(self.err(alloc::format!(
6365                            "ALTER DOMAIN DROP expects CONSTRAINT / DEFAULT / NOT NULL, got {other:?}"
6366                        )));
6367                    }
6368                }
6369            }
6370            "set" => {
6371                self.advance();
6372                match self.peek() {
6373                    Token::Default => {
6374                        self.advance();
6375                        A::SetDefault(self.parse_expr(0)?)
6376                    }
6377                    Token::Not => {
6378                        self.advance();
6379                        if !matches!(self.peek(), Token::Null) {
6380                            return Err(self.err("expected NULL after NOT".into()));
6381                        }
6382                        self.advance();
6383                        A::SetNotNull
6384                    }
6385                    other => {
6386                        return Err(self.err(alloc::format!(
6387                            "ALTER DOMAIN SET expects DEFAULT / NOT NULL, got {other:?}"
6388                        )));
6389                    }
6390                }
6391            }
6392            "rename" => {
6393                self.advance();
6394                if !matches!(self.peek(), Token::To) {
6395                    return Err(self.err("expected TO after RENAME".into()));
6396                }
6397                self.advance();
6398                A::RenameTo(self.expect_ident_like()?)
6399            }
6400            other => {
6401                return Err(self.err(alloc::format!("unsupported ALTER DOMAIN action {other:?}")));
6402            }
6403        };
6404        Ok(Statement::AlterDomain { name, action })
6405    }
6406
6407    fn parse_alter_sequence_after_keyword(&mut self) -> Result<Statement, ParseError> {
6408        let if_exists = self.parse_if_exists();
6409        let name = self.expect_ident_like()?;
6410        // v7.39 (read01 round 49) — `RENAME TO new`; mutually exclusive with
6411        // the option list (PG allows only one or the other).
6412        if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("rename")) {
6413            self.advance();
6414            if matches!(self.peek(), Token::To) {
6415                self.advance();
6416            } else {
6417                self.expect_keyword_ident("to")?;
6418            }
6419            let new = self.expect_ident_like()?;
6420            return Ok(Statement::AlterSequence(
6421                crate::ast::AlterSequenceStatement {
6422                    name,
6423                    if_exists,
6424                    options: crate::ast::SequenceOptions::default(),
6425                    rename_to: Some(new),
6426                },
6427            ));
6428        }
6429        let options = self.parse_sequence_options(/* allow_restart = */ true)?;
6430        Ok(Statement::AlterSequence(
6431            crate::ast::AlterSequenceStatement {
6432                name,
6433                if_exists,
6434                options,
6435                rename_to: None,
6436            },
6437        ))
6438    }
6439
6440    fn parse_sequence_data_type(&mut self) -> Result<crate::ast::SequenceDataType, ParseError> {
6441        let kw = self.expect_ident_like()?;
6442        match kw.to_ascii_lowercase().as_str() {
6443            "smallint" | "int2" => Ok(crate::ast::SequenceDataType::SmallInt),
6444            "integer" | "int" | "int4" => Ok(crate::ast::SequenceDataType::Int),
6445            "bigint" | "int8" => Ok(crate::ast::SequenceDataType::BigInt),
6446            other => Err(self.err(alloc::format!(
6447                "expected SMALLINT / INTEGER / BIGINT after SEQUENCE AS, got {other:?}"
6448            ))),
6449        }
6450    }
6451
6452    fn parse_sequence_options(
6453        &mut self,
6454        allow_restart: bool,
6455    ) -> Result<crate::ast::SequenceOptions, ParseError> {
6456        use crate::ast::{SeqBound, SequenceOptions, SequenceOwnedBy};
6457        let mut opts = SequenceOptions::default();
6458        #[allow(clippy::while_let_loop)]
6459        loop {
6460            // Match an ident; stop at any non-ident token (sentinel,
6461            // semicolon, end of statement).
6462            let kw_lc = match self.peek() {
6463                Token::Ident(s) | Token::QuotedIdent(s) => s.to_ascii_lowercase(),
6464                _ => break,
6465            };
6466            match kw_lc.as_str() {
6467                "increment" => {
6468                    self.advance();
6469                    // Optional BY.
6470                    if self.peek_is_by() {
6471                        self.advance();
6472                    }
6473                    opts.increment = Some(self.expect_signed_int()?);
6474                }
6475                "minvalue" => {
6476                    self.advance();
6477                    opts.min_value = Some(SeqBound::Value(self.expect_signed_int()?));
6478                }
6479                "maxvalue" => {
6480                    self.advance();
6481                    opts.max_value = Some(SeqBound::Value(self.expect_signed_int()?));
6482                }
6483                "no" => {
6484                    self.advance();
6485                    let what = self.expect_ident_like()?;
6486                    match what.to_ascii_lowercase().as_str() {
6487                        "minvalue" => opts.min_value = Some(SeqBound::NoBound),
6488                        "maxvalue" => opts.max_value = Some(SeqBound::NoBound),
6489                        "cycle" => opts.cycle = Some(false),
6490                        other => {
6491                            return Err(self.err(alloc::format!(
6492                                "expected MINVALUE / MAXVALUE / CYCLE after NO, got {other:?}"
6493                            )));
6494                        }
6495                    }
6496                }
6497                "start" => {
6498                    self.advance();
6499                    // Optional WITH.
6500                    if matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s)
6501                        if s.eq_ignore_ascii_case("with"))
6502                    {
6503                        self.advance();
6504                    }
6505                    opts.start = Some(self.expect_signed_int()?);
6506                }
6507                "restart" if allow_restart => {
6508                    self.advance();
6509                    // Optional WITH n; bare RESTART means restart at START.
6510                    let mut with_val: Option<i64> = None;
6511                    if matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s)
6512                        if s.eq_ignore_ascii_case("with"))
6513                    {
6514                        self.advance();
6515                        with_val = Some(self.expect_signed_int()?);
6516                    } else if matches!(self.peek(), Token::Integer(_) | Token::Minus) {
6517                        with_val = Some(self.expect_signed_int()?);
6518                    }
6519                    opts.restart = Some(with_val);
6520                }
6521                "cache" => {
6522                    self.advance();
6523                    opts.cache = Some(self.expect_signed_int()?);
6524                }
6525                "cycle" => {
6526                    self.advance();
6527                    opts.cycle = Some(true);
6528                }
6529                "owned" => {
6530                    self.advance();
6531                    match self.peek() {
6532                        Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("by") => {
6533                            self.advance();
6534                        }
6535                        other => {
6536                            return Err(
6537                                self.err(alloc::format!("expected BY after OWNED, got {other:?}"))
6538                            );
6539                        }
6540                    }
6541                    // OWNED BY {NONE | tab.col}. Read just one ident
6542                    // (NOT expect_ident_like which would auto-strip
6543                    // a schema prefix and consume the `.col` we need).
6544                    let first = match self.advance() {
6545                        Token::Ident(s) | Token::QuotedIdent(s) => s,
6546                        other => {
6547                            return Err(self.err(alloc::format!(
6548                                "expected identifier or NONE after OWNED BY, got {other:?}"
6549                            )));
6550                        }
6551                    };
6552                    if first.eq_ignore_ascii_case("none") {
6553                        opts.owned_by = Some(SequenceOwnedBy::None);
6554                    } else if matches!(self.peek(), Token::Dot) {
6555                        self.advance();
6556                        let second = match self.advance() {
6557                            Token::Ident(s) | Token::QuotedIdent(s) => s,
6558                            other => {
6559                                return Err(self.err(alloc::format!(
6560                                    "expected column name after OWNED BY {first}., got {other:?}"
6561                                )));
6562                            }
6563                        };
6564                        // v7.17 dump-compat fix — pg_dump emits
6565                        // OWNED BY clauses as
6566                        // `schema.table.column` (three segments).
6567                        // If a third `.<ident>` follows, treat the
6568                        // first ident as schema (drop it; SPG is
6569                        // single-schema) and the middle / last
6570                        // pair as table.column. Otherwise it's
6571                        // the two-segment form table.column.
6572                        if matches!(self.peek(), Token::Dot) {
6573                            self.advance();
6574                            let third = match self.advance() {
6575                                Token::Ident(s) | Token::QuotedIdent(s) => s,
6576                                other => {
6577                                    return Err(self.err(alloc::format!(
6578                                        "expected column name after OWNED BY {first}.{second}., got {other:?}"
6579                                    )));
6580                                }
6581                            };
6582                            let _ = first; // schema prefix discarded
6583                            opts.owned_by = Some(SequenceOwnedBy::Column {
6584                                table: second,
6585                                column: third,
6586                            });
6587                        } else {
6588                            opts.owned_by = Some(SequenceOwnedBy::Column {
6589                                table: first,
6590                                column: second,
6591                            });
6592                        }
6593                    } else {
6594                        return Err(self.err(alloc::format!(
6595                            "expected table.column or NONE after OWNED BY, got {first:?}"
6596                        )));
6597                    }
6598                }
6599                _ => break,
6600            }
6601        }
6602        Ok(opts)
6603    }
6604
6605    fn expect_signed_int(&mut self) -> Result<i64, ParseError> {
6606        let neg = if matches!(self.peek(), Token::Minus) {
6607            self.advance();
6608            true
6609        } else {
6610            false
6611        };
6612        match self.peek() {
6613            Token::Integer(n) => {
6614                let v = *n;
6615                self.advance();
6616                Ok(if neg { -v } else { v })
6617            }
6618            other => Err(self.err(alloc::format!("expected signed integer, got {other:?}"))),
6619        }
6620    }
6621
6622    /// v7.17.0 Phase 3.1 — absorb `[NOT] DEFERRABLE [INITIALLY
6623    /// {DEFERRED | IMMEDIATE}]` constraint-timing clauses. Each
6624    /// clause is fully accepted and discarded — SPG always runs
6625    /// constraint checks immediately (single-writer model). The
6626    /// loop allows DEFERRABLE and the INITIALLY suffix to appear
6627    /// in either order (per the SQL spec they're independent),
6628    /// though pg_dump always emits them in the canonical
6629    /// `[NOT] DEFERRABLE INITIALLY {DEFERRED|IMMEDIATE}` shape.
6630    /// Stops at the first token that isn't part of the clause.
6631    fn consume_optional_deferrable_clauses(&mut self) -> Result<(), ParseError> {
6632        self.consume_deferrable_clauses_timed().map(|_| ())
6633    }
6634
6635    /// v7.39 (round 288) — the same scan, but reporting what it saw:
6636    /// `(deferrable, initially_deferred)`. The clauses were parsed and
6637    /// dropped, so `DEFERRABLE INITIALLY DEFERRED` on an FK behaved as
6638    /// NOT DEFERRABLE and a circular-FK migration could not load.
6639    fn consume_deferrable_clauses_timed(&mut self) -> Result<(bool, bool), ParseError> {
6640        let mut deferrable = false;
6641        let mut initially_deferred = false;
6642        loop {
6643            // Bare `DEFERRABLE` (Phase 3.1 — was hard-error pre-3.1).
6644            if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("deferrable")) {
6645                self.advance();
6646                deferrable = true;
6647                if self.consume_optional_initially_clause()? {
6648                    initially_deferred = true;
6649                }
6650                continue;
6651            }
6652            // `NOT DEFERRABLE` — already worked pre-3.1.
6653            if matches!(self.peek(), Token::Not) {
6654                let look = self.tokens.get(self.pos + 1);
6655                if matches!(look, Some(Token::Ident(s)) if s.eq_ignore_ascii_case("deferrable")) {
6656                    self.advance(); // NOT
6657                    self.advance(); // DEFERRABLE
6658                    deferrable = false;
6659                    initially_deferred = false;
6660                    let _ = self.consume_optional_initially_clause()?;
6661                    continue;
6662                }
6663                break;
6664            }
6665            // Standalone `INITIALLY {DEFERRED|IMMEDIATE}` — PG
6666            // accepts this without a leading [NOT] DEFERRABLE
6667            // (the timing keyword alone). pg_dump occasionally
6668            // emits it on FK constraints that inherit timing.
6669            if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("initially")) {
6670                if self.consume_optional_initially_clause()? {
6671                    initially_deferred = true;
6672                    // PG: a bare `INITIALLY DEFERRED` implies DEFERRABLE.
6673                    deferrable = true;
6674                }
6675                continue;
6676            }
6677            break;
6678        }
6679        Ok((deferrable, initially_deferred))
6680    }
6681
6682    /// Helper for [`consume_optional_deferrable_clauses`]. When the
6683    /// next token is `INITIALLY`, consume it plus the required
6684    /// `DEFERRED` | `IMMEDIATE` trailer. No-op otherwise.
6685    /// Returns true when the timing seen was `DEFERRED`.
6686    fn consume_optional_initially_clause(&mut self) -> Result<bool, ParseError> {
6687        if !matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("initially")) {
6688            return Ok(false);
6689        }
6690        self.advance(); // INITIALLY
6691        match self.advance() {
6692            Token::Ident(s)
6693                if s.eq_ignore_ascii_case("deferred") || s.eq_ignore_ascii_case("immediate") =>
6694            {
6695                Ok(s.eq_ignore_ascii_case("deferred"))
6696            }
6697            other => Err(self.err(alloc::format!(
6698                "expected DEFERRED or IMMEDIATE after INITIALLY, got {other:?}"
6699            ))),
6700        }
6701    }
6702
6703    /// v7.17.0 Phase 4.2 — consume a MySQL `CREATE PROCEDURE` body
6704    /// in its entirety so the parser returns Empty without
6705    /// touching the runtime. The CREATE+PROCEDURE keywords are
6706    /// already consumed; this swallows everything from the
6707    /// procedure name through the matching `END`, including
6708    /// nested `BEGIN`/`END` blocks, internal `;` terminators
6709    /// (DELIMITER `//` makes the script splitter forward the
6710    /// whole block as one statement), `@var` session-variable
6711    /// references, and the trailing terminator.
6712    ///
6713    /// Tracks nesting depth so:
6714    ///   BEGIN
6715    ///     IF cond THEN
6716    ///       BEGIN ... END;
6717    ///     END IF;
6718    ///   END
6719    /// terminates at the outer END.
6720    fn consume_mysql_routine_body(&mut self) {
6721        // Outer skeleton: name, (...), optional clauses, BEGIN
6722        // <body> END [;]. Scan for the first BEGIN — anything
6723        // before it is signature decoration we don't care about.
6724        // Once inside BEGIN, count up on BEGIN, down on END.
6725        let mut depth: i32 = 0;
6726        let mut started = false;
6727        loop {
6728            match self.peek().clone() {
6729                Token::Begin => {
6730                    self.advance();
6731                    depth += 1;
6732                    started = true;
6733                }
6734                Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("end") => {
6735                    self.advance();
6736                    if started {
6737                        depth -= 1;
6738                        if depth <= 0 {
6739                            // Optional trailing ident (`END IF`,
6740                            // `END LOOP`, `END WHILE`, `END CASE`,
6741                            // `END label_name`) — eat the next
6742                            // ident if present so we don't
6743                            // mistake `END IF;` for the outer
6744                            // close.
6745                            if matches!(self.peek(), Token::Ident(_) | Token::QuotedIdent(_)) {
6746                                // If the next token is one of the
6747                                // PL/SQL block-closer keywords,
6748                                // the END belongs to an inner
6749                                // block; bump depth back up.
6750                                let is_inner_close = matches!(
6751                                    self.peek(),
6752                                    Token::Ident(s) | Token::QuotedIdent(s)
6753                                        if matches!(
6754                                            s.to_ascii_lowercase().as_str(),
6755                                            "if" | "loop" | "while" | "case" | "repeat"
6756                                        )
6757                                );
6758                                if is_inner_close {
6759                                    self.advance();
6760                                    depth += 1;
6761                                    continue;
6762                                }
6763                            }
6764                            // Eat optional trailing `;`.
6765                            if matches!(self.peek(), Token::Semicolon) {
6766                                self.advance();
6767                            }
6768                            return;
6769                        }
6770                    }
6771                }
6772                Token::Eof => return,
6773                _ => {
6774                    self.advance();
6775                }
6776            }
6777        }
6778    }
6779
6780    /// v7.17.0 Phase 2.6 — absorb the MySQL view-prefix clauses
6781    /// that appear between `CREATE` and `VIEW` in mysqldump output:
6782    ///
6783    /// * `ALGORITHM = {UNDEFINED|MERGE|TEMPTABLE}`
6784    /// * `DEFINER = <user>`  (user may be a quoted string, a bare
6785    ///   ident, or `ident @ ident-or-quoted-string` host form)
6786    /// * `SQL SECURITY {DEFINER|INVOKER}`
6787    ///
6788    /// Each clause may appear at most once but in any order.
6789    /// The hints are pure planner / permission metadata that
6790    /// SPG's view-rewrite engine handles uniformly; we accept
6791    /// and discard. Returns `Ok(())` once a non-clause token is
6792    /// peeked (the caller then checks for the `VIEW` keyword).
6793    fn consume_mysql_view_prefix(&mut self) -> Result<(), ParseError> {
6794        loop {
6795            match self.peek().clone() {
6796                Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("algorithm") => {
6797                    self.advance(); // ALGORITHM
6798                    // Optional `=`. MySQL spec requires it but be
6799                    // generous.
6800                    if matches!(self.peek(), Token::Eq) {
6801                        self.advance();
6802                    }
6803                    // UNDEFINED / MERGE / TEMPTABLE — accept any
6804                    // bare ident; unknown values still parse so
6805                    // future MySQL versions don't break.
6806                    if matches!(
6807                        self.peek(),
6808                        Token::Ident(_) | Token::QuotedIdent(_) | Token::String(_)
6809                    ) {
6810                        self.advance();
6811                    }
6812                }
6813                Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("definer") => {
6814                    self.advance(); // DEFINER
6815                    if matches!(self.peek(), Token::Eq) {
6816                        self.advance();
6817                    }
6818                    // User: quoted string, ident, OR ident @ host
6819                    // (host may itself be quoted or bare).
6820                    match self.peek().clone() {
6821                        Token::String(_) | Token::Ident(_) | Token::QuotedIdent(_) => {
6822                            self.advance();
6823                            // Optional `@host`.
6824                            if matches!(self.peek(), Token::At) {
6825                                self.advance();
6826                                if matches!(
6827                                    self.peek(),
6828                                    Token::Ident(_) | Token::QuotedIdent(_) | Token::String(_)
6829                                ) {
6830                                    self.advance();
6831                                }
6832                            }
6833                        }
6834                        _ => {}
6835                    }
6836                }
6837                Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("sql") => {
6838                    // `SQL SECURITY {DEFINER|INVOKER}`. Only honoured
6839                    // when followed by SECURITY — the dispatcher must
6840                    // not consume a bare `SQL` token (it's not a
6841                    // legal CREATE prefix on its own).
6842                    let save = self.pos;
6843                    self.advance(); // SQL
6844                    if matches!(self.peek(), Token::Ident(s2) | Token::QuotedIdent(s2)
6845                        if s2.eq_ignore_ascii_case("security"))
6846                    {
6847                        self.advance(); // SECURITY
6848                        // DEFINER / INVOKER trailing ident.
6849                        if matches!(self.peek(), Token::Ident(_) | Token::QuotedIdent(_)) {
6850                            self.advance();
6851                        }
6852                    } else {
6853                        // Not a SQL SECURITY clause — roll back and
6854                        // bail; the caller will error out cleanly.
6855                        self.pos = save;
6856                        return Ok(());
6857                    }
6858                }
6859                _ => return Ok(()),
6860            }
6861        }
6862    }
6863
6864    fn parse_if_not_exists(&mut self) -> bool {
6865        if matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("if"))
6866        {
6867            let save = self.pos;
6868            self.advance();
6869            if matches!(self.peek(), Token::Not) {
6870                self.advance();
6871                if matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("exists"))
6872                {
6873                    self.advance();
6874                    return true;
6875                }
6876            }
6877            self.pos = save;
6878        }
6879        false
6880    }
6881
6882    fn parse_if_exists(&mut self) -> bool {
6883        if matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("if"))
6884        {
6885            let save = self.pos;
6886            self.advance();
6887            if matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("exists"))
6888            {
6889                self.advance();
6890                return true;
6891            }
6892            self.pos = save;
6893        }
6894        false
6895    }
6896
6897    /// v7.12.4 — body of `CREATE [OR REPLACE] TRIGGER`. The
6898    /// `[OR REPLACE]` flag and the `TRIGGER` keyword have already
6899    /// been consumed.
6900    fn parse_create_trigger_after_keyword(
6901        &mut self,
6902        or_replace: bool,
6903    ) -> Result<Statement, ParseError> {
6904        let name = self.expect_ident_like()?;
6905        let timing = {
6906            let ident = self.expect_ident_like()?;
6907            if ident.eq_ignore_ascii_case("before") {
6908                TriggerTiming::Before
6909            } else if ident.eq_ignore_ascii_case("after") {
6910                TriggerTiming::After
6911            } else if ident.eq_ignore_ascii_case("instead") {
6912                let next = self.expect_ident_like()?;
6913                if !next.eq_ignore_ascii_case("of") {
6914                    return Err(self.err(alloc::format!(
6915                        "expected OF after INSTEAD in trigger timing, got {next:?}"
6916                    )));
6917                }
6918                TriggerTiming::InsteadOf
6919            } else {
6920                return Err(self.err(alloc::format!(
6921                    "expected BEFORE / AFTER / INSTEAD OF in trigger timing, got {ident:?}"
6922                )));
6923            }
6924        };
6925        // Events: INSERT [ OR UPDATE [ OR DELETE [ OR TRUNCATE ] ] ].
6926        // OR is a reserved keyword token (Token::Or), not an Ident.
6927        // v7.13.0 — after an UPDATE event we may optionally see
6928        // `OF col, col, …` (mailrs round-5 G7). Columns are
6929        // captured into `update_columns` once across the whole
6930        // events list; multiple `UPDATE OF` clauses are rejected.
6931        let mut events: Vec<TriggerEvent> = Vec::new();
6932        let mut update_columns: Vec<String> = Vec::new();
6933        let (first_ev, first_cols) = self.parse_trigger_event_with_optional_of()?;
6934        events.push(first_ev);
6935        if !first_cols.is_empty() {
6936            update_columns = first_cols;
6937        }
6938        while matches!(self.peek(), Token::Or) {
6939            self.advance();
6940            let (ev, cols) = self.parse_trigger_event_with_optional_of()?;
6941            events.push(ev);
6942            if !cols.is_empty() {
6943                if !update_columns.is_empty() {
6944                    return Err(
6945                        self.err("CREATE TRIGGER: `UPDATE OF cols` may appear at most once".into())
6946                    );
6947                }
6948                update_columns = cols;
6949            }
6950        }
6951        // ON <table>
6952        let tok = self.peek();
6953        let Token::On = tok else {
6954            return Err(self.err(alloc::format!(
6955                "expected ON after trigger events, got {tok:?}"
6956            )));
6957        };
6958        self.advance();
6959        let table = self.expect_ident_like()?;
6960        // v7.39 (read01 round 82) — a CONSTRAINT TRIGGER may carry `FROM
6961        // reftable` and `[NOT] DEFERRABLE [INITIALLY {DEFERRED|IMMEDIATE}]`
6962        // between the table and FOR EACH ROW. Accept and skip them: SPG fires
6963        // the trigger as a plain AFTER trigger (correct for every non-deferred
6964        // use; deferral timing is not yet honoured).
6965        if matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s)
6966            if s.eq_ignore_ascii_case("from"))
6967        {
6968            self.advance();
6969            let _reftable = self.expect_ident_like()?;
6970        }
6971        self.consume_optional_deferrable_clauses()?;
6972        // FOR EACH ROW / FOR EACH STATEMENT. FOR is a reserved
6973        // keyword (Token::For); EACH / ROW / STATEMENT are bare
6974        // idents.
6975        if !matches!(self.peek(), Token::For) {
6976            return Err(self.err(alloc::format!(
6977                "expected FOR EACH ROW / STATEMENT, got {:?}",
6978                self.peek()
6979            )));
6980        }
6981        self.advance();
6982        let for_each = {
6983            let e = self.expect_ident_like()?;
6984            if !e.eq_ignore_ascii_case("each") {
6985                return Err(self.err(alloc::format!("expected EACH after FOR, got {e:?}")));
6986            }
6987            let unit = self.expect_ident_like()?;
6988            if unit.eq_ignore_ascii_case("row") {
6989                TriggerForEach::Row
6990            } else if unit.eq_ignore_ascii_case("statement") {
6991                TriggerForEach::Statement
6992            } else {
6993                return Err(self.err(alloc::format!(
6994                    "expected ROW / STATEMENT after FOR EACH, got {unit:?}"
6995                )));
6996            }
6997        };
6998        // v7.39 (round 138) — optional `WHEN ( condition )` before EXECUTE.
6999        let when_condition = if matches!(self.peek(),
7000            Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("when"))
7001        {
7002            self.advance();
7003            Some(self.parse_paren_expr("WHEN")?)
7004        } else {
7005            None
7006        };
7007        // EXECUTE FUNCTION/PROCEDURE name(...)
7008        let exec = self.expect_ident_like()?;
7009        if !exec.eq_ignore_ascii_case("execute") {
7010            return Err(self.err(alloc::format!(
7011                "expected EXECUTE FUNCTION/PROCEDURE in CREATE TRIGGER, got {exec:?}"
7012            )));
7013        }
7014        let fn_or_proc = self.expect_ident_like()?;
7015        if !(fn_or_proc.eq_ignore_ascii_case("function")
7016            || fn_or_proc.eq_ignore_ascii_case("procedure"))
7017        {
7018            return Err(self.err(alloc::format!(
7019                "expected FUNCTION / PROCEDURE after EXECUTE, got {fn_or_proc:?}"
7020            )));
7021        }
7022        let function = self.expect_ident_like()?;
7023        // Optional empty arg list `()`.
7024        if matches!(self.peek(), Token::LParen) {
7025            self.advance();
7026            if !matches!(self.peek(), Token::RParen) {
7027                return Err(self.err(alloc::format!(
7028                    "v7.12.4 trigger function calls take no args; got {:?}",
7029                    self.peek()
7030                )));
7031            }
7032            self.advance();
7033        }
7034        Ok(Statement::CreateTrigger(CreateTriggerStatement {
7035            name,
7036            or_replace,
7037            timing,
7038            events,
7039            table,
7040            for_each,
7041            function,
7042            update_columns,
7043            when_condition,
7044        }))
7045    }
7046
7047    /// v7.39 (round 139) — `CREATE RULE <name> AS ON <event> TO <table>
7048    /// [WHERE <cond>] DO [ALSO|INSTEAD] { NOTHING | cmd | ( cmd; … ) }`.
7049    fn parse_create_rule_after_keyword(
7050        &mut self,
7051        or_replace: bool,
7052    ) -> Result<Statement, ParseError> {
7053        let name = self.expect_ident_like()?;
7054        if !matches!(self.peek(), Token::As) {
7055            return Err(self.err(alloc::format!(
7056                "expected AS in CREATE RULE, got {:?}",
7057                self.peek()
7058            )));
7059        }
7060        self.advance();
7061        if !matches!(self.peek(), Token::On) {
7062            return Err(self.err(alloc::format!(
7063                "expected ON in CREATE RULE, got {:?}",
7064                self.peek()
7065            )));
7066        }
7067        self.advance();
7068        let event = self.parse_rule_event()?;
7069        if !matches!(self.peek(), Token::To)
7070            && !matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("to"))
7071        {
7072            return Err(self.err(alloc::format!(
7073                "expected TO after rule event, got {:?}",
7074                self.peek()
7075            )));
7076        }
7077        self.advance();
7078        let table = self.expect_ident_like()?;
7079        // Optional `WHERE <cond>` (no parentheses, unlike a trigger WHEN).
7080        let when_condition = if matches!(self.peek(), Token::Where) {
7081            self.advance();
7082            Some(self.parse_expr(0)?)
7083        } else {
7084            None
7085        };
7086        if !matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("do"))
7087        {
7088            return Err(self.err(alloc::format!(
7089                "expected DO in CREATE RULE, got {:?}",
7090                self.peek()
7091            )));
7092        }
7093        self.advance();
7094        // `DO [ ALSO | INSTEAD ]` — ALSO is the default when neither is written.
7095        let instead = if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("instead"))
7096        {
7097            self.advance();
7098            true
7099        } else if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("also")) {
7100            self.advance();
7101            false
7102        } else {
7103            false
7104        };
7105        // `NOTHING` | `( cmd; … )` | `cmd`.
7106        let commands = if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("nothing"))
7107        {
7108            self.advance();
7109            Vec::new()
7110        } else if matches!(self.peek(), Token::LParen) {
7111            self.advance();
7112            let mut cmds = Vec::new();
7113            loop {
7114                cmds.push(self.parse_one_statement()?);
7115                if matches!(self.peek(), Token::Semicolon) {
7116                    self.advance();
7117                    if matches!(self.peek(), Token::RParen) {
7118                        break;
7119                    }
7120                    continue;
7121                }
7122                break;
7123            }
7124            if !matches!(self.peek(), Token::RParen) {
7125                return Err(self.err(alloc::format!(
7126                    "expected ) closing the CREATE RULE command list, got {:?}",
7127                    self.peek()
7128                )));
7129            }
7130            self.advance();
7131            cmds
7132        } else {
7133            alloc::vec![self.parse_one_statement()?]
7134        };
7135        Ok(Statement::CreateRule(crate::ast::CreateRuleStatement {
7136            name,
7137            or_replace,
7138            event,
7139            table,
7140            instead,
7141            when_condition,
7142            commands,
7143        }))
7144    }
7145
7146    /// v7.39 (round 139) — a rule event keyword → uppercase string.
7147    fn parse_rule_event(&mut self) -> Result<alloc::string::String, ParseError> {
7148        if matches!(self.peek(), Token::Insert) {
7149            self.advance();
7150            return Ok(alloc::string::String::from("INSERT"));
7151        }
7152        if matches!(self.peek(), Token::Select) {
7153            self.advance();
7154            return Ok(alloc::string::String::from("SELECT"));
7155        }
7156        match self.peek() {
7157            Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("update") => {
7158                self.advance();
7159                Ok(alloc::string::String::from("UPDATE"))
7160            }
7161            Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("delete") => {
7162                self.advance();
7163                Ok(alloc::string::String::from("DELETE"))
7164            }
7165            other => Err(self.err(alloc::format!(
7166                "expected INSERT / UPDATE / DELETE / SELECT in CREATE RULE, got {other:?}"
7167            ))),
7168        }
7169    }
7170
7171    /// v7.13.0 — parse one trigger event, then optionally consume
7172    /// `OF col, col, …` after `UPDATE` (mailrs round-5 G7). Other
7173    /// events (INSERT/DELETE/TRUNCATE) don't accept the OF tail.
7174    fn parse_trigger_event_with_optional_of(
7175        &mut self,
7176    ) -> Result<(TriggerEvent, Vec<String>), ParseError> {
7177        let ev = self.parse_trigger_event()?;
7178        if !matches!(ev, TriggerEvent::Update) {
7179            return Ok((ev, Vec::new()));
7180        }
7181        // `OF` is a bare ident.
7182        if !matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("of")) {
7183            return Ok((ev, Vec::new()));
7184        }
7185        self.advance(); // OF
7186        let mut cols: Vec<String> = Vec::new();
7187        loop {
7188            cols.push(self.expect_ident_like()?);
7189            if matches!(self.peek(), Token::Comma) {
7190                self.advance();
7191                continue;
7192            }
7193            break;
7194        }
7195        if cols.is_empty() {
7196            return Err(
7197                self.err("CREATE TRIGGER: `UPDATE OF` requires at least one column name".into())
7198            );
7199        }
7200        Ok((ev, cols))
7201    }
7202
7203    /// v7.12.4 — `BEGIN stmt; stmt; … END[;]` PL/pgSQL block.
7204    /// v7.12.6 — optional `DECLARE var TYPE [:= init];` prelude
7205    /// before `BEGIN`, and IF / RAISE / embedded SQL statements
7206    /// inside the body.
7207    /// Called by [`parse_plpgsql_body`] after the body's tokens
7208    /// have been lexed into this temporary parser.
7209    pub(crate) fn parse_plpgsql_block(&mut self) -> Result<PlPgSqlBlock, ParseError> {
7210        // v7.12.6 — optional DECLARE prelude.
7211        let declarations = if matches!(
7212            self.peek(),
7213            Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("declare")
7214        ) {
7215            self.advance();
7216            self.parse_plpgsql_declare_block()?
7217        } else {
7218            Vec::new()
7219        };
7220        // BEGIN keyword (PL/pgSQL — distinct from the SQL
7221        // `BEGIN` transaction-start, but we can reuse the
7222        // reserved Token::Begin since the body is a separate
7223        // lex/parse context).
7224        if !matches!(self.peek(), Token::Begin) {
7225            return Err(self.err(alloc::format!(
7226                "expected BEGIN at start of plpgsql block, got {:?}",
7227                self.peek()
7228            )));
7229        }
7230        self.advance();
7231        let statements = self.parse_plpgsql_stmt_list_until_end()?;
7232        // v7.37.20 (20.10) — optional EXCEPTION clause between the
7233        // body's last statement and the trailing END. When present
7234        // it's a series of `WHEN <cond> [OR <cond>]* THEN <body>`
7235        // arms terminated by END.
7236        let exception_handlers = if matches!(
7237            self.peek(),
7238            Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("exception")
7239        ) {
7240            self.advance();
7241            self.parse_plpgsql_exception_handlers()?
7242        } else {
7243            Vec::new()
7244        };
7245        Ok(PlPgSqlBlock {
7246            declarations,
7247            statements,
7248            exception_handlers,
7249        })
7250    }
7251
7252    /// v7.37.20 (20.10) — parse EXCEPTION handlers `WHEN <cond>
7253    /// [OR <cond>]* THEN <body>` sequence up to the trailing END.
7254    fn parse_plpgsql_exception_handlers(
7255        &mut self,
7256    ) -> Result<Vec<crate::ast::ExceptionHandler>, ParseError> {
7257        let mut out: Vec<crate::ast::ExceptionHandler> = Vec::new();
7258        loop {
7259            // Stop at END — the block-level trailing END LOOP / END;
7260            // is handled by the caller.
7261            if matches!(
7262                self.peek(),
7263                Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("end")
7264            ) {
7265                return Ok(out);
7266            }
7267            // WHEN <cond> [OR <cond>]* THEN <body>
7268            if !matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("when"))
7269            {
7270                return Err(self.err(alloc::format!(
7271                    "expected WHEN or END inside EXCEPTION clause, got {:?}",
7272                    self.peek()
7273                )));
7274            }
7275            self.advance();
7276            let mut conditions: Vec<String> = Vec::new();
7277            conditions.push(self.expect_ident_like()?);
7278            while matches!(self.peek(), Token::Or) {
7279                self.advance();
7280                conditions.push(self.expect_ident_like()?);
7281            }
7282            let then_kw = self.expect_ident_like()?;
7283            if !then_kw.eq_ignore_ascii_case("then") {
7284                return Err(self.err(alloc::format!(
7285                    "expected THEN after WHEN condition list, got {then_kw:?}"
7286                )));
7287            }
7288            let body = self.parse_plpgsql_stmt_list_until_end()?;
7289            out.push(crate::ast::ExceptionHandler { conditions, body });
7290        }
7291    }
7292
7293    /// v7.12.6 — parse the `DECLARE ... [var TYPE [:= init];]+`
7294    /// prelude. Caller has already consumed `DECLARE`. We stop
7295    /// reading entries when we hit `BEGIN`.
7296    fn parse_plpgsql_declare_block(&mut self) -> Result<Vec<PlPgSqlDeclare>, ParseError> {
7297        let mut out: Vec<PlPgSqlDeclare> = Vec::new();
7298        loop {
7299            if matches!(self.peek(), Token::Begin) {
7300                return Ok(out);
7301            }
7302            let name = self.expect_ident_like()?;
7303            // v7.37.20 (20.7) — type inference: if the next token is
7304            // `:=` or `=` (no explicit type), infer from the default
7305            // expression. Otherwise the ident that follows is the
7306            // declared type.
7307            //
7308            // v7.37.20 (20.8) — `<table>.<col>%TYPE` / `<table>%ROWTYPE`
7309            // (PG-standard). SPG parse-accepts and treats identically
7310            // to inference — the eventual runtime value determines
7311            // the local's type, which is faithful to how SPG handles
7312            // untyped locals today (see 20.7). Full compile-time
7313            // catalog lookup queues with v7.40 PL/pgSQL epic.
7314            let ty = if matches!(self.peek(), Token::ColonEq | Token::Eq) {
7315                // Sentinel: `FunctionArgType::Raw("_infer_")` tells the
7316                // downstream declaration walker to type the local by
7317                // the runtime type of the default expression.
7318                FunctionArgType::Raw("_infer_".into())
7319            } else {
7320                let ty_token = self.expect_ident_like()?;
7321                // Detect `<ident>[.<ident>][%TYPE | %ROWTYPE]`:
7322                // consume optional `.<ident>` qualifier + `%<KW>`
7323                // suffix. Both qualifier and suffix map to _infer_.
7324                if matches!(self.peek(), Token::Dot) {
7325                    self.advance();
7326                    let _ = self.expect_ident_like()?;
7327                }
7328                if matches!(self.peek(), Token::Percent) {
7329                    self.advance();
7330                    // Consume the trailing TYPE / ROWTYPE ident.
7331                    let _ = self.expect_ident_like()?;
7332                    FunctionArgType::Raw("_infer_".into())
7333                } else {
7334                    match map_type_ident_to_column_type_name(&ty_token) {
7335                        Some(t) => FunctionArgType::Typed(t),
7336                        None => FunctionArgType::Raw(ty_token),
7337                    }
7338                }
7339            };
7340            let default = match self.peek() {
7341                Token::ColonEq => {
7342                    self.advance();
7343                    Some(self.parse_expr(0)?)
7344                }
7345                Token::Eq => {
7346                    // PL/pgSQL also accepts `=` for the
7347                    // DECLARE default (PG treats them the same
7348                    // in this position).
7349                    self.advance();
7350                    Some(self.parse_expr(0)?)
7351                }
7352                _ => None,
7353            };
7354            // Mandatory `;` between declarations.
7355            if !matches!(self.peek(), Token::Semicolon) {
7356                return Err(self.err(alloc::format!(
7357                    "expected ; after DECLARE entry for {name:?}, got {:?}",
7358                    self.peek()
7359                )));
7360            }
7361            self.advance();
7362            out.push(PlPgSqlDeclare { name, ty, default });
7363        }
7364    }
7365
7366    /// v7.12.6 — parse PL/pgSQL statements up to (and consuming)
7367    /// the terminating `END;` (or `END IF;` etc — handled by the
7368    /// per-construct sub-parsers). Used by both the outer block
7369    /// and the IF/ELSE branch bodies.
7370    fn parse_plpgsql_stmt_list_until_end(&mut self) -> Result<Vec<PlPgSqlStmt>, ParseError> {
7371        let mut statements: Vec<PlPgSqlStmt> = Vec::new();
7372        loop {
7373            // Allow trailing semicolons + END.
7374            while matches!(self.peek(), Token::Semicolon) {
7375                self.advance();
7376            }
7377            // END / ELSE / ELSIF / EXCEPTION — handled by the caller.
7378            if matches!(
7379                self.peek(),
7380                Token::Ident(s) | Token::QuotedIdent(s)
7381                    if s.eq_ignore_ascii_case("end")
7382                        || s.eq_ignore_ascii_case("else")
7383                        || s.eq_ignore_ascii_case("elsif")
7384                        || s.eq_ignore_ascii_case("elseif")
7385                        || s.eq_ignore_ascii_case("exception")
7386                        || s.eq_ignore_ascii_case("when")
7387            ) {
7388                return Ok(statements);
7389            }
7390            // Otherwise: one statement, then expect `;` or
7391            // a block-terminator keyword.
7392            let stmt = self.parse_plpgsql_stmt()?;
7393            statements.push(stmt);
7394            match self.peek() {
7395                Token::Semicolon => {
7396                    self.advance();
7397                }
7398                Token::Ident(s) | Token::QuotedIdent(s)
7399                    if s.eq_ignore_ascii_case("end")
7400                        || s.eq_ignore_ascii_case("else")
7401                        || s.eq_ignore_ascii_case("elsif")
7402                        || s.eq_ignore_ascii_case("elseif")
7403                        || s.eq_ignore_ascii_case("exception")
7404                        || s.eq_ignore_ascii_case("when") =>
7405                {
7406                    // Final statement of the block without `;`.
7407                }
7408                other => {
7409                    return Err(self.err(alloc::format!(
7410                        "expected ; or END/ELSE/ELSIF after plpgsql statement, got {other:?}"
7411                    )));
7412                }
7413            }
7414        }
7415    }
7416
7417    fn parse_plpgsql_stmt(&mut self) -> Result<PlPgSqlStmt, ParseError> {
7418        // RETURN keyword?
7419        if matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("return"))
7420        {
7421            self.advance();
7422            return self.parse_plpgsql_return();
7423        }
7424        // v7.12.6 — IF block.
7425        if matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("if"))
7426        {
7427            self.advance();
7428            return self.parse_plpgsql_if();
7429        }
7430        // v7.37.20 (20.6) — FOR <var> IN EXECUTE <string_expr> LOOP.
7431        // Detected by peeking that token pos+3 is Ident("execute").
7432        if matches!(self.peek(), Token::For)
7433            && matches!(
7434                self.tokens.get(self.pos + 1),
7435                Some(Token::Ident(_) | Token::QuotedIdent(_))
7436            )
7437            && matches!(self.tokens.get(self.pos + 2), Some(Token::In))
7438            && matches!(
7439                self.tokens.get(self.pos + 3),
7440                Some(Token::Ident(s) | Token::QuotedIdent(s)) if s.eq_ignore_ascii_case("execute")
7441            )
7442        {
7443            self.advance(); // FOR
7444            let var = self.expect_ident_like()?;
7445            self.advance(); // IN
7446            self.advance(); // EXECUTE
7447            // Prescan for LOOP at paren depth 0 so parse_expr stops
7448            // before the LOOP keyword (same trick as the bare-SELECT
7449            // ForQuery arm).
7450            let mut depth: i32 = 0;
7451            let mut loop_pos: Option<usize> = None;
7452            let mut scan = self.pos;
7453            while scan < self.tokens.len() {
7454                match self.tokens.get(scan) {
7455                    Some(Token::LParen) => depth += 1,
7456                    Some(Token::RParen) => depth -= 1,
7457                    Some(Token::Ident(s) | Token::QuotedIdent(s))
7458                        if depth == 0 && s.eq_ignore_ascii_case("loop") =>
7459                    {
7460                        loop_pos = Some(scan);
7461                        break;
7462                    }
7463                    _ => {}
7464                }
7465                scan += 1;
7466            }
7467            let loop_pos = loop_pos.ok_or_else(|| {
7468                self.err(alloc::format!(
7469                    "FOR <var> IN EXECUTE <expr> ... LOOP: no LOOP keyword found"
7470                ))
7471            })?;
7472            let saved_loop = self.tokens[loop_pos].clone();
7473            self.tokens[loop_pos] = Token::Semicolon;
7474            let expr_result = self.parse_expr(0);
7475            self.tokens[loop_pos] = saved_loop;
7476            let sql_expr = expr_result?;
7477            let loop_kw = self.expect_ident_like()?;
7478            if !loop_kw.eq_ignore_ascii_case("loop") {
7479                return Err(self.err(alloc::format!(
7480                    "expected LOOP after FOR <var> IN EXECUTE <expr>, got {loop_kw:?}"
7481                )));
7482            }
7483            let body = self.parse_plpgsql_stmt_list_until_end()?;
7484            let end_kw = self.expect_ident_like()?;
7485            if !end_kw.eq_ignore_ascii_case("end") {
7486                return Err(self.err(alloc::format!(
7487                    "expected END LOOP after FOR IN EXECUTE body, got {end_kw:?}"
7488                )));
7489            }
7490            let loop_kw2 = self.expect_ident_like()?;
7491            if !loop_kw2.eq_ignore_ascii_case("loop") {
7492                return Err(self.err(alloc::format!(
7493                    "expected END LOOP after FOR IN EXECUTE body, got END {loop_kw2:?}"
7494                )));
7495            }
7496            return Ok(PlPgSqlStmt::ForExecute {
7497                var,
7498                sql_expr,
7499                body,
7500            });
7501        }
7502        // v7.37.20 (20.5) — FOR <var> IN <SELECT> LOOP.
7503        //
7504        // Two syntactic forms:
7505        //   FOR var IN SELECT ... ORDER BY ... LOOP ...
7506        //   FOR var IN (SELECT ...) LOOP ...
7507        //
7508        // Bare-SELECT form: to keep parse_select_stmt from swallowing
7509        // the trailing `LOOP` keyword as a table alias, we prescan
7510        // forward to find LOOP at paren depth 0, splice a fake
7511        // Semicolon at that position (so SELECT parses cleanly),
7512        // then re-splice LOOP back in.
7513        //
7514        // Paren-wrapped form: parse `(` `SELECT ...` `)` then expect
7515        // LOOP directly — no scan required.
7516        if matches!(self.peek(), Token::For)
7517            && matches!(
7518                self.tokens.get(self.pos + 1),
7519                Some(Token::Ident(_) | Token::QuotedIdent(_))
7520            )
7521            && matches!(self.tokens.get(self.pos + 2), Some(Token::In))
7522            && (matches!(self.tokens.get(self.pos + 3), Some(Token::Select))
7523                || matches!(self.tokens.get(self.pos + 3), Some(Token::LParen)))
7524        {
7525            self.advance(); // FOR
7526            let var = self.expect_ident_like()?;
7527            // IN
7528            self.advance();
7529            let query = if matches!(self.peek(), Token::LParen) {
7530                // Paren-wrapped SELECT.
7531                self.advance();
7532                let inner = self.parse_select_stmt()?;
7533                let Statement::Select(q) = inner else {
7534                    return Err(self.err(alloc::format!(
7535                        "expected SELECT inside (…), got {:?}",
7536                        self.peek()
7537                    )));
7538                };
7539                if !matches!(self.peek(), Token::RParen) {
7540                    return Err(self.err(alloc::format!(
7541                        "expected ')' after FOR-IN-SELECT body, got {:?}",
7542                        self.peek()
7543                    )));
7544                }
7545                self.advance();
7546                q
7547            } else {
7548                // Bare SELECT: prescan to find the LOOP boundary.
7549                let mut depth: i32 = 0;
7550                let mut loop_pos: Option<usize> = None;
7551                let mut scan = self.pos;
7552                while scan < self.tokens.len() {
7553                    match self.tokens.get(scan) {
7554                        Some(Token::LParen) => depth += 1,
7555                        Some(Token::RParen) => depth -= 1,
7556                        Some(Token::Ident(s) | Token::QuotedIdent(s))
7557                            if depth == 0 && s.eq_ignore_ascii_case("loop") =>
7558                        {
7559                            loop_pos = Some(scan);
7560                            break;
7561                        }
7562                        _ => {}
7563                    }
7564                    scan += 1;
7565                }
7566                let loop_pos = loop_pos.ok_or_else(|| {
7567                    self.err(alloc::format!(
7568                        "FOR <var> IN <SELECT> ... LOOP: no LOOP keyword found"
7569                    ))
7570                })?;
7571                // Swap the LOOP token with a synthetic Semicolon so
7572                // parse_select_stmt stops there, then restore afterward.
7573                let saved_loop = self.tokens[loop_pos].clone();
7574                self.tokens[loop_pos] = Token::Semicolon;
7575                let parse_result = self.parse_select_stmt();
7576                self.tokens[loop_pos] = saved_loop;
7577                let inner = parse_result?;
7578                let Statement::Select(q) = inner else {
7579                    return Err(self.err(alloc::format!(
7580                        "expected SELECT after FOR <var> IN, got {:?}",
7581                        self.peek()
7582                    )));
7583                };
7584                q
7585            };
7586            let loop_kw = self.expect_ident_like()?;
7587            if !loop_kw.eq_ignore_ascii_case("loop") {
7588                return Err(self.err(alloc::format!(
7589                    "expected LOOP after FOR <var> IN <SELECT>, got {loop_kw:?}"
7590                )));
7591            }
7592            let body = self.parse_plpgsql_stmt_list_until_end()?;
7593            let end_kw = self.expect_ident_like()?;
7594            if !end_kw.eq_ignore_ascii_case("end") {
7595                return Err(self.err(alloc::format!(
7596                    "expected END LOOP after FOR IN SELECT body, got {end_kw:?}"
7597                )));
7598            }
7599            let loop_kw2 = self.expect_ident_like()?;
7600            if !loop_kw2.eq_ignore_ascii_case("loop") {
7601                return Err(self.err(alloc::format!(
7602                    "expected END LOOP after FOR IN SELECT body, got END {loop_kw2:?}"
7603                )));
7604            }
7605            return Ok(PlPgSqlStmt::ForQuery {
7606                var,
7607                query: Box::new(query),
7608                body,
7609            });
7610        }
7611        // v7.37.20 (20.4) — FOR <var> IN [REVERSE] <start>..<end> LOOP.
7612        // FOR is a reserved keyword token (Token::For).
7613        if matches!(self.peek(), Token::For)
7614            && matches!(
7615                self.tokens.get(self.pos + 1),
7616                Some(Token::Ident(_) | Token::QuotedIdent(_))
7617            )
7618            && matches!(self.tokens.get(self.pos + 2), Some(Token::In))
7619        {
7620            self.advance(); // FOR
7621            let var = self.expect_ident_like()?;
7622            if !matches!(self.peek(), Token::In) {
7623                return Err(self.err(alloc::format!(
7624                    "expected IN after FOR <var>, got {:?}",
7625                    self.peek()
7626                )));
7627            }
7628            self.advance();
7629            let reverse = matches!(
7630                self.peek(),
7631                Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("reverse")
7632            );
7633            if reverse {
7634                self.advance();
7635            }
7636            let start = self.parse_expr(0)?;
7637            if !matches!(self.peek(), Token::DotDot) {
7638                return Err(self.err(alloc::format!(
7639                    "expected '..' between FOR loop bounds, got {:?}",
7640                    self.peek()
7641                )));
7642            }
7643            self.advance();
7644            let end = self.parse_expr(0)?;
7645            let loop_kw = self.expect_ident_like()?;
7646            if !loop_kw.eq_ignore_ascii_case("loop") {
7647                return Err(self.err(alloc::format!(
7648                    "expected LOOP after FOR <var> IN start..end, got {loop_kw:?}"
7649                )));
7650            }
7651            let body = self.parse_plpgsql_stmt_list_until_end()?;
7652            let end_kw = self.expect_ident_like()?;
7653            if !end_kw.eq_ignore_ascii_case("end") {
7654                return Err(self.err(alloc::format!(
7655                    "expected END LOOP after FOR body, got {end_kw:?}"
7656                )));
7657            }
7658            let loop_kw2 = self.expect_ident_like()?;
7659            if !loop_kw2.eq_ignore_ascii_case("loop") {
7660                return Err(self.err(alloc::format!(
7661                    "expected END LOOP after FOR body, got END {loop_kw2:?}"
7662                )));
7663            }
7664            return Ok(PlPgSqlStmt::ForRange {
7665                var,
7666                start,
7667                end,
7668                reverse,
7669                body,
7670            });
7671        }
7672        // v7.37.20 (20.2) — bare `LOOP <body> END LOOP;`.
7673        if matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("loop"))
7674        {
7675            self.advance();
7676            let body = self.parse_plpgsql_stmt_list_until_end()?;
7677            let end_kw = self.expect_ident_like()?;
7678            if !end_kw.eq_ignore_ascii_case("end") {
7679                return Err(self.err(alloc::format!(
7680                    "expected END LOOP after LOOP body, got {end_kw:?}"
7681                )));
7682            }
7683            let loop_kw = self.expect_ident_like()?;
7684            if !loop_kw.eq_ignore_ascii_case("loop") {
7685                return Err(self.err(alloc::format!(
7686                    "expected END LOOP after LOOP body, got END {loop_kw:?}"
7687                )));
7688            }
7689            return Ok(PlPgSqlStmt::Loop { body });
7690        }
7691        // v7.37.20 (20.2) — `EXIT [WHEN <cond>]` inside a loop.
7692        if matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("exit"))
7693        {
7694            self.advance();
7695            let when = if matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("when"))
7696            {
7697                self.advance();
7698                Some(self.parse_expr(0)?)
7699            } else {
7700                None
7701            };
7702            return Ok(PlPgSqlStmt::Exit { when });
7703        }
7704        // v7.37.20 (20.13) — `EXECUTE <string_expr>`. Dispatches an
7705        // already-parsed Statement or a runtime-computed SQL string.
7706        // The disambiguator vs the extended-query-protocol `EXECUTE
7707        // <stmt_name>` (which is a top-level Statement, not a
7708        // plpgsql line) is that inside a DO block / trigger body the
7709        // EXECUTE keyword ALWAYS refers to dynamic SQL.
7710        if matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("execute"))
7711        {
7712            self.advance();
7713            let sql = self.parse_expr(0)?;
7714            return Ok(PlPgSqlStmt::ExecuteDynamic { sql });
7715        }
7716        // v7.37.20 (20.2) — `CONTINUE [WHEN <cond>]` inside a loop.
7717        if matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("continue"))
7718        {
7719            self.advance();
7720            let when = if matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("when"))
7721            {
7722                self.advance();
7723                Some(self.parse_expr(0)?)
7724            } else {
7725                None
7726            };
7727            return Ok(PlPgSqlStmt::Continue { when });
7728        }
7729        // v7.37.20 (20.3) — WHILE <cond> LOOP <body> END LOOP.
7730        if matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("while"))
7731        {
7732            self.advance();
7733            let condition = self.parse_expr(0)?;
7734            let loop_kw = self.expect_ident_like()?;
7735            if !loop_kw.eq_ignore_ascii_case("loop") {
7736                return Err(self.err(alloc::format!(
7737                    "expected LOOP after WHILE <condition>, got {loop_kw:?}"
7738                )));
7739            }
7740            let body = self.parse_plpgsql_stmt_list_until_end()?;
7741            // Expect END LOOP.
7742            let end_kw = self.expect_ident_like()?;
7743            if !end_kw.eq_ignore_ascii_case("end") {
7744                return Err(self.err(alloc::format!(
7745                    "expected END LOOP after WHILE body, got {end_kw:?}"
7746                )));
7747            }
7748            let loop_kw2 = self.expect_ident_like()?;
7749            if !loop_kw2.eq_ignore_ascii_case("loop") {
7750                return Err(self.err(alloc::format!(
7751                    "expected END LOOP after WHILE body, got END {loop_kw2:?}"
7752                )));
7753            }
7754            return Ok(PlPgSqlStmt::While { condition, body });
7755        }
7756        // v7.12.6 — RAISE.
7757        if matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("raise"))
7758        {
7759            self.advance();
7760            return self.parse_plpgsql_raise();
7761        }
7762        // v7.37.20 (20.14) — ASSERT <cond> [, <msg>].
7763        if matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("assert"))
7764        {
7765            self.advance();
7766            let condition = self.parse_expr(0)?;
7767            let message = if matches!(self.peek(), Token::Comma) {
7768                self.advance();
7769                Some(self.parse_expr(0)?)
7770            } else {
7771                None
7772            };
7773            return Ok(PlPgSqlStmt::Assert { condition, message });
7774        }
7775        // v7.37.20 (20.12) — PERFORM <select>. Per PG docs:
7776        //   "PERFORM is equivalent to SELECT but discards the
7777        //    result." Side effects (function calls, RAISE inside
7778        //    SQL functions, etc.) still execute. We desugar to
7779        //    `SELECT <body>` and wrap in EmbeddedSql so the engine's
7780        //    existing embedded-statement path handles execution +
7781        //    result-discard cleanly. The result is naturally
7782        //    discarded because EmbeddedSql doesn't propagate row
7783        //    sets back to the plpgsql interpreter.
7784        if matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("perform"))
7785        {
7786            self.advance();
7787            // Splice a synthetic Token::Select into the stream at
7788            // the current position so parse_select_stmt parses the
7789            // remainder as a normal SELECT body. Token-stream
7790            // surgery mirrors the try_parse_plpgsql_select_into
7791            // pattern used for SELECT … INTO desugaring.
7792            self.tokens.insert(self.pos, Token::Select);
7793            let select = self.parse_select_stmt()?;
7794            let Statement::Select(s) = select else {
7795                return Err(self.err(alloc::format!(
7796                    "expected SELECT body after PERFORM, got {:?}",
7797                    self.peek()
7798                )));
7799            };
7800            return Ok(PlPgSqlStmt::EmbeddedSql(Box::new(Statement::Select(s))));
7801        }
7802        // v7.16.2 — `SELECT <projection> INTO <var> [FROM …]`
7803        // plpgsql-specific shape (mailrs round-10 migrate-042).
7804        // PG's SELECT INTO at top-level SQL would CREATE a new
7805        // table; inside plpgsql it ASSIGNS the query result to
7806        // a local variable. We detect the INTO at paren-depth
7807        // 0 between SELECT and the statement boundary; if
7808        // found, split the token stream into "pre-INTO
7809        // projection" + "var" + "post-INTO FROM/WHERE…" and
7810        // rebuild as a SelectInto with a regular SELECT body
7811        // (no INTO clause).
7812        if matches!(self.peek(), Token::Select)
7813            && let Some((select_body, var_name)) = self.try_parse_plpgsql_select_into()?
7814        {
7815            return Ok(PlPgSqlStmt::SelectInto {
7816                var: var_name,
7817                body: Box::new(select_body),
7818            });
7819        }
7820        // v7.12.6 — embedded SQL statements. INSERT/UPDATE/DELETE/
7821        // SELECT can appear directly inside a trigger body; we
7822        // recurse into the regular Statement parser, which will
7823        // stop at the trailing `;` (which our caller then
7824        // consumes).
7825        // v7.16.2 — top-level DO blocks (mailrs round-10 A.2)
7826        // also embed ALTER / CREATE / DROP statements; route
7827        // those through the same parser so the DO body parses
7828        // cleanly.
7829        if matches!(self.peek(), Token::Insert)
7830            || matches!(self.peek(), Token::Select)
7831            || matches!(self.peek(), Token::Create)
7832            || matches!(self.peek(), Token::Drop)
7833            || matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s)
7834                if s.eq_ignore_ascii_case("update")
7835                    || s.eq_ignore_ascii_case("delete")
7836                    || s.eq_ignore_ascii_case("alter"))
7837        {
7838            let stmt = self.parse_one_statement()?;
7839            return Ok(PlPgSqlStmt::EmbeddedSql(Box::new(stmt)));
7840        }
7841        // Otherwise: assignment. `NEW.col` / `OLD.col` / `var`
7842        // followed by `:=` and an expression.
7843        let target = self.parse_plpgsql_assign_target()?;
7844        // PL/pgSQL assignment uses `:=`. The lexer represents
7845        // this as a colon followed by `=`; check both shapes.
7846        match self.peek() {
7847            Token::ColonEq => {
7848                self.advance();
7849            }
7850            Token::Colon => {
7851                self.advance();
7852                if !matches!(self.peek(), Token::Eq) {
7853                    return Err(self.err(alloc::format!(
7854                        "expected := after plpgsql assign target, got `:` then {:?}",
7855                        self.peek()
7856                    )));
7857                }
7858                self.advance();
7859            }
7860            other => {
7861                return Err(self.err(alloc::format!(
7862                    "expected := after plpgsql assign target, got {other:?}"
7863                )));
7864            }
7865        }
7866        let value = self.parse_expr(0)?;
7867        Ok(PlPgSqlStmt::Assign { target, value })
7868    }
7869
7870    /// v7.12.6 — `IF cond THEN body [ELSIF cond THEN body]*
7871    /// [ELSE body] END IF`. `IF` keyword already consumed.
7872    fn parse_plpgsql_if(&mut self) -> Result<PlPgSqlStmt, ParseError> {
7873        let mut branches: Vec<(Expr, Vec<PlPgSqlStmt>)> = Vec::new();
7874        let mut else_branch: Vec<PlPgSqlStmt> = Vec::new();
7875        loop {
7876            // <expr> THEN
7877            let cond = self.parse_expr(0)?;
7878            let then_kw = self.expect_ident_like()?;
7879            if !then_kw.eq_ignore_ascii_case("then") {
7880                return Err(self.err(alloc::format!(
7881                    "expected THEN after IF/ELSIF condition, got {then_kw:?}"
7882                )));
7883            }
7884            let body = self.parse_plpgsql_stmt_list_until_end()?;
7885            branches.push((cond, body));
7886            // Look at terminator: ELSIF/ELSEIF, ELSE, or END IF.
7887            match self.peek() {
7888                Token::Ident(s) | Token::QuotedIdent(s)
7889                    if s.eq_ignore_ascii_case("elsif") || s.eq_ignore_ascii_case("elseif") =>
7890                {
7891                    self.advance();
7892                    continue;
7893                }
7894                Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("else") => {
7895                    self.advance();
7896                    else_branch = self.parse_plpgsql_stmt_list_until_end()?;
7897                    break;
7898                }
7899                Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("end") => {
7900                    break;
7901                }
7902                other => {
7903                    return Err(self.err(alloc::format!(
7904                        "expected ELSIF / ELSE / END after IF branch body, got {other:?}"
7905                    )));
7906                }
7907            }
7908        }
7909        // Expect `END IF` (the END keyword is the one we're
7910        // looking at right now).
7911        let end_kw = self.expect_ident_like()?;
7912        if !end_kw.eq_ignore_ascii_case("end") {
7913            return Err(self.err(alloc::format!("expected END IF, got {end_kw:?}")));
7914        }
7915        let if_kw = self.expect_ident_like()?;
7916        if !if_kw.eq_ignore_ascii_case("if") {
7917            return Err(self.err(alloc::format!("expected END IF, got END {if_kw:?}")));
7918        }
7919        Ok(PlPgSqlStmt::If {
7920            branches,
7921            else_branch,
7922        })
7923    }
7924
7925    /// v7.12.6 — `RAISE { NOTICE | WARNING | INFO | LOG | DEBUG
7926    /// | EXCEPTION } '<message>' [, args]*`. The `RAISE` keyword
7927    /// is already consumed.
7928    fn parse_plpgsql_raise(&mut self) -> Result<PlPgSqlStmt, ParseError> {
7929        let lvl_ident = self.expect_ident_like()?;
7930        let level = match lvl_ident.to_ascii_lowercase().as_str() {
7931            "notice" => RaiseLevel::Notice,
7932            "warning" => RaiseLevel::Warning,
7933            "info" => RaiseLevel::Info,
7934            "log" => RaiseLevel::Log,
7935            "debug" => RaiseLevel::Debug,
7936            "exception" => RaiseLevel::Exception,
7937            other => {
7938                return Err(self.err(alloc::format!(
7939                    "expected RAISE level (NOTICE/WARNING/INFO/LOG/DEBUG/EXCEPTION), got {other:?}"
7940                )));
7941            }
7942        };
7943        // Message: required for v7.12.6. PG accepts a bare
7944        // RAISE-rethrow form (no message), reserved for future
7945        // RAISE-no-args support.
7946        let Token::String(msg) = self.peek() else {
7947            return Err(self.err(alloc::format!(
7948                "expected RAISE message string, got {:?}",
7949                self.peek()
7950            )));
7951        };
7952        let message = msg.clone();
7953        self.advance();
7954        // Optional comma-separated args (PG `%` format substitution).
7955        let mut args: Vec<Expr> = Vec::new();
7956        while matches!(self.peek(), Token::Comma) {
7957            self.advance();
7958            args.push(self.parse_expr(0)?);
7959        }
7960        Ok(PlPgSqlStmt::Raise {
7961            level,
7962            message,
7963            args,
7964        })
7965    }
7966
7967    /// v7.16.2 — scan ahead for a plpgsql-flavoured `SELECT
7968    /// <projection> INTO <var> [FROM …]` (mailrs round-10
7969    /// migrate-042). Returns `(rebuilt_select_without_into,
7970    /// var_name)` when the pattern matches; `None` for
7971    /// regular SELECTs (those go through the embedded-SQL
7972    /// path). Token-stream surgery so the rebuilt SELECT
7973    /// parses through the regular `parse_select_stmt`.
7974    #[allow(clippy::too_many_lines)]
7975    fn try_parse_plpgsql_select_into(
7976        &mut self,
7977    ) -> Result<Option<(SelectStatement, String)>, ParseError> {
7978        // Scan forward from `self.pos + 1` (past Token::Select)
7979        // for Token::Into at paren-depth 0, stopping at the
7980        // first `;`, `END`, `ELSE`, `ELSIF` keyword that would
7981        // end the plpgsql statement.
7982        let start = self.pos;
7983        let mut into_pos: Option<usize> = None;
7984        let mut depth: i32 = 0;
7985        let mut i = start + 1;
7986        while i < self.tokens.len() {
7987            match &self.tokens[i] {
7988                Token::LParen => depth += 1,
7989                Token::RParen => depth -= 1,
7990                Token::Semicolon if depth == 0 => break,
7991                Token::Ident(s)
7992                    if depth == 0
7993                        && (s.eq_ignore_ascii_case("end")
7994                            || s.eq_ignore_ascii_case("else")
7995                            || s.eq_ignore_ascii_case("elsif")) =>
7996                {
7997                    break;
7998                }
7999                Token::Into if depth == 0 => {
8000                    into_pos = Some(i);
8001                    break;
8002                }
8003                _ => {}
8004            }
8005            i += 1;
8006        }
8007        let Some(into_at) = into_pos else {
8008            return Ok(None);
8009        };
8010        // The token immediately after INTO must be the target
8011        // var ident; anything else (e.g. INSERT INTO table)
8012        // ruled out by the depth-0 check above. Capture it.
8013        let var = match self.tokens.get(into_at + 1) {
8014            Some(Token::Ident(s) | Token::QuotedIdent(s)) => s.clone(),
8015            other => {
8016                return Err(self.err(alloc::format!(
8017                    "expected variable name after SELECT … INTO, got {other:?}"
8018                )));
8019            }
8020        };
8021        // Find the end of the plpgsql SELECT INTO statement —
8022        // same boundary rules as the depth-0 scan above.
8023        let mut end = into_at + 2;
8024        let mut depth2: i32 = 0;
8025        while end < self.tokens.len() {
8026            match &self.tokens[end] {
8027                Token::LParen => depth2 += 1,
8028                Token::RParen => depth2 -= 1,
8029                Token::Semicolon if depth2 == 0 => break,
8030                Token::Ident(s)
8031                    if depth2 == 0
8032                        && (s.eq_ignore_ascii_case("end")
8033                            || s.eq_ignore_ascii_case("else")
8034                            || s.eq_ignore_ascii_case("elsif")) =>
8035                {
8036                    break;
8037                }
8038                _ => {}
8039            }
8040            end += 1;
8041        }
8042        // Rebuild a token stream that represents the SELECT
8043        // WITHOUT the INTO clause: [SELECT .. up-to-INTO] + [
8044        // post-var tokens up to statement end]. Run the
8045        // regular `parse_select_stmt` against it.
8046        let mut rebuilt: Vec<Token> = Vec::with_capacity(end - start);
8047        for j in start..into_at {
8048            rebuilt.push(self.tokens[j].clone());
8049        }
8050        for j in (into_at + 2)..end {
8051            rebuilt.push(self.tokens[j].clone());
8052        }
8053        rebuilt.push(Token::Eof);
8054        let saved_pos = self.pos;
8055        let saved_tokens = core::mem::replace(&mut self.tokens, rebuilt);
8056        self.pos = 0;
8057        // parse_select_stmt → parse_bare_select consumes Token::Select itself.
8058        if !matches!(self.peek(), Token::Select) {
8059            self.tokens = saved_tokens;
8060            self.pos = saved_pos;
8061            return Err(self.err("plpgsql SELECT … INTO: rebuilt stream missing SELECT".into()));
8062        }
8063        let sel = self.parse_select_stmt();
8064        self.tokens = saved_tokens;
8065        self.pos = end;
8066        let sel = sel?;
8067        let Statement::Select(body) = sel else {
8068            return Err(self.err(alloc::format!(
8069                "plpgsql SELECT … INTO: rebuilt SELECT did not produce a Select node, got {sel:?}"
8070            )));
8071        };
8072        Ok(Some((body, var)))
8073    }
8074
8075    fn parse_plpgsql_assign_target(&mut self) -> Result<AssignTarget, ParseError> {
8076        // v7.16.1 — read the head token DIRECTLY rather than
8077        // via `expect_ident_like`. The v7.14.0 schema-qualifier
8078        // strip (`public.t` → `t`) inside `expect_ident_like`
8079        // greedily consumes any `ident . ident` pair, which
8080        // silently turned every `NEW.col := …` /
8081        // `OLD.col := …` plpgsql assignment into a Local("col")
8082        // assignment — the head "new"/"old" was eaten as if it
8083        // were a schema name and the Dot was consumed too, so
8084        // this function's own `peek() == Token::Dot` check
8085        // below never fired. Every BEFORE trigger that rewrote
8086        // a NEW cell was a silent no-op for two major releases
8087        // (v7.14.0 + v7.15.0) until the e2e_trigger workspace-
8088        // gate failures were investigated as v7.16.1 backlog.
8089        let head = match self.advance() {
8090            Token::Ident(s) | Token::QuotedIdent(s) => s,
8091            other => {
8092                return Err(self.err(alloc::format!(
8093                    "expected NEW / OLD / <local_var> as plpgsql assign target, got {other:?}"
8094                )));
8095            }
8096        };
8097        if matches!(self.peek(), Token::Dot) {
8098            self.advance();
8099            let col = self.expect_ident_like()?;
8100            if head.eq_ignore_ascii_case("new") {
8101                return Ok(AssignTarget::NewColumn(col));
8102            }
8103            if head.eq_ignore_ascii_case("old") {
8104                return Ok(AssignTarget::OldColumn(col));
8105            }
8106            return Err(self.err(alloc::format!(
8107                "plpgsql assign target must be NEW.<col> / OLD.<col> / <local_var>; \
8108                 got {head:?}.<col>"
8109            )));
8110        }
8111        Ok(AssignTarget::Local(head))
8112    }
8113
8114    fn parse_plpgsql_return(&mut self) -> Result<PlPgSqlStmt, ParseError> {
8115        // RETURN NEW / OLD / NULL — bare-ident forms.
8116        match self.peek() {
8117            Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("new") => {
8118                self.advance();
8119                return Ok(PlPgSqlStmt::Return(ReturnTarget::New));
8120            }
8121            Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("old") => {
8122                self.advance();
8123                return Ok(PlPgSqlStmt::Return(ReturnTarget::Old));
8124            }
8125            Token::Null => {
8126                self.advance();
8127                return Ok(PlPgSqlStmt::Return(ReturnTarget::Null));
8128            }
8129            // Bare `RETURN;` (no value) — treated as `RETURN NULL`
8130            // per PL/pgSQL convention.
8131            Token::Semicolon => {
8132                return Ok(PlPgSqlStmt::Return(ReturnTarget::Null));
8133            }
8134            _ => {}
8135        }
8136        // v7.37.20 (20.11) — RETURN QUERY <select> / RETURN QUERY
8137        // EXECUTE <expr>. In a DO block context RETURN QUERY has no
8138        // caller-visible effect (blocks don't return sets), so we
8139        // desugar it identically to PERFORM: parse the SELECT (or
8140        // EXECUTE dynamic) as embedded SQL that runs for side
8141        // effects and discards the result. RETURN NEXT <expr>
8142        // (single-row accumulator) queues with v7.40 SETOF function
8143        // infrastructure.
8144        // v7.39 (read01 round 66) — `RETURN NEXT <expr>`: append a row to the set
8145        // and keep going.
8146        if matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("next"))
8147        {
8148            self.advance();
8149            let e = self.parse_expr(0)?;
8150            return Ok(PlPgSqlStmt::ReturnNext(e));
8151        }
8152        if matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("query"))
8153        {
8154            self.advance();
8155            // v7.39 (read01 round 68) — `RETURN QUERY EXECUTE <sql expr>`: the
8156            // rows go to the set, like the static form. It used to desugar to a
8157            // bare ExecuteDynamic, whose result was DISCARDED.
8158            if matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("execute"))
8159            {
8160                self.advance();
8161                let sql = self.parse_expr(0)?;
8162                return Ok(PlPgSqlStmt::ReturnQueryExecute { sql });
8163            }
8164            // Bare RETURN QUERY <select>. If the current token is
8165            // not already SELECT (e.g., the user wrote `RETURN QUERY
8166            // <projection> FROM ...` in a shorthand — rare but PG
8167            // accepts a bare projection here), splice one in. Same
8168            // trick as PERFORM.
8169            if !matches!(self.peek(), Token::Select) {
8170                self.tokens.insert(self.pos, Token::Select);
8171            }
8172            let select = self.parse_select_stmt()?;
8173            let Statement::Select(s) = select else {
8174                return Err(self.err(alloc::format!(
8175                    "expected SELECT body after RETURN QUERY, got {:?}",
8176                    self.peek()
8177                )));
8178            };
8179            // v7.39 (read01 round 66) — a REAL statement now. It used to desugar
8180            // to an embedded side-effect SELECT whose rows were DISCARDED, which
8181            // in a SETOF function is the entire answer thrown away.
8182            return Ok(PlPgSqlStmt::ReturnQuery(Box::new(s)));
8183        }
8184        // Fall through: parse a full expression.
8185        let e = self.parse_expr(0)?;
8186        Ok(PlPgSqlStmt::Return(ReturnTarget::Expr(e)))
8187    }
8188
8189    fn parse_trigger_event(&mut self) -> Result<TriggerEvent, ParseError> {
8190        // INSERT is a reserved Token; UPDATE / DELETE / TRUNCATE
8191        // are ident-shaped (the parser keys off case-insensitive
8192        // match — same shape used by the top-level Update / Delete
8193        // dispatchers at parse_one_statement).
8194        if matches!(self.peek(), Token::Insert) {
8195            self.advance();
8196            return Ok(TriggerEvent::Insert);
8197        }
8198        match self.peek() {
8199            Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("update") => {
8200                self.advance();
8201                Ok(TriggerEvent::Update)
8202            }
8203            Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("delete") => {
8204                self.advance();
8205                Ok(TriggerEvent::Delete)
8206            }
8207            Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("truncate") => {
8208                self.advance();
8209                Ok(TriggerEvent::Truncate)
8210            }
8211            other => Err(self.err(alloc::format!(
8212                "expected INSERT / UPDATE / DELETE / TRUNCATE in trigger event list, got {other:?}"
8213            ))),
8214        }
8215    }
8216
8217    /// v6.1.2 → v6.1.3 — `CREATE PUBLICATION <name>` body. Accepts:
8218    ///   - (no clause) → implicit `FOR ALL TABLES`
8219    ///   - `FOR ALL TABLES`
8220    ///   - `FOR ALL TABLES EXCEPT t1, t2, …` (v6.1.3)
8221    ///   - `FOR TABLE t1, t2, …` (v6.1.3) — `FOR TABLES …` also
8222    ///     accepted as an SPG lenience. PG18-measured (round 753): PG
8223    ///     REJECTS the bare plural (`invalid publication object list`,
8224    ///     TABLES only pairs with IN SCHEMA); the old note claimed an
8225    ///     unverifiable "PG 19 accepts both". Ledgered, not load-bearing.
8226    fn parse_create_publication_after_keyword(&mut self) -> Result<Statement, ParseError> {
8227        let name = self.expect_ident_or_string()?;
8228        // Bare DDL maps to FOR ALL TABLES — matches the v6.1.2
8229        // shape so existing publications keep parsing identically.
8230        let scope = if matches!(self.peek(), Token::For) {
8231            self.advance();
8232            if matches!(self.peek(), Token::All) {
8233                self.advance();
8234                if !matches!(self.peek(), Token::Tables) {
8235                    return Err(self.err(format!(
8236                        "expected TABLES after FOR ALL, got {:?}",
8237                        self.peek()
8238                    )));
8239                }
8240                self.advance();
8241                if matches!(self.peek(), Token::Except) {
8242                    self.advance();
8243                    let tables = self.parse_publication_table_list()?;
8244                    PublicationScope::AllTablesExcept(tables)
8245                } else {
8246                    PublicationScope::AllTables
8247                }
8248            } else if matches!(self.peek(), Token::Table) {
8249                self.advance();
8250                let tables = self.parse_publication_table_list()?;
8251                PublicationScope::ForTables(tables)
8252            } else if matches!(self.peek(), Token::Tables) {
8253                // v7.39 (round 754, F31-B5) — PG18-measured: the bare
8254                // plural (`FOR TABLES t`) is REJECTED (`invalid
8255                // publication object list`); TABLES only pairs with
8256                // `IN SCHEMA`. The old arm accepted it on an
8257                // unverifiable "PG 19 accepts both" claim.
8258                self.advance();
8259                if !matches!(self.peek(), Token::In) {
8260                    return Err(self.err(alloc::string::String::from(
8261                        "invalid publication object list",
8262                    )));
8263                }
8264                self.advance();
8265                if !matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("schema")) {
8266                    return Err(self.err(format!(
8267                        "expected SCHEMA after FOR TABLES IN, got {:?}",
8268                        self.peek()
8269                    )));
8270                }
8271                self.advance();
8272                let schema = self.expect_ident_or_string()?;
8273                PublicationScope::TablesInSchema(schema)
8274            } else {
8275                return Err(self.err(format!(
8276                    "expected ALL TABLES or TABLE <list> after FOR, got {:?}",
8277                    self.peek()
8278                )));
8279            }
8280        } else {
8281            PublicationScope::AllTables
8282        };
8283        Ok(Statement::CreatePublication(CreatePublicationStatement {
8284            name,
8285            scope,
8286        }))
8287    }
8288
8289    /// v6.1.3 — Comma-separated identifier list for the publication
8290    /// FOR-clause. Requires at least one entry; empty list is a
8291    /// parse error (PG behaviour). Quoted idents are accepted; the
8292    /// names round-trip through `Display` as `quote_ident(name)`.
8293    ///
8294    /// v7.37.21 (21.2 + 21.3) — accept-and-discard the per-table
8295    /// `(col_list) WHERE (predicate)` modifiers PG 15+ emits in
8296    /// pg_dump output. SPG's publication state today is per-table
8297    /// only (matching the pre-PG-15 surface); the col list + WHERE
8298    /// are parsed so dumps load through and the table name reaches
8299    /// `PublicationScope::ForTables`, but the filter is not enforced
8300    /// at publish time. Re-open when a customer dogfood gate
8301    /// requires per-row-filter or column-subset publish semantics
8302    /// (which gates on persistent slot state landing first, 21.12).
8303    fn parse_publication_table_list(&mut self) -> Result<Vec<String>, ParseError> {
8304        let first = self.parse_publication_table_entry()?;
8305        let mut out = alloc::vec![first];
8306        while matches!(self.peek(), Token::Comma) {
8307            self.advance();
8308            out.push(self.parse_publication_table_entry()?);
8309        }
8310        Ok(out)
8311    }
8312
8313    /// One table entry inside a FOR TABLE clause:
8314    ///     tab_name [ (col, col, …) ] [ WHERE (predicate) ]
8315    /// Returns just the table name; the column list + WHERE predicate
8316    /// are consumed and discarded per the parse-accept-discard
8317    /// commitment above.
8318    fn parse_publication_table_entry(&mut self) -> Result<String, ParseError> {
8319        let name = self.expect_ident_like()?;
8320        // Optional column list — `(col, col, …)`.
8321        if matches!(self.peek(), Token::LParen) {
8322            self.advance();
8323            // Empty parens are a PG error too; require ≥ 1 column.
8324            let _ = self.expect_ident_like()?;
8325            while matches!(self.peek(), Token::Comma) {
8326                self.advance();
8327                let _ = self.expect_ident_like()?;
8328            }
8329            if !matches!(self.peek(), Token::RParen) {
8330                return Err(self.err(alloc::format!(
8331                    "expected ')' to close publication column list, got {:?}",
8332                    self.peek()
8333                )));
8334            }
8335            self.advance();
8336        }
8337        // Optional row filter — `WHERE (predicate)`.
8338        if matches!(self.peek(), Token::Where) {
8339            self.advance();
8340            if !matches!(self.peek(), Token::LParen) {
8341                return Err(self.err(alloc::format!(
8342                    "expected '(' after WHERE in publication row filter, got {:?}",
8343                    self.peek()
8344                )));
8345            }
8346            self.advance();
8347            let _ = self.parse_expr(0)?;
8348            if !matches!(self.peek(), Token::RParen) {
8349                return Err(self.err(alloc::format!(
8350                    "expected ')' to close publication WHERE filter, got {:?}",
8351                    self.peek()
8352                )));
8353            }
8354            self.advance();
8355        }
8356        Ok(name)
8357    }
8358
8359    /// v6.1.4 — `CREATE SUBSCRIPTION <name>
8360    ///                 CONNECTION '<conn>'
8361    ///                 PUBLICATION <pub> [, <pub> ...]`.
8362    ///
8363    /// The clause order is fixed (CONNECTION first, then
8364    /// PUBLICATION) to match PG. No WITH-options accepted in
8365    /// v6.1.4 — `enabled` defaults to true, no other knobs ship.
8366    fn parse_create_subscription_after_keyword(&mut self) -> Result<Statement, ParseError> {
8367        let name = self.expect_ident_or_string()?;
8368        if !matches!(self.peek(), Token::Connection) {
8369            return Err(self.err(format!(
8370                "expected CONNECTION after CREATE SUBSCRIPTION <name>, got {:?}",
8371                self.peek()
8372            )));
8373        }
8374        self.advance();
8375        let conn_str = self.expect_string_literal()?;
8376        if !matches!(self.peek(), Token::Publication) {
8377            return Err(self.err(format!(
8378                "expected PUBLICATION after CONNECTION '<conn>', got {:?}",
8379                self.peek()
8380            )));
8381        }
8382        self.advance();
8383        // Reuse the publication FOR-list parser shape: at least one
8384        // identifier, comma-separated.
8385        let first = self.expect_ident_like()?;
8386        let mut publications = alloc::vec![first];
8387        while matches!(self.peek(), Token::Comma) {
8388            self.advance();
8389            publications.push(self.expect_ident_like()?);
8390        }
8391        Ok(Statement::CreateSubscription(CreateSubscriptionStatement {
8392            name,
8393            conn_str,
8394            publications,
8395        }))
8396    }
8397
8398    /// v6.1.7 — `WAIT FOR WAL POSITION <pos> [WITH TIMEOUT <ms>]`.
8399    /// All keywords after `WAIT` are bare idents in v6.1.x; no
8400    /// lexer churn. Both `<pos>` and `<ms>` are positive integers
8401    /// that fit `u64`.
8402    /// Parameter name in `SET <name>`. A GUC name may be dotted, but the
8403    /// qualifier is a *namespace* the app owns (`app.user_id`,
8404    /// `myapp.tenant` — the request-context / RLS pattern), NOT a schema
8405    /// to discard. So parse the raw segments here instead of
8406    /// `expect_ident_like`, which strips a leading `schema.` qualifier
8407    /// and would collapse `SET app.foo` to just `foo`. Standard GUCs are
8408    /// a single segment and round-trip unchanged.
8409    fn parse_set_param_name(&mut self) -> Result<String, ParseError> {
8410        let mut parts: alloc::vec::Vec<String> = alloc::vec::Vec::new();
8411        loop {
8412            let seg = match self.advance() {
8413                Token::Ident(s) | Token::QuotedIdent(s) => s,
8414                other if unreserved_keyword_text(&other).is_some() => {
8415                    unreserved_keyword_text(&other).unwrap()
8416                }
8417                other => {
8418                    return Err(ParseError {
8419                        message: format!("expected parameter name, got {other:?}"),
8420                        token_pos: self.consumed_pos(),
8421                    });
8422                }
8423            };
8424            parts.push(seg);
8425            if matches!(self.peek(), Token::Dot) {
8426                self.advance();
8427                continue;
8428            }
8429            break;
8430        }
8431        Ok(parts.join(".").to_ascii_lowercase())
8432    }
8433
8434    fn parse_set_value(&mut self) -> Result<crate::ast::SetValue, ParseError> {
8435        Self::parse_set_value_inner(self)
8436    }
8437
8438    fn parse_set_value_inner(&mut self) -> Result<crate::ast::SetValue, ParseError> {
8439        match self.advance() {
8440            Token::String(s) => Ok(crate::ast::SetValue::String(s)),
8441            Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("default") => {
8442                Ok(crate::ast::SetValue::Default)
8443            }
8444            Token::Ident(s) | Token::QuotedIdent(s) => {
8445                let mut accum = s;
8446                while matches!(self.peek(), Token::Dot) {
8447                    self.advance();
8448                    let next = self.expect_ident_like()?;
8449                    accum.push('.');
8450                    accum.push_str(&next);
8451                }
8452                Ok(crate::ast::SetValue::Ident(accum))
8453            }
8454            Token::Integer(n) => Ok(crate::ast::SetValue::Number(n.to_string())),
8455            Token::Float(f) => Ok(crate::ast::SetValue::Number(f.to_string())),
8456            // v7.22 (mailrs round-13 gap 2) — PG boolean parameter
8457            // spellings that lex as keyword tokens, not idents:
8458            // `SET standard_conforming_strings = on` is in every
8459            // pg_dump preamble (`off` already lexes as an ident).
8460            // v7.39 (round 769, F31 tranche 5 #150) — `SET x TO DEFAULT`:
8461            // DEFAULT lexes as its keyword token, so the ident arm above
8462            // never saw it and the everyday reset form was a syntax error.
8463            Token::Default => Ok(crate::ast::SetValue::Default),
8464            Token::On => Ok(crate::ast::SetValue::Ident("on".to_string())),
8465            Token::True => Ok(crate::ast::SetValue::Ident("true".to_string())),
8466            Token::False => Ok(crate::ast::SetValue::Ident("false".to_string())),
8467            // v7.14.0 — MySQL session/user variable RHS
8468            // (e.g. `SET OLD_FOREIGN_KEY_CHECKS = @@FOREIGN_KEY_CHECKS`).
8469            // Wrap as Ident so the SET handler can record it; the
8470            // engine treats `@VAR` / `@@VAR` values as opaque
8471            // strings.
8472            Token::SessionVar(s) => Ok(crate::ast::SetValue::Ident(s)),
8473            // v7.14.0 — `SET sql_mode = 'NO_AUTO_VALUE_ON_ZERO,STRICT_TRANS_TABLES'`
8474            // is the common MySQL preamble shape. Allow a `+` or
8475            // `-` prefix on negative numerics for parity with PG
8476            // (some param defaults are negative).
8477            Token::Minus => match self.advance() {
8478                Token::Integer(n) => Ok(crate::ast::SetValue::Number(alloc::format!("-{n}"))),
8479                Token::Float(f) => Ok(crate::ast::SetValue::Number(alloc::format!("-{f}"))),
8480                other => Err(self.err(format!(
8481                    "expected numeric after `-` in SET value, got {other:?}"
8482                ))),
8483            },
8484            other => Err(self.err(format!(
8485                "expected literal, identifier, or DEFAULT after `=` in SET, got {other:?}"
8486            ))),
8487        }
8488    }
8489
8490    /// v7.38 轴 4 — `[ISOLATION LEVEL …] [READ ONLY|WRITE]
8491    /// [[NOT] DEFERRABLE]` modes after `SET TRANSACTION` or
8492    /// `START TRANSACTION` / `BEGIN`. Returns the isolation level
8493    /// (default `ReadCommitted` if no `ISOLATION LEVEL` clause was
8494    /// present). Modes are comma-separated per PG; SPG also
8495    /// accepts space-separated for tolerance. READ ONLY / WRITE
8496    /// / DEFERRABLE are parsed-and-ignored (recorded for future
8497    /// surface but not behaviorally honoured today).
8498    /// Parse the trailing `[ISOLATION LEVEL …] [READ ONLY|WRITE]
8499    /// [[NOT] DEFERRABLE]` modes of BEGIN / START TRANSACTION / SET
8500    /// TRANSACTION. Returns `Some(level)` only when an explicit `ISOLATION
8501    /// LEVEL` clause was given, so a bare `BEGIN` / `BEGIN READ ONLY` keeps the
8502    /// session default rather than forcing READ COMMITTED.
8503    fn parse_isolation_level_clauses(
8504        &mut self,
8505    ) -> Result<crate::ast::TransactionModes, ParseError> {
8506        let mut level = IsolationLevel::default();
8507        let mut have_level = false;
8508        // v7.39 — READ ONLY / READ WRITE used to be consumed and dropped,
8509        // so `BEGIN READ ONLY` opened an ordinary read-write transaction.
8510        let mut read_only: Option<bool> = None;
8511        loop {
8512            // ISOLATION LEVEL …
8513            let saw_isolation =
8514                matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("isolation"));
8515            if saw_isolation {
8516                self.advance(); // ISOLATION
8517                if !matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("level")) {
8518                    return Err(self.err(alloc::format!(
8519                        "expected LEVEL after ISOLATION, got {:?}",
8520                        self.peek()
8521                    )));
8522                }
8523                self.advance(); // LEVEL
8524                // SERIALIZABLE | REPEATABLE READ | READ COMMITTED | READ UNCOMMITTED
8525                let w1 = self
8526                    .expect_ident_like()
8527                    .map_err(|e| self.err(alloc::format!("isolation level: {e:?}")))?;
8528                let lc = w1.to_ascii_lowercase();
8529                level = match lc.as_str() {
8530                    "serializable" => IsolationLevel::Serializable,
8531                    "repeatable" => {
8532                        // Expect READ
8533                        let w2 = self
8534                            .expect_ident_like()
8535                            .map_err(|e| self.err(alloc::format!("REPEATABLE …: {e:?}")))?;
8536                        if !w2.eq_ignore_ascii_case("read") {
8537                            return Err(self.err(alloc::format!(
8538                                "expected READ after REPEATABLE, got {w2:?}"
8539                            )));
8540                        }
8541                        IsolationLevel::RepeatableRead
8542                    }
8543                    "read" => {
8544                        let w2 = self
8545                            .expect_ident_like()
8546                            .map_err(|e| self.err(alloc::format!("READ …: {e:?}")))?;
8547                        match w2.to_ascii_lowercase().as_str() {
8548                            "committed" => IsolationLevel::ReadCommitted,
8549                            "uncommitted" => IsolationLevel::ReadUncommitted,
8550                            other => {
8551                                return Err(self.err(alloc::format!(
8552                                    "expected COMMITTED or UNCOMMITTED after READ, got {other:?}"
8553                                )));
8554                            }
8555                        }
8556                    }
8557                    other => {
8558                        return Err(self.err(alloc::format!("unknown isolation level {other:?}")));
8559                    }
8560                };
8561                have_level = true;
8562            } else if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("read")) {
8563                // v7.39 — READ ONLY | READ WRITE. The comment here used to
8564                // read "parsed, not behaviorally honoured", and it was
8565                // accurate: the clause was thrown away, so `BEGIN READ ONLY`
8566                // opened an ordinary read-write transaction and accepted
8567                // every write in it.
8568                self.advance();
8569                match self.peek().clone() {
8570                    Token::Ident(s) if s.eq_ignore_ascii_case("only") => {
8571                        self.advance();
8572                        read_only = Some(true);
8573                    }
8574                    Token::Ident(s) if s.eq_ignore_ascii_case("write") => {
8575                        self.advance();
8576                        read_only = Some(false);
8577                    }
8578                    other => {
8579                        return Err(self.err(alloc::format!(
8580                            "expected ONLY or WRITE after READ, got {other:?}"
8581                        )));
8582                    }
8583                }
8584            } else if matches!(self.peek(), Token::Not) {
8585                // NOT DEFERRABLE — `NOT` lexes as a reserved keyword.
8586                self.advance();
8587                if !matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("deferrable")) {
8588                    return Err(self.err(alloc::format!(
8589                        "expected DEFERRABLE after NOT, got {:?}",
8590                        self.peek()
8591                    )));
8592                }
8593                self.advance();
8594            } else if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("deferrable"))
8595            {
8596                self.advance();
8597            } else {
8598                break;
8599            }
8600            // Optional comma between modes.
8601            if matches!(self.peek(), Token::Comma) {
8602                self.advance();
8603            }
8604        }
8605        Ok(crate::ast::TransactionModes {
8606            isolation: have_level.then_some(level),
8607            read_only,
8608        })
8609    }
8610
8611    fn parse_wait_after_keyword(&mut self) -> Result<Statement, ParseError> {
8612        // FOR is a v6.1.2-reserved keyword (Token::For). The
8613        // other two are bare idents — they've never needed lexer
8614        // support and we keep it that way.
8615        if !matches!(self.peek(), Token::For) {
8616            return Err(self.err(format!("expected FOR after WAIT, got {:?}", self.peek())));
8617        }
8618        self.advance();
8619        self.expect_keyword_ident("wal")?;
8620        self.expect_keyword_ident("position")?;
8621        let pos = self.expect_u64_literal()?;
8622        let timeout_ms = if matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("with"))
8623        {
8624            self.advance();
8625            self.expect_keyword_ident("timeout")?;
8626            Some(self.expect_u64_literal()?)
8627        } else {
8628            None
8629        };
8630        Ok(Statement::WaitForWalPosition { pos, timeout_ms })
8631    }
8632
8633    /// v6.1.7 helper — consume a `Token::Integer` and check it
8634    /// fits `u64`. WAL positions and millisecond timeouts are
8635    /// non-negative.
8636    fn expect_u64_literal(&mut self) -> Result<u64, ParseError> {
8637        match self.advance() {
8638            Token::Integer(n) if n >= 0 => Ok(n as u64),
8639            Token::Integer(n) => Err(ParseError {
8640                message: format!("expected non-negative integer, got {n}"),
8641                token_pos: self.consumed_pos(),
8642            }),
8643            other => Err(ParseError {
8644                message: format!("expected integer literal, got {other:?}"),
8645                token_pos: self.consumed_pos(),
8646            }),
8647        }
8648    }
8649
8650    /// `CREATE USER` body — name + WITH PASSWORD '<pw>' + optional
8651    /// ROLE '<role>' (defaults to readonly). All string slots accept
8652    /// either a quoted ident or a quoted string literal.
8653    /// `CREATE {USER|ROLE} name [WITH] [PASSWORD 'x'] [LOGIN|NOLOGIN]
8654    /// [INHERIT|NOINHERIT] [SUPERUSER|NOSUPERUSER] [ROLE 'admin']`.
8655    ///
8656    /// `is_user` = the statement said USER, which in PG means LOGIN by default.
8657    /// The legacy SPG `ROLE 'readwrite'` clause (the coarse read/write/admin
8658    /// wire role) still parses — it is a different axis from the PG attributes.
8659    /// v7.39 (round 547) — is this ALTER ROLE / DATABASE one of the
8660    /// SET forms? Peeks past the name (and an `IN DATABASE d`) for SET
8661    /// or RESET, so the plain attribute forms keep their old path.
8662    fn peeks_db_role_setting(&self) -> bool {
8663        let mut i = self.pos + 1; // past the object's name
8664        let word = |p: usize| -> Option<String> {
8665            match self.tokens.get(p) {
8666                Some(Token::Ident(s) | Token::QuotedIdent(s)) => Some(s.to_ascii_lowercase()),
8667                Some(Token::In) => Some(String::from("in")),
8668                _ => None,
8669            }
8670        };
8671        if word(i).as_deref() == Some("in") && word(i + 1).as_deref() == Some("database") {
8672            i += 3; // IN DATABASE <name>
8673        }
8674        matches!(word(i).as_deref(), Some("set" | "reset"))
8675    }
8676
8677    fn parse_db_role_setting(&mut self, is_database: bool) -> Result<Statement, ParseError> {
8678        use crate::ast::SetDbRoleSettingStatement;
8679        // `ALTER ROLE ALL SET …` — ALL lexes as a KEYWORD, not an
8680        // identifier, so the ordinary name reader refuses it. Same trap
8681        // as TABLE / INDEX / FULL / DEFAULT before it.
8682        let name = if matches!(self.peek(), Token::All) {
8683            self.advance();
8684            String::from("all")
8685        } else {
8686            self.expect_ident_or_string()?
8687        };
8688        // `ALTER ROLE ALL SET …` is PG's every-role scope (oid 0).
8689        let all = name.eq_ignore_ascii_case("all");
8690        let (mut database, mut role) = if is_database {
8691            (Some(name), None)
8692        } else if all {
8693            (None, None)
8694        } else {
8695            (None, Some(name))
8696        };
8697        if matches!(self.peek(), Token::In) {
8698            self.advance();
8699            self.advance(); // DATABASE
8700            database = Some(self.expect_ident_or_string()?);
8701        }
8702        let resetting = matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("reset"));
8703        self.advance(); // SET | RESET
8704        if resetting && matches!(self.peek(), Token::All) {
8705            self.advance();
8706            self.consume_until_statement_boundary();
8707            return Ok(Statement::SetDbRoleSetting(Box::new(
8708                SetDbRoleSettingStatement {
8709                    database,
8710                    role,
8711                    param: None,
8712                    value: None,
8713                },
8714            )));
8715        }
8716        let param = self.expect_ident_like()?;
8717        let value = if resetting {
8718            None
8719        } else {
8720            // `SET p = v` and PG's `SET p TO v` both. TO lexes as a
8721            // KEYWORD, so the ident-only check missed it and consumed
8722            // the word itself as the value — the same trap as ALL, one
8723            // clause over.
8724            if matches!(self.peek(), Token::Eq | Token::To) || self.peek_keyword_ident("to") {
8725                self.advance();
8726            }
8727            Some(self.take_guc_value())
8728        };
8729        self.consume_until_statement_boundary();
8730        Ok(Statement::SetDbRoleSetting(Box::new(
8731            SetDbRoleSettingStatement {
8732                database,
8733                role,
8734                param: Some(param),
8735                value,
8736            },
8737        )))
8738    }
8739
8740    /// The remainder of a `SET <p> = …` clause as PG renders it back:
8741    /// a quoted literal loses its quotes, a bare word or number does not.
8742    fn take_guc_value(&mut self) -> String {
8743        match self.advance() {
8744            Token::String(s) => s,
8745            Token::Integer(n) => format!("{n}"),
8746            Token::Float(f) => format!("{f}"),
8747            Token::Ident(s) | Token::QuotedIdent(s) => s,
8748            other => format!("{other:?}"),
8749        }
8750    }
8751
8752    fn parse_create_user_after_keyword(&mut self, is_user: bool) -> Result<Statement, ParseError> {
8753        let name = self.expect_ident_or_string()?;
8754        if self.peek_keyword_ident("with") {
8755            self.advance();
8756        }
8757        let mut password = String::new();
8758        let mut role = String::new();
8759        let mut login: Option<bool> = None;
8760        let mut inherit: Option<bool> = None;
8761        let mut superuser: Option<bool> = None;
8762        // Not a `while let`: the pattern would borrow `self` across the
8763        // body, which calls `self.advance()` / `self.expect_*` (&mut).
8764        #[allow(clippy::while_let_loop)]
8765        loop {
8766            let (Token::Ident(w) | Token::QuotedIdent(w)) = self.peek() else {
8767                break;
8768            };
8769            match w.to_ascii_lowercase().as_str() {
8770                "password" => {
8771                    self.advance();
8772                    password = self.expect_string_literal()?;
8773                }
8774                // PG accepts (and pg_dump emits) ENCRYPTED PASSWORD; the value
8775                // is the same slot.
8776                "encrypted" => {
8777                    self.advance();
8778                    self.expect_keyword_ident("password")?;
8779                    password = self.expect_string_literal()?;
8780                }
8781                "login" => {
8782                    self.advance();
8783                    login = Some(true);
8784                }
8785                "nologin" => {
8786                    self.advance();
8787                    login = Some(false);
8788                }
8789                "inherit" => {
8790                    self.advance();
8791                    inherit = Some(true);
8792                }
8793                "noinherit" => {
8794                    self.advance();
8795                    inherit = Some(false);
8796                }
8797                "superuser" => {
8798                    self.advance();
8799                    superuser = Some(true);
8800                }
8801                "nosuperuser" => {
8802                    self.advance();
8803                    superuser = Some(false);
8804                }
8805                // SPG's own coarse wire role: `ROLE 'readwrite'`.
8806                "role" => {
8807                    self.advance();
8808                    role = self.expect_string_literal()?;
8809                }
8810                // Every other PG role option (CREATEDB, CONNECTION LIMIT n,
8811                // VALID UNTIL '…', CREATEROLE, REPLICATION, BYPASSRLS …) is
8812                // accepted and ignored so a pg_dump role block restores. They
8813                // gate capabilities SPG does not have.
8814                "createdb" | "nocreatedb" | "createrole" | "nocreaterole" | "replication"
8815                | "noreplication" | "bypassrls" | "nobypassrls" => {
8816                    self.advance();
8817                }
8818                "connection" => {
8819                    self.advance();
8820                    self.expect_keyword_ident("limit")?;
8821                    self.advance(); // the number
8822                }
8823                "valid" => {
8824                    self.advance();
8825                    self.expect_keyword_ident("until")?;
8826                    self.expect_string_literal()?;
8827                }
8828                _ => break,
8829            }
8830        }
8831        if role.is_empty() {
8832            role = "readonly".to_string();
8833        }
8834        Ok(Statement::CreateUser(crate::ast::CreateUserStatement {
8835            name,
8836            password,
8837            role,
8838            login,
8839            inherit,
8840            superuser,
8841            is_user,
8842        }))
8843    }
8844
8845    /// v7.39 (RLS) — parenthesised policy qualifier `( <expr> )`; caller has
8846    /// consumed the USING / WITH CHECK keyword.
8847    fn parse_paren_expr(&mut self, clause: &str) -> Result<Expr, ParseError> {
8848        if !matches!(self.peek(), Token::LParen) {
8849            return Err(self.err(alloc::format!(
8850                "expected '(' after {clause}, got {:?}",
8851                self.peek()
8852            )));
8853        }
8854        self.advance();
8855        let e = self.parse_expr(0)?;
8856        if !matches!(self.peek(), Token::RParen) {
8857            return Err(self.err(alloc::format!(
8858                "expected ')' to close {clause}, got {:?}",
8859                self.peek()
8860            )));
8861        }
8862        self.advance();
8863        Ok(e)
8864    }
8865
8866    /// v7.39 (RLS) — `TO role [, role]*`; caller has consumed `TO`.
8867    fn parse_policy_roles(&mut self) -> Result<Vec<String>, ParseError> {
8868        let mut roles = Vec::new();
8869        loop {
8870            roles.push(self.expect_ident_like()?);
8871            if matches!(self.peek(), Token::Comma) {
8872                self.advance();
8873            } else {
8874                break;
8875            }
8876        }
8877        Ok(roles)
8878    }
8879
8880    /// v7.39 (RLS) — `CREATE POLICY name ON table [AS {PERMISSIVE|RESTRICTIVE}]
8881    /// [FOR cmd] [TO roles] [USING (expr)] [WITH CHECK (expr)]`. Caller consumed
8882    /// `CREATE POLICY`.
8883    fn parse_create_policy_after_keyword(&mut self) -> Result<Statement, ParseError> {
8884        use crate::ast::PolicyCmd;
8885        let name = self.expect_ident_like()?;
8886        if !matches!(self.peek(), Token::On) {
8887            return Err(self.err(alloc::format!(
8888                "expected ON after CREATE POLICY name, got {:?}",
8889                self.peek()
8890            )));
8891        }
8892        self.advance();
8893        let table = self.expect_ident_like()?;
8894
8895        let mut permissive = true;
8896        if matches!(self.peek(), Token::As) {
8897            self.advance();
8898            let w = self.expect_ident_like()?;
8899            permissive = if w.eq_ignore_ascii_case("permissive") {
8900                true
8901            } else if w.eq_ignore_ascii_case("restrictive") {
8902                false
8903            } else {
8904                return Err(self.err(alloc::format!(
8905                    "expected PERMISSIVE or RESTRICTIVE after AS, got {w:?}"
8906                )));
8907            };
8908        }
8909
8910        let mut cmd = PolicyCmd::All;
8911        if matches!(self.peek(), Token::For) {
8912            self.advance();
8913            cmd = self.parse_policy_cmd()?;
8914        }
8915
8916        let mut roles = Vec::new();
8917        if matches!(self.peek(), Token::To) {
8918            self.advance();
8919            roles = self.parse_policy_roles()?;
8920        }
8921
8922        let mut using = None;
8923        if matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("using"))
8924        {
8925            self.advance();
8926            using = Some(self.parse_paren_expr("USING")?);
8927        }
8928
8929        let mut with_check = None;
8930        if matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("with"))
8931        {
8932            self.advance();
8933            if !matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("check"))
8934            {
8935                return Err(self.err(alloc::format!(
8936                    "expected CHECK after WITH, got {:?}",
8937                    self.peek()
8938                )));
8939            }
8940            self.advance();
8941            with_check = Some(self.parse_paren_expr("WITH CHECK")?);
8942        }
8943
8944        // Clause-per-command matrix (PG wording).
8945        match cmd {
8946            PolicyCmd::Insert => {
8947                if using.is_some() {
8948                    return Err(self.err("only WITH CHECK expression allowed for INSERT".into()));
8949                }
8950            }
8951            PolicyCmd::Select | PolicyCmd::Delete => {
8952                if with_check.is_some() {
8953                    return Err(self.err("WITH CHECK cannot be applied to SELECT or DELETE".into()));
8954                }
8955            }
8956            PolicyCmd::Update | PolicyCmd::All => {}
8957        }
8958
8959        Ok(Statement::CreatePolicy(crate::ast::CreatePolicyStatement {
8960            name,
8961            table,
8962            permissive,
8963            cmd,
8964            roles,
8965            using,
8966            with_check,
8967        }))
8968    }
8969
8970    /// v7.39 (RLS) — the command word after `FOR`.
8971    fn parse_policy_cmd(&mut self) -> Result<crate::ast::PolicyCmd, ParseError> {
8972        use crate::ast::PolicyCmd;
8973        match self.peek().clone() {
8974            Token::All => {
8975                self.advance();
8976                Ok(PolicyCmd::All)
8977            }
8978            Token::Select => {
8979                self.advance();
8980                Ok(PolicyCmd::Select)
8981            }
8982            Token::Insert => {
8983                self.advance();
8984                Ok(PolicyCmd::Insert)
8985            }
8986            Token::Ident(s) if s.eq_ignore_ascii_case("update") => {
8987                self.advance();
8988                Ok(PolicyCmd::Update)
8989            }
8990            Token::Ident(s) if s.eq_ignore_ascii_case("delete") => {
8991                self.advance();
8992                Ok(PolicyCmd::Delete)
8993            }
8994            other => Err(self.err(alloc::format!(
8995                "expected ALL/SELECT/INSERT/UPDATE/DELETE after FOR, got {other:?}"
8996            ))),
8997        }
8998    }
8999
9000    /// v7.39 (RLS) — `ALTER POLICY name ON table { RENAME TO new | [TO roles]
9001    /// [USING (expr)] [WITH CHECK (expr)] }`. Caller consumed `ALTER POLICY`.
9002    fn parse_alter_policy_after_keyword(&mut self) -> Result<Statement, ParseError> {
9003        let name = self.expect_ident_like()?;
9004        if !matches!(self.peek(), Token::On) {
9005            return Err(self.err(alloc::format!(
9006                "expected ON after ALTER POLICY name, got {:?}",
9007                self.peek()
9008            )));
9009        }
9010        self.advance();
9011        let table = self.expect_ident_like()?;
9012
9013        // RENAME TO new
9014        if matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("rename"))
9015        {
9016            self.advance();
9017            if !matches!(self.peek(), Token::To) {
9018                return Err(self.err(alloc::format!(
9019                    "expected TO after RENAME, got {:?}",
9020                    self.peek()
9021                )));
9022            }
9023            self.advance();
9024            let new = self.expect_ident_like()?;
9025            return Ok(Statement::AlterPolicy(crate::ast::AlterPolicyStatement {
9026                name,
9027                table,
9028                rename_to: Some(new),
9029                roles: None,
9030                using: None,
9031                with_check: None,
9032            }));
9033        }
9034
9035        let mut roles = None;
9036        if matches!(self.peek(), Token::To) {
9037            self.advance();
9038            roles = Some(self.parse_policy_roles()?);
9039        }
9040        let mut using = None;
9041        if matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("using"))
9042        {
9043            self.advance();
9044            using = Some(self.parse_paren_expr("USING")?);
9045        }
9046        let mut with_check = None;
9047        if matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("with"))
9048        {
9049            self.advance();
9050            if !matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("check"))
9051            {
9052                return Err(self.err(alloc::format!(
9053                    "expected CHECK after WITH, got {:?}",
9054                    self.peek()
9055                )));
9056            }
9057            self.advance();
9058            with_check = Some(self.parse_paren_expr("WITH CHECK")?);
9059        }
9060        Ok(Statement::AlterPolicy(crate::ast::AlterPolicyStatement {
9061            name,
9062            table,
9063            rename_to: None,
9064            roles,
9065            using,
9066            with_check,
9067        }))
9068    }
9069
9070    /// v7.39 (RLS) — `DROP POLICY [IF EXISTS] name ON table`. Caller consumed
9071    /// `DROP POLICY`.
9072    fn parse_drop_policy_after_keyword(&mut self) -> Result<Statement, ParseError> {
9073        let if_exists = self.consume_if_exists();
9074        let name = self.expect_ident_like()?;
9075        if !matches!(self.peek(), Token::On) {
9076            return Err(self.err(alloc::format!(
9077                "expected ON after DROP POLICY name, got {:?}",
9078                self.peek()
9079            )));
9080        }
9081        self.advance();
9082        let table = self.expect_ident_like()?;
9083        Ok(Statement::DropPolicy(crate::ast::DropPolicyStatement {
9084            name,
9085            table,
9086            if_exists,
9087        }))
9088    }
9089}
9090fn wrap_from_leaves(
9091    e: &mut Expr,
9092    names: &[String],
9093    make: &dyn Fn(Expr) -> Expr,
9094    refs: &dyn Fn(&Expr) -> bool,
9095) {
9096    if let Expr::Column(c) = e {
9097        if c.qualifier
9098            .as_deref()
9099            .is_some_and(|q| names.iter().any(|n| n.eq_ignore_ascii_case(q)))
9100        {
9101            let taken = core::mem::replace(e, Expr::Literal(Literal::Null));
9102            *e = make(taken);
9103        }
9104        return;
9105    }
9106    match e {
9107        Expr::Binary { lhs, rhs, .. } => {
9108            wrap_from_leaves(lhs, names, make, refs);
9109            wrap_from_leaves(rhs, names, make, refs);
9110        }
9111        Expr::Unary { expr, .. } | Expr::Cast { expr, .. } | Expr::IsNull { expr, .. } => {
9112            wrap_from_leaves(expr, names, make, refs)
9113        }
9114        Expr::FunctionCall { args, .. } => {
9115            for a in args.iter_mut() {
9116                wrap_from_leaves(a, names, make, refs);
9117            }
9118        }
9119        Expr::Case {
9120            operand,
9121            branches,
9122            else_branch,
9123        } => {
9124            if let Some(o) = operand.as_deref_mut() {
9125                wrap_from_leaves(o, names, make, refs);
9126            }
9127            for (w, t) in branches.iter_mut() {
9128                wrap_from_leaves(w, names, make, refs);
9129                wrap_from_leaves(t, names, make, refs);
9130            }
9131            if let Some(el) = else_branch.as_deref_mut() {
9132                wrap_from_leaves(el, names, make, refs);
9133            }
9134        }
9135        // Compound variants the walk doesn't decompose: keep the
9136        // pre-D.30 behavior — wrap the whole sub-expr if it touches
9137        // a source table, so nothing regresses.
9138        other => {
9139            if refs(other) {
9140                let taken = core::mem::replace(other, Expr::Literal(Literal::Null));
9141                *other = make(taken);
9142            }
9143        }
9144    }
9145}
9146
9147/// v7.39 (round 241) — does this expression reference any of the FROM /
9148/// USING table names (shared by the UPDATE…FROM and DELETE…USING
9149/// lowerings)?
9150fn expr_refs_tables(e: &Expr, names: &[String]) -> bool {
9151    match e {
9152        Expr::Column(c) => c
9153            .qualifier
9154            .as_deref()
9155            .is_some_and(|q| names.iter().any(|n| n.eq_ignore_ascii_case(q))),
9156        Expr::Binary { lhs, rhs, .. } => {
9157            expr_refs_tables(lhs, names) || expr_refs_tables(rhs, names)
9158        }
9159        Expr::Unary { expr, .. } | Expr::Cast { expr, .. } => expr_refs_tables(expr, names),
9160        Expr::FunctionCall { args, .. } => args.iter().any(|a| expr_refs_tables(a, names)),
9161        Expr::Case {
9162            operand,
9163            branches,
9164            else_branch,
9165        } => {
9166            operand
9167                .as_deref()
9168                .is_some_and(|o| expr_refs_tables(o, names))
9169                || branches
9170                    .iter()
9171                    .any(|(w, t)| expr_refs_tables(w, names) || expr_refs_tables(t, names))
9172                || else_branch
9173                    .as_deref()
9174                    .is_some_and(|el| expr_refs_tables(el, names))
9175        }
9176        _ => false,
9177    }
9178}
9179
9180impl Parser {
9181    /// v4.4 `UPDATE <table> SET col = expr [, col = expr]* [WHERE cond]`.
9182    /// Caller already consumed the leading `UPDATE` ident.
9183    /// v7.39 (round 420) — does a JOIN clause start here? Used to spot
9184    /// MySQL's multi-table `UPDATE a JOIN b ON …` / `UPDATE a LEFT JOIN b …`
9185    /// after the target name has been read. `JOIN` is a reserved token;
9186    /// the qualifiers are bare idents.
9187    fn peek_is_update_join_start(&self) -> bool {
9188        match self.peek() {
9189            // JOIN and its qualifiers are reserved lexer tokens (the grammar
9190            // dedicates arms to `LEFT [OUTER] JOIN` and friends).
9191            Token::Join
9192            | Token::Inner
9193            | Token::Left
9194            | Token::Right
9195            | Token::Cross
9196            | Token::Full => true,
9197            // NATURAL / STRAIGHT_JOIN arrive as bare idents.
9198            Token::Ident(s) | Token::QuotedIdent(s) => {
9199                matches!(s.to_ascii_lowercase().as_str(), "natural" | "straight_join")
9200            }
9201            _ => false,
9202        }
9203    }
9204
9205    /// v7.39 (round 430) — `SET @x = <expr> [, @y := <expr>]`, MySQL's
9206    /// USER-variable assignment. Its own per-session namespace, an arbitrary
9207    /// expression on the right, and `:=` as a second spelling of `=`.
9208    ///
9209    /// Out-of-line (`inline(never)`): the statement-parse frame it is called
9210    /// from sits on the nesting recursion chain (a CTE body, a subquery),
9211    /// and holding this loop's `Vec` + `String` locals there overflowed the
9212    /// 512 KiB guard (`e2e_in_list_depth::round25_union_cte_search_shape`).
9213    #[inline(never)]
9214    fn parse_set_user_vars(&mut self) -> Result<Statement, ParseError> {
9215        let mut assigns: Vec<(String, Expr)> = Vec::new();
9216        let mut settings: Vec<(String, Expr)> = Vec::new();
9217        loop {
9218            // v7.39 (round 554) — a plain NAME here is a session
9219            // setting, not a user variable. mysqldump writes the two in
9220            // one statement — `SET @OLD_SQL_MODE=@@SQL_MODE,
9221            // SQL_MODE='NO_AUTO_VALUE_ON_ZERO'` saves a value and
9222            // changes it — and this refused the mixture outright, so no
9223            // dump could be restored past its preamble.
9224            if let Token::Ident(name) | Token::QuotedIdent(name) = self.peek().clone() {
9225                self.advance();
9226                if !matches!(self.peek(), Token::Eq | Token::ColonEq) {
9227                    return Err(self.err(alloc::format!(
9228                        "expected `=` after {name}, got {:?}",
9229                        self.peek()
9230                    )));
9231                }
9232                self.advance();
9233                let value = self.parse_expr(0)?;
9234                settings.push((name.to_ascii_lowercase(), value));
9235                if matches!(self.peek(), Token::Comma) {
9236                    self.advance();
9237                    continue;
9238                }
9239                break;
9240            }
9241            let Token::SessionVar(raw) = self.peek().clone() else {
9242                return Err(self.err(alloc::format!(
9243                    "expected a user variable after SET, got {:?}",
9244                    self.peek()
9245                )));
9246            };
9247            if raw.starts_with("@@") {
9248                return Err(self.err(alloc::string::String::from(
9249                    "cannot mix `@@` settings with `@` user variables in one SET",
9250                )));
9251            }
9252            self.advance();
9253            if !matches!(self.peek(), Token::Eq | Token::ColonEq) {
9254                return Err(self.err(alloc::format!(
9255                    "expected `=` or `:=` after {raw}, got {:?}",
9256                    self.peek()
9257                )));
9258            }
9259            self.advance();
9260            let value = self.parse_expr(0)?;
9261            assigns.push((raw.trim_start_matches('@').to_ascii_lowercase(), value));
9262            if matches!(self.peek(), Token::Comma) {
9263                self.advance();
9264                continue;
9265            }
9266            break;
9267        }
9268        Ok(Statement::SetUserVars(assigns, settings))
9269    }
9270
9271    fn parse_update_after_keyword(&mut self) -> Result<Statement, ParseError> {
9272        // v7.39 (round 646) — `UPDATE ONLY t SET …`. Read as a table
9273        // NAMED `only` until now, which failed on `relation "only" does
9274        // not exist`. The lookahead is what keeps a table actually
9275        // called `only` working: the keyword is only a keyword when a
9276        // TABLE NAME follows it — and `SET` arrives as an identifier
9277        // here, so `UPDATE only SET a = 2` would otherwise take `SET`
9278        // for the table and die on the `=`. Measured by the pin.
9279        let only = matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s)
9280            if s.eq_ignore_ascii_case("only"))
9281            && matches!(
9282                self.tokens.get(self.pos + 1),
9283                Some(Token::Ident(n) | Token::QuotedIdent(n)) if !n.eq_ignore_ascii_case("set")
9284            );
9285        if only {
9286            self.advance();
9287        }
9288        let table = self.expect_ident_like()?;
9289        // v7.39 (round 241) — `UPDATE t [AS] alias SET …`. PG allows the
9290        // bare spelling; a bare identifier that is the SET keyword itself
9291        // is the clause, not an alias.
9292        // v7.39 (round 420) — nor is a bare join qualifier (`LEFT` / `INNER`
9293        // / …) an alias: `UPDATE a LEFT JOIN b …` starts the MySQL
9294        // multi-table form, and swallowing `LEFT` as `a`'s alias made the
9295        // following JOIN a syntax error.
9296        let starts_join = self.mysql_dialect && self.peek_is_update_join_start();
9297        let alias = if matches!(self.peek(), Token::As) {
9298            self.advance();
9299            Some(self.expect_ident_like()?)
9300        } else {
9301            match self.peek() {
9302                Token::Ident(s) | Token::QuotedIdent(s)
9303                    if !s.eq_ignore_ascii_case("set") && !starts_join =>
9304                {
9305                    let a = s.clone();
9306                    self.advance();
9307                    Some(a)
9308                }
9309                _ => None,
9310            }
9311        };
9312        // v7.39 (round 420) — MySQL's multi-table UPDATE:
9313        //     UPDATE a, b        SET a.v = b.v WHERE a.id = b.id
9314        //     UPDATE a JOIN b ON a.id = b.id      SET a.v = b.v + 1
9315        //     UPDATE a LEFT JOIN b ON a.id = b.id SET a.v = COALESCE(b.v, -1)
9316        // The FIRST table is the mutation target and the rest are sources —
9317        // exactly the shape PG spells `UPDATE a SET … FROM b WHERE …`, which
9318        // SPG already lowers onto correlated subqueries. So rewind, let
9319        // `parse_from_clause` read the whole list (it handles aliases, comma
9320        // lists, and every JOIN form), then peel the target off the front.
9321        let (mysql_from, mysql_on, mysql_outer) = if self.mysql_dialect
9322            && (matches!(self.peek(), Token::Comma) || self.peek_is_update_join_start())
9323        {
9324            // NOTE: `advance()` destroys the tokens it returns
9325            // (`mem::replace(.., Eof)`), so re-parsing by rewinding `self.pos`
9326            // is NOT possible — the tail is read forward, once, through the
9327            // same grammar `parse_from_clause` uses after its primary.
9328            let target_qual = alias.clone().unwrap_or_else(|| table.clone());
9329            let mut joins = self.parse_from_joins(&target_qual)?;
9330            if joins.is_empty() {
9331                return Err(self.err(alloc::string::String::from(
9332                    "multi-table UPDATE needs at least one source table",
9333                )));
9334            }
9335            let head = joins.remove(0);
9336            // A LEFT join keeps every target row (the unmatched ones see NULL
9337            // on the source side), so it must NOT get the EXISTS row filter
9338            // the inner / comma forms use.
9339            let outer = matches!(head.kind, crate::ast::JoinKind::Left);
9340            let src = FromClause {
9341                primary: head.table,
9342                joins,
9343            };
9344            (Some(src), head.on, outer)
9345        } else {
9346            (None, None, false)
9347        };
9348        self.expect_keyword_ident("set")?;
9349        let mut assignments = Vec::new();
9350        loop {
9351            // `SET (a, b) = (e1, e2)` / `SET (a, b) = (SELECT x, y
9352            // …)` — the parenthesized multi-assignment. Expressions
9353            // assign positionally; a subquery RHS clones per column
9354            // keeping only the Nth projection item.
9355            if matches!(self.peek(), Token::LParen) {
9356                self.advance();
9357                let mut cols = alloc::vec![self.expect_ident_like()?];
9358                while matches!(self.peek(), Token::Comma) {
9359                    self.advance();
9360                    cols.push(self.expect_ident_like()?);
9361                }
9362                if !matches!(self.peek(), Token::RParen) {
9363                    return Err(self.err(format!(
9364                        "expected ')' after SET column list, got {:?}",
9365                        self.peek()
9366                    )));
9367                }
9368                self.advance();
9369                if !matches!(self.peek(), Token::Eq) {
9370                    return Err(self.err(format!(
9371                        "expected `=` after SET column list, got {:?}",
9372                        self.peek()
9373                    )));
9374                }
9375                self.advance();
9376                if !matches!(self.peek(), Token::LParen) {
9377                    return Err(self.err(format!(
9378                        "expected '(' after SET (…) =, got {:?}",
9379                        self.peek()
9380                    )));
9381                }
9382                self.advance();
9383                if matches!(self.peek(), Token::Select) {
9384                    let inner = match self.parse_select_stmt()? {
9385                        Statement::Select(s) => s,
9386                        other => {
9387                            return Err(self.err(alloc::format!(
9388                                "expected SELECT in SET (…) = (SELECT …), got {other:?}"
9389                            )));
9390                        }
9391                    };
9392                    if !matches!(self.peek(), Token::RParen) {
9393                        return Err(self.err(format!(
9394                            "expected ')' after SET subquery, got {:?}",
9395                            self.peek()
9396                        )));
9397                    }
9398                    self.advance();
9399                    if inner.items.len() != cols.len() {
9400                        return Err(self.err(alloc::format!(
9401                            "SET (…) = (SELECT …) arity mismatch: {} columns, {} items",
9402                            cols.len(),
9403                            inner.items.len()
9404                        )));
9405                    }
9406                    for (i, col) in cols.into_iter().enumerate() {
9407                        let mut sub = inner.clone();
9408                        sub.items = alloc::vec![sub.items[i].clone()];
9409                        assignments.push((col, Expr::ScalarSubquery(Box::new(sub))));
9410                    }
9411                } else {
9412                    let mut exprs = alloc::vec![self.parse_expr(0)?];
9413                    while matches!(self.peek(), Token::Comma) {
9414                        self.advance();
9415                        exprs.push(self.parse_expr(0)?);
9416                    }
9417                    if !matches!(self.peek(), Token::RParen) {
9418                        return Err(self.err(format!(
9419                            "expected ')' after SET row values, got {:?}",
9420                            self.peek()
9421                        )));
9422                    }
9423                    self.advance();
9424                    if exprs.len() != cols.len() {
9425                        return Err(self.err(alloc::format!(
9426                            "SET (…) = (…) arity mismatch: {} columns, {} values",
9427                            cols.len(),
9428                            exprs.len()
9429                        )));
9430                    }
9431                    for (col, e) in cols.into_iter().zip(exprs) {
9432                        assignments.push((col, e));
9433                    }
9434                }
9435                if matches!(self.peek(), Token::Comma) {
9436                    self.advance();
9437                    continue;
9438                }
9439                break;
9440            }
9441            // v7.39 (round 420) — MySQL's multi-table UPDATE qualifies its
9442            // assignment targets (`SET a.v = b.v`). `expect_ident_like`
9443            // SILENTLY strips a `<qual>.` prefix (it exists for PG's
9444            // `public.` dump qualifiers), so the qualifier has to be read off
9445            // the token stream first — otherwise `SET b.v = 888` would write
9446            // to the TARGET table's `v` while naming a source table, a
9447            // silent-wrong. A qualifier naming a SOURCE table means a
9448            // multi-TARGET update — mutating two tables in one statement —
9449            // which SPG does not model, so it is refused loudly.
9450            let set_qual: Option<String> = if mysql_from.is_some()
9451                && matches!(self.tokens.get(self.pos + 1), Some(Token::Dot))
9452            {
9453                match self.peek() {
9454                    Token::Ident(s) | Token::QuotedIdent(s) => Some(s.clone()),
9455                    _ => None,
9456                }
9457            } else {
9458                None
9459            };
9460            let col = self.expect_ident_like()?;
9461            if let Some(q) = set_qual {
9462                let names_target = q.eq_ignore_ascii_case(&table)
9463                    || alias.as_deref().is_some_and(|a| a.eq_ignore_ascii_case(&q));
9464                if !names_target {
9465                    return Err(self.err(alloc::format!(
9466                        "multi-table UPDATE can only assign to its first table \
9467                         ({table}); `{q}.{col}` targets another table"
9468                    )));
9469                }
9470            }
9471            // v7.37 D.53 — array element assignment target `SET arr[i] = v`,
9472            // desugared to `arr = __array_assign(arr, i, v)` (mirrors the
9473            // `__column_default` marker lowering just below). PG assigns to the
9474            // i-th (1-based) element, NULL-padding when i exceeds the length.
9475            if matches!(self.peek(), Token::LBracket) {
9476                self.advance();
9477                let index = self.parse_expr(0)?;
9478                // v7.39 (round 257) — the SLICE target `SET arr[lo:hi] = src`
9479                // (and the open `arr[lo:]`), lowered to
9480                // `__array_assign_slice`. Only the single-subscript form
9481                // parsed before, so a slice assignment was a syntax error.
9482                let mut slice_hi: Option<Option<Expr>> = None;
9483                if matches!(self.peek(), Token::Colon) {
9484                    self.advance();
9485                    slice_hi = Some(if matches!(self.peek(), Token::RBracket) {
9486                        None
9487                    } else {
9488                        Some(self.parse_expr(0)?)
9489                    });
9490                }
9491                if !matches!(self.peek(), Token::RBracket) {
9492                    return Err(self.err(format!(
9493                        "expected `]` after array subscript in UPDATE SET, got {:?}",
9494                        self.peek()
9495                    )));
9496                }
9497                self.advance();
9498                if !matches!(self.peek(), Token::Eq) {
9499                    return Err(self.err(format!(
9500                        "expected `=` after array subscript in UPDATE SET, got {:?}",
9501                        self.peek()
9502                    )));
9503                }
9504                self.advance();
9505                let value = self.parse_expr(0)?;
9506                // PG merges several subscript writes to the same column into one
9507                // array (`SET arr[1]=x, arr[3]=y`), so chain onto any prior
9508                // assignment to `col` rather than each overwriting the original.
9509                let existing = assignments.iter().position(|(c, _)| c == &col);
9510                let base = match existing {
9511                    Some(i) => assignments[i].1.clone(),
9512                    None => Expr::Column(ColumnName {
9513                        qualifier: None,
9514                        name: col.clone(),
9515                    }),
9516                };
9517                let assigned = match slice_hi {
9518                    None => Expr::FunctionCall {
9519                        name: "__array_assign".to_string(),
9520                        args: alloc::vec![base, index, value],
9521                    },
9522                    Some(hi) => Expr::FunctionCall {
9523                        name: "__array_assign_slice".to_string(),
9524                        args: alloc::vec![
9525                            base,
9526                            index,
9527                            hi.unwrap_or(Expr::Literal(crate::ast::Literal::Null)),
9528                            value,
9529                        ],
9530                    },
9531                };
9532                match existing {
9533                    Some(i) => assignments[i].1 = assigned,
9534                    None => assignments.push((col, assigned)),
9535                }
9536                if matches!(self.peek(), Token::Comma) {
9537                    self.advance();
9538                    continue;
9539                }
9540                break;
9541            }
9542            if !matches!(self.peek(), Token::Eq) {
9543                return Err(self.err(format!(
9544                    "expected `=` after column name in UPDATE SET, got {:?}",
9545                    self.peek()
9546                )));
9547            }
9548            self.advance();
9549            // `SET col = DEFAULT` — the column's declared default;
9550            // rides out as a marker call the update executor
9551            // resolves against the schema.
9552            let value = if matches!(self.peek(), Token::Default) {
9553                self.advance();
9554                Expr::FunctionCall {
9555                    name: "__column_default".to_string(),
9556                    args: Vec::new(),
9557                }
9558            } else {
9559                self.parse_expr(0)?
9560            };
9561            assignments.push((col, value));
9562            if matches!(self.peek(), Token::Comma) {
9563                self.advance();
9564                continue;
9565            }
9566            break;
9567        }
9568        // `UPDATE t SET … FROM src [, …] WHERE cond` — PG's joined
9569        // update. Lowers onto the correlated-subquery machinery:
9570        // the WHERE becomes EXISTS(SELECT 1 FROM src WHERE cond)
9571        // and each assignment that references a FROM-list table
9572        // wraps into a correlated scalar subquery
9573        // (SELECT expr FROM src WHERE cond). Equivalent for the
9574        // unique-join shape (the overwhelmingly common one); a
9575        // multi-match, which PG resolves by arbitrary pick,
9576        // surfaces as a scalar-subquery cardinality error instead
9577        // of a silent arbitrary result.
9578        // v7.39 (round 420) — the MySQL multi-table form supplies the source
9579        // list up front (`UPDATE a, b SET …`) instead of via FROM, so it feeds
9580        // the SAME lowering below. Both spellings together is not legal in
9581        // either dialect.
9582        let from_clause = if let Some(fc) = mysql_from {
9583            if matches!(self.peek(), Token::From) {
9584                return Err(self.err(alloc::string::String::from(
9585                    "multi-table UPDATE already names its sources; drop the FROM clause",
9586                )));
9587            }
9588            Some(fc)
9589        } else if matches!(self.peek(), Token::From) {
9590            self.advance();
9591            Some(self.parse_from_clause()?)
9592        } else {
9593            None
9594        };
9595        let where_ = if matches!(self.peek(), Token::Where) {
9596            self.advance();
9597            Some(self.parse_expr(0)?)
9598        } else {
9599            None
9600        };
9601        // v7.39 (round 421, fixing round 420) — the SOURCE subquery's filter
9602        // and the TARGET-row filter are NOT the same predicate once a LEFT
9603        // join is involved:
9604        //   * inner / comma / PG's `FROM`: the ON predicate and the WHERE are
9605        //     one conjunction, and the whole thing filters target rows via
9606        //     EXISTS.
9607        //   * LEFT join: only the ON predicate belongs inside the source
9608        //     subquery. The WHERE still filters TARGET rows (with source
9609        //     columns read through the correlated subquery, which yields NULL
9610        //     for an unmatched row — exactly LEFT-join semantics).
9611        // Round 420 folded ON into WHERE unconditionally and then dropped the
9612        // outer filter for the LEFT case, so `UPDATE a LEFT JOIN b ON … SET …
9613        // WHERE a.id > 1` updated EVERY row.
9614        let sub_where = match (mysql_on.clone(), where_.clone()) {
9615            _ if mysql_outer => mysql_on.clone(),
9616            (Some(on), Some(w)) => Some(Expr::Binary {
9617                lhs: Box::new(on),
9618                op: crate::ast::BinOp::And,
9619                rhs: Box::new(w),
9620            }),
9621            (Some(on), None) => Some(on),
9622            (None, w) => w,
9623        };
9624        // v7.39 (round 413) — MySQL `UPDATE … [ORDER BY …] [LIMIT n]`. PG
9625        // has no such clause on UPDATE, so this is accepted only under the
9626        // MySQL dialect; a PG session's `UPDATE … ORDER BY …` still errors.
9627        let update_order_limit = self.parse_mysql_dml_order_limit("UPDATE")?;
9628        let mut returning = self.parse_optional_returning()?;
9629        // v7.39 (round 533) — kept for the engine, which can resolve the
9630        // UNQUALIFIED leaves this lowering has to leave alone.
9631        let from_sources = from_clause.as_ref().map(|fc| {
9632            alloc::boxed::Box::new(crate::ast::UpdateFromSources {
9633                from: fc.clone(),
9634                sub_where: sub_where.clone(),
9635            })
9636        });
9637        let (assignments, where_) = if let Some(fc) = from_clause {
9638            let names: Vec<String> = core::iter::once(&fc.primary)
9639                .chain(fc.joins.iter().map(|j| &j.table))
9640                .flat_map(|t| {
9641                    t.alias
9642                        .clone()
9643                        .into_iter()
9644                        .chain(core::iter::once(t.name.clone()))
9645                })
9646                .collect();
9647            let refs_list = |e: &Expr| -> bool {
9648                fn walk(e: &Expr, names: &[String]) -> bool {
9649                    match e {
9650                        Expr::Column(c) => c
9651                            .qualifier
9652                            .as_deref()
9653                            .is_some_and(|q| names.iter().any(|n| n.eq_ignore_ascii_case(q))),
9654                        Expr::Binary { lhs, rhs, .. } => walk(lhs, names) || walk(rhs, names),
9655                        Expr::Unary { expr, .. } | Expr::Cast { expr, .. } => walk(expr, names),
9656                        Expr::FunctionCall { args, .. } => args.iter().any(|a| walk(a, names)),
9657                        Expr::Case {
9658                            operand,
9659                            branches,
9660                            else_branch,
9661                        } => {
9662                            operand.as_deref().is_some_and(|o| walk(o, names))
9663                                || branches
9664                                    .iter()
9665                                    .any(|(w, t)| walk(w, names) || walk(t, names))
9666                                || else_branch.as_deref().is_some_and(|el| walk(el, names))
9667                        }
9668                        _ => false,
9669                    }
9670                }
9671                walk(e, &names)
9672            };
9673            let sub_select = |items: Vec<SelectItem>| SelectStatement {
9674                locking: None,
9675                ctes: Vec::new(),
9676                distinct: false,
9677                distinct_on: Vec::new(),
9678                items,
9679                from: Some(fc.clone()),
9680                where_: sub_where.clone(),
9681                group_by: None,
9682                group_by_all: false,
9683                having: None,
9684                unions: Vec::new(),
9685                order_by: Vec::new(),
9686                limit: None,
9687                offset: None,
9688                limit_with_ties: false,
9689                window_check_exprs: Vec::new(),
9690            };
9691            // v7.37 D.30 — replace each FROM-qualified column *leaf* in the
9692            // assignment RHS with a correlated scalar subquery, instead of
9693            // wrapping the whole RHS. Wrapping the whole expr moved a target-
9694            // column reference (`SET v = v + u.bonus`, where `v` is the target
9695            // table's column) inside a subquery whose FROM only has the source
9696            // table, so the unqualified `v` resolved against the source and
9697            // errored ColumnNotFound. Leaving target columns in the outer UPDATE
9698            // context — where they belong — fixes it; only the source columns
9699            // (`u.bonus`) become subqueries. A whole-expr fallback covers
9700            // compound variants the leaf-walk doesn't decompose.
9701            let make_subq = |inner: Expr| {
9702                Expr::ScalarSubquery(Box::new(sub_select(alloc::vec![SelectItem::Expr {
9703                    expr: inner,
9704                    alias: None,
9705                }])))
9706            };
9707            let assignments = assignments
9708                .into_iter()
9709                .map(|(col, mut expr)| {
9710                    wrap_from_leaves(&mut expr, &names, &make_subq, &refs_list);
9711                    (col, expr)
9712                })
9713                .collect();
9714            let exists = Expr::Exists {
9715                subquery: Box::new(sub_select(alloc::vec![SelectItem::Expr {
9716                    expr: Expr::Literal(Literal::Integer(1)),
9717                    alias: None,
9718                }])),
9719                negated: false,
9720            };
9721            // v7.39 (round 241) — RETURNING may reference the FROM-list
9722            // tables too (`RETURNING emp.id, dept.name`); the same
9723            // leaf-to-correlated-subquery lowering the assignments get.
9724            // Without it the qualifier died at eval with "unknown table
9725            // qualifier". (RETURNING was parsed before this block — the
9726            // lowering is a pure AST transformation.)
9727            if let Some(items) = returning.as_mut() {
9728                for item in items.iter_mut() {
9729                    if let SelectItem::Expr { expr, .. } = item {
9730                        wrap_from_leaves(expr, &names, &make_subq, &refs_list);
9731                    }
9732                }
9733            }
9734            // v7.39 (round 420, corrected in 421) — a MySQL LEFT JOIN keeps
9735            // EVERY matching target row: it gets no EXISTS filter, but the
9736            // caller's WHERE still applies, with source columns read through
9737            // the correlated subquery (NULL when unmatched — LEFT-join
9738            // semantics). `sub_where` above already excluded the WHERE from
9739            // the source subquery for this case.
9740            if mysql_outer {
9741                let mut outer = where_;
9742                if let Some(w) = outer.as_mut() {
9743                    wrap_from_leaves(w, &names, &make_subq, &refs_list);
9744                }
9745                (assignments, outer)
9746            } else {
9747                (assignments, Some(exists))
9748            }
9749        } else {
9750            (assignments, where_)
9751        };
9752        Ok(Statement::Update(crate::ast::UpdateStatement {
9753            ctes: Vec::new(),
9754            table,
9755            only,
9756            alias,
9757            assignments,
9758            from_sources,
9759            where_,
9760            order_limit: update_order_limit,
9761            returning,
9762        }))
9763    }
9764
9765    /// v7.39 (round 432) — MySQL's `[ORDER BY …] [LIMIT n]` tail on a DML
9766    /// statement. UPDATE grew it in round 413 and DELETE in round 432; the
9767    /// clause and its meaning are identical, so both call this rather than
9768    /// keeping two copies that could disagree on, say, whether `LIMIT 0` is
9769    /// legal. PG has no such clause on either statement, so it is read only
9770    /// under the MySQL dialect — a PG session's `DELETE … ORDER BY …` still
9771    /// errors.
9772    ///
9773    /// `#[inline(never)]`: its locals would otherwise land on the statement-
9774    /// parsing recursion frame, which is what tipped the 512 KiB nesting
9775    /// stack in round 430.
9776    #[inline(never)]
9777    fn parse_mysql_dml_order_limit(
9778        &mut self,
9779        what: &str,
9780    ) -> Result<Option<alloc::boxed::Box<crate::ast::DmlOrderLimit>>, ParseError> {
9781        if !self.mysql_dialect {
9782            return Ok(None);
9783        }
9784        let order_by = self.parse_order_by_keys()?;
9785        let limit = if matches!(self.peek(), Token::Limit) {
9786            self.advance();
9787            let tok = self.advance();
9788            let Token::Integer(n) = tok else {
9789                return Err(self.err(alloc::format!(
9790                    "expected integer after {what} LIMIT, got {tok:?}"
9791                )));
9792            };
9793            // MySQL rejects the `LIMIT offset, count` form here — only a
9794            // single row count is legal on a DML statement.
9795            if matches!(self.peek(), Token::Comma) {
9796                return Err(self.err(alloc::format!(
9797                    "{what} LIMIT takes a row count, not an offset"
9798                )));
9799            }
9800            let n = u32::try_from(n)
9801                .map_err(|_| self.err(alloc::format!("{what} LIMIT out of range: {n}")))?;
9802            Some(n)
9803        } else {
9804            None
9805        };
9806        if order_by.is_empty() && limit.is_none() {
9807            return Ok(None);
9808        }
9809        Ok(Some(alloc::boxed::Box::new(crate::ast::DmlOrderLimit {
9810            order_by,
9811            limit,
9812        })))
9813    }
9814
9815    /// v4.4 `DELETE FROM <table> [WHERE cond]`. Caller already consumed
9816    /// the leading `DELETE` ident.
9817    fn parse_delete_after_keyword(&mut self) -> Result<Statement, ParseError> {
9818        // v7.39 (round 421) — MySQL's multi-table DELETE names its target(s)
9819        // BEFORE the FROM: `DELETE a FROM a JOIN b ON …`. (`DELETE FROM a
9820        // USING a, b WHERE …` — the third MySQL spelling — needs no special
9821        // parse here; it reaches the existing USING path with the target
9822        // repeated in the list, which the source-list peel below handles.)
9823        // More than one name is a multi-TARGET delete, which SPG does not
9824        // model; it is refused rather than half-applied.
9825        let mysql_pre_target: Option<String> =
9826            if self.mysql_dialect && !matches!(self.peek(), Token::From) {
9827                let first = self.expect_ident_like()?;
9828                if matches!(self.peek(), Token::Comma) {
9829                    return Err(self.err(alloc::format!(
9830                        "multi-table DELETE can only delete from one table; \
9831                     `DELETE {first}, …` names several"
9832                    )));
9833                }
9834                Some(first)
9835            } else {
9836                None
9837            };
9838        if !matches!(self.peek(), Token::From) {
9839            return Err(self.err(format!("expected FROM after DELETE, got {:?}", self.peek())));
9840        }
9841        self.advance();
9842        // v7.39 (round 646) — `DELETE FROM ONLY t`, same shape and same
9843        // lookahead as the UPDATE spelling.
9844        let only = matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s)
9845            if s.eq_ignore_ascii_case("only"))
9846            && matches!(
9847                self.tokens.get(self.pos + 1),
9848                Some(Token::Ident(_) | Token::QuotedIdent(_))
9849            );
9850        if only {
9851            self.advance();
9852        }
9853        let table = self.expect_ident_like()?;
9854        // v7.39 (round 241) — `DELETE FROM t [AS] alias …`. The bare
9855        // spelling must not swallow the clause keywords that can follow
9856        // the target.
9857        let alias = if matches!(self.peek(), Token::As) {
9858            self.advance();
9859            Some(self.expect_ident_like()?)
9860        } else {
9861            match self.peek() {
9862                Token::Ident(s) | Token::QuotedIdent(s)
9863                    if !s.eq_ignore_ascii_case("using") && !s.eq_ignore_ascii_case("returning") =>
9864                {
9865                    let a = s.clone();
9866                    self.advance();
9867                    Some(a)
9868                }
9869                _ => None,
9870            }
9871        };
9872        // v7.39 (round 421) — MySQL's multi-table DELETE source list, read
9873        // through the SAME join grammar the FROM clause uses (see the
9874        // `advance()`-destroys-tokens note on `parse_from_joins`).
9875        let mut mysql_on: Option<Expr> = None;
9876        let mut mysql_outer = false;
9877        let mysql_using = if mysql_pre_target.is_some()
9878            && (matches!(self.peek(), Token::Comma) || self.peek_is_update_join_start())
9879        {
9880            let target_qual = alias.clone().unwrap_or_else(|| table.clone());
9881            let mut joins = self.parse_from_joins(&target_qual)?;
9882            if joins.is_empty() {
9883                return Err(self.err(alloc::string::String::from(
9884                    "multi-table DELETE needs at least one source table",
9885                )));
9886            }
9887            let head = joins.remove(0);
9888            mysql_outer = matches!(head.kind, crate::ast::JoinKind::Left);
9889            mysql_on = head.on;
9890            Some(FromClause {
9891                primary: head.table,
9892                joins,
9893            })
9894        } else {
9895            None
9896        };
9897        // The pre-FROM target must be the table the FROM names (or its
9898        // alias) — `DELETE b FROM a JOIN b …` would delete from a table that
9899        // is not the scan target.
9900        if let Some(t) = &mysql_pre_target {
9901            let names_target = t.eq_ignore_ascii_case(&table)
9902                || alias.as_deref().is_some_and(|a| a.eq_ignore_ascii_case(t));
9903            if !names_target {
9904                return Err(self.err(alloc::format!(
9905                    "DELETE target `{t}` is not the first table in the FROM clause ({table})"
9906                )));
9907            }
9908        }
9909        // `DELETE FROM t USING src [, …] WHERE cond` — PG's joined
9910        // delete. Same lowering as UPDATE … FROM: the WHERE
9911        // becomes EXISTS(SELECT 1 FROM src WHERE cond), driven per
9912        // target row by the correlated machinery.
9913        let using_clause = if let Some(fc) = mysql_using {
9914            Some(fc)
9915        } else if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("using")) {
9916            self.advance();
9917            let mut fc = self.parse_from_clause()?;
9918            // v7.39 (round 421) — MySQL's `DELETE FROM a USING a, b WHERE …`
9919            // repeats the TARGET as the first USING entry (PG's spelling
9920            // lists only the extra sources). Peel it so the source subquery
9921            // does not re-scan — and shadow — the target table.
9922            let primary_is_target =
9923                fc.primary.name.eq_ignore_ascii_case(&table) && fc.primary.alias.is_none();
9924            if self.mysql_dialect && primary_is_target && !fc.joins.is_empty() {
9925                let head = fc.joins.remove(0);
9926                mysql_outer = matches!(head.kind, crate::ast::JoinKind::Left);
9927                mysql_on = head.on;
9928                fc = FromClause {
9929                    primary: head.table,
9930                    joins: fc.joins,
9931                };
9932            }
9933            Some(fc)
9934        } else {
9935            None
9936        };
9937        let where_ = if matches!(self.peek(), Token::Where) {
9938            self.advance();
9939            Some(self.parse_expr(0)?)
9940        } else {
9941            None
9942        };
9943        // v7.39 (round 432) — MySQL's `DELETE … [ORDER BY …] [LIMIT n]`,
9944        // read before RETURNING (MariaDB's own extension trails the LIMIT).
9945        let delete_order_limit = self.parse_mysql_dml_order_limit("DELETE")?;
9946        let mut returning = self.parse_optional_returning()?;
9947        let where_ = if let Some(fc) = using_clause {
9948            // v7.39 (round 241) — same RETURNING lowering as UPDATE…FROM:
9949            // a USING-table reference in RETURNING becomes a correlated
9950            // scalar subquery over the USING list.
9951            let names: Vec<String> = core::iter::once(&fc.primary)
9952                .chain(fc.joins.iter().map(|j| &j.table))
9953                .flat_map(|t| {
9954                    t.alias
9955                        .clone()
9956                        .into_iter()
9957                        .chain(core::iter::once(t.name.clone()))
9958                })
9959                .collect();
9960            // v7.39 (round 421) — same ON / WHERE split as UPDATE: a LEFT
9961            // join filters the SOURCE subquery on the ON predicate alone and
9962            // leaves the WHERE filtering TARGET rows (so the anti-join idiom
9963            // `LEFT JOIN b ON … WHERE b.id IS NULL` deletes the unmatched
9964            // rows); every other form folds ON and WHERE into one EXISTS.
9965            let sub_where = match (mysql_on.clone(), where_.clone()) {
9966                _ if mysql_outer => mysql_on.clone(),
9967                (Some(on), Some(w)) => Some(Expr::Binary {
9968                    lhs: Box::new(on),
9969                    op: crate::ast::BinOp::And,
9970                    rhs: Box::new(w),
9971                }),
9972                (Some(on), None) => Some(on),
9973                (None, w) => w,
9974            };
9975            let exists_where = sub_where.clone();
9976            let sub_fc = fc.clone();
9977            let make_subq = move |leaf: Expr| -> Expr {
9978                Expr::ScalarSubquery(Box::new(SelectStatement {
9979                    locking: None,
9980                    ctes: Vec::new(),
9981                    distinct: false,
9982                    distinct_on: Vec::new(),
9983                    items: alloc::vec![SelectItem::Expr {
9984                        expr: leaf,
9985                        alias: None,
9986                    }],
9987                    from: Some(sub_fc.clone()),
9988                    where_: sub_where.clone(),
9989                    group_by: None,
9990                    group_by_all: false,
9991                    having: None,
9992                    unions: Vec::new(),
9993                    order_by: Vec::new(),
9994                    limit: None,
9995                    offset: None,
9996                    limit_with_ties: false,
9997                    window_check_exprs: Vec::new(),
9998                }))
9999            };
10000            let refs = |e: &Expr| expr_refs_tables(e, &names);
10001            if let Some(items) = returning.as_mut() {
10002                for item in items.iter_mut() {
10003                    if let SelectItem::Expr { expr, .. } = item {
10004                        wrap_from_leaves(expr, &names, &make_subq, &refs);
10005                    }
10006                }
10007            }
10008            // A LEFT join deletes the target rows the WHERE selects, reading
10009            // source columns through the correlated subquery (NULL when
10010            // unmatched); no EXISTS row filter.
10011            if mysql_outer {
10012                let mut outer = where_;
10013                if let Some(w) = outer.as_mut() {
10014                    wrap_from_leaves(w, &names, &make_subq, &refs);
10015                }
10016                outer
10017            } else {
10018                Some(Expr::Exists {
10019                    subquery: Box::new(SelectStatement {
10020                        locking: None,
10021                        ctes: Vec::new(),
10022                        distinct: false,
10023                        distinct_on: Vec::new(),
10024                        items: alloc::vec![SelectItem::Expr {
10025                            expr: Expr::Literal(Literal::Integer(1)),
10026                            alias: None,
10027                        }],
10028                        from: Some(fc),
10029                        where_: exists_where,
10030                        group_by: None,
10031                        group_by_all: false,
10032                        having: None,
10033                        unions: Vec::new(),
10034                        order_by: Vec::new(),
10035                        limit: None,
10036                        offset: None,
10037                        limit_with_ties: false,
10038                        window_check_exprs: Vec::new(),
10039                    }),
10040                    negated: false,
10041                })
10042            }
10043        } else {
10044            where_
10045        };
10046        Ok(Statement::Delete(crate::ast::DeleteStatement {
10047            ctes: Vec::new(),
10048            table,
10049            only,
10050            alias,
10051            where_,
10052            order_limit: delete_order_limit,
10053            returning,
10054        }))
10055    }
10056
10057    /// v7.17.0 Phase 3.P0-42 — parse `MERGE INTO <target> [alias]
10058    /// USING <source> [alias] ON <expr> WHEN [NOT] MATCHED [AND
10059    /// <expr>] THEN <action> [WHEN …]` after the leading `MERGE`
10060    /// keyword. v7.17 surface:
10061    ///   * source: table reference (subquery source is a follow-up)
10062    ///   * actions: UPDATE SET / DELETE / DO NOTHING (matched);
10063    ///     INSERT (cols) VALUES (vals) / DO NOTHING (not matched)
10064    ///   * AND-conditioned WHEN clauses; clauses tried in declaration
10065    ///     order
10066    fn parse_merge_after_keyword(&mut self) -> Result<Statement, ParseError> {
10067        // INTO
10068        let is_into_kw = matches!(self.peek(), Token::Into)
10069            || matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("into"));
10070        if !is_into_kw {
10071            return Err(self.err(format!("expected INTO after MERGE, got {:?}", self.peek())));
10072        }
10073        self.advance();
10074        let target = self.expect_ident_like()?;
10075        // Optional alias — bare ident before USING.
10076        let target_alias = match self.peek() {
10077            Token::Ident(s) | Token::QuotedIdent(s) if !s.eq_ignore_ascii_case("using") => {
10078                Some(self.expect_ident_like()?)
10079            }
10080            _ => None,
10081        };
10082        // USING
10083        let is_using_kw = matches!(
10084            self.peek(),
10085            Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("using")
10086        );
10087        if !is_using_kw {
10088            return Err(self.err(format!(
10089                "expected USING after MERGE INTO target, got {:?}",
10090                self.peek()
10091            )));
10092        }
10093        self.advance();
10094        // v7.37 D.44 — `USING (SELECT …) alias` subquery source, or `USING
10095        // <table> [alias]`. PG requires an alias after a subquery source.
10096        let (source, source_select) = if matches!(self.peek(), Token::LParen) {
10097            self.advance(); // (
10098            // v7.39 (round 768, F31-D5) — `USING (VALUES …)`: the same
10099            // constant-SELECT lowering the derived-table parser uses
10100            // (PG deletes through this form; it was a parse error).
10101            let inner = if matches!(self.peek(), Token::Values) {
10102                self.advance(); // VALUES
10103                Statement::Select(self.parse_values_rows_body()?)
10104            } else {
10105                self.parse_select_stmt()?
10106            };
10107            match self.advance() {
10108                Token::RParen => {}
10109                other => {
10110                    return Err(self.err(format!(
10111                        "expected ')' after MERGE USING subquery, got {other:?}"
10112                    )));
10113                }
10114            }
10115            let Statement::Select(sub) = inner else {
10116                return Err(self.err("MERGE USING subquery must be a SELECT".into()));
10117            };
10118            (String::new(), Some(Box::new(sub)))
10119        } else {
10120            (self.expect_ident_like()?, None)
10121        };
10122        let source_alias = match self.peek() {
10123            Token::Ident(s) | Token::QuotedIdent(s)
10124                if !s.eq_ignore_ascii_case("on") && !s.eq_ignore_ascii_case("as") =>
10125            {
10126                Some(self.expect_ident_like()?)
10127            }
10128            Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("as") => {
10129                self.advance(); // AS
10130                Some(self.expect_ident_like()?)
10131            }
10132            _ => None,
10133        };
10134        // v7.39 (round 768, F31-D5) — optional positional column-alias
10135        // list after the source alias (`s(id, v)`).
10136        let mut source_column_aliases: Vec<String> = Vec::new();
10137        if source_alias.is_some() && matches!(self.peek(), Token::LParen) {
10138            self.advance();
10139            loop {
10140                source_column_aliases.push(self.expect_ident_like()?);
10141                match self.peek() {
10142                    Token::Comma => {
10143                        self.advance();
10144                    }
10145                    Token::RParen => {
10146                        self.advance();
10147                        break;
10148                    }
10149                    other => {
10150                        return Err(self.err(format!(
10151                            "expected ',' or ')' in MERGE source column list, got {other:?}"
10152                        )));
10153                    }
10154                }
10155            }
10156        }
10157        if source_select.is_some() && source_alias.is_none() {
10158            return Err(self.err("MERGE USING (subquery) requires an alias".into()));
10159        }
10160        // ON
10161        if !matches!(self.peek(), Token::On) {
10162            return Err(self.err(format!(
10163                "expected ON after MERGE … USING source, got {:?}",
10164                self.peek()
10165            )));
10166        }
10167        self.advance();
10168        let on = self.parse_expr(0)?;
10169        // One or more WHEN clauses.
10170        let mut clauses: Vec<crate::ast::MergeWhenClause> = Vec::new();
10171        loop {
10172            let is_when_kw = matches!(
10173                self.peek(),
10174                Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("when")
10175            );
10176            if !is_when_kw {
10177                break;
10178            }
10179            self.advance(); // WHEN
10180            // [NOT] MATCHED
10181            let matched = if matches!(self.peek(), Token::Not) {
10182                self.advance();
10183                crate::ast::MergeMatched::NotMatched
10184            } else {
10185                crate::ast::MergeMatched::Matched
10186            };
10187            let is_matched_kw = matches!(
10188                self.peek(),
10189                Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("matched")
10190            );
10191            if !is_matched_kw {
10192                return Err(self.err(format!(
10193                    "expected MATCHED in WHEN clause, got {:?}",
10194                    self.peek()
10195                )));
10196            }
10197            self.advance();
10198            // v7.39 (round 146, PG17) — `NOT MATCHED [BY TARGET | BY SOURCE]`.
10199            // BY TARGET is the default (a synonym); BY SOURCE flips the clause
10200            // to fire for target rows no source row matches.
10201            let mut matched = matched;
10202            if matches!(matched, crate::ast::MergeMatched::NotMatched) && self.peek_is_by() {
10203                self.advance();
10204                match self.peek() {
10205                    Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("source") => {
10206                        self.advance();
10207                        matched = crate::ast::MergeMatched::NotMatchedBySource;
10208                    }
10209                    Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("target") => {
10210                        self.advance();
10211                    }
10212                    other => {
10213                        return Err(self.err(format!(
10214                            "expected SOURCE or TARGET after NOT MATCHED BY, got {other:?}"
10215                        )));
10216                    }
10217                }
10218            }
10219            // Optional AND <expr>
10220            let condition = if matches!(self.peek(), Token::And) {
10221                self.advance();
10222                Some(self.parse_expr(0)?)
10223            } else {
10224                None
10225            };
10226            // THEN
10227            let is_then_kw = matches!(
10228                self.peek(),
10229                Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("then")
10230            );
10231            if !is_then_kw {
10232                return Err(self.err(format!(
10233                    "expected THEN in WHEN clause, got {:?}",
10234                    self.peek()
10235                )));
10236            }
10237            self.advance();
10238            // Action: INSERT / UPDATE / DELETE / DO NOTHING
10239            let action = match self.peek().clone() {
10240                Token::Insert => {
10241                    self.advance();
10242                    // v7.39 (read01 round 123) — the `(cols)` list is OPTIONAL,
10243                    // exactly like a plain INSERT: `WHEN NOT MATCHED THEN INSERT
10244                    // VALUES (…)` omits it and fills every column in declaration
10245                    // order. PG accepts this; SPG used to require the list.
10246                    let mut columns: Vec<String> = Vec::new();
10247                    if matches!(self.peek(), Token::LParen) {
10248                        self.advance();
10249                        loop {
10250                            columns.push(self.expect_ident_like()?);
10251                            if matches!(self.peek(), Token::Comma) {
10252                                self.advance();
10253                                continue;
10254                            }
10255                            break;
10256                        }
10257                        if !matches!(self.peek(), Token::RParen) {
10258                            return Err(self.err(format!(
10259                                "expected ')' after INSERT column list, got {:?}",
10260                                self.peek()
10261                            )));
10262                        }
10263                        self.advance();
10264                    }
10265                    // VALUES (...)
10266                    if !matches!(self.peek(), Token::Values) {
10267                        return Err(self.err(format!(
10268                            "expected VALUES in MERGE INSERT, got {:?}",
10269                            self.peek()
10270                        )));
10271                    }
10272                    self.advance();
10273                    if !matches!(self.peek(), Token::LParen) {
10274                        return Err(self.err(format!(
10275                            "expected '(' after VALUES in MERGE INSERT, got {:?}",
10276                            self.peek()
10277                        )));
10278                    }
10279                    self.advance();
10280                    let mut values: Vec<crate::ast::Expr> = Vec::new();
10281                    loop {
10282                        values.push(self.parse_expr(0)?);
10283                        if matches!(self.peek(), Token::Comma) {
10284                            self.advance();
10285                            continue;
10286                        }
10287                        break;
10288                    }
10289                    if !matches!(self.peek(), Token::RParen) {
10290                        return Err(self.err(format!(
10291                            "expected ')' after MERGE INSERT values, got {:?}",
10292                            self.peek()
10293                        )));
10294                    }
10295                    self.advance();
10296                    // Empty column list = positional into every column, so the
10297                    // count is checked against the table arity at execution.
10298                    if !columns.is_empty() && columns.len() != values.len() {
10299                        return Err(self.err(format!(
10300                            "MERGE INSERT column count ({}) ≠ value count ({})",
10301                            columns.len(),
10302                            values.len()
10303                        )));
10304                    }
10305                    crate::ast::MergeAction::Insert { columns, values }
10306                }
10307                Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("update") => {
10308                    self.advance();
10309                    // SET
10310                    let is_set_kw = matches!(
10311                        self.peek(),
10312                        Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("set")
10313                    );
10314                    if !is_set_kw {
10315                        return Err(self.err(format!(
10316                            "expected SET after UPDATE in MERGE, got {:?}",
10317                            self.peek()
10318                        )));
10319                    }
10320                    self.advance();
10321                    let mut assignments: Vec<(String, crate::ast::Expr)> = Vec::new();
10322                    loop {
10323                        let col = self.expect_ident_like()?;
10324                        if !matches!(self.peek(), Token::Eq) {
10325                            return Err(self.err(format!(
10326                                "expected '=' in MERGE UPDATE assignment, got {:?}",
10327                                self.peek()
10328                            )));
10329                        }
10330                        self.advance();
10331                        let expr = self.parse_expr(0)?;
10332                        assignments.push((col, expr));
10333                        if matches!(self.peek(), Token::Comma) {
10334                            self.advance();
10335                            continue;
10336                        }
10337                        break;
10338                    }
10339                    crate::ast::MergeAction::Update { assignments }
10340                }
10341                Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("delete") => {
10342                    self.advance();
10343                    crate::ast::MergeAction::Delete
10344                }
10345                Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("do") => {
10346                    self.advance();
10347                    let is_nothing_kw = matches!(
10348                        self.peek(),
10349                        Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("nothing")
10350                    );
10351                    if !is_nothing_kw {
10352                        return Err(self.err(format!(
10353                            "expected NOTHING after DO in MERGE clause, got {:?}",
10354                            self.peek()
10355                        )));
10356                    }
10357                    self.advance();
10358                    crate::ast::MergeAction::DoNothing
10359                }
10360                other => {
10361                    return Err(self.err(format!(
10362                        "expected INSERT / UPDATE / DELETE / DO NOTHING in MERGE clause, got {other:?}"
10363                    )));
10364                }
10365            };
10366            // PG's grammar simply has no INSERT production under BY SOURCE
10367            // (a target row already exists there) — same syntax error.
10368            if matches!(matched, crate::ast::MergeMatched::NotMatchedBySource)
10369                && matches!(action, crate::ast::MergeAction::Insert { .. })
10370            {
10371                return Err(self.err(String::from("syntax error at or near \"INSERT\"")));
10372            }
10373            clauses.push(crate::ast::MergeWhenClause {
10374                matched,
10375                condition,
10376                action,
10377            });
10378        }
10379        if clauses.is_empty() {
10380            return Err(self.err(String::from("MERGE requires at least one WHEN clause")));
10381        }
10382        // v7.38 (read01 U-merge) — PG rejects a WHEN clause that follows an
10383        // unconditional (no `AND`) WHEN of the same match kind: it could
10384        // never fire. Check per match kind in clause order.
10385        let mut seen_unconditional_matched = false;
10386        let mut seen_unconditional_not_matched = false;
10387        let mut seen_unconditional_by_source = false;
10388        for c in &clauses {
10389            let seen = match c.matched {
10390                crate::ast::MergeMatched::Matched => &mut seen_unconditional_matched,
10391                crate::ast::MergeMatched::NotMatched => &mut seen_unconditional_not_matched,
10392                crate::ast::MergeMatched::NotMatchedBySource => &mut seen_unconditional_by_source,
10393            };
10394            if *seen {
10395                return Err(self.err(String::from(
10396                    "unreachable WHEN clause specified after unconditional WHEN clause",
10397                )));
10398            }
10399            if c.condition.is_none() {
10400                *seen = true;
10401            }
10402        }
10403        // v7.39 (round 130) — optional trailing `RETURNING <projection>` (PG17+).
10404        let returning = self.parse_optional_returning()?;
10405        Ok(Statement::Merge(crate::ast::MergeStatement {
10406            // Attached by `parse_with_cte_then_select` when the MERGE
10407            // heads a WITH clause (round 149).
10408            ctes: Vec::new(),
10409            target,
10410            target_alias,
10411            source,
10412            source_alias,
10413            source_select,
10414            source_column_aliases,
10415            on,
10416            clauses,
10417            returning,
10418        }))
10419    }
10420
10421    /// v7.9.4 — parse the optional trailing `RETURNING <projection>`
10422    /// clause on INSERT / UPDATE / DELETE. Same projection grammar
10423    /// as SELECT, so `RETURNING *`, `RETURNING col`,
10424    /// `RETURNING expr AS alias`, and `RETURNING a, b, c` all work.
10425    fn parse_optional_returning(
10426        &mut self,
10427    ) -> Result<Option<Vec<crate::ast::SelectItem>>, ParseError> {
10428        let is_returning_kw = matches!(
10429            self.peek(),
10430            Token::Ident(s) if s.eq_ignore_ascii_case("returning")
10431        );
10432        if !is_returning_kw {
10433            return Ok(None);
10434        }
10435        self.advance();
10436        let mut items = Vec::new();
10437        loop {
10438            items.push(self.parse_select_item()?);
10439            if matches!(self.peek(), Token::Comma) {
10440                self.advance();
10441                continue;
10442            }
10443            break;
10444        }
10445        Ok(Some(items))
10446    }
10447
10448    /// v6.0.4 — parse the tail of an ALTER statement after the
10449    /// leading `ALTER` keyword has been consumed. Only one form is
10450    /// supported in v6.0.4:
10451    ///
10452    /// ```text
10453    /// ALTER INDEX <name> REBUILD [WITH (encoding = <enc>)]
10454    /// ```
10455    fn parse_alter_after_keyword(&mut self) -> Result<Statement, ParseError> {
10456        // ALTER INDEX <name> ... | ALTER TABLE <name> SET hot_tier_bytes = <n>
10457        // v7.14.0 — `ALTER TABLE ONLY` modifier (PG partition-
10458        // exclusion) is accepted by stripping the `ONLY` keyword
10459        // before the table parse.
10460        // v7.14.0 — `ALTER SEQUENCE / ALTER VIEW / ALTER OWNER`
10461        // and the long PG-dump tail are accepted as no-ops.
10462        match self.advance() {
10463            Token::Index => {}
10464            Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("index") => {}
10465            // v6.7.2 — ALTER TABLE t SET hot_tier_bytes = X
10466            // v7.14.0 — ALTER TABLE ONLY t … strip the `ONLY`.
10467            Token::Table => {
10468                if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("only")) {
10469                    self.advance();
10470                }
10471                return self.parse_alter_table_after_keyword();
10472            }
10473            Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("policy") => {
10474                return self.parse_alter_policy_after_keyword();
10475            }
10476            Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("table") => {
10477                if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("only")) {
10478                    self.advance();
10479                }
10480                return self.parse_alter_table_after_keyword();
10481            }
10482            // v7.17.0 — ALTER SEQUENCE name <options>. Moved out
10483            // of the silent-noop tail.
10484            Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("sequence") => {
10485                return self.parse_alter_sequence_after_keyword();
10486            }
10487            // v7.37 D.55 — ALTER TYPE name ADD VALUE [IF NOT EXISTS] 'label'
10488            // [{BEFORE | AFTER} 'existing']. Real enum evolution; other ALTER
10489            // TYPE forms (RENAME / OWNER / SET SCHEMA) still no-op below.
10490            Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("type") => {
10491                // NB: the match arm consumed `TYPE` via self.advance(); the
10492                // cursor is now at the type name — do NOT advance again.
10493                let type_name = self.expect_ident_like()?;
10494                let is_add_value = matches!(self.peek(), Token::Ident(a) if a.eq_ignore_ascii_case("add"))
10495                    && matches!(self.tokens.get(self.pos + 1), Some(Token::Ident(v)) if v.eq_ignore_ascii_case("value"));
10496                if is_add_value {
10497                    self.advance(); // ADD
10498                    self.advance(); // VALUE
10499                    // `IF NOT EXISTS` — NOT lexes as the keyword `Token::Not`,
10500                    // IF/EXISTS as identifiers.
10501                    let if_not_exists = if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("if"))
10502                    {
10503                        let n1 = self.tokens.get(self.pos + 1);
10504                        let n2 = self.tokens.get(self.pos + 2);
10505                        if matches!(n1, Some(Token::Not))
10506                            && matches!(n2, Some(Token::Ident(s)) if s.eq_ignore_ascii_case("exists"))
10507                        {
10508                            self.advance();
10509                            self.advance();
10510                            self.advance();
10511                            true
10512                        } else {
10513                            false
10514                        }
10515                    } else {
10516                        false
10517                    };
10518                    let label = self.expect_string_literal()?;
10519                    let position = if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("before") || s.eq_ignore_ascii_case("after"))
10520                    {
10521                        let is_before = matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("before"));
10522                        self.advance();
10523                        let anchor = self.expect_string_literal()?;
10524                        Some((is_before, anchor))
10525                    } else {
10526                        None
10527                    };
10528                    return Ok(Statement::AlterTypeAddValue {
10529                        type_name,
10530                        label,
10531                        if_not_exists,
10532                        position,
10533                    });
10534                }
10535                // v7.39 (read01 round 49) — `RENAME VALUE 'old' TO 'new'`.
10536                // Used to fall into the no-op tail below: accepted, silently
10537                // ignored. `RENAME TO <newtype>` keeps falling through.
10538                if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("rename"))
10539                    && matches!(
10540                        self.tokens.get(self.pos + 1),
10541                        Some(Token::Ident(s)) if s.eq_ignore_ascii_case("value")
10542                    )
10543                {
10544                    self.advance(); // RENAME
10545                    self.advance(); // VALUE
10546                    let old = self.expect_string_literal()?;
10547                    if matches!(self.peek(), Token::To) {
10548                        self.advance();
10549                    } else {
10550                        self.expect_keyword_ident("to")?;
10551                    }
10552                    let new = self.expect_string_literal()?;
10553                    return Ok(Statement::AlterTypeRenameValue {
10554                        type_name,
10555                        old,
10556                        new,
10557                    });
10558                }
10559                // Other ALTER TYPE forms — the ACTION stays a no-op
10560                // (pg_dump tail), but v7.39 (round 708) the NAME is
10561                // validated: `ALTER TYPE nosuch RENAME TO x` reported
10562                // success for a type that does not exist.
10563                self.consume_until_statement_boundary();
10564                return Ok(Statement::ValidateOnly {
10565                    kind: crate::ast::ValidateOnlyKind::TypeName,
10566                    names: alloc::vec![type_name],
10567                });
10568            }
10569            // v7.14.0 — ALTER VIEW / ALTER FUNCTION /
10570            // ALTER DOMAIN / ALTER DATABASE / ALTER USER / ALTER
10571            // ROLE / ALTER SCHEMA / ALTER OWNER / ALTER DEFAULT
10572            // PRIVILEGES — accept as no-op so pg_dump's tail loads.
10573            // v7.17.0 NOTE: ALTER SEQUENCE moved out (above).
10574            // v7.39 (round 260) — ALTER DOMAIN is REAL now, so it leaves the
10575            // pg_dump no-op list below: every form used to report success
10576            // and change nothing, which is worse than refusing outright
10577            // (a migration dropping a constraint kept rejecting data).
10578            // NOTE: the enclosing `match self.advance()` already consumed
10579            // the DOMAIN keyword, so the name is next.
10580            Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("domain") => {
10581                return self.parse_alter_domain_after_keyword();
10582            }
10583            // v7.39 (round 547) — `ALTER ROLE|USER <r> [IN DATABASE <d>]
10584            // SET|RESET …` and `ALTER DATABASE <d> SET|RESET …`. These
10585            // used to fall into the pg_dump no-op tail below, so a DBA
10586            // setting a per-role default was told it worked and nothing
10587            // happened. Intercepted here, BEFORE that tail.
10588            // v7.39 (round 695) — `ALTER SYSTEM SET <name> = …` / `RESET
10589            // <name>` / `RESET ALL`. Same reason the ROLE / DATABASE
10590            // interception below exists: swallowed with the no-op tail, an
10591            // unknown parameter name was ACCEPTED where PG18 answers
10592            // `unrecognized configuration parameter`. SPG applies nothing
10593            // either way — there is no postgresql.auto.conf — but it now
10594            // says so about a name it does not know.
10595            Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("system") => {
10596                // NOTE: the scrutinee is `self.advance()`, so SYSTEM is
10597                // already consumed here. An extra advance eats the SET and
10598                // the parameter name is never seen — which is exactly the
10599                // bug a panic in this branch disproved: the branch WAS on
10600                // the path, the reading of it was wrong.
10601                let mut parameter = None;
10602                // SET <name> … | RESET <name> | RESET ALL
10603                if matches!(self.peek(), Token::Ident(k)
10604                    if k.eq_ignore_ascii_case("set") || k.eq_ignore_ascii_case("reset"))
10605                {
10606                    self.advance();
10607                    if let Token::Ident(n) | Token::QuotedIdent(n) = self.peek().clone()
10608                        && !n.eq_ignore_ascii_case("all")
10609                    {
10610                        self.advance();
10611                        // A dotted GUC (`plpgsql.check_asserts`) is two
10612                        // tokens; keep the whole name.
10613                        let mut full = n;
10614                        while matches!(self.peek(), Token::Dot) {
10615                            self.advance();
10616                            if let Token::Ident(t) | Token::QuotedIdent(t) = self.advance() {
10617                                full.push('.');
10618                                full.push_str(&t);
10619                            }
10620                        }
10621                        parameter = Some(full);
10622                    }
10623                }
10624                self.consume_until_statement_boundary();
10625                return Ok(Statement::AlterSystem { parameter });
10626            }
10627            Token::Ident(s) | Token::QuotedIdent(s)
10628                if matches!(
10629                    s.to_ascii_lowercase().as_str(),
10630                    "role" | "user" | "database"
10631                ) && self.peeks_db_role_setting() =>
10632            {
10633                let is_database = s.eq_ignore_ascii_case("database");
10634                return self.parse_db_role_setting(is_database);
10635            }
10636            // v7.39 (round 708) — `ALTER ROLE|USER <name> [WITH attrs…]`
10637            // (the non-SET forms; SET/RESET took the branch above). The
10638            // attributes still no-op — recorded, and the ignored PASSWORD
10639            // is ledgered as its own follow-up — but the ROLE is validated:
10640            // any name was accepted for a role that does not exist.
10641            Token::Ident(s) | Token::QuotedIdent(s)
10642                if s.eq_ignore_ascii_case("role") || s.eq_ignore_ascii_case("user") =>
10643            {
10644                // NB: the enclosing `match self.advance()` already consumed
10645                // ROLE/USER — the round-695 trap, hit again in this round's
10646                // first draft (the name was eaten and WITH parsed as the
10647                // role). The cursor is at the name.
10648                let name = self.expect_ident_or_string()?;
10649                // v7.39 (round 750) — scan the attribute tail for
10650                // PASSWORD. Everything else stays a recorded no-op, but
10651                // a dropped credential rotation is a SECURITY bug:
10652                // `ALTER USER x PASSWORD 'new'` answered ALTER ROLE and
10653                // changed nothing, so the old password kept working.
10654                // ENCRYPTED/UNENCRYPTED are PG-noise prefixes; `PASSWORD
10655                // NULL` clears the credential.
10656                let mut password: Option<Option<String>> = None;
10657                loop {
10658                    match self.peek() {
10659                        Token::Semicolon | Token::Eof => break,
10660                        Token::Ident(w) if w.eq_ignore_ascii_case("password") => {
10661                            self.advance();
10662                            match self.advance() {
10663                                Token::String(p) => password = Some(Some(p)),
10664                                Token::Null => password = Some(None),
10665                                Token::Ident(n) if n.eq_ignore_ascii_case("null") => {
10666                                    password = Some(None);
10667                                }
10668                                other => {
10669                                    return Err(self.err(alloc::format!(
10670                                        "expected password string or NULL after PASSWORD, got {other:?}"
10671                                    )));
10672                                }
10673                            }
10674                        }
10675                        _ => {
10676                            self.advance();
10677                        }
10678                    }
10679                }
10680                if name.eq_ignore_ascii_case("all") {
10681                    // `ALTER ROLE ALL …` names every role; nothing to check.
10682                    return Ok(Statement::Empty);
10683                }
10684                if let Some(pw) = password {
10685                    return Ok(Statement::AlterRolePassword { name, password: pw });
10686                }
10687                return Ok(Statement::ValidateOnly {
10688                    kind: crate::ast::ValidateOnlyKind::RoleName,
10689                    names: alloc::vec![name],
10690                });
10691            }
10692            // v7.39 (round 709) — ALTER COLLATION / TEXT SEARCH
10693            // CONFIGURATION / EVENT TRIGGER / LARGE OBJECT leave the no-op
10694            // list far enough to validate the NAME; the actions still no-op.
10695            // (TEXT SEARCH DICTIONARY / PARSER / TEMPLATE stay noise: SPG
10696            // models none of them and their dumps are rare.)
10697            Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("collation") => {
10698                let name = self.expect_ident_or_string()?;
10699                self.consume_until_statement_boundary();
10700                return Ok(Statement::ValidateOnly {
10701                    kind: crate::ast::ValidateOnlyKind::CollationName,
10702                    names: alloc::vec![name],
10703                });
10704            }
10705            Token::Ident(s) | Token::QuotedIdent(s)
10706                if s.eq_ignore_ascii_case("text")
10707                    && matches!(self.peek(), Token::Ident(k) if k.eq_ignore_ascii_case("search"))
10708                    && matches!(self.tokens.get(self.pos + 1), Some(Token::Ident(k)) if k.eq_ignore_ascii_case("configuration")) =>
10709            {
10710                self.advance(); // SEARCH
10711                self.advance(); // CONFIGURATION
10712                let name = self.expect_ident_like()?;
10713                self.consume_until_statement_boundary();
10714                return Ok(Statement::ValidateOnly {
10715                    kind: crate::ast::ValidateOnlyKind::TsConfigName,
10716                    names: alloc::vec![name],
10717                });
10718            }
10719            Token::Ident(s) | Token::QuotedIdent(s)
10720                if s.eq_ignore_ascii_case("event")
10721                    && matches!(self.peek(), Token::Ident(k) if k.eq_ignore_ascii_case("trigger")) =>
10722            {
10723                self.advance(); // TRIGGER
10724                let name = self.expect_ident_like()?;
10725                self.consume_until_statement_boundary();
10726                return Ok(Statement::ValidateOnly {
10727                    kind: crate::ast::ValidateOnlyKind::EventTriggerName,
10728                    names: alloc::vec![name],
10729                });
10730            }
10731            Token::Ident(s) | Token::QuotedIdent(s)
10732                if s.eq_ignore_ascii_case("large")
10733                    && matches!(self.peek(), Token::Ident(k) if k.eq_ignore_ascii_case("object")) =>
10734            {
10735                self.advance(); // OBJECT
10736                let oid = match self.advance() {
10737                    Token::Integer(n) => alloc::format!("{n}"),
10738                    other => {
10739                        return Err(
10740                            self.err(alloc::format!("expected large object oid, got {other:?}"))
10741                        );
10742                    }
10743                };
10744                self.consume_until_statement_boundary();
10745                return Ok(Statement::ValidateOnly {
10746                    kind: crate::ast::ValidateOnlyKind::LargeObjectOid,
10747                    names: alloc::vec![oid],
10748                });
10749            }
10750            // v7.39 (round 708) — `ALTER AGGREGATE name(args) …`. Same
10751            // argument-list parse as DROP AGGREGATE (round 707); the
10752            // action no-ops, the existence check is real.
10753            Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("aggregate") => {
10754                // Same round-695 trap as above: AGGREGATE is already
10755                // consumed; the cursor is at the name.
10756                let name = self.expect_ident_like()?;
10757                let mut names = alloc::vec![name];
10758                if matches!(self.peek(), Token::LParen) {
10759                    self.advance();
10760                    loop {
10761                        match self.peek().clone() {
10762                            Token::RParen => {
10763                                self.advance();
10764                                break;
10765                            }
10766                            Token::Star => {
10767                                self.advance();
10768                                names.push(String::from("*"));
10769                            }
10770                            Token::Comma => {
10771                                self.advance();
10772                            }
10773                            _ => {
10774                                let mut t = self.expect_ident_like()?;
10775                                while let Token::Ident(nx) = self.peek() {
10776                                    let nx = nx.clone();
10777                                    self.advance();
10778                                    t.push(' ');
10779                                    t.push_str(&nx);
10780                                }
10781                                names.push(t);
10782                            }
10783                        }
10784                    }
10785                }
10786                self.consume_until_statement_boundary();
10787                return Ok(Statement::ValidateOnly {
10788                    kind: crate::ast::ValidateOnlyKind::AggregateName,
10789                    names,
10790                });
10791            }
10792            Token::Ident(s) | Token::QuotedIdent(s)
10793                if matches!(
10794                    s.to_ascii_lowercase().as_str(),
10795                    "view"
10796                        | "function"
10797                        | "database"
10798                        | "schema"
10799                        | "owner"
10800                        | "default"
10801                        | "extension"
10802                        | "materialized"
10803                        | "publication"
10804                        | "subscription"
10805                        // v7.37.17 (17.6 siblings) — additional ALTER
10806                        // targets pg_dump / pg_dumpall / operator DB
10807                        // migration scripts commonly emit. SPG has
10808                        // no matching machinery for any of these; the
10809                        // parser accepts + Empty-returns so pg_dump
10810                        // tail statements don't stall.
10811                        | "tablespace"
10812                        | "language"
10813                        | "operator"
10814                        | "conversion"
10815                        | "statistics"
10816                        | "server"
10817                        | "foreign"
10818                        // `text` stays for TEXT SEARCH DICTIONARY / PARSER
10819                        // / TEMPLATE (CONFIGURATION intercepted above).
10820                        | "text"
10821                ) =>
10822            {
10823                self.consume_until_statement_boundary();
10824                return Ok(Statement::Empty);
10825            }
10826            other => {
10827                return Err(self.err(format!(
10828                    "expected INDEX / TABLE / SEQUENCE / VIEW / FUNCTION / TYPE / OWNER / etc \
10829                     after ALTER, got {other:?}"
10830                )));
10831            }
10832        }
10833        // v7.16.2 — optional `IF EXISTS` after ALTER INDEX
10834        // (mailrs migrate-042 ships these). The presence of an
10835        // IF EXISTS makes the subsequent name lookup tolerate
10836        // a missing index — engine returns CommandOk no-op.
10837        let if_exists = if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("if")) {
10838            let next = self.tokens.get(self.pos + 1);
10839            if matches!(next, Some(Token::Ident(s)) if s.eq_ignore_ascii_case("exists")) {
10840                self.advance();
10841                self.advance();
10842                true
10843            } else {
10844                false
10845            }
10846        } else {
10847            false
10848        };
10849        let name = self.expect_ident_like()?;
10850        // v7.16.2 — RENAME TO new_name shape (mailrs migrate-042).
10851        // Detect BEFORE the REBUILD path so the existing REBUILD
10852        // arm stays untouched.
10853        if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("rename")) {
10854            self.advance();
10855            if matches!(self.peek(), Token::To) {
10856                self.advance();
10857            } else {
10858                self.expect_keyword_ident("to")?;
10859            }
10860            let new = self.expect_ident_like()?;
10861            return Ok(Statement::AlterIndex(crate::ast::AlterIndexStatement {
10862                name,
10863                target: crate::ast::AlterIndexTarget::Rename { new, if_exists },
10864            }));
10865        }
10866        // v7.39 (round 710) — SET ( … ) / RESET ( … ) storage parameters.
10867        // A syntax error before; the index is validated, the params no-op.
10868        if matches!(self.peek(), Token::Ident(k) if k.eq_ignore_ascii_case("reset"))
10869            || (matches!(self.peek(), Token::Ident(k) if k.eq_ignore_ascii_case("set"))
10870                && matches!(self.tokens.get(self.pos + 1), Some(Token::LParen)))
10871        {
10872            self.consume_until_statement_boundary();
10873            return Ok(Statement::AlterIndex(crate::ast::AlterIndexStatement {
10874                name,
10875                target: crate::ast::AlterIndexTarget::StorageParams,
10876            }));
10877        }
10878        // REBUILD
10879        self.expect_keyword_ident("rebuild")?;
10880        // Optional: WITH (encoding = <enc>)
10881        let encoding = if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("with")) {
10882            self.advance();
10883            if !matches!(self.peek(), Token::LParen) {
10884                return Err(self.err(format!(
10885                    "expected '(' after WITH in ALTER INDEX REBUILD, got {:?}",
10886                    self.peek()
10887                )));
10888            }
10889            self.advance();
10890            self.expect_keyword_ident("encoding")?;
10891            if !matches!(self.peek(), Token::Eq) {
10892                return Err(self.err(format!(
10893                    "expected '=' after encoding in ALTER INDEX REBUILD, got {:?}",
10894                    self.peek()
10895                )));
10896            }
10897            self.advance();
10898            let enc_ident = match self.advance() {
10899                Token::Ident(s) | Token::QuotedIdent(s) => s,
10900                other => {
10901                    return Err(self.err(format!("expected encoding name after =, got {other:?}")));
10902                }
10903            };
10904            let enc = match enc_ident.to_ascii_lowercase().as_str() {
10905                "f32" => VecEncoding::F32,
10906                "sq8" => VecEncoding::Sq8,
10907                "half" => VecEncoding::F16,
10908                other => {
10909                    return Err(self.err(format!(
10910                        "unknown vector encoding {other:?} in ALTER INDEX REBUILD; supported: F32, SQ8, HALF"
10911                    )));
10912                }
10913            };
10914            if !matches!(self.peek(), Token::RParen) {
10915                return Err(self.err(format!(
10916                    "expected ')' after encoding value, got {:?}",
10917                    self.peek()
10918                )));
10919            }
10920            self.advance();
10921            Some(enc)
10922        } else {
10923            None
10924        };
10925        Ok(Statement::AlterIndex(crate::ast::AlterIndexStatement {
10926            name,
10927            target: crate::ast::AlterIndexTarget::Rebuild { encoding },
10928        }))
10929    }
10930
10931    /// v6.7.2 — `ALTER TABLE <name> SET hot_tier_bytes = <n>`. The
10932    /// only `SET` form currently supported; future v6.7.x can add
10933    /// more SET subjects without changing the dispatch shape.
10934    /// v7.13.2 — mailrs round-6 S1: accepts comma-separated
10935    /// subactions. Single-subaction shape stays a 1-element vec.
10936    fn parse_alter_table_after_keyword(&mut self) -> Result<Statement, ParseError> {
10937        let table_name = self.expect_ident_like()?;
10938        let mut targets: Vec<crate::ast::AlterTableTarget> = Vec::new();
10939        loop {
10940            let subaction = self.parse_alter_table_subaction()?;
10941            // ADD COLUMN with inline REFERENCES emits both an
10942            // AddColumn and an AddForeignKey subaction; the
10943            // helper returns 1 or 2 items.
10944            targets.extend(subaction);
10945            if matches!(self.peek(), Token::Comma) {
10946                self.advance();
10947                continue;
10948            }
10949            break;
10950        }
10951        Ok(Statement::AlterTable(crate::ast::AlterTableStatement {
10952            name: table_name,
10953            targets,
10954        }))
10955    }
10956
10957    /// Parse one ALTER TABLE subaction. Returns a Vec because
10958    /// inline `REFERENCES` on `ADD COLUMN` produces both an
10959    /// AddColumn and an AddForeignKey entry (mailrs round-6 S3).
10960    /// v7.39.9 — MySQL's `FIRST` / `AFTER <col>` trailer on ADD /
10961    /// MODIFY / CHANGE COLUMN. Absent is the PostgreSQL form, which
10962    /// appends.
10963    fn parse_column_position(&mut self) -> Option<crate::ast::ColumnPosition> {
10964        match self.peek() {
10965            Token::Ident(s) if s.eq_ignore_ascii_case("first") => {
10966                self.advance();
10967                Some(crate::ast::ColumnPosition::First)
10968            }
10969            Token::Ident(s) if s.eq_ignore_ascii_case("after") => {
10970                self.advance();
10971                let name = self.expect_ident_like().ok()?;
10972                Some(crate::ast::ColumnPosition::After(name))
10973            }
10974            _ => None,
10975        }
10976    }
10977
10978    fn parse_alter_table_subaction(
10979        &mut self,
10980    ) -> Result<Vec<crate::ast::AlterTableTarget>, ParseError> {
10981        match self.peek() {
10982            // v7.39.9 — MySQL's own ALTER TABLE vocabulary. Each one is
10983            // a statement a real migration emits and SPG answered 1064
10984            // for; measured against MySQL 9.7.2, one at a time, beside
10985            // the published image.
10986            Token::Ident(s)
10987                if s.eq_ignore_ascii_case("modify") || s.eq_ignore_ascii_case("change") =>
10988            {
10989                let changing = s.eq_ignore_ascii_case("change");
10990                self.advance();
10991                if matches!(self.peek(), Token::Ident(k) if k.eq_ignore_ascii_case("column")) {
10992                    self.advance();
10993                }
10994                // `parse_column_def_with_fk` reads the NAME itself, so
10995                // `MODIFY` hands it the column and `CHANGE` eats the old
10996                // name first and lets it read the new one.
10997                let old_name = if changing {
10998                    Some(self.expect_ident_like()?)
10999                } else {
11000                    None
11001                };
11002                let (definition, _fk) = self.parse_column_def_with_fk()?;
11003                let column = old_name.clone().unwrap_or_else(|| definition.name.clone());
11004                let rename_to = if changing {
11005                    Some(definition.name.clone())
11006                } else {
11007                    None
11008                };
11009                let position = self.parse_column_position();
11010                Ok(alloc::vec![crate::ast::AlterTableTarget::ModifyColumn {
11011                    column,
11012                    rename_to,
11013                    definition,
11014                    position,
11015                }])
11016            }
11017            Token::Ident(s) if s.eq_ignore_ascii_case("auto_increment") => {
11018                self.advance();
11019                if matches!(self.peek(), Token::Eq) {
11020                    self.advance();
11021                }
11022                let n = self.expect_u64_literal()?;
11023                Ok(alloc::vec![
11024                    crate::ast::AlterTableTarget::SetTableAutoIncrement(
11025                        i64::try_from(n).unwrap_or(i64::MAX)
11026                    )
11027                ])
11028            }
11029            Token::Ident(s) if s.eq_ignore_ascii_case("engine") => {
11030                self.advance();
11031                if matches!(self.peek(), Token::Eq) {
11032                    self.advance();
11033                }
11034                // v7.39.10 — as WRITTEN, the way `CREATE TABLE`'s ENGINE
11035                // clause has kept it since v7.39.3. The lexer folds a
11036                // bare identifier, and MySQL names the engine back
11037                // exactly: measured, `ALTER TABLE f1 ENGINE=NoSuchEng`
11038                // answers `Unknown storage engine 'NoSuchEng'` there and
11039                // answered `'nosucheng'` here — the one thing that
11040                // message is for is telling the operator which word in
11041                // their migration was wrong.
11042                let at = self.pos;
11043                let name = self.expect_ident_like()?;
11044                let written = self
11045                    .source_span(at, at)
11046                    .map(|raw| raw.trim().trim_matches('`').trim_matches('\''))
11047                    .filter(|raw| raw.eq_ignore_ascii_case(&name))
11048                    .map(alloc::string::String::from);
11049                Ok(alloc::vec![crate::ast::AlterTableTarget::SetEngine(
11050                    written.unwrap_or(name)
11051                )])
11052            }
11053            Token::Ident(s) if s.eq_ignore_ascii_case("convert") => {
11054                self.advance();
11055                // CONVERT TO CHARACTER SET <cs> [COLLATE <c>]
11056                if matches!(self.peek(), Token::To) {
11057                    self.advance();
11058                }
11059                let kw = self.expect_ident_like()?;
11060                if !kw.eq_ignore_ascii_case("character") {
11061                    return Err(self.err("expected CHARACTER after CONVERT TO".into()));
11062                }
11063                let set_kw = self.expect_ident_like()?;
11064                if !set_kw.eq_ignore_ascii_case("set") {
11065                    return Err(self.err("expected SET after CHARACTER".into()));
11066                }
11067                let charset = self.expect_ident_like()?;
11068                let collate =
11069                    if matches!(self.peek(), Token::Ident(k) if k.eq_ignore_ascii_case("collate")) {
11070                        self.advance();
11071                        Some(self.expect_ident_like()?)
11072                    } else {
11073                        None
11074                    };
11075                Ok(alloc::vec![
11076                    crate::ast::AlterTableTarget::ConvertToCharacterSet { charset, collate }
11077                ])
11078            }
11079            Token::Ident(s) if s.eq_ignore_ascii_case("set") => {
11080                self.advance();
11081                // v7.37.18 (18.7-18.15) — SET ( option = value, … )
11082                // storage parameters: paren-prefixed; consume.
11083                if matches!(self.peek(), Token::LParen) {
11084                    self.consume_until_statement_boundary();
11085                    return Ok(Vec::new());
11086                }
11087                let setting = self.expect_ident_like()?;
11088                if setting.eq_ignore_ascii_case("hot_tier_bytes") {
11089                    if !matches!(self.peek(), Token::Eq) {
11090                        return Err(self.err(alloc::format!(
11091                            "expected '=' after hot_tier_bytes, got {:?}",
11092                            self.peek()
11093                        )));
11094                    }
11095                    self.advance();
11096                    let n = self.expect_u64_literal()?;
11097                    return Ok(alloc::vec![crate::ast::AlterTableTarget::SetHotTierBytes(n)]);
11098                }
11099                // v7.37.18 (18.7 / 18.8 / 18.11 / 18.13 / 18.14) —
11100                // accept-and-no-op for ALTER TABLE SET <subject>
11101                // forms that pg_dump emits but SPG either treats
11102                // as N/A (single-tenant, single-owner, no shared
11103                // tablespaces) or accepts the dump-side declaration
11104                // without runtime effect:
11105                //   SET SCHEMA <name>            (18.11)
11106                //   SET TABLESPACE <name>        (18.8)
11107                //   SET LOGGED / UNLOGGED        (18.7 alt-form)
11108                //   SET WITHOUT CLUSTER          (18.13)
11109                //   SET WITHOUT OIDS             (PG legacy)
11110                //   SET (option = value, …)      (storage parameters)
11111                //   SET REPLICA IDENTITY {…}     (18.14)
11112                if setting.eq_ignore_ascii_case("schema")
11113                    || setting.eq_ignore_ascii_case("tablespace")
11114                    || setting.eq_ignore_ascii_case("logged")
11115                    || setting.eq_ignore_ascii_case("unlogged")
11116                    || setting.eq_ignore_ascii_case("without")
11117                {
11118                    self.consume_until_statement_boundary();
11119                    return Ok(Vec::new());
11120                }
11121                if setting.eq_ignore_ascii_case("replica") {
11122                    // SET REPLICA IDENTITY {DEFAULT|FULL|NOTHING|USING INDEX <name>}
11123                    self.consume_until_statement_boundary();
11124                    return Ok(Vec::new());
11125                }
11126                // SET (option=value, …) — storage parameters.
11127                if matches!(self.peek(), Token::LParen) {
11128                    self.consume_until_statement_boundary();
11129                    return Ok(Vec::new());
11130                }
11131                Err(self.err(alloc::format!(
11132                    "ALTER TABLE SET: unknown setting {setting:?}; supported: \
11133                     hot_tier_bytes / SCHEMA / TABLESPACE / LOGGED / UNLOGGED / \
11134                     WITHOUT CLUSTER / WITHOUT OIDS / REPLICA IDENTITY / (storage_params)"
11135                )))
11136            }
11137            // v7.39 (round 647) — `ALTER TABLE c INHERIT p`. Carried now,
11138            // not ignored: round 645 gave SPG the inheritance the
11139            // v7.37.18 no-op said it did not have.
11140            Token::Ident(s) if s.eq_ignore_ascii_case("inherit") => {
11141                self.advance();
11142                let parent = self.expect_ident_like()?;
11143                self.consume_until_statement_boundary();
11144                Ok(alloc::vec![crate::ast::AlterTableTarget::Inherit {
11145                    parent,
11146                    detach: false
11147                }])
11148            }
11149            // `NO INHERIT <parent>`. Guarded to NOT match `NO FORCE ROW
11150            // LEVEL SECURITY`, which has its own RLS arm below — without
11151            // the guard this swallowed NO FORCE as a no-op.
11152            Token::Ident(s)
11153                if s.eq_ignore_ascii_case("no")
11154                    && !matches!(
11155                        self.tokens.get(self.pos + 1),
11156                        Some(Token::Ident(t)) if t.eq_ignore_ascii_case("force")
11157                    ) =>
11158            {
11159                self.advance();
11160                if matches!(self.peek(), Token::Ident(k) | Token::QuotedIdent(k)
11161                    if k.eq_ignore_ascii_case("inherit"))
11162                {
11163                    self.advance();
11164                    let parent = self.expect_ident_like()?;
11165                    self.consume_until_statement_boundary();
11166                    return Ok(alloc::vec![crate::ast::AlterTableTarget::Inherit {
11167                        parent,
11168                        detach: true
11169                    }]);
11170                }
11171                self.consume_until_statement_boundary();
11172                Ok(Vec::new())
11173            }
11174            // v7.37.18 (18.10) — ALTER TABLE OWNER TO <user>. SPG is
11175            // single-owner, so there is still nothing to record.
11176            //
11177            // v7.39 (round 652) — but the name now reaches the engine,
11178            // which refuses a role that does not exist as PG does. The
11179            // no-op was swallowing the whole statement, so a dump naming
11180            // a role this server never heard of restored clean and left
11181            // the table owned by whoever ran the restore.
11182            Token::Ident(s) if s.eq_ignore_ascii_case("owner") => {
11183                self.advance();
11184                if matches!(self.peek(), Token::To) {
11185                    self.advance();
11186                }
11187                let role = self.expect_ident_like()?;
11188                Ok(alloc::vec![crate::ast::AlterTableTarget::OwnerTo {
11189                    role
11190                }])
11191            }
11192            // v7.37.18 (18.13) — ALTER TABLE CLUSTER ON <index>.
11193            // PG sets a hint; SPG doesn't have clustered storage, so the
11194            // hint itself stays a no-op.
11195            //
11196            // v7.39 (round 652) — the index name is checked now. PG
11197            // errors on one that does not exist, and swallowing that let
11198            // a typo'd CLUSTER ON pass silently.
11199            Token::Ident(s) if s.eq_ignore_ascii_case("cluster") => {
11200                self.advance();
11201                // `ON` is a reserved token, not an ident.
11202                if !matches!(self.peek(), Token::On) {
11203                    return Err(self.err(alloc::format!(
11204                        "expected ON after CLUSTER, got {:?}",
11205                        self.peek()
11206                    )));
11207                }
11208                self.advance();
11209                let index = self.expect_ident_like()?;
11210                Ok(alloc::vec![crate::ast::AlterTableTarget::ClusterOn {
11211                    index: Some(index)
11212                }])
11213            }
11214            // v7.39 (read01 round 49) — ALTER TABLE REPLICA IDENTITY
11215            // { DEFAULT | FULL | NOTHING | USING INDEX <name> }. PG records
11216            // what a logical decoder puts in the old-tuple image; SPG's
11217            // replication is SQL-text, so there is nothing to record.
11218            // Accept-and-no-op (it used to be a parse error).
11219            Token::Ident(s) if s.eq_ignore_ascii_case("replica") => {
11220                self.advance();
11221                // v7.39 (round 710) — `REPLICA IDENTITY USING INDEX <i>`
11222                // validates the index; DEFAULT / FULL / NOTHING stay no-op.
11223                if matches!(self.peek(), Token::Ident(k) if k.eq_ignore_ascii_case("identity"))
11224                    && matches!(self.tokens.get(self.pos + 1), Some(Token::Ident(k)) if k.eq_ignore_ascii_case("using"))
11225                {
11226                    self.advance(); // IDENTITY
11227                    self.advance(); // USING
11228                    if matches!(self.peek(), Token::Index)
11229                        || matches!(self.peek(), Token::Ident(k) if k.eq_ignore_ascii_case("index"))
11230                    {
11231                        self.advance();
11232                    }
11233                    let index = self.expect_ident_like()?;
11234                    self.consume_until_statement_boundary();
11235                    return Ok(alloc::vec![
11236                        crate::ast::AlterTableTarget::ReplicaIdentityUsingIndex { index }
11237                    ]);
11238                }
11239                self.consume_until_statement_boundary();
11240                Ok(Vec::new())
11241            }
11242            // v7.37.18 (18.15) — ALTER TABLE VALIDATE CONSTRAINT <name>.
11243            //
11244            // v7.39 (round 652) — it used to consume the statement and
11245            // return nothing, on the stated theory that SPG validated at
11246            // ADD CONSTRAINT time so there was never anything left to
11247            // validate. Measured against PG18, ADD CONSTRAINT did not
11248            // scan the existing rows at all — the comment described a
11249            // property SPG did not have, which is why nobody looked. Both
11250            // halves are real now: ADD scans unless told NOT VALID, and
11251            // this scans what NOT VALID skipped.
11252            Token::Ident(s) if s.eq_ignore_ascii_case("validate") => {
11253                self.advance();
11254                self.expect_keyword_ident("constraint")?;
11255                let name = self.expect_ident_like()?;
11256                Ok(alloc::vec![
11257                    crate::ast::AlterTableTarget::ValidateConstraint { name }
11258                ])
11259            }
11260            // v7.37.18 (18.18) — RESET ( option [, …] ). Inverse of
11261            // SET (option = value, …). PG uses it to clear per-table
11262            // storage params like fillfactor or autovacuum_*. SPG
11263            // engine-manages those parameters; accept-and-no-op.
11264            Token::Ident(s) if s.eq_ignore_ascii_case("reset") => {
11265                self.advance();
11266                self.consume_until_statement_boundary();
11267                Ok(Vec::new())
11268            }
11269            // v7.37.18 (18.18) — OF <type_name> / NOT OF. Composite-
11270            // type-of binding (PG 9.0+). SPG composite types
11271            // (v7.37.5 ζ-B sub-commit) follow CREATE TYPE; ALTER
11272            // TABLE OF is rare and inverse of CREATE TABLE OF.
11273            // Accept-and-no-op until a customer dump round-trips it.
11274            Token::Ident(s) if s.eq_ignore_ascii_case("of") => {
11275                self.advance();
11276                // v7.39 (round 710) — the type name is validated now.
11277                let type_name = self.expect_ident_like()?;
11278                self.consume_until_statement_boundary();
11279                Ok(alloc::vec![crate::ast::AlterTableTarget::OfType {
11280                    type_name
11281                }])
11282            }
11283            // v7.37.18 (18.18) — `NOT OF` lexes NOT as Token::Not
11284            // (reserved keyword) rather than Token::Ident("not"),
11285            // so it needs its own arm. Accept-and-no-op same as OF.
11286            Token::Not => {
11287                self.advance();
11288                self.consume_until_statement_boundary();
11289                Ok(Vec::new())
11290            }
11291            // v7.39 (RLS) — FORCE ROW LEVEL SECURITY (sets relforcerowsecurity).
11292            Token::Ident(s) if s.eq_ignore_ascii_case("force") => {
11293                self.advance();
11294                self.expect_row_level_security()?;
11295                Ok(alloc::vec![crate::ast::AlterTableTarget::SetRowSecurity {
11296                    enabled: None,
11297                    force: Some(true),
11298                }])
11299            }
11300            // v7.39 (RLS) — NO FORCE ROW LEVEL SECURITY.
11301            Token::Ident(s)
11302                if s.eq_ignore_ascii_case("no")
11303                    && matches!(
11304                        self.tokens.get(self.pos + 1),
11305                        Some(Token::Ident(t)) if t.eq_ignore_ascii_case("force")
11306                    ) =>
11307            {
11308                self.advance(); // NO
11309                self.advance(); // FORCE
11310                self.expect_row_level_security()?;
11311                Ok(alloc::vec![crate::ast::AlterTableTarget::SetRowSecurity {
11312                    enabled: None,
11313                    force: Some(false),
11314                }])
11315            }
11316            // v7.39 (RLS) — ENABLE/DISABLE ROW LEVEL SECURITY
11317            // (sets relrowsecurity). The guard requires the next token to be
11318            // `ROW` so the ENABLE/DISABLE TRIGGER arm still matches its case.
11319            Token::Ident(s)
11320                if (s.eq_ignore_ascii_case("enable") || s.eq_ignore_ascii_case("disable"))
11321                    && matches!(
11322                        self.tokens.get(self.pos + 1),
11323                        Some(Token::Ident(t)) if t.eq_ignore_ascii_case("row")
11324                    ) =>
11325            {
11326                let enabled = s.eq_ignore_ascii_case("enable");
11327                self.advance(); // ENABLE/DISABLE
11328                self.expect_row_level_security()?;
11329                Ok(alloc::vec![crate::ast::AlterTableTarget::SetRowSecurity {
11330                    enabled: Some(enabled),
11331                    force: None,
11332                }])
11333            }
11334            Token::Ident(s) if s.eq_ignore_ascii_case("add") => {
11335                self.advance();
11336                // v7.39 (round 431) — MySQL's `ALTER TABLE t ADD [UNIQUE]
11337                // {INDEX|KEY} [name] (cols)`, which every ORM migration
11338                // emits. The same grammar CREATE TABLE already accepts
11339                // inline (`KEY idx (a)`, prefix lengths and all), so it goes
11340                // through the SAME parser — an ALTER-only copy would be a
11341                // second place for the two to drift.
11342                if self.peek_mysql_inline_key_start() {
11343                    return Ok(match self.parse_mysql_inline_key()? {
11344                        Some(c) => {
11345                            alloc::vec![crate::ast::AlterTableTarget::AddTableConstraint(c)]
11346                        }
11347                        // FULLTEXT / SPATIAL parse and are accepted as a
11348                        // no-op here exactly as they are inline.
11349                        None => Vec::new(),
11350                    });
11351                }
11352                // v7.14.0 — ADD CONSTRAINT <name> { FOREIGN KEY |
11353                // PRIMARY KEY | UNIQUE | CHECK }. pg_dump emits
11354                // PRIMARY KEY this way; mysqldump emits both.
11355                // Peek-only dispatch (no advance) — `advance()`
11356                // destructively replaces consumed tokens with Eof,
11357                // so saved-pos restore would land on Eofs.
11358                if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("constraint"))
11359                {
11360                    // The next-but-one ident is the constraint
11361                    // name; the one after THAT is the kind.
11362                    let kind_pos = self.pos + 2;
11363                    let kind = self.tokens.get(kind_pos).cloned();
11364                    if matches!(&kind, Some(Token::Ident(s)) if s.eq_ignore_ascii_case("foreign"))
11365                    {
11366                        let fk = self.parse_table_level_fk()?;
11367                        return Ok(alloc::vec![
11368                            crate::ast::AlterTableTarget::AddForeignKey(fk)
11369                        ]);
11370                    }
11371                    if matches!(&kind, Some(Token::Ident(s)) if s.eq_ignore_ascii_case("primary"))
11372                    {
11373                        self.advance(); // CONSTRAINT
11374                        // v7.39 (read01 round 48) — keep the name; the engine
11375                        // stores it now instead of dropping it on the floor.
11376                        let con_name = self.expect_ident_like()?;
11377                        self.advance(); // PRIMARY
11378                        self.expect_keyword_ident("key")?;
11379                        let cols = self.parse_paren_ident_list("PRIMARY KEY")?;
11380                        // v7.39 (round 711) — the ALTER form carries the
11381                        // timing too (pg_dump writes it here).
11382                        let (deferrable, initially_deferred) =
11383                            self.consume_deferrable_clauses_timed()?;
11384                        return Ok(alloc::vec![
11385                            crate::ast::AlterTableTarget::AddTableConstraint(
11386                                crate::ast::TableConstraint::PrimaryKey {
11387                                    name: Some(con_name),
11388                                    columns: cols,
11389                                    deferrable,
11390                                    initially_deferred,
11391                                }
11392                            )
11393                        ]);
11394                    }
11395                    if matches!(&kind, Some(Token::Ident(s)) if s.eq_ignore_ascii_case("unique"))
11396                    {
11397                        self.advance(); // CONSTRAINT
11398                        // v7.39 (read01 round 48) — keep the name.
11399                        let con_name = self.expect_ident_like()?;
11400                        // v7.22 (mailrs round-13 gap 6) — delegate so
11401                        // the optional `NULLS [NOT] DISTINCT` modifier
11402                        // parses here too (pg_dump emits the ALTER
11403                        // form; semantics enforced by the engine
11404                        // since v7.13).
11405                        let mut uc = self.parse_table_level_unique()?;
11406                        if let crate::ast::TableConstraint::Unique { name, .. } = &mut uc {
11407                            *name = Some(con_name);
11408                        }
11409                        return Ok(alloc::vec![
11410                            crate::ast::AlterTableTarget::AddTableConstraint(uc)
11411                        ]);
11412                    }
11413                    if matches!(&kind, Some(Token::Ident(s)) if s.eq_ignore_ascii_case("check"))
11414                    {
11415                        self.advance(); // CONSTRAINT
11416                        // v7.39 (read01 round 48) — keep the name.
11417                        let con_name = self.expect_ident_like()?;
11418                        self.advance(); // CHECK
11419                        if !matches!(self.peek(), Token::LParen) {
11420                            return Err(self.err(alloc::format!(
11421                                "expected '(' after CHECK, got {:?}", self.peek()
11422                            )));
11423                        }
11424                        self.advance();
11425                        let expr = self.parse_expr(0)?;
11426                        if matches!(self.peek(), Token::RParen) {
11427                            self.advance();
11428                        }
11429                        let not_valid = self.parse_not_valid_suffix();
11430                        return Ok(alloc::vec![
11431                            crate::ast::AlterTableTarget::AddTableConstraint(
11432                                crate::ast::TableConstraint::Check {
11433                                    name: Some(con_name),
11434                                    expr,
11435                                    not_valid,
11436                                }
11437                            )
11438                        ]);
11439                    }
11440                    // v7.39 (round 211) — ADD CONSTRAINT <name> EXCLUDE
11441                    // [USING <am>] (<col> WITH <op>[, …]). pg_dump emits
11442                    // exclusion constraints via this ALTER form.
11443                    if matches!(&kind, Some(Token::Ident(s)) if s.eq_ignore_ascii_case("exclude"))
11444                    {
11445                        self.advance(); // CONSTRAINT
11446                        let con_name = self.expect_ident_like()?;
11447                        let mut ex = self.parse_table_level_exclude()?;
11448                        if let crate::ast::TableConstraint::Exclude { name, .. } = &mut ex {
11449                            *name = Some(con_name);
11450                        }
11451                        return Ok(alloc::vec![
11452                            crate::ast::AlterTableTarget::AddTableConstraint(ex)
11453                        ]);
11454                    }
11455                    // Unknown kind — fall through to FK path which
11456                    // produces a descriptive parse error.
11457                }
11458                let is_fk = matches!(
11459                    self.peek(),
11460                    Token::Ident(s) if s.eq_ignore_ascii_case("constraint")
11461                        || s.eq_ignore_ascii_case("foreign")
11462                );
11463                if is_fk {
11464                    let fk = self.parse_table_level_fk()?;
11465                    return Ok(alloc::vec![crate::ast::AlterTableTarget::AddForeignKey(fk)]);
11466                }
11467                // v7.14.0 — bare ADD PRIMARY KEY / UNIQUE / CHECK
11468                // (no CONSTRAINT prefix) — same dispatch.
11469                match self.peek().clone() {
11470                    Token::Ident(s) if s.eq_ignore_ascii_case("primary") => {
11471                        self.advance();
11472                        self.expect_keyword_ident("key")?;
11473                        let cols = self.parse_paren_ident_list("PRIMARY KEY")?;
11474                        let (deferrable, initially_deferred) =
11475                            self.consume_deferrable_clauses_timed()?;
11476                        return Ok(alloc::vec![
11477                            crate::ast::AlterTableTarget::AddTableConstraint(
11478                                crate::ast::TableConstraint::PrimaryKey {
11479                                    name: None,
11480                                    columns: cols,
11481                                    deferrable,
11482                                    initially_deferred,
11483                                }
11484                            )
11485                        ]);
11486                    }
11487                    Token::Ident(s) if s.eq_ignore_ascii_case("unique") => {
11488                        // v7.22 — delegate (NULLS [NOT] DISTINCT).
11489                        let uc = self.parse_table_level_unique()?;
11490                        return Ok(alloc::vec![
11491                            crate::ast::AlterTableTarget::AddTableConstraint(uc)
11492                        ]);
11493                    }
11494                    // v7.39 (round 652) — bare ADD CHECK (no CONSTRAINT
11495                    // prefix). The other three bare forms were here and
11496                    // this one was not, so it fell through to the column
11497                    // path and came back as "unexpected reserved keyword
11498                    // 'check' at start of column definition".
11499                    _ if self.peek_table_level_check_start() => {
11500                        let chk = self.parse_table_level_check()?;
11501                        let not_valid = self.parse_not_valid_suffix();
11502                        let crate::ast::TableConstraint::Check { expr, .. } = chk else {
11503                            unreachable!("parse_table_level_check returns Check")
11504                        };
11505                        return Ok(alloc::vec![
11506                            crate::ast::AlterTableTarget::AddTableConstraint(
11507                                crate::ast::TableConstraint::Check {
11508                                    name: None,
11509                                    expr,
11510                                    not_valid,
11511                                }
11512                            )
11513                        ]);
11514                    }
11515                    // v7.39 (round 211) — bare ADD EXCLUDE (no CONSTRAINT prefix).
11516                    Token::Ident(s) if s.eq_ignore_ascii_case("exclude") => {
11517                        let ex = self.parse_table_level_exclude()?;
11518                        return Ok(alloc::vec![
11519                            crate::ast::AlterTableTarget::AddTableConstraint(ex)
11520                        ]);
11521                    }
11522                    _ => {}
11523                }
11524                if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("column")) {
11525                    self.advance();
11526                }
11527                let mut if_not_exists = false;
11528                if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("if")) {
11529                    self.advance();
11530                    if !matches!(self.peek(), Token::Not) {
11531                        return Err(self.err(alloc::format!(
11532                            "expected NOT after IF in ALTER TABLE ADD COLUMN, got {:?}",
11533                            self.peek()
11534                        )));
11535                    }
11536                    self.advance();
11537                    if !matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("exists")) {
11538                        return Err(self.err(alloc::format!(
11539                            "expected EXISTS after IF NOT in ALTER TABLE ADD COLUMN, got {:?}",
11540                            self.peek()
11541                        )));
11542                    }
11543                    self.advance();
11544                    if_not_exists = true;
11545                }
11546                // v7.13.2 — mailrs round-6 S3: `ADD COLUMN col TYPE
11547                // REFERENCES other(col) [ON DELETE …]`. parse_column_def
11548                // returns ColumnDef + an optional inline FK.
11549                let (column, col_level_fk) = self.parse_column_def_with_fk()?;
11550                let col_name = column.name.clone();
11551                // v7.39.9 — MySQL says where the column goes.
11552                let position = self.parse_column_position();
11553                let mut out = alloc::vec![crate::ast::AlterTableTarget::AddColumn {
11554                    column,
11555                    if_not_exists,
11556                    position,
11557                }];
11558                if let Some(mut fk) = col_level_fk {
11559                    if fk.columns.is_empty() {
11560                        fk.columns.push(col_name);
11561                    }
11562                    out.push(crate::ast::AlterTableTarget::AddForeignKey(fk));
11563                }
11564                Ok(out)
11565            }
11566            Token::Drop => {
11567                self.advance();
11568                // v7.13.3 — dispatch on the next token. mailrs round-7
11569                // S8 closed DROP COLUMN; round-6 S7 closed
11570                // DROP CONSTRAINT. Both share IF EXISTS / CASCADE /
11571                // RESTRICT modifiers.
11572                //   DROP CONSTRAINT [IF EXISTS] <name> [CASCADE|RESTRICT]
11573                //   DROP [COLUMN] [IF EXISTS] <col> [CASCADE|RESTRICT]
11574                let subject = match self.peek() {
11575                    Token::Ident(s) if s.eq_ignore_ascii_case("constraint") => {
11576                        self.advance();
11577                        "constraint"
11578                    }
11579                    Token::Ident(s) if s.eq_ignore_ascii_case("column") => {
11580                        self.advance();
11581                        "column"
11582                    }
11583                    // v7.39 (round 431) — MySQL `DROP {INDEX|KEY} name`.
11584                    // `INDEX` lexes as the reserved Token::Index, so it is
11585                    // unambiguous. `KEY` is a plain ident, and PG allows a
11586                    // column literally named "key", so only read it as the
11587                    // keyword when a name follows it.
11588                    Token::Index => {
11589                        self.advance();
11590                        "index"
11591                    }
11592                    Token::Ident(s)
11593                        if s.eq_ignore_ascii_case("key")
11594                            && matches!(
11595                                self.tokens.get(self.pos + 1),
11596                                Some(Token::Ident(_) | Token::QuotedIdent(_))
11597                            ) =>
11598                    {
11599                        self.advance();
11600                        "index"
11601                    }
11602                    // PG-canonical bare `DROP <col>` without COLUMN
11603                    // keyword is also valid; treat any other ident
11604                    // as the column name.
11605                    Token::Ident(_) | Token::QuotedIdent(_) => "column",
11606                    other => {
11607                        return Err(self.err(alloc::format!(
11608                            "expected COLUMN / CONSTRAINT after DROP in ALTER TABLE, got {other:?}"
11609                        )));
11610                    }
11611                };
11612                let mut if_exists = false;
11613                if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("if")) {
11614                    let n1 = self.tokens.get(self.pos + 1);
11615                    if matches!(n1, Some(Token::Ident(s)) if s.eq_ignore_ascii_case("exists")) {
11616                        self.advance();
11617                        self.advance();
11618                        if_exists = true;
11619                    }
11620                }
11621                let name = self.expect_ident_like()?;
11622                let mut cascade = false;
11623                if matches!(
11624                    self.peek(),
11625                    Token::Ident(s) if s.eq_ignore_ascii_case("cascade")
11626                        || s.eq_ignore_ascii_case("restrict")
11627                ) {
11628                    if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("cascade"))
11629                    {
11630                        cascade = true;
11631                    }
11632                    self.advance();
11633                }
11634                if subject == "index" {
11635                    Ok(alloc::vec![crate::ast::AlterTableTarget::DropIndex {
11636                        name,
11637                        if_exists,
11638                    }])
11639                } else if subject == "constraint" {
11640                    Ok(alloc::vec![crate::ast::AlterTableTarget::DropForeignKey {
11641                        name,
11642                        if_exists,
11643                    }])
11644                } else {
11645                    Ok(alloc::vec![crate::ast::AlterTableTarget::DropColumn {
11646                        column: name,
11647                        if_exists,
11648                        cascade,
11649                    }])
11650                }
11651            }
11652            Token::Ident(s) if s.eq_ignore_ascii_case("alter") => {
11653                self.advance();
11654                // v7.37.18 (18.16) — `ALTER TABLE … ALTER CONSTRAINT
11655                // <name> {DEFERRABLE|NOT DEFERRABLE} [INITIALLY
11656                // {IMMEDIATE|DEFERRED}]`. SPG enforces constraints
11657                // immediately; accept-and-no-op.
11658                if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("constraint")) {
11659                    self.advance();
11660                    self.consume_until_statement_boundary();
11661                    return Ok(Vec::new());
11662                }
11663                if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("column")) {
11664                    self.advance();
11665                }
11666                let col_name = self.expect_ident_like()?;
11667                match self.peek() {
11668                    Token::Ident(s) if s.eq_ignore_ascii_case("type") => {
11669                        self.advance();
11670                    }
11671                    // v7.14.0 — pg_dump emits BIGSERIAL via
11672                    // `ALTER TABLE … ALTER COLUMN id SET DEFAULT
11673                    // nextval('seq')` (the sequence is created
11674                    // separately). SPG's BIGSERIAL already uses
11675                    // AUTO_INCREMENT; accept SET DEFAULT / DROP
11676                    // DEFAULT / SET NOT NULL / DROP NOT NULL as
11677                    // engine no-ops by consuming the tail.
11678                    Token::Ident(s) if s.eq_ignore_ascii_case("set") => {
11679                        // v7.22 (round-13 T2) — `SET DEFAULT
11680                        // nextval('…')` is how pg_dump spells a
11681                        // SERIAL column (plain integer in CREATE
11682                        // TABLE + this ALTER). It used to be
11683                        // swallowed as a no-op, which silently
11684                        // STRIPPED auto-increment from imported
11685                        // schemas — the first post-import INSERT
11686                        // without an explicit id then violated NOT
11687                        // NULL. Lower it to the auto-increment
11688                        // marker instead.
11689                        let is_default_nextval =
11690                            matches!(self.tokens.get(self.pos + 1), Some(Token::Default))
11691                                && matches!(
11692                                    self.tokens.get(self.pos + 2),
11693                                    Some(Token::Ident(f)) if f.eq_ignore_ascii_case("nextval")
11694                                );
11695                        if is_default_nextval {
11696                            let seq_name = self.scan_sequence_name_until_boundary();
11697                            return Ok(alloc::vec![
11698                                crate::ast::AlterTableTarget::SetColumnAutoIncrement {
11699                                    column: col_name,
11700                                    seq_name,
11701                                }
11702                            ]);
11703                        }
11704                        // v7.37.18 (18.1 + 18.2) — proper lowering.
11705                        self.advance(); // consume "set"
11706                        match self.peek().clone() {
11707                            Token::Default => {
11708                                self.advance();
11709                                let default_expr = self.parse_expr(0)?;
11710                                return Ok(alloc::vec![
11711                                    crate::ast::AlterTableTarget::AlterColumnSetDefault {
11712                                        column: col_name,
11713                                        default_expr,
11714                                    }
11715                                ]);
11716                            }
11717                            Token::Not => {
11718                                self.advance();
11719                                if !matches!(self.peek(), Token::Null) {
11720                                    return Err(self.err(alloc::format!(
11721                                        "expected NULL after ALTER COLUMN SET NOT, got {:?}",
11722                                        self.peek()
11723                                    )));
11724                                }
11725                                self.advance();
11726                                return Ok(alloc::vec![
11727                                    crate::ast::AlterTableTarget::AlterColumnSetNotNull {
11728                                        column: col_name,
11729                                    }
11730                                ]);
11731                            }
11732                            // `SET EXPRESSION AS (expr)` (PG 17) — change a
11733                            // stored generated column's expression and
11734                            // recompute existing rows.
11735                            Token::Ident(s) if s.eq_ignore_ascii_case("expression") => {
11736                                self.advance(); // EXPRESSION
11737                                if matches!(self.peek(), Token::As) {
11738                                    self.advance();
11739                                }
11740                                let expr = self.parse_expr(0)?;
11741                                return Ok(alloc::vec![
11742                                    crate::ast::AlterTableTarget::AlterColumnSetExpression {
11743                                        column: col_name,
11744                                        expr,
11745                                    }
11746                                ]);
11747                            }
11748                            other => {
11749                                // Other SET subjects (STATISTICS,
11750                                // STORAGE, COMPRESSION, …) stay no-ops —
11751                                // storage hints with no SPG semantics.
11752                                let _ = other;
11753                                self.consume_until_statement_boundary();
11754                                return Ok(Vec::new());
11755                            }
11756                        }
11757                    }
11758                    Token::Ident(s) if s.eq_ignore_ascii_case("drop") => {
11759                        self.advance(); // consume "drop"
11760                        return self.parse_alter_column_drop_tail(col_name);
11761                    }
11762                    Token::Drop => {
11763                        self.advance(); // consume Drop token
11764                        return self.parse_alter_column_drop_tail(col_name);
11765                    }
11766                    Token::Ident(s) if s.eq_ignore_ascii_case("add") => {
11767                        // v7.22 (round-13 T2) — `ALTER COLUMN c ADD
11768                        // GENERATED { ALWAYS | BY DEFAULT } AS
11769                        // IDENTITY ( … )`: pg_dump's spelling for
11770                        // identity columns. Same auto-increment
11771                        // lowering as the nextval default; the
11772                        // sequence options inside the parens are
11773                        // no-ops under SPG's max+1 semantics.
11774                        let is_generated = matches!(
11775                            self.tokens.get(self.pos + 1),
11776                            Some(Token::Ident(g)) if g.eq_ignore_ascii_case("generated")
11777                        );
11778                        if !is_generated {
11779                            return Err(self.err(alloc::format!(
11780                                "expected GENERATED after ALTER COLUMN {col_name} ADD, got {:?}",
11781                                self.tokens.get(self.pos + 1)
11782                            )));
11783                        }
11784                        let seq_name = self.scan_sequence_name_until_boundary();
11785                        return Ok(alloc::vec![
11786                            crate::ast::AlterTableTarget::SetColumnAutoIncrement {
11787                                column: col_name,
11788                                seq_name,
11789                            }
11790                        ]);
11791                    }
11792                    // v7.39 (round 220) — `RESTART [WITH n]` on an identity
11793                    // column: floor the next allocated value at n (bare
11794                    // RESTART = restart from the start value, 1).
11795                    Token::Ident(s) if s.eq_ignore_ascii_case("restart") => {
11796                        self.advance();
11797                        let with = if matches!(self.peek(), Token::Ident(w) if w.eq_ignore_ascii_case("with"))
11798                        {
11799                            self.advance();
11800                            let neg = if matches!(self.peek(), Token::Minus) {
11801                                self.advance();
11802                                true
11803                            } else {
11804                                false
11805                            };
11806                            match self.advance() {
11807                                Token::Integer(v) => Some(if neg { -v } else { v }),
11808                                other => {
11809                                    return Err(self.err(alloc::format!(
11810                                        "expected integer after RESTART WITH, got {other:?}"
11811                                    )));
11812                                }
11813                            }
11814                        } else {
11815                            None
11816                        };
11817                        return Ok(alloc::vec![
11818                            crate::ast::AlterTableTarget::AlterColumnRestart {
11819                                column: col_name,
11820                                with,
11821                            }
11822                        ]);
11823                    }
11824                    other => {
11825                        return Err(self.err(alloc::format!(
11826                            "expected TYPE / SET / DROP / ADD after ALTER COLUMN <name>, got {other:?}"
11827                        )));
11828                    }
11829                }
11830                // v7.39 (round 713) — the type parser has consumed a
11831                // trailing `COLLATE <name>` since Phase 2.5, and
11832                // `parse_column_type_name` discarded it: `ALTER COLUMN t
11833                // TYPE text COLLATE "C"` parsed clean and changed
11834                // nothing. Keep the clause; the engine re-collates.
11835                let (new_type, _, _, _, coll, coll_explicit, coll_name, _, _, _, _, _, _, _) =
11836                    self.parse_type_with_implied_flags()?;
11837                let collation = if coll_explicit {
11838                    coll_name.map(|n| (coll, n))
11839                } else {
11840                    None
11841                };
11842                let using = if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("using"))
11843                {
11844                    self.advance();
11845                    Some(self.parse_expr(0)?)
11846                } else {
11847                    None
11848                };
11849                Ok(alloc::vec![crate::ast::AlterTableTarget::AlterColumnType {
11850                    column: col_name,
11851                    new_type,
11852                    using,
11853                    collation,
11854                }])
11855            }
11856            // v7.15.0 — `ALTER TABLE t RENAME [COLUMN] old TO new`.
11857            // PG also supports `RENAME TO new_table` for table-name
11858            // rename; that surface is deferred (pg_dump never emits
11859            // it). If the first post-RENAME ident is `TO`, the user
11860            // is asking for table rename — error with a clear
11861            // message rather than misparsing `TO` as a column name.
11862            Token::Ident(s) if s.eq_ignore_ascii_case("rename") => {
11863                self.advance();
11864                // v7.16.2 — `ALTER TABLE t RENAME TO new_table`
11865                // table-name rename (mailrs round-10 A.5 — used
11866                // by migrate-042's `RENAME TO email_contacts`).
11867                // `TO` lexes as Token::To.
11868                if matches!(self.peek(), Token::To)
11869                    || matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("to"))
11870                {
11871                    self.advance();
11872                    let new = self.expect_ident_like()?;
11873                    return Ok(alloc::vec![crate::ast::AlterTableTarget::RenameTable {
11874                        new,
11875                    }]);
11876                }
11877                // v7.39 (read01 round 48) — `RENAME CONSTRAINT old TO new`.
11878                if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("constraint")) {
11879                    self.advance();
11880                    let old = self.expect_ident_like()?;
11881                    if matches!(self.peek(), Token::To) {
11882                        self.advance();
11883                    } else {
11884                        self.expect_keyword_ident("to")?;
11885                    }
11886                    let new = self.expect_ident_like()?;
11887                    return Ok(alloc::vec![
11888                        crate::ast::AlterTableTarget::RenameConstraint { old, new }
11889                    ]);
11890                }
11891                // v7.39.9 — MySQL's `RENAME {INDEX|KEY} old TO new`.
11892                // PostgreSQL renames an index with its own top-level
11893                // `ALTER INDEX`, so this spelling had nowhere to go and
11894                // answered 1064; MySQL 9.7.2 parses it and answers 1176
11895                // when the key is not there.
11896                if matches!(self.peek(), Token::Index)
11897                    || matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("key"))
11898                {
11899                    self.advance();
11900                    let old = self.expect_ident_like()?;
11901                    if matches!(self.peek(), Token::To) {
11902                        self.advance();
11903                    } else {
11904                        self.expect_keyword_ident("to")?;
11905                    }
11906                    let new = self.expect_ident_like()?;
11907                    return Ok(alloc::vec![crate::ast::AlterTableTarget::RenameIndex {
11908                        old,
11909                        new,
11910                    }]);
11911                }
11912                if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("column")) {
11913                    self.advance();
11914                }
11915                let old = self.expect_ident_like()?;
11916                // `TO` is a reserved keyword token; accept both
11917                // Token::To and Token::Ident("to") for consistency.
11918                if matches!(self.peek(), Token::To) {
11919                    self.advance();
11920                } else {
11921                    self.expect_keyword_ident("to")?;
11922                }
11923                let new = self.expect_ident_like()?;
11924                Ok(alloc::vec![crate::ast::AlterTableTarget::RenameColumn {
11925                    old,
11926                    new,
11927                }])
11928            }
11929            // v7.16.1 — `ALTER TABLE t { ENABLE | DISABLE } TRIGGER
11930            // { ALL | <name> }`. pg_dump --disable-triggers wraps
11931            // every data block with these. Real disable semantics —
11932            // not no-op — because reload correctness assumes the
11933            // triggers don't fire (rows already carry their
11934            // computed values from prod).
11935            Token::Ident(s)
11936                if s.eq_ignore_ascii_case("enable") || s.eq_ignore_ascii_case("disable") =>
11937            {
11938                let enabled = s.eq_ignore_ascii_case("enable");
11939                self.advance();
11940                // PG also accepts ENABLE/DISABLE { REPLICA | ALWAYS }
11941                // TRIGGER … and ENABLE/DISABLE RULE / ROW LEVEL
11942                // SECURITY. v7.16.1 only matches TRIGGER (mailrs's
11943                // pg_dump output) — anything else falls through to
11944                // the catch-all error below.
11945                // v7.22 (round-13 T3) — mysqldump wraps every data
11946                // section in `/*!40000 ALTER TABLE t DISABLE KEYS */`
11947                // + ENABLE KEYS (a MyISAM index-rebuild hint). SPG
11948                // maintains indexes incrementally — engine no-op.
11949                if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("keys")) {
11950                    self.advance();
11951                    return Ok(Vec::new());
11952                }
11953                // v7.37.18 (18.12) — ENABLE/DISABLE ALWAYS TRIGGER
11954                // and ENABLE/DISABLE REPLICA TRIGGER. PG uses these
11955                // to gate triggers on session_replication_role; SPG
11956                // has no replica role, so the prefix is consumed and
11957                // treated identically to the plain ENABLE/DISABLE
11958                // TRIGGER form.
11959                if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("always"))
11960                    || matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("replica"))
11961                {
11962                    self.advance();
11963                }
11964                if !matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("trigger")) {
11965                    return Err(self.err(alloc::format!(
11966                        "expected TRIGGER after {}, got {:?}",
11967                        if enabled { "ENABLE" } else { "DISABLE" },
11968                        self.peek()
11969                    )));
11970                }
11971                self.advance();
11972                // `ALL` lexes as Token::All (reserved); also
11973                // accept Token::Ident("all") for symmetry.
11974                // v7.37.18 (18.12) — USER / REPLICA / ALWAYS post-
11975                // TRIGGER selectors. USER (= all user triggers) is
11976                // semantically ALL here; REPLICA / ALWAYS gate on
11977                // session_replication_role which SPG doesn't track.
11978                // All map to TriggerSelector::All.
11979                let which = if matches!(self.peek(), Token::All)
11980                    || matches!(self.peek(), Token::Ident(s)
11981                        if s.eq_ignore_ascii_case("all")
11982                            || s.eq_ignore_ascii_case("user")
11983                            || s.eq_ignore_ascii_case("replica")
11984                            || s.eq_ignore_ascii_case("always"))
11985                {
11986                    self.advance();
11987                    crate::ast::TriggerSelector::All
11988                } else {
11989                    let name = self.expect_ident_like()?;
11990                    crate::ast::TriggerSelector::Named(name)
11991                };
11992                Ok(alloc::vec![crate::ast::AlterTableTarget::SetTriggerEnabled {
11993                    which,
11994                    enabled,
11995                }])
11996            }
11997            // v7.37.16 (16.3) — ATTACH PARTITION child <bounds>
11998            Token::Ident(s) if s.eq_ignore_ascii_case("attach") => {
11999                self.advance();
12000                if !matches!(self.peek(), Token::Partition)
12001                    && !matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s)
12002                        if s.eq_ignore_ascii_case("partition"))
12003                {
12004                    return Err(self.err(alloc::format!(
12005                        "expected PARTITION after ATTACH, got {:?}",
12006                        self.peek()
12007                    )));
12008                }
12009                self.advance();
12010                let child = self.expect_ident_like()?;
12011                let bounds = self.parse_partition_bounds_tail()?;
12012                Ok(alloc::vec![
12013                    crate::ast::AlterTableTarget::AttachPartition { child, bounds }
12014                ])
12015            }
12016            // v7.37.16 (16.4 + 16.5) — DETACH PARTITION child [CONCURRENTLY] [FINALIZE]
12017            Token::Ident(s) if s.eq_ignore_ascii_case("detach") => {
12018                self.advance();
12019                if !matches!(self.peek(), Token::Partition)
12020                    && !matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s)
12021                        if s.eq_ignore_ascii_case("partition"))
12022                {
12023                    return Err(self.err(alloc::format!(
12024                        "expected PARTITION after DETACH, got {:?}",
12025                        self.peek()
12026                    )));
12027                }
12028                self.advance();
12029                let child = self.expect_ident_like()?;
12030                let mut concurrently = false;
12031                let mut finalize = false;
12032                loop {
12033                    match self.peek().clone() {
12034                        Token::Ident(s) | Token::QuotedIdent(s)
12035                            if s.eq_ignore_ascii_case("concurrently") =>
12036                        {
12037                            self.advance();
12038                            concurrently = true;
12039                        }
12040                        Token::Ident(s) | Token::QuotedIdent(s)
12041                            if s.eq_ignore_ascii_case("finalize") =>
12042                        {
12043                            self.advance();
12044                            finalize = true;
12045                        }
12046                        _ => break,
12047                    }
12048                }
12049                Ok(alloc::vec![crate::ast::AlterTableTarget::DetachPartition {
12050                    child,
12051                    concurrently,
12052                    finalize,
12053                }])
12054            }
12055            other => Err(self.err(alloc::format!(
12056                "expected SET / ADD / DROP / ALTER / RENAME / ENABLE / DISABLE / ATTACH / DETACH in ALTER TABLE, got {other:?}"
12057            ))),
12058        }
12059    }
12060
12061    /// v7.37.16 (16.3) — parse the `FOR VALUES …` / `DEFAULT`
12062    /// tail used by both CREATE TABLE … PARTITION OF and ALTER
12063    /// TABLE … ATTACH PARTITION. Shares the same grammar as
12064    /// `parse_partition_of_tail`'s bounds branch.
12065    /// v7.37.18 (18.1 + 18.2) — parse the tail of `ALTER COLUMN
12066    /// col DROP …`. Accepts `DROP DEFAULT` and `DROP NOT NULL`,
12067    /// lowering each to the respective AlterTableTarget. Any
12068    /// other DROP subject (IDENTITY, EXPRESSION, etc.) stays a
12069    /// no-op via consume_until_statement_boundary.
12070    fn parse_alter_column_drop_tail(
12071        &mut self,
12072        col_name: String,
12073    ) -> Result<Vec<crate::ast::AlterTableTarget>, ParseError> {
12074        match self.peek().clone() {
12075            Token::Default => {
12076                self.advance();
12077                Ok(alloc::vec![
12078                    crate::ast::AlterTableTarget::AlterColumnDropDefault { column: col_name }
12079                ])
12080            }
12081            Token::Not => {
12082                self.advance();
12083                if !matches!(self.peek(), Token::Null) {
12084                    return Err(self.err(alloc::format!(
12085                        "expected NULL after ALTER COLUMN DROP NOT, got {:?}",
12086                        self.peek()
12087                    )));
12088                }
12089                self.advance();
12090                Ok(alloc::vec![
12091                    crate::ast::AlterTableTarget::AlterColumnDropNotNull { column: col_name }
12092                ])
12093            }
12094            // `DROP EXPRESSION [IF EXISTS]` — de-generate a stored
12095            // generated column into a plain column.
12096            Token::Ident(s) if s.eq_ignore_ascii_case("expression") => {
12097                self.advance();
12098                // v7.39 (round 187, U10) — IF EXISTS was consumed but
12099                // dropped, so the engine still errored on a plain
12100                // column; PG's semantics are NOTICE + skip.
12101                let mut if_exists = false;
12102                if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("if")) {
12103                    self.advance();
12104                    if matches!(self.peek(), Token::Ident(e) if e.eq_ignore_ascii_case("exists")) {
12105                        self.advance();
12106                        if_exists = true;
12107                    }
12108                }
12109                Ok(alloc::vec![
12110                    crate::ast::AlterTableTarget::AlterColumnDropExpression {
12111                        column: col_name,
12112                        if_exists,
12113                    }
12114                ])
12115            }
12116            // v7.38 (read01, T28) — `DROP IDENTITY [IF EXISTS]` — de-generate an
12117            // identity column into a plain column.
12118            Token::Ident(s) if s.eq_ignore_ascii_case("identity") => {
12119                self.advance();
12120                let mut if_exists = false;
12121                if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("if")) {
12122                    self.advance();
12123                    if matches!(self.peek(), Token::Ident(e) if e.eq_ignore_ascii_case("exists")) {
12124                        self.advance();
12125                        if_exists = true;
12126                    }
12127                }
12128                Ok(alloc::vec![
12129                    crate::ast::AlterTableTarget::AlterColumnDropIdentity {
12130                        column: col_name,
12131                        if_exists,
12132                    }
12133                ])
12134            }
12135            _ => {
12136                self.consume_until_statement_boundary();
12137                Ok(Vec::new())
12138            }
12139        }
12140    }
12141
12142    /// Parse the optional trailer of `COPY … TO STDOUT`: nothing (text
12143    /// format, no header), the modern `[WITH] ( opt [, opt]* )` list, or
12144    /// the legacy space-separated `[WITH] CSV|TEXT [HEADER] [DELIMITER
12145    /// 'c'] [NULL 'str'] [QUOTE 'c']` spelling.
12146    fn parse_copy_to_options(&mut self) -> Result<crate::ast::CopyOptions, ParseError> {
12147        let mut opts = crate::ast::CopyOptions::default();
12148        if matches!(self.peek(), Token::Eof | Token::Semicolon) {
12149            return Ok(opts);
12150        }
12151        if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("with")) {
12152            self.advance();
12153        }
12154        if matches!(self.peek(), Token::LParen) {
12155            self.advance();
12156            loop {
12157                self.parse_one_copy_option(&mut opts)?;
12158                match self.peek() {
12159                    Token::Comma => {
12160                        self.advance();
12161                    }
12162                    Token::RParen => {
12163                        self.advance();
12164                        break;
12165                    }
12166                    other => {
12167                        return Err(self.err(alloc::format!(
12168                            "expected ',' or ')' in COPY options, got {other:?}"
12169                        )));
12170                    }
12171                }
12172            }
12173        } else {
12174            while !matches!(self.peek(), Token::Eof | Token::Semicolon) {
12175                self.parse_one_copy_option(&mut opts)?;
12176            }
12177        }
12178        if !matches!(self.peek(), Token::Eof | Token::Semicolon) {
12179            return Err(self.err(alloc::format!(
12180                "unexpected token after COPY options: {:?}",
12181                self.peek()
12182            )));
12183        }
12184        Ok(opts)
12185    }
12186
12187    fn parse_one_copy_option(
12188        &mut self,
12189        opts: &mut crate::ast::CopyOptions,
12190    ) -> Result<(), ParseError> {
12191        use crate::ast::CopyFormat;
12192        // The option keyword. NULL lexes as its own token; the rest are
12193        // bare identifiers.
12194        let kw = match self.advance() {
12195            Token::Null => alloc::string::String::from("NULL"),
12196            Token::Ident(s) => s.to_uppercase(),
12197            other => {
12198                return Err(self.err(alloc::format!(
12199                    "expected a COPY option keyword, got {other:?}"
12200                )));
12201            }
12202        };
12203        match kw.as_str() {
12204            "FORMAT" => {
12205                let fmt = self.expect_ident_like()?;
12206                match fmt.to_ascii_uppercase().as_str() {
12207                    "CSV" => opts.format = CopyFormat::Csv,
12208                    "TEXT" => opts.format = CopyFormat::Text,
12209                    other => {
12210                        return Err(self.err(alloc::format!(
12211                            "COPY format \"{}\" not recognized",
12212                            other.to_ascii_lowercase()
12213                        )));
12214                    }
12215                }
12216            }
12217            // Legacy bare format keywords.
12218            "CSV" => opts.format = CopyFormat::Csv,
12219            "TEXT" => opts.format = CopyFormat::Text,
12220            "HEADER" => {
12221                opts.header = match self.peek() {
12222                    Token::True => {
12223                        self.advance();
12224                        true
12225                    }
12226                    Token::False => {
12227                        self.advance();
12228                        false
12229                    }
12230                    Token::Ident(s) if s.eq_ignore_ascii_case("on") => {
12231                        self.advance();
12232                        true
12233                    }
12234                    Token::Ident(s) if s.eq_ignore_ascii_case("off") => {
12235                        self.advance();
12236                        false
12237                    }
12238                    // Bare HEADER (no boolean) means HEADER true.
12239                    _ => true,
12240                };
12241            }
12242            // r1066 (7.38 S5.1) — pgbench 14+ loads with
12243            // `COPY … WITH (FREEZE ON)`. The hint's PG effect is
12244            // vacuum bookkeeping on a freshly created/truncated
12245            // table; SPG's per-statement visibility makes it a
12246            // faithful no-op, and rejecting it aborted `pgbench -i`
12247            // against the drop-in. Accept ON/OFF/bare, change nothing.
12248            "FREEZE" => match self.peek() {
12249                Token::True | Token::False => {
12250                    self.advance();
12251                }
12252                Token::Ident(s)
12253                    if s.eq_ignore_ascii_case("on") || s.eq_ignore_ascii_case("off") =>
12254                {
12255                    self.advance();
12256                }
12257                _ => {}
12258            },
12259            "DELIMITER" | "QUOTE" | "ESCAPE" => {
12260                let s = match self.advance() {
12261                    Token::String(s) => s,
12262                    other => {
12263                        return Err(self.err(alloc::format!(
12264                            "COPY {kw} expects a single-character string, got {other:?}"
12265                        )));
12266                    }
12267                };
12268                // v7.39 (round 247) — PG's wording (0A000), keyword in
12269                // lowercase: "COPY delimiter must be a single one-byte
12270                // character".
12271                let one_byte_err = || {
12272                    self.err(alloc::format!(
12273                        "COPY {} must be a single one-byte character",
12274                        kw.to_ascii_lowercase()
12275                    ))
12276                };
12277                let mut chars = s.chars();
12278                let c = chars.next().ok_or_else(one_byte_err)?;
12279                if chars.next().is_some() || c.len_utf8() != 1 {
12280                    return Err(one_byte_err());
12281                }
12282                match kw.as_str() {
12283                    "DELIMITER" => opts.delimiter = Some(c),
12284                    "QUOTE" => opts.quote = Some(c),
12285                    _ => opts.escape = Some(c),
12286                }
12287            }
12288            // v7.39 (round 247) — `FORCE_QUOTE (col, …)` / `FORCE_QUOTE *`.
12289            "FORCE_QUOTE" => {
12290                if matches!(self.peek(), Token::Star) {
12291                    self.advance();
12292                    opts.force_quote = Some(Vec::new());
12293                } else {
12294                    if !matches!(self.peek(), Token::LParen) {
12295                        return Err(self.err(alloc::format!(
12296                            "expected '(' or '*' after FORCE_QUOTE, got {:?}",
12297                            self.peek()
12298                        )));
12299                    }
12300                    self.advance();
12301                    let mut cols = Vec::new();
12302                    loop {
12303                        cols.push(self.expect_ident_like()?);
12304                        match self.peek() {
12305                            Token::Comma => {
12306                                self.advance();
12307                            }
12308                            Token::RParen => {
12309                                self.advance();
12310                                break;
12311                            }
12312                            other => {
12313                                return Err(self.err(alloc::format!(
12314                                    "expected ',' or ')' in FORCE_QUOTE list, got {other:?}"
12315                                )));
12316                            }
12317                        }
12318                    }
12319                    opts.force_quote = Some(cols);
12320                }
12321            }
12322            "NULL" => {
12323                opts.null_str = Some(match self.advance() {
12324                    Token::String(s) => s,
12325                    other => {
12326                        return Err(self.err(alloc::format!(
12327                            "COPY NULL expects a quoted string, got {other:?}"
12328                        )));
12329                    }
12330                });
12331            }
12332            // v7.39 (round 265) — the two CSV FROM-side column lists. Same
12333            // grammar as FORCE_QUOTE; PG accepts `*` for FORCE_NOT_NULL /
12334            // FORCE_NULL too.
12335            "FORCE_NOT_NULL" | "FORCE_NULL" => {
12336                let cols = self.parse_copy_column_list(&kw)?;
12337                if kw == "FORCE_NOT_NULL" {
12338                    opts.force_not_null = Some(cols);
12339                } else {
12340                    opts.force_null = Some(cols);
12341                }
12342            }
12343            other => {
12344                // PG's wording, lowercased option name.
12345                return Err(self.err(alloc::format!(
12346                    "option \"{}\" not recognized",
12347                    other.to_ascii_lowercase()
12348                )));
12349            }
12350        }
12351        Ok(())
12352    }
12353
12354    /// v7.39 (round 265) — `( col, … )` or `*` after a COPY column-list
12355    /// option (FORCE_QUOTE / FORCE_NOT_NULL / FORCE_NULL). An empty vec
12356    /// is the `*` spelling.
12357    fn parse_copy_column_list(&mut self, kw: &str) -> Result<Vec<String>, ParseError> {
12358        if matches!(self.peek(), Token::Star) {
12359            self.advance();
12360            return Ok(Vec::new());
12361        }
12362        if !matches!(self.peek(), Token::LParen) {
12363            return Err(self.err(alloc::format!(
12364                "expected '(' or '*' after {kw}, got {:?}",
12365                self.peek()
12366            )));
12367        }
12368        self.advance();
12369        let mut cols = Vec::new();
12370        loop {
12371            cols.push(self.expect_ident_like()?);
12372            match self.peek() {
12373                Token::Comma => {
12374                    self.advance();
12375                }
12376                Token::RParen => {
12377                    self.advance();
12378                    break;
12379                }
12380                other => {
12381                    return Err(self.err(alloc::format!(
12382                        "expected ',' or ')' in {kw} list, got {other:?}"
12383                    )));
12384                }
12385            }
12386        }
12387        Ok(cols)
12388    }
12389
12390    fn parse_partition_bounds_tail(
12391        &mut self,
12392    ) -> Result<crate::ast::PartitionOfBoundsAst, ParseError> {
12393        use crate::ast::PartitionOfBoundsAst;
12394        match self.peek() {
12395            Token::Default => {
12396                self.advance();
12397                Ok(PartitionOfBoundsAst::Default)
12398            }
12399            Token::For => {
12400                self.advance();
12401                if !matches!(self.peek(), Token::Values) {
12402                    return Err(
12403                        self.err(format!("expected VALUES after FOR, got {:?}", self.peek()))
12404                    );
12405                }
12406                self.advance();
12407                let want_with = matches!(
12408                    self.peek(),
12409                    Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("with")
12410                );
12411                if want_with {
12412                    self.advance();
12413                    if !matches!(self.peek(), Token::LParen) {
12414                        return Err(self.err(format!(
12415                            "expected '(' after FOR VALUES WITH, got {:?}",
12416                            self.peek()
12417                        )));
12418                    }
12419                    self.advance();
12420                    let (mut modulus, mut remainder): (Option<u32>, Option<u32>) = (None, None);
12421                    loop {
12422                        let key = self.expect_ident_like()?;
12423                        let n = match self.peek().clone() {
12424                            Token::Integer(v) if u32::try_from(v).is_ok() => {
12425                                self.advance();
12426                                v as u32
12427                            }
12428                            other => {
12429                                return Err(self.err(format!(
12430                                    "FOR VALUES WITH: expected unsigned integer literal, got {other:?}"
12431                                )));
12432                            }
12433                        };
12434                        match key.to_ascii_uppercase().as_str() {
12435                            "MODULUS" => modulus = Some(n),
12436                            "REMAINDER" => remainder = Some(n),
12437                            other => {
12438                                return Err(self.err(format!(
12439                                    "FOR VALUES WITH: unknown key {other:?}; \
12440                                     expected MODULUS or REMAINDER"
12441                                )));
12442                            }
12443                        }
12444                        match self.peek() {
12445                            Token::Comma => {
12446                                self.advance();
12447                            }
12448                            Token::RParen => {
12449                                self.advance();
12450                                break;
12451                            }
12452                            other => {
12453                                return Err(self.err(format!(
12454                                    "expected ',' or ')' in FOR VALUES WITH list, got {other:?}"
12455                                )));
12456                            }
12457                        }
12458                    }
12459                    let modulus = modulus
12460                        .ok_or_else(|| self.err("FOR VALUES WITH: missing MODULUS".to_string()))?;
12461                    let remainder = remainder.ok_or_else(|| {
12462                        self.err("FOR VALUES WITH: missing REMAINDER".to_string())
12463                    })?;
12464                    if modulus == 0 {
12465                        return Err(self.err("FOR VALUES WITH: MODULUS must be > 0".to_string()));
12466                    }
12467                    if remainder >= modulus {
12468                        return Err(self.err(format!(
12469                            "FOR VALUES WITH: REMAINDER ({remainder}) must be < MODULUS ({modulus})"
12470                        )));
12471                    }
12472                    return Ok(PartitionOfBoundsAst::Hash { modulus, remainder });
12473                }
12474                match self.peek() {
12475                    Token::From => {
12476                        self.advance();
12477                        let lower = Box::new(self.parse_partition_bound_expr()?);
12478                        if !matches!(self.peek(), Token::To) {
12479                            return Err(self.err(format!(
12480                                "expected TO after FROM (...), got {:?}",
12481                                self.peek()
12482                            )));
12483                        }
12484                        self.advance();
12485                        let upper = Box::new(self.parse_partition_bound_expr()?);
12486                        Ok(PartitionOfBoundsAst::Range { lower, upper })
12487                    }
12488                    Token::In => {
12489                        self.advance();
12490                        if !matches!(self.peek(), Token::LParen) {
12491                            return Err(self.err(format!(
12492                                "expected '(' after FOR VALUES IN, got {:?}",
12493                                self.peek()
12494                            )));
12495                        }
12496                        self.advance();
12497                        let mut values = Vec::new();
12498                        loop {
12499                            values.push(self.parse_expr(0)?);
12500                            match self.peek() {
12501                                Token::Comma => {
12502                                    self.advance();
12503                                }
12504                                Token::RParen => {
12505                                    self.advance();
12506                                    break;
12507                                }
12508                                other => {
12509                                    return Err(self.err(format!(
12510                                        "expected ',' or ')' in FOR VALUES IN list, got {other:?}"
12511                                    )));
12512                                }
12513                            }
12514                        }
12515                        if values.is_empty() {
12516                            return Err(
12517                                self.err("FOR VALUES IN requires at least one literal".to_string())
12518                            );
12519                        }
12520                        Ok(PartitionOfBoundsAst::List { values })
12521                    }
12522                    other => Err(self.err(format!(
12523                        "expected FROM / IN / WITH after FOR VALUES, got {other:?}"
12524                    ))),
12525                }
12526            }
12527            other => Err(self.err(format!(
12528                "expected DEFAULT or FOR VALUES after ATTACH PARTITION child, got {other:?}"
12529            ))),
12530        }
12531    }
12532
12533    /// v7.16.2 — peek for `information_schema.<tbl>` /
12534    /// `pg_catalog.<tbl>` triples and, if matched, consume all
12535    /// three tokens + return a synthetic table name the engine's
12536    /// SELECT path recognises as a virtual view. Returns `None`
12537    /// when the head doesn't look like a meta-qualified name.
12538    /// Used by `parse_table_ref` to bypass the
12539    /// `expect_ident_like` schema-strip for these specific PG
12540    /// meta schemas (mailrs round-10 A.3).
12541    fn try_peek_meta_qualified(&mut self) -> Option<(String, String)> {
12542        // Extract the schema name. Must be a plain ident token.
12543        let schema = match self.tokens.get(self.pos) {
12544            Some(Token::Ident(s) | Token::QuotedIdent(s)) => s.clone(),
12545            _ => return None,
12546        };
12547        // Dot.
12548        if !matches!(self.tokens.get(self.pos + 1), Some(Token::Dot)) {
12549            return None;
12550        }
12551        // The table-side ident may lex as a reserved keyword
12552        // (e.g. `Token::Tables`). Tolerate the common ones via a
12553        // helper that reads the trailing token's underlying name.
12554        let tbl = match self.tokens.get(self.pos + 2)? {
12555            Token::Ident(t) | Token::QuotedIdent(t) => t.clone(),
12556            Token::Tables => "tables".to_string(),
12557            // Other PG meta table names that may collide with
12558            // reserved keywords land here as needed.
12559            _ => return None,
12560        };
12561        // Strip the `pg_` prefix from `pg_catalog.pg_class`-style
12562        // names so the synthetic name doesn't double-prefix
12563        // (`__spg_pg_class`, not `__spg_pg_pg_class`).
12564        let (prefix, normalised) = if schema.eq_ignore_ascii_case("information_schema") {
12565            ("__spg_info_", tbl.to_ascii_lowercase())
12566        } else if schema.eq_ignore_ascii_case("pg_catalog") {
12567            // v7.39 (round 541) — only the catalogs SPG actually
12568            // synthesises are rewritten, which is what the BARE path
12569            // has always checked. Anything else keeps its own name and
12570            // takes the ordinary route: `pg_stat_activity` and friends
12571            // resolve through meta_view_result, and a name that is no
12572            // catalog at all gets PG's "relation does not exist"
12573            // instead of a message about a view SPG cannot materialise.
12574            let lowered = tbl.to_ascii_lowercase();
12575            if !SYNTHESISED_PG_CATALOGS.contains(&lowered.as_str()) {
12576                self.advance(); // schema
12577                self.advance(); // dot
12578                self.advance(); // tbl
12579                return Some((lowered.clone(), lowered));
12580            }
12581            let bare = lowered
12582                .strip_prefix("pg_")
12583                .map(alloc::string::String::from)
12584                .unwrap_or(lowered);
12585            ("__spg_pg_", bare)
12586        } else if schema.eq_ignore_ascii_case("mysql") {
12587            // v7.17.0 Phase 3.P0-65 — MySQL system schema
12588            // (`mysql.user`, `mysql.db`). Same synthetic-name
12589            // shape as pg_catalog.
12590            ("__spg_mysql_", tbl.to_ascii_lowercase())
12591        } else {
12592            return None;
12593        };
12594        self.advance(); // schema
12595        self.advance(); // dot
12596        self.advance(); // tbl
12597        Some((
12598            alloc::format!("{prefix}{normalised}"),
12599            tbl.to_ascii_lowercase(),
12600        ))
12601    }
12602
12603    /// Unqualified PG meta-table names (`FROM pg_extension`, `FROM
12604    /// pg_class`) resolve the same way: PG puts `pg_catalog` at the
12605    /// implicit front of every search_path, so a bare reference to a
12606    /// known catalog table always means the catalog table. Only the
12607    /// names the engine actually synthesises are recognised — any
12608    /// other `pg_*` ident stays a user table (mailrs embed round-12).
12609    fn try_peek_meta_bare(&mut self) -> Option<(String, String)> {
12610        // v7.38 (read01 P3.21) — every catalog view SPG synthesises
12611        // (`__spg_pg_*`) is bare-resolvable, matching PG's implicit
12612        // `pg_catalog` at the front of every search_path. (pg_stat_activity
12613        // / pg_stat_statements / pg_locks / pg_statio_user_tables route
12614        // through the meta_view_result path instead, and already resolve
12615        // bare — they must NOT be listed here or the __spg_ rewrite would
12616        // mis-target them.)
12617        const PG_META_TABLES: &[&str] = SYNTHESISED_PG_CATALOGS;
12618        let name = match self.tokens.get(self.pos) {
12619            Some(Token::Ident(s)) => s.to_ascii_lowercase(),
12620            _ => return None,
12621        };
12622        // A following dot means this ident is a schema qualifier,
12623        // not a table name — let the qualified path handle it.
12624        if matches!(self.tokens.get(self.pos + 1), Some(Token::Dot)) {
12625            return None;
12626        }
12627        if !PG_META_TABLES.contains(&name.as_str()) {
12628            return None;
12629        }
12630        self.advance();
12631        let bare = name.strip_prefix("pg_").unwrap_or(&name);
12632        Some((alloc::format!("__spg_pg_{bare}"), name.clone()))
12633    }
12634
12635    /// Consume a bare ident if its lowercase matches `kw`, else err.
12636    /// v7.39 (read01 round 57) — is the next token this bare keyword-ident?
12637    /// Peeks only; the caller advances.
12638    fn peek_keyword_ident(&self, kw: &str) -> bool {
12639        matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case(kw))
12640    }
12641
12642    fn expect_keyword_ident(&mut self, kw: &str) -> Result<(), ParseError> {
12643        match self.advance() {
12644            Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case(kw) => Ok(()),
12645            other => Err(ParseError {
12646                message: format!("expected {kw:?}, got {other:?}"),
12647                token_pos: self.consumed_pos(),
12648            }),
12649        }
12650    }
12651
12652    /// Accept either a quoted identifier (`"foo"`) or a quoted string
12653    /// literal (`'foo'`) — same shape used by CREATE USER for the
12654    /// username slot.
12655    fn expect_ident_or_string(&mut self) -> Result<String, ParseError> {
12656        match self.advance() {
12657            Token::Ident(s) | Token::QuotedIdent(s) | Token::String(s) => Ok(s),
12658            other => Err(ParseError {
12659                message: format!("expected identifier or string, got {other:?}"),
12660                token_pos: self.consumed_pos(),
12661            }),
12662        }
12663    }
12664
12665    fn expect_string_literal(&mut self) -> Result<String, ParseError> {
12666        match self.advance() {
12667            Token::String(s) => Ok(s),
12668            other => Err(ParseError {
12669                message: format!("expected quoted string, got {other:?}"),
12670                token_pos: self.consumed_pos(),
12671            }),
12672        }
12673    }
12674
12675    fn parse_select_stmt(&mut self) -> Result<Statement, ParseError> {
12676        // v7.30.2 (mailrs round-25 ask 2) — derived tables /
12677        // subqueries recurse through here without passing
12678        // parse_expr; share the same nesting budget.
12679        self.enter_nested()?;
12680        let r = self.parse_select_stmt_inner();
12681        self.nest_depth -= 1;
12682        r
12683    }
12684
12685    fn parse_select_stmt_inner(&mut self) -> Result<Statement, ParseError> {
12686        // Caller dispatches on Token::Select; the inner helper handles
12687        // the rest. ORDER BY / LIMIT bind at this top level; UNION peers
12688        // get a fresh bare-select parse and may not have their own ORDER
12689        // BY / LIMIT.
12690        let mut head = self.parse_bare_select()?;
12691        let into = self.pending_select_into.take();
12692        self.parse_setop_chain_into(&mut head)?;
12693        self.parse_select_tail_into(&mut head)?;
12694        // v7.38.19 — `SELECT … INTO t` lowers to the SAME node as
12695        // `CREATE TABLE t AS SELECT …`, which is what a comment in
12696        // `ast.rs` has claimed since v7.38 and what only CTAS actually
12697        // did. The tail (ORDER BY / LIMIT) is parsed first so it belongs
12698        // to the body, as it does in PostgreSQL.
12699        if let Some((name, temporary)) = into {
12700            return Ok(Statement::CreateMaterializedView(
12701                crate::ast::CreateMaterializedViewStatement {
12702                    temporary,
12703                    name,
12704                    if_not_exists: false,
12705                    columns: Vec::new(),
12706                    body: head,
12707                    with_data: true,
12708                    as_plain_table: true,
12709                },
12710            ));
12711        }
12712        Ok(Statement::Select(head))
12713    }
12714
12715    /// v7.37.17 (17.6 siblings) — the three SQL set operations
12716    /// share the peer chain: UNION [ALL], EXCEPT [ALL] (a reserved
12717    /// token), and INTERSECT [ALL] (a bare ident — it was never
12718    /// reserved in SPG's lexer). PG precedence: INTERSECT binds
12719    /// tighter than UNION / EXCEPT — the executor folds the chain
12720    /// left-to-right, which is already correct for LEADING
12721    /// intersects; an INTERSECT pair that FOLLOWS a union/except
12722    /// pair nests into that previous peer, so A UNION B INTERSECT C
12723    /// = A ∪ (B ∩ C). Shared by the top level and parenthesized
12724    /// groups.
12725    fn parse_setop_chain_into(&mut self, head: &mut SelectStatement) -> Result<(), ParseError> {
12726        // A parenthesized group arrives with its own (already
12727        // regrouped) unions on `head`; only the pairs THIS chain
12728        // appends participate in the precedence regroup below —
12729        // nesting an outer INTERSECT into a group-internal peer
12730        // would dissolve the explicit grouping.
12731        let boundary = head.unions.len();
12732        loop {
12733            let base = match self.peek() {
12734                Token::Union => UnionKind::Distinct,
12735                Token::Except => UnionKind::Except,
12736                Token::Ident(s) if s.eq_ignore_ascii_case("intersect") => UnionKind::Intersect,
12737                _ => break,
12738            };
12739            self.advance();
12740            let kind = if matches!(self.peek(), Token::All) {
12741                self.advance();
12742                match base {
12743                    UnionKind::Distinct => UnionKind::All,
12744                    UnionKind::Except => UnionKind::ExceptAll,
12745                    _ => UnionKind::IntersectAll,
12746                }
12747            } else {
12748                base
12749            };
12750            let peer = self.parse_bare_select()?;
12751            head.unions.push((kind, peer));
12752        }
12753        let mut pairs = core::mem::take(&mut head.unions);
12754        let tail = pairs.split_off(boundary);
12755        let mut regrouped: Vec<(UnionKind, SelectStatement)> = pairs;
12756        for (kind, peer) in tail {
12757            let is_intersect = matches!(kind, UnionKind::Intersect | UnionKind::IntersectAll);
12758            // An intersect nests into the previous element of THIS
12759            // chain only; with no new previous element it stays at
12760            // the outer level (the left fold applies it to the
12761            // whole head, group included).
12762            match (
12763                is_intersect,
12764                regrouped.len() > boundary,
12765                regrouped.last_mut(),
12766            ) {
12767                (true, true, Some((_, prev))) => prev.unions.push((kind, peer)),
12768                _ => regrouped.push((kind, peer)),
12769            }
12770        }
12771        head.unions = regrouped;
12772        Ok(())
12773    }
12774
12775    /// v7.37.17 (17.6 siblings) — the shared SELECT tail: ORDER BY /
12776    /// LIMIT / OFFSET / FETCH FIRST / FOR-lock clauses. Extracted so
12777    /// the top-level bare VALUES statement reuses it verbatim.
12778    /// v6.4.0 — parse an optional `ORDER BY <expr> [ASC|DESC] [NULLS …], …`
12779    /// clause into its key list (empty when no `ORDER BY` follows). Extracted
12780    /// (v7.39 round 135) so the grouping-set path can parse ORDER BY early,
12781    /// where the grouping-set universe is still in scope.
12782    fn parse_order_by_keys(&mut self) -> Result<Vec<OrderBy>, ParseError> {
12783        if !matches!(self.peek(), Token::Order) {
12784            return Ok(Vec::new());
12785        }
12786        self.advance();
12787        if !self.peek_is_by() {
12788            return Err(self.err(format!("expected BY after ORDER, got {:?}", self.peek())));
12789        }
12790        self.advance();
12791        let mut keys = Vec::new();
12792        loop {
12793            // v7.39 (round 691) — save/restore, the discipline this parser
12794            // already uses around `pending_sample_preds`, so a subquery inside
12795            // a key neither inherits nor leaks the channel.
12796            let saved_flag = core::mem::replace(&mut self.in_order_by_key, true);
12797            let saved_coll = self.order_key_collation.take();
12798            let parsed = self.parse_expr(0);
12799            self.in_order_by_key = saved_flag;
12800            let collation = core::mem::replace(&mut self.order_key_collation, saved_coll);
12801            let expr = parsed?;
12802            let desc = if matches!(self.peek(), Token::Desc) {
12803                self.advance();
12804                true
12805            } else if matches!(self.peek(), Token::Asc) {
12806                self.advance();
12807                false
12808            } else if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("using")) {
12809                // `ORDER BY x USING <op>` — PG's operator-class spelling. SPG has
12810                // one ordering per type, so the btree comparison operators map
12811                // onto it: < / <= are ASC, > / >= are DESC. Any other operator
12812                // would need a custom operator class — honest error.
12813                self.advance();
12814                match self.advance() {
12815                    Token::Lt | Token::LtEq => false,
12816                    Token::Gt | Token::GtEq => true,
12817                    other => {
12818                        return Err(self.err(alloc::format!(
12819                            "ORDER BY USING supports the btree comparison \
12820                             operators (< <= > >=); got {other:?}"
12821                        )));
12822                    }
12823                }
12824            } else {
12825                false
12826            };
12827            // v7.24 (round-16 A) — explicit NULLS FIRST/LAST.
12828            let nulls_first = self.parse_optional_nulls_placement()?;
12829            keys.push(OrderBy {
12830                expr,
12831                desc,
12832                nulls_first,
12833                collation,
12834            });
12835            if matches!(self.peek(), Token::Comma) {
12836                self.advance();
12837            } else {
12838                break;
12839            }
12840        }
12841        Ok(keys)
12842    }
12843
12844    fn parse_select_tail_into(&mut self, head: &mut SelectStatement) -> Result<(), ParseError> {
12845        // v7.39 (round 135) — a grouping-set query may have already parsed +
12846        // rewritten its ORDER BY (to reference synthetic grouping columns); if
12847        // no ORDER BY token is present, keep that pre-set order_by rather than
12848        // clobbering it with an empty list.
12849        let parsed_keys = self.parse_order_by_keys()?;
12850        head.order_by = if parsed_keys.is_empty() {
12851            core::mem::take(&mut head.order_by)
12852        } else {
12853            parsed_keys
12854        };
12855        // v7.39 (round 314, V39) — the row-count clauses come in EITHER
12856        // order. PG's grammar takes a limit clause and an offset clause
12857        // as an unordered pair, so `OFFSET 2 LIMIT 3` means exactly what
12858        // `LIMIT 3 OFFSET 2` does (measured: same rows). This used to
12859        // parse them in a fixed LIMIT-then-OFFSET sequence, so the other
12860        // spelling died on `expected end of input, got Limit`.
12861        //
12862        // Each may appear at most once, and LIMIT and FETCH FIRST are
12863        // two spellings of the same clause — PG rejects `LIMIT 1 LIMIT 2`,
12864        // `OFFSET 1 OFFSET 2` and `LIMIT 2 FETCH FIRST 3 ROWS ONLY` alike.
12865        // A second one is left unconsumed here, which the caller reports
12866        // as trailing input rather than silently taking the last.
12867        let mut saw_limit = false;
12868        let mut saw_offset = false;
12869        loop {
12870            if !saw_limit && matches!(self.peek(), Token::Limit) {
12871                self.advance();
12872                // v7.17.0 Phase 5.1 — `LIMIT NULL` / `LIMIT ALL` are
12873                // PG synonyms for "no limit". Treat both as None
12874                // (no head.limit set) so the engine's existing
12875                // unlimited-result path takes over. Reject was the
12876                // pre-5.1 behaviour and broke pg_dump-flavoured
12877                // tooling that occasionally emits LIMIT NULL.
12878                if self.consume_limit_unbounded_sentinel() {
12879                    head.limit = None;
12880                } else {
12881                    let first = self.parse_limit_expr("LIMIT")?;
12882                    // MySQL `LIMIT offset, count` — the first number is
12883                    // the offset when a comma follows.
12884                    if matches!(self.peek(), Token::Comma) {
12885                        self.advance();
12886                        let count = self.parse_limit_expr("LIMIT")?;
12887                        head.offset = Some(first);
12888                        saw_offset = true;
12889                        head.limit = Some(count);
12890                    } else {
12891                        head.limit = Some(first);
12892                    }
12893                }
12894                saw_limit = true;
12895                continue;
12896            }
12897            if !saw_offset && matches!(self.peek(), Token::Offset) {
12898                self.advance();
12899                // PG also accepts an optional `ROW` / `ROWS` trailer
12900                // after the offset value (`OFFSET 10 ROWS`). The
12901                // FETCH-FIRST branch below relies on the same.
12902                let off = self.parse_limit_expr("OFFSET")?;
12903                self.consume_optional_rows_keyword();
12904                head.offset = Some(off);
12905                saw_offset = true;
12906                continue;
12907            }
12908            // v7.17.0 Phase 5.1 — `FETCH FIRST <int|$N> ROWS ONLY` is
12909            // the SQL-standard alias for LIMIT. PG accepts both
12910            // spellings interchangeably; pg_dump emits FETCH FIRST in
12911            // newer versions. We map it onto `head.limit` so the
12912            // engine path is unified.
12913            if !saw_limit
12914                && matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s)
12915                    if s.eq_ignore_ascii_case("fetch"))
12916            {
12917                self.advance(); // FETCH
12918                // `FIRST` or `NEXT` (both legal per SQL standard).
12919                if matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s)
12920                    if s.eq_ignore_ascii_case("first") || s.eq_ignore_ascii_case("next"))
12921                {
12922                    self.advance();
12923                }
12924                // Count (optional in the bare `FETCH FIRST ROW ONLY` —
12925                // implicit 1 — but we always consume one if present).
12926                let count = if matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s)
12927                    if s.eq_ignore_ascii_case("row") || s.eq_ignore_ascii_case("rows"))
12928                {
12929                    // Bare `FETCH FIRST ROW ONLY` = LIMIT 1.
12930                    crate::ast::LimitExpr::Literal(1)
12931                } else {
12932                    self.parse_limit_expr("FETCH FIRST")?
12933                };
12934                // Eat `ROW` / `ROWS` if not already consumed above.
12935                self.consume_optional_rows_keyword();
12936                // Optional `ONLY` (the spec form) — or the SQL:2008
12937                // `WITH TIES` form. v7.17.0 Phase 3.P0-49: the executor
12938                // now honours WITH TIES by extending past the LIMIT
12939                // truncation point through every row that shares the
12940                // last-kept row's ORDER BY key.
12941                if matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s)
12942                    if s.eq_ignore_ascii_case("only"))
12943                {
12944                    self.advance();
12945                } else if matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s)
12946                    if s.eq_ignore_ascii_case("with"))
12947                {
12948                    self.advance(); // WITH
12949                    if matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s)
12950                        if s.eq_ignore_ascii_case("ties"))
12951                    {
12952                        self.advance();
12953                        head.limit_with_ties = true;
12954                    }
12955                }
12956                head.limit = Some(count);
12957                saw_limit = true;
12958                continue;
12959            }
12960            break;
12961        }
12962        // v7.17.0 Phase 3.4 — trailing row-lock clauses:
12963        //   FOR { UPDATE | NO KEY UPDATE | SHARE | KEY SHARE }
12964        //       [ OF table_name [, …] ]
12965        //       [ NOWAIT | SKIP LOCKED ]
12966        // Multiple FOR clauses may stack (PG: `FOR UPDATE OF t1
12967        // FOR SHARE OF t2`). SPG is a single-writer engine — every
12968        // SELECT already returns a consistent snapshot — so these
12969        // are accept-and-discard: the parser absorbs them so
12970        // mailrs / Rails / Django code paths that emit `SELECT
12971        // … FOR UPDATE` for advisory pessimistic locking load
12972        // without a parser error. The on-disk locking model is
12973        // unchanged; callers that rely on FOR UPDATE for read-
12974        // through-write ordering still get the right answer
12975        // because SPG serialises writes anyway.
12976        head.locking = self
12977            .consume_optional_for_lock_clauses()
12978            .map(alloc::boxed::Box::new);
12979        Ok(())
12980    }
12981
12982    /// v7.17.0 Phase 3.4 — eat zero or more `FOR { UPDATE | NO KEY
12983    /// UPDATE | SHARE | KEY SHARE } [ OF tbl[, …] ] [ NOWAIT | SKIP
12984    /// LOCKED ]` trailers. Each clause is fully accepted and
12985    /// discarded — SPG's single-writer model already satisfies the
12986    /// callers' implicit ordering requirement. Stops at the first
12987    /// token that isn't `FOR`.
12988    fn consume_optional_for_lock_clauses(&mut self) -> Option<crate::ast::LockingClause> {
12989        // v7.39 (round 293, E3 Phase 1) — the clause is REPORTED now,
12990        // not discarded. PG keeps the strongest of several clauses; the
12991        // policy of the last one wins, which is what this loop records.
12992        let mut seen: Option<crate::ast::LockingClause> = None;
12993        while matches!(self.peek(), Token::For) {
12994            // v7.37.14 (A2.5-stub) — record that this query asked
12995            // for a row lock the parser is about to silently
12996            // discard. Operators surface the count via
12997            // `spg_sql::silent_for_update_count()` so they can
12998            // gauge how much of the workload depends on advisory
12999            // FOR UPDATE / FOR SHARE / FOR KEY SHARE semantics
13000            // before v7.37.15's per-row tuple locking lands.
13001            crate::record_silent_for_update_clause();
13002            self.advance(); // FOR
13003            // `NO KEY` prefix (PG) — `NO` is reserved-keyword-shaped
13004            // (`Token::Not` isn't it; PG `NO` lexes as Token::Ident).
13005            let mut no_key = false;
13006            let mut key = false;
13007            if matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s)
13008                if s.eq_ignore_ascii_case("no"))
13009            {
13010                self.advance(); // NO
13011                no_key = true;
13012                // The next ident should be KEY but be generous;
13013                // anything followed by UPDATE/SHARE is accepted.
13014                if matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s)
13015                    if s.eq_ignore_ascii_case("key"))
13016                {
13017                    self.advance(); // KEY
13018                }
13019            }
13020            // `KEY` prefix (PG `FOR KEY SHARE`).
13021            if matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s)
13022                if s.eq_ignore_ascii_case("key"))
13023            {
13024                self.advance(); // KEY
13025                key = true;
13026            }
13027            // Lock-strength keyword: UPDATE / SHARE. Required, but
13028            // we're lenient — an unexpected token here just bails
13029            // (we already consumed FOR; caller's downstream
13030            // dispatch will error if anything actually depends on
13031            // the trailing tokens).
13032            let is_update = matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s)
13033                if s.eq_ignore_ascii_case("update"));
13034            if matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s)
13035                if s.eq_ignore_ascii_case("update") || s.eq_ignore_ascii_case("share"))
13036            {
13037                self.advance();
13038                use crate::ast::LockStrength as LS;
13039                let strength = match (is_update, no_key, key) {
13040                    (true, true, _) => LS::NoKeyUpdate,
13041                    (true, _, _) => LS::Update,
13042                    (false, _, true) => LS::KeyShare,
13043                    (false, _, _) => LS::Share,
13044                };
13045                seen = Some(crate::ast::LockingClause {
13046                    strength,
13047                    of_tables: alloc::vec::Vec::new(),
13048                    policy: crate::ast::LockWait::Wait,
13049                });
13050            } else {
13051                // FOR by itself (or `FOR KEY` with nothing after) —
13052                // give up on the lock-clause path. We've already
13053                // advanced past FOR; further attempts to parse
13054                // here would clobber state.
13055                return seen;
13056            }
13057            // Optional `OF tbl[, tbl …]`. mailrs emits this when
13058            // joining and locking only a subset of tables.
13059            if matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s)
13060                if s.eq_ignore_ascii_case("of"))
13061            {
13062                self.advance(); // OF
13063                #[allow(clippy::while_let_loop)]
13064                loop {
13065                    match self.peek() {
13066                        Token::Ident(_) | Token::QuotedIdent(_) => {
13067                            // v7.39 (round 294) — the name is CAPTURED now: PG
13068                            // validates it against the FROM clause, and an
13069                            // uncaptured list silently means "lock everything".
13070                            let mut nm = match self.advance() {
13071                                Token::Ident(n) | Token::QuotedIdent(n) => n,
13072                                _ => alloc::string::String::new(),
13073                            };
13074                            // Optional schema-qualified `schema.table`.
13075                            if matches!(self.peek(), Token::Dot) {
13076                                self.advance();
13077                                if let Token::Ident(n) | Token::QuotedIdent(n) = self.peek().clone()
13078                                {
13079                                    self.advance();
13080                                    nm = n;
13081                                }
13082                            }
13083                            if let Some(c) = seen.as_mut() {
13084                                c.of_tables.push(nm);
13085                            }
13086                        }
13087                        _ => break,
13088                    }
13089                    if matches!(self.peek(), Token::Comma) {
13090                        self.advance();
13091                    } else {
13092                        break;
13093                    }
13094                }
13095            }
13096            // Optional `NOWAIT` | `SKIP LOCKED`.
13097            match self.peek().clone() {
13098                Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("nowait") => {
13099                    self.advance();
13100                    if let Some(c) = seen.as_mut() {
13101                        c.policy = crate::ast::LockWait::NoWait;
13102                    }
13103                }
13104                Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("skip") => {
13105                    self.advance(); // SKIP
13106                    if matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s)
13107                        if s.eq_ignore_ascii_case("locked"))
13108                    {
13109                        self.advance(); // LOCKED
13110                        if let Some(c) = seen.as_mut() {
13111                            c.policy = crate::ast::LockWait::SkipLocked;
13112                        }
13113                    }
13114                }
13115                _ => {}
13116            }
13117            // Loop: PG allows multiple FOR clauses chained.
13118        }
13119        seen
13120    }
13121
13122    /// v7.9.24 — accept `LIMIT <int>` or `LIMIT $N`. mailrs H2.
13123    /// Bind value gets resolved during prepared-statement Execute;
13124    /// the Pratt expression parser would over-accept here (e.g.
13125    /// `LIMIT 5 + 5`), so we narrowly accept only the two PG forms.
13126    /// v7.17.0 Phase 5.1 — consume the `LIMIT NULL` / `LIMIT ALL`
13127    /// sentinel tokens (PG synonyms for "no limit"). Returns true
13128    /// when one was consumed; caller skips the regular
13129    /// limit-value parse and leaves `head.limit` at None.
13130    fn consume_limit_unbounded_sentinel(&mut self) -> bool {
13131        if matches!(self.peek(), Token::Null) {
13132            self.advance();
13133            return true;
13134        }
13135        if matches!(self.peek(), Token::All) {
13136            self.advance();
13137            return true;
13138        }
13139        false
13140    }
13141
13142    /// v7.17.0 Phase 5.1 — eat an optional trailing `ROW` / `ROWS`
13143    /// keyword after a LIMIT / OFFSET / FETCH FIRST value, the
13144    /// SQL-standard shape. No-op when missing.
13145    fn consume_optional_rows_keyword(&mut self) {
13146        if matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s)
13147            if s.eq_ignore_ascii_case("row") || s.eq_ignore_ascii_case("rows"))
13148        {
13149            self.advance();
13150        }
13151    }
13152
13153    /// v7.39 (round 284) — `LIMIT` / `OFFSET` over a general expression.
13154    ///
13155    /// PG's row-count clause takes an `a_expr`, so `LIMIT 1+1` and
13156    /// `OFFSET 2+3` are legal; only `FETCH FIRST` is restricted to a
13157    /// constant, which is why that spelling keeps the token path below.
13158    ///
13159    /// Constants are folded here rather than carried into the tree: the
13160    /// 15+ execution paths that read the row count go through
13161    /// `limit_literal()`, which answers `Option<u32>` — and `None` there
13162    /// means "no limit". A clause the engine could not resolve would
13163    /// therefore return the WHOLE table instead of failing. Folding at
13164    /// parse time keeps that impossible; a non-constant clause is still
13165    /// a clean error (recorded residual — closing it wants a resolution
13166    /// pre-pass on the simple-query path, where `substitute_placeholders`
13167    /// does not run).
13168    fn parse_limit_expr(&mut self, label: &str) -> Result<crate::ast::LimitExpr, ParseError> {
13169        // PG restricts FETCH FIRST to a constant or a PARENTHESISED
13170        // expression: `FETCH FIRST 1+1 ROWS ONLY` is a syntax error, but
13171        // `FETCH FIRST (1+1) ROWS ONLY` and `FETCH FIRST (SELECT 3) ROWS
13172        // ONLY` both work (its grammar takes a c_expr). Both measured
13173        // against PG 18.4 in round 305.
13174        if label == "FETCH FIRST" && !matches!(self.peek(), Token::LParen) {
13175            return self.parse_limit_constant(label);
13176        }
13177        // One pass, no rewind: `advance()` takes each token by
13178        // `mem::replace`, so a consumed token reads back as Eof and this
13179        // parser cannot backtrack. Everything — bare literal included —
13180        // is therefore folded from the parsed expression rather than
13181        // re-read from the token stream.
13182        let start = self.pos;
13183        let e = self.parse_expr(0)?;
13184        if let crate::ast::Expr::Placeholder(n) = e {
13185            return Ok(crate::ast::LimitExpr::Placeholder(n));
13186        }
13187        let neg_label = if label == "OFFSET" { "OFFSET" } else { "LIMIT" };
13188        match fold_limit_constant(&e) {
13189            Some(Ok(v)) if v < 0 => Err(ParseError {
13190                message: alloc::format!("{neg_label} must not be negative"),
13191                token_pos: start,
13192            }),
13193            Some(Ok(v)) => u32::try_from(v)
13194                .map(crate::ast::LimitExpr::Literal)
13195                .map_err(|_| ParseError {
13196                    message: alloc::format!("{label} value too large: {v}"),
13197                    token_pos: start,
13198                }),
13199            Some(Err(message)) => Err(ParseError {
13200                message: message.replace("{L}", neg_label),
13201                token_pos: start,
13202            }),
13203            // v7.39 (round 305, V23) — not foldable at parse time
13204            // (`LIMIT (SELECT 4)`, `LIMIT greatest(2,3)`). Carry the
13205            // expression; the engine evaluates it once before dispatch.
13206            None => Ok(crate::ast::LimitExpr::Expr(alloc::boxed::Box::new(e))),
13207        }
13208    }
13209
13210    fn parse_limit_constant(&mut self, label: &str) -> Result<crate::ast::LimitExpr, ParseError> {
13211        // v7.39 (round 239) — PG's row-count clause takes a bigint with its
13212        // coercion rules, not just an integer token: a NUMERIC rounds half
13213        // away from zero (`LIMIT 2.5` keeps 3 rows), a negative count is
13214        // refused with PG's wording ("LIMIT must not be negative", 2201W /
13215        // 2201X — FETCH FIRST shares LIMIT's), and a string coerces by its
13216        // content, failing as an input-syntax error on the value. General
13217        // expressions (`LIMIT 1+1`) stay unsupported — a recorded residual;
13218        // they need an Expr-carrying LimitExpr variant.
13219        let neg_label = if label == "OFFSET" { "OFFSET" } else { "LIMIT" };
13220        let err_at = |message: alloc::string::String, pos: usize| ParseError {
13221            message,
13222            token_pos: pos,
13223        };
13224        match self.advance() {
13225            Token::Integer(n) if n >= 0 => u32::try_from(n)
13226                .map(crate::ast::LimitExpr::Literal)
13227                .map_err(|_| ParseError {
13228                    message: alloc::format!("{label} value too large: {n}"),
13229                    token_pos: self.consumed_pos(),
13230                }),
13231            Token::Integer(_) => Err(err_at(
13232                alloc::format!("{neg_label} must not be negative"),
13233                self.pos.saturating_sub(1),
13234            )),
13235            Token::Numeric(t) => {
13236                let pos = self.pos.saturating_sub(1);
13237                let v: f64 = t.parse().map_err(|_| {
13238                    err_at(
13239                        alloc::format!("invalid input syntax for type bigint: \"{t}\""),
13240                        pos,
13241                    )
13242                })?;
13243                if v < 0.0 {
13244                    return Err(err_at(
13245                        alloc::format!("{neg_label} must not be negative"),
13246                        pos,
13247                    ));
13248                }
13249                // Round half away from zero — PG's numeric→bigint cast.
13250                // (no_std: no f64::round; v is non-negative, so truncating
13251                // v + 0.5 is the same thing.)
13252                #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
13253                let rounded = (v + 0.5) as u64;
13254                u32::try_from(rounded)
13255                    .map(crate::ast::LimitExpr::Literal)
13256                    .map_err(|_| err_at(alloc::format!("{label} value too large: {t}"), pos))
13257            }
13258            Token::Minus => {
13259                let pos = self.pos.saturating_sub(1);
13260                match self.peek() {
13261                    Token::Integer(_) | Token::Numeric(_) => {
13262                        self.advance();
13263                        Err(err_at(
13264                            alloc::format!("{neg_label} must not be negative"),
13265                            pos,
13266                        ))
13267                    }
13268                    other => Err(err_at(
13269                        alloc::format!(
13270                            "expected non-negative integer or $N placeholder after {label}, got {other:?}"
13271                        ),
13272                        pos,
13273                    )),
13274                }
13275            }
13276            Token::String(t) => {
13277                let pos = self.pos.saturating_sub(1);
13278                match t.trim().parse::<i64>() {
13279                    Ok(n) if n < 0 => Err(err_at(
13280                        alloc::format!("{neg_label} must not be negative"),
13281                        pos,
13282                    )),
13283                    Ok(n) => u32::try_from(n)
13284                        .map(crate::ast::LimitExpr::Literal)
13285                        .map_err(|_| err_at(alloc::format!("{label} value too large: {t}"), pos)),
13286                    Err(_) => Err(err_at(
13287                        alloc::format!("invalid input syntax for type bigint: \"{t}\""),
13288                        pos,
13289                    )),
13290                }
13291            }
13292            Token::Placeholder(n) => Ok(crate::ast::LimitExpr::Placeholder(n)),
13293            other => Err(ParseError {
13294                message: alloc::format!(
13295                    "expected non-negative integer or $N placeholder after {label}, got {other:?}"
13296                ),
13297                token_pos: self.consumed_pos(),
13298            }),
13299        }
13300    }
13301
13302    /// Parse one SELECT block without ORDER BY / LIMIT / UNION chaining —
13303    /// just `[DISTINCT] items [FROM] [WHERE] [GROUP BY]`. Returned with
13304    /// `unions` empty and `order_by` / `limit` `None`; the top-level
13305    /// `parse_select_stmt` is responsible for filling those in.
13306    /// v7.37.17 (17.6 siblings) — rewrite every `grouping(keys…)`
13307    /// call in the expression tree to the per-set integer bitmask
13308    /// (PG semantics: one bit per argument, MSB first; 1 = the key
13309    /// is dropped in this grouping set). Runs during the ROLLUP /
13310    /// CUBE / GROUPING SETS expansion, where the set is known.
13311    /// v7.39 (round 135) — collect the distinct `grouping(...)` calls appearing
13312    /// anywhere in `expr` (an ORDER BY key), without recursing into their args.
13313    fn collect_grouping_calls(expr: &Expr, out: &mut Vec<Expr>) {
13314        if let Expr::FunctionCall { name, .. } = expr
13315            && name.eq_ignore_ascii_case("grouping")
13316        {
13317            if !out.iter().any(|e| e == expr) {
13318                out.push(expr.clone());
13319            }
13320            return;
13321        }
13322        match expr {
13323            Expr::Binary { lhs, rhs, .. } => {
13324                Self::collect_grouping_calls(lhs, out);
13325                Self::collect_grouping_calls(rhs, out);
13326            }
13327            Expr::Unary { expr, .. } | Expr::Cast { expr, .. } | Expr::IsNull { expr, .. } => {
13328                Self::collect_grouping_calls(expr, out)
13329            }
13330            Expr::FunctionCall { args, .. } => {
13331                for a in args {
13332                    Self::collect_grouping_calls(a, out);
13333                }
13334            }
13335            Expr::Case {
13336                operand,
13337                branches,
13338                else_branch,
13339            } => {
13340                if let Some(o) = operand {
13341                    Self::collect_grouping_calls(o, out);
13342                }
13343                for (c, v) in branches {
13344                    Self::collect_grouping_calls(c, out);
13345                    Self::collect_grouping_calls(v, out);
13346                }
13347                if let Some(x) = else_branch {
13348                    Self::collect_grouping_calls(x, out);
13349                }
13350            }
13351            _ => {}
13352        }
13353    }
13354
13355    /// v7.39 (round 135) — replace each `grouping(...)` call in `expr` equal to
13356    /// `grp_exprs[k]` with a reference to the synthetic ordering column
13357    /// `__grp_ord_k` (injected per grouping-set branch).
13358    fn rewrite_grouping_to_col(expr: &mut Expr, grp_exprs: &[Expr]) {
13359        if let Expr::FunctionCall { name, .. } = expr
13360            && name.eq_ignore_ascii_case("grouping")
13361        {
13362            if let Some(k) = grp_exprs.iter().position(|e| e == expr) {
13363                *expr = Expr::Column(crate::ast::ColumnName {
13364                    qualifier: None,
13365                    name: alloc::format!("__grp_ord_{k}"),
13366                });
13367            }
13368            return;
13369        }
13370        match expr {
13371            Expr::Binary { lhs, rhs, .. } => {
13372                Self::rewrite_grouping_to_col(lhs, grp_exprs);
13373                Self::rewrite_grouping_to_col(rhs, grp_exprs);
13374            }
13375            Expr::Unary { expr, .. } | Expr::Cast { expr, .. } | Expr::IsNull { expr, .. } => {
13376                Self::rewrite_grouping_to_col(expr, grp_exprs)
13377            }
13378            Expr::FunctionCall { args, .. } => {
13379                for a in args {
13380                    Self::rewrite_grouping_to_col(a, grp_exprs);
13381                }
13382            }
13383            Expr::Case {
13384                operand,
13385                branches,
13386                else_branch,
13387            } => {
13388                if let Some(o) = operand {
13389                    Self::rewrite_grouping_to_col(o, grp_exprs);
13390                }
13391                for (c, v) in branches {
13392                    Self::rewrite_grouping_to_col(c, grp_exprs);
13393                    Self::rewrite_grouping_to_col(v, grp_exprs);
13394                }
13395                if let Some(x) = else_branch {
13396                    Self::rewrite_grouping_to_col(x, grp_exprs);
13397                }
13398            }
13399            _ => {}
13400        }
13401    }
13402
13403    /// v7.39 (round 242) — one grouping element of PG's GROUP BY grammar,
13404    /// as the list of key sets it contributes. A bare expression is one
13405    /// single-key set; `ROLLUP (u1, …, un)` the n+1 unit-prefixes (largest
13406    /// first); `CUBE` every unit-subset (largest first); `GROUPING SETS`
13407    /// the concatenation of its items' sets, where an item is itself an
13408    /// element, a parenthesized key list, or the empty set `()`. A
13409    /// ROLLUP/CUBE member in parentheses is a composite UNIT: its keys
13410    /// move together.
13411    fn parse_grouping_element(&mut self) -> Result<Vec<Vec<Expr>>, ParseError> {
13412        let is_kw = |t: &Token, kw: &str| matches!(t, Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case(kw));
13413        // ROLLUP ( … ) / CUBE ( … )
13414        if (is_kw(self.peek(), "rollup") || is_kw(self.peek(), "cube"))
13415            && matches!(self.tokens.get(self.pos + 1), Some(Token::LParen))
13416        {
13417            let is_cube = is_kw(self.peek(), "cube");
13418            self.advance(); // ROLLUP / CUBE
13419            self.advance(); // (
13420            let mut units: Vec<Vec<Expr>> = Vec::new();
13421            loop {
13422                if matches!(self.peek(), Token::LParen) {
13423                    // Composite unit: (a, b) rolls up as one.
13424                    self.advance();
13425                    let mut unit = Vec::new();
13426                    if !matches!(self.peek(), Token::RParen) {
13427                        loop {
13428                            unit.push(self.parse_expr(0)?);
13429                            match self.peek() {
13430                                Token::Comma => {
13431                                    self.advance();
13432                                }
13433                                Token::RParen => break,
13434                                other => {
13435                                    return Err(self.err(format!(
13436                                        "expected ',' or ')' in grouping unit, got {other:?}"
13437                                    )));
13438                                }
13439                            }
13440                        }
13441                    }
13442                    self.advance(); // )
13443                    units.push(unit);
13444                } else {
13445                    units.push(alloc::vec![self.parse_expr(0)?]);
13446                }
13447                match self.peek() {
13448                    Token::Comma => {
13449                        self.advance();
13450                    }
13451                    Token::RParen => break,
13452                    other => {
13453                        return Err(self.err(format!(
13454                            "expected ',' or ')' in grouping list, got {other:?}"
13455                        )));
13456                    }
13457                }
13458            }
13459            self.advance(); // )
13460            let flatten = |unit_sel: &[bool]| -> Vec<Expr> {
13461                units
13462                    .iter()
13463                    .zip(unit_sel.iter())
13464                    .filter(|(_, keep)| **keep)
13465                    .flat_map(|(u, _)| u.iter().cloned())
13466                    .collect()
13467            };
13468            let n = units.len();
13469            if is_cube {
13470                let mut subsets: Vec<Vec<bool>> = (0..(1u32 << n))
13471                    .map(|mask| (0..n).map(|i| mask & (1 << i) != 0).collect())
13472                    .collect();
13473                subsets.sort_by_key(|sel| core::cmp::Reverse(sel.iter().filter(|b| **b).count()));
13474                return Ok(subsets.iter().map(|sel| flatten(sel)).collect());
13475            }
13476            return Ok((0..=n)
13477                .rev()
13478                .map(|keep| {
13479                    let sel: Vec<bool> = (0..n).map(|i| i < keep).collect();
13480                    flatten(&sel)
13481                })
13482                .collect());
13483        }
13484        // GROUPING SETS ( item [, item]* )
13485        if is_kw(self.peek(), "grouping")
13486            && matches!(self.tokens.get(self.pos + 1), Some(t) if is_kw(t, "sets"))
13487        {
13488            self.advance(); // GROUPING
13489            self.advance(); // SETS
13490            if !matches!(self.peek(), Token::LParen) {
13491                return Err(self.err(format!(
13492                    "expected '(' after GROUPING SETS, got {:?}",
13493                    self.peek()
13494                )));
13495            }
13496            self.advance(); // outer (
13497            let mut sets: Vec<Vec<Expr>> = Vec::new();
13498            loop {
13499                if matches!(self.peek(), Token::LParen) {
13500                    // A parenthesized key list (or the empty set).
13501                    self.advance();
13502                    let mut set = Vec::new();
13503                    if !matches!(self.peek(), Token::RParen) {
13504                        loop {
13505                            set.push(self.parse_expr(0)?);
13506                            match self.peek() {
13507                                Token::Comma => {
13508                                    self.advance();
13509                                }
13510                                Token::RParen => break,
13511                                other => {
13512                                    return Err(self.err(format!(
13513                                        "expected ',' or ')' in grouping set, got {other:?}"
13514                                    )));
13515                                }
13516                            }
13517                        }
13518                    }
13519                    self.advance(); // )
13520                    sets.push(set);
13521                } else {
13522                    // A nested element: ROLLUP/CUBE/GROUPING SETS or a
13523                    // bare expression.
13524                    sets.extend(self.parse_grouping_element()?);
13525                }
13526                match self.peek() {
13527                    Token::Comma => {
13528                        self.advance();
13529                    }
13530                    Token::RParen => break,
13531                    other => {
13532                        return Err(self.err(format!(
13533                            "expected ',' or ')' after a grouping set, got {other:?}"
13534                        )));
13535                    }
13536                }
13537            }
13538            self.advance(); // outer )
13539            return Ok(sets);
13540        }
13541        Ok(alloc::vec![alloc::vec![self.parse_expr(0)?]])
13542    }
13543
13544    fn substitute_grouping_calls(expr: &mut Expr, dropped: &[Expr]) {
13545        // v7.38 (read01) — a reference to a key that is dropped in this grouping
13546        // set evaluates to NULL, at any depth. Previously only a *top-level*
13547        // select item equal to a dropped key was nullified, so a key nested in
13548        // an expression (`COALESCE(g,'TOTAL')`, `g || sum(v)`) survived as a raw
13549        // column and failed to resolve against the set's synthetic schema.
13550        if dropped.iter().any(|d| d == expr) {
13551            *expr = Expr::Literal(Literal::Null);
13552            return;
13553        }
13554        if let Expr::FunctionCall { name, args } = expr
13555            && name.eq_ignore_ascii_case("grouping")
13556        {
13557            let mut mask: i64 = 0;
13558            for a in args.iter() {
13559                mask <<= 1;
13560                if dropped.iter().any(|d| d == a) {
13561                    mask |= 1;
13562                }
13563            }
13564            // v7.39 (round 242) — wrapped in a cast, NOT a bare integer
13565            // literal: a bare integer in a select item is indistinguishable
13566            // from a positional reference once `ORDER BY 1` substitutes the
13567            // item back in, and the round-232 position check then read the
13568            // mask value as an out-of-range position. The cast changes
13569            // nothing semantically (grouping() is integer).
13570            *expr = Expr::Cast {
13571                expr: alloc::boxed::Box::new(Expr::Literal(Literal::Integer(mask))),
13572                target: crate::ast::CastTarget::Int,
13573            };
13574            return;
13575        }
13576        // Generic recursion over the common expression shapes the
13577        // SELECT list uses; anything without child expressions is
13578        // left alone.
13579        match expr {
13580            Expr::FunctionCall { args, .. } => {
13581                for a in args {
13582                    Self::substitute_grouping_calls(a, dropped);
13583                }
13584            }
13585            Expr::Binary { lhs, rhs, .. } => {
13586                Self::substitute_grouping_calls(lhs, dropped);
13587                Self::substitute_grouping_calls(rhs, dropped);
13588            }
13589            Expr::Unary { expr: inner, .. } => {
13590                Self::substitute_grouping_calls(inner, dropped);
13591            }
13592            Expr::Cast { expr: inner, .. } => {
13593                Self::substitute_grouping_calls(inner, dropped);
13594            }
13595            Expr::Case {
13596                operand,
13597                branches,
13598                else_branch,
13599            } => {
13600                if let Some(op) = operand {
13601                    Self::substitute_grouping_calls(op, dropped);
13602                }
13603                for (w, t) in branches {
13604                    Self::substitute_grouping_calls(w, dropped);
13605                    Self::substitute_grouping_calls(t, dropped);
13606                }
13607                if let Some(e) = else_branch {
13608                    Self::substitute_grouping_calls(e, dropped);
13609                }
13610            }
13611            // v7.38 (read01) — recurse into the remaining child-bearing shapes
13612            // so a dropped key nested in `IS NULL` / `LIKE` / `IN (…)` / `EXTRACT`
13613            // / a subscript / `ANY`/`ALL` is nullified too (`CASE WHEN g IS NULL
13614            // …` is the canonical rollup-total label idiom).
13615            Expr::IsNull { expr: inner, .. } => Self::substitute_grouping_calls(inner, dropped),
13616            Expr::Like { expr, pattern, .. } => {
13617                Self::substitute_grouping_calls(expr, dropped);
13618                Self::substitute_grouping_calls(pattern, dropped);
13619            }
13620            Expr::InList { expr, list, .. } => {
13621                Self::substitute_grouping_calls(expr, dropped);
13622                for item in list {
13623                    Self::substitute_grouping_calls(item, dropped);
13624                }
13625            }
13626            Expr::Extract { source, .. } => Self::substitute_grouping_calls(source, dropped),
13627            Expr::Array(items) => {
13628                for item in items {
13629                    Self::substitute_grouping_calls(item, dropped);
13630                }
13631            }
13632            Expr::ArraySubscript { target, index } => {
13633                Self::substitute_grouping_calls(target, dropped);
13634                Self::substitute_grouping_calls(index, dropped);
13635            }
13636            Expr::ArraySlice { target, lo, hi } => {
13637                Self::substitute_grouping_calls(target, dropped);
13638                if let Some(lo) = lo {
13639                    Self::substitute_grouping_calls(lo, dropped);
13640                }
13641                if let Some(hi) = hi {
13642                    Self::substitute_grouping_calls(hi, dropped);
13643                }
13644            }
13645            Expr::AnyAll { expr, array, .. } => {
13646                Self::substitute_grouping_calls(expr, dropped);
13647                Self::substitute_grouping_calls(array, dropped);
13648            }
13649            _ => {}
13650        }
13651    }
13652
13653    fn parse_bare_select(&mut self) -> Result<SelectStatement, ParseError> {
13654        // v7.37.17 (17.6 siblings) — parenthesized set-operation
13655        // group: `( <select chain> )` usable anywhere a query block
13656        // is (head or peer of an outer chain). The group's own
13657        // unions ride the returned SelectStatement; the executor's
13658        // nested-peer recursion runs them.
13659        if matches!(self.peek(), Token::LParen)
13660            && matches!(
13661                self.tokens.get(self.pos + 1),
13662                Some(Token::Select | Token::LParen | Token::Values)
13663            )
13664        {
13665            self.advance(); // (
13666            self.enter_nested()?;
13667            // v7.37 D.20 — a group whose head is a VALUES list:
13668            // `(VALUES (1),(2)) UNION (VALUES (3))`. Parse the VALUES body,
13669            // otherwise recurse into a nested SELECT/group head.
13670            let mut head = (if matches!(self.peek(), Token::Values) {
13671                self.advance(); // VALUES
13672                self.parse_values_rows_body()
13673            } else {
13674                self.parse_bare_select()
13675            })
13676            .and_then(|mut h| {
13677                self.parse_setop_chain_into(&mut h)?;
13678                Ok(h)
13679            });
13680            self.nest_depth -= 1;
13681            let mut head = match &mut head {
13682                Ok(h) => core::mem::take(h),
13683                Err(_) => return head,
13684            };
13685            // v7.37.17 (17.6 siblings) — group-internal tail:
13686            // `(A UNION B ORDER BY 1 LIMIT 5)`. Parse it into the
13687            // group head, then wrap the group as a derived table
13688            // (SELECT * FROM (group)) so the outer chain / outer
13689            // tail can't clobber the group's own ordering or limit.
13690            let has_tail = matches!(self.peek(), Token::Order | Token::Limit | Token::Offset)
13691                || matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s)
13692                    if s.eq_ignore_ascii_case("fetch"));
13693            if has_tail {
13694                self.parse_select_tail_into(&mut head)?;
13695                head = SelectStatement {
13696                    locking: None,
13697                    ctes: Vec::new(),
13698                    distinct: false,
13699                    distinct_on: Vec::new(),
13700                    items: alloc::vec![SelectItem::Wildcard],
13701                    from: Some(FromClause {
13702                        primary: TableRef {
13703                            name: "subquery".to_string(),
13704                            alias: None,
13705                            only: false,
13706                            as_of_segment: None,
13707                            unnest_expr: None,
13708                            unnest_column_aliases: Vec::new(),
13709                            with_ordinality: false,
13710                            generate_series_args: None,
13711                            lateral_subquery: Some(Box::new(head)),
13712                            jsonb_each_text_arg: None,
13713                            table_fn_call: None,
13714                            rows_from: None,
13715                            json_table: None,
13716                            scalar_fn_item: false,
13717                        },
13718                        joins: Vec::new(),
13719                    }),
13720                    where_: None,
13721                    group_by: None,
13722                    group_by_all: false,
13723                    having: None,
13724                    unions: Vec::new(),
13725                    order_by: Vec::new(),
13726                    limit: None,
13727                    offset: None,
13728                    limit_with_ties: false,
13729                    window_check_exprs: Vec::new(),
13730                };
13731            }
13732            if !matches!(self.peek(), Token::RParen) {
13733                return Err(self.err(format!(
13734                    "expected ')' after parenthesized query group, got {:?}",
13735                    self.peek()
13736                )));
13737            }
13738            self.advance();
13739            return Ok(head);
13740        }
13741        // `TABLE name` shorthand as a query block — valid anywhere
13742        // a SELECT head is (set-op peers included).
13743        if matches!(self.peek(), Token::Table)
13744            && matches!(
13745                self.tokens.get(self.pos + 1),
13746                Some(Token::Ident(_) | Token::QuotedIdent(_))
13747            )
13748        {
13749            return self.parse_table_shorthand();
13750        }
13751        if !matches!(self.peek(), Token::Select) {
13752            return Err(self.err(format!(
13753                "expected SELECT to start a query block, got {:?}",
13754                self.peek()
13755            )));
13756        }
13757        self.advance();
13758        // v7.39.9 — MySQL's `SELECT STRAIGHT_JOIN …` join-order hint.
13759        //
13760        // It sits where `DISTINCT` sits and tells the optimiser to join
13761        // in the written order. SPG plans its own joins, so the hint is
13762        // accepted and not acted on — but it has to PARSE, because as a
13763        // bare identifier it became a column: measured on the published
13764        // image, `SELECT STRAIGHT_JOIN a FROM t` answered `Unknown
13765        // column 'straight_join' in 'field list'` where MySQL 9.7.2
13766        // returns the rows. Only in this position, which is the only one
13767        // MySQL accepts either — a trailing `STRAIGHT_JOIN` is its 1064.
13768        if self.mysql_dialect
13769            && matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("straight_join"))
13770        {
13771            self.advance();
13772        }
13773        let distinct = if matches!(self.peek(), Token::Distinct) {
13774            self.advance();
13775            true
13776        } else {
13777            false
13778        };
13779        // v7.37.17 (17.6 siblings) — `DISTINCT ON (expr [, …])`:
13780        // keep the first row (per ORDER BY) of each group the
13781        // expressions define. Django's .distinct('field') shape.
13782        let distinct_on: Vec<Expr> = if distinct && matches!(self.peek(), Token::On) {
13783            self.advance(); // ON
13784            if !matches!(self.peek(), Token::LParen) {
13785                return Err(self.err(format!(
13786                    "expected '(' after DISTINCT ON, got {:?}",
13787                    self.peek()
13788                )));
13789            }
13790            self.advance();
13791            let mut exprs = Vec::new();
13792            loop {
13793                exprs.push(self.parse_expr(0)?);
13794                match self.peek() {
13795                    Token::Comma => {
13796                        self.advance();
13797                    }
13798                    Token::RParen => break,
13799                    other => {
13800                        return Err(self.err(format!(
13801                            "expected ',' or ')' in DISTINCT ON list, got {other:?}"
13802                        )));
13803                    }
13804                }
13805            }
13806            self.advance(); // )
13807            exprs
13808        } else {
13809            Vec::new()
13810        };
13811        let mut items = self.parse_select_list()?;
13812        // v7.38.19 — `SELECT … INTO <table>`, PostgreSQL's other spelling
13813        // of CTAS. It sits exactly here in PG's grammar, right after the
13814        // target list.
13815        //
13816        // A comment in `ast.rs` has said since v7.38 that CTAS and
13817        // `SELECT INTO` lower to the same node. Only CTAS ever did:
13818        // `SELECT i INTO t FROM src` answered `syntax error at or near
13819        // "INTO"`, which the differential found while measuring what
13820        // PostgreSQL tags each of the five materialising forms with. A
13821        // comment describing a capability the code does not have is the
13822        // defect this version has been finding all day, and this is the
13823        // one it found in the parser.
13824        //
13825        // `INTO` is captured rather than consumed here: the name has to
13826        // travel out of a function that returns a `SelectStatement`, and
13827        // the caller lowers the whole thing to the CTAS node.
13828        if matches!(self.peek(), Token::Into) {
13829            self.advance();
13830            // `TEMP` / `TEMPORARY` / `UNLOGGED` / `TABLE` are modifiers on
13831            // the target, not part of its name. SPG has one storage
13832            // class, so `UNLOGGED` is accepted and means nothing, which
13833            // is what it already means on `CREATE TABLE`.
13834            let mut temporary = false;
13835            loop {
13836                match self.peek().clone() {
13837                    Token::Ident(w) | Token::QuotedIdent(w)
13838                        if w.eq_ignore_ascii_case("temp")
13839                            || w.eq_ignore_ascii_case("temporary") =>
13840                    {
13841                        temporary = true;
13842                        self.advance();
13843                    }
13844                    Token::Ident(w) | Token::QuotedIdent(w)
13845                        if w.eq_ignore_ascii_case("unlogged") =>
13846                    {
13847                        self.advance();
13848                    }
13849                    Token::Table => {
13850                        self.advance();
13851                    }
13852                    _ => break,
13853                }
13854            }
13855            let name = match self.peek().clone() {
13856                Token::Ident(w) | Token::QuotedIdent(w) => {
13857                    self.advance();
13858                    w
13859                }
13860                other => {
13861                    return Err(self.err(alloc::format!(
13862                        "expected a table name after SELECT … INTO, got {other:?}"
13863                    )));
13864                }
13865            };
13866            self.pending_select_into = Some((name, temporary));
13867        }
13868        // Scope the TABLESAMPLE lowering channel to this SELECT:
13869        // stash whatever an enclosing select accumulated, collect
13870        // our own FROM's predicates, restore after the combine.
13871        let enclosing_sample_preds = core::mem::take(&mut self.pending_sample_preds);
13872        let mut from = if matches!(self.peek(), Token::From) {
13873            self.advance();
13874            Some(self.parse_from_clause()?)
13875        } else {
13876            None
13877        };
13878        // v7.37 D.22 — a set-returning function in the projection with no FROM
13879        // (`SELECT unnest(arr)`, `SELECT 'x', generate_series(a,b)`) expands to
13880        // rows. Move the first SRF projection item to a FROM-position derived
13881        // table and replace it in the projection with a reference to its output
13882        // column; sibling scalar columns repeat per SRF row. PG names the output
13883        // column after the function (or its AS alias). Reuses the FROM-SRF
13884        // machinery. Only fires with no FROM — mixed SRF over a real FROM already
13885        // works via the targetlist-SRF path.
13886        // v7.39 (read01 round 69) — lower `(f(args)).*`. With no FROM it IS
13887        // `SELECT * FROM f(args)` — the record's fields become the columns, which
13888        // is exactly what the function's own row shape already is. Anywhere else
13889        // (per outer row, or beside other items) it would need a real record-typed
13890        // projection, so it says so rather than answering something else.
13891        if let [
13892            SelectItem::Expr {
13893                expr: Expr::FunctionCall { name, args },
13894                ..
13895            },
13896        ] = items.as_slice()
13897            && name == "__record_expand"
13898        {
13899            let Some(Expr::FunctionCall {
13900                name: inner_name,
13901                args: inner_args,
13902            }) = args.first()
13903            else {
13904                return Err(self.err(
13905                    "(<expr>).* expands a function's record — it needs a function call".into(),
13906                ));
13907            };
13908            if from.is_some() {
13909                return Err(self.err(
13910                    "(<fn>).* over a FROM clause is not supported — call the function in FROM"
13911                        .into(),
13912                ));
13913            }
13914            let fn_ref = TableRef {
13915                name: inner_name.clone(),
13916                alias: None,
13917                only: false,
13918                as_of_segment: None,
13919                unnest_expr: None,
13920                unnest_column_aliases: Vec::new(),
13921                with_ordinality: false,
13922                generate_series_args: None,
13923                lateral_subquery: None,
13924                jsonb_each_text_arg: None,
13925                table_fn_call: Some(Box::new((
13926                    inner_name.to_ascii_lowercase(),
13927                    inner_args.clone(),
13928                ))),
13929                rows_from: None,
13930                json_table: None,
13931                scalar_fn_item: false,
13932            };
13933            items = alloc::vec![SelectItem::Wildcard];
13934            from = Some(FromClause {
13935                primary: fn_ref,
13936                joins: Vec::new(),
13937            });
13938        }
13939        // v7.39 (read01 round 74) — `(f(args)).*` beside other items, or over a
13940        // FROM, keeps its marker: the ENGINE lowers it, because naming the
13941        // record's fields takes the catalog. It becomes a LATERAL of the same
13942        // function plus one item per declared column — the machinery rounds 65
13943        // and 69 already built.
13944        // v7.39 (read01 round 67) — the lift moves ONE SRF into FROM. With two
13945        // (`SELECT generate_series(1,3), generate_series(10,11)`) PG runs them in
13946        // LOCKSTEP, padding the shorter with NULLs — a shape the lift cannot
13947        // express, since the lifted one becomes a scan and the other would
13948        // expand per its rows (a cross product, not a zip). So when the
13949        // projection holds more than one top-level function call, the lift steps
13950        // aside and the engine's target-list expansion takes the whole list.
13951        let fn_call_items = items
13952            .iter()
13953            .filter(|it| {
13954                matches!(
13955                    it,
13956                    SelectItem::Expr {
13957                        expr: Expr::FunctionCall { .. },
13958                        ..
13959                    }
13960                )
13961            })
13962            .count();
13963        if from.is_none() && fn_call_items <= 1 {
13964            let mut found: Option<(usize, TableRef, String)> = None;
13965            for (i, item) in items.iter().enumerate() {
13966                if let SelectItem::Expr {
13967                    expr: Expr::FunctionCall { name, args },
13968                    alias,
13969                } = item
13970                {
13971                    let lname = name.to_ascii_lowercase();
13972                    let colname = alias.clone().unwrap_or_else(|| lname.clone());
13973                    let (unnest, gs) = match lname.as_str() {
13974                        "unnest" if args.len() == 1 => (Some(Box::new(args[0].clone())), None),
13975                        "generate_series" if (2..=3).contains(&args.len()) => {
13976                            (None, Some(args.clone()))
13977                        }
13978                        // v7.38 (read01) — generate_subscripts(arr, dim) in a
13979                        // no-FROM projection yields the 1-based subscripts, i.e.
13980                        // generate_series(1, array_length(arr, dim)); an invalid
13981                        // dimension makes array_length NULL → 0 rows, as in PG.
13982                        "generate_subscripts" if args.len() == 2 => (
13983                            None,
13984                            Some(alloc::vec![
13985                                Expr::Literal(Literal::Integer(1)),
13986                                Expr::FunctionCall {
13987                                    name: "array_length".to_string(),
13988                                    args: args.clone(),
13989                                },
13990                            ]),
13991                        ),
13992                        // v7.38 (read01, T-srf) — string_to_table / regexp_split_to_table
13993                        // in a no-FROM projection unnest their *_to_array form.
13994                        "string_to_table" | "regexp_split_to_table" => {
13995                            let array_fn = if lname == "string_to_table" {
13996                                "string_to_array"
13997                            } else {
13998                                "regexp_split_to_array"
13999                            };
14000                            (
14001                                Some(Box::new(Expr::FunctionCall {
14002                                    name: array_fn.to_string(),
14003                                    args: args.clone(),
14004                                })),
14005                                None,
14006                            )
14007                        }
14008                        // v7.38 (read01, T15) — jsonb/json_array_elements[_text] in
14009                        // a no-FROM projection expand per element. The scalar form
14010                        // returns the elements as a TEXT array, so unnest over the
14011                        // same call materialises one row each (same rewrite the
14012                        // FROM-clause form uses).
14013                        "jsonb_array_elements"
14014                        | "json_array_elements"
14015                        | "jsonb_array_elements_text"
14016                        | "json_array_elements_text"
14017                            if args.len() == 1 =>
14018                        {
14019                            (
14020                                Some(Box::new(Expr::FunctionCall {
14021                                    name: lname.clone(),
14022                                    args: args.clone(),
14023                                })),
14024                                None,
14025                            )
14026                        }
14027                        // v7.38 (read01, T15) — jsonb/json_path_query(doc, path)
14028                        // in a no-FROM projection expands per match (scalar form
14029                        // returns the matches as a TEXT array → unnest).
14030                        "jsonb_path_query" | "json_path_query" if args.len() == 2 => (
14031                            Some(Box::new(Expr::FunctionCall {
14032                                name: lname.clone(),
14033                                args: args.clone(),
14034                            })),
14035                            None,
14036                        ),
14037                        _ => continue,
14038                    };
14039                    found = Some((
14040                        i,
14041                        TableRef {
14042                            name: colname.clone(),
14043                            alias: Some(colname.clone()),
14044                            only: false,
14045                            as_of_segment: None,
14046                            unnest_expr: unnest,
14047                            unnest_column_aliases: alloc::vec![colname.clone()],
14048                            with_ordinality: false,
14049                            generate_series_args: gs,
14050                            lateral_subquery: None,
14051                            jsonb_each_text_arg: None,
14052                            table_fn_call: None,
14053                            rows_from: None,
14054                            json_table: None,
14055                            scalar_fn_item: false,
14056                        },
14057                        colname,
14058                    ));
14059                    break;
14060                }
14061            }
14062            if let Some((idx, tref, colname)) = found {
14063                from = Some(FromClause {
14064                    primary: tref,
14065                    joins: Vec::new(),
14066                });
14067                items[idx] = SelectItem::Expr {
14068                    expr: Expr::Column(ColumnName {
14069                        qualifier: None,
14070                        name: colname.clone(),
14071                    }),
14072                    alias: Some(colname),
14073                };
14074            }
14075        }
14076        let sample_preds = core::mem::take(&mut self.pending_sample_preds);
14077        let where_ = if matches!(self.peek(), Token::Where) {
14078            self.advance();
14079            Some(self.parse_expr(0)?)
14080        } else {
14081            None
14082        };
14083        let where_ = sample_preds.into_iter().fold(where_, |acc, pred| {
14084            Some(match acc {
14085                Some(w) => Expr::Binary {
14086                    lhs: Box::new(pred),
14087                    op: crate::ast::BinOp::And,
14088                    rhs: Box::new(w),
14089                },
14090                None => pred,
14091            })
14092        });
14093        self.pending_sample_preds = enclosing_sample_preds;
14094        let mut group_by_all = false;
14095        // v7.37.17 (17.6 siblings) — ROLLUP / CUBE / GROUPING SETS
14096        // share one expansion: `grouping_sets` lists the key subsets
14097        // (first = primary, assigned to stmt.group_by; the rest
14098        // become UNION ALL peers), `grouping_universe` is the full
14099        // key list used to compute each peer's dropped keys.
14100        let mut grouping_sets: Vec<Vec<Expr>> = Vec::new();
14101        let mut grouping_universe: Vec<Expr> = Vec::new();
14102        // v7.39 (round 472) — did the GROUP BY end in MySQL's `WITH ROLLUP`?
14103        // A BOOL, not the key list: this frame is the statement parser's, and
14104        // round 430 measured that a `Vec` local here is enough on its own to
14105        // tip the 512 KiB nesting guard. The keys are recoverable from
14106        // `grouping_universe`, which a rollup fills with exactly them.
14107        let mut mysql_rollup = false;
14108        let group_by = if matches!(self.peek(), Token::Group) {
14109            self.advance();
14110            if !self.peek_is_by() {
14111                return Err(self.err(format!("expected BY after GROUP, got {:?}", self.peek())));
14112            }
14113            self.advance();
14114            // v6.4.1 — `GROUP BY ALL` shortcut. Planner expands to
14115            // every non-aggregate SELECT-list item later.
14116            if matches!(self.peek(), Token::All) {
14117                self.advance();
14118                group_by_all = true;
14119                None
14120            } else {
14121                // v7.39 (round 242) — PG's general grouping-element grammar:
14122                // GROUP BY [DISTINCT] element [, element]*, where an element
14123                // is a bare expression, ROLLUP (…), CUBE (…) or GROUPING
14124                // SETS (…) — mixed freely. Each element yields a list of
14125                // key sets; the query's grouping sets are the CARTESIAN
14126                // PRODUCT of the elements' lists (so `a, ROLLUP (b)` is
14127                // {(a,b),(a)}), and DISTINCT drops duplicate sets by
14128                // content. ROLLUP/CUBE members may be composite
14129                // (`ROLLUP ((a, b))` moves a and b as one unit), and a
14130                // GROUPING SETS item may itself be a ROLLUP/CUBE. The old
14131                // parser handled only a lone ROLLUP/CUBE/GS as the whole
14132                // clause.
14133                let distinct_sets = if matches!(self.peek(), Token::Distinct) {
14134                    self.advance();
14135                    true
14136                } else {
14137                    false
14138                };
14139                let mut element_sets: Vec<Vec<Vec<Expr>>> = Vec::new();
14140                loop {
14141                    element_sets.push(self.parse_grouping_element()?);
14142                    if matches!(self.peek(), Token::Comma) {
14143                        self.advance();
14144                    } else {
14145                        break;
14146                    }
14147                }
14148                let mut total: Vec<Vec<Expr>> = alloc::vec![Vec::new()];
14149                for el in &element_sets {
14150                    let mut next: Vec<Vec<Expr>> = Vec::new();
14151                    for base in &total {
14152                        for set in el {
14153                            let mut merged = base.clone();
14154                            for k in set {
14155                                if !merged.iter().any(|m| m == k) {
14156                                    merged.push(k.clone());
14157                                }
14158                            }
14159                            next.push(merged);
14160                        }
14161                    }
14162                    total = next;
14163                }
14164                // v7.39 (round 472) — MySQL spells a rollup as a SUFFIX:
14165                // `GROUP BY a, b WITH ROLLUP` is PG's `GROUP BY ROLLUP(a, b)`.
14166                // The keys and the aggregates come out identical; the ROW
14167                // ORDER does not, and that is the part a report depends on.
14168                // MySQL interleaves each group's subtotal right after its
14169                // own rows (east/a, east/b, east/NULL, west/a, …, NULL/NULL)
14170                // where the union-of-grouping-sets expansion emits every
14171                // leaf first and then every subtotal. MariaDB REFUSES an
14172                // ORDER BY next to ROLLUP (1221), so a client cannot fix the
14173                // order itself — measured on MariaDB 11 and MySQL 9.7, which
14174                // agree on the order and disagree only on whether ORDER BY
14175                // is allowed (MySQL allows it; SPG allows it too, since
14176                // refusing would break the clients that can write it).
14177                if self.mysql_dialect
14178                    && matches!(self.peek(), Token::Ident(w) if w.eq_ignore_ascii_case("with"))
14179                    && matches!(
14180                        self.tokens.get(self.pos + 1),
14181                        Some(Token::Ident(r)) if r.eq_ignore_ascii_case("rollup")
14182                    )
14183                {
14184                    self.advance(); // WITH
14185                    self.advance(); // ROLLUP
14186                    let keys = total.into_iter().next().unwrap_or_default();
14187                    mysql_rollup = true;
14188                    // n+1 prefixes, largest first — the same expansion
14189                    // `ROLLUP (…)` produces.
14190                    total = (0..=keys.len()).rev().map(|n| keys[..n].to_vec()).collect();
14191                }
14192                if distinct_sets {
14193                    let mut seen: Vec<Vec<String>> = Vec::new();
14194                    total.retain(|set| {
14195                        let mut key: Vec<String> =
14196                            set.iter().map(|e| alloc::format!("{e}")).collect();
14197                        key.sort();
14198                        if seen.contains(&key) {
14199                            false
14200                        } else {
14201                            seen.push(key);
14202                            true
14203                        }
14204                    });
14205                }
14206                if total.len() > 1 {
14207                    let mut universe: Vec<Expr> = Vec::new();
14208                    for set in &total {
14209                        for k in set {
14210                            if !universe.iter().any(|u| u == k) {
14211                                universe.push(k.clone());
14212                            }
14213                        }
14214                    }
14215                    grouping_universe = universe;
14216                    let primary = total[0].clone();
14217                    grouping_sets = total;
14218                    Some(primary)
14219                } else {
14220                    // One set (a plain GROUP BY list, or a single-set
14221                    // spelling like GROUPING SETS ((a, b))). An EMPTY
14222                    // single set — GROUPING SETS (()) — stays
14223                    // `Some(vec![])`: the grand-total group, which must
14224                    // run the aggregate path.
14225                    Some(total.into_iter().next().unwrap_or_default())
14226                }
14227            }
14228        } else {
14229            None
14230        };
14231        let having = if matches!(self.peek(), Token::Having) {
14232            self.advance();
14233            Some(self.parse_expr(0)?)
14234        } else {
14235            None
14236        };
14237        // `WINDOW w AS ( <window-def> ) [, ...]` — named windows.
14238        // OVER w parsed to a marker above; inline each definition
14239        // into the referencing WindowFunction nodes.
14240        let mut window_defs: Vec<(String, WindowDef)> = Vec::new();
14241        if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("window")) {
14242            self.advance();
14243            loop {
14244                let wname = self.expect_ident_like()?;
14245                if !matches!(self.peek(), Token::As) {
14246                    return Err(self.err(format!(
14247                        "expected AS after WINDOW {wname}, got {:?}",
14248                        self.peek()
14249                    )));
14250                }
14251                self.advance();
14252                // v7.39 (round 229) — PG rejects a redefinition outright.
14253                if window_defs
14254                    .iter()
14255                    .any(|(n, _)| n.eq_ignore_ascii_case(&wname))
14256                {
14257                    return Err(self.err(alloc::format!("window \"{wname}\" is already defined")));
14258                }
14259                let def = self.parse_over_clause()?;
14260                // A definition may itself copy an earlier one
14261                // (`WINDOW w1 AS (PARTITION BY g), w2 AS (w1 ORDER BY v)`),
14262                // so resolve it against the defs already in scope. Same
14263                // copy rules as an `OVER (w1 …)` in the select list.
14264                let mut probe = Expr::WindowFunction {
14265                    name: String::new(),
14266                    args: Vec::new(),
14267                    partition_by: def.0,
14268                    order_by: def.1,
14269                    frame: def.2,
14270                    null_treatment: crate::ast::NullTreatment::Respect,
14271                    filter: None,
14272                };
14273                Self::substitute_named_windows(&mut probe, &window_defs)
14274                    .map_err(|m| self.err(m))?;
14275                let Expr::WindowFunction {
14276                    partition_by,
14277                    order_by,
14278                    frame,
14279                    ..
14280                } = probe
14281                else {
14282                    unreachable!("probe is a WindowFunction")
14283                };
14284                window_defs.push((wname, (partition_by, order_by, frame)));
14285                if matches!(self.peek(), Token::Comma) {
14286                    self.advance();
14287                    continue;
14288                }
14289                break;
14290            }
14291        }
14292        // v7.39 (round 705) — which definitions did anything reference?
14293        // The ones nothing did used to be dropped here, unexamined, so
14294        // `WINDOW w AS (ORDER BY nosuch)` succeeded — PG analyses every
14295        // definition whether referenced or not. Their key expressions ride
14296        // out on the statement for the engine to resolve.
14297        let mut window_refs: Vec<String> = Vec::new();
14298        if !window_defs.is_empty() {
14299            for it in &items {
14300                if let SelectItem::Expr { expr, .. } = it {
14301                    Self::collect_named_window_refs(expr, &mut window_refs);
14302                }
14303            }
14304        }
14305        let window_check_exprs: Vec<Expr> = window_defs
14306            .iter()
14307            .filter(|(n, _)| !window_refs.iter().any(|r| r.eq_ignore_ascii_case(n)))
14308            .flat_map(|(_, (partition, order, _))| {
14309                partition
14310                    .iter()
14311                    .cloned()
14312                    .chain(order.iter().map(|(e, _, _)| e.clone()))
14313            })
14314            .collect();
14315        if !window_defs.is_empty()
14316            || items
14317                .iter()
14318                .any(|it| matches!(it, SelectItem::Expr { expr, .. } if Self::expr_has_named_window(expr)))
14319        {
14320            for it in &mut items {
14321                if let SelectItem::Expr { expr, .. } = it {
14322                    Self::substitute_named_windows(expr, &window_defs)
14323                        .map_err(|m| self.err(m))?;
14324                }
14325            }
14326        }
14327        // `GROUP BY 1` — positional keys substitute with the Nth
14328        // select item's expression (same contract ORDER BY has had
14329        // since v6.x). Out-of-range positions error.
14330        let group_by = match group_by {
14331            Some(mut keys) => {
14332                for k in &mut keys {
14333                    if let Expr::Literal(Literal::Integer(n)) = k {
14334                        let idx = *n;
14335                        if idx < 1 || idx as usize > items.len() {
14336                            return Err(self.err(alloc::format!(
14337                                "GROUP BY position {idx} is not in select list"
14338                            )));
14339                        }
14340                        match &items[(idx - 1) as usize] {
14341                            SelectItem::Expr { expr, .. } => *k = expr.clone(),
14342                            SelectItem::Wildcard | SelectItem::QualifiedWildcard(_) => {
14343                                return Err(self.err(alloc::format!(
14344                                    "GROUP BY position {idx} references a wildcard item"
14345                                )));
14346                            }
14347                        }
14348                    }
14349                }
14350                Some(keys)
14351            }
14352            None => None,
14353        };
14354        let mut stmt = SelectStatement {
14355            locking: None,
14356            ctes: Vec::new(),
14357            distinct,
14358            distinct_on,
14359            items,
14360            from,
14361            where_,
14362            group_by,
14363            group_by_all,
14364            having,
14365            unions: Vec::new(),
14366            order_by: Vec::new(),
14367            limit: None,
14368            offset: None,
14369            limit_with_ties: false,
14370            window_check_exprs,
14371        };
14372        // Grouping expansion (ROLLUP / CUBE / GROUPING SETS): the
14373        // first set is the primary (already on stmt.group_by); each
14374        // further set becomes a UNION ALL peer with its dropped
14375        // keys (universe minus the set) replaced by NULL literals
14376        // in the peer's items and group_by. PG-legal: non-grouped
14377        // select items must be group keys or aggregates, so a
14378        // dropped key's occurrences in the projection are exactly
14379        // the ones to nullify.
14380        // v7.39 (round 242) — grouping() OUTSIDE an expansion: PG allows it
14381        // over a plain GROUP BY (every argument must be a group key; the
14382        // mask is then 0) and rejects anything else with 42803. SPG's
14383        // rewrite only ran during the ROLLUP/CUBE expansion, so a plain
14384        // `SELECT grouping(a) … GROUP BY a` died at eval with "unknown
14385        // function `grouping`".
14386        if grouping_sets.len() <= 1 {
14387            let keys: Vec<Expr> = stmt.group_by.clone().unwrap_or_default();
14388            let mut calls: Vec<Expr> = Vec::new();
14389            for item in &stmt.items {
14390                if let SelectItem::Expr { expr, .. } = item {
14391                    Self::collect_grouping_calls(expr, &mut calls);
14392                }
14393            }
14394            if let Some(h) = &stmt.having {
14395                Self::collect_grouping_calls(h, &mut calls);
14396            }
14397            for call in &calls {
14398                let Expr::FunctionCall { args, .. } = call else {
14399                    continue;
14400                };
14401                for a in args {
14402                    if !keys.iter().any(|k| k == a) {
14403                        return Err(self.err(
14404                            "arguments to GROUPING must be grouping expressions of the associated query level"
14405                                .to_string(),
14406                        ));
14407                    }
14408                }
14409            }
14410            if !calls.is_empty() {
14411                for item in &mut stmt.items {
14412                    if let SelectItem::Expr { expr, .. } = item {
14413                        Self::substitute_grouping_calls(expr, &[]);
14414                    }
14415                }
14416                if let Some(h) = &mut stmt.having {
14417                    Self::substitute_grouping_calls(h, &[]);
14418                }
14419            }
14420        }
14421        if grouping_sets.len() > 1 {
14422            // The primary set's own dropped keys nullify in the
14423            // HEAD's projection too (GROUPING SETS's first set may
14424            // omit keys other sets use).
14425            let primary = grouping_sets[0].clone();
14426            let head_dropped: Vec<Expr> = grouping_universe
14427                .iter()
14428                .filter(|u| !primary.iter().any(|k| k == *u))
14429                .cloned()
14430                .collect();
14431            for set in grouping_sets.iter().skip(1) {
14432                let mut peer = stmt.clone();
14433                peer.unions = Vec::new();
14434                let dropped: Vec<&Expr> = grouping_universe
14435                    .iter()
14436                    .filter(|u| !set.iter().any(|k| k == *u))
14437                    .collect();
14438                // Empty set = grand-total group: `Some(vec![])` forces
14439                // the aggregate path (one collapsed row) instead of a
14440                // per-row passthrough. See the primary-set note above.
14441                peer.group_by = Some(set.clone());
14442                let dropped_owned: Vec<Expr> = dropped.iter().map(|d| (*d).clone()).collect();
14443                for item in &mut peer.items {
14444                    if let SelectItem::Expr { expr, alias } = item {
14445                        if dropped.iter().any(|d| *d == expr) {
14446                            // v7.39 — keep the dropped key's name on the
14447                            // NULL literal so the UNION output column
14448                            // (and any top-level ORDER BY on it) still
14449                            // resolves.
14450                            if alias.is_none()
14451                                && let Expr::Column(c) = &expr
14452                            {
14453                                *alias = Some(c.name.clone());
14454                            }
14455                            *expr = Expr::Literal(Literal::Null);
14456                        } else {
14457                            Self::substitute_grouping_calls(expr, &dropped_owned);
14458                        }
14459                    }
14460                }
14461                if let Some(h) = &mut peer.having {
14462                    Self::substitute_grouping_calls(h, &dropped_owned);
14463                }
14464                stmt.unions.push((UnionKind::All, peer));
14465            }
14466            for item in &mut stmt.items {
14467                if let SelectItem::Expr { expr, alias } = item {
14468                    if head_dropped.iter().any(|d| d == expr) {
14469                        if alias.is_none()
14470                            && let Expr::Column(c) = &expr
14471                        {
14472                            *alias = Some(c.name.clone());
14473                        }
14474                        *expr = Expr::Literal(Literal::Null);
14475                    } else {
14476                        Self::substitute_grouping_calls(expr, &head_dropped);
14477                    }
14478                }
14479            }
14480            if let Some(h) = &mut stmt.having {
14481                Self::substitute_grouping_calls(h, &head_dropped);
14482            }
14483            // v7.39 (round 135) — GROUPING() in ORDER BY. Parse the ORDER BY now
14484            // (while `grouping_universe` / the per-branch sets are in scope). For
14485            // each grouping() call in it, inject a per-branch hidden column
14486            // `__grp_ord_K` carrying that branch's mask into the head + every
14487            // peer, and rewrite the ORDER BY to reference it. `parse_select_tail_into`
14488            // preserves this pre-set order_by; the engine strips `__grp_ord_*`
14489            // from the final output. A standalone grouping-set query has ORDER BY
14490            // (not an explicit set-op) next, so consuming it here is safe.
14491            // v7.39 (round 472) — absent the client's own ORDER BY, a MySQL
14492            // rollup carries the hierarchical order: sort by the grouping
14493            // keys with the rolled-up NULLs last, which is exactly the
14494            // interleaving both oracles emit. A client's own ORDER BY wins,
14495            // which is what MySQL does (MariaDB refuses to let one be
14496            // written at all).
14497            // The synthesised keys have to travel the SAME path a written
14498            // ORDER BY does: the block below is what turns a `grouping()`
14499            // call into the per-branch `__grp_ord_K` column the engine can
14500            // actually sort on. Bypassing it left a bare `grouping(text)`
14501            // for the evaluator to reject.
14502            let synthesised_or_parsed: Vec<OrderBy> = if matches!(self.peek(), Token::Order) {
14503                self.parse_order_by_keys()?
14504            } else if mysql_rollup {
14505                Self::mysql_rollup_order(&grouping_universe)
14506            } else {
14507                Vec::new()
14508            };
14509            if !synthesised_or_parsed.is_empty() {
14510                let mut order_keys = synthesised_or_parsed;
14511                let mut grp_exprs: Vec<Expr> = Vec::new();
14512                for ob in &order_keys {
14513                    Self::collect_grouping_calls(&ob.expr, &mut grp_exprs);
14514                }
14515                for (k, gexpr) in grp_exprs.iter().enumerate() {
14516                    let colname = alloc::format!("__grp_ord_{k}");
14517                    // Head branch (primary set) uses `head_dropped`.
14518                    let mut he = gexpr.clone();
14519                    Self::substitute_grouping_calls(&mut he, &head_dropped);
14520                    stmt.items.push(SelectItem::Expr {
14521                        expr: he,
14522                        alias: Some(colname.clone()),
14523                    });
14524                    // Each peer `stmt.unions[i]` corresponds to `grouping_sets[i+1]`.
14525                    for (i, (_, peer)) in stmt.unions.iter_mut().enumerate() {
14526                        let set = &grouping_sets[i + 1];
14527                        let dropped: Vec<Expr> = grouping_universe
14528                            .iter()
14529                            .filter(|u| !set.iter().any(|k| k == *u))
14530                            .cloned()
14531                            .collect();
14532                        let mut pe = gexpr.clone();
14533                        Self::substitute_grouping_calls(&mut pe, &dropped);
14534                        peer.items.push(SelectItem::Expr {
14535                            expr: pe,
14536                            alias: Some(colname.clone()),
14537                        });
14538                    }
14539                }
14540                for ob in &mut order_keys {
14541                    Self::rewrite_grouping_to_col(&mut ob.expr, &grp_exprs);
14542                }
14543                stmt.order_by = order_keys;
14544            }
14545        }
14546        Ok(stmt)
14547    }
14548
14549    /// v7.39 (round 472) — the row order MySQL's `WITH ROLLUP` promises,
14550    /// as ORDER BY keys.
14551    ///
14552    /// Per key: the rollup marker, then the key. Sorting on the key alone
14553    /// is not enough, and a table with a NULL in it says why — MariaDB puts
14554    /// the DATA-NULL group where a plain GROUP BY puts it (first) and only
14555    /// the ROLLUP-introduced NULL last, and both print as NULL.
14556    /// `GROUPING(k)` is the one thing that tells them apart: 0 for every
14557    /// real group including the data-NULL one, 1 only for the row the
14558    /// rollup added. Measured on MariaDB 11 — `('a',1),(NULL,2),('b',3)`
14559    /// rolls up to NULL|2, a|1, b|3, NULL|6.
14560    ///
14561    /// `#[inline(never)]`: its locals must not join the statement parser's
14562    /// frame, which round 430 measured sitting against the nesting guard.
14563    #[inline(never)]
14564    fn mysql_rollup_order(keys: &[Expr]) -> Vec<OrderBy> {
14565        let mut out: Vec<OrderBy> = Vec::with_capacity(keys.len() * 2);
14566        for e in keys {
14567            out.push(OrderBy {
14568                expr: Expr::FunctionCall {
14569                    name: "grouping".into(),
14570                    args: alloc::vec![e.clone()],
14571                },
14572                desc: false,
14573                nulls_first: None,
14574                collation: None,
14575            });
14576            out.push(OrderBy {
14577                expr: e.clone(),
14578                desc: false,
14579                // MySQL orders NULL first on an ascending key.
14580                nulls_first: Some(true),
14581                collation: None,
14582            });
14583        }
14584        out
14585    }
14586
14587    /// v7.39 (round 535) — `REINDEX [(opts)] { INDEX | TABLE | SCHEMA |
14588    /// DATABASE | SYSTEM } [CONCURRENTLY] [<name>]`.
14589    #[inline(never)]
14590    fn parse_reindex_tail(&mut self) -> Result<Statement, ParseError> {
14591        use crate::ast::MaintainKind;
14592        self.skip_paren_option_list();
14593        let kind = match self.peek() {
14594            // `TABLE` and `INDEX` lex as keywords, not identifiers.
14595            Token::Table | Token::Index => {
14596                self.advance();
14597                MaintainKind::ReindexRelation
14598            }
14599            Token::Ident(s) | Token::QuotedIdent(s) => match s.to_ascii_lowercase().as_str() {
14600                "index" | "table" => {
14601                    self.advance();
14602                    MaintainKind::ReindexRelation
14603                }
14604                "schema" => {
14605                    self.advance();
14606                    MaintainKind::ReindexSchema
14607                }
14608                "system" | "database" => {
14609                    self.advance();
14610                    MaintainKind::Whole
14611                }
14612                // PG requires the object type; anything else is the
14613                // caller's problem, not something to swallow.
14614                _ => MaintainKind::ReindexRelation,
14615            },
14616            _ => MaintainKind::Whole,
14617        };
14618        // PG bars `REINDEX … CONCURRENTLY` inside a transaction block and
14619        // allows the plain form, so the modifier is recorded rather than
14620        // skipped. It still has no effect on how the reindex runs.
14621        let mut concurrently = false;
14622        if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("concurrently")) {
14623            self.advance();
14624            concurrently = true;
14625        }
14626        let target = self.take_optional_maintain_name();
14627        self.consume_until_statement_boundary();
14628        Ok(Statement::Maintain {
14629            kind,
14630            concurrently,
14631            target,
14632        })
14633    }
14634
14635    /// v7.39 (round 535) — `CLUSTER [VERBOSE] [<table> [USING <index>]]`
14636    /// and `CLUSTER [VERBOSE] <index> ON <table>`.
14637    #[inline(never)]
14638    fn parse_cluster_tail(&mut self) -> Result<Statement, ParseError> {
14639        use crate::ast::MaintainKind;
14640        self.skip_paren_option_list();
14641        if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("verbose")) {
14642            self.advance();
14643        }
14644        let target = self.take_optional_maintain_name();
14645        self.consume_until_statement_boundary();
14646        Ok(Statement::Maintain {
14647            kind: if target.is_some() {
14648                MaintainKind::ClusterRelation
14649            } else {
14650                MaintainKind::Whole
14651            },
14652            // CLUSTER has no CONCURRENTLY form, and PG runs it inside a
14653            // transaction block quite happily (measured).
14654            concurrently: false,
14655            target,
14656        })
14657    }
14658
14659    /// The next token as a relation / schema name, when there is one.
14660    fn take_optional_maintain_name(&mut self) -> Option<alloc::string::String> {
14661        match self.peek() {
14662            Token::Ident(_) | Token::QuotedIdent(_) => match self.advance() {
14663                Token::Ident(n) | Token::QuotedIdent(n) => Some(n),
14664                _ => None,
14665            },
14666            _ => None,
14667        }
14668    }
14669
14670    /// A parenthesised option list, absorbed.
14671    fn skip_paren_option_list(&mut self) {
14672        if !matches!(self.peek(), Token::LParen) {
14673            return;
14674        }
14675        let mut depth = 0usize;
14676        loop {
14677            match self.advance() {
14678                Token::LParen => depth += 1,
14679                Token::RParen => {
14680                    depth -= 1;
14681                    if depth == 0 {
14682                        return;
14683                    }
14684                }
14685                Token::Eof => return,
14686                _ => {}
14687            }
14688        }
14689    }
14690
14691    /// v7.39 (round 531) — the `LIKE` clause inside a CREATE TABLE
14692    /// column list.
14693    ///
14694    /// PG's option names are COMMENTS / COMPRESSION / CONSTRAINTS /
14695    /// DEFAULTS / GENERATED / IDENTITY / INDEXES / STATISTICS / STORAGE
14696    /// / ALL. The three that describe physical storage have no meaning
14697    /// here, so they parse and change nothing rather than making a
14698    /// dump that mentions them fail to load.
14699    ///
14700    /// `#[inline(never)]` because the CREATE TABLE frame sits on the
14701    /// parse chain the nesting sentinel is tuned against.
14702    #[inline(never)]
14703    fn parse_create_table_like(&mut self, at: usize) -> Result<crate::ast::LikeSpec, ParseError> {
14704        self.advance(); // LIKE
14705        let source = self.expect_ident_like()?;
14706        let mut options = crate::ast::LikeOptions::default();
14707        loop {
14708            let including = match self.peek() {
14709                Token::Ident(s) if s.eq_ignore_ascii_case("including") => true,
14710                Token::Ident(s) if s.eq_ignore_ascii_case("excluding") => false,
14711                _ => break,
14712            };
14713            self.advance();
14714            // `ALL` lexes as its own keyword, not an identifier.
14715            let opt = if matches!(self.peek(), Token::All) {
14716                self.advance();
14717                alloc::string::String::from("all")
14718            } else {
14719                self.expect_ident_like()?
14720            };
14721            let set = |o: &mut crate::ast::LikeOptions, on: bool| {
14722                o.defaults = on;
14723                o.constraints = on;
14724                o.identity = on;
14725                o.generated = on;
14726                o.indexes = on;
14727                o.comments = on;
14728            };
14729            match opt.to_ascii_lowercase().as_str() {
14730                "all" => set(&mut options, including),
14731                "defaults" => options.defaults = including,
14732                "constraints" => options.constraints = including,
14733                "identity" => options.identity = including,
14734                "generated" => options.generated = including,
14735                "indexes" => options.indexes = including,
14736                "comments" => options.comments = including,
14737                // No storage model to copy into.
14738                "storage" | "statistics" | "compression" => {}
14739                other => {
14740                    return Err(self.err(alloc::format!("unrecognized LIKE option {other:?}")));
14741                }
14742            }
14743        }
14744        Ok(crate::ast::LikeSpec {
14745            source,
14746            at,
14747            options,
14748        })
14749    }
14750
14751    fn parse_create_table_stmt_after_create(&mut self) -> Result<Statement, ParseError> {
14752        // Caller already consumed CREATE; we're sitting on TABLE.
14753        debug_assert!(matches!(self.peek(), Token::Table));
14754        self.advance();
14755        let if_not_exists = self.consume_if_not_exists();
14756        let name = self.expect_ident_like()?;
14757        // v7.37.6-B — `CREATE TABLE c PARTITION OF parent <bounds>`
14758        // child shape has no column list; the child inherits its
14759        // columns from the parent at engine-DDL time. Detect it
14760        // before the `(` requirement below.
14761        if matches!(self.peek(), Token::Partition)
14762            && Self::tokens_match_ident_ci(self.tokens.get(self.pos + 1), "of")
14763        {
14764            self.advance(); // PARTITION
14765            self.advance(); // of
14766            let partition_of = self.parse_partition_of_tail()?;
14767            return Ok(Statement::CreateTable(CreateTableStatement {
14768                temporary: false,
14769                name,
14770                engine: None,
14771                columns: Vec::new(),
14772                like_specs: Vec::new(),
14773                inherits: Vec::new(),
14774                if_not_exists,
14775                foreign_keys: Vec::new(),
14776                table_constraints: Vec::new(),
14777                partition_by: None,
14778                partition_of: Some(partition_of),
14779            }));
14780        }
14781        // v7.38 (read01 P6.49) — CTAS: `CREATE TABLE name AS <select>`. Reuses
14782        // the materialized-view materialisation path (run the SELECT, infer the
14783        // column types, create + populate the table) but marks the node so the
14784        // executor creates a plain table without a mat-view registry entry.
14785        if matches!(self.peek(), Token::As) {
14786            self.advance();
14787            let body_stmt = self.parse_select_stmt()?;
14788            let Statement::Select(body) = body_stmt else {
14789                return Err(self.err(format!(
14790                    "CREATE TABLE {name:?} AS body must be a SELECT, got {body_stmt:?}"
14791                )));
14792            };
14793            let with_data = self.parse_optional_with_data(true)?;
14794            return Ok(Statement::CreateMaterializedView(
14795                crate::ast::CreateMaterializedViewStatement {
14796                    temporary: false,
14797                    name,
14798                    if_not_exists,
14799                    columns: Vec::new(),
14800                    body,
14801                    with_data,
14802                    as_plain_table: true,
14803                },
14804            ));
14805        }
14806        if !matches!(self.peek(), Token::LParen) {
14807            return Err(self.err(format!(
14808                "expected '(' after table name, got {:?}",
14809                self.peek()
14810            )));
14811        }
14812        self.advance();
14813        let mut columns = Vec::new();
14814        let mut foreign_keys: Vec<ForeignKeyConstraint> = Vec::new();
14815        let mut table_constraints: Vec<crate::ast::TableConstraint> = Vec::new();
14816        let mut like_specs: Vec<crate::ast::LikeSpec> = Vec::new();
14817        loop {
14818            // v7.39 (round 621) — `CREATE TABLE c () INHERITS (p)`, the empty
14819            // column list. It is how a child that adds nothing of its own is
14820            // written, and this loop demanded at least one entry: `syntax
14821            // error at or near ")"`. The child takes the parent's columns,
14822            // which the INHERITS clause already arranges.
14823            if columns.is_empty() && matches!(self.peek(), Token::RParen) {
14824                self.advance();
14825                break;
14826            }
14827            // v7.6.0 / v7.9.18 — distinguish table-level constraint
14828            // clauses from column definitions. Constraints start
14829            // with `CONSTRAINT <name> …`, `FOREIGN KEY (…)`,
14830            // `PRIMARY KEY (…)`, or `UNIQUE (…)`. Anything else is
14831            // a column.
14832            if self.peek_table_level_pk_start() {
14833                table_constraints.push(self.parse_table_level_primary_key()?);
14834            } else if matches!(self.peek(), Token::Like) {
14835                // v7.39 (round 531) — `LIKE <table> [ {INCLUDING|EXCLUDING}
14836                // <opt> ]*`. The source table's shape lives in the catalog,
14837                // so this records the clause and the engine expands it.
14838                like_specs.push(self.parse_create_table_like(columns.len())?);
14839            } else if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("exclude")) {
14840                // v7.39 (round 210) — bare `EXCLUDE [USING m] (col WITH op, …)`.
14841                table_constraints.push(self.parse_table_level_exclude()?);
14842            } else if self.peek_table_level_unique_start() {
14843                table_constraints.push(self.parse_table_level_unique()?);
14844            } else if self.peek_table_level_check_start() {
14845                // v7.13.0 — table-level CHECK (mailrs round-5 G3).
14846                table_constraints.push(self.parse_table_level_check()?);
14847            } else if self.peek_mysql_inline_key_start() {
14848                // v7.14.0 — mysqldump emits inline `KEY name (cols)`,
14849                // `INDEX name (cols)`, `UNIQUE KEY name (cols)`,
14850                // `FULLTEXT KEY name (cols)`, `SPATIAL KEY name (cols)`
14851                // inside the column list. Skip name + paren list;
14852                // for UNIQUE KEY, register as a UC.
14853                if let Some(uc) = self.parse_mysql_inline_key()? {
14854                    table_constraints.push(uc);
14855                }
14856            } else if let Some(kind) = self.peek_named_table_constraint_kind() {
14857                // v7.22 (mailrs round-13 gap 5) — `CONSTRAINT <name>
14858                // { CHECK | UNIQUE | PRIMARY KEY }`: every pg_dump'd
14859                // CHECK is named, and the named-CONSTRAINT arm used
14860                // to accept FOREIGN KEY only. The name is accepted
14861                // and discarded — same handling as every other SPG
14862                // constraint name.
14863                self.advance(); // CONSTRAINT
14864                // v7.39 (read01 round 48) — the name is kept now: the schema
14865                // stores it, so DROP / RENAME CONSTRAINT can find it.
14866                let con_name = self.expect_ident_like()?;
14867                let mut tc = match kind {
14868                    NamedTableConstraintKind::Check => self.parse_table_level_check()?,
14869                    NamedTableConstraintKind::Unique => self.parse_table_level_unique()?,
14870                    NamedTableConstraintKind::PrimaryKey => self.parse_table_level_primary_key()?,
14871                    NamedTableConstraintKind::Exclude => self.parse_table_level_exclude()?,
14872                };
14873                match &mut tc {
14874                    crate::ast::TableConstraint::Check { name, .. }
14875                    | crate::ast::TableConstraint::Unique { name, .. }
14876                    | crate::ast::TableConstraint::PrimaryKey { name, .. }
14877                    | crate::ast::TableConstraint::Exclude { name, .. } => {
14878                        *name = Some(con_name);
14879                    }
14880                    _ => {}
14881                }
14882                table_constraints.push(tc);
14883            } else if self.peek_constraint_or_fk_start() {
14884                foreign_keys.push(self.parse_table_level_fk()?);
14885            } else {
14886                let (col, col_level_fk) = self.parse_column_def_with_fk()?;
14887                // v7.13.0 — fold inline UNIQUE / CHECK column
14888                // constraints into table-level entries so the
14889                // engine path stays uniform.
14890                if col.is_unique {
14891                    table_constraints.push(crate::ast::TableConstraint::Unique {
14892                        name: None,
14893                        columns: alloc::vec![col.name.clone()],
14894                        nulls_not_distinct: col.unique_nulls_not_distinct,
14895                        deferrable: col.constraint_deferrable,
14896                        initially_deferred: col.constraint_initially_deferred,
14897                    });
14898                }
14899                if let Some(check_expr) = col.check.clone() {
14900                    table_constraints.push(crate::ast::TableConstraint::Check {
14901                        name: None,
14902                        expr: check_expr,
14903                        not_valid: false,
14904                    });
14905                }
14906                columns.push(col);
14907                if let Some(fk) = col_level_fk {
14908                    foreign_keys.push(fk);
14909                }
14910            }
14911            match self.peek() {
14912                Token::Comma => {
14913                    self.advance();
14914                }
14915                Token::RParen => {
14916                    self.advance();
14917                    break;
14918                }
14919                other => {
14920                    return Err(
14921                        self.err(format!("expected ',' or ')' in column list, got {other:?}"))
14922                    );
14923                }
14924            }
14925        }
14926        // v7.39 (round 531) — a `LIKE` clause brings its own columns, so
14927        // `CREATE TABLE k (LIKE t)` is a complete definition even though
14928        // nothing is written between the parentheses.
14929        // v7.39 (round 621) — a table with NO columns is legal: PG creates it
14930        // and `INSERT … DEFAULT VALUES` puts a row in it. This refused, so the
14931        // empty parentheses were a parse error in their own right — quite apart
14932        // from `CREATE TABLE c () INHERITS (p)`, which needs table inheritance
14933        // SPG does not have (filed separately).
14934        let _ = &like_specs;
14935        // v7.39 (round 645) — `INHERITS (p1, p2)`, PG table inheritance.
14936        // It sits between the column list and the MySQL table options,
14937        // and it was a syntax error until this round.
14938        let mut inherits: Vec<String> = Vec::new();
14939        if matches!(self.peek(), Token::Ident(k) | Token::QuotedIdent(k)
14940            if k.eq_ignore_ascii_case("inherits"))
14941        {
14942            self.advance();
14943            if !matches!(self.peek(), Token::LParen) {
14944                return Err(self.err(alloc::format!(
14945                    "expected ( after INHERITS, got {:?}",
14946                    self.peek()
14947                )));
14948            }
14949            self.advance();
14950            loop {
14951                inherits.push(self.expect_ident_like()?);
14952                if matches!(self.peek(), Token::Comma) {
14953                    self.advance();
14954                    continue;
14955                }
14956                break;
14957            }
14958            if !matches!(self.peek(), Token::RParen) {
14959                return Err(self.err(alloc::format!(
14960                    "expected ) closing INHERITS, got {:?}",
14961                    self.peek()
14962                )));
14963            }
14964            self.advance();
14965        }
14966        // v7.14.0 — consume MySQL/MariaDB table options after the
14967        // closing `)`. mysqldump emits things like
14968        // `ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
14969        // AUTO_INCREMENT=42 ROW_FORMAT=DYNAMIC COMMENT='blog posts'`.
14970        // SPG accepts all forms as no-ops (each option is
14971        // `<ident> [=] <ident-or-string>` separated by whitespace).
14972        let engine = self.consume_mysql_table_options();
14973        // v7.38 (read01 P6.55) — PG storage parameters `WITH (opt=val, …)`.
14974        // SPG has no per-table reloptions, so accept and ignore them so a
14975        // pg_dump `CREATE TABLE … WITH (fillfactor=70, …)` restores cleanly.
14976        self.consume_with_reloptions();
14977        // v7.37.6-B — declarative-partition-parent suffix
14978        // (`PARTITION BY RANGE (key_col)`) sits after the column
14979        // list + MySQL table-options. v7.37.6-B only accepts RANGE
14980        // and locks the key column at one ident; the engine then
14981        // verifies the column type is TIMESTAMPTZ.
14982        let partition_by = if matches!(self.peek(), Token::Partition) {
14983            self.advance(); // PARTITION
14984            if !self.peek_is_by() {
14985                return Err(self.err(format!(
14986                    "expected BY after PARTITION, got {:?}",
14987                    self.peek()
14988                )));
14989            }
14990            self.advance();
14991            Some(self.parse_partition_by_tail()?)
14992        } else {
14993            None
14994        };
14995        Ok(Statement::CreateTable(CreateTableStatement {
14996            temporary: false,
14997            name,
14998            engine,
14999            columns,
15000            like_specs,
15001            inherits,
15002            if_not_exists,
15003            foreign_keys,
15004            table_constraints,
15005            partition_by,
15006            partition_of: None,
15007        }))
15008    }
15009
15010    /// v7.37.6-B — case-insensitive ident match helper for the
15011    /// `PARTITION OF` / `MINVALUE` / `MAXVALUE` keywords. They lex
15012    /// as `Token::Ident("of"/"minvalue"/"maxvalue")` because we
15013    /// didn't burn a global keyword slot for each (see the
15014    /// `Token::Partition` doc-comment in `lexer.rs`).
15015    fn tokens_match_ident_ci(t: Option<&Token>, want: &str) -> bool {
15016        matches!(t, Some(Token::Ident(s) | Token::QuotedIdent(s)) if s.eq_ignore_ascii_case(want))
15017    }
15018
15019    /// v7.37.6-B — after `PARTITION BY`, expect `RANGE (key_col [, ...])`.
15020    /// v7.37.16 (16.1/16.2) — extended to LIST + HASH.
15021    fn parse_partition_by_tail(&mut self) -> Result<crate::ast::PartitionBySpec, ParseError> {
15022        use crate::ast::{PartitionBySpec, PartitionKindAst};
15023        let kind = match self.peek() {
15024            Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("range") => {
15025                self.advance();
15026                PartitionKindAst::Range
15027            }
15028            Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("list") => {
15029                self.advance();
15030                PartitionKindAst::List
15031            }
15032            Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("hash") => {
15033                self.advance();
15034                PartitionKindAst::Hash
15035            }
15036            other => {
15037                return Err(self.err(format!(
15038                    "PARTITION BY: expected RANGE / LIST / HASH, got {other:?}"
15039                )));
15040            }
15041        };
15042        if !matches!(self.peek(), Token::LParen) {
15043            return Err(self.err(format!(
15044                "expected '(' after PARTITION BY <strategy>, got {:?}",
15045                self.peek()
15046            )));
15047        }
15048        self.advance();
15049        let mut key_columns = Vec::new();
15050        loop {
15051            key_columns.push(self.expect_ident_like()?);
15052            match self.peek() {
15053                Token::Comma => {
15054                    self.advance();
15055                }
15056                Token::RParen => {
15057                    self.advance();
15058                    break;
15059                }
15060                other => {
15061                    return Err(self.err(format!(
15062                        "expected ',' or ')' in PARTITION BY key list, got {other:?}"
15063                    )));
15064                }
15065            }
15066        }
15067        if key_columns.is_empty() {
15068            return Err(self.err("PARTITION BY requires at least one key column".to_string()));
15069        }
15070        Ok(PartitionBySpec { kind, key_columns })
15071    }
15072
15073    /// v7.37.6-B — after `PARTITION OF`, expect
15074    ///   <parent> FOR VALUES FROM ( <expr> ) TO ( <expr> )
15075    /// or
15076    ///   <parent> DEFAULT
15077    fn parse_partition_of_tail(&mut self) -> Result<crate::ast::PartitionOfSpec, ParseError> {
15078        use crate::ast::{PartitionOfBoundsAst, PartitionOfSpec};
15079        let parent_name = self.expect_ident_like()?;
15080        // v7.37.6-B rejects an explicit column list — the child
15081        // inherits from the parent. mailrs round-7 taught us that
15082        // CREATE TABLE-side schema reconciliation hides drift, so
15083        // we surface this as a parse error rather than silently
15084        // ignoring user columns.
15085        if matches!(self.peek(), Token::LParen) {
15086            return Err(self.err(
15087                "CREATE TABLE … PARTITION OF parent: explicit column list not supported \
15088                 at v7.37.6-B; the child inherits its columns from the parent"
15089                    .to_string(),
15090            ));
15091        }
15092        let bounds = match self.peek() {
15093            Token::Default => {
15094                self.advance();
15095                PartitionOfBoundsAst::Default
15096            }
15097            Token::For => {
15098                self.advance();
15099                if !matches!(self.peek(), Token::Values) {
15100                    return Err(
15101                        self.err(format!("expected VALUES after FOR, got {:?}", self.peek()))
15102                    );
15103                }
15104                self.advance();
15105                // WITH is not a reserved Token in the lexer — it lexes
15106                // as Token::Ident("with"). Disambiguate manually.
15107                let want_with = matches!(
15108                    self.peek(),
15109                    Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("with")
15110                );
15111                if want_with {
15112                    self.advance();
15113                    if !matches!(self.peek(), Token::LParen) {
15114                        return Err(self.err(format!(
15115                            "expected '(' after FOR VALUES WITH, got {:?}",
15116                            self.peek()
15117                        )));
15118                    }
15119                    self.advance();
15120                    let (mut modulus, mut remainder): (Option<u32>, Option<u32>) = (None, None);
15121                    loop {
15122                        let key = self.expect_ident_like()?;
15123                        let n = match self.peek().clone() {
15124                            Token::Integer(v) if u32::try_from(v).is_ok() => {
15125                                self.advance();
15126                                v as u32
15127                            }
15128                            other => {
15129                                return Err(self.err(format!(
15130                                    "FOR VALUES WITH: expected unsigned integer literal, got {other:?}"
15131                                )));
15132                            }
15133                        };
15134                        match key.to_ascii_uppercase().as_str() {
15135                            "MODULUS" => modulus = Some(n),
15136                            "REMAINDER" => remainder = Some(n),
15137                            other => {
15138                                return Err(self.err(format!(
15139                                    "FOR VALUES WITH: unknown key {other:?}; \
15140                                     expected MODULUS or REMAINDER"
15141                                )));
15142                            }
15143                        }
15144                        match self.peek() {
15145                            Token::Comma => {
15146                                self.advance();
15147                            }
15148                            Token::RParen => {
15149                                self.advance();
15150                                break;
15151                            }
15152                            other => {
15153                                return Err(self.err(format!(
15154                                    "expected ',' or ')' in FOR VALUES WITH list, got {other:?}"
15155                                )));
15156                            }
15157                        }
15158                    }
15159                    let modulus = modulus
15160                        .ok_or_else(|| self.err("FOR VALUES WITH: missing MODULUS".to_string()))?;
15161                    let remainder = remainder.ok_or_else(|| {
15162                        self.err("FOR VALUES WITH: missing REMAINDER".to_string())
15163                    })?;
15164                    if modulus == 0 {
15165                        return Err(self.err("FOR VALUES WITH: MODULUS must be > 0".to_string()));
15166                    }
15167                    if remainder >= modulus {
15168                        return Err(self.err(format!(
15169                            "FOR VALUES WITH: REMAINDER ({remainder}) \
15170                             must be < MODULUS ({modulus})"
15171                        )));
15172                    }
15173                    PartitionOfBoundsAst::Hash { modulus, remainder }
15174                } else {
15175                    match self.peek() {
15176                        Token::From => {
15177                            self.advance();
15178                            let lower = Box::new(self.parse_partition_bound_expr()?);
15179                            if !matches!(self.peek(), Token::To) {
15180                                return Err(self.err(format!(
15181                                    "expected TO after FROM (...), got {:?}",
15182                                    self.peek()
15183                                )));
15184                            }
15185                            self.advance();
15186                            let upper = Box::new(self.parse_partition_bound_expr()?);
15187                            PartitionOfBoundsAst::Range { lower, upper }
15188                        }
15189                        // v7.37.16 (16.1) — FOR VALUES IN (lit [, lit, …])
15190                        Token::In => {
15191                            self.advance();
15192                            if !matches!(self.peek(), Token::LParen) {
15193                                return Err(self.err(format!(
15194                                    "expected '(' after FOR VALUES IN, got {:?}",
15195                                    self.peek()
15196                                )));
15197                            }
15198                            self.advance();
15199                            let mut values = Vec::new();
15200                            loop {
15201                                values.push(self.parse_expr(0)?);
15202                                match self.peek() {
15203                                    Token::Comma => {
15204                                        self.advance();
15205                                    }
15206                                    Token::RParen => {
15207                                        self.advance();
15208                                        break;
15209                                    }
15210                                    other => {
15211                                        return Err(self.err(format!(
15212                                        "expected ',' or ')' in FOR VALUES IN list, got {other:?}"
15213                                    )));
15214                                    }
15215                                }
15216                            }
15217                            if values.is_empty() {
15218                                return Err(self.err(
15219                                    "FOR VALUES IN requires at least one literal".to_string(),
15220                                ));
15221                            }
15222                            PartitionOfBoundsAst::List { values }
15223                        }
15224                        other => {
15225                            return Err(self.err(format!(
15226                                "expected FROM / IN / WITH after FOR VALUES, got {other:?}"
15227                            )));
15228                        }
15229                    }
15230                }
15231            }
15232            other => {
15233                return Err(self.err(format!(
15234                    "expected FOR VALUES or DEFAULT after PARTITION OF parent, got {other:?}"
15235                )));
15236            }
15237        };
15238        Ok(PartitionOfSpec {
15239            parent_name,
15240            bounds,
15241        })
15242    }
15243
15244    /// v7.37.6-B — a single `( <expr> )` bound. `MINVALUE` /
15245    /// `MAXVALUE` lex as Ident; rewrite them into FunctionCall
15246    /// markers (no-arg builtins) so the engine resolves them
15247    /// against [`spg_storage::PartitionBound::{MinValue, MaxValue}`].
15248    fn parse_partition_bound_expr(&mut self) -> Result<crate::ast::Expr, ParseError> {
15249        if !matches!(self.peek(), Token::LParen) {
15250            return Err(self.err(format!(
15251                "expected '(' before partition bound, got {:?}",
15252                self.peek()
15253            )));
15254        }
15255        self.advance();
15256        let expr = match self.peek() {
15257            Token::Ident(s) | Token::QuotedIdent(s)
15258                if s.eq_ignore_ascii_case("minvalue") || s.eq_ignore_ascii_case("maxvalue") =>
15259            {
15260                let name = s.to_ascii_uppercase();
15261                self.advance();
15262                crate::ast::Expr::FunctionCall {
15263                    name,
15264                    args: Vec::new(),
15265                }
15266            }
15267            _ => self.parse_expr(0)?,
15268        };
15269        if !matches!(self.peek(), Token::RParen) {
15270            return Err(self.err(format!(
15271                "expected ')' after partition bound, got {:?}",
15272                self.peek()
15273            )));
15274        }
15275        self.advance();
15276        Ok(expr)
15277    }
15278
15279    /// v7.14.0 — true when the next tokens look like an inline
15280    /// MySQL index declaration: KEY / INDEX / UNIQUE KEY /
15281    /// UNIQUE INDEX / FULLTEXT [KEY|INDEX] / SPATIAL [KEY|INDEX]
15282    /// — each followed by an optional name + `(...)`. Critical:
15283    /// a column NAMED `key` / `index` (PG accepts as ident) must
15284    /// NOT be mistaken for the KEY constraint shape. We disambig
15285    /// by requiring the keyword to be followed by either `(` or
15286    /// `<ident> (`.
15287    fn peek_mysql_inline_key_start(&self) -> bool {
15288        let cur = self.peek();
15289        // Shapes:
15290        //   KEY (cols)
15291        //   KEY name (cols)
15292        //   INDEX (cols)
15293        //   INDEX name (cols)
15294        //   UNIQUE KEY [name] (cols)
15295        //   UNIQUE INDEX [name] (cols)
15296        //   FULLTEXT [KEY|INDEX] [name] (cols)
15297        //   SPATIAL [KEY|INDEX] [name] (cols)
15298        let after_keyword_followed_by_paren_or_ident_paren = |skip: usize| -> bool {
15299            // tokens at skip = the position AFTER the index-form
15300            // keywords (KEY/INDEX) have been consumed.
15301            match self.tokens.get(skip) {
15302                Some(Token::LParen) => true,
15303                Some(Token::Ident(_) | Token::QuotedIdent(_)) => {
15304                    matches!(self.tokens.get(skip + 1), Some(Token::LParen))
15305                }
15306                _ => false,
15307            }
15308        };
15309        // `INDEX` lexes as Token::Index (reserved), not as
15310        // Token::Ident("index"). Both shapes count as a KEY/INDEX
15311        // start; the peek helper below handles either.
15312        let is_key_or_index_tok = |t: &Token| -> bool {
15313            matches!(t, Token::Index)
15314                || matches!(t, Token::Ident(s) if s.eq_ignore_ascii_case("key") || s.eq_ignore_ascii_case("index"))
15315        };
15316        match cur {
15317            Token::Index => after_keyword_followed_by_paren_or_ident_paren(self.pos + 1),
15318            Token::Ident(s) if s.eq_ignore_ascii_case("key") || s.eq_ignore_ascii_case("index") => {
15319                after_keyword_followed_by_paren_or_ident_paren(self.pos + 1)
15320            }
15321            Token::Ident(s)
15322                if s.eq_ignore_ascii_case("fulltext") || s.eq_ignore_ascii_case("spatial") =>
15323            {
15324                let nxt = self.tokens.get(self.pos + 1);
15325                let after_after = if nxt.is_some_and(is_key_or_index_tok) {
15326                    self.pos + 2
15327                } else {
15328                    self.pos + 1
15329                };
15330                after_keyword_followed_by_paren_or_ident_paren(after_after)
15331            }
15332            Token::Ident(s) if s.eq_ignore_ascii_case("unique") => {
15333                let nxt = self.tokens.get(self.pos + 1);
15334                if !nxt.is_some_and(is_key_or_index_tok) {
15335                    return false;
15336                }
15337                after_keyword_followed_by_paren_or_ident_paren(self.pos + 2)
15338            }
15339            _ => false,
15340        }
15341    }
15342
15343    /// v7.14.0 — parse the MySQL inline KEY/INDEX form. Returns
15344    /// Some(TableConstraint::Unique) for UNIQUE KEY (so SPG
15345    /// enforces uniqueness on INSERT). v7.15.0: plain KEY/INDEX
15346    /// returns Some(TableConstraint::Index) so the engine builds
15347    /// a real BTree index on the leading column (mysqldump
15348    /// `KEY idx_posts_author (author_id)` shape).
15349    /// FULLTEXT / SPATIAL still return None — accepted-as-no-op
15350    /// (the storage layer has no matching AM).
15351    fn parse_mysql_inline_key(
15352        &mut self,
15353    ) -> Result<Option<crate::ast::TableConstraint>, ParseError> {
15354        // Detect UNIQUE prefix.
15355        let is_unique = if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("unique"))
15356        {
15357            self.advance();
15358            true
15359        } else {
15360            false
15361        };
15362        // Consume FULLTEXT / SPATIAL prefix and record which one
15363        // it was. v7.17.0 Phase 2.2 — FULLTEXT routes through a
15364        // dedicated TableConstraint variant so the engine can
15365        // build a tsvector-GIN; SPATIAL still has no matching
15366        // AM, so it falls back to accept-as-no-op.
15367        let mut is_fulltext = false;
15368        let mut is_spatial = false;
15369        if let Token::Ident(s) = self.peek().clone() {
15370            if s.eq_ignore_ascii_case("fulltext") {
15371                self.advance();
15372                is_fulltext = true;
15373            } else if s.eq_ignore_ascii_case("spatial") {
15374                self.advance();
15375                is_spatial = true;
15376            }
15377        }
15378        // KEY / INDEX keyword. `INDEX` lexes as Token::Index
15379        // (reserved); accept either token shape.
15380        match self.peek() {
15381            Token::Index => {
15382                self.advance();
15383            }
15384            Token::Ident(s) if s.eq_ignore_ascii_case("key") || s.eq_ignore_ascii_case("index") => {
15385                self.advance();
15386            }
15387            other => {
15388                return Err(self.err(alloc::format!(
15389                    "expected KEY/INDEX in inline index declaration, got {other:?}"
15390                )));
15391            }
15392        }
15393        // Optional index name (an ident before the `(`).
15394        // v7.15.0 — capture the name when present so the engine
15395        // builds the secondary index under the user's chosen
15396        // name (matches mysqldump's `KEY idx_x (col)` shape).
15397        let mut idx_name: Option<String> = None;
15398        if matches!(self.peek(), Token::Ident(_) | Token::QuotedIdent(_))
15399            && matches!(self.tokens.get(self.pos + 1), Some(Token::LParen))
15400        {
15401            if let Token::Ident(s) | Token::QuotedIdent(s) = self.advance() {
15402                idx_name = Some(s);
15403            }
15404        }
15405        // Optional `USING BTREE` / `USING HASH` (MySQL).
15406        if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("using")) {
15407            self.advance();
15408            if matches!(self.peek(), Token::Ident(_) | Token::QuotedIdent(_)) {
15409                self.advance();
15410            }
15411        }
15412        // Required column list `(col [, col]*)`.
15413        if !matches!(self.peek(), Token::LParen) {
15414            return Err(self.err(alloc::format!(
15415                "expected '(' in inline KEY/INDEX, got {:?}",
15416                self.peek()
15417            )));
15418        }
15419        self.advance();
15420        let mut cols: Vec<String> = Vec::new();
15421        while let Token::Ident(s) | Token::QuotedIdent(s) = self.peek().clone() {
15422            self.advance();
15423            cols.push(s);
15424            // Skip optional `(length)` per-column prefix.
15425            if matches!(self.peek(), Token::LParen) {
15426                let mut depth = 1usize;
15427                self.advance();
15428                while depth > 0 {
15429                    match self.peek() {
15430                        Token::LParen => depth += 1,
15431                        Token::RParen => depth -= 1,
15432                        Token::Eof => break,
15433                        _ => {}
15434                    }
15435                    self.advance();
15436                }
15437            }
15438            // Skip optional ASC / DESC.
15439            if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("asc") || s.eq_ignore_ascii_case("desc"))
15440                || matches!(self.peek(), Token::Asc | Token::Desc)
15441            {
15442                self.advance();
15443            }
15444            if matches!(self.peek(), Token::Comma) {
15445                self.advance();
15446                continue;
15447            }
15448            break;
15449        }
15450        if matches!(self.peek(), Token::RParen) {
15451            self.advance();
15452        }
15453        // Trailing options on the inline index — comment / etc.
15454        // Skip until comma or `)`.
15455        while !matches!(self.peek(), Token::Comma | Token::RParen | Token::Eof) {
15456            self.advance();
15457        }
15458        if cols.is_empty() {
15459            return Ok(None);
15460        }
15461        if is_unique {
15462            // Carry the captured idx_name on UNIQUE too so future
15463            // engine work can name the underlying BTree
15464            // accordingly; today the unique-constraint installer
15465            // synthesises the name itself, but Display round-trip
15466            // benefits from preserving it.
15467            Ok(Some(crate::ast::TableConstraint::Unique {
15468                name: idx_name,
15469                columns: cols,
15470                nulls_not_distinct: false,
15471                // MySQL inline UNIQUE KEY has no deferral vocabulary.
15472                deferrable: false,
15473                initially_deferred: false,
15474            }))
15475        } else if is_fulltext {
15476            // v7.17.0 Phase 2.2 — MySQL `FULLTEXT KEY` now
15477            // routes through `TableConstraint::FulltextIndex`;
15478            // the engine builds a tsvector-GIN over each named
15479            // column so MATCH AGAINST gets a real inverted
15480            // index instead of a silently-dropped declaration.
15481            Ok(Some(crate::ast::TableConstraint::FulltextIndex {
15482                name: idx_name,
15483                columns: cols,
15484            }))
15485        } else if is_spatial {
15486            // SPG has no native SPATIAL AM. Accept-as-no-op
15487            // (declaration is parsed, but no index is built).
15488            Ok(None)
15489        } else {
15490            // v7.15.0 — plain KEY / INDEX builds a real BTree
15491            // secondary index.
15492            Ok(Some(crate::ast::TableConstraint::Index {
15493                name: idx_name,
15494                columns: cols,
15495            }))
15496        }
15497    }
15498
15499    /// v7.14.0 — consume MySQL/MariaDB table-options tail after
15500    /// the closing `)`: ENGINE=..., DEFAULT CHARSET=...,
15501    /// COLLATE=..., AUTO_INCREMENT=N, ROW_FORMAT=..., COMMENT='...'
15502    /// (in any order, separated by whitespace).
15503    /// v7.38 (read01 P6.55) — consume and discard a PG `WITH (opt=val, …)`
15504    /// storage-parameter clause on CREATE TABLE. SPG has no per-table
15505    /// reloptions; accepting them keeps pg_dump restores working. `WITH` is a
15506    /// bare ident here, and only the parenthesised form is reloptions (so this
15507    /// never eats a `WITH DATA` / `WITH CHECK OPTION` trailer).
15508    fn consume_with_reloptions(&mut self) {
15509        let is_with = matches!(
15510            self.peek(),
15511            Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("with")
15512        );
15513        if !is_with || !matches!(self.tokens.get(self.pos + 1), Some(Token::LParen)) {
15514            return;
15515        }
15516        self.advance(); // WITH
15517        self.advance(); // (
15518        let mut depth = 1u32;
15519        while depth > 0 && !matches!(self.peek(), Token::Eof) {
15520            match self.peek() {
15521                Token::LParen => depth += 1,
15522                Token::RParen => depth -= 1,
15523                _ => {}
15524            }
15525            self.advance();
15526        }
15527    }
15528
15529    /// v7.39 — returns the `ENGINE=` name, which used to be consumed and
15530    /// dropped with everything else here. The rest of the MySQL table
15531    /// options genuinely have no meaning for SPG's storage; the engine
15532    /// name does, because MySQL REFUSES one it does not know and a dump
15533    /// with a typo in it should not quietly become a table.
15534    fn consume_mysql_table_options(&mut self) -> Option<alloc::string::String> {
15535        let mut engine: Option<alloc::string::String> = None;
15536        loop {
15537            // Heuristic: a table option is an ident (or `DEFAULT`
15538            // reserved keyword) followed by `=` and an
15539            // ident / string / integer.
15540            let name_lc = match self.peek().clone() {
15541                Token::Ident(s) | Token::QuotedIdent(s) => s.to_ascii_lowercase(),
15542                Token::Default => alloc::string::String::from("default"),
15543                _ => break,
15544            };
15545            let known = matches!(
15546                name_lc.as_str(),
15547                "engine"
15548                    | "default"
15549                    | "charset"
15550                    | "collate"
15551                    | "auto_increment"
15552                    | "row_format"
15553                    | "comment"
15554                    | "pack_keys"
15555                    | "stats_persistent"
15556                    | "stats_auto_recalc"
15557                    | "stats_sample_pages"
15558                    | "key_block_size"
15559                    | "tablespace"
15560                    | "min_rows"
15561                    | "max_rows"
15562                    | "checksum"
15563                    | "delay_key_write"
15564                    | "insert_method"
15565                    | "data"
15566                    | "index"
15567                    | "encryption"
15568                    | "compression"
15569            );
15570            if !known {
15571                break;
15572            }
15573            self.advance(); // option name
15574            // `DEFAULT` optional prefix is followed by `CHARSET` /
15575            // `COLLATE`; consume the next ident too.
15576            if name_lc == "default" {
15577                if matches!(self.peek(), Token::Ident(_) | Token::QuotedIdent(_)) {
15578                    self.advance();
15579                }
15580            }
15581            if matches!(self.peek(), Token::Eq) {
15582                self.advance();
15583            }
15584            match self.peek().clone() {
15585                Token::Ident(v) | Token::QuotedIdent(v) | Token::String(v) => {
15586                    if name_lc == "engine" {
15587                        // v7.39.3 — as WRITTEN. MySQL 9.7.2 refuses an
15588                        // engine it does not know and names it back
15589                        // exactly: `Unknown storage engine 'NoSuchEng'`,
15590                        // measured. The lexer folds a bare identifier, so
15591                        // the message quoted a name the dump did not
15592                        // contain, which is the one thing that message is
15593                        // for. Guarded the same way the column spelling
15594                        // is: the span runs to the next token, so what
15595                        // comes back has to be the same word.
15596                        let written = self
15597                            .source_span(self.pos, self.pos)
15598                            .map(|raw| raw.trim().trim_matches('`').trim_matches('\''))
15599                            .filter(|raw| raw.eq_ignore_ascii_case(&v))
15600                            .map(alloc::string::String::from);
15601                        engine = Some(written.unwrap_or(v));
15602                    }
15603                    self.advance();
15604                }
15605                Token::Integer(_) => {
15606                    self.advance();
15607                }
15608                _ => {}
15609            }
15610        }
15611        engine
15612    }
15613
15614    /// v7.9.18 — true when the next tokens are `PRIMARY KEY (…)`.
15615    /// PRIMARY and KEY are bare idents; we look-ahead 2 to be
15616    /// sure (otherwise a column literally named `primary` would
15617    /// be mistaken).
15618    fn peek_table_level_pk_start(&self) -> bool {
15619        let cur = self.peek();
15620        let nxt = self.tokens.get(self.pos + 1);
15621        let nxt2 = self.tokens.get(self.pos + 2);
15622        let is_primary = matches!(cur, Token::Ident(s) if s.eq_ignore_ascii_case("primary"));
15623        let is_key = matches!(nxt, Some(Token::Ident(s)) if s.eq_ignore_ascii_case("key"));
15624        let is_lparen = matches!(nxt2, Some(Token::LParen));
15625        is_primary && is_key && is_lparen
15626    }
15627
15628    /// v7.9.18 — true when the next tokens are `UNIQUE (…)`.
15629    /// v7.13.0 — also matches `UNIQUE NULLS [NOT] DISTINCT (…)`
15630    /// (mailrs round-5 G10).
15631    fn peek_table_level_unique_start(&self) -> bool {
15632        let cur = self.peek();
15633        let is_unique = matches!(cur, Token::Ident(s) if s.eq_ignore_ascii_case("unique"));
15634        if !is_unique {
15635            return false;
15636        }
15637        let n1 = self.tokens.get(self.pos + 1);
15638        // Plain `UNIQUE (…)`.
15639        if matches!(n1, Some(Token::LParen)) {
15640            return true;
15641        }
15642        // `UNIQUE NULLS [NOT] DISTINCT (…)`.
15643        let is_nulls = matches!(n1, Some(Token::Ident(s)) if s.eq_ignore_ascii_case("nulls"));
15644        if !is_nulls {
15645            return false;
15646        }
15647        let n2 = self.tokens.get(self.pos + 2);
15648        let n3 = self.tokens.get(self.pos + 3);
15649        let n4 = self.tokens.get(self.pos + 4);
15650        // `UNIQUE NULLS DISTINCT (…)` — 4 tokens before `(`.
15651        if matches!(n2, Some(Token::Distinct)) && matches!(n3, Some(Token::LParen)) {
15652            return true;
15653        }
15654        // `UNIQUE NULLS NOT DISTINCT (…)` — 5 tokens before `(`.
15655        if matches!(n2, Some(Token::Not))
15656            && matches!(n3, Some(Token::Distinct))
15657            && matches!(n4, Some(Token::LParen))
15658        {
15659            return true;
15660        }
15661        false
15662    }
15663
15664    fn parse_table_level_primary_key(&mut self) -> Result<crate::ast::TableConstraint, ParseError> {
15665        self.advance(); // PRIMARY
15666        self.advance(); // KEY
15667        let columns = self.parse_paren_ident_list("PRIMARY KEY")?;
15668        // v7.39 (round 711) — the trailer's values are CARRIED now; round
15669        // 621 consumed and dropped them (the storing half of F08).
15670        let (deferrable, initially_deferred) = self.consume_deferrable_clauses_timed()?;
15671        Ok(crate::ast::TableConstraint::PrimaryKey {
15672            name: None,
15673            columns,
15674            deferrable,
15675            initially_deferred,
15676        })
15677    }
15678
15679    fn parse_table_level_unique(&mut self) -> Result<crate::ast::TableConstraint, ParseError> {
15680        self.advance(); // UNIQUE
15681        // v7.13.0 — optional `NULLS NOT DISTINCT` modifier
15682        // (mailrs round-5 G10, PG 15+ surface). Default behaviour
15683        // is `NULLS DISTINCT` per the SQL standard.
15684        let mut nulls_not_distinct = false;
15685        if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("nulls")) {
15686            let n1 = self.tokens.get(self.pos + 1);
15687            let n2 = self.tokens.get(self.pos + 2);
15688            let is_not = matches!(n1, Some(Token::Not));
15689            let is_distinct = matches!(n2, Some(Token::Distinct));
15690            if is_not && is_distinct {
15691                self.advance(); // NULLS
15692                self.advance(); // NOT
15693                self.advance(); // DISTINCT
15694                nulls_not_distinct = true;
15695            } else if matches!(n1, Some(Token::Distinct)) {
15696                self.advance(); // NULLS
15697                self.advance(); // DISTINCT
15698            }
15699        }
15700        let columns = self.parse_paren_ident_list("UNIQUE")?;
15701        let (deferrable, initially_deferred) = self.consume_deferrable_clauses_timed()?;
15702        Ok(crate::ast::TableConstraint::Unique {
15703            name: None,
15704            columns,
15705            nulls_not_distinct,
15706            deferrable,
15707            initially_deferred,
15708        })
15709    }
15710
15711    /// v7.13.0 — table-level `CHECK (<expr>)` constraint
15712    /// (mailrs round-5 G3). Consumes `CHECK` then a parenthesised
15713    /// expression.
15714    /// v7.39 (round 210) — `EXCLUDE [USING <method>] ( <col> WITH <op>
15715    /// [, <col> WITH <op>]* ) [WHERE (...)]`. The operator is read as a
15716    /// standalone token spelling (`&&`, `=`, `@>`, `<@`, `&<`, `&>`).
15717    /// v7.39 (round 652) — the optional `NOT VALID` suffix on a
15718    /// constraint added by ALTER TABLE. `NOT` alone is not enough to
15719    /// commit: `NOT` starts no other suffix here, but reading both
15720    /// tokens before advancing keeps the caller's error message intact
15721    /// if someone writes `NOT NULL` by mistake.
15722    fn parse_not_valid_suffix(&mut self) -> bool {
15723        if !matches!(self.peek(), Token::Not) {
15724            return false;
15725        }
15726        if !matches!(self.tokens.get(self.pos + 1), Some(Token::Ident(s)) if s.eq_ignore_ascii_case("valid"))
15727        {
15728            return false;
15729        }
15730        self.advance();
15731        self.advance();
15732        true
15733    }
15734
15735    fn parse_table_level_exclude(&mut self) -> Result<crate::ast::TableConstraint, ParseError> {
15736        self.advance(); // EXCLUDE
15737        // Optional `USING <method>`.
15738        let mut method = None;
15739        if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("using")) {
15740            self.advance();
15741            method = Some(match self.advance() {
15742                Token::Ident(s) | Token::QuotedIdent(s) => s.to_ascii_lowercase(),
15743                other => {
15744                    return Err(self.err(alloc::format!(
15745                        "expected index method after USING, got {other:?}"
15746                    )));
15747                }
15748            });
15749        }
15750        if !matches!(self.peek(), Token::LParen) {
15751            return Err(self.err(alloc::format!(
15752                "expected '(' after EXCLUDE, got {:?}",
15753                self.peek()
15754            )));
15755        }
15756        self.advance();
15757        let mut elements: Vec<(String, String)> = Vec::new();
15758        loop {
15759            let col = match self.advance() {
15760                Token::Ident(s) | Token::QuotedIdent(s) => s,
15761                other => {
15762                    return Err(self.err(alloc::format!(
15763                        "expected column name in EXCLUDE, got {other:?}"
15764                    )));
15765                }
15766            };
15767            if !matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("with")) {
15768                return Err(self.err(alloc::format!(
15769                    "expected WITH after EXCLUDE column, got {:?}",
15770                    self.peek()
15771                )));
15772            }
15773            self.advance();
15774            let op = match self.advance() {
15775                Token::InetOverlap => String::from("&&"),
15776                Token::Intersects => String::from("?#"),
15777                Token::IsBelow => String::from("<^"),
15778                Token::IsAbove => String::from(">^"),
15779                Token::PatternLt => String::from("~<~"),
15780                Token::PatternLtEq => String::from("~<=~"),
15781                Token::PatternGt => String::from("~>~"),
15782                Token::PatternGtEq => String::from("~>=~"),
15783                Token::TsMatchOld => String::from("@@@"),
15784                Token::Eq => String::from("="),
15785                Token::JsonContains => String::from("@>"),
15786                Token::JsonContainedBy => String::from("<@"),
15787                Token::OverLeft => String::from("&<"),
15788                Token::OverRight => String::from("&>"),
15789                other => {
15790                    return Err(self.err(alloc::format!(
15791                        "unsupported EXCLUDE operator {other:?} (SPG supports &&, =, @>, <@, &<, &>)"
15792                    )));
15793                }
15794            };
15795            elements.push((col, op));
15796            if matches!(self.peek(), Token::Comma) {
15797                self.advance();
15798                continue;
15799            }
15800            break;
15801        }
15802        if !matches!(self.peek(), Token::RParen) {
15803            return Err(self.err(alloc::format!(
15804                "expected ')' to close EXCLUDE, got {:?}",
15805                self.peek()
15806            )));
15807        }
15808        self.advance();
15809        Ok(crate::ast::TableConstraint::Exclude {
15810            name: None,
15811            method,
15812            elements,
15813        })
15814    }
15815
15816    fn parse_table_level_check(&mut self) -> Result<crate::ast::TableConstraint, ParseError> {
15817        self.advance(); // CHECK
15818        if !matches!(self.peek(), Token::LParen) {
15819            return Err(self.err(alloc::format!(
15820                "expected '(' after CHECK, got {:?}",
15821                self.peek()
15822            )));
15823        }
15824        self.advance();
15825        let expr = self.parse_expr(0)?;
15826        if !matches!(self.peek(), Token::RParen) {
15827            return Err(self.err(alloc::format!(
15828                "expected ')' to close CHECK predicate, got {:?}",
15829                self.peek()
15830            )));
15831        }
15832        self.advance();
15833        // A CHECK written inside CREATE TABLE cannot be NOT VALID: there
15834        // are no existing rows for PG to skip, so it rejects the suffix.
15835        Ok(crate::ast::TableConstraint::Check {
15836            name: None,
15837            expr,
15838            not_valid: false,
15839        })
15840    }
15841
15842    /// v7.13.0 — `true` when the next token is `CHECK` (a bare ident).
15843    fn peek_table_level_check_start(&self) -> bool {
15844        matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("check"))
15845    }
15846
15847    /// v7.22 (round-13 gap 5) — `Some(kind)` when the next tokens are
15848    /// `CONSTRAINT <name> { CHECK | UNIQUE | PRIMARY }`. FOREIGN stays
15849    /// on the dedicated FK path (`parse_table_level_fk` consumes its
15850    /// own CONSTRAINT prefix).
15851    fn peek_named_table_constraint_kind(&self) -> Option<NamedTableConstraintKind> {
15852        if !matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("constraint")) {
15853            return None;
15854        }
15855        // tokens[pos+1] is the constraint name (any ident-like);
15856        // tokens[pos+2] is the kind keyword.
15857        match self.tokens.get(self.pos + 2) {
15858            Some(Token::Ident(s)) if s.eq_ignore_ascii_case("check") => {
15859                Some(NamedTableConstraintKind::Check)
15860            }
15861            Some(Token::Ident(s)) if s.eq_ignore_ascii_case("unique") => {
15862                Some(NamedTableConstraintKind::Unique)
15863            }
15864            Some(Token::Ident(s)) if s.eq_ignore_ascii_case("primary") => {
15865                Some(NamedTableConstraintKind::PrimaryKey)
15866            }
15867            Some(Token::Ident(s)) if s.eq_ignore_ascii_case("exclude") => {
15868                Some(NamedTableConstraintKind::Exclude)
15869            }
15870            _ => None,
15871        }
15872    }
15873
15874    fn parse_paren_ident_list(&mut self, ctx: &str) -> Result<Vec<String>, ParseError> {
15875        if !matches!(self.peek(), Token::LParen) {
15876            return Err(self.err(alloc::format!(
15877                "expected '(' after {ctx}, got {:?}",
15878                self.peek()
15879            )));
15880        }
15881        self.advance();
15882        let mut out = Vec::new();
15883        loop {
15884            out.push(self.expect_ident_like()?);
15885            match self.peek() {
15886                Token::Comma => {
15887                    self.advance();
15888                }
15889                Token::RParen => {
15890                    self.advance();
15891                    break;
15892                }
15893                other => {
15894                    return Err(self.err(alloc::format!(
15895                        "expected ',' or ')' in {ctx} list, got {other:?}"
15896                    )));
15897                }
15898            }
15899        }
15900        if out.is_empty() {
15901            return Err(self.err(alloc::format!("{ctx} requires at least one column")));
15902        }
15903        Ok(out)
15904    }
15905
15906    /// v7.6.0 — true when the next tokens are `CONSTRAINT <name>
15907    /// FOREIGN KEY` or bare `FOREIGN KEY`. Both introduce a
15908    /// table-level FK; a column def never starts with either keyword
15909    /// (column names are not in this reserved set).
15910    fn peek_constraint_or_fk_start(&self) -> bool {
15911        let is_constraint_kw = matches!(
15912            self.peek(),
15913            Token::Ident(s) if s.eq_ignore_ascii_case("constraint")
15914        );
15915        let is_foreign_kw = matches!(
15916            self.peek(),
15917            Token::Ident(s) if s.eq_ignore_ascii_case("foreign")
15918        );
15919        is_constraint_kw || is_foreign_kw
15920    }
15921
15922    /// v7.6.0 — parse a table-level FK clause:
15923    /// `[CONSTRAINT <name>] FOREIGN KEY (<col>[,<col>]*) REFERENCES
15924    /// <tbl> [(<pcol>[,<pcol>]*)] [ON DELETE <action>] [ON UPDATE <action>]`.
15925    fn parse_table_level_fk(&mut self) -> Result<ForeignKeyConstraint, ParseError> {
15926        let mut name: Option<String> = None;
15927        if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("constraint")) {
15928            self.advance();
15929            name = Some(self.expect_ident_like()?);
15930        }
15931        // `FOREIGN`
15932        match self.advance() {
15933            Token::Ident(s) if s.eq_ignore_ascii_case("foreign") => {}
15934            other => return Err(self.err(format!("expected FOREIGN, got {other:?}"))),
15935        }
15936        // `KEY`
15937        match self.advance() {
15938            Token::Ident(s) if s.eq_ignore_ascii_case("key") => {}
15939            other => return Err(self.err(format!("expected KEY after FOREIGN, got {other:?}"))),
15940        }
15941        // `(col, col, ...)`
15942        if !matches!(self.peek(), Token::LParen) {
15943            return Err(self.err(format!(
15944                "expected '(' after FOREIGN KEY, got {:?}",
15945                self.peek()
15946            )));
15947        }
15948        self.advance();
15949        let mut columns = Vec::new();
15950        loop {
15951            columns.push(self.expect_ident_like()?);
15952            match self.peek() {
15953                Token::Comma => {
15954                    self.advance();
15955                }
15956                Token::RParen => {
15957                    self.advance();
15958                    break;
15959                }
15960                other => {
15961                    return Err(self.err(format!(
15962                        "expected ',' or ')' in FK column list, got {other:?}"
15963                    )));
15964                }
15965            }
15966        }
15967        if columns.is_empty() {
15968            return Err(self.err("FOREIGN KEY requires at least one column".into()));
15969        }
15970        let (
15971            parent_table,
15972            parent_columns,
15973            on_delete,
15974            on_update,
15975            match_type,
15976            deferrable,
15977            initially_deferred,
15978        ) = self.parse_references_tail(columns.len())?;
15979        Ok(ForeignKeyConstraint {
15980            name,
15981            columns,
15982            parent_table,
15983            parent_columns,
15984            on_delete,
15985            on_update,
15986            match_type,
15987            deferrable,
15988            initially_deferred,
15989        })
15990    }
15991
15992    /// v7.6.0 — parse the tail `REFERENCES <tbl> [(<pcol>...)] [ON
15993    /// DELETE <action>] [ON UPDATE <action>]`. `expected_arity` is
15994    /// the local column count, used to default the parent column
15995    /// list when omitted (SQL spec: parent's PK is implied).
15996    fn parse_references_tail(
15997        &mut self,
15998        expected_arity: usize,
15999    ) -> Result<
16000        (
16001            String,
16002            Vec<String>,
16003            FkAction,
16004            FkAction,
16005            crate::ast::MatchType,
16006            // v7.39 (round 288) — deferrable, initially_deferred.
16007            bool,
16008            bool,
16009        ),
16010        ParseError,
16011    > {
16012        match self.advance() {
16013            Token::Ident(s) if s.eq_ignore_ascii_case("references") => {}
16014            other => return Err(self.err(format!("expected REFERENCES, got {other:?}"))),
16015        }
16016        let parent_table = self.expect_ident_like()?;
16017        let mut parent_columns: Vec<String> = Vec::new();
16018        if matches!(self.peek(), Token::LParen) {
16019            self.advance();
16020            loop {
16021                parent_columns.push(self.expect_ident_like()?);
16022                match self.peek() {
16023                    Token::Comma => {
16024                        self.advance();
16025                    }
16026                    Token::RParen => {
16027                        self.advance();
16028                        break;
16029                    }
16030                    other => {
16031                        return Err(self.err(format!(
16032                            "expected ',' or ')' in REFERENCES column list, got {other:?}"
16033                        )));
16034                    }
16035                }
16036            }
16037        }
16038        if !parent_columns.is_empty() && parent_columns.len() != expected_arity {
16039            return Err(self.err(format!(
16040                "FK arity mismatch: {} local column(s) vs {} parent column(s)",
16041                expected_arity,
16042                parent_columns.len()
16043            )));
16044        }
16045        // Optional `MATCH {SIMPLE | FULL | PARTIAL}`. PG's grammar puts
16046        // it between the referenced column list and the ON / DEFERRABLE
16047        // trailers. SPG implements MATCH SIMPLE semantics (the FK check
16048        // is skipped when any referencing column is NULL), so SIMPLE —
16049        // the default, and the only spelling pg_dump emits — is accepted
16050        // as a no-op. MATCH FULL / MATCH PARTIAL need the per-FK
16051        // mixed-NULL rule, which is not wired yet; reject them honestly
16052        // rather than silently applying SIMPLE (PG itself errors on
16053        // MATCH PARTIAL as "not yet implemented").
16054        let mut match_type = crate::ast::MatchType::Simple;
16055        if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("match")) {
16056            self.advance();
16057            // `FULL` is a reserved keyword token (FULL OUTER JOIN);
16058            // SIMPLE / PARTIAL arrive as bare identifiers.
16059            let kind = match self.advance() {
16060                Token::Full => "FULL".to_string(),
16061                Token::Ident(s) => s.to_uppercase(),
16062                other => {
16063                    return Err(self.err(format!(
16064                        "expected FULL, PARTIAL or SIMPLE after MATCH, got {other:?}"
16065                    )));
16066                }
16067            };
16068            match kind.as_str() {
16069                "SIMPLE" => {} // Default — match_type stays Simple.
16070                // v7.38 (read01, T29) — MATCH FULL: the check is skipped only
16071                // when ALL referencing columns are NULL; a mixed-NULL key errors.
16072                "FULL" => match_type = crate::ast::MatchType::Full,
16073                "PARTIAL" => {
16074                    return Err(self.err("MATCH PARTIAL not yet implemented".to_string()));
16075                }
16076                _ => {
16077                    return Err(self.err(format!(
16078                        "expected FULL, PARTIAL or SIMPLE after MATCH, got {kind}"
16079                    )));
16080                }
16081            }
16082        }
16083        // v7.6.7 / v7.17.0 Phase 3.1 — interleave `[NOT] DEFERRABLE
16084        // [INITIALLY {DEFERRED | IMMEDIATE}]` and `ON DELETE
16085        // <action>` / `ON UPDATE <action>` in either order. PG /
16086        // pg_dump emits the timing clause AFTER the ON clauses
16087        // (`ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED`),
16088        // but the SQL spec allows either order. We loop over
16089        // every possible trailer and dispatch on the next token,
16090        // stopping when nothing matches. Phase 3.1 changes the
16091        // bare DEFERRABLE form from hard-error to accept-as-
16092        // immediate; SPG is single-writer with no deferred-
16093        // constraint window so the runtime semantics are always
16094        // immediate even when INITIALLY DEFERRED is requested.
16095        // PG's default referential action (no ON DELETE / ON UPDATE
16096        // clause) is NO ACTION, not RESTRICT — the two enforce
16097        // identically in SPG (single-writer, no deferred window; see the
16098        // shared match arm in constraints.rs) but information_schema.
16099        // referential_constraints must report NO ACTION to match PG.
16100        let mut on_delete = FkAction::NoAction;
16101        let mut on_update = FkAction::NoAction;
16102        let mut seen_on_delete = false;
16103        let mut seen_on_update = false;
16104        let mut deferrable = false;
16105        let mut initially_deferred = false;
16106        loop {
16107            // DEFERRABLE / NOT DEFERRABLE / INITIALLY shapes.
16108            let before = self.pos;
16109            let (d, idef) = self.consume_deferrable_clauses_timed()?;
16110            if self.pos != before {
16111                deferrable = d;
16112                initially_deferred = idef;
16113                continue;
16114            }
16115            // ON DELETE / ON UPDATE.
16116            if !matches!(self.peek(), Token::On) {
16117                break;
16118            }
16119            self.advance();
16120            let which = self.advance();
16121            let action = self.parse_fk_action()?;
16122            match which {
16123                Token::Ident(ref s) if s.eq_ignore_ascii_case("delete") => {
16124                    if seen_on_delete {
16125                        return Err(self.err("ON DELETE specified twice".into()));
16126                    }
16127                    seen_on_delete = true;
16128                    on_delete = action;
16129                }
16130                Token::Ident(ref s) if s.eq_ignore_ascii_case("update") => {
16131                    if seen_on_update {
16132                        return Err(self.err("ON UPDATE specified twice".into()));
16133                    }
16134                    seen_on_update = true;
16135                    on_update = action;
16136                }
16137                other => {
16138                    return Err(
16139                        self.err(format!("expected DELETE or UPDATE after ON, got {other:?}"))
16140                    );
16141                }
16142            }
16143        }
16144        Ok((
16145            parent_table,
16146            parent_columns,
16147            on_delete,
16148            on_update,
16149            match_type,
16150            deferrable,
16151            initially_deferred,
16152        ))
16153    }
16154
16155    /// v7.6.0 — parse `CASCADE | RESTRICT | SET NULL | SET DEFAULT |
16156    /// NO ACTION`.
16157    fn parse_fk_action(&mut self) -> Result<FkAction, ParseError> {
16158        match self.advance() {
16159            Token::Ident(s) if s.eq_ignore_ascii_case("cascade") => Ok(FkAction::Cascade),
16160            Token::Ident(s) if s.eq_ignore_ascii_case("restrict") => Ok(FkAction::Restrict),
16161            Token::Ident(s) if s.eq_ignore_ascii_case("set") => match self.advance() {
16162                Token::Null => Ok(FkAction::SetNull),
16163                Token::Default => Ok(FkAction::SetDefault),
16164                other => Err(self.err(format!(
16165                    "expected NULL or DEFAULT after SET in FK action, got {other:?}"
16166                ))),
16167            },
16168            Token::Ident(s) if s.eq_ignore_ascii_case("no") => match self.advance() {
16169                Token::Ident(s) if s.eq_ignore_ascii_case("action") => Ok(FkAction::NoAction),
16170                other => Err(self.err(format!(
16171                    "expected ACTION after NO in FK action, got {other:?}"
16172                ))),
16173            },
16174            other => Err(self.err(format!(
16175                "expected CASCADE | RESTRICT | SET NULL | SET DEFAULT | NO ACTION, got {other:?}"
16176            ))),
16177        }
16178    }
16179
16180    /// Recognise the optional `IF NOT EXISTS` prefix shared by `CREATE
16181    /// TABLE` and `CREATE INDEX`. Returns `true` if consumed.
16182    fn consume_if_not_exists(&mut self) -> bool {
16183        // `IF` arrives as a bare Ident (we don't reserve it because it
16184        // also appears mid-expression in PG, though we don't support
16185        // those forms yet).
16186        let looks_like_if = matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("if"));
16187        if !looks_like_if {
16188            return false;
16189        }
16190        // Peek one ahead before committing: only consume IF when it's
16191        // actually `IF NOT EXISTS`.
16192        if !matches!(self.tokens.get(self.pos + 1), Some(Token::Not)) {
16193            return false;
16194        }
16195        if !matches!(
16196            self.tokens.get(self.pos + 2),
16197            Some(Token::Ident(s)) if s.eq_ignore_ascii_case("exists")
16198        ) {
16199            return false;
16200        }
16201        self.advance(); // IF
16202        self.advance(); // NOT
16203        self.advance(); // EXISTS
16204        true
16205    }
16206
16207    /// v7.12.4 — `IF EXISTS` modifier for DROP statements.
16208    /// Consumes IF EXISTS as a pair; returns false otherwise
16209    /// without consuming any tokens.
16210    /// v7.39 (RLS) — consume the `ROW LEVEL SECURITY` keyword triple after
16211    /// ENABLE/DISABLE/FORCE/NO FORCE.
16212    fn expect_row_level_security(&mut self) -> Result<(), ParseError> {
16213        for kw in ["row", "level", "security"] {
16214            if !matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case(kw))
16215            {
16216                return Err(self.err(alloc::format!(
16217                    "expected {} in ROW LEVEL SECURITY, got {:?}",
16218                    kw.to_ascii_uppercase(),
16219                    self.peek()
16220                )));
16221            }
16222            self.advance();
16223        }
16224        Ok(())
16225    }
16226
16227    fn consume_if_exists(&mut self) -> bool {
16228        let looks_like_if = matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("if"));
16229        if !looks_like_if {
16230            return false;
16231        }
16232        if !matches!(
16233            self.tokens.get(self.pos + 1),
16234            Some(Token::Ident(s)) if s.eq_ignore_ascii_case("exists")
16235        ) {
16236            return false;
16237        }
16238        self.advance(); // IF
16239        self.advance(); // EXISTS
16240        true
16241    }
16242
16243    /// v7.9.14 — consume `ASC | DESC | NULLS FIRST | NULLS LAST`
16244    /// qualifiers after an index column ref. ASC / DESC are
16245    /// reserved tokens; NULLS / FIRST / LAST are bare idents.
16246    /// We accept and discard them since single-column BTree
16247    /// stores rows in natural key order today.
16248    /// v7.24 (round-16 A) — `NULLS FIRST` / `NULLS LAST` after an
16249    /// ORDER BY key. Returns None when absent.
16250    fn parse_optional_nulls_placement(&mut self) -> Result<Option<bool>, ParseError> {
16251        if !matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("nulls")) {
16252            return Ok(None);
16253        }
16254        self.advance();
16255        match self.advance() {
16256            Token::Ident(s) if s.eq_ignore_ascii_case("first") => Ok(Some(true)),
16257            Token::Ident(s) if s.eq_ignore_ascii_case("last") => Ok(Some(false)),
16258            other => Err(self.err(alloc::format!(
16259                "expected FIRST or LAST after NULLS, got {other:?}"
16260            ))),
16261        }
16262    }
16263
16264    /// v7.39 (round 537) — the per-column ordering clause, REPORTED now
16265    /// rather than discarded.
16266    ///
16267    /// SPG's index does not scan in a direction — column ordering is
16268    /// intrinsic to the storage — but `pg_indexes.indexdef` is a
16269    /// reproduction of the DDL, and dropping the clause meant
16270    /// `CREATE INDEX i ON t (a DESC NULLS LAST)` read back as `(a)`. A
16271    /// dump lost it, and a schema diff saw drift on every run.
16272    fn consume_optional_index_column_qualifiers(&mut self) -> crate::ast::IndexColumnOrder {
16273        let mut order = crate::ast::IndexColumnOrder::default();
16274        loop {
16275            match self.peek() {
16276                Token::Asc => {
16277                    self.advance();
16278                }
16279                Token::Desc => {
16280                    order.descending = true;
16281                    self.advance();
16282                }
16283                Token::Ident(s) if s.eq_ignore_ascii_case("nulls") => {
16284                    let look = self.tokens.get(self.pos + 1);
16285                    if matches!(
16286                        look,
16287                        Some(Token::Ident(k)) if k.eq_ignore_ascii_case("first")
16288                            || k.eq_ignore_ascii_case("last")
16289                    ) {
16290                        self.advance();
16291                        order.nulls_first = Some(matches!(
16292                            self.advance(),
16293                            Token::Ident(k) if k.eq_ignore_ascii_case("first")
16294                        ));
16295                    } else {
16296                        break;
16297                    }
16298                }
16299                _ => break,
16300            }
16301        }
16302        order
16303    }
16304
16305    fn parse_create_index_stmt_after_create(
16306        &mut self,
16307        is_unique: bool,
16308    ) -> Result<Statement, ParseError> {
16309        // Caller consumed CREATE (and the optional UNIQUE); we're on INDEX.
16310        debug_assert!(matches!(self.peek(), Token::Index));
16311        self.advance();
16312        // v7.37.17 (17.6 partial) — CONCURRENTLY noise word (PG 8.2+).
16313        // SPG's CREATE INDEX is synchronous end-to-end today (real
16314        // CONCURRENTLY variant with restartable scans queues with
16315        // v7.39 indexes epic), so the modifier has no runtime effect
16316        // — same accept-and-no-op shape as v7.37.16.5 DETACH
16317        // PARTITION CONCURRENTLY and v7.37.19.8 REFRESH MATERIALIZED
16318        // VIEW CONCURRENTLY.
16319        let mut concurrently = false;
16320        if matches!(
16321            self.peek(),
16322            Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("concurrently")
16323        ) {
16324            self.advance();
16325            concurrently = true;
16326        }
16327        let if_not_exists = self.consume_if_not_exists();
16328        // v7.39 (read01 round 93) — the index name is optional (PG since
16329        // forever): `CREATE INDEX ON t (a)` lets the server pick a name.
16330        // When the token after `[IF NOT EXISTS]` is already `ON`, no name
16331        // was given; leave it empty and the engine derives a PG-style
16332        // `<table>_<cols>_idx` name at CREATE time (with collision counter).
16333        let name = if matches!(self.peek(), Token::On) {
16334            String::new()
16335        } else {
16336            self.expect_ident_like()?
16337        };
16338        if !matches!(self.peek(), Token::On) {
16339            return Err(self.err(format!(
16340                "expected ON after CREATE INDEX <name>, got {:?}",
16341                self.peek()
16342            )));
16343        }
16344        self.advance();
16345        let table = self.expect_ident_like()?;
16346        // Optional `USING <method>` — only recognised method in v2.0 is
16347        // `hnsw` (a single-layer NSW graph for kNN). `USING` is the bare
16348        // ident `using` (we don't promote it to a reserved keyword
16349        // because it isn't reserved anywhere else in our SQL surface).
16350        let mut method_name: Option<String> = None;
16351        let method = if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("using")) {
16352            self.advance();
16353            let m = self.expect_ident_like()?;
16354            method_name = Some(m.to_ascii_lowercase());
16355            match m.to_ascii_lowercase().as_str() {
16356                "hnsw" => IndexMethod::Hnsw,
16357                "btree" => IndexMethod::BTree,
16358                "brin" => IndexMethod::Brin,
16359                // v7.12.3 — real GIN inverted index over `tsvector`.
16360                // v7.9.26b's `USING gin` → BTree silent fallback is
16361                // gone; the engine validates that the indexed column
16362                // is `tsvector` at CREATE INDEX time.
16363                "gin" => IndexMethod::Gin,
16364                // v7.9.26b — PG `pg_dump` emits `USING gist` /
16365                // `USING spgist` / `USING hash` for their built-in
16366                // AMs that SPG doesn't have a matching
16367                // implementation for; degrade to BTree on the
16368                // leading column so the schema loads + the index
16369                // catalogue stays consistent. Operator pays the
16370                // planner cost only for the queries that would have
16371                // used the specialised AM.
16372                "gist" | "spgist" | "hash" => IndexMethod::BTree,
16373                // v7.11.3 — pgvector ships both `ivfflat` and
16374                // `hnsw`. Customers shouldn't have to choose
16375                // their on-disk index method based on what SPG
16376                // implements; accept `ivfflat` as a synonym for
16377                // `hnsw` so PG schemas using either method drop
16378                // in. The vector distance op (`<->` / `<#>` /
16379                // `<=>`) at query time still picks the metric.
16380                "ivfflat" => IndexMethod::Hnsw,
16381                other => {
16382                    return Err(self.err(alloc::format!(
16383                        "unknown index method {other:?}; supported: hnsw, btree, brin, gin (gist/spgist/hash accepted as BTree fallback)"
16384                    )));
16385                }
16386            }
16387        } else {
16388            IndexMethod::BTree
16389        };
16390        if !matches!(self.peek(), Token::LParen) {
16391            return Err(self.err(format!(
16392                "expected '(' before indexed column, got {:?}",
16393                self.peek()
16394            )));
16395        }
16396        self.advance();
16397        // v6.8.2 — accept either a bare column ident (legacy) or
16398        // an expression `fn(col, …)` for expression indexes.
16399        // Distinguish by peeking the token *after* the current
16400        // ident: `ident )` is the legacy column-only path;
16401        // anything else triggers the Pratt expression parser.
16402        // (`advance()` uses `mem::replace` to nil out the current
16403        // slot, so we can't save+rewind cleanly — peek-ahead via
16404        // direct index avoids the mutation.)
16405        let mut opclass: Option<String> = None;
16406        let mut key_collation: Option<String> = None;
16407        let (column, expression): (String, Option<Expr>) = match self.peek().clone() {
16408            // Single column with `)` immediately after — fast path.
16409            // v7.9.29 — also: bare column followed by `,` (the
16410            // multi-column form `(a, b, c)`). Without this branch
16411            // the leading ident gets pulled into `parse_expr`
16412            // which then sets `expression = Some(Column(a))` and
16413            // breaks Display round-trip on the multi-column shape.
16414            Token::Ident(s) | Token::QuotedIdent(s)
16415                if matches!(
16416                    self.tokens.get(self.pos + 1),
16417                    Some(Token::RParen | Token::Comma)
16418                ) =>
16419            {
16420                self.advance();
16421                (s, None)
16422            }
16423            // v7.9.22 — single column followed by a pgvector
16424            // opclass ident: `(col vector_cosine_ops)`. mailrs G5.
16425            // v7.15.0 — capture the opclass instead of discarding
16426            // it so the engine can dispatch (e.g. `gin_trgm_ops`
16427            // → real trigram-shingle GIN over a TEXT column).
16428            // Vector/HNSW opclasses still take their distance
16429            // metric from the query operator (`<->` / `<#>` /
16430            // `<=>`), so for those callers the opclass stays
16431            // informational.
16432            // v7.22 (mailrs round-13 gap 7) — pg_dump qualifies the
16433            // opclass: `(embedding public.vector_cosine_ops)`. Strip
16434            // the schema and dispatch on the bare opclass, the same
16435            // treatment table/type names get.
16436            Token::Ident(s) | Token::QuotedIdent(s)
16437                if matches!(
16438                    self.tokens.get(self.pos + 1),
16439                    Some(Token::Ident(_) | Token::QuotedIdent(_))
16440                ) && matches!(self.tokens.get(self.pos + 2), Some(Token::Dot))
16441                    && matches!(
16442                        self.tokens.get(self.pos + 3),
16443                        Some(Token::Ident(op) | Token::QuotedIdent(op))
16444                            if is_vector_opclass_name(op)
16445                    ) =>
16446            {
16447                self.advance(); // column name
16448                self.advance(); // schema qualifier
16449                self.advance(); // dot
16450                let op_tok = self.advance();
16451                if let Token::Ident(op) | Token::QuotedIdent(op) = op_tok {
16452                    opclass = Some(op.to_ascii_lowercase());
16453                }
16454                (s, None)
16455            }
16456            // r1038 — an operator class is recognised by its POSITION, not
16457            // by a list of names. It used to be `is_vector_opclass_name`,
16458            // so `USING gin (doc jsonb_path_ops)` — ordinary PG, and what
16459            // sentori's migration wrote — was a syntax error while
16460            // `USING gin (doc)` parsed. Anything sitting between a column
16461            // name and a `,` `)` ASC DESC NULLS COLLATE is an opclass;
16462            // two bare identifiers in a row are not valid there otherwise.
16463            Token::Ident(s) | Token::QuotedIdent(s)
16464                if matches!(
16465                    self.tokens.get(self.pos + 1),
16466                    Some(Token::Ident(op) | Token::QuotedIdent(op))
16467                        if is_vector_opclass_name(op) || Self::opclass_position_follows(
16468                            self.tokens.get(self.pos + 2)
16469                        )
16470                ) =>
16471            {
16472                self.advance(); // column name
16473                // Capture the opclass token, lower-cased for
16474                // case-insensitive engine dispatch.
16475                let op_tok = self.advance();
16476                if let Token::Ident(op) | Token::QuotedIdent(op) = op_tok {
16477                    opclass = Some(op.to_ascii_lowercase());
16478                }
16479                (s, None)
16480            }
16481            Token::Ident(_) | Token::QuotedIdent(_) => {
16482                // v7.39 (round 538) — an explicit COLLATE on the key,
16483                // read by LOOKAHEAD because `parse_expr` absorbs the
16484                // clause as a no-op (SPG orders text by bytes, which is
16485                // the C collation, so it changes nothing to honour). PG
16486                // still PRINTS it: an explicitly written `"C"` and the
16487                // collation a column inherits are different collation
16488                // OBJECTS even where they sort identically, which is why
16489                // `(a COLLATE "C")` shows on a C-collation database too.
16490                if matches!(
16491                    self.tokens.get(self.pos + 1),
16492                    Some(Token::Ident(w)) if w.eq_ignore_ascii_case("collate")
16493                ) {
16494                    key_collation = match self.tokens.get(self.pos + 2) {
16495                        Some(Token::Ident(n) | Token::QuotedIdent(n) | Token::String(n)) => {
16496                            Some(n.clone())
16497                        }
16498                        _ => None,
16499                    };
16500                }
16501                // v7.39.2 — the clause is read by the LOOKAHEAD above and
16502                // belongs to the KEY, not to the expression. Since
16503                // `COLLATE` became a node, letting `parse_expr` build one
16504                // here put the collation in twice and the key deparsed as
16505                // `(c COLLATE "C" COLLATE "C")`. The ORDER-BY-key channel
16506                // is the same idea and already exists, so this borrows it:
16507                // absorb into the side channel, and the key's own
16508                // lookahead is what carries it.
16509                // v7.39.2 — and the key can only CARRY the byte-order
16510                // spellings. Absorbing into the side channel accepts any
16511                // name, so suppressing the node here without this check
16512                // silently accepted `(name COLLATE "en_US")`, which SPG's
16513                // index cannot honour — a refusal that was doing real
16514                // work, removed by the suppression and put back here.
16515                if let Some(name) = &key_collation {
16516                    let lc = name.to_ascii_lowercase();
16517                    let byte_order = matches!(
16518                        lc.as_str(),
16519                        "c" | "posix" | "default" | "ucs_basic" | "pg_c_utf8"
16520                    );
16521                    let mysql_ok = self.mysql_dialect
16522                        && (lc.ends_with("_ci")
16523                            || lc.ends_with("_bin")
16524                            || lc == "binary"
16525                            || matches!(lc.as_str(), "case_insensitive" | "nocase"));
16526                    if !byte_order && !mysql_ok {
16527                        return Err(self.err(alloc::format!(
16528                            "COLLATE {name:?} is not supported in this position: an index \
16529                             key carries the byte-order spellings only. Declare it on the \
16530                             column (`x text COLLATE {name:?}`) instead"
16531                        )));
16532                    }
16533                }
16534                let saved_key_ctx = self.in_order_by_key;
16535                self.in_order_by_key = true;
16536                let key_expr = self.parse_expr(0);
16537                self.in_order_by_key = saved_key_ctx;
16538                let key_expr = key_expr?;
16539                let primary = extract_first_column(&key_expr).ok_or_else(|| {
16540                    self.err("expression index key must reference at least one column".into())
16541                })?;
16542                (primary, Some(key_expr))
16543            }
16544            // v7.37.43-T4 — parenthesised expression index key
16545            // `CREATE INDEX … ON t ((payload->'bundle'->>'id'))`.
16546            // PG's CREATE INDEX requires the expression to be in
16547            // its own parens to disambiguate function calls from
16548            // column lists, so this `LParen` is the inner open-paren
16549            // of an expression key. parse_expr handles the recursive
16550            // descent and consumes the matching `RParen`.
16551            Token::LParen => {
16552                let key_expr = self.parse_expr(0)?;
16553                let primary = extract_first_column(&key_expr).ok_or_else(|| {
16554                    self.err("expression index key must reference at least one column".into())
16555                })?;
16556                (primary, Some(key_expr))
16557            }
16558            other => {
16559                return Err(self.err(format!(
16560                    "expected column ident or expression, got {other:?}"
16561                )));
16562            }
16563        };
16564        // v7.9.14 — accept extra comma-separated columns inside
16565        // the index key parens (`CREATE INDEX … (a, b, c)`).
16566        // mailrs F2.
16567        //
16568        // v7.39.11 — each extra column's `ASC` / `DESC` / `NULLS FIRST`
16569        // / `NULLS LAST` is KEPT. It used to be parsed and dropped on
16570        // the floor, so `CREATE INDEX i ON t (a, b DESC)` read back from
16571        // `pg_get_indexdef` as `(a, b)`: a dump lost the clause and a
16572        // schema diff saw drift on every run. Reported by sentori
16573        // against 7.39.10, and the same defect round 537 fixed for the
16574        // LEADING column, in the loop right beside it.
16575        let mut extra_columns: Vec<String> = Vec::new();
16576        let mut extra_orders: Vec<crate::ast::IndexColumnOrder> = Vec::new();
16577        // The leading column may also have ASC/DESC after it — and that
16578        // one is the column SPG indexes, so its clause is kept.
16579        let key_order = self.consume_optional_index_column_qualifiers();
16580        while matches!(self.peek(), Token::Comma) {
16581            self.advance();
16582            let extra = self.expect_ident_like()?;
16583            extra_orders.push(self.consume_optional_index_column_qualifiers());
16584            extra_columns.push(extra);
16585        }
16586        if !matches!(self.peek(), Token::RParen) {
16587            return Err(self.err(format!(
16588                "expected ')' after indexed column / expression, got {:?}",
16589                self.peek()
16590            )));
16591        }
16592        self.advance();
16593        // v6.8.0 — optional `INCLUDE (col1, col2, …)` clause for
16594        // index-only-scan annotation. Bare ident (not a reserved
16595        // keyword) so we test by case-insensitive string match.
16596        let included_columns = if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("include"))
16597        {
16598            self.advance();
16599            if !matches!(self.peek(), Token::LParen) {
16600                return Err(self.err(format!("expected '(' after INCLUDE, got {:?}", self.peek())));
16601            }
16602            self.advance();
16603            let mut cols = Vec::new();
16604            loop {
16605                cols.push(self.expect_ident_like()?);
16606                match self.peek() {
16607                    Token::Comma => {
16608                        self.advance();
16609                    }
16610                    Token::RParen => {
16611                        self.advance();
16612                        break;
16613                    }
16614                    other => {
16615                        return Err(self.err(format!(
16616                            "expected ',' or ')' in INCLUDE list, got {other:?}"
16617                        )));
16618                    }
16619                }
16620            }
16621            cols
16622        } else {
16623            Vec::new()
16624        };
16625        // v7.11.3 — accept and discard PG `WITH (k = v, ...)` index
16626        // storage parameters. pgvector emits `WITH (lists = N)` for
16627        // ivfflat and `WITH (m = N, ef_construction = M)` for hnsw;
16628        // SPG's HNSW picks its own parameters today (tunable via
16629        // env vars), so the WITH clause is informational and dropped.
16630        if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("with")) {
16631            self.advance();
16632            if !matches!(self.peek(), Token::LParen) {
16633                return Err(self.err(format!(
16634                    "expected '(' after WITH in CREATE INDEX, got {:?}",
16635                    self.peek()
16636                )));
16637            }
16638            self.advance();
16639            loop {
16640                if matches!(self.peek(), Token::RParen) {
16641                    self.advance();
16642                    break;
16643                }
16644                // Drain `key = value` or bare `key` tokens.
16645                let _ = self.advance(); // key
16646                if matches!(self.peek(), Token::Eq) {
16647                    self.advance();
16648                    let _ = self.advance(); // value (int / string / ident)
16649                }
16650                match self.peek() {
16651                    Token::Comma => {
16652                        self.advance();
16653                    }
16654                    Token::RParen => {
16655                        self.advance();
16656                        break;
16657                    }
16658                    other => {
16659                        return Err(self.err(format!(
16660                            "expected ',' or ')' in WITH (…) clause, got {other:?}"
16661                        )));
16662                    }
16663                }
16664            }
16665        }
16666        // v7.39 (read01 round 52) — optional `NULLS [NOT] DISTINCT` (PG 15+),
16667        // which sits between the key list and the WHERE clause.
16668        let mut nulls_not_distinct = false;
16669        if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("nulls")) {
16670            let n1 = self.tokens.get(self.pos + 1);
16671            let n2 = self.tokens.get(self.pos + 2);
16672            if matches!(n1, Some(Token::Not)) && matches!(n2, Some(Token::Distinct)) {
16673                self.advance(); // NULLS
16674                self.advance(); // NOT
16675                self.advance(); // DISTINCT
16676                nulls_not_distinct = true;
16677            } else if matches!(n1, Some(Token::Distinct)) {
16678                self.advance(); // NULLS
16679                self.advance(); // DISTINCT
16680            }
16681        }
16682        // v6.8.1 — optional `WHERE <expr>` partial-index predicate.
16683        let partial_predicate = if matches!(self.peek(), Token::Where) {
16684            self.advance();
16685            Some(self.parse_expr(0)?)
16686        } else {
16687            None
16688        };
16689        // v7.9.29 — UNIQUE on a vector index (HNSW) makes no
16690        // sense: uniqueness over an ANN structure has no clean
16691        // semantics. Reject early. (BRIN UNIQUE is similarly
16692        // meaningless — block both.)
16693        if is_unique && !matches!(method, IndexMethod::BTree) {
16694            return Err(self.err(alloc::format!(
16695                "UNIQUE is only supported on BTree indexes, got USING {:?}",
16696                method
16697            )));
16698        }
16699        Ok(Statement::CreateIndex(CreateIndexStatement {
16700            concurrently,
16701            name,
16702            key_order,
16703            key_collation,
16704            table,
16705            column,
16706            nulls_not_distinct,
16707            method,
16708            if_not_exists,
16709            included_columns,
16710            partial_predicate,
16711            extra_columns: extra_columns.clone(),
16712            extra_orders: extra_orders.clone(),
16713            expression,
16714            is_unique,
16715            opclass,
16716            method_name,
16717        }))
16718    }
16719
16720    /// v7.6.0 — wraps `parse_column_def` and consumes an optional
16721    /// column-level `REFERENCES ...` clause. The trailing FK is
16722    /// normalised into table-level shape (single-element columns +
16723    /// parent_columns) so the engine sees one uniform constraint list.
16724    fn parse_column_def_with_fk(
16725        &mut self,
16726    ) -> Result<(ColumnDef, Option<ForeignKeyConstraint>), ParseError> {
16727        let col = self.parse_column_def()?;
16728        // v7.39 (round 308, V29) — an explicitly named inline FK:
16729        // `col INT CONSTRAINT fk_a REFERENCES tbl(pcol)`. The column-def
16730        // loop leaves this spelling intact precisely so the name can be
16731        // kept here; PG reports it in violation messages and matches it
16732        // in `SET CONSTRAINTS`.
16733        let declared_name = if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("constraint"))
16734        {
16735            self.advance();
16736            Some(self.expect_ident_like()?)
16737        } else {
16738            None
16739        };
16740        // Inline form: `col INT REFERENCES tbl(pcol) [ON DELETE ...] [ON UPDATE ...]`.
16741        let inline_references = matches!(
16742            self.peek(),
16743            Token::Ident(s) if s.eq_ignore_ascii_case("references")
16744        );
16745        if !inline_references {
16746            return Ok((col, None));
16747        }
16748        let (
16749            parent_table,
16750            parent_columns,
16751            on_delete,
16752            on_update,
16753            match_type,
16754            deferrable,
16755            initially_deferred,
16756        ) = self.parse_references_tail(1)?;
16757        let fk = ForeignKeyConstraint {
16758            name: declared_name,
16759            columns: vec![col.name.clone()],
16760            parent_table,
16761            parent_columns,
16762            on_delete,
16763            on_update,
16764            match_type,
16765            deferrable,
16766            initially_deferred,
16767        };
16768        Ok((col, Some(fk)))
16769    }
16770
16771    /// v7.13.0 — parse a column type (consuming the type ident and
16772    /// any trailing parameters / `[]`), without surrounding column
16773    /// constraints. Used by ALTER COLUMN TYPE (mailrs round-5 G8).
16774    /// Returns the resolved `ColumnTypeName` plus implied
16775    /// `(auto_increment, not_null)` flags from PG SERIAL family
16776    /// shorthands — callers that don't expect those (ALTER COLUMN
16777    /// TYPE) can discard them.
16778    fn parse_column_type_name(&mut self) -> Result<ColumnTypeName, ParseError> {
16779        let (ty, _, _, _, _, _, _, _, _, _, _, _, _, _) = self.parse_type_with_implied_flags()?;
16780        Ok(ty)
16781    }
16782
16783    #[allow(clippy::type_complexity)]
16784    fn parse_type_with_implied_flags(
16785        &mut self,
16786    ) -> Result<
16787        (
16788            ColumnTypeName,
16789            bool,
16790            bool,
16791            Option<String>,
16792            Collation,
16793            // v7.39 (round 370, M4 P4a) — was `COLLATE` written explicitly?
16794            bool,
16795            // v7.39 (round 676) — the collation NAME as written, which the
16796            // `Collation` enum above cannot carry.
16797            Option<String>,
16798            bool,
16799            // v7.17.0 Phase 3.P0-36 — MySQL inline ENUM variant
16800            // list captured at type-parse time. None for all
16801            // non-ENUM types.
16802            Option<Vec<String>>,
16803            // v7.17.0 Phase 3.P0-37 — MySQL inline SET variant
16804            // list. Distinct from ENUM (subset semantics).
16805            Option<Vec<String>>,
16806            // v7.39 (round 386, epic P1) — declared TINYINT / MEDIUMINT
16807            // width, lost when the type collapses to SmallInt / Int.
16808            Option<MysqlIntWidth>,
16809            // v7.39 (round 424) — declared fractional-seconds precision of a
16810            // MySQL temporal column (bare spelling = 0). None under PG.
16811            Option<u8>,
16812            // v7.39.2 — written `TIMESTAMP` rather than `DATETIME`. The
16813            // two are different types on MySQL and SPG stores both as
16814            // `Timestamp`, so the spelling has to travel separately or
16815            // a dump silently rewrites the column.
16816            bool,
16817            // v7.39.3 — a MySQL `FLOAT(m,d)` / `DOUBLE(m,d)` pair. Not a
16818            // display hint: it rounds on write.
16819            Option<(u8, u8)>,
16820        ),
16821        ParseError,
16822    > {
16823        let mut ty_ident = match self.advance() {
16824            Token::Ident(s) => s,
16825            // v7.37.5 β-P2 — `INTERVAL` lexes as a reserved keyword
16826            // (Token::Interval) since v7.9.25 to drive the `INTERVAL
16827            // '<span>'` literal grammar. As a column type it lands
16828            // here directly; downstream resolution still uses the
16829            // canonical lowercase string.
16830            Token::Interval => "interval".to_string(),
16831            other => {
16832                return Err(ParseError {
16833                    message: format!("expected column type, got {other:?}"),
16834                    token_pos: self.consumed_pos(),
16835                });
16836            }
16837        };
16838        // v7.22 (mailrs round-13 gap 4) — schema-qualified type names:
16839        // pg_dump qualifies extension types (`public.vector(1024)`).
16840        // SPG is single-namespace; drop the schema and resolve the
16841        // bare type — same treatment table names already get.
16842        while matches!(self.peek(), Token::Dot) {
16843            self.advance();
16844            ty_ident = self.expect_ident_like()?;
16845        }
16846        let mut implied_auto_increment = false;
16847        let mut implied_not_null = false;
16848        let mut user_type_ref: Option<String> = None;
16849        // v7.17.0 Phase 3.P0-36 — MySQL inline ENUM('a','b','c')
16850        // value list, captured here and bubbled up through the
16851        // ColumnDef so the engine can attach it to the column
16852        // schema (and validate INSERT cells against it).
16853        let mut inline_enum_variants: Option<Vec<String>> = None;
16854        // v7.17.0 Phase 3.P0-37 — MySQL inline SET variant list.
16855        let mut inline_set_variants: Option<Vec<String>> = None;
16856        // v7.39 (round 386, type-fidelity epic P1) — the declared MySQL
16857        // narrow-int width (TINYINT / MEDIUMINT), captured before the type
16858        // collapses to SmallInt / Int. Only under the MySQL dialect.
16859        let mut mysql_int_width: Option<MysqlIntWidth> = None;
16860        // v7.39 (round 424) — the declared fractional-seconds precision of a
16861        // MySQL temporal column. Set by the temporal arms below; stays None
16862        // for PG (whose temporal columns keep full microseconds).
16863        let mut mysql_fsp: Option<u8> = None;
16864        let mut mysql_declared_timestamp = false;
16865        let mut mysql_float_md: Option<(u8, u8)> = None;
16866        let mut ty = match ty_ident.as_str() {
16867            // PG SERIAL family. Implies NOT NULL + AUTO_INCREMENT.
16868            "smallserial" | "serial2" => {
16869                implied_auto_increment = true;
16870                implied_not_null = true;
16871                ColumnTypeName::SmallInt
16872            }
16873            "serial" | "serial4" => {
16874                implied_auto_increment = true;
16875                implied_not_null = true;
16876                ColumnTypeName::Int
16877            }
16878            "bigserial" | "serial8" => {
16879                implied_auto_increment = true;
16880                implied_not_null = true;
16881                ColumnTypeName::BigInt
16882            }
16883            // MySQL flavours we accept by aliasing to the closest SPG
16884            // type. TINYINT covers MySQL's i8 — held inside SMALLINT
16885            // since SPG doesn't have a dedicated i8. MEDIUMINT (MySQL
16886            // 24-bit) → INT. UNSIGNED modifiers are consumed below
16887            // without semantic effect.
16888            // v7.38 (read01 P4.19-sibling) — `int2` / `int4` / `int8` are
16889            // PG's internal type names; pg_dump and hand-written PG schemas
16890            // use them interchangeably with smallint / int / bigint (the cast
16891            // path already accepted them, only the column grammar didn't).
16892            "smallint" | "int2" => {
16893                // v7.14.0 — MySQL display-width on integers
16894                // (`SMALLINT(5)`, `INT(11)`, `BIGINT(20)`). The
16895                // parenthesised number is purely cosmetic — it
16896                // doesn't change storage. Accept + discard.
16897                self.consume_optional_paren_size();
16898                ColumnTypeName::SmallInt
16899            }
16900            // v7.17.0 Phase 4.3 — MySQL `TINYINT(1)` is the
16901            // canonical encoding for BOOLEAN. Every MySQL driver
16902            // (JDBC `tinyInt1isBit=true`, PHP `mysql_field_type`,
16903            // .NET `MySqlConnection`, sqlx) maps it to bit. Pre-
16904            // 4.3 SPG classified TINYINT(1) as SmallInt, which
16905            // gave the customer i16-shaped values where the app
16906            // expected bool — a Tier-A silent type drift on
16907            // mysqldump restores. Now: `TINYINT(1)` → Bool;
16908            // `TINYINT` (no width) and `TINYINT(N)` for N ≠ 1
16909            // stay SmallInt (the legacy width-agnostic path).
16910            "tinyint" => {
16911                let width = self.peek_optional_paren_size_value();
16912                self.consume_optional_paren_size();
16913                if width == Some(1) {
16914                    ColumnTypeName::Bool
16915                } else {
16916                    // v7.39 (round 386, epic P1) — TINYINT is i8; record the
16917                    // lost width so the write path can enforce -128..127.
16918                    if self.mysql_dialect {
16919                        mysql_int_width = Some(MysqlIntWidth::Tiny);
16920                    }
16921                    ColumnTypeName::SmallInt
16922                }
16923            }
16924            "mediumint" => {
16925                self.consume_optional_paren_size();
16926                // v7.39 (round 386, epic P1) — MEDIUMINT is 24-bit; record it.
16927                if self.mysql_dialect {
16928                    mysql_int_width = Some(MysqlIntWidth::Medium);
16929                }
16930                ColumnTypeName::Int
16931            }
16932            "int" | "integer" | "int4" => {
16933                self.consume_optional_paren_size();
16934                ColumnTypeName::Int
16935            }
16936            "bigint" | "int8" => {
16937                self.consume_optional_paren_size();
16938                ColumnTypeName::BigInt
16939            }
16940            // v7.13.0 — `DOUBLE PRECISION` (PG canonical spelling)
16941            // (mailrs round-5 G6). Consume the optional `PRECISION`
16942            // tail when the type keyword was `double` / `DOUBLE`.
16943            //
16944            // v7.39 (round 269) — REAL is 32-bit, not "the same as our
16945            // FLOAT". `FLOAT(p)` picks the width the way PG does:
16946            // p in 1..=24 is real, 25..=53 is double precision, and
16947            // anything else is an error.
16948            "float" | "double" | "real" => {
16949                if ty_ident.eq_ignore_ascii_case("double")
16950                    && matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("precision"))
16951                {
16952                    self.advance();
16953                }
16954                if ty_ident.eq_ignore_ascii_case("real") {
16955                    // v7.39 (round 274) — the two dialects genuinely
16956                    // disagree: PG's REAL is 4-byte, MySQL's REAL is a
16957                    // synonym for DOUBLE (8-byte). Round 269 made REAL
16958                    // 32-bit globally and thereby narrowed the stored
16959                    // precision of every MySQL REAL column.
16960                    if self.mysql_dialect {
16961                        ColumnTypeName::Float
16962                    } else {
16963                        ColumnTypeName::Real
16964                    }
16965                } else if self.mysql_dialect
16966                    && matches!(self.peek(), Token::LParen)
16967                    && self.peek_paren_has_comma()
16968                {
16969                    // v7.39 (round 360) — MySQL's `FLOAT(m,d)` / `DOUBLE(m,d)`
16970                    // display form (`FLOAT(10,2)`), which PG has no
16971                    // equivalent of. It was `syntax error at or near ","`,
16972                    // so the whole CREATE failed.
16973                    //
16974                    // v7.39.2 — the guard said `float` while the comment
16975                    // said both, so `DOUBLE(10,2)` — which every legacy
16976                    // MySQL schema uses for money — still failed the
16977                    // whole CREATE with `syntax error at or near "("`.
16978                    // Measured on 9.7.2: both forms are accepted, and the
16979                    // digits are NOT a display hint, they round on write
16980                    // (3.14159265358979 into either stores 3.14). The
16981                    // rounding is recorded as a residual; accepting the
16982                    // syntax and keeping the width is the half this
16983                    // change makes.
16984                    // v7.39.3 — keep the pair. The digits are not a
16985                    // display hint: MySQL 9.7.2 ROUNDS on write and
16986                    // refuses a value wider than `m` (errno 1264), so a
16987                    // column declared for money held more precision here
16988                    // than its schema said.
16989                    let (m, d) = self.parse_optional_numeric_params()?;
16990                    mysql_float_md = Some((
16991                        u8::try_from(m).unwrap_or(u8::MAX),
16992                        u8::try_from(d.max(0)).unwrap_or(u8::MAX),
16993                    ));
16994                    if ty_ident.eq_ignore_ascii_case("float") {
16995                        ColumnTypeName::Real
16996                    } else {
16997                        ColumnTypeName::Float
16998                    }
16999                } else if ty_ident.eq_ignore_ascii_case("float")
17000                    && matches!(self.peek(), Token::LParen)
17001                {
17002                    // PG words the two bounds differently, and
17003                    // parse_paren_size already rejects a zero.
17004                    let p = self.parse_paren_size("FLOAT")?;
17005                    if p > 53 {
17006                        return Err(self.err(String::from(
17007                            "precision for type float must be less than 54 bits",
17008                        )));
17009                    }
17010                    if p <= 24 {
17011                        ColumnTypeName::Real
17012                    } else {
17013                        ColumnTypeName::Float
17014                    }
17015                } else if ty_ident.eq_ignore_ascii_case("float") && self.mysql_dialect {
17016                    // v7.39.2 — MySQL's bare FLOAT is FOUR bytes; PG's is
17017                    // eight (it is `float8`'s spelling there). SPG used
17018                    // PG's for both, so a MySQL FLOAT column silently
17019                    // kept more precision than MySQL does — measured,
17020                    // 3.14159265358979 comes back as 3.14159 there and
17021                    // came back whole here — and reported itself as
17022                    // `double` to every reflection.
17023                    //
17024                    // This is the mirror of the REAL split above: the
17025                    // two dialects disagree about which spelling means
17026                    // which width, and one of them was already honoured.
17027                    ColumnTypeName::Real
17028                } else {
17029                    ColumnTypeName::Float
17030                }
17031            }
17032            // v7.13.0 — `FLOAT8` (PG short form) maps the same as FLOAT.
17033            "float4" => ColumnTypeName::Real,
17034            "float8" => ColumnTypeName::Float,
17035            "text" => ColumnTypeName::Text,
17036            // v7.39 (round 360) — MySQL's sized TEXT and BLOB families.
17037            // `LONGTEXT`, `BLOB` and `VARBINARY` appear in nearly every
17038            // real MySQL schema and NONE of them existed: the CREATE
17039            // failed outright with `type "blob" does not exist`, so the
17040            // table was never made. The sizes differ only in MySQL's
17041            // maximum length, which SPG does not cap, so they collapse
17042            // onto TEXT and BYTEA the way the unsized spellings do.
17043            "tinytext" | "mediumtext" | "longtext" => ColumnTypeName::Text,
17044            "blob" | "tinyblob" | "mediumblob" | "longblob" => ColumnTypeName::Bytes,
17045            // `VARBINARY(n)` / `BINARY(n)` — a length that SPG does not
17046            // enforce, consumed so the declaration parses.
17047            "varbinary" | "binary" => {
17048                self.consume_optional_paren_size();
17049                ColumnTypeName::Bytes
17050            }
17051            "name" => ColumnTypeName::Name,
17052            "xid" => ColumnTypeName::Xid,
17053            "oid" => ColumnTypeName::Oid,
17054            "xid8" => ColumnTypeName::Xid8,
17055            "bool" | "boolean" => ColumnTypeName::Bool,
17056            // v7.39 (round 620) — an UNBOUNDED `varchar` is the same type as
17057            // an unbounded `character varying`, which the arm below has always
17058            // read as text. Only the short spelling demanded a length, so
17059            // `CREATE TABLE t (x VARCHAR)` — as ordinary a line of DDL as
17060            // there is — failed on `VARCHAR type requires (N)` while the long
17061            // spelling of the same thing was accepted. The same asymmetry
17062            // round 613 closed on the CAST side, here on the DDL side.
17063            "varchar" => {
17064                if matches!(self.peek(), Token::LParen) {
17065                    ColumnTypeName::Varchar(self.parse_paren_size("VARCHAR")?)
17066                } else {
17067                    ColumnTypeName::Text
17068                }
17069            }
17070            // v7.39 (bpchar epic) — bare `char` = char(1), same as bare
17071            // `character` below (SQL standard).
17072            "char" => {
17073                if matches!(self.peek(), Token::LParen) {
17074                    ColumnTypeName::Char(self.parse_paren_size("CHAR")?)
17075                } else {
17076                    ColumnTypeName::Char(1)
17077                }
17078            }
17079            // pg_dump's canonical spellings: `character varying(n)` = varchar,
17080            // `character(n)` = char, bare `character` = char(1). Unbounded
17081            // `character varying` maps to text.
17082            "character" => {
17083                if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("varying")) {
17084                    self.advance();
17085                    if matches!(self.peek(), Token::LParen) {
17086                        ColumnTypeName::Varchar(self.parse_paren_size("VARCHAR")?)
17087                    } else {
17088                        ColumnTypeName::Text
17089                    }
17090                } else if matches!(self.peek(), Token::LParen) {
17091                    ColumnTypeName::Char(self.parse_paren_size("CHAR")?)
17092                } else {
17093                    ColumnTypeName::Char(1)
17094                }
17095            }
17096            "vector" => {
17097                let dim = self.parse_paren_size("VECTOR")?;
17098                let encoding = self.parse_optional_vector_encoding()?;
17099                ColumnTypeName::Vector { dim, encoding }
17100            }
17101            // v7.39 (round 345, M5) — `DECIMAL` and `DEC` are the SQL
17102            // standard's own spellings of NUMERIC, and PG 18.4 accepts both
17103            // (measured: `DECIMAL(10,2)` and `DEC(5,1)` both report as
17104            // `numeric`). Only `NUMERIC` parsed, so `CREATE TABLE t (a
17105            // DECIMAL(10,2))` — how nearly every money column is written,
17106            // in either dialect — was a syntax error and the table was
17107            // never created. `FIXED` is MySQL's alias alone, so it is
17108            // taken only in that dialect.
17109            "numeric" | "decimal" | "dec" => {
17110                let (precision, scale) = self.parse_optional_numeric_params()?;
17111                ColumnTypeName::Numeric(precision, scale)
17112            }
17113            "fixed" if self.mysql_dialect => {
17114                let (precision, scale) = self.parse_optional_numeric_params()?;
17115                ColumnTypeName::Numeric(precision, scale)
17116            }
17117            "date" => ColumnTypeName::Date,
17118            // MySQL's `DATETIME` is the same domain as standard
17119            // `TIMESTAMP` — accept both spellings.
17120            "timestamp" | "datetime" => {
17121                // pg_dump emits `TIMESTAMP(6) WITH TIME ZONE` — the optional
17122                // fractional-seconds precision comes BEFORE the `WITH/WITHOUT
17123                // TIME ZONE` clause, so consume it first.
17124                // v7.39 (round 424) — under MySQL the precision is SEMANTIC
17125                // (it truncates on write and pads on render), so capture it;
17126                // a bare spelling means precision 0 there. PG stores µs always
17127                // and keeps `None`.
17128                let n = self.take_optional_paren_size();
17129                if self.mysql_dialect {
17130                    mysql_fsp = Some(n.unwrap_or(0).min(6));
17131                    // v7.39.2 — remember WHICH spelling was written.
17132                    // MySQL and MariaDB keep `timestamp` and `datetime`
17133                    // apart everywhere a client can read the type back,
17134                    // and SPG reported `datetime` for both — so a dump
17135                    // and reload silently changed the column's declared
17136                    // type, and MySQL's TIMESTAMP is not DATETIME (a
17137                    // different range, and UTC conversion on the way in
17138                    // and out).
17139                    mysql_declared_timestamp = ty_ident.eq_ignore_ascii_case("timestamp");
17140                }
17141                // v7.14.0 — PG canonical `TIMESTAMP WITH TIME ZONE`
17142                // / `TIMESTAMP WITHOUT TIME ZONE`. pg_dump emits
17143                // the full form. SPG canonicalises:
17144                //   - WITH TIME ZONE    → Timestamptz
17145                //   - WITHOUT TIME ZONE → Timestamp
17146                if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("with"))
17147                    && matches!(self.tokens.get(self.pos + 1), Some(Token::Ident(s)) if s.eq_ignore_ascii_case("time"))
17148                    && matches!(self.tokens.get(self.pos + 2), Some(Token::Ident(s)) if s.eq_ignore_ascii_case("zone"))
17149                {
17150                    self.advance(); // WITH
17151                    self.advance(); // TIME
17152                    self.advance(); // ZONE
17153                    ColumnTypeName::Timestamptz
17154                } else if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("without"))
17155                    && matches!(self.tokens.get(self.pos + 1), Some(Token::Ident(s)) if s.eq_ignore_ascii_case("time"))
17156                    && matches!(self.tokens.get(self.pos + 2), Some(Token::Ident(s)) if s.eq_ignore_ascii_case("zone"))
17157                {
17158                    self.advance(); // WITHOUT
17159                    self.advance(); // TIME
17160                    self.advance(); // ZONE
17161                    ColumnTypeName::Timestamp
17162                } else {
17163                    // A second `(precision)` cannot legally follow, but the
17164                    // old grammar tolerated it; keep that tolerance.
17165                    self.consume_optional_paren_size();
17166                    ColumnTypeName::Timestamp
17167                }
17168            }
17169            // v7.9.2 — `TIMESTAMPTZ` and full PG spelling
17170            // `TIMESTAMP WITH TIME ZONE`. Same storage as TIMESTAMP;
17171            // only PG-wire OID differs.
17172            "timestamptz" => {
17173                self.consume_optional_paren_size();
17174                ColumnTypeName::Timestamptz
17175            }
17176            // v4.9: JSON / JSONB. Stored as raw text — no parse-time
17177            // validation. We accept the JSONB spelling too because
17178            // most PG clients default to it; SPG doesn't distinguish
17179            // the two (no path-operator perf advantage to model).
17180            "json" => ColumnTypeName::Json,
17181            "jsonb" => ColumnTypeName::Jsonb,
17182            // v7.10.4 — PG `BYTEA` and the SPG `BYTES` alias both
17183            // surface here. Same storage shape; mapping happens at
17184            // the engine side via the ColumnTypeName → DataType
17185            // resolver. Literal forms are handled at coerce_value
17186            // time so the lexer stays untouched.
17187            "bytea" | "bytes" => ColumnTypeName::Bytes,
17188            // v7.17.0 Phase 7 — PG network address types
17189            // v7.17.0 had a Text-backed fallback here for
17190            // `inet` / `cidr` / `macaddr`. v7.37.5 ζ-A promoted
17191            // each to a first-class type; the keywords are
17192            // bound below in the ζ-A block.
17193            // v7.12.0 — PG full-text search types. mailrs G-CRIT-3.
17194            // The actual `to_tsvector` / `@@` / `ts_rank` surface
17195            // arrives in v7.12.1+; the type itself loads here so
17196            // mailrs's `scripts/init-schema.sql` runs unmodified.
17197            "tsvector" => ColumnTypeName::TsVector,
17198            "tsquery" => ColumnTypeName::TsQuery,
17199            // v7.17.0 — PG `UUID`. Wire OID 2950. The drop-in PG
17200            // surface for Django / Rails / Hibernate's default
17201            // PK pattern.
17202            "uuid" => ColumnTypeName::Uuid,
17203            // v7.37.5 β-P2 — PG `INTERVAL` as a column type.
17204            // Storage = three-field {months, days, micros}, catalog
17205            // tag 34, FILE_VERSION 48+, wire OID 1186. Prior to this
17206            // line `INTERVAL` was parser-rejected at CREATE TABLE.
17207            "interval" => {
17208                // pg_dump emits field-qualified forms like `INTERVAL DAY TO
17209                // SECOND` and an optional `(p)` precision. SPG stores the full
17210                // {months,days,micros}; consume + ignore the qualifier/precision.
17211                while matches!(self.peek(), Token::To)
17212                    || matches!(self.peek(), Token::Ident(s) if matches!(
17213                        s.to_ascii_lowercase().as_str(),
17214                        "year" | "month" | "day" | "hour" | "minute" | "second"
17215                    ))
17216                {
17217                    self.advance();
17218                }
17219                self.consume_optional_paren_size();
17220                ColumnTypeName::Interval
17221            }
17222            // v7.17.0 Phase 3.P0-32 — PG `TIME` (without time zone).
17223            // i64 microseconds since 00:00:00. Wire OID 1083.
17224            // pg_dump emits `TIME(6)` and `TIME(6) WITH TIME ZONE`.
17225            "time" => {
17226                // v7.39 (round 424) — MySQL TIME carries a semantic
17227                // fractional-seconds precision, bare meaning 0.
17228                let n = self.take_optional_paren_size();
17229                if self.mysql_dialect {
17230                    mysql_fsp = Some(n.unwrap_or(0).min(6));
17231                }
17232                if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("with"))
17233                    && matches!(self.tokens.get(self.pos + 1), Some(Token::Ident(s)) if s.eq_ignore_ascii_case("time"))
17234                    && matches!(self.tokens.get(self.pos + 2), Some(Token::Ident(s)) if s.eq_ignore_ascii_case("zone"))
17235                {
17236                    self.advance();
17237                    self.advance();
17238                    self.advance();
17239                    ColumnTypeName::TimeTz
17240                } else if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("without"))
17241                    && matches!(self.tokens.get(self.pos + 1), Some(Token::Ident(s)) if s.eq_ignore_ascii_case("time"))
17242                    && matches!(self.tokens.get(self.pos + 2), Some(Token::Ident(s)) if s.eq_ignore_ascii_case("zone"))
17243                {
17244                    self.advance();
17245                    self.advance();
17246                    self.advance();
17247                    ColumnTypeName::Time
17248                } else {
17249                    ColumnTypeName::Time
17250                }
17251            }
17252            // v7.17.0 Phase 3.P0-33 — MySQL `YEAR`. u16 in
17253            // 1901..=2155 + zero-year sentinel 0. Wire = INT4.
17254            "year" => ColumnTypeName::Year,
17255            // v7.17.0 Phase 3.P0-34 — PG `TIMETZ` / `TIME WITH
17256            // TIME ZONE`. i64 us + i32 offset_secs. Wire OID 1266.
17257            "timetz" => ColumnTypeName::TimeTz,
17258            // v7.17.0 Phase 3.P0-35 — PG `MONEY` — i64 cents.
17259            // Wire OID 790.
17260            "money" => ColumnTypeName::Money,
17261            // v7.17.0 Phase 3.P0-38 — PG range types.
17262            "int4range" => ColumnTypeName::Range(RangeKindAst::Int4),
17263            "int8range" => ColumnTypeName::Range(RangeKindAst::Int8),
17264            "numrange" => ColumnTypeName::Range(RangeKindAst::Num),
17265            "tsrange" => ColumnTypeName::Range(RangeKindAst::Ts),
17266            "tstzrange" => ColumnTypeName::Range(RangeKindAst::TsTz),
17267            "daterange" => ColumnTypeName::Range(RangeKindAst::Date),
17268            // v7.37.5 δ — PG 14+ multirange keywords.
17269            "int4multirange" => ColumnTypeName::Multirange(RangeKindAst::Int4),
17270            "int8multirange" => ColumnTypeName::Multirange(RangeKindAst::Int8),
17271            "nummultirange" => ColumnTypeName::Multirange(RangeKindAst::Num),
17272            "tsmultirange" => ColumnTypeName::Multirange(RangeKindAst::Ts),
17273            "tstzmultirange" => ColumnTypeName::Multirange(RangeKindAst::TsTz),
17274            "datemultirange" => ColumnTypeName::Multirange(RangeKindAst::Date),
17275            // v7.37.5 ε — PG geometry scalar keywords.
17276            "point" => ColumnTypeName::Point,
17277            "lseg" => ColumnTypeName::Lseg,
17278            "path" => ColumnTypeName::Path,
17279            "box" => ColumnTypeName::PgBox,
17280            "polygon" => ColumnTypeName::Polygon,
17281            "line" => ColumnTypeName::Line,
17282            "circle" => ColumnTypeName::Circle,
17283            // v7.37.5 ζ-A — network / bit / xml / "char" keywords.
17284            "inet" => ColumnTypeName::Inet,
17285            "cidr" => ColumnTypeName::Cidr,
17286            "macaddr" => ColumnTypeName::Macaddr,
17287            "macaddr8" => ColumnTypeName::Macaddr8,
17288            // `bit`, `bit(N)`, `bit varying`, `bit varying(N)`. SPG carries the
17289            // width in the value, so the optional `(N)` typmod is accepted and
17290            // ignored (the column stores whatever width it's given).
17291            "bit" => {
17292                let varying = matches!(
17293                    self.peek(),
17294                    Token::Ident(k) if k.eq_ignore_ascii_case("varying")
17295                );
17296                if varying {
17297                    self.advance();
17298                }
17299                // v7.39 (round 281) — the length used to be parsed and
17300                // dropped, so `bit(3)` accepted a five-bit string.
17301                let n = if matches!(self.peek(), Token::LParen) {
17302                    self.parse_paren_size("BIT")?
17303                } else {
17304                    0
17305                };
17306                if varying {
17307                    ColumnTypeName::BitVarying(n)
17308                } else {
17309                    ColumnTypeName::Bit(n)
17310                }
17311            }
17312            "varbit" => {
17313                let n = if matches!(self.peek(), Token::LParen) {
17314                    self.parse_paren_size("VARBIT")?
17315                } else {
17316                    0
17317                };
17318                ColumnTypeName::BitVarying(n)
17319            }
17320            "xml" => ColumnTypeName::Xml,
17321            // v7.17.0 Phase 3.P0-39 — PG hstore extension type.
17322            "hstore" => ColumnTypeName::Hstore,
17323            // v7.17.0 Phase 3.P0-36 — MySQL inline ENUM
17324            // `ENUM('a','b','c')`. Storage is TEXT; the value
17325            // list lands on `inline_enum_variants` for the
17326            // engine to validate INSERT cells against. Empty
17327            // value list is a parse error (matches MySQL).
17328            "enum" => {
17329                // Expect the opening `(`.
17330                if !matches!(self.peek(), Token::LParen) {
17331                    return Err(self.err(alloc::format!(
17332                        "expected '(' after ENUM, got {:?}",
17333                        self.peek()
17334                    )));
17335                }
17336                self.advance();
17337                let mut variants: Vec<String> = Vec::new();
17338                loop {
17339                    match self.advance() {
17340                        Token::String(s) => variants.push(s),
17341                        other => {
17342                            return Err(self.err(alloc::format!(
17343                                "ENUM(...) expects string literal variants, got {other:?}"
17344                            )));
17345                        }
17346                    }
17347                    match self.peek() {
17348                        Token::Comma => {
17349                            self.advance();
17350                            continue;
17351                        }
17352                        Token::RParen => {
17353                            self.advance();
17354                            break;
17355                        }
17356                        other => {
17357                            return Err(self.err(alloc::format!(
17358                                "expected ',' or ')' in ENUM(...), got {other:?}"
17359                            )));
17360                        }
17361                    }
17362                }
17363                if variants.is_empty() {
17364                    return Err(self.err("ENUM(...) must declare at least one variant".into()));
17365                }
17366                inline_enum_variants = Some(variants);
17367                // Storage is plain TEXT; the variant list lives on
17368                // the ColumnSchema side.
17369                ColumnTypeName::Text
17370            }
17371            // v7.17.0 Phase 3.P0-37 — MySQL inline SET
17372            // `SET('a','b','c')`. Same parse shape as ENUM;
17373            // semantics differ (subset rather than pick-one).
17374            "set" => {
17375                if !matches!(self.peek(), Token::LParen) {
17376                    return Err(self.err(alloc::format!(
17377                        "expected '(' after SET, got {:?}",
17378                        self.peek()
17379                    )));
17380                }
17381                self.advance();
17382                let mut variants: Vec<String> = Vec::new();
17383                loop {
17384                    match self.advance() {
17385                        Token::String(s) => variants.push(s),
17386                        other => {
17387                            return Err(self.err(alloc::format!(
17388                                "SET(...) expects string literal variants, got {other:?}"
17389                            )));
17390                        }
17391                    }
17392                    match self.peek() {
17393                        Token::Comma => {
17394                            self.advance();
17395                            continue;
17396                        }
17397                        Token::RParen => {
17398                            self.advance();
17399                            break;
17400                        }
17401                        other => {
17402                            return Err(self.err(alloc::format!(
17403                                "expected ',' or ')' in SET(...), got {other:?}"
17404                            )));
17405                        }
17406                    }
17407                }
17408                if variants.is_empty() {
17409                    return Err(self.err("SET(...) must declare at least one variant".into()));
17410                }
17411                inline_set_variants = Some(variants);
17412                ColumnTypeName::Text
17413            }
17414            _other => {
17415                // v7.17.0 Phase 1.4 — unknown ident → defer
17416                // resolution to the engine. Stored as Text in
17417                // ColumnTypeName + the original name carried as
17418                // `user_type_ref` so CREATE TABLE can look up
17419                // user-defined enum / domain types.
17420                user_type_ref = Some(ty_ident.clone());
17421                ColumnTypeName::Text
17422            }
17423        };
17424        // v7.17.0 Phase 4.4 — MySQL's `UNSIGNED` modifier sits
17425        // right after the type keyword. Pre-4.4 SPG consumed +
17426        // discarded the keyword, leaving a customer column
17427        // declared `id INT UNSIGNED NOT NULL` silently accepting
17428        // negative values — a Tier-A correctness drift where
17429        // application invariants (auto-increment-IDs never
17430        // negative) silently broke on cutover. Now: capture as
17431        // a column flag, persist on the schema, enforce at
17432        // INSERT / UPDATE time.
17433        let is_unsigned = if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("unsigned"))
17434        {
17435            self.advance();
17436            true
17437        } else {
17438            false
17439        };
17440        // v7.14.0 — mysqldump emits `<type> CHARACTER SET <name>` and
17441        // `<type> COLLATE <name>` post-fixes on text columns. SPG
17442        // stores text as UTF-8 always so CHARACTER SET is still a
17443        // no-op. v7.17.0 Phase 2.5 — COLLATE no longer drops the
17444        // name: it gets classified into a `Collation` variant the
17445        // engine consults at WHERE-eval time. PG `default` /
17446        // `pg_catalog.default` / `C` / `POSIX` collations all
17447        // resolve to `Binary` (the prior behaviour); `_ci` /
17448        // `case_insensitive` / `nocase` shift to CaseInsensitive.
17449        // The schema-qualifier form (`pg_catalog.default`) lexes
17450        // as `Ident '.' Ident` — peek for the `.` and consume both
17451        // halves so it's treated as one collation name. PG's
17452        // `IDENT.IDENT` collation form (which can appear here) is
17453        // resolved by Collation::from_collation_name on the bare
17454        // identifier after the dot.
17455        let mut collation = Collation::Binary;
17456        // v7.39 (round 370, M4 P4a) — whether an explicit `COLLATE <name>`
17457        // clause was written. The engine needs this to tell an explicit
17458        // `COLLATE utf8mb4_bin` (byte-wise) apart from a column with no
17459        // clause at all: both resolve to `Collation::Binary`, but under the
17460        // MySQL dialect the latter takes the folding default collation.
17461        let mut collation_explicit = false;
17462        let mut collation_name: Option<alloc::string::String> = None;
17463        loop {
17464            if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("character"))
17465                && matches!(self.tokens.get(self.pos + 1), Some(Token::Ident(s)) if s.eq_ignore_ascii_case("set"))
17466            {
17467                self.advance(); // CHARACTER
17468                self.advance(); // SET
17469                if matches!(
17470                    self.peek(),
17471                    Token::Ident(_) | Token::QuotedIdent(_) | Token::String(_)
17472                ) {
17473                    self.advance();
17474                }
17475                continue;
17476            }
17477            if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("collate")) {
17478                self.advance(); // COLLATE
17479                // Accept Ident / QuotedIdent / String AND the
17480                // keyword-tokenised `Default` (PG `pg_catalog.default`
17481                // and bare `DEFAULT` collation names — `default` is a
17482                // reserved word so the lexer hands back Token::Default
17483                // not Token::Ident).
17484                let read_collation_atom = |this: &mut Self| -> Option<alloc::string::String> {
17485                    match this.peek().clone() {
17486                        Token::Ident(s) | Token::QuotedIdent(s) | Token::String(s) => {
17487                            this.advance();
17488                            Some(s)
17489                        }
17490                        Token::Default => {
17491                            this.advance();
17492                            Some(alloc::string::String::from("default"))
17493                        }
17494                        _ => None,
17495                    }
17496                };
17497                let raw = if let Some(head) = read_collation_atom(self) {
17498                    // Schema-qualified PG form: `pg_catalog.default`.
17499                    if matches!(self.peek(), Token::Dot) {
17500                        self.advance();
17501                        let tail = read_collation_atom(self).unwrap_or_default();
17502                        alloc::format!("{head}.{tail}")
17503                    } else {
17504                        head
17505                    }
17506                } else {
17507                    alloc::string::String::new()
17508                };
17509                if !raw.is_empty() {
17510                    collation_explicit = true;
17511                    // v7.39 (round 676) — keep the name too. The enum below
17512                    // folds C / POSIX / en_US / default into one value, and
17513                    // `pg_attribute.attcollation` has to tell them apart.
17514                    // The schema qualifier goes: PG's `pg_catalog.default`
17515                    // and a bare `default` name the same collation.
17516                    // v7.39 (round 679) — strip a SCHEMA qualifier, not an
17517                    // encoding suffix. Round 676 used `rsplit('.')` for
17518                    // both, and `COLLATE "en_US.utf8"` came out as `utf8`:
17519                    // PG writes `pg_catalog.default` (qualifier) and
17520                    // `en_US.utf8` (locale + encoding) with the same
17521                    // separator. Only `pg_catalog.` is a qualifier, and it
17522                    // is the only one PG's own dumps emit.
17523                    let bare = raw.trim_matches(|c: char| c == '"' || c == '\'');
17524                    let bare = bare.strip_prefix("pg_catalog.").unwrap_or(bare);
17525                    collation_name = Some(alloc::string::String::from(bare));
17526                    let parsed = Collation::from_collation_name(&raw);
17527                    // Last COLLATE clause wins, but `Binary` from a
17528                    // bare keyword like `default` should not
17529                    // silently downgrade a stronger one set earlier
17530                    // on the same column. v7.17 only ships one
17531                    // non-Binary variant so a simple OR is enough.
17532                    if parsed != Collation::Binary {
17533                        collation = parsed;
17534                    }
17535                }
17536                continue;
17537            }
17538            break;
17539        }
17540        // v7.10.10 — postfix `[]` widens the base type to its array
17541        // type. PG accepts `TYPE[]` after any base type and so does
17542        // SPG now (round-753 probe: INT[] / NUMERIC[] / TIMESTAMP[]
17543        // all through; the old "only TEXT[]" note was stale).
17544        if matches!(self.peek(), Token::LBracket) {
17545            self.advance();
17546            if !matches!(self.peek(), Token::RBracket) {
17547                return Err(self.err(alloc::format!(
17548                    "TEXT[] takes no dimension; got {:?}",
17549                    self.peek()
17550                )));
17551            }
17552            self.advance();
17553            // v7.11.13 — widened to INT[] and BIGINT[] in addition
17554            // to TEXT[]. Other base types (BOOL[], NUMERIC[], etc.)
17555            // still error here.
17556            ty = match ty {
17557                ColumnTypeName::Text => ColumnTypeName::TextArray,
17558                ColumnTypeName::Int => ColumnTypeName::IntArray,
17559                ColumnTypeName::BigInt => ColumnTypeName::BigIntArray,
17560                // v7.37.5 β-P4 — INTERVAL[] via the same postfix
17561                // `[]` grammar. Wire OID 1187.
17562                ColumnTypeName::Interval => ColumnTypeName::IntervalArray,
17563                // v7.37.5 γ — full PG array-of-scalar family.
17564                ColumnTypeName::Bool => ColumnTypeName::BoolArray,
17565                ColumnTypeName::SmallInt => ColumnTypeName::SmallIntArray,
17566                ColumnTypeName::Float => ColumnTypeName::FloatArray,
17567                // NUMERIC(p, s) loses its precision params at the
17568                // array level (matches PG: `NUMERIC[]` is untyped,
17569                // per-element precision flows through values).
17570                ColumnTypeName::Numeric(_, _) => ColumnTypeName::NumericArray,
17571                ColumnTypeName::Date => ColumnTypeName::DateArray,
17572                ColumnTypeName::Timestamp => ColumnTypeName::TimestampArray,
17573                ColumnTypeName::Timestamptz => ColumnTypeName::TimestamptzArray,
17574                ColumnTypeName::Uuid => ColumnTypeName::UuidArray,
17575                ColumnTypeName::Json => ColumnTypeName::JsonArray,
17576                ColumnTypeName::Jsonb => ColumnTypeName::JsonbArray,
17577                ColumnTypeName::Bytes => ColumnTypeName::BytesArray,
17578                // VARCHAR(n)[] / CHAR(n)[] drop the length cap at
17579                // the array level (matches PG semantics where the
17580                // element precision is per-row, not column-wide).
17581                ColumnTypeName::Varchar(_) => ColumnTypeName::VarcharArray,
17582                ColumnTypeName::Char(_) => ColumnTypeName::CharArray,
17583                // v7.37.5 ζ-A — MONEY[] (OID 791) ship-triage
17584                // follow-up.
17585                ColumnTypeName::Money => ColumnTypeName::MoneyArray,
17586                other => {
17587                    return Err(self.err(alloc::format!("{other:?}[] not yet supported")));
17588                }
17589            };
17590            // v7.17.0 Phase 3.P0-40 — second `[]` widens 1D → 2D
17591            // for INT/TEXT/BIGINT. Anything else is an error.
17592            if matches!(self.peek(), Token::LBracket) {
17593                self.advance();
17594                if !matches!(self.peek(), Token::RBracket) {
17595                    return Err(self.err(alloc::format!(
17596                        "TYPE[][] second dimension takes no size; got {:?}",
17597                        self.peek()
17598                    )));
17599                }
17600                self.advance();
17601                ty = match ty {
17602                    ColumnTypeName::IntArray => ColumnTypeName::IntArray2D,
17603                    ColumnTypeName::BigIntArray => ColumnTypeName::BigIntArray2D,
17604                    ColumnTypeName::TextArray => ColumnTypeName::TextArray2D,
17605                    // v7.39 (read01 round 75) — bool[][].
17606                    ColumnTypeName::BoolArray => ColumnTypeName::BoolArray2D,
17607                    other => {
17608                        return Err(self.err(alloc::format!(
17609                            "v7.17 2D arrays support INT[][] / BIGINT[][] / \
17610                             TEXT[][] only; got {other:?}"
17611                        )));
17612                    }
17613                };
17614            }
17615        }
17616        Ok((
17617            ty,
17618            implied_auto_increment,
17619            implied_not_null,
17620            user_type_ref,
17621            collation,
17622            collation_explicit,
17623            collation_name,
17624            is_unsigned,
17625            inline_enum_variants,
17626            inline_set_variants,
17627            mysql_int_width,
17628            mysql_fsp,
17629            mysql_declared_timestamp,
17630            mysql_float_md,
17631        ))
17632    }
17633
17634    fn parse_column_def(&mut self) -> Result<ColumnDef, ParseError> {
17635        // v7.20 — PG reserves the table-constraint keywords, so a
17636        // BARE `UNIQUE` / `PRIMARY` / … in column position is a
17637        // malformed constraint clause (e.g. `UNIQUE a` missing its
17638        // parens), not a column named "unique". Since v7.17's
17639        // unknown-type leniency (`user_type_ref`) such a clause
17640        // would otherwise parse as a column with a user-defined
17641        // type — silently accepting invalid DDL. Quoted
17642        // identifiers ("unique" / `unique`) remain valid names.
17643        if let Token::Ident(s) = self.peek()
17644            && [
17645                "unique",
17646                "primary",
17647                "foreign",
17648                "constraint",
17649                "check",
17650                "references",
17651                "exclude",
17652            ]
17653            .iter()
17654            .any(|kw| s.eq_ignore_ascii_case(kw))
17655        {
17656            return Err(self.err(alloc::format!(
17657                "unexpected reserved keyword '{s}' at start of column definition \
17658                 (malformed table constraint?)"
17659            )));
17660        }
17661        let name_tok = self.pos;
17662        let name = self.expect_ident_like()?;
17663        // v7.39.3 — MySQL 9.7.2 reports a column by the SPELLING it was
17664        // declared with: `MyCol` stays `MyCol` in SHOW COLUMNS, in
17665        // information_schema, and in SHOW CREATE (measured). SPG folded
17666        // an unquoted name, so a table restored from a dump reported
17667        // names the application had never written.
17668        //
17669        // The written form comes back from the source span, which only
17670        // the MySQL dialect keeps. The span runs to the START of the
17671        // next token, so a comment or unusual spacing between them
17672        // arrives with it — hence the check that what came back is the
17673        // same identifier. It is not decoration: without it,
17674        // `CREATE TABLE t (MyCol /* c */ INT)` names the column
17675        // `MyCol /* c */`.
17676        let name = self
17677            .source_span(name_tok, name_tok)
17678            .map(|raw| raw.trim().trim_matches('`').trim_matches('"'))
17679            .filter(|raw| raw.eq_ignore_ascii_case(&name))
17680            .map_or(name, alloc::string::String::from);
17681        let (
17682            ty,
17683            implied_auto_increment,
17684            implied_not_null,
17685            user_type_ref,
17686            collation,
17687            collation_explicit,
17688            collation_name,
17689            is_unsigned,
17690            inline_enum_variants,
17691            inline_set_variants,
17692            mysql_int_width,
17693            mysql_fsp,
17694            mysql_declared_timestamp,
17695            mysql_float_md,
17696        ) = self.parse_type_with_implied_flags()?;
17697        // Column constraints: `DEFAULT <expr>`, `NOT NULL`, and the
17698        // MySQL-flavoured `AUTO_INCREMENT` may appear in any order;
17699        // each at most once.
17700        let mut default: Option<Expr> = None;
17701        let mut nullable = !implied_not_null;
17702        let mut nullability_seen = implied_not_null;
17703        let mut auto_increment = implied_auto_increment;
17704        let mut is_primary_key = false;
17705        let mut is_unique = false;
17706        let mut unique_nulls_not_distinct = false;
17707        let mut constraint_deferrable = false;
17708        let mut constraint_initially_deferred = false;
17709        let mut check: Option<Expr> = None;
17710        let mut on_update_runtime: Option<Expr> = None;
17711        let mut generated_stored_expr: Option<Box<Expr>> = None;
17712        let mut identity_always = false;
17713        loop {
17714            // v7.22 (mailrs round-13 gap 3) — PG 18 catalogs
17715            // not-null constraints by name and pg_dump emits them
17716            // inline: `id bigint CONSTRAINT contacts_id_not_null1
17717            // NOT NULL`. Accept and discard the name; whatever
17718            // constraint follows is parsed by the arms below.
17719            if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("constraint")) {
17720                // v7.39 (round 308, V29) — a name on an inline
17721                // REFERENCES belongs to the FOREIGN KEY, and the caller
17722                // (`parse_column_def_with_fk`) is what builds it, so
17723                // leave the whole clause for it. Dropping the name here
17724                // is what made `CONSTRAINT fk_a REFERENCES …` come back
17725                // as the synthesised `c_pid_fkey` — which then could
17726                // not be matched by `SET CONSTRAINTS fk_a`. Peek only:
17727                // `advance()` takes tokens by `mem::replace`, so there
17728                // is no rewinding once consumed.
17729                if matches!(
17730                    self.tokens.get(self.pos + 2),
17731                    Some(Token::Ident(s)) if s.eq_ignore_ascii_case("references")
17732                ) {
17733                    break;
17734                }
17735                self.advance();
17736                let _name = self.expect_ident_like()?;
17737                continue;
17738            }
17739            // v7.39 (round 379) — MySQL's SHORT generated-column form
17740            // omits `GENERATED ALWAYS`: `<col> <type> AS (<expr>)
17741            // [STORED | VIRTUAL]`. mysqldump emits the long form (handled
17742            // below), but hand-written schemas and app migrations use this.
17743            // STORED / VIRTUAL is optional (MySQL defaults to VIRTUAL);
17744            // SPG computes-and-stores either way, like the long form.
17745            if matches!(self.peek(), Token::As) {
17746                self.advance();
17747                if !matches!(self.peek(), Token::LParen) {
17748                    return Err(self.err(alloc::format!(
17749                        "expected '(' after AS in a generated column, got {:?}",
17750                        self.peek()
17751                    )));
17752                }
17753                self.advance();
17754                let expr = self.parse_expr(0)?;
17755                if !matches!(self.peek(), Token::RParen) {
17756                    return Err(self.err(alloc::format!(
17757                        "expected ')' after AS (<expr>), got {:?}",
17758                        self.peek()
17759                    )));
17760                }
17761                self.advance();
17762                if matches!(self.peek(), Token::Ident(s)
17763                    if s.eq_ignore_ascii_case("stored") || s.eq_ignore_ascii_case("virtual"))
17764                {
17765                    self.advance();
17766                }
17767                generated_stored_expr = Some(alloc::boxed::Box::new(expr));
17768                continue;
17769            }
17770            // v7.22 (round-13 T2) — inline `GENERATED { ALWAYS |
17771            // BY DEFAULT } AS IDENTITY [(seq options)]` (PG 10+;
17772            // the modern replacement for SERIAL in hand-written
17773            // schemas). Both flavours map onto the auto-increment
17774            // machinery — SPG's serial semantics ≈ BY DEFAULT;
17775            // ALWAYS's reject-explicit-values nuance is documented
17776            // leniency. Generated EXPRESSION columns
17777            // (`AS (expr) STORED`) are not supported: error loudly
17778            // instead of silently storing NULLs.
17779            if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("generated")) {
17780                self.advance();
17781                let mut saw_generated_always = false;
17782                match self.peek().clone() {
17783                    Token::Ident(s) if s.eq_ignore_ascii_case("always") => {
17784                        self.advance();
17785                        saw_generated_always = true;
17786                    }
17787                    Token::Ident(b) | Token::QuotedIdent(b) if b.eq_ignore_ascii_case("by") => {
17788                        self.advance();
17789                        if !matches!(self.peek(), Token::Default) {
17790                            return Err(self.err(alloc::format!(
17791                                "expected DEFAULT after GENERATED BY, got {:?}",
17792                                self.peek()
17793                            )));
17794                        }
17795                        self.advance();
17796                    }
17797                    other => {
17798                        return Err(self.err(alloc::format!(
17799                            "expected ALWAYS or BY DEFAULT after GENERATED, got {other:?}"
17800                        )));
17801                    }
17802                }
17803                if !matches!(self.peek(), Token::As) {
17804                    return Err(self.err(alloc::format!(
17805                        "expected AS after GENERATED ALWAYS/BY DEFAULT, got {:?}",
17806                        self.peek()
17807                    )));
17808                }
17809                self.advance();
17810                // v7.37.7(sentori Epic 3 P1)— `GENERATED ALWAYS AS
17811                // ( <expr> ) STORED` stored computed-column. The
17812                // expression is captured for the engine to recompute
17813                // on every INSERT / UPDATE. v7.37.7 accepts the
17814                // STORED keyword only; PG also has VIRTUAL, which
17815                // v7.37.7 carves out (sentori only uses STORED).
17816                if matches!(self.peek(), Token::LParen) {
17817                    self.advance();
17818                    let expr = self.parse_expr(0)?;
17819                    if !matches!(self.peek(), Token::RParen) {
17820                        return Err(self.err(alloc::format!(
17821                            "expected ')' after GENERATED ALWAYS AS (<expr>), got {:?}",
17822                            self.peek()
17823                        )));
17824                    }
17825                    self.advance();
17826                    let stored = match self.peek() {
17827                        Token::Ident(s) | Token::QuotedIdent(s)
17828                            if s.eq_ignore_ascii_case("stored") =>
17829                        {
17830                            self.advance();
17831                            true
17832                        }
17833                        // v7.38 (read01 P4.14) — accept PG 18's VIRTUAL
17834                        // generated columns. SPG computes them on write and
17835                        // persists like STORED; the two are observably
17836                        // identical for query results (the value, recompute
17837                        // on base-column change, and NOT NULL enforcement all
17838                        // match), so a PG 18 schema/dump using VIRTUAL loads
17839                        // and behaves correctly. The compute-on-read storage
17840                        // saving is an invisible internal difference.
17841                        Token::Ident(s) | Token::QuotedIdent(s)
17842                            if s.eq_ignore_ascii_case("virtual") =>
17843                        {
17844                            self.advance();
17845                            false
17846                        }
17847                        other => {
17848                            return Err(self.err(alloc::format!(
17849                                "expected STORED or VIRTUAL after GENERATED ALWAYS AS (<expr>), \
17850                                 got {other:?}"
17851                            )));
17852                        }
17853                    };
17854                    let _ = stored; // STORED / VIRTUAL both compute-and-store.
17855                    generated_stored_expr = Some(Box::new(expr));
17856                    continue;
17857                }
17858                self.expect_keyword_ident("identity")?;
17859                // Optional `(START WITH 1 INCREMENT BY 1 …)` —
17860                // consume the balanced parens and discard (SPG's
17861                // auto-increment is max+1-scan based).
17862                if matches!(self.peek(), Token::LParen) {
17863                    let mut depth = 0usize;
17864                    loop {
17865                        match self.advance() {
17866                            Token::LParen => depth += 1,
17867                            Token::RParen => {
17868                                depth -= 1;
17869                                if depth == 0 {
17870                                    break;
17871                                }
17872                            }
17873                            Token::Eof => {
17874                                return Err(self.err(
17875                                    "unterminated sequence-options parens after IDENTITY".into(),
17876                                ));
17877                            }
17878                            _ => {}
17879                        }
17880                    }
17881                }
17882                auto_increment = true;
17883                // v7.38 (read01) — remember the ALWAYS flavour so the engine
17884                // can reject explicit non-DEFAULT INSERT values (unless
17885                // OVERRIDING SYSTEM VALUE) the way PG does.
17886                identity_always = saw_generated_always;
17887                // PG identity columns are implicitly NOT NULL.
17888                nullable = false;
17889                continue;
17890            }
17891            // v7.17.0 Phase 2.1 — MySQL `ON UPDATE
17892            // CURRENT_TIMESTAMP[(N)]`. Only CURRENT_TIMESTAMP
17893            // is accepted today. The "ON" token is an Ident
17894            // (not reserved) — peek before consuming.
17895            if matches!(self.peek(), Token::On)
17896                && matches!(self.tokens.get(self.pos + 1), Some(Token::Ident(s)) if s.eq_ignore_ascii_case("update"))
17897            {
17898                self.advance(); // ON
17899                self.advance(); // update
17900                // Accept CURRENT_TIMESTAMP / CURRENT_TIMESTAMP(N).
17901                let next = self.peek().clone();
17902                match next {
17903                    Token::Ident(s) | Token::QuotedIdent(s)
17904                        if s.eq_ignore_ascii_case("current_timestamp") =>
17905                    {
17906                        self.advance();
17907                        // Optional `(N)` precision.
17908                        if matches!(self.peek(), Token::LParen) {
17909                            self.advance();
17910                            if !matches!(self.peek(), Token::Integer(_)) {
17911                                return Err(self.err(alloc::format!(
17912                                    "expected integer precision inside CURRENT_TIMESTAMP(…), got {:?}",
17913                                    self.peek()
17914                                )));
17915                            }
17916                            self.advance();
17917                            if !matches!(self.peek(), Token::RParen) {
17918                                return Err(self.err(alloc::format!(
17919                                    "expected ')' after CURRENT_TIMESTAMP precision, got {:?}",
17920                                    self.peek()
17921                                )));
17922                            }
17923                            self.advance();
17924                        }
17925                        on_update_runtime = Some(Expr::FunctionCall {
17926                            name: "now".into(),
17927                            args: Vec::new(),
17928                        });
17929                        continue;
17930                    }
17931                    other => {
17932                        return Err(self.err(alloc::format!(
17933                            "v7.17 only supports ON UPDATE CURRENT_TIMESTAMP, got {other:?}"
17934                        )));
17935                    }
17936                }
17937            }
17938            if matches!(self.peek(), Token::Default) {
17939                if default.is_some() {
17940                    return Err(self.err("DEFAULT specified twice".into()));
17941                }
17942                self.advance();
17943                default = Some(self.parse_expr(0)?);
17944                continue;
17945            }
17946            // v7.39 (round 621) — `NOT DEFERRABLE` shares this arm's leading
17947            // token with NOT NULL and sits EARLIER in the loop than the
17948            // deferrability arm, so without the lookahead it was reported as
17949            // "NOT NULL specified twice" (or "expected NULL after NOT").
17950            if matches!(self.peek(), Token::Not)
17951                && matches!(self.tokens.get(self.pos + 1), Some(Token::Ident(s)) if s.eq_ignore_ascii_case("deferrable"))
17952            {
17953                // NOT DEFERRABLE — explicit immediate; nothing to carry.
17954                self.consume_optional_deferrable_clauses()?;
17955                continue;
17956            }
17957            if matches!(self.peek(), Token::Not) {
17958                if nullability_seen {
17959                    return Err(self.err("NOT NULL specified twice".into()));
17960                }
17961                self.advance();
17962                if !matches!(self.peek(), Token::Null) {
17963                    return Err(self.err(format!(
17964                        "expected NULL after NOT in column def, got {:?}",
17965                        self.peek()
17966                    )));
17967                }
17968                self.advance();
17969                nullable = false;
17970                nullability_seen = true;
17971                continue;
17972            }
17973            // v7.14.0 — MySQL accepts a bare `NULL` as an explicit
17974            // "this column is nullable" marker (the default in
17975            // standard SQL anyway). mysqldump emits it routinely
17976            // (`col TYPE NULL DEFAULT NULL` for nullable
17977            // timestamps etc). Accept + no-op.
17978            if matches!(self.peek(), Token::Null) {
17979                if nullability_seen && !nullable {
17980                    // v7.39 (round 761, F31 tranche 2 #31) — PG's
17981                    // sentence, PG18-measured (the table name is the
17982                    // caller's; the column half is exact).
17983                    return Err(self.err(alloc::format!(
17984                        "conflicting NULL/NOT NULL declarations for column \"{name}\""
17985                    )));
17986                }
17987                self.advance();
17988                nullable = true;
17989                nullability_seen = true;
17990                continue;
17991            }
17992            // `AUTO_INCREMENT` or its abbreviated form `AUTOINCREMENT`
17993            // arrives as a bare Ident. Match either, case-insensitive.
17994            if let Token::Ident(s) = self.peek()
17995                && (s.eq_ignore_ascii_case("auto_increment")
17996                    || s.eq_ignore_ascii_case("autoincrement"))
17997            {
17998                if auto_increment {
17999                    return Err(self.err("AUTO_INCREMENT specified twice".into()));
18000                }
18001                self.advance();
18002                auto_increment = true;
18003                continue;
18004            }
18005            // v7.9.13 — inline `PRIMARY KEY` column constraint
18006            // (mailrs F1). Implies `NOT NULL`. The engine creates
18007            // a BTree index for the PK column at CREATE TABLE time
18008            // so FK parent-side index lookups resolve.
18009            // v7.39 (round 621) — `[NOT] DEFERRABLE [INITIALLY {DEFERRED |
18010            // IMMEDIATE}]` after an inline PK / UNIQUE / REFERENCES. Every
18011            // spelling was a parse error, so a pg_dump carrying one stopped
18012            // mid-restore. The clauses are consumed by the same helper the FK
18013            // path has used since round 288 and recorded nowhere: SPG enforces
18014            // the constraint IMMEDIATELY either way, which fails earlier than
18015            // PG inside a transaction that violates-then-repairs — a refusal,
18016            // not a wrong answer. True deferral is the open remainder of F08.
18017            if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("deferrable") || s.eq_ignore_ascii_case("initially"))
18018                || (matches!(self.peek(), Token::Not)
18019                    && matches!(self.tokens.get(self.pos + 1), Some(Token::Ident(s)) if s.eq_ignore_ascii_case("deferrable")))
18020            {
18021                // v7.39 (round 711) — CARRIED now (the storing half of
18022                // F08); round 621 only consumed.
18023                let (d, idef) = self.consume_deferrable_clauses_timed()?;
18024                constraint_deferrable |= d;
18025                constraint_initially_deferred |= idef;
18026                continue;
18027            }
18028            if let Token::Ident(s) = self.peek()
18029                && s.eq_ignore_ascii_case("primary")
18030            {
18031                if is_primary_key {
18032                    return Err(self.err("PRIMARY KEY specified twice".into()));
18033                }
18034                // Peek-ahead for the required `KEY` token.
18035                let next = self.tokens.get(self.pos + 1);
18036                let next_is_key = matches!(
18037                    next,
18038                    Some(Token::Ident(k)) if k.eq_ignore_ascii_case("key")
18039                );
18040                if !next_is_key {
18041                    return Err(self.err(format!(
18042                        "expected KEY after PRIMARY in column def, got {:?}",
18043                        next
18044                    )));
18045                }
18046                self.advance(); // PRIMARY
18047                self.advance(); // KEY
18048                is_primary_key = true;
18049                if nullability_seen && nullable {
18050                    return Err(self.err(
18051                        "column declared NULL but inline PRIMARY KEY implies NOT NULL".into(),
18052                    ));
18053                }
18054                nullable = false;
18055                nullability_seen = true;
18056                continue;
18057            }
18058            // v7.13.0 — inline `UNIQUE` column constraint
18059            // (mailrs round-5 G2). Fold into a single-column
18060            // table-level UNIQUE at CREATE TABLE post-process time.
18061            if let Token::Ident(s) = self.peek()
18062                && s.eq_ignore_ascii_case("unique")
18063            {
18064                if is_unique {
18065                    return Err(self.err("UNIQUE specified twice".into()));
18066                }
18067                self.advance();
18068                is_unique = true;
18069                // v7.38 (read01 P4.19) — optional `NULLS [NOT] DISTINCT`
18070                // (PG 15+); default is NULLS DISTINCT per the SQL standard.
18071                if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("nulls")) {
18072                    let n1 = self.tokens.get(self.pos + 1);
18073                    let n2 = self.tokens.get(self.pos + 2);
18074                    if matches!(n1, Some(Token::Not)) && matches!(n2, Some(Token::Distinct)) {
18075                        self.advance(); // NULLS
18076                        self.advance(); // NOT
18077                        self.advance(); // DISTINCT
18078                        unique_nulls_not_distinct = true;
18079                    } else if matches!(n1, Some(Token::Distinct)) {
18080                        self.advance(); // NULLS
18081                        self.advance(); // DISTINCT
18082                    }
18083                }
18084                continue;
18085            }
18086            // v7.13.0 — inline `CHECK (<expr>)` column constraint
18087            // (mailrs round-5 G3). PG semantics: column-level
18088            // CHECK is equivalent to a table-level CHECK. Multiple
18089            // inline CHECKs on the same column AND together.
18090            if let Token::Ident(s) = self.peek()
18091                && s.eq_ignore_ascii_case("check")
18092            {
18093                self.advance();
18094                if !matches!(self.peek(), Token::LParen) {
18095                    return Err(self.err(alloc::format!(
18096                        "expected '(' after CHECK in column def, got {:?}",
18097                        self.peek()
18098                    )));
18099                }
18100                self.advance();
18101                let pred = self.parse_expr(0)?;
18102                if !matches!(self.peek(), Token::RParen) {
18103                    return Err(self.err(alloc::format!(
18104                        "expected ')' to close CHECK predicate, got {:?}",
18105                        self.peek()
18106                    )));
18107                }
18108                self.advance();
18109                check = Some(match check.take() {
18110                    Some(prev) => Expr::Binary {
18111                        op: BinOp::And,
18112                        lhs: Box::new(prev),
18113                        rhs: Box::new(pred),
18114                    },
18115                    None => pred,
18116                });
18117                continue;
18118            }
18119            break;
18120        }
18121        Ok(ColumnDef {
18122            name,
18123            ty,
18124            nullable,
18125            default,
18126            auto_increment,
18127            is_primary_key,
18128            is_unique,
18129            unique_nulls_not_distinct,
18130            constraint_deferrable,
18131            constraint_initially_deferred,
18132            check,
18133            user_type_ref,
18134            on_update_runtime,
18135            collation,
18136            collation_explicit,
18137            collation_name,
18138            is_unsigned,
18139            inline_enum_variants,
18140            inline_set_variants,
18141            generated_stored_expr,
18142            identity_always,
18143            mysql_int_width,
18144            mysql_fsp,
18145            mysql_declared_timestamp,
18146            mysql_float_md,
18147        })
18148    }
18149
18150    /// `NUMERIC` may appear without parameters, with one (precision
18151    /// only, scale=0), or with both. Returns `(precision, scale)` with
18152    /// 0 = unspecified for the bare form.
18153    fn parse_optional_numeric_params(&mut self) -> Result<(u16, i16), ParseError> {
18154        if !matches!(self.peek(), Token::LParen) {
18155            // Bare `NUMERIC` — PG treats this as "unlimited precision";
18156            // we surface it as precision=0 to mean "unconstrained" so
18157            // the engine doesn't need a separate variant.
18158            return Ok((0, 0));
18159        }
18160        self.advance();
18161        // v7.39 (round 272) — PG's declared precision runs to 1000, and
18162        // it words the out-of-range case with the value it saw. SPG
18163        // capped at 38 (i128's width), so a `numeric(50,10)` column PG
18164        // accepts failed to parse at all; values wider than i128 are
18165        // carried by the arbitrary-precision form.
18166        let precision = match self.advance() {
18167            Token::Integer(n) if (1..=1000).contains(&n) => {
18168                u16::try_from(n).expect("range-checked")
18169            }
18170            Token::Integer(n) => {
18171                return Err(ParseError {
18172                    message: format!("NUMERIC precision {n} must be between 1 and 1000"),
18173                    token_pos: self.consumed_pos(),
18174                });
18175            }
18176            other => {
18177                return Err(ParseError {
18178                    message: format!(
18179                        "NUMERIC precision must be an integer in 1..=1000, got {other:?}"
18180                    ),
18181                    token_pos: self.consumed_pos(),
18182                });
18183            }
18184        };
18185        // v7.39 (round 273) — PG's declared scale runs -1000..=1000 and is
18186        // NOT bounded by the precision (`numeric(10,11)` is legal; a value
18187        // then overflows). A negative scale rounds to tens / hundreds / …
18188        let scale = if matches!(self.peek(), Token::Comma) {
18189            self.advance();
18190            let neg = if matches!(self.peek(), Token::Minus) {
18191                self.advance();
18192                true
18193            } else {
18194                false
18195            };
18196            match self.advance() {
18197                Token::Integer(n) => {
18198                    let signed = if neg { -n } else { n };
18199                    if !(-1000..=1000).contains(&signed) {
18200                        return Err(ParseError {
18201                            message: format!(
18202                                "NUMERIC scale {signed} must be between -1000 and 1000"
18203                            ),
18204                            token_pos: self.consumed_pos(),
18205                        });
18206                    }
18207                    i16::try_from(signed).expect("range-checked")
18208                }
18209                other => {
18210                    return Err(ParseError {
18211                        message: format!("NUMERIC scale must be an integer, got {other:?}"),
18212                        token_pos: self.consumed_pos(),
18213                    });
18214                }
18215            }
18216        } else {
18217            0
18218        };
18219        if !matches!(self.peek(), Token::RParen) {
18220            return Err(self.err(format!(
18221                "expected ')' to close NUMERIC params, got {:?}",
18222                self.peek()
18223            )));
18224        }
18225        self.advance();
18226        Ok((precision, scale))
18227    }
18228
18229    /// Parse `(N)` where `N` is a positive integer literal — used by the
18230    /// `VARCHAR`/`CHAR`/`VECTOR` column types. `label` is the type name
18231    /// for the error message.
18232    /// v6.0.1: parse the optional `USING <encoding>` clause that
18233    /// follows `VECTOR(N)` in a column definition. Missing clause
18234    /// → `VecEncoding::F32` (pre-v6 default). Unknown encoding
18235    /// ident → `ParseError` listing the encodings recognised today.
18236    fn parse_optional_vector_encoding(&mut self) -> Result<VecEncoding, ParseError> {
18237        if !matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("using")) {
18238            return Ok(VecEncoding::F32);
18239        }
18240        // v7.13.2 — mailrs round-6 S6: `USING` after a vector type
18241        // overlaps with `ALTER COLUMN TYPE … USING <expr>`. Only
18242        // consume the token when the very next token is a known
18243        // vector-encoding keyword (SQ8 / HALF). Otherwise leave
18244        // `USING` for the caller — it's the rewrite-expression form.
18245        let n1 = self.tokens.get(self.pos + 1);
18246        let next_is_encoding = matches!(
18247            n1,
18248            Some(Token::Ident(s))
18249                if s.eq_ignore_ascii_case("sq8") || s.eq_ignore_ascii_case("half")
18250        );
18251        if !next_is_encoding {
18252            return Ok(VecEncoding::F32);
18253        }
18254        self.advance();
18255        let enc_ident = match self.advance() {
18256            Token::Ident(s) => s,
18257            other => {
18258                return Err(self.err(format!(
18259                    "expected vector encoding after USING, got {other:?}"
18260                )));
18261            }
18262        };
18263        match enc_ident.to_ascii_lowercase().as_str() {
18264            "sq8" => Ok(VecEncoding::Sq8),
18265            // v6.0.3: `HALF` (pgvector convention) selects IEEE-754
18266            // binary16 per-element storage.
18267            "half" => Ok(VecEncoding::F16),
18268            other => Err(self.err(format!(
18269                "unknown vector encoding {other:?}; supported: SQ8, HALF"
18270            ))),
18271        }
18272    }
18273
18274    /// v7.17.0 Phase 4.3 — peek at the MySQL display-width
18275    /// without consuming it. Returns `Some(N)` when the next
18276    /// tokens are `( <int> )`; None otherwise. Used by the
18277    /// TINYINT classifier to decide whether to map to Bool or
18278    /// SmallInt.
18279    fn peek_optional_paren_size_value(&self) -> Option<i64> {
18280        if !matches!(self.peek(), Token::LParen) {
18281            return None;
18282        }
18283        let next = self.tokens.get(self.pos + 1)?;
18284        let n = match next {
18285            Token::Integer(n) => *n,
18286            _ => return None,
18287        };
18288        if !matches!(self.tokens.get(self.pos + 2), Some(Token::RParen)) {
18289            return None;
18290        }
18291        Some(n)
18292    }
18293
18294    /// v7.14.0 — consume an optional MySQL display-width
18295    /// parenthesised number after an integer type, returning
18296    /// nothing. `TINYINT(1)` etc.
18297    /// v7.39 (round 360) — does the parenthesised group ahead contain a
18298    /// comma, i.e. is it MySQL's `(m,d)` rather than PG's `(p)`?
18299    fn peek_paren_has_comma(&self) -> bool {
18300        let mut i = self.pos + 1;
18301        let mut depth = 1usize;
18302        while depth > 0 {
18303            match self.tokens.get(i) {
18304                Some(Token::LParen) => depth += 1,
18305                Some(Token::RParen) => depth -= 1,
18306                Some(Token::Comma) if depth == 1 => return true,
18307                None | Some(Token::Eof) => return false,
18308                _ => {}
18309            }
18310            i += 1;
18311        }
18312        false
18313    }
18314
18315    /// v7.39 (round 424) — the same optional `(N)` modifier, but RETURNING
18316    /// the number. Temporal columns need it: MySQL's `DATETIME(3)` declares a
18317    /// fractional-seconds precision that drives write truncation and render
18318    /// padding, where `consume_optional_paren_size` throws it away.
18319    /// `Some(0)` for an explicit `(0)`, `None` when no modifier is written.
18320    fn take_optional_paren_size(&mut self) -> Option<u8> {
18321        let Some(Token::Integer(n)) = self
18322            .tokens
18323            .get(self.pos + 1)
18324            .filter(|_| matches!(self.peek(), Token::LParen))
18325            .cloned()
18326        else {
18327            self.consume_optional_paren_size();
18328            return None;
18329        };
18330        if !matches!(self.tokens.get(self.pos + 2), Some(Token::RParen)) {
18331            self.consume_optional_paren_size();
18332            return None;
18333        }
18334        self.consume_optional_paren_size();
18335        u8::try_from(n).ok()
18336    }
18337
18338    fn consume_optional_paren_size(&mut self) {
18339        if !matches!(self.peek(), Token::LParen) {
18340            return;
18341        }
18342        self.advance();
18343        // Skip until matching RParen (allow nested or any tokens).
18344        let mut depth = 1usize;
18345        while depth > 0 {
18346            match self.peek() {
18347                Token::LParen => depth += 1,
18348                Token::RParen => depth -= 1,
18349                Token::Eof => return,
18350                _ => {}
18351            }
18352            self.advance();
18353        }
18354    }
18355
18356    fn parse_paren_size(&mut self, label: &str) -> Result<u32, ParseError> {
18357        if !matches!(self.peek(), Token::LParen) {
18358            return Err(self.err(format!("{label} type requires (N), got {:?}", self.peek())));
18359        }
18360        self.advance();
18361        let n = match self.advance() {
18362            Token::Integer(n) if n > 0 => u32::try_from(n).map_err(|_| ParseError {
18363                message: format!("{label} size too large: {n}"),
18364                token_pos: self.consumed_pos(),
18365            })?,
18366            other => {
18367                return Err(ParseError {
18368                    message: format!("expected positive integer {label} size, got {other:?}"),
18369                    token_pos: self.consumed_pos(),
18370                });
18371            }
18372        };
18373        if !matches!(self.peek(), Token::RParen) {
18374            return Err(self.err(format!(
18375                "expected ')' after {label} size, got {:?}",
18376                self.peek()
18377            )));
18378        }
18379        self.advance();
18380        Ok(n)
18381    }
18382
18383    /// v7.39 (round 406) — the `ON CONFLICT DO NOTHING` clause that MySQL's
18384    /// `INSERT IGNORE` lowers to: a bare target (arbitrate on every unique
18385    /// key, like MySQL) whose action skips conflicting rows.
18386    /// v7.39 (round 419) — resolve the conflict clause for ANY of the four
18387    /// INSERT source forms (VALUES / SELECT / parenthesized source / WITH).
18388    /// Before this the MySQL upsert lowerings (`ON DUPLICATE KEY UPDATE`,
18389    /// `REPLACE INTO`) were wired into the VALUES branch ONLY, so the very
18390    /// common bulk-upsert spellings —
18391    ///     INSERT INTO t SELECT … ON DUPLICATE KEY UPDATE c = VALUES(c)
18392    ///     REPLACE INTO t SELECT …
18393    /// — were a parse error / a duplicate-key failure respectively.
18394    ///
18395    /// Precedence: an explicitly written clause beats a statement-level flag.
18396    /// `ON DUPLICATE KEY UPDATE` first, then PG's own `ON CONFLICT`, then the
18397    /// implicit `REPLACE` and `IGNORE` lowerings.
18398    fn parse_insert_conflict_clause(
18399        &mut self,
18400        replace: bool,
18401        ignore: bool,
18402    ) -> Result<Option<crate::ast::OnConflictClause>, ParseError> {
18403        if let Some(c) = self.parse_optional_on_duplicate_key()? {
18404            return Ok(Some(c));
18405        }
18406        if let Some(c) = self.parse_optional_on_conflict()? {
18407            return Ok(Some(c));
18408        }
18409        if replace {
18410            // REPLACE INTO = delete-then-insert, which PG spells as
18411            // `ON CONFLICT DO UPDATE SET` over every column; the engine
18412            // reads an empty assignment list as "take the incoming row".
18413            return Ok(Some(crate::ast::OnConflictClause {
18414                target_columns: Vec::new(),
18415                index_where: None,
18416                constraint_name: None,
18417                mysql_lowered: true,
18418                action: crate::ast::OnConflictAction::Update {
18419                    assignments: Vec::new(),
18420                    where_: None,
18421                },
18422            }));
18423        }
18424        if ignore {
18425            return Ok(Some(Self::insert_ignore_clause()));
18426        }
18427        Ok(None)
18428    }
18429
18430    /// v7.39 (round 419, extracted from the VALUES branch) — MySQL's
18431    /// `ON DUPLICATE KEY UPDATE col = expr [, …]`. Bare target (MySQL
18432    /// watches every unique key, which `mysql_lowered` records); `VALUES(col)`
18433    /// in an assignment is MySQL's spelling of `EXCLUDED.col`.
18434    fn parse_optional_on_duplicate_key(
18435        &mut self,
18436    ) -> Result<Option<crate::ast::OnConflictClause>, ParseError> {
18437        if !(matches!(self.peek(), Token::On)
18438            && matches!(self.tokens.get(self.pos + 1),
18439                Some(Token::Ident(s)) if s.eq_ignore_ascii_case("duplicate")))
18440        {
18441            return Ok(None);
18442        }
18443        self.advance(); // ON
18444        self.advance(); // DUPLICATE
18445        if !matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("key")) {
18446            return Err(self.err(format!(
18447                "expected KEY after ON DUPLICATE, got {:?}",
18448                self.peek()
18449            )));
18450        }
18451        self.advance();
18452        if !matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("update")) {
18453            return Err(self.err(format!(
18454                "expected UPDATE after ON DUPLICATE KEY, got {:?}",
18455                self.peek()
18456            )));
18457        }
18458        self.advance();
18459        let mut assignments: Vec<(String, Expr)> = Vec::new();
18460        loop {
18461            let col = self.expect_ident_like()?;
18462            if !matches!(self.peek(), Token::Eq) {
18463                return Err(self.err(format!(
18464                    "expected '=' in ON DUPLICATE KEY UPDATE, got {:?}",
18465                    self.peek()
18466                )));
18467            }
18468            self.advance();
18469            let mut expr = self.parse_expr(0)?;
18470            Self::rewrite_mysql_values_refs(&mut expr);
18471            assignments.push((col, expr));
18472            if matches!(self.peek(), Token::Comma) {
18473                self.advance();
18474                continue;
18475            }
18476            break;
18477        }
18478        Ok(Some(crate::ast::OnConflictClause {
18479            target_columns: Vec::new(),
18480            index_where: None,
18481            constraint_name: None,
18482            mysql_lowered: true,
18483            action: crate::ast::OnConflictAction::Update {
18484                assignments,
18485                where_: None,
18486            },
18487        }))
18488    }
18489
18490    fn insert_ignore_clause() -> crate::ast::OnConflictClause {
18491        crate::ast::OnConflictClause {
18492            target_columns: Vec::new(),
18493            index_where: None,
18494            constraint_name: None,
18495            mysql_lowered: true,
18496            action: crate::ast::OnConflictAction::Nothing,
18497        }
18498    }
18499
18500    fn parse_insert_stmt(&mut self, replace: bool) -> Result<Statement, ParseError> {
18501        debug_assert!(
18502            matches!(self.peek(), Token::Insert)
18503                || (replace
18504                    && matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("replace")))
18505        );
18506        self.advance();
18507        // v7.39 (round 406) — MySQL `INSERT IGNORE INTO t …` skips a row that
18508        // would raise a duplicate-key error instead of failing the statement,
18509        // i.e. `ON CONFLICT DO NOTHING` over every unique key. IGNORE is a
18510        // plain ident to the lexer; only the MySQL dialect accepts it here.
18511        let ignore = self.mysql_dialect
18512            && matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("ignore"));
18513        if ignore {
18514            self.advance();
18515        }
18516        if !matches!(self.peek(), Token::Into) {
18517            return Err(self.err(format!("expected INTO after INSERT, got {:?}", self.peek())));
18518        }
18519        self.advance();
18520        let table = self.expect_ident_like()?;
18521        // v7.39 (round 240) — `INSERT INTO t AS alias`: PG's insert_target
18522        // grammar requires the AS keyword here (a bare identifier would be
18523        // ambiguous with a column list). The alias is what the ON CONFLICT
18524        // DO UPDATE expressions refer to the target row by.
18525        let alias = if matches!(self.peek(), Token::As) {
18526            self.advance();
18527            Some(self.expect_ident_like()?)
18528        } else {
18529            None
18530        };
18531        // v7.39 (round 428) — MySQL's SET-form INSERT:
18532        //     INSERT INTO t SET a = 1, b = 'x'
18533        // It is exactly `INSERT INTO t (a, b) VALUES (1, 'x')` — omitted
18534        // columns take their DEFAULT, `SET a = DEFAULT` is legal, and it
18535        // composes with IGNORE / ON DUPLICATE KEY UPDATE / REPLACE (all
18536        // measured). So it lowers to the column list + one VALUES row and
18537        // rejoins the ordinary path, which already handles every one of
18538        // those. PG has no such spelling, hence the dialect gate.
18539        if self.mysql_dialect
18540            && matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("set"))
18541        {
18542            self.advance(); // SET
18543            let mut names = Vec::new();
18544            let mut values = Vec::new();
18545            loop {
18546                names.push(self.expect_ident_like()?);
18547                if !matches!(self.peek(), Token::Eq) {
18548                    return Err(self.err(alloc::format!(
18549                        "expected '=' in INSERT … SET, got {:?}",
18550                        self.peek()
18551                    )));
18552                }
18553                self.advance();
18554                // `SET a = DEFAULT` rides the same `__column_default` marker
18555                // the VALUES-row and UPDATE-SET paths use; the INSERT
18556                // executor resolves it against the target column.
18557                if matches!(self.peek(), Token::Default) {
18558                    self.advance();
18559                    values.push(Expr::FunctionCall {
18560                        name: "__column_default".to_string(),
18561                        args: Vec::new(),
18562                    });
18563                } else {
18564                    values.push(self.parse_expr(0)?);
18565                }
18566                if matches!(self.peek(), Token::Comma) {
18567                    self.advance();
18568                    continue;
18569                }
18570                break;
18571            }
18572            let on_conflict = self.parse_insert_conflict_clause(replace, ignore)?;
18573            let returning = self.parse_optional_returning()?;
18574            return Ok(Statement::Insert(InsertStatement {
18575                ctes: Vec::new(),
18576                table,
18577                alias,
18578                columns: Some(names),
18579                rows: alloc::vec![values],
18580                select_source: None,
18581                // MySQL's SET form has no `OVERRIDING …` clause (that is
18582                // PG's identity-column spelling).
18583                overriding: Overriding::None,
18584                mysql_ignore: ignore,
18585                on_conflict,
18586                returning,
18587            }));
18588        }
18589        // Optional column list — `INSERT INTO t (a, b) VALUES ...`.
18590        // v7.39 (round 151) — a SELECT or WITH right after the paren is
18591        // a parenthesized query source instead (PG select_with_parens:
18592        // `INSERT INTO t (SELECT …)` / `INSERT INTO t (WITH … SELECT …)`);
18593        // both keywords are reserved in PG, so no column list can start
18594        // with them.
18595        let columns = if matches!(self.peek(), Token::LParen) {
18596            self.advance();
18597            if matches!(self.peek(), Token::Select) || self.peek_is_with_kw() {
18598                let select_stmt = if self.peek_is_with_kw() {
18599                    self.advance();
18600                    self.parse_nested_with_select()?
18601                } else {
18602                    match self.parse_select_stmt()? {
18603                        Statement::Select(s) => s,
18604                        other => {
18605                            return Err(self.err(alloc::format!(
18606                                "expected SELECT in parenthesized INSERT source, got {other:?}"
18607                            )));
18608                        }
18609                    }
18610                };
18611                if !matches!(self.peek(), Token::RParen) {
18612                    return Err(self.err(format!(
18613                        "expected ')' after parenthesized INSERT source, got {:?}",
18614                        self.peek()
18615                    )));
18616                }
18617                self.advance();
18618                let on_conflict = self.parse_insert_conflict_clause(replace, ignore)?;
18619                let returning = self.parse_optional_returning()?;
18620                return Ok(Statement::Insert(InsertStatement {
18621                    ctes: Vec::new(),
18622                    table,
18623                    alias: alias.clone(),
18624                    columns: None,
18625                    rows: Vec::new(),
18626                    select_source: Some(Box::new(select_stmt)),
18627                    on_conflict,
18628                    returning,
18629                    overriding: Overriding::None,
18630                    mysql_ignore: ignore,
18631                }));
18632            }
18633            let mut names = Vec::new();
18634            loop {
18635                names.push(self.expect_ident_like()?);
18636                match self.peek() {
18637                    Token::Comma => {
18638                        self.advance();
18639                    }
18640                    Token::RParen => {
18641                        self.advance();
18642                        break;
18643                    }
18644                    other => {
18645                        return Err(self.err(format!(
18646                            "expected ',' or ')' in INSERT column list, got {other:?}"
18647                        )));
18648                    }
18649                }
18650            }
18651            Some(names)
18652        } else {
18653            None
18654        };
18655        // PG 10+ `OVERRIDING {SYSTEM | USER} VALUE` — pg_dump emits
18656        // OVERRIDING SYSTEM VALUE for its identity columns. The clause
18657        // is captured on the statement so the engine can apply PG's
18658        // GENERATED ALWAYS / BY DEFAULT interaction (v7.38, read01).
18659        let overriding = if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("overriding"))
18660        {
18661            self.advance();
18662            let which = self.expect_ident_like()?;
18663            let ov = if which.eq_ignore_ascii_case("system") {
18664                Overriding::System
18665            } else if which.eq_ignore_ascii_case("user") {
18666                Overriding::User
18667            } else {
18668                return Err(self.err(format!(
18669                    "expected SYSTEM or USER after OVERRIDING, got {which:?}"
18670                )));
18671            };
18672            if !matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("value")) {
18673                return Err(self.err(format!(
18674                    "expected VALUE after OVERRIDING {}, got {:?}",
18675                    which.to_ascii_uppercase(),
18676                    self.peek()
18677                )));
18678            }
18679            self.advance();
18680            ov
18681        } else {
18682            Overriding::None
18683        };
18684        // `INSERT INTO t DEFAULT VALUES` — a single row made
18685        // entirely of column defaults. Lower to the permuted
18686        // column-list path with an empty list: every schema column
18687        // is unmapped, so the engine fills each from its default
18688        // (serials advance, plain defaults evaluate, the rest NULL).
18689        if matches!(self.peek(), Token::Default) {
18690            self.advance();
18691            if !matches!(self.peek(), Token::Values) {
18692                return Err(self.err(format!(
18693                    "expected VALUES after DEFAULT in INSERT, got {:?}",
18694                    self.peek()
18695                )));
18696            }
18697            self.advance();
18698            if columns.is_some() {
18699                return Err(self.err("DEFAULT VALUES cannot follow an INSERT column list".into()));
18700            }
18701            let on_conflict = self.parse_insert_conflict_clause(replace, ignore)?;
18702            let returning = self.parse_optional_returning()?;
18703            return Ok(Statement::Insert(InsertStatement {
18704                ctes: Vec::new(),
18705                table,
18706                alias: alias.clone(),
18707                columns: Some(Vec::new()),
18708                rows: alloc::vec![Vec::new()],
18709                select_source: None,
18710                on_conflict,
18711                returning,
18712                overriding,
18713                mysql_ignore: ignore,
18714            }));
18715        }
18716        // v7.13.0 — `INSERT INTO t [(cols)] SELECT …` (mailrs
18717        // round-5 G4). Dispatch on VALUES vs SELECT. v7.39 (round 151)
18718        // — a WITH-headed source query (`INSERT INTO t WITH c AS (…)
18719        // SELECT …`) heads the SOURCE select, as in PG (the statement's
18720        // own WITH comes before INSERT).
18721        if matches!(self.peek(), Token::Select) || self.peek_is_with_kw() {
18722            let select_stmt = if self.peek_is_with_kw() {
18723                self.advance();
18724                self.parse_nested_with_select()?
18725            } else {
18726                match self.parse_select_stmt()? {
18727                    Statement::Select(s) => s,
18728                    other => {
18729                        return Err(self.err(alloc::format!(
18730                            "expected SELECT after INSERT INTO ... target, got {other:?}"
18731                        )));
18732                    }
18733                }
18734            };
18735            let on_conflict = self.parse_insert_conflict_clause(replace, ignore)?;
18736            let returning = self.parse_optional_returning()?;
18737            return Ok(Statement::Insert(InsertStatement {
18738                ctes: Vec::new(),
18739                table,
18740                alias: alias.clone(),
18741                columns,
18742                rows: Vec::new(),
18743                select_source: Some(Box::new(select_stmt)),
18744                on_conflict,
18745                returning,
18746                overriding,
18747                mysql_ignore: ignore,
18748            }));
18749        }
18750        if !matches!(self.peek(), Token::Values) {
18751            return Err(self.err(format!(
18752                "expected VALUES or SELECT after table name, got {:?}",
18753                self.peek()
18754            )));
18755        }
18756        self.advance();
18757        if !matches!(self.peek(), Token::LParen) {
18758            return Err(self.err(format!("expected '(' after VALUES, got {:?}", self.peek())));
18759        }
18760        let mut rows = Vec::new();
18761        loop {
18762            // Each iteration consumes one `(expr, expr, …)` tuple.
18763            if !matches!(self.peek(), Token::LParen) {
18764                return Err(self.err(format!(
18765                    "expected '(' for next VALUES tuple, got {:?}",
18766                    self.peek()
18767                )));
18768            }
18769            self.advance();
18770            let mut tuple = Vec::new();
18771            loop {
18772                // v7.38 (read01) — `INSERT INTO t VALUES (…, DEFAULT, …)` uses
18773                // the column's declared default for that slot. Rides out as the
18774                // same `__column_default` marker call the UPDATE `SET c = DEFAULT`
18775                // path uses; the INSERT executor resolves it per target column.
18776                if matches!(self.peek(), Token::Default) {
18777                    self.advance();
18778                    tuple.push(Expr::FunctionCall {
18779                        name: "__column_default".to_string(),
18780                        args: Vec::new(),
18781                    });
18782                } else {
18783                    tuple.push(self.parse_expr(0)?);
18784                }
18785                match self.peek() {
18786                    Token::Comma => {
18787                        self.advance();
18788                    }
18789                    Token::RParen => {
18790                        self.advance();
18791                        break;
18792                    }
18793                    other => {
18794                        return Err(self.err(format!(
18795                            "expected ',' or ')' in VALUES tuple, got {other:?}"
18796                        )));
18797                    }
18798                }
18799            }
18800            if tuple.is_empty() {
18801                return Err(self.err("INSERT VALUES tuple requires at least one value".into()));
18802            }
18803            rows.push(tuple);
18804            // Continue with comma-separated tuples.
18805            if matches!(self.peek(), Token::Comma) {
18806                self.advance();
18807            } else {
18808                break;
18809            }
18810        }
18811        // MySQL `ON DUPLICATE KEY UPDATE col = expr [, …]` — lowers
18812        // to ON CONFLICT DO UPDATE with an empty conflict target
18813        // (the engine picks the table's first unique index, which
18814        // matches MySQL's any-unique-key behaviour for the common
18815        // single-key case). `VALUES(col)` in the assignments is
18816        // MySQL's spelling of EXCLUDED.col.
18817        let on_conflict = self.parse_insert_conflict_clause(replace, ignore)?;
18818        let returning = self.parse_optional_returning()?;
18819        Ok(Statement::Insert(InsertStatement {
18820            ctes: Vec::new(),
18821            table,
18822            alias,
18823            columns,
18824            rows,
18825            select_source: None,
18826            on_conflict,
18827            returning,
18828            overriding,
18829            mysql_ignore: ignore,
18830        }))
18831    }
18832
18833    /// MySQL's `VALUES(col)` inside ON DUPLICATE KEY UPDATE reads
18834    /// the incoming row's value — exactly PG's EXCLUDED.col.
18835    fn rewrite_mysql_values_refs(e: &mut Expr) {
18836        match e {
18837            Expr::FunctionCall { name, args }
18838                if name.eq_ignore_ascii_case("values")
18839                    && args.len() == 1
18840                    && matches!(&args[0], Expr::Column(c) if c.qualifier.is_none()) =>
18841            {
18842                let Expr::Column(c) = &args[0] else {
18843                    unreachable!("guarded above");
18844                };
18845                *e = Expr::Column(crate::ast::ColumnName {
18846                    qualifier: Some("EXCLUDED".to_string()),
18847                    name: c.name.clone(),
18848                });
18849            }
18850            Expr::FunctionCall { args, .. } => {
18851                for a in args {
18852                    Self::rewrite_mysql_values_refs(a);
18853                }
18854            }
18855            Expr::Binary { lhs, rhs, .. } => {
18856                Self::rewrite_mysql_values_refs(lhs);
18857                Self::rewrite_mysql_values_refs(rhs);
18858            }
18859            Expr::Unary { expr, .. } | Expr::Cast { expr, .. } => {
18860                Self::rewrite_mysql_values_refs(expr);
18861            }
18862            Expr::Case {
18863                operand,
18864                branches,
18865                else_branch,
18866            } => {
18867                if let Some(op) = operand {
18868                    Self::rewrite_mysql_values_refs(op);
18869                }
18870                for (w, t) in branches {
18871                    Self::rewrite_mysql_values_refs(w);
18872                    Self::rewrite_mysql_values_refs(t);
18873                }
18874                if let Some(el) = else_branch {
18875                    Self::rewrite_mysql_values_refs(el);
18876                }
18877            }
18878            _ => {}
18879        }
18880    }
18881
18882    /// v7.9.7 — parse the optional `ON CONFLICT (cols) DO …`
18883    /// clause sitting between the INSERT body and the trailing
18884    /// RETURNING. All keywords come in as bare idents; `ON` is
18885    /// a reserved Token though.
18886    fn parse_optional_on_conflict(
18887        &mut self,
18888    ) -> Result<Option<crate::ast::OnConflictClause>, ParseError> {
18889        if !matches!(self.peek(), Token::On) {
18890            return Ok(None);
18891        }
18892        // Peek further: we want exactly "ON CONFLICT ...". If the
18893        // next ident isn't "conflict", let some other parser handle.
18894        let next_is_conflict = matches!(
18895            self.tokens.get(self.pos + 1),
18896            Some(Token::Ident(s) | Token::QuotedIdent(s)) if s.eq_ignore_ascii_case("conflict")
18897        );
18898        if !next_is_conflict {
18899            return Ok(None);
18900        }
18901        self.advance(); // ON
18902        self.advance(); // CONFLICT
18903        // v7.37.17 (17.6 siblings) — `ON CONSTRAINT <name>` names
18904        // the constraint instead of listing columns (the pg_dump
18905        // form); the engine resolves it.
18906        let mut constraint_name: Option<String> = None;
18907        if matches!(self.peek(), Token::On) {
18908            self.advance(); // ON
18909            match self.advance() {
18910                Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("constraint") => {
18911                }
18912                other => {
18913                    return Err(self.err(alloc::format!(
18914                        "expected CONSTRAINT after ON CONFLICT ON, got {other:?}"
18915                    )));
18916                }
18917            }
18918            constraint_name = Some(self.expect_ident_like()?);
18919        }
18920        // Optional `(col [, col]*)` target list.
18921        let mut target_columns: Vec<String> = Vec::new();
18922        if matches!(self.peek(), Token::LParen) {
18923            self.advance();
18924            loop {
18925                target_columns.push(self.expect_ident_like()?);
18926                match self.peek() {
18927                    Token::Comma => {
18928                        self.advance();
18929                    }
18930                    Token::RParen => {
18931                        self.advance();
18932                        break;
18933                    }
18934                    other => {
18935                        return Err(self.err(alloc::format!(
18936                            "expected ',' or ')' in ON CONFLICT target list, got {other:?}"
18937                        )));
18938                    }
18939                }
18940            }
18941        }
18942        // v7.39 (round 240) — optional index predicate after the target
18943        // list: `ON CONFLICT (col) WHERE pred DO …`. PG uses it to infer a
18944        // PARTIAL unique index; SPG's arbiters are full indexes, which
18945        // satisfy any predicate, so it is parsed and carried but not
18946        // consulted (recorded residual: partial-unique-index arbiters).
18947        let index_where = if !target_columns.is_empty() && matches!(self.peek(), Token::Where) {
18948            self.advance();
18949            Some(self.parse_expr(0)?)
18950        } else {
18951            None
18952        };
18953        // Required `DO`.
18954        match self.advance() {
18955            Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("do") => {}
18956            other => {
18957                return Err(self.err(alloc::format!(
18958                    "expected DO after ON CONFLICT [(…)], got {other:?}"
18959                )));
18960            }
18961        }
18962        // Action: NOTHING | UPDATE SET …
18963        let action = match self.advance() {
18964            Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("nothing") => {
18965                crate::ast::OnConflictAction::Nothing
18966            }
18967            Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("update") => {
18968                self.parse_on_conflict_update_action()?
18969            }
18970            other => {
18971                return Err(self.err(alloc::format!(
18972                    "expected NOTHING or UPDATE after ON CONFLICT DO, got {other:?}"
18973                )));
18974            }
18975        };
18976        Ok(Some(crate::ast::OnConflictClause {
18977            target_columns,
18978            index_where,
18979            constraint_name,
18980            mysql_lowered: false,
18981            action,
18982        }))
18983    }
18984
18985    /// v7.9.7 — tail of `ON CONFLICT … DO UPDATE`: parse
18986    /// `SET col = expr [, …] [WHERE cond]`. Caller already
18987    /// consumed `UPDATE`.
18988    fn parse_on_conflict_update_action(
18989        &mut self,
18990    ) -> Result<crate::ast::OnConflictAction, ParseError> {
18991        // `SET`
18992        match self.advance() {
18993            Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("set") => {}
18994            other => {
18995                return Err(self.err(alloc::format!(
18996                    "expected SET after ON CONFLICT DO UPDATE, got {other:?}"
18997                )));
18998            }
18999        }
19000        let mut assignments: Vec<(String, Expr)> = Vec::new();
19001        loop {
19002            let col = self.expect_ident_like()?;
19003            if !matches!(self.peek(), Token::Eq) {
19004                return Err(self.err(alloc::format!(
19005                    "expected `=` after column in ON CONFLICT DO UPDATE SET, got {:?}",
19006                    self.peek()
19007                )));
19008            }
19009            self.advance();
19010            let value = self.parse_expr(0)?;
19011            assignments.push((col, value));
19012            if matches!(self.peek(), Token::Comma) {
19013                self.advance();
19014                continue;
19015            }
19016            break;
19017        }
19018        let where_ = if matches!(self.peek(), Token::Where) {
19019            self.advance();
19020            Some(self.parse_expr(0)?)
19021        } else {
19022            None
19023        };
19024        Ok(crate::ast::OnConflictAction::Update {
19025            assignments,
19026            where_,
19027        })
19028    }
19029
19030    fn parse_select_list(&mut self) -> Result<Vec<SelectItem>, ParseError> {
19031        let mut items = Vec::new();
19032        // v7.39 (round 341, V66) — PG's target list may be EMPTY
19033        // (`opt_target_list: target_list | /*EMPTY*/`): `SELECT FROM t`
19034        // answers one zero-column row per row of t, and a bare `SELECT`
19035        // answers a single zero-column row. SPG required at least one
19036        // item, so both were syntax errors. Recognised by the token that
19037        // follows — nothing that can start an expression appears here.
19038        if self.select_list_is_empty_here() {
19039            return Ok(items);
19040        }
19041        loop {
19042            items.push(self.parse_select_item()?);
19043            if matches!(self.peek(), Token::Comma) {
19044                self.advance();
19045            } else {
19046                break;
19047            }
19048        }
19049        Ok(items)
19050    }
19051
19052    /// Is the target list empty at this point — i.e. does the next token
19053    /// end the SELECT's item list rather than start an item?
19054    fn select_list_is_empty_here(&self) -> bool {
19055        match self.peek() {
19056            Token::From
19057            | Token::Where
19058            | Token::Group
19059            | Token::Having
19060            | Token::Order
19061            | Token::Limit
19062            | Token::Offset
19063            | Token::Semicolon
19064            | Token::RParen
19065            | Token::Union
19066            | Token::Except
19067            | Token::Eof => true,
19068            // `FETCH FIRST … ROWS ONLY` and `WINDOW w AS …` are spelled
19069            // with unreserved keywords, so they arrive as plain idents.
19070            Token::Ident(s) => {
19071                s.eq_ignore_ascii_case("fetch")
19072                    || s.eq_ignore_ascii_case("window")
19073                    || s.eq_ignore_ascii_case("intersect")
19074            }
19075            _ => false,
19076        }
19077    }
19078
19079    fn parse_select_item(&mut self) -> Result<SelectItem, ParseError> {
19080        if matches!(self.peek(), Token::Star) {
19081            self.advance();
19082            return Ok(SelectItem::Wildcard);
19083        }
19084        // v7.39 (read01 round 128) — qualified wildcard `qualifier.*`. Intercept
19085        // BEFORE `parse_expr`, which would treat `q.` as a qualified column and
19086        // choke on the `*` ("expected identifier, got Star"). The lookahead is
19087        // `<ident> . *` with nothing binding tighter.
19088        if let Token::Ident(q) | Token::QuotedIdent(q) = self.peek().clone() {
19089            if matches!(self.tokens.get(self.pos + 1), Some(Token::Dot))
19090                && matches!(self.tokens.get(self.pos + 2), Some(Token::Star))
19091            {
19092                self.advance(); // qualifier
19093                self.advance(); // .
19094                self.advance(); // *
19095                return Ok(SelectItem::QualifiedWildcard(q));
19096            }
19097        }
19098        let start_tok = self.pos;
19099        let expr = self.parse_expr(0)?;
19100        let end_tok = self.consumed_pos();
19101        // v7.39 (read01 round 69) — `(f(args)).*`: expand the RECORD a
19102        // multi-column function returns into columns. Marked here and lowered in
19103        // `parse_bare_select`, where the FROM clause is in hand.
19104        if matches!(self.peek(), Token::Dot)
19105            && matches!(self.tokens.get(self.pos + 1), Some(Token::Star))
19106        {
19107            self.advance(); // .
19108            self.advance(); // *
19109            return Ok(SelectItem::Expr {
19110                expr: Expr::FunctionCall {
19111                    name: "__record_expand".to_string(),
19112                    args: alloc::vec![expr],
19113                },
19114                alias: None,
19115            });
19116        }
19117        // v7.39.2 — MySQL lets a STRING name a projection item, with or
19118        // without `AS`: `SELECT 1 'x'`, `SELECT COUNT(*) 'total'`,
19119        // `SELECT 1 'a b'` (which is why one quotes it). SPG answered
19120        // `syntax error at or near "'x'"` to all of them.
19121        //
19122        // Only here, not in `parse_optional_alias`: that one also names
19123        // TABLES, and MySQL 9.7.2 refuses a string there — `FROM t 'ta'`
19124        // and `FROM t AS 'ta'` are both syntax errors, measured. And only
19125        // after the lexer's own rule has joined adjacent literals, or
19126        // `SELECT 'a' 'b'` would read as a literal aliased `b` where
19127        // MySQL answers the concatenation `ab`.
19128        if self.mysql_dialect {
19129            let at_as = matches!(self.peek(), Token::As)
19130                && matches!(self.tokens.get(self.pos + 1), Some(Token::String(_)));
19131            if at_as {
19132                self.advance();
19133            }
19134            if let Token::String(name) = self.peek().clone() {
19135                self.advance();
19136                return Ok(SelectItem::Expr {
19137                    expr,
19138                    alias: Some(name),
19139                });
19140            }
19141        }
19142        let alias = match self.parse_optional_alias()? {
19143            Some(a) => Some(a),
19144            None => self.mysql_item_label(&expr, start_tok, end_tok),
19145        };
19146        Ok(SelectItem::Expr { expr, alias })
19147    }
19148
19149    /// v7.39 (round 506) — the name MariaDB 11 gives a projection item that
19150    /// carries no `AS`, filled in here so every downstream path reports it
19151    /// without knowing the rule. `None` leaves the item un-aliased, which is
19152    /// what a PG session always gets.
19153    ///
19154    /// Measured against MariaDB 11, three rules and no more:
19155    ///
19156    /// | item             | label      | why                          |
19157    /// |------------------|------------|------------------------------|
19158    /// | `lbl.a`          | `a`        | a column reports its name    |
19159    /// | `'it''s'`        | `it's`     | a string reports its VALUE   |
19160    /// | `a  +  b`        | `a  +  b`  | anything else, source text   |
19161    ///
19162    /// The third is why this lives in the parser at all: the label is the
19163    /// text the client WROTE, down to the spacing, so it cannot be printed
19164    /// back out of the parsed shape. `COUNT( * )` names itself `COUNT( * )`.
19165    ///
19166    /// Comments survive, and that is right: through a `mariadb` CLI both
19167    /// servers answer `a  + b` for `SELECT a /* c */ + b`, but that is the
19168    /// CLIENT stripping the comment before it sends. Asked over the raw
19169    /// protocol, MariaDB answers `a /* c */ + b` — byte for byte what this
19170    /// produces.
19171    fn mysql_item_label(&self, expr: &Expr, start_tok: usize, end_tok: usize) -> Option<String> {
19172        if !self.mysql_dialect {
19173            return None;
19174        }
19175        match expr {
19176            // A column already reports its own name downstream; naming it
19177            // again here would only re-state the qualifier the label drops.
19178            Expr::Column(_) => None,
19179            // v7.39.3 — `SELECT 'a' 'b'` is ONE literal whose value is
19180            // `ab`, and MySQL 9.7.2 names the column `a`: the label is
19181            // the first segment as written, not the joined value
19182            // (measured). The lexer logs where it joined them.
19183            Expr::Literal(Literal::String(v)) => Some(
19184                self.merged_first_len(start_tok)
19185                    .and_then(|n| v.get(..n))
19186                    .map_or_else(|| v.clone(), String::from),
19187            ),
19188            _ => self.source_span(start_tok, end_tok).map(str::to_string),
19189        }
19190    }
19191
19192    /// v7.37.17 (17.6 siblings) — parse `(row), (row), …` after a
19193    /// consumed VALUES keyword. Each row lowers to a constant SELECT
19194    /// with PG's default column1..columnN names; subsequent rows
19195    /// chain as UNION ALL peers. Shared by the FROM-position
19196    /// `( VALUES … )` arm and the top-level bare VALUES statement.
19197    fn parse_values_rows_body(&mut self) -> Result<SelectStatement, ParseError> {
19198        let mut row_selects: Vec<SelectStatement> = Vec::new();
19199        loop {
19200            if !matches!(self.peek(), Token::LParen) {
19201                return Err(self.err(alloc::format!(
19202                    "expected '(' to start a VALUES row, got {:?}",
19203                    self.peek()
19204                )));
19205            }
19206            self.advance(); // (
19207            let mut items: Vec<SelectItem> = Vec::new();
19208            loop {
19209                let expr = self.parse_expr(0)?;
19210                items.push(SelectItem::Expr {
19211                    expr,
19212                    alias: Some(alloc::format!("column{}", items.len() + 1)),
19213                });
19214                match self.peek() {
19215                    Token::Comma => {
19216                        self.advance();
19217                    }
19218                    Token::RParen => break,
19219                    other => {
19220                        return Err(self.err(alloc::format!(
19221                            "expected ',' or ')' in VALUES row, got {other:?}"
19222                        )));
19223                    }
19224                }
19225            }
19226            self.advance(); // )
19227            row_selects.push(SelectStatement {
19228                locking: None,
19229                ctes: Vec::new(),
19230                distinct: false,
19231                distinct_on: Vec::new(),
19232                items,
19233                from: None,
19234                where_: None,
19235                group_by: None,
19236                group_by_all: false,
19237                having: None,
19238                unions: Vec::new(),
19239                order_by: Vec::new(),
19240                limit: None,
19241                offset: None,
19242                limit_with_ties: false,
19243                window_check_exprs: Vec::new(),
19244            });
19245            if matches!(self.peek(), Token::Comma) {
19246                self.advance();
19247                continue;
19248            }
19249            break;
19250        }
19251        let mut head = row_selects.remove(0);
19252        head.unions = row_selects
19253            .into_iter()
19254            .map(|s| (UnionKind::All, s))
19255            .collect();
19256        Ok(head)
19257    }
19258
19259    fn parse_table_ref(&mut self) -> Result<TableRef, ParseError> {
19260        // v7.39 (round 621) — `FROM ONLY <table>` excludes a table's
19261        // children. It was read as a table NAMED `only`, so the query
19262        // failed on `relation "only" does not exist`.
19263        //
19264        // v7.39 (round 644) — and it is no longer a no-op. Round 621
19265        // absorbed the keyword, reasoning that SPG's children are
19266        // separate relations a plain scan does not descend into, so ONLY
19267        // already described the scan. That stopped being true when a
19268        // partition parent started unioning its children: measured,
19269        // `SELECT count(*) FROM ONLY <partitioned parent>` answered 2
19270        // where PG answers 0. The flag is carried now.
19271        let mut only = false;
19272        if matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("only"))
19273            && matches!(
19274                self.tokens.get(self.pos + 1),
19275                Some(Token::Ident(_) | Token::QuotedIdent(_))
19276            )
19277        {
19278            only = true;
19279            self.advance();
19280        }
19281        // `LATERAL generate_series(...)` / `LATERAL unnest(...)` —
19282        // for these SRFs the keyword is noise at parse time: the
19283        // join executor already substitutes outer-column references
19284        // into unnest_expr / generate_series_args per outer row
19285        // (v7.37.43-T4.5 substitute_outer_in_table_ref), and PG
19286        // licences the correlation even without the keyword. Absorb
19287        // it and fall through to the SRF arms below.
19288        // v7.39 (read01 round 69) — `LATERAL <fn>(args)` for ANY function, not
19289        // just the four builtin SRFs: a user set-returning function on a JOIN's
19290        // right side is the whole point of LATERAL. The keyword stays noise at
19291        // parse time — the join executor substitutes the outer row into the
19292        // call's arguments per outer row.
19293        if matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("lateral"))
19294            && matches!(
19295                self.tokens.get(self.pos + 1),
19296                // The json_each family has its OWN `LATERAL …` arm below, which
19297                // needs to see the keyword — absorbing it here would send those
19298                // calls down the generic table-function channel instead.
19299                Some(Token::Ident(s) | Token::QuotedIdent(s)) if !is_json_each_name(s)
19300            )
19301            && matches!(self.tokens.get(self.pos + 2), Some(Token::LParen))
19302        {
19303            self.advance(); // LATERAL
19304        }
19305        // v7.37.43-T4.5 — `LATERAL jsonb_each_text(<expr>)` —
19306        // set-returning function whose argument may reference a
19307        // preceding FROM item. We rewrite this to
19308        // `LATERAL (SELECT key, value FROM jsonb_each_text(<expr>)
19309        // AS __srf__) AS <alias>` so the existing LATERAL subquery
19310        // executor handles per-outer-row evaluation and the
19311        // SRF-primary jsonb_each_text path handles the inner
19312        // materialisation. Sentori 0067 backfill is the dogfood
19313        // shape.
19314        if matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("lateral"))
19315            && matches!(self.tokens.get(self.pos + 1), Some(Token::Ident(s) | Token::QuotedIdent(s)) if is_json_each_name(s))
19316            && matches!(self.tokens.get(self.pos + 2), Some(Token::LParen))
19317        {
19318            self.advance(); // LATERAL
19319            let each_fn = match self.peek() {
19320                Token::Ident(s) | Token::QuotedIdent(s) => s.to_ascii_lowercase(),
19321                _ => unreachable!(),
19322            };
19323            self.advance(); // jsonb_each[_text] / json_each[_text]
19324            self.advance(); // (
19325            let arg = self.parse_expr(0)?;
19326            if !matches!(self.peek(), Token::RParen) {
19327                return Err(self.err(alloc::format!(
19328                    "expected ')' after LATERAL {each_fn}() argument, got {:?}",
19329                    self.peek()
19330                )));
19331            }
19332            self.advance();
19333            let (alias_ident, column_aliases) = self.parse_optional_alias_with_columns()?;
19334            let alias = alias_ident.clone().unwrap_or_else(|| each_fn.clone());
19335            // Synthesise: SELECT __srf__.key AS <key_alias>, __srf__.value AS <value_alias>
19336            //               FROM jsonb_each_text(<arg>) AS __srf__
19337            // PG's `AS kv(key, value)` column-alias list maps
19338            // positions to names; default to (key, value) when
19339            // omitted (matching the SRF's natural column names).
19340            let srf_alias = "__srf__".to_string();
19341            let key_alias = column_aliases
19342                .first()
19343                .cloned()
19344                .unwrap_or_else(|| "key".to_string());
19345            let value_alias = column_aliases
19346                .get(1)
19347                .cloned()
19348                .unwrap_or_else(|| "value".to_string());
19349            let inner_select = crate::ast::SelectStatement {
19350                locking: None,
19351                ctes: Vec::new(),
19352                distinct: false,
19353                distinct_on: Vec::new(),
19354                items: alloc::vec![
19355                    crate::ast::SelectItem::Expr {
19356                        expr: crate::ast::Expr::Column(crate::ast::ColumnName {
19357                            qualifier: Some(srf_alias.clone()),
19358                            name: "key".to_string(),
19359                        }),
19360                        alias: Some(key_alias),
19361                    },
19362                    crate::ast::SelectItem::Expr {
19363                        expr: crate::ast::Expr::Column(crate::ast::ColumnName {
19364                            qualifier: Some(srf_alias.clone()),
19365                            name: "value".to_string(),
19366                        }),
19367                        alias: Some(value_alias),
19368                    },
19369                ],
19370                from: Some(crate::ast::FromClause {
19371                    primary: TableRef {
19372                        name: srf_alias.clone(),
19373                        alias: Some(srf_alias.clone()),
19374                        only: false,
19375                        as_of_segment: None,
19376                        unnest_expr: None,
19377                        unnest_column_aliases: Vec::new(),
19378                        with_ordinality: false,
19379                        generate_series_args: None,
19380                        lateral_subquery: None,
19381                        jsonb_each_text_arg: Some((each_fn, Box::new(arg))),
19382                        table_fn_call: None,
19383                        rows_from: None,
19384                        json_table: None,
19385                        scalar_fn_item: false,
19386                    },
19387                    joins: Vec::new(),
19388                }),
19389                where_: None,
19390                group_by: None,
19391                group_by_all: false,
19392                having: None,
19393                unions: Vec::new(),
19394                order_by: Vec::new(),
19395                limit: None,
19396                offset: None,
19397                limit_with_ties: false,
19398                window_check_exprs: Vec::new(),
19399            };
19400            return Ok(TableRef {
19401                name: alias.clone(),
19402                alias: Some(alias),
19403                only: false,
19404                as_of_segment: None,
19405                unnest_expr: None,
19406                unnest_column_aliases: Vec::new(),
19407                with_ordinality: false,
19408                generate_series_args: None,
19409                lateral_subquery: Some(Box::new(inner_select)),
19410                jsonb_each_text_arg: None,
19411                table_fn_call: None,
19412                rows_from: None,
19413                json_table: None,
19414                scalar_fn_item: false,
19415            });
19416        }
19417        // v7.37.43-T4.5 — bare `CROSS JOIN jsonb_each_text(t.col)`
19418        // without an explicit `LATERAL` keyword is the same shape
19419        // PG accepts (SRF naturally licences lateral correlation).
19420        // We mirror the LATERAL rewrite when the argument syntactic-
19421        // ally references an outer column (Column { qualifier:
19422        // Some(_), … }). For simplicity we apply the rewrite
19423        // whenever the SRF directly follows JOIN/CROSS JOIN/comma
19424        // in the FROM-list — caller-side join parsing positions
19425        // this peek correctly.
19426        // (Implementation note: detection lives below; the LATERAL
19427        // branch above already covers the explicit form; the bare
19428        // form falls through to the plain SRF arm and the engine
19429        // treats it as a constant-arg SRF if no outer reference is
19430        // present.)
19431        // v7.17.0 Phase 3.P0-41 — `LATERAL ( SELECT … )` derived
19432        // table. Detect at the head so it claims precedence over
19433        // every other table-ref shape (unnest / generate_series /
19434        // bare ident); the lateral subquery itself follows the
19435        // regular SELECT grammar.
19436        // v7.37.17 (17.6 siblings) — `FROM ( VALUES (…), (…) ) [AS]
19437        // t(cols)`. Each row lowers to a constant SELECT with PG's
19438        // default column1..columnN names; subsequent rows chain as
19439        // UNION ALL peers. The result rides the derived-table
19440        // lateral_subquery channel — zero executor work.
19441        if matches!(self.peek(), Token::LParen)
19442            && matches!(self.tokens.get(self.pos + 1), Some(Token::Values))
19443        {
19444            self.advance(); // (
19445            self.advance(); // VALUES
19446            let head = self.parse_values_rows_body()?;
19447            if !matches!(self.peek(), Token::RParen) {
19448                return Err(self.err(alloc::format!(
19449                    "expected ')' after VALUES list, got {:?}",
19450                    self.peek()
19451                )));
19452            }
19453            self.advance();
19454            let (alias_ident, column_aliases) = self.parse_optional_alias_with_columns()?;
19455            let name = alias_ident.clone().unwrap_or_else(|| "values".to_string());
19456            return Ok(TableRef {
19457                name,
19458                alias: alias_ident,
19459                only: false,
19460                as_of_segment: None,
19461                unnest_expr: None,
19462                unnest_column_aliases: column_aliases,
19463                with_ordinality: false,
19464                generate_series_args: None,
19465                lateral_subquery: Some(Box::new(head)),
19466                jsonb_each_text_arg: None,
19467                table_fn_call: None,
19468                rows_from: None,
19469                json_table: None,
19470                scalar_fn_item: false,
19471            });
19472        }
19473        // v7.37.17 (17.6 siblings) — plain derived table:
19474        // `FROM ( SELECT … ) [AS] alias`. Rides the same
19475        // lateral_subquery channel the explicit LATERAL form uses —
19476        // an uncorrelated inner SELECT executes identically. The
19477        // inner parse carries UNION tails (they live on
19478        // SelectStatement.unions).
19479        // v7.37 D.20 — the derived-table inner may itself be a
19480        // parenthesized set-operation group (`FROM ((SELECT…) UNION
19481        // (SELECT…)) s`) or a CTE (`FROM (WITH … SELECT …) z`), not just a
19482        // bare `(SELECT …)`. parse_one_statement already routes a leading
19483        // `(` set-op group (its LParen arm) and a leading WITH
19484        // (parse_with_cte_then_select), so widen the second-token gate to
19485        // Select | LParen | WITH.
19486        // v7.39 (round 869) — `Table` joins that gate. `TABLE t` is
19487        // PG's spelling of `SELECT * FROM t` and is accepted wherever a
19488        // SELECT is, so `FROM (TABLE t) x` has to parse. The desugaring
19489        // has existed since the shorthand landed and `parse_bare_select`
19490        // already routes it ("valid anywhere a SELECT head is"); what was
19491        // missing is this second-token gate, and the CTE body's dispatch
19492        // below. Round 868 found both by putting the shorthand in a
19493        // subquery — the top-level forms had been the only ones tested.
19494        if matches!(self.peek(), Token::LParen)
19495            && (matches!(
19496                self.tokens.get(self.pos + 1),
19497                Some(Token::Select | Token::LParen | Token::Table)
19498            ) || matches!(self.tokens.get(self.pos + 1),
19499                    Some(Token::Ident(s) | Token::QuotedIdent(s)) if s.eq_ignore_ascii_case("with")))
19500        {
19501            self.advance(); // (
19502            let inner = match self.parse_one_statement()? {
19503                Statement::Select(s) => s,
19504                other => {
19505                    return Err(self.err(alloc::format!(
19506                        "expected SELECT inside derived table ( … ), got {other:?}"
19507                    )));
19508                }
19509            };
19510            if !matches!(self.peek(), Token::RParen) {
19511                return Err(self.err(alloc::format!(
19512                    "expected ')' after derived-table subquery, got {:?}",
19513                    self.peek()
19514                )));
19515            }
19516            self.advance();
19517            // `AS t(a, b)` column-alias list rides the
19518            // unnest_column_aliases field (same positional-rename
19519            // contract the unnest SRFs use).
19520            let (alias_ident, column_aliases) = self.parse_optional_alias_with_columns()?;
19521            let name = alias_ident
19522                .clone()
19523                .unwrap_or_else(|| "subquery".to_string());
19524            return Ok(TableRef {
19525                name,
19526                alias: alias_ident,
19527                only: false,
19528                as_of_segment: None,
19529                unnest_expr: None,
19530                unnest_column_aliases: column_aliases,
19531                with_ordinality: false,
19532                generate_series_args: None,
19533                lateral_subquery: Some(Box::new(inner)),
19534                jsonb_each_text_arg: None,
19535                table_fn_call: None,
19536                rows_from: None,
19537                json_table: None,
19538                scalar_fn_item: false,
19539            });
19540        }
19541        if matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("lateral"))
19542            && matches!(self.tokens.get(self.pos + 1), Some(Token::LParen))
19543        {
19544            self.advance(); // LATERAL
19545            self.advance(); // (
19546            // Parse the inner SELECT.
19547            let inner = match self.parse_one_statement()? {
19548                Statement::Select(s) => s,
19549                other => {
19550                    return Err(self.err(alloc::format!(
19551                        "expected SELECT inside LATERAL ( … ), got {other:?}"
19552                    )));
19553                }
19554            };
19555            if !matches!(self.peek(), Token::RParen) {
19556                return Err(self.err(alloc::format!(
19557                    "expected ')' after LATERAL subquery, got {:?}",
19558                    self.peek()
19559                )));
19560            }
19561            self.advance();
19562            // v7.37 D.28 — `LATERAL (…) AS t(cols)` column-alias list (also how a
19563            // `(VALUES …) t(g)` derived table round-trips through view-body
19564            // Display, which renders on the lateral_subquery channel).
19565            let (alias_ident, column_aliases) = self.parse_optional_alias_with_columns()?;
19566            let name = alias_ident.clone().unwrap_or_else(|| "lateral".to_string());
19567            return Ok(TableRef {
19568                name,
19569                alias: alias_ident,
19570                only: false,
19571                as_of_segment: None,
19572                unnest_expr: None,
19573                unnest_column_aliases: column_aliases,
19574                with_ordinality: false,
19575                generate_series_args: None,
19576                lateral_subquery: Some(Box::new(inner)),
19577                jsonb_each_text_arg: None,
19578                table_fn_call: None,
19579                rows_from: None,
19580                json_table: None,
19581                scalar_fn_item: false,
19582            });
19583        }
19584        // v7.37.43-T4.5 — `jsonb_each_text(<expr>)` set-returning
19585        // function as a FROM item. Emits one row per (key, value)
19586        // pair in the JSONB object argument as TEXT columns. May
19587        // be wrapped in CROSS JOIN LATERAL when the argument
19588        // references a preceding FROM item (sentori migration
19589        // 0067 backfill shape: `CROSS JOIN LATERAL
19590        // jsonb_each_text(t.json_col) AS kv(key, value)`).
19591        if matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s) if is_json_each_name(s))
19592            && matches!(self.tokens.get(self.pos + 1), Some(Token::LParen))
19593        {
19594            let each_fn = match self.peek() {
19595                Token::Ident(s) | Token::QuotedIdent(s) => s.to_ascii_lowercase(),
19596                _ => unreachable!(),
19597            };
19598            self.advance(); // jsonb_each[_text] / json_each[_text]
19599            self.advance(); // (
19600            let arg = self.parse_expr(0)?;
19601            if !matches!(self.peek(), Token::RParen) {
19602                return Err(self.err(alloc::format!(
19603                    "expected ')' after {each_fn}() argument, got {:?}",
19604                    self.peek()
19605                )));
19606            }
19607            self.advance();
19608            let (alias_ident, column_aliases) = self.parse_optional_alias_with_columns()?;
19609            let name = alias_ident.clone().unwrap_or_else(|| each_fn.clone());
19610            return Ok(TableRef {
19611                name,
19612                alias: alias_ident,
19613                only: false,
19614                as_of_segment: None,
19615                unnest_expr: None,
19616                // `AS t(k, v)` renames key/value positionally, same as the
19617                // LATERAL-position form already does.
19618                unnest_column_aliases: column_aliases,
19619                with_ordinality: false,
19620                generate_series_args: None,
19621                lateral_subquery: None,
19622                jsonb_each_text_arg: Some((each_fn, Box::new(arg))),
19623                table_fn_call: None,
19624                rows_from: None,
19625                json_table: None,
19626                scalar_fn_item: false,
19627            });
19628        }
19629        // `jsonb_to_recordset(J) AS t(a int, b text)` / `jsonb_to_record`
19630        // (+ json_ variants) — record-returning JSON functions with a
19631        // column-definition list. Desugar to a derived table that
19632        // projects each declared column from the JSON via `->>` + a cast,
19633        // over `jsonb_array_elements(J)` for the *set (per-element) form.
19634        if matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s) if is_json_to_record_name(s))
19635            && matches!(self.tokens.get(self.pos + 1), Some(Token::LParen))
19636        {
19637            return self.parse_json_to_record_from();
19638        }
19639        // v7.38 (T15) — `regexp_matches(s, pat[, flags])` as a FROM item. Each
19640        // row is a text[] of capture groups, so it cannot desugar to unnest
19641        // (that would flatten the array). Wrap it as a derived table
19642        // `(SELECT regexp_matches(args)) AS <alias>(<col>)` — the SELECT-list
19643        // SRF path already emits one text[] row per match. PG names the column
19644        // `regexp_matches`; an `AS a(col)` alias overrides it.
19645        if matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s)
19646                if s.eq_ignore_ascii_case("regexp_matches"))
19647            && matches!(self.tokens.get(self.pos + 1), Some(Token::LParen))
19648        {
19649            self.advance(); // fn name
19650            self.advance(); // (
19651            let mut fn_args: Vec<Expr> = Vec::new();
19652            loop {
19653                fn_args.push(self.parse_expr(0)?);
19654                if matches!(self.peek(), Token::Comma) {
19655                    self.advance();
19656                    continue;
19657                }
19658                break;
19659            }
19660            if !matches!(self.peek(), Token::RParen) {
19661                return Err(self.err(alloc::format!(
19662                    "expected ')' after regexp_matches() arguments, got {:?}",
19663                    self.peek()
19664                )));
19665            }
19666            self.advance();
19667            // v7.39 (read01 round 78) — WITH ORDINALITY sits BEFORE the alias
19668            // (`f(x) WITH ORDINALITY AS t(v, o)`), and this arm never looked for
19669            // it, so it died on the `with` token while every other table function
19670            // accepted it.
19671            let with_ordinality = self.absorb_with_ordinality();
19672            let (alias_ident, column_aliases) = self.parse_optional_alias_with_columns()?;
19673            let table_alias = alias_ident
19674                .clone()
19675                .unwrap_or_else(|| "regexp_matches".to_string());
19676            // PG names a single-column function's output column after the ALIAS
19677            // when one is given (`FROM regexp_matches(…) AS m` → column `m`), so
19678            // `m` reads as that column and not as a whole-row composite. Naming
19679            // it after the function regardless made `SELECT m[1] FROM … AS m`
19680            // subscript a record.
19681            let col_name = column_aliases
19682                .first()
19683                .cloned()
19684                .or_else(|| alias_ident.clone())
19685                .unwrap_or_else(|| "regexp_matches".to_string());
19686            let inner = crate::ast::SelectStatement {
19687                locking: None,
19688                ctes: Vec::new(),
19689                distinct: false,
19690                distinct_on: Vec::new(),
19691                items: alloc::vec![SelectItem::Expr {
19692                    expr: Expr::FunctionCall {
19693                        name: "regexp_matches".to_string(),
19694                        args: fn_args,
19695                    },
19696                    alias: Some(col_name),
19697                }],
19698                from: None,
19699                where_: None,
19700                group_by: None,
19701                group_by_all: false,
19702                having: None,
19703                unions: Vec::new(),
19704                order_by: Vec::new(),
19705                limit: None,
19706                offset: None,
19707                limit_with_ties: false,
19708                window_check_exprs: Vec::new(),
19709            };
19710            return Ok(TableRef {
19711                name: table_alias.clone(),
19712                alias: Some(table_alias),
19713                only: false,
19714                as_of_segment: None,
19715                unnest_expr: None,
19716                unnest_column_aliases: column_aliases,
19717                with_ordinality,
19718                generate_series_args: None,
19719                lateral_subquery: Some(Box::new(inner)),
19720                jsonb_each_text_arg: None,
19721                table_fn_call: None,
19722                rows_from: None,
19723                json_table: None,
19724                // regexp_matches returns text[], a base type: `SELECT m FROM
19725                // regexp_matches(…) AS m` is the array, not a composite wrapping it.
19726                scalar_fn_item: true,
19727            });
19728        }
19729        // v7.37.17 (17.6 siblings) — `jsonb_array_elements[_text](<expr>)`
19730        // / json_ variants as a FROM item. Rewritten into
19731        // `unnest(<same fn>(<expr>))`: the scalar form returns the
19732        // elements as a TEXT array, and the existing unnest SRF path
19733        // materialises one row per element. PG's natural column name
19734        // is `value`; an `AS a(col)` column-alias list overrides it.
19735        if matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s)
19736                if s.eq_ignore_ascii_case("jsonb_array_elements")
19737                    || s.eq_ignore_ascii_case("json_array_elements")
19738                    || s.eq_ignore_ascii_case("jsonb_array_elements_text")
19739                    || s.eq_ignore_ascii_case("json_array_elements_text")
19740                    || s.eq_ignore_ascii_case("jsonb_object_keys")
19741                    || s.eq_ignore_ascii_case("json_object_keys")
19742                    || s.eq_ignore_ascii_case("jsonb_path_query")
19743                    || s.eq_ignore_ascii_case("json_path_query")
19744                    || s.eq_ignore_ascii_case("generate_subscripts")
19745                    || s.eq_ignore_ascii_case("string_to_table")
19746                    || s.eq_ignore_ascii_case("regexp_split_to_table"))
19747            && matches!(self.tokens.get(self.pos + 1), Some(Token::LParen))
19748        {
19749            let fn_name = match self.peek() {
19750                Token::Ident(s) | Token::QuotedIdent(s) => s.to_ascii_lowercase(),
19751                _ => unreachable!(),
19752            };
19753            self.advance(); // fn name
19754            self.advance(); // (
19755            let mut fn_args: Vec<Expr> = Vec::new();
19756            loop {
19757                fn_args.push(self.parse_expr(0)?);
19758                if matches!(self.peek(), Token::Comma) {
19759                    self.advance();
19760                    continue;
19761                }
19762                break;
19763            }
19764            if !matches!(self.peek(), Token::RParen) {
19765                return Err(self.err(alloc::format!(
19766                    "expected ')' after {fn_name}() arguments, got {:?}",
19767                    self.peek()
19768                )));
19769            }
19770            self.advance();
19771            let with_ordinality = self.absorb_with_ordinality();
19772            let (alias_ident, column_aliases) = self.parse_optional_alias_with_columns()?;
19773            let name = alias_ident.clone().unwrap_or_else(|| fn_name.clone());
19774            // PG's natural column name: the array-elements SRFs
19775            // declare an OUT parameter `value`; jsonb_object_keys
19776            // and generate_subscripts have none, so the column is
19777            // named after the function. A bare table alias on a
19778            // single-column SRF renames the column too (PG: `FROM
19779            // generate_subscripts(a, 1) AS s` projects column s) —
19780            // except for the OUT-parameter SRFs, whose column stays
19781            // `value` under a bare alias.
19782            let natural_col = if fn_name.ends_with("_array_elements")
19783                || fn_name.ends_with("_array_elements_text")
19784            {
19785                "value".to_string()
19786            } else {
19787                alias_ident.clone().unwrap_or_else(|| fn_name.clone())
19788            };
19789            let mut srf_cols = alloc::vec![column_aliases.first().cloned().unwrap_or(natural_col)];
19790            // Keep any further entries — the second names the
19791            // ordinality column under WITH ORDINALITY.
19792            srf_cols.extend(column_aliases.into_iter().skip(1));
19793            // The *_to_table SRFs are row-streams over the existing
19794            // *_to_array scalars — map the call target; the display
19795            // name (alias / column defaults) keeps the SRF spelling.
19796            let call_name = match fn_name.as_str() {
19797                "string_to_table" => "string_to_array".to_string(),
19798                "regexp_split_to_table" => "regexp_split_to_array".to_string(),
19799                _ => fn_name,
19800            };
19801            // v7.38 (read01, T-srf/T-lateral) — an SRF argument that references a
19802            // preceding FROM item (bare or qualified column) is correlated;
19803            // route it through the per-outer-row lateral channel.
19804            let expr = crate::ast::Expr::FunctionCall {
19805                name: call_name,
19806                args: fn_args,
19807            };
19808            let correlated = Self::expr_has_any_column(&expr);
19809            let tref = TableRef {
19810                name,
19811                alias: alias_ident,
19812                only: false,
19813                as_of_segment: None,
19814                unnest_expr: Some(Box::new(expr)),
19815                unnest_column_aliases: srf_cols,
19816                with_ordinality,
19817                generate_series_args: None,
19818                lateral_subquery: None,
19819                jsonb_each_text_arg: None,
19820                table_fn_call: None,
19821                rows_from: None,
19822                json_table: None,
19823                // Each of these returns a BASE type (jsonb / text / int), so the item's
19824                // row type is that scalar: `SELECT j FROM jsonb_array_elements('[1]') j`
19825                // is `1`, not `(1)`. WITH ORDINALITY makes it a real two-column item.
19826                scalar_fn_item: !with_ordinality,
19827            };
19828            return Ok(if correlated {
19829                Self::wrap_correlated_srf(tref)
19830            } else {
19831                tref
19832            });
19833        }
19834        // `ROWS FROM ( srf(args) [, srf(args)]* )` — SQL-standard
19835        // explicit parallel-zip syntax. Each entry lowers to its
19836        // array-returning scalar form (unnest(x) → x itself; the
19837        // FROM-SRF rewrite family → their scalar array calls) and
19838        // the list rides the multi-arg unnest zip channel:
19839        // NULL-padded to the longest, WITH ORDINALITY appends the
19840        // counter. generate_series has no scalar array form and
19841        // errors honestly.
19842        if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("rows"))
19843            && matches!(self.tokens.get(self.pos + 1), Some(Token::From))
19844            && matches!(self.tokens.get(self.pos + 2), Some(Token::LParen))
19845        {
19846            self.advance(); // ROWS
19847            self.advance(); // FROM
19848            self.advance(); // (
19849            let mut entries: Vec<Expr> = Vec::new();
19850            // v7.39 (read01 round 74) — the generic channel, filled in parallel.
19851            // Used only when some entry has no array form.
19852            let mut generic: Vec<(String, Vec<Expr>)> = Vec::new();
19853            loop {
19854                let fn_name = self.expect_ident_like()?.to_ascii_lowercase();
19855                if !matches!(self.peek(), Token::LParen) {
19856                    return Err(self.err(alloc::format!(
19857                        "expected '(' after {fn_name} in ROWS FROM, got {:?}",
19858                        self.peek()
19859                    )));
19860                }
19861                self.advance();
19862                let mut fn_args: Vec<Expr> = Vec::new();
19863                if !matches!(self.peek(), Token::RParen) {
19864                    loop {
19865                        fn_args.push(self.parse_expr(0)?);
19866                        if matches!(self.peek(), Token::Comma) {
19867                            self.advance();
19868                            continue;
19869                        }
19870                        break;
19871                    }
19872                }
19873                if !matches!(self.peek(), Token::RParen) {
19874                    return Err(self.err(alloc::format!(
19875                        "expected ')' after {fn_name}() arguments in ROWS FROM, got {:?}",
19876                        self.peek()
19877                    )));
19878                }
19879                self.advance();
19880                let entry = match fn_name.as_str() {
19881                    "unnest" => {
19882                        if fn_args.len() != 1 {
19883                            return Err(
19884                                self.err("unnest inside ROWS FROM takes exactly one array".into())
19885                            );
19886                        }
19887                        fn_args.pop().expect("len checked")
19888                    }
19889                    "jsonb_array_elements"
19890                    | "json_array_elements"
19891                    | "jsonb_array_elements_text"
19892                    | "json_array_elements_text"
19893                    | "jsonb_object_keys"
19894                    | "json_object_keys"
19895                    | "generate_subscripts" => crate::ast::Expr::FunctionCall {
19896                        name: fn_name,
19897                        args: fn_args,
19898                    },
19899                    "string_to_table" => crate::ast::Expr::FunctionCall {
19900                        name: "string_to_array".to_string(),
19901                        args: fn_args,
19902                    },
19903                    "regexp_split_to_table" => crate::ast::Expr::FunctionCall {
19904                        name: "regexp_split_to_array".to_string(),
19905                        args: fn_args,
19906                    },
19907                    // v7.39 (read01 round 74) — an SRF with no array form
19908                    // (`generate_series`, a user `RETURNS SETOF` function) has no
19909                    // scalar expression to zip, so the WHOLE list switches to the
19910                    // rows_from channel, which runs each function and zips the
19911                    // rows themselves. The all-array case keeps the old lowering:
19912                    // it is well-trodden and this must not disturb it.
19913                    _ => {
19914                        generic.push((fn_name, fn_args));
19915                        if matches!(self.peek(), Token::Comma) {
19916                            self.advance();
19917                            continue;
19918                        }
19919                        break;
19920                    }
19921                };
19922                generic.push((
19923                    // The array-able entries carry their lowered expr along, so a
19924                    // MIXED list still works: the engine sees the scalar array
19925                    // form and unnests it.
19926                    "__array".to_string(),
19927                    alloc::vec![entry.clone()],
19928                ));
19929                entries.push(entry);
19930                if matches!(self.peek(), Token::Comma) {
19931                    self.advance();
19932                    continue;
19933                }
19934                break;
19935            }
19936            if !matches!(self.peek(), Token::RParen) {
19937                return Err(self.err(alloc::format!(
19938                    "expected ')' to close ROWS FROM, got {:?}",
19939                    self.peek()
19940                )));
19941            }
19942            self.advance();
19943            let with_ordinality = self.absorb_with_ordinality();
19944            let (alias_ident, unnest_column_aliases) = self.parse_optional_alias_with_columns()?;
19945            let name = alias_ident.clone().unwrap_or_else(|| "rows".to_string());
19946            // v7.39 (read01 round 74) — some entry had no array form, so the whole
19947            // list rides the generic channel.
19948            if generic.iter().any(|(n, _)| n != "__array") {
19949                let correlated = generic
19950                    .iter()
19951                    .any(|(_, a)| a.iter().any(Self::expr_has_any_column));
19952                let tref = TableRef {
19953                    name,
19954                    alias: alias_ident,
19955                    only: false,
19956                    as_of_segment: None,
19957                    unnest_expr: None,
19958                    unnest_column_aliases,
19959                    with_ordinality,
19960                    generate_series_args: None,
19961                    lateral_subquery: None,
19962                    jsonb_each_text_arg: None,
19963                    table_fn_call: None,
19964                    rows_from: Some(generic),
19965                    json_table: None,
19966                    scalar_fn_item: false,
19967                };
19968                return Ok(if correlated {
19969                    Self::wrap_correlated_srf(tref)
19970                } else {
19971                    tref
19972                });
19973            }
19974            let correlated = entries.iter().any(Self::expr_has_any_column);
19975            let expr = if entries.len() == 1 {
19976                entries.pop().expect("len checked")
19977            } else {
19978                crate::ast::Expr::FunctionCall {
19979                    name: "__unnest_zip".to_string(),
19980                    args: entries,
19981                }
19982            };
19983            let tref = TableRef {
19984                name,
19985                alias: alias_ident,
19986                only: false,
19987                as_of_segment: None,
19988                unnest_expr: Some(Box::new(expr)),
19989                unnest_column_aliases,
19990                with_ordinality,
19991                generate_series_args: None,
19992                lateral_subquery: None,
19993                jsonb_each_text_arg: None,
19994                table_fn_call: None,
19995                rows_from: None,
19996                json_table: None,
19997                scalar_fn_item: false,
19998            };
19999            return Ok(if correlated {
20000                Self::wrap_correlated_srf(tref)
20001            } else {
20002                tref
20003            });
20004        }
20005        // v7.11.7 — `FROM unnest(<expr>) [AS] <alias>` set-returning
20006        // source. Detect at the head before the bare-ident fallback;
20007        // unnest is not a reserved token.
20008        if matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("unnest"))
20009            && matches!(self.tokens.get(self.pos + 1), Some(Token::LParen))
20010        {
20011            self.advance(); // unnest
20012            self.advance(); // (
20013            let mut srf_args = alloc::vec![self.parse_expr(0)?];
20014            while matches!(self.peek(), Token::Comma) {
20015                self.advance();
20016                srf_args.push(self.parse_expr(0)?);
20017            }
20018            if !matches!(self.peek(), Token::RParen) {
20019                return Err(self.err(alloc::format!(
20020                    "expected ')' after unnest() argument, got {:?}",
20021                    self.peek()
20022                )));
20023            }
20024            self.advance();
20025            // Multi-arg unnest(a, b, …) zips the arrays in
20026            // parallel, NULL-padding to the longest (PG's ROWS
20027            // FROM shorthand). Lower onto the unnest channel as an
20028            // internal marker call the executors unpack.
20029            let expr = if srf_args.len() == 1 {
20030                srf_args.pop().expect("len checked")
20031            } else {
20032                crate::ast::Expr::FunctionCall {
20033                    name: "__unnest_zip".to_string(),
20034                    args: srf_args,
20035                }
20036            };
20037            let with_ordinality = self.absorb_with_ordinality();
20038            let (alias_ident, unnest_column_aliases) = self.parse_optional_alias_with_columns()?;
20039            let name = alias_ident.clone().unwrap_or_else(|| "unnest".to_string());
20040            let correlated = Self::expr_has_any_column(&expr);
20041            let tref = TableRef {
20042                name,
20043                alias: alias_ident,
20044                only: false,
20045                as_of_segment: None,
20046                unnest_expr: Some(Box::new(expr)),
20047                unnest_column_aliases,
20048                with_ordinality,
20049                generate_series_args: None,
20050                lateral_subquery: None,
20051                jsonb_each_text_arg: None,
20052                table_fn_call: None,
20053                rows_from: None,
20054                json_table: None,
20055                scalar_fn_item: false,
20056            };
20057            return Ok(if correlated {
20058                Self::wrap_correlated_srf(tref)
20059            } else {
20060                tref
20061            });
20062        }
20063        // v7.39 (round 205, JSON_TABLE epic) — `JSON_TABLE(doc, '$path'
20064        // COLUMNS (...))` has bespoke syntax (a COLUMNS clause the
20065        // generic table-fn arg parser can't read), so it is intercepted
20066        // here BEFORE the generic dispatch. The doc expr may reference
20067        // outer columns (implicit LATERAL) — same correlated-wrap rule.
20068        if matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s)
20069                if s.eq_ignore_ascii_case("json_table"))
20070            && matches!(self.tokens.get(self.pos + 1), Some(Token::LParen))
20071        {
20072            let tref = self.parse_json_table_ref()?;
20073            let correlated = tref
20074                .json_table
20075                .as_deref()
20076                .is_some_and(|jt| Self::expr_has_any_column(&jt.doc));
20077            return Ok(if correlated {
20078                Self::wrap_correlated_srf(tref)
20079            } else {
20080                tref
20081            });
20082        }
20083        // v7.39 (read01 partitionfuncs.c) — generic FROM-position table
20084        // functions dispatched by name (`pg_partition_tree('t')`,
20085        // `pg_partition_ancestors('t')`). Same head-detection shape as
20086        // unnest; the engine executor owns the row shape per function.
20087        // v7.39 (read01 round 65) — and a USER function in FROM position
20088        // (`FROM rows_of(2)`). The SRFs with their own FROM pipeline
20089        // (generate_series / unnest / the json_each family) keep it — their arms
20090        // sit further down, so they are excluded here by name rather than by
20091        // ordering. Anything else that is an ident followed by `(` is a table
20092        // function; the engine executor decides whether it is a builtin, a
20093        // set-returning user function, or an error.
20094        // 7.38.1 S5.1 — pg_dump spells its table functions
20095        // schema-qualified (`pg_catalog.pg_options_to_table(...)`);
20096        // strip the pg_catalog prefix here so the same head-detection
20097        // fires. Only pg_catalog: a user schema's `s.f(x)` keeps its
20098        // meaning.
20099        if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("pg_catalog"))
20100            && matches!(self.tokens.get(self.pos + 1), Some(Token::Dot))
20101            && matches!(
20102                self.tokens.get(self.pos + 2),
20103                Some(Token::Ident(_) | Token::QuotedIdent(_))
20104            )
20105            && matches!(self.tokens.get(self.pos + 3), Some(Token::LParen))
20106        {
20107            self.advance(); // pg_catalog
20108            self.advance(); // .
20109        }
20110        if matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s)
20111                if !s.eq_ignore_ascii_case("generate_series")
20112                    && !s.eq_ignore_ascii_case("unnest")
20113                    && !is_json_each_name(s))
20114            && matches!(self.tokens.get(self.pos + 1), Some(Token::LParen))
20115        {
20116            // Body out-of-line — this parse sits on the FROM/subquery
20117            // recursion chain (debug frame-cliff discipline).
20118            // v7.39 (read01 round 69) — a call whose arguments reference an outer
20119            // column (`t JOIN LATERAL dbl(t.id)`) is CORRELATED: it runs once per
20120            // outer row, so it rides the lateral channel. Same rule the unnest
20121            // arm uses.
20122            let tref = self.parse_table_fn_ref()?;
20123            let correlated = tref
20124                .table_fn_call
20125                .as_deref()
20126                .is_some_and(|(_, args)| args.iter().any(Self::expr_has_any_column));
20127            return Ok(if correlated {
20128                Self::wrap_correlated_srf(tref)
20129            } else {
20130                tref
20131            });
20132        }
20133        // v7.17.0 Phase 3.10 — `FROM generate_series(start, stop
20134        // [, step])` set-returning source. Same shape as unnest:
20135        // detect at the head, parse the comma-separated arg list,
20136        // dispatch downstream through the engine's set-returning
20137        // path. Supports integer triplets (mailrs's `WITH row_no AS
20138        // (SELECT * FROM generate_series(1, N))` pattern) and
20139        // TIMESTAMP + INTERVAL triplets (the Tier-A audit's
20140        // date-range iteration pattern, which pre-3.10 had no
20141        // direct equivalent in SPG).
20142        if matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("generate_series"))
20143            && matches!(self.tokens.get(self.pos + 1), Some(Token::LParen))
20144        {
20145            self.advance(); // generate_series
20146            self.advance(); // (
20147            let mut args: Vec<Expr> = Vec::new();
20148            loop {
20149                args.push(self.parse_expr(0)?);
20150                if matches!(self.peek(), Token::Comma) {
20151                    self.advance();
20152                    continue;
20153                }
20154                break;
20155            }
20156            if !matches!(self.peek(), Token::RParen) {
20157                return Err(self.err(alloc::format!(
20158                    "expected ')' after generate_series() arguments, got {:?}",
20159                    self.peek()
20160                )));
20161            }
20162            self.advance();
20163            if args.len() < 2 || args.len() > 3 {
20164                return Err(self.err(alloc::format!(
20165                    "generate_series() expects 2 or 3 arguments (start, stop [, step]); got {}",
20166                    args.len()
20167                )));
20168            }
20169            let with_ordinality = self.absorb_with_ordinality();
20170            let (alias_ident, column_aliases) = self.parse_optional_alias_with_columns()?;
20171            let name = alias_ident
20172                .clone()
20173                .unwrap_or_else(|| "generate_series".to_string());
20174            let correlated = args.iter().any(Self::expr_has_any_column);
20175            let tref = TableRef {
20176                name,
20177                alias: alias_ident,
20178                only: false,
20179                as_of_segment: None,
20180                unnest_expr: None,
20181                unnest_column_aliases: column_aliases,
20182                with_ordinality,
20183                generate_series_args: Some(args),
20184                lateral_subquery: None,
20185                jsonb_each_text_arg: None,
20186                table_fn_call: None,
20187                rows_from: None,
20188                json_table: None,
20189                scalar_fn_item: false,
20190            };
20191            return Ok(if correlated {
20192                Self::wrap_correlated_srf(tref)
20193            } else {
20194                tref
20195            });
20196        }
20197        // v7.16.2 — preserve information_schema / pg_catalog
20198        // qualifiers (mailrs round-10 A.3). The generic
20199        // `expect_ident_like` strip silently drops the schema;
20200        // we want the engine to recognise these PG meta tables
20201        // and synthesise rows from the live catalog. Produce a
20202        // synthetic name (`__spg_info_columns` etc.) so the
20203        // engine's SELECT-side router can dispatch without
20204        // clashing with any user-defined `columns` table.
20205        let (name, meta_original) = if let Some((synth, orig)) = self.try_peek_meta_qualified() {
20206            (synth, Some(orig))
20207        } else if let Some((synth, orig)) = self.try_peek_meta_bare() {
20208            (synth, Some(orig))
20209        } else {
20210            (self.expect_ident_like()?, None)
20211        };
20212        // v6.10.2 — optional `AS OF SEGMENT '<id>'` cold-tier
20213        // time-travel clause. Parse BEFORE the alias so the
20214        // alias can still ride at the tail (`tbl AS OF SEGMENT
20215        // '5' alias`). `AS` is a reserved keyword token, while
20216        // `OF` and `SEGMENT` are bare idents.
20217        let as_of_segment = if matches!(self.peek(), Token::As)
20218            && matches!(self.tokens.get(self.pos + 1), Some(Token::Ident(s) | Token::QuotedIdent(s)) if s.eq_ignore_ascii_case("of"))
20219        {
20220            self.advance(); // AS
20221            self.advance(); // OF
20222            let kw = match self.peek().clone() {
20223                Token::Ident(s) | Token::QuotedIdent(s) => s,
20224                other => {
20225                    return Err(self.err(format!("expected SEGMENT after AS OF, got {other:?}")));
20226                }
20227            };
20228            if !kw.eq_ignore_ascii_case("segment") {
20229                return Err(self.err(format!(
20230                    "expected SEGMENT after AS OF, got {kw:?}; v6.10.2 supports SEGMENT only"
20231                )));
20232            }
20233            self.advance();
20234            // Segment id literal — accept either a string or
20235            // integer for operator ergonomics.
20236            let id = match self.advance() {
20237                Token::String(s) => s
20238                    .parse::<u32>()
20239                    .map_err(|e| self.err(format!("AS OF SEGMENT id parse: {e}")))?,
20240                Token::Integer(n) => u32::try_from(n)
20241                    .map_err(|e| self.err(format!("AS OF SEGMENT id parse: {e}")))?,
20242                other => {
20243                    return Err(self.err(format!(
20244                        "expected segment id literal after AS OF SEGMENT, got {other:?}"
20245                    )));
20246                }
20247            };
20248            Some(id)
20249        } else {
20250            None
20251        };
20252        // TABLESAMPLE is not a reserved token — keep the bare-ident
20253        // alias rule from swallowing it (`FROM t TABLESAMPLE …`).
20254        let alias = if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("tablesample"))
20255        {
20256            None
20257        } else {
20258            self.parse_optional_alias()?
20259        };
20260        // r1052 — a catalog name rewritten to its synthetic form keeps
20261        // the WRITTEN name as the relation's alias, so `pg_cast.oid`
20262        // still binds after `pg_cast` became `__spg_pg_cast`. PG
20263        // semantics: the visible name of `pg_catalog.pg_cast` IS
20264        // `pg_cast`. Without this, every table-name-qualified column
20265        // on a synthesised catalog answered "missing FROM-clause
20266        // entry" — which is the wall pg_dump hit on its first
20267        // pg_proc/pg_cast query.
20268        let alias = match (&alias, &meta_original) {
20269            (None, Some(orig)) if *orig != name => Some(orig.clone()),
20270            _ => alias,
20271        };
20272        // `TABLESAMPLE BERNOULLI(p) | SYSTEM(p)` follows the alias
20273        // (PG grammar). BERNOULLI lowers to a per-row
20274        // `random() < p/100` conjunct on the enclosing SELECT's
20275        // WHERE — exact row-level Bernoulli semantics. SYSTEM
20276        // shares the lowering: SPG has no page structure to
20277        // sample, and the row-level form returns the same expected
20278        // fraction. REPEATABLE(seed) promises a deterministic
20279        // sample SPG cannot honour yet — honest error rather than
20280        // a silently ignored seed.
20281        if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("tablesample")) {
20282            self.advance();
20283            let method = self.expect_ident_like()?;
20284            if !method.eq_ignore_ascii_case("bernoulli") && !method.eq_ignore_ascii_case("system") {
20285                return Err(self.err(alloc::format!(
20286                    "TABLESAMPLE method {method:?} not supported; use BERNOULLI or SYSTEM"
20287                )));
20288            }
20289            if !matches!(self.peek(), Token::LParen) {
20290                return Err(self.err(alloc::format!(
20291                    "expected '(' after TABLESAMPLE {}, got {:?}",
20292                    method.to_ascii_uppercase(),
20293                    self.peek()
20294                )));
20295            }
20296            self.advance();
20297            let percent = self.parse_expr(0)?;
20298            if !matches!(self.peek(), Token::RParen) {
20299                return Err(self.err(alloc::format!(
20300                    "expected ')' after TABLESAMPLE percentage, got {:?}",
20301                    self.peek()
20302                )));
20303            }
20304            self.advance();
20305            // REPEATABLE(seed) → a deterministic per-row draw seeded by
20306            // `seed`, so the sample is stable across repeats and rescans.
20307            // Non-REPEATABLE keeps the non-deterministic `random()` draw.
20308            let mut sample_seed: Option<Expr> = None;
20309            if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("repeatable")) {
20310                self.advance();
20311                if !matches!(self.peek(), Token::LParen) {
20312                    return Err(self.err(alloc::format!(
20313                        "expected '(' after REPEATABLE, got {:?}",
20314                        self.peek()
20315                    )));
20316                }
20317                self.advance();
20318                let seed = self.parse_expr(0)?;
20319                if !matches!(self.peek(), Token::RParen) {
20320                    return Err(self.err(alloc::format!(
20321                        "expected ')' after REPEATABLE seed, got {:?}",
20322                        self.peek()
20323                    )));
20324                }
20325                self.advance();
20326                sample_seed = Some(seed);
20327            }
20328            let draw = match sample_seed {
20329                Some(seed) => Expr::FunctionCall {
20330                    name: "__tsm_fract".to_string(),
20331                    args: alloc::vec![seed],
20332                },
20333                None => Expr::FunctionCall {
20334                    name: "random".to_string(),
20335                    args: Vec::new(),
20336                },
20337            };
20338            self.pending_sample_preds.push(Expr::Binary {
20339                lhs: Box::new(draw),
20340                op: crate::ast::BinOp::Lt,
20341                rhs: Box::new(Expr::Binary {
20342                    lhs: Box::new(percent),
20343                    op: crate::ast::BinOp::Div,
20344                    rhs: Box::new(Expr::Literal(crate::ast::Literal::Float(100.0))),
20345                }),
20346            });
20347        }
20348        Ok(TableRef {
20349            name,
20350            alias,
20351            only,
20352            as_of_segment,
20353            unnest_expr: None,
20354            unnest_column_aliases: Vec::new(),
20355            with_ordinality: false,
20356            generate_series_args: None,
20357            lateral_subquery: None,
20358            jsonb_each_text_arg: None,
20359            table_fn_call: None,
20360            rows_from: None,
20361            json_table: None,
20362            scalar_fn_item: false,
20363        })
20364    }
20365
20366    /// v7.13.2 — mailrs round-6 S5. Like `parse_optional_alias`
20367    /// but also accepts `AS alias(col [, col, …])` — the
20368    /// PG-standard table-function column-list form. The column
20369    /// list is only honoured when paired with `UNNEST(...)` in
20370    /// the parent; other call sites currently discard it.
20371    /// True when the expression tree contains a qualified column
20372    /// reference (`t.col`) — the syntactic marker that an SRF
20373    /// argument correlates with a preceding FROM item.
20374    fn expr_has_qualified_column(e: &Expr) -> bool {
20375        match e {
20376            Expr::Column(c) => c.qualifier.is_some(),
20377            Expr::Binary { lhs, rhs, .. } => {
20378                Self::expr_has_qualified_column(lhs) || Self::expr_has_qualified_column(rhs)
20379            }
20380            Expr::Unary { expr, .. } => Self::expr_has_qualified_column(expr),
20381            Expr::Cast { expr, .. } => Self::expr_has_qualified_column(expr),
20382            Expr::FunctionCall { args, .. } => args.iter().any(Self::expr_has_qualified_column),
20383            Expr::Case {
20384                operand,
20385                branches,
20386                else_branch,
20387            } => {
20388                operand
20389                    .as_deref()
20390                    .is_some_and(Self::expr_has_qualified_column)
20391                    || branches.iter().any(|(w, t)| {
20392                        Self::expr_has_qualified_column(w) || Self::expr_has_qualified_column(t)
20393                    })
20394                    || else_branch
20395                        .as_deref()
20396                        .is_some_and(Self::expr_has_qualified_column)
20397            }
20398            _ => false,
20399        }
20400    }
20401
20402    /// v7.38 (read01, T-lateral) — like `expr_has_qualified_column` but also
20403    /// counts a bare (unqualified) column. A set-returning function has no
20404    /// input columns of its own, so ANY column in its arguments is an outer
20405    /// (correlated) reference — `generate_series(1, n)` correlates on `n`.
20406    fn expr_has_any_column(e: &Expr) -> bool {
20407        match e {
20408            Expr::Column(_) => true,
20409            Expr::Binary { lhs, rhs, .. } => {
20410                Self::expr_has_any_column(lhs) || Self::expr_has_any_column(rhs)
20411            }
20412            Expr::Unary { expr, .. } => Self::expr_has_any_column(expr),
20413            Expr::Cast { expr, .. } => Self::expr_has_any_column(expr),
20414            Expr::FunctionCall { args, .. } => args.iter().any(Self::expr_has_any_column),
20415            // v7.39 (round 759, F31-B8b) — a column INSIDE an array
20416            // constructor or subscript fell to the `_ => false` arm, so
20417            // `unnest(ARRAY[x, x + 1])` never wrapped into the lateral
20418            // channel and the eager peer eval answered `column "x" does
20419            // not exist` (the substitution walker already recurses both
20420            // shapes; only this detector was blind to them).
20421            Expr::Array(items) => items.iter().any(Self::expr_has_any_column),
20422            Expr::ArraySubscript { target, index } => {
20423                Self::expr_has_any_column(target) || Self::expr_has_any_column(index)
20424            }
20425            Expr::Case {
20426                operand,
20427                branches,
20428                else_branch,
20429            } => {
20430                operand.as_deref().is_some_and(Self::expr_has_any_column)
20431                    || branches
20432                        .iter()
20433                        .any(|(w, t)| Self::expr_has_any_column(w) || Self::expr_has_any_column(t))
20434                    || else_branch
20435                        .as_deref()
20436                        .is_some_and(Self::expr_has_any_column)
20437            }
20438            _ => false,
20439        }
20440    }
20441
20442    /// Wrap a correlated SRF table ref (`unnest(t.col)` /
20443    /// `generate_series(1, t.n)`) into the lateral_subquery
20444    /// channel: `SELECT * FROM <srf>` executes per outer row with
20445    /// outer references substituted (v7.37.43-T4.5 machinery).
20446    /// Uncorrelated SRFs stay on their plain channels.
20447    fn wrap_correlated_srf(srf: TableRef) -> TableRef {
20448        let name = srf.name.clone();
20449        let alias = srf.alias.clone();
20450        let inner = crate::ast::SelectStatement {
20451            locking: None,
20452            ctes: Vec::new(),
20453            distinct: false,
20454            distinct_on: Vec::new(),
20455            items: alloc::vec![crate::ast::SelectItem::Wildcard],
20456            from: Some(crate::ast::FromClause {
20457                primary: srf,
20458                joins: Vec::new(),
20459            }),
20460            where_: None,
20461            group_by: None,
20462            group_by_all: false,
20463            having: None,
20464            unions: Vec::new(),
20465            order_by: Vec::new(),
20466            limit: None,
20467            offset: None,
20468            limit_with_ties: false,
20469            window_check_exprs: Vec::new(),
20470        };
20471        TableRef {
20472            name,
20473            alias,
20474            only: false,
20475            as_of_segment: None,
20476            unnest_expr: None,
20477            unnest_column_aliases: Vec::new(),
20478            with_ordinality: false,
20479            generate_series_args: None,
20480            lateral_subquery: Some(Box::new(inner)),
20481            jsonb_each_text_arg: None,
20482            table_fn_call: None,
20483            rows_from: None,
20484            json_table: None,
20485            scalar_fn_item: false,
20486        }
20487    }
20488
20489    /// True when the expression tree contains an unresolved
20490    /// `OVER w` marker (see parse_over_clause).
20491    fn expr_has_named_window(e: &Expr) -> bool {
20492        match e {
20493            Expr::WindowFunction { partition_by, .. } => matches!(
20494                partition_by.as_slice(),
20495                [Expr::Column(c)] if matches!(
20496                    c.qualifier.as_deref(),
20497                    Some("__named_window__") | Some("__named_window_ref__")
20498                )
20499            ),
20500            Expr::Binary { lhs, rhs, .. } => {
20501                Self::expr_has_named_window(lhs) || Self::expr_has_named_window(rhs)
20502            }
20503            Expr::Unary { expr, .. } | Expr::Cast { expr, .. } => Self::expr_has_named_window(expr),
20504            Expr::FunctionCall { args, .. } => args.iter().any(Self::expr_has_named_window),
20505            Expr::Case {
20506                operand,
20507                branches,
20508                else_branch,
20509            } => {
20510                operand.as_deref().is_some_and(Self::expr_has_named_window)
20511                    || branches.iter().any(|(w, t)| {
20512                        Self::expr_has_named_window(w) || Self::expr_has_named_window(t)
20513                    })
20514                    || else_branch
20515                        .as_deref()
20516                        .is_some_and(Self::expr_has_named_window)
20517            }
20518            _ => false,
20519        }
20520    }
20521
20522    /// v7.39 (round 705) — the NAMES the expression references through the
20523    /// `OVER w` markers, so `parse_bare_select` can tell which WINDOW
20524    /// definitions nothing referenced. Traversal mirrors
20525    /// `expr_has_named_window` above.
20526    fn collect_named_window_refs(e: &Expr, into: &mut Vec<String>) {
20527        match e {
20528            Expr::WindowFunction { partition_by, .. } => {
20529                if let [Expr::Column(c)] = partition_by.as_slice()
20530                    && matches!(
20531                        c.qualifier.as_deref(),
20532                        Some("__named_window__") | Some("__named_window_ref__")
20533                    )
20534                {
20535                    into.push(c.name.clone());
20536                }
20537            }
20538            Expr::Binary { lhs, rhs, .. } => {
20539                Self::collect_named_window_refs(lhs, into);
20540                Self::collect_named_window_refs(rhs, into);
20541            }
20542            Expr::Unary { expr, .. } | Expr::Cast { expr, .. } => {
20543                Self::collect_named_window_refs(expr, into);
20544            }
20545            Expr::FunctionCall { args, .. } => {
20546                for a in args {
20547                    Self::collect_named_window_refs(a, into);
20548                }
20549            }
20550            Expr::Case {
20551                operand,
20552                branches,
20553                else_branch,
20554            } => {
20555                if let Some(o) = operand.as_deref() {
20556                    Self::collect_named_window_refs(o, into);
20557                }
20558                for (w, t) in branches {
20559                    Self::collect_named_window_refs(w, into);
20560                    Self::collect_named_window_refs(t, into);
20561                }
20562                if let Some(eb) = else_branch.as_deref() {
20563                    Self::collect_named_window_refs(eb, into);
20564                }
20565            }
20566            _ => {}
20567        }
20568    }
20569
20570    /// Inline named-window definitions into the `OVER w` markers.
20571    /// An unknown name errors (PG: window "w" does not exist).
20572    #[allow(clippy::type_complexity)]
20573    fn substitute_named_windows(
20574        e: &mut Expr,
20575        defs: &[(
20576            String,
20577            (
20578                Vec<Expr>,
20579                Vec<(Expr, bool, Option<bool>)>,
20580                Option<WindowFrame>,
20581            ),
20582        )],
20583    ) -> Result<(), String> {
20584        match e {
20585            Expr::WindowFunction {
20586                partition_by,
20587                order_by,
20588                frame,
20589                ..
20590            } => {
20591                // `is_copy` distinguishes `OVER (w1 …)` (a refinable copy)
20592                // from the bare `OVER w1` (a plain reference).
20593                let named = match partition_by.as_slice() {
20594                    [Expr::Column(c)] => match c.qualifier.as_deref() {
20595                        Some("__named_window__") => Some((c.name.clone(), false)),
20596                        Some("__named_window_ref__") => Some((c.name.clone(), true)),
20597                        _ => None,
20598                    },
20599                    _ => None,
20600                };
20601                if let Some((wname, is_copy)) = named {
20602                    let Some((_, def)) = defs.iter().find(|(n, _)| n.eq_ignore_ascii_case(&wname))
20603                    else {
20604                        return Err(alloc::format!("window {wname:?} does not exist"));
20605                    };
20606                    if !is_copy {
20607                        *partition_by = def.0.clone();
20608                        *order_by = def.1.clone();
20609                        *frame = def.2.clone();
20610                        return Ok(());
20611                    }
20612                    // v7.39 (round 229) — PG's copy rules, probed against
20613                    // 18.4: a copy inherits the partitioning, may supply an
20614                    // ordering only when the base has none, and may not copy
20615                    // a base that already carries a frame (its own frame
20616                    // would be ambiguous with the inherited one).
20617                    if !def.1.is_empty() && !order_by.is_empty() {
20618                        return Err(alloc::format!(
20619                            "cannot override ORDER BY clause of window \"{wname}\""
20620                        ));
20621                    }
20622                    if def.2.is_some() {
20623                        return Err(alloc::format!(
20624                            "cannot copy window \"{wname}\" because it has a frame clause"
20625                        ));
20626                    }
20627                    *partition_by = def.0.clone();
20628                    if order_by.is_empty() {
20629                        *order_by = def.1.clone();
20630                    }
20631                }
20632                Ok(())
20633            }
20634            Expr::Binary { lhs, rhs, .. } => {
20635                Self::substitute_named_windows(lhs, defs)?;
20636                Self::substitute_named_windows(rhs, defs)
20637            }
20638            Expr::Unary { expr, .. } | Expr::Cast { expr, .. } => {
20639                Self::substitute_named_windows(expr, defs)
20640            }
20641            Expr::FunctionCall { args, .. } => {
20642                for a in args {
20643                    Self::substitute_named_windows(a, defs)?;
20644                }
20645                Ok(())
20646            }
20647            Expr::Case {
20648                operand,
20649                branches,
20650                else_branch,
20651            } => {
20652                if let Some(op) = operand {
20653                    Self::substitute_named_windows(op, defs)?;
20654                }
20655                for (w, t) in branches {
20656                    Self::substitute_named_windows(w, defs)?;
20657                    Self::substitute_named_windows(t, defs)?;
20658                }
20659                if let Some(el) = else_branch {
20660                    Self::substitute_named_windows(el, defs)?;
20661                }
20662                Ok(())
20663            }
20664            _ => Ok(()),
20665        }
20666    }
20667
20668    /// SQL-standard `TABLE name` shorthand — builds the equivalent
20669    /// `SELECT * FROM name` head. Callers own set-op chain / tail
20670    /// composition.
20671    fn parse_table_shorthand(&mut self) -> Result<SelectStatement, ParseError> {
20672        debug_assert!(matches!(self.peek(), Token::Table));
20673        self.advance(); // TABLE
20674        let tname = self.expect_ident_like()?;
20675        Ok(SelectStatement {
20676            locking: None,
20677            ctes: Vec::new(),
20678            distinct: false,
20679            distinct_on: Vec::new(),
20680            items: alloc::vec![SelectItem::Wildcard],
20681            from: Some(FromClause {
20682                primary: TableRef {
20683                    name: tname,
20684                    alias: None,
20685                    only: false,
20686                    as_of_segment: None,
20687                    unnest_expr: None,
20688                    unnest_column_aliases: Vec::new(),
20689                    with_ordinality: false,
20690                    generate_series_args: None,
20691                    lateral_subquery: None,
20692                    jsonb_each_text_arg: None,
20693                    table_fn_call: None,
20694                    rows_from: None,
20695                    json_table: None,
20696                    scalar_fn_item: false,
20697                },
20698                joins: Vec::new(),
20699            }),
20700            where_: None,
20701            group_by: None,
20702            group_by_all: false,
20703            having: None,
20704            unions: Vec::new(),
20705            order_by: Vec::new(),
20706            limit: None,
20707            offset: None,
20708            limit_with_ties: false,
20709            window_check_exprs: Vec::new(),
20710        })
20711    }
20712
20713    /// `jsonb_to_recordset(J) AS t(c1 t1, c2 t2, …)` (and record / json_
20714    /// variants) → a derived table that reads each declared column out of
20715    /// the JSON with `(row ->> 'ci')::ti`. The *set form iterates
20716    /// `jsonb_array_elements(J)` (one row per element, column `value`);
20717    /// the scalar *record form projects a single row straight off `J`.
20718    /// Rides the existing lateral-subquery channel, so no new executor or
20719    /// AST is needed.
20720    fn parse_json_to_record_from(&mut self) -> Result<TableRef, ParseError> {
20721        use crate::ast::{
20722            BinOp, ColumnName, Expr, FromClause, Literal, SelectItem, SelectStatement,
20723        };
20724        let fn_name = match self.peek() {
20725            Token::Ident(s) | Token::QuotedIdent(s) => s.to_ascii_lowercase(),
20726            _ => unreachable!("caller guarded is_json_to_record_name"),
20727        };
20728        self.advance(); // fn name
20729        self.advance(); // (
20730        let mut arg = self.parse_expr(0)?;
20731        // populate_record(base, json): the base only carries the record
20732        // type here — the JSON argument is the second expression.
20733        let mut base: Option<Expr> = None;
20734        if matches!(self.peek(), Token::Comma) {
20735            self.advance();
20736            base = Some(arg);
20737            arg = self.parse_expr(0)?;
20738        }
20739        if !matches!(self.peek(), Token::RParen) {
20740            return Err(self.err(alloc::format!(
20741                "expected ')' after {fn_name}() argument, got {:?}",
20742                self.peek()
20743            )));
20744        }
20745        self.advance(); // )
20746        let is_set = fn_name.ends_with("recordset");
20747        // `[AS] alias ( col type [, …] )` column-definition list.
20748        if matches!(self.peek(), Token::As) {
20749            self.advance();
20750        }
20751        let alias_opt = match self.peek() {
20752            Token::Ident(s) | Token::QuotedIdent(s) => {
20753                let a = s.clone();
20754                self.advance();
20755                Some(a)
20756            }
20757            _ => None,
20758        };
20759        // v7.39 (read01 round 76) — the populate family's canonical PG
20760        // spelling carries no column list at all: the row shape comes from
20761        // the BASE argument's declared type (`jsonb_populate_record(NULL::t,
20762        // j)`). The parser has no catalog, so hand the two arguments to the
20763        // engine's table-function channel, which does. Only `*_to_record*`
20764        // (whose base is bare `record`) genuinely requires the list.
20765        if !matches!(self.peek(), Token::LParen) {
20766            if let Some(base_expr) = base {
20767                let alias = alias_opt.unwrap_or_else(|| fn_name.clone());
20768                return Ok(TableRef {
20769                    name: alias.clone(),
20770                    alias: Some(alias),
20771                    only: false,
20772                    as_of_segment: None,
20773                    unnest_expr: None,
20774                    unnest_column_aliases: Vec::new(),
20775                    with_ordinality: false,
20776                    generate_series_args: None,
20777                    lateral_subquery: None,
20778                    jsonb_each_text_arg: None,
20779                    table_fn_call: Some(Box::new((fn_name, alloc::vec![base_expr, arg]))),
20780                    rows_from: None,
20781                    json_table: None,
20782                    scalar_fn_item: false,
20783                });
20784            }
20785            return Err(self.err(alloc::format!(
20786                "expected '(' to start the {fn_name} column-definition list, got {:?}",
20787                self.peek()
20788            )));
20789        }
20790        let Some(alias) = alias_opt else {
20791            return Err(self.err(alloc::format!(
20792                "{fn_name}(...) needs a column-definition list, e.g. AS t(a int, b text)"
20793            )));
20794        };
20795        self.advance(); // (
20796        let mut coldefs: Vec<(String, crate::ast::CastTarget)> = Vec::new();
20797        loop {
20798            let col = self.expect_ident_like()?;
20799            let ty = self.parse_cast_target()?;
20800            coldefs.push((col, ty));
20801            if matches!(self.peek(), Token::Comma) {
20802                self.advance();
20803                continue;
20804            }
20805            if matches!(self.peek(), Token::RParen) {
20806                self.advance();
20807                break;
20808            }
20809            return Err(self.err(alloc::format!(
20810                "expected ',' or ')' in {fn_name} column list, got {:?}",
20811                self.peek()
20812            )));
20813        }
20814        if coldefs.is_empty() {
20815            return Err(self.err(alloc::format!(
20816                "{fn_name} column-definition list must declare at least one column"
20817            )));
20818        }
20819        // Per column: (base ->> 'col')::type AS col. The base is the
20820        // per-element `value` column for the *set form, or the argument
20821        // itself for the scalar record form.
20822        let items: Vec<SelectItem> = coldefs
20823            .into_iter()
20824            .map(|(col, ty)| {
20825                let base = if is_set {
20826                    Expr::Column(ColumnName {
20827                        qualifier: None,
20828                        name: "value".to_string(),
20829                    })
20830                } else {
20831                    arg.clone()
20832                };
20833                SelectItem::Expr {
20834                    expr: Expr::Cast {
20835                        expr: Box::new(Expr::Binary {
20836                            lhs: Box::new(base),
20837                            op: BinOp::JsonGetText,
20838                            rhs: Box::new(Expr::Literal(Literal::String(col.clone()))),
20839                        }),
20840                        target: ty,
20841                    },
20842                    alias: Some(col),
20843                }
20844            })
20845            .collect();
20846        let from = if is_set {
20847            let elem_fn = if fn_name.starts_with("jsonb") {
20848                "jsonb_array_elements"
20849            } else {
20850                "json_array_elements"
20851            };
20852            Some(FromClause {
20853                primary: TableRef {
20854                    name: "value".to_string(),
20855                    alias: None,
20856                    only: false,
20857                    as_of_segment: None,
20858                    unnest_expr: Some(Box::new(Expr::FunctionCall {
20859                        name: elem_fn.to_string(),
20860                        args: alloc::vec![arg],
20861                    })),
20862                    unnest_column_aliases: alloc::vec!["value".to_string()],
20863                    with_ordinality: false,
20864                    generate_series_args: None,
20865                    lateral_subquery: None,
20866                    jsonb_each_text_arg: None,
20867                    table_fn_call: None,
20868                    rows_from: None,
20869                    json_table: None,
20870                    scalar_fn_item: false,
20871                },
20872                joins: Vec::new(),
20873            })
20874        } else {
20875            None
20876        };
20877        let inner = SelectStatement {
20878            locking: None,
20879            ctes: Vec::new(),
20880            distinct: false,
20881            distinct_on: Vec::new(),
20882            items,
20883            from,
20884            where_: None,
20885            group_by: None,
20886            group_by_all: false,
20887            having: None,
20888            unions: Vec::new(),
20889            order_by: Vec::new(),
20890            limit: None,
20891            offset: None,
20892            limit_with_ties: false,
20893            window_check_exprs: Vec::new(),
20894        };
20895        Ok(TableRef {
20896            name: alias.clone(),
20897            alias: Some(alias),
20898            only: false,
20899            as_of_segment: None,
20900            unnest_expr: None,
20901            unnest_column_aliases: Vec::new(),
20902            with_ordinality: false,
20903            generate_series_args: None,
20904            lateral_subquery: Some(Box::new(inner)),
20905            jsonb_each_text_arg: None,
20906            table_fn_call: None,
20907            rows_from: None,
20908            json_table: None,
20909            scalar_fn_item: false,
20910        })
20911    }
20912
20913    /// Absorb `WITH ORDINALITY` after an SRF call in FROM position.
20914    /// Returns true when the clause was present. `WITH` alone (a
20915    /// CTE can never start here) is not enough — the ORDINALITY
20916    /// ident must follow, so a stray WITH still errors downstream.
20917    fn absorb_with_ordinality(&mut self) -> bool {
20918        if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("with"))
20919            && matches!(self.tokens.get(self.pos + 1),
20920                Some(Token::Ident(s)) if s.eq_ignore_ascii_case("ordinality"))
20921        {
20922            self.advance();
20923            self.advance();
20924            true
20925        } else {
20926            false
20927        }
20928    }
20929
20930    /// v7.39 (read01 partitionfuncs.c) — parse a FROM-position table
20931    /// function reference (`pg_partition_tree('t') [AS a(c, …)]`).
20932    /// Out-of-line: the caller sits on the FROM recursion chain.
20933    #[inline(never)]
20934    fn parse_table_fn_ref(&mut self) -> Result<TableRef, ParseError> {
20935        let fn_name = match self.advance() {
20936            Token::Ident(s) | Token::QuotedIdent(s) => s.to_ascii_lowercase(),
20937            _ => unreachable!("caller peeked an ident"),
20938        };
20939        self.advance(); // (
20940        let mut args: Vec<Expr> = Vec::new();
20941        if !matches!(self.peek(), Token::RParen) {
20942            loop {
20943                args.push(self.parse_expr(0)?);
20944                if matches!(self.peek(), Token::Comma) {
20945                    self.advance();
20946                    continue;
20947                }
20948                break;
20949            }
20950        }
20951        if !matches!(self.peek(), Token::RParen) {
20952            return Err(self.err(alloc::format!(
20953                "expected ')' after {fn_name}() arguments, got {:?}",
20954                self.peek()
20955            )));
20956        }
20957        self.advance();
20958        // v7.39 (read01 round 68) — `f(args) WITH ORDINALITY AS a(x, n)`: the
20959        // counter column rides after the function's own, and the alias list
20960        // names it.
20961        let with_ordinality = self.absorb_with_ordinality();
20962        let (alias_ident, unnest_column_aliases) = self.parse_optional_alias_with_columns()?;
20963        let name = alias_ident.clone().unwrap_or_else(|| fn_name.clone());
20964        Ok(TableRef {
20965            name,
20966            alias: alias_ident,
20967            only: false,
20968            as_of_segment: None,
20969            unnest_expr: None,
20970            unnest_column_aliases,
20971            with_ordinality,
20972            generate_series_args: None,
20973            lateral_subquery: None,
20974            jsonb_each_text_arg: None,
20975            table_fn_call: Some(Box::new((fn_name, args))),
20976            rows_from: None,
20977            json_table: None,
20978            scalar_fn_item: false,
20979        })
20980    }
20981
20982    /// v7.39 (round 205, JSON_TABLE) — parse
20983    /// `JSON_TABLE(<doc>, '<row_path>' [PASSING …] COLUMNS (<coldefs>))
20984    /// [AS <alias>]`. The COLUMNS list is a recursive tree (NESTED
20985    /// PATH nests another COLUMNS). Out-of-line (FROM recursion chain).
20986    #[inline(never)]
20987    fn parse_json_table_ref(&mut self) -> Result<TableRef, ParseError> {
20988        self.advance(); // json_table
20989        self.advance(); // (
20990        let doc = Box::new(self.parse_expr(0)?);
20991        self.expect_comma_json_table()?;
20992        let row_path = self.parse_json_string_literal("JSON_TABLE row path")?;
20993        // Optional `PASSING <expr> AS <name> [, …]`.
20994        let mut passing: Vec<(String, Expr)> = Vec::new();
20995        if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("passing")) {
20996            self.advance();
20997            loop {
20998                let e = self.parse_expr(0)?;
20999                if !matches!(self.peek(), Token::As) {
21000                    return Err(self.err("expected AS after JSON_TABLE PASSING value".into()));
21001                }
21002                self.advance();
21003                let vname = match self.advance() {
21004                    Token::Ident(s) | Token::QuotedIdent(s) => s,
21005                    other => {
21006                        return Err(self.err(alloc::format!(
21007                            "expected PASSING variable name, got {other:?}"
21008                        )));
21009                    }
21010                };
21011                passing.push((vname, e));
21012                if matches!(self.peek(), Token::Comma) {
21013                    self.advance();
21014                    continue;
21015                }
21016                break;
21017            }
21018        }
21019        if !matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("columns")) {
21020            return Err(self.err("expected COLUMNS in JSON_TABLE".into()));
21021        }
21022        self.advance();
21023        let columns = self.parse_json_table_columns()?;
21024        if !matches!(self.peek(), Token::RParen) {
21025            return Err(self.err(alloc::format!(
21026                "expected ')' to close JSON_TABLE, got {:?}",
21027                self.peek()
21028            )));
21029        }
21030        self.advance();
21031        let alias_ident = self.parse_optional_alias()?;
21032        let name = alias_ident
21033            .clone()
21034            .unwrap_or_else(|| String::from("json_table"));
21035        Ok(TableRef {
21036            name,
21037            alias: alias_ident,
21038            only: false,
21039            as_of_segment: None,
21040            unnest_expr: None,
21041            unnest_column_aliases: Vec::new(),
21042            with_ordinality: false,
21043            generate_series_args: None,
21044            lateral_subquery: None,
21045            jsonb_each_text_arg: None,
21046            table_fn_call: None,
21047            rows_from: None,
21048            json_table: Some(Box::new(crate::ast::JsonTable {
21049                doc,
21050                row_path,
21051                columns,
21052                passing,
21053            })),
21054            scalar_fn_item: false,
21055        })
21056    }
21057
21058    fn expect_comma_json_table(&mut self) -> Result<(), ParseError> {
21059        if !matches!(self.peek(), Token::Comma) {
21060            return Err(self.err(alloc::format!(
21061                "expected ',' after JSON_TABLE document, got {:?}",
21062                self.peek()
21063            )));
21064        }
21065        self.advance();
21066        Ok(())
21067    }
21068
21069    fn parse_json_string_literal(&mut self, what: &str) -> Result<String, ParseError> {
21070        match self.advance() {
21071            Token::String(s) => Ok(s),
21072            other => Err(self.err(alloc::format!(
21073                "expected {what} string literal, got {other:?}"
21074            ))),
21075        }
21076    }
21077
21078    /// v7.39 (round 205) — `( <coldef> [, <coldef>]* )`.
21079    #[inline(never)]
21080    fn parse_json_table_columns(
21081        &mut self,
21082    ) -> Result<alloc::vec::Vec<crate::ast::JsonTableColumn>, ParseError> {
21083        if !matches!(self.peek(), Token::LParen) {
21084            return Err(self.err("expected '(' after COLUMNS".into()));
21085        }
21086        self.advance();
21087        let mut cols = Vec::new();
21088        loop {
21089            cols.push(self.parse_json_table_one_column()?);
21090            if matches!(self.peek(), Token::Comma) {
21091                self.advance();
21092                continue;
21093            }
21094            break;
21095        }
21096        if !matches!(self.peek(), Token::RParen) {
21097            return Err(self.err(alloc::format!(
21098                "expected ')' after JSON_TABLE COLUMNS, got {:?}",
21099                self.peek()
21100            )));
21101        }
21102        self.advance();
21103        Ok(cols)
21104    }
21105
21106    fn parse_json_table_one_column(&mut self) -> Result<crate::ast::JsonTableColumn, ParseError> {
21107        use crate::ast::{JsonTableColumn, JsonTableOnBehavior};
21108        // NESTED [PATH] '<p>' COLUMNS (...)
21109        if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("nested")) {
21110            self.advance();
21111            if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("path")) {
21112                self.advance();
21113            }
21114            let path = self.parse_json_string_literal("NESTED PATH")?;
21115            if !matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("columns")) {
21116                return Err(self.err("expected COLUMNS after NESTED PATH".into()));
21117            }
21118            self.advance();
21119            let columns = self.parse_json_table_columns()?;
21120            return Ok(JsonTableColumn::Nested { path, columns });
21121        }
21122        // <name> ...
21123        let name = match self.advance() {
21124            Token::Ident(s) | Token::QuotedIdent(s) => s,
21125            other => {
21126                return Err(self.err(alloc::format!("expected column name, got {other:?}")));
21127            }
21128        };
21129        // <name> FOR ORDINALITY
21130        if matches!(self.peek(), Token::For) {
21131            self.advance();
21132            if !matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("ordinality")) {
21133                return Err(self.err("expected ORDINALITY after FOR".into()));
21134            }
21135            self.advance();
21136            return Ok(JsonTableColumn::Ordinality { name });
21137        }
21138        // <name> <type> [FORMAT JSON] {PATH '<p>' | EXISTS [PATH '<p>']} [WITH WRAPPER] [ON …]
21139        let ty = self.parse_column_type_name()?;
21140        let mut format_json = false;
21141        if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("format")) {
21142            self.advance();
21143            if !matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("json")) {
21144                return Err(self.err("expected JSON after FORMAT".into()));
21145            }
21146            self.advance();
21147            format_json = true;
21148        }
21149        let mut exists = false;
21150        if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("exists")) {
21151            self.advance();
21152            exists = true;
21153        }
21154        let mut path = alloc::format!("$.{name}");
21155        if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("path")) {
21156            self.advance();
21157            path = self.parse_json_string_literal("column PATH")?;
21158        }
21159        if !exists && matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("format")) {
21160            // `FORMAT JSON` after PATH (alternate placement).
21161            self.advance();
21162            if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("json")) {
21163                self.advance();
21164            }
21165            format_json = true;
21166        }
21167        let mut wrapper = false;
21168        if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("with")) {
21169            self.advance();
21170            // optional CONDITIONAL/UNCONDITIONAL
21171            if matches!(self.peek(), Token::Ident(s)
21172                if s.eq_ignore_ascii_case("unconditional")
21173                    || s.eq_ignore_ascii_case("conditional"))
21174            {
21175                self.advance();
21176            }
21177            if !matches!(self.peek(), Token::Ident(s)
21178                if s.eq_ignore_ascii_case("wrapper") || s.eq_ignore_ascii_case("array"))
21179            {
21180                return Err(self.err("expected WRAPPER after WITH".into()));
21181            }
21182            self.advance();
21183            // optional `ARRAY` after `WRAPPER`, or `WRAPPER` after `ARRAY`
21184            if matches!(self.peek(), Token::Ident(s)
21185                if s.eq_ignore_ascii_case("wrapper") || s.eq_ignore_ascii_case("array"))
21186            {
21187                self.advance();
21188            }
21189            wrapper = true;
21190        }
21191        // ON EMPTY / ON ERROR clauses (two, in any order).
21192        let mut on_empty = JsonTableOnBehavior::Null;
21193        let mut on_error = JsonTableOnBehavior::Null;
21194        for _ in 0..2 {
21195            let behavior = if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("error"))
21196            {
21197                self.advance();
21198                Some(JsonTableOnBehavior::Error)
21199            } else if matches!(self.peek(), Token::Null) {
21200                self.advance();
21201                Some(JsonTableOnBehavior::Null)
21202            } else if matches!(self.peek(), Token::Default) {
21203                self.advance();
21204                Some(JsonTableOnBehavior::Default(Box::new(self.parse_expr(0)?)))
21205            } else {
21206                None
21207            };
21208            let Some(behavior) = behavior else { break };
21209            // `ON {EMPTY|ERROR}`
21210            if !matches!(self.peek(), Token::On) {
21211                return Err(self.err("expected ON after JSON_TABLE column behavior".into()));
21212            }
21213            self.advance();
21214            if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("empty")) {
21215                self.advance();
21216                on_empty = behavior;
21217            } else if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("error")) {
21218                self.advance();
21219                on_error = behavior;
21220            } else {
21221                return Err(self.err("expected EMPTY or ERROR after ON".into()));
21222            }
21223        }
21224        Ok(JsonTableColumn::Regular {
21225            name,
21226            ty,
21227            path,
21228            exists,
21229            format_json,
21230            wrapper,
21231            on_empty,
21232            on_error,
21233        })
21234    }
21235
21236    fn parse_optional_alias_with_columns(
21237        &mut self,
21238    ) -> Result<(Option<String>, Vec<String>), ParseError> {
21239        let alias = self.parse_optional_alias()?;
21240        if alias.is_none() {
21241            return Ok((None, Vec::new()));
21242        }
21243        let mut cols: Vec<String> = Vec::new();
21244        if matches!(self.peek(), Token::LParen) {
21245            self.advance();
21246            while let Token::Ident(s) | Token::QuotedIdent(s) = self.peek().clone() {
21247                self.advance();
21248                cols.push(s);
21249                if matches!(self.peek(), Token::Comma) {
21250                    self.advance();
21251                    continue;
21252                }
21253                break;
21254            }
21255            if matches!(self.peek(), Token::RParen) {
21256                self.advance();
21257            }
21258        }
21259        Ok((alias, cols))
21260    }
21261
21262    /// v7.37.16 — parse a `left(str, n)` / `right(str, n)` function call
21263    /// whose keyword token was already consumed and whose `(` is the
21264    /// current token. Factored out of `parse_atom` (and marked
21265    /// `#[inline(never)]`) so its `Vec`/loop locals stay OFF the giant
21266    /// recursive `parse_atom` frame — inlining them there enlarges the
21267    /// per-nesting-level stack cost that `MAX_NEST_DEPTH` is tuned
21268    /// against, risking an overflow before the budget triggers.
21269    #[inline(never)]
21270    fn parse_lr_string_function_call(&mut self, name: &str) -> Result<Expr, ParseError> {
21271        self.advance(); // (
21272        let mut args = Vec::new();
21273        if !matches!(self.peek(), Token::RParen) {
21274            loop {
21275                args.push(self.parse_expr(0)?);
21276                match self.peek() {
21277                    Token::Comma => {
21278                        self.advance();
21279                    }
21280                    Token::RParen => break,
21281                    other => {
21282                        return Err(self.err(alloc::format!(
21283                            "expected ',' or ')' in {name}() args, got {other:?}"
21284                        )));
21285                    }
21286                }
21287            }
21288        }
21289        self.advance(); // )
21290        Ok(Expr::FunctionCall {
21291            name: name.into(),
21292            args,
21293        })
21294    }
21295
21296    /// FROM-clause: a primary table reference plus zero-or-more joined
21297    /// peers expressed via either `, <table>` (cross-product, no ON) or
21298    /// `[INNER|LEFT|RIGHT [OUTER]|FULL [OUTER]|CROSS] JOIN <table> [ON expr]`.
21299    /// v1.10 keeps the join list flat (left-associative nested-loop
21300    /// semantics).
21301    fn parse_from_clause(&mut self) -> Result<FromClause, ParseError> {
21302        let primary = self.parse_table_ref()?;
21303        let primary_qual = primary
21304            .alias
21305            .clone()
21306            .unwrap_or_else(|| primary.name.clone());
21307        let joins = self.parse_from_joins(&primary_qual)?;
21308        Ok(FromClause { primary, joins })
21309    }
21310
21311    /// v7.39 (round 420) — the join tail of a FROM clause, factored out of
21312    /// [`Self::parse_from_clause`] so MySQL's multi-table UPDATE can read the
21313    /// SAME grammar after its target table has already been consumed.
21314    /// (`advance()` destroys the tokens it returns — `mem::replace(.., Eof)`
21315    /// — so re-parsing by rewinding `self.pos` is not possible; the tail must
21316    /// be parsed forward, once.)
21317    /// `left_primary_qual` is the qualifier (alias, else name) of whatever
21318    /// sits to the LEFT of the first join — the FROM primary, or the UPDATE
21319    /// target in the MySQL multi-table form. It only feeds the `USING (…)`
21320    /// desugaring, which needs a name for the left side of each equality.
21321    fn parse_from_joins(&mut self, left_primary_qual: &str) -> Result<Vec<FromJoin>, ParseError> {
21322        let mut joins = Vec::new();
21323        loop {
21324            // `, <table>` — cross-product with no ON.
21325            if matches!(self.peek(), Token::Comma) {
21326                self.advance();
21327                let table = self.parse_table_ref()?;
21328                joins.push(FromJoin {
21329                    kind: JoinKind::Cross,
21330                    table,
21331                    on: None,
21332                    using_cols: None,
21333                    natural: false,
21334                });
21335                continue;
21336            }
21337            // v7.37.16 — optional leading `NATURAL` before the join
21338            // kind: `NATURAL JOIN`, `NATURAL LEFT JOIN`, etc. NATURAL is
21339            // not a lexer keyword (it arrives as a bare Ident), so match
21340            // it case-insensitively here. When present, no ON/USING
21341            // clause is allowed — the common columns are resolved at
21342            // execution time.
21343            let natural = matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("natural"));
21344            if natural {
21345                self.advance();
21346            }
21347            // Explicit JOIN syntax. Accept INNER JOIN, LEFT [OUTER] JOIN,
21348            // CROSS JOIN, and bare JOIN (defaults to INNER).
21349            let kind =
21350                match self.peek() {
21351                    Token::Inner => {
21352                        self.advance();
21353                        if !matches!(self.peek(), Token::Join) {
21354                            return Err(self
21355                                .err(format!("expected JOIN after INNER, got {:?}", self.peek())));
21356                        }
21357                        self.advance();
21358                        JoinKind::Inner
21359                    }
21360                    Token::Left => {
21361                        self.advance();
21362                        if matches!(self.peek(), Token::Outer) {
21363                            self.advance();
21364                        }
21365                        if !matches!(self.peek(), Token::Join) {
21366                            return Err(self.err(format!(
21367                                "expected JOIN after LEFT [OUTER], got {:?}",
21368                                self.peek()
21369                            )));
21370                        }
21371                        self.advance();
21372                        JoinKind::Left
21373                    }
21374                    Token::Cross => {
21375                        self.advance();
21376                        if !matches!(self.peek(), Token::Join) {
21377                            return Err(self
21378                                .err(format!("expected JOIN after CROSS, got {:?}", self.peek())));
21379                        }
21380                        self.advance();
21381                        JoinKind::Cross
21382                    }
21383                    // v7.37.16 — RIGHT [OUTER] JOIN. OUTER is optional noise.
21384                    Token::Right => {
21385                        self.advance();
21386                        if matches!(self.peek(), Token::Outer) {
21387                            self.advance();
21388                        }
21389                        if !matches!(self.peek(), Token::Join) {
21390                            return Err(self.err(format!(
21391                                "expected JOIN after RIGHT [OUTER], got {:?}",
21392                                self.peek()
21393                            )));
21394                        }
21395                        self.advance();
21396                        JoinKind::Right
21397                    }
21398                    // v7.37.16 — FULL [OUTER] JOIN. OUTER is optional noise.
21399                    Token::Full => {
21400                        self.advance();
21401                        if matches!(self.peek(), Token::Outer) {
21402                            self.advance();
21403                        }
21404                        if !matches!(self.peek(), Token::Join) {
21405                            return Err(self.err(format!(
21406                                "expected JOIN after FULL [OUTER], got {:?}",
21407                                self.peek()
21408                            )));
21409                        }
21410                        self.advance();
21411                        JoinKind::FullOuter
21412                    }
21413                    Token::Join => {
21414                        self.advance();
21415                        JoinKind::Inner
21416                    }
21417                    _ => break,
21418                };
21419            let table = self.parse_table_ref()?;
21420            // v7.37.7 C.1 — USING (col_list) sugar. Desugars to
21421            // `prev_table.col1 = table.col1 AND prev_table.col2 = table.col2 …`
21422            // where prev_table is the most-recent left-side table
21423            // (the previous join's table if any, else the FROM primary).
21424            // PG semantics around column merging are richer (USING'd
21425            // cols become deduplicated single output columns); for
21426            // sugar purposes the predicate-only form covers the
21427            // baseline corpus shape and chained `… JOIN x USING (k)
21428            // JOIN y USING (k)` calls.
21429            // v7.37.16 — NATURAL joins carry no ON/USING clause; the
21430            // common columns resolve at execution time.
21431            if natural {
21432                joins.push(FromJoin {
21433                    kind,
21434                    table,
21435                    on: None,
21436                    using_cols: None,
21437                    natural: true,
21438                });
21439                continue;
21440            }
21441            let using_match = matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("using"));
21442            // v7.37.16 — capture the USING column list (in addition to
21443            // the ON desugar below) so the executor can perform PG's
21444            // column-merge on the output side.
21445            let mut using_cols: Option<Vec<String>> = None;
21446            let on = if matches!(self.peek(), Token::On) {
21447                self.advance();
21448                Some(self.parse_expr(0)?)
21449            } else if using_match {
21450                self.advance();
21451                if !matches!(self.peek(), Token::LParen) {
21452                    return Err(
21453                        self.err(format!("expected '(' after USING, got {:?}", self.peek()))
21454                    );
21455                }
21456                self.advance();
21457                let mut cols: Vec<String> = Vec::new();
21458                loop {
21459                    match self.peek().clone() {
21460                        Token::Ident(s) | Token::QuotedIdent(s) => {
21461                            self.advance();
21462                            cols.push(s);
21463                        }
21464                        other => {
21465                            return Err(self.err(format!(
21466                                "expected column name inside USING (…), got {other:?}"
21467                            )));
21468                        }
21469                    }
21470                    match self.peek() {
21471                        Token::Comma => {
21472                            self.advance();
21473                            continue;
21474                        }
21475                        Token::RParen => {
21476                            self.advance();
21477                            break;
21478                        }
21479                        other => {
21480                            return Err(self.err(format!(
21481                                "expected ',' or ')' inside USING (…), got {other:?}"
21482                            )));
21483                        }
21484                    }
21485                }
21486                if cols.is_empty() {
21487                    return Err(self.err("USING (…) requires at least one column".to_string()));
21488                }
21489                using_cols = Some(cols.clone());
21490                // Pick the left-side alias: prev join's table if any,
21491                // else FROM primary. Use alias when present, else
21492                // table name (PG-equivalent qualifier).
21493                let left_qual: String = joins
21494                    .last()
21495                    .map(|j| {
21496                        j.table
21497                            .alias
21498                            .clone()
21499                            .unwrap_or_else(|| j.table.name.clone())
21500                    })
21501                    .unwrap_or_else(|| alloc::string::String::from(left_primary_qual));
21502                let right_qual = table.alias.clone().unwrap_or_else(|| table.name.clone());
21503                let mut iter = cols.into_iter().map(|c| Expr::Binary {
21504                    lhs: alloc::boxed::Box::new(Expr::Column(crate::ast::ColumnName {
21505                        qualifier: Some(left_qual.clone()),
21506                        name: c.clone(),
21507                    })),
21508                    op: crate::ast::BinOp::Eq,
21509                    rhs: alloc::boxed::Box::new(Expr::Column(crate::ast::ColumnName {
21510                        qualifier: Some(right_qual.clone()),
21511                        name: c,
21512                    })),
21513                });
21514                let first = iter.next().expect("at least one col");
21515                Some(iter.fold(first, |acc, pred| Expr::Binary {
21516                    lhs: alloc::boxed::Box::new(acc),
21517                    op: crate::ast::BinOp::And,
21518                    rhs: alloc::boxed::Box::new(pred),
21519                }))
21520            } else if kind == JoinKind::Cross {
21521                None
21522            } else {
21523                return Err(self.err(format!(
21524                    "expected ON or USING after {:?} JOIN, got {:?}",
21525                    kind,
21526                    self.peek()
21527                )));
21528            };
21529            joins.push(FromJoin {
21530                kind,
21531                table,
21532                on,
21533                using_cols,
21534                natural: false,
21535            });
21536        }
21537        Ok(joins)
21538    }
21539
21540    /// Optional alias after an expression or table:
21541    /// `AS <ident>` is unambiguous; a bare `<ident>` directly after is also
21542    /// accepted (PG-style implicit alias). Returns `None` if the next token
21543    /// is not alias-shaped (e.g. comma, FROM, WHERE, semicolon, EOF, operator).
21544    fn parse_optional_alias(&mut self) -> Result<Option<String>, ParseError> {
21545        if matches!(self.peek(), Token::As) {
21546            self.advance();
21547            // v7.39 (round 340, V56) — after AS the next token MUST be an
21548            // identifier. This used to return None and "let the caller
21549            // surface the error on the next expectation", but when AS is
21550            // the LAST token there is no next expectation: `SELECT 1 AS`
21551            // parsed clean and silently dropped the alias. PG rejects it.
21552            if let Token::Ident(_) | Token::QuotedIdent(_) = self.peek() {
21553                return self.expect_ident_like().map(Some);
21554            }
21555            return Err(self.err(alloc::format!(
21556                "expected an alias after AS, got {:?}",
21557                self.peek()
21558            )));
21559        }
21560        // v7.17.0 Phase 1.3 — implicit alias (no `AS`). PG's
21561        // grammar reserves a long list of follow-keywords from the
21562        // alias slot. SPG's bareword approximation: skip a small
21563        // set of idents that would otherwise be swallowed as the
21564        // table alias and break trailing clauses like CREATE
21565        // MATERIALIZED VIEW … WITH [NO] DATA or future ON
21566        // CONFLICT WHERE shapes.
21567        if let Token::Ident(s) | Token::QuotedIdent(s) = self.peek() {
21568            if is_alias_stopword(s) {
21569                return Ok(None);
21570            }
21571            return Ok(self.expect_ident_like().ok());
21572        }
21573        Ok(None)
21574    }
21575
21576    /// Pratt loop. `min_prec` is the minimum binary-op precedence we'll accept.
21577    fn parse_expr(&mut self, min_prec: u8) -> Result<Expr, ParseError> {
21578        // v7.30.2 (mailrs round-25 ask 2) — nesting budget: a parse
21579        // error beats a stack overflow (an overflow aborts the
21580        // embedding host process).
21581        self.enter_nested()?;
21582        let r = self.parse_expr_inner(min_prec);
21583        self.nest_depth -= 1;
21584        r
21585    }
21586
21587    /// `OPERATOR([schema.]<op>)` — PG's explicit-operator spelling.
21588    /// When the upcoming tokens form one, return the underlying
21589    /// operator token and the position just past the closing paren
21590    /// so the binary loop can dispatch on the plain operator.
21591    fn peek_explicit_operator(&self) -> Option<(usize, Token)> {
21592        if !matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("operator")) {
21593            return None;
21594        }
21595        if !matches!(self.tokens.get(self.pos + 1), Some(Token::LParen)) {
21596            return None;
21597        }
21598        let mut i = self.pos + 2;
21599        // Optional schema qualifier (pg_catalog.<op> etc.).
21600        if matches!(self.tokens.get(i), Some(Token::Ident(_)))
21601            && matches!(self.tokens.get(i + 1), Some(Token::Dot))
21602        {
21603            i += 2;
21604        }
21605        let op_tok = self.tokens.get(i)?.clone();
21606        if !matches!(self.tokens.get(i + 1), Some(Token::RParen)) {
21607            return None;
21608        }
21609        Some((i + 2, op_tok))
21610    }
21611
21612    /// PG operator symbols that lower onto function calls in
21613    /// binary position: `~` / `~*` / `!~` / `!~*` (regex match
21614    /// family → regexp_like, comparison rung), `^@` (starts_with,
21615    /// comparison rung), `^` (power, tighter than `*`), `#`
21616    /// (integer XOR via `(a|b) - (a&b)` — the AND bits are a
21617    /// subset of the OR bits so the subtraction never borrows).
21618    fn try_symbol_operator(
21619        &mut self,
21620        lhs: &Expr,
21621        min_prec: u8,
21622    ) -> Result<Option<Expr>, ParseError> {
21623        enum Sym {
21624            Regex { ci: bool, negated: bool },
21625            Like { ci: bool, negated: bool },
21626            StartsWith,
21627            Power,
21628            Xor,
21629            RangeAdjacent,
21630        }
21631        // v7.39 (IS-precedence knife) — the low-precedence postfix
21632        // predicates ride this existing leaf call (zero new frame slots
21633        // on the nesting chain).
21634        if let Some(e) = self.parse_postfix_predicate(lhs, min_prec)? {
21635            return Ok(Some(e));
21636        }
21637        let (sym, prec): (Sym, u8) = match self.peek() {
21638            Token::Tilde => (
21639                Sym::Regex {
21640                    ci: false,
21641                    negated: false,
21642                },
21643                5,
21644            ),
21645            Token::TildeStar => (
21646                Sym::Regex {
21647                    ci: true,
21648                    negated: false,
21649                },
21650                5,
21651            ),
21652            Token::NotTilde => (
21653                Sym::Regex {
21654                    ci: false,
21655                    negated: true,
21656                },
21657                5,
21658            ),
21659            Token::NotTildeStar => (
21660                Sym::Regex {
21661                    ci: true,
21662                    negated: true,
21663                },
21664                5,
21665            ),
21666            // v7.37 D.25 — PG operator spellings of LIKE/ILIKE.
21667            Token::DoubleTilde => (
21668                Sym::Like {
21669                    ci: false,
21670                    negated: false,
21671                },
21672                5,
21673            ),
21674            Token::DoubleTildeStar => (
21675                Sym::Like {
21676                    ci: true,
21677                    negated: false,
21678                },
21679                5,
21680            ),
21681            Token::NotDoubleTilde => (
21682                Sym::Like {
21683                    ci: false,
21684                    negated: true,
21685                },
21686                5,
21687            ),
21688            Token::NotDoubleTildeStar => (
21689                Sym::Like {
21690                    ci: true,
21691                    negated: true,
21692                },
21693                5,
21694            ),
21695            Token::CaretAt => (Sym::StartsWith, 5),
21696            // PG `^` is exponentiation; MySQL `^` is bitwise XOR (and binds
21697            // tighter than `* / & |`, which the prec-9 rung preserves —
21698            // v7.39 round 407: +1 from the pre-XOR ladder's rung 8).
21699            Token::Caret if self.mysql_dialect => (Sym::Xor, 9),
21700            Token::Caret => (Sym::Power, 9),
21701            // v7.39 (round 760, F31-B1) — `#` is a generic operator too:
21702            // PG answers `5 # 3 + 1` as `5 # 4` = 1 (additive first).
21703            Token::Hash => (Sym::Xor, 6),
21704            Token::Adjacent => (Sym::RangeAdjacent, 5),
21705            _ => return Ok(None),
21706        };
21707        if prec < min_prec {
21708            return Ok(None);
21709        }
21710        self.advance();
21711        let rhs = self.parse_expr(prec + 1)?;
21712        let out = match sym {
21713            Sym::Regex { ci, negated } => {
21714                let mut args = alloc::vec![lhs.clone(), rhs];
21715                if ci {
21716                    args.push(Expr::Literal(Literal::String(String::from("i"))));
21717                }
21718                maybe_not(
21719                    Expr::FunctionCall {
21720                        name: String::from("regexp_like"),
21721                        args,
21722                    },
21723                    negated,
21724                )
21725            }
21726            Sym::Like { ci, negated } => Expr::Like {
21727                expr: alloc::boxed::Box::new(lhs.clone()),
21728                pattern: alloc::boxed::Box::new(rhs),
21729                negated,
21730                case_insensitive: ci,
21731            },
21732            Sym::StartsWith => Expr::FunctionCall {
21733                name: String::from("starts_with"),
21734                args: alloc::vec![lhs.clone(), rhs],
21735            },
21736            Sym::Power => Expr::FunctionCall {
21737                name: String::from("power"),
21738                args: alloc::vec![lhs.clone(), rhs],
21739            },
21740            // `#` bitwise XOR — a real operator now (was desugared to
21741            // `(a|b)-(a&b)`, algebraically identical for integers but
21742            // undefined for bit strings; the direct op handles both).
21743            Sym::Xor => Expr::Binary {
21744                lhs: Box::new(lhs.clone()),
21745                op: BinOp::BitXor,
21746                rhs: Box::new(rhs),
21747            },
21748            // range `-|-` "is adjacent to" — lowered to a catalog function.
21749            Sym::RangeAdjacent => Expr::FunctionCall {
21750                name: String::from("range_adjacent"),
21751                args: alloc::vec![lhs.clone(), rhs],
21752            },
21753        };
21754        Ok(Some(out))
21755    }
21756
21757    /// v7.39 (IS-precedence knife) — the LOW-precedence postfix
21758    /// predicates, moved out of the tight postfix-cast loop: PG binds
21759    /// `IS [NOT] NULL/TRUE/FALSE/UNKNOWN/DISTINCT FROM/JSON/NORMALIZED`
21760    /// looser than EVERY binary operator (only NOT/AND/OR are looser),
21761    /// and BETWEEN/IN/LIKE/ILIKE/SIMILAR at the comparison rung — so
21762    /// `1 + 1 IS NULL` is `(1+1) IS NULL`, not `1 + (1 IS NULL)`.
21763    /// Returns Ok(consumed expr) when a predicate fired, Err(expr back)
21764    /// when nothing at this position belongs to the family. Out-of-line
21765    /// (`inline(never)`): the caller sits on the per-nesting-level frame
21766    /// chain that MAX_NEST_DEPTH is tuned against.
21767    #[inline(never)]
21768    fn parse_postfix_predicate(
21769        &mut self,
21770        lhs: &Expr,
21771        min_prec: u8,
21772    ) -> Result<Option<Expr>, ParseError> {
21773        // Reached through try_symbol_operator (an existing leaf call of
21774        // the binary loop) so NO new stack slots land on the per-nesting
21775        // frame chain; the lhs clones only when a predicate actually
21776        // consumes it.
21777        match self.peek() {
21778            // v7.39 (round 407) — IS is rung 4, the BETWEEN/IN/LIKE
21779            // comparison family rung 5 (each +1 from the pre-XOR ladder).
21780            Token::Is if min_prec <= 4 => {}
21781            Token::Between | Token::In | Token::Like if min_prec <= 5 => {}
21782            Token::Not
21783                if min_prec <= 5
21784                    && matches!(
21785                        self.tokens.get(self.pos + 1),
21786                        Some(Token::Between | Token::In | Token::Like)
21787                    ) => {}
21788            Token::Not | Token::Ident(_)
21789                if min_prec <= 5
21790                    && (matches!(self.peek(), Token::Ident(s)
21791                            if s.eq_ignore_ascii_case("ilike")
21792                                || (self.mysql_dialect
21793                                    && (s.eq_ignore_ascii_case("regexp")
21794                                        || s.eq_ignore_ascii_case("rlike")))
21795                                || (s.eq_ignore_ascii_case("similar")
21796                                    && matches!(self.tokens.get(self.pos + 1), Some(Token::To))))
21797                        || (matches!(self.peek(), Token::Not)
21798                            && matches!(self.tokens.get(self.pos + 1),
21799                                Some(Token::Ident(s)) if s.eq_ignore_ascii_case("ilike")
21800                                    || (self.mysql_dialect
21801                                        && (s.eq_ignore_ascii_case("regexp")
21802                                            || s.eq_ignore_ascii_case("rlike")))
21803                                    || s.eq_ignore_ascii_case("similar")))) => {}
21804            _ => return Ok(None),
21805        }
21806        let mut expr = lhs.clone();
21807        // IS family: rung 4 (NOT's operand parses at 4, so `NOT x IS NULL`
21808        // still groups as NOT (x IS NULL); OR/XOR/AND at 1-3 stay outside).
21809        if min_prec <= 4 {
21810            if matches!(self.peek(), Token::Is) {
21811                self.advance();
21812                let negated = if matches!(self.peek(), Token::Not) {
21813                    self.advance();
21814                    true
21815                } else {
21816                    false
21817                };
21818                // v7.9.27b — `IS [NOT] DISTINCT FROM <rhs>`.
21819                // mailrs pg_dump.
21820                if matches!(self.peek(), Token::Distinct) {
21821                    self.advance();
21822                    if !matches!(self.peek(), Token::From) {
21823                        return Err(self.err(format!(
21824                            "expected FROM after IS{} DISTINCT, got {:?}",
21825                            if negated { " NOT" } else { "" },
21826                            self.peek()
21827                        )));
21828                    }
21829                    self.advance();
21830                    // Right-hand side: parse at the same precedence
21831                    // tier as comparison (rung 5) so `x IS DISTINCT FROM a + b`
21832                    // groups as `x IS DISTINCT FROM (a + b)`.
21833                    let rhs = self.parse_expr(5)?;
21834                    let op = if negated {
21835                        BinOp::IsNotDistinctFrom
21836                    } else {
21837                        BinOp::IsDistinctFrom
21838                    };
21839                    expr = Expr::Binary {
21840                        op,
21841                        lhs: Box::new(expr),
21842                        rhs: Box::new(rhs),
21843                    };
21844                    {
21845                        return Ok(Some(expr));
21846                    }
21847                }
21848                // v7.37.17 (17.6 siblings) — SQL:2016 / PG 16
21849                // `IS [NOT] JSON [VALUE|OBJECT|ARRAY|SCALAR]`.
21850                // Lowers onto pg_is_json(x, kind); NOT wraps the
21851                // call in a logical negation.
21852                if matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s)
21853                if s.eq_ignore_ascii_case("json"))
21854                {
21855                    self.advance(); // JSON
21856                    let kind = match self.peek() {
21857                        Token::Ident(s) | Token::QuotedIdent(s)
21858                            if matches!(
21859                                s.to_ascii_lowercase().as_str(),
21860                                "value" | "object" | "array" | "scalar"
21861                            ) =>
21862                        {
21863                            let k = s.to_ascii_lowercase();
21864                            self.advance();
21865                            k
21866                        }
21867                        _ => "value".to_string(),
21868                    };
21869                    let call = Expr::FunctionCall {
21870                        name: "pg_is_json".to_string(),
21871                        args: alloc::vec![expr, Expr::Literal(Literal::String(kind)),],
21872                    };
21873                    expr = if negated {
21874                        Expr::Unary {
21875                            op: UnOp::Not,
21876                            expr: Box::new(call),
21877                        }
21878                    } else {
21879                        call
21880                    };
21881                    {
21882                        return Ok(Some(expr));
21883                    }
21884                }
21885                // v7.38 (read01 sweep) — SQL:2016 `x IS [NOT] [form]
21886                // NORMALIZED` (form ∈ NFC/NFD/NFKC/NFKD, default NFC).
21887                // Lowers onto is_normalized(x [, 'FORM']); NOT negates.
21888                {
21889                    let form_kw = match self.peek() {
21890                        Token::Ident(s) | Token::QuotedIdent(s)
21891                            if matches!(
21892                                s.to_ascii_uppercase().as_str(),
21893                                "NFC" | "NFD" | "NFKC" | "NFKD"
21894                            ) && matches!(
21895                                self.tokens.get(self.pos + 1),
21896                                Some(Token::Ident(n) | Token::QuotedIdent(n))
21897                                    if n.eq_ignore_ascii_case("normalized")
21898                            ) =>
21899                        {
21900                            Some(s.to_ascii_uppercase())
21901                        }
21902                        _ => None,
21903                    };
21904                    let bare_normalized = form_kw.is_none()
21905                        && matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s)
21906                        if s.eq_ignore_ascii_case("normalized"));
21907                    if form_kw.is_some() || bare_normalized {
21908                        if form_kw.is_some() {
21909                            self.advance(); // form keyword
21910                        }
21911                        self.advance(); // NORMALIZED
21912                        let mut args = alloc::vec![expr];
21913                        if let Some(f) = form_kw {
21914                            args.push(Expr::Literal(Literal::String(f)));
21915                        }
21916                        let call = Expr::FunctionCall {
21917                            name: "is_normalized".to_string(),
21918                            args,
21919                        };
21920                        expr = if negated {
21921                            Expr::Unary {
21922                                op: UnOp::Not,
21923                                expr: Box::new(call),
21924                            }
21925                        } else {
21926                            call
21927                        };
21928                        {
21929                            return Ok(Some(expr));
21930                        }
21931                    }
21932                }
21933                // `x IS [NOT] TRUE | FALSE | UNKNOWN` — the
21934                // three-valued boolean tests. IS TRUE/FALSE never
21935                // return NULL, so they lower to CASE forms whose
21936                // ELSE catches the NULL branch; IS UNKNOWN on a
21937                // boolean is exactly IS NULL.
21938                if matches!(self.peek(), Token::True | Token::False)
21939                    || matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("unknown"))
21940                {
21941                    let tok = self.advance();
21942                    let test = match tok {
21943                        Token::True => Some(true),
21944                        Token::False => Some(false),
21945                        _ => None, // UNKNOWN
21946                    };
21947                    // v7.39 (round 328, V45) — kept as what the user
21948                    // wrote. These used to be lowered here into `CASE` /
21949                    // `IS NULL`; the semantics were right but the AST no
21950                    // longer knew the form, so `CHECK ((a > 1) IS TRUE)`
21951                    // was echoed back as
21952                    // `CHECK ((CASE WHEN (a > 1) THEN TRUE ELSE FALSE END))`.
21953                    expr = Expr::BoolTest {
21954                        expr: Box::new(expr),
21955                        value: test,
21956                        negated,
21957                    };
21958                    {
21959                        return Ok(Some(expr));
21960                    }
21961                }
21962                if !matches!(self.peek(), Token::Null) {
21963                    return Err(self.err(format!(
21964                    "expected NULL, DISTINCT, JSON, TRUE, FALSE or UNKNOWN after IS{}, got {:?}",
21965                    if negated { " NOT" } else { "" },
21966                    self.peek()
21967                )));
21968                }
21969                self.advance();
21970                expr = Expr::IsNull {
21971                    expr: Box::new(expr),
21972                    negated,
21973                };
21974                {
21975                    return Ok(Some(expr));
21976                }
21977            }
21978        }
21979        // BETWEEN / IN / LIKE / ILIKE / SIMILAR: comparison rung (5).
21980        if min_prec <= 5 {
21981            // `x [NOT] BETWEEN a AND b`, `x [NOT] IN (...)`, `x [NOT] LIKE p`.
21982            // Look one token ahead so a stray `NOT` not followed by any of
21983            // these flows through to the early return below untouched.
21984            let negated = if matches!(self.peek(), Token::Not) {
21985                let next = self.tokens.get(self.pos + 1);
21986                matches!(next, Some(Token::Between | Token::In | Token::Like))
21987                    || matches!(next, Some(Token::Ident(s)) if s.eq_ignore_ascii_case("ilike")
21988                    || (self.mysql_dialect
21989                        && (s.eq_ignore_ascii_case("regexp") || s.eq_ignore_ascii_case("rlike")))
21990                    || s.eq_ignore_ascii_case("similar"))
21991            } else {
21992                false
21993            };
21994            if negated {
21995                self.advance();
21996            }
21997            if matches!(self.peek(), Token::Between) {
21998                expr = self.parse_between_tail(expr, negated)?;
21999                {
22000                    return Ok(Some(expr));
22001                }
22002            }
22003            if matches!(self.peek(), Token::In) {
22004                if self.suppress_in_tail && !negated {
22005                    // POSITION(sub IN str) — IN belongs to the
22006                    // enclosing function syntax; stop here.
22007                    {
22008                        return Ok(None);
22009                    }
22010                }
22011                expr = self.parse_in_tail(expr, negated)?;
22012                {
22013                    return Ok(Some(expr));
22014                }
22015            }
22016            // v7.39 (read01 regexp.c) — `x [NOT] SIMILAR TO p [ESCAPE e]`
22017            // lowers onto the internal __similar_to(expr, pat[, esc]) call
22018            // (the SQL→regex transform runs inside, in the backtracking-
22019            // friendly shape SPG's matcher needs).
22020            if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("similar"))
22021                && matches!(self.tokens.get(self.pos + 1), Some(Token::To))
22022            {
22023                self.advance(); // SIMILAR
22024                self.advance(); // TO
22025                let pattern = self.parse_expr(6)?;
22026                let mut args = alloc::vec![expr, pattern];
22027                if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("escape")) {
22028                    self.advance();
22029                    args.push(self.parse_expr(6)?);
22030                }
22031                let call = Expr::FunctionCall {
22032                    name: "__similar_to".to_string(),
22033                    args,
22034                };
22035                expr = maybe_not(call, negated);
22036                {
22037                    return Ok(Some(expr));
22038                }
22039            }
22040            if matches!(self.peek(), Token::Like) {
22041                self.advance();
22042                // `x [NOT] LIKE ANY/ALL (ARRAY[...])` — quantified LIKE.
22043                if let Some(q) = self.try_like_any_all(&expr, negated, false)? {
22044                    expr = q;
22045                    {
22046                        return Ok(Some(expr));
22047                    }
22048                }
22049                // Pattern at the same precedence as other comparison RHSes —
22050                // 5 leaves AND/OR alone so `a LIKE 'x%' AND b` parses right.
22051                let mut pattern = self.parse_expr(6)?;
22052                // `ESCAPE 'c'` — rewrite a literal pattern to the
22053                // default backslash escape at parse time. Custom
22054                // escapes on non-literal patterns would need
22055                // matcher support; error honestly.
22056                if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("escape")) {
22057                    self.advance();
22058                    let esc = self.parse_expr(6)?;
22059                    pattern = Self::rewrite_like_escape(pattern, esc).map_err(|m| self.err(m))?;
22060                }
22061                expr = Expr::Like {
22062                    expr: Box::new(expr),
22063                    pattern: Box::new(pattern),
22064                    negated,
22065                    case_insensitive: false,
22066                };
22067                {
22068                    return Ok(Some(expr));
22069                }
22070            }
22071            // v7.25 (round-17) — ILIKE: case-insensitive LIKE. The
22072            // keyword reaches us as a plain identifier.
22073            if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("ilike")) {
22074                self.advance();
22075                if let Some(q) = self.try_like_any_all(&expr, negated, true)? {
22076                    expr = q;
22077                    {
22078                        return Ok(Some(expr));
22079                    }
22080                }
22081                let pattern = self.parse_expr(6)?;
22082                expr = Expr::Like {
22083                    expr: Box::new(expr),
22084                    pattern: Box::new(pattern),
22085                    negated,
22086                    case_insensitive: true,
22087                };
22088                {
22089                    return Ok(Some(expr));
22090                }
22091            }
22092            // v7.39 (round 380) — MySQL's REGEXP / RLIKE regex-match
22093            // operator (RLIKE is the alias). It is a keyword, not `~`, and
22094            // matches case-insensitively under the default collation, so it
22095            // lowers onto the same `regexp_like(expr, pattern, 'i')` the
22096            // `~*` operator uses, wrapped in NOT when negated.
22097            if self.mysql_dialect
22098                && matches!(self.peek(), Token::Ident(s)
22099                    if s.eq_ignore_ascii_case("regexp") || s.eq_ignore_ascii_case("rlike"))
22100            {
22101                self.advance();
22102                let pattern = self.parse_expr(6)?;
22103                let call = Expr::FunctionCall {
22104                    name: String::from("regexp_like"),
22105                    args: alloc::vec![
22106                        expr,
22107                        pattern,
22108                        Expr::Literal(Literal::String(String::from("i"))),
22109                    ],
22110                };
22111                return Ok(Some(maybe_not(call, negated)));
22112            }
22113        }
22114        let _ = expr;
22115        Ok(None)
22116    }
22117
22118    fn parse_expr_inner(&mut self, min_prec: u8) -> Result<Expr, ParseError> {
22119        let mut lhs = self.parse_unary()?;
22120        let mut chain_len = 0usize;
22121        loop {
22122            // OPERATOR([schema.]op) reduces to its underlying
22123            // operator token before the normal dispatch.
22124            let explicit = self.peek_explicit_operator();
22125            let dispatch = match &explicit {
22126                Some((_, tok)) => self.binop_here(tok),
22127                None => self.binop_here(self.peek()),
22128            };
22129            let Some((op, prec)) = dispatch else {
22130                // v7.39 (round 539) — `OPERATOR(pg_catalog.~)` and the rest
22131                // of the symbol family. `binop_here` answers None for them
22132                // because they lower onto function calls rather than a
22133                // BinOp, and the fallback below reads `self.peek()` — the
22134                // word OPERATOR, not the operator. `pg_dump` writes every
22135                // catalog predicate this way, so its first query failed
22136                // and no dump ran:
22137                //
22138                //   AND c.relname OPERATOR(pg_catalog.~) '^(t)$'
22139                //
22140                // Collapsing the wrapper to the operator it names puts the
22141                // token where the fallback already looks.
22142                if let Some((next, op_tok)) = explicit {
22143                    self.tokens.splice(self.pos..next, [op_tok]);
22144                }
22145                if let Some(e) = self.try_symbol_operator(&lhs, min_prec)? {
22146                    lhs = e;
22147                    chain_len += 1;
22148                    if chain_len > MAX_BINARY_CHAIN {
22149                        return Err(self.err(alloc::format!(
22150                            "more than {MAX_BINARY_CHAIN} chained binary operators"
22151                        )));
22152                    }
22153                    continue;
22154                }
22155                break;
22156            };
22157            if prec < min_prec {
22158                break;
22159            }
22160            // v7.30.2 (mailrs round-25 ask 2) — the chain builds
22161            // iteratively but evaluates and drops recursively;
22162            // depth beyond the budget overflows worker stacks.
22163            chain_len += 1;
22164            if chain_len > MAX_BINARY_CHAIN {
22165                return Err(self.err(alloc::format!(
22166                    "more than {MAX_BINARY_CHAIN} chained binary operators; rewrite long OR-equality chains as IN (…)"
22167                )));
22168            }
22169            match explicit {
22170                Some((end_pos, _)) => self.pos = end_pos,
22171                None => {
22172                    self.advance();
22173                }
22174            }
22175            // v7.10.12 — `x <op> ANY(arr)` / `x <op> ALL(arr)`.
22176            // ANY is a bare ident; ALL is a reserved Token. Both
22177            // require an immediate `(` to disambiguate from
22178            // identifier columns named `any` / `all`.
22179            let any_kind = match self.peek() {
22180                Token::All if matches!(self.tokens.get(self.pos + 1), Some(Token::LParen)) => {
22181                    Some(false)
22182                }
22183                Token::Ident(s) | Token::QuotedIdent(s)
22184                    if (s.eq_ignore_ascii_case("any")
22185                        || s.eq_ignore_ascii_case("some")
22186                        || s.eq_ignore_ascii_case("all"))
22187                        && matches!(self.tokens.get(self.pos + 1), Some(Token::LParen)) =>
22188                {
22189                    Some(!s.eq_ignore_ascii_case("all"))
22190                }
22191                _ => None,
22192            };
22193            if let Some(is_any) = any_kind {
22194                lhs = self.parse_any_all_rhs(lhs, op, is_any)?;
22195                continue;
22196            }
22197            let rhs = self.parse_expr(prec + 1)?;
22198            lhs = Expr::Binary {
22199                lhs: Box::new(lhs),
22200                op,
22201                rhs: Box::new(rhs),
22202            };
22203        }
22204        Ok(lhs)
22205    }
22206
22207    /// `x <op> ANY (…)` / `ALL (…)`, both the quantified-subquery form
22208    /// and the array form.
22209    ///
22210    /// `#[inline(never)]` and out of `parse_expr_inner`, which sits on the
22211    /// frame chain `MAX_NEST_DEPTH` is tuned against: a debug build gives
22212    /// this block's `Expr` temporaries and four `format!` sites slots in
22213    /// that frame on every level of `((((1))))`, which never reaches it.
22214    #[inline(never)]
22215    fn parse_any_all_rhs(
22216        &mut self,
22217        lhs: Expr,
22218        op: BinOp,
22219        is_any: bool,
22220    ) -> Result<Expr, ParseError> {
22221        self.advance(); // ident
22222        self.advance(); // (
22223        // `x op ANY (SELECT …)` — the quantified-subquery
22224        // form. `= ANY` is exactly IN; the other operators
22225        // lower onto EXISTS over the subquery as a derived
22226        // table, comparing against its single projection
22227        // aliased __v (x's columns resolve correlated).
22228        // ALL is the negated-EXISTS complement; a NULL
22229        // element makes PG return NULL where this lowering
22230        // returns true — the NOT NULL column case (the
22231        // practical one) is exact.
22232        if matches!(self.peek(), Token::Select) || self.peek_is_with_kw() {
22233            // v7.39 (round 153) — `ANY (WITH … SELECT …)` is
22234            // legal PG too (round-151 sibling). Out-of-line
22235            // (#[inline(never)] helper) — this sits on
22236            // parse_expr's recursive frame and the two-armed
22237            // SELECT temporary blew the nesting-budget stack.
22238            let mut sub = self.parse_any_all_select_body()?;
22239            if !matches!(self.peek(), Token::RParen) {
22240                return Err(self.err(alloc::format!(
22241                    "expected ')' after ANY/ALL subquery, got {:?}",
22242                    self.peek()
22243                )));
22244            }
22245            self.advance();
22246            if sub.items.len() != 1 {
22247                return Err(self.err(alloc::format!(
22248                    "ANY/ALL subquery must return one column, got {}",
22249                    sub.items.len()
22250                )));
22251            }
22252            if is_any && matches!(op, BinOp::Eq) {
22253                return Ok(Expr::InSubquery {
22254                    expr: Box::new(lhs),
22255                    subquery: Box::new(sub),
22256                    negated: false,
22257                });
22258            }
22259            // The engine's subquery resolvers materialise
22260            // the single-column result into an ARRAY the
22261            // existing AnyAll three-valued eval consumes.
22262            return Ok(Expr::AnyAll {
22263                expr: Box::new(lhs),
22264                op,
22265                array: Box::new(Expr::ScalarSubquery(Box::new(sub))),
22266                is_any,
22267            });
22268        }
22269        let arr = self.parse_expr(0)?;
22270        if !matches!(self.peek(), Token::RParen) {
22271            return Err(self.err(alloc::format!(
22272                "expected ')' after ANY/ALL argument, got {:?}",
22273                self.peek()
22274            )));
22275        }
22276        self.advance();
22277        Ok(Expr::AnyAll {
22278            expr: Box::new(lhs),
22279            op,
22280            array: Box::new(arr),
22281            is_any,
22282        })
22283    }
22284
22285    /// v7.39 (read01 geo_ops.c) — prefix `@@` (center-of). Out-of-line
22286    /// from `parse_unary` (see the frame-budget note at MAX_NEST_DEPTH).
22287    #[inline(never)]
22288    fn parse_prefix_center(&mut self) -> Result<Expr, ParseError> {
22289        self.advance();
22290        let e = self.parse_expr(9)?;
22291        Ok(build_center_call(e))
22292    }
22293
22294    /// v7.39 (round 508) — a prefix operator that IS a function: `@ x` is
22295    /// `abs(x)`, `# p` is `npoints(p)`, `@-@ p` is `length(p)`. Binds like
22296    /// unary minus.
22297    ///
22298    /// `#[inline(never)]` for the same reason as its neighbours: parse_unary
22299    /// sits on the recursive frame chain MAX_NEST_DEPTH is tuned against, so
22300    /// the Expr-sized local stays out of that frame.
22301    #[inline(never)]
22302    fn parse_prefix_call(&mut self, name: &str) -> Result<Expr, ParseError> {
22303        self.advance();
22304        let e = self.parse_expr(9)?;
22305        Ok(Expr::FunctionCall {
22306            name: alloc::string::String::from(name),
22307            args: alloc::vec![e],
22308        })
22309    }
22310
22311    /// v7.39 (read01 geo_ops.c) — prefix `?|` (vertical) / `?-`
22312    /// (horizontal). Out-of-line from `parse_unary` (frame budget).
22313    #[inline(never)]
22314    fn parse_prefix_geom_axis(&mut self, vertical: bool) -> Result<Expr, ParseError> {
22315        self.advance();
22316        let e = self.parse_expr(9)?;
22317        Ok(Expr::FunctionCall {
22318            name: alloc::string::String::from(if vertical {
22319                "isvertical"
22320            } else {
22321                "ishorizontal"
22322            }),
22323            args: alloc::vec![e],
22324        })
22325    }
22326
22327    /// v7.39 (round 355, M13) — `BINARY <expr>`, lowered onto the same
22328    /// cast the `CAST(x AS BINARY)` spelling produces. It binds tightly:
22329    /// MariaDB reads `BINARY 1 + 1` as `(BINARY 1) + 1` = 2.
22330    #[inline(never)]
22331    fn parse_binary_prefix(&mut self) -> Result<Expr, ParseError> {
22332        self.advance();
22333        let e = self.parse_expr(9)?;
22334        Ok(Expr::Cast {
22335            expr: Box::new(e),
22336            target: CastTarget::Named("binary".to_string()),
22337        })
22338    }
22339
22340    /// The prefix operators that share one shape: take the token, parse
22341    /// an operand at `prec`, wrap it.
22342    ///
22343    /// `#[inline(never)]`, and one function instead of five arms, for the
22344    /// reason the neighbouring `parse_prefix_*` helpers give: `parse_unary`
22345    /// sits on the frame chain `MAX_NEST_DEPTH` is tuned against, and a
22346    /// debug build gives EVERY arm's locals a slot in the frame, whichever
22347    /// arm runs. `((((1))))` reaches none of these arms and was carrying
22348    /// five `Expr`-sized locals per level for them anyway.
22349    #[inline(never)]
22350    fn parse_unary_op(&mut self, op: UnOp, prec: u8) -> Result<Expr, ParseError> {
22351        self.advance();
22352        let e = self.parse_expr(prec)?;
22353        Ok(Expr::Unary {
22354            op,
22355            expr: Box::new(e),
22356        })
22357    }
22358
22359    /// Unary minus. Out-of-line for the frame reason on `parse_unary_op`,
22360    /// and separate from it because of the literal folding below and the
22361    /// `format!` temporaries that folding needs.
22362    #[inline(never)]
22363    fn parse_prefix_minus(&mut self) -> Result<Expr, ParseError> {
22364        self.advance();
22365        // v7.39 (round 549) — fold the sign into an integer literal that
22366        // only fits once it is negative.
22367        //
22368        // `9223372036854775808` is one past i64::MAX, so the lexer hands
22369        // it over as a NUMERIC and `-` on a numeric stays numeric. PG
22370        // folds the sign first, so `-9223372036854775808` is a bigint
22371        // there — and `-9223372036854775808 - 1` raises "bigint out of
22372        // range" where SPG quietly answered -9223372036854775809, a value
22373        // no bigint can hold. The arithmetic itself was already checked;
22374        // only the literal's type was wrong.
22375        if let Token::Numeric(lit) = self.peek()
22376            && let Ok(folded) = alloc::format!("-{lit}").parse::<i64>()
22377        {
22378            self.advance();
22379            return Ok(Expr::Literal(Literal::Integer(folded)));
22380        }
22381        // Unary minus binds tighter than `*`/`/` (now at prec 7 after
22382        // `<->` slotted into 5 and arithmetic shifted up).
22383        let e = self.parse_expr(9)?;
22384        Ok(Expr::Unary {
22385            op: UnOp::Neg,
22386            expr: Box::new(e),
22387        })
22388    }
22389
22390    /// tsquery `!!` prefix negation, lowered to the catalog function.
22391    /// Binds like unary minus. Out-of-line for the frame reason on
22392    /// `parse_unary_op`.
22393    #[inline(never)]
22394    fn parse_prefix_tsquery_not(&mut self) -> Result<Expr, ParseError> {
22395        self.advance();
22396        let e = self.parse_expr(9)?;
22397        Ok(Expr::FunctionCall {
22398            name: String::from("tsquery_not"),
22399            args: alloc::vec![e],
22400        })
22401    }
22402
22403    fn parse_unary(&mut self) -> Result<Expr, ParseError> {
22404        match self.peek() {
22405            // NOT binds tighter than AND / XOR / OR but looser than
22406            // comparisons — its operand takes everything ≥ the comparison
22407            // rung (4), leaving AND (3) / XOR (2) / OR (1) outside so
22408            // `NOT a AND b` groups as `(NOT a) AND b`. (v7.39 round 407:
22409            // was rung 3, behaviour-identical when 3 was unused; AND now
22410            // occupies 3, so this must be 4 to keep NOT tighter than AND.)
22411            Token::Not => self.parse_unary_op(UnOp::Not, 4),
22412            // v7.39 (round 355, M13) — MySQL's `BINARY <expr>` prefix.
22413            // The body is out-of-line: `parse_unary` is one of the three
22414            // frames the parser's MAX_NEST_DEPTH is tuned against, and an
22415            // inline arm here overflowed the native stack in
22416            // `nesting_budget_errors_cleanly` — the guard test caught it,
22417            // exactly as the eval-side cliff did in rounds 346 and 351.
22418            Token::Ident(w) if self.mysql_dialect && w.eq_ignore_ascii_case("binary") => {
22419                self.parse_binary_prefix()
22420            }
22421            // v7.39 (round 353, M10) — MySQL's `!`. It binds TIGHTER than
22422            // arithmetic, unlike NOT: MariaDB answers 1 for `!1 + 1`
22423            // (`(!1)+1`) and 0 for `NOT 1 + 1` (`NOT (1+1)`), measured.
22424            Token::Bang => self.parse_unary_op(UnOp::Not, 9),
22425            Token::Minus => self.parse_prefix_minus(),
22426            // v7.39 (round 507) — unary `+`, which SPG did not have. `+1`
22427            // worked only because the lexer reads it as one signed literal;
22428            // `+ 1`, `+a`, `+(1)` and `1 + +1` were syntax errors, and both
22429            // PG18 and MariaDB take all of them. Binds like unary minus.
22430            Token::Plus => self.parse_unary_op(UnOp::Plus, 9),
22431            // Bitwise NOT binds like unary minus.
22432            Token::Tilde => self.parse_unary_op(UnOp::BitNot, 9),
22433            // v7.39 (read01 geo_ops.c) — prefix `@@` is PG's geometric
22434            // "center of" operator; desugars to center(x). The whole arm
22435            // is out-of-line: parse_unary sits on the per-nesting-level
22436            // frame chain that MAX_NEST_DEPTH is tuned against, so no
22437            // Expr-sized local may live in this frame.
22438            Token::TsMatch => self.parse_prefix_center(),
22439            // v7.39 (round 508) — the prefix operators that are named
22440            // functions in disguise: `@ x` is abs, `# p` is npoints, `@-@ p`
22441            // is length. Out-of-line for the same nesting-frame reason as
22442            // parse_prefix_center — parse_unary sits on the recursive cycle
22443            // MAX_NEST_DEPTH is tuned against, so no Expr-sized local may
22444            // live in this frame.
22445            Token::At => self.parse_prefix_call("abs"),
22446            Token::Hash => self.parse_prefix_call("npoints"),
22447            Token::AtMinusAt => self.parse_prefix_call("length"),
22448            // v7.39 (read01 geo_ops.c) — prefix `?|` / `?-`: "is vertical" /
22449            // "is horizontal" (lseg / line); desugars to the existing
22450            // isvertical()/ishorizontal() functions. Out-of-line for the
22451            // same nesting-frame reason as parse_prefix_center.
22452            Token::JsonKeysAny => self.parse_prefix_geom_axis(true),
22453            Token::GeomHoriz => self.parse_prefix_geom_axis(false),
22454            Token::DoubleBang => self.parse_prefix_tsquery_not(),
22455            _ => self.parse_atom(),
22456        }
22457    }
22458
22459    /// Parse a parenthesised scalar subquery body after the caller has consumed
22460    /// `(` and confirmed the next token is SELECT (or WITH, when `is_with`).
22461    /// v7.37 D.43 — `#[inline(never)]` keeps the large `Statement` local and the
22462    /// SELECT/WITH parse machinery off `parse_atom`'s stack frame; parse_atom sits
22463    /// on the recursive `((…))` cycle whose depth budget is tuned to that frame.
22464    /// v7.39 (read01 round 105) — is the current position `( <subquery-start>`,
22465    /// i.e. an `ARRAY(<subquery>)` and not `ARRAY[...]`? A subquery starts with
22466    /// SELECT, VALUES, or WITH (WITH lexes as a bare ident).
22467    /// `#[inline(never)]`: keeps this guard's locals off parse_atom's frame,
22468    /// which sits on the recursive nesting-budget cycle (a few extra bytes there
22469    /// tips the deep-nesting test into a stack overflow).
22470    #[inline(never)]
22471    fn array_subquery_ahead(&self) -> bool {
22472        if !matches!(self.peek(), Token::LParen) {
22473            return false;
22474        }
22475        matches!(
22476            self.tokens.get(self.pos + 1),
22477            Some(Token::Select | Token::Values)
22478        ) || matches!(
22479            self.tokens.get(self.pos + 1),
22480            Some(Token::Ident(w) | Token::QuotedIdent(w)) if w.eq_ignore_ascii_case("with")
22481        )
22482    }
22483
22484    /// v7.10.10 — `ARRAY[expr, …]` literal body. The `array` ident is consumed
22485    /// and the current token is `[`. `#[inline(never)]` so its `Vec`/loop
22486    /// locals stay off parse_atom's recursive frame (round 105).
22487    #[inline(never)]
22488    fn parse_array_literal_body(&mut self) -> Result<Expr, ParseError> {
22489        self.advance(); // consume `[`
22490        let mut items: Vec<Expr> = Vec::new();
22491        if !matches!(self.peek(), Token::RBracket) {
22492            loop {
22493                // Inside `ARRAY[...]`, a nested `[...]` is a sub-array
22494                // (`ARRAY[[1,2],[3,4]]`), not a pgvector literal.
22495                if matches!(self.peek(), Token::LBracket) {
22496                    items.push(self.parse_array_bracket_body()?);
22497                } else {
22498                    items.push(self.parse_expr(0)?);
22499                }
22500                match self.peek() {
22501                    Token::Comma => {
22502                        self.advance();
22503                    }
22504                    Token::RBracket => break,
22505                    other => {
22506                        return Err(self.err(alloc::format!(
22507                            "expected ',' or ']' in ARRAY literal, got {other:?}"
22508                        )));
22509                    }
22510                }
22511            }
22512        }
22513        self.advance(); // consume `]`
22514        Ok(Expr::Array(items))
22515    }
22516
22517    /// v7.39 (read01 round 105) — parse `ARRAY(<subquery>)`. The `array` ident
22518    /// is already consumed; the current token is `(`. Desugars to a scalar
22519    /// subquery `SELECT array_agg(c) FROM (<subquery>) AS t(c)`, which collects
22520    /// the subquery's single-column rows in order — reusing the existing
22521    /// ScalarSubquery machinery rather than adding an AST node. `#[inline(never)]`
22522    /// keeps the large `Statement` local off parse_atom's recursive frame.
22523    #[inline(never)]
22524    fn parse_array_subquery(&mut self) -> Result<Expr, ParseError> {
22525        self.advance(); // consume `(`
22526        let is_with = matches!(self.peek(), Token::Ident(w) | Token::QuotedIdent(w)
22527            if w.eq_ignore_ascii_case("with"));
22528        let sub = if is_with {
22529            self.advance(); // WITH
22530            self.parse_with_cte_then_select()?
22531        } else {
22532            self.parse_select_stmt()?
22533        };
22534        if !matches!(self.peek(), Token::RParen) {
22535            return Err(self.err(alloc::format!(
22536                "expected ')' to close ARRAY(subquery), got {:?}",
22537                self.peek()
22538            )));
22539        }
22540        self.advance(); // consume `)`
22541        // Reuse the parser to build the array_agg wrapper from the subquery's
22542        // canonical text — avoids hand-constructing the derived-table AST.
22543        let wrapper = alloc::format!(
22544            "SELECT array_agg(\"__spg_arr_c\") FROM ({sub}) AS \"__spg_arr_t\"(\"__spg_arr_c\")"
22545        );
22546        let stmt = parse_statement(&wrapper)
22547            .map_err(|e| self.err(alloc::format!("ARRAY(subquery): {}", e.message)))?;
22548        let Statement::Select(sel) = stmt else {
22549            return Err(self.err("ARRAY(subquery) did not desugar to a SELECT".into()));
22550        };
22551        Ok(Expr::ScalarSubquery(alloc::boxed::Box::new(sel)))
22552    }
22553
22554    #[inline(never)]
22555    fn parse_paren_scalar_subquery(&mut self, is_with: bool) -> Result<Expr, ParseError> {
22556        let inner = if is_with {
22557            self.advance(); // WITH
22558            self.parse_with_cte_then_select()?
22559        } else {
22560            self.parse_select_stmt()?
22561        };
22562        match self.advance() {
22563            Token::RParen => {
22564                let Statement::Select(s) = inner else {
22565                    return Err(ParseError {
22566                        message: "scalar subquery body must be a SELECT".into(),
22567                        token_pos: self.consumed_pos(),
22568                    });
22569                };
22570                Ok(Expr::ScalarSubquery(Box::new(s)))
22571            }
22572            other => Err(ParseError {
22573                message: format!("expected ')' after scalar subquery, got {other:?}"),
22574                token_pos: self.consumed_pos(),
22575            }),
22576        }
22577    }
22578
22579    /// `B'1010'` / `X'1F'` bit-string (PG) or binary-string (MySQL)
22580    /// literals. The lexer splits them into an ident + string; recombine
22581    /// here. Out-of-line and returning `Option` so `parse_atom` — the
22582    /// recursive frame the 768 KiB stack budget is tuned against — pays no
22583    /// frame for the `body` / `bits` strings and their char loops (the
22584    /// round-367 frame cliff, M20).
22585    #[inline(never)]
22586    fn try_parse_bit_string_literal(&mut self) -> Option<Result<Expr, ParseError>> {
22587        let is_hex = match self.peek() {
22588            Token::Ident(p) if p.eq_ignore_ascii_case("x") => true,
22589            Token::Ident(p) if p.eq_ignore_ascii_case("b") => false,
22590            _ => return None,
22591        };
22592        if !matches!(self.tokens.get(self.pos + 1), Some(Token::String(_))) {
22593            return None;
22594        }
22595        // v7.39.3 — where the LITERAL starts, because the errors below
22596        // are about the literal and both engines point at it. `err`
22597        // reports the CURRENT token, which by then is the one after the
22598        // string: `SELECT x'123'` pointed at Eof, so the MySQL wire's
22599        // `near '…'` snippet — which runs from the reported position to
22600        // the end — came out empty where MySQL 9.7.2 says `near
22601        // 'x'123''`.
22602        let lit_pos = self.pos;
22603        self.advance();
22604        let Token::String(body) = self.advance() else {
22605            unreachable!("guarded above");
22606        };
22607        // v7.39 (round 367, M20) — in the MySQL dialect `X'…'` and `b'…'`
22608        // are BINARY STRINGS, not PG bit strings. `X'41'` is the byte 0x41
22609        // (hex pairs, even count required — MariaDB errors on an odd
22610        // count); `b'1010'` packs its bits big-endian, left-padded to a
22611        // byte. Lower both onto the bytea cast.
22612        if self.mysql_dialect {
22613            if is_hex {
22614                if body.len() % 2 == 1 {
22615                    return Some(Err(self.err_at(
22616                        lit_pos,
22617                        alloc::format!("invalid hex string literal X'{body}': odd digit count"),
22618                    )));
22619                }
22620                for c in body.chars() {
22621                    if !c.is_ascii_hexdigit() {
22622                        return Some(Err(self.err_at(
22623                            lit_pos,
22624                            alloc::format!("invalid hexadecimal digit {c:?} in X'…'"),
22625                        )));
22626                    }
22627                }
22628                return Some(self.finish_postfix_casts(hex_literal_to_bytea_expr(&body)));
22629            }
22630            if let Some(bad) = body.chars().find(|c| *c != '0' && *c != '1') {
22631                return Some(Err(self.err_at(
22632                    lit_pos,
22633                    alloc::format!("invalid binary digit {bad:?} in b'…'"),
22634                )));
22635            }
22636            return Some(self.finish_postfix_casts(bits_literal_to_bytea_expr(&body)));
22637        }
22638        let bits = if is_hex {
22639            let mut out = String::with_capacity(body.len() * 4);
22640            for c in body.chars() {
22641                let Some(d) = c.to_digit(16) else {
22642                    // v7.39.3 — PostgreSQL 18.6's own sentence, and its
22643                    // own quoting: `"g" is not a valid hexadecimal
22644                    // digit` (measured, with the caret on the literal).
22645                    return Some(Err(self.err_at(
22646                        lit_pos,
22647                        alloc::format!("\"{c}\" is not a valid hexadecimal digit"),
22648                    )));
22649                };
22650                out.push_str(&alloc::format!("{d:04b}"));
22651            }
22652            out
22653        } else {
22654            if let Some(bad) = body.chars().find(|c| *c != '0' && *c != '1') {
22655                return Some(Err(self.err_at(
22656                    lit_pos,
22657                    alloc::format!("\"{bad}\" is not a valid binary digit"),
22658                )));
22659            }
22660            body
22661        };
22662        // Route through the postfix-cast loop so a chained cast like
22663        // `B'1010'::int` attaches onto the implicit `::bit` cast instead
22664        // of erroring at the `::`.
22665        // v7.39 (read01 varbit.c) — a distinct internal target: a B'...'
22666        // literal keeps its exact length, while an explicit `::bit` cast is
22667        // bit(1) with pad/truncate semantics (PG).
22668        Some(self.finish_postfix_casts(Expr::Cast {
22669            expr: Box::new(Expr::Literal(Literal::String(bits))),
22670            target: CastTarget::Named("__bit_literal".to_string()),
22671        }))
22672    }
22673
22674    fn parse_atom(&mut self) -> Result<Expr, ParseError> {
22675        if let Some(res) = self.try_parse_bit_string_literal() {
22676            return res;
22677        }
22678        let tok_pos = self.pos;
22679        match self.advance() {
22680            Token::Integer(n) => Ok(Expr::Literal(Literal::Integer(n))),
22681            Token::Float(x) => Ok(Expr::Literal(Literal::Float(x))),
22682            // v7.38 (read01) — dotted / over-i64 literal → exact NUMERIC (PG),
22683            // carrying the source mantissa + scale so no precision is lost. A
22684            // literal too wide for i128 falls back to double precision.
22685            // Out-of-line (#[inline(never)]) — this arm sits on the
22686            // parse_expr recursion chain; its expansion locals must not
22687            // widen the recursive frame (debug frame-cliff discipline).
22688            Token::Numeric(s) => match numeric_token_to_literal(s) {
22689                Ok(lit) => Ok(Expr::Literal(lit)),
22690                Err(msg) => Err(self.err(msg)),
22691            },
22692            Token::String(s) => Ok(Expr::Literal(Literal::String(s))),
22693            // v7.39 (round 367, M20) — a MySQL `0x…` binary-string literal
22694            // (the lexer only emits this token in the MySQL dialect). Lower
22695            // onto the existing bytea cast; out-of-line to keep this arm off
22696            // the parse recursion frame.
22697            Token::HexBytes(s) => Ok(hex_literal_to_bytea_expr(&s)),
22698            Token::True => Ok(Expr::Literal(Literal::Bool(true))),
22699            Token::False => Ok(Expr::Literal(Literal::Bool(false))),
22700            Token::Null => Ok(Expr::Literal(Literal::Null)),
22701            // v6.1.1 — `$N` placeholder. The actual Value lookup
22702            // happens in the engine eval path against the prepared-
22703            // statement bind buffer.
22704            Token::Placeholder(n) => Ok(Expr::Placeholder(n)),
22705            Token::LParen => {
22706                // v4.10: `(SELECT ...)` in expression position is a
22707                // scalar subquery; otherwise it's a parenthesised
22708                // expression. Peek for SELECT keyword to dispatch.
22709                // v7.37 D.43 — also accept `(WITH [RECURSIVE] … SELECT …)`; WITH
22710                // lexes as Ident("with") (not a reserved token). The subquery body
22711                // is parsed in `parse_paren_scalar_subquery` (marked #[inline(never)]
22712                // so its large `Statement` local stays out of parse_atom's stack
22713                // frame — parse_atom is on the recursive `((…))` cycle and the
22714                // nesting budget is tuned to its frame size).
22715                let is_with = matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s)
22716                    if s.eq_ignore_ascii_case("with"));
22717                if matches!(self.peek(), Token::Select) || is_with {
22718                    self.parse_paren_scalar_subquery(is_with)
22719                } else {
22720                    let e = self.parse_expr(0)?;
22721                    // `(a, b, …)` — a row constructor. Valid only
22722                    // in front of a comparison operator or [NOT]
22723                    // IN; both expand at parse time (lexicographic
22724                    // comparison / OR'd row equalities).
22725                    if matches!(self.peek(), Token::Comma) {
22726                        let mut row = alloc::vec![e];
22727                        while matches!(self.peek(), Token::Comma) {
22728                            self.advance();
22729                            row.push(self.parse_expr(0)?);
22730                        }
22731                        if !matches!(self.peek(), Token::RParen) {
22732                            return Err(self.err(alloc::format!(
22733                                "expected ')' after row constructor, got {:?}",
22734                                self.peek()
22735                            )));
22736                        }
22737                        self.advance();
22738                        // A bare `(a, b, …)` row constructor can carry postfix
22739                        // (`::text`, `.field`) just like `ROW(a, b, …)`; the
22740                        // early return here skips parse_atom's tail postfix
22741                        // pass, so fold casts in explicitly. For the
22742                        // comparison / predicate forms nothing postfix follows,
22743                        // so this is a no-op.
22744                        return self
22745                            .parse_row_comparison_tail(row)
22746                            .and_then(|e| self.finish_postfix_casts(e));
22747                    }
22748                    match self.advance() {
22749                        Token::RParen => Ok(e),
22750                        other => Err(ParseError {
22751                            message: format!("expected ')', got {other:?}"),
22752                            token_pos: self.consumed_pos(),
22753                        }),
22754                    }
22755                }
22756            }
22757            Token::LBracket => self.parse_vector_literal_body(),
22758            Token::Extract => self.parse_extract_atom(),
22759            Token::Interval => self.parse_interval_atom(),
22760            // `LEFT` / `RIGHT` are reserved-keyword tokens because the
22761            // grammar dedicates arms for `LEFT [OUTER] JOIN` /
22762            // `RIGHT [OUTER] JOIN`. When followed by `(` we're in
22763            // expression position calling the PG `left(string, n)` /
22764            // `right(string, n)` function; rebuild the AST as a regular
22765            // function call so the engine's apply_function dispatch picks
22766            // it up. Delegated to a #[inline(never)] helper so its locals
22767            // don't bloat this recursive `parse_atom` frame (the nesting
22768            // budget in `enter_nested` is tuned to parse_atom's size).
22769            Token::Left if matches!(self.peek(), Token::LParen) => {
22770                self.parse_lr_string_function_call("left")
22771            }
22772            Token::Right if matches!(self.peek(), Token::LParen) => {
22773                self.parse_lr_string_function_call("right")
22774            }
22775            // v4.10: EXISTS / NOT EXISTS. EXISTS isn't a reserved
22776            // token; we match on the bare ident. NOT is a token
22777            // (consumed in the comparison rung), but `EXISTS (...)`
22778            // at the top of an expression starts here.
22779            Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("exists") => {
22780                self.parse_exists_atom(false)
22781            }
22782            // v7.13.0 — `CASE [<operand>] WHEN <cond> THEN <val>
22783            // [WHEN ...] [ELSE <val>] END` (mailrs round-5 G9).
22784            // CASE is a bare ident; we dispatch on lowercase match.
22785            Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("case") => {
22786                self.parse_case_atom()
22787            }
22788            // v7.37.17 (17.6 siblings) — PG typed datetime literals:
22789            // `DATE '2003-01-02'` / `TIMESTAMP '…'` / `TIMESTAMPTZ
22790            // '…'`. Lower onto the ::cast node so the existing
22791            // runtime text→date/timestamp paths do the parsing. The
22792            // string must follow immediately, else the ident stays a
22793            // plain column reference.
22794            Token::Ident(s)
22795                if typed_literal_cast_target(&s.to_ascii_lowercase()).is_some()
22796                    && matches!(self.peek(), Token::String(_)) =>
22797            {
22798                let target =
22799                    typed_literal_cast_target(&s.to_ascii_lowercase()).expect("guard checked");
22800                let Token::String(lit) = self.advance() else {
22801                    unreachable!("peek guaranteed a string token");
22802                };
22803                Ok(Expr::Cast {
22804                    expr: Box::new(Expr::Literal(Literal::String(lit))),
22805                    target,
22806                })
22807            }
22808            // v7.39 (round 221) — the SQL-standard long spellings:
22809            // `TIME [WITHOUT|WITH] TIME ZONE '…'` / `TIMESTAMP [WITHOUT|WITH]
22810            // TIME ZONE '…'`. Consume the modifier and lower to the same
22811            // typed-literal cast (`timetz` / `timestamptz` for WITH).
22812            Token::Ident(s)
22813                if (s.eq_ignore_ascii_case("time") || s.eq_ignore_ascii_case("timestamp"))
22814                    && matches!(self.peek(), Token::Ident(w) if w.eq_ignore_ascii_case("with")
22815                        || w.eq_ignore_ascii_case("without"))
22816                    && matches!(self.tokens.get(self.pos + 1), Some(Token::Ident(t)) if t.eq_ignore_ascii_case("time"))
22817                    && matches!(self.tokens.get(self.pos + 2), Some(Token::Ident(z)) if z.eq_ignore_ascii_case("zone"))
22818                    && matches!(self.tokens.get(self.pos + 3), Some(Token::String(_))) =>
22819            {
22820                let with_tz = matches!(self.peek(), Token::Ident(w) if w.eq_ignore_ascii_case("with"));
22821                self.advance(); // WITH / WITHOUT
22822                self.advance(); // TIME
22823                self.advance(); // ZONE
22824                let Token::String(lit) = self.advance() else {
22825                    unreachable!("guard checked a string token");
22826                };
22827                let base = s.to_ascii_lowercase();
22828                let target = match (base.as_str(), with_tz) {
22829                    ("time", true) => CastTarget::Named(alloc::string::String::from("timetz")),
22830                    ("time", false) => CastTarget::Named(alloc::string::String::from("time")),
22831                    (_, true) => CastTarget::Timestamptz,
22832                    (_, false) => CastTarget::Timestamp,
22833                };
22834                Ok(Expr::Cast {
22835                    expr: Box::new(Expr::Literal(Literal::String(lit))),
22836                    target,
22837                })
22838            }
22839            // v7.39 (read01 round 105) — `ARRAY(<subquery>)` constructor:
22840            // gathers the subquery's single-column rows (in its row order)
22841            // into an array. Desugared to `array_agg` over the subquery as a
22842            // derived table; out-of-line to keep parse_atom's frame small (it
22843            // sits on the recursive nesting-budget cycle).
22844            Token::Ident(s) | Token::QuotedIdent(s)
22845                if s.eq_ignore_ascii_case("array") && self.array_subquery_ahead() =>
22846            {
22847                self.parse_array_subquery()
22848            }
22849            // v7.10.10 — `ARRAY[expr, expr, …]` constructor. ARRAY
22850            // is not a reserved token; we match by case-insensitive
22851            // ident. The opening `[` must follow immediately. v7.39 (read01
22852            // round 105) — the body moved out-of-line so its `Vec`/loop locals
22853            // leave parse_atom's frame (which sits on the nesting-budget cycle).
22854            Token::Ident(s) | Token::QuotedIdent(s)
22855                if s.eq_ignore_ascii_case("array") && matches!(self.peek(), Token::LBracket) =>
22856            {
22857                self.parse_array_literal_body()
22858            }
22859            // v7.17.0 Phase 2.2 — MySQL `MATCH(col, ...) AGAINST
22860            // ('term' [IN BOOLEAN MODE | IN NATURAL LANGUAGE MODE])`.
22861            // We special-case before the generic ident dispatch so
22862            // the AGAINST clause never reaches the function-call
22863            // loop (which would mis-read `(cols) AGAINST` as a
22864            // call with no trailing modifier). The shape is
22865            // rewritten to a Boolean OR over per-column
22866            // `to_tsvector('simple', col) @@ plainto_tsquery('simple',
22867            // term)` so the existing FTS evaluator handles
22868            // semantics — the fulltext-GIN built at CREATE TABLE
22869            // time is currently a "real index that survives dump
22870            // round-trip"; the planner hook that actually uses
22871            // it for posting-list intersection lands in a later
22872            // sub-phase (Phase 2.2b) without touching this surface.
22873            Token::Ident(s) | Token::QuotedIdent(s)
22874                if s.eq_ignore_ascii_case("match") && matches!(self.peek(), Token::LParen) =>
22875            {
22876                self.parse_match_against_atom()
22877            }
22878            Token::Ident(s) | Token::QuotedIdent(s) => self.finish_ident_atom(s),
22879            // v7.37.43-T4 — PG-unreserved keywords are legal column /
22880            // alias names in expression context too. `release` appears
22881            // in sentori `0003_partition_events.sql` as both a column
22882            // reference (SELECT … release …) and an INSERT column list
22883            // entry. Mirrors `expect_ident_like`'s expansion of the
22884            // identifier set.
22885            other if unreserved_keyword_text(&other).is_some() => {
22886                let s = unreserved_keyword_text(&other).unwrap();
22887                self.finish_ident_atom(s)
22888            }
22889            // v7.39 (round 331, V50) — `@@var` in an EXPRESSION. It parsed
22890            // only inside `SET` before, so `SELECT @@autocommit` — which
22891            // every MySQL connector asks at handshake — was a parse error.
22892            // MariaDB accepts the bare, `@@session.` and `@@global.`
22893            // spellings alike and answers from the session's own value.
22894            Token::SessionVar(v) => {
22895                // v7.39 (round 430) — ONE `@` is a MySQL USER variable, which
22896                // has nothing to do with a `@@` engine setting: its own
22897                // per-session namespace, and an unset one reads NULL instead
22898                // of raising. Stripping every `@` (as this did) made `@x` and
22899                // `@@x` the same node, so `SELECT @x` answered "Unknown
22900                // system variable".
22901                Ok(variable_ref_atom(&v))
22902            }
22903            other => Err(ParseError {
22904                message: format!("unexpected token {other:?} in expression"),
22905                token_pos: tok_pos,
22906            }),
22907        }
22908        // After parsing the atom, fold any postfix `::vector` casts.
22909        .and_then(|atom| self.finish_postfix_casts(atom))
22910    }
22911
22912    /// Postfix operators on an atom: `::TYPE` cast and `IS [NOT] NULL`.
22913    /// Both bind tighter than any binary op.
22914    /// Shared cast-target parser for postfix `::TYPE` and the
22915    /// standard `CAST(expr AS TYPE)` form (v7.25, round-17).
22916    /// If the next tokens are `( N )`, consume them and return the canonical
22917    /// `base(N)` name so a temporal cast (`::timestamp(2)`) carries its
22918    /// fractional-seconds precision into `CastTarget::Named`; otherwise `None`.
22919    fn consume_temporal_typmod(&mut self, base: &str) -> Option<alloc::string::String> {
22920        if !matches!(self.peek(), Token::LParen) {
22921            return None;
22922        }
22923        self.advance(); // (
22924        let n = match self.advance() {
22925            Token::Integer(n) => n,
22926            _ => return Some(base.to_string()), // malformed → drop precision
22927        };
22928        if matches!(self.peek(), Token::RParen) {
22929            self.advance();
22930        }
22931        Some(alloc::format!("{base}({n})"))
22932    }
22933
22934    fn parse_cast_target(&mut self) -> Result<CastTarget, ParseError> {
22935        // r1052 — `::pg_catalog.regproc` and friends: pg_dump
22936        // schema-qualifies every cast target, and `pg_catalog.X` names
22937        // exactly the builtin type X. Consume the qualifier and let
22938        // the ordinary target parse decide.
22939        if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("pg_catalog"))
22940            && matches!(self.tokens.get(self.pos + 1), Some(Token::Dot))
22941        {
22942            self.advance();
22943            self.advance();
22944        }
22945        let target = match self.advance() {
22946            Token::Ident(s) => match s.to_ascii_lowercase().as_str() {
22947                "int" | "integer" | "int4" => {
22948                    if matches!(self.peek(), Token::LBracket)
22949                        && matches!(self.tokens.get(self.pos + 1), Some(Token::RBracket))
22950                    {
22951                        self.advance();
22952                        self.advance();
22953                        CastTarget::IntArray
22954                    } else {
22955                        CastTarget::Int
22956                    }
22957                }
22958                "bigint" | "int8" => {
22959                    if matches!(self.peek(), Token::LBracket)
22960                        && matches!(self.tokens.get(self.pos + 1), Some(Token::RBracket))
22961                    {
22962                        self.advance();
22963                        self.advance();
22964                        CastTarget::BigIntArray
22965                    } else {
22966                        CastTarget::BigInt
22967                    }
22968                }
22969                "float" | "double" => CastTarget::Float,
22970                "text" => {
22971                    // v7.10.11 — `::TEXT[]` widens to TextArray.
22972                    if matches!(self.peek(), Token::LBracket)
22973                        && matches!(self.tokens.get(self.pos + 1), Some(Token::RBracket))
22974                    {
22975                        self.advance();
22976                        self.advance();
22977                        CastTarget::TextArray
22978                    } else {
22979                        CastTarget::Text
22980                    }
22981                }
22982                "bool" | "boolean" => CastTarget::Bool,
22983                "vector" => CastTarget::Vector,
22984                "date" => CastTarget::Date,
22985                // v7.38 (read01) — `::timestamp(N)` carries its fractional-
22986                // seconds precision through the Named path (the engine rounds
22987                // the sub-second field); bare `::timestamp` keeps the fast arm.
22988                "timestamp" | "datetime" => match self.consume_temporal_typmod("timestamp") {
22989                    Some(named) => CastTarget::Named(named),
22990                    None => CastTarget::Timestamp,
22991                },
22992                "timestamptz" => match self.consume_temporal_typmod("timestamptz") {
22993                    Some(named) => CastTarget::Named(named),
22994                    None => CastTarget::Timestamptz,
22995                },
22996                "interval" => CastTarget::Interval,
22997                "json" => CastTarget::Json,
22998                "jsonb" => CastTarget::Jsonb,
22999                // v7.39 (round 694) — these have dedicated CastTarget
23000                // variants, so they never reached the postfix `[]` handling
23001                // further down and `::regtype[]` was a SYNTAX error at the
23002                // `]`. PG has an array type for every scalar; take the
23003                // suffix here and hand the canonical `<ty>_array` name to
23004                // the engine, the same shape every other array cast uses.
23005                "regtype" if self.peek_postfix_array_brackets() => {
23006                    self.advance();
23007                    self.advance();
23008                    CastTarget::Named(alloc::string::String::from("regtype_array"))
23009                }
23010                "regclass" if self.peek_postfix_array_brackets() => {
23011                    self.advance();
23012                    self.advance();
23013                    CastTarget::Named(alloc::string::String::from("regclass_array"))
23014                }
23015                "regtype" => CastTarget::RegType,
23016                "regclass" => CastTarget::RegClass,
23017                // v7.12.0 — `::tsvector` / `::tsquery`.
23018                // Engine decodes the LHS text via the PG
23019                // external form parser.
23020                // v7.39 (round 352, M8) — MySQL's own cast targets.
23021                // `CAST(x AS SIGNED)` / `UNSIGNED`, with the optional
23022                // `INTEGER` / `INT` tail MariaDB also accepts. PG has no
23023                // such type, so they are taken only in that dialect and
23024                // fall through to the "type does not exist" arm otherwise.
23025                "signed" | "unsigned" if self.mysql_dialect => {
23026                    if matches!(self.peek(), Token::Ident(k)
23027                        if k.eq_ignore_ascii_case("integer") || k.eq_ignore_ascii_case("int"))
23028                    {
23029                        self.advance();
23030                    }
23031                    CastTarget::Named(s.to_ascii_lowercase())
23032                }
23033                // v7.39 (round 352, M8) — `CAST(x AS CHAR)` is UNBOUNDED
23034                // in MySQL: MariaDB answers '123' where the SQL-standard
23035                // reading (PG's, and SPG's) is `char(1)` and answers '1'.
23036                // Truncating a number to its first digit is a wrong answer
23037                // with no error, so the MySQL session gets MySQL's reading.
23038                "char" if self.mysql_dialect && !matches!(self.peek(), Token::LParen) => {
23039                    CastTarget::Text
23040                }
23041                "tsvector" => CastTarget::TsVector,
23042                "tsquery" => CastTarget::TsQuery,
23043                // v7.17.0 — `::uuid`. Engine decodes the LHS
23044                // text via `spg_storage::parse_uuid_str`.
23045                "uuid" => CastTarget::Uuid,
23046                // v7.18 — `::bytea`. Engine decodes the LHS
23047                // text via the PG hex form (`'\xdeadbeef'`)
23048                // or escape form (`'\\x05\\x00'`). Closes
23049                // mailrs D-pre #3 reverse-acceptance gap.
23050                "bytea" => CastTarget::Bytea,
23051                // v7.37.5 ship triage — generic typed-cast escape.
23052                // Anything the long-tail PG type ident table knows
23053                // about(network/bit/geometry/multirange/etc.)flows
23054                // through `CastTarget::Named(canonical)`; the engine
23055                // resolves via `column_type_to_data_type` and dispatches
23056                // through the typed `coerce_value` path. Truly
23057                // unrecognised idents still hit the error arm below
23058                // because the engine rejects them.
23059                other => {
23060                    // Optional `(N[, M])` precision args — `::numeric(10,2)`,
23061                    // `::varchar(255)`, etc. Capture into the canonical
23062                    // `name(p,s)` form so `type_name_to_data_type` can
23063                    // reconstruct the `DataType::Numeric { precision,
23064                    // scale }` (and similar param-carrying types).
23065                    let mut name = other.to_string();
23066                    // v7.39 (round 281) — `::bit varying(3)` is two
23067                    // words; fold the tail in so the typmod reaches the
23068                    // type resolver instead of tripping the parser.
23069                    if name.eq_ignore_ascii_case("bit")
23070                        && matches!(self.peek(), Token::Ident(k) if k.eq_ignore_ascii_case("varying"))
23071                    {
23072                        self.advance();
23073                        name = alloc::string::String::from("varbit");
23074                    }
23075                    // v7.39 (round 613) — `::character varying` is the same
23076                    // two-word shape and had no fold, so the `varying` was
23077                    // left behind and the cast became a bare `character`,
23078                    // which is `char(1)`: `'ab'::CHARACTER VARYING` answered
23079                    // `a` where PG answers `ab`. Silently, and for a spelling
23080                    // pg_dump writes.
23081                    if name.eq_ignore_ascii_case("character")
23082                        && matches!(self.peek(), Token::Ident(k) if k.eq_ignore_ascii_case("varying"))
23083                    {
23084                        self.advance();
23085                        name = alloc::string::String::from("varchar");
23086                    }
23087                    if matches!(self.peek(), Token::LParen) {
23088                        let mut buf = alloc::string::String::from("(");
23089                        let mut depth = 0usize;
23090                        loop {
23091                            match self.advance() {
23092                                Token::LParen => {
23093                                    depth += 1;
23094                                    if depth > 1 {
23095                                        buf.push('(');
23096                                    }
23097                                }
23098                                Token::RParen => {
23099                                    depth -= 1;
23100                                    if depth == 0 {
23101                                        buf.push(')');
23102                                        break;
23103                                    }
23104                                    buf.push(')');
23105                                }
23106                                Token::Comma => buf.push(','),
23107                                Token::Integer(n) => buf.push_str(&alloc::format!("{n}")),
23108                                // v7.39 (round 273) — a minus used to fall
23109                                // into the catch-all below and vanish, so
23110                                // `::numeric(10,-2)` reached the engine as
23111                                // the text `numeric(10,2)` and silently
23112                                // rounded to two DECIMALS instead of to
23113                                // hundreds. A dropped token is not a
23114                                // no-op when it carries a sign.
23115                                Token::Minus => buf.push('-'),
23116                                Token::Eof => break,
23117                                _ => {}
23118                            }
23119                        }
23120                        name.push_str(&buf);
23121                    }
23122                    // Optional postfix `[]` widens to the array form —
23123                    // `::BOOL[]`, `::NUMERIC[]`, `::SMALLINT[]`, etc.
23124                    // The engine's `type_name_to_data_type` recognises
23125                    // the canonical `<ty>_array` form.
23126                    if matches!(self.peek(), Token::LBracket)
23127                        && matches!(self.tokens.get(self.pos + 1), Some(Token::RBracket))
23128                    {
23129                        self.advance();
23130                        self.advance();
23131                        name.push_str("_array");
23132                    }
23133                    CastTarget::Named(name)
23134                }
23135            },
23136            Token::Interval => CastTarget::Interval,
23137            // v7.39 — a quoted type name: `::"char"` is PG's 1-byte
23138            // "char" (oid 18, SPG Char1 — distinct from bare `char`
23139            // = char(1)); other quoted names resolve like idents.
23140            Token::QuotedIdent(q) => {
23141                if q.eq_ignore_ascii_case("char") {
23142                    CastTarget::Named("char1".into())
23143                } else {
23144                    CastTarget::Named(q.to_ascii_lowercase())
23145                }
23146            }
23147            other => {
23148                return Err(ParseError {
23149                    message: format!("expected type ident after `::`, got {other:?}"),
23150                    token_pos: self.consumed_pos(),
23151                });
23152            }
23153        };
23154        // v7.37.5 ship triage — postfix `[]` widens a scalar cast
23155        // target to its array sibling. Closed-enum arms (Bool /
23156        // SmallInt / Numeric / Float / Date / …) didn't carry the
23157        // explicit widening that Text / Int / BigInt did, so
23158        // `::BOOL[]` / `::NUMERIC[]` etc. surfaced as a parse
23159        // error. The widening here mirrors the per-arm Text /
23160        // Int / BigInt logic above + folds the new ζ-A first-class
23161        // types through `CastTarget::Named("<ty>_array")`.
23162        if matches!(self.peek(), Token::LBracket)
23163            && matches!(self.tokens.get(self.pos + 1), Some(Token::RBracket))
23164        {
23165            let widened = match &target {
23166                CastTarget::Bool => Some(CastTarget::Named("bool_array".to_string())),
23167                CastTarget::Date => Some(CastTarget::Named("date_array".to_string())),
23168                // v7.39 (round 326, V43) — the two temporal types stay
23169                // distinct. Both used to widen to `timestamptz_array`, so
23170                // `::timestamp[]` named the wrong target in its own error
23171                // message and lost the zone-less identity on the way.
23172                CastTarget::Timestamp => Some(CastTarget::Named("timestamp_array".to_string())),
23173                CastTarget::Timestamptz => Some(CastTarget::Named("timestamptz_array".to_string())),
23174                CastTarget::Uuid => Some(CastTarget::Named("uuid_array".to_string())),
23175                CastTarget::Json | CastTarget::Jsonb => {
23176                    Some(CastTarget::Named("jsonb_array".to_string()))
23177                }
23178                CastTarget::Bytea => Some(CastTarget::Named("bytea_array".to_string())),
23179                CastTarget::Interval => Some(CastTarget::Named("interval_array".to_string())),
23180                CastTarget::Float => Some(CastTarget::Named("float_array".to_string())),
23181                CastTarget::Named(name) => {
23182                    let mut a = name.clone();
23183                    a.push_str("_array");
23184                    Some(CastTarget::Named(a))
23185                }
23186                // Int / BigInt / Text / Vector / TsVector / TsQuery /
23187                // RegType / RegClass / TextArray / IntArray /
23188                // BigIntArray already finalised — leave as is.
23189                _ => None,
23190            };
23191            if let Some(w) = widened {
23192                self.advance();
23193                self.advance();
23194                return Ok(w);
23195            }
23196        }
23197        Ok(target)
23198    }
23199
23200    fn finish_postfix_casts(&mut self, mut expr: Expr) -> Result<Expr, ParseError> {
23201        loop {
23202            // v7.38 (read01, T9) — composite field access `(expr).field`.
23203            // A bare `a.b` is consumed as a qualified column inside the ident
23204            // atom, so a Dot only survives to this postfix position when the
23205            // base was a parenthesised expression (`(e).id`, `(row(1,2)).f1`).
23206            // `.*` whole-row expansion is not handled here (projection-level).
23207            if matches!(self.peek(), Token::Dot)
23208                && matches!(
23209                    self.tokens.get(self.pos + 1),
23210                    Some(Token::Ident(_) | Token::QuotedIdent(_))
23211                )
23212            {
23213                self.advance(); // .
23214                let field = match self.advance() {
23215                    Token::Ident(s) | Token::QuotedIdent(s) => s,
23216                    other => {
23217                        return Err(
23218                            self.err(format!("expected a field name after '.', got {other:?}"))
23219                        );
23220                    }
23221                };
23222                expr = Expr::FieldAccess {
23223                    base: Box::new(expr),
23224                    field,
23225                };
23226                continue;
23227            }
23228            if matches!(self.peek(), Token::DoubleColon) {
23229                self.advance();
23230                // v7.9.25 / v7.9.26 — broaden the postfix `::` cast
23231                // target set to include INTERVAL (reserved Token),
23232                // TIMESTAMPTZ, and PG catalog regtype / regclass.
23233                // mailrs follow-up H3a + H3b.
23234                let target = self.parse_cast_target()?;
23235                expr = Expr::Cast {
23236                    expr: Box::new(expr),
23237                    target,
23238                };
23239                continue;
23240            }
23241            // v7.10.12 — `arr[i]` subscript. PG 1-based; engine
23242            // returns NULL for out-of-range. Multiple subscripts
23243            // chain: `a[i][j]` parses left-to-right.
23244            if matches!(self.peek(), Token::LBracket) {
23245                self.advance();
23246                // `[lo:hi]` / `[:hi]` / `[lo:]` — array slice. A
23247                // bare index stays a subscript.
23248                let lo = if matches!(self.peek(), Token::Colon) {
23249                    None
23250                } else {
23251                    Some(self.parse_expr(0)?)
23252                };
23253                if matches!(self.peek(), Token::Colon) {
23254                    self.advance();
23255                    let hi = if matches!(self.peek(), Token::RBracket) {
23256                        None
23257                    } else {
23258                        Some(Box::new(self.parse_expr(0)?))
23259                    };
23260                    if !matches!(self.peek(), Token::RBracket) {
23261                        return Err(self.err(alloc::format!(
23262                            "expected ']' after array slice, got {:?}",
23263                            self.peek()
23264                        )));
23265                    }
23266                    self.advance();
23267                    expr = Expr::ArraySlice {
23268                        target: Box::new(expr),
23269                        lo: lo.map(Box::new),
23270                        hi,
23271                    };
23272                    continue;
23273                }
23274                let index = lo.expect("non-colon branch parsed an index");
23275                if !matches!(self.peek(), Token::RBracket) {
23276                    return Err(self.err(alloc::format!(
23277                        "expected ']' after array index, got {:?}",
23278                        self.peek()
23279                    )));
23280                }
23281                self.advance();
23282                expr = Expr::ArraySubscript {
23283                    target: Box::new(expr),
23284                    index: Box::new(index),
23285                };
23286                continue;
23287            }
23288            // `expr AT TIME ZONE zone` — lowers to PG's own function
23289            // form timezone(zone, expr); the scalar implements the
23290            // offset shift (named zones error there — no tzdata).
23291            if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("at"))
23292                && matches!(self.tokens.get(self.pos + 1),
23293                    Some(Token::Ident(s)) if s.eq_ignore_ascii_case("time"))
23294                && matches!(self.tokens.get(self.pos + 2),
23295                    Some(Token::Ident(s)) if s.eq_ignore_ascii_case("zone"))
23296            {
23297                self.advance(); // AT
23298                self.advance(); // TIME
23299                self.advance(); // ZONE
23300                // Zone at comparison precedence so AND/OR stay out.
23301                let zone = self.parse_expr(6)?;
23302                expr = Expr::FunctionCall {
23303                    name: "timezone".to_string(),
23304                    args: alloc::vec![zone, expr],
23305                };
23306                continue;
23307            }
23308            // `expr COLLATE "name"` — SPG's single text ordering IS
23309            // byte order, i.e. the C collation. The byte-order
23310            // spellings absorb as no-ops; a locale collation would
23311            // silently sort differently from PG, so it errors
23312            // honestly instead.
23313            if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("collate")) {
23314                self.advance();
23315                let mut cname = match self.advance() {
23316                    Token::Ident(s) | Token::QuotedIdent(s) | Token::String(s) => s,
23317                    other => {
23318                        return Err(self.err(alloc::format!(
23319                            "expected collation name after COLLATE, got {other:?}"
23320                        )));
23321                    }
23322                };
23323                // v7.39 (round 539) — a SCHEMA-QUALIFIED collation, which
23324                // is how `pg_dump` writes the default one:
23325                // `… COLLATE pg_catalog.default`. Reading a single token
23326                // left the SCHEMA as the name, so the clause was refused
23327                // as an unsupported locale collation and no dump ran.
23328                if matches!(self.peek(), Token::Dot) {
23329                    // v7.39.2 — the qualifier is DROPPED (SPG is single
23330                    // schema) but it is checked first. PostgreSQL 18.6
23331                    // answers `schema "nosuch_schema" does not exist` for
23332                    // one it has never heard of, and dropping it unread
23333                    // meant `COLLATE nosuch_schema."C"` succeeded here —
23334                    // a name that names nothing, accepted.
23335                    let schema = cname.to_ascii_lowercase();
23336                    if !matches!(
23337                        schema.as_str(),
23338                        "pg_catalog" | "public" | "information_schema"
23339                    ) {
23340                        return Err(self.err(alloc::format!("schema \"{cname}\" does not exist")));
23341                    }
23342                    self.advance();
23343                    cname = match self.advance() {
23344                        Token::Ident(s) | Token::QuotedIdent(s) | Token::String(s) => s,
23345                        // `default` lexes as a KEYWORD, and it is the name
23346                        // pg_dump writes — the same trap round 535 hit with
23347                        // TABLE / INDEX / FULL.
23348                        Token::Default => alloc::string::String::from("default"),
23349                        other => {
23350                            return Err(self.err(alloc::format!(
23351                                "expected collation name after COLLATE, got {other:?}"
23352                            )));
23353                        }
23354                    };
23355                }
23356                let lc = cname.to_ascii_lowercase();
23357                // v7.39 (round 371, M4 P4b) — a per-expression MySQL
23358                // collation override. `… COLLATE utf8mb4_bin` (any `_bin`
23359                // family / `binary`) forces byte-wise, which is exactly
23360                // what `BINARY expr` does — lower onto that so every fold
23361                // site (comparison, LIKE, ORDER BY) suppresses via
23362                // `is_binary_coerced`. A `_ci` family override folds, and
23363                // under the MySQL dialect the default already folds, so it
23364                // absorbs as a no-op; likewise the C / byte-order spellings.
23365                // v7.39.2 — against MySQL's own list, not against the
23366                // shape of the name. `nosuch_bin` took this shortcut and
23367                // became a BINARY cast; `nosuch_ci` took the one below
23368                // and was absorbed as a no-op. Either way the client
23369                // named a collation that does not exist and was told
23370                // nothing. An unknown name now falls through to the
23371                // node, and the engine refuses it.
23372                let real = crate::charset::is_mysql_collation(&lc);
23373                if self.mysql_dialect && real && (lc.ends_with("_bin") || lc == "binary") {
23374                    expr = Expr::Cast {
23375                        expr: alloc::boxed::Box::new(expr),
23376                        target: CastTarget::Named("binary".to_string()),
23377                    };
23378                    continue;
23379                }
23380                let mysql_ci = self.mysql_dialect
23381                    && ((real && lc.ends_with("_ci"))
23382                        || matches!(lc.as_str(), "case_insensitive" | "nocase"));
23383                // v7.39 (round 691/692) — inside an ORDER BY key EVERY name
23384                // goes to the lowering channel, the byte-order spellings
23385                // included. Round 691 recorded only the names the old
23386                // allow-list rejected, which left `ORDER BY a COLLATE "C"`
23387                // absorbed as a no-op — and once a column could declare a
23388                // collation, absorbing the clause meant the COLUMN's
23389                // collation won where the query had asked for bytes.
23390                if self.in_order_by_key && !mysql_ci {
23391                    self.order_key_collation = Some(cname);
23392                    continue;
23393                }
23394                // v7.39.2 — the clause becomes a NODE rather than being
23395                // refused or absorbed.
23396                //
23397                // What stood here refused the locale names and SILENTLY
23398                // DROPPED the byte-order ones, so `'a' COLLATE "C" < 'B'`
23399                // answered `t` where PostgreSQL 18.6 answers `f`: the one
23400                // family it let through is the one where dropping it
23401                // changes the answer. Absorbing is only correct when the
23402                // collation asked for is the one the comparison would use
23403                // anyway, and that depends on the DATABASE — which the
23404                // parser cannot see. So it rides along and the engine,
23405                // which can, decides.
23406                //
23407                // `collate_derive` already modelled `Explicit(name)` and
23408                // had no way to be handed one.
23409                // v7.39.2 — a MySQL spelling does not exist on the
23410                // PostgreSQL wire, and THIS is where the wire is known.
23411                //
23412                // The check lived in the evaluator first and asked
23413                // `ctx.mysql_dialect`, which the INSERT path builds as a
23414                // hard-coded `false` — so `INSERT … VALUES (_utf8mb4'x')`
23415                // in a MySQL session was refused for a collation that
23416                // does not exist on a wire it was not on. Making that
23417                // context truthful would change INSERT-time evaluation
23418                // in other ways as a side effect; the parser already
23419                // gates the introducer on the same flag and is the
23420                // honest place to ask.
23421                if !self.mysql_dialect
23422                    && (lc.ends_with("_ci")
23423                        || lc.ends_with("_cs")
23424                        || lc.ends_with("_bin")
23425                        || lc == "binary"
23426                        || matches!(lc.as_str(), "case_insensitive" | "nocase"))
23427                {
23428                    return Err(self.err(alloc::format!(
23429                        "collation \"{cname}\" for encoding \"UTF8\" does not exist"
23430                    )));
23431                }
23432                // v7.39.3 — the node is built for EVERY name, `_ci`
23433                // included.
23434                //
23435                // A MySQL `_ci` spelling used to be absorbed here on the
23436                // reasoning that a MySQL session folds anyway, so the
23437                // clause asked for what it would have got. That stopped
23438                // being true when the fold learned to read the session's
23439                // collation NAME: under `SET NAMES utf8mb4 COLLATE
23440                // utf8mb4_bin`, `'AB' COLLATE utf8mb4_general_ci = 'ab'`
23441                // is 1 on MySQL 9.7.2 and was 0 here, because the clause
23442                // that would have made it 1 had been dropped in the
23443                // parser. Absorbing is only ever correct when the
23444                // collation asked for is the one the comparison would use
23445                // anyway, and the parser cannot know that — the same
23446                // reasoning already written above for the byte-order
23447                // spellings, applied to the family it had exempted.
23448                expr = Expr::Collate {
23449                    expr: alloc::boxed::Box::new(expr),
23450                    collation: cname,
23451                };
23452                continue;
23453            }
23454            return Ok(expr);
23455        }
23456    }
23457
23458    /// v7.39 (round 696) — a comma-separated list of bare names, stopping at
23459    /// the first token that is not one. Schema qualifiers collapse to the
23460    /// last part, which is what every other name path here does (SPG is
23461    /// single-schema).
23462    fn take_comma_separated_names(&mut self) -> Vec<String> {
23463        let mut out = Vec::new();
23464        while let Token::Ident(n) | Token::QuotedIdent(n) = self.peek().clone() {
23465            self.advance();
23466            let mut last = n;
23467            while matches!(self.peek(), Token::Dot) {
23468                self.advance();
23469                if let Token::Ident(t) | Token::QuotedIdent(t) = self.advance() {
23470                    last = t;
23471                }
23472            }
23473            out.push(last);
23474            if matches!(self.peek(), Token::Comma) {
23475                self.advance();
23476            } else {
23477                break;
23478            }
23479        }
23480        out
23481    }
23482
23483    /// v7.39 (round 694) — is the next token pair a postfix `[]`?
23484    ///
23485    /// The general cast-target path tests this inline; the types with their
23486    /// own `CastTarget` variant need it as a guard on their match arm,
23487    /// which is what this exists for.
23488    fn peek_postfix_array_brackets(&self) -> bool {
23489        matches!(self.peek(), Token::LBracket)
23490            && matches!(self.tokens.get(self.pos + 1), Some(Token::RBracket))
23491    }
23492
23493    /// Parse the operator tail after a `(a, b, …)` row constructor
23494    /// and expand at parse time. `=` is the conjunction of element
23495    /// equalities; `<>` its negation; the order operators expand
23496    /// lexicographically; `[NOT] IN ( (row), … )` ORs the row
23497    /// equalities. Anything else (a bare row value, a subquery
23498    /// RHS) errors honestly — SPG has no composite runtime value.
23499    fn parse_row_comparison_tail(&mut self, row: Vec<Expr>) -> Result<Expr, ParseError> {
23500        fn row_eq(lhs: &[Expr], rhs: &[Expr]) -> Expr {
23501            let mut it = lhs.iter().zip(rhs.iter()).map(|(l, r)| Expr::Binary {
23502                lhs: Box::new(l.clone()),
23503                op: BinOp::Eq,
23504                rhs: Box::new(r.clone()),
23505            });
23506            let first = it.next().expect("row has at least two elements");
23507            it.fold(first, |acc, e| Expr::Binary {
23508                lhs: Box::new(acc),
23509                op: BinOp::And,
23510                rhs: Box::new(e),
23511            })
23512        }
23513        // Lexicographic (a,b) OP (c,d):
23514        //   a STRICT c OR (a = c AND (b OP d))  — recursing right.
23515        fn row_lex(lhs: &[Expr], rhs: &[Expr], strict: BinOp, last: BinOp) -> Expr {
23516            if lhs.len() == 1 {
23517                return Expr::Binary {
23518                    lhs: Box::new(lhs[0].clone()),
23519                    op: last,
23520                    rhs: Box::new(rhs[0].clone()),
23521                };
23522            }
23523            let head_strict = Expr::Binary {
23524                lhs: Box::new(lhs[0].clone()),
23525                op: strict,
23526                rhs: Box::new(rhs[0].clone()),
23527            };
23528            let head_eq = Expr::Binary {
23529                lhs: Box::new(lhs[0].clone()),
23530                op: BinOp::Eq,
23531                rhs: Box::new(rhs[0].clone()),
23532            };
23533            Expr::Binary {
23534                lhs: Box::new(head_strict),
23535                op: BinOp::Or,
23536                rhs: Box::new(Expr::Binary {
23537                    lhs: Box::new(head_eq),
23538                    op: BinOp::And,
23539                    rhs: Box::new(row_lex(&lhs[1..], &rhs[1..], strict, last)),
23540                }),
23541            }
23542        }
23543        let negated_in = if matches!(self.peek(), Token::Not)
23544            && matches!(self.tokens.get(self.pos + 1), Some(Token::In))
23545        {
23546            self.advance();
23547            true
23548        } else {
23549            false
23550        };
23551        if matches!(self.peek(), Token::In) {
23552            self.advance();
23553            if !matches!(self.peek(), Token::LParen) {
23554                return Err(self.err(alloc::format!(
23555                    "expected '(' after row IN, got {:?}",
23556                    self.peek()
23557                )));
23558            }
23559            self.advance();
23560            // `(a, b) [NOT] IN (SELECT x, y)` — a multi-column subquery,
23561            // not a list of literal rows. Row-vs-list decomposes to
23562            // OR-of-AND above, but the subquery's rows are only known at
23563            // runtime, so keep it as a RowInSubquery node.
23564            if matches!(self.peek(), Token::Select) {
23565                let inner = self.parse_select_stmt()?;
23566                if !matches!(self.peek(), Token::RParen) {
23567                    return Err(self.err(alloc::format!(
23568                        "expected ')' after row IN-subquery, got {:?}",
23569                        self.peek()
23570                    )));
23571                }
23572                self.advance();
23573                let Statement::Select(s) = inner else {
23574                    unreachable!("parse_select_stmt always returns Statement::Select")
23575                };
23576                return Ok(Expr::RowInSubquery {
23577                    row,
23578                    subquery: Box::new(s),
23579                    negated: negated_in,
23580                });
23581            }
23582            let mut alternatives: Vec<Expr> = Vec::new();
23583            loop {
23584                // Optional ROW keyword before the paren row.
23585                if matches!(self.peek(), Token::Ident(k) if k.eq_ignore_ascii_case("row"))
23586                    && matches!(self.tokens.get(self.pos + 1), Some(Token::LParen))
23587                {
23588                    self.advance();
23589                }
23590                if !matches!(self.peek(), Token::LParen) {
23591                    return Err(self.err(alloc::format!(
23592                        "expected '(' to open a row inside IN, got {:?}",
23593                        self.peek()
23594                    )));
23595                }
23596                self.advance();
23597                let mut rhs = alloc::vec![self.parse_expr(0)?];
23598                while matches!(self.peek(), Token::Comma) {
23599                    self.advance();
23600                    rhs.push(self.parse_expr(0)?);
23601                }
23602                if !matches!(self.peek(), Token::RParen) {
23603                    return Err(self.err(alloc::format!(
23604                        "expected ')' after row inside IN, got {:?}",
23605                        self.peek()
23606                    )));
23607                }
23608                self.advance();
23609                if rhs.len() != row.len() {
23610                    return Err(self.err(alloc::format!(
23611                        "row IN arity mismatch: left has {}, right has {}",
23612                        row.len(),
23613                        rhs.len()
23614                    )));
23615                }
23616                alternatives.push(row_eq(&row, &rhs));
23617                if matches!(self.peek(), Token::Comma) {
23618                    self.advance();
23619                    continue;
23620                }
23621                break;
23622            }
23623            if !matches!(self.peek(), Token::RParen) {
23624                return Err(self.err(alloc::format!(
23625                    "expected ')' to close row IN list, got {:?}",
23626                    self.peek()
23627                )));
23628            }
23629            self.advance();
23630            let mut it = alternatives.into_iter();
23631            let first = it.next().expect("IN list has at least one row");
23632            let combined = it.fold(first, |acc, e| Expr::Binary {
23633                lhs: Box::new(acc),
23634                op: BinOp::Or,
23635                rhs: Box::new(e),
23636            });
23637            return Ok(maybe_not(combined, negated_in));
23638        }
23639        // SQL-standard `(S1, E1) OVERLAPS (S2, E2)` — true when the
23640        // two periods share at least one time point. Each pair is
23641        // normalised with least/greatest (PG accepts the endpoints
23642        // in either order), then lowered to the standard
23643        // `start1 < end2 AND start2 < end1` form.
23644        if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("overlaps")) {
23645            if row.len() != 2 {
23646                return Err(self.err(alloc::format!(
23647                    "OVERLAPS needs (start, end) pairs; left side has {} elements",
23648                    row.len()
23649                )));
23650            }
23651            self.advance();
23652            if matches!(self.peek(), Token::Ident(k) if k.eq_ignore_ascii_case("row"))
23653                && matches!(self.tokens.get(self.pos + 1), Some(Token::LParen))
23654            {
23655                self.advance();
23656            }
23657            if !matches!(self.peek(), Token::LParen) {
23658                return Err(self.err(alloc::format!(
23659                    "expected '(' after OVERLAPS, got {:?}",
23660                    self.peek()
23661                )));
23662            }
23663            self.advance();
23664            let r0 = self.parse_expr(0)?;
23665            if !matches!(self.peek(), Token::Comma) {
23666                return Err(self.err(alloc::format!(
23667                    "OVERLAPS needs (start, end) on the right, got {:?}",
23668                    self.peek()
23669                )));
23670            }
23671            self.advance();
23672            let r1 = self.parse_expr(0)?;
23673            if !matches!(self.peek(), Token::RParen) {
23674                return Err(self.err(alloc::format!(
23675                    "expected ')' after OVERLAPS pair, got {:?}",
23676                    self.peek()
23677                )));
23678            }
23679            self.advance();
23680            let pair_fn = |name: &str, a: &Expr, b: &Expr| Expr::FunctionCall {
23681                name: String::from(name),
23682                args: alloc::vec![a.clone(), b.clone()],
23683            };
23684            let lt = |lhs: Expr, rhs: Expr| Expr::Binary {
23685                lhs: Box::new(lhs),
23686                op: BinOp::Lt,
23687                rhs: Box::new(rhs),
23688            };
23689            return Ok(Expr::Binary {
23690                lhs: Box::new(lt(
23691                    pair_fn("least", &row[0], &row[1]),
23692                    pair_fn("greatest", &r0, &r1),
23693                )),
23694                op: BinOp::And,
23695                rhs: Box::new(lt(
23696                    pair_fn("least", &r0, &r1),
23697                    pair_fn("greatest", &row[0], &row[1]),
23698                )),
23699            });
23700        }
23701        // `(a, b, …) IS [NOT] NULL` — the SQL row null predicate. Per
23702        // PG, `IS NULL` is true only when EVERY field is NULL, and
23703        // `IS NOT NULL` is true only when every field is non-NULL — the
23704        // latter is NOT the negation of the former (a mixed row is
23705        // neither). Desugar to an AND chain of per-field `IS [NOT] NULL`,
23706        // which reproduces exactly that all-fields semantics.
23707        if matches!(self.peek(), Token::Is) {
23708            self.advance();
23709            let negated = if matches!(self.peek(), Token::Not) {
23710                self.advance();
23711                true
23712            } else {
23713                false
23714            };
23715            if !matches!(self.peek(), Token::Null) {
23716                return Err(self.err(alloc::format!(
23717                    "expected NULL after row IS [NOT], got {:?}",
23718                    self.peek()
23719                )));
23720            }
23721            self.advance();
23722            let mut it = row.iter().map(|e| Expr::IsNull {
23723                expr: Box::new(e.clone()),
23724                negated,
23725            });
23726            let first = it.next().expect("row has at least two elements");
23727            return Ok(it.fold(first, |acc, e| Expr::Binary {
23728                lhs: Box::new(acc),
23729                op: BinOp::And,
23730                rhs: Box::new(e),
23731            }));
23732        }
23733        let op = match self.peek() {
23734            Token::Eq => BinOp::Eq,
23735            Token::NotEq => BinOp::NotEq,
23736            Token::Lt => BinOp::Lt,
23737            Token::LtEq => BinOp::LtEq,
23738            Token::Gt => BinOp::Gt,
23739            Token::GtEq => BinOp::GtEq,
23740            // v7.38 (read01, composite) — a bare `(a, b, …)` not followed by a
23741            // comparison / [NOT] IN / IS [NOT] NULL / OVERLAPS is a row (record)
23742            // constructor value, identical to the `ROW(a, b, …)` keyword form:
23743            // `(1,'a')::text` → `(1,a)`, `SELECT (1,2,3)` → `(1,2,3)`. Postfix
23744            // (`::text`, `.field`) applies at the caller just as it does for the
23745            // ROW(...) node. All the comparison / predicate forms returned above.
23746            _ => {
23747                return Ok(Expr::FunctionCall {
23748                    name: String::from("row"),
23749                    args: row,
23750                });
23751            }
23752        };
23753        self.advance();
23754        // Optional ROW keyword before the paren row.
23755        if matches!(self.peek(), Token::Ident(k) if k.eq_ignore_ascii_case("row"))
23756            && matches!(self.tokens.get(self.pos + 1), Some(Token::LParen))
23757        {
23758            self.advance();
23759        }
23760        if !matches!(self.peek(), Token::LParen) {
23761            return Err(self.err(alloc::format!(
23762                "expected '(' to open the right-hand row, got {:?}",
23763                self.peek()
23764            )));
23765        }
23766        self.advance();
23767        // `(a, b) <op> (SELECT x, y)` — compare against a single-row
23768        // subquery. Kept as a node (the subquery's row is a runtime value);
23769        // the literal-RHS form below still decomposes at parse time.
23770        if matches!(self.peek(), Token::Select) {
23771            let inner = self.parse_select_stmt()?;
23772            if !matches!(self.peek(), Token::RParen) {
23773                return Err(self.err(alloc::format!(
23774                    "expected ')' after row comparison subquery, got {:?}",
23775                    self.peek()
23776                )));
23777            }
23778            self.advance();
23779            let Statement::Select(s) = inner else {
23780                unreachable!("parse_select_stmt always returns Statement::Select")
23781            };
23782            return Ok(Expr::RowCmpSubquery {
23783                row,
23784                op,
23785                subquery: Box::new(s),
23786            });
23787        }
23788        let mut rhs = alloc::vec![self.parse_expr(0)?];
23789        while matches!(self.peek(), Token::Comma) {
23790            self.advance();
23791            rhs.push(self.parse_expr(0)?);
23792        }
23793        if !matches!(self.peek(), Token::RParen) {
23794            return Err(self.err(alloc::format!(
23795                "expected ')' after right-hand row, got {:?}",
23796                self.peek()
23797            )));
23798        }
23799        self.advance();
23800        if rhs.len() != row.len() {
23801            // v7.39 (round 239) — PG's wording (42601).
23802            return Err(self.err("unequal number of entries in row expressions".to_string()));
23803        }
23804        Ok(match op {
23805            BinOp::Eq => row_eq(&row, &rhs),
23806            BinOp::NotEq => maybe_not(row_eq(&row, &rhs), true),
23807            BinOp::Lt => row_lex(&row, &rhs, BinOp::Lt, BinOp::Lt),
23808            BinOp::LtEq => row_lex(&row, &rhs, BinOp::Lt, BinOp::LtEq),
23809            BinOp::Gt => row_lex(&row, &rhs, BinOp::Gt, BinOp::Gt),
23810            BinOp::GtEq => row_lex(&row, &rhs, BinOp::Gt, BinOp::GtEq),
23811            _ => unreachable!("op restricted above"),
23812        })
23813    }
23814
23815    /// `LIKE p ESCAPE 'c'` — rewrite the pattern so the custom
23816    /// escape character becomes the matcher's default backslash:
23817    /// `c%` (escaped wildcard) → `\%`, `cc` (literal escape char)
23818    /// → the char itself, and any pre-existing backslash escapes
23819    /// itself so it stays literal. Both operands must be string
23820    /// literals — a runtime pattern would need matcher support.
23821    fn rewrite_like_escape(pattern: Expr, esc: Expr) -> Result<Expr, String> {
23822        let (Expr::Literal(Literal::String(p)), Expr::Literal(Literal::String(e))) =
23823            (&pattern, &esc)
23824        else {
23825            return Err(
23826                "LIKE ... ESCAPE requires string-literal pattern and escape \
23827                 (runtime escape characters are not supported yet)"
23828                    .into(),
23829            );
23830        };
23831        // v7.38 (read01 P6.18) — PG accepts `ESCAPE ''` to mean "no escape
23832        // character" (every `%`/`_` is a wildcard, nothing is escaped). Only a
23833        // multi-character escape is an error.
23834        let esc_ch: Option<char> = {
23835            let mut ch_iter = e.chars();
23836            match (ch_iter.next(), ch_iter.next()) {
23837                (Some(c), None) => Some(c),
23838                (None, _) => None,
23839                (Some(_), Some(_)) => {
23840                    return Err(alloc::format!(
23841                        "ESCAPE must be a single character, got {e:?}"
23842                    ));
23843                }
23844            }
23845        };
23846        let mut out = String::with_capacity(p.len() + 4);
23847        let mut chars = p.chars();
23848        while let Some(c) = chars.next() {
23849            if Some(c) == esc_ch {
23850                match chars.next() {
23851                    // Escaped wildcard / escaped escape → keep the
23852                    // next char literal via backslash.
23853                    Some(next) => {
23854                        out.push('\\');
23855                        out.push(next);
23856                    }
23857                    None => {
23858                        return Err("LIKE pattern ends with the escape character".into());
23859                    }
23860                }
23861            } else if c == '\\' && esc_ch != Some('\\') {
23862                // A raw backslash is literal under a custom (or absent) escape
23863                // — escape it for the backslash-based matcher.
23864                out.push('\\');
23865                out.push('\\');
23866            } else {
23867                out.push(c);
23868            }
23869        }
23870        Ok(Expr::Literal(Literal::String(out)))
23871    }
23872
23873    /// `x [NOT] LIKE ANY/ALL (ARRAY[p1, p2, …])` — quantified pattern
23874    /// match. Desugars to an OR (ANY) / AND (ALL) chain of per-element
23875    /// `x [NOT] LIKE pi`, which reproduces PG's three-valued semantics
23876    /// exactly (a NULL pattern makes an element NULL; `false OR NULL` =
23877    /// NULL, `true AND NULL` = NULL, …). ANY over an empty array is
23878    /// FALSE, ALL over empty is TRUE. Returns `None` when the token after
23879    /// LIKE is not `ANY(`/`ALL(`, so the caller falls back to a plain
23880    /// pattern. Only a literal `ARRAY[...]` is accepted today — a runtime
23881    /// array expression errors honestly rather than silently mismatching.
23882    fn try_like_any_all(
23883        &mut self,
23884        base: &Expr,
23885        negated: bool,
23886        case_insensitive: bool,
23887    ) -> Result<Option<Expr>, ParseError> {
23888        let is_any = match self.peek() {
23889            Token::All if matches!(self.tokens.get(self.pos + 1), Some(Token::LParen)) => false,
23890            Token::Ident(s)
23891                if s.eq_ignore_ascii_case("any")
23892                    && matches!(self.tokens.get(self.pos + 1), Some(Token::LParen)) =>
23893            {
23894                true
23895            }
23896            _ => return Ok(None),
23897        };
23898        self.advance(); // ANY / ALL
23899        self.advance(); // '('
23900        let arr = self.parse_expr(0)?;
23901        if !matches!(self.peek(), Token::RParen) {
23902            return Err(self.err(format!(
23903                "expected ')' after LIKE {} argument, got {:?}",
23904                if is_any { "ANY" } else { "ALL" },
23905                self.peek()
23906            )));
23907        }
23908        self.advance(); // ')'
23909        let Expr::Array(items) = arr else {
23910            return Err(self.err(
23911                "LIKE ANY/ALL currently requires a literal ARRAY[...] of patterns".to_string(),
23912            ));
23913        };
23914        let mut clauses = items.into_iter().map(|p| Expr::Like {
23915            expr: Box::new(base.clone()),
23916            pattern: Box::new(p),
23917            negated,
23918            case_insensitive,
23919        });
23920        let Some(first) = clauses.next() else {
23921            // ANY(empty) = FALSE, ALL(empty) = TRUE.
23922            return Ok(Some(Expr::Literal(Literal::Bool(!is_any))));
23923        };
23924        let op = if is_any { BinOp::Or } else { BinOp::And };
23925        let combined = clauses.fold(first, |acc, c| Expr::Binary {
23926            lhs: Box::new(acc),
23927            op,
23928            rhs: Box::new(c),
23929        });
23930        Ok(Some(combined))
23931    }
23932
23933    /// `x BETWEEN low AND high`  →  `(x >= low) AND (x <= high)`, wrapped in
23934    /// `NOT` when `negated`. Bounds parse at precedence 5 so the trailing
23935    /// `AND` is not swallowed.
23936    fn parse_between_tail(&mut self, expr: Expr, negated: bool) -> Result<Expr, ParseError> {
23937        self.advance(); // BETWEEN
23938        // SYMMETRIC — the bounds may arrive in either order; both
23939        // orientations OR together. ASYMMETRIC is the default and
23940        // absorbs as noise.
23941        let symmetric = if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("symmetric"))
23942        {
23943            self.advance();
23944            true
23945        } else {
23946            if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("asymmetric")) {
23947                self.advance();
23948            }
23949            false
23950        };
23951        let low = self.parse_expr(6)?;
23952        if !matches!(self.peek(), Token::And) {
23953            return Err(self.err(format!(
23954                "expected AND after BETWEEN low bound, got {:?}",
23955                self.peek()
23956            )));
23957        }
23958        self.advance();
23959        let high = self.parse_expr(6)?;
23960        let target = Box::new(expr);
23961        let range = |lo: Expr, hi: Expr| Expr::Binary {
23962            lhs: Box::new(Expr::Binary {
23963                lhs: target.clone(),
23964                op: BinOp::GtEq,
23965                rhs: Box::new(lo),
23966            }),
23967            op: BinOp::And,
23968            rhs: Box::new(Expr::Binary {
23969                lhs: target.clone(),
23970                op: BinOp::LtEq,
23971                rhs: Box::new(hi),
23972            }),
23973        };
23974        let combined = if symmetric {
23975            Expr::Binary {
23976                lhs: Box::new(range(low.clone(), high.clone())),
23977                op: BinOp::Or,
23978                rhs: Box::new(range(high, low)),
23979            }
23980        } else {
23981            range(low, high)
23982        };
23983        Ok(maybe_not(combined, negated))
23984    }
23985
23986    /// `x IN (a, b, c)`  →  chained OR of equalities. Empty list collapses
23987    /// to FALSE (TRUE under NOT IN), matching standard SQL semantics.
23988    /// v4.11: parse `WITH name AS (SELECT ...) [, ...] SELECT ...`.
23989    /// Caller already consumed the leading `WITH` ident.
23990    /// v7.38 (read01) — recursive-CTE well-formedness. PG rejects ORDER BY
23991    /// / LIMIT / OFFSET anywhere in a recursive query, and a recursive
23992    /// self-reference that appears more than once in a single term.
23993    fn validate_recursive_cte(&self, cte: &crate::ast::Cte) -> Result<(), ParseError> {
23994        use crate::ast::{CteBody, SelectStatement};
23995        if !cte.recursive {
23996            return Ok(());
23997        }
23998        let CteBody::Select(body) = &cte.body else {
23999            return Ok(());
24000        };
24001        // A recursive CTE body is `base UNION [ALL] recursive [UNION …]`;
24002        // check the anchor and every peer term.
24003        let has_order = |s: &SelectStatement| !s.order_by.is_empty();
24004        let has_limit = |s: &SelectStatement| s.limit.is_some() || s.offset.is_some();
24005        if has_order(body) || body.unions.iter().any(|(_, u)| has_order(u)) {
24006            return Err(self.err(String::from(
24007                "ORDER BY in a recursive query is not implemented",
24008            )));
24009        }
24010        if has_limit(body) || body.unions.iter().any(|(_, u)| has_limit(u)) {
24011            return Err(self.err(String::from(
24012                "LIMIT in a recursive query is not implemented",
24013            )));
24014        }
24015        let self_refs = |s: &SelectStatement| -> usize {
24016            let Some(from) = &s.from else {
24017                return 0;
24018            };
24019            let mut n = usize::from(from.primary.name.eq_ignore_ascii_case(&cte.name));
24020            for j in &from.joins {
24021                if j.table.name.eq_ignore_ascii_case(&cte.name) {
24022                    n += 1;
24023                }
24024            }
24025            n
24026        };
24027        if body.unions.iter().any(|(_, u)| self_refs(u) > 1) {
24028            return Err(self.err(alloc::format!(
24029                "recursive reference to query \"{}\" must not appear more than once",
24030                cte.name
24031            )));
24032        }
24033        // v7.39 (round 145, parse_cte.c) — the remaining well-formedness rules
24034        // apply only when the body actually references itself (a non-self-
24035        // referencing CTE under WITH RECURSIVE may use any set-op shape).
24036        let anchor_refs = self_refs(body);
24037        let union_refs = body.unions.iter().any(|(_, u)| self_refs(u) > 0);
24038        if anchor_refs > 0 || union_refs {
24039            // Shape: the top level must be UNION [ALL] arms only. A self-ref
24040            // under INTERSECT / EXCEPT (or with no set-op at all) is PG's
24041            // "does not have the form" error — SPG used to compute a value.
24042            if body.unions.is_empty()
24043                || body.unions.iter().any(|(k, _)| {
24044                    !matches!(
24045                        k,
24046                        crate::ast::UnionKind::Distinct | crate::ast::UnionKind::All
24047                    )
24048                })
24049            {
24050                return Err(self.err(alloc::format!(
24051                    "recursive query \"{}\" does not have the form non-recursive-term \
24052                     UNION [ALL] recursive-term",
24053                    cte.name
24054                )));
24055            }
24056            if anchor_refs > 0 {
24057                return Err(self.err(alloc::format!(
24058                    "recursive reference to query \"{}\" must not appear within its non-recursive term",
24059                    cte.name
24060                )));
24061            }
24062        }
24063        let is_self = |t: &crate::ast::TableRef| t.name.eq_ignore_ascii_case(&cte.name);
24064        for (_, u) in &body.unions {
24065            if self_refs(u) == 0 {
24066                continue;
24067            }
24068            // The self-reference must not sit on the nullable side of an outer
24069            // join (LEFT: right side; RIGHT: everything before it; FULL: both).
24070            if let Some(from) = &u.from {
24071                for (i, j) in from.joins.iter().enumerate() {
24072                    let left_has_self = is_self(&from.primary)
24073                        || from.joins[..i].iter().any(|pj| is_self(&pj.table));
24074                    let violated = match j.kind {
24075                        crate::ast::JoinKind::Left => is_self(&j.table),
24076                        crate::ast::JoinKind::Right => left_has_self,
24077                        crate::ast::JoinKind::FullOuter => is_self(&j.table) || left_has_self,
24078                        _ => false,
24079                    };
24080                    if violated {
24081                        return Err(self.err(alloc::format!(
24082                            "recursive reference to query \"{}\" must not appear within an outer join",
24083                            cte.name
24084                        )));
24085                    }
24086                }
24087            }
24088            // No aggregates at the top level of the recursive term (SPG used
24089            // to run them and surface a misleading downstream error).
24090            let mut items_and_having: Vec<&Expr> = Vec::new();
24091            for it in &u.items {
24092                if let crate::ast::SelectItem::Expr { expr, .. } = it {
24093                    items_and_having.push(expr);
24094                }
24095            }
24096            if let Some(h) = &u.having {
24097                items_and_having.push(h);
24098            }
24099            for e in items_and_having {
24100                if expr_has_toplevel_aggregate(e) {
24101                    return Err(self.err(String::from(
24102                        "aggregate functions are not allowed in a recursive query's recursive term",
24103                    )));
24104                }
24105            }
24106        }
24107        // A self-reference inside a sublink expression (EXISTS / IN / scalar
24108        // subquery) anywhere in the body is rejected; a plain FROM derived
24109        // table is legal in PG and untouched here.
24110        let mut all_terms: Vec<&SelectStatement> = alloc::vec![body];
24111        all_terms.extend(body.unions.iter().map(|(_, u)| u));
24112        for term in all_terms {
24113            if select_has_self_ref_in_sublink(term, &cte.name) {
24114                return Err(self.err(alloc::format!(
24115                    "recursive reference to query \"{}\" must not appear within a subquery",
24116                    cte.name
24117                )));
24118            }
24119        }
24120        Ok(())
24121    }
24122
24123    /// v7.38 (read01 U16) — desugar a CTE's SEARCH / CYCLE clause into
24124    /// extra body columns, mirroring PG's `rewriteSearchAndCycle`. Runs
24125    /// right after parse so the engine sees a plain recursive CTE with the
24126    /// tracking columns already projected. DEPTH FIRST and CYCLE are
24127    /// supported; BREADTH FIRST needs numeric-composite ordering SPG's
24128    /// text-rendered rows can't provide, and errors honestly.
24129    fn desugar_cte_search_cycle(&self, cte: &mut crate::ast::Cte) -> Result<(), ParseError> {
24130        use crate::ast::{BinOp, ColumnName, CteBody, Expr, Literal, SelectItem, UnOp};
24131        if cte.search.is_none() && cte.cycle.is_none() {
24132            return Ok(());
24133        }
24134        let cte_name = cte.name.clone();
24135        let col_names = cte.column_overrides.clone();
24136        if col_names.is_empty() {
24137            return Err(
24138                self.err("SEARCH / CYCLE requires an explicit WITH name(cols) column list".into())
24139            );
24140        }
24141        let search = cte.search.take();
24142        let cycle = cte.cycle.take();
24143        let mut extra_cols: Vec<String> = Vec::new();
24144        let col_ref = |name: &str| {
24145            Expr::Column(ColumnName {
24146                qualifier: Some(cte_name.clone()),
24147                name: name.to_string(),
24148            })
24149        };
24150        // Position of a SEARCH/CYCLE column within the CTE's column list.
24151        let pos_of = |name: &str| -> Result<usize, ParseError> {
24152            col_names
24153                .iter()
24154                .position(|c| c.eq_ignore_ascii_case(name))
24155                .ok_or_else(|| {
24156                    self.err(format!("SEARCH/CYCLE column {name:?} is not a CTE column"))
24157                })
24158        };
24159        let row_of = |items: &[SelectItem], positions: &[usize]| -> Result<Expr, ParseError> {
24160            let mut args = Vec::with_capacity(positions.len());
24161            for &p in positions {
24162                match items.get(p) {
24163                    Some(SelectItem::Expr { expr, .. }) => args.push(expr.clone()),
24164                    _ => {
24165                        return Err(self.err(
24166                            "SEARCH/CYCLE column maps to a non-expression select item".into(),
24167                        ));
24168                    }
24169                }
24170            }
24171            Ok(Expr::FunctionCall {
24172                name: "row".into(),
24173                args,
24174            })
24175        };
24176        let CteBody::Select(body) = &mut cte.body else {
24177            return Err(self.err("SEARCH / CYCLE requires a SELECT CTE body".into()));
24178        };
24179        if body.unions.is_empty() {
24180            return Err(self.err("SEARCH / CYCLE requires a recursive (UNION) CTE".into()));
24181        }
24182        let rec = body.unions.len() - 1; // recursive term = last UNION peer
24183
24184        if let Some(srch) = search {
24185            // v7.38 (T31) — SEARCH's SET column is ORDER BY'd, and PG's key is a
24186            // `record[]` (DEPTH) or `(depth, keys…)` record (BREADTH). SPG has
24187            // no typed `record[]`, but element-wise array ORDER BY is correct
24188            // (`[1,2] < [1,10] < [2]`), so a SINGLE scalar BY column maps
24189            // exactly onto a typed array: DEPTH is the root→node path
24190            // `array_append(parent, key)`, BREADTH is `[depth, key]`. This
24191            // orders numerically (multi-digit keys included), matching PG.
24192            //
24193            // A multi-column BY would need a record[] to keep the per-node key
24194            // tuple orderable, which SPG can't express — error honestly there
24195            // rather than mis-order.
24196            if srch.by_columns.len() != 1 {
24197                return Err(self.err(
24198                    "SEARCH … BY with multiple columns needs typed record[] ordering \
24199                     SPG doesn't have yet; a single BY column is supported"
24200                        .into(),
24201                ));
24202            }
24203            let key_pos = pos_of(&srch.by_columns[0])?;
24204            let base_key = match body.items.get(key_pos) {
24205                Some(SelectItem::Expr { expr, .. }) => expr.clone(),
24206                _ => {
24207                    return Err(
24208                        self.err("SEARCH BY column maps to a non-expression select item".into())
24209                    );
24210                }
24211            };
24212            let rec_key = match body.unions[rec].1.items.get(key_pos) {
24213                Some(SelectItem::Expr { expr, .. }) => expr.clone(),
24214                _ => {
24215                    return Err(
24216                        self.err("SEARCH BY column maps to a non-expression select item".into())
24217                    );
24218                }
24219            };
24220            if srch.depth_first {
24221                // base: ARRAY[key]; rec: array_append(cte.set, key).
24222                body.items.push(SelectItem::Expr {
24223                    expr: Expr::Array(alloc::vec![base_key]),
24224                    alias: Some(srch.set_column.clone()),
24225                });
24226                body.unions[rec].1.items.push(SelectItem::Expr {
24227                    expr: Expr::FunctionCall {
24228                        name: "array_append".into(),
24229                        args: alloc::vec![col_ref(&srch.set_column), rec_key],
24230                    },
24231                    alias: Some(srch.set_column.clone()),
24232                });
24233            } else {
24234                // BREADTH: [depth, key]; depth starts at 0 and increments. The
24235                // leading depth element dominates the element-wise comparison,
24236                // so shallower rows sort first, then by key — PG's (depth, key).
24237                body.items.push(SelectItem::Expr {
24238                    expr: Expr::Array(alloc::vec![Expr::Literal(Literal::Integer(0)), base_key,]),
24239                    alias: Some(srch.set_column.clone()),
24240                });
24241                // rec depth = cte.set[1] + 1.
24242                let parent_depth = Expr::ArraySubscript {
24243                    target: Box::new(col_ref(&srch.set_column)),
24244                    index: Box::new(Expr::Literal(Literal::Integer(1))),
24245                };
24246                body.unions[rec].1.items.push(SelectItem::Expr {
24247                    expr: Expr::Array(alloc::vec![
24248                        Expr::Binary {
24249                            lhs: Box::new(parent_depth),
24250                            op: BinOp::Add,
24251                            rhs: Box::new(Expr::Literal(Literal::Integer(1))),
24252                        },
24253                        rec_key,
24254                    ]),
24255                    alias: Some(srch.set_column.clone()),
24256                });
24257            }
24258            extra_cols.push(srch.set_column);
24259        }
24260
24261        if let Some(cyc) = cycle {
24262            let positions: Vec<usize> = cyc
24263                .columns
24264                .iter()
24265                .map(|c| pos_of(c))
24266                .collect::<Result<_, _>>()?;
24267            // v7.38 (read01, T9) — ROW(cols) is now a first-class composite, so
24268            // cast it to text for the cycle path: membership only needs equality,
24269            // and the record text form gives SPG a TextArray path (SPG has no
24270            // typed record[] array). Cycle detection is unaffected.
24271            let base_row = Expr::Cast {
24272                expr: Box::new(row_of(&body.items, &positions)?),
24273                target: CastTarget::Text,
24274            };
24275            let rec_row = Expr::Cast {
24276                expr: Box::new(row_of(&body.unions[rec].1.items, &positions)?),
24277                target: CastTarget::Text,
24278            };
24279            let mark = cyc.mark_value.clone().unwrap_or(Literal::Bool(true));
24280            let dflt = cyc.default_value.clone().unwrap_or(Literal::Bool(false));
24281            // base: <default> AS mark, ARRAY[ROW(cols)] AS path.
24282            body.items.push(SelectItem::Expr {
24283                expr: Expr::Literal(dflt.clone()),
24284                alias: Some(cyc.mark_column.clone()),
24285            });
24286            body.items.push(SelectItem::Expr {
24287                expr: Expr::Array(alloc::vec![base_row]),
24288                alias: Some(cyc.path_column.clone()),
24289            });
24290            // rec mark: ROW(cols) already in the path → cycle.
24291            let hit = Expr::AnyAll {
24292                expr: Box::new(rec_row.clone()),
24293                op: BinOp::Eq,
24294                array: Box::new(col_ref(&cyc.path_column)),
24295                is_any: true,
24296            };
24297            let mark_expr = if cyc.mark_value.is_some() || cyc.default_value.is_some() {
24298                Expr::Case {
24299                    operand: None,
24300                    branches: alloc::vec![(hit, Expr::Literal(mark))],
24301                    else_branch: Some(Box::new(Expr::Literal(dflt))),
24302                }
24303            } else {
24304                hit
24305            };
24306            body.unions[rec].1.items.push(SelectItem::Expr {
24307                expr: mark_expr,
24308                alias: Some(cyc.mark_column.clone()),
24309            });
24310            // rec path: array_append(cte.path, ROW(cols)).
24311            body.unions[rec].1.items.push(SelectItem::Expr {
24312                expr: Expr::FunctionCall {
24313                    name: "array_append".into(),
24314                    args: alloc::vec![col_ref(&cyc.path_column), rec_row],
24315                },
24316                alias: Some(cyc.path_column.clone()),
24317            });
24318            // rec WHERE: AND NOT cte.mark — stop expanding a cycled row.
24319            let stop = Expr::Unary {
24320                op: UnOp::Not,
24321                expr: Box::new(col_ref(&cyc.mark_column)),
24322            };
24323            let w = &mut body.unions[rec].1.where_;
24324            *w = Some(match w.take() {
24325                Some(prev) => Expr::Binary {
24326                    lhs: Box::new(prev),
24327                    op: BinOp::And,
24328                    rhs: Box::new(stop),
24329                },
24330                None => stop,
24331            });
24332            extra_cols.push(cyc.mark_column);
24333            extra_cols.push(cyc.path_column);
24334        }
24335        cte.column_overrides.extend(extra_cols);
24336        Ok(())
24337    }
24338
24339    /// v7.38 (read01 U16) — `SEARCH { DEPTH | BREADTH } FIRST BY col [,
24340    /// col…] SET seqcol`. Returns None when the next token isn't SEARCH.
24341    fn parse_cte_search_clause(&mut self) -> Result<Option<crate::ast::SearchClause>, ParseError> {
24342        if !matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("search")) {
24343            return Ok(None);
24344        }
24345        self.advance(); // SEARCH
24346        let depth_first = match self.peek() {
24347            Token::Ident(s) if s.eq_ignore_ascii_case("depth") => true,
24348            Token::Ident(s) if s.eq_ignore_ascii_case("breadth") => false,
24349            other => {
24350                return Err(self.err(format!(
24351                    "expected DEPTH or BREADTH after SEARCH, got {other:?}"
24352                )));
24353            }
24354        };
24355        self.advance();
24356        if !matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("first")) {
24357            return Err(self.err(format!(
24358                "expected FIRST after SEARCH mode, got {:?}",
24359                self.peek()
24360            )));
24361        }
24362        self.advance();
24363        if !self.peek_is_by() {
24364            return Err(self.err(format!(
24365                "expected BY after SEARCH … FIRST, got {:?}",
24366                self.peek()
24367            )));
24368        }
24369        self.advance();
24370        let mut by_columns = alloc::vec![self.expect_ident_like()?];
24371        while matches!(self.peek(), Token::Comma) {
24372            self.advance();
24373            by_columns.push(self.expect_ident_like()?);
24374        }
24375        if !matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("set")) {
24376            return Err(self.err(format!(
24377                "expected SET in SEARCH clause, got {:?}",
24378                self.peek()
24379            )));
24380        }
24381        self.advance();
24382        let set_column = self.expect_ident_like()?;
24383        Ok(Some(crate::ast::SearchClause {
24384            depth_first,
24385            by_columns,
24386            set_column,
24387        }))
24388    }
24389
24390    /// v7.38 (read01 U16) — `CYCLE col [, col…] SET markcol [TO v DEFAULT w]
24391    /// USING pathcol`. Returns None when the next token isn't CYCLE.
24392    fn parse_cte_cycle_clause(&mut self) -> Result<Option<crate::ast::CycleClause>, ParseError> {
24393        if !matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("cycle")) {
24394            return Ok(None);
24395        }
24396        self.advance(); // CYCLE
24397        let mut columns = alloc::vec![self.expect_ident_like()?];
24398        while matches!(self.peek(), Token::Comma) {
24399            self.advance();
24400            columns.push(self.expect_ident_like()?);
24401        }
24402        if !matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("set")) {
24403            return Err(self.err(format!(
24404                "expected SET in CYCLE clause, got {:?}",
24405                self.peek()
24406            )));
24407        }
24408        self.advance();
24409        let mark_column = self.expect_ident_like()?;
24410        let mut mark_value = None;
24411        let mut default_value = None;
24412        if matches!(self.peek(), Token::To) {
24413            self.advance();
24414            mark_value = Some(self.parse_cycle_literal()?);
24415            if !matches!(self.peek(), Token::Default) {
24416                return Err(self.err(format!(
24417                    "expected DEFAULT after CYCLE … TO, got {:?}",
24418                    self.peek()
24419                )));
24420            }
24421            self.advance();
24422            default_value = Some(self.parse_cycle_literal()?);
24423        }
24424        if !matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("using")) {
24425            return Err(self.err(format!(
24426                "expected USING in CYCLE clause, got {:?}",
24427                self.peek()
24428            )));
24429        }
24430        self.advance();
24431        let path_column = self.expect_ident_like()?;
24432        Ok(Some(crate::ast::CycleClause {
24433            columns,
24434            mark_column,
24435            mark_value,
24436            default_value,
24437            path_column,
24438        }))
24439    }
24440
24441    /// The mark / default value in a CYCLE `TO v DEFAULT w` — a bare
24442    /// literal (string / bool / number) in PG.
24443    fn parse_cycle_literal(&mut self) -> Result<crate::ast::Literal, ParseError> {
24444        match self.parse_expr(0)? {
24445            Expr::Literal(l) => Ok(l),
24446            other => Err(self.err(format!(
24447                "CYCLE mark/default value must be a literal, got {other:?}"
24448            ))),
24449        }
24450    }
24451
24452    fn parse_with_cte_then_select(&mut self) -> Result<Statement, ParseError> {
24453        // v4.22: WITH RECURSIVE — optional keyword right after WITH.
24454        // Comes through as an identifier; consume it if present and
24455        // mark every CTE in the clause as recursive (PG semantics —
24456        // the flag is per-WITH, not per-CTE).
24457        let mut recursive = false;
24458        if let Token::Ident(s) | Token::QuotedIdent(s) = self.peek()
24459            && s.eq_ignore_ascii_case("recursive")
24460        {
24461            self.advance();
24462            recursive = true;
24463        }
24464        let mut ctes = Vec::new();
24465        loop {
24466            let name = self.expect_ident_like()?;
24467            // v4.22: optional column-name list — `WITH t(a,b,c) AS ...`.
24468            // PG uses these to rename the body's output columns; we
24469            // do the same below by overriding `columns[i].name`.
24470            let column_overrides: Vec<String> = if matches!(self.peek(), Token::LParen) {
24471                self.advance();
24472                let mut names = Vec::new();
24473                loop {
24474                    names.push(self.expect_ident_like()?);
24475                    if matches!(self.peek(), Token::Comma) {
24476                        self.advance();
24477                        continue;
24478                    }
24479                    break;
24480                }
24481                if !matches!(self.peek(), Token::RParen) {
24482                    return Err(self.err(format!(
24483                        "expected ')' to close CTE column list, got {:?}",
24484                        self.peek()
24485                    )));
24486                }
24487                self.advance();
24488                names
24489            } else {
24490                Vec::new()
24491            };
24492            // AS is a reserved Token::As (used by SELECT-item / FROM
24493            // aliasing) — handle it specially rather than as a bare
24494            // ident.
24495            if !matches!(self.peek(), Token::As) {
24496                return Err(self.err(format!(
24497                    "expected AS after CTE name {name:?}, got {:?}",
24498                    self.peek()
24499                )));
24500            }
24501            self.advance();
24502            // v7.37.17 (17.6 siblings) — PG 12+ `AS [NOT]
24503            // MATERIALIZED` optimizer hints. SPG materialises every
24504            // CTE, so both spellings are accepted and absorbed.
24505            if matches!(self.peek(), Token::Not) {
24506                self.advance(); // NOT
24507                if matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s)
24508                    if s.eq_ignore_ascii_case("materialized"))
24509                {
24510                    self.advance();
24511                } else {
24512                    return Err(self.err(format!(
24513                        "expected MATERIALIZED after AS NOT, got {:?}",
24514                        self.peek()
24515                    )));
24516                }
24517            } else if matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s)
24518                if s.eq_ignore_ascii_case("materialized"))
24519            {
24520                self.advance();
24521            }
24522            if !matches!(self.peek(), Token::LParen) {
24523                return Err(self.err(format!(
24524                    "expected '(' after AS in WITH clause, got {:?}",
24525                    self.peek()
24526                )));
24527            }
24528            self.advance();
24529            // v7.37.43-T4.4 — accept INSERT / UPDATE / DELETE (with
24530            // RETURNING) as the CTE body in addition to SELECT.
24531            // PG writable CTE semantics. UPDATE / DELETE come in as
24532            // bare Idents (lexer keeps SELECT / INSERT as reserved
24533            // tokens but treats the rest of DML as case-insensitive
24534            // idents).
24535            let is_update_kw = matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("update"));
24536            let is_delete_kw = matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("delete"));
24537            let is_merge_kw = matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("merge"));
24538            let body = match self.peek() {
24539                Token::Select => {
24540                    let inner = self.parse_select_stmt()?;
24541                    let Statement::Select(s) = inner else {
24542                        unreachable!("parse_select_stmt returns Select");
24543                    };
24544                    crate::ast::CteBody::Select(s)
24545                }
24546                // v7.39 (round 869) — `WITH x AS (TABLE t)`. PG spells
24547                // `SELECT * FROM t` this way and accepts it wherever a
24548                // SELECT goes, so the CTE body dispatch needs its own
24549                // arm: this match is keyed on the FIRST token, and
24550                // `Token::Table` fell through to a tail that then
24551                // rejected what it got. `parse_table_shorthand` has
24552                // returned a desugared SelectStatement since the
24553                // shorthand landed — only the routing was missing.
24554                // Round 868 found this by putting the shorthand in a
24555                // subquery; every earlier check used a top-level form.
24556                // v7.39 (round 869) — `WITH x AS (TABLE t)`. PG spells
24557                // `SELECT * FROM t` this way and accepts it wherever a
24558                // SELECT goes, so the CTE body dispatch needs its own
24559                // arm: this match is keyed on the FIRST token, and
24560                // `Token::Table` fell through to a tail that rejected
24561                // what it got. `parse_table_shorthand` has returned a
24562                // desugared SelectStatement since the shorthand landed —
24563                // only the routing was missing, here and in the derived
24564                // table's second-token gate. Round 868 found both by
24565                // putting the shorthand in a subquery; every earlier
24566                // check had used a top-level form.
24567                Token::Table
24568                    if matches!(
24569                        self.tokens.get(self.pos + 1),
24570                        Some(Token::Ident(_) | Token::QuotedIdent(_))
24571                    ) =>
24572                {
24573                    let mut head = self.parse_table_shorthand()?;
24574                    self.parse_setop_chain_into(&mut head)?;
24575                    self.parse_select_tail_into(&mut head)?;
24576                    crate::ast::CteBody::Select(head)
24577                }
24578                // v7.37.17 (17.6 siblings) — VALUES as a CTE body:
24579                // WITH t(a) AS (VALUES (1), (2)) … lowers through
24580                // the shared rows helper onto a Select body.
24581                Token::Values => {
24582                    self.advance(); // VALUES
24583                    let mut head = self.parse_values_rows_body()?;
24584                    // A VALUES seed can head a set-operation chain —
24585                    // WITH RECURSIVE t(n) AS (VALUES(1) UNION ALL
24586                    // SELECT n+1 FROM t …). Attach any trailing
24587                    // UNION / INTERSECT / EXCEPT peers so the
24588                    // recursive-CTE body parses like the SELECT seed.
24589                    self.parse_setop_chain_into(&mut head)?;
24590                    crate::ast::CteBody::Select(head)
24591                }
24592                Token::Insert => {
24593                    let inner = self.parse_one_statement()?;
24594                    let Statement::Insert(s) = inner else {
24595                        unreachable!("Token::Insert routes to Insert");
24596                    };
24597                    crate::ast::CteBody::Insert(alloc::boxed::Box::new(s))
24598                }
24599                _ if is_update_kw => {
24600                    let inner = self.parse_one_statement()?;
24601                    let Statement::Update(s) = inner else {
24602                        return Err(
24603                            self.err(format!("expected UPDATE inside WITH (…), got {inner:?}"))
24604                        );
24605                    };
24606                    crate::ast::CteBody::Update(alloc::boxed::Box::new(s))
24607                }
24608                _ if is_delete_kw => {
24609                    let inner = self.parse_one_statement()?;
24610                    let Statement::Delete(s) = inner else {
24611                        return Err(
24612                            self.err(format!("expected DELETE inside WITH (…), got {inner:?}"))
24613                        );
24614                    };
24615                    crate::ast::CteBody::Delete(alloc::boxed::Box::new(s))
24616                }
24617                // v7.39 (round 149) — PG 17 allows MERGE as a
24618                // data-modifying CTE body.
24619                _ if is_merge_kw => {
24620                    let inner = self.parse_one_statement()?;
24621                    let Statement::Merge(s) = inner else {
24622                        return Err(
24623                            self.err(format!("expected MERGE inside WITH (…), got {inner:?}"))
24624                        );
24625                    };
24626                    crate::ast::CteBody::Merge(alloc::boxed::Box::new(s))
24627                }
24628                // v7.39 (round 151) — a CTE body may itself be
24629                // WITH-headed (PG grammar: PreparableStmt carries its
24630                // own with_clause). The nested statement keeps its own
24631                // ctes; the modifying-CTE-at-top-level rule is enforced
24632                // at execution.
24633                Token::Ident(s) if s.eq_ignore_ascii_case("with") => {
24634                    self.advance(); // WITH
24635                    match self.parse_with_cte_then_select()? {
24636                        Statement::Select(s) => crate::ast::CteBody::Select(s),
24637                        Statement::Insert(s) => {
24638                            crate::ast::CteBody::Insert(alloc::boxed::Box::new(s))
24639                        }
24640                        Statement::Update(s) => {
24641                            crate::ast::CteBody::Update(alloc::boxed::Box::new(s))
24642                        }
24643                        Statement::Delete(s) => {
24644                            crate::ast::CteBody::Delete(alloc::boxed::Box::new(s))
24645                        }
24646                        Statement::Merge(s) => {
24647                            crate::ast::CteBody::Merge(alloc::boxed::Box::new(s))
24648                        }
24649
24650                        other => {
24651                            return Err(self.err(format!(
24652                                "WITH body must be SELECT / INSERT / UPDATE / DELETE / MERGE, got {other:?}"
24653                            )));
24654                        }
24655                    }
24656                }
24657                other => {
24658                    return Err(self.err(format!(
24659                        "WITH body must be SELECT / INSERT / UPDATE / DELETE / MERGE, got {other:?}"
24660                    )));
24661                }
24662            };
24663            if !matches!(self.peek(), Token::RParen) {
24664                return Err(self.err(format!(
24665                    "expected ')' after CTE body, got {:?}",
24666                    self.peek()
24667                )));
24668            }
24669            self.advance();
24670            // v7.38 (read01 U16) — optional SEARCH / CYCLE on a recursive
24671            // CTE, desugared into extra body columns by the engine.
24672            let search = self.parse_cte_search_clause()?;
24673            let cycle = self.parse_cte_cycle_clause()?;
24674            let mut cte = crate::ast::Cte {
24675                name,
24676                body,
24677                recursive,
24678                column_overrides,
24679                search,
24680                cycle,
24681            };
24682            self.validate_recursive_cte(&cte)?;
24683            self.desugar_cte_search_cycle(&mut cte)?;
24684            ctes.push(cte);
24685            if matches!(self.peek(), Token::Comma) {
24686                self.advance();
24687                continue;
24688            }
24689            break;
24690        }
24691        // v7.37.43-T4.4 — the outer body may be SELECT (classical),
24692        // or INSERT / UPDATE / DELETE (writable CTE outer). Attach
24693        // the parsed CTEs to whichever statement the body produces.
24694        let outer_is_update = matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("update"));
24695        let outer_is_delete = matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("delete"));
24696        let outer_is_merge = matches!(self.peek(), Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("merge"));
24697        match self.peek() {
24698            Token::Select => {
24699                let body_stmt = self.parse_select_stmt()?;
24700                let Statement::Select(mut body) = body_stmt else {
24701                    unreachable!()
24702                };
24703                body.ctes = ctes;
24704                Ok(Statement::Select(body))
24705            }
24706            Token::Insert => {
24707                let body_stmt = self.parse_one_statement()?;
24708                let Statement::Insert(mut body) = body_stmt else {
24709                    unreachable!()
24710                };
24711                body.ctes = ctes;
24712                Ok(Statement::Insert(body))
24713            }
24714            _ if outer_is_update => {
24715                let body_stmt = self.parse_one_statement()?;
24716                let Statement::Update(mut body) = body_stmt else {
24717                    return Err(self.err(format!("expected UPDATE after WITH clause")));
24718                };
24719                body.ctes = ctes;
24720                Ok(Statement::Update(body))
24721            }
24722            _ if outer_is_delete => {
24723                let body_stmt = self.parse_one_statement()?;
24724                let Statement::Delete(mut body) = body_stmt else {
24725                    return Err(self.err(format!("expected DELETE after WITH clause")));
24726                };
24727                body.ctes = ctes;
24728                Ok(Statement::Delete(body))
24729            }
24730            // v7.39 (round 149) — PG 15 allows a WITH clause on MERGE;
24731            // WITH RECURSIVE is rejected with PG's exact message
24732            // (parse analysis, transformWithClause).
24733            _ if outer_is_merge => {
24734                if recursive {
24735                    return Err(self.err(String::from(
24736                        "WITH RECURSIVE is not supported for MERGE statement",
24737                    )));
24738                }
24739                let body_stmt = self.parse_one_statement()?;
24740                let Statement::Merge(mut body) = body_stmt else {
24741                    return Err(self.err(format!("expected MERGE after WITH clause")));
24742                };
24743                body.ctes = ctes;
24744                Ok(Statement::Merge(body))
24745            }
24746            other => Err(self.err(format!(
24747                "expected SELECT / INSERT / UPDATE / DELETE / MERGE after WITH clause, got {other:?}"
24748            ))),
24749        }
24750    }
24751
24752    /// v4.10: parse `EXISTS (SELECT ...)`. Caller (`parse_atom`)
24753    /// already consumed the leading `EXISTS` ident via
24754    /// `self.advance()`.
24755    /// v7.13.0 — parse the rest of a `CASE … END` expression after
24756    /// the leading `CASE` ident has been consumed (mailrs round-5
24757    /// G9). Supports both the searched form
24758    /// (`CASE WHEN cond THEN val …`) and the simple form
24759    /// (`CASE operand WHEN val THEN val …`).
24760    fn parse_case_atom(&mut self) -> Result<Expr, ParseError> {
24761        // Disambiguate searched vs simple form: if the next token
24762        // is `WHEN`, we're in the searched form. Otherwise the
24763        // intervening expression is the operand.
24764        let operand = if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("when")) {
24765            None
24766        } else {
24767            Some(Box::new(self.parse_expr(0)?))
24768        };
24769        let mut branches: Vec<(Expr, Expr)> = Vec::new();
24770        loop {
24771            match self.peek() {
24772                Token::Ident(s) if s.eq_ignore_ascii_case("when") => {
24773                    self.advance();
24774                    let cond = self.parse_expr(0)?;
24775                    match self.peek() {
24776                        Token::Ident(t) if t.eq_ignore_ascii_case("then") => {
24777                            self.advance();
24778                        }
24779                        other => {
24780                            return Err(self.err(alloc::format!(
24781                                "expected THEN after CASE WHEN <expr>, got {other:?}"
24782                            )));
24783                        }
24784                    }
24785                    let value = self.parse_expr(0)?;
24786                    branches.push((cond, value));
24787                }
24788                _ => break,
24789            }
24790        }
24791        if branches.is_empty() {
24792            return Err(self.err("CASE requires at least one WHEN … THEN … branch".into()));
24793        }
24794        let else_branch = if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("else"))
24795        {
24796            self.advance();
24797            Some(Box::new(self.parse_expr(0)?))
24798        } else {
24799            None
24800        };
24801        match self.peek() {
24802            Token::Ident(s) if s.eq_ignore_ascii_case("end") => {
24803                self.advance();
24804            }
24805            other => {
24806                return Err(self.err(alloc::format!(
24807                    "expected END to close CASE expression, got {other:?}"
24808                )));
24809            }
24810        }
24811        Ok(Expr::Case {
24812            operand,
24813            branches,
24814            else_branch,
24815        })
24816    }
24817
24818    /// v7.39 (round 151) — nested `WITH … SELECT …` in a subquery /
24819    /// query-source position (EXISTS / IN / INSERT source / CTE body /
24820    /// view body). Caller consumed the WITH keyword. Only a SELECT
24821    /// outer is grammatical here; the data-modifying-CTE-at-top-level
24822    /// rule (PG 0A000) is enforced at execution, where the SQLSTATE
24823    /// maps correctly.
24824    fn parse_nested_with_select(&mut self) -> Result<crate::ast::SelectStatement, ParseError> {
24825        let inner = self.parse_with_cte_then_select()?;
24826        match inner {
24827            Statement::Select(s) => Ok(s),
24828            other => Err(self.err(format!(
24829                "expected SELECT after WITH in a subquery, got {other:?}"
24830            ))),
24831        }
24832    }
24833
24834    /// True when the next token is the (unquoted) WITH keyword. WITH is
24835    /// reserved in PG, so a bare `with` can never be a column reference
24836    /// in these positions; a quoted `"with"` stays an identifier.
24837    fn peek_is_with_kw(&self) -> bool {
24838        matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("with"))
24839    }
24840
24841    /// v7.39 (round 153) — the `ANY / ALL ( [WITH …] SELECT … )` body.
24842    /// `#[inline(never)]` keeps the large SelectStatement temporaries
24843    /// off parse_expr's recursive frame (the nesting-budget stack
24844    /// cliff — see the round-153 gate regression).
24845    #[inline(never)]
24846    fn parse_any_all_select_body(&mut self) -> Result<crate::ast::SelectStatement, ParseError> {
24847        if self.peek_is_with_kw() {
24848            self.advance();
24849            self.parse_nested_with_select()
24850        } else {
24851            match self.parse_select_stmt()? {
24852                Statement::Select(s) => Ok(s),
24853                other => Err(self.err(alloc::format!(
24854                    "expected SELECT inside ANY/ALL, got {other:?}"
24855                ))),
24856            }
24857        }
24858    }
24859
24860    fn parse_exists_atom(&mut self, negated: bool) -> Result<Expr, ParseError> {
24861        if !matches!(self.peek(), Token::LParen) {
24862            return Err(self.err(format!("expected '(' after EXISTS, got {:?}", self.peek())));
24863        }
24864        self.advance();
24865        // v7.39 (round 151) — `EXISTS (WITH … SELECT …)` is legal PG.
24866        let s = if self.peek_is_with_kw() {
24867            self.advance();
24868            self.parse_nested_with_select()?
24869        } else {
24870            let inner = self.parse_select_stmt()?;
24871            let Statement::Select(s) = inner else {
24872                unreachable!("parse_select_stmt returns Select")
24873            };
24874            s
24875        };
24876        if !matches!(self.peek(), Token::RParen) {
24877            return Err(self.err(format!(
24878                "expected ')' after EXISTS-subquery, got {:?}",
24879                self.peek()
24880            )));
24881        }
24882        self.advance();
24883        Ok(Expr::Exists {
24884            subquery: Box::new(s),
24885            negated,
24886        })
24887    }
24888
24889    fn parse_in_tail(&mut self, expr: Expr, negated: bool) -> Result<Expr, ParseError> {
24890        self.advance(); // IN
24891        if !matches!(self.peek(), Token::LParen) {
24892            return Err(self.err(format!("expected '(' after IN, got {:?}", self.peek())));
24893        }
24894        self.advance();
24895        // v4.10: `IN (SELECT ...)` — subquery branch. v7.39 (round 151)
24896        // also accepts a WITH-headed subquery (`IN (WITH … SELECT …)`).
24897        if matches!(self.peek(), Token::Select) || self.peek_is_with_kw() {
24898            let s = if self.peek_is_with_kw() {
24899                self.advance();
24900                self.parse_nested_with_select()?
24901            } else {
24902                let inner = self.parse_select_stmt()?;
24903                let Statement::Select(s) = inner else {
24904                    unreachable!("parse_select_stmt always returns Statement::Select")
24905                };
24906                s
24907            };
24908            if !matches!(self.peek(), Token::RParen) {
24909                return Err(self.err(format!(
24910                    "expected ')' after IN-subquery, got {:?}",
24911                    self.peek()
24912                )));
24913            }
24914            self.advance();
24915            return Ok(Expr::InSubquery {
24916                expr: Box::new(expr),
24917                subquery: Box::new(s),
24918                negated,
24919            });
24920        }
24921        let mut elements = Vec::new();
24922        if !matches!(self.peek(), Token::RParen) {
24923            loop {
24924                elements.push(self.parse_expr(0)?);
24925                match self.peek() {
24926                    Token::Comma => {
24927                        self.advance();
24928                    }
24929                    Token::RParen => break,
24930                    other => {
24931                        return Err(
24932                            self.err(format!("expected ',' or ')' in IN list, got {other:?}"))
24933                        );
24934                    }
24935                }
24936            }
24937        }
24938        self.advance(); // ')'
24939        // v7.30.2 (mailrs round-25) — flat InList node instead of a
24940        // left-deep OR-Eq chain: chain depth scaled with the element
24941        // count and overflowed the stack (eval + drop are recursive).
24942        if elements.is_empty() {
24943            return Ok(maybe_not(Expr::Literal(Literal::Bool(false)), negated));
24944        }
24945        Ok(Expr::InList {
24946            expr: Box::new(expr),
24947            list: elements,
24948            negated,
24949        })
24950    }
24951
24952    /// Parse a pgvector array literal `[ x1, x2, ... ]`. The opening `[` is
24953    /// already consumed by the caller. Elements must be numeric literals
24954    /// (with optional unary `-`); any compound expression is rejected at
24955    /// parse time so the runtime never needs to evaluate inside a vector.
24956    /// `EXTRACT(<field> FROM <source>)`. The dispatching `parse_atom`
24957    /// has already consumed the `EXTRACT` token before calling us —
24958    /// we pick up at the opening `(`.
24959    /// v7.17.0 Phase 2.2 — MySQL `MATCH(col [, col ...]) AGAINST
24960    /// (expr [IN BOOLEAN MODE | IN NATURAL LANGUAGE MODE
24961    /// [WITH QUERY EXPANSION]])`. Rewritten in-place to a
24962    /// per-column OR-fold of
24963    /// `to_tsvector('simple', col) @@ plainto_tsquery('simple',
24964    /// term)` so the existing FTS evaluator handles semantics.
24965    ///
24966    /// The mode modifier is accepted-and-ignored at v7.17 — all
24967    /// modes map to the same `plainto_tsquery` rewrite. Boolean-
24968    /// mode operators (`+foo -bar`) would need their own parser
24969    /// (Phase 2.2c); customers who hit them today already get a
24970    /// correct lexeme-match against the bare term, only without
24971    /// the +/- precedence the customer asked for.
24972    fn parse_match_against_atom(&mut self) -> Result<Expr, ParseError> {
24973        // Already at `MATCH`-consumed position; the dispatcher
24974        // confirmed the next token is `(`.
24975        if !matches!(self.peek(), Token::LParen) {
24976            return Err(self.err(alloc::format!(
24977                "expected '(' after MATCH, got {:?}",
24978                self.peek()
24979            )));
24980        }
24981        self.advance();
24982        let mut cols: Vec<Expr> = Vec::new();
24983        loop {
24984            cols.push(self.parse_expr(0)?);
24985            match self.peek() {
24986                Token::Comma => {
24987                    self.advance();
24988                }
24989                Token::RParen => break,
24990                other => {
24991                    return Err(self.err(alloc::format!(
24992                        "expected ',' or ')' in MATCH column list, got {other:?}"
24993                    )));
24994                }
24995            }
24996        }
24997        self.advance(); // ')'
24998        // Expect AGAINST.
24999        match self.peek() {
25000            Token::Ident(s) | Token::QuotedIdent(s) if s.eq_ignore_ascii_case("against") => {
25001                self.advance();
25002            }
25003            other => {
25004                return Err(self.err(alloc::format!(
25005                    "expected AGAINST after MATCH column list, got {other:?}"
25006                )));
25007            }
25008        }
25009        if !matches!(self.peek(), Token::LParen) {
25010            return Err(self.err(alloc::format!(
25011                "expected '(' after AGAINST, got {:?}",
25012                self.peek()
25013            )));
25014        }
25015        self.advance();
25016        // Read AGAINST's argument as a single primary token —
25017        // string literal, placeholder, or column-ref ident. We
25018        // can't call `parse_expr` / `parse_unary` here because
25019        // the postfix chain inside `parse_atom` would greedily
25020        // fold a trailing `IN BOOLEAN MODE` as `expr IN (...)`
25021        // and fail at "expected '(' after IN". Customers always
25022        // write a literal or bound parameter in AGAINST, so this
25023        // restriction is non-blocking; the error path explains
25024        // the limit if a more complex expression shows up.
25025        let term = match self.advance() {
25026            Token::String(s) => Expr::Literal(crate::ast::Literal::String(s)),
25027            Token::Placeholder(n) => Expr::Placeholder(n),
25028            Token::Ident(s) | Token::QuotedIdent(s) => Expr::Column(crate::ast::ColumnName {
25029                qualifier: None,
25030                name: s,
25031            }),
25032            other => {
25033                return Err(self.err(alloc::format!(
25034                    "MATCH ... AGAINST(<term>) expects a string literal, \
25035                     bound parameter, or column ref, got {other:?}"
25036                )));
25037            }
25038        };
25039        // Optional mode tail — accept-and-ignore at v7.17:
25040        //   IN NATURAL LANGUAGE MODE [WITH QUERY EXPANSION]
25041        //   IN BOOLEAN MODE
25042        //   WITH QUERY EXPANSION
25043        loop {
25044            match self.peek() {
25045                // IN lexes as a reserved Token::In, not an ident,
25046                // so it gets its own arm.
25047                Token::In => {
25048                    self.advance();
25049                }
25050                Token::Ident(s) | Token::QuotedIdent(s)
25051                    if s.eq_ignore_ascii_case("natural")
25052                        || s.eq_ignore_ascii_case("language")
25053                        || s.eq_ignore_ascii_case("boolean")
25054                        || s.eq_ignore_ascii_case("mode")
25055                        || s.eq_ignore_ascii_case("with")
25056                        || s.eq_ignore_ascii_case("query")
25057                        || s.eq_ignore_ascii_case("expansion") =>
25058                {
25059                    self.advance();
25060                }
25061                _ => break,
25062            }
25063        }
25064        if !matches!(self.peek(), Token::RParen) {
25065            return Err(self.err(alloc::format!(
25066                "expected ')' to close AGAINST, got {:?}",
25067                self.peek()
25068            )));
25069        }
25070        self.advance();
25071        // Build per-column `to_tsvector('simple', col) @@
25072        // plainto_tsquery('simple', term)` and OR-fold.
25073        let simple_lit = || Expr::Literal(crate::ast::Literal::String(String::from("simple")));
25074        let plainto = Expr::FunctionCall {
25075            name: String::from("plainto_tsquery"),
25076            args: alloc::vec![simple_lit(), term.clone()],
25077        };
25078        let mut folded: Option<Expr> = None;
25079        for col in cols {
25080            let to_tsv = Expr::FunctionCall {
25081                name: String::from("to_tsvector"),
25082                args: alloc::vec![simple_lit(), col],
25083            };
25084            let leaf = Expr::Binary {
25085                lhs: Box::new(to_tsv),
25086                op: crate::ast::BinOp::TsMatch,
25087                rhs: Box::new(plainto.clone()),
25088            };
25089            folded = Some(match folded {
25090                None => leaf,
25091                Some(prev) => Expr::Binary {
25092                    lhs: Box::new(prev),
25093                    op: crate::ast::BinOp::Or,
25094                    rhs: Box::new(leaf),
25095                },
25096            });
25097        }
25098        match folded {
25099            Some(e) => Ok(e),
25100            None => Err(self.err(String::from(
25101                "MATCH(...) AGAINST(...) requires at least one column",
25102            ))),
25103        }
25104    }
25105
25106    fn parse_extract_atom(&mut self) -> Result<Expr, ParseError> {
25107        if !matches!(self.peek(), Token::LParen) {
25108            return Err(self.err(format!("expected '(' after EXTRACT, got {:?}", self.peek())));
25109        }
25110        self.advance();
25111        let field_name = self.expect_ident_like()?;
25112        let field = match field_name.to_ascii_lowercase().as_str() {
25113            // PG accepts the plural spellings (years/months/…/millenniums) as
25114            // aliases for the singular fields — its datetime unit table has both.
25115            // (quarter has no plural; dow/doy/isoyear/epoch/julian likewise.)
25116            "year" | "years" => ExtractField::Year,
25117            "month" | "months" => ExtractField::Month,
25118            "day" | "days" => ExtractField::Day,
25119            "hour" | "hours" => ExtractField::Hour,
25120            "minute" | "minutes" => ExtractField::Minute,
25121            "second" | "seconds" => ExtractField::Second,
25122            "microsecond" | "microseconds" => ExtractField::Microsecond,
25123            "epoch" => ExtractField::Epoch,
25124            "dow" => ExtractField::Dow,
25125            "isodow" => ExtractField::Isodow,
25126            "doy" => ExtractField::Doy,
25127            "week" | "weeks" => ExtractField::Week,
25128            "isoyear" => ExtractField::Isoyear,
25129            "quarter" => ExtractField::Quarter,
25130            "decade" | "decades" => ExtractField::Decade,
25131            "century" | "centuries" => ExtractField::Century,
25132            "millennium" | "millenniums" | "millennia" => ExtractField::Millennium,
25133            "julian" => ExtractField::Julian,
25134            "millisecond" | "milliseconds" => ExtractField::Millisecond,
25135            "timezone" => ExtractField::Timezone,
25136            "timezone_hour" => ExtractField::TimezoneHour,
25137            "timezone_minute" => ExtractField::TimezoneMinute,
25138            // v7.39 (round 253) — PG resolves EXTRACT fields at runtime and
25139            // reports an unknown one with the source type (22023); carry the
25140            // raw name so eval can word it.
25141            other => ExtractField::Other(alloc::string::String::from(other)),
25142        };
25143        if !matches!(self.peek(), Token::From) {
25144            return Err(self.err(format!(
25145                "expected FROM after EXTRACT field, got {:?}",
25146                self.peek()
25147            )));
25148        }
25149        self.advance();
25150        let source = self.parse_expr(0)?;
25151        if !matches!(self.peek(), Token::RParen) {
25152            return Err(self.err(format!(
25153                "expected ')' to close EXTRACT, got {:?}",
25154                self.peek()
25155            )));
25156        }
25157        self.advance();
25158        Ok(Expr::Extract {
25159            field,
25160            source: Box::new(source),
25161        })
25162    }
25163
25164    /// `INTERVAL '<n> <unit> [<n> <unit> ...]'` — the `INTERVAL` keyword
25165    /// is already consumed; we expect a single string literal next and
25166    /// resolve it into `Literal::Interval` at parse time so the engine
25167    /// never has to re-tokenise inside the string.
25168    /// The unquoted count of a MySQL `INTERVAL <n> <UNIT>`, when the
25169    /// tokens ahead really are one. A quoted count (`INTERVAL '2' DAY`)
25170    /// is the SQL-standard form and is left to the path below.
25171    fn peek_unquoted_interval_count(&self) -> Option<(alloc::string::String, usize)> {
25172        // A negative count lexes as `-` then the number (`INTERVAL -1 DAY`).
25173        let (offset, sign) = match self.peek() {
25174            Token::Minus => (1, "-"),
25175            _ => (0, ""),
25176        };
25177        let Some(Token::Integer(n)) = self.tokens.get(self.pos + offset) else {
25178            return None;
25179        };
25180        self.tokens
25181            .get(self.pos + offset + 1)
25182            .filter(|t| mysql_interval_unit(t).is_some())?;
25183        Some((alloc::format!("{sign}{n}"), offset + 1))
25184    }
25185
25186    /// v7.39 (round 422) — is the parenthesised group starting at the CURRENT
25187    /// `(` a single quantity followed by a time unit (`INTERVAL (1+1) DAY`),
25188    /// rather than the argument list of MySQL's `INTERVAL(N, N1, …)` function?
25189    ///
25190    /// Scans `self.tokens` by index and consumes NOTHING. Round 409 decided
25191    /// this by parsing the group and then restoring `self.pos` — which could
25192    /// never have worked, because `advance()` DESTROYS the token it returns
25193    /// (`mem::replace(.., Eof)`); the restore yielded a stream of Eof. It was
25194    /// inert only because both branches errored back then.
25195    fn interval_paren_is_quantity(&self) -> bool {
25196        let mut depth = 0usize;
25197        let mut saw_top_level_comma = false;
25198        let mut i = self.pos;
25199        while let Some(tok) = self.tokens.get(i) {
25200            match tok {
25201                Token::LParen => depth += 1,
25202                Token::RParen => {
25203                    depth = depth.saturating_sub(1);
25204                    if depth == 0 {
25205                        return !saw_top_level_comma
25206                            && mysql_interval_unit(self.tokens.get(i + 1).unwrap_or(&Token::Eof))
25207                                .is_some();
25208                    }
25209                }
25210                // A comma directly inside the outermost parens means the
25211                // argument list of the INTERVAL() function.
25212                Token::Comma if depth == 1 => saw_top_level_comma = true,
25213                Token::Eof => return false,
25214                _ => {}
25215            }
25216            i += 1;
25217        }
25218        false
25219    }
25220
25221    fn parse_interval_atom(&mut self) -> Result<Expr, ParseError> {
25222        // v7.39 (round 409) — MySQL's `INTERVAL(N, N1, N2, …)` function
25223        // (the index of the last Ni ≤ N), distinct from the interval literal.
25224        // `INTERVAL (` is ambiguous with `INTERVAL (expr) UNIT`, so the shape
25225        // is decided by a non-destructive lookahead (round 422) before either
25226        // branch consumes anything. MySQL only.
25227        if self.mysql_dialect
25228            && matches!(self.peek(), Token::LParen)
25229            && !self.interval_paren_is_quantity()
25230        {
25231            self.advance(); // (
25232            let mut args = Vec::new();
25233            if !matches!(self.peek(), Token::RParen) {
25234                loop {
25235                    args.push(self.parse_expr(0)?);
25236                    if matches!(self.peek(), Token::Comma) {
25237                        self.advance();
25238                        continue;
25239                    }
25240                    break;
25241                }
25242            }
25243            if !matches!(self.peek(), Token::RParen) {
25244                return Err(self.err(alloc::format!(
25245                    "expected ')' after INTERVAL() arguments, got {:?}",
25246                    self.peek()
25247                )));
25248            }
25249            self.advance(); // )
25250            return Ok(Expr::FunctionCall {
25251                name: alloc::string::String::from("interval"),
25252                args,
25253            });
25254        }
25255        // v7.39 (round 350, M7) — MySQL's `INTERVAL <n> <UNIT>`, with the
25256        // number UNQUOTED: `DATE_ADD(d, INTERVAL 1 MONTH)`,
25257        // `d + INTERVAL 90 MINUTE`, `INTERVAL -1 DAY`. It is how MySQL
25258        // writes every date arithmetic there is, and it did not parse at
25259        // all. PG rejects the unquoted form outright (`syntax error at or
25260        // near "1"`, measured), so it is taken only in the MySQL dialect —
25261        // PG's own `INTERVAL '1' DAY` is untouched below.
25262        if self.mysql_dialect
25263            && let Some((text, consume)) = self.peek_unquoted_interval_count()
25264        {
25265            for _ in 0..consume {
25266                self.advance(); // the optional `-` and the number
25267            }
25268            let Some(unit) = mysql_interval_unit(self.peek()) else {
25269                return Err(self.err(alloc::format!(
25270                    "expected an interval unit after INTERVAL {text}, got {:?}",
25271                    self.peek()
25272                )));
25273            };
25274            self.advance(); // the unit
25275            let (months, days, micros) = scale_mysql_interval(&text, unit)
25276                .ok_or_else(|| self.err(alloc::format!("cannot read INTERVAL {text} {unit}")))?;
25277            return Ok(Expr::Literal(Literal::Interval {
25278                months,
25279                days,
25280                micros,
25281                // The canonical rendering, so Display round-trips into a
25282                // form both dialects read back.
25283                text: alloc::format!("{text} {unit}"),
25284            }));
25285        }
25286        // v7.39 (round 422) — MySQL's interval QUANTITY may be any expression,
25287        // not just a literal: `DATE_ADD(d, INTERVAL n DAY)`,
25288        // `d + INTERVAL n*2 DAY`, `INTERVAL (1+1) DAY`, `INTERVAL ABS(-5) DAY`.
25289        // Those cannot fold into a compile-time `Literal::Interval`, so they
25290        // lower onto the existing `make_interval(y, mo, w, d, h, mi, s)`
25291        // builtin, which builds the value at run time (and yields NULL for a
25292        // NULL quantity, as MariaDB does). The literal path above still folds
25293        // the constant case — it is cheaper and round-trips through Display.
25294        //
25295        // Guarded off a String operand so PG's own `INTERVAL '1 day'` (and
25296        // MySQL's quoted spelling) keep the qualifier path below.
25297        if self.mysql_dialect && !matches!(self.peek(), Token::String(_)) {
25298            let qty = self.parse_expr(0)?;
25299            let Some(unit) = mysql_interval_unit(self.peek()) else {
25300                return Err(self.err(alloc::format!(
25301                    "expected an interval unit after INTERVAL <expr>, got {:?}",
25302                    self.peek()
25303                )));
25304            };
25305            self.advance(); // the unit
25306            return Ok(make_interval_call(qty, unit));
25307        }
25308        let tok = self.advance();
25309        let Token::String(text) = tok else {
25310            return Err(self.err(format!(
25311                "expected string literal after INTERVAL, got {tok:?}"
25312            )));
25313        };
25314        // v7.39 (read01 round 102) — SQL-standard trailing field qualifier
25315        // `<FIELD> [TO <FIELD>]` (`INTERVAL '2' YEAR`, `INTERVAL '1-6' YEAR TO
25316        // MONTH`, `INTERVAL '1 2:03:04' DAY TO SECOND`). It sets which field a
25317        // bare number means and the leading/trailing precision.
25318        let field1 = interval_field_of(self.peek());
25319        let qualifier = if let Some(f1) = field1 {
25320            self.advance();
25321            let f2 = if matches!(self.peek(), Token::To) {
25322                self.advance();
25323                let Some(f) = interval_field_of(self.peek()) else {
25324                    return Err(self.err(format!(
25325                        "expected an interval field after TO, got {:?}",
25326                        self.peek()
25327                    )));
25328                };
25329                self.advance();
25330                Some(f)
25331            } else {
25332                None
25333            };
25334            Some((f1, f2))
25335        } else {
25336            None
25337        };
25338        let (months, days, micros) = match qualifier {
25339            Some(q) => interpret_qualified_interval(&text, q),
25340            None => parse_interval_text(&text),
25341        }
25342        .ok_or_else(|| ParseError {
25343            message: format!(
25344                "cannot parse INTERVAL {text:?}; \
25345                     expected `<n> <unit> [<n> <unit> ...]` with units \
25346                     microsecond[s], millisecond[s], second[s], minute[s], \
25347                     hour[s], day[s], week[s], month[s], year[s]"
25348            ),
25349            token_pos: self.consumed_pos(),
25350        })?;
25351        Ok(Expr::Literal(Literal::Interval {
25352            months,
25353            days,
25354            micros,
25355            text,
25356        }))
25357    }
25358
25359    /// v7.38 (read01, T10) — parse a bracketed sub-array `[e, e, …]` inside an
25360    /// `ARRAY[...]` constructor, recursing on further nested `[...]` so
25361    /// `ARRAY[[1,2],[3,4]]` (and deeper) becomes nested `Expr::Array` rather
25362    /// than a pgvector literal.
25363    fn parse_array_bracket_body(&mut self) -> Result<Expr, ParseError> {
25364        self.advance(); // consume `[`
25365        let mut items: Vec<Expr> = Vec::new();
25366        if !matches!(self.peek(), Token::RBracket) {
25367            loop {
25368                if matches!(self.peek(), Token::LBracket) {
25369                    items.push(self.parse_array_bracket_body()?);
25370                } else {
25371                    items.push(self.parse_expr(0)?);
25372                }
25373                match self.peek() {
25374                    Token::Comma => {
25375                        self.advance();
25376                    }
25377                    Token::RBracket => break,
25378                    other => {
25379                        return Err(self.err(alloc::format!(
25380                            "expected ',' or ']' in array literal, got {other:?}"
25381                        )));
25382                    }
25383                }
25384            }
25385        }
25386        self.advance(); // consume `]`
25387        Ok(Expr::Array(items))
25388    }
25389
25390    fn parse_vector_literal_body(&mut self) -> Result<Expr, ParseError> {
25391        let mut elems = Vec::new();
25392        if matches!(self.peek(), Token::RBracket) {
25393            self.advance();
25394            return Ok(Expr::Literal(Literal::Vector(elems)));
25395        }
25396        loop {
25397            let e = self.parse_expr(0)?;
25398            let x = extract_numeric_literal(&e).ok_or_else(|| ParseError {
25399                message: format!("vector element must be a numeric literal, got {e:?}"),
25400                token_pos: self.pos,
25401            })?;
25402            elems.push(x);
25403            match self.peek() {
25404                Token::Comma => {
25405                    self.advance();
25406                }
25407                Token::RBracket => {
25408                    self.advance();
25409                    break;
25410                }
25411                other => {
25412                    return Err(self.err(format!("expected ',' or ']' in vector, got {other:?}")));
25413                }
25414            }
25415        }
25416        Ok(Expr::Literal(Literal::Vector(elems)))
25417    }
25418
25419    /// Atom that started with an identifier: could be `t.col`, `col`, or
25420    /// `func(arg, ...)`. Detect each shape by looking at the next token.
25421    /// v4.12: parse `(PARTITION BY expr, ... ORDER BY expr [DESC]
25422    /// [, ...])`. Caller has already consumed `OVER`. Either clause
25423    /// is optional; an empty `()` is also legal (PG semantics).
25424    /// v6.4.2 — consume an optional `IGNORE NULLS` / `RESPECT NULLS`
25425    /// modifier between `name(args)` and `OVER (...)`. Default is
25426    /// `Respect`. Unrecognised idents leave the stream unchanged.
25427    fn parse_null_treatment_modifier(&mut self) -> NullTreatment {
25428        let Token::Ident(s) = self.peek().clone() else {
25429            return NullTreatment::Respect;
25430        };
25431        let is_ignore = s.eq_ignore_ascii_case("ignore");
25432        let is_respect = s.eq_ignore_ascii_case("respect");
25433        if !is_ignore && !is_respect {
25434            return NullTreatment::Respect;
25435        }
25436        // Lookahead for NULLS — only consume both tokens together.
25437        // pos+1 must hold a "nulls" ident.
25438        if self.pos + 1 < self.tokens.len()
25439            && let Token::Ident(s2) = &self.tokens[self.pos + 1]
25440            && s2.eq_ignore_ascii_case("nulls")
25441        {
25442            self.advance();
25443            self.advance();
25444            return if is_ignore {
25445                NullTreatment::Ignore
25446            } else {
25447                NullTreatment::Respect
25448            };
25449        }
25450        NullTreatment::Respect
25451    }
25452
25453    /// v7.32 (mailrs round-29) — `agg(args) FILTER (WHERE cond)`.
25454    /// `FILTER` is an unreserved keyword, so it arrives as an `Ident`
25455    /// (same shape as the `OVER` tail). Consumes the whole clause and
25456    /// returns the predicate; returns `None` when no `FILTER` follows.
25457    fn parse_filter_clause(&mut self) -> Result<Option<Box<Expr>>, ParseError> {
25458        let (Token::Ident(s) | Token::QuotedIdent(s)) = self.peek() else {
25459            return Ok(None);
25460        };
25461        if !s.eq_ignore_ascii_case("filter") {
25462            return Ok(None);
25463        }
25464        self.advance(); // FILTER
25465        if !matches!(self.peek(), Token::LParen) {
25466            return Err(self.err(format!("expected '(' after FILTER, got {:?}", self.peek())));
25467        }
25468        self.advance(); // (
25469        if !matches!(self.peek(), Token::Where) {
25470            return Err(self.err(format!(
25471                "expected WHERE inside FILTER (...), got {:?}",
25472                self.peek()
25473            )));
25474        }
25475        self.advance(); // WHERE
25476        let cond = self.parse_expr(0)?;
25477        if !matches!(self.peek(), Token::RParen) {
25478            return Err(self.err(format!(
25479                "expected ')' to close FILTER (WHERE ...), got {:?}",
25480                self.peek()
25481            )));
25482        }
25483        self.advance(); // )
25484        Ok(Some(Box::new(cond)))
25485    }
25486
25487    /// v7.39 (round 354, M12) — consume a `SEPARATOR '<s>'` tail and push
25488    /// the separator as the aggregate's second argument, which is the
25489    /// shape `string_agg` already takes. Returns whether one was there.
25490    fn consume_group_concat_separator(&mut self, args: &mut Vec<Expr>) -> Result<bool, ParseError> {
25491        if !matches!(self.peek(), Token::Ident(k) if k.eq_ignore_ascii_case("separator")) {
25492            return Ok(false);
25493        }
25494        self.advance();
25495        let Token::String(sep) = self.peek().clone() else {
25496            return Err(self.err(alloc::format!(
25497                "expected a string literal after SEPARATOR, got {:?}",
25498                self.peek()
25499            )));
25500        };
25501        self.advance();
25502        args.push(Expr::Literal(Literal::String(sep)));
25503        Ok(true)
25504    }
25505
25506    /// v7.32 (round-29) — `WITHIN GROUP ( ORDER BY <sort_spec> )` tail
25507    /// for ordered-set aggregates. `WITHIN` is unreserved (arrives as an
25508    /// `Ident`); `GROUP` and `ORDER`/`BY` are keywords. Returns the sort
25509    /// keys, or an empty vec when no `WITHIN GROUP` follows.
25510    fn parse_within_group_clause(&mut self) -> Result<Vec<OrderBy>, ParseError> {
25511        let (Token::Ident(s) | Token::QuotedIdent(s)) = self.peek() else {
25512            return Ok(Vec::new());
25513        };
25514        if !s.eq_ignore_ascii_case("within") {
25515            return Ok(Vec::new());
25516        }
25517        self.advance(); // WITHIN
25518        if !matches!(self.peek(), Token::Group) {
25519            return Err(self.err(format!(
25520                "expected GROUP after WITHIN, got {:?}",
25521                self.peek()
25522            )));
25523        }
25524        self.advance(); // GROUP
25525        if !matches!(self.peek(), Token::LParen) {
25526            return Err(self.err(format!(
25527                "expected '(' after WITHIN GROUP, got {:?}",
25528                self.peek()
25529            )));
25530        }
25531        self.advance(); // (
25532        if !matches!(self.peek(), Token::Order) {
25533            return Err(self.err(format!(
25534                "expected ORDER BY inside WITHIN GROUP (...), got {:?}",
25535                self.peek()
25536            )));
25537        }
25538        self.advance(); // ORDER
25539        if !self.peek_is_by() {
25540            return Err(self.err(format!("expected BY after ORDER, got {:?}", self.peek())));
25541        }
25542        self.advance(); // BY
25543        let mut keys: Vec<OrderBy> = Vec::new();
25544        loop {
25545            // v7.39 (round 691) — save/restore, the discipline this parser
25546            // already uses around `pending_sample_preds`, so a subquery inside
25547            // a key neither inherits nor leaks the channel.
25548            let saved_flag = core::mem::replace(&mut self.in_order_by_key, true);
25549            let saved_coll = self.order_key_collation.take();
25550            let parsed = self.parse_expr(0);
25551            self.in_order_by_key = saved_flag;
25552            let collation = core::mem::replace(&mut self.order_key_collation, saved_coll);
25553            let expr = parsed?;
25554            let desc = if matches!(self.peek(), Token::Desc) {
25555                self.advance();
25556                true
25557            } else if matches!(self.peek(), Token::Asc) {
25558                self.advance();
25559                false
25560            } else {
25561                false
25562            };
25563            let nulls_first = self.parse_optional_nulls_placement()?;
25564            keys.push(OrderBy {
25565                expr,
25566                desc,
25567                nulls_first,
25568                collation,
25569            });
25570            if matches!(self.peek(), Token::Comma) {
25571                self.advance();
25572            } else {
25573                break;
25574            }
25575        }
25576        if !matches!(self.peek(), Token::RParen) {
25577            return Err(self.err(format!(
25578                "expected ')' to close WITHIN GROUP (ORDER BY ...), got {:?}",
25579                self.peek()
25580            )));
25581        }
25582        self.advance(); // )
25583        Ok(keys)
25584    }
25585
25586    /// No frame clause is supported.
25587    #[allow(clippy::type_complexity)] // (partitions, ordered-keys-with-desc) is the natural shape
25588    fn parse_over_clause(
25589        &mut self,
25590    ) -> Result<
25591        (
25592            Vec<Expr>,
25593            Vec<(Expr, bool, Option<bool>)>,
25594            Option<WindowFrame>,
25595        ),
25596        ParseError,
25597    > {
25598        // `OVER w` — a named-window reference. The WINDOW clause
25599        // parses after the select list, so the name rides out as a
25600        // marker in partition_by; parse_bare_select substitutes the
25601        // definition once the clause is known.
25602        if let Token::Ident(w) | Token::QuotedIdent(w) = self.peek() {
25603            let name = w.clone();
25604            self.advance();
25605            return Ok((
25606                alloc::vec![Expr::Column(crate::ast::ColumnName {
25607                    qualifier: Some("__named_window__".to_string()),
25608                    name,
25609                })],
25610                Vec::new(),
25611                None,
25612            ));
25613        }
25614        if !matches!(self.peek(), Token::LParen) {
25615            return Err(self.err(format!("expected '(' after OVER, got {:?}", self.peek())));
25616        }
25617        self.advance();
25618        let mut partition_by = Vec::new();
25619        let mut order_by = Vec::new();
25620        // v7.39 (round 229) — `OVER (w1 …)`: a *copy* of an existing named
25621        // window, refined in place. PG's rules (probed against 18.4) differ
25622        // from the bare `OVER w1` form, so the reference rides out under its
25623        // own marker and `substitute_named_windows` applies them. The base
25624        // name is any leading identifier that isn't a window-spec keyword.
25625        let base_window = match self.peek() {
25626            Token::Ident(s) | Token::QuotedIdent(s)
25627                if !s.eq_ignore_ascii_case("partition")
25628                    && !s.eq_ignore_ascii_case("rows")
25629                    && !s.eq_ignore_ascii_case("range")
25630                    && !s.eq_ignore_ascii_case("groups") =>
25631            {
25632                let n = s.clone();
25633                self.advance();
25634                Some(n)
25635            }
25636            _ => None,
25637        };
25638        // PARTITION BY ?
25639        // v7.37.6-B promoted PARTITION to a reserved keyword
25640        // (Token::Partition); pre-7.37.6-B catalogs lexed it as
25641        // `Token::Ident("partition")`. Accept both so older sources
25642        // and the new lexer surface land on the same path.
25643        let is_partition_kw = match self.peek() {
25644            Token::Partition => true,
25645            Token::Ident(s) | Token::QuotedIdent(s) => s.eq_ignore_ascii_case("partition"),
25646            _ => false,
25647        };
25648        if is_partition_kw {
25649            self.advance();
25650            if !self.peek_is_by() {
25651                return Err(self.err(format!(
25652                    "expected BY after PARTITION, got {:?}",
25653                    self.peek()
25654                )));
25655            }
25656            self.advance();
25657            loop {
25658                partition_by.push(self.parse_expr(0)?);
25659                if matches!(self.peek(), Token::Comma) {
25660                    self.advance();
25661                    continue;
25662                }
25663                break;
25664            }
25665        }
25666        // ORDER BY ?
25667        if matches!(self.peek(), Token::Order) {
25668            self.advance();
25669            if !self.peek_is_by() {
25670                return Err(self.err(format!("expected BY after ORDER, got {:?}", self.peek())));
25671            }
25672            self.advance();
25673            loop {
25674                let e = self.parse_expr(0)?;
25675                let desc = if matches!(self.peek(), Token::Desc) {
25676                    self.advance();
25677                    true
25678                } else if matches!(self.peek(), Token::Asc) {
25679                    self.advance();
25680                    false
25681                } else {
25682                    false
25683                };
25684                // v7.24.1 — NULLS FIRST/LAST inside OVER (…).
25685                let nulls_first = self.parse_optional_nulls_placement()?;
25686                order_by.push((e, desc, nulls_first));
25687                if matches!(self.peek(), Token::Comma) {
25688                    self.advance();
25689                    continue;
25690                }
25691                break;
25692            }
25693        }
25694        // v4.20: optional explicit frame, `ROWS ...` / `RANGE ...`.
25695        // Both keywords come through the lexer as identifiers; match
25696        // case-insensitively.
25697        let mut frame: Option<WindowFrame> = None;
25698        if let Token::Ident(s) | Token::QuotedIdent(s) = self.peek() {
25699            let kind = if s.eq_ignore_ascii_case("rows") {
25700                Some(FrameKind::Rows)
25701            } else if s.eq_ignore_ascii_case("range") {
25702                Some(FrameKind::Range)
25703            } else if s.eq_ignore_ascii_case("groups") {
25704                // v7.37.19 (19.11) — PG 11+ GROUPS frame mode.
25705                Some(FrameKind::Groups)
25706            } else {
25707                None
25708            };
25709            if let Some(kind) = kind {
25710                self.advance();
25711                frame = Some(self.parse_frame_tail(kind)?);
25712            }
25713        }
25714        if !matches!(self.peek(), Token::RParen) {
25715            return Err(self.err(format!(
25716                "expected ')' to close OVER clause, got {:?}",
25717                self.peek()
25718            )));
25719        }
25720        self.advance();
25721        if let Some(base) = base_window {
25722            // A copy may refine but never override the base's partitioning
25723            // (PG rejects it outright, before looking the name up).
25724            if !partition_by.is_empty() {
25725                return Err(self.err(alloc::format!(
25726                    "cannot override PARTITION BY clause of window \"{base}\""
25727                )));
25728            }
25729            partition_by = alloc::vec![Expr::Column(crate::ast::ColumnName {
25730                qualifier: Some("__named_window_ref__".to_string()),
25731                name: base,
25732            })];
25733        }
25734        Ok((partition_by, order_by, frame))
25735    }
25736
25737    /// v4.20: parse the tail of an explicit frame, given the `ROWS`
25738    /// or `RANGE` keyword was just consumed. Accepts both
25739    /// `BETWEEN <bound> AND <bound>` and the single-bound shorthand
25740    /// (`ROWS UNBOUNDED PRECEDING`, `ROWS 5 PRECEDING`, etc.) which
25741    /// PG normalises to `BETWEEN <bound> AND CURRENT ROW`.
25742    fn parse_frame_tail(&mut self, kind: FrameKind) -> Result<WindowFrame, ParseError> {
25743        let (start, end) = if matches!(self.peek(), Token::Between) {
25744            self.advance();
25745            let start = self.parse_frame_bound()?;
25746            if !matches!(self.peek(), Token::And) {
25747                return Err(self.err(format!("expected AND in frame spec, got {:?}", self.peek())));
25748            }
25749            self.advance();
25750            let end = self.parse_frame_bound()?;
25751            (start, Some(end))
25752        } else {
25753            (self.parse_frame_bound()?, None)
25754        };
25755        let exclude = self.parse_frame_exclusion()?;
25756        Ok(WindowFrame {
25757            kind,
25758            start,
25759            end,
25760            exclude,
25761        })
25762    }
25763
25764    /// Optional `EXCLUDE {CURRENT ROW | GROUP | TIES | NO OTHERS}`
25765    /// after a frame spec. NO OTHERS is the default no-op.
25766    fn parse_frame_exclusion(&mut self) -> Result<FrameExclusion, ParseError> {
25767        if !matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("exclude")) {
25768            return Ok(FrameExclusion::NoOthers);
25769        }
25770        self.advance(); // EXCLUDE
25771        match self.peek() {
25772            Token::Ident(s) if s.eq_ignore_ascii_case("current") => {
25773                self.advance();
25774                if !matches!(self.peek(), Token::Ident(r) if r.eq_ignore_ascii_case("row")) {
25775                    return Err(self.err(format!(
25776                        "expected ROW after EXCLUDE CURRENT, got {:?}",
25777                        self.peek()
25778                    )));
25779                }
25780                self.advance();
25781                Ok(FrameExclusion::CurrentRow)
25782            }
25783            // v7.39 (read01 round 109) — GROUP is a reserved keyword token, so
25784            // `EXCLUDE GROUP` arrives as `Token::Group`, not `Ident("group")`.
25785            // Without this arm it fell to the catch-all, whose message
25786            // self-contradictingly listed GROUP as expected.
25787            Token::Ident(s) if s.eq_ignore_ascii_case("group") => {
25788                self.advance();
25789                Ok(FrameExclusion::Group)
25790            }
25791            Token::Group => {
25792                self.advance();
25793                Ok(FrameExclusion::Group)
25794            }
25795            Token::Ident(s) if s.eq_ignore_ascii_case("ties") => {
25796                self.advance();
25797                Ok(FrameExclusion::Ties)
25798            }
25799            Token::Ident(s) if s.eq_ignore_ascii_case("no") => {
25800                self.advance();
25801                if !matches!(self.peek(), Token::Ident(r) if r.eq_ignore_ascii_case("others")) {
25802                    return Err(self.err(format!(
25803                        "expected OTHERS after EXCLUDE NO, got {:?}",
25804                        self.peek()
25805                    )));
25806                }
25807                self.advance();
25808                Ok(FrameExclusion::NoOthers)
25809            }
25810            other => Err(self.err(format!(
25811                "expected CURRENT ROW / GROUP / TIES / NO OTHERS after EXCLUDE, got {other:?}"
25812            ))),
25813        }
25814    }
25815
25816    /// Parse one frame bound: `UNBOUNDED PRECEDING`, `<n> PRECEDING`,
25817    /// `<interval> PRECEDING`, `CURRENT ROW`, `<n>/<interval> FOLLOWING`,
25818    /// `UNBOUNDED FOLLOWING`.
25819    fn parse_frame_bound(&mut self) -> Result<FrameBound, ParseError> {
25820        // Interval-typed offset for a value-based RANGE frame over a
25821        // DATE / TIMESTAMP ORDER BY column (PG time-series windows),
25822        // spelled `INTERVAL '1 day' PRECEDING` or `'1 day'::interval
25823        // PRECEDING`.
25824        if let Some((months, days, micros)) = self.try_take_interval_offset()? {
25825            let dir = self.expect_ident_like()?;
25826            return if dir.eq_ignore_ascii_case("preceding") {
25827                Ok(FrameBound::IntervalPreceding {
25828                    months,
25829                    days,
25830                    micros,
25831                })
25832            } else if dir.eq_ignore_ascii_case("following") {
25833                Ok(FrameBound::IntervalFollowing {
25834                    months,
25835                    days,
25836                    micros,
25837                })
25838            } else {
25839                Err(self.err(format!(
25840                    "expected PRECEDING or FOLLOWING after interval offset, got {dir:?}"
25841                )))
25842            };
25843        }
25844        // Number-led: "<n> PRECEDING" / "<n> FOLLOWING".
25845        if let Token::Integer(n) = *self.peek() {
25846            self.advance();
25847            let n: u64 = u64::try_from(n).map_err(|_| {
25848                self.err(format!(
25849                    "invalid frame offset {n} — expected non-negative integer"
25850                ))
25851            })?;
25852            let dir = self.expect_ident_like()?;
25853            return if dir.eq_ignore_ascii_case("preceding") {
25854                Ok(FrameBound::OffsetPreceding(n))
25855            } else if dir.eq_ignore_ascii_case("following") {
25856                Ok(FrameBound::OffsetFollowing(n))
25857            } else {
25858                Err(self.err(format!(
25859                    "expected PRECEDING or FOLLOWING after offset, got {dir:?}"
25860                )))
25861            };
25862        }
25863        let first = self.expect_ident_like()?;
25864        if first.eq_ignore_ascii_case("unbounded") {
25865            let dir = self.expect_ident_like()?;
25866            return if dir.eq_ignore_ascii_case("preceding") {
25867                Ok(FrameBound::UnboundedPreceding)
25868            } else if dir.eq_ignore_ascii_case("following") {
25869                Ok(FrameBound::UnboundedFollowing)
25870            } else {
25871                Err(self.err(format!(
25872                    "expected PRECEDING or FOLLOWING after UNBOUNDED, got {dir:?}"
25873                )))
25874            };
25875        }
25876        if first.eq_ignore_ascii_case("current") {
25877            let row = self.expect_ident_like()?;
25878            if !row.eq_ignore_ascii_case("row") {
25879                return Err(self.err(format!("expected ROW after CURRENT, got {row:?}")));
25880            }
25881            return Ok(FrameBound::CurrentRow);
25882        }
25883        Err(self.err(format!(
25884            "expected frame bound (UNBOUNDED/CURRENT/<n>), got {first:?}"
25885        )))
25886    }
25887
25888    /// Detect and consume a leading interval offset in a frame bound —
25889    /// `INTERVAL '1 day'` or `'1 day'::interval` — returning its folded
25890    /// `(months, days, micros)`. Leaves the cursor on the trailing
25891    /// PRECEDING / FOLLOWING keyword. Returns `None` (without advancing)
25892    /// when the next tokens are not an interval offset.
25893    fn try_take_interval_offset(&mut self) -> Result<Option<(i32, i32, i64)>, ParseError> {
25894        // Shape A — `INTERVAL '1 day'`.
25895        if matches!(self.peek(), Token::Interval) {
25896            self.advance(); // INTERVAL
25897            let atom = self.parse_interval_atom()?;
25898            if let Expr::Literal(Literal::Interval {
25899                months,
25900                days,
25901                micros,
25902                ..
25903            }) = atom
25904            {
25905                return Ok(Some((months, days, micros)));
25906            }
25907            return Err(self.err("expected an interval literal in frame offset".to_string()));
25908        }
25909        // Shape B — `'1 day'::interval`. Look ahead for the exact
25910        // string / `::` / interval-target triple before committing.
25911        if let Token::String(text) = self.peek() {
25912            let target_is_interval = match self.tokens.get(self.pos + 2) {
25913                Some(Token::Interval) => true,
25914                Some(Token::Ident(s)) => s.eq_ignore_ascii_case("interval"),
25915                _ => false,
25916            };
25917            let is_cast = matches!(self.tokens.get(self.pos + 1), Some(Token::DoubleColon))
25918                && target_is_interval;
25919            if is_cast {
25920                let text = text.clone();
25921                self.advance(); // string
25922                self.advance(); // ::
25923                self.advance(); // interval
25924                let parts = parse_interval_text(&text).ok_or_else(|| {
25925                    self.err(format!("cannot parse INTERVAL {text:?} in frame offset"))
25926                })?;
25927                return Ok(Some(parts));
25928            }
25929        }
25930        Ok(None)
25931    }
25932
25933    fn finish_ident_atom(&mut self, first: String) -> Result<Expr, ParseError> {
25934        // v7.39.2 — MySQL's charset INTRODUCER: `_utf8mb4'x'`, `N'y'`,
25935        // `_binary'z'`. All three were `ERROR 1064 syntax error` here
25936        // and all three answer the literal on MySQL 9.7.2.
25937        //
25938        // It is not only syntax, which is why it waited for
25939        // `Expr::Collate`: measured, `_binary'A' = 'a'` is 0 on MySQL
25940        // because `_binary` makes the comparison byte-wise, while
25941        // `_utf8mb4'A' = _utf8mb4'a'` is 1. Accepting the syntax and
25942        // dropping the charset would have turned a hard error into a
25943        // silently wrong comparison — worse than the error it replaced.
25944        //
25945        // An UNKNOWN charset is NOT an introducer: MySQL answers
25946        // `Unknown column '_nosuch'`, because it parses as a column
25947        // reference followed by a string. So the table decides, and it
25948        // is the same table `SET NAMES` reads.
25949        //
25950        // A space is allowed between the two (`_utf8mb4 'x'`), which
25951        // falls out of asking the token stream rather than the bytes.
25952        if self.mysql_dialect
25953            && let Token::String(_) = self.peek()
25954        {
25955            let lower = first.to_ascii_lowercase();
25956            let charset = if lower == "n" {
25957                // `N'…'` is the national character set, which MySQL
25958                // documents as utf8 — utf8mb3 in 9.7.2's spelling.
25959                //
25960                // utf8mb3 and utf8mb4 both fold case in their default
25961                // collations, so nothing SPG can be asked distinguishes
25962                // the two here: an ablation swapping this to utf8mb4
25963                // reddens no pin. Recorded rather than implied — the
25964                // spelling follows MySQL's documentation, not a
25965                // measurement.
25966                Some("utf8mb3")
25967            } else {
25968                // No filter here: the lookup below IS the test for
25969                // "is this a charset". An ablation that removed a filter
25970                // in this spot reddened nothing, which is how the two
25971                // were found to be one check written twice.
25972                lower.strip_prefix('_')
25973            };
25974            if let Some(cs) = charset
25975                && let Some(collation) = crate::charset::charset_default_collation(cs)
25976            {
25977                let Token::String(body) = self.advance() else {
25978                    unreachable!("peeked a string");
25979                };
25980                return Ok(Expr::Collate {
25981                    expr: Box::new(Expr::Literal(Literal::String(body))),
25982                    collation: String::from(collation),
25983                });
25984            }
25985        }
25986        if matches!(self.peek(), Token::Dot) {
25987            self.advance();
25988            let name = self.expect_ident_like()?;
25989            // v7.14.0 — schema-qualified function call
25990            // `<schema>.<fn>(args)`. PG dumps emit
25991            // `pg_catalog.set_config(...)` in the preamble. SPG
25992            // is single-namespace: drop the schema prefix and
25993            // route the dispatch on the bare function name.
25994            if matches!(self.peek(), Token::LParen) {
25995                return self.finish_ident_atom(name);
25996            }
25997            return Ok(Expr::Column(ColumnName {
25998                qualifier: Some(first),
25999                name,
26000            }));
26001        }
26002        if matches!(self.peek(), Token::LParen) {
26003            self.advance();
26004            // `COUNT(*)` — special-cased here because `*` isn't a normal
26005            // expression token. Lower-case match on `first` since the lexer
26006            // folds identifiers.
26007            if first.eq_ignore_ascii_case("count") && matches!(self.peek(), Token::Star) {
26008                self.advance();
26009                if !matches!(self.peek(), Token::RParen) {
26010                    return Err(self.err(format!(
26011                        "expected ')' after COUNT(*), got {:?}",
26012                        self.peek()
26013                    )));
26014                }
26015                self.advance();
26016                // v7.32 (round-29) — `COUNT(*) FILTER (WHERE …)`.
26017                let filter = self.parse_filter_clause()?;
26018                // v4.12: COUNT(*) OVER (...) — same window tail.
26019                let null_treatment = self.parse_null_treatment_modifier();
26020                if let Token::Ident(s) | Token::QuotedIdent(s) = self.peek()
26021                    && s.eq_ignore_ascii_case("over")
26022                {
26023                    self.advance();
26024                    let (partition_by, order_by, frame) = self.parse_over_clause()?;
26025                    return Ok(Expr::WindowFunction {
26026                        name: "count_star".into(),
26027                        args: Vec::new(),
26028                        partition_by,
26029                        order_by,
26030                        frame,
26031                        null_treatment,
26032                        filter,
26033                    });
26034                }
26035                if let Some(filter) = filter {
26036                    return Ok(Expr::AggregateOrdered {
26037                        call: Box::new(Expr::FunctionCall {
26038                            name: "count_star".into(),
26039                            args: Vec::new(),
26040                        }),
26041                        order_by: Vec::new(),
26042                        distinct: false,
26043                        filter: Some(filter),
26044                    });
26045                }
26046                return Ok(Expr::FunctionCall {
26047                    name: "count_star".into(),
26048                    args: Vec::new(),
26049                });
26050            }
26051            // Function call. PG-style: zero-or-more comma-separated args.
26052            let mut args = Vec::new();
26053            // v7.38 (read01, T14) — named-argument notation `argname => value`.
26054            // Names are collected in lock-step with `args` and resolved to
26055            // positional order after the loop (the AST stays positional).
26056            let mut arg_names: Vec<Option<String>> = Vec::new();
26057            let mut agg_order_by: Vec<OrderBy> = Vec::new();
26058            // v7.39 (round 354, M12) — whether a `SEPARATOR '<s>'` tail was
26059            // seen, so the value arguments before it can be folded.
26060            let mut saw_separator = false;
26061            // v7.25 (round-17) — `COUNT(DISTINCT x)` and friends.
26062            // v7.32 (round-29) — accept the dual `ALL` quantifier too
26063            // (the default; ORMs emit `COUNT(ALL x)` / `SUM(ALL x)`).
26064            let agg_distinct = if matches!(self.peek(), Token::Distinct) {
26065                self.advance();
26066                true
26067            } else if matches!(self.peek(), Token::All) {
26068                self.advance();
26069                false
26070            } else {
26071                false
26072            };
26073            // v7.37.17 (17.6 siblings) — MySQL TIMESTAMPADD /
26074            // TIMESTAMPDIFF take a bare unit keyword as the first
26075            // argument (MINUTE, DAY, ...), and GET_FORMAT takes a
26076            // bare type keyword (DATE / TIME / DATETIME); lower them
26077            // onto string literals so the evaluator sees plain text.
26078            if ((first.eq_ignore_ascii_case("timestampadd")
26079                || first.eq_ignore_ascii_case("timestampdiff"))
26080                && matches!(self.peek(), Token::Ident(u) if matches!(
26081                    u.to_ascii_lowercase().as_str(),
26082                    "microsecond" | "second" | "minute" | "hour" | "day"
26083                        | "week" | "month" | "quarter" | "year"
26084                )))
26085                || (first.eq_ignore_ascii_case("get_format")
26086                    && matches!(self.peek(), Token::Ident(u) if matches!(
26087                        u.to_ascii_lowercase().as_str(),
26088                        "date" | "time" | "datetime" | "timestamp"
26089                    )))
26090            {
26091                if let Token::Ident(u) = self.peek() {
26092                    args.push(Expr::Literal(Literal::String(u.to_ascii_lowercase())));
26093                }
26094                self.advance();
26095                if matches!(self.peek(), Token::Comma) {
26096                    self.advance();
26097                }
26098            }
26099            // `ROW(a, b, …)` keyword constructor. Followed by a
26100            // comparison operator or [NOT] IN it joins the paren
26101            // row-constructor machinery (fieldwise parse-time
26102            // expansion); bare, it stays a `row` call the evaluator
26103            // renders as PG record text.
26104            if first.eq_ignore_ascii_case("row") {
26105                let mut row_items = Vec::new();
26106                if !matches!(self.peek(), Token::RParen) {
26107                    loop {
26108                        row_items.push(self.parse_expr(0)?);
26109                        match self.peek() {
26110                            Token::Comma => {
26111                                self.advance();
26112                            }
26113                            Token::RParen => break,
26114                            other => {
26115                                return Err(self.err(format!(
26116                                    "expected ',' or ')' in ROW(...), got {other:?}"
26117                                )));
26118                            }
26119                        }
26120                    }
26121                }
26122                self.advance(); // ')'
26123                let comparison_follows = matches!(
26124                    self.peek(),
26125                    Token::Eq
26126                        | Token::NotEq
26127                        | Token::Lt
26128                        | Token::LtEq
26129                        | Token::Gt
26130                        | Token::GtEq
26131                        | Token::In
26132                ) || (matches!(self.peek(), Token::Not)
26133                    && matches!(self.tokens.get(self.pos + 1), Some(Token::In)))
26134                    || matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("overlaps"));
26135                if comparison_follows && !row_items.is_empty() {
26136                    return self.parse_row_comparison_tail(row_items);
26137                }
26138                return Ok(Expr::FunctionCall {
26139                    name: String::from("row"),
26140                    args: row_items,
26141                });
26142            }
26143            // v7.39 (read01 xml.c) — `XMLPARSE(DOCUMENT|CONTENT expr)`:
26144            // the parse-mode keyword introduces the source text. SPG
26145            // carries XML as text, so both modes lower to __xmlparse(expr)
26146            // which validates well-formedness and returns Value::Xml.
26147            if first.eq_ignore_ascii_case("xmlparse")
26148                && matches!(self.peek(), Token::Ident(kw)
26149                    if kw.eq_ignore_ascii_case("document")
26150                        || kw.eq_ignore_ascii_case("content"))
26151            {
26152                let mode = match self.advance() {
26153                    Token::Ident(kw) => kw.to_ascii_lowercase(),
26154                    _ => unreachable!("peeked an ident"),
26155                };
26156                let src = self.parse_expr(0)?;
26157                if !matches!(self.peek(), Token::RParen) {
26158                    return Err(self.err(format!(
26159                        "expected ')' to close XMLPARSE, got {:?}",
26160                        self.peek()
26161                    )));
26162                }
26163                self.advance();
26164                return Ok(Expr::FunctionCall {
26165                    name: String::from("__xmlparse"),
26166                    args: alloc::vec![src, Expr::Literal(Literal::String(mode))],
26167                });
26168            }
26169            // SQL/XML `XMLELEMENT(NAME ident [, content …])` — the NAME
26170            // keyword introduces the element name (a bare or quoted
26171            // identifier), then optional content expressions. Lower to a
26172            // plain `xmlelement(name_text, content …)` call.
26173            if first.eq_ignore_ascii_case("xmlelement")
26174                && matches!(self.peek(), Token::Ident(kw) if kw.eq_ignore_ascii_case("name"))
26175            {
26176                self.advance(); // consume NAME
26177                let elem_name = match self.peek().clone() {
26178                    Token::Ident(n) | Token::QuotedIdent(n) => {
26179                        self.advance();
26180                        n
26181                    }
26182                    other => {
26183                        return Err(self.err(format!(
26184                            "expected element name after XMLELEMENT NAME, got {other:?}"
26185                        )));
26186                    }
26187                };
26188                let mut args = alloc::vec![Expr::Literal(Literal::String(elem_name))];
26189                while matches!(self.peek(), Token::Comma) {
26190                    self.advance();
26191                    args.push(self.parse_expr(0)?);
26192                }
26193                if !matches!(self.peek(), Token::RParen) {
26194                    return Err(self.err(format!(
26195                        "expected ')' to close XMLELEMENT, got {:?}",
26196                        self.peek()
26197                    )));
26198                }
26199                self.advance();
26200                return Ok(Expr::FunctionCall {
26201                    name: String::from("xmlelement"),
26202                    args,
26203                });
26204            }
26205            // SQL/XML `XMLFOREST(value [AS name], …)` — each `value AS name`
26206            // becomes a `<name>value</name>` element; a bare column infers its
26207            // own name. Lower to `xmlforest(name1, val1, name2, val2, …)`.
26208            // v7.39.2 — MySQL's two CONVERT forms, neither of which parsed.
26209            // `CONVERT(expr USING cs)` was a syntax error at USING, and
26210            // `CONVERT(expr, CHAR)` was read as PostgreSQL's three-argument
26211            // `convert(bytea, src, dest)` and answered `column "char" does
26212            // not exist`. Both are casts in MySQL: measured on 9.7.2,
26213            // `CONVERT(0x41 USING utf8mb4)` and `CONVERT(0x41, CHAR)` are
26214            // both 'A', and `CONVERT(123, CHAR)` is '123'.
26215            //
26216            // The charset is checked against the same table the introducers
26217            // use, so an unknown one is refused rather than quietly ignored.
26218            if self.mysql_dialect
26219                && first.eq_ignore_ascii_case("convert")
26220                && !matches!(self.peek(), Token::RParen)
26221            {
26222                let save = self.pos;
26223                let inner = self.parse_expr(0)?;
26224                if matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("using")) {
26225                    self.advance();
26226                    let cs = match self.peek().clone() {
26227                        Token::Ident(n) | Token::QuotedIdent(n) => {
26228                            self.advance();
26229                            n
26230                        }
26231                        other => {
26232                            return Err(self.err(alloc::format!(
26233                                "expected a charset after USING, got {other:?}"
26234                            )));
26235                        }
26236                    };
26237                    let lc = cs.to_ascii_lowercase();
26238                    if lc != "binary" && crate::charset::charset_default_collation(&lc).is_none() {
26239                        return Err(self.err(alloc::format!("unknown character set: '{cs}'")));
26240                    }
26241                    if !matches!(self.peek(), Token::RParen) {
26242                        return Err(self.err(alloc::format!(
26243                            "expected ')' after CONVERT … USING, got {:?}",
26244                            self.peek()
26245                        )));
26246                    }
26247                    self.advance();
26248                    let target = if lc == "binary" {
26249                        CastTarget::Named("binary".to_string())
26250                    } else {
26251                        CastTarget::Text
26252                    };
26253                    return self.finish_postfix_casts(Expr::Cast {
26254                        expr: alloc::boxed::Box::new(inner),
26255                        target,
26256                    });
26257                }
26258                if matches!(self.peek(), Token::Comma) {
26259                    self.advance();
26260                    // A type name here is MySQL's cast form; anything else
26261                    // (three string arguments) is PostgreSQL's `convert`,
26262                    // which keeps its own path.
26263                    if let Ok(target) = self.parse_cast_target()
26264                        && matches!(self.peek(), Token::RParen)
26265                    {
26266                        self.advance();
26267                        return self.finish_postfix_casts(Expr::Cast {
26268                            expr: alloc::boxed::Box::new(inner),
26269                            target,
26270                        });
26271                    }
26272                }
26273                self.pos = save;
26274            }
26275            if first.eq_ignore_ascii_case("xmlforest") && !matches!(self.peek(), Token::RParen) {
26276                let mut args: Vec<Expr> = Vec::new();
26277                loop {
26278                    let val = self.parse_expr(0)?;
26279                    let name = if matches!(self.peek(), Token::As) {
26280                        self.advance();
26281                        match self.peek().clone() {
26282                            Token::Ident(n) | Token::QuotedIdent(n) => {
26283                                self.advance();
26284                                n
26285                            }
26286                            other => {
26287                                return Err(self.err(format!(
26288                                    "expected name after AS in XMLFOREST, got {other:?}"
26289                                )));
26290                            }
26291                        }
26292                    } else if let Expr::Column(c) = &val {
26293                        c.name.clone()
26294                    } else {
26295                        return Err(
26296                            self.err("XMLFOREST element without a column name needs AS".into())
26297                        );
26298                    };
26299                    args.push(Expr::Literal(Literal::String(name)));
26300                    args.push(val);
26301                    if matches!(self.peek(), Token::Comma) {
26302                        self.advance();
26303                    } else {
26304                        break;
26305                    }
26306                }
26307                if !matches!(self.peek(), Token::RParen) {
26308                    return Err(self.err(format!(
26309                        "expected ')' to close XMLFOREST, got {:?}",
26310                        self.peek()
26311                    )));
26312                }
26313                self.advance();
26314                return Ok(Expr::FunctionCall {
26315                    name: String::from("xmlforest"),
26316                    args,
26317                });
26318            }
26319            // SQL-standard `POSITION(sub IN str)` — lowers onto
26320            // strpos(str, sub). IN is the argument separator here,
26321            // so the needle parses with the IN-tail suppressed.
26322            if first.eq_ignore_ascii_case("position") && !matches!(self.peek(), Token::RParen) {
26323                let saved = self.suppress_in_tail;
26324                self.suppress_in_tail = true;
26325                let needle = self.parse_expr(0);
26326                self.suppress_in_tail = saved;
26327                let needle = needle?;
26328                if matches!(self.peek(), Token::In) {
26329                    self.advance();
26330                    let haystack = self.parse_expr(0)?;
26331                    if !matches!(self.peek(), Token::RParen) {
26332                        return Err(self.err(format!(
26333                            "expected ')' to close POSITION, got {:?}",
26334                            self.peek()
26335                        )));
26336                    }
26337                    self.advance();
26338                    return Ok(Expr::FunctionCall {
26339                        name: String::from("strpos"),
26340                        args: alloc::vec![haystack, needle],
26341                    });
26342                }
26343                // position(sub, str) comma form (incl. bytea) —
26344                // hand the parsed first arg to the generic list.
26345                args.push(needle);
26346                if matches!(self.peek(), Token::Comma) {
26347                    self.advance();
26348                }
26349            }
26350            // SQL-standard `TRIM([BOTH|LEADING|TRAILING] [chars]
26351            // FROM str)` — lowers onto btrim / ltrim / rtrim. The
26352            // plain comma forms TRIM(str) / TRIM(str, chars) keep
26353            // riding the generic argument list below.
26354            if first.eq_ignore_ascii_case("trim") {
26355                let mode = match self.peek() {
26356                    Token::Ident(k) if k.eq_ignore_ascii_case("both") => {
26357                        self.advance();
26358                        Some("btrim")
26359                    }
26360                    Token::Ident(k) if k.eq_ignore_ascii_case("leading") => {
26361                        self.advance();
26362                        Some("ltrim")
26363                    }
26364                    Token::Ident(k) if k.eq_ignore_ascii_case("trailing") => {
26365                        self.advance();
26366                        Some("rtrim")
26367                    }
26368                    _ => None,
26369                };
26370                if mode.is_some() || matches!(self.peek(), Token::From) {
26371                    // TRIM([mode] FROM str) — no strip-chars.
26372                    let chars = if matches!(self.peek(), Token::From) {
26373                        None
26374                    } else {
26375                        Some(self.parse_expr(0)?)
26376                    };
26377                    if !matches!(self.peek(), Token::From) {
26378                        return Err(self.err(format!(
26379                            "expected FROM in TRIM([BOTH|LEADING|TRAILING] [chars] FROM str), got {:?}",
26380                            self.peek()
26381                        )));
26382                    }
26383                    self.advance();
26384                    let target = self.parse_expr(0)?;
26385                    if !matches!(self.peek(), Token::RParen) {
26386                        return Err(
26387                            self.err(format!("expected ')' to close TRIM, got {:?}", self.peek()))
26388                        );
26389                    }
26390                    self.advance();
26391                    let mut trim_args = alloc::vec![target];
26392                    if let Some(c) = chars {
26393                        trim_args.push(c);
26394                    }
26395                    return Ok(Expr::FunctionCall {
26396                        name: String::from(mode.unwrap_or("btrim")),
26397                        args: trim_args,
26398                    });
26399                }
26400            }
26401            if !matches!(self.peek(), Token::RParen) {
26402                loop {
26403                    // v7.38 (read01, T14) — `argname => value` names this arg.
26404                    // v7.39 (read01 round 77) — `argname := value` is the same
26405                    // thing, and it is the spelling PG's own docs lead with. It
26406                    // was simply never lexed here, so every `f(x := 1)` died in
26407                    // the parser regardless of what `f` was.
26408                    let this_name = match (&self.tokens[self.pos], self.tokens.get(self.pos + 1)) {
26409                        (
26410                            Token::Ident(n) | Token::QuotedIdent(n),
26411                            Some(Token::FatArrow | Token::ColonEq),
26412                        ) => {
26413                            let name = n.clone();
26414                            self.advance(); // name
26415                            self.advance(); // => / :=
26416                            Some(name)
26417                        }
26418                        _ => None,
26419                    };
26420                    // v7.39 (read01 round 100) — `VARIADIC <array>` spreads an
26421                    // array's elements into a variadic call's trailing args
26422                    // (`concat_ws(',', VARIADIC ARRAY[…])`). VARIADIC isn't
26423                    // reserved, so it arrives as a bare ident before the arg.
26424                    let is_variadic = this_name.is_none()
26425                        && matches!(&self.tokens[self.pos], Token::Ident(s) if s.eq_ignore_ascii_case("variadic"));
26426                    if is_variadic {
26427                        self.advance();
26428                    }
26429                    let arg = self.parse_expr(0)?;
26430                    args.push(match &this_name {
26431                        // The callee's parameter names decide the slot, and a
26432                        // user function's live in the catalog. Carry the name
26433                        // to eval rather than guessing here.
26434                        Some(n) => Expr::NamedArg {
26435                            name: n.clone(),
26436                            expr: Box::new(arg),
26437                        },
26438                        None if is_variadic => Expr::Variadic(Box::new(arg)),
26439                        None => arg,
26440                    });
26441                    arg_names.push(this_name);
26442                    // v7.25 (round-17) — standard `CAST(expr AS type)`.
26443                    // The `::` cast already worked; this lowers the
26444                    // function form onto the same Expr::Cast node.
26445                    if first.eq_ignore_ascii_case("cast")
26446                        && args.len() == 1
26447                        && matches!(self.peek(), Token::As)
26448                    {
26449                        self.advance();
26450                        let target = self.parse_cast_target()?;
26451                        if !matches!(self.peek(), Token::RParen) {
26452                            return Err(self.err(format!(
26453                                "expected ')' to close CAST, got {:?}",
26454                                self.peek()
26455                            )));
26456                        }
26457                        self.advance();
26458                        return Ok(Expr::Cast {
26459                            expr: Box::new(args.pop().expect("one arg")),
26460                            target,
26461                        });
26462                    }
26463                    // v7.38 (read01 P6.-) — `normalize(text, FORM)` where FORM is
26464                    // a bare keyword NFC / NFD / NFKC / NFKD. PG parses these as
26465                    // keywords; SPG's lexer makes them plain idents (so they'd be
26466                    // read as column refs). Lower the keyword to the string form
26467                    // the evaluator already accepts.
26468                    if first.eq_ignore_ascii_case("normalize")
26469                        && args.len() == 1
26470                        && matches!(self.peek(), Token::Comma)
26471                    {
26472                        let form = match self.tokens.get(self.pos + 1) {
26473                            Some(Token::Ident(f) | Token::QuotedIdent(f)) => {
26474                                let up = f.to_ascii_uppercase();
26475                                matches!(up.as_str(), "NFC" | "NFD" | "NFKC" | "NFKD").then_some(up)
26476                            }
26477                            _ => None,
26478                        };
26479                        if let Some(up) = form {
26480                            self.advance(); // comma
26481                            self.advance(); // form keyword
26482                            args.push(Expr::Literal(Literal::String(up)));
26483                        }
26484                    }
26485                    // v7.37.7 C.1.8 — PG `substring(str FROM pos FOR len)` syntactic
26486                    // form. Desugars to the comma-list shape evaluator already
26487                    // handles. Triggered after the first arg when the function
26488                    // name is substring / substr and the next token is FROM
26489                    // (a reserved keyword in PG; SPG also reserves it).
26490                    // v7.39 (read01 regexp.c) — `substring(str SIMILAR pat
26491                    // ESCAPE esc)` (SQL:1999 three-part form) desugars to the
26492                    // internal __substring_similar(str, pat, esc) call.
26493                    if (first.eq_ignore_ascii_case("substring")
26494                        || first.eq_ignore_ascii_case("substr"))
26495                        && args.len() == 1
26496                        && matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("similar"))
26497                    {
26498                        self.advance(); // SIMILAR
26499                        let pattern = self.parse_expr(0)?;
26500                        if !matches!(self.peek(), Token::Ident(s) if s.eq_ignore_ascii_case("escape"))
26501                        {
26502                            return Err(self.err(format!(
26503                                "expected ESCAPE in substring(... SIMILAR ...), got {:?}",
26504                                self.peek()
26505                            )));
26506                        }
26507                        self.advance(); // ESCAPE
26508                        let esc = self.parse_expr(0)?;
26509                        if !matches!(self.peek(), Token::RParen) {
26510                            return Err(self.err(format!(
26511                                "expected ')' to close substring(... SIMILAR ...), got {:?}",
26512                                self.peek()
26513                            )));
26514                        }
26515                        self.advance();
26516                        args.push(pattern);
26517                        args.push(esc);
26518                        return Ok(Expr::FunctionCall {
26519                            name: "__substring_similar".to_string(),
26520                            args,
26521                        });
26522                    }
26523                    if (first.eq_ignore_ascii_case("substring")
26524                        || first.eq_ignore_ascii_case("substr"))
26525                        && args.len() == 1
26526                        && matches!(self.peek(), Token::From | Token::For)
26527                    {
26528                        // `substring(str FROM pos [FOR len])`, or the FOR-only
26529                        // `substring(str FOR len)` which PG treats as FROM 1.
26530                        if matches!(self.peek(), Token::From) {
26531                            self.advance();
26532                            let start = self.parse_expr(0)?;
26533                            args.push(start);
26534                        } else {
26535                            args.push(Expr::Literal(Literal::Integer(1)));
26536                        }
26537                        if matches!(self.peek(), Token::For) {
26538                            self.advance();
26539                            let length = self.parse_expr(0)?;
26540                            args.push(length);
26541                        }
26542                        if !matches!(self.peek(), Token::RParen) {
26543                            return Err(self.err(format!(
26544                                "expected ')' to close substring(... FROM ... [FOR ...]), got {:?}",
26545                                self.peek()
26546                            )));
26547                        }
26548                        self.advance();
26549                        return Ok(Expr::FunctionCall {
26550                            name: first.to_ascii_lowercase(),
26551                            args,
26552                        });
26553                    }
26554                    // PG `overlay(str PLACING repl FROM n [FOR len])`
26555                    // syntactic form. Desugars to the `overlay(str,
26556                    // repl, n[, len])` comma-list shape the evaluator
26557                    // already implements. `PLACING` is not a reserved
26558                    // token in SPG, so it arrives as a bare Ident.
26559                    if first.eq_ignore_ascii_case("overlay")
26560                        && args.len() == 1
26561                        && matches!(self.peek(), Token::Ident(kw) if kw == "placing")
26562                    {
26563                        self.advance(); // consume PLACING
26564                        args.push(self.parse_expr(0)?); // replacement
26565                        if !matches!(self.peek(), Token::From) {
26566                            return Err(self.err(format!(
26567                                "expected FROM in overlay(... PLACING ... FROM ...), got {:?}",
26568                                self.peek()
26569                            )));
26570                        }
26571                        self.advance();
26572                        args.push(self.parse_expr(0)?); // start position
26573                        if matches!(self.peek(), Token::For) {
26574                            self.advance();
26575                            args.push(self.parse_expr(0)?); // length
26576                        }
26577                        if !matches!(self.peek(), Token::RParen) {
26578                            return Err(self.err(format!(
26579                                "expected ')' to close overlay(... PLACING ... FROM ... [FOR ...]), got {:?}",
26580                                self.peek()
26581                            )));
26582                        }
26583                        self.advance();
26584                        return Ok(Expr::FunctionCall {
26585                            name: String::from("overlay"),
26586                            args,
26587                        });
26588                    }
26589                    // `TRIM(chars FROM str)` — the keyword-less
26590                    // spelling lands here after the chars parse
26591                    // (the keyword forms return earlier).
26592                    if first.eq_ignore_ascii_case("trim")
26593                        && args.len() == 1
26594                        && matches!(self.peek(), Token::From)
26595                    {
26596                        self.advance();
26597                        let target = self.parse_expr(0)?;
26598                        if !matches!(self.peek(), Token::RParen) {
26599                            return Err(self.err(format!(
26600                                "expected ')' to close TRIM(chars FROM str), got {:?}",
26601                                self.peek()
26602                            )));
26603                        }
26604                        self.advance();
26605                        let chars = args.pop().expect("one arg");
26606                        return Ok(Expr::FunctionCall {
26607                            name: String::from("btrim"),
26608                            args: alloc::vec![target, chars],
26609                        });
26610                    }
26611                    // v7.24 (round-16 A) — aggregate-internal
26612                    // ordering: `array_agg(x ORDER BY y DESC NULLS
26613                    // LAST)`. Keys close the argument list.
26614                    if matches!(self.peek(), Token::Order) {
26615                        self.advance();
26616                        if !self.peek_is_by() {
26617                            return Err(self.err(format!(
26618                                "expected BY after ORDER in aggregate args, got {:?}",
26619                                self.peek()
26620                            )));
26621                        }
26622                        self.advance();
26623                        loop {
26624                            // v7.39 (round 691) — save/restore, the discipline this parser
26625                            // already uses around `pending_sample_preds`, so a subquery inside
26626                            // a key neither inherits nor leaks the channel.
26627                            let saved_flag = core::mem::replace(&mut self.in_order_by_key, true);
26628                            let saved_coll = self.order_key_collation.take();
26629                            let parsed = self.parse_expr(0);
26630                            self.in_order_by_key = saved_flag;
26631                            let collation =
26632                                core::mem::replace(&mut self.order_key_collation, saved_coll);
26633                            let expr = parsed?;
26634                            let desc = if matches!(self.peek(), Token::Desc) {
26635                                self.advance();
26636                                true
26637                            } else if matches!(self.peek(), Token::Asc) {
26638                                self.advance();
26639                                false
26640                            } else {
26641                                false
26642                            };
26643                            let nulls_first = self.parse_optional_nulls_placement()?;
26644                            agg_order_by.push(OrderBy {
26645                                expr,
26646                                desc,
26647                                nulls_first,
26648                                collation,
26649                            });
26650                            if matches!(self.peek(), Token::Comma) {
26651                                self.advance();
26652                            } else {
26653                                break;
26654                            }
26655                        }
26656                        // v7.39 (round 354, M12) — `SEPARATOR '<s>'` may
26657                        // follow the ORDER BY inside GROUP_CONCAT.
26658                        if self.consume_group_concat_separator(&mut args)? {
26659                            saw_separator = true;
26660                        }
26661                        if !matches!(self.peek(), Token::RParen) {
26662                            return Err(self.err(format!(
26663                                "expected ')' after aggregate ORDER BY, got {:?}",
26664                                self.peek()
26665                            )));
26666                        }
26667                        break;
26668                    }
26669                    // v7.39 (round 354, M12) — …or directly after the
26670                    // arguments (`GROUP_CONCAT(t SEPARATOR '|')`). MySQL's
26671                    // own spelling of what PG passes as string_agg's second
26672                    // argument; it was a parse error, so every MySQL query
26673                    // that names its own separator failed outright.
26674                    if self.consume_group_concat_separator(&mut args)? {
26675                        saw_separator = true;
26676                        break;
26677                    }
26678                    match self.peek() {
26679                        Token::Comma => {
26680                            self.advance();
26681                        }
26682                        Token::RParen => break,
26683                        other => {
26684                            return Err(self.err(format!(
26685                                "expected ',' or ')' in function args, got {other:?}"
26686                            )));
26687                        }
26688                    }
26689                }
26690            }
26691            // v7.39 (round 354, M12) — MySQL's GROUP_CONCAT concatenates
26692            // its value arguments PER ROW: `GROUP_CONCAT(n, ':', t)` is
26693            // `3:c,1:a,…` (measured on MariaDB 11), NOT a second argument
26694            // meaning a separator — that is what the explicit SEPARATOR
26695            // tail is for. Fold them into one `concat(...)` so the
26696            // aggregate keeps its single value argument.
26697            if self.mysql_dialect && first.eq_ignore_ascii_case("group_concat") {
26698                let values = args.len() - usize::from(saw_separator);
26699                if values > 1 {
26700                    let sep_arg = if saw_separator { args.pop() } else { None };
26701                    let folded = Expr::FunctionCall {
26702                        name: "concat".to_string(),
26703                        args: core::mem::take(&mut args),
26704                    };
26705                    args.push(folded);
26706                    if let Some(sep) = sep_arg {
26707                        args.push(sep);
26708                    }
26709                }
26710            }
26711            self.advance(); // consume ')'
26712            // v7.39 (read01 round 77) — named arguments are NOT reordered here
26713            // any more. The parser has no catalog, so it could only ever resolve
26714            // the handful of `make_*` builtins whose parameter names were baked
26715            // into a table right here — every user function got
26716            // "does not support named arguments", though the catalog has been
26717            // storing its parameter names all along. Reordering happens in eval,
26718            // in one place, for builtins and user functions alike.
26719            // v7.32 (round-29) — ordered-set aggregate tail
26720            // `name(direct_args) WITHIN GROUP (ORDER BY …)`
26721            // (percentile_cont / percentile_disc / mode). The sort spec
26722            // lands in the same `order_by` slot a decorated aggregate
26723            // uses; the executor dispatches on the function name. WITHIN
26724            // GROUP and an intra-argument ORDER BY are mutually
26725            // exclusive (PG rejects both).
26726            let within_group_order = self.parse_within_group_clause()?;
26727            if !within_group_order.is_empty() && !agg_order_by.is_empty() {
26728                return Err(self.err(
26729                    "an aggregate may not carry both an in-argument ORDER BY and WITHIN GROUP"
26730                        .into(),
26731                ));
26732            }
26733            let within_group_seen = !within_group_order.is_empty();
26734            let agg_order_by = if within_group_order.is_empty() {
26735                agg_order_by
26736            } else {
26737                within_group_order
26738            };
26739            // v7.32 (round-29) — `name(args) FILTER (WHERE …)`.
26740            let filter = self.parse_filter_clause()?;
26741            // v4.12: window-function tail — `name(args) OVER (...)`.
26742            // Promotes the just-parsed FunctionCall into a
26743            // WindowFunction node carrying partition + order.
26744            // v6.4.2: also accepts `name(args) IGNORE NULLS OVER (...)`
26745            // / `RESPECT NULLS OVER (...)` between the closing paren
26746            // and `OVER`.
26747            let null_treatment = self.parse_null_treatment_modifier();
26748            if let Token::Ident(s) | Token::QuotedIdent(s) = self.peek()
26749                && s.eq_ignore_ascii_case("over")
26750            {
26751                self.advance();
26752                // v7.39 (round 230) — PG implements neither modifier for a
26753                // windowed call and says so (0A000). Both used to be parsed
26754                // and then silently dropped here, so `count(DISTINCT v)
26755                // OVER (…)` quietly answered the non-distinct count.
26756                if agg_distinct {
26757                    return Err(
26758                        self.err("DISTINCT is not implemented for window functions".to_string())
26759                    );
26760                }
26761                if !agg_order_by.is_empty() {
26762                    // PG separates the two shapes that land here: a
26763                    // WITHIN GROUP call is an ordered-set aggregate and gets
26764                    // its own message naming the aggregate; a plain
26765                    // `agg(x ORDER BY y)` gets the generic one.
26766                    let msg = if within_group_seen {
26767                        alloc::format!("OVER is not supported for ordered-set aggregate {first}")
26768                    } else {
26769                        "aggregate ORDER BY is not implemented for window functions".to_string()
26770                    };
26771                    return Err(self.err(msg));
26772                }
26773                let (partition_by, order_by, frame) = self.parse_over_clause()?;
26774                return Ok(Expr::WindowFunction {
26775                    name: first,
26776                    args,
26777                    partition_by,
26778                    order_by,
26779                    frame,
26780                    null_treatment,
26781                    filter,
26782                });
26783            }
26784            if !agg_order_by.is_empty() || agg_distinct || filter.is_some() {
26785                return Ok(Expr::AggregateOrdered {
26786                    call: Box::new(Expr::FunctionCall { name: first, args }),
26787                    order_by: agg_order_by,
26788                    distinct: agg_distinct,
26789                    filter,
26790                });
26791            }
26792            // v7.39 (round 522) — PG declares `date_add` / `date_subtract`
26793            // over TIMESTAMPTZ and has no timestamp overload, so a
26794            // timestamp argument is coerced on the way in and the answer
26795            // is timestamptz — measured: `pg_typeof(date_add(TIMESTAMP
26796            // '2020-01-01', INTERVAL '1 hour'))` is `timestamp with time
26797            // zone`. SPG answered `timestamp without time zone`, dropping
26798            // the offset from every rendering.
26799            //
26800            // Writing the coercion PG performs makes the existing
26801            // argument-driven typing (the one `date_trunc` uses) reach the
26802            // right answer, rather than teaching the type layer a second
26803            // rule. MySQL's DATE_ADD is a different function that returns
26804            // DATE or DATETIME, so this is PG-dialect only.
26805            //
26806            // Out-of-line because this sits on the RECURSIVE descent
26807            // frame: an inline block with locals here costs every nesting
26808            // level, and the suite's deep-nesting sentinel overflowed the
26809            // 512 KiB parser stack the moment one was added (round 430's
26810            // lesson, in the same shape).
26811            if !self.mysql_dialect {
26812                lift_date_add_arg_to_timestamptz(&first, &mut args);
26813            }
26814            return Ok(Expr::FunctionCall { name: first, args });
26815        }
26816        // v7.9.20 — SQL-standard parenless keyword expressions
26817        // (PG treats these as functions called without parens).
26818        // Resolve to a synthetic FunctionCall so the engine's
26819        // eval path reuses the existing function-call routing.
26820        // mailrs G3.
26821        let lc = first.to_ascii_lowercase();
26822        if matches!(
26823            lc.as_str(),
26824            "current_date"
26825                | "current_time"
26826                | "current_timestamp"
26827                | "localtimestamp"
26828                | "localtime"
26829                // v7.37.17 (17.6 siblings) — session-identity SQL-
26830                // standard parenless keywords. current_user /
26831                // session_user / user were already caught by the
26832                // pgwire canned-response shortcut but bare-select
26833                // in the embedded engine went through Expr::Column
26834                // and errored. Adding them here so the parser
26835                // resolves to a synthetic FunctionCall that reuses
26836                // the existing eval/functions.rs dispatch.
26837                | "current_user"
26838                | "session_user"
26839                | "current_role"
26840                | "current_catalog"
26841                | "current_schema"
26842                | "current_database"
26843                // v7.39 (read01 round 51) — PG 16's system_user is parenless too.
26844                | "system_user"
26845        ) {
26846            return Ok(Expr::FunctionCall {
26847                name: lc,
26848                args: Vec::new(),
26849            });
26850        }
26851        Ok(Expr::Column(ColumnName {
26852            qualifier: None,
26853            name: first,
26854        }))
26855    }
26856}
26857
26858/// v7.39 (round 522) — write the coercion PG's `date_add` /
26859/// `date_subtract` signature performs.
26860///
26861/// PG declares both over TIMESTAMPTZ and has no timestamp overload, so a
26862/// timestamp argument is cast on the way in and the answer is
26863/// timestamptz — measured: `pg_typeof(date_add(TIMESTAMP '2020-01-01',
26864/// INTERVAL '1 hour'))` is `timestamp with time zone`. SPG answered
26865/// `timestamp without time zone`, dropping the offset from every
26866/// rendering of the result.
26867///
26868/// Writing the cast the signature implies lets the existing
26869/// argument-driven typing (the one `date_trunc` uses) reach the right
26870/// answer instead of teaching the type layer a second rule. MySQL's
26871/// DATE_ADD is a different function returning DATE or DATETIME, so the
26872/// caller applies this in PG dialect only.
26873///
26874/// A free function, and not a block at the call site, because the caller
26875/// is on the recursive-descent frame chain.
26876#[inline(never)]
26877fn lift_date_add_arg_to_timestamptz(name: &str, args: &mut alloc::vec::Vec<Expr>) {
26878    if args.len() != 2
26879        || !(name.eq_ignore_ascii_case("date_add") || name.eq_ignore_ascii_case("date_subtract"))
26880    {
26881        return;
26882    }
26883    let base = args.remove(0);
26884    args.insert(
26885        0,
26886        Expr::Cast {
26887            expr: Box::new(base),
26888            target: CastTarget::Timestamptz,
26889        },
26890    );
26891}
26892
26893/// v6.8.2 — walk an expression tree and return the first column
26894/// reference's bare name. Used by `parse_create_index_stmt_after_create`
26895/// to derive `CreateIndexStatement.column` from an expression
26896/// key (so downstream planner code resolving a primary column
26897/// position keeps working with expression indexes). Returns
26898/// `None` when the expression has no column ref at all — caller
26899/// surfaces that as a parse error.
26900fn extract_first_column(expr: &Expr) -> Option<String> {
26901    match expr {
26902        Expr::Column(cn) => Some(cn.name.clone()),
26903        Expr::FunctionCall { args, .. } => args.iter().find_map(extract_first_column),
26904        Expr::Binary { lhs, rhs, .. } => {
26905            extract_first_column(lhs).or_else(|| extract_first_column(rhs))
26906        }
26907        Expr::Unary { expr: e, .. } => extract_first_column(e),
26908        // v7.39 (read01 round 93) — a cast wraps its operand: a common
26909        // expression-index key is `lower(col::text)`, where the column
26910        // sits under the `::text` cast inside the function arg. Without
26911        // descending here the key was rejected as "references no column".
26912        Expr::Cast { expr: e, .. } => extract_first_column(e),
26913        // v7.39.2 — and a COLLATE wraps its operand the same way.
26914        // `CREATE INDEX rc ON t (c COLLATE "C" DESC)` stopped naming a
26915        // column the moment the clause became a node instead of being
26916        // absorbed, and the key was rejected as referencing none. This
26917        // is the shape the wildcard below silently produces, which is
26918        // why it is spelled out.
26919        Expr::Collate { expr: e, .. } => extract_first_column(e),
26920        _ => None,
26921    }
26922}
26923
26924fn maybe_not(expr: Expr, negated: bool) -> Expr {
26925    if negated {
26926        Expr::Unary {
26927            op: UnOp::Not,
26928            expr: Box::new(expr),
26929        }
26930    } else {
26931        expr
26932    }
26933}
26934
26935/// v7.39 (round 353, M9/M10) — three operator TOKENS mean different
26936/// things in the two dialects, and SPG read all three PG's way:
26937///
26938/// | token | PG (and SPG) | MySQL, measured |
26939/// |---|---|---|
26940/// | `\|\|` | string concatenation | **OR** — `1 \|\| 0` is 1, not '10' |
26941/// | `&&` | inet / array overlap | **AND** |
26942/// | `<=>` | pgvector cosine distance | **NULL-safe equal** |
26943///
26944/// `1 || 0` answering the string '10' on a MySQL session is a wrong
26945/// answer with no error, which is why they are routed here rather than
26946/// left to the shared table.
26947impl Parser {
26948    fn binop_here(&self, tok: &Token) -> Option<(BinOp, u8)> {
26949        if self.mysql_dialect {
26950            // v7.39 (round 353, M9) — `DIV` is MySQL's truncating integer
26951            // division (`5 DIV 2` is 2, `-7 DIV 2` is -3 — toward zero —
26952            // and `5 DIV 0` is NULL). It is a plain ident to the lexer.
26953            if let Token::Ident(w) = tok
26954                && w.eq_ignore_ascii_case("div")
26955            {
26956                return Some((BinOp::IntDiv, 8));
26957            }
26958            // v7.39 (round 394) — `MOD` is MySQL's modulo operator, a synonym
26959            // for `%` (`10 MOD 3` is 1, `5.5 MOD 2` is 1.5). A plain ident to
26960            // the lexer; the `MOD(x, y)` function form is unaffected (MOD
26961            // there sits in operand position, not infix).
26962            if let Token::Ident(w) = tok
26963                && w.eq_ignore_ascii_case("mod")
26964            {
26965                return Some((BinOp::Mod, 8));
26966            }
26967            // v7.39 (round 407) — `XOR` is MySQL's logical exclusive-or, a
26968            // plain ident to the lexer. Its precedence sits between OR (1)
26969            // and AND (3) — hence rung 2, the slot freed by moving AND up.
26970            if let Token::Ident(w) = tok
26971                && w.eq_ignore_ascii_case("xor")
26972            {
26973                return Some((BinOp::LogicalXor, 2));
26974            }
26975            match tok {
26976                Token::Concat => return Some((BinOp::Or, 1)),
26977                // MySQL's `&&` is logical AND, sharing AND's rung (3).
26978                Token::InetOverlap => return Some((BinOp::And, 3)),
26979                // MySQL's `<=>` is NULL-safe equal, at the comparison rung (5).
26980                Token::CosineDistance => return Some((BinOp::IsNotDistinctFrom, 5)),
26981                _ => {}
26982            }
26983        }
26984        binop_from(tok)
26985    }
26986}
26987
26988// v7.39 (round 407) — precedence ladder. To open a rung for MySQL's `XOR`
26989// (which sits strictly between OR and AND), every level from AND upward was
26990// shifted +1: the ladder is now OR=1, XOR=2, AND=3, IS=4, comparison=5,
26991// distance=6, additive/concat/bitwise=7, multiplicative/JSON=8, prefix=9.
26992// XOR only exists in the MySQL dialect (binop_here); PG never sees it, and
26993// the *relative* order of every PG operator is unchanged by the shift.
26994fn binop_from(tok: &Token) -> Option<(BinOp, u8)> {
26995    let pair = match tok {
26996        Token::Or => (BinOp::Or, 1),
26997        Token::And => (BinOp::And, 3),
26998        Token::Eq => (BinOp::Eq, 5),
26999        Token::NotEq => (BinOp::NotEq, 5),
27000        Token::Lt => (BinOp::Lt, 5),
27001        Token::LtEq => (BinOp::LtEq, 5),
27002        Token::Gt => (BinOp::Gt, 5),
27003        Token::GtEq => (BinOp::GtEq, 5),
27004        // pgvector distance ops all sit on the same rung — tighter than
27005        // comparisons (5) so `col <-> v < threshold` parses correctly.
27006        Token::L2Distance => (BinOp::L2Distance, 6),
27007        // v7.39 (read01 geo_ops.c) — geometric predicates ride the
27008        // comparison rung.
27009        Token::GeomParallel => (BinOp::GeomParallel, 5),
27010        // v7.39 (read01 rangetypes.c) — range `&<` / `&>` on the
27011        // comparison rung.
27012        Token::OverLeft => (BinOp::OverLeft, 5),
27013        Token::OverRight => (BinOp::OverRight, 5),
27014        Token::GeomPerp => (BinOp::GeomPerp, 5),
27015        Token::GeomSameAs => (BinOp::GeomSameAs, 5),
27016        Token::ClosestPoint => (BinOp::ClosestPoint, 6),
27017        Token::GeomHoriz => (BinOp::GeomHoriz, 5),
27018        Token::InnerProduct => (BinOp::InnerProduct, 6),
27019        Token::CosineDistance => (BinOp::CosineDistance, 6),
27020        Token::Plus => (BinOp::Add, 7),
27021        Token::Minus => (BinOp::Sub, 7),
27022        // v7.39 (round 760, F31-B1) — the generic-operator rung. PG
27023        // binds every "other" operator (`||`, `|`, `&`, `#`, the
27024        // pgvector distances above) BETWEEN additive (7) and the
27025        // comparisons (5): `'a' || 1 + 1` is `'a' || 2` → `a2`,
27026        // `a & b + 1` is `a & (b + 1)`, and `flags & $1 = 0` stays
27027        // `(flags & $1) = 0`. They shared rung 7 with `+ -` since v1
27028        // ("matches PG conceptually" — the round-753 audit measured it
27029        // false; the old rung errored on `'a' || 1 + 1` with
27030        // `text + integer`). Same-level chains left-fold, as PG does.
27031        Token::Concat => (BinOp::Concat, 6),
27032        Token::Pipe => (BinOp::BitOr, 6),
27033        Token::Amp => (BinOp::BitAnd, 6),
27034        Token::Star => (BinOp::Mul, 8),
27035        Token::Slash => (BinOp::Div, 8),
27036        Token::Percent => (BinOp::Mod, 8),
27037        // v4.14: JSON path ops bind tighter than comparisons (5)
27038        // and additive (7) so `doc->'k' = 'v'` parses correctly.
27039        // Same rung as the multiplicative ops.
27040        Token::JsonGet => (BinOp::JsonGet, 8),
27041        Token::JsonGetText => (BinOp::JsonGetText, 8),
27042        Token::JsonGetPath => (BinOp::JsonGetPath, 8),
27043        Token::JsonGetPathText => (BinOp::JsonGetPathText, 8),
27044        Token::JsonContains => (BinOp::JsonContains, 8),
27045        Token::JsonPathExists => (BinOp::JsonPathExists, 8),
27046        Token::JsonContainedBy => (BinOp::JsonContainedBy, 8),
27047        Token::JsonKeyExists => (BinOp::JsonKeyExists, 8),
27048        Token::JsonKeysAny => (BinOp::JsonKeysAny, 8),
27049        Token::JsonKeysAll => (BinOp::JsonKeysAll, 8),
27050        Token::JsonDeletePath => (BinOp::JsonDeletePath, 8),
27051        // v7.12.2 — `@@` binds at the comparison rung (looser than
27052        // arithmetic, tighter than AND / OR). PG places `@@` at
27053        // the same precedence as `=` / `<`, so we follow.
27054        Token::TsMatch => (BinOp::TsMatch, 5),
27055        // v7.17.0 Phase 3.P0-47 — PG INET / CIDR containment + overlap.
27056        // PG places these at the comparison rung (same level as `=`),
27057        // so we follow.
27058        Token::InetContainedBy => (BinOp::InetContainedBy, 5),
27059        Token::InetContainedByEq => (BinOp::InetContainedByEq, 5),
27060        Token::InetContains => (BinOp::InetContains, 5),
27061        Token::InetContainsEq => (BinOp::InetContainsEq, 5),
27062        Token::InetOverlap => (BinOp::InetOverlap, 5),
27063        // v7.39 (round 508) — the geometric and pattern-order predicates
27064        // ride the comparison rung, as every other predicate does.
27065        Token::Intersects => (BinOp::Intersects, 5),
27066        Token::IsBelow => (BinOp::IsBelow, 5),
27067        Token::IsAbove => (BinOp::IsAbove, 5),
27068        Token::PatternLt => (BinOp::PatternLt, 5),
27069        Token::PatternLtEq => (BinOp::PatternLtEq, 5),
27070        Token::PatternGt => (BinOp::PatternGt, 5),
27071        Token::PatternGtEq => (BinOp::PatternGtEq, 5),
27072        // `@@@` is the old spelling of `@@` and means exactly it.
27073        Token::TsMatchOld => (BinOp::TsMatch, 5),
27074        _ => return None,
27075    };
27076    Some(pair)
27077}
27078
27079#[allow(clippy::cast_possible_truncation, clippy::cast_precision_loss)]
27080// `as f32` here is intentional: vector elements widen / narrow into f32 on
27081// purpose. i64 → f32 loses precision past 2^24, f64 → f32 loses precision
27082// past ~15 decimal digits — both are acceptable for a fixed-precision
27083// pgvector column.
27084/// v7.17.0 Phase 1.3 — words that would otherwise be eaten as an
27085/// implicit table alias and break trailing clauses. WITH lands
27086/// here so `… FROM t WITH NO DATA` doesn't consume WITH as the
27087/// alias for `t`; same for ON / WHERE / HAVING / GROUP / ORDER /
27088/// LIMIT / OFFSET / UNION / EXCEPT / INTERSECT / RETURNING / SET
27089/// / VALUES / FOR / LATERAL — all of which would otherwise be
27090/// silently swallowed by `parse_optional_alias`.
27091fn is_alias_stopword(s: &str) -> bool {
27092    matches!(
27093        s.to_ascii_lowercase().as_str(),
27094        "with"
27095            | "on"
27096            | "where"
27097            | "having"
27098            | "group"
27099            | "order"
27100            | "limit"
27101            | "offset"
27102            | "union"
27103            | "except"
27104            | "intersect"
27105            | "returning"
27106            | "set"
27107            | "values"
27108            | "for"
27109            | "window"
27110            | "tablesample"
27111            | "lateral"
27112            | "left"
27113            | "right"
27114            | "inner"
27115            | "outer"
27116            | "full"
27117            | "cross"
27118            | "join"
27119            | "natural"
27120            | "using"
27121            | "fetch"
27122    )
27123}
27124
27125fn extract_numeric_literal(e: &Expr) -> Option<f32> {
27126    match e {
27127        Expr::Literal(Literal::Integer(n)) => Some(*n as f32),
27128        Expr::Literal(Literal::Float(x)) => Some(*x as f32),
27129        // v7.38 (read01) — a dotted literal is now NUMERIC, so a vector element
27130        // like `2.5` arrives as Literal::Numeric; widen it into f32. (`no_std`,
27131        // so scale the divisor by hand instead of `f32::powi`.)
27132        Expr::Literal(Literal::Numeric { unscaled, scale }) => {
27133            let mut div = 1.0f32;
27134            for _ in 0..*scale {
27135                div *= 10.0;
27136            }
27137            Some(*unscaled as f32 / div)
27138        }
27139        Expr::Unary {
27140            op: UnOp::Neg,
27141            expr,
27142        } => extract_numeric_literal(expr).map(|x| -x),
27143        _ => None,
27144    }
27145}
27146
27147/// Parse the text inside `INTERVAL '...'` into `(months, micros)`. Accepts
27148/// one or more `<n> <unit>` pairs separated by whitespace. `<n>` may be
27149/// negative. Returns `None` if any pair fails to parse or no pair is found.
27150///
27151/// Recognised units (case-insensitive, optional trailing `s`):
27152/// `microsecond`, `millisecond`, `second`, `minute`, `hour`, `day`, `week`,
27153/// `month`, `year`. `week` widens to 7 days; `year` widens to 12 months.
27154/// v7.37.5 β — returns `(months, days, micros)`. `days` is preserved
27155/// as its own dimension so `INTERVAL '1 day'` ≠ `INTERVAL '24 hours'`
27156/// (PG-canonical: DST and month-boundary semantics depend on this).
27157/// `week` rolls into `days` (× 7). Sub-day units flow into `micros`.
27158/// ISO 8601 duration input for INTERVAL: `P1Y2M3DT4H5M6S`. Before the `T`,
27159/// `M` is months; after it, `M` is minutes. Returns `(months, days, micros)`.
27160#[allow(clippy::cast_possible_truncation)]
27161fn parse_iso8601_interval(rest: &str) -> Option<(i32, i32, i64)> {
27162    let mut months: i64 = 0;
27163    let mut days: i64 = 0;
27164    let mut micros: i64 = 0;
27165    let mut in_time = false;
27166    let mut num = alloc::string::String::new();
27167    for ch in rest.chars() {
27168        if ch.is_ascii_digit() || ch == '.' || ch == '-' || ch == '+' {
27169            num.push(ch);
27170            continue;
27171        }
27172        if ch == 'T' || ch == 't' {
27173            if !num.is_empty() {
27174                return None;
27175            }
27176            in_time = true;
27177            continue;
27178        }
27179        let n: f64 = num.parse().ok()?;
27180        num.clear();
27181        match (ch, in_time) {
27182            ('Y' | 'y', false) => months += (n * 12.0) as i64,
27183            ('M', false) => months += n as i64,
27184            ('W' | 'w', false) => days += (n * 7.0) as i64,
27185            ('D' | 'd', false) => days += n as i64,
27186            ('H' | 'h', true) => micros += (n * 3_600_000_000.0) as i64,
27187            ('M', true) => micros += (n * 60_000_000.0) as i64,
27188            ('S' | 's', true) => micros += (n * 1_000_000.0) as i64,
27189            _ => return None,
27190        }
27191    }
27192    if !num.is_empty() {
27193        return None;
27194    }
27195    Some((
27196        i32::try_from(months).ok()?,
27197        i32::try_from(days).ok()?,
27198        micros,
27199    ))
27200}
27201
27202/// PG year-month shorthand for INTERVAL: `1-2` = 1 year 2 mons (an optional
27203/// leading `-` negates the whole value). Rejects date-like strings.
27204fn parse_year_month_interval(s: &str) -> Option<(i32, i32, i64)> {
27205    let (neg, body) = match s.strip_prefix('-') {
27206        Some(b) => (true, b),
27207        None => (false, s),
27208    };
27209    let (y, m) = body.split_once('-')?;
27210    let years: i32 = y.parse().ok()?;
27211    let mons: i32 = m.parse().ok()?;
27212    if years < 0 || mons < 0 {
27213        return None;
27214    }
27215    let total = years.checked_mul(12)?.checked_add(mons)?;
27216    Some((if neg { -total } else { total }, 0, 0))
27217}
27218
27219/// Parse a clock-time interval token `HH:MM[:SS[.ffffff]]` (optionally signed)
27220/// into microseconds. Used for the `3 days 14:30:45` / bare `14:30:45` forms.
27221fn parse_interval_clock(tok: &str) -> Option<i64> {
27222    let (neg, body) = match tok.strip_prefix('-') {
27223        Some(r) => (true, r),
27224        None => (false, tok.strip_prefix('+').unwrap_or(tok)),
27225    };
27226    let mut it = body.split(':');
27227    let h: i64 = it.next()?.parse().ok()?;
27228    let m: i64 = it.next()?.parse().ok()?;
27229    let s_tok = it.next().unwrap_or("0");
27230    if it.next().is_some() {
27231        return None;
27232    }
27233    let sec_us: i64 = if let Some((sec, frac)) = s_tok.split_once('.') {
27234        let sec: i64 = sec.parse().ok()?;
27235        let mut f = alloc::string::String::from(frac);
27236        while f.len() < 6 {
27237            f.push('0');
27238        }
27239        f.truncate(6);
27240        let fus: i64 = f.parse().ok()?;
27241        sec.checked_mul(1_000_000)?.checked_add(fus)?
27242    } else {
27243        s_tok.parse::<i64>().ok()?.checked_mul(1_000_000)?
27244    };
27245    let total = h
27246        .checked_mul(3_600_000_000)?
27247        .checked_add(m.checked_mul(60_000_000)?)?
27248        .checked_add(sec_us)?;
27249    Some(if neg { -total } else { total })
27250}
27251
27252/// v7.39 (read01 round 77) — one canonical name per interval unit, covering
27253/// every spelling PG accepts (measured against live PG18.4, not guessed):
27254/// `min` / `mins` / `m` are minutes, `mon` / `mons` are months, `y` is years.
27255/// Before this, the unit table matched long names only, with an ad-hoc
27256/// `strip_suffix('s')` in front of it — so `'15 min'` (and `hrs`, `secs`,
27257/// `yrs`, every abbreviation anyone actually types) was "cannot parse as
27258/// INTERVAL", and it had also grown arms for the debris that stripping leaves
27259/// behind (`centurie`, `millenniu`). Two parallel unit matches (integer and
27260/// fractional) both read from this one table now.
27261fn canonical_interval_unit(raw: &str) -> Option<&'static str> {
27262    let u = raw.to_ascii_lowercase();
27263    Some(match u.as_str() {
27264        "microsecond" | "microseconds" | "us" | "usec" | "usecs" | "usecond" | "useconds" => {
27265            "microsecond"
27266        }
27267        "millisecond" | "milliseconds" | "ms" | "msec" | "msecs" | "msecond" | "mseconds" => {
27268            "millisecond"
27269        }
27270        "second" | "seconds" | "sec" | "secs" | "s" => "second",
27271        "minute" | "minutes" | "min" | "mins" | "m" => "minute",
27272        "hour" | "hours" | "hr" | "hrs" | "h" => "hour",
27273        "day" | "days" | "d" => "day",
27274        "week" | "weeks" | "w" => "week",
27275        "month" | "months" | "mon" | "mons" => "month",
27276        "year" | "years" | "yr" | "yrs" | "y" => "year",
27277        "decade" | "decades" | "dec" | "decs" => "decade",
27278        "century" | "centuries" | "cent" | "c" => "century",
27279        "millennium" | "millenniums" | "millennia" | "mil" | "mils" => "millennium",
27280        _ => return None,
27281    })
27282}
27283
27284/// v7.39 (read01 round 102) — the six SQL-standard interval fields that can
27285/// qualify an `INTERVAL '…' <FIELD> [TO <FIELD>]` literal.
27286#[derive(Debug, Clone, Copy, PartialEq, Eq)]
27287pub(crate) enum IntervalField {
27288    Year,
27289    Month,
27290    Day,
27291    Hour,
27292    Minute,
27293    Second,
27294}
27295
27296/// Recognise an interval field keyword (bare ident, case-insensitive). Plural
27297/// spellings aren't standard for the qualifier position, so only the singular
27298/// forms are accepted.
27299/// v7.39 (round 350, M7) — MySQL's interval units, measured against
27300/// MariaDB 11. QUARTER is three months and WEEK seven days; MICROSECOND
27301/// is the finest. (The compound spellings — `DAY_HOUR` and friends, which
27302/// take a `'1 2'` style literal — are not read here; they stay a parse
27303/// error rather than being silently misread.)
27304/// v7.39 (round 430) — lower a `@name` / `@@name` reference to its call.
27305///
27306/// ONE `@` is a MySQL USER variable: its own per-session namespace, nothing
27307/// to do with a `@@` engine setting, and an unset one reads NULL rather
27308/// than raising. (The parser used to strip every `@`, so `@x` and `@@x`
27309/// were the same node and `SELECT @x` answered "Unknown system variable".)
27310/// For `@@`, the `session.` / `global.` scope is KEPT: a global read must
27311/// not see a session override — measured, after `SET autocommit=0`,
27312/// `@@global.autocommit` is still 1.
27313///
27314/// Out-of-line and NOT a method: `parse_atom` is the giant recursive frame
27315/// the parser's nesting budget is tuned against, and building these
27316/// `String` + `Vec` locals inside it overflowed the guard's stack (the same
27317/// wall `parse_left_right_atom` and friends were factored out for).
27318#[inline(never)]
27319fn variable_ref_atom(raw: &str) -> Expr {
27320    let user_var = !raw.starts_with("@@");
27321    let bare = raw.trim_start_matches('@').to_ascii_lowercase();
27322    Expr::FunctionCall {
27323        name: String::from(if user_var {
27324            "__spg_user_var"
27325        } else {
27326            "__spg_session_var"
27327        }),
27328        args: alloc::vec![Expr::Literal(Literal::String(bare))],
27329    }
27330}
27331
27332fn mysql_interval_unit(tok: &Token) -> Option<&'static str> {
27333    let Token::Ident(s) = tok else { return None };
27334    Some(match () {
27335        () if s.eq_ignore_ascii_case("microsecond") => "microsecond",
27336        () if s.eq_ignore_ascii_case("second") => "second",
27337        () if s.eq_ignore_ascii_case("minute") => "minute",
27338        () if s.eq_ignore_ascii_case("hour") => "hour",
27339        () if s.eq_ignore_ascii_case("day") => "day",
27340        () if s.eq_ignore_ascii_case("week") => "week",
27341        () if s.eq_ignore_ascii_case("month") => "month",
27342        () if s.eq_ignore_ascii_case("quarter") => "quarter",
27343        () if s.eq_ignore_ascii_case("year") => "year",
27344        () => return None,
27345    })
27346}
27347
27348/// v7.39 (round 422) — lower `INTERVAL <expr> <unit>` onto the existing
27349/// `make_interval(years, months, weeks, days, hours, mins, secs)` builtin,
27350/// which constructs the value at run time. Only the slot the unit names
27351/// carries the quantity; QUARTER and MICROSECOND scale it into the nearest
27352/// slot the builtin has (months and fractional seconds respectively).
27353fn make_interval_call(qty: Expr, unit: &str) -> Expr {
27354    let zero = || Expr::Literal(Literal::Integer(0));
27355    let scaled = |op: crate::ast::BinOp, by: Expr| Expr::Binary {
27356        lhs: alloc::boxed::Box::new(qty.clone()),
27357        op,
27358        rhs: alloc::boxed::Box::new(by),
27359    };
27360    // (years, months, weeks, days, hours, mins, secs)
27361    let mut args = alloc::vec![zero(), zero(), zero(), zero(), zero(), zero(), zero()];
27362    match unit {
27363        "year" => args[0] = qty,
27364        "quarter" => {
27365            args[1] = scaled(crate::ast::BinOp::Mul, Expr::Literal(Literal::Integer(3)));
27366        }
27367        "month" => args[1] = qty,
27368        "week" => args[2] = qty,
27369        "day" => args[3] = qty,
27370        "hour" => args[4] = qty,
27371        "minute" => args[5] = qty,
27372        "second" => args[6] = qty,
27373        // The builtin's seconds slot takes a fraction, so microseconds ride
27374        // it scaled down; the divisor is a NUMERIC literal so the division
27375        // stays exact rather than going through a float.
27376        "microsecond" => {
27377            args[6] = scaled(
27378                crate::ast::BinOp::Div,
27379                Expr::Literal(Literal::Numeric {
27380                    unscaled: 1_000_000,
27381                    scale: 0,
27382                }),
27383            );
27384        }
27385        _ => args[3] = qty,
27386    }
27387    Expr::FunctionCall {
27388        name: alloc::string::String::from("make_interval"),
27389        args,
27390    }
27391}
27392
27393/// `(count, unit)` → `(months, days, micros)`.
27394fn scale_mysql_interval(count: &str, unit: &str) -> Option<(i32, i32, i64)> {
27395    let n: i64 = count.trim().parse().ok()?;
27396    Some(match unit {
27397        "microsecond" => (0, 0, n),
27398        "second" => (0, 0, n.checked_mul(1_000_000)?),
27399        "minute" => (0, 0, n.checked_mul(60_000_000)?),
27400        "hour" => (0, 0, n.checked_mul(3_600_000_000)?),
27401        "day" => (0, i32::try_from(n).ok()?, 0),
27402        "week" => (0, i32::try_from(n.checked_mul(7)?).ok()?, 0),
27403        "month" => (i32::try_from(n).ok()?, 0, 0),
27404        "quarter" => (i32::try_from(n.checked_mul(3)?).ok()?, 0, 0),
27405        "year" => (i32::try_from(n.checked_mul(12)?).ok()?, 0, 0),
27406        _ => return None,
27407    })
27408}
27409
27410fn interval_field_of(tok: &Token) -> Option<IntervalField> {
27411    let Token::Ident(s) = tok else { return None };
27412    Some(match () {
27413        () if s.eq_ignore_ascii_case("year") => IntervalField::Year,
27414        () if s.eq_ignore_ascii_case("month") => IntervalField::Month,
27415        () if s.eq_ignore_ascii_case("day") => IntervalField::Day,
27416        () if s.eq_ignore_ascii_case("hour") => IntervalField::Hour,
27417        () if s.eq_ignore_ascii_case("minute") => IntervalField::Minute,
27418        () if s.eq_ignore_ascii_case("second") => IntervalField::Second,
27419        () => return None,
27420    })
27421}
27422
27423/// v7.39 (read01 round 102) — interpret an interval literal under a field
27424/// qualifier. Returns `(months, days, micros)`.
27425///
27426/// * A single field applied to a bare number sets which unit the number means,
27427///   truncated to that field's precision (`INTERVAL '1.5' HOUR` → `01:00:00`);
27428///   SECOND keeps its fraction (`'90.5' SECOND` → `00:01:30.5`).
27429/// * `YEAR TO MONTH` reads the `Y-M` form (`'1-6'` → 1 year 6 months).
27430/// * Every other range, and any literal a single field can't read as a plain
27431///   number (`'2 days' DAY`), falls back to the unqualified parse — SPG's
27432///   interval-text parser already reads the `D H:MM:SS` / `H:MM` forms exactly
27433///   like PG, and the qualifier there only bounds precision.
27434fn interpret_qualified_interval(
27435    text: &str,
27436    (f1, f2): (IntervalField, Option<IntervalField>),
27437) -> Option<(i32, i32, i64)> {
27438    if let Some(f2) = f2 {
27439        if f1 == IntervalField::Year && f2 == IntervalField::Month {
27440            if let Some(m) = parse_year_month_literal(text) {
27441                return Some((m, 0, 0));
27442            }
27443        }
27444        return parse_interval_text(text);
27445    }
27446    // Single field: reinterpret a bare number; otherwise the default parse.
27447    let trimmed = text.trim();
27448    if let Ok(val) = trimmed.parse::<f64>() {
27449        // no_std: f64 has no trunc/round; cast toward zero + round-half-away.
27450        #[allow(clippy::cast_possible_truncation)]
27451        let whole = val as i64;
27452        #[allow(clippy::cast_possible_truncation)]
27453        let secs_micros = {
27454            let m = val * 1_000_000.0;
27455            if m >= 0.0 {
27456                (m + 0.5) as i64
27457            } else {
27458                (m - 0.5) as i64
27459            }
27460        };
27461        return Some(match f1 {
27462            IntervalField::Year => (i32::try_from(whole).ok()?.checked_mul(12)?, 0, 0),
27463            IntervalField::Month => (i32::try_from(whole).ok()?, 0, 0),
27464            IntervalField::Day => (0, i32::try_from(whole).ok()?, 0),
27465            IntervalField::Hour => (0, 0, whole.checked_mul(3_600_000_000)?),
27466            IntervalField::Minute => (0, 0, whole.checked_mul(60_000_000)?),
27467            IntervalField::Second => (0, 0, secs_micros),
27468        });
27469    }
27470    parse_interval_text(text)
27471}
27472
27473/// Parse the `Y-M` (optionally signed) year-to-month literal into total months.
27474fn parse_year_month_literal(text: &str) -> Option<i32> {
27475    let t = text.trim();
27476    let (neg, body) = match t.strip_prefix('-') {
27477        Some(r) => (true, r),
27478        None => (false, t.strip_prefix('+').unwrap_or(t)),
27479    };
27480    let mut it = body.split('-');
27481    let years: i32 = it.next()?.trim().parse().ok()?;
27482    let months: i32 = match it.next() {
27483        Some(m) => m.trim().parse().ok()?,
27484        None => 0,
27485    };
27486    if it.next().is_some() {
27487        return None;
27488    }
27489    let total = years.checked_mul(12)?.checked_add(months)?;
27490    Some(if neg { -total } else { total })
27491}
27492
27493pub fn parse_interval_text(s: &str) -> Option<(i32, i32, i64)> {
27494    // v7.38.19 — the two infinities, answered as the three extreme
27495    // fields PostgreSQL itself puts on the wire for them:
27496    //
27497    //   COPY (SELECT 'infinity'::interval) TO STDOUT (FORMAT binary)
27498    //     … 7fffffffffffffff 7fffffff 7fffffff
27499    //
27500    // So no caller has to know the spelling — every one of them already
27501    // reads the three numbers, and `IntervalKind::from_fields` names
27502    // what they mean.
27503    //
27504    // `inf` is NOT one of them, measured: `'inf'::interval` is *invalid
27505    // input syntax* on PostgreSQL 18.4 while `'inf'::float8` is
27506    // infinity. Interval takes the full word, in any case.
27507    {
27508        let word = s.trim();
27509        let word = word.strip_prefix('@').map_or(word, str::trim);
27510        let (neg, body) = match word.strip_prefix('-') {
27511            Some(rest) => (true, rest.trim_start()),
27512            None => (false, word.strip_prefix('+').map_or(word, str::trim_start)),
27513        };
27514        if body.eq_ignore_ascii_case("infinity") {
27515            return Some(if neg {
27516                (i32::MIN, i32::MIN, i64::MIN)
27517            } else {
27518                (i32::MAX, i32::MAX, i64::MAX)
27519            });
27520        }
27521    }
27522    // v7.39 (read01 timestamp.c) — PG's postgres_verbose forms: a leading
27523    // `@` is decorative; a trailing `ago` negates the whole interval.
27524    let mut trimmed = s.trim();
27525    trimmed = trimmed.strip_prefix('@').map_or(trimmed, str::trim);
27526    let mut negate = false;
27527    if let Some(rest) = trimmed
27528        .strip_suffix("ago")
27529        .filter(|r| r.ends_with(char::is_whitespace) || r.is_empty())
27530    {
27531        negate = true;
27532        trimmed = rest.trim();
27533    }
27534    let finish = |v: Option<(i32, i32, i64)>| -> Option<(i32, i32, i64)> {
27535        let (mo, d, us) = v?;
27536        if negate {
27537            Some((mo.checked_neg()?, d.checked_neg()?, us.checked_neg()?))
27538        } else {
27539            Some((mo, d, us))
27540        }
27541    };
27542    let s = trimmed;
27543    // ISO 8601 duration (`P1Y2M3DT4H`) and PG's year-month shorthand (`1-2`)
27544    // are single tokens, not the `<n> <unit>` pair form handled below.
27545    if let Some(rest) = trimmed.strip_prefix(['P', 'p']) {
27546        return finish(parse_iso8601_interval(rest));
27547    }
27548    if !trimmed.contains(char::is_whitespace) && trimmed.contains('-') {
27549        if let Some(iv) = parse_year_month_interval(trimmed) {
27550            return finish(Some(iv));
27551        }
27552    }
27553    // v7.39 (GUC knife 3, differential) — PG accepts a bare number as
27554    // SECONDS: `INTERVAL '0'` = 00:00:00, `INTERVAL '5'` = 00:00:05,
27555    // fractions kept to the microsecond (`'1.5'` = 00:00:01.5).
27556    if !trimmed.is_empty() && !trimmed.contains(char::is_whitespace) {
27557        if let Ok(n) = trimmed.parse::<i64>() {
27558            return finish(Some((0, 0, n.checked_mul(1_000_000)?)));
27559        }
27560        if let Ok(f) = trimmed.parse::<f64>() {
27561            if f.is_finite() {
27562                #[allow(clippy::cast_possible_truncation)]
27563                return finish(Some((0, 0, (f * 1_000_000.0) as i64)));
27564            }
27565        }
27566    }
27567    // v7.39 (round 243) — PG accepts the number and unit run together
27568    // (`'15h 2m 12s'`); split each token at the digit→letter boundary so
27569    // the `<n> <unit>` pair loop below sees them as two.
27570    let raw_parts: Vec<&str> = s.split_whitespace().collect();
27571    let mut parts: Vec<&str> = Vec::with_capacity(raw_parts.len());
27572    for p in raw_parts {
27573        let boundary = p
27574            .char_indices()
27575            .find(|(i, c)| {
27576                *i > 0
27577                    && c.is_ascii_alphabetic()
27578                    && p[..*i]
27579                        .chars()
27580                        .all(|d| d.is_ascii_digit() || matches!(d, '.' | '-' | '+'))
27581                    && p[..*i].chars().any(|d| d.is_ascii_digit())
27582            })
27583            .map(|(i, _)| i);
27584        match boundary {
27585            Some(i) => {
27586                parts.push(&p[..i]);
27587                parts.push(&p[i..]);
27588            }
27589            None => parts.push(p),
27590        }
27591    }
27592    // A bare clock-time token `HH:MM[:SS[.ffffff]]` carries the time-of-day
27593    // part (PG: `3 days 14:30:45`, or `14:30:45` alone). Extract it; whatever
27594    // remains is the `<n> <unit>` pair form handled below.
27595    let mut clock_us: i64 = 0;
27596    let mut had_clock = false;
27597    if let Some(pos) = parts.iter().position(|p| p.contains(':')) {
27598        clock_us = parse_interval_clock(parts[pos])?;
27599        parts.remove(pos);
27600        had_clock = true;
27601    }
27602    // v7.39 (read01 timestamp.c) — a lone bare number alongside a clock
27603    // time is DAYS (PG: '3 4:05:06' = 3 days 04:05:06).
27604    let mut lone_days: i32 = 0;
27605    if had_clock && parts.len() == 1 {
27606        if let Ok(n) = parts[0].parse::<i64>() {
27607            lone_days = i32::try_from(n).ok()?;
27608            parts.clear();
27609        }
27610    }
27611    if !parts.len().is_multiple_of(2) || (parts.is_empty() && !had_clock && lone_days == 0) {
27612        return None;
27613    }
27614    let mut months: i32 = 0;
27615    let mut days: i32 = lone_days;
27616    let mut micros: i64 = clock_us;
27617    let mut i = 0;
27618    while i < parts.len() {
27619        let unit_stripped = canonical_interval_unit(parts[i + 1])?;
27620        if let Ok(n) = parts[i].parse::<i64>() {
27621            match unit_stripped {
27622                "microsecond" => micros = micros.checked_add(n)?,
27623                "millisecond" => micros = micros.checked_add(n.checked_mul(1_000)?)?,
27624                "second" => micros = micros.checked_add(n.checked_mul(1_000_000)?)?,
27625                "minute" => micros = micros.checked_add(n.checked_mul(60_000_000)?)?,
27626                "hour" => micros = micros.checked_add(n.checked_mul(3_600_000_000)?)?,
27627                "day" => {
27628                    let n32 = i32::try_from(n).ok()?;
27629                    days = days.checked_add(n32)?;
27630                }
27631                "week" => {
27632                    let n32 = i32::try_from(n).ok()?;
27633                    days = days.checked_add(n32.checked_mul(7)?)?;
27634                }
27635                "month" => {
27636                    let n32 = i32::try_from(n).ok()?;
27637                    months = months.checked_add(n32)?;
27638                }
27639                "year" => {
27640                    let n32 = i32::try_from(n).ok()?;
27641                    months = months.checked_add(n32.checked_mul(12)?)?;
27642                }
27643                // v7.39 (read01 timestamp.c) — the larger calendar units.
27644                "decade" => {
27645                    let n32 = i32::try_from(n).ok()?;
27646                    months = months.checked_add(n32.checked_mul(120)?)?;
27647                }
27648                "century" => {
27649                    let n32 = i32::try_from(n).ok()?;
27650                    months = months.checked_add(n32.checked_mul(1200)?)?;
27651                }
27652                "millennium" => {
27653                    let n32 = i32::try_from(n).ok()?;
27654                    months = months.checked_add(n32.checked_mul(12000)?)?;
27655                }
27656                _ => return None,
27657            }
27658        } else if let Ok(f) = parts[i].parse::<f64>() {
27659            // Fractional units cascade down to the next-finer field the way
27660            // PG does: `1.5 days` -> `1 day 12:00:00`, `1.5 months` ->
27661            // `1 mon 15 days` (30-day month), `1.5 years` -> `1 year 6 mons`.
27662            // no_std: f64 has no trunc/fract/round methods, so do them with
27663            // casts (toward-zero) + explicit round-half-away-from-zero.
27664            #[allow(clippy::cast_possible_truncation)]
27665            fn round_i64(x: f64) -> i64 {
27666                if x >= 0.0 {
27667                    (x + 0.5) as i64
27668                } else {
27669                    (x - 0.5) as i64
27670                }
27671            }
27672            #[allow(clippy::cast_possible_truncation, clippy::cast_precision_loss)]
27673            fn add_days_frac(days: &mut i32, micros: &mut i64, d: f64) -> Option<()> {
27674                const DAY_US: f64 = 86_400_000_000.0;
27675                let whole = d as i64; // truncates toward zero
27676                let frac = d - whole as f64;
27677                *days = days.checked_add(i32::try_from(whole).ok()?)?;
27678                *micros = micros.checked_add(round_i64(frac * DAY_US))?;
27679                Some(())
27680            }
27681            #[allow(clippy::cast_possible_truncation, clippy::cast_precision_loss)]
27682            match unit_stripped {
27683                "microsecond" => micros = micros.checked_add(round_i64(f))?,
27684                "millisecond" => micros = micros.checked_add(round_i64(f * 1_000.0))?,
27685                "second" => micros = micros.checked_add(round_i64(f * 1_000_000.0))?,
27686                "minute" => micros = micros.checked_add(round_i64(f * 60_000_000.0))?,
27687                "hour" => micros = micros.checked_add(round_i64(f * 3_600_000_000.0))?,
27688                "day" => add_days_frac(&mut days, &mut micros, f)?,
27689                "week" => add_days_frac(&mut days, &mut micros, f * 7.0)?,
27690                "month" => {
27691                    let whole = f as i64;
27692                    months = months.checked_add(i32::try_from(whole).ok()?)?;
27693                    add_days_frac(&mut days, &mut micros, (f - whole as f64) * 30.0)?;
27694                }
27695                "year" => {
27696                    let m = f * 12.0;
27697                    let whole = m as i64;
27698                    months = months.checked_add(i32::try_from(whole).ok()?)?;
27699                    add_days_frac(&mut days, &mut micros, (m - whole as f64) * 30.0)?;
27700                }
27701                _ => return None,
27702            }
27703        } else {
27704            return None;
27705        }
27706        i += 2;
27707    }
27708    finish(Some((months, days, micros)))
27709}
27710
27711/// v7.37 — map a scalar type keyword to its [`CastTarget`] for the PG
27712/// `TYPE 'literal'` typed-literal syntax (`time '10:30'` == `'10:30'::time`).
27713/// `interval` is intentionally absent (handled by its own parser arm).
27714/// Returns `None` for names that aren't sensible as a bare typed literal, so
27715/// the caller falls back to treating the ident as a column reference.
27716fn typed_literal_cast_target(ident: &str) -> Option<CastTarget> {
27717    Some(match ident {
27718        "date" => CastTarget::Date,
27719        "timestamp" | "datetime" => CastTarget::Timestamp,
27720        "timestamptz" => CastTarget::Timestamptz,
27721        "bool" | "boolean" => CastTarget::Bool,
27722        "int" | "integer" | "int4" => CastTarget::Int,
27723        "bigint" | "int8" => CastTarget::BigInt,
27724        "float8" | "double precision" => CastTarget::Float,
27725        "uuid" => CastTarget::Uuid,
27726        "bytea" => CastTarget::Bytea,
27727        "json" => CastTarget::Json,
27728        "jsonb" => CastTarget::Jsonb,
27729        // Types without a dedicated CastTarget variant flow through the
27730        // generic Named path (engine resolves via column_type_to_data_type).
27731        "time" | "timetz" | "smallint" | "int2" | "numeric" | "decimal"
27732        | "real" | "float4" | "inet" | "cidr" | "macaddr" | "macaddr8"
27733        | "money" | "bit" | "varbit"
27734        // Geometric types accept the `TYPE 'literal'` prefix spelling too.
27735        | "point" | "line" | "lseg" | "box" | "path" | "polygon" | "circle"
27736        // Range / multirange types likewise.
27737        | "int4range" | "int8range" | "numrange" | "daterange" | "tsrange"
27738        | "tstzrange" | "int4multirange" | "int8multirange" | "nummultirange"
27739        | "datemultirange" | "tsmultirange" | "tstzmultirange"
27740        // v7.39 (read01 round 18) — oid / name / jsonpath literal prefixes.
27741        | "oid" | "name" | "jsonpath" | "pg_lsn" | "varchar" | "text" | "xid" | "xid8" => {
27742            CastTarget::Named(alloc::string::String::from(ident))
27743        }
27744        _ => return None,
27745    })
27746}
27747
27748/// v7.12.4 — map a bare type-name identifier (the form that
27749/// appears in a function arg list or RETURNS clause) to a
27750/// [`ColumnTypeName`]. Returns `None` for unknown / extension
27751/// types so the caller can preserve them as
27752/// [`FunctionArgType::Raw`] / [`FunctionReturn::Other`].
27753///
27754/// Subset of the full column-type grammar — we deliberately
27755/// don't parse parameterised forms (`VARCHAR(n)`, `NUMERIC(p,s)`)
27756/// here because function-arg types in v7.12.4 are mostly the
27757/// bare form (`text`, `int`, `bytea`, …).
27758/// v7.39 (round 315, V19) — does this whole phrase name a type, rather
27759/// than being `name TYPE`?
27760///
27761/// The multi-word spellings SQL allows for a bare argument type, each
27762/// verified accepted by PG 18.4 as `CREATE FUNCTION f(<phrase>)`.
27763///
27764/// NOTE this list also exists in `spg-storage`, which computes the
27765/// signature key from the rendered argument text and has to reach the
27766/// same verdict. The two crates are siblings — neither depends on the
27767/// other — and each already carries its own table of type spellings
27768/// (`map_type_ident_to_column_type_name` here, `normalize_type_name`
27769/// there), so this follows the structure rather than inventing new
27770/// duplication. Recorded as V49.
27771pub fn is_multiword_type_phrase(phrase: &str) -> bool {
27772    let t = phrase.trim().to_ascii_lowercase();
27773    let base = t.split_once('(').map_or(t.as_str(), |(h, _)| h).trim();
27774    matches!(
27775        base,
27776        "double precision"
27777            | "character varying"
27778            | "bit varying"
27779            | "timestamp with time zone"
27780            | "timestamp without time zone"
27781            | "time with time zone"
27782            | "time without time zone"
27783            | "national character"
27784            | "national character varying"
27785    )
27786}
27787
27788fn map_type_ident_to_column_type_name(ident: &str) -> Option<ColumnTypeName> {
27789    Some(match ident.to_ascii_lowercase().as_str() {
27790        "smallint" | "tinyint" => ColumnTypeName::SmallInt,
27791        "int" | "integer" | "mediumint" => ColumnTypeName::Int,
27792        "bigint" => ColumnTypeName::BigInt,
27793        "float" | "double" => ColumnTypeName::Float,
27794        // v7.39 (round 269) — real is 32-bit.
27795        "real" | "float4" => ColumnTypeName::Real,
27796        "text" => ColumnTypeName::Text,
27797        "bool" | "boolean" => ColumnTypeName::Bool,
27798        "date" => ColumnTypeName::Date,
27799        "timestamp" | "datetime" => ColumnTypeName::Timestamp,
27800        "timestamptz" => ColumnTypeName::Timestamptz,
27801        "json" => ColumnTypeName::Json,
27802        "jsonb" => ColumnTypeName::Jsonb,
27803        "bytea" | "bytes" => ColumnTypeName::Bytes,
27804        "tsvector" => ColumnTypeName::TsVector,
27805        "tsquery" => ColumnTypeName::TsQuery,
27806        "uuid" => ColumnTypeName::Uuid,
27807        "interval" => ColumnTypeName::Interval,
27808        "time" => ColumnTypeName::Time,
27809        "year" => ColumnTypeName::Year,
27810        "timetz" => ColumnTypeName::TimeTz,
27811        "money" => ColumnTypeName::Money,
27812        _ => return None,
27813    })
27814}
27815
27816/// v7.12.4 — parse a PL/pgSQL function body (the bytes between
27817/// `$$ ... $$`). Returns the parsed `BEGIN ... END;` block.
27818///
27819/// v7.12.4 grammar (strict subset — IF / LOOP / DECLARE / RAISE
27820/// / embedded SQL land in v7.12.5+):
27821///
27822/// ```text
27823///   body          := [ws] block [ws]
27824///   block         := BEGIN stmt ( ; stmt )* [ ; ] END [ ; ]
27825///   stmt          := assign | return
27826///   assign        := assign_target := expr
27827///   assign_target := ( NEW | OLD ) . ident | ident
27828///   return        := RETURN ( NEW | OLD | NULL | expr )
27829/// ```
27830///
27831/// `expr` is parsed by recursing into the regular `Parser` — so a
27832/// PL/pgSQL `NEW.search_vector := to_tsvector('english',
27833/// NEW.subject || ' ' || NEW.sender)` body shape works without
27834/// the body parser knowing what `to_tsvector` is.
27835///
27836/// Errors here cause the caller to fall back to
27837/// `FunctionBody::Raw` — keeping the CREATE FUNCTION DDL itself
27838/// successful, but the executor will refuse to invoke the
27839/// function with an "unparseable body" error.
27840/// v7.12.4 — public alias for [`parse_plpgsql_body`] re-exported
27841/// from the crate root as `spg_sql::parse_function_body`.
27842pub fn parse_function_body(body: &str) -> Result<PlPgSqlBlock, ParseError> {
27843    parse_plpgsql_body(body)
27844}
27845
27846fn parse_plpgsql_body(body: &str) -> Result<PlPgSqlBlock, ParseError> {
27847    // Use the regular lexer on the body text. The trailing
27848    // `END;` may or may not have a semicolon; the lexer treats
27849    // both forms identically.
27850    let tokens = lexer::tokenize(body).map_err(|e| ParseError {
27851        message: alloc::format!("plpgsql body lex error: {e}"),
27852        token_pos: 0,
27853    })?;
27854    let mut parser = Parser::new(tokens);
27855    parser.parse_plpgsql_block()
27856}
27857
27858/// v7.39 (GUC) — the textual body of a SET value, for list joining.
27859fn set_value_text(v: &crate::ast::SetValue) -> alloc::string::String {
27860    match v {
27861        crate::ast::SetValue::String(s)
27862        | crate::ast::SetValue::Ident(s)
27863        | crate::ast::SetValue::Number(s) => s.clone(),
27864        crate::ast::SetValue::Default => "DEFAULT".into(),
27865    }
27866}
27867
27868/// v7.39 (round 145, parse_cte.c / parse_agg.c) — true when an expression
27869/// contains an aggregate call at ITS OWN query level (recursion stops at
27870/// sublink boundaries — a sublink's aggregates belong to the sublink).
27871/// Backs the "aggregate functions are not allowed in a recursive query's
27872/// recursive term" well-formedness check.
27873fn expr_has_toplevel_aggregate(e: &Expr) -> bool {
27874    const AGG_NAMES: &[&str] = &[
27875        "count",
27876        "sum",
27877        "min",
27878        "max",
27879        "avg",
27880        "string_agg",
27881        "array_agg",
27882        "bool_and",
27883        "bool_or",
27884        "every",
27885        "any_value",
27886        "json_agg",
27887        "jsonb_agg",
27888        "json_object_agg",
27889        "jsonb_object_agg",
27890        "bit_and",
27891        "bit_or",
27892        "bit_xor",
27893        "var_pop",
27894        "var_samp",
27895        "variance",
27896        "stddev",
27897        "stddev_pop",
27898        "stddev_samp",
27899        "range_agg",
27900        "range_intersect_agg",
27901        "percentile_cont",
27902        "percentile_disc",
27903        "mode",
27904        "corr",
27905        "covar_pop",
27906        "covar_samp",
27907    ];
27908    match e {
27909        Expr::AggregateOrdered { .. } => true,
27910        Expr::FunctionCall { name, args } => {
27911            AGG_NAMES.contains(&name.to_ascii_lowercase().as_str())
27912                || args.iter().any(expr_has_toplevel_aggregate)
27913        }
27914        Expr::NamedArg { expr, .. }
27915        | Expr::Variadic(expr)
27916        | Expr::Unary { expr, .. }
27917        | Expr::Cast { expr, .. }
27918        | Expr::IsNull { expr, .. }
27919        | Expr::FieldAccess { base: expr, .. }
27920        | Expr::Extract { source: expr, .. } => expr_has_toplevel_aggregate(expr),
27921        Expr::Binary { lhs, rhs, .. } => {
27922            expr_has_toplevel_aggregate(lhs) || expr_has_toplevel_aggregate(rhs)
27923        }
27924        Expr::Like { expr, pattern, .. } => {
27925            expr_has_toplevel_aggregate(expr) || expr_has_toplevel_aggregate(pattern)
27926        }
27927        Expr::Array(items) => items.iter().any(expr_has_toplevel_aggregate),
27928        Expr::InList { expr, list, .. } => {
27929            expr_has_toplevel_aggregate(expr) || list.iter().any(expr_has_toplevel_aggregate)
27930        }
27931        Expr::ArraySubscript { target, index } => {
27932            expr_has_toplevel_aggregate(target) || expr_has_toplevel_aggregate(index)
27933        }
27934        Expr::ArraySlice { target, lo, hi } => {
27935            expr_has_toplevel_aggregate(target)
27936                || lo.as_deref().is_some_and(expr_has_toplevel_aggregate)
27937                || hi.as_deref().is_some_and(expr_has_toplevel_aggregate)
27938        }
27939        Expr::AnyAll { expr, array, .. } => {
27940            expr_has_toplevel_aggregate(expr) || expr_has_toplevel_aggregate(array)
27941        }
27942        Expr::Case {
27943            operand,
27944            branches,
27945            else_branch,
27946        } => {
27947            operand.as_deref().is_some_and(expr_has_toplevel_aggregate)
27948                || branches
27949                    .iter()
27950                    .any(|(w, t)| expr_has_toplevel_aggregate(w) || expr_has_toplevel_aggregate(t))
27951                || else_branch
27952                    .as_deref()
27953                    .is_some_and(expr_has_toplevel_aggregate)
27954        }
27955        // The outer-level operands of a sublink can aggregate; the sublink's
27956        // own body cannot leak its aggregates up here.
27957        Expr::InSubquery { expr, .. } => expr_has_toplevel_aggregate(expr),
27958        Expr::RowInSubquery { row, .. } | Expr::RowCmpSubquery { row, .. } => {
27959            row.iter().any(expr_has_toplevel_aggregate)
27960        }
27961        _ => false,
27962    }
27963}
27964
27965/// v7.39 (round 145, parse_cte.c) — true when any sublink expression
27966/// (EXISTS / IN / scalar subquery) inside this SELECT term references the
27967/// named table anywhere in its subtree. A plain FROM derived table is NOT a
27968/// sublink and is legal in a recursive term, so it is not walked here.
27969fn select_has_self_ref_in_sublink(s: &crate::ast::SelectStatement, name: &str) -> bool {
27970    let mut exprs: Vec<&Expr> = Vec::new();
27971    for it in &s.items {
27972        if let crate::ast::SelectItem::Expr { expr, .. } = it {
27973            exprs.push(expr);
27974        }
27975    }
27976    if let Some(w) = &s.where_ {
27977        exprs.push(w);
27978    }
27979    if let Some(h) = &s.having {
27980        exprs.push(h);
27981    }
27982    if let Some(g) = &s.group_by {
27983        exprs.extend(g.iter());
27984    }
27985    if let Some(from) = &s.from {
27986        for j in &from.joins {
27987            if let Some(on) = &j.on {
27988                exprs.push(on);
27989            }
27990        }
27991    }
27992    exprs.into_iter().any(|e| expr_sublink_mentions(e, name))
27993}
27994
27995/// Does this expression contain a sublink whose subquery mentions `name`?
27996fn expr_sublink_mentions(e: &Expr, name: &str) -> bool {
27997    match e {
27998        Expr::ScalarSubquery(sub) => select_mentions_table(sub, name),
27999        Expr::Exists { subquery, .. } => select_mentions_table(subquery, name),
28000        Expr::InSubquery { expr, subquery, .. } => {
28001            expr_sublink_mentions(expr, name) || select_mentions_table(subquery, name)
28002        }
28003        Expr::RowInSubquery { row, subquery, .. } => {
28004            row.iter().any(|x| expr_sublink_mentions(x, name))
28005                || select_mentions_table(subquery, name)
28006        }
28007        Expr::RowCmpSubquery { row, subquery, .. } => {
28008            row.iter().any(|x| expr_sublink_mentions(x, name))
28009                || select_mentions_table(subquery, name)
28010        }
28011        Expr::NamedArg { expr, .. }
28012        | Expr::Variadic(expr)
28013        | Expr::Unary { expr, .. }
28014        | Expr::Cast { expr, .. }
28015        | Expr::IsNull { expr, .. }
28016        | Expr::FieldAccess { base: expr, .. }
28017        | Expr::Extract { source: expr, .. } => expr_sublink_mentions(expr, name),
28018        Expr::Binary { lhs, rhs, .. } => {
28019            expr_sublink_mentions(lhs, name) || expr_sublink_mentions(rhs, name)
28020        }
28021        Expr::Like { expr, pattern, .. } => {
28022            expr_sublink_mentions(expr, name) || expr_sublink_mentions(pattern, name)
28023        }
28024        Expr::FunctionCall { args, .. } | Expr::Array(args) => {
28025            args.iter().any(|x| expr_sublink_mentions(x, name))
28026        }
28027        Expr::InList { expr, list, .. } => {
28028            expr_sublink_mentions(expr, name) || list.iter().any(|x| expr_sublink_mentions(x, name))
28029        }
28030        Expr::ArraySubscript { target, index } => {
28031            expr_sublink_mentions(target, name) || expr_sublink_mentions(index, name)
28032        }
28033        Expr::ArraySlice { target, lo, hi } => {
28034            expr_sublink_mentions(target, name)
28035                || lo
28036                    .as_deref()
28037                    .is_some_and(|x| expr_sublink_mentions(x, name))
28038                || hi
28039                    .as_deref()
28040                    .is_some_and(|x| expr_sublink_mentions(x, name))
28041        }
28042        Expr::AnyAll { expr, array, .. } => {
28043            expr_sublink_mentions(expr, name) || expr_sublink_mentions(array, name)
28044        }
28045        Expr::Case {
28046            operand,
28047            branches,
28048            else_branch,
28049        } => {
28050            operand
28051                .as_deref()
28052                .is_some_and(|x| expr_sublink_mentions(x, name))
28053                || branches
28054                    .iter()
28055                    .any(|(w, t)| expr_sublink_mentions(w, name) || expr_sublink_mentions(t, name))
28056                || else_branch
28057                    .as_deref()
28058                    .is_some_and(|x| expr_sublink_mentions(x, name))
28059        }
28060        _ => false,
28061    }
28062}
28063
28064/// Does this SELECT (in full — FROM tables, derived tables, its own
28065/// sublinks, and union arms) mention the named table?
28066fn select_mentions_table(s: &crate::ast::SelectStatement, name: &str) -> bool {
28067    if let Some(from) = &s.from {
28068        if from.primary.name.eq_ignore_ascii_case(name) {
28069            return true;
28070        }
28071        if let Some(sub) = &from.primary.lateral_subquery
28072            && select_mentions_table(sub, name)
28073        {
28074            return true;
28075        }
28076        for j in &from.joins {
28077            if j.table.name.eq_ignore_ascii_case(name) {
28078                return true;
28079            }
28080            if let Some(sub) = &j.table.lateral_subquery
28081                && select_mentions_table(sub, name)
28082            {
28083                return true;
28084            }
28085        }
28086    }
28087    if select_has_self_ref_in_sublink(s, name) {
28088        return true;
28089    }
28090    s.unions.iter().any(|(_, u)| select_mentions_table(u, name))
28091}
28092
28093/// v7.39 (round 284) — fold a constant `LIMIT` / `OFFSET` expression to a
28094/// row count, the way PG evaluates one before applying it.
28095///
28096/// `None` = not a constant (a column, a subquery, a function call).
28097/// `Some(Err(msg))` = PG rejects it, and the message is PG's; `{L}` in the
28098/// message stands in for LIMIT / OFFSET, which the caller substitutes.
28099/// All wordings were read off live PG 18.4.
28100fn fold_limit_constant(e: &crate::ast::Expr) -> Option<Result<i128, alloc::string::String>> {
28101    use crate::ast::{BinOp, Expr, Literal, UnOp};
28102    match e {
28103        Expr::Literal(Literal::Integer(n)) => Some(Ok(i128::from(*n))),
28104        Expr::Literal(Literal::Numeric { unscaled, scale }) => {
28105            Some(Ok(round_scaled_half_away(*unscaled, *scale)))
28106        }
28107        // PG coerces a string by its CONTENT, and fails on the value.
28108        Expr::Literal(Literal::String(t)) => Some(t.trim().parse::<i64>().map_or_else(
28109            |_| {
28110                Err(alloc::format!(
28111                    "invalid input syntax for type bigint: \"{t}\""
28112                ))
28113            },
28114            |n| Ok(i128::from(n)),
28115        )),
28116        Expr::Literal(Literal::Bool(_)) => Some(Err(
28117            "argument of {L} must be type bigint, not type boolean".into(),
28118        )),
28119        Expr::Unary {
28120            op: UnOp::Neg,
28121            expr,
28122        } => match fold_limit_constant(expr)? {
28123            Ok(v) => Some(Ok(-v)),
28124            e @ Err(_) => Some(e),
28125        },
28126        Expr::Binary { lhs, op, rhs } => {
28127            let a = match fold_limit_constant(lhs)? {
28128                Ok(v) => v,
28129                e @ Err(_) => return Some(e),
28130            };
28131            let b = match fold_limit_constant(rhs)? {
28132                Ok(v) => v,
28133                e @ Err(_) => return Some(e),
28134            };
28135            let out = match op {
28136                BinOp::Add => a.checked_add(b),
28137                BinOp::Sub => a.checked_sub(b),
28138                BinOp::Mul => a.checked_mul(b),
28139                BinOp::Div if b != 0 => a.checked_div(b),
28140                BinOp::Div => return Some(Err("division by zero".into())),
28141                BinOp::Mod if b != 0 => a.checked_rem(b),
28142                BinOp::Mod => return Some(Err("division by zero".into())),
28143                _ => return None,
28144            };
28145            // PG evaluates the arithmetic in the operand's own type, so an
28146            // int-by-int product that leaves int range fails there — before
28147            // the row count is ever looked at.
28148            match out {
28149                Some(v) if v > i128::from(i32::MAX) || v < i128::from(i32::MIN) => {
28150                    Some(Err("integer out of range".into()))
28151                }
28152                Some(v) => Some(Ok(v)),
28153                None => Some(Err("integer out of range".into())),
28154            }
28155        }
28156        _ => None,
28157    }
28158}
28159
28160/// Round `unscaled / 10^scale` half away from zero — PG's numeric→bigint
28161/// cast, which is what makes `LIMIT 2.5` keep three rows.
28162fn round_scaled_half_away(unscaled: i128, scale: u16) -> i128 {
28163    if scale == 0 {
28164        return unscaled;
28165    }
28166    let Some(div) = 10i128.checked_pow(u32::from(scale)) else {
28167        return 0;
28168    };
28169    let neg = unscaled < 0;
28170    let mag = unscaled.unsigned_abs() as i128;
28171    let rounded = (mag + div / 2) / div;
28172    if neg { -rounded } else { rounded }
28173}
28174
28175#[cfg(test)]
28176mod tests {
28177    use super::*;
28178    use alloc::string::ToString;
28179
28180    fn parse(s: &str) -> Statement {
28181        parse_statement(s).expect("parse ok")
28182    }
28183
28184    // v7.37.43-T4 sentori cutover acceptance — `release`, `index`,
28185    // `tables`, `partition`, etc. are unreserved keywords per PG's
28186    // `pg_get_keywords()` and MUST be usable as column / table /
28187    // alias names. Pre-T4 every drop-in user whose schema had one
28188    // of these as a column name (sentori events.release, mailrs
28189    // messages.index in some forks) blew the parser up at CREATE
28190    // TABLE time with "expected identifier, got Release". The
28191    // generalisation lives in `unreserved_keyword_text` + the
28192    // `expect_ident_like` and `parse_atom` arms that consult it.
28193    #[test]
28194    fn release_usable_as_column_name_in_create_table() {
28195        let stmt =
28196            parse("CREATE TABLE events (id INT PRIMARY KEY, release TEXT NOT NULL, payload TEXT)");
28197        if let Statement::CreateTable(t) = stmt {
28198            let names: alloc::vec::Vec<&str> = t.columns.iter().map(|c| c.name.as_str()).collect();
28199            assert_eq!(names, alloc::vec!["id", "release", "payload"]);
28200        } else {
28201            panic!("expected CreateTable");
28202        }
28203    }
28204
28205    #[test]
28206    fn release_usable_as_column_ref_in_select_projection() {
28207        // The sentori `0003_partition_events.sql` INSERT-SELECT
28208        // walk references `release` in both column lists; the
28209        // projection-side use exercises `parse_atom`'s relaxed
28210        // identifier set.
28211        parse("SELECT id, release, payload FROM events WHERE id = 1");
28212    }
28213
28214    #[test]
28215    fn release_usable_as_column_ref_in_insert_column_list() {
28216        // INSERT INTO t (id, release, payload) VALUES (…)
28217        parse("INSERT INTO events (id, release, payload) VALUES (1, '1.0.0', 'data')");
28218    }
28219
28220    #[test]
28221    fn alter_column_drop_not_null_uses_keyword_drop_token() {
28222        // Sentori `0013_audit_tombstone.sql` issues
28223        // `ALTER TABLE … ALTER COLUMN x DROP NOT NULL`. The lexer
28224        // emits Token::Drop (not Ident("drop")); the parser must
28225        // accept both in the ALTER COLUMN sub-dispatch.
28226        parse("ALTER TABLE audit_logs ALTER COLUMN org_id DROP NOT NULL");
28227    }
28228
28229    #[test]
28230    fn create_index_accepts_parenthesised_expression_key() {
28231        // sentori `0040_events_bundle_idx.sql` shape — JSONB
28232        // expression index. Pre-T4 the parser bailed at the
28233        // inner `(` with "expected column ident or expression,
28234        // got LParen". The Token::LParen arm in CREATE INDEX
28235        // routes through the expression parser instead.
28236        parse(
28237            "CREATE INDEX IF NOT EXISTS events_bundle_id_idx \
28238             ON events ((payload->'bundle'->>'id'))",
28239        );
28240    }
28241
28242    // v7.30.2 (mailrs round-25 ask 2) — nesting / chain budgets must
28243    // surface as parse errors, never stack overflows (embed hosts
28244    // abort on overflow).
28245    /// The nesting budget is a COUNT; what it has to fit inside is a
28246    /// number of BYTES, and only one of those two is stable across
28247    /// compiler versions. Round 847 measured 30,336 bytes per level
28248    /// after a toolchain move, which puts 64 levels at 1.94 MB and
28249    /// overflows a 2 MiB thread — `nesting_budget_errors_cleanly`
28250    /// aborted instead of erroring, which is precisely the outcome it
28251    /// exists to rule out.
28252    ///
28253    /// So the budget is metered rather than assumed. The ceiling leaves
28254    /// the depth SPG advertises fitting in a default 2 MiB thread with
28255    /// room to spare, in the debug build, where frames are widest.
28256    #[test]
28257    fn nesting_frame_cost_stays_under_ceiling() {
28258        // Room for MAX_NEST_DEPTH levels inside 1.2 MB, so a 2 MiB
28259        // thread keeps a margin for whatever called the parser.
28260        const CEILING: usize = 1_200_000 / MAX_NEST_DEPTH;
28261
28262        frame_meter::reset();
28263        let depth = frame_meter::SAMPLE_HI + 8;
28264        let sql = format!("SELECT {}1{}", "(".repeat(depth), ")".repeat(depth));
28265        parse(&sql);
28266
28267        let per_level = frame_meter::bytes_per_level();
28268        {
28269            extern crate std;
28270            std::eprintln!("nesting frame: {per_level} bytes/level, ceiling {CEILING}");
28271        }
28272        assert!(
28273            per_level <= CEILING,
28274            "{per_level} bytes per nesting level exceeds {CEILING}; \
28275             {MAX_NEST_DEPTH} levels would want {} bytes. Out-line arms \
28276             in parse_expr_inner / parse_unary rather than lowering the \
28277             depth or widening the stack.",
28278            per_level * MAX_NEST_DEPTH
28279        );
28280    }
28281
28282    #[test]
28283    fn nesting_budget_errors_cleanly() {
28284        let depth = MAX_NEST_DEPTH + 50;
28285        let sql = format!("SELECT {}1{}", "(".repeat(depth), ")".repeat(depth));
28286        let err = parse_statement(&sql).expect_err("must reject");
28287        assert!(err.message.contains("nests deeper"), "{err:?}");
28288        // Within budget still parses.
28289        let sql = format!("SELECT {}1{}", "(".repeat(48), ")".repeat(48));
28290        parse(&sql);
28291    }
28292
28293    #[test]
28294    fn binary_chain_budget_errors_cleanly() {
28295        let sql = format!("SELECT 1{}", " + 1".repeat(MAX_BINARY_CHAIN + 50));
28296        let err = parse_statement(&sql).expect_err("must reject");
28297        assert!(err.message.contains("chained binary"), "{err:?}");
28298        // Within budget still parses (chain depth ≤ budget is safe
28299        // for recursive eval/drop on 2 MiB stacks).
28300        let sql = format!("SELECT 1{}", " + 1".repeat(200));
28301        parse(&sql);
28302    }
28303
28304    #[test]
28305    fn in_list_unaffected_by_chain_budget() {
28306        // Flat InList: 20k elements parse fine and stay flat.
28307        let items: alloc::vec::Vec<String> = (0..20_000).map(|k| k.to_string()).collect();
28308        let sql = format!("SELECT 1 WHERE 5 IN ({})", items.join(","));
28309        let Statement::Select(s) = parse(&sql) else {
28310            panic!("expected select")
28311        };
28312        let Some(Expr::InList { list, negated, .. }) = s.where_ else {
28313            panic!("expected flat InList, got {:?}", s.where_)
28314        };
28315        assert_eq!(list.len(), 20_000);
28316        assert!(!negated);
28317    }
28318
28319    fn lit_int(n: i64) -> Expr {
28320        Expr::Literal(Literal::Integer(n))
28321    }
28322
28323    fn col(name: &str) -> Expr {
28324        Expr::Column(ColumnName {
28325            qualifier: None,
28326            name: name.into(),
28327        })
28328    }
28329
28330    #[test]
28331    fn select_single_integer() {
28332        let s = parse("SELECT 1");
28333        let Statement::Select(s) = s else {
28334            panic!("expected SELECT")
28335        };
28336        assert_eq!(s.items.len(), 1);
28337        assert!(s.from.is_none());
28338        assert!(s.where_.is_none());
28339    }
28340
28341    #[test]
28342    fn select_multiple_literal_kinds() {
28343        let s = parse("SELECT 1, 'hi', NULL, TRUE, 1.5");
28344        let Statement::Select(s) = s else {
28345            panic!("expected SELECT")
28346        };
28347        assert_eq!(s.items.len(), 5);
28348    }
28349
28350    #[test]
28351    fn select_wildcard_from_table() {
28352        let s = parse("SELECT * FROM users");
28353        let Statement::Select(s) = s else {
28354            panic!("expected SELECT")
28355        };
28356        assert!(matches!(s.items[..], [SelectItem::Wildcard]));
28357        assert_eq!(s.from.as_ref().unwrap().primary.name, "users");
28358    }
28359
28360    #[test]
28361    fn select_with_table_alias() {
28362        let s = parse("SELECT * FROM users AS u");
28363        let Statement::Select(s) = s else {
28364            panic!("expected SELECT")
28365        };
28366        let t = &s.from.as_ref().unwrap().primary;
28367        assert_eq!(t.name, "users");
28368        assert_eq!(t.alias.as_deref(), Some("u"));
28369    }
28370
28371    #[test]
28372    fn select_with_where_eq() {
28373        let s = parse("SELECT a FROM t WHERE a = 1");
28374        let Statement::Select(s) = s else {
28375            panic!("expected SELECT")
28376        };
28377        let w = s.where_.unwrap();
28378        assert_eq!(
28379            w,
28380            Expr::Binary {
28381                lhs: Box::new(col("a")),
28382                op: BinOp::Eq,
28383                rhs: Box::new(lit_int(1)),
28384            }
28385        );
28386    }
28387
28388    #[test]
28389    fn arithmetic_precedence() {
28390        let s = parse("SELECT 1 + 2 * 3");
28391        let Statement::Select(s) = s else {
28392            panic!("expected SELECT")
28393        };
28394        let SelectItem::Expr { expr, .. } = &s.items[0] else {
28395            panic!("wildcard?")
28396        };
28397        assert_eq!(
28398            expr,
28399            &Expr::Binary {
28400                lhs: Box::new(lit_int(1)),
28401                op: BinOp::Add,
28402                rhs: Box::new(Expr::Binary {
28403                    lhs: Box::new(lit_int(2)),
28404                    op: BinOp::Mul,
28405                    rhs: Box::new(lit_int(3)),
28406                }),
28407            }
28408        );
28409    }
28410
28411    #[test]
28412    fn parentheses_override_precedence() {
28413        let s = parse("SELECT (1 + 2) * 3");
28414        let Statement::Select(s) = s else {
28415            panic!("expected SELECT")
28416        };
28417        let SelectItem::Expr { expr, .. } = &s.items[0] else {
28418            panic!()
28419        };
28420        assert_eq!(
28421            expr,
28422            &Expr::Binary {
28423                lhs: Box::new(Expr::Binary {
28424                    lhs: Box::new(lit_int(1)),
28425                    op: BinOp::Add,
28426                    rhs: Box::new(lit_int(2)),
28427                }),
28428                op: BinOp::Mul,
28429                rhs: Box::new(lit_int(3)),
28430            }
28431        );
28432    }
28433
28434    #[test]
28435    fn not_binds_below_comparison() {
28436        // `NOT a = 1` should parse as `NOT (a = 1)`.
28437        let s = parse("SELECT NOT a = 1 FROM t");
28438        let Statement::Select(s) = s else {
28439            panic!("expected SELECT")
28440        };
28441        let SelectItem::Expr { expr, .. } = &s.items[0] else {
28442            panic!()
28443        };
28444        assert_eq!(
28445            expr,
28446            &Expr::Unary {
28447                op: UnOp::Not,
28448                expr: Box::new(Expr::Binary {
28449                    lhs: Box::new(col("a")),
28450                    op: BinOp::Eq,
28451                    rhs: Box::new(lit_int(1)),
28452                }),
28453            }
28454        );
28455    }
28456
28457    #[test]
28458    fn unary_minus_binds_above_multiplication() {
28459        // `-a * 2` should be `(-a) * 2`.
28460        let s = parse("SELECT -a * 2 FROM t");
28461        let Statement::Select(s) = s else {
28462            panic!("expected SELECT")
28463        };
28464        let SelectItem::Expr { expr, .. } = &s.items[0] else {
28465            panic!()
28466        };
28467        assert_eq!(
28468            expr,
28469            &Expr::Binary {
28470                lhs: Box::new(Expr::Unary {
28471                    op: UnOp::Neg,
28472                    expr: Box::new(col("a")),
28473                }),
28474                op: BinOp::Mul,
28475                rhs: Box::new(lit_int(2)),
28476            }
28477        );
28478    }
28479
28480    #[test]
28481    fn qualified_column() {
28482        let s = parse("SELECT t.col FROM t");
28483        let Statement::Select(s) = s else {
28484            panic!("expected SELECT")
28485        };
28486        let SelectItem::Expr { expr, .. } = &s.items[0] else {
28487            panic!()
28488        };
28489        assert_eq!(
28490            expr,
28491            &Expr::Column(ColumnName {
28492                qualifier: Some("t".into()),
28493                name: "col".into()
28494            })
28495        );
28496    }
28497
28498    #[test]
28499    fn select_item_alias_with_as() {
28500        let s = parse("SELECT a AS y FROM t");
28501        let Statement::Select(s) = s else {
28502            panic!("expected SELECT")
28503        };
28504        let SelectItem::Expr { alias, .. } = &s.items[0] else {
28505            panic!()
28506        };
28507        assert_eq!(alias.as_deref(), Some("y"));
28508    }
28509
28510    #[test]
28511    fn trailing_semicolon_accepted() {
28512        let s = parse("SELECT 1;");
28513        let Statement::Select(s) = s else {
28514            panic!("expected SELECT")
28515        };
28516        assert_eq!(s.items.len(), 1);
28517    }
28518
28519    #[test]
28520    fn boolean_chain_with_and_or_not() {
28521        // (NOT a) OR (b AND (NOT c))
28522        let s = parse("SELECT NOT a OR b AND NOT c FROM t");
28523        let Statement::Select(s) = s else {
28524            panic!("expected SELECT")
28525        };
28526        let SelectItem::Expr { expr, .. } = &s.items[0] else {
28527            panic!()
28528        };
28529        let expected = Expr::Binary {
28530            lhs: Box::new(Expr::Unary {
28531                op: UnOp::Not,
28532                expr: Box::new(col("a")),
28533            }),
28534            op: BinOp::Or,
28535            rhs: Box::new(Expr::Binary {
28536                lhs: Box::new(col("b")),
28537                op: BinOp::And,
28538                rhs: Box::new(Expr::Unary {
28539                    op: UnOp::Not,
28540                    expr: Box::new(col("c")),
28541                }),
28542            }),
28543        };
28544        assert_eq!(expr, &expected);
28545    }
28546
28547    #[test]
28548    fn empty_input_errors() {
28549        // v7.14.0 — pg_dump preambles emit several comment-only
28550        // / blank-line statements that collapse to Statement::
28551        // Empty rather than a parse error. The old "SELECT in
28552        // message" assertion is stale; verify the new contract:
28553        // empty / whitespace / comment-only input parses to
28554        // Statement::Empty.
28555        assert!(matches!(parse_statement("").unwrap(), Statement::Empty));
28556        assert!(matches!(
28557            parse_statement("  \n\t ").unwrap(),
28558            Statement::Empty
28559        ));
28560        // Sanity: malformed-but-non-empty still errors.
28561        assert!(parse_statement("SELECT FROM WHERE").is_err());
28562    }
28563
28564    #[test]
28565    fn unmatched_paren_errors() {
28566        assert!(parse_statement("SELECT (1 + 2").is_err());
28567    }
28568
28569    #[test]
28570    fn display_round_trip_simple_select() {
28571        let original = parse("SELECT a + 1 FROM t WHERE a > 0");
28572        let text = original.to_string();
28573        let again = parse_statement(&text).expect("re-parse");
28574        assert_eq!(original, again);
28575    }
28576
28577    // --- CREATE TABLE & INSERT (v0.3) ---------------------------------------
28578
28579    #[test]
28580    fn create_table_single_column() {
28581        let s = parse("CREATE TABLE foo (a INT)");
28582        let Statement::CreateTable(c) = s else {
28583            panic!("expected CreateTable")
28584        };
28585        assert_eq!(c.name, "foo");
28586        assert_eq!(c.columns.len(), 1);
28587        assert_eq!(c.columns[0].name, "a");
28588        assert_eq!(c.columns[0].ty, ColumnTypeName::Int);
28589        assert!(c.columns[0].nullable);
28590    }
28591
28592    #[test]
28593    fn create_table_multi_column_with_not_null_mix() {
28594        let s = parse("CREATE TABLE u (id INT NOT NULL, name TEXT, score FLOAT NOT NULL, ok BOOL)");
28595        let Statement::CreateTable(c) = s else {
28596            panic!()
28597        };
28598        assert_eq!(c.columns.len(), 4);
28599        assert_eq!(c.columns[0].ty, ColumnTypeName::Int);
28600        assert!(!c.columns[0].nullable);
28601        assert_eq!(c.columns[1].ty, ColumnTypeName::Text);
28602        assert!(c.columns[1].nullable);
28603        assert_eq!(c.columns[2].ty, ColumnTypeName::Float);
28604        assert!(!c.columns[2].nullable);
28605        assert_eq!(c.columns[3].ty, ColumnTypeName::Bool);
28606    }
28607
28608    #[test]
28609    fn create_table_bigint_supported() {
28610        let s = parse("CREATE TABLE accounts (id BIGINT NOT NULL)");
28611        let Statement::CreateTable(c) = s else {
28612            panic!()
28613        };
28614        assert_eq!(c.columns[0].ty, ColumnTypeName::BigInt);
28615    }
28616
28617    #[test]
28618    fn create_table_vector_default_is_f32() {
28619        let s = parse("CREATE TABLE t (v VECTOR(128))");
28620        let Statement::CreateTable(c) = s else {
28621            panic!()
28622        };
28623        assert_eq!(
28624            c.columns[0].ty,
28625            ColumnTypeName::Vector {
28626                dim: 128,
28627                encoding: VecEncoding::F32,
28628            },
28629        );
28630    }
28631
28632    #[test]
28633    fn create_table_vector_using_sq8() {
28634        // v6.0.1: `USING SQ8` selects scalar-quantised encoding.
28635        // Case-insensitive on both `USING` and the encoding name.
28636        for sql in [
28637            "CREATE TABLE t (v VECTOR(128) USING SQ8)",
28638            "CREATE TABLE t (v VECTOR(128) using sq8)",
28639        ] {
28640            let s = parse(sql);
28641            let Statement::CreateTable(c) = s else {
28642                panic!()
28643            };
28644            assert_eq!(
28645                c.columns[0].ty,
28646                ColumnTypeName::Vector {
28647                    dim: 128,
28648                    encoding: VecEncoding::Sq8,
28649                },
28650                "{sql}",
28651            );
28652        }
28653    }
28654
28655    #[test]
28656    fn create_table_vector_using_unknown_errors() {
28657        // v7.16.1 — the inline `USING <encoding>` shape on
28658        // CREATE TABLE column defs was withdrawn before
28659        // v7.14.0 in favour of `CREATE INDEX … USING hnsw
28660        // (col vector_<metric>_ops)`; the parser now rejects
28661        // USING at column-list position with a clearer
28662        // "expected ',' or ')'" message. Test asserts the
28663        // current rejection, not the old "unknown vector
28664        // encoding" string.
28665        let err = parse_statement("CREATE TABLE t (v VECTOR(8) USING PQ8)").unwrap_err();
28666        assert!(
28667            err.message.contains("USING")
28668                || err.message.contains("using")
28669                || err.message.contains("')'")
28670                || err.message.contains("','"),
28671            "expected USING/column-list rejection, got: {}",
28672            err.message
28673        );
28674    }
28675
28676    #[test]
28677    fn vector_using_sq8_display_roundtrips() {
28678        // The Display impl must produce text that re-parses to the
28679        // same AST. Guard for the v6.0.1 `USING SQ8` suffix.
28680        let s = parse("CREATE TABLE t (v VECTOR(64) USING SQ8)");
28681        let Statement::CreateTable(c) = s else {
28682            panic!()
28683        };
28684        assert_eq!(c.columns[0].ty.to_string(), "VECTOR(64) USING SQ8");
28685    }
28686
28687    #[test]
28688    fn parser_recognises_placeholders() {
28689        use crate::ast::{Expr, SelectItem, Statement};
28690        // $N in expression position parses as Expr::Placeholder(N).
28691        let s = parse("SELECT $1, $2 + 1 FROM t WHERE x = $3");
28692        let Statement::Select(sel) = s else { panic!() };
28693        assert!(matches!(
28694            sel.items[0],
28695            SelectItem::Expr {
28696                expr: Expr::Placeholder(1),
28697                alias: None
28698            }
28699        ));
28700        // $2 + 1
28701        let SelectItem::Expr {
28702            expr: Expr::Binary { lhs, rhs, .. },
28703            ..
28704        } = &sel.items[1]
28705        else {
28706            panic!()
28707        };
28708        assert!(matches!(**lhs, Expr::Placeholder(2)));
28709        assert!(matches!(**rhs, Expr::Literal(Literal::Integer(1))));
28710        // WHERE x = $3
28711        let Some(Expr::Binary { rhs, .. }) = sel.where_.as_ref() else {
28712            panic!()
28713        };
28714        assert!(matches!(**rhs, Expr::Placeholder(3)));
28715    }
28716
28717    #[test]
28718    fn parser_rejects_dollar_zero() {
28719        // $0 is not valid in PG; the lexer rejects it.
28720        assert!(parse_statement("SELECT $0").is_err());
28721    }
28722
28723    #[test]
28724    fn placeholder_display_roundtrips() {
28725        // The Display impl must produce text that re-lexes to the
28726        // same Placeholder token.
28727        let s = parse("SELECT $42 FROM t");
28728        let printed = s.to_string();
28729        assert!(printed.contains("$42"));
28730        let again = parse(&printed);
28731        assert_eq!(s, again);
28732    }
28733
28734    #[test]
28735    fn alter_index_rebuild_bare() {
28736        use crate::ast::{AlterIndexTarget, Statement};
28737        let s = parse("ALTER INDEX my_idx REBUILD");
28738        let Statement::AlterIndex(a) = s else {
28739            panic!("expected AlterIndex, got {s:?}")
28740        };
28741        assert_eq!(a.name, "my_idx");
28742        assert_eq!(a.target, AlterIndexTarget::Rebuild { encoding: None });
28743    }
28744
28745    #[test]
28746    fn alter_index_rebuild_with_encoding() {
28747        use crate::ast::{AlterIndexTarget, Statement};
28748        for (sql, want) in [
28749            (
28750                "ALTER INDEX my_idx REBUILD WITH (encoding = F32)",
28751                VecEncoding::F32,
28752            ),
28753            (
28754                "ALTER INDEX my_idx REBUILD WITH (encoding = sq8)",
28755                VecEncoding::Sq8,
28756            ),
28757            (
28758                "ALTER INDEX my_idx REBUILD WITH (encoding = HALF)",
28759                VecEncoding::F16,
28760            ),
28761        ] {
28762            let s = parse(sql);
28763            let Statement::AlterIndex(a) = s else {
28764                panic!("{sql}: expected AlterIndex")
28765            };
28766            assert_eq!(a.name, "my_idx");
28767            assert_eq!(
28768                a.target,
28769                AlterIndexTarget::Rebuild {
28770                    encoding: Some(want)
28771                },
28772                "{sql}"
28773            );
28774        }
28775    }
28776
28777    #[test]
28778    fn alter_index_rebuild_unknown_encoding_errors() {
28779        let err = parse_statement("ALTER INDEX my_idx REBUILD WITH (encoding = PQ8)").unwrap_err();
28780        assert!(
28781            err.message.contains("unknown vector encoding"),
28782            "got: {}",
28783            err.message
28784        );
28785    }
28786
28787    #[test]
28788    fn alter_index_rebuild_display_roundtrips() {
28789        for (input, want) in [
28790            ("ALTER INDEX my_idx REBUILD", "ALTER INDEX my_idx REBUILD"),
28791            (
28792                "ALTER INDEX my_idx REBUILD WITH (encoding = SQ8)",
28793                "ALTER INDEX my_idx REBUILD WITH (encoding = SQ8)",
28794            ),
28795            (
28796                "ALTER INDEX my_idx REBUILD WITH (encoding = HALF)",
28797                "ALTER INDEX my_idx REBUILD WITH (encoding = HALF)",
28798            ),
28799        ] {
28800            let s = parse(input);
28801            assert_eq!(s.to_string(), want);
28802        }
28803    }
28804
28805    #[test]
28806    fn create_table_unknown_type_defers_to_engine() {
28807        // v4.9 picked XML as a parse-time "unsupported column
28808        // type" probe. v7.17.0 Phase 1.4 changed the contract:
28809        // an unknown type ident parses as Text + `user_type_ref`
28810        // so CREATE TABLE can resolve user-defined enum / domain
28811        // types — rejection of truly-unknown types moved to the
28812        // engine's catalog lookup. v7.37.5 ζ-A then promoted XML
28813        // to a first-class built-in, so this probe switched to a
28814        // synthetic name nothing in the lexer will ever recognise.
28815        let stmt = parse_statement("CREATE TABLE x (a my_user_type)").unwrap();
28816        let Statement::CreateTable(t) = stmt else {
28817            panic!("expected CreateTable");
28818        };
28819        assert_eq!(t.columns[0].user_type_ref.as_deref(), Some("my_user_type"));
28820    }
28821
28822    #[test]
28823    fn create_table_missing_table_keyword_errors() {
28824        assert!(parse_statement("CREATE x (a INT)").is_err());
28825    }
28826
28827    // v7.37.6-B(sentori Epic 2 P0)— `PARTITION BY RANGE` parent +
28828    // `PARTITION OF parent <bounds>` child parse + Display round-trip.
28829
28830    #[test]
28831    fn parse_create_table_partition_by_range() {
28832        use crate::ast::{PartitionBySpec, PartitionKindAst};
28833        let stmt = parse_statement(
28834            "CREATE TABLE events_partitioned (id BIGINT NOT NULL, ts TIMESTAMPTZ NOT NULL, \
28835             payload JSONB) PARTITION BY RANGE (ts)",
28836        )
28837        .unwrap();
28838        let Statement::CreateTable(t) = stmt else {
28839            panic!("expected CreateTable");
28840        };
28841        assert!(t.partition_of.is_none(), "parent has no partition_of");
28842        assert_eq!(t.columns.len(), 3);
28843        let by = t.partition_by.as_ref().expect("expected PARTITION BY");
28844        assert_eq!(
28845            by,
28846            &PartitionBySpec {
28847                kind: PartitionKindAst::Range,
28848                key_columns: alloc::vec!["ts".to_string()],
28849            }
28850        );
28851        // Display round-trip preserves the suffix. `quote_ident`
28852        // only adds double quotes when the ident needs escaping, so
28853        // a plain `ts` survives bare here.
28854        assert!(
28855            t.to_string().contains("PARTITION BY RANGE (ts)"),
28856            "Display lost PARTITION BY suffix: {t}"
28857        );
28858    }
28859
28860    #[test]
28861    fn parse_create_table_partition_of_range() {
28862        use crate::ast::{PartitionOfBoundsAst, PartitionOfSpec};
28863        let stmt = parse_statement(
28864            "CREATE TABLE events_2026_06 PARTITION OF events_partitioned \
28865             FOR VALUES FROM ('2026-06-01 00:00:00+00') TO ('2026-07-01 00:00:00+00')",
28866        )
28867        .unwrap();
28868        let Statement::CreateTable(t) = stmt else {
28869            panic!("expected CreateTable");
28870        };
28871        assert!(t.columns.is_empty(), "child inherits columns from parent");
28872        assert!(t.partition_by.is_none());
28873        let of = t.partition_of.as_ref().expect("expected PARTITION OF");
28874        assert_eq!(of.parent_name, "events_partitioned");
28875        let PartitionOfSpec { bounds, .. } = of.clone();
28876        match bounds {
28877            PartitionOfBoundsAst::Range { lower, upper } => {
28878                assert!(lower.to_string().contains("2026-06-01"));
28879                assert!(upper.to_string().contains("2026-07-01"));
28880            }
28881            other => panic!("expected Range, got {other:?}"),
28882        }
28883        // Display round-trip emits the FOR VALUES tail. `quote_ident`
28884        // skips quotes when not required, so the parent name appears
28885        // bare here.
28886        let s = t.to_string();
28887        assert!(
28888            s.contains("PARTITION OF events_partitioned"),
28889            "Display lost PARTITION OF: {s}"
28890        );
28891        assert!(s.contains("FOR VALUES FROM"), "Display lost FROM: {s}");
28892        assert!(s.contains(") TO ("), "Display lost TO: {s}");
28893    }
28894
28895    #[test]
28896    fn parse_create_table_partition_of_default() {
28897        use crate::ast::PartitionOfBoundsAst;
28898        let stmt =
28899            parse_statement("CREATE TABLE events_default PARTITION OF events_partitioned DEFAULT")
28900                .unwrap();
28901        let Statement::CreateTable(t) = stmt else {
28902            panic!("expected CreateTable");
28903        };
28904        let of = t.partition_of.as_ref().expect("expected PARTITION OF");
28905        assert_eq!(of.parent_name, "events_partitioned");
28906        assert!(matches!(of.bounds, PartitionOfBoundsAst::Default));
28907        assert!(
28908            t.to_string()
28909                .contains("PARTITION OF events_partitioned DEFAULT"),
28910            "Display lost DEFAULT: {t}"
28911        );
28912    }
28913
28914    #[test]
28915    fn parse_create_table_partition_by_list() {
28916        // v7.37.16 (16.1) — `PARTITION BY LIST (key)` parent + a
28917        // child with `FOR VALUES IN (lit, lit, …)`.
28918        use crate::ast::{PartitionBySpec, PartitionKindAst, PartitionOfBoundsAst};
28919        let parent =
28920            parse_statement("CREATE TABLE events_listed (region TEXT) PARTITION BY LIST (region)")
28921                .unwrap();
28922        let Statement::CreateTable(t) = parent else {
28923            panic!("expected CreateTable");
28924        };
28925        let Some(PartitionBySpec {
28926            kind,
28927            ref key_columns,
28928        }) = t.partition_by
28929        else {
28930            panic!("expected PARTITION BY");
28931        };
28932        assert_eq!(kind, PartitionKindAst::List);
28933        assert_eq!(*key_columns, vec!["region".to_string()]);
28934        assert!(t.to_string().contains("PARTITION BY LIST (region)"));
28935
28936        let child = parse_statement(
28937            "CREATE TABLE events_apac PARTITION OF events_listed \
28938             FOR VALUES IN ('jp', 'kr', 'tw')",
28939        )
28940        .unwrap();
28941        let Statement::CreateTable(c) = child else {
28942            panic!("expected CreateTable");
28943        };
28944        let of = c.partition_of.as_ref().expect("expected PARTITION OF");
28945        let PartitionOfBoundsAst::List { values } = &of.bounds else {
28946            panic!("expected List bounds, got {:?}", of.bounds);
28947        };
28948        assert_eq!(values.len(), 3);
28949        let disp = c.to_string();
28950        assert!(disp.contains("FOR VALUES IN ("), "Display lost IN: {disp}");
28951    }
28952
28953    #[test]
28954    fn parse_create_table_partition_by_hash() {
28955        // v7.37.16 (16.2) — `PARTITION BY HASH (key)` parent + a
28956        // child with `FOR VALUES WITH (MODULUS m, REMAINDER r)`.
28957        use crate::ast::{PartitionBySpec, PartitionKindAst, PartitionOfBoundsAst};
28958        let parent =
28959            parse_statement("CREATE TABLE orders_h (id BIGINT) PARTITION BY HASH (id)").unwrap();
28960        let Statement::CreateTable(t) = parent else {
28961            panic!("expected CreateTable");
28962        };
28963        let Some(PartitionBySpec {
28964            kind,
28965            ref key_columns,
28966        }) = t.partition_by
28967        else {
28968            panic!("expected PARTITION BY");
28969        };
28970        assert_eq!(kind, PartitionKindAst::Hash);
28971        assert_eq!(*key_columns, vec!["id".to_string()]);
28972        assert!(t.to_string().contains("PARTITION BY HASH (id)"));
28973
28974        let child = parse_statement(
28975            "CREATE TABLE orders_h_0 PARTITION OF orders_h \
28976             FOR VALUES WITH (MODULUS 4, REMAINDER 0)",
28977        )
28978        .unwrap();
28979        let Statement::CreateTable(c) = child else {
28980            panic!("expected CreateTable");
28981        };
28982        let of = c.partition_of.as_ref().expect("expected PARTITION OF");
28983        let PartitionOfBoundsAst::Hash { modulus, remainder } = of.bounds else {
28984            panic!("expected Hash bounds");
28985        };
28986        assert_eq!(modulus, 4);
28987        assert_eq!(remainder, 0);
28988        let disp = c.to_string();
28989        assert!(
28990            disp.contains("FOR VALUES WITH (MODULUS 4, REMAINDER 0)"),
28991            "Display lost HASH bounds: {disp}"
28992        );
28993
28994        // Validation: REMAINDER ≥ MODULUS is rejected at parse time.
28995        let bad = parse_statement(
28996            "CREATE TABLE orders_h_bad PARTITION OF orders_h \
28997             FOR VALUES WITH (MODULUS 4, REMAINDER 4)",
28998        );
28999        let msg = format!("{}", bad.unwrap_err());
29000        assert!(
29001            msg.contains("REMAINDER") && msg.contains("MODULUS"),
29002            "expected REMAINDER/MODULUS validation error: {msg}"
29003        );
29004    }
29005
29006    #[test]
29007    fn parse_create_table_partition_of_rejects_columns() {
29008        // v7.37.6-B contract: PARTITION OF children inherit columns
29009        // from the parent; an explicit list MUST surface as a parse
29010        // error rather than getting silently ignored.
29011        let err = parse_statement(
29012            "CREATE TABLE events_2026_06 PARTITION OF events_partitioned (id BIGINT) \
29013             FOR VALUES FROM ('a') TO ('b')",
29014        );
29015        assert!(err.is_err(), "expected parse error for explicit columns");
29016        let msg = format!("{}", err.unwrap_err());
29017        assert!(
29018            msg.contains("PARTITION OF") && msg.contains("column"),
29019            "error should mention PARTITION OF + columns: {msg}"
29020        );
29021    }
29022
29023    #[test]
29024    fn insert_single_value() {
29025        let s = parse("INSERT INTO foo VALUES (42)");
29026        let Statement::Insert(i) = s else {
29027            panic!("expected Insert")
29028        };
29029        assert_eq!(i.table, "foo");
29030        assert_eq!(i.rows.len(), 1);
29031        assert_eq!(i.rows[0].len(), 1);
29032        assert!(matches!(i.rows[0][0], Expr::Literal(Literal::Integer(42))));
29033    }
29034
29035    #[test]
29036    fn insert_multi_value_with_mixed_literals() {
29037        let s = parse("INSERT INTO foo VALUES (1, 'hi', 3.14, TRUE, NULL)");
29038        let Statement::Insert(i) = s else { panic!() };
29039        assert_eq!(i.rows.len(), 1);
29040        assert_eq!(i.rows[0].len(), 5);
29041    }
29042
29043    #[test]
29044    fn insert_missing_into_errors() {
29045        assert!(parse_statement("INSERT foo VALUES (1)").is_err());
29046    }
29047
29048    #[test]
29049    fn create_table_round_trip() {
29050        let original =
29051            parse("CREATE TABLE foo (id BIGINT NOT NULL, label TEXT, score FLOAT NOT NULL)");
29052        let text = original.to_string();
29053        let again = parse_statement(&text).expect("re-parse");
29054        assert_eq!(original, again);
29055    }
29056
29057    #[test]
29058    fn insert_round_trip_with_negation_and_string() {
29059        let original = parse("INSERT INTO t VALUES (-1, 'it''s', NULL)");
29060        let text = original.to_string();
29061        let again = parse_statement(&text).expect("re-parse");
29062        assert_eq!(original, again);
29063    }
29064
29065    #[test]
29066    fn unknown_keyword_at_statement_start_errors() {
29067        // v4.4: UPDATE is real SQL now. Use a fabricated keyword so
29068        // the top-level dispatch still has no branch to take.
29069        let err = parse_statement("FROBNICATE foo SET x = 1").unwrap_err();
29070        assert_eq!(err.message, "syntax error at or near \"FROBNICATE\"");
29071    }
29072
29073    // --- v0.8 CREATE INDEX --------------------------------------------------
29074
29075    #[test]
29076    fn create_index_basic() {
29077        let s = parse("CREATE INDEX idx_id ON users (id)");
29078        let Statement::CreateIndex(c) = s else {
29079            panic!("expected CreateIndex")
29080        };
29081        assert_eq!(c.name, "idx_id");
29082        assert_eq!(c.table, "users");
29083        assert_eq!(c.column, "id");
29084    }
29085
29086    #[test]
29087    fn create_index_missing_on_errors() {
29088        assert!(parse_statement("CREATE INDEX foo users (id)").is_err());
29089    }
29090
29091    #[test]
29092    fn create_index_missing_paren_errors() {
29093        assert!(parse_statement("CREATE INDEX foo ON users id").is_err());
29094    }
29095
29096    #[test]
29097    fn create_index_round_trip() {
29098        let original = parse("CREATE INDEX by_name ON users (name)");
29099        let again = parse_statement(&original.to_string()).unwrap();
29100        assert_eq!(original, again);
29101    }
29102
29103    // --- v7.9.29 CREATE UNIQUE INDEX [WHERE pred] (mailrs K1) -------------
29104
29105    #[test]
29106    fn create_unique_index_basic() {
29107        let s = parse("CREATE UNIQUE INDEX uq_x ON t (a)");
29108        let Statement::CreateIndex(c) = s else {
29109            panic!("expected CreateIndex");
29110        };
29111        assert!(c.is_unique);
29112        assert_eq!(c.column, "a");
29113        assert!(c.partial_predicate.is_none());
29114    }
29115
29116    #[test]
29117    fn create_unique_index_partial() {
29118        // mailrs's email_templates "one default per user" shape.
29119        let s = parse(
29120            "CREATE UNIQUE INDEX idx_email_templates_user_default \
29121             ON email_templates (user_address) WHERE is_default = true",
29122        );
29123        let Statement::CreateIndex(c) = s else {
29124            panic!("expected CreateIndex");
29125        };
29126        assert!(c.is_unique);
29127        assert_eq!(c.table, "email_templates");
29128        assert_eq!(c.column, "user_address");
29129        assert!(c.partial_predicate.is_some());
29130    }
29131
29132    #[test]
29133    fn create_unique_index_composite_with_predicate() {
29134        // mailrs's calendar_events instance: composite columns.
29135        let s = parse(
29136            "CREATE UNIQUE INDEX uq_calendar_events_instance \
29137             ON calendar_events (calendar_id, uid, recurrence_id) \
29138             WHERE recurrence_id IS NOT NULL",
29139        );
29140        let Statement::CreateIndex(c) = s else {
29141            panic!("expected CreateIndex");
29142        };
29143        assert!(c.is_unique);
29144        assert_eq!(c.column, "calendar_id");
29145        assert_eq!(
29146            c.extra_columns,
29147            vec!["uid".to_string(), "recurrence_id".to_string()]
29148        );
29149        assert!(c.partial_predicate.is_some());
29150    }
29151
29152    #[test]
29153    fn create_unique_index_using_btree_ok() {
29154        let s = parse("CREATE UNIQUE INDEX uq_x ON t USING btree (a)");
29155        assert!(matches!(s, Statement::CreateIndex(ref c) if c.is_unique));
29156    }
29157
29158    #[test]
29159    fn create_unique_index_using_hnsw_rejected() {
29160        let err =
29161            parse_statement("CREATE UNIQUE INDEX uq_v ON t USING hnsw (embedding)").unwrap_err();
29162        assert!(err.message.contains("UNIQUE"), "{}", err.message);
29163    }
29164
29165    #[test]
29166    fn create_unique_index_round_trip() {
29167        let original = parse(
29168            "CREATE UNIQUE INDEX uq_calendar_events_master \
29169             ON calendar_events (calendar_id, uid) WHERE recurrence_id IS NULL",
29170        );
29171        let again = parse_statement(&original.to_string()).unwrap();
29172        assert_eq!(original, again);
29173    }
29174
29175    #[test]
29176    fn create_unique_without_index_errors() {
29177        let err = parse_statement("CREATE UNIQUE TABLE t (a INT)").unwrap_err();
29178        // v7.39 (round 340, V56) — PG 18.4, verbatim.
29179        assert_eq!(err.message, "syntax error at or near \"TABLE\"");
29180    }
29181
29182    // --- v7.10.4 BYTES / BYTEA column type (Epic 1) ----------------------
29183
29184    #[test]
29185    fn create_table_bytea_column() {
29186        let s = parse("CREATE TABLE t (id INT NOT NULL, payload BYTEA NOT NULL)");
29187        let Statement::CreateTable(c) = s else {
29188            panic!("expected CreateTable");
29189        };
29190        assert_eq!(c.columns.len(), 2);
29191        assert_eq!(c.columns[1].ty, ColumnTypeName::Bytes);
29192        assert!(!c.columns[1].nullable);
29193    }
29194
29195    #[test]
29196    fn create_table_bytes_alias_column() {
29197        let s = parse("CREATE TABLE t (blob BYTES)");
29198        let Statement::CreateTable(c) = s else {
29199            panic!("expected CreateTable");
29200        };
29201        assert_eq!(c.columns[0].ty, ColumnTypeName::Bytes);
29202    }
29203
29204    #[test]
29205    fn bytea_round_trip_display() {
29206        let original = parse("CREATE TABLE t (a BYTEA NOT NULL)");
29207        let again = parse_statement(&original.to_string()).unwrap();
29208        assert_eq!(original, again);
29209    }
29210
29211    // --- v0.9 transactions -------------------------------------------------
29212
29213    #[test]
29214    fn begin_commit_rollback_parse_as_unit_variants() {
29215        let plain = crate::ast::TransactionModes::default();
29216        assert_eq!(parse("BEGIN"), Statement::Begin(plain));
29217        assert_eq!(parse("COMMIT"), Statement::Commit);
29218        // r1066 — PG synonyms pgbench's tpcb script relies on.
29219        assert_eq!(parse("END"), Statement::Commit);
29220        assert_eq!(parse("END TRANSACTION"), Statement::Commit);
29221        assert_eq!(parse("COMMIT WORK"), Statement::Commit);
29222        assert_eq!(parse("ROLLBACK"), Statement::Rollback);
29223        // Trailing semicolons accepted too.
29224        assert_eq!(parse("BEGIN;"), Statement::Begin(plain));
29225        // v7.39 (read01 round 118, B3) — an explicit ISOLATION LEVEL rides the
29226        // statement (with or without the WORK/TRANSACTION noise word).
29227        assert_eq!(
29228            parse("BEGIN ISOLATION LEVEL REPEATABLE READ"),
29229            Statement::Begin(crate::ast::TransactionModes {
29230                isolation: Some(IsolationLevel::RepeatableRead),
29231                read_only: None,
29232            })
29233        );
29234        assert_eq!(
29235            parse("START TRANSACTION ISOLATION LEVEL SERIALIZABLE"),
29236            Statement::Begin(crate::ast::TransactionModes {
29237                isolation: Some(IsolationLevel::Serializable),
29238                read_only: None,
29239            })
29240        );
29241        // v7.39 — this line used to read
29242        //
29243        //     // A non-isolation mode keeps the session default (None).
29244        //     assert_eq!(parse("BEGIN READ ONLY"), Statement::Begin(None));
29245        //
29246        // which pinned the defect rather than catching it: the READ ONLY
29247        // was thrown away, so the statement opened an ordinary read-write
29248        // transaction and every write inside it was accepted. The
29249        // isolation level is still absent here, because this statement
29250        // does not name one — that part was right.
29251        assert_eq!(
29252            parse("BEGIN READ ONLY"),
29253            Statement::Begin(crate::ast::TransactionModes {
29254                isolation: None,
29255                read_only: Some(true),
29256            })
29257        );
29258        assert_eq!(
29259            parse("START TRANSACTION READ WRITE"),
29260            Statement::Begin(crate::ast::TransactionModes {
29261                isolation: None,
29262                read_only: Some(false),
29263            })
29264        );
29265        assert_eq!(
29266            parse("BEGIN ISOLATION LEVEL SERIALIZABLE, READ ONLY"),
29267            Statement::Begin(crate::ast::TransactionModes {
29268                isolation: Some(IsolationLevel::Serializable),
29269                read_only: Some(true),
29270            })
29271        );
29272    }
29273
29274    // --- v1.2: pgvector distance ops + ::vector cast --------------------
29275
29276    #[test]
29277    fn inner_product_binop_parses() {
29278        let s = parse("SELECT v <#> [1.0, 2.0] FROM t");
29279        let Statement::Select(s) = s else { panic!() };
29280        let SelectItem::Expr { expr, .. } = &s.items[0] else {
29281            panic!()
29282        };
29283        assert!(matches!(
29284            expr,
29285            Expr::Binary {
29286                op: BinOp::InnerProduct,
29287                ..
29288            }
29289        ));
29290    }
29291
29292    #[test]
29293    fn cosine_distance_binop_parses() {
29294        let s = parse("SELECT v <=> [1.0, 2.0] FROM t");
29295        let Statement::Select(s) = s else { panic!() };
29296        let SelectItem::Expr { expr, .. } = &s.items[0] else {
29297            panic!()
29298        };
29299        assert!(matches!(
29300            expr,
29301            Expr::Binary {
29302                op: BinOp::CosineDistance,
29303                ..
29304            }
29305        ));
29306    }
29307
29308    #[test]
29309    fn vector_cast_postfix_wraps_string_literal() {
29310        let s = parse("SELECT '[1,2,3]'::vector FROM t");
29311        let Statement::Select(s) = s else { panic!() };
29312        let SelectItem::Expr { expr, .. } = &s.items[0] else {
29313            panic!()
29314        };
29315        assert!(matches!(
29316            expr,
29317            Expr::Cast {
29318                target: CastTarget::Vector,
29319                ..
29320            }
29321        ));
29322    }
29323
29324    #[test]
29325    fn unsupported_cast_target_errors() {
29326        // v7.37.5 ship triage promoted the parser to accept every
29327        // ident as a `CastTarget::Named(canonical)`; the engine
29328        // surfaces the "unsupported cast target" error at eval
29329        // time when `type_name_to_data_type` can't resolve it.
29330        // Parser-side error now requires a NON-ident after `::`
29331        // (e.g. a punctuation token).
29332        let err = parse_statement("SELECT 1::, FROM t").unwrap_err();
29333        assert_eq!(err.message, "syntax error at or near \",\"");
29334    }
29335
29336    #[test]
29337    fn tx_statements_round_trip() {
29338        for q in ["BEGIN", "COMMIT", "ROLLBACK"] {
29339            let original = parse(q);
29340            let again = parse_statement(&original.to_string()).unwrap();
29341            assert_eq!(original, again);
29342        }
29343    }
29344
29345    #[test]
29346    fn interval_text_parsing_units() {
29347        // v7.37.5 β — three-field shape `(months, days, micros)` so
29348        // `'1 day'` and `'24 hours'` no longer collide (PG parity).
29349        // Single unit.
29350        assert_eq!(parse_interval_text("1 day"), Some((0, 1, 0)));
29351        assert_eq!(
29352            parse_interval_text("24 hours"),
29353            Some((0, 0, 86_400_000_000))
29354        );
29355        assert_eq!(parse_interval_text("1 second"), Some((0, 0, 1_000_000)));
29356        assert_eq!(parse_interval_text("1 month"), Some((1, 0, 0)));
29357        assert_eq!(parse_interval_text("2 years"), Some((24, 0, 0)));
29358        assert_eq!(parse_interval_text("1 week"), Some((0, 7, 0)));
29359        // Compound spans accumulate per-dimension.
29360        assert_eq!(parse_interval_text("1 year 6 months"), Some((18, 0, 0)));
29361        assert_eq!(
29362            parse_interval_text("1 day 2 hours"),
29363            Some((0, 1, 7_200_000_000))
29364        );
29365        // Negative numbers carry through per-dimension.
29366        assert_eq!(parse_interval_text("-1 day"), Some((0, -1, 0)));
29367        // Bad shapes return None.
29368        assert_eq!(parse_interval_text(""), None);
29369        assert_eq!(parse_interval_text("garbage"), None);
29370        assert_eq!(parse_interval_text("1 fortnight"), None);
29371        // v7.39 (GUC knife 3) — PG reads a bare number as SECONDS
29372        // (`INTERVAL '1'` = 00:00:01), verified against the oracle.
29373        assert_eq!(parse_interval_text("1"), Some((0, 0, 1_000_000)));
29374        assert_eq!(parse_interval_text("0"), Some((0, 0, 0)));
29375        assert_eq!(parse_interval_text("1.5"), Some((0, 0, 1_500_000)));
29376    }
29377
29378    #[test]
29379    fn interval_literal_roundtrips_via_display() {
29380        let parsed = parse("SELECT INTERVAL '1 day 2 hours'");
29381        let s = parsed.to_string();
29382        // Display preserves the original text verbatim.
29383        assert!(s.contains("INTERVAL '1 day 2 hours'"), "got: {s}");
29384        // And re-parsing yields a structurally equal statement.
29385        let again = parse_statement(&s).unwrap();
29386        assert_eq!(parsed, again);
29387    }
29388
29389    // ── v6.1.2: CREATE / DROP PUBLICATION ────────────────────
29390
29391    #[test]
29392    fn parser_recognises_create_publication_bare() {
29393        let s = parse("CREATE PUBLICATION pub_a");
29394        let Statement::CreatePublication(p) = s else {
29395            panic!("expected CreatePublication, got {s:?}")
29396        };
29397        assert_eq!(p.name, "pub_a");
29398        assert_eq!(p.scope, PublicationScope::AllTables);
29399    }
29400
29401    #[test]
29402    fn parser_recognises_create_publication_for_all_tables() {
29403        let s = parse("CREATE PUBLICATION pub_a FOR ALL TABLES");
29404        let Statement::CreatePublication(p) = s else {
29405            panic!("expected CreatePublication, got {s:?}")
29406        };
29407        assert_eq!(p.name, "pub_a");
29408        assert_eq!(p.scope, PublicationScope::AllTables);
29409    }
29410
29411    #[test]
29412    fn parser_recognises_drop_publication() {
29413        let s = parse("DROP PUBLICATION pub_a");
29414        let Statement::DropPublication { name, .. } = s else {
29415            panic!("expected DropPublication, got {s:?}")
29416        };
29417        assert_eq!(name, "pub_a");
29418    }
29419
29420    #[test]
29421    fn parser_recognises_for_table_list() {
29422        let s = parse("CREATE PUBLICATION pub_a FOR TABLE t1, t2, t3");
29423        let Statement::CreatePublication(p) = s else {
29424            panic!("expected CreatePublication, got {s:?}")
29425        };
29426        assert_eq!(p.name, "pub_a");
29427        let PublicationScope::ForTables(ts) = p.scope else {
29428            panic!("expected ForTables scope")
29429        };
29430        assert_eq!(ts, alloc::vec!["t1", "t2", "t3"]);
29431    }
29432
29433    #[test]
29434    fn parser_rejects_bare_for_tables_and_takes_in_schema() {
29435        // v7.39 (round 754, F31-B5) — PG18-measured: the bare plural
29436        // is rejected (`invalid publication object list`; the old
29437        // test pinned an unverifiable "PG 19 accepts both" claim);
29438        // TABLES pairs with IN SCHEMA.
29439        let err = parse_statement("CREATE PUBLICATION pub_a FOR TABLES t1, t2")
29440            .expect_err("bare FOR TABLES must reject");
29441        assert!(
29442            alloc::format!("{err}").contains("invalid publication object list"),
29443            "got: {err}"
29444        );
29445        let s = parse("CREATE PUBLICATION pub_a FOR TABLES IN SCHEMA public");
29446        let Statement::CreatePublication(p) = s else {
29447            panic!("expected CreatePublication, got {s:?}")
29448        };
29449        let PublicationScope::TablesInSchema(schema) = p.scope else {
29450            panic!("expected TablesInSchema")
29451        };
29452        assert_eq!(schema, "public");
29453    }
29454
29455    #[test]
29456    fn parser_recognises_for_all_tables_except_list() {
29457        let s = parse("CREATE PUBLICATION p FOR ALL TABLES EXCEPT t1, t2");
29458        let Statement::CreatePublication(p) = s else {
29459            panic!()
29460        };
29461        let PublicationScope::AllTablesExcept(ts) = p.scope else {
29462            panic!("expected AllTablesExcept")
29463        };
29464        assert_eq!(ts, alloc::vec!["t1", "t2"]);
29465    }
29466
29467    #[test]
29468    fn parser_rejects_for_table_with_empty_list() {
29469        // `FOR TABLE` with nothing after is a parse error.
29470        let err = parse_statement("CREATE PUBLICATION p FOR TABLE")
29471            .expect_err("must error on empty list");
29472        // No specific message asserted — the call falls through to
29473        // expect_ident_like which yields "expected identifier, got …".
29474        assert!(!err.message.is_empty());
29475    }
29476
29477    #[test]
29478    fn parser_recognises_show_publications() {
29479        // v6.1.3 — SHOW PUBLICATIONS lands here. PUBLICATIONS is a
29480        // bare ident in this position, NOT a reserved keyword.
29481        let s = parse("SHOW PUBLICATIONS");
29482        assert!(matches!(s, Statement::ShowPublications));
29483    }
29484
29485    // ── v6.1.4: CREATE / DROP SUBSCRIPTION + SHOW SUBSCRIPTIONS ─
29486
29487    #[test]
29488    fn parser_recognises_create_subscription_single_publication() {
29489        let s = parse(
29490            "CREATE SUBSCRIPTION sub_a CONNECTION 'host=127.0.0.1 port=20002' PUBLICATION pub_a",
29491        );
29492        let Statement::CreateSubscription(c) = s else {
29493            panic!("expected CreateSubscription, got {s:?}")
29494        };
29495        assert_eq!(c.name, "sub_a");
29496        assert_eq!(c.conn_str, "host=127.0.0.1 port=20002");
29497        assert_eq!(c.publications, alloc::vec!["pub_a"]);
29498    }
29499
29500    #[test]
29501    fn parser_recognises_create_subscription_multi_publication() {
29502        let s = parse("CREATE SUBSCRIPTION sub_a CONNECTION 'host=h' PUBLICATION p1, p2, p3");
29503        let Statement::CreateSubscription(c) = s else {
29504            panic!()
29505        };
29506        assert_eq!(c.publications, alloc::vec!["p1", "p2", "p3"]);
29507    }
29508
29509    #[test]
29510    fn parser_rejects_create_subscription_missing_connection() {
29511        let err = parse_statement("CREATE SUBSCRIPTION s PUBLICATION p")
29512            .expect_err("must error on missing CONNECTION");
29513        assert_eq!(err.message, "syntax error at or near \"PUBLICATION\"");
29514    }
29515
29516    #[test]
29517    fn parser_rejects_create_subscription_missing_publication() {
29518        let err = parse_statement("CREATE SUBSCRIPTION s CONNECTION 'host=x'")
29519            .expect_err("must error on missing PUBLICATION");
29520        assert_eq!(err.message, "syntax error at end of input");
29521    }
29522
29523    #[test]
29524    fn parser_recognises_drop_subscription() {
29525        let s = parse("DROP SUBSCRIPTION sub_a");
29526        let Statement::DropSubscription { name, .. } = s else {
29527            panic!("expected DropSubscription, got {s:?}")
29528        };
29529        assert_eq!(name, "sub_a");
29530    }
29531
29532    #[test]
29533    fn parser_recognises_show_subscriptions() {
29534        let s = parse("SHOW SUBSCRIPTIONS");
29535        assert!(matches!(s, Statement::ShowSubscriptions));
29536    }
29537
29538    #[test]
29539    fn parser_recognises_wait_for_wal_position_no_timeout() {
29540        let s = parse("WAIT FOR WAL POSITION 12345");
29541        let Statement::WaitForWalPosition { pos, timeout_ms } = s else {
29542            panic!("expected WaitForWalPosition, got {s:?}")
29543        };
29544        assert_eq!(pos, 12345);
29545        assert!(timeout_ms.is_none());
29546    }
29547
29548    #[test]
29549    fn parser_recognises_wait_for_wal_position_with_timeout() {
29550        let s = parse("WAIT FOR WAL POSITION 67890 WITH TIMEOUT 5000");
29551        let Statement::WaitForWalPosition { pos, timeout_ms } = s else {
29552            panic!()
29553        };
29554        assert_eq!(pos, 67890);
29555        assert_eq!(timeout_ms, Some(5000));
29556    }
29557
29558    #[test]
29559    fn parser_rejects_wait_with_negative_position() {
29560        // The lexer treats `-` as a token; `expect_u64_literal`
29561        // only sees the Integer that follows, so the negative
29562        // arrives as a unary-minus expression at higher levels.
29563        // Bare `WAIT FOR WAL POSITION -1` thus surfaces as a
29564        // parse error one way or another.
29565        let err = parse_statement("WAIT FOR WAL POSITION -1").unwrap_err();
29566        assert!(!err.message.is_empty());
29567    }
29568
29569    #[test]
29570    fn parser_recognises_bare_analyze() {
29571        let s = parse("ANALYZE");
29572        assert!(matches!(s, Statement::Analyze(None)));
29573    }
29574
29575    #[test]
29576    fn parser_recognises_analyze_with_table() {
29577        let s = parse("ANALYZE users");
29578        let Statement::Analyze(Some(name)) = s else {
29579            panic!("expected Analyze, got {s:?}")
29580        };
29581        assert_eq!(name, "users");
29582    }
29583
29584    #[test]
29585    fn parser_recognises_analyze_with_quoted_table() {
29586        let s = parse("ANALYZE \"Mixed Case\"");
29587        let Statement::Analyze(Some(name)) = s else {
29588            panic!()
29589        };
29590        assert_eq!(name, "Mixed Case");
29591    }
29592
29593    #[test]
29594    fn parser_rejects_analyze_with_garbage_token() {
29595        let err = parse_statement("ANALYZE 42").expect_err("must error");
29596        assert!(!err.message.is_empty());
29597    }
29598
29599    #[test]
29600    fn analyze_display_roundtrips() {
29601        for sql in ["ANALYZE", "ANALYZE users"] {
29602            let s = parse(sql);
29603            let printed = s.to_string();
29604            let again = parse_statement(&printed)
29605                .unwrap_or_else(|e| panic!("re-parse failed for {printed:?}: {e}"));
29606            assert_eq!(s, again);
29607        }
29608    }
29609
29610    #[test]
29611    fn wait_for_display_roundtrips() {
29612        for sql in [
29613            "WAIT FOR WAL POSITION 12345",
29614            "WAIT FOR WAL POSITION 67890 WITH TIMEOUT 5000",
29615        ] {
29616            let s = parse(sql);
29617            let printed = s.to_string();
29618            let again = parse_statement(&printed)
29619                .unwrap_or_else(|e| panic!("re-parse failed for {printed:?}: {e}"));
29620            assert_eq!(s, again, "round-trip mismatch for {sql:?}");
29621        }
29622    }
29623
29624    #[test]
29625    fn subscription_ddl_display_roundtrips() {
29626        for sql in [
29627            "CREATE SUBSCRIPTION sub_a CONNECTION 'host=h port=20002' PUBLICATION pub_a",
29628            "CREATE SUBSCRIPTION sub_b CONNECTION 'host=h' PUBLICATION p1, p2",
29629            "DROP SUBSCRIPTION sub_a",
29630            "SHOW SUBSCRIPTIONS",
29631        ] {
29632            let s = parse(sql);
29633            let printed = s.to_string();
29634            let again = parse_statement(&printed)
29635                .unwrap_or_else(|e| panic!("re-parse failed for {printed:?}: {e}"));
29636            assert_eq!(s, again, "round-trip mismatch for {sql:?}");
29637        }
29638    }
29639
29640    #[test]
29641    fn parser_drop_dispatches_user_vs_publication() {
29642        // Pre-v6.1.2 DROP USER took the bare-ident path; v6.1.2
29643        // tokenises DROP. Both targets must still parse.
29644        let s = parse("DROP USER 'alice'");
29645        let Statement::DropUser { name, .. } = s else {
29646            panic!("expected DropUser, got {s:?}")
29647        };
29648        assert_eq!(name, "alice");
29649        // And DROP PUBLICATION lands the new variant.
29650        let s = parse("DROP PUBLICATION p1");
29651        assert!(matches!(s, Statement::DropPublication { .. }));
29652    }
29653
29654    #[test]
29655    fn publication_ddl_display_roundtrips() {
29656        // Every CREATE PUBLICATION variant must Display → parse →
29657        // same AST. v6.1.3 covers all three scope shapes.
29658        for sql in [
29659            "CREATE PUBLICATION pub_a",
29660            "CREATE PUBLICATION pub_a FOR ALL TABLES",
29661            "CREATE PUBLICATION pub_a FOR TABLE t1, t2",
29662            "CREATE PUBLICATION pub_a FOR ALL TABLES EXCEPT t1",
29663            "DROP PUBLICATION pub_a",
29664            "SHOW PUBLICATIONS",
29665        ] {
29666            let s = parse(sql);
29667            let printed = s.to_string();
29668            let again = parse_statement(&printed)
29669                .unwrap_or_else(|e| panic!("re-parse failed for {printed:?}: {e}"));
29670            assert_eq!(s, again, "round-trip mismatch for {sql:?}");
29671        }
29672    }
29673
29674    // --- v7.12.4: CREATE FUNCTION + CREATE TRIGGER + PL/pgSQL ---
29675
29676    #[test]
29677    fn create_function_returns_trigger_plpgsql_minimal() {
29678        let sql = "CREATE FUNCTION noop() RETURNS TRIGGER LANGUAGE plpgsql AS $$ BEGIN RETURN NEW; END; $$";
29679        let s = parse(sql);
29680        let Statement::CreateFunction(f) = s else {
29681            panic!("expected CreateFunction");
29682        };
29683        assert_eq!(f.name, "noop");
29684        assert!(!f.or_replace);
29685        assert!(f.args.is_empty());
29686        assert!(matches!(f.returns, FunctionReturn::Trigger));
29687        assert_eq!(f.language, "plpgsql");
29688        let FunctionBody::PlPgSql(block) = f.body else {
29689            panic!("expected PlPgSql body");
29690        };
29691        assert_eq!(block.statements.len(), 1);
29692        assert!(matches!(
29693            block.statements[0],
29694            PlPgSqlStmt::Return(ReturnTarget::New)
29695        ));
29696    }
29697
29698    #[test]
29699    fn create_function_or_replace_with_assignment() {
29700        // mailrs-shape trigger function: NEW.col := to_tsvector(...);
29701        // RETURN NEW.
29702        let sql = "CREATE OR REPLACE FUNCTION update_sv() RETURNS TRIGGER LANGUAGE plpgsql AS $$
29703BEGIN
29704  NEW.search_vector := to_tsvector('english', NEW.subject);
29705  RETURN NEW;
29706END;
29707$$";
29708        let s = parse(sql);
29709        let Statement::CreateFunction(f) = s else {
29710            panic!("expected CreateFunction");
29711        };
29712        assert!(f.or_replace);
29713        let FunctionBody::PlPgSql(block) = &f.body else {
29714            panic!("expected PlPgSql body");
29715        };
29716        assert_eq!(block.statements.len(), 2);
29717        // First statement: NEW.search_vector := to_tsvector(...)
29718        let PlPgSqlStmt::Assign { target, .. } = &block.statements[0] else {
29719            panic!("expected Assign as first stmt");
29720        };
29721        match target {
29722            AssignTarget::NewColumn(c) => assert_eq!(c, "search_vector"),
29723            other => panic!("expected NEW.col, got {other:?}"),
29724        }
29725        // Second statement: RETURN NEW
29726        assert!(matches!(
29727            block.statements[1],
29728            PlPgSqlStmt::Return(ReturnTarget::New)
29729        ));
29730    }
29731
29732    #[test]
29733    fn create_trigger_after_insert_or_update() {
29734        let sql = "CREATE TRIGGER tg AFTER INSERT OR UPDATE ON messages FOR EACH ROW EXECUTE FUNCTION update_sv()";
29735        let s = parse(sql);
29736        let Statement::CreateTrigger(t) = s else {
29737            panic!("expected CreateTrigger");
29738        };
29739        assert_eq!(t.name, "tg");
29740        assert_eq!(t.table, "messages");
29741        assert_eq!(t.timing, TriggerTiming::After);
29742        assert_eq!(t.events, vec![TriggerEvent::Insert, TriggerEvent::Update]);
29743        assert_eq!(t.for_each, TriggerForEach::Row);
29744        assert_eq!(t.function, "update_sv");
29745    }
29746
29747    #[test]
29748    fn create_trigger_before_delete_execute_procedure_alias() {
29749        // PG also accepts the legacy `EXECUTE PROCEDURE` spelling.
29750        let sql =
29751            "CREATE TRIGGER guard BEFORE DELETE ON t FOR EACH ROW EXECUTE PROCEDURE block_delete()";
29752        let s = parse(sql);
29753        let Statement::CreateTrigger(t) = s else {
29754            panic!("expected CreateTrigger");
29755        };
29756        assert_eq!(t.timing, TriggerTiming::Before);
29757        assert_eq!(t.events, vec![TriggerEvent::Delete]);
29758    }
29759
29760    #[test]
29761    fn drop_trigger_if_exists_round_trips() {
29762        // No parser support for DROP TRIGGER yet — added in v7.12.5
29763        // alongside the broader DROP …{IF EXISTS} cleanup. The
29764        // AST + Display impls are in place so we round-trip via
29765        // construction:
29766        let s = Statement::DropTrigger {
29767            name: "tg".into(),
29768            table: "messages".into(),
29769            if_exists: true,
29770        };
29771        assert_eq!(s.to_string(), "DROP TRIGGER IF EXISTS tg ON messages");
29772    }
29773
29774    #[test]
29775    fn trigger_ddl_display_roundtrips_through_parser() {
29776        // CREATE TRIGGER + its referenced CREATE FUNCTION must
29777        // Display → parse → same AST (modulo PL/pgSQL body
29778        // formatting which is parser-canonicalised).
29779        for sql in [
29780            "CREATE TRIGGER tg AFTER INSERT ON t FOR EACH ROW EXECUTE FUNCTION f()",
29781            "CREATE TRIGGER tg2 BEFORE UPDATE OR DELETE ON t FOR EACH ROW EXECUTE FUNCTION g()",
29782        ] {
29783            let s = parse(sql);
29784            let printed = s.to_string();
29785            let again = parse_statement(&printed)
29786                .unwrap_or_else(|e| panic!("re-parse failed for {printed:?}: {e}"));
29787            assert_eq!(s, again, "round-trip mismatch for {sql:?}");
29788        }
29789    }
29790}