Skip to content

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

Closed

Description

@EVWorth

What

CALL on any procedure that produces a result set panics the executor:

thread panicked at crates/mas-core/src/query/executor.rs:268:47:
index out of bounds: the len is 1 but the index is 1

Not a corner case. Reproduced against MySQL 8 through QueryExecutor::execute:

Statement Procedure body Result
CALL p_one() SELECT 'only' AS a; panic — len 1, index 1
CALL p_none() SET @noop = 1; ok, 1 set
SET @p_out = NULL; CALL p_two(@p_out); SELECT @p_out; two SELECTs + SET panic — len 3, index 3

So a single CALL to a procedure containing one SELECT — the ordinary case — crashes. The routine viewer's Execute button builds exactly that batch, and typing CALL x() in the editor does too.

Seed used:

CREATE PROCEDURE p_one() BEGIN SELECT 'only' AS a; END
CREATE PROCEDURE p_two(OUT p_out INT)
BEGIN SELECT 'hello' AS greeting; SELECT 'second' AS s; SET p_out = 42; END

Cause

The fetch loop assumes one Either::Left per statement, as its own comment says:

Either::Left(qr) => {
    // Statement complete. For SELECT this arrives after all rows;
    // for DML/DDL it is the only item for that statement.
    if stmt_idx >= 0 {
        let idx = stmt_idx as usize;
        let stmt = &statements[idx];   // <-- panics

A CALL breaks that. MySQL sends one result set per SELECT inside the procedure plus a final OK packet, so N+1 Lefts arrive for one statement. stmt_idx runs past the end of statements and the index panics.

A second defect in the same place

returns_rows decides result-set-versus-count from the statement verb, and its list is SELECT, SHOW, DESCRIBE, DESC, EXPLAIN, ANALYZE, TABLE, VALUES. CALL is not in it. Even without the panic, a procedure's rows would be discarded and reported as a rows-affected count.

Deriving this from the verb cannot work for CALL: whether it returns rows depends on the procedure body, not the statement text.

Suggested fix

Two changes at the same site:

  1. Do not index statements unconditionally. When idx is past the end, the Left belongs to a statement that already reported — attribute it to the last statement rather than panicking.
  2. Treat a Left as a result set when returns_rows(stmt) or rows actually accumulated since the last one. That covers CALL, and keeps a zero-row SELECT reporting as an empty result set rather than flipping to a count.

Baseline to preserve, both confirmed working today:

  • SELECT 1 AS n ... WHERE 1=0 → 1 set, no columns
  • SELECT 1 AS a; SELECT 2 AS b → 2 sets, statement_index 0 and 1

Relationship to other issues

This is upstream of #396, which describes OUT-param values being read from the wrong result set. That code cannot be reached today — executing any procedure with a SELECT in it crashes first. #396 should be re-examined once this is fixed.

Found while verifying #396's premise.

Activity

  1. added
    severity/p0
    kind/bug
    area/routine
    on Sep 5, 2026
  2. added a commit that references this issue on Sep 5, 2026
    4e22b38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/routinekind/bugseverity/p0

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions