From b70290869e91047bd5b24d5abab9220d8215637d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vicen=C8=9Biu=20Ciorbaru?= Date: Sat, 2 Jul 2022 22:43:22 +0300 Subject: [PATCH] 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 --- sql/filesort.cc | 55 +++++++++++++++++++++++++++++++++---------------- sql/sql_sort.h | 10 ++++++--- 2 files changed, 44 insertions(+), 21 deletions(-) diff --git a/sql/filesort.cc b/sql/filesort.cc index 15038e422d5..193b52c9bba 100644 --- a/sql/filesort.cc +++ b/sql/filesort.cc @@ -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; } diff --git a/sql/sql_sort.h b/sql/sql_sort.h index f5e85cfde43..88f719b3593 100644 --- a/sql/sql_sort.h +++ b/sql/sql_sort.h @@ -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);