From 3a395a994a3d1f5d38e16e7b4781297692114cfc Mon Sep 17 00:00:00 2001 From: unknown Date: Sun, 9 Jun 2002 15:41:19 +0300 Subject: [PATCH] Bug fix for complicated GROUP BY ... ORDER BY query. Fixed problem in access rights testing (from last patch) mysql-test/r/group_by.result: Condenced GROUP test case mysql-test/t/group_by.test: Condenced GROUP test case sql/sql_parse.cc: Fixed problem in access rights testing (from last patch) sql/sql_select.cc: Rollback Sinisa's GROUP BY patch and make a better one. --- mysql-test/r/group_by.result | 31 +++++------------------------- mysql-test/t/group_by.test | 37 +++++++++++------------------------- sql/sql_parse.cc | 2 +- sql/sql_select.cc | 13 ++++--------- 4 files changed, 21 insertions(+), 62 deletions(-) diff --git a/mysql-test/r/group_by.result b/mysql-test/r/group_by.result index d990760d39b..0ec1c03c467 100644 --- a/mysql-test/r/group_by.result +++ b/mysql-test/r/group_by.result @@ -375,38 +375,17 @@ hijklm 2 DROP TABLE t1; drop table if exists t1; create table t1 (One int unsigned, Two int unsigned, Three int unsigned, Four int unsigned); -insert into t1 values (1,2,1,4); -insert into t1 values (1,2,2,4); -insert into t1 values (1,2,3,4); -insert into t1 values (1,2,4,4); -insert into t1 values (1,1,1,4); -insert into t1 values (1,1,2,4); -insert into t1 values (1,1,3,4); -insert into t1 values (1,1,4,4); -insert into t1 values (1,3,1,4); -insert into t1 values (1,3,2,4); -insert into t1 values (1,3,3,4); -insert into t1 values (1,3,4,4); +insert into t1 values (1,2,1,4),(1,2,2,4),(1,2,3,4),(1,2,4,4),(1,1,1,4),(1,1,2,4),(1,1,3,4),(1,1,4,4),(1,3,1,4),(1,3,2,4),(1,3,3,4),(1,3,4,4); select One, Two, sum(Four) from t1 group by One,Two; One Two sum(Four) 1 1 16 1 2 16 1 3 16 -drop table if exists t1; -drop table if exists t1,t2; +drop table t1; create table t1 (id integer primary key not null auto_increment, gender char(1)); -insert into t1 values(NULL, 'M'); -insert into t1 values(NULL, 'F'); -insert into t1 values(NULL, 'F'); -insert into t1 values(NULL, 'F'); -insert into t1 values(NULL, 'M'); -create table t2 (user_id integer not null, timestamp datetime); -insert into t2 values (1, sysdate()); -insert into t2 values (2, sysdate()); -insert into t2 values (1, sysdate()); -insert into t2 values (3, sysdate()); -insert into t2 values (4, sysdate()); -insert into t2 values (4, sysdate()); +insert into t1 values (NULL, 'M'), (NULL, 'F'),(NULL, 'F'),(NULL, 'F'),(NULL, 'M'); +create table t2 (user_id integer not null, date date); +insert into t2 values (1, '2002-06-09'),(2, '2002-06-09'),(1, '2002-06-09'),(3, '2002-06-09'),(4, '2002-06-09'),(4, '2002-06-09'); select u.gender as gender, count(distinct u.id) as dist_count, (count(distinct u.id)/5*100) as percentage from t1 u, t2 l where l.user_id = u.id group by u.gender; gender dist_count percentage F 3 60.00 diff --git a/mysql-test/t/group_by.test b/mysql-test/t/group_by.test index 09a2bee0d0c..0c9197febb5 100644 --- a/mysql-test/t/group_by.test +++ b/mysql-test/t/group_by.test @@ -284,35 +284,20 @@ INSERT INTO t1 values ('hij','klm'); SELECT CONCAT(a, b),count(*) FROM t1 GROUP BY 1; DROP TABLE t1; drop table if exists t1; + +# +# Test problem with ORDER BY on a SUM() column +# + create table t1 (One int unsigned, Two int unsigned, Three int unsigned, Four int unsigned); -insert into t1 values (1,2,1,4); -insert into t1 values (1,2,2,4); -insert into t1 values (1,2,3,4); -insert into t1 values (1,2,4,4); -insert into t1 values (1,1,1,4); -insert into t1 values (1,1,2,4); -insert into t1 values (1,1,3,4); -insert into t1 values (1,1,4,4); -insert into t1 values (1,3,1,4); -insert into t1 values (1,3,2,4); -insert into t1 values (1,3,3,4); -insert into t1 values (1,3,4,4); +insert into t1 values (1,2,1,4),(1,2,2,4),(1,2,3,4),(1,2,4,4),(1,1,1,4),(1,1,2,4),(1,1,3,4),(1,1,4,4),(1,3,1,4),(1,3,2,4),(1,3,3,4),(1,3,4,4); select One, Two, sum(Four) from t1 group by One,Two; -drop table if exists t1; -drop table if exists t1,t2; +drop table t1; + create table t1 (id integer primary key not null auto_increment, gender char(1)); -insert into t1 values(NULL, 'M'); -insert into t1 values(NULL, 'F'); -insert into t1 values(NULL, 'F'); -insert into t1 values(NULL, 'F'); -insert into t1 values(NULL, 'M'); -create table t2 (user_id integer not null, timestamp datetime); -insert into t2 values (1, sysdate()); -insert into t2 values (2, sysdate()); -insert into t2 values (1, sysdate()); -insert into t2 values (3, sysdate()); -insert into t2 values (4, sysdate()); -insert into t2 values (4, sysdate()); +insert into t1 values (NULL, 'M'), (NULL, 'F'),(NULL, 'F'),(NULL, 'F'),(NULL, 'M'); +create table t2 (user_id integer not null, date date); +insert into t2 values (1, '2002-06-09'),(2, '2002-06-09'),(1, '2002-06-09'),(3, '2002-06-09'),(4, '2002-06-09'),(4, '2002-06-09'); select u.gender as gender, count(distinct u.id) as dist_count, (count(distinct u.id)/5*100) as percentage from t1 u, t2 l where l.user_id = u.id group by u.gender; select u.gender as gender, count(distinct u.id) as dist_count, (count(distinct u.id)/5*100) as percentage from t1 u, t2 l where l.user_id = u.id group by u.gender order by percentage; drop table t1,t2; diff --git a/sql/sql_parse.cc b/sql/sql_parse.cc index f9ea184a159..f63f7e2f626 100644 --- a/sql/sql_parse.cc +++ b/sql/sql_parse.cc @@ -2565,7 +2565,7 @@ check_table_access(THD *thd,uint want_access,TABLE_LIST *tables, } } else if (check_access(thd,want_access,tables->db,&tables->grant.privilege, - 0, no_errors | grant_option)) + 0, no_errors)) return TRUE; } if (grant_option) diff --git a/sql/sql_select.cc b/sql/sql_select.cc index 3feb0fdddc3..7f194b85ab8 100644 --- a/sql/sql_select.cc +++ b/sql/sql_select.cc @@ -135,7 +135,7 @@ static TABLE *get_sort_by_table(ORDER *a,ORDER *b,TABLE_LIST *tables); static void calc_group_buffer(JOIN *join,ORDER *group); static bool alloc_group_fields(JOIN *join,ORDER *group); static bool make_sum_func_list(JOIN *join,List &fields); -static bool change_to_use_tmp_fields(List &func, bool change=false); +static bool change_to_use_tmp_fields(List &func); static bool change_refs_to_tmp_fields(THD *thd, List &func); static void init_tmptable_sum_functions(Item_sum **func); static void update_tmptable_sum_func(Item_sum **func,TABLE *tmp_table); @@ -788,7 +788,7 @@ mysql_select(THD *thd,TABLE_LIST *tables,List &fields,COND *conds, tmp_table=tmp_table2; join.join_tab[0].table=0; // Table is freed - if (change_to_use_tmp_fields(all_fields,true)) // No sum funcs anymore + if (change_to_use_tmp_fields(all_fields)) // No sum funcs anymore goto err; join.tmp_table_param.field_count+=join.tmp_table_param.sum_func_count; join.tmp_table_param.sum_func_count=0; @@ -3676,7 +3676,7 @@ create_tmp_table(THD *thd,TMP_TABLE_PARAM *param,List &fields, if (blob_count == 0) { /* We need to ensure that first byte is not 0 for the delete link */ - if (hidden_null_count) + if (param->hidden_field_count) hidden_null_count++; else null_count++; @@ -6764,7 +6764,7 @@ make_sum_func_list(JOIN *join,List &fields) */ static bool -change_to_use_tmp_fields(List &items, bool change) +change_to_use_tmp_fields(List &items) { List_iterator it(items); Item *item_field,*item; @@ -6776,11 +6776,6 @@ change_to_use_tmp_fields(List &items, bool change) continue; if (item->type() == Item::FIELD_ITEM) { - if (change) - { - ((Item_field*) item)->result_field->null_ptr=0; - item->maybe_null=0; - } ((Item_field*) item)->field= ((Item_field*) item)->result_field; }