Skip to content

Commit

Permalink
Refactor Sort_param::init_for_filesort
Browse files Browse the repository at this point in the history
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
  • Loading branch information
cvicentiu authored and spetrunia committed Feb 2, 2023
1 parent 50e9f7a commit b702908
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 21 deletions.
55 changes: 37 additions & 18 deletions sql/filesort.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}


Expand Down
10 changes: 7 additions & 3 deletions sql/sql_sort.h
Original file line number Diff line number Diff line change
Expand Up @@ -590,10 +590,14 @@ class Sort_param {
*/
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);

Expand Down

0 comments on commit b702908

Please sign in to comment.