[MDEV-30717] ROW_COUNT() after REPLACE of duplicit row returns 1 Created: 2023-02-23  Updated: 2023-11-28

Status: Confirmed
Project: MariaDB Server
Component/s: Server
Affects Version/s: 10.6.11
Fix Version/s: 10.6, 10.11

Type: Bug Priority: Major
Reporter: Martin Tomec Assignee: Oleksandr Byelkin
Resolution: Unresolved Votes: 0
Labels: replace


 Description   

On table with single unique index:

CREATE TABLE `test` (
  `id` int(11) NOT NULL,
  `value` int(11) NOT NULL,
  UNIQUE KEY `id` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_general_ci

ROW_COUNT() returns allways 1, even for duplicate rows:

REPLACE INTO test VALUES (1,1); SELECT ROW_COUNT(); REPLACE INTO test VALUES (1,1); SELECT ROW_COUNT();
Query OK, 1 row affected (0.000 sec)
 
+-------------+
| ROW_COUNT() |
+-------------+
|           1 |
+-------------+
1 row in set (0.000 sec)
 
Query OK, 1 row affected (0.000 sec)
 
+-------------+
| ROW_COUNT() |
+-------------+
|           1 |
+-------------+
1 row in set (0.000 sec)

On table with multiple indexes:

CREATE TABLE `test` (
  `id` int(11) NOT NULL,
  `value` int(11) NOT NULL,
  UNIQUE KEY `id` (`id`),
  UNIQUE KEY `value` (`value`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_general_ci

ROW_COUNT() returns correct result according to documentation (1 and then 2):

REPLACE INTO test VALUES (1,1); SELECT ROW_COUNT(); REPLACE INTO test VALUES (1,1); SELECT ROW_COUNT();
Query OK, 1 row affected (0.000 sec)
 
+-------------+
| ROW_COUNT() |
+-------------+
|           1 |
+-------------+
1 row in set (0.000 sec)
 
Query OK, 2 rows affected (0.000 sec)
 
+-------------+
| ROW_COUNT() |
+-------------+
|           2 |
+-------------+
1 row in set (0.000 sec)



 Comments   
Comment by Weijun Huang [ 2023-02-23 ]

I am not sure if it is a bug. Because in MySQL 8.0, the result is as same as MariaDB.

Comment by Martin Tomec [ 2023-02-23 ]

So it seems, that the same bug is in MySQL 8.0.
REPLACE should delete row with the same unique key and insert new row. And as stated in documentation of REPLACE:

If the count is 1 for a single-row REPLACE, a row was inserted and no rows were deleted

In this case, no new rows was inserted, so the correct result according to documentation is 2 or 0.

Comment by Sergei Golubchik [ 2023-03-26 ]

shows the correct value (2) for MyISAM

Generated at Thu Feb 08 10:18:22 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.