-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathsql_cursor.cc
417 lines (344 loc) · 13.4 KB
/
sql_cursor.cc
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/* Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved.
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 also distributed 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 included with MySQL.
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 */
#include "sql/sql_cursor.h"
#include <stddef.h>
#include <sys/types.h>
#include <algorithm>
#include "memory_debugging.h"
#include "my_alloc.h"
#include "my_base.h"
#include "my_compiler.h"
#include "my_dbug.h"
#include "my_inttypes.h"
#include "my_sys.h"
#include "mysql/components/services/psi_statement_bits.h"
#include "mysql_com.h"
#include "sql/debug_sync.h"
#include "sql/field.h"
#include "sql/handler.h"
#include "sql/item.h"
#include "sql/parse_tree_node_base.h"
#include "sql/protocol.h"
#include "sql/query_options.h"
#include "sql/query_result.h"
#include "sql/sql_digest_stream.h"
#include "sql/sql_lex.h"
#include "sql/sql_list.h"
#include "sql/sql_parse.h" // mysql_execute_command
#include "sql/sql_tmp_table.h" // tmp tables
#include "sql/sql_union.h" // Query_result_union
#include "sql/system_variables.h"
#include "sql/table.h"
/****************************************************************************
Declarations.
****************************************************************************/
/**
Materialized_cursor -- an insensitive materialized server-side
cursor. The result set of this cursor is saved in a temporary
table at open. The cursor itself is simply an interface for the
handler of the temporary table.
*/
class Materialized_cursor final : public Server_side_cursor {
/* A fake unit to supply to Query_result_send when fetching */
SELECT_LEX_UNIT fake_unit;
TABLE *table;
List<Item> item_list;
ulong fetch_limit;
ulong fetch_count;
bool is_rnd_inited;
public:
Materialized_cursor(Query_result *result, TABLE *table);
int send_result_set_metadata(THD *thd, List<Item> &send_result_set_metadata);
bool is_open() const override { return table != 0; }
int open(THD *, JOIN *) override;
bool fetch(ulong num_rows) override;
void close() override;
~Materialized_cursor() override;
};
/**
Query_result_materialize -- a mediator between a cursor query and the
protocol. In case we were not able to open a non-materialzed
cursor, it creates an internal temporary memory table, and inserts
all rows into it. If the table is in the Heap engine and if it reaches
maximum Heap table size, it's converted to a disk-based temporary
table. Later this table is used to create a Materialized_cursor.
*/
class Query_result_materialize final : public Query_result_union {
Query_result *result; /**< the result object of the caller (PS or SP) */
public:
Materialized_cursor *materialized_cursor;
Query_result_materialize(Query_result *result_arg)
: Query_result_union(), result(result_arg), materialized_cursor(0) {}
bool send_result_set_metadata(THD *thd, List<Item> &list,
uint flags) override;
void cleanup(THD *) override {
table = NULL; // Pass table object to Materialized_cursor
}
};
/**************************************************************************/
/**
Attempt to open a materialized cursor.
@param thd thread handle
@param[in] result result class of the caller used as a destination
for the rows fetched from the cursor
@param[out] pcursor a pointer to store a pointer to cursor in
@return Error status
@retval false -- the query has been successfully executed; in this case
pcursor may or may not contain a pointer to an open cursor.
@retval true -- an error, 'pcursor' has been left intact.
*/
bool mysql_open_cursor(THD *thd, Query_result *result,
Server_side_cursor **pcursor) {
sql_digest_state *parent_digest;
PSI_statement_locker *parent_locker;
Query_result *save_result;
Query_result_materialize *result_materialize;
LEX *lex = thd->lex;
if (!(result_materialize =
new (thd->mem_root) Query_result_materialize(result)))
return true;
save_result = lex->result;
lex->result = result_materialize;
parent_digest = thd->m_digest;
parent_locker = thd->m_statement_psi;
thd->m_digest = NULL;
thd->m_statement_psi = NULL;
bool rc = mysql_execute_command(thd);
thd->m_digest = parent_digest;
DEBUG_SYNC(thd, "after_table_close");
thd->m_statement_psi = parent_locker;
lex->result = save_result;
/*
Possible options here:
- a materialized cursor is open. In this case rc is 0 and
result_materialize->materialized is not NULL
- an error occurred during materialization.
result_materialize->materialized_cursor is not NULL, but rc != 0
- successful completion of mysql_execute_command without
a cursor: rc is 0, result_materialize->materialized_cursor is NULL.
This is possible if some command writes directly to the
network, bypassing Query_result mechanism. An example of
such command is SHOW VARIABLES or SHOW STATUS.
*/
if (rc) {
if (result_materialize->materialized_cursor) {
/* Rollback metadata in the client-server protocol. */
result_materialize->abort_result_set(thd);
delete result_materialize->materialized_cursor;
}
goto end;
}
if (result_materialize->materialized_cursor) {
Materialized_cursor *materialized_cursor =
result_materialize->materialized_cursor;
/*
NOTE: close_thread_tables() has been called in
mysql_execute_command(), so all tables except from the cursor
temporary table have been closed.
*/
if ((rc = materialized_cursor->open(thd, nullptr))) {
delete materialized_cursor;
goto end;
}
*pcursor = materialized_cursor;
}
end:
destroy(result_materialize);
return rc;
}
/****************************************************************************
Server_side_cursor
****************************************************************************/
Server_side_cursor::~Server_side_cursor() {}
void Server_side_cursor::operator delete(void *ptr,
size_t size MY_ATTRIBUTE((unused))) {
DBUG_ENTER("Server_side_cursor::operator delete");
Server_side_cursor *cursor = (Server_side_cursor *)ptr;
/*
If this cursor has never been opened, mem_root is empty. Otherwise,
mem_root is allocated on itself. In this case, it's important to move
it out before freeing, to avoid writing into freed memory during the
free process.
*/
MEM_ROOT own_root = std::move(*cursor->m_arena.mem_root);
TRASH(ptr, size);
free_root(&own_root, MYF(0));
DBUG_VOID_RETURN;
}
/***************************************************************************
Materialized_cursor
****************************************************************************/
Materialized_cursor::Materialized_cursor(Query_result *result_arg,
TABLE *table_arg)
: Server_side_cursor(&table_arg->s->mem_root, result_arg),
fake_unit(CTX_NONE),
table(table_arg),
fetch_limit(0),
fetch_count(0),
is_rnd_inited(0) {}
/**
Preserve the original metadata to be sent to the client.
Initiate sending of the original metadata to the client
(call Protocol::send_result_set_metadata()).
@param thd Thread identifier.
@param send_result_set_metadata List of fields that would be sent.
*/
int Materialized_cursor::send_result_set_metadata(
THD *thd, List<Item> &send_result_set_metadata) {
Query_arena backup_arena;
int rc;
List_iterator_fast<Item> it_org(send_result_set_metadata);
List_iterator_fast<Item> it_dst(item_list);
Item *item_org;
Item *item_dst;
thd->swap_query_arena(m_arena, &backup_arena);
if ((rc = table->fill_item_list(&item_list))) goto end;
DBUG_ASSERT(send_result_set_metadata.elements == item_list.elements);
/*
Unless we preserve the original metadata, it will be lost,
since new fields describe columns of the temporary table.
Allocate a copy of the name for safety only. Currently
items with original names are always kept in memory,
but in case this changes a memory leak may be hard to notice.
*/
while ((item_dst = it_dst++, item_org = it_org++)) {
Send_field send_field;
Item_ident *ident = static_cast<Item_ident *>(item_dst);
item_org->make_field(&send_field);
ident->db_name = thd->mem_strdup(send_field.db_name);
ident->table_name = thd->mem_strdup(send_field.table_name);
}
/*
Original metadata result set should be sent here. After
mysql_execute_command() is finished, item_list can not be used for
sending metadata, because it references closed table.
*/
rc =
result->send_result_set_metadata(thd, item_list, Protocol::SEND_NUM_ROWS);
end:
thd->swap_query_arena(backup_arena, &m_arena);
/* Check for thd->is_error() in case of OOM */
return rc || thd->is_error();
}
int Materialized_cursor::open(THD *thd, JOIN *) {
int rc;
Query_arena backup_arena;
thd->swap_query_arena(m_arena, &backup_arena);
/* Create a list of fields and start sequential scan. */
rc = result->prepare(thd, item_list, &fake_unit);
rc = !rc && table->file->ha_rnd_init(true);
is_rnd_inited = !rc;
thd->swap_query_arena(backup_arena, &m_arena);
/* Commit or rollback metadata in the client-server protocol. */
if (!rc) {
thd->server_status |= SERVER_STATUS_CURSOR_EXISTS;
result->send_eof(thd);
} else {
result->abort_result_set(thd);
}
return rc;
}
/**
Fetch up to the given number of rows from a materialized cursor.
Precondition: the cursor is open.
If the cursor points after the last row, the fetch will automatically
close the cursor and not send any data (except the 'EOF' packet
with SERVER_STATUS_LAST_ROW_SENT). This is an extra round trip
and probably should be improved to return
SERVER_STATUS_LAST_ROW_SENT along with the last row.
*/
bool Materialized_cursor::fetch(ulong num_rows) {
THD *thd = table->in_use;
int res = 0;
result->begin_dataset();
for (fetch_limit += num_rows; fetch_count < fetch_limit; fetch_count++) {
if ((res = table->file->ha_rnd_next(table->record[0]))) break;
/* Send data only if the read was successful. */
/*
If network write failed (i.e. due to a closed socked),
the error has already been set. Return true if the error
is set.
*/
if (result->send_data(thd, item_list)) return true;
}
switch (res) {
case 0:
thd->server_status |= SERVER_STATUS_CURSOR_EXISTS;
result->send_eof(thd);
break;
case HA_ERR_END_OF_FILE:
thd->server_status |= SERVER_STATUS_LAST_ROW_SENT;
result->send_eof(thd);
close();
break;
default:
table->file->print_error(res, MYF(0));
close();
return true;
}
return false;
}
void Materialized_cursor::close() {
/* Free item_list items */
m_arena.free_items();
if (is_rnd_inited) (void)table->file->ha_rnd_end();
/*
We need to grab table->mem_root to prevent free_tmp_table from freeing:
the cursor object was allocated in this memory. The mem_root will be
freed in Materialized_cursor::operator delete.
*/
m_arena.mem_root =
new (&table->s->mem_root) MEM_ROOT(std::move(table->s->mem_root));
free_tmp_table(table->in_use, table);
table = 0;
}
Materialized_cursor::~Materialized_cursor() {
if (is_open()) close();
}
/***************************************************************************
Query_result_materialize
****************************************************************************/
bool Query_result_materialize::send_result_set_metadata(THD *thd,
List<Item> &list,
uint) {
DBUG_ASSERT(table == 0);
if (create_result_table(thd, unit->get_field_list(), false,
thd->variables.option_bits | TMP_TABLE_ALL_COLUMNS,
"", false, true))
return true;
materialized_cursor =
new (&table->s->mem_root) Materialized_cursor(result, table);
if (!materialized_cursor) {
free_tmp_table(table->in_use, table);
table = 0;
return true;
}
if (materialized_cursor->send_result_set_metadata(thd, list)) {
delete materialized_cursor;
table = 0;
materialized_cursor = 0;
return true;
}
/*
close_thread_tables() will be called in mysql_execute_command() which
will close all tables except the cursor temporary table. Hence set the
orig_table in the field definition to NULL.
*/
for (Field **fld = this->table->field; *fld; fld++) (*fld)->orig_table = NULL;
return false;
}