Skip to content

Commit 84c3a20

Browse files
committed
Fixed bug mdev-9754.
Each window name has to be resolved only once.
1 parent 6533bd1 commit 84c3a20

File tree

3 files changed

+68
-1
lines changed

3 files changed

+68
-1
lines changed

mysql-test/r/win.result

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1257,3 +1257,43 @@ a row_number() over (partition by a order by b)
12571257
3 1
12581258
3 2
12591259
drop table t1;
1260+
#
1261+
# mdev-9754: Window name resolution in prepared statement
1262+
#
1263+
create table t0 (a int);
1264+
insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
1265+
create table t1 (pk int, c int);
1266+
insert into t1 select a+1,1 from t0;
1267+
update t1 set c=2 where pk not in (1,2,3,4);
1268+
select * from t1;
1269+
pk c
1270+
1 1
1271+
2 1
1272+
3 1
1273+
4 1
1274+
5 2
1275+
6 2
1276+
7 2
1277+
8 2
1278+
9 2
1279+
10 2
1280+
prepare stmt from
1281+
'select
1282+
pk, c,
1283+
count(*) over w1 as CNT
1284+
from t1
1285+
window w1 as (partition by c order by pk
1286+
rows between 2 preceding and 2 following)';
1287+
execute stmt;
1288+
pk c CNT
1289+
1 1 3
1290+
2 1 4
1291+
3 1 4
1292+
4 1 3
1293+
5 2 3
1294+
6 2 4
1295+
7 2 5
1296+
8 2 5
1297+
9 2 4
1298+
10 2 3
1299+
drop table t0,t1;

mysql-test/t/win.test

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -785,7 +785,28 @@ execute stmt;
785785

786786
drop table t1;
787787

788+
--echo #
789+
--echo # mdev-9754: Window name resolution in prepared statement
790+
--echo #
788791

792+
create table t0 (a int);
793+
insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
794+
795+
create table t1 (pk int, c int);
796+
insert into t1 select a+1,1 from t0;
797+
update t1 set c=2 where pk not in (1,2,3,4);
798+
select * from t1;
799+
800+
prepare stmt from
801+
'select
802+
pk, c,
803+
count(*) over w1 as CNT
804+
from t1
805+
window w1 as (partition by c order by pk
806+
rows between 2 preceding and 2 following)';
807+
execute stmt;
808+
809+
drop table t0,t1;
789810

790811

791812

sql/item_windowfunc.cc

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
bool
88
Item_window_func::resolve_window_name(THD *thd)
99
{
10+
if (window_spec)
11+
{
12+
/* The window name has been already resolved */
13+
return false;
14+
}
1015
DBUG_ASSERT(window_name != NULL && window_spec == NULL);
1116
char *ref_name= window_name->str;
1217

@@ -15,7 +20,8 @@ Item_window_func::resolve_window_name(THD *thd)
1520
First look for the deinition of the window with 'window_name'
1621
in the current select
1722
*/
18-
List<Window_spec> curr_window_specs=thd->lex->current_select->window_specs;
23+
List<Window_spec> curr_window_specs=
24+
List<Window_spec> (thd->lex->current_select->window_specs);
1925
List_iterator_fast<Window_spec> it(curr_window_specs);
2026
Window_spec *win_spec;
2127
while((win_spec= it++))

0 commit comments

Comments
 (0)