MySQL 9.3.0
Source Code Documentation
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
query_result.h
Go to the documentation of this file.
1/* Copyright (c) 2015, 2025, 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 QUERY_RESULT_INCLUDED
25#define QUERY_RESULT_INCLUDED
26
27#include <assert.h>
28#include <sys/types.h>
29
30#include <cstddef>
31
32#include "mem_root_deque.h"
33#include "my_base.h"
34
35#include "my_inttypes.h"
36#include "my_io.h"
37#include "my_sys.h"
40#include "mysqld_error.h" // ER_*
41#include "sql/sql_exchange.h"
42#include "sql/sql_list.h"
43
44class Item;
45class Item_subselect;
46class PT_select_var;
50class THD;
51struct CHARSET_INFO;
52template <class Element_type>
53class mem_root_deque;
54class Table_ref;
55
56/*
57 This is used to get result from a query
58*/
59
61 protected:
63
64 public:
65 /**
66 Number of records estimated in this result.
67 Valid only for materialized derived tables/views.
68 */
70 /**
71 Cost to execute the subquery which produces this result.
72 Valid only for materialized derived tables/views.
73 */
75
77 virtual ~Query_result() = default;
78
79 virtual bool needs_file_privilege() const { return false; }
80
81 /**
82 Returns true if the data has to be exported to object store.
83 */
84 virtual bool export_result_to_object_storage() const { return false; }
85
86 /**
87 Change wrapped Query_result.
88
89 Replace the wrapped query result object with new_result and call
90 prepare() on new_result.
91
92 This base class implementation doesn't wrap other Query_results.
93
94 @retval false Success
95 @retval true Error
96 */
97 virtual bool change_query_result(THD *, Query_result *) { return false; }
98 /// @return true if an interceptor object is needed for EXPLAIN
99 virtual bool need_explain_interceptor() const { return false; }
100
101 /**
102 Perform preparation specific to the query expression or DML statement.
103
104 @returns false if success, true if error
105 */
106 virtual bool prepare(THD *, const mem_root_deque<Item *> &,
107 Query_expression *u) {
108 unit = u;
109 return false;
110 }
111
112 /**
113 Prepare for execution of the query expression or DML statement.
114
115 Generally, this will have an implementation only for outer-most
116 Query_block objects, such as data change statements (for preparation
117 of the target table(s)) or dump statements (for preparation of target file).
118
119 @returns false if success, true if error
120 */
121 virtual bool start_execution(THD *) { return false; }
122
123 /// Create table, only needed to support CREATE TABLE ... SELECT
124 virtual bool create_table_for_query_block(THD *) { return false; }
125 /*
126 Because of peculiarities of prepared statements protocol
127 we need to know number of columns in the result set (if
128 there is a result set) apart from sending columns metadata.
129 */
130 virtual uint field_count(const mem_root_deque<Item *> &fields) const;
131 virtual bool send_result_set_metadata(THD *thd,
133 uint flags) = 0;
134 virtual bool send_data(THD *thd, const mem_root_deque<Item *> &items) = 0;
135 virtual bool send_eof(THD *thd) = 0;
136 /**
137 Check if this query result set supports cursors
138
139 @returns false if success, true if error
140 */
141 virtual bool check_supports_cursor() const {
142 my_error(ER_SP_BAD_CURSOR_QUERY, MYF(0));
143 return true;
144 }
145 virtual void abort_result_set(THD *) {}
146 /**
147 Cleanup after one execution of the unit, to be ready for a next execution
148 inside the same statement.
149 @returns true if error
150 */
151 virtual bool reset() {
152 assert(false);
153 return false;
154 }
155 /**
156 Cleanup after this execution. Completes the execution and resets object
157 before next execution of a prepared statement/stored procedure.
158 */
159 virtual void cleanup() { /* do nothing */
160 }
161 /**
162 @returns true if an alternative implementation may replace this with
163 a protocol adapter.
164 */
165 virtual bool use_protocol_adapter() const { return false; }
166 /**
167 @returns true if an alternative implementation may replace this with
168 a protocol wrapper.
169 */
170 virtual bool use_protocol_wrapper() const { return false; }
171
172 /// Only overridden (and non-empty) for Query_result_union, q.v.
173 virtual void set_limit(ha_rows) {}
174
175 /// @returns server side cursor, if associated with query result
176 virtual Server_side_cursor *cursor() const {
177 assert(false);
178 return nullptr;
179 }
180
181 /// @returns create_info object if available.
182 [[nodiscard]] virtual Table_ref *get_create_table() const { return nullptr; }
183};
184
185/*
186 Base class for Query_result descendants which intercept and
187 transform result set rows. As the rows are not sent to the client,
188 sending of result set metadata should be suppressed as well.
189*/
190
192 public:
194 uint field_count(const mem_root_deque<Item *> &) const override { return 0; }
196 uint) override {
197 return false;
198 }
199};
200
202 /**
203 True if we have sent result set metadata to the client.
204 In this case the client always expects us to end the result
205 set with an eof or error packet
206 */
208
209 public:
212 uint flags) override;
213 bool send_data(THD *thd, const mem_root_deque<Item *> &items) override;
214 bool send_eof(THD *thd) override;
215 bool check_supports_cursor() const override { return false; }
216 void abort_result_set(THD *thd) override;
217 void cleanup() override { is_result_set_started = false; }
218 /**
219 An alternative implementation may provide an optimized protocol adapter
220 for this object.
221 */
222 bool use_protocol_adapter() const override { return true; }
223};
224
225class sql_exchange;
226
228 protected:
234
235 public:
238 path[0] = 0;
239 }
240 ~Query_result_to_file() override { assert(file < 0); }
241
242 bool needs_file_privilege() const override { return true; }
243 bool check_supports_cursor() const override;
244 bool send_eof(THD *thd) override;
245 void cleanup() override;
246};
247
249 protected:
251
252 public:
255
256 bool send_data(THD *, const mem_root_deque<Item *> &) override {
257 return false;
258 }
259 bool send_eof(THD *thd) override;
260 void cleanup() override;
261 bool export_result_to_object_storage() const override { return true; }
262 bool use_protocol_adapter() const override { return true; }
264};
265
266#define ESCAPE_CHARS "ntrb0ZN" // keep synchronous with READ_INFO::unescape
267
268/*
269 List of all possible characters of a numeric value text representation.
270*/
271#define NUMERIC_CHARS ".0123456789e+-"
272
276 int field_term_char; // first char of FIELDS TERMINATED BY or MAX_INT
277 /*
278 The is_ambiguous_field_sep field is true if a value of the field_sep_char
279 field is one of the 'n', 't', 'r' etc characters
280 (see the READ_INFO::unescape method and the ESCAPE_CHARS constant value).
281 */
283 /*
284 The is_ambiguous_field_term is true if field_sep_char contains the first
285 char of the FIELDS TERMINATED BY (ENCLOSED BY is empty), and items can
286 contain this character.
287 */
289 /*
290 The is_unsafe_field_sep field is true if a value of the field_sep_char
291 field is one of the '0'..'9', '+', '-', '.' and 'e' characters
292 (see the NUMERIC_CHARS constant value).
293 */
296 const CHARSET_INFO *write_cs; // output charset
297 public:
299 bool prepare(THD *thd, const mem_root_deque<Item *> &list,
300 Query_expression *u) override;
301 bool start_execution(THD *thd) override;
302 bool send_data(THD *thd, const mem_root_deque<Item *> &items) override;
303 void cleanup() override;
304};
305
307 public:
309 bool prepare(THD *thd, const mem_root_deque<Item *> &list,
310 Query_expression *u) override;
311 bool start_execution(THD *thd) override;
312 bool send_data(THD *thd, const mem_root_deque<Item *> &items) override;
313};
314
317
318 public:
321 var_list.clear();
322 }
323 bool prepare(THD *thd, const mem_root_deque<Item *> &list,
324 Query_expression *u) override;
325 bool send_data(THD *thd, const mem_root_deque<Item *> &items) override;
326 bool send_eof(THD *thd) override;
327 bool check_supports_cursor() const override;
328 void cleanup() override { row_count = 0; }
329 /**
330 An alternative implementation may provide an optimized protocol wrapper
331 for this object.
332 */
333 bool use_protocol_wrapper() const override { return true; }
334};
335
336/**
337 Base class for result from a subquery.
338*/
339
341 protected:
343
344 public:
346 : Query_result_interceptor(), item(item_arg) {}
347 bool send_data(THD *thd, const mem_root_deque<Item *> &items) override = 0;
348 bool send_eof(THD *) override { return false; }
349};
350
351#endif // QUERY_RESULT_INCLUDED
Kerberos Client Authentication nullptr
Definition: auth_kerberos_client_plugin.cc:247
Base class that is common to all subqueries and subquery predicates.
Definition: item_subselect.h:80
Base class that is used to represent any kind of expression in a relational query.
Definition: item.h:930
Definition: sql_list.h:494
Definition: parse_tree_nodes.h:1351
Definition: query_result.h:315
List< PT_select_var > var_list
Definition: query_result.h:319
bool prepare(THD *thd, const mem_root_deque< Item * > &list, Query_expression *u) override
Perform preparation specific to the query expression or DML statement.
Definition: query_result.cc:999
void cleanup() override
Cleanup after this execution.
Definition: query_result.h:328
bool send_eof(THD *thd) override
Definition: query_result.cc:1048
ha_rows row_count
Definition: query_result.h:316
Query_dumpvar()
Definition: query_result.h:320
bool send_data(THD *thd, const mem_root_deque< Item * > &items) override
Definition: query_result.cc:1016
bool check_supports_cursor() const override
Check if this query result set supports cursors.
Definition: query_result.cc:1011
bool use_protocol_wrapper() const override
An alternative implementation may provide an optimized protocol wrapper for this object.
Definition: query_result.h:333
This class represents a query expression (one query block or several query blocks combined with UNION...
Definition: sql_lex.h:638
Definition: query_result.h:306
Query_result_dump(sql_exchange *ex)
Definition: query_result.h:308
bool send_data(THD *thd, const mem_root_deque< Item * > &items) override
Definition: query_result.cc:968
bool prepare(THD *thd, const mem_root_deque< Item * > &list, Query_expression *u) override
Perform preparation specific to the query expression or DML statement.
Definition: query_result.cc:957
bool start_execution(THD *thd) override
Prepare for execution of the query expression or DML statement.
Definition: query_result.cc:963
Definition: query_result.h:273
bool start_execution(THD *thd) override
Prepare for execution of the query expression or DML statement.
Definition: query_result.cc:643
const CHARSET_INFO * write_cs
Definition: query_result.h:296
bool is_unsafe_field_sep
Definition: query_result.h:294
bool prepare(THD *thd, const mem_root_deque< Item * > &list, Query_expression *u) override
Perform preparation specific to the query expression or DML statement.
Definition: query_result.cc:556
void cleanup() override
Cleanup after this execution.
Definition: query_result.cc:948
int field_sep_char
Definition: query_result.h:275
bool fixed_row_size
Definition: query_result.h:295
int escape_char
Definition: query_result.h:275
bool send_data(THD *thd, const mem_root_deque< Item * > &items) override
Definition: query_result.cc:654
size_t field_term_length
Definition: query_result.h:274
int line_sep_char
Definition: query_result.h:275
Query_result_export(sql_exchange *ex)
Definition: query_result.h:298
bool is_ambiguous_field_sep
Definition: query_result.h:282
bool is_ambiguous_field_term
Definition: query_result.h:288
int field_term_char
Definition: query_result.h:276
Definition: query_result.h:191
Query_result_interceptor()
Definition: query_result.h:193
bool send_result_set_metadata(THD *, const mem_root_deque< Item * > &, uint) override
Definition: query_result.h:195
uint field_count(const mem_root_deque< Item * > &) const override
Definition: query_result.h:194
Definition: query_result.h:201
void cleanup() override
Cleanup after this execution.
Definition: query_result.h:217
bool is_result_set_started
True if we have sent result set metadata to the client.
Definition: query_result.h:207
bool use_protocol_adapter() const override
An alternative implementation may provide an optimized protocol adapter for this object.
Definition: query_result.h:222
Query_result_send()
Definition: query_result.h:210
bool check_supports_cursor() const override
Check if this query result set supports cursors.
Definition: query_result.h:215
bool send_data(THD *thd, const mem_root_deque< Item * > &items) override
Definition: query_result.cc:99
void abort_result_set(THD *thd) override
Definition: query_result.cc:80
bool send_result_set_metadata(THD *thd, const mem_root_deque< Item * > &list, uint flags) override
Definition: query_result.cc:72
bool send_eof(THD *thd) override
Definition: query_result.cc:114
Base class for result from a subquery.
Definition: query_result.h:340
bool send_eof(THD *) override
Definition: query_result.h:348
Query_result_subquery(Item_subselect *item_arg)
Definition: query_result.h:345
bool send_data(THD *thd, const mem_root_deque< Item * > &items) override=0
Item_subselect * item
Definition: query_result.h:342
Definition: query_result.h:227
bool needs_file_privilege() const override
Definition: query_result.h:242
IO_CACHE cache
Definition: query_result.h:231
bool check_supports_cursor() const override
Check if this query result set supports cursors.
Definition: query_result.cc:435
sql_exchange * exchange
Definition: query_result.h:229
char path[FN_REFLEN]
Definition: query_result.h:233
Query_result_to_file(sql_exchange *ex)
Definition: query_result.h:236
File file
Definition: query_result.h:230
void cleanup() override
Cleanup after this execution.
Definition: query_result.cc:451
~Query_result_to_file() override
Definition: query_result.h:240
ha_rows row_count
Definition: query_result.h:232
bool send_eof(THD *thd) override
Definition: query_result.cc:440
Definition: query_result.h:248
Query_result_to_object_store(sql_exchange *ex)
Definition: query_result.h:253
sql_exchange * get_sql_exchange()
Definition: query_result.h:263
void cleanup() override
Reset the number of affected rows.
Definition: query_result.cc:478
bool export_result_to_object_storage() const override
Returns true if the data has to be exported to object store.
Definition: query_result.h:261
bool send_data(THD *, const mem_root_deque< Item * > &) override
Definition: query_result.h:256
sql_exchange * exchange
Definition: query_result.h:250
bool use_protocol_adapter() const override
Definition: query_result.h:262
bool send_eof(THD *thd) override
Returns true incase of an error.
Definition: query_result.cc:469
Definition: query_result.h:60
virtual Server_side_cursor * cursor() const
Definition: query_result.h:176
Query_result()
Definition: query_result.h:76
virtual bool send_eof(THD *thd)=0
virtual bool send_result_set_metadata(THD *thd, const mem_root_deque< Item * > &list, uint flags)=0
virtual bool send_data(THD *thd, const mem_root_deque< Item * > &items)=0
virtual bool change_query_result(THD *, Query_result *)
Change wrapped Query_result.
Definition: query_result.h:97
virtual bool need_explain_interceptor() const
Definition: query_result.h:99
virtual bool prepare(THD *, const mem_root_deque< Item * > &, Query_expression *u)
Perform preparation specific to the query expression or DML statement.
Definition: query_result.h:106
double estimated_cost
Cost to execute the subquery which produces this result.
Definition: query_result.h:74
ha_rows estimated_rowcount
Number of records estimated in this result.
Definition: query_result.h:69
virtual void cleanup()
Cleanup after this execution.
Definition: query_result.h:159
virtual bool use_protocol_wrapper() const
Definition: query_result.h:170
virtual bool reset()
Cleanup after one execution of the unit, to be ready for a next execution inside the same statement.
Definition: query_result.h:151
virtual bool export_result_to_object_storage() const
Returns true if the data has to be exported to object store.
Definition: query_result.h:84
virtual bool check_supports_cursor() const
Check if this query result set supports cursors.
Definition: query_result.h:141
virtual uint field_count(const mem_root_deque< Item * > &fields) const
Definition: query_result.cc:68
virtual void abort_result_set(THD *)
Definition: query_result.h:145
virtual Table_ref * get_create_table() const
Definition: query_result.h:182
virtual bool use_protocol_adapter() const
Definition: query_result.h:165
virtual bool create_table_for_query_block(THD *)
Create table, only needed to support CREATE TABLE ... SELECT.
Definition: query_result.h:124
Query_expression * unit
Definition: query_result.h:62
virtual ~Query_result()=default
virtual bool needs_file_privilege() const
Definition: query_result.h:79
virtual void set_limit(ha_rows)
Only overridden (and non-empty) for Query_result_union, q.v.
Definition: query_result.h:173
virtual bool start_execution(THD *)
Prepare for execution of the query expression or DML statement.
Definition: query_result.h:121
Common base class for n-ary set operations, including unary.
Definition: query_term.h:555
Server_side_cursor – an interface for materialized implementation of cursors.
Definition: sql_cursor.h:51
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:36
Definition: table.h:2904
A (partial) implementation of std::deque allocating its blocks on a MEM_ROOT.
Definition: mem_root_deque.h:111
Definition: sql_exchange.h:192
#define L
Definition: ctype-tis620.cc:74
void my_error(int nr, myf MyFlags,...)
Fill in and print a previously registered error message.
Definition: my_error.cc:216
static int flags[50]
Definition: hp_test1.cc:40
A better implementation of the UNIX ctype(3) library.
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.
#define MYF(v)
Definition: my_inttypes.h:97
Common #defines and includes for file and socket I/O.
#define FN_REFLEN
Definition: my_io.h:87
Types to make file and socket I/O compatible.
int File
Definition: my_io_bits.h:51
Common header for many mysys elements.
Definition: os0file.h:89
std::list< T, ut::allocator< T > > list
Specialization of list which uses ut_allocator.
Definition: ut0new.h:2880
Definition: m_ctype.h:421
Definition: my_sys.h:337