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