Skip to content

Commit 2efabf8

Browse files
committed
MDEV-9847: Window functions: crash with big_tables=1
- Move filesort's sort_positions argument into class Filesort. - Make window function code construct Filesort with sort_positions=true.
1 parent 0a34dc1 commit 2efabf8

File tree

9 files changed

+60
-22
lines changed

9 files changed

+60
-22
lines changed

mysql-test/r/win.result

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1694,3 +1694,24 @@ EXPLAIN
16941694
}
16951695
}
16961696
drop table t1;
1697+
#
1698+
# MDEV-9847: Window functions: crash with big_tables=1
1699+
#
1700+
create table t1(a int);
1701+
insert into t1 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
1702+
set @tmp=@@big_tables;
1703+
set big_tables=1;
1704+
select rank() over (order by a) from t1;
1705+
rank() over (order by a)
1706+
1
1707+
2
1708+
3
1709+
4
1710+
5
1711+
6
1712+
7
1713+
8
1714+
9
1715+
10
1716+
set big_tables=@tmp;
1717+
drop table t1;

mysql-test/t/win.test

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1063,3 +1063,15 @@ from t1;
10631063

10641064
drop table t1;
10651065

1066+
1067+
--echo #
1068+
--echo # MDEV-9847: Window functions: crash with big_tables=1
1069+
--echo #
1070+
create table t1(a int);
1071+
insert into t1 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
1072+
set @tmp=@@big_tables;
1073+
set big_tables=1;
1074+
select rank() over (order by a) from t1;
1075+
set big_tables=@tmp;
1076+
drop table t1;
1077+

sql/filesort.cc

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -124,25 +124,19 @@ void Sort_param::init_for_filesort(uint sortlen, TABLE *table,
124124
@param thd Current thread
125125
@param table Table to sort
126126
@param filesort How to sort the table
127-
@param sort_positions Set to TRUE if we want to force sorting by
128-
position
129-
(Needed by UPDATE/INSERT or ALTER TABLE or
130-
when rowids are required by executor)
131-
applying WHERE condition.
132127
@param[out] found_rows Store the number of found rows here.
133128
This is the number of found rows after
134129
applying WHERE condition.
135130
@note
136-
If we sort by position (like if sort_positions is 1) filesort() will
137-
call table->prepare_for_position().
131+
If we sort by position (like if filesort->sort_positions==true)
132+
filesort() will call table->prepare_for_position().
138133
139134
@retval
140135
0 Error
141136
# SORT_INFO
142137
*/
143138

144139
SORT_INFO *filesort(THD *thd, TABLE *table, Filesort *filesort,
145-
bool sort_positions,
146140
Filesort_tracker* tracker)
147141
{
148142
int error;
@@ -203,7 +197,7 @@ SORT_INFO *filesort(THD *thd, TABLE *table, Filesort *filesort,
203197
&multi_byte_charset),
204198
table,
205199
thd->variables.max_length_for_sort_data,
206-
max_rows, sort_positions);
200+
max_rows, filesort->sort_positions);
207201

208202
sort->addon_buf= param.addon_buf;
209203
sort->addon_field= param.addon_field;

sql/filesort.h

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,25 @@ class Filesort: public Sql_alloc
4747
bool own_select;
4848
/** true means we are using Priority Queue for order by with limit. */
4949
bool using_pq;
50+
51+
/*
52+
TRUE means sort operation must produce table rowids.
53+
FALSE means that it halso has an option of producing {sort_key,
54+
addon_fields} pairs.
55+
*/
56+
bool sort_positions;
5057

5158
Filesort_tracker *tracker;
5259

53-
Filesort(ORDER *order_arg, ha_rows limit_arg, SQL_SELECT *select_arg):
60+
Filesort(ORDER *order_arg, ha_rows limit_arg, bool sort_positions_arg,
61+
SQL_SELECT *select_arg):
5462
order(order_arg),
5563
limit(limit_arg),
5664
sortorder(NULL),
5765
select(select_arg),
5866
own_select(false),
59-
using_pq(false)
67+
using_pq(false),
68+
sort_positions(sort_positions_arg)
6069
{
6170
DBUG_ASSERT(order);
6271
};
@@ -143,12 +152,10 @@ class SORT_INFO
143152
{ return filesort_buffer.sort_buffer_size(); }
144153

145154
friend SORT_INFO *filesort(THD *thd, TABLE *table, Filesort *filesort,
146-
bool sort_positions,
147155
Filesort_tracker* tracker);
148156
};
149157

