Reliable Answers for Recurring Questions: Boosting Text-to-SQL Accuracy with Template Constrained Decoding
The paper introduces TeCoD, a system that improves Text-to-SQL accuracy and reduces latency by leveraging recurring query patterns to select reusable templates and enforce them via grammar-constrained decoding.
Original paper licensed under CC BY 4.0 (http://creativecommons.org/licenses/by/4.0/). This is an AI-generated explanation of the paper below. It is not written or endorsed by the authors. For technical accuracy, refer to the original paper. Read full disclaimer
The Big Problem: The "Overconfident Intern"
Imagine you have a very smart but inexperienced intern (a Large Language Model or LLM) whose job is to translate your English questions into a specific computer language called SQL, which databases understand.
When you ask a simple question like "Show me sales from last week," the intern usually does a great job. But when you ask a complex or slightly unusual question, the intern starts to guess. Sometimes the guess is wrong, and the computer crashes or gives the wrong answer. This is especially bad for big companies (enterprises) that have their own private databases that the intern has never seen before.
The current best trick to help the intern is In-Context Learning (ICL). This is like giving the intern a cheat sheet with a few examples of past questions and their correct answers right before they answer a new question. It helps, but the paper shows that even with this cheat sheet, the intern still makes mistakes on about 30-40% of the questions.
The Solution: The "Template Library" (TeCoD)
The authors, Smit Jivani, Sarvam Maheshwari, and Sunita Sarawagi, noticed something important about how people ask questions in real life: We repeat ourselves.
If you work at a bank, you might ask, "How many customers in London have over $5,000?" today. Next week, you might ask, "How many customers in Paris have over $10,000?" The words change, but the structure of the question is identical.
The team built a system called TeCoD (Template Constrained Decoding) that acts like a Master Recipe Book for these recurring questions.
How TeCoD Works (The Three Steps)
1. The Recipe Extraction (Template Creation)
Instead of saving every single question and answer exactly as they are, TeCoD looks at past correct answers and strips out the specific details (like "London" or "$5,000"), leaving behind a "skeleton" or template.
- Analogy: Imagine a cookie cutter. The specific shape of the cookie (the numbers and names) changes, but the cutter (the template) stays the same. TeCoD saves the cutter.
2. The Matchmaker (Template Selection)
When a new question comes in, TeCoD doesn't just guess. It runs a special "Matchmaker" (a smart AI model) to see if the new question fits any of the saved cookie cutters.
- The Innovation: The paper says simple math (comparing word similarity) isn't good enough. So, they trained their Matchmaker to act like a logic detective. It asks: "Does this new question logically fit the pattern of this old recipe?"
- Result: This step is incredibly accurate (91%), ensuring they only use a template if it's a perfect fit.
3. The Strict Chef (Constrained Decoding)
If a match is found, TeCoD doesn't let the intern guess the answer freely. Instead, it forces the intern to fill in the blanks of the recipe using a strict grammar rule.
- Analogy: Imagine you are filling out a form. You can't write whatever you want; you must follow the lines and the specific type of ink allowed. TeCoD forces the AI to only generate the specific words that fit the "cookie cutter" shape, preventing it from making up weird SQL code that doesn't work.
Why This is a Big Deal
1. It's Much More Accurate
The paper tested this on standard benchmarks (BIRD and Spider).
- Old Way (Cheat Sheet only): The intern got about 60% of the recurring questions right.
- TeCoD Way: The system got nearly 90% right.
- Metaphor: It's like upgrading from a student guessing on a test to a chef following a proven recipe.
2. It's Faster
Because the system knows the structure of the answer in advance, it doesn't have to "think" about the whole sentence from scratch. It only has to fill in the blanks.
- Result: The system is 2.2 times faster for these matching questions.
3. It Doesn't Need to "Re-Learn" Everything
Usually, to get better at a specific company's database, you have to retrain the AI (fine-tuning), which is expensive and slow. TeCoD doesn't need that. It just adds new "recipes" to its library as it sees them. It's like adding a new page to a cookbook rather than rewriting the whole book.
What Happens When There Is No Match?
What if you ask a brand new question that doesn't fit any recipe?
- TeCoD has a safety net. If the Matchmaker says, "No, this doesn't fit any of our recipes," the system simply falls back to the standard method (the cheat sheet/In-Context Learning). It doesn't break; it just reverts to the slower, less accurate method for that specific question.
The Bottom Line
The paper argues that for real-world businesses, most questions are just variations of questions asked before. TeCoD exploits this by turning past successes into rigid templates. By forcing the AI to stick to these templates when possible, they get near-perfect accuracy and faster speeds without needing to retrain the AI model.
Key Takeaway: Don't let the AI guess the whole answer every time. If you've seen the question before, give it a template to fill in, and it will be much more reliable.
Drowning in papers in your field?
Get daily digests of the most novel papers matching your research keywords — with technical summaries, in your language.