Skip to content

[Bug]: Prepared SUM and AVG reject parameter markers as TEXT #26019

Closed

Description

@Ariznawlll

Is there an existing issue for the same bug?

  • I searched open and closed issues for prepared aggregate parameter markers, SUM(?), AVG(?), and the observed TEXT type error. No exact duplicate was found.

Branch Name

main

Commit ID

50319026a69e45c3c190cb7a09e9c5fbdb7f6857

Other Environment Information

  • MatrixOne: 8.0.30-MatrixOne-v1.3.0
  • MySQL comparison: 8.0.45
  • SQL-level and MySQL binary-protocol prepared statements were checked.

Actual Behavior

MatrixOne rejects numeric aggregate parameter markers while preparing the statement:

PREPARE s FROM 'SELECT SUM(?)';

Result:

ERROR 20203 (HY000): invalid argument aggregate function sum, bad value [TEXT]

AVG(?) fails in the same way. The failure occurs during PREPARE, before any value can be supplied by EXECUTE.

The result reproduced 3/3 times. A binary-protocol prepared recursive CTE with SUM(n), where n originates from a parameter marker, reaches the same error.

Expected Behavior

The statement should be prepared successfully and the parameter type should be resolved from the value supplied at execution time, matching MySQL.

MySQL 8.0.45 returns 2 for each of three runs:

PREPARE s FROM 'SELECT SUM(?)';
SET @n = 2;
EXECUTE s USING @n;

Steps to Reproduce

PREPARE s1 FROM 'SELECT SUM(?)';
SET @n = 2;
EXECUTE s1 USING @n;

PREPARE s2 FROM 'SELECT AVG(?)';
EXECUTE s2 USING @n;

Control statements with a direct numeric expression do work:

SELECT SUM(2);
PREPARE c1 FROM 'SELECT SUM(CAST(? AS SIGNED))';
PREPARE c2 FROM 'SELECT SUM(? + 0)';

Additional Information

