Skip to content

fix(query): stop assuming one result set per statement, which CALL breaks - #545

Merged
EVWorth merged 1 commit into
mainfrom
fix/executor-call-resultsets
Sep 5, 2026
Merged

EVWorth merged 1 commit into
mainfrom
fix/executor-call-resultsets

Conversation

@EVWorth

@EVWorth EVWorth commented Sep 5, 2026

Copy link
Copy Markdown
Owner

Closes #544.

The crash

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 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. 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 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 a SELECT that 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 final SELECT matches nothing looks finished one marker early. Result data and ordering are unaffected — only statement_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:

test_call_with_one_result_set_mysql ....................... FAILED (panic)
test_call_with_one_result_set_mariadb ..................... FAILED (panic)
test_procedure_batch_attributes_every_set_to_its_statement  FAILED (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:

Baseline Expected
SELECT 1 AS n ... WHERE 1=0 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 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_orders would 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 @out genuinely is the last result set.

🤖 Generated with Claude Code

…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]>
@EVWorth
EVWorth merged commit 4e22b38 into main Sep 5, 2026
10 checks passed
@EVWorth
EVWorth deleted the fix/executor-call-resultsets branch September 5, 2026 20:03
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.

[P0][bug] Any CALL to a procedure that returns rows panics the query executor

1 participant