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

Wrong result (rows missing) for SELECT DISTINCT using loose index scan with a predicate of the form A AND (A OR B)

    XMLWordPrintable

Details

    • Bug
    • Status: Confirmed (View Workflow)
    • Major
    • Resolution: Unresolved
    • 10.11, 11.4, 11.8, 12.3, 13.1, 12.3.3, 13.0.2, 13.1.1
    • 10.11, 11.4, 11.8, 12.3, 13.1
    • Optimizer
    • Ubuntu 24.04 x86_64
    • Unexpected results
    • Hide
      SELECT DISTINCT could return too few rows, or none, when it was executed with a loose index scan ("Using index for group-by") and the WHERE clause repeated a condition in the form A AND (A OR B). The same query without DISTINCT returned all rows.
      Show
      SELECT DISTINCT could return too few rows, or none, when it was executed with a loose index scan ("Using index for group-by") and the WHERE clause repeated a condition in the form A AND (A OR B). The same query without DISTINCT returned all rows.

    Description

      SELECT DISTINCT loses rows when the plan is "Using index for group-by" and the WHERE clause has the form A AND (A OR B). The same query without DISTINCT, and DISTINCT with only A, both return the two expected rows. A AND (A OR B) is equivalent to A.

      The wrong output differs between runs of the same script on a new table: the query returns either an empty set or only the row (1,0). It never returned both rows. On 13.0.2 and 13.1.1 both outputs were common (13.0.2: empty in 16 of 40 runs, one row in 24; 13.1.1: empty in 9 of 20, one row in 11); on 12.3.3 the single row was the usual output and the empty set was rare. Repeating the query on the same table gives the same output.

      The table is InnoDB with an ascending index; no setting was changed.

      How to reproduce

      CREATE TABLE t(a INT UNIQUE, b INT NOT NULL, KEY(b, a));
      INSERT INTO t VALUES (0, 0), (1, 0);
       
      SELECT a, b FROM t WHERE a IN (0,1) AND (a IN (0,1) OR b = 5);
      -- (0,0), (1,0)   correct
       
      SELECT DISTINCT a, b FROM t WHERE a IN (0,1);
      -- (0,0), (1,0)   correct
       
      SELECT DISTINCT a, b FROM t WHERE a IN (0,1) AND (a IN (0,1) OR b = 5);
      -- expected: (0,0), (1,0)
      -- actual:   Empty set, or only (1,0), depending on the run
       
      EXPLAIN SELECT DISTINCT a, b FROM t WHERE a IN (0,1) AND (a IN (0,1) OR b = 5);
      -- type=range  key=b  Extra: Using where; Using index for group-by; Using temporary
      

      Attachments

        Issue Links

          Activity

            People

              psergei Sergei Petrunia
              Ann C Ann C
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:

                Git Integration

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