Details
-
Bug
-
Status: Closed (View Workflow)
-
Major
-
Resolution: Fixed
-
13.2
-
None
-
Unexpected results
Description
With InnoDB, RETURNING ... INTO raises ER_TOO_MANY_ROWS for a second row that is found but not changed, if the statement assigns a column without reading it. Plain UPDATE ... RETURNING returns only the changed row. MyISAM, Aria and MEMORY are not affected. Related to MDEV-42156, which needs IGNORE; this does not.
How to repeat
--source include/have_innodb.inc
|
|
|
CREATE TABLE t1 (a INT, b INT) ENGINE=InnoDB; |
INSERT INTO t1 VALUES (1,1),(2,5); |
UPDATE t1 SET b=IF(a=1,100,5) RETURNING a; # one row: a=1 |
TRUNCATE TABLE t1; |
INSERT INTO t1 VALUES (1,1),(2,5); |
UPDATE t1 SET b=IF(a=1,100,5) RETURNING a INTO @a; |
DROP TABLE t1; |
Actual result: the INTO statement fails with ERROR 1172: Result consisted of more than one row, and the statement is rolled back.
Expected result: @a = 1, and the table is (1,100), (2,5). With ENGINE=MyISAM this is what happens.
Analysis
In Sql_cmd_update::update_single_table() the "second row" check runs when need_update is true. b is written but not read, so InnoDB (HA_PARTIAL_COLUMN_READ) cannot compare the records and can_compare_record is false. Row 2 counts as changed, and ER_TOO_MANY_ROWS is raised before ha_update_row() could return HA_ERR_RECORD_IS_THE_SAME.
Workarounds: read the column (SET b=IF(a=1,100,b)), match one row, or use another engine.
Suggested direction: for transactional engines, run the check after ha_update_row(). A really changed second row then rolls the statement back.
Summary for InnoDB and MyISAM
┌──────────────────────────┬───────────────────────┬───────────────────────┐
|
│ Step │ MyISAM │ InnoDB │
|
├──────────────────────────┼───────────────────────┼───────────────────────┤
|
│ Plain RETURNING a │ one row: a = 1 │ one row: a = 1 │
|
├──────────────────────────┼───────────────────────┼───────────────────────┤
|
│ Table after it │ (1,100), (2,5) │ (1,100), (2,5) │
|
├──────────────────────────┼───────────────────────┼───────────────────────┤
|
│ RETURNING a INTO @a │ works, no warning │ ERROR 1172 (42000): │
|
│ │ │ Result consisted of │
|
│ │ │ more than one row │
|
├──────────────────────────┼───────────────────────┼───────────────────────┤
|
│ SELECT @a │ 1 │ 1 │
|
├──────────────────────────┼───────────────────────┼───────────────────────┤
|
│ Table after the INTO │ (1,100), (2,5) │ (1,1), (2,5) │
|
│ statement │ │ (rolled back) │
|
└──────────────────────────┴───────────────────────┴───────────────────────┘
|
|
|
|
Attachments
Issue Links
- is caused by
-
MDEV-39563 Implement UPDATE ... RETURNING ... INTO
-
- Stalled
-
- relates to
-
MDEV-42156 UPDATE IGNORE ... RETURNING ... INTO fails with ER_TOO_MANY_ROWS when the second row is skipped by IGNORE (duplicate key)
-
- Closed
-