MySQL  8.0.19
Source Code Documentation
filesort.h
Go to the documentation of this file.
1 /* Copyright (c) 2006, 2019, Oracle and/or its affiliates. All rights reserved.
2 
3  This program is free software; you can redistribute it and/or modify
4  it under the terms of the GNU General Public License, version 2.0,
5  as published by the Free Software Foundation.
6 
7  This program is also distributed with certain software (including
8  but not limited to OpenSSL) that is licensed under separate terms,
9  as designated in a particular file or component or in included license
10  documentation. The authors of MySQL hereby grant you an additional
11  permission to link the program and your derivative works with the
12  separately licensed software that they have included with MySQL.
13 
14  This program is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License, version 2.0, for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with this program; if not, write to the Free Software
21  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
22 
23 #ifndef FILESORT_INCLUDED
24 #define FILESORT_INCLUDED
25 
26 #include <stddef.h>
27 #include <sys/types.h>
28 
29 #include "my_base.h" /* ha_rows */
30 #include "my_dbug.h"
31 #include "my_inttypes.h"
32 #include "sql/sort_param.h"
33 
34 class Addon_fields;
35 class Field;
36 class QEP_TAB;
37 class RowIterator;
38 class Sort_result;
39 class THD;
40 struct ORDER;
41 struct TABLE;
42 struct st_sort_field;
43 
44 enum class Addon_fields_status;
45 
46 /**
47  Sorting related info.
48 */
49 class Filesort {
50  public:
52  /// The QEP entry for the table to be sorted
53  QEP_TAB *const qep_tab;
54  /// Maximum number of rows to return
56  /// ORDER BY list with some precalculated info for filesort
58  /// true means we are using Priority Queue for order by with limit.
59  bool using_pq;
60  /// true means force stable sorting
63  // If true, we will always sort references to rows on table (and crucially,
64  // the result iterators used will always position the underlying table on
65  // the original row before returning from Read()).
67  // TODO: Consider moving this into private members of Filesort.
69 
70  Filesort(THD *thd, QEP_TAB *tab_arg, ORDER *order, ha_rows limit_arg,
71  bool force_stable_sort, bool remove_duplicates,
72  bool force_sort_positions);
73 
75  Field **ptabfield, uint sortlength,
76  Addon_fields_status *addon_fields_status,
77  uint *plength, uint *ppackable_length);
78 
79  // Number of elements in the sortorder array.
81 
82  /// Whether we are using addon fields (sort entire rows) or not (sort row
83  /// IDs). Note that on the first call, this actually makes Sort_param
84  /// compute the decision and cache it, so it cannot be called before the sort
85  /// order is properly set up.
86  bool using_addon_fields();
87 
88  private:
89  /* Prepare ORDER BY list for sorting. */
90  uint make_sortorder(ORDER *order);
91 
93 };
94 
95 bool filesort(THD *thd, Filesort *fsort, RowIterator *source_iterator,
96  Filesort_info *fs_info, Sort_result *sort_result,
97  ha_rows *found_rows);
98 void filesort_free_buffers(TABLE *table, bool full);
99 void change_double_for_sort(double nr, uchar *to);
100 
101 /// Declared here so we can unit test it.
102 uint sortlength(THD *thd, st_sort_field *sortorder, uint s_length);
103 
104 #endif /* FILESORT_INCLUDED */
Filesort::sortorder
st_sort_field * sortorder
ORDER BY list with some precalculated info for filesort.
Definition: filesort.h:57
Filesort::using_pq
bool using_pq
true means we are using Priority Queue for order by with limit.
Definition: filesort.h:59
THD
Definition: sql_class.h:764
my_base.h
Addon_fields
This class wraps information about usage of addon fields.
Definition: sort_param.h:133
Filesort::make_sortorder
uint make_sortorder(ORDER *order)
Definition: filesort.cc:706
Filesort::m_force_sort_positions
bool m_force_sort_positions
Definition: filesort.h:66
sort_param.h
Filesort::get_addon_fields
Addon_fields * get_addon_fields(ulong max_length_for_sort_data, Field **ptabfield, uint sortlength, Addon_fields_status *addon_fields_status, uint *plength, uint *ppackable_length)
Get descriptors of fields appended to sorted fields and calculate their total length.
Definition: filesort.cc:2320
Filesort::m_force_stable_sort
bool m_force_stable_sort
true means force stable sorting
Definition: filesort.h:61
my_dbug.h
sortlength
uint sortlength(THD *thd, st_sort_field *sortorder, uint s_length)
Declared here so we can unit test it.
Definition: filesort.cc:2184
TABLE
Definition: table.h:1305
QEP_TAB
Definition: sql_executor.h:417
Addon_fields_status::max_length_for_sort_data
@ max_length_for_sort_data
Sort_param
There are several record formats for sorting:
Definition: sort_param.h:273
Filesort::m_sort_param
Sort_param m_sort_param
Definition: filesort.h:68
Filesort::m_remove_duplicates
bool m_remove_duplicates
Definition: filesort.h:62
Filesort
Sorting related info.
Definition: filesort.h:49
my_inttypes.h
filesort_free_buffers
void filesort_free_buffers(TABLE *table, bool full)
Definition: filesort.cc:662
Filesort::m_sort_order_length
uint m_sort_order_length
Definition: filesort.h:92
RowIterator
A context for reading through a single table using a chosen access method: index read,...
Definition: row_iterator.h:61
st_sort_field
Struct that holds information about a sort field.
Definition: sort_param.h:87
uint
unsigned int uint
Definition: uca-dump.cc:29
Filesort::using_addon_fields
bool using_addon_fields()
Whether we are using addon fields (sort entire rows) or not (sort row IDs).
Definition: filesort.cc:2445
Filesort::qep_tab
QEP_TAB *const qep_tab
The QEP entry for the table to be sorted.
Definition: filesort.h:53
Addon_fields_status
Addon_fields_status
Definition: sort_param.h:44
uchar
unsigned char uchar
Definition: my_inttypes.h:51
Sort_result
Definition: sql_sort.h:141
change_double_for_sort
void change_double_for_sort(double nr, uchar *to)
Definition: filesort.cc:2461
ORDER
Order clause list element.
Definition: table.h:276
filesort
bool filesort(THD *thd, Filesort *fsort, RowIterator *source_iterator, Filesort_info *fs_info, Sort_result *sort_result, ha_rows *found_rows)
Sort a table.
Definition: filesort.cc:376
Filesort::sort_order_length
uint sort_order_length() const
Definition: filesort.h:80
ulong
unsigned long ulong
Definition: my_inttypes.h:48
Filesort_info
A class wrapping misc buffers used for sorting.
Definition: sql_sort.h:174
ha_rows
my_off_t ha_rows
Definition: my_base.h:1132
Field
Definition: field.h:695
Filesort::limit
ha_rows limit
Maximum number of rows to return.
Definition: filesort.h:55
Filesort::m_thd
THD * m_thd
Definition: filesort.h:51
Filesort::Filesort
Filesort(THD *thd, QEP_TAB *tab_arg, ORDER *order, ha_rows limit_arg, bool force_stable_sort, bool remove_duplicates, bool force_sort_positions)
Definition: filesort.cc:679