Skip to main content

spg_sql/
ast.rs

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