How proofs work¶
sqlassert does not validate by querying actual table data. It will not run count(*), search for duplicates, or sample rows.
Instead, it proves uniqueness using information available from the SQL Program and IR-linked Knowledge. If uniqueness cannot be proven, the assertion outcome is UNKNOWN and report.proved is false. Python assertion results retain structural evidence and expose it through assertion.evidence.explain().
Supported uniqueness proofs today:
PRIMARY KEYandUNIQUEconstraints fromCREATE TABLEdeclarations or IR-linked Knowledge.GROUP BYsubqueries, when the join covers the grouping keys.SELECT DISTINCTsubqueries, when the join covers the selected distinct columns.QUALIFY row_number() over (partition by ...) = 1andDISTINCT ONsubqueries, when the join covers columns that are, or determine, every partition key.- Views, CTEs, and subqueries that preserve one of the proofs above, including through their own joins.
A view, CTE, or FROM subquery is lowered like the Root Select, joins and markers included, so its facts flow to every query that reads it. Every view is also a query of its own, checked whether or not anything reads it, so a file of views (a project's models, say) needs no Root Select. A marker in a CTE that nothing reads is reported as unanalyzed. Computed expressions and arbitrary subquery semantics are not guessed.
Examples:
-- Proved by primary key.
select *
from sessions
/**ASSERT MANY TO ONE**/ join users
on sessions.user_id = users.id;
-- Proved by composite primary key plus RHS-only filter.
select *
from sessions
/**ASSERT MANY TO ONE**/ join orders
on sessions.user_id = orders.user_id
and orders.order_id = 1;
-- Proved by GROUP BY on one side and the primary key on the other.
with latest_session as (
select user_id, max(ts) as max_ts
from sessions
group by user_id
)
select *
from users
/**ASSERT ONE TO ONE**/ join latest_session
on users.id = latest_session.user_id;
-- Proved by QUALIFY row_number() = 1.
with sessions_ranked as (
select user_id, *
from sessions
qualify row_number() over (partition by user_id order by ts) = 1
)
select *
from users
/**ASSERT ONE TO ONE**/ join sessions_ranked
on users.id = sessions_ranked.user_id;
More compile-time SQL checks can be added under the same model: explicit syntax, fast validation, and clear reasons when a proof is missing.