1
/*****************************************************************************
3
Copyright (C) 1997, 2010, 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., 51 Franklin
15
St, Fifth Floor, Boston, MA 02110-1301 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
Prints a row in a select result.
110
@return query thread to run next or NULL */
115
que_thr_t* thr); /*!< in: query thread */
116
/****************************************************************//**
117
Converts a key value stored in MySQL format to an Innobase dtuple. The last
118
field of the key value may be just a prefix of a fixed length field: hence
119
the parameter key_len. But currently we do not allow search keys where the
120
last field is only a prefix of the full key field len and print a warning if
124
row_sel_convert_mysql_key_to_innobase(
125
/*==================================*/
126
dtuple_t* tuple, /*!< in/out: tuple where to build;
127
NOTE: we assume that the type info
128
in the tuple is already according
130
byte* buf, /*!< in: buffer to use in field
132
ulint buf_len, /*!< in: buffer length */
133
dict_index_t* index, /*!< in: index of the key value */
134
const byte* key_ptr, /*!< in: MySQL key value */
135
ulint key_len, /*!< in: MySQL key value length */
136
trx_t* trx); /*!< in: transaction */
137
/********************************************************************//**
138
Searches for rows in the database. This is used in the interface to
139
MySQL. This function opens a cursor, and also implements fetch next
140
and fetch prev. NOTE that if we do a search with a full key value
141
from a unique index (ROW_SEL_EXACT), then we will not store the cursor
142
position and fetch next or fetch prev must not be tried to the cursor!
143
@return DB_SUCCESS, DB_RECORD_NOT_FOUND, DB_END_OF_INDEX, DB_DEADLOCK,
144
DB_LOCK_TABLE_FULL, or DB_TOO_BIG_RECORD */
147
row_search_for_mysql(
148
/*=================*/
149
byte* buf, /*!< in/out: buffer for the fetched
150
row in the MySQL format */
151
ulint mode, /*!< in: search mode PAGE_CUR_L, ... */
152
row_prebuilt_t* prebuilt, /*!< in: prebuilt struct for the
153
table handle; this contains the info
154
of search_tuple, index; if search
155
tuple contains 0 fields then we
156
position the cursor at the start or
157
the end of the index, depending on
159
ulint match_mode, /*!< in: 0 or ROW_SEL_EXACT or
160
ROW_SEL_EXACT_PREFIX */
161
ulint direction); /*!< in: 0 or ROW_SEL_NEXT or
162
ROW_SEL_PREV; NOTE: if this is != 0,
163
then prebuilt must have a pcur
164
with stored position! In opening of a
165
cursor 'direction' should be 0. */
166
/*******************************************************************//**
167
Checks if MySQL at the moment is allowed for this table to retrieve a
168
consistent read result, or store it to the query cache.
169
@return TRUE if storing or retrieving from the query cache is permitted */
172
row_search_check_if_query_cache_permitted(
173
/*======================================*/
174
trx_t* trx, /*!< in: transaction object */
175
const char* norm_name); /*!< in: concatenation of database name,
176
'/' char, table name */
177
/*******************************************************************//**
178
Read the max AUTOINC value from an index.
179
@return DB_SUCCESS if all OK else error code */
182
row_search_max_autoinc(
183
/*===================*/
184
dict_index_t* index, /*!< in: index to search */
185
const char* col_name, /*!< in: autoinc column name */
186
ib_uint64_t* value); /*!< out: AUTOINC value read */
188
/** A structure for caching column values for prefetched rows */
189
struct sel_buf_struct{
190
byte* data; /*!< data, or NULL; if not NULL, this field
191
has allocated memory which must be explicitly
192
freed; can be != NULL even when len is
194
ulint len; /*!< data length or UNIV_SQL_NULL */
196
/*!< size of memory buffer allocated for data:
197
this can be more than len; this is defined
203
dict_table_t* table; /*!< table struct in the dictionary
205
dict_index_t* index; /*!< table index used in the search */
206
btr_pcur_t pcur; /*!< persistent cursor used to search
208
ibool asc; /*!< TRUE if cursor traveling upwards */
209
ibool pcur_is_open; /*!< TRUE if pcur has been positioned
210
and we can try to fetch new rows */
211
ibool cursor_at_end; /*!< TRUE if the cursor is open but
212
we know that there are no more
213
qualifying rows left to retrieve from
214
the index tree; NOTE though, that
215
there may still be unprocessed rows in
216
the prefetch stack; always FALSE when
217
pcur_is_open is FALSE */
218
ibool stored_cursor_rec_processed;
219
/*!< TRUE if the pcur position has been
220
stored and the record it is positioned
221
on has already been processed */
222
que_node_t** tuple_exps; /*!< array of expressions
223
which are used to calculate
224
the field values in the search
225
tuple: there is one expression
226
for each field in the search
228
dtuple_t* tuple; /*!< search tuple */
229
ulint mode; /*!< search mode: PAGE_CUR_G, ... */
230
ulint n_exact_match; /*!< number of first fields in
231
the search tuple which must be
233
ibool unique_search; /*!< TRUE if we are searching an
234
index record with a unique key */
235
ulint n_rows_fetched; /*!< number of rows fetched using pcur
236
after it was opened */
237
ulint n_rows_prefetched;/*!< number of prefetched rows cached
238
for fetch: fetching several rows in
239
the same mtr saves CPU time */
240
ulint first_prefetched;/*!< index of the first cached row in
241
select buffer arrays for each column */
242
ibool no_prefetch; /*!< no prefetch for this table */
243
sym_node_list_t columns; /*!< symbol table nodes for the columns
244
to retrieve from the table */
245
UT_LIST_BASE_NODE_T(func_node_t)
246
end_conds; /*!< conditions which determine the
247
fetch limit of the index segment we
248
have to look at: when one of these
249
fails, the result set has been
250
exhausted for the cursor in this
251
index; these conditions are normalized
252
so that in a comparison the column
253
for this table is the first argument */
254
UT_LIST_BASE_NODE_T(func_node_t)
255
other_conds; /*!< the rest of search conditions we can
256
test at this table in a join */
257
ibool must_get_clust; /*!< TRUE if index is a non-clustered
258
index and we must also fetch the
259
clustered index record; this is the
260
case if the non-clustered record does
261
not contain all the needed columns, or
262
if this is a single-table explicit
263
cursor, or a searched update or
265
ulint* clust_map; /*!< map telling how clust_ref is built
266
from the fields of a non-clustered
268
dtuple_t* clust_ref; /*!< the reference to the clustered
269
index entry is built here if index is
270
a non-clustered index */
271
btr_pcur_t clust_pcur; /*!< if index is non-clustered, we use
272
this pcur to search the clustered
274
mem_heap_t* old_vers_heap; /*!< memory heap used in building an old
275
version of a row, or NULL */
278
/** Select node states */
279
enum sel_node_state {
280
SEL_NODE_CLOSED, /*!< it is a declared cursor which is not
282
SEL_NODE_OPEN, /*!< intention locks not yet set on tables */
283
SEL_NODE_FETCH, /*!< intention locks have been set */
284
SEL_NODE_NO_MORE_ROWS /*!< cursor has reached the result set end */
287
/** Select statement node */
288
struct sel_node_struct{
289
que_common_t common; /*!< node type: QUE_NODE_SELECT */
291
state; /*!< node state */
292
que_node_t* select_list; /*!< select list */
293
sym_node_t* into_list; /*!< variables list or NULL */
294
sym_node_t* table_list; /*!< table list */
295
ibool asc; /*!< TRUE if the rows should be fetched
296
in an ascending order */
297
ibool set_x_locks; /*!< TRUE if the cursor is for update or
298
delete, which means that a row x-lock
299
should be placed on the cursor row */
300
ulint row_lock_mode; /*!< LOCK_X or LOCK_S */
301
ulint n_tables; /*!< number of tables */
302
ulint fetch_table; /*!< number of the next table to access
304
plan_t* plans; /*!< array of n_tables many plan nodes
305
containing the search plan and the
306
search data structures */
307
que_node_t* search_cond; /*!< search condition */
308
read_view_t* read_view; /*!< if the query is a non-locking
309
consistent read, its read view is
310
placed here, otherwise NULL */
311
ibool consistent_read;/*!< TRUE if the select is a consistent,
313
order_node_t* order_by; /*!< order by column definition, or
315
ibool is_aggregate; /*!< TRUE if the select list consists of
316
aggregate functions */
317
ibool aggregate_already_fetched;
318
/*!< TRUE if the aggregate row has
319
already been fetched for the current
321
ibool can_get_updated;/*!< this is TRUE if the select
322
is in a single-table explicit
323
cursor which can get updated
324
within the stored procedure,
325
or in a searched update or
326
delete; NOTE that to determine
327
of an explicit cursor if it
328
can get updated, the parser
329
checks from a stored procedure
330
if it contains positioned
331
update or delete statements */
332
sym_node_t* explicit_cursor;/*!< not NULL if an explicit cursor */
333
UT_LIST_BASE_NODE_T(sym_node_t)
334
copy_variables; /*!< variables whose values we have to
335
copy when an explicit cursor is opened,
336
so that they do not change between
340
/** Fetch statement node */
341
struct fetch_node_struct{
342
que_common_t common; /*!< type: QUE_NODE_FETCH */
343
sel_node_t* cursor_def; /*!< cursor definition */
344
sym_node_t* into_list; /*!< variables to set */
347
func; /*!< User callback function or NULL.
348
The first argument to the function
349
is a sel_node_t*, containing the
350
results of the SELECT operation for
351
one row. If the function returns
352
NULL, it is not interested in
353
further rows and the cursor is
354
modified so (cursor % NOTFOUND) is
355
true. If it returns not-NULL,
356
continue normally. See
357
row_fetch_print() for an example
358
(and a useful debugging tool). */
361
/** Open or close cursor operation type */
363
ROW_SEL_OPEN_CURSOR, /*!< open cursor */
364
ROW_SEL_CLOSE_CURSOR /*!< close cursor */
367
/** Open or close cursor statement node */
368
struct open_node_struct{
369
que_common_t common; /*!< type: QUE_NODE_OPEN */
371
op_type; /*!< operation type: open or
373
sel_node_t* cursor_def; /*!< cursor definition */
376
/** Row printf statement node */
377
struct row_printf_node_struct{
378
que_common_t common; /*!< type: QUE_NODE_ROW_PRINTF */
379
sel_node_t* sel_node; /*!< select */
382
/** Search direction for the MySQL interface */
383
enum row_sel_direction {
384
ROW_SEL_NEXT = 1, /*!< ascending direction */
385
ROW_SEL_PREV = 2 /*!< descending direction */
388
/** Match mode for the MySQL interface */
389
enum row_sel_match_mode {
390
ROW_SEL_EXACT = 1, /*!< search using a complete key value */
391
ROW_SEL_EXACT_PREFIX /*!< search using a key prefix which
392
must match rows: the prefix may
393
contain an incomplete field (the last
394
field in prefix may be just a prefix
395
of a fixed length column) */
399
#include "row0sel.ic"