MySQL  8.0.21
Source Code Documentation
filesort.h
Go to the documentation of this file.
1 /* Copyright (c) 2006, 2020, 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 JOIN;
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 table we are sorting.
53  TABLE *const table;
54  /// If true, do not free the filesort buffers (use if you expect to sort many
55  /// times, like in an uncacheable subquery).
56  const bool keep_buffers;
57  /// Maximum number of rows to return
59  /// ORDER BY list with some precalculated info for filesort
61  /// true means we are using Priority Queue for order by with limit.
62  bool using_pq;
63  /// true means force stable sorting
66  // If true, we will always sort references to rows on table (and crucially,
67  // the result iterators used will always position the underlying table on
68  // the original row before returning from Read()).
70  // TODO: Consider moving this into private members of Filesort.
72 
73  Filesort(THD *thd, TABLE *table, bool keep_buffers, ORDER *order,
74  ha_rows limit_arg, bool force_stable_sort, bool remove_duplicates,
75  bool force_sort_positions, bool unwrap_rollup);
76 
78  Addon_fields_status *addon_fields_status,
79  uint *plength, uint *ppackable_length);
80 
81  // Number of elements in the sortorder array.
83 
84  /// Whether we are using addon fields (sort entire rows) or not (sort row
85  /// IDs). Note that on the first call, this actually makes Sort_param
86  /// compute the decision and cache it, so it cannot be called before the sort
87  /// order is properly set up.
88  bool using_addon_fields();
89 
90  private:
91  /* Prepare ORDER BY list for sorting. */
92  uint make_sortorder(ORDER *order, bool unwrap_rollup);
93 
95 };
96 
97 bool filesort(THD *thd, Filesort *fsort, RowIterator *source_iterator,
98  Filesort_info *fs_info, Sort_result *sort_result,
99  ha_rows *found_rows);
100 void filesort_free_buffers(TABLE *table, bool full);
101 void change_double_for_sort(double nr, uchar *to);
102 
103 /// Declared here so we can unit test it.
104 uint sortlength(THD *thd, st_sort_field *sortorder, uint s_length);
105 
106 /// Declared here for Item_func_weight_string.
108 
109 // Avoid pulling in sql/field.h.
110 template <bool Is_big_endian>
111 void copy_integer(uchar *to, size_t to_length, const uchar *from,
112  size_t from_length, bool is_unsigned);
113 
114 static inline void copy_native_longlong(uchar *to, size_t to_length,
115  longlong val, bool is_unsigned) {
116 #ifdef WORDS_BIGENDIAN
117  constexpr bool Is_big_endian = true;
118 #else
119  constexpr bool Is_big_endian = false;
120 #endif
121  copy_integer<Is_big_endian>(to, to_length,
122  static_cast<uchar *>(static_cast<void *>(&val)),
123  sizeof(longlong), is_unsigned);
124 }
125 
126 #endif /* FILESORT_INCLUDED */
void copy_integer(uchar *to, size_t to_length, const uchar *from, size_t from_length, bool is_unsigned)
Copies an integer value to a format comparable with memcmp().
Definition: field.h:554
unsigned char uchar
Definition: my_inttypes.h:51
Definition: sql_optimizer.h:119
uint make_sortorder(ORDER *order, bool unwrap_rollup)
Definition: filesort.cc:672
Definition: table.h:279
THD * m_thd
Definition: filesort.h:51
bool m_force_sort_positions
Definition: filesort.h:69
Some integer typedefs for easier portability.
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:359
uint m_sort_order_length
Definition: filesort.h:94
A context for reading through a single table using a chosen access method: index read, scan, etc, use of cache, etc.
Definition: row_iterator.h:61
bool using_addon_fields()
Whether we are using addon fields (sort entire rows) or not (sort row IDs).
Definition: filesort.cc:2244
Definition: field.h:694
static void copy_native_longlong(uchar *to, size_t to_length, longlong val, bool is_unsigned)
Definition: filesort.h:114
TABLE *const table
The table we are sorting.
Definition: filesort.h:53
bool m_force_stable_sort
true means force stable sorting
Definition: filesort.h:64
Addon_fields * get_addon_fields(TABLE *table, 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:2126
ha_rows limit
Maximum number of rows to return.
Definition: filesort.h:58
Sort_param m_sort_param
Definition: filesort.h:71
Definition: table.h:1313
This file includes constants used by all storage engines.
This class wraps information about usage of addon fields.
Definition: sort_param.h:125
Filesort(THD *thd, TABLE *table, bool keep_buffers, ORDER *order, ha_rows limit_arg, bool force_stable_sort, bool remove_duplicates, bool force_sort_positions, bool unwrap_rollup)
Definition: filesort.cc:656
void filesort_free_buffers(TABLE *table, bool full)
Definition: filesort.cc:639
Definition: item.h:741
unsigned int uint
Definition: uca-dump.cc:29
long long int longlong
Definition: my_inttypes.h:54
Addon_fields_status
Definition: sort_param.h:44
Definition: sql_sort.h:141
Struct that holds information about a sort field.
Definition: sort_param.h:81
A class wrapping misc buffers used for sorting.
Definition: sql_sort.h:174
const bool keep_buffers
If true, do not free the filesort buffers (use if you expect to sort many times, like in an uncacheab...
Definition: filesort.h:56
bool m_remove_duplicates
Definition: filesort.h:65
There are several record formats for sorting:
Definition: sort_param.h:261
Sorting related info.
Definition: filesort.h:49
uint sort_order_length() const
Definition: filesort.h:82
bool using_pq
true means we are using Priority Queue for order by with limit.
Definition: filesort.h:62
st_sort_field * sortorder
ORDER BY list with some precalculated info for filesort.
Definition: filesort.h:60
uint sortlength(THD *thd, st_sort_field *sortorder, uint s_length)
Declared here so we can unit test it.
Definition: filesort.cc:2009
longlong get_int_sort_key_for_item(Item *item)
Declared here for Item_func_weight_string.
Definition: filesort.cc:1377
my_off_t ha_rows
Definition: my_base.h:1135
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_class.h:799
void change_double_for_sort(double nr, uchar *to)
Definition: filesort.cc:2257