From 60d358af278413af666ab06e07f4c58a41bd5374 Mon Sep 17 00:00:00 2001 From: Evgeny Potemkin Date: Fri, 6 Nov 2009 22:34:25 +0300 Subject: Bug#34384: Slow down on constant conversion. When values of different types are compared they're converted to a type that allows correct comparison. This conversion is done for each comparison and takes some time. When a constant is being compared it's possible to cache the value after conversion to speedup comparison. In some cases (large dataset, complex WHERE condition with many type conversions) query might be executed 7% faster. A test case isn't provided because all changes are internal and isn't visible outside. The behavior of the Item_cache is changed to cache values on the first request of cached value rather than at the moment of storing item to be cached. A flag named value_cached is added to the Item_cache class. It's set to TRUE when cache holds the value of the last stored item. Function named cache_value() is added to the Item_cache class and derived classes. This function actually caches the value of the saved item. Item_cache_xxx::store functions now only store item to be cached and set value_cached flag to FALSE. Item_cache_xxx::val_xxx functions are changed to call cache_value function prior to returning cached value if value_cached is FALSE. The Arg_comparator::set_cmp_func function now calls cache_converted_constant to cache constants if they need a type conversion. The Item_cache::get_cache function is overloaded to allow setting of the cache type. The cache_converted_constant function is added to the Arg_comparator class. It checks whether a value can and should be cached and if so caches it. --- sql/item.cc | 129 +++++++++++++++++++++++++++++++++++++++++++------- sql/item.h | 50 +++++++++++++------ sql/item_cmpfunc.cc | 47 ++++++++++++++++-- sql/item_cmpfunc.h | 2 + sql/item_subselect.cc | 2 + sql/item_xmlfunc.cc | 6 ++- sql/sp_rcontext.cc | 2 +- 7 files changed, 198 insertions(+), 40 deletions(-) (limited to 'sql') diff --git a/sql/item.cc b/sql/item.cc index f637f9ffaea..d394435baed 100644 --- a/sql/item.cc +++ b/sql/item.cc @@ -6958,7 +6958,22 @@ int stored_field_cmp_to_item(Field *field, Item *item) Item_cache* Item_cache::get_cache(const Item *item) { - switch (item->result_type()) { + return get_cache(item, item->result_type()); +} + + +/** + Get a cache item of given type. + + @param item value to be cached + @param type required type of cache + + @return cache item +*/ + +Item_cache* Item_cache::get_cache(const Item *item, const Item_result type) +{ + switch (type) { case INT_RESULT: return new Item_cache_int(); case REAL_RESULT: @@ -6976,6 +6991,12 @@ Item_cache* Item_cache::get_cache(const Item *item) } } +void Item_cache::store(Item *item) +{ + if (item) + example= item; + value_cached= FALSE; +} void Item_cache::print(String *str, enum_query_type query_type) { @@ -6987,17 +7008,19 @@ void Item_cache::print(String *str, enum_query_type query_type) str->append(')'); } - -void Item_cache_int::store(Item *item) +void Item_cache_int::cache_value() { - value= item->val_int_result(); - null_value= item->null_value; - unsigned_flag= item->unsigned_flag; + value_cached= TRUE; + value= example->val_int_result(); + null_value= example->null_value; + unsigned_flag= example->unsigned_flag; } void Item_cache_int::store(Item *item, longlong val_arg) { + /* An explicit values is given, save it. */ + value_cached= TRUE; value= val_arg; null_value= item->null_value; unsigned_flag= item->unsigned_flag; @@ -7007,6 +7030,8 @@ void Item_cache_int::store(Item *item, longlong val_arg) String *Item_cache_int::val_str(String *str) { DBUG_ASSERT(fixed == 1); + if (!value_cached) + cache_value(); str->set(value, default_charset()); return str; } @@ -7015,21 +7040,49 @@ String *Item_cache_int::val_str(String *str) my_decimal *Item_cache_int::val_decimal(my_decimal *decimal_val) { DBUG_ASSERT(fixed == 1); + if (!value_cached) + cache_value(); int2my_decimal(E_DEC_FATAL_ERROR, value, unsigned_flag, decimal_val); return decimal_val; } +double Item_cache_int::val_real() +{ + DBUG_ASSERT(fixed == 1); + if (!value_cached) + cache_value(); + return (double) value; +} -void Item_cache_real::store(Item *item) +longlong Item_cache_int::val_int() { - value= item->val_result(); - null_value= item->null_value; + DBUG_ASSERT(fixed == 1); + if (!value_cached) + cache_value(); + return value; } +void Item_cache_real::cache_value() +{ + value_cached= TRUE; + value= example->val_result(); + null_value= example->null_value; +} + + +double Item_cache_real::val_real() +{ + DBUG_ASSERT(fixed == 1); + if (!value_cached) + cache_value(); + return value; +} longlong Item_cache_real::val_int() { DBUG_ASSERT(fixed == 1); + if (!value_cached) + cache_value(); return (longlong) rint(value); } @@ -7037,6 +7090,8 @@ longlong Item_cache_real::val_int() String* Item_cache_real::val_str(String *str) { DBUG_ASSERT(fixed == 1); + if (!value_cached) + cache_value(); str->set_real(value, decimals, default_charset()); return str; } @@ -7045,15 +7100,18 @@ String* Item_cache_real::val_str(String *str) my_decimal *Item_cache_real::val_decimal(my_decimal *decimal_val) { DBUG_ASSERT(fixed == 1); + if (!value_cached) + cache_value(); double2my_decimal(E_DEC_FATAL_ERROR, value, decimal_val); return decimal_val; } -void Item_cache_decimal::store(Item *item) +void Item_cache_decimal::cache_value() { - my_decimal *val= item->val_decimal_result(&decimal_value); - if (!(null_value= item->null_value) && val != &decimal_value) + value_cached= TRUE; + my_decimal *val= example->val_decimal_result(&decimal_value); + if (!(null_value= example->null_value) && val != &decimal_value) my_decimal2decimal(val, &decimal_value); } @@ -7061,6 +7119,8 @@ double Item_cache_decimal::val_real() { DBUG_ASSERT(fixed); double res; + if (!value_cached) + cache_value(); my_decimal2double(E_DEC_FATAL_ERROR, &decimal_value, &res); return res; } @@ -7069,6 +7129,8 @@ longlong Item_cache_decimal::val_int() { DBUG_ASSERT(fixed); longlong res; + if (!value_cached) + cache_value(); my_decimal2int(E_DEC_FATAL_ERROR, &decimal_value, unsigned_flag, &res); return res; } @@ -7076,6 +7138,8 @@ longlong Item_cache_decimal::val_int() String* Item_cache_decimal::val_str(String *str) { DBUG_ASSERT(fixed); + if (!value_cached) + cache_value(); my_decimal_round(E_DEC_FATAL_ERROR, &decimal_value, decimals, FALSE, &decimal_value); my_decimal2string(E_DEC_FATAL_ERROR, &decimal_value, 0, 0, 0, str); @@ -7085,15 +7149,18 @@ String* Item_cache_decimal::val_str(String *str) my_decimal *Item_cache_decimal::val_decimal(my_decimal *val) { DBUG_ASSERT(fixed); + if (!value_cached) + cache_value(); return &decimal_value; } -void Item_cache_str::store(Item *item) +void Item_cache_str::cache_value() { - value_buff.set(buffer, sizeof(buffer), item->collation.collation); - value= item->str_result(&value_buff); - if ((null_value= item->null_value)) + value_cached= TRUE; + value_buff.set(buffer, sizeof(buffer), example->collation.collation); + value= example->str_result(&value_buff); + if ((null_value= example->null_value)) value= 0; else if (value != &value_buff) { @@ -7115,6 +7182,8 @@ double Item_cache_str::val_real() DBUG_ASSERT(fixed == 1); int err_not_used; char *end_not_used; + if (!value_cached) + cache_value(); if (value) return my_strntod(value->charset(), (char*) value->ptr(), value->length(), &end_not_used, &err_not_used); @@ -7126,6 +7195,8 @@ longlong Item_cache_str::val_int() { DBUG_ASSERT(fixed == 1); int err; + if (!value_cached) + cache_value(); if (value) return my_strntoll(value->charset(), value->ptr(), value->length(), 10, (char**) 0, &err); @@ -7133,9 +7204,21 @@ longlong Item_cache_str::val_int() return (longlong)0; } + +String* Item_cache_str::val_str(String *str) +{ + DBUG_ASSERT(fixed == 1); + if (!value_cached) + cache_value(); + return value; +} + + my_decimal *Item_cache_str::val_decimal(my_decimal *decimal_val) { DBUG_ASSERT(fixed == 1); + if (!value_cached) + cache_value(); if (value) string2my_decimal(E_DEC_FATAL_ERROR, value, decimal_val); else @@ -7146,6 +7229,8 @@ my_decimal *Item_cache_str::val_decimal(my_decimal *decimal_val) int Item_cache_str::save_in_field(Field *field, bool no_conversions) { + if (!value_cached) + cache_value(); int res= Item_cache::save_in_field(field, no_conversions); return (is_varbinary && field->type() == MYSQL_TYPE_STRING && value->length() < field->field_length) ? 1 : res; @@ -7180,11 +7265,19 @@ bool Item_cache_row::setup(Item * item) void Item_cache_row::store(Item * item) { + for (uint i= 0; i < item_count; i++) + values[i]->store(item->element_index(i)); +} + + +void Item_cache_row::cache_value() +{ + value_cached= TRUE; null_value= 0; - item->bring_value(); + example->bring_value(); for (uint i= 0; i < item_count; i++) { - values[i]->store(item->element_index(i)); + values[i]->cache_value(); null_value|= values[i]->null_value; } } diff --git a/sql/item.h b/sql/item.h index a2cff3ab3a9..ff951754b98 100644 --- a/sql/item.h +++ b/sql/item.h @@ -1025,7 +1025,11 @@ class sp_head; class Item_basic_constant :public Item { + table_map used_table_map; public: + Item_basic_constant(): Item(), used_table_map(0) {}; + void set_used_tables(table_map map) { used_table_map= map; } + table_map used_tables() const { return used_table_map; } /* to prevent drop fixed flag (no need parent cleanup call) */ void cleanup() { @@ -2890,15 +2894,25 @@ protected: */ Field *cached_field; enum enum_field_types cached_field_type; + /* + TRUE <=> cache holds value of the last stored item (i.e actual value). + store() stores item to be cached and sets this flag to FALSE. + On the first call of val_xxx function if this flag is set to FALSE the + cache_value() will be called to actually cache value of saved item. + cache_value() will set this flag to TRUE. + */ + bool value_cached; public: - Item_cache(): - example(0), used_table_map(0), cached_field(0), cached_field_type(MYSQL_TYPE_STRING) + Item_cache(): + example(0), used_table_map(0), cached_field(0), cached_field_type(MYSQL_TYPE_STRING), + value_cached(0) { fixed= 1; null_value= 1; } Item_cache(enum_field_types field_type_arg): - example(0), used_table_map(0), cached_field(0), cached_field_type(field_type_arg) + example(0), used_table_map(0), cached_field(0), cached_field_type(field_type_arg), + value_cached(0) { fixed= 1; null_value= 1; @@ -2918,10 +2932,10 @@ public: cached_field= ((Item_field *)item)->field; return 0; }; - virtual void store(Item *)= 0; enum Type type() const { return CACHE_ITEM; } enum_field_types field_type() const { return cached_field_type; } static Item_cache* get_cache(const Item *item); + static Item_cache* get_cache(const Item* item, const Item_result type); table_map used_tables() const { return used_table_map; } virtual void keep_array() {} virtual void print(String *str, enum_query_type query_type); @@ -2933,6 +2947,8 @@ public: { return this == item; } + virtual void store(Item *item); + virtual void cache_value()= 0; }; @@ -2941,18 +2957,19 @@ class Item_cache_int: public Item_cache protected: longlong value; public: - Item_cache_int(): Item_cache(), value(0) {} + Item_cache_int(): Item_cache(), + value(0) {} Item_cache_int(enum_field_types field_type_arg): Item_cache(field_type_arg), value(0) {} - void store(Item *item); void store(Item *item, longlong val_arg); - double val_real() { DBUG_ASSERT(fixed == 1); return (double) value; } - longlong val_int() { DBUG_ASSERT(fixed == 1); return value; } + double val_real(); + longlong val_int(); String* val_str(String *str); my_decimal *val_decimal(my_decimal *); enum Item_result result_type() const { return INT_RESULT; } bool result_as_longlong() { return TRUE; } + void cache_value(); }; @@ -2960,14 +2977,15 @@ class Item_cache_real: public Item_cache { double value; public: - Item_cache_real(): Item_cache(), value(0) {} + Item_cache_real(): Item_cache(), + value(0) {} - void store(Item *item); - double val_real() { DBUG_ASSERT(fixed == 1); return value; } + double val_real(); longlong val_int(); String* val_str(String *str); my_decimal *val_decimal(my_decimal *); enum Item_result result_type() const { return REAL_RESULT; } + void cache_value(); }; @@ -2978,12 +2996,12 @@ protected: public: Item_cache_decimal(): Item_cache() {} - void store(Item *item); double val_real(); longlong val_int(); String* val_str(String *str); my_decimal *val_decimal(my_decimal *); enum Item_result result_type() const { return DECIMAL_RESULT; } + void cache_value(); }; @@ -3001,14 +3019,14 @@ public: MYSQL_TYPE_VARCHAR && !((const Item_field *) item)->field->has_charset()) {} - void store(Item *item); double val_real(); longlong val_int(); - String* val_str(String *) { DBUG_ASSERT(fixed == 1); return value; } + String* val_str(String *); my_decimal *val_decimal(my_decimal *); enum Item_result result_type() const { return STRING_RESULT; } CHARSET_INFO *charset() const { return value->charset(); }; int save_in_field(Field *field, bool no_conversions); + void cache_value(); }; class Item_cache_row: public Item_cache @@ -3018,7 +3036,8 @@ class Item_cache_row: public Item_cache bool save_array; public: Item_cache_row() - :Item_cache(), values(0), item_count(2), save_array(0) {} + :Item_cache(), values(0), item_count(2), + save_array(0) {} /* 'allocate' used only in row transformer, to preallocate space for row @@ -3076,6 +3095,7 @@ public: values= 0; DBUG_VOID_RETURN; } + void cache_value(); }; diff --git a/sql/item_cmpfunc.cc b/sql/item_cmpfunc.cc index c29031d25b5..97ad45d9774 100644 --- a/sql/item_cmpfunc.cc +++ b/sql/item_cmpfunc.cc @@ -855,13 +855,13 @@ int Arg_comparator::set_cmp_func(Item_bool_func2 *owner_arg, { enum enum_date_cmp_type cmp_type; ulonglong const_value= (ulonglong)-1; + thd= current_thd; + owner= owner_arg; a= a1; b= a2; if ((cmp_type= can_compare_as_dates(*a, *b, &const_value))) { - thd= current_thd; - owner= owner_arg; a_type= (*a)->field_type(); b_type= (*b)->field_type(); a_cache= 0; @@ -869,6 +869,10 @@ int Arg_comparator::set_cmp_func(Item_bool_func2 *owner_arg, if (const_value != (ulonglong)-1) { + /* + cache_converted_constant can't be used here because it can't + correctly convert a DATETIME value from string to int representation. + */ Item_cache_int *cache= new Item_cache_int(); /* Mark the cache as non-const to prevent re-caching. */ cache->set_used_tables(1); @@ -894,8 +898,6 @@ int Arg_comparator::set_cmp_func(Item_bool_func2 *owner_arg, (*b)->field_type() == MYSQL_TYPE_TIME) { /* Compare TIME values as integers. */ - thd= current_thd; - owner= owner_arg; a_cache= 0; b_cache= 0; is_nulls_eq= test(owner && owner->functype() == Item_func::EQUAL_FUNC); @@ -914,10 +916,46 @@ int Arg_comparator::set_cmp_func(Item_bool_func2 *owner_arg, return 1; } + a= cache_converted_constant(thd, a, &a_cache, type); + b= cache_converted_constant(thd, b, &b_cache, type); return set_compare_func(owner_arg, type); } +/** + Convert and cache a constant. + + @param value [in] An item to cache + @param cache_item [out] Placeholder for the cache item + @param type [in] Comparison type + + @details + When given item is a constant and its type differs from comparison type + then cache its value to avoid type conversion of this constant on each + evaluation. In this case the value is cached and the reference to the cache + is returned. + Original value is returned otherwise. + + @return cache item or original value. +*/ + +Item** Arg_comparator::cache_converted_constant(THD *thd, Item **value, + Item **cache_item, + Item_result type) +{ + /* Don't need cache if doing context analysis only. */ + if (!thd->is_context_analysis_only() && + (*value)->const_item() && type != (*value)->result_type()) + { + Item_cache *cache= Item_cache::get_cache(*value, type); + cache->store(*value); + *cache_item= cache; + return cache_item; + } + return value; +} + + void Arg_comparator::set_datetime_cmp_func(Item **a1, Item **b1) { thd= current_thd; @@ -1556,6 +1594,7 @@ longlong Item_in_optimizer::val_int() bool tmp; DBUG_ASSERT(fixed == 1); cache->store(args[0]); + cache->cache_value(); if (cache->null_value) { diff --git a/sql/item_cmpfunc.h b/sql/item_cmpfunc.h index c2227fa04e0..c6badd9d920 100644 --- a/sql/item_cmpfunc.h +++ b/sql/item_cmpfunc.h @@ -94,6 +94,8 @@ public: ulonglong *const_val_arg); void set_datetime_cmp_func(Item **a1, Item **b1); + Item** cache_converted_constant(THD *thd, Item **value, Item **cache, + Item_result type); static arg_cmp_func comparator_matrix [5][2]; friend class Item_func; diff --git a/sql/item_subselect.cc b/sql/item_subselect.cc index da651cec70c..27beadaed17 100644 --- a/sql/item_subselect.cc +++ b/sql/item_subselect.cc @@ -475,6 +475,7 @@ Item_singlerow_subselect::select_transformer(JOIN *join) void Item_singlerow_subselect::store(uint i, Item *item) { row[i]->store(item); + row[i]->cache_value(); } enum Item_result Item_singlerow_subselect::result_type() const @@ -1826,6 +1827,7 @@ void subselect_engine::set_row(List &item_list, Item_cache **row) if (!(row[i]= Item_cache::get_cache(sel_item))) return; row[i]->setup(sel_item); + row[i]->store(sel_item); } if (item_list.elements > 1) res_type= ROW_RESULT; diff --git a/sql/item_xmlfunc.cc b/sql/item_xmlfunc.cc index 1eff00027f2..3e20b90e68e 100644 --- a/sql/item_xmlfunc.cc +++ b/sql/item_xmlfunc.cc @@ -941,14 +941,16 @@ static Item *create_comparator(MY_XPATH *xpath, in a loop through all of the nodes in the node set. */ - Item *fake= new Item_string("", 0, xpath->cs); + Item_string *fake= new Item_string("", 0, xpath->cs); + /* Don't cache fake because its value will be changed during comparison.*/ + fake->set_used_tables(RAND_TABLE_BIT); Item_nodeset_func *nodeset; Item *scalar, *comp; if (a->type() == Item::XPATH_NODESET) { nodeset= (Item_nodeset_func*) a; scalar= b; - comp= eq_func(oper, fake, scalar); + comp= eq_func(oper, (Item*)fake, scalar); } else { diff --git a/sql/sp_rcontext.cc b/sql/sp_rcontext.cc index 9b237b3e7cc..be8f705a53e 100644 --- a/sql/sp_rcontext.cc +++ b/sql/sp_rcontext.cc @@ -617,7 +617,7 @@ sp_rcontext::set_case_expr(THD *thd, int case_expr_id, Item **case_expr_item_ptr } m_case_expr_holders[case_expr_id]->store(case_expr_item); - + m_case_expr_holders[case_expr_id]->cache_value(); return FALSE; } -- cgit v1.2.1 From b4a3083dc8b8049e64692ffc8e6e527914fb93da Mon Sep 17 00:00:00 2001 From: Martin Hansson Date: Tue, 10 Nov 2009 13:52:46 +0100 Subject: Backport of Bug#33204 from mysql-pe to mysql-next-mr-bugfixing. Bug no 32858 was fixed in two different ways in what was then called mysql 5.1 and 6.0. The fix in 6.0 was very different since bugfix no 33204 was present. Furthermore, the two fixes were not compatible. Hence in order to backport Bug#33204 to the 5.1-based mysql-next-mr-bugfixing, it was necessary to remove the 5.1 fix of 32858 and apply the 6.0 version of the fix. --- sql/sql_class.cc | 1 - sql/sql_class.h | 34 ------- sql/sql_yacc.yy | 303 ++++++++++++++++++++++++++++++++++++++++--------------- 3 files changed, 224 insertions(+), 114 deletions(-) (limited to 'sql') diff --git a/sql/sql_class.cc b/sql/sql_class.cc index 757f6abb00b..95bdbc5e093 100644 --- a/sql/sql_class.cc +++ b/sql/sql_class.cc @@ -1626,7 +1626,6 @@ void THD::rollback_item_tree_changes() select_result::select_result() { thd=current_thd; - nest_level= -1; } void select_result::send_error(uint errcode,const char *err) diff --git a/sql/sql_class.h b/sql/sql_class.h index aaa959feae9..cefa65925d2 100644 --- a/sql/sql_class.h +++ b/sql/sql_class.h @@ -2381,7 +2381,6 @@ class select_result :public Sql_alloc { protected: THD *thd; SELECT_LEX_UNIT *unit; - uint nest_level; public: select_result(); virtual ~select_result() {}; @@ -2418,12 +2417,6 @@ public: */ virtual void cleanup(); void set_thd(THD *thd_arg) { thd= thd_arg; } - /** - The nest level, if supported. - @return - -1 if nest level is undefined, otherwise a positive integer. - */ - int get_nest_level() { return nest_level; } #ifdef EMBEDDED_LIBRARY virtual void begin_dataset() {} #else @@ -2518,14 +2511,6 @@ class select_export :public select_to_file { CHARSET_INFO *write_cs; // output charset public: select_export(sql_exchange *ex) :select_to_file(ex) {} - /** - Creates a select_export to represent INTO OUTFILE with a - defined level of subquery nesting. - */ - select_export(sql_exchange *ex, uint nest_level_arg) :select_to_file(ex) - { - nest_level= nest_level_arg; - } ~select_export(); int prepare(List &list, SELECT_LEX_UNIT *u); bool send_data(List &items); @@ -2535,15 +2520,6 @@ public: class select_dump :public select_to_file { public: select_dump(sql_exchange *ex) :select_to_file(ex) {} - /** - Creates a select_export to represent INTO DUMPFILE with a - defined level of subquery nesting. - */ - select_dump(sql_exchange *ex, uint nest_level_arg) : - select_to_file(ex) - { - nest_level= nest_level_arg; - } int prepare(List &list, SELECT_LEX_UNIT *u); bool send_data(List &items); }; @@ -3014,16 +2990,6 @@ class select_dumpvar :public select_result_interceptor { public: List var_list; select_dumpvar() { var_list.empty(); row_count= 0;} - /** - Creates a select_dumpvar to represent INTO with a defined - level of subquery nesting. - */ - select_dumpvar(uint nest_level_arg) - { - var_list.empty(); - row_count= 0; - nest_level= nest_level_arg; - } ~select_dumpvar() {} int prepare(List &list, SELECT_LEX_UNIT *u); bool send_data(List &items); diff --git a/sql/sql_yacc.yy b/sql/sql_yacc.yy index 05283718162..43f5c73f5e7 100644 --- a/sql/sql_yacc.yy +++ b/sql/sql_yacc.yy @@ -468,6 +468,90 @@ Item* handle_sql2003_note184_exception(THD *thd, Item* left, bool equal, DBUG_RETURN(result); } +/** + @brief Creates a new SELECT_LEX for a UNION branch. + + Sets up and initializes a SELECT_LEX structure for a query once the parser + discovers a UNION token. The current SELECT_LEX is pushed on the stack and + the new SELECT_LEX becomes the current one. + + @param lex The parser state. + + @param is_union_distinct True if the union preceding the new select statement + uses UNION DISTINCT. + + @param is_top_level This should be @c TRUE if the newly created SELECT_LEX + is a non-nested statement. + + @return false if successful, true if an error was + reported. In the latter case parsing should stop. + */ +bool add_select_to_union_list(LEX *lex, bool is_union_distinct, + bool is_top_level) +{ + /* + Only the last SELECT can have INTO. Since the grammar won't allow INTO in + a nested SELECT, we make this check only when creating a top-level SELECT. + */ + if (is_top_level && lex->result) + { + my_error(ER_WRONG_USAGE, MYF(0), "UNION", "INTO"); + return TRUE; + } + if (lex->current_select->linkage == GLOBAL_OPTIONS_TYPE) + { + my_parse_error(ER(ER_SYNTAX_ERROR)); + return TRUE; + } + /* This counter shouldn't be incremented for UNION parts */ + lex->nest_level--; + if (mysql_new_select(lex, 0)) + return TRUE; + mysql_init_select(lex); + lex->current_select->linkage=UNION_TYPE; + if (is_union_distinct) /* UNION DISTINCT - remember position */ + lex->current_select->master_unit()->union_distinct= + lex->current_select; + return FALSE; +} + +/** + @brief Initializes a SELECT_LEX for a query within parentheses (aka + braces). + + @return false if successful, true if an error was reported. In the latter + case parsing should stop. + */ +bool setup_select_in_parentheses(LEX *lex) +{ + SELECT_LEX * sel= lex->current_select; + if (sel->set_braces(1)) + { + my_parse_error(ER(ER_SYNTAX_ERROR)); + return TRUE; + } + if (sel->linkage == UNION_TYPE && + !sel->master_unit()->first_select()->braces && + sel->master_unit()->first_select()->linkage == + UNION_TYPE) + { + my_parse_error(ER(ER_SYNTAX_ERROR)); + return TRUE; + } + if (sel->linkage == UNION_TYPE && + sel->olap != UNSPECIFIED_OLAP_TYPE && + sel->master_unit()->fake_select_lex) + { + my_error(ER_WRONG_USAGE, MYF(0), "CUBE/ROLLUP", "ORDER BY"); + return TRUE; + } + /* select in braces, can't contain global parameters */ + if (sel->master_unit()->fake_select_lex) + sel->master_unit()->global_parameters= + sel->master_unit()->fake_select_lex; + return FALSE; +} + %} %union { int num; @@ -521,10 +605,10 @@ bool my_yyoverflow(short **a, YYSTYPE **b, ulong *yystacksize); %pure_parser /* We have threads */ /* - Currently there are 168 shift/reduce conflicts. + Currently there are 174 shift/reduce conflicts. We should not introduce new conflicts any more. */ -%expect 168 +%expect 174 /* Comments for TOKENS. @@ -1240,6 +1324,7 @@ bool my_yyoverflow(short **a, YYSTYPE **b, ulong *yystacksize); join_table_list join_table table_factor table_ref esc_table_ref select_derived derived_table_list + select_derived_union %type date_time_type; %type interval @@ -1275,8 +1360,9 @@ bool my_yyoverflow(short **a, YYSTYPE **b, ulong *yystacksize); %type internal_variable_name -%type subselect take_first_select - get_select_lex +%type subselect + get_select_lex query_specification + query_expression_body %type comp_op @@ -6757,37 +6843,22 @@ select_init: select_paren: SELECT_SYM select_part2 { - LEX *lex= Lex; - SELECT_LEX * sel= lex->current_select; - if (sel->set_braces(1)) - { - my_parse_error(ER(ER_SYNTAX_ERROR)); + if (setup_select_in_parentheses(Lex)) MYSQL_YYABORT; - } - if (sel->linkage == UNION_TYPE && - !sel->master_unit()->first_select()->braces && - sel->master_unit()->first_select()->linkage == - UNION_TYPE) - { - my_parse_error(ER(ER_SYNTAX_ERROR)); - MYSQL_YYABORT; - } - if (sel->linkage == UNION_TYPE && - sel->olap != UNSPECIFIED_OLAP_TYPE && - sel->master_unit()->fake_select_lex) - { - my_error(ER_WRONG_USAGE, MYF(0), - "CUBE/ROLLUP", "ORDER BY"); - MYSQL_YYABORT; - } - /* select in braces, can't contain global parameters */ - if (sel->master_unit()->fake_select_lex) - sel->master_unit()->global_parameters= - sel->master_unit()->fake_select_lex; } | '(' select_paren ')' ; +/* The equivalent of select_paren for nested queries. */ +select_paren_derived: + SELECT_SYM select_part2_derived + { + if (setup_select_in_parentheses(Lex)) + MYSQL_YYABORT; + } + | '(' select_paren_derived ')' + ; + select_init2: select_part2 { @@ -8626,6 +8697,7 @@ when_list: } ; +/* Equivalent to in the SQL:2003 standard. */ /* Warning - may return NULL in case of incomplete SELECT */ table_ref: table_factor { $$=$1; } @@ -8653,6 +8725,7 @@ esc_table_ref: | '{' ident table_ref '}' { $$=$3; } ; +/* Equivalent to
in the SQL:2003 standard. */ /* Warning - may return NULL in case of incomplete SELECT */ derived_table_list: esc_table_ref { $$=$1; } @@ -8806,6 +8879,13 @@ normal_join: | CROSS JOIN_SYM {} ; +/* + This is a flattening of the rules
and
+ in the SQL:2003 standard, since we don't have + + I.e. +
::=
[ ] +*/ /* Warning - may return NULL in case of incomplete SELECT */ table_factor: { @@ -8843,12 +8923,29 @@ table_factor: /* incomplete derived tables return NULL, we must be nested in select_derived rule to be here. */ } - | '(' get_select_lex select_derived union_opt ')' opt_table_alias + /* + Represents a flattening of the following rules from the SQL:2003 + standard. This sub-rule corresponds to the sub-rule +
::= ... | [ AS ] + + The following rules have been flattened into query_expression_body + (since we have no ). + + ::=
+
::= + ::= + ::= [ ] + + For the time being we use the non-standard rule + select_derived_union which is a compromise between the standard + and our parser. Possibly this rule could be replaced by our + query_expression_body. + */ + | '(' get_select_lex select_derived_union ')' opt_table_alias { /* Use $2 instead of Lex->current_select as derived table will alter value of Lex->current_select. */ - - if (!($3 || $6) && $2->embedding && + if (!($3 || $5) && $2->embedding && !$2->embedding->nested_join->join_list.elements) { /* we have a derived table ($3 == NULL) but no alias, @@ -8870,7 +8967,7 @@ table_factor: if (ti == NULL) MYSQL_YYABORT; if (!($$= sel->add_table_to_list(lex->thd, - ti, $6, 0, + new Table_ident(unit), $5, 0, TL_READ))) MYSQL_YYABORT; @@ -8878,7 +8975,8 @@ table_factor: lex->pop_context(); lex->nest_level--; } - else if ($4 || $6) + else if ($3->select_lex && + $3->select_lex->master_unit()->is_union() || $5) { /* simple nested joins cannot have aliases or unions */ my_parse_error(ER(ER_SYNTAX_ERROR)); @@ -8893,6 +8991,62 @@ table_factor: } ; +select_derived_union: + select_derived opt_order_clause opt_limit_clause + | select_derived_union + UNION_SYM + union_option + { + if (add_select_to_union_list(Lex, (bool)$3, FALSE)) + MYSQL_YYABORT; + } + query_specification + { + /* + Remove from the name resolution context stack the context of the + last select in the union. + */ + Lex->pop_context(); + } + opt_order_clause opt_limit_clause + ; + +/* The equivalent of select_init2 for nested queries. */ +select_init2_derived: + select_part2_derived + { + LEX *lex= Lex; + SELECT_LEX * sel= lex->current_select; + if (lex->current_select->set_braces(0)) + { + my_parse_error(ER(ER_SYNTAX_ERROR)); + MYSQL_YYABORT; + } + if (sel->linkage == UNION_TYPE && + sel->master_unit()->first_select()->braces) + { + my_parse_error(ER(ER_SYNTAX_ERROR)); + MYSQL_YYABORT; + } + } + ; + +/* The equivalent of select_part2 for nested queries. */ +select_part2_derived: + { + LEX *lex= Lex; + SELECT_LEX *sel= lex->current_select; + if (sel->linkage != UNION_TYPE) + mysql_init_select(lex); + lex->current_select->parsing_place= SELECT_LIST; + } + select_options select_item_list + { + Select->parsing_place= NO_MATTER; + } + opt_select_from select_lock_type + ; + /* handle contents of parentheses in join expression */ select_derived: get_select_lex @@ -9508,8 +9662,7 @@ procedure_item: select_var_list_init: { LEX *lex=Lex; - if (!lex->describe && - (!(lex->result= new select_dumpvar(lex->nest_level)))) + if (!lex->describe && (!(lex->result= new select_dumpvar()))) MYSQL_YYABORT; } select_var_list @@ -9590,7 +9743,7 @@ into_destination: LEX *lex= Lex; lex->uncacheable(UNCACHEABLE_SIDEEFFECT); if (!(lex->exchange= new sql_exchange($2.str, 0)) || - !(lex->result= new select_export(lex->exchange, lex->nest_level))) + !(lex->result= new select_export(lex->exchange))) MYSQL_YYABORT; } opt_load_data_charset @@ -9604,7 +9757,7 @@ into_destination: lex->uncacheable(UNCACHEABLE_SIDEEFFECT); if (!(lex->exchange= new sql_exchange($2.str,1))) MYSQL_YYABORT; - if (!(lex->result= new select_dump(lex->exchange, lex->nest_level))) + if (!(lex->result= new select_dump(lex->exchange))) MYSQL_YYABORT; } } @@ -13223,33 +13376,8 @@ union_clause: union_list: UNION_SYM union_option { - LEX *lex=Lex; - if (lex->result && - (lex->result->get_nest_level() == -1 || - lex->result->get_nest_level() == lex->nest_level)) - { - /* - Only the last SELECT can have INTO unless the INTO and UNION - are at different nest levels. In version 5.1 and above, INTO - will onle be allowed at top level. - */ - my_error(ER_WRONG_USAGE, MYF(0), "UNION", "INTO"); - MYSQL_YYABORT; - } - if (lex->current_select->linkage == GLOBAL_OPTIONS_TYPE) - { - my_parse_error(ER(ER_SYNTAX_ERROR)); - MYSQL_YYABORT; - } - /* This counter shouldn't be incremented for UNION parts */ - Lex->nest_level--; - if (mysql_new_select(lex, 0)) + if (add_select_to_union_list(Lex, (bool)$2, TRUE)) MYSQL_YYABORT; - mysql_init_select(lex); - lex->current_select->linkage=UNION_TYPE; - if ($2) /* UNION DISTINCT - remember position */ - lex->current_select->master_unit()->union_distinct= - lex->current_select; } select_init { @@ -13302,22 +13430,39 @@ union_option: | ALL { $$=0; } ; -take_first_select: /* empty */ - { - $$= Lex->current_select->master_unit()->first_select(); - }; +query_specification: + SELECT_SYM select_init2_derived + { + $$= Lex->current_select->master_unit()->first_select(); + } + | '(' select_paren_derived ')' + { + $$= Lex->current_select->master_unit()->first_select(); + } + ; +query_expression_body: + query_specification + | query_expression_body + UNION_SYM union_option + { + if (add_select_to_union_list(Lex, (bool)$3, FALSE)) + MYSQL_YYABORT; + } + query_specification + { + Lex->pop_context(); + $$= $1; + } + ; + +/* Corresponds to in the SQL:2003 standard. */ subselect: - SELECT_SYM subselect_start select_init2 take_first_select - subselect_end - { - $$= $4; - } - | '(' subselect_start select_paren take_first_select - subselect_end ')' - { - $$= $4; - }; + subselect_start query_expression_body subselect_end + { + $$= $2; + } + ; subselect_start: { -- cgit v1.2.1 From 92bfebb66eaddacd5d2dfa6cf749d721a921ff4c Mon Sep 17 00:00:00 2001 From: Mattias Jonsson Date: Fri, 20 Nov 2009 00:13:54 +0100 Subject: Bug#32115: Bad use of Name_resolution_context from current LEX in partitioning port from mysql-next (5.4) to mysql-next-mr-bugfixing (5.5/5.6?) 2755 Konstantin Osipov 2008-11-27 Bug#32115 will remove the pre-requisite to initialize LEX to open tables. This dependency was added in 5.1 and was supposed to be removed in 6.0. Remove asserts and initialization of LEX in places where we don't deal with partitioned tables. --- sql/event_scheduler.cc | 1 - sql/events.cc | 1 - sql/sp_head.cc | 2 +- sql/sql_acl.cc | 2 -- sql/sql_base.cc | 3 --- sql/sql_parse.cc | 1 - sql/sql_plugin.cc | 1 - sql/sql_servers.cc | 1 - sql/sql_udf.cc | 1 - sql/table.cc | 5 ++--- sql/tztime.cc | 1 - 11 files changed, 3 insertions(+), 16 deletions(-) (limited to 'sql') diff --git a/sql/event_scheduler.cc b/sql/event_scheduler.cc index ea20270b457..d9ca161f260 100644 --- a/sql/event_scheduler.cc +++ b/sql/event_scheduler.cc @@ -128,7 +128,6 @@ post_init_event_thread(THD *thd) thd->cleanup(); return TRUE; } - lex_start(thd); pthread_mutex_lock(&LOCK_thread_count); threads.append(thd); diff --git a/sql/events.cc b/sql/events.cc index f7ff2b0ccf1..ac37a2bd595 100644 --- a/sql/events.cc +++ b/sql/events.cc @@ -925,7 +925,6 @@ Events::init(my_bool opt_noacl_or_bootstrap) */ thd->thread_stack= (char*) &thd; thd->store_globals(); - lex_start(thd); /* We will need Event_db_repository anyway, even if the scheduler is diff --git a/sql/sp_head.cc b/sql/sp_head.cc index 7cabae255cc..3d2486cd98f 100644 --- a/sql/sp_head.cc +++ b/sql/sp_head.cc @@ -4005,7 +4005,7 @@ sp_head::add_used_tables_to_table_list(THD *thd, /** - Simple function for adding an explicetly named (systems) table to + Simple function for adding an explicitly named (systems) table to the global table list, e.g. "mysql", "proc". */ diff --git a/sql/sql_acl.cc b/sql/sql_acl.cc index 46d7f3ce89d..1e5eeec7dbc 100644 --- a/sql/sql_acl.cc +++ b/sql/sql_acl.cc @@ -276,7 +276,6 @@ my_bool acl_init(bool dont_read_acl_tables) DBUG_RETURN(1); /* purecov: inspected */ thd->thread_stack= (char*) &thd; thd->store_globals(); - lex_start(thd); /* It is safe to call acl_reload() since acl_* arrays and hashes which will be freed there are global static objects and thus are initialized @@ -3524,7 +3523,6 @@ my_bool grant_init() DBUG_RETURN(1); /* purecov: deadcode */ thd->thread_stack= (char*) &thd; thd->store_globals(); - lex_start(thd); return_val= grant_reload(thd); delete thd; /* Remember that we don't have a THD */ diff --git a/sql/sql_base.cc b/sql/sql_base.cc index 2c0ba87262a..787ea3cdc92 100644 --- a/sql/sql_base.cc +++ b/sql/sql_base.cc @@ -2532,9 +2532,6 @@ TABLE *open_table(THD *thd, TABLE_LIST *table_list, MEM_ROOT *mem_root, HASH_SEARCH_STATE state; DBUG_ENTER("open_table"); - /* Parsing of partitioning information from .frm needs thd->lex set up. */ - DBUG_ASSERT(thd->lex->is_lex_started); - /* find a unused table in the open table cache */ if (refresh) *refresh=0; diff --git a/sql/sql_parse.cc b/sql/sql_parse.cc index 1edb37026fb..094a715902f 100644 --- a/sql/sql_parse.cc +++ b/sql/sql_parse.cc @@ -7050,7 +7050,6 @@ bool reload_acl_and_cache(THD *thd, ulong options, TABLE_LIST *tables, { thd->thread_stack= (char*) &tmp_thd; thd->store_globals(); - lex_start(thd); } if (thd) diff --git a/sql/sql_plugin.cc b/sql/sql_plugin.cc index 936c9ae8866..d15c97de912 100644 --- a/sql/sql_plugin.cc +++ b/sql/sql_plugin.cc @@ -1359,7 +1359,6 @@ static void plugin_load(MEM_ROOT *tmp_root, int *argc, char **argv) } new_thd->thread_stack= (char*) &tables; new_thd->store_globals(); - lex_start(new_thd); new_thd->db= my_strdup("mysql", MYF(0)); new_thd->db_length= 5; bzero((uchar*)&tables, sizeof(tables)); diff --git a/sql/sql_servers.cc b/sql/sql_servers.cc index e8fa3d984a7..8432b68a1d1 100644 --- a/sql/sql_servers.cc +++ b/sql/sql_servers.cc @@ -140,7 +140,6 @@ bool servers_init(bool dont_read_servers_table) DBUG_RETURN(TRUE); thd->thread_stack= (char*) &thd; thd->store_globals(); - lex_start(thd); /* It is safe to call servers_reload() since servers_* arrays and hashes which will be freed there are global static objects and thus are initialized diff --git a/sql/sql_udf.cc b/sql/sql_udf.cc index a1a0d9633b7..f38d8921181 100644 --- a/sql/sql_udf.cc +++ b/sql/sql_udf.cc @@ -135,7 +135,6 @@ void udf_init() initialized = 1; new_thd->thread_stack= (char*) &new_thd; new_thd->store_globals(); - lex_start(new_thd); new_thd->set_db(db, sizeof(db)-1); bzero((uchar*) &tables,sizeof(tables)); diff --git a/sql/table.cc b/sql/table.cc index 22b4b2f9b5e..600bd8bb33d 100644 --- a/sql/table.cc +++ b/sql/table.cc @@ -1649,9 +1649,6 @@ int open_table_from_share(THD *thd, TABLE_SHARE *share, const char *alias, DBUG_PRINT("enter",("name: '%s.%s' form: 0x%lx", share->db.str, share->table_name.str, (long) outparam)); - /* Parsing of partitioning information from .frm needs thd->lex set up. */ - DBUG_ASSERT(thd->lex->is_lex_started); - error= 1; bzero((char*) outparam, sizeof(*outparam)); outparam->in_use= thd; @@ -1833,6 +1830,8 @@ int open_table_from_share(THD *thd, TABLE_SHARE *share, const char *alias, thd->restore_active_arena(&part_func_arena, &backup_arena); goto partititon_err; } + /* fix_partition_func needs thd->lex set up. TODO: fix! */ + DBUG_ASSERT(thd->lex->is_lex_started); outparam->part_info->is_auto_partitioned= share->auto_partitioned; DBUG_PRINT("info", ("autopartitioned: %u", share->auto_partitioned)); /* we should perform the fix_partition_func in either local or diff --git a/sql/tztime.cc b/sql/tztime.cc index 650678c721b..d4b7ae4b73a 100644 --- a/sql/tztime.cc +++ b/sql/tztime.cc @@ -1578,7 +1578,6 @@ my_tz_init(THD *org_thd, const char *default_tzname, my_bool bootstrap) DBUG_RETURN(1); thd->thread_stack= (char*) &thd; thd->store_globals(); - lex_start(thd); /* Init all memory structures that require explicit destruction */ if (my_hash_init(&tz_names, &my_charset_latin1, 20, -- cgit v1.2.1 From 4205c622e1a741bae1b2d8d213342f8e0bdd936a Mon Sep 17 00:00:00 2001 From: Mattias Jonsson Date: Fri, 20 Nov 2009 01:20:08 +0100 Subject: Bug#32115: Bad use of Name_resolution_context from current LEX in partitioning port from mysql-next (5.4?) to mysql-next-mr-bugfixes (5.5/5.6?) 3477 Mikael Ronstrom 2009-07-29 Bug#32115, made use of local lex object to avoid side effects of opening partitioned tables 3478 Mikael Ronstrom 2009-07-29 Bug#32115, added an extra test in debug builds to ensure no dangling pointers to the old lex object is still around 3479 Mikael Ronstrom 2009-07-29 Bug#32115, Removed an assert that was no longer needed 3480 Mikael Ronstrom 2009-08-05 Bug#32115, fixed review comments 3481 Mikael Ronstrom 2009-08-07 Bug#32115, remove now obsolete lex_start calls --- sql/ha_ndbcluster.cc | 2 - sql/ha_ndbcluster_binlog.cc | 1 - sql/slave.cc | 1 - sql/sql_insert.cc | 6 -- sql/sql_partition.cc | 170 +++++++++++++++++++++++++------------------- sql/table.cc | 2 - 6 files changed, 95 insertions(+), 87 deletions(-) (limited to 'sql') diff --git a/sql/ha_ndbcluster.cc b/sql/ha_ndbcluster.cc index 83cceb0da76..f83e40ec402 100644 --- a/sql/ha_ndbcluster.cc +++ b/sql/ha_ndbcluster.cc @@ -6952,7 +6952,6 @@ int ndb_create_table_from_engine(THD *thd, const char *db, LEX *old_lex= thd->lex, newlex; thd->lex= &newlex; newlex.current_select= NULL; - lex_start(thd); int res= ha_create_table_from_engine(thd, db, table_name); thd->lex= old_lex; return res; @@ -9272,7 +9271,6 @@ pthread_handler_t ndb_util_thread_func(void *arg __attribute__((unused))) thd->thread_stack= (char*)&thd; /* remember where our stack is */ if (thd->store_globals()) goto ndb_util_thread_fail; - lex_start(thd); thd->init_for_queries(); thd->version=refresh_version; thd->main_security_ctx.host_or_ip= ""; diff --git a/sql/ha_ndbcluster_binlog.cc b/sql/ha_ndbcluster_binlog.cc index e34a22cf9f4..a22ce976351 100644 --- a/sql/ha_ndbcluster_binlog.cc +++ b/sql/ha_ndbcluster_binlog.cc @@ -3669,7 +3669,6 @@ pthread_handler_t ndb_binlog_thread_func(void *arg) pthread_exit(0); return NULL; // Avoid compiler warnings } - lex_start(thd); thd->init_for_queries(); thd->command= COM_DAEMON; diff --git a/sql/slave.cc b/sql/slave.cc index 8be17860c61..d49031ef066 100644 --- a/sql/slave.cc +++ b/sql/slave.cc @@ -2121,7 +2121,6 @@ static int init_slave_thread(THD* thd, SLAVE_THD_TYPE thd_type) thd->cleanup(); DBUG_RETURN(-1); } - lex_start(thd); if (thd_type == SLAVE_THD_SQL) thd_proc_info(thd, "Waiting for the next event in relay log"); diff --git a/sql/sql_insert.cc b/sql/sql_insert.cc index 173e7f1ca2f..7edd3b496da 100644 --- a/sql/sql_insert.cc +++ b/sql/sql_insert.cc @@ -2308,12 +2308,6 @@ static void handle_delayed_insert_impl(THD *thd, Delayed_insert *di) goto err; } - /* - Open table requires an initialized lex in case the table is - partitioned. The .frm file contains a partial SQL string which is - parsed using a lex, that depends on initialized thd->lex. - */ - lex_start(thd); thd->lex->sql_command= SQLCOM_INSERT; // For innodb::store_lock() /* Statement-based replication of INSERT DELAYED has problems with RAND() diff --git a/sql/sql_partition.cc b/sql/sql_partition.cc index 417b23c076e..a05e8fbf3ab 100644 --- a/sql/sql_partition.cc +++ b/sql/sql_partition.cc @@ -932,6 +932,85 @@ int check_signed_flag(partition_info *part_info) return error; } +/** + Initialize lex object for use in fix_fields and parsing. + + SYNOPSIS + init_lex_with_single_table() + @param thd The thread object + @param table The table object + @return Operation status + @retval TRUE An error occurred, memory allocation error + @retval FALSE Ok + + DESCRIPTION + This function is used to initialize a lex object on the + stack for use by fix_fields and for parsing. In order to + work properly it also needs to initialize the + Name_resolution_context object of the lexer. + Finally it needs to set a couple of variables to ensure + proper functioning of fix_fields. +*/ + +static int +init_lex_with_single_table(THD *thd, TABLE *table, LEX *lex) +{ + TABLE_LIST *table_list; + Table_ident *table_ident; + SELECT_LEX *select_lex= &lex->select_lex; + Name_resolution_context *context= &select_lex->context; + /* + We will call the parser to create a part_info struct based on the + partition string stored in the frm file. + We will use a local lex object for this purpose. However we also + need to set the Name_resolution_object for this lex object. We + do this by using add_table_to_list where we add the table that + we're working with to the Name_resolution_context. + */ + thd->lex= lex; + lex_start(thd); + context->init(); + if ((!(table_ident= new Table_ident(thd, + table->s->table_name, + table->s->db, TRUE))) || + (!(table_list= select_lex->add_table_to_list(thd, + table_ident, + NULL, + 0)))) + return TRUE; + context->resolve_in_table_list_only(table_list); + lex->use_only_table_context= TRUE; + select_lex->cur_pos_in_select_list= UNDEF_POS; + table->map= 1; //To ensure correct calculation of const item + table->get_fields_in_item_tree= TRUE; + table_list->table= table; + return FALSE; +} + +/** + End use of local lex with single table + + SYNOPSIS + end_lex_with_single_table() + @param thd The thread object + @param table The table object + @param old_lex The real lex object connected to THD + + DESCRIPTION + This function restores the real lex object after calling + init_lex_with_single_table and also restores some table + variables temporarily set. +*/ + +static void +end_lex_with_single_table(THD *thd, TABLE *table, LEX *old_lex) +{ + LEX *lex= thd->lex; + table->map= 0; + table->get_fields_in_item_tree= FALSE; + lex_end(lex); + thd->lex= old_lex; +} /* The function uses a new feature in fix_fields where the flag @@ -972,55 +1051,18 @@ static bool fix_fields_part_func(THD *thd, Item* func_expr, TABLE *table, bool is_sub_part) { partition_info *part_info= table->part_info; - uint dir_length, home_dir_length; bool result= TRUE; - TABLE_LIST tables; - TABLE_LIST *save_table_list, *save_first_table, *save_last_table; int error; - Name_resolution_context *context; const char *save_where; - char* db_name; - char db_name_string[FN_REFLEN]; - bool save_use_only_table_context; + LEX *old_lex= thd->lex; + LEX lex; DBUG_ENTER("fix_fields_part_func"); - /* - Set-up the TABLE_LIST object to be a list with a single table - Set the object to zero to create NULL pointers and set alias - and real name to table name and get database name from file name. - TODO: Consider generalizing or refactoring Lex::add_table_to_list() so - it can be used in all places where we create TABLE_LIST objects. - Also consider creating appropriate constructors for TABLE_LIST. - */ - - bzero((void*)&tables, sizeof(TABLE_LIST)); - tables.alias= tables.table_name= (char*) table->s->table_name.str; - tables.table= table; - tables.next_local= 0; - tables.next_name_resolution_table= 0; - /* - Cache the table in Item_fields. All the tables can be cached except - the trigger pseudo table. - */ - tables.cacheable_table= TRUE; - context= thd->lex->current_context(); - tables.select_lex= context->select_lex; - strmov(db_name_string, table->s->normalized_path.str); - dir_length= dirname_length(db_name_string); - db_name_string[dir_length - 1]= 0; - home_dir_length= dirname_length(db_name_string); - db_name= &db_name_string[home_dir_length]; - tables.db= db_name; + if (init_lex_with_single_table(thd, table, &lex)) + goto end; - table->map= 1; //To ensure correct calculation of const item - table->get_fields_in_item_tree= TRUE; - save_table_list= context->table_list; - save_first_table= context->first_name_resolution_table; - save_last_table= context->last_name_resolution_table; - context->table_list= &tables; - context->first_name_resolution_table= &tables; - context->last_name_resolution_table= NULL; - func_expr->walk(&Item::change_context_processor, 0, (uchar*) context); + func_expr->walk(&Item::change_context_processor, 0, + (uchar*) &lex.select_lex.context); save_where= thd->where; thd->where= "partition function"; /* @@ -1035,30 +1077,18 @@ static bool fix_fields_part_func(THD *thd, Item* func_expr, TABLE *table, that does this during val_int must be disallowed as partition function. SEE Bug #21658 - */ - /* + This is a tricky call to prepare for since it can have a large number of interesting side effects, both desirable and undesirable. */ - - save_use_only_table_context= thd->lex->use_only_table_context; - thd->lex->use_only_table_context= TRUE; - thd->lex->current_select->cur_pos_in_select_list= UNDEF_POS; - error= func_expr->fix_fields(thd, (Item**)&func_expr); - thd->lex->use_only_table_context= save_use_only_table_context; - - context->table_list= save_table_list; - context->first_name_resolution_table= save_first_table; - context->last_name_resolution_table= save_last_table; if (unlikely(error)) { DBUG_PRINT("info", ("Field in partition function not part of table")); clear_field_flag(table); goto end; } - thd->where= save_where; if (unlikely(func_expr->const_item())) { my_error(ER_CONST_EXPR_IN_PARTITION_FUNC_ERROR, MYF(0)); @@ -1069,8 +1099,11 @@ static bool fix_fields_part_func(THD *thd, Item* func_expr, TABLE *table, goto end; result= set_up_field_array(table, is_sub_part); end: - table->get_fields_in_item_tree= FALSE; - table->map= 0; //Restore old value + end_lex_with_single_table(thd, table, old_lex); +#if !defined(DBUG_OFF) + func_expr->walk(&Item::change_context_processor, 0, + (uchar*) 0); +#endif DBUG_RETURN(result); } @@ -4109,26 +4142,13 @@ bool mysql_unpack_partition(THD *thd, LEX lex; DBUG_ENTER("mysql_unpack_partition"); - thd->lex= &lex; thd->variables.character_set_client= system_charset_info; Parser_state parser_state(thd, part_buf, part_info_len); - lex_start(thd); - *work_part_info_used= false; - /* - We need to use the current SELECT_LEX since I need to keep the - Name_resolution_context object which is referenced from the - Item_field objects. - This is not a nice solution since if the parser uses current_select - for anything else it will corrupt the current LEX object. - Also, we need to make sure there even is a select -- if the statement - was a "USE ...", current_select will be NULL, but we may still end up - here if we try to log to a partitioned table. This is currently - unsupported, but should still fail rather than crash! - */ - if (!(thd->lex->current_select= old_lex->current_select)) + if (init_lex_with_single_table(thd, table, &lex)) goto end; + /* All Items created is put into a free list on the THD object. This list is used to free all Item objects after completing a query. We don't @@ -4138,6 +4158,7 @@ bool mysql_unpack_partition(THD *thd, Thus we move away the current list temporarily and start a new list that we then save in the partition info structure. */ + *work_part_info_used= FALSE; lex.part_info= new partition_info();/* Indicates MYSQLparse from this place */ if (!lex.part_info) { @@ -4251,8 +4272,7 @@ bool mysql_unpack_partition(THD *thd, result= FALSE; end: - lex_end(thd->lex); - thd->lex= old_lex; + end_lex_with_single_table(thd, table, old_lex); thd->variables.character_set_client= old_character_set_client; DBUG_RETURN(result); } diff --git a/sql/table.cc b/sql/table.cc index 600bd8bb33d..39a7e163c37 100644 --- a/sql/table.cc +++ b/sql/table.cc @@ -1830,8 +1830,6 @@ int open_table_from_share(THD *thd, TABLE_SHARE *share, const char *alias, thd->restore_active_arena(&part_func_arena, &backup_arena); goto partititon_err; } - /* fix_partition_func needs thd->lex set up. TODO: fix! */ - DBUG_ASSERT(thd->lex->is_lex_started); outparam->part_info->is_auto_partitioned= share->auto_partitioned; DBUG_PRINT("info", ("autopartitioned: %u", share->auto_partitioned)); /* we should perform the fix_partition_func in either local or -- cgit v1.2.1 From 177ed0667e128fc2533fae1f4fd520945f40c0a8 Mon Sep 17 00:00:00 2001 From: He Zhenxing Date: Sat, 21 Nov 2009 19:50:15 +0800 Subject: Post fix for previous patch of bug#37148 --- sql/sql_table.cc | 1 + 1 file changed, 1 insertion(+) (limited to 'sql') diff --git a/sql/sql_table.cc b/sql/sql_table.cc index 2bfcabb957e..691b248fb2e 100644 --- a/sql/sql_table.cc +++ b/sql/sql_table.cc @@ -3599,6 +3599,7 @@ static inline int write_create_table_bin_log(THD *thd, (thd->current_stmt_binlog_row_based && !(create_info->options & HA_LEX_CREATE_TMP_TABLE)))) return write_bin_log(thd, TRUE, thd->query(), thd->query_length()); + return 0; } -- cgit v1.2.1 From a25a3fb13437eeb6b1a6501da58c44bc41931129 Mon Sep 17 00:00:00 2001 From: Alexey Kopytov Date: Sat, 21 Nov 2009 20:31:00 +0300 Subject: Backport of the fix for bug #33969: Updating a text field via a left join When creating a temporary TEXT/BLOB field from an Item in Item::make_string_field(), the field's type was unconditionally set to the one corresponding to the maximum length (i.e. LONGTEXT/ LONGBLOB). This resulted in problems when exactly the same TEXT/BLOB is type required in cases like CREATE ... SELECT or creating internal temporary tables for joins. Fixed by calling a different constructor for Field_blob so that an appropriate type is used depending on the Item's max_length value. --- sql/item.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'sql') diff --git a/sql/item.cc b/sql/item.cc index b59a82f8045..f8fd82fda67 100644 --- a/sql/item.cc +++ b/sql/item.cc @@ -5025,7 +5025,7 @@ Field *Item::make_string_field(TABLE *table) DBUG_ASSERT(collation.collation); if (max_length/collation.collation->mbmaxlen > CONVERT_IF_BIGGER_TO_BLOB) field= new Field_blob(max_length, maybe_null, name, - collation.collation); + collation.collation, TRUE); /* Item_type_holder holds the exact type, do not change it */ else if (max_length > 0 && (type() != Item::TYPE_HOLDER || field_type() != MYSQL_TYPE_STRING)) -- cgit v1.2.1 From f52280cc427eb6f8e6744fe7f89e5d23ec4e914b Mon Sep 17 00:00:00 2001 From: Mattias Jonsson Date: Tue, 24 Nov 2009 12:08:04 +0100 Subject: merge of bug#35765 into mysql-next-mr-bugfixing --- sql/protocol.cc | 4 ++-- sql/sql_yacc.yy | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) (limited to 'sql') diff --git a/sql/protocol.cc b/sql/protocol.cc index 5990f0f001a..ca6aa3a6052 100644 --- a/sql/protocol.cc +++ b/sql/protocol.cc @@ -411,8 +411,8 @@ bool net_send_error_packet(THD *thd, uint sql_errno, const char *err, thd->variables.character_set_results, err, strlen(err), system_charset_info, &error); - length= (uint) (strmake((char*) pos, (char*)converted_err, MYSQL_ERRMSG_SIZE) - - (char*) buff); + length= (uint) (strmake((char*) pos, (char*)converted_err, + MYSQL_ERRMSG_SIZE - 1) - (char*) buff); err= (char*) buff; DBUG_RETURN(net_write_command(net,(uchar) 255, (uchar*) "", 0, (uchar*) err, length)); diff --git a/sql/sql_yacc.yy b/sql/sql_yacc.yy index 6ff70bf8a8a..0003f343d72 100644 --- a/sql/sql_yacc.yy +++ b/sql/sql_yacc.yy @@ -4820,7 +4820,8 @@ create_table_option: ENGINE_SYM opt_equal storage_engines { Lex->create_info.db_type= $3; - Lex->create_info.used_fields|= HA_CREATE_USED_ENGINE; + if ($3) + Lex->create_info.used_fields|= HA_CREATE_USED_ENGINE; } | TYPE_SYM opt_equal storage_engines { -- cgit v1.2.1 From 76223362bded967c4b93e11115952151e9fcc2a7 Mon Sep 17 00:00:00 2001 From: Tor Didriksen Date: Mon, 30 Nov 2009 09:54:46 +0100 Subject: backport of: ------------------------------------------------------------ revno: 2617.81.1 revision-id: tor.didriksen@sun.com-20090924061133-qo02zotz3yypmfpk parent: davi.arnaut@sun.com-20090923203724-tvz7x8dauzp686v7 committer: Tor Didriksen branch nick: 6.0-codebase-bf-opt timestamp: Thu 2009-09-24 08:11:33 +0200 message: Bug#47511 Segmentation fault during cleanup in sql_union (events_bugs.test) --- sql/sql_lex.h | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) (limited to 'sql') diff --git a/sql/sql_lex.h b/sql/sql_lex.h index 9daf28653b1..bd5a5ce6c3d 100644 --- a/sql/sql_lex.h +++ b/sql/sql_lex.h @@ -403,6 +403,8 @@ public: struct LEX; class st_select_lex; class st_select_lex_unit; + + class st_select_lex_node { protected: st_select_lex_node *next, **prev, /* neighbor list */ @@ -440,8 +442,17 @@ public: { return (void*) alloc_root(mem_root, (uint) size); } static void operator delete(void *ptr,size_t size) { TRASH(ptr, size); } static void operator delete(void *ptr, MEM_ROOT *mem_root) {} - st_select_lex_node(): linkage(UNSPECIFIED_TYPE) {} + + // Ensures that at least all members used during cleanup() are initialized. + st_select_lex_node() + : next(NULL), prev(NULL), + master(NULL), slave(NULL), + link_next(NULL), link_prev(NULL), + linkage(UNSPECIFIED_TYPE) + { + } virtual ~st_select_lex_node() {} + inline st_select_lex_node* get_master() { return master; } virtual void init_query(); virtual void init_select(); @@ -487,6 +498,8 @@ class select_result; class JOIN; class select_union; class Procedure; + + class st_select_lex_unit: public st_select_lex_node { protected: TABLE_LIST result_table_list; @@ -498,6 +511,14 @@ protected: bool saved_error; public: + // Ensures that at least all members used during cleanup() are initialized. + st_select_lex_unit() + : union_result(NULL), table(NULL), result(NULL), + cleaned(false), + fake_select_lex(NULL) + { + } + bool prepared, // prepare phase already performed for UNION (unit) optimized, // optimize phase already performed for UNION (unit) executed, // already executed -- cgit v1.2.1 From 488269e2a4e2f6c2339dbf5b032c9156461f573b Mon Sep 17 00:00:00 2001 From: Tor Didriksen Date: Fri, 27 Nov 2009 15:23:20 +0100 Subject: Bug#49165 Remove unused/confusing code/comments in setup_conds() --- sql/sql_base.cc | 6 ------ sql/sql_lex.cc | 1 - sql/sql_lex.h | 5 ----- 3 files changed, 12 deletions(-) (limited to 'sql') diff --git a/sql/sql_base.cc b/sql/sql_base.cc index 5033224e6ed..b1a4cc1dfb9 100644 --- a/sql/sql_base.cc +++ b/sql/sql_base.cc @@ -8008,7 +8008,6 @@ int setup_conds(THD *thd, TABLE_LIST *tables, TABLE_LIST *leaves, COND **conds) { SELECT_LEX *select_lex= thd->lex->current_select; - Query_arena *arena= thd->stmt_arena, backup; TABLE_LIST *table= NULL; // For HP compilers /* it_is_update set to TRUE when tables of primary SELECT_LEX (SELECT_LEX @@ -8024,10 +8023,6 @@ int setup_conds(THD *thd, TABLE_LIST *tables, TABLE_LIST *leaves, select_lex->is_item_list_lookup= 0; DBUG_ENTER("setup_conds"); - if (select_lex->conds_processed_with_permanent_arena || - arena->is_conventional()) - arena= 0; // For easier test - thd->mark_used_columns= MARK_COLUMNS_READ; DBUG_PRINT("info", ("thd->mark_used_columns: %d", thd->mark_used_columns)); select_lex->cond_count= 0; @@ -8096,7 +8091,6 @@ int setup_conds(THD *thd, TABLE_LIST *tables, TABLE_LIST *leaves, We do this ON -> WHERE transformation only once per PS/SP statement. */ select_lex->where= *conds; - select_lex->conds_processed_with_permanent_arena= 1; } thd->lex->current_select->is_item_list_lookup= save_is_item_list_lookup; DBUG_RETURN(test(thd->is_error())); diff --git a/sql/sql_lex.cc b/sql/sql_lex.cc index c443f092d9b..64eb1d2b1a7 100644 --- a/sql/sql_lex.cc +++ b/sql/sql_lex.cc @@ -1672,7 +1672,6 @@ void st_select_lex::init_query() parent_lex->push_context(&context); cond_count= between_count= with_wild= 0; max_equal_elems= 0; - conds_processed_with_permanent_arena= 0; ref_pointer_array= 0; select_n_where_fields= 0; select_n_having_items= 0; diff --git a/sql/sql_lex.h b/sql/sql_lex.h index bd5a5ce6c3d..aa4a4717318 100644 --- a/sql/sql_lex.h +++ b/sql/sql_lex.h @@ -659,11 +659,6 @@ public: uint select_n_where_fields; enum_parsing_place parsing_place; /* where we are parsing expression */ bool with_sum_func; /* sum function indicator */ - /* - PS or SP cond natural joins was alredy processed with permanent - arena and all additional items which we need alredy stored in it - */ - bool conds_processed_with_permanent_arena; ulong table_join_options; uint in_sum_expr; -- cgit v1.2.1 From 9f7d24586b641fde19848dd77776d0fc7b0001f2 Mon Sep 17 00:00:00 2001 From: Evgeny Potemkin Date: Wed, 2 Dec 2009 00:25:51 +0300 Subject: Bug#33546: Slowdown on re-evaluation of constant expressions. Constant expressions in WHERE/HAVING/ON clauses aren't cached and evaluated for each row. This causes slowdown of query execution especially if constant UDF/SP function are used. Now WHERE/HAVING/ON expressions are analyzed in the top-bottom direction with help of the compile function. When analyzer meets a constant item it sets a flag for the tree transformer to cache the item and doesn't allow tree walker to go deeper. Thus, the topmost item of a constant expression if cached. This is done after all other optimizations were applied to WHERE/HAVING/ON expressions A helper function called cache_const_exprs is added to the JOIN class. It calls compile method with caching analyzer and transformer on WHERE, HAVING, ON expressions if they're present. The cache_const_expr_analyzer and cache_const_expr_transformer functions are added to the Item class. The first one check if the item can be cached and the second caches it if so. A new Item_cache_datetime class is derived from the Item_cache class. It caches both int and string values of the underlying item independently to avoid DATETIME aware int-to-string conversion. Thus it completely relies on the ability of the underlying item to correctly convert DATETIME value from int to string and vice versa. --- sql/item.cc | 131 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ sql/item.h | 38 ++++++++++++++++ sql/sql_select.cc | 39 ++++++++++++++++ sql/sql_select.h | 1 + 4 files changed, 209 insertions(+) (limited to 'sql') diff --git a/sql/item.cc b/sql/item.cc index 3bd4744764e..7de32423927 100644 --- a/sql/item.cc +++ b/sql/item.cc @@ -5766,6 +5766,67 @@ bool Item::send(Protocol *protocol, String *buffer) } +/** + Check if an item is a constant one and can be cached. + + @param arg [out] TRUE <=> Cache this item. + + @return TRUE Go deeper in item tree. + @return FALSE Don't go deeper in item tree. +*/ + +bool Item::cache_const_expr_analyzer(uchar **arg) +{ + bool *cache_flag= (bool*)*arg; + if (!*cache_flag) + { + Item *item= real_item(); + /* + Cache constant items unless it's a basic constant, constant field or + a subselect (they use their own cache). + */ + if (const_item() && + !(item->basic_const_item() || item->type() == Item::FIELD_ITEM || + item->type() == SUBSELECT_ITEM || + /* + Do not cache GET_USER_VAR() function as its const_item() may + return TRUE for the current thread but it still may change + during the execution. + */ + (item->type() == Item::FUNC_ITEM && + ((Item_func*)item)->functype() == Item_func::GUSERVAR_FUNC))) + *cache_flag= TRUE; + return TRUE; + } + return FALSE; +} + + +/** + Cache item if needed. + + @param arg TRUE <=> Cache this item. + + @return cache if cache needed. + @return this otherwise. +*/ + +Item* Item::cache_const_expr_transformer(uchar *arg) +{ + if (*(bool*)arg) + { + *((bool*)arg)= FALSE; + Item_cache *cache= Item_cache::get_cache(this); + if (!cache) + return NULL; + cache->setup(this); + cache->store(this); + return cache; + } + return this; +} + + bool Item_field::send(Protocol *protocol, String *buffer) { return protocol->store(result_field); @@ -7127,6 +7188,10 @@ Item_cache* Item_cache::get_cache(const Item *item, const Item_result type) case DECIMAL_RESULT: return new Item_cache_decimal(); case STRING_RESULT: + if (item->field_type() == MYSQL_TYPE_DATE || + item->field_type() == MYSQL_TYPE_DATETIME || + item->field_type() == MYSQL_TYPE_TIME) + return new Item_cache_datetime(item->field_type()); return new Item_cache_str(item); case ROW_RESULT: return new Item_cache_row(); @@ -7208,6 +7273,72 @@ longlong Item_cache_int::val_int() return value; } +void Item_cache_datetime::cache_value_int() +{ + value_cached= TRUE; + /* Assume here that the underlying item will do correct conversion.*/ + int_value= example->val_int_result(); + null_value= example->null_value; + unsigned_flag= example->unsigned_flag; +} + + +void Item_cache_datetime::cache_value() +{ + str_value_cached= TRUE; + /* Assume here that the underlying item will do correct conversion.*/ + String *res= example->str_result(&str_value); + if (res && res != &str_value) + str_value.copy(*res); + null_value= example->null_value; + unsigned_flag= example->unsigned_flag; +} + + +void Item_cache_datetime::store(Item *item, longlong val_arg) +{ + /* An explicit values is given, save it. */ + value_cached= TRUE; + int_value= val_arg; + null_value= item->null_value; + unsigned_flag= item->unsigned_flag; +} + + +String *Item_cache_datetime::val_str(String *str) +{ + DBUG_ASSERT(fixed == 1); + if (!str_value_cached) + cache_value(); + return &str_value; +} + + +my_decimal *Item_cache_datetime::val_decimal(my_decimal *decimal_val) +{ + DBUG_ASSERT(fixed == 1); + if (!value_cached) + cache_value_int(); + int2my_decimal(E_DEC_FATAL_ERROR, int_value, unsigned_flag, decimal_val); + return decimal_val; +} + +double Item_cache_datetime::val_real() +{ + DBUG_ASSERT(fixed == 1); + if (!value_cached) + cache_value_int(); + return (double) int_value; +} + +longlong Item_cache_datetime::val_int() +{ + DBUG_ASSERT(fixed == 1); + if (!value_cached) + cache_value_int(); + return int_value; +} + void Item_cache_real::cache_value() { value_cached= TRUE; diff --git a/sql/item.h b/sql/item.h index f69fa742744..0556c330088 100644 --- a/sql/item.h +++ b/sql/item.h @@ -903,6 +903,9 @@ public: virtual bool reset_query_id_processor(uchar *query_id_arg) { return 0; } virtual bool is_expensive_processor(uchar *arg) { return 0; } virtual bool register_field_in_read_map(uchar *arg) { return 0; } + + virtual bool cache_const_expr_analyzer(uchar **arg); + virtual Item* cache_const_expr_transformer(uchar *arg); /* Check if a partition function is allowed SYNOPSIS @@ -2292,6 +2295,7 @@ public: if (ref && result_type() == ROW_RESULT) (*ref)->bring_value(); } + bool basic_const_item() { return (*ref)->basic_const_item(); } }; @@ -2977,6 +2981,8 @@ public: } virtual void store(Item *item); virtual void cache_value()= 0; + bool basic_const_item() const + { return test(example && example->basic_const_item());} }; @@ -3127,6 +3133,38 @@ public: }; +class Item_cache_datetime: public Item_cache +{ +protected: + String str_value; + ulonglong int_value; + bool str_value_cached; +public: + Item_cache_datetime(enum_field_types field_type_arg): + Item_cache(field_type_arg), int_value(0), str_value_cached(0) + { + cmp_context= STRING_RESULT; + } + + void store(Item *item, longlong val_arg); + double val_real(); + longlong val_int(); + String* val_str(String *str); + my_decimal *val_decimal(my_decimal *); + enum Item_result result_type() const { return STRING_RESULT; } + bool result_as_longlong() { return TRUE; } + /* + In order to avoid INT <-> STRING conversion of a DATETIME value + two cache_value functions are introduced. One (cache_value) caches STRING + value, another (cache_value_int) - INT value. Thus this cache item + completely relies on the ability of the underlying item to do the + correct conversion. + */ + void cache_value_int(); + void cache_value(); +}; + + /* Item_type_holder used to store type. name, length of Item for UNIONS & derived tables. diff --git a/sql/sql_select.cc b/sql/sql_select.cc index f035e28da5c..65ff44622a8 100644 --- a/sql/sql_select.cc +++ b/sql/sql_select.cc @@ -1076,6 +1076,10 @@ JOIN::optimize() { conds=new Item_int((longlong) 0,1); // Always false } + + /* Cache constant expressions in WHERE, HAVING, ON clauses. */ + cache_const_exprs(); + if (make_join_select(this, select, conds)) { zero_result_cause= @@ -17141,6 +17145,41 @@ bool JOIN::change_result(select_result *res) DBUG_RETURN(FALSE); } +/** + Cache constant expressions in WHERE, HAVING, ON conditions. +*/ + +void JOIN::cache_const_exprs() +{ + bool cache_flag= FALSE; + bool *analyzer_arg= &cache_flag; + + /* No need in cache if all tables are constant. */ + if (const_tables == tables) + return; + + if (conds) + conds->compile(&Item::cache_const_expr_analyzer, (uchar **)&analyzer_arg, + &Item::cache_const_expr_transformer, (uchar *)&cache_flag); + cache_flag= FALSE; + if (having) + having->compile(&Item::cache_const_expr_analyzer, (uchar **)&analyzer_arg, + &Item::cache_const_expr_transformer, (uchar *)&cache_flag); + + for (JOIN_TAB *tab= join_tab + const_tables; tab < join_tab + tables ; tab++) + { + if (*tab->on_expr_ref) + { + cache_flag= FALSE; + (*tab->on_expr_ref)->compile(&Item::cache_const_expr_analyzer, + (uchar **)&analyzer_arg, + &Item::cache_const_expr_transformer, + (uchar *)&cache_flag); + } + } +} + + /** @} (end of group Query_Optimizer) */ diff --git a/sql/sql_select.h b/sql/sql_select.h index e049e4ed765..bdca4b196bc 100644 --- a/sql/sql_select.h +++ b/sql/sql_select.h @@ -555,6 +555,7 @@ public: return (unit == &thd->lex->unit && (unit->fake_select_lex == 0 || select_lex == unit->fake_select_lex)); } + void cache_const_exprs(); private: /** TRUE if the query contains an aggregate function but has no GROUP -- cgit v1.2.1 From de95fa0a7de1553dc600e7c3b663f070e8939c84 Mon Sep 17 00:00:00 2001 From: Mikael Ronstrom Date: Wed, 2 Dec 2009 08:14:22 +0100 Subject: BUG#49180, fixed MAXVALUE problem --- sql/partition_info.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'sql') diff --git a/sql/partition_info.cc b/sql/partition_info.cc index 355a4ba6b68..56d79ac0d45 100644 --- a/sql/partition_info.cc +++ b/sql/partition_info.cc @@ -801,7 +801,7 @@ int partition_info::compare_column_values(const void *first_arg, if (first->max_value || second->max_value) { if (first->max_value && second->max_value) - continue; + return 0; if (second->max_value) return -1; else -- cgit v1.2.1 From 0fe17677d8d16dddd17b2a52edd2da98f287d581 Mon Sep 17 00:00:00 2001 From: Tor Didriksen Date: Wed, 2 Dec 2009 11:00:44 +0100 Subject: Bug #49130 Running mtr tests with valgrind and debug produces lots of warnings Use safe output formats for strings that are not null terminated. --- sql/item_func.cc | 2 +- sql/protocol.cc | 4 ++-- sql/sql_test.cc | 21 ++++++++++----------- sql/sql_view.cc | 3 ++- 4 files changed, 15 insertions(+), 15 deletions(-) (limited to 'sql') diff --git a/sql/item_func.cc b/sql/item_func.cc index 455bc5568e3..883476369f2 100644 --- a/sql/item_func.cc +++ b/sql/item_func.cc @@ -3127,7 +3127,7 @@ String *udf_handler::val_str(String *str,String *save_str) if (res == str->ptr()) { str->length(res_length); - DBUG_PRINT("exit", ("str: %s", str->ptr())); + DBUG_PRINT("exit", ("str: %*.s", (int) str->length(), str->ptr())); DBUG_RETURN(str); } save_str->set(res, res_length, str->charset()); diff --git a/sql/protocol.cc b/sql/protocol.cc index ced44a6e972..855a6842f1f 100644 --- a/sql/protocol.cc +++ b/sql/protocol.cc @@ -1012,8 +1012,8 @@ bool Protocol_text::store(const char *from, size_t length, { CHARSET_INFO *tocs= this->thd->variables.character_set_results; #ifndef DBUG_OFF - DBUG_PRINT("info", ("Protocol_text::store field %u (%u): %s", field_pos, - field_count, from)); + DBUG_PRINT("info", ("Protocol_text::store field %u (%u): %*.s", + field_pos, field_count, (int) length, from)); DBUG_ASSERT(field_pos < field_count); DBUG_ASSERT(field_types == 0 || field_types[field_pos] == MYSQL_TYPE_DECIMAL || diff --git a/sql/sql_test.cc b/sql/sql_test.cc index d9beb77f546..f95d19f099e 100644 --- a/sql/sql_test.cc +++ b/sql/sql_test.cc @@ -55,19 +55,18 @@ static const char *lock_descriptions[] = void print_where(COND *cond,const char *info, enum_query_type query_type) { + char buff[256]; + String str(buff,(uint32) sizeof(buff), system_charset_info); + str.length(0); if (cond) - { - char buff[256]; - String str(buff,(uint32) sizeof(buff), system_charset_info); - str.length(0); cond->print(&str, query_type); - str.append('\0'); - DBUG_LOCK_FILE; - (void) fprintf(DBUG_FILE,"\nWHERE:(%s) ",info); - (void) fputs(str.ptr(),DBUG_FILE); - (void) fputc('\n',DBUG_FILE); - DBUG_UNLOCK_FILE; - } + str.append('\0'); + + DBUG_LOCK_FILE; + (void) fprintf(DBUG_FILE,"\nWHERE:(%s) %p ", info, cond); + (void) fputs(str.ptr(),DBUG_FILE); + (void) fputc('\n',DBUG_FILE); + DBUG_UNLOCK_FILE; } /* This is for debugging purposes */ diff --git a/sql/sql_view.cc b/sql/sql_view.cc index ebb0615f80e..f65c5dc54c1 100644 --- a/sql/sql_view.cc +++ b/sql/sql_view.cc @@ -819,7 +819,8 @@ static int mysql_register_view(THD *thd, TABLE_LIST *view, thd->variables.sql_mode|= sql_mode; } - DBUG_PRINT("info", ("View: %s", view_query.ptr())); + DBUG_PRINT("info", + ("View: %*.s", (int) view_query.length(), view_query.ptr())); /* fill structure */ view->source= thd->lex->create_view_select; -- cgit v1.2.1 From 4a8e68fb0bfd6e07c25d2deca4d5936d4fbc88b7 Mon Sep 17 00:00:00 2001 From: Alexander Nozdrin Date: Wed, 2 Dec 2009 18:59:00 +0300 Subject: Fix merge mistake. --- sql/sql_yacc.yy | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'sql') diff --git a/sql/sql_yacc.yy b/sql/sql_yacc.yy index 7fd69a05e98..891131e54f9 100644 --- a/sql/sql_yacc.yy +++ b/sql/sql_yacc.yy @@ -603,10 +603,10 @@ bool my_yyoverflow(short **a, YYSTYPE **b, ulong *yystacksize); %pure_parser /* We have threads */ /* - Currently there are 168 shift/reduce conflicts. + Currently there are 173 shift/reduce conflicts. We should not introduce new conflicts any more. */ -%expect 168 +%expect 173 /* Comments for TOKENS. -- cgit v1.2.1 From 16ec25c0a8438473d2273722de37ade855615ea4 Mon Sep 17 00:00:00 2001 From: He Zhenxing Date: Fri, 4 Dec 2009 09:46:33 +0800 Subject: Bug#49020 Semi-sync master crashed with free_pool == NULL, assertion `free_pool_' Before this patch, semisync assumed transactions running in parallel can not be larger than max_connections, but this is not true when the event scheduler is executing events, and cause semisync run out of preallocated transaction nodes. Fix the problem by allocating transaction nodes dynamically. This patch also fixed a possible deadlock when running UNINSTALL PLUGIN rpl_semi_sync_master and updating in parallel. Fixed by releasing the internal Delegate lock before unlock the plugins. --- sql/rpl_handler.cc | 35 ++++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) (limited to 'sql') diff --git a/sql/rpl_handler.cc b/sql/rpl_handler.cc index da7aade5b99..c4b55e3d068 100644 --- a/sql/rpl_handler.cc +++ b/sql/rpl_handler.cc @@ -137,28 +137,53 @@ void delegates_destroy() */ #define FOREACH_OBSERVER(r, f, thd, args) \ param.server_id= thd->server_id; \ + /* + Use a struct to make sure that they are allocated adjacent, check + delete_dynamic(). + */ \ + struct { \ + DYNAMIC_ARRAY plugins; \ + /* preallocate 8 slots */ \ + plugin_ref plugins_buffer[8]; \ + } s; \ + DYNAMIC_ARRAY *plugins= &s.plugins; \ + plugin_ref *plugins_buffer= s.plugins_buffer; \ + my_init_dynamic_array2(plugins, sizeof(plugin_ref), \ + plugins_buffer, 8, 8); \ read_lock(); \ Observer_info_iterator iter= observer_info_iter(); \ Observer_info *info= iter++; \ for (; info; info= iter++) \ { \ plugin_ref plugin= \ - my_plugin_lock(thd, &info->plugin); \ + my_plugin_lock(0, &info->plugin); \ if (!plugin) \ { \ - r= 1; \ + /* plugin is not intialized or deleted, this is not an error */ \ + r= 0; \ break; \ } \ + insert_dynamic(plugins, (uchar *)&plugin); \ if (((Observer *)info->observer)->f \ && ((Observer *)info->observer)->f args) \ { \ r= 1; \ - plugin_unlock(thd, plugin); \ + sql_print_error("Run function '" #f "' in plugin '%s' failed", \ + info->plugin_int->name.str); \ break; \ } \ - plugin_unlock(thd, plugin); \ } \ - unlock() + unlock(); \ + /* + Unlock plugins should be done after we released the Delegate lock + to avoid possible deadlock when this is the last user of the + plugin, and when we unlock the plugin, it will try to + deinitialize the plugin, which will try to lock the Delegate in + order to remove the observers. + */ \ + plugin_unlock_list(0, (plugin_ref*)plugins->buffer, \ + plugins->elements); \ + delete_dynamic(plugins) int Trans_delegate::after_commit(THD *thd, bool all) -- cgit v1.2.1 From c24ef7e9298f8986641aec57ec4bb5e0ed43c111 Mon Sep 17 00:00:00 2001 From: Magne Mahre Date: Tue, 8 Dec 2009 13:19:38 +0100 Subject: Bug#35589 SET PASSWORD caused a crash Bug#35591 FLUSH PRIVILEGES caused a crash A race condition on the privilege hash tables (proc_priv_hash and func_priv_hash) caused one thread to try to delete elements that had already been deleted by another thread. The bug was caused by reading and saving the pointers to the hash tables outside mutex protection. This led to an inconsistency where a thread copied a pointer to a hash, another thread did the same, the first thread then deleted the hash, and the second then crashed when it in turn tried to delete the deleted hash. The fix is to ensure that operations on the shared hash structures happens under mutex protection (moving the locking up a little) --- sql/sql_acl.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'sql') diff --git a/sql/sql_acl.cc b/sql/sql_acl.cc index 75c69a22119..4288c6ee2f6 100644 --- a/sql/sql_acl.cc +++ b/sql/sql_acl.cc @@ -3806,11 +3806,11 @@ static my_bool grant_reload_procs_priv(THD *thd) DBUG_RETURN(TRUE); } + rw_wrlock(&LOCK_grant); /* Save a copy of the current hash if we need to undo the grant load */ old_proc_priv_hash= proc_priv_hash; old_func_priv_hash= func_priv_hash; - rw_wrlock(&LOCK_grant); if ((return_val= grant_load_procs_priv(table.table))) { /* Error; Reverting to old hash */ -- cgit v1.2.1