@@ -7754,13 +7754,13 @@ void binlog_unsafe_map_init()
7754
7754
7755
7755
/* *
7756
7756
@brief
7757
- Finding fiels that are used in the GROUP BY of this st_select_lex
7757
+ Collect fiels that are used in the GROUP BY of this st_select_lex
7758
7758
7759
7759
@param thd The thread handle
7760
7760
7761
7761
@details
7762
- This method looks through the fields which are used in the GROUP BY of this
7763
- st_select_lex and saves this fields.
7762
+ This method looks through the fields that are used in the GROUP BY of this
7763
+ st_select_lex and saves onfo on these fields.
7764
7764
*/
7765
7765
7766
7766
void st_select_lex::collect_grouping_fields_for_derived (THD *thd,
@@ -7826,7 +7826,7 @@ bool st_select_lex::collect_grouping_fields(THD *thd)
7826
7826
This method traverses the AND-OR condition cond and for each subformula of
7827
7827
the condition it checks whether it can be usable for the extraction of a
7828
7828
condition over the grouping fields of this select. The method uses
7829
- the call-back parameter checker to ckeck whether a primary formula
7829
+ the call-back parameter checker to check whether a primary formula
7830
7830
depends only on grouping fields.
7831
7831
The subformulas that are not usable are marked with the flag NO_EXTRACTION_FL.
7832
7832
The subformulas that can be entierly extracted are marked with the flag
@@ -7870,7 +7870,7 @@ st_select_lex::check_cond_extraction_for_grouping_fields(THD *thd, Item *cond,
7870
7870
else if (!and_cond)
7871
7871
break ;
7872
7872
}
7873
- if (item)
7873
+ if ((and_cond && count == 0 ) || item)
7874
7874
cond->set_extraction_flag (NO_EXTRACTION_FL);
7875
7875
if (count_full == arg_list->elements )
7876
7876
{
@@ -9723,7 +9723,7 @@ void st_select_lex::mark_or_conds_to_avoid_pushdown(Item *cond)
9723
9723
@details
9724
9724
The method finds out what conditions can be extracted from cond depended
9725
9725
only on the grouping fields of this SELECT or fields equal to them.
9726
- If the condition that can be pushed is AND-condition it is splitted out
9726
+ If the condition that can be pushed is AND-condition it is splitted up
9727
9727
and for each its element it is checked if it can be pushed.
9728
9728
Pushable elements are attached to the attach_to_conds list.
9729
9729
If the condition isn't AND-condition it is entirely pushed into
@@ -9745,9 +9745,11 @@ void st_select_lex::mark_or_conds_to_avoid_pushdown(Item *cond)
9745
9745
9746
9746
bool
9747
9747
st_select_lex::build_pushable_cond_for_having_pushdown (THD *thd,
9748
- Item *cond,
9749
- Pushdown_checker checker)
9748
+ Item *cond)
9750
9749
{
9750
+ Pushdown_checker checker=
9751
+ &Item::pushable_equality_checker_for_having_pushdown;
9752
+
9751
9753
bool is_multiple_equality= cond->type () == Item::FUNC_ITEM &&
9752
9754
((Item_func*) cond)->functype () == Item_func::MULT_EQUAL_FUNC;
9753
9755
@@ -9862,33 +9864,15 @@ Field_pair *get_corresponding_field_pair(Item *item,
9862
9864
9863
9865
/* *
9864
9866
@brief
9865
- Find fields in WHERE clause multiple equalities that can be used in pushdown
9867
+ Collect fields in multiple equalities usable for pushdown from having
9866
9868
9867
9869
@param thd The thread handle
9868
9870
9869
9871
@details
9870
9872
This method looks through the multiple equalities of the WHERE clause
9871
- trying to find any of them which fields are used in the GROUP BY of the
9872
- SELECT. If such multiple equality exists conditions in the HAVING
9873
- clause that use fields of this multiple equality can be pushed down
9874
- into the WHERE clause as well as the conditions depended on the fields
9875
- from the GROUP BY or fields equal to them that are taken from the HAVING
9876
- clause multiple equalities.
9877
-
9878
- Example:
9879
-
9880
- SELECT a,MAX(b),c
9881
- FROM t1
9882
- WHERE (t1.a=t1.c)
9883
- GROUP BY t1.a
9884
- HAVING (t1.c>1)
9885
-
9886
- =>
9887
-
9888
- SELECT a,MAX(b),c
9889
- FROM t1
9890
- WHERE (t1.a=t1.c) AND (t1.c>1)
9891
- GROUP BY t1.a
9873
+ trying to find any of them whose fields are used in the GROUP BY of the
9874
+ SELECT. Any field from these multiple equality is included into the
9875
+ the list of fields against which any candidate for pushing is checked.
9892
9876
9893
9877
@retval
9894
9878
true - if an error occurs
@@ -9909,22 +9893,16 @@ bool st_select_lex::collect_fields_equal_to_grouping(THD *thd)
9909
9893
Item *item;
9910
9894
while ((item= it++))
9911
9895
{
9912
- if (item->type () != Item::FIELD_ITEM &&
9913
- item->type () != Item::REF_ITEM)
9914
- continue ;
9915
-
9916
9896
if (get_corresponding_field_pair (item, grouping_tmp_fields))
9917
9897
break ;
9918
9898
}
9919
9899
if (!item)
9920
9900
break ;
9921
- it.rewind ();
9922
9901
9902
+ it.rewind ();
9923
9903
while ((item= it++))
9924
9904
{
9925
- if ((item->type () != Item::FIELD_ITEM &&
9926
- item->type () != Item::REF_ITEM) ||
9927
- get_corresponding_field_pair (item, grouping_tmp_fields))
9905
+ if (get_corresponding_field_pair (item, grouping_tmp_fields))
9928
9906
continue ;
9929
9907
Field_pair *grouping_tmp_field=
9930
9908
new Field_pair (((Item_field *)item->real_item ())->field , item);
@@ -9937,7 +9915,7 @@ bool st_select_lex::collect_fields_equal_to_grouping(THD *thd)
9937
9915
9938
9916
/* *
9939
9917
@brief
9940
- Cleanup and fix for the condition that is ready to be pushed down
9918
+ Cleanup and fix of the condition that is ready to be pushed down
9941
9919
9942
9920
@param thd The thread handle
9943
9921
@param cond The condition to be processed
@@ -9954,7 +9932,7 @@ bool st_select_lex::collect_fields_equal_to_grouping(THD *thd)
9954
9932
*/
9955
9933
9956
9934
static
9957
- bool cleanup_inequalities_for_having_pushdown (THD *thd, Item *cond)
9935
+ bool cleanup_condition_pushed_from_having (THD *thd, Item *cond)
9958
9936
{
9959
9937
if (cond->type () == Item::FUNC_ITEM &&
9960
9938
((Item_func*) cond)->functype () == Item_func::MULT_EQUAL_FUNC)
@@ -9966,7 +9944,7 @@ bool cleanup_inequalities_for_having_pushdown(THD *thd, Item *cond)
9966
9944
Item *item;
9967
9945
9968
9946
while ((item=it++))
9969
- cleanup_inequalities_for_having_pushdown (thd, item);
9947
+ cleanup_condition_pushed_from_having (thd, item);
9970
9948
}
9971
9949
else
9972
9950
{
@@ -10075,7 +10053,7 @@ Item *remove_pushed_top_conjuncts_for_having(THD *thd, Item *cond)
10075
10053
This function builds the most restrictive condition depending only on
10076
10054
the fields used in the GROUP BY of this select (directly or indirectly
10077
10055
through equality) that can be extracted from the HAVING clause of this
10078
- select having and pushes it into the WHERE clause of this select.
10056
+ select and pushes it into the WHERE clause of this select.
10079
10057
10080
10058
Example of the transformation:
10081
10059
@@ -10095,11 +10073,11 @@ Item *remove_pushed_top_conjuncts_for_having(THD *thd, Item *cond)
10095
10073
In details:
10096
10074
1. Collect fields used in the GROUP BY grouping_fields of this SELECT
10097
10075
2. Collect fields equal to grouping_fields from the WHERE clause
10098
- of this SELECT and attach them to the grouping_fields list.
10099
- 3. Search for the conditions in the HAVING clause of this select
10100
- that depends only on grouping_fields. Store them in the
10101
- attach_to_conds list.
10102
- 4. Remove pushable conditions from the HAVING clause having .
10076
+ of this SELECT and add them to the grouping_fields list.
10077
+ 3. Extract the most restrictive condition from the HAVING clause of this
10078
+ select that depends only on the grouping fields (directly or indirectly
10079
+ through equality). Store it in the attach_to_conds list.
10080
+ 4. Remove pushable conditions from the HAVING clause if it's possible .
10103
10081
10104
10082
@note
10105
10083
This method is similar to st_select_lex::pushdown_cond_into_where_clause().
@@ -10119,52 +10097,40 @@ Item *st_select_lex::pushdown_from_having_into_where(THD *thd, Item *having)
10119
10097
thd->lex ->current_select = this ;
10120
10098
10121
10099
/*
10122
- 1. Collect fields used in the GROUP BY grouping_fields of this SELECT
10100
+ 1. Collect fields used in the GROUP BY grouping fields of this SELECT
10123
10101
2. Collect fields equal to grouping_fields from the WHERE clause
10124
- of this SELECT and attach them to the grouping_fields list.
10102
+ of this SELECT and add them to the grouping fields list.
10125
10103
*/
10126
- if (have_window_funcs ())
10127
- {
10128
- if (group_list.first || join->implicit_grouping )
10129
- return having;
10130
- ORDER *common_partition_fields=
10131
- find_common_window_func_partition_fields (thd);
10132
- if (!common_partition_fields ||
10133
- collect_grouping_fields (thd) ||
10134
- collect_fields_equal_to_grouping (thd))
10135
- return having;
10136
- }
10137
- else if (collect_grouping_fields (thd) ||
10138
- collect_fields_equal_to_grouping (thd))
10104
+ if (collect_grouping_fields (thd) ||
10105
+ collect_fields_equal_to_grouping (thd))
10139
10106
return having;
10140
10107
10141
10108
/*
10142
- 3. Search for the conditions in the HAVING clause of this select
10143
- that depends only on grouping_fields. Store them in the
10144
- attach_to_conds list.
10109
+ 3. Extract the most restrictive condition from the HAVING clause of this
10110
+ select that depends only on the grouping fields (directly or indirectly
10111
+ through equality). Store it in the attach_to_conds list.
10145
10112
*/
10146
10113
thd->having_pushdown = true ;
10147
10114
List_iterator_fast<Item> it (attach_to_conds);
10148
10115
Item *item;
10149
10116
check_cond_extraction_for_grouping_fields (thd, having,
10150
10117
&Item::dep_on_grouping_fields_checker_for_having_pushdown);
10151
- if (build_pushable_cond_for_having_pushdown (thd, having,
10152
- &Item::pushable_equality_checker_for_having_pushdown))
10118
+ if (build_pushable_cond_for_having_pushdown (thd, having))
10153
10119
{
10154
10120
attach_to_conds.empty ();
10155
10121
goto exit;
10156
10122
}
10157
10123
if (attach_to_conds.elements != 0 )
10158
10124
{
10159
10125
/*
10160
- 4. Remove pushable conditions from the HAVING clause having .
10126
+ 4. Remove pushable conditions from the HAVING clause if it's possible .
10161
10127
*/
10162
10128
having= remove_pushed_top_conjuncts_for_having (thd, having);
10163
10129
10164
10130
it.rewind ();
10165
10131
while ((item=it++))
10166
10132
{
10167
- if (cleanup_inequalities_for_having_pushdown (thd, item))
10133
+ if (cleanup_condition_pushed_from_having (thd, item))
10168
10134
{
10169
10135
attach_to_conds.empty ();
10170
10136
goto exit;
0 commit comments