Enforcing MDL: rejecting SQL that violates declared relationships (working code) #2538
Replies: 1 comment
|
Hi @tejusarora — sqlsure looks cool. I think this is a genuinely interesting idea. It's a nice way to keep AI-generated SQL aligned with the semantics the MDL actually declares. That said, I think a lot of it comes down to the quality of the semantic layer (MDL) itself. With a well-built semantic layer, this kind of enforcement can help the AI generate correct SQL more efficiently. But if the semantic layer isn't high-quality, hints that are too strong could backfire and push generation in the wrong direction. For sqlsure's positioning, though, I think it's completely reasonable to treat the semantic layer as the single source of truth — after all, the quality of the semantic layer is something the user is responsible for guaranteeing. And I think continuing to expand the coverage of the analysis (calculated columns, views, nested relationships, etc.) would be really valuable. Thanks for sharing 👍 |
Uh oh!
There was an error while loading. Please reload this page.
Hi — I built an open-source enforcement layer that consumes MDL. WrenAI's context layer declares semantics (primaryKey, relationship joinType + condition, cube measures); sqlsure (Apache-2.0) makes those declarations enforceable: it validates any SQL — agent-generated or human — against them before execution, and rejects violations with machine-actionable fix hints.
I wrote an MDL loader (
primaryKey→ grain,joinType+condition→ join edges, cube measure expressions → additivity) and ran it against themdl.jsonshipped inexamples/v5-jaffle:COUNT(*)after the declared one-to-many customers→orders join → warning (inflated customer counts), with theCOUNT(DISTINCT)fixDeterministic, ~0.1 ms per check, no LLM calls, never reads data. The check itself: parse the SQL, look up the joins/aggregates against the declared facts, return
{approved, violations[{rule, severity, message, fix}]}— so an agent can self-repair and re-check before executing.Loader: https://github.com/sqlsure/sqlsure/blob/main/integrations/mdl_loader.py
Would a verification step like this be interesting as a skill in the agent loop? Happy to adapt the loader to whatever MDL corners it does not cover yet — the v5-jaffle example is small, so I am sure there are shapes (calculated columns, views, nested relationships) it has not met.
All reactions