Skip to content

[Bug]: ROW_COUNT() always returns 0 #24767

Closed
Closed

Description

@ck89119

Is there an existing issue for the same bug?

  • I have checked the existing issues.

Branch Name

4.0-dev (also affects main)

Commit ID

198f1ad

Other Environment Information

  • Hardware parameters: N/A
  • OS type: macOS (darwin), standalone mo-service
  • Others: verified with text protocol (mysql CLI), server-side PREPARE/EXECUTE, and binary protocol (go-sql-driver/mysql)

Actual Behavior

SELECT ROW_COUNT() always returns 0, regardless of the previous statement.

create table t(id int primary key, v int);

insert into t values (1,10),(2,20);
select row_count();   -- got 0

insert ignore into t values (3,30);   -- a new row is actually inserted
select row_count();   -- got 0

update t set v=v+1 where id in (1,2); -- 2 rows updated
select row_count();   -- got 0

delete from t where id=1;             -- 1 row deleted
select row_count();   -- got 0

This affects every statement type (plain INSERT, INSERT IGNORE, UPDATE, DELETE, REPLACE, ...), not just the ones above.

Expected Behavior

Following MySQL semantics, ROW_COUNT() should return:

  • the number of affected rows for the previous DML (INSERT/UPDATE/DELETE/REPLACE/LOAD);
  • -1 after a statement that returns a result set (e.g. SELECT);
  • 0 after a statement that affects no rows (e.g. DDL).

So for the example above the expected values are 2, 1, 2, 1.

Steps to Reproduce

  1. Start a standalone mo-service.
  2. Connect with any MySQL client.
  3. Run a DML statement, then SELECT ROW_COUNT(); in the same session.
  4. Observe that the result is always 0.

Additional information

Root cause: the ROW_COUNT() builtin is an unimplemented stub that hard-codes return 0.

// pkg/sql/plan/function/func_unary.go
func RowCount(...) error {
    return opNoneParamToFixed[uint64](result, proc, length, func() uint64 {
        return 0
    })
}

git log --all -S "func RowCount" shows it has never been implemented since the function was introduced. Unlike the sibling builtin last_insert_id() (which reads proc.GetLastInsertID()), there is no session/process state tracking the previous statement's affected rows for ROW_COUNT() to read.

Note (to avoid confusion): the affected-rows count reported in the protocol OK packet is correct. Verified across three protocol paths — text protocol, server-side PREPARE/EXECUTE, and binary protocol (go-sql-driver/mysql) — INSERT IGNORE new row = 1, duplicate = 0, UPDATE = matched rows, DELETE = correct. The bug is strictly in the ROW_COUNT() SQL function, which is what client tools/frameworks often use to read affected rows.

Separately observed (not part of this bug, opposite direction): MatrixOne's UPDATE affected-rows uses "matched rows" semantics rather than MySQL's "changed rows" — setting a column to its existing value returns 1 in MatrixOne vs 0 in MySQL.

