20 #include "sql_executor.h"
54 Field *create_tmp_field_from_field(THD *thd,
Field *org_field,
60 new_field= org_field->new_field(thd->mem_root, table,
61 table == org_field->table);
64 new_field->init(table);
65 new_field->orig_table= org_field->orig_table;
67 item->result_field= new_field;
69 new_field->field_name=
name;
70 new_field->flags|= (org_field->flags & NO_DEFAULT_VALUE_FLAG);
71 if (org_field->maybe_null() || (item && item->maybe_null))
72 new_field->flags&= ~NOT_NULL_FLAG;
73 if (org_field->type() == MYSQL_TYPE_VAR_STRING ||
74 org_field->type() == MYSQL_TYPE_VARCHAR)
75 table->s->db_create_options|= HA_OPTION_PACK_RECORD;
76 else if (org_field->type() == FIELD_TYPE_DOUBLE)
105 Item ***copy_func,
bool modify_item)
107 bool maybe_null= item->maybe_null;
109 LINT_INIT(new_field);
111 switch (item->result_type()) {
113 new_field=
new Field_double(item->max_length, maybe_null,
114 item->item_name.
ptr(), item->decimals, TRUE);
123 if (item->max_length >= (MY_INT32_NUM_DECIMAL_DIGITS - 1))
124 new_field=
new Field_longlong(item->max_length, maybe_null,
125 item->item_name.
ptr(), item->unsigned_flag);
127 new_field=
new Field_long(item->max_length, maybe_null,
128 item->item_name.
ptr(), item->unsigned_flag);
131 DBUG_ASSERT(item->collation.collation);
137 if (item->is_temporal() || item->field_type() == MYSQL_TYPE_GEOMETRY)
141 new_field->set_derivation(item->collation.derivation);
144 new_field= Field_new_decimal::create_from_item(item);
154 new_field->init(table);
165 if (copy_func && item->real_item()->is_result_field())
166 *((*copy_func)++) = item;
168 item->set_result_field(new_field);
169 if (item->type() == Item::NULL_ITEM)
188 static Field *create_tmp_field_for_schema(THD *thd,
Item *item,
TABLE *table)
190 if (item->field_type() == MYSQL_TYPE_VARCHAR)
193 if (item->max_length > MAX_FIELD_VARCHARLENGTH)
194 field=
new Field_blob(item->max_length, item->maybe_null,
195 item->item_name.
ptr(), item->collation.collation);
199 item->item_name.
ptr(),
200 table->s, item->collation.collation);
201 table->s->db_create_options|= HA_OPTION_PACK_RECORD;
240 Field **default_field,
241 bool group,
bool modify_item,
242 bool table_cant_handle_bit_fields,
243 bool make_copy_field)
246 Item::Type orig_type=
type;
249 if (type != Item::FIELD_ITEM &&
250 item->real_item()->type() == Item::FIELD_ITEM)
253 item= item->real_item();
254 type= Item::FIELD_ITEM;
258 case Item::SUM_FUNC_ITEM:
261 result= item_sum->create_tmp_field(group, table);
263 my_error(ER_OUT_OF_RESOURCES, MYF(ME_FATALERROR));
266 case Item::FIELD_ITEM:
267 case Item::DEFAULT_VALUE_ITEM:
270 bool orig_modify= modify_item;
271 if (orig_type == Item::REF_ITEM)
277 if (field->maybe_null && !field->field->maybe_null())
279 result= create_tmp_field_from_item(thd, item, table, NULL,
283 *from_field= field->field;
285 field->result_field= result;
287 else if (table_cant_handle_bit_fields && field->field->type() ==
290 *from_field= field->field;
291 result= create_tmp_field_from_item(thd, item, table, copy_func,
296 field->result_field= result;
300 result= create_tmp_field_from_field(thd, (*from_field= field->field),
301 orig_item ? orig_item->item_name.
ptr() :
302 item->item_name.
ptr(),
304 modify_item ? field :
309 if (orig_type == Item::REF_ITEM && orig_modify)
310 ((
Item_ref*)orig_item)->set_result_field(result);
316 if (orig_type != Item::DEFAULT_VALUE_ITEM && field->field->
eq_def(result))
317 *default_field= field->field;
321 case Item::FUNC_ITEM:
322 if (((
Item_func *) item)->functype() == Item_func::FUNC_SP)
325 Field *sp_result_field= item_func_sp->get_sp_result_field();
329 DBUG_ASSERT(item_func_sp->result_field);
330 *from_field= item_func_sp->result_field;
334 *((*copy_func)++)= item;
337 result= create_tmp_field_from_field(thd,
339 item_func_sp->item_name.
ptr(),
345 item->set_result_field(result);
350 case Item::COND_ITEM:
351 case Item::FIELD_AVG_ITEM:
352 case Item::FIELD_STD_ITEM:
353 case Item::FIELD_VARIANCE_ITEM:
354 case Item::SUBSELECT_ITEM:
356 case Item::PROC_ITEM:
358 case Item::REAL_ITEM:
359 case Item::DECIMAL_ITEM:
360 case Item::STRING_ITEM:
362 case Item::NULL_ITEM:
363 case Item::VARBIN_ITEM:
369 result= create_tmp_field_from_item(thd, item, table,
370 (make_copy_field ? 0 : copy_func),
373 case Item::TYPE_HOLDER:
377 result->set_derivation(item->collation.derivation);
393 static void setup_tmp_table_column_bitmaps(
TABLE *table, uchar *bitmaps)
395 uint field_count= table->s->fields;
396 bitmap_init(&table->def_read_set, (my_bitmap_map*) bitmaps, field_count,
398 bitmap_init(&table->tmp_set,
399 (my_bitmap_map*) (bitmaps+ bitmap_buffer_size(field_count)),
402 table->def_write_set= table->def_read_set;
403 table->s->all_set= table->def_read_set;
404 bitmap_set_all(&table->s->all_set);
405 table->default_column_bitmaps();
406 table->s->column_bitmap_size= bitmap_buffer_size(field_count);
441 #define STRING_TOTAL_LENGTH_TO_PACK_ROWS 128
442 #define AVG_STRING_LENGTH_TO_PACK_ROWS 64
443 #define RATIO_TO_PACK_ROWS 2
444 #define MIN_STRING_LENGTH_TO_PACK_ROWS 10
447 create_tmp_table(THD *thd,TMP_TABLE_PARAM *param,
List<Item> &fields,
448 ORDER *
group,
bool distinct,
bool save_sum_fields,
449 ulonglong select_options, ha_rows rows_limit,
450 const char *table_alias)
455 uint
i,field_count,null_count,null_pack_length;
456 uint copy_func_count= param->func_count;
457 uint hidden_null_count, hidden_null_pack_length, hidden_field_count;
458 uint blob_count,group_null_items, string_count;
459 uint temp_pool_slot=MY_BIT_NONE;
461 ulong reclength, string_total_length;
462 bool using_unique_constraint=
false;
463 bool use_packed_rows=
false;
464 bool not_all_columns= !(select_options & TMP_TABLE_ALL_COLUMNS);
465 char *tmpname,path[FN_REFLEN];
466 uchar *pos, *group_buff, *bitmaps;
468 Field **reg_field, **from_field, **default_field;
479 uint total_uneven_bit_length= 0, hidden_uneven_bit_length= 0;
480 bool force_copy_fields= param->force_copy_fields;
482 save_sum_fields|= param->precomputed_group_by;
483 DBUG_ENTER(
"create_tmp_table");
485 (
"distinct: %d save_sum_fields: %d rows_limit: %lu group: %d",
486 (
int) distinct, (
int) save_sum_fields,
487 (ulong) rows_limit,
test(group)));
489 thd->inc_status_created_tmp_tables();
491 if (use_temp_pool && !(test_flags & TEST_KEEP_TMP_TABLES))
492 temp_pool_slot = bitmap_lock_set_next(&temp_pool);
494 if (temp_pool_slot != MY_BIT_NONE)
496 current_pid, temp_pool_slot);
501 thd->thread_id, thd->tmp_table++);
508 fn_format(path, path, mysql_tmpdir,
"", MY_REPLACE_EXT|MY_UNPACK_FILENAME);
513 if (!param->quick_group)
515 else for (
ORDER *tmp=group ; tmp ; tmp=tmp->next)
523 (*tmp->item)->marker= 4;
524 if ((*tmp->item)->max_length >= CONVERT_IF_BIGGER_TO_BLOB)
525 using_unique_constraint=
true;
527 if (param->group_length >= MAX_BLOB_WIDTH)
528 using_unique_constraint=
true;
533 field_count=param->field_count+param->func_count+param->sum_func_count;
534 hidden_field_count=param->hidden_field_count;
543 if (param->precomputed_group_by)
544 copy_func_count+= param->sum_func_count;
546 init_sql_alloc(&own_root, TABLE_ALLOC_BLOCK_SIZE, 0);
548 if (!multi_alloc_root(&own_root,
549 &table,
sizeof(*table),
550 &share,
sizeof(*share),
551 ®_field,
sizeof(
Field*) * (field_count+1),
552 &default_field,
sizeof(
Field*) * (field_count),
553 &blob_field,
sizeof(uint)*(field_count+1),
554 &from_field,
sizeof(
Field*)*field_count,
555 ©_func,
sizeof(*copy_func)*(copy_func_count+1),
556 ¶m->keyinfo,
sizeof(*param->keyinfo),
558 sizeof(*key_part_info)*(param->group_parts+1),
559 ¶m->start_recinfo,
560 sizeof(*param->recinfo)*(field_count*2+4),
561 &tmpname, (uint) strlen(path)+1,
562 &group_buff, (group && ! using_unique_constraint ?
563 param->group_length : 0),
564 &bitmaps, bitmap_buffer_size(field_count)*2,
567 if (temp_pool_slot != MY_BIT_NONE)
568 bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
572 if (!(param->copy_field= copy= new (thd->mem_root)
Copy_field[field_count]))
574 if (temp_pool_slot != MY_BIT_NONE)
575 bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
576 free_root(&own_root, MYF(0));
579 param->items_to_copy= copy_func;
580 strmov(tmpname,path);
583 memset(table, 0,
sizeof(*table));
584 memset(reg_field, 0,
sizeof(
Field*)*(field_count+1));
585 memset(default_field, 0,
sizeof(
Field*) * (field_count));
586 memset(from_field, 0,
sizeof(
Field*)*field_count);
589 mem_root_save= thd->mem_root;
592 table->field=reg_field;
593 table->alias= table_alias;
594 table->reginfo.lock_type=TL_WRITE;
595 table->db_stat=HA_OPEN_KEYFILE+HA_OPEN_RNDFILE;
597 table->temp_pool_slot = temp_pool_slot;
598 table->copy_blobs= 1;
600 table->quick_keys.init();
601 table->possible_quick_keys.init();
602 table->covering_keys.init();
603 table->merge_keys.init();
604 table->keys_in_use_for_query.init();
607 init_tmp_table_share(thd, share,
"", 0, tmpname, tmpname);
608 share->blob_field= blob_field;
609 share->db_low_byte_first=1;
610 share->table_charset= param->table_charset;
611 share->primary_key= MAX_KEY;
612 share->keys_for_keyread.init();
613 share->keys_in_use.init();
614 if (param->schema_table)
615 share->db= INFORMATION_SCHEMA_NAME;
619 reclength= string_total_length= 0;
620 blob_count= string_count= null_count= hidden_null_count= group_null_items= 0;
621 param->using_outer_summary_function= 0;
625 Field **tmp_from_field=from_field;
628 Item::Type
type= item->type();
629 if (type == Item::COPY_STR_ITEM)
636 if (item->with_sum_func && type != Item::SUM_FUNC_ITEM)
638 if (item->used_tables() & OUTER_REF_TABLE_BIT)
639 item->update_used_tables();
640 if (type == Item::SUBSELECT_ITEM ||
641 (item->used_tables() & ~OUTER_REF_TABLE_BIT))
648 param->using_outer_summary_function= 1;
653 if (type == Item::SUM_FUNC_ITEM && !group && !save_sum_fields)
656 sum_item->result_field=0;
657 for (i=0 ; i < sum_item->get_arg_count() ; i++)
659 Item *arg= sum_item->get_arg(i);
660 if (!arg->const_item())
663 create_tmp_field(thd, table, arg, arg->type(), ©_func,
664 tmp_from_field, &default_field[fieldnr],
665 group != 0,not_all_columns,
670 reclength+=new_field->pack_length();
671 if (new_field->flags & BLOB_FLAG)
673 *blob_field++= fieldnr;
676 if (new_field->type() == MYSQL_TYPE_BIT)
677 total_uneven_bit_length+= new_field->field_length & 7;
678 *(reg_field++)= new_field;
679 if (new_field->real_type() == MYSQL_TYPE_STRING ||
680 new_field->real_type() == MYSQL_TYPE_VARCHAR)
683 string_total_length+= new_field->pack_length();
685 thd->mem_root= mem_root_save;
686 arg= sum_item->set_arg(i, thd,
new Item_field(new_field));
688 if (!(new_field->flags & NOT_NULL_FLAG))
697 new_field->field_index= fieldnr++;
713 Field *new_field= (param->schema_table) ?
714 create_tmp_field_for_schema(thd, item, table) :
715 create_tmp_field(thd, table, item, type, ©_func,
716 tmp_from_field, &default_field[fieldnr],
718 !force_copy_fields &&
719 (not_all_columns || group !=0),
727 item->
marker == 4 || param->bit_fields_as_long,
732 if (thd->is_fatal_error)
736 if (type == Item::SUM_FUNC_ITEM)
737 ((
Item_sum *) item)->result_field= new_field;
739 reclength+=new_field->pack_length();
740 if (!(new_field->flags & NOT_NULL_FLAG))
742 if (new_field->type() == MYSQL_TYPE_BIT)
743 total_uneven_bit_length+= new_field->field_length & 7;
744 if (new_field->flags & BLOB_FLAG)
746 *blob_field++= fieldnr;
750 if (new_field->real_type() == MYSQL_TYPE_STRING ||
751 new_field->real_type() == MYSQL_TYPE_VARCHAR)
754 string_total_length+= new_field->pack_length();
757 if (item->marker == 4 && item->maybe_null)
760 new_field->flags|= GROUP_FLAG;
762 new_field->field_index= fieldnr++;
763 *(reg_field++)= new_field;
767 if (!--hidden_field_count)
773 hidden_null_count=null_count;
778 param->hidden_field_count= fieldnr;
786 hidden_uneven_bit_length= total_uneven_bit_length;
787 total_uneven_bit_length= 0;
790 DBUG_ASSERT(fieldnr == (uint) (reg_field - table->field));
791 DBUG_ASSERT(field_count >= (uint) (reg_field - table->field));
792 field_count= fieldnr;
795 share->fields= field_count;
799 if (blob_count || using_unique_constraint
800 || (thd->variables.big_tables && !(select_options & SELECT_SMALL_RESULT))
803 share->db_plugin= ha_lock_engine(0, myisam_hton);
804 table->file= get_new_handler(share, &table->
mem_root,
807 (param->group_parts > table->file->max_key_parts() ||
808 param->group_length > table->file->max_key_length()))
809 using_unique_constraint=
true;
813 share->db_plugin= ha_lock_engine(0, heap_hton);
814 table->file= get_new_handler(share, &table->
mem_root,
820 if (table->file->set_ha_share_ref(&share->
ha_share))
826 if (!using_unique_constraint)
827 reclength+= group_null_items;
829 share->blob_fields= blob_count;
833 if (param->hidden_field_count)
838 hidden_null_pack_length= (hidden_null_count + 7 +
839 hidden_uneven_bit_length) / 8;
840 null_pack_length= (hidden_null_pack_length +
841 (null_count + total_uneven_bit_length + 7) / 8);
842 reclength+=null_pack_length;
847 (string_total_length >= STRING_TOTAL_LENGTH_TO_PACK_ROWS &&
848 (reclength / string_total_length <= RATIO_TO_PACK_ROWS ||
849 string_total_length / string_count >= AVG_STRING_LENGTH_TO_PACK_ROWS)))
850 use_packed_rows=
true;
852 if (!use_packed_rows)
853 share->db_create_options&= ~HA_OPTION_PACK_RECORD;
855 share->reclength= reclength;
857 uint alloc_length=ALIGN_SIZE(reclength+MI_UNIQUE_HASH_LENGTH+1);
858 share->rec_buff_length= alloc_length;
859 if (!(table->record[0]= (uchar*)
860 alloc_root(&table->
mem_root, alloc_length*3)))
862 table->record[1]= table->record[0]+alloc_length;
863 share->default_values= table->record[1]+alloc_length;
866 param->func_count= copy_func - param->items_to_copy;
868 setup_tmp_table_column_bitmaps(table, bitmaps);
870 recinfo=param->start_recinfo;
871 null_flags=(uchar*) table->record[0];
872 pos=table->record[0]+ null_pack_length;
873 if (null_pack_length)
875 memset(recinfo, 0,
sizeof(*recinfo));
876 recinfo->type=FIELD_NORMAL;
877 recinfo->length=null_pack_length;
879 memset(null_flags, 255, null_pack_length);
881 table->null_flags= (uchar*) table->record[0];
882 share->null_fields= null_count+ hidden_null_count;
883 share->null_bytes= null_pack_length;
885 null_count= (blob_count == 0) ? 1 : 0;
886 hidden_field_count=param->hidden_field_count;
887 for (i=0,reg_field=table->field; i < field_count; i++,reg_field++,recinfo++)
889 Field *field= *reg_field;
891 memset(recinfo, 0,
sizeof(*recinfo));
893 if (!(field->flags & NOT_NULL_FLAG))
895 if (field->flags & GROUP_FLAG && !using_unique_constraint)
903 recinfo->type=FIELD_NORMAL;
905 memset(recinfo, 0,
sizeof(*recinfo));
909 recinfo->null_bit= (uint8)1 << (null_count & 7);
910 recinfo->null_pos= null_count/8;
912 field->move_field(pos,null_flags+null_count/8,
913 (uint8)1 << (null_count & 7));
917 field->move_field(pos,(uchar*) 0,0);
918 if (field->type() == MYSQL_TYPE_BIT)
921 ((
Field_bit*) field)->set_bit_ptr(null_flags + null_count / 8,
923 null_count+= (field->field_length & 7);
931 if (default_field[i] && default_field[i]->ptr)
939 Field *orig_field= default_field[
i];
941 diff= (my_ptrdiff_t) (orig_field->table->s->default_values-
942 orig_field->table->record[0]);
943 orig_field->move_field_offset(diff);
944 if (orig_field->is_real_null())
948 field->set_notnull();
949 memcpy(field->ptr, orig_field->ptr, field->pack_length());
951 orig_field->move_field_offset(-diff);
956 copy->set(field,from_field[i],save_sum_fields);
959 length=field->pack_length();
963 recinfo->length=length;
964 if (field->flags & BLOB_FLAG)
965 recinfo->type= (int) FIELD_BLOB;
966 else if (use_packed_rows &&
967 field->real_type() == MYSQL_TYPE_STRING &&
968 length >= MIN_STRING_LENGTH_TO_PACK_ROWS)
969 recinfo->type=FIELD_SKIP_ENDSPACE;
970 else if (use_packed_rows &&
971 field->real_type() == MYSQL_TYPE_VARCHAR &&
972 length >= MIN_STRING_LENGTH_TO_PACK_ROWS)
973 recinfo->type= FIELD_VARCHAR;
975 recinfo->type=FIELD_NORMAL;
976 if (!--hidden_field_count)
977 null_count=(null_count+7) & ~7;
980 field->table_name= &table->alias;
983 param->copy_field_end=copy;
984 param->recinfo=recinfo;
985 store_record(table,s->default_values);
987 if (thd->variables.tmp_table_size == ~ (ulonglong) 0)
988 share->max_rows= ~(ha_rows) 0;
990 share->max_rows= (ha_rows) (((share->db_type() == heap_hton) ?
991 min(thd->variables.tmp_table_size,
992 thd->variables.max_heap_table_size) :
993 thd->variables.tmp_table_size) /
995 set_if_bigger(share->max_rows,1);
1000 set_if_smaller(share->max_rows, rows_limit);
1001 param->end_write_records= rows_limit;
1003 keyinfo= param->keyinfo;
1004 keyinfo->table=
table;
1008 DBUG_PRINT(
"info",(
"Creating group key in temporary table"));
1010 param->group_buff=group_buff;
1012 share->uniques=
test(using_unique_constraint);
1013 table->key_info= share->key_info= keyinfo;
1014 keyinfo->key_part= key_part_info;
1015 keyinfo->
flags=HA_NOSAME;
1021 keyinfo->algorithm= HA_KEY_ALG_UNDEF;
1022 keyinfo->
name= (
char*)
"group_key";
1024 for (; cur_group ; cur_group= cur_group->next, key_part_info++)
1026 Field *field=(*cur_group->item)->get_tmp_table_field();
1027 DBUG_ASSERT(field->table == table);
1028 bool maybe_null=(*cur_group->item)->maybe_null;
1030 if (!using_unique_constraint)
1032 cur_group->buff=(
char*) group_buff;
1033 cur_group->field= field->new_key_field(thd->mem_root, table,
1034 group_buff +
test(maybe_null));
1036 if (!cur_group->field)
1047 keyinfo->
flags|= HA_NULL_ARE_EQUAL;
1052 key_part_info->key_part_flag|= HA_END_SPACE_ARE_EQUAL;
1053 group_buff+= cur_group->field->pack_length();
1055 keyinfo->
key_length+= key_part_info->store_length;
1060 if (distinct && field_count != param->hidden_field_count)
1068 DBUG_PRINT(
"info",(
"hidden_field_count: %d", param->hidden_field_count));
1079 null_pack_length-=hidden_null_pack_length;
1081 ((field_count-param->hidden_field_count) +
1082 (share->uniques ?
test(null_pack_length) : 0));
1092 table->key_info= share->key_info= keyinfo;
1093 keyinfo->key_part= key_part_info;
1096 keyinfo->
name= (
char*)
"<auto_key>";
1097 keyinfo->algorithm= HA_KEY_ALG_UNDEF;
1105 if (null_pack_length && share->uniques)
1107 key_part_info->null_bit=0;
1108 key_part_info->offset=hidden_null_pack_length;
1109 key_part_info->length=null_pack_length;
1110 key_part_info->field=
new Field_string(table->record[0],
1111 (uint32) key_part_info->length,
1115 NullS, &my_charset_bin);
1116 if (!key_part_info->field)
1118 key_part_info->field->init(table);
1119 key_part_info->key_type=FIELDFLAG_BINARY;
1120 key_part_info->type= HA_KEYTYPE_BINARY;
1124 for (i=param->hidden_field_count, reg_field=table->field + i ;
1126 i++, reg_field++, key_part_info++)
1129 keyinfo->
key_length+= key_part_info->store_length;
1133 if (thd->is_fatal_error)
1135 share->db_record_offset= 1;
1136 if (!param->skip_create_table)
1138 if (instantiate_tmp_table(table, param->keyinfo, param->start_recinfo,
1139 ¶m->recinfo, select_options,
1140 thd->variables.big_tables, &thd->opt_trace))
1144 thd->mem_root= mem_root_save;
1146 DEBUG_SYNC(thd,
"tmp_table_created");
1151 thd->mem_root= mem_root_save;
1152 free_tmp_table(thd,table);
1153 if (temp_pool_slot != MY_BIT_NONE)
1154 bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
1193 TABLE *create_duplicate_weedout_tmp_table(THD *thd,
1194 uint uniq_tuple_length_arg,
1200 uint temp_pool_slot=MY_BIT_NONE;
1201 char *tmpname,path[FN_REFLEN];
1209 bool using_unique_constraint=
false;
1210 Field *field, *key_field;
1211 uint null_pack_length, null_count;
1214 DBUG_ENTER(
"create_duplicate_weedout_tmp_table");
1215 DBUG_ASSERT(!sjtbl->is_confluent);
1219 thd->inc_status_created_tmp_tables();
1220 if (use_temp_pool && !(test_flags & TEST_KEEP_TMP_TABLES))
1221 temp_pool_slot = bitmap_lock_set_next(&temp_pool);
1223 if (temp_pool_slot != MY_BIT_NONE)
1225 current_pid, temp_pool_slot);
1230 thd->thread_id, thd->tmp_table++);
1232 fn_format(path, path, mysql_tmpdir,
"", MY_REPLACE_EXT|MY_UNPACK_FILENAME);
1235 if (uniq_tuple_length_arg >= CONVERT_IF_BIGGER_TO_BLOB)
1236 using_unique_constraint=
true;
1239 init_sql_alloc(&own_root, TABLE_ALLOC_BLOCK_SIZE, 0);
1240 if (!multi_alloc_root(&own_root,
1241 &table,
sizeof(*table),
1242 &share,
sizeof(*share),
1243 ®_field,
sizeof(
Field*) * (1+1),
1244 &blob_field,
sizeof(uint)*2,
1245 &keyinfo,
sizeof(*keyinfo),
1246 &key_part_info,
sizeof(*key_part_info) * 2,
1248 sizeof(*recinfo)*(1*2+4),
1249 &tmpname, (uint) strlen(path)+1,
1250 &group_buff, (!using_unique_constraint ?
1251 uniq_tuple_length_arg : 0),
1252 &bitmaps, bitmap_buffer_size(1)*2,
1255 if (temp_pool_slot != MY_BIT_NONE)
1256 bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
1259 strmov(tmpname,path);
1263 memset(table, 0,
sizeof(*table));
1264 memset(reg_field, 0,
sizeof(
Field*)*2);
1267 mem_root_save= thd->mem_root;
1270 table->field=reg_field;
1271 table->alias=
"weedout-tmp";
1272 table->reginfo.lock_type=TL_WRITE;
1273 table->db_stat=HA_OPEN_KEYFILE+HA_OPEN_RNDFILE;
1275 table->temp_pool_slot = temp_pool_slot;
1276 table->copy_blobs= 1;
1278 table->quick_keys.init();
1279 table->possible_quick_keys.init();
1280 table->covering_keys.init();
1281 table->keys_in_use_for_query.init();
1284 init_tmp_table_share(thd, share,
"", 0, tmpname, tmpname);
1285 share->blob_field= blob_field;
1286 share->db_low_byte_first=1;
1287 share->table_charset= NULL;
1288 share->primary_key= MAX_KEY;
1289 share->keys_for_keyread.init();
1290 share->keys_in_use.init();
1298 field=
new Field_varstring(uniq_tuple_length_arg, FALSE,
"rowids", share,
1302 field->table=
table;
1303 field->unireg_check= Field::NONE;
1304 field->flags= (NOT_NULL_FLAG | BINARY_FLAG | NO_DEFAULT_VALUE_FLAG);
1305 field->reset_fields();
1307 field->orig_table= NULL;
1309 field->field_index= 0;
1311 *(reg_field++)= field;
1316 share->blob_fields= 0;
1319 uint reclength= field->pack_length();
1320 if (using_unique_constraint)
1322 share->db_plugin= ha_lock_engine(0, myisam_hton);
1323 table->file= get_new_handler(share, &table->
mem_root,
1328 share->db_plugin= ha_lock_engine(0, heap_hton);
1329 table->file= get_new_handler(share, &table->
mem_root,
1335 if (table->file->set_ha_share_ref(&share->
ha_share))
1343 null_pack_length= 1;
1344 reclength += null_pack_length;
1346 share->reclength= reclength;
1348 uint alloc_length=ALIGN_SIZE(share->reclength + MI_UNIQUE_HASH_LENGTH+1);
1349 share->rec_buff_length= alloc_length;
1350 if (!(table->record[0]= (uchar*)
1351 alloc_root(&table->
mem_root, alloc_length*3)))
1353 table->record[1]= table->record[0]+alloc_length;
1354 share->default_values= table->record[1]+alloc_length;
1356 setup_tmp_table_column_bitmaps(table, bitmaps);
1358 recinfo= start_recinfo;
1359 null_flags=(uchar*) table->record[0];
1363 memset(recinfo, 0,
sizeof(*recinfo));
1364 recinfo->type= FIELD_NORMAL;
1365 recinfo->length= null_pack_length;
1367 memset(null_flags, 255, null_pack_length);
1369 table->null_flags= (uchar*) table->record[0];
1370 share->null_fields= null_count;
1371 share->null_bytes= null_pack_length;
1377 memset(recinfo, 0,
sizeof(*recinfo));
1383 recinfo->type= FIELD_NORMAL;
1384 recinfo->length= field->pack_length();
1386 field->move_field(table->record[0] + null_pack_length, 0, 0);
1388 field->table_name= &table->alias;
1391 if (thd->variables.tmp_table_size == ~ (ulonglong) 0)
1392 share->max_rows= ~(ha_rows) 0;
1394 share->max_rows= (ha_rows) (((share->db_type() == heap_hton) ?
1395 min(thd->variables.tmp_table_size,
1396 thd->variables.max_heap_table_size) :
1397 thd->variables.tmp_table_size) /
1399 set_if_bigger(share->max_rows,1);
1404 DBUG_PRINT(
"info",(
"Creating group key in temporary table"));
1406 share->uniques=
test(using_unique_constraint);
1407 table->key_info= table->s->key_info= keyinfo;
1408 keyinfo->key_part=key_part_info;
1414 keyinfo->algorithm= HA_KEY_ALG_UNDEF;
1415 keyinfo->
name= (
char*)
"weedout_key";
1417 key_part_info->null_bit=0;
1418 key_part_info->field= field;
1419 key_part_info->offset= field->offset(table->record[0]);
1420 key_part_info->length= (uint16) field->key_length();
1421 key_part_info->type= (uint8) field->key_type();
1422 key_part_info->key_type = FIELDFLAG_BINARY;
1423 if (!using_unique_constraint)
1425 key_field= field->new_key_field(thd->mem_root, table, group_buff);
1428 key_part_info->key_part_flag|= HA_END_SPACE_ARE_EQUAL;
1434 if (thd->is_fatal_error)
1436 share->db_record_offset= 1;
1437 if (share->db_type() == myisam_hton)
1439 if (instantiate_tmp_table(table, keyinfo, start_recinfo, &recinfo,
1440 0, 0, &thd->opt_trace))
1443 sjtbl->start_recinfo= start_recinfo;
1444 sjtbl->recinfo= recinfo;
1446 thd->mem_root= mem_root_save;
1450 thd->mem_root= mem_root_save;
1451 free_tmp_table(thd,table);
1452 if (temp_pool_slot != MY_BIT_NONE)
1453 bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
1481 uint field_count= field_list.elements;
1485 uint record_length= 0;
1487 uint null_pack_length;
1493 if (!multi_alloc_root(thd->mem_root,
1494 &table,
sizeof(*table),
1495 &share,
sizeof(*share),
1496 &field, (field_count + 1) *
sizeof(
Field*),
1497 &blob_field, (field_count+1) *
sizeof(uint),
1498 &bitmaps, bitmap_buffer_size(field_count)*2,
1502 memset(table, 0,
sizeof(*table));
1503 memset(share, 0,
sizeof(*share));
1504 table->field= field;
1506 table->temp_pool_slot= MY_BIT_NONE;
1507 share->blob_field= blob_field;
1508 share->fields= field_count;
1509 share->db_low_byte_first=1;
1510 setup_tmp_table_column_bitmaps(table, bitmaps);
1514 while ((cdef= it++))
1516 *field= make_field(share, 0, cdef->length,
1517 (uchar*) (f_maybe_null(cdef->pack_flag) ?
"" : 0),
1518 f_maybe_null(cdef->pack_flag) ? 1 : 0,
1519 cdef->pack_flag, cdef->sql_type, cdef->charset,
1520 cdef->geom_type, cdef->unireg_check,
1521 cdef->interval, cdef->field_name);
1524 (*field)->init(table);
1525 record_length+= (*field)->pack_length();
1526 if (! ((*field)->flags & NOT_NULL_FLAG))
1529 if ((*field)->flags & BLOB_FLAG)
1530 share->blob_field[blob_count++]= (uint) (field - table->field);
1535 share->blob_field[blob_count]= 0;
1536 share->blob_fields= blob_count;
1538 null_pack_length= (null_count + 7)/8;
1539 share->reclength= record_length + null_pack_length;
1540 share->rec_buff_length= ALIGN_SIZE(share->reclength + 1);
1541 table->record[0]= (uchar*) thd->alloc(share->rec_buff_length);
1542 if (!table->record[0])
1545 if (null_pack_length)
1547 table->null_flags= (uchar*) table->record[0];
1548 share->null_fields= null_count;
1549 share->null_bytes= null_pack_length;
1555 uchar *null_pos= table->record[0];
1556 uchar *field_pos= null_pos + share->null_bytes;
1559 for (field= table->field; *field; ++field)
1561 Field *cur_field= *field;
1562 if ((cur_field->flags & NOT_NULL_FLAG))
1563 cur_field->move_field(field_pos);
1566 cur_field->move_field(field_pos, (uchar*) null_pos, null_bit);
1568 if (null_bit == (uint8)1 << 8)
1574 if (cur_field->type() == MYSQL_TYPE_BIT &&
1575 cur_field->key_type() == HA_KEYTYPE_BIT)
1578 static_cast<Field_bit*
>(cur_field)->set_bit_ptr(null_pos, null_bit);
1579 null_bit+= cur_field->field_length & 7;
1588 field_pos+= cur_field->pack_length();
1593 for (field= table->field; *field; ++field)
1599 bool open_tmp_table(
TABLE *table)
1602 if ((error=table->file->
ha_open(table, table->s->table_name.str,O_RDWR,
1603 HA_OPEN_TMP_TABLE | HA_OPEN_INTERNAL_TABLE)))
1609 (void) table->file->extra(HA_EXTRA_QUICK);
1647 bool create_myisam_tmp_table(
TABLE *table,
KEY *keyinfo,
1650 ulonglong options, my_bool big_tables)
1656 DBUG_ENTER(
"create_myisam_tmp_table");
1660 bool using_unique_constraint=0;
1661 if (share->keys > 1)
1673 if (keyinfo->
key_length >= table->file->max_key_length() ||
1680 using_unique_constraint=1;
1681 memset(&uniquedef, 0,
sizeof(uniquedef));
1684 uniquedef.null_are_equal=1;
1687 memset(*recinfo, 0,
sizeof(**recinfo));
1688 (*recinfo)->type= FIELD_CHECK;
1689 (*recinfo)->length=MI_UNIQUE_HASH_LENGTH;
1691 share->reclength+=MI_UNIQUE_HASH_LENGTH;
1696 memset(&keydef, 0,
sizeof(keydef));
1697 keydef.flag= keyinfo->
flags;
1703 Field *field=keyinfo->key_part[
i].field;
1705 seg->language= field->charset()->number;
1706 seg->length= keyinfo->key_part[
i].length;
1707 seg->start= keyinfo->key_part[
i].offset;
1708 if (field->flags & BLOB_FLAG)
1711 ((keyinfo->key_part[
i].key_type & FIELDFLAG_BINARY) ?
1712 HA_KEYTYPE_VARBINARY2 : HA_KEYTYPE_VARTEXT2);
1713 seg->bit_start= (uint8)(field->pack_length() -
1714 portable_sizeof_char_ptr);
1715 seg->flag= HA_BLOB_PART;
1720 seg->type= keyinfo->key_part[
i].type;
1722 if (field->real_type() == MYSQL_TYPE_STRING &&
1723 keyinfo->key_part[
i].length > 4)
1724 seg->flag|= HA_SPACE_PACK;
1726 if (!(field->flags & NOT_NULL_FLAG))
1728 seg->null_bit= field->null_bit;
1729 seg->null_pos= field->null_offset();
1735 if (!using_unique_constraint)
1736 keydef.flag|= HA_NULL_ARE_EQUAL;
1741 memset(&create_info, 0,
sizeof(create_info));
1743 if (big_tables && !(options & SELECT_SMALL_RESULT))
1744 create_info.data_file_length= ~(ulonglong) 0;
1746 if ((error=mi_create(share->table_name.str, share->keys, &keydef,
1747 (uint) (*recinfo - start_recinfo),
1749 share->uniques, &uniquedef,
1751 HA_CREATE_TMP_TABLE | HA_CREATE_INTERNAL_TABLE |
1752 ((share->db_create_options & HA_OPTION_PACK_RECORD) ?
1760 table->in_use->inc_status_created_tmp_disk_tables();
1761 share->db_record_offset= 1;
1771 if (strlen(table->alias) != 0)
1772 trace_tmp.add_utf8_table(table);
1774 trace_tmp.add_alnum(
"table",
"intermediate_tmp_table");
1776 trace_tmp.add(
"row_length",table->s->reclength).
1777 add(
"key_length", table->s->key_info ?
1779 add(
"unique_constraint", table->s->uniques ?
true :
false);
1781 if (table->s->db_type() == myisam_hton)
1783 trace_tmp.add_alnum(
"location",
"disk (MyISAM)");
1784 if (table->s->db_create_options & HA_OPTION_PACK_RECORD)
1785 trace_tmp.add_alnum(
"record_format",
"packed");
1787 trace_tmp.add_alnum(
"record_format",
"fixed");
1791 DBUG_ASSERT(table->s->db_type() == heap_hton);
1792 trace_tmp.add_alnum(
"location",
"memory (heap)").
1793 add(
"row_limit_estimate", table->s->max_rows);
1820 ulonglong options, my_bool big_tables,
1823 if (table->s->db_type() == myisam_hton)
1825 if (create_myisam_tmp_table(table, keyinfo, start_recinfo, recinfo,
1826 options, big_tables))
1829 empty_record(table);
1831 if (open_tmp_table(table))
1834 if (unlikely(trace->is_started()))
1838 trace_tmp_table(trace, table);
1847 const char *save_proc_info;
1848 DBUG_ENTER(
"free_tmp_table");
1849 DBUG_PRINT(
"enter",(
"table: %s",entry->alias));
1851 save_proc_info=thd->proc_info;
1852 THD_STAGE_INFO(thd, stage_removing_tmp_table);
1857 filesort_free_buffers(entry,
true);
1871 for (
Field **ptr=entry->field ; *ptr ; ptr++)
1873 free_io_cache(entry);
1875 if (entry->temp_pool_slot != MY_BIT_NONE)
1876 bitmap_lock_clear_bit(&temp_pool, entry->temp_pool_slot);
1878 plugin_unlock(0, entry->s->db_plugin);
1880 free_root(&own_root, MYF(0));
1881 thd_proc_info(thd, save_proc_info);
1918 bool create_myisam_from_heap(THD *thd,
TABLE *table,
1921 int error,
bool ignore_last_dup,
1926 const char *save_proc_info;
1928 DBUG_ENTER(
"create_myisam_from_heap");
1930 if (table->s->db_type() != heap_hton ||
1931 error != HA_ERR_RECORD_FILE_FULL)
1937 table->file->
print_error(error, MYF(ME_FATALERROR));
1947 new_table.s= &share;
1948 new_table.s->db_plugin= ha_lock_engine(thd, myisam_hton);
1949 if (!(new_table.file= get_new_handler(&share, &new_table.
mem_root,
1950 new_table.s->db_type())))
1952 if (new_table.file->set_ha_share_ref(&share.
ha_share))
1954 delete new_table.file;
1957 save_proc_info=thd->proc_info;
1958 THD_STAGE_INFO(thd, stage_converting_heap_to_myisam);
1960 if (create_myisam_tmp_table(&new_table, table->s->key_info,
1961 start_recinfo, recinfo,
1962 (thd->lex->select_lex.options |
1963 thd->variables.option_bits),
1964 thd->variables.big_tables))
1966 if (open_tmp_table(&new_table))
1970 if (unlikely(thd->opt_trace.is_started()))
1975 DBUG_ASSERT(error == HA_ERR_RECORD_FILE_FULL);
1976 convert.add_alnum(
"cause",
"memory_table_size_exceeded");
1977 trace_tmp_table(trace, &new_table);
1980 if (table->file->indexes_are_disabled())
1982 table->file->ha_index_or_rnd_end();
1985 table->file->
print_error(write_err, MYF(ME_FATALERROR));
1991 new_table.file->extra(HA_EXTRA_NO_ROWS);
1992 new_table.no_rows=1;
1995 #ifdef TO_BE_DONE_LATER_IN_4_1
2000 table->file->info(HA_STATUS_VARIABLE);
2004 new_table.file->extra(HA_EXTRA_WRITE_CACHE);
2013 while (!table->file->
ha_rnd_next(new_table.record[1]))
2015 write_err= new_table.file->ha_write_row(new_table.record[1]);
2016 DBUG_EXECUTE_IF(
"raise_error", write_err= HA_ERR_FOUND_DUPP_KEY ;);
2021 if ((write_err=new_table.file->ha_write_row(table->record[0])))
2027 *is_duplicate= TRUE;
2032 *is_duplicate= FALSE;
2040 plugin_unlock(0, table->s->db_plugin);
2041 share.db_plugin= my_plugin_lock(0, &share.db_plugin);
2042 new_table.s= table->s;
2047 JOIN_TAB *tab= table->reginfo.join_tab;
2048 if (tab && tab->select && tab->select->quick)
2054 DBUG_ASSERT(table->pos_in_table_list &&
2056 tab->select->quick->set_handler(table->file);
2059 table->file->change_table_ptr(table, table->s);
2060 table->use_all_columns();
2062 thd_proc_info(thd, (!strcmp(save_proc_info,
"Copying to tmp table") ?
2063 "Copying to tmp table on disk" : save_proc_info));
2069 DBUG_PRINT(
"error",(
"Got error: %d",write_err));
2072 if (table->file->inited)
2078 delete new_table.file;
2079 thd_proc_info(thd, save_proc_info);