3573
3593
return(FRMTYPE_TABLE); // Is probably a .frm table
3596
/****************************************************************************
3597
Functions for creating temporary tables.
3598
****************************************************************************/
3602
void free_tmp_table(THD *thd, TABLE *entry);
3605
Create field for temporary table from given field.
3607
@param thd Thread handler
3608
@param org_field field from which new field will be created
3609
@param name New field name
3610
@param table Temporary table
3611
@param item !=NULL if item->result_field should point to new field.
3612
This is relevant for how fill_record() is going to work:
3613
If item != NULL then fill_record() will update
3614
the record in the original table.
3615
If item == NULL then fill_record() will update
3617
@param convert_blob_length If >0 create a varstring(convert_blob_length)
3618
field instead of blob.
3626
Field *create_tmp_field_from_field(THD *thd, Field *org_field,
3627
const char *name, TABLE *table,
3628
Item_field *item, uint convert_blob_length)
3633
Make sure that the blob fits into a Field_varstring which has
3636
if (convert_blob_length && convert_blob_length <= Field_varstring::MAX_SIZE &&
3637
(org_field->flags & BLOB_FLAG))
3638
new_field= new Field_varstring(convert_blob_length,
3639
org_field->maybe_null(),
3640
org_field->field_name, table->s,
3641
org_field->charset());
3643
new_field= org_field->new_field(thd->mem_root, table,
3644
table == org_field->table);
3647
new_field->init(table);
3648
new_field->orig_table= org_field->orig_table;
3650
item->result_field= new_field;
3652
new_field->field_name= name;
3653
new_field->flags|= (org_field->flags & NO_DEFAULT_VALUE_FLAG);
3654
if (org_field->maybe_null() || (item && item->maybe_null))
3655
new_field->flags&= ~NOT_NULL_FLAG; // Because of outer join
3656
if (org_field->type() == DRIZZLE_TYPE_VARCHAR)
3657
table->s->db_create_options|= HA_OPTION_PACK_RECORD;
3658
else if (org_field->type() == DRIZZLE_TYPE_DOUBLE)
3659
((Field_double *) new_field)->not_fixed= true;
3665
Create field for temporary table using type of given item.
3667
@param thd Thread handler
3668
@param item Item to create a field for
3669
@param table Temporary table
3670
@param copy_func If set and item is a function, store copy of
3672
@param modify_item 1 if item->result_field should point to new
3673
item. This is relevent for how fill_record()
3675
If modify_item is 1 then fill_record() will
3676
update the record in the original table.
3677
If modify_item is 0 then fill_record() will
3678
update the temporary table
3679
@param convert_blob_length If >0 create a varstring(convert_blob_length)
3680
field instead of blob.
3688
static Field *create_tmp_field_from_item(THD *thd __attribute__((unused)),
3689
Item *item, TABLE *table,
3690
Item ***copy_func, bool modify_item,
3691
uint convert_blob_length)
3693
bool maybe_null= item->maybe_null;
3696
switch (item->result_type()) {
3698
new_field= new Field_double(item->max_length, maybe_null,
3699
item->name, item->decimals, true);
3703
Select an integer type with the minimal fit precision.
3704
MY_INT32_NUM_DECIMAL_DIGITS is sign inclusive, don't consider the sign.
3705
Values with MY_INT32_NUM_DECIMAL_DIGITS digits may or may not fit into
3706
Field_long : make them Field_int64_t.
3708
if (item->max_length >= (MY_INT32_NUM_DECIMAL_DIGITS - 1))
3709
new_field=new Field_int64_t(item->max_length, maybe_null,
3710
item->name, item->unsigned_flag);
3712
new_field=new Field_long(item->max_length, maybe_null,
3713
item->name, item->unsigned_flag);
3716
assert(item->collation.collation);
3718
enum enum_field_types type;
3720
DATE/TIME fields have STRING_RESULT result type.
3721
To preserve type they needed to be handled separately.
3723
if ((type= item->field_type()) == DRIZZLE_TYPE_DATETIME ||
3724
type == DRIZZLE_TYPE_TIME || type == DRIZZLE_TYPE_NEWDATE ||
3725
type == DRIZZLE_TYPE_TIMESTAMP)
3726
new_field= item->tmp_table_field_from_field_type(table, 1);
3728
Make sure that the blob fits into a Field_varstring which has
3731
else if (item->max_length/item->collation.collation->mbmaxlen > 255 &&
3732
convert_blob_length <= Field_varstring::MAX_SIZE &&
3733
convert_blob_length)
3734
new_field= new Field_varstring(convert_blob_length, maybe_null,
3735
item->name, table->s,
3736
item->collation.collation);
3738
new_field= item->make_string_field(table);
3739
new_field->set_derivation(item->collation.derivation);
3741
case DECIMAL_RESULT:
3743
uint8_t dec= item->decimals;
3744
uint8_t intg= ((Item_decimal *) item)->decimal_precision() - dec;
3745
uint32_t len= item->max_length;
3748
Trying to put too many digits overall in a DECIMAL(prec,dec)
3749
will always throw a warning. We must limit dec to
3750
DECIMAL_MAX_SCALE however to prevent an assert() later.
3755
signed int overflow;
3757
dec= min(dec, (uint8_t)DECIMAL_MAX_SCALE);
3760
If the value still overflows the field with the corrected dec,
3761
we'll throw out decimals rather than integers. This is still
3762
bad and of course throws a truncation warning.
3763
+1: for decimal point
3766
overflow= my_decimal_precision_to_length(intg + dec, dec,
3767
item->unsigned_flag) - len;
3770
dec= max(0, dec - overflow); // too long, discard fract
3772
len -= item->decimals - dec; // corrected value fits
3775
new_field= new Field_new_decimal(len, maybe_null, item->name,
3776
dec, item->unsigned_flag);
3781
// This case should never be choosen
3787
new_field->init(table);
3789
if (copy_func && item->is_result_field())
3790
*((*copy_func)++) = item; // Save for copy_funcs
3792
item->set_result_field(new_field);
3793
if (item->type() == Item::NULL_ITEM)
3794
new_field->is_created_from_null_item= true;
3800
Create field for information schema table.
3802
@param thd Thread handler
3803
@param table Temporary table
3804
@param item Item to create a field for
3812
Field *create_tmp_field_for_schema(THD *thd __attribute__((unused)),
3813
Item *item, TABLE *table)
3815
if (item->field_type() == DRIZZLE_TYPE_VARCHAR)
3818
if (item->max_length > MAX_FIELD_VARCHARLENGTH)
3819
field= new Field_blob(item->max_length, item->maybe_null,
3820
item->name, item->collation.collation);
3822
field= new Field_varstring(item->max_length, item->maybe_null,
3824
table->s, item->collation.collation);
3829
return item->tmp_table_field_from_field_type(table, 0);
3834
Create field for temporary table.
3836
@param thd Thread handler
3837
@param table Temporary table
3838
@param item Item to create a field for
3839
@param type Type of item (normally item->type)
3840
@param copy_func If set and item is a function, store copy of item
3842
@param from_field if field will be created using other field as example,
3843
pointer example field will be written here
3844
@param default_field If field has a default value field, store it here
3845
@param group 1 if we are going to do a relative group by on result
3846
@param modify_item 1 if item->result_field should point to new item.
3847
This is relevent for how fill_record() is going to
3849
If modify_item is 1 then fill_record() will update
3850
the record in the original table.
3851
If modify_item is 0 then fill_record() will update
3853
@param convert_blob_length If >0 create a varstring(convert_blob_length)
3854
field instead of blob.
3862
Field *create_tmp_field(THD *thd, TABLE *table,Item *item, Item::Type type,
3863
Item ***copy_func, Field **from_field,
3864
Field **default_field,
3865
bool group, bool modify_item,
3866
bool table_cant_handle_bit_fields __attribute__((unused)),
3867
bool make_copy_field,
3868
uint convert_blob_length)
3871
Item::Type orig_type= type;
3874
if (type != Item::FIELD_ITEM &&
3875
item->real_item()->type() == Item::FIELD_ITEM)
3878
item= item->real_item();
3879
type= Item::FIELD_ITEM;
3883
case Item::SUM_FUNC_ITEM:
3885
Item_sum *item_sum=(Item_sum*) item;
3886
result= item_sum->create_tmp_field(group, table, convert_blob_length);
3888
my_error(ER_OUT_OF_RESOURCES, MYF(ME_FATALERROR));
3891
case Item::FIELD_ITEM:
3892
case Item::DEFAULT_VALUE_ITEM:
3894
Item_field *field= (Item_field*) item;
3895
bool orig_modify= modify_item;
3896
if (orig_type == Item::REF_ITEM)
3899
If item have to be able to store NULLs but underlaid field can't do it,
3900
create_tmp_field_from_field() can't be used for tmp field creation.
3902
if (field->maybe_null && !field->field->maybe_null())
3904
result= create_tmp_field_from_item(thd, item, table, NULL,
3905
modify_item, convert_blob_length);
3906
*from_field= field->field;
3907
if (result && modify_item)
3908
field->result_field= result;
3911
result= create_tmp_field_from_field(thd, (*from_field= field->field),
3912
orig_item ? orig_item->name :
3915
modify_item ? field :
3917
convert_blob_length);
3918
if (orig_type == Item::REF_ITEM && orig_modify)
3919
((Item_ref*)orig_item)->set_result_field(result);
3920
if (field->field->eq_def(result))
3921
*default_field= field->field;
3925
case Item::FUNC_ITEM:
3927
case Item::COND_ITEM:
3928
case Item::FIELD_AVG_ITEM:
3929
case Item::FIELD_STD_ITEM:
3930
case Item::SUBSELECT_ITEM:
3931
/* The following can only happen with 'CREATE TABLE ... SELECT' */
3932
case Item::PROC_ITEM:
3933
case Item::INT_ITEM:
3934
case Item::REAL_ITEM:
3935
case Item::DECIMAL_ITEM:
3936
case Item::STRING_ITEM:
3937
case Item::REF_ITEM:
3938
case Item::NULL_ITEM:
3939
case Item::VARBIN_ITEM:
3940
if (make_copy_field)
3942
assert(((Item_result_field*)item)->result_field);
3943
*from_field= ((Item_result_field*)item)->result_field;
3945
return create_tmp_field_from_item(thd, item, table,
3946
(make_copy_field ? 0 : copy_func),
3947
modify_item, convert_blob_length);
3948
case Item::TYPE_HOLDER:
3949
result= ((Item_type_holder *)item)->make_field_by_type(table);
3950
result->set_derivation(item->collation.derivation);
3952
default: // Dosen't have to be stored
3958
Create a temp table according to a field list.
3960
Given field pointers are changed to point at tmp_table for
3961
send_fields. The table object is self contained: it's
3962
allocated in its own memory root, as well as Field objects
3963
created for table columns.
3964
This function will replace Item_sum items in 'fields' list with
3965
corresponding Item_field items, pointing at the fields in the
3966
temporary table, unless this was prohibited by true
3967
value of argument save_sum_fields. The Item_field objects
3968
are created in THD memory root.
3970
@param thd thread handle
3971
@param param a description used as input to create the table
3972
@param fields list of items that will be used to define
3973
column types of the table (also see NOTES)
3974
@param group TODO document
3975
@param distinct should table rows be distinct
3976
@param save_sum_fields see NOTES
3977
@param select_options
3979
@param table_alias possible name of the temporary table that can
3980
be used for name resolving; can be "".
3983
#define STRING_TOTAL_LENGTH_TO_PACK_ROWS 128
3984
#define AVG_STRING_LENGTH_TO_PACK_ROWS 64
3985
#define RATIO_TO_PACK_ROWS 2
3986
#define MIN_STRING_LENGTH_TO_PACK_ROWS 10
3989
create_tmp_table(THD *thd,TMP_TABLE_PARAM *param,List<Item> &fields,
3990
ORDER *group, bool distinct, bool save_sum_fields,
3991
uint64_t select_options, ha_rows rows_limit,
3994
MEM_ROOT *mem_root_save, own_root;
3997
uint i,field_count,null_count,null_pack_length;
3998
uint copy_func_count= param->func_count;
3999
uint hidden_null_count, hidden_null_pack_length, hidden_field_count;
4000
uint blob_count,group_null_items, string_count;
4001
uint temp_pool_slot=MY_BIT_NONE;
4003
ulong reclength, string_total_length;
4004
bool using_unique_constraint= 0;
4005
bool use_packed_rows= 0;
4006
bool not_all_columns= !(select_options & TMP_TABLE_ALL_COLUMNS);
4007
char *tmpname,path[FN_REFLEN];
4008
uchar *pos, *group_buff, *bitmaps;
4010
Field **reg_field, **from_field, **default_field;
4014
KEY_PART_INFO *key_part_info;
4016
MI_COLUMNDEF *recinfo;
4017
uint total_uneven_bit_length= 0;
4018
bool force_copy_fields= param->force_copy_fields;
4020
status_var_increment(thd->status_var.created_tmp_tables);
4022
if (use_temp_pool && !(test_flags & TEST_KEEP_TMP_TABLES))
4023
temp_pool_slot = bitmap_lock_set_next(&temp_pool);
4025
if (temp_pool_slot != MY_BIT_NONE) // we got a slot
4026
sprintf(path, "%s_%lx_%i", tmp_file_prefix,
4027
current_pid, temp_pool_slot);
4030
/* if we run out of slots or we are not using tempool */
4031
sprintf(path,"%s%lx_%lx_%x", tmp_file_prefix,current_pid,
4032
thd->thread_id, thd->tmp_table++);
4036
No need to change table name to lower case as we are only creating
4037
MyISAM or HEAP tables here
4039
fn_format(path, path, mysql_tmpdir, "", MY_REPLACE_EXT|MY_UNPACK_FILENAME);
4044
if (!param->quick_group)
4045
group=0; // Can't use group key
4046
else for (ORDER *tmp=group ; tmp ; tmp=tmp->next)
4049
marker == 4 means two things:
4050
- store NULLs in the key, and
4051
- convert BIT fields to 64-bit long, needed because MEMORY tables
4052
can't index BIT fields.
4054
(*tmp->item)->marker= 4;
4055
if ((*tmp->item)->max_length >= CONVERT_IF_BIGGER_TO_BLOB)
4056
using_unique_constraint=1;
4058
if (param->group_length >= MAX_BLOB_WIDTH)
4059
using_unique_constraint=1;
4061
distinct=0; // Can't use distinct
4064
field_count=param->field_count+param->func_count+param->sum_func_count;
4065
hidden_field_count=param->hidden_field_count;
4068
When loose index scan is employed as access method, it already
4069
computes all groups and the result of all aggregate functions. We
4070
make space for the items of the aggregate function in the list of
4071
functions TMP_TABLE_PARAM::items_to_copy, so that the values of
4072
these items are stored in the temporary table.
4074
if (param->precomputed_group_by)
4075
copy_func_count+= param->sum_func_count;
4077
init_sql_alloc(&own_root, TABLE_ALLOC_BLOCK_SIZE, 0);
4079
if (!multi_alloc_root(&own_root,
4080
&table, sizeof(*table),
4081
&share, sizeof(*share),
4082
®_field, sizeof(Field*) * (field_count+1),
4083
&default_field, sizeof(Field*) * (field_count),
4084
&blob_field, sizeof(uint)*(field_count+1),
4085
&from_field, sizeof(Field*)*field_count,
4086
©_func, sizeof(*copy_func)*(copy_func_count+1),
4087
¶m->keyinfo, sizeof(*param->keyinfo),
4089
sizeof(*key_part_info)*(param->group_parts+1),
4090
¶m->start_recinfo,
4091
sizeof(*param->recinfo)*(field_count*2+4),
4092
&tmpname, (uint) strlen(path)+1,
4093
&group_buff, (group && ! using_unique_constraint ?
4094
param->group_length : 0),
4095
&bitmaps, bitmap_buffer_size(field_count)*2,
4098
if (temp_pool_slot != MY_BIT_NONE)
4099
bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
4100
return(NULL); /* purecov: inspected */
4102
/* Copy_field belongs to TMP_TABLE_PARAM, allocate it in THD mem_root */
4103
if (!(param->copy_field= copy= new (thd->mem_root) Copy_field[field_count]))
4105
if (temp_pool_slot != MY_BIT_NONE)
4106
bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
4107
free_root(&own_root, MYF(0)); /* purecov: inspected */
4108
return(NULL); /* purecov: inspected */
4110
param->items_to_copy= copy_func;
4111
stpcpy(tmpname,path);
4112
/* make table according to fields */
4114
memset(table, 0, sizeof(*table));
4115
memset(reg_field, 0, sizeof(Field*)*(field_count+1));
4116
memset(default_field, 0, sizeof(Field*) * (field_count));
4117
memset(from_field, 0, sizeof(Field*)*field_count);
4119
table->mem_root= own_root;
4120
mem_root_save= thd->mem_root;
4121
thd->mem_root= &table->mem_root;
4123
table->field=reg_field;
4124
table->alias= table_alias;
4125
table->reginfo.lock_type=TL_WRITE; /* Will be updated */
4126
table->db_stat=HA_OPEN_KEYFILE+HA_OPEN_RNDFILE;
4128
table->temp_pool_slot = temp_pool_slot;
4129
table->copy_blobs= 1;
4131
table->quick_keys.init();
4132
table->covering_keys.init();
4133
table->keys_in_use_for_query.init();
4135
table->setShare(share);
4136
init_tmp_table_share(thd, share, "", 0, tmpname, tmpname);
4137
share->blob_field= blob_field;
4138
share->blob_ptr_size= portable_sizeof_char_ptr;
4139
share->db_low_byte_first=1; // True for HEAP and MyISAM
4140
share->table_charset= param->table_charset;
4141
share->primary_key= MAX_KEY; // Indicate no primary key
4142
share->keys_for_keyread.init();
4143
share->keys_in_use.init();
4145
/* Calculate which type of fields we will store in the temporary table */
4147
reclength= string_total_length= 0;
4148
blob_count= string_count= null_count= hidden_null_count= group_null_items= 0;
4149
param->using_indirect_summary_function=0;
4151
List_iterator_fast<Item> li(fields);
4153
Field **tmp_from_field=from_field;
4156
Item::Type type=item->type();
4157
if (not_all_columns)
4159
if (item->with_sum_func && type != Item::SUM_FUNC_ITEM)
4161
if (item->used_tables() & OUTER_REF_TABLE_BIT)
4162
item->update_used_tables();
4163
if (type == Item::SUBSELECT_ITEM ||
4164
(item->used_tables() & ~OUTER_REF_TABLE_BIT))
4167
Mark that the we have ignored an item that refers to a summary
4168
function. We need to know this if someone is going to use
4169
DISTINCT on the result.
4171
param->using_indirect_summary_function=1;
4175
if (item->const_item() && (int) hidden_field_count <= 0)
4176
continue; // We don't have to store this
4178
if (type == Item::SUM_FUNC_ITEM && !group && !save_sum_fields)
4179
{ /* Can't calc group yet */
4180
((Item_sum*) item)->result_field=0;
4181
for (i=0 ; i < ((Item_sum*) item)->arg_count ; i++)
4183
Item **argp= ((Item_sum*) item)->args + i;
4185
if (!arg->const_item())
4188
create_tmp_field(thd, table, arg, arg->type(), ©_func,
4189
tmp_from_field, &default_field[fieldnr],
4190
group != 0,not_all_columns,
4192
param->convert_blob_length);
4194
goto err; // Should be OOM
4196
reclength+=new_field->pack_length();
4197
if (new_field->flags & BLOB_FLAG)
4199
*blob_field++= fieldnr;
4202
*(reg_field++)= new_field;
4203
if (new_field->real_type() == DRIZZLE_TYPE_VARCHAR)
4206
string_total_length+= new_field->pack_length();
4208
thd->mem_root= mem_root_save;
4209
thd->change_item_tree(argp, new Item_field(new_field));
4210
thd->mem_root= &table->mem_root;
4211
if (!(new_field->flags & NOT_NULL_FLAG))
4215
new_field->maybe_null() is still false, it will be
4216
changed below. But we have to setup Item_field correctly
4218
(*argp)->maybe_null=1;
4220
new_field->field_index= fieldnr++;
4227
The last parameter to create_tmp_field() is a bit tricky:
4229
We need to set it to 0 in union, to get fill_record() to modify the
4231
We need to set it to 1 on multi-table-update and in select to
4232
write rows to the temporary table.
4233
We here distinguish between UNION and multi-table-updates by the fact
4234
that in the later case group is set to the row pointer.
4236
Field *new_field= (param->schema_table) ?
4237
create_tmp_field_for_schema(thd, item, table) :
4238
create_tmp_field(thd, table, item, type, ©_func,
4239
tmp_from_field, &default_field[fieldnr],
4241
!force_copy_fields &&
4242
(not_all_columns || group !=0),
4244
If item->marker == 4 then we force create_tmp_field
4245
to create a 64-bit longs for BIT fields because HEAP
4246
tables can't index BIT fields directly. We do the same
4247
for distinct, as we want the distinct index to be
4248
usable in this case too.
4250
item->marker == 4 || param->bit_fields_as_long,
4252
param->convert_blob_length);
4256
if (thd->is_fatal_error)
4257
goto err; // Got OOM
4258
continue; // Some kindf of const item
4260
if (type == Item::SUM_FUNC_ITEM)
4261
((Item_sum *) item)->result_field= new_field;
4263
reclength+=new_field->pack_length();
4264
if (!(new_field->flags & NOT_NULL_FLAG))
4266
if (new_field->flags & BLOB_FLAG)
4268
*blob_field++= fieldnr;
4271
if (item->marker == 4 && item->maybe_null)
4274
new_field->flags|= GROUP_FLAG;
4276
new_field->field_index= fieldnr++;
4277
*(reg_field++)= new_field;
4279
if (!--hidden_field_count)
4282
This was the last hidden field; Remember how many hidden fields could
4285
hidden_null_count=null_count;
4287
We need to update hidden_field_count as we may have stored group
4288
functions with constant arguments
4290
param->hidden_field_count= fieldnr;
4294
assert(fieldnr == (uint) (reg_field - table->field));
4295
assert(field_count >= (uint) (reg_field - table->field));
4296
field_count= fieldnr;
4298
*blob_field= 0; // End marker
4299
share->fields= field_count;
4301
/* If result table is small; use a heap */
4302
/* future: storage engine selection can be made dynamic? */
4303
if (blob_count || using_unique_constraint ||
4304
(select_options & (OPTION_BIG_TABLES | SELECT_SMALL_RESULT)) ==
4305
OPTION_BIG_TABLES || (select_options & TMP_TABLE_FORCE_MYISAM))
4307
share->db_plugin= ha_lock_engine(0, myisam_hton);
4308
table->file= get_new_handler(share, &table->mem_root,
4311
(param->group_parts > table->file->max_key_parts() ||
4312
param->group_length > table->file->max_key_length()))
4313
using_unique_constraint=1;
4317
share->db_plugin= ha_lock_engine(0, heap_hton);
4318
table->file= get_new_handler(share, &table->mem_root,
4325
if (!using_unique_constraint)
4326
reclength+= group_null_items; // null flag is stored separately
4328
share->blob_fields= blob_count;
4329
if (blob_count == 0)
4331
/* We need to ensure that first byte is not 0 for the delete link */
4332
if (param->hidden_field_count)
4333
hidden_null_count++;
4337
hidden_null_pack_length=(hidden_null_count+7)/8;
4338
null_pack_length= (hidden_null_pack_length +
4339
(null_count + total_uneven_bit_length + 7) / 8);
4340
reclength+=null_pack_length;
4342
reclength=1; // Dummy select
4343
/* Use packed rows if there is blobs or a lot of space to gain */
4344
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)))
4347
share->reclength= reclength;
4349
uint alloc_length=ALIGN_SIZE(reclength+MI_UNIQUE_HASH_LENGTH+1);
4350
share->rec_buff_length= alloc_length;
4351
if (!(table->record[0]= (uchar*)
4352
alloc_root(&table->mem_root, alloc_length*3)))
4354
table->record[1]= table->record[0]+alloc_length;
4355
share->default_values= table->record[1]+alloc_length;
4357
copy_func[0]=0; // End marker
4358
param->func_count= copy_func - param->items_to_copy;
4360
table->setup_tmp_table_column_bitmaps(bitmaps);
4362
recinfo=param->start_recinfo;
4363
null_flags=(uchar*) table->record[0];
4364
pos=table->record[0]+ null_pack_length;
4365
if (null_pack_length)
4367
memset(recinfo, 0, sizeof(*recinfo));
4368
recinfo->type=FIELD_NORMAL;
4369
recinfo->length=null_pack_length;
4371
memset(null_flags, 255, null_pack_length); // Set null fields
4373
table->null_flags= (uchar*) table->record[0];
4374
share->null_fields= null_count+ hidden_null_count;
4375
share->null_bytes= null_pack_length;
4377
null_count= (blob_count == 0) ? 1 : 0;
4378
hidden_field_count=param->hidden_field_count;
4379
for (i=0,reg_field=table->field; i < field_count; i++,reg_field++,recinfo++)
4381
Field *field= *reg_field;
4383
memset(recinfo, 0, sizeof(*recinfo));
4385
if (!(field->flags & NOT_NULL_FLAG))
4387
if (field->flags & GROUP_FLAG && !using_unique_constraint)
4390
We have to reserve one byte here for NULL bits,
4391
as this is updated by 'end_update()'
4393
*pos++=0; // Null is stored here
4395
recinfo->type=FIELD_NORMAL;
4397
memset(recinfo, 0, sizeof(*recinfo));
4401
recinfo->null_bit= 1 << (null_count & 7);
4402
recinfo->null_pos= null_count/8;
4404
field->move_field(pos,null_flags+null_count/8,
4405
1 << (null_count & 7));
4409
field->move_field(pos,(uchar*) 0,0);
4413
Test if there is a default field value. The test for ->ptr is to skip
4414
'offset' fields generated by initalize_tables
4416
if (default_field[i] && default_field[i]->ptr)
4419
default_field[i] is set only in the cases when 'field' can
4420
inherit the default value that is defined for the field referred
4421
by the Item_field object from which 'field' has been created.
4424
Field *orig_field= default_field[i];
4425
/* Get the value from default_values */
4426
diff= (my_ptrdiff_t) (orig_field->table->s->default_values-
4427
orig_field->table->record[0]);
4428
orig_field->move_field_offset(diff); // Points now at default_values
4429
if (orig_field->is_real_null())
4433
field->set_notnull();
4434
memcpy(field->ptr, orig_field->ptr, field->pack_length());
4436
orig_field->move_field_offset(-diff); // Back to record[0]
4440
{ /* Not a table Item */
4441
copy->set(field,from_field[i],save_sum_fields);
4444
length=field->pack_length();
4447
/* Make entry for create table */
4448
recinfo->length=length;
4449
if (field->flags & BLOB_FLAG)
4450
recinfo->type= (int) FIELD_BLOB;
4452
recinfo->type=FIELD_NORMAL;
4453
if (!--hidden_field_count)
4454
null_count=(null_count+7) & ~7; // move to next byte
4456
// fix table name in field entry
4457
field->table_name= &table->alias;
4460
param->copy_field_end=copy;
4461
param->recinfo=recinfo;
4462
store_record(table,s->default_values); // Make empty default record
4464
if (thd->variables.tmp_table_size == ~ (uint64_t) 0) // No limit
4465
share->max_rows= ~(ha_rows) 0;
4467
share->max_rows= (ha_rows) (((share->db_type() == heap_hton) ?
4468
min(thd->variables.tmp_table_size,
4469
thd->variables.max_heap_table_size) :
4470
thd->variables.tmp_table_size) /
4472
set_if_bigger(share->max_rows,1); // For dummy start options
4474
Push the LIMIT clause to the temporary table creation, so that we
4475
materialize only up to 'rows_limit' records instead of all result records.
4477
set_if_smaller(share->max_rows, rows_limit);
4478
param->end_write_records= rows_limit;
4480
keyinfo= param->keyinfo;
4484
table->group=group; /* Table is grouped by key */
4485
param->group_buff=group_buff;
4487
share->uniques= test(using_unique_constraint);
4488
table->key_info=keyinfo;
4489
keyinfo->key_part=key_part_info;
4490
keyinfo->flags=HA_NOSAME;
4491
keyinfo->usable_key_parts=keyinfo->key_parts= param->group_parts;
4492
keyinfo->key_length=0;
4493
keyinfo->rec_per_key=0;
4494
keyinfo->algorithm= HA_KEY_ALG_UNDEF;
4495
keyinfo->name= (char*) "group_key";
4496
ORDER *cur_group= group;
4497
for (; cur_group ; cur_group= cur_group->next, key_part_info++)
4499
Field *field=(*cur_group->item)->get_tmp_table_field();
4500
bool maybe_null=(*cur_group->item)->maybe_null;
4501
key_part_info->null_bit=0;
4502
key_part_info->field= field;
4503
key_part_info->offset= field->offset(table->record[0]);
4504
key_part_info->length= (uint16_t) field->key_length();
4505
key_part_info->type= (uint8_t) field->key_type();
4506
key_part_info->key_type =
4507
((ha_base_keytype) key_part_info->type == HA_KEYTYPE_TEXT ||
4508
(ha_base_keytype) key_part_info->type == HA_KEYTYPE_VARTEXT1 ||
4509
(ha_base_keytype) key_part_info->type == HA_KEYTYPE_VARTEXT2) ?
4510
0 : FIELDFLAG_BINARY;
4511
if (!using_unique_constraint)
4513
cur_group->buff=(char*) group_buff;
4514
if (!(cur_group->field= field->new_key_field(thd->mem_root,table,
4519
goto err; /* purecov: inspected */
4523
To be able to group on NULL, we reserved place in group_buff
4524
for the NULL flag just before the column. (see above).
4525
The field data is after this flag.
4526
The NULL flag is updated in 'end_update()' and 'end_write()'
4528
keyinfo->flags|= HA_NULL_ARE_EQUAL; // def. that NULL == NULL
4529
key_part_info->null_bit=field->null_bit;
4530
key_part_info->null_offset= (uint) (field->null_ptr -
4531
(uchar*) table->record[0]);
4532
cur_group->buff++; // Pointer to field data
4533
group_buff++; // Skipp null flag
4535
/* In GROUP BY 'a' and 'a ' are equal for VARCHAR fields */
4536
key_part_info->key_part_flag|= HA_END_SPACE_ARE_EQUAL;
4537
group_buff+= cur_group->field->pack_length();
4539
keyinfo->key_length+= key_part_info->length;
4543
if (distinct && field_count != param->hidden_field_count)
4546
Create an unique key or an unique constraint over all columns
4547
that should be in the result. In the temporary table, there are
4548
'param->hidden_field_count' extra columns, whose null bits are stored
4549
in the first 'hidden_null_pack_length' bytes of the row.
4554
Special mode for index creation in MyISAM used to support unique
4555
indexes on blobs with arbitrary length. Such indexes cannot be
4560
null_pack_length-=hidden_null_pack_length;
4561
keyinfo->key_parts= ((field_count-param->hidden_field_count)+
4562
(share->uniques ? test(null_pack_length) : 0));
4565
if (!(key_part_info= (KEY_PART_INFO*)
4566
alloc_root(&table->mem_root,
4567
keyinfo->key_parts * sizeof(KEY_PART_INFO))))
4569
memset(key_part_info, 0, keyinfo->key_parts * sizeof(KEY_PART_INFO));
4570
table->key_info=keyinfo;
4571
keyinfo->key_part=key_part_info;
4572
keyinfo->flags=HA_NOSAME | HA_NULL_ARE_EQUAL;
4573
keyinfo->key_length=(uint16_t) reclength;
4574
keyinfo->name= (char*) "distinct_key";
4575
keyinfo->algorithm= HA_KEY_ALG_UNDEF;
4576
keyinfo->rec_per_key=0;
4579
Create an extra field to hold NULL bits so that unique indexes on
4580
blobs can distinguish NULL from 0. This extra field is not needed
4581
when we do not use UNIQUE indexes for blobs.
4583
if (null_pack_length && share->uniques)
4585
key_part_info->null_bit=0;
4586
key_part_info->offset=hidden_null_pack_length;
4587
key_part_info->length=null_pack_length;
4588
key_part_info->field= new Field_varstring(table->record[0],
4589
(uint32_t) key_part_info->length,
4597
if (!key_part_info->field)
4599
key_part_info->field->init(table);
4600
key_part_info->key_type=FIELDFLAG_BINARY;
4601
key_part_info->type= HA_KEYTYPE_BINARY;
4604
/* Create a distinct key over the columns we are going to return */
4605
for (i=param->hidden_field_count, reg_field=table->field + i ;
4607
i++, reg_field++, key_part_info++)
4609
key_part_info->null_bit=0;
4610
key_part_info->field= *reg_field;
4611
key_part_info->offset= (*reg_field)->offset(table->record[0]);
4612
key_part_info->length= (uint16_t) (*reg_field)->pack_length();
4614
The below method of computing the key format length of the
4615
key part is a copy/paste from opt_range.cc, and table.cc.
4616
This should be factored out, e.g. as a method of Field.
4617
In addition it is not clear if any of the Field::*_length
4618
methods is supposed to compute the same length. If so, it
4621
key_part_info->store_length= key_part_info->length;
4623
if ((*reg_field)->real_maybe_null())
4624
key_part_info->store_length+= HA_KEY_NULL_LENGTH;
4625
if ((*reg_field)->type() == DRIZZLE_TYPE_BLOB ||
4626
(*reg_field)->real_type() == DRIZZLE_TYPE_VARCHAR)
4627
key_part_info->store_length+= HA_KEY_BLOB_LENGTH;
4629
key_part_info->type= (uint8_t) (*reg_field)->key_type();
4630
key_part_info->key_type =
4631
((ha_base_keytype) key_part_info->type == HA_KEYTYPE_TEXT ||
4632
(ha_base_keytype) key_part_info->type == HA_KEYTYPE_VARTEXT1 ||
4633
(ha_base_keytype) key_part_info->type == HA_KEYTYPE_VARTEXT2) ?
4634
0 : FIELDFLAG_BINARY;
4638
if (thd->is_fatal_error) // If end of memory
4639
goto err; /* purecov: inspected */
4640
share->db_record_offset= 1;
4641
if (share->db_type() == myisam_hton)
4643
if (table->create_myisam_tmp_table(param->keyinfo, param->start_recinfo,
4644
¶m->recinfo, select_options))
4647
if (table->open_tmp_table())
4650
thd->mem_root= mem_root_save;
4655
thd->mem_root= mem_root_save;
4656
table->free_tmp_table(thd); /* purecov: inspected */
4657
if (temp_pool_slot != MY_BIT_NONE)
4658
bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
4659
return(NULL); /* purecov: inspected */
4662
/****************************************************************************/
4665
Create a reduced TABLE object with properly set up Field list from a
4666
list of field definitions.
4668
The created table doesn't have a table handler associated with
4669
it, has no keys, no group/distinct, no copy_funcs array.
4670
The sole purpose of this TABLE object is to use the power of Field
4671
class to read/write data to/from table->record[0]. Then one can store
4672
the record in any container (RB tree, hash, etc).
4673
The table is created in THD mem_root, so are the table's fields.
4674
Consequently, if you don't BLOB fields, you don't need to free it.
4676
@param thd connection handle
4677
@param field_list list of column definitions
4680
0 if out of memory, TABLE object in case of success
4683
TABLE *create_virtual_tmp_table(THD *thd, List<Create_field> &field_list)
4685
uint field_count= field_list.elements;
4688
Create_field *cdef; /* column definition */
4689
uint record_length= 0;
4690
uint null_count= 0; /* number of columns which may be null */
4691
uint null_pack_length; /* NULL representation array length */
4697
if (!multi_alloc_root(thd->mem_root,
4698
&table, sizeof(*table),
4699
&share, sizeof(*share),
4700
&field, (field_count + 1) * sizeof(Field*),
4701
&blob_field, (field_count+1) *sizeof(uint),
4702
&bitmaps, bitmap_buffer_size(field_count)*2,
4706
memset(table, 0, sizeof(*table));
4707
memset(share, 0, sizeof(*share));
4708
table->field= field;
4710
share->blob_field= blob_field;
4711
share->fields= field_count;
4712
share->blob_ptr_size= portable_sizeof_char_ptr;
4713
table->setup_tmp_table_column_bitmaps(bitmaps);
4715
/* Create all fields and calculate the total length of record */
4716
List_iterator_fast<Create_field> it(field_list);
4717
while ((cdef= it++))
4719
*field= make_field(share, 0, cdef->length,
4720
(uchar*) (f_maybe_null(cdef->pack_flag) ? "" : 0),
4721
f_maybe_null(cdef->pack_flag) ? 1 : 0,
4722
cdef->pack_flag, cdef->sql_type, cdef->charset,
4724
cdef->interval, cdef->field_name);
4727
(*field)->init(table);
4728
record_length+= (*field)->pack_length();
4729
if (! ((*field)->flags & NOT_NULL_FLAG))
4732
if ((*field)->flags & BLOB_FLAG)
4733
share->blob_field[blob_count++]= (uint) (field - table->field);
4737
*field= NULL; /* mark the end of the list */
4738
share->blob_field[blob_count]= 0; /* mark the end of the list */
4739
share->blob_fields= blob_count;
4741
null_pack_length= (null_count + 7)/8;
4742
share->reclength= record_length + null_pack_length;
4743
share->rec_buff_length= ALIGN_SIZE(share->reclength + 1);
4744
table->record[0]= (uchar*) thd->alloc(share->rec_buff_length);
4745
if (!table->record[0])
4748
if (null_pack_length)
4750
table->null_flags= (uchar*) table->record[0];
4751
share->null_fields= null_count;
4752
share->null_bytes= null_pack_length;
4755
table->in_use= thd; /* field->reset() may access table->in_use */
4757
/* Set up field pointers */
4758
uchar *null_pos= table->record[0];
4759
uchar *field_pos= null_pos + share->null_bytes;
4762
for (field= table->field; *field; ++field)
4764
Field *cur_field= *field;
4765
if ((cur_field->flags & NOT_NULL_FLAG))
4766
cur_field->move_field(field_pos);
4769
cur_field->move_field(field_pos, (uchar*) null_pos, null_bit);
4771
if (null_bit == (1 << 8))
4779
field_pos+= cur_field->pack_length();
4784
for (field= table->field; *field; ++field)
4785
delete *field; /* just invokes field destructor */
4790
bool Table::open_tmp_table()
4793
if ((error=file->ha_open(this, s->table_name.str,O_RDWR,
4794
HA_OPEN_TMP_TABLE | HA_OPEN_INTERNAL_TABLE)))
4796
file->print_error(error,MYF(0)); /* purecov: inspected */
4800
(void) file->extra(HA_EXTRA_QUICK); /* Faster */
4806
Create MyISAM temporary table
4809
create_myisam_tmp_table()
4810
keyinfo Description of the index (there is always one index)
4811
start_recinfo MyISAM's column descriptions
4812
recinfo INOUT End of MyISAM's column descriptions
4816
Create a MyISAM temporary table according to passed description. The is
4817
assumed to have one unique index or constraint.
4819
The passed array or MI_COLUMNDEF structures must have this form:
4821
1. 1-byte column (afaiu for 'deleted' flag) (note maybe not 1-byte
4822
when there are many nullable columns)
4824
3. One free MI_COLUMNDEF element (*recinfo points here)
4826
This function may use the free element to create hash column for unique
4834
bool Table::create_myisam_tmp_table(KEY *keyinfo,
4835
MI_COLUMNDEF *start_recinfo,
4836
MI_COLUMNDEF **recinfo,
4841
MI_UNIQUEDEF uniquedef;
4842
TABLE_SHARE *share= s;
4845
{ // Get keys for ni_create
4846
bool using_unique_constraint=0;
4847
HA_KEYSEG *seg= (HA_KEYSEG*) alloc_root(&this->mem_root,
4848
sizeof(*seg) * keyinfo->key_parts);
4852
memset(seg, 0, sizeof(*seg) * keyinfo->key_parts);
4853
if (keyinfo->key_length >= file->max_key_length() ||
4854
keyinfo->key_parts > file->max_key_parts() ||
4857
/* Can't create a key; Make a unique constraint instead of a key */
4860
using_unique_constraint=1;
4861
memset(&uniquedef, 0, sizeof(uniquedef));
4862
uniquedef.keysegs=keyinfo->key_parts;
4864
uniquedef.null_are_equal=1;
4866
/* Create extra column for hash value */
4867
memset(*recinfo, 0, sizeof(**recinfo));
4868
(*recinfo)->type= FIELD_CHECK;
4869
(*recinfo)->length=MI_UNIQUE_HASH_LENGTH;
4871
share->reclength+=MI_UNIQUE_HASH_LENGTH;
4875
/* Create an unique key */
4876
memset(&keydef, 0, sizeof(keydef));
4877
keydef.flag=HA_NOSAME | HA_BINARY_PACK_KEY | HA_PACK_KEY;
4878
keydef.keysegs= keyinfo->key_parts;
4881
for (uint i=0; i < keyinfo->key_parts ; i++,seg++)
4883
Field *field=keyinfo->key_part[i].field;
4885
seg->language= field->charset()->number;
4886
seg->length= keyinfo->key_part[i].length;
4887
seg->start= keyinfo->key_part[i].offset;
4888
if (field->flags & BLOB_FLAG)
4891
((keyinfo->key_part[i].key_type & FIELDFLAG_BINARY) ?
4892
HA_KEYTYPE_VARBINARY2 : HA_KEYTYPE_VARTEXT2);
4893
seg->bit_start= (uint8_t)(field->pack_length() - share->blob_ptr_size);
4894
seg->flag= HA_BLOB_PART;
4895
seg->length=0; // Whole blob in unique constraint
4899
seg->type= keyinfo->key_part[i].type;
4901
if (!(field->flags & NOT_NULL_FLAG))
4903
seg->null_bit= field->null_bit;
4904
seg->null_pos= (uint) (field->null_ptr - (uchar*) record[0]);
4906
We are using a GROUP BY on something that contains NULL
4907
In this case we have to tell MyISAM that two NULL should
4908
on INSERT be regarded at the same value
4910
if (!using_unique_constraint)
4911
keydef.flag|= HA_NULL_ARE_EQUAL;
4915
MI_CREATE_INFO create_info;
4916
memset(&create_info, 0, sizeof(create_info));
4918
if ((options & (OPTION_BIG_TABLES | SELECT_SMALL_RESULT)) ==
4920
create_info.data_file_length= ~(uint64_t) 0;
4922
if ((error=mi_create(share->table_name.str, share->keys, &keydef,
4923
(uint) (*recinfo-start_recinfo),
4925
share->uniques, &uniquedef,
4927
HA_CREATE_TMP_TABLE)))
4929
file->print_error(error,MYF(0)); /* purecov: inspected */
4933
status_var_increment(in_use->status_var.created_tmp_disk_tables);
4934
share->db_record_offset= 1;
4941
void Table::free_tmp_table(THD *thd)
4943
MEM_ROOT own_root= mem_root;
4944
const char *save_proc_info;
4946
save_proc_info=thd->proc_info;
4947
thd_proc_info(thd, "removing tmp table");
4952
file->ha_drop_table(s->table_name.str);
4954
file->ha_delete_table(s->table_name.str);
4959
for (Field **ptr= field ; *ptr ; ptr++)
4961
free_io_cache(this);
4963
if (temp_pool_slot != MY_BIT_NONE)
4964
bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
4966
plugin_unlock(0, s->db_plugin);
4968
free_root(&own_root, MYF(0)); /* the table is allocated in its own root */
4969
thd_proc_info(thd, save_proc_info);
4975
If a HEAP table gets full, create a MyISAM table and copy all rows
4979
bool create_myisam_from_heap(THD *thd, TABLE *table,
4980
MI_COLUMNDEF *start_recinfo,
4981
MI_COLUMNDEF **recinfo,
4982
int error, bool ignore_last_dupp_key_error)
4986
const char *save_proc_info;
4989
if (table->s->db_type() != heap_hton ||
4990
error != HA_ERR_RECORD_FILE_FULL)
4992
table->file->print_error(error,MYF(0));
4997
new_table.s= &share;
4998
new_table.s->db_plugin= ha_lock_engine(thd, myisam_hton);
4999
if (!(new_table.file= get_new_handler(&share, &new_table.mem_root,
5000
new_table.s->db_type())))
5001
return(1); // End of memory
5003
save_proc_info=thd->proc_info;
5004
thd_proc_info(thd, "converting HEAP to MyISAM");
5006
if (new_table.create_myisam_tmp_table(table->key_info, start_recinfo,
5007
recinfo, thd->lex->select_lex.options |
5010
if (new_table.open_tmp_table())
5012
if (table->file->indexes_are_disabled())
5013
new_table.file->ha_disable_indexes(HA_KEY_SWITCH_ALL);
5014
table->file->ha_index_or_rnd_end();
5015
table->file->ha_rnd_init(1);
5018
new_table.file->extra(HA_EXTRA_NO_ROWS);
5019
new_table.no_rows=1;
5022
#ifdef TO_BE_DONE_LATER_IN_4_1
5024
To use start_bulk_insert() (which is new in 4.1) we need to find
5025
all places where a corresponding end_bulk_insert() should be put.
5027
table->file->info(HA_STATUS_VARIABLE); /* update table->file->stats.records */
5028
new_table.file->ha_start_bulk_insert(table->file->stats.records);
5030
/* HA_EXTRA_WRITE_CACHE can stay until close, no need to disable it */
5031
new_table.file->extra(HA_EXTRA_WRITE_CACHE);
5035
copy all old rows from heap table to MyISAM table
5036
This is the only code that uses record[1] to read/write but this
5037
is safe as this is a temporary MyISAM table without timestamp/autoincrement.
5039
while (!table->file->rnd_next(new_table.record[1]))
5041
write_err= new_table.file->ha_write_row(new_table.record[1]);
5045
/* copy row that filled HEAP table */
5046
if ((write_err=new_table.file->ha_write_row(table->record[0])))
5048
if (new_table.file->is_fatal_error(write_err, HA_CHECK_DUP) ||
5049
!ignore_last_dupp_key_error)
5053
/* remove heap table and change to use myisam table */
5054
(void) table->file->ha_rnd_end();
5055
(void) table->file->close(); // This deletes the table !
5058
plugin_unlock(0, table->s->db_plugin);
5059
share.db_plugin= my_plugin_lock(0, &share.db_plugin);
5060
new_table.s= table->s; // Keep old share
5064
table->file->change_table_ptr(table, table->s);
5065
table->use_all_columns();
5068
const char *new_proc_info=
5069
(!strcmp(save_proc_info,"Copying to tmp table") ?
5070
"Copying to tmp table on disk" : save_proc_info);
5071
thd_proc_info(thd, new_proc_info);
5076
table->file->print_error(write_err, MYF(0));
5077
(void) table->file->ha_rnd_end();
5078
(void) new_table.file->close();
5080
new_table.file->ha_delete_table(new_table.s->table_name.str);
5082
delete new_table.file;
5083
thd_proc_info(thd, save_proc_info);
5084
table->mem_root= new_table.mem_root;
3577
5089
/*****************************************************************************
3578
5090
** Instansiate templates