Skip to content

Commit

Permalink
MDEV-12136 SELECT COUNT(DISTINCT) returns the wrong value when tmp_ta…
Browse files Browse the repository at this point in the history
…ble_size is limited

Same MDEV, second bug.
Merge buffer must fit at least MERGEBUFF2 (that is, 15) key values.
Because merge_index() can merge that many buffers, and
merge_many_buff() leaves that many buffers unmerged.
  • Loading branch information
vuvova committed Jul 13, 2017
1 parent e7f51e5 commit 27bc13b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
4 changes: 4 additions & 0 deletions mysql-test/r/count_distinct.result
Original file line number Diff line number Diff line change
Expand Up @@ -100,5 +100,9 @@ set @@tmp_table_size = 1024;
select count(distinct user_id) from t1;
count(distinct user_id)
17
alter table t1 modify user_id char(128) character set utf8;
select count(distinct user_id) from t1;
count(distinct user_id)
17
drop table t1;
set @@tmp_table_size = default;
2 changes: 2 additions & 0 deletions mysql-test/t/count_distinct.test
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ create table t1 (user_id char(64) character set utf8);
insert t1 values(1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12),(13),(14),(15),(16),(17);
set @@tmp_table_size = 1024;
select count(distinct user_id) from t1;
alter table t1 modify user_id char(128) character set utf8;
select count(distinct user_id) from t1;
drop table t1;
set @@tmp_table_size = default;

Expand Down
8 changes: 6 additions & 2 deletions sql/uniques.cc
Original file line number Diff line number Diff line change
Expand Up @@ -607,11 +607,15 @@ bool Unique::walk(TABLE *table, tree_walk_action action, void *walk_action_arg)
return 1;
if (flush_io_cache(&file) || reinit_io_cache(&file, READ_CACHE, 0L, 0, 0))
return 1;
size_t buff_sz= (max_in_memory_size / full_size + 1) * full_size;
/*
merge_buffer must fit at least MERGEBUFF2 keys, because
merge_index() can merge that many BUFFPEKs at once.
*/
size_t buff_sz= max(MERGEBUFF2, max_in_memory_size/full_size+1) * full_size;
if (!(merge_buffer = (uchar *)my_malloc(buff_sz, MYF(MY_WME))))
return 1;
if (buff_sz < (ulong) (full_size * (file_ptrs.elements + 1)))
res= merge(table, merge_buffer, buff_sz >= full_size * MERGEBUFF2) ;
res= merge(table, merge_buffer, true) ;

if (!res)
{
Expand Down

0 comments on commit 27bc13b

Please sign in to comment.