r/SQL • • 2d ago

Discussion diagnosing wrong SQL by testing one edit mutations on the data (and the fan out case that shrinks the result)

i'm working on an automated SQL grader on DuckDB for a game. pass/fail grading is the straightforward part. explaining why a wrong query is wrong is getting difficult, because "93 rows, expected 13" doesn't really tell a player much.

guessing the mistake from the SQL text didn't work for me, there are too many ways to write the same thing. so the checker only uses the query to list candidate edits (each join, each aggregate). the data decides:

  • unmatched rows: flip one INNER JOIN to LEFT, or move the outer table's WHERE conditions into its ON. if that single edit makes the result match the expected rows exactly, that join is the mistake.
  • NULLs: swap COUNT(*) and COUNT(col), or AVG(x) and AVG(COALESCE(x, 0)). same exact-match test.
  • fan-out: change the data instead of the query. keep one row per join key in the repeated table, then run the learner's query and the reference on that. if they now agree, and the original result was inflated, the duplicates were it. then it shows the source rows that got repeated.

the screenshot: "users with a completed deposit and zero withdrawals, each user once". the join to deposits repeats a user once per deposit, so one user shows up 11 times. the panel names the key and the repeated rows.

the one i was stuck on until this week: fan-out that shrinks the result. one audit problem compares SUM(fills.quantity) > SUM(orders.quantity) after the join, so the order side gets inflated and the query returns fewer rows than it should. my inflation check never fired, because nothing was inflated in the output.

what ended up working: first a cheap probe per joined table, count(t.rowid) - count(distinct t.rowid), to see if that table's rows repeat at all after the join. if one does, rewrite each aggregate over it so every source row counts once (in duckdb: list_sum(map_values(map_from_entries(list(distinct row(t.rowid, x)))))), rerun, and require an exact match like the other edits. it hasn't misfired on the problems i've run it on, but that's my own test set, so take it with salt.

two things i'd like input on:

  1. is there a cleaner way to make an aggregate count each source row once after a join? SUM(DISTINCT x) is wrong when two different rows hold the same value, which is why i went through rowid. curious if other engines have something tidier.
  2. if you review SQL regularly, what common bug would completely fool a single-edit checker like this (besides compound mistakes, which already break it)?
4 Upvotes

15 comments sorted by

View all comments

3

u/UnluckyManner9152 2d ago edited 2d ago

For question 1, I'd aggregate each table down to the join key in its own CTE first, then join the totals. Nothing gets repeated before the sums happen, so you don't need SUM(DISTINCT) or rowid tricks. For your audit problem:

WITH o AS (
  SELECT order_id, SUM(quantity) AS ordered
  FROM orders
  GROUP BY order_id
),
f AS (
  SELECT order_id, SUM(quantity) AS filled
  FROM fills
  GROUP BY order_id
)
SELECT o.order_id, o.ordered, f.filled
FROM o
JOIN f ON f.order_id = o.order_id
WHERE f.filled > o.ordered;

Each side is one row per order before the join, so neither total can be inflated. Your rowid probe still works for diagnosis: it tells you the learner's query fanned out, and this is the shape you'd point them to. Try it.

2

u/Plane_Big_5912 2d ago

yyes, pre-aggregating to the key is probably the fix i'd point someone to at as well once the probe says a table repeats.

the checker doesn't make that rewrite itself because it's only allowed to change one thing in their query.

a CTE rewrite changes the structure of the whole query, so if it then matched i couldn't say the fan out was the only problem.

rewriting the aggregate in place leaves everything else as they wrote it. it's still only a match on that problem's data, so a wrong filter that happens to match there would slip through even so.

thanks for writing the full query out.

1

u/UnluckyManner9152 2d ago

Makes sense. One edit at a time is what lets you say which mistake it was. Good luck with the grader.