Skip to content

fix(eval): replace fragile str.replace chain with robust regex in fix_table_name - #133

Open
MilindLate wants to merge 1 commit into
LLMSQL:mainfrom
MilindLate:fix/fix-table-name-regex
Open

MilindLate wants to merge 1 commit into
LLMSQL:mainfrom
MilindLate:fix/fix-table-name-regex

Conversation

@MilindLate

Copy link
Copy Markdown
Contributor

Fixes #125

Problem

fix_table_name() in llmsql/utils/evaluation_utils.py used three exact str.replace calls to swap the Table placeholder:

  • FROM 'Table' → handled
  • FROM "Table" → handled
  • FROM Table → handled

All other common spellings silently failed, causing correct model queries to be counted as SQL errors:

  • from "Table" (lowercase FROM keyword)
  • FROM "table" (lowercase placeholder)
  • FROM Table`` (backtick quotes)
  • FROM "Table" (extra whitespace)
  • FROM\n"Table" (newline between FROM and table)

Fix

Replaced the three str.replace calls with a single case-insensitive regex:

re.sub(r'(?i)\bFROM\s+("table"|\'table\'|`table`|table)(?=\s|$|,|\))', f'FROM "{table_id}"', sql)

This handles all quoting styles and any whitespace (spaces or newlines) between FROM and the placeholder.

Tests Added

Added 6 new test cases in tests/utils/test_evaluation_utils.py covering all the failing variants from the issue, plus a safety check that a value/column containing the word Table is not altered.

@codecov

codecov Bot commented Oct 9, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@DzmitryPihulski DzmitryPihulski left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @MilindLate, thank you for your contribution!

One thing is still worth adding: the lookahead (?=\s|$|,|\)) no longer matches a placeholder followed directly by ; or a keyword, which the old str.replace code did handle:

fix_table_name('SELECT a FROM "Table";', "t")      # -> unchanged: 'SELECT a FROM "Table";'
fix_table_name('SELECT a FROM "Table"WHERE b=1', "t")  # -> unchanged

In the evaluation pipeline the extractor strips the trailing ; first, so we saw no impact on the leaderboard outputs, but fix_table_name is a public helper and should not regress. For the quoted forms no lookahead is needed at all (the closing quote already ends the name); only the bare table form needs a boundary. For example:

r"""(?i)\bFROM\s+(?:"table"|'table'|`table`|table\b)"""

Please add tests for the two cases above (and keep the Table1 / "Tables" cases unchanged).

If you find LLMSQL useful, we'd be grateful if you could give the project a ⭐ on GitHub, it really helps us!

…_table_name

Fixes LLMSQL#125

Use regex pattern `\bFROM\s+(?:"table"|'table'|`table`|table\b)`
to correctly replace quoted and bare table placeholders without regressing on
trailing semicolons or unspaced following keywords, while preserving
prefixes like Table1 and "Tables".

Add comprehensive test coverage in tests/utils/test_evaluation_utils.py.
@MilindLate
MilindLate force-pushed the fix/fix-table-name-regex branch from 597ec74 to dd07713 Compare October 18, 2026 18:34
@MilindLate

Copy link
Copy Markdown
Contributor Author

Hi @DzmitryPihulski, thank you for the feedback and helpful suggestion!

I've updated the regex to r"""(?i)\bFROM\s+(?:"table"|'table'|table|table\b)""" as suggested so quoted placeholders match directly regardless of delimiters, while bare table requires a word boundary.

I've also rebased onto latest main and added tests covering:

  • Placeholder followed directly by a semicolon (SELECT a FROM "Table";)
  • Placeholder followed directly by a keyword (SELECT a FROM "Table"WHERE b=1)
  • Table prefix names like Table1 and "Tables" remaining untouched

(And starred the repo! ⭐)

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Evaluation] fix_table_name misses common spellings of "Table", so correct queries count as SQL errors

2 participants