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