Skip to content

[Bug]: Prepared GROUP BY parameter markers produce invalid parameter metadata #25992

Closed

Description

@Ariznawlll

Is there an existing issue for the same bug?

  • I have checked the existing issues.

Searched for prepared statements with GROUP BY parameter markers, incorrect parameter counts, and the observed internal error. No matching issue was found.

Branch Name

main

Commit ID

c0486a2446c84c6dd6bf142494fe85243877ddc5

Other Environment Information

  • MatrixOne: 8.0.30-MatrixOne-v1.3.0
  • MySQL comparison: 8.0.45
  • SQL-level PREPARE / EXECUTE through the mysql client

Actual Behavior

For a query with separate parameter markers in the projection and GROUP BY, MatrixOne builds incorrect parameter metadata:

PREPARE p FROM 'SELECT ? AS k, SUM(n) FROM e GROUP BY ?';
  • EXECUTE p USING @a,@b is rejected with Incorrect arguments to EXECUTE.
  • EXECUTE p without arguments passes the argument-count check and returns an internal nil-pointer error from vector evaluation.
  • The same result reproduced 3/3 times. The session remains usable after the error.
  • MySQL 8.0.45 correctly requires two arguments and returns 7 | 60 for @a=7,@b=9.

A related expression form also produces inconsistent metadata:

PREPARE p2 FROM 'SELECT ? AS k, SUM(n) FROM e GROUP BY (? + 0)';
EXECUTE p2 USING @a;

MatrixOne accepts one argument at the count check and then returns:

ERROR 20101 (HY000): internal error: get prepare params error, index 2 not exists

Expected Behavior

The two parameter markers must remain independent and be assigned consecutive positions. EXECUTE ... USING @a,@b should succeed, while other argument counts should return a normal argument-count error without entering an internal-error path.

Steps to Reproduce

DROP DATABASE IF EXISTS ps_group_min;
CREATE DATABASE ps_group_min;
USE ps_group_min;
CREATE TABLE e(n INT);
INSERT INTO e VALUES(10),(20),(30);

SET @a=7,@b=9;
PREPARE p FROM 'SELECT ? AS k,SUM(n) FROM e GROUP BY ?';

-- MatrixOne rejects the correct two-argument execution.
EXECUTE p USING @a,@b;

-- MatrixOne incorrectly accepts the zero-argument shape at validation,
-- then returns an internal error during evaluation.
EXECUTE p;

Additional Information

The group binder records expressions by their rendered AST string. Both parameter markers render as ?, even though their parser offsets are different. The projection binder can therefore replace the projection marker with the grouped expression. Later plan traversal no longer observes the original two independent parameter references, so the prepared parameter count/order becomes inconsistent with the plan expressions.

