Skip to content

Exception message for multiple statements in single query#83

Merged
ea-rus merged 4 commits intomainfrom
multi-statement
Feb 12, 2026
Merged

Exception message for multiple statements in single query#83
ea-rus merged 4 commits intomainfrom
multi-statement

Conversation

@ea-rus
Copy link
Collaborator

@ea-rus ea-rus commented Feb 11, 2026

If string contain several statement it returns message about it:

-- Step 1: Identify all customers in the 'BUILDING' segment
SELECT c_custkey, c_name FROM tpch_pg_conn.customer WHERE c_mktsegment = 'BUILDING';
-- Step 2: Link these customers to their orders
-- (Checking if any supplier or part reference exists in orders table)
SELECT * FROM tpch_pg_conn.orders LIMIT 5;

response will be

>  SELECT c_custkey, c_name FROM tpch_pg_conn.customer WHERE c_mktsegment = 'BUILDING';
--------------------------------------------------------------------------------------^
Only a single sql statement is expected. Got multiple instead

Also queries with leading comments used to raise before, fixed in this PR

select 1; -- my query

Fixes: https://linear.app/mindsdb/issue/FQE-2076/agents-agent-streaming-failed-problem-with-final-query-syntax-error

@ea-rus ea-rus requested a review from StpMax February 11, 2026 12:26
@github-actions
Copy link

github-actions bot commented Feb 11, 2026

Coverage

Coverage Report
FileStmtsMissCoverMissing
mindsdb_sql_parser
   __about__.py10100%1–10
   __init__.py1322283%49, 53, 58, 103, 120, 144–163, 170–171
   lexer.py2872193%378, 380, 382, 394, 396, 398, 404–422
   logger.py19479%14, 17, 23, 26
   parser.py11292997%130, 134, 306, 331, 437, 624, 641, 665–666, 895, 949, 1026, 1186, 1196, 1265, 1276, 1359, 1435, 1474, 1510, 1707–1708, 1887–1888, 2064, 2072, 2125–2128
   utils.py1001288%73–79, 124, 140, 142, 144, 150–153, 161
mindsdb_sql_parser/ast
   base.py36586%13, 28, 31, 46, 51
   create.py82989%7–8, 23–31, 95
   drop.py52296%10, 13
   insert.py63494%39–41, 46
   show.py48198%18
   update.py53591%40–42, 75–76
mindsdb_sql_parser/ast/mindsdb
   knowledge_base.py127398%79, 118, 128
mindsdb_sql_parser/ast/select
   case.py37197%22
   constant.py36197%23
   data.py11464%10–12, 15, 19
   identifier.py731185%47, 94–102, 112
   native_query.py13192%25
   operation.py139497%57, 66, 178, 202
   parameter.py15193%20
   select.py108397%160–165
   star.py12283%8–9
TOTAL350515596% 

Tests Skipped Failures Errors Time
329 0 💤 0 ❌ 0 🔥 13.925s ⏱️

Copy link
Collaborator

@StpMax StpMax left a comment

Choose a reason for hiding this comment

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

fail in new version:

sql = 'select "--x"'
ast = parse_sql(sql)

sql = "select '--x'"
ast = parse_sql(sql)

@ea-rus
Copy link
Collaborator Author

ea-rus commented Feb 12, 2026

updated code, the trialed semicolon is omitted in different way (as a token after lexer parsed input string)

@ea-rus ea-rus requested a review from StpMax February 12, 2026 09:10

# remove ending semicolon and spaces
sql = re.sub(r'[\s;]+$', '', sql)
def semicolon_checker(generator):

Choose a reason for hiding this comment

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

Duplicate Code: ⚠️ Duplicate Code Detected (Similarity: 85%)

This function semicolon_checker duplicates existing code.

📍 Original Location:

mindsdb_sql_parser/__init__.py:169-187 (specifically line 175)

Function: parse_sql (semicolon removal logic)

💡 Recommendation:
Keep the existing regex approach as it's more concise (1 line vs 10 lines), more performant (no token iteration overhead), and easier to maintain. Unless there's a specific requirement for token-level semicolon handling (e.g., distinguishing between statement-separating semicolons vs trailing semicolons), the simpler regex approach should be preferred.

Consider importing and reusing the existing function instead of duplicating the logic.


assert "Only a single sql statement is expected" in str(excinfo.value)

def test_trailing_semicolon(self):

Choose a reason for hiding this comment

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

Duplicate Code: ⚠️ Duplicate Code Detected (Similarity: 95%)

This function test_trailing_semicolon duplicates existing code.

📍 Original Location:

tests/test_base_sql/test_base_sql.py:8-13

Function: test_ending

💡 Recommendation:
Consolidate into a single comprehensive test. Either enhance the existing test_ending to also validate AST structure, or replace it with the PR's test_trailing_semicolon which has a clearer name. Do NOT keep both tests as they provide redundant coverage.

Consider importing and reusing the existing function instead of duplicating the logic.

@ea-rus ea-rus merged commit 4f727e4 into main Feb 12, 2026
12 checks passed
@ea-rus ea-rus deleted the multi-statement branch February 12, 2026 13:34
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.

2 participants