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

Logic bug: Reverse scan + ICP returns empty result

    XMLWordPrintable

Details

    • Unexpected results
    • Q4/2026 Server Development

    Description

      On a partitioned MyISAM table with a non-unique `(o,v)` index, a descending
      range scan with `index_condition_pushdown=on` returns an empty result for
      `WHERE o >= 7 AND v = 1 ORDER BY o DESC, v DESC`, even though the ascending
      query over the same predicate returns the qualifying rows. Forcing the query to
      avoid the `(o,v)` index returns the expected rows, which points to a wrong-result
      bug in the indexed descending scan path.

      SET SESSION optimizer_switch='index_condition_pushdown=on,derived_merge=off';
       
      DROP DATABASE IF EXISTS db;
      CREATE DATABASE db;
      USE db;
       
      CREATE TABLE t (
        id INT NOT NULL,
        p INT NOT NULL,
        o INT NOT NULL,
        v INT NOT NULL,
        KEY k_o_v(o,v)
      ) ENGINE=MyISAM
      PARTITION BY HASH(p) PARTITIONS 2;
       
      INSERT INTO t VALUES
        (1,0,10,0),
        (2,0,9,1),
        (3,0,8,0),
        (4,0,7,1),
        (5,1,10,1),
        (6,1,9,0),
        (7,1,8,1),
        (8,1,7,0);
       
      SELECT 'ASC_CONTROL_QUERY';
      SELECT id,p,o,v
      FROM t
      WHERE o >= 7 AND v = 1
      ORDER BY o ASC, v ASC;
       
      SELECT 'BUGGY_BASE_QUERY';
      EXPLAIN SELECT id,p,o,v
      FROM t
      WHERE o >= 7 AND v = 1
      ORDER BY o DESC, v DESC;
      SELECT id,p,o,v
      FROM t
      WHERE o >= 7 AND v = 1
      ORDER BY o DESC, v DESC;
       
      SELECT 'SOURCE_QUERY';
      SELECT id,p,o,v,ROW_NUMBER() OVER (ORDER BY o DESC, v DESC) AS rn
      FROM (
        SELECT id,p,o,v
        FROM t
        WHERE o >= 7 AND v = 1
        ORDER BY o DESC, v DESC
        LIMIT 18446744073709551615
      ) AS s
      ORDER BY o DESC, v DESC;
       
      SELECT 'REFERENCE_QUERY';
      SELECT s1.id,s1.p,s1.o,s1.v,
             1 + (
               SELECT COUNT(*)
               FROM t AS s2 IGNORE INDEX (k_o_v)
               WHERE s2.o >= 7
                 AND s2.v = 1
                 AND (s2.o > s1.o OR (s2.o = s1.o AND s2.v > s1.v))
             ) AS rn
      FROM t AS s1 IGNORE INDEX (k_o_v)
      WHERE s1.o >= 7
        AND s1.v = 1
      ORDER BY s1.o DESC, s1.v DESC;
       
      DROP DATABASE db;
      

      Expected Result

      The descending query should return the same four rows as the reference query in
      descending `(o,v)` order.

      Actual Result

      The descending indexed query returns an empty rowset; the `IGNORE INDEX`
      reference returns four rows.

      Attachments

        Issue Links

          Activity

            People

              psergei Sergei Petrunia
              imchifan 刘启帆
              Dave Gosselin Dave Gosselin
              Votes:
              0 Vote for this issue
              Watchers:
              5 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Time Tracking

                  Estimated:
                  Original Estimate - 0d
                  0d
                  Remaining:
                  Remaining Estimate - 0d
                  0d
                  Logged:
                  Time Spent - 6.55h
                  6.55h

                  Git Integration

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