2782
Natural_join_column::Natural_join_column(Field_translator *field_param,
2785
assert(tab->field_translation);
2786
view_field= field_param;
2793
Natural_join_column::Natural_join_column(Field *field_param,
2796
assert(tab->table == field_param->table);
2797
table_field= field_param;
2804
const char *Natural_join_column::name()
2808
assert(table_field == NULL);
2809
return view_field->name;
2812
return table_field->field_name;
2816
Item *Natural_join_column::create_item(THD *thd)
2820
assert(table_field == NULL);
2821
return create_view_field(thd, table_ref, &view_field->item,
2824
return new Item_field(thd, &thd->lex->current_select->context, table_field);
2828
Field *Natural_join_column::field()
2832
assert(table_field == NULL);
2839
const char *Natural_join_column::table_name()
2842
return table_ref->alias;
2846
const char *Natural_join_column::db_name()
2849
Test that TABLE_LIST::db is the same as st_table_share::db to
2850
ensure consistency. An exception are I_S schema tables, which
2851
are inconsistent in this respect.
2853
assert(!strcmp(table_ref->db,
2854
table_ref->table->s->db.str) ||
2855
(table_ref->schema_table &&
2856
table_ref->table->s->db.str[0] == 0));
2857
return table_ref->db;
2861
void Field_iterator_view::set(TABLE_LIST *table)
2863
assert(table->field_translation);
2865
ptr= table->field_translation;
2866
array_end= table->field_translation_end;
2870
const char *Field_iterator_table::name()
2872
return (*ptr)->field_name;
2876
Item *Field_iterator_table::create_item(THD *thd)
2878
SELECT_LEX *select= thd->lex->current_select;
2880
Item_field *item= new Item_field(thd, &select->context, *ptr);
2881
if (item && thd->variables.sql_mode & MODE_ONLY_FULL_GROUP_BY &&
2882
!thd->lex->in_sum_func && select->cur_pos_in_select_list != UNDEF_POS)
2884
select->non_agg_fields.push_back(item);
2885
item->marker= select->cur_pos_in_select_list;
2891
const char *Field_iterator_view::name()
2897
Item *Field_iterator_view::create_item(THD *thd)
2899
return create_view_field(thd, view, &ptr->item, ptr->name);
2902
Item *create_view_field(THD *thd __attribute__((__unused__)),
2903
TABLE_LIST *view, Item **field_ref,
2904
const char *name __attribute__((__unused__)))
2906
if (view->schema_table_reformed)
2908
Item *field= *field_ref;
2911
Translation table items are always Item_fields and already fixed
2912
('mysql_schema_table' function). So we can return directly the
2913
field. This case happens only for 'show & where' commands.
2915
assert(field && field->fixed);
2923
void Field_iterator_natural_join::set(TABLE_LIST *table_ref)
2925
assert(table_ref->join_columns);
2926
column_ref_it.init(*(table_ref->join_columns));
2927
cur_column_ref= column_ref_it++;
2931
void Field_iterator_natural_join::next()
2933
cur_column_ref= column_ref_it++;
2934
assert(!cur_column_ref || ! cur_column_ref->table_field ||
2935
cur_column_ref->table_ref->table ==
2936
cur_column_ref->table_field->table);
2940
void Field_iterator_table_ref::set_field_iterator()
2943
If the table reference we are iterating over is a natural join, or it is
2944
an operand of a natural join, and TABLE_LIST::join_columns contains all
2945
the columns of the join operand, then we pick the columns from
2946
TABLE_LIST::join_columns, instead of the orginial container of the
2947
columns of the join operator.
2949
if (table_ref->is_join_columns_complete)
2951
/* Necesary, but insufficient conditions. */
2952
assert(table_ref->is_natural_join ||
2953
table_ref->nested_join ||
2954
((table_ref->join_columns && /* This is a merge view. */ (table_ref->field_translation && table_ref->join_columns->elements == (ulong)(table_ref->field_translation_end - table_ref->field_translation))) ||
2955
/* This is stored table or a tmptable view. */
2956
(!table_ref->field_translation && table_ref->join_columns->elements == table_ref->table->s->fields)));
2957
field_it= &natural_join_it;
2959
/* This is a base table or stored view. */
2962
assert(table_ref->table);
2963
field_it= &table_field_it;
2965
field_it->set(table_ref);
2970
void Field_iterator_table_ref::set(TABLE_LIST *table)
2973
first_leaf= table->first_leaf_for_name_resolution();
2974
last_leaf= table->last_leaf_for_name_resolution();
2975
assert(first_leaf && last_leaf);
2976
table_ref= first_leaf;
2977
set_field_iterator();
2981
void Field_iterator_table_ref::next()
2983
/* Move to the next field in the current table reference. */
2986
If all fields of the current table reference are exhausted, move to
2987
the next leaf table reference.
2989
if (field_it->end_of_fields() && table_ref != last_leaf)
2991
table_ref= table_ref->next_name_resolution_table;
2993
set_field_iterator();
2998
const char *Field_iterator_table_ref::table_name()
3000
if (table_ref->is_natural_join)
3001
return natural_join_it.column_ref()->table_name();
3003
assert(!strcmp(table_ref->table_name,
3004
table_ref->table->s->table_name.str));
3005
return table_ref->table_name;
3009
const char *Field_iterator_table_ref::db_name()
3011
if (table_ref->is_natural_join)
3012
return natural_join_it.column_ref()->db_name();
3015
Test that TABLE_LIST::db is the same as st_table_share::db to
3016
ensure consistency. An exception are I_S schema tables, which
3017
are inconsistent in this respect.
3019
assert(!strcmp(table_ref->db, table_ref->table->s->db.str) ||
3020
(table_ref->schema_table &&
3021
table_ref->table->s->db.str[0] == 0));
3023
return table_ref->db;
3028
Create new or return existing column reference to a column of a
3032
Field_iterator_table_ref::get_or_create_column_ref()
3033
parent_table_ref the parent table reference over which the
3034
iterator is iterating
3037
Create a new natural join column for the current field of the
3038
iterator if no such column was created, or return an already
3039
created natural join column. The former happens for base tables or
3040
views, and the latter for natural/using joins. If a new field is
3041
created, then the field is added to 'parent_table_ref' if it is
3042
given, or to the original table referene of the field if
3043
parent_table_ref == NULL.
3046
This method is designed so that when a Field_iterator_table_ref
3047
walks through the fields of a table reference, all its fields
3048
are created and stored as follows:
3049
- If the table reference being iterated is a stored table, view or
3050
natural/using join, store all natural join columns in a list
3051
attached to that table reference.
3052
- If the table reference being iterated is a nested join that is
3053
not natural/using join, then do not materialize its result
3054
fields. This is OK because for such table references
3055
Field_iterator_table_ref iterates over the fields of the nested
3056
table references (recursively). In this way we avoid the storage
3057
of unnecessay copies of result columns of nested joins.
3060
# Pointer to a column of a natural join (or its operand)
3061
NULL No memory to allocate the column
3064
Natural_join_column *
3065
Field_iterator_table_ref::get_or_create_column_ref(TABLE_LIST *parent_table_ref)
3067
Natural_join_column *nj_col;
3068
bool is_created= true;
3070
TABLE_LIST *add_table_ref= parent_table_ref ?
3071
parent_table_ref : table_ref;
3073
if (field_it == &table_field_it)
3075
/* The field belongs to a stored table. */
3076
Field *tmp_field= table_field_it.field();
3077
nj_col= new Natural_join_column(tmp_field, table_ref);
3078
field_count= table_ref->table->s->fields;
3080
else if (field_it == &view_field_it)
3082
/* The field belongs to a merge view or information schema table. */
3083
Field_translator *translated_field= view_field_it.field_translator();
3084
nj_col= new Natural_join_column(translated_field, table_ref);
3085
field_count= table_ref->field_translation_end -
3086
table_ref->field_translation;
3091
The field belongs to a NATURAL join, therefore the column reference was
3092
already created via one of the two constructor calls above. In this case
3093
we just return the already created column reference.
3095
assert(table_ref->is_join_columns_complete);
3097
nj_col= natural_join_it.column_ref();
3100
assert(!nj_col->table_field ||
3101
nj_col->table_ref->table == nj_col->table_field->table);
3104
If the natural join column was just created add it to the list of
3105
natural join columns of either 'parent_table_ref' or to the table
3106
reference that directly contains the original field.
3110
/* Make sure not all columns were materialized. */
3111
assert(!add_table_ref->is_join_columns_complete);
3112
if (!add_table_ref->join_columns)
3114
/* Create a list of natural join columns on demand. */
3115
if (!(add_table_ref->join_columns= new List<Natural_join_column>))
3117
add_table_ref->is_join_columns_complete= false;
3119
add_table_ref->join_columns->push_back(nj_col);
3121
If new fields are added to their original table reference, mark if
3122
all fields were added. We do it here as the caller has no easy way
3123
of knowing when to do it.
3124
If the fields are being added to parent_table_ref, then the caller
3125
must take care to mark when all fields are created/added.
3127
if (!parent_table_ref &&
3128
add_table_ref->join_columns->elements == field_count)
3129
add_table_ref->is_join_columns_complete= true;
3137
Return an existing reference to a column of a natural/using join.
3140
Field_iterator_table_ref::get_natural_column_ref()
3143
The method should be called in contexts where it is expected that
3144
all natural join columns are already created, and that the column
3145
being retrieved is a Natural_join_column.
3148
# Pointer to a column of a natural join (or its operand)
3149
NULL No memory to allocate the column
3152
Natural_join_column *
3153
Field_iterator_table_ref::get_natural_column_ref()
3155
Natural_join_column *nj_col;
3157
assert(field_it == &natural_join_it);
3159
The field belongs to a NATURAL join, therefore the column reference was
3160
already created via one of the two constructor calls above. In this case
3161
we just return the already created column reference.
3163
nj_col= natural_join_it.column_ref();
3165
(!nj_col->table_field ||
3166
nj_col->table_ref->table == nj_col->table_field->table));
3021
3170
/*****************************************************************************
3022
3171
Functions to handle column usage bitmaps (read_set, write_set etc...)
3023
3172
*****************************************************************************/
3025
3174
/* Reset all columns bitmaps */
3027
void Table::clear_column_bitmaps()
3176
void st_table::clear_column_bitmaps()
3030
3179
Reset column read/write usage. It's identical to:
3031
3180
bitmap_clear_all(&table->def_read_set);
3032
3181
bitmap_clear_all(&table->def_write_set);
3034
memset(def_read_set.bitmap, 0, s->column_bitmap_size*2);
3183
bzero((char*) def_read_set.bitmap, s->column_bitmap_size*2);
3035
3184
column_bitmaps_set(&def_read_set, &def_write_set);
3543
3649
path path to file
3550
bool mysql_frm_type(THD *thd __attribute__((unused)),
3551
char *path, enum legacy_db_type *dbt)
3656
frm_type_enum mysql_frm_type(THD *thd __attribute__((__unused__)),
3657
char *path, enum legacy_db_type *dbt)
3554
unsigned char header[10]; /* This should be optimized */
3660
uchar header[10]; /* This should be optimized */
3557
3663
*dbt= DB_TYPE_UNKNOWN;
3559
if ((file= open(path, O_RDONLY)) < 0)
3561
error= my_read(file, (unsigned char*) header, sizeof(header), MYF(MY_NABP));
3665
if ((file= my_open(path, O_RDONLY | O_SHARE, MYF(0))) < 0)
3666
return(FRMTYPE_ERROR);
3667
error= my_read(file, (uchar*) header, sizeof(header), MYF(MY_NABP));
3562
3668
my_close(file, MYF(MY_WME));
3671
return(FRMTYPE_ERROR);
3568
3674
This is just a check for DB_TYPE. We'll return default unknown type
3569
3675
if the following test is true (arg #3). This should not have effect
3570
3676
on return value from this function (default FRMTYPE_TABLE)
3572
if (header[0] != (unsigned char) 254 || header[1] != 1 ||
3678
if (header[0] != (uchar) 254 || header[1] != 1 ||
3573
3679
(header[2] != FRM_VER && header[2] != FRM_VER+1 &&
3574
3680
(header[2] < FRM_VER+3 || header[2] > FRM_VER+4)))
3681
return(FRMTYPE_TABLE);
3577
3683
*dbt= (enum legacy_db_type) (uint) *(header + 3);
3578
return true; // Is probably a .frm table
3581
/****************************************************************************
3582
Functions for creating temporary tables.
3583
****************************************************************************/
3587
void free_tmp_table(THD *thd, Table *entry);
3590
Create field for temporary table from given field.
3592
@param thd Thread handler
3593
@param org_field field from which new field will be created
3594
@param name New field name
3595
@param table Temporary table
3596
@param item !=NULL if item->result_field should point to new field.
3597
This is relevant for how fill_record() is going to work:
3598
If item != NULL then fill_record() will update
3599
the record in the original table.
3600
If item == NULL then fill_record() will update
3602
@param convert_blob_length If >0 create a varstring(convert_blob_length)
3603
field instead of blob.
3611
Field *create_tmp_field_from_field(THD *thd, Field *org_field,
3612
const char *name, Table *table,
3613
Item_field *item, uint32_t convert_blob_length)
3618
Make sure that the blob fits into a Field_varstring which has
3621
if (convert_blob_length && convert_blob_length <= Field_varstring::MAX_SIZE &&
3622
(org_field->flags & BLOB_FLAG))
3623
new_field= new Field_varstring(convert_blob_length,
3624
org_field->maybe_null(),
3625
org_field->field_name, table->s,
3626
org_field->charset());
3628
new_field= org_field->new_field(thd->mem_root, table,
3629
table == org_field->table);
3632
new_field->init(table);
3633
new_field->orig_table= org_field->orig_table;
3635
item->result_field= new_field;
3637
new_field->field_name= name;
3638
new_field->flags|= (org_field->flags & NO_DEFAULT_VALUE_FLAG);
3639
if (org_field->maybe_null() || (item && item->maybe_null))
3640
new_field->flags&= ~NOT_NULL_FLAG; // Because of outer join
3641
if (org_field->type() == DRIZZLE_TYPE_VARCHAR)
3642
table->s->db_create_options|= HA_OPTION_PACK_RECORD;
3643
else if (org_field->type() == DRIZZLE_TYPE_DOUBLE)
3644
((Field_double *) new_field)->not_fixed= true;
3650
Create field for temporary table using type of given item.
3652
@param thd Thread handler
3653
@param item Item to create a field for
3654
@param table Temporary table
3655
@param copy_func If set and item is a function, store copy of
3657
@param modify_item 1 if item->result_field should point to new
3658
item. This is relevent for how fill_record()
3660
If modify_item is 1 then fill_record() will
3661
update the record in the original table.
3662
If modify_item is 0 then fill_record() will
3663
update the temporary table
3664
@param convert_blob_length If >0 create a varstring(convert_blob_length)
3665
field instead of blob.
3673
static Field *create_tmp_field_from_item(THD *thd __attribute__((unused)),
3674
Item *item, Table *table,
3675
Item ***copy_func, bool modify_item,
3676
uint32_t convert_blob_length)
3678
bool maybe_null= item->maybe_null;
3681
switch (item->result_type()) {
3683
new_field= new Field_double(item->max_length, maybe_null,
3684
item->name, item->decimals, true);
3688
Select an integer type with the minimal fit precision.
3689
MY_INT32_NUM_DECIMAL_DIGITS is sign inclusive, don't consider the sign.
3690
Values with MY_INT32_NUM_DECIMAL_DIGITS digits may or may not fit into
3691
Field_long : make them Field_int64_t.
3693
if (item->max_length >= (MY_INT32_NUM_DECIMAL_DIGITS - 1))
3694
new_field=new Field_int64_t(item->max_length, maybe_null,
3695
item->name, item->unsigned_flag);
3697
new_field=new Field_long(item->max_length, maybe_null,
3698
item->name, item->unsigned_flag);
3701
assert(item->collation.collation);
3703
enum enum_field_types type;
3705
DATE/TIME fields have STRING_RESULT result type.
3706
To preserve type they needed to be handled separately.
3708
if ((type= item->field_type()) == DRIZZLE_TYPE_DATETIME ||
3709
type == DRIZZLE_TYPE_TIME || type == DRIZZLE_TYPE_NEWDATE ||
3710
type == DRIZZLE_TYPE_TIMESTAMP)
3711
new_field= item->tmp_table_field_from_field_type(table, 1);
3713
Make sure that the blob fits into a Field_varstring which has
3716
else if (item->max_length/item->collation.collation->mbmaxlen > 255 &&
3717
convert_blob_length <= Field_varstring::MAX_SIZE &&
3718
convert_blob_length)
3719
new_field= new Field_varstring(convert_blob_length, maybe_null,
3720
item->name, table->s,
3721
item->collation.collation);
3723
new_field= item->make_string_field(table);
3724
new_field->set_derivation(item->collation.derivation);
3726
case DECIMAL_RESULT:
3728
uint8_t dec= item->decimals;
3729
uint8_t intg= ((Item_decimal *) item)->decimal_precision() - dec;
3730
uint32_t len= item->max_length;
3733
Trying to put too many digits overall in a DECIMAL(prec,dec)
3734
will always throw a warning. We must limit dec to
3735
DECIMAL_MAX_SCALE however to prevent an assert() later.
3740
signed int overflow;
3742
dec= cmin(dec, (uint8_t)DECIMAL_MAX_SCALE);
3745
If the value still overflows the field with the corrected dec,
3746
we'll throw out decimals rather than integers. This is still
3747
bad and of course throws a truncation warning.
3748
+1: for decimal point
3751
overflow= my_decimal_precision_to_length(intg + dec, dec,
3752
item->unsigned_flag) - len;
3755
dec= cmax(0, dec - overflow); // too long, discard fract
3757
len -= item->decimals - dec; // corrected value fits
3760
new_field= new Field_new_decimal(len, maybe_null, item->name,
3761
dec, item->unsigned_flag);
3766
// This case should never be choosen
3772
new_field->init(table);
3774
if (copy_func && item->is_result_field())
3775
*((*copy_func)++) = item; // Save for copy_funcs
3777
item->set_result_field(new_field);
3778
if (item->type() == Item::NULL_ITEM)
3779
new_field->is_created_from_null_item= true;
3785
Create field for information schema table.
3787
@param thd Thread handler
3788
@param table Temporary table
3789
@param item Item to create a field for
3797
Field *create_tmp_field_for_schema(THD *thd __attribute__((unused)),
3798
Item *item, Table *table)
3800
if (item->field_type() == DRIZZLE_TYPE_VARCHAR)
3803
if (item->max_length > MAX_FIELD_VARCHARLENGTH)
3804
field= new Field_blob(item->max_length, item->maybe_null,
3805
item->name, item->collation.collation);
3807
field= new Field_varstring(item->max_length, item->maybe_null,
3809
table->s, item->collation.collation);
3814
return item->tmp_table_field_from_field_type(table, 0);
3819
Create field for temporary table.
3821
@param thd Thread handler
3822
@param table Temporary table
3823
@param item Item to create a field for
3824
@param type Type of item (normally item->type)
3825
@param copy_func If set and item is a function, store copy of item
3827
@param from_field if field will be created using other field as example,
3828
pointer example field will be written here
3829
@param default_field If field has a default value field, store it here
3830
@param group 1 if we are going to do a relative group by on result
3831
@param modify_item 1 if item->result_field should point to new item.
3832
This is relevent for how fill_record() is going to
3834
If modify_item is 1 then fill_record() will update
3835
the record in the original table.
3836
If modify_item is 0 then fill_record() will update
3838
@param convert_blob_length If >0 create a varstring(convert_blob_length)
3839
field instead of blob.
3847
Field *create_tmp_field(THD *thd, Table *table,Item *item, Item::Type type,
3848
Item ***copy_func, Field **from_field,
3849
Field **default_field,
3850
bool group, bool modify_item,
3851
bool table_cant_handle_bit_fields __attribute__((unused)),
3852
bool make_copy_field,
3853
uint32_t convert_blob_length)
3856
Item::Type orig_type= type;
3859
if (type != Item::FIELD_ITEM &&
3860
item->real_item()->type() == Item::FIELD_ITEM)
3863
item= item->real_item();
3864
type= Item::FIELD_ITEM;
3868
case Item::SUM_FUNC_ITEM:
3870
Item_sum *item_sum=(Item_sum*) item;
3871
result= item_sum->create_tmp_field(group, table, convert_blob_length);
3873
my_error(ER_OUT_OF_RESOURCES, MYF(ME_FATALERROR));
3876
case Item::FIELD_ITEM:
3877
case Item::DEFAULT_VALUE_ITEM:
3879
Item_field *field= (Item_field*) item;
3880
bool orig_modify= modify_item;
3881
if (orig_type == Item::REF_ITEM)
3884
If item have to be able to store NULLs but underlaid field can't do it,
3885
create_tmp_field_from_field() can't be used for tmp field creation.
3887
if (field->maybe_null && !field->field->maybe_null())
3889
result= create_tmp_field_from_item(thd, item, table, NULL,
3890
modify_item, convert_blob_length);
3891
*from_field= field->field;
3892
if (result && modify_item)
3893
field->result_field= result;
3896
result= create_tmp_field_from_field(thd, (*from_field= field->field),
3897
orig_item ? orig_item->name :
3900
modify_item ? field :
3902
convert_blob_length);
3903
if (orig_type == Item::REF_ITEM && orig_modify)
3904
((Item_ref*)orig_item)->set_result_field(result);
3905
if (field->field->eq_def(result))
3906
*default_field= field->field;
3910
case Item::FUNC_ITEM:
3912
case Item::COND_ITEM:
3913
case Item::FIELD_AVG_ITEM:
3914
case Item::FIELD_STD_ITEM:
3915
case Item::SUBSELECT_ITEM:
3916
/* The following can only happen with 'CREATE TABLE ... SELECT' */
3917
case Item::PROC_ITEM:
3918
case Item::INT_ITEM:
3919
case Item::REAL_ITEM:
3920
case Item::DECIMAL_ITEM:
3921
case Item::STRING_ITEM:
3922
case Item::REF_ITEM:
3923
case Item::NULL_ITEM:
3924
case Item::VARBIN_ITEM:
3925
if (make_copy_field)
3927
assert(((Item_result_field*)item)->result_field);
3928
*from_field= ((Item_result_field*)item)->result_field;
3930
return create_tmp_field_from_item(thd, item, table,
3931
(make_copy_field ? 0 : copy_func),
3932
modify_item, convert_blob_length);
3933
case Item::TYPE_HOLDER:
3934
result= ((Item_type_holder *)item)->make_field_by_type(table);
3935
result->set_derivation(item->collation.derivation);
3937
default: // Dosen't have to be stored
3943
Create a temp table according to a field list.
3945
Given field pointers are changed to point at tmp_table for
3946
send_fields. The table object is self contained: it's
3947
allocated in its own memory root, as well as Field objects
3948
created for table columns.
3949
This function will replace Item_sum items in 'fields' list with
3950
corresponding Item_field items, pointing at the fields in the
3951
temporary table, unless this was prohibited by true
3952
value of argument save_sum_fields. The Item_field objects
3953
are created in THD memory root.
3955
@param thd thread handle
3956
@param param a description used as input to create the table
3957
@param fields list of items that will be used to define
3958
column types of the table (also see NOTES)
3959
@param group TODO document
3960
@param distinct should table rows be distinct
3961
@param save_sum_fields see NOTES
3962
@param select_options
3964
@param table_alias possible name of the temporary table that can
3965
be used for name resolving; can be "".
3968
#define STRING_TOTAL_LENGTH_TO_PACK_ROWS 128
3969
#define AVG_STRING_LENGTH_TO_PACK_ROWS 64
3970
#define RATIO_TO_PACK_ROWS 2
3971
#define MIN_STRING_LENGTH_TO_PACK_ROWS 10
3974
create_tmp_table(THD *thd,TMP_TABLE_PARAM *param,List<Item> &fields,
3975
order_st *group, bool distinct, bool save_sum_fields,
3976
uint64_t select_options, ha_rows rows_limit,
3979
MEM_ROOT *mem_root_save, own_root;
3982
uint i,field_count,null_count,null_pack_length;
3983
uint32_t copy_func_count= param->func_count;
3984
uint32_t hidden_null_count, hidden_null_pack_length, hidden_field_count;
3985
uint32_t blob_count,group_null_items, string_count;
3986
uint32_t temp_pool_slot=MY_BIT_NONE;
3987
uint32_t fieldnr= 0;
3988
ulong reclength, string_total_length;
3989
bool using_unique_constraint= 0;
3990
bool use_packed_rows= 0;
3991
bool not_all_columns= !(select_options & TMP_TABLE_ALL_COLUMNS);
3992
char *tmpname,path[FN_REFLEN];
3993
unsigned char *pos, *group_buff, *bitmaps;
3994
unsigned char *null_flags;
3995
Field **reg_field, **from_field, **default_field;
3996
uint32_t *blob_field;
3999
KEY_PART_INFO *key_part_info;
4001
MI_COLUMNDEF *recinfo;
4002
uint32_t total_uneven_bit_length= 0;
4003
bool force_copy_fields= param->force_copy_fields;
4005
status_var_increment(thd->status_var.created_tmp_tables);
4007
if (use_temp_pool && !(test_flags & TEST_KEEP_TMP_TABLES))
4008
temp_pool_slot = bitmap_lock_set_next(&temp_pool);
4010
if (temp_pool_slot != MY_BIT_NONE) // we got a slot
4011
sprintf(path, "%s_%lx_%i", tmp_file_prefix,
4012
current_pid, temp_pool_slot);
4015
/* if we run out of slots or we are not using tempool */
4016
sprintf(path,"%s%lx_%lx_%x", tmp_file_prefix,current_pid,
4017
thd->thread_id, thd->tmp_table++);
4021
No need to change table name to lower case as we are only creating
4022
MyISAM or HEAP tables here
4024
fn_format(path, path, mysql_tmpdir, "", MY_REPLACE_EXT|MY_UNPACK_FILENAME);
4029
if (!param->quick_group)
4030
group=0; // Can't use group key
4031
else for (order_st *tmp=group ; tmp ; tmp=tmp->next)
4034
marker == 4 means two things:
4035
- store NULLs in the key, and
4036
- convert BIT fields to 64-bit long, needed because MEMORY tables
4037
can't index BIT fields.
4039
(*tmp->item)->marker= 4;
4040
if ((*tmp->item)->max_length >= CONVERT_IF_BIGGER_TO_BLOB)
4041
using_unique_constraint=1;
4043
if (param->group_length >= MAX_BLOB_WIDTH)
4044
using_unique_constraint=1;
4046
distinct=0; // Can't use distinct
4049
field_count=param->field_count+param->func_count+param->sum_func_count;
4050
hidden_field_count=param->hidden_field_count;
4053
When loose index scan is employed as access method, it already
4054
computes all groups and the result of all aggregate functions. We
4055
make space for the items of the aggregate function in the list of
4056
functions TMP_TABLE_PARAM::items_to_copy, so that the values of
4057
these items are stored in the temporary table.
4059
if (param->precomputed_group_by)
4060
copy_func_count+= param->sum_func_count;
4062
init_sql_alloc(&own_root, TABLE_ALLOC_BLOCK_SIZE, 0);
4064
if (!multi_alloc_root(&own_root,
4065
&table, sizeof(*table),
4066
&share, sizeof(*share),
4067
®_field, sizeof(Field*) * (field_count+1),
4068
&default_field, sizeof(Field*) * (field_count),
4069
&blob_field, sizeof(uint)*(field_count+1),
4070
&from_field, sizeof(Field*)*field_count,
4071
©_func, sizeof(*copy_func)*(copy_func_count+1),
4072
¶m->keyinfo, sizeof(*param->keyinfo),
4074
sizeof(*key_part_info)*(param->group_parts+1),
4075
¶m->start_recinfo,
4076
sizeof(*param->recinfo)*(field_count*2+4),
4077
&tmpname, (uint) strlen(path)+1,
4078
&group_buff, (group && ! using_unique_constraint ?
4079
param->group_length : 0),
4080
&bitmaps, bitmap_buffer_size(field_count)*2,
4083
if (temp_pool_slot != MY_BIT_NONE)
4084
bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
4085
return(NULL); /* purecov: inspected */
4087
/* Copy_field belongs to TMP_TABLE_PARAM, allocate it in THD mem_root */
4088
if (!(param->copy_field= copy= new (thd->mem_root) Copy_field[field_count]))
4090
if (temp_pool_slot != MY_BIT_NONE)
4091
bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
4092
free_root(&own_root, MYF(0)); /* purecov: inspected */
4093
return(NULL); /* purecov: inspected */
4095
param->items_to_copy= copy_func;
4096
my_stpcpy(tmpname,path);
4097
/* make table according to fields */
4099
memset(table, 0, sizeof(*table));
4100
memset(reg_field, 0, sizeof(Field*)*(field_count+1));
4101
memset(default_field, 0, sizeof(Field*) * (field_count));
4102
memset(from_field, 0, sizeof(Field*)*field_count);
4104
table->mem_root= own_root;
4105
mem_root_save= thd->mem_root;
4106
thd->mem_root= &table->mem_root;
4108
table->field=reg_field;
4109
table->alias= table_alias;
4110
table->reginfo.lock_type=TL_WRITE; /* Will be updated */
4111
table->db_stat=HA_OPEN_KEYFILE+HA_OPEN_RNDFILE;
4113
table->temp_pool_slot = temp_pool_slot;
4114
table->copy_blobs= 1;
4116
table->quick_keys.init();
4117
table->covering_keys.init();
4118
table->keys_in_use_for_query.init();
4120
table->setShare(share);
4121
init_tmp_table_share(thd, share, "", 0, tmpname, tmpname);
4122
share->blob_field= blob_field;
4123
share->blob_ptr_size= portable_sizeof_char_ptr;
4124
share->db_low_byte_first=1; // True for HEAP and MyISAM
4125
share->table_charset= param->table_charset;
4126
share->primary_key= MAX_KEY; // Indicate no primary key
4127
share->keys_for_keyread.init();
4128
share->keys_in_use.init();
4130
/* Calculate which type of fields we will store in the temporary table */
4132
reclength= string_total_length= 0;
4133
blob_count= string_count= null_count= hidden_null_count= group_null_items= 0;
4134
param->using_indirect_summary_function=0;
4136
List_iterator_fast<Item> li(fields);
4138
Field **tmp_from_field=from_field;
4141
Item::Type type=item->type();
4142
if (not_all_columns)
4144
if (item->with_sum_func && type != Item::SUM_FUNC_ITEM)
4146
if (item->used_tables() & OUTER_REF_TABLE_BIT)
4147
item->update_used_tables();
4148
if (type == Item::SUBSELECT_ITEM ||
4149
(item->used_tables() & ~OUTER_REF_TABLE_BIT))
4152
Mark that the we have ignored an item that refers to a summary
4153
function. We need to know this if someone is going to use
4154
DISTINCT on the result.
4156
param->using_indirect_summary_function=1;
4160
if (item->const_item() && (int) hidden_field_count <= 0)
4161
continue; // We don't have to store this
4163
if (type == Item::SUM_FUNC_ITEM && !group && !save_sum_fields)
4164
{ /* Can't calc group yet */
4165
((Item_sum*) item)->result_field=0;
4166
for (i=0 ; i < ((Item_sum*) item)->arg_count ; i++)
4168
Item **argp= ((Item_sum*) item)->args + i;
4170
if (!arg->const_item())
4173
create_tmp_field(thd, table, arg, arg->type(), ©_func,
4174
tmp_from_field, &default_field[fieldnr],
4175
group != 0,not_all_columns,
4177
param->convert_blob_length);
4179
goto err; // Should be OOM
4181
reclength+=new_field->pack_length();
4182
if (new_field->flags & BLOB_FLAG)
4184
*blob_field++= fieldnr;
4187
*(reg_field++)= new_field;
4188
if (new_field->real_type() == DRIZZLE_TYPE_VARCHAR)
4191
string_total_length+= new_field->pack_length();
4193
thd->mem_root= mem_root_save;
4194
thd->change_item_tree(argp, new Item_field(new_field));
4195
thd->mem_root= &table->mem_root;
4196
if (!(new_field->flags & NOT_NULL_FLAG))
4200
new_field->maybe_null() is still false, it will be
4201
changed below. But we have to setup Item_field correctly
4203
(*argp)->maybe_null=1;
4205
new_field->field_index= fieldnr++;
4212
The last parameter to create_tmp_field() is a bit tricky:
4214
We need to set it to 0 in union, to get fill_record() to modify the
4216
We need to set it to 1 on multi-table-update and in select to
4217
write rows to the temporary table.
4218
We here distinguish between UNION and multi-table-updates by the fact
4219
that in the later case group is set to the row pointer.
4221
Field *new_field= (param->schema_table) ?
4222
create_tmp_field_for_schema(thd, item, table) :
4223
create_tmp_field(thd, table, item, type, ©_func,
4224
tmp_from_field, &default_field[fieldnr],
4226
!force_copy_fields &&
4227
(not_all_columns || group !=0),
4229
If item->marker == 4 then we force create_tmp_field
4230
to create a 64-bit longs for BIT fields because HEAP
4231
tables can't index BIT fields directly. We do the same
4232
for distinct, as we want the distinct index to be
4233
usable in this case too.
4235
item->marker == 4 || param->bit_fields_as_long,
4237
param->convert_blob_length);
4241
if (thd->is_fatal_error)
4242
goto err; // Got OOM
4243
continue; // Some kindf of const item
4245
if (type == Item::SUM_FUNC_ITEM)
4246
((Item_sum *) item)->result_field= new_field;
4248
reclength+=new_field->pack_length();
4249
if (!(new_field->flags & NOT_NULL_FLAG))
4251
if (new_field->flags & BLOB_FLAG)
4253
*blob_field++= fieldnr;
4256
if (item->marker == 4 && item->maybe_null)
4259
new_field->flags|= GROUP_FLAG;
4261
new_field->field_index= fieldnr++;
4262
*(reg_field++)= new_field;
4264
if (!--hidden_field_count)
4267
This was the last hidden field; Remember how many hidden fields could
4270
hidden_null_count=null_count;
4272
We need to update hidden_field_count as we may have stored group
4273
functions with constant arguments
4275
param->hidden_field_count= fieldnr;
4279
assert(fieldnr == (uint) (reg_field - table->field));
4280
assert(field_count >= (uint) (reg_field - table->field));
4281
field_count= fieldnr;
4283
*blob_field= 0; // End marker
4284
share->fields= field_count;
4286
/* If result table is small; use a heap */
4287
/* future: storage engine selection can be made dynamic? */
4288
if (blob_count || using_unique_constraint ||
4289
(select_options & (OPTION_BIG_TABLES | SELECT_SMALL_RESULT)) ==
4290
OPTION_BIG_TABLES || (select_options & TMP_TABLE_FORCE_MYISAM))
4292
share->db_plugin= ha_lock_engine(0, myisam_hton);
4293
table->file= get_new_handler(share, &table->mem_root,
4296
(param->group_parts > table->file->max_key_parts() ||
4297
param->group_length > table->file->max_key_length()))
4298
using_unique_constraint=1;
4302
share->db_plugin= ha_lock_engine(0, heap_hton);
4303
table->file= get_new_handler(share, &table->mem_root,
4310
if (!using_unique_constraint)
4311
reclength+= group_null_items; // null flag is stored separately
4313
share->blob_fields= blob_count;
4314
if (blob_count == 0)
4316
/* We need to ensure that first byte is not 0 for the delete link */
4317
if (param->hidden_field_count)
4318
hidden_null_count++;
4322
hidden_null_pack_length=(hidden_null_count+7)/8;
4323
null_pack_length= (hidden_null_pack_length +
4324
(null_count + total_uneven_bit_length + 7) / 8);
4325
reclength+=null_pack_length;
4327
reclength=1; // Dummy select
4328
/* Use packed rows if there is blobs or a lot of space to gain */
4329
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)))
4332
share->reclength= reclength;
4334
uint32_t alloc_length=ALIGN_SIZE(reclength+MI_UNIQUE_HASH_LENGTH+1);
4335
share->rec_buff_length= alloc_length;
4336
if (!(table->record[0]= (unsigned char*)
4337
alloc_root(&table->mem_root, alloc_length*3)))
4339
table->record[1]= table->record[0]+alloc_length;
4340
share->default_values= table->record[1]+alloc_length;
4342
copy_func[0]=0; // End marker
4343
param->func_count= copy_func - param->items_to_copy;
4345
table->setup_tmp_table_column_bitmaps(bitmaps);
4347
recinfo=param->start_recinfo;
4348
null_flags=(unsigned char*) table->record[0];
4349
pos=table->record[0]+ null_pack_length;
4350
if (null_pack_length)
4352
memset(recinfo, 0, sizeof(*recinfo));
4353
recinfo->type=FIELD_NORMAL;
4354
recinfo->length=null_pack_length;
4356
memset(null_flags, 255, null_pack_length); // Set null fields
4358
table->null_flags= (unsigned char*) table->record[0];
4359
share->null_fields= null_count+ hidden_null_count;
4360
share->null_bytes= null_pack_length;
4362
null_count= (blob_count == 0) ? 1 : 0;
4363
hidden_field_count=param->hidden_field_count;
4364
for (i=0,reg_field=table->field; i < field_count; i++,reg_field++,recinfo++)
4366
Field *field= *reg_field;
4368
memset(recinfo, 0, sizeof(*recinfo));
4370
if (!(field->flags & NOT_NULL_FLAG))
4372
if (field->flags & GROUP_FLAG && !using_unique_constraint)
4375
We have to reserve one byte here for NULL bits,
4376
as this is updated by 'end_update()'
4378
*pos++=0; // Null is stored here
4380
recinfo->type=FIELD_NORMAL;
4382
memset(recinfo, 0, sizeof(*recinfo));
4386
recinfo->null_bit= 1 << (null_count & 7);
4387
recinfo->null_pos= null_count/8;
4389
field->move_field(pos,null_flags+null_count/8,
4390
1 << (null_count & 7));
4394
field->move_field(pos,(unsigned char*) 0,0);
4398
Test if there is a default field value. The test for ->ptr is to skip
4399
'offset' fields generated by initalize_tables
4401
if (default_field[i] && default_field[i]->ptr)
4404
default_field[i] is set only in the cases when 'field' can
4405
inherit the default value that is defined for the field referred
4406
by the Item_field object from which 'field' has been created.
4409
Field *orig_field= default_field[i];
4410
/* Get the value from default_values */
4411
diff= (my_ptrdiff_t) (orig_field->table->s->default_values-
4412
orig_field->table->record[0]);
4413
orig_field->move_field_offset(diff); // Points now at default_values
4414
if (orig_field->is_real_null())
4418
field->set_notnull();
4419
memcpy(field->ptr, orig_field->ptr, field->pack_length());
4421
orig_field->move_field_offset(-diff); // Back to record[0]
4425
{ /* Not a table Item */
4426
copy->set(field,from_field[i],save_sum_fields);
4429
length=field->pack_length();
4432
/* Make entry for create table */
4433
recinfo->length=length;
4434
if (field->flags & BLOB_FLAG)
4435
recinfo->type= (int) FIELD_BLOB;
4437
recinfo->type=FIELD_NORMAL;
4438
if (!--hidden_field_count)
4439
null_count=(null_count+7) & ~7; // move to next byte
4441
// fix table name in field entry
4442
field->table_name= &table->alias;
4445
param->copy_field_end=copy;
4446
param->recinfo=recinfo;
4447
store_record(table,s->default_values); // Make empty default record
4449
if (thd->variables.tmp_table_size == ~ (uint64_t) 0) // No limit
4450
share->max_rows= ~(ha_rows) 0;
4452
share->max_rows= (ha_rows) (((share->db_type() == heap_hton) ?
4453
cmin(thd->variables.tmp_table_size,
4454
thd->variables.max_heap_table_size) :
4455
thd->variables.tmp_table_size) /
4457
set_if_bigger(share->max_rows,1); // For dummy start options
4459
Push the LIMIT clause to the temporary table creation, so that we
4460
materialize only up to 'rows_limit' records instead of all result records.
4462
set_if_smaller(share->max_rows, rows_limit);
4463
param->end_write_records= rows_limit;
4465
keyinfo= param->keyinfo;
4469
table->group=group; /* Table is grouped by key */
4470
param->group_buff=group_buff;
4472
share->uniques= test(using_unique_constraint);
4473
table->key_info=keyinfo;
4474
keyinfo->key_part=key_part_info;
4475
keyinfo->flags=HA_NOSAME;
4476
keyinfo->usable_key_parts=keyinfo->key_parts= param->group_parts;
4477
keyinfo->key_length=0;
4478
keyinfo->rec_per_key=0;
4479
keyinfo->algorithm= HA_KEY_ALG_UNDEF;
4480
keyinfo->name= (char*) "group_key";
4481
order_st *cur_group= group;
4482
for (; cur_group ; cur_group= cur_group->next, key_part_info++)
4484
Field *field=(*cur_group->item)->get_tmp_table_field();
4485
bool maybe_null=(*cur_group->item)->maybe_null;
4486
key_part_info->null_bit=0;
4487
key_part_info->field= field;
4488
key_part_info->offset= field->offset(table->record[0]);
4489
key_part_info->length= (uint16_t) field->key_length();
4490
key_part_info->type= (uint8_t) field->key_type();
4491
key_part_info->key_type =
4492
((ha_base_keytype) key_part_info->type == HA_KEYTYPE_TEXT ||
4493
(ha_base_keytype) key_part_info->type == HA_KEYTYPE_VARTEXT1 ||
4494
(ha_base_keytype) key_part_info->type == HA_KEYTYPE_VARTEXT2) ?
4495
0 : FIELDFLAG_BINARY;
4496
if (!using_unique_constraint)
4498
cur_group->buff=(char*) group_buff;
4499
if (!(cur_group->field= field->new_key_field(thd->mem_root,table,
4504
goto err; /* purecov: inspected */
4508
To be able to group on NULL, we reserved place in group_buff
4509
for the NULL flag just before the column. (see above).
4510
The field data is after this flag.
4511
The NULL flag is updated in 'end_update()' and 'end_write()'
4513
keyinfo->flags|= HA_NULL_ARE_EQUAL; // def. that NULL == NULL
4514
key_part_info->null_bit=field->null_bit;
4515
key_part_info->null_offset= (uint) (field->null_ptr -
4516
(unsigned char*) table->record[0]);
4517
cur_group->buff++; // Pointer to field data
4518
group_buff++; // Skipp null flag
4520
/* In GROUP BY 'a' and 'a ' are equal for VARCHAR fields */
4521
key_part_info->key_part_flag|= HA_END_SPACE_ARE_EQUAL;
4522
group_buff+= cur_group->field->pack_length();
4524
keyinfo->key_length+= key_part_info->length;
4528
if (distinct && field_count != param->hidden_field_count)
4531
Create an unique key or an unique constraint over all columns
4532
that should be in the result. In the temporary table, there are
4533
'param->hidden_field_count' extra columns, whose null bits are stored
4534
in the first 'hidden_null_pack_length' bytes of the row.
4539
Special mode for index creation in MyISAM used to support unique
4540
indexes on blobs with arbitrary length. Such indexes cannot be
4545
null_pack_length-=hidden_null_pack_length;
4546
keyinfo->key_parts= ((field_count-param->hidden_field_count)+
4547
(share->uniques ? test(null_pack_length) : 0));
4550
if (!(key_part_info= (KEY_PART_INFO*)
4551
alloc_root(&table->mem_root,
4552
keyinfo->key_parts * sizeof(KEY_PART_INFO))))
4554
memset(key_part_info, 0, keyinfo->key_parts * sizeof(KEY_PART_INFO));
4555
table->key_info=keyinfo;
4556
keyinfo->key_part=key_part_info;
4557
keyinfo->flags=HA_NOSAME | HA_NULL_ARE_EQUAL;
4558
keyinfo->key_length=(uint16_t) reclength;
4559
keyinfo->name= (char*) "distinct_key";
4560
keyinfo->algorithm= HA_KEY_ALG_UNDEF;
4561
keyinfo->rec_per_key=0;
4564
Create an extra field to hold NULL bits so that unique indexes on
4565
blobs can distinguish NULL from 0. This extra field is not needed
4566
when we do not use UNIQUE indexes for blobs.
4568
if (null_pack_length && share->uniques)
4570
key_part_info->null_bit=0;
4571
key_part_info->offset=hidden_null_pack_length;
4572
key_part_info->length=null_pack_length;
4573
key_part_info->field= new Field_varstring(table->record[0],
4574
(uint32_t) key_part_info->length,
4582
if (!key_part_info->field)
4584
key_part_info->field->init(table);
4585
key_part_info->key_type=FIELDFLAG_BINARY;
4586
key_part_info->type= HA_KEYTYPE_BINARY;
4589
/* Create a distinct key over the columns we are going to return */
4590
for (i=param->hidden_field_count, reg_field=table->field + i ;
4592
i++, reg_field++, key_part_info++)
4594
key_part_info->null_bit=0;
4595
key_part_info->field= *reg_field;
4596
key_part_info->offset= (*reg_field)->offset(table->record[0]);
4597
key_part_info->length= (uint16_t) (*reg_field)->pack_length();
4599
The below method of computing the key format length of the
4600
key part is a copy/paste from opt_range.cc, and table.cc.
4601
This should be factored out, e.g. as a method of Field.
4602
In addition it is not clear if any of the Field::*_length
4603
methods is supposed to compute the same length. If so, it
4606
key_part_info->store_length= key_part_info->length;
4608
if ((*reg_field)->real_maybe_null())
4609
key_part_info->store_length+= HA_KEY_NULL_LENGTH;
4610
if ((*reg_field)->type() == DRIZZLE_TYPE_BLOB ||
4611
(*reg_field)->real_type() == DRIZZLE_TYPE_VARCHAR)
4612
key_part_info->store_length+= HA_KEY_BLOB_LENGTH;
4614
key_part_info->type= (uint8_t) (*reg_field)->key_type();
4615
key_part_info->key_type =
4616
((ha_base_keytype) key_part_info->type == HA_KEYTYPE_TEXT ||
4617
(ha_base_keytype) key_part_info->type == HA_KEYTYPE_VARTEXT1 ||
4618
(ha_base_keytype) key_part_info->type == HA_KEYTYPE_VARTEXT2) ?
4619
0 : FIELDFLAG_BINARY;
4623
if (thd->is_fatal_error) // If end of memory
4624
goto err; /* purecov: inspected */
4625
share->db_record_offset= 1;
4626
if (share->db_type() == myisam_hton)
4628
if (table->create_myisam_tmp_table(param->keyinfo, param->start_recinfo,
4629
¶m->recinfo, select_options))
4632
if (table->open_tmp_table())
4635
thd->mem_root= mem_root_save;
4640
thd->mem_root= mem_root_save;
4641
table->free_tmp_table(thd); /* purecov: inspected */
4642
if (temp_pool_slot != MY_BIT_NONE)
4643
bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
4644
return(NULL); /* purecov: inspected */
4647
/****************************************************************************/
4650
Create a reduced Table object with properly set up Field list from a
4651
list of field definitions.
4653
The created table doesn't have a table handler associated with
4654
it, has no keys, no group/distinct, no copy_funcs array.
4655
The sole purpose of this Table object is to use the power of Field
4656
class to read/write data to/from table->record[0]. Then one can store
4657
the record in any container (RB tree, hash, etc).
4658
The table is created in THD mem_root, so are the table's fields.
4659
Consequently, if you don't BLOB fields, you don't need to free it.
4661
@param thd connection handle
4662
@param field_list list of column definitions
4665
0 if out of memory, Table object in case of success
4668
Table *create_virtual_tmp_table(THD *thd, List<Create_field> &field_list)
4670
uint32_t field_count= field_list.elements;
4671
uint32_t blob_count= 0;
4673
Create_field *cdef; /* column definition */
4674
uint32_t record_length= 0;
4675
uint32_t null_count= 0; /* number of columns which may be null */
4676
uint32_t null_pack_length; /* NULL representation array length */
4677
uint32_t *blob_field;
4678
unsigned char *bitmaps;
4682
if (!multi_alloc_root(thd->mem_root,
4683
&table, sizeof(*table),
4684
&share, sizeof(*share),
4685
&field, (field_count + 1) * sizeof(Field*),
4686
&blob_field, (field_count+1) *sizeof(uint),
4687
&bitmaps, bitmap_buffer_size(field_count)*2,
4691
memset(table, 0, sizeof(*table));
4692
memset(share, 0, sizeof(*share));
4693
table->field= field;
4695
share->blob_field= blob_field;
4696
share->fields= field_count;
4697
share->blob_ptr_size= portable_sizeof_char_ptr;
4698
table->setup_tmp_table_column_bitmaps(bitmaps);
4700
/* Create all fields and calculate the total length of record */
4701
List_iterator_fast<Create_field> it(field_list);
4702
while ((cdef= it++))
4704
*field= make_field(share, 0, cdef->length,
4705
(unsigned char*) (f_maybe_null(cdef->pack_flag) ? "" : 0),
4706
f_maybe_null(cdef->pack_flag) ? 1 : 0,
4707
cdef->pack_flag, cdef->sql_type, cdef->charset,
4709
cdef->interval, cdef->field_name);
4712
(*field)->init(table);
4713
record_length+= (*field)->pack_length();
4714
if (! ((*field)->flags & NOT_NULL_FLAG))
4717
if ((*field)->flags & BLOB_FLAG)
4718
share->blob_field[blob_count++]= (uint) (field - table->field);
4722
*field= NULL; /* mark the end of the list */
4723
share->blob_field[blob_count]= 0; /* mark the end of the list */
4724
share->blob_fields= blob_count;
4726
null_pack_length= (null_count + 7)/8;
4727
share->reclength= record_length + null_pack_length;
4728
share->rec_buff_length= ALIGN_SIZE(share->reclength + 1);
4729
table->record[0]= (unsigned char*) thd->alloc(share->rec_buff_length);
4730
if (!table->record[0])
4733
if (null_pack_length)
4735
table->null_flags= (unsigned char*) table->record[0];
4736
share->null_fields= null_count;
4737
share->null_bytes= null_pack_length;
4740
table->in_use= thd; /* field->reset() may access table->in_use */
4742
/* Set up field pointers */
4743
unsigned char *null_pos= table->record[0];
4744
unsigned char *field_pos= null_pos + share->null_bytes;
4745
uint32_t null_bit= 1;
4747
for (field= table->field; *field; ++field)
4749
Field *cur_field= *field;
4750
if ((cur_field->flags & NOT_NULL_FLAG))
4751
cur_field->move_field(field_pos);
4754
cur_field->move_field(field_pos, (unsigned char*) null_pos, null_bit);
4756
if (null_bit == (1 << 8))
4764
field_pos+= cur_field->pack_length();
4769
for (field= table->field; *field; ++field)
4770
delete *field; /* just invokes field destructor */
4775
bool Table::open_tmp_table()
4778
if ((error=file->ha_open(this, s->table_name.str,O_RDWR,
4779
HA_OPEN_TMP_TABLE | HA_OPEN_INTERNAL_TABLE)))
4781
file->print_error(error,MYF(0)); /* purecov: inspected */
4785
(void) file->extra(HA_EXTRA_QUICK); /* Faster */
4791
Create MyISAM temporary table
4794
create_myisam_tmp_table()
4795
keyinfo Description of the index (there is always one index)
4796
start_recinfo MyISAM's column descriptions
4797
recinfo INOUT End of MyISAM's column descriptions
4801
Create a MyISAM temporary table according to passed description. The is
4802
assumed to have one unique index or constraint.
4804
The passed array or MI_COLUMNDEF structures must have this form:
4806
1. 1-byte column (afaiu for 'deleted' flag) (note maybe not 1-byte
4807
when there are many nullable columns)
4809
3. One free MI_COLUMNDEF element (*recinfo points here)
4811
This function may use the free element to create hash column for unique
4819
bool Table::create_myisam_tmp_table(KEY *keyinfo,
4820
MI_COLUMNDEF *start_recinfo,
4821
MI_COLUMNDEF **recinfo,
4826
MI_UNIQUEDEF uniquedef;
4827
TABLE_SHARE *share= s;
4830
{ // Get keys for ni_create
4831
bool using_unique_constraint=0;
4832
HA_KEYSEG *seg= (HA_KEYSEG*) alloc_root(&this->mem_root,
4833
sizeof(*seg) * keyinfo->key_parts);
4837
memset(seg, 0, sizeof(*seg) * keyinfo->key_parts);
4838
if (keyinfo->key_length >= file->max_key_length() ||
4839
keyinfo->key_parts > file->max_key_parts() ||
4842
/* Can't create a key; Make a unique constraint instead of a key */
4845
using_unique_constraint=1;
4846
memset(&uniquedef, 0, sizeof(uniquedef));
4847
uniquedef.keysegs=keyinfo->key_parts;
4849
uniquedef.null_are_equal=1;
4851
/* Create extra column for hash value */
4852
memset(*recinfo, 0, sizeof(**recinfo));
4853
(*recinfo)->type= FIELD_CHECK;
4854
(*recinfo)->length=MI_UNIQUE_HASH_LENGTH;
4856
share->reclength+=MI_UNIQUE_HASH_LENGTH;
4860
/* Create an unique key */
4861
memset(&keydef, 0, sizeof(keydef));
4862
keydef.flag=HA_NOSAME | HA_BINARY_PACK_KEY | HA_PACK_KEY;
4863
keydef.keysegs= keyinfo->key_parts;
4866
for (uint32_t i=0; i < keyinfo->key_parts ; i++,seg++)
4868
Field *field=keyinfo->key_part[i].field;
4870
seg->language= field->charset()->number;
4871
seg->length= keyinfo->key_part[i].length;
4872
seg->start= keyinfo->key_part[i].offset;
4873
if (field->flags & BLOB_FLAG)
4876
((keyinfo->key_part[i].key_type & FIELDFLAG_BINARY) ?
4877
HA_KEYTYPE_VARBINARY2 : HA_KEYTYPE_VARTEXT2);
4878
seg->bit_start= (uint8_t)(field->pack_length() - share->blob_ptr_size);
4879
seg->flag= HA_BLOB_PART;
4880
seg->length=0; // Whole blob in unique constraint
4884
seg->type= keyinfo->key_part[i].type;
4886
if (!(field->flags & NOT_NULL_FLAG))
4888
seg->null_bit= field->null_bit;
4889
seg->null_pos= (uint) (field->null_ptr - (unsigned char*) record[0]);
4891
We are using a GROUP BY on something that contains NULL
4892
In this case we have to tell MyISAM that two NULL should
4893
on INSERT be regarded at the same value
4895
if (!using_unique_constraint)
4896
keydef.flag|= HA_NULL_ARE_EQUAL;
4900
MI_CREATE_INFO create_info;
4901
memset(&create_info, 0, sizeof(create_info));
4903
if ((options & (OPTION_BIG_TABLES | SELECT_SMALL_RESULT)) ==
4905
create_info.data_file_length= ~(uint64_t) 0;
4907
if ((error=mi_create(share->table_name.str, share->keys, &keydef,
4908
(uint) (*recinfo-start_recinfo),
4910
share->uniques, &uniquedef,
4912
HA_CREATE_TMP_TABLE)))
4914
file->print_error(error,MYF(0)); /* purecov: inspected */
4918
status_var_increment(in_use->status_var.created_tmp_disk_tables);
4919
share->db_record_offset= 1;
4926
void Table::free_tmp_table(THD *thd)
4928
MEM_ROOT own_root= mem_root;
4929
const char *save_proc_info;
4931
save_proc_info=thd->get_proc_info();
4932
thd->set_proc_info("removing tmp table");
4937
file->ha_drop_table(s->table_name.str);
4939
file->ha_delete_table(s->table_name.str);
4944
for (Field **ptr= field ; *ptr ; ptr++)
4946
free_io_cache(this);
4948
if (temp_pool_slot != MY_BIT_NONE)
4949
bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
4951
plugin_unlock(0, s->db_plugin);
4953
free_root(&own_root, MYF(0)); /* the table is allocated in its own root */
4954
thd->set_proc_info(save_proc_info);
4960
If a HEAP table gets full, create a MyISAM table and copy all rows
4964
bool create_myisam_from_heap(THD *thd, Table *table,
4965
MI_COLUMNDEF *start_recinfo,
4966
MI_COLUMNDEF **recinfo,
4967
int error, bool ignore_last_dupp_key_error)
4971
const char *save_proc_info;
4974
if (table->s->db_type() != heap_hton ||
4975
error != HA_ERR_RECORD_FILE_FULL)
4977
table->file->print_error(error,MYF(0));
4982
new_table.s= &share;
4983
new_table.s->db_plugin= ha_lock_engine(thd, myisam_hton);
4984
if (!(new_table.file= get_new_handler(&share, &new_table.mem_root,
4985
new_table.s->db_type())))
4986
return(1); // End of memory
4988
save_proc_info=thd->get_proc_info();
4989
thd->set_proc_info("converting HEAP to MyISAM");
4991
if (new_table.create_myisam_tmp_table(table->key_info, start_recinfo,
4992
recinfo, thd->lex->select_lex.options |
4995
if (new_table.open_tmp_table())
4997
if (table->file->indexes_are_disabled())
4998
new_table.file->ha_disable_indexes(HA_KEY_SWITCH_ALL);
4999
table->file->ha_index_or_rnd_end();
5000
table->file->ha_rnd_init(1);
5003
new_table.file->extra(HA_EXTRA_NO_ROWS);
5004
new_table.no_rows=1;
5007
#ifdef TO_BE_DONE_LATER_IN_4_1
5009
To use start_bulk_insert() (which is new in 4.1) we need to find
5010
all places where a corresponding end_bulk_insert() should be put.
5012
table->file->info(HA_STATUS_VARIABLE); /* update table->file->stats.records */
5013
new_table.file->ha_start_bulk_insert(table->file->stats.records);
5015
/* HA_EXTRA_WRITE_CACHE can stay until close, no need to disable it */
5016
new_table.file->extra(HA_EXTRA_WRITE_CACHE);
5020
copy all old rows from heap table to MyISAM table
5021
This is the only code that uses record[1] to read/write but this
5022
is safe as this is a temporary MyISAM table without timestamp/autoincrement.
5024
while (!table->file->rnd_next(new_table.record[1]))
5026
write_err= new_table.file->ha_write_row(new_table.record[1]);
5030
/* copy row that filled HEAP table */
5031
if ((write_err=new_table.file->ha_write_row(table->record[0])))
5033
if (new_table.file->is_fatal_error(write_err, HA_CHECK_DUP) ||
5034
!ignore_last_dupp_key_error)
5038
/* remove heap table and change to use myisam table */
5039
(void) table->file->ha_rnd_end();
5040
(void) table->file->close(); // This deletes the table !
5043
plugin_unlock(0, table->s->db_plugin);
5044
share.db_plugin= my_plugin_lock(0, &share.db_plugin);
5045
new_table.s= table->s; // Keep old share
5049
table->file->change_table_ptr(table, table->s);
5050
table->use_all_columns();
5053
const char *new_proc_info=
5054
(!strcmp(save_proc_info,"Copying to tmp table") ?
5055
"Copying to tmp table on disk" : save_proc_info);
5056
thd->set_proc_info(new_proc_info);
5061
table->file->print_error(write_err, MYF(0));
5062
(void) table->file->ha_rnd_end();
5063
(void) new_table.file->close();
5065
new_table.file->ha_delete_table(new_table.s->table_name.str);
5067
delete new_table.file;
5068
thd->set_proc_info(save_proc_info);
5069
table->mem_root= new_table.mem_root;
5073
my_bitmap_map *Table::use_all_columns(MY_BITMAP *bitmap)
5075
my_bitmap_map *old= bitmap->bitmap;
5076
bitmap->bitmap= s->all_set.bitmap;
5080
void Table::restore_column_map(my_bitmap_map *old)
5082
read_set->bitmap= old;
5085
uint32_t Table::find_shortest_key(const key_map *usable_keys)
5087
uint32_t min_length= UINT32_MAX;
5088
uint32_t best= MAX_KEY;
5089
if (!usable_keys->is_clear_all())
5091
for (uint32_t nr=0; nr < s->keys ; nr++)
5093
if (usable_keys->is_set(nr))
5095
if (key_info[nr].key_length < min_length)
5097
min_length= key_info[nr].key_length;
5106
/*****************************************************************************
5107
Remove duplicates from tmp table
5108
This should be recoded to add a unique index to the table and remove
5110
Table is a locked single thread table
5111
fields is the number of fields to check (from the end)
5112
*****************************************************************************/
5114
bool Table::compare_record(Field **ptr)
5116
for (; *ptr ; ptr++)
5118
if ((*ptr)->cmp_offset(s->rec_buff_length))
5124
/* Return false if row hasn't changed */
5126
bool Table::compare_record()
5128
if (s->blob_fields + s->varchar_fields == 0)
5129
return cmp_record(this, record[1]);
5130
/* Compare null bits */
5131
if (memcmp(null_flags,
5132
null_flags + s->rec_buff_length,
5134
return true; // Diff in NULL value
5135
/* Compare updated fields */
5136
for (Field **ptr= field ; *ptr ; ptr++)
5138
if (bitmap_is_set(write_set, (*ptr)->field_index) &&
5139
(*ptr)->cmp_binary_offset(s->rec_buff_length))
5149
/*****************************************************************************
5150
The different ways to read a record
5151
Returns -1 if row was not found, 0 if row was found and 1 on errors
5152
*****************************************************************************/
5154
/** Help function when we get some an error from the table handler. */
5156
int Table::report_error(int error)
5158
if (error == HA_ERR_END_OF_FILE || error == HA_ERR_KEY_NOT_FOUND)
5160
status= STATUS_GARBAGE;
5161
return -1; // key not found; ok
5164
Locking reads can legally return also these errors, do not
5165
print them to the .err log
5167
if (error != HA_ERR_LOCK_DEADLOCK && error != HA_ERR_LOCK_WAIT_TIMEOUT)
5168
sql_print_error(_("Got error %d when reading table '%s'"),
5169
error, s->path.str);
5170
file->print_error(error,MYF(0));
5177
Calculate data for each virtual field marked for write in the
5178
corresponding column map.
5181
update_virtual_fields_marked_for_write()
5182
table The Table object
5183
ignore_stored Indication whether physically stored virtual
5184
fields do not need updating.
5185
This value is false when during INSERT and UPDATE
5186
and true in all other cases.
5190
>0 - Error occurred during the generation/calculation of a virtual field value
5194
int update_virtual_fields_marked_for_write(Table *table,
5197
Field **vfield_ptr, *vfield;
5199
if ((not table) or (not table->vfield))
5202
/* Iterate over virtual fields in the table */
5203
for (vfield_ptr= table->vfield; *vfield_ptr; vfield_ptr++)
5205
vfield= (*vfield_ptr);
5206
assert(vfield->vcol_info && vfield->vcol_info->expr_item);
5208
Only update those fields that are marked in the write_set bitmap
5209
and not _already_ physically stored in the database.
5211
if (bitmap_is_set(table->write_set, vfield->field_index) &&
5212
(not (ignore_stored && vfield->is_stored))
5215
/* Generate the actual value of the virtual fields */
5216
error= vfield->vcol_info->expr_item->save_in_field(vfield, 0);
3684
return(FRMTYPE_TABLE); // Is probably a .frm table