From 1a0e1f1328c933d63e0bb0d99b9011058abd82cd Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 21 Nov 2006 10:11:43 +0200 Subject: [PATCH 01/17] Bug#23556: TRUNCATE TABLE still maps to DELETE This is the 5.0 part of the fix. Currently TRUNCATE command will not call delete_all_rows() in the handler (that implements the "fast" TRUNCATE for InnoDB) when there are triggers on the table. As decided by the architecture team TRUNCATE must use "fast" TRUNCATE even when there are triggers. Thus it must ignore the triggers. Made TRUNCATE to ignore the triggers and call delete_all_rows() for all storage engines to maintain engine consistency. mysql-test/r/trigger.result: Bug#23556: TRUNCATE TABLE still maps to DELETE - test case mysql-test/t/trigger.test: Bug#23556: TRUNCATE TABLE still maps to DELETE - test case sql/sql_delete.cc: Bug#23556: TRUNCATE TABLE still maps to DELETE - We implemenent fast TRUNCATE for InnoDB even if triggers are present. - TRUNCATE ignores triggers. --- mysql-test/r/trigger.result | 25 +++++++++++++++++++++++++ mysql-test/t/trigger.test | 25 +++++++++++++++++++++++++ sql/sql_delete.cc | 6 +++++- 3 files changed, 55 insertions(+), 1 deletion(-) diff --git a/mysql-test/r/trigger.result b/mysql-test/r/trigger.result index 5d643057666..54534ea5930 100644 --- a/mysql-test/r/trigger.result +++ b/mysql-test/r/trigger.result @@ -1241,4 +1241,29 @@ i j 2 2 13 13 drop table t1; +CREATE TABLE t1 (a INT PRIMARY KEY); +CREATE TABLE t2 (a INT PRIMARY KEY); +INSERT INTO t1 VALUES (1),(2),(3),(4),(5),(6),(7),(8); +CREATE TRIGGER trg_t1 BEFORE DELETE on t1 FOR EACH ROW +INSERT INTO t2 VALUES (OLD.a); +FLUSH STATUS; +TRUNCATE t1; +SHOW STATUS LIKE 'handler_delete'; +Variable_name Value +Handler_delete 0 +SELECT COUNT(*) FROM t2; +COUNT(*) +0 +INSERT INTO t1 VALUES (1),(2),(3),(4),(5),(6),(7),(8); +DELETE FROM t2; +FLUSH STATUS; +DELETE FROM t1; +SHOW STATUS LIKE 'handler_delete'; +Variable_name Value +Handler_delete 8 +SELECT COUNT(*) FROM t2; +COUNT(*) +8 +DROP TRIGGER trg_t1; +DROP TABLE t1,t2; End of 5.0 tests diff --git a/mysql-test/t/trigger.test b/mysql-test/t/trigger.test index 92320648033..dd3293b815f 100644 --- a/mysql-test/t/trigger.test +++ b/mysql-test/t/trigger.test @@ -1498,5 +1498,30 @@ update t1 set i= i+ 10 where j > 2; select * from t1; drop table t1; +# +# Bug#23556 TRUNCATE TABLE still maps to DELETE +# +CREATE TABLE t1 (a INT PRIMARY KEY); +CREATE TABLE t2 (a INT PRIMARY KEY); +INSERT INTO t1 VALUES (1),(2),(3),(4),(5),(6),(7),(8); + +CREATE TRIGGER trg_t1 BEFORE DELETE on t1 FOR EACH ROW + INSERT INTO t2 VALUES (OLD.a); + +FLUSH STATUS; +TRUNCATE t1; +SHOW STATUS LIKE 'handler_delete'; +SELECT COUNT(*) FROM t2; + +INSERT INTO t1 VALUES (1),(2),(3),(4),(5),(6),(7),(8); +DELETE FROM t2; + +FLUSH STATUS; +DELETE FROM t1; +SHOW STATUS LIKE 'handler_delete'; +SELECT COUNT(*) FROM t2; + +DROP TRIGGER trg_t1; +DROP TABLE t1,t2; --echo End of 5.0 tests diff --git a/sql/sql_delete.cc b/sql/sql_delete.cc index e13e7728708..0d47c2a2623 100644 --- a/sql/sql_delete.cc +++ b/sql/sql_delete.cc @@ -76,10 +76,14 @@ bool mysql_delete(THD *thd, TABLE_LIST *table_list, COND *conds, Test if the user wants to delete all rows and deletion doesn't have any side-effects (because of triggers), so we can use optimized handler::delete_all_rows() method. + We implement fast TRUNCATE for InnoDB even if triggers are present. + TRUNCATE ignores triggers. */ if (!using_limit && const_cond && (!conds || conds->val_int()) && !(specialflag & (SPECIAL_NO_NEW_FUNC | SPECIAL_SAFE_MODE)) && - !(table->triggers && table->triggers->has_delete_triggers())) + (thd->lex->sql_command == SQLCOM_TRUNCATE || + !(table->triggers && table->triggers->has_delete_triggers())) + ) { deleted= table->file->records; if (!(error=table->file->delete_all_rows())) From cab412666eba39347410f1369e56ace47938a76d Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 21 Nov 2006 10:25:10 +0200 Subject: [PATCH 02/17] Bug #23556: TRUNCATE TABLE still maps to DELETE - TRUNCATE requires DROP privilege, not DELETE mysql-test/r/grant.result: Bug #23556: TRUNCATE TABLE still maps to DELETE - test case mysql-test/r/trigger-grant.result: Bug #23556: TRUNCATE TABLE still maps to DELETE - updated test case mysql-test/t/grant.test: Bug #23556: TRUNCATE TABLE still maps to DELETE - test case mysql-test/t/trigger-grant.test: Bug #23556: TRUNCATE TABLE still maps to DELETE - updated test case --- mysql-test/r/grant.result | 20 ++++++++++++++++++ mysql-test/r/trigger-grant.result | 4 ++-- mysql-test/t/grant.test | 35 +++++++++++++++++++++++++++++++ mysql-test/t/trigger-grant.test | 4 ++-- sql/sql_parse.cc | 2 +- 5 files changed, 60 insertions(+), 5 deletions(-) diff --git a/mysql-test/r/grant.result b/mysql-test/r/grant.result index 5dbbfbd9ab8..f6864ce7b94 100644 --- a/mysql-test/r/grant.result +++ b/mysql-test/r/grant.result @@ -972,4 +972,24 @@ REVOKE EXECUTE ON PROCEDURE p1 FROM 1234567890abcdefGHIKL@localhost; ERROR HY000: String '1234567890abcdefGHIKL' is too long for user name (should be no longer than 16) REVOKE EXECUTE ON PROCEDURE t1 FROM some_user_name@1234567890abcdefghij1234567890abcdefghij1234567890abcdefghijQWERTY; ERROR HY000: String '1234567890abcdefghij1234567890abcdefghij1234567890abcdefghijQWERTY' is too long for host name (should be no longer than 60) +CREATE USER bug23556@localhost; +CREATE DATABASE bug23556; +GRANT SELECT ON bug23556.* TO bug23556@localhost; +USE bug23556; +CREATE TABLE t1 (a INT PRIMARY KEY); +INSERT INTO t1 VALUES (1),(2),(3),(4),(5); +GRANT DELETE ON t1 TO bug23556@localhost; +USE bug23556; +TRUNCATE t1; +ERROR 42000: DROP command denied to user 'bug23556'@'localhost' for table 't1' +USE bug23556; +REVOKE DELETE ON t1 FROM bug23556@localhost; +GRANT DROP ON t1 TO bug23556@localhost; +USE bug23556; +TRUNCATE t1; +USE bug23556; +DROP TABLE t1; +USE test; +DROP DATABASE bug23556; +DROP USER bug23556@localhost; End of 5.0 tests diff --git a/mysql-test/r/trigger-grant.result b/mysql-test/r/trigger-grant.result index eb211be0270..49c36513fbc 100644 --- a/mysql-test/r/trigger-grant.result +++ b/mysql-test/r/trigger-grant.result @@ -14,8 +14,8 @@ CREATE TABLE t1(num_value INT); CREATE TABLE t2(user_str TEXT); ---> connection: default -GRANT INSERT, DELETE ON mysqltest_db1.t1 TO mysqltest_dfn@localhost; -GRANT INSERT, DELETE ON mysqltest_db1.t2 TO mysqltest_dfn@localhost; +GRANT INSERT, DROP ON mysqltest_db1.t1 TO mysqltest_dfn@localhost; +GRANT INSERT, DROP ON mysqltest_db1.t2 TO mysqltest_dfn@localhost; ---> connection: default GRANT SUPER ON *.* TO mysqltest_dfn@localhost; diff --git a/mysql-test/t/grant.test b/mysql-test/t/grant.test index d3781d58780..91d8b248bcc 100644 --- a/mysql-test/t/grant.test +++ b/mysql-test/t/grant.test @@ -863,4 +863,39 @@ REVOKE EXECUTE ON PROCEDURE p1 FROM 1234567890abcdefGHIKL@localhost; --error ER_WRONG_STRING_LENGTH REVOKE EXECUTE ON PROCEDURE t1 FROM some_user_name@1234567890abcdefghij1234567890abcdefghij1234567890abcdefghijQWERTY; + + +# +# BUG#23556: TRUNCATE TABLE still maps to DELETE +# +CREATE USER bug23556@localhost; +CREATE DATABASE bug23556; +GRANT SELECT ON bug23556.* TO bug23556@localhost; +connect (bug23556,localhost,bug23556,,bug23556); + +connection default; +USE bug23556; +CREATE TABLE t1 (a INT PRIMARY KEY); INSERT INTO t1 VALUES (1),(2),(3),(4),(5); +GRANT DELETE ON t1 TO bug23556@localhost; + +connection bug23556; +USE bug23556; +--error ER_TABLEACCESS_DENIED_ERROR +TRUNCATE t1; + +connection default; +USE bug23556; +REVOKE DELETE ON t1 FROM bug23556@localhost; +GRANT DROP ON t1 TO bug23556@localhost; + +connection bug23556; +USE bug23556; +TRUNCATE t1; + +connection default; +USE bug23556; +DROP TABLE t1; +USE test; +DROP DATABASE bug23556; +DROP USER bug23556@localhost; --echo End of 5.0 tests diff --git a/mysql-test/t/trigger-grant.test b/mysql-test/t/trigger-grant.test index 6dd0c83dc92..2a0cf829bae 100644 --- a/mysql-test/t/trigger-grant.test +++ b/mysql-test/t/trigger-grant.test @@ -60,8 +60,8 @@ CREATE TABLE t2(user_str TEXT); --echo --echo ---> connection: default -GRANT INSERT, DELETE ON mysqltest_db1.t1 TO mysqltest_dfn@localhost; -GRANT INSERT, DELETE ON mysqltest_db1.t2 TO mysqltest_dfn@localhost; +GRANT INSERT, DROP ON mysqltest_db1.t1 TO mysqltest_dfn@localhost; +GRANT INSERT, DROP ON mysqltest_db1.t2 TO mysqltest_dfn@localhost; # # Check that the user must have TRIGGER privilege to create a trigger. diff --git a/sql/sql_parse.cc b/sql/sql_parse.cc index 9f295b2c247..a49afe5e188 100644 --- a/sql/sql_parse.cc +++ b/sql/sql_parse.cc @@ -3508,7 +3508,7 @@ end_with_restore_list: break; } DBUG_ASSERT(first_table == all_tables && first_table != 0); - if (check_one_table_access(thd, DELETE_ACL, all_tables)) + if (check_one_table_access(thd, DROP_ACL, all_tables)) goto error; /* Don't allow this within a transaction because we want to use From da561a802caff95d755b8d2061c27c248f22e3cd Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 25 Jan 2007 18:44:35 -0800 Subject: [PATCH 03/17] Fixed bug #24653. The bug report has demonstrated the following two problems. 1. If an ORDER/GROUP BY list includes a constant expression being optimized away and, at the same time, containing single-row subselects that return more that one row, no error is reported. Strictly speaking the standard allows to ignore error in this case. Yet, now a corresponding fatal error is reported in this case. 2. If a query requires sorting by expressions containing single-row subselects that, however, return more than one row, then the execution of the query may cause a server crash. To fix this some code has been added that blocks execution of a subselect item in case of a fatal error in the method Item_subselect::exec. mysql-test/r/subselect.result: Added a test cases for bug #24653. mysql-test/t/subselect.test: Added a test cases for bug #24653. sql/filesort.cc: Fixed bug #24653. Added a check for fatal error after reading the next row from the table in the function find_all_keys. sql/item.cc: Fixed bug #24653. Down-ported calculation of the attribute with_subselect of for Item objects. sql/item.h: Fixed bug #24653. Down-ported calculation of the attribute with_subselect of for Item objects. sql/item_cmpfunc.cc: Fixed bug #24653. Down-ported calculation of the attribute with_subselect of for Item objects. sql/item_cmpfunc.h: Fixed bug #24653. Down-ported calculation of the attribute with_subselect of for Item objects. sql/item_func.cc: Fixed bug #24653. Down-ported calculation of the attribute with_subselect of for Item objects. sql/item_subselect.cc: Fixed bug #24653. Added a check for fatal error in the method Item_subselect::exec to block evaluation of subselects in erroneous situations. Down-ported calculation of the attribute with_subselect of for Item objects. sql/sql_select.cc: Fixed bug #24653. Added a check to verify that any constant expression used in ORDER BY and/or GROUP BY lists which is optimized away does not contain subselects returning more than one row. If it does a fatal error is reported. --- mysql-test/r/subselect.result | 74 +++++++++++++++++++++++++++++++++++ mysql-test/t/subselect.test | 59 ++++++++++++++++++++++++++++ sql/filesort.cc | 6 ++- sql/item.cc | 1 + sql/item.h | 3 ++ sql/item_cmpfunc.cc | 7 ++-- sql/item_cmpfunc.h | 3 +- sql/item_func.cc | 1 + sql/item_subselect.cc | 4 ++ sql/sql_select.cc | 21 ++++++++++ 10 files changed, 174 insertions(+), 5 deletions(-) diff --git a/mysql-test/r/subselect.result b/mysql-test/r/subselect.result index a4ea8e21d61..a339a139687 100644 --- a/mysql-test/r/subselect.result +++ b/mysql-test/r/subselect.result @@ -3026,3 +3026,77 @@ 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; +CREATE TABLE t1 (a int); +INSERT INTO t1 VALUES (2), (4), (1), (3); +CREATE TABLE t2 (b int, c int); +INSERT INTO t2 VALUES +(2,1), (1,3), (2,1), (4,4), (2,2), (1,4); +SELECT a FROM t1 ORDER BY (SELECT c FROM t2 WHERE b > 2 ); +a +2 +4 +1 +3 +SELECT a FROM t1 ORDER BY (SELECT c FROM t2 WHERE b > 1); +ERROR 21000: Subquery returns more than 1 row +SELECT a FROM t1 ORDER BY (SELECT c FROM t2 WHERE b > 2), a; +a +1 +2 +3 +4 +SELECT a FROM t1 ORDER BY (SELECT c FROM t2 WHERE b > 1), a; +ERROR 21000: Subquery returns more than 1 row +SELECT b, MAX(c) FROM t2 GROUP BY b, (SELECT c FROM t2 WHERE b > 2); +b MAX(c) +1 4 +2 2 +4 4 +SELECT b, MAX(c) FROM t2 GROUP BY b, (SELECT c FROM t2 WHERE b > 1); +ERROR 21000: Subquery returns more than 1 row +SELECT a FROM t1 GROUP BY a +HAVING IFNULL((SELECT b FROM t2 WHERE b > 2), +(SELECT c FROM t2 WHERE c=a AND b > 2 ORDER BY b)) > 3; +a +1 +2 +3 +4 +SELECT a FROM t1 GROUP BY a +HAVING IFNULL((SELECT b FROM t2 WHERE b > 1), +(SELECT c FROM t2 WHERE c=a AND b > 2 ORDER BY b)) > 3; +ERROR 21000: Subquery returns more than 1 row +SELECT a FROM t1 GROUP BY a +HAVING IFNULL((SELECT b FROM t2 WHERE b > 4), +(SELECT c FROM t2 WHERE c=a AND b > 2 ORDER BY b)) > 3; +a +4 +SELECT a FROM t1 GROUP BY a +HAVING IFNULL((SELECT b FROM t2 WHERE b > 4), +(SELECT c FROM t2 WHERE c=a AND b > 1 ORDER BY b)) > 3; +ERROR 21000: Subquery returns more than 1 row +SELECT a FROM t1 +ORDER BY IFNULL((SELECT b FROM t2 WHERE b > 2), +(SELECT c FROM t2 WHERE c=a AND b > 2 ORDER BY b)); +a +2 +4 +1 +3 +SELECT a FROM t1 +ORDER BY IFNULL((SELECT b FROM t2 WHERE b > 1), +(SELECT c FROM t2 WHERE c=a AND b > 1 ORDER BY b)); +ERROR 21000: Subquery returns more than 1 row +SELECT a FROM t1 +ORDER BY IFNULL((SELECT b FROM t2 WHERE b > 4), +(SELECT c FROM t2 WHERE c=a AND b > 2 ORDER BY b)); +a +2 +1 +3 +4 +SELECT a FROM t1 +ORDER BY IFNULL((SELECT b FROM t2 WHERE b > 4), +(SELECT c FROM t2 WHERE c=a AND b > 1 ORDER BY b)); +ERROR 21000: Subquery returns more than 1 row +DROP TABLE t1,t2; diff --git a/mysql-test/t/subselect.test b/mysql-test/t/subselect.test index f816551e51f..67a18e7a30f 100644 --- a/mysql-test/t/subselect.test +++ b/mysql-test/t/subselect.test @@ -1993,4 +1993,63 @@ 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; + +# +# Bug 24653: sorting by expressions containing subselects +# that return more than one row +# + +CREATE TABLE t1 (a int); +INSERT INTO t1 VALUES (2), (4), (1), (3); + +CREATE TABLE t2 (b int, c int); +INSERT INTO t2 VALUES + (2,1), (1,3), (2,1), (4,4), (2,2), (1,4); + +SELECT a FROM t1 ORDER BY (SELECT c FROM t2 WHERE b > 2 ); +--error 1242 +SELECT a FROM t1 ORDER BY (SELECT c FROM t2 WHERE b > 1); +SELECT a FROM t1 ORDER BY (SELECT c FROM t2 WHERE b > 2), a; +--error 1242 +SELECT a FROM t1 ORDER BY (SELECT c FROM t2 WHERE b > 1), a; + +SELECT b, MAX(c) FROM t2 GROUP BY b, (SELECT c FROM t2 WHERE b > 2); +--error 1242 +SELECT b, MAX(c) FROM t2 GROUP BY b, (SELECT c FROM t2 WHERE b > 1); + + +SELECT a FROM t1 GROUP BY a + HAVING IFNULL((SELECT b FROM t2 WHERE b > 2), + (SELECT c FROM t2 WHERE c=a AND b > 2 ORDER BY b)) > 3; +--error 1242 +SELECT a FROM t1 GROUP BY a + HAVING IFNULL((SELECT b FROM t2 WHERE b > 1), + (SELECT c FROM t2 WHERE c=a AND b > 2 ORDER BY b)) > 3; + +SELECT a FROM t1 GROUP BY a + HAVING IFNULL((SELECT b FROM t2 WHERE b > 4), + (SELECT c FROM t2 WHERE c=a AND b > 2 ORDER BY b)) > 3; +--error 1242 +SELECT a FROM t1 GROUP BY a + HAVING IFNULL((SELECT b FROM t2 WHERE b > 4), + (SELECT c FROM t2 WHERE c=a AND b > 1 ORDER BY b)) > 3; + +SELECT a FROM t1 + ORDER BY IFNULL((SELECT b FROM t2 WHERE b > 2), + (SELECT c FROM t2 WHERE c=a AND b > 2 ORDER BY b)); +--error 1242 +SELECT a FROM t1 + ORDER BY IFNULL((SELECT b FROM t2 WHERE b > 1), + (SELECT c FROM t2 WHERE c=a AND b > 1 ORDER BY b)); + +SELECT a FROM t1 + ORDER BY IFNULL((SELECT b FROM t2 WHERE b > 4), + (SELECT c FROM t2 WHERE c=a AND b > 2 ORDER BY b)); +--error 1242 +SELECT a FROM t1 + ORDER BY IFNULL((SELECT b FROM t2 WHERE b > 4), + (SELECT c FROM t2 WHERE c=a AND b > 1 ORDER BY b)); + +DROP TABLE t1,t2; + # End of 4.1 tests diff --git a/sql/filesort.cc b/sql/filesort.cc index 63a8515020b..38a49e24263 100644 --- a/sql/filesort.cc +++ b/sql/filesort.cc @@ -387,7 +387,8 @@ static ha_rows find_all_keys(SORTPARAM *param, SQL_SELECT *select, byte *ref_pos,*next_pos,ref_buff[MAX_REFLENGTH]; my_off_t record; TABLE *sort_form; - volatile my_bool *killed= ¤t_thd->killed; + THD *thd= current_thd; + volatile my_bool *killed= &thd->killed; handler *file; DBUG_ENTER("find_all_keys"); DBUG_PRINT("info",("using: %s",(select?select->quick?"ranges":"where":"every row"))); @@ -474,6 +475,9 @@ static ha_rows find_all_keys(SORTPARAM *param, SQL_SELECT *select, } else file->unlock_row(); + /* It does not make sense to read more keys in case of a fatal error */ + if (thd->net.report_error) + DBUG_RETURN(HA_POS_ERROR); } (void) file->extra(HA_EXTRA_NO_CACHE); /* End cacheing of records */ if (!next_pos) diff --git a/sql/item.cc b/sql/item.cc index 1e8c70c6616..7d110ccdc25 100644 --- a/sql/item.cc +++ b/sql/item.cc @@ -47,6 +47,7 @@ Item::Item(): collation.set(&my_charset_bin, DERIVATION_COERCIBLE); name= 0; decimals= 0; max_length= 0; + with_subselect= 0; /* Put item in free list so that we can free all items at end */ THD *thd= current_thd; diff --git a/sql/item.h b/sql/item.h index ad8bea663f1..f2136c4997a 100644 --- a/sql/item.h +++ b/sql/item.h @@ -142,6 +142,9 @@ public: my_bool with_sum_func; my_bool fixed; /* If item fixed with fix_fields */ DTCollation collation; + my_bool with_subselect; /* If this item is a subselect or some + of its arguments is or contains a + subselect */ // alloc & destruct is done as start of select using sql_alloc Item(); diff --git a/sql/item_cmpfunc.cc b/sql/item_cmpfunc.cc index 859b4e0ecc1..91546c2282c 100644 --- a/sql/item_cmpfunc.cc +++ b/sql/item_cmpfunc.cc @@ -2139,6 +2139,7 @@ Item_cond::fix_fields(THD *thd, TABLE_LIST *tables, Item **ref) and_tables_cache&= tmp_table_map; const_item_cache&= item->const_item(); with_sum_func= with_sum_func || item->with_sum_func; + with_subselect|= item->with_subselect; if (item->maybe_null) maybe_null=1; } @@ -2351,7 +2352,7 @@ longlong Item_func_isnull::val_int() Handle optimization if the argument can't be null This has to be here because of the test in update_used_tables(). */ - if (!used_tables_cache) + if (!used_tables_cache && !with_subselect) return cached_value; return args[0]->is_null() ? 1: 0; } @@ -2360,7 +2361,7 @@ longlong Item_is_not_null_test::val_int() { DBUG_ASSERT(fixed == 1); DBUG_ENTER("Item_is_not_null_test::val_int"); - if (!used_tables_cache) + if (!used_tables_cache && !with_subselect) { owner->was_null|= (!cached_value); DBUG_PRINT("info", ("cached :%d", cached_value)); @@ -2387,7 +2388,7 @@ void Item_is_not_null_test::update_used_tables() else { args[0]->update_used_tables(); - if (!(used_tables_cache=args[0]->used_tables())) + if (!(used_tables_cache=args[0]->used_tables()) && !with_subselect) { /* Remember if the value is always NULL or never NULL */ cached_value= (longlong) !args[0]->is_null(); diff --git a/sql/item_cmpfunc.h b/sql/item_cmpfunc.h index 0e157fd412c..4635a301c31 100644 --- a/sql/item_cmpfunc.h +++ b/sql/item_cmpfunc.h @@ -843,7 +843,8 @@ public: else { args[0]->update_used_tables(); - if ((const_item_cache= !(used_tables_cache= args[0]->used_tables()))) + if ((const_item_cache= !(used_tables_cache= args[0]->used_tables())) && + !with_subselect) { /* Remember if the value is always NULL or never NULL */ cached_value= (longlong) args[0]->is_null(); diff --git a/sql/item_func.cc b/sql/item_func.cc index 461998477b5..92ba7520642 100644 --- a/sql/item_func.cc +++ b/sql/item_func.cc @@ -177,6 +177,7 @@ Item_func::fix_fields(THD *thd, TABLE_LIST *tables, Item **ref) used_tables_cache|= item->used_tables(); not_null_tables_cache|= item->not_null_tables(); const_item_cache&= item->const_item(); + with_subselect|= item->with_subselect; } } fix_length_and_dec(); diff --git a/sql/item_subselect.cc b/sql/item_subselect.cc index 20a092d7607..cdbcde8b56b 100644 --- a/sql/item_subselect.cc +++ b/sql/item_subselect.cc @@ -39,6 +39,7 @@ Item_subselect::Item_subselect(): engine(0), old_engine(0), used_tables_cache(0), have_to_be_excluded(0), const_item_cache(1), engine_changed(0), changed(0) { + with_subselect= 1; reset(); /* item value is NULL if select_subselect not changed this value @@ -201,6 +202,9 @@ bool Item_subselect::exec() mem root */ thd->mem_root= &thd->main_mem_root; + if (thd->net.report_error) + /* Do not execute subselect in case of a fatal error */ + return 1; res= engine->exec(); thd->mem_root= old_root; diff --git a/sql/sql_select.cc b/sql/sql_select.cc index 0254e8f56dc..97fa6bb7809 100644 --- a/sql/sql_select.cc +++ b/sql/sql_select.cc @@ -644,6 +644,13 @@ JOIN::optimize() { ORDER *org_order= order; order=remove_const(this, order,conds,1, &simple_order); + if (thd->net.report_error) + { + error= 1; + DBUG_PRINT("error",("Error from remove_const")); + DBUG_RETURN(1); + } + /* If we are using ORDER BY NULL or ORDER BY const_expression, return result in any order (even if we are using a GROUP BY) @@ -747,6 +754,12 @@ JOIN::optimize() group_list= remove_const(this, (old_group_list= group_list), conds, rollup.state == ROLLUP::STATE_NONE, &simple_group); + if (thd->net.report_error) + { + error= 1; + DBUG_PRINT("error",("Error from remove_const")); + DBUG_RETURN(1); + } if (old_group_list && !group_list) select_distinct= 0; } @@ -763,6 +776,12 @@ JOIN::optimize() { group_list= procedure->group= remove_const(this, procedure->group, conds, 1, &simple_group); + if (thd->net.report_error) + { + error= 1; + DBUG_PRINT("error",("Error from remove_const")); + DBUG_RETURN(1); + } calc_group_buffer(this, group_list); } @@ -4428,6 +4447,8 @@ remove_const(JOIN *join,ORDER *first_order, COND *cond, *simple_order=0; // Must do a temp table to sort else if (!(order_tables & not_const_tables)) { + if (order->item[0]->with_subselect) + order->item[0]->val_str(&order->item[0]->str_value); DBUG_PRINT("info",("removing: %s", order->item[0]->full_name())); continue; // skip const item } From 917adbaec4abe14b67150656ee2002be335f43f9 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 25 Jan 2007 22:50:48 -0800 Subject: [PATCH 04/17] Post merge fix --- sql/item_subselect.cc | 2 ++ 1 file changed, 2 insertions(+) diff --git a/sql/item_subselect.cc b/sql/item_subselect.cc index 7a2d749524b..d61bb25e9b7 100644 --- a/sql/item_subselect.cc +++ b/sql/item_subselect.cc @@ -199,6 +199,8 @@ bool Item_subselect::exec() /* Do not execute subselect in case of a fatal error */ return 1; + res= engine->exec(); + if (engine_changed) { engine_changed= 0; From 1495924319c1b6fab0261a7206e872f43f0c4857 Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 26 Jan 2007 17:10:45 -0800 Subject: [PATCH 05/17] Fixed bug #24420. Objects of the classes Item_func_is_not_null_test and Item_func_trig_cond must be transparent for the method Item::split_sum_func2 as these classes are pure helpers. It means that the method Item::split_sum_func2 should look at those objects as at pure wrappers. mysql-test/r/subselect3.result: Added a test case for bug #24420. mysql-test/t/subselect3.test: Added a test case for bug #24420. --- mysql-test/r/subselect3.result | 16 ++++++++++++++++ mysql-test/t/subselect3.test | 17 +++++++++++++++++ sql/item.cc | 5 ++++- 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/mysql-test/r/subselect3.result b/mysql-test/r/subselect3.result index 07ff17c9df9..29143b9e504 100644 --- a/mysql-test/r/subselect3.result +++ b/mysql-test/r/subselect3.result @@ -629,3 +629,19 @@ cc NULL NULL aa 1 1 bb NULL NULL drop table t1,t2; +create table t1 (a int, b int); +insert into t1 values (0,0), (2,2), (3,3); +create table t2 (a int, b int); +insert into t2 values (1,1), (3,3); +select a, b, (a,b) in (select a, min(b) from t2 group by a) Z from t1; +a b Z +0 0 0 +2 2 0 +3 3 1 +insert into t2 values (NULL,4); +select a, b, (a,b) in (select a, min(b) from t2 group by a) Z from t1; +a b Z +0 0 0 +2 2 0 +3 3 1 +drop table t1,t2; diff --git a/mysql-test/t/subselect3.test b/mysql-test/t/subselect3.test index 23d78721dbe..ed8480ba464 100644 --- a/mysql-test/t/subselect3.test +++ b/mysql-test/t/subselect3.test @@ -472,3 +472,20 @@ select oref, a, a in (select min(ie) from t1 where oref=t2.oref group by grp) Z drop table t1,t2; +# +# BUG#24420: row-based IN suqueries with aggregation when the left operand +# of the subquery predicate may contain NULL values +# + +create table t1 (a int, b int); +insert into t1 values (0,0), (2,2), (3,3); +create table t2 (a int, b int); +insert into t2 values (1,1), (3,3); + +select a, b, (a,b) in (select a, min(b) from t2 group by a) Z from t1; + +insert into t2 values (NULL,4); +select a, b, (a,b) in (select a, min(b) from t2 group by a) Z from t1; + +drop table t1,t2; + diff --git a/sql/item.cc b/sql/item.cc index 0017b64ba0d..9a55eb25e2c 100644 --- a/sql/item.cc +++ b/sql/item.cc @@ -1242,7 +1242,10 @@ void Item::split_sum_func2(THD *thd, Item **ref_pointer_array, if (type() == SUM_FUNC_ITEM && skip_registered && ((Item_sum *) this)->ref_by) return; - if (type() != SUM_FUNC_ITEM && with_sum_func) + if ((type() != SUM_FUNC_ITEM && with_sum_func) || + (type() == FUNC_ITEM && + (((Item_func *) this)->functype() == Item_func::ISNOTNULLTEST_FUNC || + ((Item_func *) this)->functype() == Item_func::TRIG_COND_FUNC))) { /* Will split complicated items and ignore simple ones */ split_sum_func(thd, ref_pointer_array, fields); From 190efb06e7c9fe96bb4f4294f3b0ddbc4eef12de Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 29 Jan 2007 15:07:11 +0100 Subject: [PATCH 06/17] BUG#20604: FORCE INDEX uses keys disabled by ALTER TABLE The function that checks whether we can use keys for aggregates, find_key_for_maxmin(), assumes that keys disabled by ALTER TABLE ... DISABLE KEYS are not in the set table->keys_in_use_for_query. I.E., if a key is in this set, the optimizer assumes it is free to use it. The bug is that keys disabled with ALTER TABLE ... DISABLE KEYS still appear in table->keys_in_use_for_query When the TABLE object has been initialized with setup_tables(). Before setup_tables is called, however, keys that are disabled in the aforementioned way are not included in TABLE::keys_in_use_for_query. The provided patch changes the code that updates keys_is_use_for_query so that it assumes that keys_is_use_for_query already takes into account all disabled keys, and generally all keys that should be used by the query. mysql-test/r/key.result: Test for BUG#20604. The important part of the test is the explain output that tests what indexes are used. mysql-test/t/key.test: The minimal test case that reveals the bug. The optimizer for aggregates relies on keys disabled with ALTER TABLE ... DISABLE KEYS not being in the set TABLE::keys_in_use_for_query. When the execution engine tries to use a disabled index, MyISAM returns an error. sql/sql_base.cc: Exclude the keys disabled by ALTER TABLE ... DISABLE_KEYS from TABLE::keys_in_use_for_query, and in general, don't introduce any new keys. We may not know why keys have been removed at previous stages. sql/sql_select.cc: The intersection operation between table->s->keys_in_use and table->keys_in_use_for_query is no longer necessary. We can trust that the latter is a subset of the former. sql/table.h: Added comments to TABLE_SHARE::keys_in_use and TABLE::keys_in_use_for_query. --- mysql-test/r/key.result | 7 +++++++ mysql-test/t/key.test | 11 +++++++++++ sql/sql_base.cc | 7 ++++++- sql/sql_select.cc | 11 ++++++----- sql/table.h | 23 +++++++++++++++++++++-- 5 files changed, 51 insertions(+), 8 deletions(-) diff --git a/mysql-test/r/key.result b/mysql-test/r/key.result index ea8d4338d08..853b837c46e 100644 --- a/mysql-test/r/key.result +++ b/mysql-test/r/key.result @@ -482,3 +482,10 @@ alter table t1 drop index i3, drop index i2, drop index i1; alter table t1 add index i3 (c3), add index i2 (c2), add unique index i1 (c1); ERROR 23000: Duplicate entry '1' for key 'i1' drop table t1; +CREATE TABLE t1( a TINYINT, KEY(a) ) ENGINE=MyISAM; +INSERT INTO t1 VALUES( 1 ); +ALTER TABLE t1 DISABLE KEYS; +EXPLAIN SELECT MAX(a) FROM t1 FORCE INDEX(a); +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE t1 system NULL NULL NULL NULL 1 +drop table t1; diff --git a/mysql-test/t/key.test b/mysql-test/t/key.test index 7c6b38cb871..5a5057f9c97 100644 --- a/mysql-test/t/key.test +++ b/mysql-test/t/key.test @@ -442,3 +442,14 @@ alter table t1 drop index i3, drop index i2, drop index i1; alter table t1 add index i3 (c3), add index i2 (c2), add unique index i1 (c1); drop table t1; + +# +# Bug #20604: Test for disabled keys with aggregate functions and FORCE INDEX. +# + +CREATE TABLE t1( a TINYINT, KEY(a) ) ENGINE=MyISAM; +INSERT INTO t1 VALUES( 1 ); +ALTER TABLE t1 DISABLE KEYS; +EXPLAIN SELECT MAX(a) FROM t1 FORCE INDEX(a); + +drop table t1; diff --git a/sql/sql_base.cc b/sql/sql_base.cc index c03910a7829..bb84e89a2ef 100644 --- a/sql/sql_base.cc +++ b/sql/sql_base.cc @@ -5462,7 +5462,12 @@ bool setup_tables(THD *thd, Name_resolution_context *context, get_key_map_from_key_list(&map, table, table_list->use_index); if (map.is_set_all()) DBUG_RETURN(1); - table->keys_in_use_for_query=map; + /* + Don't introduce keys in keys_in_use_for_query that weren't there + before. FORCE/USE INDEX should not add keys, it should only remove + all keys except the key(s) specified in the hint. + */ + table->keys_in_use_for_query.intersect(map); } if (table_list->ignore_index) { diff --git a/sql/sql_select.cc b/sql/sql_select.cc index c11553e9eed..bd2a7a87c53 100644 --- a/sql/sql_select.cc +++ b/sql/sql_select.cc @@ -12262,13 +12262,14 @@ test_if_skip_sort_order(JOIN_TAB *tab,ORDER *order,ha_rows select_limit, DBUG_ENTER("test_if_skip_sort_order"); LINT_INIT(ref_key_parts); + /* Check which keys can be used to resolve ORDER BY. */ + usable_keys= table->keys_in_use_for_query; + /* - Check which keys can be used to resolve ORDER BY. - We must not try to use disabled keys. + Keys disabled by ALTER TABLE ... DISABLE KEYS should have already + been taken into account. */ - usable_keys= table->s->keys_in_use; - /* we must not consider keys that are disabled by IGNORE INDEX */ - usable_keys.intersect(table->keys_in_use_for_query); + DBUG_ASSERT(usable_keys.is_subset(table->s->keys_in_use)); for (ORDER *tmp_order=order; tmp_order ; tmp_order=tmp_order->next) { diff --git a/sql/table.h b/sql/table.h index 82083d79570..3c9f2cac164 100644 --- a/sql/table.h +++ b/sql/table.h @@ -158,7 +158,12 @@ typedef struct st_table_share LEX_STRING path; /* Path to .frm file (from datadir) */ LEX_STRING normalized_path; /* unpack_filename(path) */ LEX_STRING connect_string; - key_map keys_in_use; /* Keys in use for table */ + + /* + Set of keys in use, implemented as a Bitmap. + Excludes keys disabled by ALTER TABLE ... DISABLE KEYS. + */ + key_map keys_in_use; key_map keys_for_keyread; ha_rows min_rows, max_rows; /* create information */ ulong avg_row_length; /* create information */ @@ -313,7 +318,21 @@ struct st_table { byte *write_row_record; /* Used as optimisation in THD::write_row */ byte *insert_values; /* used by INSERT ... UPDATE */ - key_map quick_keys, used_keys, keys_in_use_for_query, merge_keys; + key_map quick_keys, used_keys; + + /* + A set of keys that can be used in the query that references this + table + + All indexes disabled on the table's TABLE_SHARE (see TABLE::s) will be + subtracted from this set upon instantiation. Thus for any TABLE t it holds + that t.keys_in_use_for_query is a subset of t.s.keys_in_use. Generally we + must not introduce any new keys here (see setup_tables). + + The set is implemented as a bitmap. + */ + key_map keys_in_use_for_query; + key_map merge_keys; KEY *key_info; /* data of keys in database */ Field *next_number_field; /* Set if next_number is activated */ From 3cb3a9a149ecc29634c748d0da7932d1814a9b7f Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 30 Jan 2007 17:43:34 +0200 Subject: [PATCH 07/17] Bug #25643: SEC_TO_TIME function problem Checking for NULL before calling the val_xxx() methods only checks for such arguments that are known to be NULLs at compile time. The arguments that may or may not contain NULLs (e.g. function calls and possibly others) are not checked at all. Fixed by first calling the val_xxx() method and then checking for null in SEC_TO_TIME(). In addition QUARTER() was not returning 0 (as all the val_int() functions do when processing a NULL value). mysql-test/r/func_time.result: Bug #25643: SEC_TO_TIME function problem - test case mysql-test/t/func_time.test: Bug #25643: SEC_TO_TIME function problem - test case sql/item_timefunc.cc: Bug #25643: SEC_TO_TIME function problem - null handling fixed for QUARTER() and SEC_TO_TIME() --- mysql-test/r/func_time.result | 14 ++++++++++++++ mysql-test/t/func_time.test | 12 ++++++++++++ sql/item_timefunc.cc | 9 ++++++--- 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/mysql-test/r/func_time.result b/mysql-test/r/func_time.result index 45c05f0b5b5..32f930ca6ba 100644 --- a/mysql-test/r/func_time.result +++ b/mysql-test/r/func_time.result @@ -1207,3 +1207,17 @@ SET NAMES DEFAULT; select str_to_date('10:00 PM', '%h:%i %p') + INTERVAL 10 MINUTE; str_to_date('10:00 PM', '%h:%i %p') + INTERVAL 10 MINUTE NULL +CREATE TABLE t1 (a int, t1 time, t2 time, d date, PRIMARY KEY (a)); +INSERT INTO t1 VALUES (1, '10:00:00', NULL, NULL), +(2, '11:00:00', '11:15:00', '1972-02-06'); +SELECT t1, t2, SEC_TO_TIME( TIME_TO_SEC( t2 ) - TIME_TO_SEC( t1 ) ), QUARTER(d) +FROM t1; +t1 t2 SEC_TO_TIME( TIME_TO_SEC( t2 ) - TIME_TO_SEC( t1 ) ) QUARTER(d) +10:00:00 NULL NULL NULL +11:00:00 11:15:00 00:15:00 1 +SELECT t1, t2, SEC_TO_TIME( TIME_TO_SEC( t2 ) - TIME_TO_SEC( t1 ) ), QUARTER(d) +FROM t1 ORDER BY a DESC; +t1 t2 SEC_TO_TIME( TIME_TO_SEC( t2 ) - TIME_TO_SEC( t1 ) ) QUARTER(d) +11:00:00 11:15:00 00:15:00 1 +10:00:00 NULL NULL NULL +DROP TABLE t1; diff --git a/mysql-test/t/func_time.test b/mysql-test/t/func_time.test index a69cbb67c5b..1aa4b434a83 100644 --- a/mysql-test/t/func_time.test +++ b/mysql-test/t/func_time.test @@ -713,3 +713,15 @@ SET NAMES DEFAULT; # select str_to_date('10:00 PM', '%h:%i %p') + INTERVAL 10 MINUTE; + +# +# Bug #25643: SEC_TO_TIME function problem +# +CREATE TABLE t1 (a int, t1 time, t2 time, d date, PRIMARY KEY (a)); +INSERT INTO t1 VALUES (1, '10:00:00', NULL, NULL), + (2, '11:00:00', '11:15:00', '1972-02-06'); +SELECT t1, t2, SEC_TO_TIME( TIME_TO_SEC( t2 ) - TIME_TO_SEC( t1 ) ), QUARTER(d) + FROM t1; +SELECT t1, t2, SEC_TO_TIME( TIME_TO_SEC( t2 ) - TIME_TO_SEC( t1 ) ), QUARTER(d) + FROM t1 ORDER BY a DESC; +DROP TABLE t1; diff --git a/sql/item_timefunc.cc b/sql/item_timefunc.cc index e99db62068d..83b48645d1c 100644 --- a/sql/item_timefunc.cc +++ b/sql/item_timefunc.cc @@ -1054,7 +1054,8 @@ longlong Item_func_quarter::val_int() { DBUG_ASSERT(fixed == 1); TIME ltime; - (void) get_arg0_date(<ime, TIME_FUZZY_DATE); + if (get_arg0_date(<ime, TIME_FUZZY_DATE)) + return 0; return (longlong) ((ltime.month+2)/3); } @@ -1668,6 +1669,7 @@ String *Item_func_sec_to_time::val_str(String *str) { DBUG_ASSERT(fixed == 1); TIME ltime; + longlong arg_val= args[0]->val_int(); if ((null_value=args[0]->null_value) || str->alloc(19)) { @@ -1675,7 +1677,7 @@ String *Item_func_sec_to_time::val_str(String *str) return (String*) 0; } - sec_to_time(args[0]->val_int(), args[0]->unsigned_flag, <ime); + sec_to_time(arg_val, args[0]->unsigned_flag, <ime); make_time((DATE_TIME_FORMAT *) 0, <ime, str); return str; @@ -1686,11 +1688,12 @@ longlong Item_func_sec_to_time::val_int() { DBUG_ASSERT(fixed == 1); TIME ltime; + longlong arg_val= args[0]->val_int(); if ((null_value=args[0]->null_value)) return 0; - sec_to_time(args[0]->val_int(), args[0]->unsigned_flag, <ime); + sec_to_time(arg_val, args[0]->unsigned_flag, <ime); return (ltime.neg ? -1 : 1) * ((ltime.hour)*10000 + ltime.minute*100 + ltime.second); From 32b5759e8acf44e52ffb2559ea5af19891f29890 Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 30 Jan 2007 18:07:41 +0100 Subject: [PATCH 08/17] This is an empty changeset to trigger a rerun of pushbuild. sql/table.h: Only touched a comment. --- sql/table.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sql/table.h b/sql/table.h index 3c9f2cac164..2923eb1db7b 100644 --- a/sql/table.h +++ b/sql/table.h @@ -322,7 +322,7 @@ struct st_table { /* A set of keys that can be used in the query that references this - table + table. All indexes disabled on the table's TABLE_SHARE (see TABLE::s) will be subtracted from this set upon instantiation. Thus for any TABLE t it holds From 1944b4ca01165d34b2e0d5449704587129045684 Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 30 Jan 2007 13:06:36 -0800 Subject: [PATCH 09/17] Fixed bug #24987. Made the function opt_sum_query to return HA_ERR_KEY_NOT_FOUND when no matches were found (instead of -1 it returned prior this patch). This changes allow us to avoid possible conflicts with return values from user-defined handler methods which also may return -1. No particular test cases are provided with this fix. sql/opt_sum.cc: Fixed bug #24987. Made the function opt_sum_query to return HA_ERR_KEY_NOT_FOUND when no matches were found (instead of -1 it returned prior this patch). This changes allow us to avoid possible conflicts with return values from user-defined handler methods which also may return -1. sql/sql_select.cc: Fixed bug #24987. Made the function opt_sum_query to return HA_ERR_KEY_NOT_FOUND when no matches were found (instead of -1 it returned prior this patch). This changes allow us to avoid possible conflicts with return values from user-defined handler methods which also may return -1. --- sql/opt_sum.cc | 10 +++++----- sql/sql_select.cc | 18 ++++++++++-------- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/sql/opt_sum.cc b/sql/opt_sum.cc index 412fc0dc037..f912d67fe06 100644 --- a/sql/opt_sum.cc +++ b/sql/opt_sum.cc @@ -68,9 +68,9 @@ static int maxmin_in_range(bool max_fl, Field* field, COND *cond); GROUP BY part. RETURN VALUES - 0 No errors - 1 if all items were resolved - -1 on impossible conditions + 0 no errors + 1 if all items were resolved + HA_ERR_KEY_NOT_FOUND on impossible conditions OR an error number from my_base.h HA_ERR_... if a deadlock or a lock wait timeout happens, for example */ @@ -216,7 +216,7 @@ int opt_sum_query(TABLE_LIST *tables, List &all_fields,COND *conds) if (error) { if (error == HA_ERR_KEY_NOT_FOUND || error == HA_ERR_END_OF_FILE) - return -1; // No rows matching WHERE + return HA_ERR_KEY_NOT_FOUND; // No rows matching WHERE /* HA_ERR_LOCK_DEADLOCK or some other error */ table->file->print_error(error, MYF(0)); return(error); @@ -303,7 +303,7 @@ int opt_sum_query(TABLE_LIST *tables, List &all_fields,COND *conds) if (error) { if (error == HA_ERR_KEY_NOT_FOUND || error == HA_ERR_END_OF_FILE) - return -1; // No rows matching WHERE + return HA_ERR_KEY_NOT_FOUND; // No rows matching WHERE /* HA_ERR_LOCK_DEADLOCK or some other error */ table->file->print_error(error, MYF(0)); return(error); diff --git a/sql/sql_select.cc b/sql/sql_select.cc index 97fa6bb7809..0768e54c4da 100644 --- a/sql/sql_select.cc +++ b/sql/sql_select.cc @@ -531,23 +531,25 @@ JOIN::optimize() { int res; /* - opt_sum_query() returns -1 if no rows match to the WHERE conditions, - or 1 if all items were resolved, or 0, or an error number HA_ERR_... + opt_sum_query() returns HA_ERR_KEY_NOT_FOUND if no rows match + to the WHERE conditions, + or 1 if all items were resolved, + or 0, or an error number HA_ERR_... */ if ((res=opt_sum_query(tables_list, all_fields, conds))) { + if (res == HA_ERR_KEY_NOT_FOUND) + { + zero_result_cause= "No matching min/max row"; + error=0; + DBUG_RETURN(0); + } if (res > 1) { thd->fatal_error(); error= res; DBUG_RETURN(1); } - if (res < 0) - { - zero_result_cause= "No matching min/max row"; - error=0; - DBUG_RETURN(0); - } zero_result_cause= "Select tables optimized away"; tables_list= 0; // All tables resolved /* From d01ec5e72704513ce0e13fe5dd924c088757507e Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 31 Jan 2007 10:18:26 +0200 Subject: [PATCH 10/17] Bug #25551: inconsistent behaviour in grouping NULL, depending on index type The optimizer takes away columns from GROUP BY/DISTINCT if they constitute all the parts of an unique index. However if some of the columns can contain NULLs this cannot be done (because an UNIQUE index can have multiple rows with NULL values). Fixed by not using UNIQUE indexes with nullable columns to remove grouping columns from GROUP BY/DISTINCT. mysql-test/r/distinct.result: Bug #25551: inconsistent behaviour in grouping NULL, depending on index type - test case mysql-test/t/distinct.test: Bug #25551: inconsistent behaviour in grouping NULL, depending on index type - test case sql/sql_select.cc: Bug #25551: inconsistent behaviour in grouping NULL, depending on index type - UNIQUE NULL indices don't guarantee GROUP BY/DISTINCT. --- mysql-test/r/distinct.result | 26 +++++++++++++++++++++++++- mysql-test/t/distinct.test | 17 ++++++++++++++++- sql/sql_select.cc | 19 +++++++++++-------- 3 files changed, 52 insertions(+), 10 deletions(-) diff --git a/mysql-test/r/distinct.result b/mysql-test/r/distinct.result index 32151305698..3508a83a810 100644 --- a/mysql-test/r/distinct.result +++ b/mysql-test/r/distinct.result @@ -530,7 +530,8 @@ id select_type table type possible_keys key key_len ref rows Extra EXPLAIN SELECT DISTINCT a,b FROM t1 GROUP BY a,b; id select_type table type possible_keys key key_len ref rows Extra 1 SIMPLE t1 ALL NULL NULL NULL NULL 3 -CREATE TABLE t2(a INT, b INT, c INT, d INT, PRIMARY KEY (a,b)); +CREATE TABLE t2(a INT, b INT NOT NULL, c INT NOT NULL, d INT, +PRIMARY KEY (a,b)); INSERT INTO t2 VALUES (1,1,1,50), (1,2,3,40), (2,1,3,4); EXPLAIN SELECT DISTINCT a FROM t2; id select_type table type possible_keys key key_len ref rows Extra @@ -644,3 +645,26 @@ SELECT COUNT(*) FROM COUNT(*) 2 DROP TABLE t1, t2; +CREATE TABLE t1 (a INT, UNIQUE (a)); +INSERT INTO t1 VALUES (4),(null),(2),(1),(null),(3); +EXPLAIN SELECT DISTINCT a FROM t1; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE t1 index NULL a 5 NULL 6 Using index +SELECT DISTINCT a FROM t1; +a +NULL +1 +2 +3 +4 +EXPLAIN SELECT a FROM t1 GROUP BY a; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE t1 index NULL a 5 NULL 6 Using index +SELECT a FROM t1 GROUP BY a; +a +NULL +1 +2 +3 +4 +DROP TABLE t1; diff --git a/mysql-test/t/distinct.test b/mysql-test/t/distinct.test index 8734b940241..476e4ce7735 100644 --- a/mysql-test/t/distinct.test +++ b/mysql-test/t/distinct.test @@ -364,7 +364,8 @@ EXPLAIN SELECT a FROM t1 GROUP BY a; EXPLAIN SELECT a,b FROM t1 GROUP BY a,b; EXPLAIN SELECT DISTINCT a,b FROM t1 GROUP BY a,b; -CREATE TABLE t2(a INT, b INT, c INT, d INT, PRIMARY KEY (a,b)); +CREATE TABLE t2(a INT, b INT NOT NULL, c INT NOT NULL, d INT, + PRIMARY KEY (a,b)); INSERT INTO t2 VALUES (1,1,1,50), (1,2,3,40), (2,1,3,4); EXPLAIN SELECT DISTINCT a FROM t2; EXPLAIN SELECT DISTINCT a,a FROM t2; @@ -525,3 +526,17 @@ SELECT COUNT(*) FROM (SELECT DISTINCT a FROM t2 WHERE a='oe' COLLATE latin1_german2_ci) dt; DROP TABLE t1, t2; + +# +# Bug #25551: inconsistent behaviour in grouping NULL, depending on index type +# +CREATE TABLE t1 (a INT, UNIQUE (a)); +INSERT INTO t1 VALUES (4),(null),(2),(1),(null),(3); +EXPLAIN SELECT DISTINCT a FROM t1; +#result must have one row with NULL +SELECT DISTINCT a FROM t1; +EXPLAIN SELECT a FROM t1 GROUP BY a; +#result must have one row with NULL +SELECT a FROM t1 GROUP BY a; + +DROP TABLE t1; diff --git a/sql/sql_select.cc b/sql/sql_select.cc index b03c4556279..1486cf521a5 100644 --- a/sql/sql_select.cc +++ b/sql/sql_select.cc @@ -848,10 +848,11 @@ JOIN::optimize() } /* Check if we can optimize away GROUP BY/DISTINCT. - We can do that if there are no aggregate functions and the + We can do that if there are no aggregate functions, the fields in DISTINCT clause (if present) and/or columns in GROUP BY (if present) contain direct references to all key parts of - an unique index (in whatever order). + an unique index (in whatever order) and if the key parts of the + unique index cannot contain NULLs. Note that the unique keys for DISTINCT and GROUP BY should not be the same (as long as they are unique). @@ -11856,7 +11857,7 @@ test_if_subkey(ORDER *order, TABLE *table, uint ref, uint ref_key_parts, /* - Check if GROUP BY/DISTINCT can be optimized away because the set is + Check if GROUP BY/DISTINCT can be optimized away because the set is already known to be distinct. SYNOPSIS @@ -11864,7 +11865,7 @@ test_if_subkey(ORDER *order, TABLE *table, uint ref, uint ref_key_parts, table The table to operate on. find_func function to iterate over the list and search for a field - + DESCRIPTION Used in removing the GROUP BY/DISTINCT of the following types of statements: @@ -11875,12 +11876,13 @@ test_if_subkey(ORDER *order, TABLE *table, uint ref, uint ref_key_parts, then ,{whatever} is also distinct This function checks if all the key parts of any of the unique keys - of the table are referenced by a list : either the select list + of the table are referenced by a list : either the select list through find_field_in_item_list or GROUP BY list through find_field_in_order_list. - If the above holds then we can safely remove the GROUP BY/DISTINCT, + If the above holds and the key parts cannot contain NULLs then we + can safely remove the GROUP BY/DISTINCT, as no result set can be more distinct than an unique key. - + RETURN VALUE 1 found 0 not found. @@ -11903,7 +11905,8 @@ list_contains_unique_index(TABLE *table, key_part < key_part_end; key_part++) { - if (!find_func(key_part->field, data)) + if (key_part->field->maybe_null() || + !find_func(key_part->field, data)) break; } if (key_part == key_part_end) From e8040084a5363c49fc56d0a9db3ca2ed828a9e69 Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 31 Jan 2007 16:45:33 +0400 Subject: [PATCH 11/17] bug #25973 (ps_1general.test fails in embedded server) mysql-test/t/ps_1general.test: replace_result fixed --- mysql-test/t/ps_1general.test | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mysql-test/t/ps_1general.test b/mysql-test/t/ps_1general.test index 33b86dde9ed..d4e6a62c09e 100644 --- a/mysql-test/t/ps_1general.test +++ b/mysql-test/t/ps_1general.test @@ -588,7 +588,7 @@ prepare stmt1 from ' rename table t5 to t6, t7 to t8 ' ; create table t5 (a int) ; # rename must fail, t7 does not exist # Clean up the filename here because embedded server reports whole path ---replace_result \\ / $MYSQL_TEST_DIR . /var/master-data/ / t7.frm t7 +--replace_result \\ / $MYSQLTEST_VARDIR . /master-data/ / t7.frm t7 --error 1017 execute stmt1 ; create table t7 (a int) ; From fbc16a85c2e91385c8d4cce7ab32d092025c76ad Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 31 Jan 2007 16:04:38 +0200 Subject: [PATCH 12/17] BUG#25575: ERROR 1052 (Column in from clause is ambiguous) with sub-join Two problems here: Problem 1: While constructing the join columns list the optimizer does as follows: 1. Sets the join_using_fields/natural_join members of the right JOIN operand. 2. Makes a "table reference" (TABLE_LIST) to parent the two tables. 3. Assigns the join_using_fields/is_natural_join of the wrapper table using join_using_fields/natural_join of the rightmost table 4. Sets join_using_fields to NULL for the right JOIN operand. 5. Passes the parent table up to the same procedure on the upper level. Step 1 overrides the the join_using_fields that are set for a nested join wrapping table in step 4. Fixed by making a designated variable SELECT_LEX::prev_join_using to pass the data from step 1 to step 4 without destroying the wrapping table data. Problem 2: The optimizer checks for ambiguous columns while transforming NATURAL JOIN/JOIN USING to JOIN ON. While doing that there was no distinction between columns that are used in the generated join condition (where ambiguity can be checked) and the other columns (where ambiguity can be checked only when resolving references coming from outside the JOIN construct itself). Fixed by allowing the non-USING columns to be present in multiple copies in both sides of the join and moving the ambiguity check to the place where unqualified references to the join columns are resolved (find_field_in_natural_join()). mysql-test/r/join_nested.result: BUG#25575: ERROR 1052 (Column in from clause is ambiguous) with sub-join - test case mysql-test/t/join_nested.test: BUG#25575: ERROR 1052 (Column in from clause is ambiguous) with sub-join - test case sql/mysql_priv.h: BUG#25575: ERROR 1052 (Column in from clause is ambiguous) with sub-join - use SELECT_LEX to store the ref to JOIN USING list needed by the parser sql/sql_base.cc: BUG#25575: ERROR 1052 (Column in from clause is ambiguous) with sub-join - proper check for duplicate cols - more detailed debug output sql/sql_lex.h: BUG#25575: ERROR 1052 (Column in from clause is ambiguous) with sub-join - use SELECT_LEX to store the ref to JOIN USING list needed by the parser sql/sql_parse.cc: BUG#25575: ERROR 1052 (Column in from clause is ambiguous) with sub-join - proper check for duplicate cols in JOIN USING sql/sql_yacc.yy: BUG#25575: ERROR 1052 (Column in from clause is ambiguous) with sub-join - use SELECT_LEX to store the ref to JOIN USING list needed by the parser sql/table.cc: BUG#25575: ERROR 1052 (Column in from clause is ambiguous) with sub-join - return null if no table ref (as in nested join columns). --- mysql-test/r/join_nested.result | 28 ++++++++++++++++++ mysql-test/t/join_nested.test | 39 +++++++++++++++++++++++++ sql/mysql_priv.h | 3 +- sql/sql_base.cc | 51 ++++++++++++++++++++++++++------- sql/sql_lex.h | 14 +++++++++ sql/sql_parse.cc | 13 ++++----- sql/sql_yacc.yy | 16 +++++++---- sql/table.cc | 1 + 8 files changed, 140 insertions(+), 25 deletions(-) diff --git a/mysql-test/r/join_nested.result b/mysql-test/r/join_nested.result index f5c98f383b7..006488f9d43 100644 --- a/mysql-test/r/join_nested.result +++ b/mysql-test/r/join_nested.result @@ -1605,3 +1605,31 @@ WHERE t1.id='5'; id ct pc nm 5 NULL NULL NULL DROP TABLE t1,t2,t3,t4; +CREATE TABLE t1 (a INT, b INT); +CREATE TABLE t2 (a INT); +CREATE TABLE t3 (a INT, c INT); +CREATE TABLE t4 (a INT, c INT); +CREATE TABLE t5 (a INT, c INT); +SELECT b FROM t1 JOIN (t2 LEFT JOIN t3 USING (a) LEFT JOIN t4 USING (a) +LEFT JOIN t5 USING (a)) USING (a); +b +SELECT c FROM t1 JOIN (t2 LEFT JOIN t3 USING (a) LEFT JOIN t4 USING (a) +LEFT JOIN t5 USING (a)) USING (a); +ERROR 23000: Column 'c' in field list is ambiguous +SELECT b FROM t1 JOIN (t2 JOIN t3 USING (a) JOIN t4 USING (a) +JOIN t5 USING (a)) USING (a); +b +SELECT c FROM t1 JOIN (t2 JOIN t3 USING (a) JOIN t4 USING (a) +JOIN t5 USING (a)) USING (a); +ERROR 23000: Column 'c' in field list is ambiguous +DROP TABLE t1,t2,t3,t4,t5; +CREATE TABLE t1 (a INT, b INT); +CREATE TABLE t2 (a INT, b INT); +CREATE TABLE t3 (a INT, b INT); +INSERT INTO t1 VALUES (1,1); +INSERT INTO t2 VALUES (1,1); +INSERT INTO t3 VALUES (1,1); +SELECT * FROM t1 JOIN (t2 JOIN t3 USING (b)) USING (a); +ERROR 23000: Column 'a' in from clause is ambiguous +DROP TABLE t1,t2,t3; +End of 5.0 tests diff --git a/mysql-test/t/join_nested.test b/mysql-test/t/join_nested.test index e7405418be7..f29366797f6 100644 --- a/mysql-test/t/join_nested.test +++ b/mysql-test/t/join_nested.test @@ -1045,3 +1045,42 @@ SELECT t1.*, t4.nm WHERE t1.id='5'; DROP TABLE t1,t2,t3,t4; + +# +# BUG#25575: ERROR 1052 (Column in from clause is ambiguous) with sub-join +# +CREATE TABLE t1 (a INT, b INT); +CREATE TABLE t2 (a INT); +CREATE TABLE t3 (a INT, c INT); +CREATE TABLE t4 (a INT, c INT); +CREATE TABLE t5 (a INT, c INT); + +SELECT b FROM t1 JOIN (t2 LEFT JOIN t3 USING (a) LEFT JOIN t4 USING (a) +LEFT JOIN t5 USING (a)) USING (a); + +--error ER_NON_UNIQ_ERROR +SELECT c FROM t1 JOIN (t2 LEFT JOIN t3 USING (a) LEFT JOIN t4 USING (a) +LEFT JOIN t5 USING (a)) USING (a); + +SELECT b FROM t1 JOIN (t2 JOIN t3 USING (a) JOIN t4 USING (a) +JOIN t5 USING (a)) USING (a); + +--error ER_NON_UNIQ_ERROR +SELECT c FROM t1 JOIN (t2 JOIN t3 USING (a) JOIN t4 USING (a) +JOIN t5 USING (a)) USING (a); + +DROP TABLE t1,t2,t3,t4,t5; +CREATE TABLE t1 (a INT, b INT); +CREATE TABLE t2 (a INT, b INT); +CREATE TABLE t3 (a INT, b INT); + +INSERT INTO t1 VALUES (1,1); +INSERT INTO t2 VALUES (1,1); +INSERT INTO t3 VALUES (1,1); + +--error ER_NON_UNIQ_ERROR +SELECT * FROM t1 JOIN (t2 JOIN t3 USING (b)) USING (a); + +DROP TABLE t1,t2,t3; + +--echo End of 5.0 tests diff --git a/sql/mysql_priv.h b/sql/mysql_priv.h index caf3e6479f9..b4f2e712f33 100644 --- a/sql/mysql_priv.h +++ b/sql/mysql_priv.h @@ -982,7 +982,8 @@ bool push_new_name_resolution_context(THD *thd, TABLE_LIST *left_op, TABLE_LIST *right_op); void add_join_on(TABLE_LIST *b,Item *expr); -void add_join_natural(TABLE_LIST *a,TABLE_LIST *b,List *using_fields); +void add_join_natural(TABLE_LIST *a,TABLE_LIST *b,List *using_fields, + SELECT_LEX *lex); bool add_proc_to_list(THD *thd, Item *item); TABLE *unlink_open_table(THD *thd,TABLE *list,TABLE *find); void update_non_unique_table_error(TABLE_LIST *update, diff --git a/sql/sql_base.cc b/sql/sql_base.cc index 0949d4aa331..ad9cd5985d1 100644 --- a/sql/sql_base.cc +++ b/sql/sql_base.cc @@ -2945,7 +2945,7 @@ find_field_in_natural_join(THD *thd, TABLE_LIST *table_ref, const char *name, { List_iterator_fast field_it(*(table_ref->join_columns)); - Natural_join_column *nj_col; + Natural_join_column *nj_col, *curr_nj_col; Field *found_field; Query_arena *arena, backup; DBUG_ENTER("find_field_in_natural_join"); @@ -2956,14 +2956,21 @@ find_field_in_natural_join(THD *thd, TABLE_LIST *table_ref, const char *name, LINT_INIT(found_field); - for (;;) + for (nj_col= NULL, curr_nj_col= field_it++; curr_nj_col; + curr_nj_col= field_it++) { - if (!(nj_col= field_it++)) - DBUG_RETURN(NULL); - - if (!my_strcasecmp(system_charset_info, nj_col->name(), name)) - break; + if (!my_strcasecmp(system_charset_info, curr_nj_col->name(), name)) + { + if (nj_col) + { + my_error(ER_NON_UNIQ_ERROR, MYF(0), name, thd->where); + DBUG_RETURN(NULL); + } + nj_col= curr_nj_col; + } } + if (!nj_col) + DBUG_RETURN(NULL); if (nj_col->view_field) { @@ -3774,9 +3781,16 @@ mark_common_columns(THD *thd, TABLE_LIST *table_ref_1, TABLE_LIST *table_ref_2, { bool found= FALSE; const char *field_name_1; + /* true if field_name_1 is a member of using_fields */ + bool is_using_column_1; if (!(nj_col_1= it_1.get_or_create_column_ref(leaf_1))) goto err; field_name_1= nj_col_1->name(); + is_using_column_1= using_fields && + test_if_string_in_list(field_name_1, using_fields); + DBUG_PRINT ("info", ("field_name_1=%s.%s", + nj_col_1->table_name() ? nj_col_1->table_name() : "", + field_name_1)); /* Find a field with the same name in table_ref_2. @@ -3793,6 +3807,10 @@ mark_common_columns(THD *thd, TABLE_LIST *table_ref_1, TABLE_LIST *table_ref_2, if (!(cur_nj_col_2= it_2.get_or_create_column_ref(leaf_2))) goto err; cur_field_name_2= cur_nj_col_2->name(); + DBUG_PRINT ("info", ("cur_field_name_2=%s.%s", + cur_nj_col_2->table_name() ? + cur_nj_col_2->table_name() : "", + cur_field_name_2)); /* Compare the two columns and check for duplicate common fields. @@ -3800,10 +3818,16 @@ mark_common_columns(THD *thd, TABLE_LIST *table_ref_1, TABLE_LIST *table_ref_2, table_ref_2 (then found == TRUE), or if a field in table_ref_2 was already matched by some previous field in table_ref_1 (then cur_nj_col_2->is_common == TRUE). + Note that it is too early to check the columns outside of the + USING list for ambiguity because they are not actually "referenced" + here. These columns must be checked only on unqualified reference + by name (e.g. in SELECT list). */ if (!my_strcasecmp(system_charset_info, field_name_1, cur_field_name_2)) { - if (found || cur_nj_col_2->is_common) + DBUG_PRINT ("info", ("match c1.is_common=%d", nj_col_1->is_common)); + if (cur_nj_col_2->is_common || + (found && (!using_fields || is_using_column_1))) { my_error(ER_NON_UNIQ_ERROR, MYF(0), field_name_1, thd->where); goto err; @@ -3829,9 +3853,7 @@ mark_common_columns(THD *thd, TABLE_LIST *table_ref_1, TABLE_LIST *table_ref_2, clause (if present), mark them as common fields, and add a new equi-join condition to the ON clause. */ - if (nj_col_2 && - (!using_fields || - test_if_string_in_list(field_name_1, using_fields))) + if (nj_col_2 && (!using_fields ||is_using_column_1)) { Item *item_1= nj_col_1->create_item(thd); Item *item_2= nj_col_2->create_item(thd); @@ -3886,6 +3908,13 @@ mark_common_columns(THD *thd, TABLE_LIST *table_ref_1, TABLE_LIST *table_ref_2, eq_cond); nj_col_1->is_common= nj_col_2->is_common= TRUE; + DBUG_PRINT ("info", ("%s.%s and %s.%s are common", + nj_col_1->table_name() ? + nj_col_1->table_name() : "", + nj_col_1->name(), + nj_col_2->table_name() ? + nj_col_2->table_name() : "", + nj_col_2->name())); if (field_1) { diff --git a/sql/sql_lex.h b/sql/sql_lex.h index 1bf37c92b85..83daec89a50 100644 --- a/sql/sql_lex.h +++ b/sql/sql_lex.h @@ -587,6 +587,20 @@ public: int cur_pos_in_select_list; List udf_list; /* udf function calls stack */ + /* + This is a copy of the original JOIN USING list that comes from + the parser. The parser : + 1. Sets the natural_join of the second TABLE_LIST in the join + and the st_select_lex::prev_join_using. + 2. Makes a parent TABLE_LIST and sets its is_natural_join/ + join_using_fields members. + 3. Uses the wrapper TABLE_LIST as a table in the upper level. + We cannot assign directly to join_using_fields in the parser because + at stage (1.) the parent TABLE_LIST is not constructed yet and + the assignment will override the JOIN USING fields of the lower level + joins on the right. + */ + List *prev_join_using; void init_query(); void init_select(); diff --git a/sql/sql_parse.cc b/sql/sql_parse.cc index 007c5edd673..5c307a7260e 100644 --- a/sql/sql_parse.cc +++ b/sql/sql_parse.cc @@ -6365,11 +6365,8 @@ TABLE_LIST *st_select_lex::nest_last_join(THD *thd) If this is a JOIN ... USING, move the list of joined fields to the table reference that describes the join. */ - if (table->join_using_fields) - { - ptr->join_using_fields= table->join_using_fields; - table->join_using_fields= NULL; - } + if (prev_join_using) + ptr->join_using_fields= prev_join_using; } } join_list->push_front(ptr); @@ -6625,6 +6622,7 @@ void add_join_on(TABLE_LIST *b, Item *expr) a Left join argument b Right join argument using_fields Field names from USING clause + lex The current st_select_lex IMPLEMENTATION This function marks that table b should be joined with a either via @@ -6653,10 +6651,11 @@ void add_join_on(TABLE_LIST *b, Item *expr) None */ -void add_join_natural(TABLE_LIST *a, TABLE_LIST *b, List *using_fields) +void add_join_natural(TABLE_LIST *a, TABLE_LIST *b, List *using_fields, + SELECT_LEX *lex) { b->natural_join= a; - b->join_using_fields= using_fields; + lex->prev_join_using= using_fields; } diff --git a/sql/sql_yacc.yy b/sql/sql_yacc.yy index 90dc6d54fe1..bc58a14a9f9 100644 --- a/sql/sql_yacc.yy +++ b/sql/sql_yacc.yy @@ -5280,11 +5280,11 @@ join_table: YYERROR_UNLESS($1 && $3); } '(' using_list ')' - { add_join_natural($1,$3,$7); $$=$3; } + { add_join_natural($1,$3,$7,Select); $$=$3; } | table_ref NATURAL JOIN_SYM table_factor { YYERROR_UNLESS($1 && ($$=$4)); - add_join_natural($1,$4,NULL); + add_join_natural($1,$4,NULL,Select); } /* LEFT JOIN variants */ @@ -5311,11 +5311,15 @@ join_table: YYERROR_UNLESS($1 && $5); } USING '(' using_list ')' - { add_join_natural($1,$5,$9); $5->outer_join|=JOIN_TYPE_LEFT; $$=$5; } + { + add_join_natural($1,$5,$9,Select); + $5->outer_join|=JOIN_TYPE_LEFT; + $$=$5; + } | table_ref NATURAL LEFT opt_outer JOIN_SYM table_factor { YYERROR_UNLESS($1 && $6); - add_join_natural($1,$6,NULL); + add_join_natural($1,$6,NULL,Select); $6->outer_join|=JOIN_TYPE_LEFT; $$=$6; } @@ -5349,12 +5353,12 @@ join_table: LEX *lex= Lex; if (!($$= lex->current_select->convert_right_join())) YYABORT; - add_join_natural($$,$5,$9); + add_join_natural($$,$5,$9,Select); } | table_ref NATURAL RIGHT opt_outer JOIN_SYM table_factor { YYERROR_UNLESS($1 && $6); - add_join_natural($6,$1,NULL); + add_join_natural($6,$1,NULL,Select); LEX *lex= Lex; if (!($$= lex->current_select->convert_right_join())) YYABORT; diff --git a/sql/table.cc b/sql/table.cc index 2a492a15722..d8aeed6f54b 100644 --- a/sql/table.cc +++ b/sql/table.cc @@ -2628,6 +2628,7 @@ Field *Natural_join_column::field() const char *Natural_join_column::table_name() { + DBUG_ASSERT(table_ref); return table_ref->alias; } From 036314d917fc149f529724332d80a74f95ab959a Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 31 Jan 2007 19:52:27 +0400 Subject: [PATCH 13/17] merging mysql-test/t/ps_1general.test: replace_result fixed --- mysql-test/t/ps_1general.test | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mysql-test/t/ps_1general.test b/mysql-test/t/ps_1general.test index 7a09bf88e3e..a9d4488b1be 100644 --- a/mysql-test/t/ps_1general.test +++ b/mysql-test/t/ps_1general.test @@ -579,7 +579,7 @@ prepare stmt1 from ' rename table t5 to t6, t7 to t8 ' ; create table t5 (a int) ; # rename must fail, t7 does not exist # Clean up the filename here because embedded server reports whole path ---replace_result \\ / $MYSQLTEST_VARDIR . /master-data/ / t7.frm t7 +--replace_result \\ / $MYSQLTEST_VARDIR . /master-data/ "" t7.frm t7 --error 1017 execute stmt1 ; create table t7 (a int) ; From c15b2e4152854f48ba3f43ace52103010bca603d Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 31 Jan 2007 19:31:36 -0800 Subject: [PATCH 14/17] Fixed bug #25407. The bug could cause choosing a sub-optimal execution plan for a single-table query if a unique index with many null keys were defined for the table. It happened because the code of the check_quick_keys function made an assumption that any key may occur in an unique index only once. Yet this is not true for keys with nulls that may have multiple occurrences in the index. mysql-test/r/null_key.result: Fixed bug #25407 Adjusted result after the fix. mysql-test/r/select.result: Added a test case for bug #25407. mysql-test/t/select.test: Added a test case for bug #25407. --- mysql-test/r/null_key.result | 4 +- mysql-test/r/select.result | 86 ++++++++++++++++++++++++++++++++++++ mysql-test/t/select.test | 74 +++++++++++++++++++++++++++++++ sql/opt_range.cc | 12 ++++- 4 files changed, 173 insertions(+), 3 deletions(-) diff --git a/mysql-test/r/null_key.result b/mysql-test/r/null_key.result index 6eb3cf312a0..0b4ed15f659 100644 --- a/mysql-test/r/null_key.result +++ b/mysql-test/r/null_key.result @@ -30,7 +30,7 @@ id select_type table type possible_keys key key_len ref rows Extra 1 SIMPLE t1 ref a,b a 5 const 3 Using where; Using index explain select * from t1 where a is null and b=9 or a is null and b=7 limit 3; id select_type table type possible_keys key key_len ref rows Extra -1 SIMPLE t1 ref a,b a 5 const 2 Using where; Using index +1 SIMPLE t1 range a,b a 9 NULL 3 Using where; Using index explain select * from t1 where a > 1 and a < 3 limit 1; id select_type table type possible_keys key key_len ref rows Extra 1 SIMPLE t1 range a a 5 NULL 1 Using where; Using index @@ -258,7 +258,7 @@ INSERT INTO t1 VALUES (1,NULL),(2,NULL),(3,1),(4,2),(5,NULL),(6,NULL),(7,3),(8,4 INSERT INTO t2 VALUES (1,NULL),(2,NULL),(3,1),(4,2),(5,NULL),(6,NULL),(7,3),(8,4),(9,NULL),(10,NULL); explain select id from t1 where uniq_id is null; id select_type table type possible_keys key key_len ref rows Extra -1 SIMPLE t1 ref idx1 idx1 5 const 1 Using where +1 SIMPLE t1 ref idx1 idx1 5 const 5 Using where explain select id from t1 where uniq_id =1; id select_type table type possible_keys key key_len ref rows Extra 1 SIMPLE t1 const idx1 idx1 5 const 1 diff --git a/mysql-test/r/select.result b/mysql-test/r/select.result index 5dcf830ca97..f3938fd6413 100644 --- a/mysql-test/r/select.result +++ b/mysql-test/r/select.result @@ -3642,3 +3642,89 @@ INSERT into t1 values (1), (2), (3); SELECT * FROM t1 LIMIT 2, -1; ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-1' at line 1 DROP TABLE t1; +CREATE TABLE t1 ( +ID_with_null int NULL, +ID_better int NOT NULL, +INDEX idx1 (ID_with_null), +INDEX idx2 (ID_better) +); +INSERT INTO t1 VALUES (1,1), (2,1), (null,3), (null,3), (null,3), (null,3); +INSERT INTO t1 SELECT * FROM t1 WHERE ID_with_null IS NULL; +INSERT INTO t1 SELECT * FROM t1 WHERE ID_with_null IS NULL; +INSERT INTO t1 SELECT * FROM t1 WHERE ID_with_null IS NULL; +INSERT INTO t1 SELECT * FROM t1 WHERE ID_with_null IS NULL; +INSERT INTO t1 SELECT * FROM t1 WHERE ID_with_null IS NULL; +SELECT COUNT(*) FROM t1 WHERE ID_with_null IS NULL; +COUNT(*) +128 +SELECT COUNT(*) FROM t1 WHERE ID_better=1; +COUNT(*) +2 +EXPLAIN SELECT * FROM t1 WHERE ID_better=1 AND ID_with_null IS NULL; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE t1 ref idx1,idx2 idx2 4 const 1 Using where +DROP INDEX idx1 ON t1; +CREATE UNIQUE INDEX idx1 ON t1(ID_with_null); +EXPLAIN SELECT * FROM t1 WHERE ID_better=1 AND ID_with_null IS NULL; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE t1 ref idx1,idx2 idx2 4 const 1 Using where +DROP TABLE t1; +CREATE TABLE t1 ( +ID1_with_null int NULL, +ID2_with_null int NULL, +ID_better int NOT NULL, +INDEX idx1 (ID1_with_null, ID2_with_null), +INDEX idx2 (ID_better) +); +INSERT INTO t1 VALUES (1,1,1), (2,2,1), (3,null,3), (null,3,3), (null,null,3), +(3,null,3), (null,3,3), (null,null,3), (3,null,3), (null,3,3), (null,null,3); +INSERT INTO t1 SELECT * FROM t1 WHERE ID1_with_null IS NULL; +INSERT INTO t1 SELECT * FROM t1 WHERE ID2_with_null IS NULL; +INSERT INTO t1 SELECT * FROM t1 WHERE ID1_with_null IS NULL; +INSERT INTO t1 SELECT * FROM t1 WHERE ID2_with_null IS NULL; +INSERT INTO t1 SELECT * FROM t1 WHERE ID1_with_null IS NULL; +INSERT INTO t1 SELECT * FROM t1 WHERE ID2_with_null IS NULL; +SELECT COUNT(*) FROM t1 WHERE ID1_with_null IS NULL AND ID2_with_null=3; +COUNT(*) +24 +SELECT COUNT(*) FROM t1 WHERE ID1_with_null=3 AND ID2_with_null IS NULL; +COUNT(*) +24 +SELECT COUNT(*) FROM t1 WHERE ID1_with_null IS NULL AND ID2_with_null IS NULL; +COUNT(*) +192 +SELECT COUNT(*) FROM t1 WHERE ID_better=1; +COUNT(*) +2 +EXPLAIN SELECT * FROM t1 +WHERE ID_better=1 AND ID1_with_null IS NULL AND ID2_with_null=3 ; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE t1 ref idx1,idx2 idx2 4 const 1 Using where +EXPLAIN SELECT * FROM t1 +WHERE ID_better=1 AND ID1_with_null=3 AND ID2_with_null=3 IS NULL ; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE t1 ref idx1,idx2 idx2 4 const 1 Using where +EXPLAIN SELECT * FROM t1 +WHERE ID_better=1 AND ID1_with_null IS NULL AND ID2_with_null IS NULL; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE t1 ref idx1,idx2 idx2 4 const 1 Using where +DROP INDEX idx1 ON t1; +CREATE UNIQUE INDEX idx1 ON t1(ID1_with_null,ID2_with_null); +EXPLAIN SELECT * FROM t1 +WHERE ID_better=1 AND ID1_with_null IS NULL AND ID2_with_null=3 ; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE t1 ref idx1,idx2 idx2 4 const 1 Using where +EXPLAIN SELECT * FROM t1 +WHERE ID_better=1 AND ID1_with_null=3 AND ID2_with_null IS NULL ; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE t1 ref idx1,idx2 idx2 4 const 1 Using where +EXPLAIN SELECT * FROM t1 +WHERE ID_better=1 AND ID1_with_null IS NULL AND ID2_with_null IS NULL; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE t1 ref idx1,idx2 idx2 4 const 1 Using where +EXPLAIN SELECT * FROM t1 +WHERE ID_better=1 AND ID1_with_null IS NULL AND +(ID2_with_null=1 OR ID2_with_null=2); +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE t1 ref idx1,idx2 idx2 4 const 1 Using where +DROP TABLE t1; diff --git a/mysql-test/t/select.test b/mysql-test/t/select.test index 19b625451d4..19c7d742f5b 100644 --- a/mysql-test/t/select.test +++ b/mysql-test/t/select.test @@ -3133,3 +3133,77 @@ SELECT * FROM t1 LIMIT 2, -1; DROP TABLE t1; +# +# 25407: wrong estimate of NULL keys for unique indexes +# + +CREATE TABLE t1 ( + ID_with_null int NULL, + ID_better int NOT NULL, + INDEX idx1 (ID_with_null), + INDEX idx2 (ID_better) +); + +INSERT INTO t1 VALUES (1,1), (2,1), (null,3), (null,3), (null,3), (null,3); +INSERT INTO t1 SELECT * FROM t1 WHERE ID_with_null IS NULL; +INSERT INTO t1 SELECT * FROM t1 WHERE ID_with_null IS NULL; +INSERT INTO t1 SELECT * FROM t1 WHERE ID_with_null IS NULL; +INSERT INTO t1 SELECT * FROM t1 WHERE ID_with_null IS NULL; +INSERT INTO t1 SELECT * FROM t1 WHERE ID_with_null IS NULL; + +SELECT COUNT(*) FROM t1 WHERE ID_with_null IS NULL; +SELECT COUNT(*) FROM t1 WHERE ID_better=1; + +EXPLAIN SELECT * FROM t1 WHERE ID_better=1 AND ID_with_null IS NULL; + +DROP INDEX idx1 ON t1; +CREATE UNIQUE INDEX idx1 ON t1(ID_with_null); + +EXPLAIN SELECT * FROM t1 WHERE ID_better=1 AND ID_with_null IS NULL; + +DROP TABLE t1; + +CREATE TABLE t1 ( + ID1_with_null int NULL, + ID2_with_null int NULL, + ID_better int NOT NULL, + INDEX idx1 (ID1_with_null, ID2_with_null), + INDEX idx2 (ID_better) +); + +INSERT INTO t1 VALUES (1,1,1), (2,2,1), (3,null,3), (null,3,3), (null,null,3), + (3,null,3), (null,3,3), (null,null,3), (3,null,3), (null,3,3), (null,null,3); + +INSERT INTO t1 SELECT * FROM t1 WHERE ID1_with_null IS NULL; +INSERT INTO t1 SELECT * FROM t1 WHERE ID2_with_null IS NULL; +INSERT INTO t1 SELECT * FROM t1 WHERE ID1_with_null IS NULL; +INSERT INTO t1 SELECT * FROM t1 WHERE ID2_with_null IS NULL; +INSERT INTO t1 SELECT * FROM t1 WHERE ID1_with_null IS NULL; +INSERT INTO t1 SELECT * FROM t1 WHERE ID2_with_null IS NULL; + +SELECT COUNT(*) FROM t1 WHERE ID1_with_null IS NULL AND ID2_with_null=3; +SELECT COUNT(*) FROM t1 WHERE ID1_with_null=3 AND ID2_with_null IS NULL; +SELECT COUNT(*) FROM t1 WHERE ID1_with_null IS NULL AND ID2_with_null IS NULL; +SELECT COUNT(*) FROM t1 WHERE ID_better=1; + +EXPLAIN SELECT * FROM t1 + WHERE ID_better=1 AND ID1_with_null IS NULL AND ID2_with_null=3 ; +EXPLAIN SELECT * FROM t1 + WHERE ID_better=1 AND ID1_with_null=3 AND ID2_with_null=3 IS NULL ; +EXPLAIN SELECT * FROM t1 + WHERE ID_better=1 AND ID1_with_null IS NULL AND ID2_with_null IS NULL; + +DROP INDEX idx1 ON t1; +CREATE UNIQUE INDEX idx1 ON t1(ID1_with_null,ID2_with_null); + +EXPLAIN SELECT * FROM t1 + WHERE ID_better=1 AND ID1_with_null IS NULL AND ID2_with_null=3 ; +EXPLAIN SELECT * FROM t1 + WHERE ID_better=1 AND ID1_with_null=3 AND ID2_with_null IS NULL ; +EXPLAIN SELECT * FROM t1 + WHERE ID_better=1 AND ID1_with_null IS NULL AND ID2_with_null IS NULL; +EXPLAIN SELECT * FROM t1 + WHERE ID_better=1 AND ID1_with_null IS NULL AND + (ID2_with_null=1 OR ID2_with_null=2); + +DROP TABLE t1; diff --git a/sql/opt_range.cc b/sql/opt_range.cc index 04bc24b718a..f0af4b7db2a 100644 --- a/sql/opt_range.cc +++ b/sql/opt_range.cc @@ -193,6 +193,8 @@ public: } inline void merge_flags(SEL_ARG *arg) { maybe_flag|=arg->maybe_flag; } inline void maybe_smaller() { maybe_flag=1; } + /* Return true iff it's a single-point null interval */ + inline bool is_null_interval() { return maybe_null && max_value[0] == 1; } inline int cmp_min_to_min(SEL_ARG* arg) { return sel_cmp(field,min_value, arg->min_value, min_flag, arg->min_flag); @@ -452,6 +454,7 @@ typedef struct st_qsel_param { bool is_ror_scan; /* Number of ranges in the last checked tree->key */ uint n_ranges; + uint8 first_null_comp; /* first null component if any, 0 - otherwise */ } PARAM; class TABLE_READ_PLAN; @@ -5619,6 +5622,7 @@ check_quick_select(PARAM *param,uint idx,SEL_ARG *tree) DBUG_ENTER("check_quick_select"); param->is_ror_scan= FALSE; + param->first_null_comp= 0; if (!tree) DBUG_RETURN(HA_POS_ERROR); // Can't use it @@ -5710,6 +5714,7 @@ check_quick_keys(PARAM *param,uint idx,SEL_ARG *key_tree, ha_rows records=0, tmp; uint tmp_min_flag, tmp_max_flag, keynr, min_key_length, max_key_length; char *tmp_min_key, *tmp_max_key; + uint8 save_first_null_comp= param->first_null_comp; param->max_key_part=max(param->max_key_part,key_tree->part); if (key_tree->left != &null_element) @@ -5747,6 +5752,9 @@ check_quick_keys(PARAM *param,uint idx,SEL_ARG *key_tree, param->is_ror_scan= FALSE; } + if (!param->first_null_comp && key_tree->is_null_interval()) + param->first_null_comp= key_tree->part+1; + if (key_tree->next_key_part && key_tree->next_key_part->part == key_tree->part+1 && key_tree->next_key_part->type == SEL_ARG::KEY_RANGE) @@ -5790,7 +5798,8 @@ check_quick_keys(PARAM *param,uint idx,SEL_ARG *key_tree, (param->table->key_info[keynr].flags & (HA_NOSAME | HA_END_SPACE_KEY)) == HA_NOSAME && min_key_length == max_key_length && - !memcmp(param->min_key,param->max_key,min_key_length)) + !memcmp(param->min_key,param->max_key,min_key_length) && + !param->first_null_comp) { tmp=1; // Max one record param->n_ranges++; @@ -5865,6 +5874,7 @@ check_quick_keys(PARAM *param,uint idx,SEL_ARG *key_tree, return tmp; records+=tmp; } + param->first_null_comp= save_first_null_comp; return records; } From 0c523325120825d6462f5c82ddf40d9b41c8b527 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 1 Feb 2007 13:25:50 +0200 Subject: [PATCH 15/17] trigger.result: merge of the 5.0-opt tree mysql-test/r/trigger.result: merge of the 5.0-opt tree --- mysql-test/r/trigger.result | 50 ++++++++++++++++++------------------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/mysql-test/r/trigger.result b/mysql-test/r/trigger.result index 340b775cb85..fe0e444f87d 100644 --- a/mysql-test/r/trigger.result +++ b/mysql-test/r/trigger.result @@ -1241,6 +1241,31 @@ i j 2 2 13 13 drop table t1; +CREATE TABLE t1 (a INT PRIMARY KEY); +CREATE TABLE t2 (a INT PRIMARY KEY); +INSERT INTO t1 VALUES (1),(2),(3),(4),(5),(6),(7),(8); +CREATE TRIGGER trg_t1 BEFORE DELETE on t1 FOR EACH ROW +INSERT INTO t2 VALUES (OLD.a); +FLUSH STATUS; +TRUNCATE t1; +SHOW STATUS LIKE 'handler_delete'; +Variable_name Value +Handler_delete 0 +SELECT COUNT(*) FROM t2; +COUNT(*) +0 +INSERT INTO t1 VALUES (1),(2),(3),(4),(5),(6),(7),(8); +DELETE FROM t2; +FLUSH STATUS; +DELETE FROM t1; +SHOW STATUS LIKE 'handler_delete'; +Variable_name Value +Handler_delete 8 +SELECT COUNT(*) FROM t2; +COUNT(*) +8 +DROP TRIGGER trg_t1; +DROP TABLE t1,t2; drop table if exists t1; drop function if exists f1; create table t1 (i int); @@ -1310,29 +1335,4 @@ SELECT fubar_id FROM t2; fubar_id 1 DROP TABLE t1,t2; -CREATE TABLE t1 (a INT PRIMARY KEY); -CREATE TABLE t2 (a INT PRIMARY KEY); -INSERT INTO t1 VALUES (1),(2),(3),(4),(5),(6),(7),(8); -CREATE TRIGGER trg_t1 BEFORE DELETE on t1 FOR EACH ROW -INSERT INTO t2 VALUES (OLD.a); -FLUSH STATUS; -TRUNCATE t1; -SHOW STATUS LIKE 'handler_delete'; -Variable_name Value -Handler_delete 0 -SELECT COUNT(*) FROM t2; -COUNT(*) -0 -INSERT INTO t1 VALUES (1),(2),(3),(4),(5),(6),(7),(8); -DELETE FROM t2; -FLUSH STATUS; -DELETE FROM t1; -SHOW STATUS LIKE 'handler_delete'; -Variable_name Value -Handler_delete 8 -SELECT COUNT(*) FROM t2; -COUNT(*) -8 -DROP TRIGGER trg_t1; -DROP TABLE t1,t2; End of 5.0 tests From a30830460794651c0e9fc5ec4779cf77680514ee Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 1 Feb 2007 19:12:45 +0400 Subject: [PATCH 16/17] Bug#23299 Some queries against INFORMATION_SCHEMA with subqueries fail additional call of file->extra() method with HA_EXTRA_NO_CACHE parameter mysql-test/r/information_schema.result: test result mysql-test/t/information_schema.test: test case sql/sql_show.cc: additional call of file->extra() method with HA_EXTRA_NO_CACHE parameter --- mysql-test/r/information_schema.result | 10 ++++++++++ mysql-test/t/information_schema.test | 12 ++++++++++++ sql/sql_show.cc | 1 + 3 files changed, 23 insertions(+) diff --git a/mysql-test/r/information_schema.result b/mysql-test/r/information_schema.result index 21d7bfb1b21..b93a4c28849 100644 --- a/mysql-test/r/information_schema.result +++ b/mysql-test/r/information_schema.result @@ -1269,3 +1269,13 @@ id select_type table type possible_keys key key_len ref rows Extra 1 PRIMARY system NULL NULL NULL NULL 0 const row not found 2 DERIVED tables ALL NULL NULL NULL NULL 2 drop view v1; +create table t1 (f1 int(11)); +create table t2 (f1 int(11), f2 int(11)); +select table_name from information_schema.tables +where table_schema = 'test' and table_name not in +(select table_name from information_schema.columns +where table_schema = 'test' and column_name = 'f3'); +table_name +t1 +t2 +drop table t1,t2; diff --git a/mysql-test/t/information_schema.test b/mysql-test/t/information_schema.test index 623ccee49e4..d1dd485e21c 100644 --- a/mysql-test/t/information_schema.test +++ b/mysql-test/t/information_schema.test @@ -987,4 +987,16 @@ explain select * from v1; explain select * from (select table_name from information_schema.tables) as a; drop view v1; +# +# Bug#23299 Some queries against INFORMATION_SCHEMA with subqueries fail +# +create table t1 (f1 int(11)); +create table t2 (f1 int(11), f2 int(11)); + +select table_name from information_schema.tables +where table_schema = 'test' and table_name not in +(select table_name from information_schema.columns + where table_schema = 'test' and column_name = 'f3'); +drop table t1,t2; + # End of 5.0 tests. diff --git a/sql/sql_show.cc b/sql/sql_show.cc index 21a5ead90af..23059ac545a 100644 --- a/sql/sql_show.cc +++ b/sql/sql_show.cc @@ -3973,6 +3973,7 @@ bool get_schema_tables_result(JOIN *join) if (is_subselect) // is subselect { + table_list->table->file->extra(HA_EXTRA_NO_CACHE); table_list->table->file->extra(HA_EXTRA_RESET_STATE); table_list->table->file->delete_all_rows(); free_io_cache(table_list->table); From c01bbd33d79ac5f7ba822a3c8cadb0c31364e366 Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 2 Feb 2007 14:26:53 +0400 Subject: [PATCH 17/17] after merge fix mysql-test/r/ndb_read_multi_range.result: result fix mysql-test/t/ndb_read_multi_range.test: test case fix --- mysql-test/r/ndb_read_multi_range.result | 6 +++--- mysql-test/t/ndb_read_multi_range.test | 6 +++--- sql/opt_range.cc | 2 +- sql/sql_delete.cc | 4 ++-- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/mysql-test/r/ndb_read_multi_range.result b/mysql-test/r/ndb_read_multi_range.result index 8bc0f519cde..12f3185bb1a 100644 --- a/mysql-test/r/ndb_read_multi_range.result +++ b/mysql-test/r/ndb_read_multi_range.result @@ -417,12 +417,12 @@ a b c 9199 9200 NULL 223456 223457 NULL 245651 245652 2005-12-08 15:58:27 -select c, count(*) +select t21.c, count(*) from t21 inner join t22 using (a) where t22.b in (2,256,257,1121,1134,4102,9200,223457,245652) -group by c -order by c; +group by t21.c +order by t21.c; c count(*) NULL 7 2005-12-08 15:58:27 1 diff --git a/mysql-test/t/ndb_read_multi_range.test b/mysql-test/t/ndb_read_multi_range.test index c9059bcbd6b..9d7415880ce 100644 --- a/mysql-test/t/ndb_read_multi_range.test +++ b/mysql-test/t/ndb_read_multi_range.test @@ -249,12 +249,12 @@ t22.a in (select t12.a from t11, t12 where t11.a in(255,256) and t11.a = t12.a a delete from t22 where a > 245651; update t22 set b = a + 1; select * from t22 order by 1,2,3; -select c, count(*) +select t21.c, count(*) from t21 inner join t22 using (a) where t22.b in (2,256,257,1121,1134,4102,9200,223457,245652) -group by c -order by c; +group by t21.c +order by t21.c; DROP TABLE t1, t11, t12, t21, t22; diff --git a/sql/opt_range.cc b/sql/opt_range.cc index 1e18b2d48bd..733203b3160 100644 --- a/sql/opt_range.cc +++ b/sql/opt_range.cc @@ -563,7 +563,7 @@ public: /* Number of ranges in the last checked tree->key */ uint n_ranges; uint8 first_null_comp; /* first null component if any, 0 - otherwise */ -} PARAM; +}; class TABLE_READ_PLAN; class TRP_RANGE; diff --git a/sql/sql_delete.cc b/sql/sql_delete.cc index 7daf3be1578..ea8c0e2d83e 100644 --- a/sql/sql_delete.cc +++ b/sql/sql_delete.cc @@ -75,8 +75,8 @@ bool mysql_delete(THD *thd, TABLE_LIST *table_list, COND *conds, if (!using_limit && const_cond && (!conds || conds->val_int()) && !(specialflag & (SPECIAL_NO_NEW_FUNC | SPECIAL_SAFE_MODE)) && (thd->lex->sql_command == SQLCOM_TRUNCATE || - !(table->triggers && table->triggers->has_delete_triggers())) - ) + !(table->triggers && table->triggers->has_delete_triggers())) && + !thd->current_stmt_binlog_row_based) { /* Update the table->file->stats.records number */ table->file->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);