Skip to content

Commit c872125

Browse files
committed
MDEV-25630: Crash with window function in left expr of IN subquery
* Make Item_in_optimizer::fix_fields inherit the with_window_func attribute of the subquery's left expression (the subquery itself cannot have window functions that are aggregated in this select) * Make Item_cache_wrapper::Item_cache_wrapper() inherit with_window_func attribute of the item it is caching.
1 parent dfa2d0b commit c872125

File tree

5 files changed

+56
-0
lines changed

5 files changed

+56
-0
lines changed

mysql-test/r/win.result

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3892,5 +3892,24 @@ id rn
38923892
1 1
38933893
drop table t1;
38943894
#
3895+
# MDEV-25630: Crash with window function in left expr of IN subquery
3896+
#
3897+
CREATE TABLE t1 (i int);
3898+
INSERT INTO t1 VALUES (1),(2),(3);
3899+
SELECT lag(i) over (ORDER BY i) IN ( SELECT 1 FROM t1 a) FROM t1;
3900+
lag(i) over (ORDER BY i) IN ( SELECT 1 FROM t1 a)
3901+
NULL
3902+
1
3903+
0
3904+
DROP TABLE t1;
3905+
CREATE TABLE t1 (i int);
3906+
INSERT INTO t1 VALUES (1),(2),(3);
3907+
SELECT sum(i) over () IN ( SELECT 1 FROM t1 a) FROM t1;
3908+
sum(i) over () IN ( SELECT 1 FROM t1 a)
3909+
0
3910+
0
3911+
0
3912+
DROP TABLE t1;
3913+
#
38953914
# End of 10.2 tests
38963915
#

mysql-test/suite/encryption/r/tempfiles_encrypted.result

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3898,6 +3898,25 @@ id rn
38983898
1 1
38993899
drop table t1;
39003900
#
3901+
# MDEV-25630: Crash with window function in left expr of IN subquery
3902+
#
3903+
CREATE TABLE t1 (i int);
3904+
INSERT INTO t1 VALUES (1),(2),(3);
3905+
SELECT lag(i) over (ORDER BY i) IN ( SELECT 1 FROM t1 a) FROM t1;
3906+
lag(i) over (ORDER BY i) IN ( SELECT 1 FROM t1 a)
3907+
NULL
3908+
1
3909+
0
3910+
DROP TABLE t1;
3911+
CREATE TABLE t1 (i int);
3912+
INSERT INTO t1 VALUES (1),(2),(3);
3913+
SELECT sum(i) over () IN ( SELECT 1 FROM t1 a) FROM t1;
3914+
sum(i) over () IN ( SELECT 1 FROM t1 a)
3915+
0
3916+
0
3917+
0
3918+
DROP TABLE t1;
3919+
#
39013920
# End of 10.2 tests
39023921
#
39033922
#

mysql-test/t/win.test

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2542,6 +2542,20 @@ order by rn desc;
25422542

25432543
drop table t1;
25442544

2545+
--echo #
2546+
--echo # MDEV-25630: Crash with window function in left expr of IN subquery
2547+
--echo #
2548+
2549+
CREATE TABLE t1 (i int);
2550+
INSERT INTO t1 VALUES (1),(2),(3);
2551+
SELECT lag(i) over (ORDER BY i) IN ( SELECT 1 FROM t1 a) FROM t1;
2552+
DROP TABLE t1;
2553+
2554+
CREATE TABLE t1 (i int);
2555+
INSERT INTO t1 VALUES (1),(2),(3);
2556+
SELECT sum(i) over () IN ( SELECT 1 FROM t1 a) FROM t1;
2557+
DROP TABLE t1;
2558+
25452559
--echo #
25462560
--echo # End of 10.2 tests
25472561
--echo #

sql/item.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8203,6 +8203,7 @@ Item_cache_wrapper::Item_cache_wrapper(THD *thd, Item *item_arg):
82038203
name= item_arg->name;
82048204
name_length= item_arg->name_length;
82058205
with_subselect= orig_item->with_subselect;
8206+
with_window_func= orig_item->with_window_func;
82068207

82078208
if ((expr_value= Item_cache::get_cache(thd, orig_item)))
82088209
expr_value->setup(thd, orig_item);

sql/item_cmpfunc.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1416,6 +1416,9 @@ bool Item_in_optimizer::fix_fields(THD *thd, Item **ref)
14161416
maybe_null=1;
14171417
with_subselect= 1;
14181418
with_sum_func= with_sum_func || args[1]->with_sum_func;
1419+
with_window_func= args[0]->with_window_func;
1420+
// The subquery cannot have window functions aggregated in this select
1421+
DBUG_ASSERT(!args[1]->with_window_func);
14191422
with_field= with_field || args[1]->with_field;
14201423
with_param= args[0]->with_param || args[1]->with_param;
14211424
used_tables_and_const_cache_join(args[1]);

0 commit comments

Comments
 (0)