Skip to content

Commit a099686

Browse files
committed
cleanup: remove Field->stored_in_db, Create_field->stored_in_db
and don't set Create_field->sql_type to MYSQL_TYPE_VIRTUAL temporarily only to change it again few lines later.
1 parent 13989b3 commit a099686

File tree

16 files changed

+100
-124
lines changed

16 files changed

+100
-124
lines changed

sql/field.cc

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1648,9 +1648,7 @@ Field::Field(uchar *ptr_arg,uint32 length_arg,uchar *null_ptr_arg,
16481648
part_of_key_not_clustered(0), part_of_sortkey(0),
16491649
unireg_check(unireg_check_arg), field_length(length_arg),
16501650
null_bit(null_bit_arg), is_created_from_null_item(FALSE),
1651-
read_stats(NULL), collected_stats(0),
1652-
vcol_info(0),
1653-
stored_in_db(TRUE)
1651+
read_stats(NULL), collected_stats(0), vcol_info(0)
16541652
{
16551653
flags=null_ptr ? 0: NOT_NULL_FLAG;
16561654
comment.str= (char*) "";
@@ -9759,7 +9757,6 @@ void Create_field::init_for_tmp_table(enum_field_types sql_type_arg,
97599757
f_packtype(pack_flag)));
97609758
vcol_info= 0;
97619759
create_if_not_exists= FALSE;
9762-
stored_in_db= TRUE;
97639760

97649761
DBUG_VOID_RETURN;
97659762
}
@@ -9778,10 +9775,21 @@ bool Create_field::check(THD *thd)
97789775
ulong max_field_charlength= MAX_FIELD_CHARLENGTH;
97799776
DBUG_ENTER("Create_field::check");
97809777

9778+
/* Initialize data for a computed field */
97819779
if (vcol_info)
97829780
{
9781+
DBUG_ASSERT(vcol_info->expr_item);
9782+
97839783
vcol_info->set_field_type(sql_type);
9784-
sql_type= (enum enum_field_types)MYSQL_TYPE_VIRTUAL;
9784+
/*
9785+
Walk through the Item tree checking if all items are valid
9786+
to be part of the virtual column
9787+
*/
9788+
if (vcol_info->expr_item->walk(&Item::check_vcol_func_processor, 0, NULL))
9789+
{
9790+
my_error(ER_VIRTUAL_COLUMN_FUNCTION_IS_NOT_ALLOWED, MYF(0), field_name);
9791+
DBUG_RETURN(TRUE);
9792+
}
97859793
}
97869794

97879795
if (length > MAX_FIELD_BLOBLENGTH)
@@ -9857,30 +9865,6 @@ bool Create_field::check(THD *thd)
98579865
DBUG_RETURN(1);
98589866
}
98599867

9860-
/* Initialize data for a computed field */
9861-
if (sql_type == MYSQL_TYPE_VIRTUAL)
9862-
{
9863-
DBUG_ASSERT(vcol_info && vcol_info->expr_item);
9864-
stored_in_db= vcol_info->is_stored();
9865-
/*
9866-
Walk through the Item tree checking if all items are valid
9867-
to be part of the virtual column
9868-
*/
9869-
if (vcol_info->expr_item->walk(&Item::check_vcol_func_processor, 0, NULL))
9870-
{
9871-
my_error(ER_VIRTUAL_COLUMN_FUNCTION_IS_NOT_ALLOWED, MYF(0), field_name);
9872-
DBUG_RETURN(TRUE);
9873-
}
9874-
9875-
/*
9876-
Make a field created for the real type.
9877-
Note that regular and computed fields differ from each other only by
9878-
Field::vcol_info. It is is always NULL for a column that is not
9879-
computed.
9880-
*/
9881-
sql_type= vcol_info->get_real_type();
9882-
}
9883-
98849868
sign_len= flags & UNSIGNED_FLAG ? 0 : 1;
98859869

