diff options
author | Bjorn Munch <Bjorn.Munch@sun.com> | 2009-09-02 18:58:17 +0200 |
---|---|---|
committer | Bjorn Munch <Bjorn.Munch@sun.com> | 2009-09-02 18:58:17 +0200 |
commit | a829604260c4a485e74f2d30bbbf5f9619837b70 (patch) | |
tree | 5558803a46329d136748a32e38c66c6ff19b5b09 /sql | |
parent | fe3ea31d9310e14ffce99d23a9b5979864e96fe2 (diff) | |
parent | 8a2454f8e9fce648272577fcf8006ae6e6806cf9 (diff) | |
download | mariadb-git-a829604260c4a485e74f2d30bbbf5f9619837b70.tar.gz |
first merge from main
Diffstat (limited to 'sql')
92 files changed, 2892 insertions, 1498 deletions
diff --git a/sql/CMakeLists.txt b/sql/CMakeLists.txt index cfd049e1864..6f162f4d84d 100755 --- a/sql/CMakeLists.txt +++ b/sql/CMakeLists.txt @@ -39,7 +39,8 @@ SET_SOURCE_FILES_PROPERTIES(${CMAKE_SOURCE_DIR}/sql/sql_yacc.h ADD_DEFINITIONS(-DMYSQL_SERVER -D_CONSOLE -DHAVE_DLOPEN -DHAVE_EVENT_SCHEDULER) -ADD_EXECUTABLE(mysqld + +SET (SQL_SOURCE ../sql-common/client.c derror.cc des_key_file.cc discover.cc ../libmysql/errmsg.c field.cc field_conv.cc filesort.cc gstream.cc @@ -82,54 +83,45 @@ ADD_EXECUTABLE(mysqld ${PROJECT_SOURCE_DIR}/include/mysql_version.h ${PROJECT_SOURCE_DIR}/sql/sql_builtin.cc ${PROJECT_SOURCE_DIR}/sql/lex_hash.h) +ADD_LIBRARY(sql ${SQL_SOURCE}) -TARGET_LINK_LIBRARIES(mysqld - heap myisam myisammrg mysys yassl zlib debug dbug yassl - taocrypt strings vio regex wsock32 ws2_32) +IF (NOT EXISTS cmake_dummy.cc) + FILE (WRITE cmake_dummy.cc "") +ENDIF (NOT EXISTS cmake_dummy.cc) +ADD_EXECUTABLE(mysqld cmake_dummy.cc) SET_TARGET_PROPERTIES(mysqld PROPERTIES OUTPUT_NAME mysqld${MYSQLD_EXE_SUFFIX}) +SET_TARGET_PROPERTIES(mysqld PROPERTIES ENABLE_EXPORTS TRUE) + +SET (MYSQLD_CORE_LIBS mysys zlib dbug strings yassl taocrypt vio regex sql) +TARGET_LINK_LIBRARIES(mysqld ${MYSQLD_CORE_LIBS} ${MYSQLD_STATIC_ENGINE_LIBS}) +TARGET_LINK_LIBRARIES(mysqld ws2_32.lib) + -IF(cmake_version EQUAL 20406) -# Work around for 2.4.6 bug, OUTPUT_NAME will not set the right .PDB -# file name. Note that COMPILE_FLAGS set some temporary pdb during build, -# LINK_FLAGS sets the real one. -SET_TARGET_PROPERTIES(mysqld PROPERTIES - COMPILE_FLAGS "/Fd${CMAKE_CFG_INTDIR}/mysqld${MYSQLD_EXE_SUFFIX}.pdb" - LINK_FLAGS "/PDB:${CMAKE_CFG_INTDIR}/mysqld${MYSQLD_EXE_SUFFIX}.pdb") -ENDIF(cmake_version EQUAL 20406) - -IF(EMBED_MANIFESTS) - MYSQL_EMBED_MANIFEST("mysqld" "asInvoker") -ENDIF(EMBED_MANIFESTS) -IF(WITH_ARCHIVE_STORAGE_ENGINE) - TARGET_LINK_LIBRARIES(mysqld archive) -ENDIF(WITH_ARCHIVE_STORAGE_ENGINE) -IF(WITH_BLACKHOLE_STORAGE_ENGINE) - TARGET_LINK_LIBRARIES(mysqld blackhole) -ENDIF(WITH_BLACKHOLE_STORAGE_ENGINE) -IF(WITH_CSV_STORAGE_ENGINE) - TARGET_LINK_LIBRARIES(mysqld csv) -ENDIF(WITH_CSV_STORAGE_ENGINE) -IF(WITH_EXAMPLE_STORAGE_ENGINE) - TARGET_LINK_LIBRARIES(mysqld example) -ENDIF(WITH_EXAMPLE_STORAGE_ENGINE) -IF(WITH_FEDERATED_STORAGE_ENGINE) - TARGET_LINK_LIBRARIES(mysqld federated) -ENDIF(WITH_FEDERATED_STORAGE_ENGINE) -IF(WITH_INNOBASE_STORAGE_ENGINE) - TARGET_LINK_LIBRARIES(mysqld innobase) -ENDIF(WITH_INNOBASE_STORAGE_ENGINE) - -ADD_DEPENDENCIES(mysqld GenError) - -# NOTE CMake 2.4.6 creates strange dependencies between files in OUTPUT, -# so for now we only list one if more than one +IF(MSVC AND NOT WITHOUT_DYNAMIC_PLUGINS) + # Set module definition file. Also use non-incremental linker, + # incremental appears to crash from time to time,if used with /DEF option + SET_TARGET_PROPERTIES(mysqld PROPERTIES LINK_FLAGS "/DEF:mysqld.def /INCREMENTAL:NO") + + FOREACH (CORELIB ${MYSQLD_CORE_LIBS}) + GET_TARGET_PROPERTY(LOC ${CORELIB} LOCATION) + FILE(TO_NATIVE_PATH ${LOC} LOC) + SET (LIB_LOCATIONS ${LIB_LOCATIONS} ${LOC}) + ENDFOREACH (CORELIB ${MYSQLD_CORE_LIBS}) + + ADD_CUSTOM_COMMAND(TARGET mysqld PRE_LINK + COMMAND cscript ARGS //nologo ${PROJECT_SOURCE_DIR}/win/create_def_file.js + ${PLATFORM} ${LIB_LOCATIONS} > mysqld.def + WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/sql) +ENDIF(MSVC AND NOT WITHOUT_DYNAMIC_PLUGINS) + +ADD_DEPENDENCIES(sql GenError) # Sql Parser custom command ADD_CUSTOM_COMMAND( OUTPUT ${PROJECT_SOURCE_DIR}/sql/sql_yacc.h -# ${PROJECT_SOURCE_DIR}/sql/sql_yacc.cc - COMMAND bison.exe ARGS -y -p MYSQL --defines=sql_yacc.h + ${PROJECT_SOURCE_DIR}/sql/sql_yacc.cc + COMMAND bison ARGS -y -p MYSQL --defines=sql_yacc.h --output=sql_yacc.cc sql_yacc.yy DEPENDS ${PROJECT_SOURCE_DIR}/sql/sql_yacc.yy) @@ -146,16 +138,16 @@ ADD_CUSTOM_COMMAND( ADD_CUSTOM_TARGET( GenServerSource ALL DEPENDS ${PROJECT_SOURCE_DIR}/sql/sql_yacc.h -# ${PROJECT_SOURCE_DIR}/sql/sql_yacc.cc + ${PROJECT_SOURCE_DIR}/sql/sql_yacc.cc ${PROJECT_SOURCE_DIR}/sql/message.h -# ${PROJECT_SOURCE_DIR}/sql/message.rc + ${PROJECT_SOURCE_DIR}/sql/message.rc ${PROJECT_SOURCE_DIR}/sql/lex_hash.h) ADD_DEPENDENCIES(mysqld GenServerSource) # Remove the auto-generated files as part of 'Clean Solution' SET_DIRECTORY_PROPERTIES(PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES - "lex_hash.h;sql_yacc.h;sql_yacc.cc") + "lex_hash.h;sql_yacc.h;sql_yacc.cc;mysqld.def") ADD_LIBRARY(udf_example MODULE udf_example.c udf_example.def) ADD_DEPENDENCIES(udf_example strings GenError) diff --git a/sql/Makefile.am b/sql/Makefile.am index e477a6123ec..2a12de2eaf6 100644 --- a/sql/Makefile.am +++ b/sql/Makefile.am @@ -137,7 +137,7 @@ mysql_tzinfo_to_sql_CXXFLAGS= -DTZINFO2SQL DEFS = -DMYSQL_SERVER \ -DDEFAULT_MYSQL_HOME="\"$(MYSQLBASEdir)\"" \ - -DDATADIR="\"$(MYSQLDATAdir)\"" \ + -DMYSQL_DATADIR="\"$(MYSQLDATAdir)\"" \ -DSHAREDIR="\"$(MYSQLSHAREdir)\"" \ -DPLUGINDIR="\"$(pkgplugindir)\"" \ -DHAVE_EVENT_SCHEDULER \ diff --git a/sql/event_data_objects.cc b/sql/event_data_objects.cc index 0eba357b632..dba32cac6b2 100644 --- a/sql/event_data_objects.cc +++ b/sql/event_data_objects.cc @@ -1430,13 +1430,7 @@ Event_job_data::execute(THD *thd, bool drop) thd->variables.sql_mode= sql_mode; thd->variables.time_zone= time_zone; - /* - Peculiar initialization order is a crutch to avoid races in SHOW - PROCESSLIST which reads thd->{query/query_length} without a mutex. - */ - thd->query_length= 0; - thd->query= sp_sql.c_ptr_safe(); - thd->query_length= sp_sql.length(); + thd->set_query(sp_sql.c_ptr_safe(), sp_sql.length()); { Parser_state parser_state(thd, thd->query, thd->query_length); @@ -1497,13 +1491,8 @@ end_no_lex_start: else { ulong saved_master_access; - /* - Peculiar initialization order is a crutch to avoid races in SHOW - PROCESSLIST which reads thd->{query/query_length} without a mutex. - */ - thd->query_length= 0; - thd->query= sp_sql.c_ptr_safe(); - thd->query_length= sp_sql.length(); + + thd->set_query(sp_sql.c_ptr_safe(), sp_sql.length()); /* NOTE: even if we run in read-only mode, we should be able to lock @@ -1528,8 +1517,7 @@ end_no_lex_start: thd->end_statement(); thd->cleanup_after_query(); /* Avoid races with SHOW PROCESSLIST */ - thd->query_length= 0; - thd->query= NULL; + thd->set_query(NULL, 0); DBUG_PRINT("info", ("EXECUTED %s.%s ret: %d", dbname.str, name.str, ret)); diff --git a/sql/event_scheduler.cc b/sql/event_scheduler.cc index d9d010783e8..8c0025f9ed4 100644 --- a/sql/event_scheduler.cc +++ b/sql/event_scheduler.cc @@ -158,6 +158,7 @@ deinit_event_thread(THD *thd) thread_count--; thread_running--; delete thd; + pthread_cond_broadcast(&COND_thread_count); pthread_mutex_unlock(&LOCK_thread_count); } @@ -418,6 +419,7 @@ Event_scheduler::start() thread_count--; thread_running--; delete new_thd; + pthread_cond_broadcast(&COND_thread_count); pthread_mutex_unlock(&LOCK_thread_count); } end: @@ -550,6 +552,7 @@ error: thread_count--; thread_running--; delete new_thd; + pthread_cond_broadcast(&COND_thread_count); pthread_mutex_unlock(&LOCK_thread_count); } delete event_name; @@ -625,13 +628,13 @@ Event_scheduler::stop() DBUG_PRINT("info", ("Scheduler thread has id %lu", scheduler_thd->thread_id)); /* Lock from delete */ - pthread_mutex_lock(&scheduler_thd->LOCK_delete); + pthread_mutex_lock(&scheduler_thd->LOCK_thd_data); /* This will wake up the thread if it waits on Queue's conditional */ sql_print_information("Event Scheduler: Killing the scheduler thread, " "thread id %lu", scheduler_thd->thread_id); scheduler_thd->awake(THD::KILL_CONNECTION); - pthread_mutex_unlock(&scheduler_thd->LOCK_delete); + pthread_mutex_unlock(&scheduler_thd->LOCK_thd_data); /* thd could be 0x0, when shutting down */ sql_print_information("Event Scheduler: " diff --git a/sql/events.cc b/sql/events.cc index ea935e67bd3..10edfff2402 100644 --- a/sql/events.cc +++ b/sql/events.cc @@ -852,22 +852,23 @@ Events::fill_schema_events(THD *thd, TABLE_LIST *tables, COND * /* cond */) } -/* - Inits the scheduler's structures. +/** + Initializes the scheduler's structures. - SYNOPSIS - Events::init() + @param opt_noacl_or_bootstrap + TRUE if there is --skip-grant-tables or --bootstrap + option. In that case we disable the event scheduler. - NOTES - This function is not synchronized. + @note This function is not synchronized. - RETURN VALUE - FALSE OK - TRUE Error in case the scheduler can't start + @retval FALSE Perhaps there was an error, and the event scheduler + is disabled. But the error is not fatal and the + server start up can continue. + @retval TRUE Fatal error. Startup must terminate (call unireg_abort()). */ bool -Events::init(my_bool opt_noacl) +Events::init(my_bool opt_noacl_or_bootstrap) { THD *thd; @@ -875,11 +876,6 @@ Events::init(my_bool opt_noacl) DBUG_ENTER("Events::init"); - /* Disable the scheduler if running with --skip-grant-tables */ - if (opt_noacl) - opt_event_scheduler= EVENTS_DISABLED; - - /* We need a temporary THD during boot */ if (!(thd= new THD())) { @@ -908,23 +904,30 @@ Events::init(my_bool opt_noacl) /* Since we allow event DDL even if the scheduler is disabled, check the system tables, as we might need them. + + If run with --skip-grant-tables or --bootstrap, don't try to do the + check of system tables and don't complain: in these modes the tables + are most likely not there and we're going to disable the event + scheduler anyway. */ - if (Event_db_repository::check_system_tables(thd)) + if (opt_noacl_or_bootstrap || Event_db_repository::check_system_tables(thd)) { - sql_print_error("Event Scheduler: An error occurred when initializing " - "system tables.%s", - opt_event_scheduler == EVENTS_DISABLED ? - "" : " Disabling the Event Scheduler."); + if (! opt_noacl_or_bootstrap) + { + sql_print_error("Event Scheduler: An error occurred when initializing " + "system tables. Disabling the Event Scheduler."); + check_system_tables_error= TRUE; + } /* Disable the scheduler since the system tables are not up to date */ opt_event_scheduler= EVENTS_DISABLED; - check_system_tables_error= TRUE; goto end; } /* Was disabled explicitly from the command line, or because we're running - with --skip-grant-tables, or because we have no system tables. + with --skip-grant-tables, or --bootstrap, or because we have no system + tables. */ if (opt_event_scheduler == Events::EVENTS_DISABLED) goto end; @@ -941,7 +944,7 @@ Events::init(my_bool opt_noacl) } if (event_queue->init_queue(thd) || load_events_from_db(thd) || - opt_event_scheduler == EVENTS_ON && scheduler->start()) + (opt_event_scheduler == EVENTS_ON && scheduler->start())) { sql_print_error("Event Scheduler: Error while loading from disk."); res= TRUE; /* fatal error: request unireg_abort */ diff --git a/sql/field.cc b/sql/field.cc index 98b3b91fcbd..3bfb54fbd15 100644 --- a/sql/field.cc +++ b/sql/field.cc @@ -1165,7 +1165,7 @@ bool Field_num::get_int(CHARSET_INFO *cs, const char *from, uint len, if (unsigned_flag) { - if (((ulonglong) *rnd > unsigned_max) && (*rnd= (longlong) unsigned_max) || + if ((((ulonglong) *rnd > unsigned_max) && (*rnd= (longlong) unsigned_max)) || error == MY_ERRNO_ERANGE) { goto out_of_range; @@ -1350,7 +1350,7 @@ void Field::copy_from_tmp(int row_offset) if (null_ptr) { *null_ptr= (uchar) ((null_ptr[0] & (uchar) ~(uint) null_bit) | - null_ptr[row_offset] & (uchar) null_bit); + (null_ptr[row_offset] & (uchar) null_bit)); } } @@ -4081,8 +4081,8 @@ int Field_float::store(const char *from,uint len,CHARSET_INFO *cs) int error; char *end; double nr= my_strntod(cs,(char*) from,len,&end,&error); - if (error || (!len || (uint) (end-from) != len && - table->in_use->count_cuted_fields)) + if (error || (!len || ((uint) (end-from) != len && + table->in_use->count_cuted_fields))) { set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, (error ? ER_WARN_DATA_OUT_OF_RANGE : WARN_DATA_TRUNCATED), 1); @@ -4343,8 +4343,8 @@ int Field_double::store(const char *from,uint len,CHARSET_INFO *cs) int error; char *end; double nr= my_strntod(cs,(char*) from, len, &end, &error); - if (error || (!len || (uint) (end-from) != len && - table->in_use->count_cuted_fields)) + if (error || (!len || ((uint) (end-from) != len && + table->in_use->count_cuted_fields))) { set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, (error ? ER_WARN_DATA_OUT_OF_RANGE : WARN_DATA_TRUNCATED), 1); @@ -4598,7 +4598,6 @@ bool Field_double::send_binary(Protocol *protocol) int Field_double::cmp(const uchar *a_ptr, const uchar *b_ptr) { - ASSERT_COLUMN_MARKED_FOR_READ; double a,b; #ifdef WORDS_BIGENDIAN if (table->s->db_low_byte_first) @@ -5196,7 +5195,7 @@ int Field_time::store(longlong nr, bool unsigned_val) MYSQL_TIMESTAMP_TIME, 1); error= 1; } - else if (nr > (longlong) TIME_MAX_VALUE || nr < 0 && unsigned_val) + else if (nr > (longlong) TIME_MAX_VALUE || (nr < 0 && unsigned_val)) { tmp= TIME_MAX_VALUE; set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, @@ -5361,7 +5360,7 @@ int Field_year::store(const char *from, uint len,CHARSET_INFO *cs) int error; longlong nr= cs->cset->strntoull10rnd(cs, from, len, 0, &end, &error); - if (nr < 0 || nr >= 100 && nr <= 1900 || nr > 2155 || + if (nr < 0 || (nr >= 100 && nr <= 1900) || nr > 2155 || error == MY_ERRNO_ERANGE) { *ptr=0; @@ -5405,7 +5404,7 @@ int Field_year::store(double nr) int Field_year::store(longlong nr, bool unsigned_val) { ASSERT_COLUMN_MARKED_FOR_WRITE; - if (nr < 0 || nr >= 100 && nr <= 1900 || nr > 2155) + if (nr < 0 || (nr >= 100 && nr <= 1900) || nr > 2155) { *ptr= 0; set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1); @@ -6271,48 +6270,15 @@ check_string_copy_error(Field_str *field, const char *end, CHARSET_INFO *cs) { - const char *pos, *end_orig; - char tmp[64], *t; + const char *pos; + char tmp[32]; if (!(pos= well_formed_error_pos) && !(pos= cannot_convert_error_pos)) return FALSE; - end_orig= end; - set_if_smaller(end, pos + 6); + convert_to_printable(tmp, sizeof(tmp), pos, (end - pos), cs, 6); - for (t= tmp; pos < end; pos++) - { - /* - If the source string is ASCII compatible (mbminlen==1) - and the source character is in ASCII printable range (0x20..0x7F), - then display the character as is. - - Otherwise, if the source string is not ASCII compatible (e.g. UCS2), - or the source character is not in the printable range, - then print the character using HEX notation. - */ - if (((unsigned char) *pos) >= 0x20 && - ((unsigned char) *pos) <= 0x7F && - cs->mbminlen == 1) - { - *t++= *pos; - } - else - { - *t++= '\\'; - *t++= 'x'; - *t++= _dig_vec_upper[((unsigned char) *pos) >> 4]; - *t++= _dig_vec_upper[((unsigned char) *pos) & 15]; - } - } - if (end_orig > end) - { - *t++= '.'; - *t++= '.'; - *t++= '.'; - } - *t= '\0'; push_warning_printf(field->table->in_use, field->table->in_use->abort_on_warning ? MYSQL_ERROR::WARN_LEVEL_ERROR : @@ -6429,16 +6395,16 @@ int Field_str::store(double nr) /* Calculate the exponent from the 'e'-format conversion */ if (anr < 1.0 && anr > 0) { - for (exp= 0; anr < 1e-100; exp-= 100, anr*= 1e100); - for (; anr < 1e-10; exp-= 10, anr*= 1e10); - for (i= 1; anr < 1 / log_10[i]; exp--, i++); + for (exp= 0; anr < 1e-100; exp-= 100, anr*= 1e100) ; + for (; anr < 1e-10; exp-= 10, anr*= 1e10) ; + for (i= 1; anr < 1 / log_10[i]; exp--, i++) ; exp--; } else { - for (exp= 0; anr > 1e100; exp+= 100, anr/= 1e100); - for (; anr > 1e10; exp+= 10, anr/= 1e10); - for (i= 1; anr > log_10[i]; exp++, i++); + for (exp= 0; anr > 1e100; exp+= 100, anr/= 1e100) ; + for (; anr > 1e10; exp+= 10, anr/= 1e10) ; + for (i= 1; anr > log_10[i]; exp++, i++) ; } max_length= local_char_length - neg; @@ -8833,7 +8799,7 @@ bool Field_num::eq_def(Field *field) Field_num *from_num= (Field_num*) field; if (unsigned_flag != from_num->unsigned_flag || - zerofill && !from_num->zerofill && !zero_pack() || + (zerofill && !from_num->zerofill && !zero_pack()) || dec != from_num->dec) return 0; return 1; @@ -8974,7 +8940,7 @@ int Field_bit::store(const char *from, uint length, CHARSET_INFO *cs) ASSERT_COLUMN_MARKED_FOR_WRITE; int delta; - for (; length && !*from; from++, length--); // skip left 0's + for (; length && !*from; from++, length--) ; // skip left 0's delta= bytes_in_rec - length; if (delta < -1 || @@ -9306,7 +9272,7 @@ Field_bit::unpack(uchar *to, const uchar *from, uint param_data, and slave have the same sizes, then use the old unpack() method. */ if (param_data == 0 || - (from_bit_len == bit_len) && (from_len == bytes_in_rec)) + ((from_bit_len == bit_len) && (from_len == bytes_in_rec))) { if (bit_len > 0) { @@ -9385,7 +9351,7 @@ int Field_bit_as_char::store(const char *from, uint length, CHARSET_INFO *cs) int delta; uchar bits= (uchar) (field_length & 7); - for (; length && !*from; from++, length--); // skip left 0's + for (; length && !*from; from++, length--) ; // skip left 0's delta= bytes_in_rec - length; if (delta < 0 || diff --git a/sql/field_conv.cc b/sql/field_conv.cc index 11d0bb9cc82..3574534722e 100644 --- a/sql/field_conv.cc +++ b/sql/field_conv.cc @@ -99,7 +99,7 @@ static void do_field_to_null_str(Copy_field *copy) static void do_outer_field_to_null_str(Copy_field *copy) { if (*copy->null_row || - copy->from_null_ptr && (*copy->from_null_ptr & copy->from_bit)) + (copy->from_null_ptr && (*copy->from_null_ptr & copy->from_bit))) { bzero(copy->to_ptr,copy->from_length); copy->to_null_ptr[0]=1; // Always bit 1 @@ -212,7 +212,7 @@ static void do_copy_null(Copy_field *copy) static void do_outer_field_null(Copy_field *copy) { if (*copy->null_row || - copy->from_null_ptr && (*copy->from_null_ptr & copy->from_bit)) + (copy->from_null_ptr && (*copy->from_null_ptr & copy->from_bit))) { *copy->to_null_ptr|=copy->to_bit; copy->to_field->reset(); @@ -665,9 +665,9 @@ Copy_field::get_copy_func(Field *to,Field *from) */ if (to->real_type() != from->real_type() || !compatible_db_low_byte_first || - ((to->table->in_use->variables.sql_mode & + (((to->table->in_use->variables.sql_mode & (MODE_NO_ZERO_IN_DATE | MODE_NO_ZERO_DATE | MODE_INVALID_DATES)) && - to->type() == MYSQL_TYPE_DATE || + to->type() == MYSQL_TYPE_DATE) || to->type() == MYSQL_TYPE_DATETIME)) { if (from->real_type() == MYSQL_TYPE_ENUM || @@ -770,8 +770,8 @@ int field_conv(Field *to,Field *from) to->table->s->db_low_byte_first == from->table->s->db_low_byte_first && (!(to->table->in_use->variables.sql_mode & (MODE_NO_ZERO_IN_DATE | MODE_NO_ZERO_DATE | MODE_INVALID_DATES)) || - to->type() != MYSQL_TYPE_DATE && - to->type() != MYSQL_TYPE_DATETIME) && + (to->type() != MYSQL_TYPE_DATE && + to->type() != MYSQL_TYPE_DATETIME)) && (from->real_type() != MYSQL_TYPE_VARCHAR || ((Field_varstring*)from)->length_bytes == ((Field_varstring*)to)->length_bytes)) diff --git a/sql/gstream.cc b/sql/gstream.cc index 0c8011549f3..e2bb41b8541 100644 --- a/sql/gstream.cc +++ b/sql/gstream.cc @@ -75,7 +75,7 @@ bool Gis_read_stream::get_next_number(double *d) skip_space(); if ((m_cur >= m_limit) || - (*m_cur < '0' || *m_cur > '9') && *m_cur != '-' && *m_cur != '+') + ((*m_cur < '0' || *m_cur > '9') && *m_cur != '-' && *m_cur != '+')) { set_error_msg("Numeric constant expected"); return 1; diff --git a/sql/ha_ndbcluster.cc b/sql/ha_ndbcluster.cc index 3454f3558e8..ddb90427fdc 100644 --- a/sql/ha_ndbcluster.cc +++ b/sql/ha_ndbcluster.cc @@ -1247,7 +1247,7 @@ int ha_ndbcluster::add_index_handle(THD *thd, NDBDICT *dict, KEY *key_info, } if (idx_type == UNIQUE_ORDERED_INDEX || idx_type == UNIQUE_INDEX) { - char unique_index_name[FN_LEN]; + char unique_index_name[FN_LEN + 1]; static const char* unique_suffix= "$unique"; m_has_unique_index= TRUE; strxnmov(unique_index_name, FN_LEN, index_name, unique_suffix, NullS); @@ -5150,7 +5150,7 @@ int ha_ndbcluster::create(const char *name, uchar *data= NULL, *pack_data= NULL; bool create_from_engine= (create_info->table_options & HA_OPTION_CREATE_FROM_ENGINE); bool is_truncate= (thd->lex->sql_command == SQLCOM_TRUNCATE); - char tablespace[FN_LEN]; + char tablespace[FN_LEN + 1]; NdbDictionary::Table::SingleUserMode single_user_mode= NdbDictionary::Table::SingleUserModeLocked; DBUG_ENTER("ha_ndbcluster::create"); @@ -5613,7 +5613,7 @@ int ha_ndbcluster::create_index(const char *name, KEY *key_info, NDB_INDEX_TYPE idx_type, uint idx_no) { int error= 0; - char unique_name[FN_LEN]; + char unique_name[FN_LEN + 1]; static const char* unique_suffix= "$unique"; DBUG_ENTER("ha_ndbcluster::create_ordered_index"); DBUG_PRINT("info", ("Creating index %u: %s", idx_no, name)); @@ -6661,7 +6661,7 @@ int ndbcluster_discover(handlerton *hton, THD* thd, const char *db, size_t len; uchar* data= NULL; Ndb* ndb; - char key[FN_REFLEN]; + char key[FN_REFLEN + 1]; DBUG_ENTER("ndbcluster_discover"); DBUG_PRINT("enter", ("db: %s, name: %s", db, name)); @@ -6672,7 +6672,7 @@ int ndbcluster_discover(handlerton *hton, THD* thd, const char *db, ERR_RETURN(ndb->getNdbError()); } NDBDICT* dict= ndb->getDictionary(); - build_table_filename(key, sizeof(key), db, name, "", 0); + build_table_filename(key, sizeof(key) - 1, db, name, "", 0); /* ndb_share reference temporary */ NDB_SHARE *share= get_share(key, 0, FALSE); if (share) @@ -6837,9 +6837,9 @@ int ndbcluster_drop_database_impl(const char *path) drop_list.push_back(thd->strdup(elmt.name)); } // Drop any tables belonging to database - char full_path[FN_REFLEN]; + char full_path[FN_REFLEN + 1]; char *tmp= full_path + - build_table_filename(full_path, sizeof(full_path), dbname, "", "", 0); + build_table_filename(full_path, sizeof(full_path) - 1, dbname, "", "", 0); if (ndb->setDatabaseName(dbname)) { ERR_RETURN(ndb->getNdbError()); @@ -6908,7 +6908,7 @@ int ndb_create_table_from_engine(THD *thd, const char *db, int ndbcluster_find_all_files(THD *thd) { Ndb* ndb; - char key[FN_REFLEN]; + char key[FN_REFLEN + 1]; NDBDICT *dict; int unhandled, retries= 5, skipped; DBUG_ENTER("ndbcluster_find_all_files"); @@ -6966,7 +6966,7 @@ int ndbcluster_find_all_files(THD *thd) /* check if database exists */ char *end= key + - build_table_filename(key, sizeof(key), elmt.database, "", "", 0); + build_table_filename(key, sizeof(key) - 1, elmt.database, "", "", 0); if (my_access(key, F_OK)) { /* no such database defined, skip table */ @@ -7047,7 +7047,7 @@ int ndbcluster_find_files(handlerton *hton, THD *thd, { // extra bracket to avoid gcc 2.95.3 warning uint i; Ndb* ndb; - char name[FN_REFLEN]; + char name[FN_REFLEN + 1]; HASH ndb_tables, ok_tables; NDBDICT::List list; @@ -7117,7 +7117,8 @@ int ndbcluster_find_files(handlerton *hton, THD *thd, DBUG_PRINT("info", ("%s", file_name->str)); if (hash_search(&ndb_tables, (uchar*) file_name->str, file_name->length)) { - build_table_filename(name, sizeof(name), db, file_name->str, reg_ext, 0); + build_table_filename(name, sizeof(name) - 1, db, + file_name->str, reg_ext, 0); if (my_access(name, F_OK)) { pthread_mutex_lock(&LOCK_open); @@ -7139,7 +7140,8 @@ int ndbcluster_find_files(handlerton *hton, THD *thd, } // Check for .ndb file with this name - build_table_filename(name, sizeof(name), db, file_name->str, ha_ndb_ext, 0); + build_table_filename(name, sizeof(name) - 1, db, + file_name->str, ha_ndb_ext, 0); DBUG_PRINT("info", ("Check access for %s", name)); if (my_access(name, F_OK)) { @@ -7182,7 +7184,7 @@ int ndbcluster_find_files(handlerton *hton, THD *thd, /* setup logging to binlog for all discovered tables */ { char *end, *end1= name + - build_table_filename(name, sizeof(name), db, "", "", 0); + build_table_filename(name, sizeof(name) - 1, db, "", "", 0); for (i= 0; i < ok_tables.records; i++) { file_name_str= (char*)hash_element(&ok_tables, i); @@ -7204,7 +7206,8 @@ int ndbcluster_find_files(handlerton *hton, THD *thd, file_name_str= (char*) hash_element(&ndb_tables, i); if (!hash_search(&ok_tables, (uchar*) file_name_str, strlen(file_name_str))) { - build_table_filename(name, sizeof(name), db, file_name_str, reg_ext, 0); + build_table_filename(name, sizeof(name) - 1, + db, file_name_str, reg_ext, 0); if (my_access(name, F_OK)) { DBUG_PRINT("info", ("%s must be discovered", file_name_str)); @@ -7584,7 +7587,7 @@ void ndbcluster_print_error(int error, const NdbOperation *error_op) void ha_ndbcluster::set_dbname(const char *path_name, char *dbname) { char *end, *ptr, *tmp_name; - char tmp_buff[FN_REFLEN]; + char tmp_buff[FN_REFLEN + 1]; tmp_name= tmp_buff; /* Scan name from the end */ @@ -7610,7 +7613,7 @@ void ha_ndbcluster::set_dbname(const char *path_name, char *dbname) ptr++; } #endif - filename_to_tablename(tmp_name, dbname, FN_REFLEN); + filename_to_tablename(tmp_name, dbname, sizeof(tmp_buff) - 1); } /** @@ -7630,7 +7633,7 @@ void ha_ndbcluster::set_tabname(const char *path_name, char * tabname) { char *end, *ptr, *tmp_name; - char tmp_buff[FN_REFLEN]; + char tmp_buff[FN_REFLEN + 1]; tmp_name= tmp_buff; /* Scan name from the end */ @@ -7651,7 +7654,7 @@ ha_ndbcluster::set_tabname(const char *path_name, char * tabname) ptr++; } #endif - filename_to_tablename(tmp_name, tabname, FN_REFLEN); + filename_to_tablename(tmp_name, tabname, sizeof(tmp_buff) - 1); } /** @@ -7837,11 +7840,12 @@ uint8 ha_ndbcluster::table_cache_type() uint ndb_get_commitcount(THD *thd, char *dbname, char *tabname, Uint64 *commit_count) { - char name[FN_REFLEN]; + char name[FN_REFLEN + 1]; NDB_SHARE *share; DBUG_ENTER("ndb_get_commitcount"); - build_table_filename(name, sizeof(name), dbname, tabname, "", 0); + build_table_filename(name, sizeof(name) - 1, + dbname, tabname, "", 0); DBUG_PRINT("enter", ("name: %s", name)); pthread_mutex_lock(&ndbcluster_mutex); if (!(share=(NDB_SHARE*) hash_search(&ndbcluster_open_tables, @@ -9945,7 +9949,7 @@ bool ha_ndbcluster::check_if_incompatible_data(HA_CREATE_INFO *create_info, ai=1; } - char tablespace_name[FN_LEN]; + char tablespace_name[FN_LEN + 1]; if (get_tablespace_name(current_thd, tablespace_name, FN_LEN)) { if (create_info->tablespace) diff --git a/sql/ha_ndbcluster_binlog.cc b/sql/ha_ndbcluster_binlog.cc index baf86d739eb..d9a9738ce72 100644 --- a/sql/ha_ndbcluster_binlog.cc +++ b/sql/ha_ndbcluster_binlog.cc @@ -253,8 +253,7 @@ static void run_query(THD *thd, char *buf, char *end, const char* found_semicolon= NULL; bzero((char*) &thd->net, sizeof(NET)); - thd->query_length= end - buf; - thd->query= buf; + thd->set_query(buf, (uint) (end - buf)); thd->variables.pseudo_thread_id= thread_id; thd->transaction.stmt.modified_non_trans_table= FALSE; if (disable_binlog) @@ -297,8 +296,7 @@ static void run_query(THD *thd, char *buf, char *end, thd->main_da.reset_diagnostics_area(); thd->options= save_thd_options; - thd->query_length= save_thd_query_length; - thd->query= save_thd_query; + thd->set_query(save_thd_query, save_thd_query_length); thd->variables.pseudo_thread_id= save_thread_id; thd->status_var= save_thd_status_var; thd->transaction.all= save_thd_transaction_all; @@ -788,7 +786,7 @@ static int ndbcluster_create_ndb_apply_status_table(THD *thd) if (g_ndb_cluster_connection->get_no_ready() <= 0) DBUG_RETURN(0); - char buf[1024], *end; + char buf[1024 + 1], *end; if (ndb_extra_logging) sql_print_information("NDB: Creating " NDB_REP_DB "." NDB_APPLY_TABLE); @@ -798,7 +796,7 @@ static int ndbcluster_create_ndb_apply_status_table(THD *thd) if so, remove it since there is none in Ndb */ { - build_table_filename(buf, sizeof(buf), + build_table_filename(buf, sizeof(buf) - 1, NDB_REP_DB, NDB_APPLY_TABLE, reg_ext, 0); my_delete(buf, MYF(0)); } @@ -846,7 +844,7 @@ static int ndbcluster_create_schema_table(THD *thd) if (g_ndb_cluster_connection->get_no_ready() <= 0) DBUG_RETURN(0); - char buf[1024], *end; + char buf[1024 + 1], *end; if (ndb_extra_logging) sql_print_information("NDB: Creating " NDB_REP_DB "." NDB_SCHEMA_TABLE); @@ -856,7 +854,7 @@ static int ndbcluster_create_schema_table(THD *thd) if so, remove it since there is none in Ndb */ { - build_table_filename(buf, sizeof(buf), + build_table_filename(buf, sizeof(buf) - 1, NDB_REP_DB, NDB_SCHEMA_TABLE, reg_ext, 0); my_delete(buf, MYF(0)); } @@ -1321,8 +1319,8 @@ int ndbcluster_log_schema_op(THD *thd, NDB_SHARE *share, NDB_SCHEMA_OBJECT *ndb_schema_object; { - char key[FN_REFLEN]; - build_table_filename(key, sizeof(key), db, table_name, "", 0); + char key[FN_REFLEN + 1]; + build_table_filename(key, sizeof(key) - 1, db, table_name, "", 0); ndb_schema_object= ndb_get_schema_object(key, TRUE, FALSE); } @@ -1674,7 +1672,7 @@ ndb_handle_schema_change(THD *thd, Ndb *ndb, NdbEventOperation *pOp, if (is_remote_change && is_online_alter_table) { const char *tabname= table_share->table_name.str; - char key[FN_REFLEN]; + char key[FN_REFLEN + 1]; uchar *data= 0, *pack_data= 0; size_t length, pack_length; int error; @@ -1683,7 +1681,7 @@ ndb_handle_schema_change(THD *thd, Ndb *ndb, NdbEventOperation *pOp, DBUG_PRINT("info", ("Detected frm change of table %s.%s", dbname, tabname)); - build_table_filename(key, FN_LEN-1, dbname, tabname, NullS, 0); + build_table_filename(key, FN_LEN - 1, dbname, tabname, NullS, 0); /* If the there is no local table shadowing the altered table and it has an frm that is different than the one on disk then @@ -1855,9 +1853,11 @@ static void ndb_binlog_query(THD *thd, Cluster_schema *schema) else thd->server_id= schema->any_value; thd->db= schema->db; + int errcode = query_error_code(thd, thd->killed == THD::NOT_KILLED); thd->binlog_query(THD::STMT_QUERY_TYPE, schema->query, schema->query_length, FALSE, - schema->name[0] == 0 || thd->db[0] == 0); + schema->name[0] == 0 || thd->db[0] == 0, + errcode); thd->server_id= thd_server_id_save; thd->db= thd_db_save; } @@ -1924,8 +1924,8 @@ ndb_binlog_thread_handle_schema_event(THD *thd, Ndb *ndb, break; case SOT_TRUNCATE_TABLE: { - char key[FN_REFLEN]; - build_table_filename(key, sizeof(key), + char key[FN_REFLEN + 1]; + build_table_filename(key, sizeof(key) - 1, schema->db, schema->name, "", 0); /* ndb_share reference temporary, free below */ NDB_SHARE *share= get_share(key, 0, FALSE, FALSE); @@ -2171,8 +2171,8 @@ ndb_binlog_thread_handle_schema_event_post_epoch(THD *thd, int log_query= 0; { enum SCHEMA_OP_TYPE schema_type= (enum SCHEMA_OP_TYPE)schema->type; - char key[FN_REFLEN]; - build_table_filename(key, sizeof(key), schema->db, schema->name, "", 0); + char key[FN_REFLEN + 1]; + build_table_filename(key, sizeof(key) - 1, schema->db, schema->name, "", 0); if (schema_type == SOT_CLEAR_SLOCK) { pthread_mutex_lock(&ndbcluster_mutex); @@ -2506,8 +2506,8 @@ ndb_rep_event_name(String *event_name,const char *db, const char *tbl) bool ndbcluster_check_if_local_table(const char *dbname, const char *tabname) { - char key[FN_REFLEN]; - char ndb_file[FN_REFLEN]; + char key[FN_REFLEN + 1]; + char ndb_file[FN_REFLEN + 1]; DBUG_ENTER("ndbcluster_check_if_local_table"); build_table_filename(key, FN_LEN-1, dbname, tabname, reg_ext, 0); @@ -2532,9 +2532,9 @@ ndbcluster_check_if_local_tables_in_db(THD *thd, const char *dbname) DBUG_PRINT("info", ("Looking for files in directory %s", dbname)); LEX_STRING *tabname; List<LEX_STRING> files; - char path[FN_REFLEN]; + char path[FN_REFLEN + 1]; - build_table_filename(path, sizeof(path), dbname, "", "", 0); + build_table_filename(path, sizeof(path) - 1, dbname, "", "", 0); if (find_files(thd, &files, dbname, path, NullS, 0) != FIND_FILES_OK) { DBUG_PRINT("info", ("Failed to find files")); @@ -3753,7 +3753,6 @@ pthread_handler_t ndb_binlog_thread_func(void *arg) if (ndbcluster_terminating) { pthread_mutex_unlock(&LOCK_server_started); - pthread_mutex_lock(&LOCK_ndb_util_thread); goto err; } } diff --git a/sql/ha_partition.cc b/sql/ha_partition.cc index 67bc3156260..b345648260c 100644 --- a/sql/ha_partition.cc +++ b/sql/ha_partition.cc @@ -423,12 +423,9 @@ bool ha_partition::initialize_partition(MEM_ROOT *mem_root) int ha_partition::delete_table(const char *name) { - int error; DBUG_ENTER("ha_partition::delete_table"); - if ((error= del_ren_cre_table(name, NULL, NULL, NULL))) - DBUG_RETURN(error); - DBUG_RETURN(handler::delete_table(name)); + DBUG_RETURN(del_ren_cre_table(name, NULL, NULL, NULL)); } @@ -456,12 +453,9 @@ int ha_partition::delete_table(const char *name) int ha_partition::rename_table(const char *from, const char *to) { - int error; DBUG_ENTER("ha_partition::rename_table"); - if ((error= del_ren_cre_table(from, to, NULL, NULL))) - DBUG_RETURN(error); - DBUG_RETURN(handler::rename_table(from, to)); + DBUG_RETURN(del_ren_cre_table(from, to, NULL, NULL)); } @@ -1807,6 +1801,15 @@ uint ha_partition::del_ren_cre_table(const char *from, DBUG_PRINT("enter", ("from: (%s) to: (%s)", from, to)); name_buffer_ptr= m_name_buffer_ptr; file= m_file; + if (to == NULL && table_arg == NULL) + { + /* + Delete table, start by delete the .par file. If error, break, otherwise + delete as much as possible. + */ + if ((error= handler::delete_table(from))) + DBUG_RETURN(error); + } /* Since ha_partition has HA_FILE_BASED, it must alter underlying table names if they do not have HA_FILE_BASED and lower_case_table_names == 2. @@ -1828,6 +1831,8 @@ uint ha_partition::del_ren_cre_table(const char *from, create_partition_name(to_buff, to_path, name_buffer_ptr, NORMAL_PART_NAME, FALSE); error= (*file)->ha_rename_table(from_buff, to_buff); + if (error) + goto rename_error; } else if (table_arg == NULL) // delete branch error= (*file)->ha_delete_table(from_buff); @@ -1843,6 +1848,15 @@ uint ha_partition::del_ren_cre_table(const char *from, save_error= error; i++; } while (*(++file)); + if (to != NULL) + { + if ((error= handler::rename_table(from, to))) + { + /* Try to revert everything, ignore errors */ + (void) handler::rename_table(to, from); + goto rename_error; + } + } DBUG_RETURN(save_error); create_error: name_buffer_ptr= m_name_buffer_ptr; @@ -1850,7 +1864,21 @@ create_error: { create_partition_name(from_buff, from_path, name_buffer_ptr, NORMAL_PART_NAME, FALSE); - VOID((*file)->ha_delete_table((const char*) from_buff)); + (void) (*file)->ha_delete_table((const char*) from_buff); + name_buffer_ptr= strend(name_buffer_ptr) + 1; + } + DBUG_RETURN(error); +rename_error: + name_buffer_ptr= m_name_buffer_ptr; + for (abort_file= file, file= m_file; file < abort_file; file++) + { + /* Revert the rename, back from 'to' to the original 'from' */ + create_partition_name(from_buff, from_path, name_buffer_ptr, + NORMAL_PART_NAME, FALSE); + create_partition_name(to_buff, to_path, name_buffer_ptr, + NORMAL_PART_NAME, FALSE); + /* Ignore error here */ + (void) (*file)->ha_rename_table(to_buff, from_buff); name_buffer_ptr= strend(name_buffer_ptr) + 1; } DBUG_RETURN(error); @@ -3179,6 +3207,7 @@ int ha_partition::delete_row(const uchar *buf) int ha_partition::delete_all_rows() { int error; + bool truncate= FALSE; handler **file; THD *thd= ha_thd(); DBUG_ENTER("ha_partition::delete_all_rows"); @@ -3190,12 +3219,16 @@ int ha_partition::delete_all_rows() ha_data->next_auto_inc_val= 0; ha_data->auto_inc_initialized= FALSE; unlock_auto_increment(); + truncate= TRUE; } file= m_file; do { if ((error= (*file)->ha_delete_all_rows())) DBUG_RETURN(error); + /* Ignore the error */ + if (truncate) + (void) (*file)->ha_reset_auto_increment(0); } while (*(++file)); DBUG_RETURN(0); } @@ -3697,7 +3730,7 @@ int ha_partition::index_init(uint inx, bool sorted) */ if (m_lock_type == F_WRLCK) bitmap_union(table->read_set, &m_part_info->full_part_field_set); - else if (sorted) + if (sorted) { /* An ordered scan is requested. We must make sure all fields of the @@ -5381,6 +5414,13 @@ int ha_partition::extra(enum ha_extra_function operation) /* Currently only NDB use the *_CANNOT_BATCH */ break; } + /* + http://dev.mysql.com/doc/refman/5.1/en/partitioning-limitations.html + says we no longer support logging to partitioned tables, so we fail + here. + */ + case HA_EXTRA_MARK_AS_LOG_TABLE: + DBUG_RETURN(ER_UNSUPORTED_LOG_ENGINE); default: { /* Temporary crash to discover what is wrong */ diff --git a/sql/handler.cc b/sql/handler.cc index d22ee220dc1..e5c64452aaf 100644 --- a/sql/handler.cc +++ b/sql/handler.cc @@ -62,7 +62,9 @@ static const LEX_STRING sys_table_aliases[]= }; const char *ha_row_type[] = { - "", "FIXED", "DYNAMIC", "COMPRESSED", "REDUNDANT", "COMPACT", "PAGE", "?","?","?" + "", "FIXED", "DYNAMIC", "COMPRESSED", "REDUNDANT", "COMPACT", + /* Reserved to be "PAGE" in future versions */ "?", + "?","?","?" }; const char *tx_isolation_names[] = @@ -342,6 +344,7 @@ int ha_init_errors(void) SETMSG(HA_ERR_TABLE_READONLY, ER(ER_OPEN_AS_READONLY)); SETMSG(HA_ERR_AUTOINC_READ_FAILED, ER(ER_AUTOINC_READ_FAILED)); SETMSG(HA_ERR_AUTOINC_ERANGE, ER(ER_WARN_DATA_OUT_OF_RANGE)); + SETMSG(HA_ERR_TOO_MANY_CONCURRENT_TRXS, ER(ER_TOO_MANY_CONCURRENT_TRXS)); /* Register the error messages for use with my_error(). */ return my_error_register(errmsgs, HA_ERR_FIRST, HA_ERR_LAST); @@ -1073,6 +1076,13 @@ int ha_commit_trans(THD *thd, bool all) user, or an implicit commit issued by a DDL. */ THD_TRANS *trans= all ? &thd->transaction.all : &thd->transaction.stmt; + /* + "real" is a nick name for a transaction for which a commit will + make persistent changes. E.g. a 'stmt' transaction inside a 'all' + transation is not 'real': even though it's possible to commit it, + the changes are not durable as they might be rolled back if the + enclosing 'all' transaction is rolled back. + */ bool is_real_trans= all || thd->transaction.all.ha_list == 0; Ha_trx_info *ha_info= trans->ha_list; my_xid xid= thd->transaction.xid_state.xid.get_my_xid(); @@ -1184,16 +1194,9 @@ end: if (rw_trans) start_waiting_global_read_lock(thd); } - else if (all) - { - /* - A COMMIT of an empty transaction. There may be savepoints. - Destroy them. If the transaction is not empty - savepoints are cleared in ha_commit_one_phase() - or ha_rollback_trans(). - */ + /* Free resources and perform other cleanup even for 'empty' transactions. */ + else if (is_real_trans) thd->transaction.cleanup(); - } #endif /* USING_TRANSACTIONS */ DBUG_RETURN(error); } @@ -1206,6 +1209,13 @@ int ha_commit_one_phase(THD *thd, bool all) { int error=0; THD_TRANS *trans=all ? &thd->transaction.all : &thd->transaction.stmt; + /* + "real" is a nick name for a transaction for which a commit will + make persistent changes. E.g. a 'stmt' transaction inside a 'all' + transation is not 'real': even though it's possible to commit it, + the changes are not durable as they might be rolled back if the + enclosing 'all' transaction is rolled back. + */ bool is_real_trans=all || thd->transaction.all.ha_list == 0; Ha_trx_info *ha_info= trans->ha_list, *ha_info_next; DBUG_ENTER("ha_commit_one_phase"); @@ -1227,8 +1237,6 @@ int ha_commit_one_phase(THD *thd, bool all) } trans->ha_list= 0; trans->no_2pc=0; - if (is_real_trans) - thd->transaction.xid_state.xid.null(); if (all) { #ifdef HAVE_QUERY_CACHE @@ -1236,9 +1244,11 @@ int ha_commit_one_phase(THD *thd, bool all) query_cache.invalidate(thd->transaction.changed_tables); #endif thd->variables.tx_isolation=thd->session_tx_isolation; - thd->transaction.cleanup(); } } + /* Free resources and perform other cleanup even for 'empty' transactions. */ + if (is_real_trans) + thd->transaction.cleanup(); #endif /* USING_TRANSACTIONS */ DBUG_RETURN(error); } @@ -1249,6 +1259,13 @@ int ha_rollback_trans(THD *thd, bool all) int error=0; THD_TRANS *trans=all ? &thd->transaction.all : &thd->transaction.stmt; Ha_trx_info *ha_info= trans->ha_list, *ha_info_next; + /* + "real" is a nick name for a transaction for which a commit will + make persistent changes. E.g. a 'stmt' transaction inside a 'all' + transation is not 'real': even though it's possible to commit it, + the changes are not durable as they might be rolled back if the + enclosing 'all' transaction is rolled back. + */ bool is_real_trans=all || thd->transaction.all.ha_list == 0; DBUG_ENTER("ha_rollback_trans"); @@ -1294,18 +1311,13 @@ int ha_rollback_trans(THD *thd, bool all) } trans->ha_list= 0; trans->no_2pc=0; - if (is_real_trans) - { - if (thd->transaction_rollback_request) - thd->transaction.xid_state.rm_error= thd->main_da.sql_errno(); - else - thd->transaction.xid_state.xid.null(); - } + if (is_real_trans && thd->transaction_rollback_request) + thd->transaction.xid_state.rm_error= thd->main_da.sql_errno(); if (all) thd->variables.tx_isolation=thd->session_tx_isolation; } /* Always cleanup. Even if there nht==0. There may be savepoints. */ - if (all) + if (is_real_trans) thd->transaction.cleanup(); #endif /* USING_TRANSACTIONS */ if (all) @@ -2283,8 +2295,8 @@ int handler::update_auto_increment() DBUG_ASSERT(next_insert_id >= auto_inc_interval_for_cur_row.minimum()); if ((nr= table->next_number_field->val_int()) != 0 || - table->auto_increment_field_not_null && - thd->variables.sql_mode & MODE_NO_AUTO_VALUE_ON_ZERO) + (table->auto_increment_field_not_null && + thd->variables.sql_mode & MODE_NO_AUTO_VALUE_ON_ZERO)) { /* Update next_insert_id if we had already generated a value in this @@ -2738,6 +2750,9 @@ void handler::print_error(int error, myf errflag) case HA_ERR_AUTOINC_ERANGE: textno= ER_WARN_DATA_OUT_OF_RANGE; break; + case HA_ERR_TOO_MANY_CONCURRENT_TRXS: + textno= ER_TOO_MANY_CONCURRENT_TRXS; + break; default: { /* The error was "unknown" to this function. @@ -2964,6 +2979,7 @@ uint handler::get_dup_key(int error) */ int handler::delete_table(const char *name) { + int saved_error= 0; int error= 0; int enoent_or_zero= ENOENT; // Error if no file was deleted char buff[FN_REFLEN]; @@ -2973,21 +2989,31 @@ int handler::delete_table(const char *name) fn_format(buff, name, "", *ext, MY_UNPACK_FILENAME|MY_APPEND_EXT); if (my_delete_with_symlink(buff, MYF(0))) { - if ((error= my_errno) != ENOENT) - break; + if (my_errno != ENOENT) + { + /* + If error on the first existing file, return the error. + Otherwise delete as much as possible. + */ + if (enoent_or_zero) + return my_errno; + saved_error= my_errno; + } } else enoent_or_zero= 0; // No error for ENOENT error= enoent_or_zero; } - return error; + return saved_error ? saved_error : error; } int handler::rename_table(const char * from, const char * to) { int error= 0; - for (const char **ext= bas_ext(); *ext ; ext++) + const char **ext, **start_ext; + start_ext= bas_ext(); + for (ext= start_ext; *ext ; ext++) { if (rename_file_ext(from, to, *ext)) { @@ -2996,6 +3022,12 @@ int handler::rename_table(const char * from, const char * to) error= 0; } } + if (error) + { + /* Try to revert the rename. Ignore errors. */ + for (; ext >= start_ext; ext--) + rename_file_ext(to, from, *ext); + } return error; } @@ -3574,7 +3606,7 @@ int ha_create_table_from_engine(THD* thd, const char *db, const char *name) int error; uchar *frmblob; size_t frmlen; - char path[FN_REFLEN]; + char path[FN_REFLEN + 1]; HA_CREATE_INFO create_info; TABLE table; TABLE_SHARE share; @@ -3593,7 +3625,7 @@ int ha_create_table_from_engine(THD* thd, const char *db, const char *name) frmblob and frmlen are set, write the frm to disk */ - build_table_filename(path, FN_REFLEN-1, db, name, "", 0); + build_table_filename(path, sizeof(path) - 1, db, name, "", 0); // Save the frm file error= writefrm(path, frmblob, frmlen); my_free(frmblob, MYF(0)); @@ -4770,7 +4802,7 @@ fl_log_iterator_buffer_init(struct handler_iterator *iterator) if ((ptr= (uchar*)my_malloc(ALIGN_SIZE(sizeof(fl_buff)) + ((ALIGN_SIZE(sizeof(LEX_STRING)) + sizeof(enum log_status) + - + FN_REFLEN) * + + FN_REFLEN + 1) * (uint) dirp->number_off_files), MYF(0))) == 0) { @@ -4798,7 +4830,7 @@ fl_log_iterator_buffer_init(struct handler_iterator *iterator) name_ptr= strxnmov(buff->names[buff->entries].str= name_ptr, FN_REFLEN, fl_dir, file->name, NullS); buff->names[buff->entries].length= (name_ptr - - buff->names[buff->entries].str) - 1; + buff->names[buff->entries].str); buff->statuses[buff->entries]= st; buff->entries++; } diff --git a/sql/handler.h b/sql/handler.h index 5c7cfa4d58b..5d27d3eff6d 100644 --- a/sql/handler.h +++ b/sql/handler.h @@ -280,7 +280,9 @@ enum legacy_db_type enum row_type { ROW_TYPE_NOT_USED=-1, ROW_TYPE_DEFAULT, ROW_TYPE_FIXED, ROW_TYPE_DYNAMIC, ROW_TYPE_COMPRESSED, - ROW_TYPE_REDUNDANT, ROW_TYPE_COMPACT, ROW_TYPE_PAGE }; + ROW_TYPE_REDUNDANT, ROW_TYPE_COMPACT, + /** Unused. Reserved for future versions. */ + ROW_TYPE_PAGE }; enum enum_binlog_func { BFN_RESET_LOGS= 1, @@ -323,7 +325,9 @@ enum enum_binlog_command { #define HA_CREATE_USED_PASSWORD (1L << 17) #define HA_CREATE_USED_CONNECTION (1L << 18) #define HA_CREATE_USED_KEY_BLOCK_SIZE (1L << 19) +/** Unused. Reserved for future versions. */ #define HA_CREATE_USED_TRANSACTIONAL (1L << 20) +/** Unused. Reserved for future versions. */ #define HA_CREATE_USED_PAGE_CHECKSUM (1L << 21) typedef ulonglong my_xid; // this line is the same as in log_event.h @@ -914,13 +918,14 @@ typedef struct st_ha_create_information uint options; /* OR of HA_CREATE_ options */ uint merge_insert_method; uint extra_size; /* length of extra data segment */ - /* 0 not used, 1 if not transactional, 2 if transactional */ + /** Transactional or not. Unused; reserved for future versions. */ enum ha_choice transactional; bool table_existed; /* 1 in create if table existed */ bool frm_only; /* 1 if no ha_create_table() */ bool varchar; /* 1 if table has a VARCHAR */ enum ha_storage_media storage_media; /* DEFAULT, DISK or MEMORY */ - enum ha_choice page_checksum; /* If we have page_checksums */ + /** Per-page checksums or not. Unused; reserved for future versions. */ + enum ha_choice page_checksum; } HA_CREATE_INFO; @@ -1932,8 +1937,8 @@ private: /* Some extern variables used with handlers */ extern const char *ha_row_type[]; -extern const char *tx_isolation_names[]; -extern const char *binlog_format_names[]; +extern MYSQL_PLUGIN_IMPORT const char *tx_isolation_names[]; +extern MYSQL_PLUGIN_IMPORT const char *binlog_format_names[]; extern TYPELIB tx_isolation_typelib; extern TYPELIB myisam_stats_method_typelib; extern ulong total_ha, total_ha_2pc; diff --git a/sql/item.cc b/sql/item.cc index 768af47d660..2640b74851b 100644 --- a/sql/item.cc +++ b/sql/item.cc @@ -417,6 +417,7 @@ Item::Item(THD *thd, Item *item): name(item->name), orig_name(item->orig_name), max_length(item->max_length), + name_length(item->name_length), marker(item->marker), decimals(item->decimals), maybe_null(item->maybe_null), @@ -424,7 +425,9 @@ Item::Item(THD *thd, Item *item): unsigned_flag(item->unsigned_flag), with_sum_func(item->with_sum_func), fixed(item->fixed), + is_autogenerated_name(item->is_autogenerated_name), collation(item->collation), + with_subselect(item->with_subselect), cmp_context(item->cmp_context) { next= thd->free_list; // Put in free list @@ -596,6 +599,7 @@ bool Item_ident::remove_dependence_processor(uchar * arg) DBUG_ENTER("Item_ident::remove_dependence_processor"); if (depended_from == (st_select_lex *) arg) depended_from= 0; + context= &((st_select_lex *) arg)->context; DBUG_RETURN(0); } @@ -1504,10 +1508,6 @@ bool DTCollation::aggregate(DTCollation &dt, uint flags) { set(dt); } - else - { - // Do nothing - } } else if ((flags & MY_COLL_ALLOW_SUPERSET_CONV) && left_is_superset(this, &dt)) @@ -2261,8 +2261,10 @@ Item_decimal::Item_decimal(const char *str_arg, uint length, name= (char*) str_arg; decimals= (uint8) decimal_value.frac; fixed= 1; - max_length= my_decimal_precision_to_length(decimal_value.intg + decimals, - decimals, unsigned_flag); + max_length= my_decimal_precision_to_length_no_truncation(decimal_value.intg + + decimals, + decimals, + unsigned_flag); } Item_decimal::Item_decimal(longlong val, bool unsig) @@ -2270,8 +2272,10 @@ Item_decimal::Item_decimal(longlong val, bool unsig) int2my_decimal(E_DEC_FATAL_ERROR, val, unsig, &decimal_value); decimals= (uint8) decimal_value.frac; fixed= 1; - max_length= my_decimal_precision_to_length(decimal_value.intg + decimals, - decimals, unsigned_flag); + max_length= my_decimal_precision_to_length_no_truncation(decimal_value.intg + + decimals, + decimals, + unsigned_flag); } @@ -2280,8 +2284,10 @@ Item_decimal::Item_decimal(double val, int precision, int scale) double2my_decimal(E_DEC_FATAL_ERROR, val, &decimal_value); decimals= (uint8) decimal_value.frac; fixed= 1; - max_length= my_decimal_precision_to_length(decimal_value.intg + decimals, - decimals, unsigned_flag); + max_length= my_decimal_precision_to_length_no_truncation(decimal_value.intg + + decimals, + decimals, + unsigned_flag); } @@ -2301,8 +2307,10 @@ Item_decimal::Item_decimal(my_decimal *value_par) my_decimal2decimal(value_par, &decimal_value); decimals= (uint8) decimal_value.frac; fixed= 1; - max_length= my_decimal_precision_to_length(decimal_value.intg + decimals, - decimals, unsigned_flag); + max_length= my_decimal_precision_to_length_no_truncation(decimal_value.intg + + decimals, + decimals, + unsigned_flag); } @@ -2312,8 +2320,8 @@ Item_decimal::Item_decimal(const uchar *bin, int precision, int scale) &decimal_value, precision, scale); decimals= (uint8) decimal_value.frac; fixed= 1; - max_length= my_decimal_precision_to_length(precision, decimals, - unsigned_flag); + max_length= my_decimal_precision_to_length_no_truncation(precision, decimals, + unsigned_flag); } @@ -2368,8 +2376,10 @@ void Item_decimal::set_decimal_value(my_decimal *value_par) my_decimal2decimal(value_par, &decimal_value); decimals= (uint8) decimal_value.frac; unsigned_flag= !decimal_value.sign(); - max_length= my_decimal_precision_to_length(decimal_value.intg + decimals, - decimals, unsigned_flag); + max_length= my_decimal_precision_to_length_no_truncation(decimal_value.intg + + decimals, + decimals, + unsigned_flag); } @@ -2641,8 +2651,9 @@ void Item_param::set_decimal(const char *str, ulong length) str2my_decimal(E_DEC_FATAL_ERROR, str, &decimal_value, &end); state= DECIMAL_VALUE; decimals= decimal_value.frac; - max_length= my_decimal_precision_to_length(decimal_value.precision(), - decimals, unsigned_flag); + max_length= + my_decimal_precision_to_length_no_truncation(decimal_value.precision(), + decimals, unsigned_flag); maybe_null= 0; DBUG_VOID_RETURN; } @@ -2772,8 +2783,8 @@ bool Item_param::set_from_user_var(THD *thd, const user_var_entry *entry) CHARSET_INFO *tocs= thd->variables.collation_connection; uint32 dummy_offset; - value.cs_info.character_set_of_placeholder= - value.cs_info.character_set_client= fromcs; + value.cs_info.character_set_of_placeholder= fromcs; + value.cs_info.character_set_client= thd->variables.character_set_client; /* Setup source and destination character sets so that they are different only if conversion is necessary: this will @@ -2798,8 +2809,9 @@ bool Item_param::set_from_user_var(THD *thd, const user_var_entry *entry) my_decimal2decimal(ent_value, &decimal_value); state= DECIMAL_VALUE; decimals= ent_value->frac; - max_length= my_decimal_precision_to_length(ent_value->precision(), - decimals, unsigned_flag); + max_length= + my_decimal_precision_to_length_no_truncation(ent_value->precision(), + decimals, unsigned_flag); item_type= Item::DECIMAL_ITEM; break; } @@ -4407,16 +4419,22 @@ mark_non_agg_field: Fields from outer selects added to the aggregate function outer_fields list as its unknown at the moment whether it's aggregated or not. + We're using either the select lex of the cached table (if present) + or the field's resolution context. context->select_lex is + safe for use because it's either the SELECT we want to use + (the current level) or a stub added by non-SELECT queries. */ + SELECT_LEX *select_lex= cached_table ? + cached_table->select_lex : context->select_lex; if (!thd->lex->in_sum_func) - cached_table->select_lex->full_group_by_flag|= NON_AGG_FIELD_USED; + select_lex->full_group_by_flag|= NON_AGG_FIELD_USED; else { if (outer_fixed) thd->lex->in_sum_func->outer_fields.push_back(this); else if (thd->lex->in_sum_func->nest_level != thd->lex->current_select->nest_level) - cached_table->select_lex->full_group_by_flag|= NON_AGG_FIELD_USED; + select_lex->full_group_by_flag|= NON_AGG_FIELD_USED; } } return FALSE; @@ -7291,8 +7309,9 @@ bool Item_type_holder::join_types(THD *thd, Item *item) int item_prec = max(prev_decimal_int_part, item_int_part) + decimals; int precision= min(item_prec, DECIMAL_MAX_PRECISION); unsigned_flag&= item->unsigned_flag; - max_length= my_decimal_precision_to_length(precision, decimals, - unsigned_flag); + max_length= my_decimal_precision_to_length_no_truncation(precision, + decimals, + unsigned_flag); } switch (Field::result_merge_type(fld_type)) diff --git a/sql/item_cmpfunc.cc b/sql/item_cmpfunc.cc index ee823517df8..1ff9ca6a419 100644 --- a/sql/item_cmpfunc.cc +++ b/sql/item_cmpfunc.cc @@ -1484,8 +1484,8 @@ longlong Item_func_truth::val_int() bool Item_in_optimizer::fix_left(THD *thd, Item **ref) { - if (!args[0]->fixed && args[0]->fix_fields(thd, args) || - !cache && !(cache= Item_cache::get_cache(args[0]))) + if ((!args[0]->fixed && args[0]->fix_fields(thd, args)) || + (!cache && !(cache= Item_cache::get_cache(args[0])))) return 1; cache->setup(args[0]); @@ -2760,8 +2760,9 @@ void Item_func_case::fix_length_and_dec() agg_num_lengths(args[i + 1]); if (else_expr_num != -1) agg_num_lengths(args[else_expr_num]); - max_length= my_decimal_precision_to_length(max_length + decimals, decimals, - unsigned_flag); + max_length= my_decimal_precision_to_length_no_truncation(max_length + + decimals, decimals, + unsigned_flag); } } @@ -2996,8 +2997,8 @@ int cmp_longlong(void *cmp_arg, One of the args is unsigned and is too big to fit into the positive signed range. Report no match. */ - if (a->unsigned_flag && ((ulonglong) a->val) > (ulonglong) LONGLONG_MAX || - b->unsigned_flag && ((ulonglong) b->val) > (ulonglong) LONGLONG_MAX) + if ((a->unsigned_flag && ((ulonglong) a->val) > (ulonglong) LONGLONG_MAX) || + (b->unsigned_flag && ((ulonglong) b->val) > (ulonglong) LONGLONG_MAX)) return a->unsigned_flag ? 1 : -1; /* Although the signedness differs both args can fit into the signed diff --git a/sql/item_func.cc b/sql/item_func.cc index 0c8f236a27c..55d4b37ddb0 100644 --- a/sql/item_func.cc +++ b/sql/item_func.cc @@ -452,11 +452,45 @@ Field *Item_func::tmp_table_field(TABLE *table) return make_string_field(table); break; case DECIMAL_RESULT: - field= new Field_new_decimal(my_decimal_precision_to_length(decimal_precision(), - decimals, - unsigned_flag), - maybe_null, name, decimals, unsigned_flag); + { + uint8 dec= decimals; + uint8 intg= decimal_precision() - dec; + uint32 len= max_length; + + /* + Trying to put too many digits overall in a DECIMAL(prec,dec) + will always throw a warning. We must limit dec to + DECIMAL_MAX_SCALE however to prevent an assert() later. + */ + + if (dec > 0) + { + int overflow; + + dec= min(dec, DECIMAL_MAX_SCALE); + + /* + If the value still overflows the field with the corrected dec, + we'll throw out decimals rather than integers. This is still + bad and of course throws a truncation warning. + */ + + const int required_length= + my_decimal_precision_to_length(intg + dec, dec, + unsigned_flag); + + overflow= required_length - len; + + if (overflow > 0) + dec= max(0, dec - overflow); // too long, discard fract + else + /* Corrected value fits. */ + len= required_length; + } + + field= new Field_new_decimal(len, maybe_null, name, dec, unsigned_flag); break; + } case ROW_RESULT: default: // This case should never be chosen @@ -545,8 +579,8 @@ void Item_func::count_decimal_length() set_if_smaller(unsigned_flag, args[i]->unsigned_flag); } int precision= min(max_int_part + decimals, DECIMAL_MAX_PRECISION); - max_length= my_decimal_precision_to_length(precision, decimals, - unsigned_flag); + max_length= my_decimal_precision_to_length_no_truncation(precision, decimals, + unsigned_flag); } @@ -1141,16 +1175,15 @@ void Item_func_additive_op::result_precision() decimals= max(args[0]->decimals, args[1]->decimals); int arg1_int= args[0]->decimal_precision() - args[0]->decimals; int arg2_int= args[1]->decimal_precision() - args[1]->decimals; - int est_prec= max(arg1_int, arg2_int) + 1 + decimals; - int precision= min(est_prec, DECIMAL_MAX_PRECISION); + int precision= max(arg1_int, arg2_int) + 1 + decimals; /* Integer operations keep unsigned_flag if one of arguments is unsigned */ if (result_type() == INT_RESULT) unsigned_flag= args[0]->unsigned_flag | args[1]->unsigned_flag; else unsigned_flag= args[0]->unsigned_flag & args[1]->unsigned_flag; - max_length= my_decimal_precision_to_length(precision, decimals, - unsigned_flag); + max_length= my_decimal_precision_to_length_no_truncation(precision, decimals, + unsigned_flag); } @@ -1255,7 +1288,8 @@ void Item_func_mul::result_precision() decimals= min(args[0]->decimals + args[1]->decimals, DECIMAL_MAX_SCALE); uint est_prec = args[0]->decimal_precision() + args[1]->decimal_precision(); uint precision= min(est_prec, DECIMAL_MAX_PRECISION); - max_length= my_decimal_precision_to_length(precision, decimals,unsigned_flag); + max_length= my_decimal_precision_to_length_no_truncation(precision, decimals, + unsigned_flag); } @@ -1311,8 +1345,8 @@ void Item_func_div::result_precision() else unsigned_flag= args[0]->unsigned_flag & args[1]->unsigned_flag; decimals= min(args[0]->decimals + prec_increment, DECIMAL_MAX_SCALE); - max_length= my_decimal_precision_to_length(precision, decimals, - unsigned_flag); + max_length= my_decimal_precision_to_length_no_truncation(precision, decimals, + unsigned_flag); } @@ -1944,8 +1978,8 @@ void Item_func_round::fix_length_and_dec() unsigned_flag= args[0]->unsigned_flag; if (!args[1]->const_item()) { - max_length= args[0]->max_length; decimals= args[0]->decimals; + max_length= float_length(decimals); if (args[0]->result_type() == DECIMAL_RESULT) { max_length++; @@ -1965,8 +1999,8 @@ void Item_func_round::fix_length_and_dec() if (args[0]->decimals == NOT_FIXED_DEC) { - max_length= args[0]->max_length; decimals= min(decimals_to_set, NOT_FIXED_DEC); + max_length= float_length(decimals); hybrid_type= REAL_RESULT; return; } @@ -1999,8 +2033,9 @@ void Item_func_round::fix_length_and_dec() precision-= decimals_delta - length_increase; decimals= min(decimals_to_set, DECIMAL_MAX_SCALE); - max_length= my_decimal_precision_to_length(precision, decimals, - unsigned_flag); + max_length= my_decimal_precision_to_length_no_truncation(precision, + decimals, + unsigned_flag); break; } default: @@ -2243,8 +2278,9 @@ void Item_func_min_max::fix_length_and_dec() } } else if ((cmp_type == DECIMAL_RESULT) || (cmp_type == INT_RESULT)) - max_length= my_decimal_precision_to_length(max_int_part+decimals, decimals, - unsigned_flag); + max_length= my_decimal_precision_to_length_no_truncation(max_int_part + + decimals, decimals, + unsigned_flag); cached_field_type= agg_field_type(args, arg_count); } @@ -4463,8 +4499,8 @@ int Item_func_set_user_var::save_in_field(Field *field, bool no_conversions, update(); if (result_type() == STRING_RESULT || - result_type() == REAL_RESULT && - field->result_type() == STRING_RESULT) + (result_type() == REAL_RESULT && + field->result_type() == STRING_RESULT)) { String *result; CHARSET_INFO *cs= collation.collation; @@ -5802,6 +5838,14 @@ Item_func_sp::func_name() const } +int my_missing_function_error(const LEX_STRING &token, const char *func_name) +{ + if (token.length && is_lex_native_function (&token)) + return my_error(ER_FUNC_INEXISTENT_NAME_COLLISION, MYF(0), func_name); + else + return my_error(ER_SP_DOES_NOT_EXIST, MYF(0), "FUNCTION", func_name); +} + /** @brief Initialize the result field by creating a temporary dummy table @@ -5834,7 +5878,7 @@ Item_func_sp::init_result_field(THD *thd) if (!(m_sp= sp_find_routine(thd, TYPE_ENUM_FUNCTION, m_name, &thd->sp_func_cache, TRUE))) { - my_error(ER_SP_DOES_NOT_EXIST, MYF(0), "FUNCTION", m_name->m_qname.str); + my_missing_function_error (m_name->m_name, m_name->m_qname.str); context->process_error(thd); DBUG_RETURN(TRUE); } @@ -5946,6 +5990,9 @@ Item_func_sp::execute_impl(THD *thd) #ifndef NO_EMBEDDED_ACCESS_CHECKS Security_context *save_security_ctx= thd->security_ctx; #endif + enum enum_sp_data_access access= + (m_sp->m_chistics->daccess == SP_DEFAULT_ACCESS) ? + SP_DEFAULT_ACCESS_MAPPING : m_sp->m_chistics->daccess; DBUG_ENTER("Item_func_sp::execute_impl"); @@ -5963,11 +6010,13 @@ Item_func_sp::execute_impl(THD *thd) Throw an error if a non-deterministic function is called while statement-based replication (SBR) is active. */ + if (!m_sp->m_chistics->detistic && !trust_function_creators && + (access == SP_CONTAINS_SQL || access == SP_MODIFIES_SQL_DATA) && (mysql_bin_log.is_open() && thd->variables.binlog_format == BINLOG_FORMAT_STMT)) { - my_error(ER_BINLOG_ROW_RBR_TO_SBR, MYF(0)); + my_error(ER_BINLOG_UNSAFE_ROUTINE, MYF(0)); goto error; } diff --git a/sql/item_func.h b/sql/item_func.h index 67049af81a2..025ac12fe07 100644 --- a/sql/item_func.h +++ b/sql/item_func.h @@ -378,7 +378,8 @@ public: Item_decimal_typecast(Item *a, int len, int dec) :Item_func(a) { decimals= dec; - max_length= my_decimal_precision_to_length(len, dec, unsigned_flag); + max_length= my_decimal_precision_to_length_no_truncation(len, dec, + unsigned_flag); } String *val_str(String *str); double val_real(); @@ -1518,6 +1519,7 @@ public: ft_handler->please->close_search(ft_handler); ft_handler= 0; concat_ws= 0; + table= 0; // required by Item_func_match::eq() DBUG_VOID_RETURN; } enum Functype functype() const { return FT_FUNC; } diff --git a/sql/item_geofunc.cc b/sql/item_geofunc.cc index 24a92c78e9c..a34204b7181 100644 --- a/sql/item_geofunc.cc +++ b/sql/item_geofunc.cc @@ -416,7 +416,10 @@ String *Item_func_spatial_collection::val_str(String *str) else { enum Geometry::wkbType wkb_type; - const char *data= res->ptr() + 4/*SRID*/ + 1; + const uint data_offset= 4/*SRID*/ + 1; + if (res->length() < data_offset + sizeof(uint32)) + goto err; + const char *data= res->ptr() + data_offset; /* In the case of named collection we must check that items @@ -439,7 +442,7 @@ String *Item_func_spatial_collection::val_str(String *str) break; case Geometry::wkb_linestring: - if (str->append(data, POINT_DATA_SIZE, 512)) + if (len < POINT_DATA_SIZE || str->append(data, POINT_DATA_SIZE, 512)) goto err; break; case Geometry::wkb_polygon: @@ -448,11 +451,15 @@ String *Item_func_spatial_collection::val_str(String *str) double x1, y1, x2, y2; const char *org_data= data; - if (len < 4 + 2 * POINT_DATA_SIZE) + if (len < 4) goto err; n_points= uint4korr(data); data+= 4; + + if (n_points < 2 || len < 4 + n_points * POINT_DATA_SIZE) + goto err; + float8get(x1, data); data+= SIZEOF_STORED_DOUBLE; float8get(y1, data); diff --git a/sql/item_strfunc.cc b/sql/item_strfunc.cc index f9210c842bb..be94f19f597 100644 --- a/sql/item_strfunc.cc +++ b/sql/item_strfunc.cc @@ -1629,16 +1629,17 @@ String *Item_func_password::val_str(String *str) return 0; if (res->length() == 0) return &my_empty_string; - make_scrambled_password(tmp_value, res->c_ptr()); + my_make_scrambled_password(tmp_value, res->ptr(), res->length()); str->set(tmp_value, SCRAMBLED_PASSWORD_CHAR_LENGTH, res->charset()); return str; } -char *Item_func_password::alloc(THD *thd, const char *password) +char *Item_func_password::alloc(THD *thd, const char *password, + size_t pass_len) { char *buff= (char *) thd->alloc(SCRAMBLED_PASSWORD_CHAR_LENGTH+1); if (buff) - make_scrambled_password(buff, password); + my_make_scrambled_password(buff, password, pass_len); return buff; } @@ -1652,16 +1653,17 @@ String *Item_func_old_password::val_str(String *str) return 0; if (res->length() == 0) return &my_empty_string; - make_scrambled_password_323(tmp_value, res->c_ptr()); + my_make_scrambled_password_323(tmp_value, res->ptr(), res->length()); str->set(tmp_value, SCRAMBLED_PASSWORD_CHAR_LENGTH_323, res->charset()); return str; } -char *Item_func_old_password::alloc(THD *thd, const char *password) +char *Item_func_old_password::alloc(THD *thd, const char *password, + size_t pass_len) { char *buff= (char *) thd->alloc(SCRAMBLED_PASSWORD_CHAR_LENGTH_323+1); if (buff) - make_scrambled_password_323(buff, password); + my_make_scrambled_password_323(buff, password, pass_len); return buff; } @@ -2708,13 +2710,12 @@ String *Item_func_conv_charset::val_str(String *str) return null_value ? 0 : &str_value; /* Here we don't pass 'str' as a parameter to args[0]->val_str() - as 'str' may points to 'str_value' (e.g. see Item::save_in_field()), + as 'str' may point to 'str_value' (e.g. see Item::save_in_field()), which we use below to convert string. Use argument's 'str_value' instead. */ - String *arg= args[0]->val_str(&args[0]->str_value);; + String *arg= args[0]->val_str(&args[0]->str_value); uint dummy_errors; - arg= args[0]->val_str(&args[0]->str_value); if (!arg) { null_value=1; diff --git a/sql/item_strfunc.h b/sql/item_strfunc.h index 5265f608344..2cdb45100ae 100644 --- a/sql/item_strfunc.h +++ b/sql/item_strfunc.h @@ -268,7 +268,7 @@ public: String *val_str(String *str); void fix_length_and_dec() { max_length= SCRAMBLED_PASSWORD_CHAR_LENGTH; } const char *func_name() const { return "password"; } - static char *alloc(THD *thd, const char *password); + static char *alloc(THD *thd, const char *password, size_t pass_len); }; @@ -287,7 +287,7 @@ public: String *val_str(String *str); void fix_length_and_dec() { max_length= SCRAMBLED_PASSWORD_CHAR_LENGTH_323; } const char *func_name() const { return "old_password"; } - static char *alloc(THD *thd, const char *password); + static char *alloc(THD *thd, const char *password, size_t pass_len); }; diff --git a/sql/item_subselect.cc b/sql/item_subselect.cc index 3981b91a27c..cdb091fa07e 100644 --- a/sql/item_subselect.cc +++ b/sql/item_subselect.cc @@ -1227,6 +1227,10 @@ Item_in_subselect::single_value_transformer(JOIN *join, else { // it is single select without tables => possible optimization + // remove the dependence mark since the item is moved to upper + // select and is not outer anymore. + item->walk(&Item::remove_dependence_processor, 0, + (uchar *) select_lex->outer_select()); item= func->create(left_expr, item); // fix_field of item will be done in time of substituting substitution= item; @@ -1314,8 +1318,8 @@ Item_in_subselect::row_value_transformer(JOIN *join) Item *item_having_part2= 0; for (uint i= 0; i < cols_num; i++) { - DBUG_ASSERT(left_expr->fixed && - select_lex->ref_pointer_array[i]->fixed || + DBUG_ASSERT((left_expr->fixed && + select_lex->ref_pointer_array[i]->fixed) || (select_lex->ref_pointer_array[i]->type() == REF_ITEM && ((Item_ref*)(select_lex->ref_pointer_array[i]))->ref_type() == Item_ref::OUTER_REF)); @@ -1392,8 +1396,8 @@ Item_in_subselect::row_value_transformer(JOIN *join) for (uint i= 0; i < cols_num; i++) { Item *item, *item_isnull; - DBUG_ASSERT(left_expr->fixed && - select_lex->ref_pointer_array[i]->fixed || + DBUG_ASSERT((left_expr->fixed && + select_lex->ref_pointer_array[i]->fixed) || (select_lex->ref_pointer_array[i]->type() == REF_ITEM && ((Item_ref*)(select_lex->ref_pointer_array[i]))->ref_type() == Item_ref::OUTER_REF)); diff --git a/sql/item_sum.cc b/sql/item_sum.cc index a6d8bb8a52d..38251294053 100644 --- a/sql/item_sum.cc +++ b/sql/item_sum.cc @@ -798,8 +798,9 @@ void Item_sum_sum::fix_length_and_dec() { /* SUM result can't be longer than length(arg) + length(MAX_ROWS) */ int precision= args[0]->decimal_precision() + DECIMAL_LONGLONG_DIGITS; - max_length= my_decimal_precision_to_length(precision, decimals, - unsigned_flag); + max_length= my_decimal_precision_to_length_no_truncation(precision, + decimals, + unsigned_flag); curr_dec_buff= 0; hybrid_type= DECIMAL_RESULT; my_decimal_set_zero(dec_buffs); @@ -1233,8 +1234,9 @@ void Item_sum_avg::fix_length_and_dec() { int precision= args[0]->decimal_precision() + prec_increment; decimals= min(args[0]->decimals + prec_increment, DECIMAL_MAX_SCALE); - max_length= my_decimal_precision_to_length(precision, decimals, - unsigned_flag); + max_length= my_decimal_precision_to_length_no_truncation(precision, + decimals, + unsigned_flag); f_precision= min(precision+DECIMAL_LONGLONG_DIGITS, DECIMAL_MAX_PRECISION); f_scale= args[0]->decimals; dec_bin_size= my_decimal_get_binary_size(f_precision, f_scale); @@ -1439,8 +1441,9 @@ void Item_sum_variance::fix_length_and_dec() { int precision= args[0]->decimal_precision()*2 + prec_increment; decimals= min(args[0]->decimals + prec_increment, DECIMAL_MAX_SCALE); - max_length= my_decimal_precision_to_length(precision, decimals, - unsigned_flag); + max_length= my_decimal_precision_to_length_no_truncation(precision, + decimals, + unsigned_flag); break; } @@ -2640,8 +2643,8 @@ bool Item_sum_count_distinct::setup(THD *thd) enum enum_field_types f_type= f->type(); tree_key_length+= f->pack_length(); if ((f_type == MYSQL_TYPE_VARCHAR) || - !f->binary() && (f_type == MYSQL_TYPE_STRING || - f_type == MYSQL_TYPE_VAR_STRING)) + (!f->binary() && (f_type == MYSQL_TYPE_STRING || + f_type == MYSQL_TYPE_VAR_STRING))) { all_binary= FALSE; break; @@ -3324,8 +3327,13 @@ bool Item_func_group_concat::add() TREE_ELEMENT *el= 0; // Only for safety if (row_eligible && tree) + { el= tree_insert(tree, table->record[0] + table->s->null_bytes, 0, tree->custom_arg); + /* check if there was enough memory to insert the row */ + if (!el) + return 1; + } /* If the row is not a duplicate (el->count == 1) we can dump the row here in case of GROUP_CONCAT(DISTINCT...) diff --git a/sql/item_timefunc.cc b/sql/item_timefunc.cc index 8caff22eab9..d79b0b02998 100644 --- a/sql/item_timefunc.cc +++ b/sql/item_timefunc.cc @@ -446,7 +446,7 @@ static bool extract_date_time(DATE_TIME_FORMAT *format, strict_week_number= (*ptr=='V' || *ptr=='v'); tmp= (char*) val + min(val_len, 2); if ((week_number= (int) my_strtoll10(val, &tmp, &error)) < 0 || - strict_week_number && !week_number || + (strict_week_number && !week_number) || week_number > 53) goto err; val= tmp; @@ -542,10 +542,10 @@ static bool extract_date_time(DATE_TIME_FORMAT *format, %V,%v require %X,%x resprectively, %U,%u should be used with %Y and not %X or %x */ - if (strict_week_number && + if ((strict_week_number && (strict_week_number_year < 0 || - strict_week_number_year_type != sunday_first_n_first_week_non_iso) || - !strict_week_number && strict_week_number_year >= 0) + strict_week_number_year_type != sunday_first_n_first_week_non_iso)) || + (!strict_week_number && strict_week_number_year >= 0)) goto err; /* Number of days since year 0 till 1st Jan of this year */ diff --git a/sql/lex.h b/sql/lex.h index acb81dcf717..0a85824f6f7 100644 --- a/sql/lex.h +++ b/sql/lex.h @@ -387,7 +387,6 @@ static SYMBOL symbols[] = { { "PACK_KEYS", SYM(PACK_KEYS_SYM)}, { "PARSER", SYM(PARSER_SYM)}, { "PAGE", SYM(PAGE_SYM)}, - { "PAGE_CHECKSUM", SYM(PAGE_CHECKSUM_SYM)}, { "PARTIAL", SYM(PARTIAL)}, { "PARTITION", SYM(PARTITION_SYM)}, { "PARTITIONING", SYM(PARTITIONING_SYM)}, @@ -543,7 +542,6 @@ static SYMBOL symbols[] = { { "TO", SYM(TO_SYM)}, { "TRAILING", SYM(TRAILING)}, { "TRANSACTION", SYM(TRANSACTION_SYM)}, - { "TRANSACTIONAL", SYM(TRANSACTIONAL_SYM)}, { "TRIGGER", SYM(TRIGGER_SYM)}, { "TRIGGERS", SYM(TRIGGERS_SYM)}, { "TRUE", SYM(TRUE_SYM)}, diff --git a/sql/log.cc b/sql/log.cc index ed2eff6625d..bb81d0c723e 100644 --- a/sql/log.cc +++ b/sql/log.cc @@ -153,7 +153,8 @@ private: class binlog_trx_data { public: binlog_trx_data() - : at_least_one_stmt(0), m_pending(0), before_stmt_pos(MY_OFF_T_UNDEF) + : at_least_one_stmt(0), incident(FALSE), m_pending(0), + before_stmt_pos(MY_OFF_T_UNDEF) { trans_log.end_of_file= max_binlog_cache_size; } @@ -184,6 +185,7 @@ public: delete pending(); set_pending(0); reinit_io_cache(&trans_log, WRITE_CACHE, pos, 0, 0); + trans_log.end_of_file= max_binlog_cache_size; if (pos < before_stmt_pos) before_stmt_pos= MY_OFF_T_UNDEF; @@ -206,6 +208,7 @@ public: if (!empty()) truncate(0); before_stmt_pos= MY_OFF_T_UNDEF; + incident= FALSE; trans_log.end_of_file= max_binlog_cache_size; DBUG_ASSERT(empty()); } @@ -222,11 +225,22 @@ public: IO_CACHE trans_log; // The transaction cache + void set_incident(void) + { + incident= TRUE; + } + + bool has_incident(void) + { + return(incident); + } + /** Boolean that is true if there is at least one statement in the transaction cache. */ bool at_least_one_stmt; + bool incident; private: /* @@ -942,7 +956,7 @@ bool LOGGER::slow_log_print(THD *thd, const char *query, uint query_length, bool error= FALSE; Log_event_handler **current_handler; bool is_command= FALSE; - char user_host_buff[MAX_USER_HOST_SIZE]; + char user_host_buff[MAX_USER_HOST_SIZE + 1]; Security_context *sctx= thd->security_ctx; uint user_host_len= 0; ulonglong query_utime, lock_utime; @@ -1008,7 +1022,7 @@ bool LOGGER::general_log_write(THD *thd, enum enum_server_command command, { bool error= FALSE; Log_event_handler **current_handler= general_log_handler_list; - char user_host_buff[MAX_USER_HOST_SIZE]; + char user_host_buff[MAX_USER_HOST_SIZE + 1]; Security_context *sctx= thd->security_ctx; ulong id; uint user_host_len= 0; @@ -1391,7 +1405,8 @@ binlog_end_trans(THD *thd, binlog_trx_data *trx_data, */ if (end_ev != NULL) { - thd->binlog_flush_pending_rows_event(TRUE); + if (thd->binlog_flush_pending_rows_event(TRUE)) + DBUG_RETURN(1); /* Doing a commit or a rollback including non-transactional tables, i.e., ending a transaction where we might write the transaction @@ -1402,7 +1417,8 @@ binlog_end_trans(THD *thd, binlog_trx_data *trx_data, were, we would have to ensure that we're not ending a statement inside a stored function. */ - error= mysql_bin_log.write(thd, &trx_data->trans_log, end_ev); + error= mysql_bin_log.write(thd, &trx_data->trans_log, end_ev, + trx_data->has_incident()); trx_data->reset(); /* @@ -1428,7 +1444,11 @@ binlog_end_trans(THD *thd, binlog_trx_data *trx_data, */ thd->binlog_remove_pending_rows_event(TRUE); if (all || !(thd->options & (OPTION_BEGIN | OPTION_NOT_AUTOCOMMIT))) + { + if (trx_data->has_incident()) + mysql_bin_log.write_incident(thd, TRUE); trx_data->reset(); + } else // ...statement trx_data->truncate(trx_data->before_stmt_pos); @@ -1502,8 +1522,7 @@ static int binlog_commit(handlerton *hton, THD *thd, bool all) YESNO(thd->transaction.stmt.modified_non_trans_table))); if (!in_transaction || all) { - Query_log_event qev(thd, STRING_WITH_LEN("COMMIT"), TRUE, FALSE); - qev.error_code= 0; // see comment in MYSQL_LOG::write(THD, IO_CACHE) + Query_log_event qev(thd, STRING_WITH_LEN("COMMIT"), TRUE, TRUE, 0); error= binlog_end_trans(thd, trx_data, &qev, all); goto end; } @@ -1545,37 +1564,100 @@ static int binlog_rollback(handlerton *hton, THD *thd, bool all) YESNO(all), YESNO(thd->transaction.all.modified_non_trans_table), YESNO(thd->transaction.stmt.modified_non_trans_table))); - if (all && thd->transaction.all.modified_non_trans_table || - !all && thd->transaction.stmt.modified_non_trans_table || - (thd->options & OPTION_KEEP_LOG)) + if (mysql_bin_log.check_write_error(thd)) { /* - We write the transaction cache with a rollback last if we have - modified any non-transactional table. We do this even if we are - committing a single statement that has modified a - non-transactional table since it can have modified a - transactional table in that statement as well, which needs to be - rolled back on the slave. + "all == true" means that a "rollback statement" triggered the error and + this function was called. However, this must not happen as a rollback + is written directly to the binary log. And in auto-commit mode, a single + statement that is rolled back has the flag all == false. */ - Query_log_event qev(thd, STRING_WITH_LEN("ROLLBACK"), TRUE, FALSE); - qev.error_code= 0; // see comment in MYSQL_LOG::write(THD, IO_CACHE) - error= binlog_end_trans(thd, trx_data, &qev, all); + DBUG_ASSERT(!all); + /* + We reach this point if either only transactional tables were modified or + the effect of a statement that did not get into the binlog needs to be + rolled back. In the latter case, if a statement changed non-transactional + tables or had the OPTION_KEEP_LOG associated, we write an incident event + to the binlog in order to stop slaves and notify users that some changes + on the master did not get into the binlog and slaves will be inconsistent. + On the other hand, if a statement is transactional, we just safely roll it + back. + */ + if ((thd->transaction.stmt.modified_non_trans_table || + (thd->options & OPTION_KEEP_LOG)) && + mysql_bin_log.check_write_error(thd)) + trx_data->set_incident(); + error= binlog_end_trans(thd, trx_data, 0, all); } - else if (all && !thd->transaction.all.modified_non_trans_table || - !all && !thd->transaction.stmt.modified_non_trans_table) + else { + /* + We flush the cache with a rollback, wrapped in a beging/rollback if: + . aborting a transcation that modified a non-transactional table or; + . aborting a statement that modified both transactional and + non-transctional tables but which is not in the boundaries of any + transaction; + . the OPTION_KEEP_LOG is activate. + */ + if ((all && thd->transaction.all.modified_non_trans_table) || + (!all && thd->transaction.stmt.modified_non_trans_table && + !(thd->options & (OPTION_BEGIN | OPTION_NOT_AUTOCOMMIT))) || + ((thd->options & OPTION_KEEP_LOG))) + { + Query_log_event qev(thd, STRING_WITH_LEN("ROLLBACK"), TRUE, TRUE, 0); + error= binlog_end_trans(thd, trx_data, &qev, all); + } /* - If we have modified only transactional tables, we can truncate - the transaction cache without writing anything to the binary - log. - */ - error= binlog_end_trans(thd, trx_data, 0, all); + Otherwise, we simply truncate the cache as there is no change on + non-transactional tables as follows. + */ + else if ((all && !thd->transaction.all.modified_non_trans_table) || + (!all && !thd->transaction.stmt.modified_non_trans_table)) + error= binlog_end_trans(thd, trx_data, 0, all); } if (!all) trx_data->before_stmt_pos = MY_OFF_T_UNDEF; // part of the stmt rollback DBUG_RETURN(error); } +void MYSQL_BIN_LOG::set_write_error(THD *thd) +{ + DBUG_ENTER("MYSQL_BIN_LOG::set_write_error"); + + write_error= 1; + + if (check_write_error(thd)) + DBUG_VOID_RETURN; + + if (my_errno == EFBIG) + my_message(ER_TRANS_CACHE_FULL, ER(ER_TRANS_CACHE_FULL), MYF(MY_WME)); + else + my_error(ER_ERROR_ON_WRITE, MYF(MY_WME), name, errno); + + DBUG_VOID_RETURN; +} + +bool MYSQL_BIN_LOG::check_write_error(THD *thd) +{ + DBUG_ENTER("MYSQL_BIN_LOG::check_write_error"); + + bool checked= FALSE; + + if (!thd->is_error()) + DBUG_RETURN(checked); + + switch (thd->main_da.sql_errno()) + { + case ER_TRANS_CACHE_FULL: + case ER_ERROR_ON_WRITE: + case ER_BINLOG_LOGGING_IMPOSSIBLE: + checked= TRUE; + break; + } + + DBUG_RETURN(checked); +} + /** @note How do we handle this (unlikely but legal) case: @@ -1606,10 +1688,11 @@ static int binlog_savepoint_set(handlerton *hton, THD *thd, void *sv) binlog_trans_log_savepos(thd, (my_off_t*) sv); /* Write it to the binary log */ - + + int errcode= query_error_code(thd, thd->killed == THD::NOT_KILLED); int const error= thd->binlog_query(THD::STMT_QUERY_TYPE, - thd->query, thd->query_length, TRUE, FALSE); + thd->query, thd->query_length, TRUE, FALSE, errcode); DBUG_RETURN(error); } @@ -1625,9 +1708,10 @@ static int binlog_savepoint_rollback(handlerton *hton, THD *thd, void *sv) if (unlikely(thd->transaction.all.modified_non_trans_table || (thd->options & OPTION_KEEP_LOG))) { + int errcode= query_error_code(thd, thd->killed == THD::NOT_KILLED); int error= thd->binlog_query(THD::STMT_QUERY_TYPE, - thd->query, thd->query_length, TRUE, FALSE); + thd->query, thd->query_length, TRUE, FALSE, errcode); DBUG_RETURN(error); } binlog_trans_log_truncate(thd, *(my_off_t*)sv); @@ -2065,6 +2149,9 @@ bool MYSQL_QUERY_LOG::write(time_t event_time, const char *user_host, /* Test if someone closed between the is_open test and lock */ if (is_open()) { + /* for testing output of timestamp and thread id */ + DBUG_EXECUTE_IF("reset_log_last_time", last_time= 0;); + /* Note that my_b_write() assumes it knows the length for this */ if (event_time != last_time) { @@ -2073,7 +2160,7 @@ bool MYSQL_QUERY_LOG::write(time_t event_time, const char *user_host, localtime_r(&event_time, &start); time_buff_len= my_snprintf(local_time_buff, MAX_TIME_SIZE, - "%02d%02d%02d %2d:%02d:%02d", + "%02d%02d%02d %2d:%02d:%02d\t", start.tm_year % 100, start.tm_mon + 1, start.tm_mday, start.tm_hour, start.tm_min, start.tm_sec); @@ -3851,6 +3938,7 @@ MYSQL_BIN_LOG::flush_and_set_pending_rows_event(THD *thd, if (pending->write(file)) { pthread_mutex_unlock(&LOCK_log); + set_write_error(thd); DBUG_RETURN(1); } @@ -3925,7 +4013,8 @@ bool MYSQL_BIN_LOG::write(Log_event *event_info) */ bool const end_stmt= thd->prelocked_mode && thd->lex->requires_prelocking(); - thd->binlog_flush_pending_rows_event(end_stmt); + if (thd->binlog_flush_pending_rows_event(end_stmt)) + DBUG_RETURN(error); pthread_mutex_lock(&LOCK_log); @@ -3976,8 +4065,7 @@ bool MYSQL_BIN_LOG::write(Log_event *event_info) DBUG_PRINT("info", ("Using trans_log: cache: %d, trans_log_pos: %lu", event_info->get_cache_stmt(), (ulong) trans_log_pos)); - if (trans_log_pos == 0) - thd->binlog_start_trans_and_stmt(); + thd->binlog_start_trans_and_stmt(); file= trans_log; } /* @@ -4055,7 +4143,8 @@ bool MYSQL_BIN_LOG::write(Log_event *event_info) Write the SQL command */ - if (event_info->write(file)) + if (event_info->write(file) || + DBUG_EVALUATE_IF("injecting_fault_writing", 1, 0)) goto err; if (file == &log_file) // we are writing to the real log (disk) @@ -4069,13 +4158,7 @@ bool MYSQL_BIN_LOG::write(Log_event *event_info) err: if (error) - { - if (my_errno == EFBIG) - my_message(ER_TRANS_CACHE_FULL, ER(ER_TRANS_CACHE_FULL), MYF(0)); - else - my_error(ER_ERROR_ON_WRITE, MYF(0), name, errno); - write_error=1; - } + set_write_error(thd); } if (event_info->flags & LOG_EVENT_UPDATE_TABLE_MAP_VERSION_F) @@ -4327,6 +4410,58 @@ int MYSQL_BIN_LOG::write_cache(IO_CACHE *cache, bool lock_log, bool sync_log) return 0; // All OK } +/* + Helper function to get the error code of the query to be binlogged. + */ +int query_error_code(THD *thd, bool not_killed) +{ + int error; + + if (not_killed) + { + error= thd->is_error() ? thd->main_da.sql_errno() : 0; + + /* thd->main_da.sql_errno() might be ER_SERVER_SHUTDOWN or + ER_QUERY_INTERRUPTED, So here we need to make sure that error + is not set to these errors when specified not_killed by the + caller. + */ + if (error == ER_SERVER_SHUTDOWN || error == ER_QUERY_INTERRUPTED) + error= 0; + } + else + { + /* killed status for DELAYED INSERT thread should never be used */ + DBUG_ASSERT(!(thd->system_thread & SYSTEM_THREAD_DELAYED_INSERT)); + error= thd->killed_errno(); + } + + return error; +} + +bool MYSQL_BIN_LOG::write_incident(THD *thd, bool lock) +{ + uint error= 0; + DBUG_ENTER("MYSQL_BIN_LOG::write_incident"); + LEX_STRING const write_error_msg= + { C_STRING_WITH_LEN("error writing to the binary log") }; + Incident incident= INCIDENT_LOST_EVENTS; + Incident_log_event ev(thd, incident, write_error_msg); + if (lock) + pthread_mutex_lock(&LOCK_log); + ev.write(&log_file); + if (lock) + { + if (!error && !(error= flush_and_sync())) + { + signal_update(); + rotate_and_purge(RP_LOCK_LOG_IS_ALREADY_LOCKED); + } + pthread_mutex_unlock(&LOCK_log); + } + DBUG_RETURN(error); +} + /** Write a cached log entry to the binary log. - To support transaction over replication, we wrap the transaction @@ -4339,6 +4474,9 @@ int MYSQL_BIN_LOG::write_cache(IO_CACHE *cache, bool lock_log, bool sync_log) @param cache The cache to copy to the binlog @param commit_event The commit event to print after writing the contents of the cache. + @param incident Defines if an incident event should be created to + notify that some non-transactional changes did + not get into the binlog. @note We only come here if there is something in the cache. @@ -4348,7 +4486,8 @@ int MYSQL_BIN_LOG::write_cache(IO_CACHE *cache, bool lock_log, bool sync_log) 'cache' needs to be reinitialized after this functions returns. */ -bool MYSQL_BIN_LOG::write(THD *thd, IO_CACHE *cache, Log_event *commit_event) +bool MYSQL_BIN_LOG::write(THD *thd, IO_CACHE *cache, Log_event *commit_event, + bool incident) { DBUG_ENTER("MYSQL_BIN_LOG::write(THD *, IO_CACHE *, Log_event *)"); VOID(pthread_mutex_lock(&LOCK_log)); @@ -4370,19 +4509,8 @@ bool MYSQL_BIN_LOG::write(THD *thd, IO_CACHE *cache, Log_event *commit_event) transaction is either a BEGIN..COMMIT block or a single statement in autocommit mode. */ - Query_log_event qinfo(thd, STRING_WITH_LEN("BEGIN"), TRUE, FALSE); - /* - Imagine this is rollback due to net timeout, after all - statements of the transaction succeeded. Then we want a - zero-error code in BEGIN. In other words, if there was a - really serious error code it's already in the statement's - events, there is no need to put it also in this internally - generated event, and as this event is generated late it would - lead to false alarms. - - This is safer than thd->clear_error() against kills at shutdown. - */ - qinfo.error_code= 0; + Query_log_event qinfo(thd, STRING_WITH_LEN("BEGIN"), TRUE, TRUE, 0); + /* Now this Query_log_event has artificial log_pos 0. It must be adjusted to reflect the real position in the log. Not doing it @@ -4408,6 +4536,10 @@ bool MYSQL_BIN_LOG::write(THD *thd, IO_CACHE *cache, Log_event *commit_event) if (commit_event && commit_event->write(&log_file)) goto err; + + if (incident && write_incident(thd, FALSE)) + goto err; + if (flush_and_sync()) goto err; DBUG_EXECUTE_IF("half_binlogged_transaction", abort();); diff --git a/sql/log.h b/sql/log.h index d54df8add3b..d306d6f7182 100644 --- a/sql/log.h +++ b/sql/log.h @@ -356,9 +356,12 @@ public: void new_file(); bool write(Log_event* event_info); // binary log write - bool write(THD *thd, IO_CACHE *cache, Log_event *commit_event); + bool write(THD *thd, IO_CACHE *cache, Log_event *commit_event, bool incident); + bool write_incident(THD *thd, bool lock); int write_cache(IO_CACHE *cache, bool lock_log, bool flush_and_sync); + void set_write_error(THD *thd); + bool check_write_error(THD *thd); void start_union_events(THD *thd, query_id_t query_id_param); void stop_union_events(THD *thd); @@ -581,4 +584,6 @@ enum enum_binlog_format { }; extern TYPELIB binlog_format_typelib; +int query_error_code(THD *thd, bool not_killed); + #endif /* LOG_H */ diff --git a/sql/log_event.cc b/sql/log_event.cc index 651cd1418e3..e7cbbaba38e 100644 --- a/sql/log_event.cc +++ b/sql/log_event.cc @@ -369,6 +369,34 @@ int convert_handler_error(int error, THD* thd, TABLE *table) return (actual_error); } +inline bool concurrency_error_code(int error) +{ + switch (error) + { + case ER_LOCK_WAIT_TIMEOUT: + case ER_LOCK_DEADLOCK: + case ER_XA_RBDEADLOCK: + return TRUE; + default: + return (FALSE); + } +} + +inline bool unexpected_error_code(int unexpected_error) +{ + switch (unexpected_error) + { + case ER_NET_READ_ERROR: + case ER_NET_ERROR_ON_WRITE: + case ER_QUERY_INTERRUPTED: + case ER_SERVER_SHUTDOWN: + case ER_NEW_ABORTING_CONNECTION: + return(TRUE); + default: + return(FALSE); + } +} + /* pretty_print_str() */ @@ -791,8 +819,8 @@ Log_event::do_shall_skip(Relay_log_info *rli) (ulong) server_id, (ulong) ::server_id, rli->replicate_same_server_id, rli->slave_skip_counter)); - if (server_id == ::server_id && !rli->replicate_same_server_id || - rli->slave_skip_counter == 1 && rli->is_in_group()) + if ((server_id == ::server_id && !rli->replicate_same_server_id) || + (rli->slave_skip_counter == 1 && rli->is_in_group())) return EVENT_SKIP_IGNORE; else if (rli->slave_skip_counter > 0) return EVENT_SKIP_COUNT; @@ -2316,19 +2344,16 @@ Query_log_event::Query_log_event() query_length - size of the `query_arg' array using_trans - there is a modified transactional table suppress_use - suppress the generation of 'USE' statements - killed_status_arg - an optional with default to THD::KILLED_NO_VALUE - if the value is different from the default, the arg - is set to the current thd->killed value. - A caller might need to masquerade thd->killed with - THD::NOT_KILLED. + errcode - the error code of the query + DESCRIPTION Creates an event for binlogging - The value for local `killed_status' can be supplied by caller. + The value for `errcode' should be supplied by caller. */ Query_log_event::Query_log_event(THD* thd_arg, const char* query_arg, ulong query_length, bool using_trans, - bool suppress_use, - THD::killed_state killed_status_arg) + bool suppress_use, int errcode) + :Log_event(thd_arg, (thd_arg->thread_specific_used ? LOG_EVENT_THREAD_SPECIFIC_F : 0) | @@ -2349,22 +2374,7 @@ Query_log_event::Query_log_event(THD* thd_arg, const char* query_arg, { time_t end_time; - if (killed_status_arg == THD::KILLED_NO_VALUE) - killed_status_arg= thd_arg->killed; - error_code= - (killed_status_arg == THD::NOT_KILLED) ? - (thd_arg->is_error() ? thd_arg->main_da.sql_errno() : 0) : - ((thd_arg->system_thread & SYSTEM_THREAD_DELAYED_INSERT) ? 0 : - thd_arg->killed_errno()); - - /* thd_arg->main_da.sql_errno() might be ER_SERVER_SHUTDOWN or - ER_QUERY_INTERRUPTED, So here we need to make sure that - error_code is not set to these errors when specified NOT_KILLED - by the caller - */ - if ((killed_status_arg == THD::NOT_KILLED) && - (error_code == ER_SERVER_SHUTDOWN || error_code == ER_QUERY_INTERRUPTED)) - error_code= 0; + error_code= errcode; time(&end_time); exec_time = (ulong) (end_time - thd_arg->start_time); @@ -2751,7 +2761,8 @@ void Query_log_event::print_query_header(IO_CACHE* file, if (!(flags & LOG_EVENT_SUPPRESS_USE_F) && db) { - if ((different_db= memcmp(print_event_info->db, db, db_len + 1))) + 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_b_printf(file, "use %s%s\n", db, print_event_info->delimiter); @@ -3008,11 +3019,13 @@ int Query_log_event::do_apply_event(Relay_log_info const *rli, ::do_apply_event(), then the companion SET also have so we don't need to reset_one_shot_variables(). */ - if (rpl_filter->db_ok(thd->db)) + if (!strncmp(query_arg, "BEGIN", q_len_arg) || + !strncmp(query_arg, "COMMIT", q_len_arg) || + !strncmp(query_arg, "ROLLBACK", q_len_arg) || + rpl_filter->db_ok(thd->db)) { thd->set_time((time_t)when); - thd->query_length= q_len_arg; - thd->query= (char*)query_arg; + thd->set_query((char*)query_arg, q_len_arg); VOID(pthread_mutex_lock(&LOCK_thread_count)); thd->query_id = next_query_id(); VOID(pthread_mutex_unlock(&LOCK_thread_count)); @@ -3020,7 +3033,7 @@ int Query_log_event::do_apply_event(Relay_log_info const *rli, DBUG_PRINT("query",("%s",thd->query)); if (ignored_error_code((expected_error= error_code)) || - !check_expected_error(thd,rli,expected_error)) + !unexpected_error_code(expected_error)) { if (flags2_inited) /* @@ -3152,8 +3165,8 @@ compare_errors: actual_error= thd->is_error() ? thd->main_da.sql_errno() : 0; DBUG_PRINT("info",("expected_error: %d sql_errno: %d", expected_error, actual_error)); - if ((expected_error != actual_error) && - expected_error && + if ((expected_error && expected_error != actual_error && + !concurrency_error_code(expected_error)) && !ignored_error_code(actual_error) && !ignored_error_code(expected_error)) { @@ -3172,7 +3185,8 @@ Default database: '%s'. Query: '%s'", /* If we get the same error code as expected, or they should be ignored. */ - else if (expected_error == actual_error || + else if ((expected_error == actual_error && + !concurrency_error_code(expected_error)) || ignored_error_code(actual_error)) { DBUG_PRINT("info",("error ignored")); @@ -3216,7 +3230,6 @@ Default database: '%s'. Query: '%s'", } /* End of if (db_ok(... */ end: - VOID(pthread_mutex_lock(&LOCK_thread_count)); /* Probably we have set thd->query, thd->db, thd->catalog to point to places in the data_buf of this event. Now the event is going to be deleted @@ -3229,10 +3242,8 @@ end: */ thd->catalog= 0; thd->set_db(NULL, 0); /* will free the current database */ + thd->set_query(NULL, 0); DBUG_PRINT("info", ("end: query= 0")); - thd->query= 0; // just to be sure - thd->query_length= 0; - VOID(pthread_mutex_unlock(&LOCK_thread_count)); close_thread_tables(thd); /* As a disk space optimization, future masters will not log an event for @@ -3344,8 +3355,8 @@ void Start_log_event_v3::print(FILE* file, PRINT_EVENT_INFO* print_event_info) my_b_printf(&cache," at startup"); my_b_printf(&cache, "\n"); if (flags & LOG_EVENT_BINLOG_IN_USE_F) - my_b_printf(&cache, "# Warning: this binlog was not closed properly. " - "Most probably mysqld crashed writing it.\n"); + my_b_printf(&cache, "# Warning: this binlog is either in use or was not " + "closed properly.\n"); } if (!is_artificial_event() && created) { @@ -4366,7 +4377,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, field); + my_b_printf(&cache, "%s", field); field += field_lens[i] + 1; } @@ -4542,8 +4553,7 @@ int Load_log_event::do_apply_event(NET* net, Relay_log_info const *rli, print_query(FALSE, load_data_query, &end, (char **)&thd->lex->fname_start, (char **)&thd->lex->fname_end); *end= 0; - thd->query_length= end - load_data_query; - thd->query= load_data_query; + thd->set_query(load_data_query, (uint) (end - load_data_query)); if (sql_ex.opt_flags & REPLACE_FLAG) { @@ -4649,12 +4659,9 @@ int Load_log_event::do_apply_event(NET* net, Relay_log_info const *rli, error: thd->net.vio = 0; const char *remember_db= thd->db; - VOID(pthread_mutex_lock(&LOCK_thread_count)); thd->catalog= 0; thd->set_db(NULL, 0); /* will free the current database */ - thd->query= 0; - thd->query_length= 0; - VOID(pthread_mutex_unlock(&LOCK_thread_count)); + thd->set_query(NULL, 0); close_thread_tables(thd); DBUG_EXECUTE_IF("LOAD_DATA_INFILE_has_fatal_error", @@ -6619,9 +6626,9 @@ Execute_load_query_log_event(THD *thd_arg, const char* query_arg, uint fn_pos_end_arg, enum_load_dup_handling dup_handling_arg, bool using_trans, bool suppress_use, - THD::killed_state killed_err_arg): + int errcode): Query_log_event(thd_arg, query_arg, query_length_arg, using_trans, - suppress_use, killed_err_arg), + suppress_use, errcode), file_id(thd_arg->file_id), fn_pos_start(fn_pos_start_arg), fn_pos_end(fn_pos_end_arg), dup_handling(dup_handling_arg) { @@ -6694,7 +6701,7 @@ void Execute_load_query_log_event::print(FILE* file, { my_b_write(&cache, (uchar*) query, fn_pos_start); my_b_printf(&cache, " LOCAL INFILE \'"); - my_b_printf(&cache, local_fname); + my_b_printf(&cache, "%s", local_fname); my_b_printf(&cache, "\'"); if (dup_handling == LOAD_DUP_REPLACE) my_b_printf(&cache, " REPLACE"); @@ -6912,8 +6919,8 @@ Rows_log_event::Rows_log_event(THD *thd_arg, TABLE *tbl_arg, ulong tid, solution, to be able to terminate a started statement in the binary log: the extraneous events will be removed in the future. */ - DBUG_ASSERT(tbl_arg && tbl_arg->s && tid != ~0UL || - !tbl_arg && !cols && tid == ~0UL); + DBUG_ASSERT((tbl_arg && tbl_arg->s && tid != ~0UL) || + (!tbl_arg && !cols && tid == ~0UL)); if (thd_arg->options & OPTION_NO_FOREIGN_KEY_CHECKS) set_flags(NO_FOREIGN_KEY_CHECKS_F); @@ -7105,7 +7112,7 @@ int Rows_log_event::do_add_row_data(uchar *row_data, size_t length) #endif DBUG_ASSERT(m_rows_buf <= m_rows_cur); - DBUG_ASSERT(!m_rows_buf || m_rows_end && m_rows_buf < m_rows_end); + DBUG_ASSERT(!m_rows_buf || (m_rows_end && m_rows_buf < m_rows_end)); DBUG_ASSERT(m_rows_cur <= m_rows_end); /* The cast will always work since m_rows_cur <= m_rows_end */ diff --git a/sql/log_event.h b/sql/log_event.h index bda53da8ab0..8202dddcc76 100644 --- a/sql/log_event.h +++ b/sql/log_event.h @@ -676,6 +676,7 @@ typedef struct st_print_event_info #ifdef MYSQL_CLIENT uint verbose; table_mapping m_table_map; + table_mapping m_table_map_ignored; #endif /* @@ -1623,8 +1624,7 @@ public: #ifndef MYSQL_CLIENT Query_log_event(THD* thd_arg, const char* query_arg, ulong query_length, - bool using_trans, bool suppress_use, - THD::killed_state killed_err_arg= THD::KILLED_NO_VALUE); + bool using_trans, bool suppress_use, int error); const char* get_db() { return db; } #ifdef HAVE_REPLICATION void pack_info(Protocol* protocol); @@ -2875,8 +2875,7 @@ public: uint fn_pos_end_arg, enum_load_dup_handling dup_handling_arg, bool using_trans, bool suppress_use, - THD::killed_state - killed_err_arg= THD::KILLED_NO_VALUE); + int errcode); #ifdef HAVE_REPLICATION void pack_info(Protocol* protocol); #endif /* HAVE_REPLICATION */ diff --git a/sql/my_decimal.h b/sql/my_decimal.h index 0e79f70ab4e..21669e82c44 100644 --- a/sql/my_decimal.h +++ b/sql/my_decimal.h @@ -183,6 +183,19 @@ inline uint my_decimal_length_to_precision(uint length, uint scale, (unsigned_flag || !length ? 0:1)); } +inline uint32 my_decimal_precision_to_length_no_truncation(uint precision, + uint8 scale, + bool unsigned_flag) +{ + /* + When precision is 0 it means that original length was also 0. Thus + unsigned_flag is ignored in this case. + */ + DBUG_ASSERT(precision || !scale); + return (uint32)(precision + (scale > 0 ? 1 : 0) + + (unsigned_flag || !precision ? 0 : 1)); +} + inline uint32 my_decimal_precision_to_length(uint precision, uint8 scale, bool unsigned_flag) { @@ -192,8 +205,8 @@ inline uint32 my_decimal_precision_to_length(uint precision, uint8 scale, */ DBUG_ASSERT(precision || !scale); set_if_smaller(precision, DECIMAL_MAX_PRECISION); - return (uint32)(precision + (scale>0 ? 1:0) + - (unsigned_flag || !precision ? 0:1)); + return my_decimal_precision_to_length_no_truncation(precision, scale, + unsigned_flag); } inline diff --git a/sql/mysql_priv.h b/sql/mysql_priv.h index 69a789284b0..2347612f850 100644 --- a/sql/mysql_priv.h +++ b/sql/mysql_priv.h @@ -82,9 +82,9 @@ extern query_id_t global_query_id; inline query_id_t next_query_id() { return global_query_id++; } /* useful constants */ -extern const key_map key_map_empty; -extern key_map key_map_full; /* Should be threaded as const */ -extern const char *primary_key_name; +extern MYSQL_PLUGIN_IMPORT const key_map key_map_empty; +extern MYSQL_PLUGIN_IMPORT key_map key_map_full; /* Should be threaded as const */ +extern MYSQL_PLUGIN_IMPORT const char *primary_key_name; #include "mysql_com.h" #include <violite.h> @@ -123,8 +123,10 @@ char* query_table_status(THD *thd,const char *db,const char *table_name); "in MySQL %s. Please use %s instead.", (Old), (Ver), (New)); \ } while(0) -extern CHARSET_INFO *system_charset_info, *files_charset_info ; -extern CHARSET_INFO *national_charset_info, *table_alias_charset; +extern MYSQL_PLUGIN_IMPORT CHARSET_INFO *system_charset_info; +extern MYSQL_PLUGIN_IMPORT CHARSET_INFO *files_charset_info ; +extern MYSQL_PLUGIN_IMPORT CHARSET_INFO *national_charset_info; +extern MYSQL_PLUGIN_IMPORT CHARSET_INFO *table_alias_charset; enum Derivation @@ -690,14 +692,19 @@ typedef struct st_sql_list { } } SQL_LIST; - +#if defined(MYSQL_DYNAMIC_PLUGIN) && defined(_WIN32) +extern "C" THD *_current_thd_noinline(); +#define _current_thd() _current_thd_noinline() +#else extern pthread_key(THD*, THR_THD); inline THD *_current_thd(void) { return my_pthread_getspecific_ptr(THD*,THR_THD); } +#endif #define current_thd _current_thd() + /** The meat of thd_proc_info(THD*, char*), a macro that packs the last three calling-info parameters. @@ -1413,14 +1420,14 @@ enum enum_schema_tables get_schema_table_idx(ST_SCHEMA_TABLE *schema_table); /* sql_prepare.cc */ -void mysql_stmt_prepare(THD *thd, const char *packet, uint packet_length); -void mysql_stmt_execute(THD *thd, char *packet, uint packet_length); -void mysql_stmt_close(THD *thd, char *packet); +void mysqld_stmt_prepare(THD *thd, const char *packet, uint packet_length); +void mysqld_stmt_execute(THD *thd, char *packet, uint packet_length); +void mysqld_stmt_close(THD *thd, char *packet); void mysql_sql_stmt_prepare(THD *thd); void mysql_sql_stmt_execute(THD *thd); void mysql_sql_stmt_close(THD *thd); -void mysql_stmt_fetch(THD *thd, char *packet, uint packet_length); -void mysql_stmt_reset(THD *thd, char *packet); +void mysqld_stmt_fetch(THD *thd, char *packet, uint packet_length); +void mysqld_stmt_reset(THD *thd, char *packet); void mysql_stmt_get_longdata(THD *thd, char *pos, ulong packet_length); void reinit_stmt_before_use(THD *thd, LEX *lex); @@ -1882,8 +1889,12 @@ extern time_t server_start_time, flush_status_time; #endif /* MYSQL_SERVER */ #if defined MYSQL_SERVER || defined INNODB_COMPATIBILITY_HOOKS extern uint mysql_data_home_len; -extern char *mysql_data_home,server_version[SERVER_VERSION_LENGTH], - mysql_real_data_home[], mysql_unpacked_real_data_home[]; + +extern MYSQL_PLUGIN_IMPORT char *mysql_data_home; +extern char server_version[SERVER_VERSION_LENGTH]; +extern MYSQL_PLUGIN_IMPORT char mysql_real_data_home[]; +extern char mysql_unpacked_real_data_home[]; + extern CHARSET_INFO *character_set_filesystem; #endif /* MYSQL_SERVER || INNODB_COMPATIBILITY_HOOKS */ #ifdef MYSQL_SERVER @@ -1891,10 +1902,13 @@ extern char *opt_mysql_tmpdir, mysql_charsets_dir[], def_ft_boolean_syntax[sizeof(ft_boolean_syntax)]; extern int mysql_unpacked_real_data_home_len; #define mysql_tmpdir (my_tmpdir(&mysql_tmpdir_list)) -extern MY_TMPDIR mysql_tmpdir_list; +extern MYSQL_PLUGIN_IMPORT MY_TMPDIR mysql_tmpdir_list; extern const LEX_STRING command_name[]; -extern const char *first_keyword, *my_localhost, *delayed_user, *binary_keyword; -extern const char **errmesg; /* Error messages */ + +extern const char *first_keyword, *delayed_user, *binary_keyword; +extern MYSQL_PLUGIN_IMPORT const char *my_localhost; +extern MYSQL_PLUGIN_IMPORT const char **errmesg; /* Error messages */ + extern const char *myisam_recover_options_str; extern const char *in_left_expr_name, *in_additional_cond, *in_having_cond; extern const char * const TRG_EXT; @@ -1908,8 +1922,8 @@ extern Le_creator le_creator; extern char language[FN_REFLEN]; #endif /* MYSQL_SERVER */ #if defined MYSQL_SERVER || defined INNODB_COMPATIBILITY_HOOKS -extern char reg_ext[FN_EXTLEN]; -extern uint reg_ext_length; +extern MYSQL_PLUGIN_IMPORT char reg_ext[FN_EXTLEN]; +extern MYSQL_PLUGIN_IMPORT uint reg_ext_length; #endif /* MYSQL_SERVER || INNODB_COMPATIBILITY_HOOKS */ #ifdef MYSQL_SERVER extern char glob_hostname[FN_REFLEN], mysql_home[FN_REFLEN]; @@ -1929,20 +1943,22 @@ extern ulong slave_open_temp_tables; extern ulong query_cache_size, query_cache_min_res_unit; extern ulong slow_launch_threads, slow_launch_time; extern ulong table_cache_size, table_def_size; -extern ulong max_connections,max_connect_errors, connect_timeout; +extern MYSQL_PLUGIN_IMPORT ulong max_connections; +extern ulong max_connect_errors, connect_timeout; extern ulong slave_net_timeout, slave_trans_retries; extern uint max_user_connections; extern ulong what_to_log,flush_time; extern ulong query_buff_size; extern ulong max_prepared_stmt_count, prepared_stmt_count; -extern ulong binlog_cache_size, max_binlog_cache_size, open_files_limit; +extern ulong binlog_cache_size, open_files_limit; +extern ulonglong max_binlog_cache_size; extern ulong max_binlog_size, max_relay_log_size; extern ulong opt_binlog_rows_event_max_size; extern ulong rpl_recovery_rank, thread_cache_size, thread_pool_size; extern ulong back_log; #endif /* MYSQL_SERVER */ #if defined MYSQL_SERVER || defined INNODB_COMPATIBILITY_HOOKS -extern ulong specialflag; +extern ulong MYSQL_PLUGIN_IMPORT specialflag; #endif /* MYSQL_SERVER || INNODB_COMPATIBILITY_HOOKS */ #ifdef MYSQL_SERVER extern ulong current_pid; @@ -1955,7 +1971,7 @@ extern uint protocol_version, mysqld_port, dropping_tables; extern uint delay_key_write_options; #endif /* MYSQL_SERVER */ #if defined MYSQL_SERVER || defined INNODB_COMPATIBILITY_HOOKS -extern uint lower_case_table_names; +extern MYSQL_PLUGIN_IMPORT uint lower_case_table_names; #endif /* MYSQL_SERVER || INNODB_COMPATIBILITY_HOOKS */ #ifdef MYSQL_SERVER extern bool opt_endinfo, using_udf_functions; @@ -1963,7 +1979,7 @@ extern my_bool locked_in_memory; extern bool opt_using_transactions; #endif /* MYSQL_SERVER */ #if defined MYSQL_SERVER || defined INNODB_COMPATIBILITY_HOOKS -extern bool mysqld_embedded; +extern MYSQL_PLUGIN_IMPORT bool mysqld_embedded; #endif /* MYSQL_SERVER || INNODB_COMPATIBILITY_HOOKS */ #ifdef MYSQL_SERVER extern bool opt_large_files, server_id_supplied; @@ -1975,6 +1991,7 @@ extern bool opt_disable_networking, opt_skip_show_db; extern bool opt_ignore_builtin_innodb; extern my_bool opt_character_set_client_handshake; extern bool volatile abort_loop, shutdown_in_progress; +extern bool in_bootstrap; extern uint volatile thread_count, thread_running, global_read_lock; extern uint connection_count; extern my_bool opt_sql_bin_update, opt_safe_user_create, opt_no_mix_types; @@ -2001,7 +2018,7 @@ extern uint opt_large_page_size; extern char *opt_logname, *opt_slow_logname; extern const char *log_output_str; -extern MYSQL_BIN_LOG mysql_bin_log; +extern MYSQL_PLUGIN_IMPORT MYSQL_BIN_LOG mysql_bin_log; extern LOGGER logger; extern TABLE_LIST general_log, slow_log; extern FILE *bootstrap_file; @@ -2009,13 +2026,14 @@ extern int bootstrap_error; extern FILE *stderror_file; extern pthread_key(MEM_ROOT**,THR_MALLOC); extern pthread_mutex_t LOCK_mysql_create_db,LOCK_Acl,LOCK_open, LOCK_lock_db, - LOCK_thread_count,LOCK_mapped_file,LOCK_user_locks, LOCK_status, + LOCK_mapped_file,LOCK_user_locks, LOCK_status, LOCK_error_log, LOCK_delayed_insert, LOCK_uuid_generator, LOCK_delayed_status, LOCK_delayed_create, LOCK_crypt, LOCK_timezone, LOCK_slave_list, LOCK_active_mi, LOCK_manager, LOCK_global_read_lock, LOCK_global_system_variables, LOCK_user_conn, LOCK_prepared_stmt_count, LOCK_bytes_sent, LOCK_bytes_received, LOCK_connection_count; +extern MYSQL_PLUGIN_IMPORT pthread_mutex_t LOCK_thread_count; #ifdef HAVE_OPENSSL extern pthread_mutex_t LOCK_des_key_file; #endif @@ -2035,7 +2053,7 @@ extern const String my_null_string; extern SHOW_VAR status_vars[]; #endif /* MYSQL_SERVER */ #if defined MYSQL_SERVER || defined INNODB_COMPATIBILITY_HOOKS -extern struct system_variables global_system_variables; +extern MYSQL_PLUGIN_IMPORT struct system_variables global_system_variables; #endif /* MYSQL_SERVER || INNODB_COMPATIBILITY_HOOKS */ #ifdef MYSQL_SERVER extern struct system_variables max_system_variables; @@ -2253,6 +2271,16 @@ char *fn_rext(char *name); #if defined MYSQL_SERVER || defined INNODB_COMPATIBILITY_HOOKS uint strconvert(CHARSET_INFO *from_cs, const char *from, CHARSET_INFO *to_cs, char *to, uint to_length, uint *errors); +/* depends on errmsg.txt Database `db`, Table `t` ... */ +#define EXPLAIN_FILENAME_MAX_EXTRA_LENGTH 63 +enum enum_explain_filename_mode +{ + EXPLAIN_ALL_VERBOSE= 0, + EXPLAIN_PARTITIONS_VERBOSE, + EXPLAIN_PARTITIONS_AS_COMMENT +}; +uint explain_filename(const char *from, char *to, uint to_length, + enum_explain_filename_mode explain_mode); uint filename_to_tablename(const char *from, char *to, uint to_length); uint tablename_to_filename(const char *from, char *to, uint to_length); uint check_n_cut_mysql50_prefix(const char *from, char *to, uint to_length); @@ -2311,6 +2339,12 @@ extern void turn_parser_debug_on(); SQL_CRYPT *get_crypt_for_frm(void); #endif +/* password.c */ +extern "C" void my_make_scrambled_password_323(char *to, const char *password, + size_t pass_len); +extern "C" void my_make_scrambled_password(char *to, const char *password, + size_t pass_len); + #include "sql_view.h" /* Some inline functions for more speed */ diff --git a/sql/mysqld.cc b/sql/mysqld.cc index fa8b4a971d7..7e9eb6e7291 100644 --- a/sql/mysqld.cc +++ b/sql/mysqld.cc @@ -416,6 +416,21 @@ my_bool locked_in_memory; bool opt_using_transactions; bool volatile abort_loop; bool volatile shutdown_in_progress; +/* + True if the bootstrap thread is running. Protected by LOCK_thread_count, + just like thread_count. + Used in bootstrap() function to determine if the bootstrap thread + has completed. Note, that we can't use 'thread_count' instead, + since in 5.1, in presence of the Event Scheduler, there may be + event threads running in parallel, so it's impossible to know + what value of 'thread_count' is a sign of completion of the + bootstrap thread. + + At the same time, we can't start the event scheduler after + bootstrap either, since we want to be able to process event-related + SQL commands in the init file and in --bootstrap mode. +*/ +bool in_bootstrap= FALSE; /** @brief 'grant_option' is used to indicate if privileges needs to be checked, in which case the lock, LOCK_grant, is used @@ -507,7 +522,8 @@ ulong slave_net_timeout, slave_trans_retries; ulong slave_exec_mode_options; const char *slave_exec_mode_str= "STRICT"; ulong thread_cache_size=0, thread_pool_size= 0; -ulong binlog_cache_size=0, max_binlog_cache_size=0; +ulong binlog_cache_size=0; +ulonglong max_binlog_cache_size=0; ulong query_cache_size=0; ulong refresh_version; /* Increments on each reload */ query_id_t global_query_id; @@ -982,6 +998,7 @@ static void close_connections(void) } (void) pthread_mutex_unlock(&LOCK_thread_count); + close_active_mi(); DBUG_PRINT("quit",("close_connections thread")); DBUG_VOID_RETURN; } @@ -1663,7 +1680,6 @@ static void network_init(void) opt_enable_named_pipe) { - pipe_name[sizeof(pipe_name)-1]= 0; /* Safety if too long string */ strxnmov(pipe_name, sizeof(pipe_name)-1, "\\\\.\\pipe\\", mysqld_unix_port, NullS); bzero((char*) &saPipeSecurity, sizeof(saPipeSecurity)); @@ -3502,7 +3518,7 @@ static int init_thread_environment() (void) pthread_mutex_init(&LOCK_mysql_create_db,MY_MUTEX_INIT_SLOW); (void) pthread_mutex_init(&LOCK_lock_db,MY_MUTEX_INIT_SLOW); (void) pthread_mutex_init(&LOCK_Acl,MY_MUTEX_INIT_SLOW); - (void) pthread_mutex_init(&LOCK_open, NULL); + (void) pthread_mutex_init(&LOCK_open, MY_MUTEX_INIT_FAST); (void) pthread_mutex_init(&LOCK_thread_count,MY_MUTEX_INIT_FAST); (void) pthread_mutex_init(&LOCK_mapped_file,MY_MUTEX_INIT_SLOW); (void) pthread_mutex_init(&LOCK_status,MY_MUTEX_INIT_FAST); @@ -3649,14 +3665,17 @@ static void init_ssl() #ifdef HAVE_OPENSSL if (opt_use_ssl) { + enum enum_ssl_init_error error= SSL_INITERR_NOERROR; + /* having ssl_acceptor_fd != 0 signals the use of SSL */ ssl_acceptor_fd= new_VioSSLAcceptorFd(opt_ssl_key, opt_ssl_cert, opt_ssl_ca, opt_ssl_capath, - opt_ssl_cipher); + opt_ssl_cipher, &error); DBUG_PRINT("info",("ssl_acceptor_fd: 0x%lx", (long) ssl_acceptor_fd)); if (!ssl_acceptor_fd) { sql_print_warning("Failed to setup SSL"); + sql_print_warning("SSL error: %s", sslGetErrString(error)); opt_use_ssl = 0; have_ssl= SHOW_OPTION_DISABLED; } @@ -4298,7 +4317,6 @@ int main(int argc, char **argv) select_thread=pthread_self(); select_thread_in_use=1; - init_ssl(); #ifdef HAVE_LIBWRAP libwrapName= my_progname+dirname_length(my_progname); @@ -4353,6 +4371,7 @@ we force server id to 2, but this MySQL server will not act as a slave."); if (init_server_components()) unireg_abort(1); + init_ssl(); network_init(); #ifdef __WIN__ @@ -4421,6 +4440,11 @@ we force server id to 2, but this MySQL server will not act as a slave."); unireg_abort(1); } + execute_ddl_log_recovery(); + + if (Events::init(opt_noacl || opt_bootstrap)) + unireg_abort(1); + if (opt_bootstrap) { select_thread_in_use= 0; // Allow 'kill' to work @@ -4432,14 +4456,10 @@ we force server id to 2, but this MySQL server will not act as a slave."); if (read_init_file(opt_init_file)) unireg_abort(1); } - execute_ddl_log_recovery(); create_shutdown_thread(); start_handle_manager(); - if (Events::init(opt_noacl)) - unireg_abort(1); - sql_print_information(ER(ER_STARTUP),my_progname,server_version, ((unix_sock == INVALID_SOCKET) ? (char*) "" : mysqld_unix_port), @@ -4567,15 +4587,28 @@ default_service_handling(char **argv, const char *account_name) { char path_and_service[FN_REFLEN+FN_REFLEN+32], *pos, *end; + const char *opt_delim; end= path_and_service + sizeof(path_and_service)-3; /* We have to quote filename if it contains spaces */ pos= add_quoted_string(path_and_service, file_path, end); if (*extra_opt) { - /* Add (possible quoted) option after file_path */ + /* + Add option after file_path. There will be zero or one extra option. It's + assumed to be --defaults-file=file but isn't checked. The variable (not + the option name) should be quoted if it contains a string. + */ *pos++= ' '; - pos= add_quoted_string(pos, extra_opt, end); + if (opt_delim= strchr(extra_opt, '=')) + { + size_t length= ++opt_delim - extra_opt; + strnmov(pos, extra_opt, length); + } + else + opt_delim= extra_opt; + + pos= add_quoted_string(pos, opt_delim, end); } /* We must have servicename last */ *pos++= ' '; @@ -4721,6 +4754,7 @@ static void bootstrap(FILE *file) thd->security_ctx->master_access= ~(ulong)0; thd->thread_id= thd->variables.pseudo_thread_id= thread_id++; thread_count++; + in_bootstrap= TRUE; bootstrap_file=file; #ifndef EMBEDDED_LIBRARY // TODO: Enable this @@ -4733,7 +4767,7 @@ static void bootstrap(FILE *file) } /* Wait for thread to die */ (void) pthread_mutex_lock(&LOCK_thread_count); - while (thread_count) + while (in_bootstrap) { (void) pthread_cond_wait(&COND_thread_count,&LOCK_thread_count); DBUG_PRINT("quit",("One thread died (count=%u)",thread_count)); @@ -4778,8 +4812,9 @@ void handle_connection_in_main_thread(THD *thd) safe_mutex_assert_owner(&LOCK_thread_count); thread_cache_size=0; // Safety threads.append(thd); - (void) pthread_mutex_unlock(&LOCK_thread_count); - handle_one_connection((void*) thd); + pthread_mutex_unlock(&LOCK_thread_count); + thd->start_utime= my_micro_time(); + handle_one_connection(thd); } @@ -4804,7 +4839,7 @@ void create_thread_to_handle_connection(THD *thd) thread_created++; threads.append(thd); DBUG_PRINT("info",(("creating thread %lu"), thd->thread_id)); - thd->connect_utime= thd->start_utime= my_micro_time(); + thd->prior_thr_create_utime= thd->start_utime= my_micro_time(); if ((error=pthread_create(&thd->real_id,&connection_attrib, handle_one_connection, (void*) thd))) @@ -6581,7 +6616,7 @@ log and this option does nothing anymore.", {"max_binlog_cache_size", OPT_MAX_BINLOG_CACHE_SIZE, "Can be used to restrict the total size used to cache a multi-transaction query.", (uchar**) &max_binlog_cache_size, (uchar**) &max_binlog_cache_size, 0, - GET_ULONG, REQUIRED_ARG, ULONG_MAX, IO_SIZE, ULONG_MAX, 0, IO_SIZE, 0}, + GET_ULL, REQUIRED_ARG, ULONG_MAX, IO_SIZE, ULONGLONG_MAX, 0, IO_SIZE, 0}, {"max_binlog_size", OPT_MAX_BINLOG_SIZE, "Binary log will be rotated automatically when the size exceeds this \ value. Will also apply to relay logs if max_relay_log_size is 0. \ @@ -6618,7 +6653,7 @@ The minimum value for this variable is 4096.", "Joins that are probably going to read more than max_join_size records return an error.", (uchar**) &global_system_variables.max_join_size, (uchar**) &max_system_variables.max_join_size, 0, GET_HA_ROWS, REQUIRED_ARG, - ~0L, 1, ~0L, 0, 1, 0}, + HA_POS_ERROR, 1, HA_POS_ERROR, 0, 1, 0}, {"max_length_for_sort_data", OPT_MAX_LENGTH_FOR_SORT_DATA, "Max number of bytes in sorted records.", (uchar**) &global_system_variables.max_length_for_sort_data, @@ -6764,7 +6799,7 @@ The minimum value for this variable is 4096.", (uchar**) &opt_plugin_dir_ptr, (uchar**) &opt_plugin_dir_ptr, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0}, {"plugin-load", OPT_PLUGIN_LOAD, - "Optional colon-separated list of plugins to load, where each plugin is " + "Optional semicolon-separated list of plugins to load, where each plugin is " "identified as name=library, where name is the plugin name and library " "is the plugin library in plugin_dir.", (uchar**) &opt_plugin_load, (uchar**) &opt_plugin_load, 0, @@ -7627,7 +7662,7 @@ static int mysql_init_variables(void) /* Set directory paths */ strmake(language, LANGUAGE, sizeof(language)-1); - strmake(mysql_real_data_home, get_relative_path(DATADIR), + strmake(mysql_real_data_home, get_relative_path(MYSQL_DATADIR), sizeof(mysql_real_data_home)-1); mysql_data_home_buff[0]=FN_CURLIB; // all paths are relative from here mysql_data_home_buff[1]=0; diff --git a/sql/net_serv.cc b/sql/net_serv.cc index 0a8720bae64..73892f31ccf 100644 --- a/sql/net_serv.cc +++ b/sql/net_serv.cc @@ -186,10 +186,12 @@ my_bool net_realloc(NET *net, size_t length) pkt_length = (length+IO_SIZE-1) & ~(IO_SIZE-1); /* We must allocate some extra bytes for the end 0 and to be able to - read big compressed blocks + read big compressed blocks + 1 safety byte since uint3korr() in + my_real_read() may actually read 4 bytes depending on build flags and + platform. */ if (!(buff= (uchar*) my_realloc((char*) net->buff, pkt_length + - NET_HEADER_SIZE + COMP_HEADER_SIZE, + NET_HEADER_SIZE + COMP_HEADER_SIZE + 1, MYF(MY_WME)))) { /* @todo: 1 and 2 codes are identical. */ @@ -921,6 +923,13 @@ my_real_read(NET *net, size_t *complen) #ifdef HAVE_COMPRESS if (net->compress) { + /* + The following uint3korr() may read 4 bytes, so make sure we don't + read unallocated or uninitialized memory. The right-hand expression + must match the size of the buffer allocated in net_realloc(). + */ + DBUG_ASSERT(net->where_b + NET_HEADER_SIZE + sizeof(uint32) <= + net->max_packet + NET_HEADER_SIZE + COMP_HEADER_SIZE + 1); /* If the packet is compressed then complen > 0 and contains the number of bytes in the uncompressed packet diff --git a/sql/opt_range.cc b/sql/opt_range.cc index 147874611ce..e3aef02637f 100644 --- a/sql/opt_range.cc +++ b/sql/opt_range.cc @@ -3760,8 +3760,8 @@ TABLE_READ_PLAN *get_best_disjunct_quick(PARAM *param, SEL_IMERGE *imerge, DBUG_PRINT("info", ("index_merge scans cost %g", imerge_cost)); if (imerge_too_expensive || (imerge_cost > read_time) || - (non_cpk_scan_records+cpk_scan_records >= param->table->file->stats.records) && - read_time != DBL_MAX) + ((non_cpk_scan_records+cpk_scan_records >= param->table->file->stats.records) && + read_time != DBL_MAX)) { /* Bail out if it is obvious that both index_merge and ROR-union will be @@ -7950,7 +7950,7 @@ QUICK_RANGE_SELECT *get_quick_select_for_ref(THD *thd, TABLE *table, goto err; quick->records= records; - if (cp_buffer_from_ref(thd, table, ref) && thd->is_fatal_error || + if ((cp_buffer_from_ref(thd, table, ref) && thd->is_fatal_error) || !(range= new(alloc) QUICK_RANGE())) goto err; // out of memory @@ -8556,7 +8556,7 @@ int QUICK_RANGE_SELECT::get_next_prefix(uint prefix_length, result= file->read_range_first(last_range->min_keypart_map ? &start_key : 0, last_range->max_keypart_map ? &end_key : 0, test(last_range->flag & EQ_RANGE), - sorted); + TRUE); if (last_range->flag == (UNIQUE_RANGE | EQ_RANGE)) last_range= 0; // Stop searching @@ -8826,7 +8826,7 @@ int QUICK_RANGE_SELECT::cmp_prev(QUICK_RANGE *range_arg) cmp= key_cmp(key_part_info, range_arg->min_key, range_arg->min_length); - if (cmp > 0 || cmp == 0 && !(range_arg->flag & NEAR_MIN)) + if (cmp > 0 || (cmp == 0 && !(range_arg->flag & NEAR_MIN))) return 0; return 1; // outside of range } @@ -10886,8 +10886,14 @@ int QUICK_GROUP_MIN_MAX_SELECT::next_min_in_range() /* Compare the found key with max_key. */ int cmp_res= key_cmp(index_info->key_part, max_key, real_prefix_len + min_max_arg_len); - if (!((cur_range->flag & NEAR_MAX) && (cmp_res == -1) || - (cmp_res <= 0))) + /* + The key is outside of the range if: + the interval is open and the key is equal to the maximum boundry + or + the key is greater than the maximum + */ + if (((cur_range->flag & NEAR_MAX) && cmp_res == 0) || + cmp_res > 0) { result= HA_ERR_KEY_NOT_FOUND; continue; @@ -11004,8 +11010,14 @@ int QUICK_GROUP_MIN_MAX_SELECT::next_max_in_range() /* Compare the found key with min_key. */ int cmp_res= key_cmp(index_info->key_part, min_key, real_prefix_len + min_max_arg_len); - if (!((cur_range->flag & NEAR_MIN) && (cmp_res == 1) || - (cmp_res >= 0))) + /* + The key is outside of the range if: + the interval is open and the key is equal to the minimum boundry + or + the key is less than the minimum + */ + if (((cur_range->flag & NEAR_MIN) && cmp_res == 0) || + cmp_res < 0) continue; } /* If we got to this point, the current key qualifies as MAX. */ diff --git a/sql/opt_sum.cc b/sql/opt_sum.cc index 3ccc1e5cf41..8e7265ba1ad 100644 --- a/sql/opt_sum.cc +++ b/sql/opt_sum.cc @@ -744,8 +744,8 @@ static bool matching_cond(bool max_fl, TABLE_REF *ref, KEY *keyinfo, } else if (eq_type) { - if (!is_null && !cond->val_int() || - is_null && !test(part->field->is_null())) + if ((!is_null && !cond->val_int()) || + (is_null && !test(part->field->is_null()))) return 0; // Impossible test } else if (is_field_part) diff --git a/sql/parse_file.cc b/sql/parse_file.cc index f2dbeba1bbf..3d65fa1de31 100644 --- a/sql/parse_file.cc +++ b/sql/parse_file.cc @@ -231,7 +231,7 @@ sql_create_definition_file(const LEX_STRING *dir, const LEX_STRING *file_name, including dir name, file name itself, and an extension, and with unpack_filename() executed over it. */ - path_end= strxnmov(path, FN_REFLEN, file_name->str, NullS) - path; + path_end= strxnmov(path, sizeof(path) - 1, file_name->str, NullS) - path; } // temporary file name @@ -314,7 +314,7 @@ my_bool rename_in_schema_file(THD *thd, const char *schema, const char *old_name, const char *new_db, const char *new_name) { - char old_path[FN_REFLEN], new_path[FN_REFLEN], arc_path[FN_REFLEN]; + char old_path[FN_REFLEN + 1], new_path[FN_REFLEN + 1], arc_path[FN_REFLEN + 1]; build_table_filename(old_path, sizeof(old_path) - 1, schema, old_name, reg_ext, 0); diff --git a/sql/password.c b/sql/password.c index 1ff67888ea4..9204c660b77 100644 --- a/sql/password.c +++ b/sql/password.c @@ -137,20 +137,39 @@ void hash_password(ulong *result, const char *password, uint password_len) Create password to be stored in user database from raw string Used for pre-4.1 password handling SYNOPSIS - make_scrambled_password_323() + my_make_scrambled_password_323() to OUT store scrambled password here password IN user-supplied password + pass_len IN length of password string */ -void make_scrambled_password_323(char *to, const char *password) +void my_make_scrambled_password_323(char *to, const char *password, + size_t pass_len) { ulong hash_res[2]; - hash_password(hash_res, password, (uint) strlen(password)); + hash_password(hash_res, password, (uint) pass_len); sprintf(to, "%08lx%08lx", hash_res[0], hash_res[1]); } /* + Wrapper around my_make_scrambled_password_323() to maintain client lib ABI + compatibility. + In server code usage of my_make_scrambled_password_323() is preferred to + avoid strlen(). + SYNOPSIS + make_scrambled_password_323() + to OUT store scrambled password here + password IN NULL-terminated string with user-supplied password +*/ + +void make_scrambled_password_323(char *to, const char *password) +{ + my_make_scrambled_password_323(to, password, strlen(password)); +} + + +/* Scramble string with password. Used in pre 4.1 authentication phase. SYNOPSIS @@ -383,20 +402,21 @@ my_crypt(char *to, const uchar *s1, const uchar *s2, uint len) The result of this function is used as return value from PASSWORD() and is stored in the database. SYNOPSIS - make_scrambled_password() + my_make_scrambled_password() buf OUT buffer of size 2*SHA1_HASH_SIZE + 2 to store hex string - password IN NULL-terminated password string + password IN password string + pass_len IN length of password string */ -void -make_scrambled_password(char *to, const char *password) +void my_make_scrambled_password(char *to, const char *password, + size_t pass_len) { SHA1_CONTEXT sha1_context; uint8 hash_stage2[SHA1_HASH_SIZE]; mysql_sha1_reset(&sha1_context); /* stage 1: hash password */ - mysql_sha1_input(&sha1_context, (uint8 *) password, (uint) strlen(password)); + mysql_sha1_input(&sha1_context, (uint8 *) password, (uint) pass_len); mysql_sha1_result(&sha1_context, (uint8 *) to); /* stage 2: hash stage1 output */ mysql_sha1_reset(&sha1_context); @@ -410,6 +430,23 @@ make_scrambled_password(char *to, const char *password) /* + Wrapper around my_make_scrambled_password() to maintain client lib ABI + compatibility. + In server code usage of my_make_scrambled_password() is preferred to + avoid strlen(). + SYNOPSIS + make_scrambled_password() + buf OUT buffer of size 2*SHA1_HASH_SIZE + 2 to store hex string + password IN NULL-terminated password string +*/ + +void make_scrambled_password(char *to, const char *password) +{ + my_make_scrambled_password(to, password, strlen(password)); +} + + +/* Produce an obscure octet sequence from password and random string, recieved from the server. This sequence corresponds to the password, but password can not be easily restored from it. The sequence diff --git a/sql/protocol.cc b/sql/protocol.cc index 16975c68a54..4f69a0fdb52 100644 --- a/sql/protocol.cc +++ b/sql/protocol.cc @@ -29,11 +29,11 @@ static const unsigned int PACKET_BUFFER_EXTRA_ALLOC= 1024; /* Declared non-static only because of the embedded library. */ -void net_send_error_packet(THD *thd, uint sql_errno, const char *err); -void net_send_ok(THD *, uint, uint, ha_rows, ulonglong, const char *); -void net_send_eof(THD *thd, uint server_status, uint total_warn_count); +bool net_send_error_packet(THD *thd, uint sql_errno, const char *err); +bool net_send_ok(THD *, uint, uint, ha_rows, ulonglong, const char *); +bool net_send_eof(THD *thd, uint server_status, uint total_warn_count); #ifndef EMBEDDED_LIBRARY -static void write_eof_packet(THD *thd, NET *net, +static bool write_eof_packet(THD *thd, NET *net, uint server_status, uint total_warn_count); #endif @@ -70,8 +70,17 @@ bool Protocol_binary::net_store_data(const uchar *from, size_t length) For SIGNAL/RESIGNAL and GET DIAGNOSTICS functionality it's critical that every error that can be intercepted is issued in one place only, my_message_sql. + + @param thd Thread handler + @param sql_errno The error code to send + @param err A pointer to the error message + + @return + @retval FALSE The message was sent to the client + @retval TRUE An error occurred and the message wasn't sent properly */ -void net_send_error(THD *thd, uint sql_errno, const char *err) + +bool net_send_error(THD *thd, uint sql_errno, const char *err) { DBUG_ENTER("net_send_error"); @@ -80,6 +89,7 @@ void net_send_error(THD *thd, uint sql_errno, const char *err) DBUG_ASSERT(err && err[0]); DBUG_PRINT("enter",("sql_errno: %d err: %s", sql_errno, err)); + bool error; /* It's one case when we can push an error even though there @@ -90,11 +100,11 @@ void net_send_error(THD *thd, uint sql_errno, const char *err) /* Abort multi-result sets */ thd->server_status&= ~SERVER_MORE_RESULTS_EXISTS; - net_send_error_packet(thd, sql_errno, err); + error= net_send_error_packet(thd, sql_errno, err); thd->main_da.can_overwrite_status= FALSE; - DBUG_VOID_RETURN; + DBUG_RETURN(error); } /** @@ -113,25 +123,33 @@ void net_send_error(THD *thd, uint sql_errno, const char *err) Is not stored if no message. @param thd Thread handler + @param server_status The server status + @param total_warn_count Total number of warnings @param affected_rows Number of rows changed by statement @param id Auto_increment id for first row (if used) @param message Message to send to the client (Used by mysql_status) + + @return + @retval FALSE The message was successfully sent + @retval TRUE An error occurred and the messages wasn't sent properly + */ #ifndef EMBEDDED_LIBRARY -void +bool net_send_ok(THD *thd, uint server_status, uint total_warn_count, ha_rows affected_rows, ulonglong id, const char *message) { NET *net= &thd->net; uchar buff[MYSQL_ERRMSG_SIZE+10],*pos; + bool error= FALSE; DBUG_ENTER("my_ok"); if (! net->vio) // hack for re-parsing queries { DBUG_PRINT("info", ("vio present: NO")); - DBUG_VOID_RETURN; + DBUG_RETURN(FALSE); } buff[0]=0; // No fields @@ -162,13 +180,14 @@ net_send_ok(THD *thd, if (message && message[0]) pos= net_store_data(pos, (uchar*) message, strlen(message)); - VOID(my_net_write(net, buff, (size_t) (pos-buff))); - VOID(net_flush(net)); + error= my_net_write(net, buff, (size_t) (pos-buff)); + if (!error) + error= net_flush(net); thd->main_da.can_overwrite_status= FALSE; DBUG_PRINT("info", ("OK sent, so no more error sending allowed")); - DBUG_VOID_RETURN; + DBUG_RETURN(error); } static uchar eof_buff[1]= { (uchar) 254 }; /* Marker for end of fields */ @@ -188,37 +207,54 @@ static uchar eof_buff[1]= { (uchar) 254 }; /* Marker for end of fields */ client. @param thd Thread handler - @param no_flush Set to 1 if there will be more data to the client, - like in send_fields(). + @param server_status The server status + @param total_warn_count Total number of warnings + + @return + @retval FALSE The message was successfully sent + @retval TRUE An error occurred and the message wasn't sent properly */ -void +bool net_send_eof(THD *thd, uint server_status, uint total_warn_count) { NET *net= &thd->net; + bool error= FALSE; DBUG_ENTER("net_send_eof"); /* Set to TRUE if no active vio, to work well in case of --init-file */ if (net->vio != 0) { thd->main_da.can_overwrite_status= TRUE; - write_eof_packet(thd, net, server_status, total_warn_count); - VOID(net_flush(net)); + error= write_eof_packet(thd, net, server_status, total_warn_count); + if (!error) + error= net_flush(net); thd->main_da.can_overwrite_status= FALSE; DBUG_PRINT("info", ("EOF sent, so no more error sending allowed")); } - DBUG_VOID_RETURN; + DBUG_RETURN(error); } /** Format EOF packet according to the current protocol and write it to the network output buffer. + + @param thd The thread handler + @param net The network handler + @param server_status The server status + @param total_warn_count The number of warnings + + + @return + @retval FALSE The message was sent successfully + @retval TRUE An error occurred and the messages wasn't sent properly */ -static void write_eof_packet(THD *thd, NET *net, +static bool write_eof_packet(THD *thd, NET *net, uint server_status, uint total_warn_count) { + bool error; if (thd->client_capabilities & CLIENT_PROTOCOL_41) { uchar buff[5]; @@ -237,10 +273,12 @@ static void write_eof_packet(THD *thd, NET *net, if (thd->is_fatal_error) server_status&= ~SERVER_MORE_RESULTS_EXISTS; int2store(buff + 3, server_status); - VOID(my_net_write(net, buff, 5)); + error= my_net_write(net, buff, 5); } else - VOID(my_net_write(net, eof_buff, 1)); + error= my_net_write(net, eof_buff, 1); + + return error; } /** @@ -261,7 +299,17 @@ bool send_old_password_request(THD *thd) } -void net_send_error_packet(THD *thd, uint sql_errno, const char *err) +/** + @param thd Thread handler + @param sql_errno The error code to send + @param err A pointer to the error message + + @return + @retval FALSE The message was successfully sent + @retval TRUE An error occurred and the messages wasn't sent properly +*/ + +bool net_send_error_packet(THD *thd, uint sql_errno, const char *err) { NET *net= &thd->net; uint length; @@ -279,7 +327,7 @@ void net_send_error_packet(THD *thd, uint sql_errno, const char *err) /* In bootstrap it's ok to print on stderr */ fprintf(stderr,"ERROR: %d %s\n",sql_errno,err); } - DBUG_VOID_RETURN; + DBUG_RETURN(FALSE); } if (net->return_errno) @@ -301,9 +349,8 @@ void net_send_error_packet(THD *thd, uint sql_errno, const char *err) length=(uint) strlen(err); set_if_smaller(length,MYSQL_ERRMSG_SIZE-1); } - VOID(net_write_command(net,(uchar) 255, (uchar*) "", 0, (uchar*) err, + DBUG_RETURN(net_write_command(net,(uchar) 255, (uchar*) "", 0, (uchar*) err, length)); - DBUG_VOID_RETURN; } #endif /* EMBEDDED_LIBRARY */ @@ -389,36 +436,39 @@ void net_end_statement(THD *thd) if (thd->main_da.is_sent) return; + bool error= FALSE; + switch (thd->main_da.status()) { case Diagnostics_area::DA_ERROR: /* The query failed, send error to log and abort bootstrap. */ - net_send_error(thd, - thd->main_da.sql_errno(), - thd->main_da.message()); + error= net_send_error(thd, + thd->main_da.sql_errno(), + thd->main_da.message()); break; case Diagnostics_area::DA_EOF: - net_send_eof(thd, - thd->main_da.server_status(), - thd->main_da.total_warn_count()); + error= net_send_eof(thd, + thd->main_da.server_status(), + thd->main_da.total_warn_count()); break; case Diagnostics_area::DA_OK: - net_send_ok(thd, - thd->main_da.server_status(), - thd->main_da.total_warn_count(), - thd->main_da.affected_rows(), - thd->main_da.last_insert_id(), - thd->main_da.message()); + error= net_send_ok(thd, + thd->main_da.server_status(), + thd->main_da.total_warn_count(), + thd->main_da.affected_rows(), + thd->main_da.last_insert_id(), + thd->main_da.message()); break; case Diagnostics_area::DA_DISABLED: break; case Diagnostics_area::DA_EMPTY: default: DBUG_ASSERT(0); - net_send_ok(thd, thd->server_status, thd->total_warn_count, - 0, 0, NULL); + error= net_send_ok(thd, thd->server_status, thd->total_warn_count, + 0, 0, NULL); break; } - thd->main_da.is_sent= TRUE; + if (!error) + thd->main_da.is_sent= TRUE; } diff --git a/sql/protocol.h b/sql/protocol.h index a4770e9b6e3..251ba6fbc33 100644 --- a/sql/protocol.h +++ b/sql/protocol.h @@ -173,7 +173,7 @@ public: }; void send_warning(THD *thd, uint sql_errno, const char *err=0); -void net_send_error(THD *thd, uint sql_errno=0, const char *err=0); +bool net_send_error(THD *thd, uint sql_errno=0, const char *err=0); void net_end_statement(THD *thd); bool send_old_password_request(THD *thd); uchar *net_store_data(uchar *to,const uchar *from, size_t length); diff --git a/sql/rpl_reporting.cc b/sql/rpl_reporting.cc index 28f257790c7..a09140de3c4 100644 --- a/sql/rpl_reporting.cc +++ b/sql/rpl_reporting.cc @@ -13,6 +13,7 @@ Slave_reporting_capability::report(loglevel level, int err_code, va_list args; va_start(args, msg); + pthread_mutex_lock(&err_lock); switch (level) { case ERROR_LEVEL: @@ -38,6 +39,7 @@ Slave_reporting_capability::report(loglevel level, int err_code, my_vsnprintf(pbuff, pbuffsize, msg, args); + pthread_mutex_unlock(&err_lock); va_end(args); /* If the msg string ends with '.', do not add a ',' it would be ugly */ @@ -46,3 +48,8 @@ Slave_reporting_capability::report(loglevel level, int err_code, (pbuff[0] && *(strend(pbuff)-1) == '.') ? "" : ",", err_code); } + +Slave_reporting_capability::~Slave_reporting_capability() +{ + pthread_mutex_destroy(&err_lock); +} diff --git a/sql/rpl_reporting.h b/sql/rpl_reporting.h index 2e3fa3cea83..ce33407e516 100644 --- a/sql/rpl_reporting.h +++ b/sql/rpl_reporting.h @@ -16,6 +16,8 @@ class Slave_reporting_capability { public: + /** lock used to synchronize m_last_error on 'SHOW SLAVE STATUS' **/ + mutable pthread_mutex_t err_lock; /** Constructor. @@ -24,6 +26,7 @@ public: Slave_reporting_capability(char const *thread_name) : m_thread_name(thread_name) { + pthread_mutex_init(&err_lock, MY_MUTEX_INIT_FAST); } /** @@ -44,7 +47,9 @@ public: STATUS</code>. */ void clear_error() { + pthread_mutex_lock(&err_lock); m_last_error.clear(); + pthread_mutex_unlock(&err_lock); } /** @@ -72,6 +77,7 @@ public: Error const& last_error() const { return m_last_error; } + virtual ~Slave_reporting_capability()= 0; private: /** Last error produced by the I/O or SQL thread respectively. @@ -79,6 +85,10 @@ private: mutable Error m_last_error; char const *const m_thread_name; + + // not implemented + Slave_reporting_capability(const Slave_reporting_capability& rhs); + Slave_reporting_capability& operator=(const Slave_reporting_capability& rhs); }; #endif // RPL_REPORTING_H diff --git a/sql/rpl_rli.cc b/sql/rpl_rli.cc index 45c0efb10c2..18fbae9bb9d 100644 --- a/sql/rpl_rli.cc +++ b/sql/rpl_rli.cc @@ -947,6 +947,7 @@ int purge_relay_logs(Relay_log_info* rli, THD *thd, bool just_reset, if (count_relay_log_space(rli)) { *errmsg= "Error counting relay log space"; + error=1; goto err; } if (!just_reset) diff --git a/sql/set_var.cc b/sql/set_var.cc index bc8c91342e6..0b89333ce03 100644 --- a/sql/set_var.cc +++ b/sql/set_var.cc @@ -359,8 +359,8 @@ static sys_var_const sys_lower_case_table_names(&vars, &lower_case_table_names); static sys_var_thd_ulong_session_readonly sys_max_allowed_packet(&vars, "max_allowed_packet", &SV::max_allowed_packet); -static sys_var_long_ptr sys_max_binlog_cache_size(&vars, "max_binlog_cache_size", - &max_binlog_cache_size); +static sys_var_ulonglong_ptr sys_max_binlog_cache_size(&vars, "max_binlog_cache_size", + &max_binlog_cache_size); static sys_var_long_ptr sys_max_binlog_size(&vars, "max_binlog_size", &max_binlog_size, fix_max_binlog_size); diff --git a/sql/share/errmsg.txt b/sql/share/errmsg.txt index 2dfc5310643..5531ee71620 100644 --- a/sql/share/errmsg.txt +++ b/sql/share/errmsg.txt @@ -6177,3 +6177,32 @@ ER_TOO_LONG_TABLE_COMMENT ER_TOO_LONG_FIELD_COMMENT eng "Comment for field '%-.64s' is too long (max = %lu)" por "Comentário para o campo '%-.64s' é longo demais (max = %lu)" + +ER_FUNC_INEXISTENT_NAME_COLLISION 42000 + eng "FUNCTION %s does not exist. Check the 'Function Name Parsing and Resolution' section in the Reference Manual" + +# When updating these, please update EXPLAIN_FILENAME_MAX_EXTRA_LENGTH in +# mysql_priv.h with the new maximal additional length for explain_filename. +ER_DATABASE_NAME + eng "Database `%s`" + swe "Databas `%s`" +ER_TABLE_NAME + eng "Table `%s`" + swe "Tabell `%s`" +ER_PARTITION_NAME + eng "Partition `%s`" + swe "Partition `%s`" +ER_SUBPARTITION_NAME + eng "Subpartition `%s`" + swe "Subpartition `%s`" +ER_TEMPORARY_NAME + eng "Temporary" + swe "Temporär" +ER_RENAMED_NAME + eng "Renamed" + swe "Namnändrad" +ER_TOO_MANY_CONCURRENT_TRXS + eng "Too many active concurrent transactions" + +WARN_NON_ASCII_SEPARATOR_NOT_IMPLEMENTED + eng "Non-ASCII separator arguments are not fully supported" diff --git a/sql/slave.cc b/sql/slave.cc index c379a67201a..3b64e23ece5 100644 --- a/sql/slave.cc +++ b/sql/slave.cc @@ -38,6 +38,7 @@ #include <my_dir.h> #include <sql_common.h> #include <errmsg.h> +#include <mysqld_error.h> #include <mysys_err.h> #ifdef HAVE_REPLICATION @@ -511,7 +512,7 @@ terminate_slave_thread(THD *thd, int error; DBUG_PRINT("loop", ("killing slave thread")); - pthread_mutex_lock(&thd->LOCK_delete); + pthread_mutex_lock(&thd->LOCK_thd_data); #ifndef DONT_USE_THR_ALARM /* Error codes from pthread_kill are: @@ -522,7 +523,7 @@ terminate_slave_thread(THD *thd, DBUG_ASSERT(err != EINVAL); #endif thd->awake(THD::NOT_KILLED); - pthread_mutex_unlock(&thd->LOCK_delete); + pthread_mutex_unlock(&thd->LOCK_thd_data); /* There is a small chance that slave thread might miss the first @@ -668,7 +669,7 @@ static int end_slave_on_walk(Master_info* mi, uchar* /*unused*/) /* - Free all resources used by slave + Release slave threads at time of executing shutdown. SYNOPSIS end_slave() @@ -694,15 +695,32 @@ void end_slave() once multi-master code is ready. */ terminate_slave_threads(active_mi,SLAVE_FORCE_ALL); + } + pthread_mutex_unlock(&LOCK_active_mi); + DBUG_VOID_RETURN; +} + +/** + Free all resources used by slave threads at time of executing shutdown. + The routine must be called after all possible users of @c active_mi + have left. + + SYNOPSIS + close_active_mi() + +*/ +void close_active_mi() +{ + pthread_mutex_lock(&LOCK_active_mi); + if (active_mi) + { end_master_info(active_mi); delete active_mi; active_mi= 0; } pthread_mutex_unlock(&LOCK_active_mi); - DBUG_VOID_RETURN; } - static bool io_slave_killed(THD* thd, Master_info* mi) { DBUG_ENTER("io_slave_killed"); @@ -810,7 +828,7 @@ int init_strvar_from_file(char *var, int max_size, IO_CACHE *f, up to and including newline. */ int c; - while (((c=my_b_get(f)) != '\n' && c != my_b_EOF)); + while (((c=my_b_get(f)) != '\n' && c != my_b_EOF)) ; } DBUG_RETURN(0); } @@ -842,6 +860,29 @@ int init_intvar_from_file(int* var, IO_CACHE* f, int default_val) DBUG_RETURN(1); } + +/* + Check if the error is caused by network. + @param[in] errorno Number of the error. + RETURNS: + TRUE network error + FALSE not network error +*/ + +bool is_network_error(uint errorno) +{ + if (errorno == CR_CONNECTION_ERROR || + errorno == CR_CONN_HOST_ERROR || + errorno == CR_SERVER_GONE_ERROR || + errorno == CR_SERVER_LOST || + errorno == ER_CON_COUNT_ERROR || + errorno == ER_SERVER_SHUTDOWN) + return TRUE; + + return FALSE; +} + + /* Note that we rely on the master's version (3.23, 4.0.14 etc) instead of relying on the binlog's version. This is not perfect: imagine an upgrade @@ -854,6 +895,7 @@ int init_intvar_from_file(int* var, IO_CACHE* f, int default_val) RETURNS 0 ok 1 error + 2 transient network problem, the caller should try to reconnect */ static int get_master_version_and_clock(MYSQL* mysql, Master_info* mi) @@ -939,6 +981,8 @@ static int get_master_version_and_clock(MYSQL* mysql, Master_info* mi) unavailable (very old master not supporting UNIX_TIMESTAMP()?). */ + DBUG_SYNC_POINT("debug_lock.before_get_UNIX_TIMESTAMP", 10); + master_res= NULL; if (!mysql_real_query(mysql, STRING_WITH_LEN("SELECT UNIX_TIMESTAMP()")) && (master_res= mysql_store_result(mysql)) && (master_row= mysql_fetch_row(master_res))) @@ -946,7 +990,13 @@ static int get_master_version_and_clock(MYSQL* mysql, Master_info* mi) mi->clock_diff_with_master= (long) (time((time_t*) 0) - strtoul(master_row[0], 0, 10)); } - else if (!check_io_slave_killed(mi->io_thd, mi, NULL)) + else if (is_network_error(mysql_errno(mysql))) + { + mi->report(WARNING_LEVEL, mysql_errno(mysql), + "Get master clock failed with error: %s", mysql_error(mysql)); + goto network_err; + } + else { mi->clock_diff_with_master= 0; /* The "most sensible" value */ sql_print_warning("\"SELECT UNIX_TIMESTAMP()\" failed on master, " @@ -955,7 +1005,10 @@ static int get_master_version_and_clock(MYSQL* mysql, Master_info* mi) mysql_error(mysql), mysql_errno(mysql)); } if (master_res) + { mysql_free_result(master_res); + master_res= NULL; + } /* Check that the master's server id and ours are different. Because if they @@ -967,12 +1020,15 @@ static int get_master_version_and_clock(MYSQL* mysql, Master_info* mi) Note: we could have put a @@SERVER_ID in the previous SELECT UNIX_TIMESTAMP() instead, but this would not have worked on 3.23 masters. */ + DBUG_SYNC_POINT("debug_lock.before_get_SERVER_ID", 10); + master_res= NULL; + master_row= NULL; if (!mysql_real_query(mysql, STRING_WITH_LEN("SHOW VARIABLES LIKE 'SERVER_ID'")) && - (master_res= mysql_store_result(mysql))) + (master_res= mysql_store_result(mysql)) && + (master_row= mysql_fetch_row(master_res))) { - if ((master_row= mysql_fetch_row(master_res)) && - (::server_id == strtoul(master_row[1], 0, 10)) && + if ((::server_id == strtoul(master_row[1], 0, 10)) && !mi->rli.replicate_same_server_id) { errmsg= "The slave I/O thread stops because master and slave have equal \ @@ -981,10 +1037,34 @@ the --replicate-same-server-id option must be used on slave but this does \ not always make sense; please check the manual before using it)."; err_code= ER_SLAVE_FATAL_ERROR; sprintf(err_buff, ER(err_code), errmsg); + goto err; } + } + else if (mysql_errno(mysql)) + { + if (is_network_error(mysql_errno(mysql))) + { + mi->report(WARNING_LEVEL, mysql_errno(mysql), + "Get master SERVER_ID failed with error: %s", mysql_error(mysql)); + goto network_err; + } + /* Fatal error */ + errmsg= "The slave I/O thread stops because a fatal error is encountered \ +when it try to get the value of SERVER_ID variable from master."; + err_code= mysql_errno(mysql); + sprintf(err_buff, "%s Error: %s", errmsg, mysql_error(mysql)); + goto err; + } + else if (!master_row && master_res) + { + mi->report(WARNING_LEVEL, ER_UNKNOWN_SYSTEM_VARIABLE, + "Unknown system variable 'SERVER_ID' on master, \ +maybe it is a *VERY OLD MASTER*."); + } + if (master_res) + { mysql_free_result(master_res); - if (errmsg) - goto err; + master_res= NULL; } /* @@ -1008,23 +1088,50 @@ not always make sense; please check the manual before using it)."; if (*mysql->server_version == '3') goto err; - if ((*mysql->server_version == '4') && - !mysql_real_query(mysql, - STRING_WITH_LEN("SELECT @@GLOBAL.COLLATION_SERVER")) && - (master_res= mysql_store_result(mysql))) + if (*mysql->server_version == '4') { - if ((master_row= mysql_fetch_row(master_res)) && - strcmp(master_row[0], global_system_variables.collation_server->name)) + master_res= NULL; + if (!mysql_real_query(mysql, + STRING_WITH_LEN("SELECT @@GLOBAL.COLLATION_SERVER")) && + (master_res= mysql_store_result(mysql)) && + (master_row= mysql_fetch_row(master_res))) { - errmsg= "The slave I/O thread stops because master and slave have \ + if (strcmp(master_row[0], global_system_variables.collation_server->name)) + { + errmsg= "The slave I/O thread stops because master and slave have \ different values for the COLLATION_SERVER global variable. The values must \ -be equal for replication to work"; - err_code= ER_SLAVE_FATAL_ERROR; - sprintf(err_buff, ER(err_code), errmsg); +be equal for the Statement-format replication to work"; + err_code= ER_SLAVE_FATAL_ERROR; + sprintf(err_buff, ER(err_code), errmsg); + goto err; + } } - mysql_free_result(master_res); - if (errmsg) + else if (is_network_error(mysql_errno(mysql))) + { + mi->report(WARNING_LEVEL, mysql_errno(mysql), + "Get master COLLATION_SERVER failed with error: %s", mysql_error(mysql)); + goto network_err; + } + else if (mysql_errno(mysql) != ER_UNKNOWN_SYSTEM_VARIABLE) + { + /* Fatal error */ + errmsg= "The slave I/O thread stops because a fatal error is encountered \ +when it try to get the value of COLLATION_SERVER global variable from master."; + err_code= mysql_errno(mysql); + sprintf(err_buff, "%s Error: %s", errmsg, mysql_error(mysql)); goto err; + } + else + mi->report(WARNING_LEVEL, ER_UNKNOWN_SYSTEM_VARIABLE, + "Unknown system variable 'COLLATION_SERVER' on master, \ +maybe it is a *VERY OLD MASTER*. *NOTE*: slave may experience \ +inconsistency if replicated data deals with collation."); + + if (master_res) + { + mysql_free_result(master_res); + master_res= NULL; + } } /* @@ -1042,35 +1149,62 @@ be equal for replication to work"; This check is only necessary for 4.x masters (and < 5.0.4 masters but those were alpha). */ - if ((*mysql->server_version == '4') && - !mysql_real_query(mysql, STRING_WITH_LEN("SELECT @@GLOBAL.TIME_ZONE")) && - (master_res= mysql_store_result(mysql))) + if (*mysql->server_version == '4') { - if ((master_row= mysql_fetch_row(master_res)) && - strcmp(master_row[0], - global_system_variables.time_zone->get_name()->ptr())) + master_res= NULL; + if (!mysql_real_query(mysql, STRING_WITH_LEN("SELECT @@GLOBAL.TIME_ZONE")) && + (master_res= mysql_store_result(mysql)) && + (master_row= mysql_fetch_row(master_res))) { - errmsg= "The slave I/O thread stops because master and slave have \ + if (strcmp(master_row[0], + global_system_variables.time_zone->get_name()->ptr())) + { + errmsg= "The slave I/O thread stops because master and slave have \ different values for the TIME_ZONE global variable. The values must \ -be equal for replication to work"; - err_code= ER_SLAVE_FATAL_ERROR; - sprintf(err_buff, ER(err_code), errmsg); +be equal for the Statement-format replication to work"; + err_code= ER_SLAVE_FATAL_ERROR; + sprintf(err_buff, ER(err_code), errmsg); + goto err; + } } - mysql_free_result(master_res); - - if (errmsg) + else if (is_network_error(mysql_errno(mysql))) + { + mi->report(WARNING_LEVEL, mysql_errno(mysql), + "Get master TIME_ZONE failed with error: %s", mysql_error(mysql)); + goto network_err; + } + else + { + /* Fatal error */ + errmsg= "The slave I/O thread stops because a fatal error is encountered \ +when it try to get the value of TIME_ZONE global variable from master."; + err_code= mysql_errno(mysql); + sprintf(err_buff, "%s Error: %s", errmsg, mysql_error(mysql)); goto err; + } + if (master_res) + { + mysql_free_result(master_res); + master_res= NULL; + } } err: if (errmsg) { + if (master_res) + mysql_free_result(master_res); DBUG_ASSERT(err_code != 0); mi->report(ERROR_LEVEL, err_code, err_buff); DBUG_RETURN(1); } DBUG_RETURN(0); + +network_err: + if (master_res) + mysql_free_result(master_res); + DBUG_RETURN(2); } /* @@ -1116,15 +1250,13 @@ static int create_table_from_dump(THD* thd, MYSQL *mysql, const char* db, DBUG_RETURN(1); } thd->command = COM_TABLE_DUMP; - thd->query_length= packet_len; - /* Note that we should not set thd->query until the area is initalized */ if (!(query = thd->strmake((char*) net->read_pos, packet_len))) { sql_print_error("create_table_from_dump: out of memory"); my_message(ER_GET_ERRNO, "Out of memory", MYF(0)); DBUG_RETURN(1); } - thd->query= query; + thd->set_query(query, packet_len); thd->is_slave_error = 0; bzero((char*) &tables,sizeof(tables)); @@ -1493,6 +1625,8 @@ bool show_master_info(THD* thd, Master_info* mi) pthread_mutex_lock(&mi->data_lock); pthread_mutex_lock(&mi->rli.data_lock); + pthread_mutex_lock(&mi->err_lock); + pthread_mutex_lock(&mi->rli.err_lock); protocol->store(mi->host, &my_charset_bin); protocol->store(mi->user, &my_charset_bin); protocol->store((uint32) mi->port); @@ -1592,6 +1726,8 @@ bool show_master_info(THD* thd, Master_info* mi) // Last_SQL_Error protocol->store(mi->rli.last_error().message, &my_charset_bin); + pthread_mutex_unlock(&mi->rli.err_lock); + pthread_mutex_unlock(&mi->err_lock); pthread_mutex_unlock(&mi->rli.data_lock); pthread_mutex_unlock(&mi->data_lock); @@ -1863,25 +1999,6 @@ static ulong read_event(MYSQL* mysql, Master_info *mi, bool* suppress_warnings) DBUG_RETURN(len - 1); } - -int check_expected_error(THD* thd, Relay_log_info const *rli, - int expected_error) -{ - DBUG_ENTER("check_expected_error"); - - switch (expected_error) { - case ER_NET_READ_ERROR: - case ER_NET_ERROR_ON_WRITE: - case ER_QUERY_INTERRUPTED: - case ER_SERVER_SHUTDOWN: - case ER_NEW_ABORTING_CONNECTION: - DBUG_RETURN(1); - default: - DBUG_RETURN(0); - } -} - - /* Check if the current error is of temporary nature of not. Some errors are temporary in nature, such as @@ -2235,7 +2352,7 @@ static int exec_relay_log_event(THD* thd, Relay_log_info* rli) "the slave_transaction_retries variable.", slave_trans_retries); } - else if (exec_res && !temp_err || + else if ((exec_res && !temp_err) || (opt_using_transactions && rli->group_relay_log_pos == rli->event_relay_log_pos)) { @@ -2372,6 +2489,7 @@ pthread_handler_t handle_slave_io(void *arg) char llbuff[22]; uint retry_count; bool suppress_warnings; + int ret; #ifndef DBUG_OFF uint retry_count_reg= 0, retry_count_dump= 0, retry_count_event= 0; #endif @@ -2397,6 +2515,7 @@ pthread_handler_t handle_slave_io(void *arg) pthread_detach_this_thread(); thd->thread_stack= (char*) &thd; // remember where our stack is + mi->clear_error(); if (init_slave_thread(thd, SLAVE_THD_IO)) { pthread_cond_broadcast(&mi->start_cond); @@ -2451,8 +2570,23 @@ connected: mi->slave_running= MYSQL_SLAVE_RUN_CONNECT; thd->slave_net = &mysql->net; thd_proc_info(thd, "Checking master version"); - if (get_master_version_and_clock(mysql, mi)) + ret= get_master_version_and_clock(mysql, mi); + if (ret == 1) + /* Fatal error */ goto err; + + if (ret == 2) + { + if (check_io_slave_killed(mi->io_thd, mi, "Slave I/O thread killed" + "while calling get_master_version_and_clock(...)")) + goto err; + suppress_warnings= FALSE; + /* Try to reconnect because the error was caused by a transient network problem */ + if (try_to_reconnect(thd, mysql, mi, &retry_count, suppress_warnings, + reconnect_messages[SLAVE_RECON_ACT_REG])) + goto err; + goto connected; + } if (mi->rli.relay_log.description_event_for_queue->binlog_version > 1) { @@ -2511,6 +2645,7 @@ requesting master dump") || goto connected; }); + DBUG_ASSERT(mi->last_error().number == 0); while (!io_slave_killed(thd,mi)) { ulong event_len; @@ -2618,10 +2753,8 @@ err: // print the current replication position sql_print_information("Slave I/O thread exiting, read up to log '%s', position %s", IO_RPL_LOG_NAME, llstr(mi->master_log_pos,llbuff)); - VOID(pthread_mutex_lock(&LOCK_thread_count)); - thd->query = thd->db = 0; // extra safety - thd->query_length= thd->db_length= 0; - VOID(pthread_mutex_unlock(&LOCK_thread_count)); + thd->set_query(NULL, 0); + thd->reset_db(NULL, 0); if (mysql) { /* @@ -2973,15 +3106,14 @@ the slave SQL thread with \"SLAVE START\". We stopped at log \ must "proactively" clear playgrounds: */ rli->cleanup_context(thd, 1); - VOID(pthread_mutex_lock(&LOCK_thread_count)); /* Some extra safety, which should not been needed (normally, event deletion should already have done these assignments (each event which sets these variables is supposed to set them to 0 before terminating)). */ - thd->query= thd->db= thd->catalog= 0; - thd->query_length= thd->db_length= 0; - VOID(pthread_mutex_unlock(&LOCK_thread_count)); + thd->catalog= 0; + thd->set_query(NULL, 0); + thd->reset_db(NULL, 0); thd_proc_info(thd, "Waiting for slave mutex on exit"); pthread_mutex_lock(&rli->run_lock); /* We need data_lock, at least to wake up any waiting master_pos_wait() */ @@ -3710,6 +3842,7 @@ static int connect_to_master(THD* thd, MYSQL* mysql, Master_info* mi, if (!slave_was_killed) { + mi->clear_error(); // clear possible left over reconnect error if (reconnect) { if (!suppress_warnings && global_system_variables.log_warnings) diff --git a/sql/slave.h b/sql/slave.h index abd63315e62..a44a7eed83e 100644 --- a/sql/slave.h +++ b/sql/slave.h @@ -171,10 +171,10 @@ bool rpl_master_has_bug(const Relay_log_info *rli, uint bug_id, bool report, bool rpl_master_erroneous_autoinc(THD* thd); const char *print_slave_db_safe(const char *db); -int check_expected_error(THD* thd, Relay_log_info const *rli, int error_code); void skip_load_data_infile(NET* net); -void end_slave(); /* clean up */ +void end_slave(); /* release slave threads */ +void close_active_mi(); /* clean up slave threads data */ void clear_until_condition(Relay_log_info* rli); void clear_slave_error(Relay_log_info* rli); void end_relay_log_info(Relay_log_info* rli); diff --git a/sql/sp.cc b/sql/sp.cc index 8c8149d0afc..4d840f53e2f 100644 --- a/sql/sp.cc +++ b/sql/sp.cc @@ -70,9 +70,6 @@ enum MYSQL_PROC_FIELD_COUNT }; -/* Tells what SP_DEFAULT_ACCESS should be mapped to */ -#define SP_DEFAULT_ACCESS_MAPPING SP_CONTAINS_SQL - /*************************************************************************/ /** @@ -941,7 +938,7 @@ sp_create_routine(THD *thd, int type, sp_head *sp) /* Such a statement can always go directly to binlog, no trans cache */ thd->binlog_query(THD::MYSQL_QUERY_TYPE, log_query.c_ptr(), log_query.length(), - FALSE, FALSE, THD::NOT_KILLED); + FALSE, FALSE, 0); thd->variables.sql_mode= 0; } @@ -1308,13 +1305,20 @@ sp_find_routine(THD *thd, int type, sp_name *name, sp_cache **cp, /** This is used by sql_acl.cc:mysql_routine_grant() and is used to find the routines in 'routines'. + + @param thd Thread handler + @param routines List of needles in the hay stack + @param any Any of the needles are good enough + + @return + @retval FALSE Found. + @retval TRUE Not found */ -int -sp_exist_routines(THD *thd, TABLE_LIST *routines, bool any, bool no_error) +bool +sp_exist_routines(THD *thd, TABLE_LIST *routines, bool any) { TABLE_LIST *routine; - bool result= 0; bool sp_object_found; DBUG_ENTER("sp_exists_routine"); for (routine= routines; routine; routine= routine->next_global) @@ -1336,21 +1340,16 @@ sp_exist_routines(THD *thd, TABLE_LIST *routines, bool any, bool no_error) if (sp_object_found) { if (any) - DBUG_RETURN(1); - result= 1; + break; } else if (!any) { - if (!no_error) - { - my_error(ER_SP_DOES_NOT_EXIST, MYF(0), "FUNCTION or PROCEDURE", - routine->table_name); - DBUG_RETURN(-1); - } - DBUG_RETURN(0); + my_error(ER_SP_DOES_NOT_EXIST, MYF(0), "FUNCTION or PROCEDURE", + routine->table_name); + DBUG_RETURN(TRUE); } } - DBUG_RETURN(result); + DBUG_RETURN(FALSE); } @@ -17,6 +17,9 @@ #ifndef _SP_H_ #define _SP_H_ +/* Tells what SP_DEFAULT_ACCESS should be mapped to */ +#define SP_DEFAULT_ACCESS_MAPPING SP_CONTAINS_SQL + // Return codes from sp_create_*, sp_drop_*, and sp_show_*: #define SP_OK 0 #define SP_KEY_NOT_FOUND -1 @@ -39,8 +42,8 @@ sp_head * sp_find_routine(THD *thd, int type, sp_name *name, sp_cache **cp, bool cache_only); -int -sp_exist_routines(THD *thd, TABLE_LIST *procs, bool any, bool no_error); +bool +sp_exist_routines(THD *thd, TABLE_LIST *procs, bool any); int sp_routine_exists_in_table(THD *thd, int type, sp_name *name); diff --git a/sql/sp_head.cc b/sql/sp_head.cc index 1dba8a45926..0736e5fc2a8 100644 --- a/sql/sp_head.cc +++ b/sql/sp_head.cc @@ -1012,8 +1012,7 @@ subst_spvars(THD *thd, sp_instr *instr, LEX_STRING *query_str) else DBUG_RETURN(TRUE); - thd->query= pbuf; - thd->query_length= qbuf.length(); + thd->set_query(pbuf, qbuf.length()); DBUG_RETURN(FALSE); } @@ -1249,7 +1248,7 @@ sp_head::execute(THD *thd) */ if (thd->prelocked_mode == NON_PRELOCKED) thd->user_var_events_alloc= thd->mem_root; - + err_status= i->execute(thd, &ip); if (i->free_list) @@ -1779,8 +1778,9 @@ sp_head::execute_function(THD *thd, Item **argp, uint argcount, thd->options= binlog_save_options; if (thd->binlog_evt_union.unioned_events) { + int errcode = query_error_code(thd, thd->killed == THD::NOT_KILLED); Query_log_event qinfo(thd, binlog_buf.ptr(), binlog_buf.length(), - thd->binlog_evt_union.unioned_events_trans, FALSE); + thd->binlog_evt_union.unioned_events_trans, FALSE, errcode); if (mysql_bin_log.write(&qinfo) && thd->binlog_evt_union.unioned_events_trans) { @@ -2857,14 +2857,13 @@ sp_instr_stmt::execute(THD *thd, uint *nextp) } else *nextp= m_ip+1; - thd->query= query; - thd->query_length= query_length; + thd->set_query(query, query_length); thd->query_name_consts= 0; if (!thd->is_error()) thd->main_da.reset_diagnostics_area(); } - DBUG_RETURN(res); + DBUG_RETURN(res || thd->is_error()); } diff --git a/sql/spatial.h b/sql/spatial.h index dbf5da6665b..86c2ed8c197 100644 --- a/sql/spatial.h +++ b/sql/spatial.h @@ -116,12 +116,12 @@ struct MBR int touches(const MBR *mbr) { /* The following should be safe, even if we compare doubles */ - return ((((mbr->xmin == xmax) || (mbr->xmax == xmin)) && - (((mbr->ymin >= ymin) && (mbr->ymin <= ymax)) || - ((mbr->ymax >= ymin) && (mbr->ymax <= ymax)))) || - (((mbr->ymin == ymax) || (mbr->ymax == ymin)) && - (((mbr->xmin >= xmin) && (mbr->xmin <= xmax)) || - ((mbr->xmax >= xmin) && (mbr->xmax <= xmax))))); + return ((mbr->xmin == xmax || mbr->xmax == xmin) && + ((mbr->ymin >= ymin && mbr->ymin <= ymax) || + (mbr->ymax >= ymin && mbr->ymax <= ymax))) || + ((mbr->ymin == ymax || mbr->ymax == ymin) && + ((mbr->xmin >= xmin && mbr->xmin <= xmax) || + (mbr->xmax >= xmin && mbr->xmax <= xmax))); } int within(const MBR *mbr) diff --git a/sql/sql_acl.cc b/sql/sql_acl.cc index 4d4e4d24684..ab18a2d1d04 100644 --- a/sql/sql_acl.cc +++ b/sql/sql_acl.cc @@ -1192,12 +1192,12 @@ static void acl_update_user(const char *user, const char *host, for (uint i=0 ; i < acl_users.elements ; i++) { ACL_USER *acl_user=dynamic_element(&acl_users,i,ACL_USER*); - if (!acl_user->user && !user[0] || - acl_user->user && !strcmp(user,acl_user->user)) + if ((!acl_user->user && !user[0]) || + (acl_user->user && !strcmp(user,acl_user->user))) { - if (!acl_user->host.hostname && !host[0] || - acl_user->host.hostname && - !my_strcasecmp(system_charset_info, host, acl_user->host.hostname)) + if ((!acl_user->host.hostname && !host[0]) || + (acl_user->host.hostname && + !my_strcasecmp(system_charset_info, host, acl_user->host.hostname))) { acl_user->access=privileges; if (mqh->specified_limits & USER_RESOURCES::QUERIES_PER_HOUR) @@ -1275,16 +1275,16 @@ static void acl_update_db(const char *user, const char *host, const char *db, for (uint i=0 ; i < acl_dbs.elements ; i++) { ACL_DB *acl_db=dynamic_element(&acl_dbs,i,ACL_DB*); - if (!acl_db->user && !user[0] || - acl_db->user && - !strcmp(user,acl_db->user)) + if ((!acl_db->user && !user[0]) || + (acl_db->user && + !strcmp(user,acl_db->user))) { - if (!acl_db->host.hostname && !host[0] || - acl_db->host.hostname && - !strcmp(host, acl_db->host.hostname)) + if ((!acl_db->host.hostname && !host[0]) || + (acl_db->host.hostname && + !strcmp(host, acl_db->host.hostname))) { - if (!acl_db->db && !db[0] || - acl_db->db && !strcmp(db,acl_db->db)) + if ((!acl_db->db && !db[0]) || + (acl_db->db && !strcmp(db,acl_db->db))) { if (privileges) acl_db->access=privileges; @@ -1493,8 +1493,8 @@ bool acl_check_host(const char *host, const char *ip) return 0; VOID(pthread_mutex_lock(&acl_cache->lock)); - if (host && hash_search(&acl_check_hosts,(uchar*) host,strlen(host)) || - ip && hash_search(&acl_check_hosts,(uchar*) ip, strlen(ip))) + if ((host && hash_search(&acl_check_hosts,(uchar*) host,strlen(host))) || + (ip && hash_search(&acl_check_hosts,(uchar*) ip, strlen(ip)))) { VOID(pthread_mutex_unlock(&acl_cache->lock)); return 0; // Found host @@ -1655,7 +1655,7 @@ bool change_password(THD *thd, const char *host, const char *user, new_password)); thd->clear_error(); thd->binlog_query(THD::MYSQL_QUERY_TYPE, buff, query_length, - FALSE, FALSE, THD::NOT_KILLED); + FALSE, FALSE, 0); } end: close_thread_tables(thd); @@ -1711,8 +1711,8 @@ find_acl_user(const char *host, const char *user, my_bool exact) host, acl_user->host.hostname ? acl_user->host.hostname : "")); - if (!acl_user->user && !user[0] || - acl_user->user && !strcmp(user,acl_user->user)) + if ((!acl_user->user && !user[0]) || + (acl_user->user && !strcmp(user,acl_user->user))) { if (exact ? !my_strcasecmp(system_charset_info, host, acl_user->host.hostname ? @@ -2995,8 +2995,8 @@ int mysql_table_grant(THD *thd, TABLE_LIST *table_list, { if (!(rights & CREATE_ACL)) { - char buf[FN_REFLEN]; - build_table_filename(buf, sizeof(buf), table_list->db, + char buf[FN_REFLEN + 1]; + build_table_filename(buf, sizeof(buf) - 1, table_list->db, table_list->table_name, reg_ext, 0); fn_format(buf, buf, "", "", MY_UNPACK_FILENAME | MY_RESOLVE_SYMLINKS | MY_RETURN_REAL_PATH | MY_APPEND_EXT); @@ -3198,26 +3198,24 @@ int mysql_table_grant(THD *thd, TABLE_LIST *table_list, } -/* +/** Store routine level grants in the privilege tables - SYNOPSIS - mysql_routine_grant() - thd Thread handle - table_list List of routines to give grant - is_proc true indicates routine list are procedures - user_list List of users to give grant - rights Table level grant - revoke_grant Set to 1 if this is a REVOKE command + @param thd Thread handle + @param table_list List of routines to give grant + @param is_proc Is this a list of procedures? + @param user_list List of users to give grant + @param rights Table level grant + @param revoke_grant Is this is a REVOKE command? - RETURN - 0 ok - 1 error + @return + @retval FALSE Success. + @retval TRUE An error occurred. */ bool mysql_routine_grant(THD *thd, TABLE_LIST *table_list, bool is_proc, List <LEX_USER> &user_list, ulong rights, - bool revoke_grant, bool no_error) + bool revoke_grant, bool write_to_binlog) { List_iterator <LEX_USER> str_list (user_list); LEX_USER *Str, *tmp_Str; @@ -3228,22 +3226,20 @@ bool mysql_routine_grant(THD *thd, TABLE_LIST *table_list, bool is_proc, if (!initialized) { - if (!no_error) - my_error(ER_OPTION_PREVENTS_STATEMENT, MYF(0), - "--skip-grant-tables"); + my_error(ER_OPTION_PREVENTS_STATEMENT, MYF(0), + "--skip-grant-tables"); DBUG_RETURN(TRUE); } if (rights & ~PROC_ACLS) { - if (!no_error) - my_message(ER_ILLEGAL_GRANT_FOR_TABLE, ER(ER_ILLEGAL_GRANT_FOR_TABLE), - MYF(0)); + my_message(ER_ILLEGAL_GRANT_FOR_TABLE, ER(ER_ILLEGAL_GRANT_FOR_TABLE), + MYF(0)); DBUG_RETURN(TRUE); } if (!revoke_grant) { - if (sp_exist_routines(thd, table_list, is_proc, no_error)<0) + if (sp_exist_routines(thd, table_list, is_proc)) DBUG_RETURN(TRUE); } @@ -3324,9 +3320,8 @@ bool mysql_routine_grant(THD *thd, TABLE_LIST *table_list, bool is_proc, { if (revoke_grant) { - if (!no_error) - my_error(ER_NONEXISTING_PROC_GRANT, MYF(0), - Str->user.str, Str->host.str, table_name); + my_error(ER_NONEXISTING_PROC_GRANT, MYF(0), + Str->user.str, Str->host.str, table_name); result= TRUE; continue; } @@ -3351,16 +3346,14 @@ bool mysql_routine_grant(THD *thd, TABLE_LIST *table_list, bool is_proc, } thd->mem_root= old_root; pthread_mutex_unlock(&acl_cache->lock); - if (!result && !no_error) + + if (write_to_binlog) { write_bin_log(thd, TRUE, thd->query, thd->query_length); } rw_unlock(&LOCK_grant); - if (!result && !no_error) - my_ok(thd); - /* Tables are automatically closed */ DBUG_RETURN(result); } @@ -5326,16 +5319,13 @@ static int handle_grant_struct(uint struct_no, bool drop, uint elements; const char *user; const char *host; - ACL_USER *acl_user; - ACL_DB *acl_db; - GRANT_NAME *grant_name; + ACL_USER *acl_user= NULL; + ACL_DB *acl_db= NULL; + GRANT_NAME *grant_name= NULL; DBUG_ENTER("handle_grant_struct"); DBUG_PRINT("info",("scan struct: %u search: '%s'@'%s'", struct_no, user_from->user.str, user_from->host.str)); - LINT_INIT(acl_user); - LINT_INIT(acl_db); - LINT_INIT(grant_name); LINT_INIT(user); LINT_INIT(host); @@ -5703,6 +5693,7 @@ bool mysql_drop_user(THD *thd, List <LEX_USER> &list) List_iterator <LEX_USER> user_list(list); TABLE_LIST tables[GRANT_TABLES]; bool some_users_deleted= FALSE; + ulong old_sql_mode= thd->variables.sql_mode; DBUG_ENTER("mysql_drop_user"); /* @@ -5716,6 +5707,8 @@ bool mysql_drop_user(THD *thd, List <LEX_USER> &list) if ((result= open_grant_tables(thd, tables))) DBUG_RETURN(result != 1); + thd->variables.sql_mode&= ~MODE_PAD_CHAR_TO_FULL_LENGTH; + rw_wrlock(&LOCK_grant); VOID(pthread_mutex_lock(&acl_cache->lock)); @@ -5748,6 +5741,7 @@ bool mysql_drop_user(THD *thd, List <LEX_USER> &list) rw_unlock(&LOCK_grant); close_thread_tables(thd); + thd->variables.sql_mode= old_sql_mode; DBUG_RETURN(result); } @@ -6157,21 +6151,20 @@ bool sp_revoke_privileges(THD *thd, const char *sp_db, const char *sp_name, } -/* +/** Grant EXECUTE,ALTER privilege for a stored procedure - SYNOPSIS - sp_grant_privileges() - thd The current thread. - db DB of the stored procedure - name Name of the stored procedure + @param thd The current thread. + @param sp_db + @param sp_name + @param is_proc - RETURN - 0 OK. - < 0 Error. Error message not yet sent. + @return + @retval FALSE Success + @retval TRUE An error occured. Error message not yet sent. */ -int sp_grant_privileges(THD *thd, const char *sp_db, const char *sp_name, +bool sp_grant_privileges(THD *thd, const char *sp_db, const char *sp_name, bool is_proc) { Security_context *sctx= thd->security_ctx; @@ -6181,6 +6174,7 @@ int sp_grant_privileges(THD *thd, const char *sp_db, const char *sp_name, bool result; ACL_USER *au; char passwd_buff[SCRAMBLED_PASSWORD_CHAR_LENGTH+1]; + Dummy_error_handler error_handler; DBUG_ENTER("sp_grant_privileges"); if (!(combo=(LEX_USER*) thd->alloc(sizeof(st_lex_user)))) @@ -6231,8 +6225,11 @@ int sp_grant_privileges(THD *thd, const char *sp_db, const char *sp_name, } else { - my_error(ER_PASSWD_LENGTH, MYF(0), SCRAMBLED_PASSWORD_CHAR_LENGTH); - return -1; + push_warning_printf(thd, MYSQL_ERROR::WARN_LEVEL_WARN, + ER_PASSWD_LENGTH, + ER(ER_PASSWD_LENGTH), + SCRAMBLED_PASSWORD_CHAR_LENGTH); + return TRUE; } combo->password.str= passwd_buff; } @@ -6246,10 +6243,17 @@ int sp_grant_privileges(THD *thd, const char *sp_db, const char *sp_name, DBUG_RETURN(TRUE); thd->lex->ssl_type= SSL_TYPE_NOT_SPECIFIED; + thd->lex->ssl_cipher= thd->lex->x509_subject= thd->lex->x509_issuer= 0; bzero((char*) &thd->lex->mqh, sizeof(thd->lex->mqh)); + /* + Only care about whether the operation failed or succeeded + as all errors will be handled later. + */ + thd->push_internal_handler(&error_handler); result= mysql_routine_grant(thd, tables, is_proc, user_list, - DEFAULT_CREATE_PROC_ACLS, 0, 1); + DEFAULT_CREATE_PROC_ACLS, FALSE, FALSE); + thd->pop_internal_handler(); DBUG_RETURN(result); } diff --git a/sql/sql_acl.h b/sql/sql_acl.h index 9ae17a4bf02..a8090fba2e7 100644 --- a/sql/sql_acl.h +++ b/sql/sql_acl.h @@ -233,7 +233,7 @@ int mysql_table_grant(THD *thd, TABLE_LIST *table, List <LEX_USER> &user_list, bool revoke); bool mysql_routine_grant(THD *thd, TABLE_LIST *table, bool is_proc, List <LEX_USER> &user_list, ulong rights, - bool revoke, bool no_error); + bool revoke, bool write_to_binlog); my_bool grant_init(); void grant_free(void); my_bool grant_reload(THD *thd); @@ -264,7 +264,7 @@ void fill_effective_table_privileges(THD *thd, GRANT_INFO *grant, const char *db, const char *table); bool sp_revoke_privileges(THD *thd, const char *sp_db, const char *sp_name, bool is_proc); -int sp_grant_privileges(THD *thd, const char *sp_db, const char *sp_name, +bool sp_grant_privileges(THD *thd, const char *sp_db, const char *sp_name, bool is_proc); bool check_routine_level_acl(THD *thd, const char *db, const char *name, bool is_proc); diff --git a/sql/sql_analyse.cc b/sql/sql_analyse.cc index 9ca6e0a0a2b..d273b3319ee 100644 --- a/sql/sql_analyse.cc +++ b/sql/sql_analyse.cc @@ -246,7 +246,7 @@ bool test_if_number(NUM_INFO *info, const char *str, uint str_len) } DBUG_RETURN(0); } - for (str++; *(end - 1) == '0'; end--); // jump over zeros at the end + for (str++; *(end - 1) == '0'; end--) ; // jump over zeros at the end if (str == end) // number was something like '123.000' { char *endpos= (char*) str; diff --git a/sql/sql_base.cc b/sql/sql_base.cc index d4d813d0fbd..b81070000b3 100644 --- a/sql/sql_base.cc +++ b/sql/sql_base.cc @@ -473,13 +473,14 @@ static TABLE_SHARE @todo Rework alternative ways to deal with ER_NO_SUCH TABLE. */ - if (share || thd->is_error() && thd->main_da.sql_errno() != ER_NO_SUCH_TABLE) + if (share || (thd->is_error() && thd->main_da.sql_errno() != ER_NO_SUCH_TABLE)) DBUG_RETURN(share); /* Table didn't exist. Check if some engine can provide it */ - if ((tmp= ha_create_table_from_engine(thd, table_list->db, - table_list->table_name)) < 0) + tmp= ha_create_table_from_engine(thd, table_list->db, + table_list->table_name); + if (tmp < 0) { /* No such table in any engine. @@ -1431,11 +1432,10 @@ static inline uint tmpkeyval(THD *thd, TABLE *table) void close_temporary_tables(THD *thd) { TABLE *table; - TABLE *next; + TABLE *next= NULL; TABLE *prev_table; /* Assume thd->options has OPTION_QUOTE_SHOW_CREATE */ bool was_quote_show= TRUE; - LINT_INIT(next); if (!thd->temporary_tables) return; @@ -1541,7 +1541,7 @@ void close_temporary_tables(THD *thd) thd->variables.character_set_client= system_charset_info; Query_log_event qinfo(thd, s_query.ptr(), s_query.length() - 1 /* to remove trailing ',' */, - 0, FALSE, THD::NOT_KILLED); + 0, FALSE, 0); thd->variables.character_set_client= cs_save; mysql_bin_log.write(&qinfo); thd->variables.pseudo_thread_id= save_pseudo_thread_id; @@ -2432,7 +2432,7 @@ bool lock_table_name_if_not_cached(THD *thd, const char *db, bool check_if_table_exists(THD *thd, TABLE_LIST *table, bool *exists) { - char path[FN_REFLEN]; + char path[FN_REFLEN + 1]; int rc; DBUG_ENTER("check_if_table_exists"); @@ -2617,8 +2617,8 @@ TABLE *open_table(THD *thd, TABLE_LIST *table_list, MEM_ROOT *mem_root, distance > 0 - we have lock mode higher then we require distance == 0 - we have lock mode exactly which we need */ - if (best_distance < 0 && distance > best_distance || - distance >= 0 && distance < best_distance) + if ((best_distance < 0 && distance > best_distance) || + (distance >= 0 && distance < best_distance)) { best_distance= distance; best_table= table; @@ -2649,7 +2649,7 @@ TABLE *open_table(THD *thd, TABLE_LIST *table_list, MEM_ROOT *mem_root, real fix will be made after definition cache will be made) */ { - char path[FN_REFLEN]; + char path[FN_REFLEN + 1]; enum legacy_db_type not_used; build_table_filename(path, sizeof(path) - 1, table_list->db, table_list->table_name, reg_ext, 0); @@ -2963,6 +2963,7 @@ TABLE *open_table(THD *thd, TABLE_LIST *table_list, MEM_ROOT *mem_root, table->insert_values= 0; table->fulltext_searched= 0; table->file->ft_handler= 0; + table->reginfo.impossible_range= 0; /* Catch wrong handling of the auto_increment_field_not_null. */ DBUG_ASSERT(!table->auto_increment_field_not_null); table->auto_increment_field_not_null= FALSE; @@ -4015,9 +4016,10 @@ retry: /* this DELETE FROM is needed even with row-based binlogging */ end = strxmov(strmov(query, "DELETE FROM `"), share->db.str,"`.`",share->table_name.str,"`", NullS); + int errcode= query_error_code(thd, TRUE); thd->binlog_query(THD::STMT_QUERY_TYPE, query, (ulong)(end-query), - FALSE, FALSE, THD::NOT_KILLED); + FALSE, FALSE, errcode); my_free(query, MYF(0)); } else @@ -6339,7 +6341,7 @@ find_field_in_tables(THD *thd, Item_ident *item, (report_error == REPORT_ALL_ERRORS || report_error == REPORT_EXCEPT_NON_UNIQUE)) { - char buff[NAME_LEN*2+1]; + char buff[NAME_LEN*2 + 2]; if (db && db[0]) { strxnmov(buff,sizeof(buff)-1,db,".",table_name,NullS); @@ -6412,8 +6414,7 @@ find_item_in_list(Item *find, List<Item> &items, uint *counter, (and not an item that happens to have a name). */ bool is_ref_by_name= 0; - uint unaliased_counter; - LINT_INIT(unaliased_counter); // Dependent on found_unaliased + uint unaliased_counter= 0; *resolution= NOT_RESOLVED; @@ -7374,7 +7375,13 @@ int setup_wild(THD *thd, TABLE_LIST *tables, List<Item> &fields, /* make * substituting permanent */ SELECT_LEX *select_lex= thd->lex->current_select; select_lex->with_wild= 0; - select_lex->item_list= fields; + /* + The assignment below is translated to memcpy() call (at least on some + platforms). memcpy() expects that source and destination areas do not + overlap. That problem was detected by valgrind. + */ + if (&select_lex->item_list != &fields) + select_lex->item_list= fields; thd->restore_active_arena(arena, &backup); } @@ -7438,7 +7445,7 @@ bool setup_fields(THD *thd, Item **ref_pointer_array, thd->lex->current_select->cur_pos_in_select_list= 0; while ((item= it++)) { - if (!item->fixed && item->fix_fields(thd, it.ref()) || + if ((!item->fixed && item->fix_fields(thd, it.ref())) || (item= *(it.ref()))->check_cols(1)) { thd->lex->current_select->is_item_list_lookup= save_is_item_list_lookup; @@ -7752,8 +7759,8 @@ insert_fields(THD *thd, Name_resolution_context *context, const char *db_name, DBUG_ASSERT(tables->is_leaf_for_name_resolution()); - if (table_name && my_strcasecmp(table_alias_charset, table_name, - tables->alias) || + if ((table_name && my_strcasecmp(table_alias_charset, table_name, + tables->alias)) || (db_name && strcmp(tables->db,db_name))) continue; @@ -7784,8 +7791,8 @@ insert_fields(THD *thd, Name_resolution_context *context, const char *db_name, information_schema table, or a nested table reference. See the comment for TABLE_LIST. */ - if (!(table && !tables->view && (table->grant.privilege & SELECT_ACL) || - tables->view && (tables->grant.privilege & SELECT_ACL)) && + if (!((table && !tables->view && (table->grant.privilege & SELECT_ACL)) || + (tables->view && (tables->grant.privilege & SELECT_ACL))) && !any_privileges) { field_iterator.set(tables); @@ -7839,7 +7846,7 @@ insert_fields(THD *thd, Name_resolution_context *context, const char *db_name, */ if (any_privileges) { - DBUG_ASSERT(tables->field_translation == NULL && table || + DBUG_ASSERT((tables->field_translation == NULL && table) || tables->is_natural_join); DBUG_ASSERT(item->type() == Item::FIELD_ITEM); Item_field *fld= (Item_field*) item; @@ -7978,7 +7985,7 @@ int setup_conds(THD *thd, TABLE_LIST *tables, TABLE_LIST *leaves, if (*conds) { thd->where="where clause"; - if (!(*conds)->fixed && (*conds)->fix_fields(thd, conds) || + if ((!(*conds)->fixed && (*conds)->fix_fields(thd, conds)) || (*conds)->check_cols(1)) goto err_no_arena; } @@ -7998,8 +8005,8 @@ int setup_conds(THD *thd, TABLE_LIST *tables, TABLE_LIST *leaves, { /* Make a join an a expression */ thd->where="on clause"; - if (!embedded->on_expr->fixed && - embedded->on_expr->fix_fields(thd, &embedded->on_expr) || + if ((!embedded->on_expr->fixed && + embedded->on_expr->fix_fields(thd, &embedded->on_expr)) || embedded->on_expr->check_cols(1)) goto err_no_arena; select_lex->cond_count++; @@ -8154,8 +8161,8 @@ fill_record_n_invoke_before_triggers(THD *thd, List<Item> &fields, enum trg_event_type event) { return (fill_record(thd, fields, values, ignore_errors) || - triggers && triggers->process_triggers(thd, event, - TRG_ACTION_BEFORE, TRUE)); + (triggers && triggers->process_triggers(thd, event, + TRG_ACTION_BEFORE, TRUE))); } @@ -8249,8 +8256,8 @@ fill_record_n_invoke_before_triggers(THD *thd, Field **ptr, enum trg_event_type event) { return (fill_record(thd, ptr, values, ignore_errors) || - triggers && triggers->process_triggers(thd, event, - TRG_ACTION_BEFORE, TRUE)); + (triggers && triggers->process_triggers(thd, event, + TRG_ACTION_BEFORE, TRUE))); } diff --git a/sql/sql_cache.cc b/sql/sql_cache.cc index 32faeae9dcc..df19fd05417 100644 --- a/sql/sql_cache.cc +++ b/sql/sql_cache.cc @@ -352,11 +352,6 @@ TODO list: #define RW_UNLOCK(M) {DBUG_PRINT("lock", ("rwlock unlock 0x%lx",(ulong)(M))); \ if (!rw_unlock(M)) DBUG_PRINT("lock", ("rwlock unlock ok")); \ else DBUG_PRINT("lock", ("rwlock unlock FAILED %d", errno)); } -#define STRUCT_LOCK(M) {DBUG_PRINT("lock", ("%d struct lock...",__LINE__)); \ - pthread_mutex_lock(M);DBUG_PRINT("lock", ("struct lock OK"));} -#define STRUCT_UNLOCK(M) { \ - DBUG_PRINT("lock", ("%d struct unlock...",__LINE__)); \ - pthread_mutex_unlock(M);DBUG_PRINT("lock", ("struct unlock OK"));} #define BLOCK_LOCK_WR(B) {DBUG_PRINT("lock", ("%d LOCK_WR 0x%lx",\ __LINE__,(ulong)(B))); \ B->query()->lock_writing();} @@ -403,8 +398,6 @@ static void debug_wait_for_kill(const char *info) #define RW_WLOCK(M) rw_wrlock(M) #define RW_RLOCK(M) rw_rdlock(M) #define RW_UNLOCK(M) rw_unlock(M) -#define STRUCT_LOCK(M) pthread_mutex_lock(M) -#define STRUCT_UNLOCK(M) pthread_mutex_unlock(M) #define BLOCK_LOCK_WR(B) B->query()->lock_writing() #define BLOCK_LOCK_RD(B) B->query()->lock_reading() #define BLOCK_UNLOCK_WR(B) B->query()->unlock_writing() @@ -420,6 +413,140 @@ TYPELIB query_cache_type_typelib= /** + Serialize access to the query cache. + If the lock cannot be granted the thread hangs in a conditional wait which + is signalled on each unlock. + + The lock attempt will also fail without wait if lock_and_suspend() is in + effect by another thread. This enables a quick path in execution to skip waits + when the outcome is known. + + @return + @retval FALSE An exclusive lock was taken + @retval TRUE The locking attempt failed +*/ + +bool Query_cache::try_lock(void) +{ + bool interrupt= FALSE; + DBUG_ENTER("Query_cache::try_lock"); + + pthread_mutex_lock(&structure_guard_mutex); + while (1) + { + if (m_cache_lock_status == Query_cache::UNLOCKED) + { + m_cache_lock_status= Query_cache::LOCKED; +#ifndef DBUG_OFF + THD *thd= current_thd; + if (thd) + m_cache_lock_thread_id= thd->thread_id; +#endif + break; + } + else if (m_cache_lock_status == Query_cache::LOCKED_NO_WAIT) + { + /* + If query cache is protected by a LOCKED_NO_WAIT lock this thread + should avoid using the query cache as it is being evicted. + */ + interrupt= TRUE; + break; + } + else + { + DBUG_ASSERT(m_cache_lock_status == Query_cache::LOCKED); + pthread_cond_wait(&COND_cache_status_changed, &structure_guard_mutex); + } + } + pthread_mutex_unlock(&structure_guard_mutex); + + DBUG_RETURN(interrupt); +} + + +/** + Serialize access to the query cache. + If the lock cannot be granted the thread hangs in a conditional wait which + is signalled on each unlock. + + This method also suspends the query cache so that other threads attempting to + lock the cache with try_lock() will fail directly without waiting. + + It is used by all methods which flushes or destroys the whole cache. + */ + +void Query_cache::lock_and_suspend(void) +{ + DBUG_ENTER("Query_cache::lock_and_suspend"); + + pthread_mutex_lock(&structure_guard_mutex); + while (m_cache_lock_status != Query_cache::UNLOCKED) + pthread_cond_wait(&COND_cache_status_changed, &structure_guard_mutex); + m_cache_lock_status= Query_cache::LOCKED_NO_WAIT; +#ifndef DBUG_OFF + THD *thd= current_thd; + if (thd) + m_cache_lock_thread_id= thd->thread_id; +#endif + /* Wake up everybody, a whole cache flush is starting! */ + pthread_cond_broadcast(&COND_cache_status_changed); + pthread_mutex_unlock(&structure_guard_mutex); + + DBUG_VOID_RETURN; +} + +/** + Serialize access to the query cache. + If the lock cannot be granted the thread hangs in a conditional wait which + is signalled on each unlock. + + It is used by all methods which invalidates one or more tables. + */ + +void Query_cache::lock(void) +{ + DBUG_ENTER("Query_cache::lock"); + + pthread_mutex_lock(&structure_guard_mutex); + while (m_cache_lock_status != Query_cache::UNLOCKED) + pthread_cond_wait(&COND_cache_status_changed, &structure_guard_mutex); + m_cache_lock_status= Query_cache::LOCKED; +#ifndef DBUG_OFF + THD *thd= current_thd; + if (thd) + m_cache_lock_thread_id= thd->thread_id; +#endif + pthread_mutex_unlock(&structure_guard_mutex); + + DBUG_VOID_RETURN; +} + + +/** + Set the query cache to UNLOCKED and signal waiting threads. +*/ + +void Query_cache::unlock(void) +{ + DBUG_ENTER("Query_cache::unlock"); + pthread_mutex_lock(&structure_guard_mutex); +#ifndef DBUG_OFF + THD *thd= current_thd; + if (thd) + DBUG_ASSERT(m_cache_lock_thread_id == thd->thread_id); +#endif + DBUG_ASSERT(m_cache_lock_status == Query_cache::LOCKED || + m_cache_lock_status == Query_cache::LOCKED_NO_WAIT); + m_cache_lock_status= Query_cache::UNLOCKED; + DBUG_PRINT("Query_cache",("Sending signal")); + pthread_cond_signal(&COND_cache_status_changed); + pthread_mutex_unlock(&structure_guard_mutex); + DBUG_VOID_RETURN; +} + + +/** Helper function for determine if a SELECT statement has a SQL_NO_CACHE directive. @@ -713,14 +840,8 @@ void query_cache_insert(NET *net, const char *packet, ulong length) DBUG_EXECUTE_IF("wait_in_query_cache_insert", debug_wait_for_kill("wait_in_query_cache_insert"); ); - STRUCT_LOCK(&query_cache.structure_guard_mutex); - bool interrupt; - query_cache.wait_while_table_flush_is_in_progress(&interrupt); - if (interrupt) - { - STRUCT_UNLOCK(&query_cache.structure_guard_mutex); + if (query_cache.try_lock()) DBUG_VOID_RETURN; - } Query_cache_block *query_block= (Query_cache_block*)net->query_cache_query; if (!query_block) @@ -729,7 +850,7 @@ void query_cache_insert(NET *net, const char *packet, ulong length) We lost the writer and the currently processed query has been invalidated; there is nothing left to do. */ - STRUCT_UNLOCK(&query_cache.structure_guard_mutex); + query_cache.unlock(); DBUG_VOID_RETURN; } @@ -755,7 +876,7 @@ void query_cache_insert(NET *net, const char *packet, ulong length) query_cache.free_query(query_block); query_cache.refused++; // append_result_data no success => we need unlock - STRUCT_UNLOCK(&query_cache.structure_guard_mutex); + query_cache.unlock(); DBUG_VOID_RETURN; } @@ -777,14 +898,8 @@ void query_cache_abort(NET *net) if (net->query_cache_query == 0) DBUG_VOID_RETURN; - STRUCT_LOCK(&query_cache.structure_guard_mutex); - bool interrupt; - query_cache.wait_while_table_flush_is_in_progress(&interrupt); - if (interrupt) - { - STRUCT_UNLOCK(&query_cache.structure_guard_mutex); + if (query_cache.try_lock()) DBUG_VOID_RETURN; - } /* While we were waiting another thread might have changed the status @@ -803,8 +918,7 @@ void query_cache_abort(NET *net) DBUG_EXECUTE("check_querycache",query_cache.check_integrity(1);); } - STRUCT_UNLOCK(&query_cache.structure_guard_mutex); - + query_cache.unlock(); DBUG_VOID_RETURN; } @@ -832,15 +946,8 @@ void query_cache_end_of_result(THD *thd) emb_count_querycache_size(thd)); #endif - STRUCT_LOCK(&query_cache.structure_guard_mutex); - - bool interrupt; - query_cache.wait_while_table_flush_is_in_progress(&interrupt); - if (interrupt) - { - STRUCT_UNLOCK(&query_cache.structure_guard_mutex); + if (query_cache.try_lock()) DBUG_VOID_RETURN; - } query_block= ((Query_cache_block*) thd->net.query_cache_query); if (query_block) @@ -869,10 +976,9 @@ void query_cache_end_of_result(THD *thd) */ DBUG_ASSERT(0); query_cache.free_query(query_block); - STRUCT_UNLOCK(&query_cache.structure_guard_mutex); + query_cache.unlock(); DBUG_VOID_RETURN; } - last_result_block= header->result()->prev; allign_size= ALIGN_SIZE(last_result_block->used); len= max(query_cache.min_allocation_unit, allign_size); @@ -885,13 +991,11 @@ void query_cache_end_of_result(THD *thd) /* Drop the writer. */ header->writer(0); thd->net.query_cache_query= 0; - BLOCK_UNLOCK_WR(query_block); DBUG_EXECUTE("check_querycache",query_cache.check_integrity(1);); } - - STRUCT_UNLOCK(&query_cache.structure_guard_mutex); + query_cache.unlock(); DBUG_VOID_RETURN; } @@ -950,11 +1054,7 @@ ulong Query_cache::resize(ulong query_cache_size_arg) query_cache_size_arg)); DBUG_ASSERT(initialized); - STRUCT_LOCK(&structure_guard_mutex); - while (is_flushing()) - pthread_cond_wait(&COND_cache_status_changed, &structure_guard_mutex); - m_cache_status= Query_cache::FLUSH_IN_PROGRESS; - STRUCT_UNLOCK(&structure_guard_mutex); + lock_and_suspend(); /* Wait for all readers and writers to exit. When the list of all queries @@ -986,13 +1086,10 @@ ulong Query_cache::resize(ulong query_cache_size_arg) query_cache_size= query_cache_size_arg; new_query_cache_size= init_cache(); - STRUCT_LOCK(&structure_guard_mutex); - m_cache_status= Query_cache::NO_FLUSH_IN_PROGRESS; - pthread_cond_signal(&COND_cache_status_changed); if (new_query_cache_size) DBUG_EXECUTE("check_querycache",check_integrity(1);); - STRUCT_UNLOCK(&structure_guard_mutex); + unlock(); DBUG_RETURN(new_query_cache_size); } @@ -1089,15 +1186,16 @@ def_week_frmt: %lu, in_trans: %d, autocommit: %d", */ ha_release_temporary_latches(thd); - STRUCT_LOCK(&structure_guard_mutex); - if (query_cache_size == 0 || is_flushing()) + /* + A table- or a full flush operation can potentially take a long time to + finish. We choose not to wait for them and skip caching statements + instead. + */ + if (try_lock()) + DBUG_VOID_RETURN; + if (query_cache_size == 0) { - /* - A table- or a full flush operation can potentially take a long time to - finish. We choose not to wait for them and skip caching statements - instead. - */ - STRUCT_UNLOCK(&structure_guard_mutex); + unlock(); DBUG_VOID_RETURN; } DUMP(this); @@ -1105,7 +1203,7 @@ def_week_frmt: %lu, in_trans: %d, autocommit: %d", if (ask_handler_allowance(thd, tables_used)) { refused++; - STRUCT_UNLOCK(&structure_guard_mutex); + unlock(); DBUG_VOID_RETURN; } @@ -1153,7 +1251,7 @@ def_week_frmt: %lu, in_trans: %d, autocommit: %d", DBUG_PRINT("qcache", ("insertion in query hash")); header->unlock_n_destroy(); free_memory_block(query_block); - STRUCT_UNLOCK(&structure_guard_mutex); + unlock(); goto end; } if (!register_all_tables(query_block, tables_used, local_tables)) @@ -1163,7 +1261,7 @@ def_week_frmt: %lu, in_trans: %d, autocommit: %d", hash_delete(&queries, (uchar *) query_block); header->unlock_n_destroy(); free_memory_block(query_block); - STRUCT_UNLOCK(&structure_guard_mutex); + unlock(); goto end; } double_linked_list_simple_include(query_block, &queries_blocks); @@ -1173,7 +1271,7 @@ def_week_frmt: %lu, in_trans: %d, autocommit: %d", header->writer(net); header->tables_type(tables_type); - STRUCT_UNLOCK(&structure_guard_mutex); + unlock(); // init_n_lock make query block locked BLOCK_UNLOCK_WR(query_block); @@ -1182,7 +1280,7 @@ def_week_frmt: %lu, in_trans: %d, autocommit: %d", { // We have not enough memory to store query => do nothing refused++; - STRUCT_UNLOCK(&structure_guard_mutex); + unlock(); DBUG_PRINT("warning", ("Can't allocate query")); } } @@ -1190,7 +1288,7 @@ def_week_frmt: %lu, in_trans: %d, autocommit: %d", { // Another thread is processing the same query => do nothing refused++; - STRUCT_UNLOCK(&structure_guard_mutex); + unlock(); DBUG_PRINT("qcache", ("Another thread process same query")); } } @@ -1282,18 +1380,17 @@ Query_cache::send_result_to_client(THD *thd, char *sql, uint query_length) } } - STRUCT_LOCK(&structure_guard_mutex); + /* + Try to obtain an exclusive lock on the query cache. If the cache is + disabled or if a full cache flush is in progress, the attempt to + get the lock is aborted. + */ + if (try_lock()) + goto err; if (query_cache_size == 0) goto err_unlock; - if (is_flushing()) - { - /* Return; Query cache is temporarily disabled while we flush. */ - DBUG_PRINT("qcache",("query cache disabled")); - goto err_unlock; - } - /* Check that we haven't forgot to reset the query cache variables; make sure there are no attached query cache writer to this thread. @@ -1427,7 +1524,7 @@ def_week_frmt: %lu, in_trans: %d, autocommit: %d", DBUG_PRINT("qcache", ("Temporary table detected: '%s.%s'", table_list.db, table_list.alias)); - STRUCT_UNLOCK(&structure_guard_mutex); + unlock(); /* We should not store result of this query because it contain temporary tables => assign following variable to make check @@ -1448,7 +1545,7 @@ def_week_frmt: %lu, in_trans: %d, autocommit: %d", DBUG_PRINT("qcache", ("probably no SELECT access to %s.%s => return to normal processing", table_list.db, table_list.alias)); - STRUCT_UNLOCK(&structure_guard_mutex); + unlock(); thd->lex->safe_to_cache_query=0; // Don't try to cache this BLOCK_UNLOCK_RD(query_block); DBUG_RETURN(-1); // Privilege error @@ -1491,7 +1588,7 @@ def_week_frmt: %lu, in_trans: %d, autocommit: %d", } move_to_query_list_end(query_block); hits++; - STRUCT_UNLOCK(&structure_guard_mutex); + unlock(); /* Send cached result to client @@ -1530,7 +1627,7 @@ def_week_frmt: %lu, in_trans: %d, autocommit: %d", DBUG_RETURN(1); // Result sent to client err_unlock: - STRUCT_UNLOCK(&structure_guard_mutex); + unlock(); err: DBUG_RETURN(0); // Query was not cached } @@ -1651,47 +1748,6 @@ void Query_cache::invalidate(THD *thd, const char *key, uint32 key_length, /** - Synchronize the thread with any flushing operations. - - This helper function is called whenever a thread needs to operate on the - query cache structure (example: during invalidation). If a table flush is in - progress this function will wait for it to stop. If a full flush is in - progress, the function will set the interrupt parameter to indicate that the - current operation is redundant and should be interrupted. - - @param[out] interrupt This out-parameter will be set to TRUE if the calling - function is redundant and should be interrupted. - - @return If the interrupt-parameter is TRUE then m_cache_status is set to - NO_FLUSH_IN_PROGRESS. If the interrupt-parameter is FALSE then - m_cache_status is set to FLUSH_IN_PROGRESS. - The structure_guard_mutex will in any case be locked. -*/ - -void Query_cache::wait_while_table_flush_is_in_progress(bool *interrupt) -{ - while (is_flushing()) - { - /* - If there already is a full flush in progress query cache isn't enabled - and additional flushes are redundant; just return instead. - */ - if (m_cache_status == Query_cache::FLUSH_IN_PROGRESS) - { - *interrupt= TRUE; - return; - } - /* - If a table flush is in progress; wait on cache status to change. - */ - if (m_cache_status == Query_cache::TABLE_FLUSH_IN_PROGRESS) - pthread_cond_wait(&COND_cache_status_changed, &structure_guard_mutex); - } - *interrupt= FALSE; -} - - -/** Remove all cached queries that uses the given database. */ @@ -1700,14 +1756,11 @@ void Query_cache::invalidate(char *db) bool restart= FALSE; DBUG_ENTER("Query_cache::invalidate (db)"); - STRUCT_LOCK(&structure_guard_mutex); - bool interrupt; - wait_while_table_flush_is_in_progress(&interrupt); - if (interrupt) - { - STRUCT_UNLOCK(&structure_guard_mutex); - return; - } + /* + Lock the query cache and queue all invalidation attempts to avoid + the risk of a race between invalidation, cache inserts and flushes. + */ + lock(); THD *thd= current_thd; @@ -1763,7 +1816,7 @@ void Query_cache::invalidate(char *db) } while (restart); } // end if( tables_blocks ) } - STRUCT_UNLOCK(&structure_guard_mutex); + unlock(); DBUG_VOID_RETURN; } @@ -1787,7 +1840,10 @@ void Query_cache::invalidate_by_MyISAM_filename(const char *filename) void Query_cache::flush() { DBUG_ENTER("Query_cache::flush"); - STRUCT_LOCK(&structure_guard_mutex); + DBUG_EXECUTE_IF("wait_in_query_cache_flush1", + debug_wait_for_kill("wait_in_query_cache_flush1");); + + lock_and_suspend(); if (query_cache_size > 0) { DUMP(this); @@ -1796,7 +1852,7 @@ void Query_cache::flush() } DBUG_EXECUTE("check_querycache",query_cache.check_integrity(1);); - STRUCT_UNLOCK(&structure_guard_mutex); + unlock(); DBUG_VOID_RETURN; } @@ -1815,18 +1871,16 @@ void Query_cache::pack(ulong join_limit, uint iteration_limit) { DBUG_ENTER("Query_cache::pack"); - bool interrupt; - STRUCT_LOCK(&structure_guard_mutex); - wait_while_table_flush_is_in_progress(&interrupt); - if (interrupt) - { - STRUCT_UNLOCK(&structure_guard_mutex); + /* + If the entire qc is being invalidated we can bail out early + instead of waiting for the lock. + */ + if (try_lock()) DBUG_VOID_RETURN; - } if (query_cache_size == 0) { - STRUCT_UNLOCK(&structure_guard_mutex); + unlock(); DBUG_VOID_RETURN; } @@ -1836,7 +1890,7 @@ void Query_cache::pack(ulong join_limit, uint iteration_limit) pack_cache(); } while ((++i < iteration_limit) && join_results(join_limit)); - STRUCT_UNLOCK(&structure_guard_mutex); + unlock(); DBUG_VOID_RETURN; } @@ -1851,9 +1905,9 @@ void Query_cache::destroy() else { /* Underlying code expects the lock. */ - STRUCT_LOCK(&structure_guard_mutex); + lock_and_suspend(); free_cache(); - STRUCT_UNLOCK(&structure_guard_mutex); + unlock(); pthread_cond_destroy(&COND_cache_status_changed); pthread_mutex_destroy(&structure_guard_mutex); @@ -1872,7 +1926,7 @@ void Query_cache::init() DBUG_ENTER("Query_cache::init"); pthread_mutex_init(&structure_guard_mutex,MY_MUTEX_INIT_FAST); pthread_cond_init(&COND_cache_status_changed, NULL); - m_cache_status= Query_cache::NO_FLUSH_IN_PROGRESS; + m_cache_lock_status= Query_cache::UNLOCKED; initialized = 1; DBUG_VOID_RETURN; } @@ -2112,23 +2166,9 @@ void Query_cache::free_cache() void Query_cache::flush_cache() { - /* - If there is flush in progress, wait for it to finish, and then do - our flush. This is necessary because something could be added to - the cache before we acquire the lock again, and some code (like - Query_cache::free_cache()) depends on the fact that after the - flush the cache is empty. - */ - while (is_flushing()) - pthread_cond_wait(&COND_cache_status_changed, &structure_guard_mutex); - - /* - Setting 'FLUSH_IN_PROGRESS' will prevent other threads from using - the cache while we are in the middle of the flush, and we release - the lock so that other threads won't block. - */ - m_cache_status= Query_cache::FLUSH_IN_PROGRESS; - STRUCT_UNLOCK(&structure_guard_mutex); + + DBUG_EXECUTE_IF("wait_in_query_cache_flush2", + debug_wait_for_kill("wait_in_query_cache_flush2");); my_hash_reset(&queries); while (queries_blocks != 0) @@ -2136,10 +2176,6 @@ void Query_cache::flush_cache() BLOCK_LOCK_WR(queries_blocks); free_query_internal(queries_blocks); } - - STRUCT_LOCK(&structure_guard_mutex); - m_cache_status= Query_cache::NO_FLUSH_IN_PROGRESS; - pthread_cond_signal(&COND_cache_status_changed); } /* @@ -2319,10 +2355,6 @@ Query_cache::write_block_data(ulong data_len, uchar* data, } -/* - On success STRUCT_UNLOCK(&query_cache.structure_guard_mutex) will be done. -*/ - my_bool Query_cache::append_result_data(Query_cache_block **current_block, ulong data_len, uchar* data, @@ -2342,10 +2374,6 @@ Query_cache::append_result_data(Query_cache_block **current_block, if (*current_block == 0) { DBUG_PRINT("qcache", ("allocated first result data block %lu", data_len)); - /* - STRUCT_UNLOCK(&structure_guard_mutex) Will be done by - write_result_data if success; - */ DBUG_RETURN(write_result_data(current_block, data_len, data, query_block, Query_cache_block::RES_BEG)); } @@ -2376,10 +2404,6 @@ Query_cache::append_result_data(Query_cache_block **current_block, DBUG_PRINT("qcache", ("allocate new block for %lu bytes", data_len-last_block_free_space)); Query_cache_block *new_block = 0; - /* - On success STRUCT_UNLOCK(&structure_guard_mutex) will be done - by the next call - */ success = write_result_data(&new_block, data_len-last_block_free_space, (uchar*)(((uchar*)data)+last_block_free_space), query_block, @@ -2394,7 +2418,7 @@ Query_cache::append_result_data(Query_cache_block **current_block, else { // It is success (nobody can prevent us write data) - STRUCT_UNLOCK(&structure_guard_mutex); + unlock(); } // Now finally write data to the last block @@ -2432,7 +2456,7 @@ my_bool Query_cache::write_result_data(Query_cache_block **result_block, if (success) { // It is success (nobody can prevent us write data) - STRUCT_UNLOCK(&structure_guard_mutex); + unlock(); uint headers_len = (ALIGN_SIZE(sizeof(Query_cache_block)) + ALIGN_SIZE(sizeof(Query_cache_result))); #ifndef EMBEDDED_LIBRARY @@ -2590,36 +2614,23 @@ void Query_cache::invalidate_table(THD *thd, TABLE *table) void Query_cache::invalidate_table(THD *thd, uchar * key, uint32 key_length) { - bool interrupt; - STRUCT_LOCK(&structure_guard_mutex); - wait_while_table_flush_is_in_progress(&interrupt); - if (interrupt) - { - STRUCT_UNLOCK(&structure_guard_mutex); - return; - } + DBUG_EXECUTE_IF("wait_in_query_cache_invalidate1", + debug_wait_for_kill("wait_in_query_cache_invalidate1"); ); /* - Setting 'TABLE_FLUSH_IN_PROGRESS' will temporarily disable the cache - so that structural changes to cache won't block the entire server. - However, threads requesting to change the query cache will still have - to wait for the flush to finish. + Lock the query cache and queue all invalidation attempts to avoid + the risk of a race between invalidation, cache inserts and flushes. */ - m_cache_status= Query_cache::TABLE_FLUSH_IN_PROGRESS; - STRUCT_UNLOCK(&structure_guard_mutex); + lock(); + + DBUG_EXECUTE_IF("wait_in_query_cache_invalidate2", + debug_wait_for_kill("wait_in_query_cache_invalidate2"); ); + if (query_cache_size > 0) invalidate_table_internal(thd, key, key_length); - STRUCT_LOCK(&structure_guard_mutex); - m_cache_status= Query_cache::NO_FLUSH_IN_PROGRESS; - - /* - net_real_write might be waiting on a change on the m_cache_status - variable. - */ - pthread_cond_signal(&COND_cache_status_changed); - STRUCT_UNLOCK(&structure_guard_mutex); + unlock(); } @@ -2628,7 +2639,7 @@ void Query_cache::invalidate_table(THD *thd, uchar * key, uint32 key_length) The caller must ensure that no other thread is trying to work with the query cache when this function is executed. - @pre structure_guard_mutex is acquired or TABLE_FLUSH_IN_PROGRESS is set. + @pre structure_guard_mutex is acquired or LOCKED is set. */ void @@ -2646,7 +2657,7 @@ Query_cache::invalidate_table_internal(THD *thd, uchar *key, uint32 key_length) /** Invalidate a linked list of query cache blocks. - Each block tries to aquire a block level lock before + Each block tries to acquire a block level lock before free_query is a called. This function will in turn affect related table- and result-blocks. @@ -4170,10 +4181,7 @@ my_bool Query_cache::check_integrity(bool locked) DBUG_ENTER("check_integrity"); if (!locked) - STRUCT_LOCK(&structure_guard_mutex); - - while (is_flushing()) - pthread_cond_wait(&COND_cache_status_changed,&structure_guard_mutex); + lock_and_suspend(); if (hash_check(&queries)) { @@ -4422,7 +4430,7 @@ my_bool Query_cache::check_integrity(bool locked) } DBUG_ASSERT(result == 0); if (!locked) - STRUCT_UNLOCK(&structure_guard_mutex); + unlock(); DBUG_RETURN(result); } diff --git a/sql/sql_cache.h b/sql/sql_cache.h index f2c33eff614..777ddd39280 100644 --- a/sql/sql_cache.h +++ b/sql/sql_cache.h @@ -272,12 +272,12 @@ public: private: +#ifndef DBUG_OFF + my_thread_id m_cache_lock_thread_id; +#endif pthread_cond_t COND_cache_status_changed; - - enum Cache_status { NO_FLUSH_IN_PROGRESS, FLUSH_IN_PROGRESS, - TABLE_FLUSH_IN_PROGRESS }; - - Cache_status m_cache_status; + enum Cache_lock_status { UNLOCKED, LOCKED_NO_WAIT, LOCKED }; + Cache_lock_status m_cache_lock_status; void free_query_internal(Query_cache_block *point); void invalidate_table_internal(THD *thd, uchar *key, uint32 key_length); @@ -380,8 +380,6 @@ protected: Query_cache_block *pprev); my_bool join_results(ulong join_limit); - void wait_while_table_flush_is_in_progress(bool *interrupt); - /* Following function control structure_guard_mutex by themself or don't need structure_guard_mutex @@ -469,11 +467,6 @@ protected: friend void query_cache_end_of_result(THD *thd); friend void query_cache_abort(NET *net); - bool is_flushing(void) - { - return (m_cache_status != Query_cache::NO_FLUSH_IN_PROGRESS); - } - /* The following functions are only used when debugging We don't protect these with ifndef DBUG_OFF to not have to recompile @@ -491,6 +484,11 @@ protected: Query_cache_block_table * point, const char *name); my_bool in_blocks(Query_cache_block * point); + + bool try_lock(void); + void lock(void); + void lock_and_suspend(void); + void unlock(void); }; extern Query_cache query_cache; diff --git a/sql/sql_class.cc b/sql/sql_class.cc index cf5fdcf27a7..3f568566c89 100644 --- a/sql/sql_class.cc +++ b/sql/sql_class.cc @@ -27,6 +27,7 @@ #include "mysql_priv.h" #include "rpl_rli.h" +#include "rpl_filter.h" #include "rpl_record.h" #include "slave.h" #include <my_bitmap.h> @@ -546,6 +547,7 @@ THD::THD() first_successful_insert_id_in_prev_stmt_for_binlog(0), first_successful_insert_id_in_cur_stmt(0), stmt_depends_on_first_successful_insert_id_in_prev_stmt(FALSE), + examined_row_count(0), global_read_lock(0), is_fatal_error(0), transaction_rollback_request(0), @@ -590,7 +592,7 @@ THD::THD() // Must be reset to handle error with THD's created for init of mysqld lex->current_select= 0; start_time=(time_t) 0; - start_utime= 0L; + start_utime= prior_thr_create_utime= 0L; utime_after_lock= 0L; current_linfo = 0; slave_thread = 0; @@ -625,7 +627,7 @@ THD::THD() #ifdef SIGNAL_WITH_VIO_CLOSE active_vio = 0; #endif - pthread_mutex_init(&LOCK_delete, MY_MUTEX_INIT_FAST); + pthread_mutex_init(&LOCK_thd_data, MY_MUTEX_INIT_FAST); /* Variables with default values */ proc_info="login"; @@ -674,31 +676,40 @@ THD::THD() void THD::push_internal_handler(Internal_error_handler *handler) { - /* - TODO: The current implementation is limited to 1 handler at a time only. - THD and sp_rcontext need to be modified to use a common handler stack. - */ - DBUG_ASSERT(m_internal_handler == NULL); - m_internal_handler= handler; + if (m_internal_handler) + { + handler->m_prev_internal_handler= m_internal_handler; + m_internal_handler= handler; + } + else + { + m_internal_handler= handler; + } } bool THD::handle_error(uint sql_errno, const char *message, MYSQL_ERROR::enum_warning_level level) { - if (m_internal_handler) + if (!m_internal_handler) + return FALSE; + + for (Internal_error_handler *error_handler= m_internal_handler; + error_handler; + error_handler= m_internal_handler->m_prev_internal_handler) { - return m_internal_handler->handle_error(sql_errno, message, level, this); + if (error_handler->handle_error(sql_errno, message, level, this)) + return TRUE; } - return FALSE; // 'FALSE', as per coding style + return FALSE; } void THD::pop_internal_handler() { DBUG_ASSERT(m_internal_handler != NULL); - m_internal_handler= NULL; + m_internal_handler= m_internal_handler->m_prev_internal_handler; } extern "C" @@ -746,6 +757,12 @@ void thd_get_xid(const MYSQL_THD thd, MYSQL_XID *xid) *xid = *(MYSQL_XID *) &thd->transaction.xid_state.xid; } +#ifdef _WIN32 +extern "C" THD *_current_thd_noinline(void) +{ + return my_pthread_getspecific_ptr(THD*,THR_THD); +} +#endif /* Init common variables that has to be reset on start and on change_user */ @@ -900,8 +917,8 @@ THD::~THD() THD_CHECK_SENTRY(this); DBUG_ENTER("~THD()"); /* Ensure that no one is using THD */ - pthread_mutex_lock(&LOCK_delete); - pthread_mutex_unlock(&LOCK_delete); + pthread_mutex_lock(&LOCK_thd_data); + pthread_mutex_unlock(&LOCK_thd_data); add_to_status(&global_status_var, &status_var); /* Close connection */ @@ -928,7 +945,7 @@ THD::~THD() free_root(&transaction.mem_root,MYF(0)); #endif mysys_var=0; // Safety (shouldn't be needed) - pthread_mutex_destroy(&LOCK_delete); + pthread_mutex_destroy(&LOCK_thd_data); #ifndef DBUG_OFF dbug_sentry= THD_SENTRY_GONE; #endif @@ -1001,7 +1018,7 @@ void THD::awake(THD::killed_state state_to_set) DBUG_ENTER("THD::awake"); DBUG_PRINT("enter", ("this: 0x%lx", (long) this)); THD_CHECK_SENTRY(this); - safe_mutex_assert_owner(&LOCK_delete); + safe_mutex_assert_owner(&LOCK_thd_data); killed= state_to_set; if (state_to_set != THD::KILL_QUERY) @@ -1398,7 +1415,7 @@ int THD::send_explain_fields(select_result *result) void THD::close_active_vio() { DBUG_ENTER("close_active_vio"); - safe_mutex_assert_owner(&LOCK_delete); + safe_mutex_assert_owner(&LOCK_thd_data); #ifndef EMBEDDED_LIBRARY if (active_vio) { @@ -1779,6 +1796,8 @@ select_export::prepare(List<Item> &list, SELECT_LEX_UNIT *u) if ((uint) strlen(exchange->file_name) + NAME_LEN >= FN_REFLEN) strmake(path,exchange->file_name,FN_REFLEN-1); + write_cs= exchange->cs ? exchange->cs : &my_charset_bin; + if ((file= create_file(thd, path, exchange, &cache)) < 0) return 1; /* Check if there is any blobs in data */ @@ -1798,6 +1817,31 @@ select_export::prepare(List<Item> &list, SELECT_LEX_UNIT *u) non_string_results= TRUE; } } + if (exchange->escaped->numchars() > 1 || exchange->enclosed->numchars() > 1) + { + my_error(ER_WRONG_FIELD_TERMINATORS, MYF(0)); + return TRUE; + } + if (exchange->escaped->length() > 1 || exchange->enclosed->length() > 1 || + !my_isascii(exchange->escaped->ptr()[0]) || + !my_isascii(exchange->enclosed->ptr()[0]) || + !exchange->field_term->is_ascii() || !exchange->line_term->is_ascii() || + !exchange->line_start->is_ascii()) + { + /* + Current LOAD DATA INFILE recognizes field/line separators "as is" without + converting from client charset to data file charset. So, it is supposed, + that input file of LOAD DATA INFILE consists of data in one charset and + separators in other charset. For the compatibility with that [buggy] + behaviour SELECT INTO OUTFILE implementation has been saved "as is" too, + but the new warning message has been added: + + Non-ASCII separator arguments are not fully supported + */ + push_warning(thd, MYSQL_ERROR::WARN_LEVEL_WARN, + WARN_NON_ASCII_SEPARATOR_NOT_IMPLEMENTED, + ER(WARN_NON_ASCII_SEPARATOR_NOT_IMPLEMENTED)); + } field_term_length=exchange->field_term->length(); field_term_char= field_term_length ? (int) (uchar) (*exchange->field_term)[0] : INT_MAX; @@ -1847,6 +1891,8 @@ bool select_export::send_data(List<Item> &items) DBUG_ENTER("select_export::send_data"); char buff[MAX_FIELD_WIDTH],null_buff[2],space[MAX_FIELD_WIDTH]; + char cvt_buff[MAX_FIELD_WIDTH]; + String cvt_str(cvt_buff, sizeof(cvt_buff), write_cs); bool space_inited=0; String tmp(buff,sizeof(buff),&my_charset_bin),*res; tmp.length(0); @@ -1870,6 +1916,37 @@ bool select_export::send_data(List<Item> &items) bool enclosed = (exchange->enclosed->length() && (!exchange->opt_enclosed || result_type == STRING_RESULT)); res=item->str_result(&tmp); + if (res && !my_charset_same(write_cs, res->charset()) && + !my_charset_same(write_cs, &my_charset_bin)) + { + const char *well_formed_error_pos; + const char *cannot_convert_error_pos; + const char *from_end_pos; + const char *error_pos; + uint32 bytes; + bytes= well_formed_copy_nchars(write_cs, cvt_buff, sizeof(cvt_buff), + res->charset(), res->ptr(), res->length(), + sizeof(cvt_buff), + &well_formed_error_pos, + &cannot_convert_error_pos, + &from_end_pos); + error_pos= well_formed_error_pos ? well_formed_error_pos + : cannot_convert_error_pos; + if (error_pos) + { + char printable_buff[32]; + convert_to_printable(printable_buff, sizeof(printable_buff), + error_pos, res->ptr() + res->length() - error_pos, + res->charset(), 6); + push_warning_printf(thd, MYSQL_ERROR::WARN_LEVEL_WARN, + ER_TRUNCATED_WRONG_VALUE_FOR_FIELD, + ER(ER_TRUNCATED_WRONG_VALUE_FOR_FIELD), + "string", printable_buff, + item->name, row_count); + } + cvt_str.length(bytes); + res= &cvt_str; + } if (res && enclosed) { if (my_b_write(&cache,(uchar*) exchange->enclosed->ptr(), @@ -3044,6 +3121,25 @@ void THD::restore_sub_statement_state(Sub_statement_state *backup) } +void THD::set_statement(Statement *stmt) +{ + pthread_mutex_lock(&LOCK_thd_data); + Statement::set_statement(stmt); + pthread_mutex_unlock(&LOCK_thd_data); +} + + +/** Assign a new value to thd->query. */ + +void THD::set_query(char *query_arg, uint32 query_length_arg) +{ + pthread_mutex_lock(&LOCK_thd_data); + query= query_arg; + query_length= query_length_arg; + pthread_mutex_unlock(&LOCK_thd_data); +} + + /** Mark transaction to rollback and mark error as fatal to a sub-statement. @@ -3649,7 +3745,7 @@ show_query_type(THD::enum_binlog_query_type qtype) */ int THD::binlog_query(THD::enum_binlog_query_type qtype, char const *query_arg, ulong query_len, bool is_trans, bool suppress_use, - THD::killed_state killed_status_arg) + int errcode) { DBUG_ENTER("THD::binlog_query"); DBUG_PRINT("enter", ("qtype: %s query: '%s'", @@ -3674,7 +3770,8 @@ int THD::binlog_query(THD::enum_binlog_query_type qtype, char const *query_arg, we should print a warning. */ if (sql_log_bin_toplevel && lex->is_stmt_unsafe() && - variables.binlog_format == BINLOG_FORMAT_STMT) + variables.binlog_format == BINLOG_FORMAT_STMT && + binlog_filter->db_ok(this->db)) { /* A warning can be elevated a error when STRICT sql mode. @@ -3683,7 +3780,8 @@ int THD::binlog_query(THD::enum_binlog_query_type qtype, char const *query_arg, push_warning(this, MYSQL_ERROR::WARN_LEVEL_NOTE, ER_BINLOG_UNSAFE_STATEMENT, ER(ER_BINLOG_UNSAFE_STATEMENT)); - if (!(binlog_flags & BINLOG_FLAG_UNSAFE_STMT_PRINTED)) + if (global_system_variables.log_warnings && + !(binlog_flags & BINLOG_FLAG_UNSAFE_STMT_PRINTED)) { sql_print_warning("%s Statement: %.*s", ER(ER_BINLOG_UNSAFE_STATEMENT), @@ -3716,7 +3814,7 @@ int THD::binlog_query(THD::enum_binlog_query_type qtype, char const *query_arg, */ { Query_log_event qinfo(this, query_arg, query_len, is_trans, suppress_use, - killed_status_arg); + errcode); qinfo.flags|= LOG_EVENT_UPDATE_TABLE_MAP_VERSION_F; /* Binlog table maps will be irrelevant after a Query_log_event diff --git a/sql/sql_class.h b/sql/sql_class.h index ce4524fb982..f52d5fae76f 100644 --- a/sql/sql_class.h +++ b/sql/sql_class.h @@ -91,7 +91,7 @@ enum enum_mark_columns extern char internal_table_name[2]; extern char empty_c_string[1]; -extern const char **errmesg; +extern MYSQL_PLUGIN_IMPORT const char **errmesg; #define TC_LOG_PAGE_SIZE 8192 #define TC_LOG_MIN_SIZE (3*TC_LOG_PAGE_SIZE) @@ -631,22 +631,16 @@ public: we need to declare it char * because all table handlers are written in C and need to point to it. - Note that (A) if we set query = NULL, we must at the same time set - query_length = 0, and protect the whole operation with the - LOCK_thread_count mutex. And (B) we are ONLY allowed to set query to a - non-NULL value if its previous value is NULL. We do not need to protect - operation (B) with any mutex. To avoid crashes in races, if we do not - know that thd->query cannot change at the moment, one should print + Note that if we set query = NULL, we must at the same time set + query_length = 0, and protect the whole operation with + LOCK_thd_data mutex. To avoid crashes in races, if we do not + know that thd->query cannot change at the moment, we should print thd->query like this: - (1) reserve the LOCK_thread_count mutex; - (2) check if thd->query is NULL; - (3) if not NULL, then print at most thd->query_length characters from - it. We will see the query_length field as either 0, or the right value - for it. - Assuming that the write and read of an n-bit memory field in an n-bit - computer is atomic, we can avoid races in the above way. - This printing is needed at least in SHOW PROCESSLIST and SHOW INNODB - STATUS. + (1) reserve the LOCK_thd_data mutex; + (2) print or copy the value of query and query_length + (3) release LOCK_thd_data mutex. + This printing is needed at least in SHOW PROCESSLIST and SHOW + ENGINE INNODB STATUS. */ char *query; uint32 query_length; // current query length @@ -678,7 +672,7 @@ public: virtual ~Statement(); /* Assign execution context (note: not all members) of given stmt to self */ - void set_statement(Statement *stmt); + virtual void set_statement(Statement *stmt); void set_n_backup_statement(Statement *stmt, Statement *backup); void restore_backup_statement(Statement *stmt, Statement *backup); /* return class type */ @@ -1036,7 +1030,10 @@ show_system_thread(enum_thread_type thread) class Internal_error_handler { protected: - Internal_error_handler() {} + Internal_error_handler() : + m_prev_internal_handler(NULL) + {} + virtual ~Internal_error_handler() {} public: @@ -1069,6 +1066,28 @@ public: const char *message, MYSQL_ERROR::enum_warning_level level, THD *thd) = 0; +private: + Internal_error_handler *m_prev_internal_handler; + friend class THD; +}; + + +/** + Implements the trivial error handler which cancels all error states + and prevents an SQLSTATE to be set. +*/ + +class Dummy_error_handler : public Internal_error_handler +{ +public: + bool handle_error(uint sql_errno, + const char *message, + MYSQL_ERROR::enum_warning_level level, + THD *thd) + { + /* Ignore error */ + return TRUE; + } }; @@ -1273,7 +1292,15 @@ public: THR_LOCK_OWNER main_lock_id; // To use for conventional queries THR_LOCK_OWNER *lock_id; // If not main_lock_id, points to // the lock_id of a cursor. - pthread_mutex_t LOCK_delete; // Locked before thd is deleted + /** + Protects THD data accessed from other threads: + - thd->query and thd->query_length (used by SHOW ENGINE + INNODB STATUS and SHOW PROCESSLIST + - thd->mysys_var (used by KILL statement and shutdown). + Is locked when THD is deleted. + */ + pthread_mutex_t LOCK_thd_data; + /* all prepared statements and cursors of this connection */ Statement_map stmt_map; /* @@ -1310,6 +1337,10 @@ public: Set it using the thd_proc_info(THD *thread, const char *message) macro/function. + + This member is accessed and assigned without any synchronization. + Therefore, it may point only to constant (statically + allocated) strings, which memory won't go away over time. */ const char *proc_info; @@ -1345,7 +1376,8 @@ public: /* remote (peer) port */ uint16 peer_port; time_t start_time, user_time; - ulonglong connect_utime, thr_create_utime; // track down slow pthread_create + // track down slow pthread_create + ulonglong prior_thr_create_utime, thr_create_utime; ulonglong start_utime, utime_after_lock; thr_lock_type update_lock_default; @@ -1439,6 +1471,14 @@ public: { changed_tables= 0; savepoints= 0; + /* + If rm_error is raised, it means that this piece of a distributed + transaction has failed and must be rolled back. But the user must + rollback it explicitly, so don't start a new distributed XA until + then. + */ + if (!xid_state.rm_error) + xid_state.xid.null(); #ifdef USING_TRANSACTIONS free_root(&mem_root,MYF(MY_KEEP_PREALLOC)); #endif @@ -1852,15 +1892,15 @@ public: #ifdef SIGNAL_WITH_VIO_CLOSE inline void set_active_vio(Vio* vio) { - pthread_mutex_lock(&LOCK_delete); + pthread_mutex_lock(&LOCK_thd_data); active_vio = vio; - pthread_mutex_unlock(&LOCK_delete); + pthread_mutex_unlock(&LOCK_thd_data); } inline void clear_active_vio() { - pthread_mutex_lock(&LOCK_delete); + pthread_mutex_lock(&LOCK_thd_data); active_vio = 0; - pthread_mutex_unlock(&LOCK_delete); + pthread_mutex_unlock(&LOCK_thd_data); } void close_active_vio(); #endif @@ -1889,7 +1929,7 @@ public: int binlog_query(enum_binlog_query_type qtype, char const *query, ulong query_len, bool is_trans, bool suppress_use, - THD::killed_state killed_err_arg= THD::KILLED_NO_VALUE); + int errcode); #endif /* @@ -2210,6 +2250,9 @@ public: thd_scheduler scheduler; public: + inline Internal_error_handler *get_internal_handler() + { return m_internal_handler; } + /** Add an internal error handler to the thread execution context. @param handler the exception handler to add @@ -2230,6 +2273,14 @@ public: */ void pop_internal_handler(); + /** Overloaded to guard query/query_length fields */ + virtual void set_statement(Statement *stmt); + + /** + Assign a new value to thd->query. + Protected with LOCK_thd_data mutex. + */ + void set_query(char *query_arg, uint32 query_length_arg); private: /** The current internal error handler for this thread, or NULL. */ Internal_error_handler *m_internal_handler; @@ -2442,6 +2493,7 @@ class select_export :public select_to_file { */ bool is_unsafe_field_sep; bool fixed_row_size; + CHARSET_INFO *write_cs; // output charset public: select_export(sql_exchange *ex) :select_to_file(ex) {} /** diff --git a/sql/sql_connect.cc b/sql/sql_connect.cc index e4d7cf6feb5..98574a07a4e 100644 --- a/sql/sql_connect.cc +++ b/sql/sql_connect.cc @@ -1074,8 +1074,8 @@ static void prepare_new_connection_state(THD* thd) pthread_handler_t handle_one_connection(void *arg) { THD *thd= (THD*) arg; - ulong launch_time= (ulong) ((thd->thr_create_utime= my_micro_time()) - - thd->connect_utime); + + thd->thr_create_utime= my_micro_time(); if (thread_scheduler.init_new_connection_thread()) { @@ -1084,8 +1084,20 @@ pthread_handler_t handle_one_connection(void *arg) thread_scheduler.end_thread(thd,0); return 0; } - if (launch_time >= slow_launch_time*1000000L) - statistic_increment(slow_launch_threads,&LOCK_status); + + /* + If a thread was created to handle this connection: + increment slow_launch_threads counter if it took more than + slow_launch_time seconds to create the thread. + */ + if (thd->prior_thr_create_utime) + { + ulong launch_time= (ulong) (thd->thr_create_utime - + thd->prior_thr_create_utime); + if (launch_time >= slow_launch_time*1000000L) + statistic_increment(slow_launch_threads, &LOCK_status); + thd->prior_thr_create_utime= 0; + } /* handle_one_connection() is normally the only way a thread would diff --git a/sql/sql_db.cc b/sql/sql_db.cc index 5a266c3fac9..98d17fdd318 100644 --- a/sql/sql_db.cc +++ b/sql/sql_db.cc @@ -181,8 +181,7 @@ uchar* dboptions_get_key(my_dbopt_t *opt, size_t *length, static inline void write_to_binlog(THD *thd, char *query, uint q_len, char *db, uint db_len) { - Query_log_event qinfo(thd, query, q_len, 0, 0, THD::NOT_KILLED); - qinfo.error_code= 0; + Query_log_event qinfo(thd, query, q_len, 0, 0, 0); qinfo.db= db; qinfo.db_len= db_len; mysql_bin_log.write(&qinfo); @@ -538,13 +537,13 @@ err1: bool load_db_opt_by_name(THD *thd, const char *db_name, HA_CREATE_INFO *db_create_info) { - char db_opt_path[FN_REFLEN]; + char db_opt_path[FN_REFLEN + 1]; /* Pass an empty file name, and the database options file name as extension to avoid table name to file name encoding. */ - (void) build_table_filename(db_opt_path, sizeof(db_opt_path), + (void) build_table_filename(db_opt_path, sizeof(db_opt_path) - 1, db_name, "", MY_DB_OPT_FILE, 0); return load_db_opt(thd, db_opt_path, db_create_info); @@ -646,7 +645,7 @@ int mysql_create_db(THD *thd, char *db, HA_CREATE_INFO *create_info, VOID(pthread_mutex_lock(&LOCK_mysql_create_db)); /* Check directory */ - path_len= build_table_filename(path, sizeof(path), db, "", "", 0); + path_len= build_table_filename(path, sizeof(path) - 1, db, "", "", 0); path[path_len-1]= 0; // Remove last '/' from path if (my_stat(path,&stat_info,MYF(0))) @@ -723,8 +722,9 @@ int mysql_create_db(THD *thd, char *db, HA_CREATE_INFO *create_info, if (mysql_bin_log.is_open()) { + int errcode= query_error_code(thd, TRUE); Query_log_event qinfo(thd, query, query_length, 0, - /* suppress_use */ TRUE, THD::NOT_KILLED); + /* suppress_use */ TRUE, errcode); /* Write should use the database being created as the "current @@ -791,7 +791,7 @@ bool mysql_alter_db(THD *thd, const char *db, HA_CREATE_INFO *create_info) We pass MY_DB_OPT_FILE as "extension" to avoid "table name to file name" encoding. */ - build_table_filename(path, sizeof(path), db, "", MY_DB_OPT_FILE, 0); + build_table_filename(path, sizeof(path) - 1, db, "", MY_DB_OPT_FILE, 0); if ((error=write_db_opt(thd, path, create_info))) goto exit; @@ -811,8 +811,9 @@ bool mysql_alter_db(THD *thd, const char *db, HA_CREATE_INFO *create_info) if (mysql_bin_log.is_open()) { + int errcode= query_error_code(thd, TRUE); Query_log_event qinfo(thd, thd->query, thd->query_length, 0, - /* suppress_use */ TRUE, THD::NOT_KILLED); + /* suppress_use */ TRUE, errcode); /* Write should use the database being created as the "current @@ -883,7 +884,7 @@ bool mysql_rm_db(THD *thd,char *db,bool if_exists, bool silent) VOID(pthread_mutex_lock(&LOCK_mysql_create_db)); - length= build_table_filename(path, sizeof(path), db, "", "", 0); + length= build_table_filename(path, sizeof(path) - 1, db, "", "", 0); strmov(path+length, MY_DB_OPT_FILE); // Append db option file name del_dbopt(path); // Remove dboption hash entry path[length]= '\0'; // Remove file name @@ -958,8 +959,9 @@ bool mysql_rm_db(THD *thd,char *db,bool if_exists, bool silent) } if (mysql_bin_log.is_open()) { + int errcode= query_error_code(thd, TRUE); Query_log_event qinfo(thd, query, query_length, 0, - /* suppress_use */ TRUE, THD::NOT_KILLED); + /* suppress_use */ TRUE, errcode); /* Write should use the database being created as the "current database" and not the threads current database, which is the @@ -1839,7 +1841,7 @@ bool mysql_upgrade_db(THD *thd, LEX_STRING *old_db) for (uint idx=0 ; idx < nfiles && !thd->killed ; idx++) { FILEINFO *file= dirp->dir_entry + idx; - char *extension, tname[FN_REFLEN]; + char *extension, tname[FN_REFLEN + 1]; LEX_STRING table_str; DBUG_PRINT("info",("Examining: %s", file->name)); @@ -1928,7 +1930,7 @@ bool mysql_upgrade_db(THD *thd, LEX_STRING *old_db) for (uint idx=0 ; idx < nfiles ; idx++) { FILEINFO *file= dirp->dir_entry + idx; - char oldname[FN_REFLEN], newname[FN_REFLEN]; + char oldname[FN_REFLEN + 1], newname[FN_REFLEN + 1]; DBUG_PRINT("info",("Examining: %s", file->name)); /* skiping . and .. and MY_DB_OPT_FILE */ @@ -1958,8 +1960,9 @@ bool mysql_upgrade_db(THD *thd, LEX_STRING *old_db) /* Step8: logging */ if (mysql_bin_log.is_open()) { + int errcode= query_error_code(thd, TRUE); Query_log_event qinfo(thd, thd->query, thd->query_length, - 0, TRUE, THD::NOT_KILLED); + 0, TRUE, errcode); thd->clear_error(); mysql_bin_log.write(&qinfo); } @@ -1997,10 +2000,10 @@ exit: bool check_db_dir_existence(const char *db_name) { - char db_dir_path[FN_REFLEN]; + char db_dir_path[FN_REFLEN + 1]; uint db_dir_path_len; - db_dir_path_len= build_table_filename(db_dir_path, sizeof(db_dir_path), + db_dir_path_len= build_table_filename(db_dir_path, sizeof(db_dir_path) - 1, db_name, "", "", 0); if (db_dir_path_len && db_dir_path[db_dir_path_len - 1] == FN_LIBCHAR) diff --git a/sql/sql_delete.cc b/sql/sql_delete.cc index 7b967206305..0d4bf3f0b8f 100644 --- a/sql/sql_delete.cc +++ b/sql/sql_delete.cc @@ -187,6 +187,14 @@ bool mysql_delete(THD *thd, TABLE_LIST *table_list, COND *conds, delete select; free_underlaid_joins(thd, select_lex); thd->row_count_func= 0; + /* + Error was already created by quick select evaluation (check_quick()). + TODO: Add error code output parameter to Item::val_xxx() methods. + Currently they rely on the user checking DA for + errors when unwinding the stack after calling Item::val_xxx(). + */ + if (thd->is_error()) + DBUG_RETURN(TRUE); my_ok(thd, (ha_rows) thd->row_count_func); /* We don't need to call reset_auto_increment in this case, because @@ -389,8 +397,12 @@ cleanup: FALSE : transactional_table; + int errcode= 0; if (error < 0) thd->clear_error(); + else + errcode= query_error_code(thd, killed_status == THD::NOT_KILLED); + /* [binlog]: If 'handler::delete_all_rows()' was called and the storage engine does not inject the rows itself, we replicate @@ -402,9 +414,9 @@ cleanup: */ int log_result= thd->binlog_query(query_type, thd->query, thd->query_length, - is_trans, FALSE, killed_status); + is_trans, FALSE, errcode); - if (log_result && transactional_table) + if (log_result) { error=1; } @@ -486,7 +498,7 @@ int mysql_prepare_delete(THD *thd, TABLE_LIST *table_list, Item **conds) if (select_lex->inner_refs_list.elements && fix_inner_refs(thd, all_fields, select_lex, select_lex->ref_pointer_array)) - DBUG_RETURN(-1); + DBUG_RETURN(TRUE); select_lex->fix_prepare_information(thd, conds, &fake_conds); DBUG_RETURN(FALSE); @@ -802,7 +814,7 @@ void multi_delete::abort() /* the error was handled or nothing deleted and no side effects return */ if (error_handled || - !thd->transaction.stmt.modified_non_trans_table && !deleted) + (!thd->transaction.stmt.modified_non_trans_table && !deleted)) DBUG_VOID_RETURN; /* Something already deleted so we have to invalidate cache */ @@ -836,9 +848,10 @@ void multi_delete::abort() */ if (mysql_bin_log.is_open()) { + int errcode= query_error_code(thd, thd->killed == THD::NOT_KILLED); thd->binlog_query(THD::ROW_QUERY_TYPE, thd->query, thd->query_length, - transactional_tables, FALSE); + transactional_tables, FALSE, errcode); } thd->transaction.all.modified_non_trans_table= true; } @@ -979,11 +992,14 @@ bool multi_delete::send_eof() { if (mysql_bin_log.is_open()) { + int errcode= 0; if (local_error == 0) thd->clear_error(); + else + errcode= query_error_code(thd, killed_status == THD::NOT_KILLED); if (thd->binlog_query(THD::ROW_QUERY_TYPE, thd->query, thd->query_length, - transactional_tables, FALSE, killed_status) && + transactional_tables, FALSE, errcode) && !normal_tables) { local_error=1; // Log write failed: roll back the SQL statement @@ -1043,7 +1059,7 @@ static bool mysql_truncate_by_delete(THD *thd, TABLE_LIST *table_list) bool mysql_truncate(THD *thd, TABLE_LIST *table_list, bool dont_send_ok) { HA_CREATE_INFO create_info; - char path[FN_REFLEN]; + char path[FN_REFLEN + 1]; TABLE *table; bool error; uint path_length; @@ -1080,7 +1096,7 @@ bool mysql_truncate(THD *thd, TABLE_LIST *table_list, bool dont_send_ok) goto end; } - path_length= build_table_filename(path, sizeof(path), table_list->db, + path_length= build_table_filename(path, sizeof(path) - 1, table_list->db, table_list->table_name, reg_ext, 0); if (!dont_send_ok) diff --git a/sql/sql_derived.cc b/sql/sql_derived.cc index 41be98621a6..37adf5c403a 100644 --- a/sql/sql_derived.cc +++ b/sql/sql_derived.cc @@ -179,6 +179,7 @@ exit: { if (thd->is_error() && (thd->main_da.sql_errno() == ER_BAD_FIELD_ERROR || + thd->main_da.sql_errno() == ER_FUNC_INEXISTENT_NAME_COLLISION || thd->main_da.sql_errno() == ER_SP_DOES_NOT_EXIST)) { thd->clear_error(); diff --git a/sql/sql_handler.cc b/sql/sql_handler.cc index 16810e29343..1e92d95573a 100644 --- a/sql/sql_handler.cc +++ b/sql/sql_handler.cc @@ -252,14 +252,37 @@ bool mysql_ha_open(THD *thd, TABLE_LIST *tables, bool reopen) /* for now HANDLER can be used only for real TABLES */ tables->required_type= FRMTYPE_TABLE; + /* + We use open_tables() here, rather than, say, + open_ltable() or open_table() because we would like to be able + to open a temporary table. + */ error= open_tables(thd, &tables, &counter, 0); - /* restore the state and merge the opened table into handler_tables list */ if (thd->open_tables) { - thd->open_tables->next= thd->handler_tables; - thd->handler_tables= thd->open_tables; + if (thd->open_tables->next) + { + /* + We opened something that is more than a single table. + This happens with MERGE engine. Don't try to link + this mess into thd->handler_tables list, close it + and report an error. We must do it right away + because mysql_ha_close_table(), called down the road, + can close a single table only. + */ + close_thread_tables(thd); + my_error(ER_ILLEGAL_HA, MYF(0), tables->alias); + error= 1; + } + else + { + /* Merge the opened table into handler_tables list. */ + thd->open_tables->next= thd->handler_tables; + thd->handler_tables= thd->open_tables; + } } + /* Restore the state. */ thd->open_tables= backup_open_tables; if (error) diff --git a/sql/sql_help.cc b/sql/sql_help.cc index f51ad318568..2818aa5082c 100644 --- a/sql/sql_help.cc +++ b/sql/sql_help.cc @@ -526,7 +526,7 @@ int send_variant_2_list(MEM_ROOT *mem_root, Protocol *protocol, String **end= pointers + names->elements; List_iterator<String> it(*names); - for (pos= pointers; pos!=end; (*pos++= it++)); + for (pos= pointers; pos!=end; (*pos++= it++)) ; my_qsort(pointers,names->elements,sizeof(String*),string_ptr_cmp); diff --git a/sql/sql_insert.cc b/sql/sql_insert.cc index 6c27766ec86..7d581f8c3a9 100644 --- a/sql/sql_insert.cc +++ b/sql/sql_insert.cc @@ -301,9 +301,7 @@ static int check_update_fields(THD *thd, TABLE_LIST *insert_table_list, List<Item> &update_fields, table_map *map) { TABLE *table= insert_table_list->table; - my_bool timestamp_mark; - - LINT_INIT(timestamp_mark); + my_bool timestamp_mark= 0; if (table->timestamp_field) { @@ -393,7 +391,7 @@ void upgrade_lock_type(THD *thd, thr_lock_type *lock_type, bool is_multi_insert) { if (duplic == DUP_UPDATE || - duplic == DUP_REPLACE && *lock_type == TL_WRITE_CONCURRENT_INSERT) + (duplic == DUP_REPLACE && *lock_type == TL_WRITE_CONCURRENT_INSERT)) { *lock_type= TL_WRITE_DEFAULT; return; @@ -858,11 +856,13 @@ bool mysql_insert(THD *thd,TABLE_LIST *table_list, */ query_cache_invalidate3(thd, table_list, 1); } - if (changed && error <= 0 || thd->transaction.stmt.modified_non_trans_table - || was_insert_delayed) + if ((changed && error <= 0) || + thd->transaction.stmt.modified_non_trans_table || + was_insert_delayed) { if (mysql_bin_log.is_open()) { + int errcode= 0; if (error <= 0) { /* @@ -877,6 +877,9 @@ bool mysql_insert(THD *thd,TABLE_LIST *table_list, /* todo: consider removing */ thd->clear_error(); } + else + errcode= query_error_code(thd, thd->killed == THD::NOT_KILLED); + /* bug#22725: A query which per-row-loop can not be interrupted with @@ -893,8 +896,7 @@ bool mysql_insert(THD *thd,TABLE_LIST *table_list, if (thd->binlog_query(THD::ROW_QUERY_TYPE, thd->query, thd->query_length, transactional_table, FALSE, - (error>0) ? thd->killed : THD::NOT_KILLED) && - transactional_table) + errcode)) { error=1; } @@ -1114,6 +1116,33 @@ static bool mysql_prepare_insert_check_table(THD *thd, TABLE_LIST *table_list, /* + Get extra info for tables we insert into + + @param table table(TABLE object) we insert into, + might be NULL in case of view + @param table(TABLE_LIST object) or view we insert into +*/ + +static void prepare_for_positional_update(TABLE *table, TABLE_LIST *tables) +{ + if (table) + { + if(table->reginfo.lock_type != TL_WRITE_DELAYED) + table->prepare_for_position(); + return; + } + + DBUG_ASSERT(tables->view); + List_iterator<TABLE_LIST> it(*tables->view_tables); + TABLE_LIST *tbl; + while ((tbl= it++)) + prepare_for_positional_update(tbl->table, tbl); + + return; +} + + +/* Prepare items in INSERT statement SYNOPSIS @@ -1262,9 +1291,8 @@ bool mysql_prepare_insert(THD *thd, TABLE_LIST *table_list, Only call prepare_for_posistion() if we are not performing a DELAYED operation. It will instead be executed by delayed insert thread. */ - if ((duplic == DUP_UPDATE || duplic == DUP_REPLACE) && - (table->reginfo.lock_type != TL_WRITE_DELAYED)) - table->prepare_for_position(); + if (duplic == DUP_UPDATE || duplic == DUP_REPLACE) + prepare_for_positional_update(table, table_list); DBUG_RETURN(FALSE); } @@ -1881,7 +1909,7 @@ bool delayed_get_table(THD *thd, TABLE_LIST *table_list) thread_count++; pthread_mutex_unlock(&LOCK_thread_count); di->thd.set_db(table_list->db, (uint) strlen(table_list->db)); - di->thd.query= my_strdup(table_list->table_name, MYF(MY_WME)); + di->thd.set_query(my_strdup(table_list->table_name, MYF(MY_WME)), 0); if (di->thd.db == NULL || di->thd.query == NULL) { /* The error is reported */ @@ -2667,6 +2695,12 @@ bool Delayed_insert::handle_inserts(void) thd.variables.time_zone = row->time_zone; } + /* if the delayed insert was killed, the killed status is + ignored while binlogging */ + int errcode= 0; + if (thd.killed == THD::NOT_KILLED) + errcode= query_error_code(&thd, TRUE); + /* If the query has several rows to insert, only the first row will come here. In row-based binlogging, this means that the first row will be @@ -2677,7 +2711,7 @@ bool Delayed_insert::handle_inserts(void) */ thd.binlog_query(THD::ROW_QUERY_TYPE, row->query.str, row->query.length, - FALSE, FALSE); + FALSE, FALSE, errcode); thd.time_zone_used = backup_time_zone_used; thd.variables.time_zone = backup_time_zone; @@ -3090,7 +3124,10 @@ bool select_insert::send_data(List<Item> &values) store_values(values); thd->count_cuted_fields= CHECK_FIELD_IGNORE; if (thd->is_error()) + { + table->auto_increment_field_not_null= FALSE; DBUG_RETURN(1); + } if (table_list) // Not CREATE ... SELECT { switch (table_list->view_check_option(thd, info.ignore)) { @@ -3101,6 +3138,9 @@ bool select_insert::send_data(List<Item> &values) } } + // Release latches in case bulk insert takes a long time + ha_release_temporary_latches(thd); + error= write_record(thd, table, &info); table->auto_increment_field_not_null= FALSE; @@ -3173,7 +3213,8 @@ bool select_insert::send_eof() table->file->extra(HA_EXTRA_NO_IGNORE_DUP_KEY); table->file->extra(HA_EXTRA_WRITE_CANNOT_REPLACE); - if (changed= (info.copied || info.deleted || info.updated)) + changed= (info.copied || info.deleted || info.updated); + if (changed) { /* We must invalidate the table in the query cache before binlog writing @@ -3194,11 +3235,14 @@ bool select_insert::send_eof() */ if (mysql_bin_log.is_open()) { + int errcode= 0; if (!error) thd->clear_error(); + else + errcode= query_error_code(thd, killed_status == THD::NOT_KILLED); thd->binlog_query(THD::ROW_QUERY_TYPE, thd->query, thd->query_length, - trans_table, FALSE, killed_status); + trans_table, FALSE, errcode); } table->file->ha_release_auto_increment(); @@ -3265,8 +3309,11 @@ void select_insert::abort() { if (thd->transaction.stmt.modified_non_trans_table) { if (mysql_bin_log.is_open()) + { + int errcode= query_error_code(thd, thd->killed == THD::NOT_KILLED); thd->binlog_query(THD::ROW_QUERY_TYPE, thd->query, thd->query_length, - transactional_table, FALSE); + transactional_table, FALSE, errcode); + } if (!thd->current_stmt_binlog_row_based && !can_rollback_data()) thd->transaction.all.modified_non_trans_table= TRUE; if (changed) @@ -3658,10 +3705,14 @@ select_create::binlog_show_create_table(TABLE **tables, uint count) DBUG_ASSERT(result == 0); /* store_create_info() always return 0 */ if (mysql_bin_log.is_open()) + { + int errcode= query_error_code(thd, thd->killed == THD::NOT_KILLED); thd->binlog_query(THD::STMT_QUERY_TYPE, query.ptr(), query.length(), /* is_trans */ TRUE, - /* suppress_use */ FALSE); + /* suppress_use */ FALSE, + errcode); + } } void select_create::store_values(List<Item> &values) diff --git a/sql/sql_lex.cc b/sql/sql_lex.cc index b96ac91679b..2adbc44eb12 100644 --- a/sql/sql_lex.cc +++ b/sql/sql_lex.cc @@ -433,6 +433,16 @@ bool is_keyword(const char *name, uint len) return get_hash_symbol(name,len,0)!=0; } +/** + Check if name is a sql function + + @param name checked name + + @return is this a native function or not + @retval 0 name is a function + @retval 1 name isn't a function +*/ + bool is_lex_native_function(const LEX_STRING *name) { DBUG_ASSERT(name != NULL); @@ -770,7 +780,7 @@ bool consume_comment(Lex_input_stream *lip, int remaining_recursions_permitted) int MYSQLlex(void *arg, void *yythd) { - reg1 uchar c; + reg1 uchar c= 0; bool comment_closed; int tokval, result_state; uint length; @@ -788,7 +798,6 @@ int MYSQLlex(void *arg, void *yythd) lip->start_token(); state=lip->next_state; lip->next_state=MY_LEX_OPERATOR_OR_IDENT; - LINT_INIT(c); for (;;) { switch (state) { @@ -917,7 +926,7 @@ int MYSQLlex(void *arg, void *yythd) else #endif { - for (result_state= c; ident_map[c= lip->yyGet()]; result_state|= c); + for (result_state= c; ident_map[c= lip->yyGet()]; result_state|= c) ; /* If there were non-ASCII characters, mark that we must convert */ result_state= result_state & 0x80 ? IDENT_QUOTED : IDENT; } @@ -929,7 +938,7 @@ int MYSQLlex(void *arg, void *yythd) If we find a space then this can't be an identifier. We notice this below by checking start != lex->ptr. */ - for (; state_map[c] == MY_LEX_SKIP ; c= lip->yyGet()); + for (; state_map[c] == MY_LEX_SKIP ; c= lip->yyGet()) ; } if (start == lip->get_ptr() && c == '.' && ident_map[lip->yyPeek()]) lip->next_state=MY_LEX_IDENT_SEP; @@ -1002,7 +1011,7 @@ int MYSQLlex(void *arg, void *yythd) } else if (c == 'b') { - while ((c= lip->yyGet()) == '0' || c == '1'); + while ((c= lip->yyGet()) == '0' || c == '1') ; if ((lip->yyLength() >= 3) && !ident_map[c]) { /* Skip '0b' */ @@ -1061,7 +1070,7 @@ int MYSQLlex(void *arg, void *yythd) else #endif { - for (result_state=0; ident_map[c= lip->yyGet()]; result_state|= c); + for (result_state=0; ident_map[c= lip->yyGet()]; result_state|= c) ; /* If there were non-ASCII characters, mark that we must convert */ result_state= result_state & 0x80 ? IDENT_QUOTED : IDENT; } @@ -1096,9 +1105,12 @@ int MYSQLlex(void *arg, void *yythd) } } #ifdef USE_MB - else if (var_length < 1) - break; // Error - lip->skip_binary(var_length-1); + else if (use_mb(cs)) + { + if ((var_length= my_ismbchar(cs, lip->get_ptr() - 1, + lip->get_end_of_query()))) + lip->skip_binary(var_length-1); + } #endif } if (double_quotes) @@ -1161,7 +1173,7 @@ int MYSQLlex(void *arg, void *yythd) case MY_LEX_BIN_NUMBER: // Found b'bin-string' lip->yySkip(); // Accept opening ' - while ((c= lip->yyGet()) == '0' || c == '1'); + while ((c= lip->yyGet()) == '0' || c == '1') ; if (c != '\'') return(ABORT_SYM); // Illegal hex constant lip->yySkip(); // Accept closing ' @@ -1436,7 +1448,7 @@ int MYSQLlex(void *arg, void *yythd) [(global | local | session) .]variable_name */ - for (result_state= 0; ident_map[c= lip->yyGet()]; result_state|= c); + for (result_state= 0; ident_map[c= lip->yyGet()]; result_state|= c) ; /* If there were non-ASCII characters, mark that we must convert */ result_state= result_state & 0x80 ? IDENT_QUOTED : IDENT; diff --git a/sql/sql_lex.h b/sql/sql_lex.h index 62106a2500b..76fd5354c51 100644 --- a/sql/sql_lex.h +++ b/sql/sql_lex.h @@ -1190,7 +1190,7 @@ public: Get a character, and advance in the stream. @return the next character to parse. */ - char yyGet() + unsigned char yyGet() { char c= *m_ptr++; if (m_echo) @@ -1202,7 +1202,7 @@ public: Get the last character accepted. @return the last character accepted. */ - char yyGetLast() + unsigned char yyGetLast() { return m_ptr[-1]; } @@ -1210,7 +1210,7 @@ public: /** Look at the next character to parse, but do not accept it. */ - char yyPeek() + unsigned char yyPeek() { return m_ptr[0]; } @@ -1219,7 +1219,7 @@ public: Look ahead at some character to parse. @param n offset of the character to look up */ - char yyPeekn(int n) + unsigned char yyPeekn(int n) { return m_ptr[n]; } @@ -1976,4 +1976,6 @@ extern bool is_lex_native_function(const LEX_STRING *name); @} (End of group Semantic_Analysis) */ +int my_missing_function_error(const LEX_STRING &token, const char *name); + #endif /* MYSQL_SERVER */ diff --git a/sql/sql_list.h b/sql/sql_list.h index 0d267111dad..22df77afeb3 100644 --- a/sql/sql_list.h +++ b/sql/sql_list.h @@ -86,7 +86,7 @@ struct list_node :public Sql_alloc }; -extern list_node end_of_list; +extern MYSQL_PLUGIN_IMPORT list_node end_of_list; class base_list :public Sql_alloc { diff --git a/sql/sql_load.cc b/sql/sql_load.cc index d4f499b8d44..b7f33d51335 100644 --- a/sql/sql_load.cc +++ b/sql/sql_load.cc @@ -86,7 +86,7 @@ static int read_sep_field(THD *thd, COPY_INFO &info, TABLE_LIST *table_list, static bool write_execute_load_query_log_event(THD *thd, bool duplicates, bool ignore, bool transactional_table, - THD::killed_state killed_status); + int errcode); #endif /* EMBEDDED_LIBRARY */ /* @@ -148,6 +148,17 @@ int mysql_load(THD *thd,sql_exchange *ex,TABLE_LIST *table_list, MYF(0)); DBUG_RETURN(TRUE); } + + /* Report problems with non-ascii separators */ + if (!escaped->is_ascii() || !enclosed->is_ascii() || + !field_term->is_ascii() || + !ex->line_term->is_ascii() || !ex->line_start->is_ascii()) + { + push_warning(thd, MYSQL_ERROR::WARN_LEVEL_WARN, + WARN_NON_ASCII_SEPARATOR_NOT_IMPLEMENTED, + ER(WARN_NON_ASCII_SEPARATOR_NOT_IMPLEMENTED)); + } + if (open_and_lock_tables(thd, table_list)) DBUG_RETURN(TRUE); if (setup_tables_and_check_access(thd, &thd->lex->select_lex.context, @@ -483,10 +494,12 @@ int mysql_load(THD *thd,sql_exchange *ex,TABLE_LIST *table_list, /* If the file was not empty, wrote_create_file is true */ if (lf_info.wrote_create_file) { + int errcode= query_error_code(thd, killed_status == THD::NOT_KILLED); + if (thd->transaction.stmt.modified_non_trans_table) write_execute_load_query_log_event(thd, handle_duplicates, ignore, transactional_table, - killed_status); + errcode); else { Delete_file_log_event d(thd, db, transactional_table); @@ -528,8 +541,9 @@ int mysql_load(THD *thd,sql_exchange *ex,TABLE_LIST *table_list, read_info.end_io_cache(); if (lf_info.wrote_create_file) { + int errcode= query_error_code(thd, killed_status == THD::NOT_KILLED); write_execute_load_query_log_event(thd, handle_duplicates, ignore, - transactional_table,killed_status); + transactional_table, errcode); } } } @@ -553,7 +567,7 @@ err: static bool write_execute_load_query_log_event(THD *thd, bool duplicates, bool ignore, bool transactional_table, - THD::killed_state killed_err_arg) + int errcode) { Execute_load_query_log_event e(thd, thd->query, thd->query_length, @@ -561,7 +575,7 @@ static bool write_execute_load_query_log_event(THD *thd, (uint) ((char*)thd->lex->fname_end - (char*)thd->query), (duplicates == DUP_REPLACE) ? LOAD_DUP_REPLACE : (ignore ? LOAD_DUP_IGNORE : LOAD_DUP_ERROR), - transactional_table, FALSE, killed_err_arg); + transactional_table, FALSE, errcode); e.flags|= LOG_EVENT_UPDATE_TABLE_MAP_VERSION_F; return mysql_bin_log.write(&e); } @@ -747,9 +761,9 @@ read_sep_field(THD *thd, COPY_INFO &info, TABLE_LIST *table_list, real_item= item->real_item(); - if (!read_info.enclosed && + if ((!read_info.enclosed && (enclosed_length && length == 4 && - !memcmp(pos, STRING_WITH_LEN("NULL"))) || + !memcmp(pos, STRING_WITH_LEN("NULL")))) || (length == 1 && read_info.found_null)) { @@ -1148,8 +1162,8 @@ int READ_INFO::read_field() } // End of enclosed field if followed by field_term or line_term if (chr == my_b_EOF || - chr == line_term_char && terminator(line_term_ptr, - line_term_length)) + (chr == line_term_char && terminator(line_term_ptr, + line_term_length))) { // Maybe unexpected linefeed enclosed=1; found_end_of_line=1; diff --git a/sql/sql_parse.cc b/sql/sql_parse.cc index 8f1d3842245..fb5d58b63c4 100644 --- a/sql/sql_parse.cc +++ b/sql/sql_parse.cc @@ -457,6 +457,7 @@ pthread_handler_t handle_bootstrap(void *arg) thd->init_for_queries(); while (fgets(buff, thd->net.max_packet, file)) { + char *query; /* strlen() can't be deleted because fgets() doesn't return length */ ulong length= (ulong) strlen(buff); while (buff[length-1] != '\n' && !feof(file)) @@ -489,11 +490,10 @@ pthread_handler_t handle_bootstrap(void *arg) if (strncmp(buff, STRING_WITH_LEN("delimiter")) == 0) continue; - thd->query_length=length; - thd->query= (char*) thd->memdup_w_gap(buff, length+1, - thd->db_length+1+ - QUERY_CACHE_FLAGS_SIZE); - thd->query[length] = '\0'; + query= (char *) thd->memdup_w_gap(buff, length + 1, + thd->db_length + 1 + + QUERY_CACHE_FLAGS_SIZE); + thd->set_query(query, length); DBUG_PRINT("query",("%-.4096s",thd->query)); #if defined(ENABLED_PROFILING) && defined(COMMUNITY_SERVER) thd->profiling.start_new_query(); @@ -533,8 +533,9 @@ end: #ifndef EMBEDDED_LIBRARY (void) pthread_mutex_lock(&LOCK_thread_count); thread_count--; - (void) pthread_mutex_unlock(&LOCK_thread_count); + in_bootstrap= FALSE; (void) pthread_cond_broadcast(&COND_thread_count); + (void) pthread_mutex_unlock(&LOCK_thread_count); my_thread_end(); pthread_exit(0); #endif @@ -658,8 +659,7 @@ int mysql_table_dump(THD *thd, LEX_STRING *db, char *tbl_name) if (check_one_table_access(thd, SELECT_ACL, table_list)) goto err; thd->free_list = 0; - thd->query_length=(uint) strlen(tbl_name); - thd->query = tbl_name; + thd->set_query(tbl_name, (uint) strlen(tbl_name)); if ((error = mysqld_dump_create_info(thd, table_list, -1))) { my_error(ER_GET_ERRNO, MYF(0), my_errno); @@ -1165,12 +1165,12 @@ bool dispatch_command(enum enum_server_command command, THD *thd, } case COM_STMT_EXECUTE: { - mysql_stmt_execute(thd, packet, packet_length); + mysqld_stmt_execute(thd, packet, packet_length); break; } case COM_STMT_FETCH: { - mysql_stmt_fetch(thd, packet, packet_length); + mysqld_stmt_fetch(thd, packet, packet_length); break; } case COM_STMT_SEND_LONG_DATA: @@ -1180,17 +1180,17 @@ bool dispatch_command(enum enum_server_command command, THD *thd, } case COM_STMT_PREPARE: { - mysql_stmt_prepare(thd, packet, packet_length); + mysqld_stmt_prepare(thd, packet, packet_length); break; } case COM_STMT_CLOSE: { - mysql_stmt_close(thd, packet); + mysqld_stmt_close(thd, packet); break; } case COM_STMT_RESET: { - mysql_stmt_reset(thd, packet); + mysqld_stmt_reset(thd, packet); break; } case COM_QUERY: @@ -1239,9 +1239,8 @@ bool dispatch_command(enum enum_server_command command, THD *thd, thd->profiling.set_query_source(beginning_of_next_stmt, length); #endif + thd->set_query(beginning_of_next_stmt, length); VOID(pthread_mutex_lock(&LOCK_thread_count)); - thd->query_length= length; - thd->query= beginning_of_next_stmt; /* Count each statement from the client. */ @@ -1294,9 +1293,10 @@ bool dispatch_command(enum enum_server_command command, THD *thd, table_list.schema_table= schema_table; } - thd->query_length= (uint) (packet_end - packet); // Don't count end \0 - if (!(thd->query=fields= (char*) thd->memdup(packet,thd->query_length+1))) + uint query_length= (uint) (packet_end - packet); // Don't count end \0 + if (!(fields= (char *) thd->memdup(packet, query_length + 1))) break; + thd->set_query(fields, query_length); general_log_print(thd, command, "%s %s", table_list.table_name, fields); if (lower_case_table_names) my_casedn_str(files_charset_info, table_list.table_name); @@ -1350,7 +1350,7 @@ bool dispatch_command(enum enum_server_command command, THD *thd, if (check_access(thd, CREATE_ACL, db.str , 0, 1, 0, is_schema_db(db.str))) break; - general_log_print(thd, command, packet); + general_log_print(thd, command, "%.*s", db.length, db.str); bzero(&create_info, sizeof(create_info)); mysql_create_db(thd, (lower_case_table_names == 2 ? alias.str : db.str), &create_info, 0); @@ -1375,7 +1375,7 @@ bool dispatch_command(enum enum_server_command command, THD *thd, ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0)); break; } - general_log_write(thd, command, db.str, db.length); + general_log_write(thd, command, "%.*s", db.length, db.str); mysql_rm_db(thd, db.str, 0, 0); break; } @@ -1560,14 +1560,6 @@ bool dispatch_command(enum enum_server_command command, THD *thd, break; } - /* If commit fails, we should be able to reset the OK status. */ - thd->main_da.can_overwrite_status= TRUE; - ha_autocommit_or_rollback(thd, thd->is_error()); - thd->main_da.can_overwrite_status= FALSE; - - thd->transaction.stmt.reset(); - - /* report error issued during command execution */ if (thd->killed_errno()) { @@ -1580,6 +1572,13 @@ bool dispatch_command(enum enum_server_command command, THD *thd, thd->mysys_var->abort= 0; } + /* If commit fails, we should be able to reset the OK status. */ + thd->main_da.can_overwrite_status= TRUE; + ha_autocommit_or_rollback(thd, thd->is_error()); + thd->main_da.can_overwrite_status= FALSE; + + thd->transaction.stmt.reset(); + net_end_statement(thd); query_cache_end_of_result(thd); @@ -1590,13 +1589,12 @@ bool dispatch_command(enum enum_server_command command, THD *thd, log_slow_statement(thd); thd_proc_info(thd, "cleaning up"); - VOID(pthread_mutex_lock(&LOCK_thread_count)); // For process list - thd_proc_info(thd, 0); + thd->set_query(NULL, 0); thd->command=COM_SLEEP; - thd->query=0; - thd->query_length=0; + VOID(pthread_mutex_lock(&LOCK_thread_count)); // For process list thread_running--; VOID(pthread_mutex_unlock(&LOCK_thread_count)); + thd_proc_info(thd, 0); thd->packet.shrink(thd->variables.net_buffer_length); // Reclaim some memory free_root(thd->mem_root,MYF(MY_KEEP_PREALLOC)); DBUG_RETURN(error); @@ -1789,6 +1787,7 @@ int prepare_schema_table(THD *thd, LEX *lex, Table_ident *table_ident, bool alloc_query(THD *thd, const char *packet, uint packet_length) { + char *query; /* Remove garbage at start and end of query */ while (packet_length > 0 && my_isspace(thd->charset(), packet[0])) { @@ -1803,14 +1802,13 @@ bool alloc_query(THD *thd, const char *packet, uint packet_length) packet_length--; } /* We must allocate some extra memory for query cache */ - thd->query_length= 0; // Extra safety: Avoid races - if (!(thd->query= (char*) thd->memdup_w_gap((uchar*) (packet), - packet_length, - thd->db_length+ 1 + - QUERY_CACHE_FLAGS_SIZE))) - return TRUE; - thd->query[packet_length]=0; - thd->query_length= packet_length; + if (! (query= (char*) thd->memdup_w_gap(packet, + packet_length, + 1 + thd->db_length + + QUERY_CACHE_FLAGS_SIZE))) + return TRUE; + query[packet_length]= '\0'; + thd->set_query(query, packet_length); /* Reclaim some memory */ thd->packet.shrink(thd->variables.net_buffer_length); @@ -3880,7 +3878,9 @@ end_with_restore_list: res= mysql_routine_grant(thd, all_tables, lex->type == TYPE_ENUM_PROCEDURE, lex->users_list, grants, - lex->sql_command == SQLCOM_REVOKE, 0); + lex->sql_command == SQLCOM_REVOKE, TRUE); + if (!res) + my_ok(thd); } else { @@ -5169,7 +5169,7 @@ check_access(THD *thd, ulong want_access, const char *db, ulong *save_priv, if (schema_db) { - if (!(sctx->master_access & FILE_ACL) && (want_access & FILE_ACL) || + if ((!(sctx->master_access & FILE_ACL) && (want_access & FILE_ACL)) || (want_access & ~(SELECT_ACL | EXTRA_ACL | FILE_ACL))) { if (!no_errors) @@ -5203,7 +5203,7 @@ check_access(THD *thd, ulong want_access, const char *db, ulong *save_priv, DBUG_RETURN(FALSE); } if (((want_access & ~sctx->master_access) & ~(DB_ACLS | EXTRA_ACL)) || - ! db && dont_check_global_grants) + (! db && dont_check_global_grants)) { // We can never grant this DBUG_PRINT("error",("No possible access")); if (!no_errors) @@ -5481,7 +5481,7 @@ bool check_some_access(THD *thd, ulong want_access, TABLE_LIST *table) if (!check_access(thd, access, table->db, &table->grant.privilege, 0, 1, test(table->schema_table)) && - !check_grant(thd, access, table, 0, 1, 1)) + !check_grant(thd, access, table, 0, 1, 1)) DBUG_RETURN(0); } } @@ -5737,7 +5737,7 @@ mysql_new_select(LEX *lex, bool move_down) /* Don't evaluate this subquery during statement prepare even if it's a constant one. The flag is switched off in the end of - mysql_stmt_prepare. + mysqld_stmt_prepare. */ if (thd->stmt_arena->is_stmt_prepare()) select_lex->uncacheable|= UNCACHEABLE_PREPARE; @@ -6949,7 +6949,7 @@ uint kill_one_thread(THD *thd, ulong id, bool only_kill_query) continue; if (tmp->thread_id == id) { - pthread_mutex_lock(&tmp->LOCK_delete); // Lock from delete + pthread_mutex_lock(&tmp->LOCK_thd_data); // Lock from delete break; } } @@ -6982,7 +6982,7 @@ uint kill_one_thread(THD *thd, ulong id, bool only_kill_query) } else error=ER_KILL_DENIED_ERROR; - pthread_mutex_unlock(&tmp->LOCK_delete); + pthread_mutex_unlock(&tmp->LOCK_thd_data); } DBUG_PRINT("exit", ("%d", error)); DBUG_RETURN(error); @@ -7801,7 +7801,7 @@ bool parse_sql(THD *thd, /* Check that if MYSQLparse() failed, thd->is_error() is set. */ DBUG_ASSERT(!mysql_parse_status || - mysql_parse_status && thd->is_error()); + (mysql_parse_status && thd->is_error())); /* Reset parser state. */ diff --git a/sql/sql_partition.cc b/sql/sql_partition.cc index 4a50650b6f4..a181a6b3f13 100644 --- a/sql/sql_partition.cc +++ b/sql/sql_partition.cc @@ -918,6 +918,9 @@ bool fix_fields_part_func(THD *thd, Item* func_expr, TABLE *table, Set-up the TABLE_LIST object to be a list with a single table Set the object to zero to create NULL pointers and set alias and real name to table name and get database name from file name. + TODO: Consider generalizing or refactoring Lex::add_table_to_list() so + it can be used in all places where we create TABLE_LIST objects. + Also consider creating appropriate constructors for TABLE_LIST. */ bzero((void*)&tables, sizeof(TABLE_LIST)); @@ -925,6 +928,13 @@ bool fix_fields_part_func(THD *thd, Item* func_expr, TABLE *table, tables.table= table; tables.next_local= 0; tables.next_name_resolution_table= 0; + /* + Cache the table in Item_fields. All the tables can be cached except + the trigger pseudo table. + */ + tables.cacheable_table= TRUE; + context= thd->lex->current_context(); + tables.select_lex= context->select_lex; strmov(db_name_string, table->s->normalized_path.str); dir_length= dirname_length(db_name_string); db_name_string[dir_length - 1]= 0; @@ -932,7 +942,6 @@ bool fix_fields_part_func(THD *thd, Item* func_expr, TABLE *table, db_name= &db_name_string[home_dir_length]; tables.db= db_name; - context= thd->lex->current_context(); table->map= 1; //To ensure correct calculation of const item table->get_fields_in_item_tree= TRUE; save_table_list= context->table_list; @@ -964,8 +973,9 @@ bool fix_fields_part_func(THD *thd, Item* func_expr, TABLE *table, save_use_only_table_context= thd->lex->use_only_table_context; thd->lex->use_only_table_context= TRUE; + thd->lex->current_select->cur_pos_in_select_list= UNDEF_POS; - error= func_expr->fix_fields(thd, (Item**)0); + error= func_expr->fix_fields(thd, (Item**)&func_expr); thd->lex->use_only_table_context= save_use_only_table_context; @@ -3814,8 +3824,13 @@ bool mysql_unpack_partition(THD *thd, Item_field objects. This is not a nice solution since if the parser uses current_select for anything else it will corrupt the current LEX object. + Also, we need to make sure there even is a select -- if the statement + was a "USE ...", current_select will be NULL, but we may still end up + here if we try to log to a partitioned table. This is currently + unsupported, but should still fail rather than crash! */ - thd->lex->current_select= old_lex->current_select; + if (!(thd->lex->current_select= old_lex->current_select)) + goto end; /* All Items created is put into a free list on the THD object. This list is used to free all Item objects after completing a query. We don't @@ -5090,7 +5105,7 @@ static bool mysql_change_partitions(ALTER_PARTITION_PARAM_TYPE *lpt) handler *file= lpt->table->file; DBUG_ENTER("mysql_change_partitions"); - build_table_filename(path, sizeof(path), lpt->db, lpt->table_name, "", 0); + build_table_filename(path, sizeof(path) - 1, lpt->db, lpt->table_name, "", 0); if ((error= file->ha_change_partitions(lpt->create_info, path, &lpt->copied, &lpt->deleted, lpt->pack_frm_data, lpt->pack_frm_len))) @@ -5130,7 +5145,7 @@ static bool mysql_rename_partitions(ALTER_PARTITION_PARAM_TYPE *lpt) int error; DBUG_ENTER("mysql_rename_partitions"); - build_table_filename(path, sizeof(path), lpt->db, lpt->table_name, "", 0); + build_table_filename(path, sizeof(path) - 1, lpt->db, lpt->table_name, "", 0); if ((error= lpt->table->file->ha_rename_partitions(path))) { if (error != 1) @@ -5171,7 +5186,7 @@ static bool mysql_drop_partitions(ALTER_PARTITION_PARAM_TYPE *lpt) int error; DBUG_ENTER("mysql_drop_partitions"); - build_table_filename(path, sizeof(path), lpt->db, lpt->table_name, "", 0); + build_table_filename(path, sizeof(path) - 1, lpt->db, lpt->table_name, "", 0); if ((error= lpt->table->file->ha_drop_partitions(path))) { lpt->table->file->print_error(error, MYF(0)); @@ -5516,10 +5531,10 @@ static bool write_log_drop_shadow_frm(ALTER_PARTITION_PARAM_TYPE *lpt) partition_info *part_info= lpt->part_info; DDL_LOG_MEMORY_ENTRY *log_entry; DDL_LOG_MEMORY_ENTRY *exec_log_entry= NULL; - char shadow_path[FN_REFLEN]; + char shadow_path[FN_REFLEN + 1]; DBUG_ENTER("write_log_drop_shadow_frm"); - build_table_shadow_filename(shadow_path, sizeof(shadow_path), lpt); + build_table_shadow_filename(shadow_path, sizeof(shadow_path) - 1, lpt); pthread_mutex_lock(&LOCK_gdl); if (write_log_replace_delete_frm(lpt, 0UL, NULL, (const char*)shadow_path, FALSE)) @@ -5559,15 +5574,15 @@ static bool write_log_rename_frm(ALTER_PARTITION_PARAM_TYPE *lpt) partition_info *part_info= lpt->part_info; DDL_LOG_MEMORY_ENTRY *log_entry; DDL_LOG_MEMORY_ENTRY *exec_log_entry= part_info->exec_log_entry; - char path[FN_REFLEN]; - char shadow_path[FN_REFLEN]; + char path[FN_REFLEN + 1]; + char shadow_path[FN_REFLEN + 1]; DDL_LOG_MEMORY_ENTRY *old_first_log_entry= part_info->first_log_entry; DBUG_ENTER("write_log_rename_frm"); part_info->first_log_entry= NULL; - build_table_filename(path, sizeof(path), lpt->db, + build_table_filename(path, sizeof(path) - 1, lpt->db, lpt->table_name, "", 0); - build_table_shadow_filename(shadow_path, sizeof(shadow_path), lpt); + build_table_shadow_filename(shadow_path, sizeof(shadow_path) - 1, lpt); pthread_mutex_lock(&LOCK_gdl); if (write_log_replace_delete_frm(lpt, 0UL, shadow_path, path, TRUE)) goto error; @@ -5610,16 +5625,16 @@ static bool write_log_drop_partition(ALTER_PARTITION_PARAM_TYPE *lpt) partition_info *part_info= lpt->part_info; DDL_LOG_MEMORY_ENTRY *log_entry; DDL_LOG_MEMORY_ENTRY *exec_log_entry= part_info->exec_log_entry; - char tmp_path[FN_REFLEN]; - char path[FN_REFLEN]; + char tmp_path[FN_REFLEN + 1]; + char path[FN_REFLEN + 1]; uint next_entry= 0; DDL_LOG_MEMORY_ENTRY *old_first_log_entry= part_info->first_log_entry; DBUG_ENTER("write_log_drop_partition"); part_info->first_log_entry= NULL; - build_table_filename(path, sizeof(path), lpt->db, + build_table_filename(path, sizeof(path) - 1, lpt->db, lpt->table_name, "", 0); - build_table_filename(tmp_path, sizeof(tmp_path), lpt->db, + build_table_filename(tmp_path, sizeof(tmp_path) - 1, lpt->db, lpt->table_name, "#", 0); pthread_mutex_lock(&LOCK_gdl); if (write_log_dropped_partitions(lpt, &next_entry, (const char*)path, @@ -5669,14 +5684,14 @@ static bool write_log_add_change_partition(ALTER_PARTITION_PARAM_TYPE *lpt) partition_info *part_info= lpt->part_info; DDL_LOG_MEMORY_ENTRY *log_entry; DDL_LOG_MEMORY_ENTRY *exec_log_entry= NULL; - char tmp_path[FN_REFLEN]; - char path[FN_REFLEN]; + char tmp_path[FN_REFLEN + 1]; + char path[FN_REFLEN + 1]; uint next_entry= 0; DBUG_ENTER("write_log_add_change_partition"); - build_table_filename(path, sizeof(path), lpt->db, + build_table_filename(path, sizeof(path) - 1, lpt->db, lpt->table_name, "", 0); - build_table_filename(tmp_path, sizeof(tmp_path), lpt->db, + build_table_filename(tmp_path, sizeof(tmp_path) - 1, lpt->db, lpt->table_name, "#", 0); pthread_mutex_lock(&LOCK_gdl); if (write_log_dropped_partitions(lpt, &next_entry, (const char*)path, @@ -5723,16 +5738,16 @@ static bool write_log_final_change_partition(ALTER_PARTITION_PARAM_TYPE *lpt) partition_info *part_info= lpt->part_info; DDL_LOG_MEMORY_ENTRY *log_entry; DDL_LOG_MEMORY_ENTRY *exec_log_entry= part_info->exec_log_entry; - char path[FN_REFLEN]; - char shadow_path[FN_REFLEN]; + char path[FN_REFLEN + 1]; + char shadow_path[FN_REFLEN + 1]; DDL_LOG_MEMORY_ENTRY *old_first_log_entry= part_info->first_log_entry; uint next_entry= 0; DBUG_ENTER("write_log_final_change_partition"); part_info->first_log_entry= NULL; - build_table_filename(path, sizeof(path), lpt->db, + build_table_filename(path, sizeof(path) - 1, lpt->db, lpt->table_name, "", 0); - build_table_shadow_filename(shadow_path, sizeof(shadow_path), lpt); + build_table_shadow_filename(shadow_path, sizeof(shadow_path) - 1, lpt); pthread_mutex_lock(&LOCK_gdl); if (write_log_dropped_partitions(lpt, &next_entry, (const char*)path, lpt->alter_info->flags & ALTER_REORGANIZE_PARTITION)) diff --git a/sql/sql_prepare.cc b/sql/sql_prepare.cc index cc7ea75dbbf..c1839b7220f 100644 --- a/sql/sql_prepare.cc +++ b/sql/sql_prepare.cc @@ -127,12 +127,12 @@ class Prepared_statement: public Statement public: enum flag_values { - IS_IN_USE= 1 + IS_IN_USE= 1, + IS_SQL_PREPARE= 2 }; THD *thd; Select_fetch_protocol_binary result; - Protocol *protocol; Item_param **param_array; uint param_count; uint last_errno; @@ -148,7 +148,7 @@ public: List<LEX_STRING>& varnames, String *expanded_query); public: - Prepared_statement(THD *thd_arg, Protocol *protocol_arg); + Prepared_statement(THD *thd_arg); virtual ~Prepared_statement(); void setup_set_params(); virtual Query_arena::Type type() const; @@ -156,7 +156,8 @@ public: bool set_name(LEX_STRING *name); inline void close_cursor() { delete cursor; cursor= 0; } inline bool is_in_use() { return flags & (uint) IS_IN_USE; } - inline bool is_protocol_text() const { return protocol == &thd->protocol_text; } + inline bool is_sql_prepare() const { return flags & (uint) IS_SQL_PREPARE; } + void set_sql_prepare() { flags|= (uint) IS_SQL_PREPARE; } bool prepare(const char *packet, uint packet_length); bool execute_loop(String *expanded_query, bool open_cursor, @@ -716,9 +717,9 @@ static void setup_one_conversion_function(THD *thd, Item_param *param, prepared statement, parameter markers are replaced with variable names. Example: @verbatim - mysql_stmt_prepare("UPDATE t1 SET a=a*1.25 WHERE a=?") + mysqld_stmt_prepare("UPDATE t1 SET a=a*1.25 WHERE a=?") --> general logs gets [Prepare] UPDATE t1 SET a*1.25 WHERE a=?" - mysql_stmt_execute(stmt); + mysqld_stmt_execute(stmt); --> general and binary logs get [Execute] UPDATE t1 SET a*1.25 WHERE a=1" @endverbatim @@ -1358,7 +1359,7 @@ static int mysql_test_select(Prepared_statement *stmt, */ if (unit->prepare(thd, 0, 0)) goto error; - if (!lex->describe && !stmt->is_protocol_text()) + if (!lex->describe && !stmt->is_sql_prepare()) { /* Make copy of item list, as change_columns may change it */ List<Item> fields(lex->select_lex.item_list); @@ -1434,8 +1435,8 @@ static bool mysql_test_set_fields(Prepared_statement *stmt, THD *thd= stmt->thd; set_var_base *var; - if (tables && check_table_access(thd, SELECT_ACL, tables, UINT_MAX, FALSE) || - open_normal_and_derived_tables(thd, tables, 0)) + if ((tables && check_table_access(thd, SELECT_ACL, tables, UINT_MAX, FALSE)) + || open_normal_and_derived_tables(thd, tables, 0)) goto error; while ((var= it++)) @@ -1470,13 +1471,13 @@ static bool mysql_test_call_fields(Prepared_statement *stmt, THD *thd= stmt->thd; Item *item; - if (tables && check_table_access(thd, SELECT_ACL, tables, UINT_MAX, FALSE) || + if ((tables && check_table_access(thd, SELECT_ACL, tables, UINT_MAX, FALSE)) || open_normal_and_derived_tables(thd, tables, 0)) goto err; while ((item= it++)) { - if (!item->fixed && item->fix_fields(thd, it.ref()) || + if ((!item->fixed && item->fix_fields(thd, it.ref())) || item->check_cols(1)) goto err; } @@ -1988,7 +1989,7 @@ static bool check_prepared_statement(Prepared_statement *stmt) break; } if (res == 0) - DBUG_RETURN(stmt->is_protocol_text() ? + DBUG_RETURN(stmt->is_sql_prepare() ? FALSE : (send_prep_stmt(stmt, 0) || thd->protocol->flush())); error: DBUG_RETURN(TRUE); @@ -2056,18 +2057,19 @@ static bool init_param_array(Prepared_statement *stmt) to the client, otherwise an error message is set in THD. */ -void mysql_stmt_prepare(THD *thd, const char *packet, uint packet_length) +void mysqld_stmt_prepare(THD *thd, const char *packet, uint packet_length) { + Protocol *save_protocol= thd->protocol; Prepared_statement *stmt; bool error; - DBUG_ENTER("mysql_stmt_prepare"); + DBUG_ENTER("mysqld_stmt_prepare"); DBUG_PRINT("prep_query", ("%s", packet)); /* First of all clear possible warnings from the previous command */ mysql_reset_thd_for_next_command(thd); - if (! (stmt= new Prepared_statement(thd, &thd->protocol_binary))) + if (! (stmt= new Prepared_statement(thd))) DBUG_VOID_RETURN; /* out of memory: error is set in Sql_alloc */ if (thd->stmt_map.insert(thd, stmt)) @@ -2084,6 +2086,8 @@ void mysql_stmt_prepare(THD *thd, const char *packet, uint packet_length) sp_cache_flush_obsolete(&thd->sp_proc_cache); sp_cache_flush_obsolete(&thd->sp_func_cache); + thd->protocol= &thd->protocol_binary; + if (!(specialflag & SPECIAL_NO_PRIOR)) my_pthread_setprio(pthread_self(),QUERY_PRIOR); @@ -2097,6 +2101,9 @@ void mysql_stmt_prepare(THD *thd, const char *packet, uint packet_length) /* Statement map deletes statement on erase */ thd->stmt_map.erase(stmt); } + + thd->protocol= save_protocol; + /* check_prepared_statemnt sends the metadata packet in case of success */ DBUG_VOID_RETURN; } @@ -2227,10 +2234,8 @@ void mysql_sql_stmt_prepare(THD *thd) LEX_STRING *name= &lex->prepared_stmt_name; Prepared_statement *stmt; const char *query; - uint query_len; + uint query_len= 0; DBUG_ENTER("mysql_sql_stmt_prepare"); - LINT_INIT(query_len); - DBUG_ASSERT(thd->protocol == &thd->protocol_text); if ((stmt= (Prepared_statement*) thd->stmt_map.find_by_name(name))) { @@ -2248,11 +2253,13 @@ void mysql_sql_stmt_prepare(THD *thd) } if (! (query= get_dynamic_sql_string(lex, &query_len)) || - ! (stmt= new Prepared_statement(thd, &thd->protocol_text))) + ! (stmt= new Prepared_statement(thd))) { DBUG_VOID_RETURN; /* out of memory */ } + stmt->set_sql_prepare(); + /* Set the name first, insert should know that this statement has a name */ if (stmt->set_name(name)) { @@ -2423,7 +2430,7 @@ static void reset_stmt_params(Prepared_statement *stmt) client, otherwise an error message is set in THD. */ -void mysql_stmt_execute(THD *thd, char *packet_arg, uint packet_length) +void mysqld_stmt_execute(THD *thd, char *packet_arg, uint packet_length) { uchar *packet= (uchar*)packet_arg; // GCC 4.0.1 workaround ulong stmt_id= uint4korr(packet); @@ -2432,8 +2439,9 @@ void mysql_stmt_execute(THD *thd, char *packet_arg, uint packet_length) String expanded_query; uchar *packet_end= packet + packet_length; Prepared_statement *stmt; + Protocol *save_protocol= thd->protocol; bool open_cursor; - DBUG_ENTER("mysql_stmt_execute"); + DBUG_ENTER("mysqld_stmt_execute"); packet+= 9; /* stmt_id + 5 bytes of flags */ @@ -2444,7 +2452,7 @@ void mysql_stmt_execute(THD *thd, char *packet_arg, uint packet_length) { char llbuf[22]; my_error(ER_UNKNOWN_STMT_HANDLER, MYF(0), sizeof(llbuf), - llstr(stmt_id, llbuf), "mysql_stmt_execute"); + llstr(stmt_id, llbuf), "mysqld_stmt_execute"); DBUG_VOID_RETURN; } @@ -2459,7 +2467,9 @@ void mysql_stmt_execute(THD *thd, char *packet_arg, uint packet_length) open_cursor= test(flags & (ulong) CURSOR_TYPE_READ_ONLY); + thd->protocol= &thd->protocol_binary; stmt->execute_loop(&expanded_query, open_cursor, packet, packet_end); + thd->protocol= save_protocol; /* Close connection socket; for use with client testing (Bug#43560). */ DBUG_EXECUTE_IF("close_conn_after_stmt_execute", vio_close(thd->net.vio);); @@ -2525,7 +2535,7 @@ void mysql_sql_stmt_execute(THD *thd) @param packet_length Length of packet */ -void mysql_stmt_fetch(THD *thd, char *packet, uint packet_length) +void mysqld_stmt_fetch(THD *thd, char *packet, uint packet_length) { /* assume there is always place for 8-16 bytes */ ulong stmt_id= uint4korr(packet); @@ -2533,7 +2543,7 @@ void mysql_stmt_fetch(THD *thd, char *packet, uint packet_length) Prepared_statement *stmt; Statement stmt_backup; Server_side_cursor *cursor; - DBUG_ENTER("mysql_stmt_fetch"); + DBUG_ENTER("mysqld_stmt_fetch"); /* First of all clear possible warnings from the previous command */ mysql_reset_thd_for_next_command(thd); @@ -2542,7 +2552,7 @@ void mysql_stmt_fetch(THD *thd, char *packet, uint packet_length) { char llbuf[22]; my_error(ER_UNKNOWN_STMT_HANDLER, MYF(0), sizeof(llbuf), - llstr(stmt_id, llbuf), "mysql_stmt_fetch"); + llstr(stmt_id, llbuf), "mysqld_stmt_fetch"); DBUG_VOID_RETURN; } @@ -2583,9 +2593,9 @@ void mysql_stmt_fetch(THD *thd, char *packet, uint packet_length) This function resets statement to the state it was right after prepare. It can be used to: - - clear an error happened during mysql_stmt_send_long_data + - clear an error happened during mysqld_stmt_send_long_data - cancel long data stream for all placeholders without - having to call mysql_stmt_execute. + having to call mysqld_stmt_execute. - close an open cursor Sends 'OK' packet in case of success (statement was reset) or 'ERROR' packet (unrecoverable error/statement not found/etc). @@ -2594,12 +2604,12 @@ void mysql_stmt_fetch(THD *thd, char *packet, uint packet_length) @param packet Packet with stmt id */ -void mysql_stmt_reset(THD *thd, char *packet) +void mysqld_stmt_reset(THD *thd, char *packet) { /* There is always space for 4 bytes in buffer */ ulong stmt_id= uint4korr(packet); Prepared_statement *stmt; - DBUG_ENTER("mysql_stmt_reset"); + DBUG_ENTER("mysqld_stmt_reset"); /* First of all clear possible warnings from the previous command */ mysql_reset_thd_for_next_command(thd); @@ -2609,7 +2619,7 @@ void mysql_stmt_reset(THD *thd, char *packet) { char llbuf[22]; my_error(ER_UNKNOWN_STMT_HANDLER, MYF(0), sizeof(llbuf), - llstr(stmt_id, llbuf), "mysql_stmt_reset"); + llstr(stmt_id, llbuf), "mysqld_stmt_reset"); DBUG_VOID_RETURN; } @@ -2617,7 +2627,7 @@ void mysql_stmt_reset(THD *thd, char *packet) /* Clear parameters from data which could be set by - mysql_stmt_send_long_data() call. + mysqld_stmt_send_long_data() call. */ reset_stmt_params(stmt); @@ -2638,12 +2648,12 @@ void mysql_stmt_reset(THD *thd, char *packet) we don't send any reply to this command. */ -void mysql_stmt_close(THD *thd, char *packet) +void mysqld_stmt_close(THD *thd, char *packet) { /* There is always space for 4 bytes in packet buffer */ ulong stmt_id= uint4korr(packet); Prepared_statement *stmt; - DBUG_ENTER("mysql_stmt_close"); + DBUG_ENTER("mysqld_stmt_close"); thd->main_da.disable_status(); @@ -2742,7 +2752,7 @@ void mysql_stmt_get_longdata(THD *thd, char *packet, ulong packet_length) stmt->state= Query_arena::ERROR; stmt->last_errno= ER_WRONG_ARGUMENTS; sprintf(stmt->last_error, ER(ER_WRONG_ARGUMENTS), - "mysql_stmt_send_long_data"); + "mysqld_stmt_send_long_data"); DBUG_VOID_RETURN; } #endif @@ -2815,12 +2825,11 @@ Select_fetch_protocol_binary::send_data(List<Item> &fields) Prepared_statement ****************************************************************************/ -Prepared_statement::Prepared_statement(THD *thd_arg, Protocol *protocol_arg) +Prepared_statement::Prepared_statement(THD *thd_arg) :Statement(NULL, &main_mem_root, INITIALIZED, ++thd_arg->statement_id_counter), thd(thd_arg), result(thd_arg), - protocol(protocol_arg), param_array(0), param_count(0), last_errno(0), @@ -2846,7 +2855,7 @@ void Prepared_statement::setup_set_params() Decide if we have to expand the query (because we must write it to logs or because we want to look it up in the query cache) or not. */ - if (mysql_bin_log.is_open() && is_update_query(lex->sql_command) || + if ((mysql_bin_log.is_open() && is_update_query(lex->sql_command)) || opt_log || opt_slow_log || query_cache_is_cacheable_query(lex)) { @@ -3166,7 +3175,7 @@ Prepared_statement::set_parameters(String *expanded_query, if (res) { my_error(ER_WRONG_ARGUMENTS, MYF(0), - is_sql_ps ? "EXECUTE" : "mysql_stmt_execute"); + is_sql_ps ? "EXECUTE" : "mysqld_stmt_execute"); reset_stmt_params(this); } return res; @@ -3289,7 +3298,9 @@ Prepared_statement::reprepare() bool cur_db_changed; bool error; - Prepared_statement copy(thd, &thd->protocol_text); + Prepared_statement copy(thd); + + copy.set_sql_prepare(); /* To suppress sending metadata to the client. */ status_var_increment(thd->status_var.com_stmt_reprepare); @@ -3297,7 +3308,7 @@ Prepared_statement::reprepare() &cur_db_changed)) return TRUE; - error= (name.str && copy.set_name(&name) || + error= ((name.str && copy.set_name(&name)) || copy.prepare(query, query_length) || validate_metadata(©)); @@ -3347,7 +3358,7 @@ bool Prepared_statement::validate_metadata(Prepared_statement *copy) return FALSE -- the metadata of the original SELECT, if any, has not been sent to the client. */ - if (is_protocol_text() || lex->describe) + if (is_sql_prepare() || lex->describe) return FALSE; if (lex->select_lex.item_list.elements != @@ -3410,7 +3421,6 @@ Prepared_statement::swap_prepared_statement(Prepared_statement *copy) DBUG_ASSERT(thd == copy->thd); last_error[0]= '\0'; last_errno= 0; - /* Do not swap protocols, the copy always has protocol_text */ } @@ -3551,8 +3561,6 @@ bool Prepared_statement::execute(String *expanded_query, bool open_cursor) thd->stmt_arena= this; reinit_stmt_before_use(thd, lex); - thd->protocol= protocol; /* activate stmt protocol */ - /* Go! */ if (open_cursor) @@ -3583,8 +3591,6 @@ bool Prepared_statement::execute(String *expanded_query, bool open_cursor) if (cur_db_changed) mysql_change_db(thd, &saved_cur_db_name, TRUE); - thd->protocol= &thd->protocol_text; /* use normal protocol */ - /* Assert that if an error, no cursor is open */ DBUG_ASSERT(! (error && cursor)); @@ -3621,11 +3627,11 @@ error: } -/** Common part of DEALLOCATE PREPARE and mysql_stmt_close. */ +/** Common part of DEALLOCATE PREPARE and mysqld_stmt_close. */ void Prepared_statement::deallocate() { - /* We account deallocate in the same manner as mysql_stmt_close */ + /* We account deallocate in the same manner as mysqld_stmt_close */ status_var_increment(thd->status_var.com_stmt_close); /* Statement map calls delete stmt on erase */ thd->stmt_map.erase(this); diff --git a/sql/sql_profile.h b/sql/sql_profile.h index b5537487d26..245959e0953 100644 --- a/sql/sql_profile.h +++ b/sql/sql_profile.h @@ -16,25 +16,12 @@ #ifndef _SQL_PROFILE_H #define _SQL_PROFILE_H -#if __STDC_VERSION__ < 199901L -# if __GNUC__ >= 2 -# define __func__ __FUNCTION__ -# else -# define __func__ _unknown_func_ -extern const char * const _unknown_func_; -# endif -#elif defined(_MSC_VER) -# if _MSC_VER < 1300 -# define __func__ _unknown_func_ -extern const char * const _unknown_func_; -# else -# define __func__ __FUNCTION__ -# endif -#elif defined(__BORLANDC__) -# define __func__ __FUNC__ +#ifndef __func__ +#ifdef __FUNCTION__ +#define __func__ __FUNCTION__ #else -# define __func__ _unknown_func_ -extern const char * const _unknown_func_; +#define __func__ "unknown function" +#endif #endif extern ST_FIELD_INFO query_profile_statistics_info[]; diff --git a/sql/sql_rename.cc b/sql/sql_rename.cc index 7cc9130cc4a..0e0b8eb60b9 100644 --- a/sql/sql_rename.cc +++ b/sql/sql_rename.cc @@ -244,7 +244,7 @@ do_rename(THD *thd, TABLE_LIST *ren_table, char *new_db, char *new_table_name, char *new_table_alias, bool skip_error) { int rc= 1; - char name[FN_REFLEN]; + char name[FN_REFLEN + 1]; const char *new_alias, *old_alias; frm_type_enum frm_type; enum legacy_db_type table_type; @@ -263,14 +263,14 @@ do_rename(THD *thd, TABLE_LIST *ren_table, char *new_db, char *new_table_name, } DBUG_ASSERT(new_alias); - build_table_filename(name, sizeof(name), + build_table_filename(name, sizeof(name) - 1, new_db, new_alias, reg_ext, 0); if (!access(name,F_OK)) { my_error(ER_TABLE_EXISTS_ERROR, MYF(0), new_alias); DBUG_RETURN(1); // This can't be skipped } - build_table_filename(name, sizeof(name), + build_table_filename(name, sizeof(name) - 1, ren_table->db, old_alias, reg_ext, 0); frm_type= mysql_frm_type(thd, name, &table_type); diff --git a/sql/sql_repl.cc b/sql/sql_repl.cc index 06c6c022780..0ec8d91214c 100644 --- a/sql/sql_repl.cc +++ b/sql/sql_repl.cc @@ -147,7 +147,7 @@ static int send_file(THD *thd) if (errmsg) { sql_print_error("Failed in send_file() %s", errmsg); - DBUG_PRINT("error", (errmsg)); + DBUG_PRINT("error", ("%s", errmsg)); } DBUG_RETURN(error); } @@ -1043,6 +1043,7 @@ int reset_slave(THD *thd, Master_info* mi) Reset errors (the idea is that we forget about the old master). */ + mi->clear_error(); mi->rli.clear_error(); mi->rli.clear_until_condition(); @@ -1101,7 +1102,7 @@ void kill_zombie_dump_threads(uint32 slave_server_id) if (tmp->command == COM_BINLOG_DUMP && tmp->server_id == slave_server_id) { - pthread_mutex_lock(&tmp->LOCK_delete); // Lock from delete + pthread_mutex_lock(&tmp->LOCK_thd_data); // Lock from delete break; } } @@ -1114,7 +1115,7 @@ void kill_zombie_dump_threads(uint32 slave_server_id) again. We just to do kill the thread ourselves. */ tmp->awake(THD::KILL_QUERY); - pthread_mutex_unlock(&tmp->LOCK_delete); + pthread_mutex_unlock(&tmp->LOCK_thd_data); } } diff --git a/sql/sql_select.cc b/sql/sql_select.cc index fad533986d6..7f6c5e834a3 100644 --- a/sql/sql_select.cc +++ b/sql/sql_select.cc @@ -1350,7 +1350,7 @@ JOIN::optimize() join_tab[const_tables].type != JT_ALL && join_tab[const_tables].type != JT_FT && join_tab[const_tables].type != JT_REF_OR_NULL && - (order && simple_order || group_list && simple_group)) + ((order && simple_order) || (group_list && simple_group))) { if (add_ref_to_table_cond(thd,&join_tab[const_tables])) { DBUG_RETURN(1); @@ -1807,7 +1807,8 @@ JOIN::exec() curr_join->having= curr_join->tmp_having= 0; // Allready done /* Change sum_fields reference to calculated fields in tmp_table */ - curr_join->all_fields= *curr_all_fields; + if (curr_join != this) + curr_join->all_fields= *curr_all_fields; if (!items1) { items1= items0 + all_fields.elements; @@ -1826,8 +1827,11 @@ JOIN::exec() fields_list.elements, all_fields)) DBUG_VOID_RETURN; } - curr_join->tmp_all_fields1= tmp_all_fields1; - curr_join->tmp_fields_list1= tmp_fields_list1; + if (curr_join != this) + { + curr_join->tmp_all_fields1= tmp_all_fields1; + curr_join->tmp_fields_list1= tmp_fields_list1; + } curr_join->items1= items1; } curr_all_fields= &tmp_all_fields1; @@ -1868,9 +1872,9 @@ JOIN::exec() like SEC_TO_TIME(SUM(...)). */ - if (curr_join->group_list && (!test_if_subpart(curr_join->group_list, + if ((curr_join->group_list && (!test_if_subpart(curr_join->group_list, curr_join->order) || - curr_join->select_distinct) || + curr_join->select_distinct)) || (curr_join->select_distinct && curr_join->tmp_table_param.using_indirect_summary_function)) { /* Must copy to another table */ @@ -1975,8 +1979,11 @@ JOIN::exec() tmp_fields_list2, tmp_all_fields2, fields_list.elements, tmp_all_fields1)) DBUG_VOID_RETURN; - curr_join->tmp_fields_list2= tmp_fields_list2; - curr_join->tmp_all_fields2= tmp_all_fields2; + if (curr_join != this) + { + curr_join->tmp_fields_list2= tmp_fields_list2; + curr_join->tmp_all_fields2= tmp_all_fields2; + } } curr_fields_list= &curr_join->tmp_fields_list2; curr_all_fields= &curr_join->tmp_all_fields2; @@ -2031,8 +2038,11 @@ JOIN::exec() tmp_table_param.save_copy_field= curr_join->tmp_table_param.copy_field; tmp_table_param.save_copy_field_end= curr_join->tmp_table_param.copy_field_end; - curr_join->tmp_all_fields3= tmp_all_fields3; - curr_join->tmp_fields_list3= tmp_fields_list3; + if (curr_join != this) + { + curr_join->tmp_all_fields3= tmp_all_fields3; + curr_join->tmp_fields_list3= tmp_fields_list3; + } } else { @@ -2344,9 +2354,10 @@ mysql_select(THD *thd, Item ***rref_pointer_array, } else { - if (err= join->prepare(rref_pointer_array, tables, wild_num, - conds, og_num, order, group, having, proc_param, - select_lex, unit)) + err= join->prepare(rref_pointer_array, tables, wild_num, + conds, og_num, order, group, having, proc_param, + select_lex, unit); + if (err) { goto err; } @@ -2361,9 +2372,10 @@ mysql_select(THD *thd, Item ***rref_pointer_array, DBUG_RETURN(TRUE); thd_proc_info(thd, "init"); thd->used_tables=0; // Updated by setup_fields - if (err= join->prepare(rref_pointer_array, tables, wild_num, - conds, og_num, order, group, having, proc_param, - select_lex, unit)) + err= join->prepare(rref_pointer_array, tables, wild_num, + conds, og_num, order, group, having, proc_param, + select_lex, unit); + if (err) { goto err; } @@ -2430,7 +2442,6 @@ static ha_rows get_quick_record_count(THD *thd, SQL_SELECT *select, if (select) { select->head=table; - table->reginfo.impossible_range=0; if ((error= select->test_quick_select(thd, *(key_map *)keys,(table_map) 0, limit, 0)) == 1) DBUG_RETURN(select->quick->records); @@ -3841,7 +3852,7 @@ update_ref_and_keys(THD *thd, DYNAMIC_ARRAY *keyuse,JOIN_TAB *join_tab, if (use->key == prev->key && use->table == prev->table) { if (prev->keypart+1 < use->keypart || - prev->keypart == use->keypart && found_eq_constant) + (prev->keypart == use->keypart && found_eq_constant)) continue; /* remove */ } else if (use->keypart != 0) // First found must be 0 @@ -5146,8 +5157,8 @@ best_extension_by_limited_search(JOIN *join, { if (best_record_count > current_record_count || best_read_time > current_read_time || - idx == join->const_tables && // 's' is the first table in the QEP - s->table == join->sort_by_table) + (idx == join->const_tables && // 's' is the first table in the QEP + s->table == join->sort_by_table)) { if (best_record_count >= current_record_count && best_read_time >= current_read_time && @@ -5273,7 +5284,7 @@ find_best(JOIN *join,table_map rest_tables,uint idx,double record_count, double current_read_time=read_time+best; if (best_record_count > current_record_count || best_read_time > current_read_time || - idx == join->const_tables && s->table == join->sort_by_table) + (idx == join->const_tables && s->table == join->sort_by_table)) { if (best_record_count >= current_record_count && best_read_time >= current_read_time && @@ -6140,7 +6151,7 @@ make_join_select(JOIN *join,SQL_SELECT *select,COND *cond) } } - if (tmp || !cond) + if (tmp || !cond || tab->type == JT_REF) { DBUG_EXECUTE("where",print_where(tmp,tab->table->alias, QT_ORDINARY);); SQL_SELECT *sel= tab->select= ((SQL_SELECT*) @@ -6154,7 +6165,7 @@ make_join_select(JOIN *join,SQL_SELECT *select,COND *cond) The guard will turn the predicate on only after the first match for outer tables is encountered. */ - if (cond) + if (cond && tmp) { /* Because of QUICK_GROUP_MIN_MAX_SELECT there may be a select without @@ -6220,8 +6231,8 @@ make_join_select(JOIN *join,SQL_SELECT *select,COND *cond) the index if we are using limit and this is the first table */ - if (cond && - (!tab->keys.is_subset(tab->const_keys) && i > 0) || + if ((cond && + !tab->keys.is_subset(tab->const_keys) && i > 0) || (!tab->const_keys.is_clear_all() && i == join->const_tables && join->unit->select_limit_cnt < join->best_positions[i].records_read && @@ -7354,7 +7365,7 @@ static bool check_simple_equality(Item *left_item, Item *right_item, left_item_equal->merge(right_item_equal); /* Remove the merged multiple equality from the list */ List_iterator<Item_equal> li(cond_equal->current_level); - while ((li++) != right_item_equal); + while ((li++) != right_item_equal) ; li.remove(); } } @@ -9390,13 +9401,17 @@ static Field *create_tmp_field_from_item(THD *thd, Item *item, TABLE *table, +1: for decimal point */ - overflow= my_decimal_precision_to_length(intg + dec, dec, - item->unsigned_flag) - len; + const int required_length= + my_decimal_precision_to_length(intg + dec, dec, + item->unsigned_flag); + + overflow= required_length - len; if (overflow > 0) dec= max(0, dec - overflow); // too long, discard fract else - len -= item->decimals - dec; // corrected value fits + /* Corrected value fits. */ + len= required_length; } new_field= new Field_new_decimal(len, maybe_null, item->name, @@ -10032,9 +10047,9 @@ create_tmp_table(THD *thd,TMP_TABLE_PARAM *param,List<Item> &fields, reclength=1; // Dummy select /* Use packed rows if there is blobs or a lot of space to gain */ if (blob_count || - string_total_length >= STRING_TOTAL_LENGTH_TO_PACK_ROWS && + (string_total_length >= STRING_TOTAL_LENGTH_TO_PACK_ROWS && (reclength / string_total_length <= RATIO_TO_PACK_ROWS || - string_total_length / string_count >= AVG_STRING_LENGTH_TO_PACK_ROWS)) + string_total_length / string_count >= AVG_STRING_LENGTH_TO_PACK_ROWS))) use_packed_rows= 1; share->reclength= reclength; @@ -10602,6 +10617,9 @@ free_tmp_table(THD *thd, TABLE *entry) save_proc_info=thd->proc_info; thd_proc_info(thd, "removing tmp table"); + // Release latches since this can take a long time + ha_release_temporary_latches(thd); + if (entry->file) { if (entry->db_stat) @@ -10644,9 +10662,18 @@ bool create_myisam_from_heap(THD *thd, TABLE *table, TMP_TABLE_PARAM *param, if (table->s->db_type() != heap_hton || error != HA_ERR_RECORD_FILE_FULL) { + /* + We don't want this error to be converted to a warning, e.g. in case of + INSERT IGNORE ... SELECT. + */ + thd->fatal_error(); table->file->print_error(error,MYF(0)); DBUG_RETURN(1); } + + // Release latches since this can take a long time + ha_release_temporary_latches(thd); + new_table= *table; share= *table->s; new_table.s= &share; @@ -10828,9 +10855,8 @@ do_select(JOIN *join,List<Item> *fields,TABLE *table,Procedure *procedure) { int rc= 0; enum_nested_loop_state error= NESTED_LOOP_OK; - JOIN_TAB *join_tab; + JOIN_TAB *join_tab= NULL; DBUG_ENTER("do_select"); - LINT_INIT(join_tab); join->procedure=procedure; join->tmp_table= table; /* Save for easy recursion */ @@ -12651,7 +12677,10 @@ static int test_if_order_by_key(ORDER *order, TABLE *table, uint idx, one row). The sorting doesn't matter. */ if (key_part == key_part_end && reverse == 0) + { + *used_key_parts= 0; DBUG_RETURN(1); + } } else DBUG_RETURN(0); @@ -12917,6 +12946,8 @@ find_field_in_item_list (Field *field, void *data) The index must cover all fields in <order>, or it will not be considered. + @param no_changes No changes will be made to the query plan. + @todo - sergeyp: Results of all index merge selects actually are ordered by clustered PK values. @@ -13066,9 +13097,9 @@ test_if_skip_sort_order(JOIN_TAB *tab,ORDER *order,ha_rows select_limit, */ uint nr; key_map keys; - uint best_key_parts; - int best_key_direction; - ha_rows best_records; + uint best_key_parts= 0; + int best_key_direction= 0; + ha_rows best_records= 0; double read_time; int best_key= -1; bool is_best_covering= FALSE; @@ -13078,9 +13109,6 @@ test_if_skip_sort_order(JOIN_TAB *tab,ORDER *order,ha_rows select_limit, ha_rows table_records= table->file->stats.records; bool group= join->group && order == join->group_list; ha_rows ref_key_quick_rows= HA_POS_ERROR; - LINT_INIT(best_key_parts); - LINT_INIT(best_key_direction); - LINT_INIT(best_records); /* If not used with LIMIT, only use keys if the whole query can be @@ -13121,12 +13149,20 @@ test_if_skip_sort_order(JOIN_TAB *tab,ORDER *order,ha_rows select_limit, for (nr=0; nr < table->s->keys ; nr++) { int direction; + if (keys.is_set(nr) && (direction= test_if_order_by_key(order, table, nr, &used_key_parts))) { + /* + At this point we are sure that ref_key is a non-ordering + key (where "ordering key" is a key that will return rows + in the order required by ORDER BY). + */ + DBUG_ASSERT (ref_key != (int) nr); + bool is_covering= table->covering_keys.is_set(nr) || - nr == table->s->primary_key && - table->file->primary_key_is_clustered(); + (nr == table->s->primary_key && + table->file->primary_key_is_clustered()); /* Don't use an index scan with ORDER BY without limit. @@ -13139,7 +13175,7 @@ test_if_skip_sort_order(JOIN_TAB *tab,ORDER *order,ha_rows select_limit, */ if (is_covering || select_limit != HA_POS_ERROR || - ref_key < 0 && (group || table->force_index)) + (ref_key < 0 && (group || table->force_index))) { double rec_per_key; double index_scan_time; @@ -13148,7 +13184,8 @@ test_if_skip_sort_order(JOIN_TAB *tab,ORDER *order,ha_rows select_limit, select_limit= table_records; if (group) { - rec_per_key= keyinfo->rec_per_key[used_key_parts-1]; + rec_per_key= used_key_parts ? keyinfo->rec_per_key[used_key_parts-1] + : 1; set_if_bigger(rec_per_key, 1); /* With a grouping query each group containing on average @@ -13203,13 +13240,13 @@ test_if_skip_sort_order(JOIN_TAB *tab,ORDER *order,ha_rows select_limit, */ index_scan_time= select_limit/rec_per_key * min(rec_per_key, table->file->scan_time()); - if (is_covering || - ref_key < 0 && (group || table->force_index) || + if ((ref_key < 0 && is_covering) || + (ref_key < 0 && (group || table->force_index)) || index_scan_time < read_time) { ha_rows quick_records= table_records; - if (is_best_covering && !is_covering || - is_covering && ref_key_quick_rows < select_limit) + if ((is_best_covering && !is_covering) || + (is_covering && ref_key_quick_rows < select_limit)) continue; if (table->quick_keys.is_set(nr)) quick_records= table->quick_rows[nr]; @@ -13245,6 +13282,15 @@ test_if_skip_sort_order(JOIN_TAB *tab,ORDER *order,ha_rows select_limit, } if (!no_changes) { + /* + If ref_key used index tree reading only ('Using index' in EXPLAIN), + and best_key doesn't, then revert the decision. + */ + if (!table->covering_keys.is_set(best_key) && table->key_read) + { + table->key_read= 0; + table->file->extra(HA_EXTRA_NO_KEYREAD); + } if (!quick_created) { tab->index= best_key; @@ -13261,16 +13307,6 @@ test_if_skip_sort_order(JOIN_TAB *tab,ORDER *order,ha_rows select_limit, table->key_read=1; table->file->extra(HA_EXTRA_KEYREAD); } - else if (table->key_read) - { - /* - Clear the covering key read flags that might have been - previously set for some key other than the current best_key. - */ - table->key_read= 0; - table->file->extra(HA_EXTRA_NO_KEYREAD); - } - table->file->ha_index_or_rnd_end(); if (join->select_options & SELECT_DESCRIBE) { @@ -13417,8 +13453,8 @@ create_sort_index(THD *thd, JOIN *join, ORDER *order, */ if ((order != join->group_list || !(join->select_options & SELECT_BIG_RESULT) || - select && select->quick && - select->quick->get_type() == QUICK_SELECT_I::QS_TYPE_GROUP_MIN_MAX) && + (select && select->quick && + select->quick->get_type() == QUICK_SELECT_I::QS_TYPE_GROUP_MIN_MAX)) && test_if_skip_sort_order(tab,order,select_limit,0, is_order_by ? &table->keys_in_use_for_order_by : &table->keys_in_use_for_group_by)) @@ -13479,9 +13515,24 @@ create_sort_index(THD *thd, JOIN *join, ORDER *order, tab->records= table->sort.found_records; // For SQL_CALC_ROWS if (select) { + /* + We need to preserve tablesort's output resultset here, because + QUICK_INDEX_MERGE_SELECT::~QUICK_INDEX_MERGE_SELECT (called by + SQL_SELECT::cleanup()) may free it assuming it's the result of the quick + select operation that we no longer need. Note that all the other parts of + this data structure are cleaned up when + QUICK_INDEX_MERGE_SELECT::get_next encounters end of data, so the next + SQL_SELECT::cleanup() call changes sort.io_cache alone. + */ + IO_CACHE *tablesort_result_cache; + + tablesort_result_cache= table->sort.io_cache; + table->sort.io_cache= NULL; + select->cleanup(); // filesort did select tab->select= 0; table->quick_keys.clear_all(); // as far as we cleanup select->quick + table->sort.io_cache= tablesort_result_cache; } tab->select_cond=0; tab->last_inner= 0; @@ -14244,8 +14295,8 @@ find_order_in_list(THD *thd, Item **ref_pointer_array, TABLE_LIST *tables, /* Lookup the current GROUP field in the FROM clause. */ order_item_type= order_item->type(); from_field= (Field*) not_found_field; - if (is_group_field && - order_item_type == Item::FIELD_ITEM || + if ((is_group_field && + order_item_type == Item::FIELD_ITEM) || order_item_type == Item::REF_ITEM) { from_field= find_field_in_tables(thd, (Item_ident*) order_item, tables, @@ -14680,7 +14731,7 @@ get_sort_by_table(ORDER *a,ORDER *b,TABLE_LIST *tables) if (!map || (map & (RAND_TABLE_BIT | OUTER_REF_TABLE_BIT))) DBUG_RETURN(0); - for (; !(map & tables->table->map); tables= tables->next_leaf); + for (; !(map & tables->table->map); tables= tables->next_leaf) ; if (map != tables->table->map) DBUG_RETURN(0); // More than one table DBUG_PRINT("exit",("sort by table: %d",tables->table->tablenr)); diff --git a/sql/sql_select.h b/sql/sql_select.h index 353c2b1d610..a0366d47149 100644 --- a/sql/sql_select.h +++ b/sql/sql_select.h @@ -466,7 +466,8 @@ public: group_optimized_away= 0; all_fields= fields_arg; - fields_list= fields_arg; + if (&fields_list != &fields_arg) /* Avoid valgrind-warning */ + fields_list= fields_arg; bzero((char*) &keyuse,sizeof(keyuse)); tmp_table_param.init(); tmp_table_param.end_write_records= HA_POS_ERROR; diff --git a/sql/sql_show.cc b/sql/sql_show.cc index d08b3a248c4..ae75609e2b6 100644 --- a/sql/sql_show.cc +++ b/sql/sql_show.cc @@ -74,9 +74,6 @@ static TYPELIB grant_types = { sizeof(grant_names)/sizeof(char **), grant_names, NULL}; #endif -/* Match the values of enum ha_choice */ -static const char *ha_choice_values[] = {"", "0", "1"}; - static void store_key_options(THD *thd, String *packet, TABLE *table, KEY *key_info); @@ -601,7 +598,7 @@ mysqld_show_create(THD *thd, TABLE_LIST *table_list) if (open_normal_and_derived_tables(thd, table_list, 0)) { if (!table_list->view || - thd->is_error() && thd->main_da.sql_errno() != ER_VIEW_INVALID) + (thd->is_error() && thd->main_da.sql_errno() != ER_VIEW_INVALID)) DBUG_RETURN(TRUE); /* @@ -1151,7 +1148,7 @@ int store_create_info(THD *thd, TABLE_LIST *table_list, String *packet, { const LEX_STRING *const db= table_list->schema_table ? &INFORMATION_SCHEMA_NAME : &table->s->db; - if (strcmp(db->str, thd->db) != 0) + if (!thd->db || strcmp(db->str, thd->db)) { append_identifier(thd, packet, db->str, db->length); packet->append(STRING_WITH_LEN(".")); @@ -1428,11 +1425,6 @@ int store_create_info(THD *thd, TABLE_LIST *table_list, String *packet, /* We use CHECKSUM, instead of TABLE_CHECKSUM, for backward compability */ if (share->db_create_options & HA_OPTION_CHECKSUM) packet->append(STRING_WITH_LEN(" CHECKSUM=1")); - if (share->page_checksum != HA_CHOICE_UNDEF) - { - packet->append(STRING_WITH_LEN(" PAGE_CHECKSUM=")); - packet->append(ha_choice_values[(uint) share->page_checksum], 1); - } if (share->db_create_options & HA_OPTION_DELAY_KEY_WRITE) packet->append(STRING_WITH_LEN(" DELAY_KEY_WRITE=1")); if (create_info.row_type != ROW_TYPE_DEFAULT) @@ -1440,11 +1432,6 @@ int store_create_info(THD *thd, TABLE_LIST *table_list, String *packet, packet->append(STRING_WITH_LEN(" ROW_FORMAT=")); packet->append(ha_row_type[(uint) create_info.row_type]); } - if (share->transactional != HA_CHOICE_UNDEF) - { - packet->append(STRING_WITH_LEN(" TRANSACTIONAL=")); - packet->append(ha_choice_values[(uint) share->transactional], 1); - } if (table->s->key_block_size) { char *end; @@ -1768,16 +1755,14 @@ void mysqld_list_processes(THD *thd,const char *user, bool verbose) thd_info->start_time= tmp->start_time; thd_info->query=0; + /* Lock THD mutex that protects its data when looking at it. */ + pthread_mutex_lock(&tmp->LOCK_thd_data); if (tmp->query) { - /* - query_length is always set to 0 when we set query = NULL; see - the comment in sql_class.h why this prevents crashes in possible - races with query_length - */ uint length= min(max_query_length, tmp->query_length); thd_info->query=(char*) thd->strmake(tmp->query,length); } + pthread_mutex_unlock(&tmp->LOCK_thd_data); thread_infos.append(thd_info); } } @@ -2819,8 +2804,8 @@ make_table_name_list(THD *thd, List<LEX_STRING> *table_names, LEX *lex, LOOKUP_FIELD_VALUES *lookup_field_vals, bool with_i_schema, LEX_STRING *db_name) { - char path[FN_REFLEN]; - build_table_filename(path, sizeof(path), db_name->str, "", "", 0); + char path[FN_REFLEN + 1]; + build_table_filename(path, sizeof(path) - 1, db_name->str, "", "", 0); if (!lookup_field_vals->wild_table_value && lookup_field_vals->table_value.str) { @@ -2982,8 +2967,8 @@ static int fill_schema_table_names(THD *thd, TABLE *table, else { enum legacy_db_type not_used; - char path[FN_REFLEN]; - (void) build_table_filename(path, sizeof(path), db_name->str, + char path[FN_REFLEN + 1]; + (void) build_table_filename(path, sizeof(path) - 1, db_name->str, table_name->str, reg_ext, 0); switch (mysql_frm_type(thd, path, ¬_used)) { case FRMTYPE_ERROR: @@ -3238,10 +3223,10 @@ int get_all_tables(THD *thd, TABLE_LIST *tables, COND *cond) if lookup value is empty string then it's impossible table name or db name */ - if (lookup_field_vals.db_value.str && - !lookup_field_vals.db_value.str[0] || - lookup_field_vals.table_value.str && - !lookup_field_vals.table_value.str[0]) + if ((lookup_field_vals.db_value.str && + !lookup_field_vals.db_value.str[0]) || + (lookup_field_vals.table_value.str && + !lookup_field_vals.table_value.str[0])) { error= 0; goto err; @@ -3470,7 +3455,7 @@ int fill_schema_schemata(THD *thd, TABLE_LIST *tables, COND *cond) MY_STAT stat_info; if (!lookup_field_vals.db_value.str[0]) DBUG_RETURN(0); - path_len= build_table_filename(path, sizeof(path), + path_len= build_table_filename(path, sizeof(path) - 1, lookup_field_vals.db_value.str, "", "", 0); path[path_len-1]= 0; if (!my_stat(path,&stat_info,MYF(0))) @@ -3593,21 +3578,12 @@ static int get_schema_tables_record(THD *thd, TABLE_LIST *tables, /* We use CHECKSUM, instead of TABLE_CHECKSUM, for backward compability */ if (share->db_create_options & HA_OPTION_CHECKSUM) ptr=strmov(ptr," checksum=1"); - if (share->page_checksum != HA_CHOICE_UNDEF) - ptr= strxmov(ptr, " page_checksum=", - ha_choice_values[(uint) share->page_checksum], NullS); if (share->db_create_options & HA_OPTION_DELAY_KEY_WRITE) ptr=strmov(ptr," delay_key_write=1"); if (share->row_type != ROW_TYPE_DEFAULT) ptr=strxmov(ptr, " row_format=", ha_row_type[(uint) share->row_type], NullS); - if (share->transactional != HA_CHOICE_UNDEF) - { - ptr= strxmov(ptr, " TRANSACTIONAL=", - (share->transactional == HA_CHOICE_YES ? "1" : "0"), - NullS); - } if (share->key_block_size) { ptr= strmov(ptr, " KEY_BLOCK_SIZE="); @@ -3617,9 +3593,6 @@ static int get_schema_tables_record(THD *thd, TABLE_LIST *tables, if (is_partitioned) ptr= strmov(ptr, " partitioned"); #endif - if (share->transactional != HA_CHOICE_UNDEF) - ptr= strxmov(ptr, " transactional=", - ha_choice_values[(uint) share->transactional], NullS); table->field[19]->store(option_buff+1, (ptr == option_buff ? 0 : (uint) (ptr-option_buff)-1), cs); @@ -4116,10 +4089,10 @@ bool store_schema_proc(THD *thd, TABLE *table, TABLE *proc_table, TYPE_ENUM_PROCEDURE)) return 0; - if (lex->sql_command == SQLCOM_SHOW_STATUS_PROC && - proc_table->field[2]->val_int() == TYPE_ENUM_PROCEDURE || - lex->sql_command == SQLCOM_SHOW_STATUS_FUNC && - proc_table->field[2]->val_int() == TYPE_ENUM_FUNCTION || + if ((lex->sql_command == SQLCOM_SHOW_STATUS_PROC && + proc_table->field[2]->val_int() == TYPE_ENUM_PROCEDURE) || + (lex->sql_command == SQLCOM_SHOW_STATUS_FUNC && + proc_table->field[2]->val_int() == TYPE_ENUM_FUNCTION) || (sql_command_flags[lex->sql_command] & CF_STATUS_COMMAND) == 0) { restore_record(table, s->default_values); @@ -7071,6 +7044,12 @@ bool show_create_trigger(THD *thd, const sp_name *trg_name) if (!lst) return TRUE; + if (check_table_access(thd, TRIGGER_ACL, lst, 1, TRUE)) + { + my_error(ER_SPECIFIC_ACCESS_DENIED_ERROR, MYF(0), "TRIGGER"); + return TRUE; + } + /* Open the table by name in order to load Table_triggers_list object. diff --git a/sql/sql_string.cc b/sql/sql_string.cc index 61731f3b984..7c9793b273b 100644 --- a/sql/sql_string.cc +++ b/sql/sql_string.cc @@ -322,14 +322,23 @@ bool String::set_or_copy_aligned(const char *str,uint32 arg_length, return copy_aligned(str, arg_length, offset, cs); } - /* Copy with charset conversion */ + +/** + Copies the character data into this String, with optional character set + conversion. + + @return + FALSE ok + TRUE Could not allocate result buffer + +*/ bool String::copy(const char *str, uint32 arg_length, CHARSET_INFO *from_cs, CHARSET_INFO *to_cs, uint *errors) { uint32 offset; - - DBUG_ASSERT(str != Ptr); + + DBUG_ASSERT(!str || str != Ptr); if (!needs_conversion(arg_length, from_cs, to_cs, &offset)) { @@ -1110,3 +1119,76 @@ void String::swap(String &s) swap_variables(bool, alloced, s.alloced); swap_variables(CHARSET_INFO*, str_charset, s.str_charset); } + + +/** + Convert string to printable ASCII string + + @details This function converts input string "from" replacing non-ASCII bytes + with hexadecimal sequences ("\xXX") optionally appending "..." to the end of + the resulting string. + This function used in the ER_TRUNCATED_WRONG_VALUE_FOR_FIELD error messages, + e.g. when a string cannot be converted to a result charset. + + + @param to output buffer + @param to_len size of the output buffer (8 bytes or greater) + @param from input string + @param from_len size of the input string + @param from_cs input charset + @param nbytes maximal number of bytes to convert (from_len if 0) + + @return number of bytes in the output string +*/ + +uint convert_to_printable(char *to, size_t to_len, + const char *from, size_t from_len, + CHARSET_INFO *from_cs, size_t nbytes /*= 0*/) +{ + /* needs at least 8 bytes for '\xXX...' and zero byte */ + DBUG_ASSERT(to_len >= 8); + + char *t= to; + char *t_end= to + to_len - 1; // '- 1' is for the '\0' at the end + const char *f= from; + const char *f_end= from + (nbytes ? min(from_len, nbytes) : from_len); + char *dots= to; // last safe place to append '...' + + if (!f || t == t_end) + return 0; + + for (; t < t_end && f < f_end; f++) + { + /* + If the source string is ASCII compatible (mbminlen==1) + and the source character is in ASCII printable range (0x20..0x7F), + then display the character as is. + + Otherwise, if the source string is not ASCII compatible (e.g. UCS2), + or the source character is not in the printable range, + then print the character using HEX notation. + */ + if (((unsigned char) *f) >= 0x20 && + ((unsigned char) *f) <= 0x7F && + from_cs->mbminlen == 1) + { + *t++= *f; + } + else + { + if (t_end - t < 4) // \xXX + break; + *t++= '\\'; + *t++= 'x'; + *t++= _dig_vec_upper[((unsigned char) *f) >> 4]; + *t++= _dig_vec_upper[((unsigned char) *f) & 0x0F]; + } + if (t_end - t >= 3) // '...' + dots= t; + } + if (f < from + from_len) + memcpy(dots, STRING_WITH_LEN("...\0")); + else + *t= '\0'; + return t - to; +} diff --git a/sql/sql_string.h b/sql/sql_string.h index be11fea70dc..d62908e5d66 100644 --- a/sql/sql_string.h +++ b/sql/sql_string.h @@ -40,6 +40,9 @@ uint32 well_formed_copy_nchars(CHARSET_INFO *to_cs, size_t my_copy_with_hex_escaping(CHARSET_INFO *cs, char *dst, size_t dstlen, const char *src, size_t srclen); +uint convert_to_printable(char *to, size_t to_len, + const char *from, size_t from_len, + CHARSET_INFO *from_cs, size_t nbytes= 0); class String { @@ -366,6 +369,19 @@ public: { return (s->alloced && Ptr >= s->Ptr && Ptr < s->Ptr + s->str_length); } + bool is_ascii() const + { + if (length() == 0) + return TRUE; + if (charset()->mbminlen > 1) + return FALSE; + for (const char *c= ptr(), *end= c + length(); c < end; c++) + { + if (!my_isascii(*c)) + return FALSE; + } + return TRUE; + } }; static inline bool check_if_only_end_space(CHARSET_INFO *cs, char *str, diff --git a/sql/sql_table.cc b/sql/sql_table.cc index 5397128855a..0066c66eb59 100644 --- a/sql/sql_table.cc +++ b/sql/sql_table.cc @@ -68,6 +68,234 @@ static void wait_for_kill_signal(THD *thd) #endif +/** + @brief Helper function for explain_filename +*/ +static char* add_identifier(char *to_p, const char * end_p, + const char* name, uint name_len, int errcode) +{ + uint res; + uint errors; + const char *conv_name; + char tmp_name[FN_REFLEN]; + char conv_string[FN_REFLEN]; + + DBUG_ENTER("add_identifier"); + if (!name[name_len]) + conv_name= name; + else + { + strnmov(tmp_name, name, name_len); + tmp_name[name_len]= 0; + conv_name= tmp_name; + } + res= strconvert(&my_charset_filename, conv_name, system_charset_info, + conv_string, FN_REFLEN, &errors); + if (!res || errors) + conv_name= name; + else + { + DBUG_PRINT("info", ("conv '%s' -> '%s'", conv_name, conv_string)); + conv_name= conv_string; + } + + if (errcode) + to_p+= my_snprintf(to_p, end_p - to_p, ER(errcode), conv_name); + else + to_p+= my_snprintf(to_p, end_p - to_p, "`%s`", conv_name); + return to_p; +} + + +/** + @brief Explain a path name by split it to database, table etc. + + @details Break down the path name to its logic parts + (database, table, partition, subpartition). + filename_to_tablename cannot be used on partitions, due to the #P# part. + There can be up to 6 '#', #P# for partition, #SP# for subpartition + and #TMP# or #REN# for temporary or renamed partitions. + This should be used when something should be presented to a user in a + diagnostic, error etc. when it would be useful to know what a particular + file [and directory] means. Such as SHOW ENGINE STATUS, error messages etc. + + @param from Path name in my_charset_filename + Null terminated in my_charset_filename, normalized + to use '/' as directory separation character. + @param to Explained name in system_charset_info + @param to_length Size of to buffer + @param explain_mode Requested output format. + EXPLAIN_ALL_VERBOSE -> + [Database `db`, ]Table `tbl`[,[ Temporary| Renamed] + Partition `p` [, Subpartition `sp`]] + EXPLAIN_PARTITIONS_VERBOSE -> `db`.`tbl` + [[ Temporary| Renamed] Partition `p` + [, Subpartition `sp`]] + EXPLAIN_PARTITIONS_AS_COMMENT -> `db`.`tbl` |* + [,[ Temporary| Renamed] Partition `p` + [, Subpartition `sp`]] *| + (| is really a /, and it is all in one line) + + @retval Length of returned string +*/ + +uint explain_filename(const char *from, + char *to, + uint to_length, + enum_explain_filename_mode explain_mode) +{ + uint res= 0; + char *to_p= to; + char *end_p= to_p + to_length; + const char *db_name= NULL; + int db_name_len= 0; + const char *table_name; + int table_name_len= 0; + const char *part_name= NULL; + int part_name_len= 0; + const char *subpart_name= NULL; + int subpart_name_len= 0; + enum enum_file_name_type {NORMAL, TEMP, RENAMED} name_type= NORMAL; + const char *tmp_p; + DBUG_ENTER("explain_filename"); + DBUG_PRINT("enter", ("from '%s'", from)); + tmp_p= from; + table_name= from; + /* + If '/' then take last directory part as database. + '/' is the directory separator, not FN_LIB_CHAR + */ + while ((tmp_p= strchr(tmp_p, '/'))) + { + db_name= table_name; + /* calculate the length */ + db_name_len= tmp_p - db_name; + tmp_p++; + table_name= tmp_p; + } + tmp_p= table_name; + while (!res && (tmp_p= strchr(tmp_p, '#'))) + { + tmp_p++; + switch (tmp_p[0]) { + case 'P': + case 'p': + if (tmp_p[1] == '#') + part_name= tmp_p + 2; + else + res= 1; + tmp_p+= 2; + break; + case 'S': + case 's': + if ((tmp_p[1] == 'P' || tmp_p[1] == 'p') && tmp_p[2] == '#') + { + part_name_len= tmp_p - part_name - 1; + subpart_name= tmp_p + 3; + } + else + res= 2; + tmp_p+= 3; + break; + case 'T': + case 't': + if ((tmp_p[1] == 'M' || tmp_p[1] == 'm') && + (tmp_p[2] == 'P' || tmp_p[2] == 'p') && + tmp_p[3] == '#' && !tmp_p[4]) + name_type= TEMP; + else + res= 3; + tmp_p+= 4; + break; + case 'R': + case 'r': + if ((tmp_p[1] == 'E' || tmp_p[1] == 'e') && + (tmp_p[2] == 'N' || tmp_p[2] == 'n') && + tmp_p[3] == '#' && !tmp_p[4]) + name_type= RENAMED; + else + res= 4; + tmp_p+= 4; + break; + default: + res= 5; + } + } + if (res) + { + /* Better to give something back if we fail parsing, than nothing at all */ + DBUG_PRINT("info", ("Error in explain_filename: %u", res)); + sql_print_warning("Invalid (old?) table or database name '%s'", from); + DBUG_RETURN(my_snprintf(to, to_length, + "<result %u when explaining filename '%s'>", + res, from)); + } + if (part_name) + { + table_name_len= part_name - table_name - 3; + if (subpart_name) + subpart_name_len= strlen(subpart_name); + else + part_name_len= strlen(part_name); + if (name_type != NORMAL) + { + if (subpart_name) + subpart_name_len-= 5; + else + part_name_len-= 5; + } + } + if (db_name) + { + if (explain_mode == EXPLAIN_ALL_VERBOSE) + { + to_p= add_identifier(to_p, end_p, db_name, db_name_len, + ER_DATABASE_NAME); + to_p= strnmov(to_p, ", ", end_p - to_p); + } + else + { + to_p= add_identifier(to_p, end_p, db_name, db_name_len, 0); + to_p= strnmov(to_p, ".", end_p - to_p); + } + } + if (explain_mode == EXPLAIN_ALL_VERBOSE) + to_p= add_identifier(to_p, end_p, table_name, table_name_len, + ER_TABLE_NAME); + else + to_p= add_identifier(to_p, end_p, table_name, table_name_len, 0); + if (part_name) + { + if (explain_mode == EXPLAIN_PARTITIONS_AS_COMMENT) + to_p= strnmov(to_p, " /* ", end_p - to_p); + else if (explain_mode == EXPLAIN_PARTITIONS_VERBOSE) + to_p= strnmov(to_p, " ", end_p - to_p); + else + to_p= strnmov(to_p, ", ", end_p - to_p); + if (name_type != NORMAL) + { + if (name_type == TEMP) + to_p= strnmov(to_p, ER(ER_TEMPORARY_NAME), end_p - to_p); + else + to_p= strnmov(to_p, ER(ER_RENAMED_NAME), end_p - to_p); + to_p= strnmov(to_p, " ", end_p - to_p); + } + to_p= add_identifier(to_p, end_p, part_name, part_name_len, + ER_PARTITION_NAME); + if (subpart_name) + { + to_p= strnmov(to_p, ", ", end_p - to_p); + to_p= add_identifier(to_p, end_p, subpart_name, subpart_name_len, + ER_SUBPARTITION_NAME); + } + if (explain_mode == EXPLAIN_PARTITIONS_AS_COMMENT) + to_p= strnmov(to_p, " */", end_p - to_p); + } + DBUG_PRINT("exit", ("to '%s'", to)); + DBUG_RETURN(to_p - to); +} + + /* Translate a file name to a table name (WL #1324). @@ -1287,7 +1515,7 @@ bool mysql_write_frm(ALTER_PARTITION_PARAM_TYPE *lpt, uint flags) /* Build shadow frm file name */ - build_table_shadow_filename(shadow_path, sizeof(shadow_path), lpt); + build_table_shadow_filename(shadow_path, sizeof(shadow_path) - 1, lpt); strxmov(shadow_frm_name, shadow_path, reg_ext, NullS); if (flags & WFRM_WRITE_SHADOW) { @@ -1362,7 +1590,7 @@ bool mysql_write_frm(ALTER_PARTITION_PARAM_TYPE *lpt, uint flags) /* Build frm file name */ - build_table_filename(path, sizeof(path), lpt->db, + build_table_filename(path, sizeof(path) - 1, lpt->db, lpt->table_name, "", 0); strxmov(frm_name, path, reg_ext, NullS); /* @@ -1460,10 +1688,13 @@ void write_bin_log(THD *thd, bool clear_error, { if (mysql_bin_log.is_open()) { + int errcode= 0; if (clear_error) thd->clear_error(); + else + errcode= query_error_code(thd, TRUE); thd->binlog_query(THD::STMT_QUERY_TYPE, - query, query_length, FALSE, FALSE, THD::NOT_KILLED); + query, query_length, FALSE, FALSE, errcode); } } @@ -1561,13 +1792,14 @@ int mysql_rm_table_part2(THD *thd, TABLE_LIST *tables, bool if_exists, bool dont_log_query) { TABLE_LIST *table; - char path[FN_REFLEN], *alias; + char path[FN_REFLEN + 1], *alias; uint path_length; String wrong_tables; int error= 0; int non_temp_tables_count= 0; bool some_tables_deleted=0, tmp_table_deleted=0, foreign_key_error=0; String built_query; + String built_tmp_query; DBUG_ENTER("mysql_rm_table_part2"); LINT_INIT(alias); @@ -1635,6 +1867,25 @@ int mysql_rm_table_part2(THD *thd, TABLE_LIST *tables, bool if_exists, case 0: // removed temporary table tmp_table_deleted= 1; + if (thd->variables.binlog_format == BINLOG_FORMAT_MIXED && + thd->current_stmt_binlog_row_based) + { + if (built_tmp_query.is_empty()) + { + built_tmp_query.set_charset(system_charset_info); + built_tmp_query.append("DROP TEMPORARY TABLE IF EXISTS "); + } + + built_tmp_query.append("`"); + if (thd->db == NULL || strcmp(db,thd->db) != 0) + { + built_tmp_query.append(db); + built_tmp_query.append("`.`"); + } + built_tmp_query.append(table->table_name); + built_tmp_query.append("`,"); + } + continue; case -1: DBUG_ASSERT(thd->in_sub_stmt); @@ -1691,13 +1942,14 @@ int mysql_rm_table_part2(THD *thd, TABLE_LIST *tables, bool if_exists, } alias= (lower_case_table_names == 2) ? table->alias : table->table_name; /* remove .frm file and engine files */ - path_length= build_table_filename(path, sizeof(path), db, alias, reg_ext, + path_length= build_table_filename(path, sizeof(path) - 1, db, alias, + reg_ext, table->internal_tmp_table ? FN_IS_TMP : 0); } if (drop_temporary || - (table_type == NULL && - (access(path, F_OK) && + ((table_type == NULL && + access(path, F_OK) && ha_create_table_from_engine(thd, db, alias)) || (!drop_view && mysql_frm_type(thd, path, &frm_db_type) != FRMTYPE_TABLE))) @@ -1779,7 +2031,7 @@ int mysql_rm_table_part2(THD *thd, TABLE_LIST *tables, bool if_exists, if (!dont_log_query) { if (!thd->current_stmt_binlog_row_based || - non_temp_tables_count > 0 && !tmp_table_deleted) + (non_temp_tables_count > 0 && !tmp_table_deleted)) { /* In this case, we are either using statement-based @@ -1791,29 +2043,52 @@ int mysql_rm_table_part2(THD *thd, TABLE_LIST *tables, bool if_exists, write_bin_log(thd, !error, thd->query, thd->query_length); } else if (thd->current_stmt_binlog_row_based && - non_temp_tables_count > 0 && tmp_table_deleted) { + if (non_temp_tables_count > 0) + { + /* + In this case we have deleted both temporary and + non-temporary tables, so: + - since we have deleted a non-temporary table we have to + binlog the statement, but + - since we have deleted a temporary table we cannot binlog + the statement (since the table may have not been created on the + slave - check "if" branch below, this might cause the slave to + stop). + + Instead, we write a built statement, only containing the + non-temporary tables, to the binary log + */ + built_query.chop(); // Chop of the last comma + built_query.append(" /* generated by server */"); + write_bin_log(thd, !error, built_query.ptr(), built_query.length()); + } + /* - In this case we have deleted both temporary and - non-temporary tables, so: - - since we have deleted a non-temporary table we have to - binlog the statement, but - - since we have deleted a temporary table we cannot binlog - the statement (since the table has not been created on the - slave, this might cause the slave to stop). - - Instead, we write a built statement, only containing the - non-temporary tables, to the binary log + One needs to always log any temporary table drop, if: + 1. thread logging format is mixed mode; AND + 2. current statement logging format is set to row. */ - built_query.chop(); // Chop of the last comma - built_query.append(" /* generated by server */"); - write_bin_log(thd, !error, built_query.ptr(), built_query.length()); + if (thd->variables.binlog_format == BINLOG_FORMAT_MIXED) + { + /* + In this case we have deleted some temporary tables but we are using + row based logging for the statement. However, thread uses mixed mode + format, thence we need to log the dropping as we cannot tell for + sure whether the create was logged as statement previously or not, ie, + before switching to row mode. + */ + built_tmp_query.chop(); // Chop of the last comma + built_tmp_query.append(" /* generated by server */"); + write_bin_log(thd, !error, built_tmp_query.ptr(), built_tmp_query.length()); + } } + /* The remaining cases are: - - no tables where deleted and - - only temporary tables where deleted and row-based + - no tables were deleted and + - only temporary tables were deleted and row-based replication is used. In both these cases, nothing should be written to the binary log. @@ -1847,11 +2122,11 @@ err_with_placeholders: bool quick_rm_table(handlerton *base,const char *db, const char *table_name, uint flags) { - char path[FN_REFLEN]; + char path[FN_REFLEN + 1]; bool error= 0; DBUG_ENTER("quick_rm_table"); - uint path_length= build_table_filename(path, sizeof(path), + uint path_length= build_table_filename(path, sizeof(path) - 1, db, table_name, reg_ext, flags); if (my_delete(path,MYF(0))) error= 1; /* purecov: inspected */ @@ -2488,8 +2763,8 @@ mysql_prepare_create_table(THD *thd, HA_CREATE_INFO *create_info, } /* Don't pack rows in old tables if the user has requested this */ if ((sql_field->flags & BLOB_FLAG) || - sql_field->sql_type == MYSQL_TYPE_VARCHAR && - create_info->row_type != ROW_TYPE_FIXED) + (sql_field->sql_type == MYSQL_TYPE_VARCHAR && + create_info->row_type != ROW_TYPE_FIXED)) (*db_options)|= HA_OPTION_PACK_RECORD; it2.rewind(); } @@ -2958,7 +3233,7 @@ mysql_prepare_create_table(THD *thd, HA_CREATE_INFO *create_info, sql_field->sql_type == MYSQL_TYPE_VARCHAR || sql_field->pack_flag & FIELDFLAG_BLOB))) { - if (column_nr == 0 && (sql_field->pack_flag & FIELDFLAG_BLOB) || + if ((column_nr == 0 && (sql_field->pack_flag & FIELDFLAG_BLOB)) || sql_field->sql_type == MYSQL_TYPE_VARCHAR) key_info->flags|= HA_BINARY_PACK_KEY | HA_VAR_LENGTH_KEY; else @@ -3240,7 +3515,7 @@ bool mysql_create_table_no_lock(THD *thd, bool internal_tmp_table, uint select_field_count) { - char path[FN_REFLEN]; + char path[FN_REFLEN + 1]; uint path_length; const char *alias; uint db_options, key_count; @@ -3448,7 +3723,7 @@ bool mysql_create_table_no_lock(THD *thd, } else { - path_length= build_table_filename(path, sizeof(path), db, alias, reg_ext, + path_length= build_table_filename(path, sizeof(path) - 1, db, alias, reg_ext, internal_tmp_table ? FN_IS_TMP : 0); } @@ -3762,7 +4037,8 @@ mysql_rename_table(handlerton *base, const char *old_db, const char *new_name, uint flags) { THD *thd= current_thd; - char from[FN_REFLEN], to[FN_REFLEN], lc_from[FN_REFLEN], lc_to[FN_REFLEN]; + char from[FN_REFLEN + 1], to[FN_REFLEN + 1], + lc_from[FN_REFLEN + 1], lc_to[FN_REFLEN + 1]; char *from_base= from, *to_base= to; char tmp_name[NAME_LEN+1]; handler *file; @@ -3774,9 +4050,9 @@ mysql_rename_table(handlerton *base, const char *old_db, file= (base == NULL ? 0 : get_new_handler((TABLE_SHARE*) 0, thd->mem_root, base)); - build_table_filename(from, sizeof(from), old_db, old_name, "", + build_table_filename(from, sizeof(from) - 1, old_db, old_name, "", flags & FN_FROM_IS_TMP); - build_table_filename(to, sizeof(to), new_db, new_name, "", + build_table_filename(to, sizeof(to) - 1, new_db, new_name, "", flags & FN_TO_IS_TMP); /* @@ -3789,13 +4065,13 @@ mysql_rename_table(handlerton *base, const char *old_db, { strmov(tmp_name, old_name); my_casedn_str(files_charset_info, tmp_name); - build_table_filename(lc_from, sizeof(lc_from), old_db, tmp_name, "", + build_table_filename(lc_from, sizeof(lc_from) - 1, old_db, tmp_name, "", flags & FN_FROM_IS_TMP); from_base= lc_from; strmov(tmp_name, new_name); my_casedn_str(files_charset_info, tmp_name); - build_table_filename(lc_to, sizeof(lc_to), new_db, tmp_name, "", + build_table_filename(lc_to, sizeof(lc_to) - 1, new_db, tmp_name, "", flags & FN_TO_IS_TMP); to_base= lc_to; } @@ -3926,16 +4202,16 @@ static int prepare_for_restore(THD* thd, TABLE_LIST* table, else { char* backup_dir= thd->lex->backup_dir; - char src_path[FN_REFLEN], dst_path[FN_REFLEN], uname[FN_REFLEN]; + char src_path[FN_REFLEN], dst_path[FN_REFLEN + 1], uname[FN_REFLEN]; char* table_name= table->table_name; char* db= table->db; - VOID(tablename_to_filename(table->table_name, uname, sizeof(uname))); + VOID(tablename_to_filename(table->table_name, uname, sizeof(uname) - 1)); if (fn_format_relative_to_data_home(src_path, uname, backup_dir, reg_ext)) DBUG_RETURN(-1); // protect buffer overflow - build_table_filename(dst_path, sizeof(dst_path), + build_table_filename(dst_path, sizeof(dst_path) - 1, db, table_name, reg_ext, 0); if (lock_and_wait_for_table_name(thd,table)) @@ -4547,7 +4823,7 @@ send_result_message: const char *err_msg= thd->main_da.message(); if (!thd->vio_ok()) { - sql_print_error(err_msg); + sql_print_error("%s", err_msg); } else { @@ -4857,7 +5133,7 @@ bool mysql_create_like_table(THD* thd, TABLE_LIST* table, TABLE_LIST* src_table, HA_CREATE_INFO *create_info) { TABLE *name_lock= 0; - char src_path[FN_REFLEN], dst_path[FN_REFLEN]; + char src_path[FN_REFLEN], dst_path[FN_REFLEN + 1]; uint dst_path_length; char *db= table->db; char *table_name= table->table_name; @@ -4867,7 +5143,7 @@ bool mysql_create_like_table(THD* thd, TABLE_LIST* table, TABLE_LIST* src_table, #ifdef WITH_PARTITION_STORAGE_ENGINE char tmp_path[FN_REFLEN]; #endif - char ts_name[FN_LEN]; + char ts_name[FN_LEN + 1]; DBUG_ENTER("mysql_create_like_table"); @@ -4916,7 +5192,7 @@ bool mysql_create_like_table(THD* thd, TABLE_LIST* table, TABLE_LIST* src_table, goto err; if (!name_lock) goto table_exists; - dst_path_length= build_table_filename(dst_path, sizeof(dst_path), + dst_path_length= build_table_filename(dst_path, sizeof(dst_path) - 1, db, table_name, reg_ext, 0); if (!access(dst_path, F_OK)) goto table_exists; @@ -5314,7 +5590,7 @@ compare_tables(TABLE *table, create_info->used_fields & HA_CREATE_USED_ENGINE || create_info->used_fields & HA_CREATE_USED_CHARSET || create_info->used_fields & HA_CREATE_USED_DEFAULT_CHARSET || - create_info->used_fields & HA_CREATE_USED_ROW_FORMAT || + (table->s->row_type != create_info->row_type) || create_info->used_fields & HA_CREATE_USED_PACK_KEYS || create_info->used_fields & HA_CREATE_USED_MAX_ROWS || (alter_info->flags & (ALTER_RECREATE | ALTER_FOREIGN_KEY)) || @@ -5358,8 +5634,8 @@ compare_tables(TABLE *table, /* Don't pack rows in old tables if the user has requested this. */ if (create_info->row_type == ROW_TYPE_DYNAMIC || (tmp_new_field->flags & BLOB_FLAG) || - tmp_new_field->sql_type == MYSQL_TYPE_VARCHAR && - create_info->row_type != ROW_TYPE_FIXED) + (tmp_new_field->sql_type == MYSQL_TYPE_VARCHAR && + create_info->row_type != ROW_TYPE_FIXED)) create_info->table_options|= HA_OPTION_PACK_RECORD; /* Check if field was renamed */ @@ -5652,12 +5928,10 @@ mysql_prepare_alter_table(THD *thd, TABLE *table, } if (!(used_fields & HA_CREATE_USED_KEY_BLOCK_SIZE)) create_info->key_block_size= table->s->key_block_size; - if (!(used_fields & HA_CREATE_USED_TRANSACTIONAL)) - create_info->transactional= table->s->transactional; if (!create_info->tablespace && create_info->storage_media != HA_SM_MEMORY) { - char *tablespace= static_cast<char *>(thd->alloc(FN_LEN)); + char *tablespace= static_cast<char *>(thd->alloc(FN_LEN + 1)); /* Regular alter table of disk stored table (no tablespace/storage change) Copy tablespace name @@ -6024,10 +6298,10 @@ bool mysql_alter_table(THD *thd,char *new_db, char *new_name, { TABLE *table, *new_table= 0, *name_lock= 0; int error= 0; - char tmp_name[80],old_name[32],new_name_buff[FN_REFLEN]; + char tmp_name[80],old_name[32],new_name_buff[FN_REFLEN + 1]; char new_alias_buff[FN_REFLEN], *table_name, *db, *new_alias, *alias; char index_file[FN_REFLEN], data_file[FN_REFLEN]; - char path[FN_REFLEN]; + char path[FN_REFLEN + 1]; char reg_path[FN_REFLEN+1]; ha_rows copied,deleted; handlerton *old_db_type, *new_db_type, *save_old_db_type; @@ -6040,21 +6314,15 @@ bool mysql_alter_table(THD *thd,char *new_db, char *new_name, #endif bool need_lock_for_indexes= TRUE; KEY *key_info_buffer; - uint index_drop_count; - uint *index_drop_buffer; - uint index_add_count; - uint *index_add_buffer; - uint candidate_key_count; + uint index_drop_count= 0; + uint *index_drop_buffer= NULL; + uint index_add_count= 0; + uint *index_add_buffer= NULL; + uint candidate_key_count= 0; bool committed= 0; bool no_pk; DBUG_ENTER("mysql_alter_table"); - LINT_INIT(index_add_count); - LINT_INIT(index_drop_count); - LINT_INIT(index_add_buffer); - LINT_INIT(index_drop_buffer); - LINT_INIT(candidate_key_count); - /* Check if we attempt to alter mysql.slow_log or mysql.general_log table and return an error if @@ -6108,8 +6376,8 @@ bool mysql_alter_table(THD *thd,char *new_db, char *new_name, db=table_list->db; if (!new_db || !my_strcasecmp(table_alias_charset, new_db, db)) new_db= db; - build_table_filename(reg_path, sizeof(reg_path), db, table_name, reg_ext, 0); - build_table_filename(path, sizeof(path), db, table_name, "", 0); + build_table_filename(reg_path, sizeof(reg_path) - 1, db, table_name, reg_ext, 0); + build_table_filename(path, sizeof(path) - 1, db, table_name, "", 0); mysql_ha_rm_tables(thd, table_list, FALSE); @@ -6180,7 +6448,7 @@ bool mysql_alter_table(THD *thd,char *new_db, char *new_name, { thd->clear_error(); Query_log_event qinfo(thd, thd->query, thd->query_length, - 0, FALSE, THD::NOT_KILLED); + 0, FALSE, 0); mysql_bin_log.write(&qinfo); } my_ok(thd); @@ -6256,7 +6524,7 @@ view_err: DBUG_RETURN(TRUE); } - build_table_filename(new_name_buff, sizeof(new_name_buff), + build_table_filename(new_name_buff, sizeof(new_name_buff) - 1, new_db, new_name_buff, reg_ext, 0); if (!access(new_name_buff, F_OK)) { @@ -6307,7 +6575,10 @@ view_err: } if (create_info->row_type == ROW_TYPE_NOT_USED) + { create_info->row_type= table->s->row_type; + create_info->used_fields |= HA_CREATE_USED_ROW_FORMAT; + } DBUG_PRINT("info", ("old type: %s new type: %s", ha_resolve_storage_engine_name(old_db_type), @@ -6754,9 +7025,9 @@ view_err: } else { - char path[FN_REFLEN]; + char path[FN_REFLEN + 1]; /* table is a normal table: Create temporary table in same directory */ - build_table_filename(path, sizeof(path), new_db, tmp_name, "", + build_table_filename(path, sizeof(path) - 1, new_db, tmp_name, "", FN_IS_TMP); /* Open our intermediate table */ new_table=open_temporary_table(thd, path, new_db, tmp_name,0); @@ -6986,12 +7257,12 @@ view_err: } else if (mysql_rename_table(new_db_type, new_db, tmp_name, new_db, new_alias, FN_FROM_IS_TMP) || - (new_name != table_name || new_db != db) && // we also do rename + ((new_name != table_name || new_db != db) && // we also do rename (need_copy_table != ALTER_TABLE_METADATA_ONLY || mysql_rename_table(save_old_db_type, db, table_name, new_db, new_alias, NO_FRM_RENAME)) && Table_triggers_list::change_table_name(thd, db, table_name, - new_db, new_alias)) + new_db, new_alias))) { /* Try to get everything back. */ error=1; @@ -7084,7 +7355,7 @@ view_err: */ char path[FN_REFLEN]; TABLE *t_table; - build_table_filename(path, sizeof(path), new_db, table_name, "", 0); + build_table_filename(path + 1, sizeof(path) - 1, new_db, table_name, "", 0); t_table= open_temporary_table(thd, path, new_db, tmp_name, 0); if (t_table) { diff --git a/sql/sql_trigger.cc b/sql/sql_trigger.cc index 8cab8fff2f3..c055268ecca 100644 --- a/sql/sql_trigger.cc +++ b/sql/sql_trigger.cc @@ -344,7 +344,7 @@ bool mysql_create_or_drop_trigger(THD *thd, TABLE_LIST *tables, bool create) need second part of condition below, since check_access() function also checks that db is specified. */ - if (!thd->lex->spname->m_db.length || create && !tables->db_length) + if (!thd->lex->spname->m_db.length || (create && !tables->db_length)) { my_error(ER_NO_DB_ERROR, MYF(0)); DBUG_RETURN(TRUE); diff --git a/sql/sql_update.cc b/sql/sql_update.cc index dcccf7f147f..6884f863326 100644 --- a/sql/sql_update.cc +++ b/sql/sql_update.cc @@ -292,7 +292,7 @@ int mysql_update(THD *thd, if (select_lex->inner_refs_list.elements && fix_inner_refs(thd, all_fields, select_lex, select_lex->ref_pointer_array)) - DBUG_RETURN(-1); + DBUG_RETURN(1); if (conds) { @@ -332,7 +332,14 @@ int mysql_update(THD *thd, { delete select; free_underlaid_joins(thd, select_lex); - if (error) + /* + There was an error or the error was already sent by + the quick select evaluation. + TODO: Add error code output parameter to Item::val_xxx() methods. + Currently they rely on the user checking DA for + errors when unwinding the stack after calling Item::val_xxx(). + */ + if (error || thd->is_error()) { DBUG_RETURN(1); // Error in where } @@ -795,12 +802,15 @@ int mysql_update(THD *thd, { if (mysql_bin_log.is_open()) { + int errcode= 0; if (error < 0) thd->clear_error(); + else + errcode= query_error_code(thd, killed_status == THD::NOT_KILLED); + if (thd->binlog_query(THD::ROW_QUERY_TYPE, thd->query, thd->query_length, - transactional_table, FALSE, killed_status) && - transactional_table) + transactional_table, FALSE, errcode)) { error=1; // Rollback update } @@ -1810,7 +1820,7 @@ void multi_update::abort() { /* the error was handled or nothing deleted and no side effects return */ if (error_handled || - !thd->transaction.stmt.modified_non_trans_table && !updated) + (!thd->transaction.stmt.modified_non_trans_table && !updated)) return; /* Something already updated so we have to invalidate cache */ @@ -1847,9 +1857,10 @@ void multi_update::abort() got caught and if happens later the killed error is written into repl event. */ + int errcode= query_error_code(thd, thd->killed == THD::NOT_KILLED); thd->binlog_query(THD::ROW_QUERY_TYPE, thd->query, thd->query_length, - transactional_tables, FALSE); + transactional_tables, FALSE, errcode); } thd->transaction.all.modified_non_trans_table= TRUE; } @@ -2075,12 +2086,14 @@ bool multi_update::send_eof() { if (mysql_bin_log.is_open()) { + int errcode= 0; if (local_error == 0) thd->clear_error(); + else + errcode= query_error_code(thd, killed_status == THD::NOT_KILLED); if (thd->binlog_query(THD::ROW_QUERY_TYPE, thd->query, thd->query_length, - transactional_tables, FALSE, killed_status) && - trans_safe) + transactional_tables, FALSE, errcode)) { local_error= 1; // Rollback update } diff --git a/sql/sql_view.cc b/sql/sql_view.cc index 4f207f78688..2a4c5c950fe 100644 --- a/sql/sql_view.cc +++ b/sql/sql_view.cc @@ -661,8 +661,9 @@ bool mysql_create_view(THD *thd, TABLE_LIST *views, buff.append(STRING_WITH_LEN(" AS ")); buff.append(views->source.str, views->source.length); + int errcode= query_error_code(thd, TRUE); thd->binlog_query(THD::STMT_QUERY_TYPE, - buff.ptr(), buff.length(), FALSE, FALSE, THD::NOT_KILLED); + buff.ptr(), buff.length(), FALSE, FALSE, errcode); } VOID(pthread_mutex_unlock(&LOCK_open)); @@ -800,7 +801,7 @@ static int mysql_register_view(THD *thd, TABLE_LIST *view, char md5[MD5_BUFF_LENGTH]; bool can_be_merged; - char dir_buff[FN_REFLEN], path_buff[FN_REFLEN]; + char dir_buff[FN_REFLEN + 1], path_buff[FN_REFLEN + 1]; LEX_STRING dir, file, path; int error= 0; DBUG_ENTER("mysql_register_view"); @@ -877,11 +878,11 @@ static int mysql_register_view(THD *thd, TABLE_LIST *view, } loop_out: /* print file name */ - dir.length= build_table_filename(dir_buff, sizeof(dir_buff), + dir.length= build_table_filename(dir_buff, sizeof(dir_buff) - 1, view->db, "", "", 0); dir.str= dir_buff; - path.length= build_table_filename(path_buff, sizeof(path_buff), + path.length= build_table_filename(path_buff, sizeof(path_buff) - 1, view->db, view->table_name, reg_ext, 0); path.str= path_buff; @@ -1568,7 +1569,7 @@ err: bool mysql_drop_view(THD *thd, TABLE_LIST *views, enum_drop_mode drop_mode) { - char path[FN_REFLEN]; + char path[FN_REFLEN + 1]; TABLE_LIST *view; String non_existant_views; char *wrong_object_db= NULL, *wrong_object_name= NULL; @@ -1583,7 +1584,7 @@ bool mysql_drop_view(THD *thd, TABLE_LIST *views, enum_drop_mode drop_mode) { TABLE_SHARE *share; frm_type_enum type= FRMTYPE_ERROR; - build_table_filename(path, sizeof(path), + build_table_filename(path, sizeof(path) - 1, view->db, view->table_name, reg_ext, 0); if (access(path, F_OK) || @@ -1928,7 +1929,7 @@ mysql_rename_view(THD *thd, { LEX_STRING pathstr; File_parser *parser; - char path_buff[FN_REFLEN]; + char path_buff[FN_REFLEN + 1]; bool error= TRUE; DBUG_ENTER("mysql_rename_view"); @@ -1941,7 +1942,7 @@ mysql_rename_view(THD *thd, is_equal(&view_type, parser->type())) { TABLE_LIST view_def; - char dir_buff[FN_REFLEN]; + char dir_buff[FN_REFLEN + 1]; LEX_STRING dir, file; /* diff --git a/sql/sql_yacc.yy b/sql/sql_yacc.yy index 0b158ff7574..320b43c8e5c 100644 --- a/sql/sql_yacc.yy +++ b/sql/sql_yacc.yy @@ -47,6 +47,12 @@ #include <myisam.h> #include <myisammrg.h> +/* this is to get the bison compilation windows warnings out */ +#ifdef _MSC_VER +/* warning C4065: switch statement contains 'default' but no 'case' labels */ +#pragma warning (disable : 4065) +#endif + int yylex(void *yylval, void *yythd); const LEX_STRING null_lex_str= {0,0}; @@ -883,7 +889,6 @@ bool my_yyoverflow(short **a, YYSTYPE **b, ulong *yystacksize); %token OWNER_SYM %token PACK_KEYS_SYM %token PAGE_SYM -%token PAGE_CHECKSUM_SYM %token PARAM_MARKER %token PARSER_SYM %token PARTIAL /* SQL-2003-N */ @@ -1042,7 +1047,6 @@ bool my_yyoverflow(short **a, YYSTYPE **b, ulong *yystacksize); %token TO_SYM /* SQL-2003-R */ %token TRAILING /* SQL-2003-R */ %token TRANSACTION_SYM -%token TRANSACTIONAL_SYM %token TRIGGERS_SYM %token TRIGGER_SYM /* SQL-2003-R */ %token TRIM /* SQL-2003-N */ @@ -4202,6 +4206,10 @@ opt_sub_partition: if (Lex->part_info->no_subparts != 0 && !Lex->part_info->use_default_subpartitions) { + /* + We come here when we have defined subpartitions on the first + partition but not on all the subsequent partitions. + */ my_parse_error(ER(ER_PARTITION_WRONG_NO_SUBPART_ERROR)); MYSQL_YYABORT; } @@ -4244,6 +4252,23 @@ sub_part_definition: partition_info *part_info= lex->part_info; partition_element *curr_part= part_info->current_partition; partition_element *sub_p_elem= new partition_element(curr_part); + if (part_info->use_default_subpartitions && + part_info->partitions.elements >= 2) + { + /* + create table t1 (a int) + partition by list (a) subpartition by hash (a) + (partition p0 values in (1), + partition p1 values in (2) subpartition sp11); + causes use to arrive since we are on the second + partition, but still use_default_subpartitions + is set. When we come here we're processing at least + the second partition (the current partition processed + have already been put into the partitions list. + */ + my_parse_error(ER(ER_PARTITION_WRONG_NO_SUBPART_ERROR)); + MYSQL_YYABORT; + } if (!sub_p_elem || curr_part->subpartitions.push_back(sub_p_elem)) { @@ -4460,11 +4485,6 @@ create_table_option: Lex->create_info.table_options|= $3 ? HA_OPTION_CHECKSUM : HA_OPTION_NO_CHECKSUM; Lex->create_info.used_fields|= HA_CREATE_USED_CHECKSUM; } - | PAGE_CHECKSUM_SYM opt_equal choice - { - Lex->create_info.used_fields|= HA_CREATE_USED_PAGE_CHECKSUM; - Lex->create_info.page_checksum= $3; - } | DELAY_KEY_WRITE_SYM opt_equal ulong_num { Lex->create_info.table_options|= $3 ? HA_OPTION_DELAY_KEY_WRITE : HA_OPTION_NO_DELAY_KEY_WRITE; @@ -4524,11 +4544,6 @@ create_table_option: Lex->create_info.used_fields|= HA_CREATE_USED_KEY_BLOCK_SIZE; Lex->create_info.key_block_size= $3; } - | TRANSACTIONAL_SYM opt_equal choice - { - Lex->create_info.used_fields|= HA_CREATE_USED_TRANSACTIONAL; - Lex->create_info.transactional= $3; - } ; default_charset: @@ -4610,7 +4625,6 @@ row_types: | COMPRESSED_SYM { $$= ROW_TYPE_COMPRESSED; } | REDUNDANT_SYM { $$= ROW_TYPE_REDUNDANT; } | COMPACT_SYM { $$= ROW_TYPE_COMPACT; } - | PAGE_SYM { $$= ROW_TYPE_PAGE; } ; merge_insert_types: @@ -5925,7 +5939,7 @@ alter_list_item: MYSQL_YYABORT; } if (check_table_name($3->table.str,$3->table.length) || - $3->db.str && check_db_name(&$3->db)) + ($3->db.str && check_db_name(&$3->db))) { my_error(ER_WRONG_TABLE_NAME, MYF(0), $3->table.str); MYSQL_YYABORT; @@ -6079,8 +6093,8 @@ slave_until: | UNTIL_SYM slave_until_opts { LEX *lex=Lex; - if ((lex->mi.log_file_name || lex->mi.pos) && - (lex->mi.relay_log_name || lex->mi.relay_log_pos) || + if (((lex->mi.log_file_name || lex->mi.pos) && + (lex->mi.relay_log_name || lex->mi.relay_log_pos)) || !((lex->mi.log_file_name && lex->mi.pos) || (lex->mi.relay_log_name && lex->mi.relay_log_pos))) { @@ -8493,6 +8507,7 @@ table_factor: MYSQL_YYABORT; sel->add_joined_table($$); lex->pop_context(); + lex->nest_level--; } else if ($4 || $6) { @@ -8501,7 +8516,11 @@ table_factor: MYSQL_YYABORT; } else + { + /* nested join: FROM (t1 JOIN t2 ...), + nest_level is the same as in the outer query */ $$= $3; + } } ; @@ -9194,6 +9213,8 @@ into_destination: !(lex->result= new select_export(lex->exchange, lex->nest_level))) MYSQL_YYABORT; } + opt_load_data_charset + { Lex->exchange->cs= $4; } opt_field_term opt_line_term | DUMPFILE TEXT_STRING_filesystem { @@ -11548,7 +11569,6 @@ keyword_sp: | ONE_SYM {} | PACK_KEYS_SYM {} | PAGE_SYM {} - | PAGE_CHECKSUM_SYM {} | PARTIAL {} | PARTITIONING_SYM {} | PARTITIONS_SYM {} @@ -11625,7 +11645,6 @@ keyword_sp: | TEXT_SYM {} | THAN_SYM {} | TRANSACTION_SYM {} - | TRANSACTIONAL_SYM {} | TRIGGERS_SYM {} | TIMESTAMP {} | TIMESTAMP_ADD {} @@ -12118,15 +12137,16 @@ text_or_password: | PASSWORD '(' TEXT_STRING ')' { $$= $3.length ? YYTHD->variables.old_passwords ? - Item_func_old_password::alloc(YYTHD, $3.str) : - Item_func_password::alloc(YYTHD, $3.str) : + Item_func_old_password::alloc(YYTHD, $3.str, $3.length) : + Item_func_password::alloc(YYTHD, $3.str, $3.length) : $3.str; if ($$ == NULL) MYSQL_YYABORT; } | OLD_PASSWORD '(' TEXT_STRING ')' { - $$= $3.length ? Item_func_old_password::alloc(YYTHD, $3.str) : + $$= $3.length ? Item_func_old_password::alloc(YYTHD, $3.str, + $3.length) : $3.str; if ($$ == NULL) MYSQL_YYABORT; @@ -12588,7 +12608,7 @@ grant_user: (char *) YYTHD->alloc(SCRAMBLED_PASSWORD_CHAR_LENGTH_323+1); if (buff == NULL) MYSQL_YYABORT; - make_scrambled_password_323(buff, $4.str); + my_make_scrambled_password_323(buff, $4.str, $4.length); $1->password.str= buff; $1->password.length= SCRAMBLED_PASSWORD_CHAR_LENGTH_323; } @@ -12598,7 +12618,7 @@ grant_user: (char *) YYTHD->alloc(SCRAMBLED_PASSWORD_CHAR_LENGTH+1); if (buff == NULL) MYSQL_YYABORT; - make_scrambled_password(buff, $4.str); + my_make_scrambled_password(buff, $4.str, $4.length); $1->password.str= buff; $1->password.length= SCRAMBLED_PASSWORD_CHAR_LENGTH; } diff --git a/sql/structs.h b/sql/structs.h index 0a20eee0e9a..a58c18f97c5 100644 --- a/sql/structs.h +++ b/sql/structs.h @@ -107,6 +107,10 @@ typedef struct st_reginfo { /* Extra info about reg */ struct st_join_table *join_tab; /* Used by SELECT() */ enum thr_lock_type lock_type; /* How database is used */ bool not_exists_optimize; + /* + TRUE <=> range optimizer found that there is no rows satisfying + table conditions. + */ bool impossible_range; } REGINFO; diff --git a/sql/table.cc b/sql/table.cc index 066bbc953fa..c1d79bdcdd3 100644 --- a/sql/table.cc +++ b/sql/table.cc @@ -464,34 +464,35 @@ inline bool is_system_table_name(const char *name, uint length) CHARSET_INFO *ci= system_charset_info; return ( - /* mysql.proc table */ - length == 4 && - my_tolower(ci, name[0]) == 'p' && - my_tolower(ci, name[1]) == 'r' && - my_tolower(ci, name[2]) == 'o' && - my_tolower(ci, name[3]) == 'c' || - - length > 4 && - ( - /* one of mysql.help* tables */ - my_tolower(ci, name[0]) == 'h' && - my_tolower(ci, name[1]) == 'e' && - my_tolower(ci, name[2]) == 'l' && - my_tolower(ci, name[3]) == 'p' || - - /* one of mysql.time_zone* tables */ - my_tolower(ci, name[0]) == 't' && - my_tolower(ci, name[1]) == 'i' && - my_tolower(ci, name[2]) == 'm' && - my_tolower(ci, name[3]) == 'e' || - - /* mysql.event table */ - my_tolower(ci, name[0]) == 'e' && - my_tolower(ci, name[1]) == 'v' && - my_tolower(ci, name[2]) == 'e' && - my_tolower(ci, name[3]) == 'n' && - my_tolower(ci, name[4]) == 't' - ) + /* mysql.proc table */ + (length == 4 && + my_tolower(ci, name[0]) == 'p' && + my_tolower(ci, name[1]) == 'r' && + my_tolower(ci, name[2]) == 'o' && + my_tolower(ci, name[3]) == 'c') || + + (length > 4 && + ( + /* one of mysql.help* tables */ + (my_tolower(ci, name[0]) == 'h' && + my_tolower(ci, name[1]) == 'e' && + my_tolower(ci, name[2]) == 'l' && + my_tolower(ci, name[3]) == 'p') || + + /* one of mysql.time_zone* tables */ + (my_tolower(ci, name[0]) == 't' && + my_tolower(ci, name[1]) == 'i' && + my_tolower(ci, name[2]) == 'm' && + my_tolower(ci, name[3]) == 'e') || + + /* mysql.event table */ + (my_tolower(ci, name[0]) == 'e' && + my_tolower(ci, name[1]) == 'v' && + my_tolower(ci, name[2]) == 'e' && + my_tolower(ci, name[3]) == 'n' && + my_tolower(ci, name[4]) == 't') + ) + ) ); } @@ -723,8 +724,6 @@ static int open_binary_frm(THD *thd, TABLE_SHARE *share, uchar *head, if (!head[32]) // New frm file in 3.23 { share->avg_row_length= uint4korr(head+34); - share->transactional= (ha_choice) (head[39] & 3); - share->page_checksum= (ha_choice) ((head[39] >> 2) & 3); share->row_type= (row_type) head[40]; share->table_charset= get_charset((uint) head[38],MYF(0)); share->null_field_first= 1; @@ -2491,8 +2490,11 @@ File create_frm(THD *thd, const char *name, const char *db, int4store(fileinfo+34,create_info->avg_row_length); fileinfo[38]= (create_info->default_table_charset ? create_info->default_table_charset->number : 0); - fileinfo[39]= (uchar) ((uint) create_info->transactional | - ((uint) create_info->page_checksum << 2)); + /* + In future versions, we will store in fileinfo[39] the values of the + TRANSACTIONAL and PAGE_CHECKSUM clauses of CREATE TABLE. + */ + fileinfo[39]= 0; fileinfo[40]= (uchar) create_info->row_type; /* Next few bytes where for RAID support */ fileinfo[41]= 0; @@ -3314,8 +3316,8 @@ bool TABLE_LIST::prep_check_option(THD *thd, uint8 check_opt_type) { const char *save_where= thd->where; thd->where= "check option"; - if (!check_option->fixed && - check_option->fix_fields(thd, &check_option) || + if ((!check_option->fixed && + check_option->fix_fields(thd, &check_option)) || check_option->check_cols(1)) { DBUG_RETURN(TRUE); @@ -3341,6 +3343,7 @@ void TABLE_LIST::hide_view_error(THD *thd) if (thd->main_da.sql_errno() == ER_BAD_FIELD_ERROR || thd->main_da.sql_errno() == ER_SP_DOES_NOT_EXIST || + thd->main_da.sql_errno() == ER_FUNC_INEXISTENT_NAME_COLLISION || thd->main_da.sql_errno() == ER_PROCACCESS_DENIED_ERROR || thd->main_da.sql_errno() == ER_COLUMNACCESS_DENIED_ERROR || thd->main_da.sql_errno() == ER_TABLEACCESS_DENIED_ERROR || @@ -4030,7 +4033,7 @@ void Field_iterator_table_ref::set_field_iterator() /* Necesary, but insufficient conditions. */ DBUG_ASSERT(table_ref->is_natural_join || table_ref->nested_join || - table_ref->join_columns && + (table_ref->join_columns && /* This is a merge view. */ ((table_ref->field_translation && table_ref->join_columns->elements == @@ -4039,7 +4042,7 @@ void Field_iterator_table_ref::set_field_iterator() /* This is stored table or a tmptable view. */ (!table_ref->field_translation && table_ref->join_columns->elements == - table_ref->table->s->fields))); + table_ref->table->s->fields)))); field_it= &natural_join_it; DBUG_PRINT("info",("field_it for '%s' is Field_iterator_natural_join", table_ref->alias)); diff --git a/sql/table.h b/sql/table.h index cb53013cd59..653d04b149e 100644 --- a/sql/table.h +++ b/sql/table.h @@ -361,7 +361,9 @@ typedef struct st_table_share } enum row_type row_type; /* How rows are stored */ enum tmp_table_type tmp_table; + /** Transactional or not. Unused; reserved for future versions. */ enum ha_choice transactional; + /** Per-page checksums or not. Unused; reserved for future versions. */ enum ha_choice page_checksum; uint ref_count; /* How many TABLE objects uses this */ @@ -753,7 +755,13 @@ struct st_table { */ my_bool force_index; my_bool distinct,const_table,no_rows; - my_bool key_read, no_keyread; + + /** + If set, the optimizer has found that row retrieval should access index + tree only. + */ + my_bool key_read; + my_bool no_keyread; /* Placeholder for an open table which prevents other connections from taking name-locks on this table. Typically used with diff --git a/sql/thr_malloc.cc b/sql/thr_malloc.cc index 6bf43b51df0..0764fe8be33 100644 --- a/sql/thr_malloc.cc +++ b/sql/thr_malloc.cc @@ -21,7 +21,7 @@ extern "C" { void sql_alloc_error_handler(void) { - sql_print_error(ER(ER_OUT_OF_RESOURCES)); + sql_print_error("%s", ER(ER_OUT_OF_RESOURCES)); THD *thd= current_thd; if (thd) diff --git a/sql/time.cc b/sql/time.cc index a6619cf4cee..962b65e454c 100644 --- a/sql/time.cc +++ b/sql/time.cc @@ -111,8 +111,8 @@ uint calc_week(MYSQL_TIME *l_time, uint week_behaviour, uint *year) if (l_time->month == 1 && l_time->day <= 7-weekday) { if (!week_year && - (first_weekday && weekday != 0 || - !first_weekday && weekday >= 4)) + ((first_weekday && weekday != 0) || + (!first_weekday && weekday >= 4))) return 0; week_year= 1; (*year)--; @@ -129,8 +129,8 @@ uint calc_week(MYSQL_TIME *l_time, uint week_behaviour, uint *year) if (week_year && days >= 52*7) { weekday= (weekday + calc_days_in_year(*year)) % 7; - if (!first_weekday && weekday < 4 || - first_weekday && weekday == 0) + if ((!first_weekday && weekday < 4) || + (first_weekday && weekday == 0)) { (*year)++; return 1; diff --git a/sql/tztime.cc b/sql/tztime.cc index 2a94e179600..c7a4ad049ec 100644 --- a/sql/tztime.cc +++ b/sql/tztime.cc @@ -447,8 +447,8 @@ prepare_tz_info(TIME_ZONE_INFO *sp, MEM_ROOT *storage) } if (end_t == MY_TIME_T_MAX || - (cur_off_and_corr > 0) && - (end_t >= MY_TIME_T_MAX - cur_off_and_corr)) + ((cur_off_and_corr > 0) && + (end_t >= MY_TIME_T_MAX - cur_off_and_corr))) /* end of t space */ break; diff --git a/sql/uniques.cc b/sql/uniques.cc index 858bedb04cd..7b6b628f924 100644 --- a/sql/uniques.cc +++ b/sql/uniques.cc @@ -603,9 +603,9 @@ bool Unique::get(TABLE *table) outfile=table->sort.io_cache=(IO_CACHE*) my_malloc(sizeof(IO_CACHE), MYF(MY_ZEROFILL)); - if (!outfile || ! my_b_inited(outfile) && + if (!outfile || (! my_b_inited(outfile) && open_cached_file(outfile,mysql_tmpdir,TEMP_PREFIX,READ_RECORD_BUFFER, - MYF(MY_WME))) + MYF(MY_WME)))) return 1; reinit_io_cache(outfile,WRITE_CACHE,0L,0,0); diff --git a/sql/unireg.h b/sql/unireg.h index 6f9c44d98f9..3ff7f058e3c 100644 --- a/sql/unireg.h +++ b/sql/unireg.h @@ -29,8 +29,8 @@ #define TEMP_PREFIX "MY" #define LOG_PREFIX "ML" #define PROGDIR "bin/" -#ifndef DATADIR -#define DATADIR "data/" +#ifndef MYSQL_DATADIR +#define MYSQL_DATADIR "data/" #endif #ifndef SHAREDIR #define SHAREDIR "share/" |