Python API¶
Run validation offline, before your application or analytics job executes the query. analyze takes a whole SQL Program -- the CREATE TABLE/CREATE VIEW statements a query depends on, plus the query itself -- and proves its Unique Join and Unique Set Assertions from that declared schema. It never connects to a database.
from sqlassert import analyze
program = """
create table users (id integer primary key, name varchar);
create table sessions (user_id integer);
select *
from sessions
/**ASSERT MANY TO ONE**/ join users
on sessions.user_id = users.id
"""
report = analyze(program)
assert report.proved, report.diagnostics
The default Python engine evaluates the rules directly on the IR:
report = analyze(program)
for assertion in report.assertions:
print(assertion.evidence.explain())
The rules are in rules/join_cardinality.py, rules/uniqueness.py,
rules/nullability.py, and rules/foreign_key.py; shared proof composition, declaration lookup, and
per-analysis caching live in python_engine.py.
For a test suite, keep your model/query SQL as strings or load them from files, and include (or generate) the CREATE statements for whatever schema the query assumes:
def test_query_join_contract():
program = schema_ddl() + load_query("models/session_enrichment.sql")
report = analyze(program)
assert report.proved, report.diagnostics
report.assertions contains one result per marker, and report.diagnostics explains anything the program did that this analysis could not model:
for assertion in report.assertions:
print(assertion.outcome) # Outcome.PROVED or Outcome.UNKNOWN
print(assertion.origin) # SQL source location
for diagnostic in report.diagnostics:
print(diagnostic.code, diagnostic.message)
analyze reads only the SQL Program. To take tables, keys, and views from a
live database instead of CREATE statements, pass a catalog fetcher. Fetchers
take a connection from any driver you already use; sqlassert imports none:
import duckdb
from sqlassert import analyze_database
from sqlassert.discovery.duckdb import DuckDBKnowledgeFetcher
report = analyze_database(sql, DuckDBKnowledgeFetcher(duckdb.connect("warehouse.db")))
The fetcher reads catalog metadata only (columns, nullability, primary keys,
unique constraints, unique indexes over plain columns, foreign keys, CHECK
constraints, view definitions), never table rows. A catalog CHECK
means what the same CHECK in a CREATE TABLE means. A
catalog foreign key is used when the SQL reaches both of its tables. CREATE statements in
the SQL take precedence over catalog objects of the same name; a catalog foreign
key onto such a table binds to the SQL's declaration, and warns
unbound-foreign-key if that declaration lacks a referenced column or the SQL
declares the table twice.
PostgreSQL, MySQL, Snowflake, BigQuery, Databricks, Redshift and Trino have
fetchers too, in sqlassert.discovery.<dialect>. They take any DB-API
connection (psycopg, PyMySQL, snowflake-connector-python, ...), so sqlassert
depends on no driver:
import psycopg
from sqlassert.discovery.postgres import PostgresKnowledgeFetcher
report = analyze_database(sql, PostgresKnowledgeFetcher(psycopg.connect(dsn)), dialect="postgres")
Warehouse keys are not enforced, yet catalog keys there are trusted like any
declared key (see Dialects). Snowflake, BigQuery, Redshift
and Trino have no CHECK constraints; Databricks's come from a Delta table's
properties.
BigQueryKnowledgeFetcher also takes the project and default dataset. Trino's catalog has no keys, so
TrinoKnowledgeFetcher reports NOT NULLs only.