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 /// v7.14.0 — `DROP TABLE [IF EXISTS] name [, name…]
18 /// [CASCADE | RESTRICT]`. Engine removes the matching tables
19 /// (each one) from the catalog; IF EXISTS makes the drop
20 /// idempotent. CASCADE / RESTRICT trailers parsed silently
21 /// (SPG always cascades index drops on table drop).
22 DropTable {
23 names: Vec<String>,
24 if_exists: bool,
25 },
26 /// v7.14.0 — `DROP INDEX [IF EXISTS] name`. Removes the
27 /// matching index across whichever table holds it.
28 DropIndex {
29 name: String,
30 if_exists: bool,
31 },
32 /// v7.14.0 — empty / comment-only statement. The lexer strips
33 /// `--` line comments and `/* … */` block comments (including
34 /// the MySQL conditional `/*!NNNNN … */` form) before the
35 /// parser ever sees them; a SQL chunk that contains nothing
36 /// else lands here. Engine returns CommandOk no-op so
37 /// pg_dump / mysqldump preambles (`SET NAMES utf8mb4`
38 /// wrapped in conditional comments, etc.) load cleanly.
39 Empty,
40 Select(SelectStatement),
41 CreateTable(CreateTableStatement),
42 /// v7.9.15 — `CREATE EXTENSION [IF NOT EXISTS] <name>
43 /// [WITH SCHEMA <s>] [VERSION <v>] [CASCADE]` accepted as a
44 /// no-op so PG dumps that include extension declarations
45 /// (notably `pgvector`) load against SPG without splitting
46 /// init scripts. mailrs migration follow-up F3.
47 CreateExtension(String),
48 /// v7.9.27 — PG `DO $$ … $$ [LANGUAGE plpgsql];` block. SPG
49 /// has no PL/pgSQL; engine returns CommandOk no-op so
50 /// `pg_dump` output with idempotent DO migrations loads
51 /// against SPG without splitting scripts. The lexer
52 /// consumes the dollar-quoted body into a discarded
53 /// Token::String. mailrs migration follow-up H1.
54 DoBlock,
55 CreateIndex(CreateIndexStatement),
56 Insert(InsertStatement),
57 /// v4.4 — `UPDATE <table> SET col=expr [, ...] [WHERE cond]`.
58 Update(UpdateStatement),
59 /// v4.4 — `DELETE FROM <table> [WHERE cond]`.
60 Delete(DeleteStatement),
61 Begin,
62 Commit,
63 Rollback,
64 /// `SAVEPOINT <name>` — push a named savepoint onto the active TX's
65 /// stack so a later `ROLLBACK TO <name>` can undo just the work
66 /// since this point.
67 Savepoint(String),
68 /// `ROLLBACK TO [SAVEPOINT] <name>` — restore catalog state to the
69 /// named savepoint and discard later savepoints. Does not end the
70 /// transaction.
71 RollbackToSavepoint(String),
72 /// `RELEASE [SAVEPOINT] <name>` — discard a savepoint without
73 /// rolling back. Keeps the work done since then.
74 ReleaseSavepoint(String),
75 /// `SHOW TABLES` — return the list of tables in the catalog.
76 ShowTables,
77 /// `SHOW COLUMNS FROM <table>` — return one row per column with
78 /// its declared name / type / nullability.
79 ShowColumns(String),
80 /// `CREATE USER 'name' WITH PASSWORD 'pw' ROLE 'admin'` (v4.1).
81 /// Role is optional; defaults to `readonly` when omitted.
82 CreateUser(CreateUserStatement),
83 /// `DROP USER 'name'` (v4.1).
84 DropUser(String),
85 /// `SHOW USERS` (v4.1) — admin-only listing of (name, role).
86 ShowUsers,
87 /// v4.26 — `EXPLAIN [ANALYZE] <select>`. The engine returns a
88 /// single-column text table describing the rewritten plan tree
89 /// for `inner`. `analyze` triggers an actual exec to attach
90 /// observed row counts and elapsed micros to each node.
91 Explain(ExplainStatement),
92 /// v6.0.4 — `ALTER INDEX <name> REBUILD [WITH (encoding = ...)]`.
93 /// Synchronous rebuild of an NSW index. With the optional
94 /// encoding clause, every stored cell at the indexed column is
95 /// also re-encoded through `coerce_value` before the new graph
96 /// builds.
97 AlterIndex(AlterIndexStatement),
98 /// v6.7.2 — `ALTER TABLE <name> SET <setting> = <value>`.
99 /// The only setting in v6.7.2 is `hot_tier_bytes`, which
100 /// overrides the global `SPG_HOT_TIER_BYTES` freezer trigger
101 /// for the named table.
102 AlterTable(AlterTableStatement),
103 /// v6.1.2 — `CREATE PUBLICATION <name> [FOR ALL TABLES]`.
104 /// The catalog row lives in `spg_publications`. Publisher-side
105 /// WAL filtering arrives in v6.1.5.
106 CreatePublication(CreatePublicationStatement),
107 /// v6.1.2 — `DROP PUBLICATION <name>`. PG-compatible silent
108 /// no-op when the publication does not exist.
109 DropPublication(String),
110 /// v6.1.3 — `SHOW PUBLICATIONS`. Returns one row per
111 /// publication ordered by name with `(name, scope_summary,
112 /// table_count)` columns. The scope summary is the human-
113 /// readable form `ALL TABLES` / `FOR TABLE …` / `FOR ALL
114 /// TABLES EXCEPT …`; `table_count` is `NULL` for the
115 /// `AllTables` scope and the table-list length otherwise.
116 ShowPublications,
117 /// v6.1.4 — `CREATE SUBSCRIPTION <name> CONNECTION '<conn>'
118 /// PUBLICATION <pub_name> [, <pub_name> …]`. Catalog lands
119 /// in `spg_subscriptions`; when the subscription is
120 /// `enabled = true` (default) the server spawns a
121 /// background worker that connects to `conn` and drains the
122 /// requested publication(s) into the local engine.
123 CreateSubscription(CreateSubscriptionStatement),
124 /// v6.1.4 — `DROP SUBSCRIPTION <name>`. Like DROP
125 /// PUBLICATION, silent no-op when absent. Stops the
126 /// associated worker thread before removing the row.
127 DropSubscription(String),
128 /// v6.1.4 — `SHOW SUBSCRIPTIONS`. Returns one row per
129 /// subscription ordered by name with `(name, conn_str,
130 /// publications, enabled, last_received_pos)`.
131 ShowSubscriptions,
132 /// v6.1.7 — `WAIT FOR WAL POSITION <pos> [WITH TIMEOUT <ms>]`.
133 /// Blocks until the local server's apply position reaches
134 /// `<pos>` or `<ms>` elapses. Server-layer command: the
135 /// engine refuses it (`EngineError::Unsupported`) since
136 /// `lag_state` lives in `spg-server`'s `ServerState`.
137 WaitForWalPosition {
138 pos: u64,
139 /// `None` → wait forever; `Some(ms)` → return after `ms`
140 /// milliseconds even if the target isn't reached.
141 timeout_ms: Option<u64>,
142 },
143 /// v6.2.0 — `ANALYZE [<table>]`. Bare form walks every user
144 /// table; `ANALYZE <name>` re-stats just one. Populates
145 /// `spg_statistic` with per-column null_frac + n_distinct +
146 /// 100-bucket equi-depth histogram.
147 Analyze(Option<String>),
148 /// v6.7.3 — `COMPACT COLD SEGMENTS`. Walks every user table's
149 /// BTree-cold indices and merges small cold-tier segments
150 /// (size below `SPG_COMPACTION_TARGET_SEGMENT_BYTES`, default
151 /// 4 MiB) into a single larger segment per (table, index).
152 /// `WHERE` predicate filtering on which tables to compact is
153 /// carved out of v6.7.3 (per V6_7_DESIGN.md STABILITY entry);
154 /// v6.7.3 only supports the bare form.
155 CompactColdSegments,
156 /// v7.12.1 — `SET <name> [TO|=] <value>`. Records a session
157 /// parameter on the engine; v7.12.1 honours
158 /// `default_text_search_config` (consumed by `to_tsvector` /
159 /// `plainto_tsquery` family when called without an explicit
160 /// config arg). All other names are accepted as a no-op so PG
161 /// dumps with `SET client_encoding`, `SET search_path` etc.
162 /// load cleanly.
163 SetParameter {
164 name: String,
165 value: SetValue,
166 },
167 /// v7.14.0 — `SET a = 1, b = 2, …` MySQL-flavoured
168 /// multi-assignment (mysqldump preamble uses
169 /// `SET @OLD_FOREIGN_KEY_CHECKS = @@FOREIGN_KEY_CHECKS,
170 /// FOREIGN_KEY_CHECKS=0`). Engine applies each pair in
171 /// source order. Pairs whose LHS is a MySQL session/user
172 /// variable (`@VAR` / `@@VAR`) are recorded with the raw
173 /// name so the engine can ignore them; pairs whose LHS is
174 /// a recognised engine parameter (e.g. `FOREIGN_KEY_CHECKS`)
175 /// go through the regular `set_session_param` path.
176 SetParameterList(Vec<(String, SetValue)>),
177 /// v7.12.1 — `RESET <name>` / `RESET ALL`. Restores parameter
178 /// to its default. No-op for parameters SPG does not track.
179 ResetParameter(Option<String>),
180 /// v7.12.4 — `CREATE [OR REPLACE] FUNCTION name(args) RETURNS
181 /// <type> [LANGUAGE <lang>] AS $$ body $$ [LANGUAGE <lang>]`.
182 /// v7.12.4 ships `plpgsql` for `RETURNS TRIGGER` bodies (the
183 /// CREATE TRIGGER + AFTER/BEFORE row-level pipeline). Other
184 /// languages parse but error at exec time with a clear
185 /// unsupported message.
186 CreateFunction(CreateFunctionStatement),
187 /// v7.12.4 — `CREATE [OR REPLACE] TRIGGER name {BEFORE|AFTER}
188 /// {INSERT|UPDATE|DELETE} [OR ...] ON tbl FOR EACH ROW
189 /// EXECUTE {FUNCTION|PROCEDURE} fn_name()`. STATEMENT-level
190 /// triggers and column-list / WHEN clauses are out of scope
191 /// for v7.12.4.
192 CreateTrigger(CreateTriggerStatement),
193 /// v7.12.4 — `DROP TRIGGER [IF EXISTS] name ON tbl`. Silent
194 /// no-op when missing if `IF EXISTS` is set.
195 DropTrigger {
196 name: String,
197 table: String,
198 if_exists: bool,
199 },
200 /// v7.12.4 — `DROP FUNCTION [IF EXISTS] name`. Same shape as
201 /// DROP TRIGGER but global (no table scope).
202 DropFunction {
203 name: String,
204 if_exists: bool,
205 },
206}
207
208/// v7.12.1 — payload of a SET right-hand side. PG syntax accepts
209/// a string literal, an identifier (often a config name), an
210/// integer/float, or the bare `DEFAULT` keyword.
211#[derive(Debug, Clone, PartialEq)]
212pub enum SetValue {
213 String(String),
214 Ident(String),
215 Number(String),
216 Default,
217}
218
219/// v6.1.4 — `CREATE SUBSCRIPTION` AST node. v6.1.4 ships a
220/// single fixed-shape DDL; the WITH-clause options PG supports
221/// (`enabled`, `slot_name`, `streaming`, `binary`) are out of
222/// scope for v6.1.4 — `enabled` defaults to true and there are
223/// no other knobs to set in v6.1.x.
224#[derive(Debug, Clone, PartialEq, Eq)]
225pub struct CreateSubscriptionStatement {
226 pub name: String,
227 /// Connection string in PG keyword=value form (e.g.
228 /// `host=127.0.0.1 port=20002`). v6.1.4 only consumes the
229 /// `host` and `port` fields; the rest is reserved for
230 /// future v6.1.x options.
231 pub conn_str: String,
232 /// One or more publications on the remote side. Order is
233 /// preserved verbatim from the DDL; the worker requests them
234 /// in this order. v6.1.4 records the list; v6.1.5
235 /// publisher-side filtering enforces it.
236 pub publications: Vec<String>,
237}
238
239/// v6.1.2 — `CREATE PUBLICATION` AST node. The `scope` field uses
240/// the [`PublicationScope`] shape. v6.1.2 only accepted
241/// `AllTables`; v6.1.3 unlocks the `ForTables` / `AllTablesExcept`
242/// variants by flipping the parser gate (no AST migration).
243#[derive(Debug, Clone, PartialEq, Eq)]
244pub struct CreatePublicationStatement {
245 pub name: String,
246 pub scope: PublicationScope,
247}
248
249/// v6.1.2 — Which tables a publication covers. v6.1.3 (this commit)
250/// flips the parser gate for the `ForTables` / `AllTablesExcept`
251/// variants — the on-disk shape, snapshot serialisation, and the
252/// AST round-trip Display path were already in place in v6.1.2
253/// so this is a parser-only widening.
254#[derive(Debug, Clone, PartialEq, Eq)]
255pub enum PublicationScope {
256 AllTables,
257 ForTables(Vec<String>),
258 AllTablesExcept(Vec<String>),
259}
260
261#[derive(Debug, Clone, PartialEq, Eq)]
262pub struct AlterIndexStatement {
263 pub name: String,
264 pub target: AlterIndexTarget,
265}
266
267#[derive(Debug, Clone, Copy, PartialEq, Eq)]
268pub enum AlterIndexTarget {
269 /// `REBUILD [WITH (encoding = <enc>)]`. `encoding = None`
270 /// rebuilds the existing graph in place without touching the
271 /// column encoding; `Some(enc)` re-encodes every cell first.
272 Rebuild { encoding: Option<VecEncoding> },
273}
274
275/// v6.7.2 — `ALTER TABLE t SET <setting> = <value>`. v6.7.2 ships
276/// the single `hot_tier_bytes` setting; later v6.7.x sub-versions
277/// can add more SET subjects without changing the dispatch shape.
278#[derive(Debug, Clone, PartialEq)]
279pub struct AlterTableStatement {
280 pub name: String,
281 /// v7.13.2 — mailrs round-6 S1. One or more subactions
282 /// separated by commas in the source SQL. PG-semantic apply
283 /// is sequential; engine bails on first error (no
284 /// transactional rollback of completed subactions in v7.13).
285 /// Single-subaction shape stays a 1-element vec.
286 pub targets: Vec<AlterTableTarget>,
287}
288
289#[derive(Debug, Clone, PartialEq)]
290pub enum AlterTableTarget {
291 /// Per-table hot-tier byte budget override. The freezer
292 /// reads this before falling back to `SPG_HOT_TIER_BYTES`.
293 SetHotTierBytes(u64),
294 /// v7.6.8 — `ALTER TABLE t ADD CONSTRAINT name FOREIGN KEY
295 /// (cols) REFERENCES parent[(pcols)] [ON DELETE/UPDATE …]`.
296 /// Engine validates existing rows against the new constraint
297 /// before installing it.
298 AddForeignKey(ForeignKeyConstraint),
299 /// v7.6.8 — `ALTER TABLE t DROP CONSTRAINT [IF EXISTS] name`.
300 /// `if_exists` (v7.13.2 mailrs round-6 S7) makes the drop a
301 /// no-op when no FK with that name exists; otherwise raises.
302 DropForeignKey {
303 name: String,
304 if_exists: bool,
305 },
306 /// v7.13.0 — `ALTER TABLE t ADD [COLUMN] [IF NOT EXISTS] <col>
307 /// <type> [DEFAULT <expr>] [NOT NULL]`. mailrs round-5 G1
308 /// (20 migrate-*.sql hits). Engine appends the column to the
309 /// schema and back-fills every existing row with the DEFAULT
310 /// (or NULL when no DEFAULT and the column is nullable).
311 AddColumn {
312 column: ColumnDef,
313 if_not_exists: bool,
314 },
315 /// v7.13.0 — `ALTER TABLE t ALTER COLUMN <col> TYPE <ty>
316 /// [USING <expr>]` (mailrs round-5 G8). Engine rewrites every
317 /// existing row's column value by evaluating the optional
318 /// USING expression (default `col::<ty>`) and re-coercing
319 /// against the new column type.
320 AlterColumnType {
321 column: String,
322 new_type: ColumnTypeName,
323 using: Option<Expr>,
324 },
325 /// v7.13.3 — `ALTER TABLE t DROP [COLUMN] [IF EXISTS] <col>
326 /// [CASCADE | RESTRICT]` (mailrs round-7 S8). The column +
327 /// every row's value at that position is removed; any index
328 /// on the column is dropped. `if_exists` makes the drop a
329 /// no-op when the column is missing. `cascade` removes
330 /// dependents (FKs referencing the column, partial indexes
331 /// whose predicate names the column); without it, the engine
332 /// rejects when dependents exist.
333 DropColumn {
334 column: String,
335 if_exists: bool,
336 cascade: bool,
337 },
338 /// v7.14.0 — `ALTER TABLE t ADD CONSTRAINT name PRIMARY KEY
339 /// (cols)` / `ADD CONSTRAINT name UNIQUE (cols)` / `ADD
340 /// CONSTRAINT name CHECK (expr)` — table-level constraints
341 /// installed post-CREATE-TABLE. pg_dump emits PKs as a
342 /// separate ALTER TABLE statement, so this surface lets the
343 /// dump load straight through.
344 AddTableConstraint(TableConstraint),
345 /// v7.15.0 — `ALTER TABLE t RENAME [COLUMN] old TO new`.
346 /// Renames the column in the schema and propagates the rename
347 /// to every stored source string that references it as a
348 /// (potentially-qualified) column identifier: CHECK predicates,
349 /// partial-index predicates, runtime DEFAULT expressions, and
350 /// triggers' `UPDATE OF` column lists. Function bodies and
351 /// trigger bodies are NOT auto-rewritten — they're loose
352 /// source text and may contain references SPG can't statically
353 /// resolve to this column (NEW./OLD. + dynamic SQL). Renames
354 /// the column even if dependents exist; users renaming a
355 /// column referenced by a function body update the function
356 /// body separately.
357 RenameColumn { old: String, new: String },
358 /// v7.16.1 — `ALTER TABLE t { ENABLE | DISABLE } TRIGGER
359 /// { ALL | <name> }`. Toggles whether row-level triggers
360 /// fire on subsequent INSERT/UPDATE/DELETE on the table.
361 /// `pg_dump --disable-triggers` emits a DISABLE wrapper +
362 /// ENABLE epilogue around every table's data block so the
363 /// rows already-computed in prod don't get re-rewritten
364 /// (and so trigger-driven side effects like
365 /// audit/queueing don't re-fire during a bulk reload).
366 /// `which == TriggerSelector::All` toggles every trigger
367 /// on the table; `Named(name)` toggles one trigger. The
368 /// engine persists the disabled state on `TriggerDef.enabled`
369 /// (catalog FILE_VERSION 25+) and the row-write paths skip
370 /// the trigger when `!enabled`.
371 SetTriggerEnabled {
372 which: TriggerSelector,
373 enabled: bool,
374 },
375}
376
377/// v7.16.1 — target of `ALTER TABLE … { ENABLE | DISABLE }
378/// TRIGGER …`. PG also accepts `USER`, `REPLICA`, `ALWAYS`
379/// modifiers; v7.16.1 ships the two shapes pg_dump actually
380/// emits (`ALL` + per-name) — the rest parse-accept as `Named`
381/// shouldn't surface from a dump.
382#[derive(Debug, Clone, PartialEq, Eq)]
383pub enum TriggerSelector {
384 /// Every trigger on the table.
385 All,
386 /// A specific trigger by name.
387 Named(String),
388}
389
390#[derive(Debug, Clone, PartialEq)]
391pub struct ExplainStatement {
392 pub analyze: bool,
393 pub inner: Box<SelectStatement>,
394 /// v6.8.3 — `EXPLAIN (SUGGEST) <SELECT>` enables the index
395 /// advisor pass: after the regular plan tree, the engine
396 /// emits one suggestion line per column referenced in the
397 /// query's WHERE / JOIN that has no covering index on the
398 /// owning table.
399 pub suggest: bool,
400}
401
402#[derive(Debug, Clone, PartialEq, Eq)]
403pub struct CreateUserStatement {
404 pub name: String,
405 pub password: String,
406 /// One of `admin` / `readwrite` / `readonly`. Stored verbatim from
407 /// the parser; the engine validates against `Role::parse` so a
408 /// typo lands as a runtime error with a clear message rather than
409 /// a parse failure.
410 pub role: String,
411}
412
413/// v7.12.4 — `CREATE [OR REPLACE] FUNCTION`. v7.12.4 ships
414/// `RETURNS TRIGGER LANGUAGE plpgsql` as the primary use case
415/// (the row-level trigger body the CREATE TRIGGER below references).
416/// Non-trigger user-defined functions parse but error at execution
417/// time with a clear unsupported message; that surface lands in
418/// v7.12.5+.
419#[derive(Debug, Clone, PartialEq)]
420pub struct CreateFunctionStatement {
421 pub name: String,
422 /// `OR REPLACE` was present; an existing function with the
423 /// same name is overwritten instead of erroring.
424 pub or_replace: bool,
425 /// `(arg1 type1, ...)` — v7.12.4 only accepts the empty arg
426 /// list `()` (sufficient for trigger functions). Other shapes
427 /// parse and store the args but the executor refuses to call
428 /// them.
429 pub args: Vec<FunctionArg>,
430 /// `RETURNS <type>` — `trigger` is the supported shape for
431 /// v7.12.4; arbitrary return types parse to
432 /// [`FunctionReturn::Other`].
433 pub returns: FunctionReturn,
434 /// `LANGUAGE <lang>` clause. PG accepts the clause on either
435 /// side of `AS $$...$$`; the parser canonicalises to one slot.
436 /// `plpgsql` and `sql` are the two interesting values.
437 pub language: String,
438 /// `AS $$ ... $$` body. v7.12.4 parses PL/pgSQL bodies into
439 /// a structured AST; non-trigger / non-plpgsql bodies stay as
440 /// the raw source text so the v7.12.5+ executor can pick them
441 /// up without a parser rev.
442 pub body: FunctionBody,
443}
444
445/// v7.12.4 — one positional argument to a `CREATE FUNCTION`.
446#[derive(Debug, Clone, PartialEq)]
447pub struct FunctionArg {
448 /// `IN` / `OUT` / `INOUT` mode. v7.12.4 only accepts `IN`
449 /// (the default); `OUT` / `INOUT` parse but the executor
450 /// refuses them.
451 pub mode: FunctionArgMode,
452 /// Optional arg name. Trigger functions traditionally don't
453 /// name their args (they read NEW/OLD instead), so `None` is
454 /// the common case.
455 pub name: Option<String>,
456 /// Declared type, normalised to the SPG `DataType` mapping
457 /// where one exists. Unknown / extension types parse as a
458 /// raw string under [`FunctionArgType::Raw`].
459 pub ty: FunctionArgType,
460}
461
462#[derive(Debug, Clone, Copy, PartialEq, Eq)]
463pub enum FunctionArgMode {
464 In,
465 Out,
466 InOut,
467}
468
469#[derive(Debug, Clone, PartialEq)]
470pub enum FunctionArgType {
471 Typed(ColumnTypeName),
472 /// Unknown / extension types — kept as the parser-side raw
473 /// identifier so error messages can name them precisely.
474 Raw(String),
475}
476
477#[derive(Debug, Clone, PartialEq)]
478pub enum FunctionReturn {
479 /// `RETURNS TRIGGER` — the row-level trigger function shape.
480 /// v7.12.4 ships exactly this for execution.
481 Trigger,
482 /// `RETURNS VOID`. Parses; executor rejects in v7.12.4 unless
483 /// the function is unused (since v7.12.4 doesn't ship scalar
484 /// function invocation).
485 Void,
486 /// `RETURNS <type>` for any concrete data type. Reserved for
487 /// v7.12.5+'s scalar UDF surface.
488 Type(ColumnTypeName),
489 /// `RETURNS <ident>` for types SPG doesn't know — extension
490 /// types, RETURNS SETOF rows, RETURNS TABLE(...), etc.
491 Other(String),
492}
493
494#[derive(Debug, Clone, PartialEq)]
495pub enum FunctionBody {
496 /// v7.12.4 — parsed PL/pgSQL `BEGIN … END` block. The
497 /// trigger-function executor walks this directly without
498 /// re-parsing.
499 PlPgSql(PlPgSqlBlock),
500 /// Raw source text — parser couldn't (or didn't try to)
501 /// structure-parse the body. Used for `LANGUAGE sql`
502 /// functions and any PL/pgSQL body that contains v7.12.5+
503 /// features the v7.12.4 parser doesn't yet recognise. The
504 /// executor returns an unsupported error when invoked.
505 Raw(String),
506}
507
508/// v7.12.4 — PL/pgSQL `BEGIN ... END;` block. v7.12.6 widens
509/// from assignment + return to a real-PL/pgSQL surface:
510/// `DECLARE`-block local variables, `IF/ELSIF/ELSE/END IF`
511/// control flow, `RAISE` diagnostics, and embedded SQL
512/// statements that execute through the regular engine path.
513/// The remaining v7.12.x carve-out is loops (`LOOP/WHILE/FOR`),
514/// which mailrs's trigger doesn't need but other PG customers
515/// may; deferred to a future minor release.
516#[derive(Debug, Clone, PartialEq)]
517pub struct PlPgSqlBlock {
518 /// v7.12.6 — `DECLARE var TYPE [:= init_expr];` declarations
519 /// preceding `BEGIN`. Empty when the body opens directly with
520 /// `BEGIN`. Declarations execute in order; each may reference
521 /// earlier-declared locals in its init expression.
522 pub declarations: Vec<PlPgSqlDeclare>,
523 pub statements: Vec<PlPgSqlStmt>,
524}
525
526/// v7.12.6 — single `DECLARE` entry: variable name + declared
527/// type + optional initialiser. Variables default to SQL NULL
528/// when no init is given (matches PG).
529#[derive(Debug, Clone, PartialEq)]
530pub struct PlPgSqlDeclare {
531 pub name: String,
532 /// Declared SQL type (mapped to [`ColumnTypeName`] where SPG
533 /// knows it; raw text otherwise).
534 pub ty: FunctionArgType,
535 pub default: Option<Expr>,
536}
537
538#[derive(Debug, Clone, PartialEq)]
539pub enum PlPgSqlStmt {
540 /// `NEW.col := expr;` or `OLD.col := expr;`. OLD is parsed
541 /// for clarity in error reporting (PG also forbids it) — the
542 /// executor errors with a clear "OLD is read-only" message.
543 Assign { target: AssignTarget, value: Expr },
544 /// `RETURN <target>;` — trigger functions canonically return
545 /// `NEW` / `OLD` / `NULL`; v7.12.4 also accepts a bare
546 /// expression for forward compatibility with scalar UDFs.
547 Return(ReturnTarget),
548 /// v7.12.6 — `IF cond THEN body [ELSIF cond THEN body]*
549 /// [ELSE body] END IF;`. Branches are tried in order; first
550 /// truthy condition wins; the optional ELSE runs when no
551 /// condition matched.
552 If {
553 branches: Vec<(Expr, Vec<PlPgSqlStmt>)>,
554 else_branch: Vec<PlPgSqlStmt>,
555 },
556 /// v7.12.6 — `RAISE <level> '<fmt>' [, args]*;`. Level is one
557 /// of `NOTICE` / `WARNING` / `INFO` / `LOG` / `DEBUG`
558 /// (logging — observable side effect only) or `EXCEPTION`
559 /// (aborts the trigger and propagates as an error). v7.12.6
560 /// supports the basic format-string substitution PG uses
561 /// (`%` placeholders consumed positionally).
562 Raise {
563 level: RaiseLevel,
564 message: String,
565 args: Vec<Expr>,
566 },
567 /// v7.12.6 — embedded SQL statement inside the trigger body
568 /// (`INSERT INTO …`, `UPDATE …`, `DELETE FROM …`, `SELECT …`).
569 /// NEW.col / OLD.col references inside the embedded
570 /// statement's expression tree are substituted with the
571 /// current trigger context before the engine re-executes the
572 /// statement. Recursion depth into nested triggers is
573 /// bounded by the engine's existing trigger-fire guard.
574 EmbeddedSql(Box<Statement>),
575}
576
577#[derive(Debug, Clone, Copy, PartialEq, Eq)]
578pub enum RaiseLevel {
579 /// `RAISE NOTICE` — diagnostic message, observable in the
580 /// server log. Does not affect the trigger's outcome.
581 Notice,
582 /// `RAISE WARNING` — like NOTICE, slightly louder severity.
583 Warning,
584 /// `RAISE INFO` — like NOTICE, slightly quieter.
585 Info,
586 /// `RAISE LOG` — like NOTICE, lower priority.
587 Log,
588 /// `RAISE DEBUG` — like NOTICE, lowest priority.
589 Debug,
590 /// `RAISE EXCEPTION` — aborts the trigger function with the
591 /// given message, propagating up to the caller as a query-
592 /// level error.
593 Exception,
594}
595
596#[derive(Debug, Clone, PartialEq)]
597pub enum AssignTarget {
598 NewColumn(String),
599 OldColumn(String),
600 /// Reserved for v7.12.5 DECLARE'd local variables.
601 Local(String),
602}
603
604#[derive(Debug, Clone, PartialEq)]
605pub enum ReturnTarget {
606 /// `RETURN NEW;` — for BEFORE triggers, this is the row that
607 /// actually gets written (possibly with NEW.col mutations
608 /// applied). For AFTER triggers, the return value is ignored.
609 New,
610 /// `RETURN OLD;` — pass-through. For BEFORE DELETE this lets
611 /// the delete proceed; for BEFORE UPDATE / INSERT it's
612 /// equivalent to dropping the write.
613 Old,
614 /// `RETURN NULL;` — for BEFORE triggers, skips the write
615 /// entirely. For AFTER, the return value is ignored.
616 Null,
617 /// `RETURN <expr>;` — non-row return shape; reserved for the
618 /// scalar UDF surface in v7.12.5+. Executor errors when used
619 /// inside a trigger function.
620 Expr(Expr),
621}
622
623/// v7.12.4 — `CREATE [OR REPLACE] TRIGGER`. Always row-level
624/// (`FOR EACH ROW`) in v7.12.4 — statement-level triggers parse
625/// but the executor refuses them. `WHEN (cond)` clauses are out
626/// of scope; the trigger function can short-circuit on a leading
627/// IF inside its body once v7.12.5 lands IF.
628#[derive(Debug, Clone, PartialEq)]
629pub struct CreateTriggerStatement {
630 pub name: String,
631 pub or_replace: bool,
632 pub timing: TriggerTiming,
633 /// At least one event; `INSERT OR UPDATE OR DELETE` parses to
634 /// three entries in order.
635 pub events: Vec<TriggerEvent>,
636 pub table: String,
637 /// `FOR EACH ROW` vs `FOR EACH STATEMENT`. v7.12.4 ships
638 /// only `Row`; `Statement` parses but the executor refuses.
639 pub for_each: TriggerForEach,
640 /// Name of the function to invoke. v7.12.4 requires the
641 /// function to be `CREATE FUNCTION`'d earlier; forward
642 /// references (PG accepts) are deferred to v7.12.5.
643 pub function: String,
644 /// v7.13.0 — `UPDATE OF col, col, …` column-list filter
645 /// (mailrs round-5 G7). Non-empty only when the events list
646 /// contains UPDATE and the user wrote the column-list filter.
647 /// PG fires the trigger only when at least one of these
648 /// columns appears in the SET clause; SPG conservatively
649 /// fires on any UPDATE matching the listed columns or
650 /// rewriting them at the row level. Empty vec = no filter
651 /// (fire on every UPDATE).
652 pub update_columns: Vec<String>,
653}
654
655#[derive(Debug, Clone, Copy, PartialEq, Eq)]
656pub enum TriggerTiming {
657 /// Fires before the row is written; the trigger function's
658 /// return value (NEW or NULL) decides the row content and
659 /// whether the write proceeds at all.
660 Before,
661 /// Fires after the row is written; the return value is
662 /// ignored.
663 After,
664 /// `INSTEAD OF` is PG-VIEW-trigger-only and out of scope for
665 /// v7.12.4 (SPG has no updatable-view surface).
666 InsteadOf,
667}
668
669#[derive(Debug, Clone, Copy, PartialEq, Eq)]
670pub enum TriggerEvent {
671 Insert,
672 Update,
673 Delete,
674 /// `TRUNCATE` event parses; SPG has no TRUNCATE statement
675 /// so the trigger never fires.
676 Truncate,
677}
678
679#[derive(Debug, Clone, Copy, PartialEq, Eq)]
680pub enum TriggerForEach {
681 Row,
682 Statement,
683}
684
685#[derive(Debug, Clone, PartialEq)]
686pub struct CreateIndexStatement {
687 pub name: String,
688 pub table: String,
689 pub column: String,
690 /// Optional `USING <method>` clause. v2.0 recognises `hnsw` (NSW
691 /// graph for vector kNN); unspecified is the default B-tree index.
692 pub method: IndexMethod,
693 /// `IF NOT EXISTS` — engine returns `CommandOk` no-op when the
694 /// index name already exists, instead of raising `DuplicateIndex`.
695 pub if_not_exists: bool,
696 /// v6.8.0 — `INCLUDE (col1, col2, …)` columns. Identifies the
697 /// non-key columns the planner should treat as "covered" by
698 /// this index when checking whether a query can run as an
699 /// index-only scan. Empty when no `INCLUDE` clause was given.
700 pub included_columns: Vec<String>,
701 /// v6.8.1 — `WHERE <expr>` partial-index predicate. Only rows
702 /// for which `<expr>` evaluates truthy enter the index;
703 /// queries whose `WHERE` clause's canonical Display form
704 /// matches this expression's Display form can be served by the
705 /// partial index. Stored as a parsed `Expr` so the engine
706 /// re-uses the existing evaluation path; storage persists the
707 /// Display form on the catalog snapshot.
708 pub partial_predicate: Option<Expr>,
709 /// v6.8.2 — expression-based index. When `Some(expr)`, the
710 /// index key is the result of `expr` evaluated on each row
711 /// (e.g. `CREATE INDEX … (lower(name))`). The `column`
712 /// field still names the *primary* column the expression
713 /// touches so existing planner shortcuts that resolve a
714 /// column position stay valid. `None` = plain
715 /// column-reference index (the legacy shape).
716 pub expression: Option<Expr>,
717 /// v7.9.14 — extra column names after the leading column in a
718 /// multi-column `CREATE INDEX … (a, b, c)`. mailrs F2. The
719 /// planner today still only uses the leading column for index
720 /// seeks; the extras are tracked verbatim so the same DDL
721 /// round-trips through WAL replay + catalog snapshot, and so
722 /// the engine can emit a clear warning at INDEX CREATE time
723 /// that only the leading column is currently honoured.
724 /// Composite BTree index keys land in v7.10.
725 pub extra_columns: Vec<String>,
726 /// v7.9.29 — `CREATE UNIQUE INDEX …`. When true the engine
727 /// enforces uniqueness on the indexed key (combined with the
728 /// `partial_predicate` filter — only rows where the predicate
729 /// evaluates truthy enter the uniqueness check). Standard SQL
730 /// and PG's canonical way to express conditional uniqueness.
731 /// mailrs K1.
732 pub is_unique: bool,
733 /// v7.15.0 — operator class on the leading column, when the
734 /// CREATE INDEX named one (`(col vector_cosine_ops)` shape).
735 /// Lower-cased. Most opclasses are still informational; the
736 /// engine routes on `gin_trgm_ops` specifically to build a
737 /// trigram-shingle GIN over a TEXT column, and otherwise
738 /// keeps the current "accepted and discarded" behaviour for
739 /// pg_dump compatibility.
740 pub opclass: Option<String>,
741}
742
743#[derive(Debug, Clone, Copy, PartialEq, Eq)]
744pub enum IndexMethod {
745 /// Default — B-tree over `IndexKey`. Used for equality / range
746 /// lookups on scalar columns.
747 BTree,
748 /// `USING hnsw` — NSW graph for kNN over a vector column.
749 Hnsw,
750 /// v6.7.1 — `USING brin` — Block Range INdex. Per-segment
751 /// metadata that records (min_key, max_key) for each page in a
752 /// cold-tier segment, on the indexed column. The optimizer
753 /// can use these summaries to skip pages whose range does NOT
754 /// overlap a query's WHERE predicate. BRIN indexes carry no
755 /// in-memory data — the summaries live in the segment v2
756 /// envelope's sidecar. Created via the standard
757 /// `CREATE INDEX … USING brin (col)` syntax.
758 Brin,
759 /// v7.12.3 — `USING gin` — inverted index over a `tsvector`
760 /// column. Posting lists map `lexeme word` → row locators; the
761 /// planner uses them to narrow `WHERE col @@ tsquery` to the
762 /// candidate rows whose vectors contain a matching term, then
763 /// re-evaluates the full `@@` semantics on each candidate.
764 /// Replaces the v7.9.26b `USING gin` → BTree fallback that
765 /// silently degraded to a full scan at query time.
766 Gin,
767}
768
769#[derive(Debug, Clone, PartialEq)]
770pub struct CreateTableStatement {
771 pub name: String,
772 pub columns: Vec<ColumnDef>,
773 /// `IF NOT EXISTS` — engine returns `CommandOk` no-op when the
774 /// table name already exists, instead of raising `DuplicateTable`.
775 pub if_not_exists: bool,
776 /// v7.6.0 — table-level `FOREIGN KEY (...) REFERENCES ...`
777 /// constraints. Column-level `REFERENCES` (single-column inline
778 /// form) is normalised into this vec at parse time so the engine
779 /// sees one uniform list.
780 pub foreign_keys: Vec<ForeignKeyConstraint>,
781 /// v7.9.18 — table-level constraints: `PRIMARY KEY (a, b)` and
782 /// `UNIQUE (a, b, ...)`. mailrs migration follow-up G1 + G6.
783 /// Engine resolves each into a BTree index named after the
784 /// constraint's leading column at CREATE TABLE time; INSERT
785 /// path enforces composite uniqueness via row scan on the
786 /// leading column index.
787 pub table_constraints: Vec<TableConstraint>,
788}
789
790/// v7.9.18 — table-level constraint at the end of a CREATE TABLE
791/// column list. Either a composite PRIMARY KEY or a UNIQUE
792/// (single- or multi-column).
793#[derive(Debug, Clone, PartialEq)]
794pub enum TableConstraint {
795 /// `PRIMARY KEY (col1, col2, ...)`. Implies NOT NULL on each
796 /// referenced column. Engine builds a BTree index named
797 /// `<table>_pkey` and enforces composite uniqueness on INSERT.
798 PrimaryKey {
799 name: Option<String>,
800 columns: Vec<String>,
801 },
802 /// `UNIQUE (col1, col2, ...)`. Engine builds a BTree index
803 /// named `<table>_<leading_col>_key` (single-column) or
804 /// `<table>_<leading_col>_<…>_key` (composite) and enforces
805 /// uniqueness on INSERT.
806 Unique {
807 name: Option<String>,
808 columns: Vec<String>,
809 /// v7.13.0 — `NULLS NOT DISTINCT` modifier (mailrs round-5
810 /// G10). PG 15+ flips the NULL handling so any number of
811 /// NULL rows collide on the constraint. Default is
812 /// `false` (NULLS DISTINCT, standard SQL behaviour).
813 nulls_not_distinct: bool,
814 },
815 /// v7.13.0 — `CHECK (<expr>)` table-level constraint
816 /// (mailrs round-5 G3). Column-level inline CHECKs fold into
817 /// this same variant at parse time. Engine evaluates the
818 /// predicate against each INSERT/UPDATE candidate row; a
819 /// false / NULL result rejects the mutation.
820 Check {
821 name: Option<String>,
822 expr: Expr,
823 },
824 /// v7.15.0 — MySQL `KEY name (cols)` / `INDEX name (cols)`
825 /// non-unique secondary-index declaration inline in CREATE
826 /// TABLE. Engine builds a BTree index on the leading column
827 /// (composite columns parse but only the leading column is
828 /// honoured at v7.15 — matches the existing
829 /// `CreateIndexStatement::extra_columns` semantics). Useful
830 /// for `mysql/blog`-style schemas that lean on routine
831 /// secondary indexes for ORM lookups.
832 Index {
833 name: Option<String>,
834 columns: Vec<String>,
835 },
836}
837
838#[derive(Debug, Clone, PartialEq)]
839pub struct ColumnDef {
840 pub name: String,
841 pub ty: ColumnTypeName,
842 pub nullable: bool,
843 /// `DEFAULT <expr>` literal supplied at CREATE TABLE. Engine
844 /// evaluates this once (with an empty row) and caches the resulting
845 /// `Value` on the column schema.
846 pub default: Option<Expr>,
847 /// MySQL-style `AUTO_INCREMENT` — the engine maintains a counter
848 /// per such column and fills the slot when INSERT leaves it
849 /// unbound (omitted from a column-list INSERT or explicitly NULL).
850 pub auto_increment: bool,
851 /// v7.9.13 — inline `PRIMARY KEY` column constraint. mailrs
852 /// migration follow-up F1. Implies `NOT NULL`. Engine creates
853 /// an implicit BTree index named `<table>_pkey` over this
854 /// column at CREATE TABLE time, satisfying the parent-side
855 /// index requirement for any FOREIGN KEY pointing at it.
856 pub is_primary_key: bool,
857 /// v7.13.0 — inline `UNIQUE` column constraint
858 /// (mailrs round-5 G2). The CREATE TABLE handler folds this
859 /// into a single-column `TableConstraint::Unique` so the
860 /// engine path stays uniform with table-level UNIQUE.
861 pub is_unique: bool,
862 /// v7.13.0 — inline `CHECK (<expr>)` column constraint
863 /// (mailrs round-5 G3). Stored alongside the column so the
864 /// CREATE TABLE handler can fold these into table-level
865 /// CHECK constraints. Multiple inline CHECKs on the same
866 /// column are concatenated with AND at the table level.
867 pub check: Option<Expr>,
868}
869
870/// v7.6.0 — A single FOREIGN KEY constraint. Both column-level
871/// `REFERENCES` and table-level `FOREIGN KEY (...) REFERENCES ...`
872/// parse into this shape — the column-level form has a single-entry
873/// `columns` / `parent_columns`.
874#[derive(Debug, Clone, PartialEq)]
875pub struct ForeignKeyConstraint {
876 /// Optional `CONSTRAINT <name>` prefix. Engine ignores the name
877 /// today but parses + stores it so a future ALTER TABLE DROP
878 /// CONSTRAINT can target by name (v7.6.8).
879 pub name: Option<String>,
880 /// Local columns participating in the FK (≥ 1).
881 pub columns: Vec<String>,
882 /// Referenced parent table.
883 pub parent_table: String,
884 /// Referenced parent columns. Must have the same arity as
885 /// `columns`; engine validates parent has a PK / UNIQUE index
886 /// on exactly this column set (v7.6.1).
887 pub parent_columns: Vec<String>,
888 /// `ON DELETE` action. Defaults to `Restrict` if absent.
889 pub on_delete: FkAction,
890 /// `ON UPDATE` action. Defaults to `Restrict` if absent.
891 pub on_update: FkAction,
892}
893
894/// v7.6.0 — Referential action for `ON DELETE` / `ON UPDATE`.
895#[derive(Debug, Clone, Copy, PartialEq, Eq)]
896pub enum FkAction {
897 /// Reject the parent mutation if any child row references it.
898 /// SQL spec default; SPG default when no clause is given.
899 Restrict,
900 /// Recursively propagate the parent's delete / update to the
901 /// child rows. Same TX.
902 Cascade,
903 /// Set the child FK column(s) to NULL. Requires the FK columns
904 /// to be NULL-able.
905 SetNull,
906 /// Set the child FK column(s) to their declared DEFAULT.
907 /// Requires the child column(s) to have DEFAULT.
908 SetDefault,
909 /// SQL spec `NO ACTION` (deferred check). SPG treats this as
910 /// `Restrict` because the single-writer model has no deferred
911 /// constraint window; the keyword is accepted for compatibility.
912 NoAction,
913}
914
915/// In-cell encoding for a `VECTOR(N)` column. v6.0.1 added the
916/// optional `USING <encoding>` clause; omitting it keeps the
917/// pre-v6 `F32` default. `Sq8` quantises each cell to a per-vector
918/// affine `(min, max, [u8; dim])` triple (4× compression). `F16`
919/// (v6.0.3, DDL keyword `HALF`) stores each element as IEEE-754
920/// binary16 (2× compression, ~3 decimal digits of precision).
921#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
922pub enum VecEncoding {
923 /// IEEE-754 binary32. Pre-v6 default; matches pgvector's
924 /// uncompressed `vector` type wire / storage layout.
925 #[default]
926 F32,
927 /// v6.0.1 SQ8 — per-vector affine 8-bit quantisation. See
928 /// `spg_storage::quantize::Sq8Vector` for the math + recall
929 /// envelope (≥ 0.95 on Gaussian / unit-sphere corpora at
930 /// dim ≥ 32).
931 Sq8,
932 /// v6.0.3 halfvec — IEEE-754 binary16 (half-precision)
933 /// per-element. DDL keyword `HALF` (pgvector convention).
934 /// Bit-exact dequantise to f32 at the storage layer; no
935 /// rerank pass needed for kNN search.
936 F16,
937}
938
939impl fmt::Display for VecEncoding {
940 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
941 match self {
942 Self::F32 => f.write_str("F32"),
943 Self::Sq8 => f.write_str("SQ8"),
944 // pgvector convention: DDL keyword is `HALF`, not `F16`.
945 Self::F16 => f.write_str("HALF"),
946 }
947 }
948}
949
950/// SQL-level type names. The mapping to the storage runtime's `DataType`
951/// happens in `spg-engine` — keeping `spg-sql` free of storage deps.
952#[derive(Debug, Clone, Copy, PartialEq, Eq)]
953pub enum ColumnTypeName {
954 SmallInt,
955 Int,
956 BigInt,
957 Float,
958 Text,
959 /// `VARCHAR(N)` — TEXT capped at N Unicode characters.
960 Varchar(u32),
961 /// `CHAR(N)` — TEXT right-padded with spaces to exactly N characters.
962 Char(u32),
963 Bool,
964 /// pgvector fixed-dimension `VECTOR(N)`. v6.0.1 added the
965 /// `USING <encoding>` clause; omitting it surfaces as
966 /// `encoding = VecEncoding::F32` (the pre-v6 default).
967 Vector {
968 dim: u32,
969 encoding: VecEncoding,
970 },
971 /// `NUMERIC` / `NUMERIC(p)` / `NUMERIC(p, s)` — exact decimal.
972 /// Bare `NUMERIC` and `NUMERIC(p)` both surface with `scale=0`.
973 Numeric(u8, u8),
974 /// `DATE` — calendar day, no time-of-day component.
975 Date,
976 /// `TIMESTAMP` / `MySQL` `DATETIME` — instant with microsecond
977 /// precision.
978 Timestamp,
979 /// v7.9.2 `TIMESTAMPTZ` / `TIMESTAMP WITH TIME ZONE`. SPG
980 /// stores all timestamps as UTC microseconds-since-epoch and
981 /// does not carry per-row offset (PG's internal representation
982 /// is the same — TZ is a display convention). The distinction
983 /// from `TIMESTAMP` exists for the PG-wire layer to advertise
984 /// OID 1184 so sqlx-style clients decode into
985 /// `chrono::DateTime<Utc>` instead of `NaiveDateTime`.
986 Timestamptz,
987 /// v4.9 `JSON` — text-backed JSON document. No parse-time
988 /// validation; the engine round-trips the literal verbatim.
989 /// PG OID 114 on the wire.
990 Json,
991 /// v7.9.0 `JSONB` — same storage shape as Json, advertised as
992 /// PG OID 3802 on the wire so sqlx-style binary-typed clients
993 /// decode without a custom type registration.
994 Jsonb,
995 /// v7.10.4 `BYTES` / `BYTEA` — raw binary blob. PG wire OID 17.
996 /// Literal forms (decoded by the engine at coercion time):
997 /// - PG hex form: `'\xDEADBEEF'`
998 /// - Escape form: `'foo\\000bar'` (backslash octal triples)
999 Bytes,
1000 /// v7.10.10 `TEXT[]` — single-dimension TEXT array. PG wire
1001 /// OID 1009. Literal forms accepted by the parser:
1002 /// - `ARRAY['a', 'b', NULL]`
1003 /// - `'{a,b,NULL}'::TEXT[]` (engine decodes the external
1004 /// form at coerce time)
1005 TextArray,
1006 /// v7.11.13 `INT[]` — single-dimension i32 array. PG wire OID
1007 /// 1007. Same literal forms as TEXT[] (substituting integer
1008 /// elements).
1009 IntArray,
1010 /// v7.11.13 `BIGINT[]` — single-dimension i64 array. PG wire
1011 /// OID 1016.
1012 BigIntArray,
1013 /// v7.12.0 `tsvector` — PG full-text search lexeme set. PG
1014 /// wire OID 3614. Literal: `'foo:1 bar:2'::tsvector` (PG
1015 /// external form). G-CRIT-3.
1016 TsVector,
1017 /// v7.12.0 `tsquery` — PG full-text search parse tree. PG
1018 /// wire OID 3615.
1019 TsQuery,
1020}
1021
1022impl fmt::Display for ColumnTypeName {
1023 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1024 match self {
1025 Self::SmallInt => f.write_str("SMALLINT"),
1026 Self::Int => f.write_str("INT"),
1027 Self::BigInt => f.write_str("BIGINT"),
1028 Self::Float => f.write_str("FLOAT"),
1029 Self::Text => f.write_str("TEXT"),
1030 Self::Varchar(n) => write!(f, "VARCHAR({n})"),
1031 Self::Char(n) => write!(f, "CHAR({n})"),
1032 Self::Bool => f.write_str("BOOL"),
1033 Self::Vector { dim, encoding } => match encoding {
1034 VecEncoding::F32 => write!(f, "VECTOR({dim})"),
1035 VecEncoding::Sq8 => write!(f, "VECTOR({dim}) USING SQ8"),
1036 VecEncoding::F16 => write!(f, "VECTOR({dim}) USING HALF"),
1037 },
1038 Self::Json => f.write_str("JSON"),
1039 Self::Jsonb => f.write_str("JSONB"),
1040 Self::Bytes => f.write_str("BYTEA"),
1041 Self::TextArray => f.write_str("TEXT[]"),
1042 Self::IntArray => f.write_str("INT[]"),
1043 Self::BigIntArray => f.write_str("BIGINT[]"),
1044 Self::TsVector => f.write_str("TSVECTOR"),
1045 Self::TsQuery => f.write_str("TSQUERY"),
1046 Self::Numeric(p, s) => {
1047 if *s == 0 {
1048 write!(f, "NUMERIC({p})")
1049 } else {
1050 write!(f, "NUMERIC({p}, {s})")
1051 }
1052 }
1053 Self::Date => f.write_str("DATE"),
1054 Self::Timestamp => f.write_str("TIMESTAMP"),
1055 Self::Timestamptz => f.write_str("TIMESTAMPTZ"),
1056 }
1057 }
1058}
1059
1060/// `UPDATE <table> SET col = expr [, ...] [WHERE cond]`. v4.4 — the
1061/// engine evaluates `expr` per matched row in the table's row order
1062/// and rewrites cells in place. Indexed columns are dropped + re-
1063/// inserted into the affected B-tree on each row change.
1064#[derive(Debug, Clone, PartialEq)]
1065pub struct UpdateStatement {
1066 pub table: String,
1067 pub assignments: Vec<(String, Expr)>,
1068 pub where_: Option<Expr>,
1069 /// v7.9.4 — `RETURNING <projection>`. None = no RETURNING
1070 /// clause (legacy CommandComplete path). Some = engine
1071 /// evaluates the projection over each mutated row and
1072 /// streams the result as a Rows QueryResult.
1073 pub returning: Option<Vec<SelectItem>>,
1074}
1075
1076/// `DELETE FROM <table> [WHERE cond]`. v4.4 — removes matched rows
1077/// from the active catalog and prunes them from every index.
1078#[derive(Debug, Clone, PartialEq)]
1079pub struct DeleteStatement {
1080 pub table: String,
1081 pub where_: Option<Expr>,
1082 /// v7.9.4 — `RETURNING <projection>`.
1083 pub returning: Option<Vec<SelectItem>>,
1084}
1085
1086#[derive(Debug, Clone, PartialEq)]
1087pub struct InsertStatement {
1088 pub table: String,
1089 /// Optional column list — `INSERT INTO t (a, b) VALUES (...)`. When
1090 /// `None`, every tuple is positional and must match the table arity.
1091 /// When `Some`, the engine maps each tuple slot to the named column and
1092 /// fills the rest with NULL (must be nullable).
1093 pub columns: Option<Vec<String>>,
1094 /// One or more `(expr, expr, ...)` tuples — the multi-row VALUES form.
1095 /// v1.3+ accepts `INSERT INTO t VALUES (a), (b)`. Empty when
1096 /// `select_source` is `Some` (the engine builds rows from the
1097 /// inner SELECT result set instead).
1098 pub rows: Vec<Vec<Expr>>,
1099 /// v7.13.0 — `INSERT INTO t [(cols)] SELECT …` (mailrs
1100 /// round-5 G4). When present, `rows` is empty and the engine
1101 /// materialises the SELECT result, coerces each output tuple to
1102 /// the target column types, and inserts as a single batch.
1103 pub select_source: Option<Box<SelectStatement>>,
1104 /// v7.9.7 — `ON CONFLICT (cols) DO { NOTHING | UPDATE SET … }`
1105 /// upsert clause. None = legacy INSERT (conflict raises a
1106 /// DuplicateKey error). mailrs migration blocker #2.
1107 pub on_conflict: Option<OnConflictClause>,
1108 /// v7.9.4 — `RETURNING <projection>`.
1109 pub returning: Option<Vec<SelectItem>>,
1110}
1111
1112/// v7.9.7 — INSERT upsert clause: `ON CONFLICT (target) DO action`.
1113#[derive(Debug, Clone, PartialEq)]
1114pub struct OnConflictClause {
1115 /// Local columns that identify the conflict (must match a
1116 /// UNIQUE / PRIMARY KEY index on the target table). Empty
1117 /// list means the user wrote `ON CONFLICT DO …` without a
1118 /// target — engine picks the table's first BTree index by
1119 /// convention.
1120 pub target_columns: Vec<String>,
1121 /// The action on conflict.
1122 pub action: OnConflictAction,
1123}
1124
1125/// v7.9.7 — action on conflict.
1126#[derive(Debug, Clone, PartialEq)]
1127pub enum OnConflictAction {
1128 /// `DO NOTHING` — INSERT proceeds for non-conflicting rows,
1129 /// silently skips conflicting ones.
1130 Nothing,
1131 /// `DO UPDATE SET col = expr [, …] [WHERE cond]`. `assignments`
1132 /// may reference `EXCLUDED.col` to read the incoming row's
1133 /// value (engine wires `EXCLUDED` as a virtual table).
1134 Update {
1135 assignments: Vec<(String, Expr)>,
1136 where_: Option<Expr>,
1137 },
1138}
1139
1140#[derive(Debug, Clone, PartialEq)]
1141pub struct SelectStatement {
1142 /// v4.11: `WITH name AS (SELECT ...) [, ...]` common-table
1143 /// expressions, materialised once at query start before the
1144 /// body SELECT runs. Empty for a regular SELECT. Non-recursive
1145 /// only — no `WITH RECURSIVE` for v4.x.
1146 pub ctes: Vec<Cte>,
1147 pub distinct: bool,
1148 pub items: Vec<SelectItem>,
1149 pub from: Option<FromClause>,
1150 pub where_: Option<Expr>,
1151 pub group_by: Option<Vec<Expr>>,
1152 /// v6.4.1 — `GROUP BY ALL` shortcut: when true, the planner
1153 /// expands `group_by` to every non-aggregate SELECT-list item
1154 /// before the executor runs. Mutually exclusive with an
1155 /// explicit `group_by` list (the parser sets exactly one).
1156 pub group_by_all: bool,
1157 /// `HAVING <expr>` — filter applied *after* `GROUP BY` aggregation.
1158 /// Supports aggregate calls (e.g. `HAVING count(*) > 1`); the
1159 /// aggregate executor resolves them through the same synthetic
1160 /// schema used for the SELECT items.
1161 pub having: Option<Expr>,
1162 /// UNION / UNION ALL chain. Empty for a plain SELECT. Each peer is
1163 /// itself a `SelectStatement` with `order_by = None` and `limit =
1164 /// None` (the parser enforces that — ORDER BY / LIMIT belong to the
1165 /// top of the chain).
1166 pub unions: Vec<(UnionKind, SelectStatement)>,
1167 /// v6.4.0 — multi-key ORDER BY. Empty `Vec` means no ORDER BY.
1168 /// Keys are matched left-to-right: first key decides, ties break
1169 /// to the second, etc.
1170 pub order_by: Vec<OrderBy>,
1171 /// `LIMIT <n>` — bound on row output. `n` is an integer
1172 /// literal **or** (v7.9.24) a placeholder `$N` resolved
1173 /// against the prepared-statement Bind values. mailrs
1174 /// migration follow-up H2.
1175 pub limit: Option<LimitExpr>,
1176 /// `OFFSET <n>` — drop the first `n` rows after ORDER BY but
1177 /// before LIMIT (so `LIMIT 10 OFFSET 5` keeps rows 6..=15).
1178 pub offset: Option<LimitExpr>,
1179}
1180
1181/// v7.9.24 — LIMIT / OFFSET value. Integer literal at parse
1182/// time or a placeholder `$N` resolved during extended-query
1183/// Bind. mailrs migration follow-up H2.
1184#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1185pub enum LimitExpr {
1186 /// `LIMIT 10` — value known at parse time.
1187 Literal(u32),
1188 /// `LIMIT $N` — the 1-based parameter index, resolved against
1189 /// the bind values when the prepared statement executes.
1190 Placeholder(u16),
1191}
1192
1193impl fmt::Display for LimitExpr {
1194 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1195 match self {
1196 Self::Literal(n) => write!(f, "{n}"),
1197 Self::Placeholder(n) => write!(f, "${n}"),
1198 }
1199 }
1200}
1201
1202impl LimitExpr {
1203 /// Convenience for the simple-query path where no placeholders
1204 /// can possibly exist. Returns the literal value or `None` if
1205 /// this is a placeholder (caller must surface as Unsupported).
1206 pub fn as_literal(self) -> Option<u32> {
1207 match self {
1208 Self::Literal(n) => Some(n),
1209 Self::Placeholder(_) => None,
1210 }
1211 }
1212}
1213
1214/// v7.9.24 — extract LIMIT / OFFSET as a `u32` literal. After
1215/// the engine's `substitute_placeholders` pass these are
1216/// always Literal; in the simple-query path a Placeholder
1217/// shape returns None (executor surfaces as
1218/// "LIMIT/OFFSET ${n} requires prepared-statement binding").
1219impl SelectStatement {
1220 #[must_use]
1221 pub fn limit_literal(&self) -> Option<u32> {
1222 self.limit.and_then(LimitExpr::as_literal)
1223 }
1224 #[must_use]
1225 pub fn offset_literal(&self) -> Option<u32> {
1226 self.offset.and_then(LimitExpr::as_literal)
1227 }
1228}
1229
1230#[derive(Debug, Clone, PartialEq)]
1231pub struct Cte {
1232 pub name: String,
1233 pub body: SelectStatement,
1234 /// v4.22: `WITH RECURSIVE` — set when the WITH clause had the
1235 /// RECURSIVE keyword. Applies to every CTE in the clause per
1236 /// PG semantics. A non-recursive body in a RECURSIVE WITH is
1237 /// allowed; the engine just runs it once.
1238 pub recursive: bool,
1239 /// v4.22: optional `WITH name(a, b, c)` column-name list. When
1240 /// non-empty, these override the body's output column names
1241 /// position-by-position; the engine errors out if the count
1242 /// doesn't match the body's projection width.
1243 pub column_overrides: Vec<String>,
1244}
1245
1246#[derive(Debug, Clone, PartialEq)]
1247pub struct OrderBy {
1248 pub expr: Expr,
1249 /// `false` = ASC (default), `true` = DESC.
1250 pub desc: bool,
1251}
1252
1253#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1254pub enum UnionKind {
1255 /// `UNION` — dedupes the combined set.
1256 Distinct,
1257 /// `UNION ALL` — concatenates without dedup.
1258 All,
1259}
1260
1261#[derive(Debug, Clone, PartialEq)]
1262pub enum SelectItem {
1263 Wildcard,
1264 Expr { expr: Expr, alias: Option<String> },
1265}
1266
1267#[derive(Debug, Clone, PartialEq)]
1268pub struct TableRef {
1269 pub name: String,
1270 pub alias: Option<String>,
1271 /// v6.10.2 — `AS OF SEGMENT '<id>'` cold-tier time-travel.
1272 /// When `Some(id)`, the scan restricts to rows that live in
1273 /// segment `<id>` only — useful for forensic inspection of a
1274 /// specific freezer-emitted segment without exposing the hot
1275 /// tier. `AS OF TIMESTAMP <ts>` (PG-flavoured time travel)
1276 /// is STABILITY carve-out for v6.10 — needs the freezer to
1277 /// stamp each segment with a wall-clock at creation time.
1278 pub as_of_segment: Option<u32>,
1279 /// v7.11.7 — `FROM unnest(<expr>) [AS] <alias>` set-returning
1280 /// source. When `Some`, `name` is the alias (defaulting to
1281 /// `"unnest"` when no `AS` is given) and the engine builds a
1282 /// synthetic single-column table by evaluating the expression
1283 /// once at SELECT entry. Each TEXT[] element becomes one row;
1284 /// NULL elements become NULL cells. v7.11 supported
1285 /// uncorrelated UNNEST only as the FROM primary; v7.13.2
1286 /// (mailrs round-6 S5) widens to UNNEST in any FROM-list
1287 /// position (cross-join with regular tables).
1288 pub unnest_expr: Option<Box<Expr>>,
1289 /// v7.13.2 — mailrs round-6 S5. PG-standard
1290 /// `UNNEST(<arr>) AS alias(col_name)` column-list aliasing:
1291 /// when non-empty, the first entry overrides the projected
1292 /// column name for the unnested column. Empty = fall back to
1293 /// the table alias (pre-v7.13.2 behaviour).
1294 pub unnest_column_aliases: Vec<String>,
1295}
1296
1297/// FROM clause shape. v1.10 accepts a primary table plus a flat list of
1298/// joined peers — `FROM a [, b]* [INNER|LEFT] JOIN c ON expr ...`. The
1299/// joins evaluate left-associatively in nested-loop order.
1300#[derive(Debug, Clone, PartialEq)]
1301pub struct FromClause {
1302 pub primary: TableRef,
1303 pub joins: Vec<FromJoin>,
1304}
1305
1306#[derive(Debug, Clone, PartialEq)]
1307pub struct FromJoin {
1308 pub kind: JoinKind,
1309 pub table: TableRef,
1310 /// Required for INNER/LEFT; must be `None` for CROSS / comma-list.
1311 pub on: Option<Expr>,
1312}
1313
1314#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1315pub enum JoinKind {
1316 Inner,
1317 Left,
1318 Cross,
1319}
1320
1321#[derive(Debug, Clone, PartialEq)]
1322pub enum Expr {
1323 Literal(Literal),
1324 Column(ColumnName),
1325 /// v6.1.1 — `$N` parameter placeholder for the extended query
1326 /// protocol. The number is 1-based per PostgreSQL convention.
1327 /// Evaluation looks up `params[N-1]` from the prepared-statement
1328 /// bind buffer; out-of-range indices raise a runtime error
1329 /// (same shape as a column-not-found miss).
1330 Placeholder(u16),
1331 Binary {
1332 lhs: Box<Expr>,
1333 op: BinOp,
1334 rhs: Box<Expr>,
1335 },
1336 Unary {
1337 op: UnOp,
1338 expr: Box<Expr>,
1339 },
1340 /// PG-style `expr::TYPE` cast. v1.3 supports VECTOR, INT, BIGINT, FLOAT,
1341 /// TEXT, BOOL targets; engine coerces at evaluation time.
1342 Cast {
1343 expr: Box<Expr>,
1344 target: CastTarget,
1345 },
1346 /// Postfix `IS NULL` / `IS NOT NULL`. Returns BOOL.
1347 IsNull {
1348 expr: Box<Expr>,
1349 negated: bool,
1350 },
1351 /// Function call `name(args...)`. v1.4 supports a small built-in set
1352 /// (length, upper, lower, abs, coalesce); unknown names error at eval
1353 /// time so the parser stays open for v1.5 aggregates.
1354 FunctionCall {
1355 name: String,
1356 args: Vec<Expr>,
1357 },
1358 /// SQL `LIKE` predicate. `pattern` evaluates to text at runtime;
1359 /// wildcards are `%` (any run) and `_` (one char), backslash escapes
1360 /// the next char (so `\%` matches a literal `%`).
1361 Like {
1362 expr: Box<Expr>,
1363 pattern: Box<Expr>,
1364 negated: bool,
1365 },
1366 /// v4.12 window function call: `name(args) OVER (PARTITION BY
1367 /// ... ORDER BY ...)`. Supports `ROW_NUMBER` / `RANK` /
1368 /// `DENSE_RANK` and the partition-aware aggregates `SUM` /
1369 /// `AVG` / `COUNT` / `MIN` / `MAX`. The window frame defaults to "entire partition" for
1370 /// unordered windows and "from start of partition through
1371 /// current row" for ordered windows — no explicit ROWS /
1372 /// RANGE clause in v4.12 MVP.
1373 WindowFunction {
1374 name: String,
1375 args: Vec<Expr>,
1376 partition_by: Vec<Expr>,
1377 order_by: Vec<(Expr, bool /* desc */)>,
1378 /// v4.20 explicit frame. `None` means "use the default":
1379 /// whole-partition when unordered, running aggregate from
1380 /// partition start through current row when ordered.
1381 frame: Option<WindowFrame>,
1382 /// v6.4.2 — `IGNORE NULLS` / `RESPECT NULLS` modifier on
1383 /// LAG / LEAD / FIRST_VALUE / LAST_VALUE. Default is
1384 /// `Respect` (PG / ANSI default — NULLs participate). Other
1385 /// window functions ignore this flag.
1386 null_treatment: NullTreatment,
1387 },
1388 /// v4.10 scalar subquery — `(SELECT ...)` used in expression
1389 /// position. Must return exactly one row × one column at eval
1390 /// time; the engine errors out otherwise. Uncorrelated only —
1391 /// the inner SELECT cannot reference outer columns.
1392 ScalarSubquery(Box<SelectStatement>),
1393 /// v4.10 `[NOT] EXISTS (SELECT ...)`. Returns Bool. Inner
1394 /// projection is ignored; only row-count matters.
1395 Exists {
1396 subquery: Box<SelectStatement>,
1397 negated: bool,
1398 },
1399 /// v4.10 `expr [NOT] IN (SELECT ...)`. Inner SELECT must
1400 /// project exactly one column; membership is tested by Eq
1401 /// against each row's value (NULL handling follows ANSI:
1402 /// NULL ∈ list ⇒ NULL ; otherwise present ⇒ true).
1403 InSubquery {
1404 expr: Box<Expr>,
1405 subquery: Box<SelectStatement>,
1406 negated: bool,
1407 },
1408 /// `EXTRACT(<field> FROM <source>)` — pull an integer component
1409 /// out of a `DATE` or `TIMESTAMP`. Parsed as its own AST node
1410 /// because the `FROM` keyword is what separates the two halves,
1411 /// not a comma.
1412 Extract {
1413 field: ExtractField,
1414 source: Box<Expr>,
1415 },
1416 /// v7.10.10 — `ARRAY[expr, expr, …]` array constructor. Each
1417 /// element is evaluated independently; NULLs are allowed.
1418 /// v7.10 supports only single-dimension TEXT[] semantically;
1419 /// non-text elements coerce at engine evaluation time when
1420 /// the surrounding context (column type / cast) makes the
1421 /// target clear.
1422 Array(Vec<Expr>),
1423 /// v7.10.10 — array subscript `arr[i]`. PG 1-based; the
1424 /// engine returns NULL for out-of-range indices.
1425 ArraySubscript {
1426 target: Box<Expr>,
1427 index: Box<Expr>,
1428 },
1429 /// v7.10.12 — `expr op ANY(arr)` and `expr op ALL(arr)`. The
1430 /// operator is the comparison binary op (Eq / Ne / Lt / …);
1431 /// the engine desugars: `ANY` returns true if any element
1432 /// satisfies; `ALL` returns true only if every element does.
1433 /// NULL handling follows PG's three-valued logic.
1434 AnyAll {
1435 expr: Box<Expr>,
1436 op: BinOp,
1437 array: Box<Expr>,
1438 /// `true` = ANY, `false` = ALL.
1439 is_any: bool,
1440 },
1441 /// v7.13.0 — `CASE WHEN <cond> THEN <val> ... ELSE <val> END`
1442 /// (searched form, `operand` is None) and
1443 /// `CASE <expr> WHEN <val> THEN <val> ... END` (simple form,
1444 /// `operand` is the lead expression compared against each
1445 /// branch's match). Each `(when_expr, then_expr)` branch
1446 /// stays as written; engine short-circuits on the first match.
1447 /// `else_branch` is `None` when no ELSE; evaluates to NULL.
1448 /// mailrs round-5 G9.
1449 Case {
1450 operand: Option<Box<Expr>>,
1451 branches: Vec<(Expr, Expr)>,
1452 else_branch: Option<Box<Expr>>,
1453 },
1454}
1455
1456/// v6.4.2 — null treatment on `LAG` / `LEAD` / `FIRST_VALUE` /
1457/// `LAST_VALUE`. PG / ANSI default is `Respect` — NULLs participate
1458/// in the offset walk. `Ignore` causes the function to skip NULL
1459/// values in the argument expression, returning the next non-NULL.
1460#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
1461pub enum NullTreatment {
1462 #[default]
1463 Respect,
1464 Ignore,
1465}
1466
1467/// v4.20 explicit window frame: `ROWS|RANGE BETWEEN <bound> AND
1468/// <bound>`. `end` is `None` for the shorthand "ROWS <bound>"
1469/// where end implicitly = CURRENT ROW.
1470#[derive(Debug, Clone, PartialEq, Eq)]
1471pub struct WindowFrame {
1472 pub kind: FrameKind,
1473 pub start: FrameBound,
1474 pub end: Option<FrameBound>,
1475}
1476
1477#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1478pub enum FrameKind {
1479 Rows,
1480 Range,
1481}
1482
1483#[derive(Debug, Clone, PartialEq, Eq)]
1484pub enum FrameBound {
1485 UnboundedPreceding,
1486 OffsetPreceding(u64),
1487 CurrentRow,
1488 OffsetFollowing(u64),
1489 UnboundedFollowing,
1490}
1491
1492impl fmt::Display for FrameBound {
1493 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1494 match self {
1495 Self::UnboundedPreceding => f.write_str("UNBOUNDED PRECEDING"),
1496 Self::OffsetPreceding(n) => write!(f, "{n} PRECEDING"),
1497 Self::CurrentRow => f.write_str("CURRENT ROW"),
1498 Self::OffsetFollowing(n) => write!(f, "{n} FOLLOWING"),
1499 Self::UnboundedFollowing => f.write_str("UNBOUNDED FOLLOWING"),
1500 }
1501 }
1502}
1503
1504#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1505pub enum ExtractField {
1506 Year,
1507 Month,
1508 Day,
1509 Hour,
1510 Minute,
1511 Second,
1512 Microsecond,
1513}
1514
1515impl fmt::Display for ExtractField {
1516 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1517 f.write_str(match self {
1518 Self::Year => "YEAR",
1519 Self::Month => "MONTH",
1520 Self::Day => "DAY",
1521 Self::Hour => "HOUR",
1522 Self::Minute => "MINUTE",
1523 Self::Second => "SECOND",
1524 Self::Microsecond => "MICROSECOND",
1525 })
1526 }
1527}
1528
1529#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1530pub enum CastTarget {
1531 Int,
1532 BigInt,
1533 Float,
1534 Text,
1535 Bool,
1536 Vector,
1537 Date,
1538 Timestamp,
1539 /// v7.9.25 — `::INTERVAL` and `::TIMESTAMPTZ`. mailrs follow-up
1540 /// H3a. Engine reuses the existing runtime-interval / timestamp
1541 /// paths (parse the text input, return the matching Value).
1542 Interval,
1543 Timestamptz,
1544 /// v7.9.25 — `::JSON` and `::JSONB`. SPG already has both
1545 /// types (v7.9.0); the cast just routes Text→Json with the
1546 /// requested OID for the wire layer.
1547 Json,
1548 Jsonb,
1549 /// v7.9.26 — `::regtype` / `::regclass`. Parsed for PG dump
1550 /// compatibility; engine surfaces as Unsupported with a
1551 /// hint to use `SHOW TABLES` or `spg_table_ddl`. mailrs F3b.
1552 RegType,
1553 RegClass,
1554 /// v7.10.11 — `::TEXT[]`. Engine decodes the LHS Text into
1555 /// the PG external array form `{a,b,NULL}`.
1556 TextArray,
1557 /// v7.11.13 — `::INT[]` / `::BIGINT[]`. Decodes PG external
1558 /// `{1,2,3}` or widens a `TextArray` whose elements are
1559 /// integer-shaped.
1560 IntArray,
1561 BigIntArray,
1562 /// v7.12.0 — `::tsvector` / `::tsquery`. Decodes the PG
1563 /// external form text representation. Used by pg_dump output
1564 /// and by `WHERE col @@ 'term'::tsquery` literal patterns.
1565 TsVector,
1566 TsQuery,
1567}
1568
1569impl fmt::Display for CastTarget {
1570 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1571 f.write_str(match self {
1572 Self::Int => "int",
1573 Self::BigInt => "bigint",
1574 Self::Float => "float",
1575 Self::Text => "text",
1576 Self::Bool => "bool",
1577 Self::Vector => "vector",
1578 Self::Interval => "interval",
1579 Self::Timestamptz => "timestamptz",
1580 Self::Json => "json",
1581 Self::Jsonb => "jsonb",
1582 Self::RegType => "regtype",
1583 Self::RegClass => "regclass",
1584 Self::Date => "date",
1585 Self::Timestamp => "timestamp",
1586 Self::TextArray => "TEXT[]",
1587 Self::IntArray => "INT[]",
1588 Self::BigIntArray => "BIGINT[]",
1589 Self::TsVector => "tsvector",
1590 Self::TsQuery => "tsquery",
1591 })
1592 }
1593}
1594
1595#[derive(Debug, Clone, PartialEq)]
1596pub enum Literal {
1597 Integer(i64),
1598 Float(f64),
1599 String(String),
1600 Bool(bool),
1601 Null,
1602 /// pgvector-style array literal, e.g. `[1, 2.5, -3]`.
1603 Vector(Vec<f32>),
1604 /// `INTERVAL '<n> <unit> [<n> <unit> ...]'` — calendar-aware span.
1605 /// Split into a months part (because a month is not a fixed number of
1606 /// days) and a microseconds part (everything sub-month). `text` keeps
1607 /// the original spelling so Display round-trips byte-for-byte.
1608 Interval {
1609 months: i32,
1610 micros: i64,
1611 text: String,
1612 },
1613}
1614
1615#[derive(Debug, Clone, PartialEq, Eq)]
1616pub struct ColumnName {
1617 pub qualifier: Option<String>,
1618 pub name: String,
1619}
1620
1621#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1622pub enum BinOp {
1623 Or,
1624 And,
1625 Eq,
1626 NotEq,
1627 /// v7.9.27b — PG `a IS DISTINCT FROM b` / `a IS NOT DISTINCT
1628 /// FROM b`. NULL-safe equality: NULL IS NOT DISTINCT FROM
1629 /// NULL → true, NULL IS DISTINCT FROM NULL → false. The
1630 /// non-NULL behaviour matches `<>` / `=` exactly. Common in
1631 /// PG-style JOIN ON predicates and pg_dump output.
1632 IsDistinctFrom,
1633 IsNotDistinctFrom,
1634 Lt,
1635 LtEq,
1636 Gt,
1637 GtEq,
1638 Add,
1639 Sub,
1640 Mul,
1641 Div,
1642 /// pgvector L2 (Euclidean) distance `<->`. Defined for two vector
1643 /// operands of equal dimension; engine returns `Value::Float(d)`.
1644 L2Distance,
1645 /// pgvector inner-product `<#>` — returns `-Σ aᵢ bᵢ` so "smaller =
1646 /// more similar" remains true (matches pgvector's published convention).
1647 InnerProduct,
1648 /// pgvector cosine distance `<=>` — `1 - (a·b)/(|a| |b|)`.
1649 CosineDistance,
1650 /// SQL string concatenation `||`. NULL propagates.
1651 Concat,
1652 /// v4.14 `json -> key` — element access by string key (object)
1653 /// or integer index (array). Returns a JSON value.
1654 JsonGet,
1655 /// v4.14 `json ->> key` — same access, returns the result as
1656 /// TEXT (unwraps a top-level JSON string; renders other scalars
1657 /// as their canonical text).
1658 JsonGetText,
1659 /// v6.4.5 `json #> path_text` — walk the path encoded as a PG
1660 /// text array literal like `'{a,0,b}'`. Returns JSON.
1661 JsonGetPath,
1662 /// v6.4.5 `json #>> path_text` — same walk, returns TEXT.
1663 JsonGetPathText,
1664 /// v6.4.5 `json @> sub_json` — containment. Returns BOOL; true
1665 /// when every key/value in `sub_json` is structurally present in
1666 /// the left side. Matches PG semantics (top-level + recursive).
1667 JsonContains,
1668 /// v7.12.2 `tsvector @@ tsquery` — FTS match. Returns BOOL;
1669 /// 3VL on NULL. Symmetric: PG also accepts `tsquery @@
1670 /// tsvector` and engine eval normalises either ordering.
1671 TsMatch,
1672}
1673
1674#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1675pub enum UnOp {
1676 Not,
1677 Neg,
1678}
1679
1680// --- Display impls (round-trip-safe) --------------------------------------
1681
1682impl fmt::Display for Statement {
1683 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1684 match self {
1685 Self::Empty => Ok(()),
1686 Self::DropTable { names, if_exists } => {
1687 f.write_str("DROP TABLE ")?;
1688 if *if_exists {
1689 f.write_str("IF EXISTS ")?;
1690 }
1691 for (i, n) in names.iter().enumerate() {
1692 if i > 0 {
1693 f.write_str(", ")?;
1694 }
1695 write!(f, "{}", quote_ident(n))?;
1696 }
1697 Ok(())
1698 }
1699 Self::DropIndex { name, if_exists } => {
1700 f.write_str("DROP INDEX ")?;
1701 if *if_exists {
1702 f.write_str("IF EXISTS ")?;
1703 }
1704 write!(f, "{}", quote_ident(name))
1705 }
1706 Self::Select(s) => s.fmt(f),
1707 Self::CreateTable(s) => s.fmt(f),
1708 Self::CreateIndex(s) => s.fmt(f),
1709 Self::Insert(s) => s.fmt(f),
1710 Self::Update(s) => s.fmt(f),
1711 Self::Delete(s) => s.fmt(f),
1712 Self::Begin => f.write_str("BEGIN"),
1713 Self::Commit => f.write_str("COMMIT"),
1714 Self::Rollback => f.write_str("ROLLBACK"),
1715 Self::Savepoint(n) => write!(f, "SAVEPOINT {}", quote_ident(n)),
1716 Self::RollbackToSavepoint(n) => write!(f, "ROLLBACK TO SAVEPOINT {}", quote_ident(n)),
1717 Self::ReleaseSavepoint(n) => write!(f, "RELEASE SAVEPOINT {}", quote_ident(n)),
1718 Self::ShowTables => f.write_str("SHOW TABLES"),
1719 Self::ShowColumns(t) => write!(f, "SHOW COLUMNS FROM {}", quote_ident(t)),
1720 Self::CreateUser(s) => write!(
1721 f,
1722 "CREATE USER {} WITH PASSWORD '<redacted>' ROLE '{}'",
1723 quote_ident(&s.name),
1724 s.role
1725 ),
1726 Self::DropUser(n) => write!(f, "DROP USER {}", quote_ident(n)),
1727 Self::ShowUsers => f.write_str("SHOW USERS"),
1728 Self::ShowPublications => f.write_str("SHOW PUBLICATIONS"),
1729 Self::ShowSubscriptions => f.write_str("SHOW SUBSCRIPTIONS"),
1730 Self::CreateSubscription(s) => {
1731 write!(
1732 f,
1733 "CREATE SUBSCRIPTION {} CONNECTION '{}' PUBLICATION ",
1734 quote_ident(&s.name),
1735 s.conn_str.replace('\'', "''")
1736 )?;
1737 for (i, p) in s.publications.iter().enumerate() {
1738 if i > 0 {
1739 f.write_str(", ")?;
1740 }
1741 write!(f, "{}", quote_ident(p))?;
1742 }
1743 Ok(())
1744 }
1745 Self::DropSubscription(name) => {
1746 write!(f, "DROP SUBSCRIPTION {}", quote_ident(name))
1747 }
1748 Self::WaitForWalPosition { pos, timeout_ms } => {
1749 write!(f, "WAIT FOR WAL POSITION {pos}")?;
1750 if let Some(ms) = timeout_ms {
1751 write!(f, " WITH TIMEOUT {ms}")?;
1752 }
1753 Ok(())
1754 }
1755 Self::Analyze(None) => f.write_str("ANALYZE"),
1756 Self::Analyze(Some(t)) => write!(f, "ANALYZE {}", quote_ident(t)),
1757 Self::CompactColdSegments => f.write_str("COMPACT COLD SEGMENTS"),
1758 Self::Explain(e) => {
1759 if e.suggest {
1760 write!(f, "EXPLAIN (SUGGEST) {}", e.inner)
1761 } else if e.analyze {
1762 write!(f, "EXPLAIN ANALYZE {}", e.inner)
1763 } else {
1764 write!(f, "EXPLAIN {}", e.inner)
1765 }
1766 }
1767 Self::AlterIndex(a) => {
1768 write!(f, "ALTER INDEX {} ", quote_ident(&a.name))?;
1769 match a.target {
1770 AlterIndexTarget::Rebuild { encoding } => {
1771 f.write_str("REBUILD")?;
1772 if let Some(enc) = encoding {
1773 write!(f, " WITH (encoding = {enc})")?;
1774 }
1775 Ok(())
1776 }
1777 }
1778 }
1779 Self::AlterTable(a) => {
1780 write!(f, "ALTER TABLE {} ", quote_ident(&a.name))?;
1781 for (i, t) in a.targets.iter().enumerate() {
1782 if i > 0 {
1783 f.write_str(", ")?;
1784 }
1785 fmt_alter_target(f, t)?;
1786 }
1787 Ok(())
1788 }
1789 Self::CreatePublication(p) => {
1790 write!(f, "CREATE PUBLICATION {}", quote_ident(&p.name))?;
1791 match &p.scope {
1792 PublicationScope::AllTables => f.write_str(" FOR ALL TABLES"),
1793 PublicationScope::ForTables(ts) => {
1794 f.write_str(" FOR TABLE ")?;
1795 for (i, t) in ts.iter().enumerate() {
1796 if i > 0 {
1797 f.write_str(", ")?;
1798 }
1799 write!(f, "{}", quote_ident(t))?;
1800 }
1801 Ok(())
1802 }
1803 PublicationScope::AllTablesExcept(ts) => {
1804 f.write_str(" FOR ALL TABLES EXCEPT ")?;
1805 for (i, t) in ts.iter().enumerate() {
1806 if i > 0 {
1807 f.write_str(", ")?;
1808 }
1809 write!(f, "{}", quote_ident(t))?;
1810 }
1811 Ok(())
1812 }
1813 }
1814 }
1815 Self::CreateExtension(name) => {
1816 write!(f, "CREATE EXTENSION IF NOT EXISTS {}", quote_ident(name))
1817 }
1818 Self::DoBlock => f.write_str("DO $$ /* SPG no-op */ $$"),
1819 Self::DropPublication(name) => {
1820 write!(f, "DROP PUBLICATION {}", quote_ident(name))
1821 }
1822 Self::SetParameter { name, value } => {
1823 write!(f, "SET {name} = ")?;
1824 match value {
1825 SetValue::String(s) => write!(f, "'{}'", s.replace('\'', "''")),
1826 SetValue::Ident(s) | SetValue::Number(s) => f.write_str(s),
1827 SetValue::Default => f.write_str("DEFAULT"),
1828 }
1829 }
1830 Self::SetParameterList(pairs) => {
1831 f.write_str("SET ")?;
1832 for (i, (name, value)) in pairs.iter().enumerate() {
1833 if i > 0 {
1834 f.write_str(", ")?;
1835 }
1836 write!(f, "{name} = ")?;
1837 match value {
1838 SetValue::String(s) => write!(f, "'{}'", s.replace('\'', "''"))?,
1839 SetValue::Ident(s) | SetValue::Number(s) => f.write_str(s)?,
1840 SetValue::Default => f.write_str("DEFAULT")?,
1841 }
1842 }
1843 Ok(())
1844 }
1845 Self::ResetParameter(None) => f.write_str("RESET ALL"),
1846 Self::ResetParameter(Some(name)) => write!(f, "RESET {name}"),
1847 Self::CreateFunction(s) => s.fmt(f),
1848 Self::CreateTrigger(s) => s.fmt(f),
1849 Self::DropTrigger {
1850 name,
1851 table,
1852 if_exists,
1853 } => {
1854 f.write_str("DROP TRIGGER ")?;
1855 if *if_exists {
1856 f.write_str("IF EXISTS ")?;
1857 }
1858 write!(f, "{} ON {}", quote_ident(name), quote_ident(table))
1859 }
1860 Self::DropFunction { name, if_exists } => {
1861 f.write_str("DROP FUNCTION ")?;
1862 if *if_exists {
1863 f.write_str("IF EXISTS ")?;
1864 }
1865 write!(f, "{}", quote_ident(name))
1866 }
1867 }
1868 }
1869}
1870
1871impl fmt::Display for CreateFunctionStatement {
1872 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1873 f.write_str("CREATE ")?;
1874 if self.or_replace {
1875 f.write_str("OR REPLACE ")?;
1876 }
1877 write!(f, "FUNCTION {}(", quote_ident(&self.name))?;
1878 for (i, arg) in self.args.iter().enumerate() {
1879 if i > 0 {
1880 f.write_str(", ")?;
1881 }
1882 match arg.mode {
1883 FunctionArgMode::In => {}
1884 FunctionArgMode::Out => f.write_str("OUT ")?,
1885 FunctionArgMode::InOut => f.write_str("INOUT ")?,
1886 }
1887 if let Some(name) = &arg.name {
1888 write!(f, "{} ", quote_ident(name))?;
1889 }
1890 match &arg.ty {
1891 FunctionArgType::Typed(t) => write!(f, "{t}")?,
1892 FunctionArgType::Raw(s) => f.write_str(s)?,
1893 }
1894 }
1895 f.write_str(") RETURNS ")?;
1896 match &self.returns {
1897 FunctionReturn::Trigger => f.write_str("TRIGGER")?,
1898 FunctionReturn::Void => f.write_str("VOID")?,
1899 FunctionReturn::Type(t) => write!(f, "{t}")?,
1900 FunctionReturn::Other(s) => f.write_str(s)?,
1901 }
1902 write!(f, " LANGUAGE {} AS $$", self.language)?;
1903 match &self.body {
1904 FunctionBody::PlPgSql(b) => write!(f, "\n{b}\n")?,
1905 FunctionBody::Raw(s) => f.write_str(s)?,
1906 }
1907 f.write_str("$$")
1908 }
1909}
1910
1911impl fmt::Display for PlPgSqlBlock {
1912 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1913 if !self.declarations.is_empty() {
1914 f.write_str("DECLARE\n")?;
1915 for d in &self.declarations {
1916 write!(f, " {} ", quote_ident(&d.name))?;
1917 match &d.ty {
1918 FunctionArgType::Typed(t) => write!(f, "{t}")?,
1919 FunctionArgType::Raw(s) => f.write_str(s)?,
1920 }
1921 if let Some(e) = &d.default {
1922 write!(f, " := {e}")?;
1923 }
1924 f.write_str(";\n")?;
1925 }
1926 }
1927 f.write_str("BEGIN\n")?;
1928 for stmt in &self.statements {
1929 writeln!(f, " {stmt};")?;
1930 }
1931 f.write_str("END")
1932 }
1933}
1934
1935impl fmt::Display for PlPgSqlStmt {
1936 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1937 match self {
1938 Self::Assign { target, value } => write!(f, "{target} := {value}"),
1939 Self::Return(t) => match t {
1940 ReturnTarget::New => f.write_str("RETURN NEW"),
1941 ReturnTarget::Old => f.write_str("RETURN OLD"),
1942 ReturnTarget::Null => f.write_str("RETURN NULL"),
1943 ReturnTarget::Expr(e) => write!(f, "RETURN {e}"),
1944 },
1945 Self::If {
1946 branches,
1947 else_branch,
1948 } => {
1949 for (i, (cond, body)) in branches.iter().enumerate() {
1950 if i == 0 {
1951 write!(f, "IF {cond} THEN ")?;
1952 } else {
1953 write!(f, " ELSIF {cond} THEN ")?;
1954 }
1955 for (j, s) in body.iter().enumerate() {
1956 if j > 0 {
1957 f.write_str("; ")?;
1958 }
1959 write!(f, "{s}")?;
1960 }
1961 }
1962 if !else_branch.is_empty() {
1963 f.write_str(" ELSE ")?;
1964 for (j, s) in else_branch.iter().enumerate() {
1965 if j > 0 {
1966 f.write_str("; ")?;
1967 }
1968 write!(f, "{s}")?;
1969 }
1970 }
1971 f.write_str(" END IF")
1972 }
1973 Self::Raise {
1974 level,
1975 message,
1976 args,
1977 } => {
1978 let lvl = match level {
1979 RaiseLevel::Notice => "NOTICE",
1980 RaiseLevel::Warning => "WARNING",
1981 RaiseLevel::Info => "INFO",
1982 RaiseLevel::Log => "LOG",
1983 RaiseLevel::Debug => "DEBUG",
1984 RaiseLevel::Exception => "EXCEPTION",
1985 };
1986 write!(f, "RAISE {lvl} '{}'", message.replace('\'', "''"))?;
1987 for a in args {
1988 write!(f, ", {a}")?;
1989 }
1990 Ok(())
1991 }
1992 Self::EmbeddedSql(s) => write!(f, "{s}"),
1993 }
1994 }
1995}
1996
1997impl fmt::Display for AssignTarget {
1998 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1999 match self {
2000 Self::NewColumn(c) => write!(f, "NEW.{}", quote_ident(c)),
2001 Self::OldColumn(c) => write!(f, "OLD.{}", quote_ident(c)),
2002 Self::Local(n) => f.write_str(n),
2003 }
2004 }
2005}
2006
2007impl fmt::Display for CreateTriggerStatement {
2008 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2009 f.write_str("CREATE ")?;
2010 if self.or_replace {
2011 f.write_str("OR REPLACE ")?;
2012 }
2013 write!(f, "TRIGGER {} ", quote_ident(&self.name))?;
2014 match self.timing {
2015 TriggerTiming::Before => f.write_str("BEFORE")?,
2016 TriggerTiming::After => f.write_str("AFTER")?,
2017 TriggerTiming::InsteadOf => f.write_str("INSTEAD OF")?,
2018 }
2019 for (i, e) in self.events.iter().enumerate() {
2020 if i == 0 {
2021 f.write_str(" ")?;
2022 } else {
2023 f.write_str(" OR ")?;
2024 }
2025 match e {
2026 TriggerEvent::Insert => f.write_str("INSERT")?,
2027 TriggerEvent::Update => {
2028 f.write_str("UPDATE")?;
2029 if !self.update_columns.is_empty() {
2030 f.write_str(" OF ")?;
2031 for (j, col) in self.update_columns.iter().enumerate() {
2032 if j > 0 {
2033 f.write_str(", ")?;
2034 }
2035 f.write_str("e_ident(col))?;
2036 }
2037 }
2038 }
2039 TriggerEvent::Delete => f.write_str("DELETE")?,
2040 TriggerEvent::Truncate => f.write_str("TRUNCATE")?,
2041 }
2042 }
2043 write!(f, " ON {} FOR EACH ", quote_ident(&self.table))?;
2044 match self.for_each {
2045 TriggerForEach::Row => f.write_str("ROW")?,
2046 TriggerForEach::Statement => f.write_str("STATEMENT")?,
2047 }
2048 write!(f, " EXECUTE FUNCTION {}()", quote_ident(&self.function))
2049 }
2050}
2051
2052impl fmt::Display for CreateIndexStatement {
2053 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2054 if self.is_unique {
2055 f.write_str("CREATE UNIQUE INDEX ")?;
2056 } else {
2057 f.write_str("CREATE INDEX ")?;
2058 }
2059 if self.if_not_exists {
2060 f.write_str("IF NOT EXISTS ")?;
2061 }
2062 write!(
2063 f,
2064 "{} ON {} ",
2065 quote_ident(&self.name),
2066 quote_ident(&self.table)
2067 )?;
2068 match self.method {
2069 IndexMethod::Hnsw => f.write_str("USING hnsw ")?,
2070 IndexMethod::Brin => f.write_str("USING brin ")?,
2071 IndexMethod::Gin => f.write_str("USING gin ")?,
2072 IndexMethod::BTree => {}
2073 }
2074 if let Some(expr) = &self.expression {
2075 write!(f, "({})", expr)?;
2076 } else if self.extra_columns.is_empty() {
2077 // v7.15.0 — preserve operator class on round-trip
2078 // (`(col opclass)`) so WAL replay reconstructs the
2079 // engine-routing intent (e.g. `gin_trgm_ops` →
2080 // trigram-GIN build path).
2081 if let Some(op) = &self.opclass {
2082 write!(f, "({} {})", quote_ident(&self.column), op)?;
2083 } else {
2084 write!(f, "({})", quote_ident(&self.column))?;
2085 }
2086 } else {
2087 // v7.9.14 — multi-column key. Emit each column quoted
2088 // so the round-tripped form re-parses to identical AST.
2089 f.write_str("(")?;
2090 write!(f, "{}", quote_ident(&self.column))?;
2091 for c in &self.extra_columns {
2092 write!(f, ", {}", quote_ident(c))?;
2093 }
2094 f.write_str(")")?;
2095 }
2096 if !self.included_columns.is_empty() {
2097 f.write_str(" INCLUDE (")?;
2098 for (i, c) in self.included_columns.iter().enumerate() {
2099 if i > 0 {
2100 f.write_str(", ")?;
2101 }
2102 write!(f, "{}", quote_ident(c))?;
2103 }
2104 f.write_str(")")?;
2105 }
2106 if let Some(pred) = &self.partial_predicate {
2107 write!(f, " WHERE {}", pred)?;
2108 }
2109 Ok(())
2110 }
2111}
2112
2113impl fmt::Display for CreateTableStatement {
2114 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2115 f.write_str("CREATE TABLE ")?;
2116 if self.if_not_exists {
2117 f.write_str("IF NOT EXISTS ")?;
2118 }
2119 write!(f, "{} (", quote_ident(&self.name))?;
2120 for (i, col) in self.columns.iter().enumerate() {
2121 if i > 0 {
2122 f.write_str(", ")?;
2123 }
2124 write!(f, "{col}")?;
2125 }
2126 // v7.6.0 — render FK constraints in table-level form, after
2127 // the column list. WAL replay round-trips through Display, so
2128 // every FK must serialise here for replay to reconstruct the
2129 // schema bit-for-bit.
2130 for fk in &self.foreign_keys {
2131 f.write_str(", ")?;
2132 write!(f, "{fk}")?;
2133 }
2134 // v7.13.0 — render table-level constraints (PRIMARY KEY /
2135 // UNIQUE / CHECK) so WAL replay reconstructs them. Inline
2136 // column-level UNIQUE / CHECK get lifted to this list at
2137 // parse time, so emitting only here avoids double-counting.
2138 for tc in &self.table_constraints {
2139 f.write_str(", ")?;
2140 write!(f, "{tc}")?;
2141 }
2142 f.write_str(")")
2143 }
2144}
2145
2146fn fmt_alter_target(f: &mut fmt::Formatter<'_>, t: &AlterTableTarget) -> fmt::Result {
2147 match t {
2148 AlterTableTarget::SetHotTierBytes(n) => {
2149 write!(f, "SET hot_tier_bytes = {n}")
2150 }
2151 AlterTableTarget::AddForeignKey(fk) => write!(f, "ADD {fk}"),
2152 AlterTableTarget::DropForeignKey { name, if_exists } => {
2153 f.write_str("DROP CONSTRAINT ")?;
2154 if *if_exists {
2155 f.write_str("IF EXISTS ")?;
2156 }
2157 write!(f, "{}", quote_ident(name))
2158 }
2159 AlterTableTarget::AddColumn {
2160 column,
2161 if_not_exists,
2162 } => {
2163 f.write_str("ADD COLUMN ")?;
2164 if *if_not_exists {
2165 f.write_str("IF NOT EXISTS ")?;
2166 }
2167 write!(f, "{} {}", quote_ident(&column.name), column.ty)?;
2168 if !column.nullable {
2169 f.write_str(" NOT NULL")?;
2170 }
2171 if let Some(d) = &column.default {
2172 write!(f, " DEFAULT {d}")?;
2173 }
2174 if column.auto_increment {
2175 f.write_str(" AUTO_INCREMENT")?;
2176 }
2177 if column.is_primary_key {
2178 f.write_str(" PRIMARY KEY")?;
2179 }
2180 Ok(())
2181 }
2182 AlterTableTarget::AlterColumnType {
2183 column,
2184 new_type,
2185 using,
2186 } => {
2187 write!(f, "ALTER COLUMN {} TYPE {new_type}", quote_ident(column))?;
2188 if let Some(u) = using {
2189 write!(f, " USING {u}")?;
2190 }
2191 Ok(())
2192 }
2193 AlterTableTarget::DropColumn {
2194 column,
2195 if_exists,
2196 cascade,
2197 } => {
2198 f.write_str("DROP COLUMN ")?;
2199 if *if_exists {
2200 f.write_str("IF EXISTS ")?;
2201 }
2202 write!(f, "{}", quote_ident(column))?;
2203 if *cascade {
2204 f.write_str(" CASCADE")?;
2205 }
2206 Ok(())
2207 }
2208 AlterTableTarget::AddTableConstraint(tc) => {
2209 write!(f, "ADD {tc}")
2210 }
2211 AlterTableTarget::RenameColumn { old, new } => {
2212 write!(
2213 f,
2214 "RENAME COLUMN {} TO {}",
2215 quote_ident(old),
2216 quote_ident(new)
2217 )
2218 }
2219 AlterTableTarget::SetTriggerEnabled { which, enabled } => {
2220 f.write_str(if *enabled { "ENABLE TRIGGER " } else { "DISABLE TRIGGER " })?;
2221 match which {
2222 TriggerSelector::All => f.write_str("ALL"),
2223 TriggerSelector::Named(n) => f.write_str("e_ident(n)),
2224 }
2225 }
2226 }
2227}
2228
2229impl fmt::Display for TableConstraint {
2230 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2231 match self {
2232 Self::PrimaryKey { name, columns } => {
2233 if let Some(n) = name {
2234 write!(f, "CONSTRAINT {} ", quote_ident(n))?;
2235 }
2236 f.write_str("PRIMARY KEY (")?;
2237 for (i, c) in columns.iter().enumerate() {
2238 if i > 0 {
2239 f.write_str(", ")?;
2240 }
2241 f.write_str("e_ident(c))?;
2242 }
2243 f.write_str(")")
2244 }
2245 Self::Unique {
2246 name,
2247 columns,
2248 nulls_not_distinct,
2249 } => {
2250 if let Some(n) = name {
2251 write!(f, "CONSTRAINT {} ", quote_ident(n))?;
2252 }
2253 f.write_str("UNIQUE ")?;
2254 if *nulls_not_distinct {
2255 f.write_str("NULLS NOT DISTINCT ")?;
2256 }
2257 f.write_str("(")?;
2258 for (i, c) in columns.iter().enumerate() {
2259 if i > 0 {
2260 f.write_str(", ")?;
2261 }
2262 f.write_str("e_ident(c))?;
2263 }
2264 f.write_str(")")
2265 }
2266 Self::Check { name, expr } => {
2267 if let Some(n) = name {
2268 write!(f, "CONSTRAINT {} ", quote_ident(n))?;
2269 }
2270 write!(f, "CHECK ({expr})")
2271 }
2272 Self::Index { name, columns } => {
2273 f.write_str("KEY ")?;
2274 if let Some(n) = name {
2275 write!(f, "{} ", quote_ident(n))?;
2276 }
2277 f.write_str("(")?;
2278 for (i, c) in columns.iter().enumerate() {
2279 if i > 0 {
2280 f.write_str(", ")?;
2281 }
2282 f.write_str("e_ident(c))?;
2283 }
2284 f.write_str(")")
2285 }
2286 }
2287 }
2288}
2289
2290impl fmt::Display for ForeignKeyConstraint {
2291 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2292 if let Some(name) = &self.name {
2293 write!(f, "CONSTRAINT {} ", quote_ident(name))?;
2294 }
2295 f.write_str("FOREIGN KEY (")?;
2296 for (i, c) in self.columns.iter().enumerate() {
2297 if i > 0 {
2298 f.write_str(", ")?;
2299 }
2300 f.write_str("e_ident(c))?;
2301 }
2302 write!(f, ") REFERENCES {}", quote_ident(&self.parent_table))?;
2303 if !self.parent_columns.is_empty() {
2304 f.write_str(" (")?;
2305 for (i, c) in self.parent_columns.iter().enumerate() {
2306 if i > 0 {
2307 f.write_str(", ")?;
2308 }
2309 f.write_str("e_ident(c))?;
2310 }
2311 f.write_str(")")?;
2312 }
2313 // Only render non-default actions to keep Display output
2314 // close to user input. SPG's default is RESTRICT (matches
2315 // SQL spec).
2316 if self.on_delete != FkAction::Restrict {
2317 write!(f, " ON DELETE {}", self.on_delete)?;
2318 }
2319 if self.on_update != FkAction::Restrict {
2320 write!(f, " ON UPDATE {}", self.on_update)?;
2321 }
2322 Ok(())
2323 }
2324}
2325
2326impl fmt::Display for FkAction {
2327 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2328 match self {
2329 Self::Restrict => f.write_str("RESTRICT"),
2330 Self::Cascade => f.write_str("CASCADE"),
2331 Self::SetNull => f.write_str("SET NULL"),
2332 Self::SetDefault => f.write_str("SET DEFAULT"),
2333 Self::NoAction => f.write_str("NO ACTION"),
2334 }
2335 }
2336}
2337
2338impl fmt::Display for ColumnDef {
2339 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2340 write!(f, "{} {}", quote_ident(&self.name), self.ty)?;
2341 if let Some(d) = &self.default {
2342 write!(f, " DEFAULT {d}")?;
2343 }
2344 if self.auto_increment {
2345 f.write_str(" AUTO_INCREMENT")?;
2346 }
2347 if !self.nullable {
2348 f.write_str(" NOT NULL")?;
2349 }
2350 Ok(())
2351 }
2352}
2353
2354impl fmt::Display for InsertStatement {
2355 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2356 write!(f, "INSERT INTO {}", quote_ident(&self.table))?;
2357 if let Some(cols) = &self.columns {
2358 f.write_str(" (")?;
2359 for (i, c) in cols.iter().enumerate() {
2360 if i > 0 {
2361 f.write_str(", ")?;
2362 }
2363 f.write_str("e_ident(c))?;
2364 }
2365 f.write_str(")")?;
2366 }
2367 // v7.13.0 — INSERT…SELECT renders as `... SELECT …`,
2368 // skipping the VALUES list (mailrs round-5 G4).
2369 if let Some(sel) = &self.select_source {
2370 write!(f, " {sel}")?;
2371 } else {
2372 f.write_str(" VALUES ")?;
2373 for (ri, row) in self.rows.iter().enumerate() {
2374 if ri > 0 {
2375 f.write_str(", ")?;
2376 }
2377 f.write_str("(")?;
2378 for (i, v) in row.iter().enumerate() {
2379 if i > 0 {
2380 f.write_str(", ")?;
2381 }
2382 write!(f, "{v}")?;
2383 }
2384 f.write_str(")")?;
2385 }
2386 }
2387 Ok(())
2388 }
2389}
2390
2391impl fmt::Display for UpdateStatement {
2392 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2393 write!(f, "UPDATE {} SET ", quote_ident(&self.table))?;
2394 for (i, (col, expr)) in self.assignments.iter().enumerate() {
2395 if i > 0 {
2396 f.write_str(", ")?;
2397 }
2398 write!(f, "{} = {expr}", quote_ident(col))?;
2399 }
2400 if let Some(w) = &self.where_ {
2401 write!(f, " WHERE {w}")?;
2402 }
2403 Ok(())
2404 }
2405}
2406
2407impl fmt::Display for DeleteStatement {
2408 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2409 write!(f, "DELETE FROM {}", quote_ident(&self.table))?;
2410 if let Some(w) = &self.where_ {
2411 write!(f, " WHERE {w}")?;
2412 }
2413 Ok(())
2414 }
2415}
2416
2417impl fmt::Display for SelectStatement {
2418 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2419 write_bare_select(self, f)?;
2420 for (kind, peer) in &self.unions {
2421 f.write_str(match kind {
2422 UnionKind::Distinct => " UNION ",
2423 UnionKind::All => " UNION ALL ",
2424 })?;
2425 write_bare_select(peer, f)?;
2426 }
2427 if !self.order_by.is_empty() {
2428 f.write_str(" ORDER BY ")?;
2429 for (i, o) in self.order_by.iter().enumerate() {
2430 if i > 0 {
2431 f.write_str(", ")?;
2432 }
2433 write!(f, "{}", o.expr)?;
2434 if o.desc {
2435 f.write_str(" DESC")?;
2436 }
2437 }
2438 }
2439 if let Some(n) = &self.limit {
2440 write!(f, " LIMIT {n}")?;
2441 }
2442 if let Some(o) = &self.offset {
2443 write!(f, " OFFSET {o}")?;
2444 }
2445 Ok(())
2446 }
2447}
2448
2449fn write_bare_select(s: &SelectStatement, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2450 f.write_str("SELECT ")?;
2451 if s.distinct {
2452 f.write_str("DISTINCT ")?;
2453 }
2454 write_bare_select_body(s, f)
2455}
2456
2457fn write_bare_select_body(s: &SelectStatement, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2458 for (i, item) in s.items.iter().enumerate() {
2459 if i > 0 {
2460 f.write_str(", ")?;
2461 }
2462 write!(f, "{item}")?;
2463 }
2464 if let Some(t) = &s.from {
2465 write!(f, " FROM {t}")?;
2466 }
2467 if let Some(e) = &s.where_ {
2468 write!(f, " WHERE {e}")?;
2469 }
2470 if let Some(gs) = &s.group_by {
2471 f.write_str(" GROUP BY ")?;
2472 for (i, g) in gs.iter().enumerate() {
2473 if i > 0 {
2474 f.write_str(", ")?;
2475 }
2476 write!(f, "{g}")?;
2477 }
2478 }
2479 if let Some(h) = &s.having {
2480 write!(f, " HAVING {h}")?;
2481 }
2482 Ok(())
2483}
2484
2485impl fmt::Display for SelectItem {
2486 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2487 match self {
2488 Self::Wildcard => f.write_str("*"),
2489 Self::Expr { expr, alias } => {
2490 write!(f, "{expr}")?;
2491 if let Some(a) = alias {
2492 write!(f, " AS {}", quote_ident(a))?;
2493 }
2494 Ok(())
2495 }
2496 }
2497 }
2498}
2499
2500impl fmt::Display for FromClause {
2501 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2502 write!(f, "{}", self.primary)?;
2503 for j in &self.joins {
2504 match j.kind {
2505 JoinKind::Inner => write!(f, " INNER JOIN {}", j.table)?,
2506 JoinKind::Left => write!(f, " LEFT JOIN {}", j.table)?,
2507 JoinKind::Cross => write!(f, " CROSS JOIN {}", j.table)?,
2508 }
2509 if let Some(on) = &j.on {
2510 write!(f, " ON {on}")?;
2511 }
2512 }
2513 Ok(())
2514 }
2515}
2516
2517impl fmt::Display for TableRef {
2518 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2519 write!(f, "{}", quote_ident(&self.name))?;
2520 if let Some(a) = &self.alias {
2521 write!(f, " AS {}", quote_ident(a))?;
2522 }
2523 Ok(())
2524 }
2525}
2526
2527impl fmt::Display for ColumnName {
2528 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2529 if let Some(q) = &self.qualifier {
2530 write!(f, "{}.{}", quote_ident(q), quote_ident(&self.name))
2531 } else {
2532 write!(f, "{}", quote_ident(&self.name))
2533 }
2534 }
2535}
2536
2537impl fmt::Display for Expr {
2538 #[allow(clippy::too_many_lines)]
2539 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2540 match self {
2541 Self::Literal(l) => write!(f, "{l}"),
2542 Self::Column(c) => write!(f, "{c}"),
2543 Self::Placeholder(n) => write!(f, "${n}"),
2544 Self::Binary { lhs, op, rhs } => write!(f, "({lhs} {op} {rhs})"),
2545 Self::Unary { op, expr } => match op {
2546 UnOp::Not => write!(f, "(NOT {expr})"),
2547 UnOp::Neg => write!(f, "(-{expr})"),
2548 },
2549 Self::Cast { expr, target } => write!(f, "({expr}::{target})"),
2550 Self::IsNull { expr, negated } => {
2551 if *negated {
2552 write!(f, "({expr} IS NOT NULL)")
2553 } else {
2554 write!(f, "({expr} IS NULL)")
2555 }
2556 }
2557 Self::FunctionCall { name, args } => {
2558 write!(f, "{name}(")?;
2559 for (i, a) in args.iter().enumerate() {
2560 if i > 0 {
2561 f.write_str(", ")?;
2562 }
2563 write!(f, "{a}")?;
2564 }
2565 f.write_str(")")
2566 }
2567 Self::Like {
2568 expr,
2569 pattern,
2570 negated,
2571 } => {
2572 if *negated {
2573 write!(f, "({expr} NOT LIKE {pattern})")
2574 } else {
2575 write!(f, "({expr} LIKE {pattern})")
2576 }
2577 }
2578 Self::Extract { field, source } => write!(f, "EXTRACT({field} FROM {source})"),
2579 Self::WindowFunction {
2580 name,
2581 args,
2582 partition_by,
2583 order_by,
2584 frame,
2585 null_treatment: _,
2586 } => {
2587 write!(f, "{name}(")?;
2588 for (i, a) in args.iter().enumerate() {
2589 if i > 0 {
2590 f.write_str(", ")?;
2591 }
2592 write!(f, "{a}")?;
2593 }
2594 f.write_str(") OVER (")?;
2595 if !partition_by.is_empty() {
2596 f.write_str("PARTITION BY ")?;
2597 for (i, p) in partition_by.iter().enumerate() {
2598 if i > 0 {
2599 f.write_str(", ")?;
2600 }
2601 write!(f, "{p}")?;
2602 }
2603 }
2604 if !order_by.is_empty() {
2605 if !partition_by.is_empty() {
2606 f.write_str(" ")?;
2607 }
2608 f.write_str("ORDER BY ")?;
2609 for (i, (e, desc)) in order_by.iter().enumerate() {
2610 if i > 0 {
2611 f.write_str(", ")?;
2612 }
2613 write!(f, "{e}")?;
2614 if *desc {
2615 f.write_str(" DESC")?;
2616 }
2617 }
2618 }
2619 if let Some(fr) = frame {
2620 if !partition_by.is_empty() || !order_by.is_empty() {
2621 f.write_str(" ")?;
2622 }
2623 let k = match fr.kind {
2624 FrameKind::Rows => "ROWS",
2625 FrameKind::Range => "RANGE",
2626 };
2627 if let Some(end) = &fr.end {
2628 write!(f, "{k} BETWEEN {} AND {}", fr.start, end)?;
2629 } else {
2630 write!(f, "{k} {}", fr.start)?;
2631 }
2632 }
2633 f.write_str(")")
2634 }
2635 Self::ScalarSubquery(s) => write!(f, "({s})"),
2636 Self::Exists { subquery, negated } => {
2637 if *negated {
2638 write!(f, "NOT EXISTS ({subquery})")
2639 } else {
2640 write!(f, "EXISTS ({subquery})")
2641 }
2642 }
2643 Self::InSubquery {
2644 expr,
2645 subquery,
2646 negated,
2647 } => {
2648 if *negated {
2649 write!(f, "({expr} NOT IN ({subquery}))")
2650 } else {
2651 write!(f, "({expr} IN ({subquery}))")
2652 }
2653 }
2654 Self::Array(items) => {
2655 f.write_str("ARRAY[")?;
2656 for (i, e) in items.iter().enumerate() {
2657 if i > 0 {
2658 f.write_str(", ")?;
2659 }
2660 write!(f, "{e}")?;
2661 }
2662 f.write_str("]")
2663 }
2664 Self::ArraySubscript { target, index } => write!(f, "({target}[{index}])"),
2665 Self::AnyAll {
2666 expr,
2667 op,
2668 array,
2669 is_any,
2670 } => {
2671 let kw = if *is_any { "ANY" } else { "ALL" };
2672 write!(f, "({expr} {op} {kw}({array}))")
2673 }
2674 Self::Case {
2675 operand,
2676 branches,
2677 else_branch,
2678 } => {
2679 f.write_str("CASE")?;
2680 if let Some(op) = operand {
2681 write!(f, " {op}")?;
2682 }
2683 for (w, t) in branches {
2684 write!(f, " WHEN {w} THEN {t}")?;
2685 }
2686 if let Some(e) = else_branch {
2687 write!(f, " ELSE {e}")?;
2688 }
2689 f.write_str(" END")
2690 }
2691 }
2692 }
2693}
2694
2695impl fmt::Display for Literal {
2696 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2697 match self {
2698 Self::Integer(n) => write!(f, "{n}"),
2699 Self::Float(x) => {
2700 let s = format!("{x}");
2701 // Default Display for an integral f64 (e.g. 1.0) emits "1",
2702 // which would round-trip back to Integer. Force a dot.
2703 if s.contains('.') || s.contains('e') || s.contains('E') {
2704 f.write_str(&s)
2705 } else {
2706 write!(f, "{s}.0")
2707 }
2708 }
2709 Self::String(s) => {
2710 f.write_str("'")?;
2711 for c in s.chars() {
2712 if c == '\'' {
2713 f.write_str("''")?;
2714 } else {
2715 write!(f, "{c}")?;
2716 }
2717 }
2718 f.write_str("'")
2719 }
2720 Self::Bool(b) => f.write_str(if *b { "TRUE" } else { "FALSE" }),
2721 Self::Null => f.write_str("NULL"),
2722 Self::Vector(v) => {
2723 f.write_str("[")?;
2724 for (i, x) in v.iter().enumerate() {
2725 if i > 0 {
2726 f.write_str(", ")?;
2727 }
2728 let s = format!("{x}");
2729 // Mirror Float Display: force a dot so re-parse stays
2730 // numerically literal.
2731 if s.contains('.') || s.contains('e') || s.contains('E') {
2732 f.write_str(&s)?;
2733 } else {
2734 write!(f, "{s}.0")?;
2735 }
2736 }
2737 f.write_str("]")
2738 }
2739 Self::Interval { text, .. } => {
2740 f.write_str("INTERVAL '")?;
2741 for c in text.chars() {
2742 if c == '\'' {
2743 f.write_str("''")?;
2744 } else {
2745 write!(f, "{c}")?;
2746 }
2747 }
2748 f.write_str("'")
2749 }
2750 }
2751 }
2752}
2753
2754impl fmt::Display for BinOp {
2755 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2756 f.write_str(match self {
2757 Self::Or => "OR",
2758 Self::And => "AND",
2759 Self::Eq => "=",
2760 Self::NotEq => "<>",
2761 Self::IsDistinctFrom => "IS DISTINCT FROM",
2762 Self::IsNotDistinctFrom => "IS NOT DISTINCT FROM",
2763 Self::Lt => "<",
2764 Self::LtEq => "<=",
2765 Self::Gt => ">",
2766 Self::GtEq => ">=",
2767 Self::Add => "+",
2768 Self::Sub => "-",
2769 Self::Mul => "*",
2770 Self::Div => "/",
2771 Self::L2Distance => "<->",
2772 Self::InnerProduct => "<#>",
2773 Self::CosineDistance => "<=>",
2774 Self::Concat => "||",
2775 Self::JsonGet => "->",
2776 Self::JsonGetText => "->>",
2777 Self::JsonGetPath => "#>",
2778 Self::JsonGetPathText => "#>>",
2779 Self::JsonContains => "@>",
2780 Self::TsMatch => "@@",
2781 })
2782 }
2783}
2784
2785/// Quote `s` as a PG double-quoted identifier when required (keyword,
2786/// non-folded case, leading digit, embedded non-`[A-Za-z0-9_]`, empty).
2787/// Otherwise return it as-is. Returns an owned `String` to keep the call site
2788/// uniform.
2789fn quote_ident(s: &str) -> String {
2790 let needs_quote = match s.chars().next() {
2791 None => true,
2792 Some(c) if !c.is_ascii_alphabetic() && c != '_' => true,
2793 _ => {
2794 s.chars().any(|c| !(c.is_ascii_alphanumeric() || c == '_'))
2795 || s.chars().any(|c| c.is_ascii_uppercase())
2796 || is_keyword(s)
2797 }
2798 };
2799 if !needs_quote {
2800 return s.to_string();
2801 }
2802 let mut out = String::with_capacity(s.len() + 2);
2803 out.push('"');
2804 for c in s.chars() {
2805 if c == '"' {
2806 out.push_str("\"\"");
2807 } else {
2808 out.push(c);
2809 }
2810 }
2811 out.push('"');
2812 out
2813}
2814
2815fn is_keyword(s: &str) -> bool {
2816 matches!(
2817 &*s.to_ascii_lowercase(),
2818 "select"
2819 | "from"
2820 | "where"
2821 | "as"
2822 | "null"
2823 | "true"
2824 | "false"
2825 | "and"
2826 | "or"
2827 | "not"
2828 | "create"
2829 | "table"
2830 | "insert"
2831 | "into"
2832 | "values"
2833 | "index"
2834 | "on"
2835 | "begin"
2836 | "commit"
2837 | "rollback"
2838 | "is"
2839 | "between"
2840 | "in"
2841 | "like"
2842 | "group"
2843 | "distinct"
2844 | "union"
2845 | "all"
2846 | "join"
2847 | "inner"
2848 | "left"
2849 | "cross"
2850 | "outer"
2851 | "default"
2852 | "savepoint"
2853 | "release"
2854 | "to"
2855 | "having"
2856 | "show"
2857 | "extract"
2858 | "offset"
2859 | "asc"
2860 | "desc"
2861 | "interval"
2862 )
2863}
2864
2865#[cfg(test)]
2866mod tests {
2867 use super::*;
2868 use alloc::vec;
2869
2870 #[test]
2871 fn integer_literal_renders_without_dot() {
2872 assert_eq!(Literal::Integer(42).to_string(), "42");
2873 }
2874
2875 #[test]
2876 fn integral_float_keeps_dot() {
2877 assert_eq!(Literal::Float(1.0).to_string(), "1.0");
2878 assert_eq!(Literal::Float(1.5).to_string(), "1.5");
2879 assert_eq!(Literal::Float(2.5e-3).to_string(), "0.0025");
2880 }
2881
2882 #[test]
2883 fn string_literal_doubles_quote() {
2884 assert_eq!(Literal::String("it's".into()).to_string(), "'it''s'");
2885 }
2886
2887 #[test]
2888 fn bool_and_null_render_uppercase() {
2889 assert_eq!(Literal::Bool(true).to_string(), "TRUE");
2890 assert_eq!(Literal::Bool(false).to_string(), "FALSE");
2891 assert_eq!(Literal::Null.to_string(), "NULL");
2892 }
2893
2894 #[test]
2895 fn binary_op_always_parenthesised() {
2896 let e = Expr::Binary {
2897 lhs: Box::new(Expr::Literal(Literal::Integer(1))),
2898 op: BinOp::Add,
2899 rhs: Box::new(Expr::Literal(Literal::Integer(2))),
2900 };
2901 assert_eq!(e.to_string(), "(1 + 2)");
2902 }
2903
2904 #[test]
2905 fn select_star_from_table() {
2906 let s = SelectStatement {
2907 items: vec![SelectItem::Wildcard],
2908 from: Some(FromClause {
2909 primary: TableRef {
2910 name: "users".into(),
2911 alias: None,
2912 as_of_segment: None,
2913 unnest_expr: None,
2914 unnest_column_aliases: Vec::new(),
2915 },
2916 joins: vec![],
2917 }),
2918 where_: None,
2919 group_by: None,
2920 group_by_all: false,
2921 having: None,
2922 unions: vec![],
2923 order_by: Vec::new(),
2924 limit: None,
2925 offset: None,
2926 distinct: false,
2927 ctes: vec![],
2928 };
2929 assert_eq!(s.to_string(), "SELECT * FROM users");
2930 }
2931
2932 #[test]
2933 fn quote_ident_for_uppercase_and_keyword() {
2934 assert_eq!(quote_ident("foo"), "foo");
2935 assert_eq!(quote_ident("Foo"), "\"Foo\"");
2936 assert_eq!(quote_ident("select"), "\"select\"");
2937 assert_eq!(quote_ident(""), "\"\"");
2938 assert_eq!(quote_ident("a\"b"), "\"a\"\"b\"");
2939 }
2940}