From ee09b41ea277ee3916509e8c2ee3145b3b2e6518 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 7 Sep 2006 18:51:00 +0400 Subject: [PATCH 1/6] BUG#20492: Subsequent calls to stored procedure yield incorrect result if join is used For procedures with selects that use complicated joins with ON expression re-execution could erroneously ignore this ON expression, giving incorrect result. The problem was that optimized ON expression wasn't saved for re-execution. The solution is to properly save it. mysql-test/r/sp.result: Add result for bug#20492: Subsequent calls to stored procedure yield incorrect result if join is used. mysql-test/t/sp.test: Add test case for bug#20492: Subsequent calls to stored procedure yield incorrect result if join is used. sql/sql_select.cc: Save modified ON expression for re-execution. --- mysql-test/r/sp.result | 26 ++++++++++++++++++++++++++ mysql-test/t/sp.test | 41 +++++++++++++++++++++++++++++++++++++++++ sql/sql_select.cc | 11 ++++++++--- 3 files changed, 75 insertions(+), 3 deletions(-) diff --git a/mysql-test/r/sp.result b/mysql-test/r/sp.result index 854935b071b..9e4fdb6c6b8 100644 --- a/mysql-test/r/sp.result +++ b/mysql-test/r/sp.result @@ -5394,4 +5394,30 @@ Procedure sql_mode Create Procedure bug21416 CREATE DEFINER=`root`@`localhost` PROCEDURE `bug21416`() show create procedure bug21416 drop procedure bug21416| +DROP PROCEDURE IF EXISTS p1| +DROP VIEW IF EXISTS v1, v2| +DROP TABLE IF EXISTS t3, t4| +CREATE TABLE t3 (t3_id INT)| +INSERT INTO t3 VALUES (0)| +INSERT INTO t3 VALUES (1)| +CREATE TABLE t4 (t4_id INT)| +INSERT INTO t4 VALUES (2)| +CREATE VIEW v1 AS +SELECT t3.t3_id, t4.t4_id +FROM t3 JOIN t4 ON t3.t3_id = 0| +CREATE VIEW v2 AS +SELECT t3.t3_id AS t3_id_1, v1.t3_id AS t3_id_2, v1.t4_id +FROM t3 LEFT JOIN v1 ON t3.t3_id = 0| +CREATE PROCEDURE p1() SELECT * FROM v2| +CALL p1()| +t3_id_1 t3_id_2 t4_id +0 0 2 +1 NULL NULL +CALL p1()| +t3_id_1 t3_id_2 t4_id +0 0 2 +1 NULL NULL +DROP PROCEDURE p1| +DROP VIEW v1, v2| +DROP TABLE t3, t4| drop table t1,t2; diff --git a/mysql-test/t/sp.test b/mysql-test/t/sp.test index 4b0f463a9e3..b355829d7c7 100644 --- a/mysql-test/t/sp.test +++ b/mysql-test/t/sp.test @@ -6322,6 +6322,47 @@ create procedure bug21416() show create procedure bug21416| call bug21416()| drop procedure bug21416| + +# +# BUG#20492: Subsequent calls to stored procedure yeild incorrect +# result if join is used +# +# Optimized ON expression in join wasn't properly saved for reuse. +# +--disable_warnings +DROP PROCEDURE IF EXISTS p1| +DROP VIEW IF EXISTS v1, v2| +DROP TABLE IF EXISTS t3, t4| +--enable_warnings + +CREATE TABLE t3 (t3_id INT)| + +INSERT INTO t3 VALUES (0)| +INSERT INTO t3 VALUES (1)| + +CREATE TABLE t4 (t4_id INT)| + +INSERT INTO t4 VALUES (2)| + +CREATE VIEW v1 AS +SELECT t3.t3_id, t4.t4_id +FROM t3 JOIN t4 ON t3.t3_id = 0| + +CREATE VIEW v2 AS +SELECT t3.t3_id AS t3_id_1, v1.t3_id AS t3_id_2, v1.t4_id +FROM t3 LEFT JOIN v1 ON t3.t3_id = 0| + +CREATE PROCEDURE p1() SELECT * FROM v2| + +# Results should not differ. +CALL p1()| +CALL p1()| + +DROP PROCEDURE p1| +DROP VIEW v1, v2| +DROP TABLE t3, t4| + + # # BUG#NNNN: New bug synopsis # diff --git a/sql/sql_select.cc b/sql/sql_select.cc index 2f16b350d04..f8779de6d58 100644 --- a/sql/sql_select.cc +++ b/sql/sql_select.cc @@ -7427,9 +7427,14 @@ simplify_joins(JOIN *join, List *join_list, COND *conds, bool top) */ expr= simplify_joins(join, &nested_join->join_list, expr, FALSE); - table->on_expr= expr; - if (!table->prep_on_expr) + + if (!table->prep_on_expr || expr != table->on_expr) + { + DBUG_ASSERT(expr); + + table->on_expr= expr; table->prep_on_expr= expr->copy_andor_structure(join->thd); + } } nested_join->used_tables= (table_map) 0; nested_join->not_null_tables=(table_map) 0; @@ -7439,7 +7444,7 @@ simplify_joins(JOIN *join, List *join_list, COND *conds, bool top) } else { - if (!(table->prep_on_expr)) + if (!table->prep_on_expr) table->prep_on_expr= table->on_expr; used_tables= table->table->map; if (conds) From a6131b85c0a938c2ba951ca5bfd772ec94d76d06 Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 7 Mar 2007 09:53:46 -0700 Subject: [PATCH 2/6] Bug 8407, post review cleanup: use instr::get_cont_dest() to get the instruction continuation instruction, for CONTINUE exception handlers. sql/sp_head.cc: Post review cleanup: use instr::get_cont_dest() to get the instruction continuation instruction, for CONTINUE exception handlers. sql/sp_head.h: Post review cleanup: use instr::get_cont_dest() to get the instruction continuation instruction, for CONTINUE exception handlers. --- sql/sp_head.cc | 20 +++++++------------- sql/sp_head.h | 27 ++++++++++++--------------- 2 files changed, 19 insertions(+), 28 deletions(-) diff --git a/sql/sp_head.cc b/sql/sp_head.cc index baeedc1c9b3..c1643f0f82e 100644 --- a/sql/sp_head.cc +++ b/sql/sp_head.cc @@ -1078,7 +1078,7 @@ sp_head::execute(THD *thd) case SP_HANDLER_CONTINUE: thd->restore_active_arena(&execute_arena, &backup_arena); thd->set_n_backup_active_arena(&execute_arena, &backup_arena); - ctx->push_hstack(ip); + ctx->push_hstack(i->get_cont_dest()); // Fall through default: ip= hip; @@ -2394,7 +2394,7 @@ sp_lex_keeper::reset_lex_and_exec_core(THD *thd, uint *nextp, reinit_stmt_before_use(thd, m_lex); if (open_tables) - res= instr->exec_open_and_lock_tables(thd, m_lex->query_tables, nextp); + res= instr->exec_open_and_lock_tables(thd, m_lex->query_tables); if (!res) res= instr->exec_core(thd, nextp); @@ -2443,8 +2443,7 @@ sp_lex_keeper::reset_lex_and_exec_core(THD *thd, uint *nextp, sp_instr class functions */ -int sp_instr::exec_open_and_lock_tables(THD *thd, TABLE_LIST *tables, - uint *nextp) +int sp_instr::exec_open_and_lock_tables(THD *thd, TABLE_LIST *tables) { int result; @@ -2454,19 +2453,16 @@ int sp_instr::exec_open_and_lock_tables(THD *thd, TABLE_LIST *tables, */ if (check_table_access(thd, SELECT_ACL, tables, 0) || open_and_lock_tables(thd, tables)) - { - get_cont_dest(nextp); result= -1; - } else result= 0; return result; } -void sp_instr::get_cont_dest(uint *nextp) +uint sp_instr::get_cont_dest() { - *nextp= m_ip+1; + return (m_ip+1); } @@ -2654,9 +2650,9 @@ sp_instr_set_trigger_field::print(String *str) sp_instr_opt_meta */ -void sp_instr_opt_meta::get_cont_dest(uint *nextp) +uint sp_instr_opt_meta::get_cont_dest() { - *nextp= m_cont_dest; + return m_cont_dest; } @@ -2748,7 +2744,6 @@ sp_instr_jump_if_not::exec_core(THD *thd, uint *nextp) if (! it) { res= -1; - *nextp = m_cont_dest; } else { @@ -3317,7 +3312,6 @@ sp_instr_set_case_expr::exec_core(THD *thd, uint *nextp) spcont->clear_handler(); thd->spcont= spcont; } - *nextp= m_cont_dest; /* For continue handler */ } else *nextp= m_ip+1; diff --git a/sql/sp_head.h b/sql/sp_head.h index 10eada43721..4ef4077cc79 100644 --- a/sql/sp_head.h +++ b/sql/sp_head.h @@ -449,13 +449,15 @@ public: thd Thread handle nextp OUT index of the next instruction to execute. (For most instructions this will be the instruction following this - one). - - RETURN - 0 on success, - other if some error occured + one). Note that this parameter is undefined in case of + errors, use get_cont_dest() to find the continuation + instruction for CONTINUE error handlers. + + RETURN + 0 on success, + other if some error occurred */ - + virtual int execute(THD *thd, uint *nextp) = 0; /** @@ -463,22 +465,17 @@ public: Open and lock the tables used by this statement, as a pre-requisite to execute the core logic of this instruction with exec_core(). - If this statement fails, the next instruction to execute is also returned. - This is useful when a user defined SQL continue handler needs to be - executed. @param thd the current thread @param tables the list of tables to open and lock - @param nextp the continuation instruction, returned to the caller if this - method fails. @return zero on success, non zero on failure. */ - int exec_open_and_lock_tables(THD *thd, TABLE_LIST *tables, uint *nextp); + int exec_open_and_lock_tables(THD *thd, TABLE_LIST *tables); /** Get the continuation destination of this instruction. - @param nextp the continuation destination (output) + @return the continuation destination */ - virtual void get_cont_dest(uint *nextp); + virtual uint get_cont_dest(); /* Execute core function of instruction after all preparations (e.g. @@ -744,7 +741,7 @@ public: virtual void set_destination(uint old_dest, uint new_dest) = 0; - virtual void get_cont_dest(uint *nextp); + virtual uint get_cont_dest(); protected: From b33780693cbeb52bed77a114692e6ba5e0b008d8 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 8 Mar 2007 12:04:45 +0300 Subject: [PATCH 3/6] Fix -ansi -pedantic warning (can't cast a pointer to function to a pointer to object, that is, int foo(); void *bar= (void*) foo is not allowed. sql/lex.h: Fix -ansi -pedantic warning. sql/lex_symbol.h: Fix -ansi -pedantic warning. --- sql/lex.h | 4 ++-- sql/lex_symbol.h | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/sql/lex.h b/sql/lex.h index 5299be89d35..352d80da5c6 100644 --- a/sql/lex.h +++ b/sql/lex.h @@ -32,10 +32,10 @@ SYM_GROUP sym_group_rtree= {"RTree keys", "HAVE_RTREE_KEYS"}; #define SYM(A) SYM_OR_NULL(A),0,0,&sym_group_common #define F_SYM(A) SYM_OR_NULL(A) -#define CREATE_FUNC(A) (void *)(SYM_OR_NULL(A)), &sym_group_common +#define CREATE_FUNC(A) (void (*)())(SYM_OR_NULL(A)), &sym_group_common #ifdef HAVE_SPATIAL -#define CREATE_FUNC_GEOM(A) (void *)(SYM_OR_NULL(A)), &sym_group_geom +#define CREATE_FUNC_GEOM(A) (void (*)())(SYM_OR_NULL(A)), &sym_group_geom #else #define CREATE_FUNC_GEOM(A) 0, &sym_group_geom #endif diff --git a/sql/lex_symbol.h b/sql/lex_symbol.h index 5d929508030..c87cdb4ec43 100644 --- a/sql/lex_symbol.h +++ b/sql/lex_symbol.h @@ -25,7 +25,7 @@ typedef struct st_symbol { const char *name; uint tok; uint length; - void *create_func; + void (*create_func)(); struct st_sym_group *group; } SYMBOL; From a0521cd7493ec309e4685d5af7563d2403a759b0 Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 9 Mar 2007 08:05:08 +0300 Subject: [PATCH 4/6] Polishing: use constants instead of magic numbers. include/my_global.h: Introduce constants to be used instead of magic numbers. sql/field.cc: Polishing: use contants instead of magic numbers. sql/ha_innodb.cc: Polishing: use contants instead of magic numbers. sql/handler.cc: Polishing: use contants instead of magic numbers. sql/item.cc: Polishing: use contants instead of magic numbers. sql/item.h: Polishing: use contants instead of magic numbers. sql/item_func.cc: Polishing: use contants instead of magic numbers. sql/item_subselect.cc: Polishing: use contants instead of magic numbers. sql/log_event.cc: Polishing: use contants instead of magic numbers. sql/sql_base.cc: Polishing: use contants instead of magic numbers. sql/sql_select.cc: Polishing: use contants instead of magic numbers. sql/sql_show.cc: Polishing: use contants instead of magic numbers. sql/sql_table.cc: Polishing: use contants instead of magic numbers. --- include/my_global.h | 9 +++++++++ sql/field.cc | 6 +++--- sql/ha_innodb.cc | 16 ++++++++-------- sql/handler.cc | 6 +++--- sql/item.cc | 6 +++--- sql/item.h | 9 ++++++--- sql/item_func.cc | 5 +++-- sql/item_subselect.cc | 3 ++- sql/log_event.cc | 5 +++-- sql/sql_base.cc | 3 ++- sql/sql_select.cc | 6 +++--- sql/sql_show.cc | 43 +++++++++++++++++++++++++------------------ sql/sql_table.cc | 3 ++- 13 files changed, 72 insertions(+), 48 deletions(-) diff --git a/include/my_global.h b/include/my_global.h index 21fe1ebc3cb..61c2afc541b 100644 --- a/include/my_global.h +++ b/include/my_global.h @@ -1357,4 +1357,13 @@ do { doubleget_union _tmp; \ #define NO_EMBEDDED_ACCESS_CHECKS #endif + +/* Length of decimal number represented by INT32. */ + +#define MY_INT32_NUM_DECIMAL_DIGITS 11 + +/* Length of decimal number represented by INT64. */ + +#define MY_INT64_NUM_DECIMAL_DIGITS 21 + #endif /* my_global_h */ diff --git a/sql/field.cc b/sql/field.cc index 367cbdaa0e5..aabb0ff6061 100644 --- a/sql/field.cc +++ b/sql/field.cc @@ -1203,12 +1203,12 @@ static bool test_if_real(const char *str,int length, CHARSET_INFO *cs) String *Field::val_int_as_str(String *val_buffer, my_bool unsigned_val) { CHARSET_INFO *cs= &my_charset_bin; - uint length= 21; + uint length; longlong value= val_int(); - if (val_buffer->alloc(length)) + if (val_buffer->alloc(MY_INT64_NUM_DECIMAL_DIGITS)) return 0; length= (uint) (*cs->cset->longlong10_to_str)(cs, (char*) val_buffer->ptr(), - length, + MY_INT64_NUM_DECIMAL_DIGITS, unsigned_val ? 10 : -10, value); val_buffer->length(length); diff --git a/sql/ha_innodb.cc b/sql/ha_innodb.cc index 8a35ff000a8..cbefa9d3949 100644 --- a/sql/ha_innodb.cc +++ b/sql/ha_innodb.cc @@ -6391,16 +6391,16 @@ innodb_mutex_show_status( #ifdef UNIV_DEBUG field_list.push_back(new Item_empty_string("Mutex", FN_REFLEN)); field_list.push_back(new Item_empty_string("Module", FN_REFLEN)); - field_list.push_back(new Item_uint("Count", 21)); - field_list.push_back(new Item_uint("Spin_waits", 21)); - field_list.push_back(new Item_uint("Spin_rounds", 21)); - field_list.push_back(new Item_uint("OS_waits", 21)); - field_list.push_back(new Item_uint("OS_yields", 21)); - field_list.push_back(new Item_uint("OS_waits_time", 21)); + field_list.push_back(new Item_uint("Count", MY_INT64_NUM_DECIMAL_DIGITS)); + field_list.push_back(new Item_uint("Spin_waits", MY_INT64_NUM_DECIMAL_DIGITS)); + field_list.push_back(new Item_uint("Spin_rounds", MY_INT64_NUM_DECIMAL_DIGITS)); + field_list.push_back(new Item_uint("OS_waits", MY_INT64_NUM_DECIMAL_DIGITS)); + field_list.push_back(new Item_uint("OS_yields", MY_INT64_NUM_DECIMAL_DIGITS)); + field_list.push_back(new Item_uint("OS_waits_time", MY_INT64_NUM_DECIMAL_DIGITS)); #else /* UNIV_DEBUG */ field_list.push_back(new Item_empty_string("File", FN_REFLEN)); - field_list.push_back(new Item_uint("Line", 21)); - field_list.push_back(new Item_uint("OS_waits", 21)); + field_list.push_back(new Item_uint("Line", MY_INT64_NUM_DECIMAL_DIGITS)); + field_list.push_back(new Item_uint("OS_waits", MY_INT64_NUM_DECIMAL_DIGITS)); #endif /* UNIV_DEBUG */ if (protocol->send_fields(&field_list, diff --git a/sql/handler.cc b/sql/handler.cc index 5a27e470d70..524f47209dc 100644 --- a/sql/handler.cc +++ b/sql/handler.cc @@ -1082,9 +1082,9 @@ bool mysql_xa_recover(THD *thd) XID_STATE *xs; DBUG_ENTER("mysql_xa_recover"); - field_list.push_back(new Item_int("formatID",0,11)); - field_list.push_back(new Item_int("gtrid_length",0,11)); - field_list.push_back(new Item_int("bqual_length",0,11)); + field_list.push_back(new Item_int("formatID", 0, MY_INT32_NUM_DECIMAL_DIGITS)); + field_list.push_back(new Item_int("gtrid_length", 0, MY_INT32_NUM_DECIMAL_DIGITS)); + field_list.push_back(new Item_int("bqual_length", 0, MY_INT32_NUM_DECIMAL_DIGITS)); field_list.push_back(new Item_empty_string("data",XIDDATASIZE)); if (protocol->send_fields(&field_list, diff --git a/sql/item.cc b/sql/item.cc index 257687ebaaf..863b18739d1 100644 --- a/sql/item.cc +++ b/sql/item.cc @@ -148,7 +148,7 @@ void Hybrid_type_traits_integer::fix_length_and_dec(Item *item, Item *arg) const { item->decimals= 0; - item->max_length= 21; + item->max_length= MY_INT64_NUM_DECIMAL_DIGITS; item->unsigned_flag= 0; } @@ -2491,7 +2491,7 @@ bool Item_param::set_from_user_var(THD *thd, const user_var_entry *entry) item_result_type= REAL_RESULT; break; case INT_RESULT: - set_int(*(longlong*)entry->value, 21); + set_int(*(longlong*)entry->value, MY_INT64_NUM_DECIMAL_DIGITS); item_type= Item::INT_ITEM; item_result_type= INT_RESULT; break; @@ -6535,7 +6535,7 @@ uint32 Item_type_holder::display_length(Item *item) case MYSQL_TYPE_SHORT: return 6; case MYSQL_TYPE_LONG: - return 11; + return MY_INT32_NUM_DECIMAL_DIGITS; case MYSQL_TYPE_FLOAT: return 25; case MYSQL_TYPE_DOUBLE: diff --git a/sql/item.h b/sql/item.h index 833bebdee7e..39cdb68fa00 100644 --- a/sql/item.h +++ b/sql/item.h @@ -1494,11 +1494,14 @@ class Item_int :public Item_num { public: longlong value; - Item_int(int32 i,uint length=11) :value((longlong) i) + Item_int(int32 i,uint length= MY_INT32_NUM_DECIMAL_DIGITS) + :value((longlong) i) { max_length=length; fixed= 1; } - Item_int(longlong i,uint length=21) :value(i) + Item_int(longlong i,uint length= MY_INT64_NUM_DECIMAL_DIGITS) + :value(i) { max_length=length; fixed= 1; } - Item_int(ulonglong i, uint length= 21) :value((longlong)i) + Item_int(ulonglong i, uint length= MY_INT64_NUM_DECIMAL_DIGITS) + :value((longlong)i) { max_length=length; fixed= 1; unsigned_flag= 1; } Item_int(const char *str_arg,longlong i,uint length) :value(i) { max_length=length; name=(char*) str_arg; fixed= 1; } diff --git a/sql/item_func.cc b/sql/item_func.cc index e41bf25e8e9..6c4cf8cc5bd 100644 --- a/sql/item_func.cc +++ b/sql/item_func.cc @@ -426,7 +426,7 @@ Field *Item_func::tmp_table_field(TABLE *t_arg) switch (result_type()) { case INT_RESULT: - if (max_length > 11) + if (max_length > MY_INT32_NUM_DECIMAL_DIGITS) res= new Field_longlong(max_length, maybe_null, name, t_arg, unsigned_flag); else @@ -2316,7 +2316,8 @@ longlong Item_func_coercibility::val_int() void Item_func_locate::fix_length_and_dec() { - maybe_null=0; max_length=11; + maybe_null= 0; + max_length= MY_INT32_NUM_DECIMAL_DIGITS; agg_arg_charsets(cmp_collation, args, 2, MY_COLL_CMP_CONV, 1); } diff --git a/sql/item_subselect.cc b/sql/item_subselect.cc index 12ae0c026eb..b3744d6eb96 100644 --- a/sql/item_subselect.cc +++ b/sql/item_subselect.cc @@ -1037,7 +1037,8 @@ Item_in_subselect::single_value_transformer(JOIN *join, Item *having= item, *orig_item= item; select_lex->item_list.empty(); select_lex->item_list.push_back(new Item_int("Not_used", - (longlong) 1, 21)); + (longlong) 1, + MY_INT64_NUM_DECIMAL_DIGITS)); select_lex->ref_pointer_array[0]= select_lex->item_list.head(); item= func->create(expr, item); diff --git a/sql/log_event.cc b/sql/log_event.cc index dbf69acf70a..e272140c080 100644 --- a/sql/log_event.cc +++ b/sql/log_event.cc @@ -541,12 +541,13 @@ int Log_event::net_send(Protocol *protocol, const char* log_name, my_off_t pos) void Log_event::init_show_field_list(List* field_list) { field_list->push_back(new Item_empty_string("Log_name", 20)); - field_list->push_back(new Item_return_int("Pos", 11, + field_list->push_back(new Item_return_int("Pos", MY_INT32_NUM_DECIMAL_DIGITS, MYSQL_TYPE_LONGLONG)); field_list->push_back(new Item_empty_string("Event_type", 20)); field_list->push_back(new Item_return_int("Server_id", 10, MYSQL_TYPE_LONG)); - field_list->push_back(new Item_return_int("End_log_pos", 11, + field_list->push_back(new Item_return_int("End_log_pos", + MY_INT32_NUM_DECIMAL_DIGITS, MYSQL_TYPE_LONGLONG)); field_list->push_back(new Item_empty_string("Info", 20)); } diff --git a/sql/sql_base.cc b/sql/sql_base.cc index 6f114165fa6..e3f44539bc7 100644 --- a/sql/sql_base.cc +++ b/sql/sql_base.cc @@ -4559,7 +4559,8 @@ int setup_wild(THD *thd, TABLE_LIST *tables, List &fields, Item_int do not need fix_fields() because it is basic constant. */ - it.replace(new Item_int("Not_used", (longlong) 1, 21)); + it.replace(new Item_int("Not_used", (longlong) 1, + MY_INT64_NUM_DECIMAL_DIGITS)); } else if (insert_fields(thd, ((Item_field*) item)->context, ((Item_field*) item)->db_name, diff --git a/sql/sql_select.cc b/sql/sql_select.cc index 346b8c13940..b1ec333e81d 100644 --- a/sql/sql_select.cc +++ b/sql/sql_select.cc @@ -8491,7 +8491,7 @@ remove_eq_conds(THD *thd, COND *cond, Item::cond_result *cond_value) if ((new_cond= new Item_func_eq(args[0], new Item_int("last_insert_id()", thd->current_insert_id, - 21)))) + MY_INT64_NUM_DECIMAL_DIGITS)))) { /* Set THD::last_insert_id_used_bin_log manually, as this @@ -8757,7 +8757,7 @@ static Field *create_tmp_field_from_item(THD *thd, Item *item, TABLE *table, break; case INT_RESULT: /* Select an integer type with the minimal fit precision */ - if (item->max_length > 11) + if (item->max_length > MY_INT32_NUM_DECIMAL_DIGITS) new_field=new Field_longlong(item->max_length, maybe_null, item->name, table, item->unsigned_flag); else @@ -14964,7 +14964,7 @@ static void select_describe(JOIN *join, bool need_tmp_table, bool need_order, /* Add "rows" field to item_list. */ item_list.push_back(new Item_int((longlong) (ulonglong) join->best_positions[i]. records_read, - 21)); + MY_INT64_NUM_DECIMAL_DIGITS)); /* Build "Extra" field and add it to item_list. */ my_bool key_read=table->key_read; if ((tab->type == JT_NEXT || tab->type == JT_CONST) && diff --git a/sql/sql_show.cc b/sql/sql_show.cc index c4b06934fc3..4de70a67b36 100644 --- a/sql/sql_show.cc +++ b/sql/sql_show.cc @@ -205,7 +205,8 @@ bool mysqld_show_column_types(THD *thd) DBUG_ENTER("mysqld_show_column_types"); field_list.push_back(new Item_empty_string("Type",30)); - field_list.push_back(new Item_int("Size",(longlong) 1,21)); + field_list.push_back(new Item_int("Size",(longlong) 1, + MY_INT64_NUM_DECIMAL_DIGITS)); field_list.push_back(new Item_empty_string("Min_Value",20)); field_list.push_back(new Item_empty_string("Max_Value",20)); field_list.push_back(new Item_return_int("Prec", 4, MYSQL_TYPE_SHORT)); @@ -1284,7 +1285,7 @@ void mysqld_list_processes(THD *thd,const char *user, bool verbose) Protocol *protocol= thd->protocol; DBUG_ENTER("mysqld_list_processes"); - field_list.push_back(new Item_int("Id",0,11)); + field_list.push_back(new Item_int("Id", 0, MY_INT32_NUM_DECIMAL_DIGITS)); field_list.push_back(new Item_empty_string("User",16)); field_list.push_back(new Item_empty_string("Host",LIST_PROCESS_HOST_LEN)); field_list.push_back(field=new Item_empty_string("db",NAME_LEN)); @@ -4038,20 +4039,25 @@ ST_FIELD_INFO tables_fields_info[]= {"TABLE_NAME", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, "Name"}, {"TABLE_TYPE", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, 0}, {"ENGINE", NAME_LEN, MYSQL_TYPE_STRING, 0, 1, "Engine"}, - {"VERSION", 21 , MYSQL_TYPE_LONG, 0, 1, "Version"}, + {"VERSION", MY_INT64_NUM_DECIMAL_DIGITS , MYSQL_TYPE_LONG, 0, 1, "Version"}, {"ROW_FORMAT", 10, MYSQL_TYPE_STRING, 0, 1, "Row_format"}, - {"TABLE_ROWS", 21 , MYSQL_TYPE_LONG, 0, 1, "Rows"}, - {"AVG_ROW_LENGTH", 21 , MYSQL_TYPE_LONG, 0, 1, "Avg_row_length"}, - {"DATA_LENGTH", 21 , MYSQL_TYPE_LONG, 0, 1, "Data_length"}, - {"MAX_DATA_LENGTH", 21 , MYSQL_TYPE_LONG, 0, 1, "Max_data_length"}, - {"INDEX_LENGTH", 21 , MYSQL_TYPE_LONG, 0, 1, "Index_length"}, - {"DATA_FREE", 21 , MYSQL_TYPE_LONG, 0, 1, "Data_free"}, - {"AUTO_INCREMENT", 21 , MYSQL_TYPE_LONG, 0, 1, "Auto_increment"}, + {"TABLE_ROWS", MY_INT64_NUM_DECIMAL_DIGITS , MYSQL_TYPE_LONG, 0, 1, "Rows"}, + {"AVG_ROW_LENGTH", MY_INT64_NUM_DECIMAL_DIGITS , MYSQL_TYPE_LONG, 0, 1, + "Avg_row_length"}, + {"DATA_LENGTH", MY_INT64_NUM_DECIMAL_DIGITS , MYSQL_TYPE_LONG, 0, 1, + "Data_length"}, + {"MAX_DATA_LENGTH", MY_INT64_NUM_DECIMAL_DIGITS , MYSQL_TYPE_LONG, 0, 1, + "Max_data_length"}, + {"INDEX_LENGTH", MY_INT64_NUM_DECIMAL_DIGITS , MYSQL_TYPE_LONG, 0, 1, + "Index_length"}, + {"DATA_FREE", MY_INT64_NUM_DECIMAL_DIGITS , MYSQL_TYPE_LONG, 0, 1, "Data_free"}, + {"AUTO_INCREMENT", MY_INT64_NUM_DECIMAL_DIGITS , MYSQL_TYPE_LONG, 0, 1, + "Auto_increment"}, {"CREATE_TIME", 0, MYSQL_TYPE_TIMESTAMP, 0, 1, "Create_time"}, {"UPDATE_TIME", 0, MYSQL_TYPE_TIMESTAMP, 0, 1, "Update_time"}, {"CHECK_TIME", 0, MYSQL_TYPE_TIMESTAMP, 0, 1, "Check_time"}, {"TABLE_COLLATION", 64, MYSQL_TYPE_STRING, 0, 1, "Collation"}, - {"CHECKSUM", 21 , MYSQL_TYPE_LONG, 0, 1, "Checksum"}, + {"CHECKSUM", MY_INT64_NUM_DECIMAL_DIGITS , MYSQL_TYPE_LONG, 0, 1, "Checksum"}, {"CREATE_OPTIONS", 255, MYSQL_TYPE_STRING, 0, 1, "Create_options"}, {"TABLE_COMMENT", 80, MYSQL_TYPE_STRING, 0, 0, "Comment"}, {0, 0, MYSQL_TYPE_STRING, 0, 0, 0} @@ -4064,14 +4070,15 @@ ST_FIELD_INFO columns_fields_info[]= {"TABLE_SCHEMA", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, 0}, {"TABLE_NAME", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, 0}, {"COLUMN_NAME", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, "Field"}, - {"ORDINAL_POSITION", 21 , MYSQL_TYPE_LONG, 0, 0, 0}, + {"ORDINAL_POSITION", MY_INT64_NUM_DECIMAL_DIGITS , MYSQL_TYPE_LONG, 0, 0, 0}, {"COLUMN_DEFAULT", MAX_FIELD_VARCHARLENGTH, MYSQL_TYPE_STRING, 0, 1, "Default"}, {"IS_NULLABLE", 3, MYSQL_TYPE_STRING, 0, 0, "Null"}, {"DATA_TYPE", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, 0}, - {"CHARACTER_MAXIMUM_LENGTH", 21 , MYSQL_TYPE_LONG, 0, 1, 0}, - {"CHARACTER_OCTET_LENGTH", 21 , MYSQL_TYPE_LONG, 0, 1, 0}, - {"NUMERIC_PRECISION", 21 , MYSQL_TYPE_LONG, 0, 1, 0}, - {"NUMERIC_SCALE", 21 , MYSQL_TYPE_LONG, 0, 1, 0}, + {"CHARACTER_MAXIMUM_LENGTH", MY_INT64_NUM_DECIMAL_DIGITS , MYSQL_TYPE_LONG, 0, 1, + 0}, + {"CHARACTER_OCTET_LENGTH", MY_INT64_NUM_DECIMAL_DIGITS , MYSQL_TYPE_LONG, 0, 1, 0}, + {"NUMERIC_PRECISION", MY_INT64_NUM_DECIMAL_DIGITS , MYSQL_TYPE_LONG, 0, 1, 0}, + {"NUMERIC_SCALE", MY_INT64_NUM_DECIMAL_DIGITS , MYSQL_TYPE_LONG, 0, 1, 0}, {"CHARACTER_SET_NAME", 64, MYSQL_TYPE_STRING, 0, 1, 0}, {"COLLATION_NAME", 64, MYSQL_TYPE_STRING, 0, 1, "Collation"}, {"COLUMN_TYPE", 65535, MYSQL_TYPE_STRING, 0, 0, "Type"}, @@ -4097,7 +4104,7 @@ ST_FIELD_INFO collation_fields_info[]= { {"COLLATION_NAME", 64, MYSQL_TYPE_STRING, 0, 0, "Collation"}, {"CHARACTER_SET_NAME", 64, MYSQL_TYPE_STRING, 0, 0, "Charset"}, - {"ID", 11, MYSQL_TYPE_LONG, 0, 0, "Id"}, + {"ID", MY_INT32_NUM_DECIMAL_DIGITS, MYSQL_TYPE_LONG, 0, 0, "Id"}, {"IS_DEFAULT", 3, MYSQL_TYPE_STRING, 0, 0, "Default"}, {"IS_COMPILED", 3, MYSQL_TYPE_STRING, 0, 0, "Compiled"}, {"SORTLEN", 3 ,MYSQL_TYPE_LONG, 0, 0, "Sortlen"}, @@ -4150,7 +4157,7 @@ ST_FIELD_INFO stat_fields_info[]= {"SEQ_IN_INDEX", 2, MYSQL_TYPE_LONG, 0, 0, "Seq_in_index"}, {"COLUMN_NAME", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, "Column_name"}, {"COLLATION", 1, MYSQL_TYPE_STRING, 0, 1, "Collation"}, - {"CARDINALITY", 21, MYSQL_TYPE_LONG, 0, 1, "Cardinality"}, + {"CARDINALITY", MY_INT32_NUM_DECIMAL_DIGITS, MYSQL_TYPE_LONG, 0, 1, "Cardinality"}, {"SUB_PART", 3, MYSQL_TYPE_LONG, 0, 1, "Sub_part"}, {"PACKED", 10, MYSQL_TYPE_STRING, 0, 1, "Packed"}, {"NULLABLE", 3, MYSQL_TYPE_STRING, 0, 0, "Null"}, diff --git a/sql/sql_table.cc b/sql/sql_table.cc index 512d990347f..6bcca55dfff 100644 --- a/sql/sql_table.cc +++ b/sql/sql_table.cc @@ -4146,7 +4146,8 @@ bool mysql_checksum_table(THD *thd, TABLE_LIST *tables, HA_CHECK_OPT *check_opt) field_list.push_back(item = new Item_empty_string("Table", NAME_LEN*2)); item->maybe_null= 1; - field_list.push_back(item=new Item_int("Checksum",(longlong) 1,21)); + field_list.push_back(item= new Item_int("Checksum", (longlong) 1, + MY_INT64_NUM_DECIMAL_DIGITS)); item->maybe_null= 1; if (protocol->send_fields(&field_list, Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF)) From 264bcedd7e50f4e55b722d3ae29c5a30da461b6e Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 9 Mar 2007 10:44:48 +0300 Subject: [PATCH 5/6] Fix typo. --- sql/sql_show.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sql/sql_show.cc b/sql/sql_show.cc index 4de70a67b36..68fa07deec4 100644 --- a/sql/sql_show.cc +++ b/sql/sql_show.cc @@ -4157,7 +4157,7 @@ ST_FIELD_INFO stat_fields_info[]= {"SEQ_IN_INDEX", 2, MYSQL_TYPE_LONG, 0, 0, "Seq_in_index"}, {"COLUMN_NAME", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, "Column_name"}, {"COLLATION", 1, MYSQL_TYPE_STRING, 0, 1, "Collation"}, - {"CARDINALITY", MY_INT32_NUM_DECIMAL_DIGITS, MYSQL_TYPE_LONG, 0, 1, "Cardinality"}, + {"CARDINALITY", MY_INT64_NUM_DECIMAL_DIGITS, MYSQL_TYPE_LONG, 0, 1, "Cardinality"}, {"SUB_PART", 3, MYSQL_TYPE_LONG, 0, 1, "Sub_part"}, {"PACKED", 10, MYSQL_TYPE_STRING, 0, 1, "Packed"}, {"NULLABLE", 3, MYSQL_TYPE_STRING, 0, 0, "Null"}, From 79344c7b673883bcde941c9c9fc9cc66c7114a39 Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 14 Mar 2007 12:02:32 -0600 Subject: [PATCH 6/6] Bug#26503 (Illegal SQL exception handler code causes the server to crash) Before this fix, the parser would accept illegal code in SQL exceptions handlers, that later causes the runtime to crash when executing the code, due to memory violations in the exception handler stack. The root cause of the problem is instructions within an exception handler that jumps to code located outside of the handler. This is illegal according to the SQL 2003 standard, since labels located outside the handler are not supposed to be visible (they are "out of scope"), so any instruction that jumps to these labels, like ITERATE or LEAVE, should not parse. The section of the standard that is relevant for this is : SQL:2003 SQL/PSM (ISO/IEC 9075-4:2003) section 13.1 , syntax rule 4 The scope of the is CS excluding every contained in CS and excluding every contained in CS. shall not be equivalent to any other s within that scope. With this fix, the C++ class sp_pcontext, which represent the "parsing context" tree (a.k.a symbol table) of a stored procedure, has been changed as follows: - constructors have been cleaned up, so that only building a root node for the tree is public; building nodes inside a tree is not public. - a new member, m_label_scope, indicates if a given syntactic context belongs to a DECLARE HANDLER block, - label resolution, in the method find_label(), has been changed to implement the restriction of scope regarding labels used in a compound statement. The actions in the parser, when parsing the body of a SQL exception handler, have been changed as follows: - the implementation of an exception handler (DECLARE HANDLER) now creates explicitly a new sp_pcontext, to isolate the code inside the handler from the containing compound statement context. - registering exception handlers as a result occurs in the parent context, see the rule sp_hcond_element - the code in sp_hcond_list has been cleaned up, to avoid code duplication In addition, the flags IN_SIMPLE_CASE and IN_HANDLER, declared in sp_head.h have been removed, since they are unused and broken by design (as seen with Bug 19194 (Right recursion in parser for CASE causes excessive stack usage, limitation), representing a stack in a single flag is not possible. Tests in sp-error have been added to show that illegal constructs are now rejected. Tests in sp have been added for code coverage, to show that ITERATE or LEAVE statements are legal when jumping to a label in scope, inside the body of an exception handler. mysql-test/r/sp-error.result: SQL Exception handlers define a parsing context for label resolution. mysql-test/r/sp.result: SQL Exception handlers define a parsing context for label resolution. mysql-test/t/sp-error.test: SQL Exception handlers define a parsing context for label resolution. mysql-test/t/sp.test: SQL Exception handlers define a parsing context for label resolution. sql/sp_head.cc: Minor cleanup sql/sp_head.h: Minor cleanup sql/sp_pcontext.cc: SQL Exception handlers define a parsing context for label resolution. sql/sp_pcontext.h: SQL Exception handlers define a parsing context for label resolution. sql/sql_yacc.yy: SQL Exception handlers define a parsing context for label resolution. --- mysql-test/r/sp-error.result | 52 +++++++++++ mysql-test/r/sp.result | 165 +++++++++++++++++++++++++++++++++++ mysql-test/t/sp-error.test | 68 +++++++++++++++ mysql-test/t/sp.test | 135 ++++++++++++++++++++++++++++ sql/sp_head.cc | 2 +- sql/sp_head.h | 2 - sql/sp_pcontext.cc | 90 ++++++++++++++----- sql/sp_pcontext.h | 61 ++++++++++--- sql/sql_yacc.yy | 43 ++++----- 9 files changed, 556 insertions(+), 62 deletions(-) diff --git a/mysql-test/r/sp-error.result b/mysql-test/r/sp-error.result index 332d4fa4519..bdcb51c4db8 100644 --- a/mysql-test/r/sp-error.result +++ b/mysql-test/r/sp-error.result @@ -1400,3 +1400,55 @@ drop table table_25345_b; drop procedure proc_25345; drop function func_25345; drop function func_25345_b; +create procedure proc_26503_error_1() +begin +retry: +repeat +begin +declare continue handler for sqlexception +begin +iterate retry; +end +select "do something"; +end +until true end repeat retry; +end// +ERROR 42000: ITERATE with no matching label: retry +create procedure proc_26503_error_2() +begin +retry: +repeat +begin +declare continue handler for sqlexception +iterate retry; +select "do something"; +end +until true end repeat retry; +end// +ERROR 42000: ITERATE with no matching label: retry +create procedure proc_26503_error_3() +begin +retry: +repeat +begin +declare continue handler for sqlexception +begin +leave retry; +end +select "do something"; +end +until true end repeat retry; +end// +ERROR 42000: LEAVE with no matching label: retry +create procedure proc_26503_error_4() +begin +retry: +repeat +begin +declare continue handler for sqlexception +leave retry; +select "do something"; +end +until true end repeat retry; +end// +ERROR 42000: LEAVE with no matching label: retry diff --git a/mysql-test/r/sp.result b/mysql-test/r/sp.result index afc3b95eb5f..fb8b7467746 100644 --- a/mysql-test/r/sp.result +++ b/mysql-test/r/sp.result @@ -5805,4 +5805,169 @@ func_8407_b() 1500 drop function func_8407_a| drop function func_8407_b| +drop table if exists table_26503| +drop procedure if exists proc_26503_ok_1| +drop procedure if exists proc_26503_ok_2| +drop procedure if exists proc_26503_ok_3| +drop procedure if exists proc_26503_ok_4| +create table table_26503(a int unique)| +create procedure proc_26503_ok_1(v int) +begin +declare i int default 5; +declare continue handler for sqlexception +begin +select 'caught something'; +retry: +while i > 0 do +begin +set i = i - 1; +select 'looping', i; +iterate retry; +select 'dead code'; +end; +end while retry; +select 'leaving handler'; +end; +select 'do something'; +insert into table_26503 values (v); +select 'do something again'; +insert into table_26503 values (v); +end| +create procedure proc_26503_ok_2(v int) +begin +declare i int default 5; +declare continue handler for sqlexception +begin +select 'caught something'; +retry: +while i > 0 do +begin +set i = i - 1; +select 'looping', i; +leave retry; +select 'dead code'; +end; +end while; +select 'leaving handler'; +end; +select 'do something'; +insert into table_26503 values (v); +select 'do something again'; +insert into table_26503 values (v); +end| +create procedure proc_26503_ok_3(v int) +begin +declare i int default 5; +retry: +begin +declare continue handler for sqlexception +begin +select 'caught something'; +retry: +while i > 0 do +begin +set i = i - 1; +select 'looping', i; +iterate retry; +select 'dead code'; +end; +end while retry; +select 'leaving handler'; +end; +select 'do something'; +insert into table_26503 values (v); +select 'do something again'; +insert into table_26503 values (v); +end; +end| +create procedure proc_26503_ok_4(v int) +begin +declare i int default 5; +retry: +begin +declare continue handler for sqlexception +begin +select 'caught something'; +retry: +while i > 0 do +begin +set i = i - 1; +select 'looping', i; +leave retry; +select 'dead code'; +end; +end while; +select 'leaving handler'; +end; +select 'do something'; +insert into table_26503 values (v); +select 'do something again'; +insert into table_26503 values (v); +end; +end| +call proc_26503_ok_1(1)| +do something +do something +do something again +do something again +caught something +caught something +looping i +looping 4 +looping i +looping 3 +looping i +looping 2 +looping i +looping 1 +looping i +looping 0 +leaving handler +leaving handler +call proc_26503_ok_2(2)| +do something +do something +do something again +do something again +caught something +caught something +looping i +looping 4 +leaving handler +leaving handler +call proc_26503_ok_3(3)| +do something +do something +do something again +do something again +caught something +caught something +looping i +looping 4 +looping i +looping 3 +looping i +looping 2 +looping i +looping 1 +looping i +looping 0 +leaving handler +leaving handler +call proc_26503_ok_4(4)| +do something +do something +do something again +do something again +caught something +caught something +looping i +looping 4 +leaving handler +leaving handler +drop table table_26503| +drop procedure proc_26503_ok_1| +drop procedure proc_26503_ok_2| +drop procedure proc_26503_ok_3| +drop procedure proc_26503_ok_4| drop table t1,t2; diff --git a/mysql-test/t/sp-error.test b/mysql-test/t/sp-error.test index 396c1552e37..9e5c795d586 100644 --- a/mysql-test/t/sp-error.test +++ b/mysql-test/t/sp-error.test @@ -2021,6 +2021,74 @@ drop procedure proc_25345; drop function func_25345; drop function func_25345_b; +# +# Bug#26503 (Illegal SQL exception handler code causes the server to crash) +# + +delimiter //; + +--error ER_SP_LILABEL_MISMATCH +create procedure proc_26503_error_1() +begin +retry: + repeat + begin + declare continue handler for sqlexception + begin + iterate retry; + end + + select "do something"; + end + until true end repeat retry; +end// + +--error ER_SP_LILABEL_MISMATCH +create procedure proc_26503_error_2() +begin +retry: + repeat + begin + declare continue handler for sqlexception + iterate retry; + + select "do something"; + end + until true end repeat retry; +end// + +--error ER_SP_LILABEL_MISMATCH +create procedure proc_26503_error_3() +begin +retry: + repeat + begin + declare continue handler for sqlexception + begin + leave retry; + end + + select "do something"; + end + until true end repeat retry; +end// + +--error ER_SP_LILABEL_MISMATCH +create procedure proc_26503_error_4() +begin +retry: + repeat + begin + declare continue handler for sqlexception + leave retry; + + select "do something"; + end + until true end repeat retry; +end// + +delimiter ;// + # # BUG#NNNN: New bug synopsis # diff --git a/mysql-test/t/sp.test b/mysql-test/t/sp.test index 80fb1354b16..48e266bfbf6 100644 --- a/mysql-test/t/sp.test +++ b/mysql-test/t/sp.test @@ -6800,6 +6800,141 @@ select func_8407_b()| drop function func_8407_a| drop function func_8407_b| +# +# Bug#26503 (Illegal SQL exception handler code causes the server to crash) +# + +--disable_warnings +drop table if exists table_26503| +drop procedure if exists proc_26503_ok_1| +drop procedure if exists proc_26503_ok_2| +drop procedure if exists proc_26503_ok_3| +drop procedure if exists proc_26503_ok_4| +--enable_warnings + +create table table_26503(a int unique)| + +create procedure proc_26503_ok_1(v int) +begin + declare i int default 5; + + declare continue handler for sqlexception + begin + select 'caught something'; + retry: + while i > 0 do + begin + set i = i - 1; + select 'looping', i; + iterate retry; + select 'dead code'; + end; + end while retry; + select 'leaving handler'; + end; + + select 'do something'; + insert into table_26503 values (v); + select 'do something again'; + insert into table_26503 values (v); +end| + +create procedure proc_26503_ok_2(v int) +begin + declare i int default 5; + + declare continue handler for sqlexception + begin + select 'caught something'; + retry: + while i > 0 do + begin + set i = i - 1; + select 'looping', i; + leave retry; + select 'dead code'; + end; + end while; + select 'leaving handler'; + end; + + select 'do something'; + insert into table_26503 values (v); + select 'do something again'; + insert into table_26503 values (v); +end| + +## The outer retry label should not prevent using the inner label. + +create procedure proc_26503_ok_3(v int) +begin + declare i int default 5; + +retry: + begin + declare continue handler for sqlexception + begin + select 'caught something'; + retry: + while i > 0 do + begin + set i = i - 1; + select 'looping', i; + iterate retry; + select 'dead code'; + end; + end while retry; + select 'leaving handler'; + end; + + select 'do something'; + insert into table_26503 values (v); + select 'do something again'; + insert into table_26503 values (v); + end; +end| + +## The outer retry label should not prevent using the inner label. + +create procedure proc_26503_ok_4(v int) +begin + declare i int default 5; + +retry: + begin + declare continue handler for sqlexception + begin + select 'caught something'; + retry: + while i > 0 do + begin + set i = i - 1; + select 'looping', i; + leave retry; + select 'dead code'; + end; + end while; + select 'leaving handler'; + end; + + select 'do something'; + insert into table_26503 values (v); + select 'do something again'; + insert into table_26503 values (v); + end; +end| + +call proc_26503_ok_1(1)| +call proc_26503_ok_2(2)| +call proc_26503_ok_3(3)| +call proc_26503_ok_4(4)| + +drop table table_26503| +drop procedure proc_26503_ok_1| +drop procedure proc_26503_ok_2| +drop procedure proc_26503_ok_3| +drop procedure proc_26503_ok_4| + # # NOTE: The delimiter is `|`, and not `;`. It is changed to `;` # at the end of the file! diff --git a/sql/sp_head.cc b/sql/sp_head.cc index c1643f0f82e..63ee37e1135 100644 --- a/sql/sp_head.cc +++ b/sql/sp_head.cc @@ -470,7 +470,7 @@ sp_head::init(LEX *lex) { DBUG_ENTER("sp_head::init"); - lex->spcont= m_pcont= new sp_pcontext(NULL); + lex->spcont= m_pcont= new sp_pcontext(); /* Altough trg_table_fields list is used only in triggers we init for all diff --git a/sql/sp_head.h b/sql/sp_head.h index 4ef4077cc79..901b7a19c39 100644 --- a/sql/sp_head.h +++ b/sql/sp_head.h @@ -107,8 +107,6 @@ public: /* Possible values of m_flags */ enum { HAS_RETURN= 1, // For FUNCTIONs only: is set if has RETURN - IN_SIMPLE_CASE= 2, // Is set if parsing a simple CASE - IN_HANDLER= 4, // Is set if the parser is in a handler body MULTI_RESULTS= 8, // Is set if a procedure with SELECT(s) CONTAINS_DYNAMIC_SQL= 16, // Is set if a procedure with PREPARE/EXECUTE IS_INVOKED= 32, // Is set if this sp_head is being used diff --git a/sql/sp_pcontext.cc b/sql/sp_pcontext.cc index 6229cf14604..780243cc79f 100644 --- a/sql/sp_pcontext.cc +++ b/sql/sp_pcontext.cc @@ -25,6 +25,11 @@ #include "sp_pcontext.h" #include "sp_head.h" +/* Initial size for the dynamic arrays in sp_pcontext */ +#define PCONTEXT_ARRAY_INIT_ALLOC 16 +/* Increment size for the dynamic arrays in sp_pcontext */ +#define PCONTEXT_ARRAY_INCREMENT_ALLOC 8 + /* Sanity check for SQLSTATEs. Will not check if it's really an existing state (there are just too many), but will check length and bad characters. @@ -49,28 +54,61 @@ sp_cond_check(LEX_STRING *sqlstate) return TRUE; } -sp_pcontext::sp_pcontext(sp_pcontext *prev) - :Sql_alloc(), m_max_var_index(0), m_max_cursor_index(0), m_max_handler_index(0), - m_context_handlers(0), m_parent(prev), m_pboundary(0) +sp_pcontext::sp_pcontext() + : Sql_alloc(), + m_max_var_index(0), m_max_cursor_index(0), m_max_handler_index(0), + m_context_handlers(0), m_parent(NULL), m_pboundary(0), + m_label_scope(LABEL_DEFAULT_SCOPE) { - VOID(my_init_dynamic_array(&m_vars, sizeof(sp_variable_t *), 16, 8)); - VOID(my_init_dynamic_array(&m_case_expr_id_lst, sizeof(int), 16, 8)); - VOID(my_init_dynamic_array(&m_conds, sizeof(sp_cond_type_t *), 16, 8)); - VOID(my_init_dynamic_array(&m_cursors, sizeof(LEX_STRING), 16, 8)); - VOID(my_init_dynamic_array(&m_handlers, sizeof(sp_cond_type_t *), 16, 8)); + VOID(my_init_dynamic_array(&m_vars, sizeof(sp_variable_t *), + PCONTEXT_ARRAY_INIT_ALLOC, + PCONTEXT_ARRAY_INCREMENT_ALLOC)); + VOID(my_init_dynamic_array(&m_case_expr_id_lst, sizeof(int), + PCONTEXT_ARRAY_INIT_ALLOC, + PCONTEXT_ARRAY_INCREMENT_ALLOC)); + VOID(my_init_dynamic_array(&m_conds, sizeof(sp_cond_type_t *), + PCONTEXT_ARRAY_INIT_ALLOC, + PCONTEXT_ARRAY_INCREMENT_ALLOC)); + VOID(my_init_dynamic_array(&m_cursors, sizeof(LEX_STRING), + PCONTEXT_ARRAY_INIT_ALLOC, + PCONTEXT_ARRAY_INCREMENT_ALLOC)); + VOID(my_init_dynamic_array(&m_handlers, sizeof(sp_cond_type_t *), + PCONTEXT_ARRAY_INIT_ALLOC, + PCONTEXT_ARRAY_INCREMENT_ALLOC)); m_label.empty(); m_children.empty(); - if (!prev) - { - m_var_offset= m_cursor_offset= 0; - m_num_case_exprs= 0; - } - else - { - m_var_offset= prev->m_var_offset + prev->m_max_var_index; - m_cursor_offset= prev->current_cursor_count(); - m_num_case_exprs= prev->get_num_case_exprs(); - } + + m_var_offset= m_cursor_offset= 0; + m_num_case_exprs= 0; +} + +sp_pcontext::sp_pcontext(sp_pcontext *prev, label_scope_type label_scope) + : Sql_alloc(), + m_max_var_index(0), m_max_cursor_index(0), m_max_handler_index(0), + m_context_handlers(0), m_parent(prev), m_pboundary(0), + m_label_scope(label_scope) +{ + VOID(my_init_dynamic_array(&m_vars, sizeof(sp_variable_t *), + PCONTEXT_ARRAY_INIT_ALLOC, + PCONTEXT_ARRAY_INCREMENT_ALLOC)); + VOID(my_init_dynamic_array(&m_case_expr_id_lst, sizeof(int), + PCONTEXT_ARRAY_INIT_ALLOC, + PCONTEXT_ARRAY_INCREMENT_ALLOC)); + VOID(my_init_dynamic_array(&m_conds, sizeof(sp_cond_type_t *), + PCONTEXT_ARRAY_INIT_ALLOC, + PCONTEXT_ARRAY_INCREMENT_ALLOC)); + VOID(my_init_dynamic_array(&m_cursors, sizeof(LEX_STRING), + PCONTEXT_ARRAY_INIT_ALLOC, + PCONTEXT_ARRAY_INCREMENT_ALLOC)); + VOID(my_init_dynamic_array(&m_handlers, sizeof(sp_cond_type_t *), + PCONTEXT_ARRAY_INIT_ALLOC, + PCONTEXT_ARRAY_INCREMENT_ALLOC)); + m_label.empty(); + m_children.empty(); + + m_var_offset= prev->m_var_offset + prev->m_max_var_index; + m_cursor_offset= prev->current_cursor_count(); + m_num_case_exprs= prev->get_num_case_exprs(); } void @@ -92,9 +130,9 @@ sp_pcontext::destroy() } sp_pcontext * -sp_pcontext::push_context() +sp_pcontext::push_context(label_scope_type label_scope) { - sp_pcontext *child= new sp_pcontext(this); + sp_pcontext *child= new sp_pcontext(this, label_scope); if (child) m_children.push_back(child); @@ -257,7 +295,15 @@ sp_pcontext::find_label(char *name) if (my_strcasecmp(system_charset_info, name, lab->name) == 0) return lab; - if (m_parent) + /* + Note about exception handlers. + See SQL:2003 SQL/PSM (ISO/IEC 9075-4:2003), + section 13.1 , + syntax rule 4. + In short, a DECLARE HANDLER block can not refer + to labels from the parent context, as they are out of scope. + */ + if (m_parent && (m_label_scope == LABEL_DEFAULT_SCOPE)) return m_parent->find_label(name); return NULL; } diff --git a/sql/sp_pcontext.h b/sql/sp_pcontext.h index b2cdd5e689c..5bffda79f98 100644 --- a/sql/sp_pcontext.h +++ b/sql/sp_pcontext.h @@ -88,16 +88,33 @@ typedef struct sp_cond sp_cond_type_t *val; } sp_cond_t; +/** + The scope of a label in Stored Procedures, + for name resolution of labels in a parsing context. +*/ +enum label_scope_type +{ + /** + The labels declared in a parent context are in scope. + */ + LABEL_DEFAULT_SCOPE, + /** + The labels declared in a parent context are not in scope. + */ + LABEL_HANDLER_SCOPE +}; -/* - The parse-time context, used to keep track on declared variables/parameters, +/** + The parse-time context, used to keep track of declared variables/parameters, conditions, handlers, cursors and labels, during parsing. sp_contexts are organized as a tree, with one object for each begin-end - block, plus a root-context for the parameters. + block, one object for each exception handler, + plus a root-context for the parameters. This is used during parsing for looking up defined names (e.g. declared variables and visible labels), for error checking, and to calculate offsets to be used at runtime. (During execution variable values, active handlers and cursors, etc, are referred to by an index in a stack.) + Parsing contexts for exception handlers limit the visibility of labels. The pcontext tree is also kept during execution and is used for error checking (e.g. correct number of parameters), and in the future, used by the debugger. @@ -105,21 +122,30 @@ typedef struct sp_cond class sp_pcontext : public Sql_alloc { - sp_pcontext(const sp_pcontext &); /* Prevent use of these */ - void operator=(sp_pcontext &); +public: - public: - - sp_pcontext(sp_pcontext *prev); + /** + Constructor. + Builds a parsing context root node. + */ + sp_pcontext(); // Free memory void destroy(); + /** + Create and push a new context in the tree. + @param label_scope label scope for the new parsing context + @return the node created + */ sp_pcontext * - push_context(); + push_context(label_scope_type label_scope); - // Returns the previous context, not the one we pop + /** + Pop a node from the parsing context tree. + @return the parent node + */ sp_pcontext * pop_context(); @@ -363,6 +389,13 @@ class sp_pcontext : public Sql_alloc protected: + /** + Constructor for a tree node. + @param prev the parent parsing context + @param label_scope label_scope for this parsing context + */ + sp_pcontext(sp_pcontext *prev, label_scope_type label_scope); + /* m_max_var_index -- number of variables (including all types of arguments) in this context including all children contexts. @@ -416,6 +449,14 @@ private: List m_children; // Children contexts, used for destruction + /** + Scope of labels for this parsing context. + */ + label_scope_type m_label_scope; + +private: + sp_pcontext(const sp_pcontext &); /* Prevent use of these */ + void operator=(sp_pcontext &); }; // class sp_pcontext : public Sql_alloc diff --git a/sql/sql_yacc.yy b/sql/sql_yacc.yy index d07234ff2bd..812482e8ffb 100644 --- a/sql/sql_yacc.yy +++ b/sql/sql_yacc.yy @@ -2006,6 +2006,9 @@ sp_decl: { LEX *lex= Lex; sp_head *sp= lex->sphead; + + lex->spcont= lex->spcont->push_context(LABEL_HANDLER_SCOPE); + sp_pcontext *ctx= lex->spcont; sp_instr_hpush_jump *i= new sp_instr_hpush_jump(sp->instructions(), ctx, $2, @@ -2013,7 +2016,6 @@ sp_decl: sp->add_instr(i); sp->push_backpatch(i, ctx->push_label((char *)"", 0)); - sp->m_flags|= sp_head::IN_HANDLER; } sp_hcond_list sp_proc_stmt { @@ -2037,10 +2039,12 @@ sp_decl: sp->push_backpatch(i, lex->spcont->last_label()); /* Block end */ } lex->sphead->backpatch(hlab); - sp->m_flags&= ~sp_head::IN_HANDLER; + + lex->spcont= ctx->pop_context(); + $$.vars= $$.conds= $$.curs= 0; $$.hndlrs= $6; - ctx->add_handlers($6); + lex->spcont->add_handlers($6); } | DECLARE_SYM ident CURSOR_SYM FOR_SYM sp_cursor_stmt { @@ -2103,11 +2107,18 @@ sp_handler_type: ; sp_hcond_list: + sp_hcond_element + { $$= 1; } + | sp_hcond_list ',' sp_hcond_element + { $$+= 1; } + ; + +sp_hcond_element: sp_hcond { LEX *lex= Lex; sp_head *sp= lex->sphead; - sp_pcontext *ctx= lex->spcont; + sp_pcontext *ctx= lex->spcont->parent_context(); if (ctx->find_handler($1)) { @@ -2121,28 +2132,6 @@ sp_hcond_list: i->add_condition($1); ctx->push_handler($1); - $$= 1; - } - } - | sp_hcond_list ',' sp_hcond - { - LEX *lex= Lex; - sp_head *sp= lex->sphead; - sp_pcontext *ctx= lex->spcont; - - if (ctx->find_handler($3)) - { - my_message(ER_SP_DUP_HANDLER, ER(ER_SP_DUP_HANDLER), MYF(0)); - MYSQL_YYABORT; - } - else - { - sp_instr_hpush_jump *i= - (sp_instr_hpush_jump *)sp->last_instruction(); - - i->add_condition($3); - ctx->push_handler($3); - $$= $1 + 1; } } ; @@ -2687,7 +2676,7 @@ sp_unlabeled_control: sp_label_t *lab= lex->spcont->last_label(); lab->type= SP_LAB_BEGIN; - lex->spcont= lex->spcont->push_context(); + lex->spcont= lex->spcont->push_context(LABEL_DEFAULT_SCOPE); } sp_decls sp_proc_stmts