MDEV-24726 cleanup: make_new_field_args for easy refactorings

This commit is contained in:
Aleksey Midenkov 2025-01-17 12:39:13 +03:00
parent c7d3f24381
commit b9816615d1
6 changed files with 49 additions and 53 deletions

View file

@ -2483,16 +2483,15 @@ bool Field::optimize_range(uint idx, uint part) const
} }
Field *Field::make_new_field(MEM_ROOT *root, TABLE *new_table, Field *Field::make_new_field(make_new_field_args args)
bool keep_type __attribute__((unused)))
{ {
Field *tmp; Field *tmp;
if (!(tmp= (Field*) memdup_root(root,(char*) this,size_of()))) if (!(tmp= (Field*) memdup_root(args.root, (char*) this, size_of())))
return 0; return 0;
if (tmp->table->maybe_null) if (tmp->table->maybe_null)
tmp->flags&= ~NOT_NULL_FLAG; tmp->flags&= ~NOT_NULL_FLAG;
tmp->table= new_table; tmp->table= args.new_table;
tmp->key_start.init(0); tmp->key_start.init(0);
tmp->part_of_key.init(0); tmp->part_of_key.init(0);
tmp->part_of_sortkey.init(0); tmp->part_of_sortkey.init(0);
@ -2516,7 +2515,7 @@ Field *Field::new_key_field(MEM_ROOT *root, TABLE *new_table,
uchar *new_null_ptr, uint new_null_bit) uchar *new_null_ptr, uint new_null_bit)
{ {
Field *tmp; Field *tmp;
if ((tmp= make_new_field(root, new_table, table == new_table))) if ((tmp= make_new_field({root, new_table, table == new_table})))
{ {
tmp->ptr= new_ptr; tmp->ptr= new_ptr;
tmp->null_ptr= new_null_ptr; tmp->null_ptr= new_null_ptr;
@ -2545,7 +2544,7 @@ Field *Field::create_tmp_field(MEM_ROOT *mem_root, TABLE *new_table,
{ {
Field *new_field; Field *new_field;
if ((new_field= make_new_field(mem_root, new_table, new_table == table))) if ((new_field= make_new_field({mem_root, new_table, new_table == table})))
{ {
new_field->init_for_tmp_table(this, new_table); new_field->init_for_tmp_table(this, new_table);
new_field->flags|= flags & NO_DEFAULT_VALUE_FLAG; new_field->flags|= flags & NO_DEFAULT_VALUE_FLAG;
@ -3281,19 +3280,18 @@ void Field_decimal::sql_type(String &res) const
} }
Field *Field_decimal::make_new_field(MEM_ROOT *root, TABLE *new_table, Field *Field_decimal::make_new_field(make_new_field_args args)
bool keep_type)
{ {
if (keep_type) if (args.keep_type)
return Field_real::make_new_field(root, new_table, keep_type); return Field_real::make_new_field(args);
Field *field= new (root) Field_new_decimal(NULL, field_length, Field *field= new (args.root) Field_new_decimal(NULL, field_length,
maybe_null() ? (uchar*) "" : 0, 0, maybe_null() ? (uchar*) "" : 0, 0,
NONE, &field_name, NONE, &field_name,
dec, flags & ZEROFILL_FLAG, dec, flags & ZEROFILL_FLAG,
unsigned_flag); unsigned_flag);
if (field) if (field)
field->init_for_make_new_field(new_table, orig_table); field->init_for_make_new_field(args.new_table, orig_table);
return field; return field;
} }
@ -7783,22 +7781,21 @@ uint Field_string::get_key_image(uchar *buff, uint length, const uchar *ptr_arg,
} }
Field *Field_string::make_new_field(MEM_ROOT *root, TABLE *new_table, Field *Field_string::make_new_field(make_new_field_args args)
bool keep_type)
{ {
Field *field; Field *field;
if (type() != MYSQL_TYPE_VAR_STRING || keep_type) if (type() != MYSQL_TYPE_VAR_STRING || args.keep_type)
field= Field::make_new_field(root, new_table, keep_type); field= Field::make_new_field(args);
else if ((field= new (root) Field_varstring(field_length, maybe_null(), else if ((field= new (args.root) Field_varstring(field_length, maybe_null(),
&field_name, &field_name,
new_table->s, charset()))) args.new_table->s, charset())))
{ {
/* /*
Old VARCHAR field which should be modified to a VARCHAR on copy Old VARCHAR field which should be modified to a VARCHAR on copy
This is done to ensure that ALTER TABLE will convert old VARCHAR fields This is done to ensure that ALTER TABLE will convert old VARCHAR fields
to now VARCHAR fields. to now VARCHAR fields.
*/ */
field->init_for_make_new_field(new_table, orig_table); field->init_for_make_new_field(args.new_table, orig_table);
} }
return field; return field;
} }
@ -8278,12 +8275,9 @@ int Field_varstring::cmp_binary(const uchar *a_ptr, const uchar *b_ptr,
} }
Field *Field_varstring::make_new_field(MEM_ROOT *root, TABLE *new_table, Field *Field_varstring::make_new_field(make_new_field_args args)
bool keep_type)
{ {
Field_varstring *res= (Field_varstring*) Field::make_new_field(root, Field_varstring *res= (Field_varstring*) Field::make_new_field(args);
new_table,
keep_type);
if (res) if (res)
res->length_bytes= length_bytes; res->length_bytes= length_bytes;
return res; return res;
@ -8891,13 +8885,13 @@ static struct blob_storage_check
blob_storage_check() { val.p= -1; val.b= false; } blob_storage_check() { val.p= -1; val.b= false; }
} blob_storage_check; } blob_storage_check;
#endif #endif
Field *Field_blob::make_new_field(MEM_ROOT *root, TABLE *newt, bool keep_type) Field *Field_blob::make_new_field(make_new_field_args args)
{ {
DBUG_ASSERT((intptr(newt->blob_storage) & blob_storage_check.val.p) == 0); DBUG_ASSERT((intptr(args.new_table->blob_storage) & blob_storage_check.val.p) == 0);
if (newt->group_concat) if (args.new_table->group_concat)
return new (root) Field_blob(field_length, maybe_null(), &field_name, return new (args.root) Field_blob(field_length, maybe_null(), &field_name,
charset()); charset());
return Field::make_new_field(root, newt, keep_type); return Field::make_new_field(args);
} }
@ -9400,13 +9394,11 @@ void Field_enum::sql_type(String &res) const
} }
Field *Field_enum::make_new_field(MEM_ROOT *root, TABLE *new_table, Field *Field_enum::make_new_field(make_new_field_args args)
bool keep_type)
{ {
Field_enum *res= (Field_enum*) Field::make_new_field(root, new_table, Field_enum *res= (Field_enum*) Field::make_new_field(args);
keep_type);
if (res) if (res)
res->typelib= copy_typelib(root, typelib); res->typelib= copy_typelib(args.root, typelib);
return res; return res;
} }

View file

@ -1507,8 +1507,16 @@ public:
virtual void sort_string(uchar *buff,uint length)=0; virtual void sort_string(uchar *buff,uint length)=0;
virtual bool optimize_range(uint idx, uint part) const; virtual bool optimize_range(uint idx, uint part) const;
virtual void free() {} virtual void free() {}
virtual Field *make_new_field(MEM_ROOT *root, TABLE *new_table, struct make_new_field_args
bool keep_type); {
MEM_ROOT *root;
TABLE *new_table;
bool keep_type;
make_new_field_args(MEM_ROOT *root, TABLE *new_table, bool keep_type) :
root{root}, new_table{new_table}, keep_type{keep_type} {}
};
virtual Field *make_new_field(make_new_field_args args);
virtual Field *new_key_field(MEM_ROOT *root, TABLE *new_table, virtual Field *new_key_field(MEM_ROOT *root, TABLE *new_table,
uchar *new_ptr, uint32 length, uchar *new_ptr, uint32 length,
uchar *new_null_ptr, uint new_null_bit); uchar *new_null_ptr, uint new_null_bit);
@ -2331,8 +2339,7 @@ public:
unireg_check_arg, field_name_arg, unireg_check_arg, field_name_arg,
dec_arg, zero_arg, unsigned_arg) dec_arg, zero_arg, unsigned_arg)
{} {}
Field *make_new_field(MEM_ROOT *root, TABLE *new_table, bool keep_type) Field *make_new_field(make_new_field_args args) override;
override;
const Type_handler *type_handler() const override const Type_handler *type_handler() const override
{ return &type_handler_olddecimal; } { return &type_handler_olddecimal; }
enum ha_base_keytype key_type() const override enum ha_base_keytype key_type() const override
@ -4079,8 +4086,7 @@ public:
uint max_packed_col_length(uint max_length) override; uint max_packed_col_length(uint max_length) override;
uint size_of() const override { return sizeof *this; } uint size_of() const override { return sizeof *this; }
bool has_charset() const override { return charset() != &my_charset_bin; } bool has_charset() const override { return charset() != &my_charset_bin; }
Field *make_new_field(MEM_ROOT *root, TABLE *new_table, bool keep_type) Field *make_new_field(make_new_field_args args) override;
override;
uint get_key_image(uchar *buff, uint length, uint get_key_image(uchar *buff, uint length,
const uchar *ptr_arg, imagetype type) const override; const uchar *ptr_arg, imagetype type) const override;
sql_mode_t value_depends_on_sql_mode() const override; sql_mode_t value_depends_on_sql_mode() const override;
@ -4207,8 +4213,7 @@ public:
uint size_of() const override { return sizeof *this; } uint size_of() const override { return sizeof *this; }
bool has_charset() const override bool has_charset() const override
{ return charset() == &my_charset_bin ? FALSE : TRUE; } { return charset() == &my_charset_bin ? FALSE : TRUE; }
Field *make_new_field(MEM_ROOT *root, TABLE *new_table, bool keep_type) Field *make_new_field(make_new_field_args args) override;
override;
Field *new_key_field(MEM_ROOT *root, TABLE *new_table, Field *new_key_field(MEM_ROOT *root, TABLE *new_table,
uchar *new_ptr, uint32 length, uchar *new_ptr, uint32 length,
uchar *new_null_ptr, uint new_null_bit) override; uchar *new_null_ptr, uint new_null_bit) override;
@ -4562,7 +4567,7 @@ public:
return get_key_image_itRAW(ptr_arg, buff, length); return get_key_image_itRAW(ptr_arg, buff, length);
} }
void set_key_image(const uchar *buff,uint length) override; void set_key_image(const uchar *buff,uint length) override;
Field *make_new_field(MEM_ROOT *, TABLE *new_table, bool keep_type) override; Field *make_new_field(make_new_field_args args) override;
Field *new_key_field(MEM_ROOT *root, TABLE *new_table, Field *new_key_field(MEM_ROOT *root, TABLE *new_table,
uchar *new_ptr, uint32 length, uchar *new_ptr, uint32 length,
uchar *new_null_ptr, uint new_null_bit) override; uchar *new_null_ptr, uint new_null_bit) override;
@ -4740,8 +4745,7 @@ public:
{ {
flags|=ENUM_FLAG; flags|=ENUM_FLAG;
} }
Field *make_new_field(MEM_ROOT *root, TABLE *new_table, bool keep_type) Field *make_new_field(make_new_field_args args) override;
override;
const Type_handler *type_handler() const override const Type_handler *type_handler() const override
{ return &type_handler_enum; } { return &type_handler_enum; }
enum ha_base_keytype key_type() const override; enum ha_base_keytype key_type() const override;

