Details
-
Bug
-
Status: Confirmed (View Workflow)
-
Major
-
Resolution: Unresolved
-
10.11, 11.4, 11.8, 12.3, 12.3.2
-
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.
- 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 | |
+------+------+----+
|
```
|
|
- 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 |
```
|
|
- Actual result
The original query returns an empty result, while the query using the
materialized grouped operator returns three rows.
Attachments
Issue Links
- relates to
-
MDEV-40779 `DISTINCT` + `char_length(SPACE(AVG(…)))` + `HAVING` returns empty for one insertion order of the same rows
-
- Confirmed
-