Platforms¶
base
¶
PlatformAdapter Protocol: the contract every platform integration implements.
PlatformAdapter
¶
Bases: Protocol
Executes SQL against a data platform; returns rows + schema + latency.
Required behavior
- On success: return
ExecutionResultwithrowspopulated andschema_populated — aTypedSchema(eachColumn.typeaSqlTypebuilt from the driver's native type string in the adapter's static dialect), or anUntypedSchema(names only) when the driver reports no result-column types — with non-negativelatency_secondsanderror is None. - On query failure: return
ExecutionResultwithrows=[],schema_=None, anerrordescribing the failure, and non-negativelatency_seconds. Do NOT raise. latency_secondsmeasures wall-clock time spent insideexecute().
execute
¶
execute(sql: str) -> ExecutionResult
Execute one SQL statement and return its structured result.
cancel
¶
Abort the query currently executing on this connection, if any.
Must be safe to call from another thread while execute is blocked. A no-op when no
query is running, and best-effort: it must not raise — a cancellation that fails or
arrives late is non-fatal.
TypeResolvingAdapter
¶
Bases: Protocol
Capability for backends whose execute().schema_ reports types with unresolved parameters.
Implemented only by adapters whose driver drops type parameters (e.g. a DECIMAL scale
or an ARRAY element type); precise backends do not implement it. Both methods must be
pure (no I/O): one produces the SQL that probes for precise types, the other interprets
that probe's result rows. The caller runs the probe.
type_probe_sql
¶
Build the probe statement that recovers precise types for sql's projection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sql
|
str
|
The statement whose projected column types to resolve. |
required |
Returns:
| Type | Description |
|---|---|
str
|
A probe statement for the caller to execute; its rows feed |
types_from_probe
¶
types_from_probe(
rows: list[dict[str, Any]],
) -> list[SqlType] | ExecutionError
Parse the probe's result rows into precise SqlTypes, one per projected column.
They must be returned in the order the query projects its columns.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rows
|
list[dict[str, Any]]
|
The rows returned by executing |
required |
Returns:
| Type | Description |
|---|---|
list[SqlType] | ExecutionError
|
One precise |
list[SqlType] | ExecutionError
|
the rows cannot yield types. |
execute_within_budget
¶
execute_within_budget(
adapter: PlatformAdapter,
sql: str,
max_seconds: float | None,
) -> ExecutionResult
Execute sql on adapter, cancelling it if it exceeds max_seconds.
With max_seconds unset, runs adapter.execute directly. Otherwise runs it on a worker
thread and waits up to max_seconds; if the query is still running, calls
adapter.cancel() to abort it and returns an ExecutionResult.error describing the
overrun. The cancelled query is awaited so its connection is released before returning.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
adapter
|
PlatformAdapter
|
The platform adapter to execute against. |
required |
sql
|
str
|
The SQL statement to execute. |
required |
max_seconds
|
float | None
|
Wall-clock ceiling for the query, or |
required |
Returns:
| Type | Description |
|---|---|
ExecutionResult
|
The adapter's |
ExecutionResult
|
|
execution_error
¶
execution_error(
exc: Exception,
kind: ExecutionErrorKind = "query_failed",
) -> ExecutionError
Translate a driver exception into a typed ExecutionError, preserving structured detail.
Reads whatever structured attributes the exception happens to expose — SQLSTATE, an error
condition/class, message parameters — by duck-typing, so no driver package is imported or
enumerated and no driver message is parsed. The live exception is kept as cause for
debugging; message falls back to the exception's class name when its string form is empty.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
exc
|
Exception
|
The driver exception to translate. |
required |
kind
|
ExecutionErrorKind
|
The |
'query_failed'
|
Returns:
| Type | Description |
|---|---|
ExecutionError
|
An |
ExecutionError
|
fields, and |
rows_or_error
¶
rows_or_error(
columns: list[Column],
rows_raw: list[tuple[Any, ...]],
latency_seconds: float,
) -> ExecutionResult
Build a successful typed ExecutionResult, or an error one if column names collide.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
columns
|
list[Column]
|
The result-set columns, in order, as reported by the driver. |
required |
rows_raw
|
list[tuple[Any, ...]]
|
The positional row tuples, aligned with |
required |
latency_seconds
|
float
|
Wall-clock time spent executing the query. |
required |
Returns:
| Type | Description |
|---|---|
ExecutionResult
|
An |
ExecutionResult
|
rows or schema) when one or more column names are duplicated. |
untyped_rows_or_error
¶
untyped_rows_or_error(
names: list[str],
rows_raw: list[tuple[Any, ...]],
latency_seconds: float,
) -> ExecutionResult
Build a successful untyped ExecutionResult, or an error one if column names collide.
For engines whose driver reports no result-column types (e.g. SQLite): the schema carries column names only.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
names
|
list[str]
|
The result-column names, in order, as reported by the driver. |
required |
rows_raw
|
list[tuple[Any, ...]]
|
The positional row tuples, aligned with |
required |
latency_seconds
|
float
|
Wall-clock time spent executing the query. |
required |
Returns:
| Type | Description |
|---|---|
ExecutionResult
|
An |
ExecutionResult
|
no rows or schema) when one or more column names are duplicated. |
registry
¶
Platform-reference builders and PlatformRef -> live PlatformAdapter resolution.
duckdb_platform
¶
duckdb_platform(
name: str, path: str = ":memory:"
) -> PlatformRef
Build a PlatformRef for an in-process DuckDB database.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
A unique name identifying this platform connection. |
required |
path
|
str
|
The DuckDB database path. Defaults to |
':memory:'
|
Returns:
| Type | Description |
|---|---|
PlatformRef
|
A serializable |
PlatformRef
|
driver. |
sqlite_platform
¶
sqlite_platform(
name: str, path: str = ":memory:"
) -> PlatformRef
Build a PlatformRef for an in-process SQLite database.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
A unique name identifying this platform connection. |
required |
path
|
str
|
The SQLite database path. Defaults to |
':memory:'
|
Returns:
| Type | Description |
|---|---|
PlatformRef
|
A serializable |
PlatformRef
|
(SQLite ships with the standard library). |
postgres_platform
¶
postgres_platform(
name: str, conninfo: str = ""
) -> PlatformRef
Build a PlatformRef for a PostgreSQL database.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
A unique name identifying this platform connection. |
required |
conninfo
|
str
|
A libpq connection string (keyword/value or |
''
|
Returns:
| Type | Description |
|---|---|
PlatformRef
|
A serializable |
PlatformRef
|
driver. |
databricks_platform
¶
databricks_platform(
name: str,
*,
server_hostname: str,
http_path: str,
catalog: str | None = None,
schema: str | None = None,
) -> PlatformRef
Build a PlatformRef for a Databricks SQL Warehouse.
Holds only non-secret connection details; credentials are not included here.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
A unique name identifying this platform connection. |
required |
server_hostname
|
str
|
The workspace hostname (no scheme), e.g. |
required |
http_path
|
str
|
The SQL Warehouse HTTP path, e.g. |
required |
catalog
|
str | None
|
The default catalog, or |
None
|
schema
|
str | None
|
The default schema, or |
None
|
Returns:
| Type | Description |
|---|---|
PlatformRef
|
A serializable |
PlatformRef
|
driver. |
snowflake_platform
¶
snowflake_platform(
name: str,
*,
account: str,
user: str | None = None,
warehouse: str | None = None,
role: str | None = None,
database: str | None = None,
schema: str | None = None,
authenticator: str | None = None,
workload_identity_provider: str | None = None,
) -> PlatformRef
Build a PlatformRef for a Snowflake account.
Holds only non-secret connection details; credentials are not included here.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
A unique name identifying this platform connection. |
required |
account
|
str
|
The Snowflake account identifier. |
required |
user
|
str | None
|
The Snowflake user name, or |
None
|
warehouse
|
str | None
|
The default warehouse, or |
None
|
role
|
str | None
|
The default role, or |
None
|
database
|
str | None
|
The default database, or |
None
|
schema
|
str | None
|
The default schema, or |
None
|
authenticator
|
str | None
|
The authenticator to use (e.g. |
None
|
workload_identity_provider
|
str | None
|
The workload identity provider (e.g. |
None
|
Returns:
| Type | Description |
|---|---|
PlatformRef
|
A serializable |
PlatformRef
|
driver. |
resolve
¶
resolve(ref: PlatformRef) -> PlatformAdapter
Return a live adapter for ref, building and caching one on first use.
Reuses the cached adapter on subsequent calls for the same ref.name. An unsupported
kind is unrepresentable — PlatformRef validation rejects it before resolution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ref
|
PlatformRef
|
The platform reference to resolve. |
required |
Returns:
| Type | Description |
|---|---|
PlatformAdapter
|
The live |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
close_all
¶
Close every cached adapter and clear the cache (idempotent; no-op when empty).
duckdb
¶
DuckDBAdapter: in-process DuckDB execution backend.
DuckDBAdapter
¶
Executes SQL against an in-process DuckDB database.
Open a DuckDB connection to database (default :memory:).
cancel
¶
Interrupt the query currently executing on this connection.
Safe to call from another thread while execute is blocked, and a no-op when no
query is running. The interrupted execute raises duckdb.InterruptException,
which it surfaces as ExecutionResult.error like any other query failure.
close
¶
Release the underlying DuckDB connection (file handle / WAL lock).
Explicit close matters on Windows, where WAL locks make implicit cleanup unreliable.
execute
¶
execute(sql: str) -> ExecutionResult
Execute one SQL statement against the database.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sql
|
str
|
The SQL statement to execute. |
required |
Returns:
| Type | Description |
|---|---|
ExecutionResult
|
An |
ExecutionResult
|
failures are returned as |
postgres
¶
PostgresAdapter: PostgreSQL execution backend over psycopg (v3).
PostgresAdapter
¶
Executes SQL against a PostgreSQL database via psycopg (v3).
Open a psycopg connection.
conninfo is a libpq connection string — keyword/value
("host=... port=... user=... password=... dbname=...") or a
postgresql:// URI. Empty uses libpq defaults / PG* env vars.
cancel
¶
Cancel the query currently executing on this connection.
Sends a libpq cancel request over a separate channel, so it is safe to call from
another thread while execute is blocked, and a harmless no-op when no query is
running. The cancelled execute raises psycopg.errors.QueryCanceled, surfaced as
ExecutionResult.error. Cancellation failures are swallowed — they are non-fatal.
execute
¶
execute(sql: str) -> ExecutionResult
Execute one SQL statement against the database.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sql
|
str
|
The SQL statement to execute. |
required |
Returns:
| Type | Description |
|---|---|
ExecutionResult
|
An |
ExecutionResult
|
failures are returned as |
databricks
¶
DatabricksAdapter: Databricks SQL Warehouse execution backend over databricks-sql-connector.
DatabricksAdapter
¶
DatabricksAdapter(
*,
server_hostname: str,
http_path: str,
catalog: str | None = None,
schema: str | None = None,
)
Executes SQL against a Databricks SQL Warehouse via databricks-sql-connector.
Open a Databricks SQL connection.
Credentials are not passed here.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
server_hostname
|
str
|
The workspace hostname (no scheme), e.g. |
required |
http_path
|
str
|
The SQL Warehouse HTTP path, e.g. |
required |
catalog
|
str | None
|
The default catalog, or |
None
|
schema
|
str | None
|
The default schema, or |
None
|
cancel
¶
Cancel the query currently executing on this connection, if any.
Safe to call from another thread while execute is blocked; best-effort, so all
failures are swallowed.
execute
¶
execute(sql: str) -> ExecutionResult
Execute one SQL statement against the warehouse.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sql
|
str
|
The SQL statement to execute. |
required |
Returns:
| Type | Description |
|---|---|
ExecutionResult
|
An |
ExecutionResult
|
failures are returned as |
type_probe_sql
staticmethod
¶
Build the DESCRIBE QUERY probe that recovers precise types for sql.
cursor.description drops parametric type parameters (a DECIMAL scale, an ARRAY
element type); DESCRIBE QUERY reports the precise data_type per column without
running the query.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sql
|
str
|
The statement whose projected column types to resolve. |
required |
Returns:
| Type | Description |
|---|---|
str
|
A |
types_from_probe
staticmethod
¶
types_from_probe(
rows: list[dict[str, Any]],
) -> list[SqlType] | ExecutionError
Parse DESCRIBE QUERY rows into precise SqlTypes, in projection order.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rows
|
list[dict[str, Any]]
|
The rows returned by executing |
required |
Returns:
| Type | Description |
|---|---|
list[SqlType] | ExecutionError
|
One precise |
list[SqlType] | ExecutionError
|
the probe returns no rows or a row without a |