Skip to main content

spg_sql/
ast.rs

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