1 /*****************************************************************************
2 
3 Copyright (c) 1997, 2017, Oracle and/or its affiliates. All Rights Reserved.
4 
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License, version 2.0,
7 as published by the Free Software Foundation.
8 
9 This program is also distributed with certain software (including
10 but not limited to OpenSSL) that is licensed under separate terms,
11 as designated in a particular file or component or in included license
12 documentation.  The authors of MySQL hereby grant you an additional
13 permission to link the program and your derivative works with the
14 separately licensed software that they have included with MySQL.
15 
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 GNU General Public License, version 2.0, for more details.
20 
21 You should have received a copy of the GNU General Public License along with
22 this program; if not, write to the Free Software Foundation, Inc.,
23 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
24 
25 *****************************************************************************/
26 
27 /**************************************************//**
28 @file include/row0sel.h
29 Select
30 
31 Created 12/19/1997 Heikki Tuuri
32 *******************************************************/
33 
34 #ifndef row0sel_h
35 #define row0sel_h
36 
37 #include "univ.i"
38 #include "data0data.h"
39 #include "que0types.h"
40 #include "dict0types.h"
41 #include "trx0types.h"
42 #include "row0types.h"
43 #include "que0types.h"
44 #include "pars0sym.h"
45 #include "btr0pcur.h"
46 #include "read0read.h"
47 #include "row0mysql.h"
48 
49 /*********************************************************************//**
50 Creates a select node struct.
51 @return	own: select node struct */
52 UNIV_INTERN
53 sel_node_t*
54 sel_node_create(
55 /*============*/
56 	mem_heap_t*	heap);	/*!< in: memory heap where created */
57 /*********************************************************************//**
58 Frees the memory private to a select node when a query graph is freed,
59 does not free the heap where the node was originally created. */
60 UNIV_INTERN
61 void
62 sel_node_free_private(
63 /*==================*/
64 	sel_node_t*	node);	/*!< in: select node struct */
65 /*********************************************************************//**
66 Frees a prefetch buffer for a column, including the dynamically allocated
67 memory for data stored there. */
68 UNIV_INTERN
69 void
70 sel_col_prefetch_buf_free(
71 /*======================*/
72 	sel_buf_t*	prefetch_buf);	/*!< in, own: prefetch buffer */
73 /*********************************************************************//**
74 Gets the plan node for the nth table in a join.
75 @return	plan node */
76 UNIV_INLINE
77 plan_t*
78 sel_node_get_nth_plan(
79 /*==================*/
80 	sel_node_t*	node,	/*!< in: select node */
81 	ulint		i);	/*!< in: get ith plan node */
82 /**********************************************************************//**
83 Performs a select step. This is a high-level function used in SQL execution
84 graphs.
85 @return	query thread to run next or NULL */
86 UNIV_INTERN
87 que_thr_t*
88 row_sel_step(
89 /*=========*/
90 	que_thr_t*	thr);	/*!< in: query thread */
91 /**********************************************************************//**
92 Performs an execution step of an open or close cursor statement node.
93 @return	query thread to run next or NULL */
94 UNIV_INLINE
95 que_thr_t*
96 open_step(
97 /*======*/
98 	que_thr_t*	thr);	/*!< in: query thread */
99 /**********************************************************************//**
100 Performs a fetch for a cursor.
101 @return	query thread to run next or NULL */
102 UNIV_INTERN
103 que_thr_t*
104 fetch_step(
105 /*=======*/
106 	que_thr_t*	thr);	/*!< in: query thread */
107 /****************************************************************//**
108 Sample callback function for fetch that prints each row.
109 @return	always returns non-NULL */
110 UNIV_INTERN
111 void*
112 row_fetch_print(
113 /*============*/
114 	void*	row,		/*!< in:  sel_node_t* */
115 	void*	user_arg);	/*!< in:  not used */
116 /***********************************************************//**
117 Prints a row in a select result.
118 @return	query thread to run next or NULL */
119 UNIV_INTERN
120 que_thr_t*
121 row_printf_step(
122 /*============*/
123 	que_thr_t*	thr);	/*!< in: query thread */
124 /****************************************************************//**
125 Converts a key value stored in MySQL format to an Innobase dtuple. The last
126 field of the key value may be just a prefix of a fixed length field: hence
127 the parameter key_len. But currently we do not allow search keys where the
128 last field is only a prefix of the full key field len and print a warning if
129 such appears. */
130 UNIV_INTERN
131 void
132 row_sel_convert_mysql_key_to_innobase(
133 /*==================================*/
134 	dtuple_t*	tuple,		/*!< in/out: tuple where to build;
135 					NOTE: we assume that the type info
136 					in the tuple is already according
137 					to index! */
138 	byte*		buf,		/*!< in: buffer to use in field
139 					conversions; NOTE that dtuple->data
140 					may end up pointing inside buf so
141 					do not discard that buffer while
142 					the tuple is being used. See
143 					row_mysql_store_col_in_innobase_format()
144 					in the case of DATA_INT */
145 	ulint		buf_len,	/*!< in: buffer length */
146 	dict_index_t*	index,		/*!< in: index of the key value */
147 	const byte*	key_ptr,	/*!< in: MySQL key value */
148 	ulint		key_len,	/*!< in: MySQL key value length */
149 	trx_t*		trx);		/*!< in: transaction */
150 /********************************************************************//**
151 Searches for rows in the database. This is used in the interface to
152 MySQL. This function opens a cursor, and also implements fetch next
153 and fetch prev. NOTE that if we do a search with a full key value
154 from a unique index (ROW_SEL_EXACT), then we will not store the cursor
155 position and fetch next or fetch prev must not be tried to the cursor!
156 @return DB_SUCCESS, DB_RECORD_NOT_FOUND, DB_END_OF_INDEX, DB_DEADLOCK,
157 DB_LOCK_TABLE_FULL, or DB_TOO_BIG_RECORD */
158 UNIV_INTERN
159 dberr_t
160 row_search_for_mysql(
161 /*=================*/
162 	byte*		buf,		/*!< in/out: buffer for the fetched
163 					row in the MySQL format */
164 	ulint		mode,		/*!< in: search mode PAGE_CUR_L, ... */
165 	row_prebuilt_t*	prebuilt,	/*!< in: prebuilt struct for the
166 					table handle; this contains the info
167 					of search_tuple, index; if search
168 					tuple contains 0 fields then we
169 					position the cursor at the start or
170 					the end of the index, depending on
171 					'mode' */
172 	ulint		match_mode,	/*!< in: 0 or ROW_SEL_EXACT or
173 					ROW_SEL_EXACT_PREFIX */
174 	ulint		direction)	/*!< in: 0 or ROW_SEL_NEXT or
175 					ROW_SEL_PREV; NOTE: if this is != 0,
176 					then prebuilt must have a pcur
177 					with stored position! In opening of a
178 					cursor 'direction' should be 0. */
179 	MY_ATTRIBUTE((nonnull, warn_unused_result));
180 /*******************************************************************//**
181 Checks if MySQL at the moment is allowed for this table to retrieve a
182 consistent read result, or store it to the query cache.
183 @return	TRUE if storing or retrieving from the query cache is permitted */
184 UNIV_INTERN
185 ibool
186 row_search_check_if_query_cache_permitted(
187 /*======================================*/
188 	trx_t*		trx,		/*!< in: transaction object */
189 	const char*	norm_name);	/*!< in: concatenation of database name,
190 					'/' char, table name */
191 /*******************************************************************//**
192 Read the max AUTOINC value from an index.
193 @return	DB_SUCCESS if all OK else error code */
194 UNIV_INTERN
195 dberr_t
196 row_search_max_autoinc(
197 /*===================*/
198 	dict_index_t*	index,		/*!< in: index to search */
199 	const char*	col_name,	/*!< in: autoinc column name */
200 	ib_uint64_t*	value)		/*!< out: AUTOINC value read */
201 	MY_ATTRIBUTE((nonnull, warn_unused_result));
202 
203 /** A structure for caching column values for prefetched rows */
204 struct sel_buf_t{
205 	byte*		data;	/*!< data, or NULL; if not NULL, this field
206 				has allocated memory which must be explicitly
207 				freed; can be != NULL even when len is
208 				UNIV_SQL_NULL */
209 	ulint		len;	/*!< data length or UNIV_SQL_NULL */
210 	ulint		val_buf_size;
211 				/*!< size of memory buffer allocated for data:
212 				this can be more than len; this is defined
213 				when data != NULL */
214 };
215 
216 /** Copy used fields from cached row.
217 Copy cache record field by field, don't touch fields that
218 are not covered by current key.
219 @param[out]     buf             Where to copy the MySQL row.
220 @param[in]      cached_rec      What to copy (in MySQL row format).
221 @param[in]      prebuilt        prebuilt struct. */
222 void
223 row_sel_copy_cached_fields_for_mysql(
224         byte*           buf,
225         const byte*     cached_rec,
226         row_prebuilt_t* prebuilt);
227 
228 /** Query plan */
229 struct plan_t{
230 	dict_table_t*	table;		/*!< table struct in the dictionary
231 					cache */
232 	dict_index_t*	index;		/*!< table index used in the search */
233 	btr_pcur_t	pcur;		/*!< persistent cursor used to search
234 					the index */
235 	ibool		asc;		/*!< TRUE if cursor traveling upwards */
236 	ibool		pcur_is_open;	/*!< TRUE if pcur has been positioned
237 					and we can try to fetch new rows */
238 	ibool		cursor_at_end;	/*!< TRUE if the cursor is open but
239 					we know that there are no more
240 					qualifying rows left to retrieve from
241 					the index tree; NOTE though, that
242 					there may still be unprocessed rows in
243 					the prefetch stack; always FALSE when
244 					pcur_is_open is FALSE */
245 	ibool		stored_cursor_rec_processed;
246 					/*!< TRUE if the pcur position has been
247 					stored and the record it is positioned
248 					on has already been processed */
249 	que_node_t**	tuple_exps;	/*!< array of expressions
250 					which are used to calculate
251 					the field values in the search
252 					tuple: there is one expression
253 					for each field in the search
254 					tuple */
255 	dtuple_t*	tuple;		/*!< search tuple */
256 	ulint		mode;		/*!< search mode: PAGE_CUR_G, ... */
257 	ulint		n_exact_match;	/*!< number of first fields in
258 					the search tuple which must be
259 					exactly matched */
260 	ibool		unique_search;	/*!< TRUE if we are searching an
261 					index record with a unique key */
262 	ulint		n_rows_fetched;	/*!< number of rows fetched using pcur
263 					after it was opened */
264 	ulint		n_rows_prefetched;/*!< number of prefetched rows cached
265 					for fetch: fetching several rows in
266 					the same mtr saves CPU time */
267 	ulint		first_prefetched;/*!< index of the first cached row in
268 					select buffer arrays for each column */
269 	ibool		no_prefetch;	/*!< no prefetch for this table */
270 	sym_node_list_t	columns;	/*!< symbol table nodes for the columns
271 					to retrieve from the table */
272 	UT_LIST_BASE_NODE_T(func_node_t)
273 			end_conds;	/*!< conditions which determine the
274 					fetch limit of the index segment we
275 					have to look at: when one of these
276 					fails, the result set has been
277 					exhausted for the cursor in this
278 					index; these conditions are normalized
279 					so that in a comparison the column
280 					for this table is the first argument */
281 	UT_LIST_BASE_NODE_T(func_node_t)
282 			other_conds;	/*!< the rest of search conditions we can
283 					test at this table in a join */
284 	ibool		must_get_clust;	/*!< TRUE if index is a non-clustered
285 					index and we must also fetch the
286 					clustered index record; this is the
287 					case if the non-clustered record does
288 					not contain all the needed columns, or
289 					if this is a single-table explicit
290 					cursor, or a searched update or
291 					delete */
292 	ulint*		clust_map;	/*!< map telling how clust_ref is built
293 					from the fields of a non-clustered
294 					record */
295 	dtuple_t*	clust_ref;	/*!< the reference to the clustered
296 					index entry is built here if index is
297 					a non-clustered index */
298 	btr_pcur_t	clust_pcur;	/*!< if index is non-clustered, we use
299 					this pcur to search the clustered
300 					index */
301 	mem_heap_t*	old_vers_heap;	/*!< memory heap used in building an old
302 					version of a row, or NULL */
303 };
304 
305 /** Select node states */
306 enum sel_node_state {
307 	SEL_NODE_CLOSED,	/*!< it is a declared cursor which is not
308 				currently open */
309 	SEL_NODE_OPEN,		/*!< intention locks not yet set on tables */
310 	SEL_NODE_FETCH,		/*!< intention locks have been set */
311 	SEL_NODE_NO_MORE_ROWS	/*!< cursor has reached the result set end */
312 };
313 
314 /** Select statement node */
315 struct sel_node_t{
316 	que_common_t	common;		/*!< node type: QUE_NODE_SELECT */
317 	enum sel_node_state
318 			state;	/*!< node state */
319 	que_node_t*	select_list;	/*!< select list */
320 	sym_node_t*	into_list;	/*!< variables list or NULL */
321 	sym_node_t*	table_list;	/*!< table list */
322 	ibool		asc;		/*!< TRUE if the rows should be fetched
323 					in an ascending order */
324 	ibool		set_x_locks;	/*!< TRUE if the cursor is for update or
325 					delete, which means that a row x-lock
326 					should be placed on the cursor row */
327 	ulint		row_lock_mode;	/*!< LOCK_X or LOCK_S */
328 	ulint		n_tables;	/*!< number of tables */
329 	ulint		fetch_table;	/*!< number of the next table to access
330 					in the join */
331 	plan_t*		plans;		/*!< array of n_tables many plan nodes
332 					containing the search plan and the
333 					search data structures */
334 	que_node_t*	search_cond;	/*!< search condition */
335 	read_view_t*	read_view;	/*!< if the query is a non-locking
336 					consistent read, its read view is
337 					placed here, otherwise NULL */
338 	ibool		consistent_read;/*!< TRUE if the select is a consistent,
339 					non-locking read */
340 	order_node_t*	order_by;	/*!< order by column definition, or
341 					NULL */
342 	ibool		is_aggregate;	/*!< TRUE if the select list consists of
343 					aggregate functions */
344 	ibool		aggregate_already_fetched;
345 					/*!< TRUE if the aggregate row has
346 					already been fetched for the current
347 					cursor */
348 	ibool		can_get_updated;/*!< this is TRUE if the select
349 					is in a single-table explicit
350 					cursor which can get updated
351 					within the stored procedure,
352 					or in a searched update or
353 					delete; NOTE that to determine
354 					of an explicit cursor if it
355 					can get updated, the parser
356 					checks from a stored procedure
357 					if it contains positioned
358 					update or delete statements */
359 	sym_node_t*	explicit_cursor;/*!< not NULL if an explicit cursor */
360 	UT_LIST_BASE_NODE_T(sym_node_t)
361 			copy_variables; /*!< variables whose values we have to
362 					copy when an explicit cursor is opened,
363 					so that they do not change between
364 					fetches */
365 };
366 
367 /** Fetch statement node */
368 struct fetch_node_t{
369 	que_common_t	common;		/*!< type: QUE_NODE_FETCH */
370 	sel_node_t*	cursor_def;	/*!< cursor definition */
371 	sym_node_t*	into_list;	/*!< variables to set */
372 
373 	pars_user_func_t*
374 			func;		/*!< User callback function or NULL.
375 					The first argument to the function
376 					is a sel_node_t*, containing the
377 					results of the SELECT operation for
378 					one row. If the function returns
379 					NULL, it is not interested in
380 					further rows and the cursor is
381 					modified so (cursor % NOTFOUND) is
382 					true. If it returns not-NULL,
383 					continue normally. See
384 					row_fetch_print() for an example
385 					(and a useful debugging tool). */
386 };
387 
388 /** Open or close cursor operation type */
389 enum open_node_op {
390 	ROW_SEL_OPEN_CURSOR,	/*!< open cursor */
391 	ROW_SEL_CLOSE_CURSOR	/*!< close cursor */
392 };
393 
394 /** Open or close cursor statement node */
395 struct open_node_t{
396 	que_common_t	common;		/*!< type: QUE_NODE_OPEN */
397 	enum open_node_op
398 			op_type;	/*!< operation type: open or
399 					close cursor */
400 	sel_node_t*	cursor_def;	/*!< cursor definition */
401 };
402 
403 /** Row printf statement node */
404 struct row_printf_node_t{
405 	que_common_t	common;		/*!< type: QUE_NODE_ROW_PRINTF */
406 	sel_node_t*	sel_node;	/*!< select */
407 };
408 
409 /** Search direction for the MySQL interface */
410 enum row_sel_direction {
411 	ROW_SEL_NEXT = 1,	/*!< ascending direction */
412 	ROW_SEL_PREV = 2	/*!< descending direction */
413 };
414 
415 /** Match mode for the MySQL interface */
416 enum row_sel_match_mode {
417 	ROW_SEL_EXACT = 1,	/*!< search using a complete key value */
418 	ROW_SEL_EXACT_PREFIX	/*!< search using a key prefix which
419 				must match rows: the prefix may
420 				contain an incomplete field (the last
421 				field in prefix may be just a prefix
422 				of a fixed length column) */
423 };
424 
425 #ifndef UNIV_NONINL
426 #include "row0sel.ic"
427 #endif
428 
429 #endif
430