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