diff options
author | Michael Widenius <monty@askmonty.org> | 2010-08-27 17:12:44 +0300 |
---|---|---|
committer | Michael Widenius <monty@askmonty.org> | 2010-08-27 17:12:44 +0300 |
commit | ad6d95d3cb420557cfc7efa658181a8d20b4c154 (patch) | |
tree | 984bb45ca187a6cc38c7132a9600d91515df564e /sql | |
parent | 9bc9855c16f815e71223398ef17cd6052becc44e (diff) | |
parent | 7909541953de43c7b7d16513c8d612cfe405af67 (diff) | |
download | mariadb-git-ad6d95d3cb420557cfc7efa658181a8d20b4c154.tar.gz |
Merge with MySQL 5.1.50
- Changed to still use bcmp() in certain cases becasue
- Faster for short unaligneed strings than memcmp()
- Bettern when using valgrind
- Changed to use my_sprintf() instead of sprintf() to get higher portability for old systems
- Changed code to use MariaDB version of select->skip_record()
- Removed -%::SCCS/s.% from Makefile.am:s to remove automake warnings
Diffstat (limited to 'sql')
51 files changed, 736 insertions, 320 deletions
diff --git a/sql/Makefile.am b/sql/Makefile.am index af34e961480..5dcbbb9815a 100644 --- a/sql/Makefile.am +++ b/sql/Makefile.am @@ -191,6 +191,3 @@ install-exec-hook: test ! -x mysqld-debug$(EXEEXT) || $(INSTALL_PROGRAM) mysqld-debug$(EXEEXT) $(DESTDIR)$(libexecdir) test ! -f mysqld-debug.sym.gz || $(INSTALL_DATA) mysqld-debug.sym.gz $(DESTDIR)$(pkglibdir) test ! -f mysqld.sym.gz || $(INSTALL_DATA) mysqld.sym.gz $(DESTDIR)$(pkglibdir) - -# Don't update the files from bitkeeper -%::SCCS/s.% diff --git a/sql/field.cc b/sql/field.cc index a0b4ec61c68..c88ba52f31f 100644 --- a/sql/field.cc +++ b/sql/field.cc @@ -8696,7 +8696,13 @@ int Field_set::store(longlong nr, bool unsigned_val) { ASSERT_COLUMN_MARKED_FOR_WRITE; int error= 0; - ulonglong max_nr= set_bits(ulonglong, typelib->count); + ulonglong max_nr; + + if (sizeof(ulonglong)*8 <= typelib->count) + max_nr= ULONGLONG_MAX; + else + max_nr= (ULL(1) << typelib->count) - 1; + if ((ulonglong) nr > max_nr) { nr&= max_nr; diff --git a/sql/ha_partition.cc b/sql/ha_partition.cc index ed2cb96a115..9e07ea7258f 100644 --- a/sql/ha_partition.cc +++ b/sql/ha_partition.cc @@ -4220,6 +4220,58 @@ int ha_partition::index_read_last_map(uchar *buf, const uchar *key, /* + Optimization of the default implementation to take advantage of dynamic + partition pruning. +*/ +int ha_partition::index_read_idx_map(uchar *buf, uint index, + const uchar *key, + key_part_map keypart_map, + enum ha_rkey_function find_flag) +{ + int error= HA_ERR_KEY_NOT_FOUND; + DBUG_ENTER("ha_partition::index_read_idx_map"); + + if (find_flag == HA_READ_KEY_EXACT) + { + uint part; + m_start_key.key= key; + m_start_key.keypart_map= keypart_map; + m_start_key.flag= find_flag; + m_start_key.length= calculate_key_len(table, index, m_start_key.key, + m_start_key.keypart_map); + + get_partition_set(table, buf, index, &m_start_key, &m_part_spec); + + /* How can it be more than one partition with the current use? */ + DBUG_ASSERT(m_part_spec.start_part == m_part_spec.end_part); + + for (part= m_part_spec.start_part; part <= m_part_spec.end_part; part++) + { + if (bitmap_is_set(&(m_part_info->used_partitions), part)) + { + error= m_file[part]->index_read_idx_map(buf, index, key, + keypart_map, find_flag); + if (error != HA_ERR_KEY_NOT_FOUND && + error != HA_ERR_END_OF_FILE) + break; + } + } + } + else + { + /* + If not only used with READ_EXACT, we should investigate if possible + to optimize for other find_flag's as well. + */ + DBUG_ASSERT(0); + /* fall back on the default implementation */ + error= handler::index_read_idx_map(buf, index, key, keypart_map, find_flag); + } + DBUG_RETURN(error); +} + + +/* Read next record in a forward index scan SYNOPSIS diff --git a/sql/ha_partition.h b/sql/ha_partition.h index 9f6d9e0a5ba..e3dc7d17c6d 100644 --- a/sql/ha_partition.h +++ b/sql/ha_partition.h @@ -53,8 +53,7 @@ typedef struct st_ha_data_partition HA_CAN_FULLTEXT | \ HA_DUPLICATE_POS | \ HA_CAN_SQL_HANDLER | \ - HA_CAN_INSERT_DELAYED | \ - HA_PRIMARY_KEY_REQUIRED_FOR_POSITION) + HA_CAN_INSERT_DELAYED) class ha_partition :public handler { private: @@ -448,6 +447,15 @@ public: virtual int index_init(uint idx, bool sorted); virtual int index_end(); + /** + @breif + Positions an index cursor to the index specified in the hanlde. Fetches the + row if available. If the key value is null, begin at first key of the + index. + */ + virtual int index_read_idx_map(uchar *buf, uint index, const uchar *key, + key_part_map keypart_map, + enum ha_rkey_function find_flag); /* These methods are used to jump to next or previous entry in the index scan. There are also methods to jump to first and last entry. @@ -766,9 +774,6 @@ public: HA_PRIMARY_KEY_REQUIRED_FOR_POSITION: Does the storage engine need a PK for position? - Used with hidden primary key in InnoDB. - Hidden primary keys cannot be supported by partitioning, since the - partitioning expressions columns must be a part of the primary key. (InnoDB) HA_FILE_BASED is always set for partition handler since we use a diff --git a/sql/handler.h b/sql/handler.h index d0b5bbba996..36b0bf77b7a 100644 --- a/sql/handler.h +++ b/sql/handler.h @@ -93,7 +93,10 @@ #define HA_PRIMARY_KEY_IN_READ_INDEX (1 << 15) /* If HA_PRIMARY_KEY_REQUIRED_FOR_POSITION is set, it means that to position() - uses a primary key. Without primary key, we can't call position(). + uses a primary key given by the record argument. + Without primary key, we can't call position(). + If not set, the position is returned as the current rows position + regardless of what argument is given. */ #define HA_PRIMARY_KEY_REQUIRED_FOR_POSITION (1 << 16) #define HA_CAN_RTREEKEYS (1 << 17) @@ -1461,10 +1464,9 @@ public: virtual int rnd_next(uchar *buf)=0; virtual int rnd_pos(uchar * buf, uchar *pos)=0; /** - One has to use this method when to find - random position by record as the plain - position() call doesn't work for some - handlers for random position. + This function only works for handlers having + HA_PRIMARY_KEY_REQUIRED_FOR_POSITION set. + It will return the row with the PK given in the record argument. */ virtual int rnd_pos_by_record(uchar *record) { @@ -1486,6 +1488,12 @@ public: virtual ha_rows records_in_range(uint inx, key_range *min_key, key_range *max_key) { return (ha_rows) 10; } + /* + If HA_PRIMARY_KEY_REQUIRED_FOR_POSITION is set, then it sets ref + (reference to the row, aka position, with the primary key given in + the record). + Otherwise it set ref to the current row. + */ virtual void position(const uchar *record)=0; virtual int info(uint)=0; // see my_base.h for full description virtual void get_dynamic_partition_info(PARTITION_INFO *stat_info, diff --git a/sql/item.cc b/sql/item.cc index c2ab2a9046f..ecd208b838d 100644 --- a/sql/item.cc +++ b/sql/item.cc @@ -255,8 +255,9 @@ my_decimal *Item::val_decimal_from_int(my_decimal *decimal_value) my_decimal *Item::val_decimal_from_string(my_decimal *decimal_value) { String *res; + if (!(res= val_str(&str_value))) - return 0; // NULL or EOM + return 0; if (str2my_decimal(E_DEC_FATAL_ERROR & ~E_DEC_BAD_NUM, res->ptr(), res->length(), res->charset(), @@ -4139,8 +4140,7 @@ Item_field::fix_outer_field(THD *thd, Field **from_field, Item **reference) context->first_name_resolution_table, context->last_name_resolution_table, reference, REPORT_ALL_ERRORS, - !any_privileges && - TRUE, TRUE); + !any_privileges, TRUE); } return -1; } diff --git a/sql/item.h b/sql/item.h index af0d7648689..05fde79ce31 100644 --- a/sql/item.h +++ b/sql/item.h @@ -2802,6 +2802,7 @@ public: class Cached_item_str :public Cached_item { Item *item; + uint32 value_max_length; String value,tmp_value; public: Cached_item_str(THD *thd, Item *arg); diff --git a/sql/item_buff.cc b/sql/item_buff.cc index 2f45d0a17c2..0ac4edb3656 100644 --- a/sql/item_buff.cc +++ b/sql/item_buff.cc @@ -58,7 +58,9 @@ Cached_item::~Cached_item() {} */ Cached_item_str::Cached_item_str(THD *thd, Item *arg) - :item(arg), value(min(arg->max_length, thd->variables.max_sort_length)) + :item(arg), + value_max_length(min(arg->max_length, thd->variables.max_sort_length)), + value(value_max_length) {} bool Cached_item_str::cmp(void) @@ -67,7 +69,7 @@ bool Cached_item_str::cmp(void) bool tmp; if ((res=item->val_str(&tmp_value))) - res->length(min(res->length(), value.alloced_length())); + res->length(min(res->length(), value_max_length)); if (null_value != item->null_value) { if ((null_value= item->null_value)) diff --git a/sql/item_create.cc b/sql/item_create.cc index 2e72ab24fb0..eade395a229 100644 --- a/sql/item_create.cc +++ b/sql/item_create.cc @@ -5054,8 +5054,6 @@ create_func_cast(THD *thd, Item *a, Cast_target cast_type, CHARSET_INFO *cs) { Item *UNINIT_VAR(res); - ulong len; - uint dec; switch (cast_type) { case ITEM_CAST_BINARY: @@ -5078,11 +5076,10 @@ create_func_cast(THD *thd, Item *a, Cast_target cast_type, break; case ITEM_CAST_DECIMAL: { - if (c_len == NULL) - { - len= 0; - } - else + ulong len= 0; + uint dec= 0; + + if (c_len) { ulong decoded_size; errno= 0; @@ -5096,11 +5093,7 @@ create_func_cast(THD *thd, Item *a, Cast_target cast_type, len= decoded_size; } - if (c_dec == NULL) - { - dec= 0; - } - else + if (c_dec) { ulong decoded_size; errno= 0; @@ -5136,12 +5129,9 @@ create_func_cast(THD *thd, Item *a, Cast_target cast_type, } case ITEM_CAST_CHAR: { + int len= -1; CHARSET_INFO *real_cs= (cs ? cs : thd->variables.collation_connection); - if (c_len == NULL) - { - len= (ulong) -1L; - } - else + if (c_len) { ulong decoded_size; errno= 0; @@ -5151,7 +5141,7 @@ create_func_cast(THD *thd, Item *a, Cast_target cast_type, my_error(ER_TOO_BIG_DISPLAYWIDTH, MYF(0), "cast as char", MAX_FIELD_BLOBLENGTH); return NULL; } - len= decoded_size; + len= (int) decoded_size; } res= new (thd->mem_root) Item_char_typecast(a, len, real_cs); break; diff --git a/sql/item_func.cc b/sql/item_func.cc index dc7c75fb5d2..41cdcfa4312 100644 --- a/sql/item_func.cc +++ b/sql/item_func.cc @@ -2242,6 +2242,8 @@ void Item_func_min_max::fix_length_and_dec() max_length= my_decimal_precision_to_length_no_truncation(max_int_part + decimals, decimals, unsigned_flag); + else if (cmp_type == REAL_RESULT) + max_length= float_length(decimals); cached_field_type= agg_field_type(args, arg_count); } @@ -4718,6 +4720,7 @@ bool Item_func_get_user_var::set_value(THD *thd, bool Item_user_var_as_out_param::fix_fields(THD *thd, Item **ref) { DBUG_ASSERT(fixed == 0); + DBUG_ASSERT(thd->lex->exchange); if (Item::fix_fields(thd, ref) || !(entry= get_variable(&thd->user_vars, name, 1))) return TRUE; @@ -4727,7 +4730,9 @@ bool Item_user_var_as_out_param::fix_fields(THD *thd, Item **ref) of fields in LOAD DATA INFILE. (Since Item_user_var_as_out_param is used only there). */ - entry->collation.set(thd->variables.collation_database); + entry->collation.set(thd->lex->exchange->cs ? + thd->lex->exchange->cs : + thd->variables.collation_database); entry->update_query_id= thd->query_id; return FALSE; } diff --git a/sql/item_sum.cc b/sql/item_sum.cc index b3b07119066..934003ead19 100644 --- a/sql/item_sum.cc +++ b/sql/item_sum.cc @@ -3043,7 +3043,6 @@ Item_func_group_concat::Item_func_group_concat(THD *thd, tree(item->tree), unique_filter(item->unique_filter), table(item->table), - order(item->order), context(item->context), arg_count_order(item->arg_count_order), arg_count_field(item->arg_count_field), @@ -3056,6 +3055,24 @@ Item_func_group_concat::Item_func_group_concat(THD *thd, { quick_group= item->quick_group; result.set_charset(collation.collation); + + /* + Since the ORDER structures pointed to by the elements of the 'order' array + may be modified in find_order_in_list() called from + Item_func_group_concat::setup(), create a copy of those structures so that + such modifications done in this object would not have any effect on the + object being copied. + */ + ORDER *tmp; + if (!(order= (ORDER **) thd->alloc(sizeof(ORDER *) * arg_count_order + + sizeof(ORDER) * arg_count_order))) + return; + tmp= (ORDER *)(order + arg_count_order); + for (uint i= 0; i < arg_count_order; i++, tmp++) + { + memcpy(tmp, item->order[i], sizeof(ORDER)); + order[i]= tmp; + } } diff --git a/sql/item_timefunc.cc b/sql/item_timefunc.cc index d91ccee1575..96dede612c8 100644 --- a/sql/item_timefunc.cc +++ b/sql/item_timefunc.cc @@ -2996,7 +2996,7 @@ String *Item_func_maketime::val_str(String *str) buf, len, MYSQL_TIMESTAMP_TIME, NullS); } - + if (make_time_with_warn((DATE_TIME_FORMAT *) 0, <ime, str)) { null_value= 1; diff --git a/sql/log.cc b/sql/log.cc index c70b7816a9d..3b3da7adfe7 100644 --- a/sql/log.cc +++ b/sql/log.cc @@ -1641,6 +1641,19 @@ static int binlog_rollback(handlerton *hton, THD *thd, bool all) DBUG_RETURN(error); } +/** + Cleanup the cache. + + @param thd The client thread that wants to clean up the cache. +*/ +void MYSQL_BIN_LOG::reset_gathered_updates(THD *thd) +{ + binlog_trx_data *const trx_data= + (binlog_trx_data*) thd_get_ha_data(thd, binlog_hton); + + trx_data->reset(); +} + void MYSQL_BIN_LOG::set_write_error(THD *thd) { DBUG_ENTER("MYSQL_BIN_LOG::set_write_error"); @@ -1749,17 +1762,17 @@ static int binlog_savepoint_rollback(handlerton *hton, THD *thd, void *sv) int check_binlog_magic(IO_CACHE* log, const char** errmsg) { - char magic[4]; + uchar magic[4]; DBUG_ASSERT(my_b_tell(log) == 0); - if (my_b_read(log, (uchar*) magic, sizeof(magic))) + if (my_b_read(log, magic, sizeof(magic))) { *errmsg = "I/O error reading the header from the binary log"; sql_print_error("%s, errno=%d, io cache code=%d", *errmsg, my_errno, log->error); return 1; } - if (memcmp(magic, BINLOG_MAGIC, sizeof(magic))) + if (bcmp(magic, BINLOG_MAGIC, sizeof(magic))) { *errmsg = "Binlog has bad magic number; It's not a binary log file that can be used by this version of MySQL"; return 1; @@ -1876,7 +1889,7 @@ static int find_uniq_filename(char *name) file_info= dir_info->dir_entry; for (i=dir_info->number_off_files ; i-- ; file_info++) { - if (bcmp((uchar*) file_info->name, (uchar*) start, length) == 0 && + if (memcmp(file_info->name, start, length) == 0 && test_if_number(file_info->name+length, &number,0)) { set_if_bigger(max_found,(ulong) number); @@ -2654,7 +2667,7 @@ bool MYSQL_BIN_LOG::open(const char *log_name, an extension for the binary log files. In this case we write a standard header to it. */ - if (my_b_safe_write(&log_file, (uchar*) BINLOG_MAGIC, + if (my_b_safe_write(&log_file, BINLOG_MAGIC, BIN_LOG_HEADER_SIZE)) goto err; bytes_written+= BIN_LOG_HEADER_SIZE; @@ -5089,6 +5102,22 @@ void sql_perror(const char *message) } +/* + Unfortunately, there seems to be no good way + to restore the original streams upon failure. +*/ +static bool redirect_std_streams(const char *file) +{ + if (freopen(file, "a+", stdout) && freopen(file, "a+", stderr)) + { + setbuf(stderr, NULL); + return FALSE; + } + + return TRUE; +} + + bool flush_error_log() { bool result=0; @@ -5117,11 +5146,7 @@ bool flush_error_log() setbuf(stderr, NULL); (void) my_delete(err_renamed, MYF(0)); my_rename(log_error_file,err_renamed,MYF(0)); - if (freopen(log_error_file,"a+",stdout)) - { - freopen(log_error_file,"a+",stderr); - setbuf(stderr, NULL); - } + redirect_std_streams(log_error_file); if ((fd = my_open(err_temp, O_RDONLY, MYF(0))) >= 0) { @@ -5136,13 +5161,7 @@ bool flush_error_log() result= 1; #else my_rename(log_error_file,err_renamed,MYF(0)); - if (freopen(log_error_file,"a+",stdout)) - { - FILE *reopen; - reopen= freopen(log_error_file,"a+",stderr); - setbuf(stderr, NULL); - } - else + if (redirect_std_streams(log_error_file)) result= 1; #endif VOID(pthread_mutex_unlock(&LOCK_error_log)); @@ -5193,25 +5212,9 @@ static void print_buffer_to_nt_eventlog(enum loglevel level, char *buff, #endif /* __NT__ */ -/** - Prints a printf style message to the error log and, under NT, to the - Windows event log. - - This function prints the message into a buffer and then sends that buffer - to other functions to write that message to other logging sources. - - @param event_type Type of event to write (Error, Warning, or Info) - @param format Printf style format of message - @param args va_list list of arguments for the message - - @returns - The function always returns 0. The return value is present in the - signature to be compatible with other logging routines, which could - return an error (e.g. logging to the log tables) -*/ - #ifndef EMBEDDED_LIBRARY -static void print_buffer_to_file(enum loglevel level, const char *buffer) +static void print_buffer_to_file(enum loglevel level, const char *buffer, + size_t length) { time_t skr; struct tm tm_tmp; @@ -5225,7 +5228,7 @@ static void print_buffer_to_file(enum loglevel level, const char *buffer) localtime_r(&skr, &tm_tmp); start=&tm_tmp; - fprintf(stderr, "%02d%02d%02d %2d:%02d:%02d [%s] %s\n", + fprintf(stderr, "%02d%02d%02d %2d:%02d:%02d [%s] %.*s\n", start->tm_year % 100, start->tm_mon+1, start->tm_mday, @@ -5234,7 +5237,7 @@ static void print_buffer_to_file(enum loglevel level, const char *buffer) start->tm_sec, (level == ERROR_LEVEL ? "ERROR" : level == WARNING_LEVEL ? "Warning" : "Note"), - buffer); + (int) length, buffer); fflush(stderr); @@ -5242,17 +5245,30 @@ static void print_buffer_to_file(enum loglevel level, const char *buffer) DBUG_VOID_RETURN; } +/** + Prints a printf style message to the error log and, under NT, to the + Windows event log. + + This function prints the message into a buffer and then sends that buffer + to other functions to write that message to other logging sources. + + @param level The level of the msg significance + @param format Printf style format of message + @param args va_list list of arguments for the message + @returns + The function always returns 0. The return value is present in the + signature to be compatible with other logging routines, which could + return an error (e.g. logging to the log tables) +*/ int vprint_msg_to_log(enum loglevel level, const char *format, va_list args) { char buff[1024]; + size_t length; DBUG_ENTER("vprint_msg_to_log"); -#ifdef __NT__ - size_t length= -#endif - my_vsnprintf(buff, sizeof(buff), format, args); - print_buffer_to_file(level, buff); + length= my_vsnprintf(buff, sizeof(buff), format, args); + print_buffer_to_file(level, buff, length); #ifdef __NT__ print_buffer_to_nt_eventlog(level, buff, length, sizeof(buff)); @@ -5260,7 +5276,7 @@ int vprint_msg_to_log(enum loglevel level, const char *format, va_list args) DBUG_RETURN(0); } -#endif /*EMBEDDED_LIBRARY*/ +#endif /* EMBEDDED_LIBRARY */ void sql_print_error(const char *format, ...) @@ -5346,7 +5362,7 @@ ulong tc_log_page_waits= 0; #define TC_LOG_HEADER_SIZE (sizeof(tc_log_magic)+1) -static const char tc_log_magic[]={(char) 254, 0x23, 0x05, 0x74}; +static const uchar tc_log_magic[]={(uchar) 254, 0x23, 0x05, 0x74}; ulong opt_tc_log_size= TC_LOG_MIN_SIZE; ulong tc_log_max_pages_used=0, tc_log_page_size=0, tc_log_cur_pages_used=0; @@ -5746,7 +5762,7 @@ int TC_LOG_MMAP::recover() HASH xids; PAGE *p=pages, *end_p=pages+npages; - if (memcmp(data, tc_log_magic, sizeof(tc_log_magic))) + if (bcmp(data, tc_log_magic, sizeof(tc_log_magic))) { sql_print_error("Bad magic header in tc log"); goto err1; diff --git a/sql/log.h b/sql/log.h index 8d3880d9171..8f1ed7ee90c 100644 --- a/sql/log.h +++ b/sql/log.h @@ -356,10 +356,11 @@ public: /* Use this to start writing a new log file */ void new_file(); + void reset_gathered_updates(THD *thd); bool write(Log_event* event_info); // binary log write bool write(THD *thd, IO_CACHE *cache, Log_event *commit_event, bool incident); - bool write_incident(THD *thd, bool lock); + bool write_incident(THD *thd, bool lock); int write_cache(IO_CACHE *cache, bool lock_log, bool flush_and_sync); void set_write_error(THD *thd); bool check_write_error(THD *thd); diff --git a/sql/log_event.cc b/sql/log_event.cc index 90885315ce1..3db7929331d 100644 --- a/sql/log_event.cc +++ b/sql/log_event.cc @@ -1673,7 +1673,7 @@ beg: precision, decimals); return bin_size; } - + case MYSQL_TYPE_FLOAT: { float fl; @@ -2306,6 +2306,53 @@ bool Query_log_event::write(IO_CACHE* file) start+= 4; } + if (thd && thd->is_current_user_used()) + { + LEX_STRING user; + LEX_STRING host; + memset(&user, 0, sizeof(user)); + memset(&host, 0, sizeof(host)); + + if (thd->slave_thread && thd->has_invoker()) + { + /* user will be null, if master is older than this patch */ + user= thd->get_invoker_user(); + host= thd->get_invoker_host(); + } + else if (thd->security_ctx->priv_user) + { + Security_context *ctx= thd->security_ctx; + + user.length= strlen(ctx->priv_user); + user.str= ctx->priv_user; + if (ctx->priv_host[0] != '\0') + { + host.str= ctx->priv_host; + host.length= strlen(ctx->priv_host); + } + } + + if (user.length > 0) + { + *start++= Q_INVOKER; + + /* + Store user length and user. The max length of use is 16, so 1 byte is + enough to store the user's length. + */ + *start++= (uchar)user.length; + memcpy(start, user.str, user.length); + start+= user.length; + + /* + Store host length and host. The max length of host is 60, so 1 byte is + enough to store the host's length. + */ + *start++= (uchar)host.length; + memcpy(start, host.str, host.length); + start+= host.length; + } + } /* NOTE: When adding new status vars, please don't forget to update the MAX_SIZE_LOG_EVENT_STATUS in log_event.h and update the function @@ -2348,6 +2395,8 @@ bool Query_log_event::write(IO_CACHE* file) Query_log_event::Query_log_event() :Log_event(), data_buf(0) { + memset(&user, 0, sizeof(user)); + memset(&host, 0, sizeof(host)); } @@ -2390,6 +2439,9 @@ Query_log_event::Query_log_event(THD* thd_arg, const char* query_arg, { time_t end_time; + memset(&user, 0, sizeof(user)); + memset(&host, 0, sizeof(host)); + error_code= errcode; time(&end_time); @@ -2574,6 +2626,8 @@ Query_log_event::Query_log_event(const char* buf, uint event_len, bool catalog_nz= 1; DBUG_ENTER("Query_log_event::Query_log_event(char*,...)"); + memset(&user, 0, sizeof(user)); + memset(&host, 0, sizeof(host)); common_header_len= description_event->common_header_len; post_header_len= description_event->post_header_len[event_type-1]; DBUG_PRINT("info",("event_len: %u common_header_len: %d post_header_len: %d", @@ -2728,6 +2782,20 @@ Query_log_event::Query_log_event(const char* buf, uint event_len, data_written= master_data_written= uint4korr(pos); pos+= 4; break; + case Q_INVOKER: + { + CHECK_SPACE(pos, end, 1); + user.length= *pos++; + CHECK_SPACE(pos, end, user.length); + user.str= (char *)pos; + pos+= user.length; + + CHECK_SPACE(pos, end, 1); + host.length= *pos++; + CHECK_SPACE(pos, end, host.length); + host.str= (char *)pos; + pos+= host.length; + } default: /* That's why you must write status vars in growing order of code */ DBUG_PRINT("info",("Query_log_event has unknown status vars (first has\ @@ -2741,12 +2809,16 @@ Query_log_event::Query_log_event(const char* buf, uint event_len, time_zone_len + 1 + data_len + 1 + QUERY_CACHE_FLAGS_SIZE + + user.length + 1 + + host.length + 1 + db_len + 1, MYF(MY_WME)))) #else if (!(start= data_buf = (Log_event::Byte*) my_malloc(catalog_len + 1 + time_zone_len + 1 + - data_len + 1, + data_len + 1 + + user.length + 1 + + host.length + 1, MYF(MY_WME)))) #endif DBUG_VOID_RETURN; @@ -2769,6 +2841,11 @@ Query_log_event::Query_log_event(const char* buf, uint event_len, if (time_zone_len) copy_str_and_move(&time_zone_str, &start, time_zone_len); + if (user.length > 0) + copy_str_and_move((const char **)&(user.str), &start, user.length); + if (host.length > 0) + copy_str_and_move((const char **)&(host.str), &start, host.length); + /** if time_zone_len or catalog_len are 0, then time_zone and catalog are uninitialized at this point. shouldn't they point to the @@ -2904,7 +2981,7 @@ void Query_log_event::print_query_header(IO_CACHE* file, if (likely(charset_inited) && (unlikely(!print_event_info->charset_inited || - bcmp((uchar*) print_event_info->charset, (uchar*) charset, 6)))) + memcmp(print_event_info->charset, charset, 6)))) { CHARSET_INFO *cs_info= get_charset(uint2korr(charset), MYF(MY_WME)); if (cs_info) @@ -2927,8 +3004,8 @@ void Query_log_event::print_query_header(IO_CACHE* file, } if (time_zone_len) { - if (bcmp((uchar*) print_event_info->time_zone_str, - (uchar*) time_zone_str, time_zone_len+1)) + if (memcmp(print_event_info->time_zone_str, + time_zone_str, time_zone_len+1)) { my_b_printf(file,"SET @@session.time_zone='%s'%s\n", time_zone_str, print_event_info->delimiter); @@ -3177,7 +3254,7 @@ int Query_log_event::do_apply_event(Relay_log_info const *rli, thd->variables.collation_database= thd->db_charset; thd->table_map_for_update= (table_map)table_map_for_update; - + thd->set_invoker(&user, &host); /* Execute the query (note that we bypass dispatch_command()) */ const char* found_semicolon= NULL; mysql_parse(thd, thd->query(), thd->query_length(), &found_semicolon); @@ -5588,7 +5665,7 @@ void User_var_log_event::print(FILE* file, PRINT_EVENT_INFO* print_event_info) double real_val; char real_buf[FMT_G_BUFSIZE(14)]; float8get(real_val, val); - my_sprintf(real_buf, (real_buf, "%.14g", real_val)); + sprintf(real_buf, "%.14g", real_val); my_b_printf(&cache, ":=%s%s\n", real_buf, print_event_info->delimiter); break; case INT_RESULT: @@ -7505,8 +7582,7 @@ int Rows_log_event::do_apply_event(Relay_log_info const *rli) { int actual_error= convert_handler_error(error, thd, table); bool idempotent_error= (idempotent_error_code(error) && - ((bit_is_set(slave_exec_mode, - SLAVE_EXEC_MODE_IDEMPOTENT)) == 1)); + (slave_exec_mode & SLAVE_EXEC_MODE_IDEMPOTENT)); bool ignored_error= (idempotent_error == 0 ? ignored_error_code(actual_error) : 0); @@ -8334,7 +8410,7 @@ Write_rows_log_event::do_before_row_operations(const Slave_reporting_capability todo: to introduce a property for the event (handler?) which forces applying the event in the replace (idempotent) fashion. */ - if (bit_is_set(slave_exec_mode, SLAVE_EXEC_MODE_IDEMPOTENT) == 1 || + if ((slave_exec_mode & SLAVE_EXEC_MODE_IDEMPOTENT) || m_table->s->db_type()->db_type == DB_TYPE_NDBCLUSTER) { /* @@ -8413,7 +8489,7 @@ Write_rows_log_event::do_after_row_operations(const Slave_reporting_capability * int local_error= 0; m_table->next_number_field=0; m_table->auto_increment_field_not_null= FALSE; - if (bit_is_set(slave_exec_mode, SLAVE_EXEC_MODE_IDEMPOTENT) == 1 || + if ((slave_exec_mode & SLAVE_EXEC_MODE_IDEMPOTENT) || m_table->s->db_type()->db_type == DB_TYPE_NDBCLUSTER) { m_table->file->extra(HA_EXTRA_NO_IGNORE_DUP_KEY); @@ -8516,7 +8592,7 @@ Rows_log_event::write_row(const Relay_log_info *const rli, TABLE *table= m_table; // pointer to event's table int error; - int keynum; + int UNINIT_VAR(keynum); auto_afree_ptr<char> key(NULL); /* fill table->record[0] with default values */ @@ -8714,10 +8790,8 @@ int Write_rows_log_event::do_exec_row(const Relay_log_info *const rli) { DBUG_ASSERT(m_table != NULL); - int error= - write_row(rli, /* if 1 then overwrite */ - bit_is_set(slave_exec_mode, SLAVE_EXEC_MODE_IDEMPOTENT) == 1); - + int error= write_row(rli, (slave_exec_mode & SLAVE_EXEC_MODE_IDEMPOTENT)); + if (error && !thd->is_error()) { DBUG_ASSERT(0); diff --git a/sql/log_event.h b/sql/log_event.h index 7a6606badf5..2a2b1807781 100644 --- a/sql/log_event.h +++ b/sql/log_event.h @@ -264,7 +264,8 @@ struct sql_ex_info 1 + 2 /* type, lc_time_names_number */ + \ 1 + 2 /* type, charset_database_number */ + \ 1 + 8 /* type, table_map_for_update */ + \ - 1 + 4 /* type, master_data_written */) + 1 + 4 /* type, master_data_written */ + \ + 1 + 16 + 1 + 60/* type, user_len, user, host_len, host */) #define MAX_LOG_EVENT_HEADER ( /* in order of Query_log_event::write */ \ LOG_EVENT_HEADER_LEN + /* write_header */ \ QUERY_HEADER_LEN + /* write_data */ \ @@ -333,6 +334,8 @@ struct sql_ex_info #define Q_MASTER_DATA_WRITTEN_CODE 10 +#define Q_INVOKER 11 + /* Intvar event post-header */ /* Intvar event data */ @@ -395,7 +398,7 @@ struct sql_ex_info #define ELQ_DUP_HANDLING_OFFSET ELQ_FILE_ID_OFFSET + 12 /* 4 bytes which all binlogs should begin with */ -#define BINLOG_MAGIC "\xfe\x62\x69\x6e" +#define BINLOG_MAGIC (const uchar*) "\xfe\x62\x69\x6e" /* The 2 flags below were useless : @@ -1546,6 +1549,8 @@ protected: */ class Query_log_event: public Log_event { + LEX_STRING user; + LEX_STRING host; protected: Log_event::Byte* data_buf; public: diff --git a/sql/log_event_old.cc b/sql/log_event_old.cc index d1b17be1691..43f7db2b56f 100644 --- a/sql/log_event_old.cc +++ b/sql/log_event_old.cc @@ -441,7 +441,7 @@ copy_extra_record_fields(TABLE *table, DBUG_ASSERT(master_reclength <= table->s->reclength); if (master_reclength < table->s->reclength) - bmove_align(table->record[0] + master_reclength, + memcpy(table->record[0] + master_reclength, table->record[1] + master_reclength, table->s->reclength - master_reclength); @@ -720,7 +720,7 @@ static int find_and_fetch_row(TABLE *table, uchar *key) rnd_pos() returns the record in table->record[0], so we have to move it to table->record[1]. */ - bmove_align(table->record[1], table->record[0], table->s->reclength); + memcpy(table->record[1], table->record[0], table->s->reclength); DBUG_RETURN(error); } @@ -1213,7 +1213,7 @@ int Update_rows_log_event_old::do_exec_row(TABLE *table) overwriting the default values that where put there by the unpack_row() function. */ - bmove_align(table->record[0], m_after_image, table->s->reclength); + memcpy(table->record[0], m_after_image, table->s->reclength); copy_extra_record_fields(table, m_master_reclength, m_width); /* diff --git a/sql/mysql_priv.h b/sql/mysql_priv.h index bf973b7bfa0..bc2cff9ac50 100644 --- a/sql/mysql_priv.h +++ b/sql/mysql_priv.h @@ -1041,7 +1041,7 @@ bool mysql_opt_change_db(THD *thd, bool force_switch, bool *cur_db_changed); -void mysql_parse(THD *thd, const char *inBuf, uint length, +void mysql_parse(THD *thd, char *rawbuf, uint length, const char ** semicolon); bool mysql_test_parse_for_slave(THD *thd,char *inBuf,uint length); diff --git a/sql/mysqld.cc b/sql/mysqld.cc index ab7ad549121..8bec9dc474a 100644 --- a/sql/mysqld.cc +++ b/sql/mysqld.cc @@ -187,7 +187,7 @@ typedef fp_except fp_except_t; # define fpu_control_t unsigned int # define _FPU_EXTENDED 0x300 # define _FPU_DOUBLE 0x200 -# if defined(__GNUC__) || defined(__SUNPRO_CC) +# if defined(__GNUC__) || (defined(__SUNPRO_CC) && __SUNPRO_CC >= 0x590) # define _FPU_GETCW(cw) asm volatile ("fnstcw %0" : "=m" (*&cw)) # define _FPU_SETCW(cw) asm volatile ("fldcw %0" : : "m" (*&cw)) # else @@ -573,7 +573,7 @@ ulong query_buff_size, slow_launch_time, slave_open_temp_tables; ulong open_files_limit, max_binlog_size, max_relay_log_size; ulong slave_net_timeout, slave_trans_retries; ulong slave_exec_mode_options; -const char *slave_exec_mode_str= "STRICT"; +static const char *slave_exec_mode_str= "STRICT"; ulong thread_cache_size=0, thread_pool_size= 0; ulong binlog_cache_size=0; ulonglong max_binlog_cache_size=0; @@ -2549,7 +2549,9 @@ extern "C" sig_handler handle_segfault(int sig) { time_t curr_time; struct tm tm; +#ifdef HAVE_STACKTRACE THD *thd=current_thd; +#endif /* Strictly speaking, one needs a mutex here @@ -2616,13 +2618,14 @@ the thread stack. Please read http://dev.mysql.com/doc/mysql/en/linux.html\n\n", #endif /* HAVE_LINUXTHREADS */ #ifdef HAVE_STACKTRACE + if (!(test_flags & TEST_NO_STACKTRACE)) { - fprintf(stderr,"thd: 0x%lx\n",(long) thd); - fprintf(stderr,"\ -Attempting backtrace. You can use the following information to find out\n\ -where mysqld died. If you see no messages after this, something went\n\ -terribly wrong...\n"); + fprintf(stderr, "thd: 0x%lx\n",(long) thd); + fprintf(stderr, "Attempting backtrace. You can use the following " + "information to find out\nwhere mysqld died. If " + "you see no messages after this, something went\n" + "terribly wrong...\n"); my_print_stacktrace(thd ? (uchar*) thd->thread_stack : NULL, my_thread_stack_size); } @@ -8154,10 +8157,11 @@ static int mysql_init_variables(void) /* Things with default values that are not zero */ delay_key_write_options= (uint) DELAY_KEY_WRITE_ON; - slave_exec_mode_options= 0; - slave_exec_mode_options= (uint) - find_bit_type_or_exit(slave_exec_mode_str, &slave_exec_mode_typelib, NULL, - &error); + slave_exec_mode_options= find_bit_type_or_exit(slave_exec_mode_str, + &slave_exec_mode_typelib, + NULL, &error); + /* Default mode string must not yield a error. */ + DBUG_ASSERT(!error); if (error) return 1; opt_specialflag= SPECIAL_ENGLISH; @@ -8448,8 +8452,9 @@ mysqld_get_one_option(int optid, init_slave_skip_errors(argument); break; case OPT_SLAVE_EXEC_MODE: - slave_exec_mode_options= (uint) - find_bit_type_or_exit(argument, &slave_exec_mode_typelib, "", &error); + slave_exec_mode_options= find_bit_type_or_exit(argument, + &slave_exec_mode_typelib, + "", &error); if (error) return 1; break; @@ -8553,7 +8558,7 @@ mysqld_get_one_option(int optid, *val= 0; val+= 2; while (*val && my_isspace(mysqld_charset, *val)) - *val++; + val++; if (!*val) { sql_print_error("Bad syntax in replicate-rewrite-db - empty TO db!\n"); @@ -9143,7 +9148,7 @@ static int get_options(int *argc,char **argv) /* Set global MyISAM variables from delay_key_write_options */ fix_delay_key_write((THD*) 0, OPT_GLOBAL); /* Set global slave_exec_mode from its option */ - fix_slave_exec_mode(OPT_GLOBAL); + fix_slave_exec_mode(); global_system_variables.log_slow_filter= fix_log_slow_filter(global_system_variables.log_slow_filter); diff --git a/sql/opt_range.cc b/sql/opt_range.cc index 76f1c93e295..acff21c55d5 100644 --- a/sql/opt_range.cc +++ b/sql/opt_range.cc @@ -7627,7 +7627,7 @@ check_quick_keys(PARAM *param, uint idx, SEL_ARG *key_tree, if (unlikely(param->thd->killed != 0)) return HA_POS_ERROR; - + keynr=param->real_keynr[idx]; param->range_count++; if (!tmp_min_flag && ! tmp_max_flag && diff --git a/sql/partition_info.cc b/sql/partition_info.cc index 0de8e478f03..f37151ea51d 100644 --- a/sql/partition_info.cc +++ b/sql/partition_info.cc @@ -103,8 +103,8 @@ char *partition_info::create_default_partition_names(uint part_no, { do { - my_sprintf(move_ptr, (move_ptr,"p%u", (start_no + i))); - move_ptr+=MAX_PART_NAME_SIZE; + sprintf(move_ptr, "p%u", (start_no + i)); + move_ptr+= MAX_PART_NAME_SIZE; } while (++i < no_parts_arg); } else @@ -135,7 +135,7 @@ char *partition_info::create_subpartition_name(uint subpart_no, if (likely(ptr != NULL)) { - my_sprintf(ptr, (ptr, "%ssp%u", part_name, subpart_no)); + my_snprintf(ptr, size_alloc, "%ssp%u", part_name, subpart_no); } else { diff --git a/sql/protocol.cc b/sql/protocol.cc index 41ebbcfba90..3df0eddea93 100644 --- a/sql/protocol.cc +++ b/sql/protocol.cc @@ -596,7 +596,11 @@ void Protocol::end_partial_result_set(THD *thd_arg) bool Protocol::flush() { #ifndef EMBEDDED_LIBRARY - return net_flush(&thd->net); + bool error; + thd->main_da.can_overwrite_status= TRUE; + error= net_flush(&thd->net); + thd->main_da.can_overwrite_status= FALSE; + return error; #else return 0; #endif @@ -636,7 +640,8 @@ bool Protocol::send_fields(List<Item> *list, uint flags) if (flags & SEND_NUM_ROWS) { // Packet with number of elements uchar *pos= net_store_length(buff, list->elements); - (void) my_net_write(&thd->net, buff, (size_t) (pos-buff)); + if (my_net_write(&thd->net, buff, (size_t) (pos-buff))) + DBUG_RETURN(1); } #ifndef DBUG_OFF @@ -760,7 +765,7 @@ bool Protocol::send_fields(List<Item> *list, uint flags) if (flags & SEND_DEFAULTS) item->send(&prot, &tmp); // Send default value if (prot.write()) - break; /* purecov: inspected */ + DBUG_RETURN(1); #ifndef DBUG_OFF field_types[count++]= field.type; #endif @@ -773,7 +778,9 @@ bool Protocol::send_fields(List<Item> *list, uint flags) to show that there is no cursor. Send no warning information, as it will be sent at statement end. */ - write_eof_packet(thd, &thd->net, thd->server_status, thd->total_warn_count); + if (write_eof_packet(thd, &thd->net, thd->server_status, + thd->total_warn_count)) + DBUG_RETURN(1); } DBUG_RETURN(prepare_for_send(list)); diff --git a/sql/rpl_rli.cc b/sql/rpl_rli.cc index 46ae057f97e..c37c4735e37 100644 --- a/sql/rpl_rli.cc +++ b/sql/rpl_rli.cc @@ -1115,8 +1115,7 @@ bool Relay_log_info::cached_charset_compare(char *charset) const { DBUG_ENTER("Relay_log_info::cached_charset_compare"); - if (bcmp((uchar*) cached_charset, (uchar*) charset, - sizeof(cached_charset))) + if (memcmp(cached_charset, charset, sizeof(cached_charset))) { memcpy(const_cast<char*>(cached_charset), charset, sizeof(cached_charset)); DBUG_RETURN(1); diff --git a/sql/set_var.cc b/sql/set_var.cc index 9198832dd3c..c4e41bbf261 100644 --- a/sql/set_var.cc +++ b/sql/set_var.cc @@ -95,14 +95,13 @@ TYPELIB delay_key_write_typelib= delay_key_write_type_names, NULL }; -const char *slave_exec_mode_names[]= -{ "STRICT", "IDEMPOTENT", NullS }; -static const unsigned int slave_exec_mode_names_len[]= -{ sizeof("STRICT") - 1, sizeof("IDEMPOTENT") - 1, 0 }; +static const char *slave_exec_mode_names[]= { "STRICT", "IDEMPOTENT", NullS }; +static unsigned int slave_exec_mode_names_len[]= { sizeof("STRICT") - 1, + sizeof("IDEMPOTENT") - 1, 0 }; TYPELIB slave_exec_mode_typelib= { array_elements(slave_exec_mode_names)-1, "", - slave_exec_mode_names, (unsigned int *) slave_exec_mode_names_len + slave_exec_mode_names, slave_exec_mode_names_len }; static int sys_check_ftb_syntax(THD *thd, set_var *var); @@ -1267,7 +1266,7 @@ uchar *sys_var_set::value_ptr(THD *thd, enum_var_type type, void sys_var_set_slave_mode::set_default(THD *thd, enum_var_type type) { - slave_exec_mode_options= (ULL(1) << SLAVE_EXEC_MODE_STRICT); + slave_exec_mode_options= SLAVE_EXEC_MODE_STRICT; } bool sys_var_set_slave_mode::check(THD *thd, set_var *var) @@ -1275,8 +1274,7 @@ bool sys_var_set_slave_mode::check(THD *thd, set_var *var) bool rc= sys_var_set::check(thd, var); if (!rc && test_all_bits(var->save_result.ulong_value, - ((ULL(1) << SLAVE_EXEC_MODE_STRICT) | - (ULL(1) << SLAVE_EXEC_MODE_IDEMPOTENT)))) + SLAVE_EXEC_MODE_STRICT | SLAVE_EXEC_MODE_IDEMPOTENT)) { rc= true; my_error(ER_SLAVE_AMBIGOUS_EXEC_MODE, MYF(0), ""); @@ -1293,21 +1291,18 @@ bool sys_var_set_slave_mode::update(THD *thd, set_var *var) return rc; } -void fix_slave_exec_mode(enum_var_type type) +void fix_slave_exec_mode(void) { DBUG_ENTER("fix_slave_exec_mode"); - compile_time_assert(sizeof(slave_exec_mode_options) * CHAR_BIT - > SLAVE_EXEC_MODE_LAST_BIT - 1); + if (test_all_bits(slave_exec_mode_options, - ((ULL(1) << SLAVE_EXEC_MODE_STRICT) | - (ULL(1) << SLAVE_EXEC_MODE_IDEMPOTENT)))) + SLAVE_EXEC_MODE_STRICT | SLAVE_EXEC_MODE_IDEMPOTENT)) { - sql_print_error("Ambiguous slave modes combination." - " STRICT will be used"); - slave_exec_mode_options&= ~(ULL(1) << SLAVE_EXEC_MODE_IDEMPOTENT); + sql_print_error("Ambiguous slave modes combination. STRICT will be used"); + slave_exec_mode_options&= ~SLAVE_EXEC_MODE_IDEMPOTENT; } - if (!(slave_exec_mode_options & (ULL(1) << SLAVE_EXEC_MODE_IDEMPOTENT))) - slave_exec_mode_options|= (ULL(1)<< SLAVE_EXEC_MODE_STRICT); + if (!(slave_exec_mode_options & SLAVE_EXEC_MODE_IDEMPOTENT)) + slave_exec_mode_options|= SLAVE_EXEC_MODE_STRICT; DBUG_VOID_RETURN; } diff --git a/sql/set_var.h b/sql/set_var.h index c3067ebc42b..4fc23c29e7d 100644 --- a/sql/set_var.h +++ b/sql/set_var.h @@ -1458,7 +1458,7 @@ sys_var *find_sys_var(THD *thd, const char *str, uint length=0); int sql_set_variables(THD *thd, List<set_var_base> *var_list); bool not_all_support_one_shot(List<set_var_base> *var_list); void fix_delay_key_write(THD *thd, enum_var_type type); -void fix_slave_exec_mode(enum_var_type type); +void fix_slave_exec_mode(void); ulong fix_sql_mode(ulong sql_mode); extern sys_var_const_str sys_charset_system; extern sys_var_str sys_init_connect; diff --git a/sql/slave.cc b/sql/slave.cc index 3df5c0df0b2..b9138961c25 100644 --- a/sql/slave.cc +++ b/sql/slave.cc @@ -2073,7 +2073,7 @@ int apply_event_and_update_pos(Log_event* ev, THD* thd, Relay_log_info* rli) DBUG_PRINT("info", ("thd->options: %s%s; rli->last_event_start_time: %lu", FLAGSTR(thd->options, OPTION_NOT_AUTOCOMMIT), FLAGSTR(thd->options, OPTION_BEGIN), - rli->last_event_start_time)); + (ulong) rli->last_event_start_time)); /* Execute the event to change the database and update the binary @@ -2846,8 +2846,8 @@ pthread_handler_t handle_slave_sql(void *arg) char llbuff[22],llbuff1[22]; char saved_log_name[FN_REFLEN]; char saved_master_log_name[FN_REFLEN]; - my_off_t saved_log_pos; - my_off_t saved_master_log_pos; + my_off_t UNINIT_VAR(saved_log_pos); + my_off_t UNINIT_VAR(saved_master_log_pos); my_off_t saved_skip= 0; Relay_log_info* rli = &((Master_info*)arg)->rli; const char *errmsg; diff --git a/sql/spatial.cc b/sql/spatial.cc index a23e8ec0d90..2305a8eb97d 100644 --- a/sql/spatial.cc +++ b/sql/spatial.cc @@ -53,7 +53,7 @@ static Geometry::Class_info **ci_collection_end= Geometry::ci_collection+Geometry::wkb_last + 1; Geometry::Class_info::Class_info(const char *name, int type_id, - void(*create_func)(void *)): + create_geom_t create_func): m_type_id(type_id), m_create_func(create_func) { m_name.str= (char *) name; @@ -62,39 +62,39 @@ Geometry::Class_info::Class_info(const char *name, int type_id, ci_collection[type_id]= this; } -static void create_point(void *buffer) +static Geometry *create_point(char *buffer) { - new(buffer) Gis_point; + return new (buffer) Gis_point; } -static void create_linestring(void *buffer) +static Geometry *create_linestring(char *buffer) { - new(buffer) Gis_line_string; + return new (buffer) Gis_line_string; } -static void create_polygon(void *buffer) +static Geometry *create_polygon(char *buffer) { - new(buffer) Gis_polygon; + return new (buffer) Gis_polygon; } -static void create_multipoint(void *buffer) +static Geometry *create_multipoint(char *buffer) { - new(buffer) Gis_multi_point; + return new (buffer) Gis_multi_point; } -static void create_multipolygon(void *buffer) +static Geometry *create_multipolygon(char *buffer) { - new(buffer) Gis_multi_polygon; + return new (buffer) Gis_multi_polygon; } -static void create_multilinestring(void *buffer) +static Geometry *create_multilinestring(char *buffer) { - new(buffer) Gis_multi_line_string; + return new (buffer) Gis_multi_line_string; } -static void create_geometrycollection(void *buffer) +static Geometry *create_geometrycollection(char *buffer) { - new(buffer) Gis_geometry_collection; + return new (buffer) Gis_geometry_collection; } @@ -145,6 +145,15 @@ Geometry::Class_info *Geometry::find_class(const char *name, uint32 len) } +Geometry *Geometry::create_by_typeid(Geometry_buffer *buffer, int type_id) +{ + Class_info *ci; + if (!(ci= find_class(type_id))) + return NULL; + return (*ci->m_create_func)(buffer->data); +} + + Geometry *Geometry::construct(Geometry_buffer *buffer, const char *data, uint32 data_len) { @@ -153,6 +162,7 @@ Geometry *Geometry::construct(Geometry_buffer *buffer, if (data_len < SRID_SIZE + WKB_HEADER_SIZE) // < 4 + (1 + 4) return NULL; + /* + 1 to skip the byte order (stored in position SRID_SIZE). */ geom_type= uint4korr(data + SRID_SIZE + 1); if (!(result= create_by_typeid(buffer, (int) geom_type))) return NULL; @@ -177,9 +187,7 @@ Geometry *Geometry::create_from_wkt(Geometry_buffer *buffer, if (!(ci= find_class(name.str, name.length)) || wkt->reserve(1 + 4, 512)) return NULL; - (*ci->m_create_func)((void *)buffer); - Geometry *result= (Geometry *)buffer; - + Geometry *result= (*ci->m_create_func)(buffer->data); wkt->q_append((char) wkb_ndr); wkt->q_append((uint32) result->get_class_info()->m_type_id); if (trs->check_next_symbol('(') || diff --git a/sql/spatial.h b/sql/spatial.h index 86c2ed8c197..f778acd6c34 100644 --- a/sql/spatial.h +++ b/sql/spatial.h @@ -225,15 +225,18 @@ public: { wkb_xdr= 0, /* Big Endian */ wkb_ndr= 1 /* Little Endian */ - }; + }; + + /** Callback which creates Geometry objects on top of a given placement. */ + typedef Geometry *(*create_geom_t)(char *); class Class_info { public: LEX_STRING m_name; int m_type_id; - void (*m_create_func)(void *); - Class_info(const char *name, int type_id, void(*create_func)(void *)); + create_geom_t m_create_func; + Class_info(const char *name, int type_id, create_geom_t create_func); }; virtual const Class_info *get_class_info() const=0; @@ -263,15 +266,7 @@ public: virtual int geometry_n(uint32 num, String *result) const { return -1; } public: - static Geometry *create_by_typeid(Geometry_buffer *buffer, int type_id) - { - Class_info *ci; - if (!(ci= find_class((int) type_id))) - return NULL; - (*ci->m_create_func)((void *)buffer); - return my_reinterpret_cast(Geometry *)(buffer); - } - + static Geometry *create_by_typeid(Geometry_buffer *buffer, int type_id); static Geometry *construct(Geometry_buffer *buffer, const char *data, uint32 data_len); static Geometry *create_from_wkt(Geometry_buffer *buffer, @@ -528,11 +523,8 @@ public: const Class_info *get_class_info() const; }; -const int geometry_buffer_size= sizeof(Gis_point); -struct Geometry_buffer -{ - void *arr[(geometry_buffer_size - 1)/sizeof(void *) + 1]; -}; +struct Geometry_buffer : public + my_aligned_storage<sizeof(Gis_point), MY_ALIGNOF(Gis_point)> {}; #endif /*HAVE_SPATAIAL*/ #endif diff --git a/sql/sql_acl.cc b/sql/sql_acl.cc index 6e9ec902577..5e03175dd72 100644 --- a/sql/sql_acl.cc +++ b/sql/sql_acl.cc @@ -194,6 +194,7 @@ static bool compare_hostname(const acl_host_and_ip *host,const char *hostname, const char *ip); static my_bool acl_load(THD *thd, TABLE_LIST *tables); static my_bool grant_load(THD *thd, TABLE_LIST *tables); +static inline void get_grantor(THD *thd, char* grantor); /* Convert scrambled password to binary form, according to scramble type, @@ -2705,6 +2706,20 @@ end: DBUG_RETURN(result); } +static inline void get_grantor(THD *thd, char *grantor) +{ + const char *user= thd->security_ctx->user; + const char *host= thd->security_ctx->host_or_ip; + +#if defined(HAVE_REPLICATION) + if (thd->slave_thread && thd->has_invoker()) + { + user= thd->get_invoker_user().str; + host= thd->get_invoker_host().str; + } +#endif + strxmov(grantor, user, "@", host, NullS); +} static int replace_table_table(THD *thd, GRANT_TABLE *grant_table, TABLE *table, const LEX_USER &combo, @@ -2719,9 +2734,7 @@ static int replace_table_table(THD *thd, GRANT_TABLE *grant_table, uchar user_key[MAX_KEY_LENGTH]; DBUG_ENTER("replace_table_table"); - strxmov(grantor, thd->security_ctx->user, "@", - thd->security_ctx->host_or_ip, NullS); - + get_grantor(thd, grantor); /* The following should always succeed as new users are created before this function is called! @@ -2851,9 +2864,7 @@ static int replace_routine_table(THD *thd, GRANT_NAME *grant_name, DBUG_RETURN(-1); } - strxmov(grantor, thd->security_ctx->user, "@", - thd->security_ctx->host_or_ip, NullS); - + get_grantor(thd, grantor); /* New users are created before this function is called. diff --git a/sql/sql_base.cc b/sql/sql_base.cc index 81b13136b8e..1c8648afafd 100644 --- a/sql/sql_base.cc +++ b/sql/sql_base.cc @@ -5696,7 +5696,7 @@ static void update_field_dependencies(THD *thd, Field *field, TABLE *table) DBUG_ENTER("update_field_dependencies"); if (thd->mark_used_columns != MARK_COLUMNS_NONE) { - MY_BITMAP *current_bitmap; + MY_BITMAP *bitmap; /* We always want to register the used keys, as the column bitmap may have @@ -5707,9 +5707,9 @@ static void update_field_dependencies(THD *thd, Field *field, TABLE *table) table->merge_keys.merge(field->part_of_key); if (thd->mark_used_columns == MARK_COLUMNS_READ) - current_bitmap= table->read_set; + bitmap= table->read_set; else - current_bitmap= table->write_set; + bitmap= table->write_set; /* The test-and-set mechanism in the bitmap is not reliable during @@ -5718,7 +5718,7 @@ static void update_field_dependencies(THD *thd, Field *field, TABLE *table) only those columns that are used in the SET clause. I.e they are being set here. See multi_update::prepare() */ - if (bitmap_fast_test_and_set(current_bitmap, field->field_index)) + if (bitmap_fast_test_and_set(bitmap, field->field_index)) { if (thd->mark_used_columns == MARK_COLUMNS_WRITE) { @@ -8424,15 +8424,15 @@ my_bool mysql_rm_tmp_tables(void) (file->name[1] == '.' && !file->name[2]))) continue; - if (!bcmp((uchar*) file->name, (uchar*) tmp_file_prefix, - tmp_file_prefix_length)) + if (!memcmp(file->name, tmp_file_prefix, + tmp_file_prefix_length)) { char *ext= fn_ext(file->name); uint ext_len= strlen(ext); uint filePath_len= my_snprintf(filePath, sizeof(filePath), "%s%c%s", tmpdir, FN_LIBCHAR, file->name); - if (!bcmp((uchar*) reg_ext, (uchar*) ext, ext_len)) + if (!strcmp(reg_ext, ext)) { handler *handler_file= 0; /* We should cut file extention before deleting of table */ diff --git a/sql/sql_cache.cc b/sql/sql_cache.cc index 302d5e95a28..cde040a88a4 100644 --- a/sql/sql_cache.cc +++ b/sql/sql_cache.cc @@ -1679,7 +1679,8 @@ def_week_frmt: %lu, in_trans: %d, autocommit: %d", thd->limit_found_rows = query->found_rows(); thd->status_var.last_query_cost= 0.0; thd->query_plan_flags= (thd->query_plan_flags & ~QPLAN_QC_NO) | QPLAN_QC; - thd->main_da.disable_status(); + if (!thd->main_da.is_set()) + thd->main_da.disable_status(); BLOCK_UNLOCK_RD(query_block); DBUG_RETURN(1); // Result sent to client diff --git a/sql/sql_class.cc b/sql/sql_class.cc index fc0853d1871..2b102e47abe 100644 --- a/sql/sql_class.cc +++ b/sql/sql_class.cc @@ -747,6 +747,9 @@ THD::THD() thr_lock_owner_init(&main_lock_id, &lock_info); m_internal_handler= NULL; + current_user_used= FALSE; + memset(&invoker_user, 0, sizeof(invoker_user)); + memset(&invoker_host, 0, sizeof(invoker_host)); } @@ -1072,6 +1075,9 @@ void add_to_status(STATUS_VAR *to_var, STATUS_VAR *from_var) while (to != end) *(to++)+= *(from++); + + to_var->bytes_received+= from_var->bytes_received; + to_var->bytes_sent+= from_var->bytes_sent; } /* @@ -1097,6 +1103,9 @@ void add_diff_to_status(STATUS_VAR *to_var, STATUS_VAR *from_var, while (to != end) *(to++)+= *(from++) - *(dec++); + + to_var->bytes_received+= from_var->bytes_received - dec_var->bytes_received;; + to_var->bytes_sent+= from_var->bytes_sent - dec_var->bytes_sent; } #define SECONDS_TO_WAIT_FOR_KILL 2 @@ -1303,6 +1312,7 @@ void THD::cleanup_after_query() where= THD::DEFAULT_WHERE; /* reset table map for multi-table update */ table_map_for_update= 0; + clean_current_user_used(); } @@ -3336,6 +3346,22 @@ void THD::set_query(char *query_arg, uint32 query_length_arg) pthread_mutex_unlock(&LOCK_thd_data); } +void THD::get_definer(LEX_USER *definer) +{ + set_current_user_used(); +#if !defined(MYSQL_CLIENT) && defined(HAVE_REPLICATION) + if (slave_thread && has_invoker()) + { + definer->user = invoker_user; + definer->host= invoker_host; + definer->password.str= NULL; + definer->password.length= 0; + } + else +#endif + get_default_definer(this, definer); +} + /** Mark transaction to rollback and mark error as fatal to a sub-statement. @@ -3434,9 +3460,13 @@ bool xid_cache_insert(XID *xid, enum xa_states xa_state) bool xid_cache_insert(XID_STATE *xid_state) { pthread_mutex_lock(&LOCK_xid_cache); - DBUG_ASSERT(hash_search(&xid_cache, xid_state->xid.key(), - xid_state->xid.key_length())==0); - my_bool res=my_hash_insert(&xid_cache, (uchar*)xid_state); + if (hash_search(&xid_cache, xid_state->xid.key(), xid_state->xid.key_length())) + { + pthread_mutex_unlock(&LOCK_xid_cache); + my_error(ER_XAER_DUPID, MYF(0)); + return TRUE; + } + my_bool res= my_hash_insert(&xid_cache, (uchar*)xid_state); pthread_mutex_unlock(&LOCK_xid_cache); return res; } diff --git a/sql/sql_class.h b/sql/sql_class.h index fa802fea7ad..7f3be97fe91 100644 --- a/sql/sql_class.h +++ b/sql/sql_class.h @@ -86,9 +86,10 @@ enum enum_ha_read_modes { RFIRST, RNEXT, RPREV, RLAST, RKEY, RNEXT_SAME }; enum enum_duplicates { DUP_ERROR, DUP_REPLACE, DUP_UPDATE }; enum enum_delay_key_write { DELAY_KEY_WRITE_NONE, DELAY_KEY_WRITE_ON, DELAY_KEY_WRITE_ALL }; -enum enum_slave_exec_mode { SLAVE_EXEC_MODE_STRICT, - SLAVE_EXEC_MODE_IDEMPOTENT, - SLAVE_EXEC_MODE_LAST_BIT}; + +#define SLAVE_EXEC_MODE_STRICT (1U << 0) +#define SLAVE_EXEC_MODE_IDEMPOTENT (1U << 1) + enum enum_mark_columns { MARK_COLUMNS_NONE, MARK_COLUMNS_READ, MARK_COLUMNS_WRITE}; @@ -415,12 +416,14 @@ struct system_variables }; -/* per thread status variables */ +/** + Per thread status variables. + Must be long/ulong up to last_system_status_var so that + add_to_status/add_diff_to_status can work. +*/ typedef struct system_status_var { - ulonglong bytes_received; - ulonglong bytes_sent; ulong com_other; ulong com_stat[(uint) SQLCOM_END]; ulong created_tmp_disk_tables; @@ -476,13 +479,14 @@ typedef struct system_status_var Number of statements sent from the client */ ulong questions; + + ulonglong bytes_received; + ulonglong bytes_sent; /* IMPORTANT! SEE last_system_status_var DEFINITION BELOW. - Below 'last_system_status_var' are all variables which doesn't make any - sense to add to the /global/ status variable counter. - Status variables which it does not make sense to add to - global status variable counter + Below 'last_system_status_var' are all variables that cannot be handled + automatically by add_to_status()/add_diff_to_status(). */ double last_query_cost; } STATUS_VAR; @@ -2360,6 +2364,18 @@ public: Protected with LOCK_thd_data mutex. */ void set_query(char *query_arg, uint32 query_length_arg); + void set_current_user_used() { current_user_used= TRUE; } + bool is_current_user_used() { return current_user_used; } + void clean_current_user_used() { current_user_used= FALSE; } + void get_definer(LEX_USER *definer); + void set_invoker(const LEX_STRING *user, const LEX_STRING *host) + { + invoker_user= *user; + invoker_host= *host; + } + LEX_STRING get_invoker_user() { return invoker_user; } + LEX_STRING get_invoker_host() { return invoker_host; } + bool has_invoker() { return invoker_user.length > 0; } private: /** The current internal error handler for this thread, or NULL. */ Internal_error_handler *m_internal_handler; @@ -2379,6 +2395,25 @@ private: tree itself is reused between executions and thus is stored elsewhere. */ MEM_ROOT main_mem_root; + + /** + It will be set TURE if CURRENT_USER() is called in account management + statements or default definer is set in CREATE/ALTER SP, SF, Event, + TRIGGER or VIEW statements. + + Current user will be binlogged into Query_log_event if current_user_used + is TRUE; It will be stored into invoker_host and invoker_user by SQL thread. + */ + bool current_user_used; + + /** + It points to the invoker in the Query_log_event. + SQL thread use it as the default definer in CREATE/ALTER SP, SF, Event, + TRIGGER or VIEW statements or current user in account management + statements if it is not NULL. + */ + LEX_STRING invoker_user; + LEX_STRING invoker_host; }; @@ -2438,7 +2473,7 @@ class select_result :public Sql_alloc { protected: THD *thd; SELECT_LEX_UNIT *unit; - uint nest_level; + int nest_level; public: select_result(); virtual ~select_result() {}; @@ -2579,7 +2614,7 @@ public: Creates a select_export to represent INTO OUTFILE <filename> with a defined level of subquery nesting. */ - select_export(sql_exchange *ex, uint nest_level_arg) :select_to_file(ex) + select_export(sql_exchange *ex, int nest_level_arg) :select_to_file(ex) { nest_level= nest_level_arg; } @@ -2596,7 +2631,7 @@ public: Creates a select_export to represent INTO DUMPFILE <filename> with a defined level of subquery nesting. */ - select_dump(sql_exchange *ex, uint nest_level_arg) : + select_dump(sql_exchange *ex, int nest_level_arg) : select_to_file(ex) { nest_level= nest_level_arg; @@ -3073,7 +3108,7 @@ public: Creates a select_dumpvar to represent INTO <variable> with a defined level of subquery nesting. */ - select_dumpvar(uint nest_level_arg) + select_dumpvar(int nest_level_arg) { var_list.empty(); row_count= 0; diff --git a/sql/sql_cursor.cc b/sql/sql_cursor.cc index 6f61dc40f66..d7d029d28d4 100644 --- a/sql/sql_cursor.cc +++ b/sql/sql_cursor.cc @@ -658,7 +658,12 @@ void Materialized_cursor::fetch(ulong num_rows) if ((res= table->file->rnd_next(table->record[0]))) break; /* Send data only if the read was successful. */ - result->send_data(item_list); + /* + If network write failed (i.e. due to a closed socked), + the error has already been set. Just return. + */ + if (result->send_data(item_list)) + return; } switch (res) { diff --git a/sql/sql_insert.cc b/sql/sql_insert.cc index 6b5d60d8613..e23f2f86b82 100644 --- a/sql/sql_insert.cc +++ b/sql/sql_insert.cc @@ -3913,6 +3913,17 @@ void select_create::abort() if (table) { + if (thd->lex->sql_command == SQLCOM_CREATE_TABLE && + thd->current_stmt_binlog_row_based && + !(thd->lex->create_info.options & HA_LEX_CREATE_TMP_TABLE) && + mysql_bin_log.is_open()) + { + /* + This should be removed after BUG#47899. + */ + mysql_bin_log.reset_gathered_updates(thd); + } + table->file->extra(HA_EXTRA_NO_IGNORE_DUP_KEY); table->file->extra(HA_EXTRA_WRITE_CANNOT_REPLACE); if (!create_info->table_existed) diff --git a/sql/sql_lex.cc b/sql/sql_lex.cc index 1cedb0c41db..028ab18bc36 100644 --- a/sql/sql_lex.cc +++ b/sql/sql_lex.cc @@ -111,7 +111,7 @@ st_parsing_options::reset() } -bool Lex_input_stream::init(THD *thd, const char *buff, unsigned int length) +bool Lex_input_stream::init(THD *thd, char *buff, unsigned int length) { DBUG_EXECUTE_IF("bug42064_simulate_oom", DBUG_SET("+d,simulate_out_of_memory");); @@ -1302,11 +1302,10 @@ int MYSQLlex(void *arg, void *yythd) ulong version; version=strtol(version_str, NULL, 10); - /* Accept 'M' 'm' 'm' 'd' 'd' */ - lip->yySkipn(5); - if (version <= MYSQL_VERSION_ID) { + /* Accept 'M' 'm' 'm' 'd' 'd' */ + lip->yySkipn(5); /* Expand the content of the special comment as real code */ lip->set_echo(TRUE); state=MY_LEX_START; @@ -1314,7 +1313,16 @@ int MYSQLlex(void *arg, void *yythd) } else { + /* + Patch and skip the conditional comment to avoid it + being propagated infinitely (eg. to a slave). + */ + char *pcom= lip->yyUnput(' '); comment_closed= ! consume_comment(lip, 1); + if (! comment_closed) + { + *pcom= '!'; + } /* version allowed to have one level of comment inside. */ } } diff --git a/sql/sql_lex.h b/sql/sql_lex.h index 6372970438d..87d6403710a 100644 --- a/sql/sql_lex.h +++ b/sql/sql_lex.h @@ -1179,7 +1179,7 @@ public: @retval FALSE OK @retval TRUE Error */ - bool init(THD *thd, const char *buff, unsigned int length); + bool init(THD *thd, char *buff, unsigned int length); /** Set the echo mode. @@ -1294,6 +1294,20 @@ public: } /** + Puts a character back into the stream, canceling + the effect of the last yyGet() or yySkip(). + Note that the echo mode should not change between calls + to unput, get, or skip from the stream. + */ + char *yyUnput(char ch) + { + *--m_ptr= ch; + if (m_echo) + m_cpp_ptr--; + return m_ptr; + } + + /** End of file indicator for the query text to parse. @return true if there are no more characters to parse */ @@ -1439,7 +1453,7 @@ public: private: /** Pointer to the current position in the raw input stream. */ - const char *m_ptr; + char *m_ptr; /** Starting position of the last token parsed, in the raw buffer. */ const char *m_tok_start; @@ -1971,7 +1985,7 @@ public: @retval FALSE OK @retval TRUE Error */ - bool init(THD *thd, const char *buff, unsigned int length) + bool init(THD *thd, char *buff, unsigned int length) { return m_lip.init(thd, buff, length); } diff --git a/sql/sql_list.h b/sql/sql_list.h index 15eb85ab52c..dc840cefc66 100644 --- a/sql/sql_list.h +++ b/sql/sql_list.h @@ -74,7 +74,7 @@ public: SQL_I_List() { empty(); } - SQL_I_List(const SQL_I_List &tmp) :Sql_alloc() + SQL_I_List(const SQL_I_List &tmp) : Sql_alloc() { elements= tmp.elements; first= tmp.first; diff --git a/sql/sql_load.cc b/sql/sql_load.cc index d0506d75925..1b7f690259a 100644 --- a/sql/sql_load.cc +++ b/sql/sql_load.cc @@ -128,6 +128,7 @@ int mysql_load(THD *thd,sql_exchange *ex,TABLE_LIST *table_list, bool is_fifo=0; #ifndef EMBEDDED_LIBRARY LOAD_FILE_INFO lf_info; + THD::killed_state killed_status; #endif char *db = table_list->db; // This is never null /* @@ -138,7 +139,6 @@ int mysql_load(THD *thd,sql_exchange *ex,TABLE_LIST *table_list, char *tdb= thd->db ? thd->db : db; // Result is never null ulong skip_lines= ex->skip_lines; bool transactional_table; - THD::killed_state killed_status= THD::NOT_KILLED; DBUG_ENTER("mysql_load"); #ifdef EMBEDDED_LIBRARY @@ -455,7 +455,11 @@ int mysql_load(THD *thd,sql_exchange *ex,TABLE_LIST *table_list, error=1; thd->killed= THD::KILL_QUERY; };); - killed_status= (error == 0)? THD::NOT_KILLED : thd->killed; + +#ifndef EMBEDDED_LIBRARY + killed_status= (error == 0) ? THD::NOT_KILLED : thd->killed; +#endif + /* We must invalidate the table in query cache before binlog writing and ha_autocommit_... @@ -1202,29 +1206,6 @@ int READ_INFO::read_field() while ( to < end_of_buff) { chr = GET; -#ifdef USE_MB - if ((my_mbcharlen(read_charset, chr) > 1) && - to+my_mbcharlen(read_charset, chr) <= end_of_buff) - { - uchar* p = (uchar*)to; - *to++ = chr; - int ml = my_mbcharlen(read_charset, chr); - int i; - for (i=1; i<ml; i++) { - chr = GET; - if (chr == my_b_EOF) - goto found_eof; - *to++ = chr; - } - if (my_ismbchar(read_charset, - (const char *)p, - (const char *)to)) - continue; - for (i=0; i<ml; i++) - PUSH((uchar) *--to); - chr = GET; - } -#endif if (chr == my_b_EOF) goto found_eof; if (chr == escape_char) @@ -1309,6 +1290,39 @@ int READ_INFO::read_field() return 0; } } +#ifdef USE_MB + if (my_mbcharlen(read_charset, chr) > 1 && + to + my_mbcharlen(read_charset, chr) <= end_of_buff) + { + uchar* p= (uchar*) to; + int ml, i; + *to++ = chr; + + ml= my_mbcharlen(read_charset, chr); + + for (i= 1; i < ml; i++) + { + chr= GET; + if (chr == my_b_EOF) + { + /* + Need to back up the bytes already ready from illformed + multi-byte char + */ + to-= i; + goto found_eof; + } + *to++ = chr; + } + if (my_ismbchar(read_charset, + (const char *)p, + (const char *)to)) + continue; + for (i= 0; i < ml; i++) + PUSH((uchar) *--to); + chr= GET; + } +#endif *to++ = (uchar) chr; } /* diff --git a/sql/sql_parse.cc b/sql/sql_parse.cc index 572a8686c4d..fddd8626a19 100644 --- a/sql/sql_parse.cc +++ b/sql/sql_parse.cc @@ -473,6 +473,7 @@ static void handle_bootstrap_impl(THD *thd) buff= (char*) thd->net.buff; if (!fgets(buff + length, thd->net.max_packet - length, file)) { + net_end_statement(thd); bootstrap_error= 1; break; } @@ -1597,13 +1598,13 @@ bool dispatch_command(enum enum_server_command command, THD *thd, (sf_malloc_max_memory+1023L)/1024L); } #endif -#ifdef EMBEDDED_LIBRARY - /* Store the buffer in permanent memory */ - my_ok(thd, 0, 0, buff); -#else +#ifndef EMBEDDED_LIBRARY VOID(my_net_write(net, (uchar*) buff, length)); VOID(net_flush(net)); thd->main_da.disable_status(); +#else + /* Store the buffer in permanent memory */ + my_ok(thd, 0, 0, buff); #endif break; } @@ -4792,7 +4793,7 @@ create_sp_error: my_error(ER_XAER_NOTA, MYF(0)); break; } - thd->transaction.xid_state.xa_state=XA_ACTIVE; + thd->transaction.xid_state.xa_state= XA_ACTIVE; my_ok(thd); break; } @@ -4812,16 +4813,16 @@ create_sp_error: my_error(ER_XAER_OUTSIDE, MYF(0)); break; } - if (xid_cache_search(thd->lex->xid)) - { - my_error(ER_XAER_DUPID, MYF(0)); - break; - } DBUG_ASSERT(thd->transaction.xid_state.xid.is_null()); - thd->transaction.xid_state.xa_state=XA_ACTIVE; + thd->transaction.xid_state.xa_state= XA_ACTIVE; thd->transaction.xid_state.rm_error= 0; thd->transaction.xid_state.xid.set(thd->lex->xid); - xid_cache_insert(&thd->transaction.xid_state); + if (xid_cache_insert(&thd->transaction.xid_state)) + { + thd->transaction.xid_state.xa_state= XA_NOTR; + thd->transaction.xid_state.xid.null(); + break; + } thd->transaction.all.modified_non_trans_table= FALSE; thd->options= ((thd->options & ~(OPTION_KEEP_LOG)) | OPTION_BEGIN); thd->server_status|= SERVER_STATUS_IN_TRANS; @@ -4875,6 +4876,16 @@ create_sp_error: case SQLCOM_XA_COMMIT: if (!thd->transaction.xid_state.xid.eq(thd->lex->xid)) { + /* + xid_state.in_thd is always true beside of xa recovery + procedure. Note, that there is no race condition here + between xid_cache_search and xid_cache_delete, since we're always + deleting our own XID (thd->lex->xid == thd->transaction.xid_state.xid). + The only case when thd->lex->xid != thd->transaction.xid_state.xid + and xid_state->in_thd == 0 is in ha_recover() functionality, + which is called before starting client connections, and thus is + always single-threaded. + */ XID_STATE *xs=xid_cache_search(thd->lex->xid); if (!xs || xs->in_thd) my_error(ER_XAER_NOTA, MYF(0)); @@ -5995,13 +6006,13 @@ void mysql_init_multi_delete(LEX *lex) Parse a query. @param thd Current thread - @param inBuf Begining of the query text + @param rawbuf Begining of the query text @param length Length of the query text @param[out] found_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, +void mysql_parse(THD *thd, char *rawbuf, uint length, const char ** found_semicolon) { DBUG_ENTER("mysql_parse"); @@ -6027,7 +6038,7 @@ void mysql_parse(THD *thd, const char *inBuf, uint length, lex_start(thd); mysql_reset_thd_for_next_command(thd); - if (query_cache_send_result_to_client(thd, (char*) inBuf, length) <= 0) + if (query_cache_send_result_to_client(thd, rawbuf, length) <= 0) { LEX *lex= thd->lex; @@ -6036,7 +6047,7 @@ void mysql_parse(THD *thd, const char *inBuf, uint length, Parser_state parser_state; bool err; - if (!(err= parser_state.init(thd, inBuf, length))) + if (!(err= parser_state.init(thd, rawbuf, length))) { err= parse_sql(thd, & parser_state, NULL); *found_semicolon= parser_state.m_lip.found_semicolon; @@ -6122,14 +6133,14 @@ void mysql_parse(THD *thd, const char *inBuf, uint length, 1 can be ignored */ -bool mysql_test_parse_for_slave(THD *thd, char *inBuf, uint length) +bool mysql_test_parse_for_slave(THD *thd, char *rawbuf, uint length) { LEX *lex= thd->lex; bool error= 0; DBUG_ENTER("mysql_test_parse_for_slave"); Parser_state parser_state; - if (!(error= parser_state.init(thd, inBuf, length))) + if (!(error= parser_state.init(thd, rawbuf, length))) { lex_start(thd); mysql_reset_thd_for_next_command(thd); @@ -7718,7 +7729,7 @@ LEX_USER *create_default_definer(THD *thd) if (! (definer= (LEX_USER*) thd->alloc(sizeof(LEX_USER)))) return 0; - get_default_definer(thd, definer); + thd->get_definer(definer); return definer; } diff --git a/sql/sql_partition.cc b/sql/sql_partition.cc index 0e717078422..4ff23713daa 100644 --- a/sql/sql_partition.cc +++ b/sql/sql_partition.cc @@ -3876,7 +3876,7 @@ void get_partition_set(const TABLE *table, uchar *buf, const uint index, */ bool mysql_unpack_partition(THD *thd, - const char *part_buf, uint part_info_len, + char *part_buf, uint part_info_len, const char *part_state, uint part_state_len, TABLE* table, bool is_create_table_ind, handlerton *default_db_type, diff --git a/sql/sql_partition.h b/sql/sql_partition.h index b9efbf25a00..02a5ead1117 100644 --- a/sql/sql_partition.h +++ b/sql/sql_partition.h @@ -78,7 +78,7 @@ void get_full_part_id_from_key(const TABLE *table, uchar *buf, KEY *key_info, const key_range *key_spec, part_id_range *part_spec); -bool mysql_unpack_partition(THD *thd, const char *part_buf, +bool mysql_unpack_partition(THD *thd, char *part_buf, uint part_info_len, const char *part_state, uint part_state_len, TABLE *table, bool is_create_table_ind, diff --git a/sql/sql_prepare.cc b/sql/sql_prepare.cc index 4de7671bd1a..f5594dfe520 100644 --- a/sql/sql_prepare.cc +++ b/sql/sql_prepare.cc @@ -263,8 +263,11 @@ static bool send_prep_stmt(Prepared_statement *stmt, uint columns) &stmt->lex->param_list, Protocol::SEND_EOF); } - /* Flag that a response has already been sent */ - thd->main_da.disable_status(); + + if (!error) + /* Flag that a response has already been sent */ + thd->main_da.disable_status(); + DBUG_RETURN(error); } #else @@ -790,7 +793,7 @@ static bool insert_params_with_log(Prepared_statement *stmt, uchar *null_array, type (the types are supplied at execute). Check that the supplied type of placeholder can accept a data stream. */ - else if (!is_param_long_data_type(param)) + else if (! is_param_long_data_type(param)) DBUG_RETURN(1); res= param->query_val_str(&str); if (param->convert_str_value(thd)) @@ -836,7 +839,7 @@ static bool insert_params(Prepared_statement *stmt, uchar *null_array, type (the types are supplied at execute). Check that the supplied type of placeholder can accept a data stream. */ - else if (is_param_long_data_type(param)) + else if (! is_param_long_data_type(param)) DBUG_RETURN(1); if (param->convert_str_value(stmt->thd)) DBUG_RETURN(1); /* out of memory */ diff --git a/sql/sql_repl.cc b/sql/sql_repl.cc index 1970287b06e..95e48c531be 100644 --- a/sql/sql_repl.cc +++ b/sql/sql_repl.cc @@ -218,8 +218,7 @@ bool log_in_use(const char* log_name) if ((linfo = tmp->current_linfo)) { pthread_mutex_lock(&linfo->lock); - result = !bcmp((uchar*) log_name, (uchar*) linfo->log_file_name, - log_name_len); + result = !memcmp(log_name, linfo->log_file_name, log_name_len); pthread_mutex_unlock(&linfo->lock); if (result) break; @@ -357,6 +356,7 @@ void mysql_binlog_send(THD* thd, char* log_ident, my_off_t pos, #ifndef DBUG_OFF int left_events = max_binlog_dump_events; #endif + int old_max_allowed_packet= thd->variables.max_allowed_packet; DBUG_ENTER("mysql_binlog_send"); DBUG_PRINT("enter",("log_ident: '%s' pos: %ld", log_ident, (long) pos)); @@ -762,6 +762,7 @@ end: pthread_mutex_lock(&LOCK_thread_count); thd->current_linfo = 0; pthread_mutex_unlock(&LOCK_thread_count); + thd->variables.max_allowed_packet= old_max_allowed_packet; DBUG_VOID_RETURN; err: @@ -779,6 +780,7 @@ err: pthread_mutex_unlock(&LOCK_thread_count); if (file >= 0) (void) my_close(file, MYF(MY_WME)); + thd->variables.max_allowed_packet= old_max_allowed_packet; my_message(my_errno, errmsg, MYF(0)); DBUG_VOID_RETURN; @@ -1419,6 +1421,7 @@ bool mysql_show_binlog_events(THD* thd) bool ret = TRUE; IO_CACHE log; File file = -1; + int old_max_allowed_packet= thd->variables.max_allowed_packet; DBUG_ENTER("mysql_show_binlog_events"); Log_event::init_show_field_list(&field_list); @@ -1557,6 +1560,7 @@ err: pthread_mutex_lock(&LOCK_thread_count); thd->current_linfo = 0; pthread_mutex_unlock(&LOCK_thread_count); + thd->variables.max_allowed_packet= old_max_allowed_packet; DBUG_RETURN(ret); } diff --git a/sql/sql_select.cc b/sql/sql_select.cc index 50fe78492c0..f7da7413eef 100644 --- a/sql/sql_select.cc +++ b/sql/sql_select.cc @@ -1128,6 +1128,30 @@ JOIN::optimize() { conds=new Item_int((longlong) 0,1); // Always false } + + /* + It's necessary to check const part of HAVING cond as there is a + chance that some cond parts may become const items after + make_join_statistics() (for example when Item is a reference to + cost table field from outer join). + + This check is performed only for those conditions which do not use + aggregate functions. In such case temporary table may not be used + and const condition elements may be lost during further having + condition transformation in JOIN::exec. + */ + if (having && const_table_map && !having->with_sum_func) + { + having->update_used_tables(); + having= remove_eq_conds(thd, having, &having_value); + if (having_value == Item::COND_FALSE) + { + having= new Item_int((longlong) 0,1); + zero_result_cause= "Impossible HAVING noticed after reading const tables"; + DBUG_RETURN(0); + } + } + if (make_join_select(this, select, conds)) { zero_result_cause= @@ -11880,6 +11904,7 @@ flush_cached_records(JOIN *join,JOIN_TAB *join_tab,bool skip_last) enum_nested_loop_state rc= NESTED_LOOP_OK; int error; READ_RECORD *info; + SQL_SELECT *select; join_tab->table->null_row= 0; if (!join_tab->cache.records) @@ -11894,7 +11919,7 @@ flush_cached_records(JOIN *join,JOIN_TAB *join_tab,bool skip_last) join_tab->select->quick=0; } } - /* read through all records */ + /* read through all records */ if ((error=join_init_read_record(join_tab))) { reset_cache_write(&join_tab->cache); @@ -11908,22 +11933,26 @@ flush_cached_records(JOIN *join,JOIN_TAB *join_tab,bool skip_last) } info= &join_tab->read_record; + select= join_tab->select; + do { + int err= 0; if (join->thd->killed) { join->thd->send_kill_message(); return NESTED_LOOP_KILLED; // Aborted by user /* purecov: inspected */ } - int err= 0; - SQL_SELECT *select=join_tab->select; if (rc == NESTED_LOOP_OK && (!join_tab->cache.select || (err= join_tab->cache.select->skip_record(join->thd)) != 0 )) { if (err < 0) + { + reset_cache_write(&join_tab->cache); return NESTED_LOOP_ERROR; - rc= NESTED_LOOP_OK; + } + reset_cache_read(&join_tab->cache); for (uint i= (join_tab->cache.records- (skip_last ? 1 : 0)) ; i-- > 0 ;) { @@ -11932,7 +11961,10 @@ flush_cached_records(JOIN *join,JOIN_TAB *join_tab,bool skip_last) if (!select || (err= select->skip_record(join->thd)) != 0) { if (err < 0) + { + reset_cache_write(&join_tab->cache); return NESTED_LOOP_ERROR; + } rc= (join_tab->next_select)(join,join_tab+1,0); if (rc != NESTED_LOOP_OK && rc != NESTED_LOOP_NO_MORE_ROWS) { diff --git a/sql/sql_show.cc b/sql/sql_show.cc index b79e01f5ef4..30d6b13b0c9 100644 --- a/sql/sql_show.cc +++ b/sql/sql_show.cc @@ -1,4 +1,4 @@ -/* Copyright 2000-2008 MySQL AB, 2008 Sun Microsystems, Inc. +/* Copyright 2000, 2010 Oracle and/or its affiliates. All rights reserved. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -9,9 +9,9 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ /* Function with list databases, tables or fields */ @@ -480,8 +480,6 @@ find_files(THD *thd, List<LEX_STRING> *files, const char *db, wild_length= strlen(wild); } - - bzero((char*) &table_list,sizeof(table_list)); if (!(dirp = my_dir(path,MYF(dir ? MY_WANT_STAT : 0)))) @@ -523,9 +521,20 @@ find_files(THD *thd, List<LEX_STRING> *files, const char *db, continue; file_name_len= filename_to_tablename(file->name, uname, sizeof(uname)); - if (wild && wild_compare(uname, wild, 0)) - continue; - if (!(file_name= + if (wild) + { + if (lower_case_table_names) + { + if (my_wildcmp(files_charset_info, + uname, uname + file_name_len, + wild, wild + wild_length, + wild_prefix, wild_one, wild_many)) + continue; + } + else if (wild_compare(uname, wild, 0)) + continue; + } + if (!(file_name= thd->make_lex_string(file_name, uname, file_name_len, TRUE))) { my_dirend(dirp); @@ -2221,8 +2230,8 @@ static bool show_status_array(THD *thd, const char *wild, bool ucase_names, COND *cond) { - MY_ALIGNED_BYTE_ARRAY(buff_data, SHOW_VAR_FUNC_BUFF_SIZE, long); - char * const buff= (char *) &buff_data; + my_aligned_storage<SHOW_VAR_FUNC_BUFF_SIZE, MY_ALIGNOF(long)> buffer; + char * const buff= buffer.data; char *prefix_end; /* the variable name should not be longer than 64 characters */ char name_buffer[64]; @@ -2707,36 +2716,54 @@ bool get_lookup_field_values(THD *thd, COND *cond, TABLE_LIST *tables, { LEX *lex= thd->lex; const char *wild= lex->wild ? lex->wild->ptr() : NullS; + bool rc= 0; + bzero((char*) lookup_field_values, sizeof(LOOKUP_FIELD_VALUES)); switch (lex->sql_command) { case SQLCOM_SHOW_DATABASES: if (wild) { - lookup_field_values->db_value.str= (char*) wild; - lookup_field_values->db_value.length= strlen(wild); + thd->make_lex_string(&lookup_field_values->db_value, + wild, strlen(wild), 0); lookup_field_values->wild_db_value= 1; } - return 0; + break; case SQLCOM_SHOW_TABLES: case SQLCOM_SHOW_TABLE_STATUS: case SQLCOM_SHOW_TRIGGERS: case SQLCOM_SHOW_EVENTS: - lookup_field_values->db_value.str= lex->select_lex.db; - lookup_field_values->db_value.length=strlen(lex->select_lex.db); + thd->make_lex_string(&lookup_field_values->db_value, + lex->select_lex.db, strlen(lex->select_lex.db), 0); if (wild) { - lookup_field_values->table_value.str= (char*)wild; - lookup_field_values->table_value.length= strlen(wild); + thd->make_lex_string(&lookup_field_values->table_value, + wild, strlen(wild), 0); lookup_field_values->wild_table_value= 1; } - return 0; + break; default: /* The "default" is for queries over I_S. All previous cases handle SHOW commands. */ - return calc_lookup_values_from_cond(thd, cond, tables, lookup_field_values); + rc= calc_lookup_values_from_cond(thd, cond, tables, lookup_field_values); + break; } + + if (lower_case_table_names && !rc) + { + /* + We can safely do in-place upgrades here since all of the above cases + are allocating a new memory buffer for these strings. + */ + if (lookup_field_values->db_value.str && lookup_field_values->db_value.str[0]) + my_casedn_str(system_charset_info, lookup_field_values->db_value.str); + if (lookup_field_values->table_value.str && + lookup_field_values->table_value.str[0]) + my_casedn_str(system_charset_info, lookup_field_values->table_value.str); + } + + return rc; } @@ -2938,11 +2965,15 @@ make_table_name_list(THD *thd, List<LEX_STRING> *table_names, LEX *lex, { if (with_i_schema) { + LEX_STRING *name; ST_SCHEMA_TABLE *schema_table= find_schema_table(thd, lookup_field_vals->table_value.str); if (schema_table && !schema_table->hidden) { - if (table_names->push_back(&lookup_field_vals->table_value)) + if (!(name= + thd->make_lex_string(NULL, schema_table->table_name, + strlen(schema_table->table_name), TRUE)) || + table_names->push_back(name)) return 1; } } @@ -2981,7 +3012,7 @@ make_table_name_list(THD *thd, List<LEX_STRING> *table_names, LEX *lex, */ if (res == FIND_FILES_DIR) { - if (lex->sql_command != SQLCOM_SELECT) + if (sql_command_flags[lex->sql_command] & CF_STATUS_COMMAND) return 1; thd->clear_error(); return 2; @@ -3341,6 +3372,7 @@ int get_all_tables(THD *thd, TABLE_LIST *tables, COND *cond) error= 0; goto err; } + DBUG_PRINT("INDEX VALUES",("db_name='%s', table_name='%s'", STR_OR_NIL(lookup_field_vals.db_value.str), STR_OR_NIL(lookup_field_vals.table_value.str))); @@ -3868,7 +3900,6 @@ static int get_schema_column_record(THD *thd, TABLE_LIST *tables, uint flags=field->flags; char tmp[MAX_FIELD_WIDTH]; String type(tmp,sizeof(tmp), system_charset_info); - char *end; int decimals, field_length; if (wild && wild[0] && @@ -3889,7 +3920,7 @@ static int get_schema_column_record(THD *thd, TABLE_LIST *tables, field->field_name) & COL_ACLS; if (!tables->schema_table && !col_access) continue; - end= tmp; + char *end= tmp; for (uint bitnr=0; col_access ; col_access>>=1,bitnr++) { if (col_access & 1) @@ -3965,10 +3996,13 @@ static int get_schema_column_record(THD *thd, TABLE_LIST *tables, case MYSQL_TYPE_TINY: case MYSQL_TYPE_SHORT: case MYSQL_TYPE_LONG: - case MYSQL_TYPE_LONGLONG: case MYSQL_TYPE_INT24: field_length= field->max_display_length() - 1; break; + case MYSQL_TYPE_LONGLONG: + field_length= field->max_display_length() - + ((field->flags & UNSIGNED_FLAG) ? 0 : 1); + break; case MYSQL_TYPE_BIT: field_length= field->max_display_length(); decimals= -1; // return NULL @@ -4012,7 +4046,6 @@ static int get_schema_column_record(THD *thd, TABLE_LIST *tables, table->field[15]->store((const char*) pos, strlen((const char*) pos), cs); - end= tmp; if (field->unireg_check == Field::NEXT_NUMBER) table->field[16]->store(STRING_WITH_LEN("auto_increment"), cs); if (show_table->timestamp_field == field && diff --git a/sql/sql_table.cc b/sql/sql_table.cc index 88bcd092dfa..1d3bb23cc3d 100644 --- a/sql/sql_table.cc +++ b/sql/sql_table.cc @@ -6883,6 +6883,14 @@ view_err: if (!error && (new_name != table_name || new_db != db)) { thd_proc_info(thd, "rename"); + + /* + Workaround InnoDB ending the transaction when the table instance + is unlocked/closed (close_cached_table below), otherwise the trx + state will differ between the server and storage engine layers. + */ + ha_autocommit_or_rollback(thd, 0); + /* Then do a 'simple' rename of the table. First we need to close all instances of 'source' table. @@ -7430,6 +7438,11 @@ view_err: mysql_unlock_tables(thd, thd->lock); thd->lock=0; } + /* + If LOCK TABLES list is not empty and contains this table, + unlock the table and remove the table from this list. + */ + mysql_lock_remove(thd, thd->locked_tables, table, FALSE); /* Remove link to old table and rename the new one */ close_temporary_table(thd, table, 1, 1); /* Should pass the 'new_name' as we store table name in the cache */ diff --git a/sql/sql_update.cc b/sql/sql_update.cc index fc45b3d31a2..f6165b8d4b2 100644 --- a/sql/sql_update.cc +++ b/sql/sql_update.cc @@ -467,11 +467,10 @@ int mysql_update(THD *thd, thd_proc_info(thd, "Searching rows for update"); ha_rows tmp_limit= limit; - while (!(error=info.read_record(&info)) && - !thd->killed && !thd->is_error()) + while (!(error=info.read_record(&info)) && !thd->killed) { thd->examined_row_count++; - if (!select || select->skip_record(thd) > 0) + if (!select || (error= select->skip_record(thd)) > 0) { if (table->file->was_semi_consistent_read()) continue; /* repeat the read of the same row if it still exists */ @@ -490,7 +489,15 @@ int mysql_update(THD *thd, } } else + { table->file->unlock_row(); + if (error < 0) + { + /* Fatal error from select->skip_record() */ + error= 1; + break; + } + } } if (thd->killed && !error) error= 1; // Aborted diff --git a/sql/table.h b/sql/table.h index 0a25ee75d4e..d0836bf5b78 100644 --- a/sql/table.h +++ b/sql/table.h @@ -55,7 +55,6 @@ typedef struct st_order { struct st_order *next; Item **item; /* Point at item in select fields */ Item *item_ptr; /* Storage for initial item */ - Item **item_copy; /* For SPs; the original item ptr */ int counter; /* position in SELECT list, correct only if counter_used is true*/ bool asc; /* true if ascending */ @@ -451,7 +450,7 @@ typedef struct st_table_share #ifdef WITH_PARTITION_STORAGE_ENGINE /** @todo: Move into *ha_data for partitioning */ bool auto_partitioned; - const char *partition_info; + char *partition_info; uint partition_info_len; uint partition_info_buffer_size; const char *part_state; diff --git a/sql/udf_example.c b/sql/udf_example.c index 788582e32b6..979aebd4b23 100644 --- a/sql/udf_example.c +++ b/sql/udf_example.c @@ -1067,7 +1067,7 @@ char *myfunc_argument_name(UDF_INIT *initid __attribute__((unused)), { if (!args->attributes[0]) { - null_value= 0; + *null_value= 1; return 0; } (*length)--; /* space for ending \0 (for debugging purposes) */ diff --git a/sql/unireg.h b/sql/unireg.h index 4fbde3b8631..88a5ca5c12f 100644 --- a/sql/unireg.h +++ b/sql/unireg.h @@ -129,8 +129,8 @@ #define SPECIAL_LOG_QUERIES_NOT_USING_INDEXES 4096 /* Obsolete */ /* Extern defines */ -#define store_record(A,B) bmove_align((A)->B,(A)->record[0],(size_t) (A)->s->reclength) -#define restore_record(A,B) bmove_align((A)->record[0],(A)->B,(size_t) (A)->s->reclength) +#define store_record(A,B) memcpy((A)->B,(A)->record[0],(size_t) (A)->s->reclength) +#define restore_record(A,B) memcpy((A)->record[0],(A)->B,(size_t) (A)->s->reclength) #define cmp_record(A,B) memcmp((A)->record[0],(A)->B,(size_t) (A)->s->reclength) #define empty_record(A) { \ restore_record((A),s->default_values); \ |