Activity

  1. added
    kind/bug
    on Jun 2, 2026
  2. self-assigned this
    on Jun 2, 2026
  3. added theissue type on Jun 2, 2026
  4. added
    severity/s1
    on Jun 30, 2026
  5. added this to the MO-202607 milestone on Jun 30, 2026
  6. added
    ai-heavy
    on Jul 1, 2026
  7. added
    ai-easy
    ai-light
    and removed
    ai-heavy
    ai-easy
    on Jul 15, 2026
  8. assigned and unassigned on Jul 17, 2026
  9. Repository owner unassigned on Jul 22, 2026
  10. Ariznawlll commented on Jul 22, 2026

    @Ariznawlll
    Collaborator

    已在最新 main 上完成验证,结论:该问题已修复,并已加入 motr 黑盒回归。

    测试版本

    • MatrixOne 分支:main
    • MatrixOne commit:50319026a69e45c3c190cb7a09e9c5fbdb7f6857
    • 对应修复:PR #25750,commit e2c5dc00b

    测试步骤

    1. 在同一 MySQL session 中创建带主键、唯一键的测试表。
    2. 每执行一条 DML,立即执行 SELECT ROW_COUNT(),避免被其他 statement 改写会话状态。
    3. 覆盖普通 INSERT 两行。
    4. 覆盖 INSERT IGNORE:一条新记录加一条重复记录。
    5. 覆盖 UPDATE 两行,以及值没有变化但匹配一行的场景。
    6. 覆盖 DELETE 命中两行和命中零行。
    7. 覆盖 REPLACE 同时发生替换与新增的场景。
    8. 同时运行 frontend、function、process 和 pipeline 的 affected-rows 传递/序列化 UT。

    关键结果:

    INSERT 2 rows                       -> ROW_COUNT() = 2
    INSERT IGNORE (1 new + 1 duplicate) -> ROW_COUNT() = 1
    UPDATE matches 2 rows               -> ROW_COUNT() = 2
    UPDATE matches 1 unchanged row      -> ROW_COUNT() = 1
    DELETE matches 2 rows               -> ROW_COUNT() = 2
    DELETE matches 0 rows               -> ROW_COUNT() = 0
    REPLACE (replace + insert)           -> ROW_COUNT() = 3
    

    执行命令:

    go test ./pkg/frontend ./pkg/sql/plan/function ./pkg/vm/process ./pkg/pb/pipeline \
      -run '^(TestSessionLastAffectedRows|TestRecordLastAffectedRows|TestMigrateConnectionFromPreservesLastAffectedRows|TestRowCount|TestAffectedRows|TestProcessInfoAffectedRowsRoundTrip)$' \
      -count=3
    
    ./motr 14_issue_regression.issue_24767_row_count --repeat 10
    

    测试结果

    • affected-rows 相关 UT 连续 3 轮通过。
    • 真实 SQL 黑盒测试连续 10 轮通过。
    • ROW_COUNT() 不再固定返回 0,能够读取上一条 statement 的会话 affected rows。
    • 普通文本协议、session 状态、process 状态以及跨 pipeline 的 affected rows 传递均有覆盖。

    回归用例

    • 已加入 motr PR #61
    • 用例:14_issue_regression.issue_24767_row_count
    • motr commit:98bb44a227d4d934d0df67e51eecd2449aad8b89
    • 与本次另外两个新增黑盒用例并发重复 10 轮,共 30/30 case、440/440 条 SQL 通过。
  11. Ariznawlll commented on Jul 22, 2026

    @Ariznawlll
    Collaborator

    继续在最新 main 上完成了一轮扩展验证。本轮重点补充上一轮未覆盖的 statement 边界、事务、预处理协议和多 session 隔离场景。

    测试版本

    • MatrixOne 分支:main
    • MatrixOne commit:50319026a69e45c3c190cb7a09e9c5fbdb7f6857
    • mo-service:使用上述 commit 构建并启动的 standalone 实例

    一、真实 SQL 扩展验证

    在同一连接内依次执行目标语句,并在目标语句之后立即执行 SELECT ROW_COUNT(),结果如下:

    连接初始化                         ->  0
    CREATE TABLE                       ->  0
    INSERT 3 rows                      ->  3
    UPSERT,冲突记录发生实际更新        ->  2
    UPSERT,冲突记录的值没有变化        ->  0
    REPLACE(替换 1 行并新增 1 行)     ->  3
    INSERT ... SELECT 2 rows           ->  2
    UPDATE 命中 3 行                   ->  3
    UPDATE 命中 0 行                   ->  0
    DELETE 命中 0 行                   ->  0
    普通 SELECT 返回结果集之后          -> -1
    事务内 INSERT 2 rows               ->  2
    ROLLBACK 之后                      ->  0
    事务内 DELETE 2 rows               ->  2
    COMMIT 之后                        ->  0
    服务端 PREPARE/EXECUTE INSERT       ->  1
    TRUNCATE TABLE                     ->  0
    DROP TABLE                         ->  0
    

    以上结果符合当前 ROW_COUNT() 的设计语义:DML 返回上一条语句的 affected rows;返回结果集的语句为 -1;DDL、事务控制语句和未命中 DML 为 0。

    二、错误及连续调用边界

    使用 mysql --force 保持同一连接,在 DML 报错后继续读取 ROW_COUNT():

    INSERT 1 row                       ->  1
    紧接着第二次 SELECT ROW_COUNT()     -> -1
    重复主键 INSERT 报 ERROR 1062 后    -> -1
    INSERT IGNORE,全部为重复记录       ->  0
    UPDATE 命中 1 行但值未变化          ->  1
    

    失败语句没有遗留上一条成功 DML 的计数。MatrixOne 当前 UPDATE 使用 matched-rows 口径,因此“命中但值未变化”返回 1,这与 issue 原文中记录的现有行为一致,不属于本 issue 的 ROW_COUNT() 固定返回 0 问题。

    三、多 session 与二进制预处理协议

    使用 database/sql 和 go-sql-driver/mysql 固定两个物理连接:

    1. session 1 插入 2 行;
    2. session 2 插入 1 行;
    3. 在两条连接上分别查询 ROW_COUNT();
    4. session 1 再通过 binary prepared statement 插入 1 行;
    5. 整体连续执行 10 轮。

    每轮结果均为:

    session1=2 session2=1
    binary_prepared=1
    

    说明 affected rows 是 session 级状态,不会在连接间串值;二进制预处理协议也能正确更新该状态。

    四、实现层重复与竞态验证

    覆盖 frontend 状态记录/恢复、协议命令失败、连接迁移、函数读取、process 状态及 pipeline 编解码:

    go test ./pkg/frontend ./pkg/sql/plan/function ./pkg/vm/process ./pkg/pb/pipeline \
      -run '^(TestSessionLastAffectedRows|TestMarkRowCountFailed|TestRestoreRowCount|TestRecordLastAffectedRowsFieldList|TestRecordLastAffectedRows|TestMigrateConnectionFromPreservesLastAffectedRows|TestExecRequestRewriteFailureMarksRowCountFailed|TestExecRequestProtocolCommandRowCount|TestExecRequestStmtSendLongDataRowCount|TestRowCount|TestAffectedRows|TestProcessInfoAffectedRowsRoundTrip)$' \
      -count=10
    
    go test -race ./pkg/frontend ./pkg/sql/plan/function ./pkg/vm/process ./pkg/pb/pipeline \
      -run '^(TestSessionLastAffectedRows|TestMarkRowCountFailed|TestRestoreRowCount|TestRecordLastAffectedRowsFieldList|TestRecordLastAffectedRows|TestMigrateConnectionFromPreservesLastAffectedRows|TestExecRequestRewriteFailureMarksRowCountFailed|TestExecRequestProtocolCommandRowCount|TestExecRequestStmtSendLongDataRowCount|TestRowCount|TestAffectedRows|TestProcessInfoAffectedRowsRoundTrip)$' \
      -count=3
    

    结果:4 个 package 的普通重复测试和 -race 测试全部通过,未发现 data race。

    最终结论

    本轮扩展场景全部通过。最新 main 上 #24767 的修复不仅覆盖基础 INSERT/UPDATE/DELETE,也覆盖了 SELECT/DDL/事务边界、错误语句、UPSERT/REPLACE、INSERT…SELECT、服务端及二进制 prepared statement、多 session 隔离和连接迁移状态传递;未发现问题复现或新的 affected-rows 状态错误。

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

Metadata

Metadata

Assignees

Labels

ai-lightkind/bugseverity/s1

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions