row0sel.c
上传用户:tsgydb
上传日期:2007-04-14
资源大小:10674k
文件大小:66k
源码类别:

MySQL数据库

开发平台:

Visual C++

  1. /*******************************************************
  2. Select
  3. (c) 1997 Innobase Oy
  4. Created 12/19/1997 Heikki Tuuri
  5. *******************************************************/
  6. #include "row0sel.h"
  7. #ifdef UNIV_NONINL
  8. #include "row0sel.ic"
  9. #endif
  10. #include "dict0dict.h"
  11. #include "dict0boot.h"
  12. #include "trx0undo.h"
  13. #include "trx0trx.h"
  14. #include "btr0btr.h"
  15. #include "btr0cur.h"
  16. #include "btr0sea.h"
  17. #include "mach0data.h"
  18. #include "que0que.h"
  19. #include "row0upd.h"
  20. #include "row0row.h"
  21. #include "row0vers.h"
  22. #include "rem0cmp.h"
  23. #include "lock0lock.h"
  24. #include "eval0eval.h"
  25. #include "pars0sym.h"
  26. #include "pars0pars.h"
  27. #include "row0mysql.h"
  28. /* Maximum number of rows to prefetch; MySQL interface has another parameter */
  29. #define SEL_MAX_N_PREFETCH 16
  30. /* Number of rows fetched, after which to start prefetching; MySQL interface
  31. has another parameter */
  32. #define SEL_PREFETCH_LIMIT 1
  33. /* When a select has accessed about this many pages, it returns control back
  34. to que_run_threads: this is to allow canceling runaway queries */
  35. #define SEL_COST_LIMIT 100
  36. /* Flags for search shortcut */
  37. #define SEL_FOUND 0
  38. #define SEL_EXHAUSTED 1
  39. #define SEL_RETRY 2
  40. /*************************************************************************
  41. Creates a select node struct. */
  42. sel_node_t*
  43. sel_node_create(
  44. /*============*/
  45. /* out, own: select node struct */
  46. mem_heap_t* heap) /* in: memory heap where created */
  47. {
  48. sel_node_t* node;
  49. node = mem_heap_alloc(heap, sizeof(sel_node_t));
  50. node->common.type = QUE_NODE_SELECT;
  51. node->state = SEL_NODE_OPEN;
  52. node->select_will_do_update = FALSE;
  53. node->latch_mode = BTR_SEARCH_LEAF;
  54. node->plans = NULL;
  55. return(node);
  56. }
  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. void
  61. sel_node_free_private(
  62. /*==================*/
  63. sel_node_t* node) /* in: select node struct */
  64. {
  65. ulint i;
  66. plan_t* plan;
  67. if (node->plans != NULL) {
  68. for (i = 0; i < node->n_tables; i++) {
  69. plan = sel_node_get_nth_plan(node, i);
  70. btr_pcur_close(&(plan->pcur));
  71. btr_pcur_close(&(plan->clust_pcur));
  72. if (plan->old_vers_heap) {
  73. mem_heap_free(plan->old_vers_heap);
  74. }
  75. }
  76. }
  77. }
  78. /*************************************************************************
  79. Evaluates the values in a select list. If there are aggregate functions,
  80. their argument value is added to the aggregate total. */
  81. UNIV_INLINE
  82. void
  83. sel_eval_select_list(
  84. /*=================*/
  85. sel_node_t* node) /* in: select node */
  86. {
  87. que_node_t* exp;
  88. exp = node->select_list;
  89. while (exp) {
  90. eval_exp(exp);
  91. exp = que_node_get_next(exp);
  92. }
  93. }
  94. /*************************************************************************
  95. Assigns the values in the select list to the possible into-variables in
  96. SELECT ... INTO ... */
  97. UNIV_INLINE
  98. void
  99. sel_assign_into_var_values(
  100. /*=======================*/
  101. sym_node_t* var, /* in: first variable in a list of variables */
  102. sel_node_t* node) /* in: select node */
  103. {
  104. que_node_t* exp;
  105. if (var == NULL) {
  106. return;
  107. }
  108. exp = node->select_list;
  109. while (var) {
  110. ut_ad(exp);
  111. eval_node_copy_val(var->alias, exp);
  112. exp = que_node_get_next(exp);
  113. var = que_node_get_next(var);
  114. }
  115. }
  116. /*************************************************************************
  117. Resets the aggregate value totals in the select list of an aggregate type
  118. query. */
  119. UNIV_INLINE
  120. void
  121. sel_reset_aggregate_vals(
  122. /*=====================*/
  123. sel_node_t* node) /* in: select node */
  124. {
  125. func_node_t* func_node;
  126. ut_ad(node->is_aggregate);
  127. func_node = node->select_list;
  128. while (func_node) {
  129. eval_node_set_int_val(func_node, 0);
  130. func_node = que_node_get_next(func_node);
  131. }
  132. node->aggregate_already_fetched = FALSE;
  133. }
  134. /*************************************************************************
  135. Copies the input variable values when an explicit cursor is opened. */
  136. UNIV_INLINE
  137. void
  138. row_sel_copy_input_variable_vals(
  139. /*=============================*/
  140. sel_node_t* node) /* in: select node */
  141. {
  142. sym_node_t* var;
  143. var = UT_LIST_GET_FIRST(node->copy_variables);
  144. while (var) {
  145. eval_node_copy_val(var, var->alias);
  146. var->indirection = NULL;
  147. var = UT_LIST_GET_NEXT(col_var_list, var);
  148. }
  149. }
  150. /*************************************************************************
  151. Fetches the column values from a record. */
  152. static
  153. void
  154. row_sel_fetch_columns(
  155. /*==================*/
  156. dict_index_t* index, /* in: record index */
  157. rec_t* rec, /* in: record in a clustered or non-clustered
  158. index */
  159. sym_node_t* column) /* in: first column in a column list, or
  160. NULL */
  161. {
  162. dfield_t* val;
  163. ulint index_type;
  164. ulint field_no;
  165. byte* data;
  166. ulint len;
  167. if (index->type & DICT_CLUSTERED) {
  168. index_type = SYM_CLUST_FIELD_NO;
  169. } else {
  170. index_type = SYM_SEC_FIELD_NO;
  171. }
  172. while (column) {
  173. field_no = column->field_nos[index_type];
  174. if (field_no != ULINT_UNDEFINED) {
  175. data = rec_get_nth_field(rec, field_no, &len);
  176. if (column->copy_val) {
  177. eval_node_copy_and_alloc_val(column, data,
  178. len);
  179. } else {
  180. val = que_node_get_val(column);
  181. dfield_set_data(val, data, len);
  182. }
  183. }
  184. column = UT_LIST_GET_NEXT(col_var_list, column);
  185. }
  186. }
  187. /*************************************************************************
  188. Allocates a prefetch buffer for a column when prefetch is first time done. */
  189. static
  190. void
  191. sel_col_prefetch_buf_alloc(
  192. /*=======================*/
  193. sym_node_t* column) /* in: symbol table node for a column */
  194. {
  195. sel_buf_t* sel_buf;
  196. ulint i;
  197. ut_ad(que_node_get_type(column) == QUE_NODE_SYMBOL);
  198. column->prefetch_buf = mem_alloc(SEL_MAX_N_PREFETCH
  199. * sizeof(sel_buf_t));
  200. for (i = 0; i < SEL_MAX_N_PREFETCH; i++) {
  201. sel_buf = column->prefetch_buf + i;
  202. sel_buf->data = NULL;
  203. sel_buf->val_buf_size = 0;
  204. }
  205. }
  206. /*************************************************************************
  207. Frees a prefetch buffer for a column, including the dynamically allocated
  208. memory for data stored there. */
  209. void
  210. sel_col_prefetch_buf_free(
  211. /*======================*/
  212. sel_buf_t* prefetch_buf) /* in, own: prefetch buffer */
  213. {
  214. sel_buf_t* sel_buf;
  215. ulint i;
  216. for (i = 0; i < SEL_MAX_N_PREFETCH; i++) {
  217. sel_buf = prefetch_buf + i;
  218. if (sel_buf->val_buf_size > 0) {
  219. mem_free(sel_buf->data);
  220. }
  221. }
  222. }
  223. /*************************************************************************
  224. Pops the column values for a prefetched, cached row from the column prefetch
  225. buffers and places them to the val fields in the column nodes. */
  226. static
  227. void
  228. sel_pop_prefetched_row(
  229. /*===================*/
  230. plan_t* plan) /* in: plan node for a table */
  231. {
  232. sym_node_t* column;
  233. sel_buf_t* sel_buf;
  234. dfield_t* val;
  235. byte* data;
  236. ulint len;
  237. ulint val_buf_size;
  238. ut_ad(plan->n_rows_prefetched > 0);
  239. column = UT_LIST_GET_FIRST(plan->columns);
  240. while (column) {
  241. val = que_node_get_val(column);
  242. if (!column->copy_val) {
  243. /* We did not really push any value for the
  244. column */
  245. ut_ad(!column->prefetch_buf);
  246. ut_ad(que_node_get_val_buf_size(column) == 0);
  247. #ifdef UNIV_DEBUG
  248. dfield_set_data(val, NULL, 0);
  249. #endif
  250. goto next_col;
  251. }
  252. ut_ad(column->prefetch_buf);
  253. sel_buf = column->prefetch_buf + plan->first_prefetched;
  254. data = sel_buf->data;
  255. len = sel_buf->len;
  256. val_buf_size = sel_buf->val_buf_size;
  257. /* We must keep track of the allocated memory for
  258. column values to be able to free it later: therefore
  259. we swap the values for sel_buf and val */
  260. sel_buf->data = dfield_get_data(val);
  261. sel_buf->len = dfield_get_len(val);
  262. sel_buf->val_buf_size = que_node_get_val_buf_size(column);
  263. dfield_set_data(val, data, len);
  264. que_node_set_val_buf_size(column, val_buf_size);
  265. next_col:
  266. column = UT_LIST_GET_NEXT(col_var_list, column);
  267. }
  268. plan->n_rows_prefetched--;
  269. plan->first_prefetched++;
  270. }
  271. /*************************************************************************
  272. Pushes the column values for a prefetched, cached row to the column prefetch
  273. buffers from the val fields in the column nodes. */
  274. UNIV_INLINE
  275. void
  276. sel_push_prefetched_row(
  277. /*====================*/
  278. plan_t* plan) /* in: plan node for a table */
  279. {
  280. sym_node_t* column;
  281. sel_buf_t* sel_buf;
  282. dfield_t* val;
  283. byte* data;
  284. ulint len;
  285. ulint pos;
  286. ulint val_buf_size;
  287. if (plan->n_rows_prefetched == 0) {
  288. pos = 0;
  289. plan->first_prefetched = 0;
  290. } else {
  291. pos = plan->n_rows_prefetched;
  292. /* We have the convention that pushing new rows starts only
  293. after the prefetch stack has been emptied: */
  294. ut_ad(plan->first_prefetched == 0);
  295. }
  296. plan->n_rows_prefetched++;
  297. ut_ad(pos < SEL_MAX_N_PREFETCH);
  298. column = UT_LIST_GET_FIRST(plan->columns);
  299. while (column) {
  300. if (!column->copy_val) {
  301. /* There is no sense to push pointers to database
  302. page fields when we do not keep latch on the page! */
  303. goto next_col;
  304. }
  305. if (!column->prefetch_buf) {
  306. /* Allocate a new prefetch buffer */
  307. sel_col_prefetch_buf_alloc(column);
  308. }
  309. sel_buf = column->prefetch_buf + pos;
  310. val = que_node_get_val(column);
  311. data = dfield_get_data(val);
  312. len = dfield_get_len(val);
  313. val_buf_size = que_node_get_val_buf_size(column);
  314. /* We must keep track of the allocated memory for
  315. column values to be able to free it later: therefore
  316. we swap the values for sel_buf and val */
  317. dfield_set_data(val, sel_buf->data, sel_buf->len);
  318. que_node_set_val_buf_size(column, sel_buf->val_buf_size);
  319. sel_buf->data = data;
  320. sel_buf->len = len;
  321. sel_buf->val_buf_size = val_buf_size;
  322. next_col:
  323. column = UT_LIST_GET_NEXT(col_var_list, column);
  324. }
  325. }
  326. /*************************************************************************
  327. Builds a previous version of a clustered index record for a consistent read */
  328. static
  329. ulint
  330. row_sel_build_prev_vers(
  331. /*====================*/
  332. /* out: DB_SUCCESS or error code */
  333. read_view_t* read_view, /* in: read view */
  334. plan_t* plan, /* in: plan node for table */
  335. rec_t* rec, /* in: record in a clustered index */
  336. rec_t** old_vers, /* out: old version, or NULL if the
  337. record does not exist in the view:
  338. i.e., it was freshly inserted
  339. afterwards */
  340. mtr_t* mtr) /* in: mtr */
  341. {
  342. ulint err;
  343. if (plan->old_vers_heap) {
  344. mem_heap_empty(plan->old_vers_heap);
  345. } else {
  346. plan->old_vers_heap = mem_heap_create(512);
  347. }
  348. err = row_vers_build_for_consistent_read(rec, mtr, plan->index,
  349. read_view, plan->old_vers_heap,
  350. old_vers);
  351. return(err);
  352. }
  353. /*************************************************************************
  354. Tests the conditions which determine when the index segment we are searching
  355. through has been exhausted. */
  356. UNIV_INLINE
  357. ibool
  358. row_sel_test_end_conds(
  359. /*===================*/
  360.   /* out: TRUE if row passed the tests */
  361. plan_t* plan) /* in: plan for the table; the column values must
  362. already have been retrieved and the right sides of
  363. comparisons evaluated */
  364. {
  365. func_node_t* cond;
  366. /* All conditions in end_conds are comparisons of a column to an
  367. expression */
  368. cond = UT_LIST_GET_FIRST(plan->end_conds);
  369. while (cond) {
  370. /* Evaluate the left side of the comparison, i.e., get the
  371. column value if there is an indirection */
  372. eval_sym(cond->args);
  373. /* Do the comparison */
  374. if (!eval_cmp(cond)) {
  375. return(FALSE);
  376. }
  377. cond = UT_LIST_GET_NEXT(cond_list, cond);
  378. }
  379. return(TRUE);
  380. }
  381. /*************************************************************************
  382. Tests the other conditions. */
  383. UNIV_INLINE
  384. ibool
  385. row_sel_test_other_conds(
  386. /*=====================*/
  387. /* out: TRUE if row passed the tests */
  388. plan_t* plan) /* in: plan for the table; the column values must
  389. already have been retrieved */
  390. {
  391. func_node_t* cond;
  392. cond = UT_LIST_GET_FIRST(plan->other_conds);
  393. while (cond) {
  394. eval_exp(cond);
  395. if (!eval_node_get_ibool_val(cond)) {
  396. return(FALSE);
  397. }
  398. cond = UT_LIST_GET_NEXT(cond_list, cond);
  399. }
  400. return(TRUE);
  401. }
  402. /*************************************************************************
  403. Retrieves the clustered index record corresponding to a record in a
  404. non-clustered index. Does the necessary locking. */
  405. static
  406. ulint
  407. row_sel_get_clust_rec(
  408. /*==================*/
  409. /* out: DB_SUCCESS or error code */
  410. sel_node_t* node, /* in: select_node */
  411. plan_t* plan, /* in: plan node for table */
  412. rec_t* rec, /* in: record in a non-clustered index */
  413. que_thr_t* thr, /* in: query thread */
  414. rec_t** out_rec,/* out: clustered record or an old version of
  415. it, NULL if the old version did not exist
  416. in the read view, i.e., it was a fresh
  417. inserted version */
  418. mtr_t* mtr) /* in: mtr used to get access to the
  419. non-clustered record; the same mtr is used to
  420. access the clustered index */
  421. {
  422. dict_index_t* index;
  423. rec_t* clust_rec;
  424. rec_t* old_vers;
  425. ulint err;
  426. row_build_row_ref_fast(plan->clust_ref, plan->clust_map, rec);
  427. index = dict_table_get_first_index(plan->table);
  428. btr_pcur_open_with_no_init(index, plan->clust_ref, PAGE_CUR_LE,
  429. node->latch_mode, &(plan->clust_pcur),
  430. 0, mtr);
  431. clust_rec = btr_pcur_get_rec(&(plan->clust_pcur));
  432. ut_ad(page_rec_is_user_rec(clust_rec));
  433. if (!node->read_view) {
  434. /* Try to place a lock on the index record */
  435. err = lock_clust_rec_read_check_and_lock(0, clust_rec, index,
  436. node->row_lock_mode, thr);
  437. if (err != DB_SUCCESS) {
  438. return(err);
  439. }
  440. } else {
  441. /* This is a non-locking consistent read: if necessary, fetch
  442. a previous version of the record */
  443. if (!lock_clust_rec_cons_read_sees(clust_rec, index,
  444. node->read_view)) {
  445. err = row_sel_build_prev_vers(node->read_view, plan,
  446. clust_rec, &old_vers, mtr);
  447. if (err != DB_SUCCESS) {
  448. return(err);
  449. }
  450. clust_rec = old_vers;
  451. if (clust_rec == NULL) {
  452. *out_rec = clust_rec;
  453. return(DB_SUCCESS);
  454. }
  455. }
  456. }
  457. /* Fetch the columns needed in test conditions */
  458. row_sel_fetch_columns(index, clust_rec,
  459. UT_LIST_GET_FIRST(plan->columns));
  460. *out_rec = clust_rec;
  461. return(DB_SUCCESS);
  462. }
  463. /*************************************************************************
  464. Sets a lock on a record. */
  465. UNIV_INLINE
  466. ulint
  467. sel_set_rec_lock(
  468. /*=============*/
  469. /* out: DB_SUCCESS or error code */
  470. rec_t* rec, /* in: record */
  471. dict_index_t* index, /* in: index */
  472. ulint mode, /* in: lock mode */
  473. que_thr_t* thr) /* in: query thread */
  474. {
  475. ulint err;
  476. if (index->type & DICT_CLUSTERED) {
  477. err = lock_clust_rec_read_check_and_lock(0, rec, index, mode,
  478. thr);
  479. } else {
  480. err = lock_sec_rec_read_check_and_lock(0, rec, index, mode,
  481. thr);
  482. }
  483. return(err);
  484. }
  485. /*************************************************************************
  486. Opens a pcur to a table index. */
  487. static
  488. void
  489. row_sel_open_pcur(
  490. /*==============*/
  491. sel_node_t* node, /* in: select node */
  492. plan_t* plan, /* in: table plan */
  493. ibool search_latch_locked,
  494. /* in: TRUE if the thread currently
  495. has the search latch locked in
  496. s-mode */
  497. mtr_t* mtr) /* in: mtr */
  498. {
  499. dict_index_t* index;
  500. func_node_t* cond;
  501. que_node_t* exp;
  502. ulint n_fields;
  503. ulint has_search_latch = 0; /* RW_S_LATCH or 0 */ 
  504. ulint i;
  505. if (search_latch_locked) {
  506. has_search_latch = RW_S_LATCH;
  507. }
  508. index = plan->index;
  509. /* Calculate the value of the search tuple: the exact match columns
  510. get their expressions evaluated when we evaluate the right sides of
  511. end_conds */
  512. cond = UT_LIST_GET_FIRST(plan->end_conds);
  513. while (cond) {
  514. eval_exp(que_node_get_next(cond->args));
  515. cond = UT_LIST_GET_NEXT(cond_list, cond);
  516. }
  517. if (plan->tuple) {
  518. n_fields = dtuple_get_n_fields(plan->tuple);
  519. if (plan->n_exact_match < n_fields) {
  520. /* There is a non-exact match field which must be
  521. evaluated separately */
  522. eval_exp(plan->tuple_exps[n_fields - 1]);
  523. }
  524. for (i = 0; i < n_fields; i++) {
  525. exp = plan->tuple_exps[i];
  526. dfield_copy_data(dtuple_get_nth_field(plan->tuple, i),
  527. que_node_get_val(exp));
  528. }
  529. /* Open pcur to the index */
  530. btr_pcur_open_with_no_init(index, plan->tuple, plan->mode,
  531. node->latch_mode, &(plan->pcur),
  532. has_search_latch, mtr);
  533. } else {
  534. /* Open the cursor to the start or the end of the index
  535. (FALSE: no init) */
  536. btr_pcur_open_at_index_side(plan->asc, index, node->latch_mode,
  537. &(plan->pcur), FALSE, mtr);
  538. }
  539. ut_ad(plan->n_rows_prefetched == 0);
  540. ut_ad(plan->n_rows_fetched == 0);
  541. ut_ad(plan->cursor_at_end == FALSE);
  542.  
  543. plan->pcur_is_open = TRUE;
  544. }
  545. /*************************************************************************
  546. Restores a stored pcur position to a table index. */
  547. UNIV_INLINE
  548. ibool
  549. row_sel_restore_pcur_pos(
  550. /*=====================*/
  551. /* out: TRUE if the cursor should be moved to
  552. the next record after we return from this
  553. function (moved to the previous, in the case
  554. of a descending cursor) without processing
  555. again the current cursor record */
  556. sel_node_t* node, /* in: select node */
  557. plan_t* plan, /* in: table plan */
  558. mtr_t* mtr) /* in: mtr */
  559. {
  560. ibool equal_position;
  561. ulint relative_position;
  562. ut_ad(!plan->cursor_at_end);
  563. relative_position = btr_pcur_get_rel_pos(&(plan->pcur));
  564. equal_position = btr_pcur_restore_position(node->latch_mode,
  565. &(plan->pcur), mtr);
  566. /* If the cursor is traveling upwards, and relative_position is
  567. (1) BTR_PCUR_BEFORE: this is not allowed, as we did not have a lock
  568. yet on the successor of the page infimum;
  569. (2) BTR_PCUR_AFTER: btr_pcur_restore_position placed the cursor on the
  570. first record GREATER than the predecessor of a page supremum; we have
  571. not yet processed the cursor record: no need to move the cursor to the
  572. next record;
  573. (3) BTR_PCUR_ON: btr_pcur_restore_position placed the cursor on the
  574. last record LESS or EQUAL to the old stored user record; (a) if
  575. equal_position is FALSE, this means that the cursor is now on a record
  576. less than the old user record, and we must move to the next record;
  577. (b) if equal_position is TRUE, then if
  578. plan->stored_cursor_rec_processed is TRUE, we must move to the next
  579. record, else there is no need to move the cursor. */
  580. if (plan->asc) {
  581. if (relative_position == BTR_PCUR_ON) {
  582. if (equal_position) {
  583. return(plan->stored_cursor_rec_processed);
  584. }
  585. return(TRUE);
  586. }
  587. ut_ad(relative_position == BTR_PCUR_AFTER);
  588. return(FALSE);
  589. }
  590. /* If the cursor is traveling downwards, and relative_position is
  591. (1) BTR_PCUR_BEFORE: btr_pcur_restore_position placed the cursor on
  592. the last record LESS than the successor of a page infimum; we have not
  593. processed the cursor record: no need to move the cursor;
  594. (2) BTR_PCUR_AFTER: btr_pcur_restore_position placed the cursor on the
  595. first record GREATER than the predecessor of a page supremum; we have
  596. processed the cursor record: we should move the cursor to the previous
  597. record;
  598. (3) BTR_PCUR_ON: btr_pcur_restore_position placed the cursor on the
  599. last record LESS or EQUAL to the old stored user record; (a) if
  600. equal_position is FALSE, this means that the cursor is now on a record
  601. less than the old user record, and we need not move to the previous
  602. record; (b) if equal_position is TRUE, then if
  603. plan->stored_cursor_rec_processed is TRUE, we must move to the previous
  604. record, else there is no need to move the cursor. */
  605. if (relative_position == BTR_PCUR_BEFORE) {
  606. return(FALSE);
  607. }
  608. if (relative_position == BTR_PCUR_ON) {
  609. if (equal_position) {
  610. return(plan->stored_cursor_rec_processed);
  611. }
  612. return(FALSE);
  613. }
  614. ut_ad(relative_position == BTR_PCUR_AFTER);
  615. return(TRUE);
  616. }
  617. /*************************************************************************
  618. Resets a plan cursor to a closed state. */
  619. UNIV_INLINE
  620. void
  621. plan_reset_cursor(
  622. /*==============*/
  623. plan_t* plan) /* in: plan */
  624. {
  625. plan->pcur_is_open = FALSE;
  626. plan->cursor_at_end = FALSE;
  627. plan->n_rows_fetched = 0;
  628. plan->n_rows_prefetched = 0;
  629. }
  630. /*************************************************************************
  631. Tries to do a shortcut to fetch a clustered index record with a unique key,
  632. using the hash index if possible (not always). */
  633. static
  634. ulint
  635. row_sel_try_search_shortcut(
  636. /*========================*/
  637. /* out: SEL_FOUND, SEL_EXHAUSTED, SEL_RETRY */
  638. sel_node_t* node, /* in: select node for a consistent read */
  639. plan_t* plan, /* in: plan for a unique search in clustered
  640. index */
  641. mtr_t* mtr) /* in: mtr */
  642. {
  643. dict_index_t* index;
  644. rec_t* rec;
  645. index = plan->index;
  646. ut_ad(node->read_view);
  647. ut_ad(plan->unique_search);
  648. ut_ad(!plan->must_get_clust);
  649. ut_ad(rw_lock_own(&btr_search_latch, RW_LOCK_SHARED));
  650. row_sel_open_pcur(node, plan, TRUE, mtr);
  651. rec = btr_pcur_get_rec(&(plan->pcur));
  652. if (!page_rec_is_user_rec(rec)) {
  653. return(SEL_RETRY);
  654. }
  655. ut_ad(plan->mode == PAGE_CUR_GE);
  656. /* As the cursor is now placed on a user record after a search with
  657. the mode PAGE_CUR_GE, the up_match field in the cursor tells how many
  658. fields in the user record matched to the search tuple */ 
  659. if (btr_pcur_get_up_match(&(plan->pcur)) < plan->n_exact_match) {
  660. return(SEL_EXHAUSTED);
  661. }
  662. /* This is a non-locking consistent read: if necessary, fetch
  663. a previous version of the record */
  664. if (index->type & DICT_CLUSTERED) {
  665. if (!lock_clust_rec_cons_read_sees(rec, index,
  666. node->read_view)) {
  667. return(SEL_RETRY);
  668. }
  669. } else if (!lock_sec_rec_cons_read_sees(rec, index, node->read_view)) {
  670. return(SEL_RETRY);
  671. }
  672. /* Test deleted flag. Fetch the columns needed in test conditions. */
  673. row_sel_fetch_columns(index, rec, UT_LIST_GET_FIRST(plan->columns));
  674. if (rec_get_deleted_flag(rec)) {
  675. return(SEL_EXHAUSTED);
  676. }
  677. /* Test the rest of search conditions */
  678. if (!row_sel_test_other_conds(plan)) {
  679. return(SEL_EXHAUSTED);
  680. }
  681. ut_ad(plan->pcur.latch_mode == node->latch_mode);
  682. plan->n_rows_fetched++;
  683. return(SEL_FOUND);
  684. }
  685. /*************************************************************************
  686. Performs a select step. */
  687. static
  688. ulint
  689. row_sel(
  690. /*====*/
  691. /* out: DB_SUCCESS or error code */
  692. sel_node_t* node, /* in: select node */
  693. que_thr_t* thr) /* in: query thread */
  694. {
  695. dict_index_t* index;
  696. plan_t* plan;
  697. mtr_t mtr;
  698. ibool moved;
  699. rec_t* rec;
  700. rec_t* old_vers;
  701. rec_t* clust_rec;
  702. ibool search_latch_locked;
  703. ibool consistent_read;
  704. /* The following flag becomes TRUE when we are doing a
  705. consistent read from a non-clustered index and we must look
  706. at the clustered index to find out the previous delete mark
  707. state of the non-clustered record: */
  708. ibool cons_read_requires_clust_rec = FALSE;
  709. ulint cost_counter = 0;
  710. ibool cursor_just_opened;
  711. ibool must_go_to_next;
  712. ibool leaf_contains_updates  = FALSE;
  713. /* TRUE if select_will_do_update is
  714. TRUE and the current clustered index
  715. leaf page has been updated during
  716. the current mtr: mtr must be committed
  717. at the same time as the leaf x-latch
  718. is released */
  719. ibool mtr_has_extra_clust_latch  = FALSE;
  720. /* TRUE if the search was made using
  721. a non-clustered index, and we had to
  722. access the clustered record: now &mtr
  723. contains a clustered index latch, and
  724. &mtr must be committed before we move
  725. to the next non-clustered record */
  726. ulint found_flag;
  727. ulint err;
  728. ut_ad(thr->run_node == node);
  729. search_latch_locked = FALSE;
  730. if (node->read_view) {
  731. /* In consistent reads, we try to do with the hash index and
  732. not to use the buffer page get. This is to reduce memory bus
  733. load resulting from semaphore operations. The search latch
  734. will be s-locked when we access an index with a unique search
  735. condition, but not locked when we access an index with a
  736. less selective search condition. */
  737. consistent_read = TRUE;
  738. } else {
  739. consistent_read = FALSE;
  740. }
  741. table_loop:
  742. /* TABLE LOOP
  743.    ----------
  744. This is the outer major loop in calculating a join. We come here when
  745. node->fetch_table changes, and after adding a row to aggregate totals
  746. and, of course, when this function is called. */
  747. ut_ad(leaf_contains_updates == FALSE);
  748. ut_ad(mtr_has_extra_clust_latch == FALSE);
  749. plan = sel_node_get_nth_plan(node, node->fetch_table);
  750. index = plan->index;
  751. if (plan->n_rows_prefetched > 0) {
  752. sel_pop_prefetched_row(plan);
  753. goto next_table_no_mtr;
  754. }
  755. if (plan->cursor_at_end) {
  756. /* The cursor has already reached the result set end: no more
  757. rows to process for this table cursor, as also the prefetch
  758. stack was empty */
  759. ut_ad(plan->pcur_is_open);
  760. goto table_exhausted_no_mtr;
  761. }
  762. /* Open a cursor to index, or restore an open cursor position */
  763. mtr_start(&mtr);
  764. if (consistent_read && plan->unique_search && !plan->pcur_is_open
  765. && !plan->must_get_clust) {
  766. if (!search_latch_locked) {
  767. rw_lock_s_lock(&btr_search_latch);
  768. search_latch_locked = TRUE;
  769. } else if (btr_search_latch.writer_is_wait_ex) {
  770. /* There is an x-latch request waiting: release the
  771. s-latch for a moment; as an s-latch here is often
  772. kept for some 10 searches before being released,
  773. a waiting x-latch request would block other threads
  774. from acquiring an s-latch for a long time, lowering
  775. performance significantly in multiprocessors. */
  776. rw_lock_s_unlock(&btr_search_latch);
  777. rw_lock_s_lock(&btr_search_latch);
  778. }
  779. found_flag = row_sel_try_search_shortcut(node, plan, &mtr);
  780. if (found_flag == SEL_FOUND) {
  781. goto next_table;
  782. } else if (found_flag == SEL_EXHAUSTED) {
  783. goto table_exhausted;
  784. }
  785. ut_ad(found_flag == SEL_RETRY);
  786. plan_reset_cursor(plan);
  787. mtr_commit(&mtr);
  788. mtr_start(&mtr);
  789. }
  790. if (search_latch_locked) {
  791. rw_lock_s_unlock(&btr_search_latch);
  792. search_latch_locked = FALSE;
  793. }
  794. if (!plan->pcur_is_open) {
  795. /* Evaluate the expressions to build the search tuple and
  796. open the cursor */
  797. row_sel_open_pcur(node, plan, search_latch_locked, &mtr);
  798. cursor_just_opened = TRUE;
  799. /* A new search was made: increment the cost counter */
  800. cost_counter++;
  801. } else {
  802. /* Restore pcur position to the index */
  803. must_go_to_next = row_sel_restore_pcur_pos(node, plan, &mtr);
  804. cursor_just_opened = FALSE;
  805. if (must_go_to_next) {
  806. /* We have already processed the cursor record: move
  807. to the next */
  808. goto next_rec;
  809. }
  810. }
  811. rec_loop:
  812. /* RECORD LOOP
  813.    -----------
  814. In this loop we use pcur and try to fetch a qualifying row, and
  815. also fill the prefetch buffer for this table if n_rows_fetched has
  816. exceeded a threshold. While we are inside this loop, the following
  817. holds:
  818. (1) &mtr is started,
  819. (2) pcur is positioned and open.
  820. NOTE that if cursor_just_opened is TRUE here, it means that we came
  821. to this point right after row_sel_open_pcur. */
  822. ut_ad(mtr_has_extra_clust_latch == FALSE);
  823. rec = btr_pcur_get_rec(&(plan->pcur));
  824. /* PHASE 1: Set a lock if specified */
  825. if (!node->asc && cursor_just_opened
  826. && (rec != page_get_supremum_rec(buf_frame_align(rec)))) {
  827. /* When we open a cursor for a descending search, we must set
  828. a next-key lock on the successor record: otherwise it would
  829. be possible to insert new records next to the cursor position,
  830. and it might be that these new records should appear in the
  831. search result set, resulting in the phantom problem. */
  832. if (!consistent_read) {
  833. err = sel_set_rec_lock(page_rec_get_next(rec), index,
  834. node->row_lock_mode, thr);
  835. if (err != DB_SUCCESS) {
  836. /* Note that in this case we will store in pcur
  837. the PREDECESSOR of the record we are waiting
  838. the lock for */
  839. goto lock_wait_or_error;
  840. }
  841. }
  842. }
  843. if (rec == page_get_infimum_rec(buf_frame_align(rec))) {
  844. /* The infimum record on a page cannot be in the result set,
  845. and neither can a record lock be placed on it: we skip such
  846. a record. We also increment the cost counter as we may have
  847. processed yet another page of index. */
  848. cost_counter++;
  849. goto next_rec;
  850. }
  851. if (!consistent_read) {
  852. /* Try to place a lock on the index record */
  853. err = sel_set_rec_lock(rec, index, node->row_lock_mode, thr);
  854. if (err != DB_SUCCESS) {
  855. goto lock_wait_or_error;
  856. }
  857. }
  858. if (rec == page_get_supremum_rec(buf_frame_align(rec))) {
  859. /* A page supremum record cannot be in the result set: skip
  860. it now when we have placed a possible lock on it */
  861. goto next_rec;
  862. }
  863. ut_ad(page_rec_is_user_rec(rec));
  864. if (cost_counter > SEL_COST_LIMIT) {
  865. /* Now that we have placed the necessary locks, we can stop
  866. for a while and store the cursor position; NOTE that if we
  867. would store the cursor position BEFORE placing a record lock,
  868. it might happen that the cursor would jump over some records
  869. that another transaction could meanwhile insert adjacent to
  870. the cursor: this would result in the phantom problem. */
  871. goto stop_for_a_while;
  872. }
  873. /* PHASE 2: Check a mixed index mix id if needed */
  874. if (plan->unique_search && cursor_just_opened) {
  875. ut_ad(plan->mode == PAGE_CUR_GE);
  876. /* As the cursor is now placed on a user record after a search
  877. with the mode PAGE_CUR_GE, the up_match field in the cursor
  878. tells how many fields in the user record matched to the search
  879. tuple */ 
  880. if (btr_pcur_get_up_match(&(plan->pcur))
  881. < plan->n_exact_match) {
  882. goto table_exhausted;
  883. }
  884. /* Ok, no need to test end_conds or mix id */
  885. } else if (plan->mixed_index) {
  886.      /* We have to check if the record in a mixed cluster belongs
  887.      to this table */
  888.   if (!dict_is_mixed_table_rec(plan->table, rec)) {
  889.      goto next_rec;
  890.      }
  891. }
  892. /* We are ready to look at a possible new index entry in the result
  893. set: the cursor is now placed on a user record */
  894. /* PHASE 3: Get previous version in a consistent read */
  895. if (consistent_read) {
  896. /* This is a non-locking consistent read: if necessary, fetch
  897. a previous version of the record */
  898. if (index->type & DICT_CLUSTERED) {
  899. if (!lock_clust_rec_cons_read_sees(rec, index,
  900. node->read_view)) {
  901. err = row_sel_build_prev_vers(node->read_view,
  902. plan, rec, &old_vers,
  903. &mtr);
  904. if (err != DB_SUCCESS) {
  905. goto lock_wait_or_error;
  906. }
  907. if (old_vers == NULL) {
  908. row_sel_fetch_columns(index, rec,
  909.     UT_LIST_GET_FIRST(plan->columns));
  910. if (!row_sel_test_end_conds(plan)) {
  911. goto table_exhausted;
  912. }
  913. goto next_rec;
  914. }
  915. rec = old_vers;
  916. }
  917. } else if (!lock_sec_rec_cons_read_sees(rec, index,
  918. node->read_view)) {
  919. cons_read_requires_clust_rec = TRUE;
  920. }
  921. }
  922. /* PHASE 4: Test search end conditions and deleted flag */
  923. /* Fetch the columns needed in test conditions */
  924. row_sel_fetch_columns(index, rec, UT_LIST_GET_FIRST(plan->columns));
  925. /* Test the selection end conditions: these can only contain columns
  926. which already are found in the index, even though the index might be
  927. non-clustered */
  928. if (plan->unique_search && cursor_just_opened) {
  929. /* No test necessary: the test was already made above */
  930. } else if (!row_sel_test_end_conds(plan)) {
  931. goto table_exhausted;
  932. }
  933. if (rec_get_deleted_flag(rec) && !cons_read_requires_clust_rec) {
  934. /* The record is delete marked: we can skip it if this is
  935. not a consistent read which might see an earlier version
  936. of a non-clustered index record */
  937. if (plan->unique_search) {
  938. goto table_exhausted;
  939. }
  940. goto next_rec;
  941. }
  942. /* PHASE 5: Get the clustered index record, if needed and if we did
  943. not do the search using the clustered index */
  944. if (plan->must_get_clust || cons_read_requires_clust_rec) {
  945. /* It was a non-clustered index and we must fetch also the
  946. clustered index record */
  947. err = row_sel_get_clust_rec(node, plan, rec, thr, &clust_rec,
  948. &mtr);
  949. mtr_has_extra_clust_latch = TRUE;
  950. if (err != DB_SUCCESS) {
  951. goto lock_wait_or_error;
  952. }
  953. /* Retrieving the clustered record required a search:
  954. increment the cost counter */
  955. cost_counter++;
  956. if (clust_rec == NULL) {
  957. /* The record did not exist in the read view */
  958. ut_ad(consistent_read);
  959. goto next_rec;
  960. }
  961. if (rec_get_deleted_flag(clust_rec)) {
  962. /* The record is delete marked: we can skip it */
  963. goto next_rec;
  964. }
  965. if (node->can_get_updated) {
  966. btr_pcur_store_position(&(plan->clust_pcur), &mtr);
  967. }
  968. }
  969. /* PHASE 6: Test the rest of search conditions */
  970. if (!row_sel_test_other_conds(plan)) {
  971. if (plan->unique_search) {
  972. goto table_exhausted;
  973. }
  974. goto next_rec;
  975. }
  976. /* PHASE 7: We found a new qualifying row for the current table; push
  977. the row if prefetch is on, or move to the next table in the join */
  978. plan->n_rows_fetched++;
  979. ut_ad(plan->pcur.latch_mode == node->latch_mode);
  980. if (node->select_will_do_update) {
  981. /* This is a searched update and we can do the update in-place,
  982. saving CPU time */
  983. row_upd_in_place_in_select(node, thr, &mtr);
  984. leaf_contains_updates = TRUE;
  985. /* When the database is in the online backup mode, the number
  986. of log records for a single mtr should be small: increment the
  987. cost counter to ensure it */
  988. cost_counter += 1 + (SEL_COST_LIMIT / 8);
  989. if (plan->unique_search) {
  990. goto table_exhausted;
  991. }
  992. goto next_rec;
  993. }
  994. if ((plan->n_rows_fetched <= SEL_PREFETCH_LIMIT)
  995. || plan->unique_search || plan->no_prefetch) {
  996. /* No prefetch in operation: go to the next table */
  997. goto next_table;
  998. }
  999. sel_push_prefetched_row(plan);
  1000. if (plan->n_rows_prefetched == SEL_MAX_N_PREFETCH) {
  1001. /* The prefetch buffer is now full */
  1002. sel_pop_prefetched_row(plan);
  1003. goto next_table;
  1004. }
  1005. next_rec:
  1006. ut_ad(!search_latch_locked);
  1007. if (mtr_has_extra_clust_latch) {
  1008. /* We must commit &mtr if we are moving to the next
  1009. non-clustered index record, because we could break the
  1010. latching order if we would access a different clustered
  1011. index page right away without releasing the previous. */
  1012. goto commit_mtr_for_a_while;
  1013. }
  1014. if (leaf_contains_updates
  1015. && btr_pcur_is_after_last_on_page(&(plan->pcur), &mtr)) {
  1016. /* We must commit &mtr if we are moving to a different page,
  1017. because we have done updates to the x-latched leaf page, and
  1018. the latch would be released in btr_pcur_move_to_next, without
  1019. &mtr getting committed there */
  1020. ut_ad(node->asc);
  1021. goto commit_mtr_for_a_while;
  1022. }
  1023. if (node->asc) {
  1024. moved = btr_pcur_move_to_next(&(plan->pcur), &mtr);
  1025. } else {
  1026. moved = btr_pcur_move_to_prev(&(plan->pcur), &mtr);
  1027. }
  1028. if (!moved) {
  1029. goto table_exhausted;
  1030. }
  1031. cursor_just_opened = FALSE;
  1032. /* END OF RECORD LOOP
  1033.    ------------------ */
  1034. goto rec_loop;
  1035. next_table:
  1036. /* We found a record which satisfies the conditions: we can move to
  1037. the next table or return a row in the result set */
  1038. ut_ad(btr_pcur_is_on_user_rec(&(plan->pcur), &mtr));
  1039. if (plan->unique_search && !node->can_get_updated) {
  1040. plan->cursor_at_end = TRUE;
  1041. } else {
  1042. ut_ad(!search_latch_locked);
  1043. plan->stored_cursor_rec_processed = TRUE;
  1044. btr_pcur_store_position(&(plan->pcur), &mtr);
  1045. }
  1046. mtr_commit(&mtr);
  1047. leaf_contains_updates = FALSE;
  1048. mtr_has_extra_clust_latch = FALSE;
  1049. next_table_no_mtr:
  1050. /* If we use 'goto' to this label, it means that the row was popped
  1051. from the prefetched rows stack, and &mtr is already committed */
  1052. if (node->fetch_table + 1 == node->n_tables) {
  1053. sel_eval_select_list(node);
  1054. if (node->is_aggregate) {
  1055. goto table_loop;
  1056. }
  1057. sel_assign_into_var_values(node->into_list, node);
  1058. thr->run_node = que_node_get_parent(node);
  1059. if (search_latch_locked) {
  1060. rw_lock_s_unlock(&btr_search_latch);
  1061. }
  1062. return(DB_SUCCESS);
  1063. }
  1064. node->fetch_table++;
  1065. /* When we move to the next table, we first reset the plan cursor:
  1066. we do not care about resetting it when we backtrack from a table */
  1067. plan_reset_cursor(sel_node_get_nth_plan(node, node->fetch_table));
  1068. goto table_loop;
  1069. table_exhausted:
  1070. /* The table cursor pcur reached the result set end: backtrack to the
  1071. previous table in the join if we do not have cached prefetched rows */
  1072. plan->cursor_at_end = TRUE;
  1073. mtr_commit(&mtr);
  1074. leaf_contains_updates = FALSE;
  1075. mtr_has_extra_clust_latch = FALSE;
  1076. if (plan->n_rows_prefetched > 0) {
  1077. /* The table became exhausted during a prefetch */
  1078. sel_pop_prefetched_row(plan);
  1079. goto next_table_no_mtr;
  1080. }
  1081. table_exhausted_no_mtr:
  1082. if (node->fetch_table == 0) {
  1083. if (node->is_aggregate && !node->aggregate_already_fetched) {
  1084. node->aggregate_already_fetched = TRUE;
  1085. sel_assign_into_var_values(node->into_list, node);
  1086. thr->run_node = que_node_get_parent(node);
  1087. if (search_latch_locked) {
  1088. rw_lock_s_unlock(&btr_search_latch);
  1089. }
  1090. return(DB_SUCCESS);
  1091. }
  1092. node->state = SEL_NODE_NO_MORE_ROWS;
  1093. thr->run_node = que_node_get_parent(node);
  1094. if (search_latch_locked) {
  1095. rw_lock_s_unlock(&btr_search_latch);
  1096. }
  1097. return(DB_SUCCESS);
  1098. }
  1099. node->fetch_table--;
  1100. goto table_loop;
  1101. stop_for_a_while:
  1102. /* Return control for a while to que_run_threads, so that runaway
  1103. queries can be canceled. NOTE that when we come here, we must, in a
  1104. locking read, have placed the necessary (possibly waiting request)
  1105. record lock on the cursor record or its successor: when we reposition
  1106. the cursor, this record lock guarantees that nobody can meanwhile have
  1107. inserted new records which should have appeared in the result set,
  1108. which would result in the phantom problem. */ 
  1109. ut_ad(!search_latch_locked);
  1110. plan->stored_cursor_rec_processed = FALSE;
  1111. btr_pcur_store_position(&(plan->pcur), &mtr);
  1112. mtr_commit(&mtr);
  1113. ut_ad(sync_thread_levels_empty_gen(TRUE));
  1114. return(DB_SUCCESS);
  1115. commit_mtr_for_a_while:
  1116. /* Stores the cursor position and commits &mtr; this is used if
  1117. &mtr may contain latches which would break the latching order if
  1118. &mtr would not be committed and the latches released. */ 
  1119. plan->stored_cursor_rec_processed = TRUE;
  1120. ut_ad(!search_latch_locked);
  1121. btr_pcur_store_position(&(plan->pcur), &mtr);
  1122. mtr_commit(&mtr);
  1123. leaf_contains_updates = FALSE;
  1124. mtr_has_extra_clust_latch = FALSE;
  1125. ut_ad(sync_thread_levels_empty_gen(TRUE));
  1126. goto table_loop;
  1127. lock_wait_or_error:
  1128. /* See the note at stop_for_a_while: the same holds for this case */
  1129. ut_ad(!btr_pcur_is_before_first_on_page(&(plan->pcur), &mtr)
  1130. || !node->asc);
  1131. ut_ad(!search_latch_locked);
  1132. plan->stored_cursor_rec_processed = FALSE;
  1133. btr_pcur_store_position(&(plan->pcur), &mtr);
  1134. mtr_commit(&mtr);
  1135. ut_ad(sync_thread_levels_empty_gen(TRUE));
  1136. return(err);
  1137. }
  1138. /**************************************************************************
  1139. Performs a select step. This is a high-level function used in SQL execution
  1140. graphs. */
  1141. que_thr_t*
  1142. row_sel_step(
  1143. /*=========*/
  1144. /* out: query thread to run next or NULL */
  1145. que_thr_t* thr) /* in: query thread */
  1146. {
  1147. ulint i_lock_mode;
  1148. sym_node_t* table_node;
  1149. sel_node_t* node;
  1150. ulint err;
  1151. ut_ad(thr);
  1152. node = thr->run_node;
  1153. ut_ad(que_node_get_type(node) == QUE_NODE_SELECT);
  1154. /* If this is a new time this node is executed (or when execution
  1155. resumes after wait for a table intention lock), set intention locks
  1156. on the tables, or assign a read view */
  1157. if (node->into_list && (thr->prev_node == que_node_get_parent(node))) {
  1158. node->state = SEL_NODE_OPEN;
  1159. }
  1160. if (node->state == SEL_NODE_OPEN) {
  1161. /* It may be that the current session has not yet started
  1162. its transaction, or it has been committed: */
  1163. trx_start_if_not_started(thr_get_trx(thr));
  1164. plan_reset_cursor(sel_node_get_nth_plan(node, 0));
  1165. if (node->consistent_read) {
  1166. /* Assign a read view for the query */
  1167. node->read_view = trx_assign_read_view(
  1168. thr_get_trx(thr));
  1169. } else {
  1170. if (node->set_x_locks) {
  1171. i_lock_mode = LOCK_IX;
  1172. } else {
  1173. i_lock_mode = LOCK_IS;
  1174. }
  1175. table_node = node->table_list;
  1176. while (table_node) {
  1177. err = lock_table(0, table_node->table,
  1178. i_lock_mode, thr);
  1179. if (err != DB_SUCCESS) {
  1180. que_thr_handle_error(thr, DB_ERROR,
  1181. NULL, 0);
  1182. return(NULL);
  1183. }
  1184. table_node = que_node_get_next(table_node);
  1185. }
  1186. }
  1187. /* If this is an explicit cursor, copy stored procedure
  1188. variable values, so that the values cannot change between
  1189. fetches (currently, we copy them also for non-explicit
  1190. cursors) */
  1191. if (node->explicit_cursor &&
  1192. UT_LIST_GET_FIRST(node->copy_variables)) {
  1193. row_sel_copy_input_variable_vals(node);
  1194. }
  1195. node->state = SEL_NODE_FETCH;
  1196. node->fetch_table = 0;
  1197. if (node->is_aggregate) {
  1198. /* Reset the aggregate total values */
  1199. sel_reset_aggregate_vals(node);
  1200. }
  1201. }
  1202. err = row_sel(node, thr);
  1203. /* NOTE! if queries are parallelized, the following assignment may
  1204. have problems; the assignment should be made only if thr is the
  1205. only top-level thr in the graph: */
  1206. thr->graph->last_sel_node = node;
  1207. if (err == DB_SUCCESS) {
  1208. /* Ok: do nothing */
  1209. } else if (err == DB_LOCK_WAIT) {
  1210. return(NULL);
  1211. } else {
  1212. /* SQL error detected */
  1213. printf("SQL error %lun", err);
  1214. que_thr_handle_error(thr, DB_ERROR, NULL, 0);
  1215. return(NULL);
  1216. }
  1217. return(thr);
  1218. /**************************************************************************
  1219. Performs a fetch for a cursor. */
  1220. que_thr_t*
  1221. fetch_step(
  1222. /*=======*/
  1223. /* out: query thread to run next or NULL */
  1224. que_thr_t* thr) /* in: query thread */
  1225. {
  1226. sel_node_t* sel_node;
  1227. fetch_node_t* node;
  1228. ut_ad(thr);
  1229. node = thr->run_node;
  1230. sel_node = node->cursor_def;
  1231. ut_ad(que_node_get_type(node) == QUE_NODE_FETCH);
  1232. if (thr->prev_node != que_node_get_parent(node)) {
  1233. if (sel_node->state != SEL_NODE_NO_MORE_ROWS) {
  1234. sel_assign_into_var_values(node->into_list, sel_node);
  1235. }
  1236. thr->run_node = que_node_get_parent(node);
  1237. return(thr);
  1238. }
  1239. /* Make the fetch node the parent of the cursor definition for
  1240. the time of the fetch, so that execution knows to return to this
  1241. fetch node after a row has been selected or we know that there is
  1242. no row left */
  1243. sel_node->common.parent = node;
  1244. if (sel_node->state == SEL_NODE_CLOSED) {
  1245. /* SQL error detected */
  1246. printf("SQL error %lun", DB_ERROR);
  1247. que_thr_handle_error(thr, DB_ERROR, NULL, 0);
  1248. return(NULL);
  1249. }
  1250. thr->run_node = sel_node;
  1251. return(thr);
  1252. /***************************************************************
  1253. Prints a row in a select result. */
  1254. que_thr_t*
  1255. row_printf_step(
  1256. /*============*/
  1257. /* out: query thread to run next or NULL */
  1258. que_thr_t* thr) /* in: query thread */
  1259. {
  1260. row_printf_node_t* node;
  1261. sel_node_t* sel_node;
  1262. que_node_t* arg;
  1263. ut_ad(thr);
  1264. node = thr->run_node;
  1265. sel_node = node->sel_node;
  1266. ut_ad(que_node_get_type(node) == QUE_NODE_ROW_PRINTF);
  1267. if (thr->prev_node == que_node_get_parent(node)) {
  1268. /* Reset the cursor */
  1269. sel_node->state = SEL_NODE_OPEN;
  1270. /* Fetch next row to print */
  1271. thr->run_node = sel_node;
  1272. return(thr);
  1273. }
  1274. if (sel_node->state != SEL_NODE_FETCH) {
  1275. ut_ad(sel_node->state == SEL_NODE_NO_MORE_ROWS);
  1276. /* No more rows to print */
  1277. thr->run_node = que_node_get_parent(node);
  1278. return(thr);
  1279. }
  1280. arg = sel_node->select_list;
  1281. while (arg) {
  1282. dfield_print_also_hex(que_node_get_val(arg));
  1283. printf(" ::: ");
  1284. arg = que_node_get_next(arg);
  1285. }
  1286. printf("n");
  1287. /* Fetch next row to print */
  1288. thr->run_node = sel_node;
  1289. return(thr);
  1290. /********************************************************************
  1291. Converts a key value stored in MySQL format to an Innobase dtuple.
  1292. The last field of the key value may be just a prefix of a fixed length
  1293. field: hence the parameter key_len. */
  1294. void
  1295. row_sel_convert_mysql_key_to_innobase(
  1296. /*==================================*/
  1297. dtuple_t* tuple, /* in: tuple where to build;
  1298. NOTE: we assume that the type info
  1299. in the tuple is already according
  1300. to index! */
  1301. byte* buf, /* in: buffer to use in field
  1302. conversions */
  1303. dict_index_t* index, /* in: index of the key value */
  1304. byte* key_ptr, /* in: MySQL key value */
  1305. ulint key_len) /* in: MySQL key value length */
  1306. {
  1307. dfield_t* dfield;
  1308. ulint offset;
  1309. ulint len;
  1310. byte* key_end;
  1311. ulint n_fields = 0;
  1312. UT_NOT_USED(index);
  1313. key_end = key_ptr + key_len;
  1314. /* Permit us to access any field in the tuple (ULINT_MAX): */
  1315. dtuple_set_n_fields(tuple, ULINT_MAX);
  1316. dfield = dtuple_get_nth_field(tuple, 0);
  1317. if (dfield_get_type(dfield)->mtype == DATA_SYS) {
  1318. /* A special case: we are looking for a position in a
  1319. generated clustered index: the first and the only
  1320. ordering column is ROW_ID */
  1321. ut_a(key_len == DATA_ROW_ID_LEN);
  1322. dfield_set_data(dfield, key_ptr, DATA_ROW_ID_LEN);
  1323. dtuple_set_n_fields(tuple, 1);
  1324. return;
  1325. }
  1326.    while (key_ptr < key_end) {
  1327. offset = 0;
  1328. len = dfield_get_type(dfield)->len;
  1329. n_fields++;    
  1330.      if (!(dfield_get_type(dfield)->prtype & DATA_NOT_NULL)) {
  1331.      /* The first byte in the field tells if this is
  1332.      an SQL NULL value */
  1333.     
  1334.      offset = 1;
  1335. if (*key_ptr != 0) {
  1336.        dfield_set_data(dfield, NULL, UNIV_SQL_NULL);
  1337.        goto next_part;
  1338.        }
  1339.        }
  1340. row_mysql_store_col_in_innobase_format(
  1341. dfield, buf, key_ptr + offset, len,
  1342. dfield_get_type(dfield)->mtype,
  1343. dfield_get_type(dfield)->prtype
  1344. & DATA_UNSIGNED);
  1345. next_part:
  1346.      key_ptr += (offset + len);
  1347. if (key_ptr > key_end) {
  1348. /* The last field in key was not a complete
  1349. field but a prefix of it */
  1350. ut_ad(dfield_get_len(dfield) != UNIV_SQL_NULL);
  1351. dfield_set_data(dfield, buf,
  1352. len - (ulint)(key_ptr - key_end));
  1353. }
  1354. buf += len;
  1355.     
  1356. dfield++;
  1357.    }
  1358.   /* We set the length of tuple to n_fields: we assume that
  1359. the memory area allocated for it is big enough (usually
  1360. bigger than n_fields). */
  1361.  
  1362.   dtuple_set_n_fields(tuple, n_fields);
  1363. }
  1364. /******************************************************************
  1365. Stores the row id to the prebuilt struct. */
  1366. UNIV_INLINE
  1367. void
  1368. row_sel_store_row_id_to_prebuilt(
  1369. /*=============================*/
  1370. row_prebuilt_t* prebuilt, /* in: prebuilt */
  1371. rec_t* index_rec, /* in: record */
  1372. dict_index_t* index) /* in: index of the record */
  1373. {
  1374. byte* data;
  1375. ulint len;
  1376. data = rec_get_nth_field(index_rec,
  1377. dict_index_get_sys_col_pos(index, DATA_ROW_ID), &len);
  1378. ut_a(len == DATA_ROW_ID_LEN);
  1379. ut_memcpy(prebuilt->row_id, data, len);
  1380. }
  1381. /******************************************************************
  1382. Stores a non-SQL-NULL field in the MySQL format. */
  1383. UNIV_INLINE
  1384. void
  1385. row_sel_field_store_in_mysql_format(
  1386. /*================================*/
  1387. byte* dest, /* in/out: buffer where to store; NOTE that BLOBs
  1388. are not in themselves stored here: the caller must
  1389. allocate and copy the BLOB into buffer before, and pass
  1390. the pointer to the BLOB in 'data' */
  1391. ulint col_len,/* in: MySQL column length */
  1392. byte* data, /* in: data to store */
  1393. ulint len, /* in: length of the data */
  1394. ulint type, /* in: data type */
  1395. ulint is_unsigned)/* in: != 0 if an unsigned integer type */
  1396. {
  1397. byte* ptr;
  1398. ut_ad(len != UNIV_SQL_NULL);
  1399. if (type == DATA_INT) {
  1400. /* Convert integer data from Innobase to a little-endian
  1401. format, sign bit restored to normal */
  1402. ptr = dest + len;
  1403. for (;;) {
  1404. ptr--;
  1405. *ptr = *data;
  1406. if (ptr == dest) {
  1407. break;
  1408. }
  1409. data++;
  1410. }
  1411. if (!is_unsigned) {
  1412. dest[len - 1] = (byte) (dest[len - 1] ^ 128);
  1413. }
  1414. ut_ad(col_len == len);
  1415. } else if (type == DATA_VARCHAR || type == DATA_VARMYSQL
  1416. || type == DATA_BINARY) {
  1417. /* Store the length of the data to the first two bytes of
  1418. dest; does not do anything yet because MySQL has
  1419. no real vars! */
  1420. dest = row_mysql_store_var_len(dest, len);
  1421. ut_memcpy(dest, data, len);
  1422. /* Pad with trailing spaces */
  1423. memset(dest + len, ' ', col_len - len); 
  1424. /* ut_ad(col_len >= len + 2); No real var implemented in
  1425. MySQL yet! */
  1426. } else if (type == DATA_BLOB) {
  1427. /* Store a pointer to the BLOB buffer to dest: the BLOB was
  1428. already copied to the buffer in row_sel_store_mysql_rec */
  1429. row_mysql_store_blob_ref(dest, col_len, data, len);
  1430. } else {
  1431. ut_memcpy(dest, data, len);
  1432. ut_ad(col_len == len);
  1433. }
  1434. }
  1435. /******************************************************************
  1436. Convert a row in the Innobase format to a row in the MySQL format.
  1437. Note that the template in prebuilt may advise us to copy only a few
  1438. columns to mysql_rec, other columns are left blank. All columns may not
  1439. be needed in the query. */
  1440. static
  1441. void
  1442. row_sel_store_mysql_rec(
  1443. /*====================*/
  1444. byte* mysql_rec, /* out: row in the MySQL format */
  1445. row_prebuilt_t* prebuilt, /* in: prebuilt struct */
  1446. rec_t* rec) /* in: Innobase record in the index
  1447. which was described in prebuilt's
  1448. template */
  1449. {
  1450. mysql_row_templ_t* templ;
  1451. byte* data;
  1452. ulint len;
  1453. byte* blob_buf;
  1454. ulint i;
  1455. ut_ad(prebuilt->mysql_template);
  1456. if (prebuilt->blob_heap != NULL) {
  1457. mem_heap_free(prebuilt->blob_heap);
  1458. prebuilt->blob_heap = NULL;
  1459. }
  1460. /* Mark all columns as not SQL NULL */
  1461. memset(mysql_rec, '', prebuilt->null_bitmap_len);
  1462. for (i = 0; i < prebuilt->n_template; i++) {
  1463. templ = prebuilt->mysql_template + i;
  1464. data = rec_get_nth_field(rec, templ->rec_field_no, &len);
  1465. if (len != UNIV_SQL_NULL) {
  1466. if (templ->type == DATA_BLOB) {
  1467. /* Copy the BLOB data to the BLOB
  1468. heap of prebuilt */
  1469. if (prebuilt->blob_heap == NULL) {
  1470. prebuilt->blob_heap =
  1471. mem_heap_create(len);
  1472. }
  1473. blob_buf = mem_heap_alloc(prebuilt->blob_heap,
  1474. len);
  1475. ut_memcpy(blob_buf, data, len);
  1476. data = blob_buf;
  1477. }
  1478. row_sel_field_store_in_mysql_format(
  1479. mysql_rec + templ->mysql_col_offset,
  1480. templ->mysql_col_len, data, len,
  1481. templ->type, templ->is_unsigned);
  1482. } else {
  1483. mysql_rec[templ->mysql_null_byte_offset] |=
  1484. (byte) (templ->mysql_null_bit_mask);
  1485. }
  1486. }
  1487. /*************************************************************************
  1488. Builds a previous version of a clustered index record for a consistent read */
  1489. static
  1490. ulint
  1491. row_sel_build_prev_vers_for_mysql(
  1492. /*==============================*/
  1493. /* out: DB_SUCCESS or error code */
  1494. read_view_t* read_view, /* in: read view */
  1495. dict_index_t* clust_index, /* in: clustered index */
  1496. row_prebuilt_t* prebuilt, /* in: prebuilt struct */
  1497. rec_t* rec, /* in: record in a clustered index */
  1498. rec_t** old_vers, /* out: old version, or NULL if the
  1499. record does not exist in the view:
  1500. i.e., it was freshly inserted
  1501. afterwards */
  1502. mtr_t* mtr) /* in: mtr */
  1503. {
  1504. ulint err;
  1505. if (prebuilt->old_vers_heap) {
  1506. mem_heap_empty(prebuilt->old_vers_heap);
  1507. } else {
  1508. prebuilt->old_vers_heap = mem_heap_create(200);
  1509. }
  1510. err = row_vers_build_for_consistent_read(rec, mtr, clust_index,
  1511. read_view, prebuilt->old_vers_heap,
  1512. old_vers);
  1513. return(err);
  1514. }
  1515. /*************************************************************************
  1516. Retrieves the clustered index record corresponding to a record in a
  1517. non-clustered index. Does the necessary locking. Used in the MySQL
  1518. interface. */
  1519. static
  1520. ulint
  1521. row_sel_get_clust_rec_for_mysql(
  1522. /*============================*/
  1523. /* out: DB_SUCCESS or error code */
  1524. row_prebuilt_t* prebuilt,/* in: prebuilt struct in the handle */
  1525. dict_index_t* sec_index,/* in: secondary index where rec resides */
  1526. rec_t* rec, /* in: record in a non-clustered index */
  1527. que_thr_t* thr, /* in: query thread */
  1528. rec_t** out_rec,/* out: clustered record or an old version of
  1529. it, NULL if the old version did not exist
  1530. in the read view, i.e., it was a fresh
  1531. inserted version */
  1532. mtr_t* mtr) /* in: mtr used to get access to the
  1533. non-clustered record; the same mtr is used to
  1534. access the clustered index */
  1535. {
  1536. dict_index_t* clust_index;
  1537. rec_t* clust_rec;
  1538. rec_t* old_vers;
  1539. ulint err;
  1540. trx_t* trx;
  1541. *out_rec = NULL;
  1542. row_build_row_ref_in_tuple(prebuilt->clust_ref, sec_index, rec);
  1543. clust_index = dict_table_get_first_index(sec_index->table);
  1544. btr_pcur_open_with_no_init(clust_index, prebuilt->clust_ref,
  1545. PAGE_CUR_LE, BTR_SEARCH_LEAF,
  1546. prebuilt->clust_pcur, 0, mtr);
  1547. clust_rec = btr_pcur_get_rec(prebuilt->clust_pcur);
  1548. ut_ad(page_rec_is_user_rec(clust_rec));
  1549. if (prebuilt->select_lock_type != LOCK_NONE) {
  1550. /* Try to place a lock on the index record */
  1551. err = lock_clust_rec_read_check_and_lock(0, clust_rec,
  1552. clust_index,
  1553. prebuilt->select_lock_type, thr);
  1554. if (err != DB_SUCCESS) {
  1555. return(err);
  1556. }
  1557. } else {
  1558. /* This is a non-locking consistent read: if necessary, fetch
  1559. a previous version of the record */
  1560. trx = thr_get_trx(thr);
  1561. if (!lock_clust_rec_cons_read_sees(clust_rec, clust_index,
  1562. trx->read_view)) {
  1563. err = row_sel_build_prev_vers_for_mysql(
  1564. trx->read_view, clust_index,
  1565. prebuilt, clust_rec,
  1566. &old_vers, mtr);
  1567. if (err != DB_SUCCESS) {
  1568. return(err);
  1569. }
  1570. clust_rec = old_vers;
  1571. }
  1572. }
  1573. *out_rec = clust_rec;
  1574. if (prebuilt->select_lock_type == LOCK_X) {
  1575. /* We may use the cursor in update: store its position */
  1576. btr_pcur_store_position(prebuilt->clust_pcur, mtr);
  1577. }
  1578. return(DB_SUCCESS);
  1579. }
  1580. /************************************************************************
  1581. Restores cursor position after it has been stored. We have to take into
  1582. account that the record cursor was positioned on can have been deleted.
  1583. Then we may have to move the cursor one step up or down. */
  1584. static
  1585. ibool
  1586. sel_restore_position_for_mysql(
  1587. /*===========================*/
  1588. /* out: TRUE if we may need to
  1589. process the record the cursor is
  1590. now positioned on (i.e. we should
  1591. not go to the next record yet) */
  1592. ulint latch_mode, /* in: latch mode wished in
  1593. restoration */
  1594. btr_pcur_t* pcur, /* in: cursor whose position
  1595. has been stored */
  1596. ibool moves_up, /* in: TRUE if the cursor moves up
  1597. in the index */
  1598. mtr_t* mtr) /* in: mtr; CAUTION: may commit
  1599. mtr temporarily! */
  1600. {
  1601. ibool success;
  1602. ulint relative_position;
  1603. relative_position = pcur->rel_pos;
  1604. success = btr_pcur_restore_position(latch_mode, pcur, mtr);
  1605. if (relative_position == BTR_PCUR_ON) {
  1606. if (success) {
  1607. return(FALSE);
  1608. }
  1609. if (moves_up) {
  1610. btr_pcur_move_to_next(pcur, mtr);
  1611. return(TRUE);
  1612. }
  1613. return(TRUE);
  1614. }
  1615. if (relative_position == BTR_PCUR_AFTER) {
  1616. if (moves_up) {
  1617. return(TRUE);
  1618. }
  1619. if (btr_pcur_is_on_user_rec(pcur, mtr)) {
  1620. btr_pcur_move_to_prev(pcur, mtr);
  1621. }
  1622. return(TRUE);
  1623. }
  1624. ut_ad(relative_position == BTR_PCUR_BEFORE);
  1625. if (moves_up && btr_pcur_is_on_user_rec(pcur, mtr)) {
  1626. btr_pcur_move_to_next(pcur, mtr);
  1627. }
  1628. return(TRUE);
  1629. }
  1630. /************************************************************************
  1631. Pops a cached row for MySQL from the fetch cache. */
  1632. UNIV_INLINE
  1633. void
  1634. row_sel_pop_cached_row_for_mysql(
  1635. /*=============================*/
  1636. byte* buf, /* in/out: buffer where to copy the
  1637. row */
  1638. row_prebuilt_t* prebuilt) /* in: prebuilt struct */
  1639. {
  1640. ut_ad(prebuilt->n_fetch_cached > 0);
  1641. ut_memcpy(buf, prebuilt->fetch_cache[prebuilt->fetch_cache_first],
  1642. prebuilt->mysql_row_len);
  1643. prebuilt->n_fetch_cached--;
  1644. prebuilt->fetch_cache_first++;
  1645. if (prebuilt->n_fetch_cached == 0) {
  1646. prebuilt->fetch_cache_first = 0;
  1647. }
  1648. }
  1649. /************************************************************************
  1650. Pushes a row for MySQL to the fetch cache. */
  1651. UNIV_INLINE
  1652. void
  1653. row_sel_push_cache_row_for_mysql(
  1654. /*=============================*/
  1655. row_prebuilt_t* prebuilt, /* in: prebuilt struct */
  1656. rec_t* rec) /* in: record to push */
  1657. {
  1658. ulint i;
  1659. ut_ad(prebuilt->n_fetch_cached < MYSQL_FETCH_CACHE_SIZE);
  1660. if (prebuilt->fetch_cache[0] == NULL) {
  1661. /* Allocate memory for the fetch cache */
  1662. for (i = 0; i < MYSQL_FETCH_CACHE_SIZE; i++) {
  1663. prebuilt->fetch_cache[i] = mem_alloc(
  1664. prebuilt->mysql_row_len);
  1665. }
  1666. }
  1667. ut_ad(prebuilt->fetch_cache_first == 0);
  1668. row_sel_store_mysql_rec(
  1669. prebuilt->fetch_cache[prebuilt->n_fetch_cached],
  1670. prebuilt, rec);
  1671. prebuilt->n_fetch_cached++;
  1672. }
  1673. /************************************************************************
  1674. Searches for rows in the database. This is used in the interface to
  1675. MySQL. This function opens a cursor, and also implements fetch next
  1676. and fetch prev. NOTE that if we do a search with a full key value
  1677. from a unique index (ROW_SEL_EXACT), then we will not store the cursor
  1678. position and fetch next or fetch prev must not be tried to the cursor! */
  1679. ulint
  1680. row_search_for_mysql(
  1681. /*=================*/
  1682. /* out: DB_SUCCESS,
  1683. DB_RECORD_NOT_FOUND, 
  1684. DB_END_OF_INDEX, or DB_DEADLOCK */
  1685. byte* buf, /* in/out: buffer for the fetched
  1686. row in the MySQL format */
  1687. ulint mode, /* in: search mode PAGE_CUR_L, ... */
  1688. row_prebuilt_t* prebuilt, /* in: prebuilt struct for the
  1689. table handle; this contains the info
  1690. of search_tuple, index; if search
  1691. tuple contains 0 fields then we
  1692. position the cursor at the start or
  1693. the end of the index, depending on
  1694. 'mode' */
  1695. ulint match_mode, /* in: 0 or ROW_SEL_EXACT or
  1696. ROW_SEL_EXACT_PREFIX */ 
  1697. ulint direction) /* in: 0 or ROW_SEL_NEXT or
  1698. ROW_SEL_PREV; NOTE: if this is != 0,
  1699. then prebuilt must have a pcur
  1700. with stored position! In opening of a
  1701. cursor 'direction' should be 0. */
  1702. {
  1703. dict_index_t* index = prebuilt->index;
  1704. dtuple_t* search_tuple = prebuilt->search_tuple;
  1705. btr_pcur_t* pcur = prebuilt->pcur;
  1706. trx_t* trx = prebuilt->trx;
  1707. dict_index_t* clust_index;
  1708. que_thr_t* thr;
  1709. rec_t* rec;
  1710. rec_t* index_rec;
  1711. rec_t* clust_rec;
  1712. rec_t* old_vers;
  1713. ulint err;
  1714. ibool moved;
  1715. ibool cons_read_requires_clust_rec;
  1716. ibool was_lock_wait;
  1717. ulint ret;
  1718. ibool unique_search_from_clust_index = FALSE;
  1719. ibool mtr_has_extra_clust_latch  = FALSE;
  1720. ibool moves_up  = FALSE;
  1721. mtr_t mtr;
  1722. ut_ad(index && pcur && search_tuple);
  1723. ut_ad(trx->mysql_thread_id == os_thread_get_curr_id());
  1724. ut_ad(sync_thread_levels_empty_gen(FALSE));
  1725. if (direction == 0) {
  1726. prebuilt->n_rows_fetched = 0;
  1727. prebuilt->n_fetch_cached = 0;
  1728. prebuilt->fetch_cache_first = 0;
  1729. if (prebuilt->sel_graph == NULL) {
  1730. /* Build a dummy select query graph */
  1731. row_prebuild_sel_graph(prebuilt);
  1732. }
  1733. } else {
  1734. if (prebuilt->n_rows_fetched == 0) {
  1735. prebuilt->fetch_direction = direction;
  1736. }
  1737. if (direction != prebuilt->fetch_direction) {
  1738. if (prebuilt->n_fetch_cached > 0) {
  1739. ut_a(0);
  1740. /* TODO: scrollable cursor: restore cursor to
  1741. the place of the latest returned row,
  1742. or better: prevent caching for a scroll
  1743. cursor! */
  1744. }
  1745. prebuilt->n_rows_fetched = 0;
  1746. prebuilt->n_fetch_cached = 0;
  1747. prebuilt->fetch_cache_first = 0;
  1748. } else if (prebuilt->n_fetch_cached > 0) {
  1749. row_sel_pop_cached_row_for_mysql(buf, prebuilt);
  1750. prebuilt->n_rows_fetched++;
  1751. return(DB_SUCCESS);
  1752. }
  1753. if (prebuilt->fetch_cache_first > 0
  1754.     && prebuilt->fetch_cache_first < MYSQL_FETCH_CACHE_SIZE) {
  1755.      /* The previous returned row was popped from the fetch
  1756.      cache, but the cache was not full at the time of the
  1757.      popping: no more rows can exist in the result set */
  1758.     
  1759.      return(DB_RECORD_NOT_FOUND);
  1760. }
  1761. prebuilt->n_rows_fetched++;
  1762. if (prebuilt->n_rows_fetched > 1000000000) {
  1763. /* Prevent wrap-over */
  1764. prebuilt->n_rows_fetched = 500000000;
  1765. }
  1766. mode = pcur->search_mode;
  1767. }
  1768. if (match_mode == ROW_SEL_EXACT && index->type & DICT_UNIQUE
  1769. && index->type & DICT_CLUSTERED
  1770. && dtuple_get_n_fields(search_tuple)
  1771. == dict_index_get_n_unique(index)) {
  1772. if (direction == ROW_SEL_NEXT) {
  1773. /* MySQL sometimes seems to do fetch next even
  1774. if the search condition is unique; we do not store
  1775. pcur position in this case, so we cannot
  1776. restore cursor position, and must return
  1777.   immediately */
  1778. return(DB_RECORD_NOT_FOUND);
  1779. }
  1780. ut_a(direction == 0); /* We cannot do fetch prev, as we have
  1781. not stored the cursor position */
  1782. mode = PAGE_CUR_GE;
  1783. unique_search_from_clust_index = TRUE;
  1784. }
  1785. /* Note that if the search mode was GE or G, then the cursor
  1786. naturally moves upward (in fetch next) in alphabetical order,
  1787. otherwise downward */
  1788. if (direction == 0) {
  1789. if (mode == PAGE_CUR_GE || mode == PAGE_CUR_G) {
  1790. moves_up = TRUE;
  1791. }
  1792. } else if (direction == ROW_SEL_NEXT) {
  1793. moves_up = TRUE;
  1794. }
  1795. mtr_start(&mtr);
  1796. thr = que_fork_get_first_thr(prebuilt->sel_graph);
  1797. que_thr_move_to_run_state_for_mysql(thr, trx);
  1798. clust_index = dict_table_get_first_index(index->table);
  1799. if (direction != 0) {
  1800. moved = sel_restore_position_for_mysql(BTR_SEARCH_LEAF, pcur,
  1801. moves_up, &mtr);
  1802. if (!moved) {
  1803. goto next_rec;
  1804. }
  1805. } else if (dtuple_get_n_fields(search_tuple) > 0) {
  1806. btr_pcur_open_with_no_init(index, search_tuple, mode,
  1807. BTR_SEARCH_LEAF,
  1808. pcur, 0, &mtr);
  1809. } else {
  1810. if (mode == PAGE_CUR_G) {
  1811. btr_pcur_open_at_index_side(TRUE, index,
  1812. BTR_SEARCH_LEAF, pcur, FALSE, &mtr);
  1813. } else if (mode == PAGE_CUR_L) {
  1814. btr_pcur_open_at_index_side(FALSE, index,
  1815. BTR_SEARCH_LEAF, pcur, FALSE, &mtr);
  1816. }
  1817. }
  1818. if (!prebuilt->sql_stat_start) {
  1819. /* No need to set an intention lock or assign a read view */
  1820. } else if (prebuilt->select_lock_type == LOCK_NONE) {
  1821. /* This is a consistent read */
  1822. trx_start_if_not_started(trx);
  1823. /* Assign a read view for the query */
  1824. trx_assign_read_view(trx);
  1825. prebuilt->sql_stat_start = FALSE;
  1826. } else {
  1827. trx_start_if_not_started(trx);
  1828. if (prebuilt->select_lock_type == LOCK_S) {
  1829. err = lock_table(0, index->table, LOCK_IS, thr);
  1830. } else {
  1831. err = lock_table(0, index->table, LOCK_IX, thr);
  1832. }
  1833. if (err != DB_SUCCESS) {
  1834. goto lock_wait_or_error;
  1835. }
  1836. prebuilt->sql_stat_start = FALSE;
  1837. }
  1838. /*-------------------------------------------------------------*/
  1839. rec_loop:
  1840. cons_read_requires_clust_rec = FALSE;
  1841. rec = btr_pcur_get_rec(pcur);
  1842. if (rec == page_get_infimum_rec(buf_frame_align(rec))) {
  1843. /* The infimum record on a page cannot be in the result set,
  1844. and neither can a record lock be placed on it: we skip such
  1845. a record. */
  1846. goto next_rec;
  1847. }
  1848. if (prebuilt->select_lock_type != LOCK_NONE) {
  1849. /* Try to place a lock on the index record */
  1850. err = sel_set_rec_lock(rec, index, prebuilt->select_lock_type,
  1851. thr);
  1852. if (err != DB_SUCCESS) {
  1853. goto lock_wait_or_error;
  1854. }
  1855. }
  1856. if (rec == page_get_supremum_rec(buf_frame_align(rec))) {
  1857. /* A page supremum record cannot be in the result set: skip
  1858. it now when we have placed a possible lock on it */
  1859. goto next_rec;
  1860. }
  1861. ut_ad(page_rec_is_user_rec(rec));
  1862. if (unique_search_from_clust_index && btr_pcur_get_up_match(pcur)
  1863. == dtuple_get_n_fields(search_tuple)) {
  1864. /* The record matches enough */
  1865. ut_ad(mode == PAGE_CUR_GE);
  1866. } else if (match_mode == ROW_SEL_EXACT) {
  1867. /* Test if the index record matches completely to search_tuple
  1868. in prebuilt: if not, then we return with DB_RECORD_NOT_FOUND */
  1869. if (0 != cmp_dtuple_rec(search_tuple, rec)) {
  1870. btr_pcur_store_position(pcur, &mtr);
  1871. ret = DB_RECORD_NOT_FOUND;
  1872. goto normal_return;
  1873. }
  1874. } else if (match_mode == ROW_SEL_EXACT_PREFIX) {
  1875. if (!cmp_dtuple_is_prefix_of_rec(search_tuple, rec)) {
  1876. btr_pcur_store_position(pcur, &mtr);
  1877. ret = DB_RECORD_NOT_FOUND;
  1878. goto normal_return;
  1879. }
  1880. }
  1881. /* We are ready to look at a possible new index entry in the result
  1882. set: the cursor is now placed on a user record */
  1883. /* Get the right version of the row in a consistent read */
  1884. if (prebuilt->select_lock_type == LOCK_NONE) {
  1885. /* This is a non-locking consistent read: if necessary, fetch
  1886. a previous version of the record */
  1887. cons_read_requires_clust_rec = FALSE;
  1888. if (index == clust_index) {
  1889. if (!lock_clust_rec_cons_read_sees(rec, index,
  1890. trx->read_view)) {
  1891. err = row_sel_build_prev_vers_for_mysql(
  1892. trx->read_view, clust_index,
  1893. prebuilt, rec,
  1894. &old_vers, &mtr);
  1895. if (err != DB_SUCCESS) {
  1896. goto lock_wait_or_error;
  1897. }
  1898. if (old_vers == NULL) {
  1899. /* The row did not exist yet in
  1900. the read view */
  1901. goto next_rec;
  1902. }
  1903. rec = old_vers;
  1904. }
  1905. } else if (!lock_sec_rec_cons_read_sees(rec, index,
  1906. trx->read_view)) {
  1907. /* We are looking into a non-clustered index,
  1908. and to get the right version of the record we
  1909. have to look also into the clustered index: this
  1910. is necessary, because we can only get the undo
  1911. information via the clustered index record. */
  1912. cons_read_requires_clust_rec = TRUE;
  1913. }
  1914. }
  1915. if (rec_get_deleted_flag(rec) && !cons_read_requires_clust_rec) {
  1916. /* The record is delete marked: we can skip it if this is
  1917. not a consistent read which might see an earlier version
  1918. of a non-clustered index record */
  1919. goto next_rec;
  1920. }
  1921. /* Get the clustered index record if needed and if we did
  1922. not do the search using the clustered index */
  1923. index_rec = rec;
  1924. if (index != clust_index && (cons_read_requires_clust_rec
  1925. || prebuilt->need_to_access_clustered)) {
  1926. /* It was a non-clustered index and we must fetch also the
  1927. clustered index record */
  1928. mtr_has_extra_clust_latch = TRUE;
  1929. err = row_sel_get_clust_rec_for_mysql(prebuilt, index, rec,
  1930. thr, &clust_rec, &mtr);
  1931. if (err != DB_SUCCESS) {
  1932. goto lock_wait_or_error;
  1933. }
  1934. if (clust_rec == NULL) {
  1935. /* The record did not exist in the read view */
  1936. ut_ad(prebuilt->select_lock_type == LOCK_NONE);
  1937. goto next_rec;
  1938. }
  1939. if (rec_get_deleted_flag(clust_rec)) {
  1940. /* The record is delete marked: we can skip it */
  1941. goto next_rec;
  1942. }
  1943. rec = clust_rec;
  1944. }
  1945. /* We found a qualifying row */
  1946. if (prebuilt->n_rows_fetched >= MYSQL_FETCH_CACHE_THRESHOLD
  1947. && !prebuilt->templ_contains_blob
  1948. && prebuilt->select_lock_type == LOCK_NONE
  1949. && !prebuilt->clust_index_was_generated) {
  1950. /* Inside an update, for example, we do not cache rows,
  1951. since we may use the cursor position to do the actual
  1952. update, that is why we require ...lock_type == LOCK_NONE */
  1953. row_sel_push_cache_row_for_mysql(prebuilt, rec);
  1954. if (prebuilt->n_fetch_cached == MYSQL_FETCH_CACHE_SIZE) {
  1955. goto got_row;
  1956. }
  1957. goto next_rec;
  1958. } else {
  1959. row_sel_store_mysql_rec(buf, prebuilt, rec);
  1960. if (prebuilt->clust_index_was_generated) {
  1961. row_sel_store_row_id_to_prebuilt(prebuilt, index_rec,
  1962. index);
  1963. }
  1964. }
  1965. got_row:
  1966. /* TODO: should we in every case store the cursor position, even
  1967. if this is just a join, for example? */
  1968. if (!unique_search_from_clust_index
  1969. || prebuilt->select_lock_type == LOCK_X) {
  1970. /* Inside an update always store the cursor position */
  1971. btr_pcur_store_position(pcur, &mtr);
  1972. }
  1973. ret = DB_SUCCESS;
  1974. goto normal_return;
  1975. /*-------------------------------------------------------------*/
  1976. next_rec:
  1977. if (mtr_has_extra_clust_latch) {
  1978. /* We must commit mtr if we are moving to the next
  1979. non-clustered index record, because we could break the
  1980. latching order if we would access a different clustered
  1981. index page right away without releasing the previous. */
  1982. btr_pcur_store_position(pcur, &mtr);
  1983. mtr_commit(&mtr);
  1984. mtr_has_extra_clust_latch = FALSE;
  1985. mtr_start(&mtr);
  1986. moved = sel_restore_position_for_mysql(BTR_SEARCH_LEAF, pcur,
  1987. moves_up, &mtr);
  1988. if (moved) {
  1989. goto rec_loop;
  1990. }
  1991. }
  1992. if (moves_up) {
  1993. moved = btr_pcur_move_to_next(pcur, &mtr);
  1994. } else {
  1995. moved = btr_pcur_move_to_prev(pcur, &mtr);
  1996. }
  1997. if (!moved) {
  1998. btr_pcur_store_position(pcur, &mtr);
  1999. if (match_mode != 0) {
  2000. ret = DB_RECORD_NOT_FOUND;
  2001. } else {
  2002. ret = DB_END_OF_INDEX;
  2003. }
  2004. goto normal_return;
  2005. }
  2006. goto rec_loop;
  2007. /*-------------------------------------------------------------*/
  2008. lock_wait_or_error:
  2009. btr_pcur_store_position(pcur, &mtr);
  2010. mtr_commit(&mtr);
  2011. mtr_has_extra_clust_latch = FALSE;
  2012. trx->error_state = err;
  2013. /* The following is a patch for MySQL */
  2014. que_thr_stop_for_mysql(thr);
  2015. was_lock_wait = row_mysql_handle_errors(&err, trx, thr, NULL);
  2016. if (was_lock_wait) {
  2017. mtr_start(&mtr);
  2018. sel_restore_position_for_mysql(BTR_SEARCH_LEAF, pcur,
  2019. moves_up, &mtr);
  2020. mode = pcur->search_mode;
  2021. goto rec_loop;
  2022. }
  2023. return(err);
  2024. normal_return:
  2025. que_thr_stop_for_mysql_no_error(thr, trx);
  2026. mtr_commit(&mtr);
  2027. if (prebuilt->n_fetch_cached > 0) {
  2028. row_sel_pop_cached_row_for_mysql(buf, prebuilt);
  2029. ret = DB_SUCCESS;
  2030. }
  2031. return(ret);
  2032. }