From b9816615d1b614108fc7bddefbb97eabf98a6998 Mon Sep 17 00:00:00 2001 From: Aleksey Midenkov Date: Fri, 17 Jan 2025 12:39:13 +0300 Subject: [PATCH] MDEV-24726 cleanup: make_new_field_args for easy refactorings --- sql/field.cc | 60 ++++++++++++++++++++-------------------------- sql/field.h | 26 +++++++++++--------- sql/sql_insert.cc | 2 +- sql/sql_select.cc | 4 ++-- sql/sql_trigger.cc | 8 +++---- sql/table.cc | 2 +- 6 files changed, 49 insertions(+), 53 deletions(-) diff --git a/sql/field.cc b/sql/field.cc index 8b96c473ab6..3a2a4a3f96e 100644 --- a/sql/field.cc +++ b/sql/field.cc @@ -2483,16 +2483,15 @@ bool Field::optimize_range(uint idx, uint part) const } -Field *Field::make_new_field(MEM_ROOT *root, TABLE *new_table, - bool keep_type __attribute__((unused))) +Field *Field::make_new_field(make_new_field_args args) { 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; if (tmp->table->maybe_null) tmp->flags&= ~NOT_NULL_FLAG; - tmp->table= new_table; + tmp->table= args.new_table; tmp->key_start.init(0); tmp->part_of_key.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) { 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->null_ptr= new_null_ptr; @@ -2545,7 +2544,7 @@ Field *Field::create_tmp_field(MEM_ROOT *mem_root, TABLE *new_table, { 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->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, - bool keep_type) +Field *Field_decimal::make_new_field(make_new_field_args args) { - if (keep_type) - return Field_real::make_new_field(root, new_table, keep_type); + if (args.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, NONE, &field_name, dec, flags & ZEROFILL_FLAG, unsigned_flag); 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; } @@ -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, - bool keep_type) +Field *Field_string::make_new_field(make_new_field_args args) { Field *field; - if (type() != MYSQL_TYPE_VAR_STRING || keep_type) - field= Field::make_new_field(root, new_table, keep_type); - else if ((field= new (root) Field_varstring(field_length, maybe_null(), + if (type() != MYSQL_TYPE_VAR_STRING || args.keep_type) + field= Field::make_new_field(args); + else if ((field= new (args.root) Field_varstring(field_length, maybe_null(), &field_name, - new_table->s, charset()))) + args.new_table->s, charset()))) { /* 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 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; } @@ -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, - bool keep_type) +Field *Field_varstring::make_new_field(make_new_field_args args) { - Field_varstring *res= (Field_varstring*) Field::make_new_field(root, - new_table, - keep_type); + Field_varstring *res= (Field_varstring*) Field::make_new_field(args); if (res) res->length_bytes= length_bytes; return res; @@ -8891,13 +8885,13 @@ static struct blob_storage_check blob_storage_check() { val.p= -1; val.b= false; } } blob_storage_check; #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); - if (newt->group_concat) - return new (root) Field_blob(field_length, maybe_null(), &field_name, + DBUG_ASSERT((intptr(args.new_table->blob_storage) & blob_storage_check.val.p) == 0); + if (args.new_table->group_concat) + return new (args.root) Field_blob(field_length, maybe_null(), &field_name, 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, - bool keep_type) +Field *Field_enum::make_new_field(make_new_field_args args) { - Field_enum *res= (Field_enum*) Field::make_new_field(root, new_table, - keep_type); + Field_enum *res= (Field_enum*) Field::make_new_field(args); if (res) - res->typelib= copy_typelib(root, typelib); + res->typelib= copy_typelib(args.root, typelib); return res; } diff --git a/sql/field.h b/sql/field.h index 09b9f895273..c641d64677a 100644 --- a/sql/field.h +++ b/sql/field.h @@ -1507,8 +1507,16 @@ public: virtual void sort_string(uchar *buff,uint length)=0; virtual bool optimize_range(uint idx, uint part) const; virtual void free() {} - virtual Field *make_new_field(MEM_ROOT *root, TABLE *new_table, - bool keep_type); + struct make_new_field_args + { + 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, uchar *new_ptr, uint32 length, uchar *new_null_ptr, uint new_null_bit); @@ -2331,8 +2339,7 @@ public: unireg_check_arg, field_name_arg, dec_arg, zero_arg, unsigned_arg) {} - Field *make_new_field(MEM_ROOT *root, TABLE *new_table, bool keep_type) - override; + Field *make_new_field(make_new_field_args args) override; const Type_handler *type_handler() const override { return &type_handler_olddecimal; } enum ha_base_keytype key_type() const override @@ -4079,8 +4086,7 @@ public: uint max_packed_col_length(uint max_length) override; uint size_of() const override { return sizeof *this; } bool has_charset() const override { return charset() != &my_charset_bin; } - Field *make_new_field(MEM_ROOT *root, TABLE *new_table, bool keep_type) - override; + Field *make_new_field(make_new_field_args args) override; uint get_key_image(uchar *buff, uint length, const uchar *ptr_arg, imagetype type) 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; } bool has_charset() const override { return charset() == &my_charset_bin ? FALSE : TRUE; } - Field *make_new_field(MEM_ROOT *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, uchar *new_ptr, uint32 length, uchar *new_null_ptr, uint new_null_bit) override; @@ -4562,7 +4567,7 @@ public: return get_key_image_itRAW(ptr_arg, buff, length); } 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, uchar *new_ptr, uint32 length, uchar *new_null_ptr, uint new_null_bit) override; @@ -4740,8 +4745,7 @@ public: { flags|=ENUM_FLAG; } - Field *make_new_field(MEM_ROOT *root, TABLE *new_table, bool keep_type) - override; + Field *make_new_field(make_new_field_args args) override; const Type_handler *type_handler() const override { return &type_handler_enum; } enum ha_base_keytype key_type() const override; diff --git a/sql/sql_insert.cc b/sql/sql_insert.cc index 2a978640122..6c9b7aac053 100644 --- a/sql/sql_insert.cc +++ b/sql/sql_insert.cc @@ -2820,7 +2820,7 @@ TABLE *Delayed_insert::get_local_table(THD* client_thd) found_next_number_field= table->found_next_number_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; (*field)->unireg_check= (*org_field)->unireg_check; (*field)->invisible= (*org_field)->invisible; diff --git a/sql/sql_select.cc b/sql/sql_select.cc index 1b6686aee58..b2830a6dbe7 100644 --- a/sql/sql_select.cc +++ b/sql/sql_select.cc @@ -26168,8 +26168,8 @@ setup_copy_fields(THD *thd, TMP_TABLE_PARAM *param, saved value */ field= item->field; - item->result_field=field->make_new_field(thd->mem_root, - field->table, 1); + item->result_field=field->make_new_field({thd->mem_root, + field->table, 1}); /* We need to allocate one extra byte for null handling and another extra byte to not get warnings from purify in diff --git a/sql/sql_trigger.cc b/sql/sql_trigger.cc index f85faff37ac..3cc9a88129d 100644 --- a/sql/sql_trigger.cc +++ b/sql/sql_trigger.cc @@ -1312,8 +1312,8 @@ bool Table_triggers_list::prepare_record_accessors(TABLE *table) if (!(*fld)->null_ptr && !(*fld)->vcol_info && !(*fld)->vers_sys_field()) { Field *f; - if (!(f= *trg_fld= (*fld)->make_new_field(&table->mem_root, table, - table == (*fld)->table))) + if (!(f= *trg_fld= (*fld)->make_new_field({&table->mem_root, table, + table == (*fld)->table}))) return 1; 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++) { - if (!(*trg_fld= (*fld)->make_new_field(&table->mem_root, table, - table == (*fld)->table))) + if (!(*trg_fld= (*fld)->make_new_field({&table->mem_root, table, + table == (*fld)->table}))) return 1; (*trg_fld)->move_field_offset((my_ptrdiff_t)(table->record[1] - table->record[0])); diff --git a/sql/table.cc b/sql/table.cc index f6b2bad0f76..93c01c181ef 100644 --- a/sql/table.cc +++ b/sql/table.cc @@ -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: 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; } }