Activity

  1. added this to the MO-202607 milestone on Jul 22, 2026
  2. added
    kind/bug
    on Jul 22, 2026
  3. added theissue type on Jul 22, 2026
  4. self-assigned this
    on Jul 22, 2026
  5. added
    severity/s0
    and removed on Jul 22, 2026
  6. LeftHandCold commented on Jul 26, 2026

    @LeftHandCold
    Collaborator

    fixed

  7. Ariznawlll commented on Jul 27, 2026

    @Ariznawlll
    CollaboratorAuthor

    最新 main 验证结果:通过

    测试版本

    • MatrixOne main@8c9bb66898afa74f8b231e1915d99489ae8a0da7
    • 关联修复:PR #26042

    测试过程

    首先验证 projection 和 GROUP BY 中两个独立参数:

    PREPARE s FROM
    'SELECT ? AS k, SUM(n) AS total FROM group_src GROUP BY ?';

    使用 (7,9) 和 (11,13) 重复执行;随后扩展验证表达式形式:

    PREPARE s2 FROM
    'SELECT ? AS k, SUM(n) AS total FROM group_src GROUP BY (? + 0)';

    实际结果

    • 参数 (7,9) 返回 7,60。
    • 复用 statement 使用 (11,13) 返回 11,60。
    • GROUP BY (? + 0) 使用 (17,19) 返回 17,60。
    • 仅提供一个参数时返回 ERROR 20301: Incorrect arguments to EXECUTE,没有 nil pointer 或 index not exists 内部错误。
    • 完整正向流程独立执行 3 轮,3/3 通过。

    结论

    projection 与 GROUP BY 参数能够保持独立位置并按顺序绑定,表达式形式和错误参数个数处理均正常,原问题已修复。

  8. Ariznawlll commented on Jul 27, 2026

    @Ariznawlll
    CollaboratorAuthor

    motr 黑盒回归

    该问题已加入 matrixorigin/motr#66。

    在 MatrixOne main@8c9bb66898afa74f8b231e1915d99489ae8a0da7 上连续执行 3 轮,结果稳定通过。

  9. Ariznawlll commented on Jul 27, 2026

    @Ariznawlll
    CollaboratorAuthor

    最新 main 复验结果:通过

    测试版本

    • MatrixOne:main@cd8e175349932ccf4be8d388080a0c3310e538ac
    • 测试方式:真实 MatrixOne 服务、SQL PREPARE/EXECUTE

    测试过程

    创建数据:

    CREATE TABLE prepared_group_source(n INT);
    INSERT INTO prepared_group_source VALUES (10),(20),(30);

    验证 projection 与 GROUP BY 中两个独立参数:

    PREPARE s FROM
    'SELECT ? AS k, SUM(n) AS total
     FROM prepared_group_source
     GROUP BY ?';
    
    SET @a=7,@b=9;
    EXECUTE s USING @a,@b;
    
    SET @a=11,@b=13;
    EXECUTE s USING @a,@b;

    随后验证参数位于表达式中的扩展场景:

    PREPARE s2 FROM
    'SELECT ? AS k, SUM(n) AS total
     FROM prepared_group_source
     GROUP BY (? + 0)';
    
    SET @a=17,@b=19;
    EXECUTE s2 USING @a,@b;

    实际结果

    • (7,9) 返回 7 | 60;
    • 复用 statement,(11,13) 返回 11 | 60;
    • 表达式形式 (17,19) 返回 17 | 60;
    • 完整流程连续执行 3 轮,3/3 通过,三轮输出一致;
    • 未出现 Incorrect arguments to EXECUTE、nil pointer 或 index not exists 内部错误。

    结论

    最新 main 上两个参数能够保持独立位置并正确绑定,prepared statement 重复执行和表达式形式均正常,原问题已修复。

    该问题已加入 matrixorigin/motr#66:

  10. Ariznawlll commented on Aug 7, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:main
    • commit:d0057b3503611022cdf9a6e24c98065d209eb8e4
    • 部署方式:本地隔离双 CN(分别独立连接验证)
    • 测试日期:2026-08-07

    原问题验证

    使用 bucket 为三组不同值、value_col 为 10/20/30 的最小数据集验证:普通 SQL SELECT bucket, SUM(value_col) FROM metric_rows GROUP BY bucket 稳定返回 3 行。

    随后执行:

    PREPARE p FROM
    'SELECT ? AS projection_value, SUM(value_col) FROM metric_rows GROUP BY ?';
    SET @projection_value=7,@group_value='bucket';
    EXECUTE p USING @projection_value,@group_value;

    当前 main 返回 ERROR 1105 (HY000): invalid allocation account。在两个 CN 上各连续执行 3 次,文本协议和 binary prepared 协议均为 6/6 稳定复现。

    另外,binary prepared 的 SELECT ? AS projection_value, SUM(value_col) FROM metric_rows GROUP BY bucket 未报错但只返回 1 行;相同数据的普通 SQL 返回 3 行,存在分组结果丢失。

    Happy Path 覆盖

    1. 非 prepared 的普通 GROUP BY bucket:3/3 返回三组聚合结果。
    2. 失败后在同一连接执行 SELECT COUNT(*) FROM metric_rows:6/6 返回 3,连接可继续使用且源数据未变化。

    Unhappy Path 覆盖

    1. binary prepared 中 GROUP BY ?:双 CN 各 3 次均返回 ERROR 1105 (HY000): invalid allocation account。
    2. SQL PREPARE/EXECUTE 文本协议中 GROUP BY ?:双 CN 各 3 次均返回相同的 ERROR 1105。
    3. binary prepared 中仅 projection 使用 ?、GROUP BY bucket:双 CN 各 3 次均只返回 1 行,期望与普通 SQL 一致的 3 行;这是无报错的数据结果错误。

    白盒验证

    不适用。本轮先以 SQL 与 mysql binary prepared protocol 验证外部可观察行为;问题已具备稳定最小复现与普通 SQL 对照。

    回归测试

    已有 motr 黑盒用例:issue_25992_prepared_group_by_metadata.test。该用例在当前 main 上以原有正确预期执行时稳定失败,未修改预期结果掩盖问题。

    测试证据

    复现命令为 go run /tmp/prepared_group_by_repro.go;在 CN1、CN2 上分别连续执行 3 轮。每轮均同时验证普通 SQL 的 3 行对照、binary/text GROUP BY ? 的 1105、以及失败后的行数与连接可用性。

    测试结论

    测试不通过。

  11. Ariznawlll commented on Aug 9, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:main
    • commit:08770e1beeb23239c91165492f2fe85243a3c66a
    • 部署方式:本地隔离单 CN/单 TN/LogService,MySQL 端口 6001
    • 测试日期:2026-08-09

    原问题验证

    按原 SQL 建表 e(n) 并写入 10,20,30,执行 PREPARE p FROM 'SELECT ? AS k, SUM(n) FROM e GROUP BY ?' 后以两个参数执行。文本协议连续 3/3 返回 ERROR 1105: invalid allocation account;Go MySQL driver 的 binary COM_STMT_PREPARE/COM_STMT_EXECUTE 连续 3/3 返回相同错误。因此当前失败不是文本协议、连接复用或夹具造成的。

    Happy Path 覆盖

    1. 直接控制语句 GROUP BY (9 + 0) 返回 k=7,total=60。
    2. GROUP BY (? + 0) 使用两个 marker 在文本和 binary 协议均返回 7,60。
    3. bare-marker 失败后重新查询 e 始终仍为 COUNT=3,SUM=60。

    Unhappy Path 覆盖

    1. GROUP BY (? + 0) 只传一个参数时,文本协议返回普通 EXECUTE 参数个数错误;binary driver 在本地参数校验阶段返回 expected 2 arguments, got 1。
    2. bare GROUP BY ? 的服务端错误后,数据、后续查询和新连接均可用;未观察到 panic 或连接卡死。

    白盒验证

    不适用:已通过两个独立协议入口稳定触发公开 SQL 路径,尚不需要故障注入。

    回归测试

    已有 motr/suites/14_issue_regression/t/issue_25992_prepared_group_by_metadata.test。最新 main 执行该 case 同样以 invalid allocation account 失败;当前产品失败,未更新其 result 基线。

    测试证据

    • 文本协议原始复现:3/3 ERROR 1105。
    • binary Go driver 复现:3/3 ERROR 1105,包裹 marker 对照成功且失败后数据为 3,60。
    • ./motr --suite 14_issue_regression 中同名 case 在该 commit 失败。

    测试结论

    测试不通过。

  12. Ariznawlll commented on Aug 10, 2026

    @Ariznawlll
    CollaboratorAuthor

    在 4.2-dev 上完成回归验证。

    • 分支/提交:4.2-dev@fa6c57cbfaba17d705ebc673bd49c06bf156e4d5。该分支已包含初始修复 PR fix(plan): preserve prepared aggregate parameters #26042(fix(plan): preserve prepared aggregate parameters)。
    • 原始场景:PREPARE p FROM 'SELECT ? AS k, SUM(n) AS total FROM e GROUP BY ?'; EXECUTE p USING @a, @b;,其中 e 包含 10,20,30,@a=7、@b=9。连续 3 轮均返回 7 | 60。
    • 参数错误路径:同一 statement 少传参数时稳定返回 Incorrect arguments to EXECUTE;随后同一连接继续执行 SELECT COUNT(*), SUM(n),稳定返回 3 | 60,没有内部错误或会话污染。
    • 扩展场景:GROUP BY (? + 0) 以及带 WHERE n > ? 的嵌套来源、同时含投影参数和 GROUP BY 参数的三参数语句,连续 3 轮均返回预期结果(分别 7 | 60、7 | 50)。
    • 白盒:pkg/sql/plan.TestPreparedProjectionAndGroupMarkersStayIndependent 通过,确认投影标记与 GROUP BY 标记分别保留为独立参数位置。

    结论:#25992 所述 prepared GROUP BY 参数元数据问题在当前 4.2-dev 已无法复现,原始及扩展路径均通过。测试通过。

  13. added a commit that references this issue on Aug 10, 2026
    5d7abd7
  14. LeftHandCold commented on Sep 1, 2026

    @LeftHandCold
    Collaborator

    fixed

  15. Ariznawlll commented on Sep 2, 2026

    @Ariznawlll
    CollaboratorAuthor

    最新 main 验证结论

    • 基线:origin/main f4214a008c29c1aa9ca3a0ec84138296dde61358
    • Prepared SELECT ? AS k, SUM(n) ... GROUP BY ? 执行结果正确(7, 60),重复绑定及 GROUP BY (? + 0) 均正常。
    • 缺少 USING 参数时返回正常 ERROR 20301,不再触发历史 nil-pointer,错误后会话仍可用。
    • MOTR 已有 issue_25992_prepared_group_by_metadata 用例,本次补充了异常恢复覆盖并验证通过。

    已更新 motr#141,可以关闭该 issue。

  16. added 3 commits that reference this issue on Sep 17, 2026
    bbab76d
    48e127d
    70c9b3c
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