Skip to content

Commit f69278b

Browse files
author
Varun Gupta
committed
MDEV-16230: Server crashes when Analyze format=json is run with a window function with empty PARTITION BY and ORDER BY clauses
Currently when both PARTITION BY and ORDER BY clauses are empty then we create a Item with the first field in the select list and sort with that field. It should be created as an Item_temptable_field instead of Item_field because the print() function continues to work even if the table has been dropped.
1 parent eba2d10 commit f69278b

File tree

3 files changed

+60
-1
lines changed

3 files changed

+60
-1
lines changed

mysql-test/r/win.result

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3743,5 +3743,50 @@ a ROW_NUMBER() OVER v2
37433743
1 1
37443744
drop table t0;
37453745
#
3746+
# MDEV-16230:Server crashes when Analyze format=json is run with a window function with
3747+
# empty PARTITION BY and ORDER BY clauses
3748+
#
3749+
CREATE TABLE t1(a INT, b INT);
3750+
INSERT INTO t1 VALUES (1,1),(2,2),(3,3);
3751+
ANALYZE FORMAT=JSON SELECT row_number() OVER() FROM t1;
3752+
ANALYZE
3753+
{
3754+
"query_block": {
3755+
"select_id": 1,
3756+
"r_loops": 1,
3757+
"r_total_time_ms": "REPLACED",
3758+
"window_functions_computation": {
3759+
"sorts": {
3760+
"filesort": {
3761+
"sort_key": "`row_number() OVER()`",
3762+
"r_loops": 1,
3763+
"r_total_time_ms": "REPLACED",
3764+
"r_used_priority_queue": false,
3765+
"r_output_rows": 3,
3766+
"r_buffer_size": "REPLACED"
3767+
}
3768+
},
3769+
"temporary_table": {
3770+
"table": {
3771+
"table_name": "t1",
3772+
"access_type": "ALL",
3773+
"r_loops": 1,
3774+
"rows": 3,
3775+
"r_rows": 3,
3776+
"r_total_time_ms": "REPLACED",
3777+
"filtered": 100,
3778+
"r_filtered": 100
3779+
}
3780+
}
3781+
}
3782+
}
3783+
}
3784+
SELECT row_number() OVER() FROM t1;
3785+
row_number() OVER()
3786+
1
3787+
2
3788+
3
3789+
DROP TABLE t1;
3790+
#
37463791
# End of 10.2 tests
37473792
#

mysql-test/t/win.test

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2446,6 +2446,19 @@ WINDOW v2 AS ( PARTITION BY a ORDER BY a DESC );
24462446

24472447
drop table t0;
24482448

2449+
--echo #
2450+
--echo # MDEV-16230:Server crashes when Analyze format=json is run with a window function with
2451+
--echo # empty PARTITION BY and ORDER BY clauses
2452+
--echo #
2453+
2454+
CREATE TABLE t1(a INT, b INT);
2455+
INSERT INTO t1 VALUES (1,1),(2,2),(3,3);
2456+
2457+
--source include/analyze-format.inc
2458+
ANALYZE FORMAT=JSON SELECT row_number() OVER() FROM t1;
2459+
SELECT row_number() OVER() FROM t1;
2460+
DROP TABLE t1;
2461+
24492462
--echo #
24502463
--echo # End of 10.2 tests
24512464
--echo #

sql/sql_window.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2872,7 +2872,8 @@ bool Window_funcs_sort::setup(THD *thd, SQL_SELECT *sel,
28722872
*/
28732873
ORDER *order= (ORDER *)alloc_root(thd->mem_root, sizeof(ORDER));
28742874
memset(order, 0, sizeof(*order));
2875-
Item *item= new (thd->mem_root) Item_field(thd, join_tab->table->field[0]);
2875+
Item *item= new (thd->mem_root) Item_temptable_field(thd,
2876+
join_tab->table->field[0]);
28762877
order->item= (Item **)alloc_root(thd->mem_root, 2 * sizeof(Item *));
28772878
order->item[1]= NULL;
28782879
order->item[0]= item;

0 commit comments

Comments
 (0)