| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 1997, 2026, Oracle and/or its affiliates. |
| 4 |
Copyright (c) 2008, Google Inc. |
| 5 |
|
| 6 |
Portions of this file contain modifications contributed and copyrighted by |
| 7 |
Google, Inc. Those modifications are gratefully acknowledged and are described |
| 8 |
briefly in the InnoDB documentation. The contributions by Google are |
| 9 |
incorporated with their permission, and subject to the conditions contained in |
| 10 |
the file COPYING.Google. |
| 11 |
|
| 12 |
This program is free software; you can redistribute it and/or modify it under |
| 13 |
the terms of the GNU General Public License, version 2.0, as published by the |
| 14 |
Free Software Foundation. |
| 15 |
|
| 16 |
This program is designed to work with certain software (including |
| 17 |
but not limited to OpenSSL) that is licensed under separate terms, |
| 18 |
as designated in a particular file or component or in included license |
| 19 |
documentation. The authors of MySQL hereby grant you an additional |
| 20 |
permission to link the program and your derivative works with the |
| 21 |
separately licensed software that they have either included with |
| 22 |
the program or referenced in the documentation. |
| 23 |
|
| 24 |
This program is distributed in the hope that it will be useful, but WITHOUT |
| 25 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 26 |
FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0, |
| 27 |
for more details. |
| 28 |
|
| 29 |
You should have received a copy of the GNU General Public License along with |
| 30 |
this program; if not, write to the Free Software Foundation, Inc., |
| 31 |
51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 32 |
|
| 33 |
*****************************************************************************/ |
| 34 |
|
| 35 |
/** @file row/row0sel.cc |
| 36 |
Select |
| 37 |
|
| 38 |
Created 12/19/1997 Heikki Tuuri |
| 39 |
*******************************************************/ |
| 40 |
|
| 41 |
#include "row0sel.h" |
| 42 |
|
| 43 |
#include <sys/types.h> |
| 44 |
|
| 45 |
#include "btr0btr.h" |
| 46 |
#include "btr0cur.h" |
| 47 |
#include "btr0sea.h" |
| 48 |
#include "buf0lru.h" |
| 49 |
#include "dict0boot.h" |
| 50 |
#include "dict0dd.h" |
| 51 |
#include "dict0dict.h" |
| 52 |
#include "eval0eval.h" |
| 53 |
#include "gis0rtree.h" |
| 54 |
#include "ha_innodb.h" |
| 55 |
#include "ha_prototypes.h" |
| 56 |
#include "handler.h" |
| 57 |
#include "lob0lob.h" |
| 58 |
#include "lob0undo.h" |
| 59 |
#include "lock0lock.h" |
| 60 |
#include "mach0data.h" |
| 61 |
#include "pars0pars.h" |
| 62 |
#include "pars0sym.h" |
| 63 |
#include "que0que.h" |
| 64 |
#include "read0read.h" |
| 65 |
#include "record_buffer.h" |
| 66 |
#include "rem0cmp.h" |
| 67 |
#include "row0mysql.h" |
| 68 |
#include "row0row.h" |
| 69 |
#include "row0upd.h" |
| 70 |
#include "row0vers.h" |
| 71 |
#include "srv0mon.h" |
| 72 |
#include "trx0trx.h" |
| 73 |
#include "trx0undo.h" |
| 74 |
#include "ut0new.h" |
| 75 |
|
| 76 |
#include "my_dbug.h" |
| 77 |
|
| 78 |
/** Maximum number of rows to prefetch; MySQL interface has another parameter */ |
| 79 |
constexpr uint32_t SEL_MAX_N_PREFETCH = 16; |
| 80 |
|
| 81 |
/** Number of rows fetched, after which to start prefetching; MySQL interface |
| 82 |
has another parameter */ |
| 83 |
constexpr uint32_t SEL_PREFETCH_LIMIT = 1; |
| 84 |
|
| 85 |
/** When a select has accessed about this many pages, it returns control back |
| 86 |
to que_run_threads: this is to allow canceling runaway queries */ |
| 87 |
|
| 88 |
constexpr uint32_t SEL_COST_LIMIT = 100; |
| 89 |
|
| 90 |
/** Flags for search shortcut */ |
| 91 |
constexpr uint32_t SEL_FOUND = 0; |
| 92 |
constexpr uint32_t SEL_EXHAUSTED = 1; |
| 93 |
constexpr uint32_t SEL_RETRY = 2; |
| 94 |
|
| 95 |
/** Returns true if the user-defined column in a secondary index record |
| 96 |
is alphabetically the same as the corresponding BLOB column in the clustered |
| 97 |
index record. |
| 98 |
NOTE: the comparison is NOT done as a binary comparison, but character |
| 99 |
fields are compared with collation! |
| 100 |
@return true if the columns are equal */ |
| 101 |
static bool row_sel_sec_rec_is_for_blob( |
| 102 |
trx_t *trx, /*!< in: the operating transaction */ |
| 103 |
ulint mtype, /*!< in: main type */ |
| 104 |
ulint prtype, /*!< in: precise type */ |
| 105 |
ulint mbminmaxlen, /*!< in: minimum and maximum length of |
| 106 |
a multi-byte character */ |
| 107 |
const byte *clust_field, /*!< in: the locally stored part of |
| 108 |
the clustered index column, including |
| 109 |
the BLOB pointer; the clustered |
| 110 |
index record must be covered by |
| 111 |
a lock or a page latch to protect it |
| 112 |
against deletion (rollback or purge) */ |
| 113 |
ulint clust_len, /*!< in: length of clust_field */ |
| 114 |
const byte *sec_field, /*!< in: column in secondary index */ |
| 115 |
ulint sec_len, /*!< in: length of sec_field */ |
| 116 |
ulint prefix_len, /*!< in: index column prefix length |
| 117 |
in bytes */ |
| 118 |
const dict_table_t *table) /*!< in: table */ |
| 119 |
{ |
| 120 |
ulint len; |
| 121 |
byte buf[REC_VERSION_56_MAX_INDEX_COL_LEN]; |
| 122 |
|
| 123 |
/* This function should never be invoked on tables in |
| 124 |
ROW_FORMAT=REDUNDANT or ROW_FORMAT=COMPACT, because they |
| 125 |
should always contain enough prefix in the clustered index record. */ |
| 126 |
ut_ad(dict_table_has_atomic_blobs(table)); |
| 127 |
ut_a(clust_len >= BTR_EXTERN_FIELD_REF_SIZE); |
| 128 |
ut_ad(prefix_len >= sec_len); |
| 129 |
ut_ad(prefix_len > 0); |
| 130 |
ut_a(prefix_len <= sizeof buf); |
| 131 |
|
| 132 |
if (!memcmp(clust_field + clust_len - BTR_EXTERN_FIELD_REF_SIZE, |
| 133 |
field_ref_zero, BTR_EXTERN_FIELD_REF_SIZE)) { |
| 134 |
/* The externally stored field was not written yet. |
| 135 |
This record should only be seen by |
| 136 |
trx_rollback_or_clean_all_recovered() or any |
| 137 |
TRX_ISO_READ_UNCOMMITTED transactions. */ |
| 138 |
return false; |
| 139 |
} |
| 140 |
|
| 141 |
len = lob::btr_copy_externally_stored_field_prefix_func( |
| 142 |
trx, table->first_index(), buf, prefix_len, |
| 143 |
dict_tf_get_page_size(table->flags), clust_field, |
| 144 |
IF_DEBUG(dict_table_is_sdi(table->id), ) clust_len); |
| 145 |
|
| 146 |
if (len == 0) { |
| 147 |
/* The BLOB was being deleted as the server crashed. |
| 148 |
There should not be any secondary index records |
| 149 |
referring to this clustered index record, because |
| 150 |
btr_free_externally_stored_field() is called after all |
| 151 |
secondary index entries of the row have been purged. */ |
| 152 |
return false; |
| 153 |
} |
| 154 |
|
| 155 |
len = dtype_get_at_most_n_mbchars(prtype, mbminmaxlen, prefix_len, len, |
| 156 |
(const char *)buf); |
| 157 |
|
| 158 |
/* We are testing for equality; ASC/DESC does not matter. */ |
| 159 |
return (!cmp_data_data(mtype, prtype, true, buf, len, sec_field, sec_len)); |
| 160 |
} |
| 161 |
|
| 162 |
/** Returns true if the user-defined column values in a secondary index record |
| 163 |
are alphabetically the same as the corresponding columns in the clustered |
| 164 |
index record. |
| 165 |
NOTE: the comparison is NOT done as a binary comparison, but character |
| 166 |
fields are compared with collation! |
| 167 |
@param[in] sec_rec secondary index record |
| 168 |
@param[in] sec_index secondary index |
| 169 |
@param[in] clust_rec clustered index record; |
| 170 |
must be protected by a page s-latch |
| 171 |
@param[in] clust_index clustered index |
| 172 |
@param[in] thr query thread |
| 173 |
@param[out] is_equal set to true if the secondary record is equal |
| 174 |
to the corresponding fields in the clustered record, when compared with |
| 175 |
collation; false if not equal or if the |
| 176 |
clustered record has been marked for deletion; only valid if DB_SUCCESS was |
| 177 |
returned |
| 178 |
@return DB_SUCCESS or error code */ |
| 179 |
static dberr_t row_sel_sec_rec_is_for_clust_rec( |
| 180 |
const rec_t *sec_rec, const dict_index_t *sec_index, const rec_t *clust_rec, |
| 181 |
const dict_index_t *clust_index, que_thr_t *thr, bool &is_equal) { |
| 182 |
const byte *sec_field; |
| 183 |
ulint sec_len; |
| 184 |
const byte *clust_field; |
| 185 |
ulint n; |
| 186 |
ulint i; |
| 187 |
mem_heap_t *heap = nullptr; |
| 188 |
ulint clust_offsets_[REC_OFFS_NORMAL_SIZE]; |
| 189 |
ulint sec_offsets_[REC_OFFS_SMALL_SIZE]; |
| 190 |
ulint *clust_offs = clust_offsets_; |
| 191 |
ulint *sec_offs = sec_offsets_; |
| 192 |
trx_t *trx = thr_get_trx(thr); |
| 193 |
dberr_t err = DB_SUCCESS; |
| 194 |
const dict_table_t *const table = clust_index->table; |
| 195 |
|
| 196 |
is_equal = true; |
| 197 |
|
| 198 |
rec_offs_init(clust_offsets_); |
| 199 |
rec_offs_init(sec_offsets_); |
| 200 |
|
| 201 |
if (rec_get_deleted_flag(clust_rec, dict_table_is_comp(table))) { |
| 202 |
/* The clustered index record is delete-marked; |
| 203 |
it is not visible in the read view. Besides, |
| 204 |
if there are any externally stored columns, |
| 205 |
some of them may have already been purged. */ |
| 206 |
is_equal = false; |
| 207 |
return (DB_SUCCESS); |
| 208 |
} |
| 209 |
|
| 210 |
heap = mem_heap_create(256, UT_LOCATION_HERE); |
| 211 |
|
| 212 |
clust_offs = rec_get_offsets(clust_rec, clust_index, clust_offs, |
| 213 |
ULINT_UNDEFINED, UT_LOCATION_HERE, &heap); |
| 214 |
sec_offs = rec_get_offsets(sec_rec, sec_index, sec_offs, ULINT_UNDEFINED, |
| 215 |
UT_LOCATION_HERE, &heap); |
| 216 |
|
| 217 |
n = dict_index_get_n_ordering_defined_by_user(sec_index); |
| 218 |
|
| 219 |
for (i = 0; i < n; i++) { |
| 220 |
const dict_field_t *ifield; |
| 221 |
const dict_col_t *col; |
| 222 |
ulint clust_pos = 0; |
| 223 |
ulint clust_len; |
| 224 |
ulint len; |
| 225 |
row_ext_t *ext; |
| 226 |
|
| 227 |
ifield = sec_index->get_field(i); |
| 228 |
col = ifield->col; |
| 229 |
|
| 230 |
/* For virtual column, its value will need to be |
| 231 |
reconstructed from base column in cluster index */ |
| 232 |
if (col->is_virtual()) { |
| 233 |
const dict_v_col_t *v_col = reinterpret_cast<const dict_v_col_t *>(col); |
| 234 |
|
| 235 |
const dtuple_t *const row = |
| 236 |
row_build(ROW_COPY_POINTERS, clust_index, clust_rec, clust_offs, |
| 237 |
nullptr, nullptr, nullptr, &ext, heap); |
| 238 |
|
| 239 |
const dfield_t *const vfield = innobase_get_computed_value( |
| 240 |
row, v_col, table, &heap, heap, thr_get_trx(thr)->mysql_thd, |
| 241 |
thr->prebuilt->m_mysql_table); |
| 242 |
|
| 243 |
if (vfield == nullptr) { |
| 244 |
/* This may happen e.g. when this statement is executed in |
| 245 |
read-uncommited isolation and value (like json function) |
| 246 |
depends on an externally stored lob (like json) which |
| 247 |
was not written yet. */ |
| 248 |
err = DB_COMPUTE_VALUE_FAILED; |
| 249 |
goto func_exit; |
| 250 |
} |
| 251 |
|
| 252 |
clust_len = vfield->len; |
| 253 |
clust_field = static_cast<byte *>(vfield->data); |
| 254 |
|
| 255 |
} else { |
| 256 |
clust_pos = dict_col_get_clust_pos(col, clust_index); |
| 257 |
|
| 258 |
clust_field = rec_get_nth_field_instant(clust_rec, clust_offs, clust_pos, |
| 259 |
clust_index, &clust_len); |
| 260 |
} |
| 261 |
|
| 262 |
sec_field = rec_get_nth_field(nullptr, sec_rec, sec_offs, i, &sec_len); |
| 263 |
|
| 264 |
len = clust_len; |
| 265 |
|
| 266 |
if (ifield->prefix_len > 0 && len != UNIV_SQL_NULL && |
| 267 |
sec_len != UNIV_SQL_NULL && !col->is_virtual()) { |
| 268 |
if (rec_offs_nth_extern(clust_index, clust_offs, clust_pos)) { |
| 269 |
len -= BTR_EXTERN_FIELD_REF_SIZE; |
| 270 |
} |
| 271 |
|
| 272 |
len = dtype_get_at_most_n_mbchars(col->prtype, col->mbminmaxlen, |
| 273 |
ifield->prefix_len, len, |
| 274 |
(char *)clust_field); |
| 275 |
|
| 276 |
/* Check sec index field matches that of cluster index |
| 277 |
in the case of for table with ATOMIC BLOB, note |
| 278 |
we also need to check if sec_len is 0 */ |
| 279 |
if (rec_offs_nth_extern(clust_index, clust_offs, clust_pos) && |
| 280 |
(len < sec_len || |
| 281 |
(dict_table_has_atomic_blobs(sec_index->table) && sec_len == 0))) { |
| 282 |
if (!row_sel_sec_rec_is_for_blob( |
| 283 |
trx, col->mtype, col->prtype, col->mbminmaxlen, clust_field, |
| 284 |
clust_len, sec_field, sec_len, ifield->prefix_len, table)) { |
| 285 |
is_equal = false; |
| 286 |
goto func_exit; |
| 287 |
} |
| 288 |
|
| 289 |
continue; |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
/* For spatial index, the first field is MBR, we check |
| 294 |
if the MBR is equal or not. */ |
| 295 |
if (dict_index_is_spatial(sec_index) && i == 0) { |
| 296 |
rtr_mbr_t tmp_mbr; |
| 297 |
rtr_mbr_t sec_mbr; |
| 298 |
byte *dptr = const_cast<byte *>(clust_field); |
| 299 |
|
| 300 |
ut_ad(clust_len != UNIV_SQL_NULL); |
| 301 |
|
| 302 |
/* For externally stored field, we need to get full |
| 303 |
geo data to generate the MBR for comparing. */ |
| 304 |
if (rec_offs_nth_extern(clust_index, clust_offs, clust_pos)) { |
| 305 |
dptr = lob::btr_copy_externally_stored_field( |
| 306 |
trx, clust_index, &clust_len, nullptr, dptr, |
| 307 |
dict_tf_get_page_size(sec_index->table->flags), len, |
| 308 |
dict_index_is_sdi(sec_index), heap); |
| 309 |
} |
| 310 |
|
| 311 |
get_mbr_from_store(sec_index->rtr_srs.get(), dptr, |
| 312 |
static_cast<uint>(clust_len), SPDIMS, |
| 313 |
reinterpret_cast<double *>(&tmp_mbr), nullptr); |
| 314 |
rtr_read_mbr(sec_field, &sec_mbr); |
| 315 |
|
| 316 |
/* We use mbr_equal_physically() here because during UPDATE we have |
| 317 |
skipped updating Spatial Index only when existing MBR was matching |
| 318 |
physically to MBR of updated geometry. |
| 319 |
Checking for logical equality here will result in duplicate results |
| 320 |
if the MBR was logically equal but physically different. */ |
| 321 |
if (!mbr_equal_physically(&sec_mbr, &tmp_mbr)) { |
| 322 |
is_equal = false; |
| 323 |
goto func_exit; |
| 324 |
} |
| 325 |
} else if (col->is_multi_value()) { |
| 326 |
if (!is_multi_value_clust_and_sec_equal(clust_field, clust_len, sec_field, |
| 327 |
sec_len, col)) { |
| 328 |
is_equal = false; |
| 329 |
goto func_exit; |
| 330 |
} |
| 331 |
} else { |
| 332 |
/* We are testing for equality; ASC/DESC does not |
| 333 |
matter */ |
| 334 |
if (0 != cmp_data_data(col->mtype, col->prtype, true, clust_field, len, |
| 335 |
sec_field, sec_len)) { |
| 336 |
is_equal = false; |
| 337 |
goto func_exit; |
| 338 |
} |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
func_exit: |
| 343 |
if (UNIV_LIKELY_NULL(heap)) { |
| 344 |
mem_heap_free(heap); |
| 345 |
} |
| 346 |
return (err); |
| 347 |
} |
| 348 |
|
| 349 |
/** Creates a select node struct. |
| 350 |
@return own: select node struct */ |
| 351 |
sel_node_t *sel_node_create( |
| 352 |
mem_heap_t *heap) /*!< in: memory heap where created */ |
| 353 |
{ |
| 354 |
sel_node_t *node; |
| 355 |
|
| 356 |
node = static_cast<sel_node_t *>(mem_heap_alloc(heap, sizeof(sel_node_t))); |
| 357 |
|
| 358 |
node->common.type = QUE_NODE_SELECT; |
| 359 |
node->state = SEL_NODE_OPEN; |
| 360 |
|
| 361 |
node->plans = nullptr; |
| 362 |
|
| 363 |
return (node); |
| 364 |
} |
| 365 |
|
| 366 |
/** Frees the memory private to a select node when a query graph is freed, |
| 367 |
does not free the heap where the node was originally created. */ |
| 368 |
void sel_node_free_private(sel_node_t *node) /*!< in: select node struct */ |
| 369 |
{ |
| 370 |
ulint i; |
| 371 |
plan_t *plan; |
| 372 |
|
| 373 |
if (node->plans != nullptr) { |
| 374 |
for (i = 0; i < node->n_tables; i++) { |
| 375 |
plan = sel_node_get_nth_plan(node, i); |
| 376 |
|
| 377 |
plan->pcur.close(); |
| 378 |
plan->clust_pcur.close(); |
| 379 |
|
| 380 |
if (plan->old_vers_heap) { |
| 381 |
mem_heap_free(plan->old_vers_heap); |
| 382 |
} |
| 383 |
} |
| 384 |
} |
| 385 |
} |
| 386 |
|
| 387 |
/** Evaluates the values in a select list. If there are aggregate functions, |
| 388 |
their argument value is added to the aggregate total. */ |
| 389 |
static inline void sel_eval_select_list( |
| 390 |
sel_node_t *node) /*!< in: select node */ |
| 391 |
{ |
| 392 |
que_node_t *exp; |
| 393 |
|
| 394 |
exp = node->select_list; |
| 395 |
|
| 396 |
while (exp) { |
| 397 |
eval_exp(exp); |
| 398 |
|
| 399 |
exp = que_node_get_next(exp); |
| 400 |
} |
| 401 |
} |
| 402 |
|
| 403 |
/** Assigns the values in the select list to the possible into-variables in |
| 404 |
SELECT ... INTO ... */ |
| 405 |
static inline void sel_assign_into_var_values( |
| 406 |
sym_node_t *var, /*!< in: first variable in a |
| 407 |
list of variables */ |
| 408 |
sel_node_t *node) /*!< in: select node */ |
| 409 |
{ |
| 410 |
que_node_t *exp; |
| 411 |
|
| 412 |
if (var == nullptr) { |
| 413 |
return; |
| 414 |
} |
| 415 |
|
| 416 |
for (exp = node->select_list; var != nullptr; |
| 417 |
var = static_cast<sym_node_t *>(que_node_get_next(var))) { |
| 418 |
ut_ad(exp); |
| 419 |
|
| 420 |
eval_node_copy_val(var->alias, exp); |
| 421 |
|
| 422 |
exp = que_node_get_next(exp); |
| 423 |
} |
| 424 |
} |
| 425 |
|
| 426 |
/** Resets the aggregate value totals in the select list of an aggregate type |
| 427 |
query. */ |
| 428 |
static inline void sel_reset_aggregate_vals( |
| 429 |
sel_node_t *node) /*!< in: select node */ |
| 430 |
{ |
| 431 |
func_node_t *func_node; |
| 432 |
|
| 433 |
ut_ad(node->is_aggregate); |
| 434 |
|
| 435 |
for (func_node = static_cast<func_node_t *>(node->select_list); |
| 436 |
func_node != nullptr; |
| 437 |
func_node = static_cast<func_node_t *>(que_node_get_next(func_node))) { |
| 438 |
eval_node_set_int_val(func_node, 0); |
| 439 |
} |
| 440 |
|
| 441 |
node->aggregate_already_fetched = false; |
| 442 |
} |
| 443 |
|
| 444 |
/** Copies the input variable values when an explicit cursor is opened. */ |
| 445 |
static inline void row_sel_copy_input_variable_vals( |
| 446 |
sel_node_t *node) /*!< in: select node */ |
| 447 |
{ |
| 448 |
for (auto var : node->copy_variables) { |
| 449 |
eval_node_copy_val(var, var->alias); |
| 450 |
|
| 451 |
var->indirection = nullptr; |
| 452 |
} |
| 453 |
} |
| 454 |
|
| 455 |
/** Fetches the column values from a record. |
| 456 |
@param[in] trx the current transaction or nullptr |
| 457 |
@param[in] index record index |
| 458 |
@param[in] rec record in a clustered or non-clustered index; |
| 459 |
must be protected by a page latch |
| 460 |
@param[in] offsets rec_get_offsets(rec, index) |
| 461 |
@param[in] column first column in a column list, or NULL |
| 462 |
@param[in] allow_null_lob allow null lob if true. default is false. */ |
| 463 |
static void row_sel_fetch_columns(trx_t *trx, dict_index_t *index, |
| 464 |
const rec_t *rec, const ulint *offsets, |
| 465 |
sym_node_t *column, |
| 466 |
bool allow_null_lob = false) { |
| 467 |
dfield_t *val; |
| 468 |
ulint index_type; |
| 469 |
ulint field_no; |
| 470 |
const byte *data; |
| 471 |
ulint len; |
| 472 |
|
| 473 |
ut_ad(rec_offs_validate(rec, index, offsets)); |
| 474 |
|
| 475 |
if (index->is_clustered()) { |
| 476 |
index_type = SYM_CLUST_FIELD_NO; |
| 477 |
} else { |
| 478 |
index_type = SYM_SEC_FIELD_NO; |
| 479 |
} |
| 480 |
|
| 481 |
while (column) { |
| 482 |
mem_heap_t *heap = nullptr; |
| 483 |
bool needs_copy; |
| 484 |
|
| 485 |
field_no = column->field_nos[index_type]; |
| 486 |
|
| 487 |
if (field_no != ULINT_UNDEFINED) { |
| 488 |
if (UNIV_UNLIKELY(rec_offs_nth_extern(index, offsets, field_no))) { |
| 489 |
/* Copy an externally stored field to the |
| 490 |
temporary heap, if possible. */ |
| 491 |
|
| 492 |
heap = mem_heap_create(1, UT_LOCATION_HERE); |
| 493 |
|
| 494 |
data = lob::btr_rec_copy_externally_stored_field( |
| 495 |
trx, index, rec, offsets, dict_table_page_size(index->table), |
| 496 |
field_no, &len, nullptr, dict_index_is_sdi(index), heap); |
| 497 |
|
| 498 |
if (data == nullptr) { |
| 499 |
/* This means that the externally stored field was not written yet. |
| 500 |
This record should only be seen by following situations: |
| 501 |
- Read uncommitted transactions (TRX_ISO_READ_UNCOMMITTED) |
| 502 |
- During crash recovery [trx_rollback_or_clean_all_recovered().] |
| 503 |
- During lock-less consistent read, when the trx reads LOB even |
| 504 |
though the clust_rec is not to be seen. */ |
| 505 |
ut_ad(allow_null_lob); |
| 506 |
len = UNIV_SQL_NULL; |
| 507 |
needs_copy = false; |
| 508 |
} else { |
| 509 |
needs_copy = true; |
| 510 |
} |
| 511 |
} else { |
| 512 |
data = rec_get_nth_field_instant(rec, offsets, field_no, index, &len); |
| 513 |
|
| 514 |
needs_copy = column->copy_val; |
| 515 |
} |
| 516 |
|
| 517 |
if (needs_copy) { |
| 518 |
eval_node_copy_and_alloc_val(column, data, len); |
| 519 |
} else { |
| 520 |
val = que_node_get_val(column); |
| 521 |
dfield_set_data(val, data, len); |
| 522 |
} |
| 523 |
|
| 524 |
if (UNIV_LIKELY_NULL(heap)) { |
| 525 |
mem_heap_free(heap); |
| 526 |
} |
| 527 |
} |
| 528 |
|
| 529 |
column = UT_LIST_GET_NEXT(col_var_list, column); |
| 530 |
} |
| 531 |
} |
| 532 |
|
| 533 |
/** Allocates a prefetch buffer for a column when prefetch is first time done. |
| 534 |
*/ |
| 535 |
static void sel_col_prefetch_buf_alloc( |
| 536 |
sym_node_t *column) /*!< in: symbol table node for a column */ |
| 537 |
{ |
| 538 |
sel_buf_t *sel_buf; |
| 539 |
ulint i; |
| 540 |
|
| 541 |
ut_ad(que_node_get_type(column) == QUE_NODE_SYMBOL); |
| 542 |
|
| 543 |
column->prefetch_buf = static_cast<sel_buf_t *>(ut::malloc_withkey( |
| 544 |
UT_NEW_THIS_FILE_PSI_KEY, SEL_MAX_N_PREFETCH * sizeof(sel_buf_t))); |
| 545 |
|
| 546 |
for (i = 0; i < SEL_MAX_N_PREFETCH; i++) { |
| 547 |
sel_buf = column->prefetch_buf + i; |
| 548 |
|
| 549 |
sel_buf->data = nullptr; |
| 550 |
sel_buf->len = 0; |
| 551 |
sel_buf->val_buf_size = 0; |
| 552 |
} |
| 553 |
} |
| 554 |
|
| 555 |
/** Frees a prefetch buffer for a column, including the dynamically allocated |
| 556 |
memory for data stored there. */ |
| 557 |
void sel_col_prefetch_buf_free( |
| 558 |
sel_buf_t *prefetch_buf) /*!< in, own: prefetch buffer */ |
| 559 |
{ |
| 560 |
sel_buf_t *sel_buf; |
| 561 |
ulint i; |
| 562 |
|
| 563 |
for (i = 0; i < SEL_MAX_N_PREFETCH; i++) { |
| 564 |
sel_buf = prefetch_buf + i; |
| 565 |
|
| 566 |
if (sel_buf->val_buf_size > 0) { |
| 567 |
ut::free(sel_buf->data); |
| 568 |
} |
| 569 |
} |
| 570 |
|
| 571 |
ut::free(prefetch_buf); |
| 572 |
} |
| 573 |
|
| 574 |
/** Pops the column values for a prefetched, cached row from the column prefetch |
| 575 |
buffers and places them to the val fields in the column nodes. */ |
| 576 |
static void sel_dequeue_prefetched_row( |
| 577 |
plan_t *plan) /*!< in: plan node for a table */ |
| 578 |
{ |
| 579 |
sel_buf_t *sel_buf; |
| 580 |
dfield_t *val; |
| 581 |
byte *data; |
| 582 |
ulint len; |
| 583 |
ulint val_buf_size; |
| 584 |
|
| 585 |
ut_ad(plan->n_rows_prefetched > 0); |
| 586 |
|
| 587 |
for (auto column : plan->columns) { |
| 588 |
val = que_node_get_val(column); |
| 589 |
|
| 590 |
if (!column->copy_val) { |
| 591 |
/* We did not really push any value for the |
| 592 |
column */ |
| 593 |
|
| 594 |
ut_ad(!column->prefetch_buf); |
| 595 |
ut_ad(que_node_get_val_buf_size(column) == 0); |
| 596 |
ut_d(dfield_set_null(val)); |
| 597 |
|
| 598 |
continue; |
| 599 |
} |
| 600 |
|
| 601 |
ut_ad(column->prefetch_buf); |
| 602 |
ut_ad(!dfield_is_ext(val)); |
| 603 |
|
| 604 |
sel_buf = column->prefetch_buf + plan->first_prefetched; |
| 605 |
|
| 606 |
data = sel_buf->data; |
| 607 |
len = sel_buf->len; |
| 608 |
val_buf_size = sel_buf->val_buf_size; |
| 609 |
|
| 610 |
/* We must keep track of the allocated memory for |
| 611 |
column values to be able to free it later: therefore |
| 612 |
we swap the values for sel_buf and val */ |
| 613 |
|
| 614 |
sel_buf->data = static_cast<byte *>(dfield_get_data(val)); |
| 615 |
sel_buf->len = dfield_get_len(val); |
| 616 |
sel_buf->val_buf_size = que_node_get_val_buf_size(column); |
| 617 |
|
| 618 |
dfield_set_data(val, data, len); |
| 619 |
que_node_set_val_buf_size(column, val_buf_size); |
| 620 |
} |
| 621 |
|
| 622 |
plan->n_rows_prefetched--; |
| 623 |
|
| 624 |
plan->first_prefetched++; |
| 625 |
} |
| 626 |
|
| 627 |
/** Pushes the column values for a prefetched, cached row to the column prefetch |
| 628 |
buffers from the val fields in the column nodes. */ |
| 629 |
static inline void sel_enqueue_prefetched_row( |
| 630 |
plan_t *plan) /*!< in: plan node for a table */ |
| 631 |
{ |
| 632 |
sel_buf_t *sel_buf; |
| 633 |
dfield_t *val; |
| 634 |
byte *data; |
| 635 |
ulint len; |
| 636 |
ulint pos; |
| 637 |
ulint val_buf_size; |
| 638 |
|
| 639 |
if (plan->n_rows_prefetched == 0) { |
| 640 |
pos = 0; |
| 641 |
plan->first_prefetched = 0; |
| 642 |
} else { |
| 643 |
pos = plan->n_rows_prefetched; |
| 644 |
|
| 645 |
/* We have the convention that pushing new rows starts only |
| 646 |
after the prefetch stack has been emptied: */ |
| 647 |
|
| 648 |
ut_ad(plan->first_prefetched == 0); |
| 649 |
} |
| 650 |
|
| 651 |
plan->n_rows_prefetched++; |
| 652 |
|
| 653 |
ut_ad(pos < SEL_MAX_N_PREFETCH); |
| 654 |
|
| 655 |
for (auto column : plan->columns) { |
| 656 |
if (!column->copy_val) { |
| 657 |
/* There is no sense to push pointers to database |
| 658 |
page fields when we do not keep latch on the page! */ |
| 659 |
continue; |
| 660 |
} |
| 661 |
|
| 662 |
if (!column->prefetch_buf) { |
| 663 |
/* Allocate a new prefetch buffer */ |
| 664 |
|
| 665 |
sel_col_prefetch_buf_alloc(column); |
| 666 |
} |
| 667 |
|
| 668 |
sel_buf = column->prefetch_buf + pos; |
| 669 |
|
| 670 |
val = que_node_get_val(column); |
| 671 |
|
| 672 |
data = static_cast<byte *>(dfield_get_data(val)); |
| 673 |
len = dfield_get_len(val); |
| 674 |
val_buf_size = que_node_get_val_buf_size(column); |
| 675 |
|
| 676 |
/* We must keep track of the allocated memory for |
| 677 |
column values to be able to free it later: therefore |
| 678 |
we swap the values for sel_buf and val */ |
| 679 |
|
| 680 |
dfield_set_data(val, sel_buf->data, sel_buf->len); |
| 681 |
que_node_set_val_buf_size(column, sel_buf->val_buf_size); |
| 682 |
|
| 683 |
sel_buf->data = data; |
| 684 |
sel_buf->len = len; |
| 685 |
sel_buf->val_buf_size = val_buf_size; |
| 686 |
} |
| 687 |
} |
| 688 |
|
| 689 |
/** Builds a previous version of a clustered index record for a consistent read |
| 690 |
@return DB_SUCCESS or error code */ |
| 691 |
[[nodiscard]] static dberr_t row_sel_build_prev_vers( |
| 692 |
ReadView *read_view, /*!< in: read view */ |
| 693 |
dict_index_t *index, /*!< in: plan node for table */ |
| 694 |
rec_t *rec, /*!< in: record in a clustered index */ |
| 695 |
ulint **offsets, /*!< in/out: offsets returned by |
| 696 |
rec_get_offsets(rec, plan->index) */ |
| 697 |
mem_heap_t **offset_heap, /*!< in/out: memory heap from which |
| 698 |
the offsets are allocated */ |
| 699 |
mem_heap_t **old_vers_heap, /*!< out: old version heap to use */ |
| 700 |
rec_t **old_vers, /*!< out: old version, or NULL if the |
| 701 |
record does not exist in the view: |
| 702 |
i.e., it was freshly inserted |
| 703 |
afterwards */ |
| 704 |
mtr_t *mtr) /*!< in: mtr */ |
| 705 |
{ |
| 706 |
dberr_t err; |
| 707 |
|
| 708 |
if (*old_vers_heap) { |
| 709 |
mem_heap_empty(*old_vers_heap); |
| 710 |
} else { |
| 711 |
*old_vers_heap = mem_heap_create(512, UT_LOCATION_HERE); |
| 712 |
} |
| 713 |
|
| 714 |
err = row_vers_build_for_consistent_read(rec, mtr, index, offsets, read_view, |
| 715 |
offset_heap, *old_vers_heap, |
| 716 |
old_vers, nullptr, nullptr); |
| 717 |
return (err); |
| 718 |
} |
| 719 |
|
| 720 |
/** Builds the last committed version of a clustered index record for a |
| 721 |
semi-consistent read. */ |
| 722 |
static void row_sel_build_committed_vers_for_mysql( |
| 723 |
dict_index_t *clust_index, /*!< in: clustered index */ |
| 724 |
row_prebuilt_t *prebuilt, /*!< in: prebuilt struct */ |
| 725 |
const rec_t *rec, /*!< in: record in a clustered index */ |
| 726 |
ulint **offsets, /*!< in/out: offsets returned by |
| 727 |
rec_get_offsets(rec, clust_index) */ |
| 728 |
mem_heap_t **offset_heap, /*!< in/out: memory heap from which |
| 729 |
the offsets are allocated */ |
| 730 |
const rec_t **old_vers, /*!< out: old version, or NULL if the |
| 731 |
record does not exist in the view: |
| 732 |
i.e., it was freshly inserted |
| 733 |
afterwards */ |
| 734 |
const dtuple_t **vrow, /*!< out: to be filled with old virtual |
| 735 |
column version if any */ |
| 736 |
mtr_t *mtr) /*!< in: mtr */ |
| 737 |
{ |
| 738 |
if (prebuilt->old_vers_heap) { |
| 739 |
mem_heap_empty(prebuilt->old_vers_heap); |
| 740 |
} else { |
| 741 |
prebuilt->old_vers_heap = |
| 742 |
mem_heap_create(rec_offs_size(*offsets), UT_LOCATION_HERE); |
| 743 |
} |
| 744 |
|
| 745 |
row_vers_build_for_semi_consistent_read(rec, mtr, clust_index, offsets, |
| 746 |
offset_heap, prebuilt->old_vers_heap, |
| 747 |
old_vers, vrow); |
| 748 |
} |
| 749 |
|
| 750 |
/** Tests the conditions which determine when the index segment we are searching |
| 751 |
through has been exhausted. |
| 752 |
@return true if row passed the tests */ |
| 753 |
static inline bool row_sel_test_end_conds( |
| 754 |
plan_t *plan) /*!< in: plan for the table; the column values must |
| 755 |
already have been retrieved and the right sides of |
| 756 |
comparisons evaluated */ |
| 757 |
{ |
| 758 |
/* All conditions in end_conds are comparisons of a column to an |
| 759 |
expression */ |
| 760 |
|
| 761 |
for (auto cond : plan->end_conds) { |
| 762 |
/* Evaluate the left side of the comparison, i.e., get the |
| 763 |
column value if there is an indirection */ |
| 764 |
|
| 765 |
eval_sym(static_cast<sym_node_t *>(cond->args)); |
| 766 |
|
| 767 |
/* Do the comparison */ |
| 768 |
|
| 769 |
if (!eval_cmp(cond)) { |
| 770 |
return false; |
| 771 |
} |
| 772 |
} |
| 773 |
|
| 774 |
return true; |
| 775 |
} |
| 776 |
|
| 777 |
/** Tests the other conditions. |
| 778 |
@return true if row passed the tests */ |
| 779 |
static inline bool row_sel_test_other_conds( |
| 780 |
plan_t *plan) /*!< in: plan for the table; the column values must |
| 781 |
already have been retrieved */ |
| 782 |
{ |
| 783 |
for (auto cond : plan->other_conds) { |
| 784 |
eval_exp(cond); |
| 785 |
|
| 786 |
if (!eval_node_get_bool_val(cond)) { |
| 787 |
return false; |
| 788 |
} |
| 789 |
} |
| 790 |
|
| 791 |
return true; |
| 792 |
} |
| 793 |
|
| 794 |
/** Retrieves the clustered index record corresponding to a record in a |
| 795 |
non-clustered index. Does the necessary locking. |
| 796 |
@return DB_SUCCESS or error code */ |
| 797 |
[[nodiscard]] static dberr_t row_sel_get_clust_rec( |
| 798 |
sel_node_t *node, /*!< in: select_node */ |
| 799 |
plan_t *plan, /*!< in: plan node for table */ |
| 800 |
rec_t *rec, /*!< in: record in a non-clustered index */ |
| 801 |
que_thr_t *thr, /*!< in: query thread */ |
| 802 |
rec_t **out_rec, /*!< out: clustered record or an old version of |
| 803 |
it, NULL if the old version did not exist |
| 804 |
in the read view, i.e., it was a fresh |
| 805 |
inserted version */ |
| 806 |
mtr_t *mtr) /*!< in: mtr used to get access to the |
| 807 |
non-clustered record; the same mtr is used to |
| 808 |
access the clustered index */ |
| 809 |
{ |
| 810 |
dict_index_t *index; |
| 811 |
rec_t *clust_rec; |
| 812 |
rec_t *old_vers; |
| 813 |
dberr_t err; |
| 814 |
mem_heap_t *heap = nullptr; |
| 815 |
ulint offsets_[REC_OFFS_NORMAL_SIZE]; |
| 816 |
ulint *offsets = offsets_; |
| 817 |
rec_offs_init(offsets_); |
| 818 |
|
| 819 |
*out_rec = nullptr; |
| 820 |
|
| 821 |
offsets = rec_get_offsets(rec, plan->pcur.get_btr_cur()->index, offsets, |
| 822 |
ULINT_UNDEFINED, UT_LOCATION_HERE, &heap); |
| 823 |
|
| 824 |
row_build_row_ref_fast(plan->clust_ref, plan->clust_map, rec, offsets); |
| 825 |
|
| 826 |
index = plan->table->first_index(); |
| 827 |
|
| 828 |
plan->clust_pcur.open_no_init(index, plan->clust_ref, PAGE_CUR_LE, |
| 829 |
BTR_SEARCH_LEAF, 0, mtr, UT_LOCATION_HERE); |
| 830 |
|
| 831 |
clust_rec = plan->clust_pcur.get_rec(); |
| 832 |
|
| 833 |
/* Note: only if the search ends up on a non-infimum record is the |
| 834 |
low_match value the real match to the search tuple */ |
| 835 |
|
| 836 |
if (!page_rec_is_user_rec(clust_rec) || |
| 837 |
plan->clust_pcur.get_low_match() < dict_index_get_n_unique(index)) { |
| 838 |
ut_a(rec_get_deleted_flag(rec, dict_table_is_comp(plan->table))); |
| 839 |
ut_a(node->read_view); |
| 840 |
|
| 841 |
/* In a rare case it is possible that no clust rec is found |
| 842 |
for a delete-marked secondary index record: if in row0umod.cc |
| 843 |
in row_undo_mod_remove_clust_low() we have already removed |
| 844 |
the clust rec, while purge is still cleaning and removing |
| 845 |
secondary index records associated with earlier versions of |
| 846 |
the clustered index record. In that case we know that the |
| 847 |
clustered index record did not exist in the read view of |
| 848 |
trx. */ |
| 849 |
|
| 850 |
goto func_exit; |
| 851 |
} |
| 852 |
|
| 853 |
offsets = rec_get_offsets(clust_rec, index, offsets, ULINT_UNDEFINED, |
| 854 |
UT_LOCATION_HERE, &heap); |
| 855 |
|
| 856 |
if (!node->read_view) { |
| 857 |
/* Try to place a lock on the index record */ |
| 858 |
|
| 859 |
ulint lock_type; |
| 860 |
trx_t *trx; |
| 861 |
|
| 862 |
trx = thr_get_trx(thr); |
| 863 |
|
| 864 |
lock_type = trx->skip_gap_locks() ? LOCK_REC_NOT_GAP : LOCK_ORDINARY; |
| 865 |
|
| 866 |
err = lock_clust_rec_read_check_and_lock( |
| 867 |
lock_duration_t::REGULAR, plan->clust_pcur.get_block(), clust_rec, |
| 868 |
index, offsets, SELECT_ORDINARY, |
| 869 |
static_cast<lock_mode>(node->row_lock_mode), lock_type, thr); |
| 870 |
|
| 871 |
switch (err) { |
| 872 |
case DB_SUCCESS: |
| 873 |
case DB_SUCCESS_LOCKED_REC: |
| 874 |
/* Declare the variable uninitialized in Valgrind. |
| 875 |
It should be set to DB_SUCCESS at func_exit. */ |
| 876 |
UNIV_MEM_INVALID(&err, sizeof err); |
| 877 |
break; |
| 878 |
default: |
| 879 |
goto err_exit; |
| 880 |
} |
| 881 |
} else { |
| 882 |
/* This is a non-locking consistent read: if necessary, fetch |
| 883 |
a previous version of the record */ |
| 884 |
|
| 885 |
old_vers = nullptr; |
| 886 |
|
| 887 |
if (!lock_clust_rec_cons_read_sees(clust_rec, index, offsets, |
| 888 |
node->read_view)) { |
| 889 |
err = |
| 890 |
row_sel_build_prev_vers(node->read_view, index, clust_rec, &offsets, |
| 891 |
&heap, &plan->old_vers_heap, &old_vers, mtr); |
| 892 |
|
| 893 |
if (err != DB_SUCCESS) { |
| 894 |
goto err_exit; |
| 895 |
} |
| 896 |
|
| 897 |
clust_rec = old_vers; |
| 898 |
|
| 899 |
if (clust_rec == nullptr) { |
| 900 |
goto func_exit; |
| 901 |
} |
| 902 |
} |
| 903 |
|
| 904 |
/* If we had to go to an earlier version of row or the |
| 905 |
secondary index record is delete marked, then it may be that |
| 906 |
the secondary index record corresponding to clust_rec |
| 907 |
(or old_vers) is not rec; in that case we must ignore |
| 908 |
such row because in our snapshot rec would not have existed. |
| 909 |
Remember that from rec we cannot see directly which transaction |
| 910 |
id corresponds to it: we have to go to the clustered index |
| 911 |
record. A query where we want to fetch all rows where |
| 912 |
the secondary index value is in some interval would return |
| 913 |
a wrong result if we would not drop rows which we come to |
| 914 |
visit through secondary index records that would not really |
| 915 |
exist in our snapshot. */ |
| 916 |
|
| 917 |
if (old_vers || |
| 918 |
rec_get_deleted_flag(rec, dict_table_is_comp(plan->table))) { |
| 919 |
bool rec_equal; |
| 920 |
|
| 921 |
err = row_sel_sec_rec_is_for_clust_rec(rec, plan->index, clust_rec, index, |
| 922 |
thr, rec_equal); |
| 923 |
if (err != DB_SUCCESS) { |
| 924 |
goto err_exit; |
| 925 |
} else if (!rec_equal) { |
| 926 |
goto func_exit; |
| 927 |
} |
| 928 |
} |
| 929 |
} |
| 930 |
|
| 931 |
/* Fetch the columns needed in test conditions. The clustered |
| 932 |
index record is protected by a page latch that was acquired |
| 933 |
when plan->clust_pcur was positioned. The latch will not be |
| 934 |
released until mtr_commit(mtr). */ |
| 935 |
|
| 936 |
ut_ad(!rec_get_deleted_flag(clust_rec, rec_offs_comp(offsets))); |
| 937 |
row_sel_fetch_columns(thr_get_trx(thr), index, clust_rec, offsets, |
| 938 |
UT_LIST_GET_FIRST(plan->columns)); |
| 939 |
*out_rec = clust_rec; |
| 940 |
func_exit: |
| 941 |
err = DB_SUCCESS; |
| 942 |
err_exit: |
| 943 |
if (UNIV_LIKELY_NULL(heap)) { |
| 944 |
mem_heap_free(heap); |
| 945 |
} |
| 946 |
return (err); |
| 947 |
} |
| 948 |
|
| 949 |
/** Sets a lock on a page of R-Tree record. This is all or none action, |
| 950 |
mostly due to we cannot reposition a record in R-Tree (with the |
| 951 |
nature of splitting) |
| 952 |
@param[in] pcur cursor |
| 953 |
@param[in] first_rec record |
| 954 |
@param[in] index index |
| 955 |
@param[in] offsets rec_get_offsets(rec, index) |
| 956 |
@param[in] sel_mode select mode: SELECT_ORDINARY, |
| 957 |
SELECT_SKIP_LOKCED, or SELECT_NO_WAIT |
| 958 |
@param[in] mode lock mode |
| 959 |
@param[in] type LOCK_ORDINARY, LOCK_GAP, or LOC_REC_NOT_GAP |
| 960 |
@param[in] thr query thread |
| 961 |
@param[in] mtr mtr |
| 962 |
@return DB_SUCCESS, DB_SUCCESS_LOCKED_REC, or error code */ |
| 963 |
static inline dberr_t sel_set_rtr_rec_lock( |
| 964 |
btr_pcur_t *pcur, const rec_t *first_rec, dict_index_t *index, |
| 965 |
const ulint *offsets, select_mode sel_mode, ulint mode, ulint type, |
| 966 |
que_thr_t *thr, mtr_t *mtr) { |
| 967 |
matched_rec_t *match = pcur->m_btr_cur.rtr_info->matches; |
| 968 |
mem_heap_t *heap = nullptr; |
| 969 |
dberr_t err = DB_SUCCESS; |
| 970 |
trx_t *trx = thr_get_trx(thr); |
| 971 |
buf_block_t *cur_block = pcur->get_block(); |
| 972 |
ulint offsets_[REC_OFFS_NORMAL_SIZE]; |
| 973 |
ulint *my_offsets = const_cast<ulint *>(offsets); |
| 974 |
rec_t *rec = const_cast<rec_t *>(first_rec); |
| 975 |
rtr_rec_vector *match_rec; |
| 976 |
rtr_rec_vector::iterator end; |
| 977 |
|
| 978 |
rec_offs_init(offsets_); |
| 979 |
|
| 980 |
if (match->locked || page_rec_is_supremum(first_rec)) { |
| 981 |
return (DB_SUCCESS_LOCKED_REC); |
| 982 |
} |
| 983 |
|
| 984 |
ut_ad(page_align(first_rec) == cur_block->frame); |
| 985 |
ut_ad(match->valid); |
| 986 |
|
| 987 |
rw_lock_x_lock(&(match->block.lock), UT_LOCATION_HERE); |
| 988 |
retry: |
| 989 |
cur_block = pcur->get_block(); |
| 990 |
ut_ad(rw_lock_own(&(match->block.lock), RW_LOCK_X) || |
| 991 |
rw_lock_own(&(match->block.lock), RW_LOCK_S)); |
| 992 |
ut_ad(page_is_leaf(buf_block_get_frame(cur_block))); |
| 993 |
|
| 994 |
err = lock_sec_rec_read_check_and_lock( |
| 995 |
lock_duration_t::REGULAR, cur_block, rec, index, my_offsets, sel_mode, |
| 996 |
static_cast<lock_mode>(mode), type, thr); |
| 997 |
|
| 998 |
switch (err) { |
| 999 |
case DB_SUCCESS: |
| 1000 |
case DB_SUCCESS_LOCKED_REC: |
| 1001 |
case DB_SKIP_LOCKED: |
| 1002 |
goto lock_match; |
| 1003 |
|
| 1004 |
case DB_LOCK_WAIT: |
| 1005 |
re_scan: |
| 1006 |
mtr_commit(mtr); |
| 1007 |
trx->error_state = err; |
| 1008 |
que_thr_stop_for_mysql(thr); |
| 1009 |
thr->lock_state = QUE_THR_LOCK_ROW; |
| 1010 |
if (row_mysql_handle_errors(&err, trx, thr, nullptr)) { |
| 1011 |
thr->lock_state = QUE_THR_LOCK_NOLOCK; |
| 1012 |
mtr_start(mtr); |
| 1013 |
|
| 1014 |
mutex_enter(&match->rtr_match_mutex); |
| 1015 |
if (!match->valid && match->matched_recs->empty()) { |
| 1016 |
mutex_exit(&match->rtr_match_mutex); |
| 1017 |
err = DB_RECORD_NOT_FOUND; |
| 1018 |
goto func_end; |
| 1019 |
} |
| 1020 |
mutex_exit(&match->rtr_match_mutex); |
| 1021 |
|
| 1022 |
page_no_t page_no = page_get_page_no(pcur->get_page()); |
| 1023 |
page_id_t page_id(dict_index_get_space(index), page_no); |
| 1024 |
|
| 1025 |
cur_block = buf_page_get_gen( |
| 1026 |
page_id, dict_table_page_size(index->table), RW_X_LATCH, nullptr, |
| 1027 |
Page_fetch::NORMAL, UT_LOCATION_HERE, mtr); |
| 1028 |
} else { |
| 1029 |
mtr_start(mtr); |
| 1030 |
goto func_end; |
| 1031 |
} |
| 1032 |
|
| 1033 |
DEBUG_SYNC_C("rtr_set_lock_wait"); |
| 1034 |
|
| 1035 |
if (!match->valid) { |
| 1036 |
/* Page got deleted */ |
| 1037 |
mtr_commit(mtr); |
| 1038 |
mtr_start(mtr); |
| 1039 |
err = DB_RECORD_NOT_FOUND; |
| 1040 |
goto func_end; |
| 1041 |
} |
| 1042 |
|
| 1043 |
match->matched_recs->clear(); |
| 1044 |
|
| 1045 |
rtr_cur_search_with_match( |
| 1046 |
cur_block, index, pcur->m_btr_cur.rtr_info->search_tuple, |
| 1047 |
pcur->m_btr_cur.rtr_info->search_mode, &pcur->m_btr_cur.page_cur, |
| 1048 |
pcur->m_btr_cur.rtr_info); |
| 1049 |
|
| 1050 |
if (!page_is_leaf(buf_block_get_frame(cur_block))) { |
| 1051 |
/* Page got split and promoted (only for |
| 1052 |
root page it is possible). Release the |
| 1053 |
page and ask for a re-search */ |
| 1054 |
mtr_commit(mtr); |
| 1055 |
mtr_start(mtr); |
| 1056 |
err = DB_RECORD_NOT_FOUND; |
| 1057 |
goto func_end; |
| 1058 |
} |
| 1059 |
|
| 1060 |
rec = pcur->get_rec(); |
| 1061 |
my_offsets = offsets_; |
| 1062 |
my_offsets = rec_get_offsets(rec, index, my_offsets, ULINT_UNDEFINED, |
| 1063 |
UT_LOCATION_HERE, &heap); |
| 1064 |
|
| 1065 |
/* No match record */ |
| 1066 |
if (page_rec_is_supremum(rec) || !match->valid) { |
| 1067 |
mtr_commit(mtr); |
| 1068 |
mtr_start(mtr); |
| 1069 |
err = DB_RECORD_NOT_FOUND; |
| 1070 |
goto func_end; |
| 1071 |
} |
| 1072 |
|
| 1073 |
goto retry; |
| 1074 |
|
| 1075 |
default: |
| 1076 |
goto func_end; |
| 1077 |
} |
| 1078 |
|
| 1079 |
lock_match: |
| 1080 |
my_offsets = offsets_; |
| 1081 |
match_rec = match->matched_recs; |
| 1082 |
end = match_rec->end(); |
| 1083 |
|
| 1084 |
for (rtr_rec_vector::iterator it = match_rec->begin(); it != end; ++it) { |
| 1085 |
rtr_rec_t *rtr_rec = &(*it); |
| 1086 |
|
| 1087 |
my_offsets = rec_get_offsets(rtr_rec->r_rec, index, my_offsets, |
| 1088 |
ULINT_UNDEFINED, UT_LOCATION_HERE, &heap); |
| 1089 |
|
| 1090 |
err = lock_sec_rec_read_check_and_lock( |
| 1091 |
lock_duration_t::REGULAR, &match->block, rtr_rec->r_rec, index, |
| 1092 |
my_offsets, sel_mode, static_cast<lock_mode>(mode), type, thr); |
| 1093 |
|
| 1094 |
switch (err) { |
| 1095 |
case DB_SUCCESS: |
| 1096 |
case DB_SUCCESS_LOCKED_REC: |
| 1097 |
rtr_rec->locked = true; |
| 1098 |
break; |
| 1099 |
|
| 1100 |
case DB_LOCK_WAIT: |
| 1101 |
goto re_scan; |
| 1102 |
|
| 1103 |
case DB_SKIP_LOCKED: |
| 1104 |
break; |
| 1105 |
|
| 1106 |
default: |
| 1107 |
goto func_end; |
| 1108 |
} |
| 1109 |
} |
| 1110 |
|
| 1111 |
match->locked = true; |
| 1112 |
|
| 1113 |
func_end: |
| 1114 |
rw_lock_x_unlock(&(match->block.lock)); |
| 1115 |
if (heap != nullptr) { |
| 1116 |
mem_heap_free(heap); |
| 1117 |
} |
| 1118 |
|
| 1119 |
ut_ad(err != DB_LOCK_WAIT); |
| 1120 |
|
| 1121 |
return (err); |
| 1122 |
} |
| 1123 |
|
| 1124 |
/** Sets a lock on a record. |
| 1125 |
mostly due to we cannot reposition a record in R-Tree (with the |
| 1126 |
nature of splitting) |
| 1127 |
@param[in] pcur cursor |
| 1128 |
@param[in] rec record |
| 1129 |
@param[in] index index |
| 1130 |
@param[in] offsets rec_get_offsets(rec, index) |
| 1131 |
@param[in] sel_mode select mode: SELECT_ORDINARY, |
| 1132 |
SELECT_SKIP_LOKCED, or SELECT_NO_WAIT |
| 1133 |
@param[in] mode lock mode |
| 1134 |
@param[in] type LOCK_ORDINARY, LOCK_GAP, or LOC_REC_NOT_GAP |
| 1135 |
@param[in] thr query thread |
| 1136 |
@param[in] mtr mtr |
| 1137 |
@return DB_SUCCESS, DB_SUCCESS_LOCKED_REC, or error code */ |
| 1138 |
static inline dberr_t sel_set_rec_lock(btr_pcur_t *pcur, const rec_t *rec, |
| 1139 |
dict_index_t *index, |
| 1140 |
const ulint *offsets, |
| 1141 |
select_mode sel_mode, ulint mode, |
| 1142 |
ulint type, que_thr_t *thr, mtr_t *mtr) { |
| 1143 |
trx_t *trx; |
| 1144 |
dberr_t err = DB_SUCCESS; |
| 1145 |
const buf_block_t *block; |
| 1146 |
|
| 1147 |
block = pcur->get_block(); |
| 1148 |
|
| 1149 |
trx = thr_get_trx(thr); |
| 1150 |
ut_ad(trx_can_be_handled_by_current_thread(trx)); |
| 1151 |
|
| 1152 |
if (UT_LIST_GET_LEN(trx->lock.trx_locks) > 10000) { |
| 1153 |
if (buf_LRU_buf_pool_running_out()) { |
| 1154 |
return (DB_LOCK_TABLE_FULL); |
| 1155 |
} |
| 1156 |
} |
| 1157 |
|
| 1158 |
if (index->is_clustered()) { |
| 1159 |
err = lock_clust_rec_read_check_and_lock( |
| 1160 |
lock_duration_t::REGULAR, block, rec, index, offsets, sel_mode, |
| 1161 |
static_cast<lock_mode>(mode), type, thr); |
| 1162 |
} else { |
| 1163 |
if (dict_index_is_spatial(index)) { |
| 1164 |
if (type == LOCK_GAP || type == LOCK_ORDINARY) { |
| 1165 |
ib::error(ER_IB_MSG_1026) << "Incorrectly request GAP lock " |
| 1166 |
"on RTree"; |
| 1167 |
ut_d(ut_error); |
| 1168 |
ut_o(return (DB_SUCCESS)); |
| 1169 |
} |
| 1170 |
err = sel_set_rtr_rec_lock(pcur, rec, index, offsets, sel_mode, mode, |
| 1171 |
type, thr, mtr); |
| 1172 |
} else { |
| 1173 |
err = lock_sec_rec_read_check_and_lock( |
| 1174 |
lock_duration_t::REGULAR, block, rec, index, offsets, sel_mode, |
| 1175 |
static_cast<lock_mode>(mode), type, thr); |
| 1176 |
} |
| 1177 |
} |
| 1178 |
|
| 1179 |
return (err); |
| 1180 |
} |
| 1181 |
|
| 1182 |
/** Opens a pcur to a table index. */ |
| 1183 |
static void row_sel_open_pcur(plan_t *plan, /*!< in: table plan */ |
| 1184 |
bool search_latch_locked, |
| 1185 |
/*!< in: true if the thread currently |
| 1186 |
has the search latch locked in |
| 1187 |
s-mode */ |
| 1188 |
mtr_t *mtr) /*!< in: mtr */ |
| 1189 |
{ |
| 1190 |
dict_index_t *index; |
| 1191 |
que_node_t *exp; |
| 1192 |
ulint n_fields; |
| 1193 |
ulint has_search_latch = 0; /* RW_S_LATCH or 0 */ |
| 1194 |
ulint i; |
| 1195 |
|
| 1196 |
if (search_latch_locked) { |
| 1197 |
has_search_latch = RW_S_LATCH; |
| 1198 |
} |
| 1199 |
|
| 1200 |
index = plan->index; |
| 1201 |
|
| 1202 |
/* Calculate the value of the search tuple: the exact match columns |
| 1203 |
get their expressions evaluated when we evaluate the right sides of |
| 1204 |
end_conds */ |
| 1205 |
|
| 1206 |
for (auto cond : plan->end_conds) { |
| 1207 |
eval_exp(que_node_get_next(cond->args)); |
| 1208 |
} |
| 1209 |
|
| 1210 |
if (plan->tuple) { |
| 1211 |
n_fields = dtuple_get_n_fields(plan->tuple); |
| 1212 |
|
| 1213 |
if (plan->n_exact_match < n_fields) { |
| 1214 |
/* There is a non-exact match field which must be |
| 1215 |
evaluated separately */ |
| 1216 |
|
| 1217 |
eval_exp(plan->tuple_exps[n_fields - 1]); |
| 1218 |
} |
| 1219 |
|
| 1220 |
for (i = 0; i < n_fields; i++) { |
| 1221 |
exp = plan->tuple_exps[i]; |
| 1222 |
|
| 1223 |
dfield_copy_data(dtuple_get_nth_field(plan->tuple, i), |
| 1224 |
que_node_get_val(exp)); |
| 1225 |
} |
| 1226 |
|
| 1227 |
/* Open pcur to the index */ |
| 1228 |
|
| 1229 |
plan->pcur.open_no_init(index, plan->tuple, plan->mode, BTR_SEARCH_LEAF, |
| 1230 |
has_search_latch, mtr, UT_LOCATION_HERE); |
| 1231 |
} else { |
| 1232 |
/* Open the cursor to the start or the end of the index |
| 1233 |
(false: no init) */ |
| 1234 |
|
| 1235 |
plan->pcur.open_at_side(plan->asc, index, BTR_SEARCH_LEAF, false, 0, mtr); |
| 1236 |
} |
| 1237 |
|
| 1238 |
ut_ad(plan->n_rows_prefetched == 0); |
| 1239 |
ut_ad(plan->n_rows_fetched == 0); |
| 1240 |
ut_ad(plan->cursor_at_end == false); |
| 1241 |
|
| 1242 |
plan->pcur_is_open = true; |
| 1243 |
} |
| 1244 |
|
| 1245 |
/** Restores a stored pcur position to a table index. |
| 1246 |
@return true if the cursor should be moved to the next record after we |
| 1247 |
return from this function (moved to the previous, in the case of a |
| 1248 |
descending cursor) without processing again the current cursor |
| 1249 |
record */ |
| 1250 |
static bool row_sel_restore_pcur_pos(plan_t *plan, /*!< in: table plan */ |
| 1251 |
mtr_t *mtr) /*!< in: mtr */ |
| 1252 |
{ |
| 1253 |
ut_ad(!plan->cursor_at_end); |
| 1254 |
|
| 1255 |
auto relative_position = plan->pcur.get_rel_pos(); |
| 1256 |
|
| 1257 |
auto equal_position = |
| 1258 |
plan->pcur.restore_position(BTR_SEARCH_LEAF, mtr, UT_LOCATION_HERE); |
| 1259 |
|
| 1260 |
/* If the cursor is traveling upwards, and relative_position is |
| 1261 |
|
| 1262 |
(1) BTR_PCUR_BEFORE: this is not allowed, as we did not have a lock |
| 1263 |
yet on the successor of the page infimum; |
| 1264 |
(2) BTR_PCUR_AFTER: btr_pcur_t::restore_position placed the cursor on the |
| 1265 |
first record GREATER than the predecessor of a page supremum; we have |
| 1266 |
not yet processed the cursor record: no need to move the cursor to the |
| 1267 |
next record; |
| 1268 |
(3) BTR_PCUR_ON: btr_pcur_t::restore_position placed the cursor on the |
| 1269 |
last record LESS or EQUAL to the old stored user record; (a) if |
| 1270 |
equal_position is false, this means that the cursor is now on a record |
| 1271 |
less than the old user record, and we must move to the next record; |
| 1272 |
(b) if equal_position is true, then if |
| 1273 |
plan->stored_cursor_rec_processed is true, we must move to the next |
| 1274 |
record, else there is no need to move the cursor. */ |
| 1275 |
|
| 1276 |
if (plan->asc) { |
| 1277 |
if (relative_position == BTR_PCUR_ON) { |
| 1278 |
if (equal_position) { |
| 1279 |
return (plan->stored_cursor_rec_processed); |
| 1280 |
} |
| 1281 |
|
| 1282 |
return true; |
| 1283 |
} |
| 1284 |
|
| 1285 |
ut_ad(relative_position == BTR_PCUR_AFTER || |
| 1286 |
relative_position == BTR_PCUR_AFTER_LAST_IN_TREE); |
| 1287 |
|
| 1288 |
return false; |
| 1289 |
} |
| 1290 |
|
| 1291 |
/* If the cursor is traveling downwards, and relative_position is |
| 1292 |
|
| 1293 |
(1) BTR_PCUR_BEFORE: btr_pcur_t::restore_position placed the cursor on |
| 1294 |
the last record LESS than the successor of a page infimum; we have not |
| 1295 |
processed the cursor record: no need to move the cursor; |
| 1296 |
(2) BTR_PCUR_AFTER: btr_pcur_t::restore_position placed the cursor on the |
| 1297 |
first record GREATER than the predecessor of a page supremum; we have |
| 1298 |
processed the cursor record: we should move the cursor to the previous |
| 1299 |
record; |
| 1300 |
(3) BTR_PCUR_ON: btr_pcur_t::restore_position placed the cursor on the |
| 1301 |
last record LESS or EQUAL to the old stored user record; (a) if |
| 1302 |
equal_position is false, this means that the cursor is now on a record |
| 1303 |
less than the old user record, and we need not move to the previous |
| 1304 |
record; (b) if equal_position is true, then if |
| 1305 |
plan->stored_cursor_rec_processed is true, we must move to the previous |
| 1306 |
record, else there is no need to move the cursor. */ |
| 1307 |
|
| 1308 |
if (relative_position == BTR_PCUR_BEFORE || |
| 1309 |
relative_position == BTR_PCUR_BEFORE_FIRST_IN_TREE) { |
| 1310 |
return false; |
| 1311 |
} |
| 1312 |
|
| 1313 |
if (relative_position == BTR_PCUR_ON) { |
| 1314 |
if (equal_position) { |
| 1315 |
return (plan->stored_cursor_rec_processed); |
| 1316 |
} |
| 1317 |
|
| 1318 |
return false; |
| 1319 |
} |
| 1320 |
|
| 1321 |
ut_ad(relative_position == BTR_PCUR_AFTER || |
| 1322 |
relative_position == BTR_PCUR_AFTER_LAST_IN_TREE); |
| 1323 |
|
| 1324 |
return true; |
| 1325 |
} |
| 1326 |
|
| 1327 |
/** Resets a plan cursor to a closed state. */ |
| 1328 |
static inline void plan_reset_cursor(plan_t *plan) /*!< in: plan */ |
| 1329 |
{ |
| 1330 |
plan->pcur_is_open = false; |
| 1331 |
plan->cursor_at_end = false; |
| 1332 |
plan->n_rows_fetched = 0; |
| 1333 |
plan->n_rows_prefetched = 0; |
| 1334 |
} |
| 1335 |
|
| 1336 |
/** Tries to do a shortcut to fetch a clustered index record with a unique key, |
| 1337 |
using the hash index if possible (not always). |
| 1338 |
@return SEL_FOUND, SEL_EXHAUSTED, SEL_RETRY */ |
| 1339 |
static ulint row_sel_try_search_shortcut( |
| 1340 |
trx_t *trx, /*!< in: trx doing the operation. */ |
| 1341 |
sel_node_t *node, /*!< in: select node for a consistent read */ |
| 1342 |
plan_t *plan, /*!< in: plan for a unique search in clustered |
| 1343 |
index */ |
| 1344 |
bool search_latch_locked, |
| 1345 |
/*!< in: whether the search holds latch on |
| 1346 |
search system. */ |
| 1347 |
mtr_t *mtr) /*!< in: mtr */ |
| 1348 |
{ |
| 1349 |
dict_index_t *index; |
| 1350 |
rec_t *rec; |
| 1351 |
mem_heap_t *heap = nullptr; |
| 1352 |
ulint offsets_[REC_OFFS_NORMAL_SIZE]; |
| 1353 |
ulint *offsets = offsets_; |
| 1354 |
ulint ret; |
| 1355 |
rec_offs_init(offsets_); |
| 1356 |
|
| 1357 |
index = plan->index; |
| 1358 |
|
| 1359 |
ut_ad(node->read_view); |
| 1360 |
ut_ad(plan->unique_search); |
| 1361 |
ut_ad(!plan->must_get_clust); |
| 1362 |
#ifdef UNIV_DEBUG |
| 1363 |
if (search_latch_locked) { |
| 1364 |
ut_ad(rw_lock_own(btr_get_search_latch(index), RW_LOCK_S)); |
| 1365 |
} |
| 1366 |
#endif /* UNIV_DEBUG */ |
| 1367 |
|
| 1368 |
row_sel_open_pcur(plan, search_latch_locked, mtr); |
| 1369 |
|
| 1370 |
rec = plan->pcur.get_rec(); |
| 1371 |
|
| 1372 |
if (!page_rec_is_user_rec(rec)) { |
| 1373 |
return (SEL_RETRY); |
| 1374 |
} |
| 1375 |
|
| 1376 |
ut_ad(plan->mode == PAGE_CUR_GE); |
| 1377 |
|
| 1378 |
/* As the cursor is now placed on a user record after a search with |
| 1379 |
the mode PAGE_CUR_GE, the up_match field in the cursor tells how many |
| 1380 |
fields in the user record matched to the search tuple */ |
| 1381 |
|
| 1382 |
if (plan->pcur.get_up_match() < plan->n_exact_match) { |
| 1383 |
return (SEL_EXHAUSTED); |
| 1384 |
} |
| 1385 |
|
| 1386 |
/* This is a non-locking consistent read: if necessary, fetch |
| 1387 |
a previous version of the record */ |
| 1388 |
|
| 1389 |
offsets = rec_get_offsets(rec, index, offsets, ULINT_UNDEFINED, |
| 1390 |
UT_LOCATION_HERE, &heap); |
| 1391 |
|
| 1392 |
if (index->is_clustered()) { |
| 1393 |
if (!lock_clust_rec_cons_read_sees(rec, index, offsets, node->read_view)) { |
| 1394 |
ret = SEL_RETRY; |
| 1395 |
goto func_exit; |
| 1396 |
} |
| 1397 |
} else if (!srv_read_only_mode && |
| 1398 |
!lock_sec_rec_cons_read_sees(rec, index, node->read_view)) { |
| 1399 |
ret = SEL_RETRY; |
| 1400 |
goto func_exit; |
| 1401 |
} |
| 1402 |
|
| 1403 |
/* Test the deleted flag. */ |
| 1404 |
|
| 1405 |
if (rec_get_deleted_flag(rec, dict_table_is_comp(plan->table))) { |
| 1406 |
ret = SEL_EXHAUSTED; |
| 1407 |
goto func_exit; |
| 1408 |
} |
| 1409 |
|
| 1410 |
/* Fetch the columns needed in test conditions. The index |
| 1411 |
record is protected by a page latch that was acquired when |
| 1412 |
plan->pcur was positioned. The latch will not be released |
| 1413 |
until mtr_commit(mtr). */ |
| 1414 |
|
| 1415 |
row_sel_fetch_columns(trx, index, rec, offsets, |
| 1416 |
UT_LIST_GET_FIRST(plan->columns)); |
| 1417 |
|
| 1418 |
/* Test the rest of search conditions */ |
| 1419 |
|
| 1420 |
if (!row_sel_test_other_conds(plan)) { |
| 1421 |
ret = SEL_EXHAUSTED; |
| 1422 |
goto func_exit; |
| 1423 |
} |
| 1424 |
|
| 1425 |
ut_ad(plan->pcur.m_latch_mode == BTR_SEARCH_LEAF); |
| 1426 |
|
| 1427 |
plan->n_rows_fetched++; |
| 1428 |
ret = SEL_FOUND; |
| 1429 |
func_exit: |
| 1430 |
if (UNIV_LIKELY_NULL(heap)) { |
| 1431 |
mem_heap_free(heap); |
| 1432 |
} |
| 1433 |
return (ret); |
| 1434 |
} |
| 1435 |
|
| 1436 |
/** Performs a select step. |
| 1437 |
@return DB_SUCCESS or error code */ |
| 1438 |
[[nodiscard]] static dberr_t row_sel(sel_node_t *node, /*!< in: select node */ |
| 1439 |
que_thr_t *thr) /*!< in: query thread */ |
| 1440 |
{ |
| 1441 |
dict_index_t *index; |
| 1442 |
plan_t *plan; |
| 1443 |
mtr_t mtr; |
| 1444 |
bool moved; |
| 1445 |
rec_t *rec; |
| 1446 |
rec_t *old_vers; |
| 1447 |
rec_t *clust_rec; |
| 1448 |
bool search_latch_locked; |
| 1449 |
bool consistent_read; |
| 1450 |
|
| 1451 |
/* The following flag becomes true when we are doing a |
| 1452 |
consistent read from a non-clustered index and we must look |
| 1453 |
at the clustered index to find out the previous delete mark |
| 1454 |
state of the non-clustered record: */ |
| 1455 |
|
| 1456 |
bool cons_read_requires_clust_rec = false; |
| 1457 |
ulint cost_counter = 0; |
| 1458 |
bool cursor_just_opened; |
| 1459 |
bool must_go_to_next; |
| 1460 |
bool mtr_has_extra_clust_latch = false; |
| 1461 |
/* true if the search was made using |
| 1462 |
a non-clustered index, and we had to |
| 1463 |
access the clustered record: now &mtr |
| 1464 |
contains a clustered index latch, and |
| 1465 |
&mtr must be committed before we move |
| 1466 |
to the next non-clustered record */ |
| 1467 |
ulint found_flag; |
| 1468 |
dberr_t err; |
| 1469 |
mem_heap_t *heap = nullptr; |
| 1470 |
ulint offsets_[REC_OFFS_NORMAL_SIZE]; |
| 1471 |
ulint *offsets = offsets_; |
| 1472 |
rec_offs_init(offsets_); |
| 1473 |
|
| 1474 |
ut_ad(thr->run_node == node); |
| 1475 |
|
| 1476 |
search_latch_locked = false; |
| 1477 |
|
| 1478 |
if (node->read_view) { |
| 1479 |
/* In consistent reads, we try to do with the hash index and |
| 1480 |
not to use the buffer page get. This is to reduce memory bus |
| 1481 |
load resulting from semaphore operations. The search latch |
| 1482 |
will be s-locked when we access an index with a unique search |
| 1483 |
condition, but not locked when we access an index with a |
| 1484 |
less selective search condition. */ |
| 1485 |
|
| 1486 |
consistent_read = true; |
| 1487 |
} else { |
| 1488 |
consistent_read = false; |
| 1489 |
} |
| 1490 |
|
| 1491 |
table_loop: |
| 1492 |
/* TABLE LOOP |
| 1493 |
---------- |
| 1494 |
This is the outer major loop in calculating a join. We come here when |
| 1495 |
node->fetch_table changes, and after adding a row to aggregate totals |
| 1496 |
and, of course, when this function is called. */ |
| 1497 |
|
| 1498 |
ut_ad(mtr_has_extra_clust_latch == false); |
| 1499 |
|
| 1500 |
plan = sel_node_get_nth_plan(node, node->fetch_table); |
| 1501 |
index = plan->index; |
| 1502 |
|
| 1503 |
if (plan->n_rows_prefetched > 0) { |
| 1504 |
sel_dequeue_prefetched_row(plan); |
| 1505 |
|
| 1506 |
goto next_table_no_mtr; |
| 1507 |
} |
| 1508 |
|
| 1509 |
if (plan->cursor_at_end) { |
| 1510 |
/* The cursor has already reached the result set end: no more |
| 1511 |
rows to process for this table cursor, as also the prefetch |
| 1512 |
stack was empty */ |
| 1513 |
|
| 1514 |
ut_ad(plan->pcur_is_open); |
| 1515 |
|
| 1516 |
goto table_exhausted_no_mtr; |
| 1517 |
} |
| 1518 |
|
| 1519 |
/* Open a cursor to index, or restore an open cursor position */ |
| 1520 |
|
| 1521 |
mtr_start(&mtr); |
| 1522 |
|
| 1523 |
if (consistent_read && plan->unique_search && !plan->pcur_is_open && |
| 1524 |
!plan->must_get_clust && !plan->table->big_rows) { |
| 1525 |
if (!search_latch_locked) { |
| 1526 |
rw_lock_s_lock(btr_get_search_latch(index), UT_LOCATION_HERE); |
| 1527 |
|
| 1528 |
search_latch_locked = true; |
| 1529 |
} else if (rw_lock_get_writer(btr_get_search_latch(index)) == |
| 1530 |
RW_LOCK_X_WAIT) { |
| 1531 |
/* There is an x-latch request waiting: release the |
| 1532 |
s-latch for a moment; as an s-latch here is often |
| 1533 |
kept for some 10 searches before being released, |
| 1534 |
a waiting x-latch request would block other threads |
| 1535 |
from acquiring an s-latch for a long time, lowering |
| 1536 |
performance significantly in multiprocessors. */ |
| 1537 |
|
| 1538 |
rw_lock_s_unlock(btr_get_search_latch(index)); |
| 1539 |
rw_lock_s_lock(btr_get_search_latch(index), UT_LOCATION_HERE); |
| 1540 |
} |
| 1541 |
|
| 1542 |
found_flag = row_sel_try_search_shortcut(thr_get_trx(thr), node, plan, |
| 1543 |
search_latch_locked, &mtr); |
| 1544 |
|
| 1545 |
if (found_flag == SEL_FOUND) { |
| 1546 |
goto next_table; |
| 1547 |
|
| 1548 |
} else if (found_flag == SEL_EXHAUSTED) { |
| 1549 |
goto table_exhausted; |
| 1550 |
} |
| 1551 |
|
| 1552 |
ut_ad(found_flag == SEL_RETRY); |
| 1553 |
|
| 1554 |
plan_reset_cursor(plan); |
| 1555 |
|
| 1556 |
mtr_commit(&mtr); |
| 1557 |
mtr_start(&mtr); |
| 1558 |
} |
| 1559 |
|
| 1560 |
if (search_latch_locked) { |
| 1561 |
rw_lock_s_unlock(btr_get_search_latch(index)); |
| 1562 |
|
| 1563 |
search_latch_locked = false; |
| 1564 |
} |
| 1565 |
|
| 1566 |
if (!plan->pcur_is_open) { |
| 1567 |
/* Evaluate the expressions to build the search tuple and |
| 1568 |
open the cursor */ |
| 1569 |
|
| 1570 |
row_sel_open_pcur(plan, search_latch_locked, &mtr); |
| 1571 |
|
| 1572 |
cursor_just_opened = true; |
| 1573 |
|
| 1574 |
/* A new search was made: increment the cost counter */ |
| 1575 |
cost_counter++; |
| 1576 |
} else { |
| 1577 |
/* Restore pcur position to the index */ |
| 1578 |
|
| 1579 |
must_go_to_next = row_sel_restore_pcur_pos(plan, &mtr); |
| 1580 |
|
| 1581 |
cursor_just_opened = false; |
| 1582 |
|
| 1583 |
if (must_go_to_next) { |
| 1584 |
/* We have already processed the cursor record: move |
| 1585 |
to the next */ |
| 1586 |
|
| 1587 |
goto next_rec; |
| 1588 |
} |
| 1589 |
} |
| 1590 |
|
| 1591 |
rec_loop: |
| 1592 |
/* RECORD LOOP |
| 1593 |
----------- |
| 1594 |
In this loop we use pcur and try to fetch a qualifying row, and |
| 1595 |
also fill the prefetch buffer for this table if n_rows_fetched has |
| 1596 |
exceeded a threshold. While we are inside this loop, the following |
| 1597 |
holds: |
| 1598 |
(1) &mtr is started, |
| 1599 |
(2) pcur is positioned and open. |
| 1600 |
|
| 1601 |
NOTE that if cursor_just_opened is true here, it means that we came |
| 1602 |
to this point right after row_sel_open_pcur. */ |
| 1603 |
|
| 1604 |
ut_ad(mtr_has_extra_clust_latch == false); |
| 1605 |
|
| 1606 |
rec = plan->pcur.get_rec(); |
| 1607 |
|
| 1608 |
/* PHASE 1: Set a lock if specified */ |
| 1609 |
|
| 1610 |
if (!node->asc && cursor_just_opened && !page_rec_is_supremum(rec)) { |
| 1611 |
/* Do not support "descending search" for Spatial index */ |
| 1612 |
ut_ad(!dict_index_is_spatial(index)); |
| 1613 |
|
| 1614 |
/* When we open a cursor for a descending search, we must set |
| 1615 |
a next-key lock on the successor record: otherwise it would |
| 1616 |
be possible to insert new records next to the cursor position, |
| 1617 |
and it might be that these new records should appear in the |
| 1618 |
search result set, resulting in the phantom problem. */ |
| 1619 |
|
| 1620 |
if (!consistent_read) { |
| 1621 |
rec_t *next_rec = page_rec_get_next(rec); |
| 1622 |
ulint lock_type; |
| 1623 |
trx_t *trx; |
| 1624 |
|
| 1625 |
trx = thr_get_trx(thr); |
| 1626 |
|
| 1627 |
offsets = rec_get_offsets(next_rec, index, offsets, ULINT_UNDEFINED, |
| 1628 |
UT_LOCATION_HERE, &heap); |
| 1629 |
|
| 1630 |
if (trx->skip_gap_locks()) { |
| 1631 |
if (page_rec_is_supremum(next_rec)) { |
| 1632 |
goto skip_lock; |
| 1633 |
} |
| 1634 |
|
| 1635 |
lock_type = LOCK_REC_NOT_GAP; |
| 1636 |
} else { |
| 1637 |
lock_type = LOCK_ORDINARY; |
| 1638 |
} |
| 1639 |
|
| 1640 |
err = sel_set_rec_lock(&plan->pcur, next_rec, index, offsets, |
| 1641 |
SELECT_ORDINARY, node->row_lock_mode, lock_type, |
| 1642 |
thr, &mtr); |
| 1643 |
|
| 1644 |
switch (err) { |
| 1645 |
case DB_SUCCESS_LOCKED_REC: |
| 1646 |
err = DB_SUCCESS; |
| 1647 |
case DB_SUCCESS: |
| 1648 |
break; |
| 1649 |
default: |
| 1650 |
/* Note that in this case we will store in pcur |
| 1651 |
the PREDECESSOR of the record we are waiting |
| 1652 |
the lock for */ |
| 1653 |
goto lock_wait_or_error; |
| 1654 |
} |
| 1655 |
} |
| 1656 |
} |
| 1657 |
|
| 1658 |
skip_lock: |
| 1659 |
if (page_rec_is_infimum(rec)) { |
| 1660 |
/* The infimum record on a page cannot be in the result set, |
| 1661 |
and neither can a record lock be placed on it: we skip such |
| 1662 |
a record. We also increment the cost counter as we may have |
| 1663 |
processed yet another page of index. */ |
| 1664 |
|
| 1665 |
cost_counter++; |
| 1666 |
|
| 1667 |
goto next_rec; |
| 1668 |
} |
| 1669 |
|
| 1670 |
if (!consistent_read) { |
| 1671 |
/* Try to place a lock on the index record */ |
| 1672 |
|
| 1673 |
ulint lock_type; |
| 1674 |
trx_t *trx; |
| 1675 |
|
| 1676 |
offsets = rec_get_offsets(rec, index, offsets, ULINT_UNDEFINED, |
| 1677 |
UT_LOCATION_HERE, &heap); |
| 1678 |
|
| 1679 |
trx = thr_get_trx(thr); |
| 1680 |
|
| 1681 |
if (trx->skip_gap_locks() || dict_index_is_spatial(index)) { |
| 1682 |
if (page_rec_is_supremum(rec)) { |
| 1683 |
goto next_rec; |
| 1684 |
} |
| 1685 |
|
| 1686 |
lock_type = LOCK_REC_NOT_GAP; |
| 1687 |
} else { |
| 1688 |
lock_type = LOCK_ORDINARY; |
| 1689 |
} |
| 1690 |
|
| 1691 |
err = sel_set_rec_lock(&plan->pcur, rec, index, offsets, SELECT_ORDINARY, |
| 1692 |
node->row_lock_mode, lock_type, thr, &mtr); |
| 1693 |
|
| 1694 |
switch (err) { |
| 1695 |
case DB_SUCCESS_LOCKED_REC: |
| 1696 |
err = DB_SUCCESS; |
| 1697 |
case DB_SUCCESS: |
| 1698 |
break; |
| 1699 |
default: |
| 1700 |
goto lock_wait_or_error; |
| 1701 |
} |
| 1702 |
} |
| 1703 |
|
| 1704 |
if (page_rec_is_supremum(rec)) { |
| 1705 |
/* A page supremum record cannot be in the result set: skip |
| 1706 |
it now when we have placed a possible lock on it */ |
| 1707 |
|
| 1708 |
goto next_rec; |
| 1709 |
} |
| 1710 |
|
| 1711 |
ut_ad(page_rec_is_user_rec(rec)); |
| 1712 |
|
| 1713 |
if (cost_counter > SEL_COST_LIMIT) { |
| 1714 |
/* Now that we have placed the necessary locks, we can stop |
| 1715 |
for a while and store the cursor position; NOTE that if we |
| 1716 |
would store the cursor position BEFORE placing a record lock, |
| 1717 |
it might happen that the cursor would jump over some records |
| 1718 |
that another transaction could meanwhile insert adjacent to |
| 1719 |
the cursor: this would result in the phantom problem. */ |
| 1720 |
|
| 1721 |
goto stop_for_a_while; |
| 1722 |
} |
| 1723 |
|
| 1724 |
/* PHASE 2: Check a mixed index mix id if needed */ |
| 1725 |
|
| 1726 |
if (plan->unique_search && cursor_just_opened) { |
| 1727 |
ut_ad(plan->mode == PAGE_CUR_GE); |
| 1728 |
|
| 1729 |
/* As the cursor is now placed on a user record after a search |
| 1730 |
with the mode PAGE_CUR_GE, the up_match field in the cursor |
| 1731 |
tells how many fields in the user record matched to the search |
| 1732 |
tuple */ |
| 1733 |
|
| 1734 |
if (plan->pcur.get_up_match() < plan->n_exact_match) { |
| 1735 |
goto table_exhausted; |
| 1736 |
} |
| 1737 |
|
| 1738 |
/* Ok, no need to test end_conds or mix id */ |
| 1739 |
} |
| 1740 |
|
| 1741 |
/* We are ready to look at a possible new index entry in the result |
| 1742 |
set: the cursor is now placed on a user record */ |
| 1743 |
|
| 1744 |
/* PHASE 3: Get previous version in a consistent read */ |
| 1745 |
|
| 1746 |
cons_read_requires_clust_rec = false; |
| 1747 |
offsets = rec_get_offsets(rec, index, offsets, ULINT_UNDEFINED, |
| 1748 |
UT_LOCATION_HERE, &heap); |
| 1749 |
|
| 1750 |
if (consistent_read) { |
| 1751 |
/* This is a non-locking consistent read: if necessary, fetch |
| 1752 |
a previous version of the record */ |
| 1753 |
|
| 1754 |
if (index->is_clustered()) { |
| 1755 |
if (!lock_clust_rec_cons_read_sees(rec, index, offsets, |
| 1756 |
node->read_view)) { |
| 1757 |
err = row_sel_build_prev_vers(node->read_view, index, rec, &offsets, |
| 1758 |
&heap, &plan->old_vers_heap, &old_vers, |
| 1759 |
&mtr); |
| 1760 |
|
| 1761 |
if (err != DB_SUCCESS) { |
| 1762 |
goto lock_wait_or_error; |
| 1763 |
} |
| 1764 |
|
| 1765 |
if (old_vers == nullptr) { |
| 1766 |
/* The record does not exist |
| 1767 |
in our read view. Skip it, but |
| 1768 |
first attempt to determine |
| 1769 |
whether the index segment we |
| 1770 |
are searching through has been |
| 1771 |
exhausted. */ |
| 1772 |
|
| 1773 |
offsets = rec_get_offsets(rec, index, offsets, ULINT_UNDEFINED, |
| 1774 |
UT_LOCATION_HERE, &heap); |
| 1775 |
|
| 1776 |
/* Fetch the columns needed in |
| 1777 |
test conditions. The clustered |
| 1778 |
index record is protected by a |
| 1779 |
page latch that was acquired |
| 1780 |
by row_sel_open_pcur() or |
| 1781 |
row_sel_restore_pcur_pos(). |
| 1782 |
The latch will not be released |
| 1783 |
until mtr_commit(mtr). */ |
| 1784 |
|
| 1785 |
const bool allow_null_lob = true; |
| 1786 |
row_sel_fetch_columns(thr_get_trx(thr), index, rec, offsets, |
| 1787 |
UT_LIST_GET_FIRST(plan->columns), |
| 1788 |
allow_null_lob); |
| 1789 |
|
| 1790 |
if (!row_sel_test_end_conds(plan)) { |
| 1791 |
goto table_exhausted; |
| 1792 |
} |
| 1793 |
|
| 1794 |
goto next_rec; |
| 1795 |
} |
| 1796 |
|
| 1797 |
rec = old_vers; |
| 1798 |
} |
| 1799 |
} else if (!srv_read_only_mode && |
| 1800 |
!lock_sec_rec_cons_read_sees(rec, index, node->read_view)) { |
| 1801 |
cons_read_requires_clust_rec = true; |
| 1802 |
} |
| 1803 |
} |
| 1804 |
|
| 1805 |
/* PHASE 4: Test search end conditions and deleted flag */ |
| 1806 |
|
| 1807 |
/* Fetch the columns needed in test conditions. The record is |
| 1808 |
protected by a page latch that was acquired by |
| 1809 |
row_sel_open_pcur() or row_sel_restore_pcur_pos(). The latch |
| 1810 |
will not be released until mtr_commit(mtr). */ |
| 1811 |
|
| 1812 |
row_sel_fetch_columns(thr_get_trx(thr), index, rec, offsets, |
| 1813 |
UT_LIST_GET_FIRST(plan->columns)); |
| 1814 |
|
| 1815 |
/* Test the selection end conditions: these can only contain columns |
| 1816 |
which already are found in the index, even though the index might be |
| 1817 |
non-clustered */ |
| 1818 |
|
| 1819 |
if (plan->unique_search && cursor_just_opened) { |
| 1820 |
/* No test necessary: the test was already made above */ |
| 1821 |
|
| 1822 |
} else if (!row_sel_test_end_conds(plan)) { |
| 1823 |
goto table_exhausted; |
| 1824 |
} |
| 1825 |
|
| 1826 |
if (rec_get_deleted_flag(rec, dict_table_is_comp(plan->table)) && |
| 1827 |
!cons_read_requires_clust_rec) { |
| 1828 |
/* The record is delete marked: we can skip it if this is |
| 1829 |
not a consistent read which might see an earlier version |
| 1830 |
of a non-clustered index record */ |
| 1831 |
|
| 1832 |
if (plan->unique_search) { |
| 1833 |
goto table_exhausted; |
| 1834 |
} |
| 1835 |
|
| 1836 |
goto next_rec; |
| 1837 |
} |
| 1838 |
|
| 1839 |
/* PHASE 5: Get the clustered index record, if needed and if we did |
| 1840 |
not do the search using the clustered index */ |
| 1841 |
|
| 1842 |
if (plan->must_get_clust || cons_read_requires_clust_rec) { |
| 1843 |
/* It was a non-clustered index and we must fetch also the |
| 1844 |
clustered index record */ |
| 1845 |
|
| 1846 |
err = row_sel_get_clust_rec(node, plan, rec, thr, &clust_rec, &mtr); |
| 1847 |
mtr_has_extra_clust_latch = true; |
| 1848 |
|
| 1849 |
if (err != DB_SUCCESS) { |
| 1850 |
goto lock_wait_or_error; |
| 1851 |
} |
| 1852 |
|
| 1853 |
/* Retrieving the clustered record required a search: |
| 1854 |
increment the cost counter */ |
| 1855 |
|
| 1856 |
cost_counter++; |
| 1857 |
|
| 1858 |
if (clust_rec == nullptr) { |
| 1859 |
/* The record did not exist in the read view */ |
| 1860 |
ut_ad(consistent_read); |
| 1861 |
|
| 1862 |
goto next_rec; |
| 1863 |
} |
| 1864 |
|
| 1865 |
if (rec_get_deleted_flag(clust_rec, dict_table_is_comp(plan->table))) { |
| 1866 |
/* The record is delete marked: we can skip it */ |
| 1867 |
|
| 1868 |
goto next_rec; |
| 1869 |
} |
| 1870 |
|
| 1871 |
if (node->can_get_updated) { |
| 1872 |
plan->clust_pcur.store_position(&mtr); |
| 1873 |
} |
| 1874 |
} |
| 1875 |
|
| 1876 |
/* PHASE 6: Test the rest of search conditions */ |
| 1877 |
|
| 1878 |
if (!row_sel_test_other_conds(plan)) { |
| 1879 |
if (plan->unique_search) { |
| 1880 |
goto table_exhausted; |
| 1881 |
} |
| 1882 |
|
| 1883 |
goto next_rec; |
| 1884 |
} |
| 1885 |
|
| 1886 |
/* PHASE 7: We found a new qualifying row for the current table; push |
| 1887 |
the row if prefetch is on, or move to the next table in the join */ |
| 1888 |
|
| 1889 |
plan->n_rows_fetched++; |
| 1890 |
|
| 1891 |
ut_ad(plan->pcur.m_latch_mode == BTR_SEARCH_LEAF); |
| 1892 |
|
| 1893 |
if ((plan->n_rows_fetched <= SEL_PREFETCH_LIMIT) || plan->unique_search || |
| 1894 |
plan->no_prefetch || plan->table->big_rows) { |
| 1895 |
/* No prefetch in operation: go to the next table */ |
| 1896 |
|
| 1897 |
goto next_table; |
| 1898 |
} |
| 1899 |
|
| 1900 |
sel_enqueue_prefetched_row(plan); |
| 1901 |
|
| 1902 |
if (plan->n_rows_prefetched == SEL_MAX_N_PREFETCH) { |
| 1903 |
/* The prefetch buffer is now full */ |
| 1904 |
|
| 1905 |
sel_dequeue_prefetched_row(plan); |
| 1906 |
|
| 1907 |
goto next_table; |
| 1908 |
} |
| 1909 |
|
| 1910 |
next_rec: |
| 1911 |
ut_ad(!search_latch_locked); |
| 1912 |
|
| 1913 |
if (mtr_has_extra_clust_latch) { |
| 1914 |
/* We must commit &mtr if we are moving to the next |
| 1915 |
non-clustered index record, because we could break the |
| 1916 |
latching order if we would access a different clustered |
| 1917 |
index page right away without releasing the previous. */ |
| 1918 |
|
| 1919 |
goto commit_mtr_for_a_while; |
| 1920 |
} |
| 1921 |
|
| 1922 |
if (node->asc) { |
| 1923 |
moved = plan->pcur.move_to_next(&mtr); |
| 1924 |
} else { |
| 1925 |
moved = plan->pcur.move_to_prev(&mtr); |
| 1926 |
} |
| 1927 |
|
| 1928 |
if (!moved) { |
| 1929 |
goto table_exhausted; |
| 1930 |
} |
| 1931 |
|
| 1932 |
cursor_just_opened = false; |
| 1933 |
|
| 1934 |
/* END OF RECORD LOOP |
| 1935 |
------------------ */ |
| 1936 |
goto rec_loop; |
| 1937 |
|
| 1938 |
next_table: |
| 1939 |
/* We found a record which satisfies the conditions: we can move to |
| 1940 |
the next table or return a row in the result set */ |
| 1941 |
|
| 1942 |
ut_ad(plan->pcur.is_on_user_rec()); |
| 1943 |
|
| 1944 |
if (plan->unique_search && !node->can_get_updated) { |
| 1945 |
plan->cursor_at_end = true; |
| 1946 |
} else { |
| 1947 |
ut_ad(!search_latch_locked); |
| 1948 |
|
| 1949 |
plan->stored_cursor_rec_processed = true; |
| 1950 |
|
| 1951 |
plan->pcur.store_position(&mtr); |
| 1952 |
} |
| 1953 |
|
| 1954 |
mtr_commit(&mtr); |
| 1955 |
|
| 1956 |
mtr_has_extra_clust_latch = false; |
| 1957 |
|
| 1958 |
next_table_no_mtr: |
| 1959 |
/* If we use 'goto' to this label, it means that the row was popped |
| 1960 |
from the prefetched rows stack, and &mtr is already committed */ |
| 1961 |
|
| 1962 |
if (node->fetch_table + 1 == node->n_tables) { |
| 1963 |
sel_eval_select_list(node); |
| 1964 |
|
| 1965 |
if (node->is_aggregate) { |
| 1966 |
goto table_loop; |
| 1967 |
} |
| 1968 |
|
| 1969 |
sel_assign_into_var_values(node->into_list, node); |
| 1970 |
|
| 1971 |
thr->run_node = que_node_get_parent(node); |
| 1972 |
|
| 1973 |
err = DB_SUCCESS; |
| 1974 |
goto func_exit; |
| 1975 |
} |
| 1976 |
|
| 1977 |
node->fetch_table++; |
| 1978 |
|
| 1979 |
/* When we move to the next table, we first reset the plan cursor: |
| 1980 |
we do not care about resetting it when we backtrack from a table */ |
| 1981 |
|
| 1982 |
plan_reset_cursor(sel_node_get_nth_plan(node, node->fetch_table)); |
| 1983 |
|
| 1984 |
goto table_loop; |
| 1985 |
|
| 1986 |
table_exhausted: |
| 1987 |
/* The table cursor pcur reached the result set end: backtrack to the |
| 1988 |
previous table in the join if we do not have cached prefetched rows */ |
| 1989 |
|
| 1990 |
plan->cursor_at_end = true; |
| 1991 |
|
| 1992 |
mtr_commit(&mtr); |
| 1993 |
|
| 1994 |
mtr_has_extra_clust_latch = false; |
| 1995 |
|
| 1996 |
if (plan->n_rows_prefetched > 0) { |
| 1997 |
/* The table became exhausted during a prefetch */ |
| 1998 |
|
| 1999 |
sel_dequeue_prefetched_row(plan); |
| 2000 |
|
| 2001 |
goto next_table_no_mtr; |
| 2002 |
} |
| 2003 |
|
| 2004 |
table_exhausted_no_mtr: |
| 2005 |
if (node->fetch_table == 0) { |
| 2006 |
err = DB_SUCCESS; |
| 2007 |
|
| 2008 |
if (node->is_aggregate && !node->aggregate_already_fetched) { |
| 2009 |
node->aggregate_already_fetched = true; |
| 2010 |
|
| 2011 |
sel_assign_into_var_values(node->into_list, node); |
| 2012 |
|
| 2013 |
thr->run_node = que_node_get_parent(node); |
| 2014 |
} else { |
| 2015 |
node->state = SEL_NODE_NO_MORE_ROWS; |
| 2016 |
|
| 2017 |
thr->run_node = que_node_get_parent(node); |
| 2018 |
} |
| 2019 |
|
| 2020 |
goto func_exit; |
| 2021 |
} |
| 2022 |
|
| 2023 |
node->fetch_table--; |
| 2024 |
|
| 2025 |
goto table_loop; |
| 2026 |
|
| 2027 |
stop_for_a_while: |
| 2028 |
/* Return control for a while to que_run_threads, so that runaway |
| 2029 |
queries can be canceled. NOTE that when we come here, we must, in a |
| 2030 |
locking read, have placed the necessary (possibly waiting request) |
| 2031 |
record lock on the cursor record or its successor: when we reposition |
| 2032 |
the cursor, this record lock guarantees that nobody can meanwhile have |
| 2033 |
inserted new records which should have appeared in the result set, |
| 2034 |
which would result in the phantom problem. */ |
| 2035 |
|
| 2036 |
ut_ad(!search_latch_locked); |
| 2037 |
|
| 2038 |
plan->stored_cursor_rec_processed = false; |
| 2039 |
plan->pcur.store_position(&mtr); |
| 2040 |
|
| 2041 |
mtr_commit(&mtr); |
| 2042 |
|
| 2043 |
#ifdef UNIV_DEBUG |
| 2044 |
{ |
| 2045 |
btrsea_sync_check check(true); |
| 2046 |
|
| 2047 |
ut_ad(!sync_check_iterate(check)); |
| 2048 |
} |
| 2049 |
#endif /* UNIV_DEBUG */ |
| 2050 |
|
| 2051 |
err = DB_SUCCESS; |
| 2052 |
goto func_exit; |
| 2053 |
|
| 2054 |
commit_mtr_for_a_while: |
| 2055 |
/* Stores the cursor position and commits &mtr; this is used if |
| 2056 |
&mtr may contain latches which would break the latching order if |
| 2057 |
&mtr would not be committed and the latches released. */ |
| 2058 |
|
| 2059 |
plan->stored_cursor_rec_processed = true; |
| 2060 |
|
| 2061 |
ut_ad(!search_latch_locked); |
| 2062 |
plan->pcur.store_position(&mtr); |
| 2063 |
|
| 2064 |
mtr_commit(&mtr); |
| 2065 |
|
| 2066 |
mtr_has_extra_clust_latch = false; |
| 2067 |
|
| 2068 |
#ifdef UNIV_DEBUG |
| 2069 |
{ |
| 2070 |
dict_sync_check check(true); |
| 2071 |
|
| 2072 |
ut_ad(!sync_check_iterate(check)); |
| 2073 |
} |
| 2074 |
#endif /* UNIV_DEBUG */ |
| 2075 |
|
| 2076 |
goto table_loop; |
| 2077 |
|
| 2078 |
lock_wait_or_error: |
| 2079 |
/* See the note at stop_for_a_while: the same holds for this case */ |
| 2080 |
|
| 2081 |
ut_ad(!plan->pcur.is_before_first_on_page() || !node->asc); |
| 2082 |
ut_ad(!search_latch_locked); |
| 2083 |
|
| 2084 |
plan->stored_cursor_rec_processed = false; |
| 2085 |
plan->pcur.store_position(&mtr); |
| 2086 |
|
| 2087 |
mtr_commit(&mtr); |
| 2088 |
|
| 2089 |
#ifdef UNIV_DEBUG |
| 2090 |
{ |
| 2091 |
dict_sync_check check(true); |
| 2092 |
|
| 2093 |
ut_ad(!sync_check_iterate(check)); |
| 2094 |
} |
| 2095 |
#endif /* UNIV_DEBUG */ |
| 2096 |
|
| 2097 |
func_exit: |
| 2098 |
if (search_latch_locked) { |
| 2099 |
rw_lock_s_unlock(btr_get_search_latch(index)); |
| 2100 |
} |
| 2101 |
|
| 2102 |
if (heap != nullptr) { |
| 2103 |
mem_heap_free(heap); |
| 2104 |
} |
| 2105 |
return (err); |
| 2106 |
} |
| 2107 |
|
| 2108 |
/** Performs a select step. This is a high-level function used in SQL execution |
| 2109 |
graphs. |
| 2110 |
@return query thread to run next or NULL */ |
| 2111 |
que_thr_t *row_sel_step(que_thr_t *thr) /*!< in: query thread */ |
| 2112 |
{ |
| 2113 |
sel_node_t *node; |
| 2114 |
|
| 2115 |
ut_ad(thr); |
| 2116 |
|
| 2117 |
node = static_cast<sel_node_t *>(thr->run_node); |
| 2118 |
|
| 2119 |
ut_ad(que_node_get_type(node) == QUE_NODE_SELECT); |
| 2120 |
|
| 2121 |
/* If this is a new time this node is executed (or when execution |
| 2122 |
resumes after wait for a table intention lock), set intention locks |
| 2123 |
on the tables, or assign a read view */ |
| 2124 |
|
| 2125 |
if (node->into_list && (thr->prev_node == que_node_get_parent(node))) { |
| 2126 |
node->state = SEL_NODE_OPEN; |
| 2127 |
} |
| 2128 |
|
| 2129 |
if (node->state == SEL_NODE_OPEN) { |
| 2130 |
/* It may be that the current session has not yet started |
| 2131 |
its transaction, or it has been committed: */ |
| 2132 |
|
| 2133 |
trx_start_if_not_started_xa(thr_get_trx(thr), false, UT_LOCATION_HERE); |
| 2134 |
|
| 2135 |
plan_reset_cursor(sel_node_get_nth_plan(node, 0)); |
| 2136 |
|
| 2137 |
if (node->consistent_read) { |
| 2138 |
/* Assign a read view for the query */ |
| 2139 |
trx_assign_read_view(thr_get_trx(thr)); |
| 2140 |
|
| 2141 |
if (thr_get_trx(thr)->read_view != nullptr) { |
| 2142 |
node->read_view = thr_get_trx(thr)->read_view; |
| 2143 |
} else { |
| 2144 |
node->read_view = nullptr; |
| 2145 |
} |
| 2146 |
|
| 2147 |
} else { |
| 2148 |
sym_node_t *table_node; |
| 2149 |
lock_mode i_lock_mode; |
| 2150 |
|
| 2151 |
if (node->set_x_locks) { |
| 2152 |
i_lock_mode = LOCK_IX; |
| 2153 |
} else { |
| 2154 |
i_lock_mode = LOCK_IS; |
| 2155 |
} |
| 2156 |
|
| 2157 |
for (table_node = node->table_list; table_node != nullptr; |
| 2158 |
table_node = |
| 2159 |
static_cast<sym_node_t *>(que_node_get_next(table_node))) { |
| 2160 |
dberr_t err = lock_table(0, table_node->table, i_lock_mode, thr); |
| 2161 |
|
| 2162 |
if (err != DB_SUCCESS) { |
| 2163 |
trx_t *trx; |
| 2164 |
|
| 2165 |
trx = thr_get_trx(thr); |
| 2166 |
trx->error_state = err; |
| 2167 |
|
| 2168 |
return (nullptr); |
| 2169 |
} |
| 2170 |
} |
| 2171 |
} |
| 2172 |
|
| 2173 |
/* If this is an explicit cursor, copy stored procedure |
| 2174 |
variable values, so that the values cannot change between |
| 2175 |
fetches (currently, we copy them also for non-explicit |
| 2176 |
cursors) */ |
| 2177 |
|
| 2178 |
if (node->explicit_cursor && UT_LIST_GET_FIRST(node->copy_variables)) { |
| 2179 |
row_sel_copy_input_variable_vals(node); |
| 2180 |
} |
| 2181 |
|
| 2182 |
node->state = SEL_NODE_FETCH; |
| 2183 |
node->fetch_table = 0; |
| 2184 |
|
| 2185 |
if (node->is_aggregate) { |
| 2186 |
/* Reset the aggregate total values */ |
| 2187 |
sel_reset_aggregate_vals(node); |
| 2188 |
} |
| 2189 |
} |
| 2190 |
|
| 2191 |
dberr_t err = row_sel(node, thr); |
| 2192 |
|
| 2193 |
/* NOTE! if queries are parallelized, the following assignment may |
| 2194 |
have problems; the assignment should be made only if thr is the |
| 2195 |
only top-level thr in the graph: */ |
| 2196 |
|
| 2197 |
thr->graph->last_sel_node = node; |
| 2198 |
|
| 2199 |
if (err != DB_SUCCESS) { |
| 2200 |
thr_get_trx(thr)->error_state = err; |
| 2201 |
|
| 2202 |
return (nullptr); |
| 2203 |
} |
| 2204 |
|
| 2205 |
return (thr); |
| 2206 |
} |
| 2207 |
|
| 2208 |
/** Performs a fetch for a cursor. |
| 2209 |
@return query thread to run next or NULL */ |
| 2210 |
que_thr_t *fetch_step(que_thr_t *thr) /*!< in: query thread */ |
| 2211 |
{ |
| 2212 |
sel_node_t *sel_node; |
| 2213 |
fetch_node_t *node; |
| 2214 |
|
| 2215 |
ut_ad(thr); |
| 2216 |
|
| 2217 |
node = static_cast<fetch_node_t *>(thr->run_node); |
| 2218 |
sel_node = node->cursor_def; |
| 2219 |
|
| 2220 |
ut_ad(que_node_get_type(node) == QUE_NODE_FETCH); |
| 2221 |
|
| 2222 |
if (thr->prev_node != que_node_get_parent(node)) { |
| 2223 |
if (sel_node->state != SEL_NODE_NO_MORE_ROWS) { |
| 2224 |
if (node->into_list) { |
| 2225 |
sel_assign_into_var_values(node->into_list, sel_node); |
| 2226 |
} else { |
| 2227 |
bool ret = (*node->func->func)(sel_node, node->func->arg); |
| 2228 |
|
| 2229 |
if (!ret) { |
| 2230 |
sel_node->state = SEL_NODE_NO_MORE_ROWS; |
| 2231 |
} |
| 2232 |
} |
| 2233 |
} |
| 2234 |
|
| 2235 |
thr->run_node = que_node_get_parent(node); |
| 2236 |
|
| 2237 |
return (thr); |
| 2238 |
} |
| 2239 |
|
| 2240 |
/* Make the fetch node the parent of the cursor definition for |
| 2241 |
the time of the fetch, so that execution knows to return to this |
| 2242 |
fetch node after a row has been selected or we know that there is |
| 2243 |
no row left */ |
| 2244 |
|
| 2245 |
sel_node->common.parent = node; |
| 2246 |
|
| 2247 |
if (sel_node->state == SEL_NODE_CLOSED) { |
| 2248 |
ib::error(ER_IB_MSG_1027) << "fetch called on a closed cursor"; |
| 2249 |
|
| 2250 |
thr_get_trx(thr)->error_state = DB_ERROR; |
| 2251 |
|
| 2252 |
return (nullptr); |
| 2253 |
} |
| 2254 |
|
| 2255 |
thr->run_node = sel_node; |
| 2256 |
|
| 2257 |
return (thr); |
| 2258 |
} |
| 2259 |
|
| 2260 |
void row_sel_convert_mysql_key_to_innobase(dtuple_t *tuple, byte *buf, |
| 2261 |
ulint buf_len, dict_index_t *index, |
| 2262 |
const byte *key_ptr, ulint key_len) { |
| 2263 |
byte *original_buf = buf; |
| 2264 |
const byte *original_key_ptr = key_ptr; |
| 2265 |
dict_field_t *field; |
| 2266 |
dfield_t *dfield; |
| 2267 |
ulint data_offset; |
| 2268 |
ulint data_len; |
| 2269 |
ulint data_field_len; |
| 2270 |
bool is_null; |
| 2271 |
const byte *key_end; |
| 2272 |
ulint n_fields = 0; |
| 2273 |
|
| 2274 |
/* For documentation of the key value storage format in MySQL, see |
| 2275 |
ha_innobase::store_key_val_for_row() in ha_innodb.cc. */ |
| 2276 |
|
| 2277 |
key_end = key_ptr + key_len; |
| 2278 |
|
| 2279 |
/* Permit us to access any field in the tuple (ULINT_MAX): */ |
| 2280 |
|
| 2281 |
dtuple_set_n_fields(tuple, ULINT_MAX); |
| 2282 |
|
| 2283 |
dfield = dtuple_get_nth_field(tuple, 0); |
| 2284 |
field = index->get_field(0); |
| 2285 |
|
| 2286 |
if (UNIV_UNLIKELY(dfield_get_type(dfield)->mtype == DATA_SYS)) { |
| 2287 |
/* A special case: we are looking for a position in the |
| 2288 |
generated clustered index which InnoDB automatically added |
| 2289 |
to a table with no primary key: the first and the only |
| 2290 |
ordering column is ROW_ID which InnoDB stored to the key_ptr |
| 2291 |
buffer. */ |
| 2292 |
|
| 2293 |
ut_a(key_len == DATA_ROW_ID_LEN); |
| 2294 |
|
| 2295 |
dfield_set_data(dfield, key_ptr, DATA_ROW_ID_LEN); |
| 2296 |
|
| 2297 |
dtuple_set_n_fields(tuple, 1); |
| 2298 |
|
| 2299 |
return; |
| 2300 |
} |
| 2301 |
|
| 2302 |
while (key_ptr < key_end) { |
| 2303 |
ulint type = dfield_get_type(dfield)->mtype; |
| 2304 |
ut_a(field->col->mtype == type); |
| 2305 |
|
| 2306 |
data_offset = 0; |
| 2307 |
is_null = false; |
| 2308 |
|
| 2309 |
if (!(dfield_get_type(dfield)->prtype & DATA_NOT_NULL)) { |
| 2310 |
/* The first byte in the field tells if this is |
| 2311 |
an SQL NULL value */ |
| 2312 |
|
| 2313 |
data_offset = 1; |
| 2314 |
|
| 2315 |
if (*key_ptr != 0) { |
| 2316 |
dfield_set_null(dfield); |
| 2317 |
|
| 2318 |
is_null = true; |
| 2319 |
} |
| 2320 |
} |
| 2321 |
|
| 2322 |
/* Calculate data length and data field total length */ |
| 2323 |
if (DATA_LARGE_MTYPE(type) || DATA_GEOMETRY_MTYPE(type)) { |
| 2324 |
/* For R-tree index, data length should be the |
| 2325 |
total size of the wkb data.*/ |
| 2326 |
if (dict_index_is_spatial(index)) { |
| 2327 |
ut_ad(DATA_GEOMETRY_MTYPE(type)); |
| 2328 |
data_len = key_len; |
| 2329 |
data_field_len = data_offset + data_len; |
| 2330 |
} else { |
| 2331 |
/* The key field is a column prefix of a BLOB |
| 2332 |
or TEXT, except DATA_POINT of GEOMETRY. */ |
| 2333 |
|
| 2334 |
ut_a(field->prefix_len > 0 || DATA_POINT_MTYPE(type)); |
| 2335 |
|
| 2336 |
/* MySQL stores the actual data length to the |
| 2337 |
first 2 bytes after the optional SQL NULL |
| 2338 |
marker byte. The storage format is |
| 2339 |
little-endian, that is, the most significant |
| 2340 |
byte at a higher address. In UTF-8, MySQL |
| 2341 |
seems to reserve field->prefix_len bytes for |
| 2342 |
storing this field in the key value buffer, |
| 2343 |
even though the actual value only takes data |
| 2344 |
len bytes from the start. |
| 2345 |
For POINT of GEOMETRY, which has no prefix |
| 2346 |
because it's now a fixed length type in |
| 2347 |
InnoDB, we have to get DATA_POINT_LEN bytes, |
| 2348 |
which is original prefix length of POINT. */ |
| 2349 |
|
| 2350 |
data_len = key_ptr[data_offset] + 256 * key_ptr[data_offset + 1]; |
| 2351 |
data_field_len = |
| 2352 |
data_offset + 2 + |
| 2353 |
(type == DATA_POINT ? DATA_POINT_LEN : field->prefix_len); |
| 2354 |
|
| 2355 |
data_offset += 2; |
| 2356 |
|
| 2357 |
/* Now that we know the length, we store the |
| 2358 |
column value like it would be a fixed char |
| 2359 |
field */ |
| 2360 |
} |
| 2361 |
|
| 2362 |
} else if (field->prefix_len > 0) { |
| 2363 |
/* Looks like MySQL pads unused end bytes in the |
| 2364 |
prefix with space. Therefore, also in UTF-8, it is ok |
| 2365 |
to compare with a prefix containing full prefix_len |
| 2366 |
bytes, and no need to take at most prefix_len / 3 |
| 2367 |
UTF-8 characters from the start. |
| 2368 |
If the prefix is used as the upper end of a LIKE |
| 2369 |
'abc%' query, then MySQL pads the end with chars |
| 2370 |
0xff. TODO: in that case does it any harm to compare |
| 2371 |
with the full prefix_len bytes. How do characters |
| 2372 |
0xff in UTF-8 behave? */ |
| 2373 |
|
| 2374 |
data_len = field->prefix_len; |
| 2375 |
data_field_len = data_offset + data_len; |
| 2376 |
} else { |
| 2377 |
data_len = dfield_get_type(dfield)->len; |
| 2378 |
data_field_len = data_offset + data_len; |
| 2379 |
} |
| 2380 |
|
| 2381 |
if ((dtype_get_mysql_type(dfield_get_type(dfield)) == |
| 2382 |
DATA_MYSQL_TRUE_VARCHAR) && |
| 2383 |
(type != DATA_INT)) { |
| 2384 |
/* In a MySQL key value format, a true VARCHAR is |
| 2385 |
always preceded by 2 bytes of a length field. |
| 2386 |
dfield_get_type(dfield)->len returns the maximum |
| 2387 |
'payload' len in bytes. That does not include the |
| 2388 |
2 bytes that tell the actual data length. |
| 2389 |
|
| 2390 |
We added the check != DATA_INT to make sure we do |
| 2391 |
not treat MySQL ENUM or SET as a true VARCHAR! */ |
| 2392 |
|
| 2393 |
data_len += 2; |
| 2394 |
data_field_len += 2; |
| 2395 |
} |
| 2396 |
|
| 2397 |
/* Storing may use at most data_len bytes of buf */ |
| 2398 |
|
| 2399 |
if (UNIV_LIKELY(!is_null)) { |
| 2400 |
buf = row_mysql_store_col_in_innobase_format( |
| 2401 |
dfield, buf, false, /* MySQL key value format col */ |
| 2402 |
key_ptr + data_offset, data_len, dict_table_is_comp(index->table)); |
| 2403 |
ut_a(buf <= original_buf + buf_len); |
| 2404 |
} |
| 2405 |
|
| 2406 |
key_ptr += data_field_len; |
| 2407 |
|
| 2408 |
if (UNIV_UNLIKELY(key_ptr > key_end)) { |
| 2409 |
/* The last field in key was not a complete key field |
| 2410 |
but a prefix of it. |
| 2411 |
|
| 2412 |
Print a warning about this! HA_READ_PREFIX_LAST does |
| 2413 |
not currently work in InnoDB with partial-field key |
| 2414 |
value prefixes. Since MySQL currently uses a padding |
| 2415 |
trick to calculate LIKE 'abc%' type queries there |
| 2416 |
should never be partial-field prefixes in searches. */ |
| 2417 |
|
| 2418 |
ib::warn(ER_IB_MSG_1028) |
| 2419 |
<< "Using a partial-field key prefix in" |
| 2420 |
" search, index " |
| 2421 |
<< index->name << " of table " << index->table->name |
| 2422 |
<< ". Last data field length " << data_field_len |
| 2423 |
<< " bytes, key ptr now" |
| 2424 |
" exceeds key end by " |
| 2425 |
<< (key_ptr - key_end) << " bytes. Key value in the MySQL format:"; |
| 2426 |
|
| 2427 |
ut_print_buf(stderr, original_key_ptr, key_len); |
| 2428 |
putc('\n', stderr); |
| 2429 |
|
| 2430 |
if (!is_null) { |
| 2431 |
ulint len = dfield_get_len(dfield); |
| 2432 |
dfield_set_len(dfield, len - (ulint)(key_ptr - key_end)); |
| 2433 |
} |
| 2434 |
ut_d(ut_error); |
| 2435 |
} |
| 2436 |
|
| 2437 |
n_fields++; |
| 2438 |
field++; |
| 2439 |
dfield++; |
| 2440 |
} |
| 2441 |
|
| 2442 |
ut_a(buf <= original_buf + buf_len); |
| 2443 |
|
| 2444 |
/* We set the length of tuple to n_fields: we assume that the memory |
| 2445 |
area allocated for it is big enough (usually bigger than n_fields). */ |
| 2446 |
|
| 2447 |
dtuple_set_n_fields(tuple, n_fields); |
| 2448 |
} |
| 2449 |
|
| 2450 |
/** Stores the row id to the prebuilt struct. */ |
| 2451 |
static void row_sel_store_row_id_to_prebuilt( |
| 2452 |
row_prebuilt_t *prebuilt, /*!< in/out: prebuilt */ |
| 2453 |
const rec_t *index_rec, /*!< in: record */ |
| 2454 |
const dict_index_t *index, /*!< in: index of the record */ |
| 2455 |
const ulint *offsets) /*!< in: rec_get_offsets |
| 2456 |
(index_rec, index) */ |
| 2457 |
{ |
| 2458 |
const byte *data; |
| 2459 |
ulint len; |
| 2460 |
|
| 2461 |
ut_ad(rec_offs_validate(index_rec, index, offsets)); |
| 2462 |
|
| 2463 |
data = rec_get_nth_field(index, index_rec, offsets, |
| 2464 |
index->get_sys_col_pos(DATA_ROW_ID), &len); |
| 2465 |
|
| 2466 |
if (UNIV_UNLIKELY(len != DATA_ROW_ID_LEN)) { |
| 2467 |
ib::error(ER_IB_MSG_1029) |
| 2468 |
<< "Row id field is wrong length " << len |
| 2469 |
<< " in" |
| 2470 |
" index " |
| 2471 |
<< index->name << " of table " << index->table->name |
| 2472 |
<< ", Field number " << index->get_sys_col_pos(DATA_ROW_ID) |
| 2473 |
<< ", record:"; |
| 2474 |
|
| 2475 |
rec_print_new(stderr, index_rec, offsets); |
| 2476 |
putc('\n', stderr); |
| 2477 |
ut_error; |
| 2478 |
} |
| 2479 |
|
| 2480 |
ut_memcpy(prebuilt->row_id, data, len); |
| 2481 |
} |
| 2482 |
|
| 2483 |
/** Stores a non-SQL-NULL field in the MySQL format. The counterpart of this |
| 2484 |
function is row_mysql_store_col_in_innobase_format() in row0mysql.cc. |
| 2485 |
@param[in,out] dest buffer where to store; NOTE |
| 2486 |
that BLOBs are not in themselves stored |
| 2487 |
here: the caller must allocate and copy |
| 2488 |
the BLOB into buffer before, and pass |
| 2489 |
the pointer to the BLOB in 'data' |
| 2490 |
@param[in] templ MySQL column template. Its following fields |
| 2491 |
are referenced: type, is_unsigned, |
| 2492 |
mysql_col_len, mbminlen, mbmaxlen |
| 2493 |
@param[in] index InnoDB index |
| 2494 |
@param[in] field_no templ->rec_field_no or templ->clust_rec_field_no |
| 2495 |
or templ->icp_rec_field_no |
| 2496 |
@param[in] data data to store |
| 2497 |
@param[in] len length of the data |
| 2498 |
@param[in] sec_field secondary index field no if the secondary index |
| 2499 |
record but the prebuilt template is in |
| 2500 |
clustered index format and used only for end |
| 2501 |
range comparison. */ |
| 2502 |
void row_sel_field_store_in_mysql_format_func( |
| 2503 |
byte *dest, const mysql_row_templ_t *templ, const dict_index_t *index, |
| 2504 |
IF_DEBUG(ulint field_no, ) const byte *data, |
| 2505 |
ulint len IF_DEBUG(, ulint sec_field)) { |
| 2506 |
byte *ptr; |
| 2507 |
#ifdef UNIV_DEBUG |
| 2508 |
const dict_field_t *field = |
| 2509 |
templ->is_virtual ? nullptr : index->get_field(field_no); |
| 2510 |
|
| 2511 |
bool clust_templ_for_sec = (sec_field != ULINT_UNDEFINED); |
| 2512 |
#endif /* UNIV_DEBUG */ |
| 2513 |
|
| 2514 |
if (templ->is_multi_val) { |
| 2515 |
ib::fatal(UT_LOCATION_HERE, ER_CONVERT_MULTI_VALUE) |
| 2516 |
<< "Table name: " << index->table->name |
| 2517 |
<< " Index name: " << index->name; |
| 2518 |
} |
| 2519 |
|
| 2520 |
auto const mysql_col_len = templ->mysql_col_len; |
| 2521 |
|
| 2522 |
ut_ad(rec_field_not_null_not_add_col_def(len)); |
| 2523 |
UNIV_MEM_ASSERT_RW(data, len); |
| 2524 |
UNIV_MEM_ASSERT_W(dest, mysql_col_len); |
| 2525 |
UNIV_MEM_INVALID(dest, mysql_col_len); |
| 2526 |
|
| 2527 |
switch (templ->type) { |
| 2528 |
const byte *field_end; |
| 2529 |
byte *pad; |
| 2530 |
case DATA_INT: |
| 2531 |
/* Convert integer data from Innobase to a little-endian |
| 2532 |
format, sign bit restored to normal */ |
| 2533 |
|
| 2534 |
ptr = dest + len; |
| 2535 |
|
| 2536 |
for (;;) { |
| 2537 |
ptr--; |
| 2538 |
*ptr = *data; |
| 2539 |
if (ptr == dest) { |
| 2540 |
break; |
| 2541 |
} |
| 2542 |
data++; |
| 2543 |
} |
| 2544 |
|
| 2545 |
if (!templ->is_unsigned) { |
| 2546 |
dest[len - 1] = (byte)(dest[len - 1] ^ 128); |
| 2547 |
} |
| 2548 |
|
| 2549 |
ut_ad(mysql_col_len == len); |
| 2550 |
|
| 2551 |
break; |
| 2552 |
|
| 2553 |
case DATA_VARCHAR: |
| 2554 |
case DATA_VARMYSQL: |
| 2555 |
case DATA_BINARY: |
| 2556 |
field_end = dest + mysql_col_len; |
| 2557 |
|
| 2558 |
if (templ->mysql_type == DATA_MYSQL_TRUE_VARCHAR) { |
| 2559 |
/* This is a >= 5.0.3 type true VARCHAR. Store the |
| 2560 |
length of the data to the first byte or the first |
| 2561 |
two bytes of dest. */ |
| 2562 |
|
| 2563 |
dest = |
| 2564 |
row_mysql_store_true_var_len(dest, len, templ->mysql_length_bytes); |
| 2565 |
/* Copy the actual data. Leave the rest of the |
| 2566 |
buffer uninitialized. */ |
| 2567 |
memcpy(dest, data, len); |
| 2568 |
break; |
| 2569 |
} |
| 2570 |
|
| 2571 |
/* Copy the actual data */ |
| 2572 |
ut_memcpy(dest, data, len); |
| 2573 |
|
| 2574 |
/* Pad with trailing spaces. */ |
| 2575 |
|
| 2576 |
pad = dest + len; |
| 2577 |
|
| 2578 |
ut_ad(templ->mbminlen <= templ->mbmaxlen); |
| 2579 |
|
| 2580 |
/* We treat some Unicode charset strings specially. */ |
| 2581 |
switch (templ->mbminlen) { |
| 2582 |
case 4: |
| 2583 |
/* InnoDB should never have stripped partial |
| 2584 |
UTF-32 characters. */ |
| 2585 |
ut_a(!(len & 3)); |
| 2586 |
break; |
| 2587 |
case 2: |
| 2588 |
/* A space char is two bytes, |
| 2589 |
0x0020 in UCS2 and UTF-16 */ |
| 2590 |
|
| 2591 |
if (UNIV_UNLIKELY(len & 1)) { |
| 2592 |
/* A 0x20 has been stripped from the column. |
| 2593 |
Pad it back. */ |
| 2594 |
|
| 2595 |
if (pad < field_end) { |
| 2596 |
*pad++ = 0x20; |
| 2597 |
} |
| 2598 |
} |
| 2599 |
} |
| 2600 |
|
| 2601 |
row_mysql_pad_col(templ->mbminlen, pad, field_end - pad); |
| 2602 |
break; |
| 2603 |
|
| 2604 |
case DATA_BLOB: |
| 2605 |
/* Store a pointer to the BLOB buffer to dest: the BLOB was |
| 2606 |
already copied to the buffer in row_sel_store_mysql_rec */ |
| 2607 |
|
| 2608 |
row_mysql_store_blob_ref(dest, mysql_col_len, data, len); |
| 2609 |
break; |
| 2610 |
|
| 2611 |
case DATA_POINT: |
| 2612 |
case DATA_VAR_POINT: |
| 2613 |
case DATA_GEOMETRY: |
| 2614 |
/* We store all geometry data as BLOB data at server layer. */ |
| 2615 |
row_mysql_store_geometry(dest, mysql_col_len, data, len); |
| 2616 |
break; |
| 2617 |
|
| 2618 |
case DATA_MYSQL: |
| 2619 |
memcpy(dest, data, len); |
| 2620 |
|
| 2621 |
ut_ad(mysql_col_len >= len); |
| 2622 |
ut_ad(templ->mbmaxlen >= templ->mbminlen); |
| 2623 |
|
| 2624 |
/* If field_no equals to templ->icp_rec_field_no, we are examining a row |
| 2625 |
pointed by "icp_rec_field_no". There is possibility that icp_rec_field_no |
| 2626 |
refers to a field in a secondary index while templ->rec_field_no points |
| 2627 |
to field in a primary index. The length should still be equal, unless the |
| 2628 |
field pointed by icp_rec_field_no has a prefix or this is a virtual |
| 2629 |
column. |
| 2630 |
For end range condition check of secondary index with cluster index |
| 2631 |
template (clust_templ_for_sec), the index column data length (len) |
| 2632 |
could be smaller than the actual column length (mysql_col_len) if index |
| 2633 |
is on column prefix. This is not a real issue because the end range check |
| 2634 |
would only need the prefix part. The length check assert is relaxed for |
| 2635 |
clust_templ_for_sec. */ |
| 2636 |
ut_ad(templ->is_virtual || templ->mbmaxlen > templ->mbminlen || |
| 2637 |
mysql_col_len == len || clust_templ_for_sec || |
| 2638 |
(field_no == templ->icp_rec_field_no && field->prefix_len > 0)); |
| 2639 |
|
| 2640 |
/* The following assertion would fail for old tables |
| 2641 |
containing UTF-8 ENUM columns due to Bug #9526. */ |
| 2642 |
ut_ad(!templ->mbmaxlen || !(mysql_col_len % templ->mbmaxlen)); |
| 2643 |
/* Length of the record will be less in case of |
| 2644 |
clust_templ_for_sec is true or if it is fetched |
| 2645 |
from prefix virtual column in virtual index. */ |
| 2646 |
ut_ad(templ->is_virtual || clust_templ_for_sec || |
| 2647 |
len * templ->mbmaxlen >= mysql_col_len || |
| 2648 |
index->has_row_versions() || |
| 2649 |
(field_no == templ->icp_rec_field_no && field->prefix_len > 0)); |
| 2650 |
ut_ad(templ->is_virtual || !(field->prefix_len % templ->mbmaxlen)); |
| 2651 |
|
| 2652 |
/* Pad with spaces. This undoes the stripping |
| 2653 |
done in row0mysql.cc, function |
| 2654 |
row_mysql_store_col_in_innobase_format(). */ |
| 2655 |
if ((templ->mbminlen == 1 && templ->mbmaxlen != 1) || |
| 2656 |
(templ->is_virtual && mysql_col_len > len)) { |
| 2657 |
/* NOTE: This comment is for the second condition: |
| 2658 |
This probably comes from a prefix virtual index, where no complete |
| 2659 |
value can be got because the full virtual column can only be |
| 2660 |
calculated in server layer for now. Since server now assumes the |
| 2661 |
returned value should always have padding spaces, thus the fixup. |
| 2662 |
However, a proper and more efficient solution is that server does |
| 2663 |
not depend on the trailing spaces to check the terminal of the CHAR |
| 2664 |
string, because at least in this case,server should know it's a prefix |
| 2665 |
index search and no complete value would be got. */ |
| 2666 |
memset(dest + len, 0x20, mysql_col_len - len); |
| 2667 |
} |
| 2668 |
break; |
| 2669 |
|
| 2670 |
default: |
| 2671 |
#ifdef UNIV_DEBUG |
| 2672 |
case DATA_SYS_CHILD: |
| 2673 |
case DATA_SYS: |
| 2674 |
/* These column types should never be shipped to MySQL. */ |
| 2675 |
ut_d(ut_error); |
| 2676 |
[[fallthrough]]; |
| 2677 |
|
| 2678 |
case DATA_CHAR: |
| 2679 |
case DATA_FIXBINARY: |
| 2680 |
case DATA_FLOAT: |
| 2681 |
case DATA_DOUBLE: |
| 2682 |
case DATA_DECIMAL: |
| 2683 |
/* Above are the valid column types for MySQL data. */ |
| 2684 |
#endif /* UNIV_DEBUG */ |
| 2685 |
|
| 2686 |
/* If sec_field value is present then mapping of |
| 2687 |
secondary index records to clustered index template |
| 2688 |
happens for end range comparison. So length can |
| 2689 |
vary according to secondary index record length. */ |
| 2690 |
ut_ad((templ->is_virtual && !field) || |
| 2691 |
((field && field->prefix_len) |
| 2692 |
? field->prefix_len == len |
| 2693 |
: (clust_templ_for_sec || mysql_col_len == len))); |
| 2694 |
|
| 2695 |
memcpy(dest, data, len); |
| 2696 |
} |
| 2697 |
} |
| 2698 |
|
| 2699 |
// clang-format off |
| 2700 |
/** Convert a field in the Innobase format to a field in the MySQL format. |
| 2701 |
@param[out] mysql_rec Record in the MySQL format |
| 2702 |
@param[in,out] prebuilt Prebuilt struct |
| 2703 |
@param[in] rec InnoDB record; must be protected by a page |
| 2704 |
latch |
| 2705 |
@param[in] rec_index Index of rec |
| 2706 |
@param[in] prebuilt_index prebuilt->index |
| 2707 |
@param[in] offsets Array returned by rec_get_offsets() |
| 2708 |
@param[in] field_no templ->rec_field_no or |
| 2709 |
templ->clust_rec_field_no or |
| 2710 |
templ->icp_rec_field_no or sec field no if |
| 2711 |
clust_templ_for_sec is true |
| 2712 |
@param[in] templ row template |
| 2713 |
@param[in] sec_field_no Secondary index field no if the secondary index |
| 2714 |
record but the prebuilt template is in |
| 2715 |
clustered index format and used only for end |
| 2716 |
range comparison. |
| 2717 |
@param[in] lob_undo the LOB undo information. |
| 2718 |
@param[in,out] blob_heap If not null then use this heap for BLOBs */ |
| 2719 |
// clang-format on |
| 2720 |
[[nodiscard]] static bool row_sel_store_mysql_field( |
| 2721 |
byte *mysql_rec, row_prebuilt_t *prebuilt, const rec_t *rec, |
| 2722 |
const dict_index_t *rec_index, const dict_index_t *prebuilt_index, |
| 2723 |
const ulint *offsets, ulint field_no, const mysql_row_templ_t *templ, |
| 2724 |
ulint sec_field_no, lob::undo_vers_t *lob_undo, mem_heap_t *&blob_heap) { |
| 2725 |
DBUG_TRACE; |
| 2726 |
|
| 2727 |
const byte *data; |
| 2728 |
ulint len; |
| 2729 |
ulint clust_field_no = 0; |
| 2730 |
bool clust_templ_for_sec = (sec_field_no != ULINT_UNDEFINED); |
| 2731 |
const dict_index_t *index_used = |
| 2732 |
(clust_templ_for_sec) ? prebuilt_index : rec_index; |
| 2733 |
|
| 2734 |
ut_ad(templ); |
| 2735 |
ut_ad(prebuilt->default_rec); |
| 2736 |
ut_ad(templ >= prebuilt->mysql_template); |
| 2737 |
ut_ad(templ < &prebuilt->mysql_template[prebuilt->n_template]); |
| 2738 |
|
| 2739 |
ut_ad(clust_templ_for_sec || field_no == templ->clust_rec_field_no || |
| 2740 |
field_no == templ->rec_field_no || field_no == templ->icp_rec_field_no); |
| 2741 |
|
| 2742 |
ut_ad(rec_offs_validate(rec, index_used, offsets)); |
| 2743 |
|
| 2744 |
/* If sec_field_no is present then extract the data from record |
| 2745 |
using secondary field no. */ |
| 2746 |
if (clust_templ_for_sec) { |
| 2747 |
clust_field_no = field_no; |
| 2748 |
field_no = sec_field_no; |
| 2749 |
} |
| 2750 |
|
| 2751 |
if (rec_offs_nth_extern(index_used, offsets, field_no)) { |
| 2752 |
/* Copy an externally stored field to a temporary heap */ |
| 2753 |
|
| 2754 |
ut_a(!prebuilt->trx->has_search_latch); |
| 2755 |
ut_ad(field_no == templ->clust_rec_field_no); |
| 2756 |
ut_ad(templ->type != DATA_POINT); |
| 2757 |
|
| 2758 |
mem_heap_t *heap; |
| 2759 |
|
| 2760 |
if (DATA_LARGE_MTYPE(templ->type)) { |
| 2761 |
if (blob_heap == nullptr) { |
| 2762 |
blob_heap = mem_heap_create(UNIV_PAGE_SIZE, UT_LOCATION_HERE); |
| 2763 |
} |
| 2764 |
|
| 2765 |
heap = blob_heap; |
| 2766 |
} else { |
| 2767 |
heap = mem_heap_create(UNIV_PAGE_SIZE, UT_LOCATION_HERE); |
| 2768 |
} |
| 2769 |
|
| 2770 |
/* NOTE: if we are retrieving a big BLOB, we may |
| 2771 |
already run out of memory in the next call, which |
| 2772 |
causes an assert */ |
| 2773 |
|
| 2774 |
dict_index_t *clust_index = rec_index->table->first_index(); |
| 2775 |
|
| 2776 |
const page_size_t page_size = dict_table_page_size(rec_index->table); |
| 2777 |
|
| 2778 |
size_t lob_version = 0; |
| 2779 |
|
| 2780 |
data = lob::btr_rec_copy_externally_stored_field( |
| 2781 |
prebuilt->trx, clust_index, rec, offsets, page_size, field_no, &len, |
| 2782 |
&lob_version, dict_index_is_sdi(rec_index), heap); |
| 2783 |
|
| 2784 |
if (data == nullptr) { |
| 2785 |
/* The externally stored field was not written |
| 2786 |
yet. This record should only be seen by |
| 2787 |
trx_rollback_or_clean_all_recovered() or any |
| 2788 |
yet. This can happen after optimization which |
| 2789 |
was done after for Bug#23481444 where we read |
| 2790 |
last record in the page to find the end range |
| 2791 |
scan. If we encounter this we just return false |
| 2792 |
In any other case this row should be only seen |
| 2793 |
by recv_recovery_rollback_active() or any |
| 2794 |
TRX_ISO_READ_UNCOMMITTED transactions. */ |
| 2795 |
|
| 2796 |
if (heap != blob_heap) { |
| 2797 |
mem_heap_free(heap); |
| 2798 |
} |
| 2799 |
|
| 2800 |
ut_a((!prebuilt->idx_cond && |
| 2801 |
prebuilt->m_mysql_handler->end_range != nullptr) || |
| 2802 |
(prebuilt->trx->isolation_level == TRX_ISO_READ_UNCOMMITTED)); |
| 2803 |
return false; |
| 2804 |
} |
| 2805 |
|
| 2806 |
if (lob_undo != nullptr) { |
| 2807 |
ulint local_len; |
| 2808 |
const byte *field_data = rec_get_nth_field_instant( |
| 2809 |
rec, offsets, field_no, index_used, &local_len); |
| 2810 |
const byte *field_ref = |
| 2811 |
field_data + local_len - BTR_EXTERN_FIELD_REF_SIZE; |
| 2812 |
|
| 2813 |
lob::ref_t ref(const_cast<byte *>(field_ref)); |
| 2814 |
lob_undo->apply(clust_index, field_no, const_cast<byte *>(data), len, |
| 2815 |
lob_version, ref.page_no()); |
| 2816 |
} |
| 2817 |
|
| 2818 |
ut_a(rec_field_not_null_not_add_col_def(len)); |
| 2819 |
|
| 2820 |
row_sel_field_store_in_mysql_format(mysql_rec + templ->mysql_col_offset, |
| 2821 |
templ, rec_index, field_no, data, len, |
| 2822 |
ULINT_UNDEFINED); |
| 2823 |
|
| 2824 |
if (heap != blob_heap) { |
| 2825 |
mem_heap_free(heap); |
| 2826 |
} |
| 2827 |
} else { |
| 2828 |
/* Field is stored in the row. */ |
| 2829 |
|
| 2830 |
data = rec_get_nth_field_instant(rec, offsets, field_no, index_used, &len); |
| 2831 |
|
| 2832 |
if (len == UNIV_SQL_NULL) { |
| 2833 |
/* MySQL assumes that the field for an SQL |
| 2834 |
NULL value is set to the default value. */ |
| 2835 |
ut_ad(templ->mysql_null_bit_mask); |
| 2836 |
|
| 2837 |
UNIV_MEM_ASSERT_RW(prebuilt->default_rec + templ->mysql_col_offset, |
| 2838 |
templ->mysql_col_len); |
| 2839 |
mysql_rec[templ->mysql_null_byte_offset] |= |
| 2840 |
(byte)templ->mysql_null_bit_mask; |
| 2841 |
memcpy(mysql_rec + templ->mysql_col_offset, |
| 2842 |
(const byte *)prebuilt->default_rec + templ->mysql_col_offset, |
| 2843 |
templ->mysql_col_len); |
| 2844 |
return true; |
| 2845 |
} |
| 2846 |
|
| 2847 |
if (DATA_LARGE_MTYPE(templ->type) || DATA_GEOMETRY_MTYPE(templ->type)) { |
| 2848 |
/* It is a BLOB field locally stored in the |
| 2849 |
InnoDB record: we MUST copy its contents to |
| 2850 |
prebuilt->blob_heap here because |
| 2851 |
row_sel_field_store_in_mysql_format() stores a |
| 2852 |
pointer to the data, and the data passed to us |
| 2853 |
will be invalid as soon as the |
| 2854 |
mini-transaction is committed and the page |
| 2855 |
latch on the clustered index page is |
| 2856 |
released. |
| 2857 |
For DATA_POINT, it's stored like CHAR in InnoDB, |
| 2858 |
but it should be a BLOB field in MySQL layer. So we |
| 2859 |
still treated it as BLOB here. */ |
| 2860 |
|
| 2861 |
mem_heap_t *heap{}; |
| 2862 |
|
| 2863 |
if (blob_heap == nullptr) { |
| 2864 |
blob_heap = mem_heap_create(UNIV_PAGE_SIZE, UT_LOCATION_HERE); |
| 2865 |
} |
| 2866 |
|
| 2867 |
heap = blob_heap; |
| 2868 |
data = static_cast<byte *>(mem_heap_dup(heap, data, len)); |
| 2869 |
} |
| 2870 |
|
| 2871 |
/* Reassign the clustered index field no. */ |
| 2872 |
if (clust_templ_for_sec) { |
| 2873 |
field_no = clust_field_no; |
| 2874 |
} |
| 2875 |
|
| 2876 |
row_sel_field_store_in_mysql_format(mysql_rec + templ->mysql_col_offset, |
| 2877 |
templ, rec_index, field_no, data, len, |
| 2878 |
sec_field_no); |
| 2879 |
} |
| 2880 |
|
| 2881 |
ut_ad(rec_field_not_null_not_add_col_def(len)); |
| 2882 |
|
| 2883 |
if (templ->mysql_null_bit_mask) { |
| 2884 |
/* It is a nullable column with a non-NULL value */ |
| 2885 |
mysql_rec[templ->mysql_null_byte_offset] &= |
| 2886 |
~(byte)templ->mysql_null_bit_mask; |
| 2887 |
} |
| 2888 |
|
| 2889 |
return true; |
| 2890 |
} |
| 2891 |
|
| 2892 |
bool row_sel_store_mysql_rec(byte *mysql_rec, row_prebuilt_t *prebuilt, |
| 2893 |
const rec_t *rec, const dtuple_t *vrow, |
| 2894 |
bool rec_clust, const dict_index_t *rec_index, |
| 2895 |
const dict_index_t *prebuilt_index, |
| 2896 |
const ulint *offsets, bool clust_templ_for_sec, |
| 2897 |
lob::undo_vers_t *lob_undo, |
| 2898 |
mem_heap_t *&blob_heap) { |
| 2899 |
std::vector<const dict_col_t *> template_col; |
| 2900 |
|
| 2901 |
DBUG_TRACE; |
| 2902 |
|
| 2903 |
ut_ad(rec_clust || rec_index == prebuilt_index); |
| 2904 |
ut_ad(!rec_clust || rec_index->is_clustered()); |
| 2905 |
|
| 2906 |
/* If blob_heap provided by the caller is not that of prebuilt's blob heap |
| 2907 |
then the onus would be on the caller to empty the blob heap if required. */ |
| 2908 |
if (blob_heap != nullptr && blob_heap == prebuilt->blob_heap) { |
| 2909 |
mem_heap_empty(blob_heap); |
| 2910 |
} |
| 2911 |
|
| 2912 |
if (clust_templ_for_sec) { |
| 2913 |
/* Store all clustered index column of secondary index record. */ |
| 2914 |
for (ulint i = 0; i < dict_index_get_n_fields(prebuilt_index); i++) { |
| 2915 |
auto sec_field = |
| 2916 |
dict_index_get_nth_field_pos(rec_index, prebuilt_index, i); |
| 2917 |
|
| 2918 |
if (sec_field == ULINT_UNDEFINED) { |
| 2919 |
template_col.push_back(nullptr); |
| 2920 |
continue; |
| 2921 |
} |
| 2922 |
|
| 2923 |
const auto field = rec_index->get_field(sec_field); |
| 2924 |
const auto col = field->col; |
| 2925 |
template_col.push_back(col); |
| 2926 |
} |
| 2927 |
} |
| 2928 |
|
| 2929 |
for (ulint i = 0; i < prebuilt->n_template; i++) { |
| 2930 |
const auto templ = &prebuilt->mysql_template[i]; |
| 2931 |
|
| 2932 |
/* Skip multi-value columns; since they can not be explicitly |
| 2933 |
requested by the query, they may only be here in scenarios |
| 2934 |
where all index columns are included routinely, like these: |
| 2935 |
1. Index-only scan (done for covering index): all index field |
| 2936 |
are stored regardless of whether they are requested or not |
| 2937 |
(depending on optimization options): for multi-values they |
| 2938 |
need not be stored, as they may never be requested. |
| 2939 |
2. Cross-partition index scan, for the purpose of index merge: |
| 2940 |
not needed since multi-values do not introduce ordering and |
| 2941 |
so are not needed for index merge. */ |
| 2942 |
if (templ->is_multi_val) { |
| 2943 |
/* Multi-value columns are always virtual */ |
| 2944 |
ut_ad(templ->is_virtual); |
| 2945 |
continue; |
| 2946 |
} |
| 2947 |
|
| 2948 |
if (templ->is_virtual && rec_index->is_clustered()) { |
| 2949 |
/* Skip virtual columns if it is not a covered |
| 2950 |
search or virtual key read is not requested. */ |
| 2951 |
if ((prebuilt_index != nullptr && |
| 2952 |
!dict_index_has_virtual(prebuilt_index)) || |
| 2953 |
(!prebuilt->read_just_key && !prebuilt->m_read_virtual_key) || |
| 2954 |
!rec_clust) { |
| 2955 |
continue; |
| 2956 |
} |
| 2957 |
|
| 2958 |
dict_v_col_t *col; |
| 2959 |
col = |
| 2960 |
dict_table_get_nth_v_col(rec_index->table, templ->clust_rec_field_no); |
| 2961 |
|
| 2962 |
ut_ad(vrow); |
| 2963 |
|
| 2964 |
const auto dfield = dtuple_get_nth_v_field(vrow, col->v_pos); |
| 2965 |
|
| 2966 |
/* If this is a partitioned table, it might request |
| 2967 |
InnoDB to fill out virtual column data for search |
| 2968 |
index key values while other non key columns are also |
| 2969 |
getting selected. The non-key virtual columns may |
| 2970 |
not be materialized and we should skip them. */ |
| 2971 |
if (dfield_get_type(dfield)->mtype == DATA_MISSING) { |
| 2972 |
ut_ad(prebuilt->m_read_virtual_key); |
| 2973 |
|
| 2974 |
/* If it is part of index key the data should |
| 2975 |
have been materialized. */ |
| 2976 |
ut_ad(prebuilt_index->get_col_pos(col->v_pos, false, true) == |
| 2977 |
ULINT_UNDEFINED); |
| 2978 |
|
| 2979 |
continue; |
| 2980 |
} |
| 2981 |
|
| 2982 |
if (dfield->len == UNIV_SQL_NULL) { |
| 2983 |
mysql_rec[templ->mysql_null_byte_offset] |= |
| 2984 |
(byte)templ->mysql_null_bit_mask; |
| 2985 |
memcpy(mysql_rec + templ->mysql_col_offset, |
| 2986 |
(const byte *)prebuilt->default_rec + templ->mysql_col_offset, |
| 2987 |
templ->mysql_col_len); |
| 2988 |
} else { |
| 2989 |
row_sel_field_store_in_mysql_format( |
| 2990 |
mysql_rec + templ->mysql_col_offset, templ, rec_index, |
| 2991 |
templ->clust_rec_field_no, (const byte *)dfield->data, dfield->len, |
| 2992 |
ULINT_UNDEFINED); |
| 2993 |
if (templ->mysql_null_bit_mask) { |
| 2994 |
mysql_rec[templ->mysql_null_byte_offset] &= |
| 2995 |
~(byte)templ->mysql_null_bit_mask; |
| 2996 |
} |
| 2997 |
} |
| 2998 |
|
| 2999 |
continue; |
| 3000 |
} |
| 3001 |
|
| 3002 |
ulint field_no = |
| 3003 |
rec_clust ? templ->clust_rec_field_no : templ->rec_field_no; |
| 3004 |
|
| 3005 |
ulint sec_field_no = ULINT_UNDEFINED; |
| 3006 |
|
| 3007 |
/* We should never deliver column prefixes to MySQL, |
| 3008 |
except for evaluating innobase_index_cond() or |
| 3009 |
row_search_end_range_check(). */ |
| 3010 |
ut_ad(rec_index->get_field(field_no)->prefix_len == 0); |
| 3011 |
|
| 3012 |
if (clust_templ_for_sec) { |
| 3013 |
std::vector<const dict_col_t *>::iterator it; |
| 3014 |
const dict_field_t *field = rec_index->get_field(field_no); |
| 3015 |
const dict_col_t *col = field->col; |
| 3016 |
|
| 3017 |
it = std::find(template_col.begin(), template_col.end(), col); |
| 3018 |
|
| 3019 |
if (it == template_col.end()) { |
| 3020 |
continue; |
| 3021 |
} |
| 3022 |
|
| 3023 |
ut_ad(templ->rec_field_no == templ->clust_rec_field_no); |
| 3024 |
|
| 3025 |
sec_field_no = it - template_col.begin(); |
| 3026 |
} |
| 3027 |
|
| 3028 |
if (!row_sel_store_mysql_field(mysql_rec, prebuilt, rec, rec_index, |
| 3029 |
prebuilt_index, offsets, field_no, templ, |
| 3030 |
sec_field_no, lob_undo, blob_heap)) { |
| 3031 |
return false; |
| 3032 |
} |
| 3033 |
} |
| 3034 |
|
| 3035 |
/* FIXME: We only need to read the doc_id if an FTS indexed column is being |
| 3036 |
updated. |
| 3037 |
NOTE, the record can be cluster or secondary index record. |
| 3038 |
If secondary index is used then FTS_DOC_ID column should be part of this |
| 3039 |
index. */ |
| 3040 |
if (dict_table_has_fts_index(rec_index->table)) { |
| 3041 |
if ((rec_index->is_clustered() && !clust_templ_for_sec) || |
| 3042 |
prebuilt->fts_doc_id_in_read_set) { |
| 3043 |
prebuilt->fts_doc_id = |
| 3044 |
fts_get_doc_id_from_rec(rec_index->table, rec, rec_index, nullptr); |
| 3045 |
} |
| 3046 |
} |
| 3047 |
|
| 3048 |
return true; |
| 3049 |
} |
| 3050 |
|
| 3051 |
/** Builds a previous version of a clustered index record for a consistent read |
| 3052 |
@param[in] read_view read view |
| 3053 |
@param[in] clust_index clustered index |
| 3054 |
@param[in] prebuilt prebuilt struct |
| 3055 |
@param[in] rec record in clustered index |
| 3056 |
@param[in,out] offsets offsets returned by |
| 3057 |
rec_get_offsets(rec, clust_index) |
| 3058 |
@param[in,out] offset_heap memory heap from which the offsets are |
| 3059 |
allocated |
| 3060 |
@param[out] old_vers old version, or NULL if the record does not |
| 3061 |
exist in the view: i.e., it was freshly |
| 3062 |
inserted afterwards |
| 3063 |
@param[out] vrow dtuple to hold old virtual column data |
| 3064 |
@param[in] mtr the mini-transaction context. |
| 3065 |
@param[in,out] lob_undo Undo information for BLOBs. |
| 3066 |
@return DB_SUCCESS or error code */ |
| 3067 |
[[nodiscard]] static dberr_t row_sel_build_prev_vers_for_mysql( |
| 3068 |
ReadView *read_view, dict_index_t *clust_index, row_prebuilt_t *prebuilt, |
| 3069 |
const rec_t *rec, ulint **offsets, mem_heap_t **offset_heap, |
| 3070 |
rec_t **old_vers, const dtuple_t **vrow, mtr_t *mtr, |
| 3071 |
lob::undo_vers_t *lob_undo) { |
| 3072 |
DBUG_TRACE; |
| 3073 |
|
| 3074 |
dberr_t err; |
| 3075 |
|
| 3076 |
if (prebuilt->old_vers_heap) { |
| 3077 |
mem_heap_empty(prebuilt->old_vers_heap); |
| 3078 |
} else { |
| 3079 |
prebuilt->old_vers_heap = mem_heap_create(200, UT_LOCATION_HERE); |
| 3080 |
} |
| 3081 |
|
| 3082 |
err = row_vers_build_for_consistent_read( |
| 3083 |
rec, mtr, clust_index, offsets, read_view, offset_heap, |
| 3084 |
prebuilt->old_vers_heap, old_vers, vrow, lob_undo); |
| 3085 |
|
| 3086 |
return err; |
| 3087 |
} |
| 3088 |
|
| 3089 |
/** Helper class to cache clust_rec and old_ver */ |
| 3090 |
class Row_sel_get_clust_rec_for_mysql { |
| 3091 |
const rec_t *cached_clust_rec; |
| 3092 |
rec_t *cached_old_vers; |
| 3093 |
|
| 3094 |
public: |
| 3095 |
/** Constructor */ |
| 3096 |
Row_sel_get_clust_rec_for_mysql() |
| 3097 |
: cached_clust_rec(nullptr), cached_old_vers(nullptr) {} |
| 3098 |
|
| 3099 |
/** Retrieve the clustered index record corresponding to a record in a |
| 3100 |
non-clustered index. Does the necessary locking. |
| 3101 |
@param[in] prebuilt prebuilt struct in the handle |
| 3102 |
@param[in] sec_index secondary index where rec resides |
| 3103 |
@param[in] rec record in a non-clustered index |
| 3104 |
@param[in] thr query thread |
| 3105 |
@param[out] out_rec clustered record or an old version of it, |
| 3106 |
NULL if the old version did not exist in the |
| 3107 |
read view, i.e., it was a fresh inserted version |
| 3108 |
@param[in,out] offsets in: offsets returned by |
| 3109 |
rec_get_offsets(rec, sec_index); |
| 3110 |
out: offsets returned by |
| 3111 |
rec_get_offsets(out_rec, clust_index) |
| 3112 |
@param[in,out] offset_heap memory heap from which the offsets are allocated |
| 3113 |
@param[out] vrow virtual column to fill |
| 3114 |
@param[in] mtr mtr used to get access to the non-clustered record; |
| 3115 |
the same mtr is used to access the clustered index |
| 3116 |
@param[in] lob_undo the LOB undo information. |
| 3117 |
@return DB_SUCCESS, DB_SUCCESS_LOCKED_REC, or error code */ |
| 3118 |
dberr_t operator()(row_prebuilt_t *prebuilt, dict_index_t *sec_index, |
| 3119 |
const rec_t *rec, que_thr_t *thr, const rec_t **out_rec, |
| 3120 |
ulint **offsets, mem_heap_t **offset_heap, |
| 3121 |
const dtuple_t **vrow, mtr_t *mtr, |
| 3122 |
lob::undo_vers_t *lob_undo); |
| 3123 |
}; |
| 3124 |
|
| 3125 |
/** Retrieve the clustered index record corresponding to a record in a |
| 3126 |
non-clustered index. Does the necessary locking. |
| 3127 |
@return DB_SUCCESS, DB_SUCCESS_LOCKED_REC, or error code */ |
| 3128 |
|
| 3129 |
[[nodiscard]] dberr_t Row_sel_get_clust_rec_for_mysql::operator()( |
| 3130 |
row_prebuilt_t *prebuilt, dict_index_t *sec_index, const rec_t *rec, |
| 3131 |
que_thr_t *thr, const rec_t **out_rec, ulint **offsets, |
| 3132 |
mem_heap_t **offset_heap, const dtuple_t **vrow, mtr_t *mtr, |
| 3133 |
lob::undo_vers_t *lob_undo) { |
| 3134 |
DBUG_TRACE; |
| 3135 |
|
| 3136 |
dict_index_t *clust_index; |
| 3137 |
const rec_t *clust_rec; |
| 3138 |
rec_t *old_vers; |
| 3139 |
dberr_t err; |
| 3140 |
trx_t *trx; |
| 3141 |
|
| 3142 |
*out_rec = nullptr; |
| 3143 |
trx = thr_get_trx(thr); |
| 3144 |
|
| 3145 |
row_build_row_ref_in_tuple(prebuilt->clust_ref, rec, sec_index, *offsets); |
| 3146 |
|
| 3147 |
clust_index = sec_index->table->first_index(); |
| 3148 |
|
| 3149 |
prebuilt->clust_pcur->open_no_init(clust_index, prebuilt->clust_ref, |
| 3150 |
PAGE_CUR_LE, BTR_SEARCH_LEAF, 0, mtr, |
| 3151 |
UT_LOCATION_HERE); |
| 3152 |
|
| 3153 |
clust_rec = prebuilt->clust_pcur->get_rec(); |
| 3154 |
|
| 3155 |
prebuilt->clust_pcur->m_trx_if_known = trx; |
| 3156 |
|
| 3157 |
/* Note: only if the search ends up on a non-infimum record is the |
| 3158 |
low_match value the real match to the search tuple */ |
| 3159 |
|
| 3160 |
if (!page_rec_is_user_rec(clust_rec) || |
| 3161 |
prebuilt->clust_pcur->get_low_match() < |
| 3162 |
dict_index_get_n_unique(clust_index)) { |
| 3163 |
btr_cur_t *btr_cur = prebuilt->pcur->get_btr_cur(); |
| 3164 |
|
| 3165 |
/* If this is a spatial index scan, and we are reading |
| 3166 |
from a shadow buffer, the record could be already |
| 3167 |
deleted (due to rollback etc.). So get the original |
| 3168 |
page and verify that */ |
| 3169 |
if (dict_index_is_spatial(sec_index) && btr_cur->rtr_info->matches && |
| 3170 |
(page_align(rec) == btr_cur->rtr_info->matches->block.frame || |
| 3171 |
rec != prebuilt->pcur->get_rec())) { |
| 3172 |
#ifdef UNIV_DEBUG |
| 3173 |
rtr_info_t *rtr_info = btr_cur->rtr_info; |
| 3174 |
mutex_enter(&rtr_info->matches->rtr_match_mutex); |
| 3175 |
/* The page could be deallocated (by rollback etc.) */ |
| 3176 |
if (!rtr_info->matches->valid) { |
| 3177 |
mutex_exit(&rtr_info->matches->rtr_match_mutex); |
| 3178 |
clust_rec = nullptr; |
| 3179 |
|
| 3180 |
err = DB_SUCCESS; |
| 3181 |
goto func_exit; |
| 3182 |
} |
| 3183 |
mutex_exit(&rtr_info->matches->rtr_match_mutex); |
| 3184 |
|
| 3185 |
if (rec_get_deleted_flag(rec, dict_table_is_comp(sec_index->table)) && |
| 3186 |
prebuilt->select_lock_type == LOCK_NONE) { |
| 3187 |
clust_rec = nullptr; |
| 3188 |
|
| 3189 |
err = DB_SUCCESS; |
| 3190 |
goto func_exit; |
| 3191 |
} |
| 3192 |
|
| 3193 |
if (rec != prebuilt->pcur->get_rec()) { |
| 3194 |
clust_rec = nullptr; |
| 3195 |
|
| 3196 |
err = DB_SUCCESS; |
| 3197 |
goto func_exit; |
| 3198 |
} |
| 3199 |
|
| 3200 |
page_no_t page_no = page_get_page_no(prebuilt->pcur->get_page()); |
| 3201 |
|
| 3202 |
page_id_t page_id(dict_index_get_space(sec_index), page_no); |
| 3203 |
|
| 3204 |
buf_block_t *block = buf_page_get_gen( |
| 3205 |
page_id, dict_table_page_size(sec_index->table), RW_NO_LATCH, nullptr, |
| 3206 |
Page_fetch::NORMAL, UT_LOCATION_HERE, mtr); |
| 3207 |
|
| 3208 |
mem_heap_t *heap = mem_heap_create(256, UT_LOCATION_HERE); |
| 3209 |
dtuple_t *tuple = |
| 3210 |
dict_index_build_data_tuple(sec_index, const_cast<rec_t *>(rec), |
| 3211 |
dict_index_get_n_fields(sec_index), heap); |
| 3212 |
; |
| 3213 |
page_cur_t page_cursor; |
| 3214 |
|
| 3215 |
ulint low_match = |
| 3216 |
page_cur_search(block, sec_index, tuple, PAGE_CUR_LE, &page_cursor); |
| 3217 |
|
| 3218 |
ut_ad(low_match < dtuple_get_n_fields_cmp(tuple)); |
| 3219 |
mem_heap_free(heap); |
| 3220 |
clust_rec = nullptr; |
| 3221 |
|
| 3222 |
err = DB_SUCCESS; |
| 3223 |
goto func_exit; |
| 3224 |
#endif /* UNIV_DEBUG */ |
| 3225 |
} else if (!rec_get_deleted_flag(rec, |
| 3226 |
dict_table_is_comp(sec_index->table)) || |
| 3227 |
prebuilt->select_lock_type != LOCK_NONE) { |
| 3228 |
/* In a rare case it is possible that no clust |
| 3229 |
rec is found for a delete-marked secondary index |
| 3230 |
record: if in row0umod.cc in |
| 3231 |
row_undo_mod_remove_clust_low() we have already removed |
| 3232 |
the clust rec, while purge is still cleaning and |
| 3233 |
removing secondary index records associated with |
| 3234 |
earlier versions of the clustered index record. |
| 3235 |
In that case we know that the clustered index |
| 3236 |
record did not exist in the read view of trx. */ |
| 3237 |
ib::error(ER_IB_MSG_1030) |
| 3238 |
<< "Clustered record for sec rec not found" |
| 3239 |
" index " |
| 3240 |
<< sec_index->name << " of table " << sec_index->table->name; |
| 3241 |
|
| 3242 |
fputs("InnoDB: sec index record ", stderr); |
| 3243 |
rec_print(stderr, rec, sec_index); |
| 3244 |
fputs( |
| 3245 |
"\n" |
| 3246 |
"InnoDB: clust index record ", |
| 3247 |
stderr); |
| 3248 |
rec_print(stderr, clust_rec, clust_index); |
| 3249 |
putc('\n', stderr); |
| 3250 |
trx_print(stderr, trx, 600); |
| 3251 |
fputs( |
| 3252 |
"\n" |
| 3253 |
"InnoDB: Submit a detailed bug report" |
| 3254 |
" to http://bugs.mysql.com\n", |
| 3255 |
stderr); |
| 3256 |
ut_d(ut_error); |
| 3257 |
} |
| 3258 |
|
| 3259 |
clust_rec = nullptr; |
| 3260 |
|
| 3261 |
err = DB_SUCCESS; |
| 3262 |
goto func_exit; |
| 3263 |
} |
| 3264 |
|
| 3265 |
*offsets = rec_get_offsets(clust_rec, clust_index, *offsets, ULINT_UNDEFINED, |
| 3266 |
UT_LOCATION_HERE, offset_heap); |
| 3267 |
|
| 3268 |
if (prebuilt->select_lock_type != LOCK_NONE) { |
| 3269 |
/* Try to place a lock on the index record; we are searching |
| 3270 |
the clust rec with a unique condition, hence |
| 3271 |
we set a LOCK_REC_NOT_GAP type lock */ |
| 3272 |
|
| 3273 |
err = lock_clust_rec_read_check_and_lock( |
| 3274 |
lock_duration_t::REGULAR, prebuilt->clust_pcur->get_block(), clust_rec, |
| 3275 |
clust_index, *offsets, prebuilt->select_mode, |
| 3276 |
static_cast<lock_mode>(prebuilt->select_lock_type), LOCK_REC_NOT_GAP, |
| 3277 |
thr); |
| 3278 |
|
| 3279 |
switch (err) { |
| 3280 |
case DB_SUCCESS: |
| 3281 |
case DB_SUCCESS_LOCKED_REC: |
| 3282 |
break; |
| 3283 |
default: |
| 3284 |
goto err_exit; |
| 3285 |
} |
| 3286 |
} else { |
| 3287 |
/* This is a non-locking consistent read: if necessary, fetch |
| 3288 |
a previous version of the record */ |
| 3289 |
|
| 3290 |
old_vers = nullptr; |
| 3291 |
|
| 3292 |
/* If the isolation level allows reading of uncommitted data, |
| 3293 |
then we never look for an earlier version */ |
| 3294 |
|
| 3295 |
if (trx->isolation_level > TRX_ISO_READ_UNCOMMITTED && |
| 3296 |
!lock_clust_rec_cons_read_sees(clust_rec, clust_index, *offsets, |
| 3297 |
trx_get_read_view(trx))) { |
| 3298 |
if (clust_rec != cached_clust_rec) { |
| 3299 |
/* The following call returns 'offsets' associated with 'old_vers' */ |
| 3300 |
err = row_sel_build_prev_vers_for_mysql( |
| 3301 |
trx->read_view, clust_index, prebuilt, clust_rec, offsets, |
| 3302 |
offset_heap, &old_vers, vrow, mtr, lob_undo); |
| 3303 |
|
| 3304 |
if (err != DB_SUCCESS) { |
| 3305 |
goto err_exit; |
| 3306 |
} |
| 3307 |
cached_clust_rec = clust_rec; |
| 3308 |
cached_old_vers = old_vers; |
| 3309 |
} else { |
| 3310 |
err = DB_SUCCESS; |
| 3311 |
old_vers = cached_old_vers; |
| 3312 |
|
| 3313 |
if (old_vers != nullptr) { |
| 3314 |
DBUG_EXECUTE_IF("innodb_cached_old_vers_offsets", { |
| 3315 |
rec_offs_make_valid(old_vers, clust_index, *offsets); |
| 3316 |
if (!lob::rec_check_lobref_space_id(clust_index, old_vers, |
| 3317 |
*offsets)) { |
| 3318 |
DBUG_SUICIDE(); |
| 3319 |
} |
| 3320 |
}); |
| 3321 |
|
| 3322 |
/* The offsets need not be same for the latest version of |
| 3323 |
clust_rec and its old version old_vers. Re-calculate the offsets |
| 3324 |
for old_vers. */ |
| 3325 |
*offsets = |
| 3326 |
rec_get_offsets(old_vers, clust_index, *offsets, ULINT_UNDEFINED, |
| 3327 |
UT_LOCATION_HERE, offset_heap); |
| 3328 |
ut_ad( |
| 3329 |
lob::rec_check_lobref_space_id(clust_index, old_vers, *offsets)); |
| 3330 |
} |
| 3331 |
} |
| 3332 |
|
| 3333 |
if (old_vers == nullptr) { |
| 3334 |
goto err_exit; |
| 3335 |
} |
| 3336 |
|
| 3337 |
clust_rec = old_vers; |
| 3338 |
} |
| 3339 |
|
| 3340 |
/* If we had to go to an earlier version of row or the |
| 3341 |
secondary index record is delete marked, then it may be that |
| 3342 |
the secondary index record corresponding to clust_rec |
| 3343 |
(or old_vers) is not rec; in that case we must ignore |
| 3344 |
such row because in our snapshot rec would not have existed. |
| 3345 |
Remember that from rec we cannot see directly which transaction |
| 3346 |
id corresponds to it: we have to go to the clustered index |
| 3347 |
record. A query where we want to fetch all rows where |
| 3348 |
the secondary index value is in some interval would return |
| 3349 |
a wrong result if we would not drop rows which we come to |
| 3350 |
visit through secondary index records that would not really |
| 3351 |
exist in our snapshot. */ |
| 3352 |
|
| 3353 |
/* And for spatial index, since the rec is from shadow buffer, |
| 3354 |
so we need to check if it's exactly match the clust_rec. */ |
| 3355 |
if (clust_rec && |
| 3356 |
(old_vers || trx->isolation_level <= TRX_ISO_READ_UNCOMMITTED || |
| 3357 |
dict_index_is_spatial(sec_index) || |
| 3358 |
rec_get_deleted_flag(rec, dict_table_is_comp(sec_index->table)))) { |
| 3359 |
bool rec_equal; |
| 3360 |
|
| 3361 |
err = row_sel_sec_rec_is_for_clust_rec(rec, sec_index, clust_rec, |
| 3362 |
clust_index, thr, rec_equal); |
| 3363 |
if (err != DB_SUCCESS) { |
| 3364 |
goto err_exit; |
| 3365 |
} else if (!rec_equal) { |
| 3366 |
clust_rec = nullptr; |
| 3367 |
} |
| 3368 |
} |
| 3369 |
|
| 3370 |
err = DB_SUCCESS; |
| 3371 |
} |
| 3372 |
|
| 3373 |
func_exit: |
| 3374 |
*out_rec = clust_rec; |
| 3375 |
|
| 3376 |
/* Store the current position if select_lock_type is not |
| 3377 |
LOCK_NONE or if we are scanning using InnoDB APIs */ |
| 3378 |
if (prebuilt->select_lock_type != LOCK_NONE || prebuilt->innodb_api) { |
| 3379 |
/* We may use the cursor in update or in unlock_row(): |
| 3380 |
store its position */ |
| 3381 |
|
| 3382 |
prebuilt->clust_pcur->store_position(mtr); |
| 3383 |
} |
| 3384 |
|
| 3385 |
err_exit: |
| 3386 |
return err; |
| 3387 |
} |
| 3388 |
|
| 3389 |
/** Restores cursor position after it has been stored. We have to take into |
| 3390 |
account that the record cursor was positioned on may have been deleted. |
| 3391 |
Then we may have to move the cursor one step up or down. |
| 3392 |
@return true if we may need to process the record the cursor is now |
| 3393 |
positioned on (i.e. we should not go to the next record yet) */ |
| 3394 |
static bool sel_restore_position_for_mysql( |
| 3395 |
bool *same_user_rec, /*!< out: true if we were able to restore |
| 3396 |
the cursor on a user record with the |
| 3397 |
same ordering prefix in in the |
| 3398 |
B-tree index */ |
| 3399 |
ulint latch_mode, /*!< in: latch mode wished in |
| 3400 |
restoration */ |
| 3401 |
btr_pcur_t *pcur, /*!< in: cursor whose position |
| 3402 |
has been stored */ |
| 3403 |
bool moves_up, /*!< in: true if the cursor moves up |
| 3404 |
in the index */ |
| 3405 |
mtr_t *mtr) /*!< in: mtr; CAUTION: may commit |
| 3406 |
mtr temporarily! */ |
| 3407 |
{ |
| 3408 |
auto success = pcur->restore_position(latch_mode, mtr, UT_LOCATION_HERE); |
| 3409 |
|
| 3410 |
*same_user_rec = success; |
| 3411 |
|
| 3412 |
ut_ad(!success || pcur->m_rel_pos == BTR_PCUR_ON); |
| 3413 |
#ifdef UNIV_DEBUG |
| 3414 |
if (pcur->m_pos_state == BTR_PCUR_IS_POSITIONED_OPTIMISTIC) { |
| 3415 |
ut_ad(pcur->m_rel_pos == BTR_PCUR_BEFORE); |
| 3416 |
} else { |
| 3417 |
ut_ad(pcur->m_pos_state == BTR_PCUR_IS_POSITIONED); |
| 3418 |
ut_ad((pcur->m_rel_pos == BTR_PCUR_ON) == pcur->is_on_user_rec()); |
| 3419 |
} |
| 3420 |
#endif /* UNIV_DEBUG */ |
| 3421 |
|
| 3422 |
/* The position may need be adjusted for rel_pos and moves_up. */ |
| 3423 |
|
| 3424 |
switch (pcur->m_rel_pos) { |
| 3425 |
case BTR_PCUR_UNSET: |
| 3426 |
ut_d(ut_error); |
| 3427 |
ut_o(return (true)); |
| 3428 |
case BTR_PCUR_ON: |
| 3429 |
if (!success && moves_up) { |
| 3430 |
next: |
| 3431 |
pcur->move_to_next(mtr); |
| 3432 |
return true; |
| 3433 |
} |
| 3434 |
return (!success); |
| 3435 |
case BTR_PCUR_AFTER_LAST_IN_TREE: |
| 3436 |
case BTR_PCUR_BEFORE_FIRST_IN_TREE: |
| 3437 |
return true; |
| 3438 |
case BTR_PCUR_AFTER: |
| 3439 |
/* positioned to record after pcur->old_rec. */ |
| 3440 |
pcur->m_pos_state = BTR_PCUR_IS_POSITIONED; |
| 3441 |
prev: |
| 3442 |
if (pcur->is_on_user_rec() && !moves_up) { |
| 3443 |
pcur->move_to_prev(mtr); |
| 3444 |
} |
| 3445 |
return true; |
| 3446 |
case BTR_PCUR_BEFORE: |
| 3447 |
/* For non optimistic restoration: |
| 3448 |
The position is now set to the record before pcur->old_rec. |
| 3449 |
|
| 3450 |
For optimistic restoration: |
| 3451 |
The position also needs to take the previous search_mode into |
| 3452 |
consideration. */ |
| 3453 |
|
| 3454 |
switch (pcur->m_pos_state) { |
| 3455 |
case BTR_PCUR_IS_POSITIONED_OPTIMISTIC: |
| 3456 |
pcur->m_pos_state = BTR_PCUR_IS_POSITIONED; |
| 3457 |
if (pcur->m_search_mode == PAGE_CUR_GE) { |
| 3458 |
/* Positioned during Greater or Equal search |
| 3459 |
with BTR_PCUR_BEFORE. Optimistic restore to |
| 3460 |
the same record. If scanning for lower then |
| 3461 |
we must move to previous record. |
| 3462 |
This can happen with: |
| 3463 |
HANDLER READ idx a = (const); |
| 3464 |
HANDLER READ idx PREV; */ |
| 3465 |
goto prev; |
| 3466 |
} |
| 3467 |
return true; |
| 3468 |
case BTR_PCUR_IS_POSITIONED: |
| 3469 |
if (moves_up && pcur->is_on_user_rec()) { |
| 3470 |
goto next; |
| 3471 |
} |
| 3472 |
return true; |
| 3473 |
case BTR_PCUR_WAS_POSITIONED: |
| 3474 |
case BTR_PCUR_NOT_POSITIONED: |
| 3475 |
break; |
| 3476 |
} |
| 3477 |
} |
| 3478 |
ut_d(ut_error); |
| 3479 |
ut_o(return (true)); |
| 3480 |
} |
| 3481 |
|
| 3482 |
/** Copies a cached field for MySQL from the fetch cache. */ |
| 3483 |
static void row_sel_copy_cached_field_for_mysql( |
| 3484 |
byte *buf, /*!< in/out: row buffer */ |
| 3485 |
const byte *cache, /*!< in: cached row */ |
| 3486 |
const mysql_row_templ_t *templ) /*!< in: column template */ |
| 3487 |
{ |
| 3488 |
ut_a(!templ->is_multi_val); |
| 3489 |
|
| 3490 |
ulint len; |
| 3491 |
|
| 3492 |
buf += templ->mysql_col_offset; |
| 3493 |
cache += templ->mysql_col_offset; |
| 3494 |
|
| 3495 |
UNIV_MEM_ASSERT_W(buf, templ->mysql_col_len); |
| 3496 |
|
| 3497 |
if (templ->mysql_type == DATA_MYSQL_TRUE_VARCHAR && |
| 3498 |
(templ->type != DATA_INT)) { |
| 3499 |
/* Check for != DATA_INT to make sure we do |
| 3500 |
not treat MySQL ENUM or SET as a true VARCHAR! |
| 3501 |
Find the actual length of the true VARCHAR field. */ |
| 3502 |
row_mysql_read_true_varchar(&len, cache, templ->mysql_length_bytes); |
| 3503 |
len += templ->mysql_length_bytes; |
| 3504 |
UNIV_MEM_INVALID(buf, templ->mysql_col_len); |
| 3505 |
} else { |
| 3506 |
len = templ->mysql_col_len; |
| 3507 |
} |
| 3508 |
|
| 3509 |
/* The buf and cache have each reserved exactly templ->mysql_col_len |
| 3510 |
bytes for this column. In case of varchar we might copy fewer. */ |
| 3511 |
ut_a(len <= templ->mysql_col_len); |
| 3512 |
ut_memcpy(buf, cache, len); |
| 3513 |
} |
| 3514 |
|
| 3515 |
/** Copy used fields from cached row. |
| 3516 |
Copy cache record field by field, don't touch fields that |
| 3517 |
are not covered by current key. |
| 3518 |
@param[out] buf Where to copy the MySQL row. |
| 3519 |
@param[in] cached_rec What to copy (in MySQL row format). |
| 3520 |
@param[in] prebuilt prebuilt struct. */ |
| 3521 |
void row_sel_copy_cached_fields_for_mysql(byte *buf, const byte *cached_rec, |
| 3522 |
row_prebuilt_t *prebuilt) { |
| 3523 |
const mysql_row_templ_t *templ; |
| 3524 |
ulint i; |
| 3525 |
for (i = 0; i < prebuilt->n_template; i++) { |
| 3526 |
templ = prebuilt->mysql_template + i; |
| 3527 |
|
| 3528 |
/* Skip virtual columns */ |
| 3529 |
if (templ->is_virtual) { |
| 3530 |
continue; |
| 3531 |
} |
| 3532 |
|
| 3533 |
row_sel_copy_cached_field_for_mysql(buf, cached_rec, templ); |
| 3534 |
/* Copy NULL bit of the current field from cached_rec |
| 3535 |
to buf */ |
| 3536 |
if (templ->mysql_null_bit_mask) { |
| 3537 |
buf[templ->mysql_null_byte_offset] ^= |
| 3538 |
(buf[templ->mysql_null_byte_offset] ^ |
| 3539 |
cached_rec[templ->mysql_null_byte_offset]) & |
| 3540 |
(byte)templ->mysql_null_bit_mask; |
| 3541 |
} |
| 3542 |
} |
| 3543 |
} |
| 3544 |
|
| 3545 |
/** Get the record buffer provided by the server, if there is one. |
| 3546 |
@param prebuilt prebuilt struct |
| 3547 |
@return the record buffer, or nullptr if none was provided */ |
| 3548 |
static Record_buffer *row_sel_get_record_buffer( |
| 3549 |
const row_prebuilt_t *prebuilt) { |
| 3550 |
if (prebuilt->m_mysql_handler == nullptr) { |
| 3551 |
return nullptr; |
| 3552 |
} |
| 3553 |
return prebuilt->m_mysql_handler->ha_get_record_buffer(); |
| 3554 |
} |
| 3555 |
|
| 3556 |
/** Pops a cached row for MySQL from the fetch cache. */ |
| 3557 |
static inline void row_sel_dequeue_cached_row_for_mysql( |
| 3558 |
byte *buf, /*!< in/out: buffer where to copy the |
| 3559 |
row */ |
| 3560 |
row_prebuilt_t *prebuilt) /*!< in: prebuilt struct */ |
| 3561 |
{ |
| 3562 |
ulint i; |
| 3563 |
const mysql_row_templ_t *templ; |
| 3564 |
const byte *cached_rec; |
| 3565 |
ut_ad(prebuilt->n_fetch_cached > 0); |
| 3566 |
ut_ad(prebuilt->mysql_prefix_len <= prebuilt->mysql_row_len); |
| 3567 |
|
| 3568 |
UNIV_MEM_ASSERT_W(buf, prebuilt->mysql_row_len); |
| 3569 |
|
| 3570 |
/* The row is cached in the server-provided buffer, if there |
| 3571 |
is one. If not, get it from our own prefetch cache.*/ |
| 3572 |
const auto record_buffer = row_sel_get_record_buffer(prebuilt); |
| 3573 |
cached_rec = record_buffer |
| 3574 |
? record_buffer->record(prebuilt->fetch_cache_first) |
| 3575 |
: prebuilt->fetch_cache[prebuilt->fetch_cache_first]; |
| 3576 |
|
| 3577 |
if (UNIV_UNLIKELY(prebuilt->keep_other_fields_on_keyread)) { |
| 3578 |
row_sel_copy_cached_fields_for_mysql(buf, cached_rec, prebuilt); |
| 3579 |
} else if (prebuilt->mysql_prefix_len > 63) { |
| 3580 |
/* The record is long. Copy it field by field, in case |
| 3581 |
there are some long VARCHAR column of which only a |
| 3582 |
small length is being used. */ |
| 3583 |
UNIV_MEM_INVALID(buf, prebuilt->mysql_prefix_len); |
| 3584 |
|
| 3585 |
/* First copy the NULL bits. */ |
| 3586 |
ut_memcpy(buf, cached_rec, prebuilt->null_bitmap_len); |
| 3587 |
/* Then copy the requested fields. */ |
| 3588 |
|
| 3589 |
for (i = 0; i < prebuilt->n_template; i++) { |
| 3590 |
templ = prebuilt->mysql_template + i; |
| 3591 |
|
| 3592 |
/* Skip virtual columns */ |
| 3593 |
if (templ->is_virtual) { |
| 3594 |
if (!(dict_index_has_virtual(prebuilt->index) && |
| 3595 |
prebuilt->read_just_key)) { |
| 3596 |
continue; |
| 3597 |
} |
| 3598 |
|
| 3599 |
if (templ->is_multi_val) { |
| 3600 |
continue; |
| 3601 |
} |
| 3602 |
} |
| 3603 |
// Multi-value columns are always virtual |
| 3604 |
ut_a(!templ->is_multi_val); |
| 3605 |
|
| 3606 |
row_sel_copy_cached_field_for_mysql(buf, cached_rec, templ); |
| 3607 |
} |
| 3608 |
} else { |
| 3609 |
ut_memcpy(buf, cached_rec, prebuilt->mysql_prefix_len); |
| 3610 |
} |
| 3611 |
|
| 3612 |
prebuilt->n_fetch_cached--; |
| 3613 |
prebuilt->fetch_cache_first++; |
| 3614 |
|
| 3615 |
if (prebuilt->n_fetch_cached == 0) { |
| 3616 |
/* All the prefetched records have been returned. |
| 3617 |
Rewind so that we can insert records at the beginning |
| 3618 |
of the prefetch cache or record buffer. */ |
| 3619 |
prebuilt->fetch_cache_first = 0; |
| 3620 |
if (record_buffer != nullptr) { |
| 3621 |
record_buffer->clear(); |
| 3622 |
} |
| 3623 |
} |
| 3624 |
} |
| 3625 |
|
| 3626 |
/** Initialise the prefetch cache. */ |
| 3627 |
static inline void row_sel_prefetch_cache_init( |
| 3628 |
row_prebuilt_t *prebuilt) /*!< in/out: prebuilt struct */ |
| 3629 |
{ |
| 3630 |
ulint i; |
| 3631 |
ulint sz; |
| 3632 |
byte *ptr; |
| 3633 |
|
| 3634 |
/* We use our own prefetch cache only if the server didn't |
| 3635 |
provide one. */ |
| 3636 |
ut_ad(row_sel_get_record_buffer(prebuilt) == nullptr); |
| 3637 |
|
| 3638 |
/* Reserve space for the magic number. */ |
| 3639 |
sz = UT_ARR_SIZE(prebuilt->fetch_cache) * (prebuilt->mysql_row_len + 8); |
| 3640 |
ptr = static_cast<byte *>(ut::malloc_withkey(UT_NEW_THIS_FILE_PSI_KEY, sz)); |
| 3641 |
|
| 3642 |
for (i = 0; i < UT_ARR_SIZE(prebuilt->fetch_cache); i++) { |
| 3643 |
/* A user has reported memory corruption in these |
| 3644 |
buffers in Linux. Put magic numbers there to help |
| 3645 |
to track a possible bug. */ |
| 3646 |
|
| 3647 |
mach_write_to_4(ptr, ROW_PREBUILT_FETCH_MAGIC_N); |
| 3648 |
ptr += 4; |
| 3649 |
|
| 3650 |
prebuilt->fetch_cache[i] = ptr; |
| 3651 |
ptr += prebuilt->mysql_row_len; |
| 3652 |
|
| 3653 |
mach_write_to_4(ptr, ROW_PREBUILT_FETCH_MAGIC_N); |
| 3654 |
ptr += 4; |
| 3655 |
} |
| 3656 |
} |
| 3657 |
|
| 3658 |
/** Get the last fetch cache buffer from the queue. |
| 3659 |
@return pointer to buffer. */ |
| 3660 |
static inline byte *row_sel_fetch_last_buf( |
| 3661 |
row_prebuilt_t *prebuilt) /*!< in/out: prebuilt struct */ |
| 3662 |
{ |
| 3663 |
const auto record_buffer = row_sel_get_record_buffer(prebuilt); |
| 3664 |
|
| 3665 |
ut_ad(!prebuilt->templ_contains_blob); |
| 3666 |
if (record_buffer == nullptr) { |
| 3667 |
ut_ad(prebuilt->n_fetch_cached < MYSQL_FETCH_CACHE_SIZE); |
| 3668 |
} else { |
| 3669 |
ut_ad(prebuilt->mysql_prefix_len <= record_buffer->record_size()); |
| 3670 |
ut_ad(record_buffer->records() == prebuilt->n_fetch_cached); |
| 3671 |
} |
| 3672 |
|
| 3673 |
if (record_buffer == nullptr && prebuilt->fetch_cache[0] == nullptr) { |
| 3674 |
/* Allocate memory for the fetch cache */ |
| 3675 |
ut_ad(prebuilt->n_fetch_cached == 0); |
| 3676 |
|
| 3677 |
row_sel_prefetch_cache_init(prebuilt); |
| 3678 |
} |
| 3679 |
|
| 3680 |
/* Use the server-provided buffer if there is one. Otherwise, |
| 3681 |
use our own prefetch buffer. */ |
| 3682 |
byte *buf = record_buffer ? record_buffer->add_record() |
| 3683 |
: prebuilt->fetch_cache[prebuilt->n_fetch_cached]; |
| 3684 |
|
| 3685 |
ut_ad(prebuilt->fetch_cache_first == 0); |
| 3686 |
UNIV_MEM_INVALID(buf, record_buffer ? record_buffer->record_size() |
| 3687 |
: prebuilt->mysql_row_len); |
| 3688 |
|
| 3689 |
return (buf); |
| 3690 |
} |
| 3691 |
|
| 3692 |
/** Pushes a row for MySQL to the fetch cache. */ |
| 3693 |
static inline void row_sel_enqueue_cache_row_for_mysql( |
| 3694 |
byte *mysql_rec, /*!< in/out: MySQL record */ |
| 3695 |
row_prebuilt_t *prebuilt) /*!< in/out: prebuilt struct */ |
| 3696 |
{ |
| 3697 |
/* For non ICP code path the row should already exist in the |
| 3698 |
next fetch cache slot. */ |
| 3699 |
|
| 3700 |
if (prebuilt->idx_cond) { |
| 3701 |
byte *dest = row_sel_fetch_last_buf(prebuilt); |
| 3702 |
|
| 3703 |
ut_memcpy(dest, mysql_rec, prebuilt->mysql_prefix_len); |
| 3704 |
} |
| 3705 |
|
| 3706 |
++prebuilt->n_fetch_cached; |
| 3707 |
} |
| 3708 |
|
| 3709 |
/** Tries to do a shortcut to fetch a clustered index record with a unique key, |
| 3710 |
using the hash index if possible (not always). We assume that the search |
| 3711 |
mode is PAGE_CUR_GE, it is a consistent read, there is a read view in trx, |
| 3712 |
btr search latch has been locked in S-mode if AHI is enabled. |
| 3713 |
@return SEL_FOUND, SEL_EXHAUSTED, SEL_RETRY */ |
| 3714 |
static ulint row_sel_try_search_shortcut_for_mysql( |
| 3715 |
const rec_t **out_rec, /*!< out: record if found */ |
| 3716 |
row_prebuilt_t *prebuilt, /*!< in: prebuilt struct */ |
| 3717 |
ulint **offsets, /*!< in/out: for rec_get_offsets(*out_rec) */ |
| 3718 |
mem_heap_t **heap, /*!< in/out: heap for rec_get_offsets() */ |
| 3719 |
mtr_t *mtr) /*!< in: started mtr */ |
| 3720 |
{ |
| 3721 |
dict_index_t *index = prebuilt->index; |
| 3722 |
const dtuple_t *search_tuple = prebuilt->search_tuple; |
| 3723 |
btr_pcur_t *pcur = prebuilt->pcur; |
| 3724 |
trx_t *trx = prebuilt->trx; |
| 3725 |
const rec_t *rec; |
| 3726 |
|
| 3727 |
ut_ad(index->is_clustered()); |
| 3728 |
ut_ad(!prebuilt->templ_contains_blob); |
| 3729 |
|
| 3730 |
pcur->open_no_init(index, search_tuple, PAGE_CUR_GE, BTR_SEARCH_LEAF, |
| 3731 |
(trx->has_search_latch) ? RW_S_LATCH : 0, mtr, |
| 3732 |
UT_LOCATION_HERE); |
| 3733 |
rec = pcur->get_rec(); |
| 3734 |
|
| 3735 |
if (!page_rec_is_user_rec(rec)) { |
| 3736 |
return (SEL_RETRY); |
| 3737 |
} |
| 3738 |
|
| 3739 |
/* As the cursor is now placed on a user record after a search with |
| 3740 |
the mode PAGE_CUR_GE, the up_match field in the cursor tells how many |
| 3741 |
fields in the user record matched to the search tuple */ |
| 3742 |
|
| 3743 |
if (pcur->get_up_match() < dtuple_get_n_fields(search_tuple)) { |
| 3744 |
return (SEL_EXHAUSTED); |
| 3745 |
} |
| 3746 |
|
| 3747 |
/* This is a non-locking consistent read: if necessary, fetch |
| 3748 |
a previous version of the record */ |
| 3749 |
|
| 3750 |
*offsets = rec_get_offsets(rec, index, *offsets, ULINT_UNDEFINED, |
| 3751 |
UT_LOCATION_HERE, heap); |
| 3752 |
|
| 3753 |
if (!lock_clust_rec_cons_read_sees(rec, index, *offsets, |
| 3754 |
trx_get_read_view(trx))) { |
| 3755 |
return (SEL_RETRY); |
| 3756 |
} |
| 3757 |
|
| 3758 |
if (rec_get_deleted_flag(rec, dict_table_is_comp(index->table))) { |
| 3759 |
return (SEL_EXHAUSTED); |
| 3760 |
} |
| 3761 |
|
| 3762 |
*out_rec = rec; |
| 3763 |
|
| 3764 |
return (SEL_FOUND); |
| 3765 |
} |
| 3766 |
|
| 3767 |
/** Check a pushed-down index condition. |
| 3768 |
@return ICP_NO_MATCH, ICP_MATCH, or ICP_OUT_OF_RANGE */ |
| 3769 |
static ICP_RESULT row_search_idx_cond_check( |
| 3770 |
byte *mysql_rec, /*!< out: record |
| 3771 |
in MySQL format (invalid unless |
| 3772 |
prebuilt->idx_cond == true and |
| 3773 |
we return ICP_MATCH) */ |
| 3774 |
row_prebuilt_t *prebuilt, /*!< in/out: prebuilt struct |
| 3775 |
for the table handle */ |
| 3776 |
const rec_t *rec, /*!< in: InnoDB record */ |
| 3777 |
const ulint *offsets) /*!< in: rec_get_offsets() */ |
| 3778 |
{ |
| 3779 |
ICP_RESULT result; |
| 3780 |
ulint i; |
| 3781 |
|
| 3782 |
ut_ad(rec_offs_validate(rec, prebuilt->index, offsets)); |
| 3783 |
|
| 3784 |
if (!prebuilt->idx_cond) { |
| 3785 |
return (ICP_MATCH); |
| 3786 |
} |
| 3787 |
|
| 3788 |
MONITOR_INC(MONITOR_ICP_ATTEMPTS); |
| 3789 |
|
| 3790 |
/* Convert to MySQL format those fields that are needed for |
| 3791 |
evaluating the index condition. */ |
| 3792 |
|
| 3793 |
if (prebuilt->blob_heap != nullptr) { |
| 3794 |
mem_heap_empty(prebuilt->blob_heap); |
| 3795 |
} |
| 3796 |
|
| 3797 |
for (i = 0; i < prebuilt->idx_cond_n_cols; i++) { |
| 3798 |
const mysql_row_templ_t *templ = &prebuilt->mysql_template[i]; |
| 3799 |
|
| 3800 |
/* Skip virtual columns */ |
| 3801 |
if (templ->is_virtual) { |
| 3802 |
continue; |
| 3803 |
} |
| 3804 |
|
| 3805 |
if (!row_sel_store_mysql_field( |
| 3806 |
mysql_rec, prebuilt, rec, prebuilt->index, prebuilt->index, offsets, |
| 3807 |
templ->icp_rec_field_no, templ, ULINT_UNDEFINED, nullptr, |
| 3808 |
prebuilt->blob_heap)) { |
| 3809 |
return (ICP_NO_MATCH); |
| 3810 |
} |
| 3811 |
} |
| 3812 |
|
| 3813 |
/* We assume that the index conditions on |
| 3814 |
case-insensitive columns are case-insensitive. The |
| 3815 |
case of such columns may be wrong in a secondary |
| 3816 |
index, if the case of the column has been updated in |
| 3817 |
the past, or a record has been deleted and a record |
| 3818 |
inserted in a different case. */ |
| 3819 |
result = innobase_index_cond(prebuilt->m_mysql_handler); |
| 3820 |
switch (result) { |
| 3821 |
case ICP_MATCH: |
| 3822 |
/* Convert the remaining fields to MySQL format. |
| 3823 |
If this is a secondary index record, we must defer |
| 3824 |
this until we have fetched the clustered index record. */ |
| 3825 |
if (!prebuilt->need_to_access_clustered || |
| 3826 |
prebuilt->index->is_clustered()) { |
| 3827 |
if (!row_sel_store_mysql_rec(mysql_rec, prebuilt, rec, nullptr, false, |
| 3828 |
prebuilt->index, prebuilt->index, offsets, |
| 3829 |
false, nullptr, prebuilt->blob_heap)) { |
| 3830 |
ut_ad(prebuilt->index->is_clustered()); |
| 3831 |
return (ICP_NO_MATCH); |
| 3832 |
} |
| 3833 |
} |
| 3834 |
MONITOR_INC(MONITOR_ICP_MATCH); |
| 3835 |
return (result); |
| 3836 |
case ICP_NO_MATCH: |
| 3837 |
MONITOR_INC(MONITOR_ICP_NO_MATCH); |
| 3838 |
return (result); |
| 3839 |
case ICP_OUT_OF_RANGE: |
| 3840 |
MONITOR_INC(MONITOR_ICP_OUT_OF_RANGE); |
| 3841 |
const auto record_buffer = row_sel_get_record_buffer(prebuilt); |
| 3842 |
if (record_buffer) { |
| 3843 |
record_buffer->set_out_of_range(true); |
| 3844 |
} |
| 3845 |
return (result); |
| 3846 |
} |
| 3847 |
|
| 3848 |
ut_error; |
| 3849 |
} |
| 3850 |
|
| 3851 |
/** Check the pushed-down end-range condition to avoid extra traversal |
| 3852 |
if records are not with in view and also to avoid prefetching too |
| 3853 |
many records into the record buffer. |
| 3854 |
@param[in] mysql_rec record in MySQL format |
| 3855 |
@param[in] rec InnoDB record |
| 3856 |
@param[in] prebuilt prebuilt struct |
| 3857 |
@param[in] clust_templ_for_sec true if \a rec belongs to the secondary |
| 3858 |
index but the \a prebuilt template is in |
| 3859 |
clustered index format |
| 3860 |
@param[in] offsets information about column offsets in the |
| 3861 |
secondary index, if virtual columns need |
| 3862 |
to be copied into \a mysql_rec |
| 3863 |
@param[in,out] record_buffer the record buffer we are reading into, |
| 3864 |
or \c nullptr if there is no buffer |
| 3865 |
@retval true if the row in \a mysql_rec is out of range |
| 3866 |
@retval false if the row in \a mysql_rec is in range */ |
| 3867 |
static bool row_search_end_range_check(byte *mysql_rec, const rec_t *rec, |
| 3868 |
row_prebuilt_t *prebuilt, |
| 3869 |
bool clust_templ_for_sec, |
| 3870 |
const ulint *offsets, |
| 3871 |
Record_buffer *record_buffer) { |
| 3872 |
const auto handler = prebuilt->m_mysql_handler; |
| 3873 |
ut_ad(handler->end_range != nullptr); |
| 3874 |
|
| 3875 |
/* When reading from non-covering secondary indexes, mysql_rec won't |
| 3876 |
have the values of virtual columns until the handler has called |
| 3877 |
update_generated_read_fields(). If the end-range condition refers to a |
| 3878 |
virtual column, we may have to copy its value from the secondary index |
| 3879 |
before evaluating the condition. */ |
| 3880 |
if (clust_templ_for_sec && handler->m_virt_gcol_in_end_range) { |
| 3881 |
ut_ad(offsets != nullptr); |
| 3882 |
for (ulint i = 0; i < prebuilt->n_template; ++i) { |
| 3883 |
const auto &templ = prebuilt->mysql_template[i]; |
| 3884 |
|
| 3885 |
if (templ.is_virtual && templ.icp_rec_field_no != ULINT_UNDEFINED) { |
| 3886 |
ut_a(!templ.is_multi_val); |
| 3887 |
bool stored = row_sel_store_mysql_field( |
| 3888 |
mysql_rec, prebuilt, rec, prebuilt->index, prebuilt->index, offsets, |
| 3889 |
templ.icp_rec_field_no, &templ, ULINT_UNDEFINED, nullptr, |
| 3890 |
prebuilt->blob_heap); |
| 3891 |
/* The only reason row_sel_store_mysql_field might return false |
| 3892 |
is when it encounters an externally stored value (blob). However |
| 3893 |
such values can't be fields of secondary indexes. */ |
| 3894 |
ut_ad(stored); |
| 3895 |
ut_o(if (!stored) return false); |
| 3896 |
} |
| 3897 |
} |
| 3898 |
} |
| 3899 |
|
| 3900 |
if (handler->compare_key_in_buffer(mysql_rec) > 0) { |
| 3901 |
if (record_buffer != nullptr) { |
| 3902 |
record_buffer->set_out_of_range(true); |
| 3903 |
} |
| 3904 |
|
| 3905 |
return true; |
| 3906 |
} |
| 3907 |
|
| 3908 |
return false; |
| 3909 |
} |
| 3910 |
|
| 3911 |
/** Traverse to next/previous record. |
| 3912 |
@param[in] moves_up If true, move to next record else previous |
| 3913 |
@param[in] match_mode 0 or ROW_SEL_EXACT or ROW_SEL_EXACT_PREFIX |
| 3914 |
@param[in,out] pcur Cursor to record |
| 3915 |
@param[in] mtr Mini-transaction |
| 3916 |
|
| 3917 |
@return DB_SUCCESS or error code */ |
| 3918 |
static dberr_t row_search_traverse(bool moves_up, ulint match_mode, |
| 3919 |
btr_pcur_t *pcur, mtr_t *mtr) { |
| 3920 |
dberr_t err = DB_SUCCESS; |
| 3921 |
|
| 3922 |
if (moves_up) { |
| 3923 |
if (!pcur->move_to_next(mtr)) { |
| 3924 |
err = (match_mode != 0) ? DB_RECORD_NOT_FOUND : DB_END_OF_INDEX; |
| 3925 |
return (err); |
| 3926 |
} |
| 3927 |
} else { |
| 3928 |
if (!pcur->move_to_prev(mtr)) { |
| 3929 |
err = (match_mode != 0) ? DB_RECORD_NOT_FOUND : DB_END_OF_INDEX; |
| 3930 |
return (err); |
| 3931 |
} |
| 3932 |
} |
| 3933 |
|
| 3934 |
return (err); |
| 3935 |
} |
| 3936 |
|
| 3937 |
/** Searches for rows in the database using cursor. |
| 3938 |
Function is for temporary tables that are not shared across connections |
| 3939 |
and so lot of complexity is reduced especially locking and transaction related. |
| 3940 |
The cursor is an iterator over the table/index. |
| 3941 |
|
| 3942 |
@param[out] buf buffer for the fetched row in MySQL format |
| 3943 |
@param[in] mode search mode PAGE_CUR_L |
| 3944 |
@param[in,out] prebuilt prebuilt struct for the table handler; |
| 3945 |
this contains the info to search_tuple, |
| 3946 |
index; if search tuple contains 0 field then |
| 3947 |
we position the cursor at start or the end of |
| 3948 |
index, depending on 'mode' |
| 3949 |
@param[in] match_mode 0 or ROW_SEL_EXACT or ROW_SEL_EXACT_PREFIX |
| 3950 |
@param[in] direction 0 or ROW_SEL_NEXT or ROW_SEL_PREV; |
| 3951 |
Note: if this is != 0, then prebuilt must has a |
| 3952 |
pcur with stored position! In opening of a |
| 3953 |
cursor 'direction' should be 0. |
| 3954 |
@return DB_SUCCESS or error code */ |
| 3955 |
dberr_t row_search_no_mvcc(byte *buf, page_cur_mode_t mode, |
| 3956 |
row_prebuilt_t *prebuilt, ulint match_mode, |
| 3957 |
ulint direction) { |
| 3958 |
dict_index_t *index = prebuilt->index; |
| 3959 |
ut_ad(index->table->is_intrinsic()); |
| 3960 |
|
| 3961 |
const dtuple_t *search_tuple = prebuilt->search_tuple; |
| 3962 |
btr_pcur_t *pcur = prebuilt->pcur; |
| 3963 |
Row_sel_get_clust_rec_for_mysql row_sel_get_clust_rec_for_mysql; |
| 3964 |
|
| 3965 |
const rec_t *result_rec = nullptr; |
| 3966 |
const rec_t *clust_rec = nullptr; |
| 3967 |
|
| 3968 |
dberr_t err = DB_SUCCESS; |
| 3969 |
|
| 3970 |
mem_heap_t *heap = nullptr; |
| 3971 |
ulint offsets_[REC_OFFS_NORMAL_SIZE]; |
| 3972 |
ulint *offsets = offsets_; |
| 3973 |
rec_offs_init(offsets_); |
| 3974 |
ut_ad(index && pcur && search_tuple); |
| 3975 |
|
| 3976 |
/* Step-0: Re-use the cached mtr. */ |
| 3977 |
mtr_t *mtr; |
| 3978 |
dict_index_t *clust_index = index->table->first_index(); |
| 3979 |
|
| 3980 |
if (!index->last_sel_cur) { |
| 3981 |
dict_allocate_mem_intrinsic_cache(index); |
| 3982 |
} |
| 3983 |
|
| 3984 |
mtr = &index->last_sel_cur->mtr; |
| 3985 |
|
| 3986 |
/* Step-1: Build the select graph. */ |
| 3987 |
if (direction == 0 && prebuilt->sel_graph == nullptr) { |
| 3988 |
row_prebuild_sel_graph(prebuilt); |
| 3989 |
} |
| 3990 |
|
| 3991 |
que_thr_t *thr = que_fork_get_first_thr(prebuilt->sel_graph); |
| 3992 |
|
| 3993 |
bool moves_up; |
| 3994 |
|
| 3995 |
if (direction == 0) { |
| 3996 |
if (mode == PAGE_CUR_GE || mode == PAGE_CUR_G) { |
| 3997 |
moves_up = true; |
| 3998 |
} else { |
| 3999 |
moves_up = false; |
| 4000 |
} |
| 4001 |
|
| 4002 |
} else if (direction == ROW_SEL_NEXT) { |
| 4003 |
moves_up = true; |
| 4004 |
} else { |
| 4005 |
moves_up = false; |
| 4006 |
} |
| 4007 |
|
| 4008 |
/* Step-2: Open or Restore the cursor. |
| 4009 |
If search key is specified, cursor is open using the key else |
| 4010 |
cursor is open to return all the records. */ |
| 4011 |
if (direction != 0) { |
| 4012 |
if (prebuilt->m_temp_read_shared && !prebuilt->m_temp_tree_modified) { |
| 4013 |
if (!mtr->is_active()) { |
| 4014 |
mtr_start(mtr); |
| 4015 |
|
| 4016 |
if (!pcur->m_block_when_stored.run_with_hint([&](buf_block_t *hint) { |
| 4017 |
return hint != nullptr && |
| 4018 |
buf_page_optimistic_get( |
| 4019 |
RW_NO_LATCH, hint, pcur->m_modify_clock, |
| 4020 |
Page_fetch::NORMAL, __FILE__, __LINE__, mtr); |
| 4021 |
})) { |
| 4022 |
/* block was relocated */ |
| 4023 |
goto block_relocated; |
| 4024 |
} |
| 4025 |
} |
| 4026 |
|
| 4027 |
/* This is an intrinsic table shared read, so we |
| 4028 |
do not rely on index->last_sel_cur, instead we rely |
| 4029 |
on "prebuilt->pcur", which supposes to position on |
| 4030 |
last read position for each read session. */ |
| 4031 |
ut_ad(pcur->m_pos_state == BTR_PCUR_IS_POSITIONED); |
| 4032 |
err = row_search_traverse(moves_up, match_mode, pcur, mtr); |
| 4033 |
|
| 4034 |
if (err != DB_SUCCESS) { |
| 4035 |
return (err); /* purecov: inspected */ |
| 4036 |
} |
| 4037 |
|
| 4038 |
} else if (index->last_sel_cur->invalid || prebuilt->m_temp_tree_modified) { |
| 4039 |
block_relocated: |
| 4040 |
/* Index tree has changed and so active cached cursor is no more valid. |
| 4041 |
Re-set it based on the last selected position. */ |
| 4042 |
index->last_sel_cur->release(); |
| 4043 |
prebuilt->m_temp_tree_modified = false; |
| 4044 |
|
| 4045 |
if (direction == ROW_SEL_NEXT && pcur->m_search_mode == PAGE_CUR_GE) { |
| 4046 |
pcur->m_search_mode = PAGE_CUR_G; |
| 4047 |
} |
| 4048 |
|
| 4049 |
mtr_start(mtr); |
| 4050 |
mtr_set_log_mode(mtr, MTR_LOG_NO_REDO); |
| 4051 |
|
| 4052 |
mem_heap_t *heap = mem_heap_create(256, UT_LOCATION_HERE); |
| 4053 |
|
| 4054 |
dtuple_t *tuple = dict_index_build_data_tuple(index, pcur->m_old_rec, |
| 4055 |
pcur->m_old_n_fields, heap); |
| 4056 |
|
| 4057 |
pcur->open_no_init(index, tuple, pcur->m_search_mode, BTR_SEARCH_LEAF, 0, |
| 4058 |
mtr, UT_LOCATION_HERE); |
| 4059 |
|
| 4060 |
mem_heap_free(heap); |
| 4061 |
} else { |
| 4062 |
/* Restore the cursor for reading next record from cache |
| 4063 |
information. */ |
| 4064 |
ut_ad(index->last_sel_cur->rec != nullptr); |
| 4065 |
pcur->m_btr_cur.page_cur.rec = index->last_sel_cur->rec; |
| 4066 |
pcur->m_btr_cur.page_cur.block = index->last_sel_cur->block; |
| 4067 |
|
| 4068 |
err = row_search_traverse(moves_up, match_mode, pcur, mtr); |
| 4069 |
if (err != DB_SUCCESS) { |
| 4070 |
return (err); |
| 4071 |
} |
| 4072 |
} |
| 4073 |
} else { |
| 4074 |
/* There could be previous uncommitted transaction if SELECT |
| 4075 |
is operation as part of SELECT (IF NOT FOUND) INSERT |
| 4076 |
(IF DUPLICATE) UPDATE plan. */ |
| 4077 |
index->last_sel_cur->release(); |
| 4078 |
|
| 4079 |
/* Capture table snapshot in form of trx-id. */ |
| 4080 |
index->trx_id = dict_table_get_curr_table_sess_trx_id(index->table); |
| 4081 |
|
| 4082 |
/* Fresh search commences. */ |
| 4083 |
mtr_start(mtr); |
| 4084 |
dict_disable_redo_if_temporary(index->table, mtr); |
| 4085 |
|
| 4086 |
if (dtuple_get_n_fields(search_tuple) > 0) { |
| 4087 |
pcur->open_no_init(index, search_tuple, mode, BTR_SEARCH_LEAF, 0, mtr, |
| 4088 |
UT_LOCATION_HERE); |
| 4089 |
|
| 4090 |
} else if (mode == PAGE_CUR_G || mode == PAGE_CUR_L) { |
| 4091 |
pcur->open_at_side(mode == PAGE_CUR_G, index, BTR_SEARCH_LEAF, false, 0, |
| 4092 |
mtr); |
| 4093 |
} |
| 4094 |
} |
| 4095 |
|
| 4096 |
/* Step-3: Traverse the records filtering non-qualifying records. */ |
| 4097 |
for (/* No op */; err == DB_SUCCESS; |
| 4098 |
err = row_search_traverse(moves_up, match_mode, pcur, mtr)) { |
| 4099 |
const rec_t *rec = pcur->get_rec(); |
| 4100 |
|
| 4101 |
if (page_rec_is_infimum(rec) || page_rec_is_supremum(rec) || |
| 4102 |
rec_get_deleted_flag(rec, dict_table_is_comp(index->table))) { |
| 4103 |
/* The infimum record on a page cannot be in the |
| 4104 |
result set, and neither can a record lock be placed on |
| 4105 |
it: we skip such a record. */ |
| 4106 |
continue; |
| 4107 |
} |
| 4108 |
|
| 4109 |
offsets = rec_get_offsets(rec, index, offsets, ULINT_UNDEFINED, |
| 4110 |
UT_LOCATION_HERE, &heap); |
| 4111 |
|
| 4112 |
/* Note that we cannot trust the up_match value in the cursor |
| 4113 |
at this place because we can arrive here after moving the |
| 4114 |
cursor! Thus we have to recompare rec and search_tuple to |
| 4115 |
determine if they match enough. */ |
| 4116 |
if (match_mode == ROW_SEL_EXACT) { |
| 4117 |
/* Test if the index record matches completely to |
| 4118 |
search_tuple in prebuilt: if not, then we return with |
| 4119 |
DB_RECORD_NOT_FOUND */ |
| 4120 |
if (0 != cmp_dtuple_rec(search_tuple, rec, index, offsets)) { |
| 4121 |
err = DB_RECORD_NOT_FOUND; |
| 4122 |
break; |
| 4123 |
} |
| 4124 |
} else if (match_mode == ROW_SEL_EXACT_PREFIX) { |
| 4125 |
if (!cmp_dtuple_is_prefix_of_rec(search_tuple, rec, index, offsets)) { |
| 4126 |
err = DB_RECORD_NOT_FOUND; |
| 4127 |
break; |
| 4128 |
} |
| 4129 |
} |
| 4130 |
|
| 4131 |
/* Get the clustered index. We always need clustered index |
| 4132 |
record for snapshort verification. */ |
| 4133 |
if (index != clust_index) { |
| 4134 |
err = row_sel_get_clust_rec_for_mysql(prebuilt, index, rec, thr, |
| 4135 |
&clust_rec, &offsets, &heap, |
| 4136 |
nullptr, mtr, nullptr); |
| 4137 |
|
| 4138 |
if (err != DB_SUCCESS) { |
| 4139 |
break; |
| 4140 |
} |
| 4141 |
|
| 4142 |
if (rec_get_deleted_flag(clust_rec, dict_table_is_comp(index->table))) { |
| 4143 |
/* The record is delete marked in clustered |
| 4144 |
index. We can skip this record. */ |
| 4145 |
continue; |
| 4146 |
} |
| 4147 |
|
| 4148 |
result_rec = clust_rec; |
| 4149 |
} else { |
| 4150 |
result_rec = rec; |
| 4151 |
} |
| 4152 |
|
| 4153 |
/* Step-4: Cache the row-id of selected row to prebuilt cache.*/ |
| 4154 |
if (prebuilt->clust_index_was_generated) { |
| 4155 |
row_sel_store_row_id_to_prebuilt(prebuilt, result_rec, clust_index, |
| 4156 |
offsets); |
| 4157 |
} |
| 4158 |
|
| 4159 |
/* Step-5: Convert selected record to MySQL format and |
| 4160 |
store it. */ |
| 4161 |
if (prebuilt->template_type == ROW_MYSQL_DUMMY_TEMPLATE) { |
| 4162 |
const rec_t *ret_rec = |
| 4163 |
(index != clust_index && prebuilt->need_to_access_clustered) |
| 4164 |
? result_rec |
| 4165 |
: rec; |
| 4166 |
|
| 4167 |
offsets = rec_get_offsets(ret_rec, index, offsets, ULINT_UNDEFINED, |
| 4168 |
UT_LOCATION_HERE, &heap); |
| 4169 |
|
| 4170 |
memcpy(buf + 4, ret_rec - rec_offs_extra_size(offsets), |
| 4171 |
rec_offs_size(offsets)); |
| 4172 |
|
| 4173 |
mach_write_to_4(buf, rec_offs_extra_size(offsets) + 4); |
| 4174 |
|
| 4175 |
} else if (!row_sel_store_mysql_rec(buf, prebuilt, result_rec, nullptr, |
| 4176 |
true, clust_index, prebuilt->index, |
| 4177 |
offsets, false, nullptr, |
| 4178 |
prebuilt->blob_heap)) { |
| 4179 |
err = DB_ERROR; |
| 4180 |
break; |
| 4181 |
} |
| 4182 |
|
| 4183 |
/* Step-6: Store cursor position to fetch next record. |
| 4184 |
MySQL calls this function iteratively get_next(), get_next() |
| 4185 |
fashion. */ |
| 4186 |
ut_ad(err == DB_SUCCESS); |
| 4187 |
index->last_sel_cur->rec = pcur->get_rec(); |
| 4188 |
index->last_sel_cur->block = pcur->get_block(); |
| 4189 |
|
| 4190 |
/* This is needed in order to restore the cursor if index |
| 4191 |
structure changes while SELECT is still active. */ |
| 4192 |
pcur->m_old_rec = dict_index_copy_rec_order_prefix( |
| 4193 |
index, rec, &pcur->m_old_n_fields, &pcur->m_old_rec_buf, |
| 4194 |
&pcur->m_buf_size); |
| 4195 |
|
| 4196 |
pcur->m_block_when_stored.store(pcur->get_block()); |
| 4197 |
pcur->m_modify_clock = pcur->get_block()->get_modify_clock(IF_DEBUG(true)); |
| 4198 |
|
| 4199 |
break; |
| 4200 |
} |
| 4201 |
|
| 4202 |
if (err != DB_SUCCESS) { |
| 4203 |
index->last_sel_cur->release(); |
| 4204 |
} |
| 4205 |
|
| 4206 |
if (heap != nullptr) { |
| 4207 |
mem_heap_free(heap); |
| 4208 |
} |
| 4209 |
return (err); |
| 4210 |
} |
| 4211 |
|
| 4212 |
/** Extract virtual column data from a virtual index record and fill a dtuple |
| 4213 |
@param[in] rec the virtual (secondary) index record |
| 4214 |
@param[in] index the virtual index |
| 4215 |
@param[in,out] vrow the dtuple where data extract to |
| 4216 |
@param[in] heap memory heap to allocate memory |
| 4217 |
*/ |
| 4218 |
static void row_sel_fill_vrow(const rec_t *rec, dict_index_t *index, |
| 4219 |
const dtuple_t **vrow, mem_heap_t *heap) { |
| 4220 |
ulint offsets_[REC_OFFS_NORMAL_SIZE]; |
| 4221 |
ulint *offsets = offsets_; |
| 4222 |
rec_offs_init(offsets_); |
| 4223 |
|
| 4224 |
ut_ad(!(*vrow)); |
| 4225 |
|
| 4226 |
offsets = rec_get_offsets(rec, index, offsets, ULINT_UNDEFINED, |
| 4227 |
UT_LOCATION_HERE, &heap); |
| 4228 |
|
| 4229 |
*vrow = |
| 4230 |
dtuple_create_with_vcol(heap, 0, dict_table_get_n_v_cols(index->table)); |
| 4231 |
|
| 4232 |
/* Initialize all virtual row's mtype to DATA_MISSING */ |
| 4233 |
dtuple_init_v_fld(*vrow); |
| 4234 |
|
| 4235 |
for (ulint i = 0; i < dict_index_get_n_fields(index); i++) { |
| 4236 |
const dict_field_t *field; |
| 4237 |
const dict_col_t *col; |
| 4238 |
|
| 4239 |
field = index->get_field(i); |
| 4240 |
col = field->col; |
| 4241 |
|
| 4242 |
if (col->is_virtual()) { |
| 4243 |
const byte *data; |
| 4244 |
ulint len; |
| 4245 |
|
| 4246 |
data = rec_get_nth_field(index, rec, offsets, i, &len); |
| 4247 |
|
| 4248 |
const dict_v_col_t *vcol = reinterpret_cast<const dict_v_col_t *>(col); |
| 4249 |
|
| 4250 |
dfield_t *dfield = dtuple_get_nth_v_field(*vrow, vcol->v_pos); |
| 4251 |
dfield_set_data(dfield, data, len); |
| 4252 |
col->copy_type(dfield_get_type(dfield)); |
| 4253 |
} |
| 4254 |
} |
| 4255 |
} |
| 4256 |
/** The return type of row_compare_row_to_range() which summarizes information |
| 4257 |
about the relation between the row being processed, and the range of the scan */ |
| 4258 |
struct row_to_range_relation_t { |
| 4259 |
/** true: we don't know, false: row is not in range */ |
| 4260 |
bool row_can_be_in_range; |
| 4261 |
/** true: we don't know, false: gap has nothing in common with range */ |
| 4262 |
bool gap_can_intersect_range; |
| 4263 |
/** true: row exactly matches end of range, false: we don't know */ |
| 4264 |
bool row_must_be_at_end; |
| 4265 |
}; |
| 4266 |
|
| 4267 |
/** A helper function extracted from row_search_mvcc() which compares the row |
| 4268 |
being processed with the range of the scan. |
| 4269 |
It does not modify any of it's arguments and returns a summary of situation. |
| 4270 |
All the arguments are named the same way as local variables at place of call, |
| 4271 |
and have same values. */ |
| 4272 |
static row_to_range_relation_t row_compare_row_to_range( |
| 4273 |
const bool set_also_gap_locks, const trx_t *const trx, |
| 4274 |
const bool unique_search, const dict_index_t *const index, |
| 4275 |
const dict_index_t *const clust_index, const rec_t *const rec, |
| 4276 |
const bool comp, const page_cur_mode_t mode, const ulint direction, |
| 4277 |
const dtuple_t *search_tuple, const ulint *const offsets, |
| 4278 |
const bool moves_up, const row_prebuilt_t *const prebuilt) { |
| 4279 |
row_to_range_relation_t row_to_range_relation; |
| 4280 |
row_to_range_relation.row_can_be_in_range = true; |
| 4281 |
row_to_range_relation.gap_can_intersect_range = true; |
| 4282 |
row_to_range_relation.row_must_be_at_end = false; |
| 4283 |
|
| 4284 |
/* We don't know how to compare row which is on supremum with range, but this |
| 4285 |
should not be a problem because row_search_mvcc "skips" over them without |
| 4286 |
calling our function */ |
| 4287 |
ut_ad(!page_rec_is_supremum(rec)); |
| 4288 |
if (page_rec_is_supremum(rec)) { |
| 4289 |
return (row_to_range_relation); |
| 4290 |
} |
| 4291 |
|
| 4292 |
/* Try to place a lock on the index record; note that delete |
| 4293 |
marked records are a special case in a unique search. If there |
| 4294 |
is a non-delete marked record, then it is enough to lock its |
| 4295 |
existence with LOCK_REC_NOT_GAP. */ |
| 4296 |
|
| 4297 |
/* If we are doing a 'greater or equal than a primary key |
| 4298 |
value' search from a clustered index, and we find a record |
| 4299 |
that has that exact primary key value, then there is no need |
| 4300 |
to lock the gap before the record, because no insert in the |
| 4301 |
gap can be in our search range. That is, no phantom row can |
| 4302 |
appear that way. |
| 4303 |
|
| 4304 |
An example: if col1 is the primary key, the search is WHERE |
| 4305 |
col1 >= 100, and we find a record where col1 = 100, then no |
| 4306 |
need to lock the gap before that record. */ |
| 4307 |
|
| 4308 |
if (!set_also_gap_locks || trx->skip_gap_locks() || |
| 4309 |
(unique_search && !rec_get_deleted_flag(rec, comp)) || |
| 4310 |
dict_index_is_spatial(index) || |
| 4311 |
(index == clust_index && mode == PAGE_CUR_GE && direction == 0 && |
| 4312 |
dtuple_get_n_fields_cmp(search_tuple) == |
| 4313 |
dict_index_get_n_unique(index) && |
| 4314 |
0 == cmp_dtuple_rec(search_tuple, rec, index, offsets))) { |
| 4315 |
row_to_range_relation.gap_can_intersect_range = false; |
| 4316 |
return (row_to_range_relation); |
| 4317 |
} |
| 4318 |
|
| 4319 |
/* We don't know how to handle HANDLER interface */ |
| 4320 |
if (prebuilt->used_in_HANDLER) { |
| 4321 |
return (row_to_range_relation); |
| 4322 |
} |
| 4323 |
|
| 4324 |
/* While I believe that we handle semi-consistent reads correctly, the proof |
| 4325 |
is quite complicated and lingers on the fact that semi-consistent reads are |
| 4326 |
used only if we don't use gap locks. And fortunately, we've already checked |
| 4327 |
above that trx->skip_gap_locks() is false, so we don't have to go through the |
| 4328 |
whole reasoning about what exactly happens in case the row which is at the |
| 4329 |
end of the range got locked by another transaction, removed, purged, and while |
| 4330 |
we were doing semi-consistent read on it. */ |
| 4331 |
ut_ad(!trx->skip_gap_locks()); |
| 4332 |
ut_ad(prebuilt->row_read_type == ROW_READ_WITH_LOCKS); |
| 4333 |
|
| 4334 |
/* Following heuristics are meant to avoid locking the row itself, or even |
| 4335 |
the gap before it, in case when the row is "after the end of range". The |
| 4336 |
difficulty here is in that the index itself can be declared as either |
| 4337 |
ascending or descending, separately for each column, and cursor can be |
| 4338 |
PAGE_CUR_G(E) or PAGE_CUR_L(E) etc., and direction of scan can be 0, |
| 4339 |
ROW_SEL_NEXT or ROW_SEL_PREV, and this might be a secondary index (with |
| 4340 |
duplicates). So we limit ourselves just to the cases, which are at the |
| 4341 |
same common, tested, actionable and easy to reason about. |
| 4342 |
In particular we only handle cases where we iterate the index in its |
| 4343 |
natural order. */ |
| 4344 |
if (index == clust_index && (mode == PAGE_CUR_GE || mode == PAGE_CUR_G) && |
| 4345 |
(direction == 0 || direction == ROW_SEL_NEXT) && |
| 4346 |
prebuilt->is_reading_range()) { |
| 4347 |
ut_ad(moves_up); |
| 4348 |
const auto stop_len = dtuple_get_n_fields_cmp(prebuilt->m_stop_tuple); |
| 4349 |
if (0 < stop_len) { |
| 4350 |
const auto index_len = dict_index_get_n_unique(index); |
| 4351 |
ut_ad(prebuilt->m_mysql_handler->end_range != nullptr); |
| 4352 |
if (stop_len <= index_len) { |
| 4353 |
const auto cmp = |
| 4354 |
cmp_dtuple_rec(prebuilt->m_stop_tuple, rec, index, offsets); |
| 4355 |
|
| 4356 |
if (cmp < 0) { |
| 4357 |
row_to_range_relation.row_can_be_in_range = false; |
| 4358 |
if (prebuilt->m_stop_tuple_found) { |
| 4359 |
ut_ad(stop_len == index_len); |
| 4360 |
row_to_range_relation.gap_can_intersect_range = false; |
| 4361 |
return (row_to_range_relation); |
| 4362 |
} |
| 4363 |
return (row_to_range_relation); |
| 4364 |
} |
| 4365 |
|
| 4366 |
if (cmp == 0) { |
| 4367 |
ut_ad(!prebuilt->m_stop_tuple_found); |
| 4368 |
row_to_range_relation.row_can_be_in_range = |
| 4369 |
prebuilt->m_mysql_handler->end_range->flag != HA_READ_BEFORE_KEY; |
| 4370 |
row_to_range_relation.row_must_be_at_end = stop_len == index_len; |
| 4371 |
return (row_to_range_relation); |
| 4372 |
} |
| 4373 |
} |
| 4374 |
} |
| 4375 |
} |
| 4376 |
return (row_to_range_relation); |
| 4377 |
} |
| 4378 |
|
| 4379 |
#ifdef UNIV_DEBUG |
| 4380 |
/** If the record is not old version, copies an initial segment |
| 4381 |
of a physical record to be compared later for debug assertion code. |
| 4382 |
@param[in] pcur cursor whose position has been stored |
| 4383 |
@param[in] index index |
| 4384 |
@param[in] rec record for which to copy prefix |
| 4385 |
@param[out] n_fields number of fields copied |
| 4386 |
@param[in,out] buf memory buffer for the copied prefix, or nullptr |
| 4387 |
@param[in,out] buf_size buffer size |
| 4388 |
@return pointer to the prefix record if not old version. or nullptr if old */ |
| 4389 |
static inline rec_t *row_search_debug_copy_rec_order_prefix( |
| 4390 |
const btr_pcur_t *pcur, const dict_index_t *index, const rec_t *rec, |
| 4391 |
ulint *n_fields, byte **buf, size_t *buf_size) { |
| 4392 |
if (pcur->get_rec() == rec) { |
| 4393 |
return dict_index_copy_rec_order_prefix(index, rec, n_fields, buf, |
| 4394 |
buf_size); |
| 4395 |
} else { |
| 4396 |
return nullptr; |
| 4397 |
} |
| 4398 |
} |
| 4399 |
#endif /* UNIV_DEBUG */ |
| 4400 |
|
| 4401 |
/** Searches for rows in the database using cursor. |
| 4402 |
Function is mainly used for tables that are shared accorss connection and |
| 4403 |
so it employs technique that can help re-construct the rows that |
| 4404 |
transaction is suppose to see. |
| 4405 |
It also has optimization such as pre-caching the rows, using AHI, etc. |
| 4406 |
|
| 4407 |
@param[out] buf buffer for the fetched row in MySQL format |
| 4408 |
@param[in] mode search mode PAGE_CUR_L |
| 4409 |
@param[in,out] prebuilt prebuilt struct for the table handler; |
| 4410 |
this contains the info to search_tuple, |
| 4411 |
index; if search tuple contains 0 field then |
| 4412 |
we position the cursor at start or the end of |
| 4413 |
index, depending on 'mode' |
| 4414 |
@param[in] match_mode 0 or ROW_SEL_EXACT or ROW_SEL_EXACT_PREFIX |
| 4415 |
@param[in] direction 0 or ROW_SEL_NEXT or ROW_SEL_PREV; |
| 4416 |
Note: if this is != 0, then prebuilt must has a |
| 4417 |
pcur with stored position! In opening of a |
| 4418 |
cursor 'direction' should be 0. |
| 4419 |
@return DB_SUCCESS or error code */ |
| 4420 |
dberr_t row_search_mvcc(byte *buf, page_cur_mode_t mode, |
| 4421 |
row_prebuilt_t *prebuilt, ulint match_mode, |
| 4422 |
const ulint direction) { |
| 4423 |
DBUG_TRACE; |
| 4424 |
|
| 4425 |
dict_index_t *index = prebuilt->index; |
| 4426 |
bool comp = dict_table_is_comp(index->table); |
| 4427 |
const dtuple_t *search_tuple = prebuilt->search_tuple; |
| 4428 |
btr_pcur_t *pcur = prebuilt->pcur; |
| 4429 |
trx_t *trx = prebuilt->trx; |
| 4430 |
dict_index_t *clust_index; |
| 4431 |
/* True if we are scanning a secondary index, but the template is based |
| 4432 |
on the primary index. */ |
| 4433 |
bool clust_templ_for_sec; |
| 4434 |
que_thr_t *thr; |
| 4435 |
const rec_t *prev_rec = nullptr; |
| 4436 |
#ifdef UNIV_DEBUG |
| 4437 |
const rec_t *prev_rec_debug = nullptr; |
| 4438 |
ulint prev_rec_debug_n_fields = 0; |
| 4439 |
byte *prev_rec_debug_buf = nullptr; |
| 4440 |
size_t prev_rec_debug_buf_size = 0; |
| 4441 |
#endif /* UNIV_DEBUG */ |
| 4442 |
const rec_t *rec = nullptr; |
| 4443 |
byte *end_range_cache = nullptr; |
| 4444 |
const dtuple_t *prev_vrow = nullptr; |
| 4445 |
const dtuple_t *vrow = nullptr; |
| 4446 |
const rec_t *result_rec = nullptr; |
| 4447 |
const rec_t *clust_rec; |
| 4448 |
Row_sel_get_clust_rec_for_mysql row_sel_get_clust_rec_for_mysql; |
| 4449 |
dberr_t err = DB_SUCCESS; |
| 4450 |
bool unique_search = false; |
| 4451 |
bool mtr_has_extra_clust_latch = false; |
| 4452 |
bool moves_up = false; |
| 4453 |
bool set_also_gap_locks = true; |
| 4454 |
/* if the query is a plain locking SELECT, and the isolation level |
| 4455 |
is <= TRX_ISO_READ_COMMITTED, then this is set to false */ |
| 4456 |
bool did_semi_consistent_read = false; |
| 4457 |
/* if the returned record was locked and we did a semi-consistent |
| 4458 |
read (fetch the newest committed version), then this is set to |
| 4459 |
true */ |
| 4460 |
ulint next_offs; |
| 4461 |
bool same_user_rec = false; |
| 4462 |
mtr_t mtr; |
| 4463 |
mem_heap_t *heap = nullptr; |
| 4464 |
ulint offsets_[REC_OFFS_NORMAL_SIZE]; |
| 4465 |
ulint *offsets = offsets_; |
| 4466 |
ulint sec_offsets_[REC_OFFS_NORMAL_SIZE]; |
| 4467 |
ulint *sec_offsets = nullptr; |
| 4468 |
bool table_lock_waited = false; |
| 4469 |
byte *next_buf = nullptr; |
| 4470 |
bool spatial_search = false; |
| 4471 |
ulint end_loop = 0; |
| 4472 |
|
| 4473 |
rec_offs_init(offsets_); |
| 4474 |
|
| 4475 |
ut_ad(index && pcur && search_tuple); |
| 4476 |
ut_a(prebuilt->magic_n == ROW_PREBUILT_ALLOCATED); |
| 4477 |
ut_a(prebuilt->magic_n2 == ROW_PREBUILT_ALLOCATED); |
| 4478 |
ut_a(!trx->has_search_latch); |
| 4479 |
|
| 4480 |
/* We don't support FTS queries from the HANDLER interfaces, because |
| 4481 |
we implemented FTS as reversed inverted index with auxiliary tables. |
| 4482 |
So anything related to traditional index query would not apply to |
| 4483 |
it. */ |
| 4484 |
if (prebuilt->index->type & DICT_FTS) { |
| 4485 |
return DB_END_OF_INDEX; |
| 4486 |
} |
| 4487 |
|
| 4488 |
#ifdef UNIV_DEBUG |
| 4489 |
{ |
| 4490 |
btrsea_sync_check check(trx->has_search_latch); |
| 4491 |
ut_ad(!sync_check_iterate(check)); |
| 4492 |
} |
| 4493 |
#endif /* UNIV_DEBUG */ |
| 4494 |
|
| 4495 |
if (dict_table_is_discarded(prebuilt->table)) { |
| 4496 |
return DB_TABLESPACE_DELETED; |
| 4497 |
|
| 4498 |
} else if (prebuilt->table->ibd_file_missing) { |
| 4499 |
return DB_TABLESPACE_NOT_FOUND; |
| 4500 |
|
| 4501 |
} else if (!prebuilt->index_usable) { |
| 4502 |
return DB_MISSING_HISTORY; |
| 4503 |
|
| 4504 |
} else if (prebuilt->index->is_corrupted()) { |
| 4505 |
return DB_CORRUPTION; |
| 4506 |
} |
| 4507 |
|
| 4508 |
/* We need to get the virtual column values stored in secondary |
| 4509 |
index key, if this is covered index scan or virtual key read is |
| 4510 |
requested. */ |
| 4511 |
bool need_vrow = dict_index_has_virtual(prebuilt->index) && |
| 4512 |
(prebuilt->read_just_key || prebuilt->m_read_virtual_key); |
| 4513 |
|
| 4514 |
/* Reset the new record lock info. |
| 4515 |
Then we are able to remove the record locks set here on an |
| 4516 |
individual row. */ |
| 4517 |
prebuilt->new_rec_lock.reset(); |
| 4518 |
/*-------------------------------------------------------------*/ |
| 4519 |
/* PHASE 1: Try to pop the row from the record buffer or from |
| 4520 |
the prefetch cache */ |
| 4521 |
|
| 4522 |
const auto record_buffer = row_sel_get_record_buffer(prebuilt); |
| 4523 |
|
| 4524 |
if (UNIV_UNLIKELY(direction == 0)) { |
| 4525 |
trx->op_info = "starting index read"; |
| 4526 |
|
| 4527 |
prebuilt->n_rows_fetched = 0; |
| 4528 |
prebuilt->n_fetch_cached = 0; |
| 4529 |
prebuilt->fetch_cache_first = 0; |
| 4530 |
prebuilt->m_end_range = false; |
| 4531 |
if (record_buffer != nullptr) { |
| 4532 |
record_buffer->reset(); |
| 4533 |
} |
| 4534 |
|
| 4535 |
if (prebuilt->sel_graph == nullptr) { |
| 4536 |
/* Build a dummy select query graph */ |
| 4537 |
row_prebuild_sel_graph(prebuilt); |
| 4538 |
} |
| 4539 |
} else { |
| 4540 |
trx->op_info = "fetching rows"; |
| 4541 |
|
| 4542 |
if (prebuilt->n_rows_fetched == 0) { |
| 4543 |
prebuilt->fetch_direction = direction; |
| 4544 |
} |
| 4545 |
|
| 4546 |
if (UNIV_UNLIKELY(direction != prebuilt->fetch_direction)) { |
| 4547 |
if (UNIV_UNLIKELY(prebuilt->n_fetch_cached > 0)) { |
| 4548 |
ut_error; |
| 4549 |
/* TODO: scrollable cursor: restore cursor to |
| 4550 |
the place of the latest returned row, |
| 4551 |
or better: prevent caching for a scroll |
| 4552 |
cursor! */ |
| 4553 |
} |
| 4554 |
|
| 4555 |
prebuilt->n_rows_fetched = 0; |
| 4556 |
prebuilt->n_fetch_cached = 0; |
| 4557 |
prebuilt->fetch_cache_first = 0; |
| 4558 |
prebuilt->m_end_range = false; |
| 4559 |
|
| 4560 |
/* A record buffer is not used for scroll cursors. |
| 4561 |
Otherwise, it would have to be reset here too. */ |
| 4562 |
ut_ad(record_buffer == nullptr); |
| 4563 |
|
| 4564 |
} else if (UNIV_LIKELY(prebuilt->n_fetch_cached > 0)) { |
| 4565 |
row_sel_dequeue_cached_row_for_mysql(buf, prebuilt); |
| 4566 |
|
| 4567 |
prebuilt->n_rows_fetched++; |
| 4568 |
|
| 4569 |
err = DB_SUCCESS; |
| 4570 |
goto func_exit; |
| 4571 |
} else if (prebuilt->m_end_range) { |
| 4572 |
err = DB_RECORD_NOT_FOUND; |
| 4573 |
goto func_exit; |
| 4574 |
} |
| 4575 |
|
| 4576 |
/* The prefetch cache is exhausted, so fetch_cache_first |
| 4577 |
should point to the beginning of the cache. */ |
| 4578 |
ut_ad(prebuilt->fetch_cache_first == 0); |
| 4579 |
|
| 4580 |
if (record_buffer != nullptr && record_buffer->is_out_of_range()) { |
| 4581 |
/* The previous returned row was popped from |
| 4582 |
the fetch cache, but the end of the range was |
| 4583 |
reached while filling the cache, so there are |
| 4584 |
no more rows to put into the cache. */ |
| 4585 |
|
| 4586 |
err = DB_RECORD_NOT_FOUND; |
| 4587 |
goto func_exit; |
| 4588 |
} |
| 4589 |
|
| 4590 |
prebuilt->n_rows_fetched++; |
| 4591 |
|
| 4592 |
if (prebuilt->n_rows_fetched > 1000000000) { |
| 4593 |
/* Prevent wrap-over */ |
| 4594 |
prebuilt->n_rows_fetched = 500000000; |
| 4595 |
} |
| 4596 |
|
| 4597 |
mode = pcur->m_search_mode; |
| 4598 |
} |
| 4599 |
|
| 4600 |
/* In a search where at most one record in the index may match, we |
| 4601 |
can use a LOCK_REC_NOT_GAP type record lock when locking a |
| 4602 |
non-delete-marked matching record. |
| 4603 |
|
| 4604 |
Note that in a unique secondary index there may be different |
| 4605 |
delete-marked versions of a record where only the primary key |
| 4606 |
values differ: thus in a secondary index we must use next-key |
| 4607 |
locks when locking delete-marked records. */ |
| 4608 |
|
| 4609 |
if (match_mode == ROW_SEL_EXACT && dict_index_is_unique(index) && |
| 4610 |
dtuple_get_n_fields(search_tuple) == dict_index_get_n_unique(index) && |
| 4611 |
(index->is_clustered() || !dtuple_contains_null(search_tuple))) { |
| 4612 |
/* Note above that a UNIQUE secondary index can contain many |
| 4613 |
rows with the same key value if one of the columns is the SQL |
| 4614 |
null. A clustered index under MySQL can never contain null |
| 4615 |
columns because we demand that all the columns in primary key |
| 4616 |
are non-null. */ |
| 4617 |
|
| 4618 |
unique_search = true; |
| 4619 |
|
| 4620 |
/* Even if the condition is unique, MySQL seems to try to |
| 4621 |
retrieve also a second row if a primary key contains more than |
| 4622 |
1 column. Return immediately if this is not a HANDLER |
| 4623 |
command. */ |
| 4624 |
|
| 4625 |
if (UNIV_UNLIKELY(direction != 0 && !prebuilt->used_in_HANDLER)) { |
| 4626 |
err = DB_RECORD_NOT_FOUND; |
| 4627 |
goto func_exit; |
| 4628 |
} |
| 4629 |
} |
| 4630 |
|
| 4631 |
/* We don't support sequential scan for Rtree index, because it |
| 4632 |
is no meaning to do so. */ |
| 4633 |
if (dict_index_is_spatial(index) && !RTREE_SEARCH_MODE(mode)) { |
| 4634 |
err = DB_END_OF_INDEX; |
| 4635 |
goto func_exit; |
| 4636 |
} |
| 4637 |
|
| 4638 |
mtr_start(&mtr); |
| 4639 |
|
| 4640 |
/*-------------------------------------------------------------*/ |
| 4641 |
/* PHASE 2: Try fast adaptive hash index search if possible */ |
| 4642 |
|
| 4643 |
/* Next test if this is the special case where we can use the fast |
| 4644 |
adaptive hash index to try the search. Since we must release the |
| 4645 |
search system latch when we retrieve an externally stored field, we |
| 4646 |
cannot use the adaptive hash index in a search in the case the row |
| 4647 |
may be long and there may be externally stored fields */ |
| 4648 |
|
| 4649 |
if (UNIV_UNLIKELY(direction == 0) && unique_search && btr_search_enabled && |
| 4650 |
index->is_clustered() && !prebuilt->templ_contains_blob && |
| 4651 |
!prebuilt->used_in_HANDLER && |
| 4652 |
(prebuilt->mysql_row_len < UNIV_PAGE_SIZE / 8) && !prebuilt->innodb_api) { |
| 4653 |
mode = PAGE_CUR_GE; |
| 4654 |
|
| 4655 |
if (trx->mysql_n_tables_locked == 0 && !prebuilt->ins_sel_stmt && |
| 4656 |
prebuilt->select_lock_type == LOCK_NONE && |
| 4657 |
trx->isolation_level > TRX_ISO_READ_UNCOMMITTED && |
| 4658 |
MVCC::is_view_active(trx->read_view)) { |
| 4659 |
/* This is a SELECT query done as a consistent read, |
| 4660 |
and the read view has already been allocated: |
| 4661 |
let us try a search shortcut through the hash |
| 4662 |
index. |
| 4663 |
NOTE that we must also test that |
| 4664 |
mysql_n_tables_locked == 0, because this might |
| 4665 |
also be INSERT INTO ... SELECT ... or |
| 4666 |
CREATE TABLE ... SELECT ... . Our algorithm is |
| 4667 |
NOT prepared to inserts interleaved with the SELECT, |
| 4668 |
and if we try that, we can deadlock on the adaptive |
| 4669 |
hash index semaphore! */ |
| 4670 |
|
| 4671 |
ut_a(!trx->has_search_latch); |
| 4672 |
rw_lock_s_lock(btr_get_search_latch(index), UT_LOCATION_HERE); |
| 4673 |
trx->has_search_latch = true; |
| 4674 |
|
| 4675 |
switch (row_sel_try_search_shortcut_for_mysql(&rec, prebuilt, &offsets, |
| 4676 |
&heap, &mtr)) { |
| 4677 |
case SEL_FOUND: |
| 4678 |
/* At this point, rec is protected by |
| 4679 |
a page latch that was acquired by |
| 4680 |
row_sel_try_search_shortcut_for_mysql(). |
| 4681 |
The latch will not be released until |
| 4682 |
mtr_commit(&mtr). */ |
| 4683 |
ut_ad(!rec_get_deleted_flag(rec, comp)); |
| 4684 |
|
| 4685 |
if (prebuilt->idx_cond) { |
| 4686 |
switch (row_search_idx_cond_check(buf, prebuilt, rec, offsets)) { |
| 4687 |
case ICP_NO_MATCH: |
| 4688 |
case ICP_OUT_OF_RANGE: |
| 4689 |
goto shortcut_mismatch; |
| 4690 |
case ICP_MATCH: |
| 4691 |
goto shortcut_match; |
| 4692 |
} |
| 4693 |
} |
| 4694 |
|
| 4695 |
if (!row_sel_store_mysql_rec(buf, prebuilt, rec, nullptr, false, |
| 4696 |
index, prebuilt->index, offsets, false, |
| 4697 |
nullptr, prebuilt->blob_heap)) { |
| 4698 |
/* Only fresh inserts may contain |
| 4699 |
incomplete externally stored |
| 4700 |
columns. Pretend that such |
| 4701 |
records do not exist. Such |
| 4702 |
records may only be accessed |
| 4703 |
at the READ UNCOMMITTED |
| 4704 |
isolation level or when |
| 4705 |
rolling back a recovered |
| 4706 |
transaction. Rollback happens |
| 4707 |
at a lower level, not here. */ |
| 4708 |
|
| 4709 |
/* Proceed as in case SEL_RETRY. */ |
| 4710 |
break; |
| 4711 |
} |
| 4712 |
|
| 4713 |
shortcut_match: |
| 4714 |
mtr_commit(&mtr); |
| 4715 |
|
| 4716 |
/* NOTE that we do NOT store the cursor |
| 4717 |
position */ |
| 4718 |
|
| 4719 |
err = DB_SUCCESS; |
| 4720 |
|
| 4721 |
rw_lock_s_unlock(btr_get_search_latch(index)); |
| 4722 |
trx->has_search_latch = false; |
| 4723 |
|
| 4724 |
goto func_exit; |
| 4725 |
|
| 4726 |
case SEL_EXHAUSTED: |
| 4727 |
shortcut_mismatch: |
| 4728 |
mtr_commit(&mtr); |
| 4729 |
|
| 4730 |
err = DB_RECORD_NOT_FOUND; |
| 4731 |
|
| 4732 |
rw_lock_s_unlock(btr_get_search_latch(index)); |
| 4733 |
trx->has_search_latch = false; |
| 4734 |
|
| 4735 |
/* NOTE that we do NOT store the cursor |
| 4736 |
position */ |
| 4737 |
|
| 4738 |
goto func_exit; |
| 4739 |
|
| 4740 |
case SEL_RETRY: |
| 4741 |
break; |
| 4742 |
|
| 4743 |
default: |
| 4744 |
ut_d(ut_error); |
| 4745 |
} |
| 4746 |
|
| 4747 |
mtr_commit(&mtr); |
| 4748 |
mtr_start(&mtr); |
| 4749 |
|
| 4750 |
rw_lock_s_unlock(btr_get_search_latch(index)); |
| 4751 |
trx->has_search_latch = false; |
| 4752 |
} |
| 4753 |
} |
| 4754 |
|
| 4755 |
/*-------------------------------------------------------------*/ |
| 4756 |
/* PHASE 3: Open or restore index cursor position */ |
| 4757 |
|
| 4758 |
spatial_search = dict_index_is_spatial(index) && mode >= PAGE_CUR_CONTAIN; |
| 4759 |
|
| 4760 |
/* The state of a running trx can only be changed by the |
| 4761 |
thread that is currently serving the transaction. Because we |
| 4762 |
are that thread, we can read trx->state without holding any |
| 4763 |
mutex. */ |
| 4764 |
|
| 4765 |
ut_ad(prebuilt->sql_stat_start || |
| 4766 |
trx->state.load(std::memory_order_relaxed) == TRX_STATE_ACTIVE); |
| 4767 |
|
| 4768 |
ut_ad(!trx_is_started(trx) || |
| 4769 |
trx->state.load(std::memory_order_relaxed) == TRX_STATE_ACTIVE); |
| 4770 |
|
| 4771 |
ut_ad(prebuilt->sql_stat_start || prebuilt->select_lock_type != LOCK_NONE || |
| 4772 |
MVCC::is_view_active(trx->read_view) || srv_read_only_mode); |
| 4773 |
|
| 4774 |
trx_start_if_not_started(trx, false, UT_LOCATION_HERE); |
| 4775 |
|
| 4776 |
if (prebuilt->table->skip_gap_locks() || |
| 4777 |
(trx->skip_gap_locks() && prebuilt->select_lock_type != LOCK_NONE && |
| 4778 |
trx->mysql_thd != nullptr && thd_is_query_block(trx->mysql_thd))) { |
| 4779 |
/* It is a plain locking SELECT and the isolation |
| 4780 |
level is low: do not lock gaps */ |
| 4781 |
|
| 4782 |
/* Reads on DD tables dont require gap-locks as serializability |
| 4783 |
between different DDL statements is achieved using |
| 4784 |
metadata locks */ |
| 4785 |
set_also_gap_locks = false; |
| 4786 |
} |
| 4787 |
|
| 4788 |
/* Note that if the search mode was GE or G, then the cursor |
| 4789 |
naturally moves upward (in fetch next) in alphabetical order, |
| 4790 |
otherwise downward */ |
| 4791 |
|
| 4792 |
if (direction == 0) { |
| 4793 |
if (mode == PAGE_CUR_GE || mode == PAGE_CUR_G || mode >= PAGE_CUR_CONTAIN) { |
| 4794 |
moves_up = true; |
| 4795 |
} |
| 4796 |
|
| 4797 |
} else if (direction == ROW_SEL_NEXT) { |
| 4798 |
moves_up = true; |
| 4799 |
} |
| 4800 |
|
| 4801 |
thr = que_fork_get_first_thr(prebuilt->sel_graph); |
| 4802 |
|
| 4803 |
que_thr_move_to_run_state_for_mysql(thr, trx); |
| 4804 |
|
| 4805 |
clust_index = index->table->first_index(); |
| 4806 |
|
| 4807 |
clust_templ_for_sec = |
| 4808 |
index != clust_index && prebuilt->need_to_access_clustered; |
| 4809 |
|
| 4810 |
/* Do some start-of-statement preparations */ |
| 4811 |
|
| 4812 |
if (!prebuilt->sql_stat_start) { |
| 4813 |
/* No need to set an intention lock or assign a read view */ |
| 4814 |
|
| 4815 |
if (!MVCC::is_view_active(trx->read_view) && !srv_read_only_mode && |
| 4816 |
prebuilt->select_lock_type == LOCK_NONE) { |
| 4817 |
ib::error(ER_IB_MSG_1031) << "MySQL is trying to perform a" |
| 4818 |
" consistent read but the read view is not" |
| 4819 |
" assigned!"; |
| 4820 |
trx_print(stderr, trx, 600); |
| 4821 |
fputc('\n', stderr); |
| 4822 |
ut_error; |
| 4823 |
} |
| 4824 |
} else if (prebuilt->select_lock_type == LOCK_NONE) { |
| 4825 |
/* This is a consistent read */ |
| 4826 |
/* Assign a read view for the query */ |
| 4827 |
|
| 4828 |
if (!srv_read_only_mode) { |
| 4829 |
trx_assign_read_view(trx); |
| 4830 |
} |
| 4831 |
|
| 4832 |
prebuilt->sql_stat_start = false; |
| 4833 |
} else { |
| 4834 |
wait_table_again: |
| 4835 |
err = lock_table(0, index->table, |
| 4836 |
prebuilt->select_lock_type == LOCK_S ? LOCK_IS : LOCK_IX, |
| 4837 |
thr); |
| 4838 |
|
| 4839 |
if (err != DB_SUCCESS) { |
| 4840 |
table_lock_waited = true; |
| 4841 |
goto lock_table_wait; |
| 4842 |
} |
| 4843 |
prebuilt->sql_stat_start = false; |
| 4844 |
} |
| 4845 |
|
| 4846 |
/* Open or restore index cursor position */ |
| 4847 |
|
| 4848 |
if (UNIV_LIKELY(direction != 0)) { |
| 4849 |
if (spatial_search) { |
| 4850 |
/* R-Tree access does not need to do |
| 4851 |
cursor position and resposition */ |
| 4852 |
goto next_rec; |
| 4853 |
} |
| 4854 |
|
| 4855 |
auto need_to_process = sel_restore_position_for_mysql( |
| 4856 |
&same_user_rec, BTR_SEARCH_LEAF, pcur, moves_up, &mtr); |
| 4857 |
|
| 4858 |
ut_ad(prev_rec == nullptr); |
| 4859 |
|
| 4860 |
if (UNIV_UNLIKELY(need_to_process)) { |
| 4861 |
if (UNIV_UNLIKELY(prebuilt->row_read_type == |
| 4862 |
ROW_READ_DID_SEMI_CONSISTENT)) { |
| 4863 |
/* We did a semi-consistent read, |
| 4864 |
but the record was removed in |
| 4865 |
the meantime. */ |
| 4866 |
prebuilt->row_read_type = ROW_READ_TRY_SEMI_CONSISTENT; |
| 4867 |
} |
| 4868 |
} else if (UNIV_LIKELY(prebuilt->row_read_type != |
| 4869 |
ROW_READ_DID_SEMI_CONSISTENT)) { |
| 4870 |
/* The cursor was positioned on the record |
| 4871 |
that we returned previously. If we need |
| 4872 |
to repeat a semi-consistent read as a |
| 4873 |
pessimistic locking read, the record |
| 4874 |
cannot be skipped. */ |
| 4875 |
|
| 4876 |
goto next_rec; |
| 4877 |
} |
| 4878 |
|
| 4879 |
} else if (dtuple_get_n_fields(search_tuple) > 0) { |
| 4880 |
pcur->m_btr_cur.thr = thr; |
| 4881 |
|
| 4882 |
if (dict_index_is_spatial(index)) { |
| 4883 |
bool need_pred_lock = set_also_gap_locks && !trx->skip_gap_locks() && |
| 4884 |
prebuilt->select_lock_type != LOCK_NONE; |
| 4885 |
|
| 4886 |
if (!prebuilt->rtr_info) { |
| 4887 |
prebuilt->rtr_info = rtr_create_rtr_info(need_pred_lock, true, |
| 4888 |
pcur->get_btr_cur(), index); |
| 4889 |
prebuilt->rtr_info->search_tuple = search_tuple; |
| 4890 |
prebuilt->rtr_info->search_mode = mode; |
| 4891 |
rtr_info_update_btr(pcur->get_btr_cur(), prebuilt->rtr_info); |
| 4892 |
} else { |
| 4893 |
rtr_info_reinit_in_cursor(pcur->get_btr_cur(), index, need_pred_lock); |
| 4894 |
prebuilt->rtr_info->search_tuple = search_tuple; |
| 4895 |
prebuilt->rtr_info->search_mode = mode; |
| 4896 |
} |
| 4897 |
} |
| 4898 |
|
| 4899 |
pcur->open_no_init(index, search_tuple, mode, BTR_SEARCH_LEAF, 0, &mtr, |
| 4900 |
UT_LOCATION_HERE); |
| 4901 |
|
| 4902 |
pcur->m_trx_if_known = trx; |
| 4903 |
|
| 4904 |
rec = pcur->get_rec(); |
| 4905 |
|
| 4906 |
if (!moves_up && !page_rec_is_supremum(rec) && set_also_gap_locks && |
| 4907 |
!trx->skip_gap_locks() && prebuilt->select_lock_type != LOCK_NONE && |
| 4908 |
!dict_index_is_spatial(index)) { |
| 4909 |
/* Try to place a gap lock on the next index record |
| 4910 |
to prevent phantoms in ORDER BY ... DESC queries */ |
| 4911 |
const rec_t *next_rec = page_rec_get_next_const(rec); |
| 4912 |
|
| 4913 |
offsets = rec_get_offsets(next_rec, index, offsets, ULINT_UNDEFINED, |
| 4914 |
UT_LOCATION_HERE, &heap); |
| 4915 |
err = sel_set_rec_lock(pcur, next_rec, index, offsets, |
| 4916 |
prebuilt->select_mode, prebuilt->select_lock_type, |
| 4917 |
LOCK_GAP, thr, &mtr); |
| 4918 |
|
| 4919 |
switch (err) { |
| 4920 |
case DB_SUCCESS_LOCKED_REC: |
| 4921 |
err = DB_SUCCESS; |
| 4922 |
case DB_SUCCESS: |
| 4923 |
break; |
| 4924 |
case DB_SKIP_LOCKED: |
| 4925 |
case DB_LOCK_NOWAIT: |
| 4926 |
ut_d(ut_error); |
| 4927 |
ut_o(goto next_rec); |
| 4928 |
default: |
| 4929 |
goto lock_wait_or_error; |
| 4930 |
} |
| 4931 |
} |
| 4932 |
} else if (mode == PAGE_CUR_G || mode == PAGE_CUR_L) { |
| 4933 |
pcur->open_at_side(mode == PAGE_CUR_G, index, BTR_SEARCH_LEAF, false, 0, |
| 4934 |
&mtr); |
| 4935 |
} |
| 4936 |
|
| 4937 |
rec_loop: |
| 4938 |
ut_ad(trx_can_be_handled_by_current_thread(trx)); |
| 4939 |
DEBUG_SYNC_C("row_search_rec_loop"); |
| 4940 |
|
| 4941 |
prebuilt->lob_undo_reset(); |
| 4942 |
|
| 4943 |
if (trx_is_interrupted(trx)) { |
| 4944 |
if (!spatial_search) { |
| 4945 |
pcur->store_position(&mtr); |
| 4946 |
} |
| 4947 |
err = DB_INTERRUPTED; |
| 4948 |
goto normal_return; |
| 4949 |
} |
| 4950 |
|
| 4951 |
/*-------------------------------------------------------------*/ |
| 4952 |
/* PHASE 4: Look for matching records in a loop */ |
| 4953 |
|
| 4954 |
rec = pcur->get_rec(); |
| 4955 |
|
| 4956 |
ut_ad(page_rec_is_comp(rec) == comp); |
| 4957 |
|
| 4958 |
if (page_rec_is_infimum(rec)) { |
| 4959 |
/* The infimum record on a page cannot be in the result set, |
| 4960 |
and neither can a record lock be placed on it: we skip such |
| 4961 |
a record. */ |
| 4962 |
|
| 4963 |
prev_rec = nullptr; |
| 4964 |
goto next_rec; |
| 4965 |
} |
| 4966 |
|
| 4967 |
if (page_rec_is_supremum(rec)) { |
| 4968 |
DBUG_EXECUTE_IF( |
| 4969 |
"compare_end_range", if (end_loop < 100) { end_loop = 100; }); |
| 4970 |
|
| 4971 |
/** Compare the last record of the page with end range |
| 4972 |
passed to InnoDB when there is no ICP and number of |
| 4973 |
loops in row_search_mvcc for rows found but not |
| 4974 |
reporting due to search views etc. |
| 4975 |
When scanning a multi-value index, we don't perform the |
| 4976 |
check because we cannot convert the indexed value |
| 4977 |
(single scalar element) into the primary index (virtual) |
| 4978 |
column type (array of values). */ |
| 4979 |
if (prev_rec != nullptr && !prebuilt->innodb_api && |
| 4980 |
prebuilt->m_mysql_handler->end_range != nullptr && |
| 4981 |
prebuilt->idx_cond == false && end_loop >= 100 && |
| 4982 |
!(clust_templ_for_sec && index->is_multi_value())) { |
| 4983 |
dict_index_t *key_index = prebuilt->index; |
| 4984 |
|
| 4985 |
if (end_range_cache == nullptr) { |
| 4986 |
end_range_cache = static_cast<byte *>(ut::malloc_withkey( |
| 4987 |
UT_NEW_THIS_FILE_PSI_KEY, prebuilt->mysql_row_len)); |
| 4988 |
} |
| 4989 |
|
| 4990 |
if (clust_templ_for_sec) { |
| 4991 |
/** Secondary index record but the template |
| 4992 |
based on PK. */ |
| 4993 |
key_index = clust_index; |
| 4994 |
} |
| 4995 |
|
| 4996 |
/** Create offsets based on prebuilt index. */ |
| 4997 |
offsets = rec_get_offsets(prev_rec, prebuilt->index, offsets, |
| 4998 |
ULINT_UNDEFINED, UT_LOCATION_HERE, &heap); |
| 4999 |
|
| 5000 |
if (row_sel_store_mysql_rec(end_range_cache, prebuilt, prev_rec, |
| 5001 |
prev_vrow, clust_templ_for_sec, key_index, |
| 5002 |
prebuilt->index, offsets, clust_templ_for_sec, |
| 5003 |
prebuilt->get_lob_undo(), |
| 5004 |
prebuilt->blob_heap)) { |
| 5005 |
if (row_search_end_range_check(end_range_cache, prev_rec, prebuilt, |
| 5006 |
clust_templ_for_sec, offsets, |
| 5007 |
record_buffer)) { |
| 5008 |
/** In case of prebuilt->fetch, |
| 5009 |
set the error in prebuilt->end_range. */ |
| 5010 |
if (next_buf != nullptr) { |
| 5011 |
prebuilt->m_end_range = true; |
| 5012 |
} |
| 5013 |
|
| 5014 |
err = DB_RECORD_NOT_FOUND; |
| 5015 |
goto normal_return; |
| 5016 |
} |
| 5017 |
} |
| 5018 |
DEBUG_SYNC_C("allow_insert"); |
| 5019 |
} |
| 5020 |
|
| 5021 |
if (set_also_gap_locks && !trx->skip_gap_locks() && |
| 5022 |
prebuilt->select_lock_type != LOCK_NONE && |
| 5023 |
!dict_index_is_spatial(index)) { |
| 5024 |
/* Try to place a lock on the index record */ |
| 5025 |
|
| 5026 |
offsets = rec_get_offsets(rec, index, offsets, ULINT_UNDEFINED, |
| 5027 |
UT_LOCATION_HERE, &heap); |
| 5028 |
err = sel_set_rec_lock(pcur, rec, index, offsets, prebuilt->select_mode, |
| 5029 |
prebuilt->select_lock_type, LOCK_ORDINARY, thr, |
| 5030 |
&mtr); |
| 5031 |
|
| 5032 |
switch (err) { |
| 5033 |
case DB_SUCCESS_LOCKED_REC: |
| 5034 |
err = DB_SUCCESS; |
| 5035 |
case DB_SUCCESS: |
| 5036 |
break; |
| 5037 |
case DB_SKIP_LOCKED: |
| 5038 |
case DB_LOCK_NOWAIT: |
| 5039 |
ut_d(ut_error); |
| 5040 |
default: |
| 5041 |
goto lock_wait_or_error; |
| 5042 |
} |
| 5043 |
DEBUG_SYNC_C("allow_insert"); |
| 5044 |
} |
| 5045 |
|
| 5046 |
/* A page supremum record cannot be in the result set: skip |
| 5047 |
it now that we have placed a possible lock on it */ |
| 5048 |
|
| 5049 |
prev_rec = nullptr; |
| 5050 |
goto next_rec; |
| 5051 |
} |
| 5052 |
|
| 5053 |
/*-------------------------------------------------------------*/ |
| 5054 |
/* Do sanity checks in case our cursor has bumped into page |
| 5055 |
corruption */ |
| 5056 |
|
| 5057 |
if (comp) { |
| 5058 |
next_offs = rec_get_next_offs(rec, true); |
| 5059 |
if (UNIV_UNLIKELY(next_offs < PAGE_NEW_SUPREMUM)) { |
| 5060 |
goto wrong_offs; |
| 5061 |
} |
| 5062 |
} else { |
| 5063 |
next_offs = rec_get_next_offs(rec, false); |
| 5064 |
if (UNIV_UNLIKELY(next_offs < PAGE_OLD_SUPREMUM)) { |
| 5065 |
goto wrong_offs; |
| 5066 |
} |
| 5067 |
} |
| 5068 |
|
| 5069 |
if (UNIV_UNLIKELY(next_offs >= UNIV_PAGE_SIZE - PAGE_DIR)) { |
| 5070 |
wrong_offs: |
| 5071 |
if (srv_force_recovery == 0 || moves_up == false) { |
| 5072 |
ib::error(ER_IB_MSG_1032) |
| 5073 |
<< "Rec address " << static_cast<const void *>(rec) |
| 5074 |
<< ", buf block fix count " |
| 5075 |
<< btr_cur_get_block(pcur->get_btr_cur())->page.buf_fix_count; |
| 5076 |
|
| 5077 |
ib::error(ER_IB_MSG_1033) |
| 5078 |
<< "Index corruption: rec offs " << page_offset(rec) << " next offs " |
| 5079 |
<< next_offs << ", page no " << page_get_page_no(page_align(rec)) |
| 5080 |
<< ", index " << index->name << " of table " << index->table->name |
| 5081 |
<< ". Run CHECK TABLE. You may need to" |
| 5082 |
" restore from a backup, or dump + drop +" |
| 5083 |
" reimport the table."; |
| 5084 |
err = DB_CORRUPTION; |
| 5085 |
|
| 5086 |
ut_d(ut_error); |
| 5087 |
ut_o(goto lock_wait_or_error); |
| 5088 |
} else { |
| 5089 |
/* The user may be dumping a corrupt table. Jump |
| 5090 |
over the corruption to recover as much as possible. */ |
| 5091 |
|
| 5092 |
ib::info(ER_IB_MSG_1034) |
| 5093 |
<< "Index corruption: rec offs " << page_offset(rec) << " next offs " |
| 5094 |
<< next_offs << ", page no " << page_get_page_no(page_align(rec)) |
| 5095 |
<< ", index " << index->name << " of table " << index->table->name |
| 5096 |
<< ". We try to skip the rest of the page."; |
| 5097 |
|
| 5098 |
pcur->move_to_last_on_page(&mtr); |
| 5099 |
|
| 5100 |
prev_rec = nullptr; |
| 5101 |
goto next_rec; |
| 5102 |
} |
| 5103 |
} |
| 5104 |
/*-------------------------------------------------------------*/ |
| 5105 |
|
| 5106 |
/* Calculate the 'offsets' associated with 'rec' */ |
| 5107 |
|
| 5108 |
ut_ad(fil_page_index_page_check(pcur->get_page())); |
| 5109 |
ut_ad(btr_page_get_index_id(pcur->get_page()) == index->id); |
| 5110 |
|
| 5111 |
offsets = rec_get_offsets(rec, index, offsets, ULINT_UNDEFINED, |
| 5112 |
UT_LOCATION_HERE, &heap); |
| 5113 |
|
| 5114 |
if (UNIV_UNLIKELY(srv_force_recovery > 0)) { |
| 5115 |
if (!rec_validate(rec, offsets) || |
| 5116 |
!btr_index_rec_validate(rec, index, false)) { |
| 5117 |
ib::info(ER_IB_MSG_1035) |
| 5118 |
<< "Index corruption: rec offs " << page_offset(rec) << " next offs " |
| 5119 |
<< next_offs << ", page no " << page_get_page_no(page_align(rec)) |
| 5120 |
<< ", index " << index->name << " of table " << index->table->name |
| 5121 |
<< ". We try to skip the record."; |
| 5122 |
|
| 5123 |
prev_rec = nullptr; |
| 5124 |
goto next_rec; |
| 5125 |
} |
| 5126 |
} |
| 5127 |
|
| 5128 |
prev_rec = rec; |
| 5129 |
ut_d(prev_rec_debug = row_search_debug_copy_rec_order_prefix( |
| 5130 |
pcur, index, prev_rec, &prev_rec_debug_n_fields, &prev_rec_debug_buf, |
| 5131 |
&prev_rec_debug_buf_size)); |
| 5132 |
|
| 5133 |
/* Note that we cannot trust the up_match value in the cursor at this |
| 5134 |
place because we can arrive here after moving the cursor! Thus |
| 5135 |
we have to recompare rec and search_tuple to determine if they |
| 5136 |
match enough. */ |
| 5137 |
|
| 5138 |
if (match_mode == ROW_SEL_EXACT) { |
| 5139 |
/* Test if the index record matches completely to search_tuple |
| 5140 |
in prebuilt: if not, then we return with DB_RECORD_NOT_FOUND */ |
| 5141 |
|
| 5142 |
/* fputs("Comparing rec and search tuple\n", stderr); */ |
| 5143 |
|
| 5144 |
if (0 != cmp_dtuple_rec(search_tuple, rec, index, offsets)) { |
| 5145 |
if (set_also_gap_locks && !trx->skip_gap_locks() && |
| 5146 |
prebuilt->select_lock_type != LOCK_NONE && |
| 5147 |
!dict_index_is_spatial(index)) { |
| 5148 |
err = sel_set_rec_lock(pcur, rec, index, offsets, prebuilt->select_mode, |
| 5149 |
prebuilt->select_lock_type, LOCK_GAP, thr, &mtr); |
| 5150 |
|
| 5151 |
switch (err) { |
| 5152 |
case DB_SUCCESS_LOCKED_REC: |
| 5153 |
case DB_SUCCESS: |
| 5154 |
break; |
| 5155 |
case DB_SKIP_LOCKED: |
| 5156 |
case DB_LOCK_NOWAIT: |
| 5157 |
ut_d(ut_error); |
| 5158 |
default: |
| 5159 |
goto lock_wait_or_error; |
| 5160 |
} |
| 5161 |
} |
| 5162 |
|
| 5163 |
pcur->store_position(&mtr); |
| 5164 |
|
| 5165 |
/* The found record was not a match, but may be used |
| 5166 |
as NEXT record (index_next). Set the relative position |
| 5167 |
to BTR_PCUR_BEFORE, to reflect that the position of |
| 5168 |
the persistent cursor is before the found/stored row |
| 5169 |
(pcur->m_old_rec). */ |
| 5170 |
ut_ad(pcur->m_rel_pos == BTR_PCUR_ON); |
| 5171 |
pcur->m_rel_pos = BTR_PCUR_BEFORE; |
| 5172 |
|
| 5173 |
err = DB_RECORD_NOT_FOUND; |
| 5174 |
goto normal_return; |
| 5175 |
} |
| 5176 |
|
| 5177 |
} else if (match_mode == ROW_SEL_EXACT_PREFIX) { |
| 5178 |
if (!cmp_dtuple_is_prefix_of_rec(search_tuple, rec, index, offsets)) { |
| 5179 |
if (set_also_gap_locks && !trx->skip_gap_locks() && |
| 5180 |
prebuilt->select_lock_type != LOCK_NONE && |
| 5181 |
!dict_index_is_spatial(index)) { |
| 5182 |
err = sel_set_rec_lock(pcur, rec, index, offsets, prebuilt->select_mode, |
| 5183 |
prebuilt->select_lock_type, LOCK_GAP, thr, &mtr); |
| 5184 |
|
| 5185 |
switch (err) { |
| 5186 |
case DB_SUCCESS_LOCKED_REC: |
| 5187 |
case DB_SUCCESS: |
| 5188 |
break; |
| 5189 |
case DB_SKIP_LOCKED: |
| 5190 |
case DB_LOCK_NOWAIT: |
| 5191 |
ut_d(ut_error); |
| 5192 |
default: |
| 5193 |
goto lock_wait_or_error; |
| 5194 |
} |
| 5195 |
} |
| 5196 |
|
| 5197 |
pcur->store_position(&mtr); |
| 5198 |
|
| 5199 |
/* The found record was not a match, but may be used |
| 5200 |
as NEXT record (index_next). Set the relative position |
| 5201 |
to BTR_PCUR_BEFORE, to reflect that the position of |
| 5202 |
the persistent cursor is before the found/stored row |
| 5203 |
(pcur->old_rec). */ |
| 5204 |
ut_ad(pcur->m_rel_pos == BTR_PCUR_ON); |
| 5205 |
pcur->m_rel_pos = BTR_PCUR_BEFORE; |
| 5206 |
|
| 5207 |
err = DB_RECORD_NOT_FOUND; |
| 5208 |
goto normal_return; |
| 5209 |
} |
| 5210 |
} |
| 5211 |
|
| 5212 |
/* We are ready to look at a possible new index entry in the result |
| 5213 |
set: the cursor is now placed on a user record */ |
| 5214 |
|
| 5215 |
if (prebuilt->select_lock_type != LOCK_NONE) { |
| 5216 |
auto row_to_range_relation = row_compare_row_to_range( |
| 5217 |
set_also_gap_locks, trx, unique_search, index, clust_index, rec, comp, |
| 5218 |
mode, direction, search_tuple, offsets, moves_up, prebuilt); |
| 5219 |
|
| 5220 |
ulint lock_type; |
| 5221 |
if (row_to_range_relation.row_can_be_in_range) { |
| 5222 |
if (row_to_range_relation.gap_can_intersect_range) { |
| 5223 |
lock_type = LOCK_ORDINARY; |
| 5224 |
} else { |
| 5225 |
lock_type = LOCK_REC_NOT_GAP; |
| 5226 |
} |
| 5227 |
} else { |
| 5228 |
if (row_to_range_relation.gap_can_intersect_range) { |
| 5229 |
lock_type = LOCK_GAP; |
| 5230 |
} else { |
| 5231 |
err = DB_RECORD_NOT_FOUND; |
| 5232 |
goto normal_return; |
| 5233 |
} |
| 5234 |
} |
| 5235 |
/* in case of semi-consistent read, we use SELECT_SKIP_LOCKED, so we don't |
| 5236 |
waste time on creating a WAITING lock, as we won't wait on it anyway */ |
| 5237 |
const bool use_semi_consistent = |
| 5238 |
prebuilt->row_read_type == ROW_READ_TRY_SEMI_CONSISTENT && |
| 5239 |
!unique_search && index == clust_index && !trx_is_high_priority(trx); |
| 5240 |
err = sel_set_rec_lock( |
| 5241 |
pcur, rec, index, offsets, |
| 5242 |
use_semi_consistent ? SELECT_SKIP_LOCKED : prebuilt->select_mode, |
| 5243 |
prebuilt->select_lock_type, lock_type, thr, &mtr); |
| 5244 |
|
| 5245 |
switch (err) { |
| 5246 |
const rec_t *old_vers; |
| 5247 |
case DB_SUCCESS_LOCKED_REC: |
| 5248 |
if (trx->releases_non_matching_rows()) { |
| 5249 |
/* Note that a record of |
| 5250 |
prebuilt->index was locked. */ |
| 5251 |
ut_ad(!prebuilt->new_rec_lock[row_prebuilt_t::LOCK_PCUR]); |
| 5252 |
prebuilt->new_rec_lock[row_prebuilt_t::LOCK_PCUR] = true; |
| 5253 |
} |
| 5254 |
err = DB_SUCCESS; |
| 5255 |
[[fallthrough]]; |
| 5256 |
case DB_SUCCESS: |
| 5257 |
if (row_to_range_relation.row_must_be_at_end) { |
| 5258 |
prebuilt->m_stop_tuple_found = true; |
| 5259 |
} |
| 5260 |
break; |
| 5261 |
case DB_SKIP_LOCKED: |
| 5262 |
if (prebuilt->select_mode == SELECT_SKIP_LOCKED) { |
| 5263 |
goto next_rec; |
| 5264 |
} |
| 5265 |
DEBUG_SYNC_C("semi_consistent_read_would_wait"); |
| 5266 |
ut_a(use_semi_consistent); |
| 5267 |
ut_a(trx->allow_semi_consistent()); |
| 5268 |
/* The following call returns 'offsets' associated with 'old_vers' */ |
| 5269 |
row_sel_build_committed_vers_for_mysql( |
| 5270 |
clust_index, prebuilt, rec, &offsets, &heap, &old_vers, |
| 5271 |
need_vrow ? &vrow : nullptr, &mtr); |
| 5272 |
|
| 5273 |
ut_ad(!dict_index_is_spatial(index)); |
| 5274 |
err = DB_SUCCESS; |
| 5275 |
|
| 5276 |
if (old_vers == nullptr) { |
| 5277 |
/* The row was not yet committed */ |
| 5278 |
goto next_rec; |
| 5279 |
} |
| 5280 |
|
| 5281 |
did_semi_consistent_read = true; |
| 5282 |
rec = old_vers; |
| 5283 |
prev_rec = rec; |
| 5284 |
ut_d(prev_rec_debug = row_search_debug_copy_rec_order_prefix( |
| 5285 |
pcur, index, prev_rec, &prev_rec_debug_n_fields, |
| 5286 |
&prev_rec_debug_buf, &prev_rec_debug_buf_size)); |
| 5287 |
break; |
| 5288 |
case DB_LOCK_WAIT: |
| 5289 |
/* Lock wait for R-tree should already |
| 5290 |
be handled in sel_set_rtr_rec_lock() */ |
| 5291 |
ut_ad(!dict_index_is_spatial(index)); |
| 5292 |
/* Never unlock rows that were part of a conflict. */ |
| 5293 |
prebuilt->new_rec_lock.reset(); |
| 5294 |
ut_a(!use_semi_consistent); |
| 5295 |
goto lock_wait_or_error; |
| 5296 |
case DB_RECORD_NOT_FOUND: |
| 5297 |
if (dict_index_is_spatial(index)) { |
| 5298 |
goto next_rec; |
| 5299 |
} else { |
| 5300 |
goto lock_wait_or_error; |
| 5301 |
} |
| 5302 |
|
| 5303 |
default: |
| 5304 |
ut_a(!use_semi_consistent); |
| 5305 |
goto lock_wait_or_error; |
| 5306 |
} |
| 5307 |
if (err == DB_SUCCESS && !row_to_range_relation.row_can_be_in_range) { |
| 5308 |
err = DB_RECORD_NOT_FOUND; |
| 5309 |
goto normal_return; |
| 5310 |
} |
| 5311 |
} else { |
| 5312 |
/* This is a non-locking consistent read: if necessary, fetch |
| 5313 |
a previous version of the record */ |
| 5314 |
|
| 5315 |
if (trx->isolation_level == TRX_ISO_READ_UNCOMMITTED) { |
| 5316 |
/* Do nothing: we let a non-locking SELECT read the |
| 5317 |
latest version of the record */ |
| 5318 |
|
| 5319 |
} else if (index == clust_index) { |
| 5320 |
/* Fetch a previous version of the row if the current |
| 5321 |
one is not visible in the snapshot; if we have a very |
| 5322 |
high force recovery level set, we try to avoid crashes |
| 5323 |
by skipping this lookup */ |
| 5324 |
|
| 5325 |
if (srv_force_recovery < 5 && |
| 5326 |
!lock_clust_rec_cons_read_sees(rec, index, offsets, |
| 5327 |
trx_get_read_view(trx))) { |
| 5328 |
rec_t *old_vers; |
| 5329 |
/* The following call returns 'offsets' associated with 'old_vers' */ |
| 5330 |
err = row_sel_build_prev_vers_for_mysql( |
| 5331 |
trx->read_view, clust_index, prebuilt, rec, &offsets, &heap, |
| 5332 |
&old_vers, need_vrow ? &vrow : nullptr, &mtr, |
| 5333 |
prebuilt->get_lob_undo()); |
| 5334 |
|
| 5335 |
if (err != DB_SUCCESS) { |
| 5336 |
goto lock_wait_or_error; |
| 5337 |
} |
| 5338 |
|
| 5339 |
if (old_vers == nullptr) { |
| 5340 |
/* The row did not exist yet in |
| 5341 |
the read view */ |
| 5342 |
|
| 5343 |
goto next_rec; |
| 5344 |
} |
| 5345 |
|
| 5346 |
rec = old_vers; |
| 5347 |
prev_rec = rec; |
| 5348 |
ut_d(prev_rec_debug = row_search_debug_copy_rec_order_prefix( |
| 5349 |
pcur, index, prev_rec, &prev_rec_debug_n_fields, |
| 5350 |
&prev_rec_debug_buf, &prev_rec_debug_buf_size)); |
| 5351 |
} |
| 5352 |
} else { |
| 5353 |
/* We are looking into a non-clustered index, |
| 5354 |
and to get the right version of the record we |
| 5355 |
have to look also into the clustered index: this |
| 5356 |
is necessary, because we can only get the undo |
| 5357 |
information via the clustered index record. */ |
| 5358 |
|
| 5359 |
ut_ad(!index->is_clustered()); |
| 5360 |
|
| 5361 |
if (!srv_read_only_mode && |
| 5362 |
!lock_sec_rec_cons_read_sees(rec, index, trx->read_view)) { |
| 5363 |
/* We should look at the clustered index. |
| 5364 |
However, as this is a non-locking read, |
| 5365 |
we can skip the clustered index lookup if |
| 5366 |
the condition does not match the secondary |
| 5367 |
index entry. */ |
| 5368 |
switch (row_search_idx_cond_check(buf, prebuilt, rec, offsets)) { |
| 5369 |
case ICP_NO_MATCH: |
| 5370 |
goto next_rec; |
| 5371 |
case ICP_OUT_OF_RANGE: |
| 5372 |
err = DB_RECORD_NOT_FOUND; |
| 5373 |
goto idx_cond_failed; |
| 5374 |
case ICP_MATCH: |
| 5375 |
goto requires_clust_rec; |
| 5376 |
} |
| 5377 |
|
| 5378 |
ut_error; |
| 5379 |
} |
| 5380 |
} |
| 5381 |
} |
| 5382 |
|
| 5383 |
#ifdef UNIV_DEBUG |
| 5384 |
if (did_semi_consistent_read) { |
| 5385 |
ut_a(prebuilt->select_lock_type != LOCK_NONE); |
| 5386 |
ut_a(!prebuilt->table->is_intrinsic()); |
| 5387 |
ut_a(prebuilt->row_read_type == ROW_READ_TRY_SEMI_CONSISTENT); |
| 5388 |
ut_a(prebuilt->trx->allow_semi_consistent()); |
| 5389 |
ut_a(prebuilt->new_rec_locks_count() == 0); |
| 5390 |
} |
| 5391 |
#endif /* UNIV_DEBUG */ |
| 5392 |
|
| 5393 |
/* NOTE that at this point rec can be an old version of a clustered |
| 5394 |
index record built for a consistent read. We cannot assume after this |
| 5395 |
point that rec is on a buffer pool page. Functions like |
| 5396 |
page_rec_is_comp() cannot be used! */ |
| 5397 |
|
| 5398 |
if (rec_get_deleted_flag(rec, comp)) { |
| 5399 |
/* The record is delete-marked: we can skip it */ |
| 5400 |
|
| 5401 |
/* No need to keep a lock on a delete-marked record in lower isolation |
| 5402 |
levels - it's similar to when Server sees the WHERE condition doesn't match |
| 5403 |
and calls unlock_row(). */ |
| 5404 |
prebuilt->try_unlock(true); |
| 5405 |
|
| 5406 |
/* This is an optimization to skip setting the next key lock |
| 5407 |
on the record that follows this delete-marked record. This |
| 5408 |
optimization works because of the unique search criteria |
| 5409 |
which precludes the presence of a range lock between this |
| 5410 |
delete marked record and the record following it. |
| 5411 |
|
| 5412 |
For now this is applicable only to clustered indexes while |
| 5413 |
doing a unique search except for HANDLER queries because |
| 5414 |
HANDLER allows NEXT and PREV even in unique search on |
| 5415 |
clustered index. There is scope for further optimization |
| 5416 |
applicable to unique secondary indexes. Current behaviour is |
| 5417 |
to widen the scope of a lock on an already delete marked record |
| 5418 |
if the same record is deleted twice by the same transaction */ |
| 5419 |
if (index == clust_index && unique_search && !prebuilt->used_in_HANDLER) { |
| 5420 |
err = DB_RECORD_NOT_FOUND; |
| 5421 |
|
| 5422 |
goto normal_return; |
| 5423 |
} |
| 5424 |
|
| 5425 |
goto next_rec; |
| 5426 |
} |
| 5427 |
|
| 5428 |
/* Check if the record matches the index condition. */ |
| 5429 |
switch (row_search_idx_cond_check(buf, prebuilt, rec, offsets)) { |
| 5430 |
case ICP_NO_MATCH: |
| 5431 |
prebuilt->try_unlock(true); |
| 5432 |
goto next_rec; |
| 5433 |
case ICP_OUT_OF_RANGE: |
| 5434 |
err = DB_RECORD_NOT_FOUND; |
| 5435 |
prebuilt->try_unlock(true); |
| 5436 |
goto idx_cond_failed; |
| 5437 |
case ICP_MATCH: |
| 5438 |
break; |
| 5439 |
} |
| 5440 |
|
| 5441 |
/* Get the clustered index record if needed, if we did not do the |
| 5442 |
search using the clustered index. */ |
| 5443 |
|
| 5444 |
if (index != clust_index && prebuilt->need_to_access_clustered) { |
| 5445 |
requires_clust_rec: |
| 5446 |
ut_ad(index != clust_index); |
| 5447 |
/* We use a 'goto' to the preceding label if a consistent |
| 5448 |
read of a secondary index record requires us to look up old |
| 5449 |
versions of the associated clustered index record. */ |
| 5450 |
|
| 5451 |
ut_ad(rec_offs_validate(rec, index, offsets)); |
| 5452 |
|
| 5453 |
/* It was a non-clustered index and we must fetch also the |
| 5454 |
clustered index record */ |
| 5455 |
|
| 5456 |
mtr_has_extra_clust_latch = true; |
| 5457 |
|
| 5458 |
ut_ad(!vrow); |
| 5459 |
|
| 5460 |
/* The following call returns 'offsets' associated with |
| 5461 |
'clust_rec'. Note that 'clust_rec' can be an old version |
| 5462 |
built for a consistent read. */ |
| 5463 |
err = row_sel_get_clust_rec_for_mysql( |
| 5464 |
prebuilt, index, rec, thr, &clust_rec, &offsets, &heap, |
| 5465 |
need_vrow ? &vrow : nullptr, &mtr, prebuilt->get_lob_undo()); |
| 5466 |
switch (err) { |
| 5467 |
case DB_SUCCESS: |
| 5468 |
if (clust_rec == nullptr) { |
| 5469 |
/* The record did not exist in the read view */ |
| 5470 |
ut_ad(prebuilt->select_lock_type == LOCK_NONE || |
| 5471 |
dict_index_is_spatial(index)); |
| 5472 |
|
| 5473 |
goto next_rec; |
| 5474 |
} |
| 5475 |
break; |
| 5476 |
case DB_SKIP_LOCKED: |
| 5477 |
goto next_rec; |
| 5478 |
case DB_SUCCESS_LOCKED_REC: |
| 5479 |
ut_a(clust_rec != nullptr); |
| 5480 |
if (trx->releases_non_matching_rows()) { |
| 5481 |
/* Note that the clustered index record |
| 5482 |
was locked. */ |
| 5483 |
ut_ad(!prebuilt->new_rec_lock[row_prebuilt_t::LOCK_CLUST_PCUR]); |
| 5484 |
prebuilt->new_rec_lock[row_prebuilt_t::LOCK_CLUST_PCUR] = true; |
| 5485 |
} |
| 5486 |
err = DB_SUCCESS; |
| 5487 |
break; |
| 5488 |
default: |
| 5489 |
vrow = nullptr; |
| 5490 |
goto lock_wait_or_error; |
| 5491 |
} |
| 5492 |
|
| 5493 |
if (rec_get_deleted_flag(clust_rec, comp)) { |
| 5494 |
/* The record is delete marked: we can skip it */ |
| 5495 |
|
| 5496 |
/* No need to keep a lock on a delete-marked record in lower isolation |
| 5497 |
levels - it's similar to when Server sees the WHERE condition doesn't |
| 5498 |
match and calls unlock_row(). */ |
| 5499 |
prebuilt->try_unlock(true); |
| 5500 |
|
| 5501 |
goto next_rec; |
| 5502 |
} |
| 5503 |
|
| 5504 |
if (need_vrow && !vrow) { |
| 5505 |
if (!heap) { |
| 5506 |
heap = mem_heap_create(100, UT_LOCATION_HERE); |
| 5507 |
} |
| 5508 |
row_sel_fill_vrow(rec, index, &vrow, heap); |
| 5509 |
} |
| 5510 |
|
| 5511 |
result_rec = clust_rec; |
| 5512 |
ut_ad(rec_offs_validate(result_rec, clust_index, offsets)); |
| 5513 |
|
| 5514 |
if (prebuilt->idx_cond) { |
| 5515 |
/* Convert the record to MySQL format. We were |
| 5516 |
unable to do this in row_search_idx_cond_check(), |
| 5517 |
because the condition is on the secondary index |
| 5518 |
and the requested column is in the clustered index. |
| 5519 |
We convert all fields, including those that |
| 5520 |
may have been used in ICP, because the |
| 5521 |
secondary index may contain a column prefix |
| 5522 |
rather than the full column. Also, as noted |
| 5523 |
in Bug #56680, the column in the secondary |
| 5524 |
index may be in the wrong case, and the |
| 5525 |
authoritative case is in result_rec, the |
| 5526 |
appropriate version of the clustered index record. */ |
| 5527 |
if (!row_sel_store_mysql_rec(buf, prebuilt, result_rec, vrow, true, |
| 5528 |
clust_index, prebuilt->index, offsets, false, |
| 5529 |
nullptr, prebuilt->blob_heap)) { |
| 5530 |
goto next_rec; |
| 5531 |
} |
| 5532 |
} |
| 5533 |
|
| 5534 |
/* TODO: This is for a temporary fix, will be removed later */ |
| 5535 |
/* Check duplicate rec for spatial index. */ |
| 5536 |
if (dict_index_is_spatial(index) && rec_get_deleted_flag(rec, comp) && |
| 5537 |
prebuilt->rtr_info->is_dup) { |
| 5538 |
dtuple_t *clust_row; |
| 5539 |
row_ext_t *ext = nullptr; |
| 5540 |
rtr_mbr_t clust_mbr; |
| 5541 |
rtr_mbr_t index_mbr; |
| 5542 |
ulint *index_offsets; |
| 5543 |
const dtuple_t *index_entry; |
| 5544 |
bool *is_dup_rec = prebuilt->rtr_info->is_dup; |
| 5545 |
|
| 5546 |
*is_dup_rec = false; |
| 5547 |
|
| 5548 |
if (!heap) { |
| 5549 |
heap = mem_heap_create(100, UT_LOCATION_HERE); |
| 5550 |
} |
| 5551 |
|
| 5552 |
clust_row = row_build(ROW_COPY_DATA, clust_index, clust_rec, offsets, |
| 5553 |
nullptr, nullptr, nullptr, &ext, heap); |
| 5554 |
index_entry = row_build_index_entry(clust_row, ext, index, heap); |
| 5555 |
rtr_get_mbr_from_tuple(index_entry, &clust_mbr); |
| 5556 |
|
| 5557 |
index_offsets = rec_get_offsets(rec, index, nullptr, ULINT_UNDEFINED, |
| 5558 |
UT_LOCATION_HERE, &heap); |
| 5559 |
rtr_get_mbr_from_rec(rec, index_offsets, &index_mbr); |
| 5560 |
|
| 5561 |
/* We use mbr_equal_physically() because we are comparing |
| 5562 |
MBR between Clustured Index & Spatial Index to identify duplicates. |
| 5563 |
As during UPDATE we check for MBR being physically equal, check |
| 5564 |
for duplicate should also happen physically.*/ |
| 5565 |
if (mbr_equal_physically(&clust_mbr, &index_mbr)) { |
| 5566 |
ut_ad(!rec_get_deleted_flag(clust_rec, comp)); |
| 5567 |
/* Duplicate because it has the same MBR as the record in PK, |
| 5568 |
but the record in PK is not delete marked, while ours is. */ |
| 5569 |
*is_dup_rec = true; |
| 5570 |
} |
| 5571 |
} |
| 5572 |
} else { |
| 5573 |
result_rec = rec; |
| 5574 |
} |
| 5575 |
|
| 5576 |
/* We found a qualifying record 'result_rec'. At this point, |
| 5577 |
'offsets' are associated with 'result_rec'. */ |
| 5578 |
|
| 5579 |
ut_ad(rec_offs_validate(result_rec, result_rec != rec ? clust_index : index, |
| 5580 |
offsets)); |
| 5581 |
ut_ad(!rec_get_deleted_flag(result_rec, comp)); |
| 5582 |
|
| 5583 |
/* If we cannot prefetch records, we should not have a record buffer. |
| 5584 |
See ha_innobase::ha_is_record_buffer_wanted(). */ |
| 5585 |
ut_ad(prebuilt->can_prefetch_records() || record_buffer == nullptr); |
| 5586 |
|
| 5587 |
/* Decide whether to prefetch extra rows. |
| 5588 |
At this point, the clustered index record is protected |
| 5589 |
by a page latch that was acquired when pcur was positioned. |
| 5590 |
The latch will not be released until mtr_commit(&mtr). */ |
| 5591 |
|
| 5592 |
if (record_buffer != nullptr || |
| 5593 |
((match_mode == ROW_SEL_EXACT || |
| 5594 |
prebuilt->n_rows_fetched >= MYSQL_FETCH_CACHE_THRESHOLD) && |
| 5595 |
prebuilt->can_prefetch_records())) { |
| 5596 |
/* Inside an update, for example, we do not cache rows, |
| 5597 |
since we may use the cursor position to do the actual |
| 5598 |
update, that is why we require ...lock_type == LOCK_NONE. |
| 5599 |
Since we keep space in prebuilt only for the BLOBs of |
| 5600 |
a single row, we cannot cache rows in the case there |
| 5601 |
are BLOBs in the fields to be fetched. In HANDLER (note: |
| 5602 |
the HANDLER statement, not the handler class) we do |
| 5603 |
not cache rows because there the cursor is a scrollable |
| 5604 |
cursor. */ |
| 5605 |
|
| 5606 |
const auto max_rows_to_cache = |
| 5607 |
record_buffer ? record_buffer->max_records() : MYSQL_FETCH_CACHE_SIZE; |
| 5608 |
ut_a(prebuilt->n_fetch_cached < max_rows_to_cache); |
| 5609 |
|
| 5610 |
/* We only convert from InnoDB row format to MySQL row |
| 5611 |
format when ICP is disabled. */ |
| 5612 |
|
| 5613 |
if (!prebuilt->idx_cond) { |
| 5614 |
/* We use next_buf to track the allocation of buffers |
| 5615 |
where we store and enqueue the buffers for our |
| 5616 |
pre-fetch optimisation. |
| 5617 |
|
| 5618 |
If next_buf == 0 then we store the converted record |
| 5619 |
directly into the MySQL record buffer (buf). If it is |
| 5620 |
!= 0 then we allocate a pre-fetch buffer and store the |
| 5621 |
converted record there. |
| 5622 |
|
| 5623 |
If the conversion fails and the MySQL record buffer |
| 5624 |
was not written to then we reset next_buf so that |
| 5625 |
we can re-use the MySQL record buffer in the next |
| 5626 |
iteration. */ |
| 5627 |
byte *prev_buf = next_buf; |
| 5628 |
|
| 5629 |
next_buf = next_buf ? row_sel_fetch_last_buf(prebuilt) : buf; |
| 5630 |
|
| 5631 |
if (!row_sel_store_mysql_rec( |
| 5632 |
next_buf, prebuilt, result_rec, vrow, result_rec != rec, |
| 5633 |
result_rec != rec ? clust_index : index, prebuilt->index, offsets, |
| 5634 |
false, nullptr, prebuilt->blob_heap)) { |
| 5635 |
if (next_buf == buf) { |
| 5636 |
ut_a(prebuilt->n_fetch_cached == 0); |
| 5637 |
next_buf = nullptr; |
| 5638 |
} |
| 5639 |
|
| 5640 |
/* Only fresh inserts may contain incomplete |
| 5641 |
externally stored columns. Pretend that such |
| 5642 |
records do not exist. Such records may only be |
| 5643 |
accessed at the READ UNCOMMITTED isolation |
| 5644 |
level or when rolling back a recovered |
| 5645 |
transaction. Rollback happens at a lower |
| 5646 |
level, not here. */ |
| 5647 |
goto next_rec; |
| 5648 |
} |
| 5649 |
|
| 5650 |
/* If we are filling a server-provided buffer, and the |
| 5651 |
server has pushed down an end range condition, evaluate |
| 5652 |
the condition to prevent that we read too many rows. |
| 5653 |
When scanning a multi-value index, we don't perform the |
| 5654 |
check because we cannot convert the indexed value |
| 5655 |
(single scalar element) into the primary index (virtual) |
| 5656 |
column type (array of values). */ |
| 5657 |
if (record_buffer != nullptr && |
| 5658 |
prebuilt->m_mysql_handler->end_range != nullptr && |
| 5659 |
!(clust_templ_for_sec && index->is_multi_value())) { |
| 5660 |
/* If the end-range condition refers to a |
| 5661 |
virtual column and we are reading from the |
| 5662 |
clustered index, next_buf does not have the |
| 5663 |
value of the virtual column. Get the offsets in |
| 5664 |
the secondary index so that we can read the |
| 5665 |
virtual column from the index. */ |
| 5666 |
if (clust_templ_for_sec && |
| 5667 |
prebuilt->m_mysql_handler->m_virt_gcol_in_end_range) { |
| 5668 |
if (sec_offsets == nullptr) { |
| 5669 |
rec_offs_init(sec_offsets_); |
| 5670 |
sec_offsets = sec_offsets_; |
| 5671 |
} |
| 5672 |
sec_offsets = |
| 5673 |
rec_get_offsets(rec, index, sec_offsets, ULINT_UNDEFINED, |
| 5674 |
UT_LOCATION_HERE, &heap); |
| 5675 |
} |
| 5676 |
|
| 5677 |
if (row_search_end_range_check(next_buf, rec, prebuilt, |
| 5678 |
clust_templ_for_sec, sec_offsets, |
| 5679 |
record_buffer)) { |
| 5680 |
if (next_buf != buf) { |
| 5681 |
record_buffer->remove_last(); |
| 5682 |
} |
| 5683 |
next_buf = prev_buf; |
| 5684 |
err = DB_RECORD_NOT_FOUND; |
| 5685 |
goto normal_return; |
| 5686 |
} |
| 5687 |
} |
| 5688 |
|
| 5689 |
if (next_buf != buf) { |
| 5690 |
row_sel_enqueue_cache_row_for_mysql(next_buf, prebuilt); |
| 5691 |
} |
| 5692 |
} else { |
| 5693 |
row_sel_enqueue_cache_row_for_mysql(buf, prebuilt); |
| 5694 |
} |
| 5695 |
|
| 5696 |
if (prebuilt->n_fetch_cached < max_rows_to_cache) { |
| 5697 |
goto next_rec; |
| 5698 |
} |
| 5699 |
|
| 5700 |
} else { |
| 5701 |
/* We cannot use a record buffer for this scan, so assert that |
| 5702 |
we don't have one. If we have a record buffer here, |
| 5703 |
ha_innobase::is_record_buffer_wanted() should be updated so |
| 5704 |
that a buffer is not allocated unnecessarily. */ |
| 5705 |
ut_ad(record_buffer == nullptr); |
| 5706 |
|
| 5707 |
if (UNIV_UNLIKELY(prebuilt->template_type == ROW_MYSQL_DUMMY_TEMPLATE)) { |
| 5708 |
/* CHECK TABLE: fetch the row */ |
| 5709 |
|
| 5710 |
if (result_rec != rec && !prebuilt->need_to_access_clustered) { |
| 5711 |
/* We used 'offsets' for the clust |
| 5712 |
rec, recalculate them for 'rec' */ |
| 5713 |
offsets = rec_get_offsets(rec, index, offsets, ULINT_UNDEFINED, |
| 5714 |
UT_LOCATION_HERE, &heap); |
| 5715 |
result_rec = rec; |
| 5716 |
} |
| 5717 |
|
| 5718 |
memcpy(buf + 4, result_rec - rec_offs_extra_size(offsets), |
| 5719 |
rec_offs_size(offsets)); |
| 5720 |
mach_write_to_4(buf, rec_offs_extra_size(offsets) + 4); |
| 5721 |
} else if (!prebuilt->idx_cond && !prebuilt->innodb_api) { |
| 5722 |
/* The record was not yet converted to MySQL format. */ |
| 5723 |
if (!row_sel_store_mysql_rec( |
| 5724 |
buf, prebuilt, result_rec, vrow, result_rec != rec, |
| 5725 |
result_rec != rec ? clust_index : index, prebuilt->index, offsets, |
| 5726 |
false, prebuilt->get_lob_undo(), prebuilt->blob_heap)) { |
| 5727 |
/* Only fresh inserts may contain |
| 5728 |
incomplete externally stored |
| 5729 |
columns. Pretend that such records do |
| 5730 |
not exist. Such records may only be |
| 5731 |
accessed at the READ UNCOMMITTED |
| 5732 |
isolation level or when rolling back a |
| 5733 |
recovered transaction. Rollback |
| 5734 |
happens at a lower level, not here. */ |
| 5735 |
goto next_rec; |
| 5736 |
} |
| 5737 |
} |
| 5738 |
|
| 5739 |
if (prebuilt->clust_index_was_generated) { |
| 5740 |
row_sel_store_row_id_to_prebuilt(prebuilt, result_rec, |
| 5741 |
result_rec == rec ? index : clust_index, |
| 5742 |
offsets); |
| 5743 |
} |
| 5744 |
} |
| 5745 |
|
| 5746 |
/* From this point on, 'offsets' are invalid. */ |
| 5747 |
|
| 5748 |
/* We have an optimization to save CPU time: if this is a consistent |
| 5749 |
read on a unique condition on the clustered index, then we do not |
| 5750 |
store the pcur position, because any fetch next or prev will anyway |
| 5751 |
return 'end of file'. Exceptions are locking reads and the MySQL |
| 5752 |
HANDLER command where the user can move the cursor with PREV or NEXT |
| 5753 |
even after a unique search. */ |
| 5754 |
|
| 5755 |
err = DB_SUCCESS; |
| 5756 |
|
| 5757 |
idx_cond_failed: |
| 5758 |
if (!unique_search || !index->is_clustered() || direction != 0 || |
| 5759 |
prebuilt->select_lock_type != LOCK_NONE || prebuilt->used_in_HANDLER || |
| 5760 |
prebuilt->innodb_api) { |
| 5761 |
/* Inside an update always store the cursor position */ |
| 5762 |
|
| 5763 |
if (!spatial_search) { |
| 5764 |
pcur->store_position(&mtr); |
| 5765 |
} |
| 5766 |
|
| 5767 |
if (prebuilt->innodb_api && (pcur->get_rec() != result_rec)) { |
| 5768 |
ulint rec_size = rec_offs_size(offsets); |
| 5769 |
if (!prebuilt->innodb_api_rec_size || |
| 5770 |
(prebuilt->innodb_api_rec_size < rec_size)) { |
| 5771 |
prebuilt->innodb_api_buf = static_cast<byte *>( |
| 5772 |
mem_heap_alloc(prebuilt->cursor_heap, rec_size)); |
| 5773 |
prebuilt->innodb_api_rec_size = rec_size; |
| 5774 |
} |
| 5775 |
prebuilt->innodb_api_rec = |
| 5776 |
rec_copy(prebuilt->innodb_api_buf, result_rec, offsets); |
| 5777 |
} |
| 5778 |
} |
| 5779 |
|
| 5780 |
goto normal_return; |
| 5781 |
|
| 5782 |
next_rec: |
| 5783 |
|
| 5784 |
if (end_loop >= 99 && need_vrow && vrow == nullptr && prev_rec != nullptr) { |
| 5785 |
if (!heap) { |
| 5786 |
heap = mem_heap_create(100, UT_LOCATION_HERE); |
| 5787 |
} |
| 5788 |
|
| 5789 |
prev_vrow = nullptr; |
| 5790 |
row_sel_fill_vrow(prev_rec, index, &prev_vrow, heap); |
| 5791 |
} else { |
| 5792 |
prev_vrow = vrow; |
| 5793 |
} |
| 5794 |
|
| 5795 |
end_loop++; |
| 5796 |
|
| 5797 |
/* Reset the old and new "did semi-consistent read" flags. */ |
| 5798 |
if (UNIV_UNLIKELY(prebuilt->row_read_type == ROW_READ_DID_SEMI_CONSISTENT)) { |
| 5799 |
prebuilt->row_read_type = ROW_READ_TRY_SEMI_CONSISTENT; |
| 5800 |
} |
| 5801 |
did_semi_consistent_read = false; |
| 5802 |
prebuilt->new_rec_lock.reset(); |
| 5803 |
|
| 5804 |
vrow = nullptr; |
| 5805 |
|
| 5806 |
/*-------------------------------------------------------------*/ |
| 5807 |
/* PHASE 5: Move the cursor to the next index record */ |
| 5808 |
|
| 5809 |
/* NOTE: For moves_up==false, the mini-transaction will be |
| 5810 |
committed and restarted every time when switching b-tree |
| 5811 |
pages. For moves_up==true in index condition pushdown, we can |
| 5812 |
scan an entire secondary index tree within a single |
| 5813 |
mini-transaction. As long as the prebuilt->idx_cond does not |
| 5814 |
match, we do not need to consult the clustered index or |
| 5815 |
return records to MySQL, and thus we can avoid repositioning |
| 5816 |
the cursor. What prevents us from buffer-fixing all leaf pages |
| 5817 |
within the mini-transaction is the btr_leaf_page_release() |
| 5818 |
call in btr_pcur::move_to_next_page(). Only the leaf page where |
| 5819 |
the cursor is positioned will remain buffer-fixed. |
| 5820 |
For R-tree spatial search, we also commit the mini-transaction |
| 5821 |
each time */ |
| 5822 |
|
| 5823 |
if (mtr_has_extra_clust_latch || spatial_search) { |
| 5824 |
/* If we have extra cluster latch, we must commit |
| 5825 |
mtr if we are moving to the next non-clustered |
| 5826 |
index record, because we could break the latching |
| 5827 |
order if we would access a different clustered |
| 5828 |
index page right away without releasing the previous. */ |
| 5829 |
|
| 5830 |
bool is_pcur_rec = (pcur->get_rec() == prev_rec); |
| 5831 |
|
| 5832 |
/* No need to do store restore for R-tree */ |
| 5833 |
if (!spatial_search) { |
| 5834 |
pcur->store_position(&mtr); |
| 5835 |
} |
| 5836 |
|
| 5837 |
mtr_commit(&mtr); |
| 5838 |
mtr_has_extra_clust_latch = false; |
| 5839 |
|
| 5840 |
DEBUG_SYNC_C("row_search_before_mtr_restart_for_extra_clust"); |
| 5841 |
|
| 5842 |
mtr_start(&mtr); |
| 5843 |
|
| 5844 |
if (!spatial_search) { |
| 5845 |
const auto result = sel_restore_position_for_mysql( |
| 5846 |
&same_user_rec, BTR_SEARCH_LEAF, pcur, moves_up, &mtr); |
| 5847 |
|
| 5848 |
if (result) { |
| 5849 |
prev_rec = nullptr; |
| 5850 |
goto rec_loop; |
| 5851 |
} |
| 5852 |
|
| 5853 |
ut_ad(same_user_rec); |
| 5854 |
|
| 5855 |
if (is_pcur_rec && pcur->get_rec() != prev_rec) { |
| 5856 |
/* prev_rec is invalid. */ |
| 5857 |
prev_rec = nullptr; |
| 5858 |
} |
| 5859 |
|
| 5860 |
#ifdef UNIV_DEBUG |
| 5861 |
if (prev_rec != nullptr && prev_rec_debug != nullptr) { |
| 5862 |
const ulint *offsets1; |
| 5863 |
const ulint *offsets2; |
| 5864 |
|
| 5865 |
auto heap_tmp = mem_heap_create(256, UT_LOCATION_HERE); |
| 5866 |
|
| 5867 |
offsets1 = rec_get_offsets(prev_rec_debug, index, nullptr, |
| 5868 |
prev_rec_debug_n_fields, UT_LOCATION_HERE, |
| 5869 |
&heap_tmp); |
| 5870 |
|
| 5871 |
offsets2 = |
| 5872 |
rec_get_offsets(prev_rec, index, nullptr, prev_rec_debug_n_fields, |
| 5873 |
UT_LOCATION_HERE, &heap_tmp); |
| 5874 |
|
| 5875 |
ut_ad(!cmp_rec_rec(prev_rec_debug, prev_rec, offsets1, offsets2, index, |
| 5876 |
page_is_spatial_non_leaf(prev_rec, index), nullptr, |
| 5877 |
false)); |
| 5878 |
mem_heap_free(heap_tmp); |
| 5879 |
} |
| 5880 |
#endif /* UNIV_DEBUG */ |
| 5881 |
} |
| 5882 |
} |
| 5883 |
|
| 5884 |
if (moves_up) { |
| 5885 |
bool move; |
| 5886 |
|
| 5887 |
if (spatial_search) { |
| 5888 |
move = rtr_pcur_move_to_next(search_tuple, mode, prebuilt->select_mode, |
| 5889 |
pcur, 0, &mtr); |
| 5890 |
} else { |
| 5891 |
move = pcur->move_to_next(&mtr); |
| 5892 |
} |
| 5893 |
|
| 5894 |
if (!move) { |
| 5895 |
not_moved: |
| 5896 |
if (!spatial_search) { |
| 5897 |
pcur->store_position(&mtr); |
| 5898 |
} |
| 5899 |
|
| 5900 |
if (match_mode != 0) { |
| 5901 |
err = DB_RECORD_NOT_FOUND; |
| 5902 |
} else { |
| 5903 |
err = DB_END_OF_INDEX; |
| 5904 |
} |
| 5905 |
|
| 5906 |
goto normal_return; |
| 5907 |
} |
| 5908 |
} else { |
| 5909 |
if (UNIV_UNLIKELY(!pcur->move_to_prev(&mtr))) { |
| 5910 |
goto not_moved; |
| 5911 |
} |
| 5912 |
} |
| 5913 |
|
| 5914 |
goto rec_loop; |
| 5915 |
|
| 5916 |
lock_wait_or_error: |
| 5917 |
/* Reset the old and new "did semi-consistent read" flags. */ |
| 5918 |
if (UNIV_UNLIKELY(prebuilt->row_read_type == ROW_READ_DID_SEMI_CONSISTENT)) { |
| 5919 |
prebuilt->row_read_type = ROW_READ_TRY_SEMI_CONSISTENT; |
| 5920 |
} |
| 5921 |
did_semi_consistent_read = false; |
| 5922 |
|
| 5923 |
/*-------------------------------------------------------------*/ |
| 5924 |
if (!dict_index_is_spatial(index)) { |
| 5925 |
pcur->store_position(&mtr); |
| 5926 |
} |
| 5927 |
|
| 5928 |
lock_table_wait: |
| 5929 |
mtr_commit(&mtr); |
| 5930 |
mtr_has_extra_clust_latch = false; |
| 5931 |
|
| 5932 |
trx->error_state = err; |
| 5933 |
|
| 5934 |
/* The following is a patch for MySQL */ |
| 5935 |
|
| 5936 |
if (thr->is_active) { |
| 5937 |
que_thr_stop_for_mysql(thr); |
| 5938 |
} |
| 5939 |
|
| 5940 |
thr->lock_state = QUE_THR_LOCK_ROW; |
| 5941 |
|
| 5942 |
if (row_mysql_handle_errors(&err, trx, thr, nullptr)) { |
| 5943 |
/* It was a lock wait, and it ended */ |
| 5944 |
|
| 5945 |
thr->lock_state = QUE_THR_LOCK_NOLOCK; |
| 5946 |
mtr_start(&mtr); |
| 5947 |
|
| 5948 |
/* Table lock waited, go try to obtain table lock |
| 5949 |
again */ |
| 5950 |
if (table_lock_waited) { |
| 5951 |
table_lock_waited = false; |
| 5952 |
|
| 5953 |
goto wait_table_again; |
| 5954 |
} |
| 5955 |
|
| 5956 |
if (!dict_index_is_spatial(index)) { |
| 5957 |
sel_restore_position_for_mysql(&same_user_rec, BTR_SEARCH_LEAF, pcur, |
| 5958 |
moves_up, &mtr); |
| 5959 |
prev_rec = nullptr; |
| 5960 |
} |
| 5961 |
|
| 5962 |
if (!same_user_rec && trx->releases_non_matching_rows()) { |
| 5963 |
/* Since we were not able to restore the cursor |
| 5964 |
on the same user record, we cannot use |
| 5965 |
row_prebuilt_t::try_unlock() to unlock any records, and |
| 5966 |
we must thus reset the new rec lock info. Since |
| 5967 |
in lock0lock.cc we have blocked the inheriting of gap |
| 5968 |
X-locks, we actually do not have any new record locks |
| 5969 |
set in this case. |
| 5970 |
|
| 5971 |
Note that if we were able to restore on the 'same' |
| 5972 |
user record, it is still possible that we were actually |
| 5973 |
waiting on a delete-marked record, and meanwhile |
| 5974 |
it was removed by purge and inserted again by some |
| 5975 |
other user. But that is no problem, because in |
| 5976 |
rec_loop we will again try to set a lock, and |
| 5977 |
new_rec_lock_info in trx will be right at the end. */ |
| 5978 |
|
| 5979 |
prebuilt->new_rec_lock.reset(); |
| 5980 |
} |
| 5981 |
|
| 5982 |
mode = pcur->m_search_mode; |
| 5983 |
|
| 5984 |
goto rec_loop; |
| 5985 |
} |
| 5986 |
|
| 5987 |
thr->lock_state = QUE_THR_LOCK_NOLOCK; |
| 5988 |
|
| 5989 |
goto func_exit; |
| 5990 |
|
| 5991 |
normal_return: |
| 5992 |
/*-------------------------------------------------------------*/ |
| 5993 |
que_thr_stop_for_mysql_no_error(thr, trx); |
| 5994 |
|
| 5995 |
mtr_commit(&mtr); |
| 5996 |
|
| 5997 |
/* Rollback blocking transactions from hit list for high priority |
| 5998 |
transaction, if any. We should not be holding latches here as |
| 5999 |
we are going to rollback the blocking transactions. */ |
| 6000 |
trx_kill_blocking(trx); |
| 6001 |
|
| 6002 |
DEBUG_SYNC_C("row_search_for_mysql_before_return"); |
| 6003 |
|
| 6004 |
if (prebuilt->idx_cond != 0) { |
| 6005 |
/* When ICP is active we don't write to the MySQL buffer |
| 6006 |
directly, only to buffers that are enqueued in the pre-fetch |
| 6007 |
queue. We need to dequeue the first buffer and copy the contents |
| 6008 |
to the record buffer that was passed in by MySQL. */ |
| 6009 |
|
| 6010 |
if (prebuilt->n_fetch_cached > 0) { |
| 6011 |
row_sel_dequeue_cached_row_for_mysql(buf, prebuilt); |
| 6012 |
err = DB_SUCCESS; |
| 6013 |
} |
| 6014 |
|
| 6015 |
} else if (next_buf != nullptr) { |
| 6016 |
/* We may or may not have enqueued some buffers to the |
| 6017 |
pre-fetch queue, but we definitely wrote to the record |
| 6018 |
buffer passed to use by MySQL. */ |
| 6019 |
|
| 6020 |
DEBUG_SYNC_C("row_search_cached_row"); |
| 6021 |
err = DB_SUCCESS; |
| 6022 |
} |
| 6023 |
|
| 6024 |
#ifdef UNIV_DEBUG |
| 6025 |
if (dict_index_is_spatial(index) && err != DB_SUCCESS && |
| 6026 |
err != DB_END_OF_INDEX && err != DB_INTERRUPTED) { |
| 6027 |
rtr_node_path_t *path = pcur->m_btr_cur.rtr_info->path; |
| 6028 |
|
| 6029 |
ut_ad(path->empty()); |
| 6030 |
} |
| 6031 |
#endif |
| 6032 |
|
| 6033 |
func_exit: |
| 6034 |
trx->op_info = ""; |
| 6035 |
|
| 6036 |
if (end_range_cache != nullptr) { |
| 6037 |
ut::free(end_range_cache); |
| 6038 |
} |
| 6039 |
|
| 6040 |
if (heap != nullptr) { |
| 6041 |
mem_heap_free(heap); |
| 6042 |
} |
| 6043 |
|
| 6044 |
#ifdef UNIV_DEBUG |
| 6045 |
if (prev_rec_debug_buf != nullptr) { |
| 6046 |
ut::free(prev_rec_debug_buf); |
| 6047 |
} |
| 6048 |
#endif /* UNIV_DEBUG */ |
| 6049 |
|
| 6050 |
/* Set or reset the "did semi-consistent read" flag on return. |
| 6051 |
The flag did_semi_consistent_read is set if and only if |
| 6052 |
the record being returned was fetched with a semi-consistent read. */ |
| 6053 |
ut_ad(prebuilt->row_read_type != ROW_READ_WITH_LOCKS || |
| 6054 |
!did_semi_consistent_read); |
| 6055 |
|
| 6056 |
if (prebuilt->row_read_type != ROW_READ_WITH_LOCKS) { |
| 6057 |
if (did_semi_consistent_read) { |
| 6058 |
prebuilt->row_read_type = ROW_READ_DID_SEMI_CONSISTENT; |
| 6059 |
} else { |
| 6060 |
prebuilt->row_read_type = ROW_READ_TRY_SEMI_CONSISTENT; |
| 6061 |
} |
| 6062 |
} |
| 6063 |
|
| 6064 |
#ifdef UNIV_DEBUG |
| 6065 |
{ |
| 6066 |
btrsea_sync_check check(trx->has_search_latch); |
| 6067 |
|
| 6068 |
ut_ad(!sync_check_iterate(check)); |
| 6069 |
} |
| 6070 |
#endif /* UNIV_DEBUG */ |
| 6071 |
|
| 6072 |
DEBUG_SYNC_C("innodb_row_search_for_mysql_exit"); |
| 6073 |
|
| 6074 |
prebuilt->lob_undo_reset(); |
| 6075 |
|
| 6076 |
ut_a(!trx->has_search_latch); |
| 6077 |
|
| 6078 |
return err; |
| 6079 |
} |
| 6080 |
|
| 6081 |
/** Count rows in a R-Tree leaf level. |
| 6082 |
@return DB_SUCCESS if successful */ |
| 6083 |
dberr_t row_count_rtree_recs( |
| 6084 |
row_prebuilt_t *prebuilt, /*!< in: prebuilt struct for the |
| 6085 |
table handle; this contains the info |
| 6086 |
of search_tuple, index; if search |
| 6087 |
tuple contains 0 fields then we |
| 6088 |
position the cursor at the start or |
| 6089 |
the end of the index, depending on |
| 6090 |
'mode' */ |
| 6091 |
ulint *n_rows, /*!< out: number of entries |
| 6092 |
seen in the consistent read */ |
| 6093 |
ulint *n_dups) /*!< out: number of dup entries */ |
| 6094 |
{ |
| 6095 |
dict_index_t *index = prebuilt->index; |
| 6096 |
dberr_t ret = DB_SUCCESS; |
| 6097 |
mtr_t mtr; |
| 6098 |
mem_heap_t *heap; |
| 6099 |
dtuple_t *entry; |
| 6100 |
dtuple_t *search_entry = prebuilt->search_tuple; |
| 6101 |
ulint entry_len; |
| 6102 |
ulint i; |
| 6103 |
byte *buf; |
| 6104 |
bool is_dup = false; |
| 6105 |
|
| 6106 |
ut_a(dict_index_is_spatial(index)); |
| 6107 |
|
| 6108 |
*n_rows = 0; |
| 6109 |
*n_dups = 0; |
| 6110 |
|
| 6111 |
heap = mem_heap_create(256, UT_LOCATION_HERE); |
| 6112 |
|
| 6113 |
/* Build a search tuple. */ |
| 6114 |
entry_len = dict_index_get_n_fields(index); |
| 6115 |
entry = dtuple_create(heap, entry_len); |
| 6116 |
|
| 6117 |
for (i = 0; i < entry_len; i++) { |
| 6118 |
const dict_field_t *ind_field = index->get_field(i); |
| 6119 |
const dict_col_t *col = ind_field->col; |
| 6120 |
dfield_t *dfield = dtuple_get_nth_field(entry, i); |
| 6121 |
|
| 6122 |
if (i == 0) { |
| 6123 |
double *mbr; |
| 6124 |
double tmp_mbr[SPDIMS * 2]; |
| 6125 |
|
| 6126 |
dfield->type.mtype = DATA_GEOMETRY; |
| 6127 |
dfield->type.prtype |= DATA_GIS_MBR; |
| 6128 |
|
| 6129 |
/* Allocate memory for mbr field */ |
| 6130 |
mbr = static_cast<double *>(mem_heap_alloc(heap, DATA_MBR_LEN)); |
| 6131 |
|
| 6132 |
/* Set mbr field data. */ |
| 6133 |
dfield_set_data(dfield, mbr, DATA_MBR_LEN); |
| 6134 |
|
| 6135 |
for (uint j = 0; j < SPDIMS; j++) { |
| 6136 |
tmp_mbr[j * 2] = DBL_MAX; |
| 6137 |
tmp_mbr[j * 2 + 1] = -DBL_MAX; |
| 6138 |
} |
| 6139 |
dfield_write_mbr(dfield, tmp_mbr); |
| 6140 |
continue; |
| 6141 |
} |
| 6142 |
|
| 6143 |
dfield->type.mtype = col->mtype; |
| 6144 |
dfield->type.prtype = col->prtype; |
| 6145 |
} |
| 6146 |
|
| 6147 |
prebuilt->search_tuple = entry; |
| 6148 |
|
| 6149 |
ulint bufsize = std::max(UNIV_PAGE_SIZE, prebuilt->mysql_row_len); |
| 6150 |
buf = static_cast<byte *>( |
| 6151 |
ut::malloc_withkey(UT_NEW_THIS_FILE_PSI_KEY, bufsize)); |
| 6152 |
|
| 6153 |
ulint cnt = 1000; |
| 6154 |
|
| 6155 |
ut_ad(!index->table->is_intrinsic()); |
| 6156 |
|
| 6157 |
ret = row_search_mvcc(buf, PAGE_CUR_WITHIN, prebuilt, 0, 0); |
| 6158 |
|
| 6159 |
prebuilt->rtr_info->is_dup = &is_dup; |
| 6160 |
|
| 6161 |
loop: |
| 6162 |
/* Check thd->killed every 1,000 scanned rows */ |
| 6163 |
if (--cnt == 0) { |
| 6164 |
if (trx_is_interrupted(prebuilt->trx)) { |
| 6165 |
ret = DB_INTERRUPTED; |
| 6166 |
goto func_exit; |
| 6167 |
} |
| 6168 |
cnt = 1000; |
| 6169 |
} |
| 6170 |
|
| 6171 |
switch (ret) { |
| 6172 |
case DB_SUCCESS: |
| 6173 |
break; |
| 6174 |
case DB_DEADLOCK: |
| 6175 |
case DB_LOCK_TABLE_FULL: |
| 6176 |
case DB_LOCK_WAIT_TIMEOUT: |
| 6177 |
case DB_INTERRUPTED: |
| 6178 |
goto func_exit; |
| 6179 |
default: |
| 6180 |
/* fall through (this error is ignored by CHECK TABLE) */ |
| 6181 |
case DB_END_OF_INDEX: |
| 6182 |
ret = DB_SUCCESS; |
| 6183 |
func_exit: |
| 6184 |
/* This may be pointing to a local variable. */ |
| 6185 |
prebuilt->rtr_info->is_dup = nullptr; |
| 6186 |
|
| 6187 |
prebuilt->search_tuple = search_entry; |
| 6188 |
ut::free(buf); |
| 6189 |
mem_heap_free(heap); |
| 6190 |
|
| 6191 |
return (ret); |
| 6192 |
} |
| 6193 |
|
| 6194 |
*n_rows = *n_rows + 1; |
| 6195 |
if (is_dup) { |
| 6196 |
*n_dups = *n_dups + 1; |
| 6197 |
is_dup = false; |
| 6198 |
} |
| 6199 |
|
| 6200 |
ret = row_search_mvcc(buf, PAGE_CUR_WITHIN, prebuilt, 0, ROW_SEL_NEXT); |
| 6201 |
|
| 6202 |
goto loop; |
| 6203 |
} |
| 6204 |
|
| 6205 |
/** Read the AUTOINC column from the current row. If the value is less than |
| 6206 |
0 and the type is not unsigned then we reset the value to 0. |
| 6207 |
@return value read from the column */ |
| 6208 |
static uint64_t row_search_autoinc_read_column( |
| 6209 |
dict_index_t *index, /*!< in: index to read from */ |
| 6210 |
const rec_t *rec, /*!< in: current rec */ |
| 6211 |
ulint col_no, /*!< in: column number */ |
| 6212 |
ulint mtype, /*!< in: column main type */ |
| 6213 |
bool unsigned_type) /*!< in: signed or unsigned flag */ |
| 6214 |
{ |
| 6215 |
ulint len; |
| 6216 |
const byte *data; |
| 6217 |
uint64_t value; |
| 6218 |
mem_heap_t *heap = nullptr; |
| 6219 |
ulint offsets_[REC_OFFS_NORMAL_SIZE]; |
| 6220 |
ulint *offsets = offsets_; |
| 6221 |
|
| 6222 |
rec_offs_init(offsets_); |
| 6223 |
|
| 6224 |
offsets = |
| 6225 |
rec_get_offsets(rec, index, offsets, col_no + 1, UT_LOCATION_HERE, &heap); |
| 6226 |
|
| 6227 |
if (rec_offs_nth_sql_null(index, offsets, col_no)) { |
| 6228 |
/* There is no non-NULL value in the auto-increment column. */ |
| 6229 |
value = 0; |
| 6230 |
goto func_exit; |
| 6231 |
} |
| 6232 |
|
| 6233 |
data = rec_get_nth_field(index, rec, offsets, col_no, &len); |
| 6234 |
|
| 6235 |
value = row_parse_int(data, len, mtype, unsigned_type); |
| 6236 |
|
| 6237 |
func_exit: |
| 6238 |
if (UNIV_LIKELY_NULL(heap)) { |
| 6239 |
mem_heap_free(heap); |
| 6240 |
} |
| 6241 |
|
| 6242 |
return (value); |
| 6243 |
} |
| 6244 |
|
| 6245 |
/** Get the maximum and non-delete-marked record in an index. |
| 6246 |
@param[in] index Index tree |
| 6247 |
@param[in,out] mtr Mini-transaction (may be committed and restarted) |
| 6248 |
@return maximum record, page s-latched in mtr |
| 6249 |
@retval NULL if there are no records, or if all of them are delete-marked */ |
| 6250 |
static const rec_t *row_search_get_max_rec(dict_index_t *index, mtr_t *mtr) { |
| 6251 |
btr_pcur_t pcur; |
| 6252 |
const rec_t *rec; |
| 6253 |
|
| 6254 |
/* Open at the high/right end (false), and init cursor */ |
| 6255 |
pcur.open_at_side(false, index, BTR_SEARCH_LEAF, true, 0, mtr); |
| 6256 |
|
| 6257 |
do { |
| 6258 |
const page_t *page; |
| 6259 |
|
| 6260 |
page = pcur.get_page(); |
| 6261 |
rec = page_find_rec_last_not_deleted(page); |
| 6262 |
|
| 6263 |
if (page_rec_is_user_rec(rec)) { |
| 6264 |
break; |
| 6265 |
} else { |
| 6266 |
rec = nullptr; |
| 6267 |
} |
| 6268 |
pcur.move_before_first_on_page(); |
| 6269 |
} while (pcur.move_to_prev(mtr)); |
| 6270 |
|
| 6271 |
pcur.close(); |
| 6272 |
|
| 6273 |
return (rec); |
| 6274 |
} |
| 6275 |
|
| 6276 |
/** Read the max AUTOINC value from an index. |
| 6277 |
@return DB_SUCCESS if all OK else error code, DB_RECORD_NOT_FOUND if |
| 6278 |
column name can't be found in index */ |
| 6279 |
dberr_t row_search_max_autoinc( |
| 6280 |
dict_index_t *index, /*!< in: index to search */ |
| 6281 |
const char *col_name, /*!< in: name of autoinc column */ |
| 6282 |
uint64_t *value) /*!< out: AUTOINC value read */ |
| 6283 |
{ |
| 6284 |
dict_field_t *dfield = index->get_field(0); |
| 6285 |
dberr_t error = DB_SUCCESS; |
| 6286 |
*value = 0; |
| 6287 |
|
| 6288 |
if (strcmp(col_name, dfield->name) != 0) { |
| 6289 |
error = DB_RECORD_NOT_FOUND; |
| 6290 |
} else { |
| 6291 |
mtr_t mtr; |
| 6292 |
const rec_t *rec; |
| 6293 |
|
| 6294 |
mtr_start(&mtr); |
| 6295 |
|
| 6296 |
rec = row_search_get_max_rec(index, &mtr); |
| 6297 |
|
| 6298 |
if (rec != nullptr) { |
| 6299 |
bool unsigned_type = (dfield->col->prtype & DATA_UNSIGNED) != 0; |
| 6300 |
|
| 6301 |
*value = row_search_autoinc_read_column(index, rec, 0, dfield->col->mtype, |
| 6302 |
unsigned_type); |
| 6303 |
} |
| 6304 |
|
| 6305 |
mtr_commit(&mtr); |
| 6306 |
} |
| 6307 |
|
| 6308 |
return (error); |
| 6309 |
} |
| 6310 |
|
| 6311 |
/** Convert the innodb_table_stats clustered index record to |
| 6312 |
table_stats format. |
| 6313 |
@param[in] clust_rec clustered index record |
| 6314 |
@param[in] clust_index clustered index |
| 6315 |
@param[in] clust_offsets offsets of the clustered index |
| 6316 |
record |
| 6317 |
@param[out] tbl_stats table_stats information |
| 6318 |
to be filled. */ |
| 6319 |
static void convert_to_table_stats_record(rec_t *clust_rec, |
| 6320 |
dict_index_t *clust_index, |
| 6321 |
ulint *clust_offsets, |
| 6322 |
TableStatsRecord &tbl_stats) { |
| 6323 |
for (ulint i = 0; i < rec_offs_n_fields(clust_offsets); i++) { |
| 6324 |
const byte *data; |
| 6325 |
ulint len; |
| 6326 |
data = rec_get_nth_field(clust_index, clust_rec, clust_offsets, i, &len); |
| 6327 |
|
| 6328 |
if (len == UNIV_SQL_NULL) { |
| 6329 |
continue; |
| 6330 |
} |
| 6331 |
|
| 6332 |
tbl_stats.set_data(data, i, len); |
| 6333 |
} |
| 6334 |
} |
| 6335 |
|
| 6336 |
/** Search the record present in innodb_table_stats table using |
| 6337 |
db_name, table_name and fill it in table stats structure. |
| 6338 |
@param[in] db_name database name |
| 6339 |
@param[in] tbl_name table name |
| 6340 |
@param[out] table_stats stats table structure. |
| 6341 |
@return true if successful else false. */ |
| 6342 |
bool row_search_table_stats(const char *db_name, const char *tbl_name, |
| 6343 |
TableStatsRecord &table_stats) { |
| 6344 |
mtr_t mtr; |
| 6345 |
btr_pcur_t pcur; |
| 6346 |
rec_t *rec; |
| 6347 |
bool move = true; |
| 6348 |
ulint *offsets; |
| 6349 |
dict_table_t *table = dict_sys->table_stats; |
| 6350 |
dict_index_t *clust_index = table->first_index(); |
| 6351 |
dtuple_t *dtuple; |
| 6352 |
dfield_t *dfield; |
| 6353 |
bool found_rec = false; |
| 6354 |
mem_heap_t *heap = mem_heap_create(100, UT_LOCATION_HERE); |
| 6355 |
|
| 6356 |
dtuple = dtuple_create(heap, clust_index->n_uniq); |
| 6357 |
dict_index_copy_types(dtuple, clust_index, clust_index->n_uniq); |
| 6358 |
|
| 6359 |
dfield = dtuple_get_nth_field(dtuple, TableStatsRecord::DB_NAME_COL_NO); |
| 6360 |
dfield_set_data(dfield, db_name, strlen(db_name)); |
| 6361 |
|
| 6362 |
dfield = dtuple_get_nth_field(dtuple, TableStatsRecord::TABLE_NAME_COL_NO); |
| 6363 |
dfield_set_data(dfield, tbl_name, strlen(tbl_name)); |
| 6364 |
|
| 6365 |
mtr_start(&mtr); |
| 6366 |
pcur.open_no_init(clust_index, dtuple, PAGE_CUR_GE, BTR_SEARCH_LEAF, 0, &mtr, |
| 6367 |
UT_LOCATION_HERE); |
| 6368 |
|
| 6369 |
for (; move == true; move = pcur.move_to_next(&mtr)) { |
| 6370 |
rec = pcur.get_rec(); |
| 6371 |
offsets = rec_get_offsets(rec, clust_index, nullptr, ULINT_UNDEFINED, |
| 6372 |
UT_LOCATION_HERE, &heap); |
| 6373 |
|
| 6374 |
if (page_rec_is_infimum(rec) || page_rec_is_supremum(rec)) { |
| 6375 |
continue; |
| 6376 |
} |
| 6377 |
|
| 6378 |
if (0 != cmp_dtuple_rec(dtuple, rec, clust_index, offsets)) { |
| 6379 |
break; |
| 6380 |
} |
| 6381 |
|
| 6382 |
if (rec_get_deleted_flag(rec, dict_table_is_comp(table))) { |
| 6383 |
continue; |
| 6384 |
} |
| 6385 |
|
| 6386 |
found_rec = true; |
| 6387 |
convert_to_table_stats_record(rec, clust_index, offsets, table_stats); |
| 6388 |
} |
| 6389 |
|
| 6390 |
mtr_commit(&mtr); |
| 6391 |
mem_heap_free(heap); |
| 6392 |
return (found_rec); |
| 6393 |
} |
| 6394 |
|
| 6395 |
/** Search the record present in innodb_index_stats using |
| 6396 |
db_name, table name and index_name and fill the |
| 6397 |
cardinality for the each column. |
| 6398 |
@param[in] db_name database name |
| 6399 |
@param[in] tbl_name table name |
| 6400 |
@param[in] index_name index name |
| 6401 |
@param[in] col_offset offset of the column in the index |
| 6402 |
@param[out] cardinality cardinality of the column. |
| 6403 |
@return true if successful else false. */ |
| 6404 |
bool row_search_index_stats(const char *db_name, const char *tbl_name, |
| 6405 |
const char *index_name, ulint col_offset, |
| 6406 |
ulonglong *cardinality) { |
| 6407 |
mtr_t mtr; |
| 6408 |
btr_pcur_t pcur; |
| 6409 |
rec_t *rec; |
| 6410 |
bool move = true; |
| 6411 |
ulint *offsets; |
| 6412 |
dict_table_t *table = dict_sys->index_stats; |
| 6413 |
dict_index_t *clust_index = table->first_index(); |
| 6414 |
dtuple_t *dtuple; |
| 6415 |
dfield_t *dfield; |
| 6416 |
mem_heap_t *heap = mem_heap_create(100, UT_LOCATION_HERE); |
| 6417 |
ulint n_recs = 0; |
| 6418 |
|
| 6419 |
/** Number of fields to search in the table. */ |
| 6420 |
static constexpr unsigned N_SEARCH_FIELDS = 3; |
| 6421 |
/** Column number of innodb_index_stats.database_name. */ |
| 6422 |
static constexpr unsigned DB_NAME_COL_NO = 0; |
| 6423 |
/** Column number of innodb_index_stats.table_name. */ |
| 6424 |
static constexpr unsigned TABLE_NAME_COL_NO = 1; |
| 6425 |
/** Column number of innodb_index_stats.index_name. */ |
| 6426 |
static constexpr unsigned INDEX_NAME_COL_NO = 2; |
| 6427 |
/** Column number of innodb_index_stats.stat_value. */ |
| 6428 |
static constexpr unsigned STAT_VALUE_COL_NO = 5; |
| 6429 |
|
| 6430 |
ulint cardinality_index_offset = clust_index->get_col_pos(STAT_VALUE_COL_NO); |
| 6431 |
|
| 6432 |
/** Search the innodb_index_stats table using |
| 6433 |
(database_name, table_name, index_name). */ |
| 6434 |
dtuple = dtuple_create(heap, N_SEARCH_FIELDS); |
| 6435 |
dict_index_copy_types(dtuple, clust_index, N_SEARCH_FIELDS); |
| 6436 |
|
| 6437 |
dfield = dtuple_get_nth_field(dtuple, DB_NAME_COL_NO); |
| 6438 |
dfield_set_data(dfield, db_name, strlen(db_name)); |
| 6439 |
|
| 6440 |
dfield = dtuple_get_nth_field(dtuple, TABLE_NAME_COL_NO); |
| 6441 |
dfield_set_data(dfield, tbl_name, strlen(tbl_name)); |
| 6442 |
|
| 6443 |
dfield = dtuple_get_nth_field(dtuple, INDEX_NAME_COL_NO); |
| 6444 |
dfield_set_data(dfield, index_name, strlen(index_name)); |
| 6445 |
|
| 6446 |
mtr_start(&mtr); |
| 6447 |
pcur.open_no_init(clust_index, dtuple, PAGE_CUR_GE, BTR_SEARCH_LEAF, 0, &mtr, |
| 6448 |
UT_LOCATION_HERE); |
| 6449 |
|
| 6450 |
for (; move == true; move = pcur.move_to_next(&mtr)) { |
| 6451 |
rec = pcur.get_rec(); |
| 6452 |
offsets = rec_get_offsets(rec, clust_index, nullptr, ULINT_UNDEFINED, |
| 6453 |
UT_LOCATION_HERE, &heap); |
| 6454 |
|
| 6455 |
if (page_rec_is_infimum(rec) || page_rec_is_supremum(rec)) { |
| 6456 |
continue; |
| 6457 |
} |
| 6458 |
|
| 6459 |
if (0 != cmp_dtuple_rec(dtuple, rec, clust_index, offsets)) { |
| 6460 |
break; |
| 6461 |
} |
| 6462 |
|
| 6463 |
if (rec_get_deleted_flag(rec, dict_table_is_comp(table))) { |
| 6464 |
continue; |
| 6465 |
} |
| 6466 |
|
| 6467 |
if (n_recs == col_offset) { |
| 6468 |
const byte *data; |
| 6469 |
ulint len; |
| 6470 |
data = rec_get_nth_field(clust_index, rec, offsets, |
| 6471 |
cardinality_index_offset, &len); |
| 6472 |
|
| 6473 |
*cardinality = static_cast<ulonglong>(round(mach_read_from_8(data))); |
| 6474 |
mtr_commit(&mtr); |
| 6475 |
mem_heap_free(heap); |
| 6476 |
return (true); |
| 6477 |
} |
| 6478 |
|
| 6479 |
n_recs++; |
| 6480 |
} |
| 6481 |
|
| 6482 |
mtr_commit(&mtr); |
| 6483 |
mem_heap_free(heap); |
| 6484 |
return (false); |
| 6485 |
} |