[MDEV-32657] Dependent query cannot recognize expr in more than 1 nesting level Created: 2023-11-01  Updated: 2023-11-02

Status: Confirmed
Project: MariaDB Server
Component/s: None
Affects Version/s: 10.5, 10.6, 10.9
Fix Version/s: 10.11, 11.0, 11.1, 11.2

Type: Bug Priority: Major
Reporter: Michael Assignee: Oleksandr Byelkin
Resolution: Unresolved Votes: 0
Labels: None


 Description   

repro: https://dbfiddle.uk/1BpvGW2V

here is the same repro working for MySQL: https://dbfiddle.uk/wa8Kc48N
and for PostgreSQL: https://dbfiddle.uk/6Z-IfLMx

The failing query patten is important to be supported. In complex queries, it allows to deduplicate subqueries used later more than one without having to put them in the main select.

I would be grateful if it can be fixed.



 Comments   
Comment by Sergei Golubchik [ 2023-11-02 ]

Test case from the dbfiddle:

CREATE TABLE invoice (
  id INTEGER PRIMARY KEY AUTO_INCREMENT NOT NULL,
  amount DOUBLE DEFAULT NULL,
  name VARCHAR(100) DEFAULT NULL
);
INSERT INTO invoice (amount, name) 
VALUES (4.0, 'Michael'), (15.0, 'Bara'), (4.0, 'Michael'), (6.0, 'John');
SELECT sum(amount), name
from invoice
group by name
having sum(amount) > 6;
SELECT sum(amount), name
from invoice
group by name
having (select name = 'John'); -- refer parent data in subquery - ok!
SELECT sum(amount), name
from invoice
group by name
having (select sum(amount) > 6); -- refer parent aggregate data in subquery - ok!
SELECT sum(amount), name
from invoice
group by name
having (select sum(amount) > 6 from (select 1) t); -- still ok!
SELECT sum(amount), name
from invoice
group by name
having (select v > 6 from (select sum(amount) v) t); -- the same, only the `sum()` expr
                                                    -- is moved inside the subsubquery
SELECT sum(amount), name
from invoice
group by name
having (select v = 'John' from (select name v) t); -- even simple column reference
                                                  -- is broken

Generated at Thu Feb 08 10:33:00 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.