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}
508
509impl fmt::Display for ColumnTypeName {
510 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
511 match self {
512 Self::SmallInt => f.write_str("SMALLINT"),
513 Self::Int => f.write_str("INT"),
514 Self::BigInt => f.write_str("BIGINT"),
515 Self::Float => f.write_str("FLOAT"),
516 Self::Text => f.write_str("TEXT"),
517 Self::Varchar(n) => write!(f, "VARCHAR({n})"),
518 Self::Char(n) => write!(f, "CHAR({n})"),
519 Self::Bool => f.write_str("BOOL"),
520 Self::Vector { dim, encoding } => match encoding {
521 VecEncoding::F32 => write!(f, "VECTOR({dim})"),
522 VecEncoding::Sq8 => write!(f, "VECTOR({dim}) USING SQ8"),
523 VecEncoding::F16 => write!(f, "VECTOR({dim}) USING HALF"),
524 },
525 Self::Json => f.write_str("JSON"),
526 Self::Jsonb => f.write_str("JSONB"),
527 Self::Bytes => f.write_str("BYTEA"),
528 Self::TextArray => f.write_str("TEXT[]"),
529 Self::Numeric(p, s) => {
530 if *s == 0 {
531 write!(f, "NUMERIC({p})")
532 } else {
533 write!(f, "NUMERIC({p}, {s})")
534 }
535 }
536 Self::Date => f.write_str("DATE"),
537 Self::Timestamp => f.write_str("TIMESTAMP"),
538 Self::Timestamptz => f.write_str("TIMESTAMPTZ"),
539 }
540 }
541}
542
543/// `UPDATE <table> SET col = expr [, ...] [WHERE cond]`. v4.4 — the
544/// engine evaluates `expr` per matched row in the table's row order
545/// and rewrites cells in place. Indexed columns are dropped + re-
546/// inserted into the affected B-tree on each row change.
547#[derive(Debug, Clone, PartialEq)]
548pub struct UpdateStatement {
549 pub table: String,
550 pub assignments: Vec<(String, Expr)>,
551 pub where_: Option<Expr>,
552 /// v7.9.4 — `RETURNING <projection>`. None = no RETURNING
553 /// clause (legacy CommandComplete path). Some = engine
554 /// evaluates the projection over each mutated row and
555 /// streams the result as a Rows QueryResult.
556 pub returning: Option<Vec<SelectItem>>,
557}
558
559/// `DELETE FROM <table> [WHERE cond]`. v4.4 — removes matched rows
560/// from the active catalog and prunes them from every index.
561#[derive(Debug, Clone, PartialEq)]
562pub struct DeleteStatement {
563 pub table: String,
564 pub where_: Option<Expr>,
565 /// v7.9.4 — `RETURNING <projection>`.
566 pub returning: Option<Vec<SelectItem>>,
567}
568
569#[derive(Debug, Clone, PartialEq)]
570pub struct InsertStatement {
571 pub table: String,
572 /// Optional column list — `INSERT INTO t (a, b) VALUES (...)`. When
573 /// `None`, every tuple is positional and must match the table arity.
574 /// When `Some`, the engine maps each tuple slot to the named column and
575 /// fills the rest with NULL (must be nullable).
576 pub columns: Option<Vec<String>>,
577 /// One or more `(expr, expr, ...)` tuples — the multi-row VALUES form.
578 /// v1.3+ accepts `INSERT INTO t VALUES (a), (b)`.
579 pub rows: Vec<Vec<Expr>>,
580 /// v7.9.7 — `ON CONFLICT (cols) DO { NOTHING | UPDATE SET … }`
581 /// upsert clause. None = legacy INSERT (conflict raises a
582 /// DuplicateKey error). mailrs migration blocker #2.
583 pub on_conflict: Option<OnConflictClause>,
584 /// v7.9.4 — `RETURNING <projection>`.
585 pub returning: Option<Vec<SelectItem>>,
586}
587
588/// v7.9.7 — INSERT upsert clause: `ON CONFLICT (target) DO action`.
589#[derive(Debug, Clone, PartialEq)]
590pub struct OnConflictClause {
591 /// Local columns that identify the conflict (must match a
592 /// UNIQUE / PRIMARY KEY index on the target table). Empty
593 /// list means the user wrote `ON CONFLICT DO …` without a
594 /// target — engine picks the table's first BTree index by
595 /// convention.
596 pub target_columns: Vec<String>,
597 /// The action on conflict.
598 pub action: OnConflictAction,
599}
600
601/// v7.9.7 — action on conflict.
602#[derive(Debug, Clone, PartialEq)]
603pub enum OnConflictAction {
604 /// `DO NOTHING` — INSERT proceeds for non-conflicting rows,
605 /// silently skips conflicting ones.
606 Nothing,
607 /// `DO UPDATE SET col = expr [, …] [WHERE cond]`. `assignments`
608 /// may reference `EXCLUDED.col` to read the incoming row's
609 /// value (engine wires `EXCLUDED` as a virtual table).
610 Update {
611 assignments: Vec<(String, Expr)>,
612 where_: Option<Expr>,
613 },
614}
615
616#[derive(Debug, Clone, PartialEq)]
617pub struct SelectStatement {
618 /// v4.11: `WITH name AS (SELECT ...) [, ...]` common-table
619 /// expressions, materialised once at query start before the
620 /// body SELECT runs. Empty for a regular SELECT. Non-recursive
621 /// only — no `WITH RECURSIVE` for v4.x.
622 pub ctes: Vec<Cte>,
623 pub distinct: bool,
624 pub items: Vec<SelectItem>,
625 pub from: Option<FromClause>,
626 pub where_: Option<Expr>,
627 pub group_by: Option<Vec<Expr>>,
628 /// v6.4.1 — `GROUP BY ALL` shortcut: when true, the planner
629 /// expands `group_by` to every non-aggregate SELECT-list item
630 /// before the executor runs. Mutually exclusive with an
631 /// explicit `group_by` list (the parser sets exactly one).
632 pub group_by_all: bool,
633 /// `HAVING <expr>` — filter applied *after* `GROUP BY` aggregation.
634 /// Supports aggregate calls (e.g. `HAVING count(*) > 1`); the
635 /// aggregate executor resolves them through the same synthetic
636 /// schema used for the SELECT items.
637 pub having: Option<Expr>,
638 /// UNION / UNION ALL chain. Empty for a plain SELECT. Each peer is
639 /// itself a `SelectStatement` with `order_by = None` and `limit =
640 /// None` (the parser enforces that — ORDER BY / LIMIT belong to the
641 /// top of the chain).
642 pub unions: Vec<(UnionKind, SelectStatement)>,
643 /// v6.4.0 — multi-key ORDER BY. Empty `Vec` means no ORDER BY.
644 /// Keys are matched left-to-right: first key decides, ties break
645 /// to the second, etc.
646 pub order_by: Vec<OrderBy>,
647 /// `LIMIT <n>` — bound on row output. `n` is an integer
648 /// literal **or** (v7.9.24) a placeholder `$N` resolved
649 /// against the prepared-statement Bind values. mailrs
650 /// migration follow-up H2.
651 pub limit: Option<LimitExpr>,
652 /// `OFFSET <n>` — drop the first `n` rows after ORDER BY but
653 /// before LIMIT (so `LIMIT 10 OFFSET 5` keeps rows 6..=15).
654 pub offset: Option<LimitExpr>,
655}
656
657/// v7.9.24 — LIMIT / OFFSET value. Integer literal at parse
658/// time or a placeholder `$N` resolved during extended-query
659/// Bind. mailrs migration follow-up H2.
660#[derive(Debug, Clone, Copy, PartialEq, Eq)]
661pub enum LimitExpr {
662 /// `LIMIT 10` — value known at parse time.
663 Literal(u32),
664 /// `LIMIT $N` — the 1-based parameter index, resolved against
665 /// the bind values when the prepared statement executes.
666 Placeholder(u16),
667}
668
669impl fmt::Display for LimitExpr {
670 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
671 match self {
672 Self::Literal(n) => write!(f, "{n}"),
673 Self::Placeholder(n) => write!(f, "${n}"),
674 }
675 }
676}
677
678impl LimitExpr {
679 /// Convenience for the simple-query path where no placeholders
680 /// can possibly exist. Returns the literal value or `None` if
681 /// this is a placeholder (caller must surface as Unsupported).
682 pub fn as_literal(self) -> Option<u32> {
683 match self {
684 Self::Literal(n) => Some(n),
685 Self::Placeholder(_) => None,
686 }
687 }
688}
689
690/// v7.9.24 — extract LIMIT / OFFSET as a `u32` literal. After
691/// the engine's `substitute_placeholders` pass these are
692/// always Literal; in the simple-query path a Placeholder
693/// shape returns None (executor surfaces as
694/// "LIMIT/OFFSET ${n} requires prepared-statement binding").
695impl SelectStatement {
696 #[must_use]
697 pub fn limit_literal(&self) -> Option<u32> {
698 self.limit.and_then(LimitExpr::as_literal)
699 }
700 #[must_use]
701 pub fn offset_literal(&self) -> Option<u32> {
702 self.offset.and_then(LimitExpr::as_literal)
703 }
704}
705
706#[derive(Debug, Clone, PartialEq)]
707pub struct Cte {
708 pub name: String,
709 pub body: SelectStatement,
710 /// v4.22: `WITH RECURSIVE` — set when the WITH clause had the
711 /// RECURSIVE keyword. Applies to every CTE in the clause per
712 /// PG semantics. A non-recursive body in a RECURSIVE WITH is
713 /// allowed; the engine just runs it once.
714 pub recursive: bool,
715 /// v4.22: optional `WITH name(a, b, c)` column-name list. When
716 /// non-empty, these override the body's output column names
717 /// position-by-position; the engine errors out if the count
718 /// doesn't match the body's projection width.
719 pub column_overrides: Vec<String>,
720}
721
722#[derive(Debug, Clone, PartialEq)]
723pub struct OrderBy {
724 pub expr: Expr,
725 /// `false` = ASC (default), `true` = DESC.
726 pub desc: bool,
727}
728
729#[derive(Debug, Clone, Copy, PartialEq, Eq)]
730pub enum UnionKind {
731 /// `UNION` — dedupes the combined set.
732 Distinct,
733 /// `UNION ALL` — concatenates without dedup.
734 All,
735}
736
737#[derive(Debug, Clone, PartialEq)]
738pub enum SelectItem {
739 Wildcard,
740 Expr { expr: Expr, alias: Option<String> },
741}
742
743#[derive(Debug, Clone, PartialEq)]
744pub struct TableRef {
745 pub name: String,
746 pub alias: Option<String>,
747 /// v6.10.2 — `AS OF SEGMENT '<id>'` cold-tier time-travel.
748 /// When `Some(id)`, the scan restricts to rows that live in
749 /// segment `<id>` only — useful for forensic inspection of a
750 /// specific freezer-emitted segment without exposing the hot
751 /// tier. `AS OF TIMESTAMP <ts>` (PG-flavoured time travel)
752 /// is STABILITY carve-out for v6.10 — needs the freezer to
753 /// stamp each segment with a wall-clock at creation time.
754 pub as_of_segment: Option<u32>,
755 /// v7.11.7 — `FROM unnest(<expr>) [AS] <alias>` set-returning
756 /// source. When `Some`, `name` is the alias (defaulting to
757 /// `"unnest"` when no `AS` is given) and the engine builds a
758 /// synthetic single-column table by evaluating the expression
759 /// once at SELECT entry. Each TEXT[] element becomes one row;
760 /// NULL elements become NULL cells. v7.11 supports
761 /// uncorrelated UNNEST only (the expr cannot reference outer
762 /// columns) and only as the FROM primary (no JOINs).
763 pub unnest_expr: Option<Box<Expr>>,
764}
765
766/// FROM clause shape. v1.10 accepts a primary table plus a flat list of
767/// joined peers — `FROM a [, b]* [INNER|LEFT] JOIN c ON expr ...`. The
768/// joins evaluate left-associatively in nested-loop order.
769#[derive(Debug, Clone, PartialEq)]
770pub struct FromClause {
771 pub primary: TableRef,
772 pub joins: Vec<FromJoin>,
773}
774
775#[derive(Debug, Clone, PartialEq)]
776pub struct FromJoin {
777 pub kind: JoinKind,
778 pub table: TableRef,
779 /// Required for INNER/LEFT; must be `None` for CROSS / comma-list.
780 pub on: Option<Expr>,
781}
782
783#[derive(Debug, Clone, Copy, PartialEq, Eq)]
784pub enum JoinKind {
785 Inner,
786 Left,
787 Cross,
788}
789
790#[derive(Debug, Clone, PartialEq)]
791pub enum Expr {
792 Literal(Literal),
793 Column(ColumnName),
794 /// v6.1.1 — `$N` parameter placeholder for the extended query
795 /// protocol. The number is 1-based per PostgreSQL convention.
796 /// Evaluation looks up `params[N-1]` from the prepared-statement
797 /// bind buffer; out-of-range indices raise a runtime error
798 /// (same shape as a column-not-found miss).
799 Placeholder(u16),
800 Binary {
801 lhs: Box<Expr>,
802 op: BinOp,
803 rhs: Box<Expr>,
804 },
805 Unary {
806 op: UnOp,
807 expr: Box<Expr>,
808 },
809 /// PG-style `expr::TYPE` cast. v1.3 supports VECTOR, INT, BIGINT, FLOAT,
810 /// TEXT, BOOL targets; engine coerces at evaluation time.
811 Cast {
812 expr: Box<Expr>,
813 target: CastTarget,
814 },
815 /// Postfix `IS NULL` / `IS NOT NULL`. Returns BOOL.
816 IsNull {
817 expr: Box<Expr>,
818 negated: bool,
819 },
820 /// Function call `name(args...)`. v1.4 supports a small built-in set
821 /// (length, upper, lower, abs, coalesce); unknown names error at eval
822 /// time so the parser stays open for v1.5 aggregates.
823 FunctionCall {
824 name: String,
825 args: Vec<Expr>,
826 },
827 /// SQL `LIKE` predicate. `pattern` evaluates to text at runtime;
828 /// wildcards are `%` (any run) and `_` (one char), backslash escapes
829 /// the next char (so `\%` matches a literal `%`).
830 Like {
831 expr: Box<Expr>,
832 pattern: Box<Expr>,
833 negated: bool,
834 },
835 /// v4.12 window function call: `name(args) OVER (PARTITION BY
836 /// ... ORDER BY ...)`. Supports `ROW_NUMBER` / `RANK` /
837 /// `DENSE_RANK` and the partition-aware aggregates `SUM` /
838 /// `AVG` / `COUNT` / `MIN` / `MAX`. The window frame defaults to "entire partition" for
839 /// unordered windows and "from start of partition through
840 /// current row" for ordered windows — no explicit ROWS /
841 /// RANGE clause in v4.12 MVP.
842 WindowFunction {
843 name: String,
844 args: Vec<Expr>,
845 partition_by: Vec<Expr>,
846 order_by: Vec<(Expr, bool /* desc */)>,
847 /// v4.20 explicit frame. `None` means "use the default":
848 /// whole-partition when unordered, running aggregate from
849 /// partition start through current row when ordered.
850 frame: Option<WindowFrame>,
851 /// v6.4.2 — `IGNORE NULLS` / `RESPECT NULLS` modifier on
852 /// LAG / LEAD / FIRST_VALUE / LAST_VALUE. Default is
853 /// `Respect` (PG / ANSI default — NULLs participate). Other
854 /// window functions ignore this flag.
855 null_treatment: NullTreatment,
856 },
857 /// v4.10 scalar subquery — `(SELECT ...)` used in expression
858 /// position. Must return exactly one row × one column at eval
859 /// time; the engine errors out otherwise. Uncorrelated only —
860 /// the inner SELECT cannot reference outer columns.
861 ScalarSubquery(Box<SelectStatement>),
862 /// v4.10 `[NOT] EXISTS (SELECT ...)`. Returns Bool. Inner
863 /// projection is ignored; only row-count matters.
864 Exists {
865 subquery: Box<SelectStatement>,
866 negated: bool,
867 },
868 /// v4.10 `expr [NOT] IN (SELECT ...)`. Inner SELECT must
869 /// project exactly one column; membership is tested by Eq
870 /// against each row's value (NULL handling follows ANSI:
871 /// NULL ∈ list ⇒ NULL ; otherwise present ⇒ true).
872 InSubquery {
873 expr: Box<Expr>,
874 subquery: Box<SelectStatement>,
875 negated: bool,
876 },
877 /// `EXTRACT(<field> FROM <source>)` — pull an integer component
878 /// out of a `DATE` or `TIMESTAMP`. Parsed as its own AST node
879 /// because the `FROM` keyword is what separates the two halves,
880 /// not a comma.
881 Extract {
882 field: ExtractField,
883 source: Box<Expr>,
884 },
885 /// v7.10.10 — `ARRAY[expr, expr, …]` array constructor. Each
886 /// element is evaluated independently; NULLs are allowed.
887 /// v7.10 supports only single-dimension TEXT[] semantically;
888 /// non-text elements coerce at engine evaluation time when
889 /// the surrounding context (column type / cast) makes the
890 /// target clear.
891 Array(Vec<Expr>),
892 /// v7.10.10 — array subscript `arr[i]`. PG 1-based; the
893 /// engine returns NULL for out-of-range indices.
894 ArraySubscript {
895 target: Box<Expr>,
896 index: Box<Expr>,
897 },
898 /// v7.10.12 — `expr op ANY(arr)` and `expr op ALL(arr)`. The
899 /// operator is the comparison binary op (Eq / Ne / Lt / …);
900 /// the engine desugars: `ANY` returns true if any element
901 /// satisfies; `ALL` returns true only if every element does.
902 /// NULL handling follows PG's three-valued logic.
903 AnyAll {
904 expr: Box<Expr>,
905 op: BinOp,
906 array: Box<Expr>,
907 /// `true` = ANY, `false` = ALL.
908 is_any: bool,
909 },
910}
911
912/// v6.4.2 — null treatment on `LAG` / `LEAD` / `FIRST_VALUE` /
913/// `LAST_VALUE`. PG / ANSI default is `Respect` — NULLs participate
914/// in the offset walk. `Ignore` causes the function to skip NULL
915/// values in the argument expression, returning the next non-NULL.
916#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
917pub enum NullTreatment {
918 #[default]
919 Respect,
920 Ignore,
921}
922
923/// v4.20 explicit window frame: `ROWS|RANGE BETWEEN <bound> AND
924/// <bound>`. `end` is `None` for the shorthand "ROWS <bound>"
925/// where end implicitly = CURRENT ROW.
926#[derive(Debug, Clone, PartialEq, Eq)]
927pub struct WindowFrame {
928 pub kind: FrameKind,
929 pub start: FrameBound,
930 pub end: Option<FrameBound>,
931}
932
933#[derive(Debug, Clone, Copy, PartialEq, Eq)]
934pub enum FrameKind {
935 Rows,
936 Range,
937}
938
939#[derive(Debug, Clone, PartialEq, Eq)]
940pub enum FrameBound {
941 UnboundedPreceding,
942 OffsetPreceding(u64),
943 CurrentRow,
944 OffsetFollowing(u64),
945 UnboundedFollowing,
946}
947
948impl fmt::Display for FrameBound {
949 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
950 match self {
951 Self::UnboundedPreceding => f.write_str("UNBOUNDED PRECEDING"),
952 Self::OffsetPreceding(n) => write!(f, "{n} PRECEDING"),
953 Self::CurrentRow => f.write_str("CURRENT ROW"),
954 Self::OffsetFollowing(n) => write!(f, "{n} FOLLOWING"),
955 Self::UnboundedFollowing => f.write_str("UNBOUNDED FOLLOWING"),
956 }
957 }
958}
959
960#[derive(Debug, Clone, Copy, PartialEq, Eq)]
961pub enum ExtractField {
962 Year,
963 Month,
964 Day,
965 Hour,
966 Minute,
967 Second,
968 Microsecond,
969}
970
971impl fmt::Display for ExtractField {
972 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
973 f.write_str(match self {
974 Self::Year => "YEAR",
975 Self::Month => "MONTH",
976 Self::Day => "DAY",
977 Self::Hour => "HOUR",
978 Self::Minute => "MINUTE",
979 Self::Second => "SECOND",
980 Self::Microsecond => "MICROSECOND",
981 })
982 }
983}
984
985#[derive(Debug, Clone, Copy, PartialEq, Eq)]
986pub enum CastTarget {
987 Int,
988 BigInt,
989 Float,
990 Text,
991 Bool,
992 Vector,
993 Date,
994 Timestamp,
995 /// v7.9.25 — `::INTERVAL` and `::TIMESTAMPTZ`. mailrs follow-up
996 /// H3a. Engine reuses the existing runtime-interval / timestamp
997 /// paths (parse the text input, return the matching Value).
998 Interval,
999 Timestamptz,
1000 /// v7.9.25 — `::JSON` and `::JSONB`. SPG already has both
1001 /// types (v7.9.0); the cast just routes Text→Json with the
1002 /// requested OID for the wire layer.
1003 Json,
1004 Jsonb,
1005 /// v7.9.26 — `::regtype` / `::regclass`. Parsed for PG dump
1006 /// compatibility; engine surfaces as Unsupported with a
1007 /// hint to use `SHOW TABLES` or `spg_table_ddl`. mailrs F3b.
1008 RegType,
1009 RegClass,
1010 /// v7.10.11 — `::TEXT[]`. Engine decodes the LHS Text into
1011 /// the PG external array form `{a,b,NULL}`.
1012 TextArray,
1013}
1014
1015impl fmt::Display for CastTarget {
1016 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1017 f.write_str(match self {
1018 Self::Int => "int",
1019 Self::BigInt => "bigint",
1020 Self::Float => "float",
1021 Self::Text => "text",
1022 Self::Bool => "bool",
1023 Self::Vector => "vector",
1024 Self::Interval => "interval",
1025 Self::Timestamptz => "timestamptz",
1026 Self::Json => "json",
1027 Self::Jsonb => "jsonb",
1028 Self::RegType => "regtype",
1029 Self::RegClass => "regclass",
1030 Self::Date => "date",
1031 Self::Timestamp => "timestamp",
1032 Self::TextArray => "TEXT[]",
1033 })
1034 }
1035}
1036
1037#[derive(Debug, Clone, PartialEq)]
1038pub enum Literal {
1039 Integer(i64),
1040 Float(f64),
1041 String(String),
1042 Bool(bool),
1043 Null,
1044 /// pgvector-style array literal, e.g. `[1, 2.5, -3]`.
1045 Vector(Vec<f32>),
1046 /// `INTERVAL '<n> <unit> [<n> <unit> ...]'` — calendar-aware span.
1047 /// Split into a months part (because a month is not a fixed number of
1048 /// days) and a microseconds part (everything sub-month). `text` keeps
1049 /// the original spelling so Display round-trips byte-for-byte.
1050 Interval {
1051 months: i32,
1052 micros: i64,
1053 text: String,
1054 },
1055}
1056
1057#[derive(Debug, Clone, PartialEq, Eq)]
1058pub struct ColumnName {
1059 pub qualifier: Option<String>,
1060 pub name: String,
1061}
1062
1063#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1064pub enum BinOp {
1065 Or,
1066 And,
1067 Eq,
1068 NotEq,
1069 /// v7.9.27b — PG `a IS DISTINCT FROM b` / `a IS NOT DISTINCT
1070 /// FROM b`. NULL-safe equality: NULL IS NOT DISTINCT FROM
1071 /// NULL → true, NULL IS DISTINCT FROM NULL → false. The
1072 /// non-NULL behaviour matches `<>` / `=` exactly. Common in
1073 /// PG-style JOIN ON predicates and pg_dump output.
1074 IsDistinctFrom,
1075 IsNotDistinctFrom,
1076 Lt,
1077 LtEq,
1078 Gt,
1079 GtEq,
1080 Add,
1081 Sub,
1082 Mul,
1083 Div,
1084 /// pgvector L2 (Euclidean) distance `<->`. Defined for two vector
1085 /// operands of equal dimension; engine returns `Value::Float(d)`.
1086 L2Distance,
1087 /// pgvector inner-product `<#>` — returns `-Σ aᵢ bᵢ` so "smaller =
1088 /// more similar" remains true (matches pgvector's published convention).
1089 InnerProduct,
1090 /// pgvector cosine distance `<=>` — `1 - (a·b)/(|a| |b|)`.
1091 CosineDistance,
1092 /// SQL string concatenation `||`. NULL propagates.
1093 Concat,
1094 /// v4.14 `json -> key` — element access by string key (object)
1095 /// or integer index (array). Returns a JSON value.
1096 JsonGet,
1097 /// v4.14 `json ->> key` — same access, returns the result as
1098 /// TEXT (unwraps a top-level JSON string; renders other scalars
1099 /// as their canonical text).
1100 JsonGetText,
1101 /// v6.4.5 `json #> path_text` — walk the path encoded as a PG
1102 /// text array literal like `'{a,0,b}'`. Returns JSON.
1103 JsonGetPath,
1104 /// v6.4.5 `json #>> path_text` — same walk, returns TEXT.
1105 JsonGetPathText,
1106 /// v6.4.5 `json @> sub_json` — containment. Returns BOOL; true
1107 /// when every key/value in `sub_json` is structurally present in
1108 /// the left side. Matches PG semantics (top-level + recursive).
1109 JsonContains,
1110}
1111
1112#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1113pub enum UnOp {
1114 Not,
1115 Neg,
1116}
1117
1118// --- Display impls (round-trip-safe) --------------------------------------
1119
1120impl fmt::Display for Statement {
1121 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1122 match self {
1123 Self::Select(s) => s.fmt(f),
1124 Self::CreateTable(s) => s.fmt(f),
1125 Self::CreateIndex(s) => s.fmt(f),
1126 Self::Insert(s) => s.fmt(f),
1127 Self::Update(s) => s.fmt(f),
1128 Self::Delete(s) => s.fmt(f),
1129 Self::Begin => f.write_str("BEGIN"),
1130 Self::Commit => f.write_str("COMMIT"),
1131 Self::Rollback => f.write_str("ROLLBACK"),
1132 Self::Savepoint(n) => write!(f, "SAVEPOINT {}", quote_ident(n)),
1133 Self::RollbackToSavepoint(n) => write!(f, "ROLLBACK TO SAVEPOINT {}", quote_ident(n)),
1134 Self::ReleaseSavepoint(n) => write!(f, "RELEASE SAVEPOINT {}", quote_ident(n)),
1135 Self::ShowTables => f.write_str("SHOW TABLES"),
1136 Self::ShowColumns(t) => write!(f, "SHOW COLUMNS FROM {}", quote_ident(t)),
1137 Self::CreateUser(s) => write!(
1138 f,
1139 "CREATE USER {} WITH PASSWORD '<redacted>' ROLE '{}'",
1140 quote_ident(&s.name),
1141 s.role
1142 ),
1143 Self::DropUser(n) => write!(f, "DROP USER {}", quote_ident(n)),
1144 Self::ShowUsers => f.write_str("SHOW USERS"),
1145 Self::ShowPublications => f.write_str("SHOW PUBLICATIONS"),
1146 Self::ShowSubscriptions => f.write_str("SHOW SUBSCRIPTIONS"),
1147 Self::CreateSubscription(s) => {
1148 write!(
1149 f,
1150 "CREATE SUBSCRIPTION {} CONNECTION '{}' PUBLICATION ",
1151 quote_ident(&s.name),
1152 s.conn_str.replace('\'', "''")
1153 )?;
1154 for (i, p) in s.publications.iter().enumerate() {
1155 if i > 0 {
1156 f.write_str(", ")?;
1157 }
1158 write!(f, "{}", quote_ident(p))?;
1159 }
1160 Ok(())
1161 }
1162 Self::DropSubscription(name) => {
1163 write!(f, "DROP SUBSCRIPTION {}", quote_ident(name))
1164 }
1165 Self::WaitForWalPosition { pos, timeout_ms } => {
1166 write!(f, "WAIT FOR WAL POSITION {pos}")?;
1167 if let Some(ms) = timeout_ms {
1168 write!(f, " WITH TIMEOUT {ms}")?;
1169 }
1170 Ok(())
1171 }
1172 Self::Analyze(None) => f.write_str("ANALYZE"),
1173 Self::Analyze(Some(t)) => write!(f, "ANALYZE {}", quote_ident(t)),
1174 Self::CompactColdSegments => f.write_str("COMPACT COLD SEGMENTS"),
1175 Self::Explain(e) => {
1176 if e.suggest {
1177 write!(f, "EXPLAIN (SUGGEST) {}", e.inner)
1178 } else if e.analyze {
1179 write!(f, "EXPLAIN ANALYZE {}", e.inner)
1180 } else {
1181 write!(f, "EXPLAIN {}", e.inner)
1182 }
1183 }
1184 Self::AlterIndex(a) => {
1185 write!(f, "ALTER INDEX {} ", quote_ident(&a.name))?;
1186 match a.target {
1187 AlterIndexTarget::Rebuild { encoding } => {
1188 f.write_str("REBUILD")?;
1189 if let Some(enc) = encoding {
1190 write!(f, " WITH (encoding = {enc})")?;
1191 }
1192 Ok(())
1193 }
1194 }
1195 }
1196 Self::AlterTable(a) => {
1197 write!(f, "ALTER TABLE {} ", quote_ident(&a.name))?;
1198 match &a.target {
1199 AlterTableTarget::SetHotTierBytes(n) => {
1200 write!(f, "SET hot_tier_bytes = {n}")
1201 }
1202 AlterTableTarget::AddForeignKey(fk) => write!(f, "ADD {fk}"),
1203 AlterTableTarget::DropForeignKey(name) => {
1204 write!(f, "DROP CONSTRAINT {}", quote_ident(name))
1205 }
1206 }
1207 }
1208 Self::CreatePublication(p) => {
1209 write!(f, "CREATE PUBLICATION {}", quote_ident(&p.name))?;
1210 match &p.scope {
1211 PublicationScope::AllTables => f.write_str(" FOR ALL TABLES"),
1212 PublicationScope::ForTables(ts) => {
1213 f.write_str(" FOR TABLE ")?;
1214 for (i, t) in ts.iter().enumerate() {
1215 if i > 0 {
1216 f.write_str(", ")?;
1217 }
1218 write!(f, "{}", quote_ident(t))?;
1219 }
1220 Ok(())
1221 }
1222 PublicationScope::AllTablesExcept(ts) => {
1223 f.write_str(" FOR ALL TABLES EXCEPT ")?;
1224 for (i, t) in ts.iter().enumerate() {
1225 if i > 0 {
1226 f.write_str(", ")?;
1227 }
1228 write!(f, "{}", quote_ident(t))?;
1229 }
1230 Ok(())
1231 }
1232 }
1233 }
1234 Self::CreateExtension(name) => {
1235 write!(f, "CREATE EXTENSION IF NOT EXISTS {}", quote_ident(name))
1236 }
1237 Self::DoBlock => f.write_str("DO $$ /* SPG no-op */ $$"),
1238 Self::DropPublication(name) => {
1239 write!(f, "DROP PUBLICATION {}", quote_ident(name))
1240 }
1241 }
1242 }
1243}
1244
1245impl fmt::Display for CreateIndexStatement {
1246 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1247 if self.is_unique {
1248 f.write_str("CREATE UNIQUE INDEX ")?;
1249 } else {
1250 f.write_str("CREATE INDEX ")?;
1251 }
1252 if self.if_not_exists {
1253 f.write_str("IF NOT EXISTS ")?;
1254 }
1255 write!(
1256 f,
1257 "{} ON {} ",
1258 quote_ident(&self.name),
1259 quote_ident(&self.table)
1260 )?;
1261 match self.method {
1262 IndexMethod::Hnsw => f.write_str("USING hnsw ")?,
1263 IndexMethod::Brin => f.write_str("USING brin ")?,
1264 IndexMethod::BTree => {}
1265 }
1266 if let Some(expr) = &self.expression {
1267 write!(f, "({})", expr)?;
1268 } else if self.extra_columns.is_empty() {
1269 write!(f, "({})", quote_ident(&self.column))?;
1270 } else {
1271 // v7.9.14 — multi-column key. Emit each column quoted
1272 // so the round-tripped form re-parses to identical AST.
1273 f.write_str("(")?;
1274 write!(f, "{}", quote_ident(&self.column))?;
1275 for c in &self.extra_columns {
1276 write!(f, ", {}", quote_ident(c))?;
1277 }
1278 f.write_str(")")?;
1279 }
1280 if !self.included_columns.is_empty() {
1281 f.write_str(" INCLUDE (")?;
1282 for (i, c) in self.included_columns.iter().enumerate() {
1283 if i > 0 {
1284 f.write_str(", ")?;
1285 }
1286 write!(f, "{}", quote_ident(c))?;
1287 }
1288 f.write_str(")")?;
1289 }
1290 if let Some(pred) = &self.partial_predicate {
1291 write!(f, " WHERE {}", pred)?;
1292 }
1293 Ok(())
1294 }
1295}
1296
1297impl fmt::Display for CreateTableStatement {
1298 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1299 f.write_str("CREATE TABLE ")?;
1300 if self.if_not_exists {
1301 f.write_str("IF NOT EXISTS ")?;
1302 }
1303 write!(f, "{} (", quote_ident(&self.name))?;
1304 for (i, col) in self.columns.iter().enumerate() {
1305 if i > 0 {
1306 f.write_str(", ")?;
1307 }
1308 write!(f, "{col}")?;
1309 }
1310 // v7.6.0 — render FK constraints in table-level form, after
1311 // the column list. WAL replay round-trips through Display, so
1312 // every FK must serialise here for replay to reconstruct the
1313 // schema bit-for-bit.
1314 for fk in &self.foreign_keys {
1315 f.write_str(", ")?;
1316 write!(f, "{fk}")?;
1317 }
1318 f.write_str(")")
1319 }
1320}
1321
1322impl fmt::Display for ForeignKeyConstraint {
1323 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1324 if let Some(name) = &self.name {
1325 write!(f, "CONSTRAINT {} ", quote_ident(name))?;
1326 }
1327 f.write_str("FOREIGN KEY (")?;
1328 for (i, c) in self.columns.iter().enumerate() {
1329 if i > 0 {
1330 f.write_str(", ")?;
1331 }
1332 f.write_str("e_ident(c))?;
1333 }
1334 write!(f, ") REFERENCES {}", quote_ident(&self.parent_table))?;
1335 if !self.parent_columns.is_empty() {
1336 f.write_str(" (")?;
1337 for (i, c) in self.parent_columns.iter().enumerate() {
1338 if i > 0 {
1339 f.write_str(", ")?;
1340 }
1341 f.write_str("e_ident(c))?;
1342 }
1343 f.write_str(")")?;
1344 }
1345 // Only render non-default actions to keep Display output
1346 // close to user input. SPG's default is RESTRICT (matches
1347 // SQL spec).
1348 if self.on_delete != FkAction::Restrict {
1349 write!(f, " ON DELETE {}", self.on_delete)?;
1350 }
1351 if self.on_update != FkAction::Restrict {
1352 write!(f, " ON UPDATE {}", self.on_update)?;
1353 }
1354 Ok(())
1355 }
1356}
1357
1358impl fmt::Display for FkAction {
1359 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1360 match self {
1361 Self::Restrict => f.write_str("RESTRICT"),
1362 Self::Cascade => f.write_str("CASCADE"),
1363 Self::SetNull => f.write_str("SET NULL"),
1364 Self::SetDefault => f.write_str("SET DEFAULT"),
1365 Self::NoAction => f.write_str("NO ACTION"),
1366 }
1367 }
1368}
1369
1370impl fmt::Display for ColumnDef {
1371 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1372 write!(f, "{} {}", quote_ident(&self.name), self.ty)?;
1373 if let Some(d) = &self.default {
1374 write!(f, " DEFAULT {d}")?;
1375 }
1376 if self.auto_increment {
1377 f.write_str(" AUTO_INCREMENT")?;
1378 }
1379 if !self.nullable {
1380 f.write_str(" NOT NULL")?;
1381 }
1382 Ok(())
1383 }
1384}
1385
1386impl fmt::Display for InsertStatement {
1387 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1388 write!(f, "INSERT INTO {}", quote_ident(&self.table))?;
1389 if let Some(cols) = &self.columns {
1390 f.write_str(" (")?;
1391 for (i, c) in cols.iter().enumerate() {
1392 if i > 0 {
1393 f.write_str(", ")?;
1394 }
1395 f.write_str("e_ident(c))?;
1396 }
1397 f.write_str(")")?;
1398 }
1399 f.write_str(" VALUES ")?;
1400 for (ri, row) in self.rows.iter().enumerate() {
1401 if ri > 0 {
1402 f.write_str(", ")?;
1403 }
1404 f.write_str("(")?;
1405 for (i, v) in row.iter().enumerate() {
1406 if i > 0 {
1407 f.write_str(", ")?;
1408 }
1409 write!(f, "{v}")?;
1410 }
1411 f.write_str(")")?;
1412 }
1413 Ok(())
1414 }
1415}
1416
1417impl fmt::Display for UpdateStatement {
1418 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1419 write!(f, "UPDATE {} SET ", quote_ident(&self.table))?;
1420 for (i, (col, expr)) in self.assignments.iter().enumerate() {
1421 if i > 0 {
1422 f.write_str(", ")?;
1423 }
1424 write!(f, "{} = {expr}", quote_ident(col))?;
1425 }
1426 if let Some(w) = &self.where_ {
1427 write!(f, " WHERE {w}")?;
1428 }
1429 Ok(())
1430 }
1431}
1432
1433impl fmt::Display for DeleteStatement {
1434 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1435 write!(f, "DELETE FROM {}", quote_ident(&self.table))?;
1436 if let Some(w) = &self.where_ {
1437 write!(f, " WHERE {w}")?;
1438 }
1439 Ok(())
1440 }
1441}
1442
1443impl fmt::Display for SelectStatement {
1444 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1445 write_bare_select(self, f)?;
1446 for (kind, peer) in &self.unions {
1447 f.write_str(match kind {
1448 UnionKind::Distinct => " UNION ",
1449 UnionKind::All => " UNION ALL ",
1450 })?;
1451 write_bare_select(peer, f)?;
1452 }
1453 if !self.order_by.is_empty() {
1454 f.write_str(" ORDER BY ")?;
1455 for (i, o) in self.order_by.iter().enumerate() {
1456 if i > 0 {
1457 f.write_str(", ")?;
1458 }
1459 write!(f, "{}", o.expr)?;
1460 if o.desc {
1461 f.write_str(" DESC")?;
1462 }
1463 }
1464 }
1465 if let Some(n) = &self.limit {
1466 write!(f, " LIMIT {n}")?;
1467 }
1468 if let Some(o) = &self.offset {
1469 write!(f, " OFFSET {o}")?;
1470 }
1471 Ok(())
1472 }
1473}
1474
1475fn write_bare_select(s: &SelectStatement, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1476 f.write_str("SELECT ")?;
1477 if s.distinct {
1478 f.write_str("DISTINCT ")?;
1479 }
1480 write_bare_select_body(s, f)
1481}
1482
1483fn write_bare_select_body(s: &SelectStatement, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1484 for (i, item) in s.items.iter().enumerate() {
1485 if i > 0 {
1486 f.write_str(", ")?;
1487 }
1488 write!(f, "{item}")?;
1489 }
1490 if let Some(t) = &s.from {
1491 write!(f, " FROM {t}")?;
1492 }
1493 if let Some(e) = &s.where_ {
1494 write!(f, " WHERE {e}")?;
1495 }
1496 if let Some(gs) = &s.group_by {
1497 f.write_str(" GROUP BY ")?;
1498 for (i, g) in gs.iter().enumerate() {
1499 if i > 0 {
1500 f.write_str(", ")?;
1501 }
1502 write!(f, "{g}")?;
1503 }
1504 }
1505 if let Some(h) = &s.having {
1506 write!(f, " HAVING {h}")?;
1507 }
1508 Ok(())
1509}
1510
1511impl fmt::Display for SelectItem {
1512 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1513 match self {
1514 Self::Wildcard => f.write_str("*"),
1515 Self::Expr { expr, alias } => {
1516 write!(f, "{expr}")?;
1517 if let Some(a) = alias {
1518 write!(f, " AS {}", quote_ident(a))?;
1519 }
1520 Ok(())
1521 }
1522 }
1523 }
1524}
1525
1526impl fmt::Display for FromClause {
1527 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1528 write!(f, "{}", self.primary)?;
1529 for j in &self.joins {
1530 match j.kind {
1531 JoinKind::Inner => write!(f, " INNER JOIN {}", j.table)?,
1532 JoinKind::Left => write!(f, " LEFT JOIN {}", j.table)?,
1533 JoinKind::Cross => write!(f, " CROSS JOIN {}", j.table)?,
1534 }
1535 if let Some(on) = &j.on {
1536 write!(f, " ON {on}")?;
1537 }
1538 }
1539 Ok(())
1540 }
1541}
1542
1543impl fmt::Display for TableRef {
1544 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1545 write!(f, "{}", quote_ident(&self.name))?;
1546 if let Some(a) = &self.alias {
1547 write!(f, " AS {}", quote_ident(a))?;
1548 }
1549 Ok(())
1550 }
1551}
1552
1553impl fmt::Display for ColumnName {
1554 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1555 if let Some(q) = &self.qualifier {
1556 write!(f, "{}.{}", quote_ident(q), quote_ident(&self.name))
1557 } else {
1558 write!(f, "{}", quote_ident(&self.name))
1559 }
1560 }
1561}
1562
1563impl fmt::Display for Expr {
1564 #[allow(clippy::too_many_lines)]
1565 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1566 match self {
1567 Self::Literal(l) => write!(f, "{l}"),
1568 Self::Column(c) => write!(f, "{c}"),
1569 Self::Placeholder(n) => write!(f, "${n}"),
1570 Self::Binary { lhs, op, rhs } => write!(f, "({lhs} {op} {rhs})"),
1571 Self::Unary { op, expr } => match op {
1572 UnOp::Not => write!(f, "(NOT {expr})"),
1573 UnOp::Neg => write!(f, "(-{expr})"),
1574 },
1575 Self::Cast { expr, target } => write!(f, "({expr}::{target})"),
1576 Self::IsNull { expr, negated } => {
1577 if *negated {
1578 write!(f, "({expr} IS NOT NULL)")
1579 } else {
1580 write!(f, "({expr} IS NULL)")
1581 }
1582 }
1583 Self::FunctionCall { name, args } => {
1584 write!(f, "{name}(")?;
1585 for (i, a) in args.iter().enumerate() {
1586 if i > 0 {
1587 f.write_str(", ")?;
1588 }
1589 write!(f, "{a}")?;
1590 }
1591 f.write_str(")")
1592 }
1593 Self::Like {
1594 expr,
1595 pattern,
1596 negated,
1597 } => {
1598 if *negated {
1599 write!(f, "({expr} NOT LIKE {pattern})")
1600 } else {
1601 write!(f, "({expr} LIKE {pattern})")
1602 }
1603 }
1604 Self::Extract { field, source } => write!(f, "EXTRACT({field} FROM {source})"),
1605 Self::WindowFunction {
1606 name,
1607 args,
1608 partition_by,
1609 order_by,
1610 frame,
1611 null_treatment: _,
1612 } => {
1613 write!(f, "{name}(")?;
1614 for (i, a) in args.iter().enumerate() {
1615 if i > 0 {
1616 f.write_str(", ")?;
1617 }
1618 write!(f, "{a}")?;
1619 }
1620 f.write_str(") OVER (")?;
1621 if !partition_by.is_empty() {
1622 f.write_str("PARTITION BY ")?;
1623 for (i, p) in partition_by.iter().enumerate() {
1624 if i > 0 {
1625 f.write_str(", ")?;
1626 }
1627 write!(f, "{p}")?;
1628 }
1629 }
1630 if !order_by.is_empty() {
1631 if !partition_by.is_empty() {
1632 f.write_str(" ")?;
1633 }
1634 f.write_str("ORDER BY ")?;
1635 for (i, (e, desc)) in order_by.iter().enumerate() {
1636 if i > 0 {
1637 f.write_str(", ")?;
1638 }
1639 write!(f, "{e}")?;
1640 if *desc {
1641 f.write_str(" DESC")?;
1642 }
1643 }
1644 }
1645 if let Some(fr) = frame {
1646 if !partition_by.is_empty() || !order_by.is_empty() {
1647 f.write_str(" ")?;
1648 }
1649 let k = match fr.kind {
1650 FrameKind::Rows => "ROWS",
1651 FrameKind::Range => "RANGE",
1652 };
1653 if let Some(end) = &fr.end {
1654 write!(f, "{k} BETWEEN {} AND {}", fr.start, end)?;
1655 } else {
1656 write!(f, "{k} {}", fr.start)?;
1657 }
1658 }
1659 f.write_str(")")
1660 }
1661 Self::ScalarSubquery(s) => write!(f, "({s})"),
1662 Self::Exists { subquery, negated } => {
1663 if *negated {
1664 write!(f, "NOT EXISTS ({subquery})")
1665 } else {
1666 write!(f, "EXISTS ({subquery})")
1667 }
1668 }
1669 Self::InSubquery {
1670 expr,
1671 subquery,
1672 negated,
1673 } => {
1674 if *negated {
1675 write!(f, "({expr} NOT IN ({subquery}))")
1676 } else {
1677 write!(f, "({expr} IN ({subquery}))")
1678 }
1679 }
1680 Self::Array(items) => {
1681 f.write_str("ARRAY[")?;
1682 for (i, e) in items.iter().enumerate() {
1683 if i > 0 {
1684 f.write_str(", ")?;
1685 }
1686 write!(f, "{e}")?;
1687 }
1688 f.write_str("]")
1689 }
1690 Self::ArraySubscript { target, index } => write!(f, "({target}[{index}])"),
1691 Self::AnyAll {
1692 expr,
1693 op,
1694 array,
1695 is_any,
1696 } => {
1697 let kw = if *is_any { "ANY" } else { "ALL" };
1698 write!(f, "({expr} {op} {kw}({array}))")
1699 }
1700 }
1701 }
1702}
1703
1704impl fmt::Display for Literal {
1705 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1706 match self {
1707 Self::Integer(n) => write!(f, "{n}"),
1708 Self::Float(x) => {
1709 let s = format!("{x}");
1710 // Default Display for an integral f64 (e.g. 1.0) emits "1",
1711 // which would round-trip back to Integer. Force a dot.
1712 if s.contains('.') || s.contains('e') || s.contains('E') {
1713 f.write_str(&s)
1714 } else {
1715 write!(f, "{s}.0")
1716 }
1717 }
1718 Self::String(s) => {
1719 f.write_str("'")?;
1720 for c in s.chars() {
1721 if c == '\'' {
1722 f.write_str("''")?;
1723 } else {
1724 write!(f, "{c}")?;
1725 }
1726 }
1727 f.write_str("'")
1728 }
1729 Self::Bool(b) => f.write_str(if *b { "TRUE" } else { "FALSE" }),
1730 Self::Null => f.write_str("NULL"),
1731 Self::Vector(v) => {
1732 f.write_str("[")?;
1733 for (i, x) in v.iter().enumerate() {
1734 if i > 0 {
1735 f.write_str(", ")?;
1736 }
1737 let s = format!("{x}");
1738 // Mirror Float Display: force a dot so re-parse stays
1739 // numerically literal.
1740 if s.contains('.') || s.contains('e') || s.contains('E') {
1741 f.write_str(&s)?;
1742 } else {
1743 write!(f, "{s}.0")?;
1744 }
1745 }
1746 f.write_str("]")
1747 }
1748 Self::Interval { text, .. } => {
1749 f.write_str("INTERVAL '")?;
1750 for c in text.chars() {
1751 if c == '\'' {
1752 f.write_str("''")?;
1753 } else {
1754 write!(f, "{c}")?;
1755 }
1756 }
1757 f.write_str("'")
1758 }
1759 }
1760 }
1761}
1762
1763impl fmt::Display for BinOp {
1764 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1765 f.write_str(match self {
1766 Self::Or => "OR",
1767 Self::And => "AND",
1768 Self::Eq => "=",
1769 Self::NotEq => "<>",
1770 Self::IsDistinctFrom => "IS DISTINCT FROM",
1771 Self::IsNotDistinctFrom => "IS NOT DISTINCT FROM",
1772 Self::Lt => "<",
1773 Self::LtEq => "<=",
1774 Self::Gt => ">",
1775 Self::GtEq => ">=",
1776 Self::Add => "+",
1777 Self::Sub => "-",
1778 Self::Mul => "*",
1779 Self::Div => "/",
1780 Self::L2Distance => "<->",
1781 Self::InnerProduct => "<#>",
1782 Self::CosineDistance => "<=>",
1783 Self::Concat => "||",
1784 Self::JsonGet => "->",
1785 Self::JsonGetText => "->>",
1786 Self::JsonGetPath => "#>",
1787 Self::JsonGetPathText => "#>>",
1788 Self::JsonContains => "@>",
1789 })
1790 }
1791}
1792
1793/// Quote `s` as a PG double-quoted identifier when required (keyword,
1794/// non-folded case, leading digit, embedded non-`[A-Za-z0-9_]`, empty).
1795/// Otherwise return it as-is. Returns an owned `String` to keep the call site
1796/// uniform.
1797fn quote_ident(s: &str) -> String {
1798 let needs_quote = match s.chars().next() {
1799 None => true,
1800 Some(c) if !c.is_ascii_alphabetic() && c != '_' => true,
1801 _ => {
1802 s.chars().any(|c| !(c.is_ascii_alphanumeric() || c == '_'))
1803 || s.chars().any(|c| c.is_ascii_uppercase())
1804 || is_keyword(s)
1805 }
1806 };
1807 if !needs_quote {
1808 return s.to_string();
1809 }
1810 let mut out = String::with_capacity(s.len() + 2);
1811 out.push('"');
1812 for c in s.chars() {
1813 if c == '"' {
1814 out.push_str("\"\"");
1815 } else {
1816 out.push(c);
1817 }
1818 }
1819 out.push('"');
1820 out
1821}
1822
1823fn is_keyword(s: &str) -> bool {
1824 matches!(
1825 &*s.to_ascii_lowercase(),
1826 "select"
1827 | "from"
1828 | "where"
1829 | "as"
1830 | "null"
1831 | "true"
1832 | "false"
1833 | "and"
1834 | "or"
1835 | "not"
1836 | "create"
1837 | "table"
1838 | "insert"
1839 | "into"
1840 | "values"
1841 | "index"
1842 | "on"
1843 | "begin"
1844 | "commit"
1845 | "rollback"
1846 | "is"
1847 | "between"
1848 | "in"
1849 | "like"
1850 | "group"
1851 | "distinct"
1852 | "union"
1853 | "all"
1854 | "join"
1855 | "inner"
1856 | "left"
1857 | "cross"
1858 | "outer"
1859 | "default"
1860 | "savepoint"
1861 | "release"
1862 | "to"
1863 | "having"
1864 | "show"
1865 | "extract"
1866 | "offset"
1867 | "asc"
1868 | "desc"
1869 | "interval"
1870 )
1871}
1872
1873#[cfg(test)]
1874mod tests {
1875 use super::*;
1876 use alloc::vec;
1877
1878 #[test]
1879 fn integer_literal_renders_without_dot() {
1880 assert_eq!(Literal::Integer(42).to_string(), "42");
1881 }
1882
1883 #[test]
1884 fn integral_float_keeps_dot() {
1885 assert_eq!(Literal::Float(1.0).to_string(), "1.0");
1886 assert_eq!(Literal::Float(1.5).to_string(), "1.5");
1887 assert_eq!(Literal::Float(2.5e-3).to_string(), "0.0025");
1888 }
1889
1890 #[test]
1891 fn string_literal_doubles_quote() {
1892 assert_eq!(Literal::String("it's".into()).to_string(), "'it''s'");
1893 }
1894
1895 #[test]
1896 fn bool_and_null_render_uppercase() {
1897 assert_eq!(Literal::Bool(true).to_string(), "TRUE");
1898 assert_eq!(Literal::Bool(false).to_string(), "FALSE");
1899 assert_eq!(Literal::Null.to_string(), "NULL");
1900 }
1901
1902 #[test]
1903 fn binary_op_always_parenthesised() {
1904 let e = Expr::Binary {
1905 lhs: Box::new(Expr::Literal(Literal::Integer(1))),
1906 op: BinOp::Add,
1907 rhs: Box::new(Expr::Literal(Literal::Integer(2))),
1908 };
1909 assert_eq!(e.to_string(), "(1 + 2)");
1910 }
1911
1912 #[test]
1913 fn select_star_from_table() {
1914 let s = SelectStatement {
1915 items: vec![SelectItem::Wildcard],
1916 from: Some(FromClause {
1917 primary: TableRef {
1918 name: "users".into(),
1919 alias: None,
1920 as_of_segment: None,
1921 unnest_expr: None,
1922 },
1923 joins: vec![],
1924 }),
1925 where_: None,
1926 group_by: None,
1927 group_by_all: false,
1928 having: None,
1929 unions: vec![],
1930 order_by: Vec::new(),
1931 limit: None,
1932 offset: None,
1933 distinct: false,
1934 ctes: vec![],
1935 };
1936 assert_eq!(s.to_string(), "SELECT * FROM users");
1937 }
1938
1939 #[test]
1940 fn quote_ident_for_uppercase_and_keyword() {
1941 assert_eq!(quote_ident("foo"), "foo");
1942 assert_eq!(quote_ident("Foo"), "\"Foo\"");
1943 assert_eq!(quote_ident("select"), "\"select\"");
1944 assert_eq!(quote_ident(""), "\"\"");
1945 assert_eq!(quote_ident("a\"b"), "\"a\"\"b\"");
1946 }
1947}