From 590400f743d029f13ddd19e07ccac0153c2a7c1f Mon Sep 17 00:00:00 2001 From: Alexander Barkov Date: Tue, 28 Nov 2017 06:25:14 +0400 Subject: [PATCH] MDEV-14517 Cleanup for Item::with_subselect and Item::has_subquery() --- sql/filesort.cc | 4 ++-- sql/item.cc | 6 ++---- sql/item.h | 37 +++++++++++++++++++++++++++---------- sql/item_cmpfunc.cc | 6 +++--- sql/item_cmpfunc.h | 2 +- sql/item_func.cc | 4 ++-- sql/item_row.cc | 2 +- sql/item_row.h | 4 +++- sql/item_subselect.cc | 1 - sql/item_subselect.h | 1 + sql/item_sum.cc | 6 +++--- sql/item_windowfunc.cc | 4 ++-- sql/opt_subselect.cc | 2 +- sql/sql_select.cc | 2 +- 14 files changed, 49 insertions(+), 32 deletions(-) diff --git a/sql/filesort.cc b/sql/filesort.cc index fc6453b904b..4fffd0dca91 100644 --- a/sql/filesort.cc +++ b/sql/filesort.cc @@ -830,11 +830,11 @@ static ha_rows find_all_keys(THD *thd, Sort_param *param, SQL_SELECT *select, MY_BITMAP *tmp_write_set= sort_form->write_set; MY_BITMAP *tmp_vcol_set= sort_form->vcol_set; - if (select->cond->with_subselect) + if (select->cond->with_subquery()) sort_form->column_bitmaps_set(save_read_set, save_write_set, save_vcol_set); write_record= (select->skip_record(thd) > 0); - if (select->cond->with_subselect) + if (select->cond->with_subquery()) sort_form->column_bitmaps_set(tmp_read_set, tmp_write_set, tmp_vcol_set); diff --git a/sql/item.cc b/sql/item.cc index 29d97aa4b23..a919a7010ae 100644 --- a/sql/item.cc +++ b/sql/item.cc @@ -532,7 +532,6 @@ Item::Item(THD *thd): marker= 0; maybe_null=null_value=with_sum_func=with_window_func=with_field=0; in_rollup= 0; - with_subselect= 0; /* Initially this item is not attached to any JOIN_TAB. */ join_tab_idx= MAX_TABLES; @@ -577,8 +576,7 @@ Item::Item(THD *thd, Item *item): with_window_func(item->with_window_func), with_field(item->with_field), fixed(item->fixed), - is_autogenerated_name(item->is_autogenerated_name), - with_subselect(item->has_subquery()) + is_autogenerated_name(item->is_autogenerated_name) { next= thd->free_list; // Put in free list thd->free_list= this; @@ -8194,7 +8192,7 @@ Item_cache_wrapper::Item_cache_wrapper(THD *thd, Item *item_arg): with_sum_func= orig_item->with_sum_func; with_field= orig_item->with_field; name= item_arg->name; - with_subselect= orig_item->with_subselect; + m_with_subquery= orig_item->with_subquery(); if ((expr_value= orig_item->get_cache(thd))) expr_value->setup(thd, orig_item); diff --git a/sql/item.h b/sql/item.h index a2fbf07dc49..88146c4cee8 100644 --- a/sql/item.h +++ b/sql/item.h @@ -703,9 +703,6 @@ public: bool fixed; /* If item fixed with fix_fields */ bool is_autogenerated_name; /* indicate was name of this Item autogenerated or set by user */ - bool with_subselect; /* If this item is a subselect or some - of its arguments is or contains a - subselect */ // alloc & destruct is done as start of select on THD::mem_root Item(THD *thd); /* @@ -1871,9 +1868,10 @@ public: virtual bool is_outer_field() const { DBUG_ASSERT(fixed); return FALSE; } /** - Checks if this item or any of its decendents contains a subquery. + Checks if this item or any of its decendents contains a subquery. This is a + replacement of the former Item::has_subquery() and Item::with_subselect. */ - virtual bool has_subquery() const { return with_subselect; } + virtual bool with_subquery() const { DBUG_ASSERT(fixed); return false; } Item* set_expr_cache(THD *thd); @@ -1949,6 +1947,21 @@ inline Item* get_item_copy (THD *thd, MEM_ROOT *mem_root, T* item) } +/* + This class is a replacement for the former member Item::with_subselect. + Determines if the descendant Item is a subselect or some of + its arguments is or contains a subselect. +*/ +class With_subquery_cache +{ +protected: + bool m_with_subquery; +public: + With_subquery_cache(): m_with_subquery(false) { } + void join(const Item *item) { m_with_subquery|= item->with_subquery(); } +}; + + class Type_geometry_attributes { uint m_geometry_type; @@ -4268,7 +4281,8 @@ public: */ class Item_func_or_sum: public Item_result_field, public Item_args, - public Used_tables_and_const_cache + public Used_tables_and_const_cache, + public With_subquery_cache { protected: bool agg_arg_charsets(DTCollation &c, Item **items, uint nitems, @@ -4351,6 +4365,7 @@ public: Used_tables_and_const_cache(item) { } Item_func_or_sum(THD *thd, List &list): Item_result_field(thd), Item_args(thd, list) { } + bool with_subquery() const { DBUG_ASSERT(fixed); return m_with_subquery; } bool walk(Item_processor processor, bool walk_subquery, void *arg) { if (walk_args(processor, walk_subquery, arg)) @@ -4565,9 +4580,9 @@ public: /** Checks if the item tree that ref points to contains a subquery. */ - virtual bool has_subquery() const - { - return (*ref)->has_subquery(); + virtual bool with_subquery() const + { + return (*ref)->with_subquery(); } Item *get_copy(THD *thd, MEM_ROOT *mem_root) { return get_item_copy(thd, mem_root, this); } @@ -4684,7 +4699,8 @@ class Expression_cache_tracker; The objects of this class can store its values in an expression cache. */ -class Item_cache_wrapper :public Item_result_field +class Item_cache_wrapper :public Item_result_field, + public With_subquery_cache { private: /* Pointer on the cached expression */ @@ -4711,6 +4727,7 @@ public: enum Type type() const { return EXPR_CACHE_ITEM; } enum Type real_type() const { return orig_item->type(); } + bool with_subquery() const { DBUG_ASSERT(fixed); return m_with_subquery; } bool set_cache(THD *thd); Expression_cache_tracker* init_tracker(MEM_ROOT *mem_root); diff --git a/sql/item_cmpfunc.cc b/sql/item_cmpfunc.cc index f72426bd18c..e4b8153507a 100644 --- a/sql/item_cmpfunc.cc +++ b/sql/item_cmpfunc.cc @@ -1365,7 +1365,7 @@ bool Item_in_optimizer::fix_fields(THD *thd, Item **ref) } if (args[1]->maybe_null) maybe_null=1; - with_subselect= 1; + m_with_subquery= true; with_sum_func= with_sum_func || args[1]->with_sum_func; with_field= with_field || args[1]->with_field; used_tables_and_const_cache_join(args[1]); @@ -4603,7 +4603,7 @@ Item_cond::fix_fields(THD *thd, Item **ref) with_sum_func|= item->with_sum_func; with_field|= item->with_field; - with_subselect|= item->has_subquery(); + m_with_subquery|= item->with_subquery(); with_window_func|= item->with_window_func; maybe_null|= item->maybe_null; } @@ -6607,7 +6607,7 @@ bool Item_equal::fix_fields(THD *thd, Item **ref) used_tables_cache|= item->used_tables(); tmp_table_map= item->not_null_tables(); not_null_tables_cache|= tmp_table_map; - DBUG_ASSERT(!item->with_sum_func && !item->with_subselect); + DBUG_ASSERT(!item->with_sum_func && !item->with_subquery()); if (item->maybe_null) maybe_null= 1; if (!item->get_item_equal()) diff --git a/sql/item_cmpfunc.h b/sql/item_cmpfunc.h index 49716fb3c8f..e18315fde87 100644 --- a/sql/item_cmpfunc.h +++ b/sql/item_cmpfunc.h @@ -356,7 +356,7 @@ public: Item_in_optimizer(THD *thd, Item *a, Item *b): Item_bool_func(thd, a, b), cache(0), expr_cache(0), save_cache(0), result_for_null_param(UNKNOWN) - { with_subselect= true; } + { m_with_subquery= true; } bool fix_fields(THD *, Item **); bool fix_left(THD *thd); table_map not_null_tables() const { return 0; } diff --git a/sql/item_func.cc b/sql/item_func.cc index e9cf5a695d0..be5e5064d60 100644 --- a/sql/item_func.cc +++ b/sql/item_func.cc @@ -362,7 +362,7 @@ Item_func::fix_fields(THD *thd, Item **ref) with_window_func= with_window_func || item->with_window_func; with_field= with_field || item->with_field; used_tables_and_const_cache_join(item); - with_subselect|= item->has_subquery(); + m_with_subquery|= item->with_subquery(); } } if (check_arguments()) @@ -3248,7 +3248,7 @@ udf_handler::fix_fields(THD *thd, Item_func_or_sum *func, func->maybe_null=1; func->with_sum_func= func->with_sum_func || item->with_sum_func; func->with_field= func->with_field || item->with_field; - func->with_subselect|= item->with_subselect; + func->With_subquery_cache::join(item); func->used_tables_and_const_cache_join(item); f_args.arg_type[i]=item->result_type(); } diff --git a/sql/item_row.cc b/sql/item_row.cc index 3660c983f87..c7e0f164af9 100644 --- a/sql/item_row.cc +++ b/sql/item_row.cc @@ -64,7 +64,7 @@ bool Item_row::fix_fields(THD *thd, Item **ref) with_sum_func= with_sum_func || item->with_sum_func; with_window_func = with_window_func || item->with_window_func; with_field= with_field || item->with_field; - with_subselect|= item->with_subselect; + m_with_subquery|= item->with_subquery(); } fixed= 1; return FALSE; diff --git a/sql/item_row.h b/sql/item_row.h index a6fdd2b212c..206a8ed83c5 100644 --- a/sql/item_row.h +++ b/sql/item_row.h @@ -35,7 +35,8 @@ */ class Item_row: public Item, private Item_args, - private Used_tables_and_const_cache + private Used_tables_and_const_cache, + private With_subquery_cache { table_map not_null_tables_cache; /** @@ -52,6 +53,7 @@ public: not_null_tables_cache(0), with_null(0) { } + bool with_subquery() const { DBUG_ASSERT(fixed); return m_with_subquery; } enum Type type() const { return ROW_ITEM; }; const Type_handler *type_handler() const { return &type_handler_row; } void illegal_method_call(const char *); diff --git a/sql/item_subselect.cc b/sql/item_subselect.cc index 678b4192f45..8e4c5dcbbf1 100644 --- a/sql/item_subselect.cc +++ b/sql/item_subselect.cc @@ -64,7 +64,6 @@ Item_subselect::Item_subselect(THD *thd_arg): #ifndef DBUG_OFF exec_counter= 0; #endif - with_subselect= 1; reset(); /* Item value is NULL if select_result_interceptor didn't change this value diff --git a/sql/item_subselect.h b/sql/item_subselect.h index df94ceb2493..812352e4c4f 100644 --- a/sql/item_subselect.h +++ b/sql/item_subselect.h @@ -182,6 +182,7 @@ public: return null_value; } bool fix_fields(THD *thd, Item **ref); + bool with_subquery() const { DBUG_ASSERT(fixed); return true; } bool mark_as_dependent(THD *thd, st_select_lex *select, Item *item); void fix_after_pullout(st_select_lex *new_parent, Item **ref, bool merge); void recalc_used_tables(st_select_lex *new_parent, bool after_pullout); diff --git a/sql/item_sum.cc b/sql/item_sum.cc index c0c5e1a43b4..4fecd7de269 100644 --- a/sql/item_sum.cc +++ b/sql/item_sum.cc @@ -1126,7 +1126,7 @@ Item_sum_num::fix_fields(THD *thd, Item **ref) if (args[i]->fix_fields(thd, args + i) || args[i]->check_cols(1)) return TRUE; set_if_bigger(decimals, args[i]->decimals); - with_subselect|= args[i]->with_subselect; + m_with_subquery|= args[i]->with_subquery(); with_window_func|= args[i]->with_window_func; } result_field=0; @@ -1157,7 +1157,7 @@ Item_sum_hybrid::fix_fields(THD *thd, Item **ref) if ((!item->fixed && item->fix_fields(thd, args)) || (item= args[0])->check_cols(1)) return TRUE; - with_subselect= args[0]->with_subselect; + m_with_subquery= args[0]->with_subquery(); with_window_func|= args[0]->with_window_func; fix_length_and_dec(); @@ -3447,7 +3447,7 @@ Item_func_group_concat::fix_fields(THD *thd, Item **ref) args[i]->fix_fields(thd, args + i)) || args[i]->check_cols(1)) return TRUE; - with_subselect|= args[i]->with_subselect; + m_with_subquery|= args[i]->with_subquery(); with_window_func|= args[i]->with_window_func; } diff --git a/sql/item_windowfunc.cc b/sql/item_windowfunc.cc index a216930cad6..f993276dab3 100644 --- a/sql/item_windowfunc.cc +++ b/sql/item_windowfunc.cc @@ -238,8 +238,8 @@ bool Item_sum_hybrid_simple::fix_fields(THD *thd, Item **ref) return TRUE; } Type_std_attributes::set(args[0]); - for (uint i= 0; i < arg_count && !with_subselect; i++) - with_subselect= with_subselect || args[i]->with_subselect; + for (uint i= 0; i < arg_count && !m_with_subquery; i++) + m_with_subquery|= args[i]->with_subquery(); Item *item2= args[0]->real_item(); if (item2->type() == Item::FIELD_ITEM) diff --git a/sql/opt_subselect.cc b/sql/opt_subselect.cc index 04d0a9b4a3f..0ab588f7d19 100644 --- a/sql/opt_subselect.cc +++ b/sql/opt_subselect.cc @@ -1629,7 +1629,7 @@ static bool convert_subq_to_sj(JOIN *parent_join, Item_in_subselect *subq_pred) // The subqueries were replaced for Item_int(1) earlier subq_pred->reset_strategy(SUBS_SEMI_JOIN); // for subsequent executions - /*TODO: also reset the 'with_subselect' there. */ + /*TODO: also reset the 'm_with_subquery' there. */ /* n. Adjust the parent_join->table_count counter */ uint table_no= parent_join->table_count; diff --git a/sql/sql_select.cc b/sql/sql_select.cc index c576aae32ad..f894f37caf4 100644 --- a/sql/sql_select.cc +++ b/sql/sql_select.cc @@ -12810,7 +12810,7 @@ remove_const(JOIN *join,ORDER *first_order, COND *cond, *simple_order=0; // Must do a temp table to sort else if (!(order_tables & not_const_tables)) { - if (order->item[0]->has_subquery()) + if (order->item[0]->with_subquery()) { /* Delay the evaluation of constant ORDER and/or GROUP expressions that