Skip to main content

spg_engine/
execute.rs

1//! Statement execution + prepared-statement dispatch, split out of
2//! `lib.rs` (lib.rs split 17). The public `execute` / `execute_in` /
3//! `execute_with_cancel` entry points, the `prepare` / `prepare_cached`
4//! / `describe_prepared` / `execute_prepared` prepared-statement path,
5//! and the internal pipeline (`execute_inner_with_cancel` →
6//! `execute_stmt_with_cancel`) that pre-resolves clock / sequence /
7//! placeholder rewrites and routes each parsed Statement to its domain
8//! handler (DDL / DML / SELECT / transaction / SHOW / …). Whole
9//! `impl Engine` methods reached via the Engine type, so the public
10//! surface is unchanged; `execute_stmt_with_cancel` is pub(crate) for
11//! the plpgsql + trigger re-entry paths.
12
13use alloc::string::String;
14use alloc::vec::Vec;
15
16use spg_sql::ast::Statement;
17use spg_sql::parser::{self, ParseError};
18use spg_storage::{ColumnSchema, Value};
19
20use crate::describe;
21use crate::{
22    CancelToken, Engine, EngineError, IMPLICIT_TX, QueryResult, TxId, expand_group_by_all,
23    plan_cache, reorder, resolve_order_by_position, rewrite_clock_calls, substitute_placeholders,
24};
25
26impl Engine {
27    pub fn execute(&mut self, sql: &str) -> Result<QueryResult, EngineError> {
28        self.execute_in_with_cancel(sql, IMPLICIT_TX, CancelToken::none())
29    }
30
31    /// v4.5 — write path with cooperative cancellation. Same dispatch
32    /// as `execute_in_with_cancel(sql, IMPLICIT_TX, cancel)`. Kept as
33    /// a separate entry point for backward-compat with the v4.5
34    /// public API.
35    pub fn execute_with_cancel(
36        &mut self,
37        sql: &str,
38        cancel: CancelToken<'_>,
39    ) -> Result<QueryResult, EngineError> {
40        self.execute_in_with_cancel(sql, IMPLICIT_TX, cancel)
41    }
42
43    /// v4.41.1 multi-slot write entry. Routes `sql` through the TX
44    /// slot identified by `tx_id` so spg-server dispatch can scope
45    /// each implicit-wrap BEGIN..stmt..COMMIT to its own slot in
46    /// `tx_catalogs`. `IMPLICIT_TX` is the legacy single-slot path
47    /// every other caller (engine self-tests, replay, spg-embedded)
48    /// implicitly takes via `execute()` / `execute_with_cancel()`.
49    pub fn execute_in(&mut self, sql: &str, tx_id: TxId) -> Result<QueryResult, EngineError> {
50        self.execute_in_with_cancel(sql, tx_id, CancelToken::none())
51    }
52
53    /// v4.41.1 write path with cooperative cancellation + explicit TX
54    /// scope. Sets `self.current_tx` for the duration of the call so
55    /// every `exec_*` helper transparently sees its TX's shadow
56    /// catalog and savepoint stack; restores on exit so the field is
57    /// only valid mid-call (no leakage across calls).
58    pub fn execute_in_with_cancel(
59        &mut self,
60        sql: &str,
61        tx_id: TxId,
62        cancel: CancelToken<'_>,
63    ) -> Result<QueryResult, EngineError> {
64        let saved = self.current_tx;
65        self.current_tx = Some(tx_id);
66        // v7.34 (crash-recovery P0 #2) — row-level redo capture. Arm the
67        // active catalog before dispatch; on success drain the physical
68        // changes into `last_redo` for the embedding layer's WAL, on
69        // failure discard them (a failed statement leaves no redo; the
70        // drain clears the tables' capture buffers either way).
71        if self.redo_capture {
72            self.active_catalog_mut().enable_redo_all();
73        }
74        let result = self.execute_inner_with_cancel(sql, cancel);
75        if self.redo_capture {
76            let drained = self.active_catalog_mut().drain_redo();
77            if result.is_ok() {
78                self.last_redo = drained;
79            }
80        }
81        self.current_tx = saved;
82        result
83    }
84
85    /// v6.1.1 — parse and pre-process a SQL string ONCE so the
86    /// resulting [`Statement`] can be cached and re-executed via
87    /// [`Engine::execute_prepared`]. Returns the same `Statement`
88    /// the simple-query path would synthesise internally (clock
89    /// rewrites + ORDER BY position-ref resolution applied at
90    /// prepare time, since both are session-independent). The
91    /// `$N` placeholders in the SQL stay as `Expr::Placeholder(n)`
92    /// nodes; they're resolved to concrete values per-call by
93    /// `execute_prepared`'s substitution walk.
94    ///
95    /// Pgwire's `Parse` (P) message lands here.
96    pub fn prepare(&self, sql: &str) -> Result<Statement, ParseError> {
97        let mut stmt = parser::parse_statement_with(sql, self.backslash_escapes)?;
98        let now_micros = self.clock.map(|f| f());
99        rewrite_clock_calls(&mut stmt, now_micros);
100        if let Statement::Select(s) = &mut stmt {
101            // v6.4.1 — expand `GROUP BY ALL` to every non-aggregate
102            // SELECT-list item BEFORE position / alias resolution so
103            // downstream passes see the explicit list.
104            expand_group_by_all(s);
105            resolve_order_by_position(s);
106            // v6.2.3 — cost-based JOIN reorder. No-op for
107            // single-table FROMs or any non-INNER join shape.
108            reorder::reorder_joins(s, &self.catalog, &self.statistics);
109        }
110        Ok(stmt)
111    }
112
113    /// v6.3.0 — cached prepare. Returns a cloned `Statement` from
114    /// the plan cache on hit, runs the full `prepare()` path on miss
115    /// and inserts the resulting plan before returning. Skipping the
116    /// parse + JOIN-reorder pipeline on hit is the dominant win for
117    /// JDBC / sqlx / pgx clients that reuse the same SQL string.
118    ///
119    /// Returns a cloned `Statement` (not a borrow) because the
120    /// pgwire layer owns its `PreparedStmt` map per-session and the
121    /// engine-level cache must stay available for other sessions.
122    /// Clone cost on a 5-table JOIN AST is well under the parse cost
123    /// it replaces.
124    pub fn prepare_cached(&mut self, sql: &str) -> Result<Statement, ParseError> {
125        // v6.3.1 — version-aware lookup. If the cached plan was
126        // prepared before the most recent ANALYZE, evict and replan.
127        let current_version = self.statistics.version();
128        if let Some(plan) = self.plan_cache.get(sql) {
129            if plan.statistics_version == current_version {
130                return Ok(plan.stmt.clone());
131            }
132            // Stale entry — fall through to evict + re-prepare.
133        }
134        self.plan_cache.evict(sql);
135        let stmt = self.prepare(sql)?;
136        let source_tables = plan_cache::collect_source_tables(&stmt);
137        let plan = plan_cache::PreparedPlan {
138            stmt: stmt.clone(),
139            statistics_version: current_version,
140            source_tables,
141            describe_columns: alloc::vec::Vec::new(),
142        };
143        self.plan_cache.insert(String::from(sql), plan);
144        Ok(stmt)
145    }
146
147    /// v6.3.0 — read-only accessor for tests and v6.3.1 invalidation.
148    pub fn plan_cache(&self) -> &plan_cache::PlanCache {
149        &self.plan_cache
150    }
151
152    /// v6.3.0 — mutable accessor for v6.3.1 invalidation hooks.
153    pub fn plan_cache_mut(&mut self) -> &mut plan_cache::PlanCache {
154        &mut self.plan_cache
155    }
156
157    /// v6.3.3 — Describe a prepared `Statement` without executing.
158    /// Returns `(parameter_oids, output_columns)`. Empty
159    /// `output_columns` means the statement has no row-producing
160    /// shape we could resolve here (JOIN, subquery, non-SELECT, …)
161    /// — pgwire layer maps that to a `NoData` reply.
162    pub fn describe_prepared(&self, stmt: &Statement) -> (Vec<u32>, Vec<ColumnSchema>) {
163        describe::describe_prepared(stmt, self.active_catalog())
164    }
165
166    /// v6.1.1 — execute a [`Statement`] previously returned by
167    /// [`Engine::prepare`], substituting `Expr::Placeholder(n)`
168    /// nodes for the corresponding [`Value`] in `params` (1-based
169    /// per PG: `$1` → `params[0]`). Bind-time string parameters
170    /// are decoded into typed `Value`s by the pgwire layer before
171    /// this call so the resulting AST hits the same execution
172    /// path as a simple query — no SQL re-parse.
173    ///
174    /// Pgwire's `Execute` (E) message after a `Bind` (B) lands here.
175    pub fn execute_prepared(
176        &mut self,
177        stmt: Statement,
178        params: &[Value],
179    ) -> Result<QueryResult, EngineError> {
180        self.execute_prepared_with_cancel(stmt, params, CancelToken::none())
181    }
182
183    /// v7.17.0 Phase 2.3 — prepared-statement entry that honors a
184    /// caller-supplied `CancelToken`. Mirrors `execute_prepared`'s
185    /// `current_tx` save/restore so the extended-query path stays
186    /// transactionally consistent with the simple-query path.
187    pub fn execute_prepared_with_cancel(
188        &mut self,
189        mut stmt: Statement,
190        params: &[Value],
191        cancel: CancelToken<'_>,
192    ) -> Result<QueryResult, EngineError> {
193        substitute_placeholders(&mut stmt, params)?;
194        // v7.16.0 — set `current_tx` for the duration of the
195        // dispatch so the `exec_*` helpers see the right TX
196        // slot (matches what `execute_in_with_cancel` does for
197        // simple-query). Pre-v7.16 the simple-query path
198        // worked because every public entry point routed
199        // through `execute_in_with_cancel`; the prepared path
200        // skipped the wrap and so its INSERTs/UPDATEs landed
201        // in the no-tx default slot, silently invisible to a
202        // BEGIN/COMMIT-bracketed flow. Caught by spg-sqlx's
203        // first transaction-visibility test.
204        let saved = self.current_tx;
205        self.current_tx = Some(IMPLICIT_TX);
206        let result = self.execute_stmt_with_cancel(stmt, cancel);
207        self.current_tx = saved;
208        result
209    }
210
211    fn execute_inner_with_cancel(
212        &mut self,
213        sql: &str,
214        cancel: CancelToken<'_>,
215    ) -> Result<QueryResult, EngineError> {
216        cancel.check()?;
217        let stmt = self.prepare(sql)?;
218        // v6.5.1 — wrap the executor with a wall-clock window so we
219        // can record into spg_stat_query. Skip when the engine has
220        // no clock attached (no_std embedded callers).
221        let start_us = self.clock.map(|f| f());
222        let result = self.execute_stmt_with_cancel(stmt, cancel);
223        if let (Some(t0), Ok(_)) = (start_us, &result) {
224            let now = self.clock.map_or(t0, |f| f());
225            let elapsed = now.saturating_sub(t0).max(0) as u64;
226            self.query_stats.record(sql, elapsed, now as u64);
227            // v6.5.6 — slow-query log: fire callback when elapsed
228            // exceeds the configured floor.
229            if let (Some(threshold), Some(logger)) =
230                (self.slow_query_threshold_us, self.slow_query_logger)
231                && elapsed >= threshold
232            {
233                logger(sql, elapsed);
234            }
235        }
236        result
237    }
238
239    pub(crate) fn execute_stmt_with_cancel(
240        &mut self,
241        stmt: Statement,
242        cancel: CancelToken<'_>,
243    ) -> Result<QueryResult, EngineError> {
244        cancel.check()?;
245        // v7.17.0 Phase 1.1 — pre-resolve nextval / currval /
246        // setval calls in the statement tree. Walks SELECT
247        // projection, INSERT VALUES, UPDATE SET, DELETE WHERE,
248        // and DEFAULT exprs; replaces sequence FunctionCall
249        // nodes with concrete Literal values minted against the
250        // catalog. This is the only place that mutates sequence
251        // state from a SELECT-shaped path (exec_select_cancel is
252        // `&self` and can't reach the catalog mutably).
253        //
254        // Fast-path: when no sequences exist anywhere in the
255        // catalog (the typical hot-path INSERT load), skip the
256        // walker entirely. Single map-emptiness check on the
257        // catalog beats walking every expression on every call.
258        let mut stmt = stmt;
259        // v7.17 dump-compat — the fast-path check
260        // `sequences().is_empty()` skips pre-resolve when no
261        // sequence exists in the *currently active* catalog
262        // snapshot. The committed catalog or the implicit-TX
263        // catalog may legitimately disagree on this between
264        // CREATE SEQUENCE and a later setval(): always run the
265        // resolver — the walk is O(expr-count) and dwarfed by
266        // the parse cost we just paid.
267        self.pre_resolve_sequence_calls_in_statement(&mut stmt)?;
268        let result = match stmt {
269            Statement::CreateTable(s) => self.exec_create_table(s),
270            // v7.9.15 — CREATE EXTENSION is a no-op on SPG. Returns
271            // CommandOk with affected=0; modified_catalog=false so
272            // the WAL doesn't grow a useless entry. mailrs F3.
273            Statement::CreateExtension(_) => Ok(QueryResult::CommandOk {
274                affected: 0,
275                modified_catalog: false,
276            }),
277            // v7.16.2 — DO $$ ... $$ block. mailrs round-10 A.2
278            // — the pre-v7.9.27 no-op SILENTLY swallowed every
279            // mailrs migrate-038/-040/-042 idempotent rename
280            // (the IF EXISTS … THEN ALTER … END block never
281            // ran). v7.16.2 dispatches to exec_do_block which
282            // runs the PlPgSqlBlock at top level via the same
283            // execute_stmts machinery the trigger executor
284            // uses (NEW=None, OLD=None — DO blocks have no
285            // row context).
286            Statement::DoBlock(body) => self.exec_do_block(body),
287            // v7.14.0 — empty-statement no-op for pg_dump /
288            // mysqldump preamble lines that collapse to nothing
289            // after comment-stripping.
290            Statement::Empty => Ok(QueryResult::CommandOk {
291                affected: 0,
292                modified_catalog: false,
293            }),
294            Statement::DropTable { names, if_exists } => self.exec_drop_table(names, if_exists),
295            Statement::DropIndex { name, if_exists } => self.exec_drop_index(name, if_exists),
296            Statement::CreateIndex(s) => self.exec_create_index(s),
297            Statement::Insert(s) => self.exec_insert(s),
298            Statement::Update(mut s) => {
299                // Materialise uncorrelated subqueries in SET / WHERE
300                // before the row walk — the SELECT path has done this
301                // since v4.10; UPDATE gained it for mailrs's
302                // `UPDATE … WHERE id IN (SELECT … FOR UPDATE SKIP
303                // LOCKED)` claim pattern (embed round-12).
304                for (_, e) in &mut s.assignments {
305                    self.resolve_expr_subqueries(e, cancel)?;
306                }
307                if let Some(w) = &mut s.where_ {
308                    self.resolve_expr_subqueries(w, cancel)?;
309                }
310                self.exec_update_cancel(&s, cancel)
311            }
312            Statement::Delete(mut s) => {
313                if let Some(w) = &mut s.where_ {
314                    self.resolve_expr_subqueries(w, cancel)?;
315                }
316                self.exec_delete_cancel(&s, cancel)
317            }
318            Statement::Merge(s) => self.exec_merge_cancel(&s, cancel),
319            Statement::Select(s) => self.exec_select_cancel(&s, cancel),
320            Statement::Begin => self.exec_begin(),
321            Statement::Commit => self.exec_commit(),
322            Statement::Rollback => self.exec_rollback(),
323            Statement::Savepoint(name) => self.exec_savepoint(name),
324            Statement::RollbackToSavepoint(name) => self.exec_rollback_to_savepoint(&name),
325            Statement::ReleaseSavepoint(name) => self.exec_release_savepoint(&name),
326            Statement::ShowTables => Ok(self.exec_show_tables()),
327            Statement::ShowDatabases => Ok(self.exec_show_databases()),
328            Statement::ShowCreateTable(name) => self.exec_show_create_table(&name),
329            Statement::ShowIndexes(name) => self.exec_show_indexes(&name),
330            Statement::ShowStatus => Ok(self.exec_show_status()),
331            Statement::ShowVariables => Ok(self.exec_show_variables()),
332            Statement::ShowProcesslist => Ok(self.exec_show_processlist()),
333            Statement::ShowColumns(table) => self.exec_show_columns(&table),
334            Statement::ShowUsers => Ok(self.exec_show_users()),
335            Statement::ShowPublications => Ok(self.exec_show_publications()),
336            Statement::ShowSubscriptions => Ok(self.exec_show_subscriptions()),
337            Statement::CreateUser(s) => self.exec_create_user(&s),
338            Statement::DropUser(name) => self.exec_drop_user(&name),
339            Statement::Explain(e) => self.exec_explain(&e, cancel),
340            Statement::AlterIndex(s) => self.exec_alter_index(s),
341            Statement::AlterTable(s) => self.exec_alter_table(s),
342            Statement::CreatePublication(s) => self.exec_create_publication(s),
343            Statement::DropPublication(name) => self.exec_drop_publication(&name),
344            Statement::CreateSubscription(s) => self.exec_create_subscription(s),
345            Statement::DropSubscription(name) => self.exec_drop_subscription(&name),
346            // v6.1.7 — WAIT FOR WAL POSITION needs `lag_state`,
347            // which lives in spg-server's ServerState. The engine
348            // surfaces a clear error; the server-layer dispatch
349            // intercepts the SQL before it reaches the engine on
350            // a server build, so this arm only fires for
351            // engine-only callers (spg-embedded, lib tests).
352            Statement::WaitForWalPosition { .. } => Err(EngineError::Unsupported(
353                "WAIT FOR WAL POSITION must be handled by the server layer".into(),
354            )),
355            // v6.2.0 — ANALYZE recomputes per-column histograms.
356            Statement::Analyze(target) => self.exec_analyze(target.as_deref()),
357            // v6.7.3 — COMPACT COLD SEGMENTS.
358            Statement::CompactColdSegments => self.exec_compact_cold_segments(),
359            // v7.12.1 — SET / RESET session parameter. Engine
360            // tracks the value in `session_params`; FTS dispatcher
361            // reads `default_text_search_config`. Everything else
362            // is a recorded no-op (PG dump compat).
363            Statement::SetParameter { name, value } => {
364                self.set_session_param(name, value);
365                Ok(QueryResult::CommandOk {
366                    affected: 0,
367                    modified_catalog: false,
368                })
369            }
370            // v7.14.0 — MySQL multi-assignment SET. Each pair runs
371            // through `set_session_param` so engine-known params
372            // (FOREIGN_KEY_CHECKS, session_replication_role, …) take
373            // effect; unknown pairs (including `@VAR` LHS from the
374            // mysqldump preamble) are recorded then ignored.
375            Statement::SetParameterList(pairs) => {
376                for (name, value) in pairs {
377                    self.set_session_param(name, value);
378                }
379                Ok(QueryResult::CommandOk {
380                    affected: 0,
381                    modified_catalog: false,
382                })
383            }
384            // v7.12.4 — CREATE FUNCTION / CREATE TRIGGER / DROP …
385            // for the PL/pgSQL trigger surface. exec_* methods are
386            // defined alongside the existing CREATE handlers below.
387            Statement::CreateFunction(s) => self.exec_create_function(s),
388            Statement::CreateTrigger(s) => self.exec_create_trigger(s),
389            Statement::DropTrigger {
390                name,
391                table,
392                if_exists,
393            } => self.exec_drop_trigger(&name, &table, if_exists),
394            Statement::DropFunction { name, if_exists } => {
395                self.exec_drop_function(&name, if_exists)
396            }
397            Statement::CreateSequence(s) => self.exec_create_sequence(s),
398            Statement::AlterSequence(s) => self.exec_alter_sequence(s),
399            Statement::DropSequence { names, if_exists } => {
400                self.exec_drop_sequence(&names, if_exists)
401            }
402            Statement::CreateView(s) => self.exec_create_view(s),
403            Statement::DropView { names, if_exists } => self.exec_drop_view(&names, if_exists),
404            Statement::CreateMaterializedView(s) => self.exec_create_materialized_view(s),
405            Statement::RefreshMaterializedView { name, with_data } => {
406                self.exec_refresh_materialized_view(&name, with_data)
407            }
408            Statement::DropMaterializedView { names, if_exists } => {
409                self.exec_drop_materialized_view(&names, if_exists)
410            }
411            Statement::CreateType(s) => self.exec_create_type(s),
412            Statement::DropType { names, if_exists } => self.exec_drop_type(&names, if_exists),
413            Statement::CreateDomain(s) => self.exec_create_domain(s),
414            Statement::DropDomain { names, if_exists } => self.exec_drop_domain(&names, if_exists),
415            Statement::CreateSchema {
416                name,
417                if_not_exists,
418            } => self.exec_create_schema(name, if_not_exists),
419            Statement::DropSchema { names, if_exists } => self.exec_drop_schema(&names, if_exists),
420            Statement::ResetParameter(target) => {
421                match target {
422                    None => self.session_params.clear(),
423                    Some(name) => {
424                        self.session_params.remove(&name.to_ascii_lowercase());
425                    }
426                }
427                Ok(QueryResult::CommandOk {
428                    affected: 0,
429                    modified_catalog: false,
430                })
431            }
432        };
433        self.enforce_row_limit(result)
434    }
435}