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
Using dbt? sqlassert-dbt proves your unique, not_null, accepted_values and relationships tests from the compiled SQL and skips the ones that can't fail, so dbt build runs fewer warehouse queries.
Alpha: the markers (/**ASSERT ...**/, /**DECLARE ...**/) are the stable contract; the Python API may change. Tested with DuckDB, PostgreSQL, MySQL, Snowflake, BigQuery, Databricks / Spark, Redshift and Trino SQL (see Dialects); only DuckDB programs are checked by the soundness oracle, and only DuckDB and SQLite are high support: elsewhere some dialect facts are unprobed, so a proof can be wrong.
Pitfalls it catches
A join that silently multiplies rows
It works today because every user has one email row. Then someone adds a second one, and every downstream count doubles. Say what the join must do:
create table users (id integer primary key, email varchar);
create table user_emails (user_id integer, email varchar);
select u.id, e.email
from users u
/**ASSERT MANY TO ONE**/ join user_emails e on e.user_id = u.id;
$ sqlassert check join.sql
join.sql:6:1: error unproved-many-to-one-join: /**ASSERT MANY TO ONE**/ is not proved (UNKNOWN)
Nothing makes user_emails.user_id unique, so the join may fan out, and --explain says exactly that: "No known unique set on this relation — user_emails AS e". Add the key (or a GROUP BY, DISTINCT, or QUALIFY row_number() = 1) and it is PROVED. From then on, a change that breaks it fails CI rather than corrupting a dashboard.
Fan traps: sums counted twice
Two one-to-many joins from the same row, then a sum: each order is counted once per payment.
select c.id, sum(o.amount) as ordered, sum(p.amount) as paid
from customers c
join orders o on o.customer_id = c.id
join payments p on p.customer_id = c.id
group by c.id;
$ sqlassert suggest revenue.sql
...
low line 9 Root Select Fan trap SUM(o.amount) reads rows that JOIN payments AS p may repeat: the join is not proved to repeat rows of only one input (fan trap), so a value may be counted more than once
low line 9 Root Select Fan trap SUM(p.amount) reads rows that JOIN payments AS p may repeat: ...
Chasm traps (sum(c.credit) over customers JOIN orders), scalar subqueries that can return two rows, and implicit casts in join keys are flagged the same way.
Joins that do nothing, and LIMITs that pick random rows
select o.id, u.name
from orders o
left join users u on u.id = o.user_id -- user_id is NOT NULL REFERENCES users(id)
left join coupons c on c.id = o.coupon_id -- nothing from coupons is used
order by u.name
limit 10;
$ sqlassert suggest orders.sql
rewrite line 7 LEFT JOIN users AS u INNER JOIN every left row matches exactly one right row, so the LEFT JOIN pads nothing: write JOIN
--- orders.sql
+++ orders.sql (rewritten)
@@ -5,5 +5,5 @@
select o.id, u.name
from orders o
-left join users u on u.id = o.user_id -- user_id is NOT NULL REFERENCES users(id)
+join users u on u.id = o.user_id -- user_id is NOT NULL REFERENCES users(id)
left join coupons c on c.id = o.coupon_id -- nothing from coupons is used
order by u.name
rewrite line 8 LEFT JOIN coupons AS c Redundant Join at most one match per row and no column of coupons AS c is used outside its ON: delete the join
--- orders.sql
+++ orders.sql (rewritten)
@@ -6,5 +6,5 @@
from orders o
left join users u on u.id = o.user_id -- user_id is NOT NULL REFERENCES users(id)
-left join coupons c on c.id = o.coupon_id -- nothing from coupons is used
+-- nothing from coupons is used
order by u.name
limit 10;
marker line 7 LEFT JOIN users AS u Exactly One Join /**ASSERT EXACTLY ONE**/
...
hazard line 10 Root Select Arbitrary LIMIT the ORDER BY does not determine ('id'), so rows tied on it come in an arbitrary order and LIMIT may keep different rows on each run: order by a key as well
Every rewrite is backed by a proof that it leaves the result unchanged, row for row, and comes with the exact diff that makes it; comments and markers in the text it replaces are kept. --annotate pastes the suggested markers into the file.
A CASE that forgets the new status
with o as (
select * from orders
/**DECLARE ENUM(status IN ('new', 'paid', 'void', 'refunded'))**/
)
select id,
/**ASSERT EXHAUSTIVE**/ case status when 'new' then 'open' when 'paid' then 'closed' when 'void' then 'closed' end as state
from o;
$ sqlassert check orders.sql --explain
orders.sql:7:3: error unproved-exhaustive: /**ASSERT EXHAUSTIVE**/ is not proved (UNKNOWN)
#1 UNKNOWN: exhaustive_case — CASE status WHEN 'new' THEN 'open' ...
#2 UNKNOWN: status may be 'refunded', which no WHEN arm lists
Without the marker, refunded orders would quietly get a NULL state.
dbt tests that scan the warehouse for nothing
$ sqlassert-dbt build . -- --target prod
Proved statically (a test here can be skipped):
models/schema.yml unique_stg_customers_customer_id (test on stg_customers)
...
Proved tests are passed to dbt build as --exclude, so they never run. See sqlassert-dbt.
What it proves
| Assertion | Marker | Guards against | DECLARE form |
|---|---|---|---|
| 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 | yes |
| Not Null | NOT NULL(cols) |
NULLs from outer joins and nullable sources | yes |
| Not Null Elements | NOT NULL ELEMENTS(cols) |
NULLs inside arrays, rows and maps | yes |
| Foreign Key | FOREIGN KEY(cols) REFERENCES rel(cols) |
orphaned rows | yes |
| Functional Dependency | DETERMINES(a -> b) |
a value that should follow from a key but doesn't | yes |
| Enum | ENUM(col IN (...)) |
unexpected values | yes |
| Exhaustive CASE | EXHAUSTIVE before a CASE |
a value no WHEN arm handles |
– |
Every marker is a SQL comment (/**ASSERT ...**/), so annotated SQL still runs unchanged on your database.
| 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. |
| Dialect | Key constraints | Catalog fetcher | Support |
|---|---|---|---|
| DuckDB | enforced | yes | high, plus the soundness oracle |
| PostgreSQL | enforced | yes | low |
MySQL 8.4+ (strict sql_mode) |
enforced | yes | low |
| Snowflake | unenforced, RELY |
yes | low |
| BigQuery | unenforced | yes | low |
| Databricks / Spark | unenforced, RELY |
yes | low |
| Redshift | unenforced | yes | low |
| Trino | none | yes (NOT NULL only) | low |
A low-support dialect has dialect facts written from memory and not yet probed on the engine, so its analysis can contain mistakes: a PROVED assertion there can be wrong. A dialect with no such fact is high support (DuckDB, SQLite); see Support levels.
Unenforced keys are trusted as declarations by default; trust_unenforced_keys=False keeps only those marked RELY (see Dialects).
Documentation
The full reference is at polvoazul.github.io/sqlassert:
- Quickstart: install, a first marker,
checkandsuggest. - Markers: every
ASSERTandDECLARE, what proves it, and what it guards against. - CLI:
checkfor CI (exit codes, output formats, configuration),suggest, rewrites and hazards. - Python API:
analyze,analyze_databaseand catalog fetchers. - For dbt users:
sqlassert-dbt check,buildandsuggest. - Dialects: what each engine is assumed to do.
- Diagnostic codes: what each code means and how to fix it.
- How proofs work: proofs, not data checks.
Roadmap
Open work is tracked as one file per issue in docs/issues/.
Citation
If you use sqlassert in published research, please cite the version you used. See CITATION.cff for citation details.
Metadata
Release files for sqlassert 0.4.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| sqlassert-0.4.1.tar.gz | 1.0 MB | With provenance | Details for sqlassert-0.4.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| sqlassert-0.4.1-py3-none-any.whl | Python 3 | none | any | Details for sqlassert-0.4.1-py3-none-any.whl |
Total release size: 1.3 MB
Release files / sqlassert-0.4.1.tar.gz
| Download URL | sqlassert-0.4.1.tar.gz |
|---|---|
| Size | 1.0 MB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
6e9f16b1ba42275ae5fcf4d206ed3315c32d485898818bbb4159ac1c82551e15
|
|
BLAKE2b-256 checksum How to use checksums |
f4a077f3c42c9468f8571b8484f888e09ec20931e4279bafc19522beef88ac9d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 28, 2026.
Transparency logRelease files / sqlassert-0.4.1-py3-none-any.whl
| Download URL | sqlassert-0.4.1-py3-none-any.whl |
|---|---|
| Size | 291.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ba4af497c922a6ff86cc46eabd597b137fea4a7f0c88f6233aa42549c33c7e5a
|
|
BLAKE2b-256 checksum How to use checksums |
709434095a8e38cd1d52a40767c18bf50b945c047f595ab5b04243005e162985
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 28, 2026.
Transparency logSecurity for sqlassert 0.4.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for sqlassert-0.4.1.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 28, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.