Mind the Trap As LLMs Guess the Data Model: Grain-Aware Guards Against Fan-out in Text-to-SQL and Its Benchmarks
Share this paper
In one minute
- AI that writes database queries can silently count the same amount twice.
- Short data notes cut double counts from 18.4% to about 13%.
- Check totals before anyone sees them, and ask the user when unsure.
Words to know
- Text-to-SQL
- A model turns a plain question into a database query.
- Fan-out error
- An amount is counted several times because a join repeated its row.
- Grain
- What one value belongs to, such as one fee per order.
- Join key
- The ID that links two tables.
- Benchmark
- A shared set of test questions used to compare models.
In one line
When a model writing SQL was told what each amount belongs to and how often each linking ID repeats, its double-count rate dropped to about 13% of runs, down from 18.4%.
The paper in plain English
Text-to-SQL means a model turns a plain question into a database query. This paper studies one way that goes wrong: the double count.
Say a query joins orders to order lines. Then it sums an order-level amount. The amount repeats once for each line. The paper's example asks how much shipping was charged on orders that include a toy. The joined query returns 28. The right answer is 18. The query runs with no error, and the number looks fine.
The paper calls this a fan-out error. It traces the error to two facts that table and column names do not show. The first is the grain of each amount. The grain is what one value belongs to, such as one fee per order. The second is how many rows share each value of the ID that links two tables. That ID is the join key. You can count both facts from the data.
The author gave these facts to eight current models as short notes. Some notes went in the prompt. Others came through a tool that the model had to call on every table it read.
The test used 229 questions from BIRD. BIRD is a widely used text-to-SQL benchmark. On these questions, the original reference answer double counted. Fan-out errors fell from 18.4% of runs to 13.6% with the notes in the prompt. They fell to 13.1% with the tool.
On the 183 questions that played no part in designing the notes, errors fell from 16.1% to 12.4% and 12.2%. Accuracy rose by only about 3 points. Most of the remaining errors had other causes. A separate audit of the benchmark found something else. In BIRD's development set, 4.2% of the reference answers were distorted by fan-out.
Key ideas
The double count is silent
A sum, count or average over repeated rows raises no error. It usually returns a believable number. With no extra context, all eight models made grain errors. The double count is one kind of grain error. This happened on the paper's synthetic test set. The rate ran from 2% to 42% of runs.
Two facts the table design does not show
The model needs the grain of each amount. It also needs the number of rows each join key repeats on. You get both by counting the data. Table names do not show them. The notes also say where blank values break a rule, such as one category per product.
Name the grain, give the count
Generic labels such as "can have many" added nothing beyond simple column counts. This was on the synthetic set. Notes that name the grain and give the repeat count did better. They cut grain errors from 12.8% of runs to 8.3%. The 12.8% figure is for column counts alone. Telling models to check the data made them check more. It did not reduce errors.
Benchmark answers can be wrong too
The paper's audit used two LLM judges to label each flag. It found 4.2% of BIRD's development reference answers distorted by fan-out. BIRD's maintainers later revised the set on their own. 44% of the answers with a definite flag changed. Only 16% of those with no flag changed. A model that avoids the trap gets marked wrong against such an answer.
Numbers
- 18.4%
- of runs made a fan-out error with no extra context (229 BIRD train questions, 8 models)
- 13.6%
- of runs made a fan-out error with the grain notes in the prompt (13.1% when delivered by a tool)
- 3.0 points
- accuracy gain on the same questions with the notes in the prompt, from 60.3% to 63.3%
- 4.2%
- of reference answers in BIRD's development set are distorted by fan-out (audit flags confirmed by two LLM judges)
How to use this in your AI workflow
Give your model the same two facts the paper tested. Then check totals before anyone sees them.
Profile each table once
Tag each column as an ID, a category or an amount. For each ID, count the rows per value: lowest, highest and average. For each amount, find the broadest column that gives it exactly one value. Check the tags by hand, because one wrong tag hid a count in the paper. The first pass can be slow. The paper reports up to 27 minutes for one table in its largest databases.
Write the notes in plain lines
State what each amount belongs to. State how often each key repeats. In the paper's example, shipping_fee has one value per order_id. Each order_id sits on 1 to 6 rows, 3.5 on average. So a sum over rows counts the fee that many times. Add a line where blanks break a rule, with the number of blank rows.
Start with the prompt, then test a tool
Put the notes for the tables in play into the prompt. This worked about as well as the tool. It used up to about twice the input tokens of a run with no notes. With the tool, two of the eight models lost accuracy on questions that mean to count every row. The paper calls this a risk, not a proven harm. Test it on your own questions. Keep a line that tells the model not to guess how tables relate from their names. Every run in the paper had that line. On its own it was not enough.
Check the final query against the data
Before a number reaches a user, compare the joined row count with the distinct row count of each table. Apply the query's filters first. If rows repeat under a SUM, COUNT or AVG, flag the answer. Do not auto-fix it. The paper's audit flagged 70 to 75% of answers that reproduced a known double count. It also flagged 53% of correct answers on questions that meant to count every row. Ask the user which reading they want.
Review your hints and metric definitions
For 127 of the 229 questions, the benchmark hint spelled out the double-counting formula. With no notes, those questions drew a fan-out error in 24.1% of runs. The other questions drew one in 11.3%. Some models saw the repeats and followed the hint anyway. Check that your own metric definitions and example queries say which level to sum at.
For finance teams
Finance data is full of header and line tables. Examples are invoices and invoice lines, orders and shipments, and journals and journal lines. Some amounts are stored once per header, such as freight or a document-level discount. These repeat as soon as a query joins to the lines. Write grain notes for those amounts first. Tie AI totals back to a known control total. The paper tested public benchmark databases, not company finance systems. Treat this as a starting point.
Ask AI about this paper
Stuck on a word or an idea? Ask in plain words. The answer comes from the write-up on this page.
Limits
- The setup is easier than real work. It used SQLite only and one run per BIRD question. The models saw only the tables the reference queries read, not the whole database.
- No person labelled or checked the data. LLM judges made the column roles, corrected answers and error labels. The paper's counts made without a judge point the same way. The synthetic-set results come from the same questions the notes were designed on. The 183 untouched BIRD questions are not a random sample.
- The fix is partial. About 13% of runs still double counted. On the 183 questions outside the design, the accuracy gain was 1.8 points. That gain was not significant. With the tool version, some models risk removing repeats that should be counted.
Share this paper
One paper a week, in plain English
Every Monday: the new AI papers worth your time. Short lines, the key number, and what to do with it.
One email a week. Unsubscribe with one click. No spam.