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