MySQL 9.1.0
Source Code Documentation
temp_table_param.h
Go to the documentation of this file.
1/* Copyright (c) 2015, 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 TEMP_TABLE_PARAM_INCLUDED
25#define TEMP_TABLE_PARAM_INCLUDED
26
27#include <sys/types.h>
28#include <vector>
29
30#include "my_base.h"
31#include "my_inttypes.h"
32#include "sql/field.h"
33#include "sql/mem_root_array.h"
34#include "sql/thr_malloc.h"
35
36class KEY;
37class Item;
38class Window;
39struct CHARSET_INFO;
40struct MEM_ROOT;
41
42enum Copy_func_type : int;
43
44/**
45 Helper class for copy_funcs(); represents an Item to copy from table to
46 next tmp table.
47*/
48class Func_ptr {
49 public:
50 Func_ptr(Item *item, Field *result_field, Item *result_item = nullptr);
51
52 Item *func() const { return m_func; }
53 void set_func(Item *func);
54 Field *result_field() const { return m_result_field; }
55 Item *result_item() const;
57 return m_func_bits & (1 << type);
58 }
59
60 private:
63
64 // A premade Item for m_result_field (may be nullptr if allocation failed).
65 // This has two purposes:
66 //
67 // - It avoids repeated constructions if the field is used multiple times
68 // (e.g., first in a SELECT list, then in a sort order).
69 // - It gives a canonical, unique item, so that we can compare it with ==
70 // (in FindReplacementItem(), where ->eq would have a metadata issues).
71 // This is important if we are to replace it with something else again
72 // later.
73 //
74 // It is created on-demand to avoid getting into the thd->stmt_arena field
75 // list for a temporary table that is freed later anyway.
76 // It is usually an Item_field, but if supplied from constructor, can be of
77 // any type.
78 mutable Item *m_result_item = nullptr;
79
80 // A bitmap where all CFT_* enums are bit indexes, and we have a 1 if m_func
81 // is of the type given by that enum. E.g., if m_func is an Item_field,
82 // (1 << CFT_FIELDS) will be set here. This is used for quickly finding out
83 // which items to copy in copy_funcs(), without having to look at the actual
84 // items (which involves virtual function calls).
86};
87
88/// Used by copy_funcs()
90
91/**
92 Object containing parameters used when creating and using temporary
93 tables. Temporary tables created with the help of this object are
94 used only internally by the query execution engine.
95*/
96
98 public:
100
102 Func_ptr_array *items_to_copy; /* Fields in tmp table */
103
104 /**
105 After temporary table creation, points to an index on the table
106 created depending on the purpose of the table - grouping,
107 duplicate elimination, etc. There is at most one such index.
108 */
110
111 /**
112 LIMIT (maximum number of rows) for this temp table, or HA_POS_ERROR
113 for no limit. Enforced by MaterializeIterator when writing to the table.
114 */
116
117 /**
118 Number of items in the query. Includes both aggregate functions (e.g., SUM),
119 and non-aggregates (e.g., RAND), window functions and fields.
120 Also counts functions referred to from windowing or aggregate functions,
121 i.e., "SELECT SUM(RAND())" sets this counter to 2.
122
123 @see count_field_types
124 */
126 /**
127 Number of fields in the query that have aggregate functions. Note
128 that the optimizer may choose to optimize away these fields by
129 replacing them with constants, in which case sum_func_count will
130 need to be updated.
131
132 @see optimize_aggregated_query, count_field_types
133 */
137 /**
138 Whether we allow running GROUP BY processing into a temporary table,
139 i.e., keeping many different aggregations going at once without
140 having ordered input. This is usually the case, but is currently not
141 supported for aggregation UDFs, aggregates with DISTINCT, or ROLLUP.
142
143 Note that even if this is true, the optimizer may choose to not use
144 a temporary table, as it is often more efficient to just read along
145 an index.
146 */
148 /**
149 Number of outer_sum_funcs i.e the number of set functions that are
150 aggregated in a query block outer to this subquery.
151
152 @see count_field_types
153 */
155 /**
156 Enabled when we have at least one outer_sum_func. Needed when used
157 along with distinct.
158
159 @see create_tmp_table
160 */
164 /*
165 True if GROUP BY and its aggregate functions are already computed
166 by a table access method (e.g. by loose index scan). In this case
167 query execution should not perform aggregation and should treat
168 aggregate functions as normal functions.
169 */
172 /**
173 true <=> don't actually create table handler when creating the result
174 table. This allows range optimizer to add indexes later.
175 Used for materialized derived tables/views.
176 @see Table_ref::update_derived_keys.
177 */
179 /*
180 If true, create_tmp_field called from create_tmp_table will convert
181 all BIT fields to 64-bit longs. This is a workaround the limitation
182 that MEMORY tables cannot index BIT columns.
183 */
185
186 /// Whether the UNIQUE index can be promoted to PK
188
189 /// Whether UNIQUE keys should always be implemented by way of a hidden hash
190 /// field, never a unique index. Needed for materialization of mixed
191 /// UNION ALL / UNION DISTINCT queries (see comments in create_result_table())
192 /// and for DISTINCT deduplication using materialization (See
193 /// CreateTemporaryTableFromSelectList()).
195
196 /// This tmp table is used for a window's frame buffer
198
199 /// For INTERSECT and EXCEPT computation
200 enum {
204 } m_operation{TTP_UNION_OR_TABLE};
205 /// The tempoary table rows need a counter to keep track of its
206 /// duplicates: needed for EXCEPT and INTERSECT computation.
207 bool needs_set_counter() { return m_operation != TTP_UNION_OR_TABLE; }
208 /// For INTERSECT and EXCEPT computation.
209 /// Cf. TABLE::m_last_operation_is_distinct.
211
212 /// If this is the out table of a window: the said window
214
220 func_count(0),
223 group_parts(0),
224 group_length(0),
229 schema_table(false),
231 force_copy_fields(false),
232 skip_create_table(false),
233 bit_fields_as_long(false),
235 m_window(nullptr) {}
236
239 group_buff(other.group_buff),
241 keyinfo(other.keyinfo),
243 func_count(other.func_count),
261 m_window(other.m_window) {}
262
263 // Used by CTE derived table clones to set correct info, see
264 // Common_table_expr::clone_tmp_table. The info may be consulted e.g.
265 // by get_hidden_field_count_for_derived(), e.g. by HW.
267 if (this == &other) {
268 return *this;
269 }
270 for (const auto &cf : other.copy_fields) copy_fields.push_back(cf);
271 group_buff = other.group_buff;
273 keyinfo = other.keyinfo;
275 func_count = other.func_count;
278 group_parts = other.group_parts;
293 m_window = other.m_window;
294 return *this;
295 }
296
297 void cleanup() { copy_fields.clear(); }
298};
299
300#endif // TEMP_TABLE_PARAM_INCLUDED
Kerberos Client Authentication nullptr
Definition: auth_kerberos_client_plugin.cc:251
Definition: field.h:577
Helper class for copy_funcs(); represents an Item to copy from table to next tmp table.
Definition: temp_table_param.h:48
Item * m_func
Definition: temp_table_param.h:61
Field * m_result_field
Definition: temp_table_param.h:62
Func_ptr(Item *item, Field *result_field, Item *result_item=nullptr)
Definition: sql_tmp_table.cc:2984
int m_func_bits
Definition: temp_table_param.h:85
bool should_copy(Copy_func_type type) const
Definition: temp_table_param.h:56
Item * result_item() const
Definition: sql_tmp_table.cc:2995
Item * func() const
Definition: temp_table_param.h:52
Item * m_result_item
Definition: temp_table_param.h:78
Field * result_field() const
Definition: temp_table_param.h:54
void set_func(Item *func)
Definition: sql_tmp_table.cc:2990
Base class that is used to represent any kind of expression in a relational query.
Definition: item.h:930
Definition: key.h:113
A typesafe replacement for DYNAMIC_ARRAY.
Definition: mem_root_array.h:426
Object containing parameters used when creating and using temporary tables.
Definition: temp_table_param.h:97
bool force_copy_fields
Definition: temp_table_param.h:171
uint sum_func_count
Number of fields in the query that have aggregate functions.
Definition: temp_table_param.h:134
bool using_outer_summary_function
Enabled when we have at least one outer_sum_func.
Definition: temp_table_param.h:161
uint outer_sum_func_count
Number of outer_sum_funcs i.e the number of set functions that are aggregated in a query block outer ...
Definition: temp_table_param.h:154
Window * m_window
If this is the out table of a window: the said window.
Definition: temp_table_param.h:213
Func_ptr_array * items_to_copy
Definition: temp_table_param.h:102
bool m_last_operation_is_distinct
For INTERSECT and EXCEPT computation.
Definition: temp_table_param.h:210
bool allow_group_via_temp_table
Whether we allow running GROUP BY processing into a temporary table, i.e., keeping many different agg...
Definition: temp_table_param.h:147
bool force_hash_field_for_unique
Whether UNIQUE keys should always be implemented by way of a hidden hash field, never a unique index.
Definition: temp_table_param.h:194
bool m_window_frame_buffer
This tmp table is used for a window's frame buffer.
Definition: temp_table_param.h:197
KEY * keyinfo
After temporary table creation, points to an index on the table created depending on the purpose of t...
Definition: temp_table_param.h:109
Temp_table_param(MEM_ROOT *mem_root, const Temp_table_param &other)
Definition: temp_table_param.h:237
CHARSET_INFO * table_charset
Definition: temp_table_param.h:162
@ TTP_UNION_OR_TABLE
Definition: temp_table_param.h:201
@ TTP_INTERSECT
Definition: temp_table_param.h:203
@ TTP_EXCEPT
Definition: temp_table_param.h:202
uint group_null_parts
Definition: temp_table_param.h:136
uint func_count
Number of items in the query.
Definition: temp_table_param.h:125
enum Temp_table_param::@191 TTP_UNION_OR_TABLE
For INTERSECT and EXCEPT computation.
uint group_length
Definition: temp_table_param.h:136
Mem_root_array< Copy_field > copy_fields
Definition: temp_table_param.h:99
bool schema_table
Definition: temp_table_param.h:163
bool needs_set_counter()
The tempoary table rows need a counter to keep track of its duplicates: needed for EXCEPT and INTERSE...
Definition: temp_table_param.h:207
Temp_table_param(MEM_ROOT *mem_root= *THR_MALLOC)
Definition: temp_table_param.h:215
uint hidden_field_count
Definition: temp_table_param.h:135
bool skip_create_table
true <=> don't actually create table handler when creating the result table.
Definition: temp_table_param.h:178
bool can_use_pk_for_unique
Whether the UNIQUE index can be promoted to PK.
Definition: temp_table_param.h:187
uint group_parts
Definition: temp_table_param.h:136
void cleanup()
Definition: temp_table_param.h:297
Temp_table_param & operator=(const Temp_table_param &other)
Definition: temp_table_param.h:266
bool precomputed_group_by
Definition: temp_table_param.h:170
uchar * group_buff
Definition: temp_table_param.h:101
bool bit_fields_as_long
Definition: temp_table_param.h:184
ha_rows end_write_records
LIMIT (maximum number of rows) for this temp table, or HA_POS_ERROR for no limit.
Definition: temp_table_param.h:115
Represents the (explicit) window of a SQL 2003 section 7.11 <window clause>, or the implicit (inlined...
Definition: window.h:110
static MEM_ROOT mem_root
Definition: client_plugin.cc:114
This file includes constants used by all storage engines.
my_off_t ha_rows
Definition: my_base.h:1141
#define HA_POS_ERROR
Definition: my_base.h:1143
Some integer typedefs for easier portability.
unsigned char uchar
Definition: my_inttypes.h:52
thread_local MEM_ROOT ** THR_MALLOC
Definition: mysqld.cc:1569
required string type
Definition: replication_group_member_actions.proto:34
Copy_func_type
Definition: sql_executor.h:161
Definition: m_ctype.h:421
The MEM_ROOT is a simple arena, where allocations are carved out of larger blocks.
Definition: my_alloc.h:83
Mem_root_array< Func_ptr > Func_ptr_array
Used by copy_funcs()
Definition: temp_table_param.h:89