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

Window function with COUNT DISTINCT and HAVING loses all surviving groups

    XMLWordPrintable

Details

    • Bug
    • Status: Closed (View Workflow)
    • Major
    • Resolution: Duplicate
    • 12.3.2
    • N/A
    • Optimizer
    • None
    • Not for Release Notes

    Description

      Description

      MariaDB Server 12.3.2 returns no rows for a grouped COUNT DISTINCT query with DENSE_RANK and HAVING, although all three groups satisfy HAVING. Removing the window function, or replacing COUNT DISTINCT with ordinary COUNT, returns the surviving groups. Projecting the HAVING aggregates confirms their values satisfy both conditions.

      How to repeat

       
      CREATE TEMPORARY TABLE win_count (id INT,k VARCHAR(10),x DOUBLE,txt LONGTEXT);
      INSERT INTO win_count VALUES
          (1,'a',3.71,'one'),(2,'b',5.25,'two'),(3,'c',9.12,'three');
      CREATE TEMPORARY TABLE win_copies (v INT);
      INSERT INTO win_copies VALUES (1),(2),(3);
       
      SELECT DENSE_RANK() OVER (ORDER BY p.txt) AS r,COUNT(DISTINCT p.k) AS n
      FROM win_count p CROSS JOIN win_copies d GROUP BY p.txt
      HAVING AVG(p.id)<78 AND SUM(d.v)>=1;
      

      Expected result

      Three rows: `(1,1),(2,1),(3,1)`. Each group contains three copies of one source row and one distinct k. The group AVG(id) values are 1, 3 and 2, and SUM(d.v) is 6 for every group, so every group satisfies HAVING.

      Actual result

      No rows.

      Attachments

        Issue Links

          Activity

            People

              Unassigned Unassigned
              chen7897 cl hl
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Git Integration

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