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