Uploaded image for project: 'MariaDB Server'
  1. MariaDB Server
  2. MDEV-42212

UPDATE ... RETURNING ... INTO raises ER_TOO_MANY_ROWS on InnoDB when the second found row needs no update

    XMLWordPrintable

Details

    • 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

          Activity

            People

              Unassigned Unassigned
              bar Alexander Barkov
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Git Integration

                  Error rendering 'com.xiplink.jira.git.jira_git_plugin:git-issue-webpanel'. Please contact your Jira administrators.