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