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

COUNT(DISTINCT) returns 2 instead of 1 for a self-join with a constant equality predicate when big_tables=1

    XMLWordPrintable

Details

    • Bug
    • Status: Confirmed (View Workflow)
    • Major
    • Resolution: Unresolved
    • 10.11, 11.4, 11.8, 11.8.9
    • 10.11, 11.4, 11.8
    • Optimizer
    • Linux x86_64, MariaDB 11.8.9 binary distribution
    • Unexpected results

    Description

      On MariaDB 11.8.9, enabling big_tables causes COUNT(DISTINCT t.a) to return 2 for a self-join filtered by t.a = 1. All qualifying rows have t.a = 1, so the distinct count should be 1.

      Replacing the literal with an equivalent scalar COUNT() subquery returns the correct result. Disabling big_tables also restores the correct result.

      The discrepancy was verified using the native mariadb command-line client in a fresh instance.

      Reproducer:

      Run the following statements in an empty database, using the same connection:

      CREATE TABLE t (
      a INT PRIMARY KEY
      ) ENGINE=InnoDB;
       
      INSERT INTO t VALUES (1), (2);
       
      SET SESSION big_tables = 1;
       
      SELECT COUNT(DISTINCT t.a) AS result
      FROM t, t AS t2
      WHERE t.a = 1;
       
      SELECT COUNT(DISTINCT t.a) AS result
      FROM t, t AS t2
      WHERE t.a = CAST(
      (SELECT COUNT(*) FROM (SELECT 1) AS s)
      AS SIGNED
      );
       
      SET SESSION big_tables = 0;
       
      SELECT COUNT(DISTINCT t.a) AS result
      FROM t, t AS t2
      WHERE t.a = 1;
      

      Actual result on 11.8.9-MariaDB:

      Query 1, with big_tables=1 and a literal predicate:

      result
      2
      

      Query 2, with big_tables=1 and an equivalent scalar-subquery predicate:

      result
      1
      

      Query 3, after setting big_tables=0:

      result
      1
      

      Expected result:

      All three queries should return:

      result
      1
      

      The self-join produces two qualifying rows, but t.a is 1 in both rows. COUNT(DISTINCT t.a) must therefore return 1.

      The scalar subquery also returns exactly 1, so replacing the literal equality bound must not change the aggregate result.

      Attachments

        Issue Links

          Activity

            People

              psergei Sergei Petrunia
              jxgui jinxingui
              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.