1
/*****************************************************************************
3
Copyright (c) 1997, 2009, Innobase Oy. All Rights Reserved.
5
This program is free software; you can redistribute it and/or modify it under
6
the terms of the GNU General Public License as published by the Free Software
7
Foundation; version 2 of the License.
9
This program is distributed in the hope that it will be useful, but WITHOUT
10
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
13
You should have received a copy of the GNU General Public License along with
14
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15
Place, Suite 330, Boston, MA 02111-1307 USA
17
*****************************************************************************/
19
/**************************************************//**
20
@file include/row0sel.h
23
Created 12/19/1997 Heikki Tuuri
24
*******************************************************/
30
#include "data0data.h"
31
#include "que0types.h"
32
#include "dict0types.h"
33
#include "trx0types.h"
34
#include "row0types.h"
35
#include "que0types.h"
38
#include "read0read.h"
39
#include "row0mysql.h"
41
/*********************************************************************//**
42
Creates a select node struct.
43
@return own: select node struct */
48
mem_heap_t* heap); /*!< in: memory heap where created */
49
/*********************************************************************//**
50
Frees the memory private to a select node when a query graph is freed,
51
does not free the heap where the node was originally created. */
54
sel_node_free_private(
55
/*==================*/
56
sel_node_t* node); /*!< in: select node struct */
57
/*********************************************************************//**
58
Frees a prefetch buffer for a column, including the dynamically allocated
59
memory for data stored there. */
62
sel_col_prefetch_buf_free(
63
/*======================*/
64
sel_buf_t* prefetch_buf); /*!< in, own: prefetch buffer */
65
/*********************************************************************//**
66
Gets the plan node for the nth table in a join.
70
sel_node_get_nth_plan(
71
/*==================*/
72
sel_node_t* node, /*!< in: select node */
73
ulint i); /*!< in: get ith plan node */
74
/**********************************************************************//**
75
Performs a select step. This is a high-level function used in SQL execution
77
@return query thread to run next or NULL */
82
que_thr_t* thr); /*!< in: query thread */
83
/**********************************************************************//**
84
Performs an execution step of an open or close cursor statement node.
85
@return query thread to run next or NULL */
90
que_thr_t* thr); /*!< in: query thread */
91
/**********************************************************************//**
92
Performs a fetch for a cursor.
93
@return query thread to run next or NULL */
98
que_thr_t* thr); /*!< in: query thread */
99
/****************************************************************//**
100
Sample callback function for fetch that prints each row.
101
@return always returns non-NULL */
106
void* row, /*!< in: sel_node_t* */
107
void* user_arg); /*!< in: not used */
108
/****************************************************************//**
109
Callback function for fetch that stores an unsigned 4 byte integer to the
110
location pointed. The column's type must be DATA_INT, DATA_UNSIGNED, length
112
@return always returns NULL */
115
row_fetch_store_uint4(
116
/*==================*/
117
void* row, /*!< in: sel_node_t* */
118
void* user_arg); /*!< in: data pointer */
119
/***********************************************************//**
120
Prints a row in a select result.
121
@return query thread to run next or NULL */
126
que_thr_t* thr); /*!< in: query thread */
127
/****************************************************************//**
128
Converts a key value stored in MySQL format to an Innobase dtuple. The last
129
field of the key value may be just a prefix of a fixed length field: hence
130
the parameter key_len. But currently we do not allow search keys where the
131
last field is only a prefix of the full key field len and print a warning if
135
row_sel_convert_mysql_key_to_innobase(
136
/*==================================*/
137
dtuple_t* tuple, /*!< in/out: tuple where to build;
138
NOTE: we assume that the type info
139
in the tuple is already according
141
byte* buf, /*!< in: buffer to use in field
143
ulint buf_len, /*!< in: buffer length */
144
dict_index_t* index, /*!< in: index of the key value */
145
const byte* key_ptr, /*!< in: MySQL key value */
146
ulint key_len, /*!< in: MySQL key value length */
147
trx_t* trx); /*!< in: transaction */
148
/********************************************************************//**
149
Searches for rows in the database. This is used in the interface to
150
MySQL. This function opens a cursor, and also implements fetch next
151
and fetch prev. NOTE that if we do a search with a full key value
152
from a unique index (ROW_SEL_EXACT), then we will not store the cursor
153
position and fetch next or fetch prev must not be tried to the cursor!
154
@return DB_SUCCESS, DB_RECORD_NOT_FOUND, DB_END_OF_INDEX, DB_DEADLOCK,
155
DB_LOCK_TABLE_FULL, or DB_TOO_BIG_RECORD */
158
row_search_for_mysql(
159
/*=================*/
160
byte* buf, /*!< in/out: buffer for the fetched
161
row in the MySQL format */
162
ulint mode, /*!< in: search mode PAGE_CUR_L, ... */
163
row_prebuilt_t* prebuilt, /*!< in: prebuilt struct for the
164
table handle; this contains the info
165
of search_tuple, index; if search
166
tuple contains 0 fields then we
167
position the cursor at the start or
168
the end of the index, depending on
170
ulint match_mode, /*!< in: 0 or ROW_SEL_EXACT or
171
ROW_SEL_EXACT_PREFIX */
172
ulint direction); /*!< in: 0 or ROW_SEL_NEXT or
173
ROW_SEL_PREV; NOTE: if this is != 0,
174
then prebuilt must have a pcur
175
with stored position! In opening of a
176
cursor 'direction' should be 0. */
177
/*******************************************************************//**
178
Checks if MySQL at the moment is allowed for this table to retrieve a
179
consistent read result, or store it to the query cache.
180
@return TRUE if storing or retrieving from the query cache is permitted */
183
row_search_check_if_query_cache_permitted(
184
/*======================================*/
185
trx_t* trx, /*!< in: transaction object */
186
const char* norm_name); /*!< in: concatenation of database name,
187
'/' char, table name */
188
/*******************************************************************//**
189
Read the max AUTOINC value from an index.
190
@return DB_SUCCESS if all OK else error code */
193
row_search_max_autoinc(
194
/*===================*/
195
dict_index_t* index, /*!< in: index to search */
196
const char* col_name, /*!< in: autoinc column name */
197
ib_uint64_t* value); /*!< out: AUTOINC value read */
199
/** A structure for caching column values for prefetched rows */
200
struct sel_buf_struct{
201
byte* data; /*!< data, or NULL; if not NULL, this field
202
has allocated memory which must be explicitly
203
freed; can be != NULL even when len is
205
ulint len; /*!< data length or UNIV_SQL_NULL */
207
/*!< size of memory buffer allocated for data:
208
this can be more than len; this is defined
214
dict_table_t* table; /*!< table struct in the dictionary
216
dict_index_t* index; /*!< table index used in the search */
217
btr_pcur_t pcur; /*!< persistent cursor used to search
219
ibool asc; /*!< TRUE if cursor traveling upwards */
220
ibool pcur_is_open; /*!< TRUE if pcur has been positioned
221
and we can try to fetch new rows */
222
ibool cursor_at_end; /*!< TRUE if the cursor is open but
223
we know that there are no more
224
qualifying rows left to retrieve from
225
the index tree; NOTE though, that
226
there may still be unprocessed rows in
227
the prefetch stack; always FALSE when
228
pcur_is_open is FALSE */
229
ibool stored_cursor_rec_processed;
230
/*!< TRUE if the pcur position has been
231
stored and the record it is positioned
232
on has already been processed */
233
que_node_t** tuple_exps; /*!< array of expressions
234
which are used to calculate
235
the field values in the search
236
tuple: there is one expression
237
for each field in the search
239
dtuple_t* tuple; /*!< search tuple */
240
ulint mode; /*!< search mode: PAGE_CUR_G, ... */
241
ulint n_exact_match; /*!< number of first fields in
242
the search tuple which must be
244
ibool unique_search; /*!< TRUE if we are searching an
245
index record with a unique key */
246
ulint n_rows_fetched; /*!< number of rows fetched using pcur
247
after it was opened */
248
ulint n_rows_prefetched;/*!< number of prefetched rows cached
249
for fetch: fetching several rows in
250
the same mtr saves CPU time */
251
ulint first_prefetched;/*!< index of the first cached row in
252
select buffer arrays for each column */
253
ibool no_prefetch; /*!< no prefetch for this table */
254
sym_node_list_t columns; /*!< symbol table nodes for the columns
255
to retrieve from the table */
256
UT_LIST_BASE_NODE_T(func_node_t)
257
end_conds; /*!< conditions which determine the
258
fetch limit of the index segment we
259
have to look at: when one of these
260
fails, the result set has been
261
exhausted for the cursor in this
262
index; these conditions are normalized
263
so that in a comparison the column
264
for this table is the first argument */
265
UT_LIST_BASE_NODE_T(func_node_t)
266
other_conds; /*!< the rest of search conditions we can
267
test at this table in a join */
268
ibool must_get_clust; /*!< TRUE if index is a non-clustered
269
index and we must also fetch the
270
clustered index record; this is the
271
case if the non-clustered record does
272
not contain all the needed columns, or
273
if this is a single-table explicit
274
cursor, or a searched update or
276
ulint* clust_map; /*!< map telling how clust_ref is built
277
from the fields of a non-clustered
279
dtuple_t* clust_ref; /*!< the reference to the clustered
280
index entry is built here if index is
281
a non-clustered index */
282
btr_pcur_t clust_pcur; /*!< if index is non-clustered, we use
283
this pcur to search the clustered
285
mem_heap_t* old_vers_heap; /*!< memory heap used in building an old
286
version of a row, or NULL */
289
/** Select node states */
290
enum sel_node_state {
291
SEL_NODE_CLOSED, /*!< it is a declared cursor which is not
293
SEL_NODE_OPEN, /*!< intention locks not yet set on tables */
294
SEL_NODE_FETCH, /*!< intention locks have been set */
295
SEL_NODE_NO_MORE_ROWS /*!< cursor has reached the result set end */
298
/** Select statement node */
299
struct sel_node_struct{
300
que_common_t common; /*!< node type: QUE_NODE_SELECT */
302
state; /*!< node state */
303
que_node_t* select_list; /*!< select list */
304
sym_node_t* into_list; /*!< variables list or NULL */
305
sym_node_t* table_list; /*!< table list */
306
ibool asc; /*!< TRUE if the rows should be fetched
307
in an ascending order */
308
ibool set_x_locks; /*!< TRUE if the cursor is for update or
309
delete, which means that a row x-lock
310
should be placed on the cursor row */
311
ulint row_lock_mode; /*!< LOCK_X or LOCK_S */
312
ulint n_tables; /*!< number of tables */
313
ulint fetch_table; /*!< number of the next table to access
315
plan_t* plans; /*!< array of n_tables many plan nodes
316
containing the search plan and the
317
search data structures */
318
que_node_t* search_cond; /*!< search condition */
319
read_view_t* read_view; /*!< if the query is a non-locking
320
consistent read, its read view is
321
placed here, otherwise NULL */
322
ibool consistent_read;/*!< TRUE if the select is a consistent,
324
order_node_t* order_by; /*!< order by column definition, or
326
ibool is_aggregate; /*!< TRUE if the select list consists of
327
aggregate functions */
328
ibool aggregate_already_fetched;
329
/*!< TRUE if the aggregate row has
330
already been fetched for the current
332
ibool can_get_updated;/*!< this is TRUE if the select
333
is in a single-table explicit
334
cursor which can get updated
335
within the stored procedure,
336
or in a searched update or
337
delete; NOTE that to determine
338
of an explicit cursor if it
339
can get updated, the parser
340
checks from a stored procedure
341
if it contains positioned
342
update or delete statements */
343
sym_node_t* explicit_cursor;/*!< not NULL if an explicit cursor */
344
UT_LIST_BASE_NODE_T(sym_node_t)
345
copy_variables; /*!< variables whose values we have to
346
copy when an explicit cursor is opened,
347
so that they do not change between
351
/** Fetch statement node */
352
struct fetch_node_struct{
353
que_common_t common; /*!< type: QUE_NODE_FETCH */
354
sel_node_t* cursor_def; /*!< cursor definition */
355
sym_node_t* into_list; /*!< variables to set */
358
func; /*!< User callback function or NULL.
359
The first argument to the function
360
is a sel_node_t*, containing the
361
results of the SELECT operation for
362
one row. If the function returns
363
NULL, it is not interested in
364
further rows and the cursor is
365
modified so (cursor % NOTFOUND) is
366
true. If it returns not-NULL,
367
continue normally. See
368
row_fetch_print() for an example
369
(and a useful debugging tool). */
372
/** Open or close cursor operation type */
374
ROW_SEL_OPEN_CURSOR, /*!< open cursor */
375
ROW_SEL_CLOSE_CURSOR /*!< close cursor */
378
/** Open or close cursor statement node */
379
struct open_node_struct{
380
que_common_t common; /*!< type: QUE_NODE_OPEN */
382
op_type; /*!< operation type: open or
384
sel_node_t* cursor_def; /*!< cursor definition */
387
/** Row printf statement node */
388
struct row_printf_node_struct{
389
que_common_t common; /*!< type: QUE_NODE_ROW_PRINTF */
390
sel_node_t* sel_node; /*!< select */
393
/** Search direction for the MySQL interface */
394
enum row_sel_direction {
395
ROW_SEL_NEXT = 1, /*!< ascending direction */
396
ROW_SEL_PREV = 2 /*!< descending direction */
399
/** Match mode for the MySQL interface */
400
enum row_sel_match_mode {
401
ROW_SEL_EXACT = 1, /*!< search using a complete key value */
402
ROW_SEL_EXACT_PREFIX /*!< search using a key prefix which
403
must match rows: the prefix may
404
contain an incomplete field (the last
405
field in prefix may be just a prefix
406
of a fixed length column) */
410
#include "row0sel.ic"