Query Stack¶
The Query Stack APIs let you compose multiple SQL operations into an immutable StatementStack and execute them in a single driver call. Each operation preserves the underlying SQLResult/ArrowResult so downstream helpers continue to work without copying data.
Overview¶
The stack system is composed of:
StatementStack– immutable builder with push helpers for execute/execute_many/execute_script/execute_arrowStackOperation– the tuple-like value object stored inside the stack (method, statement, arguments, keyword arguments)StackResult– wraps the driver’s raw result while surfacing stack metadata (rows_affected, warning, error)AsyncDriverAdapterBase.execute_stack/SyncDriverAdapterBase.execute_stack– adapter hooks that select native pipelines or the sequential fallback
Execute a Stack¶
statement stack¶from sqlspec import SQLSpec
from sqlspec.adapters.sqlite import SqliteConfig
from sqlspec.core.stack import StatementStack
spec = SQLSpec()
config = spec.add_config(SqliteConfig(connection_config={"database": ":memory:"}))
stack = (
StatementStack()
.push_execute("create table teams (id integer primary key, name text)")
.push_execute_many("insert into teams (name) values (:name)", [{"name": "Litestar"}, {"name": "SQLSpec"}])
.push_execute("select id, name from teams order by id")
)
with spec.provide_session(config) as session:
results = session.execute_stack(stack)
rows = results[-1].result.all()
Native Pipelines and Round Trips¶
StatementStack always runs in order. When a driver supports native pipelines,
the stack can be sent as a single pipeline or batch, reducing round trips. Drivers
without native support fall back to sequential execution while preserving results
per operation.
Native stack execution is available in:
AsyncPG
Psycopg (pipeline mode)
OracleDB
StatementStack¶
- class sqlspec.core.stack.StatementStack[source]¶
Bases:
objectImmutable builder that preserves ordered SQL operations.
- property operations: tuple[StackOperation, ...]¶
- push_execute_many(statement, parameter_sets, /, *filters, statement_config=None, **kwargs)[source]¶
StackResult¶
- class sqlspec.core.result.StackResult[source]¶
Bases:
objectWrapper for per-operation stack results that surfaces driver results directly.
-
result:
StatementResult|ArrowResult¶
- rows_affected¶
- error¶
- warning¶
- metadata¶
- get_result()[source]¶
Return the underlying driver result.
- Return type:
StatementResult|ArrowResult
- with_error(error)[source]¶
Return a copy of the result that records the provided error.
- Return type:
- classmethod from_sql_result(result)[source]¶
Convert a standard SQLResult into a stack-friendly representation.
- Return type:
- classmethod from_arrow_result(result)[source]¶
Create a stack result from an ArrowResult instance.
- Return type:
-
result:
Driver APIs¶
- async AsyncDriverAdapterBase.execute_stack(stack, *, continue_on_error=False)[source]
Execute a StatementStack sequentially using the adapter’s primitives.
- Return type:
- SyncDriverAdapterBase.execute_stack(stack, *, continue_on_error=False)[source]
Execute a StatementStack sequentially using the adapter’s primitives.
- Return type:
Exceptions¶
Usage Highlights¶
Build stacks once and reuse them across requests/tasks.
Call
session.execute_stack(stack, continue_on_error=False)to run fail-fast or setcontinue_on_error=Trueto record per-operation errors.Inspect
StackResult.resultto call helpers likeall(),one(),to_pandas(), orto_arrow().Adapters lists per-adapter capabilities, including whether native pipelines or sequential fallback are used for stacks.