Skip to content

[Bug]: string UNION ALL projection error desynchronizes the MySQL connection #30010

Closed

Description

@Ariznawlll

Description

A valid two-column UNION ALL with a constant string in the right branch and an outer filter triggers a recovered internal vector/slice error. After the error, the same MySQL-protocol connection cannot execute a normal query: the next response has a wrong packet sequence number.

Environment

  • Branch: main
  • Commit: 5a5501b3e1048f3d19d96824cc68a62b7f0fcb07 (fixed official-main checkout tested on 2026-10-12)
  • Deployment: local 1 LOG + 1 TN + 1 CN, PyMySQL client; MySQL 8.0.46 control

Steps to reproduce

On one connection, execute these two statements in order:

SELECT v FROM (SELECT 1 AS k, 'ten' AS v
               UNION ALL SELECT 2, 'twenty') AS u
WHERE k > 0;
SELECT 1;

Actual behavior

The first query returns 1105 internal error: invalid bytes based slice (or, with LENGTH(v)/GROUP_CONCAT(v), 20101 internal error: panic unsafe slice cast: from length 8 is not enough for length 24). The next SELECT 1 on the same connection fails in the client with Packet sequence number wrong - got 2 expected 1. A new connection can still execute SELECT 1, so the service stays available but the affected session is unusable.

Expected behavior

The valid first query returns ('ten'), ('twenty'); the second returns 1. If any statement fails, the protocol response must still allow a subsequent statement on the same connection, as it does after ordinary SQL errors.

Stability and controls

  • Minimal query followed by SELECT 1: 3/3 first-query errors and 3/3 same-connection packet-sequence failures with fresh connections per repetition.
  • SELECT v without the outer filter, a one-column string UNION ALL, and the same two-column query with a table-backed right branch all return correct results.
  • An ordinary unknown-column error followed by SELECT 1 succeeds on the same connection 3/3; a fresh connection after this internal error also succeeds.
  • MySQL 8.0.46 returns the two expected string rows and keeps the connection usable.

Evidence and code analysis

At this SHA, EXPLAIN VERBOSE shows the UNION ALL left output as ten but the right Project output as integer 2 instead of twenty. The recovered unsafe-slice stack begins in pkg/common/util/unsafe.go:89; the remapping path is in pkg/sql/plan/query_builder.go around the set-operation branch. The wrong positional mapping likely causes a type-incompatible physical vector; the exact ownership of the protocol desynchronization still needs investigation.

Regression coverage

Cover direct and server-prepared string projections, normal controls, and a follow-up query on the same connection after the error. Assert process availability and clean response sequencing.

Related

#30007 tracks silent wrong numeric results and persisted values from a nearby positional-remapping path; #29989 tracks the explicit projection-width error. This issue tracks the distinct vector error plus connection-state failure.

Activity

  1. added this to the MO-202610 milestone on Oct 12, 2026
  2. added
    kind/bug
    on Oct 12, 2026
  3. added theissue type on Oct 12, 2026
  4. Ariznawlll commented on Oct 12, 2026

    @Ariznawlll
    CollaboratorAuthor

    现象:Prepared 查询报错后,同一连接无法继续使用

    测试版本:main / 5a5501b3e1048f3d19d96824cc68a62b7f0fcb07。在同一连接依次执行:

    PREPARE stmt FROM
      'SELECT v
         FROM (SELECT 1 AS k, "ten" AS v
               UNION ALL
               SELECT 2, "twenty") AS u
        WHERE k > ?';
    SET @threshold = 0;
    EXECUTE stmt USING @threshold;
    SELECT 1;
    步骤 MatrixOne 实际结果 MySQL 8.0.46 结果
    PREPARE 成功 成功
    EXECUTE 1105 internal error: invalid bytes based slice 返回 ten、twenty
    随后的 SELECT 1(同一连接) 客户端报 Packet sequence number wrong - got 2 expected 1 返回 1

    使用全新连接重复 3 次,结果一致。这里验证的是 SQL PREPARE/EXECUTE,尚未验证驱动层 COM_STMT_PREPARE。

  5. Ariznawlll commented on Oct 12, 2026

    @Ariznawlll
    CollaboratorAuthor

    补充定位与修复(PR #30015):

    1. 外层过滤触发 UNION ALL 列裁剪时,右分支的列序号映射到了错误列;字符串结果因此拿到整数向量,产生本 issue 的结果行编码错误。PR 已修正该映射。
    2. 行编码出错后,协议层曾先发送一个 ERR 包,再把同一错误交给语句处理层发送第二个 ERR 包;多余的包造成下一条 SQL 的包序号错误。这个独立缺陷已记录为 [Bug]: row serialization error sends duplicate ERR packets and desynchronizes connection #30016,同一 PR 一并修复。

    新增 SQL 回归覆盖直接查询、SQL PREPARE 复用及同连接后续查询;本地新用例重复 3 次均得到正确结果。协议单测覆盖文本和二进制 prepared 两条路径,完整 frontend 单测已通过。PR 的 GitHub CI 仍在运行。

  6. Ariznawlll commented on Oct 13, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:main;commit:175a645a85994f2c95992d3aa8d6dacb6d9e167f;2026-10-13。
    • 环境:本地隔离 MatrixOne 服务;按用例使用真实 SQL、MySQL 二进制协议或异步索引。

    原问题验证

    字符串 UNION ALL 过滤投影的直接查询返回 ten、twenty;SQL PREPARE 在参数 0→1→0 复用时分别返回两行、一行、两行,同一连接随后的 SELECT 1 均为 1。 对应定向用例在 MOTR 入口重复执行 3 次均通过(每次脚本内再执行 3 轮)。

    Happy Path 覆盖

    未过滤 UNION 对照与过滤查询一致,结果不再出现整数向量误当字符串。

    Unhappy Path 覆盖

    先前会导致内部编码错误及下一条命令包序号错乱;本次每次复用后同连接可继续查询。

    白盒验证

    本次以真实 SQL/协议行为直接判定;没有把仅凭源码推断当作运行结果。

    回归测试

    MOTR PR #234 中的 sql_composition_contracts.py 已提交;离线 CI 通过,仍待正常 review/merge。该缺陷由确定性语义/类型/协议边界触发,不需要另加 big-data 或 chaos 才能验收。

    测试证据

    PR 验证记录及 CI 运行。

    测试结论

    测试通过。

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

Metadata

Metadata

Assignees

Labels

kind/bug

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions