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