98869870
switch (sql_type) {
@@ -10456,7 +10440,6 @@ Create_field::Create_field(THD *thd, Field *old_field, Field *orig_field)
1045610440
decimals= old_field->decimals();
1045710441
vcol_info= old_field->vcol_info;
1045810442
create_if_not_exists= FALSE;
10459-
stored_in_db= old_field->stored_in_db;
1046010443
option_list= old_field->option_list;
1046110444
option_struct= old_field->option_struct;
1046210445

sql/field.h

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -555,20 +555,20 @@ class Virtual_column_info: public Sql_alloc
555555
when a Create_field object is created/initialized.
556556
*/
557557
enum_field_types field_type; /* Real field type*/
558-
/* Flag indicating that the field is physically stored in the database */
559-
bool stored_in_db;
560558
/* Flag indicating that the field used in a partitioning expression */
561559
bool in_partitioning_expr;
562560

563561
public:
562+
/* Flag indicating that the field is physically stored in the database */
563+
bool stored_in_db;
564564
/* The expression to compute the value of the virtual column */
565565
Item *expr_item;
566566
/* Text representation of the defining expression */
567567
LEX_STRING expr_str;
568568

569569
Virtual_column_info()
570570
: field_type((enum enum_field_types)MYSQL_TYPE_VIRTUAL),
571-
stored_in_db(FALSE), in_partitioning_expr(FALSE),
571+
in_partitioning_expr(FALSE), stored_in_db(FALSE),
572572
expr_item(NULL)
573573
{
574574
expr_str.str= NULL;
@@ -713,12 +713,6 @@ class Field: public Value_source
713713
can be computed from other fields.
714714
*/
715715
Virtual_column_info *vcol_info;
716-
/*
717-
Flag indicating that the field is physically stored in tables
718-
rather than just computed from other fields.
719-
As of now, FALSE can be set only for computed virtual columns.
720-
*/
721-
bool stored_in_db;
722716

723717
Field(uchar *ptr_arg,uint32 length_arg,uchar *null_ptr_arg,
724718
uchar null_bit_arg, utype unireg_check_arg,
@@ -1048,6 +1042,8 @@ class Field: public Value_source
10481042
null_bit= p_null_bit;
10491043
}
10501044

1045+
bool stored_in_db() const { return !vcol_info || vcol_info->stored_in_db; }
1046+
10511047
inline THD *get_thd() const
10521048
{ return likely(table) ? table->in_use : current_thd; }
10531049

@@ -3467,20 +3463,13 @@ class Create_field :public Sql_alloc
34673463
can be computed from other fields.
34683464
*/
34693465
Virtual_column_info *vcol_info;
3470-
/*
3471-
Flag indicating that the field is physically stored in tables
3472-
rather than just computed from other fields.
3473-
As of now, FALSE can be set only for computed virtual columns.
3474-
*/
3475-
bool stored_in_db;
34763466

34773467
Create_field() :change(0), after(0), comment(null_lex_str),
34783468
def(0), on_update(0), sql_type(MYSQL_TYPE_NULL),
34793469
flags(0), pack_length(0), key_length(0), interval(0),
34803470
srid(0), geom_type(Field::GEOM_GEOMETRY),
34813471
field(0), option_list(NULL), option_struct(NULL),
3482-
create_if_not_exists(false), vcol_info(0),
3483-
stored_in_db(true)
3472+
create_if_not_exists(false), vcol_info(0)
34843473
{
34853474
interval_list.empty();
34863475
}
@@ -3498,6 +3487,8 @@ class Create_field :public Sql_alloc
34983487

34993488
bool check(THD *thd);
35003489

3490+
bool stored_in_db() const { return !vcol_info || vcol_info->stored_in_db; }
3491+
35013492
ha_storage_media field_storage_type() const
35023493
{
35033494
return (ha_storage_media)

sql/sp_head.cc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -886,7 +886,6 @@ sp_head::create_result_field(uint field_max_length, const char *field_name,
886886
field_name ? field_name : (const char *) m_name.str);
887887

888888
field->vcol_info= m_return_field_def.vcol_info;
889-
field->stored_in_db= m_return_field_def.stored_in_db;
890889
if (field)
891890
field->init(table);
892891

sql/sql_base.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8789,7 +8789,7 @@ fill_record(THD *thd, TABLE *table_arg, List<Item> &fields, List<Item> &values,
87898789
ER_THD(thd, ER_WARNING_NON_DEFAULT_VALUE_FOR_VIRTUAL_COLUMN),
87908790
rfield->field_name, table->s->table_name.str);
87918791
}
8792-
if ((!rfield->vcol_info || rfield->stored_in_db) &&
8792+
if (rfield->stored_in_db() &&
87938793
(value->save_in_field(rfield, 0)) < 0 && !ignore_errors)
87948794
{
87958795
my_message(ER_UNKNOWN_ERROR, ER_THD(thd, ER_UNKNOWN_ERROR), MYF(0));

sql/sql_insert.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1501,7 +1501,7 @@ bool mysql_prepare_insert(THD *thd, TABLE_LIST *table_list,
15011501
{
15021502
for (Field **vfield_ptr= table->vfield; *vfield_ptr; vfield_ptr++)
15031503
{
1504-
if ((*vfield_ptr)->stored_in_db)
1504+
if ((*vfield_ptr)->vcol_info->stored_in_db)
15051505
{
15061506
thd->lex->unit.insert_table_with_stored_vcol= table;
15071507
break;

sql/sql_load.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ int mysql_load(THD *thd,sql_exchange *ex,TABLE_LIST *table_list,
303303
{
304304
for (Field **vfield_ptr= table->vfield; *vfield_ptr; vfield_ptr++)
305305
{
306-
if ((*vfield_ptr)->stored_in_db)
306+
if ((*vfield_ptr)->vcol_info->stored_in_db)
307307
{
308308
thd->lex->unit.insert_table_with_stored_vcol= table;
309309
break;

sql/sql_select.cc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15678,7 +15678,6 @@ Field *create_tmp_field_from_field(THD *thd, Field *org_field,
1567815678
else if (org_field->type() == FIELD_TYPE_DOUBLE)
1567915679
((Field_double *) new_field)->not_fixed= TRUE;
1568015680
new_field->vcol_info= 0;
15681-
new_field->stored_in_db= TRUE;
1568215681
new_field->cond_selectivity= 1.0;
1568315682
new_field->next_equal_field= NULL;
1568415683
new_field->option_list= NULL;

sql/sql_show.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1844,7 +1844,7 @@ int show_create_table(THD *thd, TABLE_LIST *table_list, String *packet,
18441844
field->vcol_info->expr_str.length,
18451845
system_charset_info);
18461846
packet->append(STRING_WITH_LEN(")"));
1847-
if (field->stored_in_db)
1847+
if (field->vcol_info->stored_in_db)
18481848
packet->append(STRING_WITH_LEN(" PERSISTENT"));
18491849
else
18501850
packet->append(STRING_WITH_LEN(" VIRTUAL"));
@@ -5344,7 +5344,7 @@ static int get_schema_column_record(THD *thd, TABLE_LIST *tables,
53445344
table->field[17]->store(type.ptr(), type.length(), cs);
53455345
if (field->vcol_info)
53465346
{
5347-
if (field->stored_in_db)
5347+
if (field->vcol_info->stored_in_db)
53485348
table->field[17]->store(STRING_WITH_LEN("PERSISTENT"), cs);
53495349
else
53505350
table->field[17]->store(STRING_WITH_LEN("VIRTUAL"), cs);

sql/sql_table.cc

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3466,7 +3466,6 @@ mysql_prepare_create_table(THD *thd, HA_CREATE_INFO *create_info,
34663466
sql_field->flags= dup_field->flags;
34673467
sql_field->interval= dup_field->interval;
34683468
sql_field->vcol_info= dup_field->vcol_info;
3469-
sql_field->stored_in_db= dup_field->stored_in_db;
34703469
it2.remove(); // Remove first (create) definition
34713470
select_field_pos--;
34723471
break;
@@ -3508,14 +3507,14 @@ mysql_prepare_create_table(THD *thd, HA_CREATE_INFO *create_info,
35083507
(virtual fields) and update their offset later
35093508
(see the next loop).
35103509
*/
3511-
if (sql_field->stored_in_db)
3510+
if (sql_field->stored_in_db())
35123511
record_offset+= sql_field->pack_length;
35133512
}
35143513
/* Update virtual fields' offset*/
35153514
it.rewind();
35163515
while ((sql_field=it++))
35173516
{
3518-
if (!sql_field->stored_in_db)
3517+
if (!sql_field->stored_in_db())
35193518
{
35203519
sql_field->offset= record_offset;
35213520
record_offset+= sql_field->pack_length;
@@ -3868,7 +3867,7 @@ mysql_prepare_create_table(THD *thd, HA_CREATE_INFO *create_info,
38683867
}
38693868
}
38703869
#endif
3871-
if (!sql_field->stored_in_db)
3870+
if (!sql_field->stored_in_db())
38723871
{
38733872
/* Key fields must always be physically stored. */
38743873
my_error(ER_KEY_BASED_ON_GENERATED_VIRTUAL_COLUMN, MYF(0));
@@ -6240,18 +6239,6 @@ static bool fill_alter_inplace_info(THD *thd,
62406239
ha_alter_info->handler_flags|= Alter_inplace_info::ALTER_COLUMN_TYPE;
62416240
}
62426241

6243-
/*
6244-
Check if the altered column is computed and either
6245-
is stored or is used in the partitioning expression.
6246-
TODO: Mark such a column with an alter flag only if
6247-
the defining expression has changed.
6248-
*/
6249-
if (field->vcol_info &&
6250-
(field->stored_in_db || field->vcol_info->is_in_partitioning_expr()))
6251-
{
6252-
ha_alter_info->handler_flags|= Alter_inplace_info::ALTER_COLUMN_VCOL;
6253-
}
6254-
62556242
/* Check if field was renamed */
62566243
if (my_strcasecmp(system_charset_info, field->field_name,
62576244
new_field->field_name))
@@ -6318,20 +6305,29 @@ static bool fill_alter_inplace_info(THD *thd,
63186305
new_field_it.init(alter_info->create_list);
63196306
while ((new_field= new_field_it++))
63206307
{
6321-
if (! new_field->field)
6308+
Virtual_column_info *vcol_info;
6309+
if (new_field->field)
6310+
vcol_info= new_field->field->vcol_info;
6311+
else
63226312
{
6313+
vcol_info= new_field->vcol_info;
63236314
/*
63246315
Field is not present in old version of table and therefore was added.
63256316
Again corresponding storage engine flag should be already set.
63266317
*/
63276318
DBUG_ASSERT(ha_alter_info->handler_flags & Alter_inplace_info::ADD_COLUMN);
6319+
}
63286320

6329-
if (new_field->vcol_info &&
6330-
(new_field->stored_in_db || new_field->vcol_info->is_in_partitioning_expr()))
6331-
{
6332-
ha_alter_info->handler_flags|= Alter_inplace_info::ALTER_COLUMN_VCOL;
6333-
}
6334-
break;
6321+
/*
6322+
Check if the altered column is computed and either
6323+
is stored or is used in the partitioning expression.
6324+
TODO: Mark such a column with an alter flag only if
6325+
the defining expression has changed.
6326+
*/
6327+
if (vcol_info &&
6328+
(vcol_info->stored_in_db || vcol_info->is_in_partitioning_expr()))
6329+
{
6330+
ha_alter_info->handler_flags|= Alter_inplace_info::ALTER_COLUMN_VCOL;
63356331
}
63366332
}
63376333

@@ -7394,7 +7390,7 @@ mysql_prepare_alter_table(THD *thd, TABLE *table,
73947390
of the list for now. Their positions will be corrected later.
73957391
*/
73967392
new_create_list.push_back(def, thd->mem_root);
7397-
if (field->stored_in_db != def->stored_in_db)
7393+
if (field->stored_in_db() != def->stored_in_db())
73987394
{
73997395
my_error(ER_UNSUPPORTED_ACTION_ON_VIRTUAL_COLUMN, MYF(0));
74007396
goto err;

sql/table.cc

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1675,7 +1675,10 @@ int TABLE_SHARE::init_from_binary_frm_image(THD *thd, bool write,
16751675
reg_field->field_index= i;
16761676
reg_field->comment=comment;
16771677
reg_field->vcol_info= vcol_info;
1678-
reg_field->stored_in_db= fld_stored_in_db;
1678+
if (vcol_info)
1679+
reg_field->vcol_info->stored_in_db= fld_stored_in_db;
1680+
else
1681+
DBUG_ASSERT(fld_stored_in_db == true);
16791682
if (field_type == MYSQL_TYPE_BIT && !f_bit_as_char(pack_flag))
16801683
{
16811684
null_bits_are_used= 1;
@@ -1698,7 +1701,7 @@ int TABLE_SHARE::init_from_binary_frm_image(THD *thd, bool write,
16981701

16991702
if (use_hash && my_hash_insert(&share->name_hash, (uchar*) field_ptr))
17001703
goto err;
1701-
if (!reg_field->stored_in_db)
1704+
if (!reg_field->stored_in_db())
17021705
{
17031706
share->stored_fields--;
17041707
if (share->stored_rec_length>=recpos)
@@ -2526,6 +2529,10 @@ bool unpack_vcol_info_from_frm(THD *thd,
25262529
/* From now on use vcol_info generated by the parser. */
25272530
field->vcol_info= vcol_storage.vcol_info;
25282531

2532+
/* copy the stored_in_db property, the parser doesn't generate it */
2533+
field->vcol_info->stored_in_db=
2534+
table->s->field[field->field_index]->vcol_info->stored_in_db;
2535+
25292536
/* Validate the Item tree. */
25302537
if (fix_vcol_expr(thd, table, field))
25312538
{
@@ -6169,7 +6176,7 @@ void TABLE::mark_virtual_columns_for_write(bool insert_fl)
61696176
tmp_vfield= *vfield_ptr;
61706177
if (bitmap_is_set(write_set, tmp_vfield->field_index))
61716178
bitmap_updated= mark_virtual_col(tmp_vfield);
6172-
else if (tmp_vfield->stored_in_db)
6179+
else if (tmp_vfield->vcol_info->stored_in_db)
61736180
{
61746181
bool mark_fl= insert_fl;
61756182
if (!mark_fl)
@@ -6903,13 +6910,15 @@ int update_virtual_fields(THD *thd, TABLE *table,
69036910
for (vfield_ptr= table->vfield; *vfield_ptr; vfield_ptr++)
69046911
{
69056912
vfield= (*vfield_ptr);
6906-
DBUG_ASSERT(vfield->vcol_info && vfield->vcol_info->expr_item);
6913+
Virtual_column_info *vcol_info= vfield->vcol_info;
6914+
DBUG_ASSERT(vcol_info);
6915+
DBUG_ASSERT(vcol_info->expr_item);
69076916
if ((bitmap_is_set(table->vcol_set, vfield->field_index) &&
6908-
(vcol_update_mode == VCOL_UPDATE_FOR_WRITE || !vfield->stored_in_db)) ||
6917+
(vcol_update_mode == VCOL_UPDATE_FOR_WRITE || !vcol_info->stored_in_db)) ||
69096918
vcol_update_mode == VCOL_UPDATE_ALL)
69106919
{
69116920
/* Compute the actual value of the virtual fields */
6912-
error= vfield->vcol_info->expr_item->save_in_field(vfield, 0);
6921+
error= vcol_info->expr_item->save_in_field(vfield, 0);
69136922
DBUG_PRINT("info", ("field '%s' - updated", vfield->field_name));
69146923
}
69156924
else

0 commit comments

Comments
 (0)