Skip to main content

spg_sql/
ast.rs

1//! AST for the PG-dialect subset SPG accepts in v0.2.
2//!
3//! `Display` is implemented so that for any AST `a` produced by [`crate::parser`],
4//! re-parsing `format!("{a}")` yields a structurally equal AST. Binary and
5//! unary operators always emit parentheses to remove any precedence
6//! ambiguity — round-trip safety wins over prettiness.
7
8use alloc::boxed::Box;
9use alloc::format;
10use alloc::string::{String, ToString};
11use alloc::vec::Vec;
12use core::fmt;
13
14#[derive(Debug, Clone, PartialEq)]
15#[allow(clippy::large_enum_variant)] // Statement::Select dominates; Boxing would touch every match site
16pub enum Statement {
17    Select(SelectStatement),
18    CreateTable(CreateTableStatement),
19    /// v7.9.15 — `CREATE EXTENSION [IF NOT EXISTS] <name>
20    /// [WITH SCHEMA <s>] [VERSION <v>] [CASCADE]` accepted as a
21    /// no-op so PG dumps that include extension declarations
22    /// (notably `pgvector`) load against SPG without splitting
23    /// init scripts. mailrs migration follow-up F3.
24    CreateExtension(String),
25    CreateIndex(CreateIndexStatement),
26    Insert(InsertStatement),
27    /// v4.4 — `UPDATE <table> SET col=expr [, ...] [WHERE cond]`.
28    Update(UpdateStatement),
29    /// v4.4 — `DELETE FROM <table> [WHERE cond]`.
30    Delete(DeleteStatement),
31    Begin,
32    Commit,
33    Rollback,
34    /// `SAVEPOINT <name>` — push a named savepoint onto the active TX's
35    /// stack so a later `ROLLBACK TO <name>` can undo just the work
36    /// since this point.
37    Savepoint(String),
38    /// `ROLLBACK TO [SAVEPOINT] <name>` — restore catalog state to the
39    /// named savepoint and discard later savepoints. Does not end the
40    /// transaction.
41    RollbackToSavepoint(String),
42    /// `RELEASE [SAVEPOINT] <name>` — discard a savepoint without
43    /// rolling back. Keeps the work done since then.
44    ReleaseSavepoint(String),
45    /// `SHOW TABLES` — return the list of tables in the catalog.
46    ShowTables,
47    /// `SHOW COLUMNS FROM <table>` — return one row per column with
48    /// its declared name / type / nullability.
49    ShowColumns(String),
50    /// `CREATE USER 'name' WITH PASSWORD 'pw' ROLE 'admin'` (v4.1).
51    /// Role is optional; defaults to `readonly` when omitted.
52    CreateUser(CreateUserStatement),
53    /// `DROP USER 'name'` (v4.1).
54    DropUser(String),
55    /// `SHOW USERS` (v4.1) — admin-only listing of (name, role).
56    ShowUsers,
57    /// v4.26 — `EXPLAIN [ANALYZE] <select>`. The engine returns a
58    /// single-column text table describing the rewritten plan tree
59    /// for `inner`. `analyze` triggers an actual exec to attach
60    /// observed row counts and elapsed micros to each node.
61    Explain(ExplainStatement),
62    /// v6.0.4 — `ALTER INDEX <name> REBUILD [WITH (encoding = ...)]`.
63    /// Synchronous rebuild of an NSW index. With the optional
64    /// encoding clause, every stored cell at the indexed column is
65    /// also re-encoded through `coerce_value` before the new graph
66    /// builds.
67    AlterIndex(AlterIndexStatement),
68    /// v6.7.2 — `ALTER TABLE <name> SET <setting> = <value>`.
69    /// The only setting in v6.7.2 is `hot_tier_bytes`, which
70    /// overrides the global `SPG_HOT_TIER_BYTES` freezer trigger
71    /// for the named table.
72    AlterTable(AlterTableStatement),
73    /// v6.1.2 — `CREATE PUBLICATION <name> [FOR ALL TABLES]`.
74    /// The catalog row lives in `spg_publications`. Publisher-side
75    /// WAL filtering arrives in v6.1.5.
76    CreatePublication(CreatePublicationStatement),
77    /// v6.1.2 — `DROP PUBLICATION <name>`. PG-compatible silent
78    /// no-op when the publication does not exist.
79    DropPublication(String),
80    /// v6.1.3 — `SHOW PUBLICATIONS`. Returns one row per
81    /// publication ordered by name with `(name, scope_summary,
82    /// table_count)` columns. The scope summary is the human-
83    /// readable form `ALL TABLES` / `FOR TABLE …` / `FOR ALL
84    /// TABLES EXCEPT …`; `table_count` is `NULL` for the
85    /// `AllTables` scope and the table-list length otherwise.
86    ShowPublications,
87    /// v6.1.4 — `CREATE SUBSCRIPTION <name> CONNECTION '<conn>'
88    /// PUBLICATION <pub_name> [, <pub_name> …]`. Catalog lands
89    /// in `spg_subscriptions`; when the subscription is
90    /// `enabled = true` (default) the server spawns a
91    /// background worker that connects to `conn` and drains the
92    /// requested publication(s) into the local engine.
93    CreateSubscription(CreateSubscriptionStatement),
94    /// v6.1.4 — `DROP SUBSCRIPTION <name>`. Like DROP
95    /// PUBLICATION, silent no-op when absent. Stops the
96    /// associated worker thread before removing the row.
97    DropSubscription(String),
98    /// v6.1.4 — `SHOW SUBSCRIPTIONS`. Returns one row per
99    /// subscription ordered by name with `(name, conn_str,
100    /// publications, enabled, last_received_pos)`.
101    ShowSubscriptions,
102    /// v6.1.7 — `WAIT FOR WAL POSITION <pos> [WITH TIMEOUT <ms>]`.
103    /// Blocks until the local server's apply position reaches
104    /// `<pos>` or `<ms>` elapses. Server-layer command: the
105    /// engine refuses it (`EngineError::Unsupported`) since
106    /// `lag_state` lives in `spg-server`'s `ServerState`.
107    WaitForWalPosition {
108        pos: u64,
109        /// `None` → wait forever; `Some(ms)` → return after `ms`
110        /// milliseconds even if the target isn't reached.
111        timeout_ms: Option<u64>,
112    },
113    /// v6.2.0 — `ANALYZE [<table>]`. Bare form walks every user
114    /// table; `ANALYZE <name>` re-stats just one. Populates
115    /// `spg_statistic` with per-column null_frac + n_distinct +
116    /// 100-bucket equi-depth histogram.
117    Analyze(Option<String>),
118    /// v6.7.3 — `COMPACT COLD SEGMENTS`. Walks every user table's
119    /// BTree-cold indices and merges small cold-tier segments
120    /// (size below `SPG_COMPACTION_TARGET_SEGMENT_BYTES`, default
121    /// 4 MiB) into a single larger segment per (table, index).
122    /// `WHERE` predicate filtering on which tables to compact is
123    /// carved out of v6.7.3 (per V6_7_DESIGN.md STABILITY entry);
124    /// v6.7.3 only supports the bare form.
125    CompactColdSegments,
126}
127
128/// v6.1.4 — `CREATE SUBSCRIPTION` AST node. v6.1.4 ships a
129/// single fixed-shape DDL; the WITH-clause options PG supports
130/// (`enabled`, `slot_name`, `streaming`, `binary`) are out of
131/// scope for v6.1.4 — `enabled` defaults to true and there are
132/// no other knobs to set in v6.1.x.
133#[derive(Debug, Clone, PartialEq, Eq)]
134pub struct CreateSubscriptionStatement {
135    pub name: String,
136    /// Connection string in PG keyword=value form (e.g.
137    /// `host=127.0.0.1 port=20002`). v6.1.4 only consumes the
138    /// `host` and `port` fields; the rest is reserved for
139    /// future v6.1.x options.
140    pub conn_str: String,
141    /// One or more publications on the remote side. Order is
142    /// preserved verbatim from the DDL; the worker requests them
143    /// in this order. v6.1.4 records the list; v6.1.5
144    /// publisher-side filtering enforces it.
145    pub publications: Vec<String>,
146}
147
148/// v6.1.2 — `CREATE PUBLICATION` AST node. The `scope` field uses
149/// the [`PublicationScope`] shape. v6.1.2 only accepted
150/// `AllTables`; v6.1.3 unlocks the `ForTables` / `AllTablesExcept`
151/// variants by flipping the parser gate (no AST migration).
152#[derive(Debug, Clone, PartialEq, Eq)]
153pub struct CreatePublicationStatement {
154    pub name: String,
155    pub scope: PublicationScope,
156}
157
158/// v6.1.2 — Which tables a publication covers. v6.1.3 (this commit)
159/// flips the parser gate for the `ForTables` / `AllTablesExcept`
160/// variants — the on-disk shape, snapshot serialisation, and the
161/// AST round-trip Display path were already in place in v6.1.2
162/// so this is a parser-only widening.
163#[derive(Debug, Clone, PartialEq, Eq)]
164pub enum PublicationScope {
165    AllTables,
166    ForTables(Vec<String>),
167    AllTablesExcept(Vec<String>),
168}
169
170#[derive(Debug, Clone, PartialEq, Eq)]
171pub struct AlterIndexStatement {
172    pub name: String,
173    pub target: AlterIndexTarget,
174}
175
176#[derive(Debug, Clone, Copy, PartialEq, Eq)]
177pub enum AlterIndexTarget {
178    /// `REBUILD [WITH (encoding = <enc>)]`. `encoding = None`
179    /// rebuilds the existing graph in place without touching the
180    /// column encoding; `Some(enc)` re-encodes every cell first.
181    Rebuild { encoding: Option<VecEncoding> },
182}
183
184/// v6.7.2 — `ALTER TABLE t SET <setting> = <value>`. v6.7.2 ships
185/// the single `hot_tier_bytes` setting; later v6.7.x sub-versions
186/// can add more SET subjects without changing the dispatch shape.
187#[derive(Debug, Clone, PartialEq)]
188pub struct AlterTableStatement {
189    pub name: String,
190    pub target: AlterTableTarget,
191}
192
193#[derive(Debug, Clone, PartialEq)]
194pub enum AlterTableTarget {
195    /// Per-table hot-tier byte budget override. The freezer
196    /// reads this before falling back to `SPG_HOT_TIER_BYTES`.
197    SetHotTierBytes(u64),
198    /// v7.6.8 — `ALTER TABLE t ADD CONSTRAINT name FOREIGN KEY
199    /// (cols) REFERENCES parent[(pcols)] [ON DELETE/UPDATE …]`.
200    /// Engine validates existing rows against the new constraint
201    /// before installing it.
202    AddForeignKey(ForeignKeyConstraint),
203    /// v7.6.8 — `ALTER TABLE t DROP CONSTRAINT name`. Removes the
204    /// constraint by user-supplied name; raises if no FK with that
205    /// name exists on the table.
206    DropForeignKey(String),
207}
208
209#[derive(Debug, Clone, PartialEq)]
210pub struct ExplainStatement {
211    pub analyze: bool,
212    pub inner: Box<SelectStatement>,
213    /// v6.8.3 — `EXPLAIN (SUGGEST) <SELECT>` enables the index
214    /// advisor pass: after the regular plan tree, the engine
215    /// emits one suggestion line per column referenced in the
216    /// query's WHERE / JOIN that has no covering index on the
217    /// owning table.
218    pub suggest: bool,
219}
220
221#[derive(Debug, Clone, PartialEq, Eq)]
222pub struct CreateUserStatement {
223    pub name: String,
224    pub password: String,
225    /// One of `admin` / `readwrite` / `readonly`. Stored verbatim from
226    /// the parser; the engine validates against `Role::parse` so a
227    /// typo lands as a runtime error with a clear message rather than
228    /// a parse failure.
229    pub role: String,
230}
231
232#[derive(Debug, Clone, PartialEq)]
233pub struct CreateIndexStatement {
234    pub name: String,
235    pub table: String,
236    pub column: String,
237    /// Optional `USING <method>` clause. v2.0 recognises `hnsw` (NSW
238    /// graph for vector kNN); unspecified is the default B-tree index.
239    pub method: IndexMethod,
240    /// `IF NOT EXISTS` — engine returns `CommandOk` no-op when the
241    /// index name already exists, instead of raising `DuplicateIndex`.
242    pub if_not_exists: bool,
243    /// v6.8.0 — `INCLUDE (col1, col2, …)` columns. Identifies the
244    /// non-key columns the planner should treat as "covered" by
245    /// this index when checking whether a query can run as an
246    /// index-only scan. Empty when no `INCLUDE` clause was given.
247    pub included_columns: Vec<String>,
248    /// v6.8.1 — `WHERE <expr>` partial-index predicate. Only rows
249    /// for which `<expr>` evaluates truthy enter the index;
250    /// queries whose `WHERE` clause's canonical Display form
251    /// matches this expression's Display form can be served by the
252    /// partial index. Stored as a parsed `Expr` so the engine
253    /// re-uses the existing evaluation path; storage persists the
254    /// Display form on the catalog snapshot.
255    pub partial_predicate: Option<Expr>,
256    /// v6.8.2 — expression-based index. When `Some(expr)`, the
257    /// index key is the result of `expr` evaluated on each row
258    /// (e.g. `CREATE INDEX … (lower(name))`). The `column`
259    /// field still names the *primary* column the expression
260    /// touches so existing planner shortcuts that resolve a
261    /// column position stay valid. `None` = plain
262    /// column-reference index (the legacy shape).
263    pub expression: Option<Expr>,
264    /// v7.9.14 — extra column names after the leading column in a
265    /// multi-column `CREATE INDEX … (a, b, c)`. mailrs F2. The
266    /// planner today still only uses the leading column for index
267    /// seeks; the extras are tracked verbatim so the same DDL
268    /// round-trips through WAL replay + catalog snapshot, and so
269    /// the engine can emit a clear warning at INDEX CREATE time
270    /// that only the leading column is currently honoured.
271    /// Composite BTree index keys land in v7.10.
272    pub extra_columns: Vec<String>,
273}
274
275#[derive(Debug, Clone, Copy, PartialEq, Eq)]
276pub enum IndexMethod {
277    /// Default — B-tree over `IndexKey`. Used for equality / range
278    /// lookups on scalar columns.
279    BTree,
280    /// `USING hnsw` — NSW graph for kNN over a vector column.
281    Hnsw,
282    /// v6.7.1 — `USING brin` — Block Range INdex. Per-segment
283    /// metadata that records (min_key, max_key) for each page in a
284    /// cold-tier segment, on the indexed column. The optimizer
285    /// can use these summaries to skip pages whose range does NOT
286    /// overlap a query's WHERE predicate. BRIN indexes carry no
287    /// in-memory data — the summaries live in the segment v2
288    /// envelope's sidecar. Created via the standard
289    /// `CREATE INDEX … USING brin (col)` syntax.
290    Brin,
291}
292
293#[derive(Debug, Clone, PartialEq)]
294pub struct CreateTableStatement {
295    pub name: String,
296    pub columns: Vec<ColumnDef>,
297    /// `IF NOT EXISTS` — engine returns `CommandOk` no-op when the
298    /// table name already exists, instead of raising `DuplicateTable`.
299    pub if_not_exists: bool,
300    /// v7.6.0 — table-level `FOREIGN KEY (...) REFERENCES ...`
301    /// constraints. Column-level `REFERENCES` (single-column inline
302    /// form) is normalised into this vec at parse time so the engine
303    /// sees one uniform list.
304    pub foreign_keys: Vec<ForeignKeyConstraint>,
305    /// v7.9.18 — table-level constraints: `PRIMARY KEY (a, b)` and
306    /// `UNIQUE (a, b, ...)`. mailrs migration follow-up G1 + G6.
307    /// Engine resolves each into a BTree index named after the
308    /// constraint's leading column at CREATE TABLE time; INSERT
309    /// path enforces composite uniqueness via row scan on the
310    /// leading column index.
311    pub table_constraints: Vec<TableConstraint>,
312}
313
314/// v7.9.18 — table-level constraint at the end of a CREATE TABLE
315/// column list. Either a composite PRIMARY KEY or a UNIQUE
316/// (single- or multi-column).
317#[derive(Debug, Clone, PartialEq)]
318pub enum TableConstraint {
319    /// `PRIMARY KEY (col1, col2, ...)`. Implies NOT NULL on each
320    /// referenced column. Engine builds a BTree index named
321    /// `<table>_pkey` and enforces composite uniqueness on INSERT.
322    PrimaryKey {
323        name: Option<String>,
324        columns: Vec<String>,
325    },
326    /// `UNIQUE (col1, col2, ...)`. Engine builds a BTree index
327    /// named `<table>_<leading_col>_key` (single-column) or
328    /// `<table>_<leading_col>_<…>_key` (composite) and enforces
329    /// uniqueness on INSERT.
330    Unique {
331        name: Option<String>,
332        columns: Vec<String>,
333    },
334}
335
336#[derive(Debug, Clone, PartialEq)]
337pub struct ColumnDef {
338    pub name: String,
339    pub ty: ColumnTypeName,
340    pub nullable: bool,
341    /// `DEFAULT <expr>` literal supplied at CREATE TABLE. Engine
342    /// evaluates this once (with an empty row) and caches the resulting
343    /// `Value` on the column schema.
344    pub default: Option<Expr>,
345    /// MySQL-style `AUTO_INCREMENT` — the engine maintains a counter
346    /// per such column and fills the slot when INSERT leaves it
347    /// unbound (omitted from a column-list INSERT or explicitly NULL).
348    pub auto_increment: bool,
349    /// v7.9.13 — inline `PRIMARY KEY` column constraint. mailrs
350    /// migration follow-up F1. Implies `NOT NULL`. Engine creates
351    /// an implicit BTree index named `<table>_pkey` over this
352    /// column at CREATE TABLE time, satisfying the parent-side
353    /// index requirement for any FOREIGN KEY pointing at it.
354    pub is_primary_key: bool,
355}
356
357/// v7.6.0 — A single FOREIGN KEY constraint. Both column-level
358/// `REFERENCES` and table-level `FOREIGN KEY (...) REFERENCES ...`
359/// parse into this shape — the column-level form has a single-entry
360/// `columns` / `parent_columns`.
361#[derive(Debug, Clone, PartialEq)]
362pub struct ForeignKeyConstraint {
363    /// Optional `CONSTRAINT <name>` prefix. Engine ignores the name
364    /// today but parses + stores it so a future ALTER TABLE DROP
365    /// CONSTRAINT can target by name (v7.6.8).
366    pub name: Option<String>,
367    /// Local columns participating in the FK (≥ 1).
368    pub columns: Vec<String>,
369    /// Referenced parent table.
370    pub parent_table: String,
371    /// Referenced parent columns. Must have the same arity as
372    /// `columns`; engine validates parent has a PK / UNIQUE index
373    /// on exactly this column set (v7.6.1).
374    pub parent_columns: Vec<String>,
375    /// `ON DELETE` action. Defaults to `Restrict` if absent.
376    pub on_delete: FkAction,
377    /// `ON UPDATE` action. Defaults to `Restrict` if absent.
378    pub on_update: FkAction,
379}
380
381/// v7.6.0 — Referential action for `ON DELETE` / `ON UPDATE`.
382#[derive(Debug, Clone, Copy, PartialEq, Eq)]
383pub enum FkAction {
384    /// Reject the parent mutation if any child row references it.
385    /// SQL spec default; SPG default when no clause is given.
386    Restrict,
387    /// Recursively propagate the parent's delete / update to the
388    /// child rows. Same TX.
389    Cascade,
390    /// Set the child FK column(s) to NULL. Requires the FK columns
391    /// to be NULL-able.
392    SetNull,
393    /// Set the child FK column(s) to their declared DEFAULT.
394    /// Requires the child column(s) to have DEFAULT.
395    SetDefault,
396    /// SQL spec `NO ACTION` (deferred check). SPG treats this as
397    /// `Restrict` because the single-writer model has no deferred
398    /// constraint window; the keyword is accepted for compatibility.
399    NoAction,
400}
401
402/// In-cell encoding for a `VECTOR(N)` column. v6.0.1 added the
403/// optional `USING <encoding>` clause; omitting it keeps the
404/// pre-v6 `F32` default. `Sq8` quantises each cell to a per-vector
405/// affine `(min, max, [u8; dim])` triple (4× compression). `F16`
406/// (v6.0.3, DDL keyword `HALF`) stores each element as IEEE-754
407/// binary16 (2× compression, ~3 decimal digits of precision).
408#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
409pub enum VecEncoding {
410    /// IEEE-754 binary32. Pre-v6 default; matches pgvector's
411    /// uncompressed `vector` type wire / storage layout.
412    #[default]
413    F32,
414    /// v6.0.1 SQ8 — per-vector affine 8-bit quantisation. See
415    /// `spg_storage::quantize::Sq8Vector` for the math + recall
416    /// envelope (≥ 0.95 on Gaussian / unit-sphere corpora at
417    /// dim ≥ 32).
418    Sq8,
419    /// v6.0.3 halfvec — IEEE-754 binary16 (half-precision)
420    /// per-element. DDL keyword `HALF` (pgvector convention).
421    /// Bit-exact dequantise to f32 at the storage layer; no
422    /// rerank pass needed for kNN search.
423    F16,
424}
425
426impl fmt::Display for VecEncoding {
427    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
428        match self {
429            Self::F32 => f.write_str("F32"),
430            Self::Sq8 => f.write_str("SQ8"),
431            // pgvector convention: DDL keyword is `HALF`, not `F16`.
432            Self::F16 => f.write_str("HALF"),
433        }
434    }
435}
436
437/// SQL-level type names. The mapping to the storage runtime's `DataType`
438/// happens in `spg-engine` — keeping `spg-sql` free of storage deps.
439#[derive(Debug, Clone, Copy, PartialEq, Eq)]
440pub enum ColumnTypeName {
441    SmallInt,
442    Int,
443    BigInt,
444    Float,
445    Text,
446    /// `VARCHAR(N)` — TEXT capped at N Unicode characters.
447    Varchar(u32),
448    /// `CHAR(N)` — TEXT right-padded with spaces to exactly N characters.
449    Char(u32),
450    Bool,
451    /// pgvector fixed-dimension `VECTOR(N)`. v6.0.1 added the
452    /// `USING <encoding>` clause; omitting it surfaces as
453    /// `encoding = VecEncoding::F32` (the pre-v6 default).
454    Vector {
455        dim: u32,
456        encoding: VecEncoding,
457    },
458    /// `NUMERIC` / `NUMERIC(p)` / `NUMERIC(p, s)` — exact decimal.
459    /// Bare `NUMERIC` and `NUMERIC(p)` both surface with `scale=0`.
460    Numeric(u8, u8),
461    /// `DATE` — calendar day, no time-of-day component.
462    Date,
463    /// `TIMESTAMP` / `MySQL` `DATETIME` — instant with microsecond
464    /// precision.
465    Timestamp,
466    /// v7.9.2 `TIMESTAMPTZ` / `TIMESTAMP WITH TIME ZONE`. SPG
467    /// stores all timestamps as UTC microseconds-since-epoch and
468    /// does not carry per-row offset (PG's internal representation
469    /// is the same — TZ is a display convention). The distinction
470    /// from `TIMESTAMP` exists for the PG-wire layer to advertise
471    /// OID 1184 so sqlx-style clients decode into
472    /// `chrono::DateTime<Utc>` instead of `NaiveDateTime`.
473    Timestamptz,
474    /// v4.9 `JSON` — text-backed JSON document. No parse-time
475    /// validation; the engine round-trips the literal verbatim.
476    /// PG OID 114 on the wire.
477    Json,
478    /// v7.9.0 `JSONB` — same storage shape as Json, advertised as
479    /// PG OID 3802 on the wire so sqlx-style binary-typed clients
480    /// decode without a custom type registration.
481    Jsonb,
482}
483
484impl fmt::Display for ColumnTypeName {
485    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
486        match self {
487            Self::SmallInt => f.write_str("SMALLINT"),
488            Self::Int => f.write_str("INT"),
489            Self::BigInt => f.write_str("BIGINT"),
490            Self::Float => f.write_str("FLOAT"),
491            Self::Text => f.write_str("TEXT"),
492            Self::Varchar(n) => write!(f, "VARCHAR({n})"),
493            Self::Char(n) => write!(f, "CHAR({n})"),
494            Self::Bool => f.write_str("BOOL"),
495            Self::Vector { dim, encoding } => match encoding {
496                VecEncoding::F32 => write!(f, "VECTOR({dim})"),
497                VecEncoding::Sq8 => write!(f, "VECTOR({dim}) USING SQ8"),
498                VecEncoding::F16 => write!(f, "VECTOR({dim}) USING HALF"),
499            },
500            Self::Json => f.write_str("JSON"),
501            Self::Jsonb => f.write_str("JSONB"),
502            Self::Numeric(p, s) => {
503                if *s == 0 {
504                    write!(f, "NUMERIC({p})")
505                } else {
506                    write!(f, "NUMERIC({p}, {s})")
507                }
508            }
509            Self::Date => f.write_str("DATE"),
510            Self::Timestamp => f.write_str("TIMESTAMP"),
511            Self::Timestamptz => f.write_str("TIMESTAMPTZ"),
512        }
513    }
514}
515
516/// `UPDATE <table> SET col = expr [, ...] [WHERE cond]`. v4.4 — the
517/// engine evaluates `expr` per matched row in the table's row order
518/// and rewrites cells in place. Indexed columns are dropped + re-
519/// inserted into the affected B-tree on each row change.
520#[derive(Debug, Clone, PartialEq)]
521pub struct UpdateStatement {
522    pub table: String,
523    pub assignments: Vec<(String, Expr)>,
524    pub where_: Option<Expr>,
525    /// v7.9.4 — `RETURNING <projection>`. None = no RETURNING
526    /// clause (legacy CommandComplete path). Some = engine
527    /// evaluates the projection over each mutated row and
528    /// streams the result as a Rows QueryResult.
529    pub returning: Option<Vec<SelectItem>>,
530}
531
532/// `DELETE FROM <table> [WHERE cond]`. v4.4 — removes matched rows
533/// from the active catalog and prunes them from every index.
534#[derive(Debug, Clone, PartialEq)]
535pub struct DeleteStatement {
536    pub table: String,
537    pub where_: Option<Expr>,
538    /// v7.9.4 — `RETURNING <projection>`.
539    pub returning: Option<Vec<SelectItem>>,
540}
541
542#[derive(Debug, Clone, PartialEq)]
543pub struct InsertStatement {
544    pub table: String,
545    /// Optional column list — `INSERT INTO t (a, b) VALUES (...)`. When
546    /// `None`, every tuple is positional and must match the table arity.
547    /// When `Some`, the engine maps each tuple slot to the named column and
548    /// fills the rest with NULL (must be nullable).
549    pub columns: Option<Vec<String>>,
550    /// One or more `(expr, expr, ...)` tuples — the multi-row VALUES form.
551    /// v1.3+ accepts `INSERT INTO t VALUES (a), (b)`.
552    pub rows: Vec<Vec<Expr>>,
553    /// v7.9.7 — `ON CONFLICT (cols) DO { NOTHING | UPDATE SET … }`
554    /// upsert clause. None = legacy INSERT (conflict raises a
555    /// DuplicateKey error). mailrs migration blocker #2.
556    pub on_conflict: Option<OnConflictClause>,
557    /// v7.9.4 — `RETURNING <projection>`.
558    pub returning: Option<Vec<SelectItem>>,
559}
560
561/// v7.9.7 — INSERT upsert clause: `ON CONFLICT (target) DO action`.
562#[derive(Debug, Clone, PartialEq)]
563pub struct OnConflictClause {
564    /// Local columns that identify the conflict (must match a
565    /// UNIQUE / PRIMARY KEY index on the target table). Empty
566    /// list means the user wrote `ON CONFLICT DO …` without a
567    /// target — engine picks the table's first BTree index by
568    /// convention.
569    pub target_columns: Vec<String>,
570    /// The action on conflict.
571    pub action: OnConflictAction,
572}
573
574/// v7.9.7 — action on conflict.
575#[derive(Debug, Clone, PartialEq)]
576pub enum OnConflictAction {
577    /// `DO NOTHING` — INSERT proceeds for non-conflicting rows,
578    /// silently skips conflicting ones.
579    Nothing,
580    /// `DO UPDATE SET col = expr [, …] [WHERE cond]`. `assignments`
581    /// may reference `EXCLUDED.col` to read the incoming row's
582    /// value (engine wires `EXCLUDED` as a virtual table).
583    Update {
584        assignments: Vec<(String, Expr)>,
585        where_: Option<Expr>,
586    },
587}
588
589#[derive(Debug, Clone, PartialEq)]
590pub struct SelectStatement {
591    /// v4.11: `WITH name AS (SELECT ...) [, ...]` common-table
592    /// expressions, materialised once at query start before the
593    /// body SELECT runs. Empty for a regular SELECT. Non-recursive
594    /// only — no `WITH RECURSIVE` for v4.x.
595    pub ctes: Vec<Cte>,
596    pub distinct: bool,
597    pub items: Vec<SelectItem>,
598    pub from: Option<FromClause>,
599    pub where_: Option<Expr>,
600    pub group_by: Option<Vec<Expr>>,
601    /// v6.4.1 — `GROUP BY ALL` shortcut: when true, the planner
602    /// expands `group_by` to every non-aggregate SELECT-list item
603    /// before the executor runs. Mutually exclusive with an
604    /// explicit `group_by` list (the parser sets exactly one).
605    pub group_by_all: bool,
606    /// `HAVING <expr>` — filter applied *after* `GROUP BY` aggregation.
607    /// Supports aggregate calls (e.g. `HAVING count(*) > 1`); the
608    /// aggregate executor resolves them through the same synthetic
609    /// schema used for the SELECT items.
610    pub having: Option<Expr>,
611    /// UNION / UNION ALL chain. Empty for a plain SELECT. Each peer is
612    /// itself a `SelectStatement` with `order_by = None` and `limit =
613    /// None` (the parser enforces that — ORDER BY / LIMIT belong to the
614    /// top of the chain).
615    pub unions: Vec<(UnionKind, SelectStatement)>,
616    /// v6.4.0 — multi-key ORDER BY. Empty `Vec` means no ORDER BY.
617    /// Keys are matched left-to-right: first key decides, ties break
618    /// to the second, etc.
619    pub order_by: Vec<OrderBy>,
620    pub limit: Option<u32>,
621    /// `OFFSET <n>` — drop the first `n` rows after ORDER BY but
622    /// before LIMIT (so `LIMIT 10 OFFSET 5` keeps rows 6..=15).
623    pub offset: Option<u32>,
624}
625
626#[derive(Debug, Clone, PartialEq)]
627pub struct Cte {
628    pub name: String,
629    pub body: SelectStatement,
630    /// v4.22: `WITH RECURSIVE` — set when the WITH clause had the
631    /// RECURSIVE keyword. Applies to every CTE in the clause per
632    /// PG semantics. A non-recursive body in a RECURSIVE WITH is
633    /// allowed; the engine just runs it once.
634    pub recursive: bool,
635    /// v4.22: optional `WITH name(a, b, c)` column-name list. When
636    /// non-empty, these override the body's output column names
637    /// position-by-position; the engine errors out if the count
638    /// doesn't match the body's projection width.
639    pub column_overrides: Vec<String>,
640}
641
642#[derive(Debug, Clone, PartialEq)]
643pub struct OrderBy {
644    pub expr: Expr,
645    /// `false` = ASC (default), `true` = DESC.
646    pub desc: bool,
647}
648
649#[derive(Debug, Clone, Copy, PartialEq, Eq)]
650pub enum UnionKind {
651    /// `UNION` — dedupes the combined set.
652    Distinct,
653    /// `UNION ALL` — concatenates without dedup.
654    All,
655}
656
657#[derive(Debug, Clone, PartialEq)]
658pub enum SelectItem {
659    Wildcard,
660    Expr { expr: Expr, alias: Option<String> },
661}
662
663#[derive(Debug, Clone, PartialEq)]
664pub struct TableRef {
665    pub name: String,
666    pub alias: Option<String>,
667    /// v6.10.2 — `AS OF SEGMENT '<id>'` cold-tier time-travel.
668    /// When `Some(id)`, the scan restricts to rows that live in
669    /// segment `<id>` only — useful for forensic inspection of a
670    /// specific freezer-emitted segment without exposing the hot
671    /// tier. `AS OF TIMESTAMP <ts>` (PG-flavoured time travel)
672    /// is STABILITY carve-out for v6.10 — needs the freezer to
673    /// stamp each segment with a wall-clock at creation time.
674    pub as_of_segment: Option<u32>,
675}
676
677/// FROM clause shape. v1.10 accepts a primary table plus a flat list of
678/// joined peers — `FROM a [, b]* [INNER|LEFT] JOIN c ON expr ...`. The
679/// joins evaluate left-associatively in nested-loop order.
680#[derive(Debug, Clone, PartialEq)]
681pub struct FromClause {
682    pub primary: TableRef,
683    pub joins: Vec<FromJoin>,
684}
685
686#[derive(Debug, Clone, PartialEq)]
687pub struct FromJoin {
688    pub kind: JoinKind,
689    pub table: TableRef,
690    /// Required for INNER/LEFT; must be `None` for CROSS / comma-list.
691    pub on: Option<Expr>,
692}
693
694#[derive(Debug, Clone, Copy, PartialEq, Eq)]
695pub enum JoinKind {
696    Inner,
697    Left,
698    Cross,
699}
700
701#[derive(Debug, Clone, PartialEq)]
702pub enum Expr {
703    Literal(Literal),
704    Column(ColumnName),
705    /// v6.1.1 — `$N` parameter placeholder for the extended query
706    /// protocol. The number is 1-based per PostgreSQL convention.
707    /// Evaluation looks up `params[N-1]` from the prepared-statement
708    /// bind buffer; out-of-range indices raise a runtime error
709    /// (same shape as a column-not-found miss).
710    Placeholder(u16),
711    Binary {
712        lhs: Box<Expr>,
713        op: BinOp,
714        rhs: Box<Expr>,
715    },
716    Unary {
717        op: UnOp,
718        expr: Box<Expr>,
719    },
720    /// PG-style `expr::TYPE` cast. v1.3 supports VECTOR, INT, BIGINT, FLOAT,
721    /// TEXT, BOOL targets; engine coerces at evaluation time.
722    Cast {
723        expr: Box<Expr>,
724        target: CastTarget,
725    },
726    /// Postfix `IS NULL` / `IS NOT NULL`. Returns BOOL.
727    IsNull {
728        expr: Box<Expr>,
729        negated: bool,
730    },
731    /// Function call `name(args...)`. v1.4 supports a small built-in set
732    /// (length, upper, lower, abs, coalesce); unknown names error at eval
733    /// time so the parser stays open for v1.5 aggregates.
734    FunctionCall {
735        name: String,
736        args: Vec<Expr>,
737    },
738    /// SQL `LIKE` predicate. `pattern` evaluates to text at runtime;
739    /// wildcards are `%` (any run) and `_` (one char), backslash escapes
740    /// the next char (so `\%` matches a literal `%`).
741    Like {
742        expr: Box<Expr>,
743        pattern: Box<Expr>,
744        negated: bool,
745    },
746    /// v4.12 window function call: `name(args) OVER (PARTITION BY
747    /// ... ORDER BY ...)`. Supports `ROW_NUMBER` / `RANK` /
748    /// `DENSE_RANK` and the partition-aware aggregates `SUM` /
749    /// `AVG` / `COUNT` / `MIN` / `MAX`. The window frame defaults to "entire partition" for
750    /// unordered windows and "from start of partition through
751    /// current row" for ordered windows — no explicit ROWS /
752    /// RANGE clause in v4.12 MVP.
753    WindowFunction {
754        name: String,
755        args: Vec<Expr>,
756        partition_by: Vec<Expr>,
757        order_by: Vec<(Expr, bool /* desc */)>,
758        /// v4.20 explicit frame. `None` means "use the default":
759        /// whole-partition when unordered, running aggregate from
760        /// partition start through current row when ordered.
761        frame: Option<WindowFrame>,
762        /// v6.4.2 — `IGNORE NULLS` / `RESPECT NULLS` modifier on
763        /// LAG / LEAD / FIRST_VALUE / LAST_VALUE. Default is
764        /// `Respect` (PG / ANSI default — NULLs participate). Other
765        /// window functions ignore this flag.
766        null_treatment: NullTreatment,
767    },
768    /// v4.10 scalar subquery — `(SELECT ...)` used in expression
769    /// position. Must return exactly one row × one column at eval
770    /// time; the engine errors out otherwise. Uncorrelated only —
771    /// the inner SELECT cannot reference outer columns.
772    ScalarSubquery(Box<SelectStatement>),
773    /// v4.10 `[NOT] EXISTS (SELECT ...)`. Returns Bool. Inner
774    /// projection is ignored; only row-count matters.
775    Exists {
776        subquery: Box<SelectStatement>,
777        negated: bool,
778    },
779    /// v4.10 `expr [NOT] IN (SELECT ...)`. Inner SELECT must
780    /// project exactly one column; membership is tested by Eq
781    /// against each row's value (NULL handling follows ANSI:
782    /// NULL ∈ list ⇒ NULL ; otherwise present ⇒ true).
783    InSubquery {
784        expr: Box<Expr>,
785        subquery: Box<SelectStatement>,
786        negated: bool,
787    },
788    /// `EXTRACT(<field> FROM <source>)` — pull an integer component
789    /// out of a `DATE` or `TIMESTAMP`. Parsed as its own AST node
790    /// because the `FROM` keyword is what separates the two halves,
791    /// not a comma.
792    Extract {
793        field: ExtractField,
794        source: Box<Expr>,
795    },
796}
797
798/// v6.4.2 — null treatment on `LAG` / `LEAD` / `FIRST_VALUE` /
799/// `LAST_VALUE`. PG / ANSI default is `Respect` — NULLs participate
800/// in the offset walk. `Ignore` causes the function to skip NULL
801/// values in the argument expression, returning the next non-NULL.
802#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
803pub enum NullTreatment {
804    #[default]
805    Respect,
806    Ignore,
807}
808
809/// v4.20 explicit window frame: `ROWS|RANGE BETWEEN <bound> AND
810/// <bound>`. `end` is `None` for the shorthand "ROWS <bound>"
811/// where end implicitly = CURRENT ROW.
812#[derive(Debug, Clone, PartialEq, Eq)]
813pub struct WindowFrame {
814    pub kind: FrameKind,
815    pub start: FrameBound,
816    pub end: Option<FrameBound>,
817}
818
819#[derive(Debug, Clone, Copy, PartialEq, Eq)]
820pub enum FrameKind {
821    Rows,
822    Range,
823}
824
825#[derive(Debug, Clone, PartialEq, Eq)]
826pub enum FrameBound {
827    UnboundedPreceding,
828    OffsetPreceding(u64),
829    CurrentRow,
830    OffsetFollowing(u64),
831    UnboundedFollowing,
832}
833
834impl fmt::Display for FrameBound {
835    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
836        match self {
837            Self::UnboundedPreceding => f.write_str("UNBOUNDED PRECEDING"),
838            Self::OffsetPreceding(n) => write!(f, "{n} PRECEDING"),
839            Self::CurrentRow => f.write_str("CURRENT ROW"),
840            Self::OffsetFollowing(n) => write!(f, "{n} FOLLOWING"),
841            Self::UnboundedFollowing => f.write_str("UNBOUNDED FOLLOWING"),
842        }
843    }
844}
845
846#[derive(Debug, Clone, Copy, PartialEq, Eq)]
847pub enum ExtractField {
848    Year,
849    Month,
850    Day,
851    Hour,
852    Minute,
853    Second,
854    Microsecond,
855}
856
857impl fmt::Display for ExtractField {
858    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
859        f.write_str(match self {
860            Self::Year => "YEAR",
861            Self::Month => "MONTH",
862            Self::Day => "DAY",
863            Self::Hour => "HOUR",
864            Self::Minute => "MINUTE",
865            Self::Second => "SECOND",
866            Self::Microsecond => "MICROSECOND",
867        })
868    }
869}
870
871#[derive(Debug, Clone, Copy, PartialEq, Eq)]
872pub enum CastTarget {
873    Int,
874    BigInt,
875    Float,
876    Text,
877    Bool,
878    Vector,
879    Date,
880    Timestamp,
881}
882
883impl fmt::Display for CastTarget {
884    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
885        f.write_str(match self {
886            Self::Int => "int",
887            Self::BigInt => "bigint",
888            Self::Float => "float",
889            Self::Text => "text",
890            Self::Bool => "bool",
891            Self::Vector => "vector",
892            Self::Date => "date",
893            Self::Timestamp => "timestamp",
894        })
895    }
896}
897
898#[derive(Debug, Clone, PartialEq)]
899pub enum Literal {
900    Integer(i64),
901    Float(f64),
902    String(String),
903    Bool(bool),
904    Null,
905    /// pgvector-style array literal, e.g. `[1, 2.5, -3]`.
906    Vector(Vec<f32>),
907    /// `INTERVAL '<n> <unit> [<n> <unit> ...]'` — calendar-aware span.
908    /// Split into a months part (because a month is not a fixed number of
909    /// days) and a microseconds part (everything sub-month). `text` keeps
910    /// the original spelling so Display round-trips byte-for-byte.
911    Interval {
912        months: i32,
913        micros: i64,
914        text: String,
915    },
916}
917
918#[derive(Debug, Clone, PartialEq, Eq)]
919pub struct ColumnName {
920    pub qualifier: Option<String>,
921    pub name: String,
922}
923
924#[derive(Debug, Clone, Copy, PartialEq, Eq)]
925pub enum BinOp {
926    Or,
927    And,
928    Eq,
929    NotEq,
930    Lt,
931    LtEq,
932    Gt,
933    GtEq,
934    Add,
935    Sub,
936    Mul,
937    Div,
938    /// pgvector L2 (Euclidean) distance `<->`. Defined for two vector
939    /// operands of equal dimension; engine returns `Value::Float(d)`.
940    L2Distance,
941    /// pgvector inner-product `<#>` — returns `-Σ aᵢ bᵢ` so "smaller =
942    /// more similar" remains true (matches pgvector's published convention).
943    InnerProduct,
944    /// pgvector cosine distance `<=>` — `1 - (a·b)/(|a| |b|)`.
945    CosineDistance,
946    /// SQL string concatenation `||`. NULL propagates.
947    Concat,
948    /// v4.14 `json -> key` — element access by string key (object)
949    /// or integer index (array). Returns a JSON value.
950    JsonGet,
951    /// v4.14 `json ->> key` — same access, returns the result as
952    /// TEXT (unwraps a top-level JSON string; renders other scalars
953    /// as their canonical text).
954    JsonGetText,
955    /// v6.4.5 `json #> path_text` — walk the path encoded as a PG
956    /// text array literal like `'{a,0,b}'`. Returns JSON.
957    JsonGetPath,
958    /// v6.4.5 `json #>> path_text` — same walk, returns TEXT.
959    JsonGetPathText,
960    /// v6.4.5 `json @> sub_json` — containment. Returns BOOL; true
961    /// when every key/value in `sub_json` is structurally present in
962    /// the left side. Matches PG semantics (top-level + recursive).
963    JsonContains,
964}
965
966#[derive(Debug, Clone, Copy, PartialEq, Eq)]
967pub enum UnOp {
968    Not,
969    Neg,
970}
971
972// --- Display impls (round-trip-safe) --------------------------------------
973
974impl fmt::Display for Statement {
975    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
976        match self {
977            Self::Select(s) => s.fmt(f),
978            Self::CreateTable(s) => s.fmt(f),
979            Self::CreateIndex(s) => s.fmt(f),
980            Self::Insert(s) => s.fmt(f),
981            Self::Update(s) => s.fmt(f),
982            Self::Delete(s) => s.fmt(f),
983            Self::Begin => f.write_str("BEGIN"),
984            Self::Commit => f.write_str("COMMIT"),
985            Self::Rollback => f.write_str("ROLLBACK"),
986            Self::Savepoint(n) => write!(f, "SAVEPOINT {}", quote_ident(n)),
987            Self::RollbackToSavepoint(n) => write!(f, "ROLLBACK TO SAVEPOINT {}", quote_ident(n)),
988            Self::ReleaseSavepoint(n) => write!(f, "RELEASE SAVEPOINT {}", quote_ident(n)),
989            Self::ShowTables => f.write_str("SHOW TABLES"),
990            Self::ShowColumns(t) => write!(f, "SHOW COLUMNS FROM {}", quote_ident(t)),
991            Self::CreateUser(s) => write!(
992                f,
993                "CREATE USER {} WITH PASSWORD '<redacted>' ROLE '{}'",
994                quote_ident(&s.name),
995                s.role
996            ),
997            Self::DropUser(n) => write!(f, "DROP USER {}", quote_ident(n)),
998            Self::ShowUsers => f.write_str("SHOW USERS"),
999            Self::ShowPublications => f.write_str("SHOW PUBLICATIONS"),
1000            Self::ShowSubscriptions => f.write_str("SHOW SUBSCRIPTIONS"),
1001            Self::CreateSubscription(s) => {
1002                write!(
1003                    f,
1004                    "CREATE SUBSCRIPTION {} CONNECTION '{}' PUBLICATION ",
1005                    quote_ident(&s.name),
1006                    s.conn_str.replace('\'', "''")
1007                )?;
1008                for (i, p) in s.publications.iter().enumerate() {
1009                    if i > 0 {
1010                        f.write_str(", ")?;
1011                    }
1012                    write!(f, "{}", quote_ident(p))?;
1013                }
1014                Ok(())
1015            }
1016            Self::DropSubscription(name) => {
1017                write!(f, "DROP SUBSCRIPTION {}", quote_ident(name))
1018            }
1019            Self::WaitForWalPosition { pos, timeout_ms } => {
1020                write!(f, "WAIT FOR WAL POSITION {pos}")?;
1021                if let Some(ms) = timeout_ms {
1022                    write!(f, " WITH TIMEOUT {ms}")?;
1023                }
1024                Ok(())
1025            }
1026            Self::Analyze(None) => f.write_str("ANALYZE"),
1027            Self::Analyze(Some(t)) => write!(f, "ANALYZE {}", quote_ident(t)),
1028            Self::CompactColdSegments => f.write_str("COMPACT COLD SEGMENTS"),
1029            Self::Explain(e) => {
1030                if e.suggest {
1031                    write!(f, "EXPLAIN (SUGGEST) {}", e.inner)
1032                } else if e.analyze {
1033                    write!(f, "EXPLAIN ANALYZE {}", e.inner)
1034                } else {
1035                    write!(f, "EXPLAIN {}", e.inner)
1036                }
1037            }
1038            Self::AlterIndex(a) => {
1039                write!(f, "ALTER INDEX {} ", quote_ident(&a.name))?;
1040                match a.target {
1041                    AlterIndexTarget::Rebuild { encoding } => {
1042                        f.write_str("REBUILD")?;
1043                        if let Some(enc) = encoding {
1044                            write!(f, " WITH (encoding = {enc})")?;
1045                        }
1046                        Ok(())
1047                    }
1048                }
1049            }
1050            Self::AlterTable(a) => {
1051                write!(f, "ALTER TABLE {} ", quote_ident(&a.name))?;
1052                match &a.target {
1053                    AlterTableTarget::SetHotTierBytes(n) => {
1054                        write!(f, "SET hot_tier_bytes = {n}")
1055                    }
1056                    AlterTableTarget::AddForeignKey(fk) => write!(f, "ADD {fk}"),
1057                    AlterTableTarget::DropForeignKey(name) => {
1058                        write!(f, "DROP CONSTRAINT {}", quote_ident(name))
1059                    }
1060                }
1061            }
1062            Self::CreatePublication(p) => {
1063                write!(f, "CREATE PUBLICATION {}", quote_ident(&p.name))?;
1064                match &p.scope {
1065                    PublicationScope::AllTables => f.write_str(" FOR ALL TABLES"),
1066                    PublicationScope::ForTables(ts) => {
1067                        f.write_str(" FOR TABLE ")?;
1068                        for (i, t) in ts.iter().enumerate() {
1069                            if i > 0 {
1070                                f.write_str(", ")?;
1071                            }
1072                            write!(f, "{}", quote_ident(t))?;
1073                        }
1074                        Ok(())
1075                    }
1076                    PublicationScope::AllTablesExcept(ts) => {
1077                        f.write_str(" FOR ALL TABLES EXCEPT ")?;
1078                        for (i, t) in ts.iter().enumerate() {
1079                            if i > 0 {
1080                                f.write_str(", ")?;
1081                            }
1082                            write!(f, "{}", quote_ident(t))?;
1083                        }
1084                        Ok(())
1085                    }
1086                }
1087            }
1088            Self::CreateExtension(name) => {
1089                write!(f, "CREATE EXTENSION IF NOT EXISTS {}", quote_ident(name))
1090            }
1091            Self::DropPublication(name) => {
1092                write!(f, "DROP PUBLICATION {}", quote_ident(name))
1093            }
1094        }
1095    }
1096}
1097
1098impl fmt::Display for CreateIndexStatement {
1099    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1100        f.write_str("CREATE INDEX ")?;
1101        if self.if_not_exists {
1102            f.write_str("IF NOT EXISTS ")?;
1103        }
1104        write!(
1105            f,
1106            "{} ON {} ",
1107            quote_ident(&self.name),
1108            quote_ident(&self.table)
1109        )?;
1110        match self.method {
1111            IndexMethod::Hnsw => f.write_str("USING hnsw ")?,
1112            IndexMethod::Brin => f.write_str("USING brin ")?,
1113            IndexMethod::BTree => {}
1114        }
1115        if let Some(expr) = &self.expression {
1116            write!(f, "({})", expr)?;
1117        } else {
1118            write!(f, "({})", quote_ident(&self.column))?;
1119        }
1120        if !self.included_columns.is_empty() {
1121            f.write_str(" INCLUDE (")?;
1122            for (i, c) in self.included_columns.iter().enumerate() {
1123                if i > 0 {
1124                    f.write_str(", ")?;
1125                }
1126                write!(f, "{}", quote_ident(c))?;
1127            }
1128            f.write_str(")")?;
1129        }
1130        if let Some(pred) = &self.partial_predicate {
1131            write!(f, " WHERE {}", pred)?;
1132        }
1133        Ok(())
1134    }
1135}
1136
1137impl fmt::Display for CreateTableStatement {
1138    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1139        f.write_str("CREATE TABLE ")?;
1140        if self.if_not_exists {
1141            f.write_str("IF NOT EXISTS ")?;
1142        }
1143        write!(f, "{} (", quote_ident(&self.name))?;
1144        for (i, col) in self.columns.iter().enumerate() {
1145            if i > 0 {
1146                f.write_str(", ")?;
1147            }
1148            write!(f, "{col}")?;
1149        }
1150        // v7.6.0 — render FK constraints in table-level form, after
1151        // the column list. WAL replay round-trips through Display, so
1152        // every FK must serialise here for replay to reconstruct the
1153        // schema bit-for-bit.
1154        for fk in &self.foreign_keys {
1155            f.write_str(", ")?;
1156            write!(f, "{fk}")?;
1157        }
1158        f.write_str(")")
1159    }
1160}
1161
1162impl fmt::Display for ForeignKeyConstraint {
1163    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1164        if let Some(name) = &self.name {
1165            write!(f, "CONSTRAINT {} ", quote_ident(name))?;
1166        }
1167        f.write_str("FOREIGN KEY (")?;
1168        for (i, c) in self.columns.iter().enumerate() {
1169            if i > 0 {
1170                f.write_str(", ")?;
1171            }
1172            f.write_str(&quote_ident(c))?;
1173        }
1174        write!(f, ") REFERENCES {}", quote_ident(&self.parent_table))?;
1175        if !self.parent_columns.is_empty() {
1176            f.write_str(" (")?;
1177            for (i, c) in self.parent_columns.iter().enumerate() {
1178                if i > 0 {
1179                    f.write_str(", ")?;
1180                }
1181                f.write_str(&quote_ident(c))?;
1182            }
1183            f.write_str(")")?;
1184        }
1185        // Only render non-default actions to keep Display output
1186        // close to user input. SPG's default is RESTRICT (matches
1187        // SQL spec).
1188        if self.on_delete != FkAction::Restrict {
1189            write!(f, " ON DELETE {}", self.on_delete)?;
1190        }
1191        if self.on_update != FkAction::Restrict {
1192            write!(f, " ON UPDATE {}", self.on_update)?;
1193        }
1194        Ok(())
1195    }
1196}
1197
1198impl fmt::Display for FkAction {
1199    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1200        match self {
1201            Self::Restrict => f.write_str("RESTRICT"),
1202            Self::Cascade => f.write_str("CASCADE"),
1203            Self::SetNull => f.write_str("SET NULL"),
1204            Self::SetDefault => f.write_str("SET DEFAULT"),
1205            Self::NoAction => f.write_str("NO ACTION"),
1206        }
1207    }
1208}
1209
1210impl fmt::Display for ColumnDef {
1211    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1212        write!(f, "{} {}", quote_ident(&self.name), self.ty)?;
1213        if let Some(d) = &self.default {
1214            write!(f, " DEFAULT {d}")?;
1215        }
1216        if self.auto_increment {
1217            f.write_str(" AUTO_INCREMENT")?;
1218        }
1219        if !self.nullable {
1220            f.write_str(" NOT NULL")?;
1221        }
1222        Ok(())
1223    }
1224}
1225
1226impl fmt::Display for InsertStatement {
1227    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1228        write!(f, "INSERT INTO {}", quote_ident(&self.table))?;
1229        if let Some(cols) = &self.columns {
1230            f.write_str(" (")?;
1231            for (i, c) in cols.iter().enumerate() {
1232                if i > 0 {
1233                    f.write_str(", ")?;
1234                }
1235                f.write_str(&quote_ident(c))?;
1236            }
1237            f.write_str(")")?;
1238        }
1239        f.write_str(" VALUES ")?;
1240        for (ri, row) in self.rows.iter().enumerate() {
1241            if ri > 0 {
1242                f.write_str(", ")?;
1243            }
1244            f.write_str("(")?;
1245            for (i, v) in row.iter().enumerate() {
1246                if i > 0 {
1247                    f.write_str(", ")?;
1248                }
1249                write!(f, "{v}")?;
1250            }
1251            f.write_str(")")?;
1252        }
1253        Ok(())
1254    }
1255}
1256
1257impl fmt::Display for UpdateStatement {
1258    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1259        write!(f, "UPDATE {} SET ", quote_ident(&self.table))?;
1260        for (i, (col, expr)) in self.assignments.iter().enumerate() {
1261            if i > 0 {
1262                f.write_str(", ")?;
1263            }
1264            write!(f, "{} = {expr}", quote_ident(col))?;
1265        }
1266        if let Some(w) = &self.where_ {
1267            write!(f, " WHERE {w}")?;
1268        }
1269        Ok(())
1270    }
1271}
1272
1273impl fmt::Display for DeleteStatement {
1274    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1275        write!(f, "DELETE FROM {}", quote_ident(&self.table))?;
1276        if let Some(w) = &self.where_ {
1277            write!(f, " WHERE {w}")?;
1278        }
1279        Ok(())
1280    }
1281}
1282
1283impl fmt::Display for SelectStatement {
1284    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1285        write_bare_select(self, f)?;
1286        for (kind, peer) in &self.unions {
1287            f.write_str(match kind {
1288                UnionKind::Distinct => " UNION ",
1289                UnionKind::All => " UNION ALL ",
1290            })?;
1291            write_bare_select(peer, f)?;
1292        }
1293        if !self.order_by.is_empty() {
1294            f.write_str(" ORDER BY ")?;
1295            for (i, o) in self.order_by.iter().enumerate() {
1296                if i > 0 {
1297                    f.write_str(", ")?;
1298                }
1299                write!(f, "{}", o.expr)?;
1300                if o.desc {
1301                    f.write_str(" DESC")?;
1302                }
1303            }
1304        }
1305        if let Some(n) = &self.limit {
1306            write!(f, " LIMIT {n}")?;
1307        }
1308        if let Some(o) = &self.offset {
1309            write!(f, " OFFSET {o}")?;
1310        }
1311        Ok(())
1312    }
1313}
1314
1315fn write_bare_select(s: &SelectStatement, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1316    f.write_str("SELECT ")?;
1317    if s.distinct {
1318        f.write_str("DISTINCT ")?;
1319    }
1320    write_bare_select_body(s, f)
1321}
1322
1323fn write_bare_select_body(s: &SelectStatement, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1324    for (i, item) in s.items.iter().enumerate() {
1325        if i > 0 {
1326            f.write_str(", ")?;
1327        }
1328        write!(f, "{item}")?;
1329    }
1330    if let Some(t) = &s.from {
1331        write!(f, " FROM {t}")?;
1332    }
1333    if let Some(e) = &s.where_ {
1334        write!(f, " WHERE {e}")?;
1335    }
1336    if let Some(gs) = &s.group_by {
1337        f.write_str(" GROUP BY ")?;
1338        for (i, g) in gs.iter().enumerate() {
1339            if i > 0 {
1340                f.write_str(", ")?;
1341            }
1342            write!(f, "{g}")?;
1343        }
1344    }
1345    if let Some(h) = &s.having {
1346        write!(f, " HAVING {h}")?;
1347    }
1348    Ok(())
1349}
1350
1351impl fmt::Display for SelectItem {
1352    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1353        match self {
1354            Self::Wildcard => f.write_str("*"),
1355            Self::Expr { expr, alias } => {
1356                write!(f, "{expr}")?;
1357                if let Some(a) = alias {
1358                    write!(f, " AS {}", quote_ident(a))?;
1359                }
1360                Ok(())
1361            }
1362        }
1363    }
1364}
1365
1366impl fmt::Display for FromClause {
1367    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1368        write!(f, "{}", self.primary)?;
1369        for j in &self.joins {
1370            match j.kind {
1371                JoinKind::Inner => write!(f, " INNER JOIN {}", j.table)?,
1372                JoinKind::Left => write!(f, " LEFT JOIN {}", j.table)?,
1373                JoinKind::Cross => write!(f, " CROSS JOIN {}", j.table)?,
1374            }
1375            if let Some(on) = &j.on {
1376                write!(f, " ON {on}")?;
1377            }
1378        }
1379        Ok(())
1380    }
1381}
1382
1383impl fmt::Display for TableRef {
1384    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1385        write!(f, "{}", quote_ident(&self.name))?;
1386        if let Some(a) = &self.alias {
1387            write!(f, " AS {}", quote_ident(a))?;
1388        }
1389        Ok(())
1390    }
1391}
1392
1393impl fmt::Display for ColumnName {
1394    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1395        if let Some(q) = &self.qualifier {
1396            write!(f, "{}.{}", quote_ident(q), quote_ident(&self.name))
1397        } else {
1398            write!(f, "{}", quote_ident(&self.name))
1399        }
1400    }
1401}
1402
1403impl fmt::Display for Expr {
1404    #[allow(clippy::too_many_lines)]
1405    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1406        match self {
1407            Self::Literal(l) => write!(f, "{l}"),
1408            Self::Column(c) => write!(f, "{c}"),
1409            Self::Placeholder(n) => write!(f, "${n}"),
1410            Self::Binary { lhs, op, rhs } => write!(f, "({lhs} {op} {rhs})"),
1411            Self::Unary { op, expr } => match op {
1412                UnOp::Not => write!(f, "(NOT {expr})"),
1413                UnOp::Neg => write!(f, "(-{expr})"),
1414            },
1415            Self::Cast { expr, target } => write!(f, "({expr}::{target})"),
1416            Self::IsNull { expr, negated } => {
1417                if *negated {
1418                    write!(f, "({expr} IS NOT NULL)")
1419                } else {
1420                    write!(f, "({expr} IS NULL)")
1421                }
1422            }
1423            Self::FunctionCall { name, args } => {
1424                write!(f, "{name}(")?;
1425                for (i, a) in args.iter().enumerate() {
1426                    if i > 0 {
1427                        f.write_str(", ")?;
1428                    }
1429                    write!(f, "{a}")?;
1430                }
1431                f.write_str(")")
1432            }
1433            Self::Like {
1434                expr,
1435                pattern,
1436                negated,
1437            } => {
1438                if *negated {
1439                    write!(f, "({expr} NOT LIKE {pattern})")
1440                } else {
1441                    write!(f, "({expr} LIKE {pattern})")
1442                }
1443            }
1444            Self::Extract { field, source } => write!(f, "EXTRACT({field} FROM {source})"),
1445            Self::WindowFunction {
1446                name,
1447                args,
1448                partition_by,
1449                order_by,
1450                frame,
1451                null_treatment: _,
1452            } => {
1453                write!(f, "{name}(")?;
1454                for (i, a) in args.iter().enumerate() {
1455                    if i > 0 {
1456                        f.write_str(", ")?;
1457                    }
1458                    write!(f, "{a}")?;
1459                }
1460                f.write_str(") OVER (")?;
1461                if !partition_by.is_empty() {
1462                    f.write_str("PARTITION BY ")?;
1463                    for (i, p) in partition_by.iter().enumerate() {
1464                        if i > 0 {
1465                            f.write_str(", ")?;
1466                        }
1467                        write!(f, "{p}")?;
1468                    }
1469                }
1470                if !order_by.is_empty() {
1471                    if !partition_by.is_empty() {
1472                        f.write_str(" ")?;
1473                    }
1474                    f.write_str("ORDER BY ")?;
1475                    for (i, (e, desc)) in order_by.iter().enumerate() {
1476                        if i > 0 {
1477                            f.write_str(", ")?;
1478                        }
1479                        write!(f, "{e}")?;
1480                        if *desc {
1481                            f.write_str(" DESC")?;
1482                        }
1483                    }
1484                }
1485                if let Some(fr) = frame {
1486                    if !partition_by.is_empty() || !order_by.is_empty() {
1487                        f.write_str(" ")?;
1488                    }
1489                    let k = match fr.kind {
1490                        FrameKind::Rows => "ROWS",
1491                        FrameKind::Range => "RANGE",
1492                    };
1493                    if let Some(end) = &fr.end {
1494                        write!(f, "{k} BETWEEN {} AND {}", fr.start, end)?;
1495                    } else {
1496                        write!(f, "{k} {}", fr.start)?;
1497                    }
1498                }
1499                f.write_str(")")
1500            }
1501            Self::ScalarSubquery(s) => write!(f, "({s})"),
1502            Self::Exists { subquery, negated } => {
1503                if *negated {
1504                    write!(f, "NOT EXISTS ({subquery})")
1505                } else {
1506                    write!(f, "EXISTS ({subquery})")
1507                }
1508            }
1509            Self::InSubquery {
1510                expr,
1511                subquery,
1512                negated,
1513            } => {
1514                if *negated {
1515                    write!(f, "({expr} NOT IN ({subquery}))")
1516                } else {
1517                    write!(f, "({expr} IN ({subquery}))")
1518                }
1519            }
1520        }
1521    }
1522}
1523
1524impl fmt::Display for Literal {
1525    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1526        match self {
1527            Self::Integer(n) => write!(f, "{n}"),
1528            Self::Float(x) => {
1529                let s = format!("{x}");
1530                // Default Display for an integral f64 (e.g. 1.0) emits "1",
1531                // which would round-trip back to Integer. Force a dot.
1532                if s.contains('.') || s.contains('e') || s.contains('E') {
1533                    f.write_str(&s)
1534                } else {
1535                    write!(f, "{s}.0")
1536                }
1537            }
1538            Self::String(s) => {
1539                f.write_str("'")?;
1540                for c in s.chars() {
1541                    if c == '\'' {
1542                        f.write_str("''")?;
1543                    } else {
1544                        write!(f, "{c}")?;
1545                    }
1546                }
1547                f.write_str("'")
1548            }
1549            Self::Bool(b) => f.write_str(if *b { "TRUE" } else { "FALSE" }),
1550            Self::Null => f.write_str("NULL"),
1551            Self::Vector(v) => {
1552                f.write_str("[")?;
1553                for (i, x) in v.iter().enumerate() {
1554                    if i > 0 {
1555                        f.write_str(", ")?;
1556                    }
1557                    let s = format!("{x}");
1558                    // Mirror Float Display: force a dot so re-parse stays
1559                    // numerically literal.
1560                    if s.contains('.') || s.contains('e') || s.contains('E') {
1561                        f.write_str(&s)?;
1562                    } else {
1563                        write!(f, "{s}.0")?;
1564                    }
1565                }
1566                f.write_str("]")
1567            }
1568            Self::Interval { text, .. } => {
1569                f.write_str("INTERVAL '")?;
1570                for c in text.chars() {
1571                    if c == '\'' {
1572                        f.write_str("''")?;
1573                    } else {
1574                        write!(f, "{c}")?;
1575                    }
1576                }
1577                f.write_str("'")
1578            }
1579        }
1580    }
1581}
1582
1583impl fmt::Display for BinOp {
1584    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1585        f.write_str(match self {
1586            Self::Or => "OR",
1587            Self::And => "AND",
1588            Self::Eq => "=",
1589            Self::NotEq => "<>",
1590            Self::Lt => "<",
1591            Self::LtEq => "<=",
1592            Self::Gt => ">",
1593            Self::GtEq => ">=",
1594            Self::Add => "+",
1595            Self::Sub => "-",
1596            Self::Mul => "*",
1597            Self::Div => "/",
1598            Self::L2Distance => "<->",
1599            Self::InnerProduct => "<#>",
1600            Self::CosineDistance => "<=>",
1601            Self::Concat => "||",
1602            Self::JsonGet => "->",
1603            Self::JsonGetText => "->>",
1604            Self::JsonGetPath => "#>",
1605            Self::JsonGetPathText => "#>>",
1606            Self::JsonContains => "@>",
1607        })
1608    }
1609}
1610
1611/// Quote `s` as a PG double-quoted identifier when required (keyword,
1612/// non-folded case, leading digit, embedded non-`[A-Za-z0-9_]`, empty).
1613/// Otherwise return it as-is. Returns an owned `String` to keep the call site
1614/// uniform.
1615fn quote_ident(s: &str) -> String {
1616    let needs_quote = match s.chars().next() {
1617        None => true,
1618        Some(c) if !c.is_ascii_alphabetic() && c != '_' => true,
1619        _ => {
1620            s.chars().any(|c| !(c.is_ascii_alphanumeric() || c == '_'))
1621                || s.chars().any(|c| c.is_ascii_uppercase())
1622                || is_keyword(s)
1623        }
1624    };
1625    if !needs_quote {
1626        return s.to_string();
1627    }
1628    let mut out = String::with_capacity(s.len() + 2);
1629    out.push('"');
1630    for c in s.chars() {
1631        if c == '"' {
1632            out.push_str("\"\"");
1633        } else {
1634            out.push(c);
1635        }
1636    }
1637    out.push('"');
1638    out
1639}
1640
1641fn is_keyword(s: &str) -> bool {
1642    matches!(
1643        &*s.to_ascii_lowercase(),
1644        "select"
1645            | "from"
1646            | "where"
1647            | "as"
1648            | "null"
1649            | "true"
1650            | "false"
1651            | "and"
1652            | "or"
1653            | "not"
1654            | "create"
1655            | "table"
1656            | "insert"
1657            | "into"
1658            | "values"
1659            | "index"
1660            | "on"
1661            | "begin"
1662            | "commit"
1663            | "rollback"
1664            | "is"
1665            | "between"
1666            | "in"
1667            | "like"
1668            | "group"
1669            | "distinct"
1670            | "union"
1671            | "all"
1672            | "join"
1673            | "inner"
1674            | "left"
1675            | "cross"
1676            | "outer"
1677            | "default"
1678            | "savepoint"
1679            | "release"
1680            | "to"
1681            | "having"
1682            | "show"
1683            | "extract"
1684            | "offset"
1685            | "asc"
1686            | "desc"
1687            | "interval"
1688    )
1689}
1690
1691#[cfg(test)]
1692mod tests {
1693    use super::*;
1694    use alloc::vec;
1695
1696    #[test]
1697    fn integer_literal_renders_without_dot() {
1698        assert_eq!(Literal::Integer(42).to_string(), "42");
1699    }
1700
1701    #[test]
1702    fn integral_float_keeps_dot() {
1703        assert_eq!(Literal::Float(1.0).to_string(), "1.0");
1704        assert_eq!(Literal::Float(1.5).to_string(), "1.5");
1705        assert_eq!(Literal::Float(2.5e-3).to_string(), "0.0025");
1706    }
1707
1708    #[test]
1709    fn string_literal_doubles_quote() {
1710        assert_eq!(Literal::String("it's".into()).to_string(), "'it''s'");
1711    }
1712
1713    #[test]
1714    fn bool_and_null_render_uppercase() {
1715        assert_eq!(Literal::Bool(true).to_string(), "TRUE");
1716        assert_eq!(Literal::Bool(false).to_string(), "FALSE");
1717        assert_eq!(Literal::Null.to_string(), "NULL");
1718    }
1719
1720    #[test]
1721    fn binary_op_always_parenthesised() {
1722        let e = Expr::Binary {
1723            lhs: Box::new(Expr::Literal(Literal::Integer(1))),
1724            op: BinOp::Add,
1725            rhs: Box::new(Expr::Literal(Literal::Integer(2))),
1726        };
1727        assert_eq!(e.to_string(), "(1 + 2)");
1728    }
1729
1730    #[test]
1731    fn select_star_from_table() {
1732        let s = SelectStatement {
1733            items: vec![SelectItem::Wildcard],
1734            from: Some(FromClause {
1735                primary: TableRef {
1736                    name: "users".into(),
1737                    alias: None,
1738                    as_of_segment: None,
1739                },
1740                joins: vec![],
1741            }),
1742            where_: None,
1743            group_by: None,
1744            group_by_all: false,
1745            having: None,
1746            unions: vec![],
1747            order_by: Vec::new(),
1748            limit: None,
1749            offset: None,
1750            distinct: false,
1751            ctes: vec![],
1752        };
1753        assert_eq!(s.to_string(), "SELECT * FROM users");
1754    }
1755
1756    #[test]
1757    fn quote_ident_for_uppercase_and_keyword() {
1758        assert_eq!(quote_ident("foo"), "foo");
1759        assert_eq!(quote_ident("Foo"), "\"Foo\"");
1760        assert_eq!(quote_ident("select"), "\"select\"");
1761        assert_eq!(quote_ident(""), "\"\"");
1762        assert_eq!(quote_ident("a\"b"), "\"a\"\"b\"");
1763    }
1764}