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

Materializing a grouped input changes the result of a window query with `HAVING SUM(DISTINCT ...)`

    XMLWordPrintable

Details

    • Bug
    • Status: Confirmed (View Workflow)
    • Major
    • Resolution: Unresolved
    • 10.11, 11.4, 11.8, 12.3, 12.3.2
    • 10.11, 11.4, 11.8, 12.3
    • None
    • None

    Description

      MariaDB returns different results before and after the grouped relational
      operator is materialized. The original query combines `GROUP BY`,
      `HAVING SUM(DISTINCT ...)`, and a window function and returns an empty result.
      An equivalent query that first materializes the grouped aggregate into a
      temporary table returns all three expected rows.

      Every group has `SUM(DISTINCT a.c2) = 9`, so the condition
      `SUM(DISTINCT a.c2) <= 67` is true. The original `HAVING` condition and the
      filter on the materialized aggregate therefore express the same predicate.
      Materializing the grouped operator must not change the relational result.

      1. How to repeat

      Create an isolated database and two small tables:

       
      ```sql
      DROP DATABASE IF EXISTS window_having_test;
      CREATE DATABASE window_having_test;
      USE window_having_test;
       
      CREATE TABLE a (
          c2 INT,
          c8 INT
      );
       
      CREATE TABLE b (
          c1 INT,
          c13 INT
      );
       
      INSERT INTO a VALUES (9, 51);
      INSERT INTO b VALUES (1, 1), (2, 2), (3, 3);
      ```
      
      

      Run the original query:

       
      ```sql
      SELECT SUM(DISTINCT a.c2) AS s,
             b.c1,
             ROW_NUMBER() OVER () AS rn
      FROM a
      CROSS JOIN b
      GROUP BY b.c1
      HAVING SUM(DISTINCT a.c2) <= 67;
      ```
      
      

      Result:

       
      ```text
      Empty set
      ```
      
      

      Now materialize the grouped aggregate operator into a temporary table:

       
      ```sql
      CREATE TEMPORARY TABLE grouped_cut AS
      SELECT SUM(DISTINCT a.c2) AS s,
             b.c1
      FROM a
      CROSS JOIN b
      GROUP BY b.c1;
      ```
      
      

      Apply the original aggregate predicate to the materialized aggregate column
      and retain the same window function:

       
      ```sql
      SELECT grouped_cut.s,
             grouped_cut.c1,
             ROW_NUMBER() OVER () AS rn
      FROM grouped_cut
      WHERE grouped_cut.s <= 67;
      ```
      
      

      Result:

       
      ```text
      +------+------+----+
      | s    | c1   | rn |
      +------+------+----+
      |    9 |    1 |  1 |
      |    9 |    2 |  2 |
      |    9 |    3 |  3 |
      +------+------+----+
      ```
      
      

      1. Expected result

      The original and materialized queries should return the same three rows.
      Window evaluation occurs after grouping and `HAVING`, and materializing the
      grouped operator does not change the value of any group:

       
      ```text
      9, 1, 1
      9, 2, 2
      9, 3, 3
      ```
      
      

      1. Actual result

      The original query returns an empty result, while the query using the
      materialized grouped operator returns three rows.

      Attachments

        Issue Links

          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.