Skip to content

[Bug]: PREPARE of EXPLAIN ANALYZE returns an internal error #26602

Closed

Description

@Ariznawlll

Description

PREPARE / EXECUTE of EXPLAIN ANALYZE returns internal error: invalid bytes based slice on current main. With the client instructed to continue after the first error, the same session then receives a second internal error and is disconnected. This is not tied to MATCH, vector indexes, or parameter binding: the smallest reproduction is a literal predicate over an ordinary two-column table.

Environment

  • Branch: main
  • Commit: 59517279b80b9e260feebb41f2a5105da5941b50
  • Deployment: locally built two-CN MatrixOne instance; reproduction executed through the MySQL text protocol against one CN.
  • Date: 2026-08-03

Steps to reproduce

CREATE DATABASE IF NOT EXISTS prepared_explain_analyze_repro;
USE prepared_explain_analyze_repro;

CREATE TABLE t (
  id INT PRIMARY KEY,
  v INT NOT NULL
);
INSERT INTO t VALUES (1, 7), (2, 8), (3, 7);

PREPARE p FROM
  'EXPLAIN ANALYZE SELECT id FROM t WHERE v = 7 ORDER BY id';
EXECUTE p;
DEALLOCATE PREPARE p;

The parameterized form fails identically:

PREPARE p FROM
  'EXPLAIN ANALYZE SELECT id FROM t WHERE v = ? ORDER BY id';
SET @v = 7;
EXECUTE p USING @v;

Actual behavior

EXECUTE returns:

ERROR 1105 (HY000): internal error: invalid bytes based slice

When the client continues in the same session, a following SELECT returns a second internal error: invalid bytes based slice, and the next statement reports Lost connection to MySQL server during query.

The literal and parameterized minimal reproducers each failed 3/3 times. The CN process remained available: a new connection could execute SELECT 1 and an independent indexed fulltext query after every failed run.

Expected behavior

EXECUTE should return the single text column containing the EXPLAIN ANALYZE output, as direct EXPLAIN ANALYZE does. An execution error must not corrupt the session protocol state or disconnect the client.

Stability and controls

  • Literal prepared EXPLAIN ANALYZE: fails 3/3.
  • Parameterized prepared EXPLAIN ANALYZE: fails 3/3.
  • Direct EXPLAIN ANALYZE SELECT ...: succeeds 3/3 and returns the expected plan text.
  • Prepared ordinary SELECT ... WHERE v = ?: succeeds 3/3 and returns 1,3.
  • Prepared EXPLAIN without ANALYZE, including a parameterized fulltext query: succeeds 3/3.
  • Post-failure new-session health and indexed-query controls: succeed after every failing run.

Evidence

The minimal scripts and the three raw client outputs for each branch are preserved in the test artifacts as prepared-explain-analyze-literal.sql, prepared-explain-normal-param.sql, prepared-explain-ft-param-explain.sql, and prepared-explain-analyze-session-state.sql.

Code analysis

The behavior is consistent with a result-metadata mismatch in pkg/frontend/result_row_stmt.go. getPreparedResultColumns special-cases only *tree.ExplainStmt and declares one varchar result column, but does not handle *tree.ExplainAnalyze (lines 54-66 at the tested commit). The direct ExplainAnalyze execution path constructs an explain-text result. The observed invalid bytes based slice is raised while serializing a value through the bytes-oriented result path (pkg/frontend/output.go:672). This is a code-level hypothesis; the SQL reproduction above is independent of this analysis.

Regression coverage

Add deterministic frontend/SQL coverage for both literal and parameterized PREPARE ... FROM 'EXPLAIN ANALYZE SELECT ...', asserting one text result column, successful execution, and a usable same session afterwards. The existing prepared-EXPLAIN coverage introduced by #26046 appears to cover EXPLAIN but not EXPLAIN ANALYZE.

Related

