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    /// v7.14.0 — `DROP TABLE [IF EXISTS] name [, name…]
18    /// [CASCADE | RESTRICT]`. Engine removes the matching tables
19    /// (each one) from the catalog; IF EXISTS makes the drop
20    /// idempotent. CASCADE / RESTRICT trailers parsed silently
21    /// (SPG always cascades index drops on table drop).
22    DropTable {
23        names: Vec<String>,
24        if_exists: bool,
25    },
26    /// v7.14.0 — `DROP INDEX [IF EXISTS] name`. Removes the
27    /// matching index across whichever table holds it.
28    DropIndex {
29        name: String,
30        if_exists: bool,
31    },
32    /// v7.14.0 — empty / comment-only statement. The lexer strips
33    /// `--` line comments and `/* … */` block comments (including
34    /// the MySQL conditional `/*!NNNNN … */` form) before the
35    /// parser ever sees them; a SQL chunk that contains nothing
36    /// else lands here. Engine returns CommandOk no-op so
37    /// pg_dump / mysqldump preambles (`SET NAMES utf8mb4`
38    /// wrapped in conditional comments, etc.) load cleanly.
39    Empty,
40    Select(SelectStatement),
41    CreateTable(CreateTableStatement),
42    /// v7.9.15 — `CREATE EXTENSION [IF NOT EXISTS] <name>
43    /// [WITH SCHEMA <s>] [VERSION <v>] [CASCADE]` accepted as a
44    /// no-op so PG dumps that include extension declarations
45    /// (notably `pgvector`) load against SPG without splitting
46    /// init scripts. mailrs migration follow-up F3.
47    CreateExtension(String),
48    /// v7.9.27 → v7.16.2 — PG `DO $$ … $$ [LANGUAGE plpgsql];`
49    /// block. The body is now CAPTURED as a [`PlPgSqlBlock`] and
50    /// the engine executes it at top level (mailrs round-10
51    /// A.2). Pre-v7.16.2 the parser discarded the body and the
52    /// engine returned CommandOk — a SEV-1 silent no-op that
53    /// turned mailrs's `DO BEGIN IF EXISTS … THEN ALTER … END
54    /// $$` idempotent migrations into invisible no-ops.
55    DoBlock(PlPgSqlBlock),
56    CreateIndex(CreateIndexStatement),
57    Insert(InsertStatement),
58    /// v4.4 — `UPDATE <table> SET col=expr [, ...] [WHERE cond]`.
59    Update(UpdateStatement),
60    /// v4.4 — `DELETE FROM <table> [WHERE cond]`.
61    Delete(DeleteStatement),
62    /// v7.17.0 Phase 3.P0-42 — SQL:2003 / PG 15+ `MERGE` statement.
63    /// `MERGE INTO target [alias] USING source [alias] ON cond
64    /// WHEN MATCHED [AND cond] THEN { UPDATE SET … | DELETE | DO NOTHING }
65    /// WHEN NOT MATCHED [AND cond] THEN { INSERT (cols) VALUES (vals) | DO NOTHING }
66    /// [WHEN …]`. SPG v7.17 supports table-based source (subquery
67    /// source is a follow-up); BY SOURCE / BY TARGET and RETURNING
68    /// are also follow-ups.
69    Merge(MergeStatement),
70    Begin,
71    Commit,
72    Rollback,
73    /// `SAVEPOINT <name>` — push a named savepoint onto the active TX's
74    /// stack so a later `ROLLBACK TO <name>` can undo just the work
75    /// since this point.
76    Savepoint(String),
77    /// `ROLLBACK TO [SAVEPOINT] <name>` — restore catalog state to the
78    /// named savepoint and discard later savepoints. Does not end the
79    /// transaction.
80    RollbackToSavepoint(String),
81    /// `RELEASE [SAVEPOINT] <name>` — discard a savepoint without
82    /// rolling back. Keeps the work done since then.
83    ReleaseSavepoint(String),
84    /// `SHOW TABLES` — return the list of tables in the catalog.
85    ShowTables,
86    /// v7.17.0 Phase 3.P0-58 — MySQL `SHOW DATABASES` /
87    /// `SHOW SCHEMAS`. SPG is single-database; the executor
88    /// returns the canonical MySQL set so the mysql / MariaDB
89    /// client populates its database selector.
90    ShowDatabases,
91    /// v7.17.0 Phase 3.P0-59 — MySQL `SHOW CREATE TABLE <t>`
92    /// returns a 2-column row `(Table, "Create Table")` carrying
93    /// the synthesized DDL. mysqldump emits this for every
94    /// table at scrape time.
95    ShowCreateTable(String),
96    /// v7.17.0 Phase 3.P0-60 — MySQL `SHOW INDEXES FROM <t>`
97    /// (also `SHOW INDEX`, `SHOW KEYS`).
98    ShowIndexes(String),
99    /// v7.17.0 Phase 3.P0-61 — MySQL `SHOW STATUS`.
100    ShowStatus,
101    /// v7.17.0 Phase 3.P0-61 — MySQL `SHOW VARIABLES`.
102    ShowVariables,
103    /// v7.17.0 Phase 3.P0-62 — MySQL `SHOW PROCESSLIST`.
104    ShowProcesslist,
105    /// `SHOW COLUMNS FROM <table>` — return one row per column with
106    /// its declared name / type / nullability.
107    ShowColumns(String),
108    /// `CREATE USER 'name' WITH PASSWORD 'pw' ROLE 'admin'` (v4.1).
109    /// Role is optional; defaults to `readonly` when omitted.
110    CreateUser(CreateUserStatement),
111    /// `DROP USER 'name'` (v4.1).
112    DropUser(String),
113    /// `SHOW USERS` (v4.1) — admin-only listing of (name, role).
114    ShowUsers,
115    /// v4.26 — `EXPLAIN [ANALYZE] <select>`. The engine returns a
116    /// single-column text table describing the rewritten plan tree
117    /// for `inner`. `analyze` triggers an actual exec to attach
118    /// observed row counts and elapsed micros to each node.
119    Explain(ExplainStatement),
120    /// v6.0.4 — `ALTER INDEX <name> REBUILD [WITH (encoding = ...)]`.
121    /// Synchronous rebuild of an NSW index. With the optional
122    /// encoding clause, every stored cell at the indexed column is
123    /// also re-encoded through `coerce_value` before the new graph
124    /// builds.
125    AlterIndex(AlterIndexStatement),
126    /// v6.7.2 — `ALTER TABLE <name> SET <setting> = <value>`.
127    /// The only setting in v6.7.2 is `hot_tier_bytes`, which
128    /// overrides the global `SPG_HOT_TIER_BYTES` freezer trigger
129    /// for the named table.
130    AlterTable(AlterTableStatement),
131    /// v6.1.2 — `CREATE PUBLICATION <name> [FOR ALL TABLES]`.
132    /// The catalog row lives in `spg_publications`. Publisher-side
133    /// WAL filtering arrives in v6.1.5.
134    CreatePublication(CreatePublicationStatement),
135    /// v6.1.2 — `DROP PUBLICATION <name>`. PG-compatible silent
136    /// no-op when the publication does not exist.
137    DropPublication(String),
138    /// v6.1.3 — `SHOW PUBLICATIONS`. Returns one row per
139    /// publication ordered by name with `(name, scope_summary,
140    /// table_count)` columns. The scope summary is the human-
141    /// readable form `ALL TABLES` / `FOR TABLE …` / `FOR ALL
142    /// TABLES EXCEPT …`; `table_count` is `NULL` for the
143    /// `AllTables` scope and the table-list length otherwise.
144    ShowPublications,
145    /// v6.1.4 — `CREATE SUBSCRIPTION <name> CONNECTION '<conn>'
146    /// PUBLICATION <pub_name> [, <pub_name> …]`. Catalog lands
147    /// in `spg_subscriptions`; when the subscription is
148    /// `enabled = true` (default) the server spawns a
149    /// background worker that connects to `conn` and drains the
150    /// requested publication(s) into the local engine.
151    CreateSubscription(CreateSubscriptionStatement),
152    /// v6.1.4 — `DROP SUBSCRIPTION <name>`. Like DROP
153    /// PUBLICATION, silent no-op when absent. Stops the
154    /// associated worker thread before removing the row.
155    DropSubscription(String),
156    /// v6.1.4 — `SHOW SUBSCRIPTIONS`. Returns one row per
157    /// subscription ordered by name with `(name, conn_str,
158    /// publications, enabled, last_received_pos)`.
159    ShowSubscriptions,
160    /// v6.1.7 — `WAIT FOR WAL POSITION <pos> [WITH TIMEOUT <ms>]`.
161    /// Blocks until the local server's apply position reaches
162    /// `<pos>` or `<ms>` elapses. Server-layer command: the
163    /// engine refuses it (`EngineError::Unsupported`) since
164    /// `lag_state` lives in `spg-server`'s `ServerState`.
165    WaitForWalPosition {
166        pos: u64,
167        /// `None` → wait forever; `Some(ms)` → return after `ms`
168        /// milliseconds even if the target isn't reached.
169        timeout_ms: Option<u64>,
170    },
171    /// v6.2.0 — `ANALYZE [<table>]`. Bare form walks every user
172    /// table; `ANALYZE <name>` re-stats just one. Populates
173    /// `spg_statistic` with per-column null_frac + n_distinct +
174    /// 100-bucket equi-depth histogram.
175    Analyze(Option<String>),
176    /// v6.7.3 — `COMPACT COLD SEGMENTS`. Walks every user table's
177    /// BTree-cold indices and merges small cold-tier segments
178    /// (size below `SPG_COMPACTION_TARGET_SEGMENT_BYTES`, default
179    /// 4 MiB) into a single larger segment per (table, index).
180    /// `WHERE` predicate filtering on which tables to compact is
181    /// carved out of v6.7.3 (per V6_7_DESIGN.md STABILITY entry);
182    /// v6.7.3 only supports the bare form.
183    CompactColdSegments,
184    /// v7.12.1 — `SET <name> [TO|=] <value>`. Records a session
185    /// parameter on the engine; v7.12.1 honours
186    /// `default_text_search_config` (consumed by `to_tsvector` /
187    /// `plainto_tsquery` family when called without an explicit
188    /// config arg). All other names are accepted as a no-op so PG
189    /// dumps with `SET client_encoding`, `SET search_path` etc.
190    /// load cleanly.
191    SetParameter {
192        name: String,
193        value: SetValue,
194    },
195    /// v7.14.0 — `SET a = 1, b = 2, …` MySQL-flavoured
196    /// multi-assignment (mysqldump preamble uses
197    /// `SET @OLD_FOREIGN_KEY_CHECKS = @@FOREIGN_KEY_CHECKS,
198    /// FOREIGN_KEY_CHECKS=0`). Engine applies each pair in
199    /// source order. Pairs whose LHS is a MySQL session/user
200    /// variable (`@VAR` / `@@VAR`) are recorded with the raw
201    /// name so the engine can ignore them; pairs whose LHS is
202    /// a recognised engine parameter (e.g. `FOREIGN_KEY_CHECKS`)
203    /// go through the regular `set_session_param` path.
204    SetParameterList(Vec<(String, SetValue)>),
205    /// v7.12.1 — `RESET <name>` / `RESET ALL`. Restores parameter
206    /// to its default. No-op for parameters SPG does not track.
207    ResetParameter(Option<String>),
208    /// v7.12.4 — `CREATE [OR REPLACE] FUNCTION name(args) RETURNS
209    /// <type> [LANGUAGE <lang>] AS $$ body $$ [LANGUAGE <lang>]`.
210    /// v7.12.4 ships `plpgsql` for `RETURNS TRIGGER` bodies (the
211    /// CREATE TRIGGER + AFTER/BEFORE row-level pipeline). Other
212    /// languages parse but error at exec time with a clear
213    /// unsupported message.
214    CreateFunction(CreateFunctionStatement),
215    /// v7.12.4 — `CREATE [OR REPLACE] TRIGGER name {BEFORE|AFTER}
216    /// {INSERT|UPDATE|DELETE} [OR ...] ON tbl FOR EACH ROW
217    /// EXECUTE {FUNCTION|PROCEDURE} fn_name()`. STATEMENT-level
218    /// triggers and column-list / WHEN clauses are out of scope
219    /// for v7.12.4.
220    CreateTrigger(CreateTriggerStatement),
221    /// v7.12.4 — `DROP TRIGGER [IF EXISTS] name ON tbl`. Silent
222    /// no-op when missing if `IF EXISTS` is set.
223    DropTrigger {
224        name: String,
225        table: String,
226        if_exists: bool,
227    },
228    /// v7.12.4 — `DROP FUNCTION [IF EXISTS] name`. Same shape as
229    /// DROP TRIGGER but global (no table scope).
230    DropFunction {
231        name: String,
232        if_exists: bool,
233    },
234    /// v7.17.0 — `CREATE [TEMPORARY] SEQUENCE [IF NOT EXISTS] name
235    /// [AS data_type]
236    /// [INCREMENT [BY] n]
237    /// [MINVALUE n | NO MINVALUE]
238    /// [MAXVALUE n | NO MAXVALUE]
239    /// [START [WITH] n]
240    /// [CACHE n]
241    /// [[NO] CYCLE]
242    /// [OWNED BY {table.col | NONE}]`.
243    /// Closes the round-7+ silent-no-op SEQUENCE story so pg_dump
244    /// emits + nextval/currval/setval downstream all work.
245    CreateSequence(CreateSequenceStatement),
246    /// v7.17.0 — `ALTER SEQUENCE [IF EXISTS] name <options>` with
247    /// the same option grammar as CREATE SEQUENCE, plus
248    /// `RESTART [WITH n]` and `OWNED BY ...` re-attach.
249    AlterSequence(AlterSequenceStatement),
250    /// v7.17.0 — `DROP SEQUENCE [IF EXISTS] name [, name…]
251    /// [CASCADE | RESTRICT]`. CASCADE / RESTRICT trailers parsed
252    /// silently (no FK on sequences).
253    DropSequence {
254        names: Vec<String>,
255        if_exists: bool,
256    },
257    /// v7.17.0 Phase 1.2 — `CREATE [OR REPLACE] [TEMPORARY] VIEW
258    /// [IF NOT EXISTS] name [(col, …)] AS <SELECT …>`. Closes the
259    /// silent-no-op VIEW story from the v7.17 customer-readiness
260    /// audit: pre-v7.17 SPG parsed CREATE VIEW as Statement::Empty
261    /// so any downstream `SELECT FROM v` errored with table-not-
262    /// found. The view body is stored verbatim; SELECT FROM <v>
263    /// rewrites at exec-time by prepending the view body as a
264    /// synthetic CTE.
265    CreateView(CreateViewStatement),
266    /// v7.17.0 Phase 1.2 — `DROP VIEW [IF EXISTS] name [, name…]
267    /// [CASCADE | RESTRICT]`. Removes the matching view from the
268    /// catalog; CASCADE/RESTRICT parsed silently.
269    DropView {
270        names: Vec<String>,
271        if_exists: bool,
272    },
273    /// v7.17.0 Phase 1.3 — `CREATE MATERIALIZED VIEW [IF NOT
274    /// EXISTS] name [(col, …)] AS <SELECT …> [WITH [NO] DATA]`.
275    /// Closes the silent-no-op MATERIALIZED VIEW story. Storage
276    /// model: the materialised result lives as a regular table
277    /// with the matching name + a parallel
278    /// `materialized_views` registry mapping name → body source
279    /// (used by REFRESH).
280    CreateMaterializedView(CreateMaterializedViewStatement),
281    /// v7.17.0 Phase 1.3 — `REFRESH MATERIALIZED VIEW name [WITH
282    /// [NO] DATA]`. Re-runs the stored body and replaces the
283    /// cached rows. `WITH NO DATA` truncates without re-running.
284    RefreshMaterializedView {
285        name: String,
286        with_data: bool,
287    },
288    /// v7.17.0 Phase 1.3 — `DROP MATERIALIZED VIEW [IF EXISTS]
289    /// name [, name…] [CASCADE | RESTRICT]`. Drops both the
290    /// backing table and the source registry entry.
291    DropMaterializedView {
292        names: Vec<String>,
293        if_exists: bool,
294    },
295    /// v7.17.0 Phase 1.4 — `CREATE TYPE name AS ENUM ('a', 'b',
296    /// …)`. Closes the silent-no-op CREATE TYPE story so PG
297    /// dumps that declare enum types load with real constraints
298    /// instead of becoming free-form TEXT. Future kinds
299    /// (composite / range / domain) extend the inner `kind`
300    /// enum.
301    CreateType(CreateTypeStatement),
302    /// v7.17.0 Phase 1.4 — `DROP TYPE [IF EXISTS] name [, name…]
303    /// [CASCADE | RESTRICT]`. Removes the matching enum/domain
304    /// from the catalog.
305    DropType {
306        names: Vec<String>,
307        if_exists: bool,
308    },
309    /// v7.17.0 Phase 1.5 — `CREATE DOMAIN name AS base_type
310    /// [DEFAULT expr] [NOT NULL | NULL] [CHECK (expr)]*`.
311    /// A DOMAIN is a named CHECK-constrained alias over a built-
312    /// in type. The CHECK + NOT NULL + DEFAULT clauses apply to
313    /// every column declared with the domain. Closes the
314    /// silent-no-op CREATE DOMAIN story so PG dumps that ship
315    /// validated identifier types (email, positive_int, …) keep
316    /// their guarantees.
317    CreateDomain(CreateDomainStatement),
318    /// v7.17.0 Phase 1.5 — `DROP DOMAIN [IF EXISTS] name
319    /// [, name…] [CASCADE | RESTRICT]`. Removes the matching
320    /// domain from the catalog.
321    DropDomain {
322        names: Vec<String>,
323        if_exists: bool,
324    },
325    /// v7.17.0 Phase 1.6 — `CREATE SCHEMA [IF NOT EXISTS]
326    /// name [AUTHORIZATION user]`. SPG is single-database;
327    /// schemas are tracked as a namespace registry so pg_dump
328    /// multi-schema declarations land cleanly and `SELECT *
329    /// FROM information_schema.schemata` returns real entries.
330    /// Schema-qualified `schema.table` references still strip
331    /// the prefix at lookup time per PG (schemas are not
332    /// isolation boundaries in v7.17 — see project-next-docket
333    /// for the v7.18+ isolation tracking).
334    CreateSchema {
335        name: String,
336        if_not_exists: bool,
337    },
338    /// v7.17.0 Phase 1.6 — `DROP SCHEMA [IF EXISTS] name
339    /// [, name…] [CASCADE | RESTRICT]`. Removes the schema
340    /// from the registry; built-in `public` / `pg_catalog` /
341    /// `information_schema` cannot be dropped.
342    DropSchema {
343        names: Vec<String>,
344        if_exists: bool,
345    },
346}
347
348/// v7.17.0 Phase 1.5 — `CREATE DOMAIN` AST.
349#[derive(Debug, Clone, PartialEq)]
350pub struct CreateDomainStatement {
351    pub name: String,
352    /// Base type for the domain (one of the built-in
353    /// `ColumnTypeName` variants). User-defined enum / domain
354    /// bases are deferred to Phase 1.5b.
355    pub base_type: ColumnTypeName,
356    /// Optional `DEFAULT <expr>`. Resolved at engine-side
357    /// CREATE TABLE time when a column is bound to this domain.
358    pub default: Option<Expr>,
359    /// `NOT NULL` from the domain definition. Engine ORs this
360    /// with the column-level nullability so the strictest of the
361    /// two wins (i.e. the column is non-nullable if either side
362    /// says so).
363    pub not_null: bool,
364    /// Zero-or-more `CHECK (expr)` predicates. Each one is
365    /// enforced as part of the column's CHECK list at INSERT /
366    /// UPDATE time, with `VALUE` substituted for the column's
367    /// current cell value.
368    pub checks: Vec<Expr>,
369}
370
371/// v7.17.0 Phase 1.4 — `CREATE TYPE` AST.
372#[derive(Debug, Clone, PartialEq, Eq)]
373pub struct CreateTypeStatement {
374    pub name: String,
375    pub kind: TypeKind,
376}
377
378/// v7.17.0 Phase 1.4 — flavour of the new type. Only ENUM is
379/// implemented; the variant set is open so Phase 1.5 (DOMAIN)
380/// and later (COMPOSITE, RANGE) can land without an AST shape
381/// migration.
382#[derive(Debug, Clone, PartialEq, Eq)]
383pub enum TypeKind {
384    /// `AS ENUM ('a', 'b', …)`. Order is preserved (PG enum
385    /// labels are ordered).
386    Enum { labels: Vec<String> },
387}
388
389/// v7.12.1 — payload of a SET right-hand side. PG syntax accepts
390/// a string literal, an identifier (often a config name), an
391/// integer/float, or the bare `DEFAULT` keyword.
392#[derive(Debug, Clone, PartialEq)]
393pub enum SetValue {
394    String(String),
395    Ident(String),
396    Number(String),
397    Default,
398}
399
400/// v6.1.4 — `CREATE SUBSCRIPTION` AST node. v6.1.4 ships a
401/// single fixed-shape DDL; the WITH-clause options PG supports
402/// (`enabled`, `slot_name`, `streaming`, `binary`) are out of
403/// scope for v6.1.4 — `enabled` defaults to true and there are
404/// no other knobs to set in v6.1.x.
405#[derive(Debug, Clone, PartialEq, Eq)]
406pub struct CreateSubscriptionStatement {
407    pub name: String,
408    /// Connection string in PG keyword=value form (e.g.
409    /// `host=127.0.0.1 port=20002`). v6.1.4 only consumes the
410    /// `host` and `port` fields; the rest is reserved for
411    /// future v6.1.x options.
412    pub conn_str: String,
413    /// One or more publications on the remote side. Order is
414    /// preserved verbatim from the DDL; the worker requests them
415    /// in this order. v6.1.4 records the list; v6.1.5
416    /// publisher-side filtering enforces it.
417    pub publications: Vec<String>,
418}
419
420/// v7.17.0 — `CREATE SEQUENCE` AST node. See [`Statement::CreateSequence`].
421#[derive(Debug, Clone, PartialEq, Eq)]
422pub struct CreateSequenceStatement {
423    pub name: String,
424    pub if_not_exists: bool,
425    pub temporary: bool,
426    /// Optional `AS data_type`. Default in PG is BIGINT; SPG matches.
427    pub data_type: Option<SequenceDataType>,
428    pub options: SequenceOptions,
429}
430
431/// v7.17.0 — narrow type for `AS` clause of CREATE SEQUENCE.
432#[derive(Debug, Clone, Copy, PartialEq, Eq)]
433pub enum SequenceDataType {
434    SmallInt,
435    Int,
436    BigInt,
437}
438
439/// v7.17.0 — option grammar shared by CREATE / ALTER SEQUENCE.
440/// All fields are optional. `min_value`/`max_value` carry
441/// `Some(SeqBound::NoBound)` for `NO MINVALUE` / `NO MAXVALUE`.
442#[derive(Debug, Clone, Default, PartialEq, Eq)]
443pub struct SequenceOptions {
444    pub increment: Option<i64>,
445    pub min_value: Option<SeqBound>,
446    pub max_value: Option<SeqBound>,
447    pub start: Option<i64>,
448    /// `RESTART [WITH n]` — ALTER-only. `Some(None)` = bare
449    /// RESTART, `Some(Some(n))` = RESTART WITH n.
450    pub restart: Option<Option<i64>>,
451    pub cache: Option<i64>,
452    pub cycle: Option<bool>,
453    pub owned_by: Option<SequenceOwnedBy>,
454}
455
456/// v7.17.0 — `MINVALUE n` / `NO MINVALUE`.
457#[derive(Debug, Clone, Copy, PartialEq, Eq)]
458pub enum SeqBound {
459    Value(i64),
460    NoBound,
461}
462
463/// v7.17.0 — `OWNED BY {table.col | NONE}`.
464#[derive(Debug, Clone, PartialEq, Eq)]
465pub enum SequenceOwnedBy {
466    None,
467    Column { table: String, column: String },
468}
469
470/// v7.17.0 Phase 1.3 — `CREATE MATERIALIZED VIEW` AST node.
471#[derive(Debug, Clone, PartialEq)]
472pub struct CreateMaterializedViewStatement {
473    pub name: String,
474    pub if_not_exists: bool,
475    /// Optional `(col, col, …)` rename list. Applies to the
476    /// backing table at CREATE / REFRESH time.
477    pub columns: Vec<String>,
478    /// Underlying SELECT. Re-parsed at REFRESH time to rebuild
479    /// the cached rows.
480    pub body: SelectStatement,
481    /// `WITH DATA` (default) = materialise the rows at CREATE
482    /// time. `WITH NO DATA` = create an empty backing table;
483    /// callers must REFRESH before SELECT returns rows.
484    pub with_data: bool,
485}
486
487/// v7.17.0 Phase 1.2 — `CREATE VIEW` AST node.
488#[derive(Debug, Clone, PartialEq)]
489pub struct CreateViewStatement {
490    pub name: String,
491    pub or_replace: bool,
492    pub if_not_exists: bool,
493    pub temporary: bool,
494    /// Optional `(col, col, …)` rename list. When non-empty,
495    /// these override the body's projected column names per-
496    /// position at SELECT-from-view time.
497    pub columns: Vec<String>,
498    /// Underlying SELECT. Re-parsed lazily at SELECT-from-view
499    /// time to materialise the view as a synthetic CTE.
500    pub body: SelectStatement,
501}
502
503/// v7.17.0 — `ALTER SEQUENCE` AST node.
504#[derive(Debug, Clone, PartialEq, Eq)]
505pub struct AlterSequenceStatement {
506    pub name: String,
507    pub if_exists: bool,
508    pub options: SequenceOptions,
509}
510
511/// v6.1.2 — `CREATE PUBLICATION` AST node. The `scope` field uses
512/// the [`PublicationScope`] shape. v6.1.2 only accepted
513/// `AllTables`; v6.1.3 unlocks the `ForTables` / `AllTablesExcept`
514/// variants by flipping the parser gate (no AST migration).
515#[derive(Debug, Clone, PartialEq, Eq)]
516pub struct CreatePublicationStatement {
517    pub name: String,
518    pub scope: PublicationScope,
519}
520
521/// v6.1.2 — Which tables a publication covers. v6.1.3 (this commit)
522/// flips the parser gate for the `ForTables` / `AllTablesExcept`
523/// variants — the on-disk shape, snapshot serialisation, and the
524/// AST round-trip Display path were already in place in v6.1.2
525/// so this is a parser-only widening.
526#[derive(Debug, Clone, PartialEq, Eq)]
527pub enum PublicationScope {
528    AllTables,
529    ForTables(Vec<String>),
530    AllTablesExcept(Vec<String>),
531}
532
533#[derive(Debug, Clone, PartialEq, Eq)]
534pub struct AlterIndexStatement {
535    pub name: String,
536    pub target: AlterIndexTarget,
537}
538
539#[derive(Debug, Clone, PartialEq, Eq)]
540pub enum AlterIndexTarget {
541    /// `REBUILD [WITH (encoding = <enc>)]`. `encoding = None`
542    /// rebuilds the existing graph in place without touching the
543    /// column encoding; `Some(enc)` re-encodes every cell first.
544    Rebuild { encoding: Option<VecEncoding> },
545    /// v7.16.2 — `[IF EXISTS] RENAME TO <new>`. mailrs migrate-042
546    /// uses this; PG drops the IF EXISTS noisily as ERROR, mailrs
547    /// uses it to make the migration idempotent (re-running on a
548    /// DB where the rename already happened is a no-op rather
549    /// than an error).
550    Rename { new: String, if_exists: bool },
551}
552
553/// v6.7.2 — `ALTER TABLE t SET <setting> = <value>`. v6.7.2 ships
554/// the single `hot_tier_bytes` setting; later v6.7.x sub-versions
555/// can add more SET subjects without changing the dispatch shape.
556#[derive(Debug, Clone, PartialEq)]
557pub struct AlterTableStatement {
558    pub name: String,
559    /// v7.13.2 — mailrs round-6 S1. One or more subactions
560    /// separated by commas in the source SQL. PG-semantic apply
561    /// is sequential; engine bails on first error (no
562    /// transactional rollback of completed subactions in v7.13).
563    /// Single-subaction shape stays a 1-element vec.
564    pub targets: Vec<AlterTableTarget>,
565}
566
567#[derive(Debug, Clone, PartialEq)]
568#[allow(clippy::large_enum_variant)]
569pub enum AlterTableTarget {
570    /// Per-table hot-tier byte budget override. The freezer
571    /// reads this before falling back to `SPG_HOT_TIER_BYTES`.
572    SetHotTierBytes(u64),
573    /// v7.6.8 — `ALTER TABLE t ADD CONSTRAINT name FOREIGN KEY
574    /// (cols) REFERENCES parent[(pcols)] [ON DELETE/UPDATE …]`.
575    /// Engine validates existing rows against the new constraint
576    /// before installing it.
577    AddForeignKey(ForeignKeyConstraint),
578    /// v7.6.8 — `ALTER TABLE t DROP CONSTRAINT [IF EXISTS] name`.
579    /// `if_exists` (v7.13.2 mailrs round-6 S7) makes the drop a
580    /// no-op when no FK with that name exists; otherwise raises.
581    DropForeignKey { name: String, if_exists: bool },
582    /// v7.13.0 — `ALTER TABLE t ADD [COLUMN] [IF NOT EXISTS] <col>
583    /// <type> [DEFAULT <expr>] [NOT NULL]`. mailrs round-5 G1
584    /// (20 migrate-*.sql hits). Engine appends the column to the
585    /// schema and back-fills every existing row with the DEFAULT
586    /// (or NULL when no DEFAULT and the column is nullable).
587    AddColumn {
588        column: ColumnDef,
589        if_not_exists: bool,
590    },
591    /// v7.13.0 — `ALTER TABLE t ALTER COLUMN <col> TYPE <ty>
592    /// [USING <expr>]` (mailrs round-5 G8). Engine rewrites every
593    /// existing row's column value by evaluating the optional
594    /// USING expression (default `col::<ty>`) and re-coercing
595    /// against the new column type.
596    AlterColumnType {
597        column: String,
598        new_type: ColumnTypeName,
599        using: Option<Expr>,
600    },
601    /// v7.13.3 — `ALTER TABLE t DROP [COLUMN] [IF EXISTS] <col>
602    /// [CASCADE | RESTRICT]` (mailrs round-7 S8). The column +
603    /// every row's value at that position is removed; any index
604    /// on the column is dropped. `if_exists` makes the drop a
605    /// no-op when the column is missing. `cascade` removes
606    /// dependents (FKs referencing the column, partial indexes
607    /// whose predicate names the column); without it, the engine
608    /// rejects when dependents exist.
609    DropColumn {
610        column: String,
611        if_exists: bool,
612        cascade: bool,
613    },
614    /// v7.14.0 — `ALTER TABLE t ADD CONSTRAINT name PRIMARY KEY
615    /// (cols)` / `ADD CONSTRAINT name UNIQUE (cols)` / `ADD
616    /// CONSTRAINT name CHECK (expr)` — table-level constraints
617    /// installed post-CREATE-TABLE. pg_dump emits PKs as a
618    /// separate ALTER TABLE statement, so this surface lets the
619    /// dump load straight through.
620    AddTableConstraint(TableConstraint),
621    /// v7.15.0 — `ALTER TABLE t RENAME [COLUMN] old TO new`.
622    /// Renames the column in the schema and propagates the rename
623    /// to every stored source string that references it as a
624    /// (potentially-qualified) column identifier: CHECK predicates,
625    /// partial-index predicates, runtime DEFAULT expressions, and
626    /// triggers' `UPDATE OF` column lists. Function bodies and
627    /// trigger bodies are NOT auto-rewritten — they're loose
628    /// source text and may contain references SPG can't statically
629    /// resolve to this column (NEW./OLD. + dynamic SQL). Renames
630    /// the column even if dependents exist; users renaming a
631    /// column referenced by a function body update the function
632    /// body separately.
633    RenameColumn { old: String, new: String },
634    /// v7.22 (round-13 T2) — mark a column auto-incrementing.
635    /// pg_dump splits SERIAL/IDENTITY columns into a plain integer
636    /// column plus either `ALTER COLUMN c SET DEFAULT nextval(…)`
637    /// (serial) or `ALTER COLUMN c ADD GENERATED … AS IDENTITY (…)`
638    /// (identity); both lower to this. SPG's auto-increment is
639    /// max+1-scan based, so the dump's `setval(…)` calls stay
640    /// no-ops without losing the sequence position.
641    SetColumnAutoIncrement {
642        column: String,
643        /// The implicit sequence pg_dump names for an identity
644        /// column (`ADD GENERATED … ( SEQUENCE NAME s … )`) or the
645        /// nextval target for a serial default. The engine creates
646        /// it if absent so the dump's later `setval(s, …)` lands.
647        seq_name: Option<String>,
648    },
649    /// v7.16.2 — `ALTER TABLE old RENAME TO new`. Renames the
650    /// table itself (mailrs round-10 A.5 carve-out — mailrs's
651    /// migrate-042 uses it). The engine moves the table entry
652    /// in the catalog under the new name; child catalog state
653    /// (FKs pointing at this table, triggers watching this
654    /// table) tracks the rename through the storage layer.
655    RenameTable { new: String },
656    /// v7.16.1 — `ALTER TABLE t { ENABLE | DISABLE } TRIGGER
657    /// { ALL | <name> }`. Toggles whether row-level triggers
658    /// fire on subsequent INSERT/UPDATE/DELETE on the table.
659    /// `pg_dump --disable-triggers` emits a DISABLE wrapper +
660    /// ENABLE epilogue around every table's data block so the
661    /// rows already-computed in prod don't get re-rewritten
662    /// (and so trigger-driven side effects like
663    /// audit/queueing don't re-fire during a bulk reload).
664    /// `which == TriggerSelector::All` toggles every trigger
665    /// on the table; `Named(name)` toggles one trigger. The
666    /// engine persists the disabled state on `TriggerDef.enabled`
667    /// (catalog FILE_VERSION 25+) and the row-write paths skip
668    /// the trigger when `!enabled`.
669    SetTriggerEnabled {
670        which: TriggerSelector,
671        enabled: bool,
672    },
673}
674
675/// v7.16.1 — target of `ALTER TABLE … { ENABLE | DISABLE }
676/// TRIGGER …`. PG also accepts `USER`, `REPLICA`, `ALWAYS`
677/// modifiers; v7.16.1 ships the two shapes pg_dump actually
678/// emits (`ALL` + per-name) — the rest parse-accept as `Named`
679/// shouldn't surface from a dump.
680#[derive(Debug, Clone, PartialEq, Eq)]
681pub enum TriggerSelector {
682    /// Every trigger on the table.
683    All,
684    /// A specific trigger by name.
685    Named(String),
686}
687
688#[derive(Debug, Clone, PartialEq)]
689pub struct ExplainStatement {
690    pub analyze: bool,
691    pub inner: Box<SelectStatement>,
692    /// v6.8.3 — `EXPLAIN (SUGGEST) <SELECT>` enables the index
693    /// advisor pass: after the regular plan tree, the engine
694    /// emits one suggestion line per column referenced in the
695    /// query's WHERE / JOIN that has no covering index on the
696    /// owning table.
697    pub suggest: bool,
698}
699
700#[derive(Debug, Clone, PartialEq, Eq)]
701pub struct CreateUserStatement {
702    pub name: String,
703    pub password: String,
704    /// One of `admin` / `readwrite` / `readonly`. Stored verbatim from
705    /// the parser; the engine validates against `Role::parse` so a
706    /// typo lands as a runtime error with a clear message rather than
707    /// a parse failure.
708    pub role: String,
709}
710
711/// v7.12.4 — `CREATE [OR REPLACE] FUNCTION`. v7.12.4 ships
712/// `RETURNS TRIGGER LANGUAGE plpgsql` as the primary use case
713/// (the row-level trigger body the CREATE TRIGGER below references).
714/// Non-trigger user-defined functions parse but error at execution
715/// time with a clear unsupported message; that surface lands in
716/// v7.12.5+.
717#[derive(Debug, Clone, PartialEq)]
718pub struct CreateFunctionStatement {
719    pub name: String,
720    /// `OR REPLACE` was present; an existing function with the
721    /// same name is overwritten instead of erroring.
722    pub or_replace: bool,
723    /// `(arg1 type1, ...)` — v7.12.4 only accepts the empty arg
724    /// list `()` (sufficient for trigger functions). Other shapes
725    /// parse and store the args but the executor refuses to call
726    /// them.
727    pub args: Vec<FunctionArg>,
728    /// `RETURNS <type>` — `trigger` is the supported shape for
729    /// v7.12.4; arbitrary return types parse to
730    /// [`FunctionReturn::Other`].
731    pub returns: FunctionReturn,
732    /// `LANGUAGE <lang>` clause. PG accepts the clause on either
733    /// side of `AS $$...$$`; the parser canonicalises to one slot.
734    /// `plpgsql` and `sql` are the two interesting values.
735    pub language: String,
736    /// `AS $$ ... $$` body. v7.12.4 parses PL/pgSQL bodies into
737    /// a structured AST; non-trigger / non-plpgsql bodies stay as
738    /// the raw source text so the v7.12.5+ executor can pick them
739    /// up without a parser rev.
740    pub body: FunctionBody,
741}
742
743/// v7.12.4 — one positional argument to a `CREATE FUNCTION`.
744#[derive(Debug, Clone, PartialEq)]
745pub struct FunctionArg {
746    /// `IN` / `OUT` / `INOUT` mode. v7.12.4 only accepts `IN`
747    /// (the default); `OUT` / `INOUT` parse but the executor
748    /// refuses them.
749    pub mode: FunctionArgMode,
750    /// Optional arg name. Trigger functions traditionally don't
751    /// name their args (they read NEW/OLD instead), so `None` is
752    /// the common case.
753    pub name: Option<String>,
754    /// Declared type, normalised to the SPG `DataType` mapping
755    /// where one exists. Unknown / extension types parse as a
756    /// raw string under [`FunctionArgType::Raw`].
757    pub ty: FunctionArgType,
758}
759
760#[derive(Debug, Clone, Copy, PartialEq, Eq)]
761pub enum FunctionArgMode {
762    In,
763    Out,
764    InOut,
765}
766
767#[derive(Debug, Clone, PartialEq)]
768pub enum FunctionArgType {
769    Typed(ColumnTypeName),
770    /// Unknown / extension types — kept as the parser-side raw
771    /// identifier so error messages can name them precisely.
772    Raw(String),
773}
774
775#[derive(Debug, Clone, PartialEq)]
776pub enum FunctionReturn {
777    /// `RETURNS TRIGGER` — the row-level trigger function shape.
778    /// v7.12.4 ships exactly this for execution.
779    Trigger,
780    /// `RETURNS VOID`. Parses; executor rejects in v7.12.4 unless
781    /// the function is unused (since v7.12.4 doesn't ship scalar
782    /// function invocation).
783    Void,
784    /// `RETURNS <type>` for any concrete data type. Reserved for
785    /// v7.12.5+'s scalar UDF surface.
786    Type(ColumnTypeName),
787    /// `RETURNS <ident>` for types SPG doesn't know — extension
788    /// types, RETURNS SETOF rows, RETURNS TABLE(...), etc.
789    Other(String),
790}
791
792#[derive(Debug, Clone, PartialEq)]
793pub enum FunctionBody {
794    /// v7.12.4 — parsed PL/pgSQL `BEGIN … END` block. The
795    /// trigger-function executor walks this directly without
796    /// re-parsing.
797    PlPgSql(PlPgSqlBlock),
798    /// Raw source text — parser couldn't (or didn't try to)
799    /// structure-parse the body. Used for `LANGUAGE sql`
800    /// functions and any PL/pgSQL body that contains v7.12.5+
801    /// features the v7.12.4 parser doesn't yet recognise. The
802    /// executor returns an unsupported error when invoked.
803    Raw(String),
804}
805
806/// v7.12.4 — PL/pgSQL `BEGIN ... END;` block. v7.12.6 widens
807/// from assignment + return to a real-PL/pgSQL surface:
808/// `DECLARE`-block local variables, `IF/ELSIF/ELSE/END IF`
809/// control flow, `RAISE` diagnostics, and embedded SQL
810/// statements that execute through the regular engine path.
811/// The remaining v7.12.x carve-out is loops (`LOOP/WHILE/FOR`),
812/// which mailrs's trigger doesn't need but other PG customers
813/// may; deferred to a future minor release.
814#[derive(Debug, Clone, PartialEq)]
815pub struct PlPgSqlBlock {
816    /// v7.12.6 — `DECLARE var TYPE [:= init_expr];` declarations
817    /// preceding `BEGIN`. Empty when the body opens directly with
818    /// `BEGIN`. Declarations execute in order; each may reference
819    /// earlier-declared locals in its init expression.
820    pub declarations: Vec<PlPgSqlDeclare>,
821    pub statements: Vec<PlPgSqlStmt>,
822}
823
824/// v7.12.6 — single `DECLARE` entry: variable name + declared
825/// type + optional initialiser. Variables default to SQL NULL
826/// when no init is given (matches PG).
827#[derive(Debug, Clone, PartialEq)]
828pub struct PlPgSqlDeclare {
829    pub name: String,
830    /// Declared SQL type (mapped to [`ColumnTypeName`] where SPG
831    /// knows it; raw text otherwise).
832    pub ty: FunctionArgType,
833    pub default: Option<Expr>,
834}
835
836#[derive(Debug, Clone, PartialEq)]
837pub enum PlPgSqlStmt {
838    /// `NEW.col := expr;` or `OLD.col := expr;`. OLD is parsed
839    /// for clarity in error reporting (PG also forbids it) — the
840    /// executor errors with a clear "OLD is read-only" message.
841    Assign { target: AssignTarget, value: Expr },
842    /// v7.16.2 — plpgsql `SELECT <projection> INTO <var>
843    /// [FROM …]` (mailrs round-10 migrate-042). The `body` is
844    /// the SELECT statement with the INTO clause stripped; the
845    /// engine runs it via `Engine::execute`, takes the first
846    /// row's first column, and assigns to the local variable
847    /// in the DECLARE scope. Single-column / single-row
848    /// queries only at v7.16.2; multi-target (`INTO a, b`) is
849    /// a v7.16.x follow-up.
850    SelectInto {
851        var: String,
852        body: Box<SelectStatement>,
853    },
854    /// `RETURN <target>;` — trigger functions canonically return
855    /// `NEW` / `OLD` / `NULL`; v7.12.4 also accepts a bare
856    /// expression for forward compatibility with scalar UDFs.
857    Return(ReturnTarget),
858    /// v7.12.6 — `IF cond THEN body [ELSIF cond THEN body]*
859    /// [ELSE body] END IF;`. Branches are tried in order; first
860    /// truthy condition wins; the optional ELSE runs when no
861    /// condition matched.
862    If {
863        branches: Vec<(Expr, Vec<PlPgSqlStmt>)>,
864        else_branch: Vec<PlPgSqlStmt>,
865    },
866    /// v7.12.6 — `RAISE <level> '<fmt>' [, args]*;`. Level is one
867    /// of `NOTICE` / `WARNING` / `INFO` / `LOG` / `DEBUG`
868    /// (logging — observable side effect only) or `EXCEPTION`
869    /// (aborts the trigger and propagates as an error). v7.12.6
870    /// supports the basic format-string substitution PG uses
871    /// (`%` placeholders consumed positionally).
872    Raise {
873        level: RaiseLevel,
874        message: String,
875        args: Vec<Expr>,
876    },
877    /// v7.12.6 — embedded SQL statement inside the trigger body
878    /// (`INSERT INTO …`, `UPDATE …`, `DELETE FROM …`, `SELECT …`).
879    /// NEW.col / OLD.col references inside the embedded
880    /// statement's expression tree are substituted with the
881    /// current trigger context before the engine re-executes the
882    /// statement. Recursion depth into nested triggers is
883    /// bounded by the engine's existing trigger-fire guard.
884    EmbeddedSql(Box<Statement>),
885}
886
887#[derive(Debug, Clone, Copy, PartialEq, Eq)]
888pub enum RaiseLevel {
889    /// `RAISE NOTICE` — diagnostic message, observable in the
890    /// server log. Does not affect the trigger's outcome.
891    Notice,
892    /// `RAISE WARNING` — like NOTICE, slightly louder severity.
893    Warning,
894    /// `RAISE INFO` — like NOTICE, slightly quieter.
895    Info,
896    /// `RAISE LOG` — like NOTICE, lower priority.
897    Log,
898    /// `RAISE DEBUG` — like NOTICE, lowest priority.
899    Debug,
900    /// `RAISE EXCEPTION` — aborts the trigger function with the
901    /// given message, propagating up to the caller as a query-
902    /// level error.
903    Exception,
904}
905
906#[derive(Debug, Clone, PartialEq)]
907pub enum AssignTarget {
908    NewColumn(String),
909    OldColumn(String),
910    /// Reserved for v7.12.5 DECLARE'd local variables.
911    Local(String),
912}
913
914#[derive(Debug, Clone, PartialEq)]
915pub enum ReturnTarget {
916    /// `RETURN NEW;` — for BEFORE triggers, this is the row that
917    /// actually gets written (possibly with NEW.col mutations
918    /// applied). For AFTER triggers, the return value is ignored.
919    New,
920    /// `RETURN OLD;` — pass-through. For BEFORE DELETE this lets
921    /// the delete proceed; for BEFORE UPDATE / INSERT it's
922    /// equivalent to dropping the write.
923    Old,
924    /// `RETURN NULL;` — for BEFORE triggers, skips the write
925    /// entirely. For AFTER, the return value is ignored.
926    Null,
927    /// `RETURN <expr>;` — non-row return shape; reserved for the
928    /// scalar UDF surface in v7.12.5+. Executor errors when used
929    /// inside a trigger function.
930    Expr(Expr),
931}
932
933/// v7.12.4 — `CREATE [OR REPLACE] TRIGGER`. Always row-level
934/// (`FOR EACH ROW`) in v7.12.4 — statement-level triggers parse
935/// but the executor refuses them. `WHEN (cond)` clauses are out
936/// of scope; the trigger function can short-circuit on a leading
937/// IF inside its body once v7.12.5 lands IF.
938#[derive(Debug, Clone, PartialEq)]
939pub struct CreateTriggerStatement {
940    pub name: String,
941    pub or_replace: bool,
942    pub timing: TriggerTiming,
943    /// At least one event; `INSERT OR UPDATE OR DELETE` parses to
944    /// three entries in order.
945    pub events: Vec<TriggerEvent>,
946    pub table: String,
947    /// `FOR EACH ROW` vs `FOR EACH STATEMENT`. v7.12.4 ships
948    /// only `Row`; `Statement` parses but the executor refuses.
949    pub for_each: TriggerForEach,
950    /// Name of the function to invoke. v7.12.4 requires the
951    /// function to be `CREATE FUNCTION`'d earlier; forward
952    /// references (PG accepts) are deferred to v7.12.5.
953    pub function: String,
954    /// v7.13.0 — `UPDATE OF col, col, …` column-list filter
955    /// (mailrs round-5 G7). Non-empty only when the events list
956    /// contains UPDATE and the user wrote the column-list filter.
957    /// PG fires the trigger only when at least one of these
958    /// columns appears in the SET clause; SPG conservatively
959    /// fires on any UPDATE matching the listed columns or
960    /// rewriting them at the row level. Empty vec = no filter
961    /// (fire on every UPDATE).
962    pub update_columns: Vec<String>,
963}
964
965#[derive(Debug, Clone, Copy, PartialEq, Eq)]
966pub enum TriggerTiming {
967    /// Fires before the row is written; the trigger function's
968    /// return value (NEW or NULL) decides the row content and
969    /// whether the write proceeds at all.
970    Before,
971    /// Fires after the row is written; the return value is
972    /// ignored.
973    After,
974    /// `INSTEAD OF` is PG-VIEW-trigger-only and out of scope for
975    /// v7.12.4 (SPG has no updatable-view surface).
976    InsteadOf,
977}
978
979#[derive(Debug, Clone, Copy, PartialEq, Eq)]
980pub enum TriggerEvent {
981    Insert,
982    Update,
983    Delete,
984    /// `TRUNCATE` event parses; SPG has no TRUNCATE statement
985    /// so the trigger never fires.
986    Truncate,
987}
988
989#[derive(Debug, Clone, Copy, PartialEq, Eq)]
990pub enum TriggerForEach {
991    Row,
992    Statement,
993}
994
995#[derive(Debug, Clone, PartialEq)]
996pub struct CreateIndexStatement {
997    pub name: String,
998    pub table: String,
999    pub column: String,
1000    /// Optional `USING <method>` clause. v2.0 recognises `hnsw` (NSW
1001    /// graph for vector kNN); unspecified is the default B-tree index.
1002    pub method: IndexMethod,
1003    /// `IF NOT EXISTS` — engine returns `CommandOk` no-op when the
1004    /// index name already exists, instead of raising `DuplicateIndex`.
1005    pub if_not_exists: bool,
1006    /// v6.8.0 — `INCLUDE (col1, col2, …)` columns. Identifies the
1007    /// non-key columns the planner should treat as "covered" by
1008    /// this index when checking whether a query can run as an
1009    /// index-only scan. Empty when no `INCLUDE` clause was given.
1010    pub included_columns: Vec<String>,
1011    /// v6.8.1 — `WHERE <expr>` partial-index predicate. Only rows
1012    /// for which `<expr>` evaluates truthy enter the index;
1013    /// queries whose `WHERE` clause's canonical Display form
1014    /// matches this expression's Display form can be served by the
1015    /// partial index. Stored as a parsed `Expr` so the engine
1016    /// re-uses the existing evaluation path; storage persists the
1017    /// Display form on the catalog snapshot.
1018    pub partial_predicate: Option<Expr>,
1019    /// v6.8.2 — expression-based index. When `Some(expr)`, the
1020    /// index key is the result of `expr` evaluated on each row
1021    /// (e.g. `CREATE INDEX … (lower(name))`). The `column`
1022    /// field still names the *primary* column the expression
1023    /// touches so existing planner shortcuts that resolve a
1024    /// column position stay valid. `None` = plain
1025    /// column-reference index (the legacy shape).
1026    pub expression: Option<Expr>,
1027    /// v7.9.14 — extra column names after the leading column in a
1028    /// multi-column `CREATE INDEX … (a, b, c)`. mailrs F2. The
1029    /// planner today still only uses the leading column for index
1030    /// seeks; the extras are tracked verbatim so the same DDL
1031    /// round-trips through WAL replay + catalog snapshot, and so
1032    /// the engine can emit a clear warning at INDEX CREATE time
1033    /// that only the leading column is currently honoured.
1034    /// Composite BTree index keys land in v7.10.
1035    pub extra_columns: Vec<String>,
1036    /// v7.9.29 — `CREATE UNIQUE INDEX …`. When true the engine
1037    /// enforces uniqueness on the indexed key (combined with the
1038    /// `partial_predicate` filter — only rows where the predicate
1039    /// evaluates truthy enter the uniqueness check). Standard SQL
1040    /// and PG's canonical way to express conditional uniqueness.
1041    /// mailrs K1.
1042    pub is_unique: bool,
1043    /// v7.15.0 — operator class on the leading column, when the
1044    /// CREATE INDEX named one (`(col vector_cosine_ops)` shape).
1045    /// Lower-cased. Most opclasses are still informational; the
1046    /// engine routes on `gin_trgm_ops` specifically to build a
1047    /// trigram-shingle GIN over a TEXT column, and otherwise
1048    /// keeps the current "accepted and discarded" behaviour for
1049    /// pg_dump compatibility.
1050    pub opclass: Option<String>,
1051}
1052
1053#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1054pub enum IndexMethod {
1055    /// Default — B-tree over `IndexKey`. Used for equality / range
1056    /// lookups on scalar columns.
1057    BTree,
1058    /// `USING hnsw` — NSW graph for kNN over a vector column.
1059    Hnsw,
1060    /// v6.7.1 — `USING brin` — Block Range INdex. Per-segment
1061    /// metadata that records (min_key, max_key) for each page in a
1062    /// cold-tier segment, on the indexed column. The optimizer
1063    /// can use these summaries to skip pages whose range does NOT
1064    /// overlap a query's WHERE predicate. BRIN indexes carry no
1065    /// in-memory data — the summaries live in the segment v2
1066    /// envelope's sidecar. Created via the standard
1067    /// `CREATE INDEX … USING brin (col)` syntax.
1068    Brin,
1069    /// v7.12.3 — `USING gin` — inverted index over a `tsvector`
1070    /// column. Posting lists map `lexeme word` → row locators; the
1071    /// planner uses them to narrow `WHERE col @@ tsquery` to the
1072    /// candidate rows whose vectors contain a matching term, then
1073    /// re-evaluates the full `@@` semantics on each candidate.
1074    /// Replaces the v7.9.26b `USING gin` → BTree fallback that
1075    /// silently degraded to a full scan at query time.
1076    Gin,
1077}
1078
1079#[derive(Debug, Clone, PartialEq)]
1080pub struct CreateTableStatement {
1081    pub name: String,
1082    pub columns: Vec<ColumnDef>,
1083    /// `IF NOT EXISTS` — engine returns `CommandOk` no-op when the
1084    /// table name already exists, instead of raising `DuplicateTable`.
1085    pub if_not_exists: bool,
1086    /// v7.6.0 — table-level `FOREIGN KEY (...) REFERENCES ...`
1087    /// constraints. Column-level `REFERENCES` (single-column inline
1088    /// form) is normalised into this vec at parse time so the engine
1089    /// sees one uniform list.
1090    pub foreign_keys: Vec<ForeignKeyConstraint>,
1091    /// v7.9.18 — table-level constraints: `PRIMARY KEY (a, b)` and
1092    /// `UNIQUE (a, b, ...)`. mailrs migration follow-up G1 + G6.
1093    /// Engine resolves each into a BTree index named after the
1094    /// constraint's leading column at CREATE TABLE time; INSERT
1095    /// path enforces composite uniqueness via row scan on the
1096    /// leading column index.
1097    pub table_constraints: Vec<TableConstraint>,
1098}
1099
1100/// v7.9.18 — table-level constraint at the end of a CREATE TABLE
1101/// column list. Either a composite PRIMARY KEY or a UNIQUE
1102/// (single- or multi-column).
1103#[derive(Debug, Clone, PartialEq)]
1104pub enum TableConstraint {
1105    /// `PRIMARY KEY (col1, col2, ...)`. Implies NOT NULL on each
1106    /// referenced column. Engine builds a BTree index named
1107    /// `<table>_pkey` and enforces composite uniqueness on INSERT.
1108    PrimaryKey {
1109        name: Option<String>,
1110        columns: Vec<String>,
1111    },
1112    /// `UNIQUE (col1, col2, ...)`. Engine builds a BTree index
1113    /// named `<table>_<leading_col>_key` (single-column) or
1114    /// `<table>_<leading_col>_<…>_key` (composite) and enforces
1115    /// uniqueness on INSERT.
1116    Unique {
1117        name: Option<String>,
1118        columns: Vec<String>,
1119        /// v7.13.0 — `NULLS NOT DISTINCT` modifier (mailrs round-5
1120        /// G10). PG 15+ flips the NULL handling so any number of
1121        /// NULL rows collide on the constraint. Default is
1122        /// `false` (NULLS DISTINCT, standard SQL behaviour).
1123        nulls_not_distinct: bool,
1124    },
1125    /// v7.13.0 — `CHECK (<expr>)` table-level constraint
1126    /// (mailrs round-5 G3). Column-level inline CHECKs fold into
1127    /// this same variant at parse time. Engine evaluates the
1128    /// predicate against each INSERT/UPDATE candidate row; a
1129    /// false / NULL result rejects the mutation.
1130    Check { name: Option<String>, expr: Expr },
1131    /// v7.15.0 — MySQL `KEY name (cols)` / `INDEX name (cols)`
1132    /// non-unique secondary-index declaration inline in CREATE
1133    /// TABLE. Engine builds a BTree index on the leading column
1134    /// (composite columns parse but only the leading column is
1135    /// honoured at v7.15 — matches the existing
1136    /// `CreateIndexStatement::extra_columns` semantics). Useful
1137    /// for `mysql/blog`-style schemas that lean on routine
1138    /// secondary indexes for ORM lookups.
1139    Index {
1140        name: Option<String>,
1141        columns: Vec<String>,
1142    },
1143    /// v7.17.0 Phase 2.2 — MySQL `FULLTEXT KEY/INDEX [name]
1144    /// (cols)` inline declaration. Pre-v7.17 the parser
1145    /// silently dropped these so MyISAM-imported FULLTEXT
1146    /// indexes vanished; v7.17 routes them through the
1147    /// existing tsvector-GIN engine path so MATCH AGAINST
1148    /// queries get a real inverted index instead of falling
1149    /// back to a full scan. Multi-column FULLTEXT KEYs build
1150    /// one GIN per column at v7.17 (per-column posting lists);
1151    /// the leading column drives query planning.
1152    FulltextIndex {
1153        name: Option<String>,
1154        columns: Vec<String>,
1155    },
1156}
1157
1158#[derive(Debug, Clone, PartialEq)]
1159#[allow(clippy::struct_excessive_bools)] // grammar-driven; each flag maps to a distinct PG column-constraint keyword
1160pub struct ColumnDef {
1161    pub name: String,
1162    pub ty: ColumnTypeName,
1163    pub nullable: bool,
1164    /// `DEFAULT <expr>` literal supplied at CREATE TABLE. Engine
1165    /// evaluates this once (with an empty row) and caches the resulting
1166    /// `Value` on the column schema.
1167    pub default: Option<Expr>,
1168    /// MySQL-style `AUTO_INCREMENT` — the engine maintains a counter
1169    /// per such column and fills the slot when INSERT leaves it
1170    /// unbound (omitted from a column-list INSERT or explicitly NULL).
1171    pub auto_increment: bool,
1172    /// v7.9.13 — inline `PRIMARY KEY` column constraint. mailrs
1173    /// migration follow-up F1. Implies `NOT NULL`. Engine creates
1174    /// an implicit BTree index named `<table>_pkey` over this
1175    /// column at CREATE TABLE time, satisfying the parent-side
1176    /// index requirement for any FOREIGN KEY pointing at it.
1177    pub is_primary_key: bool,
1178    /// v7.13.0 — inline `UNIQUE` column constraint
1179    /// (mailrs round-5 G2). The CREATE TABLE handler folds this
1180    /// into a single-column `TableConstraint::Unique` so the
1181    /// engine path stays uniform with table-level UNIQUE.
1182    pub is_unique: bool,
1183    /// v7.13.0 — inline `CHECK (<expr>)` column constraint
1184    /// (mailrs round-5 G3). Stored alongside the column so the
1185    /// CREATE TABLE handler can fold these into table-level
1186    /// CHECK constraints. Multiple inline CHECKs on the same
1187    /// column are concatenated with AND at the table level.
1188    pub check: Option<Expr>,
1189    /// v7.17.0 Phase 1.4 — user-defined type reference. When the
1190    /// parser sees an unknown column-type ident (anything not in
1191    /// the built-in `parse_column_type_name` table), it sets
1192    /// `ty = ColumnTypeName::Text` and records the original name
1193    /// here. The engine resolves at CREATE TABLE time: if a
1194    /// catalog enum/domain with this name exists, the column is
1195    /// bound to it (label-checked on INSERT for enums; CHECK-
1196    /// constrained for domains); otherwise the CREATE TABLE
1197    /// errors with "unknown type".
1198    pub user_type_ref: Option<String>,
1199    /// v7.17.0 Phase 2.1 — MySQL-style `ON UPDATE
1200    /// CURRENT_TIMESTAMP` column attribute. When set, an
1201    /// UPDATE that does NOT explicitly bind this column
1202    /// overrides the new value with `now()` (engine clock).
1203    /// Pre-v7.17 SPG silently accepted the syntax and never
1204    /// fired the override — `updated_at` columns from mysqldump
1205    /// stayed pinned at their initial DEFAULT forever, an
1206    /// audit Tier-S silent-failure. Generalised as a stored
1207    /// expression source so future shapes (`ON UPDATE
1208    /// CURRENT_TIMESTAMP(6)`, `ON UPDATE LOCALTIMESTAMP`) reuse
1209    /// the same field; v7.17 only accepts CURRENT_TIMESTAMP.
1210    pub on_update_runtime: Option<Expr>,
1211    /// v7.17.0 Phase 2.5 — text collation derived from the
1212    /// post-fix `COLLATE <name>` clause (and / or the table-level
1213    /// `COLLATE=<name>` for MySQL dumps that don't repeat it
1214    /// per column). Pre-2.5 SPG accepted the clause and
1215    /// discarded the name, leaving every column byte-compared
1216    /// — a Tier-S silent failure when the customer expected
1217    /// `_ci` / `case_insensitive` semantics. Parser normalises
1218    /// the raw collation name into the variants in `Collation`.
1219    /// Default `Binary` preserves the legacy compare path.
1220    pub collation: Collation,
1221    /// v7.17.0 Phase 4.4 — MySQL `UNSIGNED` modifier flag. Pre-
1222    /// 4.4 SPG accepted and discarded the keyword, leaving
1223    /// negative values silently accepted on a column the
1224    /// customer declared `INT UNSIGNED NOT NULL`. Now: the engine
1225    /// rejects negative INSERT / UPDATE values on UNSIGNED int
1226    /// columns. SPG widening to `u64`-shaped storage is out of
1227    /// v7.17 scope; the upper bound remains the signed-type max
1228    /// (i64::MAX for BIGINT UNSIGNED), which still strictly
1229    /// exceeds what every mailrs / Rails app actually uses.
1230    pub is_unsigned: bool,
1231    /// v7.17.0 Phase 3.P0-36 — MySQL inline `ENUM('a','b','c')`
1232    /// value list captured at parse time. When `Some`, the parser
1233    /// recognised `ENUM(...)` in the type slot; the engine
1234    /// validates INSERT cells against this list at
1235    /// column_def_to_schema time and persists the variants on
1236    /// `ColumnSchema.inline_enum_variants`. None for all
1237    /// non-ENUM columns.
1238    pub inline_enum_variants: Option<Vec<String>>,
1239    /// v7.17.0 Phase 3.P0-37 — MySQL inline `SET('a','b','c')`
1240    /// value list. Distinct from ENUM (subset semantics rather
1241    /// than pick-one). None for all non-SET columns.
1242    pub inline_set_variants: Option<Vec<String>>,
1243}
1244
1245/// v7.17.0 Phase 2.5 — text collation classification surfaced
1246/// from the SQL parser. Mirrors `spg_storage::Collation`; the
1247/// engine bridges between the two at CREATE TABLE time.
1248///
1249/// Recognised collation-name patterns (case-insensitive):
1250///   * `case_insensitive`, `*_ci`, `*_ai_ci`, `nocase`         → CaseInsensitive
1251///   * Everything else (`C`, `POSIX`, `default`,
1252///     `pg_catalog.default`, `*_cs`, `*_bin`, unknown names)   → Binary
1253#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1254pub enum Collation {
1255    Binary,
1256    CaseInsensitive,
1257}
1258
1259#[allow(clippy::derivable_impls)]
1260impl Default for Collation {
1261    fn default() -> Self {
1262        Self::Binary
1263    }
1264}
1265
1266impl Collation {
1267    /// Classify a `COLLATE <name>` ident into one of the supported
1268    /// variants. Empty / unknown names fall back to `Binary` —
1269    /// matches the pre-2.5 silent-accept behaviour for snapshots
1270    /// that load through but don't actually depend on the
1271    /// collation semantics.
1272    #[must_use]
1273    pub fn from_collation_name(name: &str) -> Self {
1274        let lc = name.trim().to_ascii_lowercase();
1275        // Strip any quotes / schema-qualifier the parser left on
1276        // (e.g. `pg_catalog.default`).
1277        let bare = lc
1278            .trim_matches(|c: char| c == '"' || c == '\'')
1279            .rsplit('.')
1280            .next()
1281            .unwrap_or("");
1282        if bare.is_empty() {
1283            return Self::Binary;
1284        }
1285        if bare == "case_insensitive" || bare == "nocase" {
1286            return Self::CaseInsensitive;
1287        }
1288        // MySQL `_ci` suffix (covers `utf8mb4_general_ci`,
1289        // `utf8mb4_unicode_ci`, `utf8mb4_0900_ai_ci`, …).
1290        if bare.ends_with("_ci") {
1291            return Self::CaseInsensitive;
1292        }
1293        Self::Binary
1294    }
1295}
1296
1297/// v7.6.0 — A single FOREIGN KEY constraint. Both column-level
1298/// `REFERENCES` and table-level `FOREIGN KEY (...) REFERENCES ...`
1299/// parse into this shape — the column-level form has a single-entry
1300/// `columns` / `parent_columns`.
1301#[derive(Debug, Clone, PartialEq)]
1302pub struct ForeignKeyConstraint {
1303    /// Optional `CONSTRAINT <name>` prefix. Engine ignores the name
1304    /// today but parses + stores it so a future ALTER TABLE DROP
1305    /// CONSTRAINT can target by name (v7.6.8).
1306    pub name: Option<String>,
1307    /// Local columns participating in the FK (≥ 1).
1308    pub columns: Vec<String>,
1309    /// Referenced parent table.
1310    pub parent_table: String,
1311    /// Referenced parent columns. Must have the same arity as
1312    /// `columns`; engine validates parent has a PK / UNIQUE index
1313    /// on exactly this column set (v7.6.1).
1314    pub parent_columns: Vec<String>,
1315    /// `ON DELETE` action. Defaults to `Restrict` if absent.
1316    pub on_delete: FkAction,
1317    /// `ON UPDATE` action. Defaults to `Restrict` if absent.
1318    pub on_update: FkAction,
1319}
1320
1321/// v7.6.0 — Referential action for `ON DELETE` / `ON UPDATE`.
1322#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1323pub enum FkAction {
1324    /// Reject the parent mutation if any child row references it.
1325    /// SQL spec default; SPG default when no clause is given.
1326    Restrict,
1327    /// Recursively propagate the parent's delete / update to the
1328    /// child rows. Same TX.
1329    Cascade,
1330    /// Set the child FK column(s) to NULL. Requires the FK columns
1331    /// to be NULL-able.
1332    SetNull,
1333    /// Set the child FK column(s) to their declared DEFAULT.
1334    /// Requires the child column(s) to have DEFAULT.
1335    SetDefault,
1336    /// SQL spec `NO ACTION` (deferred check). SPG treats this as
1337    /// `Restrict` because the single-writer model has no deferred
1338    /// constraint window; the keyword is accepted for compatibility.
1339    NoAction,
1340}
1341
1342/// In-cell encoding for a `VECTOR(N)` column. v6.0.1 added the
1343/// optional `USING <encoding>` clause; omitting it keeps the
1344/// pre-v6 `F32` default. `Sq8` quantises each cell to a per-vector
1345/// affine `(min, max, [u8; dim])` triple (4× compression). `F16`
1346/// (v6.0.3, DDL keyword `HALF`) stores each element as IEEE-754
1347/// binary16 (2× compression, ~3 decimal digits of precision).
1348#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
1349pub enum VecEncoding {
1350    /// IEEE-754 binary32. Pre-v6 default; matches pgvector's
1351    /// uncompressed `vector` type wire / storage layout.
1352    #[default]
1353    F32,
1354    /// v6.0.1 SQ8 — per-vector affine 8-bit quantisation. See
1355    /// `spg_storage::quantize::Sq8Vector` for the math + recall
1356    /// envelope (≥ 0.95 on Gaussian / unit-sphere corpora at
1357    /// dim ≥ 32).
1358    Sq8,
1359    /// v6.0.3 halfvec — IEEE-754 binary16 (half-precision)
1360    /// per-element. DDL keyword `HALF` (pgvector convention).
1361    /// Bit-exact dequantise to f32 at the storage layer; no
1362    /// rerank pass needed for kNN search.
1363    F16,
1364}
1365
1366impl fmt::Display for VecEncoding {
1367    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1368        match self {
1369            Self::F32 => f.write_str("F32"),
1370            Self::Sq8 => f.write_str("SQ8"),
1371            // pgvector convention: DDL keyword is `HALF`, not `F16`.
1372            Self::F16 => f.write_str("HALF"),
1373        }
1374    }
1375}
1376
1377/// SQL-level type names. The mapping to the storage runtime's `DataType`
1378/// happens in `spg-engine` — keeping `spg-sql` free of storage deps.
1379#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1380pub enum ColumnTypeName {
1381    SmallInt,
1382    Int,
1383    BigInt,
1384    Float,
1385    Text,
1386    /// `VARCHAR(N)` — TEXT capped at N Unicode characters.
1387    Varchar(u32),
1388    /// `CHAR(N)` — TEXT right-padded with spaces to exactly N characters.
1389    Char(u32),
1390    Bool,
1391    /// pgvector fixed-dimension `VECTOR(N)`. v6.0.1 added the
1392    /// `USING <encoding>` clause; omitting it surfaces as
1393    /// `encoding = VecEncoding::F32` (the pre-v6 default).
1394    Vector {
1395        dim: u32,
1396        encoding: VecEncoding,
1397    },
1398    /// `NUMERIC` / `NUMERIC(p)` / `NUMERIC(p, s)` — exact decimal.
1399    /// Bare `NUMERIC` and `NUMERIC(p)` both surface with `scale=0`.
1400    Numeric(u8, u8),
1401    /// `DATE` — calendar day, no time-of-day component.
1402    Date,
1403    /// `TIMESTAMP` / `MySQL` `DATETIME` — instant with microsecond
1404    /// precision.
1405    Timestamp,
1406    /// v7.9.2 `TIMESTAMPTZ` / `TIMESTAMP WITH TIME ZONE`. SPG
1407    /// stores all timestamps as UTC microseconds-since-epoch and
1408    /// does not carry per-row offset (PG's internal representation
1409    /// is the same — TZ is a display convention). The distinction
1410    /// from `TIMESTAMP` exists for the PG-wire layer to advertise
1411    /// OID 1184 so sqlx-style clients decode into
1412    /// `chrono::DateTime<Utc>` instead of `NaiveDateTime`.
1413    Timestamptz,
1414    /// v4.9 `JSON` — text-backed JSON document. No parse-time
1415    /// validation; the engine round-trips the literal verbatim.
1416    /// PG OID 114 on the wire.
1417    Json,
1418    /// v7.9.0 `JSONB` — same storage shape as Json, advertised as
1419    /// PG OID 3802 on the wire so sqlx-style binary-typed clients
1420    /// decode without a custom type registration.
1421    Jsonb,
1422    /// v7.10.4 `BYTES` / `BYTEA` — raw binary blob. PG wire OID 17.
1423    /// Literal forms (decoded by the engine at coercion time):
1424    ///   - PG hex form: `'\xDEADBEEF'`
1425    ///   - Escape form: `'foo\\000bar'` (backslash octal triples)
1426    Bytes,
1427    /// v7.10.10 `TEXT[]` — single-dimension TEXT array. PG wire
1428    /// OID 1009. Literal forms accepted by the parser:
1429    ///   - `ARRAY['a', 'b', NULL]`
1430    ///   - `'{a,b,NULL}'::TEXT[]` (engine decodes the external
1431    ///     form at coerce time)
1432    TextArray,
1433    /// v7.11.13 `INT[]` — single-dimension i32 array. PG wire OID
1434    /// 1007. Same literal forms as TEXT[] (substituting integer
1435    /// elements).
1436    IntArray,
1437    /// v7.11.13 `BIGINT[]` — single-dimension i64 array. PG wire
1438    /// OID 1016.
1439    BigIntArray,
1440    /// v7.12.0 `tsvector` — PG full-text search lexeme set. PG
1441    /// wire OID 3614. Literal: `'foo:1 bar:2'::tsvector` (PG
1442    /// external form). G-CRIT-3.
1443    TsVector,
1444    /// v7.12.0 `tsquery` — PG full-text search parse tree. PG
1445    /// wire OID 3615.
1446    TsQuery,
1447    /// v7.17.0 `UUID` — 128-bit identifier. PG wire OID 2950.
1448    /// Literal input accepts canonical hyphenated, unhyphenated,
1449    /// uppercase, and `{...}`-braced forms; display normalises to
1450    /// canonical lowercase 8-4-4-4-12. The drop-in PG surface for
1451    /// Django / Rails / Hibernate `id UUID PRIMARY KEY DEFAULT
1452    /// gen_random_uuid()`.
1453    Uuid,
1454    /// v7.17.0 Phase 3.P0-32 `TIME` (without time zone) — i64
1455    /// microseconds since 00:00:00. PG wire OID 1083. Literal
1456    /// input is `'HH:MM:SS'` with an optional `.fraction` suffix
1457    /// (6-digit microsecond precision). Display normalises to
1458    /// the canonical `HH:MM:SS[.ffffff]`.
1459    Time,
1460    /// v7.17.0 Phase 3.P0-33 MySQL `YEAR` — u16 in range
1461    /// 1901..=2155 plus the zero-year sentinel 0. No dedicated
1462    /// PG OID; advertised as INT4 on the wire. Display always
1463    /// 4 digits zero-padded.
1464    Year,
1465    /// v7.17.0 Phase 3.P0-34 PG `TIME WITH TIME ZONE` (TIMETZ) —
1466    /// i64 us since 00:00:00 (local) + i32 offset_secs from UTC.
1467    /// Wire OID 1266. Literal input is `'HH:MM:SS[.ffffff]±HH[:MM]'`.
1468    /// Offset range: ±14 hours.
1469    TimeTz,
1470    /// v7.17.0 Phase 3.P0-35 PG `MONEY` — i64 cents
1471    /// (locale-independent storage). Wire OID 790. Literal input
1472    /// accepts `$N.NN`, `$N,NNN.NN`, bare integer (treated as
1473    /// major units), optional leading `-`. Display: en_US locale.
1474    Money,
1475    /// v7.17.0 Phase 3.P0-38 PG range types. Pair stores the
1476    /// element kind tag (Int4 / Int8 / Num / Ts / TsTz / Date)
1477    /// — the engine bridges to `DataType::Range(RangeKind)`.
1478    Range(RangeKindAst),
1479    /// v7.17.0 Phase 3.P0-39 PG `hstore` extension type — flat
1480    /// `text => text` map with NULL value support.
1481    Hstore,
1482    /// v7.17.0 Phase 3.P0-40 — 2D arrays for INT / TEXT / BIGINT.
1483    IntArray2D,
1484    BigIntArray2D,
1485    TextArray2D,
1486}
1487
1488/// v7.17.0 Phase 3.P0-38 — PG range element kind. Mirrors
1489/// `spg_storage::RangeKind`; we keep it spg-sql-local so the AST
1490/// crate doesn't depend on storage. Bridged at engine boundary.
1491#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
1492pub enum RangeKindAst {
1493    Int4,
1494    Int8,
1495    Num,
1496    Ts,
1497    TsTz,
1498    Date,
1499}
1500
1501impl fmt::Display for ColumnTypeName {
1502    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1503        match self {
1504            Self::SmallInt => f.write_str("SMALLINT"),
1505            Self::Int => f.write_str("INT"),
1506            Self::BigInt => f.write_str("BIGINT"),
1507            Self::Float => f.write_str("FLOAT"),
1508            Self::Text => f.write_str("TEXT"),
1509            Self::Varchar(n) => write!(f, "VARCHAR({n})"),
1510            Self::Char(n) => write!(f, "CHAR({n})"),
1511            Self::Bool => f.write_str("BOOL"),
1512            Self::Vector { dim, encoding } => match encoding {
1513                VecEncoding::F32 => write!(f, "VECTOR({dim})"),
1514                VecEncoding::Sq8 => write!(f, "VECTOR({dim}) USING SQ8"),
1515                VecEncoding::F16 => write!(f, "VECTOR({dim}) USING HALF"),
1516            },
1517            Self::Json => f.write_str("JSON"),
1518            Self::Jsonb => f.write_str("JSONB"),
1519            Self::Bytes => f.write_str("BYTEA"),
1520            Self::TextArray => f.write_str("TEXT[]"),
1521            Self::IntArray => f.write_str("INT[]"),
1522            Self::BigIntArray => f.write_str("BIGINT[]"),
1523            Self::TsVector => f.write_str("TSVECTOR"),
1524            Self::TsQuery => f.write_str("TSQUERY"),
1525            Self::Uuid => f.write_str("UUID"),
1526            Self::Numeric(p, s) => {
1527                if *s == 0 {
1528                    write!(f, "NUMERIC({p})")
1529                } else {
1530                    write!(f, "NUMERIC({p}, {s})")
1531                }
1532            }
1533            Self::Date => f.write_str("DATE"),
1534            Self::Timestamp => f.write_str("TIMESTAMP"),
1535            Self::Timestamptz => f.write_str("TIMESTAMPTZ"),
1536            Self::Time => f.write_str("TIME"),
1537            Self::Year => f.write_str("YEAR"),
1538            Self::TimeTz => f.write_str("TIMETZ"),
1539            Self::Money => f.write_str("MONEY"),
1540            Self::Range(k) => f.write_str(match k {
1541                RangeKindAst::Int4 => "INT4RANGE",
1542                RangeKindAst::Int8 => "INT8RANGE",
1543                RangeKindAst::Num => "NUMRANGE",
1544                RangeKindAst::Ts => "TSRANGE",
1545                RangeKindAst::TsTz => "TSTZRANGE",
1546                RangeKindAst::Date => "DATERANGE",
1547            }),
1548            Self::Hstore => f.write_str("HSTORE"),
1549            Self::IntArray2D => f.write_str("INT[][]"),
1550            Self::BigIntArray2D => f.write_str("BIGINT[][]"),
1551            Self::TextArray2D => f.write_str("TEXT[][]"),
1552        }
1553    }
1554}
1555
1556/// `UPDATE <table> SET col = expr [, ...] [WHERE cond]`. v4.4 — the
1557/// engine evaluates `expr` per matched row in the table's row order
1558/// and rewrites cells in place. Indexed columns are dropped + re-
1559/// inserted into the affected B-tree on each row change.
1560#[derive(Debug, Clone, PartialEq)]
1561pub struct UpdateStatement {
1562    pub table: String,
1563    pub assignments: Vec<(String, Expr)>,
1564    pub where_: Option<Expr>,
1565    /// v7.9.4 — `RETURNING <projection>`. None = no RETURNING
1566    /// clause (legacy CommandComplete path). Some = engine
1567    /// evaluates the projection over each mutated row and
1568    /// streams the result as a Rows QueryResult.
1569    pub returning: Option<Vec<SelectItem>>,
1570}
1571
1572/// `DELETE FROM <table> [WHERE cond]`. v4.4 — removes matched rows
1573/// from the active catalog and prunes them from every index.
1574#[derive(Debug, Clone, PartialEq)]
1575pub struct DeleteStatement {
1576    pub table: String,
1577    pub where_: Option<Expr>,
1578    /// v7.9.4 — `RETURNING <projection>`.
1579    pub returning: Option<Vec<SelectItem>>,
1580}
1581
1582/// v7.17.0 Phase 3.P0-42 — SQL:2003 / PG 15+ MERGE statement.
1583/// One WHEN clause fires per source row depending on whether the
1584/// `on` condition matched any target row(s); the executor walks
1585/// `clauses` in declaration order and fires the first whose
1586/// `matched` kind and optional `condition` are both satisfied.
1587#[derive(Debug, Clone, PartialEq)]
1588pub struct MergeStatement {
1589    pub target: String,
1590    pub target_alias: Option<String>,
1591    pub source: String,
1592    pub source_alias: Option<String>,
1593    pub on: Expr,
1594    pub clauses: Vec<MergeWhenClause>,
1595}
1596
1597#[derive(Debug, Clone, PartialEq)]
1598pub struct MergeWhenClause {
1599    pub matched: MergeMatched,
1600    /// Optional `AND <expr>` filter — when present, the clause
1601    /// only fires for the source rows whose match-pair satisfies
1602    /// the predicate.
1603    pub condition: Option<Expr>,
1604    pub action: MergeAction,
1605}
1606
1607#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1608pub enum MergeMatched {
1609    Matched,
1610    NotMatched,
1611}
1612
1613#[derive(Debug, Clone, PartialEq)]
1614pub enum MergeAction {
1615    /// `INSERT (cols) VALUES (vals)`. SPG v7.17 requires the
1616    /// explicit column list (the bare `INSERT VALUES (vals)`
1617    /// shape lands later).
1618    Insert {
1619        columns: Vec<String>,
1620        values: Vec<Expr>,
1621    },
1622    /// `UPDATE SET col = expr [, …]` — applied to every matched
1623    /// target row for the firing source row.
1624    Update { assignments: Vec<(String, Expr)> },
1625    /// `DELETE` — drop every matched target row.
1626    Delete,
1627    /// `DO NOTHING` — explicit no-op (the SQL standard accepts
1628    /// the clause and SPG mirrors so a customer-side MERGE that
1629    /// uses it for branch-control doesn't error).
1630    DoNothing,
1631}
1632
1633#[derive(Debug, Clone, PartialEq)]
1634pub struct InsertStatement {
1635    pub table: String,
1636    /// Optional column list — `INSERT INTO t (a, b) VALUES (...)`. When
1637    /// `None`, every tuple is positional and must match the table arity.
1638    /// When `Some`, the engine maps each tuple slot to the named column and
1639    /// fills the rest with NULL (must be nullable).
1640    pub columns: Option<Vec<String>>,
1641    /// One or more `(expr, expr, ...)` tuples — the multi-row VALUES form.
1642    /// v1.3+ accepts `INSERT INTO t VALUES (a), (b)`. Empty when
1643    /// `select_source` is `Some` (the engine builds rows from the
1644    /// inner SELECT result set instead).
1645    pub rows: Vec<Vec<Expr>>,
1646    /// v7.13.0 — `INSERT INTO t [(cols)] SELECT …` (mailrs
1647    /// round-5 G4). When present, `rows` is empty and the engine
1648    /// materialises the SELECT result, coerces each output tuple to
1649    /// the target column types, and inserts as a single batch.
1650    pub select_source: Option<Box<SelectStatement>>,
1651    /// v7.9.7 — `ON CONFLICT (cols) DO { NOTHING | UPDATE SET … }`
1652    /// upsert clause. None = legacy INSERT (conflict raises a
1653    /// DuplicateKey error). mailrs migration blocker #2.
1654    pub on_conflict: Option<OnConflictClause>,
1655    /// v7.9.4 — `RETURNING <projection>`.
1656    pub returning: Option<Vec<SelectItem>>,
1657}
1658
1659/// v7.9.7 — INSERT upsert clause: `ON CONFLICT (target) DO action`.
1660#[derive(Debug, Clone, PartialEq)]
1661pub struct OnConflictClause {
1662    /// Local columns that identify the conflict (must match a
1663    /// UNIQUE / PRIMARY KEY index on the target table). Empty
1664    /// list means the user wrote `ON CONFLICT DO …` without a
1665    /// target — engine picks the table's first BTree index by
1666    /// convention.
1667    pub target_columns: Vec<String>,
1668    /// The action on conflict.
1669    pub action: OnConflictAction,
1670}
1671
1672/// v7.9.7 — action on conflict.
1673#[derive(Debug, Clone, PartialEq)]
1674pub enum OnConflictAction {
1675    /// `DO NOTHING` — INSERT proceeds for non-conflicting rows,
1676    /// silently skips conflicting ones.
1677    Nothing,
1678    /// `DO UPDATE SET col = expr [, …] [WHERE cond]`. `assignments`
1679    /// may reference `EXCLUDED.col` to read the incoming row's
1680    /// value (engine wires `EXCLUDED` as a virtual table).
1681    Update {
1682        assignments: Vec<(String, Expr)>,
1683        where_: Option<Expr>,
1684    },
1685}
1686
1687#[derive(Debug, Clone, PartialEq, Default)]
1688pub struct SelectStatement {
1689    /// v4.11: `WITH name AS (SELECT ...) [, ...]` common-table
1690    /// expressions, materialised once at query start before the
1691    /// body SELECT runs. Empty for a regular SELECT. Non-recursive
1692    /// only — no `WITH RECURSIVE` for v4.x.
1693    pub ctes: Vec<Cte>,
1694    pub distinct: bool,
1695    pub items: Vec<SelectItem>,
1696    pub from: Option<FromClause>,
1697    pub where_: Option<Expr>,
1698    pub group_by: Option<Vec<Expr>>,
1699    /// v6.4.1 — `GROUP BY ALL` shortcut: when true, the planner
1700    /// expands `group_by` to every non-aggregate SELECT-list item
1701    /// before the executor runs. Mutually exclusive with an
1702    /// explicit `group_by` list (the parser sets exactly one).
1703    pub group_by_all: bool,
1704    /// `HAVING <expr>` — filter applied *after* `GROUP BY` aggregation.
1705    /// Supports aggregate calls (e.g. `HAVING count(*) > 1`); the
1706    /// aggregate executor resolves them through the same synthetic
1707    /// schema used for the SELECT items.
1708    pub having: Option<Expr>,
1709    /// UNION / UNION ALL chain. Empty for a plain SELECT. Each peer is
1710    /// itself a `SelectStatement` with `order_by = None` and `limit =
1711    /// None` (the parser enforces that — ORDER BY / LIMIT belong to the
1712    /// top of the chain).
1713    pub unions: Vec<(UnionKind, SelectStatement)>,
1714    /// v6.4.0 — multi-key ORDER BY. Empty `Vec` means no ORDER BY.
1715    /// Keys are matched left-to-right: first key decides, ties break
1716    /// to the second, etc.
1717    pub order_by: Vec<OrderBy>,
1718    /// `LIMIT <n>` — bound on row output. `n` is an integer
1719    /// literal **or** (v7.9.24) a placeholder `$N` resolved
1720    /// against the prepared-statement Bind values. mailrs
1721    /// migration follow-up H2.
1722    pub limit: Option<LimitExpr>,
1723    /// `OFFSET <n>` — drop the first `n` rows after ORDER BY but
1724    /// before LIMIT (so `LIMIT 10 OFFSET 5` keeps rows 6..=15).
1725    pub offset: Option<LimitExpr>,
1726    /// v7.17.0 Phase 3.P0-49 — `FETCH FIRST <n> ROWS WITH TIES`
1727    /// (SQL:2008). When true and an ORDER BY is present, the
1728    /// executor extends past the LIMIT-truncated tail to include
1729    /// every row whose ORDER BY key equals the last-kept row's
1730    /// key. Requires an ORDER BY; the executor errors otherwise
1731    /// (matching PG's `WITH TIES` rule). The parser was already
1732    /// accepting `WITH TIES` since Phase 5.1; this field captures
1733    /// the choice so the executor can act on it.
1734    pub limit_with_ties: bool,
1735}
1736
1737/// v7.9.24 — LIMIT / OFFSET value. Integer literal at parse
1738/// time or a placeholder `$N` resolved during extended-query
1739/// Bind. mailrs migration follow-up H2.
1740#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1741pub enum LimitExpr {
1742    /// `LIMIT 10` — value known at parse time.
1743    Literal(u32),
1744    /// `LIMIT $N` — the 1-based parameter index, resolved against
1745    /// the bind values when the prepared statement executes.
1746    Placeholder(u16),
1747}
1748
1749impl fmt::Display for LimitExpr {
1750    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1751        match self {
1752            Self::Literal(n) => write!(f, "{n}"),
1753            Self::Placeholder(n) => write!(f, "${n}"),
1754        }
1755    }
1756}
1757
1758impl LimitExpr {
1759    /// Convenience for the simple-query path where no placeholders
1760    /// can possibly exist. Returns the literal value or `None` if
1761    /// this is a placeholder (caller must surface as Unsupported).
1762    pub fn as_literal(self) -> Option<u32> {
1763        match self {
1764            Self::Literal(n) => Some(n),
1765            Self::Placeholder(_) => None,
1766        }
1767    }
1768}
1769
1770/// v7.9.24 — extract LIMIT / OFFSET as a `u32` literal. After
1771/// the engine's `substitute_placeholders` pass these are
1772/// always Literal; in the simple-query path a Placeholder
1773/// shape returns None (executor surfaces as
1774/// "LIMIT/OFFSET ${n} requires prepared-statement binding").
1775impl SelectStatement {
1776    #[must_use]
1777    pub fn limit_literal(&self) -> Option<u32> {
1778        self.limit.and_then(LimitExpr::as_literal)
1779    }
1780    #[must_use]
1781    pub fn offset_literal(&self) -> Option<u32> {
1782        self.offset.and_then(LimitExpr::as_literal)
1783    }
1784}
1785
1786#[derive(Debug, Clone, PartialEq)]
1787pub struct Cte {
1788    pub name: String,
1789    pub body: SelectStatement,
1790    /// v4.22: `WITH RECURSIVE` — set when the WITH clause had the
1791    /// RECURSIVE keyword. Applies to every CTE in the clause per
1792    /// PG semantics. A non-recursive body in a RECURSIVE WITH is
1793    /// allowed; the engine just runs it once.
1794    pub recursive: bool,
1795    /// v4.22: optional `WITH name(a, b, c)` column-name list. When
1796    /// non-empty, these override the body's output column names
1797    /// position-by-position; the engine errors out if the count
1798    /// doesn't match the body's projection width.
1799    pub column_overrides: Vec<String>,
1800}
1801
1802#[derive(Debug, Clone, PartialEq)]
1803pub struct OrderBy {
1804    pub expr: Expr,
1805    /// `false` = ASC (default), `true` = DESC.
1806    pub desc: bool,
1807    /// v7.24 (mailrs round-16 A) — explicit `NULLS FIRST` /
1808    /// `NULLS LAST`. `None` = PG default (NULLS LAST for ASC,
1809    /// NULLS FIRST for DESC); the engine resolves the effective
1810    /// value via `nulls_first.unwrap_or(desc)`.
1811    pub nulls_first: Option<bool>,
1812}
1813
1814#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1815pub enum UnionKind {
1816    /// `UNION` — dedupes the combined set.
1817    Distinct,
1818    /// `UNION ALL` — concatenates without dedup.
1819    All,
1820}
1821
1822#[derive(Debug, Clone, PartialEq)]
1823pub enum SelectItem {
1824    Wildcard,
1825    Expr { expr: Expr, alias: Option<String> },
1826}
1827
1828#[derive(Debug, Clone, PartialEq)]
1829pub struct TableRef {
1830    pub name: String,
1831    pub alias: Option<String>,
1832    /// v6.10.2 — `AS OF SEGMENT '<id>'` cold-tier time-travel.
1833    /// When `Some(id)`, the scan restricts to rows that live in
1834    /// segment `<id>` only — useful for forensic inspection of a
1835    /// specific freezer-emitted segment without exposing the hot
1836    /// tier. `AS OF TIMESTAMP <ts>` (PG-flavoured time travel)
1837    /// is STABILITY carve-out for v6.10 — needs the freezer to
1838    /// stamp each segment with a wall-clock at creation time.
1839    pub as_of_segment: Option<u32>,
1840    /// v7.11.7 — `FROM unnest(<expr>) [AS] <alias>` set-returning
1841    /// source. When `Some`, `name` is the alias (defaulting to
1842    /// `"unnest"` when no `AS` is given) and the engine builds a
1843    /// synthetic single-column table by evaluating the expression
1844    /// once at SELECT entry. Each TEXT[] element becomes one row;
1845    /// NULL elements become NULL cells. v7.11 supported
1846    /// uncorrelated UNNEST only as the FROM primary; v7.13.2
1847    /// (mailrs round-6 S5) widens to UNNEST in any FROM-list
1848    /// position (cross-join with regular tables).
1849    pub unnest_expr: Option<Box<Expr>>,
1850    /// v7.13.2 — mailrs round-6 S5. PG-standard
1851    /// `UNNEST(<arr>) AS alias(col_name)` column-list aliasing:
1852    /// when non-empty, the first entry overrides the projected
1853    /// column name for the unnested column. Empty = fall back to
1854    /// the table alias (pre-v7.13.2 behaviour).
1855    pub unnest_column_aliases: Vec<String>,
1856    /// v7.17.0 Phase 3.10 — `FROM generate_series(start, stop
1857    /// [, step])` set-returning source. When `Some`, the engine
1858    /// materialises a single-column virtual table by stepping
1859    /// `start` to `stop` inclusive. Args are the literal arg list
1860    /// (2 for default-step, 3 for explicit-step). Supports:
1861    ///   * SmallInt / Int / BigInt with integer step (default = 1)
1862    ///   * Timestamp with INTERVAL step (PG date-range pattern)
1863    /// Mutually exclusive with `unnest_expr` — both populate the
1864    /// same downstream dispatch slot. `name` defaults to
1865    /// `"generate_series"` when no alias is provided.
1866    pub generate_series_args: Option<Vec<Expr>>,
1867    /// v7.17.0 Phase 3.P0-41 — `LATERAL ( SELECT … )` derived
1868    /// table. When `Some`, the TableRef is a parenthesised SELECT
1869    /// that may reference columns from the preceding FROM items
1870    /// (correlated derived table). The executor materialises the
1871    /// subquery per left-row, substituting outer-column references
1872    /// against the current join row's values before running the
1873    /// inner SELECT, then cross-joins the result back.
1874    /// Mutually exclusive with `name` / `unnest_expr` /
1875    /// `generate_series_args`.
1876    pub lateral_subquery: Option<Box<SelectStatement>>,
1877}
1878
1879/// FROM clause shape. v1.10 accepts a primary table plus a flat list of
1880/// joined peers — `FROM a [, b]* [INNER|LEFT] JOIN c ON expr ...`. The
1881/// joins evaluate left-associatively in nested-loop order.
1882#[derive(Debug, Clone, PartialEq)]
1883pub struct FromClause {
1884    pub primary: TableRef,
1885    pub joins: Vec<FromJoin>,
1886}
1887
1888#[derive(Debug, Clone, PartialEq)]
1889pub struct FromJoin {
1890    pub kind: JoinKind,
1891    pub table: TableRef,
1892    /// Required for INNER/LEFT; must be `None` for CROSS / comma-list.
1893    pub on: Option<Expr>,
1894}
1895
1896#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1897pub enum JoinKind {
1898    Inner,
1899    Left,
1900    Cross,
1901}
1902
1903#[derive(Debug, Clone, PartialEq)]
1904pub enum Expr {
1905    Literal(Literal),
1906    Column(ColumnName),
1907    /// v6.1.1 — `$N` parameter placeholder for the extended query
1908    /// protocol. The number is 1-based per PostgreSQL convention.
1909    /// Evaluation looks up `params[N-1]` from the prepared-statement
1910    /// bind buffer; out-of-range indices raise a runtime error
1911    /// (same shape as a column-not-found miss).
1912    Placeholder(u16),
1913    Binary {
1914        lhs: Box<Expr>,
1915        op: BinOp,
1916        rhs: Box<Expr>,
1917    },
1918    Unary {
1919        op: UnOp,
1920        expr: Box<Expr>,
1921    },
1922    /// PG-style `expr::TYPE` cast. v1.3 supports VECTOR, INT, BIGINT, FLOAT,
1923    /// TEXT, BOOL targets; engine coerces at evaluation time.
1924    Cast {
1925        expr: Box<Expr>,
1926        target: CastTarget,
1927    },
1928    /// Postfix `IS NULL` / `IS NOT NULL`. Returns BOOL.
1929    IsNull {
1930        expr: Box<Expr>,
1931        negated: bool,
1932    },
1933    /// Function call `name(args...)`. v1.4 supports a small built-in set
1934    /// (length, upper, lower, abs, coalesce); unknown names error at eval
1935    /// time so the parser stays open for v1.5 aggregates.
1936    FunctionCall {
1937        name: String,
1938        args: Vec<Expr>,
1939    },
1940    /// v7.24 (mailrs round-16 A) — an aggregate call with an
1941    /// internal ordering: `array_agg(x ORDER BY y DESC NULLS LAST)`.
1942    /// Wraps the plain [`Expr::FunctionCall`] so every existing
1943    /// FunctionCall consumer stays untouched; only the aggregate
1944    /// executor (and the expression walkers) know the wrapper.
1945    /// Non-aggregate evaluation contexts reject it at eval time.
1946    AggregateOrdered {
1947        call: Box<Expr>,
1948        order_by: Vec<OrderBy>,
1949        /// v7.25 (round-17) — `COUNT(DISTINCT x)` /
1950        /// `string_agg(DISTINCT s, ',')`. The wrapper carries every
1951        /// aggregate modifier so plain FunctionCall stays untouched.
1952        distinct: bool,
1953        /// v7.32 (mailrs round-29) — `agg(args) FILTER (WHERE cond)`.
1954        /// Only the rows where `cond` is true contribute to this
1955        /// aggregate (SQL:2003 T612 / PG 9.4). Carried as a first-class
1956        /// modifier — NOT desugared to `agg(CASE WHEN cond THEN arg
1957        /// END)`, which is faithful for NULL-ignoring aggregates but
1958        /// WRONG for `array_agg` (it would collect a NULL per excluded
1959        /// row). The executor instead skips excluded rows before
1960        /// accumulation, which is correct for every aggregate.
1961        filter: Option<Box<Expr>>,
1962    },
1963    /// SQL `LIKE` predicate. `pattern` evaluates to text at runtime;
1964    /// wildcards are `%` (any run) and `_` (one char), backslash escapes
1965    /// the next char (so `\%` matches a literal `%`).
1966    Like {
1967        expr: Box<Expr>,
1968        pattern: Box<Expr>,
1969        negated: bool,
1970        /// v7.25 (mailrs round-17) — `ILIKE`: case-insensitive
1971        /// match. PG folds both operands.
1972        case_insensitive: bool,
1973    },
1974    /// v4.12 window function call: `name(args) OVER (PARTITION BY
1975    /// ... ORDER BY ...)`. Supports `ROW_NUMBER` / `RANK` /
1976    /// `DENSE_RANK` and the partition-aware aggregates `SUM` /
1977    /// `AVG` / `COUNT` / `MIN` / `MAX`. The window frame defaults to "entire partition" for
1978    /// unordered windows and "from start of partition through
1979    /// current row" for ordered windows — no explicit ROWS /
1980    /// RANGE clause in v4.12 MVP.
1981    WindowFunction {
1982        name: String,
1983        args: Vec<Expr>,
1984        partition_by: Vec<Expr>,
1985        /// v7.24.1 — third slot: explicit NULLS FIRST/LAST
1986        /// (None = PG default, same contract as [`OrderBy`]).
1987        order_by: Vec<(
1988            Expr,
1989            bool,         /* desc */
1990            Option<bool>, /* nulls_first */
1991        )>,
1992        /// v4.20 explicit frame. `None` means "use the default":
1993        /// whole-partition when unordered, running aggregate from
1994        /// partition start through current row when ordered.
1995        frame: Option<WindowFrame>,
1996        /// v6.4.2 — `IGNORE NULLS` / `RESPECT NULLS` modifier on
1997        /// LAG / LEAD / FIRST_VALUE / LAST_VALUE. Default is
1998        /// `Respect` (PG / ANSI default — NULLs participate). Other
1999        /// window functions ignore this flag.
2000        null_treatment: NullTreatment,
2001    },
2002    /// v4.10 scalar subquery — `(SELECT ...)` used in expression
2003    /// position. Must return exactly one row × one column at eval
2004    /// time; the engine errors out otherwise. Uncorrelated only —
2005    /// the inner SELECT cannot reference outer columns.
2006    ScalarSubquery(Box<SelectStatement>),
2007    /// v4.10 `[NOT] EXISTS (SELECT ...)`. Returns Bool. Inner
2008    /// projection is ignored; only row-count matters.
2009    Exists {
2010        subquery: Box<SelectStatement>,
2011        negated: bool,
2012    },
2013    /// v4.10 `expr [NOT] IN (SELECT ...)`. Inner SELECT must
2014    /// project exactly one column; membership is tested by Eq
2015    /// against each row's value (NULL handling follows ANSI:
2016    /// NULL ∈ list ⇒ NULL ; otherwise present ⇒ true).
2017    InSubquery {
2018        expr: Box<Expr>,
2019        subquery: Box<SelectStatement>,
2020        negated: bool,
2021    },
2022    /// v7.30.2 (mailrs round-25) — `expr [NOT] IN (a, b, …)` as a FLAT
2023    /// list. Both the parser's literal-list path and the engine's
2024    /// IN-subquery materialisation used to desugar into a left-deep
2025    /// OR-Eq chain, so expression depth scaled with the element count
2026    /// — a 24k-row subquery result overflowed the 2 MiB worker stack
2027    /// (recursive eval AND recursive Box drop) and aborted embedding
2028    /// host processes. The flat node keeps depth constant: eval is an
2029    /// iterative scan with PG three-valued logic, drop is a Vec drop.
2030    InList {
2031        expr: Box<Expr>,
2032        list: Vec<Expr>,
2033        negated: bool,
2034    },
2035    /// `EXTRACT(<field> FROM <source>)` — pull an integer component
2036    /// out of a `DATE` or `TIMESTAMP`. Parsed as its own AST node
2037    /// because the `FROM` keyword is what separates the two halves,
2038    /// not a comma.
2039    Extract {
2040        field: ExtractField,
2041        source: Box<Expr>,
2042    },
2043    /// v7.10.10 — `ARRAY[expr, expr, …]` array constructor. Each
2044    /// element is evaluated independently; NULLs are allowed.
2045    /// v7.10 supports only single-dimension TEXT[] semantically;
2046    /// non-text elements coerce at engine evaluation time when
2047    /// the surrounding context (column type / cast) makes the
2048    /// target clear.
2049    Array(Vec<Expr>),
2050    /// v7.10.10 — array subscript `arr[i]`. PG 1-based; the
2051    /// engine returns NULL for out-of-range indices.
2052    ArraySubscript {
2053        target: Box<Expr>,
2054        index: Box<Expr>,
2055    },
2056    /// v7.10.12 — `expr op ANY(arr)` and `expr op ALL(arr)`. The
2057    /// operator is the comparison binary op (Eq / Ne / Lt / …);
2058    /// the engine desugars: `ANY` returns true if any element
2059    /// satisfies; `ALL` returns true only if every element does.
2060    /// NULL handling follows PG's three-valued logic.
2061    AnyAll {
2062        expr: Box<Expr>,
2063        op: BinOp,
2064        array: Box<Expr>,
2065        /// `true` = ANY, `false` = ALL.
2066        is_any: bool,
2067    },
2068    /// v7.13.0 — `CASE WHEN <cond> THEN <val> ... ELSE <val> END`
2069    /// (searched form, `operand` is None) and
2070    /// `CASE <expr> WHEN <val> THEN <val> ... END` (simple form,
2071    /// `operand` is the lead expression compared against each
2072    /// branch's match). Each `(when_expr, then_expr)` branch
2073    /// stays as written; engine short-circuits on the first match.
2074    /// `else_branch` is `None` when no ELSE; evaluates to NULL.
2075    /// mailrs round-5 G9.
2076    Case {
2077        operand: Option<Box<Expr>>,
2078        branches: Vec<(Expr, Expr)>,
2079        else_branch: Option<Box<Expr>>,
2080    },
2081}
2082
2083/// v6.4.2 — null treatment on `LAG` / `LEAD` / `FIRST_VALUE` /
2084/// `LAST_VALUE`. PG / ANSI default is `Respect` — NULLs participate
2085/// in the offset walk. `Ignore` causes the function to skip NULL
2086/// values in the argument expression, returning the next non-NULL.
2087#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
2088pub enum NullTreatment {
2089    #[default]
2090    Respect,
2091    Ignore,
2092}
2093
2094/// v4.20 explicit window frame: `ROWS|RANGE BETWEEN <bound> AND
2095/// <bound>`. `end` is `None` for the shorthand "ROWS <bound>"
2096/// where end implicitly = CURRENT ROW.
2097#[derive(Debug, Clone, PartialEq, Eq)]
2098pub struct WindowFrame {
2099    pub kind: FrameKind,
2100    pub start: FrameBound,
2101    pub end: Option<FrameBound>,
2102}
2103
2104#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2105pub enum FrameKind {
2106    Rows,
2107    Range,
2108}
2109
2110#[derive(Debug, Clone, PartialEq, Eq)]
2111pub enum FrameBound {
2112    UnboundedPreceding,
2113    OffsetPreceding(u64),
2114    CurrentRow,
2115    OffsetFollowing(u64),
2116    UnboundedFollowing,
2117}
2118
2119impl fmt::Display for FrameBound {
2120    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2121        match self {
2122            Self::UnboundedPreceding => f.write_str("UNBOUNDED PRECEDING"),
2123            Self::OffsetPreceding(n) => write!(f, "{n} PRECEDING"),
2124            Self::CurrentRow => f.write_str("CURRENT ROW"),
2125            Self::OffsetFollowing(n) => write!(f, "{n} FOLLOWING"),
2126            Self::UnboundedFollowing => f.write_str("UNBOUNDED FOLLOWING"),
2127        }
2128    }
2129}
2130
2131#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2132pub enum ExtractField {
2133    Year,
2134    Month,
2135    Day,
2136    Hour,
2137    Minute,
2138    Second,
2139    Microsecond,
2140    /// Seconds since 1970-01-01 00:00:00 UTC (PG returns numeric;
2141    /// SPG keeps the integer convention — truncated seconds).
2142    Epoch,
2143}
2144
2145impl fmt::Display for ExtractField {
2146    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2147        f.write_str(match self {
2148            Self::Year => "YEAR",
2149            Self::Month => "MONTH",
2150            Self::Day => "DAY",
2151            Self::Hour => "HOUR",
2152            Self::Minute => "MINUTE",
2153            Self::Second => "SECOND",
2154            Self::Microsecond => "MICROSECOND",
2155            Self::Epoch => "EPOCH",
2156        })
2157    }
2158}
2159
2160#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2161pub enum CastTarget {
2162    Int,
2163    BigInt,
2164    Float,
2165    Text,
2166    Bool,
2167    Vector,
2168    Date,
2169    Timestamp,
2170    /// v7.9.25 — `::INTERVAL` and `::TIMESTAMPTZ`. mailrs follow-up
2171    /// H3a. Engine reuses the existing runtime-interval / timestamp
2172    /// paths (parse the text input, return the matching Value).
2173    Interval,
2174    Timestamptz,
2175    /// v7.9.25 — `::JSON` and `::JSONB`. SPG already has both
2176    /// types (v7.9.0); the cast just routes Text→Json with the
2177    /// requested OID for the wire layer.
2178    Json,
2179    Jsonb,
2180    /// v7.9.26 — `::regtype` / `::regclass`. Parsed for PG dump
2181    /// compatibility; engine surfaces as Unsupported with a
2182    /// hint to use `SHOW TABLES` or `spg_table_ddl`. mailrs F3b.
2183    RegType,
2184    RegClass,
2185    /// v7.10.11 — `::TEXT[]`. Engine decodes the LHS Text into
2186    /// the PG external array form `{a,b,NULL}`.
2187    TextArray,
2188    /// v7.11.13 — `::INT[]` / `::BIGINT[]`. Decodes PG external
2189    /// `{1,2,3}` or widens a `TextArray` whose elements are
2190    /// integer-shaped.
2191    IntArray,
2192    BigIntArray,
2193    /// v7.12.0 — `::tsvector` / `::tsquery`. Decodes the PG
2194    /// external form text representation. Used by pg_dump output
2195    /// and by `WHERE col @@ 'term'::tsquery` literal patterns.
2196    TsVector,
2197    TsQuery,
2198    /// v7.17.0 — `::uuid`. Decodes the LHS Text via
2199    /// `spg_storage::parse_uuid_str` (accepts canonical hyphenated,
2200    /// unhyphenated, uppercase, and brace-wrapped forms); malformed
2201    /// input is a SQL error.
2202    Uuid,
2203    /// v7.18 — `::bytea`. Decodes the LHS Text via PG's hex form
2204    /// (`'\xdeadbeef'`) or escape form (`'\x05\x00'`); Bytes
2205    /// inputs pass through unchanged. Closes the mailrs D-pre #3
2206    /// reverse-acceptance gap — anywhere a PG schema writes
2207    /// `expr::bytea`, SPG now matches.
2208    Bytea,
2209}
2210
2211impl fmt::Display for CastTarget {
2212    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2213        f.write_str(match self {
2214            Self::Int => "int",
2215            Self::BigInt => "bigint",
2216            Self::Float => "float",
2217            Self::Text => "text",
2218            Self::Bool => "bool",
2219            Self::Vector => "vector",
2220            Self::Interval => "interval",
2221            Self::Timestamptz => "timestamptz",
2222            Self::Json => "json",
2223            Self::Jsonb => "jsonb",
2224            Self::RegType => "regtype",
2225            Self::RegClass => "regclass",
2226            Self::Date => "date",
2227            Self::Timestamp => "timestamp",
2228            Self::TextArray => "TEXT[]",
2229            Self::IntArray => "INT[]",
2230            Self::BigIntArray => "BIGINT[]",
2231            Self::TsVector => "tsvector",
2232            Self::TsQuery => "tsquery",
2233            Self::Uuid => "uuid",
2234            Self::Bytea => "bytea",
2235        })
2236    }
2237}
2238
2239#[derive(Debug, Clone, PartialEq)]
2240pub enum Literal {
2241    Integer(i64),
2242    Float(f64),
2243    String(String),
2244    Bool(bool),
2245    Null,
2246    /// pgvector-style array literal, e.g. `[1, 2.5, -3]`.
2247    Vector(Vec<f32>),
2248    /// TEXT[] value carried through the prepared-bind path
2249    /// (`= ANY($1)` has no column context to re-parse a `{a,b}`
2250    /// text form, so the array rides the AST natively).
2251    TextArray(Vec<Option<String>>),
2252    /// INT[] value carried through the prepared-bind path.
2253    IntArray(Vec<Option<i32>>),
2254    /// BIGINT[] value carried through the prepared-bind path.
2255    BigIntArray(Vec<Option<i64>>),
2256    /// `INTERVAL '<n> <unit> [<n> <unit> ...]'` — calendar-aware span.
2257    /// Split into a months part (because a month is not a fixed number of
2258    /// days) and a microseconds part (everything sub-month). `text` keeps
2259    /// the original spelling so Display round-trips byte-for-byte.
2260    Interval {
2261        months: i32,
2262        micros: i64,
2263        text: String,
2264    },
2265}
2266
2267#[derive(Debug, Clone, PartialEq, Eq)]
2268pub struct ColumnName {
2269    pub qualifier: Option<String>,
2270    pub name: String,
2271}
2272
2273#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2274pub enum BinOp {
2275    Or,
2276    And,
2277    Eq,
2278    NotEq,
2279    /// v7.9.27b — PG `a IS DISTINCT FROM b` / `a IS NOT DISTINCT
2280    /// FROM b`. NULL-safe equality: NULL IS NOT DISTINCT FROM
2281    /// NULL → true, NULL IS DISTINCT FROM NULL → false. The
2282    /// non-NULL behaviour matches `<>` / `=` exactly. Common in
2283    /// PG-style JOIN ON predicates and pg_dump output.
2284    IsDistinctFrom,
2285    IsNotDistinctFrom,
2286    Lt,
2287    LtEq,
2288    Gt,
2289    GtEq,
2290    Add,
2291    Sub,
2292    Mul,
2293    Div,
2294    /// pgvector L2 (Euclidean) distance `<->`. Defined for two vector
2295    /// operands of equal dimension; engine returns `Value::Float(d)`.
2296    L2Distance,
2297    /// pgvector inner-product `<#>` — returns `-Σ aᵢ bᵢ` so "smaller =
2298    /// more similar" remains true (matches pgvector's published convention).
2299    InnerProduct,
2300    /// pgvector cosine distance `<=>` — `1 - (a·b)/(|a| |b|)`.
2301    CosineDistance,
2302    /// SQL string concatenation `||`. NULL propagates.
2303    Concat,
2304    /// Bitwise OR `|` on integers.
2305    BitOr,
2306    /// Bitwise AND `&` on integers.
2307    BitAnd,
2308    /// v4.14 `json -> key` — element access by string key (object)
2309    /// or integer index (array). Returns a JSON value.
2310    JsonGet,
2311    /// v4.14 `json ->> key` — same access, returns the result as
2312    /// TEXT (unwraps a top-level JSON string; renders other scalars
2313    /// as their canonical text).
2314    JsonGetText,
2315    /// v6.4.5 `json #> path_text` — walk the path encoded as a PG
2316    /// text array literal like `'{a,0,b}'`. Returns JSON.
2317    JsonGetPath,
2318    /// v6.4.5 `json #>> path_text` — same walk, returns TEXT.
2319    JsonGetPathText,
2320    /// v6.4.5 `json @> sub_json` — containment. Returns BOOL; true
2321    /// when every key/value in `sub_json` is structurally present in
2322    /// the left side. Matches PG semantics (top-level + recursive).
2323    JsonContains,
2324    /// v7.12.2 `tsvector @@ tsquery` — FTS match. Returns BOOL;
2325    /// 3VL on NULL. Symmetric: PG also accepts `tsquery @@
2326    /// tsvector` and engine eval normalises either ordering.
2327    TsMatch,
2328    /// v7.17.0 Phase 3.P0-47 — PG INET / CIDR strict contained-in
2329    /// `<<`. LHS network is strictly inside RHS network (no equality).
2330    InetContainedBy,
2331    /// v7.17.0 Phase 3.P0-47 — PG INET / CIDR contained-in-or-equal
2332    /// `<<=`. LHS network ⊆ RHS network.
2333    InetContainedByEq,
2334    /// v7.17.0 Phase 3.P0-47 — PG INET / CIDR strict contains `>>`.
2335    /// LHS network strictly contains RHS network.
2336    InetContains,
2337    /// v7.17.0 Phase 3.P0-47 — PG INET / CIDR contains-or-equal `>>=`.
2338    /// LHS network ⊇ RHS network.
2339    InetContainsEq,
2340    /// v7.17.0 Phase 3.P0-47 — PG INET / CIDR network overlap `&&`.
2341    /// True iff either network contains any address of the other.
2342    InetOverlap,
2343}
2344
2345#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2346pub enum UnOp {
2347    Not,
2348    Neg,
2349    /// Bitwise NOT `~` on integers.
2350    BitNot,
2351}
2352
2353// --- Display impls (round-trip-safe) --------------------------------------
2354
2355impl Statement {
2356    /// v7.18 — classify whether the statement is read-only at
2357    /// engine level. Used by `spg-sqlx`'s `SpgConnection` to
2358    /// route SELECT-shaped traffic through the fan-out
2359    /// `AsyncReadHandle` (no writer-lock contention) while
2360    /// keeping DML / DDL / TX-control on the single-writer path.
2361    ///
2362    /// The classification matches what
2363    /// `Engine::execute_readonly_with_cancel` accepts: anything
2364    /// that does NOT mutate catalog, statistics, session state,
2365    /// or transaction state. WaitForWalPosition is included
2366    /// (engine returns `Unsupported`, but the classification is
2367    /// semantically read-only — no mutation). Empty is excluded
2368    /// out of an abundance of caution — the no-op routes
2369    /// through the writer so any future side effect lands
2370    /// uniformly.
2371    ///
2372    /// **Not connection-state aware**. `SET LOCAL` / `RESET`
2373    /// affect session parameters and must run on the writer
2374    /// engine that owns the session state; they classify as
2375    /// writer-path here. Same for `BEGIN` / `COMMIT` /
2376    /// `ROLLBACK` / `SAVEPOINT` — transaction control is
2377    /// always writer-path.
2378    #[must_use]
2379    pub fn is_readonly(&self) -> bool {
2380        match self {
2381            Statement::Select(_)
2382            | Statement::Explain(_)
2383            | Statement::ShowTables
2384            | Statement::ShowDatabases
2385            | Statement::ShowCreateTable(_)
2386            | Statement::ShowIndexes(_)
2387            | Statement::ShowStatus
2388            | Statement::ShowVariables
2389            | Statement::ShowProcesslist
2390            | Statement::ShowColumns(_)
2391            | Statement::ShowUsers
2392            | Statement::ShowPublications
2393            | Statement::ShowSubscriptions
2394            | Statement::WaitForWalPosition { .. } => true,
2395            // Everything else mutates catalog, statistics,
2396            // session state, or transaction state — writer path.
2397            // Listed explicitly so a new Statement variant fails
2398            // the match exhaustiveness check and forces a
2399            // classification decision at add-site.
2400            Statement::Empty
2401            | Statement::DropTable { .. }
2402            | Statement::DropIndex { .. }
2403            | Statement::CreateTable(_)
2404            | Statement::CreateExtension(_)
2405            | Statement::DoBlock(_)
2406            | Statement::CreateIndex(_)
2407            | Statement::Insert(_)
2408            | Statement::Update(_)
2409            | Statement::Delete(_)
2410            | Statement::Merge(_)
2411            | Statement::Begin
2412            | Statement::Commit
2413            | Statement::Rollback
2414            | Statement::Savepoint(_)
2415            | Statement::RollbackToSavepoint(_)
2416            | Statement::ReleaseSavepoint(_)
2417            | Statement::CreateUser(_)
2418            | Statement::DropUser(_)
2419            | Statement::AlterIndex(_)
2420            | Statement::AlterTable(_)
2421            | Statement::CreatePublication(_)
2422            | Statement::DropPublication(_)
2423            | Statement::CreateSubscription(_)
2424            | Statement::DropSubscription(_)
2425            | Statement::Analyze(_)
2426            | Statement::CompactColdSegments
2427            | Statement::SetParameter { .. }
2428            | Statement::SetParameterList(_)
2429            | Statement::ResetParameter(_)
2430            | Statement::CreateFunction(_)
2431            | Statement::CreateTrigger(_)
2432            | Statement::DropTrigger { .. }
2433            | Statement::DropFunction { .. }
2434            | Statement::CreateSequence(_)
2435            | Statement::AlterSequence(_)
2436            | Statement::DropSequence { .. }
2437            | Statement::CreateView(_)
2438            | Statement::DropView { .. }
2439            | Statement::CreateMaterializedView(_)
2440            | Statement::RefreshMaterializedView { .. }
2441            | Statement::DropMaterializedView { .. }
2442            | Statement::CreateType(_)
2443            | Statement::DropType { .. }
2444            | Statement::CreateDomain(_)
2445            | Statement::DropDomain { .. }
2446            | Statement::CreateSchema { .. }
2447            | Statement::DropSchema { .. } => false,
2448        }
2449    }
2450}
2451
2452impl fmt::Display for Statement {
2453    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2454        match self {
2455            Self::Empty => Ok(()),
2456            Self::DropTable { names, if_exists } => {
2457                f.write_str("DROP TABLE ")?;
2458                if *if_exists {
2459                    f.write_str("IF EXISTS ")?;
2460                }
2461                for (i, n) in names.iter().enumerate() {
2462                    if i > 0 {
2463                        f.write_str(", ")?;
2464                    }
2465                    write!(f, "{}", quote_ident(n))?;
2466                }
2467                Ok(())
2468            }
2469            Self::DropIndex { name, if_exists } => {
2470                f.write_str("DROP INDEX ")?;
2471                if *if_exists {
2472                    f.write_str("IF EXISTS ")?;
2473                }
2474                write!(f, "{}", quote_ident(name))
2475            }
2476            Self::Select(s) => s.fmt(f),
2477            Self::CreateTable(s) => s.fmt(f),
2478            Self::CreateIndex(s) => s.fmt(f),
2479            Self::Insert(s) => s.fmt(f),
2480            Self::Update(s) => s.fmt(f),
2481            Self::Delete(s) => s.fmt(f),
2482            Self::Merge(s) => {
2483                // v7.17.0 Phase 3.P0-42 — MERGE display is approximate
2484                // (it round-trips for the cases tests cover, not for
2485                // round-tripping every edge of the surface).
2486                f.write_str("MERGE INTO ")?;
2487                write!(f, "{}", quote_ident(&s.target))?;
2488                if let Some(a) = &s.target_alias {
2489                    write!(f, " {}", quote_ident(a))?;
2490                }
2491                f.write_str(" USING ")?;
2492                write!(f, "{}", quote_ident(&s.source))?;
2493                if let Some(a) = &s.source_alias {
2494                    write!(f, " {}", quote_ident(a))?;
2495                }
2496                write!(f, " ON {}", s.on)?;
2497                for clause in &s.clauses {
2498                    f.write_str(" WHEN ")?;
2499                    f.write_str(match clause.matched {
2500                        MergeMatched::Matched => "MATCHED",
2501                        MergeMatched::NotMatched => "NOT MATCHED",
2502                    })?;
2503                    if let Some(c) = &clause.condition {
2504                        write!(f, " AND {c}")?;
2505                    }
2506                    f.write_str(" THEN ")?;
2507                    match &clause.action {
2508                        MergeAction::Insert { columns, values } => {
2509                            f.write_str("INSERT (")?;
2510                            for (i, c) in columns.iter().enumerate() {
2511                                if i > 0 {
2512                                    f.write_str(", ")?;
2513                                }
2514                                write!(f, "{}", quote_ident(c))?;
2515                            }
2516                            f.write_str(") VALUES (")?;
2517                            for (i, v) in values.iter().enumerate() {
2518                                if i > 0 {
2519                                    f.write_str(", ")?;
2520                                }
2521                                write!(f, "{v}")?;
2522                            }
2523                            f.write_str(")")?;
2524                        }
2525                        MergeAction::Update { assignments } => {
2526                            f.write_str("UPDATE SET ")?;
2527                            for (i, (c, e)) in assignments.iter().enumerate() {
2528                                if i > 0 {
2529                                    f.write_str(", ")?;
2530                                }
2531                                write!(f, "{} = {e}", quote_ident(c))?;
2532                            }
2533                        }
2534                        MergeAction::Delete => f.write_str("DELETE")?,
2535                        MergeAction::DoNothing => f.write_str("DO NOTHING")?,
2536                    }
2537                }
2538                Ok(())
2539            }
2540            Self::Begin => f.write_str("BEGIN"),
2541            Self::Commit => f.write_str("COMMIT"),
2542            Self::Rollback => f.write_str("ROLLBACK"),
2543            Self::Savepoint(n) => write!(f, "SAVEPOINT {}", quote_ident(n)),
2544            Self::RollbackToSavepoint(n) => write!(f, "ROLLBACK TO SAVEPOINT {}", quote_ident(n)),
2545            Self::ReleaseSavepoint(n) => write!(f, "RELEASE SAVEPOINT {}", quote_ident(n)),
2546            Self::ShowTables => f.write_str("SHOW TABLES"),
2547            Self::ShowDatabases => f.write_str("SHOW DATABASES"),
2548            Self::ShowCreateTable(t) => write!(f, "SHOW CREATE TABLE {}", quote_ident(t)),
2549            Self::ShowIndexes(t) => write!(f, "SHOW INDEXES FROM {}", quote_ident(t)),
2550            Self::ShowStatus => f.write_str("SHOW STATUS"),
2551            Self::ShowVariables => f.write_str("SHOW VARIABLES"),
2552            Self::ShowProcesslist => f.write_str("SHOW PROCESSLIST"),
2553            Self::ShowColumns(t) => write!(f, "SHOW COLUMNS FROM {}", quote_ident(t)),
2554            Self::CreateUser(s) => write!(
2555                f,
2556                "CREATE USER {} WITH PASSWORD '<redacted>' ROLE '{}'",
2557                quote_ident(&s.name),
2558                s.role
2559            ),
2560            Self::DropUser(n) => write!(f, "DROP USER {}", quote_ident(n)),
2561            Self::ShowUsers => f.write_str("SHOW USERS"),
2562            Self::ShowPublications => f.write_str("SHOW PUBLICATIONS"),
2563            Self::ShowSubscriptions => f.write_str("SHOW SUBSCRIPTIONS"),
2564            Self::CreateSubscription(s) => {
2565                write!(
2566                    f,
2567                    "CREATE SUBSCRIPTION {} CONNECTION '{}' PUBLICATION ",
2568                    quote_ident(&s.name),
2569                    s.conn_str.replace('\'', "''")
2570                )?;
2571                for (i, p) in s.publications.iter().enumerate() {
2572                    if i > 0 {
2573                        f.write_str(", ")?;
2574                    }
2575                    write!(f, "{}", quote_ident(p))?;
2576                }
2577                Ok(())
2578            }
2579            Self::DropSubscription(name) => {
2580                write!(f, "DROP SUBSCRIPTION {}", quote_ident(name))
2581            }
2582            Self::WaitForWalPosition { pos, timeout_ms } => {
2583                write!(f, "WAIT FOR WAL POSITION {pos}")?;
2584                if let Some(ms) = timeout_ms {
2585                    write!(f, " WITH TIMEOUT {ms}")?;
2586                }
2587                Ok(())
2588            }
2589            Self::Analyze(None) => f.write_str("ANALYZE"),
2590            Self::Analyze(Some(t)) => write!(f, "ANALYZE {}", quote_ident(t)),
2591            Self::CompactColdSegments => f.write_str("COMPACT COLD SEGMENTS"),
2592            Self::Explain(e) => {
2593                if e.suggest {
2594                    write!(f, "EXPLAIN (SUGGEST) {}", e.inner)
2595                } else if e.analyze {
2596                    write!(f, "EXPLAIN ANALYZE {}", e.inner)
2597                } else {
2598                    write!(f, "EXPLAIN {}", e.inner)
2599                }
2600            }
2601            Self::AlterIndex(a) => {
2602                write!(f, "ALTER INDEX ")?;
2603                match &a.target {
2604                    AlterIndexTarget::Rebuild { encoding } => {
2605                        write!(f, "{} REBUILD", quote_ident(&a.name))?;
2606                        if let Some(enc) = encoding {
2607                            write!(f, " WITH (encoding = {enc})")?;
2608                        }
2609                        Ok(())
2610                    }
2611                    AlterIndexTarget::Rename { new, if_exists } => {
2612                        if *if_exists {
2613                            f.write_str("IF EXISTS ")?;
2614                        }
2615                        write!(f, "{} RENAME TO {}", quote_ident(&a.name), quote_ident(new))
2616                    }
2617                }
2618            }
2619            Self::AlterTable(a) => {
2620                write!(f, "ALTER TABLE {} ", quote_ident(&a.name))?;
2621                for (i, t) in a.targets.iter().enumerate() {
2622                    if i > 0 {
2623                        f.write_str(", ")?;
2624                    }
2625                    fmt_alter_target(f, t)?;
2626                }
2627                Ok(())
2628            }
2629            Self::CreatePublication(p) => {
2630                write!(f, "CREATE PUBLICATION {}", quote_ident(&p.name))?;
2631                match &p.scope {
2632                    PublicationScope::AllTables => f.write_str(" FOR ALL TABLES"),
2633                    PublicationScope::ForTables(ts) => {
2634                        f.write_str(" FOR TABLE ")?;
2635                        for (i, t) in ts.iter().enumerate() {
2636                            if i > 0 {
2637                                f.write_str(", ")?;
2638                            }
2639                            write!(f, "{}", quote_ident(t))?;
2640                        }
2641                        Ok(())
2642                    }
2643                    PublicationScope::AllTablesExcept(ts) => {
2644                        f.write_str(" FOR ALL TABLES EXCEPT ")?;
2645                        for (i, t) in ts.iter().enumerate() {
2646                            if i > 0 {
2647                                f.write_str(", ")?;
2648                            }
2649                            write!(f, "{}", quote_ident(t))?;
2650                        }
2651                        Ok(())
2652                    }
2653                }
2654            }
2655            Self::CreateExtension(name) => {
2656                write!(f, "CREATE EXTENSION IF NOT EXISTS {}", quote_ident(name))
2657            }
2658            Self::DoBlock(body) => write!(f, "DO $$ {body} $$"),
2659            Self::DropPublication(name) => {
2660                write!(f, "DROP PUBLICATION {}", quote_ident(name))
2661            }
2662            Self::SetParameter { name, value } => {
2663                write!(f, "SET {name} = ")?;
2664                match value {
2665                    SetValue::String(s) => write!(f, "'{}'", s.replace('\'', "''")),
2666                    SetValue::Ident(s) | SetValue::Number(s) => f.write_str(s),
2667                    SetValue::Default => f.write_str("DEFAULT"),
2668                }
2669            }
2670            Self::SetParameterList(pairs) => {
2671                f.write_str("SET ")?;
2672                for (i, (name, value)) in pairs.iter().enumerate() {
2673                    if i > 0 {
2674                        f.write_str(", ")?;
2675                    }
2676                    write!(f, "{name} = ")?;
2677                    match value {
2678                        SetValue::String(s) => write!(f, "'{}'", s.replace('\'', "''"))?,
2679                        SetValue::Ident(s) | SetValue::Number(s) => f.write_str(s)?,
2680                        SetValue::Default => f.write_str("DEFAULT")?,
2681                    }
2682                }
2683                Ok(())
2684            }
2685            Self::ResetParameter(None) => f.write_str("RESET ALL"),
2686            Self::ResetParameter(Some(name)) => write!(f, "RESET {name}"),
2687            Self::CreateFunction(s) => s.fmt(f),
2688            Self::CreateTrigger(s) => s.fmt(f),
2689            Self::DropTrigger {
2690                name,
2691                table,
2692                if_exists,
2693            } => {
2694                f.write_str("DROP TRIGGER ")?;
2695                if *if_exists {
2696                    f.write_str("IF EXISTS ")?;
2697                }
2698                write!(f, "{} ON {}", quote_ident(name), quote_ident(table))
2699            }
2700            Self::DropFunction { name, if_exists } => {
2701                f.write_str("DROP FUNCTION ")?;
2702                if *if_exists {
2703                    f.write_str("IF EXISTS ")?;
2704                }
2705                write!(f, "{}", quote_ident(name))
2706            }
2707            Self::CreateSequence(s) => s.fmt(f),
2708            Self::AlterSequence(s) => s.fmt(f),
2709            Self::DropSequence { names, if_exists } => {
2710                f.write_str("DROP SEQUENCE ")?;
2711                if *if_exists {
2712                    f.write_str("IF EXISTS ")?;
2713                }
2714                for (i, n) in names.iter().enumerate() {
2715                    if i > 0 {
2716                        f.write_str(", ")?;
2717                    }
2718                    write!(f, "{}", quote_ident(n))?;
2719                }
2720                Ok(())
2721            }
2722            Self::CreateView(v) => v.fmt(f),
2723            Self::DropView { names, if_exists } => {
2724                f.write_str("DROP VIEW ")?;
2725                if *if_exists {
2726                    f.write_str("IF EXISTS ")?;
2727                }
2728                for (i, n) in names.iter().enumerate() {
2729                    if i > 0 {
2730                        f.write_str(", ")?;
2731                    }
2732                    write!(f, "{}", quote_ident(n))?;
2733                }
2734                Ok(())
2735            }
2736            Self::CreateMaterializedView(v) => v.fmt(f),
2737            Self::RefreshMaterializedView { name, with_data } => {
2738                write!(f, "REFRESH MATERIALIZED VIEW {}", quote_ident(name))?;
2739                if !*with_data {
2740                    f.write_str(" WITH NO DATA")?;
2741                }
2742                Ok(())
2743            }
2744            Self::DropMaterializedView { names, if_exists } => {
2745                f.write_str("DROP MATERIALIZED VIEW ")?;
2746                if *if_exists {
2747                    f.write_str("IF EXISTS ")?;
2748                }
2749                for (i, n) in names.iter().enumerate() {
2750                    if i > 0 {
2751                        f.write_str(", ")?;
2752                    }
2753                    write!(f, "{}", quote_ident(n))?;
2754                }
2755                Ok(())
2756            }
2757            Self::CreateType(t) => t.fmt(f),
2758            Self::DropType { names, if_exists } => {
2759                f.write_str("DROP TYPE ")?;
2760                if *if_exists {
2761                    f.write_str("IF EXISTS ")?;
2762                }
2763                for (i, n) in names.iter().enumerate() {
2764                    if i > 0 {
2765                        f.write_str(", ")?;
2766                    }
2767                    write!(f, "{}", quote_ident(n))?;
2768                }
2769                Ok(())
2770            }
2771            Self::CreateDomain(d) => d.fmt(f),
2772            Self::DropDomain { names, if_exists } => {
2773                f.write_str("DROP DOMAIN ")?;
2774                if *if_exists {
2775                    f.write_str("IF EXISTS ")?;
2776                }
2777                for (i, n) in names.iter().enumerate() {
2778                    if i > 0 {
2779                        f.write_str(", ")?;
2780                    }
2781                    write!(f, "{}", quote_ident(n))?;
2782                }
2783                Ok(())
2784            }
2785            Self::CreateSchema {
2786                name,
2787                if_not_exists,
2788            } => {
2789                f.write_str("CREATE SCHEMA ")?;
2790                if *if_not_exists {
2791                    f.write_str("IF NOT EXISTS ")?;
2792                }
2793                write!(f, "{}", quote_ident(name))
2794            }
2795            Self::DropSchema { names, if_exists } => {
2796                f.write_str("DROP SCHEMA ")?;
2797                if *if_exists {
2798                    f.write_str("IF EXISTS ")?;
2799                }
2800                for (i, n) in names.iter().enumerate() {
2801                    if i > 0 {
2802                        f.write_str(", ")?;
2803                    }
2804                    write!(f, "{}", quote_ident(n))?;
2805                }
2806                Ok(())
2807            }
2808        }
2809    }
2810}
2811
2812impl fmt::Display for CreateDomainStatement {
2813    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2814        write!(
2815            f,
2816            "CREATE DOMAIN {} AS {}",
2817            quote_ident(&self.name),
2818            self.base_type
2819        )?;
2820        if let Some(d) = &self.default {
2821            write!(f, " DEFAULT {d}")?;
2822        }
2823        if self.not_null {
2824            f.write_str(" NOT NULL")?;
2825        }
2826        for c in &self.checks {
2827            write!(f, " CHECK ({c})")?;
2828        }
2829        Ok(())
2830    }
2831}
2832
2833impl fmt::Display for CreateTypeStatement {
2834    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2835        write!(f, "CREATE TYPE {} AS ", quote_ident(&self.name))?;
2836        match &self.kind {
2837            TypeKind::Enum { labels } => {
2838                f.write_str("ENUM (")?;
2839                for (i, l) in labels.iter().enumerate() {
2840                    if i > 0 {
2841                        f.write_str(", ")?;
2842                    }
2843                    write!(f, "'{}'", l.replace('\'', "''"))?;
2844                }
2845                f.write_str(")")
2846            }
2847        }
2848    }
2849}
2850
2851impl fmt::Display for CreateMaterializedViewStatement {
2852    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2853        f.write_str("CREATE MATERIALIZED VIEW ")?;
2854        if self.if_not_exists {
2855            f.write_str("IF NOT EXISTS ")?;
2856        }
2857        write!(f, "{}", quote_ident(&self.name))?;
2858        if !self.columns.is_empty() {
2859            f.write_str(" (")?;
2860            for (i, c) in self.columns.iter().enumerate() {
2861                if i > 0 {
2862                    f.write_str(", ")?;
2863                }
2864                write!(f, "{}", quote_ident(c))?;
2865            }
2866            f.write_str(")")?;
2867        }
2868        write!(f, " AS {}", self.body)?;
2869        if !self.with_data {
2870            f.write_str(" WITH NO DATA")?;
2871        }
2872        Ok(())
2873    }
2874}
2875
2876impl fmt::Display for CreateViewStatement {
2877    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2878        f.write_str("CREATE ")?;
2879        if self.or_replace {
2880            f.write_str("OR REPLACE ")?;
2881        }
2882        if self.temporary {
2883            f.write_str("TEMPORARY ")?;
2884        }
2885        f.write_str("VIEW ")?;
2886        if self.if_not_exists {
2887            f.write_str("IF NOT EXISTS ")?;
2888        }
2889        write!(f, "{}", quote_ident(&self.name))?;
2890        if !self.columns.is_empty() {
2891            f.write_str(" (")?;
2892            for (i, c) in self.columns.iter().enumerate() {
2893                if i > 0 {
2894                    f.write_str(", ")?;
2895                }
2896                write!(f, "{}", quote_ident(c))?;
2897            }
2898            f.write_str(")")?;
2899        }
2900        write!(f, " AS {}", self.body)
2901    }
2902}
2903
2904impl fmt::Display for CreateSequenceStatement {
2905    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2906        f.write_str("CREATE ")?;
2907        if self.temporary {
2908            f.write_str("TEMPORARY ")?;
2909        }
2910        f.write_str("SEQUENCE ")?;
2911        if self.if_not_exists {
2912            f.write_str("IF NOT EXISTS ")?;
2913        }
2914        write!(f, "{}", quote_ident(&self.name))?;
2915        if let Some(dt) = self.data_type {
2916            write!(f, " AS {dt}")?;
2917        }
2918        write_sequence_options(f, &self.options)
2919    }
2920}
2921
2922impl fmt::Display for AlterSequenceStatement {
2923    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2924        f.write_str("ALTER SEQUENCE ")?;
2925        if self.if_exists {
2926            f.write_str("IF EXISTS ")?;
2927        }
2928        write!(f, "{}", quote_ident(&self.name))?;
2929        write_sequence_options(f, &self.options)
2930    }
2931}
2932
2933impl fmt::Display for SequenceDataType {
2934    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2935        f.write_str(match self {
2936            Self::SmallInt => "smallint",
2937            Self::Int => "integer",
2938            Self::BigInt => "bigint",
2939        })
2940    }
2941}
2942
2943fn write_sequence_options(f: &mut fmt::Formatter<'_>, o: &SequenceOptions) -> fmt::Result {
2944    if let Some(n) = o.increment {
2945        write!(f, " INCREMENT BY {n}")?;
2946    }
2947    match o.min_value {
2948        Some(SeqBound::Value(n)) => write!(f, " MINVALUE {n}")?,
2949        Some(SeqBound::NoBound) => f.write_str(" NO MINVALUE")?,
2950        None => {}
2951    }
2952    match o.max_value {
2953        Some(SeqBound::Value(n)) => write!(f, " MAXVALUE {n}")?,
2954        Some(SeqBound::NoBound) => f.write_str(" NO MAXVALUE")?,
2955        None => {}
2956    }
2957    if let Some(n) = o.start {
2958        write!(f, " START WITH {n}")?;
2959    }
2960    match o.restart {
2961        Some(Some(n)) => write!(f, " RESTART WITH {n}")?,
2962        Some(None) => f.write_str(" RESTART")?,
2963        None => {}
2964    }
2965    if let Some(n) = o.cache {
2966        write!(f, " CACHE {n}")?;
2967    }
2968    match o.cycle {
2969        Some(true) => f.write_str(" CYCLE")?,
2970        Some(false) => f.write_str(" NO CYCLE")?,
2971        None => {}
2972    }
2973    if let Some(ob) = &o.owned_by {
2974        match ob {
2975            SequenceOwnedBy::None => f.write_str(" OWNED BY NONE")?,
2976            SequenceOwnedBy::Column { table, column } => {
2977                write!(
2978                    f,
2979                    " OWNED BY {}.{}",
2980                    quote_ident(table),
2981                    quote_ident(column)
2982                )?;
2983            }
2984        }
2985    }
2986    Ok(())
2987}
2988
2989impl fmt::Display for CreateFunctionStatement {
2990    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2991        f.write_str("CREATE ")?;
2992        if self.or_replace {
2993            f.write_str("OR REPLACE ")?;
2994        }
2995        write!(f, "FUNCTION {}(", quote_ident(&self.name))?;
2996        for (i, arg) in self.args.iter().enumerate() {
2997            if i > 0 {
2998                f.write_str(", ")?;
2999            }
3000            match arg.mode {
3001                FunctionArgMode::In => {}
3002                FunctionArgMode::Out => f.write_str("OUT ")?,
3003                FunctionArgMode::InOut => f.write_str("INOUT ")?,
3004            }
3005            if let Some(name) = &arg.name {
3006                write!(f, "{} ", quote_ident(name))?;
3007            }
3008            match &arg.ty {
3009                FunctionArgType::Typed(t) => write!(f, "{t}")?,
3010                FunctionArgType::Raw(s) => f.write_str(s)?,
3011            }
3012        }
3013        f.write_str(") RETURNS ")?;
3014        match &self.returns {
3015            FunctionReturn::Trigger => f.write_str("TRIGGER")?,
3016            FunctionReturn::Void => f.write_str("VOID")?,
3017            FunctionReturn::Type(t) => write!(f, "{t}")?,
3018            FunctionReturn::Other(s) => f.write_str(s)?,
3019        }
3020        write!(f, " LANGUAGE {} AS $$", self.language)?;
3021        match &self.body {
3022            FunctionBody::PlPgSql(b) => write!(f, "\n{b}\n")?,
3023            FunctionBody::Raw(s) => f.write_str(s)?,
3024        }
3025        f.write_str("$$")
3026    }
3027}
3028
3029impl fmt::Display for PlPgSqlBlock {
3030    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3031        if !self.declarations.is_empty() {
3032            f.write_str("DECLARE\n")?;
3033            for d in &self.declarations {
3034                write!(f, "  {} ", quote_ident(&d.name))?;
3035                match &d.ty {
3036                    FunctionArgType::Typed(t) => write!(f, "{t}")?,
3037                    FunctionArgType::Raw(s) => f.write_str(s)?,
3038                }
3039                if let Some(e) = &d.default {
3040                    write!(f, " := {e}")?;
3041                }
3042                f.write_str(";\n")?;
3043            }
3044        }
3045        f.write_str("BEGIN\n")?;
3046        for stmt in &self.statements {
3047            writeln!(f, "  {stmt};")?;
3048        }
3049        f.write_str("END")
3050    }
3051}
3052
3053impl fmt::Display for PlPgSqlStmt {
3054    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3055        match self {
3056            Self::Assign { target, value } => write!(f, "{target} := {value}"),
3057            Self::SelectInto { var, body } => write!(f, "{body} INTO {var}"),
3058            Self::Return(t) => match t {
3059                ReturnTarget::New => f.write_str("RETURN NEW"),
3060                ReturnTarget::Old => f.write_str("RETURN OLD"),
3061                ReturnTarget::Null => f.write_str("RETURN NULL"),
3062                ReturnTarget::Expr(e) => write!(f, "RETURN {e}"),
3063            },
3064            Self::If {
3065                branches,
3066                else_branch,
3067            } => {
3068                for (i, (cond, body)) in branches.iter().enumerate() {
3069                    if i == 0 {
3070                        write!(f, "IF {cond} THEN ")?;
3071                    } else {
3072                        write!(f, " ELSIF {cond} THEN ")?;
3073                    }
3074                    for (j, s) in body.iter().enumerate() {
3075                        if j > 0 {
3076                            f.write_str("; ")?;
3077                        }
3078                        write!(f, "{s}")?;
3079                    }
3080                }
3081                if !else_branch.is_empty() {
3082                    f.write_str(" ELSE ")?;
3083                    for (j, s) in else_branch.iter().enumerate() {
3084                        if j > 0 {
3085                            f.write_str("; ")?;
3086                        }
3087                        write!(f, "{s}")?;
3088                    }
3089                }
3090                f.write_str(" END IF")
3091            }
3092            Self::Raise {
3093                level,
3094                message,
3095                args,
3096            } => {
3097                let lvl = match level {
3098                    RaiseLevel::Notice => "NOTICE",
3099                    RaiseLevel::Warning => "WARNING",
3100                    RaiseLevel::Info => "INFO",
3101                    RaiseLevel::Log => "LOG",
3102                    RaiseLevel::Debug => "DEBUG",
3103                    RaiseLevel::Exception => "EXCEPTION",
3104                };
3105                write!(f, "RAISE {lvl} '{}'", message.replace('\'', "''"))?;
3106                for a in args {
3107                    write!(f, ", {a}")?;
3108                }
3109                Ok(())
3110            }
3111            Self::EmbeddedSql(s) => write!(f, "{s}"),
3112        }
3113    }
3114}
3115
3116impl fmt::Display for AssignTarget {
3117    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3118        match self {
3119            Self::NewColumn(c) => write!(f, "NEW.{}", quote_ident(c)),
3120            Self::OldColumn(c) => write!(f, "OLD.{}", quote_ident(c)),
3121            Self::Local(n) => f.write_str(n),
3122        }
3123    }
3124}
3125
3126impl fmt::Display for CreateTriggerStatement {
3127    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3128        f.write_str("CREATE ")?;
3129        if self.or_replace {
3130            f.write_str("OR REPLACE ")?;
3131        }
3132        write!(f, "TRIGGER {} ", quote_ident(&self.name))?;
3133        match self.timing {
3134            TriggerTiming::Before => f.write_str("BEFORE")?,
3135            TriggerTiming::After => f.write_str("AFTER")?,
3136            TriggerTiming::InsteadOf => f.write_str("INSTEAD OF")?,
3137        }
3138        for (i, e) in self.events.iter().enumerate() {
3139            if i == 0 {
3140                f.write_str(" ")?;
3141            } else {
3142                f.write_str(" OR ")?;
3143            }
3144            match e {
3145                TriggerEvent::Insert => f.write_str("INSERT")?,
3146                TriggerEvent::Update => {
3147                    f.write_str("UPDATE")?;
3148                    if !self.update_columns.is_empty() {
3149                        f.write_str(" OF ")?;
3150                        for (j, col) in self.update_columns.iter().enumerate() {
3151                            if j > 0 {
3152                                f.write_str(", ")?;
3153                            }
3154                            f.write_str(&quote_ident(col))?;
3155                        }
3156                    }
3157                }
3158                TriggerEvent::Delete => f.write_str("DELETE")?,
3159                TriggerEvent::Truncate => f.write_str("TRUNCATE")?,
3160            }
3161        }
3162        write!(f, " ON {} FOR EACH ", quote_ident(&self.table))?;
3163        match self.for_each {
3164            TriggerForEach::Row => f.write_str("ROW")?,
3165            TriggerForEach::Statement => f.write_str("STATEMENT")?,
3166        }
3167        write!(f, " EXECUTE FUNCTION {}()", quote_ident(&self.function))
3168    }
3169}
3170
3171impl fmt::Display for CreateIndexStatement {
3172    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3173        if self.is_unique {
3174            f.write_str("CREATE UNIQUE INDEX ")?;
3175        } else {
3176            f.write_str("CREATE INDEX ")?;
3177        }
3178        if self.if_not_exists {
3179            f.write_str("IF NOT EXISTS ")?;
3180        }
3181        write!(
3182            f,
3183            "{} ON {} ",
3184            quote_ident(&self.name),
3185            quote_ident(&self.table)
3186        )?;
3187        match self.method {
3188            IndexMethod::Hnsw => f.write_str("USING hnsw ")?,
3189            IndexMethod::Brin => f.write_str("USING brin ")?,
3190            IndexMethod::Gin => f.write_str("USING gin ")?,
3191            IndexMethod::BTree => {}
3192        }
3193        if let Some(expr) = &self.expression {
3194            write!(f, "({})", expr)?;
3195        } else if self.extra_columns.is_empty() {
3196            // v7.15.0 — preserve operator class on round-trip
3197            // (`(col opclass)`) so WAL replay reconstructs the
3198            // engine-routing intent (e.g. `gin_trgm_ops` →
3199            // trigram-GIN build path).
3200            if let Some(op) = &self.opclass {
3201                write!(f, "({} {})", quote_ident(&self.column), op)?;
3202            } else {
3203                write!(f, "({})", quote_ident(&self.column))?;
3204            }
3205        } else {
3206            // v7.9.14 — multi-column key. Emit each column quoted
3207            // so the round-tripped form re-parses to identical AST.
3208            f.write_str("(")?;
3209            write!(f, "{}", quote_ident(&self.column))?;
3210            for c in &self.extra_columns {
3211                write!(f, ", {}", quote_ident(c))?;
3212            }
3213            f.write_str(")")?;
3214        }
3215        if !self.included_columns.is_empty() {
3216            f.write_str(" INCLUDE (")?;
3217            for (i, c) in self.included_columns.iter().enumerate() {
3218                if i > 0 {
3219                    f.write_str(", ")?;
3220                }
3221                write!(f, "{}", quote_ident(c))?;
3222            }
3223            f.write_str(")")?;
3224        }
3225        if let Some(pred) = &self.partial_predicate {
3226            write!(f, " WHERE {}", pred)?;
3227        }
3228        Ok(())
3229    }
3230}
3231
3232impl fmt::Display for CreateTableStatement {
3233    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3234        f.write_str("CREATE TABLE ")?;
3235        if self.if_not_exists {
3236            f.write_str("IF NOT EXISTS ")?;
3237        }
3238        write!(f, "{} (", quote_ident(&self.name))?;
3239        for (i, col) in self.columns.iter().enumerate() {
3240            if i > 0 {
3241                f.write_str(", ")?;
3242            }
3243            write!(f, "{col}")?;
3244        }
3245        // v7.6.0 — render FK constraints in table-level form, after
3246        // the column list. WAL replay round-trips through Display, so
3247        // every FK must serialise here for replay to reconstruct the
3248        // schema bit-for-bit.
3249        for fk in &self.foreign_keys {
3250            f.write_str(", ")?;
3251            write!(f, "{fk}")?;
3252        }
3253        // v7.13.0 — render table-level constraints (PRIMARY KEY /
3254        // UNIQUE / CHECK) so WAL replay reconstructs them. Inline
3255        // column-level UNIQUE / CHECK get lifted to this list at
3256        // parse time, so emitting only here avoids double-counting.
3257        for tc in &self.table_constraints {
3258            f.write_str(", ")?;
3259            write!(f, "{tc}")?;
3260        }
3261        f.write_str(")")
3262    }
3263}
3264
3265fn fmt_alter_target(f: &mut fmt::Formatter<'_>, t: &AlterTableTarget) -> fmt::Result {
3266    match t {
3267        AlterTableTarget::SetHotTierBytes(n) => {
3268            write!(f, "SET hot_tier_bytes = {n}")
3269        }
3270        AlterTableTarget::AddForeignKey(fk) => write!(f, "ADD {fk}"),
3271        AlterTableTarget::DropForeignKey { name, if_exists } => {
3272            f.write_str("DROP CONSTRAINT ")?;
3273            if *if_exists {
3274                f.write_str("IF EXISTS ")?;
3275            }
3276            write!(f, "{}", quote_ident(name))
3277        }
3278        AlterTableTarget::AddColumn {
3279            column,
3280            if_not_exists,
3281        } => {
3282            f.write_str("ADD COLUMN ")?;
3283            if *if_not_exists {
3284                f.write_str("IF NOT EXISTS ")?;
3285            }
3286            write!(f, "{} {}", quote_ident(&column.name), column.ty)?;
3287            if !column.nullable {
3288                f.write_str(" NOT NULL")?;
3289            }
3290            if let Some(d) = &column.default {
3291                write!(f, " DEFAULT {d}")?;
3292            }
3293            if column.auto_increment {
3294                f.write_str(" AUTO_INCREMENT")?;
3295            }
3296            if column.is_primary_key {
3297                f.write_str(" PRIMARY KEY")?;
3298            }
3299            Ok(())
3300        }
3301        AlterTableTarget::AlterColumnType {
3302            column,
3303            new_type,
3304            using,
3305        } => {
3306            write!(f, "ALTER COLUMN {} TYPE {new_type}", quote_ident(column))?;
3307            if let Some(u) = using {
3308                write!(f, " USING {u}")?;
3309            }
3310            Ok(())
3311        }
3312        AlterTableTarget::DropColumn {
3313            column,
3314            if_exists,
3315            cascade,
3316        } => {
3317            f.write_str("DROP COLUMN ")?;
3318            if *if_exists {
3319                f.write_str("IF EXISTS ")?;
3320            }
3321            write!(f, "{}", quote_ident(column))?;
3322            if *cascade {
3323                f.write_str(" CASCADE")?;
3324            }
3325            Ok(())
3326        }
3327        AlterTableTarget::AddTableConstraint(tc) => {
3328            write!(f, "ADD {tc}")
3329        }
3330        AlterTableTarget::SetColumnAutoIncrement { column, seq_name } => {
3331            // Round-trip-safe spelling: re-parsing this form lowers
3332            // back to SetColumnAutoIncrement (the nextval default is
3333            // how pg_dump says "serial").
3334            let seq = seq_name
3335                .clone()
3336                .unwrap_or_else(|| alloc::format!("{column}_seq"));
3337            write!(
3338                f,
3339                "ALTER COLUMN {} SET DEFAULT nextval('{seq}')",
3340                quote_ident(column)
3341            )
3342        }
3343        AlterTableTarget::RenameColumn { old, new } => {
3344            write!(
3345                f,
3346                "RENAME COLUMN {} TO {}",
3347                quote_ident(old),
3348                quote_ident(new)
3349            )
3350        }
3351        AlterTableTarget::RenameTable { new } => {
3352            write!(f, "RENAME TO {}", quote_ident(new))
3353        }
3354        AlterTableTarget::SetTriggerEnabled { which, enabled } => {
3355            f.write_str(if *enabled {
3356                "ENABLE TRIGGER "
3357            } else {
3358                "DISABLE TRIGGER "
3359            })?;
3360            match which {
3361                TriggerSelector::All => f.write_str("ALL"),
3362                TriggerSelector::Named(n) => f.write_str(&quote_ident(n)),
3363            }
3364        }
3365    }
3366}
3367
3368impl fmt::Display for TableConstraint {
3369    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3370        match self {
3371            Self::PrimaryKey { name, columns } => {
3372                if let Some(n) = name {
3373                    write!(f, "CONSTRAINT {} ", quote_ident(n))?;
3374                }
3375                f.write_str("PRIMARY KEY (")?;
3376                for (i, c) in columns.iter().enumerate() {
3377                    if i > 0 {
3378                        f.write_str(", ")?;
3379                    }
3380                    f.write_str(&quote_ident(c))?;
3381                }
3382                f.write_str(")")
3383            }
3384            Self::Unique {
3385                name,
3386                columns,
3387                nulls_not_distinct,
3388            } => {
3389                if let Some(n) = name {
3390                    write!(f, "CONSTRAINT {} ", quote_ident(n))?;
3391                }
3392                f.write_str("UNIQUE ")?;
3393                if *nulls_not_distinct {
3394                    f.write_str("NULLS NOT DISTINCT ")?;
3395                }
3396                f.write_str("(")?;
3397                for (i, c) in columns.iter().enumerate() {
3398                    if i > 0 {
3399                        f.write_str(", ")?;
3400                    }
3401                    f.write_str(&quote_ident(c))?;
3402                }
3403                f.write_str(")")
3404            }
3405            Self::Check { name, expr } => {
3406                if let Some(n) = name {
3407                    write!(f, "CONSTRAINT {} ", quote_ident(n))?;
3408                }
3409                write!(f, "CHECK ({expr})")
3410            }
3411            Self::Index { name, columns } => {
3412                f.write_str("KEY ")?;
3413                if let Some(n) = name {
3414                    write!(f, "{} ", quote_ident(n))?;
3415                }
3416                f.write_str("(")?;
3417                for (i, c) in columns.iter().enumerate() {
3418                    if i > 0 {
3419                        f.write_str(", ")?;
3420                    }
3421                    f.write_str(&quote_ident(c))?;
3422                }
3423                f.write_str(")")
3424            }
3425            Self::FulltextIndex { name, columns } => {
3426                // Mysqldump emits `FULLTEXT KEY name (cols)` —
3427                // Display rounds back to that shape so dump
3428                // replay reproduces the input verbatim.
3429                f.write_str("FULLTEXT KEY ")?;
3430                if let Some(n) = name {
3431                    write!(f, "{} ", quote_ident(n))?;
3432                }
3433                f.write_str("(")?;
3434                for (i, c) in columns.iter().enumerate() {
3435                    if i > 0 {
3436                        f.write_str(", ")?;
3437                    }
3438                    f.write_str(&quote_ident(c))?;
3439                }
3440                f.write_str(")")
3441            }
3442        }
3443    }
3444}
3445
3446impl fmt::Display for ForeignKeyConstraint {
3447    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3448        if let Some(name) = &self.name {
3449            write!(f, "CONSTRAINT {} ", quote_ident(name))?;
3450        }
3451        f.write_str("FOREIGN KEY (")?;
3452        for (i, c) in self.columns.iter().enumerate() {
3453            if i > 0 {
3454                f.write_str(", ")?;
3455            }
3456            f.write_str(&quote_ident(c))?;
3457        }
3458        write!(f, ") REFERENCES {}", quote_ident(&self.parent_table))?;
3459        if !self.parent_columns.is_empty() {
3460            f.write_str(" (")?;
3461            for (i, c) in self.parent_columns.iter().enumerate() {
3462                if i > 0 {
3463                    f.write_str(", ")?;
3464                }
3465                f.write_str(&quote_ident(c))?;
3466            }
3467            f.write_str(")")?;
3468        }
3469        // Only render non-default actions to keep Display output
3470        // close to user input. SPG's default is RESTRICT (matches
3471        // SQL spec).
3472        if self.on_delete != FkAction::Restrict {
3473            write!(f, " ON DELETE {}", self.on_delete)?;
3474        }
3475        if self.on_update != FkAction::Restrict {
3476            write!(f, " ON UPDATE {}", self.on_update)?;
3477        }
3478        Ok(())
3479    }
3480}
3481
3482impl fmt::Display for FkAction {
3483    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3484        match self {
3485            Self::Restrict => f.write_str("RESTRICT"),
3486            Self::Cascade => f.write_str("CASCADE"),
3487            Self::SetNull => f.write_str("SET NULL"),
3488            Self::SetDefault => f.write_str("SET DEFAULT"),
3489            Self::NoAction => f.write_str("NO ACTION"),
3490        }
3491    }
3492}
3493
3494impl fmt::Display for ColumnDef {
3495    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3496        // v7.30.1 (mailrs round-24 class audit) — the type position
3497        // must re-parse to the same ColumnDef: a user-defined type
3498        // reference and the MySQL inline ENUM / SET value lists all
3499        // lower `ty` to Text, so rendering `ty` lost them.
3500        write!(f, "{}", quote_ident(&self.name))?;
3501        if let Some(ut) = &self.user_type_ref {
3502            write!(f, " {}", quote_ident(ut))?;
3503        } else if let Some(variants) = &self.inline_enum_variants {
3504            write_variant_list(f, "ENUM", variants)?;
3505        } else if let Some(variants) = &self.inline_set_variants {
3506            write_variant_list(f, "SET", variants)?;
3507        } else {
3508            write!(f, " {}", self.ty)?;
3509        }
3510        if self.is_unsigned {
3511            f.write_str(" UNSIGNED")?;
3512        }
3513        // v7.17.0 Phase 2.5 — render COLLATE for round-trippable
3514        // DDL. Only emits when non-default so the typical output
3515        // stays unchanged.
3516        match self.collation {
3517            Collation::Binary => {}
3518            Collation::CaseInsensitive => f.write_str(" COLLATE \"case_insensitive\"")?,
3519        }
3520        if let Some(d) = &self.default {
3521            write!(f, " DEFAULT {d}")?;
3522        }
3523        if self.auto_increment {
3524            f.write_str(" AUTO_INCREMENT")?;
3525        }
3526        if !self.nullable {
3527            f.write_str(" NOT NULL")?;
3528        }
3529        // v7.30.1 (mailrs round-24 class audit) — inline PRIMARY KEY
3530        // is NOT lifted to a table-level constraint at parse time
3531        // (unlike UNIQUE / CHECK), so the WAL round trip of a
3532        // prepared CREATE TABLE silently dropped the primary key.
3533        if self.is_primary_key {
3534            f.write_str(" PRIMARY KEY")?;
3535        }
3536        // The parser accepts only CURRENT_TIMESTAMP here (stored as
3537        // now()), so that spelling is the lossless round trip.
3538        if self.on_update_runtime.is_some() {
3539            f.write_str(" ON UPDATE CURRENT_TIMESTAMP")?;
3540        }
3541        Ok(())
3542    }
3543}
3544
3545/// v7.30.1 — `ENUM('a', 'b')` / `SET('a', 'b')` inline value-list
3546/// types (MySQL flavour; `ty` is Text underneath).
3547fn write_variant_list(f: &mut fmt::Formatter<'_>, kw: &str, variants: &[String]) -> fmt::Result {
3548    write!(f, " {kw}(")?;
3549    for (i, v) in variants.iter().enumerate() {
3550        if i > 0 {
3551            f.write_str(", ")?;
3552        }
3553        write!(f, "'{}'", v.replace('\'', "''"))?;
3554    }
3555    f.write_str(")")
3556}
3557
3558impl fmt::Display for InsertStatement {
3559    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3560        write!(f, "INSERT INTO {}", quote_ident(&self.table))?;
3561        if let Some(cols) = &self.columns {
3562            f.write_str(" (")?;
3563            for (i, c) in cols.iter().enumerate() {
3564                if i > 0 {
3565                    f.write_str(", ")?;
3566                }
3567                f.write_str(&quote_ident(c))?;
3568            }
3569            f.write_str(")")?;
3570        }
3571        // v7.13.0 — INSERT…SELECT renders as `... SELECT …`,
3572        // skipping the VALUES list (mailrs round-5 G4).
3573        if let Some(sel) = &self.select_source {
3574            write!(f, " {sel}")?;
3575        } else {
3576            f.write_str(" VALUES ")?;
3577            for (ri, row) in self.rows.iter().enumerate() {
3578                if ri > 0 {
3579                    f.write_str(", ")?;
3580                }
3581                f.write_str("(")?;
3582                for (i, v) in row.iter().enumerate() {
3583                    if i > 0 {
3584                        f.write_str(", ")?;
3585                    }
3586                    write!(f, "{v}")?;
3587                }
3588                f.write_str(")")?;
3589            }
3590        }
3591        // v7.30.1 (mailrs round-24) — ON CONFLICT must survive the
3592        // Display round trip: WAL persistence renders the bind-final
3593        // AST through this impl, and a replayed bare INSERT turns a
3594        // legal upsert no-op into a UNIQUE violation that refuses to
3595        // open the catalog.
3596        if let Some(oc) = &self.on_conflict {
3597            write!(f, " {oc}")?;
3598        }
3599        write_returning(self.returning.as_deref(), f)?;
3600        Ok(())
3601    }
3602}
3603
3604/// v7.30.1 (mailrs round-24) — render the ON CONFLICT clause the
3605/// parser produced, so the AST→SQL round trip preserves upsert
3606/// semantics (WAL replay depends on it).
3607impl fmt::Display for OnConflictClause {
3608    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3609        f.write_str("ON CONFLICT")?;
3610        if !self.target_columns.is_empty() {
3611            f.write_str(" (")?;
3612            for (i, c) in self.target_columns.iter().enumerate() {
3613                if i > 0 {
3614                    f.write_str(", ")?;
3615                }
3616                f.write_str(&quote_ident(c))?;
3617            }
3618            f.write_str(")")?;
3619        }
3620        match &self.action {
3621            OnConflictAction::Nothing => f.write_str(" DO NOTHING"),
3622            OnConflictAction::Update {
3623                assignments,
3624                where_,
3625            } => {
3626                f.write_str(" DO UPDATE SET ")?;
3627                for (i, (col, expr)) in assignments.iter().enumerate() {
3628                    if i > 0 {
3629                        f.write_str(", ")?;
3630                    }
3631                    write!(f, "{} = {expr}", quote_ident(col))?;
3632                }
3633                if let Some(w) = where_ {
3634                    write!(f, " WHERE {w}")?;
3635                }
3636                Ok(())
3637            }
3638        }
3639    }
3640}
3641
3642/// v7.30.1 (mailrs round-24) — shared `RETURNING <projection>`
3643/// tail for the three DML Display impls.
3644fn write_returning(ret: Option<&[SelectItem]>, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3645    let Some(items) = ret else {
3646        return Ok(());
3647    };
3648    f.write_str(" RETURNING ")?;
3649    for (i, item) in items.iter().enumerate() {
3650        if i > 0 {
3651            f.write_str(", ")?;
3652        }
3653        write!(f, "{item}")?;
3654    }
3655    Ok(())
3656}
3657
3658impl fmt::Display for UpdateStatement {
3659    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3660        write!(f, "UPDATE {} SET ", quote_ident(&self.table))?;
3661        for (i, (col, expr)) in self.assignments.iter().enumerate() {
3662            if i > 0 {
3663                f.write_str(", ")?;
3664            }
3665            write!(f, "{} = {expr}", quote_ident(col))?;
3666        }
3667        if let Some(w) = &self.where_ {
3668            write!(f, " WHERE {w}")?;
3669        }
3670        write_returning(self.returning.as_deref(), f)?;
3671        Ok(())
3672    }
3673}
3674
3675impl fmt::Display for DeleteStatement {
3676    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3677        write!(f, "DELETE FROM {}", quote_ident(&self.table))?;
3678        if let Some(w) = &self.where_ {
3679            write!(f, " WHERE {w}")?;
3680        }
3681        write_returning(self.returning.as_deref(), f)?;
3682        Ok(())
3683    }
3684}
3685
3686impl fmt::Display for SelectStatement {
3687    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3688        // v7.30.1 (mailrs round-24 class audit) — the WITH clause
3689        // must survive the round trip; a CTE-using statement
3690        // re-parsed without it references undefined tables.
3691        if !self.ctes.is_empty() {
3692            f.write_str("WITH ")?;
3693            if self.ctes.iter().any(|c| c.recursive) {
3694                f.write_str("RECURSIVE ")?;
3695            }
3696            for (i, cte) in self.ctes.iter().enumerate() {
3697                if i > 0 {
3698                    f.write_str(", ")?;
3699                }
3700                f.write_str(&quote_ident(&cte.name))?;
3701                if !cte.column_overrides.is_empty() {
3702                    f.write_str(" (")?;
3703                    for (ci, c) in cte.column_overrides.iter().enumerate() {
3704                        if ci > 0 {
3705                            f.write_str(", ")?;
3706                        }
3707                        f.write_str(&quote_ident(c))?;
3708                    }
3709                    f.write_str(")")?;
3710                }
3711                write!(f, " AS ({})", cte.body)?;
3712            }
3713            f.write_str(" ")?;
3714        }
3715        write_bare_select(self, f)?;
3716        for (kind, peer) in &self.unions {
3717            f.write_str(match kind {
3718                UnionKind::Distinct => " UNION ",
3719                UnionKind::All => " UNION ALL ",
3720            })?;
3721            write_bare_select(peer, f)?;
3722        }
3723        if !self.order_by.is_empty() {
3724            f.write_str(" ORDER BY ")?;
3725            for (i, o) in self.order_by.iter().enumerate() {
3726                if i > 0 {
3727                    f.write_str(", ")?;
3728                }
3729                write!(f, "{}", o.expr)?;
3730                if o.desc {
3731                    f.write_str(" DESC")?;
3732                }
3733                match o.nulls_first {
3734                    Some(true) => f.write_str(" NULLS FIRST")?,
3735                    Some(false) => f.write_str(" NULLS LAST")?,
3736                    None => {}
3737                }
3738            }
3739        }
3740        // v7.30.1 (mailrs round-24 class audit) — WITH TIES only
3741        // exists in the FETCH FIRST spelling; rendering it as LIMIT
3742        // dropped the tie-extension semantics on replay. The parser
3743        // accepts OFFSET before FETCH, so keep that order here.
3744        if self.limit_with_ties {
3745            if let Some(o) = &self.offset {
3746                write!(f, " OFFSET {o}")?;
3747            }
3748            if let Some(n) = &self.limit {
3749                write!(f, " FETCH FIRST {n} ROWS WITH TIES")?;
3750            }
3751        } else {
3752            if let Some(n) = &self.limit {
3753                write!(f, " LIMIT {n}")?;
3754            }
3755            if let Some(o) = &self.offset {
3756                write!(f, " OFFSET {o}")?;
3757            }
3758        }
3759        Ok(())
3760    }
3761}
3762
3763fn write_bare_select(s: &SelectStatement, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3764    f.write_str("SELECT ")?;
3765    if s.distinct {
3766        f.write_str("DISTINCT ")?;
3767    }
3768    write_bare_select_body(s, f)
3769}
3770
3771fn write_bare_select_body(s: &SelectStatement, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3772    for (i, item) in s.items.iter().enumerate() {
3773        if i > 0 {
3774            f.write_str(", ")?;
3775        }
3776        write!(f, "{item}")?;
3777    }
3778    if let Some(t) = &s.from {
3779        write!(f, " FROM {t}")?;
3780    }
3781    if let Some(e) = &s.where_ {
3782        write!(f, " WHERE {e}")?;
3783    }
3784    if let Some(gs) = &s.group_by {
3785        f.write_str(" GROUP BY ")?;
3786        for (i, g) in gs.iter().enumerate() {
3787            if i > 0 {
3788                f.write_str(", ")?;
3789            }
3790            write!(f, "{g}")?;
3791        }
3792    } else if s.group_by_all {
3793        // v7.30.1 (mailrs round-24 class audit) — the GROUP BY ALL
3794        // shortcut parses to group_by: None + this flag; dropping
3795        // it turned an aggregate query into a bare projection on
3796        // re-parse.
3797        f.write_str(" GROUP BY ALL")?;
3798    }
3799    if let Some(h) = &s.having {
3800        write!(f, " HAVING {h}")?;
3801    }
3802    Ok(())
3803}
3804
3805impl fmt::Display for SelectItem {
3806    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3807        match self {
3808            Self::Wildcard => f.write_str("*"),
3809            Self::Expr { expr, alias } => {
3810                write!(f, "{expr}")?;
3811                if let Some(a) = alias {
3812                    write!(f, " AS {}", quote_ident(a))?;
3813                }
3814                Ok(())
3815            }
3816        }
3817    }
3818}
3819
3820impl fmt::Display for FromClause {
3821    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3822        write!(f, "{}", self.primary)?;
3823        for j in &self.joins {
3824            match j.kind {
3825                JoinKind::Inner => write!(f, " INNER JOIN {}", j.table)?,
3826                JoinKind::Left => write!(f, " LEFT JOIN {}", j.table)?,
3827                JoinKind::Cross => write!(f, " CROSS JOIN {}", j.table)?,
3828            }
3829            if let Some(on) = &j.on {
3830                write!(f, " ON {on}")?;
3831            }
3832        }
3833        Ok(())
3834    }
3835}
3836
3837impl fmt::Display for TableRef {
3838    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3839        // v7.30.1 (mailrs round-24 class audit) — the dynamic
3840        // table-ref shapes must round-trip: rendering only the
3841        // (synthetic) name turned LATERAL / unnest() /
3842        // generate_series() into references to nonexistent tables
3843        // on re-parse.
3844        if let Some(inner) = &self.lateral_subquery {
3845            write!(f, "LATERAL ({inner})")?;
3846            if let Some(a) = &self.alias {
3847                write!(f, " AS {}", quote_ident(a))?;
3848            }
3849            return Ok(());
3850        }
3851        if let Some(expr) = &self.unnest_expr {
3852            write!(f, "UNNEST({expr})")?;
3853            if let Some(a) = &self.alias {
3854                write!(f, " AS {}", quote_ident(a))?;
3855                if !self.unnest_column_aliases.is_empty() {
3856                    f.write_str(" (")?;
3857                    for (i, c) in self.unnest_column_aliases.iter().enumerate() {
3858                        if i > 0 {
3859                            f.write_str(", ")?;
3860                        }
3861                        f.write_str(&quote_ident(c))?;
3862                    }
3863                    f.write_str(")")?;
3864                }
3865            }
3866            return Ok(());
3867        }
3868        if let Some(args) = &self.generate_series_args {
3869            f.write_str("generate_series(")?;
3870            for (i, a) in args.iter().enumerate() {
3871                if i > 0 {
3872                    f.write_str(", ")?;
3873                }
3874                write!(f, "{a}")?;
3875            }
3876            f.write_str(")")?;
3877            if let Some(a) = &self.alias {
3878                write!(f, " AS {}", quote_ident(a))?;
3879            }
3880            return Ok(());
3881        }
3882        write!(f, "{}", quote_ident(&self.name))?;
3883        if let Some(seg) = self.as_of_segment {
3884            write!(f, " AS OF SEGMENT {seg}")?;
3885        }
3886        if let Some(a) = &self.alias {
3887            write!(f, " AS {}", quote_ident(a))?;
3888        }
3889        Ok(())
3890    }
3891}
3892
3893impl fmt::Display for ColumnName {
3894    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3895        if let Some(q) = &self.qualifier {
3896            write!(f, "{}.{}", quote_ident(q), quote_ident(&self.name))
3897        } else {
3898            write!(f, "{}", quote_ident(&self.name))
3899        }
3900    }
3901}
3902
3903impl fmt::Display for Expr {
3904    #[allow(clippy::too_many_lines)]
3905    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3906        match self {
3907            Self::Literal(l) => write!(f, "{l}"),
3908            Self::Column(c) => write!(f, "{c}"),
3909            Self::Placeholder(n) => write!(f, "${n}"),
3910            Self::Binary { lhs, op, rhs } => write!(f, "({lhs} {op} {rhs})"),
3911            Self::Unary { op, expr } => match op {
3912                UnOp::Not => write!(f, "(NOT {expr})"),
3913                UnOp::Neg => write!(f, "(-{expr})"),
3914                UnOp::BitNot => write!(f, "(~{expr})"),
3915            },
3916            Self::Cast { expr, target } => write!(f, "({expr}::{target})"),
3917            Self::AggregateOrdered {
3918                call,
3919                order_by,
3920                distinct,
3921                filter,
3922            } => {
3923                let fmt_order_by = |f: &mut fmt::Formatter<'_>| -> fmt::Result {
3924                    for (i, o) in order_by.iter().enumerate() {
3925                        if i > 0 {
3926                            f.write_str(", ")?;
3927                        }
3928                        write!(f, "{}", o.expr)?;
3929                        if o.desc {
3930                            f.write_str(" DESC")?;
3931                        }
3932                        match o.nulls_first {
3933                            Some(true) => f.write_str(" NULLS FIRST")?,
3934                            Some(false) => f.write_str(" NULLS LAST")?,
3935                            None => {}
3936                        }
3937                    }
3938                    Ok(())
3939                };
3940                // Ordered-set aggregates (`percentile_cont(f) WITHIN
3941                // GROUP (ORDER BY x)`) render the in-parens args as the
3942                // direct argument and the sort spec under WITHIN GROUP —
3943                // not as an in-argument ORDER BY.
3944                let ordered_set = matches!(
3945                    call.as_ref(),
3946                    Expr::FunctionCall { name, .. }
3947                        if matches!(
3948                            name.to_ascii_lowercase().as_str(),
3949                            "percentile_cont" | "percentile_disc" | "mode"
3950                        )
3951                );
3952                if ordered_set {
3953                    write!(f, "{call} WITHIN GROUP (ORDER BY ")?;
3954                    fmt_order_by(f)?;
3955                    f.write_str(")")?;
3956                } else {
3957                    // `name([DISTINCT ]args [ORDER BY …])` — peel the
3958                    // inner call's parens to splice modifiers.
3959                    let inner = alloc::format!("{call}");
3960                    let body = inner.strip_suffix(')').unwrap_or(&inner);
3961                    let (head, args_part) = body.split_once('(').unwrap_or((body, ""));
3962                    write!(f, "{head}(")?;
3963                    if *distinct {
3964                        f.write_str("DISTINCT ")?;
3965                    }
3966                    write!(f, "{args_part}")?;
3967                    if !order_by.is_empty() {
3968                        f.write_str(" ORDER BY ")?;
3969                        fmt_order_by(f)?;
3970                    }
3971                    f.write_str(")")?;
3972                }
3973                if let Some(cond) = filter {
3974                    write!(f, " FILTER (WHERE {cond})")?;
3975                }
3976                Ok(())
3977            }
3978            Self::IsNull { expr, negated } => {
3979                if *negated {
3980                    write!(f, "({expr} IS NOT NULL)")
3981                } else {
3982                    write!(f, "({expr} IS NULL)")
3983                }
3984            }
3985            Self::FunctionCall { name, args } => {
3986                write!(f, "{name}(")?;
3987                for (i, a) in args.iter().enumerate() {
3988                    if i > 0 {
3989                        f.write_str(", ")?;
3990                    }
3991                    write!(f, "{a}")?;
3992                }
3993                f.write_str(")")
3994            }
3995            Self::Like {
3996                expr,
3997                pattern,
3998                negated,
3999                case_insensitive,
4000            } => {
4001                let op = match (negated, case_insensitive) {
4002                    (false, false) => "LIKE",
4003                    (true, false) => "NOT LIKE",
4004                    (false, true) => "ILIKE",
4005                    (true, true) => "NOT ILIKE",
4006                };
4007                write!(f, "({expr} {op} {pattern})")
4008            }
4009            Self::Extract { field, source } => write!(f, "EXTRACT({field} FROM {source})"),
4010            Self::WindowFunction {
4011                name,
4012                args,
4013                partition_by,
4014                order_by,
4015                frame,
4016                null_treatment,
4017            } => {
4018                write!(f, "{name}(")?;
4019                for (i, a) in args.iter().enumerate() {
4020                    if i > 0 {
4021                        f.write_str(", ")?;
4022                    }
4023                    write!(f, "{a}")?;
4024                }
4025                f.write_str(")")?;
4026                // v7.30.1 (mailrs round-24 class audit) — IGNORE
4027                // NULLS sits between the arg list and OVER; dropping
4028                // it reverted replayed queries to RESPECT NULLS.
4029                if matches!(null_treatment, NullTreatment::Ignore) {
4030                    f.write_str(" IGNORE NULLS")?;
4031                }
4032                f.write_str(" OVER (")?;
4033                if !partition_by.is_empty() {
4034                    f.write_str("PARTITION BY ")?;
4035                    for (i, p) in partition_by.iter().enumerate() {
4036                        if i > 0 {
4037                            f.write_str(", ")?;
4038                        }
4039                        write!(f, "{p}")?;
4040                    }
4041                }
4042                if !order_by.is_empty() {
4043                    if !partition_by.is_empty() {
4044                        f.write_str(" ")?;
4045                    }
4046                    f.write_str("ORDER BY ")?;
4047                    for (i, (e, desc, nulls_first)) in order_by.iter().enumerate() {
4048                        if i > 0 {
4049                            f.write_str(", ")?;
4050                        }
4051                        write!(f, "{e}")?;
4052                        if *desc {
4053                            f.write_str(" DESC")?;
4054                        }
4055                        match nulls_first {
4056                            Some(true) => f.write_str(" NULLS FIRST")?,
4057                            Some(false) => f.write_str(" NULLS LAST")?,
4058                            None => {}
4059                        }
4060                    }
4061                }
4062                if let Some(fr) = frame {
4063                    if !partition_by.is_empty() || !order_by.is_empty() {
4064                        f.write_str(" ")?;
4065                    }
4066                    let k = match fr.kind {
4067                        FrameKind::Rows => "ROWS",
4068                        FrameKind::Range => "RANGE",
4069                    };
4070                    if let Some(end) = &fr.end {
4071                        write!(f, "{k} BETWEEN {} AND {}", fr.start, end)?;
4072                    } else {
4073                        write!(f, "{k} {}", fr.start)?;
4074                    }
4075                }
4076                f.write_str(")")
4077            }
4078            Self::ScalarSubquery(s) => write!(f, "({s})"),
4079            Self::Exists { subquery, negated } => {
4080                if *negated {
4081                    write!(f, "NOT EXISTS ({subquery})")
4082                } else {
4083                    write!(f, "EXISTS ({subquery})")
4084                }
4085            }
4086            Self::InSubquery {
4087                expr,
4088                subquery,
4089                negated,
4090            } => {
4091                if *negated {
4092                    write!(f, "({expr} NOT IN ({subquery}))")
4093                } else {
4094                    write!(f, "({expr} IN ({subquery}))")
4095                }
4096            }
4097            Self::InList {
4098                expr,
4099                list,
4100                negated,
4101            } => {
4102                let kw = if *negated { " NOT IN (" } else { " IN (" };
4103                write!(f, "({expr}{kw}")?;
4104                for (i, e) in list.iter().enumerate() {
4105                    if i > 0 {
4106                        f.write_str(", ")?;
4107                    }
4108                    write!(f, "{e}")?;
4109                }
4110                f.write_str("))")
4111            }
4112            Self::Array(items) => {
4113                f.write_str("ARRAY[")?;
4114                for (i, e) in items.iter().enumerate() {
4115                    if i > 0 {
4116                        f.write_str(", ")?;
4117                    }
4118                    write!(f, "{e}")?;
4119                }
4120                f.write_str("]")
4121            }
4122            Self::ArraySubscript { target, index } => write!(f, "({target}[{index}])"),
4123            Self::AnyAll {
4124                expr,
4125                op,
4126                array,
4127                is_any,
4128            } => {
4129                let kw = if *is_any { "ANY" } else { "ALL" };
4130                write!(f, "({expr} {op} {kw}({array}))")
4131            }
4132            Self::Case {
4133                operand,
4134                branches,
4135                else_branch,
4136            } => {
4137                f.write_str("CASE")?;
4138                if let Some(op) = operand {
4139                    write!(f, " {op}")?;
4140                }
4141                for (w, t) in branches {
4142                    write!(f, " WHEN {w} THEN {t}")?;
4143                }
4144                if let Some(e) = else_branch {
4145                    write!(f, " ELSE {e}")?;
4146                }
4147                f.write_str(" END")
4148            }
4149        }
4150    }
4151}
4152
4153impl fmt::Display for Literal {
4154    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4155        match self {
4156            Self::Integer(n) => write!(f, "{n}"),
4157            Self::Float(x) => {
4158                let s = format!("{x}");
4159                // Default Display for an integral f64 (e.g. 1.0) emits "1",
4160                // which would round-trip back to Integer. Force a dot.
4161                if s.contains('.') || s.contains('e') || s.contains('E') {
4162                    f.write_str(&s)
4163                } else {
4164                    write!(f, "{s}.0")
4165                }
4166            }
4167            Self::String(s) => {
4168                f.write_str("'")?;
4169                for c in s.chars() {
4170                    if c == '\'' {
4171                        f.write_str("''")?;
4172                    } else {
4173                        write!(f, "{c}")?;
4174                    }
4175                }
4176                f.write_str("'")
4177            }
4178            Self::Bool(b) => f.write_str(if *b { "TRUE" } else { "FALSE" }),
4179            Self::Null => f.write_str("NULL"),
4180            // PG external array form. Display round-trip re-enters
4181            // through the column-typed text coerce, same as pgwire.
4182            Self::TextArray(items) => {
4183                f.write_str("'{")?;
4184                for (i, it) in items.iter().enumerate() {
4185                    if i > 0 {
4186                        f.write_str(",")?;
4187                    }
4188                    match it {
4189                        None => f.write_str("NULL")?,
4190                        Some(s) => {
4191                            f.write_str("\"")?;
4192                            for c in s.chars() {
4193                                match c {
4194                                    // array-element escapes
4195                                    '"' | '\\' => write!(f, "\\{c}")?,
4196                                    // the OUTER wrapper is a SQL string
4197                                    // literal — embedded quotes must
4198                                    // double, or the rendered form
4199                                    // (WAL replay parses it back) is
4200                                    // invalid SQL
4201                                    '\'' => f.write_str("''")?,
4202                                    _ => write!(f, "{c}")?,
4203                                }
4204                            }
4205                            f.write_str("\"")?;
4206                        }
4207                    }
4208                }
4209                f.write_str("}'")
4210            }
4211            Self::IntArray(items) => {
4212                f.write_str("'{")?;
4213                for (i, it) in items.iter().enumerate() {
4214                    if i > 0 {
4215                        f.write_str(",")?;
4216                    }
4217                    match it {
4218                        None => f.write_str("NULL")?,
4219                        Some(n) => write!(f, "{n}")?,
4220                    }
4221                }
4222                f.write_str("}'")
4223            }
4224            Self::BigIntArray(items) => {
4225                f.write_str("'{")?;
4226                for (i, it) in items.iter().enumerate() {
4227                    if i > 0 {
4228                        f.write_str(",")?;
4229                    }
4230                    match it {
4231                        None => f.write_str("NULL")?,
4232                        Some(n) => write!(f, "{n}")?,
4233                    }
4234                }
4235                f.write_str("}'")
4236            }
4237            Self::Vector(v) => {
4238                f.write_str("[")?;
4239                for (i, x) in v.iter().enumerate() {
4240                    if i > 0 {
4241                        f.write_str(", ")?;
4242                    }
4243                    let s = format!("{x}");
4244                    // Mirror Float Display: force a dot so re-parse stays
4245                    // numerically literal.
4246                    if s.contains('.') || s.contains('e') || s.contains('E') {
4247                        f.write_str(&s)?;
4248                    } else {
4249                        write!(f, "{s}.0")?;
4250                    }
4251                }
4252                f.write_str("]")
4253            }
4254            Self::Interval { text, .. } => {
4255                f.write_str("INTERVAL '")?;
4256                for c in text.chars() {
4257                    if c == '\'' {
4258                        f.write_str("''")?;
4259                    } else {
4260                        write!(f, "{c}")?;
4261                    }
4262                }
4263                f.write_str("'")
4264            }
4265        }
4266    }
4267}
4268
4269impl fmt::Display for BinOp {
4270    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4271        f.write_str(match self {
4272            Self::Or => "OR",
4273            Self::And => "AND",
4274            Self::Eq => "=",
4275            Self::NotEq => "<>",
4276            Self::IsDistinctFrom => "IS DISTINCT FROM",
4277            Self::IsNotDistinctFrom => "IS NOT DISTINCT FROM",
4278            Self::Lt => "<",
4279            Self::LtEq => "<=",
4280            Self::Gt => ">",
4281            Self::GtEq => ">=",
4282            Self::Add => "+",
4283            Self::Sub => "-",
4284            Self::Mul => "*",
4285            Self::Div => "/",
4286            Self::L2Distance => "<->",
4287            Self::InnerProduct => "<#>",
4288            Self::CosineDistance => "<=>",
4289            Self::Concat => "||",
4290            Self::BitOr => "|",
4291            Self::BitAnd => "&",
4292            Self::JsonGet => "->",
4293            Self::JsonGetText => "->>",
4294            Self::JsonGetPath => "#>",
4295            Self::JsonGetPathText => "#>>",
4296            Self::JsonContains => "@>",
4297            Self::TsMatch => "@@",
4298            Self::InetContainedBy => "<<",
4299            Self::InetContainedByEq => "<<=",
4300            Self::InetContains => ">>",
4301            Self::InetContainsEq => ">>=",
4302            Self::InetOverlap => "&&",
4303        })
4304    }
4305}
4306
4307/// Quote `s` as a PG double-quoted identifier when required (keyword,
4308/// non-folded case, leading digit, embedded non-`[A-Za-z0-9_]`, empty).
4309/// Otherwise return it as-is. Returns an owned `String` to keep the call site
4310/// uniform.
4311fn quote_ident(s: &str) -> String {
4312    let needs_quote = match s.chars().next() {
4313        None => true,
4314        Some(c) if !c.is_ascii_alphabetic() && c != '_' => true,
4315        _ => {
4316            s.chars().any(|c| !(c.is_ascii_alphanumeric() || c == '_'))
4317                || s.chars().any(|c| c.is_ascii_uppercase())
4318                || is_keyword(s)
4319        }
4320    };
4321    if !needs_quote {
4322        return s.to_string();
4323    }
4324    let mut out = String::with_capacity(s.len() + 2);
4325    out.push('"');
4326    for c in s.chars() {
4327        if c == '"' {
4328            out.push_str("\"\"");
4329        } else {
4330            out.push(c);
4331        }
4332    }
4333    out.push('"');
4334    out
4335}
4336
4337fn is_keyword(s: &str) -> bool {
4338    matches!(
4339        &*s.to_ascii_lowercase(),
4340        "select"
4341            | "from"
4342            | "where"
4343            | "as"
4344            | "null"
4345            | "true"
4346            | "false"
4347            | "and"
4348            | "or"
4349            | "not"
4350            | "create"
4351            | "table"
4352            | "insert"
4353            | "into"
4354            | "values"
4355            | "index"
4356            | "on"
4357            | "begin"
4358            | "commit"
4359            | "rollback"
4360            | "is"
4361            | "between"
4362            | "in"
4363            | "like"
4364            | "group"
4365            | "distinct"
4366            | "union"
4367            | "all"
4368            | "join"
4369            | "inner"
4370            | "left"
4371            | "cross"
4372            | "outer"
4373            | "default"
4374            | "savepoint"
4375            | "release"
4376            | "to"
4377            | "having"
4378            | "show"
4379            | "extract"
4380            | "offset"
4381            | "asc"
4382            | "desc"
4383            | "interval"
4384    )
4385}
4386
4387#[cfg(test)]
4388mod tests {
4389    use super::*;
4390    use alloc::vec;
4391
4392    #[test]
4393    fn integer_literal_renders_without_dot() {
4394        assert_eq!(Literal::Integer(42).to_string(), "42");
4395    }
4396
4397    #[test]
4398    fn integral_float_keeps_dot() {
4399        assert_eq!(Literal::Float(1.0).to_string(), "1.0");
4400        assert_eq!(Literal::Float(1.5).to_string(), "1.5");
4401        assert_eq!(Literal::Float(2.5e-3).to_string(), "0.0025");
4402    }
4403
4404    #[test]
4405    fn string_literal_doubles_quote() {
4406        assert_eq!(Literal::String("it's".into()).to_string(), "'it''s'");
4407    }
4408
4409    #[test]
4410    fn bool_and_null_render_uppercase() {
4411        assert_eq!(Literal::Bool(true).to_string(), "TRUE");
4412        assert_eq!(Literal::Bool(false).to_string(), "FALSE");
4413        assert_eq!(Literal::Null.to_string(), "NULL");
4414    }
4415
4416    #[test]
4417    fn binary_op_always_parenthesised() {
4418        let e = Expr::Binary {
4419            lhs: Box::new(Expr::Literal(Literal::Integer(1))),
4420            op: BinOp::Add,
4421            rhs: Box::new(Expr::Literal(Literal::Integer(2))),
4422        };
4423        assert_eq!(e.to_string(), "(1 + 2)");
4424    }
4425
4426    #[test]
4427    fn select_star_from_table() {
4428        let s = SelectStatement {
4429            items: vec![SelectItem::Wildcard],
4430            from: Some(FromClause {
4431                primary: TableRef {
4432                    name: "users".into(),
4433                    alias: None,
4434                    as_of_segment: None,
4435                    unnest_expr: None,
4436                    unnest_column_aliases: Vec::new(),
4437                    generate_series_args: None,
4438                    lateral_subquery: None,
4439                },
4440                joins: vec![],
4441            }),
4442            where_: None,
4443            group_by: None,
4444            group_by_all: false,
4445            having: None,
4446            unions: vec![],
4447            order_by: Vec::new(),
4448            limit: None,
4449            offset: None,
4450            limit_with_ties: false,
4451            distinct: false,
4452            ctes: vec![],
4453        };
4454        assert_eq!(s.to_string(), "SELECT * FROM users");
4455    }
4456
4457    #[test]
4458    fn quote_ident_for_uppercase_and_keyword() {
4459        assert_eq!(quote_ident("foo"), "foo");
4460        assert_eq!(quote_ident("Foo"), "\"Foo\"");
4461        assert_eq!(quote_ident("select"), "\"select\"");
4462        assert_eq!(quote_ident(""), "\"\"");
4463        assert_eq!(quote_ident("a\"b"), "\"a\"\"b\"");
4464    }
4465}