File: temp_table_param.h

package info (click to toggle)
mysql-8.0 8.0.43-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,273,924 kB
  • sloc: cpp: 4,684,605; ansic: 412,450; pascal: 108,398; java: 83,641; perl: 30,221; cs: 27,067; sql: 26,594; sh: 24,181; python: 21,816; yacc: 17,169; php: 11,522; xml: 7,388; javascript: 7,076; makefile: 2,194; lex: 1,075; awk: 670; asm: 520; objc: 183; ruby: 97; lisp: 86
file content (263 lines) | stat: -rw-r--r-- 9,485 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/* Copyright (c) 2015, 2025, Oracle and/or its affiliates.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License, version 2.0,
   as published by the Free Software Foundation.

   This program is designed to work with certain software (including
   but not limited to OpenSSL) that is licensed under separate terms,
   as designated in a particular file or component or in included license
   documentation.  The authors of MySQL hereby grant you an additional
   permission to link the program and your derivative works with the
   separately licensed software that they have either included with
   the program or referenced in the documentation.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License, version 2.0, for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */

#ifndef TEMP_TABLE_PARAM_INCLUDED
#define TEMP_TABLE_PARAM_INCLUDED

#include <sys/types.h>
#include <vector>

#include "my_base.h"
#include "my_inttypes.h"
#include "sql/field.h"
#include "sql/mem_root_array.h"
#include "sql/thr_malloc.h"

class KEY;
class Item;
class Window;
struct CHARSET_INFO;
struct MEM_ROOT;

enum Copy_func_type : int;

/**
   Helper class for copy_funcs(); represents an Item to copy from table to
   next tmp table.
*/
class Func_ptr {
 public:
  Func_ptr(Item *item, Field *result_field);

  Item *func() const { return m_func; }
  void set_func(Item *func);
  Field *result_field() const { return m_result_field; }
  Item_field *result_item() const;
  bool should_copy(Copy_func_type type) const {
    return m_func_bits & (1 << type);
  }

 private:
  Item *m_func;
  Field *m_result_field;

  // A premade Item_field for m_result_field (may be nullptr if allocation
  // failed). This has two purposes:
  //
  //  - It avoids repeated constructions if the field is used multiple times
  //    (e.g., first in a SELECT list, then in a sort order).
  //  - It gives a canonical, unique item, so that we can compare it with ==
  //    (in FindReplacementItem(), where ->eq would have a metadata issues).
  //    This is important if we are to replace it with something else again
  //    later.
  //
  // It is created on-demand to avoid getting into the thd->stmt_arena field
  // list for a temporary table that is freed later anyway.
  mutable Item_field *m_result_item = nullptr;

  // A bitmap where all CFT_* enums are bit indexes, and we have a 1 if m_func
  // is of the type given by that enum. E.g., if m_func is an Item_field,
  // (1 << CFT_FIELDS) will be set here. This is used for quickly finding out
  // which items to copy in copy_funcs(), without having to look at the actual
  // items (which involves virtual function calls).
  int m_func_bits;
};

/// Used by copy_funcs()
typedef Mem_root_array<Func_ptr> Func_ptr_array;

/**
  Object containing parameters used when creating and using temporary
  tables. Temporary tables created with the help of this object are
  used only internally by the query execution engine.
*/

class Temp_table_param {
 public:
  Mem_root_array<Copy_field> copy_fields;

  uchar *group_buff;
  Func_ptr_array *items_to_copy; /* Fields in tmp table */

  /**
    After temporary table creation, points to an index on the table
    created depending on the purpose of the table - grouping,
    duplicate elimination, etc. There is at most one such index.
  */
  KEY *keyinfo;

  /**
    LIMIT (maximum number of rows) for this temp table, or HA_POS_ERROR
    for no limit. Enforced by MaterializeIterator when writing to the table.
   */
  ha_rows end_write_records{HA_POS_ERROR};

