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