Skip to main content

spg_sql/
ast.rs

1//! AST for the PG-dialect subset SPG accepts in v0.2.
2//!
3//! `Display` is implemented so that for any AST `a` produced by [`crate::parser`],
4//! re-parsing `format!("{a}")` yields a structurally equal AST. Binary and
5//! unary operators always emit parentheses to remove any precedence
6//! ambiguity — round-trip safety wins over prettiness.
7
8use alloc::boxed::Box;
9use alloc::format;
10use alloc::string::{String, ToString};
11use alloc::vec::Vec;
12use core::fmt;
13
14#[derive(Debug, Clone, PartialEq)]
15#[allow(clippy::large_enum_variant)] // Statement::Select dominates; Boxing would touch every match site
16pub enum Statement {
17    Select(SelectStatement),
18    CreateTable(CreateTableStatement),
19    /// v7.9.15 — `CREATE EXTENSION [IF NOT EXISTS] <name>
20    /// [WITH SCHEMA <s>] [VERSION <v>] [CASCADE]` accepted as a
21    /// no-op so PG dumps that include extension declarations
22    /// (notably `pgvector`) load against SPG without splitting
23    /// init scripts. mailrs migration follow-up F3.
24    CreateExtension(String),
25    /// v7.9.27 — PG `DO $$ … $$ [LANGUAGE plpgsql];` block. SPG
26    /// has no PL/pgSQL; engine returns CommandOk no-op so
27    /// `pg_dump` output with idempotent DO migrations loads
28    /// against SPG without splitting scripts. The lexer
29    /// consumes the dollar-quoted body into a discarded
30    /// Token::String. mailrs migration follow-up H1.
31    DoBlock,
32    CreateIndex(CreateIndexStatement),
33    Insert(InsertStatement),
34    /// v4.4 — `UPDATE <table> SET col=expr [, ...] [WHERE cond]`.
35    Update(UpdateStatement),
36    /// v4.4 — `DELETE FROM <table> [WHERE cond]`.
37    Delete(DeleteStatement),
38    Begin,
39    Commit,
40    Rollback,
41    /// `SAVEPOINT <name>` — push a named savepoint onto the active TX's
42    /// stack so a later `ROLLBACK TO <name>` can undo just the work
43    /// since this point.
44    Savepoint(String),
45    /// `ROLLBACK TO [SAVEPOINT] <name>` — restore catalog state to the
46    /// named savepoint and discard later savepoints. Does not end the
47    /// transaction.
48    RollbackToSavepoint(String),
49    /// `RELEASE [SAVEPOINT] <name>` — discard a savepoint without
50    /// rolling back. Keeps the work done since then.
51    ReleaseSavepoint(String),
52    /// `SHOW TABLES` — return the list of tables in the catalog.
53    ShowTables,
54    /// `SHOW COLUMNS FROM <table>` — return one row per column with
55    /// its declared name / type / nullability.
56    ShowColumns(String),
57    /// `CREATE USER 'name' WITH PASSWORD 'pw' ROLE 'admin'` (v4.1).
58    /// Role is optional; defaults to `readonly` when omitted.
59    CreateUser(CreateUserStatement),
60    /// `DROP USER 'name'` (v4.1).
61    DropUser(String),
62    /// `SHOW USERS` (v4.1) — admin-only listing of (name, role).
63    ShowUsers,
64    /// v4.26 — `EXPLAIN [ANALYZE] <select>`. The engine returns a
65    /// single-column text table describing the rewritten plan tree
66    /// for `inner`. `analyze` triggers an actual exec to attach
67    /// observed row counts and elapsed micros to each node.
68    Explain(ExplainStatement),
69    /// v6.0.4 — `ALTER INDEX <name> REBUILD [WITH (encoding = ...)]`.
70    /// Synchronous rebuild of an NSW index. With the optional
71    /// encoding clause, every stored cell at the indexed column is
72    /// also re-encoded through `coerce_value` before the new graph
73    /// builds.
74    AlterIndex(AlterIndexStatement),
75    /// v6.7.2 — `ALTER TABLE <name> SET <setting> = <value>`.
76    /// The only setting in v6.7.2 is `hot_tier_bytes`, which
77    /// overrides the global `SPG_HOT_TIER_BYTES` freezer trigger
78    /// for the named table.
79    AlterTable(AlterTableStatement),
80    /// v6.1.2 — `CREATE PUBLICATION <name> [FOR ALL TABLES]`.
81    /// The catalog row lives in `spg_publications`. Publisher-side
82    /// WAL filtering arrives in v6.1.5.
83    CreatePublication(CreatePublicationStatement),
84    /// v6.1.2 — `DROP PUBLICATION <name>`. PG-compatible silent
85    /// no-op when the publication does not exist.
86    DropPublication(String),
87    /// v6.1.3 — `SHOW PUBLICATIONS`. Returns one row per
88    /// publication ordered by name with `(name, scope_summary,
89    /// table_count)` columns. The scope summary is the human-
90    /// readable form `ALL TABLES` / `FOR TABLE …` / `FOR ALL
91    /// TABLES EXCEPT …`; `table_count` is `NULL` for the
92    /// `AllTables` scope and the table-list length otherwise.
93    ShowPublications,
94    /// v6.1.4 — `CREATE SUBSCRIPTION <name> CONNECTION '<conn>'
95    /// PUBLICATION <pub_name> [, <pub_name> …]`. Catalog lands
96    /// in `spg_subscriptions`; when the subscription is
97    /// `enabled = true` (default) the server spawns a
98    /// background worker that connects to `conn` and drains the
99    /// requested publication(s) into the local engine.
100    CreateSubscription(CreateSubscriptionStatement),
101    /// v6.1.4 — `DROP SUBSCRIPTION <name>`. Like DROP
102    /// PUBLICATION, silent no-op when absent. Stops the
103    /// associated worker thread before removing the row.
104    DropSubscription(String),
105    /// v6.1.4 — `SHOW SUBSCRIPTIONS`. Returns one row per
106    /// subscription ordered by name with `(name, conn_str,
107    /// publications, enabled, last_received_pos)`.
108    ShowSubscriptions,
109    /// v6.1.7 — `WAIT FOR WAL POSITION <pos> [WITH TIMEOUT <ms>]`.
110    /// Blocks until the local server's apply position reaches
111    /// `<pos>` or `<ms>` elapses. Server-layer command: the
112    /// engine refuses it (`EngineError::Unsupported`) since
113    /// `lag_state` lives in `spg-server`'s `ServerState`.
114    WaitForWalPosition {
115        pos: u64,
116        /// `None` → wait forever; `Some(ms)` → return after `ms`
117        /// milliseconds even if the target isn't reached.
118        timeout_ms: Option<u64>,
119    },
120    /// v6.2.0 — `ANALYZE [<table>]`. Bare form walks every user
121    /// table; `ANALYZE <name>` re-stats just one. Populates
122    /// `spg_statistic` with per-column null_frac + n_distinct +
123    /// 100-bucket equi-depth histogram.
124    Analyze(Option<String>),
125    /// v6.7.3 — `COMPACT COLD SEGMENTS`. Walks every user table's
126    /// BTree-cold indices and merges small cold-tier segments
127    /// (size below `SPG_COMPACTION_TARGET_SEGMENT_BYTES`, default
128    /// 4 MiB) into a single larger segment per (table, index).
129    /// `WHERE` predicate filtering on which tables to compact is
130    /// carved out of v6.7.3 (per V6_7_DESIGN.md STABILITY entry);
131    /// v6.7.3 only supports the bare form.
132    CompactColdSegments,
133    /// v7.12.1 — `SET <name> [TO|=] <value>`. Records a session
134    /// parameter on the engine; v7.12.1 honours
135    /// `default_text_search_config` (consumed by `to_tsvector` /
136    /// `plainto_tsquery` family when called without an explicit
137    /// config arg). All other names are accepted as a no-op so PG
138    /// dumps with `SET client_encoding`, `SET search_path` etc.
139    /// load cleanly.
140    SetParameter {
141        name: String,
142        value: SetValue,
143    },
144    /// v7.12.1 — `RESET <name>` / `RESET ALL`. Restores parameter
145    /// to its default. No-op for parameters SPG does not track.
146    ResetParameter(Option<String>),
147    /// v7.12.4 — `CREATE [OR REPLACE] FUNCTION name(args) RETURNS
148    /// <type> [LANGUAGE <lang>] AS $$ body $$ [LANGUAGE <lang>]`.
149    /// v7.12.4 ships `plpgsql` for `RETURNS TRIGGER` bodies (the
150    /// CREATE TRIGGER + AFTER/BEFORE row-level pipeline). Other
151    /// languages parse but error at exec time with a clear
152    /// unsupported message.
153    CreateFunction(CreateFunctionStatement),
154    /// v7.12.4 — `CREATE [OR REPLACE] TRIGGER name {BEFORE|AFTER}
155    /// {INSERT|UPDATE|DELETE} [OR ...] ON tbl FOR EACH ROW
156    /// EXECUTE {FUNCTION|PROCEDURE} fn_name()`. STATEMENT-level
157    /// triggers and column-list / WHEN clauses are out of scope
158    /// for v7.12.4.
159    CreateTrigger(CreateTriggerStatement),
160    /// v7.12.4 — `DROP TRIGGER [IF EXISTS] name ON tbl`. Silent
161    /// no-op when missing if `IF EXISTS` is set.
162    DropTrigger {
163        name: String,
164        table: String,
165        if_exists: bool,
166    },
167    /// v7.12.4 — `DROP FUNCTION [IF EXISTS] name`. Same shape as
168    /// DROP TRIGGER but global (no table scope).
169    DropFunction {
170        name: String,
171        if_exists: bool,
172    },
173}
174
175/// v7.12.1 — payload of a SET right-hand side. PG syntax accepts
176/// a string literal, an identifier (often a config name), an
177/// integer/float, or the bare `DEFAULT` keyword.
178#[derive(Debug, Clone, PartialEq)]
179pub enum SetValue {
180    String(String),
181    Ident(String),
182    Number(String),
183    Default,
184}
185
186/// v6.1.4 — `CREATE SUBSCRIPTION` AST node. v6.1.4 ships a
187/// single fixed-shape DDL; the WITH-clause options PG supports
188/// (`enabled`, `slot_name`, `streaming`, `binary`) are out of
189/// scope for v6.1.4 — `enabled` defaults to true and there are
190/// no other knobs to set in v6.1.x.
191#[derive(Debug, Clone, PartialEq, Eq)]
192pub struct CreateSubscriptionStatement {
193    pub name: String,
194    /// Connection string in PG keyword=value form (e.g.
195    /// `host=127.0.0.1 port=20002`). v6.1.4 only consumes the
196    /// `host` and `port` fields; the rest is reserved for
197    /// future v6.1.x options.
198    pub conn_str: String,
199    /// One or more publications on the remote side. Order is
200    /// preserved verbatim from the DDL; the worker requests them
201    /// in this order. v6.1.4 records the list; v6.1.5
202    /// publisher-side filtering enforces it.
203    pub publications: Vec<String>,
204}
205
206/// v6.1.2 — `CREATE PUBLICATION` AST node. The `scope` field uses
207/// the [`PublicationScope`] shape. v6.1.2 only accepted
208/// `AllTables`; v6.1.3 unlocks the `ForTables` / `AllTablesExcept`
209/// variants by flipping the parser gate (no AST migration).
210#[derive(Debug, Clone, PartialEq, Eq)]
211pub struct CreatePublicationStatement {
212    pub name: String,
213    pub scope: PublicationScope,
214}
215
216/// v6.1.2 — Which tables a publication covers. v6.1.3 (this commit)
217/// flips the parser gate for the `ForTables` / `AllTablesExcept`
218/// variants — the on-disk shape, snapshot serialisation, and the
219/// AST round-trip Display path were already in place in v6.1.2
220/// so this is a parser-only widening.
221#[derive(Debug, Clone, PartialEq, Eq)]
222pub enum PublicationScope {
223    AllTables,
224    ForTables(Vec<String>),
225    AllTablesExcept(Vec<String>),
226}
227
228#[derive(Debug, Clone, PartialEq, Eq)]
229pub struct AlterIndexStatement {
230    pub name: String,
231    pub target: AlterIndexTarget,
232}
233
234#[derive(Debug, Clone, Copy, PartialEq, Eq)]
235pub enum AlterIndexTarget {
236    /// `REBUILD [WITH (encoding = <enc>)]`. `encoding = None`
237    /// rebuilds the existing graph in place without touching the
238    /// column encoding; `Some(enc)` re-encodes every cell first.
239    Rebuild { encoding: Option<VecEncoding> },
240}
241
242/// v6.7.2 — `ALTER TABLE t SET <setting> = <value>`. v6.7.2 ships
243/// the single `hot_tier_bytes` setting; later v6.7.x sub-versions
244/// can add more SET subjects without changing the dispatch shape.
245#[derive(Debug, Clone, PartialEq)]
246pub struct AlterTableStatement {
247    pub name: String,
248    /// v7.13.2 — mailrs round-6 S1. One or more subactions
249    /// separated by commas in the source SQL. PG-semantic apply
250    /// is sequential; engine bails on first error (no
251    /// transactional rollback of completed subactions in v7.13).
252    /// Single-subaction shape stays a 1-element vec.
253    pub targets: Vec<AlterTableTarget>,
254}
255
256#[derive(Debug, Clone, PartialEq)]
257pub enum AlterTableTarget {
258    /// Per-table hot-tier byte budget override. The freezer
259    /// reads this before falling back to `SPG_HOT_TIER_BYTES`.
260    SetHotTierBytes(u64),
261    /// v7.6.8 — `ALTER TABLE t ADD CONSTRAINT name FOREIGN KEY
262    /// (cols) REFERENCES parent[(pcols)] [ON DELETE/UPDATE …]`.
263    /// Engine validates existing rows against the new constraint
264    /// before installing it.
265    AddForeignKey(ForeignKeyConstraint),
266    /// v7.6.8 — `ALTER TABLE t DROP CONSTRAINT [IF EXISTS] name`.
267    /// `if_exists` (v7.13.2 mailrs round-6 S7) makes the drop a
268    /// no-op when no FK with that name exists; otherwise raises.
269    DropForeignKey {
270        name: String,
271        if_exists: bool,
272    },
273    /// v7.13.0 — `ALTER TABLE t ADD [COLUMN] [IF NOT EXISTS] <col>
274    /// <type> [DEFAULT <expr>] [NOT NULL]`. mailrs round-5 G1
275    /// (20 migrate-*.sql hits). Engine appends the column to the
276    /// schema and back-fills every existing row with the DEFAULT
277    /// (or NULL when no DEFAULT and the column is nullable).
278    AddColumn {
279        column: ColumnDef,
280        if_not_exists: bool,
281    },
282    /// v7.13.0 — `ALTER TABLE t ALTER COLUMN <col> TYPE <ty>
283    /// [USING <expr>]` (mailrs round-5 G8). Engine rewrites every
284    /// existing row's column value by evaluating the optional
285    /// USING expression (default `col::<ty>`) and re-coercing
286    /// against the new column type.
287    AlterColumnType {
288        column: String,
289        new_type: ColumnTypeName,
290        using: Option<Expr>,
291    },
292}
293
294#[derive(Debug, Clone, PartialEq)]
295pub struct ExplainStatement {
296    pub analyze: bool,
297    pub inner: Box<SelectStatement>,
298    /// v6.8.3 — `EXPLAIN (SUGGEST) <SELECT>` enables the index
299    /// advisor pass: after the regular plan tree, the engine
300    /// emits one suggestion line per column referenced in the
301    /// query's WHERE / JOIN that has no covering index on the
302    /// owning table.
303    pub suggest: bool,
304}
305
306#[derive(Debug, Clone, PartialEq, Eq)]
307pub struct CreateUserStatement {
308    pub name: String,
309    pub password: String,
310    /// One of `admin` / `readwrite` / `readonly`. Stored verbatim from
311    /// the parser; the engine validates against `Role::parse` so a
312    /// typo lands as a runtime error with a clear message rather than
313    /// a parse failure.
314    pub role: String,
315}
316
317/// v7.12.4 — `CREATE [OR REPLACE] FUNCTION`. v7.12.4 ships
318/// `RETURNS TRIGGER LANGUAGE plpgsql` as the primary use case
319/// (the row-level trigger body the CREATE TRIGGER below references).
320/// Non-trigger user-defined functions parse but error at execution
321/// time with a clear unsupported message; that surface lands in
322/// v7.12.5+.
323#[derive(Debug, Clone, PartialEq)]
324pub struct CreateFunctionStatement {
325    pub name: String,
326    /// `OR REPLACE` was present; an existing function with the
327    /// same name is overwritten instead of erroring.
328    pub or_replace: bool,
329    /// `(arg1 type1, ...)` — v7.12.4 only accepts the empty arg
330    /// list `()` (sufficient for trigger functions). Other shapes
331    /// parse and store the args but the executor refuses to call
332    /// them.
333    pub args: Vec<FunctionArg>,
334    /// `RETURNS <type>` — `trigger` is the supported shape for
335    /// v7.12.4; arbitrary return types parse to
336    /// [`FunctionReturn::Other`].
337    pub returns: FunctionReturn,
338    /// `LANGUAGE <lang>` clause. PG accepts the clause on either
339    /// side of `AS $$...$$`; the parser canonicalises to one slot.
340    /// `plpgsql` and `sql` are the two interesting values.
341    pub language: String,
342    /// `AS $$ ... $$` body. v7.12.4 parses PL/pgSQL bodies into
343    /// a structured AST; non-trigger / non-plpgsql bodies stay as
344    /// the raw source text so the v7.12.5+ executor can pick them
345    /// up without a parser rev.
346    pub body: FunctionBody,
347}
348
349/// v7.12.4 — one positional argument to a `CREATE FUNCTION`.
350#[derive(Debug, Clone, PartialEq)]
351pub struct FunctionArg {
352    /// `IN` / `OUT` / `INOUT` mode. v7.12.4 only accepts `IN`
353    /// (the default); `OUT` / `INOUT` parse but the executor
354    /// refuses them.
355    pub mode: FunctionArgMode,
356    /// Optional arg name. Trigger functions traditionally don't
357    /// name their args (they read NEW/OLD instead), so `None` is
358    /// the common case.
359    pub name: Option<String>,
360    /// Declared type, normalised to the SPG `DataType` mapping
361    /// where one exists. Unknown / extension types parse as a
362    /// raw string under [`FunctionArgType::Raw`].
363    pub ty: FunctionArgType,
364}
365
366#[derive(Debug, Clone, Copy, PartialEq, Eq)]
367pub enum FunctionArgMode {
368    In,
369    Out,
370    InOut,
371}
372
373#[derive(Debug, Clone, PartialEq)]
374pub enum FunctionArgType {
375    Typed(ColumnTypeName),
376    /// Unknown / extension types — kept as the parser-side raw
377    /// identifier so error messages can name them precisely.
378    Raw(String),
379}
380
381#[derive(Debug, Clone, PartialEq)]
382pub enum FunctionReturn {
383    /// `RETURNS TRIGGER` — the row-level trigger function shape.
384    /// v7.12.4 ships exactly this for execution.
385    Trigger,
386    /// `RETURNS VOID`. Parses; executor rejects in v7.12.4 unless
387    /// the function is unused (since v7.12.4 doesn't ship scalar
388    /// function invocation).
389    Void,
390    /// `RETURNS <type>` for any concrete data type. Reserved for
391    /// v7.12.5+'s scalar UDF surface.
392    Type(ColumnTypeName),
393    /// `RETURNS <ident>` for types SPG doesn't know — extension
394    /// types, RETURNS SETOF rows, RETURNS TABLE(...), etc.
395    Other(String),
396}
397
398#[derive(Debug, Clone, PartialEq)]
399pub enum FunctionBody {
400    /// v7.12.4 — parsed PL/pgSQL `BEGIN … END` block. The
401    /// trigger-function executor walks this directly without
402    /// re-parsing.
403    PlPgSql(PlPgSqlBlock),
404    /// Raw source text — parser couldn't (or didn't try to)
405    /// structure-parse the body. Used for `LANGUAGE sql`
406    /// functions and any PL/pgSQL body that contains v7.12.5+
407    /// features the v7.12.4 parser doesn't yet recognise. The
408    /// executor returns an unsupported error when invoked.
409    Raw(String),
410}
411
412/// v7.12.4 — PL/pgSQL `BEGIN ... END;` block. v7.12.6 widens
413/// from assignment + return to a real-PL/pgSQL surface:
414/// `DECLARE`-block local variables, `IF/ELSIF/ELSE/END IF`
415/// control flow, `RAISE` diagnostics, and embedded SQL
416/// statements that execute through the regular engine path.
417/// The remaining v7.12.x carve-out is loops (`LOOP/WHILE/FOR`),
418/// which mailrs's trigger doesn't need but other PG customers
419/// may; deferred to a future minor release.
420#[derive(Debug, Clone, PartialEq)]
421pub struct PlPgSqlBlock {
422    /// v7.12.6 — `DECLARE var TYPE [:= init_expr];` declarations
423    /// preceding `BEGIN`. Empty when the body opens directly with
424    /// `BEGIN`. Declarations execute in order; each may reference
425    /// earlier-declared locals in its init expression.
426    pub declarations: Vec<PlPgSqlDeclare>,
427    pub statements: Vec<PlPgSqlStmt>,
428}
429
430/// v7.12.6 — single `DECLARE` entry: variable name + declared
431/// type + optional initialiser. Variables default to SQL NULL
432/// when no init is given (matches PG).
433#[derive(Debug, Clone, PartialEq)]
434pub struct PlPgSqlDeclare {
435    pub name: String,
436    /// Declared SQL type (mapped to [`ColumnTypeName`] where SPG
437    /// knows it; raw text otherwise).
438    pub ty: FunctionArgType,
439    pub default: Option<Expr>,
440}
441
442#[derive(Debug, Clone, PartialEq)]
443pub enum PlPgSqlStmt {
444    /// `NEW.col := expr;` or `OLD.col := expr;`. OLD is parsed
445    /// for clarity in error reporting (PG also forbids it) — the
446    /// executor errors with a clear "OLD is read-only" message.
447    Assign { target: AssignTarget, value: Expr },
448    /// `RETURN <target>;` — trigger functions canonically return
449    /// `NEW` / `OLD` / `NULL`; v7.12.4 also accepts a bare
450    /// expression for forward compatibility with scalar UDFs.
451    Return(ReturnTarget),
452    /// v7.12.6 — `IF cond THEN body [ELSIF cond THEN body]*
453    /// [ELSE body] END IF;`. Branches are tried in order; first
454    /// truthy condition wins; the optional ELSE runs when no
455    /// condition matched.
456    If {
457        branches: Vec<(Expr, Vec<PlPgSqlStmt>)>,
458        else_branch: Vec<PlPgSqlStmt>,
459    },
460    /// v7.12.6 — `RAISE <level> '<fmt>' [, args]*;`. Level is one
461    /// of `NOTICE` / `WARNING` / `INFO` / `LOG` / `DEBUG`
462    /// (logging — observable side effect only) or `EXCEPTION`
463    /// (aborts the trigger and propagates as an error). v7.12.6
464    /// supports the basic format-string substitution PG uses
465    /// (`%` placeholders consumed positionally).
466    Raise {
467        level: RaiseLevel,
468        message: String,
469        args: Vec<Expr>,
470    },
471    /// v7.12.6 — embedded SQL statement inside the trigger body
472    /// (`INSERT INTO …`, `UPDATE …`, `DELETE FROM …`, `SELECT …`).
473    /// NEW.col / OLD.col references inside the embedded
474    /// statement's expression tree are substituted with the
475    /// current trigger context before the engine re-executes the
476    /// statement. Recursion depth into nested triggers is
477    /// bounded by the engine's existing trigger-fire guard.
478    EmbeddedSql(Box<Statement>),
479}
480
481#[derive(Debug, Clone, Copy, PartialEq, Eq)]
482pub enum RaiseLevel {
483    /// `RAISE NOTICE` — diagnostic message, observable in the
484    /// server log. Does not affect the trigger's outcome.
485    Notice,
486    /// `RAISE WARNING` — like NOTICE, slightly louder severity.
487    Warning,
488    /// `RAISE INFO` — like NOTICE, slightly quieter.
489    Info,
490    /// `RAISE LOG` — like NOTICE, lower priority.
491    Log,
492    /// `RAISE DEBUG` — like NOTICE, lowest priority.
493    Debug,
494    /// `RAISE EXCEPTION` — aborts the trigger function with the
495    /// given message, propagating up to the caller as a query-
496    /// level error.
497    Exception,
498}
499
500#[derive(Debug, Clone, PartialEq)]
501pub enum AssignTarget {
502    NewColumn(String),
503    OldColumn(String),
504    /// Reserved for v7.12.5 DECLARE'd local variables.
505    Local(String),
506}
507
508#[derive(Debug, Clone, PartialEq)]
509pub enum ReturnTarget {
510    /// `RETURN NEW;` — for BEFORE triggers, this is the row that
511    /// actually gets written (possibly with NEW.col mutations
512    /// applied). For AFTER triggers, the return value is ignored.
513    New,
514    /// `RETURN OLD;` — pass-through. For BEFORE DELETE this lets
515    /// the delete proceed; for BEFORE UPDATE / INSERT it's
516    /// equivalent to dropping the write.
517    Old,
518    /// `RETURN NULL;` — for BEFORE triggers, skips the write
519    /// entirely. For AFTER, the return value is ignored.
520    Null,
521    /// `RETURN <expr>;` — non-row return shape; reserved for the
522    /// scalar UDF surface in v7.12.5+. Executor errors when used
523    /// inside a trigger function.
524    Expr(Expr),
525}
526
527/// v7.12.4 — `CREATE [OR REPLACE] TRIGGER`. Always row-level
528/// (`FOR EACH ROW`) in v7.12.4 — statement-level triggers parse
529/// but the executor refuses them. `WHEN (cond)` clauses are out
530/// of scope; the trigger function can short-circuit on a leading
531/// IF inside its body once v7.12.5 lands IF.
532#[derive(Debug, Clone, PartialEq)]
533pub struct CreateTriggerStatement {
534    pub name: String,
535    pub or_replace: bool,
536    pub timing: TriggerTiming,
537    /// At least one event; `INSERT OR UPDATE OR DELETE` parses to
538    /// three entries in order.
539    pub events: Vec<TriggerEvent>,
540    pub table: String,
541    /// `FOR EACH ROW` vs `FOR EACH STATEMENT`. v7.12.4 ships
542    /// only `Row`; `Statement` parses but the executor refuses.
543    pub for_each: TriggerForEach,
544    /// Name of the function to invoke. v7.12.4 requires the
545    /// function to be `CREATE FUNCTION`'d earlier; forward
546    /// references (PG accepts) are deferred to v7.12.5.
547    pub function: String,
548    /// v7.13.0 — `UPDATE OF col, col, …` column-list filter
549    /// (mailrs round-5 G7). Non-empty only when the events list
550    /// contains UPDATE and the user wrote the column-list filter.
551    /// PG fires the trigger only when at least one of these
552    /// columns appears in the SET clause; SPG conservatively
553    /// fires on any UPDATE matching the listed columns or
554    /// rewriting them at the row level. Empty vec = no filter
555    /// (fire on every UPDATE).
556    pub update_columns: Vec<String>,
557}
558
559#[derive(Debug, Clone, Copy, PartialEq, Eq)]
560pub enum TriggerTiming {
561    /// Fires before the row is written; the trigger function's
562    /// return value (NEW or NULL) decides the row content and
563    /// whether the write proceeds at all.
564    Before,
565    /// Fires after the row is written; the return value is
566    /// ignored.
567    After,
568    /// `INSTEAD OF` is PG-VIEW-trigger-only and out of scope for
569    /// v7.12.4 (SPG has no updatable-view surface).
570    InsteadOf,
571}
572
573#[derive(Debug, Clone, Copy, PartialEq, Eq)]
574pub enum TriggerEvent {
575    Insert,
576    Update,
577    Delete,
578    /// `TRUNCATE` event parses; SPG has no TRUNCATE statement
579    /// so the trigger never fires.
580    Truncate,
581}
582
583#[derive(Debug, Clone, Copy, PartialEq, Eq)]
584pub enum TriggerForEach {
585    Row,
586    Statement,
587}
588
589#[derive(Debug, Clone, PartialEq)]
590pub struct CreateIndexStatement {
591    pub name: String,
592    pub table: String,
593    pub column: String,
594    /// Optional `USING <method>` clause. v2.0 recognises `hnsw` (NSW
595    /// graph for vector kNN); unspecified is the default B-tree index.
596    pub method: IndexMethod,
597    /// `IF NOT EXISTS` — engine returns `CommandOk` no-op when the
598    /// index name already exists, instead of raising `DuplicateIndex`.
599    pub if_not_exists: bool,
600    /// v6.8.0 — `INCLUDE (col1, col2, …)` columns. Identifies the
601    /// non-key columns the planner should treat as "covered" by
602    /// this index when checking whether a query can run as an
603    /// index-only scan. Empty when no `INCLUDE` clause was given.
604    pub included_columns: Vec<String>,
605    /// v6.8.1 — `WHERE <expr>` partial-index predicate. Only rows
606    /// for which `<expr>` evaluates truthy enter the index;
607    /// queries whose `WHERE` clause's canonical Display form
608    /// matches this expression's Display form can be served by the
609    /// partial index. Stored as a parsed `Expr` so the engine
610    /// re-uses the existing evaluation path; storage persists the
611    /// Display form on the catalog snapshot.
612    pub partial_predicate: Option<Expr>,
613    /// v6.8.2 — expression-based index. When `Some(expr)`, the
614    /// index key is the result of `expr` evaluated on each row
615    /// (e.g. `CREATE INDEX … (lower(name))`). The `column`
616    /// field still names the *primary* column the expression
617    /// touches so existing planner shortcuts that resolve a
618    /// column position stay valid. `None` = plain
619    /// column-reference index (the legacy shape).
620    pub expression: Option<Expr>,
621    /// v7.9.14 — extra column names after the leading column in a
622    /// multi-column `CREATE INDEX … (a, b, c)`. mailrs F2. The
623    /// planner today still only uses the leading column for index
624    /// seeks; the extras are tracked verbatim so the same DDL
625    /// round-trips through WAL replay + catalog snapshot, and so
626    /// the engine can emit a clear warning at INDEX CREATE time
627    /// that only the leading column is currently honoured.
628    /// Composite BTree index keys land in v7.10.
629    pub extra_columns: Vec<String>,
630    /// v7.9.29 — `CREATE UNIQUE INDEX …`. When true the engine
631    /// enforces uniqueness on the indexed key (combined with the
632    /// `partial_predicate` filter — only rows where the predicate
633    /// evaluates truthy enter the uniqueness check). Standard SQL
634    /// and PG's canonical way to express conditional uniqueness.
635    /// mailrs K1.
636    pub is_unique: bool,
637}
638
639#[derive(Debug, Clone, Copy, PartialEq, Eq)]
640pub enum IndexMethod {
641    /// Default — B-tree over `IndexKey`. Used for equality / range
642    /// lookups on scalar columns.
643    BTree,
644    /// `USING hnsw` — NSW graph for kNN over a vector column.
645    Hnsw,
646    /// v6.7.1 — `USING brin` — Block Range INdex. Per-segment
647    /// metadata that records (min_key, max_key) for each page in a
648    /// cold-tier segment, on the indexed column. The optimizer
649    /// can use these summaries to skip pages whose range does NOT
650    /// overlap a query's WHERE predicate. BRIN indexes carry no
651    /// in-memory data — the summaries live in the segment v2
652    /// envelope's sidecar. Created via the standard
653    /// `CREATE INDEX … USING brin (col)` syntax.
654    Brin,
655    /// v7.12.3 — `USING gin` — inverted index over a `tsvector`
656    /// column. Posting lists map `lexeme word` → row locators; the
657    /// planner uses them to narrow `WHERE col @@ tsquery` to the
658    /// candidate rows whose vectors contain a matching term, then
659    /// re-evaluates the full `@@` semantics on each candidate.
660    /// Replaces the v7.9.26b `USING gin` → BTree fallback that
661    /// silently degraded to a full scan at query time.
662    Gin,
663}
664
665#[derive(Debug, Clone, PartialEq)]
666pub struct CreateTableStatement {
667    pub name: String,
668    pub columns: Vec<ColumnDef>,
669    /// `IF NOT EXISTS` — engine returns `CommandOk` no-op when the
670    /// table name already exists, instead of raising `DuplicateTable`.
671    pub if_not_exists: bool,
672    /// v7.6.0 — table-level `FOREIGN KEY (...) REFERENCES ...`
673    /// constraints. Column-level `REFERENCES` (single-column inline
674    /// form) is normalised into this vec at parse time so the engine
675    /// sees one uniform list.
676    pub foreign_keys: Vec<ForeignKeyConstraint>,
677    /// v7.9.18 — table-level constraints: `PRIMARY KEY (a, b)` and
678    /// `UNIQUE (a, b, ...)`. mailrs migration follow-up G1 + G6.
679    /// Engine resolves each into a BTree index named after the
680    /// constraint's leading column at CREATE TABLE time; INSERT
681    /// path enforces composite uniqueness via row scan on the
682    /// leading column index.
683    pub table_constraints: Vec<TableConstraint>,
684}
685
686/// v7.9.18 — table-level constraint at the end of a CREATE TABLE
687/// column list. Either a composite PRIMARY KEY or a UNIQUE
688/// (single- or multi-column).
689#[derive(Debug, Clone, PartialEq)]
690pub enum TableConstraint {
691    /// `PRIMARY KEY (col1, col2, ...)`. Implies NOT NULL on each
692    /// referenced column. Engine builds a BTree index named
693    /// `<table>_pkey` and enforces composite uniqueness on INSERT.
694    PrimaryKey {
695        name: Option<String>,
696        columns: Vec<String>,
697    },
698    /// `UNIQUE (col1, col2, ...)`. Engine builds a BTree index
699    /// named `<table>_<leading_col>_key` (single-column) or
700    /// `<table>_<leading_col>_<…>_key` (composite) and enforces
701    /// uniqueness on INSERT.
702    Unique {
703        name: Option<String>,
704        columns: Vec<String>,
705        /// v7.13.0 — `NULLS NOT DISTINCT` modifier (mailrs round-5
706        /// G10). PG 15+ flips the NULL handling so any number of
707        /// NULL rows collide on the constraint. Default is
708        /// `false` (NULLS DISTINCT, standard SQL behaviour).
709        nulls_not_distinct: bool,
710    },
711    /// v7.13.0 — `CHECK (<expr>)` table-level constraint
712    /// (mailrs round-5 G3). Column-level inline CHECKs fold into
713    /// this same variant at parse time. Engine evaluates the
714    /// predicate against each INSERT/UPDATE candidate row; a
715    /// false / NULL result rejects the mutation.
716    Check {
717        name: Option<String>,
718        expr: Expr,
719    },
720}
721
722#[derive(Debug, Clone, PartialEq)]
723pub struct ColumnDef {
724    pub name: String,
725    pub ty: ColumnTypeName,
726    pub nullable: bool,
727    /// `DEFAULT <expr>` literal supplied at CREATE TABLE. Engine
728    /// evaluates this once (with an empty row) and caches the resulting
729    /// `Value` on the column schema.
730    pub default: Option<Expr>,
731    /// MySQL-style `AUTO_INCREMENT` — the engine maintains a counter
732    /// per such column and fills the slot when INSERT leaves it
733    /// unbound (omitted from a column-list INSERT or explicitly NULL).
734    pub auto_increment: bool,
735    /// v7.9.13 — inline `PRIMARY KEY` column constraint. mailrs
736    /// migration follow-up F1. Implies `NOT NULL`. Engine creates
737    /// an implicit BTree index named `<table>_pkey` over this
738    /// column at CREATE TABLE time, satisfying the parent-side
739    /// index requirement for any FOREIGN KEY pointing at it.
740    pub is_primary_key: bool,
741    /// v7.13.0 — inline `UNIQUE` column constraint
742    /// (mailrs round-5 G2). The CREATE TABLE handler folds this
743    /// into a single-column `TableConstraint::Unique` so the
744    /// engine path stays uniform with table-level UNIQUE.
745    pub is_unique: bool,
746    /// v7.13.0 — inline `CHECK (<expr>)` column constraint
747    /// (mailrs round-5 G3). Stored alongside the column so the
748    /// CREATE TABLE handler can fold these into table-level
749    /// CHECK constraints. Multiple inline CHECKs on the same
750    /// column are concatenated with AND at the table level.
751    pub check: Option<Expr>,
752}
753
754/// v7.6.0 — A single FOREIGN KEY constraint. Both column-level
755/// `REFERENCES` and table-level `FOREIGN KEY (...) REFERENCES ...`
756/// parse into this shape — the column-level form has a single-entry
757/// `columns` / `parent_columns`.
758#[derive(Debug, Clone, PartialEq)]
759pub struct ForeignKeyConstraint {
760    /// Optional `CONSTRAINT <name>` prefix. Engine ignores the name
761    /// today but parses + stores it so a future ALTER TABLE DROP
762    /// CONSTRAINT can target by name (v7.6.8).
763    pub name: Option<String>,
764    /// Local columns participating in the FK (≥ 1).
765    pub columns: Vec<String>,
766    /// Referenced parent table.
767    pub parent_table: String,
768    /// Referenced parent columns. Must have the same arity as
769    /// `columns`; engine validates parent has a PK / UNIQUE index
770    /// on exactly this column set (v7.6.1).
771    pub parent_columns: Vec<String>,
772    /// `ON DELETE` action. Defaults to `Restrict` if absent.
773    pub on_delete: FkAction,
774    /// `ON UPDATE` action. Defaults to `Restrict` if absent.
775    pub on_update: FkAction,
776}
777
778/// v7.6.0 — Referential action for `ON DELETE` / `ON UPDATE`.
779#[derive(Debug, Clone, Copy, PartialEq, Eq)]
780pub enum FkAction {
781    /// Reject the parent mutation if any child row references it.
782    /// SQL spec default; SPG default when no clause is given.
783    Restrict,
784    /// Recursively propagate the parent's delete / update to the
785    /// child rows. Same TX.
786    Cascade,
787    /// Set the child FK column(s) to NULL. Requires the FK columns
788    /// to be NULL-able.
789    SetNull,
790    /// Set the child FK column(s) to their declared DEFAULT.
791    /// Requires the child column(s) to have DEFAULT.
792    SetDefault,
793    /// SQL spec `NO ACTION` (deferred check). SPG treats this as
794    /// `Restrict` because the single-writer model has no deferred
795    /// constraint window; the keyword is accepted for compatibility.
796    NoAction,
797}
798
799/// In-cell encoding for a `VECTOR(N)` column. v6.0.1 added the
800/// optional `USING <encoding>` clause; omitting it keeps the
801/// pre-v6 `F32` default. `Sq8` quantises each cell to a per-vector
802/// affine `(min, max, [u8; dim])` triple (4× compression). `F16`
803/// (v6.0.3, DDL keyword `HALF`) stores each element as IEEE-754
804/// binary16 (2× compression, ~3 decimal digits of precision).
805#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
806pub enum VecEncoding {
807    /// IEEE-754 binary32. Pre-v6 default; matches pgvector's
808    /// uncompressed `vector` type wire / storage layout.
809    #[default]
810    F32,
811    /// v6.0.1 SQ8 — per-vector affine 8-bit quantisation. See
812    /// `spg_storage::quantize::Sq8Vector` for the math + recall
813    /// envelope (≥ 0.95 on Gaussian / unit-sphere corpora at
814    /// dim ≥ 32).
815    Sq8,
816    /// v6.0.3 halfvec — IEEE-754 binary16 (half-precision)
817    /// per-element. DDL keyword `HALF` (pgvector convention).
818    /// Bit-exact dequantise to f32 at the storage layer; no
819    /// rerank pass needed for kNN search.
820    F16,
821}
822
823impl fmt::Display for VecEncoding {
824    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
825        match self {
826            Self::F32 => f.write_str("F32"),
827            Self::Sq8 => f.write_str("SQ8"),
828            // pgvector convention: DDL keyword is `HALF`, not `F16`.
829            Self::F16 => f.write_str("HALF"),
830        }
831    }
832}
833
834/// SQL-level type names. The mapping to the storage runtime's `DataType`
835/// happens in `spg-engine` — keeping `spg-sql` free of storage deps.
836#[derive(Debug, Clone, Copy, PartialEq, Eq)]
837pub enum ColumnTypeName {
838    SmallInt,
839    Int,
840    BigInt,
841    Float,
842    Text,
843    /// `VARCHAR(N)` — TEXT capped at N Unicode characters.
844    Varchar(u32),
845    /// `CHAR(N)` — TEXT right-padded with spaces to exactly N characters.
846    Char(u32),
847    Bool,
848    /// pgvector fixed-dimension `VECTOR(N)`. v6.0.1 added the
849    /// `USING <encoding>` clause; omitting it surfaces as
850    /// `encoding = VecEncoding::F32` (the pre-v6 default).
851    Vector {
852        dim: u32,
853        encoding: VecEncoding,
854    },
855    /// `NUMERIC` / `NUMERIC(p)` / `NUMERIC(p, s)` — exact decimal.
856    /// Bare `NUMERIC` and `NUMERIC(p)` both surface with `scale=0`.
857    Numeric(u8, u8),
858    /// `DATE` — calendar day, no time-of-day component.
859    Date,
860    /// `TIMESTAMP` / `MySQL` `DATETIME` — instant with microsecond
861    /// precision.
862    Timestamp,
863    /// v7.9.2 `TIMESTAMPTZ` / `TIMESTAMP WITH TIME ZONE`. SPG
864    /// stores all timestamps as UTC microseconds-since-epoch and
865    /// does not carry per-row offset (PG's internal representation
866    /// is the same — TZ is a display convention). The distinction
867    /// from `TIMESTAMP` exists for the PG-wire layer to advertise
868    /// OID 1184 so sqlx-style clients decode into
869    /// `chrono::DateTime<Utc>` instead of `NaiveDateTime`.
870    Timestamptz,
871    /// v4.9 `JSON` — text-backed JSON document. No parse-time
872    /// validation; the engine round-trips the literal verbatim.
873    /// PG OID 114 on the wire.
874    Json,
875    /// v7.9.0 `JSONB` — same storage shape as Json, advertised as
876    /// PG OID 3802 on the wire so sqlx-style binary-typed clients
877    /// decode without a custom type registration.
878    Jsonb,
879    /// v7.10.4 `BYTES` / `BYTEA` — raw binary blob. PG wire OID 17.
880    /// Literal forms (decoded by the engine at coercion time):
881    ///   - PG hex form: `'\xDEADBEEF'`
882    ///   - Escape form: `'foo\\000bar'` (backslash octal triples)
883    Bytes,
884    /// v7.10.10 `TEXT[]` — single-dimension TEXT array. PG wire
885    /// OID 1009. Literal forms accepted by the parser:
886    ///   - `ARRAY['a', 'b', NULL]`
887    ///   - `'{a,b,NULL}'::TEXT[]` (engine decodes the external
888    ///     form at coerce time)
889    TextArray,
890    /// v7.11.13 `INT[]` — single-dimension i32 array. PG wire OID
891    /// 1007. Same literal forms as TEXT[] (substituting integer
892    /// elements).
893    IntArray,
894    /// v7.11.13 `BIGINT[]` — single-dimension i64 array. PG wire
895    /// OID 1016.
896    BigIntArray,
897    /// v7.12.0 `tsvector` — PG full-text search lexeme set. PG
898    /// wire OID 3614. Literal: `'foo:1 bar:2'::tsvector` (PG
899    /// external form). G-CRIT-3.
900    TsVector,
901    /// v7.12.0 `tsquery` — PG full-text search parse tree. PG
902    /// wire OID 3615.
903    TsQuery,
904}
905
906impl fmt::Display for ColumnTypeName {
907    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
908        match self {
909            Self::SmallInt => f.write_str("SMALLINT"),
910            Self::Int => f.write_str("INT"),
911            Self::BigInt => f.write_str("BIGINT"),
912            Self::Float => f.write_str("FLOAT"),
913            Self::Text => f.write_str("TEXT"),
914            Self::Varchar(n) => write!(f, "VARCHAR({n})"),
915            Self::Char(n) => write!(f, "CHAR({n})"),
916            Self::Bool => f.write_str("BOOL"),
917            Self::Vector { dim, encoding } => match encoding {
918                VecEncoding::F32 => write!(f, "VECTOR({dim})"),
919                VecEncoding::Sq8 => write!(f, "VECTOR({dim}) USING SQ8"),
920                VecEncoding::F16 => write!(f, "VECTOR({dim}) USING HALF"),
921            },
922            Self::Json => f.write_str("JSON"),
923            Self::Jsonb => f.write_str("JSONB"),
924            Self::Bytes => f.write_str("BYTEA"),
925            Self::TextArray => f.write_str("TEXT[]"),
926            Self::IntArray => f.write_str("INT[]"),
927            Self::BigIntArray => f.write_str("BIGINT[]"),
928            Self::TsVector => f.write_str("TSVECTOR"),
929            Self::TsQuery => f.write_str("TSQUERY"),
930            Self::Numeric(p, s) => {
931                if *s == 0 {
932                    write!(f, "NUMERIC({p})")
933                } else {
934                    write!(f, "NUMERIC({p}, {s})")
935                }
936            }
937            Self::Date => f.write_str("DATE"),
938            Self::Timestamp => f.write_str("TIMESTAMP"),
939            Self::Timestamptz => f.write_str("TIMESTAMPTZ"),
940        }
941    }
942}
943
944/// `UPDATE <table> SET col = expr [, ...] [WHERE cond]`. v4.4 — the
945/// engine evaluates `expr` per matched row in the table's row order
946/// and rewrites cells in place. Indexed columns are dropped + re-
947/// inserted into the affected B-tree on each row change.
948#[derive(Debug, Clone, PartialEq)]
949pub struct UpdateStatement {
950    pub table: String,
951    pub assignments: Vec<(String, Expr)>,
952    pub where_: Option<Expr>,
953    /// v7.9.4 — `RETURNING <projection>`. None = no RETURNING
954    /// clause (legacy CommandComplete path). Some = engine
955    /// evaluates the projection over each mutated row and
956    /// streams the result as a Rows QueryResult.
957    pub returning: Option<Vec<SelectItem>>,
958}
959
960/// `DELETE FROM <table> [WHERE cond]`. v4.4 — removes matched rows
961/// from the active catalog and prunes them from every index.
962#[derive(Debug, Clone, PartialEq)]
963pub struct DeleteStatement {
964    pub table: String,
965    pub where_: Option<Expr>,
966    /// v7.9.4 — `RETURNING <projection>`.
967    pub returning: Option<Vec<SelectItem>>,
968}
969
970#[derive(Debug, Clone, PartialEq)]
971pub struct InsertStatement {
972    pub table: String,
973    /// Optional column list — `INSERT INTO t (a, b) VALUES (...)`. When
974    /// `None`, every tuple is positional and must match the table arity.
975    /// When `Some`, the engine maps each tuple slot to the named column and
976    /// fills the rest with NULL (must be nullable).
977    pub columns: Option<Vec<String>>,
978    /// One or more `(expr, expr, ...)` tuples — the multi-row VALUES form.
979    /// v1.3+ accepts `INSERT INTO t VALUES (a), (b)`. Empty when
980    /// `select_source` is `Some` (the engine builds rows from the
981    /// inner SELECT result set instead).
982    pub rows: Vec<Vec<Expr>>,
983    /// v7.13.0 — `INSERT INTO t [(cols)] SELECT …` (mailrs
984    /// round-5 G4). When present, `rows` is empty and the engine
985    /// materialises the SELECT result, coerces each output tuple to
986    /// the target column types, and inserts as a single batch.
987    pub select_source: Option<Box<SelectStatement>>,
988    /// v7.9.7 — `ON CONFLICT (cols) DO { NOTHING | UPDATE SET … }`
989    /// upsert clause. None = legacy INSERT (conflict raises a
990    /// DuplicateKey error). mailrs migration blocker #2.
991    pub on_conflict: Option<OnConflictClause>,
992    /// v7.9.4 — `RETURNING <projection>`.
993    pub returning: Option<Vec<SelectItem>>,
994}
995
996/// v7.9.7 — INSERT upsert clause: `ON CONFLICT (target) DO action`.
997#[derive(Debug, Clone, PartialEq)]
998pub struct OnConflictClause {
999    /// Local columns that identify the conflict (must match a
1000    /// UNIQUE / PRIMARY KEY index on the target table). Empty
1001    /// list means the user wrote `ON CONFLICT DO …` without a
1002    /// target — engine picks the table's first BTree index by
1003    /// convention.
1004    pub target_columns: Vec<String>,
1005    /// The action on conflict.
1006    pub action: OnConflictAction,
1007}
1008
1009/// v7.9.7 — action on conflict.
1010#[derive(Debug, Clone, PartialEq)]
1011pub enum OnConflictAction {
1012    /// `DO NOTHING` — INSERT proceeds for non-conflicting rows,
1013    /// silently skips conflicting ones.
1014    Nothing,
1015    /// `DO UPDATE SET col = expr [, …] [WHERE cond]`. `assignments`
1016    /// may reference `EXCLUDED.col` to read the incoming row's
1017    /// value (engine wires `EXCLUDED` as a virtual table).
1018    Update {
1019        assignments: Vec<(String, Expr)>,
1020        where_: Option<Expr>,
1021    },
1022}
1023
1024#[derive(Debug, Clone, PartialEq)]
1025pub struct SelectStatement {
1026    /// v4.11: `WITH name AS (SELECT ...) [, ...]` common-table
1027    /// expressions, materialised once at query start before the
1028    /// body SELECT runs. Empty for a regular SELECT. Non-recursive
1029    /// only — no `WITH RECURSIVE` for v4.x.
1030    pub ctes: Vec<Cte>,
1031    pub distinct: bool,
1032    pub items: Vec<SelectItem>,
1033    pub from: Option<FromClause>,
1034    pub where_: Option<Expr>,
1035    pub group_by: Option<Vec<Expr>>,
1036    /// v6.4.1 — `GROUP BY ALL` shortcut: when true, the planner
1037    /// expands `group_by` to every non-aggregate SELECT-list item
1038    /// before the executor runs. Mutually exclusive with an
1039    /// explicit `group_by` list (the parser sets exactly one).
1040    pub group_by_all: bool,
1041    /// `HAVING <expr>` — filter applied *after* `GROUP BY` aggregation.
1042    /// Supports aggregate calls (e.g. `HAVING count(*) > 1`); the
1043    /// aggregate executor resolves them through the same synthetic
1044    /// schema used for the SELECT items.
1045    pub having: Option<Expr>,
1046    /// UNION / UNION ALL chain. Empty for a plain SELECT. Each peer is
1047    /// itself a `SelectStatement` with `order_by = None` and `limit =
1048    /// None` (the parser enforces that — ORDER BY / LIMIT belong to the
1049    /// top of the chain).
1050    pub unions: Vec<(UnionKind, SelectStatement)>,
1051    /// v6.4.0 — multi-key ORDER BY. Empty `Vec` means no ORDER BY.
1052    /// Keys are matched left-to-right: first key decides, ties break
1053    /// to the second, etc.
1054    pub order_by: Vec<OrderBy>,
1055    /// `LIMIT <n>` — bound on row output. `n` is an integer
1056    /// literal **or** (v7.9.24) a placeholder `$N` resolved
1057    /// against the prepared-statement Bind values. mailrs
1058    /// migration follow-up H2.
1059    pub limit: Option<LimitExpr>,
1060    /// `OFFSET <n>` — drop the first `n` rows after ORDER BY but
1061    /// before LIMIT (so `LIMIT 10 OFFSET 5` keeps rows 6..=15).
1062    pub offset: Option<LimitExpr>,
1063}
1064
1065/// v7.9.24 — LIMIT / OFFSET value. Integer literal at parse
1066/// time or a placeholder `$N` resolved during extended-query
1067/// Bind. mailrs migration follow-up H2.
1068#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1069pub enum LimitExpr {
1070    /// `LIMIT 10` — value known at parse time.
1071    Literal(u32),
1072    /// `LIMIT $N` — the 1-based parameter index, resolved against
1073    /// the bind values when the prepared statement executes.
1074    Placeholder(u16),
1075}
1076
1077impl fmt::Display for LimitExpr {
1078    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1079        match self {
1080            Self::Literal(n) => write!(f, "{n}"),
1081            Self::Placeholder(n) => write!(f, "${n}"),
1082        }
1083    }
1084}
1085
1086impl LimitExpr {
1087    /// Convenience for the simple-query path where no placeholders
1088    /// can possibly exist. Returns the literal value or `None` if
1089    /// this is a placeholder (caller must surface as Unsupported).
1090    pub fn as_literal(self) -> Option<u32> {
1091        match self {
1092            Self::Literal(n) => Some(n),
1093            Self::Placeholder(_) => None,
1094        }
1095    }
1096}
1097
1098/// v7.9.24 — extract LIMIT / OFFSET as a `u32` literal. After
1099/// the engine's `substitute_placeholders` pass these are
1100/// always Literal; in the simple-query path a Placeholder
1101/// shape returns None (executor surfaces as
1102/// "LIMIT/OFFSET ${n} requires prepared-statement binding").
1103impl SelectStatement {
1104    #[must_use]
1105    pub fn limit_literal(&self) -> Option<u32> {
1106        self.limit.and_then(LimitExpr::as_literal)
1107    }
1108    #[must_use]
1109    pub fn offset_literal(&self) -> Option<u32> {
1110        self.offset.and_then(LimitExpr::as_literal)
1111    }
1112}
1113
1114#[derive(Debug, Clone, PartialEq)]
1115pub struct Cte {
1116    pub name: String,
1117    pub body: SelectStatement,
1118    /// v4.22: `WITH RECURSIVE` — set when the WITH clause had the
1119    /// RECURSIVE keyword. Applies to every CTE in the clause per
1120    /// PG semantics. A non-recursive body in a RECURSIVE WITH is
1121    /// allowed; the engine just runs it once.
1122    pub recursive: bool,
1123    /// v4.22: optional `WITH name(a, b, c)` column-name list. When
1124    /// non-empty, these override the body's output column names
1125    /// position-by-position; the engine errors out if the count
1126    /// doesn't match the body's projection width.
1127    pub column_overrides: Vec<String>,
1128}
1129
1130#[derive(Debug, Clone, PartialEq)]
1131pub struct OrderBy {
1132    pub expr: Expr,
1133    /// `false` = ASC (default), `true` = DESC.
1134    pub desc: bool,
1135}
1136
1137#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1138pub enum UnionKind {
1139    /// `UNION` — dedupes the combined set.
1140    Distinct,
1141    /// `UNION ALL` — concatenates without dedup.
1142    All,
1143}
1144
1145#[derive(Debug, Clone, PartialEq)]
1146pub enum SelectItem {
1147    Wildcard,
1148    Expr { expr: Expr, alias: Option<String> },
1149}
1150
1151#[derive(Debug, Clone, PartialEq)]
1152pub struct TableRef {
1153    pub name: String,
1154    pub alias: Option<String>,
1155    /// v6.10.2 — `AS OF SEGMENT '<id>'` cold-tier time-travel.
1156    /// When `Some(id)`, the scan restricts to rows that live in
1157    /// segment `<id>` only — useful for forensic inspection of a
1158    /// specific freezer-emitted segment without exposing the hot
1159    /// tier. `AS OF TIMESTAMP <ts>` (PG-flavoured time travel)
1160    /// is STABILITY carve-out for v6.10 — needs the freezer to
1161    /// stamp each segment with a wall-clock at creation time.
1162    pub as_of_segment: Option<u32>,
1163    /// v7.11.7 — `FROM unnest(<expr>) [AS] <alias>` set-returning
1164    /// source. When `Some`, `name` is the alias (defaulting to
1165    /// `"unnest"` when no `AS` is given) and the engine builds a
1166    /// synthetic single-column table by evaluating the expression
1167    /// once at SELECT entry. Each TEXT[] element becomes one row;
1168    /// NULL elements become NULL cells. v7.11 supported
1169    /// uncorrelated UNNEST only as the FROM primary; v7.13.2
1170    /// (mailrs round-6 S5) widens to UNNEST in any FROM-list
1171    /// position (cross-join with regular tables).
1172    pub unnest_expr: Option<Box<Expr>>,
1173    /// v7.13.2 — mailrs round-6 S5. PG-standard
1174    /// `UNNEST(<arr>) AS alias(col_name)` column-list aliasing:
1175    /// when non-empty, the first entry overrides the projected
1176    /// column name for the unnested column. Empty = fall back to
1177    /// the table alias (pre-v7.13.2 behaviour).
1178    pub unnest_column_aliases: Vec<String>,
1179}
1180
1181/// FROM clause shape. v1.10 accepts a primary table plus a flat list of
1182/// joined peers — `FROM a [, b]* [INNER|LEFT] JOIN c ON expr ...`. The
1183/// joins evaluate left-associatively in nested-loop order.
1184#[derive(Debug, Clone, PartialEq)]
1185pub struct FromClause {
1186    pub primary: TableRef,
1187    pub joins: Vec<FromJoin>,
1188}
1189
1190#[derive(Debug, Clone, PartialEq)]
1191pub struct FromJoin {
1192    pub kind: JoinKind,
1193    pub table: TableRef,
1194    /// Required for INNER/LEFT; must be `None` for CROSS / comma-list.
1195    pub on: Option<Expr>,
1196}
1197
1198#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1199pub enum JoinKind {
1200    Inner,
1201    Left,
1202    Cross,
1203}
1204
1205#[derive(Debug, Clone, PartialEq)]
1206pub enum Expr {
1207    Literal(Literal),
1208    Column(ColumnName),
1209    /// v6.1.1 — `$N` parameter placeholder for the extended query
1210    /// protocol. The number is 1-based per PostgreSQL convention.
1211    /// Evaluation looks up `params[N-1]` from the prepared-statement
1212    /// bind buffer; out-of-range indices raise a runtime error
1213    /// (same shape as a column-not-found miss).
1214    Placeholder(u16),
1215    Binary {
1216        lhs: Box<Expr>,
1217        op: BinOp,
1218        rhs: Box<Expr>,
1219    },
1220    Unary {
1221        op: UnOp,
1222        expr: Box<Expr>,
1223    },
1224    /// PG-style `expr::TYPE` cast. v1.3 supports VECTOR, INT, BIGINT, FLOAT,
1225    /// TEXT, BOOL targets; engine coerces at evaluation time.
1226    Cast {
1227        expr: Box<Expr>,
1228        target: CastTarget,
1229    },
1230    /// Postfix `IS NULL` / `IS NOT NULL`. Returns BOOL.
1231    IsNull {
1232        expr: Box<Expr>,
1233        negated: bool,
1234    },
1235    /// Function call `name(args...)`. v1.4 supports a small built-in set
1236    /// (length, upper, lower, abs, coalesce); unknown names error at eval
1237    /// time so the parser stays open for v1.5 aggregates.
1238    FunctionCall {
1239        name: String,
1240        args: Vec<Expr>,
1241    },
1242    /// SQL `LIKE` predicate. `pattern` evaluates to text at runtime;
1243    /// wildcards are `%` (any run) and `_` (one char), backslash escapes
1244    /// the next char (so `\%` matches a literal `%`).
1245    Like {
1246        expr: Box<Expr>,
1247        pattern: Box<Expr>,
1248        negated: bool,
1249    },
1250    /// v4.12 window function call: `name(args) OVER (PARTITION BY
1251    /// ... ORDER BY ...)`. Supports `ROW_NUMBER` / `RANK` /
1252    /// `DENSE_RANK` and the partition-aware aggregates `SUM` /
1253    /// `AVG` / `COUNT` / `MIN` / `MAX`. The window frame defaults to "entire partition" for
1254    /// unordered windows and "from start of partition through
1255    /// current row" for ordered windows — no explicit ROWS /
1256    /// RANGE clause in v4.12 MVP.
1257    WindowFunction {
1258        name: String,
1259        args: Vec<Expr>,
1260        partition_by: Vec<Expr>,
1261        order_by: Vec<(Expr, bool /* desc */)>,
1262        /// v4.20 explicit frame. `None` means "use the default":
1263        /// whole-partition when unordered, running aggregate from
1264        /// partition start through current row when ordered.
1265        frame: Option<WindowFrame>,
1266        /// v6.4.2 — `IGNORE NULLS` / `RESPECT NULLS` modifier on
1267        /// LAG / LEAD / FIRST_VALUE / LAST_VALUE. Default is
1268        /// `Respect` (PG / ANSI default — NULLs participate). Other
1269        /// window functions ignore this flag.
1270        null_treatment: NullTreatment,
1271    },
1272    /// v4.10 scalar subquery — `(SELECT ...)` used in expression
1273    /// position. Must return exactly one row × one column at eval
1274    /// time; the engine errors out otherwise. Uncorrelated only —
1275    /// the inner SELECT cannot reference outer columns.
1276    ScalarSubquery(Box<SelectStatement>),
1277    /// v4.10 `[NOT] EXISTS (SELECT ...)`. Returns Bool. Inner
1278    /// projection is ignored; only row-count matters.
1279    Exists {
1280        subquery: Box<SelectStatement>,
1281        negated: bool,
1282    },
1283    /// v4.10 `expr [NOT] IN (SELECT ...)`. Inner SELECT must
1284    /// project exactly one column; membership is tested by Eq
1285    /// against each row's value (NULL handling follows ANSI:
1286    /// NULL ∈ list ⇒ NULL ; otherwise present ⇒ true).
1287    InSubquery {
1288        expr: Box<Expr>,
1289        subquery: Box<SelectStatement>,
1290        negated: bool,
1291    },
1292    /// `EXTRACT(<field> FROM <source>)` — pull an integer component
1293    /// out of a `DATE` or `TIMESTAMP`. Parsed as its own AST node
1294    /// because the `FROM` keyword is what separates the two halves,
1295    /// not a comma.
1296    Extract {
1297        field: ExtractField,
1298        source: Box<Expr>,
1299    },
1300    /// v7.10.10 — `ARRAY[expr, expr, …]` array constructor. Each
1301    /// element is evaluated independently; NULLs are allowed.
1302    /// v7.10 supports only single-dimension TEXT[] semantically;
1303    /// non-text elements coerce at engine evaluation time when
1304    /// the surrounding context (column type / cast) makes the
1305    /// target clear.
1306    Array(Vec<Expr>),
1307    /// v7.10.10 — array subscript `arr[i]`. PG 1-based; the
1308    /// engine returns NULL for out-of-range indices.
1309    ArraySubscript {
1310        target: Box<Expr>,
1311        index: Box<Expr>,
1312    },
1313    /// v7.10.12 — `expr op ANY(arr)` and `expr op ALL(arr)`. The
1314    /// operator is the comparison binary op (Eq / Ne / Lt / …);
1315    /// the engine desugars: `ANY` returns true if any element
1316    /// satisfies; `ALL` returns true only if every element does.
1317    /// NULL handling follows PG's three-valued logic.
1318    AnyAll {
1319        expr: Box<Expr>,
1320        op: BinOp,
1321        array: Box<Expr>,
1322        /// `true` = ANY, `false` = ALL.
1323        is_any: bool,
1324    },
1325    /// v7.13.0 — `CASE WHEN <cond> THEN <val> ... ELSE <val> END`
1326    /// (searched form, `operand` is None) and
1327    /// `CASE <expr> WHEN <val> THEN <val> ... END` (simple form,
1328    /// `operand` is the lead expression compared against each
1329    /// branch's match). Each `(when_expr, then_expr)` branch
1330    /// stays as written; engine short-circuits on the first match.
1331    /// `else_branch` is `None` when no ELSE; evaluates to NULL.
1332    /// mailrs round-5 G9.
1333    Case {
1334        operand: Option<Box<Expr>>,
1335        branches: Vec<(Expr, Expr)>,
1336        else_branch: Option<Box<Expr>>,
1337    },
1338}
1339
1340/// v6.4.2 — null treatment on `LAG` / `LEAD` / `FIRST_VALUE` /
1341/// `LAST_VALUE`. PG / ANSI default is `Respect` — NULLs participate
1342/// in the offset walk. `Ignore` causes the function to skip NULL
1343/// values in the argument expression, returning the next non-NULL.
1344#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
1345pub enum NullTreatment {
1346    #[default]
1347    Respect,
1348    Ignore,
1349}
1350
1351/// v4.20 explicit window frame: `ROWS|RANGE BETWEEN <bound> AND
1352/// <bound>`. `end` is `None` for the shorthand "ROWS <bound>"
1353/// where end implicitly = CURRENT ROW.
1354#[derive(Debug, Clone, PartialEq, Eq)]
1355pub struct WindowFrame {
1356    pub kind: FrameKind,
1357    pub start: FrameBound,
1358    pub end: Option<FrameBound>,
1359}
1360
1361#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1362pub enum FrameKind {
1363    Rows,
1364    Range,
1365}
1366
1367#[derive(Debug, Clone, PartialEq, Eq)]
1368pub enum FrameBound {
1369    UnboundedPreceding,
1370    OffsetPreceding(u64),
1371    CurrentRow,
1372    OffsetFollowing(u64),
1373    UnboundedFollowing,
1374}
1375
1376impl fmt::Display for FrameBound {
1377    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1378        match self {
1379            Self::UnboundedPreceding => f.write_str("UNBOUNDED PRECEDING"),
1380            Self::OffsetPreceding(n) => write!(f, "{n} PRECEDING"),
1381            Self::CurrentRow => f.write_str("CURRENT ROW"),
1382            Self::OffsetFollowing(n) => write!(f, "{n} FOLLOWING"),
1383            Self::UnboundedFollowing => f.write_str("UNBOUNDED FOLLOWING"),
1384        }
1385    }
1386}
1387
1388#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1389pub enum ExtractField {
1390    Year,
1391    Month,
1392    Day,
1393    Hour,
1394    Minute,
1395    Second,
1396    Microsecond,
1397}
1398
1399impl fmt::Display for ExtractField {
1400    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1401        f.write_str(match self {
1402            Self::Year => "YEAR",
1403            Self::Month => "MONTH",
1404            Self::Day => "DAY",
1405            Self::Hour => "HOUR",
1406            Self::Minute => "MINUTE",
1407            Self::Second => "SECOND",
1408            Self::Microsecond => "MICROSECOND",
1409        })
1410    }
1411}
1412
1413#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1414pub enum CastTarget {
1415    Int,
1416    BigInt,
1417    Float,
1418    Text,
1419    Bool,
1420    Vector,
1421    Date,
1422    Timestamp,
1423    /// v7.9.25 — `::INTERVAL` and `::TIMESTAMPTZ`. mailrs follow-up
1424    /// H3a. Engine reuses the existing runtime-interval / timestamp
1425    /// paths (parse the text input, return the matching Value).
1426    Interval,
1427    Timestamptz,
1428    /// v7.9.25 — `::JSON` and `::JSONB`. SPG already has both
1429    /// types (v7.9.0); the cast just routes Text→Json with the
1430    /// requested OID for the wire layer.
1431    Json,
1432    Jsonb,
1433    /// v7.9.26 — `::regtype` / `::regclass`. Parsed for PG dump
1434    /// compatibility; engine surfaces as Unsupported with a
1435    /// hint to use `SHOW TABLES` or `spg_table_ddl`. mailrs F3b.
1436    RegType,
1437    RegClass,
1438    /// v7.10.11 — `::TEXT[]`. Engine decodes the LHS Text into
1439    /// the PG external array form `{a,b,NULL}`.
1440    TextArray,
1441    /// v7.11.13 — `::INT[]` / `::BIGINT[]`. Decodes PG external
1442    /// `{1,2,3}` or widens a `TextArray` whose elements are
1443    /// integer-shaped.
1444    IntArray,
1445    BigIntArray,
1446    /// v7.12.0 — `::tsvector` / `::tsquery`. Decodes the PG
1447    /// external form text representation. Used by pg_dump output
1448    /// and by `WHERE col @@ 'term'::tsquery` literal patterns.
1449    TsVector,
1450    TsQuery,
1451}
1452
1453impl fmt::Display for CastTarget {
1454    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1455        f.write_str(match self {
1456            Self::Int => "int",
1457            Self::BigInt => "bigint",
1458            Self::Float => "float",
1459            Self::Text => "text",
1460            Self::Bool => "bool",
1461            Self::Vector => "vector",
1462            Self::Interval => "interval",
1463            Self::Timestamptz => "timestamptz",
1464            Self::Json => "json",
1465            Self::Jsonb => "jsonb",
1466            Self::RegType => "regtype",
1467            Self::RegClass => "regclass",
1468            Self::Date => "date",
1469            Self::Timestamp => "timestamp",
1470            Self::TextArray => "TEXT[]",
1471            Self::IntArray => "INT[]",
1472            Self::BigIntArray => "BIGINT[]",
1473            Self::TsVector => "tsvector",
1474            Self::TsQuery => "tsquery",
1475        })
1476    }
1477}
1478
1479#[derive(Debug, Clone, PartialEq)]
1480pub enum Literal {
1481    Integer(i64),
1482    Float(f64),
1483    String(String),
1484    Bool(bool),
1485    Null,
1486    /// pgvector-style array literal, e.g. `[1, 2.5, -3]`.
1487    Vector(Vec<f32>),
1488    /// `INTERVAL '<n> <unit> [<n> <unit> ...]'` — calendar-aware span.
1489    /// Split into a months part (because a month is not a fixed number of
1490    /// days) and a microseconds part (everything sub-month). `text` keeps
1491    /// the original spelling so Display round-trips byte-for-byte.
1492    Interval {
1493        months: i32,
1494        micros: i64,
1495        text: String,
1496    },
1497}
1498
1499#[derive(Debug, Clone, PartialEq, Eq)]
1500pub struct ColumnName {
1501    pub qualifier: Option<String>,
1502    pub name: String,
1503}
1504
1505#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1506pub enum BinOp {
1507    Or,
1508    And,
1509    Eq,
1510    NotEq,
1511    /// v7.9.27b — PG `a IS DISTINCT FROM b` / `a IS NOT DISTINCT
1512    /// FROM b`. NULL-safe equality: NULL IS NOT DISTINCT FROM
1513    /// NULL → true, NULL IS DISTINCT FROM NULL → false. The
1514    /// non-NULL behaviour matches `<>` / `=` exactly. Common in
1515    /// PG-style JOIN ON predicates and pg_dump output.
1516    IsDistinctFrom,
1517    IsNotDistinctFrom,
1518    Lt,
1519    LtEq,
1520    Gt,
1521    GtEq,
1522    Add,
1523    Sub,
1524    Mul,
1525    Div,
1526    /// pgvector L2 (Euclidean) distance `<->`. Defined for two vector
1527    /// operands of equal dimension; engine returns `Value::Float(d)`.
1528    L2Distance,
1529    /// pgvector inner-product `<#>` — returns `-Σ aᵢ bᵢ` so "smaller =
1530    /// more similar" remains true (matches pgvector's published convention).
1531    InnerProduct,
1532    /// pgvector cosine distance `<=>` — `1 - (a·b)/(|a| |b|)`.
1533    CosineDistance,
1534    /// SQL string concatenation `||`. NULL propagates.
1535    Concat,
1536    /// v4.14 `json -> key` — element access by string key (object)
1537    /// or integer index (array). Returns a JSON value.
1538    JsonGet,
1539    /// v4.14 `json ->> key` — same access, returns the result as
1540    /// TEXT (unwraps a top-level JSON string; renders other scalars
1541    /// as their canonical text).
1542    JsonGetText,
1543    /// v6.4.5 `json #> path_text` — walk the path encoded as a PG
1544    /// text array literal like `'{a,0,b}'`. Returns JSON.
1545    JsonGetPath,
1546    /// v6.4.5 `json #>> path_text` — same walk, returns TEXT.
1547    JsonGetPathText,
1548    /// v6.4.5 `json @> sub_json` — containment. Returns BOOL; true
1549    /// when every key/value in `sub_json` is structurally present in
1550    /// the left side. Matches PG semantics (top-level + recursive).
1551    JsonContains,
1552    /// v7.12.2 `tsvector @@ tsquery` — FTS match. Returns BOOL;
1553    /// 3VL on NULL. Symmetric: PG also accepts `tsquery @@
1554    /// tsvector` and engine eval normalises either ordering.
1555    TsMatch,
1556}
1557
1558#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1559pub enum UnOp {
1560    Not,
1561    Neg,
1562}
1563
1564// --- Display impls (round-trip-safe) --------------------------------------
1565
1566impl fmt::Display for Statement {
1567    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1568        match self {
1569            Self::Select(s) => s.fmt(f),
1570            Self::CreateTable(s) => s.fmt(f),
1571            Self::CreateIndex(s) => s.fmt(f),
1572            Self::Insert(s) => s.fmt(f),
1573            Self::Update(s) => s.fmt(f),
1574            Self::Delete(s) => s.fmt(f),
1575            Self::Begin => f.write_str("BEGIN"),
1576            Self::Commit => f.write_str("COMMIT"),
1577            Self::Rollback => f.write_str("ROLLBACK"),
1578            Self::Savepoint(n) => write!(f, "SAVEPOINT {}", quote_ident(n)),
1579            Self::RollbackToSavepoint(n) => write!(f, "ROLLBACK TO SAVEPOINT {}", quote_ident(n)),
1580            Self::ReleaseSavepoint(n) => write!(f, "RELEASE SAVEPOINT {}", quote_ident(n)),
1581            Self::ShowTables => f.write_str("SHOW TABLES"),
1582            Self::ShowColumns(t) => write!(f, "SHOW COLUMNS FROM {}", quote_ident(t)),
1583            Self::CreateUser(s) => write!(
1584                f,
1585                "CREATE USER {} WITH PASSWORD '<redacted>' ROLE '{}'",
1586                quote_ident(&s.name),
1587                s.role
1588            ),
1589            Self::DropUser(n) => write!(f, "DROP USER {}", quote_ident(n)),
1590            Self::ShowUsers => f.write_str("SHOW USERS"),
1591            Self::ShowPublications => f.write_str("SHOW PUBLICATIONS"),
1592            Self::ShowSubscriptions => f.write_str("SHOW SUBSCRIPTIONS"),
1593            Self::CreateSubscription(s) => {
1594                write!(
1595                    f,
1596                    "CREATE SUBSCRIPTION {} CONNECTION '{}' PUBLICATION ",
1597                    quote_ident(&s.name),
1598                    s.conn_str.replace('\'', "''")
1599                )?;
1600                for (i, p) in s.publications.iter().enumerate() {
1601                    if i > 0 {
1602                        f.write_str(", ")?;
1603                    }
1604                    write!(f, "{}", quote_ident(p))?;
1605                }
1606                Ok(())
1607            }
1608            Self::DropSubscription(name) => {
1609                write!(f, "DROP SUBSCRIPTION {}", quote_ident(name))
1610            }
1611            Self::WaitForWalPosition { pos, timeout_ms } => {
1612                write!(f, "WAIT FOR WAL POSITION {pos}")?;
1613                if let Some(ms) = timeout_ms {
1614                    write!(f, " WITH TIMEOUT {ms}")?;
1615                }
1616                Ok(())
1617            }
1618            Self::Analyze(None) => f.write_str("ANALYZE"),
1619            Self::Analyze(Some(t)) => write!(f, "ANALYZE {}", quote_ident(t)),
1620            Self::CompactColdSegments => f.write_str("COMPACT COLD SEGMENTS"),
1621            Self::Explain(e) => {
1622                if e.suggest {
1623                    write!(f, "EXPLAIN (SUGGEST) {}", e.inner)
1624                } else if e.analyze {
1625                    write!(f, "EXPLAIN ANALYZE {}", e.inner)
1626                } else {
1627                    write!(f, "EXPLAIN {}", e.inner)
1628                }
1629            }
1630            Self::AlterIndex(a) => {
1631                write!(f, "ALTER INDEX {} ", quote_ident(&a.name))?;
1632                match a.target {
1633                    AlterIndexTarget::Rebuild { encoding } => {
1634                        f.write_str("REBUILD")?;
1635                        if let Some(enc) = encoding {
1636                            write!(f, " WITH (encoding = {enc})")?;
1637                        }
1638                        Ok(())
1639                    }
1640                }
1641            }
1642            Self::AlterTable(a) => {
1643                write!(f, "ALTER TABLE {} ", quote_ident(&a.name))?;
1644                for (i, t) in a.targets.iter().enumerate() {
1645                    if i > 0 {
1646                        f.write_str(", ")?;
1647                    }
1648                    fmt_alter_target(f, t)?;
1649                }
1650                Ok(())
1651            }
1652            Self::CreatePublication(p) => {
1653                write!(f, "CREATE PUBLICATION {}", quote_ident(&p.name))?;
1654                match &p.scope {
1655                    PublicationScope::AllTables => f.write_str(" FOR ALL TABLES"),
1656                    PublicationScope::ForTables(ts) => {
1657                        f.write_str(" FOR TABLE ")?;
1658                        for (i, t) in ts.iter().enumerate() {
1659                            if i > 0 {
1660                                f.write_str(", ")?;
1661                            }
1662                            write!(f, "{}", quote_ident(t))?;
1663                        }
1664                        Ok(())
1665                    }
1666                    PublicationScope::AllTablesExcept(ts) => {
1667                        f.write_str(" FOR ALL TABLES EXCEPT ")?;
1668                        for (i, t) in ts.iter().enumerate() {
1669                            if i > 0 {
1670                                f.write_str(", ")?;
1671                            }
1672                            write!(f, "{}", quote_ident(t))?;
1673                        }
1674                        Ok(())
1675                    }
1676                }
1677            }
1678            Self::CreateExtension(name) => {
1679                write!(f, "CREATE EXTENSION IF NOT EXISTS {}", quote_ident(name))
1680            }
1681            Self::DoBlock => f.write_str("DO $$ /* SPG no-op */ $$"),
1682            Self::DropPublication(name) => {
1683                write!(f, "DROP PUBLICATION {}", quote_ident(name))
1684            }
1685            Self::SetParameter { name, value } => {
1686                write!(f, "SET {name} = ")?;
1687                match value {
1688                    SetValue::String(s) => write!(f, "'{}'", s.replace('\'', "''")),
1689                    SetValue::Ident(s) | SetValue::Number(s) => f.write_str(s),
1690                    SetValue::Default => f.write_str("DEFAULT"),
1691                }
1692            }
1693            Self::ResetParameter(None) => f.write_str("RESET ALL"),
1694            Self::ResetParameter(Some(name)) => write!(f, "RESET {name}"),
1695            Self::CreateFunction(s) => s.fmt(f),
1696            Self::CreateTrigger(s) => s.fmt(f),
1697            Self::DropTrigger {
1698                name,
1699                table,
1700                if_exists,
1701            } => {
1702                f.write_str("DROP TRIGGER ")?;
1703                if *if_exists {
1704                    f.write_str("IF EXISTS ")?;
1705                }
1706                write!(f, "{} ON {}", quote_ident(name), quote_ident(table))
1707            }
1708            Self::DropFunction { name, if_exists } => {
1709                f.write_str("DROP FUNCTION ")?;
1710                if *if_exists {
1711                    f.write_str("IF EXISTS ")?;
1712                }
1713                write!(f, "{}", quote_ident(name))
1714            }
1715        }
1716    }
1717}
1718
1719impl fmt::Display for CreateFunctionStatement {
1720    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1721        f.write_str("CREATE ")?;
1722        if self.or_replace {
1723            f.write_str("OR REPLACE ")?;
1724        }
1725        write!(f, "FUNCTION {}(", quote_ident(&self.name))?;
1726        for (i, arg) in self.args.iter().enumerate() {
1727            if i > 0 {
1728                f.write_str(", ")?;
1729            }
1730            match arg.mode {
1731                FunctionArgMode::In => {}
1732                FunctionArgMode::Out => f.write_str("OUT ")?,
1733                FunctionArgMode::InOut => f.write_str("INOUT ")?,
1734            }
1735            if let Some(name) = &arg.name {
1736                write!(f, "{} ", quote_ident(name))?;
1737            }
1738            match &arg.ty {
1739                FunctionArgType::Typed(t) => write!(f, "{t}")?,
1740                FunctionArgType::Raw(s) => f.write_str(s)?,
1741            }
1742        }
1743        f.write_str(") RETURNS ")?;
1744        match &self.returns {
1745            FunctionReturn::Trigger => f.write_str("TRIGGER")?,
1746            FunctionReturn::Void => f.write_str("VOID")?,
1747            FunctionReturn::Type(t) => write!(f, "{t}")?,
1748            FunctionReturn::Other(s) => f.write_str(s)?,
1749        }
1750        write!(f, " LANGUAGE {} AS $$", self.language)?;
1751        match &self.body {
1752            FunctionBody::PlPgSql(b) => write!(f, "\n{b}\n")?,
1753            FunctionBody::Raw(s) => f.write_str(s)?,
1754        }
1755        f.write_str("$$")
1756    }
1757}
1758
1759impl fmt::Display for PlPgSqlBlock {
1760    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1761        if !self.declarations.is_empty() {
1762            f.write_str("DECLARE\n")?;
1763            for d in &self.declarations {
1764                write!(f, "  {} ", quote_ident(&d.name))?;
1765                match &d.ty {
1766                    FunctionArgType::Typed(t) => write!(f, "{t}")?,
1767                    FunctionArgType::Raw(s) => f.write_str(s)?,
1768                }
1769                if let Some(e) = &d.default {
1770                    write!(f, " := {e}")?;
1771                }
1772                f.write_str(";\n")?;
1773            }
1774        }
1775        f.write_str("BEGIN\n")?;
1776        for stmt in &self.statements {
1777            writeln!(f, "  {stmt};")?;
1778        }
1779        f.write_str("END")
1780    }
1781}
1782
1783impl fmt::Display for PlPgSqlStmt {
1784    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1785        match self {
1786            Self::Assign { target, value } => write!(f, "{target} := {value}"),
1787            Self::Return(t) => match t {
1788                ReturnTarget::New => f.write_str("RETURN NEW"),
1789                ReturnTarget::Old => f.write_str("RETURN OLD"),
1790                ReturnTarget::Null => f.write_str("RETURN NULL"),
1791                ReturnTarget::Expr(e) => write!(f, "RETURN {e}"),
1792            },
1793            Self::If {
1794                branches,
1795                else_branch,
1796            } => {
1797                for (i, (cond, body)) in branches.iter().enumerate() {
1798                    if i == 0 {
1799                        write!(f, "IF {cond} THEN ")?;
1800                    } else {
1801                        write!(f, " ELSIF {cond} THEN ")?;
1802                    }
1803                    for (j, s) in body.iter().enumerate() {
1804                        if j > 0 {
1805                            f.write_str("; ")?;
1806                        }
1807                        write!(f, "{s}")?;
1808                    }
1809                }
1810                if !else_branch.is_empty() {
1811                    f.write_str(" ELSE ")?;
1812                    for (j, s) in else_branch.iter().enumerate() {
1813                        if j > 0 {
1814                            f.write_str("; ")?;
1815                        }
1816                        write!(f, "{s}")?;
1817                    }
1818                }
1819                f.write_str(" END IF")
1820            }
1821            Self::Raise {
1822                level,
1823                message,
1824                args,
1825            } => {
1826                let lvl = match level {
1827                    RaiseLevel::Notice => "NOTICE",
1828                    RaiseLevel::Warning => "WARNING",
1829                    RaiseLevel::Info => "INFO",
1830                    RaiseLevel::Log => "LOG",
1831                    RaiseLevel::Debug => "DEBUG",
1832                    RaiseLevel::Exception => "EXCEPTION",
1833                };
1834                write!(f, "RAISE {lvl} '{}'", message.replace('\'', "''"))?;
1835                for a in args {
1836                    write!(f, ", {a}")?;
1837                }
1838                Ok(())
1839            }
1840            Self::EmbeddedSql(s) => write!(f, "{s}"),
1841        }
1842    }
1843}
1844
1845impl fmt::Display for AssignTarget {
1846    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1847        match self {
1848            Self::NewColumn(c) => write!(f, "NEW.{}", quote_ident(c)),
1849            Self::OldColumn(c) => write!(f, "OLD.{}", quote_ident(c)),
1850            Self::Local(n) => f.write_str(n),
1851        }
1852    }
1853}
1854
1855impl fmt::Display for CreateTriggerStatement {
1856    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1857        f.write_str("CREATE ")?;
1858        if self.or_replace {
1859            f.write_str("OR REPLACE ")?;
1860        }
1861        write!(f, "TRIGGER {} ", quote_ident(&self.name))?;
1862        match self.timing {
1863            TriggerTiming::Before => f.write_str("BEFORE")?,
1864            TriggerTiming::After => f.write_str("AFTER")?,
1865            TriggerTiming::InsteadOf => f.write_str("INSTEAD OF")?,
1866        }
1867        for (i, e) in self.events.iter().enumerate() {
1868            if i == 0 {
1869                f.write_str(" ")?;
1870            } else {
1871                f.write_str(" OR ")?;
1872            }
1873            match e {
1874                TriggerEvent::Insert => f.write_str("INSERT")?,
1875                TriggerEvent::Update => {
1876                    f.write_str("UPDATE")?;
1877                    if !self.update_columns.is_empty() {
1878                        f.write_str(" OF ")?;
1879                        for (j, col) in self.update_columns.iter().enumerate() {
1880                            if j > 0 {
1881                                f.write_str(", ")?;
1882                            }
1883                            f.write_str(&quote_ident(col))?;
1884                        }
1885                    }
1886                }
1887                TriggerEvent::Delete => f.write_str("DELETE")?,
1888                TriggerEvent::Truncate => f.write_str("TRUNCATE")?,
1889            }
1890        }
1891        write!(f, " ON {} FOR EACH ", quote_ident(&self.table))?;
1892        match self.for_each {
1893            TriggerForEach::Row => f.write_str("ROW")?,
1894            TriggerForEach::Statement => f.write_str("STATEMENT")?,
1895        }
1896        write!(f, " EXECUTE FUNCTION {}()", quote_ident(&self.function))
1897    }
1898}
1899
1900impl fmt::Display for CreateIndexStatement {
1901    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1902        if self.is_unique {
1903            f.write_str("CREATE UNIQUE INDEX ")?;
1904        } else {
1905            f.write_str("CREATE INDEX ")?;
1906        }
1907        if self.if_not_exists {
1908            f.write_str("IF NOT EXISTS ")?;
1909        }
1910        write!(
1911            f,
1912            "{} ON {} ",
1913            quote_ident(&self.name),
1914            quote_ident(&self.table)
1915        )?;
1916        match self.method {
1917            IndexMethod::Hnsw => f.write_str("USING hnsw ")?,
1918            IndexMethod::Brin => f.write_str("USING brin ")?,
1919            IndexMethod::Gin => f.write_str("USING gin ")?,
1920            IndexMethod::BTree => {}
1921        }
1922        if let Some(expr) = &self.expression {
1923            write!(f, "({})", expr)?;
1924        } else if self.extra_columns.is_empty() {
1925            write!(f, "({})", quote_ident(&self.column))?;
1926        } else {
1927            // v7.9.14 — multi-column key. Emit each column quoted
1928            // so the round-tripped form re-parses to identical AST.
1929            f.write_str("(")?;
1930            write!(f, "{}", quote_ident(&self.column))?;
1931            for c in &self.extra_columns {
1932                write!(f, ", {}", quote_ident(c))?;
1933            }
1934            f.write_str(")")?;
1935        }
1936        if !self.included_columns.is_empty() {
1937            f.write_str(" INCLUDE (")?;
1938            for (i, c) in self.included_columns.iter().enumerate() {
1939                if i > 0 {
1940                    f.write_str(", ")?;
1941                }
1942                write!(f, "{}", quote_ident(c))?;
1943            }
1944            f.write_str(")")?;
1945        }
1946        if let Some(pred) = &self.partial_predicate {
1947            write!(f, " WHERE {}", pred)?;
1948        }
1949        Ok(())
1950    }
1951}
1952
1953impl fmt::Display for CreateTableStatement {
1954    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1955        f.write_str("CREATE TABLE ")?;
1956        if self.if_not_exists {
1957            f.write_str("IF NOT EXISTS ")?;
1958        }
1959        write!(f, "{} (", quote_ident(&self.name))?;
1960        for (i, col) in self.columns.iter().enumerate() {
1961            if i > 0 {
1962                f.write_str(", ")?;
1963            }
1964            write!(f, "{col}")?;
1965        }
1966        // v7.6.0 — render FK constraints in table-level form, after
1967        // the column list. WAL replay round-trips through Display, so
1968        // every FK must serialise here for replay to reconstruct the
1969        // schema bit-for-bit.
1970        for fk in &self.foreign_keys {
1971            f.write_str(", ")?;
1972            write!(f, "{fk}")?;
1973        }
1974        // v7.13.0 — render table-level constraints (PRIMARY KEY /
1975        // UNIQUE / CHECK) so WAL replay reconstructs them. Inline
1976        // column-level UNIQUE / CHECK get lifted to this list at
1977        // parse time, so emitting only here avoids double-counting.
1978        for tc in &self.table_constraints {
1979            f.write_str(", ")?;
1980            write!(f, "{tc}")?;
1981        }
1982        f.write_str(")")
1983    }
1984}
1985
1986fn fmt_alter_target(f: &mut fmt::Formatter<'_>, t: &AlterTableTarget) -> fmt::Result {
1987    match t {
1988        AlterTableTarget::SetHotTierBytes(n) => {
1989            write!(f, "SET hot_tier_bytes = {n}")
1990        }
1991        AlterTableTarget::AddForeignKey(fk) => write!(f, "ADD {fk}"),
1992        AlterTableTarget::DropForeignKey { name, if_exists } => {
1993            f.write_str("DROP CONSTRAINT ")?;
1994            if *if_exists {
1995                f.write_str("IF EXISTS ")?;
1996            }
1997            write!(f, "{}", quote_ident(name))
1998        }
1999        AlterTableTarget::AddColumn {
2000            column,
2001            if_not_exists,
2002        } => {
2003            f.write_str("ADD COLUMN ")?;
2004            if *if_not_exists {
2005                f.write_str("IF NOT EXISTS ")?;
2006            }
2007            write!(f, "{} {}", quote_ident(&column.name), column.ty)?;
2008            if !column.nullable {
2009                f.write_str(" NOT NULL")?;
2010            }
2011            if let Some(d) = &column.default {
2012                write!(f, " DEFAULT {d}")?;
2013            }
2014            if column.auto_increment {
2015                f.write_str(" AUTO_INCREMENT")?;
2016            }
2017            if column.is_primary_key {
2018                f.write_str(" PRIMARY KEY")?;
2019            }
2020            Ok(())
2021        }
2022        AlterTableTarget::AlterColumnType {
2023            column,
2024            new_type,
2025            using,
2026        } => {
2027            write!(f, "ALTER COLUMN {} TYPE {new_type}", quote_ident(column))?;
2028            if let Some(u) = using {
2029                write!(f, " USING {u}")?;
2030            }
2031            Ok(())
2032        }
2033    }
2034}
2035
2036impl fmt::Display for TableConstraint {
2037    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2038        match self {
2039            Self::PrimaryKey { name, columns } => {
2040                if let Some(n) = name {
2041                    write!(f, "CONSTRAINT {} ", quote_ident(n))?;
2042                }
2043                f.write_str("PRIMARY KEY (")?;
2044                for (i, c) in columns.iter().enumerate() {
2045                    if i > 0 {
2046                        f.write_str(", ")?;
2047                    }
2048                    f.write_str(&quote_ident(c))?;
2049                }
2050                f.write_str(")")
2051            }
2052            Self::Unique {
2053                name,
2054                columns,
2055                nulls_not_distinct,
2056            } => {
2057                if let Some(n) = name {
2058                    write!(f, "CONSTRAINT {} ", quote_ident(n))?;
2059                }
2060                f.write_str("UNIQUE ")?;
2061                if *nulls_not_distinct {
2062                    f.write_str("NULLS NOT DISTINCT ")?;
2063                }
2064                f.write_str("(")?;
2065                for (i, c) in columns.iter().enumerate() {
2066                    if i > 0 {
2067                        f.write_str(", ")?;
2068                    }
2069                    f.write_str(&quote_ident(c))?;
2070                }
2071                f.write_str(")")
2072            }
2073            Self::Check { name, expr } => {
2074                if let Some(n) = name {
2075                    write!(f, "CONSTRAINT {} ", quote_ident(n))?;
2076                }
2077                write!(f, "CHECK ({expr})")
2078            }
2079        }
2080    }
2081}
2082
2083impl fmt::Display for ForeignKeyConstraint {
2084    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2085        if let Some(name) = &self.name {
2086            write!(f, "CONSTRAINT {} ", quote_ident(name))?;
2087        }
2088        f.write_str("FOREIGN KEY (")?;
2089        for (i, c) in self.columns.iter().enumerate() {
2090            if i > 0 {
2091                f.write_str(", ")?;
2092            }
2093            f.write_str(&quote_ident(c))?;
2094        }
2095        write!(f, ") REFERENCES {}", quote_ident(&self.parent_table))?;
2096        if !self.parent_columns.is_empty() {
2097            f.write_str(" (")?;
2098            for (i, c) in self.parent_columns.iter().enumerate() {
2099                if i > 0 {
2100                    f.write_str(", ")?;
2101                }
2102                f.write_str(&quote_ident(c))?;
2103            }
2104            f.write_str(")")?;
2105        }
2106        // Only render non-default actions to keep Display output
2107        // close to user input. SPG's default is RESTRICT (matches
2108        // SQL spec).
2109        if self.on_delete != FkAction::Restrict {
2110            write!(f, " ON DELETE {}", self.on_delete)?;
2111        }
2112        if self.on_update != FkAction::Restrict {
2113            write!(f, " ON UPDATE {}", self.on_update)?;
2114        }
2115        Ok(())
2116    }
2117}
2118
2119impl fmt::Display for FkAction {
2120    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2121        match self {
2122            Self::Restrict => f.write_str("RESTRICT"),
2123            Self::Cascade => f.write_str("CASCADE"),
2124            Self::SetNull => f.write_str("SET NULL"),
2125            Self::SetDefault => f.write_str("SET DEFAULT"),
2126            Self::NoAction => f.write_str("NO ACTION"),
2127        }
2128    }
2129}
2130
2131impl fmt::Display for ColumnDef {
2132    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2133        write!(f, "{} {}", quote_ident(&self.name), self.ty)?;
2134        if let Some(d) = &self.default {
2135            write!(f, " DEFAULT {d}")?;
2136        }
2137        if self.auto_increment {
2138            f.write_str(" AUTO_INCREMENT")?;
2139        }
2140        if !self.nullable {
2141            f.write_str(" NOT NULL")?;
2142        }
2143        Ok(())
2144    }
2145}
2146
2147impl fmt::Display for InsertStatement {
2148    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2149        write!(f, "INSERT INTO {}", quote_ident(&self.table))?;
2150        if let Some(cols) = &self.columns {
2151            f.write_str(" (")?;
2152            for (i, c) in cols.iter().enumerate() {
2153                if i > 0 {
2154                    f.write_str(", ")?;
2155                }
2156                f.write_str(&quote_ident(c))?;
2157            }
2158            f.write_str(")")?;
2159        }
2160        // v7.13.0 — INSERT…SELECT renders as `... SELECT …`,
2161        // skipping the VALUES list (mailrs round-5 G4).
2162        if let Some(sel) = &self.select_source {
2163            write!(f, " {sel}")?;
2164        } else {
2165            f.write_str(" VALUES ")?;
2166            for (ri, row) in self.rows.iter().enumerate() {
2167                if ri > 0 {
2168                    f.write_str(", ")?;
2169                }
2170                f.write_str("(")?;
2171                for (i, v) in row.iter().enumerate() {
2172                    if i > 0 {
2173                        f.write_str(", ")?;
2174                    }
2175                    write!(f, "{v}")?;
2176                }
2177                f.write_str(")")?;
2178            }
2179        }
2180        Ok(())
2181    }
2182}
2183
2184impl fmt::Display for UpdateStatement {
2185    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2186        write!(f, "UPDATE {} SET ", quote_ident(&self.table))?;
2187        for (i, (col, expr)) in self.assignments.iter().enumerate() {
2188            if i > 0 {
2189                f.write_str(", ")?;
2190            }
2191            write!(f, "{} = {expr}", quote_ident(col))?;
2192        }
2193        if let Some(w) = &self.where_ {
2194            write!(f, " WHERE {w}")?;
2195        }
2196        Ok(())
2197    }
2198}
2199
2200impl fmt::Display for DeleteStatement {
2201    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2202        write!(f, "DELETE FROM {}", quote_ident(&self.table))?;
2203        if let Some(w) = &self.where_ {
2204            write!(f, " WHERE {w}")?;
2205        }
2206        Ok(())
2207    }
2208}
2209
2210impl fmt::Display for SelectStatement {
2211    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2212        write_bare_select(self, f)?;
2213        for (kind, peer) in &self.unions {
2214            f.write_str(match kind {
2215                UnionKind::Distinct => " UNION ",
2216                UnionKind::All => " UNION ALL ",
2217            })?;
2218            write_bare_select(peer, f)?;
2219        }
2220        if !self.order_by.is_empty() {
2221            f.write_str(" ORDER BY ")?;
2222            for (i, o) in self.order_by.iter().enumerate() {
2223                if i > 0 {
2224                    f.write_str(", ")?;
2225                }
2226                write!(f, "{}", o.expr)?;
2227                if o.desc {
2228                    f.write_str(" DESC")?;
2229                }
2230            }
2231        }
2232        if let Some(n) = &self.limit {
2233            write!(f, " LIMIT {n}")?;
2234        }
2235        if let Some(o) = &self.offset {
2236            write!(f, " OFFSET {o}")?;
2237        }
2238        Ok(())
2239    }
2240}
2241
2242fn write_bare_select(s: &SelectStatement, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2243    f.write_str("SELECT ")?;
2244    if s.distinct {
2245        f.write_str("DISTINCT ")?;
2246    }
2247    write_bare_select_body(s, f)
2248}
2249
2250fn write_bare_select_body(s: &SelectStatement, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2251    for (i, item) in s.items.iter().enumerate() {
2252        if i > 0 {
2253            f.write_str(", ")?;
2254        }
2255        write!(f, "{item}")?;
2256    }
2257    if let Some(t) = &s.from {
2258        write!(f, " FROM {t}")?;
2259    }
2260    if let Some(e) = &s.where_ {
2261        write!(f, " WHERE {e}")?;
2262    }
2263    if let Some(gs) = &s.group_by {
2264        f.write_str(" GROUP BY ")?;
2265        for (i, g) in gs.iter().enumerate() {
2266            if i > 0 {
2267                f.write_str(", ")?;
2268            }
2269            write!(f, "{g}")?;
2270        }
2271    }
2272    if let Some(h) = &s.having {
2273        write!(f, " HAVING {h}")?;
2274    }
2275    Ok(())
2276}
2277
2278impl fmt::Display for SelectItem {
2279    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2280        match self {
2281            Self::Wildcard => f.write_str("*"),
2282            Self::Expr { expr, alias } => {
2283                write!(f, "{expr}")?;
2284                if let Some(a) = alias {
2285                    write!(f, " AS {}", quote_ident(a))?;
2286                }
2287                Ok(())
2288            }
2289        }
2290    }
2291}
2292
2293impl fmt::Display for FromClause {
2294    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2295        write!(f, "{}", self.primary)?;
2296        for j in &self.joins {
2297            match j.kind {
2298                JoinKind::Inner => write!(f, " INNER JOIN {}", j.table)?,
2299                JoinKind::Left => write!(f, " LEFT JOIN {}", j.table)?,
2300                JoinKind::Cross => write!(f, " CROSS JOIN {}", j.table)?,
2301            }
2302            if let Some(on) = &j.on {
2303                write!(f, " ON {on}")?;
2304            }
2305        }
2306        Ok(())
2307    }
2308}
2309
2310impl fmt::Display for TableRef {
2311    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2312        write!(f, "{}", quote_ident(&self.name))?;
2313        if let Some(a) = &self.alias {
2314            write!(f, " AS {}", quote_ident(a))?;
2315        }
2316        Ok(())
2317    }
2318}
2319
2320impl fmt::Display for ColumnName {
2321    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2322        if let Some(q) = &self.qualifier {
2323            write!(f, "{}.{}", quote_ident(q), quote_ident(&self.name))
2324        } else {
2325            write!(f, "{}", quote_ident(&self.name))
2326        }
2327    }
2328}
2329
2330impl fmt::Display for Expr {
2331    #[allow(clippy::too_many_lines)]
2332    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2333        match self {
2334            Self::Literal(l) => write!(f, "{l}"),
2335            Self::Column(c) => write!(f, "{c}"),
2336            Self::Placeholder(n) => write!(f, "${n}"),
2337            Self::Binary { lhs, op, rhs } => write!(f, "({lhs} {op} {rhs})"),
2338            Self::Unary { op, expr } => match op {
2339                UnOp::Not => write!(f, "(NOT {expr})"),
2340                UnOp::Neg => write!(f, "(-{expr})"),
2341            },
2342            Self::Cast { expr, target } => write!(f, "({expr}::{target})"),
2343            Self::IsNull { expr, negated } => {
2344                if *negated {
2345                    write!(f, "({expr} IS NOT NULL)")
2346                } else {
2347                    write!(f, "({expr} IS NULL)")
2348                }
2349            }
2350            Self::FunctionCall { name, args } => {
2351                write!(f, "{name}(")?;
2352                for (i, a) in args.iter().enumerate() {
2353                    if i > 0 {
2354                        f.write_str(", ")?;
2355                    }
2356                    write!(f, "{a}")?;
2357                }
2358                f.write_str(")")
2359            }
2360            Self::Like {
2361                expr,
2362                pattern,
2363                negated,
2364            } => {
2365                if *negated {
2366                    write!(f, "({expr} NOT LIKE {pattern})")
2367                } else {
2368                    write!(f, "({expr} LIKE {pattern})")
2369                }
2370            }
2371            Self::Extract { field, source } => write!(f, "EXTRACT({field} FROM {source})"),
2372            Self::WindowFunction {
2373                name,
2374                args,
2375                partition_by,
2376                order_by,
2377                frame,
2378                null_treatment: _,
2379            } => {
2380                write!(f, "{name}(")?;
2381                for (i, a) in args.iter().enumerate() {
2382                    if i > 0 {
2383                        f.write_str(", ")?;
2384                    }
2385                    write!(f, "{a}")?;
2386                }
2387                f.write_str(") OVER (")?;
2388                if !partition_by.is_empty() {
2389                    f.write_str("PARTITION BY ")?;
2390                    for (i, p) in partition_by.iter().enumerate() {
2391                        if i > 0 {
2392                            f.write_str(", ")?;
2393                        }
2394                        write!(f, "{p}")?;
2395                    }
2396                }
2397                if !order_by.is_empty() {
2398                    if !partition_by.is_empty() {
2399                        f.write_str(" ")?;
2400                    }
2401                    f.write_str("ORDER BY ")?;
2402                    for (i, (e, desc)) in order_by.iter().enumerate() {
2403                        if i > 0 {
2404                            f.write_str(", ")?;
2405                        }
2406                        write!(f, "{e}")?;
2407                        if *desc {
2408                            f.write_str(" DESC")?;
2409                        }
2410                    }
2411                }
2412                if let Some(fr) = frame {
2413                    if !partition_by.is_empty() || !order_by.is_empty() {
2414                        f.write_str(" ")?;
2415                    }
2416                    let k = match fr.kind {
2417                        FrameKind::Rows => "ROWS",
2418                        FrameKind::Range => "RANGE",
2419                    };
2420                    if let Some(end) = &fr.end {
2421                        write!(f, "{k} BETWEEN {} AND {}", fr.start, end)?;
2422                    } else {
2423                        write!(f, "{k} {}", fr.start)?;
2424                    }
2425                }
2426                f.write_str(")")
2427            }
2428            Self::ScalarSubquery(s) => write!(f, "({s})"),
2429            Self::Exists { subquery, negated } => {
2430                if *negated {
2431                    write!(f, "NOT EXISTS ({subquery})")
2432                } else {
2433                    write!(f, "EXISTS ({subquery})")
2434                }
2435            }
2436            Self::InSubquery {
2437                expr,
2438                subquery,
2439                negated,
2440            } => {
2441                if *negated {
2442                    write!(f, "({expr} NOT IN ({subquery}))")
2443                } else {
2444                    write!(f, "({expr} IN ({subquery}))")
2445                }
2446            }
2447            Self::Array(items) => {
2448                f.write_str("ARRAY[")?;
2449                for (i, e) in items.iter().enumerate() {
2450                    if i > 0 {
2451                        f.write_str(", ")?;
2452                    }
2453                    write!(f, "{e}")?;
2454                }
2455                f.write_str("]")
2456            }
2457            Self::ArraySubscript { target, index } => write!(f, "({target}[{index}])"),
2458            Self::AnyAll {
2459                expr,
2460                op,
2461                array,
2462                is_any,
2463            } => {
2464                let kw = if *is_any { "ANY" } else { "ALL" };
2465                write!(f, "({expr} {op} {kw}({array}))")
2466            }
2467            Self::Case {
2468                operand,
2469                branches,
2470                else_branch,
2471            } => {
2472                f.write_str("CASE")?;
2473                if let Some(op) = operand {
2474                    write!(f, " {op}")?;
2475                }
2476                for (w, t) in branches {
2477                    write!(f, " WHEN {w} THEN {t}")?;
2478                }
2479                if let Some(e) = else_branch {
2480                    write!(f, " ELSE {e}")?;
2481                }
2482                f.write_str(" END")
2483            }
2484        }
2485    }
2486}
2487
2488impl fmt::Display for Literal {
2489    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2490        match self {
2491            Self::Integer(n) => write!(f, "{n}"),
2492            Self::Float(x) => {
2493                let s = format!("{x}");
2494                // Default Display for an integral f64 (e.g. 1.0) emits "1",
2495                // which would round-trip back to Integer. Force a dot.
2496                if s.contains('.') || s.contains('e') || s.contains('E') {
2497                    f.write_str(&s)
2498                } else {
2499                    write!(f, "{s}.0")
2500                }
2501            }
2502            Self::String(s) => {
2503                f.write_str("'")?;
2504                for c in s.chars() {
2505                    if c == '\'' {
2506                        f.write_str("''")?;
2507                    } else {
2508                        write!(f, "{c}")?;
2509                    }
2510                }
2511                f.write_str("'")
2512            }
2513            Self::Bool(b) => f.write_str(if *b { "TRUE" } else { "FALSE" }),
2514            Self::Null => f.write_str("NULL"),
2515            Self::Vector(v) => {
2516                f.write_str("[")?;
2517                for (i, x) in v.iter().enumerate() {
2518                    if i > 0 {
2519                        f.write_str(", ")?;
2520                    }
2521                    let s = format!("{x}");
2522                    // Mirror Float Display: force a dot so re-parse stays
2523                    // numerically literal.
2524                    if s.contains('.') || s.contains('e') || s.contains('E') {
2525                        f.write_str(&s)?;
2526                    } else {
2527                        write!(f, "{s}.0")?;
2528                    }
2529                }
2530                f.write_str("]")
2531            }
2532            Self::Interval { text, .. } => {
2533                f.write_str("INTERVAL '")?;
2534                for c in text.chars() {
2535                    if c == '\'' {
2536                        f.write_str("''")?;
2537                    } else {
2538                        write!(f, "{c}")?;
2539                    }
2540                }
2541                f.write_str("'")
2542            }
2543        }
2544    }
2545}
2546
2547impl fmt::Display for BinOp {
2548    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2549        f.write_str(match self {
2550            Self::Or => "OR",
2551            Self::And => "AND",
2552            Self::Eq => "=",
2553            Self::NotEq => "<>",
2554            Self::IsDistinctFrom => "IS DISTINCT FROM",
2555            Self::IsNotDistinctFrom => "IS NOT DISTINCT FROM",
2556            Self::Lt => "<",
2557            Self::LtEq => "<=",
2558            Self::Gt => ">",
2559            Self::GtEq => ">=",
2560            Self::Add => "+",
2561            Self::Sub => "-",
2562            Self::Mul => "*",
2563            Self::Div => "/",
2564            Self::L2Distance => "<->",
2565            Self::InnerProduct => "<#>",
2566            Self::CosineDistance => "<=>",
2567            Self::Concat => "||",
2568            Self::JsonGet => "->",
2569            Self::JsonGetText => "->>",
2570            Self::JsonGetPath => "#>",
2571            Self::JsonGetPathText => "#>>",
2572            Self::JsonContains => "@>",
2573            Self::TsMatch => "@@",
2574        })
2575    }
2576}
2577
2578/// Quote `s` as a PG double-quoted identifier when required (keyword,
2579/// non-folded case, leading digit, embedded non-`[A-Za-z0-9_]`, empty).
2580/// Otherwise return it as-is. Returns an owned `String` to keep the call site
2581/// uniform.
2582fn quote_ident(s: &str) -> String {
2583    let needs_quote = match s.chars().next() {
2584        None => true,
2585        Some(c) if !c.is_ascii_alphabetic() && c != '_' => true,
2586        _ => {
2587            s.chars().any(|c| !(c.is_ascii_alphanumeric() || c == '_'))
2588                || s.chars().any(|c| c.is_ascii_uppercase())
2589                || is_keyword(s)
2590        }
2591    };
2592    if !needs_quote {
2593        return s.to_string();
2594    }
2595    let mut out = String::with_capacity(s.len() + 2);
2596    out.push('"');
2597    for c in s.chars() {
2598        if c == '"' {
2599            out.push_str("\"\"");
2600        } else {
2601            out.push(c);
2602        }
2603    }
2604    out.push('"');
2605    out
2606}
2607
2608fn is_keyword(s: &str) -> bool {
2609    matches!(
2610        &*s.to_ascii_lowercase(),
2611        "select"
2612            | "from"
2613            | "where"
2614            | "as"
2615            | "null"
2616            | "true"
2617            | "false"
2618            | "and"
2619            | "or"
2620            | "not"
2621            | "create"
2622            | "table"
2623            | "insert"
2624            | "into"
2625            | "values"
2626            | "index"
2627            | "on"
2628            | "begin"
2629            | "commit"
2630            | "rollback"
2631            | "is"
2632            | "between"
2633            | "in"
2634            | "like"
2635            | "group"
2636            | "distinct"
2637            | "union"
2638            | "all"
2639            | "join"
2640            | "inner"
2641            | "left"
2642            | "cross"
2643            | "outer"
2644            | "default"
2645            | "savepoint"
2646            | "release"
2647            | "to"
2648            | "having"
2649            | "show"
2650            | "extract"
2651            | "offset"
2652            | "asc"
2653            | "desc"
2654            | "interval"
2655    )
2656}
2657
2658#[cfg(test)]
2659mod tests {
2660    use super::*;
2661    use alloc::vec;
2662
2663    #[test]
2664    fn integer_literal_renders_without_dot() {
2665        assert_eq!(Literal::Integer(42).to_string(), "42");
2666    }
2667
2668    #[test]
2669    fn integral_float_keeps_dot() {
2670        assert_eq!(Literal::Float(1.0).to_string(), "1.0");
2671        assert_eq!(Literal::Float(1.5).to_string(), "1.5");
2672        assert_eq!(Literal::Float(2.5e-3).to_string(), "0.0025");
2673    }
2674
2675    #[test]
2676    fn string_literal_doubles_quote() {
2677        assert_eq!(Literal::String("it's".into()).to_string(), "'it''s'");
2678    }
2679
2680    #[test]
2681    fn bool_and_null_render_uppercase() {
2682        assert_eq!(Literal::Bool(true).to_string(), "TRUE");
2683        assert_eq!(Literal::Bool(false).to_string(), "FALSE");
2684        assert_eq!(Literal::Null.to_string(), "NULL");
2685    }
2686
2687    #[test]
2688    fn binary_op_always_parenthesised() {
2689        let e = Expr::Binary {
2690            lhs: Box::new(Expr::Literal(Literal::Integer(1))),
2691            op: BinOp::Add,
2692            rhs: Box::new(Expr::Literal(Literal::Integer(2))),
2693        };
2694        assert_eq!(e.to_string(), "(1 + 2)");
2695    }
2696
2697    #[test]
2698    fn select_star_from_table() {
2699        let s = SelectStatement {
2700            items: vec![SelectItem::Wildcard],
2701            from: Some(FromClause {
2702                primary: TableRef {
2703                    name: "users".into(),
2704                    alias: None,
2705                    as_of_segment: None,
2706                    unnest_expr: None,
2707                },
2708                joins: vec![],
2709            }),
2710            where_: None,
2711            group_by: None,
2712            group_by_all: false,
2713            having: None,
2714            unions: vec![],
2715            order_by: Vec::new(),
2716            limit: None,
2717            offset: None,
2718            distinct: false,
2719            ctes: vec![],
2720        };
2721        assert_eq!(s.to_string(), "SELECT * FROM users");
2722    }
2723
2724    #[test]
2725    fn quote_ident_for_uppercase_and_keyword() {
2726        assert_eq!(quote_ident("foo"), "foo");
2727        assert_eq!(quote_ident("Foo"), "\"Foo\"");
2728        assert_eq!(quote_ident("select"), "\"select\"");
2729        assert_eq!(quote_ident(""), "\"\"");
2730        assert_eq!(quote_ident("a\"b"), "\"a\"\"b\"");
2731    }
2732}