@@ -898,7 +898,7 @@ JOIN::optimize()
898
898
thd->restore_active_arena(arena, &backup);
899
899
}
900
900
901
- conds= optimize_cond(this, conds, join_list, &cond_value);
901
+ conds= optimize_cond(this, conds, join_list, &select_lex-> cond_value);
902
902
if (thd->is_error())
903
903
{
904
904
error= 1;
@@ -907,24 +907,20 @@ JOIN::optimize()
907
907
}
908
908
909
909
{
910
- having= optimize_cond(this, having, join_list, &having_value);
910
+ having= optimize_cond(this, having, join_list, &select_lex-> having_value);
911
911
if (thd->is_error())
912
912
{
913
913
error= 1;
914
914
DBUG_PRINT("error",("Error from optimize_cond"));
915
915
DBUG_RETURN(1);
916
916
}
917
- if (select_lex->where)
918
- select_lex->cond_value= cond_value;
919
- if (select_lex->having)
920
- select_lex->having_value= having_value;
921
-
922
- if (cond_value == Item::COND_FALSE || having_value == Item::COND_FALSE ||
917
+ if (select_lex->cond_value == Item::COND_FALSE ||
918
+ select_lex->having_value == Item::COND_FALSE ||
923
919
(!unit->select_limit_cnt && !(select_options & OPTION_FOUND_ROWS)))
924
920
{ /* Impossible cond */
925
- DBUG_PRINT("info", (having_value == Item::COND_FALSE ?
921
+ DBUG_PRINT("info", (select_lex-> having_value == Item::COND_FALSE ?
926
922
"Impossible HAVING" : "Impossible WHERE"));
927
- zero_result_cause= having_value == Item::COND_FALSE ?
923
+ zero_result_cause= select_lex-> having_value == Item::COND_FALSE ?
928
924
"Impossible HAVING" : "Impossible WHERE";
929
925
tables= 0;
930
926
error= 0;
@@ -1149,10 +1145,9 @@ JOIN::optimize()
1149
1145
if (having && const_table_map)
1150
1146
{
1151
1147
having->update_used_tables();
1152
- having= remove_eq_conds(thd, having, &having_value);
1153
- if (having_value == Item::COND_FALSE)
1148
+ having= remove_eq_conds(thd, having, &select_lex-> having_value);
1149
+ if (select_lex-> having_value == Item::COND_FALSE)
1154
1150
{
1155
- having= new Item_int((longlong) 0,1);
1156
1151
zero_result_cause= "Impossible HAVING noticed after reading const tables";
1157
1152
error= 0;
1158
1153
DBUG_RETURN(0);
@@ -1823,8 +1818,8 @@ JOIN::exec()
1823
1818
In this case JOIN::exec must check for JOIN::having_value, in the
1824
1819
same way it checks for JOIN::cond_value.
1825
1820
*/
1826
- if (cond_value != Item::COND_FALSE &&
1827
- having_value != Item::COND_FALSE &&
1821
+ if (select_lex-> cond_value != Item::COND_FALSE &&
1822
+ select_lex-> having_value != Item::COND_FALSE &&
1828
1823
(!conds || conds->val_int()) &&
1829
1824
(!having || having->val_int()))
1830
1825
{
@@ -9278,16 +9273,36 @@ static void restore_prev_nj_state(JOIN_TAB *last)
9278
9273
}
9279
9274
9280
9275
9276
+ /**
9277
+ Optimize conditions by
9278
+
9279
+ a) applying transitivity to build multiple equality predicates
9280
+ (MEP): if x=y and y=z the MEP x=y=z is built.
9281
+ b) apply constants where possible. If the value of x is known to be
9282
+ 42, x is replaced with a constant of value 42. By transitivity, this
9283
+ also applies to MEPs, so the MEP in a) will become 42=x=y=z.
9284
+ c) remove conditions that are impossible or always true
9285
+
9286
+ @param join pointer to the structure providing all context info
9287
+ for the query
9288
+ @param conds conditions to optimize
9289
+ @param join_list list of join tables to which the condition
9290
+ refers to
9291
+ @param[out] cond_value Not changed if conds was empty
9292
+ COND_TRUE if conds is always true
9293
+ COND_FALSE if conds is impossible
9294
+ COND_OK otherwise
9295
+
9296
+ @return optimized conditions
9297
+ */
9281
9298
static COND *
9282
9299
optimize_cond(JOIN *join, COND *conds, List<TABLE_LIST> *join_list,
9283
9300
Item::cond_result *cond_value)
9284
9301
{
9285
9302
THD *thd= join->thd;
9286
9303
DBUG_ENTER("optimize_cond");
9287
9304
9288
- if (!conds)
9289
- *cond_value= Item::COND_TRUE;
9290
- else
9305
+ if (conds)
9291
9306
{
9292
9307
/*
9293
9308
Build all multiple equality predicates and eliminate equality
0 commit comments