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

`SELECT DISTINCT` returns duplicate rows for statistical aggregates grouped by an unprojected key

    XMLWordPrintable

Details

    Description

      MariaDB returns duplicate rows from a `SELECT DISTINCT` query when a
      statistical aggregate is projected and the `GROUP BY` contains an additional
      unprojected key.

      Each group in the reduced example has the same variance, date, and projected
      modulo value. The groups differ only by `g`, which is intentionally retained as
      a grouping key but is not part of the result. The two projected rows are
      therefore identical and `DISTINCT` must collapse them into one row. MariaDB
      12.3.2 instead returns both copies.

      Moving the grouped relation behind a derived table and applying `DISTINCT` in
      the outer query returns the correct single row. The issue also affects
      `STDDEV_SAMP`, `VAR_POP`, and `STDDEV_POP` in the same query shape.

      Two independently generated VECT queries exposed this same defect. One query
      using `STD` (an alias of `STDDEV_POP`) returned 64 copies of one completely
      identical row, although `DISTINCT` should have returned one row. A second query
      using `VAR_SAMP` returned 100 rows containing only 89 distinct projected rows;
      11 rows should have been eliminated. Both reduce to the query shape below, so
      they are treated as two triggers for one database bug rather than as separate
      bugs.

      1. How to repeat

      The following setup is self-contained:

       
      ```sql
      DROP DATABASE IF EXISTS distinct_statistical_aggregate_test;
      CREATE DATABASE distinct_statistical_aggregate_test;
      USE distinct_statistical_aggregate_test;
       
      CREATE TABLE samples (
          g INT,
          x INT,
          d DATE
      );
       
      INSERT INTO samples VALUES
          (1, 10, '2025-01-01'),
          (1, 10, '2025-01-01'),
          (2, 20, '2025-01-01'),
          (2, 20, '2025-01-01');
      ```
      
      

      Run the direct `DISTINCT` query:

      ```sql
      SELECT DISTINCT
             VAR_SAMP(x) AS variance_value,
             d,
             MOD(g, g) AS modulo_value
      FROM samples
      GROUP BY d, MOD(g, g), g;
      ```
      
      

      MariaDB 12.3.2 returns two identical rows:

       
      ```text
      +----------------+------------+--------------+
      | variance_value | d          | modulo_value |
      +----------------+------------+--------------+
      |              0 | 2025-01-01 |            0 |
      |              0 | 2025-01-01 |            0 |
      +----------------+------------+--------------+
      ```
      
      

      Apply `DISTINCT` outside the grouped relation as a control:

       
      ```sql
      SELECT DISTINCT variance_value, d, modulo_value
      FROM (
          SELECT VAR_SAMP(x) AS variance_value,
                 d,
                 MOD(g, g) AS modulo_value
          FROM samples
          GROUP BY d, MOD(g, g), g
      ) AS grouped_result;
      ```
      

      The control query correctly returns one row:

       
      ```text
      +----------------+------------+--------------+
      | variance_value | d          | modulo_value |
      +----------------+------------+--------------+
      |              0 | 2025-01-01 |            0 |
      +----------------+------------+--------------+
      ```
      
      

      The direct query also returns duplicate rows when `VAR_SAMP` is replaced
      with any of the following expressions:

       
      ```text
      STDDEV_SAMP(x)
      VAR_POP(x)
      STDDEV_POP(x)
      ```
      
      

      `AVG(x - x)` returns the correct single row in the same query shape.

      1. Expected result

      The direct and derived-table forms should both return one row. Although the
      grouped relation has two groups, their projected values are identical, so
      `SELECT DISTINCT` must eliminate the duplicate:

       
      ```text
      0, 2025-01-01, 0
      ```
      
      

      1. Actual result

      MariaDB 12.3.2 returns two identical rows from the direct query. Applying the
      same `DISTINCT` operation outside a derived table returns the correct single
      row.

      Attachments

        Activity

          People

            psergei Sergei Petrunia
            chen7897 cl hl
            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.