diff options
Diffstat (limited to 'sql')
72 files changed, 2786 insertions, 989 deletions
diff --git a/sql/Makefile.am b/sql/Makefile.am index 19bdf8055f3..f812bbe72af 100644 --- a/sql/Makefile.am +++ b/sql/Makefile.am @@ -60,7 +60,7 @@ noinst_HEADERS = item.h item_func.h item_sum.h item_cmpfunc.h \ stacktrace.h sql_sort.h sql_cache.h set_var.h \ spatial.h gstream.h client_settings.h tzfile.h \ tztime.h examples/ha_example.h examples/ha_archive.h \ - examples/ha_tina.h + examples/ha_tina.h ha_blackhole.h mysqld_SOURCES = sql_lex.cc sql_handler.cc \ item.cc item_sum.cc item_buff.cc item_func.cc \ item_cmpfunc.cc item_strfunc.cc item_timefunc.cc \ @@ -92,7 +92,7 @@ mysqld_SOURCES = sql_lex.cc sql_handler.cc \ gstream.cc spatial.cc sql_help.cc protocol_cursor.cc \ tztime.cc my_time.c \ examples/ha_example.cc examples/ha_archive.cc \ - examples/ha_tina.cc + examples/ha_tina.cc ha_blackhole.cc gen_lex_hash_SOURCES = gen_lex_hash.cc gen_lex_hash_LDADD = $(LDADD) $(CXXLDFLAGS) diff --git a/sql/examples/ha_archive.cc b/sql/examples/ha_archive.cc index 491056d0e59..bc4af0c7dc7 100644 --- a/sql/examples/ha_archive.cc +++ b/sql/examples/ha_archive.cc @@ -114,6 +114,8 @@ data - The data is stored in a "row +blobs" format. */ +/* If the archive storage engine has been inited */ +static bool archive_inited= 0; /* Variables for archive share methods */ pthread_mutex_t archive_mutex; static HASH archive_open_tables; @@ -157,6 +159,7 @@ static byte* archive_get_key(ARCHIVE_SHARE *share,uint *length, bool archive_db_init() { + archive_inited= 1; VOID(pthread_mutex_init(&archive_mutex, MY_MUTEX_INIT_FAST)); return (hash_init(&archive_open_tables, system_charset_info, 32, 0, 0, (hash_get_key) archive_get_key, 0, 0)); @@ -176,8 +179,12 @@ bool archive_db_init() bool archive_db_end() { - hash_free(&archive_open_tables); - VOID(pthread_mutex_destroy(&archive_mutex)); + if (archive_inited) + { + hash_free(&archive_open_tables); + VOID(pthread_mutex_destroy(&archive_mutex)); + } + archive_inited= 0; return FALSE; } diff --git a/sql/field.cc b/sql/field.cc index e2f75034e52..bf581e62f34 100644 --- a/sql/field.cc +++ b/sql/field.cc @@ -48,6 +48,793 @@ const char field_separator=','; #define DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE 320 +/* + Rules for merging different types of fields in UNION + + NOTE: to avoid 256*256 table, gap in table types numeration is skiped + following #defines describe that gap and how to canculate number of fields + and index of field in thia array. +*/ +#define FIELDTYPE_TEAR_FROM (MYSQL_TYPE_NEWDATE+1) +#define FIELDTYPE_TEAR_TO (MYSQL_TYPE_ENUM-1) +#define FIELDTYPE_NUM (FIELDTYPE_TEAR_FROM + (255-FIELDTYPE_TEAR_TO)) +inline int field_type2index (enum_field_types field_type) +{ + return (field_type < FIELDTYPE_TEAR_FROM ? + field_type : + ((int)FIELDTYPE_TEAR_FROM) + (field_type - FIELDTYPE_TEAR_TO) - 1); +} + +static enum_field_types field_types_merge_rules [FIELDTYPE_NUM][FIELDTYPE_NUM]= +{ + /* MYSQL_TYPE_DECIMAL -> */ + { + //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY + MYSQL_TYPE_DECIMAL, MYSQL_TYPE_DECIMAL, + //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG + MYSQL_TYPE_DECIMAL, MYSQL_TYPE_DECIMAL, + //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE + MYSQL_TYPE_DOUBLE, MYSQL_TYPE_DOUBLE, + //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP + MYSQL_TYPE_DECIMAL, MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24 + MYSQL_TYPE_DECIMAL, MYSQL_TYPE_DECIMAL, + //MYSQL_TYPE_DATE MYSQL_TYPE_TIME + MYSQL_TYPE_VAR_STRING, MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR + MYSQL_TYPE_VAR_STRING, MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_NEWDATE <14> + MYSQL_TYPE_VAR_STRING, + //<246> MYSQL_TYPE_ENUM + MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB + MYSQL_TYPE_VAR_STRING, MYSQL_TYPE_TINY_BLOB, + //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB + MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB, + //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING + MYSQL_TYPE_BLOB, MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY + MYSQL_TYPE_STRING, MYSQL_TYPE_VAR_STRING + }, + /* MYSQL_TYPE_TINY -> */ + { + //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY + MYSQL_TYPE_DECIMAL, MYSQL_TYPE_TINY, + //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG + MYSQL_TYPE_SHORT, MYSQL_TYPE_LONG, + //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE + MYSQL_TYPE_FLOAT, MYSQL_TYPE_DOUBLE, + //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP + MYSQL_TYPE_TINY, MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24 + MYSQL_TYPE_LONGLONG, MYSQL_TYPE_INT24, + //MYSQL_TYPE_DATE MYSQL_TYPE_TIME + MYSQL_TYPE_VAR_STRING, MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR + MYSQL_TYPE_VAR_STRING, MYSQL_TYPE_TINY, + //MYSQL_TYPE_NEWDATE <14> + MYSQL_TYPE_VAR_STRING, + //<246> MYSQL_TYPE_ENUM + MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB + MYSQL_TYPE_VAR_STRING, MYSQL_TYPE_TINY_BLOB, + //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB + MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB, + //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING + MYSQL_TYPE_BLOB, MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY + MYSQL_TYPE_STRING, MYSQL_TYPE_VAR_STRING + }, + /* MYSQL_TYPE_SHORT -> */ + { + //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY + MYSQL_TYPE_DECIMAL, MYSQL_TYPE_SHORT, + //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG + MYSQL_TYPE_SHORT, MYSQL_TYPE_LONG, + //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE + MYSQL_TYPE_FLOAT, MYSQL_TYPE_DOUBLE, + //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP + MYSQL_TYPE_SHORT, MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24 + MYSQL_TYPE_LONGLONG, MYSQL_TYPE_INT24, + //MYSQL_TYPE_DATE MYSQL_TYPE_TIME + MYSQL_TYPE_VAR_STRING, MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR + MYSQL_TYPE_VAR_STRING, MYSQL_TYPE_SHORT, + //MYSQL_TYPE_NEWDATE <14> + MYSQL_TYPE_VAR_STRING, + //<246> MYSQL_TYPE_ENUM + MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB + MYSQL_TYPE_VAR_STRING, MYSQL_TYPE_TINY_BLOB, + //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB + MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB, + //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING + MYSQL_TYPE_BLOB, MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY + MYSQL_TYPE_STRING, MYSQL_TYPE_VAR_STRING + }, + /* MYSQL_TYPE_LONG -> */ + { + //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY + MYSQL_TYPE_DECIMAL, MYSQL_TYPE_LONG, + //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG + MYSQL_TYPE_LONG, MYSQL_TYPE_LONG, + //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE + MYSQL_TYPE_DOUBLE, MYSQL_TYPE_DOUBLE, + //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP + MYSQL_TYPE_LONG, MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24 + MYSQL_TYPE_LONGLONG, MYSQL_TYPE_LONG, + //MYSQL_TYPE_DATE MYSQL_TYPE_TIME + MYSQL_TYPE_VAR_STRING, MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR + MYSQL_TYPE_VAR_STRING, MYSQL_TYPE_LONG, + //MYSQL_TYPE_NEWDATE <14> + MYSQL_TYPE_VAR_STRING, + //<246> MYSQL_TYPE_ENUM + MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB + MYSQL_TYPE_VAR_STRING, MYSQL_TYPE_TINY_BLOB, + //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB + MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB, + //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING + MYSQL_TYPE_BLOB, MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY + MYSQL_TYPE_STRING, MYSQL_TYPE_VAR_STRING + }, + /* MYSQL_TYPE_FLOAT -> */ + { + //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY + MYSQL_TYPE_DOUBLE, MYSQL_TYPE_FLOAT, + //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG + MYSQL_TYPE_FLOAT, MYSQL_TYPE_DOUBLE, + //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE + MYSQL_TYPE_FLOAT, MYSQL_TYPE_DOUBLE, + //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP + MYSQL_TYPE_FLOAT, MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24 + MYSQL_TYPE_DOUBLE, MYSQL_TYPE_FLOAT, + //MYSQL_TYPE_DATE MYSQL_TYPE_TIME + MYSQL_TYPE_VAR_STRING, MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR + MYSQL_TYPE_VAR_STRING, MYSQL_TYPE_FLOAT, + //MYSQL_TYPE_NEWDATE <14> + MYSQL_TYPE_VAR_STRING, + //<246> MYSQL_TYPE_ENUM + MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB + MYSQL_TYPE_VAR_STRING, MYSQL_TYPE_TINY_BLOB, + //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB + MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB, + //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING + MYSQL_TYPE_BLOB, MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY + MYSQL_TYPE_STRING, MYSQL_TYPE_VAR_STRING + }, + /* MYSQL_TYPE_DOUBLE -> */ + { + //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY + MYSQL_TYPE_DOUBLE, MYSQL_TYPE_DOUBLE, + //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG + MYSQL_TYPE_DOUBLE, MYSQL_TYPE_DOUBLE, + //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE + MYSQL_TYPE_DOUBLE, MYSQL_TYPE_DOUBLE, + //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP + MYSQL_TYPE_DOUBLE, MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24 + MYSQL_TYPE_DOUBLE, MYSQL_TYPE_DOUBLE, + //MYSQL_TYPE_DATE MYSQL_TYPE_TIME + MYSQL_TYPE_VAR_STRING, MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR + MYSQL_TYPE_VAR_STRING, MYSQL_TYPE_DOUBLE, + //MYSQL_TYPE_NEWDATE <14> + MYSQL_TYPE_VAR_STRING, + //<246> MYSQL_TYPE_ENUM + MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB + MYSQL_TYPE_VAR_STRING, MYSQL_TYPE_TINY_BLOB, + //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB + MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB, + //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING + MYSQL_TYPE_BLOB, MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY + MYSQL_TYPE_STRING, MYSQL_TYPE_VAR_STRING + }, + /* MYSQL_TYPE_NULL -> */ + { + //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY + MYSQL_TYPE_DECIMAL, MYSQL_TYPE_TINY, + //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG + MYSQL_TYPE_SHORT, MYSQL_TYPE_LONG, + //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE + MYSQL_TYPE_FLOAT, MYSQL_TYPE_DOUBLE, + //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP + MYSQL_TYPE_NULL, MYSQL_TYPE_TIMESTAMP, + //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24 + MYSQL_TYPE_LONGLONG, MYSQL_TYPE_INT24, + //MYSQL_TYPE_DATE MYSQL_TYPE_TIME + MYSQL_TYPE_NEWDATE, MYSQL_TYPE_TIME, + //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR + MYSQL_TYPE_DATETIME, MYSQL_TYPE_YEAR, + //MYSQL_TYPE_NEWDATE <14> + MYSQL_TYPE_NEWDATE, + //<246> MYSQL_TYPE_ENUM + MYSQL_TYPE_ENUM, + //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB + MYSQL_TYPE_SET, MYSQL_TYPE_TINY_BLOB, + //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB + MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB, + //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING + MYSQL_TYPE_BLOB, MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY + MYSQL_TYPE_STRING, MYSQL_TYPE_GEOMETRY + }, + /* MYSQL_TYPE_TIMESTAMP -> */ + { + //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY + MYSQL_TYPE_VAR_STRING, MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG + MYSQL_TYPE_VAR_STRING, MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE + MYSQL_TYPE_VAR_STRING, MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP + MYSQL_TYPE_TIMESTAMP, MYSQL_TYPE_TIMESTAMP, + //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24 + MYSQL_TYPE_VAR_STRING, MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_DATE MYSQL_TYPE_TIME + MYSQL_TYPE_DATETIME, MYSQL_TYPE_DATETIME, + //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR + MYSQL_TYPE_DATETIME, MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_NEWDATE <14> + MYSQL_TYPE_DATETIME, + //<246> MYSQL_TYPE_ENUM + MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB + MYSQL_TYPE_VAR_STRING, MYSQL_TYPE_TINY_BLOB, + //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB + MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB, + //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING + MYSQL_TYPE_BLOB, MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY + MYSQL_TYPE_STRING, MYSQL_TYPE_VAR_STRING + }, + /* MYSQL_TYPE_LONGLONG -> */ + { + //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY + MYSQL_TYPE_DECIMAL, MYSQL_TYPE_LONGLONG, + //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG + MYSQL_TYPE_LONGLONG, MYSQL_TYPE_LONGLONG, + //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE + MYSQL_TYPE_DOUBLE, MYSQL_TYPE_DOUBLE, + //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP + MYSQL_TYPE_LONGLONG, MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24 + MYSQL_TYPE_LONGLONG, MYSQL_TYPE_LONG, + //MYSQL_TYPE_DATE MYSQL_TYPE_TIME + MYSQL_TYPE_VAR_STRING, MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR + MYSQL_TYPE_VAR_STRING, MYSQL_TYPE_LONGLONG, + //MYSQL_TYPE_NEWDATE <14> + MYSQL_TYPE_VAR_STRING, + //<246> MYSQL_TYPE_ENUM + MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB + MYSQL_TYPE_VAR_STRING, MYSQL_TYPE_TINY_BLOB, + //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB + MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB, + //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING + MYSQL_TYPE_BLOB, MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY + MYSQL_TYPE_STRING, MYSQL_TYPE_VAR_STRING + }, + /* MYSQL_TYPE_INT24 -> */ + { + //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY + MYSQL_TYPE_DECIMAL, MYSQL_TYPE_INT24, + //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG + MYSQL_TYPE_INT24, MYSQL_TYPE_LONG, + //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE + MYSQL_TYPE_FLOAT, MYSQL_TYPE_DOUBLE, + //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP + MYSQL_TYPE_INT24, MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24 + MYSQL_TYPE_LONGLONG, MYSQL_TYPE_INT24, + //MYSQL_TYPE_DATE MYSQL_TYPE_TIME + MYSQL_TYPE_VAR_STRING, MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR + MYSQL_TYPE_VAR_STRING, MYSQL_TYPE_INT24, + //MYSQL_TYPE_NEWDATE <14> + MYSQL_TYPE_VAR_STRING, + //<246> MYSQL_TYPE_ENUM + MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB + MYSQL_TYPE_VAR_STRING, MYSQL_TYPE_TINY_BLOB, + //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB + MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB, + //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING + MYSQL_TYPE_BLOB, MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY + MYSQL_TYPE_STRING, MYSQL_TYPE_VAR_STRING + }, + /* MYSQL_TYPE_DATE -> */ + { + //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY + MYSQL_TYPE_VAR_STRING, MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG + MYSQL_TYPE_VAR_STRING, MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE + MYSQL_TYPE_VAR_STRING, MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP + MYSQL_TYPE_NEWDATE, MYSQL_TYPE_DATETIME, + //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24 + MYSQL_TYPE_VAR_STRING, MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_DATE MYSQL_TYPE_TIME + MYSQL_TYPE_NEWDATE, MYSQL_TYPE_DATETIME, + //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR + MYSQL_TYPE_DATETIME, MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_NEWDATE <14> + MYSQL_TYPE_NEWDATE, + //<246> MYSQL_TYPE_ENUM + MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB + MYSQL_TYPE_VAR_STRING, MYSQL_TYPE_TINY_BLOB, + //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB + MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB, + //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING + MYSQL_TYPE_BLOB, MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY + MYSQL_TYPE_STRING, MYSQL_TYPE_VAR_STRING + }, + /* MYSQL_TYPE_TIME -> */ + { + //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY + MYSQL_TYPE_VAR_STRING, MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG + MYSQL_TYPE_VAR_STRING, MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE + MYSQL_TYPE_VAR_STRING, MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP + MYSQL_TYPE_TIME, MYSQL_TYPE_DATETIME, + //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24 + MYSQL_TYPE_VAR_STRING, MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_DATE MYSQL_TYPE_TIME + MYSQL_TYPE_DATETIME, MYSQL_TYPE_TIME, + //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR + MYSQL_TYPE_DATETIME, MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_NEWDATE <14> + MYSQL_TYPE_DATETIME, + //<246> MYSQL_TYPE_ENUM + MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB + MYSQL_TYPE_VAR_STRING, MYSQL_TYPE_TINY_BLOB, + //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB + MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB, + //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING + MYSQL_TYPE_BLOB, MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY + MYSQL_TYPE_STRING, MYSQL_TYPE_VAR_STRING + }, + /* MYSQL_TYPE_DATETIME -> */ + { + //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY + MYSQL_TYPE_VAR_STRING, MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG + MYSQL_TYPE_VAR_STRING, MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE + MYSQL_TYPE_VAR_STRING, MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP + MYSQL_TYPE_DATETIME, MYSQL_TYPE_DATETIME, + //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24 + MYSQL_TYPE_VAR_STRING, MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_DATE MYSQL_TYPE_TIME + MYSQL_TYPE_DATETIME, MYSQL_TYPE_DATETIME, + //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR + MYSQL_TYPE_DATETIME, MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_NEWDATE <14> + MYSQL_TYPE_DATETIME, + //<246> MYSQL_TYPE_ENUM + MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB + MYSQL_TYPE_VAR_STRING, MYSQL_TYPE_TINY_BLOB, + //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB + MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB, + //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING + MYSQL_TYPE_BLOB, MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY + MYSQL_TYPE_STRING, MYSQL_TYPE_VAR_STRING + }, + /* MYSQL_TYPE_YEAR -> */ + { + //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY + MYSQL_TYPE_DECIMAL, MYSQL_TYPE_TINY, + //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG + MYSQL_TYPE_SHORT, MYSQL_TYPE_LONG, + //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE + MYSQL_TYPE_FLOAT, MYSQL_TYPE_DOUBLE, + //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP + MYSQL_TYPE_YEAR, MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24 + MYSQL_TYPE_LONGLONG, MYSQL_TYPE_INT24, + //MYSQL_TYPE_DATE MYSQL_TYPE_TIME + MYSQL_TYPE_VAR_STRING, MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR + MYSQL_TYPE_VAR_STRING, MYSQL_TYPE_YEAR, + //MYSQL_TYPE_NEWDATE <14> + MYSQL_TYPE_VAR_STRING, + //<246> MYSQL_TYPE_ENUM + MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB + MYSQL_TYPE_VAR_STRING, MYSQL_TYPE_TINY_BLOB, + //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB + MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB, + //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING + MYSQL_TYPE_BLOB, MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY + MYSQL_TYPE_STRING, MYSQL_TYPE_VAR_STRING + }, + /* MYSQL_TYPE_NEWDATE -> */ + { + //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY + MYSQL_TYPE_VAR_STRING, MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG + MYSQL_TYPE_VAR_STRING, MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE + MYSQL_TYPE_VAR_STRING, MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP + MYSQL_TYPE_NEWDATE, MYSQL_TYPE_DATETIME, + //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24 + MYSQL_TYPE_VAR_STRING, MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_DATE MYSQL_TYPE_TIME + MYSQL_TYPE_NEWDATE, MYSQL_TYPE_DATETIME, + //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR + MYSQL_TYPE_DATETIME, MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_NEWDATE <14> + MYSQL_TYPE_NEWDATE, + //<246> MYSQL_TYPE_ENUM + MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB + MYSQL_TYPE_VAR_STRING, MYSQL_TYPE_TINY_BLOB, + //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB + MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB, + //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING + MYSQL_TYPE_BLOB, MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY + MYSQL_TYPE_STRING, MYSQL_TYPE_VAR_STRING + }, + /* MYSQL_TYPE_ENUM -> */ + { + //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY + MYSQL_TYPE_VAR_STRING, MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG + MYSQL_TYPE_VAR_STRING, MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE + MYSQL_TYPE_VAR_STRING, MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP + MYSQL_TYPE_ENUM, MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24 + MYSQL_TYPE_VAR_STRING, MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_DATE MYSQL_TYPE_TIME + MYSQL_TYPE_VAR_STRING, MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR + MYSQL_TYPE_VAR_STRING, MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_NEWDATE <14> + MYSQL_TYPE_VAR_STRING, + //<246> MYSQL_TYPE_ENUM + MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB + MYSQL_TYPE_VAR_STRING, MYSQL_TYPE_TINY_BLOB, + //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB + MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB, + //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING + MYSQL_TYPE_BLOB, MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY + MYSQL_TYPE_STRING, MYSQL_TYPE_VAR_STRING + }, + /* MYSQL_TYPE_SET -> */ + { + //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY + MYSQL_TYPE_VAR_STRING, MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG + MYSQL_TYPE_VAR_STRING, MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE + MYSQL_TYPE_VAR_STRING, MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP + MYSQL_TYPE_SET, MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24 + MYSQL_TYPE_VAR_STRING, MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_DATE MYSQL_TYPE_TIME + MYSQL_TYPE_VAR_STRING, MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR + MYSQL_TYPE_VAR_STRING, MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_NEWDATE <14> + MYSQL_TYPE_VAR_STRING, + //<246> MYSQL_TYPE_ENUM + MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB + MYSQL_TYPE_VAR_STRING, MYSQL_TYPE_TINY_BLOB, + //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB + MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB, + //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING + MYSQL_TYPE_BLOB, MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY + MYSQL_TYPE_STRING, MYSQL_TYPE_VAR_STRING + }, + /* MYSQL_TYPE_TINY_BLOB -> */ + { + //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY + MYSQL_TYPE_TINY_BLOB, MYSQL_TYPE_TINY_BLOB, + //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG + MYSQL_TYPE_TINY_BLOB, MYSQL_TYPE_TINY_BLOB, + //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE + MYSQL_TYPE_TINY_BLOB, MYSQL_TYPE_TINY_BLOB, + //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP + MYSQL_TYPE_TINY_BLOB, MYSQL_TYPE_TINY_BLOB, + //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24 + MYSQL_TYPE_TINY_BLOB, MYSQL_TYPE_TINY_BLOB, + //MYSQL_TYPE_DATE MYSQL_TYPE_TIME + MYSQL_TYPE_TINY_BLOB, MYSQL_TYPE_TINY_BLOB, + //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR + MYSQL_TYPE_TINY_BLOB, MYSQL_TYPE_TINY_BLOB, + //MYSQL_TYPE_NEWDATE <14> + MYSQL_TYPE_TINY_BLOB, + //<246> MYSQL_TYPE_ENUM + MYSQL_TYPE_TINY_BLOB, + //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB + MYSQL_TYPE_TINY_BLOB, MYSQL_TYPE_TINY_BLOB, + //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB + MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB, + //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING + MYSQL_TYPE_BLOB, MYSQL_TYPE_TINY_BLOB, + //MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY + MYSQL_TYPE_TINY_BLOB, MYSQL_TYPE_TINY_BLOB + }, + /* MYSQL_TYPE_MEDIUM_BLOB -> */ + { + //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY + MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB, + //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG + MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB, + //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE + MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB, + //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP + MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB, + //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24 + MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB, + //MYSQL_TYPE_DATE MYSQL_TYPE_TIME + MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB, + //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR + MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB, + //MYSQL_TYPE_NEWDATE <14> + MYSQL_TYPE_MEDIUM_BLOB, + //<246> MYSQL_TYPE_ENUM + MYSQL_TYPE_MEDIUM_BLOB, + //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB + MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB, + //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB + MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB, + //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING + MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB, + //MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY + MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB + }, + /* MYSQL_TYPE_LONG_BLOB -> */ + { + //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY + MYSQL_TYPE_LONG_BLOB, MYSQL_TYPE_LONG_BLOB, + //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG + MYSQL_TYPE_LONG_BLOB, MYSQL_TYPE_LONG_BLOB, + //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE + MYSQL_TYPE_LONG_BLOB, MYSQL_TYPE_LONG_BLOB, + //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP + MYSQL_TYPE_LONG_BLOB, MYSQL_TYPE_LONG_BLOB, + //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24 + MYSQL_TYPE_LONG_BLOB, MYSQL_TYPE_LONG_BLOB, + //MYSQL_TYPE_DATE MYSQL_TYPE_TIME + MYSQL_TYPE_LONG_BLOB, MYSQL_TYPE_LONG_BLOB, + //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR + MYSQL_TYPE_LONG_BLOB, MYSQL_TYPE_LONG_BLOB, + //MYSQL_TYPE_NEWDATE <14> + MYSQL_TYPE_LONG_BLOB, + //<246> MYSQL_TYPE_ENUM + MYSQL_TYPE_LONG_BLOB, + //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB + MYSQL_TYPE_LONG_BLOB, MYSQL_TYPE_LONG_BLOB, + //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB + MYSQL_TYPE_LONG_BLOB, MYSQL_TYPE_LONG_BLOB, + //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING + MYSQL_TYPE_LONG_BLOB, MYSQL_TYPE_LONG_BLOB, + //MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY + MYSQL_TYPE_LONG_BLOB, MYSQL_TYPE_LONG_BLOB + }, + /* MYSQL_TYPE_BLOB -> */ + { + //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY + MYSQL_TYPE_BLOB, MYSQL_TYPE_BLOB, + //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG + MYSQL_TYPE_BLOB, MYSQL_TYPE_BLOB, + //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE + MYSQL_TYPE_BLOB, MYSQL_TYPE_BLOB, + //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP + MYSQL_TYPE_BLOB, MYSQL_TYPE_BLOB, + //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24 + MYSQL_TYPE_BLOB, MYSQL_TYPE_BLOB, + //MYSQL_TYPE_DATE MYSQL_TYPE_TIME + MYSQL_TYPE_BLOB, MYSQL_TYPE_BLOB, + //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR + MYSQL_TYPE_BLOB, MYSQL_TYPE_BLOB, + //MYSQL_TYPE_NEWDATE <14> + MYSQL_TYPE_BLOB, + //<246> MYSQL_TYPE_ENUM + MYSQL_TYPE_BLOB, + //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB + MYSQL_TYPE_BLOB, MYSQL_TYPE_BLOB, + //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB + MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB, + //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING + MYSQL_TYPE_BLOB, MYSQL_TYPE_BLOB, + //MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY + MYSQL_TYPE_BLOB, MYSQL_TYPE_BLOB + }, + /* MYSQL_TYPE_VAR_STRING -> */ + { + //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY + MYSQL_TYPE_VAR_STRING, MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG + MYSQL_TYPE_VAR_STRING, MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE + MYSQL_TYPE_VAR_STRING, MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP + MYSQL_TYPE_VAR_STRING, MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24 + MYSQL_TYPE_VAR_STRING, MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_DATE MYSQL_TYPE_TIME + MYSQL_TYPE_VAR_STRING, MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR + MYSQL_TYPE_VAR_STRING, MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_NEWDATE <14> + MYSQL_TYPE_VAR_STRING, + //<246> MYSQL_TYPE_ENUM + MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB + MYSQL_TYPE_VAR_STRING, MYSQL_TYPE_TINY_BLOB, + //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB + MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB, + //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING + MYSQL_TYPE_BLOB, MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY + MYSQL_TYPE_VAR_STRING, MYSQL_TYPE_VAR_STRING + }, + /* MYSQL_TYPE_STRING -> */ + { + //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY + MYSQL_TYPE_STRING, MYSQL_TYPE_STRING, + //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG + MYSQL_TYPE_STRING, MYSQL_TYPE_STRING, + //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE + MYSQL_TYPE_STRING, MYSQL_TYPE_STRING, + //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP + MYSQL_TYPE_STRING, MYSQL_TYPE_STRING, + //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24 + MYSQL_TYPE_STRING, MYSQL_TYPE_STRING, + //MYSQL_TYPE_DATE MYSQL_TYPE_TIME + MYSQL_TYPE_STRING, MYSQL_TYPE_STRING, + //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR + MYSQL_TYPE_STRING, MYSQL_TYPE_STRING, + //MYSQL_TYPE_NEWDATE <14> + MYSQL_TYPE_STRING, + //<246> MYSQL_TYPE_ENUM + MYSQL_TYPE_STRING, + //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB + MYSQL_TYPE_STRING, MYSQL_TYPE_TINY_BLOB, + //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB + MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB, + //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING + MYSQL_TYPE_BLOB, MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY + MYSQL_TYPE_STRING, MYSQL_TYPE_STRING + }, + /* MYSQL_TYPE_GEOMETRY -> */ + { + //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY + MYSQL_TYPE_VAR_STRING, MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG + MYSQL_TYPE_VAR_STRING, MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE + MYSQL_TYPE_VAR_STRING, MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP + MYSQL_TYPE_GEOMETRY, MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24 + MYSQL_TYPE_VAR_STRING, MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_DATE MYSQL_TYPE_TIME + MYSQL_TYPE_VAR_STRING, MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR + MYSQL_TYPE_VAR_STRING, MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_NEWDATE <14> + MYSQL_TYPE_VAR_STRING, + //<246> MYSQL_TYPE_ENUM + MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB + MYSQL_TYPE_VAR_STRING, MYSQL_TYPE_TINY_BLOB, + //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB + MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB, + //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING + MYSQL_TYPE_BLOB, MYSQL_TYPE_VAR_STRING, + //MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY + MYSQL_TYPE_STRING, MYSQL_TYPE_GEOMETRY + } +}; + +/* + Return type of which can carry value of both given types in UNION result + + SYNOPSIS + Field::field_type_merge() + a, b types for merging + + RETURN + type of field +*/ + +enum_field_types Field::field_type_merge(enum_field_types a, + enum_field_types b) +{ + DBUG_ASSERT(a < FIELDTYPE_TEAR_FROM || a > FIELDTYPE_TEAR_TO); + DBUG_ASSERT(b < FIELDTYPE_TEAR_FROM || b > FIELDTYPE_TEAR_TO); + return field_types_merge_rules[field_type2index(a)] + [field_type2index(b)]; +} + + +static Item_result field_types_result_type [FIELDTYPE_NUM]= +{ + //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY + REAL_RESULT, INT_RESULT, + //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG + INT_RESULT, INT_RESULT, + //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE + REAL_RESULT, REAL_RESULT, + //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP + STRING_RESULT, STRING_RESULT, + //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24 + INT_RESULT, INT_RESULT, + //MYSQL_TYPE_DATE MYSQL_TYPE_TIME + STRING_RESULT, STRING_RESULT, + //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR + STRING_RESULT, INT_RESULT, + //MYSQL_TYPE_NEWDATE <14> + STRING_RESULT, + //<246> MYSQL_TYPE_ENUM + STRING_RESULT, + //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB + STRING_RESULT, STRING_RESULT, + //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB + STRING_RESULT, STRING_RESULT, + //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING + STRING_RESULT, STRING_RESULT, + //MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY + STRING_RESULT, STRING_RESULT +}; + + +/* + Detect Item_result by given field type of UNION merge result + + SYNOPSIS + Field::result_merge_type() + field_type given field type + + RETURN + Item_result (type of internal MySQL expression result) +*/ + +Item_result Field::result_merge_type(enum_field_types field_type) +{ + DBUG_ASSERT(field_type < FIELDTYPE_TEAR_FROM || field_type + > FIELDTYPE_TEAR_TO); + return field_types_result_type[field_type2index(field_type)]; +} + /***************************************************************************** Static help functions *****************************************************************************/ @@ -164,156 +951,6 @@ static bool test_if_real(const char *str,int length, CHARSET_INFO *cs) #endif -/* - Tables of filed type compatibility. - - There are tables for every type, table consist of list of types in which - given type can be converted without data lost, list should be ended with - FIELD_CAST_STOP -*/ -static Field::field_cast_enum field_cast_decimal[]= -{Field::FIELD_CAST_DECIMAL, - Field::FIELD_CAST_STRING, Field::FIELD_CAST_VARSTRING, - Field::FIELD_CAST_BLOB, Field::FIELD_CAST_STOP}; -static Field::field_cast_enum field_cast_tiny[]= -{Field::FIELD_CAST_TINY, - Field::FIELD_CAST_SHORT, Field::FIELD_CAST_MEDIUM, Field::FIELD_CAST_LONG, - Field::FIELD_CAST_LONGLONG, - Field::FIELD_CAST_FLOAT, Field::FIELD_CAST_DOUBLE, - Field::FIELD_CAST_STRING, Field::FIELD_CAST_VARSTRING, - Field::FIELD_CAST_BLOB, Field::FIELD_CAST_STOP}; -static Field::field_cast_enum field_cast_short[]= -{Field::FIELD_CAST_SHORT, - Field::FIELD_CAST_MEDIUM, Field::FIELD_CAST_LONG, Field::FIELD_CAST_LONGLONG, - Field::FIELD_CAST_FLOAT, Field::FIELD_CAST_DOUBLE, - Field::FIELD_CAST_STRING, Field::FIELD_CAST_VARSTRING, - Field::FIELD_CAST_BLOB, Field::FIELD_CAST_STOP}; -static Field::field_cast_enum field_cast_medium[]= -{Field::FIELD_CAST_MEDIUM, - Field::FIELD_CAST_LONG, Field::FIELD_CAST_LONGLONG, - Field::FIELD_CAST_DOUBLE, - Field::FIELD_CAST_STRING, Field::FIELD_CAST_VARSTRING, - Field::FIELD_CAST_BLOB, Field::FIELD_CAST_STOP}; -static Field::field_cast_enum field_cast_long[]= -{Field::FIELD_CAST_LONG, - Field::FIELD_CAST_LONGLONG, - Field::FIELD_CAST_DOUBLE, - Field::FIELD_CAST_STRING, Field::FIELD_CAST_VARSTRING, - Field::FIELD_CAST_BLOB, Field::FIELD_CAST_STOP}; -static Field::field_cast_enum field_cast_longlong[]= -{Field::FIELD_CAST_LONGLONG, - Field::FIELD_CAST_DOUBLE, - Field::FIELD_CAST_STRING, Field::FIELD_CAST_VARSTRING, - Field::FIELD_CAST_BLOB, Field::FIELD_CAST_STOP}; -static Field::field_cast_enum field_cast_float[]= -{Field::FIELD_CAST_FLOAT, - Field::FIELD_CAST_DOUBLE, - Field::FIELD_CAST_STRING, Field::FIELD_CAST_VARSTRING, - Field::FIELD_CAST_BLOB, Field::FIELD_CAST_STOP}; -static Field::field_cast_enum field_cast_double[]= -{Field::FIELD_CAST_DOUBLE, - Field::FIELD_CAST_STRING, Field::FIELD_CAST_VARSTRING, - Field::FIELD_CAST_BLOB, Field::FIELD_CAST_STOP}; -static Field::field_cast_enum field_cast_null[]= -{Field::FIELD_CAST_NULL, - Field::FIELD_CAST_DECIMAL, Field::FIELD_CAST_TINY, Field::FIELD_CAST_SHORT, - Field::FIELD_CAST_MEDIUM, Field::FIELD_CAST_LONG, Field::FIELD_CAST_LONGLONG, - Field::FIELD_CAST_FLOAT, Field::FIELD_CAST_DOUBLE, - Field::FIELD_CAST_TIMESTAMP, Field::FIELD_CAST_YEAR, - Field::FIELD_CAST_DATE, Field::FIELD_CAST_NEWDATE, - Field::FIELD_CAST_TIME, Field::FIELD_CAST_DATETIME, - Field::FIELD_CAST_STRING, Field::FIELD_CAST_VARSTRING, - Field::FIELD_CAST_BLOB, - Field::FIELD_CAST_GEOM, Field::FIELD_CAST_ENUM, Field::FIELD_CAST_SET, - Field::FIELD_CAST_STOP}; -static Field::field_cast_enum field_cast_timestamp[]= -{Field::FIELD_CAST_TIMESTAMP, - Field::FIELD_CAST_DATETIME, - Field::FIELD_CAST_STRING, Field::FIELD_CAST_VARSTRING, - Field::FIELD_CAST_BLOB, Field::FIELD_CAST_STOP}; -static Field::field_cast_enum field_cast_year[]= -{Field::FIELD_CAST_YEAR, - Field::FIELD_CAST_SHORT, Field::FIELD_CAST_MEDIUM, Field::FIELD_CAST_LONG, - Field::FIELD_CAST_LONGLONG, - Field::FIELD_CAST_FLOAT, Field::FIELD_CAST_DOUBLE, - Field::FIELD_CAST_STRING, Field::FIELD_CAST_VARSTRING, - Field::FIELD_CAST_BLOB, Field::FIELD_CAST_STOP}; -static Field::field_cast_enum field_cast_date[]= -{Field::FIELD_CAST_DATE, - Field::FIELD_CAST_DATETIME, - Field::FIELD_CAST_STRING, Field::FIELD_CAST_VARSTRING, - Field::FIELD_CAST_BLOB, Field::FIELD_CAST_STOP}; -static Field::field_cast_enum field_cast_newdate[]= -{Field::FIELD_CAST_NEWDATE, - Field::FIELD_CAST_DATE, - Field::FIELD_CAST_DATETIME, - Field::FIELD_CAST_STRING, Field::FIELD_CAST_VARSTRING, - Field::FIELD_CAST_BLOB, Field::FIELD_CAST_STOP}; -static Field::field_cast_enum field_cast_time[]= -{Field::FIELD_CAST_TIME, - Field::FIELD_CAST_DATETIME, - Field::FIELD_CAST_STRING, Field::FIELD_CAST_VARSTRING, - Field::FIELD_CAST_BLOB, Field::FIELD_CAST_STOP}; -static Field::field_cast_enum field_cast_datetime[]= -{Field::FIELD_CAST_DATETIME, - Field::FIELD_CAST_STRING, Field::FIELD_CAST_VARSTRING, - Field::FIELD_CAST_BLOB, Field::FIELD_CAST_STOP}; -static Field::field_cast_enum field_cast_string[]= -{Field::FIELD_CAST_STRING, - Field::FIELD_CAST_VARSTRING, Field::FIELD_CAST_BLOB, Field::FIELD_CAST_STOP}; -static Field::field_cast_enum field_cast_varstring[]= -{Field::FIELD_CAST_VARSTRING, - Field::FIELD_CAST_BLOB, Field::FIELD_CAST_STOP}; -static Field::field_cast_enum field_cast_blob[]= -{Field::FIELD_CAST_BLOB, - Field::FIELD_CAST_STOP}; -/* - Geometrical, enum and set fields can be casted only to expressions -*/ -static Field::field_cast_enum field_cast_geom[]= -{Field::FIELD_CAST_STOP}; -static Field::field_cast_enum field_cast_enum[]= -{Field::FIELD_CAST_STOP}; -static Field::field_cast_enum field_cast_set[]= -{Field::FIELD_CAST_STOP}; -// Array of pointers on conversion table for all fields types casting -static Field::field_cast_enum *field_cast_array[]= -{0, //FIELD_CAST_STOP - field_cast_decimal, field_cast_tiny, field_cast_short, - field_cast_medium, field_cast_long, field_cast_longlong, - field_cast_float, field_cast_double, - field_cast_null, - field_cast_timestamp, field_cast_year, field_cast_date, field_cast_newdate, - field_cast_time, field_cast_datetime, - field_cast_string, field_cast_varstring, field_cast_blob, - field_cast_geom, field_cast_enum, field_cast_set -}; - - -/* - Check if field of given type can store a value of this field. - - SYNOPSIS - type type for test - - RETURN - 1 can - 0 can not -*/ - -bool Field::field_cast_compatible(Field::field_cast_enum type) -{ - DBUG_ASSERT(type != FIELD_CAST_STOP); - Field::field_cast_enum *array= field_cast_array[field_cast_type()]; - while (*array != FIELD_CAST_STOP) - { - if (*(array++) == type) - return 1; - } - return 0; -} - - /**************************************************************************** ** Functions for the base classes ** This is an unpacked number. @@ -373,9 +1010,15 @@ void Field_num::add_zerofill_and_unsigned(String &res) const void Field_num::make_field(Send_field *field) { /* table_cache_key is not set for temp tables */ - field->db_name= (orig_table->table_cache_key ? orig_table->table_cache_key : - ""); - field->org_table_name= orig_table->real_name; + if (orig_table->table_cache_key) + { + field->db_name= orig_table->table_cache_key; + field->org_table_name= orig_table->real_name; + } + else + { + field->db_name= field->org_table_name= ""; + } field->table_name= orig_table->table_name; field->col_name=field->org_col_name=field_name; field->charsetnr= charset()->number; @@ -389,9 +1032,15 @@ void Field_num::make_field(Send_field *field) void Field_str::make_field(Send_field *field) { /* table_cache_key is not set for temp tables */ - field->db_name= (orig_table->table_cache_key ? orig_table->table_cache_key : - ""); - field->org_table_name= orig_table->real_name; + if (orig_table->table_cache_key) + { + field->db_name= orig_table->table_cache_key; + field->org_table_name= orig_table->real_name; + } + else + { + field->db_name= field->org_table_name= ""; + } field->table_name= orig_table->table_name; field->col_name=field->org_col_name=field_name; field->charsetnr= charset()->number; @@ -968,7 +1617,9 @@ int Field_decimal::store(longlong nr) double Field_decimal::val_real(void) { int not_used; - return my_strntod(&my_charset_bin, ptr, field_length, NULL, ¬_used); + char *end_not_used; + return my_strntod(&my_charset_bin, ptr, field_length, &end_not_used, + ¬_used); } longlong Field_decimal::val_int(void) @@ -1776,6 +2427,24 @@ void Field_medium::sql_type(String &res) const ** long int ****************************************************************************/ +/* + A helper function to check whether the next character + in the string "s" is MINUS SIGN. +*/ +#ifdef HAVE_CHARSET_ucs2 +static bool test_if_minus(CHARSET_INFO *cs, + const char *s, const char *e) +{ + my_wc_t wc; + return cs->cset->mb_wc(cs, &wc, (uchar*) s, (uchar*) e) > 0 && wc == '-'; +} +#else +/* + If not UCS2 support is compiled then it is easier +*/ +#define test_if_minus(cs, s, e) (*s == '-') +#endif + int Field_long::store(const char *from,uint len,CHARSET_INFO *cs) { @@ -1790,7 +2459,7 @@ int Field_long::store(const char *from,uint len,CHARSET_INFO *cs) if (unsigned_flag) { - if (!len || *from == '-') + if (!len || test_if_minus(cs, from, from + len)) { tmp=0; // Set negative to 0 my_errno=ERANGE; @@ -2086,7 +2755,7 @@ int Field_longlong::store(const char *from,uint len,CHARSET_INFO *cs) my_errno=0; if (unsigned_flag) { - if (!len || *from == '-') + if (!len || test_if_minus(cs, from, from + len)) { tmp=0; // Set negative to 0 my_errno= ERANGE; @@ -4352,8 +5021,9 @@ int Field_string::store(longlong nr) double Field_string::val_real(void) { int not_used; + char *end_not_used; CHARSET_INFO *cs=charset(); - return my_strntod(cs,ptr,field_length,(char**)0,¬_used); + return my_strntod(cs, ptr, field_length, &end_not_used, ¬_used); } @@ -4569,7 +5239,9 @@ double Field_varstring::val_real(void) int not_used; uint length=uint2korr(ptr)+HA_KEY_BLOB_LENGTH; CHARSET_INFO *cs=charset(); - return my_strntod(cs, ptr+HA_KEY_BLOB_LENGTH, length, (char**)0, ¬_used); + char *end_not_used; + return my_strntod(cs, ptr+HA_KEY_BLOB_LENGTH, length, &end_not_used, + ¬_used); } @@ -4947,12 +5619,13 @@ double Field_blob::val_real(void) { int not_used; char *blob; + char *end_not_used; memcpy_fixed(&blob,ptr+packlength,sizeof(char*)); if (!blob) return 0.0; uint32 length=get_length(ptr); CHARSET_INFO *cs=charset(); - return my_strntod(cs,blob,length,(char**)0, ¬_used); + return my_strntod(cs,blob,length, &end_not_used, ¬_used); } @@ -6033,40 +6706,6 @@ Field *make_field(char *ptr, uint32 field_length, } -/* - Check if field_type is appropriate field type - to create field for tmp table using - item->tmp_table_field() method - - SYNOPSIS - field_types_to_be_kept() - field_type - field type - - NOTE - it is used in function get_holder_example_field() - from item.cc - - RETURN - 1 - can use item->tmp_table_field() method - 0 - can not use item->tmp_table_field() method - -*/ - -bool field_types_to_be_kept(enum_field_types field_type) -{ - switch (field_type) - { - case FIELD_TYPE_DATE: - case FIELD_TYPE_NEWDATE: - case FIELD_TYPE_TIME: - case FIELD_TYPE_DATETIME: - return 1; - default: - return 0; - } -} - - /* Create a field suitable for create of table */ create_field::create_field(Field *old_field,Field *orig_field) diff --git a/sql/field.h b/sql/field.h index fd0f2f9c2f1..5dc124aba35 100644 --- a/sql/field.h +++ b/sql/field.h @@ -31,6 +31,17 @@ class Protocol; struct st_cache_field; void field_conv(Field *to,Field *from); +inline uint get_enum_pack_length(int elements) +{ + return elements < 256 ? 1 : 2; +} + +inline uint get_set_pack_length(int elements) +{ + uint len= (elements + 7) / 8; + return len > 4 ? 8 : len; +} + class Field { Field(const Item &); /* Prevent use of these */ @@ -75,17 +86,6 @@ public: GEOM_GEOMETRYCOLLECTION = 7 }; enum imagetype { itRAW, itMBR}; - enum field_cast_enum - { - FIELD_CAST_STOP, FIELD_CAST_DECIMAL, FIELD_CAST_TINY, FIELD_CAST_SHORT, - FIELD_CAST_MEDIUM, FIELD_CAST_LONG, FIELD_CAST_LONGLONG, - FIELD_CAST_FLOAT, FIELD_CAST_DOUBLE, - FIELD_CAST_NULL, - FIELD_CAST_TIMESTAMP, FIELD_CAST_YEAR, FIELD_CAST_DATE, FIELD_CAST_NEWDATE, - FIELD_CAST_TIME, FIELD_CAST_DATETIME, - FIELD_CAST_STRING, FIELD_CAST_VARSTRING, FIELD_CAST_BLOB, - FIELD_CAST_GEOM, FIELD_CAST_ENUM, FIELD_CAST_SET - }; utype unireg_check; uint32 field_length; // Length of field @@ -119,6 +119,8 @@ public: virtual String *val_str(String*,String *)=0; virtual Item_result result_type () const=0; virtual Item_result cmp_type () const { return result_type(); } + static enum_field_types field_type_merge(enum_field_types, enum_field_types); + static Item_result result_merge_type(enum_field_types); bool eq(Field *field) { return ptr == field->ptr && null_ptr == field->null_ptr; } virtual bool eq_def(Field *field); virtual uint32 pack_length() const { return (uint32) field_length; } @@ -277,6 +279,7 @@ public: virtual bool get_date(TIME *ltime,uint fuzzydate); virtual bool get_time(TIME *ltime); virtual CHARSET_INFO *charset(void) const { return &my_charset_bin; } + virtual CHARSET_INFO *sort_charset(void) const { return charset(); } virtual bool has_charset(void) const { return FALSE; } virtual void set_charset(CHARSET_INFO *charset) { } bool set_warning(const unsigned int level, const unsigned int code, @@ -289,8 +292,6 @@ public: int cuted_increment); void set_datetime_warning(const uint level, const uint code, double nr, timestamp_type ts_type); - virtual field_cast_enum field_cast_type()= 0; - bool field_cast_compatible(field_cast_enum type); /* maximum possible display length */ virtual uint32 max_length()= 0; friend bool reopen_table(THD *,struct st_table *,bool); @@ -397,7 +398,6 @@ public: bool zero_pack() const { return 0; } void sql_type(String &str) const; uint32 max_length() { return field_length; } - field_cast_enum field_cast_type() { return FIELD_CAST_DECIMAL; } }; @@ -429,7 +429,6 @@ public: uint32 pack_length() const { return 1; } void sql_type(String &str) const; uint32 max_length() { return 4; } - field_cast_enum field_cast_type() { return FIELD_CAST_TINY; } }; @@ -466,7 +465,6 @@ public: uint32 pack_length() const { return 2; } void sql_type(String &str) const; uint32 max_length() { return 6; } - field_cast_enum field_cast_type() { return FIELD_CAST_SHORT; } }; @@ -498,7 +496,6 @@ public: uint32 pack_length() const { return 3; } void sql_type(String &str) const; uint32 max_length() { return 8; } - field_cast_enum field_cast_type() { return FIELD_CAST_MEDIUM; } }; @@ -535,7 +532,6 @@ public: uint32 pack_length() const { return 4; } void sql_type(String &str) const; uint32 max_length() { return 11; } - field_cast_enum field_cast_type() { return FIELD_CAST_LONG; } }; @@ -575,7 +571,6 @@ public: void sql_type(String &str) const; bool can_be_compared_as_longlong() const { return TRUE; } uint32 max_length() { return 20; } - field_cast_enum field_cast_type() { return FIELD_CAST_LONGLONG; } }; #endif @@ -610,7 +605,6 @@ public: uint32 pack_length() const { return sizeof(float); } void sql_type(String &str) const; uint32 max_length() { return 24; } - field_cast_enum field_cast_type() { return FIELD_CAST_FLOAT; } }; @@ -645,7 +639,6 @@ public: uint32 pack_length() const { return sizeof(double); } void sql_type(String &str) const; uint32 max_length() { return 53; } - field_cast_enum field_cast_type() { return FIELD_CAST_DOUBLE; } }; @@ -676,7 +669,6 @@ public: void sql_type(String &str) const; uint size_of() const { return sizeof(*this); } uint32 max_length() { return 4; } - field_cast_enum field_cast_type() { return FIELD_CAST_NULL; } }; @@ -728,7 +720,6 @@ public: } bool get_date(TIME *ltime,uint fuzzydate); bool get_time(TIME *ltime); - field_cast_enum field_cast_type() { return FIELD_CAST_TIMESTAMP; } timestamp_auto_set_type get_auto_set_type() const; }; @@ -752,7 +743,6 @@ public: bool send_binary(Protocol *protocol); void sql_type(String &str) const; bool can_be_compared_as_longlong() const { return TRUE; } - field_cast_enum field_cast_type() { return FIELD_CAST_YEAR; } }; @@ -785,7 +775,6 @@ public: void sql_type(String &str) const; bool can_be_compared_as_longlong() const { return TRUE; } bool zero_pack() const { return 1; } - field_cast_enum field_cast_type() { return FIELD_CAST_DATE; } }; class Field_newdate :public Field_str { @@ -817,7 +806,6 @@ public: bool zero_pack() const { return 1; } bool get_date(TIME *ltime,uint fuzzydate); bool get_time(TIME *ltime); - field_cast_enum field_cast_type() { return FIELD_CAST_NEWDATE; } }; @@ -852,7 +840,6 @@ public: void sql_type(String &str) const; bool can_be_compared_as_longlong() const { return TRUE; } bool zero_pack() const { return 1; } - field_cast_enum field_cast_type() { return FIELD_CAST_TIME; } }; @@ -890,7 +877,6 @@ public: bool zero_pack() const { return 1; } bool get_date(TIME *ltime,uint fuzzydate); bool get_time(TIME *ltime); - field_cast_enum field_cast_type() { return FIELD_CAST_DATETIME; } }; @@ -936,7 +922,6 @@ public: enum_field_types real_type() const { return FIELD_TYPE_STRING; } bool has_charset(void) const { return charset() == &my_charset_bin ? FALSE : TRUE; } - field_cast_enum field_cast_type() { return FIELD_CAST_STRING; } }; @@ -985,7 +970,6 @@ public: enum_field_types real_type() const { return FIELD_TYPE_VAR_STRING; } bool has_charset(void) const { return charset() == &my_charset_bin ? FALSE : TRUE; } - field_cast_enum field_cast_type() { return FIELD_CAST_VARSTRING; } }; @@ -1080,7 +1064,6 @@ public: uint size_of() const { return sizeof(*this); } bool has_charset(void) const { return charset() == &my_charset_bin ? FALSE : TRUE; } - field_cast_enum field_cast_type() { return FIELD_CAST_BLOB; } uint32 max_length(); }; @@ -1110,7 +1093,6 @@ public: void get_key_image(char *buff,uint length, CHARSET_INFO *cs,imagetype type); void set_key_image(char *buff,uint length, CHARSET_INFO *cs); - field_cast_enum field_cast_type() { return FIELD_CAST_GEOM; } }; #endif /*HAVE_SPATIAL*/ @@ -1152,7 +1134,8 @@ public: bool optimize_range(uint idx, uint part) { return 0; } bool eq_def(Field *field); bool has_charset(void) const { return TRUE; } - field_cast_enum field_cast_type() { return FIELD_CAST_ENUM; } + /* enum and set are sorted as integers */ + CHARSET_INFO *sort_charset(void) const { return &my_charset_bin; } }; @@ -1178,7 +1161,6 @@ public: void sql_type(String &str) const; enum_field_types real_type() const { return FIELD_TYPE_SET; } bool has_charset(void) const { return TRUE; } - field_cast_enum field_cast_type() { return FIELD_CAST_SET; } }; @@ -1265,7 +1247,6 @@ int set_field_to_null(Field *field); int set_field_to_null_with_conversions(Field *field, bool no_conversions); bool test_if_int(const char *str, int length, const char *int_end, CHARSET_INFO *cs); -bool field_types_to_be_kept(enum_field_types field_type); /* The following are for the interface with the .frm file diff --git a/sql/filesort.cc b/sql/filesort.cc index 24088210f1a..75b114fc140 100644 --- a/sql/filesort.cc +++ b/sql/filesort.cc @@ -452,8 +452,11 @@ static ha_rows find_all_keys(SORTPARAM *param, SQL_SELECT *select, if (*killed) { DBUG_PRINT("info",("Sort killed by user")); - (void) file->extra(HA_EXTRA_NO_CACHE); - file->ha_rnd_end(); + if (!indexfile && !quick_select) + { + (void) file->extra(HA_EXTRA_NO_CACHE); + file->ha_rnd_end(); + } DBUG_RETURN(HA_POS_ERROR); /* purecov: inspected */ } if (error == 0) @@ -656,12 +659,18 @@ static void make_sortkey(register SORTPARAM *param, to[3]= (uchar) (value >> 32); to[2]= (uchar) (value >> 40); to[1]= (uchar) (value >> 48); - to[0]= (uchar) (value >> 56) ^ 128; // Fix sign + if (item->unsigned_flag) /* Fix sign */ + to[0]= (uchar) (value >> 56); + else + to[0]= (uchar) (value >> 56) ^ 128; /* Reverse signbit */ #else to[3]= (uchar) value; to[2]= (uchar) (value >> 8); to[1]= (uchar) (value >> 16); - to[0]= (uchar) (value >> 24) ^ 128; // Fix sign + if (item->unsigned_flag) /* Fix sign */ + to[0]= (uchar) (value >> 24); + else + to[0]= (uchar) (value >> 24) ^ 128; /* Reverse signbit */ #endif break; } @@ -1127,7 +1136,7 @@ sortlength(SORT_FIELD *sortorder, uint s_length, bool *multi_byte_charset) else { sortorder->length=sortorder->field->pack_length(); - if (use_strnxfrm((cs=sortorder->field->charset()))) + if (use_strnxfrm((cs=sortorder->field->sort_charset()))) { sortorder->need_strxnfrm= 1; *multi_byte_charset= 1; diff --git a/sql/ha_berkeley.cc b/sql/ha_berkeley.cc index e33cd3fca1b..626201a38a6 100644 --- a/sql/ha_berkeley.cc +++ b/sql/ha_berkeley.cc @@ -340,8 +340,9 @@ void berkeley_cleanup_log_files(void) ** Berkeley DB tables *****************************************************************************/ +static const char *ha_bdb_bas_exts[]= { ha_berkeley_ext, NullS }; const char **ha_berkeley::bas_ext() const -{ static const char *ext[]= { ha_berkeley_ext, NullS }; return ext; } +{ return ha_bdb_bas_exts; } ulong ha_berkeley::index_flags(uint idx, uint part, bool all_parts) const diff --git a/sql/ha_blackhole.cc b/sql/ha_blackhole.cc new file mode 100644 index 00000000000..e34d5d723a4 --- /dev/null +++ b/sql/ha_blackhole.cc @@ -0,0 +1,188 @@ +/* Copyright (C) 2005 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ + + +#ifdef __GNUC__ +#pragma implementation // gcc: Class implementation +#endif + +#include <mysql_priv.h> + +#ifdef HAVE_BLACKHOLE_DB +#include "ha_blackhole.h" + + +const char **ha_blackhole::bas_ext() const +{ + static const char *ext[]= { NullS }; + return ext; +} + +int ha_blackhole::open(const char *name, int mode, uint test_if_locked) +{ + DBUG_ENTER("ha_blackhole::open"); + thr_lock_init(&thr_lock); + thr_lock_data_init(&thr_lock,&lock,NULL); + DBUG_RETURN(0); +} + +int ha_blackhole::close(void) +{ + DBUG_ENTER("ha_blackhole::close"); + thr_lock_delete(&thr_lock); + DBUG_RETURN(0); +} + +int ha_blackhole::create(const char *name, TABLE *table_arg, + HA_CREATE_INFO *create_info) +{ + DBUG_ENTER("ha_blackhole::create"); + DBUG_RETURN(0); +} + +const char *ha_blackhole::index_type(uint key_number) +{ + DBUG_ENTER("ha_blackhole::index_type"); + DBUG_RETURN((table->key_info[key_number].flags & HA_FULLTEXT) ? + "FULLTEXT" : + (table->key_info[key_number].flags & HA_SPATIAL) ? + "SPATIAL" : + (table->key_info[key_number].algorithm == HA_KEY_ALG_RTREE) ? + "RTREE" : + "BTREE"); +} + +int ha_blackhole::write_row(byte * buf) +{ + DBUG_ENTER("ha_blackhole::write_row"); + DBUG_RETURN(0); +} + +int ha_blackhole::rnd_init(bool scan) +{ + DBUG_ENTER("ha_blackhole::rnd_init"); + DBUG_RETURN(0); +} + + +int ha_blackhole::rnd_next(byte *buf) +{ + DBUG_ENTER("ha_blackhole::rnd_next"); + DBUG_RETURN(HA_ERR_END_OF_FILE); +} + + +int ha_blackhole::rnd_pos(byte * buf, byte *pos) +{ + DBUG_ENTER("ha_blackhole::rnd_pos"); + DBUG_ASSERT(0); + DBUG_RETURN(0); +} + + +void ha_blackhole::position(const byte *record) +{ + DBUG_ENTER("ha_blackhole::position"); + DBUG_ASSERT(0); + DBUG_VOID_RETURN; +} + + +void ha_blackhole::info(uint flag) +{ + DBUG_ENTER("ha_blackhole::info"); + + records= 0; + deleted= 0; + errkey= 0; + mean_rec_length= 0; + data_file_length= 0; + index_file_length= 0; + max_data_file_length= 0; + delete_length= 0; + if (flag & HA_STATUS_AUTO) + auto_increment_value= 1; + DBUG_VOID_RETURN; +} + +int ha_blackhole::external_lock(THD *thd, int lock_type) +{ + DBUG_ENTER("ha_blackhole::external_lock"); + DBUG_RETURN(0); +} + + +THR_LOCK_DATA **ha_blackhole::store_lock(THD *thd, + THR_LOCK_DATA **to, + enum thr_lock_type lock_type) +{ + *to++= &lock; + + return to; +} + + +int ha_blackhole::index_read(byte * buf, const byte * key, + uint key_len, enum ha_rkey_function find_flag) +{ + DBUG_ENTER("ha_blackhole::index_read"); + DBUG_RETURN(0); +} + + +int ha_blackhole::index_read_idx(byte * buf, uint idx, const byte * key, + uint key_len, enum ha_rkey_function find_flag) +{ + DBUG_ENTER("ha_blackhole::index_read_idx"); + DBUG_RETURN(HA_ERR_END_OF_FILE); +} + + +int ha_blackhole::index_read_last(byte * buf, const byte * key, uint key_len) +{ + DBUG_ENTER("ha_blackhole::index_read_last"); + DBUG_RETURN(HA_ERR_END_OF_FILE); +} + + +int ha_blackhole::index_next(byte * buf) +{ + DBUG_ENTER("ha_blackhole::index_next"); + DBUG_RETURN(HA_ERR_END_OF_FILE); +} + + +int ha_blackhole::index_prev(byte * buf) +{ + DBUG_ENTER("ha_blackhole::index_prev"); + DBUG_RETURN(HA_ERR_END_OF_FILE); +} + + +int ha_blackhole::index_first(byte * buf) +{ + DBUG_ENTER("ha_blackhole::index_first"); + DBUG_RETURN(HA_ERR_END_OF_FILE); +} + + +int ha_blackhole::index_last(byte * buf) +{ + DBUG_ENTER("ha_blackhole::index_last"); + DBUG_RETURN(HA_ERR_END_OF_FILE); +} + +#endif /* HAVE_BLACKHOLE_DB */ diff --git a/sql/ha_blackhole.h b/sql/ha_blackhole.h new file mode 100644 index 00000000000..b6f924e94b9 --- /dev/null +++ b/sql/ha_blackhole.h @@ -0,0 +1,88 @@ +/* Copyright (C) 2005 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ + +#ifdef __GNUC__ +#pragma interface /* gcc class implementation */ +#endif + +/* + Class definition for the blackhole storage engine + "Dumbest named feature ever" +*/ +class ha_blackhole: public handler +{ + THR_LOCK_DATA lock; /* MySQL lock */ + THR_LOCK thr_lock; + +public: + ha_blackhole(TABLE *table): handler(table) + { + } + ~ha_blackhole() + { + } + /* The name that will be used for display purposes */ + const char *table_type() const { return "BLACKHOLE"; } + /* + The name of the index type that will be used for display + don't implement this method unless you really have indexes + */ + const char *index_type(uint key_number); + const char **bas_ext() const; + ulong table_flags() const + { + return(HA_NULL_IN_KEY | HA_CAN_FULLTEXT | HA_CAN_SQL_HANDLER | + HA_DUPP_POS | HA_CAN_INDEX_BLOBS | HA_AUTO_PART_KEY | + HA_FILE_BASED | HA_CAN_GEOMETRY | HA_READ_RND_SAME | + HA_CAN_INSERT_DELAYED); + } + ulong index_flags(uint inx, uint part, bool all_parts) const + { + return ((table->key_info[inx].algorithm == HA_KEY_ALG_FULLTEXT) ? + 0 : HA_READ_NEXT | HA_READ_PREV | HA_READ_RANGE | + HA_READ_ORDER | HA_KEYREAD_ONLY); + } + /* The following defines can be increased if necessary */ +#define BLACKHOLE_MAX_KEY 64 /* Max allowed keys */ +#define BLACKHOLE_MAX_KEY_SEG 16 /* Max segments for key */ +#define BLACKHOLE_MAX_KEY_LENGTH 1000 + uint max_supported_keys() const { return BLACKHOLE_MAX_KEY; } + uint max_supported_key_length() const { return BLACKHOLE_MAX_KEY_LENGTH; } + uint max_supported_key_part_length() const { return BLACKHOLE_MAX_KEY_LENGTH; } + int open(const char *name, int mode, uint test_if_locked); + int close(void); + int write_row(byte * buf); + int rnd_init(bool scan); + int rnd_next(byte *buf); + int rnd_pos(byte * buf, byte *pos); + int index_read(byte * buf, const byte * key, + uint key_len, enum ha_rkey_function find_flag); + int index_read_idx(byte * buf, uint idx, const byte * key, + uint key_len, enum ha_rkey_function find_flag); + int index_read_last(byte * buf, const byte * key, uint key_len); + int index_next(byte * buf); + int index_prev(byte * buf); + int index_first(byte * buf); + int index_last(byte * buf); + void position(const byte *record); + void info(uint flag); + int external_lock(THD *thd, int lock_type); + int create(const char *name, TABLE *table_arg, + HA_CREATE_INFO *create_info); + THR_LOCK_DATA **store_lock(THD *thd, + THR_LOCK_DATA **to, + enum thr_lock_type lock_type); +}; diff --git a/sql/ha_heap.cc b/sql/ha_heap.cc index 3c2249ce281..4dc48c7422b 100644 --- a/sql/ha_heap.cc +++ b/sql/ha_heap.cc @@ -506,9 +506,15 @@ int ha_heap::create(const char *name, TABLE *table_arg, seg->null_bit= 0; seg->null_pos= 0; } - if (field->flags & AUTO_INCREMENT_FLAG) + if (field->flags & AUTO_INCREMENT_FLAG && + table_arg->found_next_number_field && + key == table_arg->next_number_index) { - auto_key= key + 1; + /* + Store key number and type for found auto_increment key + We have to store type as seg->type can differ from it + */ + auto_key= key+ 1; auto_key_type= field->key_type(); } } diff --git a/sql/ha_innodb.cc b/sql/ha_innodb.cc index 702139624ff..0bdd620706d 100644 --- a/sql/ha_innodb.cc +++ b/sql/ha_innodb.cc @@ -472,7 +472,7 @@ innobase_mysql_tmpfile(void) { char filename[FN_REFLEN]; int fd2 = -1; - File fd = create_temp_file(filename, NullS, "ib", + File fd = create_temp_file(filename, mysql_tmpdir, "ib", #ifdef __WIN__ O_BINARY | O_TRUNC | O_SEQUENTIAL | O_TEMPORARY | O_SHORT_LIVED | @@ -2316,7 +2316,10 @@ ha_innobase::write_row( if (table->timestamp_field_type & TIMESTAMP_AUTO_SET_ON_INSERT) table->timestamp_field->set_time(); - if (user_thd->lex->sql_command == SQLCOM_ALTER_TABLE + if ((user_thd->lex->sql_command == SQLCOM_ALTER_TABLE + || user_thd->lex->sql_command == SQLCOM_OPTIMIZE + || user_thd->lex->sql_command == SQLCOM_CREATE_INDEX + || user_thd->lex->sql_command == SQLCOM_DROP_INDEX) && num_write_row >= 10000) { /* ALTER TABLE is COMMITted at every 10000 copied rows. The IX table lock for the original table has to be re-issued. diff --git a/sql/ha_innodb.h b/sql/ha_innodb.h index e76a966c6b9..f10785b695d 100644 --- a/sql/ha_innodb.h +++ b/sql/ha_innodb.h @@ -202,7 +202,6 @@ extern "C" { extern ulong srv_max_buf_pool_modified_pct; extern ulong srv_max_purge_lag; extern ulong srv_auto_extend_increment; -extern ulong srv_max_purge_lag; } extern TYPELIB innobase_lock_typelib; diff --git a/sql/ha_myisam.cc b/sql/ha_myisam.cc index a7beae664b9..7ddb7ca25ed 100644 --- a/sql/ha_myisam.cc +++ b/sql/ha_myisam.cc @@ -1334,7 +1334,7 @@ int ha_myisam::create(const char *name, register TABLE *table_arg, HA_CREATE_INFO *info) { int error; - uint i,j,recpos,minpos,fieldpos,temp_length,length; + uint i,j,recpos,minpos,fieldpos,temp_length,length, create_flags= 0; bool found_real_auto_increment=0; enum ha_base_keytype type; char buff[FN_REFLEN]; @@ -1510,17 +1510,21 @@ int ha_myisam::create(const char *name, register TABLE *table_arg, create_info.data_file_name= info->data_file_name; create_info.index_file_name=info->index_file_name; + if (info->options & HA_LEX_CREATE_TMP_TABLE) + create_flags|= HA_CREATE_TMP_TABLE; + if (options & HA_OPTION_PACK_RECORD) + create_flags|= HA_PACK_RECORD; + if (options & HA_OPTION_CHECKSUM) + create_flags|= HA_CREATE_CHECKSUM; + if (options & HA_OPTION_DELAY_KEY_WRITE) + create_flags|= HA_CREATE_DELAY_KEY_WRITE; + /* TODO: Check that the following fn_format is really needed */ error=mi_create(fn_format(buff,name,"","",2+4), table_arg->keys,keydef, (uint) (recinfo_pos-recinfo), recinfo, 0, (MI_UNIQUEDEF*) 0, - &create_info, - (((options & HA_OPTION_PACK_RECORD) ? HA_PACK_RECORD : 0) | - ((options & HA_OPTION_CHECKSUM) ? HA_CREATE_CHECKSUM : 0) | - ((options & HA_OPTION_DELAY_KEY_WRITE) ? - HA_CREATE_DELAY_KEY_WRITE : 0))); - + &create_info, create_flags); my_free((gptr) recinfo,MYF(0)); DBUG_RETURN(error); diff --git a/sql/ha_ndbcluster.cc b/sql/ha_ndbcluster.cc index 9e34baae198..7025ac2cd1a 100644 --- a/sql/ha_ndbcluster.cc +++ b/sql/ha_ndbcluster.cc @@ -225,7 +225,7 @@ Ndb *ha_ndbcluster::get_ndb() * manage uncommitted insert/deletes during transactio to get records correct */ -struct Ndb_table_local_info { +struct Ndb_local_table_statistics { int no_uncommitted_rows_count; ulong last_count; ha_rows records; @@ -246,7 +246,8 @@ void ha_ndbcluster::records_update() if (m_ha_not_exact_count) return; DBUG_ENTER("ha_ndbcluster::records_update"); - struct Ndb_table_local_info *info= (struct Ndb_table_local_info *)m_table_info; + struct Ndb_local_table_statistics *info= + (struct Ndb_local_table_statistics *)m_table_info; DBUG_PRINT("info", ("id=%d, no_uncommitted_rows_count=%d", ((const NDBTAB *)m_table)->getTableId(), info->no_uncommitted_rows_count)); @@ -282,7 +283,8 @@ void ha_ndbcluster::no_uncommitted_rows_init(THD *thd) if (m_ha_not_exact_count) return; DBUG_ENTER("ha_ndbcluster::no_uncommitted_rows_init"); - struct Ndb_table_local_info *info= (struct Ndb_table_local_info *)m_table_info; + struct Ndb_local_table_statistics *info= + (struct Ndb_local_table_statistics *)m_table_info; Thd_ndb *thd_ndb= (Thd_ndb *)thd->transaction.thd_ndb; if (info->last_count != thd_ndb->count) { @@ -301,8 +303,8 @@ void ha_ndbcluster::no_uncommitted_rows_update(int c) if (m_ha_not_exact_count) return; DBUG_ENTER("ha_ndbcluster::no_uncommitted_rows_update"); - struct Ndb_table_local_info *info= - (struct Ndb_table_local_info *)m_table_info; + struct Ndb_local_table_statistics *info= + (struct Ndb_local_table_statistics *)m_table_info; info->no_uncommitted_rows_count+= c; DBUG_PRINT("info", ("id=%d, no_uncommitted_rows_count=%d", ((const NDBTAB *)m_table)->getTableId(), @@ -328,6 +330,35 @@ void ha_ndbcluster::no_uncommitted_rows_reset(THD *thd) # The mapped error code */ +void ha_ndbcluster::invalidateDictionaryCache() +{ + NDBDICT *dict= get_ndb()->getDictionary(); + DBUG_PRINT("info", ("invalidating %s", m_tabname)); + dict->invalidateTable(m_tabname); + table->version=0L; /* Free when thread is ready */ + /* Invalidate indexes */ + for (uint i= 0; i < table->keys; i++) + { + NDBINDEX *index = (NDBINDEX *) m_index[i].index; + NDBINDEX *unique_index = (NDBINDEX *) m_index[i].unique_index; + NDB_INDEX_TYPE idx_type= m_index[i].type; + + switch(idx_type) { + case(PRIMARY_KEY_ORDERED_INDEX): + case(ORDERED_INDEX): + dict->invalidateIndex(index->getName(), m_tabname); + break; + case(UNIQUE_ORDERED_INDEX): + dict->invalidateIndex(index->getName(), m_tabname); + case(UNIQUE_INDEX): + dict->invalidateIndex(unique_index->getName(), m_tabname); + break; + case(PRIMARY_KEY_INDEX): + case(UNDEFINED_INDEX): + break; + } + } +} int ha_ndbcluster::ndb_err(NdbConnection *trans) { @@ -339,11 +370,7 @@ int ha_ndbcluster::ndb_err(NdbConnection *trans) switch (err.classification) { case NdbError::SchemaError: { - Ndb *ndb= get_ndb(); - NDBDICT *dict= ndb->getDictionary(); - DBUG_PRINT("info", ("invalidateTable %s", m_tabname)); - dict->invalidateTable(m_tabname); - table->version=0L; /* Free when thread is ready */ + invalidateDictionaryCache(); break; } default: @@ -691,10 +718,8 @@ bool ha_ndbcluster::uses_blob_value(bool all_fields) Get metadata for this table from NDB IMPLEMENTATION - - save the NdbDictionary::Table for easy access - check that frm-file on disk is equal to frm-file of table accessed in NDB - - build a list of the indexes for the table */ int ha_ndbcluster::get_metadata(const char *path) @@ -733,7 +758,7 @@ int ha_ndbcluster::get_metadata(const char *path) if (!invalidating_ndb_table) { DBUG_PRINT("info", ("Invalidating table")); - dict->invalidateTable(m_tabname); + invalidateDictionaryCache(); invalidating_ndb_table= TRUE; } else @@ -758,11 +783,12 @@ int ha_ndbcluster::get_metadata(const char *path) if (error) DBUG_RETURN(error); - - m_table= NULL; - m_table_info= NULL; - DBUG_RETURN(build_index_list(table, ILBP_OPEN)); + m_tableVersion= tab->getObjectVersion(); + m_table= (void *)tab; + m_table_info= NULL; // Set in external lock + + DBUG_RETURN(build_index_list(ndb, table, ILBP_OPEN)); } static int fix_unique_index_attr_order(NDB_INDEX_DATA &data, @@ -790,7 +816,7 @@ static int fix_unique_index_attr_order(NDB_INDEX_DATA &data, #endif for (unsigned j= 0; j < sz; j++) { - const NdbDictionary::Column *c= index->getColumn(j); + const NDBCOL *c= index->getColumn(j); if (strncmp(field_name, c->getName(), name_sz) == 0) { data.unique_index_attrid_map[i]= j; @@ -802,7 +828,7 @@ static int fix_unique_index_attr_order(NDB_INDEX_DATA &data, DBUG_RETURN(0); } -int ha_ndbcluster::build_index_list(TABLE *tab, enum ILBP phase) +int ha_ndbcluster::build_index_list(Ndb *ndb, TABLE *tab, enum ILBP phase) { uint i; int error= 0; @@ -811,8 +837,7 @@ int ha_ndbcluster::build_index_list(TABLE *tab, enum ILBP phase) static const char* unique_suffix= "$unique"; KEY* key_info= tab->key_info; const char **key_name= tab->keynames.type_names; - Ndb *ndb= get_ndb(); - NdbDictionary::Dictionary *dict= ndb->getDictionary(); + NDBDICT *dict= ndb->getDictionary(); DBUG_ENTER("build_index_list"); // Save information about all known indexes @@ -1928,7 +1953,11 @@ int ha_ndbcluster::update_row(const byte *old_data, byte *new_data) statistic_increment(ha_update_count,&LOCK_status); if (table->timestamp_field_type & TIMESTAMP_AUTO_SET_ON_UPDATE) + { table->timestamp_field->set_time(); + // Set query_id so that field is really updated + table->timestamp_field->query_id= thd->query_id; + } /* Check for update of primary key for special handling */ if ((table->primary_key != MAX_KEY) && @@ -2276,10 +2305,14 @@ void ha_ndbcluster::print_results() fprintf(DBUG_FILE, "Double\t%f", value); break; } - case NdbDictionary::Column::Decimal: { + case NdbDictionary::Column::Olddecimal: { char *value= field->ptr; - - fprintf(DBUG_FILE, "Decimal\t'%-*s'", field->pack_length(), value); + fprintf(DBUG_FILE, "Olddecimal\t'%-*s'", field->pack_length(), value); + break; + } + case NdbDictionary::Column::Olddecimalunsigned: { + char *value= field->ptr; + fprintf(DBUG_FILE, "Olddecimalunsigned\t'%-*s'", field->pack_length(), value); break; } case NdbDictionary::Column::Char:{ @@ -2950,9 +2983,9 @@ int ha_ndbcluster::reset() DBUG_RETURN(1); } - +static const char *ha_ndb_bas_exts[]= { ha_ndb_ext, NullS }; const char **ha_ndbcluster::bas_ext() const -{ static const char *ext[]= { ha_ndb_ext, NullS }; return ext; } +{ return ha_ndb_bas_exts; } /* @@ -3031,6 +3064,10 @@ THR_LOCK_DATA **ha_ndbcluster::store_lock(THD *thd, When a table lock is held one transaction will be started which holds the table lock and for each statement a hupp transaction will be started + If we are locking the table then: + - save the NdbDictionary::Table for easy access + - save reference to table statistics + - refresh list of the indexes for the table if needed (if altered) */ int ha_ndbcluster::external_lock(THD *thd, int lock_type) @@ -3137,7 +3174,15 @@ int ha_ndbcluster::external_lock(THD *thd, int lock_type) if (!(tab= dict->getTable(m_tabname, &tab_info))) ERR_RETURN(dict->getNdbError()); DBUG_PRINT("info", ("Table schema version: %d", tab->getObjectVersion())); - m_table= (void *)tab; + if (m_table != (void *)tab || m_tableVersion != tab->getObjectVersion()) + { + /* + The table has been altered, refresh the index list + */ + build_index_list(ndb, table, ILBP_OPEN); + m_table= (void *)tab; + m_tableVersion = tab->getObjectVersion(); + } m_table_info= tab_info; } no_uncommitted_rows_init(thd); @@ -3312,10 +3357,6 @@ static int create_ndb_column(NDBCOL &col, const enum enum_field_types mysql_type= field->real_type(); switch (mysql_type) { // Numeric types - case MYSQL_TYPE_DECIMAL: - col.setType(NDBCOL::Char); - col.setLength(field->pack_length()); - break; case MYSQL_TYPE_TINY: if (field->flags & UNSIGNED_FLAG) col.setType(NDBCOL::Tinyunsigned); @@ -3359,6 +3400,26 @@ static int create_ndb_column(NDBCOL &col, col.setType(NDBCOL::Double); col.setLength(1); break; + case MYSQL_TYPE_DECIMAL: + { + Field_decimal *f= (Field_decimal*)field; + uint precision= f->pack_length(); + uint scale= f->decimals(); + if (field->flags & UNSIGNED_FLAG) + { + col.setType(NDBCOL::Olddecimalunsigned); + precision-= (scale > 0); + } + else + { + col.setType(NDBCOL::Olddecimal); + precision-= 1 + (scale > 0); + } + col.setPrecision(precision); + col.setScale(scale); + col.setLength(1); + } + break; // Date types case MYSQL_TYPE_DATETIME: col.setType(NDBCOL::Datetime); @@ -3671,7 +3732,7 @@ int ha_ndbcluster::create(const char *name, m_dbname, m_tabname)); // Create secondary indexes - my_errno= build_index_list(form, ILBP_CREATE); + my_errno= build_index_list(ndb, form, ILBP_CREATE); if (!my_errno) my_errno= write_ndb_file(); @@ -3891,6 +3952,7 @@ ha_ndbcluster::ha_ndbcluster(TABLE *table_arg): m_active_trans(NULL), m_active_cursor(NULL), m_table(NULL), + m_tableVersion(-1), m_table_info(NULL), m_table_flags(HA_REC_NOT_IN_SEQ | HA_NULL_IN_KEY | @@ -4030,7 +4092,9 @@ Thd_ndb* ha_ndbcluster::seize_thd_ndb() DBUG_ENTER("seize_thd_ndb"); thd_ndb= new Thd_ndb(); - thd_ndb->ndb->getDictionary()->set_local_table_data_size(sizeof(Ndb_table_local_info)); + thd_ndb->ndb->getDictionary()->set_local_table_data_size( + sizeof(Ndb_local_table_statistics) + ); if (thd_ndb->ndb->init(max_transactions) != 0) { ERR_PRINT(thd_ndb->ndb->getNdbError()); @@ -4123,7 +4187,7 @@ int ndbcluster_discover(THD* thd, const char *db, const char *name, ndb->setDatabaseName(db); NDBDICT* dict= ndb->getDictionary(); - dict->set_local_table_data_size(sizeof(Ndb_table_local_info)); + dict->set_local_table_data_size(sizeof(Ndb_local_table_statistics)); dict->invalidateTable(name); if (!(tab= dict->getTable(name))) { @@ -4171,7 +4235,7 @@ int ndbcluster_table_exists(THD* thd, const char *db, const char *name) ndb->setDatabaseName(db); NDBDICT* dict= ndb->getDictionary(); - dict->set_local_table_data_size(sizeof(Ndb_table_local_info)); + dict->set_local_table_data_size(sizeof(Ndb_local_table_statistics)); dict->invalidateTable(name); if (!(tab= dict->getTable(name))) { @@ -4375,7 +4439,7 @@ bool ndbcluster_init() // Create a Ndb object to open the connection to NDB g_ndb= new Ndb(g_ndb_cluster_connection, "sys"); - g_ndb->getDictionary()->set_local_table_data_size(sizeof(Ndb_table_local_info)); + g_ndb->getDictionary()->set_local_table_data_size(sizeof(Ndb_local_table_statistics)); if (g_ndb->init() != 0) { ERR_PRINT (g_ndb->getNdbError()); @@ -4387,7 +4451,7 @@ bool ndbcluster_init() DBUG_PRINT("info",("NDBCLUSTER storage engine at %s on port %d", g_ndb_cluster_connection->get_connected_host(), g_ndb_cluster_connection->get_connected_port())); - g_ndb_cluster_connection->wait_until_ready(10,0); + g_ndb_cluster_connection->wait_until_ready(10,3); } else if(res == 1) { diff --git a/sql/ha_ndbcluster.h b/sql/ha_ndbcluster.h index 07b305bad3e..9fab34448ce 100644 --- a/sql/ha_ndbcluster.h +++ b/sql/ha_ndbcluster.h @@ -156,7 +156,7 @@ class ha_ndbcluster: public handler int create_unique_index(const char *name, KEY *key_info); int initialize_autoincrement(const void *table); enum ILBP {ILBP_CREATE = 0, ILBP_OPEN = 1}; // Index List Build Phase - int build_index_list(TABLE *tab, enum ILBP phase); + int build_index_list(Ndb *ndb, TABLE *tab, enum ILBP phase); int get_metadata(const char* path); void release_metadata(); NDB_INDEX_TYPE get_index_type(uint idx_no) const; @@ -201,6 +201,7 @@ class ha_ndbcluster: public handler void print_results(); longlong get_auto_increment(); + void invalidateDictionaryCache(); int ndb_err(NdbConnection*); bool uses_blob_value(bool all_fields); @@ -212,6 +213,7 @@ class ha_ndbcluster: public handler NdbConnection *m_active_trans; NdbResultSet *m_active_cursor; void *m_table; + int m_tableVersion; void *m_table_info; char m_dbname[FN_HEADLEN]; //char m_schemaname[FN_HEADLEN]; diff --git a/sql/handler.cc b/sql/handler.cc index 70ba236a5d5..b12032ccd81 100644 --- a/sql/handler.cc +++ b/sql/handler.cc @@ -32,6 +32,9 @@ #ifdef HAVE_BERKELEY_DB #include "ha_berkeley.h" #endif +#ifdef HAVE_BLACKHOLE_DB +#include "ha_blackhole.h" +#endif #ifdef HAVE_EXAMPLE_DB #include "examples/ha_example.h" #endif @@ -96,6 +99,8 @@ struct show_table_type_st sys_table_types[]= "Archive storage engine", DB_TYPE_ARCHIVE_DB}, {"CSV",&have_csv_db, "CSV storage engine", DB_TYPE_CSV_DB}, + {"BLACKHOLE",&have_blackhole_db, + "Storage engine designed to act as null storage", DB_TYPE_BLACKHOLE_DB}, {NullS, NULL, NullS, DB_TYPE_UNKNOWN} }; @@ -204,6 +209,10 @@ handler *get_new_handler(TABLE *table, enum db_type db_type) case DB_TYPE_ARCHIVE_DB: return new ha_archive(table); #endif +#ifdef HAVE_BLACKHOLE_DB + case DB_TYPE_BLACKHOLE_DB: + return new ha_blackhole(table); +#endif #ifdef HAVE_CSV_DB case DB_TYPE_CSV_DB: return new ha_tina(table); diff --git a/sql/handler.h b/sql/handler.h index 0426312f404..50ce33b5067 100644 --- a/sql/handler.h +++ b/sql/handler.h @@ -150,7 +150,8 @@ enum db_type DB_TYPE_BERKELEY_DB, DB_TYPE_INNODB, DB_TYPE_GEMINI, DB_TYPE_NDBCLUSTER, DB_TYPE_EXAMPLE_DB, DB_TYPE_ARCHIVE_DB, DB_TYPE_CSV_DB, - + DB_TYPE_FEDERATED_DB, + DB_TYPE_BLACKHOLE_DB, DB_TYPE_DEFAULT // Must be last }; diff --git a/sql/item.cc b/sql/item.cc index 17d41fda677..3b87e5ee2cf 100644 --- a/sql/item.cc +++ b/sql/item.cc @@ -236,6 +236,18 @@ Item *Item_string::safe_charset_converter(CHARSET_INFO *tocs) return NULL; } conv->str_value.copy(); + /* + The above line executes str_value.realloc() internally, + which alligns Alloced_length using ALLIGN_SIZE. + In the case of Item_string::str_value we don't want + Alloced_length to be longer than str_length. + Otherwise, some functions like Item_func_concat::val_str() + try to reuse str_value as a buffer for concatenation result + for optimization purposes, so our string constant become + corrupted. See bug#8785 for more details. + Let's shrink Alloced_length to str_length to avoid this problem. + */ + conv->str_value.shrink_to_length(); return conv; } @@ -384,7 +396,6 @@ void Item::split_sum_func2(THD *thd, Item **ref_pointer_array, */ bool DTCollation::aggregate(DTCollation &dt, uint flags) { - nagg++; if (!my_charset_same(collation, dt.collation)) { /* @@ -400,7 +411,6 @@ bool DTCollation::aggregate(DTCollation &dt, uint flags) else { set(dt); - strong= nagg; } } else if (dt.collation == &my_charset_bin) @@ -408,7 +418,6 @@ bool DTCollation::aggregate(DTCollation &dt, uint flags) if (dt.derivation <= derivation) { set(dt); - strong= nagg; } else ; // Do nothing @@ -424,20 +433,18 @@ bool DTCollation::aggregate(DTCollation &dt, uint flags) dt.collation->state & MY_CS_UNICODE) { set(dt); - strong= nagg; } else if ((flags & MY_COLL_ALLOW_COERCIBLE_CONV) && derivation < dt.derivation && - dt.derivation >= DERIVATION_COERCIBLE) + dt.derivation >= DERIVATION_SYSCONST) { // Do nothing; } else if ((flags & MY_COLL_ALLOW_COERCIBLE_CONV) && dt.derivation < derivation && - derivation >= DERIVATION_COERCIBLE) + derivation >= DERIVATION_SYSCONST) { set(dt); - strong= nagg; } else { @@ -453,7 +460,6 @@ bool DTCollation::aggregate(DTCollation &dt, uint flags) else if (dt.derivation < derivation) { set(dt); - strong= nagg; } else { @@ -1140,8 +1146,9 @@ double Item_param::val() case LONG_DATA_VALUE: { int dummy_err; + char *end_not_used; return my_strntod(str_value.charset(), (char*) str_value.ptr(), - str_value.length(), (char**) 0, &dummy_err); + str_value.length(), &end_not_used, &dummy_err); } case TIME_VALUE: /* @@ -1312,6 +1319,34 @@ bool Item_param::convert_str_value(THD *thd) return rc; } +bool Item_param::fix_fields(THD *thd, TABLE_LIST *tables, Item **ref) +{ + DBUG_ASSERT(fixed == 0); + SELECT_LEX *cursel= (SELECT_LEX *) thd->lex->current_select; + + /* + Parameters in a subselect should mark the subselect as not constant + during prepare + */ + if (state == NO_VALUE) + { + /* + SELECT_LEX_UNIT::item set only for subqueries, so test of it presence + can be barrier to stop before derived table SELECT or very outer SELECT + */ + for(; + cursel->master_unit()->item; + cursel= cursel->outer_select()) + { + Item_subselect *subselect_item= cursel->master_unit()->item; + subselect_item->used_tables_cache|= OUTER_REF_TABLE_BIT; + subselect_item->const_item_cache= 0; + } + } + fixed= 1; + return 0; +} + /* End of Item_param related */ @@ -1693,10 +1728,10 @@ Field *Item::tmp_table_field_from_field_type(TABLE *table) case MYSQL_TYPE_NULL: return new Field_null((char*) 0, max_length, Field::NONE, name, table, &my_charset_bin); - case MYSQL_TYPE_NEWDATE: case MYSQL_TYPE_INT24: return new Field_medium((char*) 0, max_length, null_ptr, 0, Field::NONE, name, table, 0, unsigned_flag); + case MYSQL_TYPE_NEWDATE: case MYSQL_TYPE_DATE: return new Field_date(maybe_null, name, table, &my_charset_bin); case MYSQL_TYPE_TIME: @@ -2286,6 +2321,7 @@ void Item_ref::set_properties() decimals= (*ref)->decimals; collation.set((*ref)->collation); with_sum_func= (*ref)->with_sum_func; + unsigned_flag= (*ref)->unsigned_flag; fixed= 1; } @@ -2585,10 +2621,12 @@ double Item_cache_str::val() DBUG_ASSERT(fixed == 1); int err; if (value) + { + char *end_not_used; return my_strntod(value->charset(), (char*) value->ptr(), - value->length(), (char**) 0, &err); - else - return (double)0; + value->length(), &end_not_used, &err); + } + return (double)0; } @@ -2691,204 +2729,274 @@ void Item_cache_row::bring_value() } -/* - Returns field for temporary table dependind on item type - - SYNOPSIS - get_holder_example_field() - thd - thread handler - item - pointer to item - table - empty table object - - NOTE - It is possible to return field for Item_func - items only if field type of this item is - date or time or datetime type. - also see function field_types_to_be_kept() from - field.cc - - RETURN - # - field - 0 - no field -*/ - -Field *get_holder_example_field(THD *thd, Item *item, TABLE *table) -{ - DBUG_ASSERT(table); - - Item_func *tmp_item= 0; - if (item->type() == Item::FIELD_ITEM) - return (((Item_field*) item)->field); - if (item->type() == Item::FUNC_ITEM) - tmp_item= (Item_func *) item; - else if (item->type() == Item::SUM_FUNC_ITEM) - { - Item_sum *item_sum= (Item_sum *) item; - if (item_sum->keep_field_type()) - { - if (item_sum->args[0]->type() == Item::FIELD_ITEM) - return (((Item_field*) item_sum->args[0])->field); - if (item_sum->args[0]->type() == Item::FUNC_ITEM) - tmp_item= (Item_func *) item_sum->args[0]; - } - } - return (tmp_item && field_types_to_be_kept(tmp_item->field_type()) ? - tmp_item->tmp_table_field(table) : 0); -} - - -Item_type_holder::Item_type_holder(THD *thd, Item *item, TABLE *table) - :Item(thd, item), item_type(item->result_type()), - orig_type(item_type) +Item_type_holder::Item_type_holder(THD *thd, Item *item) + :Item(thd, item), enum_set_typelib(0), fld_type(get_real_type(item)) { DBUG_ASSERT(item->fixed); - /* - It is safe assign pointer on field, because it will be used just after - all JOIN::prepare calls and before any SELECT execution - */ - field_example= get_holder_example_field(thd, item, table); - max_length= real_length(item); + max_length= display_length(item); maybe_null= item->maybe_null; collation.set(item->collation); + get_full_info(item); } /* - STRING_RESULT, REAL_RESULT, INT_RESULT, ROW_RESULT + Return expression type of Item_type_holder + + SYNOPSIS + Item_type_holder::result_type() - ROW_RESULT should never appear in Item_type_holder::join_types, - but it is included in following table just to make table full - (there DBUG_ASSERT in function to catch ROW_RESULT) + RETURN + Item_result (type of internal MySQL expression result) */ -static Item_result type_convertor[4][4]= -{{STRING_RESULT, STRING_RESULT, STRING_RESULT, ROW_RESULT}, - {STRING_RESULT, REAL_RESULT, REAL_RESULT, ROW_RESULT}, - {STRING_RESULT, REAL_RESULT, INT_RESULT, ROW_RESULT}, - {ROW_RESULT, ROW_RESULT, ROW_RESULT, ROW_RESULT}}; + +Item_result Item_type_holder::result_type() const +{ + return Field::result_merge_type(fld_type); +} /* - Values of 'from' field can be stored in 'to' field. + Find real field type of item SYNOPSIS - is_attr_compatible() - from Item which values should be saved - to Item where values should be saved + Item_type_holder::get_real_type() RETURN - 1 can be saved - 0 can not be saved + type of field which should be created to store item value */ -inline bool is_attr_compatible(Item *from, Item *to) +enum_field_types Item_type_holder::get_real_type(Item *item) { - return ((to->max_length >= from->max_length) && - (to->maybe_null || !from->maybe_null) && - (to->result_type() != STRING_RESULT || - from->result_type() != STRING_RESULT || - (from->collation.collation == to->collation.collation))); -} - - -bool Item_type_holder::join_types(THD *thd, Item *item, TABLE *table) -{ - uint32 new_length= real_length(item); - bool use_new_field= 0, use_expression_type= 0; - Item_result new_result_type= type_convertor[item_type][item->result_type()]; - Field *field= get_holder_example_field(thd, item, table); - bool item_is_a_field= field; - /* - Check if both items point to fields: in this case we - can adjust column types of result table in the union smartly. - */ - if (field_example && item_is_a_field) + switch(item->type()) { - /* Can 'field_example' field store data of the column? */ - if ((use_new_field= - (!field->field_cast_compatible(field_example->field_cast_type()) || - !is_attr_compatible(item, this)))) - { - /* - The old field can't store value of the new field. - Check if the new field can store value of the old one. - */ - use_expression_type|= - (!field_example->field_cast_compatible(field->field_cast_type()) || - !is_attr_compatible(this, item)); - } - } - else if (field_example || item_is_a_field) + case FIELD_ITEM: { /* - Expression types can't be mixed with field types, we have to use - expression types. + Item_fields::field_type ask Field_type() but sometimes field return + a different type, like for enum/set, so we need to ask real type. */ - use_new_field= 1; // make next if test easier - use_expression_type= 1; + Field *field= ((Item_field *) item)->field; + enum_field_types type= field->real_type(); + /* work around about varchar type field detection */ + if (type == MYSQL_TYPE_STRING && field->type() == MYSQL_TYPE_VAR_STRING) + return MYSQL_TYPE_VAR_STRING; + return type; } - - /* Check whether size/type of the result item should be changed */ - if (use_new_field || - (new_result_type != item_type) || (new_length > max_length) || - (!maybe_null && item->maybe_null) || - (item_type == STRING_RESULT && - collation.collation != item->collation.collation)) + case SUM_FUNC_ITEM: { - const char *old_cs,*old_derivation; - if (use_expression_type || !item_is_a_field) - field_example= 0; - else + /* + Argument of aggregate function sometimes should be asked about field + type + */ + Item_sum *item_sum= (Item_sum *) item; + if (item_sum->keep_field_type()) + return get_real_type(item_sum->args[0]); + break; + } + case FUNC_ITEM: + if (((Item_func *) item)->functype() == Item_func::VAR_VALUE_FUNC) { /* - It is safe to assign a pointer to field here, because it will be used - before any table is closed. + There are work around of problem with changing variable type on the + fly and variable always report "string" as field type to get + acceptable information for client in send_field, so we make field + type from expression type. */ - field_example= field; + switch (item->result_type()) + { + case STRING_RESULT: + return MYSQL_TYPE_VAR_STRING; + case INT_RESULT: + return MYSQL_TYPE_LONGLONG; + case REAL_RESULT: + return MYSQL_TYPE_DOUBLE; + case ROW_RESULT: + default: + DBUG_ASSERT(0); + return MYSQL_TYPE_VAR_STRING; + } } + break; + default: + break; + } + return item->field_type(); +} + +/* + Find field type which can carry current Item_type_holder type and + type of given Item. + + SYNOPSIS + Item_type_holder::join_types() + thd thread handler + item given item to join its parameters with this item ones + RETURN + TRUE error - types are incompatible + FALSE OK +*/ + +bool Item_type_holder::join_types(THD *thd, Item *item) +{ + max_length= max(max_length, display_length(item)); + fld_type= Field::field_type_merge(fld_type, get_real_type(item)); + if (Field::result_merge_type(fld_type) == STRING_RESULT) + { + const char *old_cs, *old_derivation; old_cs= collation.collation->name; old_derivation= collation.derivation_name(); - if (item_type == STRING_RESULT && collation.aggregate(item->collation)) + if (collation.aggregate(item->collation)) { my_error(ER_CANT_AGGREGATE_2COLLATIONS, MYF(0), old_cs, old_derivation, item->collation.collation->name, item->collation.derivation_name(), "UNION"); - return 1; + return TRUE; } - - max_length= max(max_length, new_length); - decimals= max(decimals, item->decimals); - maybe_null|= item->maybe_null; - item_type= new_result_type; } - DBUG_ASSERT(item_type != ROW_RESULT); - return 0; + decimals= max(decimals, item->decimals); + maybe_null|= item->maybe_null; + get_full_info(item); + return FALSE; } +/* + Calculate lenth for merging result for given Item type + + SYNOPSIS + Item_type_holder::real_length() + item Item for lrngth detection -uint32 Item_type_holder::real_length(Item *item) + RETURN + length +*/ + +uint32 Item_type_holder::display_length(Item *item) { if (item->type() == Item::FIELD_ITEM) return ((Item_field *)item)->max_disp_length(); - switch (item->result_type()) + switch (item->field_type()) { - case STRING_RESULT: + case MYSQL_TYPE_DECIMAL: + case MYSQL_TYPE_TIMESTAMP: + case MYSQL_TYPE_DATE: + case MYSQL_TYPE_TIME: + case MYSQL_TYPE_DATETIME: + case MYSQL_TYPE_YEAR: + case MYSQL_TYPE_NEWDATE: + case MYSQL_TYPE_ENUM: + case MYSQL_TYPE_SET: + case MYSQL_TYPE_TINY_BLOB: + case MYSQL_TYPE_MEDIUM_BLOB: + case MYSQL_TYPE_LONG_BLOB: + case MYSQL_TYPE_BLOB: + case MYSQL_TYPE_VAR_STRING: + case MYSQL_TYPE_STRING: + case MYSQL_TYPE_GEOMETRY: return item->max_length; - case REAL_RESULT: + case MYSQL_TYPE_TINY: + return 4; + case MYSQL_TYPE_SHORT: + return 6; + case MYSQL_TYPE_LONG: + return 11; + case MYSQL_TYPE_FLOAT: + return 25; + case MYSQL_TYPE_DOUBLE: return 53; - case INT_RESULT: + case MYSQL_TYPE_NULL: + return 4; + case MYSQL_TYPE_LONGLONG: return 20; - case ROW_RESULT: + case MYSQL_TYPE_INT24: + return 8; default: DBUG_ASSERT(0); // we should never go there return 0; } } + +/* + Make temporary table field according collected information about type + of UNION result + + SYNOPSIS + Item_type_holder::make_field_by_type() + table temporary table for which we create fields + + RETURN + created field +*/ + +Field *Item_type_holder::make_field_by_type(TABLE *table) +{ + /* + The field functions defines a field to be not null if null_ptr is not 0 + */ + uchar *null_ptr= maybe_null ? (uchar*) "" : 0; + switch (fld_type) + { + case MYSQL_TYPE_ENUM: + DBUG_ASSERT(enum_set_typelib); + return new Field_enum((char *) 0, max_length, null_ptr, 0, + Field::NONE, name, + table, get_enum_pack_length(enum_set_typelib->count), + enum_set_typelib, collation.collation); + case MYSQL_TYPE_SET: + DBUG_ASSERT(enum_set_typelib); + return new Field_set((char *) 0, max_length, null_ptr, 0, + Field::NONE, name, + table, get_set_pack_length(enum_set_typelib->count), + enum_set_typelib, collation.collation); + case MYSQL_TYPE_VAR_STRING: + table->db_create_options|= HA_OPTION_PACK_RECORD; + return new Field_string(max_length, maybe_null, name, table, + collation.collation); + default: + break; + } + return tmp_table_field_from_field_type(table); +} + + +/* + Get full information from Item about enum/set fields to be able to create + them later + + SYNOPSIS + Item_type_holder::get_full_info + item Item for information collection +*/ +void Item_type_holder::get_full_info(Item *item) +{ + if (fld_type == MYSQL_TYPE_ENUM || + fld_type == MYSQL_TYPE_SET) + { + /* + We can have enum/set type after merging only if we have one enum/set + field and number of NULL fields + */ + DBUG_ASSERT((enum_set_typelib && + get_real_type(item) == MYSQL_TYPE_NULL) || + (!enum_set_typelib && + item->type() == Item::FIELD_ITEM && + (get_real_type(item) == MYSQL_TYPE_ENUM || + get_real_type(item) == MYSQL_TYPE_SET) && + ((Field_enum*)((Item_field *) item)->field)->typelib)); + if (!enum_set_typelib) + { + enum_set_typelib= ((Field_enum*)((Item_field *) item)->field)->typelib; + } + } +} + + double Item_type_holder::val() { DBUG_ASSERT(0); // should never be called diff --git a/sql/item.h b/sql/item.h index a8b892292d3..d949095b455 100644 --- a/sql/item.h +++ b/sql/item.h @@ -31,8 +31,9 @@ void item_init(void); /* Init item functions */ enum Derivation { - DERIVATION_IGNORABLE= 4, - DERIVATION_COERCIBLE= 3, + DERIVATION_IGNORABLE= 5, + DERIVATION_COERCIBLE= 4, + DERIVATION_SYSCONST= 3, DERIVATION_IMPLICIT= 2, DERIVATION_NONE= 1, DERIVATION_EXPLICIT= 0 @@ -61,22 +62,16 @@ class DTCollation { public: CHARSET_INFO *collation; enum Derivation derivation; - uint nagg; // Total number of aggregated collations. - uint strong; // Number of the strongest collation. DTCollation() { collation= &my_charset_bin; derivation= DERIVATION_NONE; - nagg= 0; - strong= 0; } DTCollation(CHARSET_INFO *collation_arg, Derivation derivation_arg) { collation= collation_arg; derivation= derivation_arg; - nagg= 0; - strong= 0; } void set(DTCollation &dt) { @@ -102,6 +97,7 @@ public: case DERIVATION_IGNORABLE: return "IGNORABLE"; case DERIVATION_COERCIBLE: return "COERCIBLE"; case DERIVATION_IMPLICIT: return "IMPLICIT"; + case DERIVATION_SYSCONST: return "SYSCONST"; case DERIVATION_EXPLICIT: return "EXPLICIT"; case DERIVATION_NONE: return "NONE"; default: return "UNKNOWN"; @@ -119,7 +115,7 @@ public: static void *operator new(size_t size, MEM_ROOT *mem_root) { return (void*) alloc_root(mem_root, (uint) size); } static void operator delete(void *ptr,size_t size) {} - static void operator delete(void *ptr,size_t size, MEM_ROOT *mem_root) {} + static void operator delete(void *ptr, MEM_ROOT *mem_root) {} enum Type {FIELD_ITEM, FUNC_ITEM, SUM_FUNC_ITEM, STRING_ITEM, INT_ITEM, REAL_ITEM, NULL_ITEM, VARBIN_ITEM, @@ -474,6 +470,17 @@ public: Item *safe_charset_converter(CHARSET_INFO *tocs); }; +class Item_null_result :public Item_null +{ +public: + Field *result_field; + Item_null_result() : Item_null(), result_field(0) {} + bool is_result_field() { return result_field != 0; } + void save_in_result_field(bool no_conversions) + { + save_in_field(result_field, no_conversions); + } +}; /* Item represents one placeholder ('?') of prepared statement */ @@ -552,6 +559,7 @@ public: bool get_time(TIME *tm); bool get_date(TIME *tm, uint fuzzydate); int save_in_field(Field *field, bool no_conversions); + bool fix_fields(THD *, struct st_table_list *, Item **); void set_null(); void set_int(longlong i, uint32 max_length_arg); @@ -719,8 +727,9 @@ public: { DBUG_ASSERT(fixed == 1); int err; + char *end_not_used; return my_strntod(str_value.charset(), (char*) str_value.ptr(), - str_value.length(), (char**) 0, &err); + str_value.length(), &end_not_used, &err); } longlong val_int() { @@ -742,7 +751,7 @@ public: Item *new_item() { return new Item_string(name, str_value.ptr(), - str_value.length(), &my_charset_bin); + str_value.length(), collation.collation); } Item *safe_charset_converter(CHARSET_INFO *tocs); String *const_string() { return &str_value; } @@ -905,6 +914,7 @@ public: void save_org_in_field(Field *field) { (*ref)->save_org_in_field(field); } enum Item_result result_type () const { return (*ref)->result_type(); } enum_field_types field_type() const { return (*ref)->field_type(); } + Field *get_tmp_table_field() { return result_field; } table_map used_tables() const { return depended_from ? OUTER_REF_TABLE_BIT : (*ref)->used_tables(); @@ -996,6 +1006,8 @@ public: The following class is used to optimize comparing of date and bigint columns We need to save the original item, to be able to set the field to the original value in 'opt_range'. + An instance of Item_int_with_ref may refer to a signed or an unsigned + integer. */ class Item_int_with_ref :public Item_int @@ -1010,6 +1022,11 @@ public: { return ref->save_in_field(field, no_conversions); } + Item *new_item() + { + return (ref->unsigned_flag)? new Item_uint(ref->name, ref->max_length) : + new Item_int(ref->name, ref->max_length); + } }; @@ -1044,9 +1061,10 @@ public: double val() { int err; + char *end_not_used; return (null_value ? 0.0 : my_strntod(str_value.charset(), (char*) str_value.ptr(), - str_value.length(),NULL,&err)); + str_value.length(), &end_not_used, &err)); } longlong val_int() { @@ -1315,32 +1333,32 @@ public: /* - Used to store type. name, length of Item for UNIONS & derived table + Item_type_holder used to store type. name, length of Item for UNIONS & + derived tables. + + Item_type_holder do not need cleanup() because its time of live limited by + single SP/PS execution. */ class Item_type_holder: public Item { protected: - Item_result item_type; - Item_result orig_type; - Field *field_example; + TYPELIB *enum_set_typelib; + enum_field_types fld_type; + + void get_full_info(Item *item); public: - Item_type_holder(THD*, Item*, TABLE *); + Item_type_holder(THD*, Item*); - Item_result result_type () const { return item_type; } + Item_result result_type() const; + virtual enum_field_types field_type() const { return fld_type; }; enum Type type() const { return TYPE_HOLDER; } double val(); longlong val_int(); String *val_str(String*); - bool join_types(THD *thd, Item *, TABLE *); - Field *example() { return field_example; } - static uint32 real_length(Item *item); - void cleanup() - { - DBUG_ENTER("Item_type_holder::cleanup"); - Item::cleanup(); - item_type= orig_type; - DBUG_VOID_RETURN; - } + bool join_types(THD *thd, Item *); + Field *make_field_by_type(TABLE *table); + static uint32 display_length(Item *item); + static enum_field_types get_real_type(Item *); }; diff --git a/sql/item_cmpfunc.cc b/sql/item_cmpfunc.cc index aff11ac7b25..8498ab4800e 100644 --- a/sql/item_cmpfunc.cc +++ b/sql/item_cmpfunc.cc @@ -265,7 +265,7 @@ int Arg_comparator::set_compare_func(Item_bool_func2 *item, Item_result type) comparators= 0; return 1; } - if (!(comparators= (Arg_comparator *) sql_alloc(sizeof(Arg_comparator)*n))) + if (!(comparators= new Arg_comparator[n])) return 1; for (uint i=0; i < n; i++) { @@ -393,10 +393,16 @@ int Arg_comparator::compare_e_binary_string() int Arg_comparator::compare_real() { - double val1= (*a)->val(); + /* + Fix yet another manifestation of Bug#2338. 'Volatile' will instruct + gcc to flush double values out of 80-bit Intel FPU registers before + performing the comparison. + */ + volatile double val1, val2; + val1= (*a)->val(); if (!(*a)->null_value) { - double val2= (*b)->val(); + val2= (*b)->val(); if (!(*b)->null_value) { owner->null_value= 0; @@ -1528,6 +1534,12 @@ in_row::in_row(uint elements, Item * item) size= sizeof(cmp_item_row); compare= (qsort2_cmp) cmp_row; tmp.store_value(item); + /* + We need to reset these as otherwise we will call sort() with + uninitialized (even if not used) elements + */ + used_count= elements; + collation= 0; } in_row::~in_row() diff --git a/sql/item_cmpfunc.h b/sql/item_cmpfunc.h index 061ed468b78..37b0674a094 100644 --- a/sql/item_cmpfunc.h +++ b/sql/item_cmpfunc.h @@ -284,6 +284,7 @@ public: Item_func_equal(Item *a,Item *b) :Item_bool_rowready_func2(a,b) {}; longlong val_int(); void fix_length_and_dec(); + table_map not_null_tables() const { return 0; } enum Functype functype() const { return EQUAL_FUNC; } enum Functype rev_functype() const { return EQUAL_FUNC; } cond_result eq_cmp_result() const { return COND_TRUE; } @@ -965,6 +966,8 @@ public: enum Functype functype() const { return COND_AND_FUNC; } longlong val_int(); const char *func_name() const { return "and"; } + table_map not_null_tables() const + { return abort_on_null ? not_null_tables_cache: and_tables_cache; } Item* copy_andor_structure(THD *thd) { Item_cond_and *item; @@ -1012,6 +1015,7 @@ public: enum Type type() const { return FUNC_ITEM; } longlong val_int(); const char *func_name() const { return "xor"; } + void top_level_item() {} }; diff --git a/sql/item_create.cc b/sql/item_create.cc index d959a6f393a..c0361c928be 100644 --- a/sql/item_create.cc +++ b/sql/item_create.cc @@ -441,7 +441,7 @@ Item *create_func_version(void) { return new Item_string(NullS,server_version, (uint) strlen(server_version), - system_charset_info, DERIVATION_IMPLICIT); + system_charset_info, DERIVATION_SYSCONST); } Item *create_func_weekday(Item* a) diff --git a/sql/item_func.cc b/sql/item_func.cc index 34c8732a9f2..eb6e395c266 100644 --- a/sql/item_func.cc +++ b/sql/item_func.cc @@ -79,8 +79,6 @@ bool Item_func::agg_arg_collations(DTCollation &c, Item **av, uint count, uint flags) { uint i; - c.nagg= 0; - c.strong= 0; c.set(av[0]->collation); for (i= 1; i < count; i++) { @@ -1348,11 +1346,6 @@ longlong Item_func_char_length::val_int() longlong Item_func_coercibility::val_int() { DBUG_ASSERT(fixed == 1); - if (args[0]->null_value) - { - null_value= 1; - return 0; - } null_value= 0; return (longlong) args[0]->collation.derivation; } @@ -2361,7 +2354,7 @@ static user_var_entry *get_variable(HASH *hash, LEX_STRING &name, entry->value=0; entry->length=0; entry->update_query_id=0; - entry->collation.set(NULL, DERIVATION_NONE); + entry->collation.set(NULL, DERIVATION_IMPLICIT); /* If we are here, we were called from a SET or a query which sets a variable. Imagine it is this: @@ -2419,8 +2412,8 @@ bool Item_func_set_user_var::fix_fields(THD *thd, TABLE_LIST *tables, and the variable has previously been initialized. */ if (!entry->collation.collation || !args[0]->null_value) - entry->collation.set(args[0]->collation); - collation.set(entry->collation); + entry->collation.set(args[0]->collation.collation, DERIVATION_IMPLICIT); + collation.set(entry->collation.collation, DERIVATION_IMPLICIT); cached_result_type= args[0]->result_type(); return 0; } @@ -2432,7 +2425,7 @@ Item_func_set_user_var::fix_length_and_dec() maybe_null=args[0]->maybe_null; max_length=args[0]->max_length; decimals=args[0]->decimals; - collation.set(args[0]->collation); + collation.set(args[0]->collation.collation, DERIVATION_IMPLICIT); } @@ -2641,25 +2634,25 @@ Item_func_set_user_var::update() case REAL_RESULT: { res= update_hash((void*) &save_result.vreal,sizeof(save_result.vreal), - REAL_RESULT, &my_charset_bin, DERIVATION_NONE); + REAL_RESULT, &my_charset_bin, DERIVATION_IMPLICIT); break; } case INT_RESULT: { res= update_hash((void*) &save_result.vint, sizeof(save_result.vint), - INT_RESULT, &my_charset_bin, DERIVATION_NONE); + INT_RESULT, &my_charset_bin, DERIVATION_IMPLICIT); break; } case STRING_RESULT: { if (!save_result.vstr) // Null value res= update_hash((void*) 0, 0, STRING_RESULT, &my_charset_bin, - DERIVATION_NONE); + DERIVATION_IMPLICIT); else res= update_hash((void*) save_result.vstr->ptr(), save_result.vstr->length(), STRING_RESULT, save_result.vstr->charset(), - args[0]->collation.derivation); + DERIVATION_IMPLICIT); break; } case ROW_RESULT: @@ -2877,7 +2870,10 @@ void Item_func_get_user_var::fix_length_and_dec() } } else + { + collation.set(&my_charset_bin, DERIVATION_IMPLICIT); null_value= 1; + } if (error) thd->fatal_error(); diff --git a/sql/item_func.h b/sql/item_func.h index 8a5347d675e..3a309f4ae99 100644 --- a/sql/item_func.h +++ b/sql/item_func.h @@ -51,7 +51,7 @@ public: SP_CONTAINS_FUNC,SP_OVERLAPS_FUNC, SP_STARTPOINT,SP_ENDPOINT,SP_EXTERIORRING, SP_POINTN,SP_GEOMETRYN,SP_INTERIORRINGN, - NOT_FUNC, NOT_ALL_FUNC, NOW_FUNC}; + NOT_FUNC, NOT_ALL_FUNC, NOW_FUNC, VAR_VALUE_FUNC}; enum optimize_type { OPTIMIZE_NONE,OPTIMIZE_KEY,OPTIMIZE_OP, OPTIMIZE_NULL }; enum Type type() const { return FUNC_ITEM; } virtual enum Functype functype() const { return UNKNOWN_FUNC; } @@ -616,7 +616,8 @@ public: Item_func_coercibility(Item *a) :Item_int_func(a) {} longlong val_int(); const char *func_name() const { return "coercibility"; } - void fix_length_and_dec() { max_length=10; } + void fix_length_and_dec() { max_length=10; maybe_null= 0; } + table_map not_null_tables() const { return 0; } }; class Item_func_locate :public Item_int_func @@ -828,8 +829,11 @@ public: double val() { int err; - String *res; res=val_str(&str_value); - return res ? my_strntod(res->charset(),(char*) res->ptr(),res->length(),0,&err) : 0.0; + String *res; + char *end_not_used; + res=val_str(&str_value); + return res ? my_strntod(res->charset(), (char*) res->ptr(), res->length(), + &end_not_used, &err) : 0.0; } longlong val_int() { @@ -977,6 +981,7 @@ public: select @t1:=1,@t1,@t:="hello",@t from foo where (@t1:= t2.b) */ enum_field_types field_type() const { return MYSQL_TYPE_STRING; } + enum Functype functype() const { return VAR_VALUE_FUNC; } const char *func_name() const { return "get_user_var"; } bool const_item() const; table_map used_tables() const diff --git a/sql/item_strfunc.cc b/sql/item_strfunc.cc index a92c4e94c87..8b9351d95a5 100644 --- a/sql/item_strfunc.cc +++ b/sql/item_strfunc.cc @@ -63,10 +63,11 @@ double Item_str_func::val() DBUG_ASSERT(fixed == 1); int err; char buff[64]; + char *end_not_used; String *res, tmp(buff,sizeof(buff), &my_charset_bin); res= val_str(&tmp); return res ? my_strntod(res->charset(), (char*) res->ptr(),res->length(), - NULL, &err) : 0.0; + &end_not_used, &err) : 0.0; } longlong Item_str_func::val_int() @@ -234,6 +235,7 @@ String *Item_func_aes_decrypt::val_str(String *str) void Item_func_aes_decrypt::fix_length_and_dec() { max_length=args[0]->max_length; + maybe_null= 1; } @@ -275,8 +277,7 @@ String *Item_func_concat::val_str(String *str) current_thd->variables.max_allowed_packet); goto null; } - if (!args[0]->const_item() && - res->alloced_length() >= res->length()+res2->length()) + if (res->alloced_length() >= res->length()+res2->length()) { // Use old buffer res->append(*res2); } @@ -1508,6 +1509,23 @@ String *Item_func_decode::val_str(String *str) } +Item *Item_func_sysconst::safe_charset_converter(CHARSET_INFO *tocs) +{ + Item_string *conv; + uint conv_errors; + String tmp, cstr, *ostr= val_str(&tmp); + cstr.copy(ostr->ptr(), ostr->length(), ostr->charset(), tocs, &conv_errors); + if (conv_errors || !(conv= new Item_string(cstr.ptr(), cstr.length(), + cstr.charset(), + collation.derivation))) + { + return NULL; + } + conv->str_value.copy(); + return conv; +} + + String *Item_func_database::val_str(String *str) { DBUG_ASSERT(fixed == 1); @@ -1645,7 +1663,8 @@ String *Item_func_format::val_str(String *str) { DBUG_ASSERT(fixed == 1); double nr =args[0]->val(); - uint32 diff,length,str_length; + int diff; + uint32 length, str_length; uint dec; if ((null_value=args[0]->null_value)) return 0; /* purecov: inspected */ @@ -1662,17 +1681,20 @@ String *Item_func_format::val_str(String *str) if (str_length >= dec+4) { char *tmp,*pos; - length= str->length()+(diff=(str_length- dec-1)/3); + length= str->length()+(diff= (int)(str_length- dec-1)/3); str= copy_if_not_alloced(&tmp_str,str,length); str->length(length); tmp= (char*) str->ptr()+length - dec-1; for (pos= (char*) str->ptr()+length-1; pos != tmp; pos--) - pos[0]= pos[-(int) diff]; + pos[0]= pos[-diff]; while (diff) { - pos[0]=pos[-(int) diff]; pos--; - pos[0]=pos[-(int) diff]; pos--; - pos[0]=pos[-(int) diff]; pos--; + *pos= *(pos - diff); + pos--; + *pos= *(pos - diff); + pos--; + *pos= *(pos - diff); + pos--; pos[0]=','; pos--; diff--; @@ -1863,6 +1885,7 @@ String *Item_func_char::val_str(String *str) { int32 num=(int32) args[i]->val_int(); if (!args[i]->null_value) + { #ifdef USE_MB if (use_mb(collation.collation)) { @@ -1878,6 +1901,7 @@ b1: str->append((char)(num>>8)); } #endif str->append((char)num); + } } str->set_charset(collation.collation); str->realloc(str->length()); // Add end 0 (for Purify) @@ -2266,12 +2290,11 @@ bool Item_func_set_collation::eq(const Item *item, bool binary_cmp) const String *Item_func_charset::val_str(String *str) { DBUG_ASSERT(fixed == 1); - String *res = args[0]->val_str(str); uint dummy_errors; - if ((null_value=(args[0]->null_value || !res->charset()))) - return 0; - str->copy(res->charset()->csname,strlen(res->charset()->csname), + CHARSET_INFO *cs= args[0]->collation.collation; + null_value= 0; + str->copy(cs->csname, strlen(cs->csname), &my_charset_latin1, collation.collation, &dummy_errors); return str; } @@ -2279,12 +2302,11 @@ String *Item_func_charset::val_str(String *str) String *Item_func_collation::val_str(String *str) { DBUG_ASSERT(fixed == 1); - String *res = args[0]->val_str(str); uint dummy_errors; + CHARSET_INFO *cs= args[0]->collation.collation; - if ((null_value=(args[0]->null_value || !res->charset()))) - return 0; - str->copy(res->charset()->name,strlen(res->charset()->name), + null_value= 0; + str->copy(cs->name, strlen(cs->name), &my_charset_latin1, collation.collation, &dummy_errors); return str; } @@ -2448,6 +2470,7 @@ String* Item_func_export_set::val_str(String* str) uint num_set_values = 64; ulonglong mask = 0x1; str->length(0); + str->set_charset(collation.collation); /* Check if some argument is a NULL value */ if (args[0]->null_value || args[1]->null_value || args[2]->null_value) diff --git a/sql/item_strfunc.h b/sql/item_strfunc.h index c1c0969672c..323b52b826c 100644 --- a/sql/item_strfunc.h +++ b/sql/item_strfunc.h @@ -337,10 +337,18 @@ public: }; -class Item_func_database :public Item_str_func +class Item_func_sysconst :public Item_str_func { public: - Item_func_database() { collation.set(system_charset_info,DERIVATION_IMPLICIT); } + Item_func_sysconst() + { collation.set(system_charset_info,DERIVATION_SYSCONST); } + Item *safe_charset_converter(CHARSET_INFO *tocs); +}; + +class Item_func_database :public Item_func_sysconst +{ +public: + Item_func_database() :Item_func_sysconst() {} String *val_str(String *); void fix_length_and_dec() { @@ -350,10 +358,10 @@ public: const char *func_name() const { return "database"; } }; -class Item_func_user :public Item_str_func +class Item_func_user :public Item_func_sysconst { public: - Item_func_user() { collation.set(system_charset_info, DERIVATION_IMPLICIT); } + Item_func_user() :Item_func_sysconst() {} String *val_str(String *); void fix_length_and_dec() { @@ -633,7 +641,9 @@ public: { collation.set(system_charset_info); max_length= 64 * collation.collation->mbmaxlen; // should be enough + maybe_null= 0; }; + table_map not_null_tables() const { return 0; } }; class Item_func_collation :public Item_str_func @@ -646,7 +656,9 @@ public: { collation.set(system_charset_info); max_length= 64 * collation.collation->mbmaxlen; // should be enough + maybe_null= 0; }; + table_map not_null_tables() const { return 0; } }; class Item_func_crc32 :public Item_int_func diff --git a/sql/item_subselect.cc b/sql/item_subselect.cc index 3a1e1918e55..301740c50ef 100644 --- a/sql/item_subselect.cc +++ b/sql/item_subselect.cc @@ -369,25 +369,13 @@ Item_singlerow_subselect::select_transformer(JOIN *join) } substitution= select_lex->item_list.head(); /* - as far as we moved content to upper leven, field which depend of + as far as we moved content to upper level, field which depend of 'upper' select is not really dependent => we remove this dependence */ substitution->walk(&Item::remove_dependence_processor, (byte *) select_lex->outer_select()); - if (join->conds || join->having) - { - Item *cond; - if (!join->having) - cond= join->conds; - else if (!join->conds) - cond= join->having; - else - if (!(cond= new Item_cond_and(join->conds, join->having))) - goto err; - if (!(substitution= new Item_func_if(cond, substitution, - new Item_null()))) - goto err; - } + /* SELECT without FROM clause can't have WHERE or HAVING clause */ + DBUG_ASSERT(join->conds == 0 && join->having == 0); return RES_REDUCE; } return RES_OK; @@ -542,7 +530,7 @@ bool Item_in_subselect::test_limit(SELECT_LEX_UNIT *unit) Item_in_subselect::Item_in_subselect(Item * left_exp, st_select_lex *select_lex): - Item_exists_subselect(), transformed(0), upper_item(0) + Item_exists_subselect(), optimizer(0), transformed(0), upper_item(0) { DBUG_ENTER("Item_in_subselect::Item_in_subselect"); left_expr= left_exp; @@ -616,8 +604,14 @@ String *Item_exists_subselect::val_str(String *str) return str; } + double Item_in_subselect::val() { + /* + As far as Item_in_subselect called only from Item_in_optimizer this + method should not be used + */ + DBUG_ASSERT(0); DBUG_ASSERT(fixed == 1); if (exec()) { @@ -630,6 +624,7 @@ double Item_in_subselect::val() return (double) value; } + longlong Item_in_subselect::val_int() { DBUG_ASSERT(fixed == 1); @@ -644,8 +639,14 @@ longlong Item_in_subselect::val_int() return value; } + String *Item_in_subselect::val_str(String *str) { + /* + As far as Item_in_subselect called only from Item_in_optimizer this + method should not be used + */ + DBUG_ASSERT(0); DBUG_ASSERT(fixed == 1); if (exec()) { @@ -669,18 +670,9 @@ Item_subselect::trans_res Item_in_subselect::single_value_transformer(JOIN *join, Comp_creator *func) { - const char *save_where= thd->where; DBUG_ENTER("Item_in_subselect::single_value_transformer"); - if (changed) - { - DBUG_RETURN(RES_OK); - } - SELECT_LEX *select_lex= join->select_lex; - Item_arena *arena, backup; - arena= thd->change_arena_if_needed(&backup); - thd->where= "scalar IN/ALL/ANY subquery"; /* Check that the right part of the subselect contains no more than one @@ -689,7 +681,7 @@ Item_in_subselect::single_value_transformer(JOIN *join, if (select_lex->item_list.elements > 1) { my_error(ER_OPERAND_COLUMNS, MYF(0), 1); - goto err; + DBUG_RETURN(RES_ERROR); } /* @@ -709,11 +701,12 @@ Item_in_subselect::single_value_transformer(JOIN *join, if (substitution) { // It is second (third, ...) SELECT of UNION => All is done - goto ok; + DBUG_RETURN(RES_OK); } Item *subs; if (!select_lex->group_list.elements && + !select_lex->having && !select_lex->with_sum_func && !(select_lex->next_select())) { @@ -748,7 +741,7 @@ Item_in_subselect::single_value_transformer(JOIN *join, reference */ if (item->fix_fields(thd, join->tables_list, 0)) - goto err; + DBUG_RETURN(RES_ERROR); /* we added aggregate function => we have to change statistic */ count_field_types(&join->tmp_table_param, join->all_fields, 0); @@ -764,25 +757,16 @@ Item_in_subselect::single_value_transformer(JOIN *join, if (upper_item) upper_item->set_sub_test(item); } - // left expression belong to outer select - SELECT_LEX *current= thd->lex->current_select, *up; - thd->lex->current_select= up= current->return_after_parsing(); - if (!left_expr->fixed && - left_expr->fix_fields(thd, up->get_table_list(), &left_expr)) - { - thd->lex->current_select= current; - goto err; - } - thd->lex->current_select= current; + /* fix fields is already called for left expression */ substitution= func->create(left_expr, subs); - goto ok; + DBUG_RETURN(RES_OK); } if (!substitution) { //first call for this unit SELECT_LEX_UNIT *unit= select_lex->master_unit(); - substitution= optimizer= new Item_in_optimizer(left_expr, this); + substitution= optimizer; SELECT_LEX *current= thd->lex->current_select, *up; @@ -791,7 +775,7 @@ Item_in_subselect::single_value_transformer(JOIN *join, if (!optimizer || optimizer->fix_left(thd, up->get_table_list(), 0)) { thd->lex->current_select= current; - goto err; + DBUG_RETURN(RES_ERROR); } thd->lex->current_select= current; @@ -835,7 +819,7 @@ Item_in_subselect::single_value_transformer(JOIN *join, if (join->having->fix_fields(thd, join->tables_list, 0)) { select_lex->having_fix_field= 0; - goto err; + DBUG_RETURN(RES_ERROR); } select_lex->having_fix_field= 0; } @@ -865,7 +849,7 @@ Item_in_subselect::single_value_transformer(JOIN *join, if (join->having->fix_fields(thd, join->tables_list, 0)) { select_lex->having_fix_field= 0; - goto err; + DBUG_RETURN(RES_ERROR); } select_lex->having_fix_field= 0; item= new Item_cond_or(item, @@ -879,7 +863,7 @@ Item_in_subselect::single_value_transformer(JOIN *join, */ select_lex->where= join->conds= and_items(join->conds, item); if (join->conds->fix_fields(thd, join->tables_list, 0)) - goto err; + DBUG_RETURN(RES_ERROR); } else { @@ -901,7 +885,7 @@ Item_in_subselect::single_value_transformer(JOIN *join, 0)) { select_lex->having_fix_field= 0; - goto err; + DBUG_RETURN(RES_ERROR); } select_lex->having_fix_field= 0; } @@ -919,54 +903,34 @@ Item_in_subselect::single_value_transformer(JOIN *join, push_warning(thd, MYSQL_ERROR::WARN_LEVEL_NOTE, ER_SELECT_REDUCED, warn_buff); } - if (arena) - thd->restore_backup_item_arena(arena, &backup); DBUG_RETURN(RES_REDUCE); } } } -ok: - if (arena) - thd->restore_backup_item_arena(arena, &backup); - thd->where= save_where; DBUG_RETURN(RES_OK); - -err: - if (arena) - thd->restore_backup_item_arena(arena, &backup); - DBUG_RETURN(RES_ERROR); } Item_subselect::trans_res Item_in_subselect::row_value_transformer(JOIN *join) { - const char *save_where= thd->where; DBUG_ENTER("Item_in_subselect::row_value_transformer"); - if (changed) - { - DBUG_RETURN(RES_OK); - } - Item_arena *arena, backup; Item *item= 0; SELECT_LEX *select_lex= join->select_lex; - thd->where= "row IN/ALL/ANY subquery"; - arena= thd->change_arena_if_needed(&backup); - if (select_lex->item_list.elements != left_expr->cols()) { my_error(ER_OPERAND_COLUMNS, MYF(0), left_expr->cols()); - goto err; + DBUG_RETURN(RES_ERROR); } if (!substitution) { //first call for this unit SELECT_LEX_UNIT *unit= select_lex->master_unit(); - substitution= optimizer= new Item_in_optimizer(left_expr, this); + substitution= optimizer; SELECT_LEX *current= thd->lex->current_select, *up; thd->lex->current_select= up= current->return_after_parsing(); @@ -974,7 +938,7 @@ Item_in_subselect::row_value_transformer(JOIN *join) if (!optimizer || optimizer->fix_left(thd, up->get_table_list(), 0)) { thd->lex->current_select= current; - goto err; + DBUG_RETURN(RES_ERROR); } // we will refer to apper level cache array => we have to save it in PS @@ -993,7 +957,7 @@ Item_in_subselect::row_value_transformer(JOIN *join) DBUG_ASSERT(left_expr->fixed && select_lex->ref_pointer_array[i]->fixed); if (select_lex->ref_pointer_array[i]-> check_cols(left_expr->el(i)->cols())) - goto err; + DBUG_RETURN(RES_ERROR); Item *func= new Item_ref_null_helper(this, select_lex->ref_pointer_array+i, (char *) "<no matter>", @@ -1021,7 +985,7 @@ Item_in_subselect::row_value_transformer(JOIN *join) if (join->having->fix_fields(thd, join->tables_list, 0)) { select_lex->having_fix_field= 0; - goto err; + DBUG_RETURN(RES_ERROR); } select_lex->having_fix_field= 0; } @@ -1034,27 +998,112 @@ Item_in_subselect::row_value_transformer(JOIN *join) */ select_lex->where= join->conds= and_items(join->conds, item); if (join->conds->fix_fields(thd, join->tables_list, 0)) - goto err; + DBUG_RETURN(RES_ERROR); } - if (arena) - thd->restore_backup_item_arena(arena, &backup); - thd->where= save_where; DBUG_RETURN(RES_OK); - -err: - if (arena) - thd->restore_backup_item_arena(arena, &backup); - DBUG_RETURN(RES_ERROR); } Item_subselect::trans_res Item_in_subselect::select_transformer(JOIN *join) { + return select_in_like_transformer(join, &eq_creator); +} + + +/* + Prepare IN/ALL/ANY/SOME subquery transformation and call appropriate + transformation function + + SYNOPSIS + Item_in_subselect::select_in_like_transformer() + join JOIN object of transforming subquery + func creator of condition function of subquery + + DESCRIPTION + To decide which transformation procedure (scalar or row) applicable here + we have to call fix_fields() for left expression to be able to call + cols() method on it. Also this method make arena management for + underlying transformation methods. + + RETURN + RES_OK OK + RES_REDUCE OK, and current subquery was reduced during transformation + RES_ERROR Error +*/ + +Item_subselect::trans_res +Item_in_subselect::select_in_like_transformer(JOIN *join, Comp_creator *func) +{ + Item_arena *arena, backup; + SELECT_LEX *current= thd->lex->current_select, *up; + const char *save_where= thd->where; + Item_subselect::trans_res res= RES_ERROR; + bool result; + + DBUG_ENTER("Item_in_subselect::select_in_like_transformer"); + + if (changed) + { + DBUG_RETURN(RES_OK); + } + + thd->where= "IN/ALL/ANY subquery"; + + /* + In some optimisation cases we will not need this Item_in_optimizer + object, but we can't know it here, but here we need address correct + reference on left expresion. + */ + if (!optimizer) + { + arena= thd->change_arena_if_needed(&backup); + result= (!(optimizer= new Item_in_optimizer(left_expr, this))); + if (arena) + thd->restore_backup_item_arena(arena, &backup); + if (result) + goto err; + } + + thd->lex->current_select= up= current->return_after_parsing(); + result= (!left_expr->fixed && + left_expr->fix_fields(thd, up->get_table_list(), + optimizer->arguments())); + /* fix_fields can change reference to left_expr, we need reassign it */ + left_expr= optimizer->arguments()[0]; + + thd->lex->current_select= current; + if (result) + goto err; + transformed= 1; + arena= thd->change_arena_if_needed(&backup); + /* + Both transformers call fix_fields() only for Items created inside them, + and all that items do not make permanent changes in current item arena + which allow to us call them with changed arena (if we do not know nature + of Item, we have to call fix_fields() for it only with original arena to + avoid memory leack) + */ if (left_expr->cols() == 1) - return single_value_transformer(join, &eq_creator); - return row_value_transformer(join); + res= single_value_transformer(join, func); + else + { + /* we do not support row operation for ALL/ANY/SOME */ + if (func != &eq_creator) + { + if (arena) + thd->restore_backup_item_arena(arena, &backup); + my_error(ER_OPERAND_COLUMNS, MYF(0), 1); + DBUG_RETURN(RES_ERROR); + } + res= row_value_transformer(join); + } + if (arena) + thd->restore_backup_item_arena(arena, &backup); +err: + thd->where= save_where; + DBUG_RETURN(res); } @@ -1077,7 +1126,7 @@ Item_allany_subselect::select_transformer(JOIN *join) transformed= 1; if (upper_item) upper_item->show= 1; - return single_value_transformer(join, func); + return select_in_like_transformer(join, func); } @@ -1191,7 +1240,7 @@ int subselect_single_select_engine::prepare() int subselect_union_engine::prepare() { - return unit->prepare(thd, result, SELECT_NO_UNLOCK); + return unit->prepare(thd, result, SELECT_NO_UNLOCK, ""); } int subselect_uniquesubquery_engine::prepare() diff --git a/sql/item_subselect.h b/sql/item_subselect.h index ab2d441ed7a..a6e005d5d26 100644 --- a/sql/item_subselect.h +++ b/sql/item_subselect.h @@ -119,6 +119,7 @@ public: friend class Item_in_optimizer; friend bool Item_field::fix_fields(THD *, TABLE_LIST *, Item **); friend bool Item_ref::fix_fields(THD *, TABLE_LIST *, Item **); + friend bool Item_param::fix_fields(THD *, TABLE_LIST *, Item **); }; /* single value subselect */ @@ -220,8 +221,8 @@ public: Item_in_subselect(Item * left_expr, st_select_lex *select_lex); Item_in_subselect() - :Item_exists_subselect(), abort_on_null(0), transformed(0), upper_item(0) - + :Item_exists_subselect(), optimizer(0), abort_on_null(0), transformed(0), + upper_item(0) {} subs_type substype() { return IN_SUBS; } @@ -232,8 +233,8 @@ public: was_null= 0; } trans_res select_transformer(JOIN *join); - trans_res single_value_transformer(JOIN *join, - Comp_creator *func); + trans_res select_in_like_transformer(JOIN *join, Comp_creator *func); + trans_res single_value_transformer(JOIN *join, Comp_creator *func); trans_res row_value_transformer(JOIN * join); longlong val_int(); double val(); diff --git a/sql/item_sum.cc b/sql/item_sum.cc index 029a1fd6c48..6bd2cc00b3e 100644 --- a/sql/item_sum.cc +++ b/sql/item_sum.cc @@ -471,13 +471,14 @@ double Item_sum_hybrid::val() { DBUG_ASSERT(fixed == 1); int err; + char *end_not_used; if (null_value) return 0.0; switch (hybrid_type) { case STRING_RESULT: String *res; res=val_str(&str_value); return (res ? my_strntod(res->charset(), (char*) res->ptr(),res->length(), - (char**) 0, &err) : 0.0); + &end_not_used, &err) : 0.0); case INT_RESULT: if (unsigned_flag) return ulonglong2double(sum_int); diff --git a/sql/item_sum.h b/sql/item_sum.h index d1e82387944..dab136e4716 100644 --- a/sql/item_sum.h +++ b/sql/item_sum.h @@ -600,9 +600,11 @@ public: double val() { int err; - String *res; res=val_str(&str_value); + char *end_not_used; + String *res; + res=val_str(&str_value); return res ? my_strntod(res->charset(),(char*) res->ptr(),res->length(), - (char**) 0, &err) : 0.0; + &end_not_used, &err) : 0.0; } longlong val_int() { diff --git a/sql/key.cc b/sql/key.cc index b1f4c9533a9..7ddd40de2c9 100644 --- a/sql/key.cc +++ b/sql/key.cc @@ -293,7 +293,7 @@ bool check_if_key_used(TABLE *table, uint idx, List<Item> &fields) f.rewind(); while ((field=(Item_field*) f++)) { - if (key_part->field == field->field) + if (key_part->field->eq(field->field)) return 1; } } diff --git a/sql/lock.cc b/sql/lock.cc index 7cfa2aebe7b..8f1cd080db7 100644 --- a/sql/lock.cc +++ b/sql/lock.cc @@ -715,7 +715,7 @@ static void print_lock_error(int error) least the first step above) global_read_lock_blocks_commit count of threads which have the global read lock and block - commits (i.e. have completed the second step above) + commits (i.e. are in or have completed the second step above) waiting_for_read_lock count of threads which want to take a global read lock but cannot protect_against_global_read_lock @@ -886,7 +886,8 @@ void start_waiting_global_read_lock(THD *thd) if (unlikely(thd->global_read_lock)) DBUG_VOID_RETURN; (void) pthread_mutex_lock(&LOCK_open); - tmp= (!--protect_against_global_read_lock && waiting_for_read_lock); + tmp= (!--protect_against_global_read_lock && + (waiting_for_read_lock || global_read_lock_blocks_commit)); (void) pthread_mutex_unlock(&LOCK_open); if (tmp) pthread_cond_broadcast(&COND_refresh); diff --git a/sql/log.cc b/sql/log.cc index 38844877f1b..c8a3b512b6d 100644 --- a/sql/log.cc +++ b/sql/log.cc @@ -1274,7 +1274,7 @@ bool MYSQL_LOG::write(Log_event* event_info) binlog_[wild_]{do|ignore}_table?" (WL#1049)" */ if ((thd && !(thd->options & OPTION_BIN_LOG)) || - (local_db && !db_ok(local_db, binlog_do_db, binlog_ignore_db))) + (!db_ok(local_db, binlog_do_db, binlog_ignore_db))) { VOID(pthread_mutex_unlock(&LOCK_log)); DBUG_PRINT("error",("!db_ok('%s')", local_db)); @@ -1322,6 +1322,7 @@ COLLATION_CONNECTION=%u,COLLATION_DATABASE=%u,COLLATION_SERVER=%u", (uint) thd->variables.collation_server->number); Query_log_event e(thd, buf, written, 0, FALSE); e.set_log_pos(this); + e.error_code = 0; // This statement cannot fail (see [1]). if (e.write(file)) goto err; } @@ -1338,6 +1339,7 @@ COLLATION_CONNECTION=%u,COLLATION_DATABASE=%u,COLLATION_SERVER=%u", "'", NullS); Query_log_event e(thd, buf, buf_end - buf, 0, FALSE); e.set_log_pos(this); + e.error_code = 0; // This statement cannot fail (see [1]). if (e.write(file)) goto err; } @@ -1390,6 +1392,7 @@ COLLATION_CONNECTION=%u,COLLATION_DATABASE=%u,COLLATION_SERVER=%u", thd->variables.convert_set->name); Query_log_event e(thd, buf, (ulong) (p - buf), 0); e.set_log_pos(this); + e.error_code = 0; // This statement cannot fail (see [1]). if (e.write(file)) goto err; } @@ -1407,12 +1410,22 @@ COLLATION_CONNECTION=%u,COLLATION_DATABASE=%u,COLLATION_SERVER=%u", { Query_log_event e(thd, "SET FOREIGN_KEY_CHECKS=0", 24, 0, FALSE); e.set_log_pos(this); + e.error_code = 0; // This statement cannot fail (see [1]). if (e.write(file)) goto err; } } - /* Write the SQL command */ + /* + Write the SQL command + + [1] If this statement has an error code, the slave is required to fail + with the same error code or stop. The preamble and epilogue should + *not* have this error code since the execution of those is + guaranteed *not* to produce any error code. This would therefore + stop the slave even if the execution of the real statement can be + handled gracefully by the slave. + */ event_info->set_log_pos(this); if (event_info->write(file)) @@ -1426,6 +1439,7 @@ COLLATION_CONNECTION=%u,COLLATION_DATABASE=%u,COLLATION_SERVER=%u", { Query_log_event e(thd, "SET FOREIGN_KEY_CHECKS=1", 24, 0, FALSE); e.set_log_pos(this); + e.error_code = 0; // This statement cannot fail (see [1]). if (e.write(file)) goto err; } diff --git a/sql/log_event.cc b/sql/log_event.cc index 19113a3b97e..8a949b81fc1 100644 --- a/sql/log_event.cc +++ b/sql/log_event.cc @@ -1016,7 +1016,6 @@ int Query_log_event::exec_event(struct st_relay_log_info* rli) VOID(pthread_mutex_unlock(&LOCK_thread_count)); thd->variables.pseudo_thread_id= thread_id; // for temp tables - mysql_log.write(thd,COM_QUERY,"%s",thd->query); DBUG_PRINT("query",("%s",thd->query)); if (ignored_error_code((expected_error= error_code)) || !check_expected_error(thd,rli,expected_error)) @@ -1046,6 +1045,10 @@ START SLAVE; . Query: '%s'", expected_error, thd->query); goto end; } + /* If the query was not ignored, it is printed to the general log */ + if (thd->net.last_errno != ER_SLAVE_IGNORED_TABLE) + mysql_log.write(thd,COM_QUERY,"%s",thd->query); + /* If we expected a non-zero error code, and we don't get the same error code, and none of them should be ignored. @@ -2528,7 +2531,12 @@ int User_var_log_event::exec_event(struct st_relay_log_info* rli) 0 can be passed as last argument (reference on item) */ e.fix_fields(thd, 0, 0); - e.update_hash(val, val_len, type, charset, DERIVATION_NONE); + /* + A variable can just be considered as a table with + a single record and with a single column. Thus, like + a column value, it could always have IMPLICIT derivation. + */ + e.update_hash(val, val_len, type, charset, DERIVATION_IMPLICIT); free_root(thd->mem_root,0); rli->inc_event_relay_log_pos(get_event_len()); @@ -2923,8 +2931,10 @@ int Create_file_log_event::exec_event(struct st_relay_log_info* rli) strmov(p, ".info"); // strmov takes less code than memcpy strnmov(proc_info, "Making temp file ", 17); // no end 0 thd->proc_info= proc_info; - if ((fd = my_open(fname_buf, O_WRONLY|O_CREAT|O_BINARY|O_TRUNC, - MYF(MY_WME))) < 0 || + my_delete(fname_buf, MYF(0)); // old copy may exist already + if ((fd= my_create(fname_buf, CREATE_MODE, + O_WRONLY | O_BINARY | O_EXCL | O_NOFOLLOW, + MYF(MY_WME))) < 0 || init_io_cache(&file, fd, IO_SIZE, WRITE_CACHE, (my_off_t)0, 0, MYF(MY_WME|MY_NABP))) { @@ -2948,8 +2958,10 @@ int Create_file_log_event::exec_event(struct st_relay_log_info* rli) my_close(fd, MYF(0)); // fname_buf now already has .data, not .info, because we did our trick - if ((fd = my_open(fname_buf, O_WRONLY|O_CREAT|O_BINARY|O_TRUNC, - MYF(MY_WME))) < 0) + my_delete(fname_buf, MYF(0)); // old copy may exist already + if ((fd= my_create(fname_buf, CREATE_MODE, + O_WRONLY | O_BINARY | O_EXCL | O_NOFOLLOW, + MYF(MY_WME))) < 0) { slave_print_error(rli,my_errno, "Error in Create_file event: could not open file '%s'", fname_buf); goto err; @@ -3073,7 +3085,7 @@ int Append_block_log_event::exec_event(struct st_relay_log_info* rli) memcpy(p, ".data", 6); strnmov(proc_info, "Making temp file ", 17); // no end 0 thd->proc_info= proc_info; - if ((fd = my_open(fname, O_WRONLY|O_APPEND|O_BINARY, MYF(MY_WME))) < 0) + if ((fd = my_open(fname, O_WRONLY|O_APPEND|O_BINARY|O_NOFOLLOW, MYF(MY_WME))) < 0) { slave_print_error(rli,my_errno, "Error in Append_block event: could not open file '%s'", fname); goto err; @@ -3270,7 +3282,7 @@ int Execute_load_log_event::exec_event(struct st_relay_log_info* rli) Load_log_event* lev = 0; memcpy(p, ".info", 6); - if ((fd = my_open(fname, O_RDONLY|O_BINARY, MYF(MY_WME))) < 0 || + if ((fd = my_open(fname, O_RDONLY|O_BINARY|O_NOFOLLOW, MYF(MY_WME))) < 0 || init_io_cache(&file, fd, IO_SIZE, READ_CACHE, (my_off_t)0, 0, MYF(MY_WME|MY_NABP))) { diff --git a/sql/mysql_priv.h b/sql/mysql_priv.h index 6218bc49f53..2b13b2053d7 100644 --- a/sql/mysql_priv.h +++ b/sql/mysql_priv.h @@ -212,6 +212,10 @@ extern CHARSET_INFO *national_charset_info, *table_alias_charset; #define OPTION_RELAXED_UNIQUE_CHECKS (1L << 27) #define SELECT_NO_UNLOCK (1L << 28) +/* If set to 0, then the thread will ignore all warnings with level notes. + Set by executing SET SQL_NOTES=1 */ +#define OPTION_SQL_NOTES (1L << 31) + /* Bits for different SQL modes modes (including ANSI mode) */ #define MODE_REAL_AS_FLOAT 1 #define MODE_PIPES_AS_CONCAT 2 @@ -750,7 +754,7 @@ bool close_temporary_table(THD *thd, const char *db, const char *table_name); void close_temporary(TABLE *table, bool delete_table=1); bool rename_temporary_table(THD* thd, TABLE *table, const char *new_db, const char *table_name); -void remove_db_from_cache(const my_string db); +void remove_db_from_cache(const char *db); void flush_tables(); bool remove_table_from_cache(THD *thd, const char *db, const char *table, bool return_if_owned_by_thd=0); @@ -886,7 +890,7 @@ extern ulong ha_read_first_count, ha_read_last_count; extern ulong ha_read_rnd_count, ha_read_rnd_next_count, ha_discover_count; extern ulong ha_commit_count, ha_rollback_count,table_cache_size; extern ulong max_connections,max_connect_errors, connect_timeout; -extern ulong slave_net_timeout; +extern ulong slave_net_timeout, slave_trans_retries; extern ulong max_user_connections; extern ulong long_query_count, what_to_log,flush_time; extern ulong query_buff_size, thread_stack,thread_stack_min; @@ -911,7 +915,7 @@ extern my_bool opt_sql_bin_update, opt_safe_user_create, opt_no_mix_types; extern my_bool opt_safe_show_db, opt_local_infile; extern my_bool opt_slave_compressed_protocol, use_temp_pool; extern my_bool opt_readonly, lower_case_file_system; -extern my_bool opt_enable_named_pipe, opt_sync_frm; +extern my_bool opt_enable_named_pipe, opt_sync_frm, opt_allow_suspicious_udfs; extern my_bool opt_secure_auth; extern uint opt_crash_binlog_innodb; extern char *shared_memory_base_name, *mysqld_unix_port; @@ -963,6 +967,7 @@ extern SHOW_COMP_OPTION have_query_cache, have_berkeley_db, have_innodb; extern SHOW_COMP_OPTION have_geometry, have_rtree_keys; extern SHOW_COMP_OPTION have_crypt; extern SHOW_COMP_OPTION have_compress; +extern SHOW_COMP_OPTION have_blackhole_db; #ifndef __WIN__ extern pthread_t signal_thread; diff --git a/sql/mysqld.cc b/sql/mysqld.cc index 53dca59bc92..d75efbd0b00 100644 --- a/sql/mysqld.cc +++ b/sql/mysqld.cc @@ -290,7 +290,7 @@ const char *opt_ndbcluster_connectstring= 0; my_bool opt_ndb_shm, opt_ndb_optimized_node_selection; #endif my_bool opt_readonly, use_temp_pool, relay_log_purge; -my_bool opt_sync_bdb_logs, opt_sync_frm; +my_bool opt_sync_bdb_logs, opt_sync_frm, opt_allow_suspicious_udfs; my_bool opt_secure_auth= 0; my_bool opt_short_log_format= 0; my_bool opt_log_queries_not_using_indexes= 0; @@ -298,18 +298,20 @@ my_bool lower_case_file_system= 0; my_bool opt_innodb_safe_binlog= 0; volatile bool mqh_used = 0; +#ifdef HAVE_INITGROUPS +static bool calling_initgroups= FALSE; /* Used in SIGSEGV handler. */ +#endif uint mysqld_port, test_flags, select_errors, dropping_tables, ha_open_options; uint delay_key_write_options, protocol_version; uint lower_case_table_names; uint opt_crash_binlog_innodb; uint volatile thread_count, thread_running, kill_cached_threads, wake_thread; - ulong back_log, connect_timeout, concurrency; ulong server_id, thd_startup_options; ulong table_cache_size, thread_stack, thread_stack_min, what_to_log; ulong query_buff_size, slow_launch_time, slave_open_temp_tables; ulong open_files_limit, max_binlog_size, max_relay_log_size; -ulong slave_net_timeout; +ulong slave_net_timeout, slave_trans_retries; ulong thread_cache_size=0, binlog_cache_size=0, max_binlog_cache_size=0; ulong query_cache_size=0; ulong com_stat[(uint) SQLCOM_END], com_other; @@ -396,6 +398,7 @@ SHOW_COMP_OPTION have_berkeley_db, have_innodb, have_isam, have_ndbcluster, SHOW_COMP_OPTION have_raid, have_openssl, have_symlink, have_query_cache; SHOW_COMP_OPTION have_geometry, have_rtree_keys; SHOW_COMP_OPTION have_crypt, have_compress; +SHOW_COMP_OPTION have_blackhole_db; /* Thread specific variables */ @@ -528,6 +531,7 @@ extern "C" pthread_handler_decl(handle_slave,arg); static ulong find_bit_type(const char *x, TYPELIB *bit_lib); static void clean_up(bool print_message); static void clean_up_mutexes(void); +static void wait_for_signal_thread_to_end(void); static int test_if_case_insensitive(const char *dir_name); static void create_pid_file(); @@ -916,6 +920,7 @@ extern "C" void unireg_abort(int exit_code) sql_print_error("Aborting\n"); clean_up(exit_code || !opt_bootstrap); /* purecov: inspected */ DBUG_PRINT("quit",("done with cleanup in unireg_abort")); + wait_for_signal_thread_to_end(); clean_up_mutexes(); my_end(opt_endinfo ? MY_CHECK_ERROR | MY_GIVE_INFO : 0); exit(exit_code); /* purecov: inspected */ @@ -950,6 +955,7 @@ void clean_up(bool print_message) item_user_lock_free(); lex_free(); /* Free some memory */ set_var_free(); + free_charsets(); #ifdef HAVE_DLOPEN if (!opt_noacl) udf_free(); @@ -1020,6 +1026,29 @@ void clean_up(bool print_message) } /* clean_up */ +/* + This is mainly needed when running with purify, but it's still nice to + know that all child threads have died when mysqld exits +*/ + +static void wait_for_signal_thread_to_end() +{ +#ifndef __NETWARE__ + uint i; + /* + Wait up to 10 seconds for signal thread to die. We use this mainly to + avoid getting warnings that my_thread_end has not been called + */ + for (i= 0 ; i < 100 && signal_thread_in_use; i++) + { + if (pthread_kill(signal_thread, MYSQL_KILL_SIGNAL)) + break; + my_sleep(100); // Give it time to die + } +#endif +} + + static void clean_up_mutexes() { (void) pthread_mutex_destroy(&LOCK_mysql_create_db); @@ -1142,7 +1171,15 @@ static void set_user(const char *user, struct passwd *user_info) #if !defined(__WIN__) && !defined(OS2) && !defined(__NETWARE__) DBUG_ASSERT(user_info); #ifdef HAVE_INITGROUPS - initgroups((char*) user,user_info->pw_gid); + /* + We can get a SIGSEGV when calling initgroups() on some systems when NSS + is configured to use LDAP and the server is statically linked. We set + calling_initgroups as a flag to the SIGSEGV handler that is then used to + output a specific message to help the user resolve this problem. + */ + calling_initgroups= TRUE; + initgroups((char*) user, user_info->pw_gid); + calling_initgroups= FALSE; #endif if (setgid(user_info->pw_gid) == -1) { @@ -1396,6 +1433,7 @@ void close_connection(THD *thd, uint errcode, bool lock) errcode ? ER(errcode) : "")); if (lock) (void) pthread_mutex_lock(&LOCK_thread_count); + thd->killed=1; if ((vio=thd->net.vio) != 0) { if (errcode) @@ -1896,6 +1934,17 @@ information that should help you find out what is causing the crash.\n"); fflush(stderr); #endif /* HAVE_STACKTRACE */ +#ifdef HAVE_INITGROUPS + if (calling_initgroups) + fprintf(stderr, "\n\ +This crash occured while the server was calling initgroups(). This is\n\ +often due to the use of a mysqld that is statically linked against glibc\n\ +and configured to use LDAP in /etc/nsswitch.conf. You will need to either\n\ +upgrade to a version of glibc that does not have this problem (2.3.4 or\n\ +later when used with nscd), disable LDAP in your nsswitch.conf, or use a\n\ +mysqld that is not statically linked.\n"); +#endif + if (test_flags & TEST_CORE_ON_SIGNAL) { fprintf(stderr, "Writing a core file\n"); @@ -2095,6 +2144,7 @@ extern "C" void *signal_hand(void *arg __attribute__((unused))) while ((error=my_sigwait(&set,&sig)) == EINTR) ; if (cleanup_done) { + DBUG_PRINT("quit",("signal_handler: calling my_thread_end()")); my_thread_end(); signal_thread_in_use= 0; pthread_exit(0); // Safety @@ -3013,8 +3063,17 @@ we force server id to 2, but this MySQL server will not act as a slave."); #endif if (opt_bootstrap) /* If running with bootstrap, do not start replication. */ opt_skip_slave_start= 1; - /* init_slave() must be called after the thread keys are created */ - init_slave(); + /* + init_slave() must be called after the thread keys are created. + Some parts of the code (e.g. SHOW STATUS LIKE 'slave_running' and other + places) assume that active_mi != 0, so let's fail if it's 0 (out of + memory); a message has already been printed. + */ + if (init_slave() && !active_mi) + { + end_thr_alarm(1); // Don't allow alarms + unireg_abort(1); + } if (opt_bootstrap) { @@ -3036,9 +3095,10 @@ we force server id to 2, but this MySQL server will not act as a slave."); printf(ER(ER_READY),my_progname,server_version, ((unix_sock == INVALID_SOCKET) ? (char*) "" : mysqld_unix_port), - mysqld_port); + mysqld_port, ""); if (MYSQL_COMPILATION_COMMENT[0] != '\0') fputs(" " MYSQL_COMPILATION_COMMENT, stdout); + putchar('\n'); fflush(stdout); @@ -3089,21 +3149,7 @@ we force server id to 2, but this MySQL server will not act as a slave."); CloseHandle(hEventShutdown); } #endif -#ifndef __NETWARE__ - { - uint i; - /* - Wait up to 10 seconds for signal thread to die. We use this mainly to - avoid getting warnings that my_thread_end has not been called - */ - for (i= 0 ; i < 100 && signal_thread_in_use; i++) - { - if (pthread_kill(signal_thread, MYSQL_KILL_SIGNAL)) - break; - my_sleep(100); // Give it time to die - } - } -#endif + wait_for_signal_thread_to_end(); clean_up_mutexes(); my_end(opt_endinfo ? MY_CHECK_ERROR | MY_GIVE_INFO : 0); @@ -3771,10 +3817,19 @@ pthread_handler_decl(handle_connections_shared_memory,arg) char *suffix_pos; char connect_number_char[22], *p; const char *errmsg= 0; + SECURITY_ATTRIBUTES *sa_event= 0, *sa_mapping= 0; my_thread_init(); DBUG_ENTER("handle_connections_shared_memorys"); DBUG_PRINT("general",("Waiting for allocated shared memory.")); + if (my_security_attr_create(&sa_event, &errmsg, + GENERIC_ALL, SYNCHRONIZE | EVENT_MODIFY_STATE)) + goto error; + + if (my_security_attr_create(&sa_mapping, &errmsg, + GENERIC_ALL, FILE_MAP_READ | FILE_MAP_WRITE)) + goto error; + /* The name of event and file-mapping events create agree next rule: shared_memory_base_name+unique_part @@ -3784,22 +3839,22 @@ pthread_handler_decl(handle_connections_shared_memory,arg) */ suffix_pos= strxmov(tmp,shared_memory_base_name,"_",NullS); strmov(suffix_pos, "CONNECT_REQUEST"); - if ((smem_event_connect_request= CreateEvent(0,FALSE,FALSE,tmp)) == 0) + if ((smem_event_connect_request= CreateEvent(sa_event, + FALSE, FALSE, tmp)) == 0) { errmsg= "Could not create request event"; goto error; } strmov(suffix_pos, "CONNECT_ANSWER"); - if ((event_connect_answer= CreateEvent(0,FALSE,FALSE,tmp)) == 0) + if ((event_connect_answer= CreateEvent(sa_event, FALSE, FALSE, tmp)) == 0) { errmsg="Could not create answer event"; goto error; } strmov(suffix_pos, "CONNECT_DATA"); - if ((handle_connect_file_map= CreateFileMapping(INVALID_HANDLE_VALUE,0, - PAGE_READWRITE, - 0,sizeof(connect_number), - tmp)) == 0) + if ((handle_connect_file_map= + CreateFileMapping(INVALID_HANDLE_VALUE, sa_mapping, + PAGE_READWRITE, 0, sizeof(connect_number), tmp)) == 0) { errmsg= "Could not create file mapping"; goto error; @@ -3844,10 +3899,9 @@ pthread_handler_decl(handle_connections_shared_memory,arg) suffix_pos= strxmov(tmp,shared_memory_base_name,"_",connect_number_char, "_",NullS); strmov(suffix_pos, "DATA"); - if ((handle_client_file_map= CreateFileMapping(INVALID_HANDLE_VALUE,0, - PAGE_READWRITE,0, - smem_buffer_length, - tmp)) == 0) + if ((handle_client_file_map= + CreateFileMapping(INVALID_HANDLE_VALUE, sa_mapping, + PAGE_READWRITE, 0, smem_buffer_length, tmp)) == 0) { errmsg= "Could not create file mapping"; goto errorconn; @@ -3860,31 +3914,33 @@ pthread_handler_decl(handle_connections_shared_memory,arg) goto errorconn; } strmov(suffix_pos, "CLIENT_WROTE"); - if ((event_client_wrote= CreateEvent(0, FALSE, FALSE, tmp)) == 0) + if ((event_client_wrote= CreateEvent(sa_event, FALSE, FALSE, tmp)) == 0) { errmsg= "Could not create client write event"; goto errorconn; } strmov(suffix_pos, "CLIENT_READ"); - if ((event_client_read= CreateEvent(0, FALSE, FALSE, tmp)) == 0) + if ((event_client_read= CreateEvent(sa_event, FALSE, FALSE, tmp)) == 0) { errmsg= "Could not create client read event"; goto errorconn; } strmov(suffix_pos, "SERVER_READ"); - if ((event_server_read= CreateEvent(0, FALSE, FALSE, tmp)) == 0) + if ((event_server_read= CreateEvent(sa_event, FALSE, FALSE, tmp)) == 0) { errmsg= "Could not create server read event"; goto errorconn; } strmov(suffix_pos, "SERVER_WROTE"); - if ((event_server_wrote= CreateEvent(0, FALSE, FALSE, tmp)) == 0) + if ((event_server_wrote= CreateEvent(sa_event, + FALSE, FALSE, tmp)) == 0) { errmsg= "Could not create server write event"; goto errorconn; } strmov(suffix_pos, "CONNECTION_CLOSED"); - if ((event_conn_closed= CreateEvent(0, TRUE , FALSE, tmp)) == 0) + if ((event_conn_closed= CreateEvent(sa_event, + TRUE, FALSE, tmp)) == 0) { errmsg= "Could not create closed connection event"; goto errorconn; @@ -3959,6 +4015,8 @@ error: strxmov(buff, "Can't create shared memory service: ", errmsg, ".", NullS); sql_perror(buff); } + my_security_attr_free(sa_event); + my_security_attr_free(sa_mapping); if (handle_connect_map) UnmapViewOfFile(handle_connect_map); if (handle_connect_file_map) CloseHandle(handle_connect_file_map); if (event_connect_answer) CloseHandle(event_connect_answer); @@ -4074,7 +4132,7 @@ enum options_mysqld OPT_QUERY_CACHE_TYPE, OPT_QUERY_CACHE_WLOCK_INVALIDATE, OPT_RECORD_BUFFER, OPT_RECORD_RND_BUFFER, OPT_RELAY_LOG_SPACE_LIMIT, OPT_RELAY_LOG_PURGE, OPT_SLAVE_NET_TIMEOUT, OPT_SLAVE_COMPRESSED_PROTOCOL, OPT_SLOW_LAUNCH_TIME, - OPT_READONLY, OPT_DEBUGGING, + OPT_SLAVE_TRANS_RETRIES, OPT_READONLY, OPT_DEBUGGING, OPT_SORT_BUFFER, OPT_TABLE_CACHE, OPT_THREAD_CONCURRENCY, OPT_THREAD_CACHE_SIZE, OPT_TMP_TABLE_SIZE, OPT_THREAD_STACK, @@ -4101,7 +4159,7 @@ enum options_mysqld OPT_BDB_MAX_LOCK, OPT_ERROR_LOG_FILE, OPT_DEFAULT_WEEK_FORMAT, - OPT_RANGE_ALLOC_BLOCK_SIZE, + OPT_RANGE_ALLOC_BLOCK_SIZE, OPT_ALLOW_SUSPICIOUS_UDFS, OPT_QUERY_ALLOC_BLOCK_SIZE, OPT_QUERY_PREALLOC_SIZE, OPT_TRANS_ALLOC_BLOCK_SIZE, OPT_TRANS_PREALLOC_SIZE, OPT_SYNC_FRM, OPT_SYNC_BINLOG, @@ -4139,8 +4197,15 @@ struct my_option my_long_options[] = (gptr*) &abort_slave_event_count, (gptr*) &abort_slave_event_count, 0, GET_INT, REQUIRED_ARG, 0, 0, 0, 0, 0, 0}, #endif /* HAVE_REPLICATION */ - {"ansi", 'a', "Use ANSI SQL syntax instead of MySQL syntax.", 0, 0, 0, + {"ansi", 'a', "Use ANSI SQL syntax instead of MySQL syntax. This mode will also set transaction isolation level 'serializable'.", 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}, + {"allow-suspicious-udfs", OPT_ALLOW_SUSPICIOUS_UDFS, + "Allows use of UDFs consisting of only one symbol xxx() " + "without corresponding xxx_init() or xxx_deinit(). That also means " + "that one can load any function from any library, for example exit() " + "from libc.so", + (gptr*) &opt_allow_suspicious_udfs, (gptr*) &opt_allow_suspicious_udfs, + 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0}, {"basedir", 'b', "Path to installation directory. All paths are usually resolved relative to this.", (gptr*) &mysql_home_ptr, (gptr*) &mysql_home_ptr, 0, GET_STR, REQUIRED_ARG, @@ -5010,7 +5075,7 @@ The minimum value for this variable is 4096.", "Max number of errors/warnings to store for a statement.", (gptr*) &global_system_variables.max_error_count, (gptr*) &max_system_variables.max_error_count, - 0, GET_ULONG, REQUIRED_ARG, DEFAULT_ERROR_COUNT, 1, 65535, 0, 1, 0}, + 0, GET_ULONG, REQUIRED_ARG, DEFAULT_ERROR_COUNT, 0, 65535, 0, 1, 0}, {"max_heap_table_size", OPT_MAX_HEP_TABLE_SIZE, "Don't allow creation of heap tables bigger than this.", (gptr*) &global_system_variables.max_heap_table_size, @@ -5027,7 +5092,7 @@ The minimum value for this variable is 4096.", (gptr*) &max_system_variables.max_length_for_sort_data, 0, GET_ULONG, REQUIRED_ARG, 1024, 4, 8192*1024L, 0, 1, 0}, {"max_relay_log_size", OPT_MAX_RELAY_LOG_SIZE, - "If non-zero: relay log will be rotated automatically when the size exceeds this value; if zero (the default): when the size exceeds max_binlog_size. 0 expected, the minimum value for this variable is 4096.", + "If non-zero: relay log will be rotated automatically when the size exceeds this value; if zero (the default): when the size exceeds max_binlog_size. 0 excepted, the minimum value for this variable is 4096.", (gptr*) &max_relay_log_size, (gptr*) &max_relay_log_size, 0, GET_ULONG, REQUIRED_ARG, 0L, 0L, 1024*1024L*1024L, 0, IO_SIZE, 0}, { "max_seeks_for_key", OPT_MAX_SEEKS_FOR_KEY, @@ -5198,6 +5263,12 @@ The minimum value for this variable is 4096.", "Number of seconds to wait for more data from a master/slave connection before aborting the read.", (gptr*) &slave_net_timeout, (gptr*) &slave_net_timeout, 0, GET_ULONG, REQUIRED_ARG, SLAVE_NET_TIMEOUT, 1, LONG_TIMEOUT, 0, 1, 0}, + {"slave_transaction_retries", OPT_SLAVE_TRANS_RETRIES, + "Number of times the slave SQL thread will retry a transaction in case " + "it failed with a deadlock or elapsed lock wait timeout, " + "before giving up and stopping.", + (gptr*) &slave_trans_retries, (gptr*) &slave_trans_retries, 0, + GET_ULONG, REQUIRED_ARG, 0L, 0L, (longlong) ULONG_MAX, 0, 1, 0}, #endif /* HAVE_REPLICATION */ {"slow_launch_time", OPT_SLOW_LAUNCH_TIME, "If creating the thread takes longer than this value (in seconds), the Slow_launch_threads counter will be incremented.", @@ -5445,7 +5516,8 @@ struct show_var_st status_vars[]= { {"Select_range_check", (char*) &select_range_check_count, SHOW_LONG}, {"Select_scan", (char*) &select_scan_count, SHOW_LONG}, {"Slave_open_temp_tables", (char*) &slave_open_temp_tables, SHOW_LONG}, - {"Slave_running", (char*) 0, SHOW_SLAVE_RUNNING}, + {"Slave_running", (char*) 0, SHOW_SLAVE_RUNNING}, + {"Slave_retried_transactions",(char*) 0, SHOW_SLAVE_RETRIED_TRANS}, {"Slow_launch_threads", (char*) &slow_launch_threads, SHOW_LONG}, {"Slow_queries", (char*) &long_query_count, SHOW_LONG}, {"Sort_merge_passes", (char*) &filesort_merge_passes, SHOW_LONG}, @@ -5622,7 +5694,8 @@ static void mysql_init_variables(void) language_ptr= language; mysql_data_home= mysql_real_data_home; thd_startup_options= (OPTION_UPDATE_LOG | OPTION_AUTO_IS_NULL | - OPTION_BIN_LOG | OPTION_QUOTE_SHOW_CREATE); + OPTION_BIN_LOG | OPTION_QUOTE_SHOW_CREATE | + OPTION_SQL_NOTES); protocol_version= PROTOCOL_VERSION; what_to_log= ~ (1L << (uint) COM_TIME); refresh_version= flush_version= 1L; /* Increments on each reload */ @@ -5709,6 +5782,11 @@ static void mysql_init_variables(void) #else have_archive_db= SHOW_OPTION_NO; #endif +#ifdef HAVE_BLACKHOLE_DB + have_blackhole_db= SHOW_OPTION_YES; +#else + have_blackhole_db= SHOW_OPTION_NO; +#endif #ifdef HAVE_CSV_DB have_csv_db= SHOW_OPTION_YES; #else diff --git a/sql/net_serv.cc b/sql/net_serv.cc index 5699b5a2f55..c8b2e28ec52 100644 --- a/sql/net_serv.cc +++ b/sql/net_serv.cc @@ -251,7 +251,7 @@ my_bool my_net_write(NET *net,const char *packet,ulong len) { uchar buff[NET_HEADER_SIZE]; - if (unlikely(!net->vio)) /* nowhere to write */ + if (unlikely(!net->vio)) /* nowhere to write */ return 0; /* Big packets are handled by splitting them in packets of MAX_PACKET_LENGTH diff --git a/sql/opt_sum.cc b/sql/opt_sum.cc index 613b2a4aec1..4ab506cc4e1 100644 --- a/sql/opt_sum.cc +++ b/sql/opt_sum.cc @@ -624,7 +624,6 @@ static bool find_key_for_maxmin(bool max_fl, TABLE_REF *ref, { if (!(field->flags & PART_KEY_FLAG)) return 0; // Not key field - *prefix_len= 0; TABLE *table= field->table; uint idx= 0; @@ -637,6 +636,7 @@ static bool find_key_for_maxmin(bool max_fl, TABLE_REF *ref, KEY_PART_INFO *part,*part_end; key_part_map key_part_to_use= 0; uint jdx= 0; + *prefix_len= 0; for (part= keyinfo->key_part, part_end= part+keyinfo->key_parts ; part != part_end ; part++, jdx++, key_part_to_use= (key_part_to_use << 1) | 1) diff --git a/sql/procedure.h b/sql/procedure.h index 5365b2e1102..abe50bdc0a0 100644 --- a/sql/procedure.h +++ b/sql/procedure.h @@ -59,7 +59,11 @@ public: void set(double nr) { value=nr; } void set(longlong nr) { value=(double) nr; } void set(const char *str,uint length,CHARSET_INFO *cs) - { int err; value=my_strntod(cs,(char*) str,length,(char**)0,&err); } + { + int err; + char *end_not_used; + value= my_strntod(cs, (char*) str, length, &end_not_used, &err); + } double val() { return value; } longlong val_int() { return (longlong) value; } String *val_str(String *s) { s->set(value,decimals,default_charset()); return s; } @@ -99,9 +103,10 @@ public: double val() { int err; - CHARSET_INFO *cs=str_value.charset(); + CHARSET_INFO *cs= str_value.charset(); + char *end_not_used; return my_strntod(cs, (char*) str_value.ptr(), str_value.length(), - (char**) 0, &err); + &end_not_used, &err); } longlong val_int() { diff --git a/sql/protocol.cc b/sql/protocol.cc index 95cd8415c85..773bbe697a3 100644 --- a/sql/protocol.cc +++ b/sql/protocol.cc @@ -58,6 +58,7 @@ void send_error(THD *thd, uint sql_errno, const char *err) uint length; char buff[MYSQL_ERRMSG_SIZE+2], *pos; #endif + const char *orig_err= err; NET *net= &thd->net; DBUG_ENTER("send_error"); DBUG_PRINT("enter",("sql_errno: %d err: %s", sql_errno, @@ -82,6 +83,7 @@ void send_error(THD *thd, uint sql_errno, const char *err) err=ER(sql_errno); /* purecov: inspected */ } } + orig_err= err; } #ifdef EMBEDDED_LIBRARY @@ -120,6 +122,9 @@ void send_error(THD *thd, uint sql_errno, const char *err) } VOID(net_write_command(net,(uchar) 255, "", 0, (char*) err,length)); #endif /* EMBEDDED_LIBRARY*/ + if (!thd->killed) + push_warning(thd, MYSQL_ERROR::WARN_LEVEL_ERROR, sql_errno, + orig_err ? orig_err : ER(sql_errno)); thd->is_fatal_error=0; // Error message is given thd->net.report_error= 0; @@ -247,6 +252,9 @@ net_printf(THD *thd, uint errcode, ...) strmake(net->last_error, text_pos, length); strmake(net->sqlstate, mysql_errno_to_sqlstate(errcode), SQLSTATE_LENGTH); #endif + if (!thd->killed) + push_warning(thd, MYSQL_ERROR::WARN_LEVEL_ERROR, errcode, + text_pos ? text_pos : ER(errcode)); thd->is_fatal_error=0; // Error message is given DBUG_VOID_RETURN; } diff --git a/sql/set_var.cc b/sql/set_var.cc index ca7987d2636..9cf3dcac047 100644 --- a/sql/set_var.cc +++ b/sql/set_var.cc @@ -320,6 +320,8 @@ sys_var_bool_ptr sys_slave_compressed_protocol("slave_compressed_protocol", #ifdef HAVE_REPLICATION sys_var_long_ptr sys_slave_net_timeout("slave_net_timeout", &slave_net_timeout); +sys_var_long_ptr sys_slave_trans_retries("slave_transaction_retries", + &slave_trans_retries); #endif sys_var_long_ptr sys_slow_launch_time("slow_launch_time", &slow_launch_time); @@ -423,6 +425,9 @@ static sys_var_thd_bit sys_log_binlog("sql_log_bin", static sys_var_thd_bit sys_sql_warnings("sql_warnings", 0, set_option_bit, OPTION_WARNINGS); +static sys_var_thd_bit sys_sql_notes("sql_notes", 0, + set_option_bit, + OPTION_SQL_NOTES); static sys_var_thd_bit sys_auto_is_null("sql_auto_is_null", 0, set_option_bit, OPTION_AUTO_IS_NULL); @@ -601,6 +606,7 @@ sys_var *sys_variables[]= #ifdef HAVE_REPLICATION &sys_slave_compressed_protocol, &sys_slave_net_timeout, + &sys_slave_trans_retries, &sys_slave_skip_counter, #endif &sys_slow_launch_time, @@ -610,6 +616,7 @@ sys_var *sys_variables[]= &sys_sql_max_join_size, &sys_sql_mode, &sys_sql_warnings, + &sys_sql_notes, &sys_storage_engine, #ifdef HAVE_REPLICATION &sys_sync_binlog_period, @@ -696,6 +703,7 @@ struct show_var_st init_vars[]= { {sys_group_concat_max_len.name, (char*) &sys_group_concat_max_len, SHOW_SYS}, {"have_archive", (char*) &have_archive_db, SHOW_HAVE}, {"have_bdb", (char*) &have_berkeley_db, SHOW_HAVE}, + {"have_blackhole_engine", (char*) &have_blackhole_db, SHOW_HAVE}, {"have_compress", (char*) &have_compress, SHOW_HAVE}, {"have_crypt", (char*) &have_crypt, SHOW_HAVE}, {"have_csv", (char*) &have_csv_db, SHOW_HAVE}, @@ -838,6 +846,7 @@ struct show_var_st init_vars[]= { {sys_read_rnd_buff_size.name,(char*) &sys_read_rnd_buff_size, SHOW_SYS}, #ifdef HAVE_REPLICATION {sys_relay_log_purge.name, (char*) &sys_relay_log_purge, SHOW_SYS}, + {"relay_log_space_limit", (char*) &relay_log_space_limit, SHOW_LONGLONG}, #endif {sys_rpl_recovery_rank.name,(char*) &sys_rpl_recovery_rank, SHOW_SYS}, {"secure_auth", (char*) &sys_secure_auth, SHOW_SYS}, @@ -851,6 +860,7 @@ struct show_var_st init_vars[]= { {"skip_show_database", (char*) &opt_skip_show_db, SHOW_BOOL}, #ifdef HAVE_REPLICATION {sys_slave_net_timeout.name,(char*) &sys_slave_net_timeout, SHOW_SYS}, + {sys_slave_trans_retries.name,(char*) &sys_slave_trans_retries, SHOW_SYS}, #endif {sys_slow_launch_time.name, (char*) &sys_slow_launch_time, SHOW_SYS}, #ifdef HAVE_SYS_UN_H @@ -859,6 +869,8 @@ struct show_var_st init_vars[]= { {sys_sort_buffer.name, (char*) &sys_sort_buffer, SHOW_SYS}, {sys_sql_mode.name, (char*) &sys_sql_mode, SHOW_SYS}, {sys_storage_engine.name, (char*) &sys_storage_engine, SHOW_SYS}, + {"sql_notes", (char*) &sys_sql_notes, SHOW_BOOL}, + {"sql_warnings", (char*) &sys_sql_warnings, SHOW_BOOL}, #ifdef HAVE_REPLICATION {sys_sync_binlog_period.name,(char*) &sys_sync_binlog_period, SHOW_SYS}, {sys_sync_replication.name, (char*) &sys_sync_replication, SHOW_SYS}, @@ -1223,6 +1235,12 @@ static void fix_server_id(THD *thd, enum_var_type type) server_id_supplied = 1; } +bool sys_var_long_ptr::check(THD *thd, set_var *var) +{ + longlong v= var->value->val_int(); + var->save_result.ulonglong_value= v < 0 ? 0 : v; + return 0; +} bool sys_var_long_ptr::update(THD *thd, set_var *var) { @@ -3089,8 +3107,14 @@ ulong fix_sql_mode(ulong sql_mode) */ if (sql_mode & MODE_ANSI) + { sql_mode|= (MODE_REAL_AS_FLOAT | MODE_PIPES_AS_CONCAT | MODE_ANSI_QUOTES | - MODE_IGNORE_SPACE | MODE_ONLY_FULL_GROUP_BY); + MODE_IGNORE_SPACE); + /* + MODE_ONLY_FULL_GROUP_BY removed from ANSI mode because it is currently + overly restrictive (see BUG#8510). + */ + } if (sql_mode & MODE_ORACLE) sql_mode|= (MODE_PIPES_AS_CONCAT | MODE_ANSI_QUOTES | MODE_IGNORE_SPACE | diff --git a/sql/set_var.h b/sql/set_var.h index df2fd41c7bd..080a2a95ae0 100644 --- a/sql/set_var.h +++ b/sql/set_var.h @@ -93,6 +93,7 @@ public: sys_var_long_ptr(const char *name_arg, ulong *value_ptr, sys_after_update_func func) :sys_var(name_arg,func), value(value_ptr) {} + bool check(THD *thd, set_var *var); bool update(THD *thd, set_var *var); void set_default(THD *thd, enum_var_type type); SHOW_TYPE type() { return SHOW_LONG; } diff --git a/sql/share/english/errmsg.txt b/sql/share/english/errmsg.txt index 8ede3f61a0b..854adab455e 100644 --- a/sql/share/english/errmsg.txt +++ b/sql/share/english/errmsg.txt @@ -90,7 +90,7 @@ character-set=latin1 "Specified key was too long; max key length is %d bytes", "Key column '%-.64s' doesn't exist in table", "BLOB column '%-.64s' can't be used in key specification with the used table type", -"Column length too big for column '%-.64s' (max = %d); use BLOB instead", +"Column length too big for column '%-.64s' (max = %d); use BLOB or TEXT instead", "Incorrect table definition; there can be only one auto column and it must be defined as a key", "%s: ready for connections.\nVersion: '%s' socket: '%s' port: %d", "%s: Normal shutdown\n", @@ -143,7 +143,7 @@ character-set=latin1 "No paths allowed for shared library", "Function '%-.64s' already exists", "Can't open shared library '%-.64s' (errno: %d %-.64s)", -"Can't find function '%-.64s' in library'", +"Can't find function '%-.64s' in library", "Function '%-.64s' is not defined", "Host '%-.64s' is blocked because of many connection errors; unblock with 'mysqladmin flush-hosts'", "Host '%-.64s' is not allowed to connect to this MySQL server", diff --git a/sql/slave.cc b/sql/slave.cc index a39cbdbe14b..605f8289946 100644 --- a/sql/slave.cc +++ b/sql/slave.cc @@ -27,6 +27,7 @@ #include <my_dir.h> #include <sql_common.h> +#define MAX_SLAVE_RETRY_PAUSE 5 bool use_slave_mask = 0; MY_BITMAP slave_error_mask; @@ -2335,7 +2336,7 @@ st_relay_log_info::st_relay_log_info() ignore_log_space_limit(0), last_master_timestamp(0), slave_skip_counter(0), abort_pos_wait(0), slave_run_id(0), sql_thd(0), last_slave_errno(0), inited(0), abort_slave(0), slave_running(0), until_condition(UNTIL_NONE), - until_log_pos(0) + until_log_pos(0), retried_trans(0) { group_relay_log_name[0]= event_relay_log_name[0]= group_master_log_name[0]= 0; @@ -2959,6 +2960,60 @@ static int exec_relay_log_event(THD* thd, RELAY_LOG_INFO* rli) exec_res = ev->exec_event(rli); DBUG_ASSERT(rli->sql_thd==thd); delete ev; + if (slave_trans_retries) + { + if (exec_res && + (thd->net.last_errno == ER_LOCK_DEADLOCK || + thd->net.last_errno == ER_LOCK_WAIT_TIMEOUT) && + !thd->is_fatal_error) + { + const char *errmsg; + /* + We were in a transaction which has been rolled back because of a + deadlock (currently, InnoDB deadlock detected by InnoDB) or lock + wait timeout (innodb_lock_wait_timeout exceeded); let's seek back to + BEGIN log event and retry it all again. + We have to not only seek but also + a) init_master_info(), to seek back to hot relay log's start for later + (for when we will come back to this hot log after re-processing the + possibly existing old logs where BEGIN is: check_binlog_magic() will + then need the cache to be at position 0 (see comments at beginning of + init_master_info()). + b) init_relay_log_pos(), because the BEGIN may be an older relay log. + */ + if (rli->trans_retries < slave_trans_retries) + { + if (init_master_info(rli->mi, 0, 0, 0, SLAVE_SQL)) + sql_print_error("Failed to initialize the master info structure"); + else if (init_relay_log_pos(rli, + rli->group_relay_log_name, + rli->group_relay_log_pos, + 1, &errmsg)) + sql_print_error("Error initializing relay log position: %s", + errmsg); + else + { + exec_res= 0; + /* chance for concurrent connection to get more locks */ + safe_sleep(thd, min(rli->trans_retries, MAX_SLAVE_RETRY_PAUSE), + (CHECK_KILLED_FUNC)sql_slave_killed, (void*)rli); + pthread_mutex_lock(&rli->data_lock); // because of SHOW STATUS + rli->trans_retries++; + rli->retried_trans++; + pthread_mutex_unlock(&rli->data_lock); + DBUG_PRINT("info", ("Slave retries transaction " + "rli->trans_retries: %lu", rli->trans_retries)); + } + } + else + sql_print_error("Slave SQL thread retried transaction %lu time(s) " + "in vain, giving up. Consider raising the value of " + "the slave_transaction_retries variable.", + slave_trans_retries); + } + if (!((thd->options & OPTION_BEGIN) && opt_using_transactions)) + rli->trans_retries= 0; // restart from fresh + } return exec_res; } else @@ -3370,6 +3425,7 @@ slave_begin: pthread_mutex_lock(&rli->log_space_lock); rli->ignore_log_space_limit= 0; pthread_mutex_unlock(&rli->log_space_lock); + rli->trans_retries= 0; // start from "no error" if (init_relay_log_pos(rli, rli->group_relay_log_name, diff --git a/sql/slave.h b/sql/slave.h index bcd79dd4a39..5a85e26d9ad 100644 --- a/sql/slave.h +++ b/sql/slave.h @@ -294,7 +294,16 @@ typedef struct st_relay_log_info UNTIL_LOG_NAMES_CMP_UNKNOWN= -2, UNTIL_LOG_NAMES_CMP_LESS= -1, UNTIL_LOG_NAMES_CMP_EQUAL= 0, UNTIL_LOG_NAMES_CMP_GREATER= 1 } until_log_names_cmp_result; - + + /* + trans_retries varies between 0 to slave_transaction_retries and counts how + many times the slave has retried the present transaction; gets reset to 0 + when the transaction finally succeeds. retried_trans is a cumulative + counter: how many times the slave has retried a transaction (any) since + slave started. + */ + ulong trans_retries, retried_trans; + st_relay_log_info(); ~st_relay_log_info(); diff --git a/sql/sql_acl.cc b/sql/sql_acl.cc index 7c17a4ef275..02da05d195f 100644 --- a/sql/sql_acl.cc +++ b/sql/sql_acl.cc @@ -139,6 +139,7 @@ my_bool acl_init(THD *org_thd, bool dont_read_acl_tables) MYSQL_LOCK *lock; my_bool return_val=1; bool check_no_resolve= specialflag & SPECIAL_NO_RESOLVE; + char tmp_name[NAME_LEN+1]; DBUG_ENTER("acl_init"); @@ -197,6 +198,23 @@ my_bool acl_init(THD *org_thd, bool dont_read_acl_tables) ACL_HOST host; update_hostname(&host.host,get_field(&mem, table->field[0])); host.db= get_field(&mem, table->field[1]); + if (lower_case_table_names) + { + /* + We make a temporary copy of the database, force it to lower case, + and then check it against the original name. + */ + (void)strnmov(tmp_name, host.db, sizeof(tmp_name)); + my_casedn_str(files_charset_info, host.db); + if (strcmp(host.db, tmp_name) != 0) + { + sql_print_warning("'host' entry '%s|%s' had database in mixed " + "case that has been forced to lowercase because " + "lower_case_table_names is set. It will not be " + "possible to remove this privilege using REVOKE.", + host.host.hostname, host.db); + } + } host.access= get_access(table,2); host.access= fix_rights_for_db(host.access); host.sort= get_sort(2,host.host.hostname,host.db); @@ -380,6 +398,23 @@ my_bool acl_init(THD *org_thd, bool dont_read_acl_tables) } db.access=get_access(table,3); db.access=fix_rights_for_db(db.access); + if (lower_case_table_names) + { + /* + We make a temporary copy of the database, force it to lower case, + and then check it against the original name. + */ + (void)strnmov(tmp_name, db.db, sizeof(tmp_name)); + my_casedn_str(files_charset_info, db.db); + if (strcmp(db.db, tmp_name) != 0) + { + sql_print_warning("'db' entry '%s %s@%s' had database in mixed " + "case that has been forced to lowercase because " + "lower_case_table_names is set. It will not be " + "possible to remove this privilege using REVOKE.", + db.db, db.user, db.host.hostname, db.host.hostname); + } + } db.sort=get_sort(3,db.host.hostname,db.db,db.user); #ifndef TO_BE_REMOVED if (table->fields <= 9) @@ -959,6 +994,9 @@ static void acl_insert_db(const char *user, const char *host, const char *db, /* Get privilege for a host, user and db combination + + as db_is_pattern changes the semantics of comparison, + acl_cache is not used if db_is_pattern is set. */ ulong acl_get(const char *host, const char *ip, @@ -978,7 +1016,7 @@ ulong acl_get(const char *host, const char *ip, db=tmp_db; } key_length=(uint) (end-key); - if ((entry=(acl_entry*) acl_cache->search(key,key_length))) + if (!db_is_pattern && (entry=(acl_entry*) acl_cache->search(key,key_length))) { db_access=entry->access; VOID(pthread_mutex_unlock(&acl_cache->lock)); @@ -1027,7 +1065,8 @@ ulong acl_get(const char *host, const char *ip, } exit: /* Save entry in cache for quick retrieval */ - if ((entry= (acl_entry*) malloc(sizeof(acl_entry)+key_length))) + if (!db_is_pattern && + (entry= (acl_entry*) malloc(sizeof(acl_entry)+key_length))) { entry->access=(db_access & host_access); entry->length=key_length; @@ -1339,7 +1378,7 @@ bool hostname_requires_resolving(const char *hostname) return FALSE; for (; (cur=*hostname); hostname++) { - if ((cur != '%') && (cur != '_') && (cur != '.') && + if ((cur != '%') && (cur != '_') && (cur != '.') && (cur != '/') && ((cur < '0') || (cur > '9'))) return TRUE; } @@ -1753,7 +1792,8 @@ static byte* get_key_column(GRANT_COLUMN *buff,uint *length, class GRANT_TABLE :public Sql_alloc { public: - char *host,*db, *user, *tname, *hash_key, *orig_host; + acl_host_and_ip host; + char *db, *user, *tname, *hash_key; ulong privs, cols; ulong sort; uint key_length; @@ -1772,12 +1812,10 @@ GRANT_TABLE::GRANT_TABLE(const char *h, const char *d,const char *u, :privs(p), cols(c) { /* Host given by user */ - orig_host= strdup_root(&memex,h); - /* Convert empty hostname to '%' for easy comparision */ - host= orig_host[0] ? orig_host : (char*) "%"; + update_hostname(&host, strdup_root(&memex, h)); db = strdup_root(&memex,d); user = strdup_root(&memex,u); - sort= get_sort(3,host,db,user); + sort= get_sort(3,host.hostname,db,user); tname= strdup_root(&memex,t); if (lower_case_table_names) { @@ -1796,17 +1834,12 @@ GRANT_TABLE::GRANT_TABLE(TABLE *form, TABLE *col_privs) { byte key[MAX_KEY_LENGTH]; - orig_host= host= get_field(&memex, form->field[0]); + update_hostname(&host, get_field(&memex, form->field[0])); db= get_field(&memex,form->field[1]); user= get_field(&memex,form->field[2]); if (!user) user= (char*) ""; - if (!orig_host) - { - orig_host= (char*) ""; - host= (char*) "%"; - } - sort= get_sort(3, orig_host, db, user); + sort= get_sort(3, host.hostname, db, user); tname= get_field(&memex,form->field[3]); if (!db || !tname) { @@ -1833,7 +1866,7 @@ GRANT_TABLE::GRANT_TABLE(TABLE *form, TABLE *col_privs) if (cols) { int key_len; - col_privs->field[0]->store(orig_host,(uint) strlen(orig_host), + col_privs->field[0]->store(host.hostname,(uint) strlen(host.hostname), &my_charset_latin1); col_privs->field[1]->store(db,(uint) strlen(db), &my_charset_latin1); col_privs->field[2]->store(user,(uint) strlen(user), &my_charset_latin1); @@ -1910,17 +1943,12 @@ static GRANT_TABLE *table_hash_search(const char *host,const char* ip, { if (exact) { - if ((host && - !my_strcasecmp(&my_charset_latin1, host, grant_table->host)) || - (ip && !strcmp(ip,grant_table->host))) + if (compare_hostname(&grant_table->host, host, ip)) return grant_table; } else { - if (((host && !wild_case_compare(&my_charset_latin1, - host,grant_table->host)) || - (ip && !wild_case_compare(&my_charset_latin1, - ip,grant_table->host))) && + if (compare_hostname(&grant_table->host, host, ip) && (!found || found->sort < grant_table->sort)) found=grant_table; // Host ok } @@ -2657,7 +2685,7 @@ my_bool grant_init(THD *org_thd) if (check_no_resolve) { - if (hostname_requires_resolving(mem_check->host)) + if (hostname_requires_resolving(mem_check->host.hostname)) { sql_print_warning("'tables_priv' entry '%s %s@%s' " "ignored in --skip-name-resolve mode.", @@ -2946,10 +2974,7 @@ bool check_grant_db(THD *thd,const char *db) idx); if (len < grant_table->key_length && !memcmp(grant_table->hash_key,helping,len) && - (thd->host && !wild_case_compare(&my_charset_latin1, - thd->host,grant_table->host) || - (thd->ip && !wild_case_compare(&my_charset_latin1, - thd->ip,grant_table->host)))) + compare_hostname(&grant_table->host, thd->host, thd->ip)) { error=0; // Found match break; @@ -3289,7 +3314,7 @@ int mysql_show_grants(THD *thd,LEX_USER *lex_user) if (!strcmp(lex_user->user.str,user) && !my_strcasecmp(&my_charset_latin1, lex_user->host.str, - grant_table->orig_host)) + grant_table->host.hostname)) { ulong table_access= grant_table->privs; if ((table_access | grant_table->cols) != 0) @@ -3571,7 +3596,7 @@ int mysql_drop_user(THD *thd, List <LEX_USER> &list) counter); if (!(user=grant_table->user)) user= ""; - if (!(host=grant_table->host)) + if (!(host=grant_table->host.hostname)) host= ""; if (!strcmp(user_name->user.str,user) && @@ -3700,7 +3725,7 @@ int mysql_revoke_all(THD *thd, List <LEX_USER> &list) counter); if (!(user=grant_table->user)) user= ""; - if (!(host=grant_table->host)) + if (!(host=grant_table->host.hostname)) host= ""; if (!strcmp(lex_user->user.str,user) && diff --git a/sql/sql_base.cc b/sql/sql_base.cc index fe1f268e277..8d694c48849 100644 --- a/sql/sql_base.cc +++ b/sql/sql_base.cc @@ -485,58 +485,62 @@ void close_temporary(TABLE *table,bool delete_table) void close_temporary_tables(THD *thd) { TABLE *table,*next; - char *query, *name_in_query, *end; - uint greatest_key_length= 0; + char *query, *end; + uint query_buf_size; + bool found_user_tables = 0; if (!thd->temporary_tables) return; - /* - We write a DROP TEMPORARY TABLE for each temp table left, so that our - replication slave can clean them up. Not one multi-table DROP TABLE binlog - event: this would cause problems if slave uses --replicate-*-table. - */ LINT_INIT(end); + query_buf_size= 50; // Enough for DROP ... TABLE IF EXISTS - /* We'll re-use always same buffer so make it big enough for longest name */ for (table=thd->temporary_tables ; table ; table=table->next) - greatest_key_length= max(greatest_key_length, table->key_length); + /* + We are going to add 4 ` around the db/table names, so 1 does not look + enough; indeed it is enough, because table->key_length is greater (by 8, + because of server_id and thread_id) than db||table. + */ + query_buf_size+= table->key_length+1; - if ((query = alloc_root(thd->mem_root, greatest_key_length+50))) + if ((query = alloc_root(thd->mem_root, query_buf_size))) // Better add "if exists", in case a RESET MASTER has been done - name_in_query= strmov(query, "DROP /*!40005 TEMPORARY */ TABLE IF EXISTS `"); + end=strmov(query, "DROP /*!40005 TEMPORARY */ TABLE IF EXISTS "); for (table=thd->temporary_tables ; table ; table=next) { - /* - In we are OOM for 'query' this is not fatal. We skip temporary tables - not created directly by the user. - */ - if (query && mysql_bin_log.is_open() && (table->real_name[0] != '#')) + if (query) // we might be out of memory, but this is not fatal { + // skip temporary tables not created directly by the user + if (table->real_name[0] != '#') + found_user_tables = 1; /* Here we assume table_cache_key always starts with \0 terminated db name */ - end = strxmov(name_in_query, table->table_cache_key, "`.`", - table->real_name, "`", NullS); - Query_log_event qinfo(thd, query, (ulong)(end-query), 0, FALSE); - /* - Imagine the thread had created a temp table, then was doing a SELECT, and - the SELECT was killed. Then it's not clever to mark the statement above as - "killed", because it's not really a statement updating data, and there - are 99.99% chances it will succeed on slave. And, if thread is - killed now, it's not clever either. - If a real update (one updating a persistent table) was killed on the - master, then this real update will be logged with error_code=killed, - rightfully causing the slave to stop. - */ - qinfo.error_code= 0; - mysql_bin_log.write(&qinfo); + end = strxmov(end,"`",table->table_cache_key,"`.`", + table->real_name,"`,", NullS); } next=table->next; close_temporary(table); } + if (query && found_user_tables && mysql_bin_log.is_open()) + { + /* The -1 is to remove last ',' */ + thd->clear_error(); + Query_log_event qinfo(thd, query, (ulong)(end-query)-1, 0, FALSE); + /* + Imagine the thread had created a temp table, then was doing a SELECT, and + the SELECT was killed. Then it's not clever to mark the statement above as + "killed", because it's not really a statement updating data, and there + are 99.99% chances it will succeed on slave. + If a real update (one updating a persistent table) was killed on the + master, then this real update will be logged with error_code=killed, + rightfully causing the slave to stop. + */ + qinfo.error_code= 0; + mysql_bin_log.write(&qinfo); + } thd->temporary_tables=0; } @@ -2864,8 +2868,18 @@ static void mysql_rm_tmp_tables(void) ** and afterwards delete those marked unused. */ -void remove_db_from_cache(const my_string db) +void remove_db_from_cache(const char *db) { + char name_buff[NAME_LEN+1]; + if (db && lower_case_table_names) + { + /* + convert database to lower case for comparision. + */ + strmake(name_buff, db, sizeof(name_buff)-1); + my_casedn_str(files_charset_info, name_buff); + db= name_buff; + } for (uint idx=0 ; idx < open_cache.records ; idx++) { TABLE *table=(TABLE*) hash_element(&open_cache,idx); diff --git a/sql/sql_cache.cc b/sql/sql_cache.cc index e1367412601..6eff958257b 100644 --- a/sql/sql_cache.cc +++ b/sql/sql_cache.cc @@ -1421,7 +1421,7 @@ ulong Query_cache::init_cache() init(); approx_additional_data_size = (sizeof(Query_cache) + sizeof(gptr)*(def_query_hash_size+ - def_query_hash_size)); + def_table_hash_size)); if (query_cache_size < approx_additional_data_size) goto err; diff --git a/sql/sql_class.cc b/sql/sql_class.cc index e11d8369f57..c20d5f79277 100644 --- a/sql/sql_class.cc +++ b/sql/sql_class.cc @@ -176,6 +176,7 @@ THD::THD() lock=locked_tables=0; used_tables=0; cuted_fields= sent_row_count= 0L; + limit_found_rows= 0; statement_id_counter= 0UL; // Must be reset to handle error with THD's created for init of mysqld lex->current_select= 0; @@ -707,6 +708,8 @@ struct Item_change_record: public ilink Item *old_value; /* Placement new was hidden by `new' in ilink (TODO: check): */ static void *operator new(size_t size, void *mem) { return mem; } + static void operator delete(void *ptr, size_t size) {} + static void operator delete(void *ptr, void *mem) { /* never called */ } }; diff --git a/sql/sql_class.h b/sql/sql_class.h index 657a92b394c..703bb030ab9 100644 --- a/sql/sql_class.h +++ b/sql/sql_class.h @@ -1295,7 +1295,11 @@ public: #include <myisam.h> -/* Param to create temporary tables when doing SELECT:s */ +/* + Param to create temporary tables when doing SELECT:s + NOTE + This structure is copied using memcpy as a part of JOIN. +*/ class TMP_TABLE_PARAM :public Sql_alloc { @@ -1307,7 +1311,6 @@ private: public: List<Item> copy_funcs; List<Item> save_copy_funcs; - List_iterator_fast<Item> copy_funcs_it; Copy_field *copy_field, *copy_field_end; Copy_field *save_copy_field, *save_copy_field_end; byte *group_buff; @@ -1324,7 +1327,7 @@ public: uint convert_blob_length; TMP_TABLE_PARAM() - :copy_funcs_it(copy_funcs), copy_field(0), group_parts(0), + :copy_field(0), group_parts(0), group_length(0), group_null_parts(0), convert_blob_length(0) {} ~TMP_TABLE_PARAM() diff --git a/sql/sql_db.cc b/sql/sql_db.cc index 407e2b23e38..c918480812c 100644 --- a/sql/sql_db.cc +++ b/sql/sql_db.cc @@ -668,7 +668,9 @@ int mysql_rm_db(THD *thd,char *db,bool if_exists, bool silent) thd->clear_error(); mysql_bin_log.write(&qinfo); } + thd->server_status|= SERVER_STATUS_DB_DROPPED; send_ok(thd, (ulong) deleted); + thd->server_status&= ~SERVER_STATUS_DB_DROPPED; } exit: diff --git a/sql/sql_delete.cc b/sql/sql_delete.cc index 8b4a0f0f6d0..cecdf8d1c62 100644 --- a/sql/sql_delete.cc +++ b/sql/sql_delete.cc @@ -117,6 +117,7 @@ int mysql_delete(THD *thd, TABLE_LIST *table_list, COND *conds, SQL_LIST *order, bzero((char*) &tables,sizeof(tables)); tables.table = table; + tables.alias = table_list->alias; table->sort.io_cache = (IO_CACHE *) my_malloc(sizeof(IO_CACHE), MYF(MY_FAE | MY_ZEROFILL)); diff --git a/sql/sql_derived.cc b/sql/sql_derived.cc index 9475ec08c96..e9f9b432c21 100644 --- a/sql/sql_derived.cc +++ b/sql/sql_derived.cc @@ -123,7 +123,7 @@ static int mysql_derived(THD *thd, LEX *lex, SELECT_LEX_UNIT *unit, DBUG_RETURN(1); // out of memory // st_select_lex_unit::prepare correctly work for single select - if ((res= unit->prepare(thd, derived_result, 0))) + if ((res= unit->prepare(thd, derived_result, 0, org_table_list->alias))) goto exit; @@ -132,10 +132,16 @@ static int mysql_derived(THD *thd, LEX *lex, SELECT_LEX_UNIT *unit, /* Temp table is created so that it hounours if UNION without ALL is to be processed + + As 'distinct' parameter we always pass FALSE (0), because underlying + query will control distinct condition by itself. Correct test of + distinct underlying query will be is_union && + !unit->union_distinct->next_select() (i.e. it is union and last distinct + SELECT is last SELECT of UNION). */ if (!(table= create_tmp_table(thd, &derived_result->tmp_table_param, - unit->types, (ORDER*) 0, - is_union && unit->union_distinct, 1, + unit->types, (ORDER*) 0, + FALSE, 1, (first_select->options | thd->options | TMP_TABLE_ALL_COLUMNS), HA_POS_ERROR, @@ -155,7 +161,7 @@ static int mysql_derived(THD *thd, LEX *lex, SELECT_LEX_UNIT *unit, if (is_union) { // execute union without clean up - if (!(res= unit->prepare(thd, derived_result, SELECT_NO_UNLOCK))) + if (!(res= unit->prepare(thd, derived_result, SELECT_NO_UNLOCK, ""))) res= unit->exec(); } else diff --git a/sql/sql_error.cc b/sql/sql_error.cc index eab5ec890df..d19e9fbdb09 100644 --- a/sql/sql_error.cc +++ b/sql/sql_error.cc @@ -104,6 +104,10 @@ MYSQL_ERROR *push_warning(THD *thd, MYSQL_ERROR::enum_warning_level level, { MYSQL_ERROR *err= 0; DBUG_ENTER("push_warning"); + + if (level == MYSQL_ERROR::WARN_LEVEL_NOTE && !(thd->options & OPTION_SQL_NOTES)) + return(0); + if (thd->query_id != thd->warn_id) mysql_reset_errors(thd); diff --git a/sql/sql_insert.cc b/sql/sql_insert.cc index 844e5e7dac2..1f190a450de 100644 --- a/sql/sql_insert.cc +++ b/sql/sql_insert.cc @@ -688,7 +688,8 @@ public: thd.current_tablenr=0; thd.version=refresh_version; thd.command=COM_DELAYED_INSERT; - thd.lex->current_select= 0; /* for my_message_sql */ + thd.lex->current_select= 0; // for my_message_sql + thd.lex->sql_command= SQLCOM_INSERT; // For innodb::store_lock() bzero((char*) &thd.net, sizeof(thd.net)); // Safety bzero((char*) &table_list, sizeof(table_list)); // Safety diff --git a/sql/sql_lex.cc b/sql/sql_lex.cc index ed974a48ad3..d6dcd9ce9ae 100644 --- a/sql/sql_lex.cc +++ b/sql/sql_lex.cc @@ -295,7 +295,18 @@ static char *get_text(LEX *lex) found_escape=1; if (lex->ptr == lex->end_of_query) return 0; - yySkip(); +#ifdef USE_MB + int l; + if (use_mb(cs) && + (l = my_ismbchar(cs, + (const char *)lex->ptr, + (const char *)lex->end_of_query))) { + lex->ptr += l; + continue; + } + else +#endif + yySkip(); } else if (c == sep) { @@ -323,6 +334,10 @@ static char *get_text(LEX *lex) else { uchar *to; + + /* Re-use found_escape for tracking state of escapes */ + found_escape= 0; + for (to=start ; str != end ; str++) { #ifdef USE_MB @@ -336,7 +351,7 @@ static char *get_text(LEX *lex) continue; } #endif - if (*str == '\\' && str+1 != end) + if (!found_escape && *str == '\\' && str+1 != end) { switch(*++str) { case 'n': @@ -362,15 +377,20 @@ static char *get_text(LEX *lex) *to++= '\\'; // remember prefix for wildcard /* Fall through */ default: - *to++ = *str; + found_escape= 1; + str--; break; } } - else if (*str == sep) - *to++= *str++; // Two ' or " + else if (!found_escape && *str == sep) + { + found_escape= 1; + } else + { *to++ = *str; - + found_escape= 0; + } } *to=0; lex->yytoklen=(uint) (to-start); diff --git a/sql/sql_lex.h b/sql/sql_lex.h index 7cb71607edf..927982e444f 100644 --- a/sql/sql_lex.h +++ b/sql/sql_lex.h @@ -125,27 +125,46 @@ enum tablespace_op_type /* The state of the lex parsing for selects + master and slaves are pointers to select_lex. + master is pointer to upper level node. + slave is pointer to lower level node + select_lex is a SELECT without union + unit is container of either + - One SELECT + - UNION of selects + select_lex and unit are both inherited form select_lex_node + neighbors are two select_lex or units on the same level + All select describing structures linked with following pointers: - - list of neighbors (next/prev) (prev of first element point to slave + - list of neighbors (next/prev) (prev of first element point to slave pointer of upper structure) - - one level units for unit (union) structure - - member of one union(unit) for ordinary select_lex - - pointer to master - - outer select_lex for unit (union) - - unit structure for ordinary select_lex - - pointer to slave - - first list element of select_lex belonged to this unit for unit - - first unit in list of units that belong to this select_lex (as - subselects or derived tables) for ordinary select_lex - - list of all select_lex (for group operation like correcting list of opened - tables) - - if unit contain several selects (union) then it have special - select_lex called fake_select_lex. It used for storing global parameters - and executing union. subqueries of global ORDER BY clause will be - attached to this fake_select_lex, which will allow them correctly - resolve fields of 'upper' union and other more outer selects. - - for example for following query: + - For select this is a list of UNION's (or one element list) + - For units this is a list of sub queries for the upper level select + + - pointer to master (master), which is + If this is a unit + - pointer to outer select_lex + If this is a select_lex + - pointer to outer unit structure for select + + - pointer to slave (slave), which is either: + If this is a unit: + - first SELECT that belong to this unit + If this is a select_lex + - first unit that belong to this SELECT (subquries or derived tables) + + - list of all select_lex (link_next/link_prev) + This is to be used for things like derived tables creation, where we + go through this list and create the derived tables. + + If unit contain several selects (UNION now, INTERSECT etc later) + then it have special select_lex called fake_select_lex. It used for + storing global parameters (like ORDER BY, LIMIT) and executing union. + Subqueries used in global ORDER BY clause will be attached to this + fake_select_lex, which will allow them correctly resolve fields of + 'upper' UNION and outer selects. + + For example for following query: select * from table1 @@ -163,6 +182,11 @@ enum tablespace_op_type we will have following structure: + select1: (select * from table1 ...) + select2: (select * from table2 ...) + select3: (select * from table3) + select1.1.1: (select * from table1_1_1) + ... main unit fake0 @@ -185,7 +209,12 @@ enum tablespace_op_type relation in main unit will be following: - + (bigger picture for: + main unit + fake0 + select1 select2 select3 + in the above picture) + main unit |^^^^|fake_select_lex |||||+--------------------------------------------+ @@ -241,7 +270,7 @@ public: static void *operator new(size_t size, MEM_ROOT *mem_root) { return (void*) alloc_root(mem_root, (uint) size); } static void operator delete(void *ptr,size_t size) {} - static void operator delete(void *ptr,size_t size, MEM_ROOT *mem_root) {} + static void operator delete(void *ptr, MEM_ROOT *mem_root) {} st_select_lex_node(): linkage(UNSPECIFIED_TYPE) {} virtual ~st_select_lex_node() {} inline st_select_lex_node* get_master() { return master; } @@ -360,7 +389,8 @@ public: void exclude_tree(); /* UNION methods */ - int prepare(THD *thd, select_result *result, ulong additional_options); + int prepare(THD *thd, select_result *result, ulong additional_options, + const char *tmp_table_alias); int exec(); int cleanup(); inline void unclean() { cleaned= 0; } @@ -382,7 +412,7 @@ private: typedef class st_select_lex_unit SELECT_LEX_UNIT; /* - SELECT_LEX - store information of parsed SELECT_LEX statment + SELECT_LEX - store information of parsed SELECT statment */ class st_select_lex: public st_select_lex_node { diff --git a/sql/sql_list.h b/sql/sql_list.h index a607b31d60c..be3e29b0c62 100644 --- a/sql/sql_list.h +++ b/sql/sql_list.h @@ -41,8 +41,8 @@ public: static void *operator new(size_t size, MEM_ROOT *mem_root) { return (void*) alloc_root(mem_root, (uint) size); } static void operator delete(void *ptr, size_t size) { TRASH(ptr, size); } - static void operator delete(void *ptr, size_t size, MEM_ROOT *mem_root) - { TRASH(ptr, size); } + static void operator delete(void *ptr, MEM_ROOT *mem_root) + { /* never called */ } static void operator delete[](void *ptr, size_t size) { TRASH(ptr, size); } #ifdef HAVE_purify bool dummy; diff --git a/sql/sql_parse.cc b/sql/sql_parse.cc index e66eeb279d2..86544f72316 100644 --- a/sql/sql_parse.cc +++ b/sql/sql_parse.cc @@ -47,6 +47,7 @@ extern "C" int gethostname(char *name, int namelen); #endif +static void time_out_user_resource_limits(THD *thd, USER_CONN *uc); #ifndef NO_EMBEDDED_ACCESS_CHECKS static int check_for_max_user_connections(THD *thd, USER_CONN *uc); #endif @@ -58,6 +59,7 @@ static void remove_escape(char *name); static void refresh_status(void); static bool append_file_to_dir(THD *thd, const char **filename_ptr, const char *table_name); +static void log_slow_query(THD *thd); const char *any_db="*any*"; // Special symbol for check_access @@ -442,6 +444,7 @@ static int check_for_max_user_connections(THD *thd, USER_CONN *uc) error=1; goto end; } + time_out_user_resource_limits(thd, uc); if (uc->user_resources.connections && uc->user_resources.connections <= uc->conn_per_hour) { @@ -543,36 +546,56 @@ bool is_update_query(enum enum_sql_command command) } /* - Check if maximum queries per hour limit has been reached - returns 0 if OK. + Reset per-hour user resource limits when it has been more than + an hour since they were last checked - In theory we would need a mutex in the USER_CONN structure for this to - be 100 % safe, but as the worst scenario is that we would miss counting - a couple of queries, this isn't critical. -*/ + SYNOPSIS: + time_out_user_resource_limits() + thd Thread handler + uc User connection details + NOTE: + This assumes that the LOCK_user_conn mutex has been acquired, so it is + safe to test and modify members of the USER_CONN structure. +*/ -static bool check_mqh(THD *thd, uint check_command) +static void time_out_user_resource_limits(THD *thd, USER_CONN *uc) { -#ifdef NO_EMBEDDED_ACCESS_CHECKS - return(0); -#else - bool error=0; + bool error= 0; time_t check_time = thd->start_time ? thd->start_time : time(NULL); - USER_CONN *uc=thd->user_connect; - DBUG_ENTER("check_mqh"); - DBUG_ASSERT(uc != 0); + DBUG_ENTER("time_out_user_resource_limits"); /* If more than a hour since last check, reset resource checking */ if (check_time - uc->intime >= 3600) { - (void) pthread_mutex_lock(&LOCK_user_conn); uc->questions=1; uc->updates=0; uc->conn_per_hour=0; uc->intime=check_time; - (void) pthread_mutex_unlock(&LOCK_user_conn); } + + DBUG_VOID_RETURN; +} + + +/* + Check if maximum queries per hour limit has been reached + returns 0 if OK. +*/ + +static bool check_mqh(THD *thd, uint check_command) +{ +#ifndef NO_EMBEDDED_ACCESS_CHECKS + bool error= 0; + time_t check_time = thd->start_time ? thd->start_time : time(NULL); + USER_CONN *uc=thd->user_connect; + DBUG_ENTER("check_mqh"); + DBUG_ASSERT(uc != 0); + + (void) pthread_mutex_lock(&LOCK_user_conn); + + time_out_user_resource_limits(thd, uc); + /* Check that we have not done too many questions / hour */ if (uc->user_resources.questions && uc->questions++ >= uc->user_resources.questions) @@ -595,7 +618,10 @@ static bool check_mqh(THD *thd, uint check_command) } } end: + (void) pthread_mutex_unlock(&LOCK_user_conn); DBUG_RETURN(error); +#else + return (0); #endif /* NO_EMBEDDED_ACCESS_CHECKS */ } @@ -656,12 +682,12 @@ static int check_connection(THD *thd) { uint connect_errors= 0; NET *net= &thd->net; + ulong pkt_len= 0; + char *end; DBUG_PRINT("info", ("New connection received on %s", vio_description(net->vio))); - vio_in_addr(net->vio,&thd->remote.sin_addr); - if (!thd->host) // If TCP/IP connection { char ip[30]; @@ -671,6 +697,7 @@ static int check_connection(THD *thd) if (!(thd->ip= my_strdup(ip,MYF(0)))) return (ER_OUT_OF_RESOURCES); thd->host_or_ip= thd->ip; + vio_in_addr(net->vio,&thd->remote.sin_addr); #if !defined(HAVE_SYS_UN_H) || defined(HAVE_mit_thread) /* Fast local hostname resolve for Win32 */ if (!strcmp(thd->ip,"127.0.0.1")) @@ -706,10 +733,10 @@ static int check_connection(THD *thd) DBUG_PRINT("info",("Host: %s",thd->host)); thd->host_or_ip= thd->host; thd->ip= 0; + /* Reset sin_addr */ + bzero((char*) &thd->remote, sizeof(thd->remote)); } vio_keepalive(net->vio, TRUE); - ulong pkt_len= 0; - char *end; { /* buff[] needs to big enough to hold the server_version variable */ char buff[SERVER_VERSION_LENGTH + SCRAMBLE_LENGTH + 64]; @@ -1472,6 +1499,7 @@ bool dispatch_command(enum enum_server_command command, THD *thd, { if (alloc_query(thd, packet, packet_length)) break; // fatal error is set + char *packet_end= thd->query + thd->query_length; mysql_log.write(thd,command,"%s",thd->query); DBUG_PRINT("query",("%-.4096s",thd->query)); mysql_parse(thd,thd->query, thd->query_length); @@ -1487,7 +1515,9 @@ bool dispatch_command(enum enum_server_command command, THD *thd, if (thd->lock || thd->open_tables || thd->derived_tables) close_thread_tables(thd); #endif - ulong length= thd->query_length-(ulong)(packet-thd->query); + ulong length= (ulong)(packet_end-packet); + + log_slow_query(thd); /* Remove garbage at start of query */ while (my_isspace(thd->charset(), *packet) && length > 0) @@ -1499,6 +1529,7 @@ bool dispatch_command(enum enum_server_command command, THD *thd, thd->query_length= length; thd->query= packet; thd->query_id= query_id++; + thd->set_time(); /* Reset the query start time. */ /* TODO: set thd->lex->sql_command to SQLCOM_END here */ VOID(pthread_mutex_unlock(&LOCK_thread_count)); #ifndef EMBEDDED_LIBRARY @@ -1795,6 +1826,24 @@ bool dispatch_command(enum enum_server_command command, THD *thd, if (thd->is_fatal_error) send_error(thd,0); // End of memory ? + log_slow_query(thd); + + thd->proc_info="cleaning up"; + VOID(pthread_mutex_lock(&LOCK_thread_count)); // For process list + thd->proc_info=0; + thd->command=COM_SLEEP; + thd->query=0; + thd->query_length=0; + thread_running--; + VOID(pthread_mutex_unlock(&LOCK_thread_count)); + thd->packet.shrink(thd->variables.net_buffer_length); // Reclaim some memory + free_root(thd->mem_root,MYF(MY_KEEP_PREALLOC)); + DBUG_RETURN(error); +} + + +static void log_slow_query(THD *thd) +{ time_t start_of_query=thd->start_time; thd->end_time(); // Set start time @@ -1813,17 +1862,6 @@ bool dispatch_command(enum enum_server_command command, THD *thd, mysql_slow_log.write(thd, thd->query, thd->query_length, start_of_query); } } - thd->proc_info="cleaning up"; - VOID(pthread_mutex_lock(&LOCK_thread_count)); // For process list - thd->proc_info=0; - thd->command=COM_SLEEP; - thd->query=0; - thd->query_length=0; - thread_running--; - VOID(pthread_mutex_unlock(&LOCK_thread_count)); - thd->packet.shrink(thd->variables.net_buffer_length); // Reclaim some memory - free_root(thd->mem_root,MYF(MY_KEEP_PREALLOC)); - DBUG_RETURN(error); } @@ -1924,9 +1962,14 @@ mysql_execute_command(THD *thd) } /* Skip if we are in the slave thread, some table rules have been - given and the table list says the query should not be replicated + given and the table list says the query should not be replicated. + Exception is DROP TEMPORARY TABLE IF EXISTS: we always execute it + (otherwise we have stale files on slave caused by exclusion of one tmp + table). */ - if (all_tables_not_ok(thd,tables)) + if (!(lex->sql_command == SQLCOM_DROP_TABLE && + lex->drop_temporary && lex->drop_if_exists) && + all_tables_not_ok(thd,tables)) { /* we warn the slave SQL thread */ my_error(ER_SLAVE_IGNORED_TABLE, MYF(0)); @@ -2768,18 +2811,23 @@ unsent_create_error: select_lex->options |= OPTION_BUFFER_RESULT; } - if (!(res= open_and_lock_tables(thd, tables)) && - !(res= mysql_prepare_insert(thd, tables, first_local_table, - tables->table, lex->field_list, 0, + if ((res= open_and_lock_tables(thd, tables))) + break; + + TABLE *table= tables->table; + /* Skip first table, which is the table we are inserting in */ + lex->select_lex.table_list.first= (byte*) first_local_table->next; + tables= (TABLE_LIST *) lex->select_lex.table_list.first; + first_local_table->next= 0; + + if (!(res= mysql_prepare_insert(thd, tables, first_local_table, + table, lex->field_list, 0, lex->update_list, lex->value_list, lex->duplicates)) && - (result= new select_insert(tables->table, &lex->field_list, + (result= new select_insert(table, &lex->field_list, &lex->update_list, &lex->value_list, lex->duplicates, lex->ignore))) { - TABLE *table= tables->table; - /* Skip first table, which is the table we are inserting in */ - lex->select_lex.table_list.first= (byte*) first_local_table->next; /* insert/replace from SELECT give its SELECT_LEX for SELECT, and item_list belong to SELECT @@ -2787,7 +2835,6 @@ unsent_create_error: lex->select_lex.resolve_mode= SELECT_LEX::SELECT_MODE; res= handle_select(thd, lex, result); /* revert changes for SP */ - lex->select_lex.table_list.first= (byte*) first_local_table; lex->select_lex.resolve_mode= SELECT_LEX::INSERT_MODE; delete result; table->insert_values= 0; @@ -2796,6 +2843,8 @@ unsent_create_error: } else res= -1; + first_local_table->next= tables; + lex->select_lex.table_list.first= (byte*) first_local_table; break; } case SQLCOM_TRUNCATE: @@ -3350,9 +3399,12 @@ purposes internal to the MySQL server", MYF(0)); my_strcasecmp(&my_charset_latin1, user->host.str, thd->host_or_ip))) { - if (check_access(thd, UPDATE_ACL, "mysql",0,1,0)) + if (check_access(thd, UPDATE_ACL, "mysql", 0, 1, 1)) + { + send_error(thd, ER_PASSWORD_NOT_ALLOWED); goto error; - break; // We are allowed to do changes + } + break; // We are allowed to do global changes } } } @@ -3679,6 +3731,7 @@ check_access(THD *thd, ulong want_access, const char *db, ulong *save_priv, db ? db : "", want_access, thd->master_access)); #ifndef NO_EMBEDDED_ACCESS_CHECKS ulong db_access; + bool db_is_pattern= test(want_access & GRANT_ACL); #endif ulong dummy; if (save_priv) @@ -3705,9 +3758,8 @@ check_access(THD *thd, ulong want_access, const char *db, ulong *save_priv, */ db_access= thd->db_access; if (!(thd->master_access & SELECT_ACL) && - (db && (!thd->db || strcmp(db,thd->db)))) - db_access=acl_get(thd->host, thd->ip, - thd->priv_user, db, test(want_access & GRANT_ACL)); + (db && (!thd->db || db_is_pattern || strcmp(db,thd->db)))) + db_access=acl_get(thd->host, thd->ip, thd->priv_user, db, db_is_pattern); *save_priv=thd->master_access | db_access; DBUG_RETURN(FALSE); } @@ -3725,9 +3777,8 @@ check_access(THD *thd, ulong want_access, const char *db, ulong *save_priv, if (db == any_db) DBUG_RETURN(FALSE); // Allow select on anything - if (db && (!thd->db || strcmp(db,thd->db))) - db_access=acl_get(thd->host, thd->ip, - thd->priv_user, db, test(want_access & GRANT_ACL)); + if (db && (!thd->db || db_is_pattern || strcmp(db,thd->db))) + db_access=acl_get(thd->host, thd->ip, thd->priv_user, db, db_is_pattern); else db_access=thd->db_access; DBUG_PRINT("info",("db_access: %lu", db_access)); @@ -4118,6 +4169,20 @@ void mysql_parse(THD *thd, char *inBuf, uint length) send_error(thd, 0, NullS); else { + /* + Binlog logs a string starting from thd->query and having length + thd->query_length; so we set thd->query_length correctly (to not + log several statements in one event, when we executed only first). + We set it to not see the ';' (otherwise it would get into binlog + and Query_log_event::print() would give ';;' output). + This also helps display only the current query in SHOW + PROCESSLIST. + Note that we don't need LOCK_thread_count to modify query_length. + */ + if (lex->found_colon && + (thd->query_length= (ulong)(lex->found_colon - thd->query))) + thd->query_length--; + /* Actually execute the query */ mysql_execute_command(thd); query_cache_end_of_result(thd); } @@ -4338,7 +4403,7 @@ bool add_field_to_list(THD *thd, char *field_name, enum_field_types type, /* The user has given a length to the blob column */ if (new_field->length < 256) type= FIELD_TYPE_TINY_BLOB; - if (new_field->length < 65536) + else if (new_field->length < 65536) type= FIELD_TYPE_BLOB; else if (new_field->length < 256L*256L*256L) type= FIELD_TYPE_MEDIUM_BLOB; @@ -4473,9 +4538,7 @@ bool add_field_to_list(THD *thd, char *field_name, enum_field_types type, net_printf(thd,ER_TOO_BIG_SET,field_name); /* purecov: inspected */ DBUG_RETURN(1); /* purecov: inspected */ } - new_field->pack_length= (interval_list->elements + 7) / 8; - if (new_field->pack_length > 4) - new_field->pack_length=8; + new_field->pack_length= get_set_pack_length(interval_list->elements); List_iterator<String> it(*interval_list); String *tmp; @@ -4492,7 +4555,7 @@ bool add_field_to_list(THD *thd, char *field_name, enum_field_types type, case FIELD_TYPE_ENUM: { // Should be safe - new_field->pack_length= interval_list->elements < 256 ? 1 : 2; + new_field->pack_length= get_enum_pack_length(interval_list->elements); List_iterator<String> it(*interval_list); String *tmp; diff --git a/sql/sql_prepare.cc b/sql/sql_prepare.cc index 1dc46aef4da..b57f88ac172 100644 --- a/sql/sql_prepare.cc +++ b/sql/sql_prepare.cc @@ -1079,7 +1079,7 @@ static int mysql_test_select(Prepared_statement *stmt, thd->used_tables= 0; // Updated by setup_fields // JOIN::prepare calls - if (unit->prepare(thd, 0, 0)) + if (unit->prepare(thd, 0, 0, "")) { send_error(thd); goto err_prep; @@ -1228,7 +1228,7 @@ static int select_like_statement_test(Prepared_statement *stmt, thd->used_tables= 0; // Updated by setup_fields // JOIN::prepare calls - if (lex->unit.prepare(thd, 0, 0)) + if (lex->unit.prepare(thd, 0, 0, "")) { res= thd->net.report_error ? -1 : 1; } diff --git a/sql/sql_repl.cc b/sql/sql_repl.cc index fd165ad1fa5..d02bb5ff0a3 100644 --- a/sql/sql_repl.cc +++ b/sql/sql_repl.cc @@ -246,7 +246,7 @@ bool log_in_use(const char* log_name) if ((linfo = tmp->current_linfo)) { pthread_mutex_lock(&linfo->lock); - result = !memcmp(log_name, linfo->log_file_name, log_name_len); + result = !bcmp(log_name, linfo->log_file_name, log_name_len); pthread_mutex_unlock(&linfo->lock); if (result) break; diff --git a/sql/sql_select.cc b/sql/sql_select.cc index 662e019a525..0d1021ec9af 100644 --- a/sql/sql_select.cc +++ b/sql/sql_select.cc @@ -157,7 +157,7 @@ static bool change_refs_to_tmp_fields(THD *thd, Item **ref_pointer_array, uint elements, List<Item> &items); static void init_tmptable_sum_functions(Item_sum **func); static void update_tmptable_sum_func(Item_sum **func,TABLE *tmp_table); -static void copy_sum_funcs(Item_sum **func_ptr); +static void copy_sum_funcs(Item_sum **func_ptr, Item_sum **end); static bool add_ref_to_table_cond(THD *thd, JOIN_TAB *join_tab); static bool init_sum_functions(Item_sum **func, Item_sum **end); static bool update_sum_func(Item_sum **func); @@ -579,7 +579,7 @@ JOIN::optimize() !(select_options & SELECT_DESCRIBE)) { /* purecov: inspected */ my_message(ER_TOO_BIG_SELECT, ER(ER_TOO_BIG_SELECT), MYF(0)); - error= 1; /* purecov: inspected */ + error= -1; DBUG_RETURN(1); } if (const_tables && !thd->locked_tables && @@ -1083,7 +1083,9 @@ JOIN::exec() else error=(int) result->send_eof(); } - thd->limit_found_rows= thd->examined_row_count= 0; + /* Single select (without union and limit) always returns 1 row */ + thd->limit_found_rows= 1; + thd->examined_row_count= 0; DBUG_VOID_RETURN; } thd->limit_found_rows= thd->examined_row_count= 0; @@ -1267,7 +1269,6 @@ JOIN::exec() { DBUG_VOID_RETURN; } - curr_join->group_list= 0; } thd->proc_info="Copying to group table"; @@ -1287,8 +1288,10 @@ JOIN::exec() } } if (curr_join->make_sum_func_list(*curr_all_fields, *curr_fields_list, - 1) || - (tmp_error= do_select(curr_join, (List<Item> *) 0, curr_tmp_table, + 1)) + DBUG_VOID_RETURN; + curr_join->group_list= 0; + if ((tmp_error= do_select(curr_join, (List<Item> *) 0, curr_tmp_table, 0))) { error= tmp_error; @@ -1326,7 +1329,7 @@ JOIN::exec() if (curr_join->tmp_having) curr_join->tmp_having->update_used_tables(); if (remove_duplicates(curr_join, curr_tmp_table, - curr_join->fields_list, curr_join->tmp_having)) + *curr_fields_list, curr_join->tmp_having)) DBUG_VOID_RETURN; curr_join->tmp_having=0; curr_join->select_distinct=0; @@ -4601,14 +4604,14 @@ const_expression_in_where(COND *cond, Item *comp_item, Item **const_item) create_tmp_field_from_field() thd Thread handler org_field field from which new field will be created + name New field name item Item to create a field for table Temporary table - modify_item 1 if item->result_field should point to new item. - This is relevent for how fill_record() is going to - work: - If modify_item is 1 then fill_record() will update + item !=NULL if item->result_field should point to new field. + This is relevant for how fill_record() is going to work: + If item != NULL then fill_record() will update the record in the original table. - If modify_item is 0 then fill_record() will update + If item == NULL then fill_record() will update the temporary table convert_blob_length If >0 create a varstring(convert_blob_length) field instead of blob. @@ -4619,8 +4622,8 @@ const_expression_in_where(COND *cond, Item *comp_item, Item **const_item) */ static Field* create_tmp_field_from_field(THD *thd, Field* org_field, - Item *item, TABLE *table, - bool modify_item, + const char *name, TABLE *table, + Item_field *item, uint convert_blob_length) { Field *new_field; @@ -4633,10 +4636,10 @@ static Field* create_tmp_field_from_field(THD *thd, Field* org_field, new_field= org_field->new_field(thd->mem_root, table); if (new_field) { - if (modify_item) - ((Item_field *)item)->result_field= new_field; + if (item) + item->result_field= new_field; else - new_field->field_name= item->name; + new_field->field_name= name; if (org_field->maybe_null()) new_field->flags&= ~NOT_NULL_FLAG; // Because of outer join if (org_field->type() == FIELD_TYPE_VAR_STRING) @@ -4771,6 +4774,15 @@ Field *create_tmp_field(THD *thd, TABLE *table,Item *item, Item::Type type, item->name,table,item_sum->decimals); case Item_sum::UNIQUE_USERS_FUNC: return new Field_long(9,maybe_null,item->name,table,1); + case Item_sum::MIN_FUNC: + case Item_sum::MAX_FUNC: + if (item_sum->args[0]->type() == Item::FIELD_ITEM) + { + *from_field= ((Item_field*) item_sum->args[0])->field; + return create_tmp_field_from_field(thd, *from_field, item->name, table, + NULL, convert_blob_length); + } + /* fall through */ default: switch (item_sum->result_type()) { case REAL_RESULT: @@ -4806,8 +4818,10 @@ Field *create_tmp_field(THD *thd, TABLE *table,Item *item, Item::Type type, case Item::DEFAULT_VALUE_ITEM: { Item_field *field= (Item_field*) item; - return create_tmp_field_from_field(thd, (*from_field= field->field), item, - table, modify_item, convert_blob_length); + return create_tmp_field_from_field(thd, (*from_field= field->field), + item->name, table, + modify_item ? (Item_field*) item : NULL, + convert_blob_length); } case Item::FUNC_ITEM: case Item::COND_ITEM: @@ -4825,14 +4839,7 @@ Field *create_tmp_field(THD *thd, TABLE *table,Item *item, Item::Type type, return create_tmp_field_from_item(thd, item, table, copy_func, modify_item, convert_blob_length); case Item::TYPE_HOLDER: - { - Field *example= ((Item_type_holder *)item)->example(); - if (example) - return create_tmp_field_from_field(thd, example, item, table, 0, - convert_blob_length); - return create_tmp_field_from_item(thd, item, table, copy_func, 0, - convert_blob_length); - } + return ((Item_type_holder *)item)->make_field_by_type(table); default: // Dosen't have to be stored return 0; } @@ -5329,8 +5336,6 @@ create_tmp_table(THD *thd,TMP_TABLE_PARAM *param,List<Item> &fields, if (create_myisam_tmp_table(table,param,select_options)) goto err; } - /* Set table_name for easier debugging */ - table->table_name= base_name(tmpname); if (!open_tmp_table(table)) DBUG_RETURN(table); @@ -6729,26 +6734,32 @@ end_write_group(JOIN *join, JOIN_TAB *join_tab __attribute__((unused)), { if (join->procedure) join->procedure->end_group(); - if (idx < (int) join->send_group_parts) + int send_group_parts= join->send_group_parts; + if (idx < send_group_parts) { if (!join->first_record) { /* No matching rows for group function */ join->clear(); } - copy_sum_funcs(join->sum_funcs); - if (!join->having || join->having->val_int()) + copy_sum_funcs(join->sum_funcs, + join->sum_funcs_end[send_group_parts]); + if (join->having && join->having->val_int() == 0) + error= -1; + else if ((error=table->file->write_row(table->record[0]))) { - if ((error=table->file->write_row(table->record[0]))) - { - if (create_myisam_from_heap(join->thd, table, - &join->tmp_table_param, - error, 0)) - DBUG_RETURN(-1); // Not a table_is_full error - } - else - join->send_records++; + if (create_myisam_from_heap(join->thd, table, + &join->tmp_table_param, + error, 0)) + DBUG_RETURN(-1); + } + if (join->rollup.state != ROLLUP::STATE_NONE && error <= 0) + { + if (join->rollup_write_data((uint) (idx+1), table)) + error= 1; } + if (error > 0) + DBUG_RETURN(-1); if (end_of_records) DBUG_RETURN(0); } @@ -7131,7 +7142,7 @@ test_if_skip_sort_order(JOIN_TAB *tab,ORDER *order,ha_rows select_limit, { ref_key= tab->ref.key; ref_key_parts= tab->ref.key_parts; - if (tab->type == JT_REF_OR_NULL) + if (tab->type == JT_REF_OR_NULL || tab->type == JT_FT) DBUG_RETURN(0); } else if (select && select->quick) // Range found by opt_range @@ -7165,7 +7176,20 @@ test_if_skip_sort_order(JOIN_TAB *tab,ORDER *order,ha_rows select_limit, /* Found key that can be used to retrieve data in sorted order */ if (tab->ref.key >= 0) { - tab->ref.key= new_ref_key; + /* + We'll use ref access method on key new_ref_key. In general case + the index search tuple for new_ref_key will be different (e.g. + when one index is defined as (part1, part2, ...) and another as + (part1, part2(N), ...) and the WHERE clause contains + "part1 = const1 AND part2=const2". + So we build tab->ref from scratch here. + */ + KEYUSE *keyuse= tab->keyuse; + while (keyuse->key != new_ref_key && keyuse->table == tab->table) + keyuse++; + if (create_ref_for_key(tab->join, tab, keyuse, + tab->join->const_table_map)) + DBUG_RETURN(0); } else { @@ -7622,8 +7646,8 @@ static int remove_dup_with_hash_index(THD *thd, TABLE *table, { byte *key_buffer, *key_pos, *record=table->record[0]; int error; - handler *file=table->file; - ulong extra_length=ALIGN_SIZE(key_length)-key_length; + handler *file= table->file; + ulong extra_length= ALIGN_SIZE(key_length)-key_length; uint *field_lengths,*field_length; HASH hash; DBUG_ENTER("remove_dup_with_hash_index"); @@ -7637,22 +7661,34 @@ static int remove_dup_with_hash_index(THD *thd, TABLE *table, NullS)) DBUG_RETURN(1); + { + Field **ptr; + ulong total_length= 0; + for (ptr= first_field, field_length=field_lengths ; *ptr ; ptr++) + { + uint length= (*ptr)->pack_length(); + (*field_length++)= length; + total_length+= length; + } + DBUG_PRINT("info",("field_count: %u key_length: %lu total_length: %lu", + field_count, key_length, total_length)); + DBUG_ASSERT(total_length <= key_length); + key_length= total_length; + extra_length= ALIGN_SIZE(key_length)-key_length; + } + if (hash_init(&hash, &my_charset_bin, (uint) file->records, 0, - key_length,(hash_get_key) 0, 0, 0)) + key_length, (hash_get_key) 0, 0, 0)) { my_free((char*) key_buffer,MYF(0)); DBUG_RETURN(1); } - { - Field **ptr; - for (ptr= first_field, field_length=field_lengths ; *ptr ; ptr++) - (*field_length++)= (*ptr)->pack_length(); - } file->ha_rnd_init(1); key_pos=key_buffer; for (;;) { + byte *org_key_pos; if (thd->killed) { my_error(ER_SERVER_SHUTDOWN,MYF(0)); @@ -7675,6 +7711,7 @@ static int remove_dup_with_hash_index(THD *thd, TABLE *table, } /* copy fields to key buffer */ + org_key_pos= key_pos; field_length=field_lengths; for (Field **ptr= first_field ; *ptr ; ptr++) { @@ -7682,14 +7719,14 @@ static int remove_dup_with_hash_index(THD *thd, TABLE *table, key_pos+= *field_length++; } /* Check if it exists before */ - if (hash_search(&hash,key_pos-key_length,key_length)) + if (hash_search(&hash, org_key_pos, key_length)) { /* Duplicated found ; Remove the row */ if ((error=file->delete_row(record))) goto err; } else - (void) my_hash_insert(&hash, key_pos-key_length); + (void) my_hash_insert(&hash, org_key_pos); key_pos+=extra_length; } my_free((char*) key_buffer,MYF(0)); @@ -8614,8 +8651,7 @@ copy_fields(TMP_TABLE_PARAM *param) for (; ptr != end; ptr++) (*ptr->do_copy)(ptr); - List_iterator_fast<Item> &it=param->copy_funcs_it; - it.rewind(); + List_iterator_fast<Item> it(param->copy_funcs); Item_copy_string *item; while ((item = (Item_copy_string*) it++)) item->copy(); @@ -8865,11 +8901,10 @@ update_tmptable_sum_func(Item_sum **func_ptr, /* Copy result of sum functions to record in tmp_table */ static void -copy_sum_funcs(Item_sum **func_ptr) +copy_sum_funcs(Item_sum **func_ptr, Item_sum **end_ptr) { - Item_sum *func; - for (; (func = *func_ptr) ; func_ptr++) - (void) func->save_in_result_field(1); + for (; func_ptr != end_ptr ; func_ptr++) + (void) (*func_ptr)->save_in_result_field(1); return; } @@ -8990,14 +9025,16 @@ bool JOIN::rollup_init() */ tmp_table_param.group_parts= send_group_parts; - if (!(rollup.fields= (List<Item>*) thd->alloc((sizeof(Item*) + - sizeof(List<Item>) + - ref_pointer_array_size) - * send_group_parts))) + if (!(rollup.null_items= (Item_null_result**) thd->alloc((sizeof(Item*) + + sizeof(Item**) + + sizeof(List<Item>) + + ref_pointer_array_size) + * send_group_parts ))) return 1; + + rollup.fields= (List<Item>*) (rollup.null_items + send_group_parts); rollup.ref_pointer_arrays= (Item***) (rollup.fields + send_group_parts); ref_array= (Item**) (rollup.ref_pointer_arrays+send_group_parts); - rollup.item_null= new (thd->mem_root) Item_null(); /* Prepare space for field list for the different levels @@ -9005,12 +9042,16 @@ bool JOIN::rollup_init() */ for (i= 0 ; i < send_group_parts ; i++) { + rollup.null_items[i]= new (thd->mem_root) Item_null_result(); List<Item> *rollup_fields= &rollup.fields[i]; rollup_fields->empty(); rollup.ref_pointer_arrays[i]= ref_array; ref_array+= all_fields.elements; + } + for (i= 0 ; i < send_group_parts; i++) + { for (j=0 ; j < fields_list.elements ; j++) - rollup_fields->push_back(rollup.item_null); + rollup.fields[i].push_back(rollup.null_items[i]); } return 0; } @@ -9114,7 +9155,8 @@ bool JOIN::rollup_make_fields(List<Item> &fields_arg, List<Item> &sel_fields, { /* Check if this is something that is part of this group by */ ORDER *group_tmp; - for (group_tmp= start_group ; group_tmp ; group_tmp= group_tmp->next) + for (group_tmp= start_group, i= pos ; + group_tmp ; group_tmp= group_tmp->next, i++) { if (*group_tmp->item == item) { @@ -9123,7 +9165,9 @@ bool JOIN::rollup_make_fields(List<Item> &fields_arg, List<Item> &sel_fields, set to NULL in this level */ item->maybe_null= 1; // Value will be null sometimes - item= rollup.item_null; + Item_null_result *null_item= rollup.null_items[i]; + null_item->result_field= ((Item_field *) item)->result_field; + item= null_item; break; } } @@ -9172,7 +9216,7 @@ int JOIN::rollup_send_data(uint idx) ref_pointer_array_size); if ((!having || having->val_int())) { - if (send_records < unit->select_limit_cnt && + if (send_records < unit->select_limit_cnt && do_send_rows && result->send_data(rollup.fields[i])) return 1; send_records++; @@ -9184,6 +9228,58 @@ int JOIN::rollup_send_data(uint idx) } /* + Write all rollup levels higher than the current one to a temp table + + SYNOPSIS: + rollup_write_data() + idx Level we are on: + 0 = Total sum level + 1 = First group changed (a) + 2 = Second group changed (a,b) + table reference to temp table + + SAMPLE + SELECT a, b, SUM(c) FROM t1 GROUP BY a,b WITH ROLLUP + + RETURN + 0 ok + 1 if write_data_failed() +*/ + +int JOIN::rollup_write_data(uint idx, TABLE *table) +{ + uint i; + for (i= send_group_parts ; i-- > idx ; ) + { + /* Get reference pointers to sum functions in place */ + memcpy((char*) ref_pointer_array, + (char*) rollup.ref_pointer_arrays[i], + ref_pointer_array_size); + if ((!having || having->val_int())) + { + int error; + Item *item; + List_iterator_fast<Item> it(rollup.fields[i]); + while ((item= it++)) + { + if (item->type() == Item::NULL_ITEM && item->is_result_field()) + item->save_in_result_field(1); + } + copy_sum_funcs(sum_funcs_end[i+1], sum_funcs_end[i]); + if ((error= table->file->write_row(table->record[0]))) + { + if (create_myisam_from_heap(thd, table, &tmp_table_param, + error, 0)) + return 1; + } + } + } + /* Restore ref_pointer_array */ + set_items_ref_array(current_ref_pointer_array); + return 0; +} + +/* clear results if there are not rows found for group (end_send_group/end_write_group) @@ -9502,7 +9598,8 @@ int mysql_explain_union(THD *thd, SELECT_LEX_UNIT *unit, select_result *result) unit->fake_select_lex->select_number= UINT_MAX; // jost for initialization unit->fake_select_lex->type= "UNION RESULT"; unit->fake_select_lex->options|= SELECT_DESCRIBE; - if (!(res= unit->prepare(thd, result, SELECT_NO_UNLOCK | SELECT_DESCRIBE))) + if (!(res= unit->prepare(thd, result, SELECT_NO_UNLOCK | SELECT_DESCRIBE, + ""))) res= unit->exec(); res|= unit->cleanup(); } diff --git a/sql/sql_select.h b/sql/sql_select.h index bbd169d1850..4ea7e1b23e7 100644 --- a/sql/sql_select.h +++ b/sql/sql_select.h @@ -124,7 +124,7 @@ typedef struct st_rollup { enum State { STATE_NONE, STATE_INITED, STATE_READY }; State state; - Item *item_null; + Item_null_result **null_items; Item ***ref_pointer_arrays; List<Item> *fields; } ROLLUP; @@ -295,6 +295,7 @@ class JOIN :public Sql_alloc bool rollup_make_fields(List<Item> &all_fields, List<Item> &fields, Item_sum ***func); int rollup_send_data(uint idx); + int rollup_write_data(uint idx, TABLE *table); bool test_in_subselect(Item **where); void join_free(bool full); void clear(); diff --git a/sql/sql_show.cc b/sql/sql_show.cc index 8d741b4dc67..f979b3ca771 100644 --- a/sql/sql_show.cc +++ b/sql/sql_show.cc @@ -1227,7 +1227,15 @@ static void append_directory(THD *thd, String *packet, const char *dir_type, packet->append(' '); packet->append(dir_type); packet->append(" DIRECTORY='", 12); +#ifdef __WIN__ + char *winfilename = thd->memdup(filename, length); + for (uint i=0; i < length; i++) + if (winfilename[i] == '\\') + winfilename[i] = '/'; + packet->append(winfilename, length); +#else packet->append(filename, length); +#endif packet->append('\''); } } @@ -1879,6 +1887,19 @@ int mysqld_show(THD *thd, const char *wild, show_var_st *variables, pthread_mutex_unlock(&LOCK_active_mi); break; } + case SHOW_SLAVE_RETRIED_TRANS: + { + /* + TODO: in 5.1 with multimaster, have one such counter per line in SHOW + SLAVE STATUS, and have the sum over all lines here. + */ + pthread_mutex_lock(&LOCK_active_mi); + pthread_mutex_lock(&active_mi->rli.data_lock); + end= int10_to_str(active_mi->rli.retried_trans, buff, 10); + pthread_mutex_unlock(&active_mi->rli.data_lock); + pthread_mutex_unlock(&LOCK_active_mi); + break; + } #endif /* HAVE_REPLICATION */ case SHOW_OPENTABLES: end= int10_to_str((long) cached_tables(), buff, 10); diff --git a/sql/sql_string.h b/sql/sql_string.h index 9136dddbbf2..8dff5558120 100644 --- a/sql/sql_string.h +++ b/sql/sql_string.h @@ -73,7 +73,7 @@ public: { return (void*) alloc_root(mem_root, (uint) size); } static void operator delete(void *ptr_arg,size_t size) {} - static void operator delete(void *ptr_arg,size_t size, MEM_ROOT *mem_root) + static void operator delete(void *ptr_arg, MEM_ROOT *mem_root) {} ~String() { free(); } @@ -177,6 +177,10 @@ public: } } } + inline void shrink_to_length() + { + Alloced_length= str_length; + } bool is_alloced() { return alloced; } inline String& operator = (const String &s) { diff --git a/sql/sql_table.cc b/sql/sql_table.cc index 5f3875ba934..fffef49effc 100644 --- a/sql/sql_table.cc +++ b/sql/sql_table.cc @@ -1971,7 +1971,9 @@ static int mysql_admin_table(THD* thd, TABLE_LIST* tables, thd->exit_cond(old_message); if (thd->killed) goto err; - open_for_modify=0; + /* Flush entries in the query cache involving this table. */ + query_cache_invalidate3(thd, table->table, 0); + open_for_modify= 0; } int result_code = (table->table->file->*operator_func)(thd, check_opt); diff --git a/sql/sql_udf.cc b/sql/sql_udf.cc index 0bb8ac8a28b..05df069d69a 100644 --- a/sql/sql_udf.cc +++ b/sql/sql_udf.cc @@ -74,32 +74,49 @@ static HASH udf_hash; static rw_lock_t THR_LOCK_udf; -static udf_func *add_udf(LEX_STRING *name, Item_result ret, char *dl, - Item_udftype typ); +static udf_func *add_udf(LEX_STRING *name, Item_result ret, + char *dl, Item_udftype typ); static void del_udf(udf_func *udf); static void *find_udf_dl(const char *dl); - -static void init_syms(udf_func *tmp) +static char *init_syms(udf_func *tmp, char *nm) { - char nm[MAX_FIELD_NAME+16],*end; + char *end; + + if (!((tmp->func= dlsym(tmp->dlhandle, tmp->name.str)))) + return tmp->name.str; - tmp->func = dlsym(tmp->dlhandle, tmp->name.str); end=strmov(nm,tmp->name.str); - (void) strmov(end,"_init"); - tmp->func_init = dlsym(tmp->dlhandle, nm); - (void) strmov(end,"_deinit"); - tmp->func_deinit = dlsym(tmp->dlhandle, nm); + if (tmp->type == UDFTYPE_AGGREGATE) { - (void)strmov( end, "_clear" ); - tmp->func_clear = dlsym( tmp->dlhandle, nm ); - (void)strmov( end, "_add" ); - tmp->func_add = dlsym( tmp->dlhandle, nm ); - /* Give error if _clear and _add doesn't exists */ - if (!tmp->func_clear || ! tmp->func_add) - tmp->func= 0; + (void)strmov(end, "_clear"); + if (!((tmp->func_clear= dlsym(tmp->dlhandle, nm)))) + return nm; + (void)strmov(end, "_add"); + if (!((tmp->func_add= dlsym(tmp->dlhandle, nm)))) + return nm; + } + + (void) strmov(end,"_deinit"); + tmp->func_deinit= dlsym(tmp->dlhandle, nm); + + (void) strmov(end,"_init"); + tmp->func_init= dlsym(tmp->dlhandle, nm); + + /* + to prefent loading "udf" from, e.g. libc.so + let's ensure that at least one auxiliary symbol is defined + */ + if (!tmp->func_init && !tmp->func_deinit && tmp->type != UDFTYPE_AGGREGATE) + { + if (opt_allow_suspicious_udfs) + sql_print_error(ER(ER_CANT_FIND_DL_ENTRY), nm); + else + return nm; } + + return 0; } extern "C" byte* get_hash_key(const byte *buff,uint *length, @@ -111,7 +128,7 @@ extern "C" byte* get_hash_key(const byte *buff,uint *length, } /* -** Read all predeclared functions from func@mysql and accept all that +** Read all predeclared functions from mysql.func and accept all that ** can be used. */ @@ -153,7 +170,7 @@ void udf_init() if (simple_open_n_lock_tables(new_thd, &tables)) { DBUG_PRINT("error",("Can't open udf table")); - sql_print_error("Can't open the mysql/func table. Please run the mysql_install_db script to create it."); + sql_print_error("Can't open the mysql.func table. Please run the mysql_install_db script to create it."); goto end; } @@ -171,10 +188,23 @@ void udf_init() if (table->fields >= 4) // New func table udftype=(Item_udftype) table->field[3]->val_int(); - if (!(tmp = add_udf(&name,(Item_result) table->field[1]->val_int(), - dl_name, udftype))) + /* + Ensure that the .dll doesn't have a path + This is done to ensure that only approved dll from the system + directories are used (to make this even remotely secure). + */ + if (strchr(dl_name, '/') || name.length > NAME_LEN) + { + sql_print_error("Invalid row in mysql.func table for function '%.64s'", + name.str); + continue; + } + + + if (!(tmp= add_udf(&name,(Item_result) table->field[1]->val_int(), + dl_name, udftype))) { - sql_print_error("Can't alloc memory for udf function: name"); + sql_print_error("Can't alloc memory for udf function: '%.64s'", name.str); continue; } @@ -191,13 +221,15 @@ void udf_init() new_dl=1; } tmp->dlhandle = dl; - init_syms(tmp); - if (!tmp->func) { - sql_print_error(ER(ER_CANT_FIND_DL_ENTRY), name); - del_udf(tmp); - if (new_dl) - dlclose(dl); + char buf[MAX_FIELD_NAME+16], *missing; + if ((missing= init_syms(tmp, buf))) + { + sql_print_error(ER(ER_CANT_FIND_DL_ENTRY), missing); + del_udf(tmp); + if (new_dl) + dlclose(dl); + } } } if (error > 0) @@ -239,7 +271,7 @@ void udf_free() { initialized= 0; rwlock_destroy(&THR_LOCK_udf); - } + } DBUG_VOID_RETURN; } @@ -406,12 +438,13 @@ int mysql_create_function(THD *thd,udf_func *udf) new_dl=1; } udf->dlhandle=dl; - init_syms(udf); - - if (udf->func == NULL) { - net_printf(thd, ER_CANT_FIND_DL_ENTRY, udf->name); - goto err; + char buf[MAX_FIELD_NAME+16], *missing; + if ((missing= init_syms(udf, buf))) + { + net_printf(thd, ER_CANT_FIND_DL_ENTRY, missing); + goto err; + } } udf->name.str=strdup_root(&mem,udf->name.str); udf->dl=strdup_root(&mem,udf->dl); @@ -427,7 +460,7 @@ int mysql_create_function(THD *thd,udf_func *udf) u_d->func_clear=udf->func_clear; u_d->func_add=udf->func_add; - /* create entry in mysql/func table */ + /* create entry in mysql.func table */ bzero((char*) &tables,sizeof(tables)); tables.db= (char*) "mysql"; @@ -447,7 +480,7 @@ int mysql_create_function(THD *thd,udf_func *udf) close_thread_tables(thd); if (error) { - net_printf(thd, ER_ERROR_ON_WRITE, "func@mysql",error); + net_printf(thd, ER_ERROR_ON_WRITE, "mysql.func",error); del_udf(u_d); goto err; } diff --git a/sql/sql_union.cc b/sql/sql_union.cc index 882316d57d7..a54fb613fd2 100644 --- a/sql/sql_union.cc +++ b/sql/sql_union.cc @@ -29,7 +29,7 @@ int mysql_union(THD *thd, LEX *lex, select_result *result, { DBUG_ENTER("mysql_union"); int res= 0; - if (!(res= unit->prepare(thd, result, SELECT_NO_UNLOCK))) + if (!(res= unit->prepare(thd, result, SELECT_NO_UNLOCK, ""))) res= unit->exec(); res|= unit->cleanup(); DBUG_RETURN(res); @@ -142,7 +142,8 @@ st_select_lex_unit::init_prepare_fake_select_lex(THD *thd) int st_select_lex_unit::prepare(THD *thd_arg, select_result *sel_result, - ulong additional_options) + ulong additional_options, + const char *tmp_table_alias) { SELECT_LEX *lex_select_save= thd_arg->lex->current_select; SELECT_LEX *sl, *first_select; @@ -215,8 +216,6 @@ int st_select_lex_unit::prepare(THD *thd_arg, select_result *sel_result, select_limit_cnt= sl->select_limit+sl->offset_limit; if (select_limit_cnt < sl->select_limit) select_limit_cnt= HA_POS_ERROR; // no limit - if (select_limit_cnt == HA_POS_ERROR || sl->braces) - sl->options&= ~OPTION_FOUND_ROWS; can_skip_order_by= is_union && (!sl->braces || select_limit_cnt == HA_POS_ERROR); @@ -254,7 +253,7 @@ int st_select_lex_unit::prepare(THD *thd_arg, select_result *sel_result, while ((item_tmp= it++)) { /* Error's in 'new' will be detected after loop */ - types.push_back(new Item_type_holder(thd_arg, item_tmp, empty_table)); + types.push_back(new Item_type_holder(thd_arg, item_tmp)); } if (thd_arg->is_fatal_error) @@ -273,8 +272,7 @@ int st_select_lex_unit::prepare(THD *thd_arg, select_result *sel_result, Item *type, *item_tmp; while ((type= tp++, item_tmp= it++)) { - if (((Item_type_holder*)type)->join_types(thd_arg, item_tmp, - empty_table)) + if (((Item_type_holder*)type)->join_types(thd_arg, item_tmp)) DBUG_RETURN(-1); } } @@ -306,7 +304,7 @@ int st_select_lex_unit::prepare(THD *thd_arg, select_result *sel_result, (first_select_in_union()->options | thd_arg->options | TMP_TABLE_ALL_COLUMNS), - HA_POS_ERROR, (char*) ""))) + HA_POS_ERROR, (char *) tmp_table_alias))) goto err; table->file->extra(HA_EXTRA_WRITE_CACHE); table->file->extra(HA_EXTRA_IGNORE_DUP_KEY); @@ -342,7 +340,7 @@ int st_select_lex_unit::prepare(THD *thd_arg, select_result *sel_result, if (arena->is_stmt_prepare()) { /* prepare fake select to initialize it correctly */ - ulong options_tmp= init_prepare_fake_select_lex(thd); + (void) init_prepare_fake_select_lex(thd); if (!(fake_select_lex->join= new JOIN(thd, item_list, thd->options, result))) { @@ -416,7 +414,7 @@ int st_select_lex_unit::exec() } /* re-enabling indexes for next subselect iteration */ if (union_distinct && table->file->enable_indexes(HA_KEY_SWITCH_ALL)) - DBUG_ASSERT(1); + DBUG_ASSERT(0); } for (SELECT_LEX *sl= select_cursor; sl; sl= sl->next_select()) { @@ -447,21 +445,14 @@ int st_select_lex_unit::exec() if (select_limit_cnt < sl->select_limit) select_limit_cnt= HA_POS_ERROR; // no limit - /* - When using braces, SQL_CALC_FOUND_ROWS affects the whole query. - We don't calculate found_rows() per union part - */ - if (select_limit_cnt == HA_POS_ERROR || sl->braces) - sl->options&= ~OPTION_FOUND_ROWS; - else - { - /* - We are doing an union without braces. In this case - SQL_CALC_FOUND_ROWS should be done on all sub parts - */ - sl->options|= found_rows_for_union; - } - sl->join->select_options=sl->options; + /* + When using braces, SQL_CALC_FOUND_ROWS affects the whole query: + we don't calculate found_rows() per union part. + Otherwise, SQL_CALC_FOUND_ROWS should be done on all sub parts. + */ + sl->join->select_options= + (select_limit_cnt == HA_POS_ERROR || sl->braces) ? + sl->options & ~OPTION_FOUND_ROWS : sl->options | found_rows_for_union; res= sl->join->optimize(); } if (!res) @@ -493,7 +484,8 @@ int st_select_lex_unit::exec() } /* Needed for the following test and for records_at_start in next loop */ table->file->info(HA_STATUS_VARIABLE); - if (found_rows_for_union & sl->options) + if (found_rows_for_union && !sl->braces && + select_limit_cnt != HA_POS_ERROR) { /* This is a union without braces. Remember the number of rows that diff --git a/sql/sql_update.cc b/sql/sql_update.cc index 663f2d2be34..6c12381b4bd 100644 --- a/sql/sql_update.cc +++ b/sql/sql_update.cc @@ -574,7 +574,10 @@ int mysql_multi_update_lock(THD *thd, else { DBUG_PRINT("info",("setting table `%s` for read-only", tl->alias)); - tl->lock_type= TL_READ; + // If we are using the binary log, we need TL_READ_NO_INSERT to get + // correct order of statements. Otherwise, we use a TL_READ lock to + // improve performance. + tl->lock_type= using_update_log ? TL_READ_NO_INSERT : TL_READ; tl->updating= 0; wants= SELECT_ACL; } diff --git a/sql/sql_yacc.yy b/sql/sql_yacc.yy index e70efe14557..9f25e17b6fd 100644 --- a/sql/sql_yacc.yy +++ b/sql/sql_yacc.yy @@ -1111,7 +1111,7 @@ create_select: SELECT_SYM { LEX *lex=Lex; - lex->lock_option= (using_update_log) ? TL_READ_NO_INSERT : TL_READ; + lex->lock_option= using_update_log ? TL_READ_NO_INSERT : TL_READ; if (lex->sql_command == SQLCOM_INSERT) lex->sql_command= SQLCOM_INSERT_SELECT; else if (lex->sql_command == SQLCOM_REPLACE) @@ -2389,7 +2389,10 @@ select: select_init: SELECT_SYM select_init2 | - '(' SELECT_SYM select_part2 ')' + '(' select_paren ')' union_opt; + +select_paren: + SELECT_SYM select_part2 { LEX *lex= Lex; SELECT_LEX * sel= lex->current_select; @@ -2408,7 +2411,8 @@ select_init: if (sel->master_unit()->fake_select_lex) sel->master_unit()->global_parameters= sel->master_unit()->fake_select_lex; - } union_opt; + } + | '(' select_paren ')'; select_init2: select_part2 @@ -3404,8 +3408,7 @@ when_list2: }; join_table_list: - '(' join_table_list ')' { $$=$2; } - | join_table { $$=$1; } + join_table { $$=$1; } | join_table_list ',' join_table_list { $$=$3; } | join_table_list normal_join join_table_list { $$=$3; } | join_table_list STRAIGHT_JOIN join_table_list @@ -3482,7 +3485,7 @@ join_table: } | '{' ident join_table LEFT OUTER JOIN_SYM join_table ON expr '}' { add_join_on($7,$9); $7->outer_join|=JOIN_TYPE_LEFT; $$=$7; } - | '(' SELECT_SYM select_derived ')' opt_table_alias + | '(' select_derived union_opt ')' opt_table_alias { LEX *lex=Lex; SELECT_LEX_UNIT *unit= lex->current_select->master_unit(); @@ -3493,9 +3496,27 @@ join_table: (List<String> *)0))) YYABORT; - }; + } + | '(' join_table_list ')' { $$=$2; }; select_derived: + SELECT_SYM select_derived2 + | '(' select_derived ')' + { + LEX *lex= Lex; + SELECT_LEX * sel= lex->current_select; + if (sel->set_braces(1)) + { + yyerror(ER(ER_SYNTAX_ERROR)); + YYABORT; + } + /* select in braces, can't contain global parameters */ + if (sel->master_unit()->fake_select_lex) + sel->master_unit()->global_parameters= + sel->master_unit()->fake_select_lex; + }; + +select_derived2: { LEX *lex= Lex; lex->derived_tables= 1; @@ -3517,7 +3538,7 @@ select_derived: { Select->parsing_place= NO_MATTER; } - opt_select_from union_opt + opt_select_from ; opt_outer: diff --git a/sql/structs.h b/sql/structs.h index 846b3400fab..ba081b570c1 100644 --- a/sql/structs.h +++ b/sql/structs.h @@ -180,7 +180,7 @@ enum SHOW_TYPE SHOW_SSL_CTX_SESS_TIMEOUTS, SHOW_SSL_CTX_SESS_CACHE_FULL, SHOW_SSL_GET_CIPHER_LIST, #endif /* HAVE_OPENSSL */ - SHOW_RPL_STATUS, SHOW_SLAVE_RUNNING, + SHOW_RPL_STATUS, SHOW_SLAVE_RUNNING, SHOW_SLAVE_RETRIED_TRANS, SHOW_KEY_CACHE_LONG, SHOW_KEY_CACHE_CONST_LONG }; diff --git a/sql/table.cc b/sql/table.cc index 6427755dab4..cdcd5148787 100644 --- a/sql/table.cc +++ b/sql/table.cc @@ -195,7 +195,6 @@ int openfrm(const char *name, const char *alias, uint db_stat, uint prgflag, goto err_not_open; /* purecov: inspected */ bzero((char*) keyinfo,n_length); outparam->key_info=keyinfo; - outparam->max_key_length= outparam->total_key_length= 0; key_part= my_reinterpret_cast(KEY_PART_INFO*) (keyinfo+keys); strpos=disk_buff+6; @@ -251,11 +250,6 @@ int openfrm(const char *name, const char *alias, uint db_stat, uint prgflag, } key_part->store_length=key_part->length; } - set_if_bigger(outparam->max_key_length,keyinfo->key_length+ - keyinfo->key_parts); - outparam->total_key_length+= keyinfo->key_length; - if (keyinfo->flags & HA_NOSAME) - set_if_bigger(outparam->max_unique_length,keyinfo->key_length); } keynames=(char*) key_part; strpos+= (strmov(keynames, (char *) strpos) - keynames)+1; @@ -685,6 +679,12 @@ int openfrm(const char *name, const char *alias, uint db_stat, uint prgflag, } } keyinfo->usable_key_parts=usable_parts; // Filesort + + set_if_bigger(outparam->max_key_length,keyinfo->key_length+ + keyinfo->key_parts); + outparam->total_key_length+= keyinfo->key_length; + if (keyinfo->flags & HA_NOSAME) + set_if_bigger(outparam->max_unique_length,keyinfo->key_length); } if (primary_key < MAX_KEY && (outparam->keys_in_use.is_set(primary_key))) @@ -1229,6 +1229,10 @@ File create_frm(register my_string name, uint reclength, uchar *fileinfo, uint key_length; ulong length; char fill[IO_SIZE]; + int create_flags= O_RDWR | O_TRUNC; + + if (create_info->options & HA_LEX_CREATE_TMP_TABLE) + create_flags|= O_EXCL | O_NOFOLLOW; #if SIZEOF_OFF_T > 4 /* Fix this when we have new .frm files; Current limit is 4G rows (QQ) */ @@ -1243,7 +1247,7 @@ File create_frm(register my_string name, uint reclength, uchar *fileinfo, */ set_if_smaller(create_info->raid_chunks, 255); - if ((file=my_create(name,CREATE_MODE,O_RDWR | O_TRUNC,MYF(MY_WME))) >= 0) + if ((file= my_create(name, CREATE_MODE, create_flags, MYF(MY_WME))) >= 0) { bzero((char*) fileinfo,64); fileinfo[0]=(uchar) 254; fileinfo[1]= 1; fileinfo[2]= FRM_VER+3; // Header diff --git a/sql/table.h b/sql/table.h index eed9969dac8..054f24267b7 100644 --- a/sql/table.h +++ b/sql/table.h @@ -90,7 +90,9 @@ struct st_table { uint null_fields; /* number of null fields */ uint blob_fields; /* number of blob fields */ key_map keys_in_use, keys_for_keyread, read_only_keys; - key_map quick_keys, used_keys, keys_in_use_for_query; + key_map quick_keys; + key_map used_keys; /* keys that cover all used table fields */ + key_map keys_in_use_for_query; KEY *key_info; /* data of keys in database */ TYPELIB keynames; /* Pointers to keynames */ ha_rows max_rows; /* create information */ |