sqlassert¶
sqlassert adds asserts and static proofs to your SQL, without touching your data, to catch common mistakes before they ship.
Mark what a query must guarantee (this join never multiplies rows, this
column is a key, this CASE covers every status) and sqlassert proves it
from the SQL and schema alone. A proof holds for any data, not just the rows
in the table today, and it runs in milliseconds in your test suite or CI, with
no warehouse and no connection.
pip install sqlassert
sqlassert suggest my_query.sql # what can already be proved, and what looks wrong
Try the interactive demo · Quickstart · GitHub
Alpha
The markers (/**ASSERT ...**/, /**DECLARE ...**/) are the stable
contract; the Python API may change. Only DuckDB and SQLite are high
support: elsewhere some dialect facts are unprobed, so a
proof can be wrong.
What it proves¶
| Assertion | Marker | Guards against |
|---|---|---|
| Join cardinality | UNIQUE, MANY TO ONE, ONE TO MANY, ONE TO ONE before a join |
row multiplication |
| Exactly One Join | EXACTLY ONE before a join |
rows multiplied or dropped; a LEFT JOIN that pads |
| Unique Set | UNIQUE(cols), NOT NULL UNIQUE(cols), PRIMARY KEY(cols) |
duplicate keys, wrong grain |
| Not Null | NOT NULL(cols) |
NULLs from outer joins and nullable sources |
| Not Null Elements | NOT NULL ELEMENTS(cols) |
NULLs inside arrays, rows and maps |
| Foreign Key | FOREIGN KEY(cols) REFERENCES rel(cols) |
orphaned rows |
| Functional Dependency | DETERMINES(a -> b) |
a value that should follow from a key but doesn't |
| Enum | ENUM(col IN (...)) |
unexpected values |
| Exhaustive CASE | EXHAUSTIVE before a CASE |
a value no WHEN arm handles |
Tools¶
| Tool | What it does |
|---|---|
analyze(sql) |
Python API: prove a SQL Program's markers from its CREATE statements, in your test suite. |
analyze_database |
The same, with tables, keys and views read from a live catalog (metadata only, never rows). |
sqlassert check |
CLI for CI: text, JSON, SARIF and GitHub annotation output, pyproject.toml config. |
sqlassert suggest |
Lists provable markers, proved rewrites, and hazards (fan traps, chasm traps, arbitrary LIMIT, ...). |
sqlassert-dbt |
Checks a dbt project from its manifest and skips the tests the SQL proves. |