diff options
author | Alexey Botchkov <holyfoot@mysql.com> | 2009-11-03 17:54:41 +0400 |
---|---|---|
committer | Alexey Botchkov <holyfoot@mysql.com> | 2009-11-03 17:54:41 +0400 |
commit | d9ea0d0cd5030da40b791a75269cc4dbc4e0d8fe (patch) | |
tree | f08d046d55a73412d040b0fa93efb1e474adb19e /sql/sql_select.cc | |
parent | 5c4423c5af8e614f29061cd0f7bb79bfb0bfa7fc (diff) | |
download | mariadb-git-d9ea0d0cd5030da40b791a75269cc4dbc4e0d8fe.tar.gz |
Bug#41371 Select returns 1 row with condition "col is not null and col is null"
For application compatibility reasons MySQL converts "<autoincrement_column> IS NULL"
predicates to "<autoincrement_column> = LAST_INSERT_ID()" in the first SELECT following an
INSERT regardless of whether they're top level predicates or not. This causes wrong and
obscure results when these predicates are combined with others on the same columns. Fixed
by only doing the transformation on a single top-level predicate if a special SQL mode is
turned on (sql_auto_is_null).
Also made sql_auto_is_null off by default.
per-file comments:
mysql-test/r/func_isnull.result
Bug#41371 Select returns 1 row with condition "col is not null and col is null"
test result updated
mysql-test/t/func_isnull.test
Bug#41371 Select returns 1 row with condition "col is not null and col is null"
test case added
sql/mysqld.cc
Bug#41371 Select returns 1 row with condition "col is not null and col is null"
sql_auto_is_null now is OFF by default.
sql/sql_select.cc
Bug#41371 Select returns 1 row with condition "col is not null and col is null"
remove_eq_conds() split in two parts - one only checks the upper condition,
the req_remove_eq_conds() recursively checks all the condition tree.
mysql-test/extra/rpl_tests/rpl_insert_id.test
Bug#41371 Select returns 1 row with condition "col is not null and col is null"
test fixed (set the sql_auto_is_null variable)
mysql-test/r/mysqlbinlog.result
Bug#41371 Select returns 1 row with condition "col is not null and col is null"
result updated
mysql-test/r/mysqlbinlog2.result
Bug#41371 Select returns 1 row with condition "col is not null and col is null"
result updated
mysql-test/r/odbc.result
Bug#41371 Select returns 1 row with condition "col is not null and col is null"
result updated
mysql-test/r/query_cache.result
Bug#41371 Select returns 1 row with condition "col is not null and col is null"
result updated
mysql-test/r/user_var-binlog.result
Bug#41371 Select returns 1 row with condition "col is not null and col is null"
result updated
mysql-test/suite/binlog/r/binlog_row_ctype_ucs.result
Bug#41371 Select returns 1 row with condition "col is not null and col is null"
result updated
mysql-test/suite/binlog/r/binlog_stm_ctype_ucs.result
Bug#41371 Select returns 1 row with condition "col is not null and col is null"
result updated
mysql-test/suite/rpl/r/rpl_insert_id.result
Bug#41371 Select returns 1 row with condition "col is not null and col is null"
result updated
mysql-test/suite/rpl/r/rpl_row_mysqlbinlog.result
Bug#41371 Select returns 1 row with condition "col is not null and col is null"
result updated
mysql-test/suite/rpl/r/rpl_sp.result
Bug#41371 Select returns 1 row with condition "col is not null and col is null"
result updated
mysql-test/t/odbc.test
Bug#41371 Select returns 1 row with condition "col is not null and col is null"
test fixed (set the sql_auto_is_null variable)
Diffstat (limited to 'sql/sql_select.cc')
-rw-r--r-- | sql/sql_select.cc | 154 |
1 files changed, 103 insertions, 51 deletions
diff --git a/sql/sql_select.cc b/sql/sql_select.cc index 01b21dd7e2d..1d19ba7e96e 100644 --- a/sql/sql_select.cc +++ b/sql/sql_select.cc @@ -9119,18 +9119,26 @@ optimize_cond(JOIN *join, COND *conds, List<TABLE_LIST> *join_list, /** - Remove const and eq items. + Handles the reqursive job for remove_eq_conds() - @return - Return new item, or NULL if no condition @n - cond_value is set to according: - - COND_OK : query is possible (field = constant) - - COND_TRUE : always true ( 1 = 1 ) - - COND_FALSE : always false ( 1 = 2 ) + Remove const and eq items. Return new item, or NULL if no condition + cond_value is set to according: + COND_OK query is possible (field = constant) + COND_TRUE always true ( 1 = 1 ) + COND_FALSE always false ( 1 = 2 ) + + SYNPOSIS + remove_eq_conds() + thd THD environment + cond the condition to handle + cond_value the resulting value of the condition + + RETURN + *COND with the simplified condition */ -COND * -remove_eq_conds(THD *thd, COND *cond, Item::cond_result *cond_value) +static COND * +internal_remove_eq_conds(THD *thd, COND *cond, Item::cond_result *cond_value) { if (cond->type() == Item::COND_ITEM) { @@ -9144,7 +9152,7 @@ remove_eq_conds(THD *thd, COND *cond, Item::cond_result *cond_value) Item *item; while ((item=li++)) { - Item *new_item=remove_eq_conds(thd, item, &tmp_cond_value); + Item *new_item=internal_remove_eq_conds(thd, item, &tmp_cond_value); if (!new_item) li.remove(); else if (item != new_item) @@ -9193,6 +9201,86 @@ remove_eq_conds(THD *thd, COND *cond, Item::cond_result *cond_value) else if (cond->type() == Item::FUNC_ITEM && ((Item_func*) cond)->functype() == Item_func::ISNULL_FUNC) { + Item_func_isnull *func=(Item_func_isnull*) cond; + Item **args= func->arguments(); + if (args[0]->type() == Item::FIELD_ITEM) + { + Field *field=((Item_field*) args[0])->field; + /* fix to replace 'NULL' dates with '0' (shreeve@uci.edu) */ + /* + datetime_field IS NULL has to be modified to + datetime_field == 0 + */ + if (((field->type() == MYSQL_TYPE_DATE) || + (field->type() == MYSQL_TYPE_DATETIME)) && + (field->flags & NOT_NULL_FLAG) && !field->table->maybe_null) + { + COND *new_cond; + if ((new_cond= new Item_func_eq(args[0],new Item_int("0", 0, 2)))) + { + cond=new_cond; + /* + Item_func_eq can't be fixed after creation so we do not check + cond->fixed, also it do not need tables so we use 0 as second + argument. + */ + cond->fix_fields(thd, &cond); + } + } + } + if (cond->const_item()) + { + *cond_value= eval_const_cond(cond) ? Item::COND_TRUE : Item::COND_FALSE; + return (COND*) 0; + } + } + else if (cond->const_item()) + { + *cond_value= eval_const_cond(cond) ? Item::COND_TRUE : Item::COND_FALSE; + return (COND*) 0; + } + else if ((*cond_value= cond->eq_cmp_result()) != Item::COND_OK) + { // boolan compare function + Item *left_item= ((Item_func*) cond)->arguments()[0]; + Item *right_item= ((Item_func*) cond)->arguments()[1]; + if (left_item->eq(right_item,1)) + { + if (!left_item->maybe_null || + ((Item_func*) cond)->functype() == Item_func::EQUAL_FUNC) + return (COND*) 0; // Compare of identical items + } + } + *cond_value=Item::COND_OK; + return cond; // Point at next and level +} + + +/** + Remove const and eq items. Return new item, or NULL if no condition + cond_value is set to according: + COND_OK query is possible (field = constant) + COND_TRUE always true ( 1 = 1 ) + COND_FALSE always false ( 1 = 2 ) + + SYNPOSIS + remove_eq_conds() + thd THD environment + cond the condition to handle + cond_value the resulting value of the condition + + NOTES + calls the inner_remove_eq_conds to check all the tree reqursively + + RETURN + *COND with the simplified condition +*/ + +COND * +remove_eq_conds(THD *thd, COND *cond, Item::cond_result *cond_value) +{ + if (cond->type() == Item::FUNC_ITEM && + ((Item_func*) cond)->functype() == Item_func::ISNULL_FUNC) + { /* Handles this special case for some ODBC applications: The are requesting the row that was just updated with a auto_increment @@ -9235,52 +9323,16 @@ remove_eq_conds(THD *thd, COND *cond, Item::cond_result *cond_value) clear for next row */ thd->substitute_null_with_insert_id= FALSE; + + *cond_value= Item::COND_OK; + return cond; } - /* fix to replace 'NULL' dates with '0' (shreeve@uci.edu) */ - else if (((field->type() == MYSQL_TYPE_DATE) || - (field->type() == MYSQL_TYPE_DATETIME)) && - (field->flags & NOT_NULL_FLAG) && - !field->table->maybe_null) - { - COND *new_cond; - if ((new_cond= new Item_func_eq(args[0],new Item_int("0", 0, 2)))) - { - cond=new_cond; - /* - Item_func_eq can't be fixed after creation so we do not check - cond->fixed, also it do not need tables so we use 0 as second - argument. - */ - cond->fix_fields(thd, &cond); - } - } - } - if (cond->const_item()) - { - *cond_value= eval_const_cond(cond) ? Item::COND_TRUE : Item::COND_FALSE; - return (COND*) 0; - } - } - else if (cond->const_item()) - { - *cond_value= eval_const_cond(cond) ? Item::COND_TRUE : Item::COND_FALSE; - return (COND*) 0; - } - else if ((*cond_value= cond->eq_cmp_result()) != Item::COND_OK) - { // boolan compare function - Item *left_item= ((Item_func*) cond)->arguments()[0]; - Item *right_item= ((Item_func*) cond)->arguments()[1]; - if (left_item->eq(right_item,1)) - { - if (!left_item->maybe_null || - ((Item_func*) cond)->functype() == Item_func::EQUAL_FUNC) - return (COND*) 0; // Compare of identical items } } - *cond_value=Item::COND_OK; - return cond; // Point at next and level + return internal_remove_eq_conds(thd, cond, cond_value); // Scan all the condition } + /* Check if equality can be used in removing components of GROUP BY/DISTINCT |