From 1ac26b4a6cfd5cf72b39e4fda31d2bb114463ad8 Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 7 Sep 2005 10:30:06 -0700 Subject: [PATCH 1/4] Small tweak for random read performance (we shouldn't always tell the OS that we will be doing a linear scan). sql/examples/ha_tina.cc: Small tweak for random read performance. --- sql/examples/ha_tina.cc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sql/examples/ha_tina.cc b/sql/examples/ha_tina.cc index bbcdfb0dafb..8a9aa91c680 100644 --- a/sql/examples/ha_tina.cc +++ b/sql/examples/ha_tina.cc @@ -609,7 +609,8 @@ int ha_tina::rnd_init(bool scan) records= 0; chain_ptr= chain; #ifdef HAVE_MADVISE - (void)madvise(share->mapped_file,share->file_stat.st_size,MADV_SEQUENTIAL); + if (scan) + (void)madvise(share->mapped_file,share->file_stat.st_size,MADV_SEQUENTIAL); #endif DBUG_RETURN(0); From 792221e36579dbbec0f4335b49198a25d2a2ffd0 Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 7 Sep 2005 22:38:36 +0400 Subject: [PATCH 2/4] Fix bug #12922 if(sum(),...) with group from view returns wrong results Fields of view represented by Item_direct_view_ref. When complex expression such as if(sum()>...,...) is splited in simpler parts by refs was ignored. Beside this direct ref doesn't use it's result_field and thus can't store it's result in tmp table which is needed for sum() ... group. All this results in reported bug. Item::split_sum_func2() now converts Item_direct_view_ref to Item_ref to make fields from view being storable in tmp table. sql/item.h: Fix bug #12922 if(sum(),...) with group from view returns wrong results Added function ref_type() to distinguish Item_ref subclasses sql/item.cc: Fix bug #12922 if(sum(),...) with group from view returns wrong results Item::split_sum_func2() now converts Item_direct_view_ref to Item_ref to make fields from view being storable in tmp table. mysql-test/t/view.test: Test case for bug#12922 if(sum(),...) with group from view returns wrong results mysql-test/r/view.result: Test case for bug#12922 if(sum(),...) with group from view returns wrong results --- mysql-test/r/view.result | 9 +++++++++ mysql-test/t/view.test | 10 ++++++++++ sql/item.cc | 16 +++++++++------- sql/item.h | 4 ++++ 4 files changed, 32 insertions(+), 7 deletions(-) diff --git a/mysql-test/r/view.result b/mysql-test/r/view.result index a544fb4b020..88c635509f5 100644 --- a/mysql-test/r/view.result +++ b/mysql-test/r/view.result @@ -2151,3 +2151,12 @@ select * from v1; strcmp(f1,'a') drop view v1; drop table t1; +create table t1 (f1 int, f2 int,f3 int); +insert into t1 values (1,10,20),(2,0,0); +create view v1 as select * from t1; +select if(sum(f1)>1,f2,f3) from v1 group by f1; +if(sum(f1)>1,f2,f3) +20 +0 +drop view v1; +drop table t1; diff --git a/mysql-test/t/view.test b/mysql-test/t/view.test index 97625632618..0e6fddf58ae 100644 --- a/mysql-test/t/view.test +++ b/mysql-test/t/view.test @@ -2018,3 +2018,13 @@ create view v1 as select strcmp(f1,'a') from t1; select * from v1; drop view v1; drop table t1; + +# +# Bug #12922 if(sum(),...) with group from view returns wrong results +# +create table t1 (f1 int, f2 int,f3 int); +insert into t1 values (1,10,20),(2,0,0); +create view v1 as select * from t1; +select if(sum(f1)>1,f2,f3) from v1 group by f1; +drop view v1; +drop table t1; diff --git a/sql/item.cc b/sql/item.cc index b4992faa65e..1edf0a470d3 100644 --- a/sql/item.cc +++ b/sql/item.cc @@ -1022,9 +1022,9 @@ void Item::split_sum_func2(THD *thd, Item **ref_pointer_array, /* Will split complicated items and ignore simple ones */ split_sum_func(thd, ref_pointer_array, fields); } - else if ((type() == SUM_FUNC_ITEM || - (used_tables() & ~PARAM_TABLE_BIT)) && - type() != REF_ITEM) + else if ((type() == SUM_FUNC_ITEM || (used_tables() & ~PARAM_TABLE_BIT)) && + (type() != REF_ITEM || + ((Item_ref*)this)->ref_type() == Item_ref::VIEW_REF)) { /* Replace item with a reference so that we can easily calculate @@ -1033,15 +1033,17 @@ void Item::split_sum_func2(THD *thd, Item **ref_pointer_array, The test above is to ensure we don't do a reference for things that are constants (PARAM_TABLE_BIT is in effect a constant) or already referenced (for example an item in HAVING) + Exception is Item_direct_view_ref which we need to convert to + Item_ref to allow fields from view being stored in tmp table. */ uint el= fields.elements; - Item *new_item; - ref_pointer_array[el]= this; + Item *new_item, *real_item= real_item(); + + ref_pointer_array[el]= real_item; if (!(new_item= new Item_ref(&thd->lex->current_select->context, ref_pointer_array + el, 0, name))) return; // fatal_error is set - fields.push_front(this); - ref_pointer_array[el]= this; + fields.push_front(real_item); thd->change_item_tree(ref, new_item); } } diff --git a/sql/item.h b/sql/item.h index b934e1f9f3f..f128c72413d 100644 --- a/sql/item.h +++ b/sql/item.h @@ -1537,6 +1537,7 @@ class Item_ref :public Item_ident protected: void set_properties(); public: + enum Ref_Type { REF, DIRECT_REF, VIEW_REF }; Field *result_field; /* Save result here */ Item **ref; Item_ref(Name_resolution_context *context_arg, @@ -1617,6 +1618,7 @@ public: void cleanup(); Item_field *filed_for_view_update() { return (*ref)->filed_for_view_update(); } + virtual Ref_Type ref_type() { return REF; } }; @@ -1641,6 +1643,7 @@ public: bool val_bool(); bool is_null(); bool get_date(TIME *ltime,uint fuzzydate); + virtual Ref_Type ref_type() { return DIRECT_REF; } }; /* @@ -1660,6 +1663,7 @@ public: bool fix_fields(THD *, Item **); bool eq(const Item *item, bool binary_cmp) const; + virtual Ref_Type ref_type() { return VIEW_REF; } }; From eb061b654fb2a2aaef09e2bf8e7b3a8f300447be Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 7 Sep 2005 23:03:56 +0400 Subject: [PATCH 3/4] Fix for Bug#12995: Inside function "Table 't4' was not locked with LOCK TABLES" Any form of HANDLER statement is forbidden from usage in stored procedures/functions. mysql-test/r/sp-error.result: Results for the test case for Bug#12995 added. mysql-test/t/sp-error.test: Test case for Bug#12995 added. sql/sql_yacc.yy: Forbid any form of "HANDLER" statement from use in stored procedures/functions. --- mysql-test/r/sp-error.result | 21 +++++++++++++++++++++ mysql-test/t/sp-error.test | 30 ++++++++++++++++++++++++++++++ sql/sql_yacc.yy | 15 +++++++++++++++ 3 files changed, 66 insertions(+) diff --git a/mysql-test/r/sp-error.result b/mysql-test/r/sp-error.result index 1a061529fb0..09d829e9d12 100644 --- a/mysql-test/r/sp-error.result +++ b/mysql-test/r/sp-error.result @@ -765,3 +765,24 @@ OPTIMIZE TABLE t1; RETURN 1; END| ERROR 0A000: OPTIMIZE TABLE is not allowed in stored procedures +DROP FUNCTION IF EXISTS bug12995| +CREATE FUNCTION bug12995() RETURNS INT +BEGIN +HANDLER t1 OPEN; +RETURN 1; +END| +ERROR 0A000: HANDLER is not allowed in stored procedures +CREATE FUNCTION bug12995() RETURNS INT +BEGIN +HANDLER t1 READ FIRST; +RETURN 1; +END| +ERROR 0A000: HANDLER is not allowed in stored procedures +CREATE FUNCTION bug12995() RETURNS INT +BEGIN +HANDLER t1 CLOSE; +RETURN 1; +END| +ERROR 0A000: HANDLER is not allowed in stored procedures +SELECT bug12995()| +ERROR 42000: FUNCTION test.bug12995 does not exist diff --git a/mysql-test/t/sp-error.test b/mysql-test/t/sp-error.test index abb927ab3b8..9f91c32c104 100644 --- a/mysql-test/t/sp-error.test +++ b/mysql-test/t/sp-error.test @@ -1099,6 +1099,36 @@ BEGIN OPTIMIZE TABLE t1; RETURN 1; END| +delimiter ;| + +# +# Bug##12995 "Inside function "Table 't4' was not locked with LOCK TABLES" +# +delimiter |; +--disable_warnings +DROP FUNCTION IF EXISTS bug12995| +--enable_warnings +--error ER_SP_BADSTATEMENT +CREATE FUNCTION bug12995() RETURNS INT +BEGIN + HANDLER t1 OPEN; + RETURN 1; +END| +--error ER_SP_BADSTATEMENT +CREATE FUNCTION bug12995() RETURNS INT +BEGIN + HANDLER t1 READ FIRST; + RETURN 1; +END| +--error ER_SP_BADSTATEMENT +CREATE FUNCTION bug12995() RETURNS INT +BEGIN + HANDLER t1 CLOSE; + RETURN 1; +END| +--error 1305 +SELECT bug12995()| +delimiter ;| # # BUG#NNNN: New bug synopsis diff --git a/sql/sql_yacc.yy b/sql/sql_yacc.yy index cc286e4ff45..71adab4853c 100644 --- a/sql/sql_yacc.yy +++ b/sql/sql_yacc.yy @@ -8157,6 +8157,11 @@ handler: HANDLER_SYM table_ident OPEN_SYM opt_table_alias { LEX *lex= Lex; + if (lex->sphead) + { + my_error(ER_SP_BADSTATEMENT, MYF(0), "HANDLER"); + YYABORT; + } lex->sql_command = SQLCOM_HA_OPEN; if (!lex->current_select->add_table_to_list(lex->thd, $2, $4, 0)) YYABORT; @@ -8164,6 +8169,11 @@ handler: | HANDLER_SYM table_ident_nodb CLOSE_SYM { LEX *lex= Lex; + if (lex->sphead) + { + my_error(ER_SP_BADSTATEMENT, MYF(0), "HANDLER"); + YYABORT; + } lex->sql_command = SQLCOM_HA_CLOSE; if (!lex->current_select->add_table_to_list(lex->thd, $2, 0, 0)) YYABORT; @@ -8171,6 +8181,11 @@ handler: | HANDLER_SYM table_ident_nodb READ_SYM { LEX *lex=Lex; + if (lex->sphead) + { + my_error(ER_SP_BADSTATEMENT, MYF(0), "HANDLER"); + YYABORT; + } lex->sql_command = SQLCOM_HA_READ; lex->ha_rkey_mode= HA_READ_KEY_EXACT; /* Avoid purify warnings */ lex->current_select->select_limit= new Item_int((int32) 1); From d319c8ffbd95d885e3f64a3ab29c397b57be272d Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 7 Sep 2005 23:32:39 +0400 Subject: [PATCH 4/4] item.cc: Fix for fix bug#12922. sql/item.cc: Fix for fix bug#12922. --- sql/item.cc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/sql/item.cc b/sql/item.cc index 1edf0a470d3..56b03055968 100644 --- a/sql/item.cc +++ b/sql/item.cc @@ -1037,13 +1037,13 @@ void Item::split_sum_func2(THD *thd, Item **ref_pointer_array, Item_ref to allow fields from view being stored in tmp table. */ uint el= fields.elements; - Item *new_item, *real_item= real_item(); + Item *new_item, *real_itm= real_item(); - ref_pointer_array[el]= real_item; + ref_pointer_array[el]= real_itm; if (!(new_item= new Item_ref(&thd->lex->current_select->context, ref_pointer_array + el, 0, name))) return; // fatal_error is set - fields.push_front(real_item); + fields.push_front(real_itm); thd->change_item_tree(ref, new_item); } }