19 #include "my_global.h"
23 #include "frm_crypt.h"
25 #include "sql_table.h"
27 #include "sql_trigger.h"
28 #include "sql_parse.h"
30 #include "sql_partition.h"
34 #include "sql_derived.h"
41 #include "table_cache.h"
45 LEX_STRING INFORMATION_SCHEMA_NAME= {C_STRING_WITH_LEN(
"information_schema")};
48 LEX_STRING PERFORMANCE_SCHEMA_DB_NAME= {C_STRING_WITH_LEN(
"performance_schema")};
51 LEX_STRING MYSQL_SCHEMA_NAME= {C_STRING_WITH_LEN(
"mysql")};
54 LEX_STRING GENERAL_LOG_NAME= {C_STRING_WITH_LEN(
"general_log")};
57 LEX_STRING SLOW_LOG_NAME= {C_STRING_WITH_LEN(
"slow_log")};
60 LEX_STRING RLI_INFO_NAME= {C_STRING_WITH_LEN(
"slave_relay_log_info")};
63 LEX_STRING MI_INFO_NAME= {C_STRING_WITH_LEN(
"slave_master_info")};
66 LEX_STRING WORKER_INFO_NAME= {C_STRING_WITH_LEN(
"slave_worker_info")};
70 void open_table_error(
TABLE_SHARE *share,
int error,
int db_errno,
71 myf errortype,
int errarg);
72 static int open_binary_frm(THD *thd,
TABLE_SHARE *share,
73 uchar *head, File
file);
74 static void fix_type_pointers(
const char ***array,
TYPELIB *point_to_type,
75 uint types,
char **names);
76 static uint find_field(
Field **fields, uchar *
record, uint start, uint length);
82 inline bool is_system_table_name(
const char *
name, uint length);
84 static ulong get_form_pos(File
file, uchar *head);
93 DBUG_ENTER(
"Object_creation_ctx::set_n_backup");
95 backup_ctx= create_backup_ctx(thd);
98 DBUG_RETURN(backup_ctx);
106 backup_ctx->change_env(thd);
115 Default_object_creation_ctx::Default_object_creation_ctx(THD *thd)
116 : m_client_cs(thd->variables.character_set_client),
117 m_connection_cl(thd->variables.collation_connection)
120 Default_object_creation_ctx::Default_object_creation_ctx(
122 : m_client_cs(client_cs),
123 m_connection_cl(connection_cl)
127 Default_object_creation_ctx::create_backup_ctx(THD *thd)
const
132 void Default_object_creation_ctx::change_env(THD *thd)
const
137 thd->update_charset();
160 if (!view->view_client_cs_name.str ||
161 !view->view_connection_cl_name.str)
163 push_warning_printf(thd, Sql_condition::WARN_LEVEL_NOTE,
164 ER_VIEW_NO_CREATION_CTX,
165 ER(ER_VIEW_NO_CREATION_CTX),
166 (
const char *) view->db,
167 (
const char *) view->table_name);
177 bool invalid_creation_ctx;
179 invalid_creation_ctx= resolve_charset(view->view_client_cs_name.str,
183 invalid_creation_ctx= resolve_collation(view->view_connection_cl_name.str,
186 invalid_creation_ctx;
188 if (invalid_creation_ctx)
190 sql_print_warning(
"View '%s'.'%s': there is unknown charset/collation "
191 "names (client: '%s'; connection: '%s').",
192 (
const char *) view->db,
193 (
const char *) view->table_name,
194 (
const char *) view->view_client_cs_name.str,
195 (
const char *) view->view_connection_cl_name.str);
197 push_warning_printf(thd, Sql_condition::WARN_LEVEL_NOTE,
198 ER_VIEW_INVALID_CREATION_CTX,
199 ER(ER_VIEW_INVALID_CREATION_CTX),
200 (
const char *) view->db,
201 (
const char *) view->table_name);
211 static uchar *get_field_name(
Field **buff,
size_t *length,
212 my_bool not_used __attribute__((unused)))
214 *length= (uint) strlen((*buff)->field_name);
215 return (uchar*) (*buff)->field_name;
240 char *fn_rext(
char *
name)
242 char *res= strrchr(name,
'.');
243 if (res && !strcmp(res, reg_ext))
245 return name + strlen(name);
250 DBUG_ASSERT(db != NULL);
251 DBUG_ASSERT(name != NULL);
253 if (is_infoschema_db(db->str, db->length))
254 return TABLE_CATEGORY_INFORMATION;
256 if ((db->length == PERFORMANCE_SCHEMA_DB_NAME.length) &&
257 (my_strcasecmp(system_charset_info,
258 PERFORMANCE_SCHEMA_DB_NAME.str,
260 return TABLE_CATEGORY_PERFORMANCE;
262 if ((db->length == MYSQL_SCHEMA_NAME.length) &&
263 (my_strcasecmp(system_charset_info,
264 MYSQL_SCHEMA_NAME.str,
267 if (is_system_table_name(name->str, name->length))
268 return TABLE_CATEGORY_SYSTEM;
270 if ((name->length == GENERAL_LOG_NAME.length) &&
271 (my_strcasecmp(system_charset_info,
272 GENERAL_LOG_NAME.str,
274 return TABLE_CATEGORY_LOG;
276 if ((name->length == SLOW_LOG_NAME.length) &&
277 (my_strcasecmp(system_charset_info,
280 return TABLE_CATEGORY_LOG;
282 if ((name->length == RLI_INFO_NAME.length) &&
283 (my_strcasecmp(system_charset_info,
286 return TABLE_CATEGORY_RPL_INFO;
288 if ((name->length == MI_INFO_NAME.length) &&
289 (my_strcasecmp(system_charset_info,
292 return TABLE_CATEGORY_RPL_INFO;
294 if ((name->length == WORKER_INFO_NAME.length) &&
295 (my_strcasecmp(system_charset_info,
296 WORKER_INFO_NAME.str,
298 return TABLE_CATEGORY_RPL_INFO;
301 return TABLE_CATEGORY_USER;
324 char *key_buff, *path_buff;
325 char path[FN_REFLEN];
328 DBUG_ENTER(
"alloc_table_share");
329 DBUG_PRINT(
"enter", (
"table: '%s'.'%s'",
330 table_list->db, table_list->table_name));
332 path_length= build_table_filename(path,
sizeof(path) - 1,
334 table_list->table_name,
"", 0);
335 init_sql_alloc(&mem_root, TABLE_ALLOC_BLOCK_SIZE, 0);
336 if (multi_alloc_root(&mem_root,
337 &share,
sizeof(*share),
338 &key_buff, key_length,
339 &path_buff, path_length + 1,
340 &cache_element_array,
341 table_cache_instances *
sizeof(*cache_element_array),
344 memset(share, 0,
sizeof(*share));
346 share->set_table_cache_key(key_buff, key, key_length);
348 share->path.str= path_buff;
349 share->path.length= path_length;
350 strmov(share->path.str, path);
351 share->normalized_path.str= share->path.str;
352 share->normalized_path.length= path_length;
354 share->version= refresh_version;
363 share->table_map_id= ~0ULL;
364 share->cached_row_logging_check= -1;
368 memset(cache_element_array, 0,
369 table_cache_instances *
sizeof(*cache_element_array));
372 memcpy((
char*) &share->mem_root, (
char*) &mem_root,
sizeof(mem_root));
374 &share->LOCK_ha_data, MY_MUTEX_INIT_FAST);
403 void init_tmp_table_share(THD *thd,
TABLE_SHARE *share,
const char *key,
407 DBUG_ENTER(
"init_tmp_table_share");
408 DBUG_PRINT(
"enter", (
"table: '%s'.'%s'", key, table_name));
410 memset(share, 0,
sizeof(*share));
411 init_sql_alloc(&share->mem_root, TABLE_ALLOC_BLOCK_SIZE, 0);
413 share->tmp_table= INTERNAL_TMP_TABLE;
414 share->db.str= (
char*) key;
415 share->db.length= strlen(key);
416 share->table_cache_key.str= (
char*) key;
417 share->table_cache_key.length= key_length;
418 share->table_name.str= (
char*) table_name;
419 share->table_name.length= strlen(table_name);
420 share->path.str= (
char*) path;
421 share->normalized_path.str= (
char*) path;
422 share->path.length= share->normalized_path.length= strlen(path);
423 share->frm_version= FRM_VER_TRUE_VARCHAR;
425 share->cached_row_logging_check= -1;
431 share->table_map_id= (ulonglong) thd->query_id;
450 DBUG_ENTER(
"TABLE_SHARE::destroy");
451 DBUG_PRINT(
"info", (
"db: %s table: %s", db.str, table_name.str));
458 if (tmp_table == NO_TMP_TABLE)
460 my_hash_free(&name_hash);
462 plugin_unlock(NULL, db_plugin);
467 for (idx= keys; idx; idx--, info_it++)
469 if (info_it->
flags & HA_USES_PARSER)
471 plugin_unlock(NULL, info_it->
parser);
476 #ifdef HAVE_PSI_TABLE_INTERFACE
485 free_root(&own_root, MYF(0));
499 DBUG_ENTER(
"free_table_share");
500 DBUG_PRINT(
"enter", (
"table: %s.%s", share->db.str, share->table_name.str));
501 DBUG_ASSERT(share->ref_count == 0);
522 while ((ticket= it++))
550 inline bool is_system_table_name(
const char *name, uint length)
557 my_tolower(ci, name[0]) ==
'p' &&
558 my_tolower(ci, name[1]) ==
'r' &&
559 my_tolower(ci, name[2]) ==
'o' &&
560 my_tolower(ci, name[3]) ==
'c') ||
565 (my_tolower(ci, name[0]) ==
'h' &&
566 my_tolower(ci, name[1]) ==
'e' &&
567 my_tolower(ci, name[2]) ==
'l' &&
568 my_tolower(ci, name[3]) ==
'p') ||
571 (my_tolower(ci, name[0]) ==
't' &&
572 my_tolower(ci, name[1]) ==
'i' &&
573 my_tolower(ci, name[2]) ==
'm' &&
574 my_tolower(ci, name[3]) ==
'e') ||
577 (my_tolower(ci, name[0]) ==
'e' &&
578 my_tolower(ci, name[1]) ==
'v' &&
579 my_tolower(ci, name[2]) ==
'e' &&
580 my_tolower(ci, name[3]) ==
'n' &&
581 my_tolower(ci, name[4]) ==
't')
592 static inline bool has_disabled_path_chars(
const char *str)
635 int open_table_def(THD *thd,
TABLE_SHARE *share, uint db_flags)
637 int error, table_type;
641 char path[FN_REFLEN];
643 DBUG_ENTER(
"open_table_def");
644 DBUG_PRINT(
"enter", (
"table: '%s'.'%s' path: '%s'", share->db.str,
645 share->table_name.str, share->normalized_path.str));
650 strxmov(path, share->normalized_path.str, reg_ext, NullS);
652 path, O_RDONLY | O_SHARE, MYF(0))) < 0)
665 if (has_disabled_path_chars(share->table_name.str) ||
666 has_disabled_path_chars(share->db.str) ||
667 !strncmp(share->db.str, MYSQL50_TABLE_NAME_PREFIX,
668 MYSQL50_TABLE_NAME_PREFIX_LENGTH) ||
669 !strncmp(share->table_name.str, MYSQL50_TABLE_NAME_PREFIX,
670 MYSQL50_TABLE_NAME_PREFIX_LENGTH))
675 strxnmov(path,
sizeof(path)-1,
676 mysql_data_home,
"/", share->db.str,
"/",
677 share->table_name.str, reg_ext, NullS);
678 length= unpack_filename(path, path) - reg_ext_length;
683 DBUG_ASSERT(length <= share->normalized_path.length);
689 if (length == share->normalized_path.length ||
691 path, O_RDONLY | O_SHARE, MYF(0))) < 0))
696 strmov(share->normalized_path.str, path);
697 share->normalized_path.length= length;
704 if (head[0] == (uchar) 254 && head[1] == 1)
706 if (head[2] == FRM_VER || head[2] == FRM_VER+1 ||
707 (head[2] >= FRM_VER+3 && head[2] <= FRM_VER+4))
710 if (db_flags & OPEN_VIEW_ONLY)
723 else if (memcmp(head, STRING_WITH_LEN(
"TYPE=")) == 0)
726 if (memcmp(head+5,
"VIEW", 4) == 0)
729 if (db_flags & OPEN_VIEW)
742 root_ptr= my_pthread_getspecific_ptr(
MEM_ROOT**, THR_MALLOC);
744 *root_ptr= &share->mem_root;
745 error= open_binary_frm(thd, share, head, file);
749 else if (table_type == 2)
760 else if (!is_equal(&view_type, share->
view_def->type()))
766 share->
table_category= get_table_category(& share->db, & share->table_name);
769 thd->status_var.opened_shares++;
775 if (error && !error_given)
778 open_table_error(share, error, (share->open_errno= my_errno), 0);
792 if (field->type() == MYSQL_TYPE_BLOB ||
793 field->type() == MYSQL_TYPE_GEOMETRY)
794 key_part_flag|= HA_BLOB_PART;
795 else if (field->real_type() == MYSQL_TYPE_VARCHAR)
796 key_part_flag|= HA_VAR_LENGTH_PART;
797 else if (field->type() == MYSQL_TYPE_BIT)
798 key_part_flag|= HA_BIT_PART;
811 fieldnr= field->field_index + 1;
812 null_bit= field->null_bit;
813 null_offset= field->null_offset();
814 offset= field->offset(field->table->record[0]);
815 length= (uint16) field->key_length();
816 store_length= length;
820 store_length+= HA_KEY_NULL_LENGTH;
821 if (field->type() == MYSQL_TYPE_BLOB ||
822 field->real_type() == MYSQL_TYPE_VARCHAR ||
823 field->type() == MYSQL_TYPE_GEOMETRY)
825 store_length+= HA_KEY_BLOB_LENGTH;
829 type= (uint8) field->key_type();
831 ((ha_base_keytype) type == HA_KEYTYPE_TEXT ||
832 (ha_base_keytype) type == HA_KEYTYPE_VARTEXT1 ||
833 (ha_base_keytype) type == HA_KEYTYPE_VARTEXT2) ?
834 0 : FIELDFLAG_BINARY;
851 uint primary_key_n,
KEY *keyinfo, uint key_n,
852 uint key_part_n, uint *usable_parts)
855 Field *field= key_part->field;
858 if (key_part_n == 0 && key_n != primary_key_n)
859 field->flags |= (((keyinfo->
flags & HA_NOSAME) &&
861 UNIQUE_KEY_FLAG : MULTIPLE_KEY_FLAG);
863 field->key_start.set_bit(key_n);
864 if (field->key_length() == key_part->length &&
865 !(field->flags & BLOB_FLAG))
867 if (handler_file->index_flags(key_n, key_part_n, 0) & HA_KEYREAD_ONLY)
869 share->keys_for_keyread.set_bit(key_n);
870 field->part_of_key.set_bit(key_n);
871 field->part_of_key_not_clustered.set_bit(key_n);
873 if (handler_file->index_flags(key_n, key_part_n, 1) & HA_READ_ORDER)
874 field->part_of_sortkey.set_bit(key_n);
877 if (!(key_part->key_part_flag & HA_REVERSE_SORT) &&
878 *usable_parts == key_part_n)
910 static uint add_pk_parts_to_sk(
KEY *sk, uint sk_n,
KEY *pk, uint pk_n,
915 bool is_unique_key=
false;
930 bool pk_field_is_in_sk=
false;
933 if (sk->key_part[j].fieldnr == pk_key_part->fieldnr &&
934 share->field[pk_key_part->fieldnr - 1]->key_length() ==
935 sk->key_part[j].length)
937 pk_field_is_in_sk=
true;
943 if (!pk_field_is_in_sk)
946 if (max_key_length + pk_key_part->length > MAX_KEY_LENGTH)
949 *current_key_part= *pk_key_part;
950 setup_key_part_field(share, handler_file, pk_n, sk, sk_n,
952 *current_rec_per_key++= 0;
956 max_key_length+= pk_key_part->length;
980 static int open_binary_frm(THD *thd,
TABLE_SHARE *share, uchar *head,
983 int error, errarg= 0;
984 uint new_frm_ver, field_pack_length, new_field_pack_flag;
985 uint interval_count, interval_parts, read_length, int_length;
986 uint db_create_options, keys, key_parts, n_length;
987 uint key_info_length, com_length, null_bit_pos;
988 uint extra_rec_buf_length;
990 bool use_extended_sk;
992 char *keynames, *names, *comment_pos;
995 uchar *disk_buff, *strpos, *null_flags, *null_pos;
996 ulong pos, record_offset, *rec_per_key, rec_buff_length;
1001 Field **field_ptr, *reg_field;
1002 const char **interval_array;
1003 enum legacy_db_type legacy_db_type;
1004 my_bitmap_map *bitmaps;
1005 uchar *extra_segment_buff= 0;
1006 const uint format_section_header_size= 8;
1007 uchar *format_section_fields= 0;
1008 DBUG_ENTER(
"open_binary_frm");
1010 new_field_pack_flag= head[27];
1011 new_frm_ver= (head[2] - FRM_VER);
1012 field_pack_length= new_frm_ver < 2 ? 11 : 17;
1017 if (!(pos= get_form_pos(file, head)))
1023 share->frm_version= head[2];
1030 if (share->frm_version == FRM_VER_TRUE_VARCHAR -1 && head[33] == 5)
1031 share->frm_version= FRM_VER_TRUE_VARCHAR;
1033 #ifdef WITH_PARTITION_STORAGE_ENGINE
1035 !(share->default_part_db_type=
1036 ha_checktype(thd, (
enum legacy_db_type) (uint) *(head+61), 1, 0)))
1038 DBUG_PRINT(
"info", (
"default_part_db_type = %u", head[61]));
1040 legacy_db_type= (
enum legacy_db_type) (uint) *(head+3);
1041 DBUG_ASSERT(share->db_plugin == NULL);
1046 if (legacy_db_type > DB_TYPE_UNKNOWN &&
1047 legacy_db_type < DB_TYPE_FIRST_DYNAMIC)
1048 share->db_plugin= ha_lock_engine(NULL,
1050 share->db_create_options= db_create_options= uint2korr(head+30);
1051 share->db_options_in_use= share->db_create_options;
1052 share->mysql_version= uint4korr(head+51);
1053 share->null_field_first= 0;
1056 share->avg_row_length= uint4korr(head+34);
1057 share->row_type= (row_type) head[40];
1058 share->table_charset= get_charset((((uint) head[41]) << 8) +
1059 (uint) head[38],MYF(0));
1060 share->null_field_first= 1;
1061 share->stats_sample_pages= uint2korr(head+42);
1062 share->stats_auto_recalc=
static_cast<enum_stats_auto_recalc
>(head[44]);
1064 if (!share->table_charset)
1067 if (use_mb(default_charset_info))
1070 sql_print_warning(
"'%s' had no or invalid character set, "
1071 "and default character set is multi-byte, "
1072 "so character column sizes may have changed",
1075 share->table_charset= default_charset_info;
1077 share->db_record_offset= 1;
1079 share->db_low_byte_first=
test(legacy_db_type != DB_TYPE_ISAM);
1081 share->max_rows= uint4korr(head+18);
1082 share->min_rows= uint4korr(head+22);
1085 key_info_length= (uint) uint2korr(head+28);
1086 mysql_file_seek(file, (ulong) uint2korr(head+6), MY_SEEK_SET, MYF(0));
1087 if (read_string(file,(uchar**) &disk_buff,key_info_length))
1089 if (disk_buff[0] & 0x80)
1091 share->keys= keys= (disk_buff[1] << 7) | (disk_buff[0] & 0x7f);
1092 share->key_parts= key_parts= uint2korr(disk_buff+2);
1096 share->keys= keys= disk_buff[0];
1097 share->key_parts= key_parts= disk_buff[1];
1099 share->keys_for_keyread.init(0);
1100 share->keys_in_use.init(keys);
1105 ha_check_storage_engine_flag(share->db_type(),
1106 HTON_SUPPORTS_EXTENDED_KEYS);
1108 uint total_key_parts;
1109 if (use_extended_sk)
1111 uint primary_key_parts= keys ?
1112 (new_frm_ver >= 3) ? (uint) strpos[4] : (uint) strpos[3] : 0;
1113 total_key_parts= key_parts + primary_key_parts * (keys - 1);
1116 total_key_parts= key_parts;
1119 if (!(keyinfo = (
KEY*) alloc_root(&share->mem_root,
1120 n_length + uint2korr(disk_buff+4))))
1122 memset(keyinfo, 0, n_length);
1123 share->key_info= keyinfo;
1124 key_part= reinterpret_cast<KEY_PART_INFO*>(keyinfo+keys);
1126 if (!(rec_per_key= (ulong*) alloc_root(&share->mem_root,
1127 sizeof(ulong) * total_key_parts)))
1130 for (i=0 ; i < keys ; i++, keyinfo++)
1133 if (new_frm_ver >= 3)
1135 keyinfo->flags= (uint) uint2korr(strpos) ^ HA_NOSAME;
1136 keyinfo->key_length= (uint) uint2korr(strpos+2);
1137 keyinfo->user_defined_key_parts= (uint) strpos[4];
1138 keyinfo->algorithm= (
enum ha_key_alg) strpos[5];
1139 keyinfo->block_size= uint2korr(strpos+6);
1144 keyinfo->flags= ((uint) strpos[0]) ^ HA_NOSAME;
1145 keyinfo->key_length= (uint) uint2korr(strpos+1);
1146 keyinfo->user_defined_key_parts= (uint) strpos[3];
1147 keyinfo->algorithm= HA_KEY_ALG_UNDEF;
1151 keyinfo->key_part= key_part;
1152 keyinfo->rec_per_key= rec_per_key;
1153 for (j=keyinfo->user_defined_key_parts ; j-- ; key_part++)
1156 key_part->fieldnr= (uint16) (uint2korr(strpos) & FIELD_NR_MASK);
1157 key_part->offset= (uint) uint2korr(strpos+2)-1;
1158 key_part->key_type= (uint) uint2korr(strpos+5);
1160 if (new_frm_ver >= 1)
1162 key_part->key_part_flag= *(strpos+4);
1163 key_part->length= (uint) uint2korr(strpos+7);
1168 key_part->length= *(strpos+4);
1169 key_part->key_part_flag=0;
1170 if (key_part->length > 128)
1172 key_part->length&=127;
1173 key_part->key_part_flag=HA_REVERSE_SORT;
1177 key_part->store_length=key_part->length;
1187 keyinfo->actual_key_parts= keyinfo->user_defined_key_parts;
1188 keyinfo->actual_flags= keyinfo->flags;
1189 if (use_extended_sk && i && !(keyinfo->flags & HA_NOSAME))
1192 keyinfo->unused_key_parts= primary_key_parts;
1193 key_part+= primary_key_parts;
1194 rec_per_key+= primary_key_parts;
1195 share->key_parts+= primary_key_parts;
1198 keynames=(
char*) key_part;
1199 strpos+= (strmov(keynames, (
char *) strpos) - keynames)+1;
1202 for (keyinfo= share->key_info, i=0; i < keys; i++, keyinfo++)
1204 if (keyinfo->flags & HA_USES_COMMENT)
1206 keyinfo->comment.length= uint2korr(strpos);
1207 keyinfo->comment.str= strmake_root(&share->mem_root, (
char*) strpos+2,
1208 keyinfo->comment.length);
1209 strpos+= 2 + keyinfo->comment.length;
1211 DBUG_ASSERT(
test(keyinfo->flags & HA_USES_COMMENT) ==
1212 (keyinfo->comment.length > 0));
1215 share->reclength = uint2korr((head+16));
1216 if (*(head+26) == 1)
1218 #ifdef HAVE_CRYPTED_FRM
1219 else if (*(head+26) == 2)
1221 crypted= get_crypt_for_frm();
1226 record_offset= (ulong) (uint2korr(head+6)+
1227 ((uint2korr(head+14) == 0xffff ?
1228 uint4korr(head+47) : uint2korr(head+14))));
1230 if ((n_length= uint4korr(head+55)))
1233 uchar *next_chunk, *buff_end;
1234 DBUG_PRINT(
"info", (
"extra segment size is %u bytes", n_length));
1235 if (!(extra_segment_buff= (uchar*) my_malloc(n_length, MYF(MY_WME))))
1237 next_chunk= extra_segment_buff;
1239 n_length, record_offset + share->reclength,
1244 share->connect_string.length= uint2korr(next_chunk);
1245 if (!(share->connect_string.str= strmake_root(&share->mem_root,
1246 (
char*) next_chunk + 2,
1247 share->connect_string.
1252 next_chunk+= share->connect_string.length + 2;
1253 buff_end= extra_segment_buff + n_length;
1254 if (next_chunk + 2 < buff_end)
1256 uint str_db_type_length= uint2korr(next_chunk);
1258 name.str= (
char*) next_chunk + 2;
1259 name.length= str_db_type_length;
1262 if (tmp_plugin != NULL && !plugin_equals(tmp_plugin, share->db_plugin))
1264 if (legacy_db_type > DB_TYPE_UNKNOWN &&
1265 legacy_db_type < DB_TYPE_FIRST_DYNAMIC &&
1266 legacy_db_type != ha_legacy_type(
1277 plugin_unlock(NULL, share->db_plugin);
1278 share->db_plugin= my_plugin_lock(NULL, &tmp_plugin);
1279 DBUG_PRINT(
"info", (
"setting dbtype to '%.*s' (%d)",
1280 str_db_type_length, next_chunk + 2,
1281 ha_legacy_type(share->db_type())));
1283 #ifdef WITH_PARTITION_STORAGE_ENGINE
1284 else if (str_db_type_length == 9 &&
1285 !strncmp((
char *) next_chunk + 2,
"partition", 9))
1294 if (!plugin_is_ready(&name, MYSQL_STORAGE_ENGINE_PLUGIN))
1297 my_error(ER_OPTION_PREVENTS_STATEMENT, MYF(0),
1298 "--skip-partition");
1301 plugin_unlock(NULL, share->db_plugin);
1302 share->db_plugin= ha_lock_engine(NULL, partition_hton);
1303 DBUG_PRINT(
"info", (
"setting dbtype to '%.*s' (%d)",
1304 str_db_type_length, next_chunk + 2,
1305 ha_legacy_type(share->db_type())));
1308 else if (!tmp_plugin)
1312 name.str[name.length]=0;
1313 my_error(ER_UNKNOWN_STORAGE_ENGINE, MYF(0), name.str);
1317 next_chunk+= str_db_type_length + 2;
1319 if (next_chunk + 5 < buff_end)
1321 uint32 partition_info_str_len = uint4korr(next_chunk);
1322 #ifdef WITH_PARTITION_STORAGE_ENGINE
1323 if ((share->partition_info_buffer_size=
1324 share->partition_info_str_len= partition_info_str_len))
1326 if (!(share->partition_info_str= (
char*)
1327 memdup_root(&share->mem_root, next_chunk + 4,
1328 partition_info_str_len + 1)))
1334 if (partition_info_str_len)
1336 DBUG_PRINT(
"info", (
"WITH_PARTITION_STORAGE_ENGINE is not defined"));
1340 next_chunk+= 5 + partition_info_str_len;
1342 #if MYSQL_VERSION_ID < 50200
1343 if (share->mysql_version >= 50106 && share->mysql_version <= 50109)
1356 if (share->mysql_version >= 50110 && next_chunk < buff_end)
1359 #ifdef WITH_PARTITION_STORAGE_ENGINE
1360 share->auto_partitioned= *next_chunk;
1364 keyinfo= share->key_info;
1365 for (i= 0; i < keys; i++, keyinfo++)
1367 if (keyinfo->flags & HA_USES_PARSER)
1370 if (next_chunk >= buff_end)
1373 (
"fulltext key uses parser that is not defined in .frm"));
1376 parser_name.str= (
char*) next_chunk;
1377 parser_name.length= strlen((
char*) next_chunk);
1378 next_chunk+= parser_name.length + 1;
1379 keyinfo->parser= my_plugin_lock_by_name(NULL, &parser_name,
1380 MYSQL_FTPARSER_PLUGIN);
1381 if (! keyinfo->parser)
1383 my_error(ER_PLUGIN_IS_NOT_LOADED, MYF(0), parser_name.str);
1388 if (forminfo[46] == (uchar)255)
1391 if (next_chunk + 2 > buff_end)
1394 (
"long table comment is not defined in .frm"));
1397 share->comment.length = uint2korr(next_chunk);
1398 if (! (share->comment.str= strmake_root(&share->mem_root,
1399 (
char*)next_chunk + 2, share->comment.length)))
1403 next_chunk+= 2 + share->comment.length;
1406 if (next_chunk + format_section_header_size < buff_end)
1419 DBUG_PRINT(
"info", (
"Found format section"));
1422 const uint format_section_length= uint2korr(next_chunk);
1423 const uint format_section_flags= uint4korr(next_chunk+2);
1426 if (next_chunk + format_section_length > buff_end)
1428 DBUG_PRINT(
"error", (
"format section length too long: %u",
1429 format_section_length));
1432 DBUG_PRINT(
"info", (
"format_section_length: %u, format_section_flags: %u",
1433 format_section_length, format_section_flags));
1435 share->default_storage_media=
1436 (
enum ha_storage_media) (format_section_flags & 0x7);
1439 const char *tablespace=
1440 (
const char*)next_chunk + format_section_header_size;
1441 const uint tablespace_length= strlen(tablespace);
1442 if (tablespace_length &&
1443 !(share->tablespace= strmake_root(&share->mem_root,
1444 tablespace, tablespace_length+1)))
1448 DBUG_PRINT(
"info", (
"tablespace: '%s'",
1449 share->tablespace ? share->tablespace :
"<null>"));
1452 format_section_fields=
1453 next_chunk + format_section_header_size + tablespace_length + 1;
1455 next_chunk+= format_section_length;
1458 share->key_block_size= uint2korr(head+62);
1461 extra_rec_buf_length= uint2korr(head+59);
1462 rec_buff_length= ALIGN_SIZE(share->reclength + 1 + extra_rec_buf_length);
1463 share->rec_buff_length= rec_buff_length;
1464 if (!(record= (uchar *) alloc_root(&share->mem_root,
1467 share->default_values= record;
1469 record_offset, MYF(MY_NABP)))
1473 #ifdef HAVE_CRYPTED_FRM
1476 crypted->decode((
char*) forminfo+256,288-256);
1477 if (sint2korr(forminfo+284) != 0)
1482 share->fields= uint2korr(forminfo+258);
1483 pos= uint2korr(forminfo+260);
1484 n_length= uint2korr(forminfo+268);
1485 interval_count= uint2korr(forminfo+270);
1486 interval_parts= uint2korr(forminfo+272);
1487 int_length= uint2korr(forminfo+274);
1488 share->null_fields= uint2korr(forminfo+282);
1489 com_length= uint2korr(forminfo+284);
1490 if (forminfo[46] != (uchar)255)
1492 share->comment.length= (int) (forminfo[46]);
1493 share->comment.str= strmake_root(&share->mem_root, (
char*) forminfo+47,
1494 share->comment.length);
1497 DBUG_PRINT(
"info",(
"i_count: %d i_parts: %d index: %d n_length: %d int_length: %d com_length: %d", interval_count,interval_parts, share->keys,n_length,int_length, com_length));
1499 if (!(field_ptr = (
Field **)
1500 alloc_root(&share->mem_root,
1501 (uint) ((share->fields+1)*
sizeof(
Field*)+
1502 interval_count*
sizeof(
TYPELIB)+
1503 (share->fields+interval_parts+
1504 keys+3)*
sizeof(
char *)+
1505 (n_length+int_length+com_length)))))
1508 share->field= field_ptr;
1509 read_length=(uint) (share->fields * field_pack_length +
1510 pos+ (uint) (n_length+int_length+com_length));
1511 if (read_string(file,(uchar**) &disk_buff,read_length))
1513 #ifdef HAVE_CRYPTED_FRM
1516 crypted->decode((
char*) disk_buff,read_length);
1521 strpos= disk_buff+pos;
1523 share->intervals= (
TYPELIB*) (field_ptr+share->fields+1);
1524 interval_array= (
const char **) (share->intervals+interval_count);
1525 names= (
char*) (interval_array+share->fields+interval_parts+keys+3);
1526 if (!interval_count)
1527 share->intervals= 0;
1528 memcpy((
char*) names, strpos+(share->fields*field_pack_length),
1529 (uint) (n_length+int_length));
1530 comment_pos= names+(n_length+int_length);
1531 memcpy(comment_pos, disk_buff+read_length-com_length, com_length);
1533 fix_type_pointers(&interval_array, &share->fieldnames, 1, &names);
1534 if (share->fieldnames.count != share->fields)
1536 fix_type_pointers(&interval_array, share->intervals, interval_count,
1542 for (interval= share->intervals;
1543 interval < share->intervals + interval_count;
1546 uint count= (uint) (interval->count + 1) *
sizeof(uint);
1547 if (!(interval->type_lengths= (uint *) alloc_root(&share->mem_root,
1550 for (count= 0; count < interval->count; count++)
1552 char *val= (
char*) interval->type_names[count];
1553 interval->type_lengths[count]= strlen(val);
1555 interval->type_lengths[count]= 0;
1560 fix_type_pointers(&interval_array, &share->keynames, 1, &keynames);
1563 if (!(handler_file= get_new_handler(share, thd->mem_root,
1567 if (handler_file->set_ha_share_ref(&share->
ha_share))
1570 record= share->default_values-1;
1571 if (share->null_field_first)
1573 null_flags= null_pos= (uchar*) record+1;
1574 null_bit_pos= (db_create_options & HA_OPTION_PACK_RECORD) ? 0 : 1;
1580 share->null_bytes= (share->null_fields + null_bit_pos + 7) / 8;
1582 #ifndef WE_WANT_TO_SUPPORT_VERY_OLD_FRM_FILES
1585 share->null_bytes= (share->null_fields+7)/8;
1586 null_flags= null_pos= (uchar*) (record + 1 +share->reclength -
1592 use_hash= share->fields >= MAX_FIELDS_BEFORE_HASH;
1594 use_hash= !my_hash_init(&share->name_hash,
1595 system_charset_info,
1597 (my_hash_get_key) get_field_name,0,0);
1599 for (i=0 ; i < share->fields; i++, strpos+=field_pack_length, field_ptr++)
1601 uint pack_flag, interval_nr, unireg_type, recpos, field_length;
1602 enum_field_types field_type;
1604 Field::geometry_type geom_type= Field::GEOM_GEOMETRY;
1607 if (new_frm_ver >= 3)
1610 field_length= uint2korr(strpos+3);
1611 recpos= uint3korr(strpos+5);
1612 pack_flag= uint2korr(strpos+8);
1613 unireg_type= (uint) strpos[10];
1614 interval_nr= (uint) strpos[12];
1615 uint comment_length=uint2korr(strpos+15);
1616 field_type=(enum_field_types) (uint) strpos[13];
1619 if (field_type == MYSQL_TYPE_GEOMETRY)
1622 geom_type= (Field::geometry_type) strpos[14];
1623 charset= &my_charset_bin;
1631 uint csid= strpos[14] + (((uint) strpos[11]) << 8);
1633 charset= &my_charset_bin;
1634 else if (!(charset= get_charset(csid, MYF(0))))
1641 if (!comment_length)
1643 comment.str= (
char*)
"";
1648 comment.str= (
char*) comment_pos;
1649 comment.length= comment_length;
1650 comment_pos+= comment_length;
1655 field_length= (uint) strpos[3];
1656 recpos= uint2korr(strpos+4),
1657 pack_flag= uint2korr(strpos+6);
1658 pack_flag&= ~FIELDFLAG_NO_DEFAULT;
1659 unireg_type= (uint) strpos[8];
1660 interval_nr= (uint) strpos[10];
1663 field_type= (enum_field_types) f_packtype(pack_flag);
1664 if (f_is_binary(pack_flag))
1672 if (!f_is_blob(pack_flag))
1675 if (!(charset= get_charset_by_csname(share->table_charset->csname,
1676 MY_CS_BINSORT, MYF(0))))
1677 charset= &my_charset_bin;
1680 charset= &my_charset_bin;
1683 charset= share->table_charset;
1684 memset(&comment, 0,
sizeof(comment));
1687 if (interval_nr && charset->mbminlen > 1)
1690 TYPELIB *interval= share->intervals + interval_nr - 1;
1691 unhex_type2(interval);
1694 #ifndef TO_BE_DELETED_ON_PRODUCTION
1695 if (field_type == MYSQL_TYPE_NEWDECIMAL && !share->mysql_version)
1702 uint decimals= f_decimals(pack_flag);
1703 field_length= my_decimal_precision_to_length(field_length,
1705 f_is_dec(pack_flag) == 0);
1706 sql_print_error(
"Found incompatible DECIMAL field '%s' in %s; "
1707 "Please do \"ALTER TABLE `%s` FORCE\" to fix it!",
1708 share->fieldnames.type_names[i], share->table_name.str,
1709 share->table_name.str);
1710 push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
1711 ER_CRASHED_ON_USAGE,
1712 "Found incompatible DECIMAL field '%s' in %s; "
1713 "Please do \"ALTER TABLE `%s` FORCE\" to fix it!",
1714 share->fieldnames.type_names[i],
1715 share->table_name.str,
1716 share->table_name.str);
1721 *field_ptr= reg_field=
1722 make_field(share, record+recpos,
1723 (uint32) field_length,
1724 null_pos, null_bit_pos,
1729 (Field::utype) MTYP_TYPENR(unireg_type),
1731 share->intervals+interval_nr-1 :
1733 share->fieldnames.type_names[i]);
1740 reg_field->field_index=
i;
1741 reg_field->comment=comment;
1742 if (field_type == MYSQL_TYPE_BIT && !f_bit_as_char(pack_flag))
1744 if ((null_bit_pos+= field_length & 7) > 7)
1750 if (!(reg_field->flags & NOT_NULL_FLAG))
1752 if (!(null_bit_pos= (null_bit_pos + 1) & 7))
1755 if (f_no_default(pack_flag))
1756 reg_field->flags|= NO_DEFAULT_VALUE_FLAG;
1758 if (reg_field->unireg_check == Field::NEXT_NUMBER)
1759 share->found_next_number_field= field_ptr;
1762 if (my_hash_insert(&share->name_hash, (uchar*) field_ptr) )
1773 if (format_section_fields)
1775 const uchar field_flags= format_section_fields[
i];
1776 const uchar field_storage= (field_flags & STORAGE_TYPE_MASK);
1777 const uchar field_column_format=
1778 ((field_flags >> COLUMN_FORMAT_SHIFT)& COLUMN_FORMAT_MASK);
1779 DBUG_PRINT(
"debug", (
"field flags: %u, storage: %u, column_format: %u",
1780 field_flags, field_storage, field_column_format));
1781 reg_field->set_storage_type((ha_storage_media)field_storage);
1782 reg_field->set_column_format((column_format_type)field_column_format);
1790 uint primary_key=(uint) (find_type(primary_key_name, &share->keynames,
1791 FIND_TYPE_NO_PREFIX) - 1);
1793 keyinfo= share->key_info;
1794 key_part= keyinfo->key_part;
1796 for (uint key=0 ; key < share->keys ; key++,keyinfo++)
1798 uint usable_parts= 0;
1799 keyinfo->name=(
char*) share->keynames.type_names[key];
1801 if (share->key_info[key].
flags & HA_FULLTEXT)
1802 share->key_info[key].algorithm= HA_KEY_ALG_FULLTEXT;
1804 if (primary_key >= MAX_KEY && (keyinfo->flags & HA_NOSAME))
1811 for (i=0 ; i < keyinfo->user_defined_key_parts ;i++)
1813 DBUG_ASSERT(key_part[i].fieldnr > 0);
1815 Field *table_field= share->field[key_part[
i].fieldnr - 1];
1824 table_field->type() == MYSQL_TYPE_BLOB &&
1825 table_field->field_length == key_part[
i].length)
1829 table_field->key_length() != key_part[
i].length)
1831 primary_key= MAX_KEY;
1837 for (i=0 ; i < keyinfo->user_defined_key_parts ; key_part++,i++)
1840 if (new_field_pack_flag <= 1)
1841 key_part->fieldnr= (uint16) find_field(share->field,
1842 share->default_values,
1843 (uint) key_part->offset,
1844 (uint) key_part->length);
1845 if (!key_part->fieldnr)
1850 field= key_part->field= share->field[key_part->fieldnr-1];
1851 key_part->type= field->key_type();
1854 key_part->null_offset=field->null_offset(share->default_values);
1855 key_part->null_bit= field->null_bit;
1856 key_part->store_length+=HA_KEY_NULL_LENGTH;
1857 keyinfo->flags|=HA_NULL_PART_KEY;
1858 keyinfo->key_length+= HA_KEY_NULL_LENGTH;
1860 if (field->type() == MYSQL_TYPE_BLOB ||
1861 field->real_type() == MYSQL_TYPE_VARCHAR ||
1862 field->type() == MYSQL_TYPE_GEOMETRY)
1864 key_part->store_length+=HA_KEY_BLOB_LENGTH;
1865 if (i + 1 <= keyinfo->user_defined_key_parts)
1866 keyinfo->key_length+= HA_KEY_BLOB_LENGTH;
1870 setup_key_part_field(share, handler_file, primary_key,
1871 keyinfo, key, i, &usable_parts);
1873 field->flags|= PART_KEY_FLAG;
1874 if (key == primary_key)
1876 field->flags|= PRI_KEY_FLAG;
1881 if (ha_option & HA_PRIMARY_KEY_IN_READ_INDEX)
1883 if (field->key_length() == key_part->length &&
1884 !(field->flags & BLOB_FLAG))
1885 field->part_of_key= share->keys_in_use;
1886 if (field->part_of_sortkey.is_set(key))
1887 field->part_of_sortkey= share->keys_in_use;
1890 if (field->key_length() != key_part->length)
1892 #ifndef TO_BE_DELETED_ON_PRODUCTION
1893 if (field->type() == MYSQL_TYPE_NEWDECIMAL)
1902 keyinfo->key_length-= (key_part->length - field->key_length());
1903 key_part->store_length-= (uint16)(key_part->length -
1904 field->key_length());
1905 key_part->length= (uint16)field->key_length();
1906 sql_print_error(
"Found wrong key definition in %s; "
1907 "Please do \"ALTER TABLE `%s` FORCE \" to fix it!",
1908 share->table_name.str,
1909 share->table_name.str);
1910 push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
1911 ER_CRASHED_ON_USAGE,
1912 "Found wrong key definition in %s; "
1913 "Please do \"ALTER TABLE `%s` FORCE\" to fix "
1915 share->table_name.str,
1916 share->table_name.str);
1921 key_part->key_part_flag|= HA_PART_KEY_SEG;
1926 if (use_extended_sk && primary_key < MAX_KEY &&
1927 key && !(keyinfo->flags & HA_NOSAME))
1928 key_part+= add_pk_parts_to_sk(keyinfo, key, share->key_info, primary_key,
1929 share, handler_file, &usable_parts);
1932 key_part+= keyinfo->unused_key_parts;
1934 keyinfo->usable_key_parts= usable_parts;
1936 set_if_bigger(share->max_key_length,keyinfo->key_length+
1937 keyinfo->user_defined_key_parts);
1938 share->total_key_length+= keyinfo->key_length;
1943 if ((keyinfo->flags & HA_NOSAME) ||
1944 (ha_option & HA_ANY_INDEX_MAY_BE_UNIQUE))
1945 set_if_bigger(share->max_unique_length,keyinfo->key_length);
1947 if (primary_key < MAX_KEY &&
1948 (share->keys_in_use.is_set(primary_key)))
1950 share->primary_key= primary_key;
1957 Field *field= share->key_info[primary_key].key_part[0].field;
1958 if (field && field->result_type() == INT_RESULT)
1961 share->rowid_field_offset= (share->key_info[primary_key].key_part[0].
1967 share->primary_key = MAX_KEY;
1970 share->primary_key= MAX_KEY;
1973 if (new_field_pack_flag <= 1)
1976 uint null_length= (share->null_fields+7)/8;
1977 memset(share->default_values + (null_flags - (uchar*) record), 255,
1981 if (share->found_next_number_field)
1983 reg_field= *share->found_next_number_field;
1984 if ((
int) (share->next_number_index= (uint)
1985 find_ref_key(share->key_info, share->keys,
1986 share->default_values, reg_field,
1987 &share->next_number_key_offset,
1988 &share->next_number_keypart)) < 0)
1995 reg_field->flags |= AUTO_INCREMENT_FLAG;
1998 if (share->blob_fields)
2004 if (!(share->blob_field= save=
2005 (uint*) alloc_root(&share->mem_root,
2006 (uint) (share->blob_fields*
sizeof(uint)))))
2008 for (k=0, ptr= share->field ; *ptr ; ptr++, k++)
2010 if ((*ptr)->flags & BLOB_FLAG)
2019 share->null_bytes= (null_pos - (uchar*) null_flags +
2020 (null_bit_pos + 7) / 8);
2021 share->last_null_bit_pos= null_bit_pos;
2023 share->db_low_byte_first= handler_file->low_byte_first();
2024 share->column_bitmap_size= bitmap_buffer_size(share->fields);
2026 if (!(bitmaps= (my_bitmap_map*) alloc_root(&share->mem_root,
2027 share->column_bitmap_size)))
2029 bitmap_init(&share->all_set, bitmaps, share->fields, FALSE);
2030 bitmap_set_all(&share->all_set);
2032 delete handler_file;
2035 (void) my_hash_check(&share->name_hash);
2037 my_free(extra_segment_buff);
2041 share->error= error;
2042 share->open_errno= my_errno;
2043 share->errarg= errarg;
2045 my_free(extra_segment_buff);
2047 delete handler_file;
2048 my_hash_free(&share->name_hash);
2050 open_table_error(share, error, share->open_errno, errarg);
2082 int open_table_from_share(THD *thd,
TABLE_SHARE *share,
const char *alias,
2083 uint db_stat, uint prgflag, uint ha_open_flags,
2084 TABLE *outparam,
bool is_create_table)
2087 uint records,
i, bitmap_size;
2088 bool error_reported= FALSE;
2089 uchar *record, *bitmaps;
2091 DBUG_ENTER(
"open_table_from_share");
2092 DBUG_PRINT(
"enter",(
"name: '%s.%s' form: 0x%lx", share->db.str,
2093 share->table_name.str, (
long) outparam));
2096 memset(outparam, 0,
sizeof(*outparam));
2097 outparam->in_use= thd;
2099 outparam->db_stat= db_stat;
2100 outparam->write_row_record= NULL;
2102 init_sql_alloc(&outparam->
mem_root, TABLE_ALLOC_BLOCK_SIZE, 0);
2104 if (!(outparam->alias= my_strdup(alias, MYF(MY_WME))))
2106 outparam->quick_keys.init();
2107 outparam->possible_quick_keys.init();
2108 outparam->covering_keys.init();
2109 outparam->merge_keys.init();
2110 outparam->keys_in_use_for_query.init();
2114 if (!(prgflag & OPEN_FRM_FILE_ONLY))
2116 if (!(outparam->file= get_new_handler(share, &outparam->
mem_root,
2119 if (outparam->file->set_ha_share_ref(&share->
ha_share))
2124 DBUG_ASSERT(!db_stat);
2128 outparam->reginfo.lock_type= TL_UNLOCK;
2129 outparam->current_lock= F_UNLCK;
2131 if ((db_stat & HA_OPEN_KEYFILE) || (prgflag & DELAYED_OPEN))
2133 if (prgflag & (READ_ALL+EXTRA_RECORD))
2136 if (!(record= (uchar*) alloc_root(&outparam->
mem_root,
2137 share->rec_buff_length * records)))
2143 outparam->record[0]= outparam->record[1]= share->default_values;
2147 outparam->record[0]= record;
2149 outparam->record[1]= record+ share->rec_buff_length;
2151 outparam->record[1]= outparam->record[0];
2161 memcpy(outparam->record[0], share->default_values, share->rec_buff_length);
2162 memcpy(outparam->record[1], share->default_values, share->null_bytes);
2166 if (!(field_ptr = (
Field **) alloc_root(&outparam->
mem_root,
2167 (uint) ((share->fields+1)*
2171 outparam->field= field_ptr;
2173 record= (uchar*) outparam->record[0]-1;
2174 if (share->null_field_first)
2175 outparam->null_flags= (uchar*) record+1;
2177 outparam->null_flags= (uchar*) (record+ 1+ share->reclength -
2181 for (i=0 ; i < share->fields; i++, field_ptr++)
2184 *field_ptr= new_field;
2185 if (new_field == NULL)
2187 new_field->init(outparam);
2188 new_field->move_field_offset((my_ptrdiff_t) (outparam->record[0] -
2189 outparam->s->default_values));
2193 if (share->found_next_number_field)
2194 outparam->found_next_number_field=
2195 outparam->field[(uint) (share->found_next_number_field - share->field)];
2198 if (share->key_parts)
2200 KEY *key_info, *key_info_end;
2201 KEY_PART_INFO *key_part;
2203 n_length= share->keys *
sizeof(
KEY) +
2204 share->key_parts *
sizeof(KEY_PART_INFO);
2206 if (!(key_info= (
KEY*) alloc_root(&outparam->
mem_root, n_length)))
2208 outparam->key_info= key_info;
2209 key_part= (
reinterpret_cast<KEY_PART_INFO*
>(key_info+share->keys));
2211 memcpy(key_info, share->key_info,
sizeof(*key_info)*share->keys);
2212 memcpy(key_part, share->key_info[0].key_part, (
sizeof(*key_part) *
2215 for (key_info_end= key_info + share->keys ;
2216 key_info < key_info_end ;
2219 KEY_PART_INFO *key_part_end;
2221 key_info->table= outparam;
2222 key_info->key_part= key_part;
2225 key_part < key_part_end ;
2228 Field *field= key_part->field= outparam->field[key_part->fieldnr-1];
2230 if (field->key_length() != key_part->length &&
2231 !(field->flags & BLOB_FLAG))
2237 field= key_part->field=field->new_field(&outparam->
mem_root,
2239 field->field_length= key_part->length;
2247 #ifdef WITH_PARTITION_STORAGE_ENGINE
2248 if (share->partition_info_str_len && outparam->file)
2261 Query_arena *backup_stmt_arena_ptr= thd->stmt_arena;
2262 Query_arena backup_arena;
2263 Query_arena part_func_arena(&outparam->
mem_root,
2264 Query_arena::STMT_INITIALIZED);
2265 thd->set_n_backup_active_arena(&part_func_arena, &backup_arena);
2266 thd->stmt_arena= &part_func_arena;
2268 bool work_part_info_used;
2270 tmp= mysql_unpack_partition(thd, share->partition_info_str,
2271 share->partition_info_str_len,
2272 outparam, is_create_table,
2273 share->default_part_db_type,
2274 &work_part_info_used);
2277 thd->stmt_arena= backup_stmt_arena_ptr;
2278 thd->restore_active_arena(&part_func_arena, &backup_arena);
2279 goto partititon_err;
2281 outparam->part_info->is_auto_partitioned= share->auto_partitioned;
2282 DBUG_PRINT(
"info", (
"autopartitioned: %u", share->auto_partitioned));
2287 if (!work_part_info_used)
2288 tmp= fix_partition_func(thd, outparam, is_create_table);
2289 thd->stmt_arena= backup_stmt_arena_ptr;
2290 thd->restore_active_arena(&part_func_arena, &backup_arena);
2293 if (work_part_info_used)
2294 tmp= fix_partition_func(thd, outparam, is_create_table);
2296 outparam->part_info->item_free_list= part_func_arena.free_list;
2300 if (is_create_table)
2307 error_reported= TRUE;
2316 bitmap_size= share->column_bitmap_size;
2317 if (!(bitmaps= (uchar*) alloc_root(&outparam->
mem_root, bitmap_size*3)))
2319 bitmap_init(&outparam->def_read_set,
2320 (my_bitmap_map*) bitmaps, share->fields, FALSE);
2321 bitmap_init(&outparam->def_write_set,
2322 (my_bitmap_map*) (bitmaps+bitmap_size), share->fields, FALSE);
2323 bitmap_init(&outparam->tmp_set,
2324 (my_bitmap_map*) (bitmaps+bitmap_size*2), share->fields, FALSE);
2325 outparam->default_column_bitmaps();
2332 if ((ha_err= (outparam->file->
2333 ha_open(outparam, share->normalized_path.str,
2334 (db_stat & HA_READ_ONLY ? O_RDONLY : O_RDWR),
2335 (db_stat & HA_OPEN_TEMPORARY ? HA_OPEN_TMP_TABLE :
2336 ((db_stat & HA_WAIT_IF_LOCKED) ||
2337 (specialflag & SPECIAL_WAIT_IF_LOCKED)) ?
2338 HA_OPEN_WAIT_IF_LOCKED :
2339 (db_stat & (HA_ABORT_IF_LOCKED | HA_GET_INFO)) ?
2340 HA_OPEN_ABORT_IF_LOCKED :
2341 HA_OPEN_IGNORE_IF_LOCKED) | ha_open_flags))))
2344 share->crashed= ((ha_err == HA_ERR_CRASHED_ON_USAGE) &&
2345 outparam->file->auto_repair() &&
2346 !(ha_open_flags & HA_OPEN_FOR_REPAIR));
2350 case HA_ERR_NO_SUCH_TABLE:
2363 DBUG_PRINT(
"error", (
"open file: %s failed, too many files opened (errno: %d)",
2364 share->normalized_path.str, ha_err));
2370 error_reported= TRUE;
2371 if (ha_err == HA_ERR_TABLE_DEF_CHANGED)
2379 #if defined(HAVE_purify) && !defined(DBUG_OFF)
2380 memset(bitmaps, 0, bitmap_size*3);
2388 else if (outparam->file)
2392 | HA_BINLOG_ROW_CAPABLE))
2393 ||
test(flags & HA_HAS_OWN_BINLOGGING);
2400 thd->status_var.opened_tables++;
2405 if (! error_reported)
2406 open_table_error(share, error, my_errno, 0);
2407 delete outparam->file;
2408 #ifdef WITH_PARTITION_STORAGE_ENGINE
2409 if (outparam->part_info)
2410 free_items(outparam->part_info->item_free_list);
2413 outparam->db_stat=0;
2414 free_root(&outparam->
mem_root, MYF(0));
2415 my_free((
void *) outparam->alias);
2416 DBUG_RETURN (error);
2429 int closefrm(
register TABLE *
table,
bool free_share)
2432 DBUG_ENTER(
"closefrm");
2433 DBUG_PRINT(
"enter", (
"table: 0x%lx", (
long) table));
2437 my_free((
void *) table->alias);
2441 for (
Field **ptr=table->field ; *ptr ; ptr++)
2447 #ifdef WITH_PARTITION_STORAGE_ENGINE
2448 if (table->part_info)
2451 free_items(table->part_info->item_free_list);
2452 table->part_info->item_free_list= 0;
2453 table->part_info= 0;
2458 if (table->s->tmp_table == NO_TMP_TABLE)
2461 free_table_share(table->s);
2463 free_root(&table->
mem_root, MYF(0));
2470 void free_blobs(
register TABLE *table)
2473 for (ptr= table->s->blob_field, end=ptr + table->s->blob_fields ;
2482 if (table->field[*ptr])
2497 void free_field_buffers_larger_than(
TABLE *table, uint32
size)
2500 for (ptr= table->s->blob_field, end=ptr + table->s->blob_fields ;
2505 if (blob->get_field_buffer_size() >
size)
2520 static ulong get_form_pos(File file, uchar *head)
2525 DBUG_ENTER(
"get_form_pos");
2527 names= uint2korr(head+8);
2529 if (!(names= uint2korr(head+8)))
2532 length= uint2korr(head+4);
2536 if (!(buf= (uchar*) my_malloc(length+names*4, MYF(MY_WME))))
2546 ret_value= uint4korr(pos);
2550 DBUG_RETURN(ret_value);
2561 int read_string(File file, uchar**
to,
size_t length)
2563 DBUG_ENTER(
"read_string");
2566 if (!(*to= (uchar*) my_malloc(length+1,MYF(MY_WME))) ||
2573 *((
char*) *to+length)=
'\0';
2581 const char *newname)
2583 uint
i,bufflength,maxlength,n_length,length,names;
2584 ulong endpos,newpos;
2585 uchar buff[IO_SIZE];
2587 DBUG_ENTER(
"make_new_entry");
2589 length=(uint) strlen(newname)+1;
2590 n_length=uint2korr(fileinfo+4);
2591 maxlength=uint2korr(fileinfo+6);
2592 names=uint2korr(fileinfo+8);
2593 newpos=uint4korr(fileinfo+10);
2595 if (64+length+n_length+(names+1)*4 > maxlength)
2598 int4store(fileinfo+10,newpos);
2601 bufflength= (uint) (endpos & (IO_SIZE-1));
2603 while (endpos > maxlength)
2605 mysql_file_seek(file, (ulong) (endpos-bufflength), MY_SEEK_SET, MYF(0));
2608 mysql_file_seek(file, (ulong) (endpos-bufflength+IO_SIZE), MY_SEEK_SET,
2612 endpos-=bufflength; bufflength=IO_SIZE;
2614 memset(buff, 0, IO_SIZE);
2619 int2store(fileinfo+6,maxlength);
2620 for (i=names, pos= (uchar*) *formnames->type_names+n_length-1; i-- ;
2623 endpos=uint4korr(pos)+IO_SIZE;
2624 int4store(pos,endpos);
2631 (void) strxmov((
char*) buff,
"/",newname,
"/",NullS);
2634 (
void) strxmov((
char*) buff,newname,
"/",NullS);
2636 if (
mysql_file_write(file, buff, (
size_t) length+1, MYF(MY_NABP+MY_WME)) ||
2638 (uchar*) (*formnames->type_names+n_length-1),
2639 names*4, MYF(MY_NABP+MY_WME))) ||
2643 int2store(fileinfo+8,names+1);
2644 int2store(fileinfo+4,n_length+length);
2646 DBUG_RETURN(newpos);
2652 void open_table_error(
TABLE_SHARE *share,
int error,
int db_errno,
int errarg)
2655 char buff[FN_REFLEN];
2656 char errbuf[MYSYS_STRERROR_SIZE];
2657 myf errortype= ME_ERROR+ME_WAITTANG;
2658 DBUG_ENTER(
"open_table_error");
2663 if (db_errno == ENOENT)
2664 my_error(ER_NO_SUCH_TABLE, MYF(0), share->db.str, share->table_name.str);
2667 strxmov(buff, share->normalized_path.str, reg_ext, NullS);
2668 my_error((db_errno == EMFILE) ? ER_CANT_OPEN_FILE : ER_FILE_NOT_FOUND,
2670 db_errno, my_strerror(errbuf,
sizeof(errbuf), db_errno));
2676 const char *datext=
"";
2678 if (share->db_type() != NULL)
2680 if ((file= get_new_handler(share, current_thd->mem_root,
2683 if (!(datext= *file->
bas_ext()))
2687 err_no= (db_errno == ENOENT) ? ER_FILE_NOT_FOUND : (db_errno == EAGAIN) ?
2688 ER_FILE_USED : ER_CANT_OPEN_FILE;
2689 strxmov(buff, share->normalized_path.str, datext, NullS);
2690 my_error(err_no,errortype, buff,
2691 db_errno, my_strerror(errbuf,
sizeof(errbuf), db_errno));
2697 const char *csname= get_charset_name((uint) errarg);
2699 if (!csname || csname[0] ==
'?')
2701 my_snprintf(tmp,
sizeof(tmp),
"#%d", errarg);
2704 my_printf_error(ER_UNKNOWN_COLLATION,
2705 "Unknown collation '%s' in table '%-.64s' definition",
2706 MYF(0), csname, share->table_name.str);
2710 strxmov(buff, share->normalized_path.str, reg_ext, NullS);
2711 my_printf_error(ER_NOT_FORM_FILE,
2712 "Table '%-.64s' was created with a different version "
2713 "of MySQL and cannot be read",
2720 my_error(ER_FRM_UNKNOWN_TYPE, MYF(0), share->path.str,
2725 strxmov(buff, share->normalized_path.str, reg_ext, NullS);
2726 my_error(ER_NOT_FORM_FILE, errortype, buff);
2740 fix_type_pointers(
const char ***array,
TYPELIB *point_to_type, uint types,
2743 char *type_name, *ptr;
2749 point_to_type->name=0;
2750 point_to_type->type_names= *array;
2754 while ((type_name=strchr(ptr+1,chr)) != NullS)
2756 *((*array)++) = ptr+1;
2764 point_to_type->count= (uint) (*array - point_to_type->type_names);
2766 *((*array)++)= NullS;
2778 result->count=strings.elements;
2780 uint nbytes= (
sizeof(
char*) +
sizeof(uint)) * (result->count + 1);
2781 if (!(result->type_names= (
const char**) alloc_root(mem_root, nbytes)))
2783 result->type_lengths= (uint*) (result->type_names + result->count + 1);
2786 for (uint i=0; (tmp=it++) ; i++)
2788 result->type_names[
i]= tmp->ptr();
2789 result->type_lengths[
i]= tmp->length();
2791 result->type_names[result->count]= 0;
2792 result->type_lengths[result->count]= 0;
2810 static uint find_field(
Field **fields, uchar *record, uint start, uint length)
2816 for (field= fields, i=1 ; *field ; i++,field++)
2818 if ((*field)->offset(record) == start)
2820 if ((*field)->key_length() == length)
2822 if (!pos || fields[pos-1]->pack_length() <
2823 (*field)->pack_length())
2833 int set_zone(
register int nr,
int min_zone,
int max_zone)
2844 ulong next_io_size(
register ulong pos)
2847 if ((offset= pos & (IO_SIZE-1)))
2848 return pos-offset+IO_SIZE;
2867 void append_unescaped(
String *res,
const char *pos, uint length)
2869 const char *end= pos+length;
2872 for (; pos != end ; pos++)
2874 #if defined(USE_MB) && MYSQL_VERSION_ID < 40100
2876 if (use_mb(default_charset_info) &&
2877 (mblen= my_ismbchar(default_charset_info, pos, end)))
2879 res->append(pos, mblen);
2917 File create_frm(THD *thd,
const char *name,
const char *db,
2918 const char *table, uint reclength, uchar *fileinfo,
2923 uchar fill[IO_SIZE];
2924 int create_flags= O_RDWR | O_TRUNC;
2925 ulong key_comment_total_bytes= 0;
2928 if (create_info->options & HA_LEX_CREATE_TMP_TABLE)
2929 create_flags|= O_EXCL | O_NOFOLLOW;
2932 if (create_info->max_rows > UINT_MAX32)
2933 create_info->max_rows= UINT_MAX32;
2934 if (create_info->min_rows > UINT_MAX32)
2935 create_info->min_rows= UINT_MAX32;
2938 name, CREATE_MODE, create_flags, MYF(0))) >= 0)
2940 uint key_length, tmp_key_length, tmp, csid;
2941 memset(fileinfo, 0, 64);
2943 fileinfo[0]=(uchar) 254;
2945 fileinfo[2]= FRM_VER+3+
test(create_info->varchar);
2947 fileinfo[3]= (uchar) ha_legacy_type(
2948 ha_checktype(thd,ha_legacy_type(create_info->db_type),0,0));
2950 int2store(fileinfo+6,IO_SIZE);
2963 for (i= 0; i < keys; i++)
2965 DBUG_ASSERT(
test(key_info[i].flags & HA_USES_COMMENT) ==
2966 (key_info[i].comment.length > 0));
2967 if (key_info[i].flags & HA_USES_COMMENT)
2968 key_comment_total_bytes += 2 + key_info[
i].comment.length;
2971 key_length= keys * (8 + MAX_REF_PARTS * 9 + NAME_LEN + 1) + 16
2972 + key_comment_total_bytes;
2974 length= next_io_size((ulong) (IO_SIZE+key_length+reclength+
2975 create_info->extra_size));
2976 int4store(fileinfo+10,length);
2977 tmp_key_length= (key_length < 0xffff) ? key_length : 0xffff;
2978 int2store(fileinfo+14,tmp_key_length);
2979 int2store(fileinfo+16,reclength);
2980 int4store(fileinfo+18,create_info->max_rows);
2981 int4store(fileinfo+22,create_info->min_rows);
2985 create_info->table_options|=HA_OPTION_LONG_BLOB_PTR;
2986 int2store(fileinfo+30,create_info->table_options);
2989 int4store(fileinfo+34,create_info->avg_row_length);
2990 csid= (create_info->default_table_charset ?
2991 create_info->default_table_charset->number : 0);
2992 fileinfo[38]= (uchar) csid;
2998 fileinfo[40]= (uchar) create_info->
row_type;
3000 fileinfo[41]= (uchar) (csid >> 8);
3001 int2store(fileinfo+42, create_info->stats_sample_pages & 0xffff);
3002 fileinfo[44]= (uchar) create_info->stats_auto_recalc;
3005 int4store(fileinfo+47, key_length);
3006 tmp= MYSQL_VERSION_ID;
3007 int4store(fileinfo+51, tmp);
3008 int4store(fileinfo+55, create_info->extra_size);
3013 int2store(fileinfo+62, create_info->key_block_size);
3014 memset(fill, 0, IO_SIZE);
3015 for (; length > IO_SIZE ; length-= IO_SIZE)
3027 if (my_errno == ENOENT)
3028 my_error(ER_BAD_DB_ERROR,MYF(0),db);
3030 my_error(ER_CANT_CREATE_TABLE,MYF(0),table,my_errno);
3039 DBUG_ENTER(
"update_create_info_from_table");
3041 create_info->max_rows= share->max_rows;
3042 create_info->min_rows= share->min_rows;
3043 create_info->table_options= share->db_create_options;
3044 create_info->avg_row_length= share->avg_row_length;
3045 create_info->
row_type= share->row_type;
3046 create_info->default_table_charset= share->table_charset;
3047 create_info->table_charset= 0;
3048 create_info->comment= share->comment;
3049 create_info->storage_media= share->default_storage_media;
3050 create_info->tablespace= share->tablespace;
3056 rename_file_ext(
const char * from,
const char * to,
const char * ext)
3058 char from_b[FN_REFLEN],to_b[FN_REFLEN];
3059 (void) strxmov(from_b,from,ext,NullS);
3060 (void) strxmov(to_b,to,ext,NullS);
3081 char buff[MAX_FIELD_WIDTH], *
to;
3082 String str(buff,
sizeof(buff),&my_charset_bin);
3085 field->val_str(&str);
3086 if (!(length= str.length()))
3091 if (!(to= strmake_root(mem, str.ptr(), length)))
3093 res->set(to, length, field->charset());
3113 char buff[MAX_FIELD_WIDTH], *
to;
3114 String str(buff,
sizeof(buff),&my_charset_bin);
3117 field->val_str(&str);
3118 length= str.length();
3119 if (!length || !(to= (
char*) alloc_root(mem,length+1)))
3121 memcpy(to,str.ptr(),(uint) length);
3131 uint calculate_key_len(
TABLE *table, uint key,
const uchar *buf,
3132 key_part_map keypart_map)
3135 DBUG_ASSERT(((keypart_map + 1) & keypart_map) == 0);
3137 KEY *key_info= table->key_info + key;
3138 KEY_PART_INFO *key_part= key_info->key_part;
3142 while (key_part < end_key_part && keypart_map)
3144 length+= key_part->store_length;
3169 enum_ident_name_check check_and_convert_db_name(
LEX_STRING *org_name,
3170 bool preserve_lettercase)
3172 char *name= org_name->str;
3173 uint name_length= org_name->length;
3174 bool check_for_path_chars;
3175 enum_ident_name_check ident_check_status;
3177 if (!name_length || name_length > NAME_LEN)
3179 my_error(ER_WRONG_DB_NAME, MYF(0), org_name->str);
3180 return IDENT_NAME_WRONG;
3183 if ((check_for_path_chars= check_mysql50_prefix(name)))
3185 name+= MYSQL50_TABLE_NAME_PREFIX_LENGTH;
3186 name_length-= MYSQL50_TABLE_NAME_PREFIX_LENGTH;
3189 if (!preserve_lettercase && lower_case_table_names && name != any_db)
3190 my_casedn_str(files_charset_info, name);
3192 ident_check_status= check_table_name(name, name_length, check_for_path_chars);
3193 if (ident_check_status == IDENT_NAME_WRONG)
3194 my_error(ER_WRONG_DB_NAME, MYF(0), org_name->str);
3195 else if (ident_check_status == IDENT_NAME_TOO_LONG)
3196 my_error(ER_TOO_LONG_IDENT, MYF(0), org_name->str);
3197 return ident_check_status;
3217 enum_ident_name_check check_table_name(
const char *name,
size_t length,
3218 bool check_for_path_chars)
3221 size_t name_length= 0;
3222 const char *end= name+length;
3223 if (!length || length > NAME_LEN)
3224 return IDENT_NAME_WRONG;
3225 #if defined(USE_MB) && defined(USE_MB_IDENT)
3226 bool last_char_is_space= FALSE;
3228 if (name[length-1]==
' ')
3229 return IDENT_NAME_WRONG;
3234 #if defined(USE_MB) && defined(USE_MB_IDENT)
3235 last_char_is_space= my_isspace(system_charset_info, *name);
3236 if (use_mb(system_charset_info))
3238 int len=my_ismbchar(system_charset_info, name, end);
3247 if (check_for_path_chars &&
3248 (*name ==
'/' || *name ==
'\\' || *name ==
'~' || *name == FN_EXTCHAR))
3249 return IDENT_NAME_WRONG;
3253 #if defined(USE_MB) && defined(USE_MB_IDENT)
3254 if (last_char_is_space)
3255 return IDENT_NAME_WRONG;
3256 else if (name_length > NAME_CHAR_LEN)
3257 return IDENT_NAME_TOO_LONG;
3259 return IDENT_NAME_OK;
3263 bool check_column_name(
const char *name)
3266 size_t name_length= 0;
3267 bool last_char_is_space= TRUE;
3271 #if defined(USE_MB) && defined(USE_MB_IDENT)
3272 last_char_is_space= my_isspace(system_charset_info, *name);
3273 if (use_mb(system_charset_info))
3275 int len=my_ismbchar(system_charset_info, name,
3276 name+system_charset_info->mbmaxlen);
3285 last_char_is_space= *name==
' ';
3287 if (*name == NAMES_SEP_CHAR)
3293 return last_char_is_space || (name_length > NAME_CHAR_LEN);
3318 my_bool error= FALSE;
3320 DBUG_ENTER(
"table_check_intact");
3321 DBUG_PRINT(
"info",(
"table: %s expected_count: %d",
3322 table->alias, table_def->count));
3328 if (table->s->fields != table_def->count)
3330 DBUG_PRINT(
"info", (
"Column count has changed, checking the definition"));
3333 if (MYSQL_VERSION_ID > table->s->mysql_version)
3335 report_error(ER_COL_COUNT_DOESNT_MATCH_PLEASE_UPDATE,
3336 ER(ER_COL_COUNT_DOESNT_MATCH_PLEASE_UPDATE),
3337 table->alias, table_def->count, table->s->fields,
3338 static_cast<int>(table->s->mysql_version),
3342 else if (MYSQL_VERSION_ID == table->s->mysql_version)
3344 report_error(ER_COL_COUNT_DOESNT_MATCH_CORRUPTED_V2,
3345 ER(ER_COL_COUNT_DOESNT_MATCH_CORRUPTED_V2),
3346 table->s->db.str, table->s->table_name.str,
3347 table_def->count, table->s->fields);
3358 char buffer[STRING_BUFFER_USUAL_SIZE];
3359 for (i=0 ; i < table_def->count; i++, field_def++)
3361 String sql_type(buffer,
sizeof(buffer), system_charset_info);
3363 if (i < table->s->fields)
3365 Field *field= table->field[
i];
3367 if (strncmp(field->field_name, field_def->name.str,
3368 field_def->name.length))
3375 report_error(0,
"Incorrect definition of table %s.%s: "
3376 "expected column '%s' at position %d, found '%s'.",
3377 table->s->db.str, table->alias, field_def->name.str, i,
3380 field->sql_type(sql_type);
3398 if (strncmp(sql_type.c_ptr_safe(), field_def->type.str,
3399 field_def->type.length - 1))
3401 report_error(0,
"Incorrect definition of table %s.%s: "
3402 "expected column '%s' at position %d to have type "
3403 "%s, found type %s.", table->s->db.str, table->alias,
3404 field_def->name.str, i, field_def->type.str,
3405 sql_type.c_ptr_safe());
3408 else if (field_def->cset.str && !field->has_charset())
3410 report_error(0,
"Incorrect definition of table %s.%s: "
3411 "expected the type of column '%s' at position %d "
3412 "to have character set '%s' but the type has no "
3413 "character set.", table->s->db.str, table->alias,
3414 field_def->name.str, i, field_def->cset.str);
3417 else if (field_def->cset.str &&
3418 strcmp(field->charset()->csname, field_def->cset.str))
3420 report_error(0,
"Incorrect definition of table %s.%s: "
3421 "expected the type of column '%s' at position %d "
3422 "to have character set '%s' but found "
3423 "character set '%s'.", table->s->db.str, table->alias,
3424 field_def->name.str, i, field_def->cset.str,
3425 field->charset()->csname);
3431 report_error(0,
"Incorrect definition of table %s.%s: "
3432 "expected column '%s' at position %d to have type %s "
3433 " but the column is not found.",
3434 table->s->db.str, table->alias,
3435 field_def->name.str, i, field_def->type.str);
3462 uint Wait_for_flush::get_deadlock_weight()
const
3464 return m_deadlock_weight;
3514 if (gvisitor->enter_node(src_ctx))
3517 while ((table= tables_it++))
3519 if (gvisitor->inspect_edge(&table->in_use->mdl_context))
3521 goto end_leave_node;
3526 while ((table= tables_it++))
3528 if (table->in_use->mdl_context.visit_subgraph(gvisitor))
3530 goto end_leave_node;
3537 gvisitor->leave_node(src_ctx);
3569 uint deadlock_weight)
3573 MDL_wait::enum_wait_status wait_status;
3581 DBUG_ASSERT(version != refresh_version && ref_count != 0);
3594 &stage_waiting_for_table_flush);
3622 switch (wait_status)
3624 case MDL_wait::GRANTED:
3626 case MDL_wait::VICTIM:
3627 my_error(ER_LOCK_DEADLOCK, MYF(0));
3629 case MDL_wait::TIMEOUT:
3630 my_error(ER_LOCK_WAIT_TIMEOUT, MYF(0));
3632 case MDL_wait::KILLED:
3654 DBUG_ASSERT(s->ref_count > 0 || s->tmp_table != NO_TMP_TABLE);
3656 if (thd->lex->need_correct_ident())
3657 alias_name_used= my_strcasecmp(table_alias_charset,
3661 if (strcmp(alias, tl->alias))
3663 uint length= (uint) strlen(tl->alias)+1;
3664 alias= (
char*) my_realloc((
char*) alias, length, MYF(MY_WME));
3665 memcpy((
char*) alias, tl->alias, length);
3668 tablenr= thd->current_tablenr++;
3676 status= STATUS_GARBAGE | STATUS_NOT_FOUND;
3678 fulltext_searched= 0;
3679 file->ft_handler= 0;
3680 reginfo.impossible_range= 0;
3683 DBUG_ASSERT(!auto_increment_field_not_null);
3684 auto_increment_field_not_null= FALSE;
3686 pos_in_table_list= tl;
3688 clear_column_bitmaps();
3693 DBUG_ASSERT(!file->extra(HA_EXTRA_IS_ATTACHED_CHILDREN));
3714 bool TABLE::fill_item_list(
List<Item> *item_list)
const
3720 for (
Field **ptr= field; *ptr; ptr++)
3723 if (!item || item_list->push_back(item))
3744 void TABLE::reset_item_list(
List<Item> *item_list)
const
3747 for (
Field **ptr= field; *ptr; ptr++)
3750 DBUG_ASSERT(item_field != 0);
3771 class st_select_lex *select)
3773 DBUG_ASSERT(belongs_to && select);
3778 if (join_nest == NULL)
3782 join_nest->nested_join=
3785 join_nest->db= (
char *)
"";
3786 join_nest->db_length= 0;
3787 join_nest->table_name= (
char *)
"";
3788 join_nest->table_name_length= 0;
3789 join_nest->alias= (
char *)alias;
3791 join_nest->embedding= embedding;
3792 join_nest->join_list= belongs_to;
3793 join_nest->select_lex= select;
3795 join_nest->nested_join->join_list.empty();
3807 void TABLE_LIST::calc_md5(
char *buffer)
3809 uchar digest[MD5_HASH_SIZE];
3811 select_stmt.length);
3812 array_to_hex((
char *) buffer, digest, MD5_HASH_SIZE);
3843 if ((tbl= merge_underlying_list))
3849 if (tbl->merge_underlying_list)
3851 DBUG_ASSERT(tbl->view &&
3859 }
while ((tbl= tbl->next_local));
3861 if (!multitable_view)
3863 table= merge_underlying_list->table;
3868 if (!merge_underlying_list->updatable)
3870 schema_table= merge_underlying_list->schema_table;
3893 bool TABLE_LIST::setup_underlying(THD *thd)
3895 DBUG_ENTER(
"TABLE_LIST::setup_underlying");
3897 if (!field_translation && merge_underlying_list)
3900 SELECT_LEX *select= &view->select_lex;
3904 uint field_count= 0;
3911 for (tbl= merge_underlying_list; tbl; tbl= tbl->next_local)
3913 if (tbl->merge_underlying_list &&
3914 tbl->setup_underlying(thd))
3924 alloc(select->item_list.elements *
3930 while ((item= it++))
3932 transl[field_count].name= item->item_name.
ptr();
3933 transl[field_count++].item= item;
3935 field_translation= transl;
3936 field_translation_end= transl + field_count;
3940 if (view->select_lex.ftfunc_list->elements)
3943 SELECT_LEX *current_select= thd->lex->current_select;
3945 li(*(view->select_lex.ftfunc_list));
3947 current_select->ftfunc_list->push_front(ifm);
3972 bool TABLE_LIST::prep_where(THD *thd,
Item **conds,
3973 bool no_where_clause)
3975 DBUG_ENTER(
"TABLE_LIST::prep_where");
3977 for (
TABLE_LIST *tbl= merge_underlying_list; tbl; tbl= tbl->next_local)
3979 if (tbl->view && tbl->prep_where(thd, conds, no_where_clause))
3985 if (where && !where_processed)
3997 Switch_resolve_place SRP(&thd->lex->current_select->resolve_place,
3998 st_select_lex::RESOLVE_NONE,
3999 effective_with_check != VIEW_CHECK_NONE);
4001 if (where->fix_fields(thd, &where))
4009 if (!no_where_clause)
4013 Prepared_stmt_arena_holder ps_arena_holder(thd);
4016 for (; tbl; tbl= tbl->embedding)
4018 if (tbl->outer_join)
4026 tbl->set_join_cond(and_conds(tbl->join_cond(),
4033 where_processed= TRUE;
4061 merge_on_conds(THD *thd,
TABLE_LIST *table,
bool is_cascaded)
4063 DBUG_ENTER(
"merge_on_conds");
4066 DBUG_PRINT(
"info", (
"alias: %s", table->alias));
4067 if (table->join_cond())
4069 if (!table->nested_join)
4074 if (tbl->view && !is_cascaded)
4076 cond= and_conds(cond, merge_on_conds(thd, tbl, is_cascaded));
4108 bool TABLE_LIST::prep_check_option(THD *thd, uint8 check_opt_type)
4110 DBUG_ENTER(
"TABLE_LIST::prep_check_option");
4111 bool is_cascaded= check_opt_type == VIEW_CHECK_CASCADED;
4113 for (
TABLE_LIST *tbl= merge_underlying_list; tbl; tbl= tbl->next_local)
4116 if (tbl->view && tbl->prep_check_option(thd, (is_cascaded ?
4117 VIEW_CHECK_CASCADED :
4122 if (check_opt_type && !check_option_processed)
4124 Prepared_stmt_arena_holder ps_arena_holder(thd);
4128 DBUG_ASSERT(where->fixed);
4133 for (
TABLE_LIST *tbl= merge_underlying_list; tbl; tbl= tbl->next_local)
4135 if (tbl->check_option)
4136 check_option= and_conds(check_option, tbl->check_option);
4139 check_option= and_conds(check_option,
4140 merge_on_conds(thd,
this, is_cascaded));
4142 check_option_processed= TRUE;
4147 const char *save_where= thd->where;
4148 thd->where=
"check option";
4149 if ((!check_option->fixed &&
4150 check_option->fix_fields(thd, &check_option)) ||
4151 check_option->check_cols(1))
4155 thd->where= save_where;
4176 if (thd->killed || thd->get_internal_handler())
4179 DBUG_ASSERT(thd->is_error());
4181 switch (thd->get_stmt_da()->sql_errno()) {
4182 case ER_BAD_FIELD_ERROR:
4183 case ER_SP_DOES_NOT_EXIST:
4184 case ER_FUNC_INEXISTENT_NAME_COLLISION:
4185 case ER_PROCACCESS_DENIED_ERROR:
4186 case ER_COLUMNACCESS_DENIED_ERROR:
4187 case ER_TABLEACCESS_DENIED_ERROR:
4188 case ER_TABLE_NOT_LOCKED:
4189 case ER_NO_SUCH_TABLE:
4193 my_error(ER_VIEW_INVALID, MYF(0),
4194 top->view_db.str, top->view_name.str);
4198 case ER_NO_DEFAULT_FOR_FIELD:
4203 my_error(ER_NO_DEFAULT_FOR_VIEW_FIELD, MYF(0),
4204 top->view_db.str, top->view_name.str);
4227 if (table == table_to_find && merge_underlying_list == 0)
4230 for (
TABLE_LIST *tbl= merge_underlying_list; tbl; tbl= tbl->next_local)
4233 if ((result= tbl->find_underlying_table(table_to_find)))
4246 void TABLE_LIST::cleanup_items()
4248 if (!field_translation)
4252 transl < field_translation_end;
4271 int TABLE_LIST::view_check_option(THD *thd,
bool ignore_failure)
const
4273 if (check_option && check_option->val_int() == 0)
4278 push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
4279 ER_VIEW_CHECK_FAILED, ER(ER_VIEW_CHECK_FAILED),
4280 main_view->view_db.str, main_view->view_name.str);
4281 return(VIEW_CHECK_SKIP);
4283 my_error(ER_VIEW_CHECK_FAILED, MYF(0), main_view->view_db.str,
4284 main_view->view_name.str);
4285 return(VIEW_CHECK_ERROR);
4287 return(VIEW_CHECK_OK);
4308 bool TABLE_LIST::check_single_table(
TABLE_LIST **table_arg,
4312 for (
TABLE_LIST *tbl= merge_underlying_list; tbl; tbl= tbl->next_local)
4316 if (tbl->table->map & map)
4321 tbl->check_option= view_arg->check_option;
4324 else if (tbl->check_single_table(table_arg, map, view_arg))
4343 bool TABLE_LIST::set_insert_values(
MEM_ROOT *mem_root)
4347 if (!table->insert_values &&
4348 !(table->insert_values= (uchar *)alloc_root(mem_root,
4349 table->s->rec_buff_length)))
4354 DBUG_ASSERT(view && merge_underlying_list);
4355 for (
TABLE_LIST *tbl= merge_underlying_list; tbl; tbl= tbl->next_local)
4356 if (tbl->set_insert_values(mem_root))
4379 bool TABLE_LIST::is_leaf_for_name_resolution()
4381 return (view || is_natural_join || is_join_columns_complete ||
4409 TABLE_LIST *TABLE_LIST::first_leaf_for_name_resolution()
4413 LINT_INIT(cur_table_ref);
4415 if (is_leaf_for_name_resolution())
4417 DBUG_ASSERT(nested_join);
4419 for (cur_nested_join= nested_join;
4421 cur_nested_join= cur_table_ref->nested_join)
4424 cur_table_ref= it++;
4431 if (!(cur_table_ref->outer_join & JOIN_TYPE_RIGHT))
4434 while ((next= it++))
4435 cur_table_ref= next;
4437 if (cur_table_ref->is_leaf_for_name_resolution())
4440 return cur_table_ref;
4467 TABLE_LIST *TABLE_LIST::last_leaf_for_name_resolution()
4472 if (is_leaf_for_name_resolution())
4474 DBUG_ASSERT(nested_join);
4476 for (cur_nested_join= nested_join;
4478 cur_nested_join= cur_table_ref->nested_join)
4480 cur_table_ref= cur_nested_join->join_list.head();
4486 if ((cur_table_ref->outer_join & JOIN_TYPE_RIGHT))
4490 cur_table_ref= it++;
4491 while ((next= it++))
4492 cur_table_ref= next;
4494 if (cur_table_ref->is_leaf_for_name_resolution())
4497 return cur_table_ref;
4509 void TABLE_LIST::register_want_access(ulong want_access)
4512 want_access&= ~SHOW_VIEW_ACL;
4519 for (
TABLE_LIST *tbl= merge_underlying_list; tbl; tbl= tbl->next_local)
4520 tbl->register_want_access(want_access);
4536 #ifndef NO_EMBEDDED_ACCESS_CHECKS
4537 bool TABLE_LIST::prepare_view_securety_context(THD *thd)
4539 DBUG_ENTER(
"TABLE_LIST::prepare_view_securety_context");
4540 DBUG_PRINT(
"enter", (
"table: %s", alias));
4542 DBUG_ASSERT(!prelocking_placeholder && view);
4545 DBUG_PRINT(
"info", (
"This table is suid view => load contest"));
4546 DBUG_ASSERT(view && view_sctx);
4547 if (acl_getroot(view_sctx, definer.user.str, definer.host.str,
4548 definer.host.str, thd->db))
4550 if ((thd->lex->sql_command == SQLCOM_SHOW_CREATE) ||
4551 (thd->lex->sql_command == SQLCOM_SHOW_FIELDS))
4553 push_warning_printf(thd, Sql_condition::WARN_LEVEL_NOTE,
4555 ER(ER_NO_SUCH_USER),
4556 definer.user.str, definer.host.str);
4560 if (thd->security_ctx->master_access & SUPER_ACL)
4562 my_error(ER_NO_SUCH_USER, MYF(0), definer.user.str, definer.host.str);
4567 if (thd->password == 2)
4568 my_error(ER_ACCESS_DENIED_NO_PASSWORD_ERROR, MYF(0),
4569 thd->security_ctx->priv_user,
4570 thd->security_ctx->priv_host);
4572 my_error(ER_ACCESS_DENIED_ERROR, MYF(0),
4573 thd->security_ctx->priv_user,
4574 thd->security_ctx->priv_host,
4575 (thd->password ? ER(ER_YES) : ER(ER_NO)));
4595 #ifndef NO_EMBEDDED_ACCESS_CHECKS
4596 Security_context *TABLE_LIST::find_view_security_context(THD *thd)
4598 Security_context *sctx;
4600 DBUG_ENTER(
"TABLE_LIST::find_view_security_context");
4603 while (upper_view && !upper_view->view_suid)
4605 DBUG_ASSERT(!upper_view->prelocking_placeholder);
4606 upper_view= upper_view->referencing_view;
4610 DBUG_PRINT(
"info", (
"Securety context of view %s will be used",
4611 upper_view->alias));
4612 sctx= upper_view->view_sctx;
4617 DBUG_PRINT(
"info", (
"Current global context will be used"));
4618 sctx= thd->security_ctx;
4637 bool TABLE_LIST::prepare_security(THD *thd)
4641 DBUG_ENTER(
"TABLE_LIST::prepare_security");
4642 #ifndef NO_EMBEDDED_ACCESS_CHECKS
4643 Security_context *save_security_ctx= thd->security_ctx;
4645 DBUG_ASSERT(!prelocking_placeholder);
4646 if (prepare_view_securety_context(thd))
4648 thd->security_ctx= find_view_security_context(thd);
4649 opt_trace_disable_if_no_security_context_access(thd);
4652 DBUG_ASSERT(tbl->referencing_view);
4653 char *local_db, *local_table_name;
4656 local_db= tbl->view_db.str;
4657 local_table_name= tbl->view_name.str;
4662 local_table_name= tbl->table_name;
4664 fill_effective_table_privileges(thd, &tbl->grant, local_db,
4667 tbl->table->grant= grant;
4669 thd->security_ctx= save_security_ctx;
4681 DBUG_ASSERT(tab->field_translation);
4682 view_field= field_param;
4689 Natural_join_column::Natural_join_column(
Item_field *field_param,
4692 DBUG_ASSERT(tab->table == field_param->field->table);
4693 table_field= field_param;
4700 const char *Natural_join_column::name()
4704 DBUG_ASSERT(table_field == NULL);
4705 return view_field->name;
4708 return table_field->field_name;
4712 Item *Natural_join_column::create_item(THD *thd)
4716 DBUG_ASSERT(table_field == NULL);
4717 SELECT_LEX *select= thd->lex->current_select;
4718 return create_view_field(thd, table_ref, &view_field->item,
4719 view_field->name, &select->context);
4725 Field *Natural_join_column::field()
4729 DBUG_ASSERT(table_field == NULL);
4732 return table_field->field;
4736 const char *Natural_join_column::table_name()
4738 DBUG_ASSERT(table_ref);
4739 return table_ref->alias;
4743 const char *Natural_join_column::db_name()
4746 return table_ref->view_db.str;
4753 DBUG_ASSERT(!strcmp(table_ref->db,
4754 table_ref->table->s->db.str) ||
4755 (table_ref->schema_table &&
4756 is_infoschema_db(table_ref->table->s->db.str,
4757 table_ref->table->s->db.length)));
4758 return table_ref->db;
4765 return &(table_ref->grant);
4766 return &(table_ref->table->grant);
4770 void Field_iterator_view::set(
TABLE_LIST *table)
4772 DBUG_ASSERT(table->field_translation);
4774 ptr= table->field_translation;
4775 array_end= table->field_translation_end;
4779 const char *Field_iterator_table::name()
4781 return (*ptr)->field_name;
4785 Item *Field_iterator_table::create_item(THD *thd)
4787 SELECT_LEX *select= thd->lex->current_select;
4794 if (item && !thd->lex->in_sum_func &&
4795 select->cur_pos_in_all_fields != SELECT_LEX::ALL_FIELDS_UNDEF_POS)
4797 if (thd->variables.sql_mode & MODE_ONLY_FULL_GROUP_BY)
4800 select->set_non_agg_field_used(
true);
4802 if (thd->lex->current_select->with_sum_func &&
4803 !thd->lex->current_select->group_list.elements)
4804 item->maybe_null=
true;
4810 const char *Field_iterator_view::name()
4816 Item *Field_iterator_view::create_item(THD *thd)
4818 SELECT_LEX *select= thd->lex->current_select;
4819 return create_view_field(thd, view, &ptr->item, ptr->name,
4827 bool save_wrapper= thd->lex->select_lex.no_wrap_view_item;
4828 Item *field= *field_ref;
4829 DBUG_ENTER(
"create_view_field");
4831 if (view->schema_table_reformed)
4838 DBUG_ASSERT(field && field->fixed);
4843 thd->lex->current_select->no_wrap_view_item= TRUE;
4846 if (field->fix_fields(thd, field_ref))
4848 thd->lex->current_select->no_wrap_view_item= save_wrapper;
4853 thd->lex->current_select->no_wrap_view_item= save_wrapper;
4859 view->alias, view->table_name, name);
4864 void Field_iterator_natural_join::set(
TABLE_LIST *table_ref)
4866 DBUG_ASSERT(table_ref->join_columns);
4867 column_ref_it.init(*(table_ref->join_columns));
4868 cur_column_ref= column_ref_it++;
4872 void Field_iterator_natural_join::next()
4874 cur_column_ref= column_ref_it++;
4875 DBUG_ASSERT(!cur_column_ref || ! cur_column_ref->table_field ||
4876 cur_column_ref->table_ref->table ==
4877 cur_column_ref->table_field->field->table);
4881 void Field_iterator_table_ref::set_field_iterator()
4883 DBUG_ENTER(
"Field_iterator_table_ref::set_field_iterator");
4891 if (table_ref->is_join_columns_complete)
4894 DBUG_ASSERT(table_ref->is_natural_join ||
4895 table_ref->nested_join ||
4896 (table_ref->join_columns &&
4898 ((table_ref->field_translation &&
4899 table_ref->join_columns->elements ==
4900 (ulong)(table_ref->field_translation_end -
4901 table_ref->field_translation)) ||
4903 (!table_ref->field_translation &&
4904 table_ref->join_columns->elements ==
4905 table_ref->table->s->fields))));
4906 field_it= &natural_join_it;
4907 DBUG_PRINT(
"info",(
"field_it for '%s' is Field_iterator_natural_join",
4911 else if (table_ref->field_translation)
4913 DBUG_ASSERT(table_ref->view &&
4915 field_it= &view_field_it;
4916 DBUG_PRINT(
"info", (
"field_it for '%s' is Field_iterator_view",
4922 DBUG_ASSERT(table_ref->table || table_ref->view);
4923 field_it= &table_field_it;
4924 DBUG_PRINT(
"info", (
"field_it for '%s' is Field_iterator_table",
4927 field_it->set(table_ref);
4932 void Field_iterator_table_ref::set(
TABLE_LIST *table)
4935 first_leaf= table->first_leaf_for_name_resolution();
4936 last_leaf= table->last_leaf_for_name_resolution();
4937 DBUG_ASSERT(first_leaf && last_leaf);
4938 table_ref= first_leaf;
4939 set_field_iterator();
4943 void Field_iterator_table_ref::next()
4951 if (field_it->end_of_fields() && table_ref != last_leaf)
4953 table_ref= table_ref->next_name_resolution_table;
4954 DBUG_ASSERT(table_ref);
4955 set_field_iterator();
4960 const char *Field_iterator_table_ref::get_table_name()
4962 if (table_ref->view)
4963 return table_ref->view_name.str;
4964 else if (table_ref->is_natural_join)
4965 return natural_join_it.column_ref()->table_name();
4967 DBUG_ASSERT(!strcmp(table_ref->table_name,
4968 table_ref->table->s->table_name.str));
4969 return table_ref->table_name;
4973 const char *Field_iterator_table_ref::get_db_name()
4975 if (table_ref->view)
4976 return table_ref->view_db.str;
4977 else if (table_ref->is_natural_join)
4978 return natural_join_it.column_ref()->db_name();
4985 DBUG_ASSERT(!strcmp(table_ref->db, table_ref->table->s->db.str) ||
4986 (table_ref->schema_table &&
4987 is_infoschema_db(table_ref->table->s->db.str,
4988 table_ref->table->s->db.length)));
4990 return table_ref->db;
4994 GRANT_INFO *Field_iterator_table_ref::grant()
4996 if (table_ref->view)
4997 return &(table_ref->grant);
4998 else if (table_ref->is_natural_join)
4999 return natural_join_it.column_ref()->grant();
5000 return &(table_ref->table->grant);
5042 Field_iterator_table_ref::get_or_create_column_ref(THD *thd,
TABLE_LIST *parent_table_ref)
5045 bool is_created= TRUE;
5047 TABLE_LIST *add_table_ref= parent_table_ref ?
5048 parent_table_ref : table_ref;
5049 LINT_INIT(field_count);
5051 if (field_it == &table_field_it)
5054 Field *tmp_field= table_field_it.field();
5056 new Item_field(thd, &thd->lex->current_select->context, tmp_field);
5060 field_count= table_ref->table->s->fields;
5062 else if (field_it == &view_field_it)
5067 field_count= table_ref->field_translation_end -
5068 table_ref->field_translation;
5077 DBUG_ASSERT(table_ref->is_join_columns_complete);
5079 nj_col= natural_join_it.column_ref();
5080 DBUG_ASSERT(nj_col);
5082 DBUG_ASSERT(!nj_col->table_field ||
5083 nj_col->table_ref->table == nj_col->table_field->field->table);
5093 DBUG_ASSERT(!add_table_ref->is_join_columns_complete);
5094 if (!add_table_ref->join_columns)
5099 add_table_ref->is_join_columns_complete= FALSE;
5101 add_table_ref->join_columns->push_back(nj_col);
5109 if (!parent_table_ref &&
5110 add_table_ref->join_columns->elements == field_count)
5111 add_table_ref->is_join_columns_complete= TRUE;
5135 Field_iterator_table_ref::get_natural_column_ref()
5139 DBUG_ASSERT(field_it == &natural_join_it);
5145 nj_col= natural_join_it.column_ref();
5146 DBUG_ASSERT(nj_col &&
5147 (!nj_col->table_field ||
5148 nj_col->table_ref->table == nj_col->table_field->field->table));
5158 void TABLE::clear_column_bitmaps()
5165 memset(def_read_set.bitmap, 0, s->column_bitmap_size*2);
5166 column_bitmaps_set(&def_read_set, &def_write_set);
5184 DBUG_ENTER(
"TABLE::prepare_for_position");
5186 if ((file->ha_table_flags() & HA_PRIMARY_KEY_REQUIRED_FOR_POSITION) &&
5187 s->primary_key < MAX_KEY)
5189 mark_columns_used_by_index_no_reset(s->primary_key, read_set);
5191 file->column_bitmaps_signal();
5206 void TABLE::mark_columns_used_by_index(uint
index)
5209 DBUG_ENTER(
"TABLE::mark_columns_used_by_index");
5212 bitmap_clear_all(bitmap);
5213 mark_columns_used_by_index_no_reset(index, bitmap);
5214 column_bitmaps_set(bitmap, bitmap);
5223 void TABLE::mark_columns_used_by_index_no_reset(uint
index,
5226 KEY_PART_INFO *key_part= key_info[
index].key_part;
5227 KEY_PART_INFO *key_part_end= (key_part +
5229 for (;key_part != key_part_end; key_part++)
5230 bitmap_set_bit(bitmap, key_part->fieldnr-1);
5242 void TABLE::mark_auto_increment_column()
5244 DBUG_ASSERT(found_next_number_field);
5249 bitmap_set_bit(read_set, found_next_number_field->field_index);
5250 bitmap_set_bit(write_set, found_next_number_field->field_index);
5251 if (s->next_number_keypart)
5252 mark_columns_used_by_index_no_reset(s->next_number_index, read_set);
5253 file->column_bitmaps_signal();
5275 void TABLE::mark_columns_needed_for_delete()
5281 if (file->ha_table_flags() & HA_REQUIRES_KEY_COLUMNS_FOR_DELETE)
5284 for (reg_field= field ; *reg_field ; reg_field++)
5286 if ((*reg_field)->flags & PART_KEY_FLAG)
5287 bitmap_set_bit(read_set, (*reg_field)->field_index);
5289 file->column_bitmaps_signal();
5291 if (file->ha_table_flags() & HA_PRIMARY_KEY_REQUIRED_FOR_DELETE)
5299 if (s->primary_key == MAX_KEY)
5306 if (!(mysql_bin_log.is_open() && in_use &&
5307 in_use->is_current_stmt_binlog_format_row()))
5308 file->use_hidden_primary_key();
5311 mark_columns_used_by_index_no_reset(s->primary_key, read_set);
5313 file->column_bitmaps_signal();
5344 DBUG_ENTER(
"mark_columns_needed_for_update");
5346 if (file->ha_table_flags() & HA_REQUIRES_KEY_COLUMNS_FOR_DELETE)
5350 for (reg_field= field ; *reg_field ; reg_field++)
5353 if (merge_keys.is_overlapping((*reg_field)->part_of_key))
5354 bitmap_set_bit(read_set, (*reg_field)->field_index);
5356 file->column_bitmaps_signal();
5359 if (file->ha_table_flags() & HA_PRIMARY_KEY_REQUIRED_FOR_DELETE)
5366 if (s->primary_key == MAX_KEY)
5373 if (!(mysql_bin_log.is_open() && in_use &&
5374 in_use->is_current_stmt_binlog_format_row()))
5375 file->use_hidden_primary_key();
5378 mark_columns_used_by_index_no_reset(s->primary_key, read_set);
5380 file->column_bitmaps_signal();
5418 DBUG_ENTER(
"mark_columns_per_binlog_row_image");
5419 DBUG_ASSERT(read_set->bitmap);
5420 DBUG_ASSERT(write_set->bitmap);
5426 if ((mysql_bin_log.is_open() && in_use &&
5427 in_use->is_current_stmt_binlog_format_row() &&
5428 !ha_check_storage_engine_flag(s->db_type(), HTON_NO_BINLOG_ROW_OPT)))
5431 THD *thd= current_thd;
5434 if (s->primary_key >= MAX_KEY)
5435 bitmap_set_all(read_set);
5437 switch (thd->variables.binlog_row_image)
5439 case BINLOG_ROW_IMAGE_FULL:
5440 if (s->primary_key < MAX_KEY)
5441 bitmap_set_all(read_set);
5442 bitmap_set_all(write_set);
5444 case BINLOG_ROW_IMAGE_NOBLOB:
5446 for (
Field **ptr=field ; *ptr ; ptr++)
5457 if ((s->primary_key < MAX_KEY) &&
5458 ((field->flags & PRI_KEY_FLAG) ||
5459 (field->type() != MYSQL_TYPE_BLOB)))
5460 bitmap_set_bit(read_set, field->field_index);
5462 if (field->type() != MYSQL_TYPE_BLOB)
5463 bitmap_set_bit(write_set, field->field_index);
5466 case BINLOG_ROW_IMAGE_MINIMAL:
5468 if (s->primary_key < MAX_KEY)
5469 mark_columns_used_by_index_no_reset(s->primary_key, read_set);
5475 file->column_bitmaps_signal();
5498 DBUG_ASSERT(!s->keys);
5499 max_keys= key_count;
5500 if (!(key_info= s->key_info=
5501 (
KEY*) alloc_root(&mem_root,
sizeof(
KEY)*max_keys)))
5504 memset(key_info, 0,
sizeof(
KEY)*max_keys);
5530 DBUG_ASSERT(!created && s->keys < max_keys && key_parts);
5532 KEY* cur_key= key_info + s->keys;
5535 bool key_start= TRUE;
5536 uint field_count= 0;
5538 KEY_PART_INFO* key_part_info;
5541 for (i= 0, reg_field=field ; *reg_field; i++, reg_field++)
5543 if (key_parts->is_set(i))
5547 DBUG_ASSERT(!((*reg_field)->flags & BLOB_FLAG));
5555 key_len+= tkp.store_length;
5556 if (key_len > MI_MAX_KEY_LENGTH)
5564 const uint key_part_count= key_parts->bits_set();
5567 size_t key_buf_size=
sizeof(KEY_PART_INFO) * key_part_count +
5568 sizeof(ulong) * key_part_count;
5569 key_buf= (uchar*) alloc_root(&mem_root, key_buf_size);
5573 memset(key_buf, 0, key_buf_size);
5574 cur_key->key_part= key_part_info= (KEY_PART_INFO*) key_buf;
5577 s->key_parts+= key_part_count;
5579 cur_key->algorithm= HA_KEY_ALG_BTREE;
5580 cur_key->
name= key_name;
5582 cur_key->
rec_per_key= (ulong*) (key_buf +
sizeof(KEY_PART_INFO) * key_part_count);
5583 cur_key->table=
this;
5585 if (field_count == key_part_count)
5586 covering_keys.set_bit(s->keys);
5588 keys_in_use_for_group_by.set_bit(s->keys);
5589 keys_in_use_for_order_by.set_bit(s->keys);
5590 for (i= 0, reg_field=field ; *reg_field; i++, reg_field++)
5592 if (!(key_parts->is_set(i)))
5596 (*reg_field)->key_start.set_bit(s->keys);
5598 (*reg_field)->part_of_key.set_bit(s->keys);
5599 (*reg_field)->part_of_sortkey.set_bit(s->keys);
5600 (*reg_field)->flags|= PART_KEY_FLAG;
5604 set_if_bigger(s->max_key_length, cur_key->
key_length);
5624 void TABLE::use_index(
int key_to_save)
5626 DBUG_ASSERT(!created && s->keys && key_to_save < (
int)s->keys);
5629 for (
int i= 0; i < (int)s->keys; i++)
5633 for (kp= key_info[i].key_part, j= 0;
5637 if (i == key_to_save)
5639 if (kp->field->key_start.is_set(i))
5640 kp->field->key_start.set_prefix(1);
5641 kp->field->part_of_key.set_prefix(1);
5642 kp->field->part_of_sortkey.set_prefix(1);
5646 kp->field->key_start.clear_all();
5647 kp->field->part_of_key.clear_all();
5648 kp->field->part_of_sortkey.clear_all();
5653 if (key_to_save < 0)
5656 key_info= s->key_info= 0;
5659 covering_keys.clear_all();
5660 keys_in_use_for_group_by.clear_all();
5661 keys_in_use_for_order_by.clear_all();
5666 if (key_to_save > 0)
5667 key_info[0]= key_info[key_to_save];
5670 if (covering_keys.is_set(key_to_save))
5671 covering_keys.set_prefix(1);
5673 covering_keys.clear_all();
5674 keys_in_use_for_group_by.set_prefix(1);
5675 keys_in_use_for_order_by.set_prefix(1);
5688 void TABLE::mark_columns_needed_for_insert()
5702 if (found_next_number_field)
5703 mark_auto_increment_column();
5714 void TABLE_LIST::reinit_before_use(THD *thd)
5730 table_name_length= 0;
5732 else if (schema_table_name)
5734 table_name= schema_table_name;
5735 table_name_length= strlen(schema_table_name);
5739 schema_table_state= NOT_PROCESSED;
5745 embedded= parent_embedding;
5746 if (embedded->prep_join_cond)
5749 parent_embedding= embedded->embedding;
5751 while (parent_embedding &&
5752 parent_embedding->nested_join->join_list.head() == embedded);
5754 mdl_request.
ticket= NULL;
5772 return (select_lex ? select_lex->master_unit()->item : 0);
5832 bool TABLE_LIST::process_index_hints(
TABLE *tbl)
5835 tbl->keys_in_use_for_query= tbl->keys_in_use_for_group_by=
5836 tbl->keys_in_use_for_order_by= tbl->s->keys_in_use;
5842 key_map index_join[INDEX_HINT_FORCE + 1];
5843 key_map index_order[INDEX_HINT_FORCE + 1];
5844 key_map index_group[INDEX_HINT_FORCE + 1];
5846 bool have_empty_use_join= FALSE, have_empty_use_order= FALSE,
5847 have_empty_use_group= FALSE;
5851 while ((hint= iter++))
5856 if (hint->type == INDEX_HINT_USE && !hint->key_name.str)
5858 if (hint->clause & INDEX_HINT_MASK_JOIN)
5860 index_join[hint->type].clear_all();
5861 have_empty_use_join= TRUE;
5863 if (hint->clause & INDEX_HINT_MASK_ORDER)
5865 index_order[hint->type].clear_all();
5866 have_empty_use_order= TRUE;
5868 if (hint->clause & INDEX_HINT_MASK_GROUP)
5870 index_group[hint->type].clear_all();
5871 have_empty_use_group= TRUE;
5880 if (tbl->s->keynames.type_names == 0 ||
5881 (pos= find_type(&tbl->s->keynames, hint->key_name.str,
5882 hint->key_name.length, 1)) <= 0)
5884 my_error(ER_KEY_DOES_NOT_EXITS, MYF(0), hint->key_name.str, alias);
5891 if (hint->clause & INDEX_HINT_MASK_JOIN)
5892 index_join[hint->type].set_bit (pos);
5893 if (hint->clause & INDEX_HINT_MASK_ORDER)
5894 index_order[hint->type].set_bit (pos);
5895 if (hint->clause & INDEX_HINT_MASK_GROUP)
5896 index_group[hint->type].set_bit (pos);
5900 if ((!index_join[INDEX_HINT_FORCE].is_clear_all() ||
5901 !index_order[INDEX_HINT_FORCE].is_clear_all() ||
5902 !index_group[INDEX_HINT_FORCE].is_clear_all()) &&
5903 (!index_join[INDEX_HINT_USE].is_clear_all() || have_empty_use_join ||
5904 !index_order[INDEX_HINT_USE].is_clear_all() || have_empty_use_order ||
5905 !index_group[INDEX_HINT_USE].is_clear_all() || have_empty_use_group))
5907 my_error(ER_WRONG_USAGE, MYF(0), index_hint_type_name[INDEX_HINT_USE],
5908 index_hint_type_name[INDEX_HINT_FORCE]);
5913 if (!index_order[INDEX_HINT_FORCE].is_clear_all())
5916 index_order[INDEX_HINT_USE].merge(index_order[INDEX_HINT_FORCE]);
5919 if (!index_group[INDEX_HINT_FORCE].is_clear_all())
5922 index_group[INDEX_HINT_USE].merge(index_group[INDEX_HINT_FORCE]);
5930 if (!index_join[INDEX_HINT_FORCE].is_clear_all() ||
5933 tbl->force_index= TRUE;
5934 index_join[INDEX_HINT_USE].merge(index_join[INDEX_HINT_FORCE]);
5938 if (!index_join[INDEX_HINT_USE].is_clear_all() || have_empty_use_join)
5939 tbl->keys_in_use_for_query.intersect(index_join[INDEX_HINT_USE]);
5940 if (!index_order[INDEX_HINT_USE].is_clear_all() || have_empty_use_order)
5941 tbl->keys_in_use_for_order_by.intersect (index_order[INDEX_HINT_USE]);
5942 if (!index_group[INDEX_HINT_USE].is_clear_all() || have_empty_use_group)
5943 tbl->keys_in_use_for_group_by.intersect (index_group[INDEX_HINT_USE]);
5946 tbl->keys_in_use_for_query.subtract (index_join[INDEX_HINT_IGNORE]);
5947 tbl->keys_in_use_for_order_by.subtract (index_order[INDEX_HINT_IGNORE]);
5948 tbl->keys_in_use_for_group_by.subtract (index_group[INDEX_HINT_IGNORE]);
5952 tbl->covering_keys.intersect(tbl->keys_in_use_for_query);
5957 size_t max_row_length(
TABLE *table,
const uchar *data)
5960 size_t length= table_s->reclength + 2 * table_s->fields;
5961 uint *
const beg= table_s->blob_field;
5962 uint *
const end= beg + table_s->blob_fields;
5964 for (uint *ptr= beg ; ptr != end ; ++ptr)
5967 length+= blob->get_length((
const uchar*)
5968 (data + blob->offset(table->record[0]))) +
5980 void init_mdl_requests(
TABLE_LIST *table_list)
5982 for ( ; table_list ; table_list= table_list->next_global)
5983 table_list->mdl_request.
init(MDL_key::TABLE,
5984 table_list->db, table_list->table_name,
5985 table_list->lock_type >= TL_WRITE_ALLOW_WRITE ?
5986 MDL_SHARED_WRITE : MDL_SHARED_READ,
5996 return get_unit()->get_result()->estimated_rowcount <= 1;
6017 table->file->stats.records=
derived->get_result()->estimated_rowcount;
6019 error= table->file->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
6103 table_map ref_by_tbl)
6110 while ((entry= ki++))
6116 if (entry->referenced_by & ref_by_tbl)
6125 if (!entry->referenced_by)
6132 THD *thd= field->table->in_use;
6134 entry=
new (thd->stmt_arena->mem_root)
Derived_key();
6137 entry->referenced_by= ref_by_tbl;
6138 entry->used_fields.clear_all();
6139 if (derived_key_list.push_back(entry, thd->stmt_arena->mem_root))
6141 field->table->max_keys++;
6144 if (entry->used_fields.bits_set() < MAX_REF_PARTS)
6146 field->part_of_key.set_bit(key - 1);
6147 field->flags|= PART_KEY_FLAG;
6148 entry->used_fields.set_bit(field->field_index);
6174 bool TABLE_LIST::update_derived_keys(
Field *field,
Item **values,
6178 if (field->table->in_use->lex->is_ps_or_view_context_analysis() ||
6179 field->flags & BLOB_FLAG)
6183 if (derived_key_list.elements == 0)
6185 table->keys_in_use_for_query.set_all();
6186 table->s->uniques= 0;
6189 for (uint i= 0; i < num_values; i++)
6191 table_map tables= values[
i]->used_tables() & ~PSEUDO_TABLE_BITS;
6192 if (!tables || values[i]->real_item()->
type() != Item::FIELD_ITEM)
6194 for (table_map tbl= 1; tables >= tbl; tbl<<= 1)
6196 if (! (tables & tbl))
6198 if (add_derived_key(derived_key_list, field, tbl))
6203 if (add_derived_key(derived_key_list, field, (table_map)0))
6217 return ((e1->referenced_by < e2->referenced_by) ? -1 :
6218 ((e1->referenced_by > e2->referenced_by) ? 1 : 0));
6242 char buf[NAME_CHAR_LEN];
6245 if (!derived_key_list.elements)
6248 if (table->
alloc_keys(derived_key_list.elements))
6252 derived_key_list.
sort((Node_cmp_func)Derived_key_comp, 0);
6253 while ((entry= it++))
6255 sprintf(buf,
"<auto_key%i>", key++);
6257 table->in_use->strdup(buf)))
6287 bool TABLE_LIST::handle_derived(LEX *lex,
6296 for (SELECT_LEX *sl= unit->first_select(); sl; sl= sl->next_select())
6297 if (sl->handle_derived(lex, processor))
6301 return mysql_handle_single_derived(lex,
this, processor);
6317 return (view ? &view->unit :
derived);
6336 memset(const_key_parts, 0,
sizeof(key_part_map) * s->keys);
6341 for (uint index= 0; index < s->keys; index++)
6343 KEY_PART_INFO *keyinfo= key_info[
index].key_part;
6346 for (key_part_map part_map= (key_part_map)1;
6347 keyinfo < keyinfo_end;
6348 keyinfo++, part_map<<= 1)
6350 if (const_expression_in_where(conds, NULL, keyinfo->field))
6351 const_key_parts[
index]|= part_map;
6372 DBUG_ENTER(
"check_read_removal");
6373 DBUG_ASSERT(file->ha_table_flags() & HA_READ_BEFORE_WRITE_REMOVAL);
6374 DBUG_ASSERT(index != MAX_KEY);
6377 if ((key_info[index].flags & HA_NOSAME) == 0)
6381 bitmap_clear_all(&tmp_set);
6382 mark_columns_used_by_index_no_reset(index, &tmp_set);
6383 if (!bitmap_cmp(&tmp_set, read_set))
6387 DBUG_RETURN(file->start_read_removal());
6399 bool is_simple_order(
ORDER *order)
6401 for (
ORDER *ord= order; ord; ord= ord->next)
6403 if (ord->item[0]->real_item()->type() != Item::FIELD_ITEM)