| 29973 | */ |
| 29974 | |
| 29975 | static bool |
| 29976 | change_to_use_tmp_fields(THD *thd, Ref_ptr_array ref_pointer_array, |
| 29977 | List<Item> &res_selected_fields, |
| 29978 | List<Item> &res_all_fields, |
| 29979 | uint elements, List<Item> &all_fields) |
| 29980 | { |
| 29981 | List_iterator_fast<Item> it(all_fields); |
| 29982 | Item *item_field,*item; |
| 29983 | DBUG_ENTER("change_to_use_tmp_fields"); |
| 29984 | |
| 29985 | res_selected_fields.empty(); |
| 29986 | res_all_fields.empty(); |
| 29987 | |
| 29988 | uint border= all_fields.elements - elements; |
| 29989 | for (uint i= 0; (item= it++); i++) |
| 29990 | { |
| 29991 | Field *field; |
| 29992 | /* |
| 29993 | SUM_FUNC_ITEM will be replaced by the calculated value which is |
| 29994 | stored in the temporary table. |
| 29995 | The first part of the following test is for items that are expressions |
| 29996 | with SUM_FUNC_ITEMS, like 'sum(a)+1'. In this case we keep the original |
| 29997 | item, which contain an Item_ref that points to the SUM_FUNC_ITEM that |
| 29998 | will be replaced with a pointer to the calculated value. |
| 29999 | The second test is for window functions. Window functions contains |
| 30000 | only pointers to Item_refs, which will be adjusted to point to the |
| 30001 | temporary table. |
| 30002 | */ |
| 30003 | enum Item::Type item_type= item->type(); |
| 30004 | if ((item->with_sum_func() && item_type != Item::SUM_FUNC_ITEM) || |
| 30005 | item->with_window_func()) |
| 30006 | item_field= item; |
| 30007 | else if (item_type == Item::FIELD_ITEM || |
| 30008 | item_type == Item::DEFAULT_VALUE_ITEM) |
| 30009 | { |
| 30010 | if (!(item_field= item->get_tmp_table_item(thd))) |
| 30011 | DBUG_RETURN(true); |
| 30012 | } |
| 30013 | else if (item_type == Item::FUNC_ITEM && |
| 30014 | ((Item_func*)item)->functype() == Item_func::SUSERVAR_FUNC) |
| 30015 | { |
| 30016 | field= item->get_tmp_table_field(); |
| 30017 | if (field != NULL) |
| 30018 | { |
| 30019 | /* |
| 30020 | Replace "@:=<expression>" with "@:=<tmp table |
| 30021 | column>". Otherwise, we would re-evaluate <expression>, and |
| 30022 | if expression were a subquery, this would access |
| 30023 | already-unlocked tables. |
| 30024 | */ |
| 30025 | Item_func_set_user_var* suv= |
| 30026 | new (thd->mem_root) Item_func_set_user_var(thd, (Item_func_set_user_var*) item); |
| 30027 | Item_field *new_field= new (thd->mem_root) Item_field(thd, field); |
| 30028 | if (!suv || !new_field) |
| 30029 | DBUG_RETURN(true); // Fatal error |
| 30030 | new_field->set_refers_to_temp_table(); |
| 30031 | List<Item> list; |
| 30032 | list.push_back(new_field, thd->mem_root); |
no test coverage detected