diff options
author | Vicențiu Ciorbaru <vicentiu@mariadb.org> | 2021-01-19 10:12:37 +0200 |
---|---|---|
committer | Vicențiu Ciorbaru <vicentiu@mariadb.org> | 2021-03-07 12:08:25 +0200 |
commit | 341fa8b3421a6b59a74d2c6d03b25db9133c925d (patch) | |
tree | e229fad28ffda93b1018b8566e89ac49441062c4 | |
parent | 27d08bab7bf665be99c96757c15089dfa632f733 (diff) | |
download | mariadb-git-341fa8b3421a6b59a74d2c6d03b25db9133c925d.tar.gz |
cleanup: test_if_subpart documented to highlight side effect
Also document (through function parameters names) which ORDER must be passed
to the function: first GROUP BY, then ORDER BY.
-rw-r--r-- | sql/sql_select.cc | 21 |
1 files changed, 16 insertions, 5 deletions
diff --git a/sql/sql_select.cc b/sql/sql_select.cc index e99e9cb8356..c8a8df978a3 100644 --- a/sql/sql_select.cc +++ b/sql/sql_select.cc @@ -253,7 +253,7 @@ static ORDER *create_distinct_group(THD *thd, Ref_ptr_array ref_pointer_array, ORDER *order, List<Item> &fields, List<Item> &all_fields, bool *all_order_by_fields_used); -static bool test_if_subpart(ORDER *a,ORDER *b); +static bool test_if_subpart(ORDER *group_order, ORDER *final_order); static TABLE *get_sort_by_table(ORDER *a,ORDER *b,List<TABLE_LIST> &tables, table_map const_tables); static void calc_group_buffer(JOIN *join,ORDER *group); @@ -2731,6 +2731,13 @@ int JOIN::optimize_stage2() calc_group_buffer(this, group_list); } + /* + Remove ORDER BY if GROUP BY is more specific. Example + GROUP BY a, b ORDER BY a + + Alternatively remove ORDER BY if there are aggregate functions and no + GROUP BY, this always leads to one row result, no point in sorting. + */ if (test_if_subpart(group_list, order) || (!group_list && tmp_table_param.sum_func_count)) { @@ -24824,14 +24831,18 @@ count_field_types(SELECT_LEX *select_lex, TMP_TABLE_PARAM *param, /** Return 1 if second is a subpart of first argument. - If first parts has different direction, change it to second part - (group is sorted like order) + SIDE EFFECT: + For all the first items in the list that match, the *direction of the + group is set to the same as order*. The direction of the group doesn't + matter if the ORDER BY clause overrides it anyway. */ static bool -test_if_subpart(ORDER *a,ORDER *b) +test_if_subpart(ORDER *group_order, ORDER *final_order) { - for (; a && b; a=a->next,b=b->next) + ORDER *a= group_order; + ORDER *b= final_order; + for (;a && b; a=a->next,b=b->next) { if ((*a->item)->eq(*b->item,1)) a->direction=b->direction; |