Skip to content

[Bug]: REPLACE / INSERT ... ON DUPLICATE KEY UPDATE report non-MySQL affected-rows #24907

Closed

Description

@ck89119

Is there an existing issue for the same bug?

  • I have checked the existing issues.

Branch Name

4.0-dev

Commit ID

7a8e7cd

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:

  • REPLACE replacing an existing row -> 2 (delete + insert)
  • ON DUPLICATE KEY UPDATE updating a row -> 2
  • ON DUPLICATE KEY UPDATE no-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: 0

Additional information

  • Root cause is at the planner/executor level, independent of ROW_COUNT(). The final affected-rows is Scope.affectedRows() (pkg/sql/compile/compile.go:5122), which sums GetAffectedRows() only over operators whose AddAffectedRows == true. Secondary/hidden-index branches set it to false by design — see the comment at pkg/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 reports 1 instead of 2.
  • For ON DUPLICATE KEY UPDATE, the operator reports 1 for insert/update/no-op alike; it does not implement MySQL's 2 (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 just ROW_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.

Activity

  1. added
    kind/bug
    on Jun 9, 2026
  2. self-assigned this
    on Jun 11, 2026
  3. added
    severity/s0
    on Jun 30, 2026
  4. added this to the MO-202607 milestone on Jun 30, 2026
  5. added
    ai-heavy
    ai-medium
    and removed
    ai-heavy
    on Jul 1, 2026
  6. assigned and unassigned on Jul 10, 2026
  7. ghost closed this as completedon Jul 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    ai-mediumkind/bugseverity/s0

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions