Repository navigation
Closed
Closed
Copy link
Labels
ai-medium人和AI来回几轮,各出一半力kind/bugSomething isn't workingseverity/s0Active / top priority for current sprint. Owner has committed to working on it now.
Milestone
Description
Is there an existing issue for the same bug?
- I have checked the existing issues.
Branch Name
4.0-dev
Commit ID
Other Environment Information
- Hardware parameters: N/A
- OS type: macOS (darwin/arm64); also reproducible on Linux
- Others: standalone
mo-service -launch etc/launch/launch.toml
Actual Behavior
MatrixOne counts affected rows as the number of rows net-written to the target main table, counted once. For statements where MySQL uses its client-protocol convention, the affected-rows value (returned in the OK packet and by ROW_COUNT()) differs from MySQL:
| Statement | MatrixOne | MySQL |
|---|---|---|
REPLACE over an existing key (delete + insert) |
1 | 2 |
INSERT ... ON DUPLICATE KEY UPDATE -> row updated |
1 | 2 |
INSERT ... ON DUPLICATE KEY UPDATE -> no-op (same values) |
1 | 0 |
INSERT ... ON DUPLICATE KEY UPDATE -> new row inserted |
1 | 1 |
REPLACE inserting a new key |
1 | 1 |
Expected Behavior
MySQL-compatible affected-rows:
REPLACEreplacing an existing row ->2(delete + insert)ON DUPLICATE KEY UPDATEupdating a row ->2ON DUPLICATE KEY UPDATEno-op (row set to its current values) ->0
(MySQL docs: "with ON DUPLICATE KEY UPDATE, the affected-rows value per row is 1 if the row is inserted as a new row, 2 if an existing row is updated, and 0 if an existing row is set to its current values"; REPLACE returns 2 when it replaces an existing row.)
Steps to Reproduce
drop database if exists rc; create database rc; use rc;
create table t(id int primary key, v int);
insert into t values (1,1),(2,2);
-- REPLACE over an existing key
replace into t values (1,100);
select row_count(); -- MO: 1, MySQL: 2
-- ON DUPLICATE KEY UPDATE: update an existing row
insert into t values (1,5) on duplicate key update v=v+1;
select row_count(); -- MO: 1, MySQL: 2
-- ON DUPLICATE KEY UPDATE: no-op (set to current value)
insert into t values (1,6) on duplicate key update v=v;
select row_count(); -- MO: 1, MySQL: 0Additional information
- Root cause is at the planner/executor level, independent of
ROW_COUNT(). The final affected-rows isScope.affectedRows()(pkg/sql/compile/compile.go:5122), which sumsGetAffectedRows()only over operators whoseAddAffectedRows == true. Secondary/hidden-index branches set it tofalseby design — see the comment atpkg/sql/plan/build_dml_util.go:112:addAffectedRows bool // for hidden table, should not update affect Rows, e.g. delete 1 row from table t with schema a int, b unique key, c key, affect rows should be 1 instead of 3 - For
REPLACE, only the insert into the main table is counted (the conflicting-row delete is not), so a replace-over-existing reports1instead of2. - For
ON DUPLICATE KEY UPDATE, the operator reports1for insert/update/no-op alike; it does not implement MySQL's2(update) /0(no-op) convention. - The affected-rows value flows into the OK packet seen by every client (JDBC
executeUpdate(), drivers' "rows affected", etc.), not justROW_COUNT(). A fix should change the affected-rows accounting (e.g. count the REPLACE delete, and distinguish update vs no-op in the ODKU operator) and update affected BVT goldens. - Discovered while implementing the MySQL-compatible
ROW_COUNT()builtin (PR 4.0: fix: implement ROW_COUNT() to return previous statement's affected rows #24768). That PR faithfully surfaces the executor's affected-rows, so it intentionally does not encode these non-MySQL values in its golden; this issue tracks the underlying accounting.
Metadata
Metadata
Assignees
Labels
ai-medium人和AI来回几轮,各出一半力kind/bugSomething isn't workingseverity/s0Active / top priority for current sprint. Owner has committed to working on it now.
Activity
4.0: test: cover ROW_COUNT() after REPLACE collision and ODKU duplica…
test: cover ROW_COUNT() after REPLACE collision and ODKU duplicate hit
3.0: bring ROW_COUNT() to parity with 4.0-dev (matrixorigin#24768)