From 0739179857699d68758ff2e56e9414735546ded6 Mon Sep 17 00:00:00 2001 From: Varun Gupta Date: Tue, 8 Aug 2017 21:13:45 +0530 Subject: MDEV-13458: Wrong result for aggregate function with distinct clause when the value for tmp_table_size is small Fixed by making sure that the sort buffer would have atleast MERGEBUFF2 keys. Also fixed MDEV-13457 by making sure that an empty tree is never dumped to the disk --- sql/uniques.cc | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'sql/uniques.cc') diff --git a/sql/uniques.cc b/sql/uniques.cc index 8b7da7e6e52..89ab4682829 100644 --- a/sql/uniques.cc +++ b/sql/uniques.cc @@ -96,6 +96,9 @@ Unique::Unique(qsort_cmp2 comp_func, void * comp_func_fixed_arg, */ max_elements= (ulong) (max_in_memory_size / ALIGN_SIZE(sizeof(TREE_ELEMENT)+size)); + if (!max_elements) + max_elements= 1; + (void) open_cached_file(&file, mysql_tmpdir,TEMP_PREFIX, DISK_BUFFER_SIZE, MYF(MY_WME)); } @@ -608,10 +611,11 @@ bool Unique::walk(TABLE *table, tree_walk_action action, void *walk_action_arg) if (flush_io_cache(&file) || reinit_io_cache(&file, READ_CACHE, 0L, 0, 0)) return 1; /* - merge_buffer must fit at least MERGEBUFF2 keys, because - merge_index() can merge that many BUFFPEKs at once. + merge_buffer must fit at least MERGEBUFF2 + 1 keys, because + merge_index() can merge that many BUFFPEKs at once. The extra space for one key + is needed when a piece of merge buffer is re-read, see merge_walk() */ - size_t buff_sz= max(MERGEBUFF2, max_in_memory_size/full_size+1) * full_size; + size_t buff_sz= max(MERGEBUFF2+1, 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))) @@ -673,7 +677,7 @@ bool Unique::merge(TABLE *table, uchar *buff, bool without_last_merge) full_size; sort_param.min_dupl_count= min_dupl_count; sort_param.res_length= 0; - sort_param.keys= (uint) (max_in_memory_size / sort_param.sort_length); + sort_param.keys= (uint) max((max_in_memory_size / sort_param.sort_length), MERGEBUFF2); sort_param.not_killable= 1; sort_param.unique_buff= buff + (sort_param.keys * sort_param.sort_length); -- cgit v1.2.1 From 0f44c8ab28adf0533595a417453765197deb4bfa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vicen=C8=9Biu=20Ciorbaru?= Date: Tue, 19 Sep 2017 02:04:45 +0300 Subject: Fix merge error --- sql/uniques.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'sql/uniques.cc') diff --git a/sql/uniques.cc b/sql/uniques.cc index 8dd028c93f5..b562bb0f660 100644 --- a/sql/uniques.cc +++ b/sql/uniques.cc @@ -712,8 +712,8 @@ bool Unique::merge(TABLE *table, uchar *buff, bool without_last_merge) full_size; sort_param.min_dupl_count= min_dupl_count; sort_param.res_length= 0; - sort_param.keys= (uint) MY_MAX((max_in_memory_size / sort_param.sort_length), - MERGEBUFF2); + sort_param.max_keys_per_buffer= + (uint) MY_MAX((max_in_memory_size / sort_param.sort_length), MERGEBUFF2); sort_param.not_killable= 1; sort_param.unique_buff= buff +(sort_param.max_keys_per_buffer * -- cgit v1.2.1