From 3daaa8d5f4d87da61d5b005378b42d8144c33ef7 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 6 Apr 2006 21:42:03 +0400 Subject: [PATCH 1/2] Remove redundant code in opt_sum_query() --- sql/opt_sum.cc | 27 +++++++-------------------- 1 file changed, 7 insertions(+), 20 deletions(-) diff --git a/sql/opt_sum.cc b/sql/opt_sum.cc index 8a5a5875d47..373753a7b80 100644 --- a/sql/opt_sum.cc +++ b/sql/opt_sum.cc @@ -123,8 +123,11 @@ int opt_sum_query(TABLE_LIST *tables, List &all_fields,COND *conds) If the storage manager of 'tl' gives exact row count, compute the total number of rows. If there are no outer table dependencies, this count may be used as the real count. + Schema tables are filled after this function is invoked, so we can't + get row count */ - if (tl->table->file->table_flags() & HA_NOT_EXACT_COUNT) + if ((tl->table->file->table_flags() & HA_NOT_EXACT_COUNT) || + tl->schema_table) { is_exact_count= FALSE; count= 1; // ensure count != 0 @@ -149,31 +152,15 @@ int opt_sum_query(TABLE_LIST *tables, List &all_fields,COND *conds) switch (item_sum->sum_func()) { case Item_sum::COUNT_FUNC: /* - If the expr in count(expr) can never be null we can change this + If the expr in COUNT(expr) can never be null we can change this to the number of rows in the tables if this number is exact and there are no outer joins. */ if (!conds && !((Item_sum_count*) item)->args[0]->maybe_null && !outer_tables && is_exact_count) { - longlong count= 1; - TABLE_LIST *table; - for (table= tables; table; table= table->next_leaf) - { - if (outer_tables || (table->table->file->table_flags() & - HA_NOT_EXACT_COUNT) || table->schema_table) - { - const_result= 0; // Can't optimize left join - break; - } - tables->table->file->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK); - count*= table->table->file->records; - } - if (!table) - { - ((Item_sum_count*) item)->make_const(count); - recalc_const_item= 1; - } + ((Item_sum_count*) item)->make_const(count); + recalc_const_item= 1; } else const_result= 0; From 998b9d8b14892a91ad32fd1be83f9d60fddba98d Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 6 Apr 2006 15:29:15 -0700 Subject: [PATCH 2/2] Fixed bug #15917: unexpected complain for a NIST test case. The problem was due to the fact that with --lower-case-table-names set to 1 the function find_field_in_group did not convert the prefix 'HU' in HU.PROJ.CITY into lower case when looking for it in the group list. Yet the names in the group list were extended by the database name in lower case. mysql-test/r/having.result: Added a test case for bug #15917. mysql-test/t/having.test: Added a test case for bug #15917. sql/item.cc: Fixed bug #15917: unexpected complain for a NIST test case. The problem was due to the fact that with --lower-case-table-names set to 1 the function find_field_in_group did not convert the prefix 'HU' in HU.PROJ.CITY into lower case when looking for it in the group list. Yet the names in the group list were extended by the database name in lower case. The needed conversion was added to the code of find_field_in_group. --- mysql-test/r/having.result | 35 ++++++++++++++++++++++++++++++ mysql-test/t/having.test | 44 ++++++++++++++++++++++++++++++++++++++ sql/item.cc | 9 ++++++++ 3 files changed, 88 insertions(+) diff --git a/mysql-test/r/having.result b/mysql-test/r/having.result index 225d5a475ff..c827e11e50e 100644 --- a/mysql-test/r/having.result +++ b/mysql-test/r/having.result @@ -359,3 +359,38 @@ group by s1 collate latin1_swedish_ci having s1 = 'y'; s1 count(s1) y 1 drop table t1; +DROP SCHEMA IF EXISTS HU; +Warnings: +Note 1008 Can't drop database 'HU'; database doesn't exist +CREATE SCHEMA HU ; +USE HU ; +CREATE TABLE STAFF +(EMPNUM CHAR(3) NOT NULL UNIQUE, +EMPNAME CHAR(20), +GRADE DECIMAL(4), +CITY CHAR(15)); +CREATE TABLE PROJ +(PNUM CHAR(3) NOT NULL UNIQUE, +PNAME CHAR(20), +PTYPE CHAR(6), +BUDGET DECIMAL(9), +CITY CHAR(15)); +INSERT INTO STAFF VALUES ('E1','Alice',12,'Deale'); +INSERT INTO STAFF VALUES ('E2','Betty',10,'Vienna'); +INSERT INTO STAFF VALUES ('E3','Carmen',13,'Vienna'); +INSERT INTO STAFF VALUES ('E4','Don',12,'Deale'); +INSERT INTO STAFF VALUES ('E5','Ed',13,'Akron'); +INSERT INTO PROJ VALUES ('P1','MXSS','Design',10000,'Deale'); +INSERT INTO PROJ VALUES ('P2','CALM','Code',30000,'Vienna'); +INSERT INTO PROJ VALUES ('P3','SDP','Test',30000,'Tampa'); +INSERT INTO PROJ VALUES ('P4','SDP','Design',20000,'Deale'); +INSERT INTO PROJ VALUES ('P5','IRM','Test',10000,'Vienna'); +INSERT INTO PROJ VALUES ('P6','PAYR','Design',50000,'Deale'); +SELECT EMPNUM, GRADE*1000 +FROM HU.STAFF WHERE GRADE * 1000 > +ANY (SELECT SUM(BUDGET) FROM HU.PROJ +GROUP BY CITY, PTYPE +HAVING HU.PROJ.CITY = HU.STAFF.CITY); +EMPNUM GRADE*1000 +E3 13000 +DROP SCHEMA HU; diff --git a/mysql-test/t/having.test b/mysql-test/t/having.test index 78628bef198..9e5bc4bc136 100644 --- a/mysql-test/t/having.test +++ b/mysql-test/t/having.test @@ -347,3 +347,47 @@ group by s1 collate latin1_swedish_ci having s1 = 'y'; # MySQL returns: 1 row, with count(s1) = 1 drop table t1; + + +# +# Bug #15917: unexpected complain for a name in having clause +# when the server is run on Windows or with --lower-case-table-names=1 +# + +DROP SCHEMA IF EXISTS HU; +CREATE SCHEMA HU ; +USE HU ; + +CREATE TABLE STAFF + (EMPNUM CHAR(3) NOT NULL UNIQUE, + EMPNAME CHAR(20), + GRADE DECIMAL(4), + CITY CHAR(15)); + +CREATE TABLE PROJ + (PNUM CHAR(3) NOT NULL UNIQUE, + PNAME CHAR(20), + PTYPE CHAR(6), + BUDGET DECIMAL(9), + CITY CHAR(15)); + +INSERT INTO STAFF VALUES ('E1','Alice',12,'Deale'); +INSERT INTO STAFF VALUES ('E2','Betty',10,'Vienna'); +INSERT INTO STAFF VALUES ('E3','Carmen',13,'Vienna'); +INSERT INTO STAFF VALUES ('E4','Don',12,'Deale'); +INSERT INTO STAFF VALUES ('E5','Ed',13,'Akron'); + +INSERT INTO PROJ VALUES ('P1','MXSS','Design',10000,'Deale'); +INSERT INTO PROJ VALUES ('P2','CALM','Code',30000,'Vienna'); +INSERT INTO PROJ VALUES ('P3','SDP','Test',30000,'Tampa'); +INSERT INTO PROJ VALUES ('P4','SDP','Design',20000,'Deale'); +INSERT INTO PROJ VALUES ('P5','IRM','Test',10000,'Vienna'); +INSERT INTO PROJ VALUES ('P6','PAYR','Design',50000,'Deale'); + +SELECT EMPNUM, GRADE*1000 + FROM HU.STAFF WHERE GRADE * 1000 > + ANY (SELECT SUM(BUDGET) FROM HU.PROJ + GROUP BY CITY, PTYPE + HAVING HU.PROJ.CITY = HU.STAFF.CITY); + +DROP SCHEMA HU; diff --git a/sql/item.cc b/sql/item.cc index e1bde85e200..e3da950ceef 100644 --- a/sql/item.cc +++ b/sql/item.cc @@ -3054,6 +3054,7 @@ static Item** find_field_in_group_list(Item *find_item, ORDER *group_list) int found_match_degree= 0; Item_ident *cur_field; int cur_match_degree= 0; + char name_buff[NAME_LEN+1]; if (find_item->type() == Item::FIELD_ITEM || find_item->type() == Item::REF_ITEM) @@ -3065,6 +3066,14 @@ static Item** find_field_in_group_list(Item *find_item, ORDER *group_list) else return NULL; + if (db_name && lower_case_table_names) + { + /* Convert database to lower case for comparison */ + strmake(name_buff, db_name, sizeof(name_buff)-1); + my_casedn_str(files_charset_info, name_buff); + db_name= name_buff; + } + DBUG_ASSERT(field_name != 0); for (ORDER *cur_group= group_list ; cur_group ; cur_group= cur_group->next)