Refactor Sort_param::init_for_filesort

No logic changes.
Extract some of init_for_filesort logic into a separate function:
* Sort_param::setup_lengths_and_limit can be used to fill in the various
  xxx_length members of Sort_param, without having to allocate any of the
  other buffers.

Reviewer: Monty
This commit is contained in:
Vicențiu Ciorbaru 2022-07-02 22:43:22 +03:00 committed by Sergei Petrunia
parent 50e9f7aee5
commit b70290869e
2 changed files with 44 additions and 21 deletions

View file

@ -87,43 +87,62 @@ static uint32 read_keypart_length(const uchar *from, uint bytes)
}
// @param sortlen [Maximum] length of the sort key
void Sort_param::init_for_filesort(uint sortlen, TABLE *table,
ha_rows maxrows, Filesort *filesort)
/*
Initialize Sort_param for doing a filesort
@param table Table to sort
@param Filesort Filesort parameter to filesort()
@param sortlen [Maximum] length of the sort key
@param limit_rows Number of rows to return (may be less than rows to sort)
*/
void Sort_param::init_for_filesort(TABLE *table, Filesort *filesort,
uint sortlen, ha_rows limit_rows_arg)
{
DBUG_ASSERT(addon_fields == NULL);
sort_length= sortlen;
ref_length= table->file->ref_length;
accepted_rows= filesort->accepted_rows;
if (!(table->file->ha_table_flags() & HA_FAST_KEY_READ) &&
!table->fulltext_searched && !filesort->sort_positions)
{
/*
Get the descriptors of all fields whose values are appended
/*
Get the descriptors of all fields whose values are appended
to sorted fields and get its total length in addon_buf.length
*/
addon_fields= get_addon_fields(table, sort_length, &addon_length,
addon_fields= get_addon_fields(table, sortlen, &addon_length,
&m_packable_length);
}
if (using_addon_fields())
DBUG_ASSERT((using_addon_fields() == 0 || addon_length != 0));
setup_lengths_and_limit(table, sortlen, addon_length, limit_rows_arg);
accepted_rows= filesort->accepted_rows;
}
void Sort_param::setup_lengths_and_limit(TABLE *table,
uint sort_len_arg,
uint addon_length_arg,
ha_rows limit_rows_arg)
{
sort_form= table;
sort_length= sort_len_arg;
limit_rows= limit_rows_arg;
ref_length= table->file->ref_length;
if (addon_length_arg)
{
DBUG_ASSERT(addon_length < UINT_MAX32);
res_length= addon_length;
DBUG_ASSERT(addon_length_arg < UINT_MAX32);
res_length= addon_length_arg;
}
else
{
res_length= ref_length;
/*
The reference to the record is considered
as an additional sorted field
The reference (rowid) to the record is considered as an additional
sorted field as we want to access rows in rowid order if possible.
*/
sort_length+= ref_length;
}
rec_length= sort_length + addon_length;
limit_rows= maxrows;
sort_form= table;
rec_length= sort_length + addon_length_arg;
}

View file

@ -590,10 +590,14 @@ public:
*/
tmp_buffer.set_charset(&my_charset_bin);
}
void init_for_filesort(uint sortlen, TABLE *table,
ha_rows maxrows, Filesort *filesort);
void (*unpack)(TABLE *);
void init_for_filesort(TABLE *table, Filesort *filesort,
uint sortlen, ha_rows limit_rows_arg);
void setup_lengths_and_limit(TABLE *table,
uint sortlen,
uint addon_length,
ha_rows limit_rows_arg);
void (*unpack)(TABLE *);
/// Enables the packing of addons if possible.
void try_to_pack_addons(ulong max_length_for_sort_data);