Repository navigation
Description
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
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 0This 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);
-1after a statement that returns a result set (e.g. SELECT);0after 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
- Start a standalone
mo-service. - Connect with any MySQL client.
- Run a DML statement, then
SELECT ROW_COUNT();in the same session. - 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
Ariznawlll commentedon Jul 22, 2026
已在最新
main上完成验证,结论:该问题已修复,并已加入 motr 黑盒回归。测试版本
main50319026a69e45c3c190cb7a09e9c5fbdb7f6857e2c5dc00b测试步骤
SELECT ROW_COUNT(),避免被其他 statement 改写会话状态。关键结果:
执行命令:
测试结果
ROW_COUNT()不再固定返回 0,能够读取上一条 statement 的会话 affected rows。回归用例
14_issue_regression.issue_24767_row_count98bb44a227d4d934d0df67e51eecd2449aad8b8930/30case、440/440条 SQL 通过。Ariznawlll commentedon Jul 22, 2026
继续在最新
main上完成了一轮扩展验证。本轮重点补充上一轮未覆盖的 statement 边界、事务、预处理协议和多 session 隔离场景。测试版本
main50319026a69e45c3c190cb7a09e9c5fbdb7f6857mo-service:使用上述 commit 构建并启动的 standalone 实例一、真实 SQL 扩展验证
在同一连接内依次执行目标语句,并在目标语句之后立即执行
SELECT ROW_COUNT(),结果如下:以上结果符合当前
ROW_COUNT()的设计语义:DML 返回上一条语句的 affected rows;返回结果集的语句为-1;DDL、事务控制语句和未命中 DML 为0。二、错误及连续调用边界
使用
mysql --force保持同一连接,在 DML 报错后继续读取ROW_COUNT():失败语句没有遗留上一条成功 DML 的计数。MatrixOne 当前 UPDATE 使用 matched-rows 口径,因此“命中但值未变化”返回
1,这与 issue 原文中记录的现有行为一致,不属于本 issue 的ROW_COUNT()固定返回 0 问题。三、多 session 与二进制预处理协议
使用
database/sql和go-sql-driver/mysql固定两个物理连接:ROW_COUNT();每轮结果均为:
说明 affected rows 是 session 级状态,不会在连接间串值;二进制预处理协议也能正确更新该状态。
四、实现层重复与竞态验证
覆盖 frontend 状态记录/恢复、协议命令失败、连接迁移、函数读取、process 状态及 pipeline 编解码:
结果:4 个 package 的普通重复测试和
-race测试全部通过,未发现 data race。最终结论
本轮扩展场景全部通过。最新
main上 #24767 的修复不仅覆盖基础 INSERT/UPDATE/DELETE,也覆盖了 SELECT/DDL/事务边界、错误语句、UPSERT/REPLACE、INSERT…SELECT、服务端及二进制 prepared statement、多 session 隔离和连接迁移状态传递;未发现问题复现或新的 affected-rows 状态错误。