Repository navigation
fix(eval): keep the trailing SQL candidate and stop cutting queries at a quoted semicolon - #135
ddaihao2022 wants to merge 1 commit into
Conversation
…t a quoted semicolon find_sql() returned the first 10 candidates. A model that reasons before answering starts its prose with words like "select", so those prose candidates filled every slot and the real query at the end was never evaluated. It also ended a query at the first ";", even inside a string literal or a quoted identifier, turning WHERE b = 'x;y'; into WHERE b = 'x'. - prefer the contents of a ```sql block when the model writes one - otherwise keep the last N candidates instead of the first N - find the end of a query with a scanner that skips quoted runs, so a ";" inside a literal (including a doubled-quote escape) is not a terminator; the fence is checked before the quotes so ``` still ends a query - document the any-of-N scoring rule in the evaluation docs Closes LLMSQL#126
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
There was a problem hiding this comment.
Hi @ddaihao2022, thank you for your contribution!
One small regression before we merge: the old pattern required a word boundary after the keyword (SELECT\b), but the new _SELECT = re.compile(r"(?i)(SELECT)") does not. So ordinary words now start candidates:
find_sql('I selected the Price column.\nSELECT "Price" FROM "Table"')
# main: ['SELECT "Price" FROM "Table"']
# PR: ['selected the Price column.\nSELECT "Price" FROM "Table"', 'SELECT "Price" FROM "Table"']These junk candidates fail to execute (inflating sql_errors) and, more importantly, use up the 10 candidate slots in long reasoning outputs. Please change it to:
_SELECT = re.compile(r"(?i)(SELECT)\b")and add a test for "selected" / "Selection" not producing candidates.
For context, we re-scored all 13 leaderboard models' full outputs (80,330 each) with your branch: accuracy goes up by +0.02 to +0.54 pp (most for models that reason in prose, like Nemotron-3-Nano and Qwen3-0.6B). With the \b fix, items flipping right→wrong drop from 223 to 179 and wrong→right rise from 1,294 to 1,297, so the fix is a clear improvement.
If you find LLMSQL useful, we'd be grateful if you could give the project a ⭐ on GitHub, it really helps us!
Summary
find_sql()(llmsql/utils/regex_extractor.py) had two extraction bugs described in #126.SELECTstarts a candidate, so a model that explains itself before answering ("I need to select the right column...") filled all 10 slots with prose and its actual query was never evaluated.;ended the query even inside quotes.SELECT a FROM "Table" WHERE b = 'x;y';was truncated toSELECT a FROM "Table" WHERE b = 'x.This pull request prefers the contents of a
```sqlblock when the model writes one, otherwise keeps the last N candidates, and replaces the.*?(?=(?:;|\n{4,}|```|$))lookahead with a small scanner that skips quoted runs. It also documents the any-of-N scoring rule in the evaluation docs.Closes #126
Changes
llmsql/utils/regex_extractor.py_fenced_sql_blocks(): collect the contents of```sqlblocks, dropping the terminating;so fenced queries look the same as scanned ones._query_end(): walk forward fromSELECTand stop at a;, a run of four or more newlines, a fence, or the end of the text. Quoted runs are skipped through_skip_quoted(), which also understands the doubled-quote escape ('it''s'). The fence is tested before the quote check so```still terminates a query instead of being read as a backtick identifier.find_sql(): prefer fenced blocks, otherwise scan, then returnresults[-limit:]instead ofresults[:limit].limit <= 0still returns[].tests/utils/test_regex_extractor.py: three new classes covering a;inside a literal / escaped literal / quoted identifier / backtick identifier, a query that follows 12 lines of reasoning prose, fence preference, and the fallback when a fence is untagged or empty.docs/docs/evaluation.rst: new "How a Prediction Is Scored" section stating that a sample counts as correct when any of the extracted candidates matches gold, and which candidates are kept.No change to the scoring itself: the evaluator still loops over every candidate and marks a match when any result equals gold.
Verification
PYTHONPATH=. pytest tests/utils/test_regex_extractor.py -q→ 32 passed (20 pre-existing + 12 new).llmsql/utils/regex_extractor.pystashed, 9 of the new tests fail, so they pin both bugs rather than describing the current behaviour.PYTHONPATH=. pytest tests/utils/test_evaluation_utils.py tests/utils/test_regex_extractor.py tests/utils/test_rich_utils.py tests/utils/test_utils.py tests/test_init.py -q→ 91 passed, 1 skipped, 3 failed. The 3 failures aretests/test_init.pylazy-import tests that needtorch, which is not installed on this machine; they are unrelated to this change and fail identically on an unmodified checkout.ruff check llmsql/utils/regex_extractor.py tests/utils/test_regex_extractor.py→ All checks passed.mypy llmsql/utils/regex_extractor.py→ Success: no issues found in 1 source file.The rest of the suite (inference tests,
tests/cli, integration) needstorch/vllmand a GPU, so it was not run here.Checklist
torch/vllm— not run, no GPU on this machine