//Why the naive version fails
Dump 300 tables into a prompt and ask for SQL, and you get confident nonsense — invented columns, wrong joins, cross-schema leakage. The model isn't the bottleneck; context selection is.
//Schema retrieval first
Each question first retrieves only the handful of relevant tables from an indexed schema store. The model sees a focused, correct slice of the warehouse — not all 300 tables — which is what makes the joins right.
//Validate, then repair
Generated SQL is parsed, dry-run, and checked against guardrails before it ever touches data. On error, the failure is fed back for a single repair pass. Reliability comes from the loop around the model, not the model alone.
▸ key result
300+tables across 20+ schemas served reliably — the architecture, not the prompt, is what scales.