From f985e787758e3081e18543b5490724d30296aebf Mon Sep 17 00:00:00 2001 From: Kristofer Pettersson Date: Thu, 9 Oct 2008 09:26:42 +0200 Subject: [PATCH 1/4] Bug#24289 Status Variable "Questions" gets wrong values with Stored Routines When running Stored Routines the Status Variable "Questions" was wrongly incremented. According to the manual it should contain the "number of statements that clients have sent to the server" Introduced a new status variable 'questions' to replace the query_id variable which currently corresponds badly with the number of statements sent by the client. The new behavior is ment to be backward compatible with 4.0 and at the same time work with new features in a similar way. This is a backport from 6.0 mysql-test/r/status2.result: Added test case mysql-test/t/status2.test: Added test case sql/mysqld.cc: Introduced a new status variable 'questions' to replace the query_id variable which currently corresponds badly with the number of statements sent by the client. sql/sql_class.h: Introduced a new status variable 'questions' to replace the query_id variable which currently corresponds badly with the number of statements sent by the client. sql/sql_parse.cc: To be backward compatible with 4.0 and at the same time extend the interpretation of the Question variable, it should be increased on all COM-commands but COM_STATISTICS, COM_PING, COM_STMT_PREPARE, COM_STMT_CLOSE and COM_STMT_RESET. Since COM_QUERY can process multiple statements, there has to be an extra increase there as well. sql/sql_show.cc: Removed deprecated SHOW_QUESTION status code. sql/structs.h: Removed deprecated SHOW_QUESTION status code. --- mysql-test/r/status2.result | 64 +++++++++++++++++++++++++++++++++++++ mysql-test/t/status2.test | 64 +++++++++++++++++++++++++++++++++++++ sql/mysqld.cc | 4 ++- sql/sql_class.h | 13 ++++++-- sql/sql_parse.cc | 23 ++++++++++++- sql/sql_show.cc | 3 -- sql/structs.h | 2 +- 7 files changed, 164 insertions(+), 9 deletions(-) create mode 100644 mysql-test/r/status2.result create mode 100644 mysql-test/t/status2.test diff --git a/mysql-test/r/status2.result b/mysql-test/r/status2.result new file mode 100644 index 00000000000..7f6eab693e5 --- /dev/null +++ b/mysql-test/r/status2.result @@ -0,0 +1,64 @@ +# +# Bug#24289 Status Variable "Questions" gets wrong values with Stored Routines +# +FLUSH STATUS; +DROP TABLE IF EXISTS t1,t2; +DROP PROCEDURE IF EXISTS p1; +DROP FUNCTION IF EXISTS f1; +CREATE FUNCTION f1() RETURNS INTEGER +BEGIN +DECLARE foo INTEGER; +DECLARE bar INTEGER; +SET foo=1; +SET bar=2; +RETURN foo; +END $$ +CREATE PROCEDURE p1() +BEGIN +SELECT 1; +END $$ +CREATE TABLE t1 (c1 INT); +CREATE TABLE t2 (c1 INT); +INSERT INTO t1 VALUES (1); +Assert Questions == 9 +SHOW STATUS LIKE 'Questions'; +Variable_name Value +Questions 9 +SELECT f1(); +f1() +1 +Assert Questions == 11 +SHOW STATUS LIKE 'Questions'; +Variable_name Value +Questions 11 +CALL p1(); +1 +1 +Assert Questions == 13 +SHOW STATUS LIKE 'Questions'; +Variable_name Value +Questions 13 +SELECT 1; +1 +1 +Assert Questions == 15 +SHOW STATUS LIKE 'Questions'; +Variable_name Value +Questions 15 +FLUSH STATUS; +SELECT 1; +1 +1 +Assert Questions == 16 +SHOW STATUS LIKE 'Questions'; +Variable_name Value +Questions 16 +Global status updated; Assert diff == 5 +FLUSH STATUS; +SELECT 5; +5 +5 +DROP TABLE t1,t2; +DROP PROCEDURE p1; +DROP FUNCTION f1; +End of 6.0 tests diff --git a/mysql-test/t/status2.test b/mysql-test/t/status2.test new file mode 100644 index 00000000000..b834cd0c6ad --- /dev/null +++ b/mysql-test/t/status2.test @@ -0,0 +1,64 @@ +--echo # +--echo # Bug#24289 Status Variable "Questions" gets wrong values with Stored Routines +--echo # +# The bogus connection below is needed to make the gobal statement count +# deterministic when the test is run for the first time. +connect (con1,localhost,root,,); +connection con1; +connection default; +disconnect con1; +FLUSH STATUS; +--disable_warnings +DROP TABLE IF EXISTS t1,t2; +DROP PROCEDURE IF EXISTS p1; +DROP FUNCTION IF EXISTS f1; +--enable_warnings +DELIMITER $$; +CREATE FUNCTION f1() RETURNS INTEGER +BEGIN + DECLARE foo INTEGER; + DECLARE bar INTEGER; + SET foo=1; + SET bar=2; + RETURN foo; +END $$ +CREATE PROCEDURE p1() +BEGIN + SELECT 1; +END $$ +DELIMITER ;$$ +CREATE TABLE t1 (c1 INT); +CREATE TABLE t2 (c1 INT); +INSERT INTO t1 VALUES (1); +--echo Assert Questions == 9 +SHOW STATUS LIKE 'Questions'; +SELECT f1(); +--echo Assert Questions == 11 +SHOW STATUS LIKE 'Questions'; +CALL p1(); +--echo Assert Questions == 13 +SHOW STATUS LIKE 'Questions'; +SELECT 1; +--echo Assert Questions == 15 +SHOW STATUS LIKE 'Questions'; +connect (con1,localhost,root,,); +connection con1; +FLUSH STATUS; +let $org_questions= `SHOW GLOBAL STATUS LIKE 'questions'`; +SELECT 1; +connection default; +disconnect con1; +--echo Assert Questions == 16 +SHOW STATUS LIKE 'Questions'; +--echo Global status updated; Assert diff == 5 +FLUSH STATUS; +let $new_questions= `SHOW GLOBAL STATUS LIKE 'questions'`; +--disable_log +let $diff= `SELECT SUBSTRING('$new_questions',10)-SUBSTRING('$org_questions',10)`; +--enable_log +eval SELECT $diff; +DROP TABLE t1,t2; +DROP PROCEDURE p1; +DROP FUNCTION f1; +--echo End of 6.0 tests + diff --git a/sql/mysqld.cc b/sql/mysqld.cc index 757427f9fdf..c3e5449b22b 100644 --- a/sql/mysqld.cc +++ b/sql/mysqld.cc @@ -6573,7 +6573,9 @@ struct show_var_st status_vars[]= { {"Qcache_queries_in_cache", (char*) &query_cache.queries_in_cache, SHOW_LONG_CONST}, {"Qcache_total_blocks", (char*) &query_cache.total_blocks, SHOW_LONG_CONST}, #endif /*HAVE_QUERY_CACHE*/ - {"Questions", (char*) 0, SHOW_QUESTION}, + {"Questions", (char*) offsetof(STATUS_VAR, questions), + SHOW_LONG_STATUS}, + {"Rpl_status", (char*) 0, SHOW_RPL_STATUS}, {"Select_full_join", (char*) offsetof(STATUS_VAR, select_full_join_count), SHOW_LONG_STATUS}, {"Select_full_range_join", (char*) offsetof(STATUS_VAR, select_full_range_join_count), SHOW_LONG_STATUS}, diff --git a/sql/sql_class.h b/sql/sql_class.h index a9700c9e91b..9fe0a7423de 100644 --- a/sql/sql_class.h +++ b/sql/sql_class.h @@ -664,10 +664,17 @@ typedef struct system_status_var ulong com_stmt_fetch; ulong com_stmt_reset; ulong com_stmt_close; + /* + Number of statements sent from the client + */ + ulong questions; /* - Status variables which it does not make sense to add to - global status variable counter + IMPORTANT! + SEE last_system_status_var DEFINITION BELOW. + + Below 'last_system_status_var' are all variables which doesn't make any + sense to add to the /global/ status variable counter. */ double last_query_cost; } STATUS_VAR; @@ -678,7 +685,7 @@ typedef struct system_status_var counter */ -#define last_system_status_var com_stmt_close +#define last_system_status_var questions void free_tmp_table(THD *thd, TABLE *entry); diff --git a/sql/sql_parse.cc b/sql/sql_parse.cc index 83b111b7c4c..da1a4a352ae 100644 --- a/sql/sql_parse.cc +++ b/sql/sql_parse.cc @@ -1690,8 +1690,24 @@ bool dispatch_command(enum enum_server_command command, THD *thd, thd->set_time(); VOID(pthread_mutex_lock(&LOCK_thread_count)); thd->query_id= global_query_id; - if (command != COM_STATISTICS && command != COM_PING) + + switch( command ) { + /* Ignore these statements. */ + case COM_STATISTICS: + case COM_PING: + break; + /* Only increase id on these statements but don't count them. */ + case COM_STMT_PREPARE: + case COM_STMT_CLOSE: + case COM_STMT_RESET: next_query_id(); + break; + /* Increase id and count all other statements. */ + default: + statistic_increment(thd->status_var.questions, &LOCK_status); + next_query_id(); + } + thread_running++; /* TODO: set thd->lex->sql_command to SQLCOM_END here */ VOID(pthread_mutex_unlock(&LOCK_thread_count)); @@ -1896,6 +1912,11 @@ bool dispatch_command(enum enum_server_command command, THD *thd, VOID(pthread_mutex_lock(&LOCK_thread_count)); thd->query_length= length; thd->query= next_packet; + /* + Count each statement from the client. + */ + statistic_increment(thd->status_var.questions, &LOCK_status); + thd->query_id= next_query_id(); thd->set_time(); /* Reset the query start time. */ /* TODO: set thd->lex->sql_command to SQLCOM_END here */ diff --git a/sql/sql_show.cc b/sql/sql_show.cc index c30e0a00d95..d6854e00228 100644 --- a/sql/sql_show.cc +++ b/sql/sql_show.cc @@ -1520,9 +1520,6 @@ static bool show_status_array(THD *thd, const char *wild, nr= (long) (thd->query_start() - server_start_time); end= int10_to_str(nr, buff, 10); break; - case SHOW_QUESTION: - end= int10_to_str((long) thd->query_id, buff, 10); - break; #ifdef HAVE_REPLICATION case SHOW_RPL_STATUS: end= strmov(buff, rpl_status_type[(int)rpl_status]); diff --git a/sql/structs.h b/sql/structs.h index bc373fe4b52..ab8537612fa 100644 --- a/sql/structs.h +++ b/sql/structs.h @@ -170,7 +170,7 @@ enum SHOW_TYPE SHOW_UNDEF, SHOW_LONG, SHOW_LONGLONG, SHOW_INT, SHOW_CHAR, SHOW_CHAR_PTR, SHOW_DOUBLE_STATUS, - SHOW_BOOL, SHOW_MY_BOOL, SHOW_OPENTABLES, SHOW_STARTTIME, SHOW_QUESTION, + SHOW_BOOL, SHOW_MY_BOOL, SHOW_OPENTABLES, SHOW_STARTTIME, SHOW_LONG_CONST, SHOW_INT_CONST, SHOW_HAVE, SHOW_SYS, SHOW_HA_ROWS, SHOW_VARS, #ifdef HAVE_OPENSSL From 3ad228d7fba6fa2e5b98569f798583b8f8b90db9 Mon Sep 17 00:00:00 2001 From: Davi Arnaut Date: Wed, 15 Oct 2008 18:34:51 -0300 Subject: [PATCH 2/4] Bug#37075: offset of limit clause might be truncated on 32-bits server w/o big tables The problem is that the offset argument of the limit clause might be truncated on a 32-bits server built without big tables support. The truncation was happening because the original 64-bits long argument was being cast to a 32-bits (ha_rows) offset counter. The solution is to check if the conversing resulted in value truncation and if so, the offset is set to the maximum possible value that can fit on the type. mysql-test/r/limit.result: Add test case result for Bug#37075 mysql-test/t/limit.test: Add test case for Bug#37075 sql/sql_lex.cc: Check for truncation of the offset value. If value was truncated, set to the maximum possible value. --- mysql-test/r/limit.result | 3 +++ mysql-test/t/limit.test | 8 ++++++++ sql/sql_lex.cc | 22 ++++++++++++++++++---- 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/mysql-test/r/limit.result b/mysql-test/r/limit.result index 2acf74162a4..caed588acdb 100644 --- a/mysql-test/r/limit.result +++ b/mysql-test/r/limit.result @@ -111,3 +111,6 @@ set @a=-14632475938453979136; execute s using @a, @a; ERROR HY000: Incorrect arguments to EXECUTE End of 5.0 tests +select 1 as a limit 4294967296,10; +a +End of 5.1 tests diff --git a/mysql-test/t/limit.test b/mysql-test/t/limit.test index 9cccca1adc3..5847b90367a 100644 --- a/mysql-test/t/limit.test +++ b/mysql-test/t/limit.test @@ -95,3 +95,11 @@ set @a=-14632475938453979136; execute s using @a, @a; --echo End of 5.0 tests + +# +# Bug#37075: offset of limit clause might be truncated to 0 on 32-bits server w/o big tables +# + +select 1 as a limit 4294967296,10; + +--echo End of 5.1 tests diff --git a/sql/sql_lex.cc b/sql/sql_lex.cc index ba9c0e93134..71aa80b8170 100644 --- a/sql/sql_lex.cc +++ b/sql/sql_lex.cc @@ -2041,12 +2041,26 @@ st_lex::copy_db_to(char **p_db, uint *p_db_length) const void st_select_lex_unit::set_limit(SELECT_LEX *sl) { ha_rows select_limit_val; + ulonglong val; DBUG_ASSERT(! thd->stmt_arena->is_stmt_prepare()); - select_limit_val= (ha_rows)(sl->select_limit ? sl->select_limit->val_uint() : - HA_POS_ERROR); - offset_limit_cnt= (ha_rows)(sl->offset_limit ? sl->offset_limit->val_uint() : - ULL(0)); + val= sl->select_limit ? sl->select_limit->val_uint() : HA_POS_ERROR; + select_limit_val= (ha_rows)val; +#ifndef BIG_TABLES + /* + Check for overflow : ha_rows can be smaller then ulonglong if + BIG_TABLES is off. + */ + if (val != (ulonglong)select_limit_val) + select_limit_val= HA_POS_ERROR; +#endif + val= sl->offset_limit ? sl->offset_limit->val_uint() : ULL(0); + offset_limit_cnt= (ha_rows)val; +#ifndef BIG_TABLES + /* Check for truncation. */ + if (val != (ulonglong)offset_limit_cnt) + offset_limit_cnt= HA_POS_ERROR; +#endif select_limit_cnt= select_limit_val + offset_limit_cnt; if (select_limit_cnt < select_limit_val) select_limit_cnt= HA_POS_ERROR; // no limit From ca6e05765c7a404377dd84924a816e654b20618d Mon Sep 17 00:00:00 2001 From: Georgi Kodinov Date: Thu, 16 Oct 2008 14:16:27 +0300 Subject: [PATCH 3/4] Bug #39958: Test "windows" lacks a cleanup Added the missing DROP TABLE mysql-test/r/windows.result: Bug #39958: added the missing DROP TABLE mysql-test/t/windows.test: Bug #39958: added the missing DROP TABLE --- mysql-test/r/windows.result | 1 + mysql-test/t/windows.test | 1 + 2 files changed, 2 insertions(+) mode change 100644 => 100755 mysql-test/t/windows.test diff --git a/mysql-test/r/windows.result b/mysql-test/r/windows.result index b5f9a48d805..5a54db8bb84 100644 --- a/mysql-test/r/windows.result +++ b/mysql-test/r/windows.result @@ -18,4 +18,5 @@ EXPLAIN SELECT * FROM t1 WHERE b = (SELECT max(2)); id select_type table type possible_keys key key_len ref rows Extra 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables 2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL No tables used +DROP TABLE t1; End of 5.0 tests. diff --git a/mysql-test/t/windows.test b/mysql-test/t/windows.test old mode 100644 new mode 100755 index 6976ee98750..6e6a7ec93a3 --- a/mysql-test/t/windows.test +++ b/mysql-test/t/windows.test @@ -33,5 +33,6 @@ drop table t1; # CREATE TABLE t1 (a int, b int); INSERT INTO t1 VALUES (1,1); EXPLAIN SELECT * FROM t1 WHERE b = (SELECT max(2)); +DROP TABLE t1; --echo End of 5.0 tests. From b591793496f4eb4e9953383c09ce4033bd85850f Mon Sep 17 00:00:00 2001 From: Gleb Shchepa Date: Thu, 16 Oct 2008 21:37:17 +0500 Subject: [PATCH 4/4] Bug #39844: Query Crash Mysql Server 5.0.67 Server crashed during a sort order optimization of a dependent subquery: SELECT (SELECT t1.a FROM t1, t2 WHERE t1.a = t2.b AND t2.a = t3.c ORDER BY t1.a) FROM t3; Bitmap of tables, that the reference to outer table column uses, in addition to the regular table bit has the OUTER_REF_TABLE_BIT bit set. The only_eq_ref_tables function traverses this map bit by bit simultaneously with join->map2table list. Obviously join->map2table never contains an entry for the OUTER_REF_TABLE_BIT pseudo-table, so the server crashed there. The only_eq_ref_tables function has been modified to traverse regular table bits only like the update_depend_map function (resetting of the OUTER_REF_TABLE_BIT there is enough, but resetting of the whole set of PSEUDO_TABLE_BITS is used there for sure). mysql-test/r/order_by.result: Added test case for bug #39844. mysql-test/t/order_by.test: Added test case for bug #39844. sql/sql_select.cc: Bug #39844: Query Crash Mysql Server 5.0.67 The only_eq_ref_tables function has been modified to traverse regular table bits only like the update_depend_map function (resetting of the OUTER_REF_TABLE_BIT there is enough, but resetting of the whole set of PSEUDO_TABLE_BITS is used there for sure). --- mysql-test/r/order_by.result | 16 ++++++++++++++++ mysql-test/t/order_by.test | 18 ++++++++++++++++++ sql/sql_select.cc | 1 + 3 files changed, 35 insertions(+) diff --git a/mysql-test/r/order_by.result b/mysql-test/r/order_by.result index 9f6a1b3932c..f64bbc79cbd 100644 --- a/mysql-test/r/order_by.result +++ b/mysql-test/r/order_by.result @@ -1076,3 +1076,19 @@ set session max_sort_length= 2180; select * from t1 order by b; ERROR HY001: Out of sort memory; increase server sort buffer size drop table t1; +# +# Bug #39844: Query Crash Mysql Server 5.0.67 +# +CREATE TABLE t1 (a INT PRIMARY KEY); +CREATE TABLE t2 (a INT PRIMARY KEY, b INT); +CREATE TABLE t3 (c INT); +INSERT INTO t1 (a) VALUES (1), (2); +INSERT INTO t2 (a,b) VALUES (1,2), (2,3); +INSERT INTO t3 (c) VALUES (1), (2); +SELECT +(SELECT t1.a FROM t1, t2 WHERE t1.a = t2.b AND t2.a = t3.c ORDER BY t1.a) +FROM t3; +(SELECT t1.a FROM t1, t2 WHERE t1.a = t2.b AND t2.a = t3.c ORDER BY t1.a) +2 +NULL +DROP TABLE t1, t2, t3; diff --git a/mysql-test/t/order_by.test b/mysql-test/t/order_by.test index 9a55c27df99..6d7ee1c1ca7 100644 --- a/mysql-test/t/order_by.test +++ b/mysql-test/t/order_by.test @@ -738,3 +738,21 @@ set session max_sort_length= 2180; select * from t1 order by b; drop table t1; + +--echo # +--echo # Bug #39844: Query Crash Mysql Server 5.0.67 +--echo # + +CREATE TABLE t1 (a INT PRIMARY KEY); +CREATE TABLE t2 (a INT PRIMARY KEY, b INT); +CREATE TABLE t3 (c INT); + +INSERT INTO t1 (a) VALUES (1), (2); +INSERT INTO t2 (a,b) VALUES (1,2), (2,3); +INSERT INTO t3 (c) VALUES (1), (2); + +SELECT + (SELECT t1.a FROM t1, t2 WHERE t1.a = t2.b AND t2.a = t3.c ORDER BY t1.a) + FROM t3; + +DROP TABLE t1, t2, t3; diff --git a/sql/sql_select.cc b/sql/sql_select.cc index 7ff069f0996..9723dd8c4e4 100644 --- a/sql/sql_select.cc +++ b/sql/sql_select.cc @@ -6574,6 +6574,7 @@ only_eq_ref_tables(JOIN *join,ORDER *order,table_map tables) { if (specialflag & SPECIAL_SAFE_MODE) return 0; // skip this optimize /* purecov: inspected */ + tables&= ~PSEUDO_TABLE_BITS; for (JOIN_TAB **tab=join->map2table ; tables ; tab++, tables>>=1) { if (tables & 1 && !eq_ref_table(join, order, *tab))