Skip to main content
sqlassert logo

sqlassert

PyPI version CI MIT license Python 3.13+

sqlassert adds asserts and static proofs to your SQL, without touching your data, to catch common mistakes before they ship.

Open the interactive demo showing highlighted SQL and a Fan Trap finding

Open the interactive demo

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 Catalog fetcher Support
DuckDB yes high, plus the soundness oracle
PostgreSQL yes low
MySQL 8.4+ (strict sql_mode) yes low
Snowflake yes low
BigQuery yes low
Databricks / Spark yes low
Redshift yes low
Trino 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.

Every declared key, foreign key and CHECK is trusted, whether or not the engine enforces it: Snowflake, BigQuery, Databricks and Redshift keys prove what they state (see Dialects).

Documentation

The full reference is at polvoazul.github.io/sqlassert:

  • Quickstart: install, a first marker, check and suggest.
  • Markers: every ASSERT and DECLARE, what proves it, and what it guards against.
  • CLI: check for CI (exit codes, output formats, configuration), suggest, rewrites and hazards.
  • Python API: analyze, analyze_database and catalog fetchers.
  • For dbt users: sqlassert-dbt check, build and suggest.
  • 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.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sqlassert 0.4.2
File Size Uploaded Provenance Actions
sqlassert-0.4.2.tar.gz 1.1 MB With provenance Details for sqlassert-0.4.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for sqlassert 0.4.2
File Interpreter ABI Platform Actions
sqlassert-0.4.2-py3-none-any.whl Python 3 none any Details for sqlassert-0.4.2-py3-none-any.whl

Total release size: 1.4 MB

Release files / sqlassert-0.4.2.tar.gz

Download URL sqlassert-0.4.2.tar.gz
Size 1.1 MB
Tags Source
SHA-256 checksum
How to use checksums
dd34f60cb8dd7cb658c57f18ffea37d836937557973b35ccb39ef5dd6b20c703
BLAKE2b-256 checksum
How to use checksums
d59baf391298e53e2f449455786a79c3f28c2cefd9b403c06bb8cceada487ec2
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 29, 2026.

Transparency log

Release files / sqlassert-0.4.2-py3-none-any.whl

Download URL sqlassert-0.4.2-py3-none-any.whl
Size 312.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f0fcfcf66ef85f655b92d00884e336cd61c52a17a3335202ab4cd941dfc9b460
BLAKE2b-256 checksum
How to use checksums
36c55eb4aaa9c141de0af84c2c13d858f9e58302685f84f77a981baf040613ab
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 29, 2026.

Transparency log

Security for sqlassert 0.4.2

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.

Select from 2 files

Attestation for sqlassert-0.4.2.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 29, 2026.

Transparency log

A 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.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page