MDEV-21044: Wrong result when using a smaller size for sort buffer

Make sure that the sort buffers can store atleast one sort key.
This is needed to make sure that all merge buffers are read else
with no sort keys some merge buffers are skipped because the code
makes a conclusion there is no data to be read.
This commit is contained in:
Varun Gupta 2019-11-18 11:50:58 +05:30
parent 214023aa0e
commit 2909725636
3 changed files with 47 additions and 0 deletions

View file

@ -3207,3 +3207,33 @@ pk
2
3
DROP TABLE t1;
#
# MDEV-21044: Wrong result when using a smaller size for sort buffer
#
create table t1(a varchar(765),b int);
insert into t1 values ("a",1),("b",2),("c",3),("e",4);
insert into t1 values ("d",5),("f",6),("g",7),("h",8);
insert into t1 values ("k",11),("l",12),("i",9),("j",10);
insert into t1 values ("m",13),("n",14),("o",15),("p",16);
set @save_sort_buffer_size= @@sort_buffer_size;
set sort_buffer_size=1024;
select * from t1 order by b;
a b
a 1
b 2
c 3
e 4
d 5
f 6
g 7
h 8
i 9
j 10
k 11
l 12
m 13
n 14
o 15
p 16
set @@sort_buffer_size= @save_sort_buffer_size;
drop table t1;

View file

@ -2141,3 +2141,19 @@ INSERT INTO t1 VALUES (1),(2),(3);
SELECT DISTINCT pk FROM t1 GROUP BY 'foo';
SELECT DISTINCT pk FROM t1;
DROP TABLE t1;
--echo #
--echo # MDEV-21044: Wrong result when using a smaller size for sort buffer
--echo #
create table t1(a varchar(765),b int);
insert into t1 values ("a",1),("b",2),("c",3),("e",4);
insert into t1 values ("d",5),("f",6),("g",7),("h",8);
insert into t1 values ("k",11),("l",12),("i",9),("j",10);
insert into t1 values ("m",13),("n",14),("o",15),("p",16);
set @save_sort_buffer_size= @@sort_buffer_size;
set sort_buffer_size=1024;
select * from t1 order by b;
set @@sort_buffer_size= @save_sort_buffer_size;
drop table t1;

View file

@ -343,6 +343,7 @@ ha_rows filesort(THD *thd, TABLE *table, SORT_FIELD *sortorder, uint s_length,
param.max_keys_per_buffer=((param.max_keys_per_buffer *
(param.rec_length + sizeof(char*))) /
param.rec_length - 1);
set_if_bigger(param.max_keys_per_buffer, 1);
maxbuffer--; // Offset from 0
if (merge_many_buff(&param,
(uchar*) table_sort.get_sort_keys(),