28 #include "sql_resolver.h"
29 #include "sql_optimizer.h"
33 #include "opt_explain_format.h"
35 static void remove_redundant_subquery_clauses(st_select_lex *subq_select_lex,
36 int hidden_group_field_count,
37 int hidden_order_field_count,
41 setup_without_group(THD *thd,
Ref_ptr_array ref_pointer_array,
49 int *hidden_group_field_count,
50 int *hidden_order_field_count);
51 static bool resolve_subquery(THD *thd,
JOIN *join);
56 match_exprs_for_only_full_group_by(THD *thd,
List<Item> &all_fields,
57 int hidden_group_exprs_count,
58 int hidden_order_exprs_count,
59 int select_exprs_count,
77 uint wild_num,
Item *conds_init, uint og_num,
80 SELECT_LEX *select_lex_arg,
81 SELECT_LEX_UNIT *unit_arg)
83 DBUG_ENTER(
"JOIN::prepare");
90 Prepare_error_tracker tracker(thd);
96 if (select_options & SELECT_DISTINCT)
109 thd->lex->current_select->is_item_list_lookup= 1;
114 if (thd->derived_tables_processing)
115 select_lex->exclude_from_table_unique_test= TRUE;
120 trace_prepare.add_select_number(
select_lex->select_number);
128 FALSE, SELECT_ACL, SELECT_ACL))
138 table_ptr= table_ptr->next_leaf)
145 if (table_ptr->table->part_info)
156 DBUG_ASSERT(
select_lex->parsing_place == NO_MATTER);
166 if (setup_fields(thd, ref_ptrs,
fields_list, MARK_COLUMNS_READ,
170 int hidden_order_field_count;
171 if (setup_without_group(thd, ref_ptrs,
tables_list,
174 &hidden_group_field_count,
175 &hidden_order_field_count))
189 !(thd->lex->context_analysis_only & CONTEXT_ANALYSIS_ONLY_VIEW))
191 remove_redundant_subquery_clauses(
select_lex, hidden_group_field_count,
192 hidden_order_field_count,
198 nesting_map save_allow_sum_func= thd->lex->allow_sum_func;
199 thd->where=
"having clause";
200 thd->lex->allow_sum_func|= (nesting_map)1 << select_lex_arg->nest_level;
202 select_lex->resolve_place= st_select_lex::RESOLVE_HAVING;
203 bool having_fix_rc= (!
having->fixed &&
209 select_lex->resolve_place= st_select_lex::RESOLVE_NONE;
210 if (having_fix_rc || thd->is_error())
212 thd->lex->allow_sum_func= save_allow_sum_func;
227 opt_trace_print_expanded_query(thd,
select_lex, &trace_wrapper);
238 !(thd->lex->context_analysis_only & CONTEXT_ANALYSIS_ONLY_VIEW) &&
239 !(select_options & SELECT_DESCRIBE))
242 if (resolve_subquery(thd,
this))
250 bool real_order= FALSE;
252 for (ord=
order; ord; ord= ord->next)
254 Item *item= *ord->item;
263 ((item->type() != Item::FIELD_ITEM ||
265 ((
Item_field *) item)->field->sort_length()) &&
267 (item->type() != Item::FUNC_ITEM ||
269 item->result_type() != STRING_RESULT ||
273 if (item->with_sum_func && item->type() != Item::SUM_FUNC_ITEM)
274 item->split_sum_func(thd, ref_ptrs,
all_fields);
289 item_sum= item_sum->next;
292 }
while (item_sum != end);
308 for (
ORDER *ord= group_list; ord; ord= ord->next)
310 if ((*ord->item)->type() == Item::FIELD_ITEM &&
311 (*ord->item)->field_type() == MYSQL_TYPE_BIT)
317 ord->item= &ref_ptrs[el];
330 if (thd->variables.sql_mode & MODE_ONLY_FULL_GROUP_BY && !group_list &&
334 my_message(ER_MIX_OF_GROUP_FUNC_AND_FIELDS,
335 ER(ER_MIX_OF_GROUP_FUNC_AND_FIELDS), MYF(0));
341 for (
ORDER *group_tmp= group_list ; group_tmp ; group_tmp= group_tmp->next)
346 if (result && result->prepare(
fields_list, unit_arg))
351 this->
group= group_list != 0;
354 if (tmp_table_param.sum_func_count && !group_list)
356 implicit_grouping= TRUE;
361 #ifdef RESTRICTED_GROUP
362 if (implicit_grouping)
364 my_message(ER_WRONG_SUM_SELECT,ER(ER_WRONG_SUM_SELECT),MYF(0));
373 #ifdef WITH_PARTITION_STORAGE_ENGINE
382 if (prune_partitions(thd, tbl->table,
383 tbl->join_cond() ? tbl->join_cond() :
conds))
406 SELECT_LEX *select_lex,
407 const SELECT_LEX *outer)
409 bool has_nullables=
false;
410 const uint elements= predicate->unit->first_select()->item_list.elements;
411 DBUG_ENTER(
"subquery_allows_materialization");
412 DBUG_ASSERT(elements >= 1);
413 DBUG_ASSERT(predicate->left_expr->cols() == elements);
416 select_lex->select_number,
417 "IN (SELECT)",
"materialization");
419 const char *cause= NULL;
420 if (predicate->substype() != Item_subselect::IN_SUBS)
423 cause=
"not an IN predicate";
425 else if (select_lex->is_part_of_union())
430 else if (!select_lex->master_unit()->first_select()->leaf_tables)
433 cause=
"no inner tables";
435 else if (!outer->join)
441 cause=
"parent query has no JOIN";
443 else if (!outer->leaf_tables)
446 cause=
"no tables in outer query";
472 DBUG_ASSERT(predicate->left_expr->fixed);
475 for (uint
i= 0;
i < elements;
i++)
477 Item *
const inner= it++;
478 Item *
const outer= predicate->left_expr->element_index(
i);
481 cause=
"type mismatch";
489 has_nullables|= outer->maybe_null | inner->maybe_null;
494 trace_mat.add(
"has_nullable_expressions", has_nullables);
506 const bool is_top_level= predicate->is_top_level_item();
507 trace_mat.add(
"treat_UNKNOWN_as_FALSE", is_top_level);
509 if (!is_top_level && has_nullables && (elements > 1))
510 cause=
"cannot_handle_partial_matches";
513 trace_mat.add(
"possible",
true);
518 DBUG_ASSERT(cause != NULL);
519 trace_mat.add(
"possible",
false).add_alnum(
"cause", cause);
545 static bool resolve_subquery(THD *thd,
JOIN *join)
547 DBUG_ENTER(
"resolve_subquery");
549 bool chose_semijoin=
false;
550 SELECT_LEX *
const select_lex= join->
select_lex;
551 SELECT_LEX *
const outer= select_lex->outer_select();
562 DBUG_ASSERT(subq_predicate);
571 (subq_predicate->substype() == Item_subselect::IN_SUBS) ?
572 static_cast<Item_in_subselect *>(subq_predicate) : NULL;
576 DBUG_ASSERT(select_lex == thd->lex->current_select);
577 thd->lex->current_select= outer;
578 char const *save_where= thd->where;
579 thd->where=
"IN/ALL/ANY subquery";
581 bool result= !in_predicate->left_expr->fixed &&
582 in_predicate->left_expr->fix_fields(thd,
583 &in_predicate->left_expr);
584 thd->lex->current_select= select_lex;
585 thd->where= save_where;
597 if (select_lex->item_list.elements != in_predicate->left_expr->cols())
599 my_error(ER_OPERAND_COLUMNS, MYF(0), in_predicate->left_expr->cols());
605 DBUG_PRINT(
"info", (
"Checking if subq can be converted to semi-join"));
622 if (thd->optimizer_switch_flag(OPTIMIZER_SWITCH_SEMIJOIN) &&
624 !select_lex->is_part_of_union() &&
625 !select_lex->group_list.elements &&
626 !join->
having && !select_lex->with_sum_func &&
627 (outer->resolve_place == st_select_lex::RESOLVE_CONDITION ||
628 outer->resolve_place == st_select_lex::RESOLVE_JOIN_NEST) &&
630 select_lex->master_unit()->first_select()->leaf_tables &&
631 in_predicate->exec_method ==
633 outer->leaf_tables &&
634 !((join->select_options | outer->join->select_options)
635 & SELECT_STRAIGHT_JOIN))
637 DBUG_PRINT(
"info", (
"Subquery is semi-join conversion candidate"));
643 outer->join->sj_subselects.push_back(in_predicate);
644 chose_semijoin=
true;
651 select_lex->select_number,
"IN (SELECT)",
"semijoin");
652 oto1.add(
"chosen", chose_semijoin);
655 if (!chose_semijoin &&
656 subq_predicate->select_transformer(join) == Item_subselect::RES_ERROR)
719 while ((ref= ref_it++))
721 bool direct_ref=
false;
722 Item *item= ref->outer_ref;
723 Item **item_ref= ref->ref;
731 if (!ref_pointer_array.is_null() && !ref->found_in_select_list)
733 int el= all_fields.elements;
734 ref_pointer_array[el]= item;
736 all_fields.push_front(item);
741 item_ref= &ref_pointer_array[el];
744 if (ref->in_sum_func)
747 if (ref->in_sum_func->nest_level > select->nest_level)
751 for (sum_func= ref->in_sum_func; sum_func &&
752 sum_func->aggr_level >= select->nest_level;
753 sum_func= sum_func->in_sum_func)
755 if (sum_func->aggr_level == select->nest_level)
771 if ((*
group->item)->walk(&Item::find_item_processor, TRUE,
779 new_ref= direct_ref ?
781 ref->field_name, ref->alias_name_used) :
782 new Item_ref(ref->context, item_ref, ref->table_name,
783 ref->field_name, ref->alias_name_used);
786 ref->outer_ref= new_ref;
787 ref->ref= &ref->outer_ref;
791 thd->lex->used_tables|= item->used_tables();
792 thd->lex->current_select->select_list_tables|= item->used_tables();
825 void remove_redundant_subquery_clauses(st_select_lex *subq_select_lex,
826 int hidden_group_field_count,
827 int hidden_order_field_count,
831 Item_subselect *subq_predicate= subq_select_lex->master_unit()->item;
839 if (subq_predicate->substype() == Item_subselect::SINGLEROW_SUBS)
843 DBUG_ASSERT (subq_predicate->substype() == Item_subselect::EXISTS_SUBS ||
844 subq_predicate->substype() == Item_subselect::IN_SUBS ||
845 subq_predicate->substype() == Item_subselect::ALL_SUBS ||
846 subq_predicate->substype() == Item_subselect::ANY_SUBS);
851 REMOVE_ORDER= 1 << 0,
852 REMOVE_DISTINCT= 1 << 1,
858 if (subq_select_lex->order_list.elements)
860 changelog|= REMOVE_ORDER;
861 for (
ORDER *o= subq_select_lex->order_list.first; o != NULL; o= o->next)
863 if (*o->item == o->item_ptr)
865 static_cast<uchar*>(static_cast<void*>(subq_select_lex)));
867 subq_select_lex->join->order= NULL;
868 subq_select_lex->order_list.empty();
869 while (hidden_order_field_count-- > 0)
872 ref_pointer_array[fields.elements]= NULL;
876 if (subq_select_lex->options & SELECT_DISTINCT)
878 changelog|= REMOVE_DISTINCT;
879 subq_select_lex->join->select_distinct=
false;
880 subq_select_lex->options&= ~SELECT_DISTINCT;
887 if (subq_select_lex->group_list.elements &&
888 !subq_select_lex->with_sum_func && !subq_select_lex->join->having)
890 changelog|= REMOVE_GROUP;
891 for (
ORDER *g= subq_select_lex->group_list.first; g != NULL; g= g->next)
893 if (*g->item == g->item_ptr)
895 static_cast<uchar*>(static_cast<void*>(subq_select_lex)));
897 subq_select_lex->join->group_list= NULL;
898 subq_select_lex->group_list.empty();
899 while (hidden_group_field_count-- > 0)
902 ref_pointer_array[fields.elements]= NULL;
909 if (unlikely(trace->is_started()))
913 if (changelog & REMOVE_ORDER)
914 trace_changes.add_alnum(
"removed_ordering");
915 if (changelog & REMOVE_DISTINCT)
916 trace_changes.add_alnum(
"removed_distinct");
917 if (changelog & REMOVE_GROUP)
918 trace_changes.add_alnum(
"removed_grouping");
928 setup_without_group(THD *thd,
Ref_ptr_array ref_pointer_array,
936 int *hidden_group_field_count,
937 int *hidden_order_field_count)
940 st_select_lex *
const select= thd->lex->current_select;
941 nesting_map save_allow_sum_func=thd->lex->allow_sum_func;
946 const bool saved_non_agg_field_used= select->non_agg_field_used();
947 DBUG_ENTER(
"setup_without_group");
949 thd->lex->allow_sum_func&= ~((nesting_map)1 << select->nest_level);
950 res= setup_conds(thd, tables, leaves, conds);
953 select->set_non_agg_field_used(saved_non_agg_field_used);
956 int all_fields_count= all_fields.elements;
957 res= res || setup_group(thd, ref_pointer_array, tables, fields, all_fields,
959 *hidden_group_field_count= all_fields.elements - all_fields_count;
962 all_fields_count= all_fields.elements;
963 thd->lex->allow_sum_func|= (nesting_map)1 << select->nest_level;
964 res= res ||
setup_order(thd, ref_pointer_array, tables, fields, all_fields,
966 *hidden_order_field_count= all_fields.elements - all_fields_count;
968 res= res || match_exprs_for_only_full_group_by(thd, all_fields,
969 *hidden_group_field_count,
970 *hidden_order_field_count,
971 fields.elements, group);
973 thd->lex->allow_sum_func= save_allow_sum_func;
1018 bool is_group_field)
1020 Item *order_item= *order->item;
1021 Item::Type order_item_type;
1025 enum_resolution_type resolution;
1031 if (order_item->type() == Item::INT_ITEM && order_item->basic_const_item())
1033 uint count= (uint) order_item->val_int();
1034 if (!count || count > fields.elements)
1036 my_error(ER_BAD_FIELD_ERROR, MYF(0),
1037 order_item->full_name(), thd->where);
1040 order->item= &ref_pointer_array[count - 1];
1041 order->in_field_list= 1;
1042 order->counter= count;
1043 order->counter_used= 1;
1047 select_item= find_item_in_list(order_item, fields, &counter,
1048 REPORT_EXCEPT_NOT_FOUND, &resolution);
1054 if (select_item != not_found_item)
1056 Item *view_ref= NULL;
1062 if (resolution == RESOLVED_BEHIND_ALIAS && !order_item->fixed &&
1063 order_item->fix_fields(thd, order->item))
1067 order_item_type= order_item->type();
1068 from_field= (
Field*) not_found_field;
1069 if ((is_group_field &&
1070 order_item_type == Item::FIELD_ITEM) ||
1071 order_item_type == Item::REF_ITEM)
1073 from_field= find_field_in_tables(thd, (
Item_ident*) order_item, tables,
1074 NULL, &view_ref, IGNORE_ERRORS, TRUE,
1077 from_field= (
Field*) not_found_field;
1080 if (from_field == not_found_field ||
1081 (from_field != view_ref_found ?
1083 ((*select_item)->type() == Item::FIELD_ITEM &&
1084 ((
Item_field*) (*select_item))->field->eq(from_field)) :
1089 ((*select_item)->type() == Item::REF_ITEM &&
1090 view_ref->type() == Item::REF_ITEM &&
1091 ((
Item_ref *) (*select_item))->ref ==
1108 if (*order->item != *select_item)
1110 order->item= &ref_pointer_array[counter];
1111 order->in_field_list=1;
1112 if (resolution == RESOLVED_AGAINST_ALIAS)
1124 push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN, ER_NON_UNIQ_ERROR,
1125 ER(ER_NON_UNIQ_ERROR),
1127 current_thd->where);
1131 order->in_field_list=0;
1156 bool save_group_fix_field= thd->lex->current_select->group_fix_field;
1158 thd->lex->current_select->group_fix_field= TRUE;
1159 bool ret= (!order_item->fixed &&
1160 (order_item->fix_fields(thd, order->item) ||
1161 (order_item= *order->item)->check_cols(1) ||
1162 thd->is_fatal_error));
1163 thd->lex->current_select->group_fix_field= save_group_fix_field;
1167 uint el= all_fields.elements;
1168 all_fields.push_front(order_item);
1169 ref_pointer_array[el]= order_item;
1176 DBUG_ASSERT(order_item == *order->item);
1177 order->item= &ref_pointer_array[el];
1192 thd->where=
"order clause";
1193 DBUG_ASSERT(thd->lex->current_select->cur_pos_in_all_fields ==
1194 SELECT_LEX::ALL_FIELDS_UNDEF_POS);
1195 for (; order; order=order->next)
1197 thd->lex->current_select->cur_pos_in_all_fields=
1198 fields.elements - all_fields.elements - 1;
1199 if (find_order_in_list(thd, ref_pointer_array, tables, order, fields,
1203 thd->lex->current_select->cur_pos_in_all_fields=
1204 SELECT_LEX::ALL_FIELDS_UNDEF_POS;
1243 match_exprs_for_only_full_group_by(THD *thd,
List<Item> &all_fields,
1244 int hidden_group_exprs_count,
1245 int hidden_order_exprs_count,
1246 int select_exprs_count,
1250 !(thd->variables.sql_mode & MODE_ONLY_FULL_GROUP_BY))
1282 const int idx_of_first_hidden_group= hidden_order_exprs_count;
1283 const int idx_of_first_select= all_fields.elements - select_exprs_count;
1288 int cur_pos_in_all_fields;
1292 non_agg_fields_it(thd->lex->current_select->non_agg_fields);
1294 non_agg_field= non_agg_fields_it++;
1295 while (non_agg_field && (expr= exprs_it++))
1298 if (idx >= idx_of_first_hidden_group &&
1299 idx < idx_of_first_select)
1301 cur_pos_in_all_fields= idx - idx_of_first_select;
1303 if ((expr->
marker == SELECT_LEX::ALL_FIELDS_UNDEF_POS) ||
1304 expr->type() == Item::SUM_FUNC_ITEM ||
1305 expr->const_item() ||
1306 (expr->real_item()->type() == Item::FIELD_ITEM &&
1307 expr->used_tables() & OUTER_REF_TABLE_BIT))
1310 while (non_agg_field)
1325 if (non_agg_field->
marker < cur_pos_in_all_fields)
1332 goto next_non_agg_field;
1334 if (non_agg_field->
marker > cur_pos_in_all_fields)
1344 for (
ORDER *grp= group_exprs; grp; grp= grp->next)
1345 if ((*grp->item)->eq(static_cast<Item *>(non_agg_field),
false))
1348 goto next_non_agg_field;
1356 my_error(ER_WRONG_FIELD_WITH_GROUP, MYF(0), non_agg_field->full_name());
1359 non_agg_field= non_agg_fields_it++;
1397 thd->where=
"group statement";
1398 for (
ORDER *ord= order; ord; ord= ord->next)
1400 if (find_order_in_list(thd, ref_pointer_array, tables, ord, fields,
1404 (*ord->item)->marker= SELECT_LEX::ALL_FIELDS_UNDEF_POS;
1405 if ((*ord->item)->with_sum_func)
1407 my_error(ER_WRONG_GROUP_FIELD, MYF(0), (*ord->item)->full_name());
1459 static bool change_group_ref(THD *thd,
Item_func *expr,
ORDER *group_list,
1462 if (expr->arg_count)
1465 Item **arg,**arg_end;
1466 bool arg_changed= FALSE;
1467 for (arg= expr->arguments(),
1468 arg_end= expr->arguments()+expr->arg_count;
1469 arg != arg_end; arg++)
1472 if (item->type() == Item::FIELD_ITEM || item->type() == Item::REF_ITEM)
1475 for (group_tmp= group_list; group_tmp; group_tmp= group_tmp->next)
1477 if (item->
eq(*group_tmp->item,0))
1480 if (!(new_item=
new Item_ref(context, group_tmp->item, 0,
1481 item->item_name.
ptr())))
1483 thd->change_item_tree(arg, new_item);
1488 else if (item->type() == Item::FUNC_ITEM)
1490 if (change_group_ref(thd, (
Item_func *) item, group_list, &arg_changed))
1496 expr->maybe_null= 1;
1512 tmp_table_param.quick_group= 0;
1513 rollup.state= ROLLUP::STATE_INITED;
1519 tmp_table_param.group_parts= send_group_parts;
1525 rollup.ref_pointer_arrays=
1528 all_fields.elements *
sizeof(
Item*)) * send_group_parts));
1532 if (!null_items || !
rollup.ref_pointer_arrays || !
rollup.fields)
1535 ref_array= (
Item**) (
rollup.ref_pointer_arrays+send_group_parts);
1541 group_tmp= group_list;
1542 for (i= 0 ; i < send_group_parts ; i++)
1546 (*group_tmp->item)->result_type());
1548 rollup_fields->empty();
1550 ref_array+= all_fields.elements;
1551 group_tmp= group_tmp->next;
1553 for (i= 0 ; i < send_group_parts; i++)
1560 while ((item= it++))
1562 bool found_in_group= 0;
1564 for (group_tmp= group_list; group_tmp; group_tmp= group_tmp->next)
1566 if (*group_tmp->item == item)
1568 item->maybe_null= 1;
1573 if (item->type() == Item::FUNC_ITEM && !found_in_group)
1575 bool changed= FALSE;
1576 if (change_group_ref(thd, (
Item_func *) item, group_list, &changed))
1584 item->with_sum_func= 1;