150158
SORT_INFO *filesort(THD *thd, TABLE *table, Filesort *filesort,
151-
bool sort_positions,
152159
Filesort_tracker* tracker);
153160

154161
void change_double_for_sort(double nr,uchar *to);

sql/sql_delete.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -492,12 +492,13 @@ bool mysql_delete(THD *thd, TABLE_LIST *table_list, COND *conds,
492492
{
493493

494494
{
495-
Filesort fsort(order, HA_POS_ERROR, select);
495+
Filesort fsort(order, HA_POS_ERROR, true, select);
496496
DBUG_ASSERT(query_plan.index == MAX_KEY);
497+
497498
Filesort_tracker *fs_tracker=
498499
thd->lex->explain->get_upd_del_plan()->filesort_tracker;
499500

500-
if (!(file_sort= filesort(thd, table, &fsort, true, fs_tracker)))
501+
if (!(file_sort= filesort(thd, table, &fsort, fs_tracker)))
501502
goto got_error;
502503

503504
thd->inc_examined_row_count(file_sort->examined_rows);

sql/sql_select.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2814,7 +2814,9 @@ JOIN::optimize_distinct()
28142814
bool
28152815
JOIN::add_sorting_to_table(JOIN_TAB *tab, ORDER *order)
28162816
{
2817-
tab->filesort= new (thd->mem_root) Filesort(order, HA_POS_ERROR, tab->select);
2817+
tab->filesort=
2818+
new (thd->mem_root) Filesort(order, HA_POS_ERROR, tab->keep_current_rowid,
2819+
tab->select);
28182820
if (!tab->filesort)
28192821
return true;
28202822
/*
@@ -21279,7 +21281,7 @@ create_sort_index(THD *thd, JOIN *join, JOIN_TAB *tab, Filesort *fsort)
2127921281

2128021282
if (table->s->tmp_table)
2128121283
table->file->info(HA_STATUS_VARIABLE); // Get record count
21282-
file_sort= filesort(thd, table, fsort, tab->keep_current_rowid, fsort->tracker);
21284+
file_sort= filesort(thd, table, fsort, fsort->tracker);
2128321285
DBUG_ASSERT(tab->filesort_result == 0);
2128421286
tab->filesort_result= file_sort;
2128521287
tab->records= 0;

sql/sql_table.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9447,13 +9447,14 @@ copy_data_between_tables(THD *thd, TABLE *from, TABLE *to,
94479447

94489448
THD_STAGE_INFO(thd, stage_sorting);
94499449
Filesort_tracker dummy_tracker(false);
9450-
Filesort fsort(order, HA_POS_ERROR, NULL);
9450+
Filesort fsort(order, HA_POS_ERROR, true, NULL);
9451+
94519452
if (thd->lex->select_lex.setup_ref_array(thd, order_num) ||
94529453
setup_order(thd, thd->lex->select_lex.ref_pointer_array,
94539454
&tables, fields, all_fields, order))
94549455
goto err;
94559456

9456-
if (!(file_sort= filesort(thd, from, &fsort, true, &dummy_tracker)))
9457+
if (!(file_sort= filesort(thd, from, &fsort, &dummy_tracker)))
94579458
goto err;
94589459
}
94599460
thd_progress_next_stage(thd);

sql/sql_update.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -558,12 +558,12 @@ int mysql_update(THD *thd,
558558
to update
559559
NOTE: filesort will call table->prepare_for_position()
560560
*/
561-
Filesort fsort(order, limit, select);
561+
Filesort fsort(order, limit, true, select);
562562

563563
Filesort_tracker *fs_tracker=
564564
thd->lex->explain->get_upd_del_plan()->filesort_tracker;
565565

566-
if (!(file_sort= filesort(thd, table, &fsort, true, fs_tracker)))
566+
if (!(file_sort= filesort(thd, table, &fsort, fs_tracker)))
567567
goto err;
568568
thd->inc_examined_row_count(file_sort->examined_rows);
569569

sql/sql_window.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1834,7 +1834,7 @@ bool Window_funcs_sort::setup(THD *thd, SQL_SELECT *sel,
18341834
ORDER* sort_order= concat_order_lists(thd->mem_root,
18351835
spec->partition_list->first,
18361836
spec->order_list->first);
1837-
filesort= new (thd->mem_root) Filesort(sort_order, HA_POS_ERROR, NULL);
1837+
filesort= new (thd->mem_root) Filesort(sort_order, HA_POS_ERROR, true, NULL);
18381838
/* Apply the same condition that the subsequent sort has. */
18391839
filesort->select= sel;
18401840

0 commit comments

Comments
 (0)