Beyond the Harness: End-to-End Optimization of Context Artifacts for Enterprise Text-to-SQL
Organizations: Amazon
Abstract
Deploying LLMs for enterprise Text-to-SQL is bottlenecked less by the model than by what context reaches it: business logic spans thousands of tables, and no model can ingest a full catalog at once. We argue that the most effective place to intervene is therefore the knowledge-base context the model consumes, and that this context should be constructed from historical usage rather than tuned for as a fixed input. Using a query-DAG decomposition--the same family of intermediates that enterprise benchmarks like BEAVER annotate, here recovered from production SQL--we compare the value of oracle query graphs versus retrieved knowledge-base context. In this ablation, retrieved knowledge-base context provides the largest marginal improvement when added to the full oracle graph. Building on this, we optimize a distillation procedure that turns historical query profiles into reusable SQL reference cards. On a benchmark of 5176 production queries from a major online retailer, optimizing these context artifacts yields larger gains (-- AST similarity) than optimizing the retrieval harness (--). On the public BEAVER benchmark, which lacks the production-usage signals available in our internal setting, the picture is more mixed: table cards alone perform about the same as raw historical SQL. The best optimized variant retrieves both cards and raw SQL, scoring versus (p-value ) for the comparable baseline on a held-out subset, using retrieved context and harness changes but no agentic loop.
Figures & tables
| Similarity | ||||
| Model | Input | AST | String | Linkage |
| Claude Sonnet 4.5 | : NL | 0.100 | 0.303 | – |
| Qwen Coder 3-30B | 0.082 | 0.281 | – | |
| Claude Sonnet 4.5 | : NL + RAG | 0.278 | 0.447 | – |
| Qwen Coder 3-30B | 0.184 | 0.346 | – | |
| Claude Sonnet 4.5 | : NL + Linkage | 0.248 | 0.468 | 0.735 |
| Table Selection | End-to-End | Exec. | ||||||
| Model | Harness | Context | Recall | Prec. | AST | String | LLM-J | EX |
| Claude Sonnet 4.6 | Baseline | Baseline | 0.714 | 0.658 | 0.490 | 0.591 | 0.553 | 0.255 |
| Optimized | Baseline | 0.741 | 0.679 | 0.503 | 0.600 | 0.546 | 0.275 | |
| Baseline | Optimized | 0.805 | 0.777 | 0.550 | 0.639 | 0.597 | 0.333 | |
| Optimized | Optimized | 0.766 | 0.717 | 0.570 | 0.655 | 0.600 | 0.304 | |
| Qwen Coder 3-30B | Baseline | Baseline | 0.660 | 0.577 | 0.407 | 0.508 | 0.503 | 0.176 |
| Method | Context injected | Tools / calls | Exec. acc. | 95% CI |
| Few-shot (ours) | schemas + demos | – | 6.33% | [4.1, 9.7] |
| Few-shot † | schemas + demos | – | 8.8% | – |
| ReFoRCE † | self-explored schema | explore, vote, fix | 10.8% | – |
| Raw queries (Optimized) | retrieved gold SQL | – | 6.33% | [4.1, 9.7] |
| Table cards (Optimized) | aggregated per-table | – | 6.67% | [4.4, 10.1] |
| Both (Optimized) | cards + raw queries | – | 9.00% | [6.3, 12.8] |
Appendix figures & tables4 assets
Supplementary material from the paper’s appendix.
Appendix
| Table Selection | End-to-End | Exec. | ||||||
| Model | Harness | Context | Recall | Prec. | AST | String | LLM-J | EX |
| Claude Sonnet 4.6 | Baseline | Baseline | [0.682, 0.745] | [0.628, 0.689] | [0.461, 0.520] | [0.564, 0.618] | [0.525, 0.580] | [0.180, 0.347] |
| Optimized | Baseline | [0.712, 0.770] | [0.650, 0.709] | [0.473, 0.533] | [0.574, 0.628] | [0.518, 0.574] | [0.197, 0.368] | |
| Baseline | Optimized | [0.778, 0.830] | [0.749, 0.802] | [0.521, 0.579] | [0.613, 0.665] | [0.570, 0.625] | [0.249, 0.429] | |
| Optimized | Optimized | [0.738, 0.793] | [0.689, 0.745] | [0.540, 0.599] | [0.627, 0.681] | [0.571, 0.629] | [0.223, 0.399] | |
| Qwen Coder 3-30B | Baseline | Baseline | [0.628, 0.690] | [0.545, 0.607] | [0.381, 0.435] | [0.484, 0.534] | [0.478, 0.531] | [0.115, 0.262] |
| Query structure | Exec. acc. |
| Single-level ( base ) | |
| Deeply compositional (nested-CTE) |
Text-to-SQL
Text-to-SQL translates natural-language questions into executable database queries, linking user intent to schemas, values, and SQL logic.
See all 173 papers