From 38b7ede9c6146884c17fe9290a0913d2a95fc548 Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 29 Nov 2005 19:17:39 +0100 Subject: [PATCH 1/4] BUG#5390 - problems with merge tables Problem #1: INSERT...SELECT INSERT ... SELECT with the same table on both sides (hidden below a MERGE table) does now work by buffering the select result. The duplicate detection works now after open_and_lock_tables() on the locks. I did not find a test case that failed without the change in sql_update.cc. I made the change anyway as it should in theory fix a possible MERGE table problem with multi-table update. mysql-test/r/create.result: BUG#5390 - problems with merge tables Removed a duplicate test. mysql-test/r/merge.result: BUG#5390 - problems with merge tables Problem #1: INSERT...SELECT Added test results. mysql-test/t/create.test: BUG#5390 - problems with merge tables Removed a duplicate test. mysql-test/t/merge.test: BUG#5390 - problems with merge tables Problem #1: INSERT...SELECT Added tests. sql/lock.cc: BUG#5390 - problems with merge tables Problem #1: INSERT...SELECT Added a new function to find a duplicate lock in a list of tables. sql/mysql_priv.h: BUG#5390 - problems with merge tables Problem #1: INSERT...SELECT Added a declaration for the new function. sql/sql_parse.cc: BUG#5390 - problems with merge tables Problem #1: INSERT...SELECT Changed the duplicate tables detection for INSERT ... SELECT to use the new function, which does also work for MERGE tables. sql/sql_update.cc: BUG#5390 - problems with merge tables Changed the duplicate tables detection for UPDATE to use the new function, which does also work for MERGE tables. --- mysql-test/r/create.result | 11 ----- mysql-test/r/merge.result | 49 +++++++++++++++++++++ mysql-test/t/create.test | 7 --- mysql-test/t/merge.test | 26 +++++++++++ sql/lock.cc | 89 ++++++++++++++++++++++++++++++++++++-- sql/mysql_priv.h | 1 + sql/sql_parse.cc | 11 ++--- sql/sql_update.cc | 2 +- 8 files changed, 169 insertions(+), 27 deletions(-) diff --git a/mysql-test/r/create.result b/mysql-test/r/create.result index c99ad8960dc..b362cdf7f58 100644 --- a/mysql-test/r/create.result +++ b/mysql-test/r/create.result @@ -189,17 +189,6 @@ select * from t1; 0 1 2 0 0 1 drop table t1; -create table t1 select 1,2,3; -create table if not exists t1 select 1,2; -create table if not exists t1 select 1,2,3,4; -Column count doesn't match value count at row 1 -create table if not exists t1 select 1; -select * from t1; -1 2 3 -1 2 3 -0 1 2 -0 0 1 -drop table t1; create table t1 (a int not null, b int, primary key (a)); insert into t1 values (1,1); create table if not exists t1 select 2; diff --git a/mysql-test/r/merge.result b/mysql-test/r/merge.result index ffb715e3903..c6e9ec0d46f 100644 --- a/mysql-test/r/merge.result +++ b/mysql-test/r/merge.result @@ -645,3 +645,52 @@ select min(a), max(a) from t1; min(a) max(a) 9999999999.9990 9999999999.9990 drop table t1, t2, t3; +create table t1(a int); +create table t2(a int); +insert into t1 values (1); +insert into t2 values (2); +create table t3 (a int) engine=merge union=(t1, t2) insert_method=first; +select * from t3; +a +1 +2 +insert t2 select * from t2; +select * from t2; +a +2 +2 +insert t3 select * from t1; +select * from t3; +a +1 +1 +2 +2 +insert t1 select * from t3; +select * from t1; +a +1 +1 +1 +1 +2 +2 +select * from t2; +a +2 +2 +select * from t3; +a +1 +1 +1 +1 +2 +2 +2 +2 +check table t1, t2; +Table Op Msg_type Msg_text +test.t1 check status OK +test.t2 check status OK +drop table t1, t2, t3; diff --git a/mysql-test/t/create.test b/mysql-test/t/create.test index ed2c76932da..644f4af15eb 100644 --- a/mysql-test/t/create.test +++ b/mysql-test/t/create.test @@ -151,13 +151,6 @@ drop table t1; # bug #1434 # -create table t1 select 1,2,3; -create table if not exists t1 select 1,2; ---error 1136 -create table if not exists t1 select 1,2,3,4; -create table if not exists t1 select 1; -select * from t1; -drop table t1; create table t1 select 1,2,3; create table if not exists t1 select 1,2; --error 1136 diff --git a/mysql-test/t/merge.test b/mysql-test/t/merge.test index a3e3d487c67..ef12227d8f1 100644 --- a/mysql-test/t/merge.test +++ b/mysql-test/t/merge.test @@ -297,3 +297,29 @@ select min(a), max(a) from t1; flush tables; select min(a), max(a) from t1; drop table t1, t2, t3; + +# +# BUG#5390 - problems with merge tables +# Problem #1: INSERT...SELECT +# +#drop table if exists t1, t2, t3; +create table t1(a int); +create table t2(a int); +insert into t1 values (1); +insert into t2 values (2); +create table t3 (a int) engine=merge union=(t1, t2) insert_method=first; +select * from t3; +# +insert t2 select * from t2; +select * from t2; +# +insert t3 select * from t1; +select * from t3; +# +insert t1 select * from t3; +select * from t1; +select * from t2; +select * from t3; +check table t1, t2; +drop table t1, t2, t3; + diff --git a/sql/lock.cc b/sql/lock.cc index 47ccc44952d..8944f23977f 100644 --- a/sql/lock.cc +++ b/sql/lock.cc @@ -390,6 +390,88 @@ MYSQL_LOCK *mysql_lock_merge(MYSQL_LOCK *a,MYSQL_LOCK *b) } +/* + Find duplicate lock in tables. + + SYNOPSIS + mysql_lock_have_duplicate() + thd The current thread. + table The table to check for duplicate lock. + tables The list of tables to search for the dup lock. + + NOTE + This is mainly meant for MERGE tables in INSERT ... SELECT + situations. The 'real', underlying tables can be found only after + the table is opened. The easier way is to check this after the + tables are locked. + + RETURN + 1 A table from 'tables' matches a lock on 'table'. + 0 No duplicate lock is present. + -1 Error. +*/ + +int mysql_lock_have_duplicate(THD *thd, TABLE *table, TABLE_LIST *tables) +{ + uint count; + MYSQL_LOCK *sql_lock1; + MYSQL_LOCK *sql_lock2; + TABLE **tables1= &table; + TABLE **tables2; + TABLE **table_ptr; + TABLE_LIST *tablist2; + TABLE *write_lock_used; + THR_LOCK_DATA **lock_data1; + THR_LOCK_DATA **end_data1; + THR_LOCK_DATA **lock_data2; + THR_LOCK_DATA **end_data2; + THR_LOCK *lock1; + DBUG_ENTER("mysql_lock_have_duplicate"); + + if (! (sql_lock1= get_lock_data(thd, tables1, 1, 1, &write_lock_used))) + goto err0; + + count=0; + for (tablist2 = tables; tablist2; tablist2= tablist2->next) + count++; + if (! (tables2= (TABLE**) sql_alloc(sizeof(TABLE*) * count))) + goto err1; + table_ptr= tables2; + for (tablist2 = tables; tablist2; tablist2= tablist2->next) + *(table_ptr++)= tablist2->table; + if (! (sql_lock2= get_lock_data(thd, tables2, count, 1, &write_lock_used))) + goto err1; + + count= 1; + for (lock_data1= sql_lock1->locks, + end_data1= lock_data1 + sql_lock1->lock_count; + lock_data1 < end_data1; + lock_data1++) + { + lock1= (*lock_data1)->lock; + for (lock_data2= sql_lock2->locks, + end_data2= lock_data2 + sql_lock2->lock_count; + lock_data2 < end_data2; + lock_data2++) + { + if ((*lock_data2)->lock == lock1) + goto end; + } + } + count= 0; + + end: + my_free((gptr) sql_lock2, MYF(0)); + my_free((gptr) sql_lock1, MYF(0)); + DBUG_RETURN(count); + + err1: + my_free((gptr) sql_lock1, MYF(0)); + err0: + DBUG_RETURN(-1); +} + + /* unlock a set of external */ static int unlock_external(THD *thd, TABLE **table,uint count) @@ -426,6 +508,7 @@ static MYSQL_LOCK *get_lock_data(THD *thd, TABLE **table_ptr, uint count, MYSQL_LOCK *sql_lock; THR_LOCK_DATA **locks; TABLE **to; + DBUG_ENTER("get_lock_data"); *write_lock_used=0; for (i=tables=lock_count=0 ; i < count ; i++) @@ -441,7 +524,7 @@ static MYSQL_LOCK *get_lock_data(THD *thd, TABLE **table_ptr, uint count, my_malloc(sizeof(*sql_lock)+ sizeof(THR_LOCK_DATA*)*tables+sizeof(table_ptr)*lock_count, MYF(0)))) - return 0; + DBUG_RETURN(0); locks=sql_lock->locks=(THR_LOCK_DATA**) (sql_lock+1); to=sql_lock->table=(TABLE**) (locks+tables); sql_lock->table_count=lock_count; @@ -461,13 +544,13 @@ static MYSQL_LOCK *get_lock_data(THD *thd, TABLE **table_ptr, uint count, { my_error(ER_OPEN_AS_READONLY,MYF(0),table->table_name); my_free((gptr) sql_lock,MYF(0)); - return 0; + DBUG_RETURN(0); } } locks=table->file->store_lock(thd, locks, get_old_locks ? TL_IGNORE : lock_type); } - return sql_lock; + DBUG_RETURN(sql_lock); } /***************************************************************************** diff --git a/sql/mysql_priv.h b/sql/mysql_priv.h index 62e65ea95da..3972a01f7ba 100644 --- a/sql/mysql_priv.h +++ b/sql/mysql_priv.h @@ -788,6 +788,7 @@ void mysql_lock_remove(THD *thd, MYSQL_LOCK *locked,TABLE *table); void mysql_lock_abort(THD *thd, TABLE *table); bool mysql_lock_abort_for_thread(THD *thd, TABLE *table); MYSQL_LOCK *mysql_lock_merge(MYSQL_LOCK *a,MYSQL_LOCK *b); +int mysql_lock_have_duplicate(THD *thd, TABLE *table, TABLE_LIST *tables); bool lock_global_read_lock(THD *thd); void unlock_global_read_lock(THD *thd); bool wait_if_global_read_lock(THD *thd, bool abort_on_refresh, bool is_not_commit); diff --git a/sql/sql_parse.cc b/sql/sql_parse.cc index aac442d35a2..f49e257a85a 100644 --- a/sql/sql_parse.cc +++ b/sql/sql_parse.cc @@ -2078,11 +2078,6 @@ mysql_execute_command(void) if (thd->select_limit < select_lex->select_limit) thd->select_limit= HA_POS_ERROR; // No limit - if (check_dup(tables->db, tables->real_name, tables->next)) - { - /* Using same table for INSERT and SELECT */ - select_lex->options |= OPTION_BUFFER_RESULT; - } { /* TODO: Delete the following loop when locks is set by sql_yacc */ TABLE_LIST *table; @@ -2095,6 +2090,12 @@ mysql_execute_command(void) (byte*) (((TABLE_LIST *) lex->select_lex.table_list.first)->next); if (!(res=open_and_lock_tables(thd, tables))) { + /* MERGE sub-tables can only be detected after open. */ + if (mysql_lock_have_duplicate(thd, tables->table, tables->next)) + { + /* Using same table for INSERT and SELECT */ + select_lex->options |= OPTION_BUFFER_RESULT; + } if ((result=new select_insert(tables->table,&lex->field_list, lex->duplicates))) res=handle_select(thd,lex,result); diff --git a/sql/sql_update.cc b/sql/sql_update.cc index b2b10a9b59a..888b475ffce 100644 --- a/sql/sql_update.cc +++ b/sql/sql_update.cc @@ -712,7 +712,7 @@ int multi_update::prepare(List ¬_used_values) { TABLE *table=table_ref->table; if (!(tables_to_update & table->map) && - check_dup(table_ref->db, table_ref->real_name, update_tables)) + mysql_lock_have_duplicate(thd, table, update_tables)) table->no_cache= 1; // Disable row cache } DBUG_RETURN(thd->fatal_error != 0); From f6cac54bf6598b2063a2cb1477994f22e86e78f6 Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 7 Dec 2005 19:52:26 +0100 Subject: [PATCH 2/4] BUG#5390 - problems with merge tables Problem #1: INSERT...SELECT, Version for 4.1. INSERT ... SELECT with the same table on both sides (hidden below a MERGE table) does now work by buffering the select result. The duplicate detection works now after open_and_lock_tables() on the locks. I did not find a test case that failed without the change in sql_update.cc. I made the change anyway as it should in theory fix a possible MERGE table problem with multi-table update. libmysqld/ha_blackhole.cc: BUG#5390 - problems with merge tables No idea, how the symlink change made it into my patch. mysql-test/r/create.result: BUG#5390 - problems with merge tables Removed a duplicate test. mysql-test/r/merge.result: BUG#5390 - problems with merge tables Problem #1: INSERT...SELECT, Version for 4.1. Added test results. mysql-test/t/create.test: BUG#5390 - problems with merge tables Removed a duplicate test. mysql-test/t/merge.test: BUG#5390 - problems with merge tables Problem #1: INSERT...SELECT, Version for 4.1. Added tests. sql/lock.cc: BUG#5390 - problems with merge tables Problem #1: INSERT...SELECT, Version for 4.1. Added a new function to find a duplicate lock in a list of tables. sql/mysql_priv.h: BUG#5390 - problems with merge tables Problem #1: INSERT...SELECT, Version for 4.1. Added a declaration for the new function. sql/sql_parse.cc: BUG#5390 - problems with merge tables Problem #1: INSERT...SELECT, Version for 4.1. Changed the duplicate tables detection for INSERT ... SELECT to use the new function, which does also work for MERGE tables. sql/sql_update.cc: BUG#5390 - problems with merge tables Problem #1: INSERT...SELECT, Version for 4.1. Changed the duplicate tables detection for UPDATE to use the new function, which does also work for MERGE tables. --- mysql-test/r/create.result | 15 ------- mysql-test/r/merge.result | 49 +++++++++++++++++++++ mysql-test/t/create.test | 7 --- mysql-test/t/merge.test | 26 +++++++++++ sql/lock.cc | 89 ++++++++++++++++++++++++++++++++++++-- sql/mysql_priv.h | 1 + sql/sql_parse.cc | 11 ++--- sql/sql_update.cc | 3 +- 8 files changed, 169 insertions(+), 32 deletions(-) diff --git a/mysql-test/r/create.result b/mysql-test/r/create.result index a933027cdb1..d096de4ff6a 100644 --- a/mysql-test/r/create.result +++ b/mysql-test/r/create.result @@ -247,21 +247,6 @@ select * from t1; 0 1 2 0 0 1 drop table t1; -create table t1 select 1,2,3; -create table if not exists t1 select 1,2; -Warnings: -Note 1050 Table 't1' already exists -create table if not exists t1 select 1,2,3,4; -ERROR 21S01: Column count doesn't match value count at row 1 -create table if not exists t1 select 1; -Warnings: -Note 1050 Table 't1' already exists -select * from t1; -1 2 3 -1 2 3 -0 1 2 -0 0 1 -drop table t1; create table t1 (a int not null, b int, primary key (a)); insert into t1 values (1,1); create table if not exists t1 select 2; diff --git a/mysql-test/r/merge.result b/mysql-test/r/merge.result index 3035908688a..038ea43cabc 100644 --- a/mysql-test/r/merge.result +++ b/mysql-test/r/merge.result @@ -717,3 +717,52 @@ SELECT b FROM t2; b 3 DROP TABLE t1, t2; +create table t1(a int); +create table t2(a int); +insert into t1 values (1); +insert into t2 values (2); +create table t3 (a int) engine=merge union=(t1, t2) insert_method=first; +select * from t3; +a +1 +2 +insert t2 select * from t2; +select * from t2; +a +2 +2 +insert t3 select * from t1; +select * from t3; +a +1 +1 +2 +2 +insert t1 select * from t3; +select * from t1; +a +1 +1 +1 +1 +2 +2 +select * from t2; +a +2 +2 +select * from t3; +a +1 +1 +1 +1 +2 +2 +2 +2 +check table t1, t2; +Table Op Msg_type Msg_text +test.t1 check status OK +test.t2 check status OK +drop table t1, t2, t3; diff --git a/mysql-test/t/create.test b/mysql-test/t/create.test index c9d16916f8a..f8570b4d373 100644 --- a/mysql-test/t/create.test +++ b/mysql-test/t/create.test @@ -200,13 +200,6 @@ drop table t1; # bug #1434 # -create table t1 select 1,2,3; -create table if not exists t1 select 1,2; ---error 1136 -create table if not exists t1 select 1,2,3,4; -create table if not exists t1 select 1; -select * from t1; -drop table t1; create table t1 select 1,2,3; create table if not exists t1 select 1,2; --error 1136 diff --git a/mysql-test/t/merge.test b/mysql-test/t/merge.test index d384c017611..a723443b395 100644 --- a/mysql-test/t/merge.test +++ b/mysql-test/t/merge.test @@ -350,4 +350,30 @@ INSERT INTO t2 (b) VALUES (1) ON DUPLICATE KEY UPDATE b=3; SELECT b FROM t2; DROP TABLE t1, t2; + +# +# BUG#5390 - problems with merge tables +# Problem #1: INSERT...SELECT +# +#drop table if exists t1, t2, t3; +create table t1(a int); +create table t2(a int); +insert into t1 values (1); +insert into t2 values (2); +create table t3 (a int) engine=merge union=(t1, t2) insert_method=first; +select * from t3; +# +insert t2 select * from t2; +select * from t2; +# +insert t3 select * from t1; +select * from t3; +# +insert t1 select * from t3; +select * from t1; +select * from t2; +select * from t3; +check table t1, t2; +drop table t1, t2, t3; + # End of 4.1 tests diff --git a/sql/lock.cc b/sql/lock.cc index 944c36d4d1e..a571b7f8ee8 100644 --- a/sql/lock.cc +++ b/sql/lock.cc @@ -394,6 +394,88 @@ MYSQL_LOCK *mysql_lock_merge(MYSQL_LOCK *a,MYSQL_LOCK *b) } +/* + Find duplicate lock in tables. + + SYNOPSIS + mysql_lock_have_duplicate() + thd The current thread. + table The table to check for duplicate lock. + tables The list of tables to search for the dup lock. + + NOTE + This is mainly meant for MERGE tables in INSERT ... SELECT + situations. The 'real', underlying tables can be found only after + the table is opened. The easier way is to check this after the + tables are locked. + + RETURN + 1 A table from 'tables' matches a lock on 'table'. + 0 No duplicate lock is present. + -1 Error. +*/ + +int mysql_lock_have_duplicate(THD *thd, TABLE *table, TABLE_LIST *tables) +{ + uint count; + MYSQL_LOCK *sql_lock1; + MYSQL_LOCK *sql_lock2; + TABLE **tables1= &table; + TABLE **tables2; + TABLE **table_ptr; + TABLE_LIST *tablist2; + TABLE *write_lock_used; + THR_LOCK_DATA **lock_data1; + THR_LOCK_DATA **end_data1; + THR_LOCK_DATA **lock_data2; + THR_LOCK_DATA **end_data2; + THR_LOCK *lock1; + DBUG_ENTER("mysql_lock_have_duplicate"); + + if (! (sql_lock1= get_lock_data(thd, tables1, 1, 1, &write_lock_used))) + goto err0; + + count=0; + for (tablist2 = tables; tablist2; tablist2= tablist2->next) + count++; + if (! (tables2= (TABLE**) sql_alloc(sizeof(TABLE*) * count))) + goto err1; + table_ptr= tables2; + for (tablist2 = tables; tablist2; tablist2= tablist2->next) + *(table_ptr++)= tablist2->table; + if (! (sql_lock2= get_lock_data(thd, tables2, count, 1, &write_lock_used))) + goto err1; + + count= 1; + for (lock_data1= sql_lock1->locks, + end_data1= lock_data1 + sql_lock1->lock_count; + lock_data1 < end_data1; + lock_data1++) + { + lock1= (*lock_data1)->lock; + for (lock_data2= sql_lock2->locks, + end_data2= lock_data2 + sql_lock2->lock_count; + lock_data2 < end_data2; + lock_data2++) + { + if ((*lock_data2)->lock == lock1) + goto end; + } + } + count= 0; + + end: + my_free((gptr) sql_lock2, MYF(0)); + my_free((gptr) sql_lock1, MYF(0)); + DBUG_RETURN(count); + + err1: + my_free((gptr) sql_lock1, MYF(0)); + err0: + DBUG_RETURN(-1); +} + + /* unlock a set of external */ static int unlock_external(THD *thd, TABLE **table,uint count) @@ -430,6 +512,7 @@ static MYSQL_LOCK *get_lock_data(THD *thd, TABLE **table_ptr, uint count, MYSQL_LOCK *sql_lock; THR_LOCK_DATA **locks; TABLE **to; + DBUG_ENTER("get_lock_data"); *write_lock_used=0; for (i=tables=lock_count=0 ; i < count ; i++) @@ -445,7 +528,7 @@ static MYSQL_LOCK *get_lock_data(THD *thd, TABLE **table_ptr, uint count, my_malloc(sizeof(*sql_lock)+ sizeof(THR_LOCK_DATA*)*tables+sizeof(table_ptr)*lock_count, MYF(0)))) - return 0; + DBUG_RETURN(0); locks=sql_lock->locks=(THR_LOCK_DATA**) (sql_lock+1); to=sql_lock->table=(TABLE**) (locks+tables); sql_lock->table_count=lock_count; @@ -465,7 +548,7 @@ static MYSQL_LOCK *get_lock_data(THD *thd, TABLE **table_ptr, uint count, { my_error(ER_OPEN_AS_READONLY,MYF(0),table->table_name); my_free((gptr) sql_lock,MYF(0)); - return 0; + DBUG_RETURN(0); } } THR_LOCK_DATA **org_locks = locks; @@ -475,7 +558,7 @@ static MYSQL_LOCK *get_lock_data(THD *thd, TABLE **table_ptr, uint count, for ( ; org_locks != locks ; org_locks++) (*org_locks)->debug_print_param= (void *) table; } - return sql_lock; + DBUG_RETURN(sql_lock); } diff --git a/sql/mysql_priv.h b/sql/mysql_priv.h index c128fac8d9e..429a71b4437 100644 --- a/sql/mysql_priv.h +++ b/sql/mysql_priv.h @@ -1022,6 +1022,7 @@ void mysql_lock_remove(THD *thd, MYSQL_LOCK *locked,TABLE *table); void mysql_lock_abort(THD *thd, TABLE *table); bool mysql_lock_abort_for_thread(THD *thd, TABLE *table); MYSQL_LOCK *mysql_lock_merge(MYSQL_LOCK *a,MYSQL_LOCK *b); +int mysql_lock_have_duplicate(THD *thd, TABLE *table, TABLE_LIST *tables); bool lock_global_read_lock(THD *thd); void unlock_global_read_lock(THD *thd); bool wait_if_global_read_lock(THD *thd, bool abort_on_refresh, bool is_not_commit); diff --git a/sql/sql_parse.cc b/sql/sql_parse.cc index fbca542dc24..90de630da60 100644 --- a/sql/sql_parse.cc +++ b/sql/sql_parse.cc @@ -2897,16 +2897,17 @@ unsent_create_error: if (unit->select_limit_cnt < select_lex->select_limit) unit->select_limit_cnt= HA_POS_ERROR; // No limit - if (find_real_table_in_list(tables->next, tables->db, tables->real_name)) + if ((res= open_and_lock_tables(thd, tables))) + break; + + insert_table= tables->table; + /* MERGE sub-tables can only be detected after open. */ + if (mysql_lock_have_duplicate(thd, insert_table, tables->next)) { /* Using same table for INSERT and SELECT */ select_lex->options |= OPTION_BUFFER_RESULT; } - if ((res= open_and_lock_tables(thd, tables))) - break; - - insert_table= tables->table; /* Skip first table, which is the table we are inserting in */ select_lex->table_list.first= (byte*) first_local_table->next; tables= (TABLE_LIST *) select_lex->table_list.first; diff --git a/sql/sql_update.cc b/sql/sql_update.cc index 05e13c64aa7..d69a6b7cdd1 100644 --- a/sql/sql_update.cc +++ b/sql/sql_update.cc @@ -854,8 +854,7 @@ int multi_update::prepare(List ¬_used_values, { TABLE *table=table_ref->table; if (!(tables_to_update & table->map) && - find_real_table_in_list(update_tables, table_ref->db, - table_ref->real_name)) + mysql_lock_have_duplicate(thd, table, update_tables)) table->no_cache= 1; // Disable row cache } DBUG_RETURN(thd->is_fatal_error != 0); From 5113bdffd0d17cb40bb0917d80128de9ded94730 Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 20 Dec 2005 16:35:05 +0100 Subject: [PATCH 3/4] BUG#5390 - problems with merge tables Problem #1: INSERT...SELECT, Version for 5.0. Extended the unique table check by a check of lock data. Merge sub-tables cannot be detected by doing name checks only. mysql-test/r/create.result: BUG#5390 - problems with merge tables Removed a duplicate test. mysql-test/r/merge.result: BUG#5390 - problems with merge tables Problem #1: INSERT...SELECT Added test results. mysql-test/t/create.test: BUG#5390 - problems with merge tables Removed a duplicate test. mysql-test/t/merge.test: BUG#5390 - problems with merge tables Problem #1: INSERT...SELECT Added tests. sql/lock.cc: BUG#5390 - problems with merge tables Problem #1: INSERT...SELECT, Version for 5.0. Added a new function to find a duplicate lock in a list of tables. sql/mysql_priv.h: BUG#5390 - problems with merge tables Problem #1: INSERT...SELECT, Version for 5.0. Added the thread handle to unique_table(). Added a declaration for the new function. sql/sql_base.cc: BUG#5390 - problems with merge tables Problem #1: INSERT...SELECT, Version for 5.0. Added the thread handle to unique_table(). Added a call to the new mysql_lock_have_duplicate(), which needs the thread handle, to unique_table(). sql/sql_delete.cc: BUG#5390 - problems with merge tables Problem #1: INSERT...SELECT, Version for 5.0. Added the thread handle to unique_table(). sql/sql_insert.cc: BUG#5390 - problems with merge tables Problem #1: INSERT...SELECT, Version for 5.0. Added the thread handle to unique_table(). sql/sql_load.cc: BUG#5390 - problems with merge tables Problem #1: INSERT...SELECT, Version for 5.0. Added the thread handle to unique_table(). sql/sql_parse.cc: BUG#5390 - problems with merge tables Problem #1: INSERT...SELECT, Version for 5.0. Added the thread handle to unique_table(). sql/sql_update.cc: BUG#5390 - problems with merge tables Problem #1: INSERT...SELECT, Version for 5.0. Added the thread handle to unique_table(). Replaced a call to find_table_in_local_list() by the newly extended unique_table(). --- mysql-test/r/create.result | 15 ----- mysql-test/r/merge.result | 49 ++++++++++++++ mysql-test/t/create.test | 7 -- mysql-test/t/merge.test | 26 ++++++++ sql/lock.cc | 128 ++++++++++++++++++++++++++++++++++++- sql/mysql_priv.h | 4 +- sql/sql_base.cc | 10 +-- sql/sql_delete.cc | 4 +- sql/sql_insert.cc | 4 +- sql/sql_load.cc | 2 +- sql/sql_parse.cc | 4 +- sql/sql_update.cc | 7 +- 12 files changed, 219 insertions(+), 41 deletions(-) diff --git a/mysql-test/r/create.result b/mysql-test/r/create.result index c981d3092a7..7dabdfe0ef5 100644 --- a/mysql-test/r/create.result +++ b/mysql-test/r/create.result @@ -259,21 +259,6 @@ select * from t1; 0 1 2 0 0 1 drop table t1; -create table t1 select 1,2,3; -create table if not exists t1 select 1,2; -Warnings: -Note 1050 Table 't1' already exists -create table if not exists t1 select 1,2,3,4; -ERROR 21S01: Column count doesn't match value count at row 1 -create table if not exists t1 select 1; -Warnings: -Note 1050 Table 't1' already exists -select * from t1; -1 2 3 -1 2 3 -0 1 2 -0 0 1 -drop table t1; create table t1 (a int not null, b int, primary key (a)); insert into t1 values (1,1); create table if not exists t1 select 2; diff --git a/mysql-test/r/merge.result b/mysql-test/r/merge.result index e028e58acf5..7e3ccc83d73 100644 --- a/mysql-test/r/merge.result +++ b/mysql-test/r/merge.result @@ -719,3 +719,52 @@ SELECT b FROM t2; b 3 DROP TABLE t1, t2; +create table t1(a int); +create table t2(a int); +insert into t1 values (1); +insert into t2 values (2); +create table t3 (a int) engine=merge union=(t1, t2) insert_method=first; +select * from t3; +a +1 +2 +insert t2 select * from t2; +select * from t2; +a +2 +2 +insert t3 select * from t1; +select * from t3; +a +1 +1 +2 +2 +insert t1 select * from t3; +select * from t1; +a +1 +1 +1 +1 +2 +2 +select * from t2; +a +2 +2 +select * from t3; +a +1 +1 +1 +1 +2 +2 +2 +2 +check table t1, t2; +Table Op Msg_type Msg_text +test.t1 check status OK +test.t2 check status OK +drop table t1, t2, t3; diff --git a/mysql-test/t/create.test b/mysql-test/t/create.test index 6f3bc67cb30..4e99b0c4b8b 100644 --- a/mysql-test/t/create.test +++ b/mysql-test/t/create.test @@ -211,13 +211,6 @@ drop table t1; # bug #1434 # -create table t1 select 1,2,3; -create table if not exists t1 select 1,2; ---error 1136 -create table if not exists t1 select 1,2,3,4; -create table if not exists t1 select 1; -select * from t1; -drop table t1; create table t1 select 1,2,3; create table if not exists t1 select 1,2; --error 1136 diff --git a/mysql-test/t/merge.test b/mysql-test/t/merge.test index ff05867b7c1..060f1ea167b 100644 --- a/mysql-test/t/merge.test +++ b/mysql-test/t/merge.test @@ -352,4 +352,30 @@ INSERT INTO t2 (b) VALUES (1) ON DUPLICATE KEY UPDATE b=3; SELECT b FROM t2; DROP TABLE t1, t2; + +# +# BUG#5390 - problems with merge tables +# Problem #1: INSERT...SELECT +# +#drop table if exists t1, t2, t3; +create table t1(a int); +create table t2(a int); +insert into t1 values (1); +insert into t2 values (2); +create table t3 (a int) engine=merge union=(t1, t2) insert_method=first; +select * from t3; +# +insert t2 select * from t2; +select * from t2; +# +insert t3 select * from t1; +select * from t3; +# +insert t1 select * from t3; +select * from t1; +select * from t2; +select * from t3; +check table t1, t2; +drop table t1, t2, t3; + # End of 4.1 tests diff --git a/sql/lock.cc b/sql/lock.cc index fe8dcb3aa5e..947fa4ea53f 100644 --- a/sql/lock.cc +++ b/sql/lock.cc @@ -423,6 +423,127 @@ MYSQL_LOCK *mysql_lock_merge(MYSQL_LOCK *a,MYSQL_LOCK *b) } +/* + Find duplicate lock in tables. + + SYNOPSIS + mysql_lock_have_duplicate() + thd The current thread. + needle The table to check for duplicate lock. + haystack The list of tables to search for the dup lock. + + NOTE + This is mainly meant for MERGE tables in INSERT ... SELECT + situations. The 'real', underlying tables can be found only after + the table is opened. The easier way is to check this after the + tables are locked. + + RETURN + 1 A table from 'tables' matches a lock on 'table'. + 0 No duplicate lock is present. + -1 Error. +*/ + +TABLE_LIST *mysql_lock_have_duplicate(THD *thd, TABLE_LIST *needle, + TABLE_LIST *haystack) +{ + uint count; + uint dup_pos; + TABLE *write_lock_used; /* dummy */ + TABLE **tables1; + TABLE **tables2; + TABLE **table_ptr; + TABLE_LIST *tlist_ptr; + MYSQL_LOCK *sql_lock1; + MYSQL_LOCK *sql_lock2; + THR_LOCK_DATA **lock_data1; + THR_LOCK_DATA **end_data1; + THR_LOCK_DATA **lock_data2; + THR_LOCK_DATA **end_data2; + THR_LOCK *lock1; + DBUG_ENTER("mysql_lock_have_duplicate"); + + /* Table may not be defined for derived or view tables. */ + if (! needle->table) + DBUG_RETURN(NULL); + + /* Get lock(s) for needle. */ + tables1= &needle->table; + if (! (sql_lock1= get_lock_data(thd, tables1, 1, 1, &write_lock_used))) + goto err0; + + /* Count real tables in list. */ + count=0; + for (tlist_ptr = haystack; tlist_ptr; tlist_ptr= tlist_ptr->next_global) + if (! tlist_ptr->placeholder() && ! tlist_ptr->schema_table) + count++; + /* Allocate a table array. */ + if (! (tables2= (TABLE**) sql_alloc(sizeof(TABLE*) * count))) + goto err1; + table_ptr= tables2; + /* Assign table pointers. */ + for (tlist_ptr = haystack; tlist_ptr; tlist_ptr= tlist_ptr->next_global) + if (! tlist_ptr->placeholder() && ! tlist_ptr->schema_table) + *(table_ptr++)= tlist_ptr->table; + /* Get lock(s) for haystack. */ + if (! (sql_lock2= get_lock_data(thd, tables2, count, 1, &write_lock_used))) + goto err1; + + /* Initialize duplicate position to an impossible value. */ + dup_pos= UINT_MAX; + /* + Find a duplicate lock. + In case of merge tables, sql_lock1 can have more than 1 lock. + */ + for (lock_data1= sql_lock1->locks, + end_data1= lock_data1 + sql_lock1->lock_count; + lock_data1 < end_data1; + lock_data1++) + { + lock1= (*lock_data1)->lock; + for (lock_data2= sql_lock2->locks, + end_data2= lock_data2 + sql_lock2->lock_count; + lock_data2 < end_data2; + lock_data2++) + { + if ((*lock_data2)->lock == lock1) + { + DBUG_PRINT("ingo", ("duplicate lock found")); + /* Change duplicate position to the real value. */ + dup_pos= lock_data2 - sql_lock2->locks; + goto end; + } + } + } + + end: + tlist_ptr= NULL; /* In case that no duplicate was found. */ + if (dup_pos != UINT_MAX) + { + /* Duplicate found. Search the matching TABLE_LIST object. */ + count= 0; + for (tlist_ptr = haystack; tlist_ptr; tlist_ptr= tlist_ptr->next_global) + { + if (! tlist_ptr->placeholder() && ! tlist_ptr->schema_table) + { + count+= tlist_ptr->table->file->lock_count(); + if (count > dup_pos) + break; + } + } + } + my_free((gptr) sql_lock2, MYF(0)); + my_free((gptr) sql_lock1, MYF(0)); + DBUG_RETURN(tlist_ptr); + + err1: + my_free((gptr) sql_lock1, MYF(0)); + err0: + /* This non-null but special value indicates error, if caller cares. */ + DBUG_RETURN(needle); +} + + /* unlock a set of external */ static int unlock_external(THD *thd, TABLE **table,uint count) @@ -460,6 +581,7 @@ static MYSQL_LOCK *get_lock_data(THD *thd, TABLE **table_ptr, uint count, MYSQL_LOCK *sql_lock; THR_LOCK_DATA **locks; TABLE **to; + DBUG_ENTER("get_lock_data"); *write_lock_used=0; for (i=tables=lock_count=0 ; i < count ; i++) @@ -488,7 +610,7 @@ static MYSQL_LOCK *get_lock_data(THD *thd, TABLE **table_ptr, uint count, my_malloc(sizeof(*sql_lock)+ sizeof(THR_LOCK_DATA*)*tables+sizeof(table_ptr)*lock_count, MYF(0)))) - return 0; + DBUG_RETURN(0); locks=sql_lock->locks=(THR_LOCK_DATA**) (sql_lock+1); to=sql_lock->table=(TABLE**) (locks+tables); sql_lock->table_count=lock_count; @@ -508,7 +630,7 @@ static MYSQL_LOCK *get_lock_data(THD *thd, TABLE **table_ptr, uint count, { my_error(ER_OPEN_AS_READONLY, MYF(0), table->alias); my_free((gptr) sql_lock,MYF(0)); - return 0; + DBUG_RETURN(0); } } THR_LOCK_DATA **org_locks = locks; @@ -518,7 +640,7 @@ static MYSQL_LOCK *get_lock_data(THD *thd, TABLE **table_ptr, uint count, for ( ; org_locks != locks ; org_locks++) (*org_locks)->debug_print_param= (void *) table; } - return sql_lock; + DBUG_RETURN(sql_lock); } diff --git a/sql/mysql_priv.h b/sql/mysql_priv.h index 3f4ed137a59..ee41acfc6a5 100644 --- a/sql/mysql_priv.h +++ b/sql/mysql_priv.h @@ -981,7 +981,7 @@ TABLE_LIST *find_table_in_list(TABLE_LIST *table, uint offset_to_list, const char *db_name, const char *table_name); -TABLE_LIST *unique_table(TABLE_LIST *table, TABLE_LIST *table_list); +TABLE_LIST *unique_table(THD *thd, TABLE_LIST *table, TABLE_LIST *table_list); TABLE **find_temporary_table(THD *thd, const char *db, const char *table_name); bool close_temporary_table(THD *thd, const char *db, const char *table_name); void close_temporary(TABLE *table, bool delete_table); @@ -1269,6 +1269,8 @@ void mysql_lock_remove(THD *thd, MYSQL_LOCK *locked,TABLE *table); void mysql_lock_abort(THD *thd, TABLE *table); bool mysql_lock_abort_for_thread(THD *thd, TABLE *table); MYSQL_LOCK *mysql_lock_merge(MYSQL_LOCK *a,MYSQL_LOCK *b); +TABLE_LIST *mysql_lock_have_duplicate(THD *thd, TABLE_LIST *needle, + TABLE_LIST *haystack); bool lock_global_read_lock(THD *thd); void unlock_global_read_lock(THD *thd); bool wait_if_global_read_lock(THD *thd, bool abort_on_refresh, diff --git a/sql/sql_base.cc b/sql/sql_base.cc index 0e1c1525c9e..92aab055aac 100644 --- a/sql/sql_base.cc +++ b/sql/sql_base.cc @@ -698,6 +698,7 @@ TABLE_LIST *find_table_in_list(TABLE_LIST *table, SYNOPSIS unique_table() + thd thread handle table table which should be checked table_list list of tables @@ -723,7 +724,7 @@ TABLE_LIST *find_table_in_list(TABLE_LIST *table, 0 if table is unique */ -TABLE_LIST* unique_table(TABLE_LIST *table, TABLE_LIST *table_list) +TABLE_LIST* unique_table(THD *thd, TABLE_LIST *table, TABLE_LIST *table_list) { TABLE_LIST *res; const char *d_name, *t_name; @@ -758,9 +759,10 @@ TABLE_LIST* unique_table(TABLE_LIST *table, TABLE_LIST *table_list) DBUG_PRINT("info", ("real table: %s.%s", d_name, t_name)); for (;;) { - if (!(res= find_table_in_global_list(table_list, d_name, t_name)) || - (!res->table || res->table != table->table) && - (res->select_lex && !res->select_lex->exclude_from_table_unique_test)) + if (((! (res= find_table_in_global_list(table_list, d_name, t_name))) && + (! (res= mysql_lock_have_duplicate(thd, table, table_list)))) || + ((!res->table || res->table != table->table) && + res->select_lex && !res->select_lex->exclude_from_table_unique_test)) break; /* If we found entry of this table or or table of SELECT which already diff --git a/sql/sql_delete.cc b/sql/sql_delete.cc index 983f0931ef9..b77acbcbd2a 100644 --- a/sql/sql_delete.cc +++ b/sql/sql_delete.cc @@ -348,7 +348,7 @@ bool mysql_prepare_delete(THD *thd, TABLE_LIST *table_list, Item **conds) } { TABLE_LIST *duplicate; - if ((duplicate= unique_table(table_list, table_list->next_global))) + if ((duplicate= unique_table(thd, table_list, table_list->next_global))) { update_non_unique_table_error(table_list, "DELETE", duplicate); DBUG_RETURN(TRUE); @@ -438,7 +438,7 @@ bool mysql_multi_delete_prepare(THD *thd) */ { TABLE_LIST *duplicate; - if ((duplicate= unique_table(target_tbl->correspondent_table, + if ((duplicate= unique_table(thd, target_tbl->correspondent_table, lex->query_tables))) { update_non_unique_table_error(target_tbl->correspondent_table, diff --git a/sql/sql_insert.cc b/sql/sql_insert.cc index 8903f28be11..e4d70e8f1bb 100644 --- a/sql/sql_insert.cc +++ b/sql/sql_insert.cc @@ -870,7 +870,7 @@ bool mysql_prepare_insert(THD *thd, TABLE_LIST *table_list, { Item *fake_conds= 0; TABLE_LIST *duplicate; - if ((duplicate= unique_table(table_list, table_list->next_global))) + if ((duplicate= unique_table(thd, table_list, table_list->next_global))) { update_non_unique_table_error(table_list, "INSERT", duplicate); DBUG_RETURN(TRUE); @@ -2171,7 +2171,7 @@ select_insert::prepare(List &values, SELECT_LEX_UNIT *u) query */ if (!(lex->current_select->options & OPTION_BUFFER_RESULT) && - unique_table(table_list, table_list->next_global)) + unique_table(thd, table_list, table_list->next_global)) { /* Using same table for INSERT and SELECT */ lex->current_select->options|= OPTION_BUFFER_RESULT; diff --git a/sql/sql_load.cc b/sql/sql_load.cc index 2a0145af9c7..4f3bfee5d3a 100644 --- a/sql/sql_load.cc +++ b/sql/sql_load.cc @@ -178,7 +178,7 @@ bool mysql_load(THD *thd,sql_exchange *ex,TABLE_LIST *table_list, table is marked to be 'used for insert' in which case we should never mark this table as as 'const table' (ie, one that has only one row). */ - if (unique_table(table_list, table_list->next_global)) + if (unique_table(thd, table_list, table_list->next_global)) { my_error(ER_UPDATE_TABLE_USED, MYF(0), table_list->table_name); DBUG_RETURN(TRUE); diff --git a/sql/sql_parse.cc b/sql/sql_parse.cc index 1428e50253a..a8a60772e47 100644 --- a/sql/sql_parse.cc +++ b/sql/sql_parse.cc @@ -2835,7 +2835,7 @@ mysql_execute_command(THD *thd) if (!(lex->create_info.options & HA_LEX_CREATE_TMP_TABLE)) { TABLE_LIST *duplicate; - if ((duplicate= unique_table(create_table, select_tables))) + if ((duplicate= unique_table(thd, create_table, select_tables))) { update_non_unique_table_error(create_table, "CREATE", duplicate); res= 1; @@ -2851,7 +2851,7 @@ mysql_execute_command(THD *thd) tab= tab->next_local) { TABLE_LIST *duplicate; - if ((duplicate= unique_table(tab, select_tables))) + if ((duplicate= unique_table(thd, tab, select_tables))) { update_non_unique_table_error(tab, "CREATE", duplicate); res= 1; diff --git a/sql/sql_update.cc b/sql/sql_update.cc index 94c00036540..e38848bc821 100644 --- a/sql/sql_update.cc +++ b/sql/sql_update.cc @@ -616,7 +616,7 @@ bool mysql_prepare_update(THD *thd, TABLE_LIST *table_list, /* Check that we are not using table that we are updating in a sub select */ { TABLE_LIST *duplicate; - if ((duplicate= unique_table(table_list, table_list->next_global))) + if ((duplicate= unique_table(thd, table_list, table_list->next_global))) { update_non_unique_table_error(table_list, "UPDATE", duplicate); my_error(ER_UPDATE_TABLE_USED, MYF(0), table_list->table_name); @@ -839,7 +839,7 @@ reopen_tables: tl->lock_type != TL_READ_NO_INSERT) { TABLE_LIST *duplicate; - if ((duplicate= unique_table(tl, table_list))) + if ((duplicate= unique_table(thd, tl, table_list))) { update_non_unique_table_error(table_list, "UPDATE", duplicate); DBUG_RETURN(TRUE); @@ -1026,8 +1026,7 @@ int multi_update::prepare(List ¬_used_values, { TABLE *table=table_ref->table; if (!(tables_to_update & table->map) && - find_table_in_local_list(update_tables, table_ref->db, - table_ref->table_name)) + unique_table(thd, table_ref, update_tables)) table->no_cache= 1; // Disable row cache } DBUG_RETURN(thd->is_fatal_error != 0); From 0040aff93131cd9a1330f0ad0b8a88b0de29ce04 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 22 Dec 2005 13:48:00 +0100 Subject: [PATCH 4/4] BUG#5390 - problems with merge tables Problem #1: INSERT...SELECT, Version for 5.1. Extended the unique table check by a check of lock data. Merge sub-tables cannot be detected by doing name checks only. mysql-test/r/create.result: BUG#5390 - problems with merge tables Removed a duplicate test. mysql-test/r/merge.result: BUG#5390 - problems with merge tables Problem #1: INSERT...SELECT Added test results. mysql-test/t/create.test: BUG#5390 - problems with merge tables Removed a duplicate test. mysql-test/t/merge.test: BUG#5390 - problems with merge tables Problem #1: INSERT...SELECT Added tests. sql/lock.cc: BUG#5390 - problems with merge tables Problem #1: INSERT...SELECT, Version for 5.1. Added a new function to find a duplicate lock in a list of tables. sql/mysql_priv.h: Problem #1: INSERT...SELECT, Version for 5.1. Added the thread handle to unique_table(). Added a declaration for the new function. sql/sql_base.cc: BUG#5390 - problems with merge tables Problem #1: INSERT...SELECT, Version for 5.1. Added the thread handle to unique_table(). Added a call to the new mysql_lock_have_duplicate(), which needs the thread handle, to unique_table(). sql/sql_delete.cc: BUG#5390 - problems with merge tables Problem #1: INSERT...SELECT, Version for 5.1. Added the thread handle to unique_table(). sql/sql_insert.cc: BUG#5390 - problems with merge tables Problem #1: INSERT...SELECT, Version for 5.1. Added the thread handle to unique_table(). sql/sql_load.cc: BUG#5390 - problems with merge tables Problem #1: INSERT...SELECT, Version for 5.1. Added the thread handle to unique_table(). sql/sql_parse.cc: BUG#5390 - problems with merge tables Problem #1: INSERT...SELECT, Version for 5.1. Added the thread handle to unique_table(). sql/sql_update.cc: BUG#5390 - problems with merge tables Problem #1: INSERT...SELECT, Version for 5.1. Added the thread handle to unique_table(). Replaced a call to find_table_in_local_list() by the newly extended unique_table(). --- mysql-test/r/create.result | 15 ----- mysql-test/r/merge.result | 49 +++++++++++++++ mysql-test/t/create.test | 7 --- mysql-test/t/merge.test | 26 ++++++++ sql/lock.cc | 123 ++++++++++++++++++++++++++++++++++++- sql/mysql_priv.h | 4 +- sql/sql_base.cc | 10 +-- sql/sql_delete.cc | 4 +- sql/sql_insert.cc | 4 +- sql/sql_load.cc | 2 +- sql/sql_parse.cc | 4 +- sql/sql_update.cc | 7 +-- 12 files changed, 216 insertions(+), 39 deletions(-) diff --git a/mysql-test/r/create.result b/mysql-test/r/create.result index f73d891e7f0..ae2cbfffba7 100644 --- a/mysql-test/r/create.result +++ b/mysql-test/r/create.result @@ -259,21 +259,6 @@ select * from t1; 0 1 2 0 0 1 drop table t1; -create table t1 select 1,2,3; -create table if not exists t1 select 1,2; -Warnings: -Note 1050 Table 't1' already exists -create table if not exists t1 select 1,2,3,4; -ERROR 21S01: Column count doesn't match value count at row 1 -create table if not exists t1 select 1; -Warnings: -Note 1050 Table 't1' already exists -select * from t1; -1 2 3 -1 2 3 -0 1 2 -0 0 1 -drop table t1; create table t1 (a int not null, b int, primary key (a)); insert into t1 values (1,1); create table if not exists t1 select 2; diff --git a/mysql-test/r/merge.result b/mysql-test/r/merge.result index 786c61401b2..437b001070a 100644 --- a/mysql-test/r/merge.result +++ b/mysql-test/r/merge.result @@ -722,3 +722,52 @@ SELECT b FROM t2; b 3 DROP TABLE t1, t2; +create table t1(a int); +create table t2(a int); +insert into t1 values (1); +insert into t2 values (2); +create table t3 (a int) engine=merge union=(t1, t2) insert_method=first; +select * from t3; +a +1 +2 +insert t2 select * from t2; +select * from t2; +a +2 +2 +insert t3 select * from t1; +select * from t3; +a +1 +1 +2 +2 +insert t1 select * from t3; +select * from t1; +a +1 +1 +1 +1 +2 +2 +select * from t2; +a +2 +2 +select * from t3; +a +1 +1 +1 +1 +2 +2 +2 +2 +check table t1, t2; +Table Op Msg_type Msg_text +test.t1 check status OK +test.t2 check status OK +drop table t1, t2, t3; diff --git a/mysql-test/t/create.test b/mysql-test/t/create.test index 6f3bc67cb30..4e99b0c4b8b 100644 --- a/mysql-test/t/create.test +++ b/mysql-test/t/create.test @@ -211,13 +211,6 @@ drop table t1; # bug #1434 # -create table t1 select 1,2,3; -create table if not exists t1 select 1,2; ---error 1136 -create table if not exists t1 select 1,2,3,4; -create table if not exists t1 select 1; -select * from t1; -drop table t1; create table t1 select 1,2,3; create table if not exists t1 select 1,2; --error 1136 diff --git a/mysql-test/t/merge.test b/mysql-test/t/merge.test index 520bacd8f17..9fe7bb787cb 100644 --- a/mysql-test/t/merge.test +++ b/mysql-test/t/merge.test @@ -355,4 +355,30 @@ INSERT INTO t2 (b) VALUES (1) ON DUPLICATE KEY UPDATE b=3; SELECT b FROM t2; DROP TABLE t1, t2; + +# +# BUG#5390 - problems with merge tables +# Problem #1: INSERT...SELECT +# +#drop table if exists t1, t2, t3; +create table t1(a int); +create table t2(a int); +insert into t1 values (1); +insert into t2 values (2); +create table t3 (a int) engine=merge union=(t1, t2) insert_method=first; +select * from t3; +# +insert t2 select * from t2; +select * from t2; +# +insert t3 select * from t1; +select * from t3; +# +insert t1 select * from t3; +select * from t1; +select * from t2; +select * from t3; +check table t1, t2; +drop table t1, t2, t3; + # End of 4.1 tests diff --git a/sql/lock.cc b/sql/lock.cc index 288adc19032..f544708d075 100644 --- a/sql/lock.cc +++ b/sql/lock.cc @@ -428,6 +428,127 @@ MYSQL_LOCK *mysql_lock_merge(MYSQL_LOCK *a,MYSQL_LOCK *b) } +/* + Find duplicate lock in tables. + + SYNOPSIS + mysql_lock_have_duplicate() + thd The current thread. + needle The table to check for duplicate lock. + haystack The list of tables to search for the dup lock. + + NOTE + This is mainly meant for MERGE tables in INSERT ... SELECT + situations. The 'real', underlying tables can be found only after + the table is opened. The easier way is to check this after the + tables are locked. + + RETURN + 1 A table from 'tables' matches a lock on 'table'. + 0 No duplicate lock is present. + -1 Error. +*/ + +TABLE_LIST *mysql_lock_have_duplicate(THD *thd, TABLE_LIST *needle, + TABLE_LIST *haystack) +{ + uint count; + uint dup_pos; + TABLE *write_lock_used; /* dummy */ + TABLE **tables1; + TABLE **tables2; + TABLE **table_ptr; + TABLE_LIST *tlist_ptr; + MYSQL_LOCK *sql_lock1; + MYSQL_LOCK *sql_lock2; + THR_LOCK_DATA **lock_data1; + THR_LOCK_DATA **end_data1; + THR_LOCK_DATA **lock_data2; + THR_LOCK_DATA **end_data2; + THR_LOCK *lock1; + DBUG_ENTER("mysql_lock_have_duplicate"); + + /* Table may not be defined for derived or view tables. */ + if (! needle->table) + DBUG_RETURN(NULL); + + /* Get lock(s) for needle. */ + tables1= &needle->table; + if (! (sql_lock1= get_lock_data(thd, tables1, 1, 1, &write_lock_used))) + goto err0; + + /* Count real tables in list. */ + count=0; + for (tlist_ptr = haystack; tlist_ptr; tlist_ptr= tlist_ptr->next_global) + if (! tlist_ptr->placeholder() && ! tlist_ptr->schema_table) + count++; + /* Allocate a table array. */ + if (! (tables2= (TABLE**) sql_alloc(sizeof(TABLE*) * count))) + goto err1; + table_ptr= tables2; + /* Assign table pointers. */ + for (tlist_ptr = haystack; tlist_ptr; tlist_ptr= tlist_ptr->next_global) + if (! tlist_ptr->placeholder() && ! tlist_ptr->schema_table) + *(table_ptr++)= tlist_ptr->table; + /* Get lock(s) for haystack. */ + if (! (sql_lock2= get_lock_data(thd, tables2, count, 1, &write_lock_used))) + goto err1; + + /* Initialize duplicate position to an impossible value. */ + dup_pos= UINT_MAX; + /* + Find a duplicate lock. + In case of merge tables, sql_lock1 can have more than 1 lock. + */ + for (lock_data1= sql_lock1->locks, + end_data1= lock_data1 + sql_lock1->lock_count; + lock_data1 < end_data1; + lock_data1++) + { + lock1= (*lock_data1)->lock; + for (lock_data2= sql_lock2->locks, + end_data2= lock_data2 + sql_lock2->lock_count; + lock_data2 < end_data2; + lock_data2++) + { + if ((*lock_data2)->lock == lock1) + { + DBUG_PRINT("ingo", ("duplicate lock found")); + /* Change duplicate position to the real value. */ + dup_pos= lock_data2 - sql_lock2->locks; + goto end; + } + } + } + + end: + tlist_ptr= NULL; /* In case that no duplicate was found. */ + if (dup_pos != UINT_MAX) + { + /* Duplicate found. Search the matching TABLE_LIST object. */ + count= 0; + for (tlist_ptr = haystack; tlist_ptr; tlist_ptr= tlist_ptr->next_global) + { + if (! tlist_ptr->placeholder() && ! tlist_ptr->schema_table) + { + count+= tlist_ptr->table->file->lock_count(); + if (count > dup_pos) + break; + } + } + } + my_free((gptr) sql_lock2, MYF(0)); + my_free((gptr) sql_lock1, MYF(0)); + DBUG_RETURN(tlist_ptr); + + err1: + my_free((gptr) sql_lock1, MYF(0)); + err0: + /* This non-null but special value indicates error, if caller cares. */ + DBUG_RETURN(needle); +} + + /* unlock a set of external */ static int unlock_external(THD *thd, TABLE **table,uint count) @@ -465,8 +586,8 @@ static MYSQL_LOCK *get_lock_data(THD *thd, TABLE **table_ptr, uint count, MYSQL_LOCK *sql_lock; THR_LOCK_DATA **locks; TABLE **to; - DBUG_ENTER("get_lock_data"); + DBUG_PRINT("info", ("count %d", count)); *write_lock_used=0; for (i=tables=lock_count=0 ; i < count ; i++) diff --git a/sql/mysql_priv.h b/sql/mysql_priv.h index 48d1ea8e798..c815cb22495 100644 --- a/sql/mysql_priv.h +++ b/sql/mysql_priv.h @@ -1016,7 +1016,7 @@ TABLE_LIST *find_table_in_list(TABLE_LIST *table, uint offset_to_list, const char *db_name, const char *table_name); -TABLE_LIST *unique_table(TABLE_LIST *table, TABLE_LIST *table_list); +TABLE_LIST *unique_table(THD *thd, TABLE_LIST *table, TABLE_LIST *table_list); TABLE *find_temporary_table(THD *thd, const char *db, const char *table_name); TABLE *find_temporary_table(THD *thd, TABLE_LIST *table_list); bool close_temporary_table(THD *thd, TABLE_LIST *table_list); @@ -1375,6 +1375,8 @@ void mysql_lock_remove(THD *thd, MYSQL_LOCK *locked,TABLE *table); void mysql_lock_abort(THD *thd, TABLE *table); bool mysql_lock_abort_for_thread(THD *thd, TABLE *table); MYSQL_LOCK *mysql_lock_merge(MYSQL_LOCK *a,MYSQL_LOCK *b); +TABLE_LIST *mysql_lock_have_duplicate(THD *thd, TABLE_LIST *needle, + TABLE_LIST *haystack); bool lock_global_read_lock(THD *thd); void unlock_global_read_lock(THD *thd); bool wait_if_global_read_lock(THD *thd, bool abort_on_refresh, diff --git a/sql/sql_base.cc b/sql/sql_base.cc index b9cd1afcd26..032cf485862 100644 --- a/sql/sql_base.cc +++ b/sql/sql_base.cc @@ -1246,6 +1246,7 @@ TABLE_LIST *find_table_in_list(TABLE_LIST *table, SYNOPSIS unique_table() + thd thread handle table table which should be checked table_list list of tables @@ -1271,7 +1272,7 @@ TABLE_LIST *find_table_in_list(TABLE_LIST *table, 0 if table is unique */ -TABLE_LIST* unique_table(TABLE_LIST *table, TABLE_LIST *table_list) +TABLE_LIST* unique_table(THD *thd, TABLE_LIST *table, TABLE_LIST *table_list) { TABLE_LIST *res; const char *d_name, *t_name; @@ -1306,9 +1307,10 @@ TABLE_LIST* unique_table(TABLE_LIST *table, TABLE_LIST *table_list) DBUG_PRINT("info", ("real table: %s.%s", d_name, t_name)); for (;;) { - if (!(res= find_table_in_global_list(table_list, d_name, t_name)) || - (!res->table || res->table != table->table) && - (res->select_lex && !res->select_lex->exclude_from_table_unique_test)) + if (((! (res= find_table_in_global_list(table_list, d_name, t_name))) && + (! (res= mysql_lock_have_duplicate(thd, table, table_list)))) || + ((!res->table || res->table != table->table) && + res->select_lex && !res->select_lex->exclude_from_table_unique_test)) break; /* If we found entry of this table or or table of SELECT which already diff --git a/sql/sql_delete.cc b/sql/sql_delete.cc index 745139924ab..66644abe9e3 100644 --- a/sql/sql_delete.cc +++ b/sql/sql_delete.cc @@ -374,7 +374,7 @@ bool mysql_prepare_delete(THD *thd, TABLE_LIST *table_list, Item **conds) } { TABLE_LIST *duplicate; - if ((duplicate= unique_table(table_list, table_list->next_global))) + if ((duplicate= unique_table(thd, table_list, table_list->next_global))) { update_non_unique_table_error(table_list, "DELETE", duplicate); DBUG_RETURN(TRUE); @@ -464,7 +464,7 @@ bool mysql_multi_delete_prepare(THD *thd) */ { TABLE_LIST *duplicate; - if ((duplicate= unique_table(target_tbl->correspondent_table, + if ((duplicate= unique_table(thd, target_tbl->correspondent_table, lex->query_tables))) { update_non_unique_table_error(target_tbl->correspondent_table, diff --git a/sql/sql_insert.cc b/sql/sql_insert.cc index e053f06df55..598fac102c1 100644 --- a/sql/sql_insert.cc +++ b/sql/sql_insert.cc @@ -890,7 +890,7 @@ bool mysql_prepare_insert(THD *thd, TABLE_LIST *table_list, { Item *fake_conds= 0; TABLE_LIST *duplicate; - if ((duplicate= unique_table(table_list, table_list->next_global))) + if ((duplicate= unique_table(thd, table_list, table_list->next_global))) { update_non_unique_table_error(table_list, "INSERT", duplicate); DBUG_RETURN(TRUE); @@ -2228,7 +2228,7 @@ select_insert::prepare(List &values, SELECT_LEX_UNIT *u) query */ if (!(lex->current_select->options & OPTION_BUFFER_RESULT) && - unique_table(table_list, table_list->next_global)) + unique_table(thd, table_list, table_list->next_global)) { /* Using same table for INSERT and SELECT */ lex->current_select->options|= OPTION_BUFFER_RESULT; diff --git a/sql/sql_load.cc b/sql/sql_load.cc index 70abe3e659c..3850e704718 100644 --- a/sql/sql_load.cc +++ b/sql/sql_load.cc @@ -178,7 +178,7 @@ bool mysql_load(THD *thd,sql_exchange *ex,TABLE_LIST *table_list, table is marked to be 'used for insert' in which case we should never mark this table as 'const table' (ie, one that has only one row). */ - if (unique_table(table_list, table_list->next_global)) + if (unique_table(thd, table_list, table_list->next_global)) { my_error(ER_UPDATE_TABLE_USED, MYF(0), table_list->table_name); DBUG_RETURN(TRUE); diff --git a/sql/sql_parse.cc b/sql/sql_parse.cc index 839e1dbd65f..4ebf5c4be73 100644 --- a/sql/sql_parse.cc +++ b/sql/sql_parse.cc @@ -2823,7 +2823,7 @@ mysql_execute_command(THD *thd) if (!(lex->create_info.options & HA_LEX_CREATE_TMP_TABLE)) { TABLE_LIST *duplicate; - if ((duplicate= unique_table(create_table, select_tables))) + if ((duplicate= unique_table(thd, create_table, select_tables))) { update_non_unique_table_error(create_table, "CREATE", duplicate); res= 1; @@ -2839,7 +2839,7 @@ mysql_execute_command(THD *thd) tab= tab->next_local) { TABLE_LIST *duplicate; - if ((duplicate= unique_table(tab, select_tables))) + if ((duplicate= unique_table(thd, tab, select_tables))) { update_non_unique_table_error(tab, "CREATE", duplicate); res= 1; diff --git a/sql/sql_update.cc b/sql/sql_update.cc index bd001cd9a06..65d1beeaf3b 100644 --- a/sql/sql_update.cc +++ b/sql/sql_update.cc @@ -737,7 +737,7 @@ bool mysql_prepare_update(THD *thd, TABLE_LIST *table_list, /* Check that we are not using table that we are updating in a sub select */ { TABLE_LIST *duplicate; - if ((duplicate= unique_table(table_list, table_list->next_global))) + if ((duplicate= unique_table(thd, table_list, table_list->next_global))) { update_non_unique_table_error(table_list, "UPDATE", duplicate); my_error(ER_UPDATE_TABLE_USED, MYF(0), table_list->table_name); @@ -960,7 +960,7 @@ reopen_tables: tl->lock_type != TL_READ_NO_INSERT) { TABLE_LIST *duplicate; - if ((duplicate= unique_table(tl, table_list))) + if ((duplicate= unique_table(thd, tl, table_list))) { update_non_unique_table_error(table_list, "UPDATE", duplicate); DBUG_RETURN(TRUE); @@ -1147,8 +1147,7 @@ int multi_update::prepare(List ¬_used_values, { TABLE *table=table_ref->table; if (!(tables_to_update & table->map) && - find_table_in_local_list(update_tables, table_ref->db, - table_ref->table_name)) + unique_table(thd, table_ref, update_tables)) table->no_cache= 1; // Disable row cache } DBUG_RETURN(thd->is_fatal_error != 0);