You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

413 lines
15 KiB

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