View file

@ -2820,7 +2820,7 @@ TABLE *Delayed_insert::get_local_table(THD* client_thd)
found_next_number_field= table->found_next_number_field; found_next_number_field= table->found_next_number_field;
for (org_field= table->field; *org_field; org_field++, field++) for (org_field= table->field; *org_field; org_field++, field++)
{ {
if (!(*field= (*org_field)->make_new_field(client_thd->mem_root, copy, 1))) if (!(*field= (*org_field)->make_new_field({client_thd->mem_root, copy, 1})))
goto error; goto error;
(*field)->unireg_check= (*org_field)->unireg_check; (*field)->unireg_check= (*org_field)->unireg_check;
(*field)->invisible= (*org_field)->invisible; (*field)->invisible= (*org_field)->invisible;

View file

@ -26168,8 +26168,8 @@ setup_copy_fields(THD *thd, TMP_TABLE_PARAM *param,
saved value saved value
*/ */
field= item->field; field= item->field;
item->result_field=field->make_new_field(thd->mem_root, item->result_field=field->make_new_field({thd->mem_root,
field->table, 1); field->table, 1});
/* /*
We need to allocate one extra byte for null handling and We need to allocate one extra byte for null handling and
another extra byte to not get warnings from purify in another extra byte to not get warnings from purify in

View file

@ -1312,8 +1312,8 @@ bool Table_triggers_list::prepare_record_accessors(TABLE *table)
if (!(*fld)->null_ptr && !(*fld)->vcol_info && !(*fld)->vers_sys_field()) if (!(*fld)->null_ptr && !(*fld)->vcol_info && !(*fld)->vers_sys_field())
{ {
Field *f; Field *f;
if (!(f= *trg_fld= (*fld)->make_new_field(&table->mem_root, table, if (!(f= *trg_fld= (*fld)->make_new_field({&table->mem_root, table,
table == (*fld)->table))) table == (*fld)->table})))
return 1; return 1;
f->flags= (*fld)->flags; f->flags= (*fld)->flags;
@ -1353,8 +1353,8 @@ bool Table_triggers_list::prepare_record_accessors(TABLE *table)
for (fld= table->field, trg_fld= record1_field; *fld; fld++, trg_fld++) for (fld= table->field, trg_fld= record1_field; *fld; fld++, trg_fld++)
{ {
if (!(*trg_fld= (*fld)->make_new_field(&table->mem_root, table, if (!(*trg_fld= (*fld)->make_new_field({&table->mem_root, table,
table == (*fld)->table))) table == (*fld)->table})))
return 1; return 1;
(*trg_fld)->move_field_offset((my_ptrdiff_t)(table->record[1] - (*trg_fld)->move_field_offset((my_ptrdiff_t)(table->record[1] -
table->record[0])); table->record[0]));

View file

@ -3997,7 +3997,7 @@ bool copy_keys_from_share(TABLE *outparam, MEM_ROOT *root)
We are using only a prefix of the column as a key: We are using only a prefix of the column as a key:
Create a new field for the key part that matches the index Create a new field for the key part that matches the index
*/ */
field= key_part->field=field->make_new_field(root, outparam, 0); field= key_part->field=field->make_new_field({root, outparam, 0});
field->field_length= key_part->length; field->field_length= key_part->length;
} }
} }