[MDEV-30586] DELETE with aggregation in subquery of WHERE returns bogus error Created: 2023-02-07  Updated: 2023-03-16  Resolved: 2023-03-14

Status: Closed
Project: MariaDB Server
Component/s: Data Manipulation - Delete
Affects Version/s: 10.4, 10.5, 10.6, 10.7, 10.8, 10.9, 10.10, 10.11
Fix Version/s: 10.11.3, 11.0.2, 10.4.29, 10.5.20, 10.6.13, 10.8.8, 10.9.6, 10.10.4

Type: Bug Priority: Critical
Reporter: Igor Babaev Assignee: Igor Babaev
Resolution: Fixed Votes: 0
Labels: None

Issue Links:
Problem/Incident
causes MDEV-30668 Set function aggregated in outer sele... Closed

 Description   

DELETE with nested subquery in HAVING that contains set function aggregated in outer subquery returns a bogus error message while SELECT and UPDATE with the same WHERE condition work fine:

MariaDB [test]> delete from t1
    ->   where t1.a in (select t3.a from t3 group by t3.a
    ->                    having t3.a > any (select t2.b from t2 
    ->                                         where t2.b*10 < sum(t3.b)));
ERROR 1111 (HY000): Invalid use of group function
 
MariaDB [test]> select * from t1
    ->   where t1.a in (select t3.a from t3 group by t3.a
    ->                    having t3.a > any (select t2.b from t2 
    ->                                         where t2.b*10 < sum(t3.b)));
+------+
| a    |
+------+
|    7 |
+------+
1 row in set (0.003 sec)
 
MariaDB [test]> update t1 set t1.a=t1.a+10
    ->   where t1.a in (select t3.a from t3 group by t3.a
    ->                    having t3.a > any (select t2.b from t2 
    ->                                         where t2.b*10 < sum(t3.b)));
Query OK, 1 row affected (0.006 sec)
Rows matched: 1  Changed: 1  Warnings: 0

Here's a test case to reproduce the bug

create table t1 (a int);
insert into t1 values (3), (7), (1);
 
create table t2 (b int);
insert into t2 values (2), (1), (4), (7);
 
create table t3 (a int, b int);
insert into t3 values (2,10), (7,30), (2,30), (1,10), (7,40);
 
 
delete from t1
  where t1.a in (select t3.a from t3 group by t3.a
                   having t3.a > any (select t2.b from t2 
                                        where t2.b*10 < sum(t3.b)));
 
select * from t1
  where t1.a in (select t3.a from t3 group by t3.a
                   having t3.a > any (select t2.b from t2 
                                        where t2.b*10 < sum(t3.b)));
 
update t1 set t1.a=t1.a+10
  where t1.a in (select t3.a from t3 group by t3.a
                   having t3.a > any (select t2.b from t2 
                                        where t2.b*10 < sum(t3.b)));
 
drop table t1,t2,t3;

The bug is reproducible with any engine with version 10.4 and up.
Version 10.3 works as expected

MariaDB [test]> delete from t1
    ->   where t1.a in (select t3.a from t3 group by t3.a
    ->                    having t3.a > any (select t2.b from t2 
    ->                                         where t2.b*10 < sum(t3.b)));
Query OK, 1 row affected (0.005 sec)
 
MariaDB [test]> select * from t1
    ->   where t1.a in (select t3.a from t3 group by t3.a
    ->                    having t3.a > any (select t2.b from t2 
    ->                                         where t2.b*10 < sum(t3.b)));
Empty set (0.002 sec)
 
MariaDB [test]> update t1 set t1.a=t1.a+10
    ->   where t1.a in (select t3.a from t3 group by t3.a
    ->                    having t3.a > any (select t2.b from t2 
    ->                                         where t2.b*10 < sum(t3.b)));
Query OK, 0 rows affected (0.002 sec)
Rows matched: 0  Changed: 0  Warnings: 0



 Comments   
Comment by Oleksandr Byelkin [ 2023-02-22 ]

OK to push

Comment by Igor Babaev [ 2023-03-14 ]

A fix for this bug was pushed into 10.4

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