MySQL 8.3.0
Source Code Documentation
filesort.h
Go to the documentation of this file.
1/* Copyright (c) 2006, 2023, Oracle and/or its affiliates.
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 "mem_root_array.h"
30#include "my_base.h" /* ha_rows */
31#include "my_dbug.h"
32#include "my_inttypes.h"
33#include "my_table_map.h"
34#include "sql/sort_param.h"
35
36class Addon_fields;
37class Field;
38class JOIN;
39class RowIterator;
40class Sort_result;
41class THD;
42struct ORDER;
43struct TABLE;
44struct st_sort_field;
45
46enum class Addon_fields_status;
47
48/**
49 Sorting related info.
50*/
51class Filesort {
52 public:
54 /// The tables we are sorting.
56 /// If true, do not free the filesort buffers (use if you expect to sort many
57 /// times, like in an uncacheable subquery).
58 const bool keep_buffers;
59 /// Maximum number of rows to return
61 /// ORDER BY list with some precalculated info for filesort
63 /// true means we are using Priority Queue for order by with limit.
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 // TODO(sgunders): Change tables to a table_map; however, currently
74 // some semijoin tables are missing from query_block->leaf_tables,
75 // so we can't do that yet.
77 ORDER *order, ha_rows limit_arg, bool remove_duplicates,
78 bool force_sort_rowids, bool unwrap_rollup);
79
81 uint *plength, uint *ppackable_length);
82
83 // Number of elements in the sortorder array.
84 uint sort_order_length() const { return m_sort_order_length; }
85
86 /// Whether we are using addon fields (sort entire rows) or not (sort row
87 /// IDs). Note that on the first call, this actually makes Sort_param
88 /// compute the decision and cache it, so it cannot be called before the sort
89 /// order is properly set up.
90 bool using_addon_fields();
91
92 /// Reset the decision made in using_addon_fields(). Only used in exceptional
93 /// circumstances (see NewWeedoutAccessPathForTables()).
94 void clear_addon_fields();
95
96 private:
97 /* Prepare ORDER BY list for sorting. */
98 uint make_sortorder(ORDER *order, bool unwrap_rollup);
99
101};
102
103bool filesort(THD *thd, Filesort *filesort, RowIterator *source_iterator,
104 table_map tables_to_get_rowid_for, ha_rows num_rows_estimate,
105 Filesort_info *fs_info, Sort_result *sort_result,
107void filesort_free_buffers(TABLE *table, bool full);
108void change_double_for_sort(double nr, uchar *to);
109
110/// Declared here so we can unit test it.
111uint sortlength(THD *thd, st_sort_field *sortorder, uint s_length);
112
113// Avoid pulling in sql/field.h.
114template <bool Is_big_endian>
115void copy_integer(uchar *to, size_t to_length, const uchar *from,
116 size_t from_length, bool is_unsigned);
117
118// Returns whether a sort involving this table would necessarily be on row ID,
119// even if not forced by other means.
120bool SortWillBeOnRowId(const TABLE *table);
121
122static inline void copy_native_longlong(uchar *to, size_t to_length,
123 longlong val, bool is_unsigned) {
124#ifdef WORDS_BIGENDIAN
125 constexpr bool Is_big_endian = true;
126#else
127 constexpr bool Is_big_endian = false;
128#endif
129 copy_integer<Is_big_endian>(to, to_length,
130 static_cast<uchar *>(static_cast<void *>(&val)),
131 sizeof(longlong), is_unsigned);
132}
133
134#endif /* FILESORT_INCLUDED */
This class wraps information about usage of addon fields.
Definition: sort_param.h:128
Definition: field.h:574
A class wrapping misc buffers used for sorting.
Definition: sql_sort.h:188
Sorting related info.
Definition: filesort.h:51
st_sort_field * sortorder
ORDER BY list with some precalculated info for filesort.
Definition: filesort.h:62
uint make_sortorder(ORDER *order, bool unwrap_rollup)
Definition: filesort.cc:674
Addon_fields * get_addon_fields(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:2258
bool using_pq
true means we are using Priority Queue for order by with limit.
Definition: filesort.h:64
ha_rows limit
Maximum number of rows to return.
Definition: filesort.h:60
bool m_remove_duplicates
Definition: filesort.h:65
Mem_root_array< TABLE * > tables
The tables we are sorting.
Definition: filesort.h:55
uint m_sort_order_length
Definition: filesort.h:100
bool m_force_sort_rowids
Definition: filesort.h:69
THD * m_thd
Definition: filesort.h:53
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:58
Filesort(THD *thd, Mem_root_array< TABLE * > tables, bool keep_buffers, ORDER *order, ha_rows limit_arg, bool remove_duplicates, bool force_sort_rowids, bool unwrap_rollup)
Definition: filesort.cc:660
uint sort_order_length() const
Definition: filesort.h:84
Sort_param m_sort_param
Definition: filesort.h:71
bool using_addon_fields()
Whether we are using addon fields (sort entire rows) or not (sort row IDs).
Definition: filesort.cc:2377
void clear_addon_fields()
Reset the decision made in using_addon_fields().
Definition: filesort.cc:2385
Definition: sql_optimizer.h:132
A typesafe replacement for DYNAMIC_ARRAY.
Definition: mem_root_array.h:425
A context for reading through a single table using a chosen access method: index read,...
Definition: row_iterator.h:81
There are several record formats for sorting:
Definition: sort_param.h:301
Definition: sql_sort.h:155
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:35
void filesort_free_buffers(TABLE *table, bool full)
Definition: filesort.cc:640
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:451
bool SortWillBeOnRowId(const TABLE *table)
Definition: filesort.cc:2187
bool filesort(THD *thd, Filesort *filesort, RowIterator *source_iterator, table_map tables_to_get_rowid_for, ha_rows num_rows_estimate, Filesort_info *fs_info, Sort_result *sort_result, ha_rows *found_rows)
Sort a table.
Definition: filesort.cc:366
static void copy_native_longlong(uchar *to, size_t to_length, longlong val, bool is_unsigned)
Definition: filesort.h:122
void change_double_for_sort(double nr, uchar *to)
Definition: filesort.cc:2392
uint sortlength(THD *thd, st_sort_field *sortorder, uint s_length)
Declared here so we can unit test it.
Definition: filesort.cc:2097
This file includes constants used by all storage engines.
my_off_t ha_rows
Definition: my_base.h:1140
Some integer typedefs for easier portability.
unsigned char uchar
Definition: my_inttypes.h:51
long long int longlong
Definition: my_inttypes.h:54
uint64_t table_map
Definition: my_table_map.h:29
static PFS_engine_table_share_proxy table
Definition: pfs.cc:60
constexpr value_type found_rows
Definition: classic_protocol_constants.h:38
constexpr value_type is_unsigned
Definition: classic_protocol_constants.h:272
R::iterator remove_duplicates(R *rp, LESSF &&lessf=LESSF(), EQUALF &&equalf=EQUALF())
Definition: tablespace_impl.cc:391
Addon_fields_status
Definition: sort_param.h:48
Definition: table.h:283
Definition: table.h:1403
Struct that holds information about a sort field.
Definition: sort_param.h:85