{"slug": "show-hn-sqlsure-deterministic-semantic-checks-for-ai-generated-sql", "title": "Show HN: Sqlsure – deterministic semantic checks for AI-generated SQL", "summary": "Sqlsure, a deterministic semantic checker for AI-generated SQL, catches logical errors like double-counted revenue and exposed patient identifiers that databases and linters miss. The tool, which runs in 0.1 ms without network calls, flagged 45 errors in 2,568 expert-written benchmark queries with zero false alarms, including a BIRD dev gold answer provably wrong by 8×.", "body_md": "**AI writes your SQL. sqlsure makes sure it's right.**\n\nA query can be perfectly valid, run without error, and return a number that's silently wrong — revenue double-counted by a join, an average summed, a patient identifier exposed. Databases don't catch this. Linters don't catch this. LLMs reviewing their own SQL don't catch this.\n\nsqlsure does — deterministically, in 0.1 ms, before the query runs.\n\nProof, not promises:we ran sqlsure over the gold answers of the two benchmarks every text-to-SQL model is graded on.2,568 expert-written queries, 45 flags, zero false alarms— including a BIRD dev gold answer that is[provably wrong by 8×]from the exact bug class sqlsure targets, and a schema defect[now filed upstream].\n\nsqlsure judges SQL against facts your team already declared — dbt `unique`\n\ntests become grain, `relationships`\n\ntests become join cardinality, one-line\n`meta`\n\ntags mark what's safe to sum. No new language to learn, no model to\nmaintain by hand. Rules are dictionary lookups, not LLM calls: same input,\nsame verdict, every time, offline.\n\nEvery rejection carries a machine-actionable `fix`\n\n, so AI agents\nself-repair: **draft → check → fix → check → execute.** In our benchmark,\napplying the fix verbatim produced a passing query 10/10 times.\n\n```\npip install sqlsure\npython\nfrom sqlsure import SemanticModel, check\nviolations = check(sql, model)   # [] means semantically safe\n```\n\nOr clone and run the 30-second demo:\n\n```\npython check.py                   # 5 wrong queries rejected, 1 approved — with fixes\npython -m sqlsure.scan path/to/dbt-repo --report report.md   # audit any dbt repo\n```\n\n**1. CI gate** — blocks the merge when a PR double-counts:\n\n```\npython -m sqlsure.cli --model model.json query.sql   # exit 1 on violations\n```\n\n**2. MCP server** — your AI agent must pass inspection before executing:\n\n```\nclaude mcp add sqlsure -- python -m sqlsure.mcp_server --model /abs/path/model.json\n```\n\nSee [docs/MCP.md](/sqlsure/sqlsure/blob/main/docs/MCP.md) for tool reference and agent-loop patterns.\n\n**3. Library** — embed `check()`\n\ninside any text-to-SQL product or agent\nframework. A drop-in [SemanticGate](/sqlsure/sqlsure/blob/main/integrations/semantic_gate.py) wraps\nVanna/WrenAI-style generators; a\n[semantic eval metric](/sqlsure/sqlsure/blob/main/integrations/eval_metric.py) scores NL2SQL output\nwhere execution-accuracy is blind.\n\n| Rule | Severity | Catches |\n|---|---|---|\n| FANOUT | error | SUM/COUNT of additive measure after one-to-many join |\n| CHASM | error | two+ fan-out joins multiplying each other |\n| ADDITIVITY | error | SUM of a non-additive measure (rates, averages) |\n| SEMI_ADDITIVE | error | balances/censuses summed across their snapshot dimension |\n| JOIN_KEY | error | join on columns matching no declared relationship |\n| CROSS_JOIN | error | join with no predicate |\n| WEIGHTED_AVG | warning | AVG silently re-weighted by fan-out |\n| UNDECLARED_JOIN | warning | join with no declared relationship (unverifiable ≠ safe) |\n| SENSITIVE_COLUMN | policy | PHI/PII column exposed in query output |\n\nWhen sqlsure can't verify something, it says \"can't verify\" — never \"looks fine.\" Honest uncertainty is a feature.\n\n**Deterministic**— same SQL + same rulebook = same verdict, always; rules are dictionary lookups, auditable line by line** Offline**— zero network calls;** your SQL never leaves your machine****No data access**— parses query*text*; never connects to a database**No telemetry**— nothing collected, ever ([SECURITY.md](/sqlsure/sqlsure/blob/main/SECURITY.md))** Supply chain**— releases ship exclusively via PyPI Trusted Publishing (OIDC) from tagged commits with public CI runs; two runtime deps\n\n-\n**dbt**(works today):`manifest.json`\n\nor`schema.yml`\n\n— the tests teams already wrote become enforceable semantics, zero config -\n**Plain PK/FK declarations**(works today — powered the benchmark audits) -\n**The live database itself**(works today): no semantic layer at all?`sqlsure.introspect`\n\nbuilds the rulebook from the catalog — SQLite PRAGMAs or`information_schema`\n\nPK/FK (postgres/mysql). Introspecting BIRD's own database files recovered 2 foreign keys missing from the benchmark's published schema ([bird-bench/mini_dev#37](https://github.com/bird-bench/mini_dev/issues/37))\n\n``` python\nfrom sqlsure.introspect import model_from_sqlite\nmodel = model_from_sqlite(\"app.db\")   # PK -> grain, FK -> join edges\n```\n\n-\n**Hand-written JSON**—[model.example.json](/sqlsure/sqlsure/blob/main/model.example.json) -\n**OSI and WrenAI MDL**(working loaders in[integrations/](/sqlsure/sqlsure/blob/main/integrations)):[OSI](/sqlsure/sqlsure/blob/main/integrations/osi_loader.py)demonstrated on the spec's published examples;[WrenAI MDL](/sqlsure/sqlsure/blob/main/integrations/mdl_loader.py)demonstrated on WrenAI's own shipped example manifest —`primaryKey`\n\n→ grain, relationship`joinType`\n\n+`condition`\n\n→ join edges, cube measures → additivity -\nCube, Snowflake Semantic Views — adapters on the roadmap; the engine only ever sees one\n\n`SemanticModel`\n\n**16/16 rule tests, 100% recall / 0% false positives** on the paired benchmark ([docs/METRICS.md](/sqlsure/sqlsure/blob/main/docs/METRICS.md))**Real production repos**(Mattermost's warehouse, Fivetran packages, dbt's jaffle shop) —[docs/TEST-REPORTS.md](/sqlsure/sqlsure/blob/main/docs/TEST-REPORTS.md)**Spider + BIRD gold queries**— the zero-noise external audit above\n\n[docs/EVIDENCE.md](/sqlsure/sqlsure/blob/main/docs/EVIDENCE.md)— what it does for you, every claim linked to a rerunnable measurement[docs/ARCHITECTURE.md](/sqlsure/sqlsure/blob/main/docs/ARCHITECTURE.md)— how it physically works, ELI5 → god level, with real intermediate outputs[docs/FOR-DUMMIES.md](/sqlsure/sqlsure/blob/main/docs/FOR-DUMMIES.md)— every concept from zero[docs/INTEGRATIONS.md](/sqlsure/sqlsure/blob/main/docs/INTEGRATIONS.md)— GitHub Action, pre-commit, MCP, Snowflake UDF / Cortex Agent tool, query-history audit[docs/MCP.md](/sqlsure/sqlsure/blob/main/docs/MCP.md)— MCP server documentation[CONTRIBUTING.md](/sqlsure/sqlsure/blob/main/CONTRIBUTING.md)— adding rules and loaders\n\nApache-2.0 · [sqlsure.ai](https://sqlsure.ai)\n\nmcp-name: io.github.sqlsure/sqlsure", "url": "https://wpnews.pro/news/show-hn-sqlsure-deterministic-semantic-checks-for-ai-generated-sql", "canonical_source": "https://github.com/sqlsure/sqlsure", "published_at": "2026-07-11 20:03:42+00:00", "updated_at": "2026-07-11 20:35:15.059956+00:00", "lang": "en", "topics": ["ai-tools", "developer-tools", "ai-agents"], "entities": ["sqlsure", "dbt", "BIRD", "PyPI"], "alternates": {"html": "https://wpnews.pro/news/show-hn-sqlsure-deterministic-semantic-checks-for-ai-generated-sql", "markdown": "https://wpnews.pro/news/show-hn-sqlsure-deterministic-semantic-checks-for-ai-generated-sql.md", "text": "https://wpnews.pro/news/show-hn-sqlsure-deterministic-semantic-checks-for-ai-generated-sql.txt", "jsonld": "https://wpnews.pro/news/show-hn-sqlsure-deterministic-semantic-checks-for-ai-generated-sql.jsonld"}}