10532
/****************************************************************************
10533
Create internal temporary table
10534
****************************************************************************/
10537
Create field for temporary table from given field.
10539
@param thd Thread handler
10540
@param org_field field from which new field will be created
10541
@param name New field name
10542
@param table Temporary table
10543
@param item !=NULL if item->result_field should point to new field.
10544
This is relevant for how fill_record() is going to work:
10545
If item != NULL then fill_record() will update
10546
the record in the original table.
10547
If item == NULL then fill_record() will update
10548
the temporary table
10549
@param convert_blob_length If >0 create a varstring(convert_blob_length)
10550
field instead of blob.
10558
Field *create_tmp_field_from_field(THD *thd, Field *org_field,
10559
const char *name, TABLE *table,
10560
Item_field *item, uint convert_blob_length)
10565
Make sure that the blob fits into a Field_varstring which has
10568
if (convert_blob_length && convert_blob_length <= Field_varstring::MAX_SIZE &&
10569
(org_field->flags & BLOB_FLAG))
10570
new_field= new Field_varstring(convert_blob_length,
10571
org_field->maybe_null(),
10572
org_field->field_name, table->s,
10573
org_field->charset());
10575
new_field= org_field->new_field(thd->mem_root, table,
10576
table == org_field->table);
10579
new_field->init(table);
10580
new_field->orig_table= org_field->orig_table;
10582
item->result_field= new_field;
10584
new_field->field_name= name;
10585
new_field->flags|= (org_field->flags & NO_DEFAULT_VALUE_FLAG);
10586
if (org_field->maybe_null() || (item && item->maybe_null))
10587
new_field->flags&= ~NOT_NULL_FLAG; // Because of outer join
10588
if (org_field->type() == MYSQL_TYPE_VAR_STRING ||
10589
org_field->type() == MYSQL_TYPE_VARCHAR)
10590
table->s->db_create_options|= HA_OPTION_PACK_RECORD;
10591
else if (org_field->type() == FIELD_TYPE_DOUBLE)
10592
((Field_double *) new_field)->not_fixed= true;
10598
Create field for temporary table using type of given item.
10600
@param thd Thread handler
10601
@param item Item to create a field for
10602
@param table Temporary table
10603
@param copy_func If set and item is a function, store copy of
10605
@param modify_item 1 if item->result_field should point to new
10606
item. This is relevent for how fill_record()
10608
If modify_item is 1 then fill_record() will
10609
update the record in the original table.
10610
If modify_item is 0 then fill_record() will
10611
update the temporary table
10612
@param convert_blob_length If >0 create a varstring(convert_blob_length)
10613
field instead of blob.
10621
static Field *create_tmp_field_from_item(THD *thd __attribute__((__unused__)),
10622
Item *item, TABLE *table,
10623
Item ***copy_func, bool modify_item,
10624
uint convert_blob_length)
10626
bool maybe_null= item->maybe_null;
10629
switch (item->result_type()) {
10631
new_field= new Field_double(item->max_length, maybe_null,
10632
item->name, item->decimals, true);
10636
Select an integer type with the minimal fit precision.
10637
MY_INT32_NUM_DECIMAL_DIGITS is sign inclusive, don't consider the sign.
10638
Values with MY_INT32_NUM_DECIMAL_DIGITS digits may or may not fit into
10639
Field_long : make them Field_int64_t.
10641
if (item->max_length >= (MY_INT32_NUM_DECIMAL_DIGITS - 1))
10642
new_field=new Field_int64_t(item->max_length, maybe_null,
10643
item->name, item->unsigned_flag);
10645
new_field=new Field_long(item->max_length, maybe_null,
10646
item->name, item->unsigned_flag);
10648
case STRING_RESULT:
10649
assert(item->collation.collation);
10651
enum enum_field_types type;
10653
DATE/TIME fields have STRING_RESULT result type.
10654
To preserve type they needed to be handled separately.
10656
if ((type= item->field_type()) == MYSQL_TYPE_DATETIME ||
10657
type == MYSQL_TYPE_TIME || type == MYSQL_TYPE_NEWDATE ||
10658
type == MYSQL_TYPE_TIMESTAMP)
10659
new_field= item->tmp_table_field_from_field_type(table, 1);
10661
Make sure that the blob fits into a Field_varstring which has
10664
else if (item->max_length/item->collation.collation->mbmaxlen > 255 &&
10665
convert_blob_length <= Field_varstring::MAX_SIZE &&
10666
convert_blob_length)
10667
new_field= new Field_varstring(convert_blob_length, maybe_null,
10668
item->name, table->s,
10669
item->collation.collation);
10671
new_field= item->make_string_field(table);
10672
new_field->set_derivation(item->collation.derivation);
10674
case DECIMAL_RESULT:
10676
uint8_t dec= item->decimals;
10677
uint8_t intg= ((Item_decimal *) item)->decimal_precision() - dec;
10678
uint32_t len= item->max_length;
10681
Trying to put too many digits overall in a DECIMAL(prec,dec)
10682
will always throw a warning. We must limit dec to
10683
DECIMAL_MAX_SCALE however to prevent an assert() later.
10688
signed int overflow;
10690
dec= min(dec, DECIMAL_MAX_SCALE);
10693
If the value still overflows the field with the corrected dec,
10694
we'll throw out decimals rather than integers. This is still
10695
bad and of course throws a truncation warning.
10696
+1: for decimal point
10699
overflow= my_decimal_precision_to_length(intg + dec, dec,
10700
item->unsigned_flag) - len;
10703
dec= max(0, dec - overflow); // too long, discard fract
10705
len -= item->decimals - dec; // corrected value fits
10708
new_field= new Field_new_decimal(len, maybe_null, item->name,
10709
dec, item->unsigned_flag);
10714
// This case should never be choosen
10720
new_field->init(table);
10722
if (copy_func && item->is_result_field())
10723
*((*copy_func)++) = item; // Save for copy_funcs
10725
item->set_result_field(new_field);
10726
if (item->type() == Item::NULL_ITEM)
10727
new_field->is_created_from_null_item= true;
10733
Create field for information schema table.
10735
@param thd Thread handler
10736
@param table Temporary table
10737
@param item Item to create a field for
10745
Field *create_tmp_field_for_schema(THD *thd __attribute__((__unused__)),
10746
Item *item, TABLE *table)
10748
if (item->field_type() == MYSQL_TYPE_VARCHAR)
10751
if (item->max_length > MAX_FIELD_VARCHARLENGTH)
10752
field= new Field_blob(item->max_length, item->maybe_null,
10753
item->name, item->collation.collation);
10755
field= new Field_varstring(item->max_length, item->maybe_null,
10757
table->s, item->collation.collation);
10759
field->init(table);
10762
return item->tmp_table_field_from_field_type(table, 0);
10767
Create field for temporary table.
10769
@param thd Thread handler
10770
@param table Temporary table
10771
@param item Item to create a field for
10772
@param type Type of item (normally item->type)
10773
@param copy_func If set and item is a function, store copy of item
10775
@param from_field if field will be created using other field as example,
10776
pointer example field will be written here
10777
@param default_field If field has a default value field, store it here
10778
@param group 1 if we are going to do a relative group by on result
10779
@param modify_item 1 if item->result_field should point to new item.
10780
This is relevent for how fill_record() is going to
10782
If modify_item is 1 then fill_record() will update
10783
the record in the original table.
10784
If modify_item is 0 then fill_record() will update
10785
the temporary table
10786
@param convert_blob_length If >0 create a varstring(convert_blob_length)
10787
field instead of blob.
10795
Field *create_tmp_field(THD *thd, TABLE *table,Item *item, Item::Type type,
10796
Item ***copy_func, Field **from_field,
10797
Field **default_field,
10798
bool group, bool modify_item,
10799
bool table_cant_handle_bit_fields __attribute__((__unused__)),
10800
bool make_copy_field,
10801
uint convert_blob_length)
10804
Item::Type orig_type= type;
10805
Item *orig_item= 0;
10807
if (type != Item::FIELD_ITEM &&
10808
item->real_item()->type() == Item::FIELD_ITEM)
10811
item= item->real_item();
10812
type= Item::FIELD_ITEM;
10816
case Item::SUM_FUNC_ITEM:
10818
Item_sum *item_sum=(Item_sum*) item;
10819
result= item_sum->create_tmp_field(group, table, convert_blob_length);
10821
my_error(ER_OUT_OF_RESOURCES, MYF(ME_FATALERROR));
10824
case Item::FIELD_ITEM:
10825
case Item::DEFAULT_VALUE_ITEM:
10827
Item_field *field= (Item_field*) item;
10828
bool orig_modify= modify_item;
10829
if (orig_type == Item::REF_ITEM)
10832
If item have to be able to store NULLs but underlaid field can't do it,
10833
create_tmp_field_from_field() can't be used for tmp field creation.
10835
if (field->maybe_null && !field->field->maybe_null())
10837
result= create_tmp_field_from_item(thd, item, table, NULL,
10838
modify_item, convert_blob_length);
10839
*from_field= field->field;
10840
if (result && modify_item)
10841
field->result_field= result;
10844
result= create_tmp_field_from_field(thd, (*from_field= field->field),
10845
orig_item ? orig_item->name :
10848
modify_item ? field :
10850
convert_blob_length);
10851
if (orig_type == Item::REF_ITEM && orig_modify)
10852
((Item_ref*)orig_item)->set_result_field(result);
10853
if (field->field->eq_def(result))
10854
*default_field= field->field;
10858
case Item::FUNC_ITEM:
10860
case Item::COND_ITEM:
10861
case Item::FIELD_AVG_ITEM:
10862
case Item::FIELD_STD_ITEM:
10863
case Item::SUBSELECT_ITEM:
10864
/* The following can only happen with 'CREATE TABLE ... SELECT' */
10865
case Item::PROC_ITEM:
10866
case Item::INT_ITEM:
10867
case Item::REAL_ITEM:
10868
case Item::DECIMAL_ITEM:
10869
case Item::STRING_ITEM:
10870
case Item::REF_ITEM:
10871
case Item::NULL_ITEM:
10872
case Item::VARBIN_ITEM:
10873
if (make_copy_field)
10875
assert(((Item_result_field*)item)->result_field);
10876
*from_field= ((Item_result_field*)item)->result_field;
10878
return create_tmp_field_from_item(thd, item, table,
10879
(make_copy_field ? 0 : copy_func),
10880
modify_item, convert_blob_length);
10881
case Item::TYPE_HOLDER:
10882
result= ((Item_type_holder *)item)->make_field_by_type(table);
10883
result->set_derivation(item->collation.derivation);
10885
default: // Dosen't have to be stored
10891
Set up column usage bitmaps for a temporary table
10894
For temporary tables, we need one bitmap with all columns set and
10895
a tmp_set bitmap to be used by things like filesort.
10898
void setup_tmp_table_column_bitmaps(TABLE *table, uchar *bitmaps)
10900
uint field_count= table->s->fields;
10901
bitmap_init(&table->def_read_set, (my_bitmap_map*) bitmaps, field_count,
10903
bitmap_init(&table->tmp_set,
10904
(my_bitmap_map*) (bitmaps+ bitmap_buffer_size(field_count)),
10905
field_count, false);
10906
/* write_set and all_set are copies of read_set */
10907
table->def_write_set= table->def_read_set;
10908
table->s->all_set= table->def_read_set;
10909
bitmap_set_all(&table->s->all_set);
10910
table->default_column_bitmaps();
10915
Create a temp table according to a field list.
10917
Given field pointers are changed to point at tmp_table for
10918
send_fields. The table object is self contained: it's
10919
allocated in its own memory root, as well as Field objects
10920
created for table columns.
10921
This function will replace Item_sum items in 'fields' list with
10922
corresponding Item_field items, pointing at the fields in the
10923
temporary table, unless this was prohibited by true
10924
value of argument save_sum_fields. The Item_field objects
10925
are created in THD memory root.
10927
@param thd thread handle
10928
@param param a description used as input to create the table
10929
@param fields list of items that will be used to define
10930
column types of the table (also see NOTES)
10931
@param group TODO document
10932
@param distinct should table rows be distinct
10933
@param save_sum_fields see NOTES
10934
@param select_options
10936
@param table_alias possible name of the temporary table that can
10937
be used for name resolving; can be "".
10940
#define STRING_TOTAL_LENGTH_TO_PACK_ROWS 128
10941
#define AVG_STRING_LENGTH_TO_PACK_ROWS 64
10942
#define RATIO_TO_PACK_ROWS 2
10943
#define MIN_STRING_LENGTH_TO_PACK_ROWS 10
10946
create_tmp_table(THD *thd,TMP_TABLE_PARAM *param,List<Item> &fields,
10947
ORDER *group, bool distinct, bool save_sum_fields,
10948
uint64_t select_options, ha_rows rows_limit,
10951
MEM_ROOT *mem_root_save, own_root;
10953
TABLE_SHARE *share;
10954
uint i,field_count,null_count,null_pack_length;
10955
uint copy_func_count= param->func_count;
10956
uint hidden_null_count, hidden_null_pack_length, hidden_field_count;
10957
uint blob_count,group_null_items, string_count;
10958
uint temp_pool_slot=MY_BIT_NONE;
10960
ulong reclength, string_total_length;
10961
bool using_unique_constraint= 0;
10962
bool use_packed_rows= 0;
10963
bool not_all_columns= !(select_options & TMP_TABLE_ALL_COLUMNS);
10964
char *tmpname,path[FN_REFLEN];
10965
uchar *pos, *group_buff, *bitmaps;
10967
Field **reg_field, **from_field, **default_field;
10969
Copy_field *copy=0;
10971
KEY_PART_INFO *key_part_info;
10973
MI_COLUMNDEF *recinfo;
10974
uint total_uneven_bit_length= 0;
10975
bool force_copy_fields= param->force_copy_fields;
10977
status_var_increment(thd->status_var.created_tmp_tables);
10979
if (use_temp_pool && !(test_flags & TEST_KEEP_TMP_TABLES))
10980
temp_pool_slot = bitmap_lock_set_next(&temp_pool);
10982
if (temp_pool_slot != MY_BIT_NONE) // we got a slot
10983
sprintf(path, "%s_%lx_%i", tmp_file_prefix,
10984
current_pid, temp_pool_slot);
10987
/* if we run out of slots or we are not using tempool */
10988
sprintf(path,"%s%lx_%lx_%x", tmp_file_prefix,current_pid,
10989
thd->thread_id, thd->tmp_table++);
10993
No need to change table name to lower case as we are only creating
10994
MyISAM or HEAP tables here
10996
fn_format(path, path, mysql_tmpdir, "", MY_REPLACE_EXT|MY_UNPACK_FILENAME);
11001
if (!param->quick_group)
11002
group=0; // Can't use group key
11003
else for (ORDER *tmp=group ; tmp ; tmp=tmp->next)
11006
marker == 4 means two things:
11007
- store NULLs in the key, and
11008
- convert BIT fields to 64-bit long, needed because MEMORY tables
11009
can't index BIT fields.
11011
(*tmp->item)->marker= 4;
11012
if ((*tmp->item)->max_length >= CONVERT_IF_BIGGER_TO_BLOB)
11013
using_unique_constraint=1;
11015
if (param->group_length >= MAX_BLOB_WIDTH)
11016
using_unique_constraint=1;
11018
distinct=0; // Can't use distinct
11021
field_count=param->field_count+param->func_count+param->sum_func_count;
11022
hidden_field_count=param->hidden_field_count;
11025
When loose index scan is employed as access method, it already
11026
computes all groups and the result of all aggregate functions. We
11027
make space for the items of the aggregate function in the list of
11028
functions TMP_TABLE_PARAM::items_to_copy, so that the values of
11029
these items are stored in the temporary table.
11031
if (param->precomputed_group_by)
11032
copy_func_count+= param->sum_func_count;
11034
init_sql_alloc(&own_root, TABLE_ALLOC_BLOCK_SIZE, 0);
11036
if (!multi_alloc_root(&own_root,
11037
&table, sizeof(*table),
11038
&share, sizeof(*share),
11039
®_field, sizeof(Field*) * (field_count+1),
11040
&default_field, sizeof(Field*) * (field_count),
11041
&blob_field, sizeof(uint)*(field_count+1),
11042
&from_field, sizeof(Field*)*field_count,
11043
©_func, sizeof(*copy_func)*(copy_func_count+1),
11044
¶m->keyinfo, sizeof(*param->keyinfo),
11046
sizeof(*key_part_info)*(param->group_parts+1),
11047
¶m->start_recinfo,
11048
sizeof(*param->recinfo)*(field_count*2+4),
11049
&tmpname, (uint) strlen(path)+1,
11050
&group_buff, (group && ! using_unique_constraint ?
11051
param->group_length : 0),
11052
&bitmaps, bitmap_buffer_size(field_count)*2,
11055
if (temp_pool_slot != MY_BIT_NONE)
11056
bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
11057
return(NULL); /* purecov: inspected */
11059
/* Copy_field belongs to TMP_TABLE_PARAM, allocate it in THD mem_root */
11060
if (!(param->copy_field= copy= new (thd->mem_root) Copy_field[field_count]))
11062
if (temp_pool_slot != MY_BIT_NONE)
11063
bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
11064
free_root(&own_root, MYF(0)); /* purecov: inspected */
11065
return(NULL); /* purecov: inspected */
11067
param->items_to_copy= copy_func;
11068
strmov(tmpname,path);
11069
/* make table according to fields */
11071
bzero((char*) table,sizeof(*table));
11072
bzero((char*) reg_field,sizeof(Field*)*(field_count+1));
11073
bzero((char*) default_field, sizeof(Field*) * (field_count));
11074
bzero((char*) from_field,sizeof(Field*)*field_count);
11076
table->mem_root= own_root;
11077
mem_root_save= thd->mem_root;
11078
thd->mem_root= &table->mem_root;
11080
table->field=reg_field;
11081
table->alias= table_alias;
11082
table->reginfo.lock_type=TL_WRITE; /* Will be updated */
11083
table->db_stat=HA_OPEN_KEYFILE+HA_OPEN_RNDFILE;
11085
table->temp_pool_slot = temp_pool_slot;
11086
table->copy_blobs= 1;
11087
table->in_use= thd;
11088
table->quick_keys.init();
11089
table->covering_keys.init();
11090
table->keys_in_use_for_query.init();
11093
init_tmp_table_share(thd, share, "", 0, tmpname, tmpname);
11094
share->blob_field= blob_field;
11095
share->blob_ptr_size= portable_sizeof_char_ptr;
11096
share->db_low_byte_first=1; // True for HEAP and MyISAM
11097
share->table_charset= param->table_charset;
11098
share->primary_key= MAX_KEY; // Indicate no primary key
11099
share->keys_for_keyread.init();
11100
share->keys_in_use.init();
11102
/* Calculate which type of fields we will store in the temporary table */
11104
reclength= string_total_length= 0;
11105
blob_count= string_count= null_count= hidden_null_count= group_null_items= 0;
11106
param->using_indirect_summary_function=0;
11108
List_iterator_fast<Item> li(fields);
11110
Field **tmp_from_field=from_field;
11111
while ((item=li++))
11113
Item::Type type=item->type();
11114
if (not_all_columns)
11116
if (item->with_sum_func && type != Item::SUM_FUNC_ITEM)
11118
if (item->used_tables() & OUTER_REF_TABLE_BIT)
11119
item->update_used_tables();
11120
if (type == Item::SUBSELECT_ITEM ||
11121
(item->used_tables() & ~OUTER_REF_TABLE_BIT))
11124
Mark that the we have ignored an item that refers to a summary
11125
function. We need to know this if someone is going to use
11126
DISTINCT on the result.
11128
param->using_indirect_summary_function=1;
11132
if (item->const_item() && (int) hidden_field_count <= 0)
11133
continue; // We don't have to store this
11135
if (type == Item::SUM_FUNC_ITEM && !group && !save_sum_fields)
11136
{ /* Can't calc group yet */
11137
((Item_sum*) item)->result_field=0;
11138
for (i=0 ; i < ((Item_sum*) item)->arg_count ; i++)
11140
Item **argp= ((Item_sum*) item)->args + i;
11142
if (!arg->const_item())
11145
create_tmp_field(thd, table, arg, arg->type(), ©_func,
11146
tmp_from_field, &default_field[fieldnr],
11147
group != 0,not_all_columns,
11149
param->convert_blob_length);
11151
goto err; // Should be OOM
11153
reclength+=new_field->pack_length();
11154
if (new_field->flags & BLOB_FLAG)
11156
*blob_field++= fieldnr;
11159
*(reg_field++)= new_field;
11160
if (new_field->real_type() == MYSQL_TYPE_STRING ||
11161
new_field->real_type() == MYSQL_TYPE_VARCHAR)
11164
string_total_length+= new_field->pack_length();
11166
thd->mem_root= mem_root_save;
11167
thd->change_item_tree(argp, new Item_field(new_field));
11168
thd->mem_root= &table->mem_root;
11169
if (!(new_field->flags & NOT_NULL_FLAG))
11173
new_field->maybe_null() is still false, it will be
11174
changed below. But we have to setup Item_field correctly
11176
(*argp)->maybe_null=1;
11178
new_field->field_index= fieldnr++;
11185
The last parameter to create_tmp_field() is a bit tricky:
11187
We need to set it to 0 in union, to get fill_record() to modify the
11189
We need to set it to 1 on multi-table-update and in select to
11190
write rows to the temporary table.
11191
We here distinguish between UNION and multi-table-updates by the fact
11192
that in the later case group is set to the row pointer.
11194
Field *new_field= (param->schema_table) ?
11195
create_tmp_field_for_schema(thd, item, table) :
11196
create_tmp_field(thd, table, item, type, ©_func,
11197
tmp_from_field, &default_field[fieldnr],
11199
!force_copy_fields &&
11200
(not_all_columns || group !=0),
11202
If item->marker == 4 then we force create_tmp_field
11203
to create a 64-bit longs for BIT fields because HEAP
11204
tables can't index BIT fields directly. We do the same
11205
for distinct, as we want the distinct index to be
11206
usable in this case too.
11208
item->marker == 4 || param->bit_fields_as_long,
11210
param->convert_blob_length);
11214
if (thd->is_fatal_error)
11215
goto err; // Got OOM
11216
continue; // Some kindf of const item
11218
if (type == Item::SUM_FUNC_ITEM)
11219
((Item_sum *) item)->result_field= new_field;
11221
reclength+=new_field->pack_length();
11222
if (!(new_field->flags & NOT_NULL_FLAG))
11224
if (new_field->flags & BLOB_FLAG)
11226
*blob_field++= fieldnr;
11229
if (item->marker == 4 && item->maybe_null)
11231
group_null_items++;
11232
new_field->flags|= GROUP_FLAG;
11234
new_field->field_index= fieldnr++;
11235
*(reg_field++)= new_field;
11237
if (!--hidden_field_count)
11240
This was the last hidden field; Remember how many hidden fields could
11243
hidden_null_count=null_count;
11245
We need to update hidden_field_count as we may have stored group
11246
functions with constant arguments
11248
param->hidden_field_count= fieldnr;
11252
assert(fieldnr == (uint) (reg_field - table->field));
11253
assert(field_count >= (uint) (reg_field - table->field));
11254
field_count= fieldnr;
11256
*blob_field= 0; // End marker
11257
share->fields= field_count;
11259
/* If result table is small; use a heap */
11260
/* future: storage engine selection can be made dynamic? */
11261
if (blob_count || using_unique_constraint ||
11262
(select_options & (OPTION_BIG_TABLES | SELECT_SMALL_RESULT)) ==
11263
OPTION_BIG_TABLES || (select_options & TMP_TABLE_FORCE_MYISAM))
11265
share->db_plugin= ha_lock_engine(0, myisam_hton);
11266
table->file= get_new_handler(share, &table->mem_root,
11269
(param->group_parts > table->file->max_key_parts() ||
11270
param->group_length > table->file->max_key_length()))
11271
using_unique_constraint=1;
11275
share->db_plugin= ha_lock_engine(0, heap_hton);
11276
table->file= get_new_handler(share, &table->mem_root,
11283
if (!using_unique_constraint)
11284
reclength+= group_null_items; // null flag is stored separately
11286
share->blob_fields= blob_count;
11287
if (blob_count == 0)
11289
/* We need to ensure that first byte is not 0 for the delete link */
11290
if (param->hidden_field_count)
11291
hidden_null_count++;
11295
hidden_null_pack_length=(hidden_null_count+7)/8;
11296
null_pack_length= (hidden_null_pack_length +
11297
(null_count + total_uneven_bit_length + 7) / 8);
11298
reclength+=null_pack_length;
11300
reclength=1; // Dummy select
11301
/* Use packed rows if there is blobs or a lot of space to gain */
11302
if (blob_count || ((string_total_length >= STRING_TOTAL_LENGTH_TO_PACK_ROWS) && (reclength / string_total_length <= RATIO_TO_PACK_ROWS || (string_total_length / string_count) >= AVG_STRING_LENGTH_TO_PACK_ROWS)))
11303
use_packed_rows= 1;
11305
share->reclength= reclength;
11307
uint alloc_length=ALIGN_SIZE(reclength+MI_UNIQUE_HASH_LENGTH+1);
11308
share->rec_buff_length= alloc_length;
11309
if (!(table->record[0]= (uchar*)
11310
alloc_root(&table->mem_root, alloc_length*3)))
11312
table->record[1]= table->record[0]+alloc_length;
11313
share->default_values= table->record[1]+alloc_length;
11315
copy_func[0]=0; // End marker
11316
param->func_count= copy_func - param->items_to_copy;
11318
setup_tmp_table_column_bitmaps(table, bitmaps);
11320
recinfo=param->start_recinfo;
11321
null_flags=(uchar*) table->record[0];
11322
pos=table->record[0]+ null_pack_length;
11323
if (null_pack_length)
11325
bzero((uchar*) recinfo,sizeof(*recinfo));
11326
recinfo->type=FIELD_NORMAL;
11327
recinfo->length=null_pack_length;
11329
bfill(null_flags,null_pack_length,255); // Set null fields
11331
table->null_flags= (uchar*) table->record[0];
11332
share->null_fields= null_count+ hidden_null_count;
11333
share->null_bytes= null_pack_length;
11335
null_count= (blob_count == 0) ? 1 : 0;
11336
hidden_field_count=param->hidden_field_count;
11337
for (i=0,reg_field=table->field; i < field_count; i++,reg_field++,recinfo++)
11339
Field *field= *reg_field;
11341
bzero((uchar*) recinfo,sizeof(*recinfo));
11343
if (!(field->flags & NOT_NULL_FLAG))
11345
if (field->flags & GROUP_FLAG && !using_unique_constraint)
11348
We have to reserve one byte here for NULL bits,
11349
as this is updated by 'end_update()'
11351
*pos++=0; // Null is stored here
11353
recinfo->type=FIELD_NORMAL;
11355
bzero((uchar*) recinfo,sizeof(*recinfo));
11359
recinfo->null_bit= 1 << (null_count & 7);
11360
recinfo->null_pos= null_count/8;
11362
field->move_field(pos,null_flags+null_count/8,
11363
1 << (null_count & 7));
11367
field->move_field(pos,(uchar*) 0,0);
11371
Test if there is a default field value. The test for ->ptr is to skip
11372
'offset' fields generated by initalize_tables
11374
if (default_field[i] && default_field[i]->ptr)
11377
default_field[i] is set only in the cases when 'field' can
11378
inherit the default value that is defined for the field referred
11379
by the Item_field object from which 'field' has been created.
11382
Field *orig_field= default_field[i];
11383
/* Get the value from default_values */
11384
diff= (my_ptrdiff_t) (orig_field->table->s->default_values-
11385
orig_field->table->record[0]);
11386
orig_field->move_field_offset(diff); // Points now at default_values
11387
if (orig_field->is_real_null())
11391
field->set_notnull();
11392
memcpy(field->ptr, orig_field->ptr, field->pack_length());
11394
orig_field->move_field_offset(-diff); // Back to record[0]
11398
{ /* Not a table Item */
11399
copy->set(field,from_field[i],save_sum_fields);
11402
length=field->pack_length();
11405
/* Make entry for create table */
11406
recinfo->length=length;
11407
if (field->flags & BLOB_FLAG)
11408
recinfo->type= (int) FIELD_BLOB;
11409
else if (use_packed_rows &&
11410
field->real_type() == MYSQL_TYPE_STRING &&
11411
length >= MIN_STRING_LENGTH_TO_PACK_ROWS)
11412
recinfo->type=FIELD_SKIP_ENDSPACE;
11414
recinfo->type=FIELD_NORMAL;
11415
if (!--hidden_field_count)
11416
null_count=(null_count+7) & ~7; // move to next byte
11418
// fix table name in field entry
11419
field->table_name= &table->alias;
11422
param->copy_field_end=copy;
11423
param->recinfo=recinfo;
11424
store_record(table,s->default_values); // Make empty default record
11426
if (thd->variables.tmp_table_size == ~ (uint64_t) 0) // No limit
11427
share->max_rows= ~(ha_rows) 0;
11429
share->max_rows= (ha_rows) (((share->db_type() == heap_hton) ?
11430
min(thd->variables.tmp_table_size,
11431
thd->variables.max_heap_table_size) :
11432
thd->variables.tmp_table_size) /
11434
set_if_bigger(share->max_rows,1); // For dummy start options
11436
Push the LIMIT clause to the temporary table creation, so that we
11437
materialize only up to 'rows_limit' records instead of all result records.
11439
set_if_smaller(share->max_rows, rows_limit);
11440
param->end_write_records= rows_limit;
11442
keyinfo= param->keyinfo;
11446
table->group=group; /* Table is grouped by key */
11447
param->group_buff=group_buff;
11449
share->uniques= test(using_unique_constraint);
11450
table->key_info=keyinfo;
11451
keyinfo->key_part=key_part_info;
11452
keyinfo->flags=HA_NOSAME;
11453
keyinfo->usable_key_parts=keyinfo->key_parts= param->group_parts;
11454
keyinfo->key_length=0;
11455
keyinfo->rec_per_key=0;
11456
keyinfo->algorithm= HA_KEY_ALG_UNDEF;
11457
keyinfo->name= (char*) "group_key";
11458
ORDER *cur_group= group;
11459
for (; cur_group ; cur_group= cur_group->next, key_part_info++)
11461
Field *field=(*cur_group->item)->get_tmp_table_field();
11462
bool maybe_null=(*cur_group->item)->maybe_null;
11463
key_part_info->null_bit=0;
11464
key_part_info->field= field;
11465
key_part_info->offset= field->offset(table->record[0]);
11466
key_part_info->length= (uint16_t) field->key_length();
11467
key_part_info->type= (uint8_t) field->key_type();
11468
key_part_info->key_type =
11469
((ha_base_keytype) key_part_info->type == HA_KEYTYPE_TEXT ||
11470
(ha_base_keytype) key_part_info->type == HA_KEYTYPE_VARTEXT1 ||
11471
(ha_base_keytype) key_part_info->type == HA_KEYTYPE_VARTEXT2) ?
11472
0 : FIELDFLAG_BINARY;
11473
if (!using_unique_constraint)
11475
cur_group->buff=(char*) group_buff;
11476
if (!(cur_group->field= field->new_key_field(thd->mem_root,table,
11481
goto err; /* purecov: inspected */
11485
To be able to group on NULL, we reserved place in group_buff
11486
for the NULL flag just before the column. (see above).
11487
The field data is after this flag.
11488
The NULL flag is updated in 'end_update()' and 'end_write()'
11490
keyinfo->flags|= HA_NULL_ARE_EQUAL; // def. that NULL == NULL
11491
key_part_info->null_bit=field->null_bit;
11492
key_part_info->null_offset= (uint) (field->null_ptr -
11493
(uchar*) table->record[0]);
11494
cur_group->buff++; // Pointer to field data
11495
group_buff++; // Skipp null flag
11497
/* In GROUP BY 'a' and 'a ' are equal for VARCHAR fields */
11498
key_part_info->key_part_flag|= HA_END_SPACE_ARE_EQUAL;
11499
group_buff+= cur_group->field->pack_length();
11501
keyinfo->key_length+= key_part_info->length;
11505
if (distinct && field_count != param->hidden_field_count)
11508
Create an unique key or an unique constraint over all columns
11509
that should be in the result. In the temporary table, there are
11510
'param->hidden_field_count' extra columns, whose null bits are stored
11511
in the first 'hidden_null_pack_length' bytes of the row.
11516
Special mode for index creation in MyISAM used to support unique
11517
indexes on blobs with arbitrary length. Such indexes cannot be
11522
null_pack_length-=hidden_null_pack_length;
11523
keyinfo->key_parts= ((field_count-param->hidden_field_count)+
11524
(share->uniques ? test(null_pack_length) : 0));
11525
table->distinct= 1;
11527
if (!(key_part_info= (KEY_PART_INFO*)
11528
alloc_root(&table->mem_root,
11529
keyinfo->key_parts * sizeof(KEY_PART_INFO))))
11531
bzero((void*) key_part_info, keyinfo->key_parts * sizeof(KEY_PART_INFO));
11532
table->key_info=keyinfo;
11533
keyinfo->key_part=key_part_info;
11534
keyinfo->flags=HA_NOSAME | HA_NULL_ARE_EQUAL;
11535
keyinfo->key_length=(uint16_t) reclength;
11536
keyinfo->name= (char*) "distinct_key";
11537
keyinfo->algorithm= HA_KEY_ALG_UNDEF;
11538
keyinfo->rec_per_key=0;
11541
Create an extra field to hold NULL bits so that unique indexes on
11542
blobs can distinguish NULL from 0. This extra field is not needed
11543
when we do not use UNIQUE indexes for blobs.
11545
if (null_pack_length && share->uniques)
11547
key_part_info->null_bit=0;
11548
key_part_info->offset=hidden_null_pack_length;
11549
key_part_info->length=null_pack_length;
11550
key_part_info->field= new Field_string(table->record[0],
11551
(uint32_t) key_part_info->length,
11555
NullS, &my_charset_bin);
11556
if (!key_part_info->field)
11558
key_part_info->field->init(table);
11559
key_part_info->key_type=FIELDFLAG_BINARY;
11560
key_part_info->type= HA_KEYTYPE_BINARY;
11563
/* Create a distinct key over the columns we are going to return */
11564
for (i=param->hidden_field_count, reg_field=table->field + i ;
11566
i++, reg_field++, key_part_info++)
11568
key_part_info->null_bit=0;
11569
key_part_info->field= *reg_field;
11570
key_part_info->offset= (*reg_field)->offset(table->record[0]);
11571
key_part_info->length= (uint16_t) (*reg_field)->pack_length();
11573
The below method of computing the key format length of the
11574
key part is a copy/paste from opt_range.cc, and table.cc.
11575
This should be factored out, e.g. as a method of Field.
11576
In addition it is not clear if any of the Field::*_length
11577
methods is supposed to compute the same length. If so, it
11580
key_part_info->store_length= key_part_info->length;
11582
if ((*reg_field)->real_maybe_null())
11583
key_part_info->store_length+= HA_KEY_NULL_LENGTH;
11584
if ((*reg_field)->type() == MYSQL_TYPE_BLOB ||
11585
(*reg_field)->real_type() == MYSQL_TYPE_VARCHAR)
11586
key_part_info->store_length+= HA_KEY_BLOB_LENGTH;
11588
key_part_info->type= (uint8_t) (*reg_field)->key_type();
11589
key_part_info->key_type =
11590
((ha_base_keytype) key_part_info->type == HA_KEYTYPE_TEXT ||
11591
(ha_base_keytype) key_part_info->type == HA_KEYTYPE_VARTEXT1 ||
11592
(ha_base_keytype) key_part_info->type == HA_KEYTYPE_VARTEXT2) ?
11593
0 : FIELDFLAG_BINARY;
11597
if (thd->is_fatal_error) // If end of memory
11598
goto err; /* purecov: inspected */
11599
share->db_record_offset= 1;
11600
if (share->db_type() == myisam_hton)
11602
if (create_myisam_tmp_table(table, param->keyinfo, param->start_recinfo,
11603
¶m->recinfo, select_options))
11606
if (open_tmp_table(table))
11609
thd->mem_root= mem_root_save;
11614
thd->mem_root= mem_root_save;
11615
free_tmp_table(thd,table); /* purecov: inspected */
11616
if (temp_pool_slot != MY_BIT_NONE)
11617
bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
11618
return(NULL); /* purecov: inspected */
11625
Create a temporary table to weed out duplicate rowid combinations
11629
create_duplicate_weedout_tmp_table()
11631
uniq_tuple_length_arg
11635
Create a temporary table to weed out duplicate rowid combinations. The
11636
table has a single column that is a concatenation of all rowids in the
11639
Depending on the needed length, there are two cases:
11641
1. When the length of the column < max_key_length:
11643
CREATE TABLE tmp (col VARBINARY(n) NOT NULL, UNIQUE KEY(col));
11645
2. Otherwise (not a valid SQL syntax but internally supported):
11647
CREATE TABLE tmp (col VARBINARY NOT NULL, UNIQUE CONSTRAINT(col));
11649
The code in this function was produced by extraction of relevant parts
11650
from create_tmp_table().
11657
TABLE *create_duplicate_weedout_tmp_table(THD *thd,
11658
uint uniq_tuple_length_arg,
11659
SJ_TMP_TABLE *sjtbl)
11661
MEM_ROOT *mem_root_save, own_root;
11663
TABLE_SHARE *share;
11664
uint temp_pool_slot=MY_BIT_NONE;
11665
char *tmpname,path[FN_REFLEN];
11667
KEY_PART_INFO *key_part_info;
11672
MI_COLUMNDEF *recinfo, *start_recinfo;
11673
bool using_unique_constraint=false;
11674
bool use_packed_rows= false;
11675
Field *field, *key_field;
11676
uint blob_count, null_pack_length, null_count;
11681
STEP 1: Get temporary table name
11683
statistic_increment(thd->status_var.created_tmp_tables, &LOCK_status);
11684
if (use_temp_pool && !(test_flags & TEST_KEEP_TMP_TABLES))
11685
temp_pool_slot = bitmap_lock_set_next(&temp_pool);
11687
if (temp_pool_slot != MY_BIT_NONE) // we got a slot
11688
sprintf(path, "%s_%lx_%i", tmp_file_prefix,
11689
current_pid, temp_pool_slot);
11692
/* if we run out of slots or we are not using tempool */
11693
sprintf(path,"%s%lx_%lx_%x", tmp_file_prefix,current_pid,
11694
thd->thread_id, thd->tmp_table++);
11696
fn_format(path, path, mysql_tmpdir, "", MY_REPLACE_EXT|MY_UNPACK_FILENAME);
11698
/* STEP 2: Figure if we'll be using a key or blob+constraint */
11699
if (uniq_tuple_length_arg >= CONVERT_IF_BIGGER_TO_BLOB)
11700
using_unique_constraint= true;
11702
/* STEP 3: Allocate memory for temptable description */
11703
init_sql_alloc(&own_root, TABLE_ALLOC_BLOCK_SIZE, 0);
11704
if (!multi_alloc_root(&own_root,
11705
&table, sizeof(*table),
11706
&share, sizeof(*share),
11707
®_field, sizeof(Field*) * (1+1),
11708
&blob_field, sizeof(uint)*2,
11709
&keyinfo, sizeof(*keyinfo),
11710
&key_part_info, sizeof(*key_part_info) * 2,
11712
sizeof(*recinfo)*(1*2+4),
11713
&tmpname, (uint) strlen(path)+1,
11714
&group_buff, (!using_unique_constraint ?
11715
uniq_tuple_length_arg : 0),
11716
&bitmaps, bitmap_buffer_size(1)*2,
11719
if (temp_pool_slot != MY_BIT_NONE)
11720
bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
11723
strmov(tmpname,path);
11726
/* STEP 4: Create TABLE description */
11727
bzero((char*) table,sizeof(*table));
11728
bzero((char*) reg_field,sizeof(Field*)*2);
11730
table->mem_root= own_root;
11731
mem_root_save= thd->mem_root;
11732
thd->mem_root= &table->mem_root;
11734
table->field=reg_field;
11735
table->alias= "weedout-tmp";
11736
table->reginfo.lock_type=TL_WRITE; /* Will be updated */
11737
table->db_stat=HA_OPEN_KEYFILE+HA_OPEN_RNDFILE;
11739
table->temp_pool_slot = temp_pool_slot;
11740
table->copy_blobs= 1;
11741
table->in_use= thd;
11742
table->quick_keys.init();
11743
table->covering_keys.init();
11744
table->keys_in_use_for_query.init();
11747
init_tmp_table_share(thd, share, "", 0, tmpname, tmpname);
11748
share->blob_field= blob_field;
11749
share->blob_ptr_size= portable_sizeof_char_ptr;
11750
share->db_low_byte_first=1; // True for HEAP and MyISAM
11751
share->table_charset= NULL;
11752
share->primary_key= MAX_KEY; // Indicate no primary key
11753
share->keys_for_keyread.init();
11754
share->keys_in_use.init();
11758
/* Create the field */
11761
For the sake of uniformity, always use Field_varstring (altough we could
11762
use Field_string for shorter keys)
11764
field= new Field_varstring(uniq_tuple_length_arg, false, "rowids", share,
11768
field->table= table;
11769
field->key_start.init(0);
11770
field->part_of_key.init(0);
11771
field->part_of_sortkey.init(0);
11772
field->unireg_check= Field::NONE;
11773
field->flags= (NOT_NULL_FLAG | BINARY_FLAG | NO_DEFAULT_VALUE_FLAG);
11774
field->reset_fields();
11775
field->init(table);
11776
field->orig_table= NULL;
11778
field->field_index= 0;
11780
*(reg_field++)= field;
11785
share->blob_fields= 0;
11788
uint reclength= field->pack_length();
11789
if (using_unique_constraint)
11791
share->db_plugin= ha_lock_engine(0, myisam_hton);
11792
table->file= get_new_handler(share, &table->mem_root,
11794
assert(uniq_tuple_length_arg <= table->file->max_key_length());
11798
share->db_plugin= ha_lock_engine(0, heap_hton);
11799
table->file= get_new_handler(share, &table->mem_root,
11807
null_pack_length= 1;
11808
reclength += null_pack_length;
11810
share->reclength= reclength;
11812
uint alloc_length=ALIGN_SIZE(share->reclength + MI_UNIQUE_HASH_LENGTH+1);
11813
share->rec_buff_length= alloc_length;
11814
if (!(table->record[0]= (uchar*)
11815
alloc_root(&table->mem_root, alloc_length*3)))
11817
table->record[1]= table->record[0]+alloc_length;
11818
share->default_values= table->record[1]+alloc_length;
11820
setup_tmp_table_column_bitmaps(table, bitmaps);
11822
recinfo= start_recinfo;
11823
null_flags=(uchar*) table->record[0];
11824
pos=table->record[0]+ null_pack_length;
11825
if (null_pack_length)
11827
bzero((uchar*) recinfo,sizeof(*recinfo));
11828
recinfo->type=FIELD_NORMAL;
11829
recinfo->length=null_pack_length;
11831
bfill(null_flags,null_pack_length,255); // Set null fields
11833
table->null_flags= (uchar*) table->record[0];
11834
share->null_fields= null_count;
11835
share->null_bytes= null_pack_length;
11840
//Field *field= *reg_field;
11842
bzero((uchar*) recinfo,sizeof(*recinfo));
11843
field->move_field(pos,(uchar*) 0,0);
11847
Test if there is a default field value. The test for ->ptr is to skip
11848
'offset' fields generated by initalize_tables
11850
// Initialize the table field:
11851
bzero(field->ptr, field->pack_length());
11853
length=field->pack_length();
11856
/* Make entry for create table */
11857
recinfo->length=length;
11858
if (field->flags & BLOB_FLAG)
11859
recinfo->type= (int) FIELD_BLOB;
11860
else if (use_packed_rows &&
11861
field->real_type() == MYSQL_TYPE_STRING &&
11862
length >= MIN_STRING_LENGTH_TO_PACK_ROWS)
11863
recinfo->type=FIELD_SKIP_ENDSPACE;
11865
recinfo->type=FIELD_NORMAL;
11867
field->table_name= &table->alias;
11870
//param->recinfo=recinfo;
11871
//store_record(table,s->default_values); // Make empty default record
11873
if (thd->variables.tmp_table_size == ~ (uint64_t) 0) // No limit
11874
share->max_rows= ~(ha_rows) 0;
11876
share->max_rows= (ha_rows) (((share->db_type() == heap_hton) ?
11877
min(thd->variables.tmp_table_size,
11878
thd->variables.max_heap_table_size) :
11879
thd->variables.tmp_table_size) /
11881
set_if_bigger(share->max_rows,1); // For dummy start options
11884
//// keyinfo= param->keyinfo;
11888
share->uniques= test(using_unique_constraint);
11889
table->key_info=keyinfo;
11890
keyinfo->key_part=key_part_info;
11891
keyinfo->flags=HA_NOSAME;
11892
keyinfo->usable_key_parts= keyinfo->key_parts= 1;
11893
keyinfo->key_length=0;
11894
keyinfo->rec_per_key=0;
11895
keyinfo->algorithm= HA_KEY_ALG_UNDEF;
11896
keyinfo->name= (char*) "weedout_key";
11898
key_part_info->null_bit=0;
11899
key_part_info->field= field;
11900
key_part_info->offset= field->offset(table->record[0]);
11901
key_part_info->length= (uint16_t) field->key_length();
11902
key_part_info->type= (uint8_t) field->key_type();
11903
key_part_info->key_type = FIELDFLAG_BINARY;
11904
if (!using_unique_constraint)
11906
if (!(key_field= field->new_key_field(thd->mem_root, table,
11911
key_part_info->key_part_flag|= HA_END_SPACE_ARE_EQUAL; //todo need this?
11913
keyinfo->key_length+= key_part_info->length;
11917
if (thd->is_fatal_error) // If end of memory
11919
share->db_record_offset= 1;
11920
if (share->db_type() == myisam_hton)
11923
if (create_myisam_tmp_table(table, keyinfo, start_recinfo, &recinfo, 0))
11926
sjtbl->start_recinfo= start_recinfo;
11927
sjtbl->recinfo= recinfo;
11928
if (open_tmp_table(table))
11931
thd->mem_root= mem_root_save;
11935
thd->mem_root= mem_root_save;
11936
free_tmp_table(thd,table); /* purecov: inspected */
11937
if (temp_pool_slot != MY_BIT_NONE)
11938
bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
11939
return(NULL); /* purecov: inspected */
11942
/****************************************************************************/
11945
Create a reduced TABLE object with properly set up Field list from a
11946
list of field definitions.
11948
The created table doesn't have a table handler associated with
11949
it, has no keys, no group/distinct, no copy_funcs array.
11950
The sole purpose of this TABLE object is to use the power of Field
11951
class to read/write data to/from table->record[0]. Then one can store
11952
the record in any container (RB tree, hash, etc).
11953
The table is created in THD mem_root, so are the table's fields.
11954
Consequently, if you don't BLOB fields, you don't need to free it.
11956
@param thd connection handle
11957
@param field_list list of column definitions
11960
0 if out of memory, TABLE object in case of success
11963
TABLE *create_virtual_tmp_table(THD *thd, List<Create_field> &field_list)
11965
uint field_count= field_list.elements;
11966
uint blob_count= 0;
11968
Create_field *cdef; /* column definition */
11969
uint record_length= 0;
11970
uint null_count= 0; /* number of columns which may be null */
11971
uint null_pack_length; /* NULL representation array length */
11975
TABLE_SHARE *share;
11977
if (!multi_alloc_root(thd->mem_root,
11978
&table, sizeof(*table),
11979
&share, sizeof(*share),
11980
&field, (field_count + 1) * sizeof(Field*),
11981
&blob_field, (field_count+1) *sizeof(uint),
11982
&bitmaps, bitmap_buffer_size(field_count)*2,
11986
bzero(table, sizeof(*table));
11987
bzero(share, sizeof(*share));
11988
table->field= field;
11990
share->blob_field= blob_field;
11991
share->fields= field_count;
11992
share->blob_ptr_size= portable_sizeof_char_ptr;
11993
setup_tmp_table_column_bitmaps(table, bitmaps);
11995
/* Create all fields and calculate the total length of record */
11996
List_iterator_fast<Create_field> it(field_list);
11997
while ((cdef= it++))
11999
*field= make_field(share, 0, cdef->length,
12000
(uchar*) (f_maybe_null(cdef->pack_flag) ? "" : 0),
12001
f_maybe_null(cdef->pack_flag) ? 1 : 0,
12002
cdef->pack_flag, cdef->sql_type, cdef->charset,
12003
cdef->unireg_check,
12004
cdef->interval, cdef->field_name);
12007
(*field)->init(table);
12008
record_length+= (*field)->pack_length();
12009
if (! ((*field)->flags & NOT_NULL_FLAG))
12012
if ((*field)->flags & BLOB_FLAG)
12013
share->blob_field[blob_count++]= (uint) (field - table->field);
12017
*field= NULL; /* mark the end of the list */
12018
share->blob_field[blob_count]= 0; /* mark the end of the list */
12019
share->blob_fields= blob_count;
12021
null_pack_length= (null_count + 7)/8;
12022
share->reclength= record_length + null_pack_length;
12023
share->rec_buff_length= ALIGN_SIZE(share->reclength + 1);
12024
table->record[0]= (uchar*) thd->alloc(share->rec_buff_length);
12025
if (!table->record[0])
12028
if (null_pack_length)
12030
table->null_flags= (uchar*) table->record[0];
12031
share->null_fields= null_count;
12032
share->null_bytes= null_pack_length;
12035
table->in_use= thd; /* field->reset() may access table->in_use */
12037
/* Set up field pointers */
12038
uchar *null_pos= table->record[0];
12039
uchar *field_pos= null_pos + share->null_bytes;
12042
for (field= table->field; *field; ++field)
12044
Field *cur_field= *field;
12045
if ((cur_field->flags & NOT_NULL_FLAG))
12046
cur_field->move_field(field_pos);
12049
cur_field->move_field(field_pos, (uchar*) null_pos, null_bit);
12051
if (null_bit == (1 << 8))
12057
cur_field->reset();
12059
field_pos+= cur_field->pack_length();
12064
for (field= table->field; *field; ++field)
12065
delete *field; /* just invokes field destructor */
12070
static bool open_tmp_table(TABLE *table)
12073
if ((error=table->file->ha_open(table, table->s->table_name.str,O_RDWR,
12074
HA_OPEN_TMP_TABLE | HA_OPEN_INTERNAL_TABLE)))
12076
table->file->print_error(error,MYF(0)); /* purecov: inspected */
12080
(void) table->file->extra(HA_EXTRA_QUICK); /* Faster */
12086
Create MyISAM temporary table
12089
create_myisam_tmp_table()
12090
table Table object that descrimes the table to be created
12091
keyinfo Description of the index (there is always one index)
12092
start_recinfo MyISAM's column descriptions
12093
recinfo INOUT End of MyISAM's column descriptions
12094
options Option bits
12097
Create a MyISAM temporary table according to passed description. The is
12098
assumed to have one unique index or constraint.
12100
The passed array or MI_COLUMNDEF structures must have this form:
12102
1. 1-byte column (afaiu for 'deleted' flag) (note maybe not 1-byte
12103
when there are many nullable columns)
12105
3. One free MI_COLUMNDEF element (*recinfo points here)
12107
This function may use the free element to create hash column for unique
12115
static bool create_myisam_tmp_table(TABLE *table, KEY *keyinfo,
12116
MI_COLUMNDEF *start_recinfo,
12117
MI_COLUMNDEF **recinfo,
12122
MI_UNIQUEDEF uniquedef;
12123
TABLE_SHARE *share= table->s;
12126
{ // Get keys for ni_create
12127
bool using_unique_constraint=0;
12128
HA_KEYSEG *seg= (HA_KEYSEG*) alloc_root(&table->mem_root,
12129
sizeof(*seg) * keyinfo->key_parts);
12133
bzero(seg, sizeof(*seg) * keyinfo->key_parts);
12134
if (keyinfo->key_length >= table->file->max_key_length() ||
12135
keyinfo->key_parts > table->file->max_key_parts() ||
12138
/* Can't create a key; Make a unique constraint instead of a key */
12141
using_unique_constraint=1;
12142
bzero((char*) &uniquedef,sizeof(uniquedef));
12143
uniquedef.keysegs=keyinfo->key_parts;
12145
uniquedef.null_are_equal=1;
12147
/* Create extra column for hash value */
12148
bzero((uchar*) *recinfo,sizeof(**recinfo));
12149
(*recinfo)->type= FIELD_CHECK;
12150
(*recinfo)->length=MI_UNIQUE_HASH_LENGTH;
12152
share->reclength+=MI_UNIQUE_HASH_LENGTH;
12156
/* Create an unique key */
12157
bzero((char*) &keydef,sizeof(keydef));
12158
keydef.flag=HA_NOSAME | HA_BINARY_PACK_KEY | HA_PACK_KEY;
12159
keydef.keysegs= keyinfo->key_parts;
12162
for (uint i=0; i < keyinfo->key_parts ; i++,seg++)
12164
Field *field=keyinfo->key_part[i].field;
12166
seg->language= field->charset()->number;
12167
seg->length= keyinfo->key_part[i].length;
12168
seg->start= keyinfo->key_part[i].offset;
12169
if (field->flags & BLOB_FLAG)
12172
((keyinfo->key_part[i].key_type & FIELDFLAG_BINARY) ?
12173
HA_KEYTYPE_VARBINARY2 : HA_KEYTYPE_VARTEXT2);
12174
seg->bit_start= (uint8_t)(field->pack_length() - share->blob_ptr_size);
12175
seg->flag= HA_BLOB_PART;
12176
seg->length=0; // Whole blob in unique constraint
12180
seg->type= keyinfo->key_part[i].type;
12181
/* Tell handler if it can do suffic space compression */
12182
if (field->real_type() == MYSQL_TYPE_STRING &&
12183
keyinfo->key_part[i].length > 4)
12184
seg->flag|= HA_SPACE_PACK;
12186
if (!(field->flags & NOT_NULL_FLAG))
12188
seg->null_bit= field->null_bit;
12189
seg->null_pos= (uint) (field->null_ptr - (uchar*) table->record[0]);
12191
We are using a GROUP BY on something that contains NULL
12192
In this case we have to tell MyISAM that two NULL should
12193
on INSERT be regarded at the same value
12195
if (!using_unique_constraint)
12196
keydef.flag|= HA_NULL_ARE_EQUAL;
12200
MI_CREATE_INFO create_info;
12201
bzero((char*) &create_info,sizeof(create_info));
12203
if ((options & (OPTION_BIG_TABLES | SELECT_SMALL_RESULT)) ==
12205
create_info.data_file_length= ~(uint64_t) 0;
12207
if ((error=mi_create(share->table_name.str, share->keys, &keydef,
12208
(uint) (*recinfo-start_recinfo),
12210
share->uniques, &uniquedef,
12212
HA_CREATE_TMP_TABLE)))
12214
table->file->print_error(error,MYF(0)); /* purecov: inspected */
12218
status_var_increment(table->in_use->status_var.created_tmp_disk_tables);
12219
share->db_record_offset= 1;
12227
free_tmp_table(THD *thd, TABLE *entry)
12229
MEM_ROOT own_root= entry->mem_root;
12230
const char *save_proc_info;
12232
save_proc_info=thd->proc_info;
12233
thd_proc_info(thd, "removing tmp table");
12237
if (entry->db_stat)
12238
entry->file->ha_drop_table(entry->s->table_name.str);
12240
entry->file->ha_delete_table(entry->s->table_name.str);
12241
delete entry->file;
12245
for (Field **ptr=entry->field ; *ptr ; ptr++)
12247
free_io_cache(entry);
12249
if (entry->temp_pool_slot != MY_BIT_NONE)
12250
bitmap_lock_clear_bit(&temp_pool, entry->temp_pool_slot);
12252
plugin_unlock(0, entry->s->db_plugin);
12254
free_root(&own_root, MYF(0)); /* the table is allocated in its own root */
12255
thd_proc_info(thd, save_proc_info);
12261
If a HEAP table gets full, create a MyISAM table and copy all rows
12265
bool create_myisam_from_heap(THD *thd, TABLE *table,
12266
MI_COLUMNDEF *start_recinfo,
12267
MI_COLUMNDEF **recinfo,
12268
int error, bool ignore_last_dupp_key_error)
12272
const char *save_proc_info;
12275
if (table->s->db_type() != heap_hton ||
12276
error != HA_ERR_RECORD_FILE_FULL)
12278
table->file->print_error(error,MYF(0));
12283
new_table.s= &share;
12284
new_table.s->db_plugin= ha_lock_engine(thd, myisam_hton);
12285
if (!(new_table.file= get_new_handler(&share, &new_table.mem_root,
12286
new_table.s->db_type())))
12287
return(1); // End of memory
12289
save_proc_info=thd->proc_info;
12290
thd_proc_info(thd, "converting HEAP to MyISAM");
12292
if (create_myisam_tmp_table(&new_table, table->key_info, start_recinfo,
12293
recinfo, thd->lex->select_lex.options |
12296
if (open_tmp_table(&new_table))
12298
if (table->file->indexes_are_disabled())
12299
new_table.file->ha_disable_indexes(HA_KEY_SWITCH_ALL);
12300
table->file->ha_index_or_rnd_end();
12301
table->file->ha_rnd_init(1);
12302
if (table->no_rows)
12304
new_table.file->extra(HA_EXTRA_NO_ROWS);
12305
new_table.no_rows=1;
12308
#ifdef TO_BE_DONE_LATER_IN_4_1
12310
To use start_bulk_insert() (which is new in 4.1) we need to find
12311
all places where a corresponding end_bulk_insert() should be put.
12313
table->file->info(HA_STATUS_VARIABLE); /* update table->file->stats.records */
12314
new_table.file->ha_start_bulk_insert(table->file->stats.records);
12316
/* HA_EXTRA_WRITE_CACHE can stay until close, no need to disable it */
12317
new_table.file->extra(HA_EXTRA_WRITE_CACHE);
12321
copy all old rows from heap table to MyISAM table
12322
This is the only code that uses record[1] to read/write but this
12323
is safe as this is a temporary MyISAM table without timestamp/autoincrement.
12325
while (!table->file->rnd_next(new_table.record[1]))
12327
write_err= new_table.file->ha_write_row(new_table.record[1]);
12331
/* copy row that filled HEAP table */
12332
if ((write_err=new_table.file->ha_write_row(table->record[0])))
12334
if (new_table.file->is_fatal_error(write_err, HA_CHECK_DUP) ||
12335
!ignore_last_dupp_key_error)
12339
/* remove heap table and change to use myisam table */
12340
(void) table->file->ha_rnd_end();
12341
(void) table->file->close(); // This deletes the table !
12342
delete table->file;
12344
plugin_unlock(0, table->s->db_plugin);
12345
share.db_plugin= my_plugin_lock(0, &share.db_plugin);
12346
new_table.s= table->s; // Keep old share
12350
table->file->change_table_ptr(table, table->s);
12351
table->use_all_columns();
12352
if (save_proc_info)
12354
const char *new_proc_info=
12355
(!strcmp(save_proc_info,"Copying to tmp table") ?
12356
"Copying to tmp table on disk" : save_proc_info);
12357
thd_proc_info(thd, new_proc_info);
12362
table->file->print_error(write_err, MYF(0));
12363
(void) table->file->ha_rnd_end();
12364
(void) new_table.file->close();
12366
new_table.file->ha_delete_table(new_table.s->table_name.str);
12368
delete new_table.file;
12369
thd_proc_info(thd, save_proc_info);
12370
table->mem_root= new_table.mem_root;