The parameter binder creates an untyped marker as T_text by default. Numeric aggregate overload resolution happens while constructing the prepared plan, before the execution value is available, so SUM and AVG reject the marker as TEXT.

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 22, 2026

    @LeftHandCold
    Collaborator

    Confirmed on current-main baseline e15943929885c2c5c83daed90f0f8caeb9a1f0d0. A bare prepared marker is initially bound as TEXT, while SUM/AVG use a fixed numeric-only aggregate type check with no implicit conversion, so the direct form fails during planning.

    This needs static prepare-time numeric inference rather than execution-value-dependent typing. However, a direct aggregate special case would not close the recursive-CTE reproduction: at the aggregate boundary the marker has already become a CTE column, so the inferred type must survive recursive seed/term common-column resolution as well. The window-aggregate binder must also obey the same contract.

    I therefore do not propose a direct-only PR as a close. A complete fix should cover direct SUM/AVG, recursive CTEs, repeated executions with numeric/numeric-string/NULL/invalid-string inputs, parameter metadata, window aggregates, and cached re-execution.

  7. LeftHandCold commented on Jul 23, 2026

    @LeftHandCold
    Collaborator

    Re-reviewed while implementing the related prepared-statement batch, now against current main (0d7eeb38b4). No equivalent fix has landed, and the prior no-PR decision still holds.

    A local SUM(?)/AVG(?) binder exception would only fix the direct expression. The recursive-CTE reproduction reaches the aggregate after the marker has become a common CTE column, so numeric intent must survive seed/recursive-term common-type resolution; window aggregates must use the same inference contract. Without that, a small PR would advertise support while the production recursive/window paths still reject or mis-type the value.

    A complete PR therefore needs one static prepare-time numeric inference contract with direct aggregate, recursive CTE, window aggregate, parameter metadata, cached/repeated execution, and numeric/numeric-string/NULL/invalid-string coverage. I am not opening a partial aggregate-only PR.

  8. LeftHandCold commented on Aug 1, 2026

    @LeftHandCold
    Collaborator

    fixed

  9. Ariznawlll commented on Aug 4, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:main
    • commit:c726a6199094184a2e6ebc7504c3e011982b8976
    • 部署方式:本地隔离单节点 LOG / TN / CN 集群;SQL-level prepared statements 和 Go MySQL binary protocol。
    • 测试日期:2026-08-04

    原问题验证

    原问题的 SUM(?) 与 AVG(?) marker 推断路径已恢复。每轮同时执行 text SELECT SUM(?)、SELECT AVG(?)、SELECT SUM(?) OVER (),并以 2 作为绑定参数;结果均为 2。完整用例重复 3 次。

    Happy Path 覆盖

    1. text prepared SUM(?)、AVG(?)、window SUM(?) OVER () 均成功。
    2. recursive CTE WITH RECURSIVE ... SELECT SUM(n),AVG(n) 用两个 marker 绑定 1,4,结果为 10,2.5。
    3. binary prepared SUM(?) 依次绑定整数 2、浮点 3.5、NULL;前两者有行结果,NULL 返回 NULL。
    4. 现有 binary prepare metadata 单测覆盖 SUM、AVG 和 window SUM 的 parameter/result metadata。

    Unhappy Path 覆盖

    1. NULL marker:结果为 NULL,没有 nil panic 或残留旧参数。
    2. 非数字字符串 marker:当前方言接受/转换该输入;测试完整消费结果后继续执行 SELECT 1 成功,未污染 prepared handle 或连接。此处不把方言允许的 coercion 误报为 bug。
    3. 连续不同类型重绑均在每轮中完成,验证不复用旧二进制参数。

    白盒验证

    已运行 TestPreparedNumericAggregateBinaryProtocolMetadata,其验证 prepare response 中的 marker 与 DOUBLE result metadata。此 issue 的主要风险是 planner 参数推断,公共 SQL 结果、recursive/window 分支和 binary 重绑均已实际执行。

    回归测试

    现有 package UT 覆盖 metadata;稳定 text SQL 可扩展进 prepare BVT。binary 类型重绑更适合 motr/suites/14_issue_regression scenario。当前没有新提交。

    测试证据

    go test ./pkg/tests/issues -run '^TestCodexLive26019PreparedAggregatesTextAndBinary$' -count=3 -v
    go test ./pkg/frontend -run '^TestPreparedNumericAggregateBinaryProtocolMetadata$' -count=1 -v
    

    live 用例三轮均通过。

    测试结论

    测试通过。

  10. Ariznawlll commented on Aug 4, 2026

    @Ariznawlll
    CollaboratorAuthor

    motr 回归补充

    本评论补充已通过路径的可执行回归;此前原问题的完整验证结论见 #26019 的已有测试评论。

    • 测试版本:main@c726a6199094184a2e6ebc7504c3e011982b8976
    • 拓扑:本地单 LOG/TN/CN,经 127.0.0.1:6001;2026-08-04。
    • 回归 PR:motr#74,提交 3d79e01b6348e23bb747de5ab9a9d16995304a29。

    已加入:

    1. suites/14_issue_regression/t/issue_26019_prepared_sum_avg.test
      • SQL PREPARE/EXECUTE 下 SUM(?) 以同一 handle 依次绑定 INT(2)、FLOAT(3.5)、NULL;结果为 2.0、3.5、NULL。
      • AVG(?) 覆盖整数/浮点重绑;SUM(?) OVER () 覆盖窗口聚合绑定。
      • 递归 CTE 中 marker 进入递归列后再经 SUM/AVG,断言 10.0/2.5。
    2. suites/scenarios/14_issue_regression/issue_26019_protocol_prepared_aggregates.go(由同名 .sh 调用)
      • 使用 go-sql-driver/mysql 的真实 COM_STMT_PREPARE/EXECUTE,不是 SQL 文本 EXECUTE。
      • 同一物理连接(连接池上限 1)复用 SUM(?) handle,验证 int64、float64、nil;再验证 AVG(?) 和窗口 SUM(?) OVER ()。
      • NULL 后继续 SELECT 1,防止只检查结果而漏掉 statement/session 损坏。

    执行证据:

    ./motr --config /private/tmp/motr-prepared-session-16031.yml --repeat 3 \
      14_issue_regression.issue_26019_prepared_sum_avg \
      14_issue_regression.issue_26019_protocol_prepared_aggregates
    
    SQL:3/3 success(每轮 27 个断言)
    binary scenario:3/3 success
    

    该场景的稳定 stdout 已记录在 suites/scenarios/14_issue_regression/out/issue_26019_protocol_prepared_aggregates.out;14_issue_regression 整套运行时同样成功。白盒注入不适用:该问题可由公开协议接口稳定触发,回归应保留在 motr 协议场景。

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