From fc809c70cc108b2f0cf0f0b32d9fdbb92d0b2f88 Mon Sep 17 00:00:00 2001 From: "malff/marcsql@weblab.(none)" <> Date: Tue, 24 Apr 2007 09:24:21 -0600 Subject: [PATCH 01/29] Bug#25411 (trigger code truncated), PART I The issue found with bug 25411 is due to the function skip_rear_comments() which damages the source code while implementing a work around. The root cause of the problem is in the lexical analyser, which does not process special comments properly. For special comments like : [1] aaa /*!50000 bbb */ ccc since 5.0 is a version older that the current code, the parser is in lining the content of the special comment, so that the query to process is [2] aaa bbb ccc However, the text of the query captured when processing a stored procedure, stored function or trigger (or event in 5.1), can be after rebuilding it: [3] aaa bbb */ ccc which is wrong. To fix bug 25411 properly, the lexical analyser needs to return [2] when in lining special comments. In order to implement this, some preliminary cleanup is required in the code, which is implemented by this patch. Before this change, the structure named LEX (or st_lex) contains attributes that belong to lexical analysis, as well as attributes that represents the abstract syntax tree (AST) of a statement. Creating a new LEX structure for each statements (which makes sense for the AST part) also re-initialized the lexical analysis phase each time, which is conceptually wrong. With this patch, the previous st_lex structure has been split in two: - st_lex represents the Abstract Syntax Tree for a statement. The name "lex" has not been changed to avoid a bigger impact in the code base. - class lex_input_stream represents the internal state of the lexical analyser, which by definition should *not* be reinitialized when parsing multiple statements from the same input stream. This change is a pre-requisite for bug 25411, since the implementation of lex_input_stream will later improve to deal properly with special comments, and this processing can not be done with the current implementation of sp_head::reset_lex and sp_head::restore_lex, which interfere with the lexer. This change set alone does not fix bug 25411. --- mysql-test/r/bdb_notembedded.result | 35 ++++ mysql-test/t/bdb_notembedded.test | 38 ++++ sql/item_func.cc | 2 +- sql/log_event.cc | 9 +- sql/mysql_priv.h | 6 +- sql/slave.cc | 3 +- sql/sp.cc | 9 +- sql/sp_head.cc | 31 +-- sql/sp_head.h | 6 +- sql/sql_class.cc | 2 +- sql/sql_class.h | 10 + sql/sql_lex.cc | 299 +++++++++++++++------------- sql/sql_lex.h | 70 +++++-- sql/sql_parse.cc | 103 +++++++--- sql/sql_prepare.cc | 8 +- sql/sql_trigger.cc | 8 +- sql/sql_view.cc | 14 +- sql/sql_yacc.yy | 171 ++++++++++------ 18 files changed, 530 insertions(+), 294 deletions(-) create mode 100644 mysql-test/r/bdb_notembedded.result create mode 100644 mysql-test/t/bdb_notembedded.test diff --git a/mysql-test/r/bdb_notembedded.result b/mysql-test/r/bdb_notembedded.result new file mode 100644 index 00000000000..14cb5fad915 --- /dev/null +++ b/mysql-test/r/bdb_notembedded.result @@ -0,0 +1,35 @@ +set autocommit=1; +reset master; +create table bug16206 (a int); +insert into bug16206 values(1); +start transaction; +insert into bug16206 values(2); +commit; +show binlog events; +Log_name Pos Event_type Server_id End_log_pos Info +f n Format_desc 1 n Server ver: VERSION, Binlog ver: 4 +f n Query 1 n use `test`; create table bug16206 (a int) +f n Query 1 n use `test`; insert into bug16206 values(1) +f n Query 1 n use `test`; insert into bug16206 values(2) +drop table bug16206; +reset master; +create table bug16206 (a int) engine= bdb; +insert into bug16206 values(0); +insert into bug16206 values(1); +start transaction; +insert into bug16206 values(2); +commit; +insert into bug16206 values(3); +show binlog events; +Log_name Pos Event_type Server_id End_log_pos Info +f n Format_desc 1 n Server ver: VERSION, Binlog ver: 4 +f n Query 1 n use `test`; create table bug16206 (a int) engine= bdb +f n Query 1 n use `test`; insert into bug16206 values(0) +f n Query 1 n use `test`; insert into bug16206 values(1) +f n Query 1 n use `test`; BEGIN +f n Query 1 n use `test`; insert into bug16206 values(2) +f n Query 1 n use `test`; COMMIT +f n Query 1 n use `test`; insert into bug16206 values(3) +drop table bug16206; +set autocommit=0; +End of 5.0 tests diff --git a/mysql-test/t/bdb_notembedded.test b/mysql-test/t/bdb_notembedded.test new file mode 100644 index 00000000000..24e64ebbfb2 --- /dev/null +++ b/mysql-test/t/bdb_notembedded.test @@ -0,0 +1,38 @@ +-- source include/not_embedded.inc +-- source include/have_bdb.inc + +# +# Bug #16206: Superfluous COMMIT event in binlog when updating BDB in autocommit mode +# +set autocommit=1; + +let $VERSION=`select version()`; + +reset master; +create table bug16206 (a int); +insert into bug16206 values(1); +start transaction; +insert into bug16206 values(2); +commit; +--replace_result $VERSION VERSION +--replace_column 1 f 2 n 5 n +show binlog events; +drop table bug16206; + +reset master; +create table bug16206 (a int) engine= bdb; +insert into bug16206 values(0); +insert into bug16206 values(1); +start transaction; +insert into bug16206 values(2); +commit; +insert into bug16206 values(3); +--replace_result $VERSION VERSION +--replace_column 1 f 2 n 5 n +show binlog events; +drop table bug16206; + +set autocommit=0; + + +--echo End of 5.0 tests diff --git a/sql/item_func.cc b/sql/item_func.cc index d972bde9155..7e4a06a1805 100644 --- a/sql/item_func.cc +++ b/sql/item_func.cc @@ -4272,7 +4272,7 @@ int get_var_with_binlog(THD *thd, enum_sql_command sql_command, List tmp_var_list; LEX *sav_lex= thd->lex, lex_tmp; thd->lex= &lex_tmp; - lex_start(thd, NULL, 0); + lex_start(thd); tmp_var_list.push_back(new set_var_user(new Item_func_set_user_var(name, new Item_null()))); /* Create the variable */ diff --git a/sql/log_event.cc b/sql/log_event.cc index 8bb63e72bde..173ca6232ee 100644 --- a/sql/log_event.cc +++ b/sql/log_event.cc @@ -1879,7 +1879,8 @@ int Query_log_event::exec_event(struct st_relay_log_info* rli, thd->variables.collation_database= thd->db_charset; /* Execute the query (note that we bypass dispatch_command()) */ - mysql_parse(thd, thd->query, thd->query_length); + const char* found_semicolon= NULL; + mysql_parse(thd, thd->query, thd->query_length, &found_semicolon); } else @@ -2987,10 +2988,12 @@ int Load_log_event::exec_event(NET* net, struct st_relay_log_info* rli, thd->query_error= 0; clear_all_errors(thd, rli); /* - Usually mysql_init_query() is called by mysql_parse(), but we need it here + Usually lex_start() is called by mysql_parse(), but we need it here as the present method does not call mysql_parse(). */ - mysql_init_query(thd, 0, 0); + lex_start(thd); + mysql_reset_thd_for_next_command(thd); + if (!use_rli_only_for_errors) { /* Saved for InnoDB, see comment in Query_log_event::exec_event() */ diff --git a/sql/mysql_priv.h b/sql/mysql_priv.h index de567eacbeb..4017457b644 100644 --- a/sql/mysql_priv.h +++ b/sql/mysql_priv.h @@ -697,13 +697,15 @@ bool do_rename(THD *thd, TABLE_LIST *ren_table, char *new_db, char *new_table_name, char *new_table_alias, bool skip_error); bool mysql_change_db(THD *thd,const char *name,bool no_access_check); -void mysql_parse(THD *thd,char *inBuf,uint length); + +void mysql_parse(THD *thd, const char *inBuf, uint length, + const char ** semicolon); + bool mysql_test_parse_for_slave(THD *thd,char *inBuf,uint length); bool is_update_query(enum enum_sql_command command); bool alloc_query(THD *thd, const char *packet, uint packet_length); void mysql_init_select(LEX *lex); void mysql_reset_thd_for_next_command(THD *thd); -void mysql_init_query(THD *thd, uchar *buf, uint length); bool mysql_new_select(LEX *lex, bool move_down); void create_select_for_variable(const char *var_name); void mysql_init_multi_delete(LEX *lex); diff --git a/sql/slave.cc b/sql/slave.cc index ba8c3ff902a..2b07f28b167 100644 --- a/sql/slave.cc +++ b/sql/slave.cc @@ -1517,6 +1517,7 @@ static int create_table_from_dump(THD* thd, MYSQL *mysql, const char* db, handler *file; ulonglong save_options; NET *net= &mysql->net; + const char *found_semicolon= NULL; DBUG_ENTER("create_table_from_dump"); packet_len= my_net_read(net); // read create table statement @@ -1567,7 +1568,7 @@ static int create_table_from_dump(THD* thd, MYSQL *mysql, const char* db, save_db_length= thd->db_length; DBUG_ASSERT(db != 0); thd->reset_db((char*)db, strlen(db)); - mysql_parse(thd, thd->query, packet_len); // run create table + mysql_parse(thd, thd->query, packet_len, &found_semicolon); // run create table thd->db = save_db; // leave things the way the were before thd->db_length= save_db_length; thd->options = save_options; diff --git a/sql/sp.cc b/sql/sp.cc index c8701aa2c87..a17ac9f30aa 100644 --- a/sql/sp.cc +++ b/sql/sp.cc @@ -434,10 +434,15 @@ db_load_routine(THD *thd, int type, sp_name *name, sp_head **sphp, if ((ret= sp_use_new_db(thd, name->m_db, &old_db, 1, &dbchanged))) goto end; - lex_start(thd, (uchar*)defstr.c_ptr(), defstr.length()); + { + Lex_input_stream lip(thd, defstr.c_ptr(), defstr.length()); + thd->m_lip= &lip; + lex_start(thd); + ret= MYSQLparse(thd); + } thd->spcont= 0; - if (MYSQLparse(thd) || thd->is_fatal_error || newlex.sphead == NULL) + if (ret || thd->is_fatal_error || newlex.sphead == NULL) { sp_head *sp= newlex.sphead; diff --git a/sql/sp_head.cc b/sql/sp_head.cc index 21225d82188..548a6c903dc 100644 --- a/sql/sp_head.cc +++ b/sql/sp_head.cc @@ -519,9 +519,10 @@ void sp_head::init_strings(THD *thd, LEX *lex) { DBUG_ENTER("sp_head::init_strings"); - uchar *endp; /* Used to trim the end */ + const char *endp; /* Used to trim the end */ /* During parsing, we must use thd->mem_root */ MEM_ROOT *root= thd->mem_root; + Lex_input_stream *lip=thd->m_lip; if (m_param_begin && m_param_end) { @@ -531,17 +532,17 @@ sp_head::init_strings(THD *thd, LEX *lex) } /* If ptr has overrun end_of_query then end_of_query is the end */ - endp= (lex->ptr > lex->end_of_query ? lex->end_of_query : lex->ptr); + endp= (lip->ptr > lip->end_of_query ? lip->end_of_query : lip->ptr); /* Trim "garbage" at the end. This is sometimes needed with the "/ * ! VERSION... * /" wrapper in dump files. */ - endp= skip_rear_comments(m_body_begin, endp); + endp= skip_rear_comments((char*) m_body_begin, (char*) endp); m_body.length= endp - m_body_begin; - m_body.str= strmake_root(root, (char *)m_body_begin, m_body.length); - m_defstr.length= endp - lex->buf; - m_defstr.str= strmake_root(root, (char *)lex->buf, m_defstr.length); + m_body.str= strmake_root(root, m_body_begin, m_body.length); + m_defstr.length= endp - lip->buf; + m_defstr.str= strmake_root(root, lip->buf, m_defstr.length); DBUG_VOID_RETURN; } @@ -1756,24 +1757,13 @@ sp_head::reset_lex(THD *thd) DBUG_ENTER("sp_head::reset_lex"); LEX *sublex; LEX *oldlex= thd->lex; - my_lex_states org_next_state= oldlex->next_state; (void)m_lex.push_front(oldlex); thd->lex= sublex= new st_lex; - /* Reset most stuff. The length arguments doesn't matter here. */ - lex_start(thd, oldlex->buf, (ulong) (oldlex->end_of_query - oldlex->ptr)); + /* Reset most stuff. */ + lex_start(thd); - /* - next_state is normally the same (0), but it happens that we swap lex in - "mid-sentence", so we must restore it. - */ - sublex->next_state= org_next_state; - /* We must reset ptr and end_of_query again */ - sublex->ptr= oldlex->ptr; - sublex->end_of_query= oldlex->end_of_query; - sublex->tok_start= oldlex->tok_start; - sublex->yylineno= oldlex->yylineno; /* And keep the SP stuff too */ sublex->sphead= oldlex->sphead; sublex->spcont= oldlex->spcont; @@ -1806,9 +1796,6 @@ sp_head::restore_lex(THD *thd) if (! oldlex) return; // Nothing to restore - // Update some state in the old one first - oldlex->ptr= sublex->ptr; - oldlex->next_state= sublex->next_state; oldlex->trg_table_fields.push_back(&sublex->trg_table_fields); /* diff --git a/sql/sp_head.h b/sql/sp_head.h index cce400d6a14..4632f6808fd 100644 --- a/sql/sp_head.h +++ b/sql/sp_head.h @@ -125,7 +125,7 @@ public: create_field m_return_field_def; /* This is used for FUNCTIONs only. */ - uchar *m_tmp_query; // Temporary pointer to sub query string + const char *m_tmp_query; // Temporary pointer to sub query string uint m_old_cmq; // Old CLIENT_MULTI_QUERIES value st_sp_chistics *m_chistics; ulong m_sql_mode; // For SHOW CREATE and execution @@ -174,7 +174,9 @@ public: */ HASH m_sroutines; // Pointers set during parsing - uchar *m_param_begin, *m_param_end, *m_body_begin; + const char *m_param_begin; + const char *m_param_end; + const char *m_body_begin; /* Security context for stored routine which should be run under diff --git a/sql/sql_class.cc b/sql/sql_class.cc index a94f903a47b..0d8f2ca6394 100644 --- a/sql/sql_class.cc +++ b/sql/sql_class.cc @@ -176,7 +176,7 @@ THD::THD() rand_used(0), time_zone_used(0), last_insert_id_used(0), last_insert_id_used_bin_log(0), insert_id_used(0), clear_next_insert_id(0), in_lock_tables(0), bootstrap(0), - derived_tables_processing(FALSE), spcont(NULL) + derived_tables_processing(FALSE), spcont(NULL), m_lip(NULL) { ulong tmp; diff --git a/sql/sql_class.h b/sql/sql_class.h index 99803802001..d7091b5a3bd 100644 --- a/sql/sql_class.h +++ b/sql/sql_class.h @@ -28,6 +28,7 @@ class Slave_log_event; class Format_description_log_event; class sp_rcontext; class sp_cache; +class Lex_input_stream; enum enum_enable_or_disable { LEAVE_AS_IS, ENABLE, DISABLE }; enum enum_ha_read_modes { RFIRST, RNEXT, RPREV, RLAST, RKEY, RNEXT_SAME }; @@ -1492,6 +1493,15 @@ public: query_id_t first_query_id; } binlog_evt_union; + /** + Character input stream consumed by the lexical analyser, + used during parsing. + Note that since the parser is not re-entrant, we keep only one input + stream here. This member is valid only when executing code during parsing, + and may point to invalid memory after that. + */ + Lex_input_stream *m_lip; + THD(); ~THD(); diff --git a/sql/sql_lex.cc b/sql/sql_lex.cc index 3be844b6761..6fe4bfbd4f1 100644 --- a/sql/sql_lex.cc +++ b/sql/sql_lex.cc @@ -32,13 +32,13 @@ sys_var_long_ptr trg_new_row_fake_var(0, 0); /* Macros to look like lex */ -#define yyGet() *(lex->ptr++) -#define yyGetLast() lex->ptr[-1] -#define yyPeek() lex->ptr[0] -#define yyPeek2() lex->ptr[1] -#define yyUnget() lex->ptr-- -#define yySkip() lex->ptr++ -#define yyLength() ((uint) (lex->ptr - lex->tok_start)-1) +#define yyGet() *(lip->ptr++) +#define yyGetLast() lip->ptr[-1] +#define yyPeek() lip->ptr[0] +#define yyPeek2() lip->ptr[1] +#define yyUnget() lip->ptr-- +#define yySkip() lip->ptr++ +#define yyLength() ((uint) (lip->ptr - lip->tok_start)-1) /* Longest standard keyword name */ #define TOCK_NAME_LENGTH 24 @@ -108,6 +108,27 @@ st_parsing_options::reset() allows_derived= TRUE; } +Lex_input_stream::Lex_input_stream(THD *thd, + const char* buffer, + unsigned int length) +: m_thd(thd), + yylineno(1), + yytoklen(0), + yylval(NULL), + ptr(buffer), + tok_start(NULL), + tok_end(NULL), + end_of_query(buffer + length), + tok_start_prev(NULL), + buf(buffer), + next_state(MY_LEX_START), + found_semicolon(NULL) +{ +} + +Lex_input_stream::~Lex_input_stream() +{} + /* This is called before every query that is to be parsed. @@ -115,14 +136,12 @@ st_parsing_options::reset() (We already do too much here) */ -void lex_start(THD *thd, uchar *buf,uint length) +void lex_start(THD *thd) { LEX *lex= thd->lex; DBUG_ENTER("lex_start"); lex->thd= lex->unit.thd= thd; - lex->buf= lex->ptr= buf; - lex->end_of_query= buf+length; lex->context_stack.empty(); lex->unit.init_query(); @@ -155,15 +174,13 @@ void lex_start(THD *thd, uchar *buf,uint length) lex->stmt_prepare_mode= FALSE; lex->derived_tables= 0; lex->lock_option= TL_READ; - lex->found_semicolon= 0; lex->safe_to_cache_query= 1; lex->time_zone_tables_used= 0; lex->leaf_tables_insert= 0; lex->parsing_options.reset(); lex->empty_field_list_on_rset= 0; lex->select_lex.select_number= 1; - lex->next_state=MY_LEX_START; - lex->yylineno = 1; + lex->in_comment=0; lex->length=0; lex->select_lex.in_sum_expr=0; @@ -201,22 +218,22 @@ void lex_end(LEX *lex) } -static int find_keyword(LEX *lex, uint len, bool function) +static int find_keyword(Lex_input_stream *lip, uint len, bool function) { - uchar *tok=lex->tok_start; + const char *tok= lip->tok_start; - SYMBOL *symbol = get_hash_symbol((const char *)tok,len,function); + SYMBOL *symbol= get_hash_symbol(tok, len, function); if (symbol) { - lex->yylval->symbol.symbol=symbol; - lex->yylval->symbol.str= (char*) tok; - lex->yylval->symbol.length=len; + lip->yylval->symbol.symbol=symbol; + lip->yylval->symbol.str= (char*) tok; + lip->yylval->symbol.length=len; if ((symbol->tok == NOT_SYM) && - (lex->thd->variables.sql_mode & MODE_HIGH_NOT_PRECEDENCE)) + (lip->m_thd->variables.sql_mode & MODE_HIGH_NOT_PRECEDENCE)) return NOT2_SYM; if ((symbol->tok == OR_OR_SYM) && - !(lex->thd->variables.sql_mode & MODE_PIPES_AS_CONCAT)) + !(lip->m_thd->variables.sql_mode & MODE_PIPES_AS_CONCAT)) return OR2_SYM; return symbol->tok; @@ -245,12 +262,12 @@ bool is_keyword(const char *name, uint len) /* make a copy of token before ptr and set yytoklen */ -static LEX_STRING get_token(LEX *lex,uint length) +static LEX_STRING get_token(Lex_input_stream *lip, uint length) { LEX_STRING tmp; yyUnget(); // ptr points now after last token char - tmp.length=lex->yytoklen=length; - tmp.str=(char*) lex->thd->strmake((char*) lex->tok_start,tmp.length); + tmp.length=lip->yytoklen=length; + tmp.str= lip->m_thd->strmake(lip->tok_start, tmp.length); return tmp; } @@ -261,14 +278,15 @@ static LEX_STRING get_token(LEX *lex,uint length) future to operate multichar strings (like ucs2) */ -static LEX_STRING get_quoted_token(LEX *lex,uint length, char quote) +static LEX_STRING get_quoted_token(Lex_input_stream *lip, + uint length, char quote) { LEX_STRING tmp; byte *from, *to, *end; yyUnget(); // ptr points now after last token char - tmp.length=lex->yytoklen=length; - tmp.str=(char*) lex->thd->alloc(tmp.length+1); - for (from= (byte*) lex->tok_start, to= (byte*) tmp.str, end= to+length ; + tmp.length=lip->yytoklen=length; + tmp.str=(char*) lip->m_thd->alloc(tmp.length+1); + for (from= (byte*) lip->tok_start, to= (byte*) tmp.str, end= to+length ; to != end ; ) { @@ -285,15 +303,14 @@ static LEX_STRING get_quoted_token(LEX *lex,uint length, char quote) Fix sometimes to do only one scan of the string */ -static char *get_text(LEX *lex) +static char *get_text(Lex_input_stream *lip) { reg1 uchar c,sep; uint found_escape=0; - CHARSET_INFO *cs= lex->thd->charset(); + CHARSET_INFO *cs= lip->m_thd->charset(); sep= yyGetLast(); // String should end with this - //lex->tok_start=lex->ptr-1; // Remember ' - while (lex->ptr != lex->end_of_query) + while (lip->ptr != lip->end_of_query) { c = yyGet(); #ifdef USE_MB @@ -301,18 +318,18 @@ static char *get_text(LEX *lex) int l; if (use_mb(cs) && (l = my_ismbchar(cs, - (const char *)lex->ptr-1, - (const char *)lex->end_of_query))) { - lex->ptr += l-1; + lip->ptr-1, + lip->end_of_query))) { + lip->ptr += l-1; continue; } } #endif if (c == '\\' && - !(lex->thd->variables.sql_mode & MODE_NO_BACKSLASH_ESCAPES)) + !(lip->m_thd->variables.sql_mode & MODE_NO_BACKSLASH_ESCAPES)) { // Escaped character found_escape=1; - if (lex->ptr == lex->end_of_query) + if (lip->ptr == lip->end_of_query) return 0; yySkip(); } @@ -327,21 +344,23 @@ static char *get_text(LEX *lex) yyUnget(); /* Found end. Unescape and return string */ - uchar *str,*end,*start; + const char *str; + const char *end; + char *start; - str=lex->tok_start+1; - end=lex->ptr-1; - if (!(start=(uchar*) lex->thd->alloc((uint) (end-str)+1))) + str=lip->tok_start+1; + end=lip->ptr-1; + if (!(start=(char*) lip->m_thd->alloc((uint) (end-str)+1))) return (char*) ""; // Sql_alloc has set error flag if (!found_escape) { - lex->yytoklen=(uint) (end-str); - memcpy(start,str,lex->yytoklen); - start[lex->yytoklen]=0; + lip->yytoklen=(uint) (end-str); + memcpy(start,str,lip->yytoklen); + start[lip->yytoklen]=0; } else { - uchar *to; + char *to; for (to=start ; str != end ; str++) { @@ -356,7 +375,7 @@ static char *get_text(LEX *lex) continue; } #endif - if (!(lex->thd->variables.sql_mode & MODE_NO_BACKSLASH_ESCAPES) && + if (!(lip->m_thd->variables.sql_mode & MODE_NO_BACKSLASH_ESCAPES) && *str == '\\' && str+1 != end) { switch(*++str) { @@ -393,7 +412,7 @@ static char *get_text(LEX *lex) *to++ = *str; } *to=0; - lex->yytoklen=(uint) (to-start); + lip->yytoklen=(uint) (to-start); } return (char*) start; } @@ -506,20 +525,21 @@ int MYSQLlex(void *arg, void *yythd) int tokval, result_state; uint length; enum my_lex_states state; - LEX *lex= ((THD *)yythd)->lex; + THD *thd= (THD *)yythd; + Lex_input_stream *lip= thd->m_lip; + LEX *lex= thd->lex; YYSTYPE *yylval=(YYSTYPE*) arg; - CHARSET_INFO *cs= ((THD *) yythd)->charset(); + CHARSET_INFO *cs= thd->charset(); uchar *state_map= cs->state_map; uchar *ident_map= cs->ident_map; - lex->yylval=yylval; // The global state + lip->yylval=yylval; // The global state - lex->tok_end_prev= lex->tok_end; - lex->tok_start_prev= lex->tok_start; + lip->tok_start_prev= lip->tok_start; - lex->tok_start=lex->tok_end=lex->ptr; - state=lex->next_state; - lex->next_state=MY_LEX_OPERATOR_OR_IDENT; + lip->tok_start=lip->tok_end=lip->ptr; + state=lip->next_state; + lip->next_state=MY_LEX_OPERATOR_OR_IDENT; LINT_INIT(c); for (;;) { @@ -530,9 +550,9 @@ int MYSQLlex(void *arg, void *yythd) for (c=yyGet() ; (state_map[c] == MY_LEX_SKIP) ; c= yyGet()) { if (c == '\n') - lex->yylineno++; + lip->yylineno++; } - lex->tok_start=lex->ptr-1; // Start of real token + lip->tok_start=lip->ptr-1; // Start of real token state= (enum my_lex_states) state_map[c]; break; case MY_LEX_ESCAPE: @@ -551,13 +571,13 @@ int MYSQLlex(void *arg, void *yythd) state=MY_LEX_COMMENT; break; } - yylval->lex_str.str=(char*) (lex->ptr=lex->tok_start);// Set to first chr + yylval->lex_str.str=(char*) (lip->ptr=lip->tok_start);// Set to first chr yylval->lex_str.length=1; c=yyGet(); if (c != ')') - lex->next_state= MY_LEX_START; // Allow signed numbers + lip->next_state= MY_LEX_START; // Allow signed numbers if (c == ',') - lex->tok_start=lex->ptr; // Let tok_start point at next item + lip->tok_start=lip->ptr; // Let tok_start point at next item /* Check for a placeholder: it should not precede a possible identifier because of binlogging: when a placeholder is replaced with @@ -575,14 +595,14 @@ int MYSQLlex(void *arg, void *yythd) break; } /* Found N'string' */ - lex->tok_start++; // Skip N + lip->tok_start++; // Skip N yySkip(); // Skip ' - if (!(yylval->lex_str.str = get_text(lex))) + if (!(yylval->lex_str.str = get_text(lip))) { state= MY_LEX_CHAR; // Read char by char break; } - yylval->lex_str.length= lex->yytoklen; + yylval->lex_str.length= lip->yytoklen; return(NCHAR_STRING); case MY_LEX_IDENT_OR_HEX: @@ -598,7 +618,7 @@ int MYSQLlex(void *arg, void *yythd) break; } case MY_LEX_IDENT: - uchar *start; + const char *start; #if defined(USE_MB) && defined(USE_MB_IDENT) if (use_mb(cs)) { @@ -606,13 +626,13 @@ int MYSQLlex(void *arg, void *yythd) if (my_mbcharlen(cs, yyGetLast()) > 1) { int l = my_ismbchar(cs, - (const char *)lex->ptr-1, - (const char *)lex->end_of_query); + lip->ptr-1, + lip->end_of_query); if (l == 0) { state = MY_LEX_CHAR; continue; } - lex->ptr += l - 1; + lip->ptr += l - 1; } while (ident_map[c=yyGet()]) { @@ -620,10 +640,10 @@ int MYSQLlex(void *arg, void *yythd) { int l; if ((l = my_ismbchar(cs, - (const char *)lex->ptr-1, - (const char *)lex->end_of_query)) == 0) + lip->ptr-1, + lip->end_of_query)) == 0) break; - lex->ptr += l-1; + lip->ptr += l-1; } } } @@ -634,8 +654,8 @@ int MYSQLlex(void *arg, void *yythd) /* If there were non-ASCII characters, mark that we must convert */ result_state= result_state & 0x80 ? IDENT_QUOTED : IDENT; } - length= (uint) (lex->ptr - lex->tok_start)-1; - start= lex->ptr; + length= (uint) (lip->ptr - lip->tok_start)-1; + start= lip->ptr; if (lex->ignore_space) { /* @@ -644,19 +664,19 @@ int MYSQLlex(void *arg, void *yythd) */ for (; state_map[c] == MY_LEX_SKIP ; c= yyGet()); } - if (start == lex->ptr && c == '.' && ident_map[yyPeek()]) - lex->next_state=MY_LEX_IDENT_SEP; + if (start == lip->ptr && c == '.' && ident_map[yyPeek()]) + lip->next_state=MY_LEX_IDENT_SEP; else { // '(' must follow directly if function yyUnget(); - if ((tokval = find_keyword(lex,length,c == '('))) + if ((tokval = find_keyword(lip, length, c == '('))) { - lex->next_state= MY_LEX_START; // Allow signed numbers + lip->next_state= MY_LEX_START; // Allow signed numbers return(tokval); // Was keyword } yySkip(); // next state does a unget } - yylval->lex_str=get_token(lex,length); + yylval->lex_str=get_token(lip, length); /* Note: "SELECT _bla AS 'alias'" @@ -673,12 +693,12 @@ int MYSQLlex(void *arg, void *yythd) return(result_state); // IDENT or IDENT_QUOTED case MY_LEX_IDENT_SEP: // Found ident and now '.' - yylval->lex_str.str=(char*) lex->ptr; + yylval->lex_str.str=(char*) lip->ptr; yylval->lex_str.length=1; c=yyGet(); // should be '.' - lex->next_state= MY_LEX_IDENT_START;// Next is an ident (not a keyword) + lip->next_state= MY_LEX_IDENT_START;// Next is an ident (not a keyword) if (!ident_map[yyPeek()]) // Probably ` or " - lex->next_state= MY_LEX_START; + lip->next_state= MY_LEX_START; return((int) c); case MY_LEX_NUMBER_IDENT: // number or ident which num-start @@ -698,36 +718,36 @@ int MYSQLlex(void *arg, void *yythd) { yySkip(); while (my_isdigit(cs,yyGet())) ; - yylval->lex_str=get_token(lex,yyLength()); + yylval->lex_str=get_token(lip, yyLength()); return(FLOAT_NUM); } } yyUnget(); /* purecov: inspected */ } - else if (c == 'x' && (lex->ptr - lex->tok_start) == 2 && - lex->tok_start[0] == '0' ) + else if (c == 'x' && (lip->ptr - lip->tok_start) == 2 && + lip->tok_start[0] == '0' ) { // Varbinary while (my_isxdigit(cs,(c = yyGet()))) ; - if ((lex->ptr - lex->tok_start) >= 4 && !ident_map[c]) + if ((lip->ptr - lip->tok_start) >= 4 && !ident_map[c]) { - yylval->lex_str=get_token(lex,yyLength()); + yylval->lex_str=get_token(lip, yyLength()); yylval->lex_str.str+=2; // Skip 0x yylval->lex_str.length-=2; - lex->yytoklen-=2; + lip->yytoklen-=2; return (HEX_NUM); } yyUnget(); } - else if (c == 'b' && (lex->ptr - lex->tok_start) == 2 && - lex->tok_start[0] == '0' ) + else if (c == 'b' && (lip->ptr - lip->tok_start) == 2 && + lip->tok_start[0] == '0' ) { // b'bin-number' while (my_isxdigit(cs,(c = yyGet()))) ; - if ((lex->ptr - lex->tok_start) >= 4 && !ident_map[c]) + if ((lip->ptr - lip->tok_start) >= 4 && !ident_map[c]) { - yylval->lex_str= get_token(lex, yyLength()); + yylval->lex_str= get_token(lip, yyLength()); yylval->lex_str.str+= 2; // Skip 0x yylval->lex_str.length-= 2; - lex->yytoklen-= 2; + lip->yytoklen-= 2; return (BIN_NUM); } yyUnget(); @@ -745,10 +765,10 @@ int MYSQLlex(void *arg, void *yythd) { int l; if ((l = my_ismbchar(cs, - (const char *)lex->ptr-1, - (const char *)lex->end_of_query)) == 0) + lip->ptr-1, + lip->end_of_query)) == 0) break; - lex->ptr += l-1; + lip->ptr += l-1; } } } @@ -760,16 +780,16 @@ int MYSQLlex(void *arg, void *yythd) result_state= result_state & 0x80 ? IDENT_QUOTED : IDENT; } if (c == '.' && ident_map[yyPeek()]) - lex->next_state=MY_LEX_IDENT_SEP;// Next is '.' + lip->next_state=MY_LEX_IDENT_SEP;// Next is '.' - yylval->lex_str= get_token(lex,yyLength()); + yylval->lex_str= get_token(lip, yyLength()); return(result_state); case MY_LEX_USER_VARIABLE_DELIMITER: // Found quote char { uint double_quotes= 0; char quote_char= c; // Used char - lex->tok_start=lex->ptr; // Skip first ` + lip->tok_start=lip->ptr; // Skip first ` while ((c=yyGet())) { int var_length; @@ -789,23 +809,23 @@ int MYSQLlex(void *arg, void *yythd) #ifdef USE_MB else if (var_length < 1) break; // Error - lex->ptr+= var_length-1; + lip->ptr+= var_length-1; #endif } if (double_quotes) - yylval->lex_str=get_quoted_token(lex,yyLength() - double_quotes, + yylval->lex_str=get_quoted_token(lip, yyLength() - double_quotes, quote_char); else - yylval->lex_str=get_token(lex,yyLength()); + yylval->lex_str=get_token(lip, yyLength()); if (c == quote_char) yySkip(); // Skip end ` - lex->next_state= MY_LEX_START; + lip->next_state= MY_LEX_START; return(IDENT_QUOTED); } case MY_LEX_INT_OR_REAL: // Compleat int or incompleat real if (c != '.') { // Found complete integer number. - yylval->lex_str=get_token(lex,yyLength()); + yylval->lex_str=get_token(lip, yyLength()); return int_token(yylval->lex_str.str,yylval->lex_str.length); } // fall through @@ -823,47 +843,47 @@ int MYSQLlex(void *arg, void *yythd) break; } while (my_isdigit(cs,yyGet())) ; - yylval->lex_str=get_token(lex,yyLength()); + yylval->lex_str=get_token(lip, yyLength()); return(FLOAT_NUM); } - yylval->lex_str=get_token(lex,yyLength()); + yylval->lex_str=get_token(lip, yyLength()); return(DECIMAL_NUM); case MY_LEX_HEX_NUMBER: // Found x'hexstring' yyGet(); // Skip ' while (my_isxdigit(cs,(c = yyGet()))) ; - length=(lex->ptr - lex->tok_start); // Length of hexnum+3 + length=(lip->ptr - lip->tok_start); // Length of hexnum+3 if (!(length & 1) || c != '\'') { return(ABORT_SYM); // Illegal hex constant } yyGet(); // get_token makes an unget - yylval->lex_str=get_token(lex,length); + yylval->lex_str=get_token(lip, length); yylval->lex_str.str+=2; // Skip x' yylval->lex_str.length-=3; // Don't count x' and last ' - lex->yytoklen-=3; + lip->yytoklen-=3; return (HEX_NUM); case MY_LEX_BIN_NUMBER: // Found b'bin-string' yyGet(); // Skip ' while ((c= yyGet()) == '0' || c == '1'); - length= (lex->ptr - lex->tok_start); // Length of bin-num + 3 + length= (lip->ptr - lip->tok_start); // Length of bin-num + 3 if (c != '\'') return(ABORT_SYM); // Illegal hex constant yyGet(); // get_token makes an unget - yylval->lex_str= get_token(lex, length); + yylval->lex_str= get_token(lip, length); yylval->lex_str.str+= 2; // Skip b' yylval->lex_str.length-= 3; // Don't count b' and last ' - lex->yytoklen-= 3; + lip->yytoklen-= 3; return (BIN_NUM); case MY_LEX_CMP_OP: // Incomplete comparison operator if (state_map[yyPeek()] == MY_LEX_CMP_OP || state_map[yyPeek()] == MY_LEX_LONG_CMP_OP) yySkip(); - if ((tokval = find_keyword(lex,(uint) (lex->ptr - lex->tok_start),0))) + if ((tokval = find_keyword(lip,(uint) (lip->ptr - lip->tok_start),0))) { - lex->next_state= MY_LEX_START; // Allow signed numbers + lip->next_state= MY_LEX_START; // Allow signed numbers return(tokval); } state = MY_LEX_CHAR; // Something fishy found @@ -877,9 +897,9 @@ int MYSQLlex(void *arg, void *yythd) if (state_map[yyPeek()] == MY_LEX_CMP_OP) yySkip(); } - if ((tokval = find_keyword(lex,(uint) (lex->ptr - lex->tok_start),0))) + if ((tokval = find_keyword(lip,(uint) (lip->ptr - lip->tok_start),0))) { - lex->next_state= MY_LEX_START; // Found long op + lip->next_state= MY_LEX_START; // Found long op return(tokval); } state = MY_LEX_CHAR; // Something fishy found @@ -892,24 +912,24 @@ int MYSQLlex(void *arg, void *yythd) break; } yySkip(); - tokval = find_keyword(lex,2,0); // Is a bool operator - lex->next_state= MY_LEX_START; // Allow signed numbers + tokval = find_keyword(lip,2,0); // Is a bool operator + lip->next_state= MY_LEX_START; // Allow signed numbers return(tokval); case MY_LEX_STRING_OR_DELIMITER: - if (((THD *) yythd)->variables.sql_mode & MODE_ANSI_QUOTES) + if (thd->variables.sql_mode & MODE_ANSI_QUOTES) { state= MY_LEX_USER_VARIABLE_DELIMITER; break; } /* " used for strings */ case MY_LEX_STRING: // Incomplete text string - if (!(yylval->lex_str.str = get_text(lex))) + if (!(yylval->lex_str.str = get_text(lip))) { state= MY_LEX_CHAR; // Read char by char break; } - yylval->lex_str.length=lex->yytoklen; + yylval->lex_str.length=lip->yytoklen; return(TEXT_STRING); case MY_LEX_COMMENT: // Comment @@ -933,7 +953,7 @@ int MYSQLlex(void *arg, void *yythd) state=MY_LEX_START; if (my_isdigit(cs,yyPeek())) { // Version number - version=strtol((char*) lex->ptr,(char**) &lex->ptr,10); + version=strtol((char*) lip->ptr,(char**) &lip->ptr,10); } if (version <= MYSQL_VERSION_ID) { @@ -941,13 +961,13 @@ int MYSQLlex(void *arg, void *yythd) break; } } - while (lex->ptr != lex->end_of_query && + while (lip->ptr != lip->end_of_query && ((c=yyGet()) != '*' || yyPeek() != '/')) { if (c == '\n') - lex->yylineno++; + lip->yylineno++; } - if (lex->ptr != lex->end_of_query) + if (lip->ptr != lip->end_of_query) yySkip(); // remove last '/' state = MY_LEX_START; // Try again break; @@ -972,14 +992,13 @@ int MYSQLlex(void *arg, void *yythd) case MY_LEX_SEMICOLON: // optional line terminator if (yyPeek()) { - THD* thd= (THD*)yythd; if ((thd->client_capabilities & CLIENT_MULTI_STATEMENTS) && !lex->stmt_prepare_mode) { lex->safe_to_cache_query= 0; - lex->found_semicolon=(char*) lex->ptr; + lip->found_semicolon= lip->ptr; thd->server_status|= SERVER_MORE_RESULTS_EXISTS; - lex->next_state= MY_LEX_END; + lip->next_state= MY_LEX_END; return (END_OF_INPUT); } state= MY_LEX_CHAR; // Return ';' @@ -987,15 +1006,15 @@ int MYSQLlex(void *arg, void *yythd) } /* fall true */ case MY_LEX_EOL: - if (lex->ptr >= lex->end_of_query) + if (lip->ptr >= lip->end_of_query) { - lex->next_state=MY_LEX_END; // Mark for next loop + lip->next_state=MY_LEX_END; // Mark for next loop return(END_OF_INPUT); } state=MY_LEX_CHAR; break; case MY_LEX_END: - lex->next_state=MY_LEX_END; + lip->next_state=MY_LEX_END; return(0); // We found end of input last time /* Actually real shouldn't start with . but allow them anyhow */ @@ -1015,26 +1034,26 @@ int MYSQLlex(void *arg, void *yythd) case MY_LEX_STRING_OR_DELIMITER: break; case MY_LEX_USER_END: - lex->next_state=MY_LEX_SYSTEM_VAR; + lip->next_state=MY_LEX_SYSTEM_VAR; break; default: - lex->next_state=MY_LEX_HOSTNAME; + lip->next_state=MY_LEX_HOSTNAME; break; } - yylval->lex_str.str=(char*) lex->ptr; + yylval->lex_str.str=(char*) lip->ptr; yylval->lex_str.length=1; return((int) '@'); case MY_LEX_HOSTNAME: // end '@' of user@hostname for (c=yyGet() ; my_isalnum(cs,c) || c == '.' || c == '_' || c == '$'; c= yyGet()) ; - yylval->lex_str=get_token(lex,yyLength()); + yylval->lex_str=get_token(lip, yyLength()); return(LEX_HOSTNAME); case MY_LEX_SYSTEM_VAR: - yylval->lex_str.str=(char*) lex->ptr; + yylval->lex_str.str=(char*) lip->ptr; yylval->lex_str.length=1; yySkip(); // Skip '@' - lex->next_state= (state_map[yyPeek()] == + lip->next_state= (state_map[yyPeek()] == MY_LEX_USER_VARIABLE_DELIMITER ? MY_LEX_OPERATOR_OR_IDENT : MY_LEX_IDENT_OR_KEYWORD); @@ -1051,16 +1070,16 @@ int MYSQLlex(void *arg, void *yythd) result_state= result_state & 0x80 ? IDENT_QUOTED : IDENT; if (c == '.') - lex->next_state=MY_LEX_IDENT_SEP; - length= (uint) (lex->ptr - lex->tok_start)-1; + lip->next_state=MY_LEX_IDENT_SEP; + length= (uint) (lip->ptr - lip->tok_start)-1; if (length == 0) return(ABORT_SYM); // Names must be nonempty. - if ((tokval= find_keyword(lex,length,0))) + if ((tokval= find_keyword(lip, length,0))) { yyUnget(); // Put back 'c' return(tokval); // Was keyword } - yylval->lex_str=get_token(lex,length); + yylval->lex_str=get_token(lip, length); return(result_state); } } @@ -1093,7 +1112,7 @@ Alter_info::Alter_info(const Alter_info &rhs, MEM_ROOT *mem_root) Pointer to the last non-comment symbol of the statement. */ -uchar *skip_rear_comments(uchar *begin, uchar *end) +char *skip_rear_comments(char *begin, char *end) { while (begin < end && (end[-1] <= ' ' || end[-1] == '*' || end[-1] == '/' || end[-1] == ';')) diff --git a/sql/sql_lex.h b/sql/sql_lex.h index de7de0d46e9..8a9bc62a83e 100644 --- a/sql/sql_lex.h +++ b/sql/sql_lex.h @@ -469,7 +469,7 @@ public: void set_limit(st_select_lex *values); void set_thd(THD *thd_arg) { thd= thd_arg; } - friend void lex_start(THD *thd, uchar *buf, uint length); + friend void lex_start(THD *thd); friend int subselect_union_engine::exec(); List *get_unit_column_types(); @@ -675,7 +675,7 @@ public: void cut_subtree() { slave= 0; } bool test_limit(); - friend void lex_start(THD *thd, uchar *buf, uint length); + friend void lex_start(THD *thd); st_select_lex() : n_sum_items(0), n_child_sum_items(0) {} void make_empty_select() { @@ -898,30 +898,70 @@ struct st_parsing_options }; +/** + This class represents the character input stream consumed during + lexical analysis. +*/ +class Lex_input_stream +{ +public: + Lex_input_stream(THD *thd, const char* buff, unsigned int length); + ~Lex_input_stream(); + + /** Current thread. */ + THD *m_thd; + + /** Current line number. */ + uint yylineno; + + /** Length of the last token parsed. */ + uint yytoklen; + + /** Interface with bison, value of the last token parsed. */ + LEX_YYSTYPE yylval; + + /** Pointer to the current position in the input stream. */ + const char* ptr; + + /** Starting position of the last token parsed. */ + const char* tok_start; + + /** Ending position of the last token parsed. */ + const char* tok_end; + + /** End of the query text in the input stream. */ + const char* end_of_query; + + /** Starting position of the previous token parsed. */ + const char* tok_start_prev; + + /** Begining of the query text in the input stream. */ + const char* buf; + + /** Current state of the lexical analyser. */ + enum my_lex_states next_state; + + /** Position of ';' in the stream, to delimit multiple queries. */ + const char* found_semicolon; +}; + + /* The state of the lex parsing. This is saved in the THD struct */ typedef struct st_lex : public Query_tables_list { - uint yylineno,yytoklen; /* Simulate lex */ - LEX_YYSTYPE yylval; SELECT_LEX_UNIT unit; /* most upper unit */ SELECT_LEX select_lex; /* first SELECT_LEX */ /* current SELECT_LEX in parsing */ SELECT_LEX *current_select; /* list of all SELECT_LEX */ SELECT_LEX *all_selects_list; - uchar *buf; /* The beginning of string, used by SPs */ - uchar *ptr,*tok_start,*tok_end,*end_of_query; - - /* The values of tok_start/tok_end as they were one call of MYSQLlex before */ - uchar *tok_start_prev, *tok_end_prev; char *length,*dec,*change,*name; char *help_arg; char *backup_dir; /* For RESTORE/BACKUP */ char* to_log; /* For PURGE MASTER LOGS TO */ char* x509_subject,*x509_issuer,*ssl_cipher; - char* found_semicolon; /* For multi queries - next query */ String *wild; sql_exchange *exchange; select_result *result; @@ -990,7 +1030,6 @@ typedef struct st_lex : public Query_tables_list enum_sql_command sql_command, orig_sql_command; thr_lock_type lock_option; enum SSL_type ssl_type; /* defined in violite.h */ - enum my_lex_states next_state; enum enum_duplicates duplicates; enum enum_tx_isolation tx_isolation; enum enum_ha_read_modes ha_read_mode; @@ -1101,8 +1140,9 @@ typedef struct st_lex : public Query_tables_list Pointers to part of LOAD DATA statement that should be rewritten during replication ("LOCAL 'filename' REPLACE INTO" part). */ - uchar *fname_start, *fname_end; - + const char *fname_start; + const char *fname_end; + bool escape_used; st_lex(); @@ -1211,7 +1251,7 @@ struct st_lex_local: public st_lex extern void lex_init(void); extern void lex_free(void); -extern void lex_start(THD *thd, uchar *buf,uint length); +extern void lex_start(THD *thd); extern void lex_end(LEX *lex); extern int MYSQLlex(void *arg, void *yythd); -extern uchar *skip_rear_comments(uchar *begin, uchar *end); +extern char *skip_rear_comments(char *begin, char *end); diff --git a/sql/sql_parse.cc b/sql/sql_parse.cc index 1b8bfd38fc4..36ae5ca4a31 100644 --- a/sql/sql_parse.cc +++ b/sql/sql_parse.cc @@ -1236,6 +1236,7 @@ pthread_handler_t handle_bootstrap(void *arg) THD *thd=(THD*) arg; FILE *file=bootstrap_file; char *buff; + const char* found_semicolon= NULL; /* The following must be called before DBUG_ENTER */ thd->thread_stack= (char*) &thd; @@ -1312,7 +1313,7 @@ pthread_handler_t handle_bootstrap(void *arg) */ thd->query_id=next_query_id(); thd->set_time(); - mysql_parse(thd,thd->query,length); + mysql_parse(thd, thd->query, length, & found_semicolon); close_thread_tables(thd); // Free tables if (thd->is_fatal_error) @@ -1789,17 +1790,19 @@ bool dispatch_command(enum enum_server_command command, THD *thd, char *packet_end= thd->query + thd->query_length; /* 'b' stands for 'buffer' parameter', special for 'my_snprintf' */ const char *format= "%.*b"; + const char* found_semicolon= NULL; + mysql_log.write(thd,command, format, thd->query_length, thd->query); DBUG_PRINT("query",("%-.4096s",thd->query)); if (!(specialflag & SPECIAL_NO_PRIOR)) my_pthread_setprio(pthread_self(),QUERY_PRIOR); - mysql_parse(thd,thd->query, thd->query_length); + mysql_parse(thd, thd->query, thd->query_length, & found_semicolon); - while (!thd->killed && thd->lex->found_semicolon && !thd->net.report_error) + while (!thd->killed && found_semicolon && !thd->net.report_error) { - char *next_packet= thd->lex->found_semicolon; + char *next_packet= (char*) found_semicolon; net->no_send_error= 0; /* Multiple queries exits, execute them individually @@ -1824,7 +1827,7 @@ bool dispatch_command(enum enum_server_command command, THD *thd, thd->set_time(); /* Reset the query start time. */ /* TODO: set thd->lex->sql_command to SQLCOM_END here */ VOID(pthread_mutex_unlock(&LOCK_thread_count)); - mysql_parse(thd, next_packet, length); + mysql_parse(thd, next_packet, length, & found_semicolon); } if (!(specialflag & SPECIAL_NO_PRIOR)) @@ -1845,7 +1848,7 @@ bool dispatch_command(enum enum_server_command command, THD *thd, LEX_STRING conv_name; /* used as fields initializator */ - lex_start(thd, 0, 0); + lex_start(thd); statistic_increment(thd->status_var.com_stat[SQLCOM_SHOW_FIELDS], &LOCK_status); @@ -1882,7 +1885,10 @@ bool dispatch_command(enum enum_server_command command, THD *thd, break; /* init structures for VIEW processing */ table_list.select_lex= &(thd->lex->select_lex); - mysql_init_query(thd, (uchar*)"", 0); + + lex_start(thd); + mysql_reset_thd_for_next_command(thd); + thd->lex-> select_lex.table_list.link_in_list((byte*) &table_list, (byte**) &table_list.next_local); @@ -5687,20 +5693,6 @@ bool my_yyoverflow(short **yyss, YYSTYPE **yyvs, ulong *yystacksize) } -/**************************************************************************** - Initialize global thd variables needed for query -****************************************************************************/ - -void -mysql_init_query(THD *thd, uchar *buf, uint length) -{ - DBUG_ENTER("mysql_init_query"); - lex_start(thd, buf, length); - mysql_reset_thd_for_next_command(thd); - DBUG_VOID_RETURN; -} - - /* Reset THD part responsible for command processing state. @@ -5887,21 +5879,54 @@ void mysql_init_multi_delete(LEX *lex) mysql_test_parse_for_slave() in this same file. */ -void mysql_parse(THD *thd, char *inBuf, uint length) +/** + Parse a query. + @param thd Current thread + @param inBuf Begining of the query text + @param length Length of the query text + @param [out] semicolon For multi queries, position of the character of + the next query in the query text. +*/ + +void mysql_parse(THD *thd, const char *inBuf, uint length, + const char ** found_semicolon) { DBUG_ENTER("mysql_parse"); DBUG_EXECUTE_IF("parser_debug", turn_parser_debug_on();); - mysql_init_query(thd, (uchar*) inBuf, length); - if (query_cache_send_result_to_client(thd, inBuf, length) <= 0) + /* + Warning. + The purpose of query_cache_send_result_to_client() is to lookup the + query in the query cache first, to avoid parsing and executing it. + So, the natural implementation would be to: + - first, call query_cache_send_result_to_client, + - second, if caching failed, initialise the lexical and syntactic parser. + The problem is that the query cache depends on a clean initialization + of the thd and thd->lex structures, which happen to be implemented + by: + - lex_start() + - mysql_reset_thd_for_next_command() + So, initializing the lexical analyser *before* using the query cache + is required for the cache to work properly. + FIXME: cleanup the dependencies in the code to simplify this. + */ + Lex_input_stream lip(thd, inBuf, length); + thd->m_lip= &lip; + lex_start(thd); + mysql_reset_thd_for_next_command(thd); + + if (query_cache_send_result_to_client(thd, (char*) inBuf, length) <= 0) { LEX *lex= thd->lex; - + sp_cache_flush_obsolete(&thd->sp_proc_cache); sp_cache_flush_obsolete(&thd->sp_func_cache); - - if (!MYSQLparse((void *)thd) && ! thd->is_fatal_error) + + int err= MYSQLparse(thd); + *found_semicolon= lip.found_semicolon; + + if (!err && ! thd->is_fatal_error) { #ifndef NO_EMBEDDED_ACCESS_CHECKS if (mqh_used && thd->user_connect && @@ -5924,8 +5949,8 @@ void mysql_parse(THD *thd, char *inBuf, uint length) PROCESSLIST. Note that we don't need LOCK_thread_count to modify query_length. */ - if (lex->found_semicolon && - (thd->query_length= (ulong)(lex->found_semicolon - thd->query))) + if (lip.found_semicolon && + (thd->query_length= (ulong)(lip.found_semicolon - thd->query))) thd->query_length--; /* Actually execute the query */ mysql_execute_command(thd); @@ -5952,6 +5977,12 @@ void mysql_parse(THD *thd, char *inBuf, uint length) thd->cleanup_after_query(); DBUG_ASSERT(thd->change_list.is_empty()); } + else + { + /* There are no multi queries in the cache. */ + *found_semicolon= NULL; + } + DBUG_VOID_RETURN; } @@ -5972,8 +6003,13 @@ bool mysql_test_parse_for_slave(THD *thd, char *inBuf, uint length) bool error= 0; DBUG_ENTER("mysql_test_parse_for_slave"); - mysql_init_query(thd, (uchar*) inBuf, length); - if (!MYSQLparse((void*) thd) && ! thd->is_fatal_error && + Lex_input_stream lip(thd, inBuf, length); + thd->m_lip= &lip; + lex_start(thd); + mysql_reset_thd_for_next_command(thd); + int err= MYSQLparse((void*) thd); + + if (!err && ! thd->is_fatal_error && all_tables_not_ok(thd,(TABLE_LIST*) lex->select_lex.table_list.first)) error= 1; /* Ignore question */ thd->end_statement(); @@ -7032,8 +7068,9 @@ bool check_simple_select() if (lex->current_select != &lex->select_lex) { char command[80]; - strmake(command, lex->yylval->symbol.str, - min(lex->yylval->symbol.length, sizeof(command)-1)); + Lex_input_stream *lip= thd->m_lip; + strmake(command, lip->yylval->symbol.str, + min(lip->yylval->symbol.length, sizeof(command)-1)); my_error(ER_CANT_USE_OPTION_HERE, MYF(0), command); return 1; } diff --git a/sql/sql_prepare.cc b/sql/sql_prepare.cc index 85092f14624..ab26fc4e85d 100644 --- a/sql/sql_prepare.cc +++ b/sql/sql_prepare.cc @@ -2799,11 +2799,15 @@ bool Prepared_statement::prepare(const char *packet, uint packet_len) old_stmt_arena= thd->stmt_arena; thd->stmt_arena= this; - lex_start(thd, (uchar*) thd->query, thd->query_length); + + Lex_input_stream lip(thd, thd->query, thd->query_length); + thd->m_lip= &lip; + lex_start(thd); lex->safe_to_cache_query= FALSE; lex->stmt_prepare_mode= TRUE; + int err= MYSQLparse((void *)thd); - error= MYSQLparse((void *)thd) || thd->is_fatal_error || + error= err || thd->is_fatal_error || thd->net.report_error || init_param_array(this); /* diff --git a/sql/sql_trigger.cc b/sql/sql_trigger.cc index 55d51ad07b7..26b557a8247 100644 --- a/sql/sql_trigger.cc +++ b/sql/sql_trigger.cc @@ -978,10 +978,14 @@ bool Table_triggers_list::check_n_load(THD *thd, const char *db, LEX_STRING *trg_definer= it_definer++; thd->variables.sql_mode= (ulong)*trg_sql_mode; - lex_start(thd, (uchar*)trg_create_str->str, trg_create_str->length); + Lex_input_stream lip(thd, trg_create_str->str, trg_create_str->length); + thd->m_lip= &lip; + lex_start(thd); thd->spcont= 0; - if (MYSQLparse((void *)thd) || thd->is_fatal_error) + int err= MYSQLparse((void *)thd); + + if (err || thd->is_fatal_error) { /* Currently sphead is always deleted in case of a parse error */ DBUG_ASSERT(lex.sphead == 0); diff --git a/sql/sql_view.cc b/sql/sql_view.cc index 83beec3d1be..5ec32ac3e79 100644 --- a/sql/sql_view.cc +++ b/sql/sql_view.cc @@ -761,8 +761,8 @@ static int mysql_register_view(THD *thd, TABLE_LIST *view, view->query.str= (char*)str.ptr(); view->query.length= str.length()-1; // we do not need last \0 view->source.str= thd->query + thd->lex->create_view_select_start; - view->source.length= (char *)skip_rear_comments((uchar *)view->source.str, - (uchar *)thd->query + + view->source.length= (char *)skip_rear_comments((char *)view->source.str, + (char *)thd->query + thd->query_length) - view->source.str; view->file_version= 1; @@ -973,10 +973,14 @@ bool mysql_make_view(THD *thd, File_parser *parser, TABLE_LIST *table, now Lex placed in statement memory */ table->view= lex= thd->lex= (LEX*) new(thd->mem_root) st_lex_local; - lex_start(thd, (uchar*)table->query.str, table->query.length); - view_select= &lex->select_lex; - view_select->select_number= ++thd->select_number; + { + Lex_input_stream lip(thd, table->query.str, table->query.length); + thd->m_lip= &lip; + lex_start(thd); + view_select= &lex->select_lex; + view_select->select_number= ++thd->select_number; + ulong save_mode= thd->variables.sql_mode; /* switch off modes which can prevent normal parsing of VIEW - MODE_REAL_AS_FLOAT affect only CREATE TABLE parsing diff --git a/sql/sql_yacc.yy b/sql/sql_yacc.yy index 4a50a602121..82d148c4a1e 100644 --- a/sql/sql_yacc.yy +++ b/sql/sql_yacc.yy @@ -86,12 +86,13 @@ const LEX_STRING null_lex_str={0,0}; void my_parse_error(const char *s) { THD *thd= current_thd; + Lex_input_stream *lip= thd->m_lip; - char *yytext= (char*) thd->lex->tok_start; + const char *yytext= lip->tok_start; /* Push an error into the error stack */ my_printf_error(ER_PARSE_ERROR, ER(ER_PARSE_ERROR), MYF(0), s, - (yytext ? (char*) yytext : ""), - thd->lex->yylineno); + (yytext ? yytext : ""), + lip->yylineno); } /** @@ -1619,7 +1620,9 @@ create_function_tail: } | '(' { - LEX *lex= Lex; + THD *thd= YYTHD; + LEX *lex= thd->lex; + Lex_input_stream *lip= thd->m_lip; sp_head *sp; /* @@ -1639,9 +1642,9 @@ create_function_tail: } /* Order is important here: new - reset - init */ sp= new sp_head(); - sp->reset_thd_mem_root(YYTHD); + sp->reset_thd_mem_root(thd); sp->init(lex); - sp->init_sp_name(YYTHD, lex->spname); + sp->init_sp_name(thd, lex->spname); sp->m_type= TYPE_ENUM_FUNCTION; lex->sphead= sp; @@ -1650,15 +1653,17 @@ create_function_tail: * stored procedure, otherwise yylex will chop it into pieces * at each ';'. */ - sp->m_old_cmq= YYTHD->client_capabilities & CLIENT_MULTI_QUERIES; - YYTHD->client_capabilities &= ~CLIENT_MULTI_QUERIES; - lex->sphead->m_param_begin= lex->tok_start+1; + sp->m_old_cmq= thd->client_capabilities & CLIENT_MULTI_QUERIES; + thd->client_capabilities &= ~CLIENT_MULTI_QUERIES; + lex->sphead->m_param_begin= lip->tok_start+1; } sp_fdparam_list ')' { - LEX *lex= Lex; + THD *thd= YYTHD; + LEX *lex= thd->lex; + Lex_input_stream *lip= thd->m_lip; - lex->sphead->m_param_end= lex->tok_start; + lex->sphead->m_param_end= lip->tok_start; } RETURNS_SYM { @@ -1682,10 +1687,12 @@ create_function_tail: } sp_c_chistics { - LEX *lex= Lex; + THD *thd= YYTHD; + LEX *lex= thd->lex; + Lex_input_stream *lip= thd->m_lip; lex->sphead->m_chistics= &lex->sp_chistics; - lex->sphead->m_body_begin= lex->tok_start; + lex->sphead->m_body_begin= lip->tok_start; } sp_proc_stmt { @@ -2233,14 +2240,18 @@ sp_opt_default: sp_proc_stmt: { - LEX *lex= Lex; + THD *thd= YYTHD; + LEX *lex= thd->lex; + Lex_input_stream *lip= thd->m_lip; - lex->sphead->reset_lex(YYTHD); - lex->sphead->m_tmp_query= lex->tok_start; + lex->sphead->reset_lex(thd); + lex->sphead->m_tmp_query= lip->tok_start; } statement { - LEX *lex= Lex; + THD *thd= YYTHD; + LEX *lex= thd->lex; + Lex_input_stream *lip= thd->m_lip; sp_head *sp= lex->sphead; sp->m_flags|= sp_get_flags_for_command(lex); @@ -2267,15 +2278,15 @@ sp_proc_stmt: lex->tok_end otherwise. */ if (yychar == YYEMPTY) - i->m_query.length= lex->ptr - sp->m_tmp_query; + i->m_query.length= lip->ptr - sp->m_tmp_query; else - i->m_query.length= lex->tok_end - sp->m_tmp_query; - i->m_query.str= strmake_root(YYTHD->mem_root, - (char *)sp->m_tmp_query, + i->m_query.length= lip->tok_end - sp->m_tmp_query; + i->m_query.str= strmake_root(thd->mem_root, + sp->m_tmp_query, i->m_query.length); sp->add_instr(i); } - sp->restore_lex(YYTHD); + sp->restore_lex(thd); } | RETURN_SYM { Lex->sphead->reset_lex(YYTHD); } @@ -4444,10 +4455,18 @@ select_item: }; remember_name: - { $$=(char*) Lex->tok_start; }; + { + THD *thd= YYTHD; + Lex_input_stream *lip= thd->m_lip; + $$= (char*) lip->tok_start; + }; remember_end: - { $$=(char*) Lex->tok_end; }; + { + THD *thd= YYTHD; + Lex_input_stream *lip= thd->m_lip; + $$=(char*) lip->tok_end; + }; select_item2: table_wild { $$=$1; } /* table.* */ @@ -6292,12 +6311,14 @@ procedure_list2: procedure_item: remember_name expr { - LEX *lex= Lex; - if (add_proc_to_list(lex->thd, $2)) + THD *thd= YYTHD; + Lex_input_stream *lip= thd->m_lip; + + if (add_proc_to_list(thd, $2)) MYSQL_YYABORT; if (!$2->name) - $2->set_name($1,(uint) ((char*) lex->tok_end - $1), - YYTHD->charset()); + $2->set_name($1,(uint) ((char*) lip->tok_end - $1), + thd->charset()); } ; @@ -7337,13 +7358,16 @@ use: USE_SYM ident load: LOAD DATA_SYM { - LEX *lex=Lex; + THD *thd= YYTHD; + LEX *lex= thd->lex; + Lex_input_stream *lip= thd->m_lip; + if (lex->sphead) { my_error(ER_SP_BADSTATEMENT, MYF(0), "LOAD DATA"); MYSQL_YYABORT; } - lex->fname_start= lex->ptr; + lex->fname_start= lip->ptr; } load_data {} @@ -7378,8 +7402,10 @@ load_data: } opt_duplicate INTO { - LEX *lex=Lex; - lex->fname_end= lex->ptr; + THD *thd= YYTHD; + LEX *lex= thd->lex; + Lex_input_stream *lip= thd->m_lip; + lex->fname_end= lip->ptr; } TABLE_SYM table_ident { @@ -7559,15 +7585,16 @@ text_string: param_marker: PARAM_MARKER { - THD *thd=YYTHD; + THD *thd= YYTHD; LEX *lex= thd->lex; + Lex_input_stream *lip= thd->m_lip; Item_param *item; if (! lex->parsing_options.allows_variable) { my_error(ER_VIEW_SELECT_VARIABLE, MYF(0)); MYSQL_YYABORT; } - item= new Item_param((uint) (lex->tok_start - (uchar *) thd->query)); + item= new Item_param((uint) (lip->tok_start - thd->query)); if (!($$= item) || lex->param_list.push_back(item)) { my_message(ER_OUT_OF_RESOURCES, ER(ER_OUT_OF_RESOURCES), MYF(0)); @@ -7590,8 +7617,11 @@ signed_literal: literal: text_literal { $$ = $1; } | NUM_literal { $$ = $1; } - | NULL_SYM { $$ = new Item_null(); - Lex->next_state=MY_LEX_OPERATOR_OR_IDENT;} + | NULL_SYM + { + $$ = new Item_null(); + YYTHD->m_lip->next_state=MY_LEX_OPERATOR_OR_IDENT; + } | FALSE_SYM { $$= new Item_int((char*) "FALSE",0,1); } | TRUE_SYM { $$= new Item_int((char*) "TRUE",1,1); } | HEX_NUM { $$ = new Item_hex_string($1.str, $1.length);} @@ -7681,8 +7711,10 @@ order_ident: simple_ident: ident { + THD *thd= YYTHD; + LEX *lex= thd->lex; + Lex_input_stream *lip= thd->m_lip; sp_variable_t *spv; - LEX *lex = Lex; sp_pcontext *spc = lex->spcont; if (spc && (spv = spc->find_variable(&$1))) { @@ -7695,7 +7727,7 @@ simple_ident: Item_splocal *splocal; splocal= new Item_splocal($1, spv->offset, spv->type, - lex->tok_start_prev - + lip->tok_start_prev - lex->sphead->m_tmp_query); #ifndef DBUG_OFF if (splocal) @@ -8291,7 +8323,11 @@ option_value_list: option_type_value: { - if (Lex->sphead) + THD *thd= YYTHD; + LEX *lex= thd->lex; + Lex_input_stream *lip= thd->m_lip; + + if (lex->sphead) { /* If we are in SP we want have own LEX for each assignment. @@ -8303,9 +8339,8 @@ option_type_value: QQ: May be we should simply prohibit group assignments in SP? */ - LEX *lex; - Lex->sphead->reset_lex(YYTHD); - lex= Lex; + Lex->sphead->reset_lex(thd); + lex= thd->lex; /* Set new LEX as if we at start of set rule. */ lex->sql_command= SQLCOM_SET_OPTION; @@ -8313,12 +8348,14 @@ option_type_value: lex->option_type=OPT_SESSION; lex->var_list.empty(); lex->one_shot_set= 0; - lex->sphead->m_tmp_query= lex->tok_start; + lex->sphead->m_tmp_query= lip->tok_start; } } ext_option_value { - LEX *lex= Lex; + THD *thd= YYTHD; + LEX *lex= thd->lex; + Lex_input_stream *lip= thd->m_lip; if (lex->sphead) { @@ -8340,24 +8377,24 @@ option_type_value: /* Extract the query statement from the tokenizer. The - end is either lex->ptr, if there was no lookahead, - lex->tok_end otherwise. + end is either lip->ptr, if there was no lookahead, + lip->tok_end otherwise. */ if (yychar == YYEMPTY) - qbuff.length= lex->ptr - sp->m_tmp_query; + qbuff.length= lip->ptr - sp->m_tmp_query; else - qbuff.length= lex->tok_end - sp->m_tmp_query; + qbuff.length= lip->tok_end - sp->m_tmp_query; - if (!(qbuff.str= alloc_root(YYTHD->mem_root, qbuff.length + 5))) + if (!(qbuff.str= alloc_root(thd->mem_root, qbuff.length + 5))) MYSQL_YYABORT; - strmake(strmake(qbuff.str, "SET ", 4), (char *)sp->m_tmp_query, + strmake(strmake(qbuff.str, "SET ", 4), sp->m_tmp_query, qbuff.length); qbuff.length+= 4; i->m_query= qbuff; sp->add_instr(i); } - lex->sphead->restore_lex(YYTHD); + lex->sphead->restore_lex(thd); } }; @@ -9615,7 +9652,9 @@ trigger_tail: TRIGGER_SYM remember_name sp_name trg_action_time trg_event ON remember_name table_ident FOR_SYM remember_name EACH_SYM ROW_SYM { - LEX *lex= Lex; + THD *thd= YYTHD; + LEX *lex= thd->lex; + Lex_input_stream *lip= thd->m_lip; sp_head *sp; if (lex->sphead) @@ -9626,9 +9665,9 @@ trigger_tail: if (!(sp= new sp_head())) MYSQL_YYABORT; - sp->reset_thd_mem_root(YYTHD); + sp->reset_thd_mem_root(thd); sp->init(lex); - sp->init_sp_name(YYTHD, $3); + sp->init_sp_name(thd, $3); lex->stmt_definition_begin= $2; lex->ident.str= $7; @@ -9642,12 +9681,12 @@ trigger_tail: stored procedure, otherwise yylex will chop it into pieces at each ';'. */ - sp->m_old_cmq= YYTHD->client_capabilities & CLIENT_MULTI_QUERIES; - YYTHD->client_capabilities &= ~CLIENT_MULTI_QUERIES; + sp->m_old_cmq= thd->client_capabilities & CLIENT_MULTI_QUERIES; + thd->client_capabilities &= ~CLIENT_MULTI_QUERIES; bzero((char *)&lex->sp_chistics, sizeof(st_sp_chistics)); lex->sphead->m_chistics= &lex->sp_chistics; - lex->sphead->m_body_begin= lex->ptr; + lex->sphead->m_body_begin= lip->ptr; while (my_isspace(system_charset_info, lex->sphead->m_body_begin[0])) ++lex->sphead->m_body_begin; } @@ -9726,24 +9765,30 @@ sp_tail: } '(' { - LEX *lex= Lex; + THD *thd= YYTHD; + LEX *lex= thd->lex; + Lex_input_stream *lip= thd->m_lip; - lex->sphead->m_param_begin= lex->tok_start+1; + lex->sphead->m_param_begin= lip->tok_start+1; } sp_pdparam_list ')' { - LEX *lex= Lex; + THD *thd= YYTHD; + LEX *lex= thd->lex; + Lex_input_stream *lip= thd->m_lip; - lex->sphead->m_param_end= lex->tok_start; + lex->sphead->m_param_end= lip->tok_start; bzero((char *)&lex->sp_chistics, sizeof(st_sp_chistics)); } sp_c_chistics { - LEX *lex= Lex; + THD *thd= YYTHD; + LEX *lex= thd->lex; + Lex_input_stream *lip= thd->m_lip; lex->sphead->m_chistics= &lex->sp_chistics; - lex->sphead->m_body_begin= lex->tok_start; + lex->sphead->m_body_begin= lip->tok_start; } sp_proc_stmt { From 10dd997019733e7545d91ae590d0af59df55f8fd Mon Sep 17 00:00:00 2001 From: "malff/marcsql@weblab.(none)" <> Date: Wed, 25 Apr 2007 11:38:11 -0600 Subject: [PATCH 02/29] code review fix --- sql/sql_lex.cc | 6 +++--- sql/sql_lex.h | 5 ++++- sql/sql_parse.cc | 9 +++++---- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/sql/sql_lex.cc b/sql/sql_lex.cc index 95b9a77d411..ecdbb654ffd 100644 --- a/sql/sql_lex.cc +++ b/sql/sql_lex.cc @@ -122,7 +122,8 @@ Lex_input_stream::Lex_input_stream(THD *thd, tok_start_prev(NULL), buf(buffer), next_state(MY_LEX_START), - found_semicolon(NULL) + found_semicolon(NULL), + ignore_space(test(thd->variables.sql_mode & MODE_IGNORE_SPACE)) { } @@ -192,7 +193,6 @@ void lex_start(THD *thd) lex->select_lex.udf_list.empty(); lex->current_select= &lex->select_lex; lex->yacc_yyss=lex->yacc_yyvs=0; - lex->ignore_space=test(thd->variables.sql_mode & MODE_IGNORE_SPACE); lex->sql_command= lex->orig_sql_command= SQLCOM_END; lex->duplicates= DUP_ERROR; lex->ignore= 0; @@ -656,7 +656,7 @@ int MYSQLlex(void *arg, void *yythd) } length= (uint) (lip->ptr - lip->tok_start)-1; start= lip->ptr; - if (lex->ignore_space) + if (lip->ignore_space) { /* If we find a space then this can't be an identifier. We notice this diff --git a/sql/sql_lex.h b/sql/sql_lex.h index d580b2016ae..d07de73d5b9 100644 --- a/sql/sql_lex.h +++ b/sql/sql_lex.h @@ -951,6 +951,9 @@ public: /** Position of ';' in the stream, to delimit multiple queries. */ const char* found_semicolon; + + /** SQL_MODE = IGNORE_SPACE. */ + bool ignore_space; }; @@ -1069,7 +1072,7 @@ typedef struct st_lex : public Query_tables_list uint8 create_view_algorithm; uint8 create_view_check; bool drop_if_exists, drop_temporary, local_file, one_shot_set; - bool in_comment, ignore_space, verbose, no_write_to_binlog; + bool in_comment, verbose, no_write_to_binlog; bool tx_chain, tx_release; /* Special JOIN::prepare mode: changing of query is prohibited. diff --git a/sql/sql_parse.cc b/sql/sql_parse.cc index 0505f0d3574..22689a3dfa1 100644 --- a/sql/sql_parse.cc +++ b/sql/sql_parse.cc @@ -5990,16 +5990,14 @@ void mysql_parse(THD *thd, const char *inBuf, uint length, - first, call query_cache_send_result_to_client, - second, if caching failed, initialise the lexical and syntactic parser. The problem is that the query cache depends on a clean initialization - of the thd and thd->lex structures, which happen to be implemented - by: + of (among others) lex->safe_to_cache_query and thd->server_status, + which are reset respectively in - lex_start() - mysql_reset_thd_for_next_command() So, initializing the lexical analyser *before* using the query cache is required for the cache to work properly. FIXME: cleanup the dependencies in the code to simplify this. */ - Lex_input_stream lip(thd, inBuf, length); - thd->m_lip= &lip; lex_start(thd); mysql_reset_thd_for_next_command(thd); @@ -6010,6 +6008,9 @@ void mysql_parse(THD *thd, const char *inBuf, uint length, sp_cache_flush_obsolete(&thd->sp_proc_cache); sp_cache_flush_obsolete(&thd->sp_func_cache); + Lex_input_stream lip(thd, inBuf, length); + thd->m_lip= &lip; + int err= MYSQLparse(thd); *found_semicolon= lip.found_semicolon; From 62fd6aa69664cf98d96458d30aa18b39b5b6bc1d Mon Sep 17 00:00:00 2001 From: "malff/marcsql@weblab.(none)" <> Date: Wed, 25 Apr 2007 21:38:12 -0600 Subject: [PATCH 03/29] manual merge 5.0-runtime -> 5.1->runtime, with 25411 part I --- sql/event_data_objects.cc | 31 ++-- sql/ha_ndbcluster.cc | 2 +- sql/ha_ndbcluster_binlog.cc | 3 +- sql/log_event.cc | 5 +- sql/mysql_priv.h | 6 +- sql/slave.cc | 3 +- sql/sp.cc | 9 +- sql/sp_head.cc | 27 +--- sql/sp_head.h | 4 +- sql/sql_class.h | 11 ++ sql/sql_lex.cc | 284 +++++++++++++++++++----------------- sql/sql_lex.h | 71 +++++++-- sql/sql_parse.cc | 103 ++++++++----- sql/sql_partition.cc | 6 +- sql/sql_prepare.cc | 10 +- sql/sql_trigger.cc | 10 +- sql/sql_view.cc | 10 +- sql/sql_yacc.yy | 184 ++++++++++++++--------- 18 files changed, 479 insertions(+), 300 deletions(-) diff --git a/sql/event_data_objects.cc b/sql/event_data_objects.cc index f5847e603e8..4b788a8f7d8 100644 --- a/sql/event_data_objects.cc +++ b/sql/event_data_objects.cc @@ -156,10 +156,14 @@ void Event_parse_data::init_body(THD *thd) { DBUG_ENTER("Event_parse_data::init_body"); - DBUG_PRINT("info", ("body: '%s' body_begin: 0x%lx end: 0x%lx", body_begin, - (long) body_begin, (long) thd->lex->ptr)); - body.length= thd->lex->ptr - body_begin; + /* This method is called from within the parser, from sql_yacc.yy */ + DBUG_ASSERT(thd->m_lip != NULL); + + DBUG_PRINT("info", ("body: '%s' body_begin: 0x%lx end: 0x%lx", body_begin, + (long) body_begin, (long) thd->m_lip->ptr)); + + body.length= thd->m_lip->ptr - body_begin; const char *body_end= body_begin + body.length - 1; /* Trim nuls or close-comments ('*'+'/') or spaces at the end */ @@ -1912,15 +1916,20 @@ Event_job_data::execute(THD *thd, bool drop) thd->query= sp_sql.c_ptr_safe(); thd->query_length= sp_sql.length(); - lex_start(thd, thd->query, thd->query_length); - - if (MYSQLparse(thd) || thd->is_fatal_error) { - sql_print_error("Event Scheduler: " - "%serror during compilation of %s.%s", - thd->is_fatal_error ? "fatal " : "", - (const char *) dbname.str, (const char *) name.str); - goto end; + Lex_input_stream lip(thd, thd->query, thd->query_length); + thd->m_lip= &lip; + lex_start(thd); + int err= MYSQLparse(thd); + + if (err || thd->is_fatal_error) + { + sql_print_error("Event Scheduler: " + "%serror during compilation of %s.%s", + thd->is_fatal_error ? "fatal " : "", + (const char *) dbname.str, (const char *) name.str); + goto end; + } } { diff --git a/sql/ha_ndbcluster.cc b/sql/ha_ndbcluster.cc index 58d34d99174..d1e6ae56cb2 100644 --- a/sql/ha_ndbcluster.cc +++ b/sql/ha_ndbcluster.cc @@ -6564,7 +6564,7 @@ int ndb_create_table_from_engine(THD *thd, const char *db, LEX *old_lex= thd->lex, newlex; thd->lex= &newlex; newlex.current_select= NULL; - lex_start(thd, "", 0); + lex_start(thd); int res= ha_create_table_from_engine(thd, db, table_name); thd->lex= old_lex; return res; diff --git a/sql/ha_ndbcluster_binlog.cc b/sql/ha_ndbcluster_binlog.cc index c65c81c088c..a7c2d1f51e9 100644 --- a/sql/ha_ndbcluster_binlog.cc +++ b/sql/ha_ndbcluster_binlog.cc @@ -230,6 +230,7 @@ static void run_query(THD *thd, char *buf, char *end, ulonglong save_thd_options= thd->options; DBUG_ASSERT(sizeof(save_thd_options) == sizeof(thd->options)); NET save_net= thd->net; + const char* found_semicolon= NULL; bzero((char*) &thd->net, sizeof(NET)); thd->query_length= end - buf; @@ -239,7 +240,7 @@ static void run_query(THD *thd, char *buf, char *end, thd->options&= ~OPTION_BIN_LOG; DBUG_PRINT("query", ("%s", thd->query)); - mysql_parse(thd, thd->query, thd->query_length); + mysql_parse(thd, thd->query, thd->query_length, &found_semicolon); if (print_error && thd->query_error) { diff --git a/sql/log_event.cc b/sql/log_event.cc index 048db4ac95d..591a91f2011 100644 --- a/sql/log_event.cc +++ b/sql/log_event.cc @@ -5863,7 +5863,7 @@ int Rows_log_event::do_apply_event(RELAY_LOG_INFO const *rli) Table_map_log_event::do_apply_event() we don't call mysql_init_query() as that may reset the binlog format. */ - lex_start(thd, NULL, 0); + lex_start(thd); while ((error= lock_tables(thd, rli->tables_to_lock, rli->tables_to_lock_count, &need_reopen))) @@ -6513,7 +6513,8 @@ int Table_map_log_event::do_apply_event(RELAY_LOG_INFO const *rli) initialized, so we should call lex_start(); to be even safer, we call mysql_init_query() which does a more complete set of inits. */ - mysql_init_query(thd, NULL, 0); + lex_start(thd); + mysql_reset_thd_for_next_command(thd); /* Check if the slave is set to use SBR. If so, it should switch to using RBR until the end of the "statement", i.e., next diff --git a/sql/mysql_priv.h b/sql/mysql_priv.h index 3a01ed27f61..d9386c9f4d7 100644 --- a/sql/mysql_priv.h +++ b/sql/mysql_priv.h @@ -838,13 +838,15 @@ bool do_rename(THD *thd, TABLE_LIST *ren_table, char *new_db, bool skip_error); bool mysql_change_db(THD *thd, const LEX_STRING *new_db_name, bool force_switch); -void mysql_parse(THD *thd,char *inBuf,uint length); + +void mysql_parse(THD *thd, const char *inBuf, uint length, + const char ** semicolon); + bool mysql_test_parse_for_slave(THD *thd,char *inBuf,uint length); bool is_update_query(enum enum_sql_command command); bool alloc_query(THD *thd, const char *packet, uint packet_length); void mysql_init_select(LEX *lex); void mysql_reset_thd_for_next_command(THD *thd); -void mysql_init_query(THD *thd, const char *buf, uint length); bool mysql_new_select(LEX *lex, bool move_down); void create_select_for_variable(const char *var_name); void mysql_init_multi_delete(LEX *lex); diff --git a/sql/slave.cc b/sql/slave.cc index f1b9ea8476b..f89a56e2087 100644 --- a/sql/slave.cc +++ b/sql/slave.cc @@ -923,6 +923,7 @@ static int create_table_from_dump(THD* thd, MYSQL *mysql, const char* db, handler *file; ulonglong save_options; NET *net= &mysql->net; + const char *found_semicolon= NULL; DBUG_ENTER("create_table_from_dump"); packet_len= my_net_read(net); // read create table statement @@ -974,7 +975,7 @@ static int create_table_from_dump(THD* thd, MYSQL *mysql, const char* db, thd->db = (char*)db; DBUG_ASSERT(thd->db != 0); thd->db_length= strlen(thd->db); - mysql_parse(thd, thd->query, packet_len); // run create table + mysql_parse(thd, thd->query, packet_len, &found_semicolon); // run create table thd->db = save_db; // leave things the way the were before thd->db_length= save_db_length; thd->options = save_options; diff --git a/sql/sp.cc b/sql/sp.cc index 49b8b304e76..0a99be3f970 100644 --- a/sql/sp.cc +++ b/sql/sp.cc @@ -384,10 +384,15 @@ db_load_routine(THD *thd, int type, sp_name *name, sp_head **sphp, if ((ret= sp_use_new_db(thd, name->m_db, &old_db, 1, &dbchanged))) goto end; - lex_start(thd, defstr.c_ptr(), defstr.length()); + { + Lex_input_stream lip(thd, defstr.c_ptr(), defstr.length()); + thd->m_lip= &lip; + lex_start(thd); + ret= MYSQLparse(thd); + } thd->spcont= 0; - if (MYSQLparse(thd) || thd->is_fatal_error || newlex.sphead == NULL) + if (ret || thd->is_fatal_error || newlex.sphead == NULL) { sp_head *sp= newlex.sphead; diff --git a/sql/sp_head.cc b/sql/sp_head.cc index 04a7b2574a4..89fcd80abcf 100644 --- a/sql/sp_head.cc +++ b/sql/sp_head.cc @@ -557,6 +557,7 @@ sp_head::init_strings(THD *thd, LEX *lex) const char *endp; /* Used to trim the end */ /* During parsing, we must use thd->mem_root */ MEM_ROOT *root= thd->mem_root; + Lex_input_stream *lip=thd->m_lip; if (m_param_begin && m_param_end) { @@ -565,7 +566,7 @@ sp_head::init_strings(THD *thd, LEX *lex) } /* If ptr has overrun end_of_query then end_of_query is the end */ - endp= (lex->ptr > lex->end_of_query ? lex->end_of_query : lex->ptr); + endp= (lip->ptr > lip->end_of_query ? lip->end_of_query : lip->ptr); /* Trim "garbage" at the end. This is sometimes needed with the "/ * ! VERSION... * /" wrapper in dump files. @@ -574,8 +575,8 @@ sp_head::init_strings(THD *thd, LEX *lex) m_body.length= endp - m_body_begin; m_body.str= strmake_root(root, m_body_begin, m_body.length); - m_defstr.length= endp - lex->buf; - m_defstr.str= strmake_root(root, lex->buf, m_defstr.length); + m_defstr.length= endp - lip->buf; + m_defstr.str= strmake_root(root, lip->buf, m_defstr.length); DBUG_VOID_RETURN; } @@ -1815,25 +1816,13 @@ sp_head::reset_lex(THD *thd) DBUG_ENTER("sp_head::reset_lex"); LEX *sublex; LEX *oldlex= thd->lex; - my_lex_states org_next_state= oldlex->next_state; (void)m_lex.push_front(oldlex); thd->lex= sublex= new st_lex; - /* Reset most stuff. The length arguments doesn't matter here. */ - lex_start(thd, oldlex->buf, (ulong) (oldlex->end_of_query - oldlex->ptr)); + /* Reset most stuff. */ + lex_start(thd); - /* - next_state is normally the same (0), but it happens that we swap lex in - "mid-sentence", so we must restore it. - */ - sublex->next_state= org_next_state; - /* We must reset ptr and end_of_query again */ - sublex->ptr= oldlex->ptr; - sublex->end_of_query= oldlex->end_of_query; - sublex->tok_start= oldlex->tok_start; - sublex->tok_end= oldlex->tok_end; - sublex->yylineno= oldlex->yylineno; /* And keep the SP stuff too */ sublex->sphead= oldlex->sphead; sublex->spcont= oldlex->spcont; @@ -1866,10 +1855,6 @@ sp_head::restore_lex(THD *thd) if (! oldlex) return; // Nothing to restore - // Update some state in the old one first - oldlex->ptr= sublex->ptr; - oldlex->tok_end= sublex->tok_end; - oldlex->next_state= sublex->next_state; oldlex->trg_table_fields.push_back(&sublex->trg_table_fields); /* diff --git a/sql/sp_head.h b/sql/sp_head.h index 551707fa7bd..51ca529bed0 100644 --- a/sql/sp_head.h +++ b/sql/sp_head.h @@ -176,7 +176,9 @@ public: */ HASH m_sroutines; // Pointers set during parsing - const char *m_param_begin, *m_param_end, *m_body_begin; + const char *m_param_begin; + const char *m_param_end; + const char *m_body_begin; /* Security context for stored routine which should be run under diff --git a/sql/sql_class.h b/sql/sql_class.h index 48a88087fd3..fd88df4bcc8 100644 --- a/sql/sql_class.h +++ b/sql/sql_class.h @@ -31,6 +31,7 @@ class Load_log_event; class Slave_log_event; class sp_rcontext; class sp_cache; +class Lex_input_stream; class Rows_log_event; enum enum_enable_or_disable { LEAVE_AS_IS, ENABLE, DISABLE }; @@ -1423,6 +1424,16 @@ public: */ query_id_t first_query_id; } binlog_evt_union; + + /** + Character input stream consumed by the lexical analyser, + used during parsing. + Note that since the parser is not re-entrant, we keep only one input + stream here. This member is valid only when executing code during parsing, + and may point to invalid memory after that. + */ + Lex_input_stream *m_lip; + #ifdef WITH_PARTITION_STORAGE_ENGINE partition_info *work_part_info; #endif diff --git a/sql/sql_lex.cc b/sql/sql_lex.cc index 4a162478388..24c4f42b32a 100644 --- a/sql/sql_lex.cc +++ b/sql/sql_lex.cc @@ -33,13 +33,13 @@ sys_var *trg_new_row_fake_var= (sys_var*) 0x01; /* Macros to look like lex */ -#define yyGet() ((uchar) *(lex->ptr++)) -#define yyGetLast() ((uchar) lex->ptr[-1]) -#define yyPeek() ((uchar) lex->ptr[0]) -#define yyPeek2() ((uchar) lex->ptr[1]) -#define yyUnget() lex->ptr-- -#define yySkip() lex->ptr++ -#define yyLength() ((uint) (lex->ptr - lex->tok_start)-1) +#define yyGet() ((uchar) *(lip->ptr++)) +#define yyGetLast() ((uchar) lip->ptr[-1]) +#define yyPeek() ((uchar) lip->ptr[0]) +#define yyPeek2() ((uchar) lip->ptr[1]) +#define yyUnget() lip->ptr-- +#define yySkip() lip->ptr++ +#define yyLength() ((uint) (lip->ptr - lip->tok_start)-1) /* Longest standard keyword name */ #define TOCK_NAME_LENGTH 24 @@ -120,6 +120,28 @@ st_parsing_options::reset() allows_derived= TRUE; } +Lex_input_stream::Lex_input_stream(THD *thd, + const char* buffer, + unsigned int length) +: m_thd(thd), + yylineno(1), + yytoklen(0), + yylval(NULL), + ptr(buffer), + tok_start(NULL), + tok_end(NULL), + end_of_query(buffer + length), + tok_start_prev(NULL), + buf(buffer), + next_state(MY_LEX_START), + found_semicolon(NULL), + ignore_space(test(thd->variables.sql_mode & MODE_IGNORE_SPACE)) +{ +} + +Lex_input_stream::~Lex_input_stream() +{} + /* This is called before every query that is to be parsed. @@ -127,14 +149,12 @@ st_parsing_options::reset() (We already do too much here) */ -void lex_start(THD *thd, const char *buf, uint length) +void lex_start(THD *thd) { LEX *lex= thd->lex; DBUG_ENTER("lex_start"); lex->thd= lex->unit.thd= thd; - lex->buf= lex->ptr= buf; - lex->end_of_query= buf+length; lex->context_stack.empty(); lex->unit.init_query(); @@ -167,14 +187,11 @@ void lex_start(THD *thd, const char *buf, uint length) lex->stmt_prepare_mode= FALSE; lex->derived_tables= 0; lex->lock_option= TL_READ; - lex->found_semicolon= 0; lex->safe_to_cache_query= 1; lex->leaf_tables_insert= 0; lex->parsing_options.reset(); lex->empty_field_list_on_rset= 0; lex->select_lex.select_number= 1; - lex->next_state=MY_LEX_START; - lex->yylineno = 1; lex->in_comment=0; lex->length=0; lex->part_info= 0; @@ -184,7 +201,6 @@ void lex_start(THD *thd, const char *buf, uint length) lex->select_lex.ftfunc_list= &lex->select_lex.ftfunc_list_alloc; lex->select_lex.group_list.empty(); lex->select_lex.order_list.empty(); - lex->ignore_space=test(thd->variables.sql_mode & MODE_IGNORE_SPACE); lex->sql_command= SQLCOM_END; lex->duplicates= DUP_ERROR; lex->ignore= 0; @@ -236,24 +252,24 @@ void lex_end(LEX *lex) } -static int find_keyword(LEX *lex, uint len, bool function) +static int find_keyword(Lex_input_stream *lip, uint len, bool function) { - const char *tok= lex->tok_start; + const char *tok= lip->tok_start; SYMBOL *symbol= get_hash_symbol(tok, len, function); if (symbol) { - lex->yylval->symbol.symbol=symbol; - lex->yylval->symbol.str= (char*) tok; - lex->yylval->symbol.length=len; - + lip->yylval->symbol.symbol=symbol; + lip->yylval->symbol.str= (char*) tok; + lip->yylval->symbol.length=len; + if ((symbol->tok == NOT_SYM) && - (lex->thd->variables.sql_mode & MODE_HIGH_NOT_PRECEDENCE)) + (lip->m_thd->variables.sql_mode & MODE_HIGH_NOT_PRECEDENCE)) return NOT2_SYM; if ((symbol->tok == OR_OR_SYM) && - !(lex->thd->variables.sql_mode & MODE_PIPES_AS_CONCAT)) + !(lip->m_thd->variables.sql_mode & MODE_PIPES_AS_CONCAT)) return OR2_SYM; - + return symbol->tok; } return 0; @@ -286,12 +302,12 @@ bool is_lex_native_function(const LEX_STRING *name) /* make a copy of token before ptr and set yytoklen */ -static LEX_STRING get_token(LEX *lex,uint length) +static LEX_STRING get_token(Lex_input_stream *lip, uint length) { LEX_STRING tmp; yyUnget(); // ptr points now after last token char - tmp.length=lex->yytoklen=length; - tmp.str=(char*) lex->thd->strmake((char*) lex->tok_start,tmp.length); + tmp.length=lip->yytoklen=length; + tmp.str= lip->m_thd->strmake(lip->tok_start, tmp.length); return tmp; } @@ -302,15 +318,16 @@ static LEX_STRING get_token(LEX *lex,uint length) future to operate multichar strings (like ucs2) */ -static LEX_STRING get_quoted_token(LEX *lex,uint length, char quote) +static LEX_STRING get_quoted_token(Lex_input_stream *lip, + uint length, char quote) { LEX_STRING tmp; const char *from, *end; char *to; yyUnget(); // ptr points now after last token char - tmp.length=lex->yytoklen=length; - tmp.str=(char*) lex->thd->alloc(tmp.length+1); - for (from= lex->tok_start, to= tmp.str, end= to+length ; + tmp.length= lip->yytoklen=length; + tmp.str=(char*) lip->m_thd->alloc(tmp.length+1); + for (from= lip->tok_start, to= tmp.str, end= to+length ; to != end ; ) { @@ -327,31 +344,31 @@ static LEX_STRING get_quoted_token(LEX *lex,uint length, char quote) Fix sometimes to do only one scan of the string */ -static char *get_text(LEX *lex) +static char *get_text(Lex_input_stream *lip) { reg1 uchar c,sep; uint found_escape=0; - CHARSET_INFO *cs= lex->thd->charset(); + CHARSET_INFO *cs= lip->m_thd->charset(); sep= yyGetLast(); // String should end with this - while (lex->ptr != lex->end_of_query) + while (lip->ptr != lip->end_of_query) { c = yyGet(); #ifdef USE_MB { int l; if (use_mb(cs) && - (l = my_ismbchar(cs, lex->ptr-1, lex->end_of_query))) { - lex->ptr += l-1; + (l = my_ismbchar(cs, lip->ptr-1, lip->end_of_query))) { + lip->ptr += l-1; continue; } } #endif if (c == '\\' && - !(lex->thd->variables.sql_mode & MODE_NO_BACKSLASH_ESCAPES)) + !(lip->m_thd->variables.sql_mode & MODE_NO_BACKSLASH_ESCAPES)) { // Escaped character found_escape=1; - if (lex->ptr == lex->end_of_query) + if (lip->ptr == lip->end_of_query) return 0; yySkip(); } @@ -369,15 +386,15 @@ static char *get_text(LEX *lex) const char *str, *end; char *start; - str=lex->tok_start+1; - end=lex->ptr-1; - if (!(start= (char*) lex->thd->alloc((uint) (end-str)+1))) + str=lip->tok_start+1; + end=lip->ptr-1; + if (!(start= (char*) lip->m_thd->alloc((uint) (end-str)+1))) return (char*) ""; // Sql_alloc has set error flag if (!found_escape) { - lex->yytoklen=(uint) (end-str); - memcpy(start,str,lex->yytoklen); - start[lex->yytoklen]=0; + lip->yytoklen=(uint) (end-str); + memcpy(start,str,lip->yytoklen); + start[lip->yytoklen]=0; } else { @@ -395,7 +412,7 @@ static char *get_text(LEX *lex) continue; } #endif - if (!(lex->thd->variables.sql_mode & MODE_NO_BACKSLASH_ESCAPES) && + if (!(lip->m_thd->variables.sql_mode & MODE_NO_BACKSLASH_ESCAPES) && *str == '\\' && str+1 != end) { switch(*++str) { @@ -432,7 +449,7 @@ static char *get_text(LEX *lex) *to++ = *str; } *to=0; - lex->yytoklen=(uint) (to-start); + lip->yytoklen=(uint) (to-start); } return start; } @@ -545,19 +562,21 @@ int MYSQLlex(void *arg, void *yythd) int tokval, result_state; uint length; enum my_lex_states state; - LEX *lex= ((THD *)yythd)->lex; + THD *thd= (THD *)yythd; + Lex_input_stream *lip= thd->m_lip; + LEX *lex= thd->lex; YYSTYPE *yylval=(YYSTYPE*) arg; - CHARSET_INFO *cs= ((THD *) yythd)->charset(); + CHARSET_INFO *cs= thd->charset(); uchar *state_map= cs->state_map; uchar *ident_map= cs->ident_map; - lex->yylval=yylval; // The global state + lip->yylval=yylval; // The global state - lex->tok_start_prev= lex->tok_start; + lip->tok_start_prev= lip->tok_start; - lex->tok_start=lex->tok_end=lex->ptr; - state=lex->next_state; - lex->next_state=MY_LEX_OPERATOR_OR_IDENT; + lip->tok_start=lip->tok_end=lip->ptr; + state=lip->next_state; + lip->next_state=MY_LEX_OPERATOR_OR_IDENT; LINT_INIT(c); for (;;) { @@ -568,9 +587,9 @@ int MYSQLlex(void *arg, void *yythd) for (c=yyGet() ; (state_map[c] == MY_LEX_SKIP) ; c= yyGet()) { if (c == '\n') - lex->yylineno++; + lip->yylineno++; } - lex->tok_start=lex->ptr-1; // Start of real token + lip->tok_start=lip->ptr-1; // Start of real token state= (enum my_lex_states) state_map[c]; break; case MY_LEX_ESCAPE: @@ -589,13 +608,13 @@ int MYSQLlex(void *arg, void *yythd) state=MY_LEX_COMMENT; break; } - yylval->lex_str.str=(char*) (lex->ptr=lex->tok_start);// Set to first chr + yylval->lex_str.str=(char*) (lip->ptr=lip->tok_start);// Set to first chr yylval->lex_str.length=1; c=yyGet(); if (c != ')') - lex->next_state= MY_LEX_START; // Allow signed numbers + lip->next_state= MY_LEX_START; // Allow signed numbers if (c == ',') - lex->tok_start=lex->ptr; // Let tok_start point at next item + lip->tok_start=lip->ptr; // Let tok_start point at next item /* Check for a placeholder: it should not precede a possible identifier because of binlogging: when a placeholder is replaced with @@ -613,14 +632,14 @@ int MYSQLlex(void *arg, void *yythd) break; } /* Found N'string' */ - lex->tok_start++; // Skip N + lip->tok_start++; // Skip N yySkip(); // Skip ' - if (!(yylval->lex_str.str = get_text(lex))) + if (!(yylval->lex_str.str = get_text(lip))) { state= MY_LEX_CHAR; // Read char by char break; } - yylval->lex_str.length= lex->yytoklen; + yylval->lex_str.length= lip->yytoklen; return(NCHAR_STRING); case MY_LEX_IDENT_OR_HEX: @@ -643,21 +662,21 @@ int MYSQLlex(void *arg, void *yythd) result_state= IDENT_QUOTED; if (my_mbcharlen(cs, yyGetLast()) > 1) { - int l = my_ismbchar(cs, lex->ptr-1, lex->end_of_query); + int l = my_ismbchar(cs, lip->ptr-1, lip->end_of_query); if (l == 0) { state = MY_LEX_CHAR; continue; } - lex->ptr += l - 1; + lip->ptr += l - 1; } while (ident_map[c=yyGet()]) { if (my_mbcharlen(cs, c) > 1) { int l; - if ((l = my_ismbchar(cs, lex->ptr-1, lex->end_of_query)) == 0) + if ((l = my_ismbchar(cs, lip->ptr-1, lip->end_of_query)) == 0) break; - lex->ptr += l-1; + lip->ptr += l-1; } } } @@ -668,9 +687,9 @@ int MYSQLlex(void *arg, void *yythd) /* If there were non-ASCII characters, mark that we must convert */ result_state= result_state & 0x80 ? IDENT_QUOTED : IDENT; } - length= (uint) (lex->ptr - lex->tok_start)-1; - start= lex->ptr; - if (lex->ignore_space) + length= (uint) (lip->ptr - lip->tok_start)-1; + start= lip->ptr; + if (lip->ignore_space) { /* If we find a space then this can't be an identifier. We notice this @@ -678,19 +697,19 @@ int MYSQLlex(void *arg, void *yythd) */ for (; state_map[c] == MY_LEX_SKIP ; c= yyGet()); } - if (start == lex->ptr && c == '.' && ident_map[yyPeek()]) - lex->next_state=MY_LEX_IDENT_SEP; + if (start == lip->ptr && c == '.' && ident_map[yyPeek()]) + lip->next_state=MY_LEX_IDENT_SEP; else { // '(' must follow directly if function yyUnget(); - if ((tokval = find_keyword(lex,length,c == '('))) + if ((tokval = find_keyword(lip, length,c == '('))) { - lex->next_state= MY_LEX_START; // Allow signed numbers + lip->next_state= MY_LEX_START; // Allow signed numbers return(tokval); // Was keyword } yySkip(); // next state does a unget } - yylval->lex_str=get_token(lex,length); + yylval->lex_str=get_token(lip, length); /* Note: "SELECT _bla AS 'alias'" @@ -707,12 +726,12 @@ int MYSQLlex(void *arg, void *yythd) return(result_state); // IDENT or IDENT_QUOTED case MY_LEX_IDENT_SEP: // Found ident and now '.' - yylval->lex_str.str=(char*) lex->ptr; + yylval->lex_str.str=(char*) lip->ptr; yylval->lex_str.length=1; c=yyGet(); // should be '.' - lex->next_state= MY_LEX_IDENT_START;// Next is an ident (not a keyword) + lip->next_state= MY_LEX_IDENT_START;// Next is an ident (not a keyword) if (!ident_map[yyPeek()]) // Probably ` or " - lex->next_state= MY_LEX_START; + lip->next_state= MY_LEX_START; return((int) c); case MY_LEX_NUMBER_IDENT: // number or ident which num-start @@ -732,36 +751,36 @@ int MYSQLlex(void *arg, void *yythd) { yySkip(); while (my_isdigit(cs,yyGet())) ; - yylval->lex_str=get_token(lex,yyLength()); + yylval->lex_str=get_token(lip, yyLength()); return(FLOAT_NUM); } } yyUnget(); /* purecov: inspected */ } - else if (c == 'x' && (lex->ptr - lex->tok_start) == 2 && - lex->tok_start[0] == '0' ) + else if (c == 'x' && (lip->ptr - lip->tok_start) == 2 && + lip->tok_start[0] == '0' ) { // Varbinary while (my_isxdigit(cs,(c = yyGet()))) ; - if ((lex->ptr - lex->tok_start) >= 4 && !ident_map[c]) + if ((lip->ptr - lip->tok_start) >= 4 && !ident_map[c]) { - yylval->lex_str=get_token(lex,yyLength()); + yylval->lex_str=get_token(lip, yyLength()); yylval->lex_str.str+=2; // Skip 0x yylval->lex_str.length-=2; - lex->yytoklen-=2; + lip->yytoklen-=2; return (HEX_NUM); } yyUnget(); } - else if (c == 'b' && (lex->ptr - lex->tok_start) == 2 && - lex->tok_start[0] == '0' ) + else if (c == 'b' && (lip->ptr - lip->tok_start) == 2 && + lip->tok_start[0] == '0' ) { // b'bin-number' while (my_isxdigit(cs,(c = yyGet()))) ; - if ((lex->ptr - lex->tok_start) >= 4 && !ident_map[c]) + if ((lip->ptr - lip->tok_start) >= 4 && !ident_map[c]) { - yylval->lex_str= get_token(lex, yyLength()); + yylval->lex_str= get_token(lip, yyLength()); yylval->lex_str.str+= 2; // Skip 0x yylval->lex_str.length-= 2; - lex->yytoklen-= 2; + lip->yytoklen-= 2; return (BIN_NUM); } yyUnget(); @@ -778,9 +797,9 @@ int MYSQLlex(void *arg, void *yythd) if (my_mbcharlen(cs, c) > 1) { int l; - if ((l = my_ismbchar(cs, lex->ptr-1, lex->end_of_query)) == 0) + if ((l = my_ismbchar(cs, lip->ptr-1, lip->end_of_query)) == 0) break; - lex->ptr += l-1; + lip->ptr += l-1; } } } @@ -792,16 +811,16 @@ int MYSQLlex(void *arg, void *yythd) result_state= result_state & 0x80 ? IDENT_QUOTED : IDENT; } if (c == '.' && ident_map[yyPeek()]) - lex->next_state=MY_LEX_IDENT_SEP;// Next is '.' + lip->next_state=MY_LEX_IDENT_SEP;// Next is '.' - yylval->lex_str= get_token(lex,yyLength()); + yylval->lex_str= get_token(lip, yyLength()); return(result_state); case MY_LEX_USER_VARIABLE_DELIMITER: // Found quote char { uint double_quotes= 0; char quote_char= c; // Used char - lex->tok_start=lex->ptr; // Skip first ` + lip->tok_start=lip->ptr; // Skip first ` while ((c=yyGet())) { int var_length; @@ -819,23 +838,23 @@ int MYSQLlex(void *arg, void *yythd) #ifdef USE_MB else if (var_length < 1) break; // Error - lex->ptr+= var_length-1; + lip->ptr+= var_length-1; #endif } if (double_quotes) - yylval->lex_str=get_quoted_token(lex,yyLength() - double_quotes, + yylval->lex_str=get_quoted_token(lip, yyLength() - double_quotes, quote_char); else - yylval->lex_str=get_token(lex,yyLength()); + yylval->lex_str=get_token(lip, yyLength()); if (c == quote_char) yySkip(); // Skip end ` - lex->next_state= MY_LEX_START; + lip->next_state= MY_LEX_START; return(IDENT_QUOTED); } case MY_LEX_INT_OR_REAL: // Compleat int or incompleat real if (c != '.') { // Found complete integer number. - yylval->lex_str=get_token(lex,yyLength()); + yylval->lex_str=get_token(lip, yyLength()); return int_token(yylval->lex_str.str,yylval->lex_str.length); } // fall through @@ -853,47 +872,47 @@ int MYSQLlex(void *arg, void *yythd) break; } while (my_isdigit(cs,yyGet())) ; - yylval->lex_str=get_token(lex,yyLength()); + yylval->lex_str=get_token(lip, yyLength()); return(FLOAT_NUM); } - yylval->lex_str=get_token(lex,yyLength()); + yylval->lex_str=get_token(lip, yyLength()); return(DECIMAL_NUM); case MY_LEX_HEX_NUMBER: // Found x'hexstring' yyGet(); // Skip ' while (my_isxdigit(cs,(c = yyGet()))) ; - length=(lex->ptr - lex->tok_start); // Length of hexnum+3 + length=(lip->ptr - lip->tok_start); // Length of hexnum+3 if (!(length & 1) || c != '\'') { return(ABORT_SYM); // Illegal hex constant } yyGet(); // get_token makes an unget - yylval->lex_str=get_token(lex,length); + yylval->lex_str=get_token(lip, length); yylval->lex_str.str+=2; // Skip x' yylval->lex_str.length-=3; // Don't count x' and last ' - lex->yytoklen-=3; + lip->yytoklen-=3; return (HEX_NUM); case MY_LEX_BIN_NUMBER: // Found b'bin-string' yyGet(); // Skip ' while ((c= yyGet()) == '0' || c == '1'); - length= (lex->ptr - lex->tok_start); // Length of bin-num + 3 + length= (lip->ptr - lip->tok_start); // Length of bin-num + 3 if (c != '\'') return(ABORT_SYM); // Illegal hex constant yyGet(); // get_token makes an unget - yylval->lex_str= get_token(lex, length); + yylval->lex_str= get_token(lip, length); yylval->lex_str.str+= 2; // Skip b' yylval->lex_str.length-= 3; // Don't count b' and last ' - lex->yytoklen-= 3; + lip->yytoklen-= 3; return (BIN_NUM); case MY_LEX_CMP_OP: // Incomplete comparison operator if (state_map[yyPeek()] == MY_LEX_CMP_OP || state_map[yyPeek()] == MY_LEX_LONG_CMP_OP) yySkip(); - if ((tokval = find_keyword(lex,(uint) (lex->ptr - lex->tok_start),0))) + if ((tokval = find_keyword(lip, (uint) (lip->ptr - lip->tok_start),0))) { - lex->next_state= MY_LEX_START; // Allow signed numbers + lip->next_state= MY_LEX_START; // Allow signed numbers return(tokval); } state = MY_LEX_CHAR; // Something fishy found @@ -907,9 +926,9 @@ int MYSQLlex(void *arg, void *yythd) if (state_map[yyPeek()] == MY_LEX_CMP_OP) yySkip(); } - if ((tokval = find_keyword(lex,(uint) (lex->ptr - lex->tok_start),0))) + if ((tokval = find_keyword(lip, (uint) (lip->ptr - lip->tok_start),0))) { - lex->next_state= MY_LEX_START; // Found long op + lip->next_state= MY_LEX_START; // Found long op return(tokval); } state = MY_LEX_CHAR; // Something fishy found @@ -922,24 +941,24 @@ int MYSQLlex(void *arg, void *yythd) break; } yySkip(); - tokval = find_keyword(lex,2,0); // Is a bool operator - lex->next_state= MY_LEX_START; // Allow signed numbers + tokval = find_keyword(lip,2,0); // Is a bool operator + lip->next_state= MY_LEX_START; // Allow signed numbers return(tokval); case MY_LEX_STRING_OR_DELIMITER: - if (((THD *) yythd)->variables.sql_mode & MODE_ANSI_QUOTES) + if (thd->variables.sql_mode & MODE_ANSI_QUOTES) { state= MY_LEX_USER_VARIABLE_DELIMITER; break; } /* " used for strings */ case MY_LEX_STRING: // Incomplete text string - if (!(yylval->lex_str.str = get_text(lex))) + if (!(yylval->lex_str.str = get_text(lip))) { state= MY_LEX_CHAR; // Read char by char break; } - yylval->lex_str.length=lex->yytoklen; + yylval->lex_str.length=lip->yytoklen; return(TEXT_STRING); case MY_LEX_COMMENT: // Comment @@ -963,7 +982,7 @@ int MYSQLlex(void *arg, void *yythd) state=MY_LEX_START; if (my_isdigit(cs,yyPeek())) { // Version number - version=strtol((char*) lex->ptr,(char**) &lex->ptr,10); + version=strtol((char*) lip->ptr,(char**) &lip->ptr,10); } if (version <= MYSQL_VERSION_ID) { @@ -971,13 +990,13 @@ int MYSQLlex(void *arg, void *yythd) break; } } - while (lex->ptr != lex->end_of_query && + while (lip->ptr != lip->end_of_query && ((c=yyGet()) != '*' || yyPeek() != '/')) { if (c == '\n') - lex->yylineno++; + lip->yylineno++; } - if (lex->ptr != lex->end_of_query) + if (lip->ptr != lip->end_of_query) yySkip(); // remove last '/' state = MY_LEX_START; // Try again break; @@ -1002,14 +1021,13 @@ int MYSQLlex(void *arg, void *yythd) case MY_LEX_SEMICOLON: // optional line terminator if (yyPeek()) { - THD* thd= (THD*)yythd; if ((thd->client_capabilities & CLIENT_MULTI_STATEMENTS) && !lex->stmt_prepare_mode) { lex->safe_to_cache_query= 0; - lex->found_semicolon=(char*) lex->ptr; + lip->found_semicolon= lip->ptr; thd->server_status|= SERVER_MORE_RESULTS_EXISTS; - lex->next_state= MY_LEX_END; + lip->next_state= MY_LEX_END; return (END_OF_INPUT); } state= MY_LEX_CHAR; // Return ';' @@ -1017,15 +1035,15 @@ int MYSQLlex(void *arg, void *yythd) } /* fall true */ case MY_LEX_EOL: - if (lex->ptr >= lex->end_of_query) + if (lip->ptr >= lip->end_of_query) { - lex->next_state=MY_LEX_END; // Mark for next loop + lip->next_state=MY_LEX_END; // Mark for next loop return(END_OF_INPUT); } state=MY_LEX_CHAR; break; case MY_LEX_END: - lex->next_state=MY_LEX_END; + lip->next_state=MY_LEX_END; return(0); // We found end of input last time /* Actually real shouldn't start with . but allow them anyhow */ @@ -1045,26 +1063,26 @@ int MYSQLlex(void *arg, void *yythd) case MY_LEX_STRING_OR_DELIMITER: break; case MY_LEX_USER_END: - lex->next_state=MY_LEX_SYSTEM_VAR; + lip->next_state=MY_LEX_SYSTEM_VAR; break; default: - lex->next_state=MY_LEX_HOSTNAME; + lip->next_state=MY_LEX_HOSTNAME; break; } - yylval->lex_str.str=(char*) lex->ptr; + yylval->lex_str.str=(char*) lip->ptr; yylval->lex_str.length=1; return((int) '@'); case MY_LEX_HOSTNAME: // end '@' of user@hostname for (c=yyGet() ; my_isalnum(cs,c) || c == '.' || c == '_' || c == '$'; c= yyGet()) ; - yylval->lex_str=get_token(lex,yyLength()); + yylval->lex_str=get_token(lip, yyLength()); return(LEX_HOSTNAME); case MY_LEX_SYSTEM_VAR: - yylval->lex_str.str=(char*) lex->ptr; + yylval->lex_str.str=(char*) lip->ptr; yylval->lex_str.length=1; yySkip(); // Skip '@' - lex->next_state= (state_map[yyPeek()] == + lip->next_state= (state_map[yyPeek()] == MY_LEX_USER_VARIABLE_DELIMITER ? MY_LEX_OPERATOR_OR_IDENT : MY_LEX_IDENT_OR_KEYWORD); @@ -1081,16 +1099,16 @@ int MYSQLlex(void *arg, void *yythd) result_state= result_state & 0x80 ? IDENT_QUOTED : IDENT; if (c == '.') - lex->next_state=MY_LEX_IDENT_SEP; - length= (uint) (lex->ptr - lex->tok_start)-1; + lip->next_state=MY_LEX_IDENT_SEP; + length= (uint) (lip->ptr - lip->tok_start)-1; if (length == 0) return(ABORT_SYM); // Names must be nonempty. - if ((tokval= find_keyword(lex,length,0))) + if ((tokval= find_keyword(lip, length,0))) { yyUnget(); // Put back 'c' return(tokval); // Was keyword } - yylval->lex_str=get_token(lex,length); + yylval->lex_str=get_token(lip, length); return(result_state); } } diff --git a/sql/sql_lex.h b/sql/sql_lex.h index 1175776ffd3..278a59fe1ac 100644 --- a/sql/sql_lex.h +++ b/sql/sql_lex.h @@ -543,7 +543,7 @@ public: void set_thd(THD *thd_arg) { thd= thd_arg; } inline bool is_union (); - friend void lex_start(THD *thd, const char *buf, uint length); + friend void lex_start(THD *thd); friend int subselect_union_engine::exec(); List *get_unit_column_types(); @@ -744,7 +744,7 @@ public: void cut_subtree() { slave= 0; } bool test_limit(); - friend void lex_start(THD *thd, const char *buf, uint length); + friend void lex_start(THD *thd); st_select_lex() : n_sum_items(0), n_child_sum_items(0) {} void make_empty_select() { @@ -992,23 +992,67 @@ struct st_parsing_options }; +/** + This class represents the character input stream consumed during + lexical analysis. +*/ +class Lex_input_stream +{ +public: + Lex_input_stream(THD *thd, const char* buff, unsigned int length); + ~Lex_input_stream(); + + /** Current thread. */ + THD *m_thd; + + /** Current line number. */ + uint yylineno; + + /** Length of the last token parsed. */ + uint yytoklen; + + /** Interface with bison, value of the last token parsed. */ + LEX_YYSTYPE yylval; + + /** Pointer to the current position in the input stream. */ + const char* ptr; + + /** Starting position of the last token parsed. */ + const char* tok_start; + + /** Ending position of the last token parsed. */ + const char* tok_end; + + /** End of the query text in the input stream. */ + const char* end_of_query; + + /** Starting position of the previous token parsed. */ + const char* tok_start_prev; + + /** Begining of the query text in the input stream. */ + const char* buf; + + /** Current state of the lexical analyser. */ + enum my_lex_states next_state; + + /** Position of ';' in the stream, to delimit multiple queries. */ + const char* found_semicolon; + + /** SQL_MODE = IGNORE_SPACE. */ + bool ignore_space; +}; + + /* The state of the lex parsing. This is saved in the THD struct */ typedef struct st_lex : public Query_tables_list { - uint yylineno,yytoklen; /* Simulate lex */ - LEX_YYSTYPE yylval; SELECT_LEX_UNIT unit; /* most upper unit */ SELECT_LEX select_lex; /* first SELECT_LEX */ /* current SELECT_LEX in parsing */ SELECT_LEX *current_select; /* list of all SELECT_LEX */ SELECT_LEX *all_selects_list; - const char *buf; /* The beginning of string, used by SPs */ - const char *ptr,*tok_start,*tok_end,*end_of_query; - - /* The value of tok_start as they were one call of MYSQLlex before */ - const char *tok_start_prev; char *length,*dec,*change; LEX_STRING name; @@ -1017,7 +1061,6 @@ typedef struct st_lex : public Query_tables_list char *backup_dir; /* For RESTORE/BACKUP */ char* to_log; /* For PURGE MASTER LOGS TO */ char* x509_subject,*x509_issuer,*ssl_cipher; - char* found_semicolon; /* For multi queries - next query */ String *wild; sql_exchange *exchange; select_result *result; @@ -1101,7 +1144,6 @@ typedef struct st_lex : public Query_tables_list thr_lock_type lock_option; enum SSL_type ssl_type; /* defined in violite.h */ - enum my_lex_states next_state; enum enum_duplicates duplicates; enum enum_tx_isolation tx_isolation; enum enum_ha_read_modes ha_read_mode; @@ -1133,7 +1175,7 @@ typedef struct st_lex : public Query_tables_list uint8 create_view_algorithm; uint8 create_view_check; bool drop_if_exists, drop_temporary, local_file, one_shot_set; - bool in_comment, ignore_space, verbose, no_write_to_binlog; + bool in_comment, verbose, no_write_to_binlog; bool tx_chain, tx_release; /* Special JOIN::prepare mode: changing of query is prohibited. @@ -1210,7 +1252,8 @@ typedef struct st_lex : public Query_tables_list Pointers to part of LOAD DATA statement that should be rewritten during replication ("LOCAL 'filename' REPLACE INTO" part). */ - const char *fname_start, *fname_end; + const char *fname_start; + const char *fname_end; /* Reference to a struct that contains information in various commands @@ -1327,7 +1370,7 @@ struct st_lex_local: public st_lex extern void lex_init(void); extern void lex_free(void); -extern void lex_start(THD *thd, const char *buf, uint length); +extern void lex_start(THD *thd); extern void lex_end(LEX *lex); extern int MYSQLlex(void *arg, void *yythd); extern const char *skip_rear_comments(const char *ubegin, const char *uend); diff --git a/sql/sql_parse.cc b/sql/sql_parse.cc index df2ac4d72fa..d2f9d31a495 100644 --- a/sql/sql_parse.cc +++ b/sql/sql_parse.cc @@ -300,6 +300,7 @@ pthread_handler_t handle_bootstrap(void *arg) THD *thd=(THD*) arg; FILE *file=bootstrap_file; char *buff; + const char* found_semicolon= NULL; /* The following must be called before DBUG_ENTER */ thd->thread_stack= (char*) &thd; @@ -375,7 +376,7 @@ pthread_handler_t handle_bootstrap(void *arg) */ thd->query_id=next_query_id(); thd->set_time(); - mysql_parse(thd,thd->query,length); + mysql_parse(thd, thd->query, length, & found_semicolon); close_thread_tables(thd); // Free tables if (thd->is_fatal_error) @@ -889,17 +890,19 @@ bool dispatch_command(enum enum_server_command command, THD *thd, char *packet_end= thd->query + thd->query_length; /* 'b' stands for 'buffer' parameter', special for 'my_snprintf' */ const char *format= "%.*b"; + const char* found_semicolon= NULL; + general_log_print(thd, command, format, thd->query_length, thd->query); DBUG_PRINT("query",("%-.4096s",thd->query)); if (!(specialflag & SPECIAL_NO_PRIOR)) my_pthread_setprio(pthread_self(),QUERY_PRIOR); - mysql_parse(thd,thd->query, thd->query_length); + mysql_parse(thd, thd->query, thd->query_length, & found_semicolon); - while (!thd->killed && thd->lex->found_semicolon && !thd->net.report_error) + while (!thd->killed && found_semicolon && !thd->net.report_error) { - char *next_packet= thd->lex->found_semicolon; + char *next_packet= (char*) found_semicolon; net->no_send_error= 0; /* Multiple queries exits, execute them individually @@ -924,7 +927,7 @@ bool dispatch_command(enum enum_server_command command, THD *thd, thd->set_time(); /* Reset the query start time. */ /* TODO: set thd->lex->sql_command to SQLCOM_END here */ VOID(pthread_mutex_unlock(&LOCK_thread_count)); - mysql_parse(thd, next_packet, length); + mysql_parse(thd, next_packet, length, & found_semicolon); } if (!(specialflag & SPECIAL_NO_PRIOR)) @@ -946,7 +949,7 @@ bool dispatch_command(enum enum_server_command command, THD *thd, uint dummy; /* used as fields initializator */ - lex_start(thd, 0, 0); + lex_start(thd); statistic_increment(thd->status_var.com_stat[SQLCOM_SHOW_FIELDS], &LOCK_status); @@ -985,7 +988,10 @@ bool dispatch_command(enum enum_server_command command, THD *thd, break; /* init structures for VIEW processing */ table_list.select_lex= &(thd->lex->select_lex); - mysql_init_query(thd, "", 0); + + lex_start(thd); + mysql_reset_thd_for_next_command(thd); + thd->lex-> select_lex.table_list.link_in_list((byte*) &table_list, (byte**) &table_list.next_local); @@ -5038,20 +5044,6 @@ bool my_yyoverflow(short **yyss, YYSTYPE **yyvs, ulong *yystacksize) } -/**************************************************************************** - Initialize global thd variables needed for query -****************************************************************************/ - -void -mysql_init_query(THD *thd, const char *buf, uint length) -{ - DBUG_ENTER("mysql_init_query"); - lex_start(thd, buf, length); - mysql_reset_thd_for_next_command(thd); - DBUG_VOID_RETURN; -} - - /* Reset THD part responsible for command processing state. @@ -5263,22 +5255,55 @@ void mysql_init_multi_delete(LEX *lex) mysql_test_parse_for_slave() in this same file. */ -void mysql_parse(THD *thd, char *inBuf, uint length) +/** + Parse a query. + @param thd Current thread + @param inBuf Begining of the query text + @param length Length of the query text + @param [out] semicolon For multi queries, position of the character of + the next query in the query text. +*/ + +void mysql_parse(THD *thd, const char *inBuf, uint length, + const char ** found_semicolon) { DBUG_ENTER("mysql_parse"); DBUG_EXECUTE_IF("parser_debug", turn_parser_debug_on();); - mysql_init_query(thd, inBuf, length); + /* + Warning. + The purpose of query_cache_send_result_to_client() is to lookup the + query in the query cache first, to avoid parsing and executing it. + So, the natural implementation would be to: + - first, call query_cache_send_result_to_client, + - second, if caching failed, initialise the lexical and syntactic parser. + The problem is that the query cache depends on a clean initialization + of (among others) lex->safe_to_cache_query and thd->server_status, + which are reset respectively in + - lex_start() + - mysql_reset_thd_for_next_command() + So, initializing the lexical analyser *before* using the query cache + is required for the cache to work properly. + FIXME: cleanup the dependencies in the code to simplify this. + */ + lex_start(thd); + mysql_reset_thd_for_next_command(thd); - if (query_cache_send_result_to_client(thd, inBuf, length) <= 0) + if (query_cache_send_result_to_client(thd, (char*) inBuf, length) <= 0) { LEX *lex= thd->lex; - + sp_cache_flush_obsolete(&thd->sp_proc_cache); sp_cache_flush_obsolete(&thd->sp_func_cache); - - if (!MYSQLparse((void *)thd) && ! thd->is_fatal_error) + + Lex_input_stream lip(thd, inBuf, length); + thd->m_lip= &lip; + + int err= MYSQLparse(thd); + *found_semicolon= lip.found_semicolon; + + if (!err && ! thd->is_fatal_error) { #ifndef NO_EMBEDDED_ACCESS_CHECKS if (mqh_used && thd->user_connect && @@ -5301,8 +5326,8 @@ void mysql_parse(THD *thd, char *inBuf, uint length) PROCESSLIST. Note that we don't need LOCK_thread_count to modify query_length. */ - if (lex->found_semicolon && - (thd->query_length= (ulong)(lex->found_semicolon - thd->query))) + if (lip.found_semicolon && + (thd->query_length= (ulong)(lip.found_semicolon - thd->query))) thd->query_length--; /* Actually execute the query */ mysql_execute_command(thd); @@ -5329,6 +5354,12 @@ void mysql_parse(THD *thd, char *inBuf, uint length) thd->cleanup_after_query(); DBUG_ASSERT(thd->change_list.is_empty()); } + else + { + /* There are no multi queries in the cache. */ + *found_semicolon= NULL; + } + DBUG_VOID_RETURN; } @@ -5349,8 +5380,13 @@ bool mysql_test_parse_for_slave(THD *thd, char *inBuf, uint length) bool error= 0; DBUG_ENTER("mysql_test_parse_for_slave"); - mysql_init_query(thd, inBuf, length); - if (!MYSQLparse((void*) thd) && ! thd->is_fatal_error && + Lex_input_stream lip(thd, inBuf, length); + thd->m_lip= &lip; + lex_start(thd); + mysql_reset_thd_for_next_command(thd); + int err= MYSQLparse((void*) thd); + + if (!err && ! thd->is_fatal_error && all_tables_not_ok(thd,(TABLE_LIST*) lex->select_lex.table_list.first)) error= 1; /* Ignore question */ thd->end_statement(); @@ -6401,8 +6437,9 @@ bool check_simple_select() if (lex->current_select != &lex->select_lex) { char command[80]; - strmake(command, lex->yylval->symbol.str, - min(lex->yylval->symbol.length, sizeof(command)-1)); + Lex_input_stream *lip= thd->m_lip; + strmake(command, lip->yylval->symbol.str, + min(lip->yylval->symbol.length, sizeof(command)-1)); my_error(ER_CANT_USE_OPTION_HERE, MYF(0), command); return 1; } diff --git a/sql/sql_partition.cc b/sql/sql_partition.cc index d445c8bfbe0..47260b945fc 100644 --- a/sql/sql_partition.cc +++ b/sql/sql_partition.cc @@ -3718,7 +3718,11 @@ bool mysql_unpack_partition(THD *thd, thd->lex= &lex; thd->variables.character_set_client= system_charset_info; - lex_start(thd, part_buf, part_info_len); + + Lex_input_stream lip(thd, part_buf, part_info_len); + thd->m_lip= &lip; + + lex_start(thd); /* We need to use the current SELECT_LEX since I need to keep the Name_resolution_context object which is referenced from the diff --git a/sql/sql_prepare.cc b/sql/sql_prepare.cc index 31a6c7af04a..ca1b29ea57d 100644 --- a/sql/sql_prepare.cc +++ b/sql/sql_prepare.cc @@ -2850,10 +2850,14 @@ bool Prepared_statement::prepare(const char *packet, uint packet_len) old_stmt_arena= thd->stmt_arena; thd->stmt_arena= this; - lex_start(thd, thd->query, thd->query_length); - lex->stmt_prepare_mode= TRUE; - error= MYSQLparse((void *)thd) || thd->is_fatal_error || + Lex_input_stream lip(thd, thd->query, thd->query_length); + thd->m_lip= &lip; + lex_start(thd); + lex->stmt_prepare_mode= TRUE; + int err= MYSQLparse((void *)thd); + + error= err || thd->is_fatal_error || thd->net.report_error || init_param_array(this); /* diff --git a/sql/sql_trigger.cc b/sql/sql_trigger.cc index 7b4deba527a..89ac444efa9 100644 --- a/sql/sql_trigger.cc +++ b/sql/sql_trigger.cc @@ -976,10 +976,14 @@ bool Table_triggers_list::check_n_load(THD *thd, const char *db, LEX_STRING *trg_definer= it_definer++; thd->variables.sql_mode= (ulong)*trg_sql_mode; - lex_start(thd, trg_create_str->str, trg_create_str->length); - thd->spcont= 0; - if (MYSQLparse((void *)thd) || thd->is_fatal_error) + Lex_input_stream lip(thd, trg_create_str->str, trg_create_str->length); + thd->m_lip= &lip; + lex_start(thd); + thd->spcont= 0; + int err= MYSQLparse((void *)thd); + + if (err || thd->is_fatal_error) { /* Currently sphead is always deleted in case of a parse error */ DBUG_ASSERT(lex.sphead == 0); diff --git a/sql/sql_view.cc b/sql/sql_view.cc index 453cbbc091e..5eaac8acc77 100644 --- a/sql/sql_view.cc +++ b/sql/sql_view.cc @@ -985,10 +985,14 @@ bool mysql_make_view(THD *thd, File_parser *parser, TABLE_LIST *table, now Lex placed in statement memory */ table->view= lex= thd->lex= (LEX*) new(thd->mem_root) st_lex_local; - lex_start(thd, table->query.str, table->query.length); - view_select= &lex->select_lex; - view_select->select_number= ++thd->select_number; + { + Lex_input_stream lip(thd, table->query.str, table->query.length); + thd->m_lip= &lip; + lex_start(thd); + view_select= &lex->select_lex; + view_select->select_number= ++thd->select_number; + ulong save_mode= thd->variables.sql_mode; /* switch off modes which can prevent normal parsing of VIEW - MODE_REAL_AS_FLOAT affect only CREATE TABLE parsing diff --git a/sql/sql_yacc.yy b/sql/sql_yacc.yy index 0c7d2fc2187..61d78ae831a 100644 --- a/sql/sql_yacc.yy +++ b/sql/sql_yacc.yy @@ -104,12 +104,13 @@ const LEX_STRING null_lex_str={0,0}; void my_parse_error(const char *s) { THD *thd= current_thd; + Lex_input_stream *lip= thd->m_lip; - char *yytext= (char*) thd->lex->tok_start; + const char *yytext= lip->tok_start; /* Push an error into the error stack */ my_printf_error(ER_PARSE_ERROR, ER(ER_PARSE_ERROR), MYF(0), s, - (yytext ? (char*) yytext : ""), - thd->lex->yylineno); + (yytext ? yytext : ""), + lip->yylineno); } /** @@ -1837,7 +1838,9 @@ opt_ev_comment: /* empty */ { $$= 0; } ev_sql_stmt: { - LEX *lex= Lex; + THD *thd= YYTHD; + LEX *lex= thd->lex; + Lex_input_stream *lip= thd->m_lip; /* This stops the following : @@ -1874,22 +1877,23 @@ ev_sql_stmt: bzero((char *)&lex->sp_chistics, sizeof(st_sp_chistics)); lex->sphead->m_chistics= &lex->sp_chistics; - lex->sphead->m_body_begin= lex->ptr; + lex->sphead->m_body_begin= lip->ptr; - Lex->event_parse_data->body_begin= lex->ptr; + lex->event_parse_data->body_begin= lip->ptr; } ev_sql_stmt_inner { - LEX *lex=Lex; + THD *thd= YYTHD; + LEX *lex= thd->lex; /* return back to the original memory root ASAP */ - lex->sphead->init_strings(YYTHD, lex); - lex->sphead->restore_thd_mem_root(YYTHD); + lex->sphead->init_strings(thd, lex); + lex->sphead->restore_thd_mem_root(thd); lex->sp_chistics.suid= SP_IS_SUID; //always the definer! - Lex->event_parse_data->init_body(YYTHD); + lex->event_parse_data->init_body(thd); } ; @@ -1983,7 +1987,9 @@ create_function_tail: } | '(' { - LEX *lex= Lex; + THD *thd= YYTHD; + LEX *lex= thd->lex; + Lex_input_stream *lip= thd->m_lip; sp_head *sp; /* @@ -2003,9 +2009,9 @@ create_function_tail: } /* Order is important here: new - reset - init */ sp= new sp_head(); - sp->reset_thd_mem_root(YYTHD); + sp->reset_thd_mem_root(thd); sp->init(lex); - sp->init_sp_name(YYTHD, lex->spname); + sp->init_sp_name(thd, lex->spname); sp->m_type= TYPE_ENUM_FUNCTION; lex->sphead= sp; @@ -2014,15 +2020,17 @@ create_function_tail: stored procedure, otherwise yylex will chop it into pieces at each ';'. */ - $$= YYTHD->client_capabilities & CLIENT_MULTI_QUERIES; - YYTHD->client_capabilities &= ~CLIENT_MULTI_QUERIES; - lex->sphead->m_param_begin= lex->tok_start+1; + $$= thd->client_capabilities & CLIENT_MULTI_QUERIES; + thd->client_capabilities &= ~CLIENT_MULTI_QUERIES; + lex->sphead->m_param_begin= lip->tok_start+1; } sp_fdparam_list ')' { - LEX *lex= Lex; + THD *thd= YYTHD; + LEX *lex= thd->lex; + Lex_input_stream *lip= thd->m_lip; - lex->sphead->m_param_end= lex->tok_start; + lex->sphead->m_param_end= lip->tok_start; } RETURNS_SYM { @@ -2057,10 +2065,12 @@ create_function_tail: } sp_c_chistics { - LEX *lex= Lex; + THD *thd= YYTHD; + LEX *lex= thd->lex; + Lex_input_stream *lip= thd->m_lip; lex->sphead->m_chistics= &lex->sp_chistics; - lex->sphead->m_body_begin= lex->tok_start; + lex->sphead->m_body_begin= lip->tok_start; } sp_proc_stmt { @@ -2666,14 +2676,18 @@ sp_proc_stmt_if: sp_proc_stmt_statement: { - LEX *lex= Lex; + THD *thd= YYTHD; + LEX *lex= thd->lex; + Lex_input_stream *lip= thd->m_lip; - lex->sphead->reset_lex(YYTHD); - lex->sphead->m_tmp_query= lex->tok_start; + lex->sphead->reset_lex(thd); + lex->sphead->m_tmp_query= lip->tok_start; } statement { - LEX *lex= Lex; + THD *thd= YYTHD; + LEX *lex= thd->lex; + Lex_input_stream *lip= thd->m_lip; sp_head *sp= lex->sphead; sp->m_flags|= sp_get_flags_for_command(lex); @@ -2700,15 +2714,15 @@ sp_proc_stmt_statement: lex->tok_end otherwise. */ if (yychar == YYEMPTY) - i->m_query.length= lex->ptr - sp->m_tmp_query; + i->m_query.length= lip->ptr - sp->m_tmp_query; else - i->m_query.length= lex->tok_end - sp->m_tmp_query; - i->m_query.str= strmake_root(YYTHD->mem_root, + i->m_query.length= lip->tok_end - sp->m_tmp_query; + i->m_query.str= strmake_root(thd->mem_root, sp->m_tmp_query, i->m_query.length); sp->add_instr(i); } - sp->restore_lex(YYTHD); + sp->restore_lex(thd); } ; @@ -6212,10 +6226,18 @@ select_item: }; remember_name: - { $$=(char*) Lex->tok_start; }; + { + THD *thd= YYTHD; + Lex_input_stream *lip= thd->m_lip; + $$= (char*) lip->tok_start; + }; remember_end: - { $$=(char*) Lex->tok_end; }; + { + THD *thd= YYTHD; + Lex_input_stream *lip= thd->m_lip; + $$=(char*) lip->tok_end; + }; select_item2: table_wild { $$=$1; } /* table.* */ @@ -7983,12 +8005,14 @@ procedure_list2: procedure_item: remember_name expr { - LEX *lex= Lex; - if (add_proc_to_list(lex->thd, $2)) + THD *thd= YYTHD; + Lex_input_stream *lip= thd->m_lip; + + if (add_proc_to_list(thd, $2)) MYSQL_YYABORT; if (!$2->name) - $2->set_name($1,(uint) ((char*) lex->tok_end - $1), - YYTHD->charset()); + $2->set_name($1,(uint) ((char*) lip->tok_end - $1), + thd->charset()); } ; @@ -9078,13 +9102,16 @@ use: USE_SYM ident load: LOAD DATA_SYM { - LEX *lex=Lex; + THD *thd= YYTHD; + LEX *lex= thd->lex; + Lex_input_stream *lip= thd->m_lip; + if (lex->sphead) { my_error(ER_SP_BADSTATEMENT, MYF(0), "LOAD DATA"); MYSQL_YYABORT; } - lex->fname_start= lex->ptr; + lex->fname_start= lip->ptr; } load_data {} @@ -9118,8 +9145,10 @@ load_data: } opt_duplicate INTO { - LEX *lex=Lex; - lex->fname_end= lex->ptr; + THD *thd= YYTHD; + LEX *lex= thd->lex; + Lex_input_stream *lip= thd->m_lip; + lex->fname_end= lip->ptr; } TABLE_SYM table_ident { @@ -9300,14 +9329,15 @@ param_marker: PARAM_MARKER { THD *thd= YYTHD; - LEX *lex= thd->lex; + LEX *lex= thd->lex; + Lex_input_stream *lip= thd->m_lip; Item_param *item; if (! lex->parsing_options.allows_variable) { my_error(ER_VIEW_SELECT_VARIABLE, MYF(0)); MYSQL_YYABORT; } - item= new Item_param((uint) (lex->tok_start - thd->query)); + item= new Item_param((uint) (lip->tok_start - thd->query)); if (!($$= item) || lex->param_list.push_back(item)) { my_message(ER_OUT_OF_RESOURCES, ER(ER_OUT_OF_RESOURCES), MYF(0)); @@ -9330,8 +9360,11 @@ signed_literal: literal: text_literal { $$ = $1; } | NUM_literal { $$ = $1; } - | NULL_SYM { $$ = new Item_null(); - Lex->next_state=MY_LEX_OPERATOR_OR_IDENT;} + | NULL_SYM + { + $$ = new Item_null(); + YYTHD->m_lip->next_state=MY_LEX_OPERATOR_OR_IDENT; + } | FALSE_SYM { $$= new Item_int((char*) "FALSE",0,1); } | TRUE_SYM { $$= new Item_int((char*) "TRUE",1,1); } | HEX_NUM { $$ = new Item_hex_string($1.str, $1.length);} @@ -9421,8 +9454,10 @@ order_ident: simple_ident: ident { + THD *thd= YYTHD; + LEX *lex= thd->lex; + Lex_input_stream *lip= thd->m_lip; sp_variable_t *spv; - LEX *lex = Lex; sp_pcontext *spc = lex->spcont; if (spc && (spv = spc->find_variable(&$1))) { @@ -9435,7 +9470,7 @@ simple_ident: Item_splocal *splocal; splocal= new Item_splocal($1, spv->offset, spv->type, - lex->tok_start_prev - + lip->tok_start_prev - lex->sphead->m_tmp_query); #ifndef DBUG_OFF if (splocal) @@ -10085,7 +10120,11 @@ option_value_list: option_type_value: { - if (Lex->sphead) + THD *thd= YYTHD; + LEX *lex= thd->lex; + Lex_input_stream *lip= thd->m_lip; + + if (lex->sphead) { /* If we are in SP we want have own LEX for each assignment. @@ -10097,9 +10136,8 @@ option_type_value: QQ: May be we should simply prohibit group assignments in SP? */ - LEX *lex; - Lex->sphead->reset_lex(YYTHD); - lex= Lex; + lex->sphead->reset_lex(thd); + lex= thd->lex; /* Set new LEX as if we at start of set rule. */ lex->sql_command= SQLCOM_SET_OPTION; @@ -10107,12 +10145,14 @@ option_type_value: lex->option_type=OPT_SESSION; lex->var_list.empty(); lex->one_shot_set= 0; - lex->sphead->m_tmp_query= lex->tok_start; + lex->sphead->m_tmp_query= lip->tok_start; } } ext_option_value { - LEX *lex= Lex; + THD *thd= YYTHD; + LEX *lex= thd->lex; + Lex_input_stream *lip= thd->m_lip; if (lex->sphead) { @@ -10134,15 +10174,15 @@ option_type_value: /* Extract the query statement from the tokenizer. The - end is either lex->ptr, if there was no lookahead, - lex->tok_end otherwise. + end is either lip->ptr, if there was no lookahead, + lip->tok_end otherwise. */ if (yychar == YYEMPTY) - qbuff.length= lex->ptr - sp->m_tmp_query; + qbuff.length= lip->ptr - sp->m_tmp_query; else - qbuff.length= lex->tok_end - sp->m_tmp_query; + qbuff.length= lip->tok_end - sp->m_tmp_query; - if (!(qbuff.str= alloc_root(YYTHD->mem_root, qbuff.length + 5))) + if (!(qbuff.str= alloc_root(thd->mem_root, qbuff.length + 5))) MYSQL_YYABORT; strmake(strmake(qbuff.str, "SET ", 4), sp->m_tmp_query, @@ -10151,7 +10191,7 @@ option_type_value: i->m_query= qbuff; sp->add_instr(i); } - lex->sphead->restore_lex(YYTHD); + lex->sphead->restore_lex(thd); } }; @@ -11400,7 +11440,9 @@ trigger_tail: TRIGGER_SYM remember_name sp_name trg_action_time trg_event ON remember_name table_ident FOR_SYM remember_name EACH_SYM ROW_SYM { - LEX *lex= Lex; + THD *thd= YYTHD; + LEX *lex= thd->lex; + Lex_input_stream *lip= thd->m_lip; sp_head *sp; if (lex->sphead) @@ -11411,9 +11453,9 @@ trigger_tail: if (!(sp= new sp_head())) MYSQL_YYABORT; - sp->reset_thd_mem_root(YYTHD); + sp->reset_thd_mem_root(thd); sp->init(lex); - sp->init_sp_name(YYTHD, $3); + sp->init_sp_name(thd, $3); lex->stmt_definition_begin= $2; lex->ident.str= $7; lex->ident.length= $10 - $7; @@ -11426,12 +11468,12 @@ trigger_tail: stored procedure, otherwise yylex will chop it into pieces at each ';'. */ - $$= YYTHD->client_capabilities & CLIENT_MULTI_QUERIES; - YYTHD->client_capabilities &= ~CLIENT_MULTI_QUERIES; + $$= thd->client_capabilities & CLIENT_MULTI_QUERIES; + thd->client_capabilities &= ~CLIENT_MULTI_QUERIES; bzero((char *)&lex->sp_chistics, sizeof(st_sp_chistics)); lex->sphead->m_chistics= &lex->sp_chistics; - lex->sphead->m_body_begin= lex->ptr; + lex->sphead->m_body_begin= lip->ptr; while (my_isspace(system_charset_info, lex->sphead->m_body_begin[0])) ++lex->sphead->m_body_begin; } @@ -11510,24 +11552,30 @@ sp_tail: } '(' { - LEX *lex= Lex; + THD *thd= YYTHD; + LEX *lex= thd->lex; + Lex_input_stream *lip= thd->m_lip; - lex->sphead->m_param_begin= lex->tok_start+1; + lex->sphead->m_param_begin= lip->tok_start+1; } sp_pdparam_list ')' { - LEX *lex= Lex; + THD *thd= YYTHD; + LEX *lex= thd->lex; + Lex_input_stream *lip= thd->m_lip; - lex->sphead->m_param_end= lex->tok_start; + lex->sphead->m_param_end= lip->tok_start; bzero((char *)&lex->sp_chistics, sizeof(st_sp_chistics)); } sp_c_chistics { - LEX *lex= Lex; + THD *thd= YYTHD; + LEX *lex= thd->lex; + Lex_input_stream *lip= thd->m_lip; lex->sphead->m_chistics= &lex->sp_chistics; - lex->sphead->m_body_begin= lex->tok_start; + lex->sphead->m_body_begin= lip->tok_start; } sp_proc_stmt { From 012f841fd43a19c042aaec0af863f589b65f3f54 Mon Sep 17 00:00:00 2001 From: "malff/marcsql@weblab.(none)" <> Date: Fri, 27 Apr 2007 17:14:25 -0600 Subject: [PATCH 04/29] Bug#21513 (SP having body starting with quoted label rendered unusable) Before this fix, the parser would sometime change where a token starts by altering Lex_input_string::tok_start, which later confused the code in sql_yacc.yy that needs to capture the source code of a SQL statement, like to represent the body of a stored procedure. This line of code in sql_lex.cc : case MY_LEX_USER_VARIABLE_DELIMITER: lip->tok_start= lip->ptr; // Skip first ` would ... and cause the bug reported. In general, the responsibility of sql_lex.cc is to *find* where token are in the SQL text, but is *not* to make up fake or incomplete tokens. With a quoted label like `my_label`, the token starts on the first quote. Extracting the token value should not change that (it did). With this fix, the lexical analysis has been cleaned up to not change lip->tok_start (in the case found for this bug). The functions get_token() and get_quoted_token() now have an extra parameters, used when some characters from the beginning of the token need to be skipped when extracting a token value, like when extracting 'AB' from '0xAB', for example, for a HEX_NUM token. This exposed a bad assumption in Item_hex_string and Item_bin_string, which has been fixed: The assumption was that the string given, 'AB', was in fact preceded in memory by '0x', which might be false (it can be preceded by "x'" and followed by "'" -- or not be preceded by valid memory at all) If a name is needed for Item_hex_string or Item_bin_string, the name is taken from the original and true source code ('0xAB'), and assigned in the select_item rule, instead of relying on assumptions related to how memory is used. --- mysql-test/r/sp.result | 7 +++++ mysql-test/t/sp.test | 11 ++++++++ sql/item.cc | 2 -- sql/sql_lex.cc | 64 ++++++++++++++++++++---------------------- sql/sql_yacc.yy | 14 +++++---- 5 files changed, 56 insertions(+), 42 deletions(-) diff --git a/mysql-test/r/sp.result b/mysql-test/r/sp.result index 9ba6a356db2..b5b79af031e 100644 --- a/mysql-test/r/sp.result +++ b/mysql-test/r/sp.result @@ -6118,6 +6118,13 @@ Warning 1265 Data truncated for column 'bug5274_f1' at row 1 Warning 1265 Data truncated for column 'bug5274_f1' at row 1 DROP FUNCTION bug5274_f1| DROP FUNCTION bug5274_f2| +drop procedure if exists proc_21513| +create procedure proc_21513()`my_label`:BEGIN END| +show create procedure proc_21513| +Procedure sql_mode Create Procedure +proc_21513 CREATE DEFINER=`root`@`localhost` PROCEDURE `proc_21513`() +`my_label`:BEGIN END +drop procedure proc_21513| End of 5.0 tests. drop table t1,t2; CREATE TABLE t1 (a int auto_increment primary key) engine=MyISAM; diff --git a/mysql-test/t/sp.test b/mysql-test/t/sp.test index 64876ae0584..97677838d48 100644 --- a/mysql-test/t/sp.test +++ b/mysql-test/t/sp.test @@ -7054,6 +7054,17 @@ SELECT bug5274_f2()| DROP FUNCTION bug5274_f1| DROP FUNCTION bug5274_f2| +# +# Bug#21513 (SP having body starting with quoted label rendered unusable) +# +--disable_warnings +drop procedure if exists proc_21513| +--enable_warnings + +create procedure proc_21513()`my_label`:BEGIN END| +show create procedure proc_21513| + +drop procedure proc_21513| ### --echo End of 5.0 tests. diff --git a/sql/item.cc b/sql/item.cc index ed5bd67d096..a2a7d46edc3 100644 --- a/sql/item.cc +++ b/sql/item.cc @@ -4602,7 +4602,6 @@ inline uint char_val(char X) Item_hex_string::Item_hex_string(const char *str, uint str_length) { - name=(char*) str-2; // Lex makes this start with 0x max_length=(str_length+1)/2; char *ptr=(char*) sql_alloc(max_length+1); if (!ptr) @@ -4713,7 +4712,6 @@ Item_bin_string::Item_bin_string(const char *str, uint str_length) uchar bits= 0; uint power= 1; - name= (char*) str - 2; max_length= (str_length + 7) >> 3; char *ptr= (char*) sql_alloc(max_length + 1); if (!ptr) diff --git a/sql/sql_lex.cc b/sql/sql_lex.cc index ecdbb654ffd..fabd9a0e003 100644 --- a/sql/sql_lex.cc +++ b/sql/sql_lex.cc @@ -262,12 +262,12 @@ bool is_keyword(const char *name, uint len) /* make a copy of token before ptr and set yytoklen */ -static LEX_STRING get_token(Lex_input_stream *lip, uint length) +static LEX_STRING get_token(Lex_input_stream *lip, uint skip, uint length) { LEX_STRING tmp; yyUnget(); // ptr points now after last token char tmp.length=lip->yytoklen=length; - tmp.str= lip->m_thd->strmake(lip->tok_start, tmp.length); + tmp.str= lip->m_thd->strmake(lip->tok_start + skip, tmp.length); return tmp; } @@ -279,6 +279,7 @@ static LEX_STRING get_token(Lex_input_stream *lip, uint length) */ static LEX_STRING get_quoted_token(Lex_input_stream *lip, + uint skip, uint length, char quote) { LEX_STRING tmp; @@ -286,9 +287,10 @@ static LEX_STRING get_quoted_token(Lex_input_stream *lip, yyUnget(); // ptr points now after last token char tmp.length=lip->yytoklen=length; tmp.str=(char*) lip->m_thd->alloc(tmp.length+1); - for (from= (byte*) lip->tok_start, to= (byte*) tmp.str, end= to+length ; - to != end ; - ) + from= (byte*) lip->tok_start + skip; + to= (byte*) tmp.str; + end= to+length; + for ( ; to != end; ) { if ((*to++= *from++) == quote) from++; // Skip double quotes @@ -676,7 +678,7 @@ int MYSQLlex(void *arg, void *yythd) } yySkip(); // next state does a unget } - yylval->lex_str=get_token(lip, length); + yylval->lex_str=get_token(lip, 0, length); /* Note: "SELECT _bla AS 'alias'" @@ -718,7 +720,7 @@ int MYSQLlex(void *arg, void *yythd) { yySkip(); while (my_isdigit(cs,yyGet())) ; - yylval->lex_str=get_token(lip, yyLength()); + yylval->lex_str=get_token(lip, 0, yyLength()); return(FLOAT_NUM); } } @@ -730,10 +732,8 @@ int MYSQLlex(void *arg, void *yythd) while (my_isxdigit(cs,(c = yyGet()))) ; if ((lip->ptr - lip->tok_start) >= 4 && !ident_map[c]) { - yylval->lex_str=get_token(lip, yyLength()); - yylval->lex_str.str+=2; // Skip 0x - yylval->lex_str.length-=2; - lip->yytoklen-=2; + /* skip '0x' */ + yylval->lex_str=get_token(lip, 2, yyLength()-2); return (HEX_NUM); } yyUnget(); @@ -744,10 +744,8 @@ int MYSQLlex(void *arg, void *yythd) while (my_isxdigit(cs,(c = yyGet()))) ; if ((lip->ptr - lip->tok_start) >= 4 && !ident_map[c]) { - yylval->lex_str= get_token(lip, yyLength()); - yylval->lex_str.str+= 2; // Skip 0x - yylval->lex_str.length-= 2; - lip->yytoklen-= 2; + /* Skip '0b' */ + yylval->lex_str= get_token(lip, 2, yyLength()-2); return (BIN_NUM); } yyUnget(); @@ -782,14 +780,13 @@ int MYSQLlex(void *arg, void *yythd) if (c == '.' && ident_map[yyPeek()]) lip->next_state=MY_LEX_IDENT_SEP;// Next is '.' - yylval->lex_str= get_token(lip, yyLength()); + yylval->lex_str= get_token(lip, 0, yyLength()); return(result_state); case MY_LEX_USER_VARIABLE_DELIMITER: // Found quote char { uint double_quotes= 0; char quote_char= c; // Used char - lip->tok_start=lip->ptr; // Skip first ` while ((c=yyGet())) { int var_length; @@ -813,19 +810,20 @@ int MYSQLlex(void *arg, void *yythd) #endif } if (double_quotes) - yylval->lex_str=get_quoted_token(lip, yyLength() - double_quotes, + yylval->lex_str=get_quoted_token(lip, 1, + yyLength() - double_quotes -1, quote_char); else - yylval->lex_str=get_token(lip, yyLength()); + yylval->lex_str=get_token(lip, 1, yyLength() -1); if (c == quote_char) yySkip(); // Skip end ` lip->next_state= MY_LEX_START; return(IDENT_QUOTED); } - case MY_LEX_INT_OR_REAL: // Compleat int or incompleat real + case MY_LEX_INT_OR_REAL: // Complete int or incomplete real if (c != '.') { // Found complete integer number. - yylval->lex_str=get_token(lip, yyLength()); + yylval->lex_str=get_token(lip, 0, yyLength()); return int_token(yylval->lex_str.str,yylval->lex_str.length); } // fall through @@ -843,10 +841,10 @@ int MYSQLlex(void *arg, void *yythd) break; } while (my_isdigit(cs,yyGet())) ; - yylval->lex_str=get_token(lip, yyLength()); + yylval->lex_str=get_token(lip, 0, yyLength()); return(FLOAT_NUM); } - yylval->lex_str=get_token(lip, yyLength()); + yylval->lex_str=get_token(lip, 0, yyLength()); return(DECIMAL_NUM); case MY_LEX_HEX_NUMBER: // Found x'hexstring' @@ -858,10 +856,9 @@ int MYSQLlex(void *arg, void *yythd) return(ABORT_SYM); // Illegal hex constant } yyGet(); // get_token makes an unget - yylval->lex_str=get_token(lip, length); - yylval->lex_str.str+=2; // Skip x' - yylval->lex_str.length-=3; // Don't count x' and last ' - lip->yytoklen-=3; + yylval->lex_str=get_token(lip, + 2, // skip x' + length-3); // don't count x' and last ' return (HEX_NUM); case MY_LEX_BIN_NUMBER: // Found b'bin-string' @@ -871,11 +868,10 @@ int MYSQLlex(void *arg, void *yythd) if (c != '\'') return(ABORT_SYM); // Illegal hex constant yyGet(); // get_token makes an unget - yylval->lex_str= get_token(lip, length); - yylval->lex_str.str+= 2; // Skip b' - yylval->lex_str.length-= 3; // Don't count b' and last ' - lip->yytoklen-= 3; - return (BIN_NUM); + yylval->lex_str= get_token(lip, + 2, // skip b' + length-3); // don't count b' and last ' + return (BIN_NUM); case MY_LEX_CMP_OP: // Incomplete comparison operator if (state_map[yyPeek()] == MY_LEX_CMP_OP || @@ -1047,7 +1043,7 @@ int MYSQLlex(void *arg, void *yythd) for (c=yyGet() ; my_isalnum(cs,c) || c == '.' || c == '_' || c == '$'; c= yyGet()) ; - yylval->lex_str=get_token(lip, yyLength()); + yylval->lex_str=get_token(lip, 0, yyLength()); return(LEX_HOSTNAME); case MY_LEX_SYSTEM_VAR: yylval->lex_str.str=(char*) lip->ptr; @@ -1079,7 +1075,7 @@ int MYSQLlex(void *arg, void *yythd) yyUnget(); // Put back 'c' return(tokval); // Was keyword } - yylval->lex_str=get_token(lip, length); + yylval->lex_str=get_token(lip, 0, length); return(result_state); } } diff --git a/sql/sql_yacc.yy b/sql/sql_yacc.yy index 82d148c4a1e..390a991c7b6 100644 --- a/sql/sql_yacc.yy +++ b/sql/sql_yacc.yy @@ -4439,21 +4439,23 @@ select_item_list: select_item: remember_name select_item2 remember_end select_alias { - if (add_item_to_list(YYTHD, $2)) + THD *thd= YYTHD; + DBUG_ASSERT($1 < $3); + + if (add_item_to_list(thd, $2)) MYSQL_YYABORT; if ($4.str) { $2->is_autogenerated_name= FALSE; $2->set_name($4.str, $4.length, system_charset_info); } - else if (!$2->name) { - char *str = $1; - if (str[-1] == '`') - str--; - $2->set_name(str,(uint) ($3 - str), YYTHD->charset()); + else if (!$2->name) + { + $2->set_name($1, (uint) ($3 - $1), thd->charset()); } }; + remember_name: { THD *thd= YYTHD; From 9d6860b62aed269515c842b40a245c1535afb25c Mon Sep 17 00:00:00 2001 From: "malff/marcsql@weblab.(none)" <> Date: Mon, 30 Apr 2007 15:36:29 -0600 Subject: [PATCH 05/29] fix merge issues --- mysql-test/r/sp.result | 7 +++++++ sql/sql_lex.cc | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/mysql-test/r/sp.result b/mysql-test/r/sp.result index e3406e4bbde..ac394a9df3d 100644 --- a/mysql-test/r/sp.result +++ b/mysql-test/r/sp.result @@ -6210,6 +6210,13 @@ Warning 1265 Data truncated for column 'bug5274_f1' at row 1 Warning 1265 Data truncated for column 'bug5274_f1' at row 1 DROP FUNCTION bug5274_f1| DROP FUNCTION bug5274_f2| +drop procedure if exists proc_21513| +create procedure proc_21513()`my_label`:BEGIN END| +show create procedure proc_21513| +Procedure sql_mode Create Procedure +proc_21513 CREATE DEFINER=`root`@`localhost` PROCEDURE `proc_21513`() +`my_label`:BEGIN END +drop procedure proc_21513| End of 5.0 tests. drop table t1,t2; CREATE TABLE t1 (a int auto_increment primary key) engine=MyISAM; diff --git a/sql/sql_lex.cc b/sql/sql_lex.cc index d1056285d8d..c0fa16c0811 100644 --- a/sql/sql_lex.cc +++ b/sql/sql_lex.cc @@ -328,7 +328,7 @@ static LEX_STRING get_quoted_token(Lex_input_stream *lip, yyUnget(); // ptr points now after last token char tmp.length= lip->yytoklen=length; tmp.str=(char*) lip->m_thd->alloc(tmp.length+1); - from= lip->tok_start + skip + from= lip->tok_start + skip; to= tmp.str; end= to+length; for ( ; to != end; ) From ae10d3d9e222653c6ad43a5c29d105e1c8a3995c Mon Sep 17 00:00:00 2001 From: "thek@adventure.(none)" <> Date: Mon, 7 May 2007 10:23:10 +0200 Subject: [PATCH 06/29] Bug#26977 exception handlers never hreturn - In some cases, flow control optimization implemented in sp::optimize removes hreturn instructions, causing SQL exception handlers to: * never return * execute wrong logic - This patch overrides default short cut optimization on hreturn instructions to avoid this problem. --- mysql-test/r/sp-code.result | 113 ++++++++++++++++++++++++++++++++++++ mysql-test/t/sp-code.test | 75 ++++++++++++++++++++++++ sql/sp_head.cc | 16 ++++- sql/sp_head.h | 6 ++ 4 files changed, 207 insertions(+), 3 deletions(-) diff --git a/mysql-test/r/sp-code.result b/mysql-test/r/sp-code.result index 9d86a6bc08d..219f3c9b37a 100644 --- a/mysql-test/r/sp-code.result +++ b/mysql-test/r/sp-code.result @@ -620,4 +620,117 @@ SHOW PROCEDURE CODE p1; Pos Instruction 0 stmt 2 "CREATE INDEX idx ON t1 (c1)" DROP PROCEDURE p1; +drop table if exists t1; +drop procedure if exists proc_26977_broken; +drop procedure if exists proc_26977_works; +create table t1(a int unique); +create procedure proc_26977_broken(v int) +begin +declare i int default 5; +declare continue handler for sqlexception +begin +select 'caught something'; +retry: +while i > 0 do +begin +set i = i - 1; +select 'looping', i; +end; +end while retry; +end; +select 'do something'; +insert into t1 values (v); +select 'do something again'; +insert into t1 values (v); +end// +create procedure proc_26977_works(v int) +begin +declare i int default 5; +declare continue handler for sqlexception +begin +select 'caught something'; +retry: +while i > 0 do +begin +set i = i - 1; +select 'looping', i; +end; +end while retry; +select 'optimizer: keep hreturn'; +end; +select 'do something'; +insert into t1 values (v); +select 'do something again'; +insert into t1 values (v); +end// +show procedure code proc_26977_broken; +Pos Instruction +0 set i@1 5 +1 hpush_jump 8 2 CONTINUE +2 stmt 0 "select 'caught something'" +3 jump_if_not 7(7) (i@1 > 0) +4 set i@1 (i@1 - 1) +5 stmt 0 "select 'looping', i" +6 jump 3 +7 hreturn 2 +8 stmt 0 "select 'do something'" +9 stmt 5 "insert into t1 values (v)" +10 stmt 0 "select 'do something again'" +11 stmt 5 "insert into t1 values (v)" +12 hpop 1 +show procedure code proc_26977_works; +Pos Instruction +0 set i@1 5 +1 hpush_jump 9 2 CONTINUE +2 stmt 0 "select 'caught something'" +3 jump_if_not 7(7) (i@1 > 0) +4 set i@1 (i@1 - 1) +5 stmt 0 "select 'looping', i" +6 jump 3 +7 stmt 0 "select 'optimizer: keep hreturn'" +8 hreturn 2 +9 stmt 0 "select 'do something'" +10 stmt 5 "insert into t1 values (v)" +11 stmt 0 "select 'do something again'" +12 stmt 5 "insert into t1 values (v)" +13 hpop 1 +call proc_26977_broken(1); +do something +do something +do something again +do something again +caught something +caught something +looping i +looping 4 +looping i +looping 3 +looping i +looping 2 +looping i +looping 1 +looping i +looping 0 +call proc_26977_works(2); +do something +do something +do something again +do something again +caught something +caught something +looping i +looping 4 +looping i +looping 3 +looping i +looping 2 +looping i +looping 1 +looping i +looping 0 +optimizer: keep hreturn +optimizer: keep hreturn +drop table t1; +drop procedure proc_26977_broken; +drop procedure proc_26977_works; End of 5.0 tests. diff --git a/mysql-test/t/sp-code.test b/mysql-test/t/sp-code.test index 97bc29fcad2..0f249c95172 100644 --- a/mysql-test/t/sp-code.test +++ b/mysql-test/t/sp-code.test @@ -446,4 +446,79 @@ SHOW PROCEDURE CODE p1; DROP PROCEDURE p1; +# +# Bug#26977 exception handlers never hreturn +# +--disable_warnings +drop table if exists t1; +drop procedure if exists proc_26977_broken; +drop procedure if exists proc_26977_works; +--enable_warnings + +create table t1(a int unique); + +delimiter //; + +create procedure proc_26977_broken(v int) +begin + declare i int default 5; + + declare continue handler for sqlexception + begin + select 'caught something'; + retry: + while i > 0 do + begin + set i = i - 1; + select 'looping', i; + end; + end while retry; + end; + + select 'do something'; + insert into t1 values (v); + select 'do something again'; + insert into t1 values (v); +end// + +create procedure proc_26977_works(v int) +begin + declare i int default 5; + + declare continue handler for sqlexception + begin + select 'caught something'; + retry: + while i > 0 do + begin + set i = i - 1; + select 'looping', i; + end; + end while retry; + select 'optimizer: keep hreturn'; + end; + + select 'do something'; + insert into t1 values (v); + select 'do something again'; + insert into t1 values (v); +end// +delimiter ;// + +show procedure code proc_26977_broken; + +show procedure code proc_26977_works; + +## This caust an error because of jump short cut +## optimization. +call proc_26977_broken(1); + +## This works +call proc_26977_works(2); + +drop table t1; +drop procedure proc_26977_broken; +drop procedure proc_26977_works; + + --echo End of 5.0 tests. diff --git a/sql/sp_head.cc b/sql/sp_head.cc index 1ebef4f8bee..5a1faf50296 100644 --- a/sql/sp_head.cc +++ b/sql/sp_head.cc @@ -2982,10 +2982,20 @@ sp_instr_hreturn::print(String *str) uint sp_instr_hreturn::opt_mark(sp_head *sp, List *leads) { - if (m_dest) - return sp_instr_jump::opt_mark(sp, leads); - marked= 1; + + if (m_dest) + { + /* + This is an EXIT handler; next instruction step is in m_dest. + */ + return m_dest; + } + + /* + This is a CONTINUE handler; next instruction step will come from + the handler stack and not from opt_mark. + */ return UINT_MAX; } diff --git a/sql/sp_head.h b/sql/sp_head.h index 4632f6808fd..ed99885ae9a 100644 --- a/sql/sp_head.h +++ b/sql/sp_head.h @@ -973,6 +973,12 @@ public: virtual void print(String *str); + /* This instruction will not be short cut optimized. */ + virtual uint opt_shortcut_jump(sp_head *sp, sp_instr *start) + { + return m_ip; + } + virtual uint opt_mark(sp_head *sp, List *leads); private: From 4ea05eae31f0012741ea3a22de12b5b8c45cf546 Mon Sep 17 00:00:00 2001 From: "thek@adventure.(none)" <> Date: Tue, 8 May 2007 09:09:25 +0200 Subject: [PATCH 07/29] Bug #27792 query cache returns wrong result, with certain system variables - Queries in the query cache are identified by the individual characters in the query statement, the current database and the current environment expressed as a set of system variable flags. - Since the set of environment flags didn't properly describe the current environment unexpected results were returned from the query cache. - Query cache is now cleared when the variable ft_boolean_syntax is updated. - An identification flag for the variable default_week_format is added to the query cache record. Thanks to Martin Friebe who has supplied significant parts of this patch. --- mysql-test/r/query_cache.result | 52 +++++++++++++++++++++++++++++++++ mysql-test/t/query_cache.test | 46 +++++++++++++++++++++++++++++ sql/mysql_priv.h | 1 + sql/set_var.cc | 8 +++-- sql/sql_cache.cc | 2 ++ 5 files changed, 107 insertions(+), 2 deletions(-) diff --git a/mysql-test/r/query_cache.result b/mysql-test/r/query_cache.result index 24363ea27ab..4d0c262898b 100644 --- a/mysql-test/r/query_cache.result +++ b/mysql-test/r/query_cache.result @@ -1008,3 +1008,55 @@ Variable_name Value Qcache_hits 1 drop table t1; set GLOBAL query_cache_size=0; +create table t1 (a int); +insert into t1 values (1),(2),(3); +set GLOBAL query_cache_type=1; +set GLOBAL query_cache_limit=10000; +set GLOBAL query_cache_min_res_unit=0; +set GLOBAL query_cache_size= 100000; +reset query cache; +set LOCAL default_week_format = 0; +select week('2007-01-04'); +week('2007-01-04') +0 +select week('2007-01-04') from t1; +week('2007-01-04') +0 +0 +0 +set LOCAL default_week_format = 2; +select week('2007-01-04'); +week('2007-01-04') +53 +select week('2007-01-04') from t1; +week('2007-01-04') +53 +53 +53 +drop table t1; +CREATE TABLE t1 (a VARCHAR(200), b TEXT, FULLTEXT (a,b)); +INSERT INTO t1 VALUES('MySQL has now support', 'for full-text search'), +('Full-text indexes', 'are called collections'), +('Only MyISAM tables','support collections'), +('Function MATCH ... AGAINST()','is used to do a search'), +('Full-text search in MySQL', 'implements vector space model'); +set GLOBAL ft_boolean_syntax='+ -><()~*:""&|'; +select *, MATCH(a,b) AGAINST("+called +collections" IN BOOLEAN MODE) as x from t1; +a b x +MySQL has now support for full-text search 0 +Full-text indexes are called collections 1 +Only MyISAM tables support collections 0 +Function MATCH ... AGAINST() is used to do a search 0 +Full-text search in MySQL implements vector space model 0 +set GLOBAL ft_boolean_syntax='- +><()~*:""&|'; +select *, MATCH(a,b) AGAINST("+called +collections" IN BOOLEAN MODE) as x from t1; +a b x +MySQL has now support for full-text search 0 +Full-text indexes are called collections 0 +Only MyISAM tables support collections 0 +Function MATCH ... AGAINST() is used to do a search 0 +Full-text search in MySQL implements vector space model 0 +set GLOBAL query_cache_type=default; +set GLOBAL query_cache_limit=default; +set GLOBAL query_cache_min_res_unit=default; +set GLOBAL query_cache_size=default; diff --git a/mysql-test/t/query_cache.test b/mysql-test/t/query_cache.test index 3140739309e..d447df9187b 100644 --- a/mysql-test/t/query_cache.test +++ b/mysql-test/t/query_cache.test @@ -729,4 +729,50 @@ drop table t1; set GLOBAL query_cache_size=0; +# +# Bug #27792 query cache returns wrong result, with certain system variables +# + +create table t1 (a int); +insert into t1 values (1),(2),(3); +set GLOBAL query_cache_type=1; +set GLOBAL query_cache_limit=10000; +set GLOBAL query_cache_min_res_unit=0; +set GLOBAL query_cache_size= 100000; + +# default_week_format +reset query cache; +set LOCAL default_week_format = 0; +select week('2007-01-04'); +select week('2007-01-04') from t1; + +set LOCAL default_week_format = 2; +select week('2007-01-04'); +select week('2007-01-04') from t1; + +drop table t1; + +CREATE TABLE t1 (a VARCHAR(200), b TEXT, FULLTEXT (a,b)); +INSERT INTO t1 VALUES('MySQL has now support', 'for full-text search'), + ('Full-text indexes', 'are called collections'), + ('Only MyISAM tables','support collections'), + ('Function MATCH ... AGAINST()','is used to do a search'), + ('Full-text search in MySQL', 'implements vector space model'); + + +set GLOBAL ft_boolean_syntax='+ -><()~*:""&|'; + +select *, MATCH(a,b) AGAINST("+called +collections" IN BOOLEAN MODE) as x from t1; + +# swap +/- +set GLOBAL ft_boolean_syntax='- +><()~*:""&|'; + +select *, MATCH(a,b) AGAINST("+called +collections" IN BOOLEAN MODE) as x from t1; + +set GLOBAL query_cache_type=default; +set GLOBAL query_cache_limit=default; +set GLOBAL query_cache_min_res_unit=default; +set GLOBAL query_cache_size=default; + + # End of 4.1 tests diff --git a/sql/mysql_priv.h b/sql/mysql_priv.h index e5ac91e1814..6bd60280399 100644 --- a/sql/mysql_priv.h +++ b/sql/mysql_priv.h @@ -426,6 +426,7 @@ struct Query_cache_query_flags ulong sql_mode; ulong max_sort_length; ulong group_concat_max_len; + ulong default_week_format; MY_LOCALE *lc_time_names; }; #define QUERY_CACHE_FLAGS_SIZE sizeof(Query_cache_query_flags) diff --git a/sql/set_var.cc b/sql/set_var.cc index 57bb93ef4b1..520ee5c9f70 100644 --- a/sql/set_var.cc +++ b/sql/set_var.cc @@ -490,7 +490,7 @@ static sys_var_rand_seed1 sys_rand_seed1("rand_seed1"); static sys_var_rand_seed2 sys_rand_seed2("rand_seed2"); static sys_var_thd_ulong sys_default_week_format("default_week_format", - &SV::default_week_format); + &SV::default_week_format); sys_var_thd_ulong sys_group_concat_max_len("group_concat_max_len", &SV::group_concat_max_len); @@ -992,7 +992,6 @@ bool update_sys_var_str(sys_var_str *var_str, rw_lock_t *var_mutex, return 0; } - static bool sys_update_init_connect(THD *thd, set_var *var) { return update_sys_var_str(&sys_init_connect, &LOCK_sys_init_connect, var); @@ -1032,6 +1031,11 @@ static bool sys_update_ftb_syntax(THD *thd, set_var * var) { strmake(ft_boolean_syntax, var->value->str_value.c_ptr(), sizeof(ft_boolean_syntax)-1); + +#ifdef HAVE_QUERY_CACHE + query_cache.flush(); +#endif /* HAVE_QUERY_CACHE */ + return 0; } diff --git a/sql/sql_cache.cc b/sql/sql_cache.cc index fc03e03dee7..ee26bda3307 100644 --- a/sql/sql_cache.cc +++ b/sql/sql_cache.cc @@ -813,6 +813,7 @@ void Query_cache::store_query(THD *thd, TABLE_LIST *tables_used) flags.sql_mode= thd->variables.sql_mode; flags.max_sort_length= thd->variables.max_sort_length; flags.group_concat_max_len= thd->variables.group_concat_max_len; + flags.default_week_format= thd->variables.default_week_format; flags.lc_time_names= thd->variables.lc_time_names; STRUCT_LOCK(&structure_guard_mutex); @@ -1016,6 +1017,7 @@ Query_cache::send_result_to_client(THD *thd, char *sql, uint query_length) flags.sql_mode= thd->variables.sql_mode; flags.max_sort_length= thd->variables.max_sort_length; flags.group_concat_max_len= thd->variables.group_concat_max_len; + flags.default_week_format= thd->variables.default_week_format; flags.lc_time_names= thd->variables.lc_time_names; memcpy((void *)(sql + (tot_length - QUERY_CACHE_FLAGS_SIZE)), &flags, QUERY_CACHE_FLAGS_SIZE); From 5e774747bc6ad3eb9e5e9ce083095029f88296ee Mon Sep 17 00:00:00 2001 From: "thek@adventure.(none)" <> Date: Tue, 8 May 2007 11:24:07 +0200 Subject: [PATCH 08/29] 4.1 -> 5.0 Manual merge for Bug #27792 --- mysql-test/r/query_cache.result | 94 +++++++++++++++++++++++++++++++++ mysql-test/t/query_cache.test | 72 +++++++++++++++++++++++++ sql/mysql_priv.h | 1 + sql/set_var.cc | 5 ++ sql/sql_cache.cc | 19 +++++-- 5 files changed, 186 insertions(+), 5 deletions(-) diff --git a/mysql-test/r/query_cache.result b/mysql-test/r/query_cache.result index d77745176f7..3be2a388860 100644 --- a/mysql-test/r/query_cache.result +++ b/mysql-test/r/query_cache.result @@ -1321,3 +1321,97 @@ insert into t1(c1) select c1 from v1; drop table t1, t2, t3; drop view v1; set global query_cache_size=0; +create table t1 (a int); +insert into t1 values (1),(2),(3); +set GLOBAL query_cache_type=1; +set GLOBAL query_cache_limit=10000; +set GLOBAL query_cache_min_res_unit=0; +set GLOBAL query_cache_size= 100000; +reset query cache; +set LOCAL default_week_format = 0; +select week('2007-01-04'); +week('2007-01-04') +0 +select week('2007-01-04') from t1; +week('2007-01-04') +0 +0 +0 +select extract(WEEK FROM '2007-01-04') from t1; +extract(WEEK FROM '2007-01-04') +0 +0 +0 +set LOCAL default_week_format = 2; +select week('2007-01-04'); +week('2007-01-04') +53 +select week('2007-01-04') from t1; +week('2007-01-04') +53 +53 +53 +select extract(WEEK FROM '2007-01-04') from t1; +extract(WEEK FROM '2007-01-04') +53 +53 +53 +reset query cache; +set LOCAL div_precision_increment=2; +select 1/7; +1/7 +0.14 +select 1/7 from t1; +1/7 +0.14 +0.14 +0.14 +set LOCAL div_precision_increment=4; +select 1/7; +1/7 +0.1429 +select 1/7 from t1; +1/7 +0.1429 +0.1429 +0.1429 +drop table t1; +CREATE TABLE t1 (a VARCHAR(200), b TEXT, FULLTEXT (a,b)); +INSERT INTO t1 VALUES('MySQL has now support', 'for full-text search'), +('Full-text indexes', 'are called collections'), +('Only MyISAM tables','support collections'), +('Function MATCH ... AGAINST()','is used to do a search'), +('Full-text search in MySQL', 'implements vector space model'); +set GLOBAL ft_boolean_syntax='+ -><()~*:""&|'; +select *, MATCH(a,b) AGAINST("+called +collections" IN BOOLEAN MODE) as x from t1; +a b x +MySQL has now support for full-text search 0 +Full-text indexes are called collections 1 +Only MyISAM tables support collections 0 +Function MATCH ... AGAINST() is used to do a search 0 +Full-text search in MySQL implements vector space model 0 +set GLOBAL ft_boolean_syntax='- +><()~*:""&|'; +select *, MATCH(a,b) AGAINST("+called +collections" IN BOOLEAN MODE) as x from t1; +a b x +MySQL has now support for full-text search 0 +Full-text indexes are called collections 0 +Only MyISAM tables support collections 0 +Function MATCH ... AGAINST() is used to do a search 0 +Full-text search in MySQL implements vector space model 0 +create function change_global() returns integer +begin +set global ft_boolean_syntax='+ -><()~*:""&|'; +return 1; +end| +select *, change_global() from t1; +a b change_global() +MySQL has now support for full-text search 1 +Full-text indexes are called collections 1 +Only MyISAM tables support collections 1 +Function MATCH ... AGAINST() is used to do a search 1 +Full-text search in MySQL implements vector space model 1 +drop function change_global; +set GLOBAL query_cache_type=default; +set GLOBAL query_cache_limit=default; +set GLOBAL query_cache_min_res_unit=default; +set GLOBAL query_cache_size= default; diff --git a/mysql-test/t/query_cache.test b/mysql-test/t/query_cache.test index 427334805ce..1ef104f820b 100644 --- a/mysql-test/t/query_cache.test +++ b/mysql-test/t/query_cache.test @@ -899,3 +899,75 @@ insert into t1(c1) select c1 from v1; drop table t1, t2, t3; drop view v1; set global query_cache_size=0; + +# +# Query cache and changes to system variables +# + +create table t1 (a int); +insert into t1 values (1),(2),(3); +set GLOBAL query_cache_type=1; +set GLOBAL query_cache_limit=10000; +set GLOBAL query_cache_min_res_unit=0; +set GLOBAL query_cache_size= 100000; + +# default_week_format +reset query cache; +set LOCAL default_week_format = 0; +select week('2007-01-04'); +select week('2007-01-04') from t1; +select extract(WEEK FROM '2007-01-04') from t1; + +set LOCAL default_week_format = 2; +select week('2007-01-04'); +select week('2007-01-04') from t1; +select extract(WEEK FROM '2007-01-04') from t1; + +# div_precision_increment +reset query cache; +set LOCAL div_precision_increment=2; +select 1/7; +select 1/7 from t1; + +set LOCAL div_precision_increment=4; +select 1/7; +select 1/7 from t1; + +drop table t1; + +CREATE TABLE t1 (a VARCHAR(200), b TEXT, FULLTEXT (a,b)); +INSERT INTO t1 VALUES('MySQL has now support', 'for full-text search'), + ('Full-text indexes', 'are called collections'), + ('Only MyISAM tables','support collections'), + ('Function MATCH ... AGAINST()','is used to do a search'), + ('Full-text search in MySQL', 'implements vector space model'); + + +set GLOBAL ft_boolean_syntax='+ -><()~*:""&|'; + +select *, MATCH(a,b) AGAINST("+called +collections" IN BOOLEAN MODE) as x from t1; + +# swap +/- +set GLOBAL ft_boolean_syntax='- +><()~*:""&|'; + +select *, MATCH(a,b) AGAINST("+called +collections" IN BOOLEAN MODE) as x from t1; + +# If in the future we need to cache queries with functions +# be sure not to cause dead lock if the query cache is flushed +# while inserting a query in the query cache. +delimiter |; +create function change_global() returns integer +begin + set global ft_boolean_syntax='+ -><()~*:""&|'; + return 1; +end| +delimiter ;| +select *, change_global() from t1; +drop function change_global; + +set GLOBAL query_cache_type=default; +set GLOBAL query_cache_limit=default; +set GLOBAL query_cache_min_res_unit=default; +set GLOBAL query_cache_size= default; + +# End of 5.0 tests diff --git a/sql/mysql_priv.h b/sql/mysql_priv.h index f5b22c2d004..f73b0c6cd01 100644 --- a/sql/mysql_priv.h +++ b/sql/mysql_priv.h @@ -643,6 +643,7 @@ struct Query_cache_query_flags ulong max_sort_length; ulong group_concat_max_len; ulong default_week_format; + ulong div_precision_increment; MY_LOCALE *lc_time_names; }; #define QUERY_CACHE_FLAGS_SIZE sizeof(Query_cache_query_flags) diff --git a/sql/set_var.cc b/sql/set_var.cc index c7eddcdd317..1952ae9e7c6 100644 --- a/sql/set_var.cc +++ b/sql/set_var.cc @@ -1200,6 +1200,11 @@ static bool sys_update_ftb_syntax(THD *thd, set_var * var) { strmake(ft_boolean_syntax, var->value->str_value.c_ptr(), sizeof(ft_boolean_syntax)-1); + +#ifdef HAVE_QUERY_CACHE + query_cache.flush(); +#endif /* HAVE_QUERY_CACHE */ + return 0; } diff --git a/sql/sql_cache.cc b/sql/sql_cache.cc index 795711b34d8..96a8dc577df 100644 --- a/sql/sql_cache.cc +++ b/sql/sql_cache.cc @@ -865,10 +865,12 @@ void Query_cache::store_query(THD *thd, TABLE_LIST *tables_used) flags.max_sort_length= thd->variables.max_sort_length; flags.lc_time_names= thd->variables.lc_time_names; flags.group_concat_max_len= thd->variables.group_concat_max_len; + flags.div_precision_increment= thd->variables.div_precincrement; + flags.default_week_format= thd->variables.default_week_format; DBUG_PRINT("qcache", ("long %d, 4.1: %d, more results %d, pkt_nr: %d, \ CS client: %u, CS result: %u, CS conn: %u, limit: %lu, TZ: 0x%lx, \ -sql mode: 0x%lx, sort len: %lu, conncat len: %lu", - (int)flags.client_long_flag, +sql mode: 0x%lx, sort len: %lu, conncat len: %lu, div_precision: %lu, \ +def_week_frmt: %lu", (int)flags.client_protocol_41, (int)flags.more_results_exists, flags.pkt_nr, @@ -879,7 +881,9 @@ sql mode: 0x%lx, sort len: %lu, conncat len: %lu", (ulong) flags.time_zone, flags.sql_mode, flags.max_sort_length, - flags.group_concat_max_len)); + flags.group_concat_max_len, + flags.div_precision_increment, + flags.default_week_format)); /* Make InnoDB to release the adaptive hash index latch before acquiring the query cache mutex. @@ -1107,10 +1111,13 @@ Query_cache::send_result_to_client(THD *thd, char *sql, uint query_length) flags.sql_mode= thd->variables.sql_mode; flags.max_sort_length= thd->variables.max_sort_length; flags.group_concat_max_len= thd->variables.group_concat_max_len; + flags.div_precision_increment= thd->variables.div_precincrement; + flags.default_week_format= thd->variables.default_week_format; flags.lc_time_names= thd->variables.lc_time_names; DBUG_PRINT("qcache", ("long %d, 4.1: %d, more results %d, pkt_nr: %d, \ CS client: %u, CS result: %u, CS conn: %u, limit: %lu, TZ: 0x%lx, \ -sql mode: 0x%lx, sort len: %lu, conncat len: %lu", +sql mode: 0x%lx, sort len: %lu, conncat len: %lu, div_precision: %lu, \ +def_week_frmt: %lu", (int)flags.client_long_flag, (int)flags.client_protocol_41, (int)flags.more_results_exists, @@ -1122,7 +1129,9 @@ sql mode: 0x%lx, sort len: %lu, conncat len: %lu", (ulong) flags.time_zone, flags.sql_mode, flags.max_sort_length, - flags.group_concat_max_len)); + flags.group_concat_max_len, + flags.div_precision_increment, + flags.default_week_format)); memcpy((void *)(sql + (tot_length - QUERY_CACHE_FLAGS_SIZE)), &flags, QUERY_CACHE_FLAGS_SIZE); query_block = (Query_cache_block *) hash_search(&queries, (byte*) sql, From 902b02ec0b6e5859b8257d5e754ac99f23a4d4d4 Mon Sep 17 00:00:00 2001 From: "thek@adventure.(none)" <> Date: Tue, 8 May 2007 11:56:47 +0200 Subject: [PATCH 09/29] Corrected test case for 5.1 requirements --- mysql-test/r/query_cache.result | 2 +- mysql-test/t/query_cache.test | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/mysql-test/r/query_cache.result b/mysql-test/r/query_cache.result index 991270670fe..8fe57a65668 100644 --- a/mysql-test/r/query_cache.result +++ b/mysql-test/r/query_cache.result @@ -1426,7 +1426,7 @@ Full-text indexes are called collections 0 Only MyISAM tables support collections 0 Function MATCH ... AGAINST() is used to do a search 0 Full-text search in MySQL implements vector space model 0 -create function change_global() returns integer +create function change_global() returns integer deterministic begin set global ft_boolean_syntax='+ -><()~*:""&|'; return 1; diff --git a/mysql-test/t/query_cache.test b/mysql-test/t/query_cache.test index e8919ca4ee5..b878c141546 100644 --- a/mysql-test/t/query_cache.test +++ b/mysql-test/t/query_cache.test @@ -985,7 +985,7 @@ select *, MATCH(a,b) AGAINST("+called +collections" IN BOOLEAN MODE) as x from t # be sure not to cause dead lock if the query cache is flushed # while inserting a query in the query cache. delimiter |; -create function change_global() returns integer +create function change_global() returns integer deterministic begin set global ft_boolean_syntax='+ -><()~*:""&|'; return 1; From 41239f1ff04598cebd34511ea2b1e2ac11e988b3 Mon Sep 17 00:00:00 2001 From: "kostja@vajra.(none)" <> Date: Thu, 10 May 2007 16:18:01 +0400 Subject: [PATCH 10/29] No semantical change. Move checks of compatibility of requested lock type and requested table operation from mysql_insert into a separate function. --- sql/sql_insert.cc | 176 +++++++++++++++++++++++++++++++++------------- 1 file changed, 126 insertions(+), 50 deletions(-) diff --git a/sql/sql_insert.cc b/sql/sql_insert.cc index f1d86224adb..1154e98475d 100644 --- a/sql/sql_insert.cc +++ b/sql/sql_insert.cc @@ -401,6 +401,79 @@ void mark_fields_used_by_triggers_for_insert_stmt(THD *thd, TABLE *table, } +/** + Upgrade table-level lock of INSERT statement to TL_WRITE if + a more concurrent lock is infeasible for some reason. This is + necessary for engines without internal locking support (MyISAM). + An engine with internal locking implementation might later + downgrade the lock in handler::store_lock() method. +*/ + +void upgrade_lock_type(THD *thd, thr_lock_type *lock_type, + enum_duplicates duplic, + bool is_multi_insert) +{ + if (duplic == DUP_UPDATE || + duplic == DUP_REPLACE && *lock_type == TL_WRITE_CONCURRENT_INSERT) + { + *lock_type= TL_WRITE; + return; + } + + if (*lock_type == TL_WRITE_DELAYED) + { +#ifdef EMBEDDED_LIBRARY + /* No auxiliary threads in the embedded server. */ + *lock_type= TL_WRITE; + return; +#else + /* + We do not use delayed threads if: + - we're running in the safe mode or skip-new - the feature + is disabled in these modes + - we're running this query in statement level replication, + on a replication slave - because we must ensure serial + execution of queries on the slave + - it is INSERT .. ON DUPLICATE KEY UPDATE - in this case the + insert cannot be concurrent + */ + if (specialflag & (SPECIAL_NO_NEW_FUNC | SPECIAL_SAFE_MODE) || + thd->slave_thread || + thd->variables.max_insert_delayed_threads == 0) + { + *lock_type= TL_WRITE; + return; + } +#endif + bool log_on= (thd->options & OPTION_BIN_LOG || + ! (thd->security_ctx->master_access & SUPER_ACL)); + if (log_on && mysql_bin_log.is_open() && is_multi_insert) + { + /* + Statement-based binary logging does not work in this case, because: + a) two concurrent statements may have their rows intermixed in the + queue, leading to autoincrement replication problems on slave (because + the values generated used for one statement don't depend only on the + value generated for the first row of this statement, so are not + replicable) + b) if first row of the statement has an error the full statement is + not binlogged, while next rows of the statement may be inserted. + c) if first row succeeds, statement is binlogged immediately with a + zero error code (i.e. "no error"), if then second row fails, query + will fail on slave too and slave will stop (wrongly believing that the + master got no error). + So we fall back to non-delayed INSERT. + */ + *lock_type= TL_WRITE; + } + } +} + + +/** + INSERT statement implementation +*/ + bool mysql_insert(THD *thd,TABLE_LIST *table_list, List &fields, List &values_list, @@ -436,58 +509,31 @@ bool mysql_insert(THD *thd,TABLE_LIST *table_list, DBUG_ENTER("mysql_insert"); /* - in safe mode or with skip-new change delayed insert to be regular - if we are told to replace duplicates, the insert cannot be concurrent - delayed insert changed to regular in slave thread - */ -#ifdef EMBEDDED_LIBRARY - if (lock_type == TL_WRITE_DELAYED) - lock_type=TL_WRITE; -#else - if ((lock_type == TL_WRITE_DELAYED && - ((specialflag & (SPECIAL_NO_NEW_FUNC | SPECIAL_SAFE_MODE)) || - thd->slave_thread || !thd->variables.max_insert_delayed_threads)) || - (lock_type == TL_WRITE_CONCURRENT_INSERT && duplic == DUP_REPLACE) || - (duplic == DUP_UPDATE)) - lock_type=TL_WRITE; -#endif - if ((lock_type == TL_WRITE_DELAYED) && - log_on && mysql_bin_log.is_open() && - (values_list.elements > 1)) + Upgrade lock type if the requested lock is incompatible with + the current connection mode or table operation. + */ + upgrade_lock_type(thd, &table_list->lock_type, duplic, + values_list.elements > 1); + lock_type= table_list->lock_type; + + /* + We can't write-delayed into a table locked with LOCK TABLES: + this will lead to a deadlock, since the delayed thread will + never be able to get a lock on the table. QQQ: why not + upgrade the lock here instead? + */ + if (lock_type == TL_WRITE_DELAYED && thd->locked_tables && + find_locked_table(thd, table_list->db, table_list->table_name)) { - /* - Statement-based binary logging does not work in this case, because: - a) two concurrent statements may have their rows intermixed in the - queue, leading to autoincrement replication problems on slave (because - the values generated used for one statement don't depend only on the - value generated for the first row of this statement, so are not - replicable) - b) if first row of the statement has an error the full statement is - not binlogged, while next rows of the statement may be inserted. - c) if first row succeeds, statement is binlogged immediately with a - zero error code (i.e. "no error"), if then second row fails, query - will fail on slave too and slave will stop (wrongly believing that the - master got no error). - So we fallback to non-delayed INSERT. - */ - lock_type= TL_WRITE; + my_error(ER_DELAYED_INSERT_TABLE_LOCKED, MYF(0), + table_list->table_name); + DBUG_RETURN(TRUE); } - table_list->lock_type= lock_type; #ifndef EMBEDDED_LIBRARY if (lock_type == TL_WRITE_DELAYED) { res= 1; - if (thd->locked_tables) - { - DBUG_ASSERT(table_list->db); /* Must be set in the parser */ - if (find_locked_table(thd, table_list->db, table_list->table_name)) - { - my_error(ER_DELAYED_INSERT_TABLE_LOCKED, MYF(0), - table_list->table_name); - DBUG_RETURN(TRUE); - } - } if ((table= delayed_get_table(thd,table_list)) && !thd->is_fatal_error) { /* @@ -1460,6 +1506,12 @@ public: } }; +/** + delayed_insert - context of a thread responsible for delayed insert + into one table. When processing delayed inserts, we create an own + thread for every distinct table. Later on all delayed inserts directed + into that table are handled by a dedicated thread. +*/ class delayed_insert :public ilink { uint locks_in_memory; @@ -1551,6 +1603,12 @@ public: I_List delayed_threads; +/** + Return an instance of delayed insert thread that can handle + inserts into a given table, if it exists. Otherwise return NULL. +*/ + +static delayed_insert *find_handler(THD *thd, TABLE_LIST *table_list) { thd->proc_info="waiting for delay_list"; @@ -1571,6 +1629,18 @@ delayed_insert *find_handler(THD *thd, TABLE_LIST *table_list) } +/** + Attempt to find or create a delayed insert thread to handle inserts + into this table. + + @return Return an instance of the table in the delayed thread + @retval NULL too many delayed threads OR + this thread ran out of resources OR + a newly created delayed insert thread ran out of resources OR + the delayed insert thread failed to open the table. + In the last three cases an error is set in THD. +*/ + static TABLE *delayed_get_table(THD *thd,TABLE_LIST *table_list) { int error; @@ -1679,11 +1749,17 @@ static TABLE *delayed_get_table(THD *thd,TABLE_LIST *table_list) } -/* - As we can't let many threads modify the same TABLE structure, we create - an own structure for each tread. This includes a row buffer to save the - column values and new fields that points to the new row buffer. - The memory is allocated in the client thread and is freed automaticly. +/** + As we can't let many client threads modify the same TABLE + structure of the dedicated delayed insert thread, we create an + own structure for each client thread. This includes a row + buffer to save the column values and new fields that point to + the new row buffer. The memory is allocated in the client + thread and is freed automatically. + + @pre This function is called from the client thread. Delayed + insert thread mutex must be acquired before invoking this + function. */ TABLE *delayed_insert::get_local_table(THD* client_thd) From 563a626f33679a971aa2faa9e257a496fa3b0080 Mon Sep 17 00:00:00 2001 From: "thek@adventure.(none)" <> Date: Thu, 10 May 2007 14:54:55 +0200 Subject: [PATCH 11/29] Corrected merge error; missing line in debug statement. --- sql/sql_cache.cc | 1 + 1 file changed, 1 insertion(+) diff --git a/sql/sql_cache.cc b/sql/sql_cache.cc index 96a8dc577df..33d658ce6a1 100644 --- a/sql/sql_cache.cc +++ b/sql/sql_cache.cc @@ -871,6 +871,7 @@ void Query_cache::store_query(THD *thd, TABLE_LIST *tables_used) CS client: %u, CS result: %u, CS conn: %u, limit: %lu, TZ: 0x%lx, \ sql mode: 0x%lx, sort len: %lu, conncat len: %lu, div_precision: %lu, \ def_week_frmt: %lu", + (int)flags.client_long_flag, (int)flags.client_protocol_41, (int)flags.more_results_exists, flags.pkt_nr, From 518f71bdfa53de49dbc7ba71850cbe6c6e84d399 Mon Sep 17 00:00:00 2001 From: "thek@adventure.(none)" <> Date: Thu, 10 May 2007 15:03:38 +0200 Subject: [PATCH 12/29] Fixed error from merge misstake; missing line in debug statement. --- sql/sql_cache.cc | 1 + 1 file changed, 1 insertion(+) diff --git a/sql/sql_cache.cc b/sql/sql_cache.cc index 867cf2f800e..311bbfafb03 100644 --- a/sql/sql_cache.cc +++ b/sql/sql_cache.cc @@ -874,6 +874,7 @@ long %d, 4.1: %d, bin_proto: %d, more results %d, pkt_nr: %d, \ CS client: %u, CS result: %u, CS conn: %u, limit: %lu, TZ: 0x%lx, \ sql mode: 0x%lx, sort len: %lu, conncat len: %lu, div_precision: %lu, \ def_week_frmt: %lu", + (int)flags.client_long_flag, (int)flags.client_protocol_41, (int)flags.result_in_binary_protocol, (int)flags.more_results_exists, From 0d6e93e0c1c6e8a7827650f00448237a6e7cdfa1 Mon Sep 17 00:00:00 2001 From: "kostja@vajra.(none)" <> Date: Thu, 10 May 2007 18:27:36 +0400 Subject: [PATCH 13/29] Follow the coding style with class names. --- sql/sql_class.h | 4 ++-- sql/sql_insert.cc | 46 +++++++++++++++++++++++----------------------- 2 files changed, 25 insertions(+), 25 deletions(-) diff --git a/sql/sql_class.h b/sql/sql_class.h index 7f195d5a048..ad9315ae5eb 100644 --- a/sql/sql_class.h +++ b/sql/sql_class.h @@ -495,7 +495,7 @@ public: }; -class delayed_insert; +class Delayed_insert; class select_result; #define THD_SENTRY_MAGIC 0xfeedd1ff @@ -1248,7 +1248,7 @@ public: time_t start_time,time_after_lock,user_time; time_t connect_time,thr_create_time; // track down slow pthread_create thr_lock_type update_lock_default; - delayed_insert *di; + Delayed_insert *di; /* <> 0 if we are inside of trigger or stored function. */ uint in_sub_stmt; diff --git a/sql/sql_insert.cc b/sql/sql_insert.cc index 1154e98475d..db4c2f64d6a 100644 --- a/sql/sql_insert.cc +++ b/sql/sql_insert.cc @@ -29,7 +29,7 @@ waited for to open and lock the table. If accessing the thread succeeded, in - delayed_insert::get_local_table() the table of the thread is copied + Delayed_insert::get_local_table() the table of the thread is copied for local use. A copy is required because the normal insert logic works on a target table, but the other threads table object must not be used. The insert logic uses the record buffer to create a record. @@ -1507,13 +1507,13 @@ public: }; /** - delayed_insert - context of a thread responsible for delayed insert + Delayed_insert - context of a thread responsible for delayed insert into one table. When processing delayed inserts, we create an own thread for every distinct table. Later on all delayed inserts directed into that table are handled by a dedicated thread. */ -class delayed_insert :public ilink { +class Delayed_insert :public ilink { uint locks_in_memory; public: THD thd; @@ -1527,7 +1527,7 @@ public: ulong group_count; TABLE_LIST table_list; // Argument - delayed_insert() + Delayed_insert() :locks_in_memory(0), table(0),tables_in_use(0),stacked_inserts(0), status(0), dead(0), group_count(0) @@ -1552,7 +1552,7 @@ public: delayed_insert_threads++; VOID(pthread_mutex_unlock(&LOCK_thread_count)); } - ~delayed_insert() + ~Delayed_insert() { /* The following is not really needed, but just for safety */ delayed_row *row; @@ -1600,7 +1600,7 @@ public: }; -I_List delayed_threads; +I_List delayed_threads; /** @@ -1609,12 +1609,12 @@ I_List delayed_threads; */ static -delayed_insert *find_handler(THD *thd, TABLE_LIST *table_list) +Delayed_insert *find_handler(THD *thd, TABLE_LIST *table_list) { thd->proc_info="waiting for delay_list"; pthread_mutex_lock(&LOCK_delayed_insert); // Protect master list - I_List_iterator it(delayed_threads); - delayed_insert *tmp; + I_List_iterator it(delayed_threads); + Delayed_insert *tmp; while ((tmp=it++)) { if (!strcmp(tmp->thd.db,table_list->db) && @@ -1633,7 +1633,7 @@ delayed_insert *find_handler(THD *thd, TABLE_LIST *table_list) Attempt to find or create a delayed insert thread to handle inserts into this table. - @return Return an instance of the table in the delayed thread + @return Return a local copy of the table in the delayed thread @retval NULL too many delayed threads OR this thread ran out of resources OR a newly created delayed insert thread ran out of resources OR @@ -1644,7 +1644,7 @@ delayed_insert *find_handler(THD *thd, TABLE_LIST *table_list) static TABLE *delayed_get_table(THD *thd,TABLE_LIST *table_list) { int error; - delayed_insert *tmp; + Delayed_insert *tmp; TABLE *table; DBUG_ENTER("delayed_get_table"); @@ -1668,9 +1668,9 @@ static TABLE *delayed_get_table(THD *thd,TABLE_LIST *table_list) */ if (! (tmp= find_handler(thd, table_list))) { - if (!(tmp=new delayed_insert())) + if (!(tmp=new Delayed_insert())) { - my_error(ER_OUTOFMEMORY,MYF(0),sizeof(delayed_insert)); + my_error(ER_OUTOFMEMORY,MYF(0),sizeof(Delayed_insert)); goto err1; } pthread_mutex_lock(&LOCK_thread_count); @@ -1762,12 +1762,12 @@ static TABLE *delayed_get_table(THD *thd,TABLE_LIST *table_list) function. */ -TABLE *delayed_insert::get_local_table(THD* client_thd) +TABLE *Delayed_insert::get_local_table(THD* client_thd) { my_ptrdiff_t adjust_ptrs; Field **field,**org_field, *found_next_number_field; TABLE *copy; - DBUG_ENTER("delayed_insert::get_local_table"); + DBUG_ENTER("Delayed_insert::get_local_table"); /* First request insert thread to get a lock */ status=1; @@ -1875,7 +1875,7 @@ static int write_delayed(THD *thd,TABLE *table,enum_duplicates duplic, bool igno char *query, uint query_length, bool log_on) { delayed_row *row=0; - delayed_insert *di=thd->di; + Delayed_insert *di=thd->di; DBUG_ENTER("write_delayed"); thd->proc_info="waiting for handler insert"; @@ -1943,7 +1943,7 @@ static int write_delayed(THD *thd,TABLE *table,enum_duplicates duplic, bool igno static void end_delayed_insert(THD *thd) { DBUG_ENTER("end_delayed_insert"); - delayed_insert *di=thd->di; + Delayed_insert *di=thd->di; pthread_mutex_lock(&di->mutex); DBUG_PRINT("info",("tables in use: %d",di->tables_in_use)); if (!--di->tables_in_use || di->thd.killed) @@ -1962,8 +1962,8 @@ void kill_delayed_threads(void) { VOID(pthread_mutex_lock(&LOCK_delayed_insert)); // For unlink from list - I_List_iterator it(delayed_threads); - delayed_insert *tmp; + I_List_iterator it(delayed_threads); + Delayed_insert *tmp; while ((tmp=it++)) { tmp->thd.killed= THD::KILL_CONNECTION; @@ -1995,7 +1995,7 @@ void kill_delayed_threads(void) pthread_handler_t handle_delayed_insert(void *arg) { - delayed_insert *di=(delayed_insert*) arg; + Delayed_insert *di=(Delayed_insert*) arg; THD *thd= &di->thd; pthread_detach_this_thread(); @@ -2231,7 +2231,7 @@ static void free_delayed_insert_blobs(register TABLE *table) } -bool delayed_insert::handle_inserts(void) +bool Delayed_insert::handle_inserts(void) { int error; ulong max_rows; @@ -3152,8 +3152,8 @@ void select_create::abort() #ifdef HAVE_EXPLICIT_TEMPLATE_INSTANTIATION template class List_iterator_fast; #ifndef EMBEDDED_LIBRARY -template class I_List; -template class I_List_iterator; +template class I_List; +template class I_List_iterator; template class I_List; #endif /* EMBEDDED_LIBRARY */ #endif /* HAVE_EXPLICIT_TEMPLATE_INSTANTIATION */ From ad609d6e808e1d29d94199269a7997382a3c3955 Mon Sep 17 00:00:00 2001 From: "kostja@vajra.(none)" <> Date: Fri, 11 May 2007 17:26:12 +0400 Subject: [PATCH 14/29] Cleanup: now that we have Lex_input_stream, finish the transition by moving yet another relevant flag to it from struct LEX. --- mysql-test/r/ps.result | 6 +++--- mysql-test/r/ps_1general.result | 6 +++--- mysql-test/t/ps.test | 6 +++--- mysql-test/t/ps_1general.test | 6 +++--- sql/sql_lex.cc | 8 ++++---- sql/sql_lex.h | 10 +++++----- sql/sql_prepare.cc | 5 ++++- sql/sql_yacc.yy | 15 --------------- 8 files changed, 25 insertions(+), 37 deletions(-) diff --git a/mysql-test/r/ps.result b/mysql-test/r/ps.result index 20bff6bda1c..96abff8d7f6 100644 --- a/mysql-test/r/ps.result +++ b/mysql-test/r/ps.result @@ -26,11 +26,11 @@ ERROR HY000: Unknown prepared statement handler (no_such_statement) given to DEA execute stmt1; ERROR HY000: Incorrect arguments to EXECUTE prepare stmt2 from 'prepare nested_stmt from "select 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 '"select 1"' at line 1 +ERROR HY000: This command is not supported in the prepared statement protocol yet prepare stmt2 from 'execute stmt1'; -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 'stmt1' at line 1 +ERROR HY000: This command is not supported in the prepared statement protocol yet prepare stmt2 from 'deallocate prepare z'; -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 'z' at line 1 +ERROR HY000: This command is not supported in the prepared statement protocol yet prepare stmt3 from 'insert into t1 values (?,?)'; set @arg1=5, @arg2='five'; execute stmt3 using @arg1, @arg2; diff --git a/mysql-test/r/ps_1general.result b/mysql-test/r/ps_1general.result index ac8ae6def9f..df4ec793325 100644 --- a/mysql-test/r/ps_1general.result +++ b/mysql-test/r/ps_1general.result @@ -391,11 +391,11 @@ drop table t5 ; deallocate prepare stmt_do ; deallocate prepare stmt_set ; prepare stmt1 from ' prepare stmt2 from '' select 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 '' select 1 '' at line 1 +ERROR HY000: This command is not supported in the prepared statement protocol yet prepare stmt1 from ' execute stmt2 ' ; -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 'stmt2' at line 1 +ERROR HY000: This command is not supported in the prepared statement protocol yet prepare stmt1 from ' deallocate prepare never_prepared ' ; -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 'never_prepared' at line 1 +ERROR HY000: This command is not supported in the prepared statement protocol yet prepare stmt4 from ' use test ' ; ERROR HY000: This command is not supported in the prepared statement protocol yet prepare stmt3 from ' create database mysqltest '; diff --git a/mysql-test/t/ps.test b/mysql-test/t/ps.test index 3fbcf84a1f9..6c93ae25045 100644 --- a/mysql-test/t/ps.test +++ b/mysql-test/t/ps.test @@ -33,13 +33,13 @@ deallocate prepare no_such_statement; execute stmt1; # Nesting ps commands is not allowed: ---error 1064 +--error ER_UNSUPPORTED_PS prepare stmt2 from 'prepare nested_stmt from "select 1"'; ---error 1064 +--error ER_UNSUPPORTED_PS prepare stmt2 from 'execute stmt1'; ---error 1064 +--error ER_UNSUPPORTED_PS prepare stmt2 from 'deallocate prepare z'; # PS insert diff --git a/mysql-test/t/ps_1general.test b/mysql-test/t/ps_1general.test index d4e6a62c09e..2e7fea2ff3d 100644 --- a/mysql-test/t/ps_1general.test +++ b/mysql-test/t/ps_1general.test @@ -416,11 +416,11 @@ deallocate prepare stmt_do ; deallocate prepare stmt_set ; ## nonsense like prepare of prepare,execute or deallocate ---error 1064 +--error ER_UNSUPPORTED_PS prepare stmt1 from ' prepare stmt2 from '' select 1 '' ' ; ---error 1064 +--error ER_UNSUPPORTED_PS prepare stmt1 from ' execute stmt2 ' ; ---error 1064 +--error ER_UNSUPPORTED_PS prepare stmt1 from ' deallocate prepare never_prepared ' ; ## switch the database connection diff --git a/sql/sql_lex.cc b/sql/sql_lex.cc index 6a44a883b51..7bcdc499011 100644 --- a/sql/sql_lex.cc +++ b/sql/sql_lex.cc @@ -123,7 +123,8 @@ Lex_input_stream::Lex_input_stream(THD *thd, buf(buffer), next_state(MY_LEX_START), found_semicolon(NULL), - ignore_space(test(thd->variables.sql_mode & MODE_IGNORE_SPACE)) + ignore_space(test(thd->variables.sql_mode & MODE_IGNORE_SPACE)), + stmt_prepare_mode(FALSE) { } @@ -172,7 +173,6 @@ void lex_start(THD *thd) lex->describe= 0; lex->subqueries= FALSE; lex->view_prepare_mode= FALSE; - lex->stmt_prepare_mode= FALSE; lex->derived_tables= 0; lex->lock_option= TL_READ; lex->safe_to_cache_query= 1; @@ -586,7 +586,7 @@ int MYSQLlex(void *arg, void *yythd) its value in a query for the binlog, the query must stay grammatically correct. */ - else if (c == '?' && lex->stmt_prepare_mode && !ident_map[yyPeek()]) + else if (c == '?' && lip->stmt_prepare_mode && !ident_map[yyPeek()]) return(PARAM_MARKER); return((int) c); @@ -989,7 +989,7 @@ int MYSQLlex(void *arg, void *yythd) if (yyPeek()) { if ((thd->client_capabilities & CLIENT_MULTI_STATEMENTS) && - !lex->stmt_prepare_mode) + !lip->stmt_prepare_mode) { lex->safe_to_cache_query= 0; lip->found_semicolon= lip->ptr; diff --git a/sql/sql_lex.h b/sql/sql_lex.h index d07de73d5b9..d34124095d3 100644 --- a/sql/sql_lex.h +++ b/sql/sql_lex.h @@ -954,6 +954,11 @@ public: /** SQL_MODE = IGNORE_SPACE. */ bool ignore_space; + /* + TRUE if we're parsing a prepared statement: in this mode + we should allow placeholders and disallow multi-statements. + */ + bool stmt_prepare_mode; }; @@ -1082,11 +1087,6 @@ typedef struct st_lex : public Query_tables_list to an .frm file. We need this definition to stay untouched. */ bool view_prepare_mode; - /* - TRUE if we're parsing a prepared statement: in this mode - we should allow placeholders and disallow multistatements. - */ - bool stmt_prepare_mode; bool safe_to_cache_query; bool subqueries, ignore; st_parsing_options parsing_options; diff --git a/sql/sql_prepare.cc b/sql/sql_prepare.cc index d3cc3613b4a..9c41ee57a21 100644 --- a/sql/sql_prepare.cc +++ b/sql/sql_prepare.cc @@ -1762,6 +1762,9 @@ static bool check_prepared_statement(Prepared_statement *stmt, case SQLCOM_OPTIMIZE: break; + case SQLCOM_PREPARE: + case SQLCOM_EXECUTE: + case SQLCOM_DEALLOCATE_PREPARE: default: /* All other statements are not supported yet. */ my_message(ER_UNSUPPORTED_PS, ER(ER_UNSUPPORTED_PS), MYF(0)); @@ -2801,10 +2804,10 @@ bool Prepared_statement::prepare(const char *packet, uint packet_len) thd->stmt_arena= this; Lex_input_stream lip(thd, thd->query, thd->query_length); + lip.stmt_prepare_mode= TRUE; thd->m_lip= &lip; lex_start(thd); lex->safe_to_cache_query= FALSE; - lex->stmt_prepare_mode= TRUE; int err= MYSQLparse((void *)thd); error= err || thd->is_fatal_error || diff --git a/sql/sql_yacc.yy b/sql/sql_yacc.yy index 390a991c7b6..847d4df6935 100644 --- a/sql/sql_yacc.yy +++ b/sql/sql_yacc.yy @@ -1276,11 +1276,6 @@ deallocate: { THD *thd=YYTHD; LEX *lex= thd->lex; - if (lex->stmt_prepare_mode) - { - my_parse_error(ER(ER_SYNTAX_ERROR)); - MYSQL_YYABORT; - } lex->sql_command= SQLCOM_DEALLOCATE_PREPARE; lex->prepared_stmt_name= $3; }; @@ -1296,11 +1291,6 @@ prepare: { THD *thd=YYTHD; LEX *lex= thd->lex; - if (lex->stmt_prepare_mode) - { - my_parse_error(ER(ER_SYNTAX_ERROR)); - MYSQL_YYABORT; - } lex->sql_command= SQLCOM_PREPARE; lex->prepared_stmt_name= $2; }; @@ -1326,11 +1316,6 @@ execute: { THD *thd=YYTHD; LEX *lex= thd->lex; - if (lex->stmt_prepare_mode) - { - my_parse_error(ER(ER_SYNTAX_ERROR)); - MYSQL_YYABORT; - } lex->sql_command= SQLCOM_EXECUTE; lex->prepared_stmt_name= $2; } From 8b93e52e92391586edf99f26ba558ab7e6bf9bc9 Mon Sep 17 00:00:00 2001 From: "dlenev@mockturtle.local" <> Date: Fri, 11 May 2007 20:33:13 +0400 Subject: [PATCH 15/29] Fix for: Bug #20662 "Infinite loop in CREATE TABLE IF NOT EXISTS ... SELECT with locked tables" Bug #20903 "Crash when using CREATE TABLE .. SELECT and triggers" Bug #24738 "CREATE TABLE ... SELECT is not isolated properly" Bug #24508 "Inconsistent results of CREATE TABLE ... SELECT when temporary table exists" Deadlock occured when one tried to execute CREATE TABLE IF NOT EXISTS ... SELECT statement under LOCK TABLES which held read lock on target table. Attempt to execute the same statement for already existing target table with triggers caused server crashes. Also concurrent execution of CREATE TABLE ... SELECT statement and other statements involving target table suffered from various races (some of which might've led to deadlocks). Finally, attempt to execute CREATE TABLE ... SELECT in case when a temporary table with same name was already present led to the insertion of data into this temporary table and creation of empty non-temporary table. All above problems stemmed from the old implementation of CREATE TABLE ... SELECT in which we created, opened and locked target table without any special protection in a separate step and not with the rest of tables used by this statement. This underminded deadlock-avoidance approach used in server and created window for races. It also excluded target table from prelocking causing problems with trigger execution. The patch solves these problems by implementing new approach to handling of CREATE TABLE ... SELECT for base tables. We try to open and lock table to be created at the same time as the rest of tables used by this statement. If such table does not exist at this moment we create and place in the table cache special placeholder for it which prevents its creation or any other usage by other threads. We still use old approach for creation of temporary tables. Also note that we decided to postpone introduction of some tests for concurrent behaviour of CREATE TABLE ... SELECT till 5.1. The main reason for this is absence in 5.0 ability to set @@debug variable at runtime, which can be circumvented only by using several test files with individual .opt files. Since the latter is likely to slowdown test-suite unnecessary we chose not to push this tests into 5.0, but run them manually for this version and later push their optimized version into 5.1 --- mysql-test/r/create.result | 94 ++++++++ mysql-test/r/trigger.result | 35 +++ mysql-test/t/create.test | 111 ++++++++++ mysql-test/t/trigger.test | 26 +++ sql/lock.cc | 31 +-- sql/mysql_priv.h | 16 +- sql/sql_base.cc | 420 +++++++++++++++++++++++++++++++++--- sql/sql_handler.cc | 25 ++- sql/sql_insert.cc | 182 +++++++++------- sql/sql_parse.cc | 12 +- sql/sql_prepare.cc | 15 +- sql/sql_table.cc | 88 ++++++-- sql/sql_trigger.cc | 2 +- sql/sql_yacc.yy | 4 +- sql/table.h | 40 +++- 15 files changed, 918 insertions(+), 183 deletions(-) diff --git a/mysql-test/r/create.result b/mysql-test/r/create.result index afa005e74c0..e1262c7d2c2 100644 --- a/mysql-test/r/create.result +++ b/mysql-test/r/create.result @@ -769,6 +769,100 @@ t1 CREATE TABLE `t1` ( `i` int(11) default NULL ) ENGINE=MyISAM DEFAULT CHARSET=latin1 MAX_ROWS=4294967295 drop table t1; +create table t1 select * from t2; +ERROR 42S02: Table 'test.t2' doesn't exist +create table t1 select * from t1; +ERROR HY000: You can't specify target table 't1' for update in FROM clause +create table t1 select coalesce('a' collate latin1_swedish_ci,'b' collate latin1_bin); +ERROR HY000: Illegal mix of collations (latin1_swedish_ci,EXPLICIT) and (latin1_bin,EXPLICIT) for operation 'coalesce' +create table t1 (primary key(a)) select "b" as b; +ERROR 42000: Key column 'a' doesn't exist in table +create table t1 (a int); +create table if not exists t1 select 1 as a, 2 as b; +ERROR 21S01: Column count doesn't match value count at row 1 +drop table t1; +create table t1 (primary key (a)) (select 1 as a) union all (select 1 as a); +ERROR 23000: Duplicate entry '1' for key 1 +create table t1 (i int); +create table t1 select 1 as i; +ERROR 42S01: Table 't1' already exists +create table if not exists t1 select 1 as i; +Warnings: +Note 1050 Table 't1' already exists +select * from t1; +i +1 +create table t1 select coalesce('a' collate latin1_swedish_ci,'b' collate latin1_bin); +ERROR HY000: Illegal mix of collations (latin1_swedish_ci,EXPLICIT) and (latin1_bin,EXPLICIT) for operation 'coalesce' +select * from t1; +i +1 +alter table t1 add primary key (i); +create table if not exists t1 (select 2 as i) union all (select 2 as i); +ERROR 23000: Duplicate entry '2' for key 1 +select * from t1; +i +1 +2 +drop table t1; +create temporary table t1 (j int); +create table if not exists t1 select 1; +Warnings: +Note 1050 Table 't1' already exists +select * from t1; +j +1 +drop temporary table t1; +select * from t1; +ERROR 42S02: Table 'test.t1' doesn't exist +drop table t1; +ERROR 42S02: Unknown table 't1' +create table t1 (i int); +insert into t1 values (1), (2); +lock tables t1 read; +create table t2 select * from t1; +ERROR HY000: Table 't2' was not locked with LOCK TABLES +create table if not exists t2 select * from t1; +ERROR HY000: Table 't2' was not locked with LOCK TABLES +unlock tables; +create table t2 (j int); +lock tables t1 read; +create table t2 select * from t1; +ERROR HY000: Table 't2' was not locked with LOCK TABLES +create table if not exists t2 select * from t1; +ERROR HY000: Table 't2' was not locked with LOCK TABLES +unlock tables; +lock table t1 read, t2 read; +create table t2 select * from t1; +ERROR HY000: Table 't2' was locked with a READ lock and can't be updated +create table if not exists t2 select * from t1; +ERROR HY000: Table 't2' was locked with a READ lock and can't be updated +unlock tables; +lock table t1 read, t2 write; +create table t2 select * from t1; +ERROR 42S01: Table 't2' already exists +create table if not exists t2 select * from t1; +Warnings: +Note 1050 Table 't2' already exists +select * from t1; +i +1 +2 +unlock tables; +drop table t2; +lock tables t1 read; +create temporary table t2 select * from t1; +create temporary table if not exists t2 select * from t1; +Warnings: +Note 1050 Table 't2' already exists +select * from t2; +i +1 +2 +1 +2 +unlock tables; +drop table t1, t2; create table t1 (upgrade int); drop table t1; End of 5.0 tests diff --git a/mysql-test/r/trigger.result b/mysql-test/r/trigger.result index 0a0be41927a..3e6a901dc00 100644 --- a/mysql-test/r/trigger.result +++ b/mysql-test/r/trigger.result @@ -1414,4 +1414,39 @@ id val DROP TRIGGER trg27006_a_insert; DROP TRIGGER trg27006_a_update; drop table t1,t2; +drop table if exists t1, t2, t3; +create table t1 (i int); +create trigger t1_bi before insert on t1 for each row set new.i = 7; +create trigger t1_ai after insert on t1 for each row set @a := 7; +create table t2 (j int); +insert into t2 values (1), (2); +set @a:=""; +create table if not exists t1 select * from t2; +Warnings: +Note 1050 Table 't1' already exists +select * from t1; +i +7 +7 +select @a; +@a +7 +drop trigger t1_bi; +drop trigger t1_ai; +create table t3 (isave int); +create trigger t1_bi before insert on t1 for each row insert into t3 values (new.i); +create table if not exists t1 select * from t2; +Warnings: +Note 1050 Table 't1' already exists +select * from t1; +i +7 +7 +1 +2 +select * from t3; +isave +1 +2 +drop table t1, t2, t3; End of 5.0 tests diff --git a/mysql-test/t/create.test b/mysql-test/t/create.test index ba5f56e12b1..35198c793b8 100644 --- a/mysql-test/t/create.test +++ b/mysql-test/t/create.test @@ -669,6 +669,117 @@ alter table t1 max_rows=100000000000; show create table t1; drop table t1; + +# +# Tests for errors happening at various stages of CREATE TABLES ... SELECT +# +# (Also checks that it behaves atomically in the sense that in case +# of error it is automatically dropped if it has not existed before.) +# +# Error during open_and_lock_tables() of tables +--error ER_NO_SUCH_TABLE +create table t1 select * from t2; +# Rather special error which also caught during open tables pahse +--error ER_UPDATE_TABLE_USED +create table t1 select * from t1; +# Error which happens before select_create::prepare() +--error ER_CANT_AGGREGATE_2COLLATIONS +create table t1 select coalesce('a' collate latin1_swedish_ci,'b' collate latin1_bin); +# Error during table creation +--error ER_KEY_COLUMN_DOES_NOT_EXITS +create table t1 (primary key(a)) select "b" as b; +# Error in select_create::prepare() which is not related to table creation +create table t1 (a int); +--error ER_WRONG_VALUE_COUNT_ON_ROW +create table if not exists t1 select 1 as a, 2 as b; +drop table t1; +# Finally error which happens during insert +--error ER_DUP_ENTRY +create table t1 (primary key (a)) (select 1 as a) union all (select 1 as a); +# What happens if table already exists ? +create table t1 (i int); +--error ER_TABLE_EXISTS_ERROR +create table t1 select 1 as i; +create table if not exists t1 select 1 as i; +select * from t1; +# Error before select_create::prepare() +--error ER_CANT_AGGREGATE_2COLLATIONS +create table t1 select coalesce('a' collate latin1_swedish_ci,'b' collate latin1_bin); +select * from t1; +# Error which happens during insertion of rows +alter table t1 add primary key (i); +--error ER_DUP_ENTRY +create table if not exists t1 (select 2 as i) union all (select 2 as i); +select * from t1; +drop table t1; + + +# Base vs temporary tables dillema (a.k.a. bug#24508 "Inconsistent +# results of CREATE TABLE ... SELECT when temporary table exists"). +# In this situation we either have to create non-temporary table and +# insert data in it or insert data in temporary table without creation +# of permanent table. Since currently temporary tables always shadow +# permanent tables we adopt second approach. +create temporary table t1 (j int); +create table if not exists t1 select 1; +select * from t1; +drop temporary table t1; +--error ER_NO_SUCH_TABLE +select * from t1; +--error ER_BAD_TABLE_ERROR +drop table t1; + + +# +# CREATE TABLE ... SELECT and LOCK TABLES +# +# There is little sense in using CREATE TABLE ... SELECT under +# LOCK TABLES as it mostly does not work. At least we check that +# the server doesn't crash, hang and produces sensible errors. +# Includes test for bug #20662 "Infinite loop in CREATE TABLE +# IF NOT EXISTS ... SELECT with locked tables". +create table t1 (i int); +insert into t1 values (1), (2); +lock tables t1 read; +--error ER_TABLE_NOT_LOCKED +create table t2 select * from t1; +--error ER_TABLE_NOT_LOCKED +create table if not exists t2 select * from t1; +unlock tables; +create table t2 (j int); +lock tables t1 read; +--error ER_TABLE_NOT_LOCKED +create table t2 select * from t1; +# This should not be ever allowed as it will undermine +# lock-all-at-once approach +--error ER_TABLE_NOT_LOCKED +create table if not exists t2 select * from t1; +unlock tables; +lock table t1 read, t2 read; +--error ER_TABLE_NOT_LOCKED_FOR_WRITE +create table t2 select * from t1; +--error ER_TABLE_NOT_LOCKED_FOR_WRITE +create table if not exists t2 select * from t1; +unlock tables; +lock table t1 read, t2 write; +--error ER_TABLE_EXISTS_ERROR +create table t2 select * from t1; +# This is the only case which really works. +create table if not exists t2 select * from t1; +select * from t1; +unlock tables; +drop table t2; + +# OTOH CREATE TEMPORARY TABLE ... SELECT should work +# well under LOCK TABLES. +lock tables t1 read; +create temporary table t2 select * from t1; +create temporary table if not exists t2 select * from t1; +select * from t2; +unlock tables; +drop table t1, t2; + + # # Bug#21772: can not name a column 'upgrade' when create a table # diff --git a/mysql-test/t/trigger.test b/mysql-test/t/trigger.test index a01efba11db..82de4dac111 100644 --- a/mysql-test/t/trigger.test +++ b/mysql-test/t/trigger.test @@ -1737,4 +1737,30 @@ DROP TRIGGER trg27006_a_insert; DROP TRIGGER trg27006_a_update; drop table t1,t2; +# +# Bug #20903 "Crash when using CREATE TABLE .. SELECT and triggers" +# + +--disable_warnings +drop table if exists t1, t2, t3; +--enable_warnings +create table t1 (i int); +create trigger t1_bi before insert on t1 for each row set new.i = 7; +create trigger t1_ai after insert on t1 for each row set @a := 7; +create table t2 (j int); +insert into t2 values (1), (2); +set @a:=""; +create table if not exists t1 select * from t2; +select * from t1; +select @a; +# Let us check that trigger that involves table also works ok. +drop trigger t1_bi; +drop trigger t1_ai; +create table t3 (isave int); +create trigger t1_bi before insert on t1 for each row insert into t3 values (new.i); +create table if not exists t1 select * from t2; +select * from t1; +select * from t3; +drop table t1, t2, t3; + --echo End of 5.0 tests diff --git a/sql/lock.cc b/sql/lock.cc index 233d12d9cc4..9298b33b4d2 100644 --- a/sql/lock.cc +++ b/sql/lock.cc @@ -853,7 +853,6 @@ int lock_table_name(THD *thd, TABLE_LIST *table_list) TABLE *table; char key[MAX_DBKEY_LENGTH]; char *db= table_list->db; - int table_in_key_offset; uint key_length; HASH_SEARCH_STATE state; DBUG_ENTER("lock_table_name"); @@ -861,10 +860,8 @@ int lock_table_name(THD *thd, TABLE_LIST *table_list) safe_mutex_assert_owner(&LOCK_open); - table_in_key_offset= strmov(key, db) - key + 1; - key_length= (uint)(strmov(key + table_in_key_offset, table_list->table_name) - - key) + 1; - + key_length= (uint)(strmov(strmov(key, db) + 1, table_list->table_name) - + key) + 1; /* Only insert the table if we haven't insert it already */ for (table=(TABLE*) hash_first(&open_cache, (byte*)key, key_length, &state); @@ -873,29 +870,11 @@ int lock_table_name(THD *thd, TABLE_LIST *table_list) if (table->in_use == thd) DBUG_RETURN(0); - /* - Create a table entry with the right key and with an old refresh version - Note that we must use my_malloc() here as this is freed by the table - cache - */ - if (!(table= (TABLE*) my_malloc(sizeof(*table)+key_length, - MYF(MY_WME | MY_ZEROFILL)))) + if (!(table= table_cache_insert_placeholder(thd, key, key_length))) DBUG_RETURN(-1); - table->s= &table->share_not_to_be_used; - memcpy((table->s->table_cache_key= (char*) (table+1)), key, key_length); - table->s->db= table->s->table_cache_key; - table->s->table_name= table->s->table_cache_key + table_in_key_offset; - table->s->key_length=key_length; - table->in_use=thd; - table->locked_by_name=1; - table_list->table=table; - if (my_hash_insert(&open_cache, (byte*) table)) - { - my_free((gptr) table,MYF(0)); - DBUG_RETURN(-1); - } - + table_list->table= table; + /* Return 1 if table is in use */ DBUG_RETURN(test(remove_table_from_cache(thd, db, table_list->table_name, RTFC_NO_FLAG))); diff --git a/sql/mysql_priv.h b/sql/mysql_priv.h index 868f3584448..37a5fc31f1d 100644 --- a/sql/mysql_priv.h +++ b/sql/mysql_priv.h @@ -674,6 +674,8 @@ struct Query_cache_query_flags #define query_cache_invalidate_by_MyISAM_filename_ref NULL #endif /*HAVE_QUERY_CACHE*/ +uint build_table_path(char *buff, size_t bufflen, const char *db, + const char *table, const char *ext); bool mysql_create_db(THD *thd, char *db, HA_CREATE_INFO *create, bool silent); bool mysql_alter_db(THD *thd, const char *db, HA_CREATE_INFO *create); bool mysql_rm_db(THD *thd,char *db,bool if_exists, bool silent); @@ -856,12 +858,14 @@ bool mysql_create_or_drop_trigger(THD *thd, TABLE_LIST *tables, bool create); TABLE *open_ltable(THD *thd, TABLE_LIST *table_list, thr_lock_type update); TABLE *open_table(THD *thd, TABLE_LIST *table_list, MEM_ROOT* mem, bool *refresh, uint flags); -bool reopen_name_locked_table(THD* thd, TABLE_LIST* table); +bool reopen_name_locked_table(THD* thd, TABLE_LIST* table_list, bool link_in); +TABLE *table_cache_insert_placeholder(THD *thd, const char *key, + uint key_length); +bool table_cache_has_open_placeholder(THD *thd, const char *db, + const char *table_name); TABLE *find_locked_table(THD *thd, const char *db,const char *table_name); bool reopen_table(TABLE *table,bool locked); bool reopen_tables(THD *thd,bool get_locks,bool in_refresh); -void close_old_data_files(THD *thd, TABLE *table, bool abort_locks, - bool send_refresh); bool close_data_tables(THD *thd,const char *db, const char *table_name); bool wait_for_tables(THD *thd); bool table_is_used(TABLE *table, bool wait_for_name_lock); @@ -1017,6 +1021,8 @@ 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 drop_open_table(THD *thd, TABLE *table, const char *db_name, + const char *table_name); void update_non_unique_table_error(TABLE_LIST *update, const char *operation, TABLE_LIST *duplicate); @@ -1269,7 +1275,7 @@ extern double log_01[32]; extern ulonglong log_10_int[20]; extern ulonglong keybuff_size; extern ulonglong thd_startup_options; -extern ulong refresh_version,flush_version, thread_id; +extern ulong flush_version, thread_id; extern ulong binlog_cache_use, binlog_cache_disk_use; extern ulong aborted_threads,aborted_connects; extern ulong delayed_insert_timeout; @@ -1443,7 +1449,7 @@ MYSQL_LOCK *mysql_lock_tables(THD *thd, TABLE **table, uint count, #define MYSQL_LOCK_IGNORE_GLOBAL_READ_LOCK 0x0001 #define MYSQL_LOCK_IGNORE_FLUSH 0x0002 #define MYSQL_LOCK_NOTIFY_IF_NEED_REOPEN 0x0004 -#define MYSQL_OPEN_IGNORE_LOCKED_TABLES 0x0008 +#define MYSQL_OPEN_TEMPORARY_ONLY 0x0008 void mysql_unlock_tables(THD *thd, MYSQL_LOCK *sql_lock); void mysql_unlock_read_tables(THD *thd, MYSQL_LOCK *sql_lock); diff --git a/sql/sql_base.cc b/sql/sql_base.cc index 6e6611d54d2..db4491a19ae 100644 --- a/sql/sql_base.cc +++ b/sql/sql_base.cc @@ -94,6 +94,8 @@ static bool open_new_frm(THD *thd, const char *path, const char *alias, uint db_stat, uint prgflag, uint ha_open_flags, TABLE *outparam, TABLE_LIST *table_desc, MEM_ROOT *mem_root); +static void close_old_data_files(THD *thd, TABLE *table, bool morph_locks, + bool send_refresh); extern "C" byte *table_cache_key(const byte *record,uint *length, my_bool not_used __attribute__((unused))) @@ -374,7 +376,21 @@ bool close_cached_tables(THD *thd, bool if_wait_for_refresh, for (uint idx=0 ; idx < open_cache.records ; idx++) { TABLE *table=(TABLE*) hash_element(&open_cache,idx); - if ((table->s->version) < refresh_version && table->db_stat) + /* + Note that we wait here only for tables which are actually open, and + not for placeholders with TABLE::open_placeholder set. Waiting for + latter will cause deadlock in the following scenario, for example: + + conn1: lock table t1 write; + conn2: lock table t2 write; + conn1: flush tables; + conn2: flush tables; + + It also does not make sense to wait for those of placeholders that + are employed by CREATE TABLE as in this case table simply does not + exist yet. + */ + if (table->needs_reopen_or_name_lock() && table->db_stat) { found=1; DBUG_PRINT("signal", ("Waiting for COND_refresh")); @@ -616,10 +632,10 @@ bool close_thread_table(THD *thd, TABLE **table_ptr) TABLE *table= *table_ptr; DBUG_ENTER("close_thread_table"); DBUG_ASSERT(table->key_read == 0); - DBUG_ASSERT(table->file->inited == handler::NONE); + DBUG_ASSERT(!table->file || table->file->inited == handler::NONE); *table_ptr=table->next; - if (table->s->version != refresh_version || + if (table->needs_reopen_or_name_lock() || thd->version != refresh_version || !table->db_stat) { VOID(hash_delete(&open_cache,(byte*) table)); @@ -627,6 +643,12 @@ bool close_thread_table(THD *thd, TABLE **table_ptr) } else { + /* + Open placeholders have TABLE::db_stat set to 0, so they should be + handled by the first alternative. + */ + DBUG_ASSERT(!table->open_placeholder); + if (table->s->flush_version != flush_version) { table->s->flush_version= flush_version; @@ -1114,6 +1136,43 @@ TABLE *unlink_open_table(THD *thd, TABLE *list, TABLE *find) } +/** + @brief Auxiliary routine which closes and drops open table. + + @param thd Thread handle + @param table TABLE object for table to be dropped + @param db_name Name of database for this table + @param table_name Name of this table + + @note This routine assumes that table to be closed is open only + by calling thread so we needn't wait until other threads + will close the table. Also unless called under implicit or + explicit LOCK TABLES mode it assumes that table to be + dropped is already unlocked. In the former case it will + also remove lock on the table. But one should not rely on + this behaviour as it may change in future. +*/ + +void drop_open_table(THD *thd, TABLE *table, const char *db_name, + const char *table_name) +{ + if (table->s->tmp_table) + close_temporary_table(thd, db_name, table_name); + else + { + enum db_type table_type= table->s->db_type; + VOID(pthread_mutex_lock(&LOCK_open)); + /* + unlink_open_table() also tells threads waiting for refresh or close + that something has happened. + */ + thd->open_tables= unlink_open_table(thd, thd->open_tables, table); + quick_rm_table(table_type, db_name, table_name); + VOID(pthread_mutex_unlock(&LOCK_open)); + } +} + + /* When we call the following function we must have a lock on LOCK_open ; This lock will be unlocked on return. @@ -1152,6 +1211,11 @@ void wait_for_refresh(THD *thd) table_list TABLE_LIST object for table to be open, TABLE_LIST::table member should point to TABLE object which was used for name-locking. + link_in TRUE - if TABLE object for table to be opened should be + linked into THD::open_tables list. + FALSE - placeholder used for name-locking is already in + this list so we only need to preserve TABLE::next + pointer. NOTE This function assumes that its caller already acquired LOCK_open mutex. @@ -1161,7 +1225,7 @@ void wait_for_refresh(THD *thd) TRUE - Error */ -bool reopen_name_locked_table(THD* thd, TABLE_LIST* table_list) +bool reopen_name_locked_table(THD* thd, TABLE_LIST* table_list, bool link_in) { TABLE *table= table_list->table; TABLE_SHARE *share; @@ -1199,12 +1263,33 @@ bool reopen_name_locked_table(THD* thd, TABLE_LIST* table_list) share= table->s; share->db= share->table_cache_key; share->key_length=key_length; + /* + We want to prevent other connections from opening this table until end + of statement as it is likely that modifications of table's metadata are + not yet finished (for example CREATE TRIGGER have to change .TRG file, + or we might want to drop table if CREATE TABLE ... SELECT fails). + This also allows us to assume that no other connection will sneak in + before we will get table-level lock on this table. + */ share->version=0; share->flush_version=0; table->in_use = thd; check_unused(); - table->next = thd->open_tables; - thd->open_tables = table; + + if (link_in) + { + table->next= thd->open_tables; + thd->open_tables= table; + } + else + { + /* + TABLE object should be already in THD::open_tables list so we just + need to set TABLE::next correctly. + */ + table->next= orig_table.next; + } + table->tablenr=thd->current_tablenr++; table->used_fields=0; table->const_table=0; @@ -1216,6 +1301,167 @@ bool reopen_name_locked_table(THD* thd, TABLE_LIST* table_list) } +/** + @brief Create and insert into table cache placeholder for table + which will prevent its opening (or creation) (a.k.a lock + table name). + + @param thd Thread context + @param key Table cache key for name to be locked + @param key_length Table cache key length + + @return Pointer to TABLE object used for name locking or 0 in + case of failure. +*/ + +TABLE *table_cache_insert_placeholder(THD *thd, const char *key, + uint key_length) +{ + TABLE *table; + char *key_buff; + DBUG_ENTER("table_cache_insert_placeholder"); + + safe_mutex_assert_owner(&LOCK_open); + + /* + Create a table entry with the right key and with an old refresh version + Note that we must use my_multi_malloc() here as this is freed by the + table cache + */ + if (!my_multi_malloc(MYF(MY_WME | MY_ZEROFILL), + &table, sizeof(*table), + &key_buff, key_length, + NULL)) + DBUG_RETURN(NULL); + + table->s= &table->share_not_to_be_used; + memcpy(key_buff, key, key_length); + table->s->table_cache_key= key_buff; + table->s->db= table->s->table_cache_key; + table->s->table_name= table->s->table_cache_key + strlen(table->s->db) + 1; + table->s->key_length= key_length; + table->in_use= thd; + table->locked_by_name= 1; + + if (my_hash_insert(&open_cache, (byte*)table)) + { + my_free((gptr) table, MYF(0)); + DBUG_RETURN(NULL); + } + + DBUG_RETURN(table); +} + + +/** + @brief Check if table cache contains an open placeholder for the + table and if this placeholder was created by another thread. + + @param thd Thread context + @param db Name of database for table in question + @param table_name Table name + + @note The presence of open placeholder indicates that either some + other thread is trying to create table in question and obtained + an exclusive name-lock on it or that this table already exists + and is being flushed at the moment. + + @note One should acquire LOCK_open mutex before calling this function. + + @note This function is a hack which was introduced in 5.0 only to + minimize code changes. It doesn't present in 5.1. + + @retval TRUE Table cache contains open placeholder for the table + which was created by some other thread. + @retval FALSE Otherwise. +*/ + +bool table_cache_has_open_placeholder(THD *thd, const char *db, + const char *table_name) +{ + char key[MAX_DBKEY_LENGTH]; + uint key_length; + HASH_SEARCH_STATE state; + TABLE *search; + DBUG_ENTER("table_cache_has_open_placeholder"); + + safe_mutex_assert_owner(&LOCK_open); + + key_length=(uint) (strmov(strmov(key,db)+1,table_name)-key)+1; + for (search= (TABLE*) hash_first(&open_cache, (byte*) key, key_length, + &state); + search ; + search= (TABLE*) hash_next(&open_cache, (byte*) key, key_length, + &state)) + { + if (search->in_use == thd) + continue; + if (search->open_placeholder) + DBUG_RETURN(1); + } + DBUG_RETURN(0); +} + + +/** + @brief Check that table exists on disk or in some storage engine. + + @param thd Thread context + @param table Table list element + @param exists[out] Out parameter which is set to TRUE if table + exists and to FALSE otherwise. + + @note This function assumes that caller owns LOCK_open mutex. + It also assumes that the fact that there are no name-locks + on the table was checked beforehand. + + @note If there is no .FRM file for the table but it exists in one + of engines (e.g. it was created on another node of NDB cluster) + this function will fetch and create proper .FRM file for it. + + @retval TRUE Some error occured + @retval FALSE No error. 'exists' out parameter set accordingly. +*/ + +bool check_if_table_exists(THD *thd, TABLE_LIST *table, bool *exists) +{ + char path[FN_REFLEN]; + int rc; + DBUG_ENTER("check_if_table_exists"); + + safe_mutex_assert_owner(&LOCK_open); + + *exists= TRUE; + + build_table_path(path, sizeof(path), table->db, table->table_name, reg_ext); + + if (!access(path, F_OK)) + DBUG_RETURN(FALSE); + + /* .FRM file doesn't exist. Check if some engine can provide it. */ + + rc= ha_create_table_from_engine(thd, table->db, table->table_name); + + if (rc < 0) + { + /* Table does not exists in engines as well. */ + *exists= FALSE; + DBUG_RETURN(FALSE); + } + else if (!rc) + { + /* Table exists in some engine and .FRM for it was created. */ + DBUG_RETURN(FALSE); + } + else /* (rc > 0) */ + { + my_printf_error(ER_UNKNOWN_ERROR, "Failed to open '%-.64s', error while " + "unpacking from engine", MYF(0), table->table_name); + DBUG_RETURN(TRUE); + } +} + + /* Open a table. @@ -1231,12 +1477,17 @@ bool reopen_name_locked_table(THD* thd, TABLE_LIST* table_list) MYSQL_LOCK_IGNORE_FLUSH - Open table even if someone has done a flush or namelock on it. No version number checking is done. - MYSQL_OPEN_IGNORE_LOCKED_TABLES - Open table - ignoring set of locked tables and prelocked mode. + MYSQL_OPEN_TEMPORARY_ONLY - Open only temporary + table not the base table or view. IMPLEMENTATION Uses a cache of open tables to find a table not in use. + If table list element for the table to be opened has "create" flag + set and table does not exist, this function will automatically insert + a placeholder for exclusive name lock into the open tables cache and + will return the TABLE instance that corresponds to this placeholder. + RETURN NULL Open failed. If refresh is set then one should close all other tables and retry the open. @@ -1305,6 +1556,12 @@ TABLE *open_table(THD *thd, TABLE_LIST *table_list, MEM_ROOT *mem_root, } } + if (flags & MYSQL_OPEN_TEMPORARY_ONLY) + { + my_error(ER_NO_SUCH_TABLE, MYF(0), table_list->db, table_list->table_name); + DBUG_RETURN(0); + } + /* The table is not temporary - if we're in pre-locked or LOCK TABLES mode, let's try to find the requested table in the list of pre-opened @@ -1312,8 +1569,7 @@ TABLE *open_table(THD *thd, TABLE_LIST *table_list, MEM_ROOT *mem_root, open not pre-opened tables in pre-locked/LOCK TABLES mode. TODO: move this block into a separate function. */ - if (!(flags & MYSQL_OPEN_IGNORE_LOCKED_TABLES) && - (thd->locked_tables || thd->prelocked_mode)) + if (thd->locked_tables || thd->prelocked_mode) { // Using table locks TABLE *best_table= 0; int best_distance= INT_MIN; @@ -1495,7 +1751,7 @@ TABLE *open_table(THD *thd, TABLE_LIST *table_list, MEM_ROOT *mem_root, c1: name lock t2; -- blocks c2: open t1; -- blocks */ - if (table->s->version != refresh_version) + if (table->needs_reopen_or_name_lock()) { DBUG_PRINT("note", ("Found table '%s.%s' with different refresh version", @@ -1507,6 +1763,14 @@ TABLE *open_table(THD *thd, TABLE_LIST *table_list, MEM_ROOT *mem_root, continue; } + /* Avoid self-deadlocks by detecting self-dependencies. */ + if (table->open_placeholder && table->in_use == thd) + { + VOID(pthread_mutex_unlock(&LOCK_open)); + my_error(ER_UPDATE_TABLE_USED, MYF(0), table->s->table_name); + DBUG_RETURN(0); + } + /* Back off, part 1: mark the table as "unused" for the purpose of name-locking by setting table->db_stat to 0. Do @@ -1523,6 +1787,14 @@ TABLE *open_table(THD *thd, TABLE_LIST *table_list, MEM_ROOT *mem_root, and wait till the operation is complete: when any operation that juggles with table->s->version completes, it broadcasts COND_refresh condition variable. + If 'old' table we met is in use by current thread we return + without waiting since in this situation it's this thread + which is responsible for broadcasting on COND_refresh + (and this was done already in close_old_data_files()). + Good example of such situation is when we have statement + that needs two instances of table and FLUSH TABLES comes + after we open first instance but before we open second + instance. */ if (table->in_use != thd) { @@ -1564,6 +1836,40 @@ TABLE *open_table(THD *thd, TABLE_LIST *table_list, MEM_ROOT *mem_root, while (open_cache.records > table_cache_size && unused_tables) VOID(hash_delete(&open_cache,(byte*) unused_tables)); /* purecov: tested */ + if (table_list->create) + { + bool exists; + + if (check_if_table_exists(thd, table_list, &exists)) + { + VOID(pthread_mutex_unlock(&LOCK_open)); + DBUG_RETURN(NULL); + } + + if (!exists) + { + /* + Table to be created, so we need to create placeholder in table-cache. + */ + if (!(table= table_cache_insert_placeholder(thd, key, key_length))) + { + VOID(pthread_mutex_unlock(&LOCK_open)); + DBUG_RETURN(NULL); + } + /* + Link placeholder to the open tables list so it will be automatically + removed once tables are closed. Also mark it so it won't be ignored + by other trying to take name-lock. + */ + table->open_placeholder= 1; + table->next= thd->open_tables; + thd->open_tables= table; + VOID(pthread_mutex_unlock(&LOCK_open)); + DBUG_RETURN(table); + } + /* Table exists. Let us try to open it. */ + } + /* make a new table */ if (!(table=(TABLE*) my_malloc(sizeof(*table),MYF(MY_WME)))) { @@ -1794,9 +2100,24 @@ bool close_data_tables(THD *thd,const char *db, const char *table_name) } -/* - Reopen all tables with closed data files - One should have lock on LOCK_open when calling this +/** + @brief Reopen all tables with closed data files. + + @param thd Thread context + @param get_locks Should we get locks after reopening tables ? + @param in_refresh Are we in FLUSH TABLES ? TODO: It seems that + we can remove this parameter. + + @note Since this function can't properly handle prelocking and + create placeholders it should be used in very special + situations like FLUSH TABLES or ALTER TABLE. In general + case one should just repeat open_tables()/lock_tables() + combination when one needs tables to be reopened (for + example see open_and_lock_tables()). + + @note One should have lock on LOCK_open when calling this. + + @return FALSE in case of success, TRUE - otherwise. */ bool reopen_tables(THD *thd,bool get_locks,bool in_refresh) @@ -1841,7 +2162,7 @@ bool reopen_tables(THD *thd,bool get_locks,bool in_refresh) if (in_refresh) { table->s->version=0; - table->locked_by_flush=0; + table->open_placeholder= 0; } } } @@ -1867,35 +2188,71 @@ bool reopen_tables(THD *thd,bool get_locks,bool in_refresh) DBUG_RETURN(error); } -/* - Close handlers for tables in list, but leave the TABLE structure - intact so that we can re-open these quickly - abort_locks is set if called from flush_tables. + +/** + @brief Close handlers for tables in list, but leave the TABLE structure + intact so that we can re-open these quickly. + + @param thd Thread context + @param table Head of the list of TABLE objects + @param morph_locks TRUE - remove locks which we have on tables being closed + but ensure that no DML or DDL will sneak in before + we will re-open the table (i.e. temporarily morph + our table-level locks into name-locks). + FALSE - otherwise + @param send_refresh Should we awake waiters even if we didn't close any tables? */ -void close_old_data_files(THD *thd, TABLE *table, bool abort_locks, +void close_old_data_files(THD *thd, TABLE *table, bool morph_locks, bool send_refresh) { DBUG_ENTER("close_old_data_files"); bool found=send_refresh; for (; table ; table=table->next) { - if (table->s->version != refresh_version) + if (table->needs_reopen_or_name_lock()) { found=1; - if (!abort_locks) // If not from flush tables + /* + Note that it is safe to update version even for open placeholders + as later in this function we reset TABLE::open_placeholder and thus + effectively remove them from the table cache. + */ + if (!morph_locks) // If not from flush tables table->s->version= refresh_version; // Let other threads use table if (table->db_stat) { - if (abort_locks) - { - mysql_lock_abort(thd,table); // Close waiting threads - mysql_lock_remove(thd, thd->locked_tables,table); - table->locked_by_flush=1; // Will be reopened with locks - } + if (morph_locks) + { + /* + Wake up threads waiting for table-level lock on this table + so they won't sneak in when we will temporarily remove our + lock on it. This will also give them a chance to close their + instances of this table. + */ + mysql_lock_abort(thd, table); + mysql_lock_remove(thd, thd->locked_tables, table); + /* + We want to protect the table from concurrent DDL operations + (like RENAME TABLE) until we will re-open and re-lock it. + */ + table->open_placeholder= 1; + } table->file->close(); table->db_stat=0; } + else if (table->open_placeholder) + { + /* + We come here only in close-for-back-off scenario. So we have to + "close" create placeholder here to avoid deadlocks (for example, + in case of concurrent execution of CREATE TABLE t1 SELECT * FROM t2 + and RENAME TABLE t2 TO t1). In close-for-re-open scenario we will + probably want to let it stay. + */ + DBUG_ASSERT(!morph_locks); + table->open_placeholder= 0; + } } } if (found) @@ -1923,9 +2280,8 @@ bool table_is_used(TABLE *table, bool wait_for_name_lock) search= (TABLE*) hash_next(&open_cache, (byte*) key, key_length, &state)) { - if (search->locked_by_flush || - search->locked_by_name && wait_for_name_lock || - search->db_stat && search->s->version < refresh_version) + if (search->locked_by_name && wait_for_name_lock || + search->is_name_opened() && search->needs_reopen_or_name_lock()) return 1; // Table is used } } while ((table=table->next)); @@ -5661,7 +6017,7 @@ bool remove_table_from_cache(THD *thd, const char *db, const char *table_name, else if (in_use != thd) { in_use->some_tables_deleted=1; - if (table->db_stat) + if (table->is_name_opened()) { DBUG_PRINT("info", ("Found another active instance of the table")); result=1; diff --git a/sql/sql_handler.cc b/sql/sql_handler.cc index d1a5ab7dfa8..e1318aa2736 100644 --- a/sql/sql_handler.cc +++ b/sql/sql_handler.cc @@ -670,7 +670,7 @@ int mysql_ha_flush(THD *thd, TABLE_LIST *tables, uint mode_flags, while (*table_ptr) { if ((mode_flags & MYSQL_HA_FLUSH_ALL) || - ((*table_ptr)->s->version != refresh_version)) + (*table_ptr)->needs_reopen_or_name_lock()) { /* The first time it is required, lock for close_thread_table(). */ if (! did_lock && ! is_locked) @@ -771,15 +771,22 @@ void mysql_ha_mark_tables_for_reopen(THD *thd, TABLE *table) safe_mutex_assert_owner(&LOCK_open); for (; table; table= table->next) { - TABLE_LIST *hash_tables; - if ((hash_tables= (TABLE_LIST*) hash_search(&thd->handler_tables_hash, - (byte*) table->alias, - strlen(table->alias) + 1))) + /* + Some elements in open table list, for example placeholders used for + name-locking, can have alias set to 0. + */ + if (table->alias) { - /* Mark table as ready for reopen. */ - hash_tables->table= NULL; - /* End open index/table scans. */ - table->file->ha_index_or_rnd_end(); + TABLE_LIST *hash_tables; + if ((hash_tables= (TABLE_LIST*) hash_search(&thd->handler_tables_hash, + (byte*) table->alias, + strlen(table->alias) + 1))) + { + /* Mark table as ready for reopen. */ + hash_tables->table= NULL; + /* End open index/table scans. */ + table->file->ha_index_or_rnd_end(); + } } } DBUG_VOID_RETURN; diff --git a/sql/sql_insert.cc b/sql/sql_insert.cc index f1d86224adb..4ba820ddefd 100644 --- a/sql/sql_insert.cc +++ b/sql/sql_insert.cc @@ -2179,7 +2179,7 @@ bool delayed_insert::handle_inserts(void) thd.proc_info="insert"; max_rows= delayed_insert_limit; - if (thd.killed || table->s->version != refresh_version) + if (thd.killed || table->needs_reopen_or_name_lock()) { thd.killed= THD::KILL_CONNECTION; max_rows= ~(ulong)0; // Do as much as possible @@ -2791,8 +2791,8 @@ bool select_insert::send_eof() ***************************************************************************/ /* - Create table from lists of fields and items (or open existing table - with same name). + Create table from lists of fields and items (or just return TABLE + object for pre-opened existing table). SYNOPSIS create_table_from_items() @@ -2807,18 +2807,24 @@ bool select_insert::send_eof() of fields for the table (corresponding fields will be added to the end of alter_info->create_list) lock out Pointer to the MYSQL_LOCK object for table created - (open) will be returned in this parameter. Since - this table is not included in THD::lock caller is - responsible for explicitly unlocking this table. + (or open temporary table) will be returned in this + parameter. Since this table is not included in + THD::lock caller is responsible for explicitly + unlocking this table. NOTES - If 'create_info->options' bitmask has HA_LEX_CREATE_IF_NOT_EXISTS - flag and table with name provided already exists then this function will - simply open existing table. - Also note that create, open and lock sequence in this function is not - atomic and thus contains gap for deadlock and can cause other troubles. - Since this function contains some logic specific to CREATE TABLE ... SELECT - it should be changed before it can be used in other contexts. + This function behaves differently for base and temporary tables: + - For base table we assume that either table exists and was pre-opened + and locked at open_and_lock_tables() stage (and in this case we just + emit error or warning and return pre-opened TABLE object) or special + placeholder was put in table cache that guarantees that this table + won't be created or opened until the placeholder will be removed + (so there is an exclusive lock on this table). + - We don't pre-open existing temporary table, instead we either open + or create and then open table in this function. + + Since this function contains some logic specific to CREATE TABLE ... + SELECT it should be changed before it can be used in other contexts. RETURN VALUES non-zero Pointer to TABLE object for table created or opened @@ -2841,6 +2847,25 @@ static TABLE *create_table_from_items(THD *thd, HA_CREATE_INFO *create_info, bool not_used; DBUG_ENTER("create_table_from_items"); + DBUG_EXECUTE_IF("sleep_create_select_before_check_if_exists", my_sleep(6000000);); + + if (!(create_info->options & HA_LEX_CREATE_TMP_TABLE) && + create_table->table->db_stat) + { + /* Table already exists and was open at open_and_lock_tables() stage. */ + if (create_info->options & HA_LEX_CREATE_IF_NOT_EXISTS) + { + create_info->table_existed= 1; // Mark that table existed + push_warning_printf(thd, MYSQL_ERROR::WARN_LEVEL_NOTE, + ER_TABLE_EXISTS_ERROR, ER(ER_TABLE_EXISTS_ERROR), + create_table->table_name); + DBUG_RETURN(create_table->table); + } + + my_error(ER_TABLE_EXISTS_ERROR, MYF(0), create_table->table_name); + DBUG_RETURN(0); + } + tmp_table.alias= 0; tmp_table.timestamp_field= 0; tmp_table.s= &tmp_table.share_not_to_be_used; @@ -2869,8 +2894,15 @@ static TABLE *create_table_from_items(THD *thd, HA_CREATE_INFO *create_info, cr_field->flags &= ~NOT_NULL_FLAG; alter_info->create_list.push_back(cr_field); } + + DBUG_EXECUTE_IF("sleep_create_select_before_create", my_sleep(6000000);); + /* - create and lock table + Create and lock table. + + Note that we either creating (or opening existing) temporary table or + creating base table on which name we have exclusive lock. So code below + should not cause deadlocks or races. We don't log the statement, it will be logged later. @@ -2880,59 +2912,70 @@ static TABLE *create_table_from_items(THD *thd, HA_CREATE_INFO *create_info, don't want to delete from it) 2) it would be written before the CREATE TABLE, which is a wrong order. So we keep binary logging disabled when we open_table(). - NOTE: By locking table which we just have created (or for which we just have - have found that it already exists) separately from other tables used by the - statement we create potential window for deadlock. - TODO: create and open should be done atomic ! */ { tmp_disable_binlog(thd); if (!mysql_create_table(thd, create_table->db, create_table->table_name, create_info, alter_info, 0, select_field_count)) { - /* - If we are here in prelocked mode we either create temporary table - or prelocked mode is caused by the SELECT part of this statement. - */ - DBUG_ASSERT(!thd->prelocked_mode || - create_info->options & HA_LEX_CREATE_TMP_TABLE || - thd->lex->requires_prelocking()); - /* - NOTE: We don't want to ignore set of locked tables here if we are - under explicit LOCK TABLES since it will open gap for deadlock - too wide (and also is not backward compatible). - */ - if (! (table= open_table(thd, create_table, thd->mem_root, (bool*) 0, - (MYSQL_LOCK_IGNORE_FLUSH | - ((thd->prelocked_mode == PRELOCKED) ? - MYSQL_OPEN_IGNORE_LOCKED_TABLES:0))))) - quick_rm_table(create_info->db_type, create_table->db, - table_case_name(create_info, create_table->table_name)); + if (create_info->table_existed && + !(create_info->options & HA_LEX_CREATE_TMP_TABLE)) + { + /* + This means that someone created table underneath server + or it was created via different mysqld front-end to the + cluster. We don't have much options but throw an error. + */ + my_error(ER_TABLE_EXISTS_ERROR, MYF(0), create_table->table_name); + DBUG_RETURN(0); + } + + DBUG_EXECUTE_IF("sleep_create_select_before_open", my_sleep(6000000);); + + if (!(create_info->options & HA_LEX_CREATE_TMP_TABLE)) + { + VOID(pthread_mutex_lock(&LOCK_open)); + if (reopen_name_locked_table(thd, create_table, FALSE)) + { + quick_rm_table(create_info->db_type, create_table->db, + table_case_name(create_info, + create_table->table_name)); + } + else + table= create_table->table; + VOID(pthread_mutex_unlock(&LOCK_open)); + } + else + { + if (!(table= open_table(thd, create_table, thd->mem_root, (bool*) 0, + MYSQL_OPEN_TEMPORARY_ONLY)) && + !create_info->table_existed) + { + /* + This shouldn't happen as creation of temporary table should make + it preparable for open. But let us do close_temporary_table() here + just in case. + */ + close_temporary_table(thd, create_table->db, create_table->table_name); + } + } } reenable_binlog(thd); if (!table) // open failed DBUG_RETURN(0); } - /* - FIXME: What happens if trigger manages to be created while we are - obtaining this lock ? May be it is sensible just to disable - trigger execution in this case ? Or will MYSQL_LOCK_IGNORE_FLUSH - save us from that ? - */ + DBUG_EXECUTE_IF("sleep_create_select_before_lock", my_sleep(6000000);); + table->reginfo.lock_type=TL_WRITE; if (! ((*lock)= mysql_lock_tables(thd, &table, 1, MYSQL_LOCK_IGNORE_FLUSH, ¬_used))) { - VOID(pthread_mutex_lock(&LOCK_open)); - hash_delete(&open_cache,(byte*) table); - VOID(pthread_mutex_unlock(&LOCK_open)); - quick_rm_table(create_info->db_type, create_table->db, - table_case_name(create_info, create_table->table_name)); + if (!create_info->table_existed) + drop_open_table(thd, table, create_table->db, create_table->table_name); DBUG_RETURN(0); } - table->file->extra(HA_EXTRA_WRITE_CACHE); DBUG_RETURN(table); } @@ -2983,8 +3026,10 @@ select_create::prepare(List &values, SELECT_LEX_UNIT *u) (thd->variables.sql_mode & (MODE_STRICT_TRANS_TABLES | MODE_STRICT_ALL_TABLES))); - DBUG_RETURN(check_that_all_fields_are_given_values(thd, table, - table_list)); + if (check_that_all_fields_are_given_values(thd, table, table_list)) + DBUG_RETURN(1); + table->file->extra(HA_EXTRA_WRITE_CACHE); + DBUG_RETURN(0); } @@ -3016,31 +3061,18 @@ bool select_create::send_eof() { table->file->extra(HA_EXTRA_NO_IGNORE_DUP_KEY); table->file->extra(HA_EXTRA_WRITE_CANNOT_REPLACE); - VOID(pthread_mutex_lock(&LOCK_open)); - mysql_unlock_tables(thd, lock); - /* - TODO: - Check if we can remove the following two rows. - We should be able to just keep the table in the table cache. - */ - if (!table->s->tmp_table) + if (lock) { - ulong version= table->s->version; - hash_delete(&open_cache,(byte*) table); - /* Tell threads waiting for refresh that something has happened */ - if (version != refresh_version) - broadcast_refresh(); + mysql_unlock_tables(thd, lock); + lock= 0; } - lock=0; - table=0; - VOID(pthread_mutex_unlock(&LOCK_open)); } return tmp; } + void select_create::abort() { - VOID(pthread_mutex_lock(&LOCK_open)); if (lock) { mysql_unlock_tables(thd, lock); @@ -3050,22 +3082,10 @@ void select_create::abort() { table->file->extra(HA_EXTRA_NO_IGNORE_DUP_KEY); table->file->extra(HA_EXTRA_WRITE_CANNOT_REPLACE); - enum db_type table_type=table->s->db_type; - if (!table->s->tmp_table) - { - ulong version= table->s->version; - hash_delete(&open_cache,(byte*) table); - if (!create_info->table_existed) - quick_rm_table(table_type, create_table->db, create_table->table_name); - /* Tell threads waiting for refresh that something has happened */ - if (version != refresh_version) - broadcast_refresh(); - } - else if (!create_info->table_existed) - close_temporary_table(thd, create_table->db, create_table->table_name); + if (!create_info->table_existed) + drop_open_table(thd, table, create_table->db, create_table->table_name); table=0; } - VOID(pthread_mutex_unlock(&LOCK_open)); } diff --git a/sql/sql_parse.cc b/sql/sql_parse.cc index 30abb7c2c0a..4ce0dc43144 100644 --- a/sql/sql_parse.cc +++ b/sql/sql_parse.cc @@ -3014,7 +3014,13 @@ mysql_execute_command(THD *thd) select_lex->options|= SELECT_NO_UNLOCK; unit->set_limit(select_lex); - if (!(res= open_and_lock_tables(thd, select_tables))) + if (!(lex->create_info.options & HA_LEX_CREATE_TMP_TABLE)) + { + lex->link_first_table_back(create_table, link_to_local); + create_table->create= TRUE; + } + + if (!(res= open_and_lock_tables(thd, lex->query_tables))) { /* Is table which we are changing used somewhere in other parts @@ -3023,6 +3029,7 @@ mysql_execute_command(THD *thd) if (!(create_info.options & HA_LEX_CREATE_TMP_TABLE)) { TABLE_LIST *duplicate; + create_table= lex->unlink_first_table(&link_to_local); if ((duplicate= unique_table(thd, create_table, select_tables, 0))) { update_non_unique_table_error(create_table, "CREATE", duplicate); @@ -3066,6 +3073,9 @@ mysql_execute_command(THD *thd) delete sel_result; } } + else if (!(lex->create_info.options & HA_LEX_CREATE_TMP_TABLE)) + create_table= lex->unlink_first_table(&link_to_local); + } else { diff --git a/sql/sql_prepare.cc b/sql/sql_prepare.cc index d3cc3613b4a..fcd70ad92ff 100644 --- a/sql/sql_prepare.cc +++ b/sql/sql_prepare.cc @@ -1491,8 +1491,21 @@ static bool mysql_test_create_table(Prepared_statement *stmt) if (select_lex->item_list.elements) { + if (!(lex->create_info.options & HA_LEX_CREATE_TMP_TABLE)) + { + lex->link_first_table_back(create_table, link_to_local); + create_table->create= TRUE; + } + + if (open_and_lock_tables(stmt->thd, lex->query_tables)) + DBUG_RETURN(TRUE); + + if (!(lex->create_info.options & HA_LEX_CREATE_TMP_TABLE)) + create_table= lex->unlink_first_table(&link_to_local); + select_lex->context.resolve_in_select_list= TRUE; - res= select_like_stmt_test_with_open_n_lock(stmt, tables, 0, 0); + + res= select_like_stmt_test(stmt, 0, 0); } /* put tables back for PS rexecuting */ diff --git a/sql/sql_table.cc b/sql/sql_table.cc index 42d59a10712..079cc0d6456 100644 --- a/sql/sql_table.cc +++ b/sql/sql_table.cc @@ -62,8 +62,8 @@ static void set_tmp_file_path(char *buf, size_t bufsize, THD *thd); # Size of path */ -static uint build_table_path(char *buff, size_t bufflen, const char *db, - const char *table, const char *ext) +uint build_table_path(char *buff, size_t bufflen, const char *db, + const char *table, const char *ext) { strxnmov(buff, bufflen-1, mysql_data_home, "/", db, "/", table, ext, NullS); @@ -1722,7 +1722,14 @@ bool mysql_create_table(THD *thd,const char *db, const char *table_name, VOID(pthread_mutex_lock(&LOCK_open)); if (!internal_tmp_table && !(create_info->options & HA_LEX_CREATE_TMP_TABLE)) { - if (!access(path,F_OK)) + /* + Inspecting table cache for placeholders created by concurrent + CREATE TABLE ... SELECT statements to avoid interfering with them + is 5.0-only solution. Starting from 5.1 we solve this problem by + obtaining name-lock on the table to be created first. + */ + if (table_cache_has_open_placeholder(thd, db, table_name) || + !access(path, F_OK)) { if (create_info->options & HA_LEX_CREATE_IF_NOT_EXISTS) goto warn; @@ -2051,7 +2058,7 @@ static int prepare_for_restore(THD* thd, TABLE_LIST* table, to finish the restore in the handler later on */ pthread_mutex_lock(&LOCK_open); - if (reopen_name_locked_table(thd, table)) + if (reopen_name_locked_table(thd, table, TRUE)) { unlock_table_name(thd, table); pthread_mutex_unlock(&LOCK_open); @@ -2158,7 +2165,7 @@ static int prepare_for_repair(THD* thd, TABLE_LIST *table_list, to finish the repair in the handler later on. */ pthread_mutex_lock(&LOCK_open); - if (reopen_name_locked_table(thd, table_list)) + if (reopen_name_locked_table(thd, table_list, TRUE)) { unlock_table_name(thd, table_list); pthread_mutex_unlock(&LOCK_open); @@ -2803,10 +2810,24 @@ bool mysql_create_like_table(THD* thd, TABLE_LIST* table, } else { + bool exists; strxmov(dst_path, mysql_data_home, "/", db, "/", table_name, reg_ext, NullS); fn_format(dst_path, dst_path, "", "", MYF(MY_UNPACK_FILENAME)); - if (!access(dst_path, F_OK)) + + /* + Note that this critical section should actually cover most + of mysql_create_like_table() function. See bugs #18950 and + #23667 for more information. + Also note that starting from 5.1 we obtain name-lock on + target table instead of inspecting table cache for presence + of open placeholders (see comment in mysql_create_table()). + */ + pthread_mutex_lock(&LOCK_open); + exists= (table_cache_has_open_placeholder(thd, db, table_name) || + !access(dst_path, F_OK)); + pthread_mutex_unlock(&LOCK_open); + if (exists) goto table_exists; } @@ -3160,9 +3181,14 @@ view_err: else { char dir_buff[FN_REFLEN]; + bool exists; strxnmov(dir_buff, FN_REFLEN, mysql_real_data_home, new_db, NullS); - if (!access(fn_format(new_name_buff,new_name_buff,dir_buff,reg_ext,0), - F_OK)) + VOID(pthread_mutex_lock(&LOCK_open)); + exists= (table_cache_has_open_placeholder(thd, new_db, new_name) || + !access(fn_format(new_name_buff, new_name_buff, dir_buff, + reg_ext, 0), F_OK)); + VOID(pthread_mutex_unlock(&LOCK_open)); + if (exists) { /* Table will be closed in do_command() */ my_error(ER_TABLE_EXISTS_ERROR, MYF(0), new_alias); @@ -3247,8 +3273,22 @@ view_err: if (!error && (new_name != table_name || new_db != db)) { thd->proc_info="rename"; - /* Then do a 'simple' rename of the table */ - if (!access(new_name_buff,F_OK)) + /* + Then do a 'simple' rename of the table. First we need to close all + instances of 'source' table. + */ + close_cached_table(thd, table); + /* + Then, we want check once again that target table does not exist. + Note that we can't fully rely on results of previous check since + no lock was taken on target table during it. We also can't do this + before calling close_cached_table() as the latter temporarily + releases LOCK_open mutex. + Also note that starting from 5.1 we use approach with obtaining + of name-lock on target table. + */ + if (table_cache_has_open_placeholder(thd, new_db, new_name) || + !access(new_name_buff,F_OK)) { my_error(ER_TABLE_EXISTS_ERROR, MYF(0), new_name); error= -1; @@ -3256,7 +3296,6 @@ view_err: else { *fn_ext(new_name)=0; - close_cached_table(thd, table); if (mysql_rename_table(old_db_type,db,table_name,new_db,new_alias)) error= -1; else if (Table_triggers_list::change_table_name(thd, db, table_name, @@ -3806,17 +3845,6 @@ view_err: current_pid, thd->thread_id); if (lower_case_table_names) my_casedn_str(files_charset_info, old_name); - if (new_name != table_name || new_db != db) - { - if (!access(new_name_buff,F_OK)) - { - error=1; - my_error(ER_TABLE_EXISTS_ERROR, MYF(0), new_name_buff); - VOID(quick_rm_table(new_db_type,new_db,tmp_name)); - VOID(pthread_mutex_unlock(&LOCK_open)); - goto err; - } - } #if (!defined( __WIN__) && !defined( __EMX__) && !defined( OS2)) if (table->file->has_transactions()) @@ -3835,6 +3863,22 @@ view_err: table->file->extra(HA_EXTRA_FORCE_REOPEN); // Don't use this file anymore #endif + if (new_name != table_name || new_db != db) + { + /* + Check that there is no table with target name. See the + comment describing code for 'simple' ALTER TABLE ... RENAME. + */ + if (table_cache_has_open_placeholder(thd, new_db, new_name) || + !access(new_name_buff,F_OK)) + { + error=1; + my_error(ER_TABLE_EXISTS_ERROR, MYF(0), new_name_buff); + VOID(quick_rm_table(new_db_type,new_db,tmp_name)); + VOID(pthread_mutex_unlock(&LOCK_open)); + goto err; + } + } error=0; if (!need_copy_table) diff --git a/sql/sql_trigger.cc b/sql/sql_trigger.cc index 26b557a8247..cf6db22fbcb 100644 --- a/sql/sql_trigger.cc +++ b/sql/sql_trigger.cc @@ -271,7 +271,7 @@ bool mysql_create_or_drop_trigger(THD *thd, TABLE_LIST *tables, bool create) /* We also don't allow creation of triggers on views. */ tables->required_type= FRMTYPE_TABLE; - if (reopen_name_locked_table(thd, tables)) + if (reopen_name_locked_table(thd, tables, TRUE)) { unlock_table_name(thd, tables); goto end; diff --git a/sql/sql_yacc.yy b/sql/sql_yacc.yy index 390a991c7b6..12d35d5edd3 100644 --- a/sql/sql_yacc.yy +++ b/sql/sql_yacc.yy @@ -1489,9 +1489,7 @@ create: lex->sql_command= SQLCOM_CREATE_TABLE; if (!lex->select_lex.add_table_to_list(thd, $5, NULL, TL_OPTION_UPDATING, - (using_update_log ? - TL_READ_NO_INSERT: - TL_READ))) + TL_WRITE)) MYSQL_YYABORT; lex->alter_info.reset(); lex->col_list.empty(); diff --git a/sql/table.h b/sql/table.h index 61232a39f0e..da2c90ab212 100644 --- a/sql/table.h +++ b/sql/table.h @@ -188,6 +188,8 @@ typedef struct st_table_share } TABLE_SHARE; +extern ulong refresh_version; + /* Information for one open table */ struct st_table { @@ -268,7 +270,24 @@ struct st_table { my_bool force_index; my_bool distinct,const_table,no_rows; my_bool key_read, no_keyread; - my_bool locked_by_flush; + /* + Placeholder for an open table which prevents other connections + from taking name-locks on this table. Typically used with + TABLE_SHARE::version member to take an exclusive name-lock on + this table name -- a name lock that not only prevents other + threads from opening the table, but also blocks other name + locks. This is achieved by: + - setting open_placeholder to 1 - this will block other name + locks, as wait_for_locked_table_name will be forced to wait, + see table_is_used for details. + - setting version to 0 - this will force other threads to close + the instance of this table and wait (this is the same approach + as used for usual name locks). + An exclusively name-locked table currently can have no handler + object associated with it (db_stat is always 0), but please do + not rely on that. + */ + my_bool open_placeholder; my_bool locked_by_name; my_bool fulltext_searched; my_bool no_cache; @@ -291,6 +310,13 @@ struct st_table { bool fill_item_list(List *item_list) const; void reset_item_list(List *item_list) const; + /* Is table open or should be treated as such by name-locking? */ + inline bool is_name_opened() { return db_stat || open_placeholder; } + /* + Is this instance of the table should be reopen or represents a name-lock? + */ + inline bool needs_reopen_or_name_lock() + { return s->version != refresh_version; } }; enum enum_schema_table_state @@ -648,6 +674,12 @@ typedef struct st_table_list used for implicit LOCK TABLES only and won't be used in real statement. */ bool prelocking_placeholder; + /* + This TABLE_LIST object corresponds to the table to be created + so it is possible that it does not exist (used in CREATE TABLE + ... SELECT implementation). + */ + bool create; enum enum_schema_table_state schema_table_state; void calc_md5(char *buffer); @@ -655,7 +687,11 @@ typedef struct st_table_list int view_check_option(THD *thd, bool ignore_failure); bool setup_underlying(THD *thd); void cleanup_items(); - bool placeholder() {return derived || view || schema_table || !table; } + bool placeholder() + { + return derived || view || schema_table || create && !table->db_stat || + !table; + } void print(THD *thd, String *str); bool check_single_table(st_table_list **table, table_map map, st_table_list *view); From 4cafc8eeec9cb0a9d4a673b4b6542f8112bfb6dd Mon Sep 17 00:00:00 2001 From: "dlenev@mockturtle.local" <> Date: Fri, 11 May 2007 21:51:03 +0400 Subject: [PATCH 16/29] Fix for: Bug #20662 "Infinite loop in CREATE TABLE IF NOT EXISTS ... SELECT with locked tables" Bug #20903 "Crash when using CREATE TABLE .. SELECT and triggers" Bug #24738 "CREATE TABLE ... SELECT is not isolated properly" Bug #24508 "Inconsistent results of CREATE TABLE ... SELECT when temporary table exists" Deadlock occured when one tried to execute CREATE TABLE IF NOT EXISTS ... SELECT statement under LOCK TABLES which held read lock on target table. Attempt to execute the same statement for already existing target table with triggers caused server crashes. Also concurrent execution of CREATE TABLE ... SELECT statement and other statements involving target table suffered from various races (some of which might've led to deadlocks). Finally, attempt to execute CREATE TABLE ... SELECT in case when a temporary table with same name was already present led to the insertion of data into this temporary table and creation of empty non-temporary table. All above problems stemmed from the old implementation of CREATE TABLE ... SELECT in which we created, opened and locked target table without any special protection in a separate step and not with the rest of tables used by this statement. This underminded deadlock-avoidance approach used in server and created window for races. It also excluded target table from prelocking causing problems with trigger execution. The patch solves these problems by implementing new approach to handling of CREATE TABLE ... SELECT for base tables. We try to open and lock table to be created at the same time as the rest of tables used by this statement. If such table does not exist at this moment we create and place in the table cache special placeholder for it which prevents its creation or any other usage by other threads. We still use old approach for creation of temporary tables. Note that we have separate fix for 5.0 since there we use slightly different less intrusive approach. --- mysql-test/r/create.result | 94 ++++++ mysql-test/r/create_select-big.result | 164 ++++++++++ mysql-test/r/trigger.result | 35 ++ mysql-test/t/create.test | 112 +++++++ mysql-test/t/create_select-big.test | 268 +++++++++++++++ mysql-test/t/trigger.test | 26 ++ sql/lock.cc | 26 +- sql/mysql_priv.h | 22 +- sql/sql_base.cc | 449 +++++++++++++++++++++++--- sql/sql_handler.cc | 25 +- sql/sql_insert.cc | 179 +++++----- sql/sql_parse.cc | 18 +- sql/sql_prepare.cc | 15 +- sql/sql_table.cc | 228 ++++++++----- sql/sql_trigger.cc | 2 +- sql/sql_yacc.yy | 4 +- sql/table.h | 43 ++- 17 files changed, 1465 insertions(+), 245 deletions(-) create mode 100644 mysql-test/r/create_select-big.result create mode 100644 mysql-test/t/create_select-big.test diff --git a/mysql-test/r/create.result b/mysql-test/r/create.result index c7f8ba17930..ef22b21e9fb 100644 --- a/mysql-test/r/create.result +++ b/mysql-test/r/create.result @@ -782,6 +782,100 @@ t1 CREATE TABLE `t1` ( `i` int(11) DEFAULT NULL ) ENGINE=MyISAM DEFAULT CHARSET=latin1 MAX_ROWS=4294967295 drop table t1; +create table t1 select * from t2; +ERROR 42S02: Table 'test.t2' doesn't exist +create table t1 select * from t1; +ERROR HY000: You can't specify target table 't1' for update in FROM clause +create table t1 select coalesce('a' collate latin1_swedish_ci,'b' collate latin1_bin); +ERROR HY000: Illegal mix of collations (latin1_swedish_ci,EXPLICIT) and (latin1_bin,EXPLICIT) for operation 'coalesce' +create table t1 (primary key(a)) select "b" as b; +ERROR 42000: Key column 'a' doesn't exist in table +create table t1 (a int); +create table if not exists t1 select 1 as a, 2 as b; +ERROR 21S01: Column count doesn't match value count at row 1 +drop table t1; +create table t1 (primary key (a)) (select 1 as a) union all (select 1 as a); +ERROR 23000: Duplicate entry '1' for key 'PRIMARY' +create table t1 (i int); +create table t1 select 1 as i; +ERROR 42S01: Table 't1' already exists +create table if not exists t1 select 1 as i; +Warnings: +Note 1050 Table 't1' already exists +select * from t1; +i +1 +create table t1 select coalesce('a' collate latin1_swedish_ci,'b' collate latin1_bin); +ERROR HY000: Illegal mix of collations (latin1_swedish_ci,EXPLICIT) and (latin1_bin,EXPLICIT) for operation 'coalesce' +select * from t1; +i +1 +alter table t1 add primary key (i); +create table if not exists t1 (select 2 as i) union all (select 2 as i); +ERROR 23000: Duplicate entry '2' for key 'PRIMARY' +select * from t1; +i +1 +2 +drop table t1; +create temporary table t1 (j int); +create table if not exists t1 select 1; +Warnings: +Note 1050 Table 't1' already exists +select * from t1; +j +1 +drop temporary table t1; +select * from t1; +ERROR 42S02: Table 'test.t1' doesn't exist +drop table t1; +ERROR 42S02: Unknown table 't1' +create table t1 (i int); +insert into t1 values (1), (2); +lock tables t1 read; +create table t2 select * from t1; +ERROR HY000: Table 't2' was not locked with LOCK TABLES +create table if not exists t2 select * from t1; +ERROR HY000: Table 't2' was not locked with LOCK TABLES +unlock tables; +create table t2 (j int); +lock tables t1 read; +create table t2 select * from t1; +ERROR HY000: Table 't2' was not locked with LOCK TABLES +create table if not exists t2 select * from t1; +ERROR HY000: Table 't2' was not locked with LOCK TABLES +unlock tables; +lock table t1 read, t2 read; +create table t2 select * from t1; +ERROR HY000: Table 't2' was locked with a READ lock and can't be updated +create table if not exists t2 select * from t1; +ERROR HY000: Table 't2' was locked with a READ lock and can't be updated +unlock tables; +lock table t1 read, t2 write; +create table t2 select * from t1; +ERROR 42S01: Table 't2' already exists +create table if not exists t2 select * from t1; +Warnings: +Note 1050 Table 't2' already exists +select * from t1; +i +1 +2 +unlock tables; +drop table t2; +lock tables t1 read; +create temporary table t2 select * from t1; +create temporary table if not exists t2 select * from t1; +Warnings: +Note 1050 Table 't2' already exists +select * from t2; +i +1 +2 +1 +2 +unlock tables; +drop table t1, t2; create table t1 (upgrade int); drop table t1; End of 5.0 tests diff --git a/mysql-test/r/create_select-big.result b/mysql-test/r/create_select-big.result new file mode 100644 index 00000000000..1c393bd2224 --- /dev/null +++ b/mysql-test/r/create_select-big.result @@ -0,0 +1,164 @@ +drop table if exists t1,t2,t3,t4,t5; +set session debug="+d,sleep_create_select_before_create"; +create table t1 select 1 as i;; +create table t1 (j char(5)); +ERROR 42S01: Table 't1' already exists +show create table t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `i` int(1) NOT NULL DEFAULT '0' +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +drop table t1; +create table t1 select 1 as i;; +create table t1 select "Test" as j; +ERROR 42S01: Table 't1' already exists +show create table t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `i` int(1) NOT NULL DEFAULT '0' +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +drop table t1; +create table t3 (j char(5)); +create table t1 select 1 as i;; +create table t1 like t3; +ERROR 42S01: Table 't1' already exists +show create table t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `i` int(1) NOT NULL DEFAULT '0' +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +drop table t1; +create table t1 select 1 as i;; +rename table t3 to t1; +ERROR 42S01: Table 't1' already exists +show create table t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `i` int(1) NOT NULL DEFAULT '0' +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +drop table t1; +create table t1 select 1 as i;; +alter table t3 rename to t1; +ERROR 42S01: Table 't1' already exists +show create table t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `i` int(1) NOT NULL DEFAULT '0' +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +drop table t1; +create table t1 select 1 as i;; +alter table t3 rename to t1, add k int; +ERROR 42S01: Table 't1' already exists +show create table t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `i` int(1) NOT NULL DEFAULT '0' +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +drop table t1, t3; +set session debug="-d,sleep_create_select_before_create:+d,sleep_create_select_before_open"; +create table t1 select 1 as i;; +drop table t1; +create table t1 select 1 as i;; +rename table t1 to t2; +drop table t2; +create table t1 select 1 as i;; +select * from t1; +i +1 +drop table t1; +create table t1 select 1 as i;; +insert into t1 values (2); +select * from t1; +i +1 +2 +drop table t1; +set @a:=0; +create table t1 select 1 as i;; +create trigger t1_bi before insert on t1 for each row set @a:=1; +select @a; +@a +0 +drop table t1; +set session debug="-d,sleep_create_select_before_open:+d,sleep_create_select_before_lock"; +create table t1 select 1 as i;; +drop table t1; +create table t1 select 1 as i;; +rename table t1 to t2; +drop table t2; +create table t1 select 1 as i;; +select * from t1; +i +1 +drop table t1; +create table t1 select 1 as i;; +insert into t1 values (2); +select * from t1; +i +1 +2 +drop table t1; +set @a:=0; +create table t1 select 1 as i;; +create trigger t1_bi before insert on t1 for each row set @a:=1; +select @a; +@a +0 +drop table t1; +set session debug="-d,sleep_create_select_before_lock:+d,sleep_create_select_before_check_if_exists"; +create table t1 (i int); +create table if not exists t1 select 1 as i;; +drop table t1; +Warnings: +Note 1050 Table 't1' already exists +create table t1 (i int); +set @a:=0; +create table if not exists t1 select 1 as i;; +create trigger t1_bi before insert on t1 for each row set @a:=1; +Warnings: +Note 1050 Table 't1' already exists +select @a; +@a +0 +select * from t1; +i +1 +drop table t1; +set session debug="-d,sleep_create_select_before_check_if_exists"; +create table t2 (a int); +create table t4 (b int); +lock table t4 write; +select 1; +1 +1 +create table t3 as select * from t4;; +create table t1 select * from t2, t3;; +unlock tables; +select * from t1; +a b +show create table t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `a` int(11) DEFAULT NULL, + `b` int(11) DEFAULT NULL +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +drop table t1, t3; +lock table t4 read; +select 1; +1 +1 +rename table t4 to t3;; +create table if not exists t1 select 1 as i from t2, t3;; +create table t5 (j int); +rename table t5 to t1; +unlock tables; +Warnings: +Note 1050 Table 't1' already exists +select * from t1; +j +show create table t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `j` int(11) DEFAULT NULL +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +drop table t1, t2, t3; diff --git a/mysql-test/r/trigger.result b/mysql-test/r/trigger.result index c24f7b6b06f..15c325aba52 100644 --- a/mysql-test/r/trigger.result +++ b/mysql-test/r/trigger.result @@ -1414,4 +1414,39 @@ id val DROP TRIGGER trg27006_a_insert; DROP TRIGGER trg27006_a_update; drop table t1,t2; +drop table if exists t1, t2, t3; +create table t1 (i int); +create trigger t1_bi before insert on t1 for each row set new.i = 7; +create trigger t1_ai after insert on t1 for each row set @a := 7; +create table t2 (j int); +insert into t2 values (1), (2); +set @a:=""; +create table if not exists t1 select * from t2; +Warnings: +Note 1050 Table 't1' already exists +select * from t1; +i +7 +7 +select @a; +@a +7 +drop trigger t1_bi; +drop trigger t1_ai; +create table t3 (isave int); +create trigger t1_bi before insert on t1 for each row insert into t3 values (new.i); +create table if not exists t1 select * from t2; +Warnings: +Note 1050 Table 't1' already exists +select * from t1; +i +7 +7 +1 +2 +select * from t3; +isave +1 +2 +drop table t1, t2, t3; End of 5.0 tests diff --git a/mysql-test/t/create.test b/mysql-test/t/create.test index 5fca63a295c..243cdea009e 100644 --- a/mysql-test/t/create.test +++ b/mysql-test/t/create.test @@ -673,6 +673,117 @@ alter table t1 max_rows=100000000000; show create table t1; drop table t1; + +# +# Tests for errors happening at various stages of CREATE TABLES ... SELECT +# +# (Also checks that it behaves atomically in the sense that in case +# of error it is automatically dropped if it has not existed before.) +# +# Error during open_and_lock_tables() of tables +--error ER_NO_SUCH_TABLE +create table t1 select * from t2; +# Rather special error which also caught during open tables pahse +--error ER_UPDATE_TABLE_USED +create table t1 select * from t1; +# Error which happens before select_create::prepare() +--error ER_CANT_AGGREGATE_2COLLATIONS +create table t1 select coalesce('a' collate latin1_swedish_ci,'b' collate latin1_bin); +# Error during table creation +--error ER_KEY_COLUMN_DOES_NOT_EXITS +create table t1 (primary key(a)) select "b" as b; +# Error in select_create::prepare() which is not related to table creation +create table t1 (a int); +--error ER_WRONG_VALUE_COUNT_ON_ROW +create table if not exists t1 select 1 as a, 2 as b; +drop table t1; +# Finally error which happens during insert +--error ER_DUP_ENTRY_WITH_KEY_NAME +create table t1 (primary key (a)) (select 1 as a) union all (select 1 as a); +# What happens if table already exists ? +create table t1 (i int); +--error ER_TABLE_EXISTS_ERROR +create table t1 select 1 as i; +create table if not exists t1 select 1 as i; +select * from t1; +# Error before select_create::prepare() +--error ER_CANT_AGGREGATE_2COLLATIONS +create table t1 select coalesce('a' collate latin1_swedish_ci,'b' collate latin1_bin); +select * from t1; +# Error which happens during insertion of rows +alter table t1 add primary key (i); +--error ER_DUP_ENTRY_WITH_KEY_NAME +create table if not exists t1 (select 2 as i) union all (select 2 as i); +select * from t1; +drop table t1; + + +# Base vs temporary tables dillema (a.k.a. bug#24508 "Inconsistent +# results of CREATE TABLE ... SELECT when temporary table exists"). +# In this situation we either have to create non-temporary table and +# insert data in it or insert data in temporary table without creation +# of permanent table. Since currently temporary tables always shadow +# permanent tables we adopt second approach. +create temporary table t1 (j int); +create table if not exists t1 select 1; +select * from t1; +drop temporary table t1; +--error ER_NO_SUCH_TABLE +select * from t1; +--error ER_BAD_TABLE_ERROR +drop table t1; + + +# +# CREATE TABLE ... SELECT and LOCK TABLES +# +# There is little sense in using CREATE TABLE ... SELECT under +# LOCK TABLES as it mostly does not work. At least we check that +# the server doesn't crash, hang and produces sensible errors. +# Includes test for bug #20662 "Infinite loop in CREATE TABLE +# IF NOT EXISTS ... SELECT with locked tables". +create table t1 (i int); +insert into t1 values (1), (2); +lock tables t1 read; +--error ER_TABLE_NOT_LOCKED +create table t2 select * from t1; +--error ER_TABLE_NOT_LOCKED +create table if not exists t2 select * from t1; +unlock tables; +create table t2 (j int); +lock tables t1 read; +--error ER_TABLE_NOT_LOCKED +create table t2 select * from t1; +# This should not be ever allowed as it will undermine +# lock-all-at-once approach +--error ER_TABLE_NOT_LOCKED +create table if not exists t2 select * from t1; +unlock tables; +lock table t1 read, t2 read; +--error ER_TABLE_NOT_LOCKED_FOR_WRITE +create table t2 select * from t1; +--error ER_TABLE_NOT_LOCKED_FOR_WRITE +create table if not exists t2 select * from t1; +unlock tables; +lock table t1 read, t2 write; +--error ER_TABLE_EXISTS_ERROR +create table t2 select * from t1; +# This is the only case which really works. +create table if not exists t2 select * from t1; +select * from t1; +unlock tables; +drop table t2; + +# OTOH CREATE TEMPORARY TABLE ... SELECT should work +# well under LOCK TABLES. +lock tables t1 read; +create temporary table t2 select * from t1; +create temporary table if not exists t2 select * from t1; +select * from t2; +unlock tables; +drop table t1, t2; + + # # Bug#21772: can not name a column 'upgrade' when create a table # @@ -715,6 +826,7 @@ INSERT INTO t2 select * from t1; SELECT * from t2; drop table t1,t2; + # # Test incorrect database names # diff --git a/mysql-test/t/create_select-big.test b/mysql-test/t/create_select-big.test new file mode 100644 index 00000000000..3fa655c5501 --- /dev/null +++ b/mysql-test/t/create_select-big.test @@ -0,0 +1,268 @@ +# Tests for various aspects of CREATE TABLE ... SELECT implementation +# +# Note that we don't test general CREATE TABLE ... SELECT functionality here as +# it is already covered by create.test. We are more interested in extreme cases. +# +# This test takes rather long time so let us run it only in --big-test mode +--source include/big_test.inc +# We are using some debug-only features in this test +--source include/have_debug.inc + +# Create auxilliary connections +connect (addconroot1, localhost, root,,); +connect (addconroot2, localhost, root,,); +connect (addconroot3, localhost, root,,); +connection default; + +--disable_warnings +drop table if exists t1,t2,t3,t4,t5; +--enable_warnings + + +# +# Tests for concurrency problems. +# +# We introduce delays between various stages of table creation +# and check that other statements dealing with this table cannot +# interfere during those delays. +# +# What happens in situation when other statement messes with +# table to be created before it is created ? +# Concurrent CREATE TABLE +set session debug="+d,sleep_create_select_before_create"; +--send create table t1 select 1 as i; +connection addconroot1; +--sleep 2 +--error ER_TABLE_EXISTS_ERROR +create table t1 (j char(5)); +connection default; +--reap +show create table t1; +drop table t1; +# Concurrent CREATE TABLE ... SELECT +--send create table t1 select 1 as i; +connection addconroot1; +--sleep 2 +--error ER_TABLE_EXISTS_ERROR +create table t1 select "Test" as j; +connection default; +--reap +show create table t1; +drop table t1; +# Concurrent CREATE TABLE LIKE +create table t3 (j char(5)); +--send create table t1 select 1 as i; +connection addconroot1; +--sleep 2 +--error ER_TABLE_EXISTS_ERROR +create table t1 like t3; +connection default; +--reap +show create table t1; +drop table t1; +# Concurrent RENAME TABLE +--send create table t1 select 1 as i; +connection addconroot1; +--sleep 2 +--error ER_TABLE_EXISTS_ERROR +rename table t3 to t1; +connection default; +--reap +show create table t1; +drop table t1; +# Concurrent ALTER TABLE RENAME +--send create table t1 select 1 as i; +connection addconroot1; +--sleep 2 +--error ER_TABLE_EXISTS_ERROR +alter table t3 rename to t1; +connection default; +--reap +show create table t1; +drop table t1; +# Concurrent ALTER TABLE RENAME which also adds column +--send create table t1 select 1 as i; +connection addconroot1; +--sleep 2 +--error ER_TABLE_EXISTS_ERROR +alter table t3 rename to t1, add k int; +connection default; +--reap +show create table t1; +drop table t1, t3; +# What happens if other statement sneaks in after the table +# creation but before its opening ? +set session debug="-d,sleep_create_select_before_create:+d,sleep_create_select_before_open"; +# Concurrent DROP TABLE +--send create table t1 select 1 as i; +connection addconroot1; +--sleep 2 +drop table t1; +connection default; +--reap +# Concurrent RENAME TABLE +--send create table t1 select 1 as i; +connection addconroot1; +--sleep 2 +rename table t1 to t2; +connection default; +--reap +drop table t2; +# Concurrent SELECT +--send create table t1 select 1 as i; +connection addconroot1; +--sleep 2 +select * from t1; +connection default; +--reap +drop table t1; +# Concurrent INSERT +--send create table t1 select 1 as i; +connection addconroot1; +--sleep 2 +insert into t1 values (2); +connection default; +--reap +select * from t1; +drop table t1; +# Concurrent CREATE TRIGGER +set @a:=0; +--send create table t1 select 1 as i; +connection addconroot1; +--sleep 2 +create trigger t1_bi before insert on t1 for each row set @a:=1; +connection default; +--reap +select @a; +drop table t1; +# Okay, now the same tests for the potential gap between open and lock +set session debug="-d,sleep_create_select_before_open:+d,sleep_create_select_before_lock"; +# Concurrent DROP TABLE +--send create table t1 select 1 as i; +connection addconroot1; +--sleep 2 +drop table t1; +connection default; +--reap +# Concurrent RENAME TABLE +--send create table t1 select 1 as i; +connection addconroot1; +--sleep 2 +rename table t1 to t2; +connection default; +--reap +drop table t2; +# Concurrent SELECT +--send create table t1 select 1 as i; +connection addconroot1; +--sleep 2 +select * from t1; +connection default; +--reap +drop table t1; +# Concurrent INSERT +--send create table t1 select 1 as i; +connection addconroot1; +--sleep 2 +insert into t1 values (2); +connection default; +--reap +select * from t1; +drop table t1; +# Concurrent CREATE TRIGGER +set @a:=0; +--send create table t1 select 1 as i; +connection addconroot1; +--sleep 2 +create trigger t1_bi before insert on t1 for each row set @a:=1; +connection default; +--reap +select @a; +drop table t1; +# Some tests for case with existing table +set session debug="-d,sleep_create_select_before_lock:+d,sleep_create_select_before_check_if_exists"; +create table t1 (i int); +# Concurrent DROP TABLE +--send create table if not exists t1 select 1 as i; +connection addconroot1; +--sleep 2 +drop table t1; +connection default; +--reap +# Concurrent CREATE TRIGGER +create table t1 (i int); +set @a:=0; +--send create table if not exists t1 select 1 as i; +connection addconroot1; +--sleep 2 +create trigger t1_bi before insert on t1 for each row set @a:=1; +connection default; +--reap +select @a; +select * from t1; +drop table t1; +set session debug="-d,sleep_create_select_before_check_if_exists"; + + +# Test for some details of CREATE TABLE ... SELECT implementation. +# +# We check that create placeholder is handled properly if we have +# to reopen tables in open_tables(). +# This test heavily relies on current implementation of name-locking/ +# table cache so it may stop working if it changes. OTOH it such problem +# will serve as warning that such changes should not be done lightly. +create table t2 (a int); +create table t4 (b int); +connection addconroot2; +lock table t4 write; +select 1; +connection addconroot1; +# Create placeholder/name-lock for t3 +--send create table t3 as select * from t4; +--sleep 2 +connection default; +# This statement creates placeholder for t1, then opens t2, +# then meets name-lock for t3 and then reopens all tables +--send create table t1 select * from t2, t3; +--sleep 2 +connection addconroot2; +unlock tables; +connection addconroot1; +--reap +connection default; +--reap +select * from t1; +show create table t1; +drop table t1, t3; +# Now similar test which proves that we really temporarily +# remove placeholder when we reopen tables. +connection addconroot2; +lock table t4 read; +select 1; +connection addconroot1; +# Create name-lock for t3 +--send rename table t4 to t3; +--sleep 2 +connection default; +# This statement creates placeholder for t1, then opens t2, +# then meets name-lock for t3 and then reopens all tables +--send create table if not exists t1 select 1 as i from t2, t3; +--sleep 2 +connection addconroot3; +# We should be able to take name-lock on table t1 as we should not have +# open placeholder for it at this point (otherwise it is possible to +# come-up with situation which will lead to deadlock, e.g. think of +# concurrent CREATE TABLE t1 SELECT * FROM t2 and RENAME TABLE t2 TO t1) +create table t5 (j int); +# This statement takes name-lock on t1 and therefore proves +# that there is no active open placeholder for it. +rename table t5 to t1; +connection addconroot2; +unlock tables; +connection addconroot1; +--reap +connection default; +--reap +select * from t1; +show create table t1; +drop table t1, t2, t3; diff --git a/mysql-test/t/trigger.test b/mysql-test/t/trigger.test index 55b67adf7c6..616e184796e 100644 --- a/mysql-test/t/trigger.test +++ b/mysql-test/t/trigger.test @@ -1737,4 +1737,30 @@ DROP TRIGGER trg27006_a_insert; DROP TRIGGER trg27006_a_update; drop table t1,t2; +# +# Bug #20903 "Crash when using CREATE TABLE .. SELECT and triggers" +# + +--disable_warnings +drop table if exists t1, t2, t3; +--enable_warnings +create table t1 (i int); +create trigger t1_bi before insert on t1 for each row set new.i = 7; +create trigger t1_ai after insert on t1 for each row set @a := 7; +create table t2 (j int); +insert into t2 values (1), (2); +set @a:=""; +create table if not exists t1 select * from t2; +select * from t1; +select @a; +# Let us check that trigger that involves table also works ok. +drop trigger t1_bi; +drop trigger t1_ai; +create table t3 (isave int); +create trigger t1_bi before insert on t1 for each row insert into t3 values (new.i); +create table if not exists t1 select * from t2; +select * from t1; +select * from t3; +drop table t1, t2, t3; + --echo End of 5.0 tests diff --git a/sql/lock.cc b/sql/lock.cc index 4427e57a938..ac6e90f68ff 100644 --- a/sql/lock.cc +++ b/sql/lock.cc @@ -892,8 +892,6 @@ end: int lock_table_name(THD *thd, TABLE_LIST *table_list, bool check_in_use) { TABLE *table; - TABLE_SHARE *share; - char *key_buff; char key[MAX_DBKEY_LENGTH]; char *db= table_list->db; uint key_length; @@ -921,29 +919,11 @@ int lock_table_name(THD *thd, TABLE_LIST *table_list, bool check_in_use) } } } - /* - Create a table entry with the right key and with an old refresh version - Note that we must use my_multi_malloc() here as this is freed by the - table cache - */ - if (!my_multi_malloc(MYF(MY_WME | MY_ZEROFILL), - &table, sizeof(*table), - &share, sizeof(*share), - &key_buff, key_length, - NULL)) - DBUG_RETURN(-1); - table->s= share; - share->set_table_cache_key(key_buff, key, key_length); - share->tmp_table= INTERNAL_TMP_TABLE; // for intern_close_table - table->in_use= thd; - table->locked_by_name=1; - table_list->table=table; - if (my_hash_insert(&open_cache, (byte*) table)) - { - my_free((gptr) table,MYF(0)); + if (!(table= table_cache_insert_placeholder(thd, key, key_length))) DBUG_RETURN(-1); - } + + table_list->table=table; /* Return 1 if table is in use */ DBUG_RETURN(test(remove_table_from_cache(thd, db, table_list->table_name, diff --git a/sql/mysql_priv.h b/sql/mysql_priv.h index 213512cd69f..c245a976b45 100644 --- a/sql/mysql_priv.h +++ b/sql/mysql_priv.h @@ -794,8 +794,6 @@ check_and_unset_inject_value(int value) #endif -uint build_table_path(char *buff, size_t bufflen, const char *db, - const char *table, const char *ext); void write_bin_log(THD *thd, bool clear_error, char const *query, ulong query_length); @@ -970,6 +968,12 @@ bool mysql_create_table(THD *thd,const char *db, const char *table_name, List &fields, List &keys, bool tmp_table, uint select_field_count, bool use_copy_create_info); +bool mysql_create_table_no_lock(THD *thd, const char *db, + const char *table_name, + HA_CREATE_INFO *create_info, + List &fields, List &keys, + bool tmp_table, uint select_field_count, + bool use_copy_create_info); bool mysql_alter_table(THD *thd, char *new_db, char *new_name, HA_CREATE_INFO *create_info, @@ -1027,7 +1031,11 @@ TABLE_SHARE *get_cached_table_share(const char *db, const char *table_name); TABLE *open_ltable(THD *thd, TABLE_LIST *table_list, thr_lock_type update); TABLE *open_table(THD *thd, TABLE_LIST *table_list, MEM_ROOT* mem, bool *refresh, uint flags); -bool reopen_name_locked_table(THD* thd, TABLE_LIST* table); +bool reopen_name_locked_table(THD* thd, TABLE_LIST* table_list, bool link_in); +TABLE *table_cache_insert_placeholder(THD *thd, const char *key, + uint key_length); +bool lock_table_name_if_not_cached(THD *thd, const char *db, + const char *table_name, TABLE **table); TABLE *find_locked_table(THD *thd, const char *db,const char *table_name); bool reopen_tables(THD *thd,bool get_locks,bool in_refresh); bool close_data_tables(THD *thd,const char *db, const char *table_name); @@ -1195,7 +1203,9 @@ void add_join_on(TABLE_LIST *b,Item *expr); 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 unlink_open_table(THD *thd, TABLE *find, bool unlock); +void drop_open_table(THD *thd, TABLE *table, const char *db_name, + const char *table_name); void update_non_unique_table_error(TABLE_LIST *update, const char *operation, TABLE_LIST *duplicate); @@ -1630,7 +1640,7 @@ extern double log_01[32]; extern ulonglong log_10_int[20]; extern ulonglong keybuff_size; extern ulonglong thd_startup_options; -extern ulong refresh_version, thread_id; +extern ulong thread_id; extern ulong binlog_cache_use, binlog_cache_disk_use; extern ulong aborted_threads,aborted_connects; extern ulong delayed_insert_timeout; @@ -1773,7 +1783,7 @@ MYSQL_LOCK *mysql_lock_tables(THD *thd, TABLE **table, uint count, #define MYSQL_LOCK_IGNORE_GLOBAL_READ_LOCK 0x0001 #define MYSQL_LOCK_IGNORE_FLUSH 0x0002 #define MYSQL_LOCK_NOTIFY_IF_NEED_REOPEN 0x0004 -#define MYSQL_OPEN_IGNORE_LOCKED_TABLES 0x0008 +#define MYSQL_OPEN_TEMPORARY_ONLY 0x0008 void mysql_unlock_tables(THD *thd, MYSQL_LOCK *sql_lock); void mysql_unlock_read_tables(THD *thd, MYSQL_LOCK *sql_lock); diff --git a/sql/sql_base.cc b/sql/sql_base.cc index f26d6e5777b..3032171dc19 100644 --- a/sql/sql_base.cc +++ b/sql/sql_base.cc @@ -97,7 +97,7 @@ static bool open_new_frm(THD *thd, TABLE_SHARE *share, const char *alias, uint db_stat, uint prgflag, uint ha_open_flags, TABLE *outparam, TABLE_LIST *table_desc, MEM_ROOT *mem_root); -static void close_old_data_files(THD *thd, TABLE *table, bool abort_locks, +static void close_old_data_files(THD *thd, TABLE *table, bool morph_locks, bool send_refresh); static bool reopen_table(TABLE *table); static bool @@ -688,6 +688,8 @@ static void close_handle_and_leave_table_as_lock(TABLE *table) MEM_ROOT *mem_root= &table->mem_root; DBUG_ENTER("close_handle_and_leave_table_as_lock"); + DBUG_ASSERT(table->db_stat); + /* Make a local copy of the table share and free the current one. This has to be done to ensure that the table share is removed from @@ -934,8 +936,22 @@ bool close_cached_tables(THD *thd, bool if_wait_for_refresh, for (uint idx=0 ; idx < open_cache.records ; idx++) { TABLE *table=(TABLE*) hash_element(&open_cache,idx); + /* + Note that we wait here only for tables which are actually open, and + not for placeholders with TABLE::open_placeholder set. Waiting for + latter will cause deadlock in the following scenario, for example: + + conn1: lock table t1 write; + conn2: lock table t2 write; + conn1: flush tables; + conn2: flush tables; + + It also does not make sense to wait for those of placeholders that + are employed by CREATE TABLE as in this case table simply does not + exist yet. + */ if (!table->s->log_table && - ((table->s->version) < refresh_version && table->db_stat)) + (table->needs_reopen_or_name_lock() && table->db_stat)) { found=1; DBUG_PRINT("signal", ("Waiting for COND_refresh")); @@ -1249,10 +1265,10 @@ bool close_thread_table(THD *thd, TABLE **table_ptr) TABLE *table= *table_ptr; DBUG_ENTER("close_thread_table"); DBUG_ASSERT(table->key_read == 0); - DBUG_ASSERT(table->file->inited == handler::NONE); + DBUG_ASSERT(!table->file || table->file->inited == handler::NONE); *table_ptr=table->next; - if (table->s->version != refresh_version || + if (table->needs_reopen_or_name_lock() || thd->version != refresh_version || !table->db_stat) { VOID(hash_delete(&open_cache,(byte*) table)); @@ -1260,6 +1276,12 @@ bool close_thread_table(THD *thd, TABLE **table_ptr) } else { + /* + Open placeholders have TABLE::db_stat set to 0, so they should be + handled by the first alternative. + */ + DBUG_ASSERT(!table->open_placeholder); + /* Free memory and reset for next loop */ table->file->ha_reset(); table->in_use=0; @@ -1778,18 +1800,32 @@ static void relink_unused(TABLE *table) } -/* - Remove all instances of table from the current open list - Free all locks on tables that are done with LOCK TABLES - */ +/** + @brief Remove all instances of table from thread's open list and + table cache. -TABLE *unlink_open_table(THD *thd, TABLE *list, TABLE *find) + @param thd Thread context + @param find Table to remove + @param unlock TRUE - free all locks on tables removed that are + done with LOCK TABLES + FALSE - otherwise + + @note When unlock parameter is FALSE or current thread doesn't have + any tables locked with LOCK TABLES tables are assumed to be + not locked (for example already unlocked). +*/ + +void unlink_open_table(THD *thd, TABLE *find, bool unlock) { char key[MAX_DBKEY_LENGTH]; uint key_length= find->s->table_cache_key.length; - TABLE *start=list,**prev,*next; - prev= &start; + TABLE *list, **prev, *next; + DBUG_ENTER("unlink_open_table"); + safe_mutex_assert_owner(&LOCK_open); + + list= thd->open_tables; + prev= &thd->open_tables; memcpy(key, find->s->table_cache_key.str, key_length); for (; list ; list=next) { @@ -1797,7 +1833,7 @@ TABLE *unlink_open_table(THD *thd, TABLE *list, TABLE *find) if (list->s->table_cache_key.length == key_length && !memcmp(list->s->table_cache_key.str, key, key_length)) { - if (thd->locked_tables) + if (unlock && thd->locked_tables) mysql_lock_remove(thd, thd->locked_tables,list); VOID(hash_delete(&open_cache,(byte*) list)); // Close table } @@ -1810,7 +1846,41 @@ TABLE *unlink_open_table(THD *thd, TABLE *list, TABLE *find) *prev=0; // Notify any 'refresh' threads broadcast_refresh(); - return start; + DBUG_VOID_RETURN; +} + + +/** + @brief Auxiliary routine which closes and drops open table. + + @param thd Thread handle + @param table TABLE object for table to be dropped + @param db_name Name of database for this table + @param table_name Name of this table + + @note This routine assumes that table to be closed is open only + by calling thread so we needn't wait until other threads + will close the table. It also assumes that table to be + dropped is already unlocked. +*/ + +void drop_open_table(THD *thd, TABLE *table, const char *db_name, + const char *table_name) +{ + if (table->s->tmp_table) + close_temporary_table(thd, table, 1, 1); + else + { + handlerton *table_type= table->s->db_type; + VOID(pthread_mutex_lock(&LOCK_open)); + /* + unlink_open_table() also tells threads waiting for refresh or close + that something has happened. + */ + unlink_open_table(thd, table, FALSE); + quick_rm_table(table_type, db_name, table_name, 0); + VOID(pthread_mutex_unlock(&LOCK_open)); + } } @@ -1867,6 +1937,11 @@ void wait_for_condition(THD *thd, pthread_mutex_t *mutex, pthread_cond_t *cond) table_list TABLE_LIST object for table to be open, TABLE_LIST::table member should point to TABLE object which was used for name-locking. + link_in TRUE - if TABLE object for table to be opened should be + linked into THD::open_tables list. + FALSE - placeholder used for name-locking is already in + this list so we only need to preserve TABLE::next + pointer. NOTE This function assumes that its caller already acquired LOCK_open mutex. @@ -1876,7 +1951,7 @@ void wait_for_condition(THD *thd, pthread_mutex_t *mutex, pthread_cond_t *cond) TRUE - Error */ -bool reopen_name_locked_table(THD* thd, TABLE_LIST* table_list) +bool reopen_name_locked_table(THD* thd, TABLE_LIST* table_list, bool link_in) { TABLE *table= table_list->table; TABLE_SHARE *share; @@ -1907,11 +1982,32 @@ bool reopen_name_locked_table(THD* thd, TABLE_LIST* table_list) } share= table->s; + /* + We want to prevent other connections from opening this table until end + of statement as it is likely that modifications of table's metadata are + not yet finished (for example CREATE TRIGGER have to change .TRG file, + or we might want to drop table if CREATE TABLE ... SELECT fails). + This also allows us to assume that no other connection will sneak in + before we will get table-level lock on this table. + */ share->version=0; table->in_use = thd; check_unused(); - table->next = thd->open_tables; - thd->open_tables = table; + + if (link_in) + { + table->next= thd->open_tables; + thd->open_tables= table; + } + else + { + /* + TABLE object should be already in THD::open_tables list so we just + need to set TABLE::next correctly. + */ + table->next= orig_table.next; + } + table->tablenr=thd->current_tablenr++; table->used_fields=0; table->const_table=0; @@ -1921,6 +2017,173 @@ bool reopen_name_locked_table(THD* thd, TABLE_LIST* table_list) } +/** + @brief Create and insert into table cache placeholder for table + which will prevent its opening (or creation) (a.k.a lock + table name). + + @param thd Thread context + @param key Table cache key for name to be locked + @param key_length Table cache key length + + @return Pointer to TABLE object used for name locking or 0 in + case of failure. +*/ + +TABLE *table_cache_insert_placeholder(THD *thd, const char *key, + uint key_length) +{ + TABLE *table; + TABLE_SHARE *share; + char *key_buff; + DBUG_ENTER("table_cache_insert_placeholder"); + + safe_mutex_assert_owner(&LOCK_open); + + /* + Create a table entry with the right key and with an old refresh version + Note that we must use my_multi_malloc() here as this is freed by the + table cache + */ + if (!my_multi_malloc(MYF(MY_WME | MY_ZEROFILL), + &table, sizeof(*table), + &share, sizeof(*share), + &key_buff, key_length, + NULL)) + DBUG_RETURN(NULL); + + table->s= share; + share->set_table_cache_key(key_buff, key, key_length); + share->tmp_table= INTERNAL_TMP_TABLE; // for intern_close_table + table->in_use= thd; + table->locked_by_name=1; + + if (my_hash_insert(&open_cache, (byte*)table)) + { + my_free((gptr) table, MYF(0)); + DBUG_RETURN(NULL); + } + + DBUG_RETURN(table); +} + + +/** + @brief Obtain an exclusive name lock on the table if it is not cached + in the table cache. + + @param thd Thread context + @param db Name of database + @param table_name Name of table + @param[out] table Out parameter which is either: + - set to NULL if table cache contains record for + the table or + - set to point to the TABLE instance used for + name-locking. + + @note This function takes into account all records for table in table + cache, even placeholders used for name-locking. This means that + 'table' parameter can be set to NULL for some situations when + table does not really exist. + + @retval TRUE Error occured (OOM) + @retval FALSE Success. 'table' parameter set according to above rules. +*/ + +bool lock_table_name_if_not_cached(THD *thd, const char *db, + const char *table_name, TABLE **table) +{ + char key[MAX_DBKEY_LENGTH]; + uint key_length; + DBUG_ENTER("lock_table_name_if_not_cached"); + + key_length= (uint)(strmov(strmov(key, db) + 1, table_name) - key) + 1; + VOID(pthread_mutex_lock(&LOCK_open)); + + if (hash_search(&open_cache, (byte *)key, key_length)) + { + VOID(pthread_mutex_unlock(&LOCK_open)); + DBUG_PRINT("info", ("Table is cached, name-lock is not obtained")); + *table= 0; + DBUG_RETURN(FALSE); + } + if (!(*table= table_cache_insert_placeholder(thd, key, key_length))) + { + VOID(pthread_mutex_unlock(&LOCK_open)); + DBUG_RETURN(TRUE); + } + (*table)->open_placeholder= 1; + (*table)->next= thd->open_tables; + thd->open_tables= *table; + VOID(pthread_mutex_unlock(&LOCK_open)); + DBUG_RETURN(FALSE); +} + + +/** + @brief Check that table exists in table definition cache, on disk + or in some storage engine. + + @param thd Thread context + @param table Table list element + @param exists[out] Out parameter which is set to TRUE if table + exists and to FALSE otherwise. + + @note This function assumes that caller owns LOCK_open mutex. + It also assumes that the fact that there are no name-locks + on the table was checked beforehand. + + @note If there is no .FRM file for the table but it exists in one + of engines (e.g. it was created on another node of NDB cluster) + this function will fetch and create proper .FRM file for it. + + @retval TRUE Some error occured + @retval FALSE No error. 'exists' out parameter set accordingly. +*/ + +bool check_if_table_exists(THD *thd, TABLE_LIST *table, bool *exists) +{ + char path[FN_REFLEN]; + int rc; + DBUG_ENTER("check_if_table_exists"); + + safe_mutex_assert_owner(&LOCK_open); + + *exists= TRUE; + + if (get_cached_table_share(table->db, table->table_name)) + DBUG_RETURN(FALSE); + + build_table_filename(path, sizeof(path) - 1, table->db, table->table_name, + reg_ext, 0); + + if (!access(path, F_OK)) + DBUG_RETURN(FALSE); + + /* .FRM file doesn't exist. Check if some engine can provide it. */ + + rc= ha_create_table_from_engine(thd, table->db, table->table_name); + + if (rc < 0) + { + /* Table does not exists in engines as well. */ + *exists= FALSE; + DBUG_RETURN(FALSE); + } + else if (!rc) + { + /* Table exists in some engine and .FRM for it was created. */ + DBUG_RETURN(FALSE); + } + else /* (rc > 0) */ + { + my_printf_error(ER_UNKNOWN_ERROR, "Failed to open '%-.64s', error while " + "unpacking from engine", MYF(0), table->table_name); + DBUG_RETURN(TRUE); + } +} + + /* Open a table. @@ -1936,12 +2199,17 @@ bool reopen_name_locked_table(THD* thd, TABLE_LIST* table_list) MYSQL_LOCK_IGNORE_FLUSH - Open table even if someone has done a flush or namelock on it. No version number checking is done. - MYSQL_OPEN_IGNORE_LOCKED_TABLES - Open table - ignoring set of locked tables and prelocked mode. + MYSQL_OPEN_TEMPORARY_ONLY - Open only temporary + table not the base table or view. IMPLEMENTATION Uses a cache of open tables to find a table not in use. + If table list element for the table to be opened has "create" flag + set and table does not exist, this function will automatically insert + a placeholder for exclusive name lock into the open tables cache and + will return the TABLE instance that corresponds to this placeholder. + RETURN NULL Open failed. If refresh is set then one should close all other tables and retry the open. @@ -2014,6 +2282,12 @@ TABLE *open_table(THD *thd, TABLE_LIST *table_list, MEM_ROOT *mem_root, } } + if (flags & MYSQL_OPEN_TEMPORARY_ONLY) + { + my_error(ER_NO_SUCH_TABLE, MYF(0), table_list->db, table_list->table_name); + DBUG_RETURN(0); + } + /* The table is not temporary - if we're in pre-locked or LOCK TABLES mode, let's try to find the requested table in the list of pre-opened @@ -2021,8 +2295,7 @@ TABLE *open_table(THD *thd, TABLE_LIST *table_list, MEM_ROOT *mem_root, open not pre-opened tables in pre-locked/LOCK TABLES mode. TODO: move this block into a separate function. */ - if (!(flags & MYSQL_OPEN_IGNORE_LOCKED_TABLES) && - (thd->locked_tables || thd->prelocked_mode)) + if (thd->locked_tables || thd->prelocked_mode) { // Using table locks TABLE *best_table= 0; int best_distance= INT_MIN; @@ -2204,7 +2477,7 @@ TABLE *open_table(THD *thd, TABLE_LIST *table_list, MEM_ROOT *mem_root, c1: name lock t2; -- blocks c2: open t1; -- blocks */ - if (table->s->version != refresh_version && !table->s->log_table) + if (table->needs_reopen_or_name_lock() && !table->s->log_table) { DBUG_PRINT("note", ("Found table '%s.%s' with different refresh version", @@ -2217,6 +2490,14 @@ TABLE *open_table(THD *thd, TABLE_LIST *table_list, MEM_ROOT *mem_root, continue; } + /* Avoid self-deadlocks by detecting self-dependencies. */ + if (table->open_placeholder && table->in_use == thd) + { + VOID(pthread_mutex_unlock(&LOCK_open)); + my_error(ER_UPDATE_TABLE_USED, MYF(0), table->s->table_name.str); + DBUG_RETURN(0); + } + /* Back off, part 1: mark the table as "unused" for the purpose of name-locking by setting table->db_stat to 0. Do @@ -2233,6 +2514,14 @@ TABLE *open_table(THD *thd, TABLE_LIST *table_list, MEM_ROOT *mem_root, and wait till the operation is complete: when any operation that juggles with table->s->version completes, it broadcasts COND_refresh condition variable. + If 'old' table we met is in use by current thread we return + without waiting since in this situation it's this thread + which is responsible for broadcasting on COND_refresh + (and this was done already in close_old_data_files()). + Good example of such situation is when we have statement + that needs two instances of table and FLUSH TABLES comes + after we open first instance but before we open second + instance. */ if (table->in_use != thd) { @@ -2273,6 +2562,40 @@ TABLE *open_table(THD *thd, TABLE_LIST *table_list, MEM_ROOT *mem_root, while (open_cache.records > table_cache_size && unused_tables) VOID(hash_delete(&open_cache,(byte*) unused_tables)); /* purecov: tested */ + if (table_list->create) + { + bool exists; + + if (check_if_table_exists(thd, table_list, &exists)) + { + VOID(pthread_mutex_unlock(&LOCK_open)); + DBUG_RETURN(NULL); + } + + if (!exists) + { + /* + Table to be created, so we need to create placeholder in table-cache. + */ + if (!(table= table_cache_insert_placeholder(thd, key, key_length))) + { + VOID(pthread_mutex_unlock(&LOCK_open)); + DBUG_RETURN(NULL); + } + /* + Link placeholder to the open tables list so it will be automatically + removed once tables are closed. Also mark it so it won't be ignored + by other trying to take name-lock. + */ + table->open_placeholder= 1; + table->next= thd->open_tables; + thd->open_tables= table; + VOID(pthread_mutex_unlock(&LOCK_open)); + DBUG_RETURN(table); + } + /* Table exists. Let us try to open it. */ + } + /* make a new table */ if (!(table=(TABLE*) my_malloc(sizeof(*table),MYF(MY_WME)))) { @@ -2489,9 +2812,24 @@ bool close_data_tables(THD *thd,const char *db, const char *table_name) } -/* - Reopen all tables with closed data files - One should have lock on LOCK_open when calling this +/** + @brief Reopen all tables with closed data files. + + @param thd Thread context + @param get_locks Should we get locks after reopening tables ? + @param in_refresh Are we in FLUSH TABLES ? TODO: It seems that + we can remove this parameter. + + @note Since this function can't properly handle prelocking and + create placeholders it should be used in very special + situations like FLUSH TABLES or ALTER TABLE. In general + case one should just repeat open_tables()/lock_tables() + combination when one needs tables to be reopened (for + example see open_and_lock_tables()). + + @note One should have lock on LOCK_open when calling this. + + @return FALSE in case of success, TRUE - otherwise. */ bool reopen_tables(THD *thd,bool get_locks,bool in_refresh) @@ -2537,7 +2875,7 @@ bool reopen_tables(THD *thd,bool get_locks,bool in_refresh) if (in_refresh) { table->s->version=0; - table->locked_by_flush=0; + table->open_placeholder= 0; } } } @@ -2564,13 +2902,21 @@ bool reopen_tables(THD *thd,bool get_locks,bool in_refresh) } -/* - Close handlers for tables in list, but leave the TABLE structure - intact so that we can re-open these quickly - abort_locks is set if called from flush_tables. +/** + @brief Close handlers for tables in list, but leave the TABLE structure + intact so that we can re-open these quickly. + + @param thd Thread context + @param table Head of the list of TABLE objects + @param morph_locks TRUE - remove locks which we have on tables being closed + but ensure that no DML or DDL will sneak in before + we will re-open the table (i.e. temporarily morph + our table-level locks into name-locks). + FALSE - otherwise + @param send_refresh Should we awake waiters even if we didn't close any tables? */ -void close_old_data_files(THD *thd, TABLE *table, bool abort_locks, +void close_old_data_files(THD *thd, TABLE *table, bool morph_locks, bool send_refresh) { bool found= send_refresh; @@ -2582,19 +2928,41 @@ void close_old_data_files(THD *thd, TABLE *table, bool abort_locks, Reopen marked for flush. But close log tables. They are flushed only explicitly on FLUSH LOGS */ - if (table->s->version != refresh_version && !table->s->log_table) + if (table->needs_reopen_or_name_lock() && !table->s->log_table) { found=1; if (table->db_stat) { - if (abort_locks) + if (morph_locks) { - mysql_lock_abort(thd,table, TRUE); // Close waiting threads - mysql_lock_remove(thd, thd->locked_tables,table); - table->locked_by_flush=1; // Will be reopened with locks + /* + Wake up threads waiting for table-level lock on this table + so they won't sneak in when we will temporarily remove our + lock on it. This will also give them a chance to close their + instances of this table. + */ + mysql_lock_abort(thd, table, TRUE); + mysql_lock_remove(thd, thd->locked_tables, table); + /* + We want to protect the table from concurrent DDL operations + (like RENAME TABLE) until we will re-open and re-lock it. + */ + table->open_placeholder= 1; } close_handle_and_leave_table_as_lock(table); } + else if (table->open_placeholder) + { + /* + We come here only in close-for-back-off scenario. So we have to + "close" create placeholder here to avoid deadlocks (for example, + in case of concurrent execution of CREATE TABLE t1 SELECT * FROM t2 + and RENAME TABLE t2 TO t1). In close-for-re-open scenario we will + probably want to let it stay. + */ + DBUG_ASSERT(!morph_locks); + table->open_placeholder= 0; + } } } if (found) @@ -2630,10 +2998,10 @@ bool table_is_used(TABLE *table, bool wait_for_name_lock) key_length, &state)) { DBUG_PRINT("info", ("share: 0x%lx locked_by_logger: %d " - "locked_by_flush: %d locked_by_name: %d " + "open_placeholder: %d locked_by_name: %d " "db_stat: %u version: %lu", (ulong) search->s, search->locked_by_logger, - search->locked_by_flush, search->locked_by_name, + search->open_placeholder, search->locked_by_name, search->db_stat, search->s->version)); if (search->in_use == table->in_use) @@ -2649,8 +3017,7 @@ bool table_is_used(TABLE *table, bool wait_for_name_lock) */ if (!search->locked_by_logger && (search->locked_by_name && wait_for_name_lock || - search->locked_by_flush || - (search->db_stat && search->s->version < refresh_version))) + (search->is_name_opened() && search->needs_reopen_or_name_lock()))) DBUG_RETURN(1); } } while ((table=table->next)); @@ -6637,7 +7004,7 @@ bool remove_table_from_cache(THD *thd, const char *db, const char *table_name, { DBUG_PRINT("info", ("Table was in use by other thread")); in_use->some_tables_deleted=1; - if (table->db_stat) + if (table->is_name_opened()) { DBUG_PRINT("info", ("Found another active instance of the table")); result=1; @@ -7012,7 +7379,7 @@ has_two_write_locked_tables_with_auto_increment(TABLE_LIST *tables) for (TABLE_LIST *table= tables; table; table= table->next_global) { /* we must do preliminary checks as table->table may be NULL */ - if (!table->placeholder() && !table->schema_table && + if (!table->placeholder() && table->table->found_next_number_field && (table->lock_type >= TL_WRITE_ALLOW_WRITE)) { diff --git a/sql/sql_handler.cc b/sql/sql_handler.cc index cd87330cedb..aaca6373f37 100644 --- a/sql/sql_handler.cc +++ b/sql/sql_handler.cc @@ -682,7 +682,7 @@ int mysql_ha_flush(THD *thd, TABLE_LIST *tables, uint mode_flags, while (*table_ptr) { if ((mode_flags & MYSQL_HA_FLUSH_ALL) || - ((*table_ptr)->s->version != refresh_version)) + (*table_ptr)->needs_reopen_or_name_lock()) { /* The first time it is required, lock for close_thread_table(). */ if (! did_lock && ! is_locked) @@ -783,15 +783,22 @@ void mysql_ha_mark_tables_for_reopen(THD *thd, TABLE *table) safe_mutex_assert_owner(&LOCK_open); for (; table; table= table->next) { - TABLE_LIST *hash_tables; - if ((hash_tables= (TABLE_LIST*) hash_search(&thd->handler_tables_hash, - (byte*) table->alias, - strlen(table->alias) + 1))) + /* + Some elements in open table list, for example placeholders used for + name-locking, can have alias set to 0. + */ + if (table->alias) { - /* Mark table as ready for reopen. */ - hash_tables->table= NULL; - /* End open index/table scans. */ - table->file->ha_index_or_rnd_end(); + TABLE_LIST *hash_tables; + if ((hash_tables= (TABLE_LIST*) hash_search(&thd->handler_tables_hash, + (byte*) table->alias, + strlen(table->alias) + 1))) + { + /* Mark table as ready for reopen. */ + hash_tables->table= NULL; + /* End open index/table scans. */ + table->file->ha_index_or_rnd_end(); + } } } DBUG_VOID_RETURN; diff --git a/sql/sql_insert.cc b/sql/sql_insert.cc index f6ae2df3750..d6fd8359333 100644 --- a/sql/sql_insert.cc +++ b/sql/sql_insert.cc @@ -2261,7 +2261,7 @@ bool delayed_insert::handle_inserts(void) thd.proc_info="insert"; max_rows= delayed_insert_limit; - if (thd.killed || table->s->version != refresh_version) + if (thd.killed || table->needs_reopen_or_name_lock()) { thd.killed= THD::KILL_CONNECTION; max_rows= ULONG_MAX; // Do as much as possible @@ -2962,8 +2962,8 @@ bool select_insert::send_eof() ***************************************************************************/ /* - Create table from lists of fields and items (or open existing table - with same name). + Create table from lists of fields and items (or just return TABLE + object for pre-opened existing table). SYNOPSIS create_table_from_items() @@ -2978,19 +2978,25 @@ bool select_insert::send_eof() of fields for the table (corresponding fields will be added to the end of 'extra_fields' list) lock out Pointer to the MYSQL_LOCK object for table created - (open) will be returned in this parameter. Since - this table is not included in THD::lock caller is - responsible for explicitly unlocking this table. + (or open temporary table) will be returned in this + parameter. Since this table is not included in + THD::lock caller is responsible for explicitly + unlocking this table. hooks NOTES - If 'create_info->options' bitmask has HA_LEX_CREATE_IF_NOT_EXISTS - flag and table with name provided already exists then this function will - simply open existing table. - Also note that create, open and lock sequence in this function is not - atomic and thus contains gap for deadlock and can cause other troubles. - Since this function contains some logic specific to CREATE TABLE ... SELECT - it should be changed before it can be used in other contexts. + This function behaves differently for base and temporary tables: + - For base table we assume that either table exists and was pre-opened + and locked at open_and_lock_tables() stage (and in this case we just + emit error or warning and return pre-opened TABLE object) or special + placeholder was put in table cache that guarantees that this table + won't be created or opened until the placeholder will be removed + (so there is an exclusive lock on this table). + - We don't pre-open existing temporary table, instead we either open + or create and then open table in this function. + + Since this function contains some logic specific to CREATE TABLE ... + SELECT it should be changed before it can be used in other contexts. RETURN VALUES non-zero Pointer to TABLE object for table created or opened @@ -3016,6 +3022,25 @@ static TABLE *create_table_from_items(THD *thd, HA_CREATE_INFO *create_info, bool not_used; DBUG_ENTER("create_table_from_items"); + DBUG_EXECUTE_IF("sleep_create_select_before_check_if_exists", my_sleep(6000000);); + + if (!(create_info->options & HA_LEX_CREATE_TMP_TABLE) && + create_table->table->db_stat) + { + /* Table already exists and was open at open_and_lock_tables() stage. */ + if (create_info->options & HA_LEX_CREATE_IF_NOT_EXISTS) + { + create_info->table_existed= 1; // Mark that table existed + push_warning_printf(thd, MYSQL_ERROR::WARN_LEVEL_NOTE, + ER_TABLE_EXISTS_ERROR, ER(ER_TABLE_EXISTS_ERROR), + create_table->table_name); + DBUG_RETURN(create_table->table); + } + + my_error(ER_TABLE_EXISTS_ERROR, MYF(0), create_table->table_name); + DBUG_RETURN(0); + } + tmp_table.alias= 0; tmp_table.timestamp_field= 0; tmp_table.s= &share; @@ -3047,8 +3072,15 @@ static TABLE *create_table_from_items(THD *thd, HA_CREATE_INFO *create_info, cr_field->flags &= ~NOT_NULL_FLAG; extra_fields->push_back(cr_field); } + + DBUG_EXECUTE_IF("sleep_create_select_before_create", my_sleep(6000000);); + /* - create and lock table + Create and lock table. + + Note that we either creating (or opening existing) temporary table or + creating base table on which name we have exclusive lock. So code below + should not cause deadlocks or races. We don't log the statement, it will be logged later. @@ -3058,63 +3090,74 @@ static TABLE *create_table_from_items(THD *thd, HA_CREATE_INFO *create_info, don't want to delete from it) 2) it would be written before the CREATE TABLE, which is a wrong order. So we keep binary logging disabled when we open_table(). - NOTE: By locking table which we just have created (or for which we just - have have found that it already exists) separately from other tables used - by the statement we create potential window for deadlock. - TODO: create and open should be done atomic ! */ { tmp_disable_binlog(thd); - if (!mysql_create_table(thd, create_table->db, create_table->table_name, - create_info, *extra_fields, *keys, 0, - select_field_count, 0)) + if (!mysql_create_table_no_lock(thd, create_table->db, + create_table->table_name, + create_info, *extra_fields, *keys, 0, + select_field_count, 0)) { - /* - If we are here in prelocked mode we either create temporary table - or prelocked mode is caused by the SELECT part of this statement. - */ - DBUG_ASSERT(!thd->prelocked_mode || - create_info->options & HA_LEX_CREATE_TMP_TABLE || - thd->lex->requires_prelocking()); - /* - NOTE: We don't want to ignore set of locked tables here if we are - under explicit LOCK TABLES since it will open gap for deadlock - too wide (and also is not backward compatible). - */ + if (create_info->table_existed && + !(create_info->options & HA_LEX_CREATE_TMP_TABLE)) + { + /* + This means that someone created table underneath server + or it was created via different mysqld front-end to the + cluster. We don't have much options but throw an error. + */ + my_error(ER_TABLE_EXISTS_ERROR, MYF(0), create_table->table_name); + DBUG_RETURN(0); + } + + DBUG_EXECUTE_IF("sleep_create_select_before_open", my_sleep(6000000);); + + if (!(create_info->options & HA_LEX_CREATE_TMP_TABLE)) + { + VOID(pthread_mutex_lock(&LOCK_open)); + if (reopen_name_locked_table(thd, create_table, FALSE)) + { + quick_rm_table(create_info->db_type, create_table->db, + table_case_name(create_info, create_table->table_name), + 0); + } + else + table= create_table->table; + VOID(pthread_mutex_unlock(&LOCK_open)); + } + else + { + if (!(table= open_table(thd, create_table, thd->mem_root, (bool*) 0, + MYSQL_OPEN_TEMPORARY_ONLY)) && + !create_info->table_existed) + { + /* + This shouldn't happen as creation of temporary table should make + it preparable for open. But let us do close_temporary_table() here + just in case. + */ + close_temporary_table(thd, create_table); + } + } - if (! (table= open_table(thd, create_table, thd->mem_root, (bool*) 0, - (MYSQL_LOCK_IGNORE_FLUSH | - ((thd->prelocked_mode == PRELOCKED) ? - MYSQL_OPEN_IGNORE_LOCKED_TABLES:0))))) - quick_rm_table(create_info->db_type, create_table->db, - table_case_name(create_info, create_table->table_name), - 0); } reenable_binlog(thd); if (!table) // open failed DBUG_RETURN(0); } - /* - FIXME: What happens if trigger manages to be created while we are - obtaining this lock ? May be it is sensible just to disable - trigger execution in this case ? Or will MYSQL_LOCK_IGNORE_FLUSH - save us from that ? - */ + DBUG_EXECUTE_IF("sleep_create_select_before_lock", my_sleep(6000000);); + table->reginfo.lock_type=TL_WRITE; hooks->prelock(&table, 1); // Call prelock hooks if (! ((*lock)= mysql_lock_tables(thd, &table, 1, MYSQL_LOCK_IGNORE_FLUSH, ¬_used))) { - VOID(pthread_mutex_lock(&LOCK_open)); - hash_delete(&open_cache,(byte*) table); - VOID(pthread_mutex_unlock(&LOCK_open)); - quick_rm_table(create_info->db_type, create_table->db, - table_case_name(create_info, create_table->table_name), 0); + if (!create_info->table_existed) + drop_open_table(thd, table, create_table->db, create_table->table_name); DBUG_RETURN(0); } - table->file->extra(HA_EXTRA_WRITE_CACHE); DBUG_RETURN(table); } @@ -3217,6 +3260,7 @@ select_create::prepare(List &values, SELECT_LEX_UNIT *u) if (check_that_all_fields_are_given_values(thd, table, table_list)) DBUG_RETURN(1); table->mark_columns_needed_for_insert(); + table->file->extra(HA_EXTRA_WRITE_CACHE); DBUG_RETURN(0); } @@ -3319,24 +3363,19 @@ bool select_create::send_eof() table->file->extra(HA_EXTRA_NO_IGNORE_DUP_KEY); table->file->extra(HA_EXTRA_WRITE_CANNOT_REPLACE); - VOID(pthread_mutex_lock(&LOCK_open)); - mysql_unlock_tables(thd, thd->extra_lock); - if (!table->s->tmp_table) + if (thd->extra_lock) { - if (close_thread_table(thd, &table)) - broadcast_refresh(); + mysql_unlock_tables(thd, thd->extra_lock); + thd->extra_lock=0; } - thd->extra_lock=0; - table=0; - VOID(pthread_mutex_unlock(&LOCK_open)); } return tmp; } + void select_create::abort() { DBUG_ENTER("select_create::abort"); - VOID(pthread_mutex_lock(&LOCK_open)); /* We roll back the statement, including truncating the transaction @@ -3364,24 +3403,10 @@ void select_create::abort() { table->file->extra(HA_EXTRA_NO_IGNORE_DUP_KEY); table->file->extra(HA_EXTRA_WRITE_CANNOT_REPLACE); - handlerton *table_type=table->s->db_type; - if (!table->s->tmp_table) - { - ulong version= table->s->version; - table->s->version= 0; - hash_delete(&open_cache,(byte*) table); - if (!create_info->table_existed) - quick_rm_table(table_type, create_table->db, - create_table->table_name, 0); - /* Tell threads waiting for refresh that something has happened */ - if (version != refresh_version) - broadcast_refresh(); - } - else if (!create_info->table_existed) - close_temporary_table(thd, table, 1, 1); + if (!create_info->table_existed) + drop_open_table(thd, table, create_table->db, create_table->table_name); table=0; // Safety } - VOID(pthread_mutex_unlock(&LOCK_open)); DBUG_VOID_RETURN; } diff --git a/sql/sql_parse.cc b/sql/sql_parse.cc index 702deac376b..f6924e67057 100644 --- a/sql/sql_parse.cc +++ b/sql/sql_parse.cc @@ -2164,7 +2164,13 @@ mysql_execute_command(THD *thd) select_lex->options|= SELECT_NO_UNLOCK; unit->set_limit(select_lex); - if (!(res= open_and_lock_tables(thd, select_tables))) + if (!(lex->create_info.options & HA_LEX_CREATE_TMP_TABLE)) + { + lex->link_first_table_back(create_table, link_to_local); + create_table->create= TRUE; + } + + if (!(res= open_and_lock_tables(thd, lex->query_tables))) { /* Is table which we are changing used somewhere in other parts @@ -2173,6 +2179,7 @@ mysql_execute_command(THD *thd) if (!(lex->create_info.options & HA_LEX_CREATE_TMP_TABLE)) { TABLE_LIST *duplicate; + create_table= lex->unlink_first_table(&link_to_local); if ((duplicate= unique_table(thd, create_table, select_tables, 0))) { update_non_unique_table_error(create_table, "CREATE", duplicate); @@ -2198,6 +2205,12 @@ mysql_execute_command(THD *thd) } } + /* + FIXME Temporary hack which will go away once Kostja pushes + his uber-fix for ALTER/CREATE TABLE. + */ + lex->create_info.table_existed= 0; + if ((result= new select_create(create_table, &lex->create_info, lex->create_list, @@ -2217,6 +2230,9 @@ mysql_execute_command(THD *thd) lex->create_list.empty(); lex->key_list.empty(); } + else if (!(lex->create_info.options & HA_LEX_CREATE_TMP_TABLE)) + create_table= lex->unlink_first_table(&link_to_local); + } else { diff --git a/sql/sql_prepare.cc b/sql/sql_prepare.cc index ca1b29ea57d..e3d59060b86 100644 --- a/sql/sql_prepare.cc +++ b/sql/sql_prepare.cc @@ -1492,8 +1492,21 @@ static bool mysql_test_create_table(Prepared_statement *stmt) if (select_lex->item_list.elements) { + if (!(lex->create_info.options & HA_LEX_CREATE_TMP_TABLE)) + { + lex->link_first_table_back(create_table, link_to_local); + create_table->create= TRUE; + } + + if (open_normal_and_derived_tables(stmt->thd, lex->query_tables, 0)) + DBUG_RETURN(TRUE); + + if (!(lex->create_info.options & HA_LEX_CREATE_TMP_TABLE)) + create_table= lex->unlink_first_table(&link_to_local); + select_lex->context.resolve_in_select_list= TRUE; - res= select_like_stmt_test_with_open(stmt, tables, 0, 0); + + res= select_like_stmt_test(stmt, 0, 0); } /* put tables back for PS rexecuting */ diff --git a/sql/sql_table.cc b/sql/sql_table.cc index 1047b1ec35d..6f59e326814 100644 --- a/sql/sql_table.cc +++ b/sql/sql_table.cc @@ -3196,7 +3196,7 @@ static HA_CREATE_INFO *copy_create_info(HA_CREATE_INFO *lex_create_info) Create a table SYNOPSIS - mysql_create_table_internal() + mysql_create_table_no_lock() thd Thread object db Database table_name Table name @@ -3213,6 +3213,11 @@ static HA_CREATE_INFO *copy_create_info(HA_CREATE_INFO *lex_create_info) DESCRIPTION If one creates a temporary table, this is automatically opened + Note that this function assumes that caller already have taken + name-lock on table being created or used some other way to ensure + that concurrent operations won't intervene. mysql_create_table() + is a wrapper that can be used for this. + no_log is needed for the case of CREATE ... SELECT, as the logging will be done later in sql_insert.cc select_field_count is also used for CREATE ... SELECT, @@ -3223,7 +3228,7 @@ static HA_CREATE_INFO *copy_create_info(HA_CREATE_INFO *lex_create_info) TRUE error */ -bool mysql_create_table_internal(THD *thd, +bool mysql_create_table_no_lock(THD *thd, const char *db, const char *table_name, HA_CREATE_INFO *lex_create_info, List &fields, @@ -3239,7 +3244,7 @@ bool mysql_create_table_internal(THD *thd, HA_CREATE_INFO *create_info; handler *file; bool error= TRUE; - DBUG_ENTER("mysql_create_table_internal"); + DBUG_ENTER("mysql_create_table_no_lock"); DBUG_PRINT("enter", ("db: '%s' table: '%s' tmp: %d", db, table_name, internal_tmp_table)); @@ -3581,7 +3586,7 @@ warn: /* - Database locking aware wrapper for mysql_create_table_internal(), + Database and name-locking aware wrapper for mysql_create_table_no_lock(), */ bool mysql_create_table(THD *thd, const char *db, const char *table_name, @@ -3591,6 +3596,7 @@ bool mysql_create_table(THD *thd, const char *db, const char *table_name, uint select_field_count, bool use_copy_create_info) { + TABLE *name_lock= 0; bool result; DBUG_ENTER("mysql_create_table"); @@ -3611,11 +3617,44 @@ bool mysql_create_table(THD *thd, const char *db, const char *table_name, creating_table++; pthread_mutex_unlock(&LOCK_lock_db); - result= mysql_create_table_internal(thd, db, table_name, create_info, - fields, keys, internal_tmp_table, - select_field_count, - use_copy_create_info); + if (!(create_info->options & HA_LEX_CREATE_TMP_TABLE)) + { + if (lock_table_name_if_not_cached(thd, db, table_name, &name_lock)) + { + result= TRUE; + goto unlock; + } + if (!name_lock) + { + if (create_info->options & HA_LEX_CREATE_IF_NOT_EXISTS) + { + push_warning_printf(thd, MYSQL_ERROR::WARN_LEVEL_NOTE, + ER_TABLE_EXISTS_ERROR, ER(ER_TABLE_EXISTS_ERROR), + table_name); + create_info->table_existed= 1; + result= FALSE; + } + else + { + my_error(ER_TABLE_EXISTS_ERROR,MYF(0),table_name); + result= TRUE; + } + goto unlock; + } + } + result= mysql_create_table_no_lock(thd, db, table_name, create_info, + fields, keys, internal_tmp_table, + select_field_count, + use_copy_create_info); + +unlock: + if (name_lock) + { + pthread_mutex_lock(&LOCK_open); + unlink_open_table(thd, name_lock, FALSE); + pthread_mutex_unlock(&LOCK_open); + } pthread_mutex_lock(&LOCK_lock_db); if (!--creating_table && creating_database) pthread_cond_signal(&COND_refresh); @@ -3818,7 +3857,7 @@ void close_cached_table(THD *thd, TABLE *table) thd->lock=0; // Start locked threads } /* Close all copies of 'table'. This also frees all LOCK TABLES lock */ - thd->open_tables=unlink_open_table(thd,thd->open_tables,table); + unlink_open_table(thd, table, TRUE); /* When lock on LOCK_open is freed other threads can continue */ broadcast_refresh(); @@ -3894,7 +3933,7 @@ static int prepare_for_restore(THD* thd, TABLE_LIST* table, to finish the restore in the handler later on */ pthread_mutex_lock(&LOCK_open); - if (reopen_name_locked_table(thd, table)) + if (reopen_name_locked_table(thd, table, TRUE)) { unlock_table_name(thd, table); pthread_mutex_unlock(&LOCK_open); @@ -4026,7 +4065,7 @@ static int prepare_for_repair(THD *thd, TABLE_LIST *table_list, to finish the repair in the handler later on. */ pthread_mutex_lock(&LOCK_open); - if (reopen_name_locked_table(thd, table_list)) + if (reopen_name_locked_table(thd, table_list, TRUE)) { unlock_table_name(thd, table_list); pthread_mutex_unlock(&LOCK_open); @@ -4632,7 +4671,7 @@ bool mysql_create_like_table(THD* thd, TABLE_LIST* table, HA_CREATE_INFO *lex_create_info, Table_ident *table_ident) { - TABLE *tmp_table; + TABLE *tmp_table, *name_lock= 0; char src_path[FN_REFLEN], dst_path[FN_REFLEN]; char src_table_name_buff[FN_REFLEN], src_db_name_buff[FN_REFLEN]; uint dst_path_length; @@ -4641,14 +4680,14 @@ bool mysql_create_like_table(THD* thd, TABLE_LIST* table, char *src_db; char *src_table= table_ident->table.str; int err; - bool res= TRUE, unlock_dst_table= FALSE; + bool res= TRUE; enum legacy_db_type not_used; HA_CREATE_INFO *create_info; #ifdef WITH_PARTITION_STORAGE_ENGINE char tmp_path[FN_REFLEN]; #endif char ts_name[FN_LEN]; - TABLE_LIST src_tables_list, dst_tables_list; + TABLE_LIST src_tables_list; DBUG_ENTER("mysql_create_like_table"); if (!(create_info= copy_create_info(lex_create_info))) @@ -4756,6 +4795,10 @@ bool mysql_create_like_table(THD* thd, TABLE_LIST* table, } else { + if (lock_table_name_if_not_cached(thd, db, table_name, &name_lock)) + goto err; + if (!name_lock) + goto table_exists; dst_path_length= build_table_filename(dst_path, sizeof(dst_path), db, table_name, reg_ext, 0); if (!access(dst_path, F_OK)) @@ -4843,28 +4886,21 @@ bool mysql_create_like_table(THD* thd, TABLE_LIST* table, char buf[2048]; String query(buf, sizeof(buf), system_charset_info); query.length(0); // Have to zero it since constructor doesn't - uint counter; /* - Here we open the destination table. This is needed for - store_create_info() to work. The table will be closed - by close_thread_tables() at the end of the statement. + Here we open the destination table, on which we already have + name-lock. This is needed for store_create_info() to work. + The table will be closed by unlink_open_table() at the end + of this function. */ - if (open_tables(thd, &table, &counter, 0)) + table->table= name_lock; + VOID(pthread_mutex_lock(&LOCK_open)); + if (reopen_name_locked_table(thd, table, FALSE)) + { + VOID(pthread_mutex_unlock(&LOCK_open)); goto err; - - bzero((gptr)&dst_tables_list, sizeof(dst_tables_list)); - dst_tables_list.db= table->db; - dst_tables_list.table_name= table->table_name; - - /* - lock destination table name, to make sure that nobody - can drop/alter the table while we execute store_create_info() - */ - if (lock_and_wait_for_table_name(thd, &dst_tables_list)) - goto err; - else - unlock_dst_table= TRUE; + } + VOID(pthread_mutex_unlock(&LOCK_open)); IF_DBUG(int result=) store_create_info(thd, table, &query, create_info); @@ -4899,10 +4935,10 @@ table_exists: my_error(ER_TABLE_EXISTS_ERROR, MYF(0), table_name); err: - if (unlock_dst_table) + if (name_lock) { pthread_mutex_lock(&LOCK_open); - unlock_table_name(thd, &dst_tables_list); + unlink_open_table(thd, name_lock, FALSE); pthread_mutex_unlock(&LOCK_open); } DBUG_RETURN(res); @@ -5349,7 +5385,7 @@ bool mysql_alter_table(THD *thd,char *new_db, char *new_name, uint order_num, ORDER *order, bool ignore, ALTER_INFO *alter_info, bool do_send_ok) { - TABLE *table,*new_table=0; + TABLE *table, *new_table= 0, *name_lock= 0; int error= 0; char tmp_name[80],old_name[32],new_name_buff[FN_REFLEN]; char new_alias_buff[FN_REFLEN], *table_name, *db, *new_alias, *alias; @@ -5486,6 +5522,18 @@ view_err: DBUG_RETURN(TRUE); table->use_all_columns(); + List_iterator drop_it(alter_info->drop_list); + List_iterator def_it(fields); + List_iterator alter_it(alter_info->alter_list); + List create_list; // Add new fields here + List key_list; // Add new keys here + List_iterator find_it(create_list); + List_iterator key_it(keys); + List_iterator field_it(create_list); + List key_parts; + + KEY *key_info=table->key_info; + /* Check that we are not trying to rename to an existing table */ if (new_name) { @@ -5522,13 +5570,21 @@ view_err: } else { + if (lock_table_name_if_not_cached(thd, new_db, new_name, &name_lock)) + DBUG_RETURN(TRUE); + if (!name_lock) + { + my_error(ER_TABLE_EXISTS_ERROR, MYF(0), new_alias); + DBUG_RETURN(TRUE); + } + build_table_filename(new_name_buff, sizeof(new_name_buff), new_db, new_name_buff, reg_ext, 0); if (!access(new_name_buff, F_OK)) { /* Table will be closed in do_command() */ my_error(ER_TABLE_EXISTS_ERROR, MYF(0), new_alias); - DBUG_RETURN(TRUE); + goto err; } } } @@ -5563,12 +5619,10 @@ view_err: #ifdef WITH_PARTITION_STORAGE_ENGINE if (prep_alter_part_table(thd, table, alter_info, create_info, old_db_type, &partition_changed, &fast_alter_partition)) - { - DBUG_RETURN(TRUE); - } + goto err; #endif if (check_engine(thd, new_name, create_info)) - DBUG_RETURN(TRUE); + goto err; new_db_type= create_info->db_type; if (create_info->row_type == ROW_TYPE_NOT_USED) create_info->row_type= table->s->row_type; @@ -5581,7 +5635,7 @@ view_err: { DBUG_PRINT("info", ("doesn't support alter")); my_error(ER_ILLEGAL_HA, MYF(0), table_name); - DBUG_RETURN(TRUE); + goto err; } thd->proc_info="setup"; @@ -5640,7 +5694,19 @@ view_err: if (!error && (new_name != table_name || new_db != db)) { thd->proc_info="rename"; - /* Then do a 'simple' rename of the table */ + /* + Then do a 'simple' rename of the table. First we need to close all + instances of 'source' table. + */ + close_cached_table(thd, table); + /* + Then, we want check once again that target table does not exist. + Actually the order of these two steps does not matter since + earlier we took name-lock on the target table, so we do them + in this particular order only to be consistent with 5.0, in which + we don't take this name-lock and where this order really matters. + TODO: Investigate if we need this access() check at all. + */ if (!access(new_name_buff,F_OK)) { my_error(ER_TABLE_EXISTS_ERROR, MYF(0), new_name); @@ -5649,8 +5715,6 @@ view_err: else { *fn_ext(new_name)=0; - table->s->version= 0; // Force removal of table def - close_cached_table(thd, table); if (mysql_rename_table(old_db_type,db,table_name,new_db,new_alias, 0)) error= -1; else if (Table_triggers_list::change_table_name(thd, db, table_name, @@ -5682,6 +5746,8 @@ view_err: table->file->print_error(error, MYF(0)); error= -1; } + if (name_lock) + unlink_open_table(thd, name_lock, FALSE); VOID(pthread_mutex_unlock(&LOCK_open)); table_list->table= NULL; // For query cache query_cache_invalidate3(thd, table_list, 0); @@ -5718,11 +5784,6 @@ view_err: create_info->tablespace= tablespace; } restore_record(table, s->default_values); // Empty record for DEFAULT - List_iterator drop_it(alter_info->drop_list); - List_iterator def_it(fields); - List_iterator alter_it(alter_info->alter_list); - List create_list; // Add new fields here - List key_list; // Add new keys here create_field *def; /* @@ -5793,7 +5854,7 @@ view_err: if (def->sql_type == MYSQL_TYPE_BLOB) { my_error(ER_BLOB_CANT_HAVE_DEFAULT, MYF(0), def->change); - DBUG_RETURN(TRUE); + goto err; } if ((def->def=alter->def)) // Use new default def->flags&= ~NO_DEFAULT_VALUE_FLAG; @@ -5804,13 +5865,12 @@ view_err: } } def_it.rewind(); - List_iterator find_it(create_list); while ((def=def_it++)) // Add new columns { if (def->change && ! def->field) { my_error(ER_BAD_FIELD_ERROR, MYF(0), def->change, table_name); - DBUG_RETURN(TRUE); + goto err; } if (!def->after) create_list.push_back(def); @@ -5828,7 +5888,7 @@ view_err: if (!find) { my_error(ER_BAD_FIELD_ERROR, MYF(0), def->after, table_name); - DBUG_RETURN(TRUE); + goto err; } find_it.after(def); // Put element after this } @@ -5837,13 +5897,13 @@ view_err: { my_error(ER_BAD_FIELD_ERROR, MYF(0), alter_info->alter_list.head()->name, table_name); - DBUG_RETURN(TRUE); + goto err; } if (!create_list.elements) { my_message(ER_CANT_REMOVE_ALL_FIELDS, ER(ER_CANT_REMOVE_ALL_FIELDS), MYF(0)); - DBUG_RETURN(TRUE); + goto err; } /* @@ -5851,11 +5911,6 @@ view_err: for which some fields exists. */ - List_iterator key_it(keys); - List_iterator field_it(create_list); - List key_parts; - - KEY *key_info=table->key_info; for (uint i=0 ; i < table->s->keys ; i++,key_info++) { char *key_name= key_info->name; @@ -5959,7 +6014,7 @@ view_err: !my_strcasecmp(system_charset_info,key->name,primary_key_name)) { my_error(ER_WRONG_NAME_FOR_INDEX, MYF(0), key->name); - DBUG_RETURN(TRUE); + goto err; } } } @@ -6201,6 +6256,7 @@ view_err: #ifdef WITH_PARTITION_STORAGE_ENGINE if (fast_alter_partition) { + DBUG_ASSERT(!name_lock); DBUG_RETURN(fast_alter_partition_table(thd, table, alter_info, create_info, table_list, &create_list, &key_list, @@ -6261,11 +6317,12 @@ view_err: We don't log the statement, it will be logged later. */ tmp_disable_binlog(thd); - error= mysql_create_table(thd, new_db, tmp_name, - create_info,create_list,key_list,1,0,0); + error= mysql_create_table_no_lock(thd, new_db, tmp_name, + create_info, create_list, + key_list, 1, 0, 0); reenable_binlog(thd); if (error) - DBUG_RETURN(error); + goto err; /* Open the table if we need to copy the data. */ if (need_copy_table) @@ -6526,17 +6583,6 @@ view_err: current_pid, thd->thread_id); if (lower_case_table_names) my_casedn_str(files_charset_info, old_name); - if (new_name != table_name || new_db != db) - { - if (!access(new_name_buff,F_OK)) - { - error=1; - my_error(ER_TABLE_EXISTS_ERROR, MYF(0), new_name_buff); - VOID(quick_rm_table(new_db_type, new_db, tmp_name, FN_IS_TMP)); - VOID(pthread_mutex_unlock(&LOCK_open)); - goto err; - } - } #if !defined( __WIN__) if (table->file->has_transactions()) @@ -6546,7 +6592,6 @@ view_err: Win32 and InnoDB can't drop a table that is in use, so we must close the original table before doing the rename */ - table->s->version= 0; // Force removal of table def close_cached_table(thd, table); table=0; // Marker that table is closed no_table_reopen= TRUE; @@ -6556,6 +6601,21 @@ view_err: table->file->extra(HA_EXTRA_FORCE_REOPEN); // Don't use this file anymore #endif + if (new_name != table_name || new_db != db) + { + /* + Check that there is no table with target name. See the + comment describing code for 'simple' ALTER TABLE ... RENAME. + */ + if (!access(new_name_buff,F_OK)) + { + error=1; + my_error(ER_TABLE_EXISTS_ERROR, MYF(0), new_name_buff); + VOID(quick_rm_table(new_db_type, new_db, tmp_name, FN_IS_TMP)); + VOID(pthread_mutex_unlock(&LOCK_open)); + goto err; + } + } error=0; save_old_db_type= old_db_type; @@ -6606,7 +6666,6 @@ view_err: */ if (table) { - table->s->version= 0; // Force removal of table def close_cached_table(thd,table); } VOID(pthread_mutex_unlock(&LOCK_open)); @@ -6647,7 +6706,6 @@ view_err: */ if (table) { - table->s->version= 0; // Force removal of table def close_cached_table(thd,table); } VOID(quick_rm_table(old_db_type, db, old_name, FN_IS_TMP)); @@ -6673,7 +6731,6 @@ view_err: { // This shouldn't happen if (table) { - table->s->version= 0; // Force removal of table def close_cached_table(thd,table); // Remove lock for table } VOID(pthread_mutex_unlock(&LOCK_open)); @@ -6730,6 +6787,13 @@ view_err: table_list->table=0; // For query cache query_cache_invalidate3(thd, table_list, 0); + if (name_lock) + { + pthread_mutex_lock(&LOCK_open); + unlink_open_table(thd, name_lock, FALSE); + pthread_mutex_unlock(&LOCK_open); + } + end_temporary: my_snprintf(tmp_name, sizeof(tmp_name), ER(ER_INSERT_INFO), (ulong) (copied + deleted), (ulong) deleted, @@ -6749,6 +6813,12 @@ err1: VOID(quick_rm_table(new_db_type, new_db, tmp_name, FN_IS_TMP)); err: + if (name_lock) + { + pthread_mutex_lock(&LOCK_open); + unlink_open_table(thd, name_lock, FALSE); + pthread_mutex_unlock(&LOCK_open); + } DBUG_RETURN(TRUE); } /* mysql_alter_table */ diff --git a/sql/sql_trigger.cc b/sql/sql_trigger.cc index 89ac444efa9..bdfcef23101 100644 --- a/sql/sql_trigger.cc +++ b/sql/sql_trigger.cc @@ -279,7 +279,7 @@ bool mysql_create_or_drop_trigger(THD *thd, TABLE_LIST *tables, bool create) /* We also don't allow creation of triggers on views. */ tables->required_type= FRMTYPE_TABLE; - if (reopen_name_locked_table(thd, tables)) + if (reopen_name_locked_table(thd, tables, TRUE)) { unlock_table_name(thd, tables); goto end; diff --git a/sql/sql_yacc.yy b/sql/sql_yacc.yy index c02d8a85d9b..03a40c3d9e2 100644 --- a/sql/sql_yacc.yy +++ b/sql/sql_yacc.yy @@ -1582,9 +1582,7 @@ create: lex->sql_command= SQLCOM_CREATE_TABLE; if (!lex->select_lex.add_table_to_list(thd, $5, NULL, TL_OPTION_UPDATING, - (using_update_log ? - TL_READ_NO_INSERT: - TL_READ))) + TL_WRITE)) MYSQL_YYABORT; lex->create_list.empty(); lex->key_list.empty(); diff --git a/sql/table.h b/sql/table.h index 5b6aa2a8a7c..685cb67dfbc 100644 --- a/sql/table.h +++ b/sql/table.h @@ -299,6 +299,8 @@ typedef struct st_table_share } TABLE_SHARE; +extern ulong refresh_version; + /* Information for one open table */ enum index_hint_type { @@ -314,8 +316,8 @@ struct st_table { handler *file; #ifdef NOT_YET struct st_table *used_next, **used_prev; /* Link to used tables */ -#endif struct st_table *open_next, **open_prev; /* Link to open tables */ +#endif struct st_table *next, *prev; THD *in_use; /* Which thread uses this */ @@ -431,7 +433,24 @@ struct st_table { my_bool force_index; my_bool distinct,const_table,no_rows; my_bool key_read, no_keyread; - my_bool locked_by_flush; + /* + Placeholder for an open table which prevents other connections + from taking name-locks on this table. Typically used with + TABLE_SHARE::version member to take an exclusive name-lock on + this table name -- a name lock that not only prevents other + threads from opening the table, but also blocks other name + locks. This is achieved by: + - setting open_placeholder to 1 - this will block other name + locks, as wait_for_locked_table_name will be forced to wait, + see table_is_used for details. + - setting version to 0 - this will force other threads to close + the instance of this table and wait (this is the same approach + as used for usual name locks). + An exclusively name-locked table currently can have no handler + object associated with it (db_stat is always 0), but please do + not rely on that. + */ + my_bool open_placeholder; my_bool locked_by_logger; my_bool no_replicate; my_bool locked_by_name; @@ -492,7 +511,13 @@ struct st_table { read_set= &def_read_set; write_set= &def_write_set; } - + /* Is table open or should be treated as such by name-locking? */ + inline bool is_name_opened() { return db_stat || open_placeholder; } + /* + Is this instance of the table should be reopen or represents a name-lock? + */ + inline bool needs_reopen_or_name_lock() + { return s->version != refresh_version; } }; enum enum_schema_table_state @@ -888,6 +913,12 @@ typedef struct st_table_list used for implicit LOCK TABLES only and won't be used in real statement. */ bool prelocking_placeholder; + /* + This TABLE_LIST object corresponds to the table to be created + so it is possible that it does not exist (used in CREATE TABLE + ... SELECT implementation). + */ + bool create; enum enum_schema_table_state schema_table_state; void calc_md5(char *buffer); @@ -895,7 +926,11 @@ typedef struct st_table_list int view_check_option(THD *thd, bool ignore_failure); bool setup_underlying(THD *thd); void cleanup_items(); - bool placeholder() {return derived || view || schema_table || !table; } + bool placeholder() + { + return derived || view || schema_table || create && !table->db_stat || + !table; + } void print(THD *thd, String *str); bool check_single_table(st_table_list **table, table_map map, st_table_list *view); From 25114c7d0925500b12cb25b5f7f17b9015d31039 Mon Sep 17 00:00:00 2001 From: "dlenev@mockturtle.local" <> Date: Sat, 12 May 2007 00:03:50 +0400 Subject: [PATCH 17/29] Added missing DBUG_VOID_RETURN to the sp_head::init_sp_name() method. --- sql/sp_head.cc | 2 ++ 1 file changed, 2 insertions(+) diff --git a/sql/sp_head.cc b/sql/sp_head.cc index 5a1faf50296..31388e0e19c 100644 --- a/sql/sp_head.cc +++ b/sql/sp_head.cc @@ -512,6 +512,8 @@ sp_head::init_sp_name(THD *thd, sp_name *spname) m_qname.length= spname->m_qname.length; m_qname.str= strmake_root(thd->mem_root, spname->m_qname.str, m_qname.length); + + DBUG_VOID_RETURN; } From 162e9b98666817190e479401b173013aa646eb27 Mon Sep 17 00:00:00 2001 From: "kostja@vajra.(none)" <> Date: Mon, 14 May 2007 10:56:23 +0400 Subject: [PATCH 18/29] Fix a warning. --- sql/sql_insert.cc | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/sql/sql_insert.cc b/sql/sql_insert.cc index febc7883f62..26caf059e73 100644 --- a/sql/sql_insert.cc +++ b/sql/sql_insert.cc @@ -483,11 +483,6 @@ bool mysql_insert(THD *thd,TABLE_LIST *table_list, bool ignore) { int error, res; - /* - log_on is about delayed inserts only. - By default, both logs are enabled (this won't cause problems if the server - runs without --log-update or --log-bin). - */ bool transactional_table, joins_freed= FALSE; bool changed; uint value_count; @@ -501,9 +496,14 @@ bool mysql_insert(THD *thd,TABLE_LIST *table_list, Name_resolution_context_state ctx_state; #ifndef EMBEDDED_LIBRARY char *query= thd->query; -#endif + /* + log_on is about delayed inserts only. + By default, both logs are enabled (this won't cause problems if the server + runs without --log-update or --log-bin). + */ bool log_on= (thd->options & OPTION_BIN_LOG) || (!(thd->security_ctx->master_access & SUPER_ACL)); +#endif thr_lock_type lock_type = table_list->lock_type; Item *unused_conds= 0; DBUG_ENTER("mysql_insert"); From d173f2a8ff6467c7068404c81ab0928898a143ee Mon Sep 17 00:00:00 2001 From: "anozdrin/alik@ibm." <> Date: Mon, 14 May 2007 13:02:12 +0400 Subject: [PATCH 19/29] Disable random-failing IM tests. --- mysql-test/t/disabled.def | 1 + 1 file changed, 1 insertion(+) diff --git a/mysql-test/t/disabled.def b/mysql-test/t/disabled.def index df56165950f..7311aabd2a6 100644 --- a/mysql-test/t/disabled.def +++ b/mysql-test/t/disabled.def @@ -12,3 +12,4 @@ ndb_load : Bug#17233 user_limits : Bug#23921 random failure of user_limits.test +im_daemon_life_cycle : IM tests fail randomly From 0c4834b1fa53d855860abe8e7a4bcc3317b779ba Mon Sep 17 00:00:00 2001 From: "anozdrin/alik@ibm." <> Date: Mon, 14 May 2007 13:04:59 +0400 Subject: [PATCH 20/29] Disable random-failing IM tests. --- mysql-test/t/disabled.def | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/mysql-test/t/disabled.def b/mysql-test/t/disabled.def index f054cf26813..56aa2f4a8a6 100644 --- a/mysql-test/t/disabled.def +++ b/mysql-test/t/disabled.def @@ -11,7 +11,9 @@ ############################################################################## user_limits : Bug#23921 random failure of user_limits.test -im_options : Bug#20294 2006-07-24 stewart Instance manager test im_options fails randomly +im_options : Bug#20294 2006-07-24 stewart Instance manager test im_options fails randomly +im_daemon_life_cycle : IM tests fail randomly +im_cmd_line.imtest : IM tests fail randomly im_life_cycle : BUG#27851 Instance manager dies on ASSERT in ~Thread_registry() or from not being able to close a mysqld instance. concurrent_innodb : BUG#21579 2006-08-11 mleich innodb_concurrent random failures with varying differences ndb_autodiscover : BUG#18952 2006-02-16 jmiller Needs to be fixed w.r.t binlog From 6ab5b02c35295145285c7b338d90a544db6f2bc2 Mon Sep 17 00:00:00 2001 From: "anozdrin/alik@ibm." <> Date: Mon, 14 May 2007 13:20:11 +0400 Subject: [PATCH 21/29] Update description. --- mysql-test/t/disabled.def | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mysql-test/t/disabled.def b/mysql-test/t/disabled.def index 7311aabd2a6..aeeab6ee6d4 100644 --- a/mysql-test/t/disabled.def +++ b/mysql-test/t/disabled.def @@ -12,4 +12,4 @@ ndb_load : Bug#17233 user_limits : Bug#23921 random failure of user_limits.test -im_daemon_life_cycle : IM tests fail randomly +im_daemon_life_cycle : Bug#20294: Instance manager tests fail randomly From aff907986b3a2f40e009c08d988ea61625505f9d Mon Sep 17 00:00:00 2001 From: "anozdrin/alik@ibm." <> Date: Mon, 14 May 2007 13:22:03 +0400 Subject: [PATCH 22/29] Update description. --- mysql-test/t/disabled.def | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mysql-test/t/disabled.def b/mysql-test/t/disabled.def index 56aa2f4a8a6..e3d72f1f7e0 100644 --- a/mysql-test/t/disabled.def +++ b/mysql-test/t/disabled.def @@ -12,8 +12,8 @@ user_limits : Bug#23921 random failure of user_limits.test im_options : Bug#20294 2006-07-24 stewart Instance manager test im_options fails randomly -im_daemon_life_cycle : IM tests fail randomly -im_cmd_line.imtest : IM tests fail randomly +im_daemon_life_cycle : Bug#20294 2007-05-14 alik Instance manager tests fail randomly +im_cmd_line : Bug#20294 2007-05-14 alik Instance manager tests fail randomly im_life_cycle : BUG#27851 Instance manager dies on ASSERT in ~Thread_registry() or from not being able to close a mysqld instance. concurrent_innodb : BUG#21579 2006-08-11 mleich innodb_concurrent random failures with varying differences ndb_autodiscover : BUG#18952 2006-02-16 jmiller Needs to be fixed w.r.t binlog From dcf7be8cf3e85f68fcf94d8bca83f426cc5fa831 Mon Sep 17 00:00:00 2001 From: "anozdrin/alik@ibm." <> Date: Mon, 14 May 2007 19:25:03 +0400 Subject: [PATCH 23/29] Disable im_life_cycle in 5.0. --- mysql-test/t/disabled.def | 1 + 1 file changed, 1 insertion(+) diff --git a/mysql-test/t/disabled.def b/mysql-test/t/disabled.def index aeeab6ee6d4..85685234de9 100644 --- a/mysql-test/t/disabled.def +++ b/mysql-test/t/disabled.def @@ -12,4 +12,5 @@ ndb_load : Bug#17233 user_limits : Bug#23921 random failure of user_limits.test +im_life_cycle : Bug#27851: Instance manager test im_life_cycle fails randomly im_daemon_life_cycle : Bug#20294: Instance manager tests fail randomly From d748e4dedd9329c2cdae674df69d9276a5157587 Mon Sep 17 00:00:00 2001 From: "dlenev@mockturtle.local" <> Date: Mon, 14 May 2007 22:38:26 +0400 Subject: [PATCH 24/29] Fix for bug #28415 "Some ALTER TABLE statements no longer work under LOCK TABLES" and failures of alter_table.test on Windows which occured after pushing fix for bugs #20662, #20903, #24508, #24738 (various problems with CREATE TABLE SELECT). ALTER TABLE statements which were handled using "fast" alter table optimization were not properly working under LOCK TABLES if table was transactional (for all table types under Windows). Code implementing "fast" version of ALTER TABLE tried to open and lock table using open_ltable() after renaming .FRM files (which corresponds to renaming tables in normal case) in some cases (for transactional tables or on Windows). This caused problems under LOCK TABLES and conflicted with name-lock taken by ALTER TABLE RENAME on target tables. This patch solves this issue by using reopen_name_locked_table() instead of open_ltable(). --- mysql-test/include/mix1.inc | 20 +++++++++++++++ mysql-test/r/innodb_mysql.result | 11 ++++++++ sql/sql_table.cc | 44 +++++++++++++++++++++++++++++--- 3 files changed, 71 insertions(+), 4 deletions(-) diff --git a/mysql-test/include/mix1.inc b/mysql-test/include/mix1.inc index 352d5a987a2..fce37eef187 100644 --- a/mysql-test/include/mix1.inc +++ b/mysql-test/include/mix1.inc @@ -688,4 +688,24 @@ select * from t2; drop table t2; + +# +# Tests for bug #28415 "Some ALTER TABLE statements no longer work +# under LOCK TABLES" and some aspects of fast ALTER TABLE behaviour +# for transactional tables. +# +--disable_warnings +drop table if exists t1, t2; +--enable_warnings +create table t1 (i int); +alter table t1 modify i int default 1; +alter table t1 modify i int default 2, rename t2; +lock table t2 write; +alter table t2 modify i int default 3; +unlock tables; +lock table t2 write; +alter table t2 modify i int default 4, rename t1; +unlock tables; +drop table t1; + --echo End of 5.1 tests diff --git a/mysql-test/r/innodb_mysql.result b/mysql-test/r/innodb_mysql.result index e51318af827..074efc30838 100644 --- a/mysql-test/r/innodb_mysql.result +++ b/mysql-test/r/innodb_mysql.result @@ -733,4 +733,15 @@ k a c 11 15 1 12 20 1 drop table t2; +drop table if exists t1, t2; +create table t1 (i int); +alter table t1 modify i int default 1; +alter table t1 modify i int default 2, rename t2; +lock table t2 write; +alter table t2 modify i int default 3; +unlock tables; +lock table t2 write; +alter table t2 modify i int default 4, rename t1; +unlock tables; +drop table t1; End of 5.1 tests diff --git a/sql/sql_table.cc b/sql/sql_table.cc index 6f59e326814..23214c7c8a7 100644 --- a/sql/sql_table.cc +++ b/sql/sql_table.cc @@ -6673,6 +6673,7 @@ view_err: } if (! need_copy_table) { + bool needs_unlink= FALSE; if (! table) { if (new_name != table_name || new_db != db) @@ -6683,11 +6684,41 @@ view_err: table_list->db= new_db; table_list->db_length= strlen(new_db); } - - VOID(pthread_mutex_unlock(&LOCK_open)); - if (! (table= open_ltable(thd, table_list, TL_WRITE_ALLOW_READ))) + else + { + /* + TODO: Creation of name-lock placeholder here is a temporary + work-around. Long term we should change close_cached_table() call + which we invoke before table renaming operation in such way that + it will leave placeholders for table in table cache/THD::open_tables + list. By doing this we will be able easily reopen and relock these + tables later and therefore behave under LOCK TABLES in the same way + on all platforms. + */ + char key[MAX_DBKEY_LENGTH]; + uint key_length; + key_length= create_table_def_key(thd, key, table_list, 0); + if (!(name_lock= table_cache_insert_placeholder(thd, key, + key_length))) + { + VOID(pthread_mutex_unlock(&LOCK_open)); + goto err; + } + name_lock->next= thd->open_tables; + thd->open_tables= name_lock; + } + table_list->table= name_lock; + if (reopen_name_locked_table(thd, table_list, FALSE)) + { + VOID(pthread_mutex_unlock(&LOCK_open)); goto err; - VOID(pthread_mutex_lock(&LOCK_open)); + } + table= table_list->table; + /* + We can't rely on later close_cached_table() calls to close + this instance of the table since it was not properly locked. + */ + needs_unlink= TRUE; } /* Tell the handler that a new frm file is in place. */ if (table->file->create_handler_files(path, NULL, CHF_INDEX_FLAG, @@ -6696,6 +6727,11 @@ view_err: VOID(pthread_mutex_unlock(&LOCK_open)); goto err; } + if (needs_unlink) + { + unlink_open_table(thd, table, FALSE); + table= name_lock= 0; + } } if (thd->lock || new_name != table_name || no_table_reopen) // True if WIN32 From 5e184e8eb1fc6d990de8ae8189455af9b9fe570d Mon Sep 17 00:00:00 2001 From: "kostja@vajra.(none)" <> Date: Tue, 15 May 2007 17:58:26 +0400 Subject: [PATCH 25/29] A post-merge fix. --- sql/sql_base.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sql/sql_base.cc b/sql/sql_base.cc index 54581ac484c..25437699c21 100644 --- a/sql/sql_base.cc +++ b/sql/sql_base.cc @@ -1871,7 +1871,7 @@ void drop_open_table(THD *thd, TABLE *table, const char *db_name, close_temporary_table(thd, table, 1, 1); else { - handlerton *table_type= table->s->db_type; + handlerton *table_type= table->s->db_type(); VOID(pthread_mutex_lock(&LOCK_open)); /* unlink_open_table() also tells threads waiting for refresh or close From 747842e10bd7b93218252907e220e6da6e5dfcbd Mon Sep 17 00:00:00 2001 From: "kostja@vajra.(none)" <> Date: Wed, 16 May 2007 09:51:05 +0400 Subject: [PATCH 26/29] A fix and a test case for Bug#21483 "Server abort or deadlock on INSERT DELAYED with another implicit insert" Also fixes and adds test cases for bugs: 20497 "Trigger with INSERT DELAYED causes Error 1165" 21714 "Wrong NEW.value and server abort on INSERT DELAYED to a table with a trigger". Post-review fixes. Problem: In MySQL INSERT DELAYED is a way to pipe all inserts into a given table through a dedicated thread. This is necessary for simplistic storage engines like MyISAM, which do not have internal concurrency control or threading and thus can not achieve efficient INSERT throughput without support from SQL layer. DELAYED INSERT works as follows: For every distinct table, which can accept DELAYED inserts and has pending data to insert, a dedicated thread is created to write data to disk. All user connection threads that attempt to delayed-insert into this table interact with the dedicated thread in producer/consumer fashion: all records to-be inserted are pushed into a queue of the dedicated thread, which fetches the records and writes them. In this design, client connection threads never open or lock the delayed insert table. This functionality was introduced in version 3.23 and does not take into account existence of triggers, views, or pre-locking. E.g. if INSERT DELAYED is called from a stored function, which, in turn, is called from another stored function that uses the delayed table, a deadlock can occur, because delayed locking by-passes pre-locking. Besides: * the delayed thread works directly with the subject table through the storage engine API and does not invoke triggers * even if it was patched to invoke triggers, if triggers, in turn, used other tables, the delayed thread would have to open and lock involved tables (use pre-locking). * even if it was patched to use pre-locking, without deadlock detection the delayed thread could easily lock out user connection threads in case when the same table is used both in a trigger and on the right side of the insert query: the delayed thread would not release locks until all inserts are complete, and user connection can not complete inserts without having locks on the tables used on the right side of the query. Solution: These considerations suggest two general alternatives for the future of INSERT DELAYED: * it is considered a full-fledged alternative to normal INSERT * it is regarded as an optimisation that is only relevant for simplistic engines. Since we missed our chance to provide complete support of new features when 5.0 was in development, the first alternative currently renders infeasible. However, even the second alternative, which is to detect new features and convert DELAYED insert into a normal insert, is not easy to implement. The catch-22 is that we don't know if the subject table has triggers or is a view before we open it, and we only open it in the delayed thread. We don't know if the query involves pre-locking until we have opened all tables, and we always first create the delayed thread, and only then open the remaining tables. This patch detects the problematic scenarios and converts DELAYED INSERT to a normal INSERT using the following approach: * if the statement is executed under pre-locking (e.g. from within a stored function or trigger) or the right side may require pre-locking, we detect the situation before creating a delayed insert thread and convert the statement to a conventional INSERT. * if the subject table is a view or has triggers, we shutdown the delayed thread and convert the statement to a conventional INSERT. --- mysql-test/r/insert.result | 116 +++++++++++++++++ mysql-test/t/insert.test | 139 ++++++++++++++++++++ sql/sp_head.cc | 8 ++ sql/sql_base.cc | 4 +- sql/sql_insert.cc | 252 +++++++++++++++++++++++++------------ sql/sql_lex.h | 6 + 6 files changed, 442 insertions(+), 83 deletions(-) diff --git a/mysql-test/r/insert.result b/mysql-test/r/insert.result index 7900e0b7695..fa6e23d09f9 100644 --- a/mysql-test/r/insert.result +++ b/mysql-test/r/insert.result @@ -346,3 +346,119 @@ f1 f2 12 NULL drop view v1; drop table t1,t2; +DROP TABLE IF EXISTS t1; +DROP FUNCTION IF EXISTS f1; +DROP FUNCTION IF EXISTS f2; +CREATE TABLE t1 (i INT); +CREATE FUNCTION f1() RETURNS INT +BEGIN +INSERT INTO t1 VALUES (1); +RETURN 1; +END | +CREATE FUNCTION f2() RETURNS INT +BEGIN +INSERT DELAYED INTO t1 VALUES (2); +RETURN 1; +END | +SELECT f1(); +f1() +1 +SELECT f2(); +f2() +1 +INSERT INTO t1 VALUES (3); +INSERT DELAYED INTO t1 VALUES (4); +INSERT INTO t1 VALUES (f1()); +ERROR HY000: Can't update table 't1' in stored function/trigger because it is already used by statement which invoked this stored function/trigger. +INSERT DELAYED INTO t1 VALUES (f1()); +ERROR HY000: Can't update table 't1' in stored function/trigger because it is already used by statement which invoked this stored function/trigger. +INSERT INTO t1 VALUES (f2()); +ERROR HY000: Can't update table 't1' in stored function/trigger because it is already used by statement which invoked this stored function/trigger. +INSERT DELAYED INTO t1 VALUES (f2()); +ERROR HY000: Can't update table 't1' in stored function/trigger because it is already used by statement which invoked this stored function/trigger. +CREATE TRIGGER t1_bi BEFORE INSERT ON t1 FOR EACH ROW +INSERT INTO t1 VALUES (NEW.i); +INSERT INTO t1 VALUES (1); +ERROR HY000: Can't update table 't1' in stored function/trigger because it is already used by statement which invoked this stored function/trigger. +INSERT DELAYED INTO t1 VALUES (1); +ERROR HY000: Can't update table 't1' in stored function/trigger because it is already used by statement which invoked this stored function/trigger. +SELECT * FROM t1; +i +1 +2 +3 +4 +DROP FUNCTION f2; +DROP FUNCTION f1; +DROP TABLE t1; +DROP TABLE IF EXISTS t1, t2; +CREATE TABLE t1 (i INT); +CREATE TABLE t2 (i INT); +CREATE TRIGGER t1_bi BEFORE INSERT ON t1 FOR EACH ROW +INSERT DELAYED INTO t2 VALUES (NEW.i); +CREATE TRIGGER t1_bu BEFORE UPDATE ON t1 FOR EACH ROW +INSERT DELAYED INTO t2 VALUES (NEW.i); +CREATE TRIGGER t1_bd BEFORE DELETE ON t1 FOR EACH ROW +INSERT DELAYED INTO t2 VALUES (OLD.i); +INSERT INTO t1 VALUES (1); +INSERT DELAYED INTO t1 VALUES (2); +SELECT * FROM t1; +i +1 +2 +UPDATE t1 SET i = 3 WHERE i = 1; +SELECT * FROM t1; +i +3 +2 +DELETE FROM t1 WHERE i = 3; +SELECT * FROM t1; +i +2 +SELECT * FROM t2; +i +1 +2 +3 +3 +DROP TABLE t1, t2; +DROP TABLE IF EXISTS t1, t2; +CREATE TABLE t1 (i INT); +CREATE TRIGGER t1_bi BEFORE INSERT ON t1 FOR EACH ROW +SET @a= NEW.i; +SET @a= 0; +INSERT DELAYED INTO t1 VALUES (1); +SELECT @a; +@a +1 +INSERT DELAYED INTO t1 VALUES (2); +SELECT @a; +@a +2 +DROP TABLE t1; +CREATE TABLE t1 (i INT); +CREATE TABLE t2 (i INT); +CREATE TRIGGER t1_ai AFTER INSERT ON t1 FOR EACH ROW +INSERT INTO t2 VALUES (NEW.i); +CREATE TRIGGER t1_au AFTER UPDATE ON t1 FOR EACH ROW +INSERT DELAYED INTO t2 VALUES (NEW.i); +CREATE TRIGGER t1_ad AFTER DELETE ON t1 FOR EACH ROW +INSERT DELAYED INTO t2 VALUES (OLD.i); +INSERT DELAYED INTO t1 VALUES (1); +SELECT * FROM t1; +i +1 +UPDATE t1 SET i = 2 WHERE i = 1; +SELECT * FROM t1; +i +2 +DELETE FROM t1 WHERE i = 2; +SELECT * FROM t1; +i +SELECT * FROM t2; +i +1 +2 +2 +DROP TABLE t1, t2; +End of 5.0 tests. diff --git a/mysql-test/t/insert.test b/mysql-test/t/insert.test index 0a8e184ea5c..76177403bd0 100644 --- a/mysql-test/t/insert.test +++ b/mysql-test/t/insert.test @@ -216,3 +216,142 @@ select * from t1; drop view v1; drop table t1,t2; + +# +# BUG#21483: Server abort or deadlock on INSERT DELAYED with another +# implicit insert +# +# The solution is to downgrade INSERT DELAYED to normal INSERT if the +# statement uses functions and access tables or triggers, or is called +# from a function or a trigger. +# +--disable_warnings +DROP TABLE IF EXISTS t1; +DROP FUNCTION IF EXISTS f1; +DROP FUNCTION IF EXISTS f2; +--enable_warnings + +CREATE TABLE t1 (i INT); +delimiter |; +CREATE FUNCTION f1() RETURNS INT +BEGIN + INSERT INTO t1 VALUES (1); + RETURN 1; +END | +CREATE FUNCTION f2() RETURNS INT +BEGIN + INSERT DELAYED INTO t1 VALUES (2); + RETURN 1; +END | +delimiter ;| + +SELECT f1(); +SELECT f2(); +INSERT INTO t1 VALUES (3); +INSERT DELAYED INTO t1 VALUES (4); + +--error ER_CANT_UPDATE_USED_TABLE_IN_SF_OR_TRG +INSERT INTO t1 VALUES (f1()); + +--error ER_CANT_UPDATE_USED_TABLE_IN_SF_OR_TRG +INSERT DELAYED INTO t1 VALUES (f1()); + +--error ER_CANT_UPDATE_USED_TABLE_IN_SF_OR_TRG +INSERT INTO t1 VALUES (f2()); + +--error ER_CANT_UPDATE_USED_TABLE_IN_SF_OR_TRG +INSERT DELAYED INTO t1 VALUES (f2()); + +CREATE TRIGGER t1_bi BEFORE INSERT ON t1 FOR EACH ROW + INSERT INTO t1 VALUES (NEW.i); + +--error ER_CANT_UPDATE_USED_TABLE_IN_SF_OR_TRG +INSERT INTO t1 VALUES (1); + +--error ER_CANT_UPDATE_USED_TABLE_IN_SF_OR_TRG +INSERT DELAYED INTO t1 VALUES (1); + +SELECT * FROM t1; + +DROP FUNCTION f2; +DROP FUNCTION f1; +DROP TABLE t1; + +# +# BUG#20497: Trigger with INSERT DELAYED causes Error 1165 +# +# Fixed by the patch for Bug#21483 +# +--disable_warnings +DROP TABLE IF EXISTS t1, t2; +--enable_warnings + +CREATE TABLE t1 (i INT); +CREATE TABLE t2 (i INT); + +CREATE TRIGGER t1_bi BEFORE INSERT ON t1 FOR EACH ROW + INSERT DELAYED INTO t2 VALUES (NEW.i); + +CREATE TRIGGER t1_bu BEFORE UPDATE ON t1 FOR EACH ROW + INSERT DELAYED INTO t2 VALUES (NEW.i); + +CREATE TRIGGER t1_bd BEFORE DELETE ON t1 FOR EACH ROW + INSERT DELAYED INTO t2 VALUES (OLD.i); + +INSERT INTO t1 VALUES (1); +INSERT DELAYED INTO t1 VALUES (2); +SELECT * FROM t1; +UPDATE t1 SET i = 3 WHERE i = 1; +SELECT * FROM t1; +DELETE FROM t1 WHERE i = 3; +SELECT * FROM t1; +SELECT * FROM t2; + +DROP TABLE t1, t2; + +# +# BUG#21714: Wrong NEW.value and server abort on INSERT DELAYED to a +# table with a trigger +# +# Fixed by the patch for Bug#21483 +# +--disable_warnings +DROP TABLE IF EXISTS t1, t2; +--enable_warnings + +CREATE TABLE t1 (i INT); +CREATE TRIGGER t1_bi BEFORE INSERT ON t1 FOR EACH ROW + SET @a= NEW.i; + +SET @a= 0; +INSERT DELAYED INTO t1 VALUES (1); +SELECT @a; +INSERT DELAYED INTO t1 VALUES (2); +SELECT @a; + +DROP TABLE t1; + +CREATE TABLE t1 (i INT); +CREATE TABLE t2 (i INT); + +CREATE TRIGGER t1_ai AFTER INSERT ON t1 FOR EACH ROW + INSERT INTO t2 VALUES (NEW.i); + +CREATE TRIGGER t1_au AFTER UPDATE ON t1 FOR EACH ROW + INSERT DELAYED INTO t2 VALUES (NEW.i); + +CREATE TRIGGER t1_ad AFTER DELETE ON t1 FOR EACH ROW + INSERT DELAYED INTO t2 VALUES (OLD.i); + +INSERT DELAYED INTO t1 VALUES (1); +SELECT * FROM t1; +UPDATE t1 SET i = 2 WHERE i = 1; +SELECT * FROM t1; +DELETE FROM t1 WHERE i = 2; +SELECT * FROM t1; +SELECT * FROM t2; + +DROP TABLE t1, t2; + +--echo End of 5.0 tests. + diff --git a/sql/sp_head.cc b/sql/sp_head.cc index 5a1faf50296..b24fb01e2ff 100644 --- a/sql/sp_head.cc +++ b/sql/sp_head.cc @@ -3487,6 +3487,14 @@ sp_head::merge_table_list(THD *thd, TABLE_LIST *table, LEX *lex_for_tmp_check) tlen+= alen; tname[tlen]= '\0'; + /* + Upgrade the lock type because this table list will be used + only in pre-locked mode, in which DELAYED inserts are always + converted to normal inserts. + */ + if (table->lock_type == TL_WRITE_DELAYED) + table->lock_type= TL_WRITE; + /* We ignore alias when we check if table was already marked as temporary (and therefore should not be prelocked). Otherwise we will erroneously diff --git a/sql/sql_base.cc b/sql/sql_base.cc index 6e6611d54d2..f044a1edb01 100644 --- a/sql/sql_base.cc +++ b/sql/sql_base.cc @@ -2277,7 +2277,7 @@ int open_tables(THD *thd, TABLE_LIST **start, uint *counter, uint flags) */ if (!thd->prelocked_mode && !thd->lex->requires_prelocking() && - thd->lex->sroutines_list.elements) + thd->lex->uses_stored_routines()) { bool first_no_prelocking, need_prelocking, tabs_changed; TABLE_LIST **save_query_tables_last= thd->lex->query_tables_last; @@ -2465,7 +2465,7 @@ process_view_routines: */ if (tables->view && !thd->prelocked_mode && !thd->lex->requires_prelocking() && - tables->view->sroutines_list.elements) + tables->view->uses_stored_routines()) { /* We have at least one table in TL here. */ if (!query_tables_last_own) diff --git a/sql/sql_insert.cc b/sql/sql_insert.cc index db4c2f64d6a..d62f782683d 100644 --- a/sql/sql_insert.cc +++ b/sql/sql_insert.cc @@ -61,7 +61,7 @@ #include "slave.h" #ifndef EMBEDDED_LIBRARY -static TABLE *delayed_get_table(THD *thd,TABLE_LIST *table_list); +static bool delayed_get_table(THD *thd, TABLE_LIST *table_list); static int write_delayed(THD *thd,TABLE *table, enum_duplicates dup, bool ignore, char *query, uint query_length, bool log_on); static void end_delayed_insert(THD *thd); @@ -409,6 +409,7 @@ void mark_fields_used_by_triggers_for_insert_stmt(THD *thd, TABLE *table, downgrade the lock in handler::store_lock() method. */ +static void upgrade_lock_type(THD *thd, thr_lock_type *lock_type, enum_duplicates duplic, bool is_multi_insert) @@ -422,29 +423,37 @@ void upgrade_lock_type(THD *thd, thr_lock_type *lock_type, if (*lock_type == TL_WRITE_DELAYED) { -#ifdef EMBEDDED_LIBRARY - /* No auxiliary threads in the embedded server. */ - *lock_type= TL_WRITE; - return; -#else /* We do not use delayed threads if: - - we're running in the safe mode or skip-new - the feature - is disabled in these modes - - we're running this query in statement level replication, - on a replication slave - because we must ensure serial - execution of queries on the slave + - we're running in the safe mode or skip-new mode -- the + feature is disabled in these modes + - we're executing this statement on a replication slave -- + we need to ensure serial execution of queries on the + slave - it is INSERT .. ON DUPLICATE KEY UPDATE - in this case the insert cannot be concurrent + - this statement is directly or indirectly invoked from + a stored function or trigger (under pre-locking) - to + avoid deadlocks, since INSERT DELAYED involves a lock + upgrade (TL_WRITE_DELAYED -> TL_WRITE) which we should not + attempt while keeping other table level locks. + - this statement itself may require pre-locking. + We should upgrade the lock even though in most cases + delayed functionality may work. Unfortunately, we can't + easily identify whether the subject table is not used in + the statement indirectly via a stored function or trigger: + if it is used, that will lead to a deadlock between the + client connection and the delayed thread. */ if (specialflag & (SPECIAL_NO_NEW_FUNC | SPECIAL_SAFE_MODE) || thd->slave_thread || - thd->variables.max_insert_delayed_threads == 0) + thd->variables.max_insert_delayed_threads == 0 || + thd->prelocked_mode || + thd->lex->uses_stored_routines()) { *lock_type= TL_WRITE; return; } -#endif bool log_on= (thd->options & OPTION_BIN_LOG || ! (thd->security_ctx->master_access & SUPER_ACL)); if (log_on && mysql_bin_log.is_open() && is_multi_insert) @@ -470,6 +479,72 @@ void upgrade_lock_type(THD *thd, thr_lock_type *lock_type, } +/** + Find or create a delayed insert thread for the first table in + the table list, then open and lock the remaining tables. + If a table can not be used with insert delayed, upgrade the lock + and open and lock all tables using the standard mechanism. + + @param thd thread context + @param table_list list of "descriptors" for tables referenced + directly in statement SQL text. + The first element in the list corresponds to + the destination table for inserts, remaining + tables, if any, are usually tables referenced + by sub-queries in the right part of the + INSERT. + + @return Status of the operation. In case of success 'table' + member of every table_list element points to an instance of + class TABLE. + + @sa open_and_lock_tables for more information about MySQL table + level locking +*/ + +static +bool open_and_lock_for_insert_delayed(THD *thd, TABLE_LIST *table_list) +{ + DBUG_ENTER("open_and_lock_for_insert_delayed"); + +#ifndef EMBEDDED_LIBRARY + if (delayed_get_table(thd, table_list)) + DBUG_RETURN(TRUE); + + if (table_list->table) + { + /* + Open tables used for sub-selects or in stored functions, will also + cache these functions. + */ + if (open_and_lock_tables(thd, table_list->next_global)) + { + end_delayed_insert(thd); + DBUG_RETURN(TRUE); + } + /* + First table was not processed by open_and_lock_tables(), + we need to set updatability flag "by hand". + */ + if (!table_list->derived && !table_list->view) + table_list->updatable= 1; // usual table + DBUG_RETURN(FALSE); + } +#endif + /* + * This is embedded library and we don't have auxiliary + threads OR + * a lock upgrade was requested inside delayed_get_table + because + - there are too many delayed insert threads OR + - the table has triggers. + Use a normal insert. + */ + table_list->lock_type= TL_WRITE; + DBUG_RETURN(open_and_lock_tables(thd, table_list)); +} + + /** INSERT statement implementation */ @@ -483,11 +558,6 @@ bool mysql_insert(THD *thd,TABLE_LIST *table_list, bool ignore) { int error, res; - /* - log_on is about delayed inserts only. - By default, both logs are enabled (this won't cause problems if the server - runs without --log-update or --log-bin). - */ bool transactional_table, joins_freed= FALSE; bool changed; uint value_count; @@ -501,9 +571,14 @@ bool mysql_insert(THD *thd,TABLE_LIST *table_list, Name_resolution_context_state ctx_state; #ifndef EMBEDDED_LIBRARY char *query= thd->query; -#endif + /* + log_on is about delayed inserts only. + By default, both logs are enabled (this won't cause problems if the server + runs without --log-update or --log-bin). + */ bool log_on= (thd->options & OPTION_BIN_LOG) || (!(thd->security_ctx->master_access & SUPER_ACL)); +#endif thr_lock_type lock_type = table_list->lock_type; Item *unused_conds= 0; DBUG_ENTER("mysql_insert"); @@ -514,7 +589,6 @@ bool mysql_insert(THD *thd,TABLE_LIST *table_list, */ upgrade_lock_type(thd, &table_list->lock_type, duplic, values_list.elements > 1); - lock_type= table_list->lock_type; /* We can't write-delayed into a table locked with LOCK TABLES: @@ -522,7 +596,7 @@ bool mysql_insert(THD *thd,TABLE_LIST *table_list, never be able to get a lock on the table. QQQ: why not upgrade the lock here instead? */ - if (lock_type == TL_WRITE_DELAYED && thd->locked_tables && + if (table_list->lock_type == TL_WRITE_DELAYED && thd->locked_tables && find_locked_table(thd, table_list->db, table_list->table_name)) { my_error(ER_DELAYED_INSERT_TABLE_LOCKED, MYF(0), @@ -530,36 +604,16 @@ bool mysql_insert(THD *thd,TABLE_LIST *table_list, DBUG_RETURN(TRUE); } -#ifndef EMBEDDED_LIBRARY - if (lock_type == TL_WRITE_DELAYED) + if (table_list->lock_type == TL_WRITE_DELAYED) { - res= 1; - if ((table= delayed_get_table(thd,table_list)) && !thd->is_fatal_error) - { - /* - Open tables used for sub-selects or in stored functions, will also - cache these functions. - */ - res= open_and_lock_tables(thd, table_list->next_global); - /* - First is not processed by open_and_lock_tables() => we need set - updateability flags "by hands". - */ - if (!table_list->derived && !table_list->view) - table_list->updatable= 1; // usual table - } - else if (thd->net.last_errno != ER_WRONG_OBJECT) - { - /* Too many delayed insert threads; Use a normal insert */ - table_list->lock_type= lock_type= TL_WRITE; - res= open_and_lock_tables(thd, table_list); - } + if (open_and_lock_for_insert_delayed(thd, table_list)) + DBUG_RETURN(TRUE); } else -#endif /* EMBEDDED_LIBRARY */ - res= open_and_lock_tables(thd, table_list); - if (res || thd->is_fatal_error) - DBUG_RETURN(TRUE); + { + if (open_and_lock_tables(thd, table_list)) + DBUG_RETURN(TRUE); + } thd->proc_info="init"; thd->used_tables=0; @@ -577,6 +631,7 @@ bool mysql_insert(THD *thd,TABLE_LIST *table_list, /* mysql_prepare_insert set table_list->table if it was not set */ table= table_list->table; + lock_type= table_list->lock_type; context= &thd->lex->select_lex.context; /* @@ -1633,19 +1688,32 @@ Delayed_insert *find_handler(THD *thd, TABLE_LIST *table_list) Attempt to find or create a delayed insert thread to handle inserts into this table. - @return Return a local copy of the table in the delayed thread - @retval NULL too many delayed threads OR - this thread ran out of resources OR - a newly created delayed insert thread ran out of resources OR - the delayed insert thread failed to open the table. - In the last three cases an error is set in THD. + @return In case of success, table_list->table points to a local copy + of the delayed table or is set to NULL, which indicates a + request for lock upgrade. In case of failure, value of + table_list->table is undefined. + @retval TRUE - this thread ran out of resources OR + - a newly created delayed insert thread ran out of + resources OR + - the created thread failed to open and lock the table + (e.g. because it does not exist) OR + - the table opened in the created thread turned out to + be a view + @retval FALSE - table successfully opened OR + - too many delayed insert threads OR + - the table has triggers and we have to fall back to + a normal INSERT + Two latter cases indicate a request for lock upgrade. + + XXX: why do we regard INSERT DELAYED into a view as an error and + do not simply a lock upgrade? */ -static TABLE *delayed_get_table(THD *thd,TABLE_LIST *table_list) +static +bool delayed_get_table(THD *thd, TABLE_LIST *table_list) { int error; Delayed_insert *tmp; - TABLE *table; DBUG_ENTER("delayed_get_table"); /* Must be set in the parser */ @@ -1671,7 +1739,8 @@ static TABLE *delayed_get_table(THD *thd,TABLE_LIST *table_list) if (!(tmp=new Delayed_insert())) { my_error(ER_OUTOFMEMORY,MYF(0),sizeof(Delayed_insert)); - goto err1; + thd->fatal_error(); + goto end_create; } pthread_mutex_lock(&LOCK_thread_count); thread_count++; @@ -1680,9 +1749,10 @@ static TABLE *delayed_get_table(THD *thd,TABLE_LIST *table_list) tmp->thd.query= my_strdup(table_list->table_name,MYF(MY_WME)); if (tmp->thd.db == NULL || tmp->thd.query == NULL) { + /* The error is reported */ delete tmp; - my_message(ER_OUT_OF_RESOURCES, ER(ER_OUT_OF_RESOURCES), MYF(0)); - goto err1; + thd->fatal_error(); + goto end_create; } tmp->table_list= *table_list; // Needed to open table tmp->table_list.alias= tmp->table_list.table_name= tmp->thd.query; @@ -1698,7 +1768,8 @@ static TABLE *delayed_get_table(THD *thd,TABLE_LIST *table_list) tmp->unlock(); delete tmp; my_error(ER_CANT_CREATE_THREAD, MYF(0), error); - goto err1; + thd->fatal_error(); + goto end_create; } /* Wait until table is open */ @@ -1711,41 +1782,44 @@ static TABLE *delayed_get_table(THD *thd,TABLE_LIST *table_list) thd->proc_info="got old table"; if (tmp->thd.killed) { - if (tmp->thd.is_fatal_error) + if (tmp->thd.net.report_error) { - /* Copy error message and abort */ - thd->fatal_error(); - strmov(thd->net.last_error,tmp->thd.net.last_error); - thd->net.last_errno=tmp->thd.net.last_errno; + /* + Copy the error message. Note that we don't treat fatal + errors in the delayed thread as fatal errors in the + main thread. Use of my_message will enable stored + procedures continue handlers. + */ + my_message(tmp->thd.net.last_errno, tmp->thd.net.last_error, + MYF(0)); } tmp->unlock(); - goto err; + goto end_create; } if (thd->killed) { tmp->unlock(); - goto err; + goto end_create; } } pthread_mutex_unlock(&LOCK_delayed_create); } pthread_mutex_lock(&tmp->mutex); - table= tmp->get_local_table(thd); + table_list->table= tmp->get_local_table(thd); pthread_mutex_unlock(&tmp->mutex); - if (table) + if (table_list->table) + { + DBUG_ASSERT(tmp->thd.net.report_error == 0 && thd->net.report_error == 0); thd->di=tmp; - else if (tmp->thd.is_fatal_error) - thd->fatal_error(); + } /* Unlock the delayed insert object after its last access. */ tmp->unlock(); - DBUG_RETURN((table_list->table=table)); + DBUG_RETURN(table_list->table == NULL); - err1: - thd->fatal_error(); - err: +end_create: pthread_mutex_unlock(&LOCK_delayed_create); - DBUG_RETURN(0); // Continue with normal insert + DBUG_RETURN(thd->net.report_error); } @@ -1760,6 +1834,9 @@ static TABLE *delayed_get_table(THD *thd,TABLE_LIST *table_list) @pre This function is called from the client thread. Delayed insert thread mutex must be acquired before invoking this function. + + @return Not-NULL table object on success. NULL in case of an error, + which is set in client_thd. */ TABLE *Delayed_insert::get_local_table(THD* client_thd) @@ -1785,8 +1862,7 @@ TABLE *Delayed_insert::get_local_table(THD* client_thd) goto error; if (dead) { - strmov(client_thd->net.last_error,thd.net.last_error); - client_thd->net.last_errno=thd.net.last_errno; + my_message(thd.net.last_errno, thd.net.last_error, MYF(0)); goto error; } } @@ -1831,7 +1907,7 @@ TABLE *Delayed_insert::get_local_table(THD* client_thd) for (org_field= table->field; *org_field; org_field++, field++) { if (!(*field= (*org_field)->new_field(client_thd->mem_root, copy, 1))) - DBUG_RETURN(0); + goto error; (*field)->orig_table= copy; // Remove connection (*field)->move_field(adjust_ptrs); // Point at copy->record[0] if (*org_field == found_next_number_field) @@ -1871,8 +1947,9 @@ TABLE *Delayed_insert::get_local_table(THD* client_thd) /* Put a question in queue */ -static int write_delayed(THD *thd,TABLE *table,enum_duplicates duplic, bool ignore, - char *query, uint query_length, bool log_on) +static +int write_delayed(THD *thd,TABLE *table,enum_duplicates duplic, bool ignore, + char *query, uint query_length, bool log_on) { delayed_row *row=0; Delayed_insert *di=thd->di; @@ -1939,6 +2016,10 @@ static int write_delayed(THD *thd,TABLE *table,enum_duplicates duplic, bool igno DBUG_RETURN(1); } +/** + Signal the delayed insert thread that this user connection + is finished using it for this statement. +*/ static void end_delayed_insert(THD *thd) { @@ -2051,6 +2132,15 @@ pthread_handler_t handle_delayed_insert(void *arg) my_error(ER_ILLEGAL_HA, MYF(0), di->table_list.table_name); goto end; } + if (di->table->triggers) + { + /* + Table has triggers. This is not an error, but we do + not support triggers with delayed insert. Terminate the delayed + thread without an error and thus request lock upgrade. + */ + goto end; + } di->table->copy_blobs=1; /* One can now use this */ diff --git a/sql/sql_lex.h b/sql/sql_lex.h index d07de73d5b9..aa85711b4fd 100644 --- a/sql/sql_lex.h +++ b/sql/sql_lex.h @@ -886,6 +886,12 @@ public: query_tables_own_last= 0; } } + /** + true if the parsed tree contains references to stored procedures + or functions, false otherwise + */ + bool uses_stored_routines() const + { return sroutines_list.elements != 0; } }; From 752972b5702101c217deefe50687853e57d80f7c Mon Sep 17 00:00:00 2001 From: "kostja@vajra.(none)" <> Date: Wed, 16 May 2007 10:32:47 +0400 Subject: [PATCH 27/29] Correct a merge error. --- sql/sql_insert.cc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/sql/sql_insert.cc b/sql/sql_insert.cc index 93cb16715f9..785e75635b8 100644 --- a/sql/sql_insert.cc +++ b/sql/sql_insert.cc @@ -64,7 +64,7 @@ #ifndef EMBEDDED_LIBRARY static bool delayed_get_table(THD *thd, TABLE_LIST *table_list); -static int write_delayed(THD *thd, TABLE *table, enum_duplicates dup, +static int write_delayed(THD *thd, TABLE *table, enum_duplicates duplic, LEX_STRING query, bool ignore, bool log_on); static void end_delayed_insert(THD *thd); pthread_handler_t handle_delayed_insert(void *arg); @@ -1999,8 +1999,8 @@ TABLE *Delayed_insert::get_local_table(THD* client_thd) /* Put a question in queue */ static -int write_delayed(THD *thd,TABLE *table,enum_duplicates duplic, bool ignore, - char *query, uint query_length, bool log_on) +int write_delayed(THD *thd, TABLE *table, enum_duplicates duplic, + LEX_STRING query, bool ignore, bool log_on) { delayed_row *row= 0; Delayed_insert *di=thd->di; From bb2a43dd19ab1362c6902c5fab8e74aa4a09c41b Mon Sep 17 00:00:00 2001 From: "kostja@vajra.(none)" <> Date: Wed, 16 May 2007 11:49:15 +0400 Subject: [PATCH 28/29] Fix a failing assert. --- sql/sql_insert.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sql/sql_insert.cc b/sql/sql_insert.cc index 7f951867f4c..5720758128e 100644 --- a/sql/sql_insert.cc +++ b/sql/sql_insert.cc @@ -1811,7 +1811,7 @@ bool delayed_get_table(THD *thd, TABLE_LIST *table_list) pthread_mutex_unlock(&tmp->mutex); if (table_list->table) { - DBUG_ASSERT(tmp->thd.net.report_error == 0 && thd->net.report_error == 0); + DBUG_ASSERT(thd->net.report_error == 0); thd->di=tmp; } /* Unlock the delayed insert object after its last access. */ From 37843864da1a67c737c3a0f1fd8219c5b52b851f Mon Sep 17 00:00:00 2001 From: "kostja@vajra.(none)" <> Date: Wed, 16 May 2007 15:35:18 +0400 Subject: [PATCH 29/29] Make a stab at events_bugs.test failure under valgrind (no open bug report, reproduced in the runtime team tree). --- sql/event_data_objects.cc | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/sql/event_data_objects.cc b/sql/event_data_objects.cc index 4b788a8f7d8..41f2f89db24 100644 --- a/sql/event_data_objects.cc +++ b/sql/event_data_objects.cc @@ -1913,6 +1913,11 @@ Event_job_data::execute(THD *thd, bool drop) thd->variables.sql_mode= sql_mode; thd->variables.time_zone= time_zone; + /* + Peculiar initialization order is a clutch to avoid races in SHOW + PROCESSLIST which reads thd->{query/query_length} without a mutex. + */ + thd->query_length= 0; thd->query= sp_sql.c_ptr_safe(); thd->query_length= sp_sql.length(); @@ -1968,6 +1973,11 @@ end: ret= 1; else { + /* + Peculiar initialization order is a clutch to avoid races in SHOW + PROCESSLIST which reads thd->{query/query_length} without a mutex. + */ + thd->query_length= 0; thd->query= sp_sql.c_ptr_safe(); thd->query_length= sp_sql.length(); if (Events::drop_event(thd, dbname, name, FALSE))