diff options
author | Sergei Golubchik <sergii@pisem.net> | 2013-01-31 09:48:19 +0100 |
---|---|---|
committer | Sergei Golubchik <sergii@pisem.net> | 2013-01-31 09:48:19 +0100 |
commit | ab83952f293ea46c00e421c81b81a394c9cae2b4 (patch) | |
tree | 6a3e995bd1330828a0e988d896bca218863fc74c /sql | |
parent | 055b62f404ee0a0463ee8ff98a0e24c083b95f1d (diff) | |
parent | 5267af5e9e5e601d4f4b1ef40730da1e36d38d9d (diff) | |
download | mariadb-git-ab83952f293ea46c00e421c81b81a394c9cae2b4.tar.gz |
10.0-base merge
Diffstat (limited to 'sql')
123 files changed, 3274 insertions, 1740 deletions
diff --git a/sql/CMakeLists.txt b/sql/CMakeLists.txt index 98f779a8b60..8c2b6c81755 100644 --- a/sql/CMakeLists.txt +++ b/sql/CMakeLists.txt @@ -299,7 +299,6 @@ IF(WIN32 AND MYSQLD_EXECUTABLE) COMMAND ${CMAKE_COMMAND} ${CONFIG_PARAM} -P ${CMAKE_CURRENT_BINARY_DIR}/create_initial_db.cmake WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/data - COMMAND ${CMAKE_COMMAND} -E touch ${CMAKE_CURRENT_BINARY_DIR}/initdb.dep DEPENDS mysqld ) ADD_CUSTOM_TARGET(initial_database @@ -326,11 +325,12 @@ IF(WIN32) ADD_CUSTOM_COMMAND(OUTPUT ${my_bootstrap_sql} COMMAND ${CMAKE_COMMAND} -E chdir ${CMAKE_SOURCE_DIR}/scripts - cmd /c copy mysql_system_tables.sql+mysql_system_tables_data.sql+fill_help_tables.sql ${native_outfile} + cmd /c copy mysql_system_tables.sql+mysql_system_tables_data.sql+fill_help_tables.sql+mysql_performance_tables.sql ${native_outfile} DEPENDS ${CMAKE_SOURCE_DIR}/scripts/mysql_system_tables.sql ${CMAKE_SOURCE_DIR}/scripts/mysql_system_tables_data.sql ${CMAKE_SOURCE_DIR}/scripts/fill_help_tables.sql + ${CMAKE_SOURCE_DIR}/scripts/mysql_performance_tables.sql ) ADD_CUSTOM_COMMAND( diff --git a/sql/debug_sync.cc b/sql/debug_sync.cc index 6c8eae82a47..25f028e5451 100644 --- a/sql/debug_sync.cc +++ b/sql/debug_sync.cc @@ -239,7 +239,8 @@ void debug_sync_init_thread(THD *thd) if (opt_debug_sync_timeout) { thd->debug_sync_control= (st_debug_sync_control*) - my_malloc(sizeof(st_debug_sync_control), MYF(MY_WME | MY_ZEROFILL)); + my_malloc(sizeof(st_debug_sync_control), + MYF(MY_WME | MY_ZEROFILL | MY_THREAD_SPECIFIC)); if (!thd->debug_sync_control) { /* diff --git a/sql/event_data_objects.cc b/sql/event_data_objects.cc index 7c7ab4df9ad..2e31d20d54e 100644 --- a/sql/event_data_objects.cc +++ b/sql/event_data_objects.cc @@ -209,7 +209,7 @@ Event_basic::Event_basic() { DBUG_ENTER("Event_basic::Event_basic"); /* init memory root */ - init_sql_alloc(&mem_root, 256, 512); + init_sql_alloc(&mem_root, 256, 512, MYF(0)); dbname.str= name.str= NULL; dbname.length= name.length= 0; time_zone= NULL; diff --git a/sql/event_db_repository.cc b/sql/event_db_repository.cc index 12d34580d3c..9a943d8bb59 100644 --- a/sql/event_db_repository.cc +++ b/sql/event_db_repository.cc @@ -410,7 +410,6 @@ Event_db_repository::index_read_for_db_for_i_s(THD *thd, TABLE *schema_table, TABLE *event_table, const char *db) { - int ret=0; CHARSET_INFO *scs= system_charset_info; KEY *key_info; uint key_len; @@ -420,7 +419,14 @@ Event_db_repository::index_read_for_db_for_i_s(THD *thd, TABLE *schema_table, DBUG_ENTER("Event_db_repository::index_read_for_db_for_i_s"); DBUG_PRINT("info", ("Using prefix scanning on PK")); - event_table->file->ha_index_init(0, 1); + + int ret= event_table->file->ha_index_init(0, 1); + if (ret) + { + event_table->file->print_error(ret, MYF(0)); + DBUG_RETURN(true); + } + key_info= event_table->key_info; if (key_info->key_parts == 0 || diff --git a/sql/event_scheduler.cc b/sql/event_scheduler.cc index c340384f068..ec96ca45a0e 100644 --- a/sql/event_scheduler.cc +++ b/sql/event_scheduler.cc @@ -153,8 +153,6 @@ void deinit_event_thread(THD *thd) { thd->proc_info= "Clearing"; - DBUG_ASSERT(thd->net.buff != 0); - net_end(&thd->net); DBUG_PRINT("exit", ("Event thread finishing")); mysql_mutex_lock(&LOCK_thread_count); thread_count--; @@ -182,12 +180,15 @@ deinit_event_thread(THD *thd) void pre_init_event_thread(THD* thd) { + THD *orig_thd= current_thd; DBUG_ENTER("pre_init_event_thread"); + + set_current_thd(thd); thd->client_capabilities= 0; thd->security_ctx->master_access= 0; thd->security_ctx->db_access= 0; thd->security_ctx->host_or_ip= (char*)my_localhost; - my_net_init(&thd->net, NULL); + my_net_init(&thd->net, NULL, MYF(MY_THREAD_SPECIFIC)); thd->security_ctx->set_user((char*)"event_scheduler"); thd->net.read_timeout= slave_net_timeout; thd->variables.option_bits|= OPTION_AUTO_IS_NULL; @@ -207,6 +208,7 @@ pre_init_event_thread(THD* thd) /* Do not use user-supplied timeout value for system threads. */ thd->variables.lock_wait_timeout= LONG_TIMEOUT; + set_current_thd(orig_thd); DBUG_VOID_RETURN; } @@ -402,6 +404,7 @@ Event_scheduler::start() ret= TRUE; goto end; } + pre_init_event_thread(new_thd); new_thd->system_thread= SYSTEM_THREAD_EVENT_SCHEDULER; new_thd->set_command(COM_DAEMON); @@ -417,6 +420,7 @@ Event_scheduler::start() new_thd->variables.tx_read_only= false; new_thd->tx_read_only= false; + /* This should not be marked with MY_THREAD_SPECIFIC */ scheduler_param_value= (struct scheduler_param *)my_malloc(sizeof(struct scheduler_param), MYF(0)); scheduler_param_value->thd= new_thd; @@ -436,8 +440,6 @@ Event_scheduler::start() ret= TRUE; new_thd->proc_info= "Clearing"; - DBUG_ASSERT(new_thd->net.buff != 0); - net_end(&new_thd->net); mysql_mutex_lock(&LOCK_thread_count); thread_count--; dec_thread_running(); @@ -537,6 +539,7 @@ Event_scheduler::execute_top(Event_queue_element_for_exec *event_name) pthread_t th; int res= 0; DBUG_ENTER("Event_scheduler::execute_top"); + if (!(new_thd= new THD())) goto error; @@ -571,8 +574,6 @@ error: if (new_thd) { new_thd->proc_info= "Clearing"; - DBUG_ASSERT(new_thd->net.buff != 0); - net_end(&new_thd->net); mysql_mutex_lock(&LOCK_thread_count); thread_count--; dec_thread_running(); @@ -676,7 +677,7 @@ Event_scheduler::stop() */ struct timespec top_time; set_timespec(top_time, 2); - COND_STATE_WAIT(thd, NULL, &stage_waiting_for_scheduler_to_stop); + COND_STATE_WAIT(thd, &top_time, &stage_waiting_for_scheduler_to_stop); } while (state == STOPPING); DBUG_PRINT("info", ("Scheduler thread has cleaned up. Set state to INIT")); sql_print_information("Event Scheduler: Stopped"); diff --git a/sql/events.cc b/sql/events.cc index cac71697300..b9c51b77f05 100644 --- a/sql/events.cc +++ b/sql/events.cc @@ -879,7 +879,7 @@ end: } delete thd; /* Remember that we don't have a THD */ - my_pthread_setspecific_ptr(THR_THD, NULL); + set_current_thd(0); DBUG_RETURN(res); } diff --git a/sql/field.cc b/sql/field.cc index f3e3ee938d6..1769e4e55cb 100644 --- a/sql/field.cc +++ b/sql/field.cc @@ -1114,6 +1114,21 @@ bool Field::type_can_have_key_part(enum enum_field_types type) } +void Field::make_sort_key(uchar *buff,uint length) +{ + if (maybe_null()) + { + if (is_null()) + { + bzero(buff, length + 1); + return; + } + *buff++= 1; + } + sort_string(buff, length); +} + + /** Numeric fields base class constructor. */ diff --git a/sql/field.h b/sql/field.h index f450e596e03..e832928b114 100644 --- a/sql/field.h +++ b/sql/field.h @@ -519,6 +519,7 @@ public: return bytes; } + void make_sort_key(uchar *buff, uint length); virtual void make_field(Send_field *); virtual void sort_string(uchar *buff,uint length)=0; virtual bool optimize_range(uint idx, uint part); diff --git a/sql/filesort.cc b/sql/filesort.cc index c612d2c55cb..49aaa0af574 100644 --- a/sql/filesort.cc +++ b/sql/filesort.cc @@ -61,7 +61,6 @@ static void make_sortkey(Sort_param *param, uchar *to, uchar *ref_pos); static void register_used_fields(Sort_param *param); static bool save_index(Sort_param *param, uint count, Filesort_info *table_sort); -static void make_sortkey(Sort_param *param, uchar *to, uchar *ref_pos); static uint suffix_length(ulong string_length); static uint sortlength(THD *thd, SORT_FIELD *sortorder, uint s_length, bool *multi_byte_charset); @@ -181,6 +180,7 @@ ha_rows filesort(THD *thd, TABLE *table, SORT_FIELD *sortorder, uint s_length, when index_merge select has finished with it. */ table->sort.io_cache= NULL; + DBUG_ASSERT(table_sort.record_pointers == NULL); outfile= table_sort.io_cache; my_b_clear(&tempfile); @@ -200,7 +200,8 @@ ha_rows filesort(THD *thd, TABLE *table, SORT_FIELD *sortorder, uint s_length, table_sort.unpack= unpack_addon_fields; if (param.addon_field && !(table_sort.addon_buf= - (uchar *) my_malloc(param.addon_length, MYF(MY_WME)))) + (uchar *) my_malloc(param.addon_length, MYF(MY_WME | + MY_THREAD_SPECIFIC)))) goto err; if (select && select->quick) @@ -213,7 +214,8 @@ ha_rows filesort(THD *thd, TABLE *table, SORT_FIELD *sortorder, uint s_length, num_rows= table->file->estimate_rows_upper_bound(); if (multi_byte_charset && - !(param.tmp_buffer= (char*) my_malloc(param.sort_length,MYF(MY_WME)))) + !(param.tmp_buffer= (char*) my_malloc(param.sort_length, + MYF(MY_WME | MY_THREAD_SPECIFIC)))) goto err; if (check_if_pq_applicable(¶m, &table_sort, @@ -452,7 +454,7 @@ static uchar *read_buffpek_from_file(IO_CACHE *buffpek_pointers, uint count, if (count > UINT_MAX/sizeof(BUFFPEK)) return 0; /* sizeof(BUFFPEK)*count will overflow */ if (!tmp) - tmp= (uchar *)my_malloc(length, MYF(MY_WME)); + tmp= (uchar *)my_malloc(length, MYF(MY_WME | MY_THREAD_SPECIFIC)); if (tmp) { if (reinit_io_cache(buffpek_pointers,READ_CACHE,0L,0,0) || @@ -605,6 +607,8 @@ static ha_rows find_all_keys(Sort_param *param, SQL_SELECT *select, if (!quick_select) { next_pos=(uchar*) 0; /* Find records in sequence */ + DBUG_EXECUTE_IF("bug14365043_1", + DBUG_SET("+d,ha_rnd_init_fail");); if (file->ha_rnd_init_with_error(1)) DBUG_RETURN(HA_POS_ERROR); file->extra_opt(HA_EXTRA_CACHE, @@ -862,21 +866,9 @@ static void make_sortkey(register Sort_param *param, bool maybe_null=0; if ((field=sort_field->field)) { // Field - if (field->maybe_null()) - { - if (field->is_null()) - { - if (sort_field->reverse) - memset(to, 255, sort_field->length+1); - else - memset(to, 0, sort_field->length+1); - to+= sort_field->length+1; - continue; - } - else - *to++=1; - } - field->sort_string(to, sort_field->length); + field->make_sort_key(to, sort_field->length); + if ((maybe_null = field->maybe_null())) + to++; } else { // Item @@ -1036,8 +1028,11 @@ static void make_sortkey(register Sort_param *param, } if (sort_field->reverse) { /* Revers key */ - if (maybe_null) - to[-1]= ~to[-1]; + if (maybe_null && (to[-1]= !to[-1])) + { + to+= sort_field->length; // don't waste the time reversing all 0's + continue; + } length=sort_field->length; while (length--) { @@ -1154,7 +1149,8 @@ static bool save_index(Sort_param *param, uint count, Filesort_info *table_sort) res_length= param->res_length; offset= param->rec_length-res_length; if (!(to= table_sort->record_pointers= - (uchar*) my_malloc(res_length*count, MYF(MY_WME)))) + (uchar*) my_malloc(res_length*count, + MYF(MY_WME | MY_THREAD_SPECIFIC)))) DBUG_RETURN(1); /* purecov: inspected */ uchar **sort_keys= table_sort->get_sort_keys(); for (uchar **end= sort_keys+count ; sort_keys != end ; sort_keys++) @@ -1896,7 +1892,9 @@ get_addon_fields(ulong max_length_for_sort_data, if (length+sortlength > max_length_for_sort_data || !(addonf= (SORT_ADDON_FIELD *) my_malloc(sizeof(SORT_ADDON_FIELD)* - (fields+1), MYF(MY_WME)))) + (fields+1), + MYF(MY_WME | + MY_THREAD_SPECIFIC)))) return 0; *plength= length; diff --git a/sql/filesort_utils.cc b/sql/filesort_utils.cc index c4480a6376d..f8f6d5c9420 100644 --- a/sql/filesort_utils.cc +++ b/sql/filesort_utils.cc @@ -99,7 +99,7 @@ uchar **Filesort_buffer::alloc_sort_buffer(uint num_records, uint record_length) sort_buff_sz= num_records * (record_length + sizeof(uchar*)); set_if_bigger(sort_buff_sz, record_length * MERGEBUFF2); uchar **sort_keys= - (uchar**) my_malloc(sort_buff_sz, MYF(0)); + (uchar**) my_malloc(sort_buff_sz, MYF(MY_THREAD_SPECIFIC)); m_idx_array= Idx_array(sort_keys, num_records); m_record_length= record_length; uchar **start_of_data= m_idx_array.array() + m_idx_array.size(); @@ -130,7 +130,8 @@ void Filesort_buffer::sort_buffer(const Sort_param *param, uint count) uchar **keys= get_sort_keys(); uchar **buffer= NULL; if (radixsort_is_appliccable(count, param->sort_length) && - (buffer= (uchar**) my_malloc(count*sizeof(char*), MYF(0)))) + (buffer= (uchar**) my_malloc(count*sizeof(char*), + MYF(MY_THREAD_SPECIFIC)))) { radixsort_for_str_ptr(keys, count, param->sort_length, buffer); my_free(buffer); @@ -140,4 +141,3 @@ void Filesort_buffer::sort_buffer(const Sort_param *param, uint count) size_t size= param->sort_length; my_qsort2(keys, count, sizeof(uchar*), get_ptr_compare(size), &size); } - diff --git a/sql/ha_ndbcluster.cc b/sql/ha_ndbcluster.cc index 6ba4fe46441..6fc30fa4fa0 100644 --- a/sql/ha_ndbcluster.cc +++ b/sql/ha_ndbcluster.cc @@ -8649,7 +8649,7 @@ NDB_SHARE *ndbcluster_get_share(const char *key, TABLE *table, MEM_ROOT **root_ptr= my_pthread_getspecific_ptr(MEM_ROOT**, THR_MALLOC); MEM_ROOT *old_root= *root_ptr; - init_sql_alloc(&share->mem_root, 1024, 0); + init_sql_alloc(&share->mem_root, 1024, 0, MYF(0)); *root_ptr= &share->mem_root; // remember to reset before return share->state= NSS_INITIAL; /* enough space for key, db, and table_name */ @@ -9493,7 +9493,7 @@ pthread_handler_t ndb_util_thread_func(void *arg __attribute__((unused))) thd->init_for_queries(); thd->main_security_ctx.host_or_ip= ""; thd->client_capabilities = 0; - my_net_init(&thd->net, 0); + my_net_init(&thd->net, 0, MYF(MY_THREAD_SPECIFIC)); thd->main_security_ctx.master_access= ~0; thd->main_security_ctx.priv_user[0] = 0; /* Do not use user-supplied timeout value for system threads. */ @@ -9730,11 +9730,9 @@ next: mysql_mutex_lock(&LOCK_ndb_util_thread); ndb_util_thread_end: - net_end(&thd->net); ndb_util_thread_fail: if (share_list) delete [] share_list; - thd->cleanup(); delete thd; /* signal termination */ diff --git a/sql/ha_ndbcluster_binlog.cc b/sql/ha_ndbcluster_binlog.cc index 59b9d6eab6b..1544678de38 100644 --- a/sql/ha_ndbcluster_binlog.cc +++ b/sql/ha_ndbcluster_binlog.cc @@ -1,4 +1,5 @@ /* Copyright (c) 2006, 2011, Oracle and/or its affiliates. + Copyright (c) 2012, 2013, Monty Proram Ab. 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 @@ -3665,7 +3666,7 @@ pthread_handler_t ndb_binlog_thread_func(void *arg) thd->system_thread= SYSTEM_THREAD_NDBCLUSTER_BINLOG; thd->main_security_ctx.host_or_ip= ""; thd->client_capabilities= 0; - my_net_init(&thd->net, 0); + my_net_init(&thd->net, 0, MYF(MY_THREAD_SPECIFIC)); thd->main_security_ctx.master_access= ~0; thd->main_security_ctx.priv_user[0]= 0; /* Do not use user-supplied timeout value for system threads. */ @@ -3964,7 +3965,7 @@ restart: my_pthread_getspecific_ptr(MEM_ROOT**, THR_MALLOC); MEM_ROOT *old_root= *root_ptr; MEM_ROOT mem_root; - init_sql_alloc(&mem_root, 4096, 0); + init_sql_alloc(&mem_root, 4096, 0, MYF(0)); List<Cluster_schema> post_epoch_log_list; List<Cluster_schema> post_epoch_unlock_list; *root_ptr= &mem_root; @@ -4364,8 +4365,6 @@ err: my_hash_free(&ndb_schema_objects); - net_end(&thd->net); - thd->cleanup(); delete thd; ndb_binlog_thread_running= -1; diff --git a/sql/ha_partition.cc b/sql/ha_partition.cc index 6ac51b6e380..148a2329660 100644 --- a/sql/ha_partition.cc +++ b/sql/ha_partition.cc @@ -168,7 +168,7 @@ ha_partition::ha_partition(handlerton *hton, TABLE_SHARE *share) :handler(hton, share) { DBUG_ENTER("ha_partition::ha_partition(table)"); - init_alloc_root(&m_mem_root, 512, 512); + init_alloc_root(&m_mem_root, 512, 512, MYF(0)); init_handler_variables(); DBUG_VOID_RETURN; } @@ -190,7 +190,7 @@ ha_partition::ha_partition(handlerton *hton, partition_info *part_info) { DBUG_ENTER("ha_partition::ha_partition(part_info)"); DBUG_ASSERT(part_info); - init_alloc_root(&m_mem_root, 512, 512); + init_alloc_root(&m_mem_root, 512, 512, MYF(0)); init_handler_variables(); m_part_info= part_info; m_create_handler= TRUE; @@ -217,7 +217,7 @@ ha_partition::ha_partition(handlerton *hton, TABLE_SHARE *share, :handler(hton, share) { DBUG_ENTER("ha_partition::ha_partition(clone)"); - init_alloc_root(&m_mem_root, 512, 512); + init_alloc_root(&m_mem_root, 512, 512, MYF(0)); init_handler_variables(); m_part_info= part_info_arg; m_create_handler= TRUE; @@ -2850,6 +2850,17 @@ int ha_partition::open(const char *name, int mode, uint test_if_locked) if (bitmap_init(&m_bulk_insert_started, NULL, m_tot_parts + 1, FALSE)) DBUG_RETURN(error); bitmap_clear_all(&m_bulk_insert_started); + /* + Initialize the bitmap we use to keep track of partitions which returned + HA_ERR_KEY_NOT_FOUND from index_read_map. + */ + if (bitmap_init(&m_key_not_found_partitions, NULL, m_tot_parts, FALSE)) + { + bitmap_free(&m_bulk_insert_started); + DBUG_RETURN(error); + } + bitmap_clear_all(&m_key_not_found_partitions); + m_key_not_found= false; /* Initialize the bitmap we use to determine what partitions are used */ if (!m_is_clone_of) { @@ -2992,6 +3003,7 @@ err_handler: (*file)->ha_close(); err_alloc: bitmap_free(&m_bulk_insert_started); + bitmap_free(&m_key_not_found_partitions); if (!m_is_clone_of) bitmap_free(&(m_part_info->used_partitions)); @@ -3097,6 +3109,7 @@ int ha_partition::close(void) DBUG_ASSERT(table->s == table_share); destroy_record_priority_queue(); bitmap_free(&m_bulk_insert_started); + bitmap_free(&m_key_not_found_partitions); if (!m_is_clone_of) bitmap_free(&(m_part_info->used_partitions)); file= m_file; @@ -4619,21 +4632,24 @@ int ha_partition::index_read_map(uchar *buf, const uchar *key, } -/* +/** Common routine for a number of index_read variants - SYNOPSIS - ha_partition::common_index_read() - buf Buffer where the record should be returned - have_start_key TRUE <=> the left endpoint is available, i.e. - we're in index_read call or in read_range_first - call and the range has left endpoint - - FALSE <=> there is no left endpoint (we're in - read_range_first() call and the range has no left - endpoint) + @param buf Buffer where the record should be returned. + @param have_start_key TRUE <=> the left endpoint is available, i.e. + we're in index_read call or in read_range_first + call and the range has left endpoint. + FALSE <=> there is no left endpoint (we're in + read_range_first() call and the range has no left + endpoint). - DESCRIPTION + @return Operation status + @retval 0 OK + @retval HA_ERR_END_OF_FILE Whole index scanned, without finding the record. + @retval HA_ERR_KEY_NOT_FOUND Record not found, but index cursor positioned. + @retval other error code. + + @details Start scanning the range (when invoked from read_range_first()) or doing an index lookup (when invoked from index_read_XXX): - If possible, perform partition selection @@ -4643,10 +4659,6 @@ int ha_partition::index_read_map(uchar *buf, const uchar *key, handle_unordered_scan_next_partition) YES: Fill the priority queue and get the record that is the first in the ordering - - RETURN - 0 OK - other HA_ERR_END_OF_FILE or other error code. */ int ha_partition::common_index_read(uchar *buf, bool have_start_key) @@ -4656,14 +4668,16 @@ int ha_partition::common_index_read(uchar *buf, bool have_start_key) bool reverse_order= FALSE; DBUG_ENTER("ha_partition::common_index_read"); - DBUG_PRINT("info", ("m_ordered: %u have_start_key: %u", - m_ordered, have_start_key)); + DBUG_PRINT("info", ("m_ordered %u m_ordered_scan_ong %u", + m_ordered, m_ordered_scan_ongoing)); if (have_start_key) { m_start_key.length= key_len= calculate_key_len(table, active_index, m_start_key.key, m_start_key.keypart_map); + DBUG_PRINT("info", ("have_start_key map %lu find_flag %u len %u", + m_start_key.keypart_map, m_start_key.flag, key_len)); DBUG_ASSERT(key_len); } if ((error= partition_scan_set_up(buf, have_start_key))) @@ -4681,24 +4695,16 @@ int ha_partition::common_index_read(uchar *buf, bool have_start_key) } DBUG_PRINT("info", ("m_ordered %u m_o_scan_ong %u have_start_key %u", m_ordered, m_ordered_scan_ongoing, have_start_key)); - if (!m_ordered_scan_ongoing || - (have_start_key && m_start_key.flag == HA_READ_KEY_EXACT && - !m_pkey_is_clustered && - key_len >= m_curr_key_info[0]->key_length)) + if (!m_ordered_scan_ongoing) { /* - We use unordered index scan either when read_range is used and flag - is set to not use ordered or when an exact key is used and in this - case all records will be sorted equal and thus the sort order of the - resulting records doesn't matter. + We use unordered index scan when read_range is used and flag + is set to not use ordered. We also use an unordered index scan when the number of partitions to scan is only one. The unordered index scan will use the partition set created. - Need to set unordered scan ongoing since we can come here even when - it isn't set. */ DBUG_PRINT("info", ("doing unordered scan")); - m_ordered_scan_ongoing= FALSE; error= handle_unordered_scan_next_partition(buf); } else @@ -4879,6 +4885,8 @@ int ha_partition::index_next(uchar * buf) TODO(low priority): If we want partition to work with the HANDLER commands, we must be able to do index_last() -> index_prev() -> index_next() + and if direction changes, we must step back those partitions in + the record queue so we don't return a value from the wrong direction. */ DBUG_ASSERT(m_index_scan_type != partition_index_last); if (!m_ordered_scan_ongoing) @@ -5132,10 +5140,18 @@ int ha_partition::partition_scan_set_up(uchar * buf, bool idx_read_flag) int ha_partition::handle_unordered_next(uchar *buf, bool is_next_same) { - handler *file= m_file[m_part_spec.start_part]; + handler *file; int error; DBUG_ENTER("ha_partition::handle_unordered_next"); + if (m_part_spec.start_part >= m_tot_parts) + { + /* Should never happen! */ + DBUG_ASSERT(0); + DBUG_RETURN(HA_ERR_END_OF_FILE); + } + file= m_file[m_part_spec.start_part]; + /* We should consider if this should be split into three functions as partition_read_range is_next_same are always local constants @@ -5196,6 +5212,7 @@ int ha_partition::handle_unordered_next(uchar *buf, bool is_next_same) int ha_partition::handle_unordered_scan_next_partition(uchar * buf) { uint i; + int saved_error= HA_ERR_END_OF_FILE; DBUG_ENTER("ha_partition::handle_unordered_scan_next_partition"); for (i= m_part_spec.start_part; i <= m_part_spec.end_part; i++) @@ -5246,26 +5263,33 @@ int ha_partition::handle_unordered_scan_next_partition(uchar * buf) } if ((error != HA_ERR_END_OF_FILE) && (error != HA_ERR_KEY_NOT_FOUND)) DBUG_RETURN(error); - DBUG_PRINT("info", ("HA_ERR_END_OF_FILE on partition %d", i)); + + /* + If HA_ERR_KEY_NOT_FOUND, we must return that error instead of + HA_ERR_END_OF_FILE, to be able to continue search. + */ + if (saved_error != HA_ERR_KEY_NOT_FOUND) + saved_error= error; + DBUG_PRINT("info", ("END_OF_FILE/KEY_NOT_FOUND on partition %d", i)); } - m_part_spec.start_part= NO_CURRENT_PART_ID; - DBUG_RETURN(HA_ERR_END_OF_FILE); + if (saved_error == HA_ERR_END_OF_FILE) + m_part_spec.start_part= NO_CURRENT_PART_ID; + DBUG_RETURN(saved_error); } -/* - Common routine to start index scan with ordered results +/** + Common routine to start index scan with ordered results. - SYNOPSIS - handle_ordered_index_scan() - out:buf Read row in MySQL Row Format + @param[out] buf Read row in MySQL Row Format - RETURN VALUE - HA_ERR_END_OF_FILE End of scan - 0 Success - other Error code + @return Operation status + @retval HA_ERR_END_OF_FILE End of scan + @retval HA_ERR_KEY_NOT_FOUNE End of scan + @retval 0 Success + @retval other Error code - DESCRIPTION + @details This part contains the logic to handle index scans that require ordered output. This includes all except those started by read_range_first with the flag ordered set to FALSE. Thus most direct index_read and all @@ -5287,8 +5311,14 @@ int ha_partition::handle_ordered_index_scan(uchar *buf, bool reverse_order) uint j= queue_first_element(&m_queue); bool found= FALSE; uchar *part_rec_buf_ptr= m_ordered_rec_buffer; + int saved_error= HA_ERR_END_OF_FILE; DBUG_ENTER("ha_partition::handle_ordered_index_scan"); + if (m_key_not_found) + { + m_key_not_found= false; + bitmap_clear_all(&m_key_not_found_partitions); + } m_top_entry= NO_CURRENT_PART_ID; queue_remove_all(&m_queue); @@ -5350,6 +5380,13 @@ int ha_partition::handle_ordered_index_scan(uchar *buf, bool reverse_order) { DBUG_RETURN(error); } + else if (error == HA_ERR_KEY_NOT_FOUND) + { + DBUG_PRINT("info", ("HA_ERR_KEY_NOT_FOUND from partition %u", i)); + bitmap_set_bit(&m_key_not_found_partitions, i); + m_key_not_found= true; + saved_error= error; + } part_rec_buf_ptr+= m_rec_length + PARTITION_BYTES_IN_POS; } if (found) @@ -5367,7 +5404,7 @@ int ha_partition::handle_ordered_index_scan(uchar *buf, bool reverse_order) DBUG_PRINT("info", ("Record returned from partition %d", m_top_entry)); DBUG_RETURN(0); } - DBUG_RETURN(HA_ERR_END_OF_FILE); + DBUG_RETURN(saved_error); } @@ -5395,6 +5432,59 @@ void ha_partition::return_top_record(uchar *buf) } +/** + Add index_next/prev from partitions without exact match. + + If there where any partitions that returned HA_ERR_KEY_NOT_FOUND when + ha_index_read_map was done, those partitions must be included in the + following index_next/prev call. +*/ + +int ha_partition::handle_ordered_index_scan_key_not_found() +{ + int error; + uint i; + uchar *part_buf= m_ordered_rec_buffer; + uchar *curr_rec_buf= NULL; + DBUG_ENTER("ha_partition::handle_ordered_index_scan_key_not_found"); + DBUG_ASSERT(m_key_not_found); + /* + Loop over all used partitions to get the correct offset + into m_ordered_rec_buffer. + */ + for (i= 0; i < m_tot_parts; i++) + { + if (!bitmap_is_set(&m_part_info->used_partitions, i)) + continue; + + if (bitmap_is_set(&m_key_not_found_partitions, i)) + { + /* + This partition is used and did return HA_ERR_KEY_NOT_FOUND + in index_read_map. + */ + curr_rec_buf= part_buf + PARTITION_BYTES_IN_POS; + error= m_file[i]->index_next(curr_rec_buf); + /* HA_ERR_KEY_NOT_FOUND is not allowed from index_next! */ + DBUG_ASSERT(error != HA_ERR_KEY_NOT_FOUND); + if (!error) + queue_insert(&m_queue, part_buf); + else if (error != HA_ERR_END_OF_FILE && error != HA_ERR_KEY_NOT_FOUND) + DBUG_RETURN(error); + } + part_buf+= m_rec_length + PARTITION_BYTES_IN_POS; + } + DBUG_ASSERT(curr_rec_buf); + bitmap_clear_all(&m_key_not_found_partitions); + m_key_not_found= false; + + /* Update m_top_entry, which may have changed. */ + uchar *key_buffer= queue_top(&m_queue); + m_top_entry= uint2korr(key_buffer); + DBUG_RETURN(0); +} + + /* Common routine to handle index_next with ordered results @@ -5414,9 +5504,45 @@ int ha_partition::handle_ordered_next(uchar *buf, bool is_next_same) int error; uint part_id= m_top_entry; uchar *rec_buf= queue_top(&m_queue) + PARTITION_BYTES_IN_POS; - handler *file= m_file[part_id]; + handler *file; DBUG_ENTER("ha_partition::handle_ordered_next"); + if (m_key_not_found) + { + if (is_next_same) + { + /* Only rows which match the key. */ + m_key_not_found= false; + bitmap_clear_all(&m_key_not_found_partitions); + } + else + { + /* There are partitions not included in the index record queue. */ + uint old_elements= m_queue.elements; + if ((error= handle_ordered_index_scan_key_not_found())) + DBUG_RETURN(error); + /* + If the queue top changed, i.e. one of the partitions that gave + HA_ERR_KEY_NOT_FOUND in index_read_map found the next record, + return it. + Otherwise replace the old with a call to index_next (fall through). + */ + if (old_elements != m_queue.elements && part_id != m_top_entry) + { + return_top_record(buf); + DBUG_RETURN(0); + } + } + } + if (part_id >= m_tot_parts) + { + /* This should never happen! */ + DBUG_ASSERT(0); + DBUG_RETURN(HA_ERR_END_OF_FILE); + } + + file= m_file[part_id]; + if (m_index_scan_type == partition_read_range) { error= file->read_range_next(); diff --git a/sql/ha_partition.h b/sql/ha_partition.h index b37b0f0b1c3..96e47d3d676 100644 --- a/sql/ha_partition.h +++ b/sql/ha_partition.h @@ -182,6 +182,9 @@ private: static int compare_number_of_records(ha_partition *me, const uint32 *a, const uint32 *b); + /** partitions that returned HA_ERR_KEY_NOT_FOUND. */ + MY_BITMAP m_key_not_found_partitions; + bool m_key_not_found; public: handler *clone(const char *name, MEM_ROOT *mem_root); virtual void set_part_info(partition_info *part_info) @@ -518,6 +521,7 @@ private: int handle_unordered_next(uchar * buf, bool next_same); int handle_unordered_scan_next_partition(uchar * buf); int handle_ordered_index_scan(uchar * buf, bool reverse_order); + int handle_ordered_index_scan_key_not_found(); int handle_ordered_next(uchar * buf, bool next_same); int handle_ordered_prev(uchar * buf); void return_top_record(uchar * buf); diff --git a/sql/handler.cc b/sql/handler.cc index bf84b1287d5..5297a8e8cfc 100644 --- a/sql/handler.cc +++ b/sql/handler.cc @@ -698,7 +698,6 @@ static my_bool closecon_handlerton(THD *thd, plugin_ref plugin, return FALSE; } - /** @note don't bother to rollback here, it's done already @@ -708,6 +707,25 @@ void ha_close_connection(THD* thd) plugin_foreach(thd, closecon_handlerton, MYSQL_STORAGE_ENGINE_PLUGIN, 0); } +static my_bool kill_handlerton(THD *thd, plugin_ref plugin, + void *level) +{ + handlerton *hton= plugin_data(plugin, handlerton *); + + if (hton->state == SHOW_OPTION_YES && hton->kill_query && + thd_get_ha_data(thd, hton)) + hton->kill_query(hton, thd, *(enum thd_kill_levels *) level); + return FALSE; +} + +void ha_kill_query(THD* thd, enum thd_kill_levels level) +{ + DBUG_ENTER("ha_kill_query"); + plugin_foreach(thd, kill_handlerton, MYSQL_STORAGE_ENGINE_PLUGIN, &level); + DBUG_VOID_RETURN; +} + + /* ======================================================================== ======================= TRANSACTIONS ===================================*/ @@ -2561,18 +2579,25 @@ int handler::read_first_row(uchar * buf, uint primary_key) if (stats.deleted < 10 || primary_key >= MAX_KEY || !(index_flags(primary_key, 0, 0) & HA_READ_ORDER)) { - if ((!(error= ha_rnd_init(1)))) + if (!(error= ha_rnd_init(1))) { - while ((error= ha_rnd_next(buf)) == HA_ERR_RECORD_DELETED) ; - (void) ha_rnd_end(); + while ((error= ha_rnd_next(buf)) == HA_ERR_RECORD_DELETED) + /* skip deleted row */; + const int end_error= ha_rnd_end(); + if (!error) + error= end_error; } } else { /* Find the first row through the primary key */ - if (!(error = ha_index_init(primary_key, 0))) + if (!(error= ha_index_init(primary_key, 0))) + { error= ha_index_first(buf); - (void) ha_index_end(); + const int end_error= ha_index_end(); + if (!error) + error= end_error; + } } DBUG_RETURN(error); } @@ -2970,7 +2995,15 @@ void handler::get_auto_increment(ulonglong offset, ulonglong increment, table->mark_columns_used_by_index_no_reset(table->s->next_number_index, table->read_set); column_bitmaps_signal(); - ha_index_init(table->s->next_number_index, 1); + + if (ha_index_init(table->s->next_number_index, 1)) + { + /* This should never happen, assert in debug, and fail in release build */ + DBUG_ASSERT(0); + *first_value= ULONGLONG_MAX; + return; + } + if (table->s->next_number_keypart == 0) { // Autoincrement at key-start error= ha_index_last(table->record[1]); @@ -3001,13 +3034,25 @@ void handler::get_auto_increment(ulonglong offset, ulonglong increment, } if (error) - nr=1; + { + if (error == HA_ERR_END_OF_FILE || error == HA_ERR_KEY_NOT_FOUND) + { + /* No entry found, start with 1. */ + nr= 1; + } + else + { + DBUG_ASSERT(0); + nr= ULONGLONG_MAX; + } + } else nr= ((ulonglong) table->next_number_field-> val_int_offset(table->s->rec_buff_length)+1); ha_index_end(); (void) extra(HA_EXTRA_NO_KEYREAD); *first_value= nr; + return; } @@ -4926,7 +4971,9 @@ extern "C" enum icp_result handler_index_cond_check(void* h_arg) THD *thd= h->table->in_use; enum icp_result res; - if (thd_killed(thd)) + enum thd_kill_levels abort_at= h->has_transactions() ? + THD_ABORT_SOFTLY : THD_ABORT_ASAP; + if (thd_kill_level(thd) > abort_at) return ICP_ABORTED_BY_USER; if (h->end_range && h->compare_key2(h->end_range) > 0) @@ -5563,7 +5610,7 @@ fl_log_iterator_buffer_init(struct handler_iterator *iterator) /* to be able to make my_free without crash in case of error */ iterator->buffer= 0; - if (!(dirp = my_dir(fl_dir, MYF(0)))) + if (!(dirp = my_dir(fl_dir, MYF(MY_THREAD_SPECIFIC)))) { return HA_ITERATOR_ERROR; } @@ -5572,7 +5619,7 @@ fl_log_iterator_buffer_init(struct handler_iterator *iterator) sizeof(enum log_status) + + FN_REFLEN + 1) * (uint) dirp->number_off_files), - MYF(0))) == 0) + MYF(MY_THREAD_SPECIFIC))) == 0) { return HA_ITERATOR_ERROR; } @@ -5606,6 +5653,7 @@ fl_log_iterator_buffer_init(struct handler_iterator *iterator) iterator->buffer= buff; iterator->next= &fl_log_iterator_next; iterator->destroy= &fl_log_iterator_destroy; + my_dirend(dirp); return HA_ITERATOR_OK; } diff --git a/sql/handler.h b/sql/handler.h index 559648da37c..8ee1044f10c 100644 --- a/sql/handler.h +++ b/sql/handler.h @@ -882,6 +882,10 @@ struct handlerton */ int (*close_connection)(handlerton *hton, THD *thd); /* + Tell handler that query has been killed. + */ + void (*kill_query)(handlerton *hton, THD *thd, enum thd_kill_levels level); + /* sv points to an uninitialized storage area of requested size (see savepoint_offset description) */ @@ -1934,6 +1938,7 @@ public: int ha_open(TABLE *table, const char *name, int mode, uint test_if_locked); int ha_index_init(uint idx, bool sorted) { + DBUG_EXECUTE_IF("ha_index_init_fail", return HA_ERR_TABLE_DEF_CHANGED;); int result; DBUG_ENTER("ha_index_init"); DBUG_ASSERT(inited==NONE); @@ -1958,6 +1963,7 @@ public: virtual int prepare_index_scan() { return 0; } int ha_rnd_init(bool scan) __attribute__ ((warn_unused_result)) { + DBUG_EXECUTE_IF("ha_rnd_init_fail", return HA_ERR_TABLE_DEF_CHANGED;); int result; DBUG_ENTER("ha_rnd_init"); DBUG_ASSERT(inited==NONE || (inited==RND && scan)); @@ -3095,6 +3101,7 @@ int ha_finalize_handlerton(st_plugin_int *plugin); TYPELIB *ha_known_exts(void); int ha_panic(enum ha_panic_function flag); void ha_close_connection(THD* thd); +void ha_kill_query(THD* thd, enum thd_kill_levels level); bool ha_flush_logs(handlerton *db_type); void ha_drop_database(char* path); void ha_checkpoint_state(bool disable); diff --git a/sql/hostname.cc b/sql/hostname.cc index 763c4647532..ee30d071602 100644 --- a/sql/hostname.cc +++ b/sql/hostname.cc @@ -367,6 +367,14 @@ bool ip_to_hostname(struct sockaddr_storage *ip_storage, err_code= vio_getnameinfo(ip, hostname_buffer, NI_MAXHOST, NULL, 0, NI_NAMEREQD); + /* BEGIN : DEBUG */ + DBUG_EXECUTE_IF("addr_fake_ipv4", + { + strcpy(hostname_buffer, "santa.claus.ipv4.example.com"); + err_code= 0; + };); + /* END : DEBUG */ + if (err_code) { // NOTE: gai_strerror() returns a string ending by a dot. @@ -439,6 +447,12 @@ bool ip_to_hostname(struct sockaddr_storage *ip_storage, DBUG_RETURN(err_status); } + /* + To avoid crashing the server in DBUG_EXECUTE_IF, + Define a variable which depicts state of addr_info_list. + */ + bool free_addr_info_list= false; + /* Get IP-addresses for the resolved host name (FCrDNS technique). */ struct addrinfo hints; @@ -453,6 +467,42 @@ bool ip_to_hostname(struct sockaddr_storage *ip_storage, (const char *) hostname_buffer)); err_code= getaddrinfo(hostname_buffer, NULL, &hints, &addr_info_list); + if (err_code == 0) + free_addr_info_list= true; + + /* BEGIN : DEBUG */ + DBUG_EXECUTE_IF("addr_fake_ipv4", + { + if (free_addr_info_list) + freeaddrinfo(addr_info_list); + + struct sockaddr_in *debug_addr; + static struct sockaddr_in debug_sock_addr[2]; + static struct addrinfo debug_addr_info[2]; + /* Simulating ipv4 192.0.2.5 */ + debug_addr= & debug_sock_addr[0]; + debug_addr->sin_family= AF_INET; + debug_addr->sin_addr.s_addr= inet_addr("192.0.2.5"); + + /* Simulating ipv4 192.0.2.4 */ + debug_addr= & debug_sock_addr[1]; + debug_addr->sin_family= AF_INET; + debug_addr->sin_addr.s_addr= inet_addr("192.0.2.4"); + + debug_addr_info[0].ai_addr= (struct sockaddr*) & debug_sock_addr[0]; + debug_addr_info[0].ai_addrlen= sizeof (struct sockaddr_in); + debug_addr_info[0].ai_next= & debug_addr_info[1]; + + debug_addr_info[1].ai_addr= (struct sockaddr*) & debug_sock_addr[1]; + debug_addr_info[1].ai_addrlen= sizeof (struct sockaddr_in); + debug_addr_info[1].ai_next= NULL; + + addr_info_list= & debug_addr_info[0]; + err_code= 0; + free_addr_info_list= false; + };); + + /* END : DEBUG */ if (err_code == EAI_NONAME) { @@ -505,7 +555,8 @@ bool ip_to_hostname(struct sockaddr_storage *ip_storage, { DBUG_PRINT("error", ("Out of memory.")); - freeaddrinfo(addr_info_list); + if (free_addr_info_list) + freeaddrinfo(addr_info_list); DBUG_RETURN(TRUE); } @@ -539,7 +590,8 @@ bool ip_to_hostname(struct sockaddr_storage *ip_storage, /* Free the result of getaddrinfo(). */ - freeaddrinfo(addr_info_list); + if (free_addr_info_list) + freeaddrinfo(addr_info_list); /* Add an entry for the IP to the cache. */ diff --git a/sql/item.cc b/sql/item.cc index 27e6d7052d6..665521c641e 100644 --- a/sql/item.cc +++ b/sql/item.cc @@ -5576,7 +5576,7 @@ bool Item::eq_by_collation(Item *item, bool binary_cmp, CHARSET_INFO *cs) /** Create a field to hold a string value from an item. - If max_length > CONVERT_IF_BIGGER_TO_BLOB create a blob @n + If too_big_for_varchar() create a blob @n If max_length > 0 create a varchar @n If max_length == 0 create a CHAR(0) @@ -5591,7 +5591,7 @@ Field *Item::make_string_field(TABLE *table) Note: the following check is repeated in subquery_types_allow_materialization(): */ - if (max_length/collation.collation->mbmaxlen > CONVERT_IF_BIGGER_TO_BLOB) + if (too_big_for_varchar()) field= new Field_blob(max_length, maybe_null, name, collation.collation, TRUE); /* Item_type_holder holds the exact type, do not change it */ @@ -5696,7 +5696,7 @@ Field *Item::tmp_table_field_from_field_type(TABLE *table, bool fixed_length) DBUG_ASSERT(0); /* If something goes awfully wrong, it's better to get a string than die */ case MYSQL_TYPE_STRING: - if (fixed_length && max_length < CONVERT_IF_BIGGER_TO_BLOB) + if (fixed_length && !too_big_for_varchar()) { field= new Field_string(max_length, maybe_null, name, collation.collation); @@ -8848,9 +8848,10 @@ int Item_cache_temporal::save_in_field(Field *field, bool no_conversions) } -void Item_cache_temporal::store_packed(longlong val_arg) +void Item_cache_temporal::store_packed(longlong val_arg, Item *example) { /* An explicit values is given, save it. */ + store(example); value_cached= true; value= val_arg; null_value= false; @@ -9596,11 +9597,18 @@ table_map Item_ref::used_tables() const void Item_ref::update_used_tables() -{ +{ if (!get_depended_from()) - (*ref)->update_used_tables(); + (*ref)->update_used_tables(); + maybe_null|= (*ref)->maybe_null; } +void Item_direct_view_ref::update_used_tables() +{ + Item_ref::update_used_tables(); + if (view->table && view->table->maybe_null) + maybe_null= TRUE; +} table_map Item_direct_view_ref::used_tables() const { diff --git a/sql/item.h b/sql/item.h index c76b443fe09..8ec701e0255 100644 --- a/sql/item.h +++ b/sql/item.h @@ -2,7 +2,7 @@ #define SQL_ITEM_INCLUDED /* Copyright (c) 2000, 2011, Oracle and/or its affiliates. - Copyright (c) 2009-2011 Monty Program Ab + Copyright (c) 2009, 2013 Monty Program Ab 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 @@ -523,7 +523,7 @@ public: struct st_dyncall_create_def { - Item *num, *value; + Item *key, *value; CHARSET_INFO *cs; uint len, frac; DYNAMIC_COLUMN_TYPE type; @@ -611,7 +611,11 @@ public: @see Query_arena::free_list */ Item *next; - uint32 max_length; /* Maximum length, in bytes */ + /* + The maximum value length in characters multiplied by collation->mbmaxlen. + Almost always it's the maximum value length in bytes. + */ + uint32 max_length; /* TODO: convert name and name_length fields into LEX_STRING to keep them in sync (see bug #11829681/60295 etc). Then also remove some strlen(name) @@ -1424,6 +1428,8 @@ public: bool eq_by_collation(Item *item, bool binary_cmp, CHARSET_INFO *cs); uint32 max_char_length() const { return max_length / collation.collation->mbmaxlen; } + bool too_big_for_varchar() const + { return max_char_length() > CONVERT_IF_BIGGER_TO_BLOB; } void fix_length_and_charset(uint32 max_char_length_arg, CHARSET_INFO *cs) { max_length= char_to_byte_length_safe(max_char_length_arg, cs->mbmaxlen); @@ -1434,24 +1440,11 @@ public: max_length= char_to_byte_length_safe(max_char_length_arg, collation.collation->mbmaxlen); } - void fix_char_length_ulonglong(ulonglong max_char_length_arg) - { - ulonglong max_result_length= max_char_length_arg * - collation.collation->mbmaxlen; - if (max_result_length >= MAX_BLOB_WIDTH) - { - max_length= MAX_BLOB_WIDTH; - maybe_null= 1; - } - else - max_length= (uint32) max_result_length; - } /* Return TRUE if the item points to a column of an outer-joined table. */ virtual bool is_outer_field() const { DBUG_ASSERT(fixed); return FALSE; } Item* set_expr_cache(THD *thd); - virtual Item *get_cached_item() { return NULL; } virtual Item_equal *get_item_equal() { return NULL; } virtual void set_item_equal(Item_equal *item_eq) {}; @@ -2057,9 +2050,14 @@ public: bitmap_fast_test_and_set(tab->read_set, field->field_index); if (field->vcol_info) tab->mark_virtual_col(field); - } + } + } + void update_used_tables() + { + update_table_bitmaps(); + if (field && field->table) + maybe_null|= field->maybe_null(); } - void update_used_tables() { update_table_bitmaps(); } Item *get_tmp_table_item(THD *thd); bool collect_item_field_processor(uchar * arg); bool add_field_to_set_processor(uchar * arg); @@ -3109,7 +3107,11 @@ public: enum Item_result result_type () const { return orig_item->result_type(); } enum_field_types field_type() const { return orig_item->field_type(); } table_map used_tables() const { return orig_item->used_tables(); } - void update_used_tables() { orig_item->update_used_tables(); } + void update_used_tables() + { + orig_item->update_used_tables(); + maybe_null|= orig_item->maybe_null; + } bool const_item() const { return orig_item->const_item(); } table_map not_null_tables() const { return orig_item->not_null_tables(); } bool walk(Item_processor processor, bool walk_subquery, uchar *arg) @@ -3201,6 +3203,7 @@ public: Item *replace_equal_field(uchar *arg); table_map used_tables() const; table_map not_null_tables() const; + void update_used_tables(); bool walk(Item_processor processor, bool walk_subquery, uchar *arg) { return (*ref)->walk(processor, walk_subquery, arg) || @@ -4026,7 +4029,7 @@ public: bool cache_value(); bool get_date(MYSQL_TIME *ltime, ulonglong fuzzydate); int save_in_field(Field *field, bool no_conversions); - void store_packed(longlong val_arg); + void store_packed(longlong val_arg, Item *example); /* Having a clone_item method tells optimizer that this object is a constant and need not be optimized further. @@ -4035,7 +4038,7 @@ public: Item *clone_item() { Item_cache_temporal *item= new Item_cache_temporal(cached_field_type); - item->store_packed(value); + item->store_packed(value, example); return item; } }; diff --git a/sql/item_cmpfunc.cc b/sql/item_cmpfunc.cc index d950c0c1443..3b09da68927 100644 --- a/sql/item_cmpfunc.cc +++ b/sql/item_cmpfunc.cc @@ -1,5 +1,5 @@ /* Copyright (c) 2000, 2012, Oracle and/or its affiliates. - Copyright (c) 2009, 2012 Monty Program Ab + Copyright (c) 2009, 2013, Monty Program Ab. 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 @@ -914,7 +914,7 @@ get_datetime_value(THD *thd, Item ***item_arg, Item **cache_arg, if (save_arena) thd->set_query_arena(save_arena); - cache->store_packed(value); + cache->store_packed(value, item); *cache_arg= cache; *item_arg= cache_arg; } @@ -1353,7 +1353,7 @@ int Arg_comparator::compare_e_row() void Item_func_truth::fix_length_and_dec() { - maybe_null= 0; + set_persist_maybe_null(0); null_value= 0; decimals= 0; max_length= 1; @@ -1865,7 +1865,8 @@ longlong Item_func_eq::val_int() void Item_func_equal::fix_length_and_dec() { Item_bool_func2::fix_length_and_dec(); - maybe_null=null_value=0; + set_persist_maybe_null(0); + null_value= 0; } longlong Item_func_equal::val_int() @@ -2004,7 +2005,7 @@ void Item_func_interval::fix_length_and_dec() } } } - maybe_null= 0; + set_persist_maybe_null(0); max_length= 2; used_tables_cache|= row->used_tables(); not_null_tables_cache= row->not_null_tables(); @@ -2685,7 +2686,7 @@ void Item_func_nullif::fix_length_and_dec() { Item_bool_func2::fix_length_and_dec(); - maybe_null=1; + set_persist_maybe_null(1); if (args[0]) // Only false if EOM { max_length=args[0]->max_length; @@ -4546,6 +4547,8 @@ void Item_cond::update_used_tables() item->update_used_tables(); used_tables_cache|= item->used_tables(); const_item_cache&= item->const_item(); + if (!persistent_maybe_null && item->maybe_null) + maybe_null= 1; } } @@ -4720,10 +4723,9 @@ longlong Item_is_not_null_test::val_int() */ void Item_is_not_null_test::update_used_tables() { + args[0]->update_used_tables(); if (!args[0]->maybe_null) used_tables_cache= 0; /* is always true */ - else - args[0]->update_used_tables(); } @@ -5004,7 +5006,7 @@ Item_func_regex::fix_fields(THD *thd, Item **ref) int comp_res= regcomp(TRUE); if (comp_res == -1) { // Will always return NULL - maybe_null=1; + set_persist_maybe_null(1); fixed= 1; return FALSE; } @@ -5014,7 +5016,7 @@ Item_func_regex::fix_fields(THD *thd, Item **ref) maybe_null= args[0]->maybe_null; } else - maybe_null=1; + set_persist_maybe_null(1); fixed= 1; return FALSE; } @@ -5828,6 +5830,8 @@ void Item_equal::update_used_tables() used_tables_cache|= item->used_tables(); /* see commentary at Item_equal::update_const() */ const_item_cache&= item->const_item() && !item->is_outer_field(); + if (!persistent_maybe_null && item->maybe_null) + maybe_null= 1; } } @@ -6068,23 +6072,87 @@ Item* Item_equal::get_first(JOIN_TAB *context, Item *field_item) } -longlong Item_func_dyncol_exists::val_int() +longlong Item_func_dyncol_check::val_int() { char buff[STRING_BUFFER_USUAL_SIZE]; String tmp(buff, sizeof(buff), &my_charset_bin); DYNAMIC_COLUMN col; String *str; - ulonglong num; enum enum_dyncol_func_result rc; - num= args[1]->val_int(); + str= args[0]->val_str(&tmp); + if (args[0]->null_value) + goto null; + col.length= str->length(); + /* We do not change the string, so could do this trick */ + col.str= (char *)str->ptr(); + rc= mariadb_dyncol_check(&col); + if (rc < 0 && rc != ER_DYNCOL_FORMAT) + { + dynamic_column_error_message(rc); + goto null; + } + null_value= FALSE; + return rc == ER_DYNCOL_OK; + +null: + null_value= TRUE; + return 0; +} + +longlong Item_func_dyncol_exists::val_int() +{ + char buff[STRING_BUFFER_USUAL_SIZE], nmstrbuf[11]; + String tmp(buff, sizeof(buff), &my_charset_bin), + nmbuf(nmstrbuf, sizeof(nmstrbuf), system_charset_info); + DYNAMIC_COLUMN col; + String *str; + LEX_STRING buf, *name= NULL; + ulonglong num= 0; + enum enum_dyncol_func_result rc; + + if (args[1]->result_type() == INT_RESULT) + num= args[1]->val_int(); + else + { + String *nm= args[1]->val_str(&nmbuf); + if (!nm || args[1]->null_value) + { + null_value= 1; + return 1; + } + if (my_charset_same(nm->charset(), &my_charset_utf8_general_ci)) + { + buf.str= (char *) nm->ptr(); + buf.length= nm->length(); + } + else + { + uint strlen; + uint dummy_errors; + buf.str= (char *)sql_alloc((strlen= nm->length() * + my_charset_utf8_general_ci.mbmaxlen + 1)); + if (buf.str) + { + buf.length= + copy_and_convert(buf.str, strlen, &my_charset_utf8_general_ci, + nm->ptr(), nm->length(), nm->charset(), + &dummy_errors); + } + else + buf.length= 0; + } + name= &buf; + } str= args[0]->val_str(&tmp); if (args[0]->null_value || args[1]->null_value || num > UINT_MAX16) goto null; col.length= str->length(); /* We do not change the string, so could do this trick */ col.str= (char *)str->ptr(); - rc= dynamic_column_exists(&col, (uint) num); + rc= ((name == NULL) ? + mariadb_dyncol_exists(&col, (uint) num) : + mariadb_dyncol_exists_named(&col, name)); if (rc < 0) { dynamic_column_error_message(rc); diff --git a/sql/item_cmpfunc.h b/sql/item_cmpfunc.h index 34d1a0bd0ae..afb7bf005bb 100644 --- a/sql/item_cmpfunc.h +++ b/sql/item_cmpfunc.h @@ -767,6 +767,11 @@ public: my_decimal *decimal_op(my_decimal *); enum_field_types field_type() const; void fix_length_and_dec(); + void update_used_tables() + { + Item_func_coalesce::update_used_tables(); + maybe_null|= args[1]->maybe_null; + } const char *func_name() const { return "ifnull"; } Field *tmp_table_field(TABLE *table); uint decimal_precision() const; @@ -789,6 +794,11 @@ public: enum_field_types field_type() const { return cached_field_type; } bool fix_fields(THD *, Item **); void fix_length_and_dec(); + void update_used_tables() + { + Item_func::update_used_tables(); + maybe_null|= args[1]->maybe_null || args[2]->maybe_null; + } uint decimal_precision() const; const char *func_name() const { return "if"; } bool eval_not_null_tables(uchar *opt_arg); @@ -1254,6 +1264,12 @@ public: my_decimal *val_decimal(my_decimal *); bool fix_fields(THD *thd, Item **ref); void fix_length_and_dec(); + void update_used_tables() + { + Item_func::update_used_tables(); + if (else_expr_num == -1 || args[else_expr_num]->maybe_null) + maybe_null= 1; + } uint decimal_precision() const; table_map not_null_tables() const { return 0; } enum Item_result result_type () const { return cached_result_type; } @@ -1375,13 +1391,14 @@ public: enum Functype functype() const { return ISNULL_FUNC; } void fix_length_and_dec() { - decimals=0; max_length=1; maybe_null=0; + decimals=0; max_length=1; set_persist_maybe_null(0); update_used_tables(); } const char *func_name() const { return "isnull"; } /* Optimize case of not_null_column IS NULL */ virtual void update_used_tables() { + args[0]->update_used_tables(); if (!args[0]->maybe_null) { used_tables_cache= 0; /* is always false */ @@ -1389,7 +1406,6 @@ public: } else { - args[0]->update_used_tables(); used_tables_cache= args[0]->used_tables(); const_item_cache= args[0]->const_item(); } @@ -1437,7 +1453,7 @@ public: enum Functype functype() const { return ISNOTNULL_FUNC; } void fix_length_and_dec() { - decimals=0; max_length=1; maybe_null=0; + decimals=0; max_length=1; set_persist_maybe_null(0); } const char *func_name() const { return "isnotnull"; } optimize_type select_optimize() const { return OPTIMIZE_NULL; } @@ -1505,6 +1521,12 @@ public: void cleanup(); longlong val_int(); bool fix_fields(THD *thd, Item **ref); + void update_used_tables() + { + Item_bool_func::update_used_tables(); + if (regex_is_const) + maybe_null= 1; + } const char *func_name() const { return "regexp"; } virtual inline void print(String *str, enum_query_type query_type) @@ -1861,6 +1883,14 @@ public: Item *neg_transformer(THD *thd); }; +class Item_func_dyncol_check :public Item_bool_func +{ +public: + Item_func_dyncol_check(Item *str) :Item_bool_func(str) {} + longlong val_int(); + const char *func_name() const { return "column_check"; } +}; + class Item_func_dyncol_exists :public Item_bool_func { public: diff --git a/sql/item_create.cc b/sql/item_create.cc index 07e7f7b7ff9..fc31b074055 100644 --- a/sql/item_create.cc +++ b/sql/item_create.cc @@ -526,6 +526,54 @@ protected: virtual ~Create_func_coercibility() {} }; +class Create_func_dyncol_check : public Create_func_arg1 +{ +public: + virtual Item *create_1_arg(THD *thd, Item *arg1); + + static Create_func_dyncol_check s_singleton; + +protected: + Create_func_dyncol_check() {} + virtual ~Create_func_dyncol_check() {} +}; + +class Create_func_dyncol_exists : public Create_func_arg2 +{ +public: + virtual Item *create_2_arg(THD *thd, Item *arg1, Item *arg2); + + static Create_func_dyncol_exists s_singleton; + +protected: + Create_func_dyncol_exists() {} + virtual ~Create_func_dyncol_exists() {} +}; + +class Create_func_dyncol_list : public Create_func_arg1 +{ +public: + virtual Item *create_1_arg(THD *thd, Item *arg1); + + static Create_func_dyncol_list s_singleton; + +protected: + Create_func_dyncol_list() {} + virtual ~Create_func_dyncol_list() {} +}; + +class Create_func_dyncol_json : public Create_func_arg1 +{ +public: + virtual Item *create_1_arg(THD *thd, Item *arg1); + + static Create_func_dyncol_json s_singleton; + +protected: + Create_func_dyncol_json() {} + virtual ~Create_func_dyncol_json() {} +}; + class Create_func_compress : public Create_func_arg1 { @@ -3108,6 +3156,38 @@ Create_func_coercibility::create_1_arg(THD *thd, Item *arg1) } +Create_func_dyncol_check Create_func_dyncol_check::s_singleton; + +Item* +Create_func_dyncol_check::create_1_arg(THD *thd, Item *arg1) +{ + return new (thd->mem_root) Item_func_dyncol_check(arg1); +} + +Create_func_dyncol_exists Create_func_dyncol_exists::s_singleton; + +Item* +Create_func_dyncol_exists::create_2_arg(THD *thd, Item *arg1, Item *arg2) +{ + return new (thd->mem_root) Item_func_dyncol_exists(arg1, arg2); +} + +Create_func_dyncol_list Create_func_dyncol_list::s_singleton; + +Item* +Create_func_dyncol_list::create_1_arg(THD *thd, Item *arg1) +{ + return new (thd->mem_root) Item_func_dyncol_list(arg1); +} + +Create_func_dyncol_json Create_func_dyncol_json::s_singleton; + +Item* +Create_func_dyncol_json::create_1_arg(THD *thd, Item *arg1) +{ + return new (thd->mem_root) Item_func_dyncol_json(arg1); +} + Create_func_concat Create_func_concat::s_singleton; Item* @@ -5252,6 +5332,10 @@ static Native_func_registry func_array[] = { { C_STRING_WITH_LEN("CHARACTER_LENGTH") }, BUILDER(Create_func_char_length)}, { { C_STRING_WITH_LEN("CHAR_LENGTH") }, BUILDER(Create_func_char_length)}, { { C_STRING_WITH_LEN("COERCIBILITY") }, BUILDER(Create_func_coercibility)}, + { { C_STRING_WITH_LEN("COLUMN_CHECK") }, BUILDER(Create_func_dyncol_check)}, + { { C_STRING_WITH_LEN("COLUMN_EXISTS") }, BUILDER(Create_func_dyncol_exists)}, + { { C_STRING_WITH_LEN("COLUMN_LIST") }, BUILDER(Create_func_dyncol_list)}, + { { C_STRING_WITH_LEN("COLUMN_JSON") }, BUILDER(Create_func_dyncol_json)}, { { C_STRING_WITH_LEN("COMPRESS") }, BUILDER(Create_func_compress)}, { { C_STRING_WITH_LEN("CONCAT") }, BUILDER(Create_func_concat)}, { { C_STRING_WITH_LEN("CONCAT_WS") }, BUILDER(Create_func_concat_ws)}, @@ -5711,7 +5795,7 @@ static List<Item> *create_func_dyncol_prepare(THD *thd, for (uint i= 0; (def= li++) ;) { dfs[0][i++]= *def; - args->push_back(def->num); + args->push_back(def->key); args->push_back(def->value); } return args; @@ -5727,7 +5811,6 @@ Item *create_func_dyncol_create(THD *thd, List<DYNCALL_CREATE_DEF> &list) return new (thd->mem_root) Item_func_dyncol_create(*args, dfs); } - Item *create_func_dyncol_add(THD *thd, Item *str, List<DYNCALL_CREATE_DEF> &list) { @@ -5747,7 +5830,7 @@ Item *create_func_dyncol_add(THD *thd, Item *str, Item *create_func_dyncol_delete(THD *thd, Item *str, List<Item> &nums) { DYNCALL_CREATE_DEF *dfs; - Item *num; + Item *key; List_iterator_fast<Item> it(nums); List<Item> *args= new (thd->mem_root) List<Item>; @@ -5757,12 +5840,12 @@ Item *create_func_dyncol_delete(THD *thd, Item *str, List<Item> &nums) if (!args || !dfs) return NULL; - for (uint i= 0; (num= it++); i++) + for (uint i= 0; (key= it++); i++) { - dfs[i].num= num; + dfs[i].key= key; dfs[i].value= new Item_null(); dfs[i].type= DYN_COL_INT; - args->push_back(dfs[i].num); + args->push_back(dfs[i].key); args->push_back(dfs[i].value); } diff --git a/sql/item_create.h b/sql/item_create.h index ac6b0f8454f..5ecb45e9eae 100644 --- a/sql/item_create.h +++ b/sql/item_create.h @@ -180,5 +180,6 @@ Item *create_func_dyncol_get(THD *thd, Item *num, Item *str, Cast_target cast_type, const char *c_len, const char *c_dec, CHARSET_INFO *cs); +Item *create_func_dyncol_json(THD *thd, Item *str); #endif diff --git a/sql/item_func.cc b/sql/item_func.cc index 4c9f0a65d8a..390ece724cb 100644 --- a/sql/item_func.cc +++ b/sql/item_func.cc @@ -1,5 +1,5 @@ /* Copyright (c) 2000, 2011, Oracle and/or its affiliates. - Copyright (c) 2008-2011 Monty Program Ab + Copyright (c) 2009, 2013, Monty Program Ab. 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 @@ -111,7 +111,7 @@ void Item_func::set_arguments(List<Item> &list) } Item_func::Item_func(List<Item> &list) - :allowed_arg_cols(1) + :allowed_arg_cols(1), persistent_maybe_null(0) { set_arguments(list); } @@ -119,6 +119,7 @@ Item_func::Item_func(List<Item> &list) Item_func::Item_func(THD *thd, Item_func *item) :Item_result_field(thd, item), allowed_arg_cols(item->allowed_arg_cols), + persistent_maybe_null(0), arg_count(item->arg_count), used_tables_cache(item->used_tables_cache), not_null_tables_cache(item->not_null_tables_cache), @@ -446,6 +447,8 @@ void Item_func::update_used_tables() args[i]->update_used_tables(); used_tables_cache|=args[i]->used_tables(); const_item_cache&=args[i]->const_item(); + if (!persistent_maybe_null && args[i]->maybe_null) + maybe_null= 1; } } @@ -1712,7 +1715,7 @@ void Item_func_div::fix_length_and_dec() case IMPOSSIBLE_RESULT: DBUG_ASSERT(0); } - maybe_null= 1; // devision by zero + set_persist_maybe_null(1); // devision by zero DBUG_VOID_RETURN; } @@ -1796,7 +1799,7 @@ void Item_func_int_div::fix_length_and_dec() max_length=args[0]->max_length - (argtype == DECIMAL_RESULT || argtype == INT_RESULT ? args[0]->decimals : 0); - maybe_null=1; + set_persist_maybe_null(1); unsigned_flag=args[0]->unsigned_flag | args[1]->unsigned_flag; } @@ -1883,7 +1886,7 @@ void Item_func_mod::result_precision() void Item_func_mod::fix_length_and_dec() { Item_num_op::fix_length_and_dec(); - maybe_null= 1; + set_persist_maybe_null(1); unsigned_flag= args[0]->unsigned_flag; } @@ -3078,7 +3081,7 @@ longlong Item_func_field::val_int() void Item_func_field::fix_length_and_dec() { - maybe_null=0; max_length=3; + set_persist_maybe_null(0); max_length=3; cmp_type= args[0]->result_type(); for (uint i=1; i < arg_count ; i++) cmp_type= item_cmp_type(cmp_type, args[i]->result_type()); @@ -4194,7 +4197,8 @@ longlong Item_func_last_insert_id::val_int() thd->first_successful_insert_id_in_prev_stmt= value; return value; } - return thd->read_first_successful_insert_id_in_prev_stmt(); + return + static_cast<longlong>(thd->read_first_successful_insert_id_in_prev_stmt()); } @@ -4341,7 +4345,9 @@ user_var_entry *get_variable(HASH *hash, LEX_STRING &name, uint size=ALIGN_SIZE(sizeof(user_var_entry))+name.length+1+extra_size; if (!my_hash_inited(hash)) return 0; - if (!(entry = (user_var_entry*) my_malloc(size,MYF(MY_WME | ME_FATALERROR)))) + if (!(entry = (user_var_entry*) my_malloc(size, + MYF(MY_WME | ME_FATALERROR | + MY_THREAD_SPECIFIC)))) return 0; entry->name.str=(char*) entry+ ALIGN_SIZE(sizeof(user_var_entry))+ extra_size; @@ -4569,7 +4575,8 @@ update_hash(user_var_entry *entry, bool set_null, void *ptr, uint length, entry->value=0; entry->value= (char*) my_realloc(entry->value, length, MYF(MY_ALLOW_ZERO_PTR | MY_WME | - ME_FATALERROR)); + ME_FATALERROR | + MY_THREAD_SPECIFIC)); if (!entry->value) return 1; } @@ -5312,7 +5319,7 @@ void Item_func_get_user_var::fix_length_and_dec() { THD *thd=current_thd; int error; - maybe_null=1; + set_persist_maybe_null(1); decimals=NOT_FIXED_DEC; max_length=MAX_BLOB_WIDTH; @@ -5511,7 +5518,7 @@ void Item_func_get_system_var::update_null_value() void Item_func_get_system_var::fix_length_and_dec() { char *cptr; - maybe_null= TRUE; + set_persist_maybe_null(1); max_length= 0; if (var->check_type(var_type)) @@ -6078,7 +6085,7 @@ bool Item_func_match::fix_fields(THD *thd, Item **ref) status_var_increment(thd->status_var.feature_fulltext); - maybe_null=1; + set_persist_maybe_null(1); join_key=0; /* @@ -6412,7 +6419,7 @@ longlong Item_func_row_count::val_int() Item_func_sp::Item_func_sp(Name_resolution_context *context_arg, sp_name *name) :Item_func(), context(context_arg), m_name(name), m_sp(NULL), sp_result_field(NULL) { - maybe_null= 1; + set_persist_maybe_null(1); m_name->init_qname(current_thd); dummy_table= (TABLE*) sql_calloc(sizeof(TABLE)+ sizeof(TABLE_SHARE)); dummy_table->s= (TABLE_SHARE*) (dummy_table+1); @@ -6423,7 +6430,7 @@ Item_func_sp::Item_func_sp(Name_resolution_context *context_arg, sp_name *name, List<Item> &list) :Item_func(list), context(context_arg), m_name(name), m_sp(NULL),sp_result_field(NULL) { - maybe_null= 1; + set_persist_maybe_null(1); m_name->init_qname(current_thd); dummy_table= (TABLE*) sql_calloc(sizeof(TABLE)+ sizeof(TABLE_SHARE)); dummy_table->s= (TABLE_SHARE*) (dummy_table+1); @@ -6551,6 +6558,19 @@ Item_func_sp::init_result_field(THD *thd) /** + @note + Deterministic stored procedures are considered inexpensive. + Consequently such procedures may be evaluated during optimization, + if they are constant (checked by the optimizer). +*/ + +bool Item_func_sp::is_expensive() +{ + return !(m_sp->m_chistics->detistic); +} + + +/** @brief Initialize local members with values from the Field interface. @note called from Item::fix_fields. @@ -6564,7 +6584,7 @@ void Item_func_sp::fix_length_and_dec() decimals= sp_result_field->decimals(); max_length= sp_result_field->field_length; collation.set(sp_result_field->charset()); - maybe_null= 1; + set_persist_maybe_null(1); unsigned_flag= test(sp_result_field->flags & UNSIGNED_FLAG); DBUG_VOID_RETURN; diff --git a/sql/item_func.h b/sql/item_func.h index 80fa0b5d634..f562c87fe1c 100644 --- a/sql/item_func.h +++ b/sql/item_func.h @@ -1,7 +1,7 @@ #ifndef ITEM_FUNC_INCLUDED #define ITEM_FUNC_INCLUDED /* Copyright (c) 2000, 2011, Oracle and/or its affiliates. - Copyright (c) 2009-2011 Monty Program Ab + Copyright (c) 2009, 2013, Monty Program Ab. 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 @@ -39,6 +39,8 @@ protected: 0 means get this number from first argument */ uint allowed_arg_cols; + /* maybe_null can't be changed by parameters or used table state */ + bool persistent_maybe_null; public: uint arg_count; table_map used_tables_cache, not_null_tables_cache; @@ -58,19 +60,19 @@ public: NOW_FUNC, TRIG_COND_FUNC, SUSERVAR_FUNC, GUSERVAR_FUNC, COLLATE_FUNC, EXTRACT_FUNC, CHAR_TYPECAST_FUNC, FUNC_SP, UDF_FUNC, - NEG_FUNC, GSYSVAR_FUNC }; + NEG_FUNC, GSYSVAR_FUNC, DYNCOL_FUNC }; enum optimize_type { OPTIMIZE_NONE,OPTIMIZE_KEY,OPTIMIZE_OP, OPTIMIZE_NULL, OPTIMIZE_EQUAL }; enum Type type() const { return FUNC_ITEM; } virtual enum Functype functype() const { return UNKNOWN_FUNC; } Item_func(void): - allowed_arg_cols(1), arg_count(0) + allowed_arg_cols(1), persistent_maybe_null(0), arg_count(0) { with_sum_func= 0; with_field= 0; } Item_func(Item *a): - allowed_arg_cols(1), arg_count(1) + allowed_arg_cols(1), persistent_maybe_null(0), arg_count(1) { args= tmp_arg; args[0]= a; @@ -78,7 +80,7 @@ public: with_field= a->with_field; } Item_func(Item *a,Item *b): - allowed_arg_cols(1), arg_count(2) + allowed_arg_cols(1), persistent_maybe_null(0), arg_count(2) { args= tmp_arg; args[0]= a; args[1]= b; @@ -86,7 +88,7 @@ public: with_field= a->with_field || b->with_field; } Item_func(Item *a,Item *b,Item *c): - allowed_arg_cols(1) + allowed_arg_cols(1), persistent_maybe_null(0) { arg_count= 0; if ((args= (Item**) sql_alloc(sizeof(Item*)*3))) @@ -98,7 +100,7 @@ public: } } Item_func(Item *a,Item *b,Item *c,Item *d): - allowed_arg_cols(1) + allowed_arg_cols(1), persistent_maybe_null(0) { arg_count= 0; if ((args= (Item**) sql_alloc(sizeof(Item*)*4))) @@ -112,7 +114,7 @@ public: } } Item_func(Item *a,Item *b,Item *c,Item *d,Item* e): - allowed_arg_cols(1) + allowed_arg_cols(1), persistent_maybe_null(0) { arg_count= 5; if ((args= (Item**) sql_alloc(sizeof(Item*)*5))) @@ -170,6 +172,18 @@ public: my_decimal *val_decimal(my_decimal *); + void fix_char_length_ulonglong(ulonglong max_char_length_arg) + { + ulonglong max_result_length= max_char_length_arg * + collation.collation->mbmaxlen; + if (max_result_length >= MAX_BLOB_WIDTH) + { + max_length= MAX_BLOB_WIDTH; + set_persist_maybe_null(1); + } + else + max_length= (uint32) max_result_length; + } bool agg_arg_charsets(DTCollation &c, Item **items, uint nitems, uint flags, int item_sep) { @@ -371,6 +385,11 @@ public: info.bool_function= &Item::restore_to_before_no_rows_in_result; walk(&Item::call_bool_func_processor, FALSE, (uchar*) &info); } + inline void set_persist_maybe_null(bool mb_null) + { + maybe_null= mb_null; + persistent_maybe_null= 1; + } }; @@ -586,7 +605,7 @@ public: } double val_real(); enum_field_types field_type() const { return MYSQL_TYPE_DOUBLE; } - void fix_length_and_dec() { maybe_null= 1; } + void fix_length_and_dec() { set_persist_maybe_null(1); } const char *func_name() const { return "double_typecast"; } virtual void print(String *str, enum_query_type query_type); }; @@ -727,7 +746,7 @@ class Item_dec_func :public Item_real_func void fix_length_and_dec() { decimals=NOT_FIXED_DEC; max_length=float_length(decimals); - maybe_null=1; + set_persist_maybe_null(1); } }; @@ -1059,7 +1078,7 @@ public: Item_func_coercibility(Item *a) :Item_int_func(a) {} longlong val_int(); const char *func_name() const { return "coercibility"; } - void fix_length_and_dec() { max_length=10; maybe_null= 0; } + void fix_length_and_dec() { max_length=10; set_persist_maybe_null(0); } table_map not_null_tables() const { return 0; } }; @@ -1201,6 +1220,7 @@ public: const char *func_name() const { return "last_insert_id"; } void fix_length_and_dec() { + unsigned_flag= TRUE; if (arg_count) max_length= args[0]->max_length; unsigned_flag=1; @@ -1221,7 +1241,7 @@ public: {} longlong val_int(); const char *func_name() const { return "benchmark"; } - void fix_length_and_dec() { max_length=1; maybe_null=0; } + void fix_length_and_dec() { max_length=1; set_persist_maybe_null(0); } virtual void print(String *str, enum_query_type query_type); bool check_vcol_func_processor(uchar *int_arg) { @@ -1474,7 +1494,7 @@ public: double val_real() { DBUG_ASSERT(fixed == 1); null_value= 1; return 0.0; } longlong val_int() { DBUG_ASSERT(fixed == 1); null_value=1; return 0; } enum Item_result result_type () const { return STRING_RESULT; } - void fix_length_and_dec() { maybe_null=1; max_length=0; } + void fix_length_and_dec() { set_persist_maybe_null(1); max_length=0; } }; #endif /* HAVE_DLOPEN */ @@ -1495,7 +1515,7 @@ class Item_func_get_lock :public Item_int_func Item_func_get_lock(Item *a,Item *b) :Item_int_func(a,b) {} longlong val_int(); const char *func_name() const { return "get_lock"; } - void fix_length_and_dec() { max_length=1; maybe_null=1;} + void fix_length_and_dec() { max_length=1; set_persist_maybe_null(1);} bool check_vcol_func_processor(uchar *int_arg) { return trace_unsupported_by_check_vcol_func_processor(func_name()); @@ -1509,7 +1529,7 @@ public: Item_func_release_lock(Item *a) :Item_int_func(a) {} longlong val_int(); const char *func_name() const { return "release_lock"; } - void fix_length_and_dec() { max_length=1; maybe_null=1;} + void fix_length_and_dec() { max_length=1; set_persist_maybe_null(1);} bool check_vcol_func_processor(uchar *int_arg) { return trace_unsupported_by_check_vcol_func_processor(func_name()); @@ -1527,7 +1547,7 @@ public: Item_master_pos_wait(Item *a,Item *b, Item *c, Item *d) :Item_int_func(a,b,c,d) {} longlong val_int(); const char *func_name() const { return "master_pos_wait"; } - void fix_length_and_dec() { max_length=21; maybe_null=1;} + void fix_length_and_dec() { max_length=21; set_persist_maybe_null(1);} bool check_vcol_func_processor(uchar *int_arg) { return trace_unsupported_by_check_vcol_func_processor(func_name()); @@ -1744,7 +1764,8 @@ public: Item_func_inet_aton(Item *a) :Item_int_func(a) {} longlong val_int(); const char *func_name() const { return "inet_aton"; } - void fix_length_and_dec() { decimals= 0; max_length= 21; maybe_null= 1; unsigned_flag= 1;} + void fix_length_and_dec() + { decimals= 0; max_length= 21; set_persist_maybe_null(1); unsigned_flag= 1; } }; @@ -1813,7 +1834,8 @@ public: Item_func_is_free_lock(Item *a) :Item_int_func(a) {} longlong val_int(); const char *func_name() const { return "is_free_lock"; } - void fix_length_and_dec() { decimals=0; max_length=1; maybe_null=1;} + void fix_length_and_dec() + { decimals= 0; max_length= 1; set_persist_maybe_null(1); } bool check_vcol_func_processor(uchar *int_arg) { return trace_unsupported_by_check_vcol_func_processor(func_name()); @@ -1827,7 +1849,8 @@ public: Item_func_is_used_lock(Item *a) :Item_int_func(a) {} longlong val_int(); const char *func_name() const { return "is_used_lock"; } - void fix_length_and_dec() { decimals=0; max_length=10; maybe_null=1;} + void fix_length_and_dec() + { decimals= 0; max_length= 10; set_persist_maybe_null(1);} bool check_vcol_func_processor(uchar *int_arg) { return trace_unsupported_by_check_vcol_func_processor(func_name()); @@ -1850,7 +1873,7 @@ public: Item_func_row_count() :Item_int_func() {} longlong val_int(); const char *func_name() const { return "row_count"; } - void fix_length_and_dec() { decimals= 0; maybe_null=0; } + void fix_length_and_dec() { decimals= 0; set_persist_maybe_null(0); } bool check_vcol_func_processor(uchar *int_arg) { @@ -1887,7 +1910,8 @@ private: bool init_result_field(THD *thd); protected: - bool is_expensive_processor(uchar *arg) { return TRUE; } + bool is_expensive_processor(uchar *arg) + { return is_expensive(); } public: @@ -1966,7 +1990,7 @@ public: bool fix_fields(THD *thd, Item **ref); void fix_length_and_dec(void); - bool is_expensive() { return 1; } + bool is_expensive(); inline Field *get_sp_result_field() { @@ -1990,7 +2014,7 @@ public: Item_func_found_rows() :Item_int_func() {} longlong val_int(); const char *func_name() const { return "found_rows"; } - void fix_length_and_dec() { decimals= 0; maybe_null=0; } + void fix_length_and_dec() { decimals= 0; set_persist_maybe_null(0); } bool check_vcol_func_processor(uchar *int_arg) { return trace_unsupported_by_check_vcol_func_processor(func_name()); @@ -2033,6 +2057,11 @@ public: enum_field_types field_type() const { return last_value->field_type(); } bool const_item() const { return 0; } void evaluate_sideeffects(); + void update_used_tables() + { + Item_func::update_used_tables(); + maybe_null= last_value->maybe_null; + } }; diff --git a/sql/item_geofunc.cc b/sql/item_geofunc.cc index bc89a6c14b3..e3e80bdf59f 100644 --- a/sql/item_geofunc.cc +++ b/sql/item_geofunc.cc @@ -53,7 +53,7 @@ void Item_geometry_func::fix_length_and_dec() collation.set(&my_charset_bin); decimals=0; max_length= (uint32) 4294967295U; - maybe_null= 1; + set_persist_maybe_null(1); } @@ -147,7 +147,7 @@ void Item_func_as_wkt::fix_length_and_dec() { collation.set(default_charset(), DERIVATION_COERCIBLE, MY_REPERTOIRE_ASCII); max_length=MAX_BLOB_WIDTH; - maybe_null= 1; + set_persist_maybe_null(1); } diff --git a/sql/item_geofunc.h b/sql/item_geofunc.h index 3638d9f62e8..ee61f921adb 100644 --- a/sql/item_geofunc.h +++ b/sql/item_geofunc.h @@ -89,7 +89,7 @@ public: { // "GeometryCollection" is the longest fix_length_and_charset(20, default_charset()); - maybe_null= 1; + set_persist_maybe_null(1); }; }; @@ -224,7 +224,7 @@ public: { Item_func::print(str, query_type); } - void fix_length_and_dec() { maybe_null= 1; } + void fix_length_and_dec() { set_persist_maybe_null(1); } bool is_null() { (void) val_int(); return null_value; } }; @@ -251,7 +251,7 @@ public: Item_func::print(str, query_type); } - void fix_length_and_dec() { maybe_null= 1; } + void fix_length_and_dec() { set_persist_maybe_null(1); } bool is_null() { (void) val_int(); return null_value; } }; @@ -342,7 +342,7 @@ public: longlong val_int(); optimize_type select_optimize() const { return OPTIMIZE_NONE; } const char *func_name() const { return "st_isempty"; } - void fix_length_and_dec() { maybe_null= 1; } + void fix_length_and_dec() { set_persist_maybe_null(1); } }; class Item_func_issimple: public Item_bool_func @@ -356,7 +356,7 @@ public: longlong val_int(); optimize_type select_optimize() const { return OPTIMIZE_NONE; } const char *func_name() const { return "st_issimple"; } - void fix_length_and_dec() { maybe_null= 1; } + void fix_length_and_dec() { set_persist_maybe_null(1); } }; class Item_func_isclosed: public Item_bool_func @@ -366,7 +366,7 @@ public: longlong val_int(); optimize_type select_optimize() const { return OPTIMIZE_NONE; } const char *func_name() const { return "st_isclosed"; } - void fix_length_and_dec() { maybe_null= 1; } + void fix_length_and_dec() { set_persist_maybe_null(1); } }; class Item_func_dimension: public Item_int_func @@ -376,7 +376,7 @@ public: Item_func_dimension(Item *a): Item_int_func(a) {} longlong val_int(); const char *func_name() const { return "st_dimension"; } - void fix_length_and_dec() { max_length= 10; maybe_null= 1; } + void fix_length_and_dec() { max_length= 10; set_persist_maybe_null(1); } }; class Item_func_x: public Item_real_func @@ -389,7 +389,7 @@ public: void fix_length_and_dec() { Item_real_func::fix_length_and_dec(); - maybe_null= 1; + set_persist_maybe_null(1); } }; @@ -404,7 +404,7 @@ public: void fix_length_and_dec() { Item_real_func::fix_length_and_dec(); - maybe_null= 1; + set_persist_maybe_null(1); } }; @@ -416,7 +416,7 @@ public: Item_func_numgeometries(Item *a): Item_int_func(a) {} longlong val_int(); const char *func_name() const { return "st_numgeometries"; } - void fix_length_and_dec() { max_length= 10; maybe_null= 1; } + void fix_length_and_dec() { max_length= 10; set_persist_maybe_null(1); } }; @@ -427,7 +427,7 @@ public: Item_func_numinteriorring(Item *a): Item_int_func(a) {} longlong val_int(); const char *func_name() const { return "st_numinteriorrings"; } - void fix_length_and_dec() { max_length= 10; maybe_null= 1; } + void fix_length_and_dec() { max_length= 10; set_persist_maybe_null(1); } }; @@ -438,7 +438,7 @@ public: Item_func_numpoints(Item *a): Item_int_func(a) {} longlong val_int(); const char *func_name() const { return "st_numpoints"; } - void fix_length_and_dec() { max_length= 10; maybe_null= 1; } + void fix_length_and_dec() { max_length= 10; set_persist_maybe_null(1); } }; @@ -452,7 +452,7 @@ public: void fix_length_and_dec() { Item_real_func::fix_length_and_dec(); - maybe_null= 1; + set_persist_maybe_null(1); } }; @@ -467,7 +467,7 @@ public: void fix_length_and_dec() { Item_real_func::fix_length_and_dec(); - maybe_null= 1; + set_persist_maybe_null(1); } }; @@ -479,7 +479,7 @@ public: Item_func_srid(Item *a): Item_int_func(a) {} longlong val_int(); const char *func_name() const { return "srid"; } - void fix_length_and_dec() { max_length= 10; maybe_null= 1; } + void fix_length_and_dec() { max_length= 10; set_persist_maybe_null(1); } }; diff --git a/sql/item_row.cc b/sql/item_row.cc index 2c4a628075e..ee7bd837553 100644 --- a/sql/item_row.cc +++ b/sql/item_row.cc @@ -74,7 +74,8 @@ bool Item_row::fix_fields(THD *thd, Item **ref) Item **arg, **arg_end; for (arg= items, arg_end= items+arg_count; arg != arg_end ; arg++) { - if ((*arg)->fix_fields(thd, arg)) + if (!(*arg)->fixed && + (*arg)->fix_fields(thd, arg)) return TRUE; // we can't assign 'item' before, because fix_fields() can change arg Item *item= *arg; @@ -145,11 +146,13 @@ void Item_row::update_used_tables() { used_tables_cache= 0; const_item_cache= 1; + maybe_null= 0; for (uint i= 0; i < arg_count; i++) { items[i]->update_used_tables(); used_tables_cache|= items[i]->used_tables(); const_item_cache&= items[i]->const_item(); + maybe_null|= items[i]->maybe_null; } } diff --git a/sql/item_strfunc.cc b/sql/item_strfunc.cc index 021a793618f..5071e494f04 100644 --- a/sql/item_strfunc.cc +++ b/sql/item_strfunc.cc @@ -1,5 +1,6 @@ /* Copyright (c) 2000, 2011, Oracle and/or its affiliates. + Copyright (c) 2009, 2013, Monty Program Ab. 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 @@ -57,6 +58,7 @@ C_MODE_START #include "../mysys/my_static.h" // For soundex_map C_MODE_END +#include "sql_show.h" // append_identifier /** @todo Remove this. It is not safe to use a shared String object. @@ -339,7 +341,7 @@ String *Item_func_sha2::val_str_ascii(String *str) void Item_func_sha2::fix_length_and_dec() { - maybe_null = 1; + set_persist_maybe_null(1); max_length = 0; #if defined(HAVE_OPENSSL) && !defined(EMBEDDED_LIBRARY) @@ -465,7 +467,7 @@ String *Item_func_aes_decrypt::val_str(String *str) void Item_func_aes_decrypt::fix_length_and_dec() { max_length=args[0]->max_length; - maybe_null= 1; + set_persist_maybe_null(1); } @@ -1335,7 +1337,7 @@ void Item_str_func::left_right_max_length() if (args[1]->const_item()) { int length= (int) args[1]->val_int(); - if (length <= 0) + if (args[1]->null_value || length <= 0) char_length=0; else set_if_smaller(char_length, (uint) length); @@ -1442,7 +1444,9 @@ void Item_func_substr::fix_length_and_dec() if (args[1]->const_item()) { int32 start= (int32) args[1]->val_int(); - if (start < 0) + if (args[1]->null_value) + max_length= 0; + else if (start < 0) max_length= ((uint)(-start) > max_length) ? 0 : (uint)(-start); else max_length-= min((uint)(start - 1), max_length); @@ -1450,7 +1454,7 @@ void Item_func_substr::fix_length_and_dec() if (arg_count == 3 && args[2]->const_item()) { int32 length= (int32) args[2]->val_int(); - if (length <= 0) + if (args[2]->null_value || length <= 0) max_length=0; /* purecov: inspected */ else set_if_smaller(max_length,(uint) length); @@ -2409,7 +2413,7 @@ void Item_func_elt::fix_length_and_dec() set_if_bigger(decimals,args[i]->decimals); } fix_char_length(char_length); - maybe_null=1; // NULL if wrong first arg + set_persist_maybe_null(1); // NULL if wrong first arg } @@ -2648,7 +2652,9 @@ void Item_func_repeat::fix_length_and_dec() /* Assumes that the maximum length of a String is < INT_MAX32. */ /* Set here so that rest of code sees out-of-bound value as such. */ - if (count > INT_MAX32) + if (args[1]->null_value) + count= 0; + else if (count > INT_MAX32) count= INT_MAX32; ulonglong char_length= (ulonglong) args[0]->max_char_length() * count; @@ -2657,7 +2663,7 @@ void Item_func_repeat::fix_length_and_dec() else { max_length= MAX_BLOB_WIDTH; - maybe_null= 1; + set_persist_maybe_null(1); } } @@ -2727,14 +2733,16 @@ void Item_func_rpad::fix_length_and_dec() DBUG_ASSERT(collation.collation->mbmaxlen > 0); /* Assumes that the maximum length of a String is < INT_MAX32. */ /* Set here so that rest of code sees out-of-bound value as such. */ - if (char_length > INT_MAX32) + if (args[1]->null_value) + char_length= 0; + else if (char_length > INT_MAX32) char_length= INT_MAX32; fix_char_length_ulonglong(char_length); } else { max_length= MAX_BLOB_WIDTH; - maybe_null= 1; + set_persist_maybe_null(1); } } @@ -2831,14 +2839,16 @@ void Item_func_lpad::fix_length_and_dec() DBUG_ASSERT(collation.collation->mbmaxlen > 0); /* Assumes that the maximum length of a String is < INT_MAX32. */ /* Set here so that rest of code sees out-of-bound value as such. */ - if (char_length > INT_MAX32) + if (args[1]->null_value) + char_length= 0; + else if (char_length > INT_MAX32) char_length= INT_MAX32; fix_char_length_ulonglong(char_length); } else { max_length= MAX_BLOB_WIDTH; - maybe_null= 1; + set_persist_maybe_null(1); } } @@ -3776,7 +3786,8 @@ String *Item_func_uuid::val_str(String *str) Item_func_dyncol_create::Item_func_dyncol_create(List<Item> &args, DYNCALL_CREATE_DEF *dfs) - : Item_str_func(args), defs(dfs), vals(0), nums(0) + : Item_str_func(args), defs(dfs), vals(0), keys_num(NULL), keys_str(NULL), + names(FALSE), force_names(FALSE) { DBUG_ASSERT((args.elements & 0x1) == 0); // even number of arguments } @@ -3784,31 +3795,81 @@ Item_func_dyncol_create::Item_func_dyncol_create(List<Item> &args, bool Item_func_dyncol_create::fix_fields(THD *thd, Item **ref) { + uint i; bool res= Item_func::fix_fields(thd, ref); // no need Item_str_func here - vals= (DYNAMIC_COLUMN_VALUE *) alloc_root(thd->mem_root, - sizeof(DYNAMIC_COLUMN_VALUE) * - (arg_count / 2)); - nums= (uint *) alloc_root(thd->mem_root, - sizeof(uint) * (arg_count / 2)); - status_var_increment(thd->status_var.feature_dynamic_columns); - return res || vals == 0 || nums == 0; + if (!res) + { + vals= (DYNAMIC_COLUMN_VALUE *) alloc_root(thd->mem_root, + sizeof(DYNAMIC_COLUMN_VALUE) * + (arg_count / 2)); + for (i= 0; i + 1 < arg_count && args[i]->result_type() == INT_RESULT; i+= 2); + if (i + 1 < arg_count) + { + names= TRUE; + } + + keys_num= (uint *) alloc_root(thd->mem_root, + (sizeof(LEX_STRING) > sizeof(uint) ? + sizeof(LEX_STRING) : + sizeof(uint)) * + (arg_count / 2)); + keys_str= (LEX_STRING *) keys_num; + status_var_increment(thd->status_var.feature_dynamic_columns); + } + return res || vals == 0 || keys_num == 0; } void Item_func_dyncol_create::fix_length_and_dec() { - maybe_null= TRUE; + set_persist_maybe_null(1); collation.set(&my_charset_bin); decimals= 0; } -void Item_func_dyncol_create::prepare_arguments() +bool Item_func_dyncol_create::prepare_arguments(bool force_names_arg) { char buff[STRING_BUFFER_USUAL_SIZE]; String *res, tmp(buff, sizeof(buff), &my_charset_bin); uint column_count= (arg_count / 2); uint i; my_decimal dtmp, *dres; + force_names= force_names_arg; + + if (!(names || force_names)) + { + for (i= 0; i < column_count; i++) + { + uint valpos= i * 2 + 1; + DYNAMIC_COLUMN_TYPE type= defs[i].type; + if (type == DYN_COL_NULL) + switch (args[valpos]->field_type()) + { + case MYSQL_TYPE_VARCHAR: + case MYSQL_TYPE_ENUM: + case MYSQL_TYPE_SET: + case MYSQL_TYPE_TINY_BLOB: + case MYSQL_TYPE_MEDIUM_BLOB: + case MYSQL_TYPE_LONG_BLOB: + case MYSQL_TYPE_BLOB: + case MYSQL_TYPE_VAR_STRING: + case MYSQL_TYPE_STRING: + case MYSQL_TYPE_GEOMETRY: + type= DYN_COL_STRING; + break; + default: + break; + } + + if (type == DYN_COL_STRING && + args[valpos]->type() == Item::FUNC_ITEM && + ((Item_func *)args[valpos])->functype() == DYNCOL_FUNC) + { + force_names= 1; + break; + } + } + } /* get values */ for (i= 0; i < column_count; i++) @@ -3867,7 +3928,59 @@ void Item_func_dyncol_create::prepare_arguments() break; } } - nums[i]= (uint) args[i * 2]->val_int(); + if (type == DYN_COL_STRING && + args[valpos]->type() == Item::FUNC_ITEM && + ((Item_func *)args[valpos])->functype() == DYNCOL_FUNC) + { + DBUG_ASSERT(names || force_names); + type= DYN_COL_DYNCOL; + } + if (names || force_names) + { + res= args[i * 2]->val_str(&tmp); + if (res) + { + // guaranty UTF-8 string for names + if (my_charset_same(res->charset(), &my_charset_utf8_general_ci)) + { + keys_str[i].length= res->length(); + keys_str[i].str= sql_strmake(res->ptr(), res->length()); + } + else + { + uint strlen; + uint dummy_errors; + char *str= + (char *)sql_alloc((strlen= res->length() * + my_charset_utf8_general_ci.mbmaxlen + 1)); + if (str) + { + keys_str[i].length= + copy_and_convert(str, strlen, &my_charset_utf8_general_ci, + res->ptr(), res->length(), res->charset(), + &dummy_errors); + keys_str[i].str= str; + } + else + keys_str[i].length= 0; + + } + } + else + { + keys_str[i].length= 0; + keys_str[i].str= NULL; + } + } + else + keys_num[i]= (uint) args[i * 2]->val_int(); + if (args[i * 2]->null_value) + { + /* to make cleanup possible */ + for (; i < column_count; i++) + vals[i].type= DYN_COL_NULL; + return 1; + } vals[i].type= type; switch (type) { case DYN_COL_NULL: @@ -3882,11 +3995,11 @@ void Item_func_dyncol_create::prepare_arguments() case DYN_COL_DOUBLE: vals[i].x.double_value= args[valpos]->val_real(); break; + case DYN_COL_DYNCOL: case DYN_COL_STRING: res= args[valpos]->val_str(&tmp); if (res && - (vals[i].x.string.value.str= my_strndup(res->ptr(), res->length(), - MYF(MY_WME)))) + (vals[i].x.string.value.str= sql_strmake(res->ptr(), res->length()))) { vals[i].x.string.value.length= res->length(); vals[i].x.string.charset= res->charset(); @@ -3901,7 +4014,7 @@ void Item_func_dyncol_create::prepare_arguments() case DYN_COL_DECIMAL: if ((dres= args[valpos]->val_decimal(&dtmp))) { - dynamic_column_prepare_decimal(&vals[i]); + mariadb_dyncol_prepare_decimal(&vals[i]); DBUG_ASSERT(vals[i].x.decimal.value.len == dres->len); vals[i].x.decimal.value.intg= dres->intg; vals[i].x.decimal.value.frac= dres->frac; @@ -3911,7 +4024,7 @@ void Item_func_dyncol_create::prepare_arguments() } else { - dynamic_column_prepare_decimal(&vals[i]); // just to be safe + mariadb_dyncol_prepare_decimal(&vals[i]); // just to be safe DBUG_ASSERT(args[valpos]->null_value); } break; @@ -3930,24 +4043,12 @@ void Item_func_dyncol_create::prepare_arguments() } if (vals[i].type != DYN_COL_NULL && args[valpos]->null_value) { - if (vals[i].type == DYN_COL_STRING) - my_free(vals[i].x.string.value.str); vals[i].type= DYN_COL_NULL; } } + return FALSE; } -void Item_func_dyncol_create::cleanup_arguments() -{ - uint column_count= (arg_count / 2); - uint i; - - for (i= 0; i < column_count; i++) - { - if (vals[i].type == DYN_COL_STRING) - my_free(vals[i].x.string.value.str); - } -} String *Item_func_dyncol_create::val_str(String *str) { @@ -3957,30 +4058,37 @@ String *Item_func_dyncol_create::val_str(String *str) enum enum_dyncol_func_result rc; DBUG_ASSERT((arg_count & 0x1) == 0); // even number of arguments - prepare_arguments(); - - if ((rc= dynamic_column_create_many(&col, column_count, nums, vals))) + if (prepare_arguments(FALSE)) { - dynamic_column_error_message(rc); - dynamic_column_column_free(&col); res= NULL; - null_value= TRUE; + null_value= 1; } else { - /* Move result from DYNAMIC_COLUMN to str_value */ - char *ptr; - size_t length, alloc_length; - dynamic_column_reassociate(&col, &ptr, &length, &alloc_length); - str_value.reassociate(ptr, (uint32) length, (uint32) alloc_length, - &my_charset_bin); - res= &str_value; - null_value= FALSE; + if ((rc= ((names || force_names) ? + mariadb_dyncol_create_many_named(&col, column_count, keys_str, + vals, TRUE) : + mariadb_dyncol_create_many(&col, column_count, keys_num, + vals, TRUE)))) + { + dynamic_column_error_message(rc); + dynamic_column_column_free(&col); + res= NULL; + null_value= TRUE; + } + else + { + /* Move result from DYNAMIC_COLUMN to str_value */ + char *ptr; + size_t length, alloc_length; + dynstr_reassociate(&col, &ptr, &length, &alloc_length); + str_value.reassociate(ptr, (uint32) length, (uint32) alloc_length, + &my_charset_bin); + res= &str_value; + null_value= FALSE; + } } - /* cleanup */ - cleanup_arguments(); - return res; } @@ -4006,6 +4114,7 @@ void Item_func_dyncol_create::print_arguments(String *str, case DYN_COL_DOUBLE: str->append(STRING_WITH_LEN(" AS double")); break; + case DYN_COL_DYNCOL: case DYN_COL_STRING: str->append(STRING_WITH_LEN(" AS char")); if (defs[i].cs) @@ -4043,6 +4152,40 @@ void Item_func_dyncol_create::print(String *str, str->append(')'); } +String *Item_func_dyncol_json::val_str(String *str) +{ + DYNAMIC_STRING json, col; + String *res; + enum enum_dyncol_func_result rc; + + res= args[0]->val_str(str); + if (args[0]->null_value) + goto null; + + col.str= (char *)res->ptr(); + col.length= res->length(); + if ((rc= mariadb_dyncol_json(&col, &json))) + { + dynamic_column_error_message(rc); + goto null; + } + bzero(&col, sizeof(col)); + { + /* Move result from DYNAMIC_COLUMN to str */ + char *ptr; + size_t length, alloc_length; + dynstr_reassociate(&json, &ptr, &length, &alloc_length); + str->reassociate(ptr, (uint32) length, (uint32) alloc_length, + &my_charset_utf8_general_ci); + null_value= FALSE; + } + return str; + +null: + bzero(&col, sizeof(col)); + null_value= TRUE; + return NULL; +} String *Item_func_dyncol_add::val_str(String *str) { @@ -4054,21 +4197,25 @@ String *Item_func_dyncol_add::val_str(String *str) /* We store the packed data last */ res= args[arg_count - 1]->val_str(str); - if (args[arg_count - 1]->null_value) + if (args[arg_count - 1]->null_value || + init_dynamic_string(&col, NULL, res->length() + STRING_BUFFER_USUAL_SIZE, + STRING_BUFFER_USUAL_SIZE)) goto null; - init_dynamic_string(&col, NULL, res->length() + STRING_BUFFER_USUAL_SIZE, - STRING_BUFFER_USUAL_SIZE); col.length= res->length(); memcpy(col.str, res->ptr(), col.length); - prepare_arguments(); + if (prepare_arguments(mariadb_dyncol_has_names(&col))) + goto null; - if ((rc= dynamic_column_update_many(&col, column_count, nums, vals))) + if ((rc= ((names || force_names) ? + mariadb_dyncol_update_many_named(&col, column_count, + keys_str, vals) : + mariadb_dyncol_update_many(&col, column_count, + keys_num, vals)))) { dynamic_column_error_message(rc); dynamic_column_column_free(&col); - cleanup_arguments(); goto null; } @@ -4076,16 +4223,12 @@ String *Item_func_dyncol_add::val_str(String *str) /* Move result from DYNAMIC_COLUMN to str */ char *ptr; size_t length, alloc_length; - dynamic_column_reassociate(&col, &ptr, &length, &alloc_length); + dynstr_reassociate(&col, &ptr, &length, &alloc_length); str->reassociate(ptr, (uint32) length, (uint32) alloc_length, &my_charset_bin); null_value= FALSE; } - /* cleanup */ - dynamic_column_column_free(&col); - cleanup_arguments(); - return str; null: @@ -4117,10 +4260,48 @@ bool Item_dyncol_get::get_dyn_value(DYNAMIC_COLUMN_VALUE *val, String *tmp) { DYNAMIC_COLUMN dyn_str; String *res; - longlong num; + longlong num= 0; + LEX_STRING buf, *name= NULL; + char nmstrbuf[11]; + String nmbuf(nmstrbuf, sizeof(nmstrbuf), system_charset_info); enum enum_dyncol_func_result rc; - num= args[1]->val_int(); + if (args[1]->result_type() == INT_RESULT) + num= args[1]->val_int(); + else + { + String *nm= args[1]->val_str(&nmbuf); + if (!nm || args[1]->null_value) + { + null_value= 1; + return 1; + } + + if (my_charset_same(nm->charset(), &my_charset_utf8_general_ci)) + { + buf.str= (char *) nm->ptr(); + buf.length= nm->length(); + } + else + { + uint strlen; + uint dummy_errors; + buf.str= (char *)sql_alloc((strlen= nm->length() * + my_charset_utf8_general_ci.mbmaxlen + 1)); + if (buf.str) + { + buf.length= + copy_and_convert(buf.str, strlen, &my_charset_utf8_general_ci, + nm->ptr(), nm->length(), nm->charset(), + &dummy_errors); + } + else + buf.length= 0; + } + name= &buf; + } + + if (args[1]->null_value || num < 0 || num > INT_MAX) { null_value= 1; @@ -4136,7 +4317,9 @@ bool Item_dyncol_get::get_dyn_value(DYNAMIC_COLUMN_VALUE *val, String *tmp) dyn_str.str= (char*) res->ptr(); dyn_str.length= res->length(); - if ((rc= dynamic_column_get(&dyn_str, (uint) num, val))) + if ((rc= ((name == NULL) ? + mariadb_dyncol_get(&dyn_str, (uint) num, val) : + mariadb_dyncol_get_named(&dyn_str, name, val)))) { dynamic_column_error_message(rc); null_value= 1; @@ -4168,6 +4351,7 @@ String *Item_dyncol_get::val_str(String *str_result) case DYN_COL_DOUBLE: str_result->set_real(val.x.double_value, NOT_FIXED_DEC, &my_charset_latin1); break; + case DYN_COL_DYNCOL: case DYN_COL_STRING: if ((char*) tmp.ptr() <= val.x.string.value.str && (char*) tmp.ptr() + tmp.length() >= val.x.string.value.str) @@ -4190,8 +4374,7 @@ String *Item_dyncol_get::val_str(String *str_result) case DYN_COL_DECIMAL: { int res; - int length= - my_decimal_string_length((const my_decimal*)&val.x.decimal.value); + int length= decimal_string_size(&val.x.decimal.value); if (str_result->alloc(length)) goto null; if ((res= decimal2string(&val.x.decimal.value, (char*) str_result->ptr(), @@ -4244,6 +4427,7 @@ longlong Item_dyncol_get::val_int() return 0; switch (val.type) { + case DYN_COL_DYNCOL: case DYN_COL_NULL: goto null; case DYN_COL_UINT: @@ -4324,6 +4508,7 @@ double Item_dyncol_get::val_real() return 0.0; switch (val.type) { + case DYN_COL_DYNCOL: case DYN_COL_NULL: goto null; case DYN_COL_UINT: @@ -4381,6 +4566,7 @@ my_decimal *Item_dyncol_get::val_decimal(my_decimal *decimal_value) return NULL; switch (val.type) { + case DYN_COL_DYNCOL: case DYN_COL_NULL: goto null; case DYN_COL_UINT: @@ -4437,6 +4623,7 @@ bool Item_dyncol_get::get_date(MYSQL_TIME *ltime, ulonglong fuzzy_date) return 1; // Error switch (val.type) { + case DYN_COL_DYNCOL: case DYN_COL_NULL: goto null; case DYN_COL_INT: @@ -4482,7 +4669,6 @@ null: return 1; } - void Item_dyncol_get::print(String *str, enum_query_type query_type) { str->append(STRING_WITH_LEN("column_get(")); @@ -4497,7 +4683,8 @@ String *Item_func_dyncol_list::val_str(String *str) { uint i; enum enum_dyncol_func_result rc; - DYNAMIC_ARRAY arr; + LEX_STRING *names= 0; + uint count; DYNAMIC_COLUMN col; String *res= args[0]->val_str(str); @@ -4506,33 +4693,37 @@ String *Item_func_dyncol_list::val_str(String *str) col.length= res->length(); /* We do not change the string, so could do this trick */ col.str= (char *)res->ptr(); - if ((rc= dynamic_column_list(&col, &arr))) + if ((rc= mariadb_dyncol_list_named(&col, &count, &names))) { + bzero(&col, sizeof(col)); dynamic_column_error_message(rc); - delete_dynamic(&arr); goto null; } + bzero(&col, sizeof(col)); /* - We support elements from 0 - 65536, so max size for one element is - 6 (including ,). + We estimate average name length as 10 */ - if (str->alloc(arr.elements * 6)) + if (str->alloc(count * 13)) goto null; str->length(0); - for (i= 0; i < arr.elements; i++) + str->set_charset(&my_charset_utf8_general_ci); + for (i= 0; i < count; i++) { - str->qs_append(*dynamic_element(&arr, i, uint*)); - if (i < arr.elements - 1) + append_identifier(current_thd, str, names[i].str, names[i].length); + if (i < count - 1) str->qs_append(','); } - null_value= FALSE; - delete_dynamic(&arr); + if (names) + my_free(names); return str; null: null_value= TRUE; + if (names) + my_free(names); return NULL; } + diff --git a/sql/item_strfunc.h b/sql/item_strfunc.h index 9ed2627a518..a42240f1b35 100644 --- a/sql/item_strfunc.h +++ b/sql/item_strfunc.h @@ -3,6 +3,7 @@ /* Copyright (c) 2000, 2011, Oracle and/or its affiliates. + Copyright (c) 2009, 2013, Monty Program Ab. 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 @@ -354,7 +355,7 @@ public: String *val_str(String *); void fix_length_and_dec() { - maybe_null=1; + set_persist_maybe_null(1); /* 9 = MAX ((8- (arg_len % 8)) + 1) */ max_length = args[0]->max_length + 9; } @@ -370,7 +371,7 @@ public: String *val_str(String *); void fix_length_and_dec() { - maybe_null=1; + set_persist_maybe_null(1); /* 9 = MAX ((8- (arg_len % 8)) + 1) */ max_length= args[0]->max_length; if (max_length >= 9U) @@ -398,7 +399,7 @@ public: constructor_helper(); } String *val_str(String *); - void fix_length_and_dec() { maybe_null=1; max_length = 13; } + void fix_length_and_dec() { set_persist_maybe_null(1); max_length = 13; } const char *func_name() const { return "encrypt"; } bool check_vcol_func_processor(uchar *int_arg) { @@ -468,7 +469,7 @@ public: void fix_length_and_dec() { max_length= MAX_FIELD_NAME * system_charset_info->mbmaxlen; - maybe_null=1; + set_persist_maybe_null(1); } const char *func_name() const { return "database"; } const char *fully_qualified_func_name() const { return "database()"; } @@ -649,7 +650,7 @@ public: { collation.set(default_charset()); max_length=64; - maybe_null= 1; + set_persist_maybe_null(1); } }; @@ -676,7 +677,7 @@ public: Item_func_unhex(Item *a) :Item_str_func(a) { /* there can be bad hex strings */ - maybe_null= 1; + set_persist_maybe_null(1); } const char *func_name() const { return "unhex"; } String *val_str(String *); @@ -762,7 +763,7 @@ public: void fix_length_and_dec() { collation.set(&my_charset_bin, DERIVATION_COERCIBLE); - maybe_null=1; + set_persist_maybe_null(1); max_length=MAX_BLOB_WIDTH; } bool check_vcol_func_processor(uchar *int_arg) @@ -795,7 +796,7 @@ public: { decimals= 0; fix_length_and_charset(3 * 8 + 7, default_charset()); - maybe_null= 1; + set_persist_maybe_null(1); } }; @@ -910,7 +911,7 @@ public: { collation.set(system_charset_info); max_length= 64 * collation.collation->mbmaxlen; // should be enough - maybe_null= 0; + set_persist_maybe_null(0); }; table_map not_null_tables() const { return 0; } }; @@ -925,7 +926,7 @@ public: { collation.set(system_charset_info); max_length= 64 * collation.collation->mbmaxlen; // should be enough - maybe_null= 0; + set_persist_maybe_null(0); }; table_map not_null_tables() const { return 0; } }; @@ -971,7 +972,8 @@ class Item_func_uncompress: public Item_str_func String buffer; public: Item_func_uncompress(Item *a): Item_str_func(a){} - void fix_length_and_dec(){ maybe_null= 1; max_length= MAX_BLOB_WIDTH; } + void fix_length_and_dec() + { set_persist_maybe_null(1); max_length= MAX_BLOB_WIDTH; } const char *func_name() const{return "uncompress";} String *val_str(String *) ZLIB_DEPENDED_FUNCTION }; @@ -1001,9 +1003,10 @@ class Item_func_dyncol_create: public Item_str_func protected: DYNCALL_CREATE_DEF *defs; DYNAMIC_COLUMN_VALUE *vals; - uint *nums; - void prepare_arguments(); - void cleanup_arguments(); + uint *keys_num; + LEX_STRING *keys_str; + bool names, force_names; + bool prepare_arguments(bool force_names); void print_arguments(String *str, enum_query_type query_type); public: Item_func_dyncol_create(List<Item> &args, DYNCALL_CREATE_DEF *dfs); @@ -1012,6 +1015,7 @@ public: const char *func_name() const{ return "column_create"; } String *val_str(String *); virtual void print(String *str, enum_query_type query_type); + virtual enum Functype functype() const { return DYNCOL_FUNC; } }; @@ -1026,6 +1030,19 @@ public: virtual void print(String *str, enum_query_type query_type); }; +class Item_func_dyncol_json: public Item_str_func +{ +public: + Item_func_dyncol_json(Item *str) :Item_str_func(str) {} + const char *func_name() const{ return "column_json"; } + String *val_str(String *); + void fix_length_and_dec() + { + maybe_null= TRUE; + collation.set(&my_charset_bin); + decimals= 0; + } +}; /* The following functions is always called from an Item_cast function @@ -1036,11 +1053,9 @@ class Item_dyncol_get: public Item_str_func public: Item_dyncol_get(Item *str, Item *num) :Item_str_func(str, num) - { - max_length= MAX_DYNAMIC_COLUMN_LENGTH; - } + {} void fix_length_and_dec() - { maybe_null= 1; } + { set_persist_maybe_null(1); max_length= MAX_BLOB_WIDTH; } /* Mark that collation can change between calls */ bool dynamic_result() { return 1; } @@ -1059,7 +1074,8 @@ class Item_func_dyncol_list: public Item_str_func { public: Item_func_dyncol_list(Item *str) :Item_str_func(str) {}; - void fix_length_and_dec() { maybe_null= 1; max_length= MAX_BLOB_WIDTH; }; + void fix_length_and_dec() + { set_persist_maybe_null(1); max_length= MAX_BLOB_WIDTH; }; const char *func_name() const{ return "column_list"; } String *val_str(String *); }; @@ -1067,3 +1083,4 @@ public: extern String my_empty_string; #endif /* ITEM_STRFUNC_INCLUDED */ + diff --git a/sql/item_subselect.cc b/sql/item_subselect.cc index 1ec1e57e812..f2485fd50a1 100644 --- a/sql/item_subselect.cc +++ b/sql/item_subselect.cc @@ -3258,11 +3258,14 @@ int subselect_uniquesubquery_engine::scan_table() TABLE *table= tab->table; DBUG_ENTER("subselect_uniquesubquery_engine::scan_table"); - if (table->file->inited) - table->file->ha_index_end(); - - if (table->file->ha_rnd_init_with_error(1)) - DBUG_RETURN(1); + if ((table->file->inited && + (error= table->file->ha_index_end())) || + (error= table->file->ha_rnd_init(1))) + { + (void) report_error(table, error); + DBUG_RETURN(true); + } + table->file->extra_opt(HA_EXTRA_CACHE, current_thd->variables.read_buff_size); table->null_row= 0; @@ -3398,8 +3401,13 @@ int subselect_uniquesubquery_engine::exec() DBUG_RETURN(0); } - if (!table->file->inited) - table->file->ha_index_init(tab->ref.key, 0); + if (!table->file->inited && + (error= table->file->ha_index_init(tab->ref.key, 0))) + { + (void) report_error(table, error); + DBUG_RETURN(true); + } + error= table->file->ha_index_read_map(table->record[0], tab->ref.key_buff, make_prev_keypart_map(tab-> @@ -3563,8 +3571,13 @@ int subselect_indexsubquery_engine::exec() DBUG_RETURN(0); } - if (!table->file->inited) - table->file->ha_index_init(tab->ref.key, 1); + if (!table->file->inited && + (error= table->file->ha_index_init(tab->ref.key, 1))) + { + (void) report_error(table, error); + DBUG_RETURN(true); + } + error= table->file->ha_index_read_map(table->record[0], tab->ref.key_buff, make_prev_keypart_map(tab-> @@ -5053,7 +5066,7 @@ bool Ordered_key::alloc_keys_buffers() DBUG_ASSERT(key_buff_elements > 0); if (!(key_buff= (rownum_t*) my_malloc((size_t)(key_buff_elements * - sizeof(rownum_t)), MYF(MY_WME)))) + sizeof(rownum_t)), MYF(MY_WME | MY_THREAD_SPECIFIC)))) return TRUE; /* @@ -5480,7 +5493,7 @@ subselect_rowid_merge_engine::init(MY_BITMAP *non_null_key_parts, !(null_bitmaps= (MY_BITMAP**) thd->alloc(merge_keys_count * sizeof(MY_BITMAP*))) || !(row_num_to_rowid= (uchar*) my_malloc((size_t)(row_count * rowid_length), - MYF(MY_WME)))) + MYF(MY_WME | MY_THREAD_SPECIFIC)))) return TRUE; /* Create the only non-NULL key if there is any. */ diff --git a/sql/item_sum.cc b/sql/item_sum.cc index 217e65e401f..8816e1352a9 100644 --- a/sql/item_sum.cc +++ b/sql/item_sum.cc @@ -1,5 +1,5 @@ /* Copyright (c) 2000, 2011, Oracle and/or its affiliates. - Copyright (c) 2008-2011 Monty Program Ab + Copyright (c) 2008, 2013 Monty Program Ab 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 @@ -1097,7 +1097,7 @@ void Aggregator_distinct::endup() { /* go over the tree of distinct keys and calculate the aggregate value */ use_distinct_values= TRUE; - tree->walk(item_sum_distinct_walk, (void*) this); + tree->walk(table, item_sum_distinct_walk, (void*) this); use_distinct_values= FALSE; } /* prevent consecutive recalculations */ @@ -2916,13 +2916,12 @@ int group_concat_key_cmp_with_distinct(void* arg, const void* key1, const void* key2) { Item_func_group_concat *item_func= (Item_func_group_concat*)arg; - TABLE *table= item_func->table; for (uint i= 0; i < item_func->arg_count_field; i++) { Item *item= item_func->args[i]; /* - If field_item is a const item then either get_tp_table_field returns 0 + If field_item is a const item then either get_tmp_table_field returns 0 or it is an item over a const table. */ if (item->const_item()) @@ -2934,7 +2933,8 @@ int group_concat_key_cmp_with_distinct(void* arg, const void* key1, */ Field *field= item->get_tmp_table_field(); int res; - uint offset= field->offset(field->table->record[0])-table->s->null_bytes; + uint offset= (field->offset(field->table->record[0]) - + field->table->s->null_bytes); if((res= field->cmp((uchar*)key1 + offset, (uchar*)key2 + offset))) return res; } @@ -2952,28 +2952,37 @@ int group_concat_key_cmp_with_order(void* arg, const void* key1, { Item_func_group_concat* grp_item= (Item_func_group_concat*) arg; ORDER **order_item, **end; - TABLE *table= grp_item->table; for (order_item= grp_item->order, end=order_item+ grp_item->arg_count_order; order_item < end; order_item++) { Item *item= *(*order_item)->item; + /* + If field_item is a const item then either get_tmp_table_field returns 0 + or it is an item over a const table. + */ + if (item->const_item()) + continue; /* We have to use get_tmp_table_field() instead of real_item()->get_tmp_table_field() because we want the field in the temporary table, not the original field + + Note that for the case of ROLLUP, field may point to another table + tham grp_item->table. This is howver ok as the table definitions are + the same. */ Field *field= item->get_tmp_table_field(); /* - If item is a const item then either get_tp_table_field returns 0 + If item is a const item then either get_tmp_table_field returns 0 or it is an item over a const table. */ - if (field && !item->const_item()) + if (field) { int res; uint offset= (field->offset(field->table->record[0]) - - table->s->null_bytes); + field->table->s->null_bytes); if ((res= field->cmp((uchar*)key1 + offset, (uchar*)key2 + offset))) return (*order_item)->asc ? res : -res; } @@ -2997,6 +3006,7 @@ int dump_leaf_key(void* key_arg, element_count count __attribute__((unused)), { Item_func_group_concat *item= (Item_func_group_concat *) item_arg; TABLE *table= item->table; + uint max_length= table->in_use->variables.group_concat_max_len; String tmp((char *)table->record[1], table->s->reclength, default_charset_info); String tmp2; @@ -3039,7 +3049,7 @@ int dump_leaf_key(void* key_arg, element_count count __attribute__((unused)), item->row_count++; /* stop if length of result more than max_length */ - if (result->length() > item->max_length) + if (result->length() > max_length) { int well_formed_error; CHARSET_INFO *cs= item->collation.collation; @@ -3052,7 +3062,7 @@ int dump_leaf_key(void* key_arg, element_count count __attribute__((unused)), */ add_length= cs->cset->well_formed_len(cs, ptr + old_length, - ptr + item->max_length, + ptr + max_length, result->length(), &well_formed_error); result->length(old_length + add_length); @@ -3166,12 +3176,13 @@ Item_func_group_concat::Item_func_group_concat(THD *thd, */ ORDER *tmp; if (!(tmp= (ORDER *) thd->alloc(sizeof(ORDER *) * arg_count_order + - sizeof(ORDER) * arg_count_order))) + sizeof(ORDER) * arg_count_order))) return; order= (ORDER **)(tmp + arg_count_order); for (uint i= 0; i < arg_count_order; i++, tmp++) { memcpy(tmp, item->order[i], sizeof(ORDER)); + tmp->next= i == arg_count_order-1 ? 0 : tmp+1; order[i]= tmp; } } @@ -3216,19 +3227,11 @@ Field *Item_func_group_concat::make_string_field(TABLE *table) { Field *field; DBUG_ASSERT(collation.collation); - /* - max_characters is maximum number of characters - what can fit into max_length size. It's necessary - to use field size what allows to store group_concat - result without truncation. For this purpose we use - max_characters * CS->mbmaxlen. - */ - const uint32 max_characters= max_length / collation.collation->mbminlen; - if (max_characters > CONVERT_IF_BIGGER_TO_BLOB) - field= new Field_blob(max_characters * collation.collation->mbmaxlen, + if (too_big_for_varchar()) + field= new Field_blob(max_length, maybe_null, name, collation.collation, TRUE); else - field= new Field_varstring(max_characters * collation.collation->mbmaxlen, + field= new Field_varstring(max_length, maybe_null, name, table->s, collation.collation); if (field) @@ -3343,7 +3346,9 @@ Item_func_group_concat::fix_fields(THD *thd, Item **ref) result.set_charset(collation.collation); result_field= 0; null_value= 1; - max_length= thd->variables.group_concat_max_len; + max_length= thd->variables.group_concat_max_len + / collation.collation->mbminlen + * collation.collation->mbmaxlen; uint32 offset; if (separator->needs_conversion(separator->length(), separator->charset(), @@ -3461,7 +3466,8 @@ bool Item_func_group_concat::setup(THD *thd) */ if (!(table= create_tmp_table(thd, tmp_table_param, all_fields, (ORDER*) 0, 0, TRUE, - (select_lex->options | thd->variables.option_bits), + (select_lex->options | + thd->variables.option_bits), HA_POS_ERROR, (char*) ""))) DBUG_RETURN(TRUE); table->file->extra(HA_EXTRA_NO_ROWS); @@ -3485,7 +3491,8 @@ bool Item_func_group_concat::setup(THD *thd) init_tree(tree, (uint) min(thd->variables.max_heap_table_size, thd->variables.sortbuff_size/16), 0, tree_key_length, - group_concat_key_cmp_with_order , 0, NULL, (void*) this); + group_concat_key_cmp_with_order, NULL, (void*) this, + MYF(MY_THREAD_SPECIFIC)); } if (distinct) diff --git a/sql/item_sum.h b/sql/item_sum.h index c8dce60c7d4..40a28d8beae 100644 --- a/sql/item_sum.h +++ b/sql/item_sum.h @@ -1439,7 +1439,7 @@ public: virtual Field *make_string_field(TABLE *table); enum_field_types field_type() const { - if (max_length/collation.collation->mbmaxlen > CONVERT_IF_BIGGER_TO_BLOB ) + if (too_big_for_varchar()) return MYSQL_TYPE_BLOB; else return MYSQL_TYPE_VARCHAR; diff --git a/sql/item_timefunc.cc b/sql/item_timefunc.cc index 117276e488b..02a7b8511af 100644 --- a/sql/item_timefunc.cc +++ b/sql/item_timefunc.cc @@ -1,6 +1,6 @@ /* Copyright (c) 2000, 2012, Oracle and/or its affiliates. - Copyright (c) 2009, 2011, Monty Program Ab + Copyright (c) 2009, 2013, Monty Program Ab 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 @@ -945,7 +945,7 @@ void Item_func_monthname::fix_length_and_dec() collation.set(cs, DERIVATION_COERCIBLE, repertoire); decimals=0; max_length= locale->max_month_name_length * collation.collation->mbmaxlen; - maybe_null=1; + set_persist_maybe_null(1); } @@ -1095,7 +1095,7 @@ void Item_func_dayname::fix_length_and_dec() collation.set(cs, DERIVATION_COERCIBLE, repertoire); decimals=0; max_length= locale->max_day_name_length * collation.collation->mbmaxlen; - maybe_null=1; + set_persist_maybe_null(1); } @@ -1446,7 +1446,7 @@ void Item_temporal_func::fix_length_and_dec() { MAX_DATETIME_WIDTH, MAX_DATETIME_WIDTH, MAX_DATE_WIDTH, MAX_DATETIME_WIDTH, MIN_TIME_WIDTH }; - maybe_null= true; + set_persist_maybe_null(1); max_length= max_time_type_width[mysql_type_to_time_type(field_type())+2]; if (decimals) { @@ -1501,13 +1501,10 @@ bool Item_func_from_days::get_date(MYSQL_TIME *ltime, ulonglong fuzzy_date) bzero(ltime, sizeof(MYSQL_TIME)); if (get_date_from_daynr((long) value, <ime->year, <ime->month, <ime->day)) - return (null_value= 1); - - if ((fuzzy_date & TIME_NO_ZERO_DATE) && ltime->year == 0) - return (null_value= 1); + return 0; ltime->time_type= MYSQL_TIMESTAMP_DATE; - return (null_value= 0); + return 0; } @@ -1519,7 +1516,7 @@ void Item_func_curdate::fix_length_and_dec() ltime.hour= ltime.minute= ltime.second= 0; ltime.time_type= MYSQL_TIMESTAMP_DATE; Item_datefunc::fix_length_and_dec(); - maybe_null= false; + set_persist_maybe_null(0); } /** @@ -1758,7 +1755,7 @@ void Item_func_date_format::fix_length_and_dec() collation.collation->mbmaxlen; set_if_smaller(max_length,MAX_BLOB_WIDTH); } - maybe_null=1; // If wrong date + set_persist_maybe_null(1); // If wrong date } @@ -2102,7 +2099,7 @@ void Item_extract::print(String *str, enum_query_type query_type) void Item_extract::fix_length_and_dec() { - maybe_null=1; // If wrong date + set_persist_maybe_null(1); // If wrong date switch (int_type) { case INTERVAL_YEAR: max_length=4; date_value=1; break; case INTERVAL_YEAR_MONTH: max_length=6; date_value=1; break; diff --git a/sql/item_timefunc.h b/sql/item_timefunc.h index 3af08a8168e..3e3cd698efc 100644 --- a/sql/item_timefunc.h +++ b/sql/item_timefunc.h @@ -82,7 +82,7 @@ public: { decimals=0; max_length=6*MY_CHARSET_BIN_MB_MAXLEN; - maybe_null=1; + set_persist_maybe_null(1); } enum_monotonicity_info get_monotonicity_info() const; longlong val_int_endpoint(bool left_endp, bool *incl_endp); @@ -105,7 +105,7 @@ public: { decimals=0; max_length=6*MY_CHARSET_BIN_MB_MAXLEN; - maybe_null=1; + set_persist_maybe_null(1); } enum_monotonicity_info get_monotonicity_info() const; longlong val_int_endpoint(bool left_endp, bool *incl_endp); @@ -138,7 +138,7 @@ public: { decimals=0; max_length=2*MY_CHARSET_BIN_MB_MAXLEN; - maybe_null=1; + set_persist_maybe_null(1); } bool check_partition_func_processor(uchar *int_arg) {return FALSE;} bool check_vcol_func_processor(uchar *int_arg) { return FALSE;} @@ -170,7 +170,7 @@ public: { decimals= 0; fix_char_length(2); - maybe_null= 1; + set_persist_maybe_null(1); } bool check_partition_func_processor(uchar *int_arg) {return FALSE;} bool check_vcol_func_processor(uchar *int_arg) { return FALSE;} @@ -208,7 +208,7 @@ public: { decimals= 0; fix_char_length(3); - maybe_null= 1; + set_persist_maybe_null(1); } bool check_partition_func_processor(uchar *int_arg) {return FALSE;} bool check_vcol_func_processor(uchar *int_arg) { return FALSE;} @@ -229,7 +229,7 @@ public: { decimals=0; max_length=2*MY_CHARSET_BIN_MB_MAXLEN; - maybe_null=1; + set_persist_maybe_null(1); } bool check_partition_func_processor(uchar *int_arg) {return FALSE;} bool check_vcol_func_processor(uchar *int_arg) { return FALSE;} @@ -250,7 +250,7 @@ public: { decimals=0; max_length=2*MY_CHARSET_BIN_MB_MAXLEN; - maybe_null=1; + set_persist_maybe_null(1); } bool check_partition_func_processor(uchar *int_arg) {return FALSE;} bool check_vcol_func_processor(uchar *int_arg) { return FALSE;} @@ -271,7 +271,7 @@ public: { decimals=0; max_length=1*MY_CHARSET_BIN_MB_MAXLEN; - maybe_null=1; + set_persist_maybe_null(1); } bool check_partition_func_processor(uchar *int_arg) {return FALSE;} bool check_vcol_func_processor(uchar *int_arg) { return FALSE;} @@ -292,7 +292,7 @@ public: { decimals=0; max_length=2*MY_CHARSET_BIN_MB_MAXLEN; - maybe_null=1; + set_persist_maybe_null(1); } bool check_partition_func_processor(uchar *int_arg) {return FALSE;} bool check_vcol_func_processor(uchar *int_arg) { return FALSE;} @@ -313,7 +313,7 @@ public: { decimals=0; max_length=2*MY_CHARSET_BIN_MB_MAXLEN; - maybe_null=1; + set_persist_maybe_null(1); } }; @@ -327,7 +327,7 @@ public: { decimals=0; max_length=6*MY_CHARSET_BIN_MB_MAXLEN; - maybe_null=1; + set_persist_maybe_null(1); } bool check_partition_func_processor(uchar *int_arg) {return FALSE;} bool check_vcol_func_processor(uchar *int_arg) { return FALSE;} @@ -350,7 +350,7 @@ public: { decimals=0; max_length=4*MY_CHARSET_BIN_MB_MAXLEN; - maybe_null=1; + set_persist_maybe_null(1); } bool check_partition_func_processor(uchar *int_arg) {return FALSE;} bool check_vcol_func_processor(uchar *int_arg) { return FALSE;} @@ -384,7 +384,7 @@ public: { decimals= 0; fix_char_length(1); - maybe_null= 1; + set_persist_maybe_null(1); } bool check_partition_func_processor(uchar *int_arg) {return FALSE;} bool check_vcol_func_processor(uchar *int_arg) { return FALSE;} @@ -419,7 +419,7 @@ public: decimals= args[0]->decimals; set_if_smaller(decimals, TIME_SECOND_PART_DIGITS); max_length=17 + (decimals ? decimals + 1 : 0); - maybe_null= 1; + set_persist_maybe_null(1); } void find_num_type() { hybrid_type= decimals ? DECIMAL_RESULT : INT_RESULT; } double real_op() { DBUG_ASSERT(0); return 0; } @@ -466,7 +466,7 @@ public: const char *func_name() const { return "time_to_sec"; } void fix_num_length_and_dec() { - maybe_null= true; + set_persist_maybe_null(1); Item_func_seconds_hybrid::fix_num_length_and_dec(); } bool check_partition_func_processor(uchar *int_arg) {return FALSE;} @@ -537,7 +537,7 @@ public: { store_now_in_TIME(<ime); Item_timefunc::fix_length_and_dec(); - maybe_null= false; + set_persist_maybe_null(0); } bool get_date(MYSQL_TIME *res, ulonglong fuzzy_date); /* @@ -619,7 +619,7 @@ public: { store_now_in_TIME(<ime); Item_temporal_func::fix_length_and_dec(); - maybe_null= false; + set_persist_maybe_null(0); } bool get_date(MYSQL_TIME *res, ulonglong fuzzy_date); virtual void store_now_in_TIME(MYSQL_TIME *now_time)=0; @@ -664,7 +664,7 @@ public: void update_used_tables() { Item_func_now::update_used_tables(); - maybe_null= false; + set_persist_maybe_null(0); used_tables_cache|= RAND_TABLE_BIT; } }; @@ -959,7 +959,7 @@ public: void fix_length_and_dec() { decimals=0; - maybe_null=1; + set_persist_maybe_null(1); } bool check_partition_func_processor(uchar *int_arg) {return FALSE;} bool check_vcol_func_processor(uchar *int_arg) { return FALSE;} @@ -981,7 +981,7 @@ public: void fix_length_and_dec() { decimals=0; - maybe_null=1; + set_persist_maybe_null(1); } virtual void print(String *str, enum_query_type query_type); }; @@ -1003,7 +1003,7 @@ public: const char *func_name() const { return "get_format"; } void fix_length_and_dec() { - maybe_null= 1; + set_persist_maybe_null(1); decimals=0; fix_length_and_charset(17, default_charset()); } diff --git a/sql/item_xmlfunc.cc b/sql/item_xmlfunc.cc index ae0a74c5ba6..5a824e48b7b 100644 --- a/sql/item_xmlfunc.cc +++ b/sql/item_xmlfunc.cc @@ -2604,6 +2604,7 @@ void Item_xml_str_func::fix_length_and_dec() status_var_increment(current_thd->status_var.feature_xml); nodeset_func= 0; + set_persist_maybe_null(1); if (agg_arg_charsets_for_comparison(collation, args, arg_count)) return; diff --git a/sql/item_xmlfunc.h b/sql/item_xmlfunc.h index 800cf6ed760..3356b4ac902 100644 --- a/sql/item_xmlfunc.h +++ b/sql/item_xmlfunc.h @@ -34,14 +34,10 @@ protected: public: Item_xml_str_func(Item *a, Item *b): Item_str_func(a,b) - { - maybe_null= TRUE; - } + {} Item_xml_str_func(Item *a, Item *b, Item *c): Item_str_func(a,b,c) - { - maybe_null= TRUE; - } + {} void fix_length_and_dec(); String *parse_xml(String *raw_xml, String *parsed_xml_buf); bool check_vcol_func_processor(uchar *int_arg) diff --git a/sql/lex.h b/sql/lex.h index 0af44234fca..101880597d5 100644 --- a/sql/lex.h +++ b/sql/lex.h @@ -123,11 +123,10 @@ static SYMBOL symbols[] = { { "COLUMN_NAME", SYM(COLUMN_NAME_SYM)}, { "COLUMNS", SYM(COLUMNS)}, { "COLUMN_ADD", SYM(COLUMN_ADD_SYM)}, + { "COLUMN_CHECK", SYM(COLUMN_CHECK_SYM)}, { "COLUMN_CREATE", SYM(COLUMN_CREATE_SYM)}, { "COLUMN_DELETE", SYM(COLUMN_DELETE_SYM)}, - { "COLUMN_EXISTS", SYM(COLUMN_EXISTS_SYM)}, { "COLUMN_GET", SYM(COLUMN_GET_SYM)}, - { "COLUMN_LIST", SYM(COLUMN_LIST_SYM)}, { "COMMENT", SYM(COMMENT_SYM)}, { "COMMIT", SYM(COMMIT_SYM)}, { "COMMITTED", SYM(COMMITTED_SYM)}, diff --git a/sql/log.cc b/sql/log.cc index 1813120dd81..254449da05a 100644 --- a/sql/log.cc +++ b/sql/log.cc @@ -1,5 +1,5 @@ /* Copyright (c) 2000, 2011, Oracle and/or its affiliates. - Copyright (c) 2010-2011 Monty Program Ab + Copyright (c) 2009, 2013, Monty Program Ab 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 @@ -1552,10 +1552,7 @@ binlog_trans_log_savepos(THD *thd, my_off_t *pos) { DBUG_ENTER("binlog_trans_log_savepos"); DBUG_ASSERT(pos != NULL); - if (thd_get_ha_data(thd, binlog_hton) == NULL) - thd->binlog_setup_trx_data(); - binlog_cache_mngr *const cache_mngr= - (binlog_cache_mngr*) thd_get_ha_data(thd, binlog_hton); + binlog_cache_mngr *const cache_mngr= thd->binlog_setup_trx_data(); DBUG_ASSERT(mysql_bin_log.is_open()); *pos= cache_mngr->trx_cache.get_byte_position(); DBUG_PRINT("return", ("*pos: %lu", (ulong) *pos)); @@ -2103,8 +2100,9 @@ static int binlog_savepoint_set(handlerton *hton, THD *thd, void *sv) DBUG_ENTER("binlog_savepoint_set"); int error= 1; - String log_query; - if (log_query.append(STRING_WITH_LEN("SAVEPOINT ")) || + char buf[1024]; + String log_query(buf, sizeof(buf), &my_charset_bin); + if (log_query.copy(STRING_WITH_LEN("SAVEPOINT "), &my_charset_bin) || append_identifier(thd, &log_query, thd->lex->ident.str, thd->lex->ident.length)) DBUG_RETURN(1); @@ -2142,8 +2140,9 @@ static int binlog_savepoint_rollback(handlerton *hton, THD *thd, void *sv) if (unlikely(trans_has_updated_non_trans_table(thd) || (thd->variables.option_bits & OPTION_KEEP_LOG))) { - String log_query; - if (log_query.append(STRING_WITH_LEN("ROLLBACK TO ")) || + char buf[1024]; + String log_query(buf, sizeof(buf), &my_charset_bin); + if (log_query.copy(STRING_WITH_LEN("ROLLBACK TO "), &my_charset_bin) || append_identifier(thd, &log_query, thd->lex->ident.str, thd->lex->ident.length)) DBUG_RETURN(1); @@ -4967,14 +4966,14 @@ bool stmt_has_updated_non_trans_table(const THD* thd) binlog_hton, which has internal linkage. */ -int THD::binlog_setup_trx_data() +binlog_cache_mngr *THD::binlog_setup_trx_data() { DBUG_ENTER("THD::binlog_setup_trx_data"); binlog_cache_mngr *cache_mngr= (binlog_cache_mngr*) thd_get_ha_data(this, binlog_hton); if (cache_mngr) - DBUG_RETURN(0); // Already set up + DBUG_RETURN(cache_mngr); // Already set up cache_mngr= (binlog_cache_mngr*) my_malloc(sizeof(binlog_cache_mngr), MYF(MY_ZEROFILL)); if (!cache_mngr || @@ -4984,18 +4983,18 @@ int THD::binlog_setup_trx_data() LOG_PREFIX, binlog_cache_size, MYF(MY_WME))) { my_free(cache_mngr); - DBUG_RETURN(1); // Didn't manage to set it up + DBUG_RETURN(0); // Didn't manage to set it up } thd_set_ha_data(this, binlog_hton, cache_mngr); - cache_mngr= new (thd_get_ha_data(this, binlog_hton)) + cache_mngr= new (cache_mngr) binlog_cache_mngr(max_binlog_stmt_cache_size, max_binlog_cache_size, &binlog_stmt_cache_use, &binlog_stmt_cache_disk_use, &binlog_cache_use, &binlog_cache_disk_use); - DBUG_RETURN(0); + DBUG_RETURN(cache_mngr); } /* @@ -5078,9 +5077,7 @@ binlog_start_consistent_snapshot(handlerton *hton, THD *thd) int err= 0; DBUG_ENTER("binlog_start_consistent_snapshot"); - thd->binlog_setup_trx_data(); - binlog_cache_mngr *const cache_mngr= - (binlog_cache_mngr*) thd_get_ha_data(thd, binlog_hton); + binlog_cache_mngr *const cache_mngr= thd->binlog_setup_trx_data(); /* Server layer calls us with LOCK_commit_ordered locked, so this is safe. */ strmake(cache_mngr->last_commit_pos_file, mysql_bin_log.last_commit_pos_file, @@ -5192,11 +5189,7 @@ THD::binlog_get_pending_rows_event(bool is_transactional) const void THD::binlog_set_pending_rows_event(Rows_log_event* ev, bool is_transactional) { - if (thd_get_ha_data(this, binlog_hton) == NULL) - binlog_setup_trx_data(); - - binlog_cache_mngr *const cache_mngr= - (binlog_cache_mngr*) thd_get_ha_data(this, binlog_hton); + binlog_cache_mngr *const cache_mngr= binlog_setup_trx_data(); DBUG_ASSERT(cache_mngr); @@ -5275,12 +5268,18 @@ MYSQL_BIN_LOG::flush_and_set_pending_rows_event(THD *thd, /* Write pending event to the cache. */ + DBUG_EXECUTE_IF("simulate_disk_full_at_flush_pending", + {DBUG_SET("+d,simulate_file_write_error");}); if (pending->write(file)) { set_write_error(thd, is_transactional); if (check_write_error(thd) && cache_data && stmt_has_updated_non_trans_table(thd)) cache_data->set_incident(); + delete pending; + cache_data->set_pending(NULL); + DBUG_EXECUTE_IF("simulate_disk_full_at_flush_pending", + {DBUG_SET("-d,simulate_file_write_error");}); DBUG_RETURN(1); } @@ -5364,12 +5363,10 @@ bool MYSQL_BIN_LOG::write(Log_event *event_info, my_bool *with_annotate) } else { - if (thd->binlog_setup_trx_data()) + binlog_cache_mngr *const cache_mngr= thd->binlog_setup_trx_data(); + if (!cache_mngr) goto err; - binlog_cache_mngr *const cache_mngr= - (binlog_cache_mngr*) thd_get_ha_data(thd, binlog_hton); - is_trans_cache= use_trans_cache(thd, using_trans); file= cache_mngr->get_binlog_cache_log(is_trans_cache); cache_data= cache_mngr->get_binlog_cache_data(is_trans_cache); @@ -6393,8 +6390,6 @@ MYSQL_BIN_LOG::trx_group_commit_leader(group_commit_entry *leader) DBUG_ENTER("MYSQL_BIN_LOG::trx_group_commit_leader"); LINT_INIT(binlog_id); - DBUG_ASSERT(is_open()); - if (likely(is_open())) // Should always be true { /* Lock the LOCK_log(), and once we get it, collect any additional writes @@ -6421,7 +6416,11 @@ MYSQL_BIN_LOG::trx_group_commit_leader(group_commit_entry *leader) DBUG_ASSERT(leader == queue /* the leader should be first in queue */); /* Now we have in queue the list of transactions to be committed in order. */ + } + DBUG_ASSERT(is_open()); + if (likely(is_open())) // Should always be true + { /* Commit every transaction in the queue. @@ -7998,8 +7997,9 @@ TC_LOG_BINLOG::log_and_order(THD *thd, my_xid xid, bool all, int err; DBUG_ENTER("TC_LOG_BINLOG::log_and_order"); - binlog_cache_mngr *cache_mngr= - (binlog_cache_mngr*) thd_get_ha_data(thd, binlog_hton); + binlog_cache_mngr *cache_mngr= thd->binlog_setup_trx_data(); + if (!cache_mngr) + DBUG_RETURN(0); cache_mngr->using_xa= TRUE; cache_mngr->xa_xid= xid; @@ -8205,8 +8205,9 @@ binlog_background_thread(void *arg __attribute__((unused))) bool stop; MYSQL_BIN_LOG::xid_count_per_binlog *queue, *next; THD *thd; - my_thread_init(); + DBUG_ENTER("binlog_background_thread"); + thd= new THD; thd->system_thread= SYSTEM_THREAD_BINLOG_BACKGROUND; thd->thread_stack= (char*) &thd; /* Set approximate stack start */ @@ -8270,7 +8271,7 @@ binlog_background_thread(void *arg __attribute__((unused))) mysql_cond_signal(&mysql_bin_log.COND_binlog_background_thread_end); mysql_mutex_unlock(&mysql_bin_log.LOCK_binlog_background_thread); - return 0; + DBUG_RETURN(0); } #ifdef HAVE_PSI_INTERFACE @@ -8321,7 +8322,7 @@ int TC_LOG_BINLOG::recover(LOG_INFO *linfo, const char *last_log_name, sizeof(my_xid), 0, 0, MYF(0))) goto err1; - init_alloc_root(&mem_root, TC_LOG_PAGE_SIZE, TC_LOG_PAGE_SIZE); + init_alloc_root(&mem_root, TC_LOG_PAGE_SIZE, TC_LOG_PAGE_SIZE, MYF(0)); fdle->flags&= ~LOG_EVENT_BINLOG_IN_USE_F; // abort on the first error @@ -8546,7 +8547,7 @@ static int show_binlog_vars(THD *thd, SHOW_VAR *var, char *buff) } static SHOW_VAR binlog_status_vars_top[]= { - {"binlog", (char *) &show_binlog_vars, SHOW_FUNC}, + {"Binlog", (char *) &show_binlog_vars, SHOW_FUNC}, {NullS, NullS, SHOW_LONG} }; diff --git a/sql/log_event.cc b/sql/log_event.cc index fb9e6b7107f..7de72338d97 100644 --- a/sql/log_event.cc +++ b/sql/log_event.cc @@ -1,6 +1,6 @@ /* Copyright (c) 2000, 2012, Oracle and/or its affiliates. - Copyright (c) 2010, 2012, Monty Program Ab + Copyright (c) 2009, 2013, Monty Program Ab. 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 @@ -46,7 +46,7 @@ #include "rpl_record.h" #include "transaction.h" #include <my_dir.h> -#include "sql_show.h" +#include "sql_show.h" // append_identifier #endif /* MYSQL_CLIENT */ @@ -92,6 +92,23 @@ TYPELIB binlog_checksum_typelib= */ #define FMT_G_BUFSIZE(PREC) (3 + (PREC) + 5 + 1) +/* + Explicit instantiation to unsigned int of template available_buffer + function. +*/ +template unsigned int available_buffer<unsigned int>(const char*, + const char*, + unsigned int); + +/* + Explicit instantiation to unsigned int of template valid_buffer_range + function. +*/ +template bool valid_buffer_range<unsigned int>(unsigned int, + const char*, + const char*, + unsigned int); + /* replication event checksum is introduced in the following "checksum-home" version. The checksum-aware servers extract FD's version to decide whether the FD event @@ -1576,7 +1593,7 @@ Log_event* Log_event::read_log_event(const char* buf, uint event_len, ev = new Rand_log_event(buf, description_event); break; case USER_VAR_EVENT: - ev = new User_var_log_event(buf, description_event); + ev = new User_var_log_event(buf, event_len, description_event); break; case FORMAT_DESCRIPTION_EVENT: ev = new Format_description_log_event(buf, event_len, description_event); @@ -2301,7 +2318,7 @@ void Rows_log_event::print_verbose(IO_CACHE *file, for (const uchar *value= m_rows_buf; value < m_rows_end; ) { size_t length; - my_b_printf(file, "### %s %s.%s\n", + my_b_printf(file, "### %s %`s.%`s\n", sql_command, map->get_db_name(), map->get_table_name()); /* Print the first image */ @@ -2467,7 +2484,7 @@ void Query_log_event::pack_info(THD *thd, Protocol *protocol) { buf.append(STRING_WITH_LEN("use ")); append_identifier(thd, &buf, db, db_len); - buf.append("; "); + buf.append(STRING_WITH_LEN("; ")); } if (query && q_len) buf.append(query, q_len); @@ -3446,17 +3463,11 @@ void Query_log_event::print_query_header(IO_CACHE* file, } else if (db) { - /* Room for expand ` to `` + initial/final ` + \0 */ - char buf[FN_REFLEN*2+3]; - different_db= memcmp(print_event_info->db, db, db_len + 1); if (different_db) memcpy(print_event_info->db, db, db_len + 1); if (db[0] && different_db) - { - my_snprintf(buf, sizeof(buf), "%`s", db); - my_b_printf(file, "use %s%s\n", buf, print_event_info->delimiter); - } + my_b_printf(file, "use %`s%s\n", db, print_event_info->delimiter); } end=int10_to_str((long) when, strmov(buff,"SET TIMESTAMP="),10); @@ -3997,7 +4008,7 @@ Default database: '%s'. Query: '%s'", { DBUG_PRINT("info",("error ignored")); clear_all_errors(thd, const_cast<Relay_log_info*>(rli)); - thd->killed= NOT_KILLED; + thd->reset_killed(); } /* Other cases: mostly we expected no error and get one. @@ -5290,7 +5301,7 @@ void Load_log_event::print(FILE* file_arg, PRINT_EVENT_INFO* print_event_info, } if (db && db[0] && different_db) - my_b_printf(&cache, "%suse %s%s\n", + my_b_printf(&cache, "%suse %`s%s\n", commented ? "# " : "", db, print_event_info->delimiter); @@ -5342,7 +5353,7 @@ void Load_log_event::print(FILE* file_arg, PRINT_EVENT_INFO* print_event_info, { if (i) my_b_printf(&cache, ","); - my_b_printf(&cache, "%s", field); + my_b_printf(&cache, "%`s", field); field += field_lens[i] + 1; } @@ -6482,19 +6493,35 @@ void User_var_log_event::pack_info(THD *thd, Protocol* protocol) User_var_log_event:: -User_var_log_event(const char* buf, +User_var_log_event(const char* buf, uint event_len, const Format_description_log_event* description_event) :Log_event(buf, description_event) #ifndef MYSQL_CLIENT , deferred(false) #endif { + bool error= false; + const char* buf_start= buf; /* The Post-Header is empty. The Variable Data part begins immediately. */ const char *start= buf; buf+= description_event->common_header_len + description_event->post_header_len[USER_VAR_EVENT-1]; name_len= uint4korr(buf); name= (char *) buf + UV_NAME_LEN_SIZE; + + /* + We don't know yet is_null value, so we must assume that name_len + may have the bigger value possible, is_null= True and there is no + payload for val. + */ + if (0 == name_len || + !valid_buffer_range<uint>(name_len, buf_start, name, + event_len - UV_VAL_IS_NULL)) + { + error= true; + goto err; + } + buf+= UV_NAME_LEN_SIZE + name_len; is_null= (bool) *buf; flags= User_var_log_event::UNDEF_F; // defaults to UNDEF_F @@ -6507,6 +6534,14 @@ User_var_log_event(const char* buf, } else { + if (!valid_buffer_range<uint>(UV_VAL_IS_NULL + UV_VAL_TYPE_SIZE + + UV_CHARSET_NUMBER_SIZE + UV_VAL_LEN_SIZE, + buf_start, buf, event_len)) + { + error= true; + goto err; + } + type= (Item_result) buf[UV_VAL_IS_NULL]; charset_number= uint4korr(buf + UV_VAL_IS_NULL + UV_VAL_TYPE_SIZE); val_len= uint4korr(buf + UV_VAL_IS_NULL + UV_VAL_TYPE_SIZE + @@ -6514,6 +6549,12 @@ User_var_log_event(const char* buf, val= (char *) (buf + UV_VAL_IS_NULL + UV_VAL_TYPE_SIZE + UV_CHARSET_NUMBER_SIZE + UV_VAL_LEN_SIZE); + if (!valid_buffer_range<uint>(val_len, buf_start, val, event_len)) + { + error= true; + goto err; + } + /** We need to check if this is from an old server that did not pack information for flags. @@ -6548,6 +6589,10 @@ User_var_log_event(const char* buf, val_len); } } + +err: + if (error) + name= 0; } @@ -6693,8 +6738,9 @@ void User_var_log_event::print(FILE* file, PRINT_EVENT_INFO* print_event_info) char *hex_str; CHARSET_INFO *cs; - if (!(hex_str= (char *)my_alloca(2*val_len+1+2))) // 2 hex digits / byte - break; // no error, as we are 'void' + hex_str= (char *)my_malloc(2*val_len+1+2,MYF(MY_WME)); // 2 hex digits / byte + if (!hex_str) + return; str_to_hex(hex_str, val, val_len); /* For proper behaviour when mysqlbinlog|mysql, we need to explicitely @@ -6712,7 +6758,7 @@ void User_var_log_event::print(FILE* file, PRINT_EVENT_INFO* print_event_info) my_b_printf(&cache, ":=_%s %s COLLATE `%s`%s\n", cs->csname, hex_str, cs->name, print_event_info->delimiter); - my_afree(hex_str); + my_free(hex_str); } break; case ROW_RESULT: @@ -7947,9 +7993,9 @@ void Execute_load_query_log_event::pack_info(THD *thd, Protocol *protocol) buf.real_alloc(9 + db_len + q_len + 10 + 21); if (db && db_len) { - if (buf.append("use ") || + if (buf.append(STRING_WITH_LEN("use ")) || append_identifier(thd, &buf, db, db_len) || - buf.append("; ")) + buf.append(STRING_WITH_LEN("; "))) return; } if (query && q_len && buf.append(query, q_len)) diff --git a/sql/log_event.h b/sql/log_event.h index dfbefdb359e..ff13cab9cd5 100644 --- a/sql/log_event.h +++ b/sql/log_event.h @@ -2740,7 +2740,7 @@ public: void print(FILE* file, PRINT_EVENT_INFO* print_event_info); #endif - User_var_log_event(const char* buf, + User_var_log_event(const char* buf, uint event_len, const Format_description_log_event *description_event); ~User_var_log_event() {} Log_event_type get_type_code() { return USER_VAR_EVENT;} @@ -2752,9 +2752,9 @@ public: and which case the applier adjusts execution path. */ bool is_deferred() { return deferred; } - void set_deferred() { deferred= val; } + void set_deferred() { deferred= true; } #endif - bool is_valid() const { return 1; } + bool is_valid() const { return name != 0; } private: #if defined(MYSQL_SERVER) && defined(HAVE_REPLICATION) diff --git a/sql/log_event_old.cc b/sql/log_event_old.cc index bd837adc9d9..e9afe474418 100644 --- a/sql/log_event_old.cc +++ b/sql/log_event_old.cc @@ -725,7 +725,10 @@ static int find_and_fetch_row(TABLE *table, uchar *key) int error; /* We have a key: search the table using the index */ if (!table->file->inited && (error= table->file->ha_index_init(0, FALSE))) + { + table->file->print_error(error, MYF(0)); DBUG_RETURN(error); + } /* Don't print debug messages when running valgrind since they can @@ -858,7 +861,7 @@ static int find_and_fetch_row(TABLE *table, uchar *key) default: table->file->print_error(error, MYF(0)); DBUG_PRINT("info", ("Record not found")); - table->file->ha_rnd_end(); + (void) table->file->ha_rnd_end(); DBUG_RETURN(error); } } @@ -2410,7 +2413,7 @@ int Old_rows_log_event::find_row(const Relay_log_info *rli) continue; DBUG_PRINT("info",("no record matching the given row found")); table->file->print_error(error, MYF(0)); - table->file->ha_index_end(); + (void) table->file->ha_index_end(); DBUG_RETURN(error); } } diff --git a/sql/mdl.cc b/sql/mdl.cc index ce37f10128b..a18df0fede1 100644 --- a/sql/mdl.cc +++ b/sql/mdl.cc @@ -1,4 +1,4 @@ -/* Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved. +/* Copyright (c) 2007, 2011, Oracle and/or its affiliates. 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 @@ -1202,7 +1202,7 @@ MDL_wait::timed_wait(THD *thd, struct timespec *abs_timeout, THD_ENTER_COND(thd, &m_COND_wait_status, &m_LOCK_wait_status, wait_state_name, & old_stage); thd_wait_begin(thd, THD_WAIT_META_DATA_LOCK); - while (!m_wait_status && !thd_killed(thd) && + while (!m_wait_status && !thd->killed && wait_result != ETIMEDOUT && wait_result != ETIME) { wait_result= mysql_cond_timedwait(&m_COND_wait_status, &m_LOCK_wait_status, @@ -1224,7 +1224,7 @@ MDL_wait::timed_wait(THD *thd, struct timespec *abs_timeout, false, which means that the caller intends to restart the wait. */ - if (thd_killed(thd)) + if (thd->killed) m_wait_status= KILLED; else if (set_status_on_timeout) m_wait_status= TIMEOUT; @@ -2095,7 +2095,11 @@ MDL_context::acquire_lock(MDL_request *mdl_request, ulong lock_wait_timeout) */ m_wait.reset_status(); - if (lock->needs_notification(ticket)) + /* + Don't break conflicting locks if timeout is 0 as 0 is used + To check if there is any conflicting locks... + */ + if (lock->needs_notification(ticket) && lock_wait_timeout) lock->notify_conflicting_locks(this); mysql_prlock_unlock(&lock->m_rwlock); @@ -2217,7 +2221,8 @@ bool MDL_context::acquire_locks(MDL_request_list *mdl_requests, /* Sort requests according to MDL_key. */ if (! (sort_buf= (MDL_request **)my_malloc(req_count * sizeof(MDL_request*), - MYF(MY_WME)))) + MYF(MY_WME | + MY_THREAD_SPECIFIC)))) DBUG_RETURN(TRUE); for (p_req= sort_buf; p_req < sort_buf + req_count; p_req++) diff --git a/sql/mysqld.cc b/sql/mysqld.cc index 2507b29baca..75c4094324b 100644 --- a/sql/mysqld.cc +++ b/sql/mysqld.cc @@ -713,8 +713,8 @@ char *opt_logname, *opt_slow_logname, *opt_bin_logname; static volatile sig_atomic_t kill_in_progress; my_bool opt_stack_trace; -my_bool opt_expect_abort= 0; -static my_bool opt_bootstrap, opt_myisam_log; +my_bool opt_expect_abort= 0, opt_bootstrap= 0; +static my_bool opt_myisam_log; static int cleanup_done; static ulong opt_specialflag; static char *opt_binlog_index_name; @@ -1150,7 +1150,7 @@ private: void Buffered_logs::init() { - init_alloc_root(&m_root, 1024, 0); + init_alloc_root(&m_root, 1024, 0, MYF(0)); } void Buffered_logs::cleanup() @@ -1788,6 +1788,7 @@ extern "C" void unireg_abort(int exit_code) static void mysqld_exit(int exit_code) { + DBUG_ENTER("mysqld_exit"); /* Important note: we wait for the signal thread to end, but if a kill -15 signal was sent, the signal thread did @@ -1801,6 +1802,7 @@ static void mysqld_exit(int exit_code) #ifdef WITH_PERFSCHEMA_STORAGE_ENGINE shutdown_performance_schema(); #endif + DBUG_LEAVE; exit(exit_code); /* purecov: inspected */ } @@ -2519,9 +2521,13 @@ void unlink_thd(THD *thd) sync feature has been shut down at this point. */ DBUG_EXECUTE_IF("sleep_after_lock_thread_count_before_delete_thd", sleep(5);); + /* + We must delete thd inside the lock to ensure that we don't start cleanup + before THD is deleted + */ + delete thd; mysql_mutex_unlock(&LOCK_thread_count); - delete thd; DBUG_VOID_RETURN; } @@ -2623,7 +2629,7 @@ bool one_thread_per_connection_end(THD *thd, bool put_in_cache) DBUG_ENTER("one_thread_per_connection_end"); unlink_thd(thd); /* Mark that current_thd is not valid anymore */ - my_pthread_setspecific_ptr(THR_THD, 0); + set_current_thd(0); if (put_in_cache) { mysql_mutex_lock(&LOCK_thread_count); @@ -2635,9 +2641,10 @@ bool one_thread_per_connection_end(THD *thd, bool put_in_cache) /* It's safe to broadcast outside a lock (COND... is not deleted here) */ DBUG_PRINT("signal", ("Broadcasting COND_thread_count")); + mysql_cond_broadcast(&COND_thread_count); + DBUG_LEAVE; // Must match DBUG_ENTER() my_thread_end(); - mysql_cond_broadcast(&COND_thread_count); pthread_exit(0); return 0; // Avoid compiler warnings @@ -3170,9 +3177,9 @@ void my_message_sql(uint error, const char *str, myf MyFlags) THD *thd= current_thd; MYSQL_ERROR::enum_warning_level level; sql_print_message_func func; - DBUG_ENTER("my_message_sql"); - DBUG_PRINT("error", ("error: %u message: '%s' Flag: %d", error, str, MyFlags)); + DBUG_PRINT("error", ("error: %u message: '%s' Flag: %lu", error, str, + MyFlags)); DBUG_ASSERT(str != NULL); DBUG_ASSERT(error != 0); @@ -3458,6 +3465,7 @@ SHOW_VAR com_status_vars[]= { {NullS, NullS, SHOW_LONG} }; + #ifdef HAVE_PSI_STATEMENT_INTERFACE PSI_statement_info sql_statement_info[(uint) SQLCOM_END + 1]; PSI_statement_info com_statement_info[(uint) COM_END + 1]; @@ -3520,14 +3528,69 @@ void init_com_statement_info() #endif +#ifdef SAFEMALLOC +/* + Return the id for the current THD, to allow safemalloc to associate + the memory with the right id. +*/ + +extern "C" my_thread_id mariadb_dbug_id() +{ + THD *thd; + if ((thd= current_thd)) + { + return thd->thread_id; + } + return my_thread_dbug_id(); +} +#endif /* SAFEMALLOC */ + +/* Thread Mem Usage By P.Linux */ +extern "C" { +static void my_malloc_size_cb_func(long long size, my_bool is_thread_specific) +{ + /* If thread specific memory */ + if (is_thread_specific) + { + THD *thd= current_thd; + if (mysqld_server_initialized || thd) + { + /* + THD may not be set if we are called from my_net_init() before THD + thread has started. + However, this should never happen, so better to assert and + fix this. + */ + DBUG_ASSERT(thd); + if (thd) + { + DBUG_PRINT("info", ("memory_used: %lld size: %lld", + (longlong) thd->status_var.memory_used, size)); + thd->status_var.memory_used+= size; + DBUG_ASSERT((longlong) thd->status_var.memory_used >= 0); + } + } + } + // workaround for gcc 4.2.4-1ubuntu4 -fPIE (from DEB_BUILD_HARDENING=1) + int64 volatile * volatile ptr=&global_status_var.memory_used; + my_atomic_add64(ptr, size); +} +} + + static int init_common_variables() { umask(((~my_umask) & 0666)); my_decimal_set_zero(&decimal_zero); // set decimal_zero constant; + set_malloc_size_cb(my_malloc_size_cb_func); + tzset(); // Set tzname sf_leaking_memory= 0; // no memory leaks from now on +#ifdef SAFEMALLOC + sf_malloc_dbug_id= mariadb_dbug_id; +#endif max_system_variables.pseudo_thread_id= (ulong)~0; server_start_time= flush_status_time= my_time(0); @@ -3992,6 +4055,7 @@ You should consider changing lower_case_table_names to 1 or 2", static int init_thread_environment() { + DBUG_ENTER("init_thread_environment"); mysql_mutex_init(key_LOCK_thread_count, &LOCK_thread_count, MY_MUTEX_INIT_FAST); mysql_mutex_init(key_LOCK_status, &LOCK_status, MY_MUTEX_INIT_FAST); mysql_mutex_init(key_LOCK_delayed_insert, @@ -4069,9 +4133,9 @@ static int init_thread_environment() pthread_key_create(&THR_MALLOC,NULL)) { sql_print_error("Can't create thread-keys"); - return 1; + DBUG_RETURN(1); } - return 0; + DBUG_RETURN(0); } @@ -4736,6 +4800,7 @@ static void test_lc_time_sz() } #endif//DBUG_OFF + #ifdef __WIN__ int win_main(int argc, char **argv) #else @@ -4748,6 +4813,8 @@ int mysqld_main(int argc, char **argv) */ my_progname= argv[0]; sf_leaking_memory= 1; // no safemalloc memory leak reports if we exit early + mysqld_server_started= mysqld_server_initialized= 0; + #ifdef HAVE_NPTL ld_assume_kernel_is_set= (getenv("LD_ASSUME_KERNEL") != 0); #endif @@ -4760,7 +4827,6 @@ int mysqld_main(int argc, char **argv) } #endif - mysqld_server_started= mysqld_server_initialized= 0; orig_argc= argc; orig_argv= argv; my_getopt_use_args_separator= TRUE; @@ -4790,7 +4856,7 @@ int mysqld_main(int argc, char **argv) my_getopt_skip_unknown= TRUE; /* prepare all_early_options array */ - my_init_dynamic_array(&all_early_options, sizeof(my_option), 100, 25); + my_init_dynamic_array(&all_early_options, sizeof(my_option), 100, 25, MYF(0)); sys_var_add_options(&all_early_options, sys_var::PARSE_EARLY); add_terminator(&all_early_options); @@ -5060,8 +5126,6 @@ int mysqld_main(int argc, char **argv) if (Events::init(opt_noacl || opt_bootstrap)) unireg_abort(1); - mysqld_server_initialized= 1; - if (opt_bootstrap) { select_thread_in_use= 0; // Allow 'kill' to work @@ -5075,6 +5139,9 @@ int mysqld_main(int argc, char **argv) } } + /* It's now safe to use thread specific memory */ + mysqld_server_initialized= 1; + create_shutdown_thread(); start_handle_manager(); @@ -5104,7 +5171,6 @@ int mysqld_main(int argc, char **argv) Service.SetRunning(); #endif - /* Signal threads waiting for server to be started */ mysql_mutex_lock(&LOCK_server_started); mysqld_server_started= 1; @@ -5391,7 +5457,7 @@ static void bootstrap(MYSQL_FILE *file) THD *thd= new THD; thd->bootstrap=1; - my_net_init(&thd->net,(st_vio*) 0); + my_net_init(&thd->net,(st_vio*) 0, MYF(0)); thd->max_client_packet_length= thd->net.max_packet; thd->security_ctx->master_access= ~(ulong)0; thd->thread_id= thd->variables.pseudo_thread_id= thread_id++; @@ -5479,7 +5545,7 @@ void create_thread_to_handle_connection(THD *thd) if (cached_thread_count > wake_thread) { /* Get thread from cache */ - thread_cache.append(thd); + thread_cache.push_back(thd); wake_thread++; mysql_cond_signal(&COND_thread_cache); } @@ -5818,12 +5884,16 @@ void handle_connections_sockets() ** Don't allow too many connections */ + DBUG_PRINT("info", ("Creating THD for new connection")); if (!(thd= new THD)) { (void) mysql_socket_shutdown(new_sock, SHUT_RDWR); (void) mysql_socket_close(new_sock); continue; } + /* Set to get io buffers to be part of THD */ + set_current_thd(thd); + is_unix_sock= (mysql_socket_getfd(sock) == mysql_socket_getfd(unix_sock)); @@ -5831,7 +5901,7 @@ void handle_connections_sockets() mysql_socket_vio_new(new_sock, is_unix_sock ? VIO_TYPE_SOCKET : VIO_TYPE_TCPIP, is_unix_sock ? VIO_LOCALHOST: 0)) || - my_net_init(&thd->net, vio_tmp)) + my_net_init(&thd->net, vio_tmp, MYF(MY_THREAD_SPECIFIC))) { /* Only delete the temporary vio if we didn't already attach it to the @@ -5846,6 +5916,7 @@ void handle_connections_sockets() (void) mysql_socket_close(new_sock); } delete thd; + set_current_thd(0); continue; } @@ -5859,6 +5930,7 @@ void handle_connections_sockets() thd->scheduler= extra_thread_scheduler; } create_new_thread(thd); + set_current_thd(0); } DBUG_VOID_RETURN; } @@ -5952,16 +6024,19 @@ pthread_handler_t handle_connections_namedpipes(void *arg) CloseHandle(hConnectedPipe); continue; } + set_current_thd(thd); if (!(thd->net.vio= vio_new_win32pipe(hConnectedPipe)) || - my_net_init(&thd->net, thd->net.vio)) + my_net_init(&thd->net, thd->net.vio, MYF(MY_THREAD_SPECIFIC))) { close_connection(thd, ER_OUT_OF_RESOURCES); delete thd; + set_current_thd(0); continue; } /* Host is unknown */ thd->security_ctx->host= my_strdup(my_localhost, MYF(0)); create_new_thread(thd); + set_current_thd(0); } CloseHandle(connectOverlapped.hEvent); DBUG_LEAVE; @@ -6141,6 +6216,7 @@ pthread_handler_t handle_connections_shared_memory(void *arg) errmsg= "Could not set client to read mode"; goto errorconn; } + set_current_thd(thd); if (!(thd->net.vio= vio_new_win32shared_memory(handle_client_file_map, handle_client_map, event_client_wrote, @@ -6148,7 +6224,7 @@ pthread_handler_t handle_connections_shared_memory(void *arg) event_server_wrote, event_server_read, event_conn_closed)) || - my_net_init(&thd->net, thd->net.vio)) + my_net_init(&thd->net, thd->net.vio, MYF(MY_THREAD_SPECIFIC))) { close_connection(thd, ER_OUT_OF_RESOURCES); errmsg= 0; @@ -6157,6 +6233,7 @@ pthread_handler_t handle_connections_shared_memory(void *arg) thd->security_ctx->host= my_strdup(my_localhost, MYF(0)); /* Host is unknown */ create_new_thread(thd); connect_number++; + set_current_thd(thd); continue; errorconn: @@ -6184,6 +6261,7 @@ errorconn: CloseHandle(event_conn_closed); delete thd; } + set_current_thd(0); /* End shared memory handling */ error: @@ -7256,6 +7334,7 @@ SHOW_VAR status_vars[]= { {"Key", (char*) &show_default_keycache, SHOW_FUNC}, {"Last_query_cost", (char*) offsetof(STATUS_VAR, last_query_cost), SHOW_DOUBLE_STATUS}, {"Max_used_connections", (char*) &max_used_connections, SHOW_LONG}, + {"Memory_used", (char*) offsetof(STATUS_VAR, memory_used), SHOW_LONGLONG_STATUS}, {"Not_flushed_delayed_rows", (char*) &delayed_rows_in_use, SHOW_LONG_NOFLUSH}, {"Open_files", (char*) &my_file_opened, SHOW_LONG_NOFLUSH}, {"Open_streams", (char*) &my_stream_opened, SHOW_LONG_NOFLUSH}, @@ -7404,7 +7483,7 @@ static int option_cmp(my_option *a, my_option *b) static void print_help() { MEM_ROOT mem_root; - init_alloc_root(&mem_root, 4096, 4096); + init_alloc_root(&mem_root, 4096, 4096, MYF(0)); pop_dynamic(&all_options); sys_var_add_options(&all_options, sys_var::PARSE_EARLY); @@ -8168,7 +8247,7 @@ static int get_options(int *argc_ptr, char ***argv_ptr) /* prepare all_options array */ my_init_dynamic_array(&all_options, sizeof(my_option), array_elements(my_long_options), - array_elements(my_long_options)/4); + array_elements(my_long_options)/4, MYF(0)); for (my_option *opt= my_long_options; opt < my_long_options + array_elements(my_long_options) - 1; opt++) @@ -8670,7 +8749,7 @@ void refresh_status(THD *thd) add_to_status(&global_status_var, &thd->status_var); /* Reset thread's status variables */ - bzero((uchar*) &thd->status_var, sizeof(thd->status_var)); + thd->set_status_var_init(); bzero((uchar*) &thd->org_status_var, sizeof(thd->org_status_var)); thd->start_bytes_received= 0; diff --git a/sql/mysqld.h b/sql/mysqld.h index e0efbe60390..716423f9bd2 100644 --- a/sql/mysqld.h +++ b/sql/mysqld.h @@ -78,7 +78,7 @@ extern CHARSET_INFO *character_set_filesystem; extern MY_BITMAP temp_pool; extern bool opt_large_files, server_id_supplied; extern bool opt_update_log, opt_bin_log, opt_error_log; -extern my_bool opt_log, opt_slow_log; +extern my_bool opt_log, opt_slow_log, opt_bootstrap; extern my_bool opt_backup_history_log; extern my_bool opt_backup_progress_log; extern ulonglong log_output_options; @@ -670,6 +670,10 @@ inline THD *_current_thd(void) } #endif #define current_thd _current_thd() +inline int set_current_thd(THD *thd) +{ + return my_pthread_setspecific_ptr(THR_THD, thd); +} /* @todo remove, make it static in ha_maria.cc diff --git a/sql/net_serv.cc b/sql/net_serv.cc index 7e9425f51f2..b6890ab9fda 100644 --- a/sql/net_serv.cc +++ b/sql/net_serv.cc @@ -124,14 +124,15 @@ static my_bool net_write_buff(NET *, const uchar *, ulong); /** Init with packet info. */ -my_bool my_net_init(NET *net, Vio* vio) +my_bool my_net_init(NET *net, Vio* vio, uint my_flags) { DBUG_ENTER("my_net_init"); + DBUG_PRINT("enter", ("my_flags: %u", my_flags)); net->vio = vio; my_net_local_init(net); /* Set some limits */ if (!(net->buff=(uchar*) my_malloc((size_t) net->max_packet+ NET_HEADER_SIZE + COMP_HEADER_SIZE +1, - MYF(MY_WME)))) + MYF(MY_WME | my_flags)))) DBUG_RETURN(1); net->buff_end=net->buff+net->max_packet; net->error=0; net->return_status=0; @@ -143,6 +144,7 @@ my_bool my_net_init(NET *net, Vio* vio) net->net_skip_rest_factor= 0; net->last_errno=0; net->unused= 0; + net->thread_specific_malloc= test(my_flags & MY_THREAD_SPECIFIC); #ifdef MYSQL_SERVER net->extension= NULL; #endif @@ -201,7 +203,9 @@ my_bool net_realloc(NET *net, size_t length) */ if (!(buff= (uchar*) my_realloc((char*) net->buff, pkt_length + NET_HEADER_SIZE + COMP_HEADER_SIZE + 1, - MYF(MY_WME)))) + MYF(MY_WME | + (net->thread_specific_malloc ? + MY_THREAD_SPECIFIC : 0))))) { /* @todo: 1 and 2 codes are identical. */ net->error= 1; @@ -613,7 +617,10 @@ net_real_write(NET *net,const uchar *packet, size_t len) uchar *b; uint header_length=NET_HEADER_SIZE+COMP_HEADER_SIZE; if (!(b= (uchar*) my_malloc(len + NET_HEADER_SIZE + - COMP_HEADER_SIZE + 1, MYF(MY_WME)))) + COMP_HEADER_SIZE + 1, + MYF(MY_WME | + (net->thread_specific_malloc ? + MY_THREAD_SPECIFIC : 0))))) { net->error= 2; net->last_errno= ER_OUT_OF_RESOURCES; diff --git a/sql/opt_range.cc b/sql/opt_range.cc index 72eb8460496..2205d2fcab4 100644 --- a/sql/opt_range.cc +++ b/sql/opt_range.cc @@ -117,6 +117,7 @@ #include "records.h" // init_read_record, end_read_record #include <m_ctype.h> #include "sql_select.h" +#include "filesort.h" // filesort_free_buffers #ifndef EXTRA_DEBUG #define test_rb_tree(A,B) {} @@ -1772,7 +1773,8 @@ QUICK_RANGE_SELECT::QUICK_RANGE_SELECT(THD *thd, TABLE *table, uint key_nr, index= key_nr; head= table; key_part_info= head->key_info[index].key_part; - my_init_dynamic_array(&ranges, sizeof(QUICK_RANGE*), 16, 16); + my_init_dynamic_array(&ranges, sizeof(QUICK_RANGE*), 16, 16, + MYF(MY_THREAD_SPECIFIC)); /* 'thd' is not accessible in QUICK_RANGE_SELECT::reset(). */ mrr_buf_size= thd->variables.mrr_buff_size; @@ -1781,7 +1783,8 @@ QUICK_RANGE_SELECT::QUICK_RANGE_SELECT(THD *thd, TABLE *table, uint key_nr, if (!no_alloc && !parent_alloc) { // Allocates everything through the internal memroot - init_sql_alloc(&alloc, thd->variables.range_alloc_block_size, 0); + init_sql_alloc(&alloc, thd->variables.range_alloc_block_size, 0, + MYF(MY_THREAD_SPECIFIC)); thd->mem_root= &alloc; } else @@ -1791,7 +1794,7 @@ QUICK_RANGE_SELECT::QUICK_RANGE_SELECT(THD *thd, TABLE *table, uint key_nr, /* Allocate a bitmap for used columns (Q: why not on MEM_ROOT?) */ if (!(bitmap= (my_bitmap_map*) my_malloc(head->s->column_bitmap_size, - MYF(MY_WME)))) + MYF(MY_WME | MY_THREAD_SPECIFIC)))) { column_bitmap.bitmap= 0; *create_error= 1; @@ -1876,7 +1879,8 @@ QUICK_INDEX_SORT_SELECT::QUICK_INDEX_SORT_SELECT(THD *thd_param, index= MAX_KEY; head= table; bzero(&read_record, sizeof(read_record)); - init_sql_alloc(&alloc, thd->variables.range_alloc_block_size, 0); + init_sql_alloc(&alloc, thd->variables.range_alloc_block_size, 0, + MYF(MY_THREAD_SPECIFIC)); DBUG_VOID_RETURN; } @@ -1889,7 +1893,8 @@ int QUICK_INDEX_SORT_SELECT::init() int QUICK_INDEX_SORT_SELECT::reset() { DBUG_ENTER("QUICK_INDEX_SORT_SELECT::reset"); - DBUG_RETURN(read_keys_and_merge()); + const int retval= read_keys_and_merge(); + DBUG_RETURN(retval); } bool @@ -1946,7 +1951,8 @@ QUICK_ROR_INTERSECT_SELECT::QUICK_ROR_INTERSECT_SELECT(THD *thd_param, head= table; record= head->record[0]; if (!parent_alloc) - init_sql_alloc(&alloc, thd->variables.range_alloc_block_size, 0); + init_sql_alloc(&alloc, thd->variables.range_alloc_block_size, 0, + MYF(MY_THREAD_SPECIFIC)); else bzero(&alloc, sizeof(MEM_ROOT)); last_rowid= (uchar*) alloc_root(parent_alloc? parent_alloc : &alloc, @@ -2140,8 +2146,9 @@ int QUICK_ROR_INTERSECT_SELECT::init_ror_merged_scan(bool reuse_handler) There is no use of this->file. Use it for the first of merged range selects. */ - if (quick->init_ror_merged_scan(TRUE)) - DBUG_RETURN(1); + int error= quick->init_ror_merged_scan(TRUE); + if (error) + DBUG_RETURN(error); quick->file->extra(HA_EXTRA_KEYREAD_PRESERVE_FIELDS); } while ((cur= quick_it++)) @@ -2244,7 +2251,8 @@ QUICK_ROR_UNION_SELECT::QUICK_ROR_UNION_SELECT(THD *thd_param, head= table; rowid_length= table->file->ref_length; record= head->record[0]; - init_sql_alloc(&alloc, thd->variables.range_alloc_block_size, 0); + init_sql_alloc(&alloc, thd->variables.range_alloc_block_size, 0, + MYF(MY_THREAD_SPECIFIC)); thd_param->mem_root= &alloc; } @@ -2334,8 +2342,8 @@ int QUICK_ROR_UNION_SELECT::reset() List_iterator_fast<QUICK_SELECT_I> it(quick_selects); while ((quick= it++)) { - if (quick->reset()) - DBUG_RETURN(1); + if ((error= quick->reset())) + DBUG_RETURN(error); if ((error= quick->get_next())) { if (error == HA_ERR_END_OF_FILE) @@ -2346,10 +2354,10 @@ int QUICK_ROR_UNION_SELECT::reset() queue_insert(&queue, (uchar*)quick); } - if (head->file->ha_rnd_init_with_error(1)) + if ((error= head->file->ha_rnd_init(1))) { DBUG_PRINT("error", ("ROR index_merge rnd_init call failed")); - DBUG_RETURN(1); + DBUG_RETURN(error); } DBUG_RETURN(0); @@ -2978,7 +2986,8 @@ int SQL_SELECT::test_quick_select(THD *thd, key_map keys_to_use, param.force_default_mrr= ordered_output; thd->no_errors=1; // Don't warn about NULL - init_sql_alloc(&alloc, thd->variables.range_alloc_block_size, 0); + init_sql_alloc(&alloc, thd->variables.range_alloc_block_size, 0, + MYF(MY_THREAD_SPECIFIC)); if (!(param.key_parts= (KEY_PART*) alloc_root(&alloc, sizeof(KEY_PART) * @@ -3423,7 +3432,8 @@ bool prune_partitions(THD *thd, TABLE *table, Item *pprune_cond) my_bitmap_map *old_sets[2]; prune_param.part_info= part_info; - init_sql_alloc(&alloc, thd->variables.range_alloc_block_size, 0); + init_sql_alloc(&alloc, thd->variables.range_alloc_block_size, 0, + MYF(MY_THREAD_SPECIFIC)); range_par->mem_root= &alloc; range_par->old_root= thd->mem_root; @@ -10615,7 +10625,10 @@ int read_keys_and_merge_scans(THD *thd, *unique_ptr= unique; } else + { unique->reset(); + filesort_free_buffers(head, false); + } DBUG_ASSERT(file->ref_length == unique->get_size()); DBUG_ASSERT(thd->variables.sortbuff_size == unique->get_max_in_memory_size()); @@ -10774,6 +10787,13 @@ int QUICK_INDEX_INTERSECT_SELECT::get_next() If a Clustered PK scan is present, it is used only to check if row satisfies its condition (and never used for row retrieval). + Locking: to ensure that exclusive locks are only set on records that + are included in the final result we must release the lock + on all rows we read but do not include in the final result. This + must be done on each index that reads the record and the lock + must be released using the same handler (the same quick object) as + used when reading the record. + RETURN 0 - Ok other - Error code if any error occurred. @@ -10784,6 +10804,12 @@ int QUICK_ROR_INTERSECT_SELECT::get_next() List_iterator_fast<QUICK_SELECT_WITH_RECORD> quick_it(quick_selects); QUICK_SELECT_WITH_RECORD *qr; QUICK_RANGE_SELECT* quick; + + /* quick that reads the given rowid first. This is needed in order + to be able to unlock the row using the same handler object that locked + it */ + QUICK_RANGE_SELECT* quick_with_last_rowid; + int error, cmp; uint last_rowid_count=0; DBUG_ENTER("QUICK_ROR_INTERSECT_SELECT::get_next"); @@ -10797,7 +10823,10 @@ int QUICK_ROR_INTERSECT_SELECT::get_next() if (cpk_quick) { while (!error && !cpk_quick->row_in_ranges()) + { + quick->file->unlock_row(); /* row not in range; unlock */ error= quick->get_next(); + } } if (error) DBUG_RETURN(error); @@ -10809,6 +10838,7 @@ int QUICK_ROR_INTERSECT_SELECT::get_next() quick->file->position(quick->record); memcpy(last_rowid, quick->file->ref, head->file->ref_length); last_rowid_count= 1; + quick_with_last_rowid= quick; while (last_rowid_count < quick_selects.elements) { @@ -10822,9 +10852,17 @@ int QUICK_ROR_INTERSECT_SELECT::get_next() do { if ((error= quick->get_next())) + { + quick_with_last_rowid->file->unlock_row(); DBUG_RETURN(error); + } quick->file->position(quick->record); cmp= head->file->cmp_ref(quick->file->ref, last_rowid); + if (cmp < 0) + { + /* This row is being skipped. Release lock on it. */ + quick->file->unlock_row(); + } } while (cmp < 0); key_copy(qr->key_tuple, record, head->key_info + quick->index, @@ -10838,13 +10876,19 @@ int QUICK_ROR_INTERSECT_SELECT::get_next() { while (!cpk_quick->row_in_ranges()) { + quick->file->unlock_row(); /* row not in range; unlock */ if ((error= quick->get_next())) + { + quick_with_last_rowid->file->unlock_row(); DBUG_RETURN(error); + } } quick->file->position(quick->record); } memcpy(last_rowid, quick->file->ref, head->file->ref_length); + quick_with_last_rowid->file->unlock_row(); last_rowid_count= 1; + quick_with_last_rowid= quick; //save the fields here key_copy(qr->key_tuple, record, head->key_info + quick->index, @@ -10971,8 +11015,13 @@ int QUICK_RANGE_SELECT::reset() if (file->inited == handler::NONE) { + DBUG_EXECUTE_IF("bug14365043_2", + DBUG_SET("+d,ha_index_init_fail");); if ((error= file->ha_index_init(index,1))) - goto err; + { + file->print_error(error, MYF(0)); + goto err; + } } /* Allocate buffer if we need one but haven't allocated it yet */ @@ -11014,7 +11063,7 @@ err: /* Restore bitmaps set on entry */ if (in_ror_merged_scan) head->column_bitmaps_set_no_signal(save_read_set, save_write_set); - + DBUG_RETURN(error); } @@ -12882,7 +12931,8 @@ QUICK_GROUP_MIN_MAX_SELECT(TABLE *table, JOIN *join_arg, bool have_min_arg, DBUG_ASSERT(!parent_alloc); if (!parent_alloc) { - init_sql_alloc(&alloc, join->thd->variables.range_alloc_block_size, 0); + init_sql_alloc(&alloc, join->thd->variables.range_alloc_block_size, 0, + MYF(MY_THREAD_SPECIFIC)); join->thd->mem_root= &alloc; } else @@ -12937,7 +12987,8 @@ int QUICK_GROUP_MIN_MAX_SELECT::init() if (min_max_arg_part) { - if (my_init_dynamic_array(&min_max_ranges, sizeof(QUICK_RANGE*), 16, 16)) + if (my_init_dynamic_array(&min_max_ranges, sizeof(QUICK_RANGE*), 16, 16, + MYF(MY_THREAD_SPECIFIC))) return 1; if (have_min) @@ -13183,7 +13234,10 @@ int QUICK_GROUP_MIN_MAX_SELECT::reset(void) head->enable_keyread(); /* We need only the key attributes */ } if ((result= file->ha_index_init(index,1))) + { + head->file->print_error(result, MYF(0)); DBUG_RETURN(result); + } if (quick_prefix_select && quick_prefix_select->reset()) DBUG_RETURN(1); result= file->ha_index_last(record); @@ -13358,9 +13412,10 @@ int QUICK_GROUP_MIN_MAX_SELECT::next_min() */ if (min_max_arg_part && min_max_arg_part->field->is_null()) { + uchar *tmp_key_buff= (uchar*)my_alloca(max_used_key_length); /* Find the first subsequent record without NULL in the MIN/MAX field. */ - key_copy(tmp_record, record, index_info, max_used_key_length); - result= file->ha_index_read_map(record, tmp_record, + key_copy(tmp_key_buff, record, index_info, max_used_key_length); + result= file->ha_index_read_map(record, tmp_key_buff, make_keypart_map(real_key_parts), HA_READ_AFTER_KEY); /* @@ -13376,10 +13431,11 @@ int QUICK_GROUP_MIN_MAX_SELECT::next_min() if (!result) { if (key_cmp(index_info->key_part, group_prefix, real_prefix_len)) - key_restore(record, tmp_record, index_info, 0); + key_restore(record, tmp_key_buff, index_info, 0); } else if (result == HA_ERR_KEY_NOT_FOUND || result == HA_ERR_END_OF_FILE) result= 0; /* There is a result in any case. */ + my_afree(tmp_key_buff); } } diff --git a/sql/opt_range_mrr.cc b/sql/opt_range_mrr.cc index a4345059f85..1f4e36178db 100644 --- a/sql/opt_range_mrr.cc +++ b/sql/opt_range_mrr.cc @@ -248,6 +248,7 @@ walk_up_n_right: /* Here minimum contains also function code bits, and maximum is +inf */ range->start_key.key= seq->param->min_key; range->start_key.length= min_key_length; + range->start_key.keypart_map= make_prev_keypart_map(cur->min_key_parts); range->start_key.flag= (ha_rkey_function) (cur->min_key_flag ^ GEOM_FLAG); } else diff --git a/sql/opt_subselect.cc b/sql/opt_subselect.cc index da7d3dff74d..8cd4ba08ff3 100644 --- a/sql/opt_subselect.cc +++ b/sql/opt_subselect.cc @@ -853,8 +853,7 @@ bool subquery_types_allow_materialization(Item_in_subselect *in_subs) create a blob column because item->max_length is too big. The following check is copied from Item::make_string_field(): */ - if (inner->max_length / inner->collation.collation->mbmaxlen > - CONVERT_IF_BIGGER_TO_BLOB) + if (inner->too_big_for_varchar()) { DBUG_RETURN(FALSE); } @@ -3868,11 +3867,12 @@ SJ_TMP_TABLE::create_sj_weedout_tmp_table(THD *thd) fn_format(path, path, mysql_tmpdir, "", MY_REPLACE_EXT|MY_UNPACK_FILENAME); /* STEP 2: Figure if we'll be using a key or blob+constraint */ + /* it always has my_charset_bin, so mbmaxlen==1 */ if (uniq_tuple_length_arg >= CONVERT_IF_BIGGER_TO_BLOB) using_unique_constraint= TRUE; /* STEP 3: Allocate memory for temptable description */ - init_sql_alloc(&own_root, TABLE_ALLOC_BLOCK_SIZE, 0); + init_sql_alloc(&own_root, TABLE_ALLOC_BLOCK_SIZE, 0, MYF(MY_THREAD_SPECIFIC)); if (!multi_alloc_root(&own_root, &table, sizeof(*table), &share, sizeof(*share), diff --git a/sql/protocol.cc b/sql/protocol.cc index 3af7dc88b88..f6e9e9e62e1 100644 --- a/sql/protocol.cc +++ b/sql/protocol.cc @@ -776,7 +776,7 @@ bool Protocol::send_result_set_metadata(List<Item> *list, uint flags) pos= (char*) local_packet->ptr()+local_packet->length(); *pos++= 12; // Length of packed fields /* inject a NULL to test the client */ - DBUG_EXECUTE_IF("poison_rs_fields", pos[-1]= 0xfb;); + DBUG_EXECUTE_IF("poison_rs_fields", pos[-1]= (char) 0xfb;); if (item->charset_for_protocol() == &my_charset_bin || thd_charset == NULL) { /* No conversion */ diff --git a/sql/records.cc b/sql/records.cc index d52481c36f5..d28799ea9d2 100644 --- a/sql/records.cc +++ b/sql/records.cc @@ -68,6 +68,7 @@ static int rr_index_desc(READ_RECORD *info); void init_read_record_idx(READ_RECORD *info, THD *thd, TABLE *table, bool print_error, uint idx, bool reverse) { + int error; empty_record(table); bzero((char*) info,sizeof(*info)); info->thd= thd; @@ -77,8 +78,13 @@ void init_read_record_idx(READ_RECORD *info, THD *thd, TABLE *table, info->unlock_row= rr_unlock_row; table->status=0; /* And it's always found */ - if (!table->file->inited) - table->file->ha_index_init(idx, 1); + if (!table->file->inited && + (error= table->file->ha_index_init(idx, 1))) + { + if (print_error) + table->file->print_error(error, MYF(0)); + } + /* read_record will be changed to rr_index in rr_index_first */ info->read_record= reverse ? rr_index_last : rr_index_first; } @@ -588,7 +594,7 @@ static int init_rr_cache(THD *thd, READ_RECORD *info) if (info->cache_records <= 2 || !(info->cache=(uchar*) my_malloc_lock(rec_cache_size+info->cache_records* info->struct_length+1, - MYF(0)))) + MYF(MY_THREAD_SPECIFIC)))) DBUG_RETURN(1); #ifdef HAVE_valgrind // Avoid warnings in qsort diff --git a/sql/rpl_filter.cc b/sql/rpl_filter.cc index 0380bc323a3..f2bd036896d 100644 --- a/sql/rpl_filter.cc +++ b/sql/rpl_filter.cc @@ -573,7 +573,7 @@ void Rpl_filter::init_table_rule_array(DYNAMIC_ARRAY* a, bool* a_inited) { my_init_dynamic_array(a, sizeof(TABLE_RULE_ENT*), TABLE_RULE_ARR_SIZE, - TABLE_RULE_ARR_SIZE); + TABLE_RULE_ARR_SIZE, MYF(0)); *a_inited = 1; } diff --git a/sql/rpl_handler.cc b/sql/rpl_handler.cc index 9267190605c..258dae0edb2 100644 --- a/sql/rpl_handler.cc +++ b/sql/rpl_handler.cc @@ -189,7 +189,7 @@ void delegates_destroy() DYNAMIC_ARRAY *plugins= &s.plugins; \ plugin_ref *plugins_buffer= s.plugins_buffer; \ my_init_dynamic_array2(plugins, sizeof(plugin_ref), \ - plugins_buffer, 8, 8); \ + plugins_buffer, 8, 8, MYF(0)); \ read_lock(); \ Observer_info_iterator iter= observer_info_iter(); \ Observer_info *info= iter++; \ diff --git a/sql/rpl_handler.h b/sql/rpl_handler.h index 4743fffb9a0..e028fb49808 100644 --- a/sql/rpl_handler.h +++ b/sql/rpl_handler.h @@ -124,7 +124,7 @@ public: inited= FALSE; if (my_rwlock_init(&lock, NULL)) return; - init_sql_alloc(&memroot, 1024, 0); + init_sql_alloc(&memroot, 1024, 0, MYF(0)); inited= TRUE; } ~Delegate() diff --git a/sql/rpl_mi.cc b/sql/rpl_mi.cc index 2ca700a123a..3e02b555dc0 100644 --- a/sql/rpl_mi.cc +++ b/sql/rpl_mi.cc @@ -62,7 +62,7 @@ Master_info::Master_info(LEX_STRING *connection_name_arg, my_casedn_str(system_charset_info, cmp_connection_name.str); } - my_init_dynamic_array(&ignore_server_ids, sizeof(::server_id), 16, 16); + my_init_dynamic_array(&ignore_server_ids, sizeof(::server_id), 16, 16, MYF(0)); bzero((char*) &file, sizeof(file)); mysql_mutex_init(key_master_info_run_lock, &run_lock, MY_MUTEX_INIT_FAST); mysql_mutex_init(key_master_info_data_lock, &data_lock, MY_MUTEX_INIT_FAST); diff --git a/sql/rpl_mi.h b/sql/rpl_mi.h index c91699662c8..108bd51ff47 100644 --- a/sql/rpl_mi.h +++ b/sql/rpl_mi.h @@ -71,9 +71,9 @@ class Master_info : public Slave_reporting_capability /* the variables below are needed because we can change masters on the fly */ char master_log_name[FN_REFLEN+6]; /* Room for multi-*/ - char host[HOSTNAME_LENGTH+1]; - char user[USERNAME_LENGTH+1]; - char password[MAX_PASSWORD_LENGTH+1]; + char host[HOSTNAME_LENGTH*SYSTEM_CHARSET_MBMAXLEN+1]; + char user[USERNAME_LENGTH*+1]; + char password[MAX_PASSWORD_LENGTH*SYSTEM_CHARSET_MBMAXLEN+1]; LEX_STRING connection_name; /* User supplied connection name */ LEX_STRING cmp_connection_name; /* Connection name in lower case */ bool ssl; // enables use of SSL connection if true diff --git a/sql/rpl_rli.cc b/sql/rpl_rli.cc index 364dfb3bc20..2e74acc0345 100644 --- a/sql/rpl_rli.cc +++ b/sql/rpl_rli.cc @@ -182,7 +182,8 @@ a file name for --relay-log-index option", opt_relaylog_index_name); ln= rli->relay_log.generate_name(opt_relay_logname, "-relay-bin", 1, buf); /* We send the warning only at startup, not after every RESET SLAVE */ - if (!opt_relay_logname && !opt_relaylog_index_name && !name_warning_sent) + if (!opt_relay_logname && !opt_relaylog_index_name && !name_warning_sent && + !opt_bootstrap) { /* User didn't give us info to name the relay log index file. diff --git a/sql/rpl_tblmap.cc b/sql/rpl_tblmap.cc index b7ac1b2d091..7b55911d887 100644 --- a/sql/rpl_tblmap.cc +++ b/sql/rpl_tblmap.cc @@ -46,7 +46,7 @@ table_mapping::table_mapping() offsetof(entry,table_id),sizeof(ulong), 0,0,0); /* We don't preallocate any block, this is consistent with m_free=0 above */ - init_alloc_root(&m_mem_root, TABLE_ID_HASH_SIZE*sizeof(entry), 0); + init_alloc_root(&m_mem_root, TABLE_ID_HASH_SIZE*sizeof(entry), 0, MYF(0)); DBUG_VOID_RETURN; } diff --git a/sql/rpl_utility.cc b/sql/rpl_utility.cc index 069fac1c3ec..1b9e744bcc1 100644 --- a/sql/rpl_utility.cc +++ b/sql/rpl_utility.cc @@ -1117,7 +1117,7 @@ bool event_checksum_test(uchar *event_buf, ulong event_len, uint8 alg) Deferred_log_events::Deferred_log_events(Relay_log_info *rli) : last_added(NULL) { - my_init_dynamic_array(&array, sizeof(Log_event *), 32, 16); + my_init_dynamic_array(&array, sizeof(Log_event *), 32, 16, MYF(0)); } Deferred_log_events::~Deferred_log_events() diff --git a/sql/slave.cc b/sql/slave.cc index 95818e60426..78fa7998012 100644 --- a/sql/slave.cc +++ b/sql/slave.cc @@ -1793,7 +1793,9 @@ past_checksum: } } } +#ifndef DBUG_OFF after_set_capability: +#endif err: if (errmsg) @@ -2446,13 +2448,23 @@ static int init_slave_thread(THD* thd, Master_info *mi, SLAVE_THD_TYPE thd_type) { DBUG_ENTER("init_slave_thread"); -#if !defined(DBUG_OFF) - int simulate_error= 0; -#endif + int simulate_error __attribute__((unused))= 0; + DBUG_EXECUTE_IF("simulate_io_slave_error_on_init", + simulate_error|= (1 << SLAVE_THD_IO);); + DBUG_EXECUTE_IF("simulate_sql_slave_error_on_init", + simulate_error|= (1 << SLAVE_THD_SQL);); + /* We must call store_globals() before doing my_net_init() */ + if (init_thr_lock() || thd->store_globals() || + my_net_init(&thd->net, 0, MYF(MY_THREAD_SPECIFIC)) || + IF_DBUG(simulate_error & (1<< thd_type), 0)) + { + thd->cleanup(); + DBUG_RETURN(-1); + } + thd->system_thread = (thd_type == SLAVE_THD_SQL) ? SYSTEM_THREAD_SLAVE_SQL : SYSTEM_THREAD_SLAVE_IO; thd->security_ctx->skip_grants(); - my_net_init(&thd->net, 0); /* Adding MAX_LOG_EVENT_HEADER_LEN to the max_allowed_packet on all slave threads, since a replication event can become this much larger @@ -2469,17 +2481,6 @@ static int init_slave_thread(THD* thd, Master_info *mi, thd->thread_id= thd->variables.pseudo_thread_id= thread_id++; mysql_mutex_unlock(&LOCK_thread_count); - DBUG_EXECUTE_IF("simulate_io_slave_error_on_init", - simulate_error|= (1 << SLAVE_THD_IO);); - DBUG_EXECUTE_IF("simulate_sql_slave_error_on_init", - simulate_error|= (1 << SLAVE_THD_SQL);); - if (init_thr_lock() || thd->store_globals() || - IF_DBUG(simulate_error & (1<< thd_type), 0)) - { - thd->cleanup(); - DBUG_RETURN(-1); - } - if (thd_type == SLAVE_THD_SQL) THD_STAGE_INFO(thd, stage_waiting_for_the_next_event_in_relay_log); else @@ -3516,8 +3517,6 @@ err_during_init: mi->rli.relay_log.description_event_for_queue= 0; // TODO: make rpl_status part of Master_info change_rpl_status(RPL_ACTIVE_SLAVE,RPL_IDLE_SLAVE); - DBUG_ASSERT(thd->net.buff != 0); - net_end(&thd->net); // destructor will not free it, because net.vio is 0 mysql_mutex_lock(&LOCK_thread_count); THD_CHECK_SENTRY(thd); delete thd; @@ -3923,8 +3922,6 @@ err_during_init: to avoid unneeded position re-init */ thd->temporary_tables = 0; // remove tempation from destructor to close them - DBUG_ASSERT(thd->net.buff != 0); - net_end(&thd->net); // destructor will not free it, because we are weird DBUG_ASSERT(rli->sql_thd == thd); THD_CHECK_SENTRY(thd); rli->sql_thd= 0; diff --git a/sql/sp.cc b/sql/sp.cc index f6f5d640bc0..9cc68339d6f 100644 --- a/sql/sp.cc +++ b/sql/sp.cc @@ -1418,7 +1418,6 @@ bool lock_db_routines(THD *thd, char *db) { TABLE *table; uint key_len; - int nxtres= 0; Open_tables_backup open_tables_state_backup; MDL_request_list mdl_requests; Lock_db_routines_error_handler err_handler; @@ -1446,7 +1445,13 @@ bool lock_db_routines(THD *thd, char *db) table->field[MYSQL_PROC_FIELD_DB]->store(db, strlen(db), system_charset_info); key_len= table->key_info->key_part[0].store_length; table->field[MYSQL_PROC_FIELD_DB]->get_key_image(keybuf, key_len, Field::itRAW); - table->file->ha_index_init(0, 1); + int nxtres= table->file->ha_index_init(0, 1); + if (nxtres) + { + table->file->print_error(nxtres, MYF(0)); + close_system_tables(thd, &open_tables_state_backup); + DBUG_RETURN(true); + } if (! table->file->ha_index_read_map(table->record[0], keybuf, (key_part_map)1, HA_READ_KEY_EXACT)) @@ -1509,7 +1514,11 @@ sp_drop_db_routines(THD *thd, char *db) table->field[MYSQL_PROC_FIELD_DB]->get_key_image(keybuf, key_len, Field::itRAW); ret= SP_OK; - table->file->ha_index_init(0, 1); + if (table->file->ha_index_init(0, 1)) + { + ret= SP_KEY_NOT_FOUND; + goto err_idx_init; + } if (!table->file->ha_index_read_map(table->record[0], keybuf, (key_part_map)1, HA_READ_KEY_EXACT)) { @@ -1535,6 +1544,7 @@ sp_drop_db_routines(THD *thd, char *db) } table->file->ha_index_end(); +err_idx_init: close_thread_tables(thd); /* Make sure to only release the MDL lock on mysql.proc, not other diff --git a/sql/sp_head.cc b/sql/sp_head.cc index b725f6d7fcc..7cd2e789351 100644 --- a/sql/sp_head.cc +++ b/sql/sp_head.cc @@ -508,7 +508,7 @@ sp_head::operator new(size_t size) throw() MEM_ROOT own_root; sp_head *sp; - init_sql_alloc(&own_root, MEM_ROOT_BLOCK_SIZE, MEM_ROOT_PREALLOC); + init_sql_alloc(&own_root, MEM_ROOT_BLOCK_SIZE, MEM_ROOT_PREALLOC, MYF(0)); sp= (sp_head *) alloc_root(&own_root, size); if (sp == NULL) DBUG_RETURN(NULL); @@ -592,7 +592,7 @@ sp_head::init(LEX *lex) types of stored procedures to simplify reset_lex()/restore_lex() code. */ lex->trg_table_fields.empty(); - my_init_dynamic_array(&m_instr, sizeof(sp_instr *), 16, 8); + my_init_dynamic_array(&m_instr, sizeof(sp_instr *), 16, 8, MYF(0)); m_param_begin= NULL; m_param_end= NULL; @@ -1252,7 +1252,7 @@ sp_head::execute(THD *thd, bool merge_da_on_success) DBUG_RETURN(TRUE); /* init per-instruction memroot */ - init_sql_alloc(&execute_mem_root, MEM_ROOT_BLOCK_SIZE, 0); + init_sql_alloc(&execute_mem_root, MEM_ROOT_BLOCK_SIZE, 0, MYF(0)); DBUG_ASSERT(!(m_flags & IS_INVOKED)); m_flags|= IS_INVOKED; @@ -1709,7 +1709,7 @@ sp_head::execute_trigger(THD *thd, TODO: we should create sp_rcontext once per command and reuse it on subsequent executions of a trigger. */ - init_sql_alloc(&call_mem_root, MEM_ROOT_BLOCK_SIZE, 0); + init_sql_alloc(&call_mem_root, MEM_ROOT_BLOCK_SIZE, 0, MYF(0)); thd->set_n_backup_active_arena(&call_arena, &backup_arena); if (!(nctx= new sp_rcontext(m_pcont, 0, octx)) || @@ -1826,7 +1826,7 @@ sp_head::execute_function(THD *thd, Item **argp, uint argcount, TODO: we should create sp_rcontext once per command and reuse it on subsequent executions of a function/trigger. */ - init_sql_alloc(&call_mem_root, MEM_ROOT_BLOCK_SIZE, 0); + init_sql_alloc(&call_mem_root, MEM_ROOT_BLOCK_SIZE, 0, MYF(0)); thd->set_n_backup_active_arena(&call_arena, &backup_arena); if (!(nctx= new sp_rcontext(m_pcont, return_value_fld, octx)) || @@ -4017,8 +4017,6 @@ typedef struct st_sp_table Multi-set key: db_name\0table_name\0alias\0 - for normal tables db_name\0table_name\0 - for temporary tables - Note that in both cases we don't take last '\0' into account when - we count length of key. */ LEX_STRING qname; uint db_length, table_name_length; @@ -4075,19 +4073,26 @@ sp_head::merge_table_list(THD *thd, TABLE_LIST *table, LEX *lex_for_tmp_check) for (; table ; table= table->next_global) if (!table->derived && !table->schema_table) { - char tname[(SAFE_NAME_LEN + 1) * 3]; // db\0table\0alias\0 - uint tlen, alen; - - tlen= table->db_length; - memcpy(tname, table->db, tlen); - tname[tlen++]= '\0'; - memcpy(tname+tlen, table->table_name, table->table_name_length); - tlen+= table->table_name_length; - tname[tlen++]= '\0'; - alen= strlen(table->alias); - memcpy(tname+tlen, table->alias, alen); - tlen+= alen; - tname[tlen]= '\0'; + /* + Structure of key for the multi-set is "db\0table\0alias\0". + Since "alias" part can have arbitrary length we use String + object to construct the key. By default String will use + buffer allocated on stack with NAME_LEN bytes reserved for + alias, since in most cases it is going to be smaller than + NAME_LEN bytes. + */ + char tname_buff[(SAFE_NAME_LEN + 1) * 3]; + String tname(tname_buff, sizeof(tname_buff), &my_charset_bin); + uint temp_table_key_length; + + tname.length(0); + tname.append(table->db, table->db_length); + tname.append('\0'); + tname.append(table->table_name, table->table_name_length); + tname.append('\0'); + temp_table_key_length= tname.length(); + tname.append(table->alias); + tname.append('\0'); /* Upgrade the lock type because this table list will be used @@ -4102,9 +4107,10 @@ sp_head::merge_table_list(THD *thd, TABLE_LIST *table, LEX *lex_for_tmp_check) (and therefore should not be prelocked). Otherwise we will erroneously treat table with same name but with different alias as non-temporary. */ - if ((tab= (SP_TABLE*) my_hash_search(&m_sptabs, (uchar *)tname, tlen)) || - ((tab= (SP_TABLE*) my_hash_search(&m_sptabs, (uchar *)tname, - tlen - alen - 1)) && + if ((tab= (SP_TABLE*) my_hash_search(&m_sptabs, (uchar *)tname.ptr(), + tname.length())) || + ((tab= (SP_TABLE*) my_hash_search(&m_sptabs, (uchar *)tname.ptr(), + temp_table_key_length)) && tab->temp)) { if (tab->lock_type < table->lock_type) @@ -4123,11 +4129,11 @@ sp_head::merge_table_list(THD *thd, TABLE_LIST *table, LEX *lex_for_tmp_check) lex_for_tmp_check->create_info.options & HA_LEX_CREATE_TMP_TABLE) { tab->temp= TRUE; - tab->qname.length= tlen - alen - 1; + tab->qname.length= temp_table_key_length; } else - tab->qname.length= tlen; - tab->qname.str= (char*) thd->memdup(tname, tab->qname.length + 1); + tab->qname.length= tname.length(); + tab->qname.str= (char*) thd->memdup(tname.ptr(), tab->qname.length); if (!tab->qname.str) return FALSE; tab->table_name_length= table->table_name_length; @@ -4196,7 +4202,7 @@ sp_head::add_used_tables_to_table_list(THD *thd, if (!(tab_buff= (char *)thd->calloc(ALIGN_SIZE(sizeof(TABLE_LIST)) * stab->lock_count)) || !(key_buff= (char*)thd->memdup(stab->qname.str, - stab->qname.length + 1))) + stab->qname.length))) DBUG_RETURN(FALSE); for (uint j= 0; j < stab->lock_count; j++) diff --git a/sql/sp_pcontext.cc b/sql/sp_pcontext.cc index 4c5087eaf27..f11daeecb7b 100644 --- a/sql/sp_pcontext.cc +++ b/sql/sp_pcontext.cc @@ -61,20 +61,20 @@ sp_pcontext::sp_pcontext() m_label_scope(LABEL_DEFAULT_SCOPE) { (void) my_init_dynamic_array(&m_vars, sizeof(sp_variable_t *), - PCONTEXT_ARRAY_INIT_ALLOC, - PCONTEXT_ARRAY_INCREMENT_ALLOC); + PCONTEXT_ARRAY_INIT_ALLOC, + PCONTEXT_ARRAY_INCREMENT_ALLOC, MYF(0)); (void) my_init_dynamic_array(&m_case_expr_id_lst, sizeof(int), - PCONTEXT_ARRAY_INIT_ALLOC, - PCONTEXT_ARRAY_INCREMENT_ALLOC); + PCONTEXT_ARRAY_INIT_ALLOC, + PCONTEXT_ARRAY_INCREMENT_ALLOC, MYF(0)); (void) my_init_dynamic_array(&m_conds, sizeof(sp_cond_type_t *), - PCONTEXT_ARRAY_INIT_ALLOC, - PCONTEXT_ARRAY_INCREMENT_ALLOC); + PCONTEXT_ARRAY_INIT_ALLOC, + PCONTEXT_ARRAY_INCREMENT_ALLOC, MYF(0)); (void) my_init_dynamic_array(&m_cursors, sizeof(LEX_STRING), - PCONTEXT_ARRAY_INIT_ALLOC, - PCONTEXT_ARRAY_INCREMENT_ALLOC); + PCONTEXT_ARRAY_INIT_ALLOC, + PCONTEXT_ARRAY_INCREMENT_ALLOC, MYF(0)); (void) my_init_dynamic_array(&m_handlers, sizeof(sp_cond_type_t *), - PCONTEXT_ARRAY_INIT_ALLOC, - PCONTEXT_ARRAY_INCREMENT_ALLOC); + PCONTEXT_ARRAY_INIT_ALLOC, + PCONTEXT_ARRAY_INCREMENT_ALLOC, MYF(0)); m_label.empty(); m_children.empty(); @@ -89,20 +89,20 @@ sp_pcontext::sp_pcontext(sp_pcontext *prev, label_scope_type label_scope) m_label_scope(label_scope) { (void) my_init_dynamic_array(&m_vars, sizeof(sp_variable_t *), - PCONTEXT_ARRAY_INIT_ALLOC, - PCONTEXT_ARRAY_INCREMENT_ALLOC); + PCONTEXT_ARRAY_INIT_ALLOC, + PCONTEXT_ARRAY_INCREMENT_ALLOC, MYF(0)); (void) my_init_dynamic_array(&m_case_expr_id_lst, sizeof(int), - PCONTEXT_ARRAY_INIT_ALLOC, - PCONTEXT_ARRAY_INCREMENT_ALLOC); + PCONTEXT_ARRAY_INIT_ALLOC, + PCONTEXT_ARRAY_INCREMENT_ALLOC, MYF(0)); (void) my_init_dynamic_array(&m_conds, sizeof(sp_cond_type_t *), - PCONTEXT_ARRAY_INIT_ALLOC, - PCONTEXT_ARRAY_INCREMENT_ALLOC); + PCONTEXT_ARRAY_INIT_ALLOC, + PCONTEXT_ARRAY_INCREMENT_ALLOC, MYF(0)); (void) my_init_dynamic_array(&m_cursors, sizeof(LEX_STRING), - PCONTEXT_ARRAY_INIT_ALLOC, - PCONTEXT_ARRAY_INCREMENT_ALLOC); + PCONTEXT_ARRAY_INIT_ALLOC, + PCONTEXT_ARRAY_INCREMENT_ALLOC, MYF(0)); (void) my_init_dynamic_array(&m_handlers, sizeof(sp_cond_type_t *), - PCONTEXT_ARRAY_INIT_ALLOC, - PCONTEXT_ARRAY_INCREMENT_ALLOC); + PCONTEXT_ARRAY_INIT_ALLOC, + PCONTEXT_ARRAY_INCREMENT_ALLOC, MYF(0)); m_label.empty(); m_children.empty(); diff --git a/sql/sp_rcontext.cc b/sql/sp_rcontext.cc index 6fe4be989db..30acfebabb2 100644 --- a/sql/sp_rcontext.cc +++ b/sql/sp_rcontext.cc @@ -67,19 +67,15 @@ sp_rcontext::~sp_rcontext() bool sp_rcontext::init(THD *thd) { uint handler_count= m_root_parsing_ctx->max_handler_index(); - uint i; in_sub_stmt= thd->in_sub_stmt; if (init_var_table(thd) || init_var_items()) return TRUE; - if (!(m_raised_conditions= new (thd->mem_root) MYSQL_ERROR[handler_count])) + if (!(m_raised_conditions= new (thd->mem_root) Sql_condition_info[handler_count])) return TRUE; - for (i= 0; i<handler_count; i++) - m_raised_conditions[i].init(thd->mem_root); - return !(m_handler= (sp_handler_t*)thd->alloc(handler_count * sizeof(sp_handler_t))) || @@ -419,7 +415,7 @@ sp_rcontext::activate_handler(THD *thd, /* Reset error state. */ thd->clear_error(); - thd->killed= NOT_KILLED; // Some errors set thd->killed + thd->reset_killed(); // Some errors set thd->killed // (e.g. "bad data"). /* Return IP of the activated SQL handler. */ @@ -446,13 +442,12 @@ sp_rcontext::exit_handler() DBUG_VOID_RETURN; } -MYSQL_ERROR* -sp_rcontext::raised_condition() const +Sql_condition_info* sp_rcontext::raised_condition() const { if (m_ihsp > 0) { uint hindex= m_in_handler[m_ihsp - 1].index; - MYSQL_ERROR *raised= & m_raised_conditions[hindex]; + Sql_condition_info *raised= & m_raised_conditions[hindex]; return raised; } diff --git a/sql/sp_rcontext.h b/sql/sp_rcontext.h index 3d976f94381..5008a73d96c 100644 --- a/sql/sp_rcontext.h +++ b/sql/sp_rcontext.h @@ -58,6 +58,46 @@ typedef struct uint index; } sp_active_handler_t; + +class Sql_condition_info : public Sql_alloc +{ +public: + /** SQL error code. */ + uint m_sql_errno; + + /** Error level. */ + MYSQL_ERROR::enum_warning_level m_level; + + /** SQLSTATE. */ + char m_sql_state[SQLSTATE_LENGTH + 1]; + + /** Text message. */ + char m_message[MYSQL_ERRMSG_SIZE]; + + void set(uint sql_errno, const char* sqlstate, + MYSQL_ERROR::enum_warning_level level, + const char* msg) + { + m_sql_errno= sql_errno; + m_level= level; + + memcpy(m_sql_state, sqlstate, SQLSTATE_LENGTH); + m_sql_state[SQLSTATE_LENGTH]= '\0'; + + strncpy(m_message, msg, MYSQL_ERRMSG_SIZE); + } + + void clear() + { + m_sql_errno= 0; + m_level= MYSQL_ERROR::WARN_LEVEL_ERROR; + + m_sql_state[0]= '\0'; + m_message[0]= '\0'; + } +}; + + /* This class is a runtime context of a Stored Routine. It is used in an execution and is intended to contain all dynamic objects (i.e. objects, which @@ -146,8 +186,7 @@ class sp_rcontext : public Sql_alloc MYSQL_ERROR::enum_warning_level level, const char *msg); - MYSQL_ERROR * - raised_condition() const; + Sql_condition_info *raised_condition() const; void push_hstack(uint h); @@ -232,7 +271,7 @@ private: SQL conditions caught by each handler. This is an array indexed by handler index. */ - MYSQL_ERROR *m_raised_conditions; + Sql_condition_info *m_raised_conditions; uint m_hcount; // Stack pointer for m_handler uint *m_hstack; // Return stack for continue handlers diff --git a/sql/sql_acl.cc b/sql/sql_acl.cc index 723d825de17..cb7e35fae09 100644 --- a/sql/sql_acl.cc +++ b/sql/sql_acl.cc @@ -1,5 +1,5 @@ /* Copyright (c) 2000, 2011, Oracle and/or its affiliates. - Copyright (c) 2009, 2011, Monty Program Ab + Copyright (c) 2009, 2013, Monty Program Ab 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 @@ -50,6 +50,7 @@ #include "sql_connect.h" #include "hostname.h" #include "sql_db.h" +#include "sql_array.h" bool mysql_user_table_is_in_short_password_format= false; @@ -560,6 +561,18 @@ static bool update_user_table(THD *thd, TABLE *table, const char *host, 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); +/* + Enumeration of various ACL's and Hashes used in handle_grant_struct() +*/ +enum enum_acl_lists +{ + USER_ACL= 0, + DB_ACL, + COLUMN_PRIVILEGES_HASH, + PROC_PRIVILEGES_HASH, + FUNC_PRIVILEGES_HASH, + PROXY_USERS_ACL +}; /* Convert scrambled password to binary form, according to scramble type, @@ -688,7 +701,7 @@ my_bool acl_init(bool dont_read_acl_tables) return_val= acl_reload(thd); delete thd; /* Remember that we don't have a THD */ - my_pthread_setspecific_ptr(THR_THD, 0); + set_current_thd(0); DBUG_RETURN(return_val); } @@ -749,8 +762,8 @@ static my_bool acl_load(THD *thd, TABLE_LIST *tables) acl_cache->clear(1); // Clear locked hostname cache - init_sql_alloc(&mem, ACL_ALLOC_BLOCK_SIZE, 0); - (void) my_init_dynamic_array(&acl_hosts,sizeof(ACL_HOST),20,50); + init_sql_alloc(&mem, ACL_ALLOC_BLOCK_SIZE, 0, MYF(0)); + (void) my_init_dynamic_array(&acl_hosts,sizeof(ACL_HOST), 20, 50, MYF(0)); if (tables[0].table) // "host" table may not exist (e.g. in MySQL 5.6.7+) { if (init_read_record(&read_record_info, thd, table= tables[0].table, @@ -768,7 +781,12 @@ static my_bool acl_load(THD *thd, TABLE_LIST *tables) convert db to lower case and give a warning if the db wasn't already in lower case */ - (void) strmov(tmp_name, host.db); + char *end = strnmov(tmp_name, host.db, sizeof(tmp_name)); + if (end >= tmp_name + sizeof(tmp_name)) + { + sql_print_warning(ER(ER_WRONG_DB_NAME), host.db); + continue; + } my_casedn_str(files_charset_info, host.db); if (strcmp(host.db, tmp_name) != 0) sql_print_warning("'host' entry '%s|%s' had database in mixed " @@ -808,7 +826,7 @@ static my_bool acl_load(THD *thd, TABLE_LIST *tables) NULL, 1, 1, FALSE)) goto end; table->use_all_columns(); - (void) my_init_dynamic_array(&acl_users,sizeof(ACL_USER),50,100); + (void) my_init_dynamic_array(&acl_users,sizeof(ACL_USER), 50, 100, MYF(0)); password_length= table->field[2]->field_length / table->field[2]->charset()->mbmaxlen; if (password_length < SCRAMBLED_PASSWORD_CHAR_LENGTH_323) @@ -1009,7 +1027,7 @@ static my_bool acl_load(THD *thd, TABLE_LIST *tables) NULL, 1, 1, FALSE)) goto end; table->use_all_columns(); - (void) my_init_dynamic_array(&acl_dbs,sizeof(ACL_DB),50,100); + (void) my_init_dynamic_array(&acl_dbs,sizeof(ACL_DB), 50, 100, MYF(0)); while (!(read_record_info.read_record(&read_record_info))) { ACL_DB db; @@ -1038,7 +1056,12 @@ static my_bool acl_load(THD *thd, TABLE_LIST *tables) convert db to lower case and give a warning if the db wasn't already in lower case */ - (void)strmov(tmp_name, db.db); + char *end = strnmov(tmp_name, db.db, sizeof(tmp_name)); + if (end >= tmp_name + sizeof(tmp_name)) + { + sql_print_warning(ER(ER_WRONG_DB_NAME), db.db); + continue; + } my_casedn_str(files_charset_info, db.db); if (strcmp(db.db, tmp_name) != 0) { @@ -1067,7 +1090,7 @@ static my_bool acl_load(THD *thd, TABLE_LIST *tables) freeze_size(&acl_dbs); (void) my_init_dynamic_array(&acl_proxy_users, sizeof(ACL_PROXY_USER), - 50, 100); + 50, 100, MYF(0)); if (tables[3].table) { if (init_read_record(&read_record_info, thd, table= tables[3].table, @@ -1723,7 +1746,7 @@ static void init_check_host(void) { DBUG_ENTER("init_check_host"); (void) my_init_dynamic_array(&acl_wild_hosts,sizeof(struct acl_host_and_ip), - acl_users.elements,1); + acl_users.elements, 1, MYF(0)); (void) my_hash_init(&acl_check_hosts,system_charset_info, acl_users.elements, 0, 0, (my_hash_get_key) check_get_key, 0, 0); @@ -2699,7 +2722,13 @@ replace_proxies_priv_table(THD *thd, TABLE *table, const LEX_USER *user, get_grantor(thd, grantor); - table->file->ha_index_init(0, 1); + if ((error= table->file->ha_index_init(0, 1))) + { + table->file->print_error(error, MYF(0)); + DBUG_PRINT("info", ("ha_index_init error")); + DBUG_RETURN(-1); + } + if (table->file->ha_index_read_map(table->record[0], user_key, HA_WHOLE_KEY, HA_READ_KEY_EXACT)) @@ -2941,7 +2970,12 @@ GRANT_TABLE::GRANT_TABLE(TABLE *form, TABLE *col_privs) key_copy(key, col_privs->record[0], col_privs->key_info, key_prefix_len); col_privs->field[4]->store("",0, &my_charset_latin1); - col_privs->file->ha_index_init(0, 1); + if (col_privs->file->ha_index_init(0, 1)) + { + cols= 0; + return; + } + if (col_privs->file->ha_index_read_map(col_privs->record[0], (uchar*) key, (key_part_map)15, HA_READ_KEY_EXACT)) @@ -3005,15 +3039,23 @@ static GRANT_NAME *name_hash_search(HASH *name_hash, const char *user, const char *tname, bool exact, bool name_tolower) { - char helping [SAFE_NAME_LEN*2+USERNAME_LENGTH+3], *name_ptr; + char helping[SAFE_NAME_LEN*2+USERNAME_LENGTH+3]; + char *hend = helping + sizeof(helping); uint len; GRANT_NAME *grant_name,*found=0; HASH_SEARCH_STATE state; - name_ptr= strmov(strmov(helping, user) + 1, db) + 1; - len = (uint) (strmov(name_ptr, tname) - helping) + 1; + char *db_ptr= strmov(helping, user) + 1; + char *tname_ptr= strnmov(db_ptr, db, hend - db_ptr) + 1; + if (tname_ptr > hend) + return 0; // invalid name = not found + char *end= strnmov(tname_ptr, tname, hend - tname_ptr) + 1; + if (end > hend) + return 0; // invalid name = not found + + len = (uint) (end - helping); if (name_tolower) - my_casedn_str(files_charset_info, name_ptr); + my_casedn_str(files_charset_info, tname_ptr); for (grant_name= (GRANT_NAME*) my_hash_first(name_hash, (uchar*) helping, len, &state); grant_name ; @@ -3073,7 +3115,7 @@ static int replace_column_table(GRANT_TABLE *g_t, const char *db, const char *table_name, ulong rights, bool revoke_grant) { - int error=0,result=0; + int result=0; uchar key[MAX_KEY_LENGTH]; uint key_prefix_length; KEY_PART_INFO *key_part= table->key_info->key_part; @@ -3100,7 +3142,13 @@ static int replace_column_table(GRANT_TABLE *g_t, List_iterator <LEX_COLUMN> iter(columns); class LEX_COLUMN *column; - table->file->ha_index_init(0, 1); + int error= table->file->ha_index_init(0, 1); + if (error) + { + table->file->print_error(error, MYF(0)); + DBUG_RETURN(-1); + } + while ((column= iter++)) { ulong privileges= column->rights; @@ -3970,7 +4018,12 @@ bool mysql_grant(THD *thd, const char *db, List <LEX_USER> &list, if (lower_case_table_names && db) { - strmov(tmp_db,db); + char *end= strnmov(tmp_db,db, sizeof(tmp_db)); + if (end >= tmp_db + sizeof(tmp_db)) + { + my_error(ER_WRONG_DB_NAME ,MYF(0), db); + DBUG_RETURN(TRUE); + } my_casedn_str(files_charset_info, tmp_db); db=tmp_db; } @@ -4123,7 +4176,7 @@ my_bool grant_init() return_val= grant_reload(thd); delete thd; /* Remember that we don't have a THD */ - my_pthread_setspecific_ptr(THR_THD, 0); + set_current_thd(0); DBUG_RETURN(return_val); } @@ -4157,7 +4210,10 @@ static my_bool grant_load_procs_priv(TABLE *p_table) (void) my_hash_init(&func_priv_hash, &my_charset_utf8_bin, 0,0,0, (my_hash_get_key) get_grant_table, 0,0); - p_table->file->ha_index_init(0, 1); + + if (p_table->file->ha_index_init(0, 1)) + DBUG_RETURN(TRUE); + p_table->use_all_columns(); if (!p_table->file->ha_index_first(p_table->record[0])) @@ -4258,7 +4314,10 @@ static my_bool grant_load(THD *thd, TABLE_LIST *tables) t_table = tables[0].table; c_table = tables[1].table; - t_table->file->ha_index_init(0, 1); + + if (t_table->file->ha_index_init(0, 1)) + goto end_index_init; + t_table->use_all_columns(); c_table->use_all_columns(); @@ -4303,9 +4362,10 @@ static my_bool grant_load(THD *thd, TABLE_LIST *tables) return_val=0; // Return ok end_unlock: - thd->variables.sql_mode= old_sql_mode; t_table->file->ha_index_end(); my_pthread_setspecific_ptr(THR_MALLOC, save_mem_root_ptr); +end_index_init: + thd->variables.sql_mode= old_sql_mode; DBUG_RETURN(return_val); } @@ -4414,7 +4474,7 @@ my_bool grant_reload(THD *thd) opertion possible in case of failure. */ old_mem= memex; - init_sql_alloc(&memex, ACL_ALLOC_BLOCK_SIZE, 0); + init_sql_alloc(&memex, ACL_ALLOC_BLOCK_SIZE, 0, MYF(0)); if ((return_val= grant_load(thd, tables))) { // Error. Revert to old hash @@ -5994,20 +6054,19 @@ static int handle_grant_table(TABLE_LIST *tables, uint table_no, bool drop, Delete from grant structure if drop is true. Update in grant structure if drop is false and user_to is not NULL. Search in grant structure if drop is false and user_to is NULL. - Structures are numbered as follows: - 0 acl_users - 1 acl_dbs - 2 column_priv_hash - 3 proc_priv_hash - 4 func_priv_hash - 5 acl_proxy_users + Structures are enumerated as follows: + 0 ACL_USER + 1 ACL_DB + 2 COLUMN_PRIVILEGES_HASH + 3 PROC_PRIVILEGES_HASH + 4 FUNC_PRIVILEGES_HASH + 5 PROXY_USERS_ACL @retval > 0 At least one element matched. @retval 0 OK, but no element matched. - @retval -1 Wrong arguments to function. */ -static int handle_grant_struct(uint struct_no, bool drop, +static int handle_grant_struct(enum enum_acl_lists struct_no, bool drop, LEX_USER *user_from, LEX_USER *user_to) { int result= 0; @@ -6031,28 +6090,29 @@ static int handle_grant_struct(uint struct_no, bool drop, /* Get the number of elements in the in-memory structure. */ switch (struct_no) { - case 0: + case USER_ACL: elements= acl_users.elements; break; - case 1: + case DB_ACL: elements= acl_dbs.elements; break; - case 2: + case COLUMN_PRIVILEGES_HASH: grant_name_hash= &column_priv_hash; elements= grant_name_hash->records; break; - case 3: + case PROC_PRIVILEGES_HASH: grant_name_hash= &proc_priv_hash; elements= grant_name_hash->records; break; - case 4: + case FUNC_PRIVILEGES_HASH: grant_name_hash= &func_priv_hash; elements= grant_name_hash->records; break; - case 5: + case PROXY_USERS_ACL: elements= acl_proxy_users.elements; break; default: + DBUG_ASSERT(0); return -1; } @@ -6067,27 +6127,27 @@ static int handle_grant_struct(uint struct_no, bool drop, Get a pointer to the element. */ switch (struct_no) { - case 0: + case USER_ACL: acl_user= dynamic_element(&acl_users, idx, ACL_USER*); user= acl_user->user; host= acl_user->host.hostname; break; - case 1: + case DB_ACL: acl_db= dynamic_element(&acl_dbs, idx, ACL_DB*); user= acl_db->user; host= acl_db->host.hostname; break; - case 2: - case 3: - case 4: + case COLUMN_PRIVILEGES_HASH: + case PROC_PRIVILEGES_HASH: + case FUNC_PRIVILEGES_HASH: grant_name= (GRANT_NAME*) my_hash_element(grant_name_hash, idx); user= grant_name->user; host= grant_name->host.hostname; break; - case 5: + case PROXY_USERS_ACL: acl_proxy_user= dynamic_element(&acl_proxy_users, idx, ACL_PROXY_USER*); user= acl_proxy_user->get_user(); host= acl_proxy_user->get_host(); @@ -6113,21 +6173,21 @@ static int handle_grant_struct(uint struct_no, bool drop, if ( drop ) { switch ( struct_no ) { - case 0: + case USER_ACL: delete_dynamic_element(&acl_users, idx); break; - case 1: + case DB_ACL: delete_dynamic_element(&acl_dbs, idx); break; - case 2: - case 3: - case 4: + case COLUMN_PRIVILEGES_HASH: + case PROC_PRIVILEGES_HASH: + case FUNC_PRIVILEGES_HASH: my_hash_delete(grant_name_hash, (uchar*) grant_name); break; - case 5: + case PROXY_USERS_ACL: delete_dynamic_element(&acl_proxy_users, idx); break; @@ -6151,19 +6211,19 @@ static int handle_grant_struct(uint struct_no, bool drop, else if ( user_to ) { switch ( struct_no ) { - case 0: + case USER_ACL: acl_user->user= strdup_root(&mem, user_to->user.str); acl_user->host.hostname= strdup_root(&mem, user_to->host.str); break; - case 1: + case DB_ACL: acl_db->user= strdup_root(&mem, user_to->user.str); acl_db->host.hostname= strdup_root(&mem, user_to->host.str); break; - case 2: - case 3: - case 4: + case COLUMN_PRIVILEGES_HASH: + case PROC_PRIVILEGES_HASH: + case FUNC_PRIVILEGES_HASH: { /* Save old hash key and its length to be able properly update @@ -6199,11 +6259,10 @@ static int handle_grant_struct(uint struct_no, bool drop, break; } - case 5: + case PROXY_USERS_ACL: acl_proxy_user->set_user (&mem, user_to->user.str); acl_proxy_user->set_host (&mem, user_to->host.str); break; - } } else @@ -6260,7 +6319,7 @@ static int handle_grant_data(TABLE_LIST *tables, bool drop, else { /* Handle user array. */ - if ((handle_grant_struct(0, drop, user_from, user_to)) || found) + if ((handle_grant_struct(USER_ACL, drop, user_from, user_to)) || found) { result= 1; /* At least one record/element found. */ /* If search is requested, we do not need to search further. */ @@ -6278,7 +6337,7 @@ static int handle_grant_data(TABLE_LIST *tables, bool drop, else { /* Handle db array. */ - if (((handle_grant_struct(1, drop, user_from, user_to) && ! result) || + if (((handle_grant_struct(DB_ACL, drop, user_from, user_to) && ! result) || found) && ! result) { result= 1; /* At least one record/element found. */ @@ -6297,7 +6356,7 @@ static int handle_grant_data(TABLE_LIST *tables, bool drop, else { /* Handle procs array. */ - if (((handle_grant_struct(3, drop, user_from, user_to) && ! result) || + if (((handle_grant_struct(PROC_PRIVILEGES_HASH, drop, user_from, user_to) && ! result) || found) && ! result) { result= 1; /* At least one record/element found. */ @@ -6306,7 +6365,7 @@ static int handle_grant_data(TABLE_LIST *tables, bool drop, goto end; } /* Handle funcs array. */ - if (((handle_grant_struct(4, drop, user_from, user_to) && ! result) || + if (((handle_grant_struct(FUNC_PRIVILEGES_HASH, drop, user_from, user_to) && ! result) || found) && ! result) { result= 1; /* At least one record/element found. */ @@ -6341,7 +6400,7 @@ static int handle_grant_data(TABLE_LIST *tables, bool drop, else { /* Handle columns hash. */ - if (((handle_grant_struct(2, drop, user_from, user_to) && ! result) || + if (((handle_grant_struct(COLUMN_PRIVILEGES_HASH, drop, user_from, user_to) && ! result) || found) && ! result) result= 1; /* At least one record/element found. */ } @@ -6358,7 +6417,7 @@ static int handle_grant_data(TABLE_LIST *tables, bool drop, else { /* Handle proxies_priv array. */ - if ((handle_grant_struct(5, drop, user_from, user_to) && !result) || + if ((handle_grant_struct(PROXY_USERS_ACL, drop, user_from, user_to) && !result) || found) result= 1; /* At least one record/element found. */ } @@ -7086,14 +7145,25 @@ acl_check_proxy_grant_access(THD *thd, const char *host, const char *user, DBUG_RETURN(FALSE); } - /* one can grant proxy to himself to others */ - if (!strcmp(thd->security_ctx->user, user) && + /* + one can grant proxy for self to others. + Security context in THD contains two pairs of (user,host): + 1. (user,host) pair referring to inbound connection. + 2. (priv_user,priv_host) pair obtained from mysql.user table after doing + authnetication of incoming connection. + Privileges should be checked wrt (priv_user, priv_host) tuple, because + (user,host) pair obtained from inbound connection may have different + values than what is actually stored in mysql.user table and while granting + or revoking proxy privilege, user is expected to provide entries mentioned + in mysql.user table. + */ + if (!strcmp(thd->security_ctx->priv_user, user) && !my_strcasecmp(system_charset_info, host, - thd->security_ctx->host)) + thd->security_ctx->priv_host)) { DBUG_PRINT("info", ("strcmp (%s, %s) my_casestrcmp (%s, %s) equal", - thd->security_ctx->user, user, - host, thd->security_ctx->host)); + thd->security_ctx->priv_user, user, + host, thd->security_ctx->priv_host)); DBUG_RETURN(FALSE); } @@ -7759,6 +7829,7 @@ struct MPVIO_EXT :public MYSQL_PLUGIN_VIO } cached_server_packet; int packets_read, packets_written; ///< counters for send/received packets uint connect_errors; ///< if there were connect errors for this host + bool make_it_fail; /** when plugin returns a failure this tells us what really happened */ enum { SUCCESS, FAILURE, RESTART } status; }; @@ -8024,14 +8095,14 @@ static bool send_plugin_request_packet(MPVIO_EXT *mpvio, /** Finds acl entry in user database for authentication purposes. - Finds a user and copies it into mpvio. Reports an authentication - failure if a user is not found. + Finds a user and copies it into mpvio. Creates a fake user + if no matching user account is found. @note find_acl_user is not the same, because it doesn't take into account the case when user is not empty, but acl_user->user is empty @retval 0 found - @retval 1 not found + @retval 1 error */ static bool find_mpvio_user(MPVIO_EXT *mpvio) { @@ -8054,8 +8125,27 @@ static bool find_mpvio_user(MPVIO_EXT *mpvio) if (!mpvio->acl_user) { - login_failed_error(mpvio->thd); - DBUG_RETURN (1); + /* + A matching user was not found. Fake it. Take any user, make the + authentication fail later. + This way we get a realistically looking failure, with occasional + "change auth plugin" requests even for nonexistent users. The ratio + of "change auth plugin" request will be the same for real and + nonexistent users. + Note, that we cannot pick any user at random, it must always be + the same user account for the incoming sctx->user name. + */ + ulong nr1=1, nr2=4; + CHARSET_INFO *cs= &my_charset_latin1; + cs->coll->hash_sort(cs, (uchar*) sctx->user, strlen(sctx->user), &nr1, &nr2); + + mysql_mutex_lock(&acl_cache->lock); + uint i= nr1 % acl_users.elements; + ACL_USER *acl_user_tmp= dynamic_element(&acl_users, i, ACL_USER*); + mpvio->acl_user= acl_user_tmp->copy(mpvio->thd->mem_root); + mysql_mutex_unlock(&acl_cache->lock); + + mpvio->make_it_fail= true; } /* user account requires non-default plugin and the client is too old */ @@ -8182,6 +8272,7 @@ static bool parse_com_change_user_packet(MPVIO_EXT *mpvio, uint packet_length) } #ifndef NO_EMBEDDED_ACCESS_CHECKS + thd->password= passwd_len > 0; if (find_mpvio_user(mpvio)) DBUG_RETURN(1); @@ -8477,8 +8568,8 @@ static ulong parse_client_handshake_packet(MPVIO_EXT *mpvio, mpvio->cached_server_packet.pkt_len)) return packet_error; - passwd_len= my_net_read(&mpvio->thd->net); - passwd= (char*)mpvio->thd->net.read_pos; + passwd_len= my_net_read(&thd->net); + passwd= (char*)thd->net.read_pos; } *buff= (uchar*) passwd; @@ -8580,6 +8671,10 @@ static int server_mpvio_read_packet(MYSQL_PLUGIN_VIO *param, uchar **buf) *buf= (uchar*) mpvio->cached_client_reply.pkt; mpvio->cached_client_reply.pkt= 0; mpvio->packets_read++; + + if (mpvio->make_it_fail) + goto err; + DBUG_RETURN ((int) mpvio->cached_client_reply.pkt_len); } @@ -8614,6 +8709,9 @@ static int server_mpvio_read_packet(MYSQL_PLUGIN_VIO *param, uchar **buf) else *buf= mpvio->thd->net.read_pos; + if (mpvio->make_it_fail) + goto err; + DBUG_RETURN((int)pkt_len); err: @@ -8621,7 +8719,12 @@ err: { inc_host_errors(mpvio->thd->security_ctx->ip); if (!mpvio->thd->is_error()) - my_error(ER_HANDSHAKE_ERROR, MYF(0)); + { + if (mpvio->make_it_fail) + login_failed_error(mpvio->thd); + else + my_error(ER_HANDSHAKE_ERROR, MYF(0)); + } } DBUG_RETURN(-1); } @@ -8826,6 +8929,7 @@ bool acl_authenticate(THD *thd, uint connect_errors, mpvio.thd= thd; mpvio.connect_errors= connect_errors; mpvio.status= MPVIO_EXT::FAILURE; + mpvio.make_it_fail= false; mpvio.auth_info.host_or_ip= thd->security_ctx->host_or_ip; mpvio.auth_info.host_or_ip_length= (unsigned int) strlen(thd->security_ctx->host_or_ip); diff --git a/sql/sql_analyse.h b/sql/sql_analyse.h index 34c8e2da3d4..8bac29de5a3 100644 --- a/sql/sql_analyse.h +++ b/sql/sql_analyse.h @@ -121,7 +121,8 @@ public: must_be_blob(0), was_zero_fill(0), was_maybe_zerofill(0), can_be_still_num(1) { init_tree(&tree, 0, 0, sizeof(String), (qsort_cmp2) sortcmp2, - 0, (tree_element_free) free_string, NULL); }; + (tree_element_free) free_string, NULL, + MYF(MY_THREAD_SPECIFIC)); }; void add(); void get_opt_type(String*, ha_rows); @@ -162,7 +163,7 @@ public: { bin_size= my_decimal_get_binary_size(a->max_length, a->decimals); init_tree(&tree, 0, 0, bin_size, (qsort_cmp2)compare_decimal2, - 0, 0, (void *)&bin_size); + 0, (void *)&bin_size, MYF(MY_THREAD_SPECIFIC)); }; void add(); @@ -190,7 +191,8 @@ public: field_real(Item* a, analyse* b) :field_info(a,b), min_arg(0), max_arg(0), sum(0), sum_sqr(0), max_notzero_dec_len(0) { init_tree(&tree, 0, 0, sizeof(double), - (qsort_cmp2) compare_double2, 0, NULL, NULL); } + (qsort_cmp2) compare_double2, NULL, NULL, + MYF(MY_THREAD_SPECIFIC)); } void add(); void get_opt_type(String*, ha_rows); @@ -244,7 +246,8 @@ public: field_longlong(Item* a, analyse* b) :field_info(a,b), min_arg(0), max_arg(0), sum(0), sum_sqr(0) { init_tree(&tree, 0, 0, sizeof(longlong), - (qsort_cmp2) compare_longlong2, 0, NULL, NULL); } + (qsort_cmp2) compare_longlong2, NULL, NULL, + MYF(MY_THREAD_SPECIFIC)); } void add(); void get_opt_type(String*, ha_rows); @@ -289,7 +292,8 @@ public: field_ulonglong(Item* a, analyse * b) :field_info(a,b), min_arg(0), max_arg(0), sum(0),sum_sqr(0) { init_tree(&tree, 0, 0, sizeof(ulonglong), - (qsort_cmp2) compare_ulonglong2, 0, NULL, NULL); } + (qsort_cmp2) compare_ulonglong2, NULL, NULL, + MYF(MY_THREAD_SPECIFIC)); } void add(); void get_opt_type(String*, ha_rows); String *get_min_arg(String *s) { s->set(min_arg,my_thd_charset); return s; } diff --git a/sql/sql_array.h b/sql/sql_array.h index 98a4a4815af..f07126bc0ef 100644 --- a/sql/sql_array.h +++ b/sql/sql_array.h @@ -100,7 +100,8 @@ template <class Elem> class Dynamic_array public: Dynamic_array(uint prealloc=16, uint increment=16) { - my_init_dynamic_array(&array, sizeof(Elem), prealloc, increment); + my_init_dynamic_array(&array, sizeof(Elem), prealloc, increment, + MYF(MY_THREAD_SPECIFIC)); } Elem& at(int idx) diff --git a/sql/sql_audit.cc b/sql/sql_audit.cc index 131a71c1d6b..793eead9869 100644 --- a/sql/sql_audit.cc +++ b/sql/sql_audit.cc @@ -132,12 +132,9 @@ static const uint audit_handlers_count= static my_bool acquire_plugins(THD *thd, plugin_ref plugin, void *arg) { - uint event_class= *(uint*) arg; - unsigned long event_class_mask[MYSQL_AUDIT_CLASS_MASK_SIZE]; + ulong *event_class_mask= (ulong*) arg; st_mysql_audit *data= plugin_data(plugin, struct st_mysql_audit *); - set_audit_mask(event_class_mask, event_class); - /* Check if this plugin is interested in the event */ if (check_audit_mask(data->class_mask, event_class_mask)) return 0; @@ -156,7 +153,7 @@ static my_bool acquire_plugins(THD *thd, plugin_ref plugin, void *arg) { /* specify some reasonable initialization defaults */ my_init_dynamic_array(&thd->audit_class_plugins, - sizeof(plugin_ref), 16, 16); + sizeof(plugin_ref), 16, 16, MYF(0)); } /* lock the plugin and add it to the list */ @@ -176,15 +173,13 @@ static my_bool acquire_plugins(THD *thd, plugin_ref plugin, void *arg) @details Ensure that audit plugins interested in given event class are locked by current thread. */ -void mysql_audit_acquire_plugins(THD *thd, uint event_class) +void mysql_audit_acquire_plugins(THD *thd, ulong *event_class_mask) { - unsigned long event_class_mask[MYSQL_AUDIT_CLASS_MASK_SIZE]; DBUG_ENTER("mysql_audit_acquire_plugins"); - set_audit_mask(event_class_mask, event_class); if (thd && !check_audit_mask(mysql_global_audit_mask, event_class_mask) && check_audit_mask(thd->audit_class_mask, event_class_mask)) { - plugin_foreach(thd, acquire_plugins, MYSQL_AUDIT_PLUGIN, &event_class); + plugin_foreach(thd, acquire_plugins, MYSQL_AUDIT_PLUGIN, event_class_mask); add_audit_mask(thd->audit_class_mask, event_class_mask); } DBUG_VOID_RETURN; @@ -206,7 +201,9 @@ void mysql_audit_notify(THD *thd, uint event_class, uint event_subtype, ...) va_list ap; audit_handler_t *handlers= audit_handlers + event_class; DBUG_ASSERT(event_class < audit_handlers_count); - mysql_audit_acquire_plugins(thd, event_class); + unsigned long event_class_mask[MYSQL_AUDIT_CLASS_MASK_SIZE]; + set_audit_mask(event_class_mask, event_class); + mysql_audit_acquire_plugins(thd, event_class_mask); va_start(ap, event_subtype); (*handlers)(thd, event_subtype, ap); va_end(ap); @@ -364,6 +361,34 @@ int initialize_audit_plugin(st_plugin_int *plugin) add_audit_mask(mysql_global_audit_mask, data->class_mask); mysql_mutex_unlock(&LOCK_audit_mask); + /* + Pre-acquire the newly inslalled audit plugin for events that + may potentially occur further during INSTALL PLUGIN. + + When audit event is triggered, audit subsystem acquires interested + plugins by walking through plugin list. Evidently plugin list + iterator protects plugin list by acquiring LOCK_plugin, see + plugin_foreach_with_mask(). + + On the other hand [UN]INSTALL PLUGIN is acquiring LOCK_plugin + rather for a long time. + + When audit event is triggered during [UN]INSTALL PLUGIN, plugin + list iterator acquires the same lock (within the same thread) + second time. + + This hack should be removed when LOCK_plugin is fixed so it + protects only what it supposed to protect. + + See also mysql_install_plugin() and mysql_uninstall_plugin() + */ + THD *thd= current_thd; + if (thd) + { + acquire_plugins(thd, plugin_int_to_ref(plugin), data->class_mask); + add_audit_mask(thd->audit_class_mask, data->class_mask); + } + return 0; } @@ -494,7 +519,7 @@ static void event_class_dispatch(THD *thd, unsigned int event_class, #else /* EMBEDDED_LIBRARY */ -void mysql_audit_acquire_plugins(THD *thd, uint event_class) +void mysql_audit_acquire_plugins(THD *thd, ulong *event_class_mask) { } diff --git a/sql/sql_audit.h b/sql/sql_audit.h index b2ce31f1d26..46afe4b7596 100644 --- a/sql/sql_audit.h +++ b/sql/sql_audit.h @@ -31,7 +31,7 @@ extern void mysql_audit_finalize(); extern void mysql_audit_init_thd(THD *thd); extern void mysql_audit_free_thd(THD *thd); -extern void mysql_audit_acquire_plugins(THD *thd, uint event_class); +extern void mysql_audit_acquire_plugins(THD *thd, ulong *event_class_mask); #ifndef EMBEDDED_LIBRARY diff --git a/sql/sql_base.cc b/sql/sql_base.cc index ec0438946c6..969e2f2c7e4 100644 --- a/sql/sql_base.cc +++ b/sql/sql_base.cc @@ -4700,6 +4700,7 @@ open_and_process_table(THD *thd, LEX *lex, TABLE_LIST *tables, Field **table_field_ptr= tables->table->field; for ( ; *field_ptr; field_ptr++, table_field_ptr++) (*table_field_ptr)->read_stats= (*field_ptr)->read_stats; + tables->table->stats_is_read= table_share->stats_cb.stats_is_read; } } } @@ -5022,7 +5023,7 @@ bool open_tables(THD *thd, TABLE_LIST **start, uint *counter, uint flags, anything yet, to avoid penalty for statements which don't use views and thus new .FRM format. */ - init_sql_alloc(&new_frm_mem, 8024, 0); + init_sql_alloc(&new_frm_mem, 8024, 0, MYF(0)); thd->current_tablenr= 0; restart: @@ -9033,7 +9034,8 @@ fill_record(THD * thd, TABLE *table_arg, List<Item> &fields, List<Item> &values, ER(ER_WARNING_NON_DEFAULT_VALUE_FOR_VIRTUAL_COLUMN), rfield->field_name, table->s->table_name.str); } - if ((value->save_in_field(rfield, 0) < 0) && !ignore_errors) + if ((!rfield->vcol_info || rfield->stored_in_db) && + (value->save_in_field(rfield, 0)) < 0 && !ignore_errors) { my_message(ER_UNKNOWN_ERROR, ER(ER_UNKNOWN_ERROR), MYF(0)); goto err; @@ -9325,7 +9327,7 @@ my_bool mysql_rm_tmp_tables(void) my_dirend(dirp); } delete thd; - my_pthread_setspecific_ptr(THR_THD, 0); + set_current_thd(0); DBUG_RETURN(0); } @@ -9389,7 +9391,11 @@ bool mysql_notify_thread_having_shared_lock(THD *thd, THD *in_use, in_use->killed= KILL_SYSTEM_THREAD; mysql_mutex_lock(&in_use->mysys_var->mutex); if (in_use->mysys_var->current_cond) + { + mysql_mutex_lock(in_use->mysys_var->current_mutex); mysql_cond_broadcast(in_use->mysys_var->current_cond); + mysql_mutex_unlock(in_use->mysys_var->current_mutex); + } mysql_mutex_unlock(&in_use->mysys_var->mutex); signalled= TRUE; } @@ -9949,6 +9955,7 @@ int dynamic_column_error_message(enum_dyncol_func_result rc) switch (rc) { case ER_DYNCOL_YES: case ER_DYNCOL_OK: + case ER_DYNCOL_TRUNCATED: break; // it is not an error case ER_DYNCOL_FORMAT: my_error(ER_DYN_COL_WRONG_FORMAT, MYF(0)); diff --git a/sql/sql_base.h b/sql/sql_base.h index 5bbccc87cdc..78ab8c7df24 100644 --- a/sql/sql_base.h +++ b/sql/sql_base.h @@ -272,6 +272,7 @@ bool rename_temporary_table(THD* thd, TABLE *table, const char *new_db, const char *table_name); bool is_equal(const LEX_STRING *a, const LEX_STRING *b); +class Open_tables_backup; /* Functions to work with system tables. */ bool open_system_tables_for_read(THD *thd, TABLE_LIST *table_list, Open_tables_backup *backup); diff --git a/sql/sql_cache.cc b/sql/sql_cache.cc index 256a51b9575..f5e1deab546 100644 --- a/sql/sql_cache.cc +++ b/sql/sql_cache.cc @@ -1041,8 +1041,8 @@ void query_cache_insert(const char *packet, ulong length, /* Current_thd can be NULL when a new connection is immediately ended due to "Too many connections". thd->store_globals() has not been - called at this time and hence my_pthread_setspecific_ptr(THR_THD, - this) has not been called for this thread. + called at this time and hence set_current_thd(this) has not been + called for this thread. */ if (!thd) diff --git a/sql/sql_class.cc b/sql/sql_class.cc index 0709509a740..a068cdc8f88 100644 --- a/sql/sql_class.cc +++ b/sql/sql_class.cc @@ -739,7 +739,7 @@ char *thd_security_context(THD *thd, char *buffer, unsigned int length, values doesn't have to very accurate and the memory it points to is static, but we need to attempt a snapshot on the pointer values to avoid using NULL values. The pointer to thd->query however, doesn't point to static memory - and has to be protected by LOCK_thread_count or risk pointing to + and has to be protected by thd->LOCK_thd_data or risk pointing to uninitialized memory. */ const char *proc_info= thd->proc_info; @@ -774,20 +774,21 @@ char *thd_security_context(THD *thd, char *buffer, unsigned int length, str.append(proc_info); } - mysql_mutex_lock(&thd->LOCK_thd_data); - - if (thd->query()) + /* Don't wait if LOCK_thd_data is used as this could cause a deadlock */ + if (!mysql_mutex_trylock(&thd->LOCK_thd_data)) { - if (max_query_len < 1) - len= thd->query_length(); - else - len= min(thd->query_length(), max_query_len); - str.append('\n'); - str.append(thd->query(), len); + if (thd->query()) + { + if (max_query_len < 1) + len= thd->query_length(); + else + len= min(thd->query_length(), max_query_len); + str.append('\n'); + str.append(thd->query(), len); + } + mysql_mutex_unlock(&thd->LOCK_thd_data); } - mysql_mutex_unlock(&thd->LOCK_thd_data); - if (str.c_ptr_safe() == buffer) return buffer; @@ -851,7 +852,9 @@ THD::THD() m_statement_psi(NULL), m_idle_psi(NULL), m_server_idle(false), + thread_id(0), global_disable_checkpoint(0), + failed_com_change_user(0), is_fatal_error(0), transaction_rollback_request(0), is_fatal_sub_stmt_error(0), @@ -865,17 +868,28 @@ THD::THD() #if defined(ENABLED_DEBUG_SYNC) debug_sync_control(0), #endif /* defined(ENABLED_DEBUG_SYNC) */ - main_warning_info(0, false) + main_warning_info(0, false, false) { ulong tmp; mdl_context.init(this); /* + We set THR_THD to temporally point to this THD to register all the + variables that allocates memory for this THD + */ + THD *old_THR_THD= current_thd; + set_current_thd(this); + status_var.memory_used= 0; + + main_warning_info.init(); + /* Pass nominal parameters to init_alloc_root only to ensure that the destructor works OK in case of an error. The main_mem_root will be re-initialized in init_for_queries(). */ - init_sql_alloc(&main_mem_root, ALLOC_ROOT_MIN_BLOCK_SIZE, 0); + init_sql_alloc(&main_mem_root, ALLOC_ROOT_MIN_BLOCK_SIZE, 0, + MYF(MY_THREAD_SPECIFIC)); + stmt_arena= this; thread_stack= 0; scheduler= thread_scheduler; // Will be fixed later @@ -914,7 +928,6 @@ THD::THD() connection_name.length= 0; bzero(&variables, sizeof(variables)); - thread_id= 0; one_shot_set= 0; file_id = 0; query_id= 0; @@ -932,6 +945,7 @@ THD::THD() mysql_audit_init_thd(this); #endif net.vio=0; + net.buff= 0; client_capabilities= 0; // minimalistic client ull=0; system_thread= NON_SYSTEM_THREAD; @@ -973,7 +987,7 @@ THD::THD() user_connect=(USER_CONN *)0; my_hash_init(&user_vars, system_charset_info, USER_VARS_HASH_SIZE, 0, 0, (my_hash_get_key) get_var_key, - (my_hash_free_key) free_user_var, 0); + (my_hash_free_key) free_user_var, HASH_THREAD_SPECIFIC); sp_proc_cache= NULL; sp_func_cache= NULL; @@ -981,7 +995,7 @@ THD::THD() /* For user vars replication*/ if (opt_bin_log) my_init_dynamic_array(&user_var_events, - sizeof(BINLOG_USER_VAR_EVENT *), 16, 16); + sizeof(BINLOG_USER_VAR_EVENT *), 16, 16, MYF(0)); else bzero((char*) &user_var_events, sizeof(user_var_events)); @@ -1004,6 +1018,8 @@ THD::THD() prepare_derived_at_open= FALSE; create_tmp_table_for_derived= FALSE; save_prep_leaf_list= FALSE; + /* Restore THR_THD */ + set_current_thd(old_THR_THD); } @@ -1272,6 +1288,7 @@ extern "C" THD *_current_thd_noinline(void) void THD::init(void) { + DBUG_ENTER("thd::init"); mysql_mutex_lock(&LOCK_global_system_variables); plugin_thdvar_init(this); /* @@ -1302,7 +1319,7 @@ void THD::init(void) tx_read_only= variables.tx_read_only; update_charset(); reset_current_stmt_binlog_format_row(); - bzero((char *) &status_var, sizeof(status_var)); + set_status_var_init(); bzero((char *) &org_status_var, sizeof(org_status_var)); if (variables.sql_log_bin) @@ -1319,6 +1336,7 @@ void THD::init(void) debug_sync_init_thread(this); #endif /* defined(ENABLED_DEBUG_SYNC) */ apc_target.init(&LOCK_thd_data); + DBUG_VOID_RETURN; } @@ -1408,7 +1426,7 @@ void THD::change_user(void) mysql_mutex_unlock(&LOCK_status); cleanup(); - killed= NOT_KILLED; + reset_killed(); cleanup_done= 0; init(); stmt_map.reset(); @@ -1492,8 +1510,16 @@ void THD::cleanup(void) THD::~THD() { + THD *orig_thd= current_thd; THD_CHECK_SENTRY(this); DBUG_ENTER("~THD()"); + + /* + In error cases, thd may not be current thd. We have to fix this so + that memory allocation counting is done correctly + */ + set_current_thd(this); + /* Ensure that no one is using THD */ mysql_mutex_lock(&LOCK_thd_data); mysys_var=0; // Safety (shouldn't be needed) @@ -1502,10 +1528,8 @@ THD::~THD() /* Close connection */ #ifndef EMBEDDED_LIBRARY if (net.vio) - { vio_delete(net.vio); - net_end(&net); - } + net_end(&net); #endif stmt_map.reset(); /* close all prepared statements */ if (!cleanup_done) @@ -1540,6 +1564,15 @@ THD::~THD() #endif free_root(&main_mem_root, MYF(0)); + main_warning_info.free_memory(); + if (status_var.memory_used != 0) + { + DBUG_PRINT("error", ("memory_used: %lld", status_var.memory_used)); + SAFEMALLOC_REPORT_MEMORY(my_thread_dbug_id()); + DBUG_ASSERT(status_var.memory_used == 0); // Ensure everything is freed + } + + set_current_thd(orig_thd); DBUG_VOID_RETURN; } @@ -1664,6 +1697,10 @@ void THD::awake(killed_state state_to_set) MYSQL_CALLBACK(scheduler, post_kill_notification, (this)); } + /* Interrupt target waiting inside a storage engine. */ + if (state_to_set != NOT_KILLED) + ha_kill_query(this, thd_kill_level(this)); + /* Broadcast a condition to kick the target if it is waiting on it. */ if (mysys_var) { @@ -1811,7 +1848,7 @@ bool THD::store_globals() */ DBUG_ASSERT(thread_stack); - if (my_pthread_setspecific_ptr(THR_THD, this) || + if (set_current_thd(this) || my_pthread_setspecific_ptr(THR_MALLOC, &mem_root)) return 1; /* @@ -1855,7 +1892,7 @@ void THD::reset_globals() mysql_mutex_unlock(&LOCK_thd_data); /* Undocking the thread specific data. */ - my_pthread_setspecific_ptr(THR_THD, NULL); + set_current_thd(0); my_pthread_setspecific_ptr(THR_MALLOC, NULL); } @@ -3711,7 +3748,8 @@ void thd_increment_net_big_packet_count(ulong length) void THD::set_status_var_init() { - bzero((char*) &status_var, sizeof(status_var)); + bzero((char*) &status_var, offsetof(STATUS_VAR, + last_cleared_system_status_var)); } @@ -3907,19 +3945,29 @@ void THD::restore_backup_open_tables_state(Open_tables_backup *backup) DBUG_VOID_RETURN; } +#if MARIA_PLUGIN_INTERFACE_VERSION < 0x0200 /** - Check the killed state of a user thread - @param thd user thread - @retval 0 the user thread is active - @retval 1 the user thread has been killed - - This is used to signal a storage engine if it should be killed. - See also THD::check_killed(). + This is a backward compatibility method, made obsolete + by the thd_kill_statement service. Keep it here to avoid breaking the + ABI in case some binary plugins still use it. */ - +#undef thd_killed extern "C" int thd_killed(const MYSQL_THD thd) { + return thd_kill_level(thd) > THD_ABORT_SOFTLY; +} +#else +#error now thd_killed() function can go away +#endif + +/* + return thd->killed status to the client, + mapped to the API enum thd_kill_levels values. +*/ +extern "C" enum thd_kill_levels thd_kill_level(const MYSQL_THD thd) +{ THD* current= current_thd; + if (!thd) thd= current; @@ -3930,9 +3978,10 @@ extern "C" int thd_killed(const MYSQL_THD thd) apc_target->process_apc_requests(); } - if (!(thd->killed & KILL_HARD_BIT)) - return 0; - return thd->killed; + if (likely(thd->killed == NOT_KILLED)) + return THD_IS_NOT_KILLED; + + return thd->killed & KILL_HARD_BIT ? THD_ABORT_ASAP : THD_ABORT_SOFTLY; } @@ -5115,7 +5164,7 @@ THD::binlog_prepare_pending_rows_event(TABLE* table, uint32 serv_id, There is no good place to set up the transactional data, so we have to do it here. */ - if (binlog_setup_trx_data()) + if (binlog_setup_trx_data() == NULL) DBUG_RETURN(NULL); Rows_log_event* pending= binlog_get_pending_rows_event(is_transactional); @@ -5808,4 +5857,3 @@ bool Discrete_intervals_list::append(Discrete_interval *new_interval) } #endif /* !defined(MYSQL_CLIENT) */ - diff --git a/sql/sql_class.h b/sql/sql_class.h index c50d8c9aba6..fccca9e8cbf 100644 --- a/sql/sql_class.h +++ b/sql/sql_class.h @@ -700,6 +700,8 @@ typedef struct system_status_var ulonglong binlog_bytes_written; double last_query_cost; double cpu_time, busy_time; + /* Don't initialize */ + volatile int64 memory_used; /* This shouldn't be accumulated */ } STATUS_VAR; /* @@ -709,6 +711,7 @@ typedef struct system_status_var */ #define last_system_status_var questions +#define last_cleared_system_status_var memory_used void mark_transaction_to_rollback(THD *thd, bool all); @@ -1442,7 +1445,8 @@ public: m_reopen_array(NULL), m_locked_tables_count(0) { - init_sql_alloc(&m_locked_tables_root, MEM_ROOT_BLOCK_SIZE, 0); + init_sql_alloc(&m_locked_tables_root, MEM_ROOT_BLOCK_SIZE, 0, + MYF(MY_THREAD_SPECIFIC)); } void unlock_locked_tables(THD *thd); ~Locked_tables_list() @@ -1782,7 +1786,7 @@ public: bool save_prep_leaf_list; #ifndef MYSQL_CLIENT - int binlog_setup_trx_data(); + binlog_cache_mngr * binlog_setup_trx_data(); /* Public interface to write RBR events to the binlog @@ -1916,7 +1920,8 @@ public: { bzero((char*)this, sizeof(*this)); xid_state.xid.null(); - init_sql_alloc(&mem_root, ALLOC_ROOT_MIN_BLOCK_SIZE, 0); + init_sql_alloc(&mem_root, ALLOC_ROOT_MIN_BLOCK_SIZE, 0, + MYF(MY_THREAD_SPECIFIC)); } } transaction; Global_read_lock global_read_lock; @@ -2315,6 +2320,7 @@ public: bool no_errors; uint8 password; + uint8 failed_com_change_user; /** Set to TRUE if execution of the current compound statement @@ -2848,6 +2854,19 @@ public: { return ::killed_errno(killed); } + inline void reset_killed() + { + /* + Resetting killed has to be done under a mutex to ensure + its not done during an awake() call. + */ + if (killed != NOT_KILLED) + { + mysql_mutex_lock(&LOCK_thd_data); + killed= NOT_KILLED; + mysql_mutex_unlock(&LOCK_thd_data); + } + } inline void send_kill_message() const { int err= killed_errno(); @@ -4092,6 +4111,8 @@ class Unique :public Sql_alloc uint full_size; uint min_dupl_count; /* always 0 for unions, > 0 for intersections */ + bool merge(TABLE *table, uchar *buff, bool without_last_merge); + public: ulong elements; Unique(qsort_cmp2 comp_func, void *comp_func_fixed_arg, @@ -4132,7 +4153,7 @@ public: } void reset(); - bool walk(tree_walk_action action, void *walk_action_arg); + bool walk(TABLE *table, tree_walk_action action, void *walk_action_arg); uint get_size() const { return size; } ulonglong get_max_in_memory_size() const { return max_in_memory_size; } diff --git a/sql/sql_connect.cc b/sql/sql_connect.cc index dcc63a80b25..59aa51916fb 100644 --- a/sql/sql_connect.cc +++ b/sql/sql_connect.cc @@ -905,6 +905,19 @@ static int check_connection(THD *thd) my_error(ER_BAD_HOST_ERROR, MYF(0)); return 1; } + /* BEGIN : DEBUG */ + DBUG_EXECUTE_IF("addr_fake_ipv4", + { + struct sockaddr *sa= (sockaddr *) &net->vio->remote; + sa->sa_family= AF_INET; + struct in_addr *ip4= &((struct sockaddr_in *)sa)->sin_addr; + /* See RFC 5737, 192.0.2.0/23 is reserved */ + const char* fake= "192.0.2.4"; + ip4->s_addr= inet_addr(fake); + strcpy(ip, fake); + };); + /* END : DEBUG */ + if (!(thd->main_security_ctx.ip= my_strdup(ip,MYF(MY_WME)))) return 1; /* The error is set by my_strdup(). */ thd->main_security_ctx.host_or_ip= thd->main_security_ctx.ip; diff --git a/sql/sql_const.h b/sql/sql_const.h index 255a0162e93..c6aa52197d5 100644 --- a/sql/sql_const.h +++ b/sql/sql_const.h @@ -43,8 +43,8 @@ #define MAX_MBWIDTH 3 /* Max multibyte sequence */ #define MAX_FIELD_CHARLENGTH 255 #define MAX_FIELD_VARCHARLENGTH 65535 -#define MAX_FIELD_BLOBLENGTH UINT_MAX32 /* cf field_blob::get_length() */ -#define CONVERT_IF_BIGGER_TO_BLOB 512 /* Used for CREATE ... SELECT */ +#define MAX_FIELD_BLOBLENGTH UINT_MAX32 /* cf field_blob::get_length() */ +#define CONVERT_IF_BIGGER_TO_BLOB 512 /* Threshold *in characters* */ /* Max column width +1 */ #define MAX_FIELD_WIDTH (MAX_FIELD_CHARLENGTH*MAX_MBWIDTH+1) diff --git a/sql/sql_db.cc b/sql/sql_db.cc index dc8a2e9f057..87650e643f2 100644 --- a/sql/sql_db.cc +++ b/sql/sql_db.cc @@ -1,5 +1,6 @@ /* Copyright (c) 2000, 2011, Oracle and/or its affiliates. + Copyright (c) 2009, 2013, Monty Program Ab. 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 @@ -755,7 +756,7 @@ bool mysql_rm_db(THD *thd,char *db,bool if_exists, bool silent) { ulong deleted_tables= 0; bool error= true; - char path[FN_REFLEN+16]; + char path[FN_REFLEN + 16]; MY_DIR *dirp; uint length; bool found_other_files= false; @@ -928,7 +929,7 @@ update_binlog: if (!(query= (char*) thd->alloc(MAX_DROP_TABLE_Q_LEN))) goto exit; /* not much else we can do */ - query_pos= query_data_start= strmov(query,"drop table "); + query_pos= query_data_start= strmov(query,"DROP TABLE "); query_end= query + MAX_DROP_TABLE_Q_LEN; db_len= strlen(db); diff --git a/sql/sql_delete.cc b/sql/sql_delete.cc index 8a1b5f46bce..d0a83eac189 100644 --- a/sql/sql_delete.cc +++ b/sql/sql_delete.cc @@ -264,7 +264,8 @@ bool mysql_delete(THD *thd, TABLE_LIST *table_list, COND *conds, { DBUG_ASSERT(usable_index == MAX_KEY); table->sort.io_cache= (IO_CACHE *) my_malloc(sizeof(IO_CACHE), - MYF(MY_FAE | MY_ZEROFILL)); + MYF(MY_FAE | MY_ZEROFILL | + MY_THREAD_SPECIFIC)); if (!(sortorder= make_unireg_sortorder(order, &length, NULL)) || (table->sort.found_records= filesort(thd, table, sortorder, length, diff --git a/sql/sql_error.cc b/sql/sql_error.cc index 06da6250e71..23a60267737 100644 --- a/sql/sql_error.cc +++ b/sql/sql_error.cc @@ -457,23 +457,38 @@ Diagnostics_area::disable_status() m_status= DA_DISABLED; } -Warning_info::Warning_info(ulonglong warn_id_arg, bool allow_unlimited_warnings) +Warning_info::Warning_info(ulonglong warn_id_arg, + bool allow_unlimited_warnings, bool initialize) :m_statement_warn_count(0), m_current_row_for_warning(1), m_warn_id(warn_id_arg), m_allow_unlimited_warnings(allow_unlimited_warnings), + initialized(0), m_read_only(FALSE) { - /* Initialize sub structures */ - init_sql_alloc(&m_warn_root, WARN_ALLOC_BLOCK_SIZE, WARN_ALLOC_PREALLOC_SIZE); m_warn_list.empty(); bzero((char*) m_warn_count, sizeof(m_warn_count)); + if (initialize) + init(); } +void Warning_info::init() +{ + /* Initialize sub structures */ + init_sql_alloc(&m_warn_root, WARN_ALLOC_BLOCK_SIZE, + WARN_ALLOC_PREALLOC_SIZE, MYF(MY_THREAD_SPECIFIC)); + initialized= 1; +} + +void Warning_info::free_memory() +{ + if (initialized) + free_root(&m_warn_root,MYF(0)); +} Warning_info::~Warning_info() { - free_root(&m_warn_root,MYF(0)); + free_memory(); } @@ -484,7 +499,7 @@ Warning_info::~Warning_info() void Warning_info::clear_warning_info(ulonglong warn_id_arg) { m_warn_id= warn_id_arg; - free_root(&m_warn_root, MYF(0)); + free_memory(); bzero((char*) m_warn_count, sizeof(m_warn_count)); m_warn_list.empty(); m_statement_warn_count= 0; diff --git a/sql/sql_error.h b/sql/sql_error.h index f11ce6dcf0d..fadd3b51ec6 100644 --- a/sql/sql_error.h +++ b/sql/sql_error.h @@ -367,15 +367,21 @@ class Warning_info /** Indicates if push_warning() allows unlimited number of warnings. */ bool m_allow_unlimited_warnings; + bool initialized; /* Set to 1 if init() has been called */ private: Warning_info(const Warning_info &rhs); /* Not implemented */ Warning_info& operator=(const Warning_info &rhs); /* Not implemented */ public: - Warning_info(ulonglong warn_id_arg, bool allow_unlimited_warnings); + Warning_info(ulonglong warn_id_arg, bool allow_unlimited_warnings, + bool initialize=true); ~Warning_info(); + /* Allocate memory for structures */ + void init(); + void free_memory(); + /** Reset the warning information. Clear all warnings, the number of warnings, reset current row counter diff --git a/sql/sql_handler.cc b/sql/sql_handler.cc index d03b38171fc..1c93a59904c 100644 --- a/sql/sql_handler.cc +++ b/sql/sql_handler.cc @@ -323,7 +323,7 @@ bool mysql_ha_open(THD *thd, TABLE_LIST *tables, SQL_HANDLER *reopen) /* copy data to sql_handler */ if (!(sql_handler= new SQL_HANDLER(thd))) goto err; - init_alloc_root(&sql_handler->mem_root, 1024, 0); + init_alloc_root(&sql_handler->mem_root, 1024, 0, MYF(MY_THREAD_SPECIFIC)); sql_handler->db.length= strlen(tables->db); sql_handler->table_name.length= strlen(tables->table_name); @@ -846,14 +846,14 @@ retry: case RFIRST: if (keyname) { - table->file->ha_index_or_rnd_end(); - table->file->ha_index_init(keyno, 1); - error= table->file->ha_index_first(table->record[0]); + if (!(error= table->file->ha_index_or_rnd_end()) && + !(error= table->file->ha_index_init(keyno, 1))) + error= table->file->ha_index_first(table->record[0]); } else { - table->file->ha_index_or_rnd_end(); - if (!(error= table->file->ha_rnd_init(1))) + if (!(error= table->file->ha_index_or_rnd_end()) && + !(error= table->file->ha_rnd_init(1))) error= table->file->ha_rnd_next(table->record[0]); } mode= RNEXT; @@ -872,10 +872,10 @@ retry: /* else fall through */ case RLAST: DBUG_ASSERT(keyname != 0); - table->file->ha_index_or_rnd_end(); - table->file->ha_index_init(keyno, 1); - error= table->file->ha_index_last(table->record[0]); - mode= RPREV; + if (!(error= table->file->ha_index_or_rnd_end()) && + !(error= table->file->ha_index_init(keyno, 1))) + error= table->file->ha_index_last(table->record[0]); + mode=RPREV; break; case RNEXT_SAME: /* Continue scan on "(keypart1,keypart2,...)=(c1, c2, ...) */ @@ -889,13 +889,14 @@ retry: if (!(key= (uchar*) thd->calloc(ALIGN_SIZE(handler->key_len)))) goto err; - table->file->ha_index_or_rnd_end(); - table->file->ha_index_init(keyno, 1); + if ((error= table->file->ha_index_or_rnd_end())) + break; key_copy(key, table->record[0], table->key_info + keyno, handler->key_len); - error= table->file->ha_index_read_map(table->record[0], - key, handler->keypart_map, - ha_rkey_mode); + if (!(error= table->file->ha_index_init(keyno, 1))) + error= table->file->ha_index_read_map(table->record[0], + key, handler->keypart_map, + ha_rkey_mode); mode= rkey_to_rnext[(int)ha_rkey_mode]; break; } diff --git a/sql/sql_help.cc b/sql/sql_help.cc index c352272e95c..458904ebe1d 100644 --- a/sql/sql_help.cc +++ b/sql/sql_help.cc @@ -303,8 +303,14 @@ int get_topics_for_keyword(THD *thd, TABLE *topics, TABLE *relations, rtopic_id= find_fields[help_relation_help_topic_id].field; rkey_id= find_fields[help_relation_help_keyword_id].field; - topics->file->ha_index_init(iindex_topic,1); - relations->file->ha_index_init(iindex_relations,1); + if (topics->file->ha_index_init(iindex_topic,1) || + relations->file->ha_index_init(iindex_relations,1)) + { + if (topics->file->inited) + topics->file->ha_index_end(); + my_message(ER_CORRUPT_HELP_DB, ER(ER_CORRUPT_HELP_DB), MYF(0)); + DBUG_RETURN(-1); + } rkey_id->store((longlong) key_id, TRUE); rkey_id->get_key_image(buff, rkey_id->pack_length(), Field::itRAW); diff --git a/sql/sql_insert.cc b/sql/sql_insert.cc index 67f3955ea61..09e4953cdc4 100644 --- a/sql/sql_insert.cc +++ b/sql/sql_insert.cc @@ -1,5 +1,6 @@ /* Copyright (c) 2000, 2011, Oracle and/or its affiliates. + Copyright (c) 2009, 2013, Monty Program Ab. 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 @@ -2252,11 +2253,8 @@ bool delayed_get_table(THD *thd, MDL_request *grl_protection_request, want to send "Server shutdown in progress" in the INSERT THREAD. */ - if (di->thd.stmt_da->sql_errno() == ER_SERVER_SHUTDOWN) - my_message(ER_QUERY_INTERRUPTED, ER(ER_QUERY_INTERRUPTED), MYF(0)); - else - my_message(di->thd.stmt_da->sql_errno(), di->thd.stmt_da->message(), - MYF(0)); + my_message(di->thd.stmt_da->sql_errno(), di->thd.stmt_da->message(), + MYF(0)); } di->unlock(); goto end_create; @@ -2305,7 +2303,8 @@ end_create: TABLE *Delayed_insert::get_local_table(THD* client_thd) { my_ptrdiff_t adjust_ptrs; - Field **field,**org_field, *found_next_number_field, **dfield_ptr= 0; + Field **field,**org_field, *found_next_number_field; + Field **UNINIT_VAR(vfield), **UNINIT_VAR(dfield_ptr); TABLE *copy; TABLE_SHARE *share; uchar *bitmap; @@ -2341,7 +2340,7 @@ TABLE *Delayed_insert::get_local_table(THD* client_thd) killed using mysql_notify_thread_having_shared_lock() or kill_delayed_threads_for_table(). */ - if (!thd.is_error() || thd.stmt_da->sql_errno() == ER_SERVER_SHUTDOWN) + if (!thd.is_error()) my_message(ER_QUERY_INTERRUPTED, ER(ER_QUERY_INTERRUPTED), MYF(0)); else my_message(thd.stmt_da->sql_errno(), thd.stmt_da->message(), MYF(0)); @@ -2365,6 +2364,13 @@ TABLE *Delayed_insert::get_local_table(THD* client_thd) if (!copy_tmp) goto error; + if (share->vfields) + { + vfield= (Field **) client_thd->alloc((share->vfields+1)*sizeof(Field*)); + if (!vfield) + goto error; + } + /* Copy the TABLE object. */ copy= new (copy_tmp) TABLE; *copy= *table; @@ -2410,6 +2416,28 @@ TABLE *Delayed_insert::get_local_table(THD* client_thd) } } *field=0; + + if (share->vfields) + { + copy->vfield= vfield; + for (field= copy->field; *field; field++) + { + if ((*field)->vcol_info) + { + bool error_reported= FALSE; + if (unpack_vcol_info_from_frm(client_thd, + client_thd->mem_root, + copy, + *field, + &(*field)->vcol_info->expr_str, + &error_reported)) + goto error; + *vfield++= *field; + } + } + *vfield= 0; + } + if (share->default_fields) *dfield_ptr= NULL; @@ -2484,7 +2512,9 @@ int write_delayed(THD *thd, TABLE *table, enum_duplicates duplic, goto err; } - if (!(row->record= (char*) my_malloc(table->s->reclength, MYF(MY_WME)))) + /* This can't be THREAD_SPECIFIC as it's freed in delayed thread */ + if (!(row->record= (char*) my_malloc(table->s->reclength, + MYF(MY_WME)))) goto err; memcpy(row->record, table->record[0], table->s->reclength); row->start_time= thd->start_time; @@ -2715,7 +2745,10 @@ pthread_handler_t handle_delayed_insert(void *arg) thd->thread_id= thd->variables.pseudo_thread_id= thread_id++; thd->set_current_time(); threads.append(thd); - thd->killed=abort_loop ? KILL_CONNECTION : NOT_KILLED; + if (abort_loop) + thd->killed= KILL_CONNECTION; + else + thd->reset_killed(); mysql_mutex_unlock(&LOCK_thread_count); mysql_thread_set_psi_id(thd->thread_id); @@ -2814,8 +2847,12 @@ pthread_handler_t handle_delayed_insert(void *arg) set_timespec(abstime, delayed_insert_timeout); /* Information for pthread_kill */ + mysql_mutex_unlock(&di->mutex); + mysql_mutex_lock(&di->thd.mysys_var->mutex); di->thd.mysys_var->current_mutex= &di->mutex; di->thd.mysys_var->current_cond= &di->cond; + mysql_mutex_unlock(&di->thd.mysys_var->mutex); + mysql_mutex_lock(&di->mutex); THD_STAGE_INFO(&(di->thd), stage_waiting_for_insert); DBUG_PRINT("info",("Waiting for someone to insert rows")); @@ -2900,24 +2937,28 @@ pthread_handler_t handle_delayed_insert(void *arg) DBUG_LEAVE; } - di->table=0; - thd->killed= KILL_CONNECTION; // If error - mysql_mutex_unlock(&di->mutex); + { + DBUG_ENTER("handle_delayed_insert-cleanup"); + di->table=0; + thd->killed= KILL_CONNECTION; // If error + mysql_mutex_unlock(&di->mutex); - close_thread_tables(thd); // Free the table - thd->mdl_context.release_transactional_locks(); - mysql_cond_broadcast(&di->cond_client); // Safety + close_thread_tables(thd); // Free the table + thd->mdl_context.release_transactional_locks(); + mysql_cond_broadcast(&di->cond_client); // Safety - mysql_mutex_lock(&LOCK_delayed_create); // Because of delayed_get_table - mysql_mutex_lock(&LOCK_delayed_insert); - /* - di should be unlinked from the thread handler list and have no active - clients - */ - delete di; - mysql_mutex_unlock(&LOCK_delayed_insert); - mysql_mutex_unlock(&LOCK_delayed_create); + mysql_mutex_lock(&LOCK_delayed_create); // Because of delayed_get_table + mysql_mutex_lock(&LOCK_delayed_insert); + /* + di should be unlinked from the thread handler list and have no active + clients + */ + delete di; + mysql_mutex_unlock(&LOCK_delayed_insert); + mysql_mutex_unlock(&LOCK_delayed_create); + DBUG_LEAVE; + } my_thread_end(); pthread_exit(0); diff --git a/sql/sql_join_cache.cc b/sql/sql_join_cache.cc index 07266cc9cbe..fde9f70fa79 100644 --- a/sql/sql_join_cache.cc +++ b/sql/sql_join_cache.cc @@ -934,7 +934,7 @@ int JOIN_CACHE::alloc_buffer() { ulong next_buff_size; - if ((buff= (uchar*) my_malloc(buff_size, MYF(0)))) + if ((buff= (uchar*) my_malloc(buff_size, MYF(MY_THREAD_SPECIFIC)))) break; next_buff_size= buff_size > buff_size_decr ? buff_size-buff_size_decr : 0; @@ -1012,7 +1012,7 @@ int JOIN_CACHE::realloc_buffer() { int rc; free(); - rc= test(!(buff= (uchar*) my_malloc(buff_size, MYF(0)))); + rc= test(!(buff= (uchar*) my_malloc(buff_size, MYF(MY_THREAD_SPECIFIC)))); reset(TRUE); return rc; } @@ -2801,7 +2801,7 @@ int JOIN_CACHE_HASHED::realloc_buffer() { int rc; free(); - rc= test(!(buff= (uchar*) my_malloc(buff_size, MYF(0)))); + rc= test(!(buff= (uchar*) my_malloc(buff_size, MYF(MY_THREAD_SPECIFIC)))); init_hash_table(); reset(TRUE); return rc; @@ -3894,13 +3894,15 @@ int JOIN_TAB_SCAN_MRR::next() int rc= join_tab->table->file->multi_range_read_next((range_id_t*)ptr) ? -1 : 0; if (!rc) { - /* + /* If a record in in an incremental cache contains no fields then the association for the last record in cache will be equal to cache->end_pos - */ - DBUG_ASSERT((!(mrr_mode & HA_MRR_NO_ASSOCIATION))? - (cache->buff <= (uchar *) (*ptr) && - (uchar *) (*ptr) <= cache->end_pos): TRUE); + */ + /* + psergey: this makes no sense where HA_MRR_NO_ASSOC is used. + DBUG_ASSERT(cache->buff <= (uchar *) (*ptr) && + (uchar *) (*ptr) <= cache->end_pos); + */ if (join_tab->table->vfield) update_virtual_fields(join->thd, join_tab->table); } @@ -3910,663 +3912,663 @@ int JOIN_TAB_SCAN_MRR::next() static void bka_range_seq_key_info(void *init_params, uint *length, - key_part_map *map) + key_part_map *map) { - TABLE_REF *ref= &(((JOIN_CACHE*)init_params)->join_tab->ref); - *length= ref->key_length; - *map= (key_part_map(1) << ref->key_parts) - 1; +TABLE_REF *ref= &(((JOIN_CACHE*)init_params)->join_tab->ref); +*length= ref->key_length; +*map= (key_part_map(1) << ref->key_parts) - 1; } /* - Initialize retrieval of range sequence for BKA join algorithm - - SYNOPSIS - bka_range_seq_init() - init_params pointer to the BKA join cache object - n_ranges the number of ranges obtained - flags combination of MRR flags - - DESCRIPTION - The function interprets init_param as a pointer to a JOIN_CACHE_BKA - object. The function prepares for an iteration over the join keys - built for all records from the cache join buffer. - - NOTE - This function are used only as a callback function. - - RETURN VALUE - init_param value that is to be used as a parameter of bka_range_seq_next() +Initialize retrieval of range sequence for BKA join algorithm + +SYNOPSIS + bka_range_seq_init() + init_params pointer to the BKA join cache object + n_ranges the number of ranges obtained + flags combination of MRR flags + +DESCRIPTION + The function interprets init_param as a pointer to a JOIN_CACHE_BKA + object. The function prepares for an iteration over the join keys + built for all records from the cache join buffer. + +NOTE + This function are used only as a callback function. + +RETURN VALUE + init_param value that is to be used as a parameter of bka_range_seq_next() */ static range_seq_t bka_range_seq_init(void *init_param, uint n_ranges, uint flags) { - DBUG_ENTER("bka_range_seq_init"); - JOIN_CACHE_BKA *cache= (JOIN_CACHE_BKA *) init_param; - cache->reset(0); - DBUG_RETURN((range_seq_t) init_param); +DBUG_ENTER("bka_range_seq_init"); +JOIN_CACHE_BKA *cache= (JOIN_CACHE_BKA *) init_param; +cache->reset(0); +DBUG_RETURN((range_seq_t) init_param); } /* - Get the next range/key over records from the join buffer used by a BKA cache - - SYNOPSIS - bka_range_seq_next() - seq the value returned by bka_range_seq_init - range OUT reference to the next range +Get the next range/key over records from the join buffer used by a BKA cache - DESCRIPTION - The function interprets seq as a pointer to a JOIN_CACHE_BKA - object. The function returns a pointer to the range descriptor - for the key built over the next record from the join buffer. - - NOTE - This function are used only as a callback function. - - RETURN VALUE - FALSE ok, the range structure filled with info about the next range/key - TRUE no more ranges +SYNOPSIS + bka_range_seq_next() + seq the value returned by bka_range_seq_init + range OUT reference to the next range + +DESCRIPTION + The function interprets seq as a pointer to a JOIN_CACHE_BKA + object. The function returns a pointer to the range descriptor + for the key built over the next record from the join buffer. + +NOTE + This function are used only as a callback function. + +RETURN VALUE + FALSE ok, the range structure filled with info about the next range/key + TRUE no more ranges */ static bool bka_range_seq_next(range_seq_t rseq, KEY_MULTI_RANGE *range) { - DBUG_ENTER("bka_range_seq_next"); - JOIN_CACHE_BKA *cache= (JOIN_CACHE_BKA *) rseq; - TABLE_REF *ref= &cache->join_tab->ref; - key_range *start_key= &range->start_key; - if ((start_key->length= cache->get_next_key((uchar **) &start_key->key))) - { - start_key->keypart_map= (1 << ref->key_parts) - 1; - start_key->flag= HA_READ_KEY_EXACT; - range->end_key= *start_key; - range->end_key.flag= HA_READ_AFTER_KEY; - range->ptr= (char *) cache->get_curr_rec(); - range->range_flag= EQ_RANGE; - DBUG_RETURN(0); - } - DBUG_RETURN(1); +DBUG_ENTER("bka_range_seq_next"); +JOIN_CACHE_BKA *cache= (JOIN_CACHE_BKA *) rseq; +TABLE_REF *ref= &cache->join_tab->ref; +key_range *start_key= &range->start_key; +if ((start_key->length= cache->get_next_key((uchar **) &start_key->key))) +{ + start_key->keypart_map= (1 << ref->key_parts) - 1; + start_key->flag= HA_READ_KEY_EXACT; + range->end_key= *start_key; + range->end_key.flag= HA_READ_AFTER_KEY; + range->ptr= (char *) cache->get_curr_rec(); + range->range_flag= EQ_RANGE; + DBUG_RETURN(0); +} +DBUG_RETURN(1); } /* - Check whether range_info orders to skip the next record from BKA buffer - - SYNOPSIS - bka_range_seq_skip_record() - seq value returned by bka_range_seq_init() - range_info information about the next range - rowid [NOT USED] rowid of the record to be checked +Check whether range_info orders to skip the next record from BKA buffer - - DESCRIPTION - The function interprets seq as a pointer to a JOIN_CACHE_BKA object. - The function returns TRUE if the record with this range_info - is to be filtered out from the stream of records returned by - multi_range_read_next(). - - NOTE - This function are used only as a callback function. +SYNOPSIS + bka_range_seq_skip_record() + seq value returned by bka_range_seq_init() + range_info information about the next range + rowid [NOT USED] rowid of the record to be checked - RETURN VALUE - 1 record with this range_info is to be filtered out from the stream - of records returned by multi_range_read_next() - 0 the record is to be left in the stream + +DESCRIPTION + The function interprets seq as a pointer to a JOIN_CACHE_BKA object. + The function returns TRUE if the record with this range_info + is to be filtered out from the stream of records returned by + multi_range_read_next(). + +NOTE + This function are used only as a callback function. + +RETURN VALUE + 1 record with this range_info is to be filtered out from the stream + of records returned by multi_range_read_next() + 0 the record is to be left in the stream */ static bool bka_range_seq_skip_record(range_seq_t rseq, range_id_t range_info, uchar *rowid) { - DBUG_ENTER("bka_range_seq_skip_record"); - JOIN_CACHE_BKA *cache= (JOIN_CACHE_BKA *) rseq; - bool res= cache->get_match_flag_by_pos((uchar *) range_info) == - JOIN_CACHE::MATCH_FOUND; - DBUG_RETURN(res); +DBUG_ENTER("bka_range_seq_skip_record"); +JOIN_CACHE_BKA *cache= (JOIN_CACHE_BKA *) rseq; +bool res= cache->get_match_flag_by_pos((uchar *) range_info) == + JOIN_CACHE::MATCH_FOUND; +DBUG_RETURN(res); } /* - Check if the record combination from BKA cache matches the index condition +Check if the record combination from BKA cache matches the index condition - SYNOPSIS - bka_skip_index_tuple() - rseq value returned by bka_range_seq_init() - range_info record chain for the next range/key returned by MRR - - DESCRIPTION - This is wrapper for JOIN_CACHE_BKA::skip_index_tuple method, - see comments there. +SYNOPSIS + bka_skip_index_tuple() + rseq value returned by bka_range_seq_init() + range_info record chain for the next range/key returned by MRR + +DESCRIPTION + This is wrapper for JOIN_CACHE_BKA::skip_index_tuple method, + see comments there. - NOTE - This function is used as a RANGE_SEQ_IF::skip_index_tuple callback. - - RETURN VALUE - 0 The record combination satisfies the index condition - 1 Otherwise +NOTE + This function is used as a RANGE_SEQ_IF::skip_index_tuple callback. + +RETURN VALUE + 0 The record combination satisfies the index condition + 1 Otherwise */ static bool bka_skip_index_tuple(range_seq_t rseq, range_id_t range_info) { - DBUG_ENTER("bka_skip_index_tuple"); - JOIN_CACHE_BKA *cache= (JOIN_CACHE_BKA *) rseq; - THD *thd= cache->thd(); - bool res; - status_var_increment(thd->status_var.ha_icp_attempts); - if (!(res= cache->skip_index_tuple(range_info))) - status_var_increment(thd->status_var.ha_icp_match); - DBUG_RETURN(res); +DBUG_ENTER("bka_skip_index_tuple"); +JOIN_CACHE_BKA *cache= (JOIN_CACHE_BKA *) rseq; +THD *thd= cache->thd(); +bool res; +status_var_increment(thd->status_var.ha_icp_attempts); +if (!(res= cache->skip_index_tuple(range_info))) + status_var_increment(thd->status_var.ha_icp_match); +DBUG_RETURN(res); } /* - Prepare to read the record from BKA cache matching the current joined record - - SYNOPSIS - prepare_look_for_matches() - skip_last <-> ignore the last record in the buffer (always unused here) - - DESCRIPTION - The function prepares to iterate over records in the join cache buffer - matching the record loaded into the record buffer for join_tab when - performing join operation by BKA join algorithm. With BKA algorithms the - record loaded into the record buffer for join_tab always has a direct - reference to the matching records from the join buffer. When the regular - BKA join algorithm is employed the record from join_tab can refer to - only one such record. - The function sets the counter of the remaining records from the cache - buffer that would match the current join_tab record to 1. - - RETURN VALUE - TRUE there are no records in the buffer to iterate over - FALSE otherwise +Prepare to read the record from BKA cache matching the current joined record + +SYNOPSIS + prepare_look_for_matches() + skip_last <-> ignore the last record in the buffer (always unused here) + +DESCRIPTION + The function prepares to iterate over records in the join cache buffer + matching the record loaded into the record buffer for join_tab when + performing join operation by BKA join algorithm. With BKA algorithms the + record loaded into the record buffer for join_tab always has a direct + reference to the matching records from the join buffer. When the regular + BKA join algorithm is employed the record from join_tab can refer to + only one such record. + The function sets the counter of the remaining records from the cache + buffer that would match the current join_tab record to 1. + +RETURN VALUE + TRUE there are no records in the buffer to iterate over + FALSE otherwise */ - + bool JOIN_CACHE_BKA::prepare_look_for_matches(bool skip_last) { - if (!records) - return TRUE; - rem_records= 1; - return FALSE; +if (!records) + return TRUE; +rem_records= 1; +return FALSE; } /* - Get the record from the BKA cache matching the current joined record - - SYNOPSIS - get_next_candidate_for_match - - DESCRIPTION - This method is used for iterations over the records from the join - cache buffer when looking for matches for records from join_tab. - The method performs the necessary preparations to read the next record - from the join buffer into the record buffer by the method - read_next_candidate_for_match, or, to skip the next record from the join - buffer by the method skip_if_not_needed_match. - This implementation of the virtual method get_next_candidate_for_match - just decrements the counter of the records that are to be iterated over - and returns the value of curr_association as a reference to the position - of the beginning of the record fields in the buffer. - - RETURN VALUE - pointer to the start of the record fields in the join buffer - if the there is another record to iterate over, 0 - otherwise. +Get the record from the BKA cache matching the current joined record + +SYNOPSIS + get_next_candidate_for_match + +DESCRIPTION + This method is used for iterations over the records from the join + cache buffer when looking for matches for records from join_tab. + The method performs the necessary preparations to read the next record + from the join buffer into the record buffer by the method + read_next_candidate_for_match, or, to skip the next record from the join + buffer by the method skip_if_not_needed_match. + This implementation of the virtual method get_next_candidate_for_match + just decrements the counter of the records that are to be iterated over + and returns the value of curr_association as a reference to the position + of the beginning of the record fields in the buffer. + +RETURN VALUE + pointer to the start of the record fields in the join buffer + if the there is another record to iterate over, 0 - otherwise. */ uchar *JOIN_CACHE_BKA::get_next_candidate_for_match() { - if (!rem_records) - return 0; - rem_records--; - return curr_association; +if (!rem_records) + return 0; +rem_records--; +return curr_association; } /* - Check whether the matching record from the BKA cache is to be skipped - - SYNOPSIS - skip_next_candidate_for_match - rec_ptr pointer to the position in the join buffer right after - the previous record - - DESCRIPTION - This implementation of the virtual function just calls the - method get_match_flag_by_pos to check whether the record referenced - by ref_ptr has its match flag set to MATCH_FOUND. - - RETURN VALUE - TRUE the record referenced by rec_ptr has its match flag set to - MATCH_FOUND - FALSE otherwise +Check whether the matching record from the BKA cache is to be skipped + +SYNOPSIS + skip_next_candidate_for_match + rec_ptr pointer to the position in the join buffer right after + the previous record + +DESCRIPTION + This implementation of the virtual function just calls the + method get_match_flag_by_pos to check whether the record referenced + by ref_ptr has its match flag set to MATCH_FOUND. + +RETURN VALUE + TRUE the record referenced by rec_ptr has its match flag set to + MATCH_FOUND + FALSE otherwise */ bool JOIN_CACHE_BKA::skip_next_candidate_for_match(uchar *rec_ptr) { - return join_tab->check_only_first_match() && - (get_match_flag_by_pos(rec_ptr) == MATCH_FOUND); +return join_tab->check_only_first_match() && + (get_match_flag_by_pos(rec_ptr) == MATCH_FOUND); } /* - Read the next record from the BKA join cache buffer when looking for matches - - SYNOPSIS - read_next_candidate_for_match - rec_ptr pointer to the position in the join buffer right after - the previous record - - DESCRIPTION - This implementation of the virtual method read_next_candidate_for_match - calls the method get_record_by_pos to read the record referenced by rec_ptr - from the join buffer into the record buffer. If this record refers to - fields in the other join buffers the call of get_record_by_po ensures that - these fields are read into the corresponding record buffers as well. - This function is supposed to be called after a successful call of - the method get_next_candidate_for_match. - - RETURN VALUE - none +Read the next record from the BKA join cache buffer when looking for matches + +SYNOPSIS + read_next_candidate_for_match + rec_ptr pointer to the position in the join buffer right after + the previous record + +DESCRIPTION + This implementation of the virtual method read_next_candidate_for_match + calls the method get_record_by_pos to read the record referenced by rec_ptr + from the join buffer into the record buffer. If this record refers to + fields in the other join buffers the call of get_record_by_po ensures that + these fields are read into the corresponding record buffers as well. + This function is supposed to be called after a successful call of + the method get_next_candidate_for_match. + +RETURN VALUE + none */ void JOIN_CACHE_BKA::read_next_candidate_for_match(uchar *rec_ptr) { - get_record_by_pos(rec_ptr); +get_record_by_pos(rec_ptr); } /* - Initialize the BKA join cache +Initialize the BKA join cache - SYNOPSIS - init +SYNOPSIS + init - DESCRIPTION - The function initializes the cache structure. It is supposed to be called - right after a constructor for the JOIN_CACHE_BKA. +DESCRIPTION + The function initializes the cache structure. It is supposed to be called + right after a constructor for the JOIN_CACHE_BKA. - NOTES - The function first constructs a companion object of the type - JOIN_TAB_SCAN_MRR, then it calls the init method of the parent class. - - RETURN VALUE - 0 initialization with buffer allocations has been succeeded - 1 otherwise +NOTES + The function first constructs a companion object of the type + JOIN_TAB_SCAN_MRR, then it calls the init method of the parent class. + +RETURN VALUE + 0 initialization with buffer allocations has been succeeded + 1 otherwise */ int JOIN_CACHE_BKA::init() { - int res; - bool check_only_first_match= join_tab->check_only_first_match(); +int res; +bool check_only_first_match= join_tab->check_only_first_match(); - RANGE_SEQ_IF rs_funcs= { bka_range_seq_key_info, - bka_range_seq_init, - bka_range_seq_next, - check_only_first_match ? - bka_range_seq_skip_record : 0, - bka_skip_index_tuple }; +RANGE_SEQ_IF rs_funcs= { bka_range_seq_key_info, + bka_range_seq_init, + bka_range_seq_next, + check_only_first_match ? + bka_range_seq_skip_record : 0, + bka_skip_index_tuple }; - DBUG_ENTER("JOIN_CACHE_BKA::init"); +DBUG_ENTER("JOIN_CACHE_BKA::init"); - JOIN_TAB_SCAN_MRR *jsm; - if (!(join_tab_scan= jsm= new JOIN_TAB_SCAN_MRR(join, join_tab, - mrr_mode, rs_funcs))) - DBUG_RETURN(1); +JOIN_TAB_SCAN_MRR *jsm; +if (!(join_tab_scan= jsm= new JOIN_TAB_SCAN_MRR(join, join_tab, + mrr_mode, rs_funcs))) + DBUG_RETURN(1); - if ((res= JOIN_CACHE::init())) - DBUG_RETURN(res); +if ((res= JOIN_CACHE::init())) + DBUG_RETURN(res); - if (use_emb_key) - jsm->mrr_mode |= HA_MRR_MATERIALIZED_KEYS; +if (use_emb_key) + jsm->mrr_mode |= HA_MRR_MATERIALIZED_KEYS; - DBUG_RETURN(0); +DBUG_RETURN(0); } /* - Get the key built over the next record from BKA join buffer - - SYNOPSIS - get_next_key() - key pointer to the buffer where the key value is to be placed - - DESCRIPTION - The function reads key fields from the current record in the join buffer. - and builds the key value out of these fields that will be used to access - the 'join_tab' table. Some of key fields may belong to previous caches. - They are accessed via record references to the record parts stored in the - previous join buffers. The other key fields always are placed right after - the flag fields of the record. - If the key is embedded, which means that its value can be read directly - from the join buffer, then *key is set to the beginning of the key in - this buffer. Otherwise the key is built in the join_tab->ref->key_buff. - The function returns the length of the key if it succeeds ro read it. - If is assumed that the functions starts reading at the position of - the record length which is provided for each records in a BKA cache. - After the key is built the 'pos' value points to the first position after - the current record. - The function just skips the records with MATCH_IMPOSSIBLE in the - match flag field if there is any. - The function returns 0 if the initial position is after the beginning - of the record fields for last record from the join buffer. - - RETURN VALUE - length of the key value - if the starting value of 'pos' points to - the position before the fields for the last record, - 0 - otherwise. +Get the key built over the next record from BKA join buffer + +SYNOPSIS + get_next_key() + key pointer to the buffer where the key value is to be placed + +DESCRIPTION + The function reads key fields from the current record in the join buffer. + and builds the key value out of these fields that will be used to access + the 'join_tab' table. Some of key fields may belong to previous caches. + They are accessed via record references to the record parts stored in the + previous join buffers. The other key fields always are placed right after + the flag fields of the record. + If the key is embedded, which means that its value can be read directly + from the join buffer, then *key is set to the beginning of the key in + this buffer. Otherwise the key is built in the join_tab->ref->key_buff. + The function returns the length of the key if it succeeds ro read it. + If is assumed that the functions starts reading at the position of + the record length which is provided for each records in a BKA cache. + After the key is built the 'pos' value points to the first position after + the current record. + The function just skips the records with MATCH_IMPOSSIBLE in the + match flag field if there is any. + The function returns 0 if the initial position is after the beginning + of the record fields for last record from the join buffer. + +RETURN VALUE + length of the key value - if the starting value of 'pos' points to + the position before the fields for the last record, + 0 - otherwise. */ uint JOIN_CACHE_BKA::get_next_key(uchar ** key) { - uint len; - uint32 rec_len; - uchar *init_pos; - JOIN_CACHE *cache; - +uint len; +uint32 rec_len; +uchar *init_pos; +JOIN_CACHE *cache; + start: - /* Any record in a BKA cache is prepended with its length */ - DBUG_ASSERT(with_length); - - if ((pos+size_of_rec_len) > last_rec_pos || !records) - return 0; +/* Any record in a BKA cache is prepended with its length */ +DBUG_ASSERT(with_length); + +if ((pos+size_of_rec_len) > last_rec_pos || !records) + return 0; - /* Read the length of the record */ - rec_len= get_rec_length(pos); - pos+= size_of_rec_len; - init_pos= pos; +/* Read the length of the record */ +rec_len= get_rec_length(pos); +pos+= size_of_rec_len; +init_pos= pos; - /* Read a reference to the previous cache if any */ - if (prev_cache) - pos+= prev_cache->get_size_of_rec_offset(); +/* Read a reference to the previous cache if any */ +if (prev_cache) + pos+= prev_cache->get_size_of_rec_offset(); - curr_rec_pos= pos; +curr_rec_pos= pos; - /* Read all flag fields of the record */ - read_flag_fields(); +/* Read all flag fields of the record */ +read_flag_fields(); - if (with_match_flag && - (Match_flag) curr_rec_pos[0] == MATCH_IMPOSSIBLE ) - { - pos= init_pos+rec_len; - goto start; - } - - if (use_emb_key) - { - /* An embedded key is taken directly from the join buffer */ - *key= pos; - len= emb_key_length; - } - else +if (with_match_flag && + (Match_flag) curr_rec_pos[0] == MATCH_IMPOSSIBLE ) +{ + pos= init_pos+rec_len; + goto start; +} + +if (use_emb_key) +{ + /* An embedded key is taken directly from the join buffer */ + *key= pos; + len= emb_key_length; +} +else +{ + /* Read key arguments from previous caches if there are any such fields */ + if (external_key_arg_fields) { - /* Read key arguments from previous caches if there are any such fields */ - if (external_key_arg_fields) - { - uchar *rec_ptr= curr_rec_pos; - uint key_arg_count= external_key_arg_fields; - CACHE_FIELD **copy_ptr= blob_ptr-key_arg_count; - for (cache= prev_cache; key_arg_count; cache= cache->prev_cache) - { - uint len= 0; + uchar *rec_ptr= curr_rec_pos; + uint key_arg_count= external_key_arg_fields; + CACHE_FIELD **copy_ptr= blob_ptr-key_arg_count; + for (cache= prev_cache; key_arg_count; cache= cache->prev_cache) + { + uint len= 0; + DBUG_ASSERT(cache); + rec_ptr= cache->get_rec_ref(rec_ptr); + while (!cache->referenced_fields) + { + cache= cache->prev_cache; DBUG_ASSERT(cache); rec_ptr= cache->get_rec_ref(rec_ptr); - while (!cache->referenced_fields) - { - cache= cache->prev_cache; - DBUG_ASSERT(cache); - rec_ptr= cache->get_rec_ref(rec_ptr); - } - while (key_arg_count && - cache->read_referenced_field(*copy_ptr, rec_ptr, &len)) - { - copy_ptr++; - --key_arg_count; - } + } + while (key_arg_count && + cache->read_referenced_field(*copy_ptr, rec_ptr, &len)) + { + copy_ptr++; + --key_arg_count; } } - - /* - Read the other key arguments from the current record. The fields for - these arguments are always first in the sequence of the record's fields. - */ - CACHE_FIELD *copy= field_descr+flag_fields; - CACHE_FIELD *copy_end= copy+local_key_arg_fields; - bool blob_in_rec_buff= blob_data_is_in_rec_buff(curr_rec_pos); - for ( ; copy < copy_end; copy++) - read_record_field(copy, blob_in_rec_buff); - - /* Build the key over the fields read into the record buffers */ - TABLE_REF *ref= &join_tab->ref; - cp_buffer_from_ref(join->thd, join_tab->table, ref); - *key= ref->key_buff; - len= ref->key_length; } + + /* + Read the other key arguments from the current record. The fields for + these arguments are always first in the sequence of the record's fields. + */ + CACHE_FIELD *copy= field_descr+flag_fields; + CACHE_FIELD *copy_end= copy+local_key_arg_fields; + bool blob_in_rec_buff= blob_data_is_in_rec_buff(curr_rec_pos); + for ( ; copy < copy_end; copy++) + read_record_field(copy, blob_in_rec_buff); + + /* Build the key over the fields read into the record buffers */ + TABLE_REF *ref= &join_tab->ref; + cp_buffer_from_ref(join->thd, join_tab->table, ref); + *key= ref->key_buff; + len= ref->key_length; +} - pos= init_pos+rec_len; +pos= init_pos+rec_len; - return len; +return len; } /* - Check the index condition of the joined table for a record from the BKA cache - - SYNOPSIS - skip_index_tuple() - range_info pointer to the record returned by MRR - - DESCRIPTION - This function is invoked from MRR implementation to check if an index - tuple matches the index condition. It is used in the case where the index - condition actually depends on both columns of the used index and columns - from previous tables. - - NOTES - Accessing columns of the previous tables requires special handling with - BKA. The idea of BKA is to collect record combinations in a buffer and - then do a batch of ref access lookups, i.e. by the time we're doing a - lookup its previous-records-combination is not in prev_table->record[0] - but somewhere in the join buffer. - We need to get it from there back into prev_table(s)->record[0] before we - can evaluate the index condition, and that's why we need this function - instead of regular IndexConditionPushdown. - - NOTES - Possible optimization: - Before we unpack the record from a previous table - check if this table is used in the condition. - If so then unpack the record otherwise skip the unpacking. - This should be done by a special virtual method - get_partial_record_by_pos(). +Check the index condition of the joined table for a record from the BKA cache - RETURN VALUE - 1 the record combination does not satisfies the index condition - 0 otherwise +SYNOPSIS + skip_index_tuple() + range_info pointer to the record returned by MRR + +DESCRIPTION + This function is invoked from MRR implementation to check if an index + tuple matches the index condition. It is used in the case where the index + condition actually depends on both columns of the used index and columns + from previous tables. + +NOTES + Accessing columns of the previous tables requires special handling with + BKA. The idea of BKA is to collect record combinations in a buffer and + then do a batch of ref access lookups, i.e. by the time we're doing a + lookup its previous-records-combination is not in prev_table->record[0] + but somewhere in the join buffer. + We need to get it from there back into prev_table(s)->record[0] before we + can evaluate the index condition, and that's why we need this function + instead of regular IndexConditionPushdown. + +NOTES + Possible optimization: + Before we unpack the record from a previous table + check if this table is used in the condition. + If so then unpack the record otherwise skip the unpacking. + This should be done by a special virtual method + get_partial_record_by_pos(). + +RETURN VALUE + 1 the record combination does not satisfies the index condition + 0 otherwise */ bool JOIN_CACHE_BKA::skip_index_tuple(range_id_t range_info) { - DBUG_ENTER("JOIN_CACHE_BKA::skip_index_tuple"); - get_record_by_pos((uchar*)range_info); - DBUG_RETURN(!join_tab->cache_idx_cond->val_int()); +DBUG_ENTER("JOIN_CACHE_BKA::skip_index_tuple"); +get_record_by_pos((uchar*)range_info); +DBUG_RETURN(!join_tab->cache_idx_cond->val_int()); } /* - Initialize retrieval of range sequence for the BKAH join algorithm - - SYNOPSIS - bkah_range_seq_init() - init_params pointer to the BKAH join cache object - n_ranges the number of ranges obtained - flags combination of MRR flags - - DESCRIPTION - The function interprets init_param as a pointer to a JOIN_CACHE_BKAH - object. The function prepares for an iteration over distinct join keys - built over the records from the cache join buffer. - - NOTE - This function are used only as a callback function. - - RETURN VALUE - init_param value that is to be used as a parameter of - bkah_range_seq_next() +Initialize retrieval of range sequence for the BKAH join algorithm + +SYNOPSIS + bkah_range_seq_init() + init_params pointer to the BKAH join cache object + n_ranges the number of ranges obtained + flags combination of MRR flags + +DESCRIPTION + The function interprets init_param as a pointer to a JOIN_CACHE_BKAH + object. The function prepares for an iteration over distinct join keys + built over the records from the cache join buffer. + +NOTE + This function are used only as a callback function. + +RETURN VALUE + init_param value that is to be used as a parameter of + bkah_range_seq_next() */ static range_seq_t bkah_range_seq_init(void *init_param, uint n_ranges, uint flags) { - DBUG_ENTER("bkah_range_seq_init"); - JOIN_CACHE_BKAH *cache= (JOIN_CACHE_BKAH *) init_param; - cache->reset(0); - DBUG_RETURN((range_seq_t) init_param); +DBUG_ENTER("bkah_range_seq_init"); +JOIN_CACHE_BKAH *cache= (JOIN_CACHE_BKAH *) init_param; +cache->reset(0); +DBUG_RETURN((range_seq_t) init_param); } /* - Get the next range/key over records from the join buffer of a BKAH cache - - SYNOPSIS - bkah_range_seq_next() - seq value returned by bkah_range_seq_init() - range OUT reference to the next range +Get the next range/key over records from the join buffer of a BKAH cache - DESCRIPTION - The function interprets seq as a pointer to a JOIN_CACHE_BKAH - object. The function returns a pointer to the range descriptor - for the next unique key built over records from the join buffer. - - NOTE - This function are used only as a callback function. - - RETURN VALUE - FALSE ok, the range structure filled with info about the next range/key - TRUE no more ranges +SYNOPSIS + bkah_range_seq_next() + seq value returned by bkah_range_seq_init() + range OUT reference to the next range + +DESCRIPTION + The function interprets seq as a pointer to a JOIN_CACHE_BKAH + object. The function returns a pointer to the range descriptor + for the next unique key built over records from the join buffer. + +NOTE + This function are used only as a callback function. + +RETURN VALUE + FALSE ok, the range structure filled with info about the next range/key + TRUE no more ranges */ static bool bkah_range_seq_next(range_seq_t rseq, KEY_MULTI_RANGE *range) { - DBUG_ENTER("bkah_range_seq_next"); - JOIN_CACHE_BKAH *cache= (JOIN_CACHE_BKAH *) rseq; - TABLE_REF *ref= &cache->join_tab->ref; - key_range *start_key= &range->start_key; - if ((start_key->length= cache->get_next_key((uchar **) &start_key->key))) - { - start_key->keypart_map= (1 << ref->key_parts) - 1; - start_key->flag= HA_READ_KEY_EXACT; - range->end_key= *start_key; - range->end_key.flag= HA_READ_AFTER_KEY; - range->ptr= (char *) cache->get_curr_key_chain(); - range->range_flag= EQ_RANGE; - DBUG_RETURN(0); - } - DBUG_RETURN(1); +DBUG_ENTER("bkah_range_seq_next"); +JOIN_CACHE_BKAH *cache= (JOIN_CACHE_BKAH *) rseq; +TABLE_REF *ref= &cache->join_tab->ref; +key_range *start_key= &range->start_key; +if ((start_key->length= cache->get_next_key((uchar **) &start_key->key))) +{ + start_key->keypart_map= (1 << ref->key_parts) - 1; + start_key->flag= HA_READ_KEY_EXACT; + range->end_key= *start_key; + range->end_key.flag= HA_READ_AFTER_KEY; + range->ptr= (char *) cache->get_curr_key_chain(); + range->range_flag= EQ_RANGE; + DBUG_RETURN(0); +} +DBUG_RETURN(1); } /* - Check whether range_info orders to skip the next record from BKAH join buffer +Check whether range_info orders to skip the next record from BKAH join buffer - SYNOPSIS - bkah_range_seq_skip_record() - seq value returned by bkah_range_seq_init() - range_info information about the next range/key returned by MRR - rowid [NOT USED] rowid of the record to be checked (not used) - - DESCRIPTION - The function interprets seq as a pointer to a JOIN_CACHE_BKAH - object. The function returns TRUE if the record with this range_info - is to be filtered out from the stream of records returned by - multi_range_read_next(). - - NOTE - This function are used only as a callback function. - - RETURN VALUE - 1 record with this range_info is to be filtered out from the stream - of records returned by multi_range_read_next() - 0 the record is to be left in the stream +SYNOPSIS + bkah_range_seq_skip_record() + seq value returned by bkah_range_seq_init() + range_info information about the next range/key returned by MRR + rowid [NOT USED] rowid of the record to be checked (not used) + +DESCRIPTION + The function interprets seq as a pointer to a JOIN_CACHE_BKAH + object. The function returns TRUE if the record with this range_info + is to be filtered out from the stream of records returned by + multi_range_read_next(). + +NOTE + This function are used only as a callback function. + +RETURN VALUE + 1 record with this range_info is to be filtered out from the stream + of records returned by multi_range_read_next() + 0 the record is to be left in the stream */ static bool bkah_range_seq_skip_record(range_seq_t rseq, range_id_t range_info, - uchar *rowid) + uchar *rowid) { - DBUG_ENTER("bkah_range_seq_skip_record"); - JOIN_CACHE_BKAH *cache= (JOIN_CACHE_BKAH *) rseq; - bool res= cache->check_all_match_flags_for_key((uchar *) range_info); - DBUG_RETURN(res); +DBUG_ENTER("bkah_range_seq_skip_record"); +JOIN_CACHE_BKAH *cache= (JOIN_CACHE_BKAH *) rseq; +bool res= cache->check_all_match_flags_for_key((uchar *) range_info); +DBUG_RETURN(res); } - + /* - Check if the record combination from BKAH cache matches the index condition +Check if the record combination from BKAH cache matches the index condition - SYNOPSIS - bkah_skip_index_tuple() - rseq value returned by bka_range_seq_init() - range_info record chain for the next range/key returned by MRR - - DESCRIPTION - This is wrapper for JOIN_CACHE_BKA_UNIQUE::skip_index_tuple method, - see comments there. +SYNOPSIS + bkah_skip_index_tuple() + rseq value returned by bka_range_seq_init() + range_info record chain for the next range/key returned by MRR + +DESCRIPTION + This is wrapper for JOIN_CACHE_BKA_UNIQUE::skip_index_tuple method, + see comments there. - NOTE - This function is used as a RANGE_SEQ_IF::skip_index_tuple callback. - - RETURN VALUE - 0 some records from the chain satisfy the index condition - 1 otherwise +NOTE + This function is used as a RANGE_SEQ_IF::skip_index_tuple callback. + +RETURN VALUE + 0 some records from the chain satisfy the index condition + 1 otherwise */ static bool bkah_skip_index_tuple(range_seq_t rseq, range_id_t range_info) { - DBUG_ENTER("bka_unique_skip_index_tuple"); - JOIN_CACHE_BKAH *cache= (JOIN_CACHE_BKAH *) rseq; - THD *thd= cache->thd(); - bool res; - status_var_increment(thd->status_var.ha_icp_attempts); - if (!(res= cache->skip_index_tuple(range_info))) - status_var_increment(thd->status_var.ha_icp_match); - DBUG_RETURN(res); +DBUG_ENTER("bka_unique_skip_index_tuple"); +JOIN_CACHE_BKAH *cache= (JOIN_CACHE_BKAH *) rseq; +THD *thd= cache->thd(); +bool res; +status_var_increment(thd->status_var.ha_icp_attempts); +if (!(res= cache->skip_index_tuple(range_info))) + status_var_increment(thd->status_var.ha_icp_match); +DBUG_RETURN(res); } /* - Prepare to read record from BKAH cache matching the current joined record - - SYNOPSIS - prepare_look_for_matches() - skip_last <-> ignore the last record in the buffer (always unused here) - - DESCRIPTION - The function prepares to iterate over records in the join cache buffer - matching the record loaded into the record buffer for join_tab when - performing join operation by BKAH join algorithm. With BKAH algorithm, if - association labels are used, then record loaded into the record buffer - for join_tab always has a direct reference to the chain of the mathing - records from the join buffer. If association labels are not used then - then the chain of the matching records is obtained by the call of the - get_key_chain_by_join_key function. - - RETURN VALUE - TRUE there are no records in the buffer to iterate over - FALSE otherwise +Prepare to read record from BKAH cache matching the current joined record + +SYNOPSIS + prepare_look_for_matches() + skip_last <-> ignore the last record in the buffer (always unused here) + +DESCRIPTION + The function prepares to iterate over records in the join cache buffer + matching the record loaded into the record buffer for join_tab when + performing join operation by BKAH join algorithm. With BKAH algorithm, if + association labels are used, then record loaded into the record buffer + for join_tab always has a direct reference to the chain of the mathing + records from the join buffer. If association labels are not used then + then the chain of the matching records is obtained by the call of the + get_key_chain_by_join_key function. + +RETURN VALUE + TRUE there are no records in the buffer to iterate over + FALSE otherwise */ - + bool JOIN_CACHE_BKAH::prepare_look_for_matches(bool skip_last) { - last_matching_rec_ref_ptr= next_matching_rec_ref_ptr= 0; - if (no_association && - !(curr_matching_chain= get_matching_chain_by_join_key())) +last_matching_rec_ref_ptr= next_matching_rec_ref_ptr= 0; +if (no_association && + !(curr_matching_chain= get_matching_chain_by_join_key())) //psergey: added '!' return 1; last_matching_rec_ref_ptr= get_next_rec_ref(curr_matching_chain); return 0; diff --git a/sql/sql_lex.cc b/sql/sql_lex.cc index 1c07f0e23c2..82cdd4ead7b 100644 --- a/sql/sql_lex.cc +++ b/sql/sql_lex.cc @@ -1,4 +1,5 @@ /* Copyright (c) 2000, 2012, Oracle and/or its affiliates. + Copyright (c) 2009, 2013, Monty Program Ab. 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 @@ -1883,6 +1884,7 @@ void st_select_lex::init_query() ref_pointer_array= 0; select_n_where_fields= 0; select_n_having_items= 0; + n_child_sum_items= 0; subquery_in_having= explicit_limit= 0; is_item_list_lookup= 0; first_execution= 1; @@ -2559,7 +2561,7 @@ LEX::LEX() my_init_dynamic_array2(&plugins, sizeof(plugin_ref), plugins_static_buffer, INITIAL_LEX_PLUGIN_LIST_SIZE, - INITIAL_LEX_PLUGIN_LIST_SIZE); + INITIAL_LEX_PLUGIN_LIST_SIZE, 0); reset_query_tables_list(TRUE); mi.init(); } diff --git a/sql/sql_lex.h b/sql/sql_lex.h index 2390f207aa4..cc117e18d1e 100644 --- a/sql/sql_lex.h +++ b/sql/sql_lex.h @@ -226,7 +226,7 @@ struct LEX_MASTER_INFO { bzero(this, sizeof(*this)); my_init_dynamic_array(&repl_ignore_server_ids, - sizeof(::server_id), 0, 16); + sizeof(::server_id), 0, 16, MYF(0)); } void reset() { diff --git a/sql/sql_list.h b/sql/sql_list.h index 2b2d9e59771..b4e0ab84aab 100644 --- a/sql/sql_list.h +++ b/sql/sql_list.h @@ -1,7 +1,6 @@ #ifndef INCLUDES_MYSQL_SQL_LIST_H #define INCLUDES_MYSQL_SQL_LIST_H -/* - Copyright (c) 2000, 2010, Oracle and/or its affiliates. +/* Copyright (c) 2000, 2012, Oracle and/or its affiliates. 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 @@ -169,6 +168,14 @@ protected: public: uint elements; + bool operator==(const base_list &rhs) const + { + return + elements == rhs.elements && + first == rhs.first && + last == rhs.last; + } + inline void empty() { elements=0; first= &end_of_list; last=&first;} inline base_list() { empty(); } /** diff --git a/sql/sql_load.cc b/sql/sql_load.cc index 38853eb8b6d..11e23b56f71 100644 --- a/sql/sql_load.cc +++ b/sql/sql_load.cc @@ -725,14 +725,14 @@ static bool write_execute_load_query_log_event(THD *thd, sql_exchange* ex, List_iterator<Item> lu(thd->lex->update_list); List_iterator<Item> lv(thd->lex->value_list); - query_str.append(" SET "); + query_str.append(STRING_WITH_LEN(" SET ")); n= 0; while ((item= lu++)) { val= lv++; if (n++) - query_str.append(", "); + query_str.append(STRING_WITH_LEN(", ")); append_identifier(thd, &query_str, item->name, strlen(item->name)); query_str.append(val->name); } @@ -1368,7 +1368,7 @@ READ_INFO::READ_INFO(File file_par, uint tot_length, CHARSET_INFO *cs, set_if_bigger(length,line_start.length()); stack=stack_pos=(int*) sql_alloc(sizeof(int)*length); - if (!(buffer=(uchar*) my_malloc(buff_length+1,MYF(0)))) + if (!(buffer=(uchar*) my_malloc(buff_length+1,MYF(MY_THREAD_SPECIFIC)))) error=1; /* purecov: inspected */ else { @@ -1376,7 +1376,7 @@ READ_INFO::READ_INFO(File file_par, uint tot_length, CHARSET_INFO *cs, if (init_io_cache(&cache,(get_it_from_net) ? -1 : file, 0, (get_it_from_net) ? READ_NET : (is_fifo ? READ_FIFO : READ_CACHE),0L,1, - MYF(MY_WME))) + MYF(MY_WME | MY_THREAD_SPECIFIC))) { my_free(buffer); /* purecov: inspected */ buffer= NULL; @@ -1602,7 +1602,7 @@ int READ_INFO::read_field() ** We come here if buffer is too small. Enlarge it and continue */ if (!(new_buffer=(uchar*) my_realloc((char*) buffer,buff_length+1+IO_SIZE, - MYF(MY_WME)))) + MYF(MY_WME | MY_THREAD_SPECIFIC)))) return (error=1); to=new_buffer + (to-buffer); buffer=new_buffer; diff --git a/sql/sql_parse.cc b/sql/sql_parse.cc index 0690810a6dc..f1362674d0c 100644 --- a/sql/sql_parse.cc +++ b/sql/sql_parse.cc @@ -801,8 +801,6 @@ void do_handle_bootstrap(THD *thd) handle_bootstrap_impl(thd); end: - net_end(&thd->net); - thd->cleanup(); delete thd; #ifndef EMBEDDED_LIBRARY @@ -1173,7 +1171,18 @@ bool dispatch_command(enum enum_server_command command, THD *thd, thd->security_ctx->user= 0; thd->user_connect= 0; - rc= acl_authenticate(thd, 0, packet_length); + /* + to limit COM_CHANGE_USER ability to brute-force passwords, + we only allow three unsuccessful COM_CHANGE_USER per connection. + */ + if (thd->failed_com_change_user >= 3) + { + my_message(ER_UNKNOWN_COM_ERROR, ER(ER_UNKNOWN_COM_ERROR), MYF(0)); + rc= 1; + } + else + rc= acl_authenticate(thd, 0, packet_length); + MYSQL_AUDIT_NOTIFY_CONNECTION_CHANGE_USER(thd); if (rc) { @@ -1188,6 +1197,8 @@ bool dispatch_command(enum enum_server_command command, THD *thd, thd->variables.collation_connection= save_collation_connection; thd->variables.character_set_results= save_character_set_results; thd->update_charset(); + thd->failed_com_change_user++; + my_sleep(1000000); } else { @@ -1489,10 +1500,10 @@ bool dispatch_command(enum enum_server_command command, THD *thd, and flushes tables. */ bool res; - my_pthread_setspecific_ptr(THR_THD, NULL); + set_current_thd(0); res= reload_acl_and_cache(NULL, options | REFRESH_FAST, NULL, ¬_used); - my_pthread_setspecific_ptr(THR_THD, thd); + set_current_thd(thd); if (res) break; } @@ -1663,6 +1674,7 @@ bool dispatch_command(enum enum_server_command command, THD *thd, THD_STAGE_INFO(thd, stage_cleaning_up); thd->reset_query(); + thd->set_examined_row_count(0); // For processlist thd->set_command(COM_SLEEP); /* Performance Schema Interface instrumentation, end */ @@ -2595,6 +2607,7 @@ case SQLCOM_PREPARE: LEX_MASTER_INFO *lex_mi= &thd->lex->mi; Master_info *mi; bool new_master= 0; + bool master_info_added; if (check_global_access(thd, SUPER_ACL)) goto error; @@ -2617,15 +2630,19 @@ case SQLCOM_PREPARE: new_master= 1; } - res= change_master(thd, mi); + res= change_master(thd, mi, &master_info_added); if (res && new_master) { /* - The new master was added by change_master(). Remove it as it didn't - work. + If the new master was added by change_master(), remove it as it didn't + work (this will free mi as well). + + If new master was not added, we still need to free mi. */ - master_info_index->remove_master_info(&lex_mi->connection_name); - delete mi; + if (master_info_added) + master_info_index->remove_master_info(&lex_mi->connection_name); + else + delete mi; } mysql_mutex_unlock(&LOCK_active_mi); @@ -4852,16 +4869,20 @@ finish: if (! thd->in_sub_stmt) { - /* report error issued during command execution */ - if (thd->killed_errno()) + if (thd->killed != NOT_KILLED) { - if (! thd->stmt_da->is_set()) - thd->send_kill_message(); - } - if (thd->killed < KILL_CONNECTION) - { - thd->killed= NOT_KILLED; - thd->mysys_var->abort= 0; + /* report error issued during command execution */ + if (thd->killed_errno()) + { + /* If we already sent 'ok', we can ignore any kill query statements */ + if (! thd->stmt_da->is_set()) + thd->send_kill_message(); + } + if (thd->killed < KILL_CONNECTION) + { + thd->reset_killed(); + thd->mysys_var->abort= 0; + } } if (thd->is_error() || (thd->variables.option_bits & OPTION_MASTER_SQL_ERROR)) trans_rollback_stmt(thd); @@ -5011,7 +5032,8 @@ static bool execute_show_status(THD *thd, TABLE_LIST *all_tables) mysql_mutex_lock(&LOCK_status); add_diff_to_status(&global_status_var, &thd->status_var, &old_status_var); - thd->status_var= old_status_var; + memcpy(&thd->status_var, &old_status_var, + offsetof(STATUS_VAR, last_cleared_system_status_var)); mysql_mutex_unlock(&LOCK_status); return res; } @@ -6494,8 +6516,13 @@ TABLE_LIST *st_select_lex::add_table_to_list(THD *thd, ptr->next_name_resolution_table= NULL; /* Link table in global list (all used tables) */ lex->add_to_query_tables(ptr); - ptr->mdl_request.init(MDL_key::TABLE, ptr->db, ptr->table_name, mdl_type, - MDL_TRANSACTION); + + // Pure table aliases do not need to be locked: + if (!test(table_options & TL_OPTION_ALIAS)) + { + ptr->mdl_request.init(MDL_key::TABLE, ptr->db, ptr->table_name, mdl_type, + MDL_TRANSACTION); + } DBUG_RETURN(ptr); } @@ -7071,7 +7098,7 @@ static uint kill_threads_for_user(THD *thd, LEX_USER *user, mysql_mutex_unlock(&LOCK_thread_count); DBUG_RETURN(ER_KILL_DENIED_ERROR); } - if (!threads_to_kill.push_back(tmp, tmp->mem_root)) + if (!threads_to_kill.push_back(tmp, thd->mem_root)) mysql_mutex_lock(&tmp->LOCK_thd_data); // Lock from delete } } diff --git a/sql/sql_plugin.cc b/sql/sql_plugin.cc index 73539f70762..8778713d7e7 100644 --- a/sql/sql_plugin.cc +++ b/sql/sql_plugin.cc @@ -1,6 +1,6 @@ /* Copyright (c) 2005, 2012, Oracle and/or its affiliates. - Copyright (c) 2010, 2011, Monty Program Ab + Copyright (c) 2010, 2013, Monty Program Ab 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 @@ -1116,7 +1116,7 @@ static bool plugin_add(MEM_ROOT *tmp_root, plugin_array_version++; if (my_hash_insert(&plugin_hash[plugin->type], (uchar*)tmp_plugin_ptr)) tmp_plugin_ptr->state= PLUGIN_IS_FREED; - init_alloc_root(&tmp_plugin_ptr->mem_root, 4096, 4096); + init_alloc_root(&tmp_plugin_ptr->mem_root, 4096, 4096, MYF(0)); if (name->str) DBUG_RETURN(FALSE); // all done @@ -1511,8 +1511,8 @@ int plugin_init(int *argc, char **argv, int flags) init_plugin_psi_keys(); #endif - init_alloc_root(&plugin_mem_root, 4096, 4096); - init_alloc_root(&tmp_root, 4096, 4096); + init_alloc_root(&plugin_mem_root, 4096, 4096, MYF(0)); + init_alloc_root(&tmp_root, 4096, 4096, MYF(0)); if (my_hash_init(&bookmark_hash, &my_charset_bin, 16, 0, 0, get_bookmark_hash_key, NULL, HASH_UNIQUE)) @@ -1522,9 +1522,9 @@ int plugin_init(int *argc, char **argv, int flags) mysql_mutex_init(key_LOCK_plugin, &LOCK_plugin, MY_MUTEX_INIT_FAST); if (my_init_dynamic_array(&plugin_dl_array, - sizeof(struct st_plugin_dl *),16,16) || + sizeof(struct st_plugin_dl *), 16, 16, MYF(0)) || my_init_dynamic_array(&plugin_array, - sizeof(struct st_plugin_int *),16,16)) + sizeof(struct st_plugin_int *), 16, 16, MYF(0))) goto err; for (i= 0; i < MYSQL_MAX_PLUGIN_TYPE_NUM; i++) @@ -1723,12 +1723,11 @@ static bool register_builtin(struct st_maria_plugin *plugin, */ static void plugin_load(MEM_ROOT *tmp_root, int *argc, char **argv) { - THD thd; TABLE_LIST tables; TABLE *table; READ_RECORD read_record_info; int error; - THD *new_thd= &thd; + THD *new_thd= new THD; bool result; #ifdef EMBEDDED_LIBRARY No_such_table_error_handler error_handler; @@ -1739,7 +1738,7 @@ static void plugin_load(MEM_ROOT *tmp_root, int *argc, char **argv) new_thd->store_globals(); new_thd->db= my_strdup("mysql", MYF(0)); new_thd->db_length= 5; - bzero((char*) &thd.net, sizeof(thd.net)); + bzero((char*) &new_thd->net, sizeof(new_thd->net)); tables.init_one_table("mysql", 5, "plugin", 6, "plugin", TL_READ); #ifdef EMBEDDED_LIBRARY @@ -1806,7 +1805,8 @@ static void plugin_load(MEM_ROOT *tmp_root, int *argc, char **argv) close_mysql_tables(new_thd); end: /* Remember that we don't have a THD */ - my_pthread_setspecific_ptr(THR_THD, 0); + delete new_thd; + set_current_thd(0); DBUG_VOID_RETURN; } @@ -1882,8 +1882,11 @@ static bool plugin_load_list(MEM_ROOT *tmp_root, int *argc, char **argv, DBUG_RETURN(FALSE); error: mysql_mutex_unlock(&LOCK_plugin); - sql_print_error("Couldn't load plugin named '%s' with soname '%s'.", - name.str, dl.str); + if (name.str) + sql_print_error("Couldn't load plugin '%s' from '%s'.", + name.str, dl.str); + else + sql_print_error("Couldn't load plugins from '%s'.", dl.str); DBUG_RETURN(TRUE); } @@ -2028,7 +2031,7 @@ static bool finalize_install(THD *thd, TABLE *table, const LEX_STRING *name) struct st_plugin_int *tmp= plugin_find_internal(name, MYSQL_ANY_PLUGIN); int error; DBUG_ASSERT(tmp); - mysql_mutex_assert_owner(&LOCK_plugin); + mysql_mutex_assert_owner(&LOCK_plugin); // because of tmp->state if (tmp->state == PLUGIN_IS_DISABLED) { @@ -2115,8 +2118,12 @@ bool mysql_install_plugin(THD *thd, const LEX_STRING *name, This hack should be removed when LOCK_plugin is fixed so it protects only what it supposed to protect. + + See also mysql_uninstall_plugin() and initialize_audit_plugin() */ - mysql_audit_acquire_plugins(thd, MYSQL_AUDIT_GENERAL_CLASS); + unsigned long event_class_mask[MYSQL_AUDIT_CLASS_MASK_SIZE] = + { MYSQL_AUDIT_GENERAL_CLASSMASK }; + mysql_audit_acquire_plugins(thd, event_class_mask); mysql_mutex_lock(&LOCK_plugin); mysql_rwlock_wrlock(&LOCK_system_variables_hash); @@ -2259,8 +2266,15 @@ bool mysql_uninstall_plugin(THD *thd, const LEX_STRING *name, When audit event is triggered during [UN]INSTALL PLUGIN, plugin list iterator acquires the same lock (within the same thread) second time. + + This hack should be removed when LOCK_plugin is fixed so it + protects only what it supposed to protect. + + See also mysql_install_plugin() and initialize_audit_plugin() */ - mysql_audit_acquire_plugins(thd, MYSQL_AUDIT_GENERAL_CLASS); + unsigned long event_class_mask[MYSQL_AUDIT_CLASS_MASK_SIZE] = + { MYSQL_AUDIT_GENERAL_CLASSMASK }; + mysql_audit_acquire_plugins(thd, event_class_mask); mysql_mutex_lock(&LOCK_plugin); @@ -2270,11 +2284,19 @@ bool mysql_uninstall_plugin(THD *thd, const LEX_STRING *name, { fix_dl_name(thd->mem_root, &dl); st_plugin_dl *plugin_dl= plugin_dl_find(&dl); - struct st_maria_plugin *plugin; - for (plugin= plugin_dl->plugins; plugin->info; plugin++) + if (plugin_dl) { - LEX_STRING str= { const_cast<char*>(plugin->name), strlen(plugin->name) }; - error|= do_uninstall(thd, table, &str); + for (struct st_maria_plugin *plugin= plugin_dl->plugins; + plugin->info; plugin++) + { + LEX_STRING str= { const_cast<char*>(plugin->name), strlen(plugin->name) }; + error|= do_uninstall(thd, table, &str); + } + } + else + { + my_error(ER_SP_DOES_NOT_EXIST, MYF(0), "SONAME", dl.str); + error= true; } } reap_plugins(); diff --git a/sql/sql_plugin_services.h b/sql/sql_plugin_services.h index c779547059d..e3ef338eaad 100644 --- a/sql/sql_plugin_services.h +++ b/sql/sql_plugin_services.h @@ -54,13 +54,18 @@ static struct progress_report_service_st progress_report_handler= { set_thd_proc_info }; +static struct kill_statement_service_st thd_kill_statement_handler= { + thd_kill_level +}; + static struct st_service_ref list_of_services[]= { - { "my_snprintf_service", VERSION_my_snprintf, &my_snprintf_handler }, - { "thd_alloc_service", VERSION_thd_alloc, &thd_alloc_handler }, - { "thd_wait_service", VERSION_thd_wait, &thd_wait_handler }, + { "my_snprintf_service", VERSION_my_snprintf, &my_snprintf_handler }, + { "thd_alloc_service", VERSION_thd_alloc, &thd_alloc_handler }, + { "thd_wait_service", VERSION_thd_wait, &thd_wait_handler }, { "my_thread_scheduler_service", VERSION_my_thread_scheduler, &my_thread_scheduler_handler }, - { "progress_report_service", VERSION_progress_report, &progress_report_handler }, - { "debug_sync_service", VERSION_debug_sync, 0 } // updated in plugin_init() + { "progress_report_service", VERSION_progress_report, &progress_report_handler }, + { "debug_sync_service", VERSION_debug_sync, 0 }, // updated in plugin_init() + { "thd_kill_statement_service", VERSION_kill_statement, &thd_kill_statement_handler } }; diff --git a/sql/sql_prepare.cc b/sql/sql_prepare.cc index 24dde140a8a..025ff8820e6 100644 --- a/sql/sql_prepare.cc +++ b/sql/sql_prepare.cc @@ -3145,7 +3145,7 @@ Prepared_statement::Prepared_statement(THD *thd_arg) flags((uint) IS_IN_USE) { init_sql_alloc(&main_mem_root, thd_arg->variables.query_alloc_block_size, - thd_arg->variables.query_prealloc_size); + thd_arg->variables.query_prealloc_size, MYF(MY_THREAD_SPECIFIC)); *last_error= '\0'; } @@ -4039,7 +4039,7 @@ Ed_result_set::Ed_result_set(List<Ed_row> *rows_arg, */ Ed_connection::Ed_connection(THD *thd) - :m_warning_info(thd->query_id, false), + :m_warning_info(thd->query_id, false, true), m_thd(thd), m_rsets(0), m_current_rset(0) @@ -4463,7 +4463,7 @@ bool Protocol_local::send_result_set_metadata(List<Item> *columns, uint) { DBUG_ASSERT(m_rset == 0 && !alloc_root_inited(&m_rset_root)); - init_sql_alloc(&m_rset_root, MEM_ROOT_BLOCK_SIZE, 0); + init_sql_alloc(&m_rset_root, MEM_ROOT_BLOCK_SIZE, 0, MYF(MY_THREAD_SPECIFIC)); if (! (m_rset= new (&m_rset_root) List<Ed_row>)) return TRUE; diff --git a/sql/sql_priv.h b/sql/sql_priv.h index 39976e1a430..345556a0b7f 100644 --- a/sql/sql_priv.h +++ b/sql/sql_priv.h @@ -153,6 +153,41 @@ #define OPTION_ALLOW_BATCH (ULL(1) << 36) // THD, intern (slave) #define OPTION_SKIP_REPLICATION (ULL(1) << 37) // THD, user +/* + Check how many bytes are available on buffer. + + @param buf_start Pointer to buffer start. + @param buf_current Pointer to the current position on buffer. + @param buf_len Buffer length. + + @return Number of bytes available on event buffer. +*/ +template <class T> T available_buffer(const char* buf_start, + const char* buf_current, + T buf_len) +{ + return buf_len - (buf_current - buf_start); +} + +/* + Check if jump value is within buffer limits. + + @param jump Number of positions we want to advance. + @param buf_start Pointer to buffer start + @param buf_current Pointer to the current position on buffer. + @param buf_len Buffer length. + + @return True If jump value is within buffer limits. + False Otherwise. +*/ +template <class T> bool valid_buffer_range(T jump, + const char* buf_start, + const char* buf_current, + T buf_len) +{ + return (jump <= available_buffer(buf_start, buf_current, buf_len)); +} + /* The rest of the file is included in the server only */ #ifndef MYSQL_CLIENT diff --git a/sql/sql_profile.cc b/sql/sql_profile.cc index 2d00a19870b..feb7810fa28 100644 --- a/sql/sql_profile.cc +++ b/sql/sql_profile.cc @@ -46,6 +46,7 @@ #define TIME_I_S_DECIMAL_SIZE (TIME_FLOAT_DIGITS*100)+(TIME_FLOAT_DIGITS-3) #define MAX_QUERY_LENGTH 300 +#define MAX_QUERY_HISTORY 101 /** Connects Information_Schema and Profiling. @@ -264,9 +265,12 @@ void PROF_MEASUREMENT::collect() QUERY_PROFILE::QUERY_PROFILE(PROFILING *profiling_arg, const char *status_arg) :profiling(profiling_arg), profiling_query_id(0), query_source(NULL) { - profile_start= new PROF_MEASUREMENT(this, status_arg); - entries.push_back(profile_start); - profile_end= profile_start; + m_seq_counter= 1; + PROF_MEASUREMENT *prof= new PROF_MEASUREMENT(this, status_arg); + prof->m_seq= m_seq_counter++; + m_start_time_usecs= prof->time_usecs; + m_end_time_usecs= m_start_time_usecs; + entries.push_back(prof); } QUERY_PROFILE::~QUERY_PROFILE() @@ -305,9 +309,14 @@ void QUERY_PROFILE::new_status(const char *status_arg, else prof= new PROF_MEASUREMENT(this, status_arg); - profile_end= prof; + prof->m_seq= m_seq_counter++; + m_end_time_usecs= prof->time_usecs; entries.push_back(prof); + /* Maintain the query history size. */ + while (entries.elements > MAX_QUERY_HISTORY) + delete entries.pop(); + DBUG_VOID_RETURN; } @@ -467,8 +476,7 @@ bool PROFILING::show_profiles() String elapsed; - PROF_MEASUREMENT *ps= prof->profile_start; - PROF_MEASUREMENT *pe= prof->profile_end; + double query_time_usecs= prof->m_end_time_usecs - prof->m_start_time_usecs; if (++idx <= unit->offset_limit_cnt) continue; @@ -477,7 +485,7 @@ bool PROFILING::show_profiles() protocol->prepare_for_resend(); protocol->store((uint32)(prof->profiling_query_id)); - protocol->store((double)(pe->time_usecs - ps->time_usecs)/(1000.0*1000), + protocol->store((double)(query_time_usecs/(1000.0*1000)), (uint32) TIME_FLOAT_DIGITS-1, &elapsed); if (prof->query_source != NULL) protocol->store(prof->query_source, strlen(prof->query_source), @@ -537,17 +545,18 @@ int PROFILING::fill_statistics_info(THD *thd_arg, TABLE_LIST *tables, Item *cond us also include a numbering of each state per query. The query_id and the "seq" together are unique. */ - ulonglong seq; + ulong seq; void *entry_iterator; PROF_MEASUREMENT *entry, *previous= NULL; /* ...and for each query, go through all its state-change steps. */ - for (seq= 0, entry_iterator= query->entries.new_iterator(); + for (entry_iterator= query->entries.new_iterator(); entry_iterator != NULL; entry_iterator= query->entries.iterator_next(entry_iterator), - seq++, previous=entry, row_number++) + previous=entry, row_number++) { entry= query->entries.iterator_value(entry_iterator); + seq= entry->m_seq; /* Skip the first. We count spans of fence, not fence-posts. */ if (previous == NULL) continue; diff --git a/sql/sql_profile.h b/sql/sql_profile.h index 7705f6ca476..f8970bb162a 100644 --- a/sql/sql_profile.h +++ b/sql/sql_profile.h @@ -186,6 +186,7 @@ private: char *file; unsigned int line; + ulong m_seq; double time_usecs; char *allocated_status_memory; @@ -217,8 +218,9 @@ private: query_id_t profiling_query_id; /* Session-specific id. */ char *query_source; - PROF_MEASUREMENT *profile_start; - PROF_MEASUREMENT *profile_end; + double m_start_time_usecs; + double m_end_time_usecs; + ulong m_seq_counter; Queue<PROF_MEASUREMENT> entries; diff --git a/sql/sql_reload.cc b/sql/sql_reload.cc index 99fe9267589..2720dc7cd74 100644 --- a/sql/sql_reload.cc +++ b/sql/sql_reload.cc @@ -97,7 +97,7 @@ bool reload_acl_and_cache(THD *thd, unsigned long options, { delete tmp_thd; /* Remember that we don't have a THD */ - my_pthread_setspecific_ptr(THR_THD, 0); + set_current_thd(0); thd= 0; } reset_mqh((LEX_USER *)NULL, TRUE); diff --git a/sql/sql_repl.cc b/sql/sql_repl.cc index db8dc694502..917f4ea1a80 100644 --- a/sql/sql_repl.cc +++ b/sql/sql_repl.cc @@ -1641,6 +1641,31 @@ void kill_zombie_dump_threads(uint32 slave_server_id) } } +/** + Get value for a string parameter with error checking + + Note that in case of error the original string should not be updated! + + @ret 0 ok + @ret 1 error +*/ + +static bool get_string_parameter(char *to, const char *from, size_t length, + const char *name) +{ + if (from) // Empty paramaters allowed + { + size_t from_length; + if ((from_length= strlen(from)) > length) + { + my_error(ER_WRONG_STRING_LENGTH, MYF(0), from, name, (int) length); + return 1; + } + memcpy(to, from, from_length+1); + } + return 0; +} + /** Execute a CHANGE MASTER statement. @@ -1651,10 +1676,14 @@ void kill_zombie_dump_threads(uint32 slave_server_id) @param mi Pointer to Master_info object belonging to the slave's IO thread. + @param master_info_added Out parameter saying if the Master_info *mi was + added to the global list of masters. This is useful in error conditions + to know if caller should free Master_info *mi. + @retval FALSE success @retval TRUE error */ -bool change_master(THD* thd, Master_info* mi) +bool change_master(THD* thd, Master_info* mi, bool *master_info_added) { int thread_mask; const char* errmsg= 0; @@ -1669,6 +1698,7 @@ bool change_master(THD* thd, Master_info* mi) LEX_MASTER_INFO* lex_mi= &thd->lex->mi; DBUG_ENTER("change_master"); + *master_info_added= false; /* We need to check if there is an empty master_host. Otherwise change master succeeds, a master.info file is created containing @@ -1717,6 +1747,7 @@ bool change_master(THD* thd, Master_info* mi) ret= TRUE; goto err; } + *master_info_added= true; } if (global_system_variables.log_warnings > 1) sql_print_information("Master: '%.*s' Master_info_file: '%s' " @@ -1769,12 +1800,17 @@ bool change_master(THD* thd, Master_info* mi) } DBUG_PRINT("info", ("master_log_pos: %lu", (ulong) mi->master_log_pos)); - if (lex_mi->host) - strmake(mi->host, lex_mi->host, sizeof(mi->host)-1); - if (lex_mi->user) - strmake(mi->user, lex_mi->user, sizeof(mi->user)-1); - if (lex_mi->password) - strmake(mi->password, lex_mi->password, sizeof(mi->password)-1); + if (get_string_parameter(mi->host, lex_mi->host, sizeof(mi->host)-1, + "MASTER_HOST") || + get_string_parameter(mi->user, lex_mi->user, sizeof(mi->user)-1, + "MASTER_USER") || + get_string_parameter(mi->password, lex_mi->password, + sizeof(mi->password)-1, "MASTER_PASSWORD")) + { + ret= TRUE; + goto err; + } + if (lex_mi->port) mi->port = lex_mi->port; if (lex_mi->connect_retry) diff --git a/sql/sql_repl.h b/sql/sql_repl.h index 7dc58c47d52..9ca7e6b00b1 100644 --- a/sql/sql_repl.h +++ b/sql/sql_repl.h @@ -25,9 +25,9 @@ typedef struct st_slave_info { uint32 server_id; uint32 rpl_recovery_rank, master_id; - char host[HOSTNAME_LENGTH+1]; + char host[HOSTNAME_LENGTH*SYSTEM_CHARSET_MBMAXLEN+1]; char user[USERNAME_LENGTH+1]; - char password[MAX_PASSWORD_LENGTH+1]; + char password[MAX_PASSWORD_LENGTH*SYSTEM_CHARSET_MBMAXLEN+1]; uint16 port; THD* thd; } SLAVE_INFO; @@ -41,7 +41,7 @@ extern my_bool opt_sporadic_binlog_dump_fail; int start_slave(THD* thd, Master_info* mi, bool net_report); int stop_slave(THD* thd, Master_info* mi, bool net_report); -bool change_master(THD* thd, Master_info* mi); +bool change_master(THD* thd, Master_info* mi, bool *master_info_added); bool mysql_show_binlog_events(THD* thd); int reset_slave(THD *thd, Master_info* mi); int reset_master(THD* thd); diff --git a/sql/sql_select.cc b/sql/sql_select.cc index 86a4168867b..27b93cff189 100644 --- a/sql/sql_select.cc +++ b/sql/sql_select.cc @@ -1,5 +1,5 @@ /* Copyright (c) 2000, 2012 Oracle and/or its affiliates. - Copyright (c) 2009, 2012, Monty Program Ab + Copyright (c) 2009, 2013 Monty Program Ab. 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 @@ -223,7 +223,7 @@ static int create_sort_index(THD *thd, JOIN *join, ORDER *order, static int remove_duplicates(JOIN *join,TABLE *entry,List<Item> &fields, Item *having); static int remove_dup_with_compare(THD *thd, TABLE *entry, Field **field, - ulong offset,Item *having); + Item *having); static int remove_dup_with_hash_index(THD *thd,TABLE *table, uint field_count, Field **first_field, ulong key_length,Item *having); @@ -378,7 +378,7 @@ bool handle_select(THD *thd, LEX *lex, select_result *result, ER(ER_QUERY_EXCEEDED_ROWS_EXAMINED_LIMIT), thd->accessed_rows_and_keys, thd->lex->limit_rows_examined->val_uint()); - thd->killed= NOT_KILLED; + thd->reset_killed(); } /* Disable LIMIT ROWS EXAMINED after query execution. */ thd->lex->limit_rows_examined_cnt= ULONGLONG_MAX; @@ -2072,6 +2072,8 @@ bool JOIN::setup_subquery_caches() */ void JOIN::restore_tmp() { + DBUG_PRINT("info", ("restore_tmp this %p tmp_join %p", this, tmp_join)); + DBUG_ASSERT(tmp_join != this); memcpy(tmp_join, this, (size_t) sizeof(JOIN)); } @@ -4943,7 +4945,8 @@ update_ref_and_keys(THD *thd, DYNAMIC_ARRAY *keyuse,JOIN_TAB *join_tab, /* set a barrier for the array of SARGABLE_PARAM */ (*sargables)[0].field= 0; - if (my_init_dynamic_array(keyuse,sizeof(KEYUSE),20,64)) + if (my_init_dynamic_array(keyuse,sizeof(KEYUSE),20,64, + MYF(MY_THREAD_SPECIFIC))) return TRUE; if (cond) @@ -7746,8 +7749,9 @@ get_best_combination(JOIN *join) if ( !(keyuse= join->best_positions[tablenr].key)) { j->type=JT_ALL; - if (tablenr != join->const_tables) - join->full_join=1; + if (join->best_positions[tablenr].use_join_buffer && + tablenr != join->const_tables) + join->full_join= 1; } /*if (join->best_positions[tablenr].sj_strategy == SJ_OPT_LOOSE_SCAN) @@ -8740,7 +8744,9 @@ make_join_select(JOIN *join,SQL_SELECT *select,COND *cond) We will use join cache here : prevent sorting of the first table only and sort at the end. */ - if (i != join->const_tables && join->table_count > join->const_tables + 1) + if (i != join->const_tables && + join->table_count > join->const_tables + 1 && + join->best_positions[i].use_join_buffer) join->full_join= 1; } @@ -10763,7 +10769,6 @@ void JOIN::cleanup(bool full) filesort_free_buffers(first_tab->table, full); } } - if (full) { JOIN_TAB *sort_tab= first_linear_tab(this, WITHOUT_CONST_TABLES); @@ -10798,21 +10803,19 @@ void JOIN::cleanup(bool full) } } } - /* - We are not using tables anymore - Unlock all tables. We may be in an INSERT .... SELECT statement. - */ if (full) { - if (tmp_join) - tmp_table_param.copy_field= 0; - group_fields.delete_elements(); /* - Ensure that the above delete_elements() would not be called + Ensure that the following delete_elements() would not be called twice for the same list. */ - if (tmp_join && tmp_join != this) - tmp_join->group_fields= group_fields; + if (tmp_join && tmp_join != this && + tmp_join->group_fields == this->group_fields) + tmp_join->group_fields.empty(); + + // Run Cached_item DTORs! + group_fields.delete_elements(); + /* We can't call delete_elements() on copy_funcs as this will cause problems in free_elements() as some of the elements are then deleted. @@ -14325,10 +14328,20 @@ create_tmp_table(THD *thd, TMP_TABLE_PARAM *param, List<Item> &fields, if (group) { + ORDER **prev= &group; if (!param->quick_group) group=0; // Can't use group key else for (ORDER *tmp=group ; tmp ; tmp=tmp->next) { + /* Exclude found constant from the list */ + if ((*tmp->item)->const_item()) + { + *prev= tmp->next; + param->group_parts--; + continue; + } + else + prev= &(tmp->next); /* marker == 4 means two things: - store NULLs in the key, and @@ -14336,7 +14349,7 @@ create_tmp_table(THD *thd, TMP_TABLE_PARAM *param, List<Item> &fields, can't index BIT fields. */ (*tmp->item)->marker=4; // Store null in key - if ((*tmp->item)->max_length >= CONVERT_IF_BIGGER_TO_BLOB) + if ((*tmp->item)->too_big_for_varchar()) using_unique_constraint=1; } if (param->group_length >= MAX_BLOB_WIDTH) @@ -14358,7 +14371,7 @@ create_tmp_table(THD *thd, TMP_TABLE_PARAM *param, List<Item> &fields, if (param->precomputed_group_by) copy_func_count+= param->sum_func_count; - init_sql_alloc(&own_root, TABLE_ALLOC_BLOCK_SIZE, 0); + init_sql_alloc(&own_root, TABLE_ALLOC_BLOCK_SIZE, 0, MYF(MY_THREAD_SPECIFIC)); if (!multi_alloc_root(&own_root, &table, sizeof(*table), @@ -15841,11 +15854,11 @@ do_select(JOIN *join,List<Item> *fields,TABLE *table,Procedure *procedure) if (table->group && join->tmp_table_param.sum_func_count && table->s->keys && !table->file->inited) { - int tmp_error; - if ((tmp_error= table->file->ha_index_init(0, 0))) + rc= table->file->ha_index_init(0, 0); + if (rc) { - table->file->print_error(tmp_error, MYF(0)); /* purecov: inspected */ - DBUG_RETURN(-1); /* purecov: inspected */ + table->file->print_error(rc, MYF(0)); + DBUG_RETURN(-1); } } } @@ -16879,7 +16892,12 @@ int join_read_key2(THD *thd, JOIN_TAB *tab, TABLE *table, TABLE_REF *table_ref) int error; if (!table->file->inited) { - table->file->ha_index_init(table_ref->key, (tab ? tab->sorted : TRUE)); + error= table->file->ha_index_init(table_ref->key, tab ? tab->sorted : TRUE); + if (error) + { + (void) report_error(table, error); + return 1; + } } /* TODO: Why don't we do "Late NULLs Filtering" here? */ @@ -16970,8 +16988,8 @@ join_read_always_key(JOIN_TAB *tab) { if ((error= table->file->ha_index_init(tab->ref.key, tab->sorted))) { - table->file->print_error(error, MYF(0));/* purecov: inspected */ - return(1); /* purecov: inspected */ + (void) report_error(table, error); + return 1; } } @@ -17001,14 +17019,13 @@ join_read_last_key(JOIN_TAB *tab) int error; TABLE *table= tab->table; - if (!table->file->inited) + if (!table->file->inited && + (error= table->file->ha_index_init(tab->ref.key, tab->sorted))) { - if ((error= table->file->ha_index_init(tab->ref.key, tab->sorted))) - { - table->file->print_error(error, MYF(0));/* purecov: inspected */ - return(1); /* purecov: inspected */ - } + (void) report_error(table, error); + return 1; } + if (cp_buffer_from_ref(tab->join->thd, table, &tab->ref)) return -1; if ((error= table->file->ha_index_read_map(table->record[0], @@ -17227,9 +17244,10 @@ join_ft_read_first(JOIN_TAB *tab) if (!table->file->inited && (error= table->file->ha_index_init(tab->ref.key, 1))) { - table->file->print_error(error, MYF(0)); /* purecov: inspected */ - return(1); /* purecov: inspected */ + (void) report_error(table, error); + return 1; } + table->file->ft_init(); if ((error= table->file->ha_ft_read(table->record[0]))) @@ -17652,9 +17670,10 @@ end_update(JOIN *join, JOIN_TAB *join_tab __attribute__((unused)), /* Change method to update rows */ if ((error= table->file->ha_index_init(0, 0))) { - table->file->print_error(error, MYF(0));/* purecov: inspected */ - DBUG_RETURN(NESTED_LOOP_ERROR); /* purecov: inspected */ + table->file->print_error(error, MYF(0)); + DBUG_RETURN(NESTED_LOOP_ERROR); } + join->join_tab[join->top_join_tab_count-1].next_select=end_unique_update; } join->send_records++; @@ -18296,18 +18315,36 @@ static int test_if_order_by_key(ORDER *order, TABLE *table, uint idx, table->s->primary_key != MAX_KEY && table->s->primary_key != idx) { + KEY_PART_INFO *start,*end; + uint pk_part_idx= 0; on_pk_suffix= TRUE; - key_part= table->key_info[table->s->primary_key].key_part; - key_part_end=key_part+table->key_info[table->s->primary_key].key_parts; + start= key_part= table->key_info[table->s->primary_key].key_part; const_key_parts=table->const_key_parts[table->s->primary_key]; - for (; const_key_parts & 1 ; const_key_parts>>= 1) - key_part++; /* - The primary and secondary key parts were all const (i.e. there's - one row). The sorting doesn't matter. + Calculate true key_part_end and const_key_parts + (we have to stop as first not continous primary key part) */ - if (key_part == key_part_end && reverse == 0) + for (key_part_end= key_part, + end= key_part+table->key_info[table->s->primary_key].key_parts; + key_part_end < end; key_part_end++, pk_part_idx++) + { + /* Found hole in the pk_parts; Abort */ + if (!(table->key_info[idx].ext_key_part_map & + (((key_part_map) 1) << pk_part_idx))) + break; + } + /* Adjust const_key_parts */ + const_key_parts&= (((key_part_map) 1) << pk_part_idx) -1; + + for (; const_key_parts & 1 ; const_key_parts>>= 1) + key_part++; + /* + Test if the primary key parts were all const (i.e. there's one row). + The sorting doesn't matter. + */ + if (key_part == start+table->key_info[table->s->primary_key].key_parts && + reverse == 0) { key_parts= 0; reverse= 1; @@ -18327,7 +18364,8 @@ static int test_if_order_by_key(ORDER *order, TABLE *table, uint idx, if (reverse && flag != reverse) DBUG_RETURN(0); reverse=flag; // Remember if reverse - key_part++; + if (key_part < key_part_end) + key_part++; } if (on_pk_suffix) { @@ -19133,7 +19171,8 @@ create_sort_index(THD *thd, JOIN *join, ORDER *order, goto err; /* purecov: inspected */ table->sort.io_cache=(IO_CACHE*) my_malloc(sizeof(IO_CACHE), - MYF(MY_WME | MY_ZEROFILL)); + MYF(MY_WME | MY_ZEROFILL| + MY_THREAD_SPECIFIC)); table->status=0; // May be wrong if quick_select // If table has a range, move it to select @@ -19229,19 +19268,24 @@ void JOIN::clean_pre_sort_join_tab() } -/***************************************************************************** - Remove duplicates from tmp table - This should be recoded to add a unique index to the table and remove - duplicates - Table is a locked single thread table - fields is the number of fields to check (from the end) -*****************************************************************************/ +/** + Compare fields from table->record[0] and table->record[1], + possibly skipping few first fields. + + @param table + @param ptr field to start the comparison from, + somewhere in the table->field[] array + @retval 1 different + @retval 0 identical +*/ static bool compare_record(TABLE *table, Field **ptr) { for (; *ptr ; ptr++) { - if ((*ptr)->cmp_offset(table->s->rec_buff_length)) + Field *f= *ptr; + if (f->is_null() != f->is_null(table->s->rec_buff_length) || + (!f->is_null() && f->cmp_offset(table->s->rec_buff_length))) return 1; } return 0; @@ -19269,16 +19313,16 @@ static void free_blobs(Field **ptr) static int -remove_duplicates(JOIN *join, TABLE *entry,List<Item> &fields, Item *having) +remove_duplicates(JOIN *join, TABLE *table, List<Item> &fields, Item *having) { int error; - ulong reclength,offset; + ulong keylength= 0; uint field_count; THD *thd= join->thd; DBUG_ENTER("remove_duplicates"); - entry->reginfo.lock_type=TL_WRITE; + table->reginfo.lock_type=TL_WRITE; /* Calculate how many saved fields there is in list */ field_count=0; @@ -19295,11 +19339,10 @@ remove_duplicates(JOIN *join, TABLE *entry,List<Item> &fields, Item *having) join->unit->select_limit_cnt= 1; // Only send first row DBUG_RETURN(0); } - Field **first_field=entry->field+entry->s->fields - field_count; - offset= (field_count ? - entry->field[entry->s->fields - field_count]-> - offset(entry->record[0]) : 0); - reclength=entry->s->reclength-offset; + + Field **first_field=table->field+table->s->fields - field_count; + for (Field **ptr=first_field; *ptr; ptr++) + keylength+= (*ptr)->sort_length() + (*ptr)->maybe_null(); /* Disable LIMIT ROWS EXAMINED in order to avoid interrupting prematurely @@ -19307,19 +19350,18 @@ remove_duplicates(JOIN *join, TABLE *entry,List<Item> &fields, Item *having) */ thd->lex->limit_rows_examined_cnt= ULONGLONG_MAX; if (thd->killed == ABORT_QUERY) - thd->killed= NOT_KILLED; - free_io_cache(entry); // Safety - entry->file->info(HA_STATUS_VARIABLE); - if (entry->s->db_type() == heap_hton || - (!entry->s->blob_fields && - ((ALIGN_SIZE(reclength) + HASH_OVERHEAD) * entry->file->stats.records < + thd->reset_killed(); + + free_io_cache(table); // Safety + table->file->info(HA_STATUS_VARIABLE); + if (table->s->db_type() == heap_hton || + (!table->s->blob_fields && + ((ALIGN_SIZE(keylength) + HASH_OVERHEAD) * table->file->stats.records < thd->variables.sortbuff_size))) - error=remove_dup_with_hash_index(join->thd, entry, - field_count, first_field, - reclength, having); + error=remove_dup_with_hash_index(join->thd, table, field_count, first_field, + keylength, having); else - error=remove_dup_with_compare(join->thd, entry, first_field, offset, - having); + error=remove_dup_with_compare(join->thd, table, first_field, having); if (join->select_lex != join->select_lex->master_unit()->fake_select_lex) thd->lex->set_limit_rows_examined(); @@ -19329,18 +19371,13 @@ remove_duplicates(JOIN *join, TABLE *entry,List<Item> &fields, Item *having) static int remove_dup_with_compare(THD *thd, TABLE *table, Field **first_field, - ulong offset, Item *having) + Item *having) { handler *file=table->file; - char *org_record,*new_record; - uchar *record; + uchar *record=table->record[0]; int error; - ulong reclength= table->s->reclength-offset; DBUG_ENTER("remove_dup_with_compare"); - org_record=(char*) (record=table->record[0])+offset; - new_record=(char*) table->record[1]+offset; - if (file->ha_rnd_init_with_error(1)) DBUG_RETURN(1); @@ -19377,7 +19414,7 @@ static int remove_dup_with_compare(THD *thd, TABLE *table, Field **first_field, error=0; goto err; } - memcpy(new_record,org_record,reclength); + store_record(table,record[1]); /* Read through rest of file and mark duplicated rows deleted */ bool found=0; @@ -19436,8 +19473,9 @@ static int remove_dup_with_hash_index(THD *thd, TABLE *table, int error; handler *file= table->file; ulong extra_length= ALIGN_SIZE(key_length)-key_length; - uint *field_lengths,*field_length; + uint *field_lengths, *field_length; HASH hash; + Field **ptr; DBUG_ENTER("remove_dup_with_hash_index"); if (!my_multi_malloc(MYF(MY_WME), @@ -19449,21 +19487,8 @@ static int remove_dup_with_hash_index(THD *thd, TABLE *table, NullS)) DBUG_RETURN(1); - { - Field **ptr; - ulong total_length= 0; - for (ptr= first_field, field_length=field_lengths ; *ptr ; ptr++) - { - uint length= (*ptr)->sort_length(); - (*field_length++)= length; - total_length+= length; - } - DBUG_PRINT("info",("field_count: %u key_length: %lu total_length: %lu", - field_count, key_length, total_length)); - DBUG_ASSERT(total_length <= key_length); - key_length= total_length; - extra_length= ALIGN_SIZE(key_length)-key_length; - } + for (ptr= first_field, field_length=field_lengths ; *ptr ; ptr++) + (*field_length++)= (*ptr)->sort_length(); if (my_hash_init(&hash, &my_charset_bin, (uint) file->stats.records, 0, key_length, (my_hash_get_key) 0, 0, 0)) @@ -19503,10 +19528,10 @@ static int remove_dup_with_hash_index(THD *thd, TABLE *table, /* copy fields to key buffer */ org_key_pos= key_pos; field_length=field_lengths; - for (Field **ptr= first_field ; *ptr ; ptr++) + for (ptr= first_field ; *ptr ; ptr++) { - (*ptr)->sort_string(key_pos,*field_length); - key_pos+= *field_length++; + (*ptr)->make_sort_key(key_pos, *field_length); + key_pos+= (*ptr)->maybe_null() + *field_length++; } /* Check if it exists before */ if (my_hash_search(&hash, org_key_pos, key_length)) @@ -22380,6 +22405,7 @@ static void print_join(THD *thd, List_iterator_fast<TABLE_LIST> ti(*tables); TABLE_LIST **table; uint non_const_tables= 0; + DBUG_ENTER("print_join"); for (TABLE_LIST *t= ti++; t ; t= ti++) { @@ -22393,13 +22419,13 @@ static void print_join(THD *thd, if (!non_const_tables) { str->append(STRING_WITH_LEN("dual")); - return; // all tables were optimized away + DBUG_VOID_RETURN; // all tables were optimized away } ti.rewind(); if (!(table= (TABLE_LIST **)thd->alloc(sizeof(TABLE_LIST*) * non_const_tables))) - return; // out of memory + DBUG_VOID_RETURN; // out of memory TABLE_LIST *tmp, **t= table + (non_const_tables - 1); while ((tmp= ti++)) @@ -22438,6 +22464,7 @@ static void print_join(THD *thd, } print_table_array(thd, eliminated_tables, str, table, table + non_const_tables, query_type); + DBUG_VOID_RETURN; } /** @@ -22946,7 +22973,8 @@ JOIN::reoptimize(Item *added_where, table_map join_tables, reset_query_plan(); if (!keyuse.buffer && - my_init_dynamic_array(&keyuse, sizeof(KEYUSE), 20, 64)) + my_init_dynamic_array(&keyuse, sizeof(KEYUSE), 20, 64, + MYF(MY_THREAD_SPECIFIC))) { delete_dynamic(&added_keyuse); return REOPT_ERROR; @@ -23125,7 +23153,7 @@ test_if_cheaper_ordering(const JOIN_TAB *tab, ORDER *order, TABLE *table, { int direction; ha_rows select_limit= select_limit_arg; - uint used_key_parts; + uint used_key_parts= 0; if (keys.is_set(nr) && (direction= test_if_order_by_key(order, table, nr, &used_key_parts))) @@ -23193,7 +23221,7 @@ test_if_cheaper_ordering(const JOIN_TAB *tab, ORDER *order, TABLE *table, to be adjusted accordingly if some components of the secondary key are included in the primary key. */ - for(uint i= 0; i < used_pk_parts; i++) + for(uint i= 1; i < used_pk_parts; i++) { if (pkinfo->key_part[i].field->key_start.is_set(nr)) { diff --git a/sql/sql_servers.cc b/sql/sql_servers.cc index dce679a883f..b5b7f9866c5 100644 --- a/sql/sql_servers.cc +++ b/sql/sql_servers.cc @@ -158,7 +158,7 @@ bool servers_init(bool dont_read_servers_table) } /* Initialize the mem root for data */ - init_sql_alloc(&mem, ACL_ALLOC_BLOCK_SIZE, 0); + init_sql_alloc(&mem, ACL_ALLOC_BLOCK_SIZE, 0, MYF(MY_THREAD_SPECIFIC)); if (dont_read_servers_table) goto end; @@ -178,7 +178,7 @@ bool servers_init(bool dont_read_servers_table) return_val= servers_reload(thd); delete thd; /* Remember that we don't have a THD */ - my_pthread_setspecific_ptr(THR_THD, 0); + set_current_thd(0); end: DBUG_RETURN(return_val); @@ -209,7 +209,7 @@ static bool servers_load(THD *thd, TABLE_LIST *tables) my_hash_reset(&servers_cache); free_root(&mem, MYF(0)); - init_sql_alloc(&mem, ACL_ALLOC_BLOCK_SIZE, 0); + init_sql_alloc(&mem, ACL_ALLOC_BLOCK_SIZE, 0, MYF(0)); if (init_read_record(&read_record_info,thd,table=tables[0].table,NULL,1,0, FALSE)) diff --git a/sql/sql_show.cc b/sql/sql_show.cc index 1ef3ce2e40a..1f860fe23db 100644 --- a/sql/sql_show.cc +++ b/sql/sql_show.cc @@ -458,7 +458,7 @@ bool ignore_db_dirs_init() { return my_init_dynamic_array(&ignore_db_dirs_array, sizeof(LEX_STRING *), - 0, 0); + 0, 0, MYF(0)); } @@ -737,7 +737,8 @@ find_files(THD *thd, List<LEX_STRING> *files, const char *db, bzero((char*) &table_list,sizeof(table_list)); - if (!(dirp = my_dir(path,MYF(dir ? MY_WANT_STAT : 0)))) + if (!(dirp = my_dir(path,MYF((dir ? MY_WANT_STAT : 0) | + MY_THREAD_SPECIFIC)))) { if (my_errno == ENOENT) my_error(ER_BAD_DB_ERROR, MYF(ME_BELL+ME_WAITTANG), db); @@ -2334,11 +2335,14 @@ void Show_explain_request::call_in_target_thread() target_thd->query_length(), target_thd->query_charset()); + DBUG_ASSERT(current_thd == target_thd); + set_current_thd(request_thd); if (target_thd->lex->unit.print_explain(explain_buf, 0 /* explain flags*/, &printed_anything)) { failed_to_produce= TRUE; } + set_current_thd(target_thd); if (!printed_anything) failed_to_produce= TRUE; @@ -2349,10 +2353,20 @@ void Show_explain_request::call_in_target_thread() int select_result_explain_buffer::send_data(List<Item> &items) { + int res; + THD *cur_thd= current_thd; + DBUG_ENTER("select_result_explain_buffer::send_data"); + + /* + Switch to the recieveing thread, so that we correctly count memory used + by it. This is needed as it's the receiving thread that will free the + memory. + */ + set_current_thd(thd); fill_record(thd, dst_table, dst_table->field, items, TRUE, FALSE); - if ((dst_table->file->ha_write_tmp_row(dst_table->record[0]))) - return 1; - return 0; + res= dst_table->file->ha_write_tmp_row(dst_table->record[0]); + set_current_thd(cur_thd); + DBUG_RETURN(test(res)); } @@ -2578,6 +2592,18 @@ int fill_schema_processlist(THD* thd, TABLE_LIST* tables, COND* cond) } mysql_mutex_unlock(&tmp->LOCK_thd_data); + /* + This may become negative if we free a memory allocated by another + thread in this thread. However it's better that we notice it eventually + than hide it. + */ + table->field[12]->store((longlong) (tmp->status_var.memory_used + + sizeof(THD)), + FALSE); + table->field[12]->set_notnull(); + table->field[13]->store((longlong) tmp->get_examined_row_count(), TRUE); + table->field[13]->set_notnull(); + if (schema_table_store_record(thd, table)) { mysql_mutex_unlock(&LOCK_thread_count); @@ -2650,7 +2676,7 @@ int add_status_vars(SHOW_VAR *list) if (status_vars_inited) mysql_mutex_lock(&LOCK_status); if (!all_status_vars.buffer && // array is not allocated yet - do it now - my_init_dynamic_array(&all_status_vars, sizeof(SHOW_VAR), 200, 20)) + my_init_dynamic_array(&all_status_vars, sizeof(SHOW_VAR), 200, 20, MYF(0))) { res= 1; goto err; @@ -2784,7 +2810,6 @@ static bool show_status_array(THD *thd, const char *wild, int len; LEX_STRING null_lex_str; SHOW_VAR tmp, *var; - COND *partial_cond= 0; enum_check_fields save_count_cuted_fields= thd->count_cuted_fields; bool res= FALSE; CHARSET_INFO *charset= system_charset_info; @@ -2798,7 +2823,6 @@ static bool show_status_array(THD *thd, const char *wild, if (*prefix) *prefix_end++= '_'; len=name_buffer + sizeof(name_buffer) - prefix_end; - partial_cond= make_cond_for_info_schema(cond, table->pos_in_table_list); for (; variables->name; variables++) { @@ -2836,14 +2860,14 @@ static bool show_status_array(THD *thd, const char *wild, if (show_type == SHOW_ARRAY) { show_status_array(thd, wild, (SHOW_VAR *) var->value, value_type, - status_var, name_buffer, table, ucase_names, partial_cond); + status_var, name_buffer, table, ucase_names, cond); } else { if ((wild_checked || (wild && wild[0] && wild_case_compare(system_charset_info, name_buffer, wild))) && - (!partial_cond || partial_cond->val_int())) + (!cond || cond->val_int())) { char *value=var->value; const char *pos, *end; // We assign a lot of const's @@ -2999,7 +3023,8 @@ static int aggregate_user_stats(HASH *all_user_stats, HASH *agg_user_stats) { // First entry for this role. if (!(agg_user= (USER_STATS*) my_malloc(sizeof(USER_STATS), - MYF(MY_WME | MY_ZEROFILL)))) + MYF(MY_WME | MY_ZEROFILL| + MY_THREAD_SPECIFIC)))) { sql_print_error("Malloc in aggregate_user_stats failed"); DBUG_RETURN(1); @@ -4372,7 +4397,7 @@ static int fill_schema_table_from_frm(THD *thd, TABLE_LIST *tables, if (schema_table->i_s_requested_object & OPEN_TRIGGER_ONLY) { - init_sql_alloc(&tbl.mem_root, TABLE_ALLOC_BLOCK_SIZE, 0); + init_sql_alloc(&tbl.mem_root, TABLE_ALLOC_BLOCK_SIZE, 0, MYF(0)); if (!Table_triggers_list::check_n_load(thd, db_name->str, table_name->str, &tbl, 1)) { @@ -5870,7 +5895,13 @@ int fill_schema_proc(THD *thd, TABLE_LIST *tables, COND *cond) { DBUG_RETURN(1); } - proc_table->file->ha_index_init(0, 1); + + if (proc_table->file->ha_index_init(0, 1)) + { + res= 1; + goto err; + } + if ((res= proc_table->file->ha_index_first(proc_table->record[0]))) { res= (res == HA_ERR_END_OF_FILE) ? 0 : 1; @@ -5896,7 +5927,9 @@ int fill_schema_proc(THD *thd, TABLE_LIST *tables, COND *cond) } err: - proc_table->file->ha_index_end(); + if (proc_table->file->inited) + (void) proc_table->file->ha_index_end(); + close_system_tables(thd, &open_tables_state_backup); DBUG_RETURN(res); } @@ -7095,9 +7128,12 @@ int fill_variables(THD *thd, TABLE_LIST *tables, COND *cond) schema_table_idx == SCH_GLOBAL_VARIABLES) option_type= OPT_GLOBAL; + COND *partial_cond= make_cond_for_info_schema(cond, tables); + mysql_rwlock_rdlock(&LOCK_system_variables_hash); res= show_status_array(thd, wild, enumerate_sys_vars(thd, sorted_vars, option_type), - option_type, NULL, "", tables->table, upper_case_names, cond); + option_type, NULL, "", tables->table, + upper_case_names, partial_cond); mysql_rwlock_unlock(&LOCK_system_variables_hash); DBUG_RETURN(res); } @@ -7134,13 +7170,18 @@ int fill_status(THD *thd, TABLE_LIST *tables, COND *cond) tmp1= &thd->status_var; } + COND *partial_cond= make_cond_for_info_schema(cond, tables); + // Evaluate and cache const subqueries now, before the mutex. + if (partial_cond) + partial_cond->val_int(); + mysql_mutex_lock(&LOCK_status); if (option_type == OPT_GLOBAL) calc_sum_of_all_status(&tmp); res= show_status_array(thd, wild, (SHOW_VAR *)all_status_vars.buffer, option_type, tmp1, "", tables->table, - upper_case_names, cond); + upper_case_names, partial_cond); mysql_mutex_unlock(&LOCK_status); DBUG_RETURN(res); } @@ -8612,6 +8653,8 @@ ST_FIELD_INFO processlist_fields_info[]= {"MAX_STAGE", 2, MYSQL_TYPE_TINY, 0, 0, "Max_stage", SKIP_OPEN_TABLE}, {"PROGRESS", 703, MYSQL_TYPE_DECIMAL, 0, 0, "Progress", SKIP_OPEN_TABLE}, + {"MEMORY_USED", 7, MYSQL_TYPE_LONG, 0, 0, "Memory_used", SKIP_OPEN_TABLE}, + {"EXAMINED_ROWS", 7, MYSQL_TYPE_LONG, 0, 0, "Examined_rows", SKIP_OPEN_TABLE}, {0, 0, MYSQL_TYPE_STRING, 0, 0, 0, SKIP_OPEN_TABLE} }; @@ -8941,7 +8984,7 @@ int initialize_schema_table(st_plugin_int *plugin) DBUG_ENTER("initialize_schema_table"); if (!(schema_table= (ST_SCHEMA_TABLE *)my_malloc(sizeof(ST_SCHEMA_TABLE), - MYF(MY_WME | MY_ZEROFILL)))) + MYF(MY_WME | MY_ZEROFILL)))) DBUG_RETURN(1); /* Historical Requirement */ plugin->data= schema_table; // shortcut for the future diff --git a/sql/sql_signal.cc b/sql/sql_signal.cc index 9da10f5b2f5..ed4d2c23d53 100644 --- a/sql/sql_signal.cc +++ b/sql/sql_signal.cc @@ -477,7 +477,7 @@ bool Signal_statement::execute(THD *thd) bool Resignal_statement::execute(THD *thd) { - MYSQL_ERROR *signaled; + Sql_condition_info *signaled; int result= TRUE; DBUG_ENTER("Resignal_statement::execute"); @@ -490,15 +490,21 @@ bool Resignal_statement::execute(THD *thd) DBUG_RETURN(result); } + MYSQL_ERROR signaled_err(thd->mem_root); + signaled_err.set(signaled->m_sql_errno, + signaled->m_sql_state, + signaled->m_level, + signaled->m_message); + if (m_cond == NULL) { /* RESIGNAL without signal_value */ - result= raise_condition(thd, signaled); + result= raise_condition(thd, &signaled_err); DBUG_RETURN(result); } /* RESIGNAL with signal_value */ - result= raise_condition(thd, signaled); + result= raise_condition(thd, &signaled_err); DBUG_RETURN(result); } diff --git a/sql/sql_statistics.cc b/sql/sql_statistics.cc index 618fd9b7799..e34b4b21819 100644 --- a/sql/sql_statistics.cc +++ b/sql/sql_statistics.cc @@ -1296,7 +1296,7 @@ public: if (tree->elements == 0) return (ulonglong) tree->elements_in_tree(); count= 0; - tree->walk(count_distinct_walk, (void*) &count); + tree->walk(table_field->table, count_distinct_walk, (void*) &count); return count; } }; @@ -1387,7 +1387,8 @@ public: is_single_comp_pk= FALSE; uint pk= table->s->primary_key; - if (table->key_info - key_info == pk && table->key_info[pk].key_parts == 1) + if ((uint) (table->key_info - key_info) == pk && + table->key_info[pk].key_parts == 1) { prefixes= 1; is_single_comp_pk= TRUE; @@ -2499,6 +2500,8 @@ int read_statistics_for_table(THD *thd, TABLE *table, TABLE_LIST *stat_tables) } } + table->stats_is_read= TRUE; + DBUG_RETURN(0); } @@ -2557,6 +2560,8 @@ bool statistics_for_tables_is_needed(THD *thd, TABLE_LIST *tables) table_share->stats_cb.stats_can_be_read && !table_share->stats_cb.stats_is_read) return TRUE; + if (table_share->stats_cb.stats_is_read) + tl->table->stats_is_read= TRUE; } } @@ -2616,6 +2621,8 @@ int read_statistics_for_tables_if_needed(THD *thd, TABLE_LIST *tables) (void) read_statistics_for_table(thd, tl->table, stat_tables); table_share->stats_cb.stats_is_read= TRUE; } + if (table_share->stats_cb.stats_is_read) + tl->table->stats_is_read= TRUE; } } @@ -3034,7 +3041,7 @@ void set_statistics_for_table(THD *thd, TABLE *table) Use_stat_tables_mode use_stat_table_mode= get_use_stat_tables_mode(thd); table->used_stat_records= (use_stat_table_mode <= COMPLEMENTARY || - !stats_cb->stats_is_read || read_stats->cardinality_is_null) ? + !table->stats_is_read || read_stats->cardinality_is_null) ? table->file->stats.records : read_stats->cardinality; KEY *key_info, *key_info_end; for (key_info= table->key_info, key_info_end= key_info+table->s->keys; @@ -3042,7 +3049,7 @@ void set_statistics_for_table(THD *thd, TABLE *table) { key_info->is_statistics_from_stat_tables= (use_stat_table_mode > COMPLEMENTARY && - stats_cb->stats_is_read && + table->stats_is_read && key_info->read_stats->avg_frequency_is_inited() && key_info->read_stats->get_avg_frequency(0) > 0.5); } diff --git a/sql/sql_string.cc b/sql/sql_string.cc index 89536b93feb..9d11677666f 100644 --- a/sql/sql_string.cc +++ b/sql/sql_string.cc @@ -42,7 +42,9 @@ bool String::real_alloc(uint32 length) if (Alloced_length < arg_length) { free(); - if (!(Ptr=(char*) my_malloc(arg_length,MYF(MY_WME)))) + if (!(Ptr=(char*) my_malloc(arg_length,MYF(MY_WME | + (thread_specific ? + MY_THREAD_SPECIFIC : 0))))) return TRUE; Alloced_length=arg_length; alloced=1; @@ -90,10 +92,16 @@ bool String::realloc_raw(uint32 alloc_length) return TRUE; /* Overflow */ if (alloced) { - if (!(new_ptr= (char*) my_realloc(Ptr,len,MYF(MY_WME)))) + if (!(new_ptr= (char*) my_realloc(Ptr,len, + MYF(MY_WME | + (thread_specific ? + MY_THREAD_SPECIFIC : 0))))) return TRUE; // Signal error } - else if ((new_ptr= (char*) my_malloc(len,MYF(MY_WME)))) + else if ((new_ptr= (char*) my_malloc(len, + MYF(MY_WME | + (thread_specific ? + MY_THREAD_SPECIFIC : 0))))) { if (str_length > len - 1) str_length= 0; @@ -768,79 +776,6 @@ String *copy_if_not_alloced(String *to,String *from,uint32 from_length) Help functions ****************************************************************************/ -/* - copy a string from one character set to another - - SYNOPSIS - copy_and_convert() - to Store result here - to_cs Character set of result string - from Copy from here - from_length Length of from string - from_cs From character set - - NOTES - 'to' must be big enough as form_length * to_cs->mbmaxlen - - RETURN - length of bytes copied to 'to' -*/ - - -static uint32 -copy_and_convert_extended(char *to, uint32 to_length, CHARSET_INFO *to_cs, - const char *from, uint32 from_length, - CHARSET_INFO *from_cs, - uint *errors) -{ - int cnvres; - my_wc_t wc; - const uchar *from_end= (const uchar*) from+from_length; - char *to_start= to; - uchar *to_end= (uchar*) to+to_length; - my_charset_conv_mb_wc mb_wc= from_cs->cset->mb_wc; - my_charset_conv_wc_mb wc_mb= to_cs->cset->wc_mb; - uint error_count= 0; - - while (1) - { - if ((cnvres= (*mb_wc)(from_cs, &wc, (uchar*) from, - from_end)) > 0) - from+= cnvres; - else if (cnvres == MY_CS_ILSEQ) - { - error_count++; - from++; - wc= '?'; - } - else if (cnvres > MY_CS_TOOSMALL) - { - /* - A correct multibyte sequence detected - But it doesn't have Unicode mapping. - */ - error_count++; - from+= (-cnvres); - wc= '?'; - } - else - break; // Not enough characters - -outp: - if ((cnvres= (*wc_mb)(to_cs, wc, (uchar*) to, to_end)) > 0) - to+= cnvres; - else if (cnvres == MY_CS_ILUNI && wc != '?') - { - error_count++; - wc= '?'; - goto outp; - } - else - break; - } - *errors= error_count; - return (uint32) (to - to_start); -} /* diff --git a/sql/sql_string.h b/sql/sql_string.h index 2966fc2a920..58cda343dac 100644 --- a/sql/sql_string.h +++ b/sql/sql_string.h @@ -56,23 +56,26 @@ class String { char *Ptr; uint32 str_length,Alloced_length, extra_alloc; - bool alloced; + bool alloced,thread_specific; CHARSET_INFO *str_charset; public: String() { - Ptr=0; str_length=Alloced_length=extra_alloc=0; alloced=0; + Ptr=0; str_length=Alloced_length=extra_alloc=0; + alloced= thread_specific= 0; str_charset= &my_charset_bin; } String(uint32 length_arg) { - alloced=0; Alloced_length= extra_alloc= 0; (void) real_alloc(length_arg); + alloced= thread_specific= 0; + Alloced_length= extra_alloc= 0; (void) real_alloc(length_arg); str_charset= &my_charset_bin; } String(const char *str, CHARSET_INFO *cs) { Ptr=(char*) str; str_length= (uint32) strlen(str); - Alloced_length= extra_alloc= 0; alloced=0; + Alloced_length= extra_alloc= 0; + alloced= thread_specific= 0; str_charset=cs; } /* @@ -82,18 +85,21 @@ public: */ String(const char *str,uint32 len, CHARSET_INFO *cs) { - Ptr=(char*) str; str_length=len; Alloced_length= extra_alloc=0; alloced=0; + Ptr=(char*) str; str_length=len; Alloced_length= extra_alloc=0; + alloced= thread_specific= 0; str_charset=cs; } String(char *str,uint32 len, CHARSET_INFO *cs) { - Ptr=(char*) str; Alloced_length=str_length=len; extra_alloc= 0; alloced=0; + Ptr=(char*) str; Alloced_length=str_length=len; extra_alloc= 0; + alloced= thread_specific= 0; str_charset=cs; } String(const String &str) { Ptr=str.Ptr ; str_length=str.str_length ; - Alloced_length=str.Alloced_length; extra_alloc= 0; alloced=0; + Alloced_length=str.Alloced_length; extra_alloc= 0; + alloced= thread_specific= 0; str_charset=str.str_charset; } static void *operator new(size_t size, MEM_ROOT *mem_root) throw () @@ -108,6 +114,12 @@ public: { /* never called */ } ~String() { free(); } + /* Mark variable thread specific it it's not allocated already */ + inline void set_thread_specific() + { + if (!alloced) + thread_specific= 1; + } inline void set_charset(CHARSET_INFO *charset_arg) { str_charset= charset_arg; } inline CHARSET_INFO *charset() const { return str_charset; } @@ -332,6 +344,7 @@ public: Ptr=s.Ptr ; str_length=s.str_length ; Alloced_length=s.Alloced_length; extra_alloc= s.extra_alloc; alloced= s.alloced; + thread_specific= s.thread_specific; s.alloced= 0; } bool append(const String &s); diff --git a/sql/sql_table.cc b/sql/sql_table.cc index 31e4110c4b1..3c094e1740e 100644 --- a/sql/sql_table.cc +++ b/sql/sql_table.cc @@ -961,7 +961,7 @@ static int execute_ddl_log_action(THD *thd, DDL_LOG_ENTRY *ddl_log_entry) ddl_log_entry->handler_name)); handler_name.str= (char*)ddl_log_entry->handler_name; handler_name.length= strlen(ddl_log_entry->handler_name); - init_sql_alloc(&mem_root, TABLE_ALLOC_BLOCK_SIZE, 0); + init_sql_alloc(&mem_root, TABLE_ALLOC_BLOCK_SIZE, 0, MYF(MY_THREAD_SPECIFIC)); if (!strcmp(ddl_log_entry->handler_name, reg_ext)) frm_action= TRUE; else @@ -1525,7 +1525,7 @@ void execute_ddl_log_recovery() global_ddl_log.recovery_phase= FALSE; delete thd; /* Remember that we don't have a THD */ - my_pthread_setspecific_ptr(THR_THD, 0); + set_current_thd(0); DBUG_VOID_RETURN; } @@ -5395,7 +5395,7 @@ mysql_compare_tables(TABLE *table, if (table->s->tmp_table == NO_TMP_TABLE) { (void) delete_statistics_for_index(thd, table, table_key, FALSE); - if (table_key - table->key_info == table->s->primary_key) + if ((uint) (table_key - table->key_info) == table->s->primary_key) { KEY *tab_key_info= table->key_info; for (uint j=0; j < table->s->keys; j++, tab_key_info++) @@ -7555,7 +7555,8 @@ copy_data_between_tables(THD *thd, TABLE *from,TABLE *to, else { from->sort.io_cache=(IO_CACHE*) my_malloc(sizeof(IO_CACHE), - MYF(MY_FAE | MY_ZEROFILL)); + MYF(MY_FAE | MY_ZEROFILL | + MY_THREAD_SPECIFIC)); bzero((char *) &tables, sizeof(tables)); tables.table= from; tables.alias= tables.table_name= from->s->table_name.str; diff --git a/sql/sql_test.cc b/sql/sql_test.cc index 25ab84fe4db..5b3286d77b0 100644 --- a/sql/sql_test.cc +++ b/sql/sql_test.cc @@ -484,7 +484,9 @@ static void display_table_locks(void) void *saved_base; DYNAMIC_ARRAY saved_table_locks; - (void) my_init_dynamic_array(&saved_table_locks,sizeof(TABLE_LOCK_INFO), cached_open_tables() + 20,50); + (void) my_init_dynamic_array(&saved_table_locks,sizeof(TABLE_LOCK_INFO), + cached_open_tables() + 20, 50, + MYF(MY_THREAD_SPECIFIC)); mysql_mutex_lock(&THR_LOCK_lock); for (list= thr_lock_thread_list; list; list= list_rest(list)) { diff --git a/sql/sql_time.cc b/sql/sql_time.cc index 5c0471fdfaa..57dbd979933 100644 --- a/sql/sql_time.cc +++ b/sql/sql_time.cc @@ -1,5 +1,5 @@ /* Copyright (c) 2000, 2010, Oracle and/or its affiliates. - Copyright (c) 2009-2011 Monty Program Ab + Copyright (c) 2009, 2013 Monty Program Ab. 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 @@ -154,7 +154,7 @@ bool get_date_from_daynr(long daynr,uint *ret_year,uint *ret_month, uchar *month_pos; DBUG_ENTER("get_date_from_daynr"); - if (daynr < 365 || daynr > MAX_DAY_NUMBER) + if (daynr < 366 || daynr > MAX_DAY_NUMBER) DBUG_RETURN(1); year= (uint) (daynr*100 / 36525L); diff --git a/sql/sql_trigger.cc b/sql/sql_trigger.cc index aff00f9fcf4..d7d902bc6b0 100644 --- a/sql/sql_trigger.cc +++ b/sql/sql_trigger.cc @@ -1779,7 +1779,7 @@ bool Table_triggers_list::drop_all_triggers(THD *thd, char *db, char *name) DBUG_ENTER("drop_all_triggers"); bzero(&table, sizeof(table)); - init_sql_alloc(&table.mem_root, 8192, 0); + init_sql_alloc(&table.mem_root, 8192, 0, MYF(0)); if (Table_triggers_list::check_n_load(thd, db, name, &table, 1)) { @@ -1999,7 +1999,7 @@ bool Table_triggers_list::change_table_name(THD *thd, const char *db, DBUG_ENTER("change_table_name"); bzero(&table, sizeof(table)); - init_sql_alloc(&table.mem_root, 8192, 0); + init_sql_alloc(&table.mem_root, 8192, 0, MYF(0)); /* This method interfaces the mysql server code protected by diff --git a/sql/sql_truncate.cc b/sql/sql_truncate.cc index 9d4ca5e1373..4b77344c042 100644 --- a/sql/sql_truncate.cc +++ b/sql/sql_truncate.cc @@ -24,7 +24,7 @@ #include "sql_acl.h" // DROP_ACL #include "sql_parse.h" // check_one_table_access() #include "sql_truncate.h" -#include "sql_show.h" +#include "sql_show.h" //append_identifier() /** @@ -263,6 +263,7 @@ static bool recreate_temporary_table(THD *thd, TABLE *table) DBUG_ENTER("recreate_temporary_table"); memset(&create_info, 0, sizeof(create_info)); + create_info.options|= HA_LEX_CREATE_TMP_TABLE; table->file->info(HA_STATUS_AUTO | HA_STATUS_NO_LOCK); diff --git a/sql/sql_udf.cc b/sql/sql_udf.cc index 9069d876609..c792dca873c 100644 --- a/sql/sql_udf.cc +++ b/sql/sql_udf.cc @@ -151,7 +151,7 @@ void udf_init() mysql_rwlock_init(key_rwlock_THR_LOCK_udf, &THR_LOCK_udf); - init_sql_alloc(&mem, UDF_ALLOC_BLOCK_SIZE, 0); + init_sql_alloc(&mem, UDF_ALLOC_BLOCK_SIZE, 0, MYF(0)); THD *new_thd = new THD; if (!new_thd || my_hash_init(&udf_hash,system_charset_info,32,0,0,get_hash_key, NULL, 0)) @@ -258,7 +258,7 @@ end: close_mysql_tables(new_thd); delete new_thd; /* Remember that we don't have a THD */ - my_pthread_setspecific_ptr(THR_THD, 0); + set_current_thd(0); DBUG_VOID_RETURN; } diff --git a/sql/sql_union.cc b/sql/sql_union.cc index bda9b919663..106c134223e 100644 --- a/sql/sql_union.cc +++ b/sql/sql_union.cc @@ -716,7 +716,7 @@ bool st_select_lex_unit::exec() ER(ER_QUERY_EXCEEDED_ROWS_EXAMINED_LIMIT), thd->accessed_rows_and_keys, thd->lex->limit_rows_examined->val_uint()); - thd->killed= NOT_KILLED; + thd->reset_killed(); break; } } diff --git a/sql/sql_update.cc b/sql/sql_update.cc index 066bc2c24f7..0d1cb7de5f2 100644 --- a/sql/sql_update.cc +++ b/sql/sql_update.cc @@ -493,7 +493,8 @@ int mysql_update(THD *thd, ha_rows found_rows; table->sort.io_cache = (IO_CACHE *) my_malloc(sizeof(IO_CACHE), - MYF(MY_FAE | MY_ZEROFILL)); + MYF(MY_FAE | MY_ZEROFILL | + MY_THREAD_SPECIFIC)); if (!(sortorder=make_unireg_sortorder(order, &length, NULL)) || (table->sort.found_records= filesort(thd, table, sortorder, length, select, limit, @@ -2233,11 +2234,16 @@ err: } err2: - (void) table->file->ha_rnd_end(); - (void) tmp_table->file->ha_rnd_end(); + if (table->file->inited) + (void) table->file->ha_rnd_end(); + if (tmp_table->file->inited) + (void) tmp_table->file->ha_rnd_end(); check_opt_it.rewind(); while (TABLE *tbl= check_opt_it++) - tbl->file->ha_rnd_end(); + { + if (tbl->file->inited) + (void) tbl->file->ha_rnd_end(); + } if (updated != org_updated) { diff --git a/sql/sql_yacc.yy b/sql/sql_yacc.yy index ac904c990ed..56e7db96a1a 100644 --- a/sql/sql_yacc.yy +++ b/sql/sql_yacc.yy @@ -993,11 +993,10 @@ bool my_yyoverflow(short **a, YYSTYPE **b, ulong *yystacksize); %token COLLATION_SYM /* SQL-2003-N */ %token COLUMNS %token COLUMN_ADD_SYM +%token COLUMN_CHECK_SYM %token COLUMN_CREATE_SYM %token COLUMN_DELETE_SYM -%token COLUMN_EXISTS_SYM %token COLUMN_GET_SYM -%token COLUMN_LIST_SYM %token COLUMN_SYM /* SQL-2003-R */ %token COLUMN_NAME_SYM /* SQL-2003-N */ %token COMMENT_SYM @@ -8566,7 +8565,7 @@ dyncall_create_element: alloc_root(YYTHD->mem_root, sizeof(DYNCALL_CREATE_DEF)); if ($$ == NULL) MYSQL_YYABORT; - $$->num= $1; + $$->key= $1; $$->value= $3; $$->type= (DYNAMIC_COLUMN_TYPE)$4; $$->cs= lex->charset; @@ -9120,16 +9119,9 @@ function_call_nonkeyword: MYSQL_YYABORT; } | - COLUMN_EXISTS_SYM '(' expr ',' expr ')' + COLUMN_CHECK_SYM '(' expr ')' { - $$= new (YYTHD->mem_root) Item_func_dyncol_exists($3, $5); - if ($$ == NULL) - MYSQL_YYABORT; - } - | - COLUMN_LIST_SYM '(' expr ')' - { - $$= new (YYTHD->mem_root) Item_func_dyncol_list($3); + $$= new (YYTHD->mem_root) Item_func_dyncol_check($3); if ($$ == NULL) MYSQL_YYABORT; } @@ -11758,11 +11750,19 @@ show_param: { LEX *lex=Lex; lex->sql_command= SQLCOM_SHOW_AUTHORS; + push_warning_printf(YYTHD, MYSQL_ERROR::WARN_LEVEL_WARN, + ER_WARN_DEPRECATED_SYNTAX_NO_REPLACEMENT, + ER(ER_WARN_DEPRECATED_SYNTAX_NO_REPLACEMENT), + "SHOW AUTHORS"); } | CONTRIBUTORS_SYM { LEX *lex=Lex; lex->sql_command= SQLCOM_SHOW_CONTRIBUTORS; + push_warning_printf(YYTHD, MYSQL_ERROR::WARN_LEVEL_WARN, + ER_WARN_DEPRECATED_SYNTAX_NO_REPLACEMENT, + ER(ER_WARN_DEPRECATED_SYNTAX_NO_REPLACEMENT), + "SHOW CONTRIBUTORS"); } | PRIVILEGES { @@ -13277,11 +13277,10 @@ keyword: | CHECKPOINT_SYM {} | CLOSE_SYM {} | COLUMN_ADD_SYM {} + | COLUMN_CHECK_SYM {} | COLUMN_CREATE_SYM {} | COLUMN_DELETE_SYM {} - | COLUMN_EXISTS_SYM {} | COLUMN_GET_SYM {} - | COLUMN_LIST_SYM {} | COMMENT_SYM {} | COMMIT_SYM {} | CONTAINS_SYM {} diff --git a/sql/sys_vars.cc b/sql/sys_vars.cc index c585f6681f0..9b55c09a170 100644 --- a/sql/sys_vars.cc +++ b/sql/sys_vars.cc @@ -3674,7 +3674,7 @@ Sys_slave_skip_counter("sql_slave_skip_counter", "Skip the next N events from the master log", SESSION_VAR(slave_skip_counter), NO_CMD_LINE, - offsetof(Master_info, rli.slave_skip_counter), + my_offsetof(Master_info, rli.slave_skip_counter), VALID_RANGE(0, UINT_MAX), DEFAULT(0), BLOCK_SIZE(1), ON_UPDATE(update_slave_skip_counter)); @@ -3693,7 +3693,7 @@ Sys_max_relay_log_size( "max_relay_log_size", "set to max_binlog_size", SESSION_VAR(max_relay_log_size), CMD_LINE(REQUIRED_ARG), - offsetof(Master_info, rli.max_relay_log_size), + my_offsetof(Master_info, rli.max_relay_log_size), VALID_RANGE(0, 1024L*1024*1024), DEFAULT(0), BLOCK_SIZE(IO_SIZE), ON_UPDATE(update_max_relay_log_size)); diff --git a/sql/table.cc b/sql/table.cc index 22d4eed1b12..c8dc2b4ed5a 100644 --- a/sql/table.cc +++ b/sql/table.cc @@ -307,7 +307,7 @@ TABLE_SHARE *alloc_table_share(TABLE_LIST *table_list, char *key, path_length= build_table_filename(path, sizeof(path) - 1, table_list->db, table_list->table_name, "", 0); - init_sql_alloc(&mem_root, TABLE_ALLOC_BLOCK_SIZE, 0); + init_sql_alloc(&mem_root, TABLE_ALLOC_BLOCK_SIZE, 0, MYF(0)); if (multi_alloc_root(&mem_root, &share, sizeof(*share), &key_buff, key_length, @@ -340,7 +340,7 @@ TABLE_SHARE *alloc_table_share(TABLE_LIST *table_list, char *key, share->free_tables.empty(); share->m_flush_tickets.empty(); - init_sql_alloc(&share->stats_cb.mem_root, TABLE_ALLOC_BLOCK_SIZE, 0); + init_sql_alloc(&share->stats_cb.mem_root, TABLE_ALLOC_BLOCK_SIZE, 0, MYF(0)); memcpy((char*) &share->mem_root, (char*) &mem_root, sizeof(mem_root)); mysql_mutex_init(key_TABLE_SHARE_LOCK_ha_data, @@ -381,7 +381,12 @@ void init_tmp_table_share(THD *thd, TABLE_SHARE *share, const char *key, DBUG_PRINT("enter", ("table: '%s'.'%s'", key, table_name)); bzero((char*) share, sizeof(*share)); - init_sql_alloc(&share->mem_root, TABLE_ALLOC_BLOCK_SIZE, 0); + /* + This can't be MY_THREAD_SPECIFIC for slaves as they are freed + during cleanup() from Relay_log_info::close_temporary_tables() + */ + init_sql_alloc(&share->mem_root, TABLE_ALLOC_BLOCK_SIZE, 0, + MYF(thd->slave_thread ? 0 : MY_THREAD_SPECIFIC)); share->table_category= TABLE_CATEGORY_TEMPORARY; share->tmp_table= INTERNAL_TMP_TABLE; share->db.str= (char*) key; @@ -2158,9 +2163,11 @@ end: @brief Unpack the definition of a virtual column from its linear representation - @parm + @param thd The thread object @param + mem_root The mem_root object where to allocated memory + @param table The table containing the virtual column @param field The field for the virtual @@ -2189,6 +2196,7 @@ end: TRUE Otherwise */ bool unpack_vcol_info_from_frm(THD *thd, + MEM_ROOT *mem_root, TABLE *table, Field *field, LEX_STRING *vcol_expr, @@ -2216,7 +2224,7 @@ bool unpack_vcol_info_from_frm(THD *thd, "PARSE_VCOL_EXPR (<expr_string_from_frm>)". */ - if (!(vcol_expr_str= (char*) alloc_root(&table->mem_root, + if (!(vcol_expr_str= (char*) alloc_root(mem_root, vcol_expr->length + parse_vcol_keyword.length + 3))) { @@ -2250,10 +2258,10 @@ bool unpack_vcol_info_from_frm(THD *thd, We need to use CONVENTIONAL_EXECUTION here to ensure that any new items created by fix_fields() are not reverted. */ - Query_arena expr_arena(&table->mem_root, + Query_arena expr_arena(mem_root, Query_arena::STMT_CONVENTIONAL_EXECUTION); - if (!(vcol_arena= (Query_arena *) alloc_root(&table->mem_root, - sizeof(Query_arena)))) + if (!(vcol_arena= (Query_arena *) alloc_root(mem_root, + sizeof(Query_arena)))) goto err; *vcol_arena= expr_arena; table->expr_arena= vcol_arena; @@ -2336,7 +2344,7 @@ int open_table_from_share(THD *thd, TABLE_SHARE *share, const char *alias, uint records, i, bitmap_size; bool error_reported= FALSE; uchar *record, *bitmaps; - Field **field_ptr, **vfield_ptr, **dfield_ptr; + Field **field_ptr, **UNINIT_VAR(vfield_ptr), **UNINIT_VAR(dfield_ptr); uint8 save_context_analysis_only= thd->lex->context_analysis_only; DBUG_ENTER("open_table_from_share"); DBUG_PRINT("enter",("name: '%s.%s' form: 0x%lx", share->db.str, @@ -2351,7 +2359,7 @@ int open_table_from_share(THD *thd, TABLE_SHARE *share, const char *alias, outparam->db_stat= db_stat; outparam->write_row_record= NULL; - init_sql_alloc(&outparam->mem_root, TABLE_ALLOC_BLOCK_SIZE, 0); + init_sql_alloc(&outparam->mem_root, TABLE_ALLOC_BLOCK_SIZE, 0, MYF(0)); if (outparam->alias.copy(alias, strlen(alias), table_alias_charset)) goto err; @@ -2520,6 +2528,7 @@ int open_table_from_share(THD *thd, TABLE_SHARE *share, const char *alias, if (share->vfields && (*field_ptr)->vcol_info) { if (unpack_vcol_info_from_frm(thd, + &outparam->mem_root, outparam, *field_ptr, &(*field_ptr)->vcol_info->expr_str, @@ -5000,19 +5009,33 @@ TABLE *TABLE_LIST::get_real_join_table() DBUG_ASSERT(tbl->derived == NULL || tbl->derived->first_select()->next_select() == NULL); - if (tbl->table) - table= tbl->table; - tbl= (tbl->view != NULL ? - tbl->view->select_lex.get_table_list() : - tbl->derived->first_select()->get_table_list()); - - /* find left table in outer join on this level */ - while(tbl->outer_join & JOIN_TYPE_RIGHT) { - DBUG_ASSERT(tbl->next_local); - tbl= tbl->next_local; + List_iterator_fast<TABLE_LIST> ti; + { + List_iterator_fast<TABLE_LIST> + ti(tbl->view != NULL ? + tbl->view->select_lex.top_join_list : + tbl->derived->first_select()->top_join_list); + for (;;) + { + tbl= NULL; + /* + Find left table in outer join on this level + (the list is reverted). + */ + for (TABLE_LIST *t= ti++; t; t= ti++) + tbl= t; + /* + It is impossible that the list is empty + so tbl can't be NULL after above loop. + */ + if (!tbl->nested_join) + break; + /* go deeper if we've found nested join */ + ti= tbl->nested_join->join_list; + } + } } - } return tbl->table; diff --git a/sql/table.h b/sql/table.h index ecad83cff22..1a567ae75d1 100644 --- a/sql/table.h +++ b/sql/table.h @@ -1196,6 +1196,7 @@ public: bool no_partitions_used; /* If true, all partitions have been pruned away */ #endif uint max_keys; /* Size of allocated key_info array. */ + bool stats_is_read; /* Persistent statistics is read for the table */ MDL_ticket *mdl_ticket; void init(THD *thd, TABLE_LIST *tl); @@ -2411,6 +2412,9 @@ void init_mdl_requests(TABLE_LIST *table_list); int open_table_from_share(THD *thd, TABLE_SHARE *share, const char *alias, uint db_stat, uint prgflag, uint ha_open_flags, TABLE *outparam, bool is_create_table); +bool unpack_vcol_info_from_frm(THD *thd, MEM_ROOT *mem_root, + TABLE *table, Field *field, + LEX_STRING *vcol_expr, bool *error_reported); TABLE_SHARE *alloc_table_share(TABLE_LIST *table_list, char *key, uint key_length); void init_tmp_table_share(THD *thd, TABLE_SHARE *share, const char *key, diff --git a/sql/thr_malloc.cc b/sql/thr_malloc.cc index cedcbefc26f..8c7db0673ac 100644 --- a/sql/thr_malloc.cc +++ b/sql/thr_malloc.cc @@ -61,9 +61,10 @@ extern "C" { } } -void init_sql_alloc(MEM_ROOT *mem_root, uint block_size, uint pre_alloc) +void init_sql_alloc(MEM_ROOT *mem_root, uint block_size, uint pre_alloc, + myf my_flags) { - init_alloc_root(mem_root, block_size, pre_alloc); + init_alloc_root(mem_root, block_size, pre_alloc, my_flags); mem_root->error_handler=sql_alloc_error_handler; } diff --git a/sql/thr_malloc.h b/sql/thr_malloc.h index 81b7d3cc238..0b17c5cdaf1 100644 --- a/sql/thr_malloc.h +++ b/sql/thr_malloc.h @@ -20,7 +20,8 @@ typedef struct st_mem_root MEM_ROOT; -void init_sql_alloc(MEM_ROOT *root, uint block_size, uint pre_alloc_size); +void init_sql_alloc(MEM_ROOT *root, uint block_size, uint pre_alloc_size, + myf my_flags); void *sql_alloc(size_t); void *sql_calloc(size_t); char *sql_strdup(const char *str); diff --git a/sql/tztime.cc b/sql/tztime.cc index ba24cab9ca7..b16cc65d6bb 100644 --- a/sql/tztime.cc +++ b/sql/tztime.cc @@ -1637,7 +1637,7 @@ my_tz_init(THD *org_thd, const char *default_tzname, my_bool bootstrap) my_hash_free(&tz_names); goto end; } - init_sql_alloc(&tz_storage, 32 * 1024, 0); + init_sql_alloc(&tz_storage, 32 * 1024, 0, MYF(0)); mysql_mutex_init(key_tz_LOCK, &tz_LOCK, MY_MUTEX_INIT_FAST); tz_inited= 1; @@ -1718,14 +1718,11 @@ my_tz_init(THD *org_thd, const char *default_tzname, my_bool bootstrap) } table= tz_tables[0].table; - /* - It is OK to ignore ha_index_init()/ha_index_end() return values since - mysql.time_zone* tables are MyISAM and these operations always succeed - for MyISAM. - */ - (void)table->file->ha_index_init(0, 1); - table->use_all_columns(); + if (table->file->ha_index_init(0, 1)) + goto end_with_close; + + table->use_all_columns(); tz_leapcnt= 0; res= table->file->ha_index_first(table->record[0]); @@ -1803,7 +1800,7 @@ end: else { /* Remember that we don't have a THD */ - my_pthread_setspecific_ptr(THR_THD, 0); + set_current_thd(0); my_pthread_setspecific_ptr(THR_MALLOC, 0); } @@ -1913,12 +1910,8 @@ tz_load_from_open_tables(const String *tz_name, TABLE_LIST *tz_tables) tz_tables= tz_tables->next_local; table->field[0]->store(tz_name->ptr(), tz_name->length(), &my_charset_latin1); - /* - It is OK to ignore ha_index_init()/ha_index_end() return values since - mysql.time_zone* tables are MyISAM and these operations always succeed - for MyISAM. - */ - (void)table->file->ha_index_init(0, 1); + if (table->file->ha_index_init(0, 1)) + goto end; if (table->file->ha_index_read_map(table->record[0], table->field[0]->ptr, HA_WHOLE_KEY, HA_READ_KEY_EXACT)) @@ -1951,7 +1944,8 @@ tz_load_from_open_tables(const String *tz_name, TABLE_LIST *tz_tables) field->get_key_image(keybuff, min(field->key_length(), sizeof(keybuff)), Field::itRAW); - (void)table->file->ha_index_init(0, 1); + if (table->file->ha_index_init(0, 1)) + goto end; if (table->file->ha_index_read_map(table->record[0], keybuff, HA_WHOLE_KEY, HA_READ_KEY_EXACT)) @@ -1983,7 +1977,8 @@ tz_load_from_open_tables(const String *tz_name, TABLE_LIST *tz_tables) field->get_key_image(keybuff, min(field->key_length(), sizeof(keybuff)), Field::itRAW); - (void)table->file->ha_index_init(0, 1); + if (table->file->ha_index_init(0, 1)) + goto end; res= table->file->ha_index_read_map(table->record[0], keybuff, (key_part_map)1, HA_READ_KEY_EXACT); @@ -2053,7 +2048,8 @@ tz_load_from_open_tables(const String *tz_name, TABLE_LIST *tz_tables) */ table= tz_tables->table; table->field[0]->store((longlong) tzid, TRUE); - (void)table->file->ha_index_init(0, 1); + if (table->file->ha_index_init(0, 1)) + goto end; res= table->file->ha_index_read_map(table->record[0], keybuff, (key_part_map)1, HA_READ_KEY_EXACT); @@ -2187,8 +2183,8 @@ tz_load_from_open_tables(const String *tz_name, TABLE_LIST *tz_tables) end: - if (table) - (void)table->file->ha_index_end(); + if (table && table->file->inited) + (void) table->file->ha_index_end(); DBUG_RETURN(return_val); } @@ -2541,7 +2537,7 @@ scan_tz_dir(char * name_end) } else if (MY_S_ISREG(cur_dir->dir_entry[i].mystat->st_mode)) { - init_alloc_root(&tz_storage, 32768, 0); + init_alloc_root(&tz_storage, 32768, 0, MYF(MY_THREAD_SPECIFIC)); if (!tz_load(fullname, &tz_info, &tz_storage)) print_tz_as_sql(root_name_end + 1, &tz_info); else @@ -2599,7 +2595,7 @@ main(int argc, char **argv) } else { - init_alloc_root(&tz_storage, 32768, 0); + init_alloc_root(&tz_storage, 32768, 0, MYF(0)); if (strcmp(argv[1], "--leap") == 0) { @@ -2676,7 +2672,7 @@ main(int argc, char **argv) MY_INIT(argv[0]); - init_alloc_root(&tz_storage, 32768, 0); + init_alloc_root(&tz_storage, 32768, MYF(0)); /* let us set some well known timezone */ setenv("TZ", "MET", 1); diff --git a/sql/uniques.cc b/sql/uniques.cc index c246cd637bd..9fa06311ece 100644 --- a/sql/uniques.cc +++ b/sql/uniques.cc @@ -77,17 +77,21 @@ int unique_intersect_write_to_ptrs(uchar* key, element_count count, Unique *uniq Unique::Unique(qsort_cmp2 comp_func, void * comp_func_fixed_arg, uint size_arg, ulonglong max_in_memory_size_arg, uint min_dupl_count_arg) - :max_in_memory_size(max_in_memory_size_arg), size(size_arg), elements(0) + :max_in_memory_size(max_in_memory_size_arg), + record_pointers(NULL), + size(size_arg), + elements(0) { min_dupl_count= min_dupl_count_arg; full_size= size; if (min_dupl_count_arg) full_size+= sizeof(element_count); my_b_clear(&file); - init_tree(&tree, (ulong) (max_in_memory_size / 16), 0, size, comp_func, 0, - NULL, comp_func_fixed_arg); + init_tree(&tree, (ulong) (max_in_memory_size / 16), 0, size, comp_func, + NULL, comp_func_fixed_arg, MYF(MY_THREAD_SPECIFIC)); /* If the following fail's the next add will also fail */ - my_init_dynamic_array(&file_ptrs, sizeof(BUFFPEK), 16, 16); + my_init_dynamic_array(&file_ptrs, sizeof(BUFFPEK), 16, 16, + MYF(MY_THREAD_SPECIFIC)); /* If you change the following, change it in get_max_elements function, too. */ @@ -581,6 +585,7 @@ end: SYNOPSIS Unique:walk() All params are 'IN': + table parameter for the call of the merge method action function-visitor, typed in include/my_tree.h function is called for each unique element arg argument for visitor, which is passed to it on each call @@ -589,69 +594,67 @@ end: <> 0 error */ -bool Unique::walk(tree_walk_action action, void *walk_action_arg) +bool Unique::walk(TABLE *table, tree_walk_action action, void *walk_action_arg) { - int res; + int res= 0; uchar *merge_buffer; if (elements == 0) /* the whole tree is in memory */ return tree_walk(&tree, action, walk_action_arg, left_root_right); + table->sort.found_records=elements+tree.elements_in_tree; /* flush current tree to the file to have some memory for merge buffer */ if (flush()) return 1; if (flush_io_cache(&file) || reinit_io_cache(&file, READ_CACHE, 0L, 0, 0)) return 1; - if (!(merge_buffer= (uchar *) my_malloc((ulong) max_in_memory_size, MYF(0)))) + ulong buff_sz= (max_in_memory_size / full_size + 1) * full_size; + if (!(merge_buffer= (uchar *) my_malloc(buff_sz, MYF(MY_THREAD_SPECIFIC)))) return 1; - res= merge_walk(merge_buffer, (ulong) max_in_memory_size, size, - (BUFFPEK *) file_ptrs.buffer, - (BUFFPEK *) file_ptrs.buffer + file_ptrs.elements, - action, walk_action_arg, - tree.compare, tree.custom_arg, &file); + if (buff_sz < full_size * (file_ptrs.elements + 1UL)) + res= merge(table, merge_buffer, buff_sz >= full_size * MERGEBUFF2) ; + + if (!res) + { + res= merge_walk(merge_buffer, (ulong) max_in_memory_size, full_size, + (BUFFPEK *) file_ptrs.buffer, + (BUFFPEK *) file_ptrs.buffer + file_ptrs.elements, + action, walk_action_arg, + tree.compare, tree.custom_arg, &file); + } my_free(merge_buffer); return res; } + /* - Modify the TABLE element so that when one calls init_records() - the rows will be read in priority order. -*/ + DESCRIPTION + Perform multi-pass sort merge of the elements accessed through table->sort, + using the buffer buff as the merge buffer. The last pass is not performed + if without_last_merge is TRUE. + SYNOPSIS + Unique:merge() + All params are 'IN': + table the parameter to access sort context + buff merge buffer + without_last_merge TRUE <=> do not perform the last merge + RETURN VALUE + 0 OK + <> 0 error + */ -bool Unique::get(TABLE *table) +bool Unique::merge(TABLE *table, uchar *buff, bool without_last_merge) { - table->sort.found_records=elements+tree.elements_in_tree; - if (my_b_tell(&file) == 0) - { - /* Whole tree is in memory; Don't use disk if you don't need to */ - if ((record_pointers=table->sort.record_pointers= (uchar*) - my_malloc(size * tree.elements_in_tree, MYF(0)))) - { - tree_walk_action action= min_dupl_count ? - (tree_walk_action) unique_intersect_write_to_ptrs : - (tree_walk_action) unique_write_to_ptrs; - filtered_out_elems= 0; - (void) tree_walk(&tree, action, - this, left_root_right); - table->sort.found_records-= filtered_out_elems; - return 0; - } - } - /* Not enough memory; Save the result to file && free memory used by tree */ - if (flush()) - return 1; - - IO_CACHE *outfile=table->sort.io_cache; + IO_CACHE *outfile= table->sort.io_cache; BUFFPEK *file_ptr= (BUFFPEK*) file_ptrs.buffer; uint maxbuffer= file_ptrs.elements - 1; - uchar *sort_buffer; my_off_t save_pos; - bool error=1; - - /* Open cached file if it isn't open */ - outfile=table->sort.io_cache=(IO_CACHE*) my_malloc(sizeof(IO_CACHE), - MYF(MY_ZEROFILL)); + bool error= 1; + /* Open cached file if it isn't open */ + if (!outfile) + outfile= table->sort.io_cache= (IO_CACHE*) my_malloc(sizeof(IO_CACHE), + MYF(MY_THREAD_SPECIFIC|MY_ZEROFILL)); if (!outfile || (! my_b_inited(outfile) && open_cached_file(outfile,mysql_tmpdir,TEMP_PREFIX,READ_RECORD_BUFFER, @@ -662,20 +665,16 @@ bool Unique::get(TABLE *table) Sort_param sort_param; bzero((char*) &sort_param,sizeof(sort_param)); sort_param.max_rows= elements; - sort_param.sort_form=table; + sort_param.sort_form= table; sort_param.rec_length= sort_param.sort_length= sort_param.ref_length= full_size; sort_param.min_dupl_count= min_dupl_count; sort_param.res_length= 0; sort_param.max_keys_per_buffer= (uint) (max_in_memory_size / sort_param.sort_length); - sort_param.not_killable=1; + sort_param.not_killable= 1; - if (!(sort_buffer=(uchar*) my_malloc((sort_param.max_keys_per_buffer+1) * - sort_param.sort_length, - MYF(0)))) - return 1; - sort_param.unique_buff= sort_buffer+(sort_param.max_keys_per_buffer * + sort_param.unique_buff= buff +(sort_param.max_keys_per_buffer * sort_param.sort_length); sort_param.compare= (qsort2_cmp) buffpek_compare; @@ -683,27 +682,74 @@ bool Unique::get(TABLE *table) sort_param.cmp_context.key_compare_arg= tree.custom_arg; /* Merge the buffers to one file, removing duplicates */ - if (merge_many_buff(&sort_param,sort_buffer,file_ptr,&maxbuffer,&file)) + if (merge_many_buff(&sort_param,buff,file_ptr,&maxbuffer,&file)) goto err; if (flush_io_cache(&file) || reinit_io_cache(&file,READ_CACHE,0L,0,0)) goto err; sort_param.res_length= sort_param.rec_length- (min_dupl_count ? sizeof(min_dupl_count) : 0); - if (merge_index(&sort_param, sort_buffer, file_ptr, maxbuffer, &file, outfile)) + if (without_last_merge) + { + file_ptrs.elements= maxbuffer+1; + return 0; + } + if (merge_index(&sort_param, buff, file_ptr, maxbuffer, &file, outfile)) goto err; - error=0; + error= 0; err: - my_free(sort_buffer); if (flush_io_cache(outfile)) - error=1; + error= 1; /* Setup io_cache for reading */ - save_pos=outfile->pos_in_file; + save_pos= outfile->pos_in_file; if (reinit_io_cache(outfile,READ_CACHE,0L,0,0)) - error=1; + error= 1; outfile->end_of_file=save_pos; return error; } +/* + Modify the TABLE element so that when one calls init_records() + the rows will be read in priority order. +*/ + +bool Unique::get(TABLE *table) +{ + bool rc= 1; + uchar *sort_buffer= NULL; + table->sort.found_records= elements+tree.elements_in_tree; + + if (my_b_tell(&file) == 0) + { + /* Whole tree is in memory; Don't use disk if you don't need to */ + if ((record_pointers=table->sort.record_pointers= (uchar*) + my_malloc(size * tree.elements_in_tree, MYF(MY_THREAD_SPECIFIC)))) + { + tree_walk_action action= min_dupl_count ? + (tree_walk_action) unique_intersect_write_to_ptrs : + (tree_walk_action) unique_write_to_ptrs; + filtered_out_elems= 0; + (void) tree_walk(&tree, action, + this, left_root_right); + table->sort.found_records-= filtered_out_elems; + return 0; + } + } + /* Not enough memory; Save the result to file && free memory used by tree */ + if (flush()) + return 1; + + ulong buff_sz= (max_in_memory_size / full_size + 1) * full_size; + if (!(sort_buffer= (uchar*) my_malloc(buff_sz, MYF(MY_THREAD_SPECIFIC)))) + return 1; + + if (merge(table, sort_buffer, FALSE)) + goto err; + rc= 0; + +err: + my_free(sort_buffer); + return rc; +} diff --git a/sql/unireg.cc b/sql/unireg.cc index 72dbbf973e0..e40dc02c21b 100644 --- a/sql/unireg.cc +++ b/sql/unireg.cc @@ -281,7 +281,7 @@ bool mysql_create_frm(THD *thd, const char *file_name, } key_buff_length= uint4korr(fileinfo+47); - keybuff=(uchar*) my_malloc(key_buff_length, MYF(0)); + keybuff=(uchar*) my_malloc(key_buff_length, MYF(MY_THREAD_SPECIFIC)); key_info_length= pack_keys(keybuff, keys, key_info, data_offset); /* @@ -533,7 +533,7 @@ static uchar *pack_screens(List<Create_field> &create_fields, while ((field=it++)) length+=(uint) strlen(field->field_name)+1+TE_INFO_LENGTH+cols/2; - if (!(info=(uchar*) my_malloc(length,MYF(MY_WME)))) + if (!(info=(uchar*) my_malloc(length,MYF(MY_WME | MY_THREAD_SPECIFIC)))) DBUG_RETURN(0); start_screen=0; @@ -1106,7 +1106,9 @@ static bool make_empty_rec(THD *thd, File file,enum legacy_db_type table_type, bzero((char*) &share, sizeof(share)); table.s= &share; - if (!(buff=(uchar*) my_malloc((size_t) reclength,MYF(MY_WME | MY_ZEROFILL)))) + if (!(buff=(uchar*) my_malloc((size_t) reclength, + MYF(MY_WME | MY_ZEROFILL | + MY_THREAD_SPECIFIC)))) { DBUG_RETURN(1); } |