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