10526
/****************************************************************************
10527
Create internal temporary table
10528
****************************************************************************/
10531
Create field for temporary table from given field.
10533
@param thd Thread handler
10534
@param org_field field from which new field will be created
10535
@param name New field name
10536
@param table Temporary table
10537
@param item !=NULL if item->result_field should point to new field.
10538
This is relevant for how fill_record() is going to work:
10539
If item != NULL then fill_record() will update
10540
the record in the original table.
10541
If item == NULL then fill_record() will update
10542
the temporary table
10543
@param convert_blob_length If >0 create a varstring(convert_blob_length)
10544
field instead of blob.
10552
Field *create_tmp_field_from_field(THD *thd, Field *org_field,
10553
const char *name, TABLE *table,
10554
Item_field *item, uint convert_blob_length)
10559
Make sure that the blob fits into a Field_varstring which has
10562
if (convert_blob_length && convert_blob_length <= Field_varstring::MAX_SIZE &&
10563
(org_field->flags & BLOB_FLAG))
10564
new_field= new Field_varstring(convert_blob_length,
10565
org_field->maybe_null(),
10566
org_field->field_name, table->s,
10567
org_field->charset());
10569
new_field= org_field->new_field(thd->mem_root, table,
10570
table == org_field->table);
10573
new_field->init(table);
10574
new_field->orig_table= org_field->orig_table;
10576
item->result_field= new_field;
10578
new_field->field_name= name;
10579
new_field->flags|= (org_field->flags & NO_DEFAULT_VALUE_FLAG);
10580
if (org_field->maybe_null() || (item && item->maybe_null))
10581
new_field->flags&= ~NOT_NULL_FLAG; // Because of outer join
10582
if (org_field->type() == DRIZZLE_TYPE_VARCHAR)
10583
table->s->db_create_options|= HA_OPTION_PACK_RECORD;
10584
else if (org_field->type() == DRIZZLE_TYPE_DOUBLE)
10585
((Field_double *) new_field)->not_fixed= true;
10591
Create field for temporary table using type of given item.
10593
@param thd Thread handler
10594
@param item Item to create a field for
10595
@param table Temporary table
10596
@param copy_func If set and item is a function, store copy of
10598
@param modify_item 1 if item->result_field should point to new
10599
item. This is relevent for how fill_record()
10601
If modify_item is 1 then fill_record() will
10602
update the record in the original table.
10603
If modify_item is 0 then fill_record() will
10604
update the temporary table
10605
@param convert_blob_length If >0 create a varstring(convert_blob_length)
10606
field instead of blob.
10614
static Field *create_tmp_field_from_item(THD *thd __attribute__((unused)),
10615
Item *item, TABLE *table,
10616
Item ***copy_func, bool modify_item,
10617
uint convert_blob_length)
10619
bool maybe_null= item->maybe_null;
10622
switch (item->result_type()) {
10624
new_field= new Field_double(item->max_length, maybe_null,
10625
item->name, item->decimals, true);
10629
Select an integer type with the minimal fit precision.
10630
MY_INT32_NUM_DECIMAL_DIGITS is sign inclusive, don't consider the sign.
10631
Values with MY_INT32_NUM_DECIMAL_DIGITS digits may or may not fit into
10632
Field_long : make them Field_int64_t.
10634
if (item->max_length >= (MY_INT32_NUM_DECIMAL_DIGITS - 1))
10635
new_field=new Field_int64_t(item->max_length, maybe_null,
10636
item->name, item->unsigned_flag);
10638
new_field=new Field_long(item->max_length, maybe_null,
10639
item->name, item->unsigned_flag);
10641
case STRING_RESULT:
10642
assert(item->collation.collation);
10644
enum enum_field_types type;
10646
DATE/TIME fields have STRING_RESULT result type.
10647
To preserve type they needed to be handled separately.
10649
if ((type= item->field_type()) == DRIZZLE_TYPE_DATETIME ||
10650
type == DRIZZLE_TYPE_TIME || type == DRIZZLE_TYPE_NEWDATE ||
10651
type == DRIZZLE_TYPE_TIMESTAMP)
10652
new_field= item->tmp_table_field_from_field_type(table, 1);
10654
Make sure that the blob fits into a Field_varstring which has
10657
else if (item->max_length/item->collation.collation->mbmaxlen > 255 &&
10658
convert_blob_length <= Field_varstring::MAX_SIZE &&
10659
convert_blob_length)
10660
new_field= new Field_varstring(convert_blob_length, maybe_null,
10661
item->name, table->s,
10662
item->collation.collation);
10664
new_field= item->make_string_field(table);
10665
new_field->set_derivation(item->collation.derivation);
10667
case DECIMAL_RESULT:
10669
uint8_t dec= item->decimals;
10670
uint8_t intg= ((Item_decimal *) item)->decimal_precision() - dec;
10671
uint32_t len= item->max_length;
10674
Trying to put too many digits overall in a DECIMAL(prec,dec)
10675
will always throw a warning. We must limit dec to
10676
DECIMAL_MAX_SCALE however to prevent an assert() later.
10681
signed int overflow;
10683
dec= min(dec, (uint8_t)DECIMAL_MAX_SCALE);
10686
If the value still overflows the field with the corrected dec,
10687
we'll throw out decimals rather than integers. This is still
10688
bad and of course throws a truncation warning.
10689
+1: for decimal point
10692
overflow= my_decimal_precision_to_length(intg + dec, dec,
10693
item->unsigned_flag) - len;
10696
dec= max(0, dec - overflow); // too long, discard fract
10698
len -= item->decimals - dec; // corrected value fits
10701
new_field= new Field_new_decimal(len, maybe_null, item->name,
10702
dec, item->unsigned_flag);
10707
// This case should never be choosen
10713
new_field->init(table);
10715
if (copy_func && item->is_result_field())
10716
*((*copy_func)++) = item; // Save for copy_funcs
10718
item->set_result_field(new_field);
10719
if (item->type() == Item::NULL_ITEM)
10720
new_field->is_created_from_null_item= true;
10726
Create field for information schema table.
10728
@param thd Thread handler
10729
@param table Temporary table
10730
@param item Item to create a field for
10738
Field *create_tmp_field_for_schema(THD *thd __attribute__((unused)),
10739
Item *item, TABLE *table)
10741
if (item->field_type() == DRIZZLE_TYPE_VARCHAR)
10744
if (item->max_length > MAX_FIELD_VARCHARLENGTH)
10745
field= new Field_blob(item->max_length, item->maybe_null,
10746
item->name, item->collation.collation);
10748
field= new Field_varstring(item->max_length, item->maybe_null,
10750
table->s, item->collation.collation);
10752
field->init(table);
10755
return item->tmp_table_field_from_field_type(table, 0);
10760
Create field for temporary table.
10762
@param thd Thread handler
10763
@param table Temporary table
10764
@param item Item to create a field for
10765
@param type Type of item (normally item->type)
10766
@param copy_func If set and item is a function, store copy of item
10768
@param from_field if field will be created using other field as example,
10769
pointer example field will be written here
10770
@param default_field If field has a default value field, store it here
10771
@param group 1 if we are going to do a relative group by on result
10772
@param modify_item 1 if item->result_field should point to new item.
10773
This is relevent for how fill_record() is going to
10775
If modify_item is 1 then fill_record() will update
10776
the record in the original table.
10777
If modify_item is 0 then fill_record() will update
10778
the temporary table
10779
@param convert_blob_length If >0 create a varstring(convert_blob_length)
10780
field instead of blob.
10788
Field *create_tmp_field(THD *thd, TABLE *table,Item *item, Item::Type type,
10789
Item ***copy_func, Field **from_field,
10790
Field **default_field,
10791
bool group, bool modify_item,
10792
bool table_cant_handle_bit_fields __attribute__((unused)),
10793
bool make_copy_field,
10794
uint convert_blob_length)
10797
Item::Type orig_type= type;
10798
Item *orig_item= 0;
10800
if (type != Item::FIELD_ITEM &&
10801
item->real_item()->type() == Item::FIELD_ITEM)
10804
item= item->real_item();
10805
type= Item::FIELD_ITEM;
10809
case Item::SUM_FUNC_ITEM:
10811
Item_sum *item_sum=(Item_sum*) item;
10812
result= item_sum->create_tmp_field(group, table, convert_blob_length);
10814
my_error(ER_OUT_OF_RESOURCES, MYF(ME_FATALERROR));
10817
case Item::FIELD_ITEM:
10818
case Item::DEFAULT_VALUE_ITEM:
10820
Item_field *field= (Item_field*) item;
10821
bool orig_modify= modify_item;
10822
if (orig_type == Item::REF_ITEM)
10825
If item have to be able to store NULLs but underlaid field can't do it,
10826
create_tmp_field_from_field() can't be used for tmp field creation.
10828
if (field->maybe_null && !field->field->maybe_null())
10830
result= create_tmp_field_from_item(thd, item, table, NULL,
10831
modify_item, convert_blob_length);
10832
*from_field= field->field;
10833
if (result && modify_item)
10834
field->result_field= result;
10837
result= create_tmp_field_from_field(thd, (*from_field= field->field),
10838
orig_item ? orig_item->name :
10841
modify_item ? field :
10843
convert_blob_length);
10844
if (orig_type == Item::REF_ITEM && orig_modify)
10845
((Item_ref*)orig_item)->set_result_field(result);
10846
if (field->field->eq_def(result))
10847
*default_field= field->field;
10851
case Item::FUNC_ITEM:
10853
case Item::COND_ITEM:
10854
case Item::FIELD_AVG_ITEM:
10855
case Item::FIELD_STD_ITEM:
10856
case Item::SUBSELECT_ITEM:
10857
/* The following can only happen with 'CREATE TABLE ... SELECT' */
10858
case Item::PROC_ITEM:
10859
case Item::INT_ITEM:
10860
case Item::REAL_ITEM:
10861
case Item::DECIMAL_ITEM:
10862
case Item::STRING_ITEM:
10863
case Item::REF_ITEM:
10864
case Item::NULL_ITEM:
10865
case Item::VARBIN_ITEM:
10866
if (make_copy_field)
10868
assert(((Item_result_field*)item)->result_field);
10869
*from_field= ((Item_result_field*)item)->result_field;
10871
return create_tmp_field_from_item(thd, item, table,
10872
(make_copy_field ? 0 : copy_func),
10873
modify_item, convert_blob_length);
10874
case Item::TYPE_HOLDER:
10875
result= ((Item_type_holder *)item)->make_field_by_type(table);
10876
result->set_derivation(item->collation.derivation);
10878
default: // Dosen't have to be stored
10884
Set up column usage bitmaps for a temporary table
10887
For temporary tables, we need one bitmap with all columns set and
10888
a tmp_set bitmap to be used by things like filesort.
10891
void setup_tmp_table_column_bitmaps(TABLE *table, uchar *bitmaps)
10893
uint field_count= table->s->fields;
10894
bitmap_init(&table->def_read_set, (my_bitmap_map*) bitmaps, field_count,
10896
bitmap_init(&table->tmp_set,
10897
(my_bitmap_map*) (bitmaps+ bitmap_buffer_size(field_count)),
10898
field_count, false);
10899
/* write_set and all_set are copies of read_set */
10900
table->def_write_set= table->def_read_set;
10901
table->s->all_set= table->def_read_set;
10902
bitmap_set_all(&table->s->all_set);
10903
table->default_column_bitmaps();
10908
Create a temp table according to a field list.
10910
Given field pointers are changed to point at tmp_table for
10911
send_fields. The table object is self contained: it's
10912
allocated in its own memory root, as well as Field objects
10913
created for table columns.
10914
This function will replace Item_sum items in 'fields' list with
10915
corresponding Item_field items, pointing at the fields in the
10916
temporary table, unless this was prohibited by true
10917
value of argument save_sum_fields. The Item_field objects
10918
are created in THD memory root.
10920
@param thd thread handle
10921
@param param a description used as input to create the table
10922
@param fields list of items that will be used to define
10923
column types of the table (also see NOTES)
10924
@param group TODO document
10925
@param distinct should table rows be distinct
10926
@param save_sum_fields see NOTES
10927
@param select_options
10929
@param table_alias possible name of the temporary table that can
10930
be used for name resolving; can be "".
10933
#define STRING_TOTAL_LENGTH_TO_PACK_ROWS 128
10934
#define AVG_STRING_LENGTH_TO_PACK_ROWS 64
10935
#define RATIO_TO_PACK_ROWS 2
10936
#define MIN_STRING_LENGTH_TO_PACK_ROWS 10
10939
create_tmp_table(THD *thd,TMP_TABLE_PARAM *param,List<Item> &fields,
10940
ORDER *group, bool distinct, bool save_sum_fields,
10941
uint64_t select_options, ha_rows rows_limit,
10944
MEM_ROOT *mem_root_save, own_root;
10946
TABLE_SHARE *share;
10947
uint i,field_count,null_count,null_pack_length;
10948
uint copy_func_count= param->func_count;
10949
uint hidden_null_count, hidden_null_pack_length, hidden_field_count;
10950
uint blob_count,group_null_items, string_count;
10951
uint temp_pool_slot=MY_BIT_NONE;
10953
ulong reclength, string_total_length;
10954
bool using_unique_constraint= 0;
10955
bool use_packed_rows= 0;
10956
bool not_all_columns= !(select_options & TMP_TABLE_ALL_COLUMNS);
10957
char *tmpname,path[FN_REFLEN];
10958
uchar *pos, *group_buff, *bitmaps;
10960
Field **reg_field, **from_field, **default_field;
10962
Copy_field *copy=0;
10964
KEY_PART_INFO *key_part_info;
10966
MI_COLUMNDEF *recinfo;
10967
uint total_uneven_bit_length= 0;
10968
bool force_copy_fields= param->force_copy_fields;
10970
status_var_increment(thd->status_var.created_tmp_tables);
10972
if (use_temp_pool && !(test_flags & TEST_KEEP_TMP_TABLES))
10973
temp_pool_slot = bitmap_lock_set_next(&temp_pool);
10975
if (temp_pool_slot != MY_BIT_NONE) // we got a slot
10976
sprintf(path, "%s_%lx_%i", tmp_file_prefix,
10977
current_pid, temp_pool_slot);
10980
/* if we run out of slots or we are not using tempool */
10981
sprintf(path,"%s%lx_%lx_%x", tmp_file_prefix,current_pid,
10982
thd->thread_id, thd->tmp_table++);
10986
No need to change table name to lower case as we are only creating
10987
MyISAM or HEAP tables here
10989
fn_format(path, path, mysql_tmpdir, "", MY_REPLACE_EXT|MY_UNPACK_FILENAME);
10994
if (!param->quick_group)
10995
group=0; // Can't use group key
10996
else for (ORDER *tmp=group ; tmp ; tmp=tmp->next)
10999
marker == 4 means two things:
11000
- store NULLs in the key, and
11001
- convert BIT fields to 64-bit long, needed because MEMORY tables
11002
can't index BIT fields.
11004
(*tmp->item)->marker= 4;
11005
if ((*tmp->item)->max_length >= CONVERT_IF_BIGGER_TO_BLOB)
11006
using_unique_constraint=1;
11008
if (param->group_length >= MAX_BLOB_WIDTH)
11009
using_unique_constraint=1;
11011
distinct=0; // Can't use distinct
11014
field_count=param->field_count+param->func_count+param->sum_func_count;
11015
hidden_field_count=param->hidden_field_count;
11018
When loose index scan is employed as access method, it already
11019
computes all groups and the result of all aggregate functions. We
11020
make space for the items of the aggregate function in the list of
11021
functions TMP_TABLE_PARAM::items_to_copy, so that the values of
11022
these items are stored in the temporary table.
11024
if (param->precomputed_group_by)
11025
copy_func_count+= param->sum_func_count;
11027
init_sql_alloc(&own_root, TABLE_ALLOC_BLOCK_SIZE, 0);
11029
if (!multi_alloc_root(&own_root,
11030
&table, sizeof(*table),
11031
&share, sizeof(*share),
11032
®_field, sizeof(Field*) * (field_count+1),
11033
&default_field, sizeof(Field*) * (field_count),
11034
&blob_field, sizeof(uint)*(field_count+1),
11035
&from_field, sizeof(Field*)*field_count,
11036
©_func, sizeof(*copy_func)*(copy_func_count+1),
11037
¶m->keyinfo, sizeof(*param->keyinfo),
11039
sizeof(*key_part_info)*(param->group_parts+1),
11040
¶m->start_recinfo,
11041
sizeof(*param->recinfo)*(field_count*2+4),
11042
&tmpname, (uint) strlen(path)+1,
11043
&group_buff, (group && ! using_unique_constraint ?
11044
param->group_length : 0),
11045
&bitmaps, bitmap_buffer_size(field_count)*2,
11048
if (temp_pool_slot != MY_BIT_NONE)
11049
bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
11050
return(NULL); /* purecov: inspected */
11052
/* Copy_field belongs to TMP_TABLE_PARAM, allocate it in THD mem_root */
11053
if (!(param->copy_field= copy= new (thd->mem_root) Copy_field[field_count]))
11055
if (temp_pool_slot != MY_BIT_NONE)
11056
bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
11057
free_root(&own_root, MYF(0)); /* purecov: inspected */
11058
return(NULL); /* purecov: inspected */
11060
param->items_to_copy= copy_func;
11061
stpcpy(tmpname,path);
11062
/* make table according to fields */
11064
memset(table, 0, sizeof(*table));
11065
memset(reg_field, 0, sizeof(Field*)*(field_count+1));
11066
memset(default_field, 0, sizeof(Field*) * (field_count));
11067
memset(from_field, 0, sizeof(Field*)*field_count);
11069
table->mem_root= own_root;
11070
mem_root_save= thd->mem_root;
11071
thd->mem_root= &table->mem_root;
11073
table->field=reg_field;
11074
table->alias= table_alias;
11075
table->reginfo.lock_type=TL_WRITE; /* Will be updated */
11076
table->db_stat=HA_OPEN_KEYFILE+HA_OPEN_RNDFILE;
11078
table->temp_pool_slot = temp_pool_slot;
11079
table->copy_blobs= 1;
11080
table->in_use= thd;
11081
table->quick_keys.init();
11082
table->covering_keys.init();
11083
table->keys_in_use_for_query.init();
11086
init_tmp_table_share(thd, share, "", 0, tmpname, tmpname);
11087
share->blob_field= blob_field;
11088
share->blob_ptr_size= portable_sizeof_char_ptr;
11089
share->db_low_byte_first=1; // True for HEAP and MyISAM
11090
share->table_charset= param->table_charset;
11091
share->primary_key= MAX_KEY; // Indicate no primary key
11092
share->keys_for_keyread.init();
11093
share->keys_in_use.init();
11095
/* Calculate which type of fields we will store in the temporary table */
11097
reclength= string_total_length= 0;
11098
blob_count= string_count= null_count= hidden_null_count= group_null_items= 0;
11099
param->using_indirect_summary_function=0;
11101
List_iterator_fast<Item> li(fields);
11103
Field **tmp_from_field=from_field;
11104
while ((item=li++))
11106
Item::Type type=item->type();
11107
if (not_all_columns)
11109
if (item->with_sum_func && type != Item::SUM_FUNC_ITEM)
11111
if (item->used_tables() & OUTER_REF_TABLE_BIT)
11112
item->update_used_tables();
11113
if (type == Item::SUBSELECT_ITEM ||
11114
(item->used_tables() & ~OUTER_REF_TABLE_BIT))
11117
Mark that the we have ignored an item that refers to a summary
11118
function. We need to know this if someone is going to use
11119
DISTINCT on the result.
11121
param->using_indirect_summary_function=1;
11125
if (item->const_item() && (int) hidden_field_count <= 0)
11126
continue; // We don't have to store this
11128
if (type == Item::SUM_FUNC_ITEM && !group && !save_sum_fields)
11129
{ /* Can't calc group yet */
11130
((Item_sum*) item)->result_field=0;
11131
for (i=0 ; i < ((Item_sum*) item)->arg_count ; i++)
11133
Item **argp= ((Item_sum*) item)->args + i;
11135
if (!arg->const_item())
11138
create_tmp_field(thd, table, arg, arg->type(), ©_func,
11139
tmp_from_field, &default_field[fieldnr],
11140
group != 0,not_all_columns,
11142
param->convert_blob_length);
11144
goto err; // Should be OOM
11146
reclength+=new_field->pack_length();
11147
if (new_field->flags & BLOB_FLAG)
11149
*blob_field++= fieldnr;
11152
*(reg_field++)= new_field;
11153
if (new_field->real_type() == DRIZZLE_TYPE_VARCHAR)
11156
string_total_length+= new_field->pack_length();
11158
thd->mem_root= mem_root_save;
11159
thd->change_item_tree(argp, new Item_field(new_field));
11160
thd->mem_root= &table->mem_root;
11161
if (!(new_field->flags & NOT_NULL_FLAG))
11165
new_field->maybe_null() is still false, it will be
11166
changed below. But we have to setup Item_field correctly
11168
(*argp)->maybe_null=1;
11170
new_field->field_index= fieldnr++;
11177
The last parameter to create_tmp_field() is a bit tricky:
11179
We need to set it to 0 in union, to get fill_record() to modify the
11181
We need to set it to 1 on multi-table-update and in select to
11182
write rows to the temporary table.
11183
We here distinguish between UNION and multi-table-updates by the fact
11184
that in the later case group is set to the row pointer.
11186
Field *new_field= (param->schema_table) ?
11187
create_tmp_field_for_schema(thd, item, table) :
11188
create_tmp_field(thd, table, item, type, ©_func,
11189
tmp_from_field, &default_field[fieldnr],
11191
!force_copy_fields &&
11192
(not_all_columns || group !=0),
11194
If item->marker == 4 then we force create_tmp_field
11195
to create a 64-bit longs for BIT fields because HEAP
11196
tables can't index BIT fields directly. We do the same
11197
for distinct, as we want the distinct index to be
11198
usable in this case too.
11200
item->marker == 4 || param->bit_fields_as_long,
11202
param->convert_blob_length);
11206
if (thd->is_fatal_error)
11207
goto err; // Got OOM
11208
continue; // Some kindf of const item
11210
if (type == Item::SUM_FUNC_ITEM)
11211
((Item_sum *) item)->result_field= new_field;
11213
reclength+=new_field->pack_length();
11214
if (!(new_field->flags & NOT_NULL_FLAG))
11216
if (new_field->flags & BLOB_FLAG)
11218
*blob_field++= fieldnr;
11221
if (item->marker == 4 && item->maybe_null)
11223
group_null_items++;
11224
new_field->flags|= GROUP_FLAG;
11226
new_field->field_index= fieldnr++;
11227
*(reg_field++)= new_field;
11229
if (!--hidden_field_count)
11232
This was the last hidden field; Remember how many hidden fields could
11235
hidden_null_count=null_count;
11237
We need to update hidden_field_count as we may have stored group
11238
functions with constant arguments
11240
param->hidden_field_count= fieldnr;
11244
assert(fieldnr == (uint) (reg_field - table->field));
11245
assert(field_count >= (uint) (reg_field - table->field));
11246
field_count= fieldnr;
11248
*blob_field= 0; // End marker
11249
share->fields= field_count;
11251
/* If result table is small; use a heap */
11252
/* future: storage engine selection can be made dynamic? */
11253
if (blob_count || using_unique_constraint ||
11254
(select_options & (OPTION_BIG_TABLES | SELECT_SMALL_RESULT)) ==
11255
OPTION_BIG_TABLES || (select_options & TMP_TABLE_FORCE_MYISAM))
11257
share->db_plugin= ha_lock_engine(0, myisam_hton);
11258
table->file= get_new_handler(share, &table->mem_root,
11261
(param->group_parts > table->file->max_key_parts() ||
11262
param->group_length > table->file->max_key_length()))
11263
using_unique_constraint=1;
11267
share->db_plugin= ha_lock_engine(0, heap_hton);
11268
table->file= get_new_handler(share, &table->mem_root,
11275
if (!using_unique_constraint)
11276
reclength+= group_null_items; // null flag is stored separately
11278
share->blob_fields= blob_count;
11279
if (blob_count == 0)
11281
/* We need to ensure that first byte is not 0 for the delete link */
11282
if (param->hidden_field_count)
11283
hidden_null_count++;
11287
hidden_null_pack_length=(hidden_null_count+7)/8;
11288
null_pack_length= (hidden_null_pack_length +
11289
(null_count + total_uneven_bit_length + 7) / 8);
11290
reclength+=null_pack_length;
11292
reclength=1; // Dummy select
11293
/* Use packed rows if there is blobs or a lot of space to gain */
11294
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)))
11295
use_packed_rows= 1;
11297
share->reclength= reclength;
11299
uint alloc_length=ALIGN_SIZE(reclength+MI_UNIQUE_HASH_LENGTH+1);
11300
share->rec_buff_length= alloc_length;
11301
if (!(table->record[0]= (uchar*)
11302
alloc_root(&table->mem_root, alloc_length*3)))
11304
table->record[1]= table->record[0]+alloc_length;
11305
share->default_values= table->record[1]+alloc_length;
11307
copy_func[0]=0; // End marker
11308
param->func_count= copy_func - param->items_to_copy;
11310
setup_tmp_table_column_bitmaps(table, bitmaps);
11312
recinfo=param->start_recinfo;
11313
null_flags=(uchar*) table->record[0];
11314
pos=table->record[0]+ null_pack_length;
11315
if (null_pack_length)
11317
memset(recinfo, 0, sizeof(*recinfo));
11318
recinfo->type=FIELD_NORMAL;
11319
recinfo->length=null_pack_length;
11321
memset(null_flags, 255, null_pack_length); // Set null fields
11323
table->null_flags= (uchar*) table->record[0];
11324
share->null_fields= null_count+ hidden_null_count;
11325
share->null_bytes= null_pack_length;
11327
null_count= (blob_count == 0) ? 1 : 0;
11328
hidden_field_count=param->hidden_field_count;
11329
for (i=0,reg_field=table->field; i < field_count; i++,reg_field++,recinfo++)
11331
Field *field= *reg_field;
11333
memset(recinfo, 0, sizeof(*recinfo));
11335
if (!(field->flags & NOT_NULL_FLAG))
11337
if (field->flags & GROUP_FLAG && !using_unique_constraint)
11340
We have to reserve one byte here for NULL bits,
11341
as this is updated by 'end_update()'
11343
*pos++=0; // Null is stored here
11345
recinfo->type=FIELD_NORMAL;
11347
memset(recinfo, 0, sizeof(*recinfo));
11351
recinfo->null_bit= 1 << (null_count & 7);
11352
recinfo->null_pos= null_count/8;
11354
field->move_field(pos,null_flags+null_count/8,
11355
1 << (null_count & 7));
11359
field->move_field(pos,(uchar*) 0,0);
11363
Test if there is a default field value. The test for ->ptr is to skip
11364
'offset' fields generated by initalize_tables
11366
if (default_field[i] && default_field[i]->ptr)
11369
default_field[i] is set only in the cases when 'field' can
11370
inherit the default value that is defined for the field referred
11371
by the Item_field object from which 'field' has been created.
11374
Field *orig_field= default_field[i];
11375
/* Get the value from default_values */
11376
diff= (my_ptrdiff_t) (orig_field->table->s->default_values-
11377
orig_field->table->record[0]);
11378
orig_field->move_field_offset(diff); // Points now at default_values
11379
if (orig_field->is_real_null())
11383
field->set_notnull();
11384
memcpy(field->ptr, orig_field->ptr, field->pack_length());
11386
orig_field->move_field_offset(-diff); // Back to record[0]
11390
{ /* Not a table Item */
11391
copy->set(field,from_field[i],save_sum_fields);
11394
length=field->pack_length();
11397
/* Make entry for create table */
11398
recinfo->length=length;
11399
if (field->flags & BLOB_FLAG)
11400
recinfo->type= (int) FIELD_BLOB;
11402
recinfo->type=FIELD_NORMAL;
11403
if (!--hidden_field_count)
11404
null_count=(null_count+7) & ~7; // move to next byte
11406
// fix table name in field entry
11407
field->table_name= &table->alias;
11410
param->copy_field_end=copy;
11411
param->recinfo=recinfo;
11412
store_record(table,s->default_values); // Make empty default record
11414
if (thd->variables.tmp_table_size == ~ (uint64_t) 0) // No limit
11415
share->max_rows= ~(ha_rows) 0;
11417
share->max_rows= (ha_rows) (((share->db_type() == heap_hton) ?
11418
min(thd->variables.tmp_table_size,
11419
thd->variables.max_heap_table_size) :
11420
thd->variables.tmp_table_size) /
11422
set_if_bigger(share->max_rows,1); // For dummy start options
11424
Push the LIMIT clause to the temporary table creation, so that we
11425
materialize only up to 'rows_limit' records instead of all result records.
11427
set_if_smaller(share->max_rows, rows_limit);
11428
param->end_write_records= rows_limit;
11430
keyinfo= param->keyinfo;
11434
table->group=group; /* Table is grouped by key */
11435
param->group_buff=group_buff;
11437
share->uniques= test(using_unique_constraint);
11438
table->key_info=keyinfo;
11439
keyinfo->key_part=key_part_info;
11440
keyinfo->flags=HA_NOSAME;
11441
keyinfo->usable_key_parts=keyinfo->key_parts= param->group_parts;
11442
keyinfo->key_length=0;
11443
keyinfo->rec_per_key=0;
11444
keyinfo->algorithm= HA_KEY_ALG_UNDEF;
11445
keyinfo->name= (char*) "group_key";
11446
ORDER *cur_group= group;
11447
for (; cur_group ; cur_group= cur_group->next, key_part_info++)
11449
Field *field=(*cur_group->item)->get_tmp_table_field();
11450
bool maybe_null=(*cur_group->item)->maybe_null;
11451
key_part_info->null_bit=0;
11452
key_part_info->field= field;
11453
key_part_info->offset= field->offset(table->record[0]);
11454
key_part_info->length= (uint16_t) field->key_length();
11455
key_part_info->type= (uint8_t) field->key_type();
11456
key_part_info->key_type =
11457
((ha_base_keytype) key_part_info->type == HA_KEYTYPE_TEXT ||
11458
(ha_base_keytype) key_part_info->type == HA_KEYTYPE_VARTEXT1 ||
11459
(ha_base_keytype) key_part_info->type == HA_KEYTYPE_VARTEXT2) ?
11460
0 : FIELDFLAG_BINARY;
11461
if (!using_unique_constraint)
11463
cur_group->buff=(char*) group_buff;
11464
if (!(cur_group->field= field->new_key_field(thd->mem_root,table,
11469
goto err; /* purecov: inspected */
11473
To be able to group on NULL, we reserved place in group_buff
11474
for the NULL flag just before the column. (see above).
11475
The field data is after this flag.
11476
The NULL flag is updated in 'end_update()' and 'end_write()'
11478
keyinfo->flags|= HA_NULL_ARE_EQUAL; // def. that NULL == NULL
11479
key_part_info->null_bit=field->null_bit;
11480
key_part_info->null_offset= (uint) (field->null_ptr -
11481
(uchar*) table->record[0]);
11482
cur_group->buff++; // Pointer to field data
11483
group_buff++; // Skipp null flag
11485
/* In GROUP BY 'a' and 'a ' are equal for VARCHAR fields */
11486
key_part_info->key_part_flag|= HA_END_SPACE_ARE_EQUAL;
11487
group_buff+= cur_group->field->pack_length();
11489
keyinfo->key_length+= key_part_info->length;
11493
if (distinct && field_count != param->hidden_field_count)
11496
Create an unique key or an unique constraint over all columns
11497
that should be in the result. In the temporary table, there are
11498
'param->hidden_field_count' extra columns, whose null bits are stored
11499
in the first 'hidden_null_pack_length' bytes of the row.
11504
Special mode for index creation in MyISAM used to support unique
11505
indexes on blobs with arbitrary length. Such indexes cannot be
11510
null_pack_length-=hidden_null_pack_length;
11511
keyinfo->key_parts= ((field_count-param->hidden_field_count)+
11512
(share->uniques ? test(null_pack_length) : 0));
11513
table->distinct= 1;
11515
if (!(key_part_info= (KEY_PART_INFO*)
11516
alloc_root(&table->mem_root,
11517
keyinfo->key_parts * sizeof(KEY_PART_INFO))))
11519
memset(key_part_info, 0, keyinfo->key_parts * sizeof(KEY_PART_INFO));
11520
table->key_info=keyinfo;
11521
keyinfo->key_part=key_part_info;
11522
keyinfo->flags=HA_NOSAME | HA_NULL_ARE_EQUAL;
11523
keyinfo->key_length=(uint16_t) reclength;
11524
keyinfo->name= (char*) "distinct_key";
11525
keyinfo->algorithm= HA_KEY_ALG_UNDEF;
11526
keyinfo->rec_per_key=0;
11529
Create an extra field to hold NULL bits so that unique indexes on
11530
blobs can distinguish NULL from 0. This extra field is not needed
11531
when we do not use UNIQUE indexes for blobs.
11533
if (null_pack_length && share->uniques)
11535
key_part_info->null_bit=0;
11536
key_part_info->offset=hidden_null_pack_length;
11537
key_part_info->length=null_pack_length;
11538
key_part_info->field= new Field_varstring(table->record[0],
11539
(uint32_t) key_part_info->length,
11547
if (!key_part_info->field)
11549
key_part_info->field->init(table);
11550
key_part_info->key_type=FIELDFLAG_BINARY;
11551
key_part_info->type= HA_KEYTYPE_BINARY;
11554
/* Create a distinct key over the columns we are going to return */
11555
for (i=param->hidden_field_count, reg_field=table->field + i ;
11557
i++, reg_field++, key_part_info++)
11559
key_part_info->null_bit=0;
11560
key_part_info->field= *reg_field;
11561
key_part_info->offset= (*reg_field)->offset(table->record[0]);
11562
key_part_info->length= (uint16_t) (*reg_field)->pack_length();
11564
The below method of computing the key format length of the
11565
key part is a copy/paste from opt_range.cc, and table.cc.
11566
This should be factored out, e.g. as a method of Field.
11567
In addition it is not clear if any of the Field::*_length
11568
methods is supposed to compute the same length. If so, it
11571
key_part_info->store_length= key_part_info->length;
11573
if ((*reg_field)->real_maybe_null())
11574
key_part_info->store_length+= HA_KEY_NULL_LENGTH;
11575
if ((*reg_field)->type() == DRIZZLE_TYPE_BLOB ||
11576
(*reg_field)->real_type() == DRIZZLE_TYPE_VARCHAR)
11577
key_part_info->store_length+= HA_KEY_BLOB_LENGTH;
11579
key_part_info->type= (uint8_t) (*reg_field)->key_type();
11580
key_part_info->key_type =
11581
((ha_base_keytype) key_part_info->type == HA_KEYTYPE_TEXT ||
11582
(ha_base_keytype) key_part_info->type == HA_KEYTYPE_VARTEXT1 ||
11583
(ha_base_keytype) key_part_info->type == HA_KEYTYPE_VARTEXT2) ?
11584
0 : FIELDFLAG_BINARY;
11588
if (thd->is_fatal_error) // If end of memory
11589
goto err; /* purecov: inspected */
11590
share->db_record_offset= 1;
11591
if (share->db_type() == myisam_hton)
11593
if (create_myisam_tmp_table(table, param->keyinfo, param->start_recinfo,
11594
¶m->recinfo, select_options))
11597
if (open_tmp_table(table))
11600
thd->mem_root= mem_root_save;
11605
thd->mem_root= mem_root_save;
11606
free_tmp_table(thd,table); /* purecov: inspected */
11607
if (temp_pool_slot != MY_BIT_NONE)
11608
bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
11609
return(NULL); /* purecov: inspected */
11616
Create a temporary table to weed out duplicate rowid combinations
11620
create_duplicate_weedout_tmp_table()
11622
uniq_tuple_length_arg
11626
Create a temporary table to weed out duplicate rowid combinations. The
11627
table has a single column that is a concatenation of all rowids in the
11630
Depending on the needed length, there are two cases:
11632
1. When the length of the column < max_key_length:
11634
CREATE TABLE tmp (col VARBINARY(n) NOT NULL, UNIQUE KEY(col));
11636
2. Otherwise (not a valid SQL syntax but internally supported):
11638
CREATE TABLE tmp (col VARBINARY NOT NULL, UNIQUE CONSTRAINT(col));
11640
The code in this function was produced by extraction of relevant parts
11641
from create_tmp_table().
11648
TABLE *create_duplicate_weedout_tmp_table(THD *thd,
11649
uint uniq_tuple_length_arg,
11650
SJ_TMP_TABLE *sjtbl)
11652
MEM_ROOT *mem_root_save, own_root;
11654
TABLE_SHARE *share;
11655
uint temp_pool_slot=MY_BIT_NONE;
11656
char *tmpname,path[FN_REFLEN];
11658
KEY_PART_INFO *key_part_info;
11663
MI_COLUMNDEF *recinfo, *start_recinfo;
11664
bool using_unique_constraint=false;
11665
Field *field, *key_field;
11666
uint blob_count, null_pack_length, null_count;
11671
STEP 1: Get temporary table name
11673
statistic_increment(thd->status_var.created_tmp_tables, &LOCK_status);
11674
if (use_temp_pool && !(test_flags & TEST_KEEP_TMP_TABLES))
11675
temp_pool_slot = bitmap_lock_set_next(&temp_pool);
11677
if (temp_pool_slot != MY_BIT_NONE) // we got a slot
11678
sprintf(path, "%s_%lx_%i", tmp_file_prefix,
11679
current_pid, temp_pool_slot);
11682
/* if we run out of slots or we are not using tempool */
11683
sprintf(path,"%s%lx_%lx_%x", tmp_file_prefix,current_pid,
11684
thd->thread_id, thd->tmp_table++);
11686
fn_format(path, path, mysql_tmpdir, "", MY_REPLACE_EXT|MY_UNPACK_FILENAME);
11688
/* STEP 2: Figure if we'll be using a key or blob+constraint */
11689
if (uniq_tuple_length_arg >= CONVERT_IF_BIGGER_TO_BLOB)
11690
using_unique_constraint= true;
11692
/* STEP 3: Allocate memory for temptable description */
11693
init_sql_alloc(&own_root, TABLE_ALLOC_BLOCK_SIZE, 0);
11694
if (!multi_alloc_root(&own_root,
11695
&table, sizeof(*table),
11696
&share, sizeof(*share),
11697
®_field, sizeof(Field*) * (1+1),
11698
&blob_field, sizeof(uint)*2,
11699
&keyinfo, sizeof(*keyinfo),
11700
&key_part_info, sizeof(*key_part_info) * 2,
11702
sizeof(*recinfo)*(1*2+4),
11703
&tmpname, (uint) strlen(path)+1,
11704
&group_buff, (!using_unique_constraint ?
11705
uniq_tuple_length_arg : 0),
11706
&bitmaps, bitmap_buffer_size(1)*2,
11709
if (temp_pool_slot != MY_BIT_NONE)
11710
bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
11713
stpcpy(tmpname,path);
11716
/* STEP 4: Create TABLE description */
11717
memset(table, 0, sizeof(*table));
11718
memset(reg_field, 0, sizeof(Field*)*2);
11720
table->mem_root= own_root;
11721
mem_root_save= thd->mem_root;
11722
thd->mem_root= &table->mem_root;
11724
table->field=reg_field;
11725
table->alias= "weedout-tmp";
11726
table->reginfo.lock_type=TL_WRITE; /* Will be updated */
11727
table->db_stat=HA_OPEN_KEYFILE+HA_OPEN_RNDFILE;
11729
table->temp_pool_slot = temp_pool_slot;
11730
table->copy_blobs= 1;
11731
table->in_use= thd;
11732
table->quick_keys.init();
11733
table->covering_keys.init();
11734
table->keys_in_use_for_query.init();
11737
init_tmp_table_share(thd, share, "", 0, tmpname, tmpname);
11738
share->blob_field= blob_field;
11739
share->blob_ptr_size= portable_sizeof_char_ptr;
11740
share->db_low_byte_first=1; // True for HEAP and MyISAM
11741
share->table_charset= NULL;
11742
share->primary_key= MAX_KEY; // Indicate no primary key
11743
share->keys_for_keyread.init();
11744
share->keys_in_use.init();
11748
/* Create the field */
11751
For the sake of uniformity, always use Field_varstring.
11753
field= new Field_varstring(uniq_tuple_length_arg, false, "rowids", share,
11757
field->table= table;
11758
field->key_start.init(0);
11759
field->part_of_key.init(0);
11760
field->part_of_sortkey.init(0);
11761
field->unireg_check= Field::NONE;
11762
field->flags= (NOT_NULL_FLAG | BINARY_FLAG | NO_DEFAULT_VALUE_FLAG);
11763
field->reset_fields();
11764
field->init(table);
11765
field->orig_table= NULL;
11767
field->field_index= 0;
11769
*(reg_field++)= field;
11774
share->blob_fields= 0;
11777
uint reclength= field->pack_length();
11778
if (using_unique_constraint)
11780
share->db_plugin= ha_lock_engine(0, myisam_hton);
11781
table->file= get_new_handler(share, &table->mem_root,
11783
assert(uniq_tuple_length_arg <= table->file->max_key_length());
11787
share->db_plugin= ha_lock_engine(0, heap_hton);
11788
table->file= get_new_handler(share, &table->mem_root,
11796
null_pack_length= 1;
11797
reclength += null_pack_length;
11799
share->reclength= reclength;
11801
uint alloc_length=ALIGN_SIZE(share->reclength + MI_UNIQUE_HASH_LENGTH+1);
11802
share->rec_buff_length= alloc_length;
11803
if (!(table->record[0]= (uchar*)
11804
alloc_root(&table->mem_root, alloc_length*3)))
11806
table->record[1]= table->record[0]+alloc_length;
11807
share->default_values= table->record[1]+alloc_length;
11809
setup_tmp_table_column_bitmaps(table, bitmaps);
11811
recinfo= start_recinfo;
11812
null_flags=(uchar*) table->record[0];
11813
pos=table->record[0]+ null_pack_length;
11814
if (null_pack_length)
11816
memset(recinfo, 0, sizeof(*recinfo));
11817
recinfo->type=FIELD_NORMAL;
11818
recinfo->length=null_pack_length;
11820
memset(null_flags, 255, null_pack_length); // Set null fields
11822
table->null_flags= (uchar*) table->record[0];
11823
share->null_fields= null_count;
11824
share->null_bytes= null_pack_length;
11829
//Field *field= *reg_field;
11831
memset(recinfo, 0, sizeof(*recinfo));
11832
field->move_field(pos,(uchar*) 0,0);
11836
Test if there is a default field value. The test for ->ptr is to skip
11837
'offset' fields generated by initalize_tables
11839
// Initialize the table field:
11840
memset(field->ptr, 0, field->pack_length());
11842
length=field->pack_length();
11845
/* Make entry for create table */
11846
recinfo->length=length;
11847
if (field->flags & BLOB_FLAG)
11848
recinfo->type= (int) FIELD_BLOB;
11850
recinfo->type=FIELD_NORMAL;
11852
field->table_name= &table->alias;
11855
//param->recinfo=recinfo;
11856
//store_record(table,s->default_values); // Make empty default record
11858
if (thd->variables.tmp_table_size == ~ (uint64_t) 0) // No limit
11859
share->max_rows= ~(ha_rows) 0;
11861
share->max_rows= (ha_rows) (((share->db_type() == heap_hton) ?
11862
min(thd->variables.tmp_table_size,
11863
thd->variables.max_heap_table_size) :
11864
thd->variables.tmp_table_size) /
11866
set_if_bigger(share->max_rows,1); // For dummy start options
11869
//// keyinfo= param->keyinfo;
11873
share->uniques= test(using_unique_constraint);
11874
table->key_info=keyinfo;
11875
keyinfo->key_part=key_part_info;
11876
keyinfo->flags=HA_NOSAME;
11877
keyinfo->usable_key_parts= keyinfo->key_parts= 1;
11878
keyinfo->key_length=0;
11879
keyinfo->rec_per_key=0;
11880
keyinfo->algorithm= HA_KEY_ALG_UNDEF;
11881
keyinfo->name= (char*) "weedout_key";
11883
key_part_info->null_bit=0;
11884
key_part_info->field= field;
11885
key_part_info->offset= field->offset(table->record[0]);
11886
key_part_info->length= (uint16_t) field->key_length();
11887
key_part_info->type= (uint8_t) field->key_type();
11888
key_part_info->key_type = FIELDFLAG_BINARY;
11889
if (!using_unique_constraint)
11891
if (!(key_field= field->new_key_field(thd->mem_root, table,
11896
key_part_info->key_part_flag|= HA_END_SPACE_ARE_EQUAL; //todo need this?
11898
keyinfo->key_length+= key_part_info->length;
11902
if (thd->is_fatal_error) // If end of memory
11904
share->db_record_offset= 1;
11905
if (share->db_type() == myisam_hton)
11908
if (create_myisam_tmp_table(table, keyinfo, start_recinfo, &recinfo, 0))
11911
sjtbl->start_recinfo= start_recinfo;
11912
sjtbl->recinfo= recinfo;
11913
if (open_tmp_table(table))
11916
thd->mem_root= mem_root_save;
11920
thd->mem_root= mem_root_save;
11921
free_tmp_table(thd,table); /* purecov: inspected */
11922
if (temp_pool_slot != MY_BIT_NONE)
11923
bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
11924
return(NULL); /* purecov: inspected */
11927
/****************************************************************************/
11930
Create a reduced TABLE object with properly set up Field list from a
11931
list of field definitions.
11933
The created table doesn't have a table handler associated with
11934
it, has no keys, no group/distinct, no copy_funcs array.
11935
The sole purpose of this TABLE object is to use the power of Field
11936
class to read/write data to/from table->record[0]. Then one can store
11937
the record in any container (RB tree, hash, etc).
11938
The table is created in THD mem_root, so are the table's fields.
11939
Consequently, if you don't BLOB fields, you don't need to free it.
11941
@param thd connection handle
11942
@param field_list list of column definitions
11945
0 if out of memory, TABLE object in case of success
11948
TABLE *create_virtual_tmp_table(THD *thd, List<Create_field> &field_list)
11950
uint field_count= field_list.elements;
11951
uint blob_count= 0;
11953
Create_field *cdef; /* column definition */
11954
uint record_length= 0;
11955
uint null_count= 0; /* number of columns which may be null */
11956
uint null_pack_length; /* NULL representation array length */
11960
TABLE_SHARE *share;
11962
if (!multi_alloc_root(thd->mem_root,
11963
&table, sizeof(*table),
11964
&share, sizeof(*share),
11965
&field, (field_count + 1) * sizeof(Field*),
11966
&blob_field, (field_count+1) *sizeof(uint),
11967
&bitmaps, bitmap_buffer_size(field_count)*2,
11971
memset(table, 0, sizeof(*table));
11972
memset(share, 0, sizeof(*share));
11973
table->field= field;
11975
share->blob_field= blob_field;
11976
share->fields= field_count;
11977
share->blob_ptr_size= portable_sizeof_char_ptr;
11978
setup_tmp_table_column_bitmaps(table, bitmaps);
11980
/* Create all fields and calculate the total length of record */
11981
List_iterator_fast<Create_field> it(field_list);
11982
while ((cdef= it++))
11984
*field= make_field(share, 0, cdef->length,
11985
(uchar*) (f_maybe_null(cdef->pack_flag) ? "" : 0),
11986
f_maybe_null(cdef->pack_flag) ? 1 : 0,
11987
cdef->pack_flag, cdef->sql_type, cdef->charset,
11988
cdef->unireg_check,
11989
cdef->interval, cdef->field_name);
11992
(*field)->init(table);
11993
record_length+= (*field)->pack_length();
11994
if (! ((*field)->flags & NOT_NULL_FLAG))
11997
if ((*field)->flags & BLOB_FLAG)
11998
share->blob_field[blob_count++]= (uint) (field - table->field);
12002
*field= NULL; /* mark the end of the list */
12003
share->blob_field[blob_count]= 0; /* mark the end of the list */
12004
share->blob_fields= blob_count;
12006
null_pack_length= (null_count + 7)/8;
12007
share->reclength= record_length + null_pack_length;
12008
share->rec_buff_length= ALIGN_SIZE(share->reclength + 1);
12009
table->record[0]= (uchar*) thd->alloc(share->rec_buff_length);
12010
if (!table->record[0])
12013
if (null_pack_length)
12015
table->null_flags= (uchar*) table->record[0];
12016
share->null_fields= null_count;
12017
share->null_bytes= null_pack_length;
12020
table->in_use= thd; /* field->reset() may access table->in_use */
12022
/* Set up field pointers */
12023
uchar *null_pos= table->record[0];
12024
uchar *field_pos= null_pos + share->null_bytes;
12027
for (field= table->field; *field; ++field)
12029
Field *cur_field= *field;
12030
if ((cur_field->flags & NOT_NULL_FLAG))
12031
cur_field->move_field(field_pos);
12034
cur_field->move_field(field_pos, (uchar*) null_pos, null_bit);
12036
if (null_bit == (1 << 8))
12042
cur_field->reset();
12044
field_pos+= cur_field->pack_length();
12049
for (field= table->field; *field; ++field)
12050
delete *field; /* just invokes field destructor */
12055
static bool open_tmp_table(TABLE *table)
12058
if ((error=table->file->ha_open(table, table->s->table_name.str,O_RDWR,
12059
HA_OPEN_TMP_TABLE | HA_OPEN_INTERNAL_TABLE)))
12061
table->file->print_error(error,MYF(0)); /* purecov: inspected */
12065
(void) table->file->extra(HA_EXTRA_QUICK); /* Faster */
12071
Create MyISAM temporary table
12074
create_myisam_tmp_table()
12075
table Table object that descrimes the table to be created
12076
keyinfo Description of the index (there is always one index)
12077
start_recinfo MyISAM's column descriptions
12078
recinfo INOUT End of MyISAM's column descriptions
12079
options Option bits
12082
Create a MyISAM temporary table according to passed description. The is
12083
assumed to have one unique index or constraint.
12085
The passed array or MI_COLUMNDEF structures must have this form:
12087
1. 1-byte column (afaiu for 'deleted' flag) (note maybe not 1-byte
12088
when there are many nullable columns)
12090
3. One free MI_COLUMNDEF element (*recinfo points here)
12092
This function may use the free element to create hash column for unique
12100
static bool create_myisam_tmp_table(TABLE *table, KEY *keyinfo,
12101
MI_COLUMNDEF *start_recinfo,
12102
MI_COLUMNDEF **recinfo,
12107
MI_UNIQUEDEF uniquedef;
12108
TABLE_SHARE *share= table->s;
12111
{ // Get keys for ni_create
12112
bool using_unique_constraint=0;
12113
HA_KEYSEG *seg= (HA_KEYSEG*) alloc_root(&table->mem_root,
12114
sizeof(*seg) * keyinfo->key_parts);
12118
memset(seg, 0, sizeof(*seg) * keyinfo->key_parts);
12119
if (keyinfo->key_length >= table->file->max_key_length() ||
12120
keyinfo->key_parts > table->file->max_key_parts() ||
12123
/* Can't create a key; Make a unique constraint instead of a key */
12126
using_unique_constraint=1;
12127
memset(&uniquedef, 0, sizeof(uniquedef));
12128
uniquedef.keysegs=keyinfo->key_parts;
12130
uniquedef.null_are_equal=1;
12132
/* Create extra column for hash value */
12133
memset(*recinfo, 0, sizeof(**recinfo));
12134
(*recinfo)->type= FIELD_CHECK;
12135
(*recinfo)->length=MI_UNIQUE_HASH_LENGTH;
12137
share->reclength+=MI_UNIQUE_HASH_LENGTH;
12141
/* Create an unique key */
12142
memset(&keydef, 0, sizeof(keydef));
12143
keydef.flag=HA_NOSAME | HA_BINARY_PACK_KEY | HA_PACK_KEY;
12144
keydef.keysegs= keyinfo->key_parts;
12147
for (uint i=0; i < keyinfo->key_parts ; i++,seg++)
12149
Field *field=keyinfo->key_part[i].field;
12151
seg->language= field->charset()->number;
12152
seg->length= keyinfo->key_part[i].length;
12153
seg->start= keyinfo->key_part[i].offset;
12154
if (field->flags & BLOB_FLAG)
12157
((keyinfo->key_part[i].key_type & FIELDFLAG_BINARY) ?
12158
HA_KEYTYPE_VARBINARY2 : HA_KEYTYPE_VARTEXT2);
12159
seg->bit_start= (uint8_t)(field->pack_length() - share->blob_ptr_size);
12160
seg->flag= HA_BLOB_PART;
12161
seg->length=0; // Whole blob in unique constraint
12165
seg->type= keyinfo->key_part[i].type;
12167
if (!(field->flags & NOT_NULL_FLAG))
12169
seg->null_bit= field->null_bit;
12170
seg->null_pos= (uint) (field->null_ptr - (uchar*) table->record[0]);
12172
We are using a GROUP BY on something that contains NULL
12173
In this case we have to tell MyISAM that two NULL should
12174
on INSERT be regarded at the same value
12176
if (!using_unique_constraint)
12177
keydef.flag|= HA_NULL_ARE_EQUAL;
12181
MI_CREATE_INFO create_info;
12182
memset(&create_info, 0, sizeof(create_info));
12184
if ((options & (OPTION_BIG_TABLES | SELECT_SMALL_RESULT)) ==
12186
create_info.data_file_length= ~(uint64_t) 0;
12188
if ((error=mi_create(share->table_name.str, share->keys, &keydef,
12189
(uint) (*recinfo-start_recinfo),
12191
share->uniques, &uniquedef,
12193
HA_CREATE_TMP_TABLE)))
12195
table->file->print_error(error,MYF(0)); /* purecov: inspected */
12199
status_var_increment(table->in_use->status_var.created_tmp_disk_tables);
12200
share->db_record_offset= 1;
12208
free_tmp_table(THD *thd, TABLE *entry)
12210
MEM_ROOT own_root= entry->mem_root;
12211
const char *save_proc_info;
12213
save_proc_info=thd->proc_info;
12214
thd_proc_info(thd, "removing tmp table");
12218
if (entry->db_stat)
12219
entry->file->ha_drop_table(entry->s->table_name.str);
12221
entry->file->ha_delete_table(entry->s->table_name.str);
12222
delete entry->file;
12226
for (Field **ptr=entry->field ; *ptr ; ptr++)
12228
free_io_cache(entry);
12230
if (entry->temp_pool_slot != MY_BIT_NONE)
12231
bitmap_lock_clear_bit(&temp_pool, entry->temp_pool_slot);
12233
plugin_unlock(0, entry->s->db_plugin);
12235
free_root(&own_root, MYF(0)); /* the table is allocated in its own root */
12236
thd_proc_info(thd, save_proc_info);
12242
If a HEAP table gets full, create a MyISAM table and copy all rows
12246
bool create_myisam_from_heap(THD *thd, TABLE *table,
12247
MI_COLUMNDEF *start_recinfo,
12248
MI_COLUMNDEF **recinfo,
12249
int error, bool ignore_last_dupp_key_error)
12253
const char *save_proc_info;
12256
if (table->s->db_type() != heap_hton ||
12257
error != HA_ERR_RECORD_FILE_FULL)
12259
table->file->print_error(error,MYF(0));
12264
new_table.s= &share;
12265
new_table.s->db_plugin= ha_lock_engine(thd, myisam_hton);
12266
if (!(new_table.file= get_new_handler(&share, &new_table.mem_root,
12267
new_table.s->db_type())))
12268
return(1); // End of memory
12270
save_proc_info=thd->proc_info;
12271
thd_proc_info(thd, "converting HEAP to MyISAM");
12273
if (create_myisam_tmp_table(&new_table, table->key_info, start_recinfo,
12274
recinfo, thd->lex->select_lex.options |
12277
if (open_tmp_table(&new_table))
12279
if (table->file->indexes_are_disabled())
12280
new_table.file->ha_disable_indexes(HA_KEY_SWITCH_ALL);
12281
table->file->ha_index_or_rnd_end();
12282
table->file->ha_rnd_init(1);
12283
if (table->no_rows)
12285
new_table.file->extra(HA_EXTRA_NO_ROWS);
12286
new_table.no_rows=1;
12289
#ifdef TO_BE_DONE_LATER_IN_4_1
12291
To use start_bulk_insert() (which is new in 4.1) we need to find
12292
all places where a corresponding end_bulk_insert() should be put.
12294
table->file->info(HA_STATUS_VARIABLE); /* update table->file->stats.records */
12295
new_table.file->ha_start_bulk_insert(table->file->stats.records);
12297
/* HA_EXTRA_WRITE_CACHE can stay until close, no need to disable it */
12298
new_table.file->extra(HA_EXTRA_WRITE_CACHE);
12302
copy all old rows from heap table to MyISAM table
12303
This is the only code that uses record[1] to read/write but this
12304
is safe as this is a temporary MyISAM table without timestamp/autoincrement.
12306
while (!table->file->rnd_next(new_table.record[1]))
12308
write_err= new_table.file->ha_write_row(new_table.record[1]);
12312
/* copy row that filled HEAP table */
12313
if ((write_err=new_table.file->ha_write_row(table->record[0])))
12315
if (new_table.file->is_fatal_error(write_err, HA_CHECK_DUP) ||
12316
!ignore_last_dupp_key_error)
12320
/* remove heap table and change to use myisam table */
12321
(void) table->file->ha_rnd_end();
12322
(void) table->file->close(); // This deletes the table !
12323
delete table->file;
12325
plugin_unlock(0, table->s->db_plugin);
12326
share.db_plugin= my_plugin_lock(0, &share.db_plugin);
12327
new_table.s= table->s; // Keep old share
12331
table->file->change_table_ptr(table, table->s);
12332
table->use_all_columns();
12333
if (save_proc_info)
12335
const char *new_proc_info=
12336
(!strcmp(save_proc_info,"Copying to tmp table") ?
12337
"Copying to tmp table on disk" : save_proc_info);
12338
thd_proc_info(thd, new_proc_info);
12343
table->file->print_error(write_err, MYF(0));
12344
(void) table->file->ha_rnd_end();
12345
(void) new_table.file->close();
12347
new_table.file->ha_delete_table(new_table.s->table_name.str);
12349
delete new_table.file;
12350
thd_proc_info(thd, save_proc_info);
12351
table->mem_root= new_table.mem_root;