Repository navigation
Description
Is there an existing issue for the same bug?
- I have checked the existing issues.
Branch Name
main
Commit ID
Other Environment Information
- Hardware parameters: Apple Silicon (arm64), 10 cores
- OS type: macOS 15.5 (Darwin arm64), Go 1.27.1
- Others: single-node mo-service (LOG/TN/CN in one process) launched from etc/launch with a LOOM_TEST_PORT_OFFSET port shift; connected with the MySQL 9.7.1 client
Actual Behavior
SplitSqlByStatement splits a query at the semicolons that appear inside a MySQL executable comment (/*! ... */) when that comment itself contains more than one statement. The comment's own SQL is treated as top-level statement separators, so the input is cut through the middle of the comment and produces fragments that are not valid SQL:
Parse("/*!50000 select 1; select 2 */", 1) -> 2 statements (correct)
SplitSqlByStatement("/*!50000 select 1; select 2 */")
-> ["/*!50000 select 1", "select 2 */"] // both fragments are broken
When the frontend re-parses these fragments it reports a syntax error on the second fragment, even though the query is valid MySQL and the first statement already executed:
mysql> /*!50000 select 1; select 2 */;
1
1
ERROR 1064 (42000) at line 1: SQL parser error: You have an error in your SQL syntax; check the manual that corresponds to your MatrixOne server version for the right syntax to use. syntax error at line 1 column 11 near "/";
mysql> select 1; /*!50000 select 2; select 3 */;
1
1
2
2
ERROR 1064 (42000) at line 1: SQL parser error: ... syntax error at line 1 column 11 near "/";
The server log confirms the failing statement text is the chopped fragment select 2 */.
Expected Behavior
A ; inside an executable comment belongs to the comment's SQL, so statement splitting must not cut the comment. /*!50000 select 1; select 2 */ must execute both statements and return both result sets (MySQL 8.0 treats the whole /*! ... */ as executable SQL and returns two result sets), with no syntax error. SplitSqlByStatement should return the comment as a single fragment (or split it without leaving an un-terminated */ in a fragment).
Steps to Reproduce
-- run each as a single COM_QUERY (mysql -e sends the whole string)
/*!50000 select 1; select 2 */;
select 1; /*!50000 select 2; select 3 */;
Additional information
explodeStatement (SplitSqlByStatementWithSQLMode) records statement boundaries from the grammar's AppendTopLevelSemicolon, which is invoked for every stmt_list ';' stmt reduction - including a ; inside the executable comment, because the comment content is tokenized as normal SQL. The split code then tries to repair only the final semicolon of an executable comment:
// executableCommentEndsByFinalSemicolon: extend only a comment's final semicolon through its terminator
That heuristic only handles a single trailing semicolon (/*!50000 select 1; */ works). When the comment contains two statements the first ; is followed by non-whitespace, so it is not extended and the raw fragments become /*!50000 select 1 and select 2 */. Downstream (sqlForRecordByStatement, schedulingSQLByStatementWithSQLMode, rewrite/hint extraction and the multi-statement executor) then processes these broken fragments, producing the spurious syntax error ... near "/" at column 11 (the */ of select 2 */).
Relevant locations: pkg/sql/parsers/dialect/mysql/mysql_lexer.go (SplitSqlByStatementWithSQLMode line 567, fragment assembly lines 588-617, executableCommentEndsByFinalSemicolon line 638) and pkg/sql/parsers/dialect/mysql/mysql_sql.y:1091 (AppendTopLevelSemicolon). A fix should not treat semicolons scanned inside an executable comment as top-level boundaries, or should map every such semicolon to the comment terminator. Note the same fragment split is exposed directly by SplitSqlByStatement.
Found by Loom bug hunt (module: sql-parser; tests: unit, black-box)
Activity
fix(parser): preserve executable comments across statement splits (#3…