SQL cells¶
A SQL cell sends a query to a connected database via ADBC (DuckDB through its own driver) and stores the result as an Arrow Table artifact. Like Python and prompt cells, SQL cells participate in the DAG, cache by provenance hash, and surface their output to downstream cells.
-- @sql connection=warehouse
SELECT customer, SUM(amount) AS total
FROM orders
WHERE amount > :min_amount
GROUP BY customer
ORDER BY total DESC
customer total
0 acme_inc 18420.50
1 north_winds 9112.75
2 bright_solar 6033.10
The cell above pulls min_amount from an upstream Python cell, sends a parameterized query through the warehouse connection, and stores the resulting rows as an Arrow Table, which downstream cells receive as a pyarrow.Table (.to_pandas() for a DataFrame).
Connections¶
A SQL cell references a named connection. Connections live in notebook.toml under [connections.<name>], but you don't need to edit that file by hand. Open Notebook settings in the right sidebar, then the Connections panel, click + Add connection, and fill in the form. The driver dropdown switches the field layout per backend (path for DuckDB and SQLite; URI + auth + role + search path for PostgreSQL; account / warehouse / database / schema for Snowflake; project / dataset / credentials for BigQuery).
[connections.warehouse]
driver = "sqlite"
path = "analytics.db"
[connections.prod]
driver = "postgresql"
uri = "postgresql://localhost:5432/prod"
[connections.prod.auth]
user = "${PGUSER}"
password = "${PGPASS}"
Notes:
- Driver-specific extras (e.g.
options.search_path,options.warehousefor Snowflake, future driver-specific keys) round-trip through the editor unchanged. The form editorializes the keys it knows; everything else is preserved. - Auth values use
${VAR}indirection. Literal credentials get blanked whennotebook.tomlis saved, so committing the file never leaks secrets. The form shows a warning border on a literal value so you know to switch it to a variable reference. A personal server reads the variable from its own environment; a server in service mode reads it from the notebook's env. - Relative
pathvalues are notebook-local.path = "analytics.db"resolves against the notebook directory at execution time. The on-disk value stays relative so a notebook moves cleanly between machines. - Currently shipped drivers: DuckDB, SQLite, PostgreSQL, Snowflake, and BigQuery. DuckDB uses the native DuckDB DBAPI; the other four are ADBC-backed (
adbc-driver-sqlite,adbc-driver-postgresql,adbc-driver-snowflake,adbc-driver-bigquery). For Snowflake, read cells userole;write=truecells switch towrite_rolewhen configured, otherwise they reuserole. For BigQuery, read cells usecredentials_path;write=truecells switch towrite_credentials_pathwhen configured, otherwise they reusecredentials_path. Snowflake and BigQuery do not have a session-level read-only flag like PostgreSQL'sSET default_transaction_read_only = on, so the safety boundary is the grants on the configured role or service account.
DuckDB over the lake¶
A DuckDB connection can read the organization's Iceberg catalog and the notebook's mounts. catalog names a REST, AWS Glue or SQL catalog the server configures ([tool.strata.catalogs.<name>]) or, on a personal server, one the notebook defines in [catalogs.<name>]. mounts names mounts declared for the cell, in [[mounts]] or with -- @mount:
-- @sql connection=lake
SELECT t.zone, count(*) AS trips
FROM lake.taxi.trips t JOIN raw USING (zone)
GROUP BY t.zone
A SQL catalog (type = "sql") is one PyIceberg keeps in a database, the one @write_table creates for a warehouse URI such as file:///root/lake#taxi.zone_month (/root/lake/catalog.db). DuckDB cannot attach one, so each table the query names is a view over its metadata file at the snapshot the cell's provenance folds. A query reads it as <catalog>.<namespace>.<table>, but an AT (VERSION => ...) or AT (TIMESTAMP => ...) of its own on such a table fails, since a view has no versions. Rows in the database are kept per catalog name, and @write_table names its warehouse's catalog strata, so the block that reads it is [catalogs.strata]. Under any other name, its tables do not exist:
[catalogs.strata]
type = "sql"
uri = "sqlite:////root/lake/catalog.db"
warehouse = "file:///root/lake"
[connections.lake]
driver = "duckdb"
path = ":memory:"
catalog = "strata"
The catalog's s3.* keys read its tables under its s3:// warehouse and the locations of the tables the query names. A table on GCS or Azure fails naming its location, since DuckDB has no credentials for it. A block with no type is typed from its uri as PyIceberg does (sqlite and postgresql are SQL). Any other catalog type fails naming the types DuckDB reads.
Each mount is a view by its name over its Parquet, CSV or JSON files, read with the mount's storage options. file, s3, gs and az mounts work; GCS and Azure ones are read through the same fsspec filesystem (gcsfs, adlfs) a Python cell's mount uses, so a connection's mounts on one of those schemes must share their storage options. Every catalog table the query reads is an input the way an @table declaration is: the cell's provenance folds its current snapshot, the query reads that snapshot, and a new snapshot makes the cell idle (out of date) and the cells downstream of it stale. With -- @cache snapshot the cell instead keeps reading the snapshots its first run read until it is rerun. Each mount's fingerprint is folded too, so a new file does the same. The catalog is attached read-only and the mounts are views; a write cell on the connection writes only its path database. A REST catalog's s3.* keys apply only under an s3:// warehouse; otherwise its tables are read with the credentials the catalog vends. A catalog that vends nothing (its store has no STS) sets header.X-Iceberg-Access-Delegation to something other than vended-credentials, usually empty: DuckDB then asks it for no credentials, and its s3.* keys read the tables, under its s3:// warehouse or, when the warehouse is a name, under the locations of the tables the query names. A table named only at run time is outside those, so its files are read without the keys. So are data files outside a table's location (added with add_files from another prefix, or a write.data.path elsewhere): in personal mode they are read without the keys and fail with the store's 403, and service mode refuses them already. A Glue catalog is attached through Glue's Iceberg REST endpoint in its glue.region for the account glue.id, both required; its glue.* keys (or client.*, or s3.*) sign the catalog's requests and read its tables' files, and without keys the AWS credential chain (environment, profile, instance role) does.
A read cell runs reads: SELECT, set operations, VALUES, TABLE, SUMMARIZE, PIVOT, UNPIVOT, DESCRIBE, SHOW and plain EXPLAIN. Anything else (DDL, DML, COPY, ATTACH, USE, CALL, SET, PRAGMA, and EXPLAIN ANALYZE, which runs the statement it describes) is refused before the cell reaches the driver, naming the statement, because the read-only transaction the driver opens is one a COMMIT in the body can end. This holds on every driver, not only DuckDB. Use -- @sql connection=<name> write=true for a cell that changes a database.
Schema discovery¶
The Schema panel, under Notebook settings in the sidebar, shows the tables and columns visible through each declared connection. Click a connection to lazy-load its schema; click a table to expand its columns. The ↻ button re-fetches when the underlying database has changed externally. No SQL cell needs to run for this: the panel talks directly to each driver's catalog query surface (sqlite_master for SQLite, information_schema.tables JOIN columns for PostgreSQL, and the driver-specific catalog queries for Snowflake and BigQuery).
Bind parameters¶
:name placeholders resolve against upstream cell variables. One inside a string literal or a comment is not a placeholder, and strings end where the connection's dialect ends them (on Snowflake and BigQuery, \' does not). Strata coerces a strict allowlist of Python types (int, float, str, bytes, bool, None, Decimal, UUID, datetime/date/time) into ADBC bind values; anything else (a list, a table, a custom object) is rejected with a clear error. An upstream numpy scalar is stored as the Python number it holds, so it binds as that number. A value stored as a Python pickle is rejected without being loaded: the server never unpickles a cell's output. No string substitution ever: values flow through ADBC's prepared-statement layer, so adversarial strings ('; DROP TABLE …) round-trip as data, not SQL.
order_id customer_id amount placed_at
0 1042 7 245.99 2026-04-12
1 1093 14 180.50 2026-04-13
2 1117 3 299.00 2026-04-14
The DAG links the SQL cell to the Python cell automatically, with the same edge logic Strata uses for Python free variables. The run records the values it bound, so the lineage behind a result names the value it ran at.
Cache policies¶
A SQL cell's provenance hash folds together:
- The query's own tokens (so whitespace, comment and keyword-case edits don't churn the cache, and any other edit runs the query again).
- The bind parameters (type-tagged:
True≠1). - The connection's identity (host / DB / user / role / search_path, never the password).
- The hashes of every upstream artifact referenced via
:name. - The driver's freshness probe result for the touched tables.
- The driver's schema fingerprint for the touched tables.
- A salt derived from the
-- @cachepolicy below.
-- @cache controls how DB-side state factors in. Default is fingerprint.
| Policy | Behavior | When to use |
|---|---|---|
fingerprint |
Default. Probe-derived freshness token + schema fingerprint folded in. | Most queries. |
forever |
Static salt; never invalidates from DB-side state. | True reference data. User asserts. |
session |
Session-unique salt; invalidates across sessions. | Always-fresh queries / dashboards. |
ttl=<seconds> |
floor(now / ttl) in the salt; bucketed time-based invalidation. |
Stale-tolerant aggregations. |
snapshot |
Pins the query to one point in time through the warehouse's time travel (Snowflake, BigQuery), or to the Iceberg snapshots of a DuckDB connection's catalog tables. Refused on other drivers before a connection opens. See @cache. |
Reproducibility-critical reads. |
On reopen. fingerprint and snapshot say, in the cell's own annotation,
that the cached rows are good only while the source still agrees. On a SQLite or
DuckDB file with no lake, the staleness pass asks it, with the same probe a run
makes, so the cell comes back ready if the database is as the run left it and
stale if it changed. A cell left stale is not probed again in that session
until it runs. A cell whose run did not reuse its rows (a table named only
at run time, @nocache, a database that changed during the query) opens idle.
On any other connection asking the source is a query over the network, not
something to do while opening a notebook, so the cell opens idle until it runs.
forever, session and ttl can come back ready, because each has
already said what it depends on and that is settled locally: a session cell
matches only within the session that ran it, and a ttl cell only inside the
same time bucket, so one whose window has passed comes back idle anyway.
The connection counts too. Repointing [connections.<name>] at another
database, or at a different credential, means the cell is no longer ready about
what it was ready about, so it opens idle rather than green with the previous
database's rows.
Per-driver freshness¶
fingerprint correctness depends on what the driver can probe.
| Driver | Probe | Granularity | Notes |
|---|---|---|---|
| PostgreSQL | pg_stat_user_tables + pg_class.relfilenode |
per-table | Up to ~500 ms stats-collector lag. |
| SQLite | The sha256 of the database file and of its -wal sidecar, plus PRAGMA data_version / schema_version |
DB-wide | Any commit to the file, from any process, changes the token, so a write to one table invalidates cells reading another. A touch or a copy that leaves the bytes alone does not. A file is read only when the notebook's digest cache (.strata/hashes.sqlite) holds no sha256 for its stat, so an unchanged database costs a stat. A file past 64 MiB with no sha256 is not read before every run: it is keyed by its stat (inode, size, mtime and ctime), so a touch runs the cell again, and a copy over it at the same size and mtime still does, since the copy moves the ctime. On Windows, where a file's ctime is its creation time, a rewrite in place that keeps the size and restores the mtime is not seen. A :memory: database has only the pragmas. |
| DuckDB | PRAGMA database_size, but its memory columns |
DB-wide | Advances when DuckDB writes blocks; two row states between block-aligned flushes can share a token. Pin -- @cache session on a database that is being written to while you work. |
| Snowflake | INFORMATION_SCHEMA.TABLES.LAST_ALTERED |
per-table | Per-database scoping (one query per touched database). Bills cloud-services credits but each query is small. LAST_ALTERED updates even on 0-row DML, safe direction (over-invalidates, never under). |
| BigQuery | __TABLES__.last_modified_time |
per-table | Per-dataset scoping. __TABLES__ is the legacy-but-stable view; INFORMATION_SCHEMA.TABLES doesn't expose last_modified_time. Streaming-buffer caveat: tables receiving streaming inserts have last_modified_time lag by minutes-to-90-min until the buffer flushes; pin -- @cache session on those queries. Permissions: bigquery.tables.get. |
The schema fingerprint catches metadata-only changes (ADD COLUMN, type changes, nullability flips) that the freshness token would miss.
A probe can only ask about a table the query names. A table named at run time (Snowflake's IDENTIFIER($tbl) or TABLE($tbl)), a table function (read_parquet(...), query_table(...), a Postgres set-returning function) or a file read in place of a table (DuckDB's FROM 'events.parquet') is not fingerprinted, so under fingerprint such a cell re-runs every time, and the header says so. Its result's content is folded into its provenance, so a downstream cell re-runs only when the rows actually changed. Declare -- @cache session or -- @cache ttl=... to reuse results instead, or read files through a mount on a DuckDB lake connection, whose fingerprint the cache does track.
The probes run again after the query, and a DuckDB lake connection's mounts are fingerprinted again, since the query reads the live database and files after its key was taken. If one changed while the query ran, or cannot be probed again, the result is not cached: its content is folded into its provenance as above, and the console says what changed ("the database of connection 'db' changed while the query ran; its result is not cached"). So a commit during the query is never served later, even once the database is put back. Only content is compared, so a change undone before the query ends is not seen. A pinned snapshot (-- @cache snapshot, a catalog table read at its snapshot) cannot change and is not probed again, and a result no cache check finds (-- @nocache, a table named at run time) is not checked. The check costs one more probe, and one more listing of each mount, per run that misses the cache.
Read-only by default¶
SQL cells are read-only by default, but the enforcement mechanism depends on the backend:
- SQLite:
mode=roplusPRAGMA query_only=ON - DuckDB: a file opens with
read_only=True, and every read runs insideBEGIN TRANSACTION READ ONLY(which also covers:memory:) - PostgreSQL:
SET default_transaction_read_only = on - Snowflake: the configured
rolemust be read-only - BigQuery: the configured
credentials_pathmust point at a read-only service account
For SQLite, DuckDB and PostgreSQL, Strata enforces read-only at the connection/session level. For Snowflake and BigQuery, Strata selects the read-scoped role or credentials, and the cloud platform's grants are the actual boundary. In all cases, the default path is “read unless you explicitly opt into write=true,” and the statement check above refuses anything but a read before the driver sees it.
In service mode a DuckDB or SQLite cell, which runs inside the server process, is also confined: its database file must be in the notebook's directory or outside the server's state (a SQLite uri is refused), a DuckDB connection reaches only its own database and the mounts and catalog tables it reads, and a SQLite write cell cannot ATTACH, DETACH or VACUUM. A BigQuery connection there must name a credentials_path key file in the notebook's directory, so it never runs as the server's own Google credentials. See Service mode.
Write cells¶
Setup, seeding, and migration scripts opt into writable execution per cell:
-- @sql connection=warehouse write=true
DROP TABLE IF EXISTS orders;
CREATE TABLE orders (
id INTEGER PRIMARY KEY,
customer TEXT NOT NULL,
amount REAL
);
INSERT INTO orders VALUES (1, 'alice', 25.50), (2, 'bob', 199.99);
- The body is split into individual statements on sqlglot's tokenizer (ADBC's cursor runs only the first statement otherwise). Each statement runs as written in the cell, and a trailing comment is not a statement.
:namebind placeholders work the same as in read cells.- With no
-- @cache, a write cell is cached by its body and reused in the session that ran it. When it is the only write cell on a SQLite file (path), it is also reused in a later session while the file is exactly as its run left it: a probe settles it, so a notebook whose seed ran last session opens ready, and running the seed again changes nothing. A database deleted or changed since runs it again; writes from a Python cell, or through another database (a DuckDBATTACH), are not seen as writers, but the change they make is. With a second write cell on the file (any SQLiteuricounts), which may have moved the file or may undo this one, and on any other database (DuckDB, whose probe can miss a change; one in memory; a server), a new session runs it again.-- @cache session,foreverandttl=<seconds>keep their own meaning. -- @cache fingerprintand-- @cache snapshoterror early on write cells: probe-based invalidation has no anchor when the cell mutates state.- The cell still produces an Arrow artifact: a per-statement status table with
stmt,kind(CREATE TABLE,INSERT, …), androws_affected(nullable;nullfor DDL). - Read cells using the same connection stay on the read path; the override is per-cell.
A query runs off the server's event loop, so a cancel lands at once: the run is abandoned and publishes nothing. The query itself may keep running in the database, because ADBC drivers differ on whether they can cancel one.
-- @name and downstream consumption¶
A SQL cell's output variable name defaults to result; override with -- @name <identifier> (a Python identifier, not a keyword). Downstream cells receive the result as a pyarrow.Table (Arrow IPC artifacts deserialize through the standard notebook serializer); call .to_pandas() for a pandas DataFrame:
-- @sql connection=warehouse
-- @name top_customers
SELECT customer, SUM(amount) AS total
FROM orders GROUP BY customer ORDER BY total DESC LIMIT 5
# downstream Python cell
print(top_customers.shape) # (5, 2)
print(top_customers.to_pandas()["total"].sum())
A Python cell that uses a name no cell defines, below a SQL cell still named result, gets a warning on validation (sql_result_unnamed) naming that SQL cell and the -- @name to add; its NameError at run time says the same.
-- @after for setup-then-query pipelines¶
A read SQL cell that depends on a write SQL cell's side effects (the underlying database state) can declare an explicit ordering edge:
-- @sql connection=warehouse write=true
CREATE TABLE products (sku TEXT PRIMARY KEY, category TEXT);
INSERT INTO products VALUES ('A', 'widgets'), ('B', 'gadgets');
-- @sql connection=warehouse
-- @after seed
SELECT category, COUNT(*) FROM products GROUP BY category
-- @after seed adds a DAG edge from the seed cell to this one even though no Python variable flows between them: the dependency is on a side effect (the SQLite file). This is what cascade execution and staleness recompute use to ensure the right ordering. The upstream's last run is also part of this cell's cache key, as for any cell's @after: each time the seed's code runs (a new body, a new session under -- @cache session, a rerun of the same body, which rebuilds the database, or a run that fails partway), this cell is stale and its next run misses its cache, even under -- @cache forever, and a -- @cache snapshot cell takes a new snapshot. A seed that is a cache hit runs nothing and keeps it cached. A write cell after the seed runs again too, so a rerun of the seed does not leave its rows missing; two writes with no edge between them are not told apart, so give the later one -- @after.
Worked example¶
The sql_orders_report example notebook walks through all of this end-to-end: a SQL seed cell, a Python threshold cell, two parameterized SQL queries, and a Python report cell: five cells, two languages, with both fingerprint and forever cache policies side by side.
SQL-cell annotations¶
| Annotation | What it does |
|---|---|
-- @sql connection=<name> [write=true] |
Name the declared connection the cell runs on, and whether it may write |
-- @cache <policy> |
Override the default fingerprint cache policy |
-- @nocache |
Always re-execute; wins over any -- @cache policy |
-- @name <identifier> |
Name the output variable (default: result) |
-- @after <cell-id> |
An edge from an upstream cell, keyed on its last run |
-- @mount <name> <uri> ro |
A mount a DuckDB connection's mounts can name |
These are the only annotations a SQL cell acts on; validation warns about any other.
They are SQL comments, so a # line is not one (see Writing them).
See [Cell Annotations][a] for the full reference.