1
0
mirror of https://github.com/MariaDB/server.git synced 2025-07-30 16:24:05 +03:00

MDEV-26585 Wrong query results when using index for group-by

The problem was that "group_min_max optimization" does not work if
some aggregate functions, like COUNT(*), is used.
The function get_best_group_min_max() is using the join->sum_funcs
array to check which aggregate functions are used.
The bug was that aggregates in HAVING where not yet added to
join->sum_funcs at the time get_best_group_min_max() was called.

Fixed by populate join->sum_funcs already in prepare, which means that
all sum functions will be in join->sum_funcs in get_best_group_min_max().
A benefit of this approach is that we can remove several calls to
make_sum_func_list() from the code and simplify the function.

I removed some wrong setting of 'sort_and_group'.
This variable is set when alloc_group_fields() is called, as part
of allocating the cache needed by end_send_group() and does not need
to be set by other functions.

One problematic thing was that Spider is using *join->sum_funcs to detect
at which stage the optimizer is and do internal calculations of aggregate
functions. Updating join->sum_funcs early caused Spider to fail when trying
to find min/max values in opt_sum_query().
Fixed by temporarily resetting sum_funcs during opt_sum_query().

Reviewer: Sergei Petrunia
This commit is contained in:
Monty
2022-02-02 14:25:25 +02:00
parent d314bd2664
commit 38058c04a4
4 changed files with 97 additions and 32 deletions

View File

@ -1196,7 +1196,17 @@ public:
Indicates that grouping will be performed on the result set during
query execution. This field belongs to query execution.
@see make_group_fields, alloc_group_fields, JOIN::exec
If 'sort_and_group' is set, then the optimizer is going to use on of
the following algorithms to resolve GROUP BY.
- If one table, sort the table and then calculate groups on the fly.
- If more than one table, create a temporary table to hold the join,
sort it and then resolve group by on the fly.
The 'on the fly' calculation is done in end_send_group()
@see make_group_fields, alloc_group_fields, JOIN::exec,
setup_end_select_func
*/
bool sort_and_group;
bool first_record,full_join, no_field_update;
@ -1654,7 +1664,7 @@ public:
bool make_range_rowid_filters();
bool init_range_rowid_filters();
bool make_sum_func_list(List<Item> &all_fields, List<Item> &send_fields,
bool before_group_by, bool recompute= FALSE);
bool before_group_by);
/// Initialzes a slice, see comments for ref_ptrs above.
Ref_ptr_array ref_ptr_array_slice(size_t slice_num)