  /**
    Number of items in the query. Includes both aggregate functions (e.g., SUM),
    and non-aggregates (e.g., RAND), window functions and fields.
    Also counts functions referred to from windowing or aggregate functions,
    i.e., "SELECT SUM(RAND())" sets this counter to 2.

    @see count_field_types
  */
  uint func_count;
  /**
    Number of fields in the query that have aggregate functions. Note
    that the optimizer may choose to optimize away these fields by
    replacing them with constants, in which case sum_func_count will
    need to be updated.

    @see optimize_aggregated_query, count_field_types
  */
  uint sum_func_count;
  uint hidden_field_count;
  uint group_parts, group_length, group_null_parts;
  /**
    Whether we allow running GROUP BY processing into a temporary table,
    i.e., keeping many different aggregations going at once without
    having ordered input. This is usually the case, but is currently not
    supported for aggregation UDFs, aggregates with DISTINCT, or ROLLUP.

    Note that even if this is true, the optimizer may choose to not use
    a temporary table, as it is often more efficient to just read along
    an index.
   */
  bool allow_group_via_temp_table{true};
  /**
    Number of outer_sum_funcs i.e the number of set functions that are
    aggregated in a query block outer to this subquery.

    @see count_field_types
  */
  uint outer_sum_func_count;
  /**
    Enabled when we have at least one outer_sum_func. Needed when used
    along with distinct.

    @see create_tmp_table
  */
  bool using_outer_summary_function;
  CHARSET_INFO *table_charset;
  bool schema_table;
  /*
    True if GROUP BY and its aggregate functions are already computed
    by a table access method (e.g. by loose index scan). In this case
    query execution should not perform aggregation and should treat
    aggregate functions as normal functions.
  */
  bool precomputed_group_by;
  bool force_copy_fields;
  /**
    true <=> don't actually create table handler when creating the result
    table. This allows range optimizer to add indexes later.
    Used for materialized derived tables/views.
    @see Table_ref::update_derived_keys.
  */
  bool skip_create_table;
  /*
    If true, create_tmp_field called from create_tmp_table will convert
    all BIT fields to 64-bit longs. This is a workaround the limitation
    that MEMORY tables cannot index BIT columns.
  */
  bool bit_fields_as_long;

  /// Whether the UNIQUE index can be promoted to PK
  bool can_use_pk_for_unique;

  /// Whether UNIQUE keys should always be implemented by way of a hidden
  /// hash field, never a unique index. Needed for materialization of mixed
  /// UNION ALL / UNION DISTINCT queries (see comments in
  /// create_result_table()).
  bool force_hash_field_for_unique{false};

  /// This tmp table is used for a window's frame buffer
  bool m_window_frame_buffer{false};

  /// For INTERSECT and EXCEPT computation
  enum {
    TTP_UNION_OR_TABLE,
    TTP_EXCEPT,
    TTP_INTERSECT
  } m_operation{TTP_UNION_OR_TABLE};
  /// The tempoary table rows need a counter to keep track of its
  /// duplicates: needed for EXCEPT and INTERSECT computation.
  bool needs_set_counter() { return m_operation != TTP_UNION_OR_TABLE; }
  /// For INTERSECT and EXCEPT computation.
  /// Cf. TABLE::m_last_operation_is_distinct.
  bool m_last_operation_is_distinct{false};

  /// If this is the out table of a window: the said window
  Window *m_window;

  explicit Temp_table_param(MEM_ROOT *mem_root = *THR_MALLOC)
      : copy_fields(mem_root),
        group_buff(nullptr),
        items_to_copy(nullptr),
        keyinfo(nullptr),
        func_count(0),
        sum_func_count(0),
        hidden_field_count(0),
        group_parts(0),
        group_length(0),
        group_null_parts(0),
        outer_sum_func_count(0),
        using_outer_summary_function(false),
        table_charset(nullptr),
        schema_table(false),
        precomputed_group_by(false),
        force_copy_fields(false),
        skip_create_table(false),
        bit_fields_as_long(false),
        can_use_pk_for_unique(true),
        m_window(nullptr) {}

  Temp_table_param(MEM_ROOT *mem_root, const Temp_table_param &other)
      : copy_fields(mem_root),
        group_buff(other.group_buff),
        items_to_copy(other.items_to_copy),
        keyinfo(other.keyinfo),
        end_write_records(other.end_write_records),
        func_count(other.func_count),
        sum_func_count(other.sum_func_count),
        hidden_field_count(other.hidden_field_count),
        group_parts(other.group_parts),
        group_length(other.group_length),
        group_null_parts(other.group_null_parts),
        allow_group_via_temp_table(other.allow_group_via_temp_table),
        outer_sum_func_count(other.outer_sum_func_count),
        using_outer_summary_function(other.using_outer_summary_function),
        table_charset(other.table_charset),
        schema_table(other.schema_table),
        precomputed_group_by(other.precomputed_group_by),
        force_copy_fields(other.force_copy_fields),
        skip_create_table(other.skip_create_table),
        bit_fields_as_long(other.bit_fields_as_long),
        can_use_pk_for_unique(other.can_use_pk_for_unique),
        force_hash_field_for_unique(other.force_hash_field_for_unique),
        m_window_frame_buffer(other.m_window_frame_buffer),
        m_window(other.m_window) {}

  void cleanup() { copy_fields.clear(); }
};

#endif  // TEMP_TABLE_PARAM_INCLUDED