Activity

  1. added this to the MO-202608 milestone on Aug 3, 2026
  2. added
    kind/bug
    on Aug 3, 2026
  3. added theissue type on Aug 3, 2026
  4. added
    severity/s0
    and removed on Aug 3, 2026
  5. Ariznawlll commented on Aug 4, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:main
    • commit:c726a6199094184a2e6ebc7504c3e011982b8976
    • 部署方式:本地隔离单节点 LOG / TN / CN 集群;SQL PREPARE/EXECUTE 使用 mysql text protocol,协议用例使用 Go MySQL driver。
    • 测试日期:2026-08-04

    原问题验证

    创建 t(id int primary key, v int) 并写入 (1,7),(2,8),(3,7) 后,直接执行 EXPLAIN ANALYZE SELECT id FROM t WHERE v=7 ORDER BY id 可返回单列文本计划;但执行:

    PREPARE p FROM 'EXPLAIN ANALYZE SELECT id FROM t WHERE v=7 ORDER BY id';
    EXECUTE p;

    在 3 个新连接中均返回 ERROR 1105 (HY000): internal error: invalid bytes based slice。这正是 issue 原始 SQL-level PREPARE/EXECUTE 路径。

    Happy Path 覆盖

    1. 直接 EXPLAIN ANALYZE 连续执行 3 次均返回一个非空文本计划列。
    2. 同表普通 binary prepared SELECT id FROM t WHERE v=? ORDER BY id 连续执行 3 次,每次均返回 1,3;因此不是基本 COM_STMT_PREPARE/EXECUTE 不可用。
    3. 已运行 PR fix(frontend): preserve prepared EXPLAIN result metadata #26620 的 focused 白盒单测,explain、explain analyze、explain phyplan 元数据以及 rebuild 场景均通过。

    Unhappy Path 覆盖

    1. SQL-level prepared literal EXPLAIN ANALYZE:3/3 为 invalid bytes based slice。
    2. SQL-level prepared marker EXPLAIN ANALYZE ... v=?:3/3 为同一错误。
    3. Go driver 的 binary PrepareContext("EXPLAIN ANALYZE ..."):3/3 在 prepare 阶段报 ERROR 1064,错误位置稳定在第二个 EXPLAIN ANALYZE 前;普通 binary prepared SELECT 对照正常。
    4. text 执行失败后该物理连接在 driver 侧出现 busy buffer / commands out of sync,后续协议变体使用新连接独立验证,避免把连接污染误判为其他变体结果。

    白盒验证

    • 已确认修复 PR fix(frontend): preserve prepared EXPLAIN result metadata #26620 的提交 163a769a845548c0ea2549700f6999e1a4410b07 是当前 main 的祖先。
    • getPreparedResultColumnsFromPlan 已为 ExplainStmt、ExplainAnalyze、ExplainPhyPlan 构造 VARCHAR 结果列;对应 focused UT 通过。
    • 执行的命令:
    go test ./pkg/frontend -run '^(TestSendPrepareResponseForExplainUsesExplainColumn|TestRebuildPreparedExplainAnalyzeKeepsExplainColumn)$' -count=1 -v
    

    这说明需要继续追踪 live SQL/COM_STMT 执行与单测构造的 metadata/response 路径差异;本轮不把该差异臆测为单一根因。

    回归测试

    当前产品路径失败,未提交会掩盖失败的回归用例。修复后应在 prepared/protocol scenario 中加入 literal、marker、binary COM_STMT_PREPARE/EXECUTE、同连接重复执行,以及失败后连接可继续使用的断言;该场景适合 motr/suites/14_issue_regression。

    测试证据

    go test ./pkg/tests/issues -run '^TestCodexLive26602PreparedExplainAnalyzeTextAndBinary$' -count=3 -v
    go test ./pkg/tests/issues -run '^TestCodexLive26602PreparedExplainAnalyzeParameterAndBinary$' -count=3 -v
    go test ./pkg/tests/issues -run '^TestCodexLive26602BinaryProtocolPlainSelectControl$' -count=3 -v
    

    前两条均失败,第三条通过;全部在上述 main commit 的同一隔离集群执行。

    测试结论

    测试不通过。

  6. Ariznawlll commented on Aug 4, 2026

    @Ariznawlll
    CollaboratorAuthor

    本轮失败路径与回归处理

    • 测试版本:main@c726a6199094184a2e6ebc7504c3e011982b8976
    • 拓扑:本地单 LOG/TN/CN,经 127.0.0.1:6001;2026-08-04。

    原复现仍为 3/3:SQL-level PREPARE ... FROM 'EXPLAIN ANALYZE ...' 的 literal 和带参数版本,在 EXECUTE 时均报 invalid bytes based slice。普通 prepared EXPLAIN SELECT 作为控制路径可运行,说明不是连接或通用 PREPARE 失败。

    因此该失败没有写入 motr#74 的 .result:把当前 product failure 录成预期输出会掩盖缺陷。研发修复后应补一个 14_issue_regression SQL 用例,同时断言 literal/parameterized EXPLAIN ANALYZE、重复执行和失败后 session 可继续执行。详细初始复现与日志见本 issue 先前测试评论。

    测试不通过。

  7. Ariznawlll commented on Aug 6, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:main
    • commit:09e02eea38bfa87e4c4333ffb57b370091136bc8
    • 部署方式:本地单机 LogService + TN + CN,独立数据目录。
    • 测试日期:2026-08-06

    原问题验证

    对 PREPARE ... FROM 'EXPLAIN ANALYZE SELECT ...' 后的 EXECUTE,以及 WHERE v=? + EXECUTE ... USING @v,均可返回 ANALYZE plan。连续 3 轮执行 literal 和参数 7 -> 8 -> 7 均成功,且同一会话后 select count(*) 仍为 3。

    Happy Path 覆盖

    1. SQL 文本协议:literal prepared EXPLAIN ANALYZE。
    2. SQL 文本协议:参数化 prepared EXPLAIN ANALYZE,同一 handle 改变参数后复用。
    3. binary protocol:Go MySQL driver 的 db.Prepare / stmt.Query 连续执行 7 -> 8 -> 7;每次都读取到包含 Analyze: 的 plan 行。

    Unhappy Path 覆盖

    本 issue 是“内部结果字节切片”错误导致的正常语句失败;回归额外断言每次执行后连接仍可查询,覆盖原问题中的会话损坏风险。没有人为构造无效 SQL,因为那不覆盖该结果编码路径。

    白盒验证

    binary scenario 验证 COM_STMT_PREPARE/COM_STMT_EXECUTE,补足 SQL PREPARE 不能覆盖的协议分支。

    回归测试

    • motr PR:matrixorigin/motr#81
    • SQL 文件:suites/14_issue_regression/t/issue_26602_prepared_explain_analyze.test
    • binary 文件:script/14_issue_regression/issue_26602_prepared_explain_analyze_binary.sh、suites/scenarios/14_issue_regression/issue_26602_prepared_explain_analyze_binary.go
    • 结果:两条用例 record 后连续 3 轮均成功。

    测试结论

    测试通过。

  8. Ariznawlll commented on Aug 7, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:main
    • commit:d0057b3503611022cdf9a6e24c98065d209eb8e4
    • 部署方式:本地隔离双 CN(CN1 / CN2),MySQL text protocol 与 Go MySQL driver binary protocol。
    • 测试日期:2026-08-07

    原问题验证

    创建 t(id int primary key, v int) 并写入 (1,7),(2,8),(3,7) 后,以下 SQL 的 EXECUTE 已正常返回单列 EXPLAIN ANALYZE 文本:

    prepare p from 'explain analyze select id from t where v=7 order by id';
    execute p;

    每轮计划文本均含 Analyze,未再出现 invalid bytes based slice 或随后同连接不可用的问题。

    Happy Path 覆盖

    1. 直接 EXPLAIN ANALYZE SELECT ...:返回一个非空文本计划列。
    2. text protocol literal prepared:PREPARE/EXECUTE 连续 3 轮均返回含 Analyze 的单列计划。
    3. text protocol marker prepared:同一 handle 用 7 → 8 → NULL → 7 反复绑定,四次均返回含 Analyze 的计划。
    4. binary protocol:Go driver PrepareContext / QueryContext 用 7 → 8 → NULL → 7 重复执行;每次结果元数据恰为 1 列,且计划文本含 Analyze。
    5. 两个 CN 都覆盖到;每轮结束后的 select 1 均成功。

    Unhappy Path 覆盖

    1. 对含一个 marker 的 text prepared EXPLAIN ANALYZE 不提供 USING 参数,正确返回 Incorrect arguments to EXECUTE。
    2. 该拒绝后同一连接仍能查询;原表行数保持 3,没有 DDL/DML 副作用。
    3. 本 issue 是结果元数据编码路径的正常 SQL 失败,未将与该路径无关的语法错误计作覆盖。

    白盒验证

    go test ./pkg/frontend -run '^(TestSendPrepareResponseForExplainUsesExplainColumn|TestRebuildPreparedExplainAnalyzeKeepsExplainColumn)$' -count=3 -v
    

    ExplainStmt、ExplainAnalyze、ExplainPhyPlan 的 prepared-result 元数据子路径,以及 rebuild 的 ExplainAnalyze 子路径,连续 3 次通过。

    回归测试

    当前源码中的 frontend focused UT 已覆盖元数据构造。本轮未新增 motr 用例;此前关联的 motr#81 当前为 Closed 且未合入,因此没有把未合入分支作为本轮回归依据。

    测试证据

    live 双 CN 验证命令:

    go run /tmp/prepared_live_validation_d0057b35.go
    # prepared_validation_passed run=1 port=58613
    # prepared_validation_passed run=2 port=58614
    # prepared_validation_passed run=3 port=58613
    

    测试结论

    测试通过。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind/bugseverity/s0

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions