From dd077134e38e044e1a9bbf8b81211f1dc03e041d Mon Sep 17 00:00:00 2001 From: MilindLate Date: Tue, 6 Oct 2026 22:05:25 +0530 Subject: [PATCH] fix(eval): replace fragile str.replace chain with robust regex in fix_table_name Fixes #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. --- llmsql/utils/evaluation_utils.py | 8 +++--- tests/utils/test_evaluation_utils.py | 40 ++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/llmsql/utils/evaluation_utils.py b/llmsql/utils/evaluation_utils.py index 04e253a..73ab171 100644 --- a/llmsql/utils/evaluation_utils.py +++ b/llmsql/utils/evaluation_utils.py @@ -56,9 +56,11 @@ def fix_table_name(sql: str, table_id: str) -> str: str: SQL query with the correct table name substituted. """ return ( - sql.replace("FROM 'Table'", f'FROM "{table_id}"') - .replace('FROM "Table"', f'FROM "{table_id}"') - .replace("FROM Table", f'FROM "{table_id}"') + re.sub( + r"""(?i)\bFROM\s+(?:"table"|'table'|`table`|table\b)""", + f'FROM "{table_id}"', + sql, + ) .strip() ) diff --git a/tests/utils/test_evaluation_utils.py b/tests/utils/test_evaluation_utils.py index c815cfa..4838f4b 100644 --- a/tests/utils/test_evaluation_utils.py +++ b/tests/utils/test_evaluation_utils.py @@ -165,6 +165,46 @@ def test_complex_query(self) -> None: # All Table placeholders should be replaced assert "'Table'" not in result + def test_lowercase_from_keyword(self) -> None: + """Issue #125: lowercase 'from' keyword should match.""" + result = fix_table_name('select * from "Table" where 1', "t") + assert 'FROM "t"' in result + + def test_lowercase_table_placeholder(self) -> None: + """Issue #125: FROM 'table' (lowercase name) should match.""" + assert fix_table_name('SELECT * FROM "table" WHERE 1', "t") == 'SELECT * FROM "t" WHERE 1' + + def test_backtick_quoted_table(self) -> None: + """Issue #125: FROM `Table` (backtick quotes) should match.""" + assert fix_table_name("SELECT * FROM `Table` WHERE 1", "t") == 'SELECT * FROM "t" WHERE 1' + + def test_extra_whitespace_between_from_and_table(self) -> None: + """Issue #125: extra spaces between FROM and Table should match.""" + assert fix_table_name('SELECT * FROM "Table" WHERE 1', "t") == 'SELECT * FROM "t" WHERE 1' + + def test_newline_between_from_and_table(self) -> None: + """Issue #125: newline between FROM and Table should match.""" + assert fix_table_name('SELECT *\nFROM\n"Table"\nWHERE 1', "t") == 'SELECT *\nFROM "t"\nWHERE 1' + + def test_column_named_table_not_replaced(self) -> None: + """Issue #125: a column or value containing 'Table' should not be touched.""" + sql = "SELECT * FROM \"Table\" WHERE name = 'Table'" + result = fix_table_name(sql, "t") + assert "name = 'Table'" in result + + def test_placeholder_followed_directly_by_semicolon(self) -> None: + """Issue #125: placeholder followed directly by semicolon should be replaced.""" + assert fix_table_name('SELECT a FROM "Table";', "t") == 'SELECT a FROM "t";' + + def test_placeholder_followed_directly_by_keyword(self) -> None: + """Issue #125: quoted placeholder followed directly by keyword without space should be replaced.""" + assert fix_table_name('SELECT a FROM "Table"WHERE b=1', "t") == 'SELECT a FROM "t"WHERE b=1' + + def test_table_prefix_not_replaced(self) -> None: + """Issue #125: table names like Table1 or 'Tables' starting with Table should not match.""" + assert fix_table_name('SELECT a FROM Table1', "t") == 'SELECT a FROM Table1' + assert fix_table_name('SELECT a FROM "Tables"', "t") == 'SELECT a FROM "Tables"' + class TestEvaluateSample: """Test cases for evaluate_sample function."""