MySQL 8.4.2
Source Code Documentation
filesort.h
Go to the documentation of this file.
1/* Copyright (c) 2006, 2024, 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 designed to work 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 either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24#ifndef FILESORT_INCLUDED
25#define FILESORT_INCLUDED
26
27#include <stddef.h>
28#include <sys/types.h>
29
30#include "mem_root_array.h"
31#include "my_base.h" /* ha_rows */
32#include "my_dbug.h"
33#include "my_inttypes.h"
34#include "my_table_map.h"
35#include "sql/sort_param.h"
36
37class Addon_fields;
38class Field;
39class JOIN;
40class RowIterator;
41class Sort_result;
42class THD;
43struct ORDER;
44struct TABLE;
45struct st_sort_field;
46
47enum class Addon_fields_status;
48
49/**
50 Sorting related info.
51*/
52class Filesort {
53 public:
55 /// The tables we are sorting.
57 /// If true, do not free the filesort buffers (use if you expect to sort many
58 /// times, like in an uncacheable subquery).
59 const bool keep_buffers;
60 /// Maximum number of rows to return
62 /// ORDER BY list with some precalculated info for filesort
64 /// true means we are using Priority Queue for order by with limit.
67 // If true, we will always sort references to rows on table (and crucially,
68 // the result iterators used will always position the underlying table on
69 // the original row before returning from Read()).
71 // TODO: Consider moving this into private members of Filesort.
73
74 // TODO(sgunders): Change tables to a table_map; however, currently
75 // some semijoin tables are missing from query_block->leaf_tables,
76 // so we can't do that yet.
78 ORDER *order, ha_rows limit_arg, bool remove_duplicates,
79 bool force_sort_rowids, bool unwrap_rollup);
80
82 uint *plength, uint *ppackable_length);
83
84 // Number of elements in the sortorder array.
85 uint sort_order_length() const { return m_sort_order_length; }
86
87 /// Whether we are using addon fields (sort entire rows) or not (sort row
88 /// IDs). Note that on the first call, this actually makes Sort_param
89 /// compute the decision and cache it, so it cannot be called before the sort
90 /// order is properly set up.
91 bool using_addon_fields();
92
93 /// Reset the decision made in using_addon_fields(). Only used in exceptional
94 /// circumstances (see NewWeedoutAccessPathForTables()).
95 void clear_addon_fields();
96
97 private:
98 /* Prepare ORDER BY list for sorting. */
99 uint make_sortorder(ORDER *order, bool unwrap_rollup);
100
102};
103
104bool filesort(THD *thd, Filesort *filesort, RowIterator *source_iterator,
105 table_map tables_to_get_rowid_for, ha_rows num_rows_estimate,
106 Filesort_info *fs_info, Sort_result *sort_result,
108void filesort_free_buffers(TABLE *table, bool full);
109void change_double_for_sort(double nr, uchar *to);
110
111/// Declared here so we can unit test it.
112uint sortlength(THD *thd, st_sort_field *sortorder, uint s_length);
113
114// Avoid pulling in sql/field.h.
115template <bool Is_big_endian>
116void copy_integer(uchar *to, size_t to_length, const uchar *from,
117 size_t from_length, bool is_unsigned);
118
119// Returns whether a sort involving this table would necessarily be on row ID,
120// even if not forced by other means.
121bool SortWillBeOnRowId(const TABLE *table);
122
123static inline void copy_native_longlong(uchar *to, size_t to_length,
124 longlong val, bool is_unsigned) {
125#ifdef WORDS_BIGENDIAN
126 constexpr bool Is_big_endian = true;
127#else
128 constexpr bool Is_big_endian = false;
129#endif
130 copy_integer<Is_big_endian>(to, to_length,
131 static_cast<uchar *>(static_cast<void *>(&val)),
132 sizeof(longlong), is_unsigned);
133}
134
135#endif /* FILESORT_INCLUDED */
This class wraps information about usage of addon fields.
Definition: sort_param.h:129
Definition: field.h:575
A class wrapping misc buffers used for sorting.
Definition: sql_sort.h:189
Sorting related info.
Definition: filesort.h:52
st_sort_field * sortorder
ORDER BY list with some precalculated info for filesort.
Definition: filesort.h:63
uint make_sortorder(ORDER *order, bool unwrap_rollup)
Definition: filesort.cc:675
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:2261
bool using_pq
true means we are using Priority Queue for order by with limit.
Definition: filesort.h:65
ha_rows limit
Maximum number of rows to return.
Definition: filesort.h:61
bool m_remove_duplicates
Definition: filesort.h:66
Mem_root_array< TABLE * > tables
The tables we are sorting.
Definition: filesort.h:56
uint m_sort_order_length
Definition: filesort.h:101
bool m_force_sort_rowids
Definition: filesort.h:70
THD * m_thd
Definition: filesort.h:54
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:59
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:661
uint sort_order_length() const
Definition: filesort.h:85
Sort_param m_sort_param
Definition: filesort.h:72
bool using_addon_fields()
Whether we are using addon fields (sort entire rows) or not (sort row IDs).
Definition: filesort.cc:2380
void clear_addon_fields()
Reset the decision made in using_addon_fields().
Definition: filesort.cc:2388
Definition: sql_optimizer.h:133
A typesafe replacement for DYNAMIC_ARRAY.
Definition: mem_root_array.h:426
A context for reading through a single table using a chosen access method: index read,...
Definition: row_iterator.h:82
There are several record formats for sorting:
Definition: sort_param.h:302
Definition: sql_sort.h:156
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:36
void filesort_free_buffers(TABLE *table, bool full)
Definition: filesort.cc:641
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:452
bool SortWillBeOnRowId(const TABLE *table)
Definition: filesort.cc:2190
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:367
static void copy_native_longlong(uchar *to, size_t to_length, longlong val, bool is_unsigned)
Definition: filesort.h:123
void change_double_for_sort(double nr, uchar *to)
Definition: filesort.cc:2395
uint sortlength(THD *thd, st_sort_field *sortorder, uint s_length)
Declared here so we can unit test it.
Definition: filesort.cc:2100
This file includes constants used by all storage engines.
my_off_t ha_rows
Definition: my_base.h:1141
Some integer typedefs for easier portability.
unsigned char uchar
Definition: my_inttypes.h:52
long long int longlong
Definition: my_inttypes.h:55
uint64_t table_map
Definition: my_table_map.h:30
static PFS_engine_table_share_proxy table
Definition: pfs.cc:61
constexpr value_type found_rows
Definition: classic_protocol_constants.h:39
constexpr value_type is_unsigned
Definition: classic_protocol_constants.h:273
R::iterator remove_duplicates(R *rp, LESSF &&lessf=LESSF(), EQUALF &&equalf=EQUALF())
Definition: tablespace_impl.cc:392
Addon_fields_status
Definition: sort_param.h:49
Definition: table.h:286
Definition: table.h:1406
Struct that holds information about a sort field.
Definition: sort_param.h:86