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

Partitioning changes the query result for an indexed virtual column: ORDER BY ... LIMIT gives the wrong row, and DELETE or UPDATE ... ORDER BY ... LIMIT changes it

    XMLWordPrintable

Details

    Description

      Likely cause: old code, not a recent change. CS 10.4.28 has the bug too, while MySQL 5.7, 8.0 and 9.1 give the right rows.

      --source include/have_partition.inc
      --source include/have_innodb.inc
      CREATE TABLE t1 (id INT PRIMARY KEY, t VARCHAR(10), v VARCHAR(20) AS (CONCAT(t, 'x')) VIRTUAL, KEY (v)) ENGINE=InnoDB PARTITION BY HASH (id) PARTITIONS 2;
      INSERT INTO t1 (id, t) VALUES (2, 'e'), (4, 'a'), (1, 'b');
      --let $t= query_get_value(SELECT t FROM t1 ORDER BY v LIMIT 1, t, 1)
      DELETE FROM t1 ORDER BY v LIMIT 1;
      --let $n= query_get_value(SELECT COUNT(*) AS n FROM t1 WHERE id = 4, n, 1)
      if ($n != 0)
      {
        --die The lowest v is ax, but ORDER BY v LIMIT 1 gives the row with t = $t, and DELETE ... ORDER BY v LIMIT 1 keeps the ax row
      }
      CREATE TABLE t2 (a INT NOT NULL, b INT NOT NULL, v INT AS (b) VIRTUAL, KEY kv (v)) ENGINE=InnoDB PARTITION BY HASH (a) PARTITIONS 2;
      INSERT INTO t2 (a, b) VALUES (1, 2), (2, 1), (3, 1), (4, 2);
      --let $c= query_get_value(SELECT COUNT(a) AS c FROM t2 GROUP BY v, c, 1)
      if ($c != 2)
      {
        --die The first group of GROUP BY v counts $c row, not 2, as the partitioned table gives a group for each partition
      }
      DROP TABLE t1, t2;

      Leads to:

      CS 13.1.2 c8e6ab229f030119c19f3208d40f415decb41dae (Debug, Clang 22.1.8-20260622) Build 19/11/2026

      mysqltest: At line 10: The lowest v is ax, but ORDER BY v LIMIT 1 gives the row with t = b, and DELETE ... ORDER BY v LIMIT 1 keeps the ax row

      Partitioning changes the query result, and the rows that a DELETE or an UPDATE changes. Without partitioning, ORDER BY v LIMIT 1 gives the row id = 4 (v = 'ax').

      With partitioning, the DELETE removes the row id = 1 (v = 'bx'), and UPDATE t1 SET t = 'z' ORDER BY v LIMIT 1 changes that row in the same way. ORDER BY CONCAT(t, 'x') LIMIT 1, which does not name v, also gives the row id = 1, as the optimizer uses the index on v for it. MyISAM gives the same wrong rows. Run alone, the GROUP BY case fails too: SELECT v, COUNT(a) FROM t2 GROUP BY v gives 4 groups of 1 row, and EXPLAIN shows type index on kv, without "Using index". A STORED column and a read from the index alone give the right result. Optimized builds give the same result.

      MySQL

      +--------+--------------------+--------------------+------------------+--------+
      | MySQL  | ORDER BY v LIMIT 1 | The DELETE removes | GROUP BY v gives | Engine |
      +--------+--------------------+--------------------+------------------+--------+
      | 5.7.44 | t = a              | id 4               | 2 groups         | InnoDB |
      | 8.0.36 | t = a              | id 4               | 2 groups         | InnoDB |
      | 9.1.0  | t = a              | id 4               | 2 groups         | InnoDB |
      +--------+--------------------+--------------------+------------------+--------+

      From the code it seems that an ordered index scan reads the rows of each partition into a record buffer of that partition, in ha_partition::handle_ordered_index_scan() and ha_partition::handle_ordered_next(). handler::ha_index_first() and the other index reads compute the virtual columns only for a row read into table->record[0]. The priority queue then orders the rows with cmp_key_part_id() and key_rec_cmp(), on values of v that were not computed for these rows.

      Attachments

        Issue Links

          Activity

            People

              ycp Yuchen Pei
              Roel Roel Van de Paar
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

                Created:
                Updated:

                Time Tracking

                  Estimated:
                  Original Estimate - Not Specified
                  Not Specified
                  Remaining:
                  Remaining Estimate - Not Specified
                  Not Specified
                  Logged:
                  Time Spent - 0.5h
                  0.5h

                  Git Integration

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