From 33446269e18d0506802ca89df30f89e3e5978154 Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 11 Dec 2006 18:57:23 -0800 Subject: [PATCH 1/6] Fixed bug #24670: optimizations that are legal only for subqueries without tables and no WHERE condition were applied for any subquery without tables. mysql-test/r/subselect.result: Added a test case for bug #24670. mysql-test/t/subselect.test: Added a test case for bug #24670. sql/item_subselect.cc: Fixed bug #24670: optimizations that are legal only for subqueries without tables and no WHERE condition were applied for any subquery without tables. Removed an assertion that caused an abort for subqueries without tables and no WHERE condition. Blocked substitution of a single-row subquery without tables for the constant row from its select list when the subquery contained a WHERE condition. This optimization is valid only for subquries without tables with no conditions. Any subquery without tables with WHERE clause returns NULL if the WHERE condition is FALSE. Erroneously it was always considered as non-nullable that could trigger another optimization concerning IS NULL predicates which is applicable only for non-nullable expressions and ultimately led to a wrong result returned by the outer query. Added a proper implementation of the virtual method may_be_null for class subselect_single_select_engine. sql/item_subselect.h: Fixed bug #24670: optimizations that are legal only for subqueries without tables and no WHERE condition were applied for any subquery without tables. Made method may_by_null for class subselect_engine vvirtual. --- mysql-test/r/subselect.result | 13 +++++++++++++ mysql-test/t/subselect.test | 12 ++++++++++++ sql/item_subselect.cc | 19 +++++++++++++++++-- sql/item_subselect.h | 3 ++- 4 files changed, 44 insertions(+), 3 deletions(-) diff --git a/mysql-test/r/subselect.result b/mysql-test/r/subselect.result index 89315342f5e..a4ea8e21d61 100644 --- a/mysql-test/r/subselect.result +++ b/mysql-test/r/subselect.result @@ -3013,3 +3013,16 @@ t3 CREATE TABLE `t3` ( `a` datetime default NULL ) ENGINE=MyISAM DEFAULT CHARSET=latin1 DROP TABLE t1,t2,t3; +CREATE TABLE t1 (a int); +INSERT INTO t1 VALUES (1), (2); +SELECT a FROM t1 WHERE (SELECT 1 FROM DUAL WHERE 1=0) > 0; +a +SELECT a FROM t1 WHERE (SELECT 1 FROM DUAL WHERE 1=0) IS NULL; +a +1 +2 +EXPLAIN SELECT a FROM t1 WHERE (SELECT 1 FROM DUAL WHERE 1=0) IS NULL; +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY t1 ALL NULL NULL NULL NULL 2 +2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE +DROP TABLE t1; diff --git a/mysql-test/t/subselect.test b/mysql-test/t/subselect.test index 804cc2274c9..f816551e51f 100644 --- a/mysql-test/t/subselect.test +++ b/mysql-test/t/subselect.test @@ -1981,4 +1981,16 @@ SHOW CREATE TABLE t3; DROP TABLE t1,t2,t3; +# +# Bug 24670: subquery witout tables but with a WHERE clause +# + +CREATE TABLE t1 (a int); +INSERT INTO t1 VALUES (1), (2); + +SELECT a FROM t1 WHERE (SELECT 1 FROM DUAL WHERE 1=0) > 0; +SELECT a FROM t1 WHERE (SELECT 1 FROM DUAL WHERE 1=0) IS NULL; +EXPLAIN SELECT a FROM t1 WHERE (SELECT 1 FROM DUAL WHERE 1=0) IS NULL; + +DROP TABLE t1; # End of 4.1 tests diff --git a/sql/item_subselect.cc b/sql/item_subselect.cc index cd1f8f83821..20a092d7607 100644 --- a/sql/item_subselect.cc +++ b/sql/item_subselect.cc @@ -350,6 +350,7 @@ Item_singlerow_subselect::select_transformer(JOIN *join) */ !(select_lex->item_list.head()->type() == FIELD_ITEM || select_lex->item_list.head()->type() == REF_ITEM) && + !join->conds && !join->having && /* switch off this optimisation for prepare statement, because we do not rollback this changes @@ -374,8 +375,6 @@ Item_singlerow_subselect::select_transformer(JOIN *join) */ substitution->walk(&Item::remove_dependence_processor, (byte *) select_lex->outer_select()); - /* SELECT without FROM clause can't have WHERE or HAVING clause */ - DBUG_ASSERT(join->conds == 0 && join->having == 0); return RES_REDUCE; } return RES_OK; @@ -1795,6 +1794,22 @@ bool subselect_single_select_engine::no_tables() } +/* + Check statically whether the subquery can return NULL + + SINOPSYS + subselect_single_select_engine::may_be_null() + + RETURN + FALSE can guarantee that the subquery never return NULL + TRUE otherwise +*/ +bool subselect_single_select_engine::may_be_null() +{ + return ((no_tables() && !join->conds && !join->having) ? maybe_null : 1); +} + + /* Report about presence of tables in subquery diff --git a/sql/item_subselect.h b/sql/item_subselect.h index 7b064bfe92c..539dcc5676a 100644 --- a/sql/item_subselect.h +++ b/sql/item_subselect.h @@ -301,7 +301,7 @@ public: enum Item_result type() { return res_type; } enum_field_types field_type() { return res_field_type; } virtual void exclude()= 0; - bool may_be_null() { return maybe_null; }; + virtual bool may_be_null() { return maybe_null; }; virtual table_map upper_select_const_tables()= 0; static table_map calc_const_tables(TABLE_LIST *); virtual void print(String *str)= 0; @@ -335,6 +335,7 @@ public: void print (String *str); int change_item(Item_subselect *si, select_subselect *result); bool no_tables(); + bool may_be_null(); }; From 2f78d5ca81e39c55aa918758a1f9b4e46ce76d74 Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 13 Dec 2006 00:39:13 -0800 Subject: [PATCH 2/6] Fixed bug #25027. Removed an assertion that was not valid for the cases where the query in a prepared statement contained a single-row non-correlated subquery that was used as an argument of the IS NULL predicate. mysql-test/r/ps.result: Added a test case for bug #25027. mysql-test/t/ps.test: Added a test case for bug #25027. --- mysql-test/r/ps.result | 15 +++++++++++++++ mysql-test/t/ps.test | 18 ++++++++++++++++++ sql/sql_lex.cc | 1 - 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/mysql-test/r/ps.result b/mysql-test/r/ps.result index 234c12955d9..b5a9367c985 100644 --- a/mysql-test/r/ps.result +++ b/mysql-test/r/ps.result @@ -1514,4 +1514,19 @@ Variable_name Value Slow_queries 1 deallocate prepare no_index; deallocate prepare sq; +CREATE TABLE t1 (a int); +INSERT INTO t1 VALUES (1), (2); +CREATE TABLE t2 (b int); +INSERT INTO t2 VALUES (NULL); +SELECT a FROM t1 WHERE (SELECT b FROM t2) IS NULL; +a +1 +2 +PREPARE stmt FROM 'SELECT a FROM t1 WHERE (SELECT b FROM t2) IS NULL'; +EXECUTE stmt; +a +1 +2 +DEALLOCATE PREPARE stmt; +DROP TABLE t1,t2; End of 5.0 tests. diff --git a/mysql-test/t/ps.test b/mysql-test/t/ps.test index 547f9a766d1..9386436a1fd 100644 --- a/mysql-test/t/ps.test +++ b/mysql-test/t/ps.test @@ -1563,4 +1563,22 @@ execute sq; deallocate prepare no_index; deallocate prepare sq; +# +# Bug 25027: query with a single-row non-correlated subquery +# and IS NULL predicate +# + +CREATE TABLE t1 (a int); +INSERT INTO t1 VALUES (1), (2); +CREATE TABLE t2 (b int); +INSERT INTO t2 VALUES (NULL); + +SELECT a FROM t1 WHERE (SELECT b FROM t2) IS NULL; +PREPARE stmt FROM 'SELECT a FROM t1 WHERE (SELECT b FROM t2) IS NULL'; + +EXECUTE stmt; + +DEALLOCATE PREPARE stmt; +DROP TABLE t1,t2; + --echo End of 5.0 tests. diff --git a/sql/sql_lex.cc b/sql/sql_lex.cc index 3de842c8551..f5ec0c1161d 100644 --- a/sql/sql_lex.cc +++ b/sql/sql_lex.cc @@ -1903,7 +1903,6 @@ void st_select_lex_unit::set_limit(SELECT_LEX *sl) { ha_rows select_limit_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() : From acf0636e2e127106e1101a7e0f217b12fb2ceeff Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 15 Dec 2006 11:38:30 +0200 Subject: [PATCH 3/6] Bug #15439: UDF name case handling forces DELETE FROM mysql.func to remove the UDF When deleting a user defined function MySQL must remove it from both the in-memory hash table and the mysql.proc system table. Finding (and removal therefore) from the internal hash table is case insensitive (or whatever the default charset is), whereas finding and removal from the system table is case sensitive. As a result if you supply a function name that is not in the same character case to DROP FUNCTION the server will remove the function only from the in-memory hash table and will keep the row in mysql.proc system table. This will cause inconsistency between the two structures (that is fixed only by restarting the server). Fixed by using the name in the precise case (from the in-memory hash table) to delete the row in the mysql.proc system table. mysql-test/r/udf.result: Bug #15439: UDF name case handling forces DELETE FROM mysql.func to remove the UDF - test case mysql-test/t/udf.test: Bug #15439: UDF name case handling forces DELETE FROM mysql.func to remove the UDF - test case sql/sql_udf.cc: Bug #15439: UDF name case handling forces DELETE FROM mysql.func to remove the UDF - use the exact function name in deleting from mysql.proc. --- mysql-test/r/udf.result | 11 +++++++++++ mysql-test/t/udf.test | 17 +++++++++++++++++ sql/sql_udf.cc | 2 +- 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/mysql-test/r/udf.result b/mysql-test/r/udf.result index eb5d771bcfe..64b7111bbc8 100644 --- a/mysql-test/r/udf.result +++ b/mysql-test/r/udf.result @@ -194,6 +194,17 @@ DROP FUNCTION sequence; DROP FUNCTION lookup; DROP FUNCTION reverse_lookup; DROP FUNCTION avgcost; +select * from mysql.func; +name ret dl type +CREATE FUNCTION is_const RETURNS STRING SONAME "UDF_EXAMPLE_LIB"; +select IS_const(3); +IS_const(3) +const +drop function IS_const; +select * from mysql.func; +name ret dl type +select is_const(3); +ERROR 42000: FUNCTION test.is_const does not exist CREATE FUNCTION is_const RETURNS STRING SONAME "UDF_EXAMPLE_LIB"; select is_const(3) as const, diff --git a/mysql-test/t/udf.test b/mysql-test/t/udf.test index 52ae424e423..65cbc7ae3ae 100644 --- a/mysql-test/t/udf.test +++ b/mysql-test/t/udf.test @@ -187,6 +187,23 @@ DROP FUNCTION lookup; DROP FUNCTION reverse_lookup; DROP FUNCTION avgcost; +# +# Bug #15439: UDF name case handling forces DELETE FROM mysql.func to remove +# the UDF +# +select * from mysql.func; +--replace_result $UDF_EXAMPLE_LIB UDF_EXAMPLE_LIB +eval CREATE FUNCTION is_const RETURNS STRING SONAME "$UDF_EXAMPLE_LIB"; + +select IS_const(3); + +drop function IS_const; + +select * from mysql.func; + +--error 1305 +select is_const(3); + # # Bug#18761: constant expression as UDF parameters not passed in as constant # diff --git a/sql/sql_udf.cc b/sql/sql_udf.cc index 163801e2f1e..5bbfa522d58 100644 --- a/sql/sql_udf.cc +++ b/sql/sql_udf.cc @@ -536,7 +536,7 @@ int mysql_drop_function(THD *thd,const LEX_STRING *udf_name) tables.table_name= tables.alias= (char*) "func"; if (!(table = open_ltable(thd,&tables,TL_WRITE))) goto err; - table->field[0]->store(udf_name->str, udf_name->length, system_charset_info); + table->field[0]->store(udf->name.str, udf->name.length, &my_charset_bin); table->file->extra(HA_EXTRA_RETRIEVE_ALL_COLS); if (!table->file->index_read_idx(table->record[0], 0, (byte*) table->field[0]->ptr, From ce10e6ca133194632940a622fa94709a1a21a3c8 Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 19 Dec 2006 15:04:26 +0200 Subject: [PATCH 4/6] Bug #23578: Corruption prevents Optimize table from working properly with a spatial index While executing OPTIMIZE TABLE on MyISAM tables the server re-creates the index file(s) in order to sort them physically by the key. This cannot be done for R-tree indexes as it makes no sense. The server was not checking the type of the index and was accessing an R-tree index as if it was a B-tree. Fixed by preventing sorting the index file if it contains an R-tree index. myisam/mi_check.c: Bug #23578: Corruption prevents Optimize table from working properly with a spatial index - disable sorting the index file if it contains an R-tree index. mysql-test/r/gis-rtree.result: Bug #23578: Corruption prevents Optimize table from working properly with a spatial index - test case mysql-test/t/gis-rtree.test: Bug #23578: Corruption prevents Optimize table from working properly with a spatial index - test case --- myisam/mi_check.c | 8 ++++++++ mysql-test/r/gis-rtree.result | 11 +++++++++++ mysql-test/t/gis-rtree.test | 17 +++++++++++++++++ 3 files changed, 36 insertions(+) diff --git a/myisam/mi_check.c b/myisam/mi_check.c index 06fdd3bff4c..68e5860a6d1 100644 --- a/myisam/mi_check.c +++ b/myisam/mi_check.c @@ -1758,6 +1758,12 @@ int mi_sort_index(MI_CHECK *param, register MI_INFO *info, my_string name) MI_STATE_INFO old_state; DBUG_ENTER("mi_sort_index"); + /* cannot sort index files with R-tree indexes */ + for (key= 0,keyinfo= &share->keyinfo[0]; key < share->base.keys ; + key++,keyinfo++) + if (keyinfo->key_alg == HA_KEY_ALG_RTREE) + return 0; + if (!(param->testflag & T_SILENT)) printf("- Sorting index for MyISAM-table '%s'\n",name); @@ -1850,6 +1856,8 @@ static int sort_one_index(MI_CHECK *param, MI_INFO *info, MI_KEYDEF *keyinfo, char llbuff[22]; DBUG_ENTER("sort_one_index"); + /* cannot walk over R-tree indices */ + DBUG_ASSERT(keyinfo->key_alg != HA_KEY_ALG_RTREE); new_page_pos=param->new_file_pos; param->new_file_pos+=keyinfo->block_length; diff --git a/mysql-test/r/gis-rtree.result b/mysql-test/r/gis-rtree.result index e8134a50496..05d0d5634e6 100644 --- a/mysql-test/r/gis-rtree.result +++ b/mysql-test/r/gis-rtree.result @@ -881,3 +881,14 @@ ERROR 22003: Cannot get geometry object from data you send to the GEOMETRY field INSERT INTO t1(foo) VALUES (''); ERROR 22003: Cannot get geometry object from data you send to the GEOMETRY field DROP TABLE t1; +CREATE TABLE t1 (a INT AUTO_INCREMENT, b POINT NOT NULL, KEY (a), SPATIAL KEY (b)); +INSERT INTO t1 (b) VALUES (GeomFromText('POINT(1 2)')); +INSERT INTO t1 (b) SELECT b FROM t1; +INSERT INTO t1 (b) SELECT b FROM t1; +INSERT INTO t1 (b) SELECT b FROM t1; +INSERT INTO t1 (b) SELECT b FROM t1; +INSERT INTO t1 (b) SELECT b FROM t1; +OPTIMIZE TABLE t1; +Table Op Msg_type Msg_text +test.t1 optimize status OK +DROP TABLE t1; diff --git a/mysql-test/t/gis-rtree.test b/mysql-test/t/gis-rtree.test index e34dd14dbfc..1704fe7dc80 100644 --- a/mysql-test/t/gis-rtree.test +++ b/mysql-test/t/gis-rtree.test @@ -254,3 +254,20 @@ INSERT INTO t1() VALUES (); --error 1416 INSERT INTO t1(foo) VALUES (''); DROP TABLE t1; + +# +# Bug #23578: Corruption prevents Optimize table from working properly with a +# spatial index +# + +CREATE TABLE t1 (a INT AUTO_INCREMENT, b POINT NOT NULL, KEY (a), SPATIAL KEY (b)); + +INSERT INTO t1 (b) VALUES (GeomFromText('POINT(1 2)')); +INSERT INTO t1 (b) SELECT b FROM t1; +INSERT INTO t1 (b) SELECT b FROM t1; +INSERT INTO t1 (b) SELECT b FROM t1; +INSERT INTO t1 (b) SELECT b FROM t1; +INSERT INTO t1 (b) SELECT b FROM t1; + +OPTIMIZE TABLE t1; +DROP TABLE t1; From ea5557aade23efacda0f139bcec943b0c897c7e5 Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 3 Jan 2007 18:26:39 +0200 Subject: [PATCH 5/6] sql_udf.cc: Addendum to the fix for bug#15439 : valgrind pb failed sql/sql_udf.cc: Addendum to the fix for bug#15439 : valgrind pb failed --- sql/sql_udf.cc | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/sql/sql_udf.cc b/sql/sql_udf.cc index 5bbfa522d58..2ebfadb7ba9 100644 --- a/sql/sql_udf.cc +++ b/sql/sql_udf.cc @@ -510,6 +510,8 @@ int mysql_drop_function(THD *thd,const LEX_STRING *udf_name) TABLE *table; TABLE_LIST tables; udf_func *udf; + char *exact_name_str; + uint exact_name_len; DBUG_ENTER("mysql_drop_function"); if (!initialized) { @@ -523,6 +525,8 @@ int mysql_drop_function(THD *thd,const LEX_STRING *udf_name) my_error(ER_FUNCTION_NOT_DEFINED, MYF(0), udf_name->str); goto err; } + exact_name_str= udf->name.str; + exact_name_len= udf->name.length; del_udf(udf); /* Close the handle if this was function that was found during boot or @@ -536,7 +540,7 @@ int mysql_drop_function(THD *thd,const LEX_STRING *udf_name) tables.table_name= tables.alias= (char*) "func"; if (!(table = open_ltable(thd,&tables,TL_WRITE))) goto err; - table->field[0]->store(udf->name.str, udf->name.length, &my_charset_bin); + table->field[0]->store(exact_name_str, exact_name_len, &my_charset_bin); table->file->extra(HA_EXTRA_RETRIEVE_ALL_COLS); if (!table->file->index_read_idx(table->record[0], 0, (byte*) table->field[0]->ptr, From 42e31f7a45413b685c4332ced1d983ccfda29d25 Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 5 Jan 2007 14:02:50 +0200 Subject: [PATCH 6/6] Bug #15881: cast problems The optimizer removes expressions from GROUP BY/DISTINCT if they happen to participate in a = predicates of the WHERE clause (the idea being that if it's always equal to a constant it can't have multiple values). However for predicates where the expression and the constant item are of different result type this is not valid (e.g. a string column compared to 0). Fixed by additional check of the result types of the expression and the constant and if they differ the expression don't get removed from the group by list. mysql-test/r/distinct.result: Bug #15881: cast problems - test case mysql-test/t/distinct.test: Bug #15881: cast problems - test case sql/sql_select.cc: Bug #15881: cast problems - can't use = to remove GROUP BY/DISTINCT cols if they're not of the same type. --- mysql-test/r/distinct.result | 37 ++++++++++++++++++++++++++++++ mysql-test/t/distinct.test | 21 +++++++++++++++++ sql/sql_select.cc | 44 ++++++++++++++++++++++++++++++++++-- 3 files changed, 100 insertions(+), 2 deletions(-) diff --git a/mysql-test/r/distinct.result b/mysql-test/r/distinct.result index 86ab2141e2d..32151305698 100644 --- a/mysql-test/r/distinct.result +++ b/mysql-test/r/distinct.result @@ -607,3 +607,40 @@ id select_type table type possible_keys key key_len ref rows Extra SELECT DISTINCT a,a FROM t1 WHERE b < 12 ORDER BY a; a a DROP TABLE t1; +CREATE TABLE t1 (a CHAR(1)); +INSERT INTO t1 VALUES('A'), (0); +SELECT a FROM t1 WHERE a=0; +a +A +0 +Warnings: +Warning 1292 Truncated incorrect DOUBLE value: 'A' +SELECT DISTINCT a FROM t1 WHERE a=0; +a +A +0 +Warnings: +Warning 1292 Truncated incorrect DOUBLE value: 'A' +DROP TABLE t1; +CREATE TABLE t1 (a DATE); +INSERT INTO t1 VALUES ('1972-07-29'), ('1972-02-06'); +EXPLAIN SELECT (SELECT DISTINCT a FROM t1 WHERE a = '2002-08-03'); +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY NULL NULL NULL NULL NULL NULL NULL No tables used +2 SUBQUERY t1 ALL NULL NULL NULL NULL 2 Using where +EXPLAIN SELECT (SELECT DISTINCT ADDDATE(a,1) FROM t1 +WHERE ADDDATE(a,1) = '2002-08-03'); +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY NULL NULL NULL NULL NULL NULL NULL No tables used +2 SUBQUERY t1 ALL NULL NULL NULL NULL 2 Using where; Using temporary +CREATE TABLE t2 (a CHAR(5) CHARACTER SET latin1 COLLATE latin1_general_ci); +INSERT INTO t2 VALUES (0xf6); +INSERT INTO t2 VALUES ('oe'); +SELECT COUNT(*) FROM (SELECT DISTINCT a FROM t2) dt; +COUNT(*) +2 +SELECT COUNT(*) FROM +(SELECT DISTINCT a FROM t2 WHERE a='oe' COLLATE latin1_german2_ci) dt; +COUNT(*) +2 +DROP TABLE t1, t2; diff --git a/mysql-test/t/distinct.test b/mysql-test/t/distinct.test index b2cc42cc0ff..8734b940241 100644 --- a/mysql-test/t/distinct.test +++ b/mysql-test/t/distinct.test @@ -504,3 +504,24 @@ DROP TABLE t1; #DROP TABLE t1; #DROP TABLE t2; +# +# Bug #15881: cast problems +# +CREATE TABLE t1 (a CHAR(1)); INSERT INTO t1 VALUES('A'), (0); +SELECT a FROM t1 WHERE a=0; +SELECT DISTINCT a FROM t1 WHERE a=0; +DROP TABLE t1; +CREATE TABLE t1 (a DATE); +INSERT INTO t1 VALUES ('1972-07-29'), ('1972-02-06'); +EXPLAIN SELECT (SELECT DISTINCT a FROM t1 WHERE a = '2002-08-03'); +EXPLAIN SELECT (SELECT DISTINCT ADDDATE(a,1) FROM t1 + WHERE ADDDATE(a,1) = '2002-08-03'); +CREATE TABLE t2 (a CHAR(5) CHARACTER SET latin1 COLLATE latin1_general_ci); +INSERT INTO t2 VALUES (0xf6); +INSERT INTO t2 VALUES ('oe'); + +SELECT COUNT(*) FROM (SELECT DISTINCT a FROM t2) dt; +SELECT COUNT(*) FROM + (SELECT DISTINCT a FROM t2 WHERE a='oe' COLLATE latin1_german2_ci) dt; + +DROP TABLE t1, t2; diff --git a/sql/sql_select.cc b/sql/sql_select.cc index 30514f09493..0765b97c4d0 100644 --- a/sql/sql_select.cc +++ b/sql/sql_select.cc @@ -8432,6 +8432,46 @@ remove_eq_conds(THD *thd, COND *cond, Item::cond_result *cond_value) return cond; // Point at next and level } +/* + Check if equality can be used in removing components of GROUP BY/DISTINCT + + SYNOPSIS + test_if_equality_guarantees_uniqueness() + l the left comparison argument (a field if any) + r the right comparison argument (a const of any) + + DESCRIPTION + Checks if an equality predicate can be used to take away + DISTINCT/GROUP BY because it is known to be true for exactly one + distinct value (e.g. == ). + Arguments must be of the same type because e.g. + = may match more than 1 distinct value from + the column. + We must take into consideration and the optimization done for various + string constants when compared to dates etc (see Item_int_with_ref) as + well as the collation of the arguments. + + RETURN VALUE + TRUE can be used + FALSE cannot be used +*/ +static bool +test_if_equality_guarantees_uniqueness(Item *l, Item *r) +{ + return r->const_item() && + /* elements must be of the same result type */ + (r->result_type() == l->result_type() || + /* or dates compared to longs */ + (((l->type() == Item::FIELD_ITEM && + ((Item_field *)l)->field->can_be_compared_as_longlong()) || + (l->type() == Item::FUNC_ITEM && + ((Item_func *)l)->result_as_longlong())) && + r->result_type() == INT_RESULT)) + /* and must have the same collation if compared as strings */ + && (l->result_type() != STRING_RESULT || + l->collation.collation == r->collation.collation); +} + /* Return 1 if the item is a const value in all the WHERE clause */ @@ -8468,7 +8508,7 @@ const_expression_in_where(COND *cond, Item *comp_item, Item **const_item) Item *right_item= ((Item_func*) cond)->arguments()[1]; if (left_item->eq(comp_item,1)) { - if (right_item->const_item()) + if (test_if_equality_guarantees_uniqueness (left_item, right_item)) { if (*const_item) return right_item->eq(*const_item, 1); @@ -8478,7 +8518,7 @@ const_expression_in_where(COND *cond, Item *comp_item, Item **const_item) } else if (right_item->eq(comp_item,1)) { - if (left_item->const_item()) + if (test_if_equality_guarantees_uniqueness (right_item, left_item)) { if (*const_item) return left_item->eq(*const_item, 1);