Prototype Text-to-SQL demos look effortless until they touch real multi-tenant enterprise schemas. Unconstrained LLM prompts routinely hallucinate column names, drop WHERE tenant filters, multiply revenue metrics across 1:N:M joins, or allow DDL/DML injection.
1. Decouple probabilistic intent from deterministic SQL
In InvarData, frontier models never touch SQL syntax directly. Tier A maps the user question to a strictly typed Pydantic SMQ v2 Abstract Syntax Tree grounded against catalog metadata. Tier B compiles that AST deterministically into ANSI/PostgreSQL 17 SQL (18ms kernel translation, 86.9ms P95 core latency)—guaranteeing repeatable math and zero prompt-injected SQL statements.
| Architecture Approach | Execution Accuracy | Latency Profile | Tenant & Injection Safety |
|---|---|---|---|
| Raw LLM Direct Prompt | 68.4% (42.5% OOS FAR) | >2,400ms | Vulnerable to SQLi & chasm traps |
| Multi-Agent Retry Loops | 81.2% | ~14,200ms | Probabilistic filters; high token cost |
| InvarData Two-Tier Compiler | 100.0% core / 97.14% Sonnet E2E | 86.9ms core P95 | 0 leaks (1,000 probes), FORCE RLS + sqlglot AST |
2. Defense in depth: AST firewall + PostgreSQL FORCE RLS
- AST Safety Governor (sqlglot): Enforces read-only SELECT allowlists, strips DDL/DML, isolates CTE aliases, and blocks cartesian joins before execution.
- PostgreSQL 17 FORCE ROW LEVEL SECURITY: Enforces tenant boundaries at the storage engine level with parameterized $1..$n bindings—even if upstream application code misses a filter.
- Tamper-evident auditability: RFC-8785 canonical JSON SHA-256 hash chains with 64-bit advisory locks record every compiled query and verification pass.
3. VPC deployment vs. hosted SaaS APIs
Compliance-heavy teams rarely allow warehouse rows to leave their Virtual Private Cloud. A stateless 5-stage pipeline executes schema parsing, AST validation, and warehouse dispatch inside the customer VPC, sharing only semantic definitions—never raw customer records—with the LLM layer.