1
0
mirror of https://github.com/MariaDB/server.git synced 2025-07-30 16:24:05 +03:00

MDEV-28621: group by optimization incorrectly removing subquery where subject buried in a function

Workaround patch: Do not remove GROUP BY clause when it has
subquer(ies) in it.

remove_redundant_subquery_clauses() removes redundant GROUP BY clause
from queries in form:
  expr IN (SELECT no_aggregates GROUP BY ...)
  expr {CMP} {ALL|ANY|SOME} (SELECT no_aggregates GROUP BY ...)
This hits problems when the GROUP BY clause itself has subquer(y/ies).

This patch is just a workaround: it disables removal of GROUP BY clause
if the clause has one or more subqueries in it.

Tests:
- subselect_elimination.test has all known crashing cases.
- subselect4.result, insert_select.result are updated.
Note that in some cases results of SELECT are changed too (not just
EXPLAINs). These are caused by non-deterministic SQL: when running a
query like:

  x > ANY( SELECT col1 FROM t1 GROUP BY constant_expression)

without removing the GROUP BY, the executor is free to pick the value
of t1.col1 from any row in the GROUP BY group (denote it $COL1_VAL).
Then, it computes x > ANY(SELECT $COL1_VAL).

When running the same query and removing the GROUP BY:

   x > ANY( SELECT col1 FROM t1)

the executor will actually check all rows of t1.
This commit is contained in:
Sergei Petrunia
2024-05-07 14:10:35 +03:00
committed by Sergei Golubchik
parent ec6aa9ac42
commit 40b3525fcc
5 changed files with 599 additions and 65 deletions

View File

@ -973,7 +973,6 @@ select * from t1;
a
3
1
2
delete from t1;
insert into t1 values (3), (1);
insert into t1
@ -984,8 +983,6 @@ select * from t1;
a
3
1
3
2
delete from t1;
insert into t1 values (3), (1);
insert into t1
@ -996,6 +993,7 @@ select * from t1;
a
3
1
3
2
delete from t1;
insert into t1 values (3), (1);
@ -1022,7 +1020,6 @@ select * from t1;
a
3
1
2
delete from t1;
insert into t1 values (3), (1);
execute stmt;
@ -1030,7 +1027,6 @@ select * from t1;
a
3
1
2
delete from t1;
insert into t1 values (3), (1);
delete from t1
@ -1040,6 +1036,8 @@ group by (select * from (select a from t1) dt
where a = 1)));
select * from t1;
a
3
1
deallocate prepare stmt;
drop table t1,t2,t3;
#