Skip to content
Permalink
Browse files
MDEV-25565 Crash on 2-nd execution of SP/PS for query calculating win…
…dow functions

           from view

A crash of the server happened when executing a stored procedure whose the
only query calculated window functions over a mergeable view specified
as a select from non-mergeable view. The crash could be reproduced if
the window specifications of the window functions were identical and both
contained PARTITION lists and ORDER BY lists. A crash also happened on
the second execution of the prepared statement created for such query.
If to use derived tables or CTE instead of views the problem still
manifests itself crashing the server.

When optimizing the window specifications of a window function the
server can substitute the partition lists and the order lists for
the corresponding lists from another window specification in the case
when the lists are identical. This substitution is not permanent and should
be rolled back before the second execution. It was not done and this
ultimately led to a crash when resolving the column names at the second
execution of SP/PS.
  • Loading branch information
igorbabaev committed Jul 20, 2021
1 parent 872422d commit 4c38794
Show file tree
Hide file tree
Showing 5 changed files with 476 additions and 1 deletion.
@@ -3911,5 +3911,292 @@ sum(i) over () IN ( SELECT 1 FROM t1 a)
0
DROP TABLE t1;
#
# MDEV-25565: 2-nd call of SP with SELECT from view / derived table / CTE
# returning the result of calculation of 2 window
# functions that use the same window specification
#
create table t1 (a int);
insert into t1 values (3), (7), (1), (7), (1), (1), (3), (1), (5);
create view v2 as select a from t1 group by a;
create view v1 as select * from v2;
create procedure sp1() select v1.a,
sum(v1.a) over (partition by v1.a order by v1.a) as k,
avg(v1.a) over (partition by v1.a order by v1.a) as m
from v1;
call sp1();
a k m
1 1 1.0000
3 3 3.0000
5 5 5.0000
7 7 7.0000
call sp1();
a k m
1 1 1.0000
3 3 3.0000
5 5 5.0000
7 7 7.0000
prepare stmt from "select v1.a,
sum(v1.a) over (partition by v1.a order by v1.a) as k,
avg(v1.a) over (partition by v1.a order by v1.a) as m
from v1";
execute stmt;
a k m
1 1 1.0000
3 3 3.0000
5 5 5.0000
7 7 7.0000
execute stmt;
a k m
1 1 1.0000
3 3 3.0000
5 5 5.0000
7 7 7.0000
deallocate prepare stmt;
create procedure sp2() select * from
( select dt1.a,
sum(dt1.a) over (partition by dt1.a order by dt1.a) as k,
avg(dt1.a) over (partition by dt1.a order by dt1.a) as m
from (select * from v2) as dt1
) as dt;
call sp2();
a k m
1 1 1.0000
3 3 3.0000
5 5 5.0000
7 7 7.0000
call sp2();
a k m
1 1 1.0000
3 3 3.0000
5 5 5.0000
7 7 7.0000
prepare stmt from "select * from
( select dt1.a,
sum(dt1.a) over (partition by dt1.a order by dt1.a) as k,
avg(dt1.a) over (partition by dt1.a order by dt1.a) as m
from (select * from v2) as dt1
) as dt";
execute stmt;
a k m
1 1 1.0000
3 3 3.0000
5 5 5.0000
7 7 7.0000
execute stmt;
a k m
1 1 1.0000
3 3 3.0000
5 5 5.0000
7 7 7.0000
deallocate prepare stmt;
create procedure sp3() select * from
( select dt1.a,
sum(dt1.a) over (partition by dt1.a order by dt1.a) as k,
avg(dt1.a) over (partition by dt1.a order by dt1.a) as m
from ( select * from (select * from t1 group by a) as dt2 ) as dt1
) as dt;
call sp3();
a k m
1 1 1.0000
3 3 3.0000
5 5 5.0000
7 7 7.0000
call sp3();
a k m
1 1 1.0000
3 3 3.0000
5 5 5.0000
7 7 7.0000
prepare stmt from "select * from
( select dt1.a,
sum(dt1.a) over (partition by dt1.a order by dt1.a) as k,
avg(dt1.a) over (partition by dt1.a order by dt1.a) as m
from ( select * from (select * from t1 group by a) as dt2 ) as dt1
) as dt";
execute stmt;
a k m
1 1 1.0000
3 3 3.0000
5 5 5.0000
7 7 7.0000
execute stmt;
a k m
1 1 1.0000
3 3 3.0000
5 5 5.0000
7 7 7.0000
deallocate prepare stmt;
create procedure sp4() with cte1 as (select * from (select * from t1 group by a) as dt2),
cte as
( select cte1.a,
sum(cte1.a) over (partition by cte1.a order by cte1.a) as k,
avg(cte1.a) over (partition by cte1.a order by cte1.a) as m
from cte1 )
select * from cte;
call sp4();
a k m
1 1 1.0000
3 3 3.0000
5 5 5.0000
7 7 7.0000
call sp4();
a k m
1 1 1.0000
3 3 3.0000
5 5 5.0000
7 7 7.0000
prepare stmt from "with cte1 as (select * from (select * from t1 group by a) as dt2),
cte as
( select cte1.a,
sum(cte1.a) over (partition by cte1.a order by cte1.a) as k,
avg(cte1.a) over (partition by cte1.a order by cte1.a) as m
from cte1 )
select * from cte";
execute stmt;
a k m
1 1 1.0000
3 3 3.0000
5 5 5.0000
7 7 7.0000
execute stmt;
a k m
1 1 1.0000
3 3 3.0000
5 5 5.0000
7 7 7.0000
deallocate prepare stmt;
create procedure sp5() with cte1 as (select * from v2),
cte as
( select cte1.a,
sum(cte1.a) over (partition by cte1.a order by cte1.a) as k,
avg(cte1.a) over (partition by cte1.a order by cte1.a) as m
from cte1 )
select * from cte;
call sp5();
a k m
1 1 1.0000
3 3 3.0000
5 5 5.0000
7 7 7.0000
call sp5();
a k m
1 1 1.0000
3 3 3.0000
5 5 5.0000
7 7 7.0000
prepare stmt from "with cte1 as (select * from v2),
cte as
( select cte1.a,
sum(cte1.a) over (partition by cte1.a order by cte1.a) as k,
avg(cte1.a) over (partition by cte1.a order by cte1.a) as m
from cte1 )
select * from cte";
execute stmt;
a k m
1 1 1.0000
3 3 3.0000
5 5 5.0000
7 7 7.0000
execute stmt;
a k m
1 1 1.0000
3 3 3.0000
5 5 5.0000
7 7 7.0000
deallocate prepare stmt;
create procedure sp6() with
cte1 as (with cte2 as (select * from t1 group by a) select * from cte2),
cte as
( select cte1.a,
sum(cte1.a) over (partition by cte1.a order by cte1.a) as k,
avg(cte1.a) over (partition by cte1.a order by cte1.a) as m
from cte1 )
select * from cte;
call sp6();
a k m
1 1 1.0000
3 3 3.0000
5 5 5.0000
7 7 7.0000
call sp6();
a k m
1 1 1.0000
3 3 3.0000
5 5 5.0000
7 7 7.0000
prepare stmt from "with
cte1 as (with cte2 as (select * from t1 group by a) select * from cte2),
cte as
( select cte1.a,
sum(cte1.a) over (partition by cte1.a order by cte1.a) as k,
avg(cte1.a) over (partition by cte1.a order by cte1.a) as m
from cte1 )
select * from cte";
execute stmt;
a k m
1 1 1.0000
3 3 3.0000
5 5 5.0000
7 7 7.0000
execute stmt;
a k m
1 1 1.0000
3 3 3.0000
5 5 5.0000
7 7 7.0000
deallocate prepare stmt;
create procedure sp7() with
cte2 as (select * from v1),
cte1 as (select * from cte2),
cte as
( select cte1.a,
sum(cte1.a) over (partition by cte1.a order by cte1.a) as k,
avg(cte1.a) over (partition by cte1.a order by cte1.a) as m
from cte1 )
select * from cte;
call sp7();
a k m
1 1 1.0000
3 3 3.0000
5 5 5.0000
7 7 7.0000
call sp7();
a k m
1 1 1.0000
3 3 3.0000
5 5 5.0000
7 7 7.0000
prepare stmt from "with
cte2 as (select * from v1),
cte1 as (select * from cte2),
cte as
( select cte1.a,
sum(cte1.a) over (partition by cte1.a order by cte1.a) as k,
avg(cte1.a) over (partition by cte1.a order by cte1.a) as m
from cte1 )
select * from cte";
execute stmt;
a k m
1 1 1.0000
3 3 3.0000
5 5 5.0000
7 7 7.0000
execute stmt;
a k m
1 1 1.0000
3 3 3.0000
5 5 5.0000
7 7 7.0000
deallocate prepare stmt;
drop procedure sp1;
drop procedure sp2;
drop procedure sp3;
drop procedure sp4;
drop procedure sp5;
drop procedure sp6;
drop procedure sp7;
drop view v1,v2;
drop table t1;
#
# End of 10.2 tests
#

0 comments on commit 4c38794

Please sign in to comment.