The promise of AI-generated SQL has hit a wall: while writing code became cheap, reviewing it did not. A new approach detailed by SPLWare in their SQLazy project argues that direct natural-language-to-SQL generation, which typically achieves only 60% accuracy for complex queries, fails in serious production settings. The core issue is that a model cannot flag its own 10% error rate, forcing humans to review every line anyway. SQLazy proposes a radical shift in division of labor: demote the AI from generator to planner, and hand final code generation to a deterministic compiler.
The Auditability Crisis in SQL
SQL is uniquely difficult to audit because its evaluation order (innermost to outermost) is the opposite of human reading order (top to bottom). This mismatch creates three major problems: hard-to-read nested logic, impossible intermediate debugging, and a lack of testability for one-off analyses. When AI generates a 300-line query, the result may run, but the logic is a black box. If a metric definition drifts subtly, the numbers look plausible, but the business logic is wrong. The bottleneck is no longer generation speed; it is the human cognitive load required to reverse-engineer the model's output.
Deterministic Compilation Over Probabilistic Generation
SQLazy confines uncertainty to the planning layer. The AI breaks down a colloquial requirement into canonical, linear steps using business verbs like filter, sort, segment, and summarize. These steps are not final SQL; they are a specification that a non-AI compiler translates into code. Because the compiler is deterministic, the same input always yields the same output, eliminating hallucinations. If the plan is wrong, a human can spot the error in the simple step list before any code is generated. This approach ensures that reading order matches evaluation order, making logic verifiable step-by-step.
Migrating Legacy Code and Stored Procedures
The tool also addresses the maintenance liability of legacy SQL and stored procedures. Instead of regenerating unreadable code, SQLazy can reverse-translate existing nested SQL into linear step scripts. This transforms black-box code into a white-box asset that can be versioned, diffed, and unit-tested. For stored procedures, the approach embeds SQLazy fragments for complex SELECT and WHERE clauses within the host procedureβs flow control. This isolates the hardest-to-audit logic while leaving standard flow code like loops and commits in the native database syntax, balancing portability with auditability.
Key Takeaways
- AI should plan logic in linear, readable steps, while a deterministic compiler handles SQL generation to eliminate hallucinations.
- Aligning reading order with evaluation order makes SQL auditability possible, allowing humans to verify logic before code runs.
- Legacy SQL and stored procedures can be reverse-translated into auditable, version-controlled scripts, turning maintenance liabilities into reusable assets.
The Bottom Line
Stop treating AI as a code generator and start treating it as a logic planner. By handing deterministic compilation to a non-AI engine, we turn SQL from a black box into a verifiable, auditable asset.