Repository navigation
fix(query): stop assuming one result set per statement, which CALL breaks - #545
Merged
Merged
Conversation
…eaks Closes #544. Calling any procedure that returns rows panicked: index out of bounds: the len is 1 but the index is 1 crates/mas-core/src/query/executor.rs:268 Not a corner case. `CALL p()` where p contains a single SELECT — the ordinary case — crashed, on MySQL 8 and MariaDB 11 alike. That is the routine viewer's Execute button, and anyone typing CALL in the editor. The fetch loop counted completion markers and used the count to index the list of statements it had sent, as its own comment described: "For SELECT this arrives after all rows; for DML/DDL it is the only item for that statement." A CALL does not fit. The server sends one result set per SELECT in the procedure body and then an OK packet closing the call, so N+1 markers arrive for one statement, and the index ran off the end. Three changes at that site. Past the end, the marker belongs to a statement that has already reported. It is attributed there instead of indexing out of bounds — the panic is unreachable now regardless of what else is wrong. Whether a marker carries a result set no longer comes from the verb alone. CALL is absent from the row-returning list and could not usefully be added: whether it returns rows depends on the procedure body, not the statement text, so a procedure's rows were being discarded and reported as a count. Rows that actually arrived settle it, with the verb still consulted so a SELECT that matched nothing stays an empty result set rather than turning into a rows-affected report. A CALL is not finished until a marker arrives carrying no rows. Advancing on each of its result sets attributed the second and later ones to whatever statement followed the CALL. The protocol's "more results" flag would settle this exactly, but sqlx does not surface it through fetch_many, so rows-or-not is the available signal. It is wrong in one case: a procedure whose final SELECT matches nothing looks finished one marker early. Result data and ordering are unaffected; only statement_index, which labels the result tabs, drifts. Said plainly in a comment at the site. Verified against both servers. Three of the six new integration tests fail against the previous executor, all three with that panic; the other three are the baselines this change had to preserve and pass either way: SELECT matching nothing -> still one empty result set SELECT 1 AS a; SELECT 2 AS b -> statement_index 0 and 1 CALL with no result set -> still a rows-affected count The seed gains three procedures covering one result set, two, and none. The existing get_user_orders would have panicked too — the fixtures could always have caught this. Found while checking #396's premise, which cannot be reached until this is fixed. Co-Authored-By: Claude Opus 5 <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #544.
The crash
Calling any procedure that returns rows panicked:
Not a corner case.
CALL p()wherepcontains a singleSELECT— the ordinary case — crashed, on MySQL 8 and MariaDB 11 alike. That is the routine viewer's Execute button, and anyone typingCALLin the editor.The fetch loop counted completion markers and used the count to index the list of statements it had sent, as its own comment described: "For SELECT this arrives after all rows; for DML/DDL it is the only item for that statement." A
CALLdoes not fit — the server sends one result set perSELECTin the body and then an OK packet closing the call, so N+1 markers arrive for one statement.Three changes at that site
Past the end, attribute rather than index. A marker beyond the statement list belongs to a statement that has already reported. The panic is unreachable now regardless of what else turns out to be wrong.
Result-set-or-count no longer comes from the verb alone.
CALLis absent from the row-returning list and could not usefully be added — whether it returns rows depends on the procedure body, not the statement text. So even without the panic, a procedure's rows were being discarded and reported as a count. Rows that actually arrived settle it, with the verb still consulted so aSELECTthat matched nothing stays an empty result set instead of flipping to a rows-affected report.A CALL is not finished until a marker arrives carrying no rows. Advancing on each of its result sets attributed the second and later ones to whatever statement followed the
CALL.One honest limitation
That last rule uses rows-or-not because the protocol's "more results" flag would settle it exactly and sqlx does not surface it through
fetch_many. It is wrong in one case: a procedure whose finalSELECTmatches nothing looks finished one marker early. Result data and ordering are unaffected — onlystatement_index, which labels the result tabs, drifts. It's stated in a comment at the site rather than left for someone to discover.Verified against both servers
Six new integration tests. Three fail against the previous executor, all three with that panic:
The other three are the baselines this change had to preserve, captured from live runs before touching anything. They pass either way, and are here to catch a regression rather than to claim a catch:
SELECT 1 AS n ... WHERE 1=0SELECT 1 AS a; SELECT 2 AS bstatement_index0 and 1The routine-viewer batch now comes back correctly attributed:
SET→0,greeting→1,s→1,p_out→2, with the closing OK producing nothing instead of a phantom empty tab.Full run: 44 integration tests against live MySQL/MariaDB, 58 lib tests, clippy/fmt/dprint clean, 1718 frontend tests untouched and green.
Fixtures
The seed gains three procedures covering one result set, two, and none. Worth noting the existing
get_user_orderswould have panicked too — the fixtures could always have caught this.Provenance
Found while checking #396's premise. #396 cannot be reached until this lands, and should be re-examined afterwards — its own claim may not survive, since the appended
SELECT @outgenuinely is the last result set.🤖 Generated with Claude Code