| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 1996, 2026, Oracle and/or its affiliates. |
| 4 |
|
| 5 |
This program is free software; you can redistribute it and/or modify it under |
| 6 |
the terms of the GNU General Public License, version 2.0, as published by the |
| 7 |
Free Software Foundation. |
| 8 |
|
| 9 |
This program is designed to work with certain software (including |
| 10 |
but not limited to OpenSSL) that is licensed under separate terms, |
| 11 |
as designated in a particular file or component or in included license |
| 12 |
documentation. The authors of MySQL hereby grant you an additional |
| 13 |
permission to link the program and your derivative works with the |
| 14 |
separately licensed software that they have either included with |
| 15 |
the program or referenced in the documentation. |
| 16 |
|
| 17 |
This program is distributed in the hope that it will be useful, but WITHOUT |
| 18 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 19 |
FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0, |
| 20 |
for more details. |
| 21 |
|
| 22 |
You should have received a copy of the GNU General Public License along with |
| 23 |
this program; if not, write to the Free Software Foundation, Inc., |
| 24 |
51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 25 |
|
| 26 |
*****************************************************************************/ |
| 27 |
|
| 28 |
/** @file include/row0row.ic |
| 29 |
General row routines |
| 30 |
|
| 31 |
Created 4/20/1996 Heikki Tuuri |
| 32 |
*******************************************************/ |
| 33 |
|
| 34 |
#include <cmath> |
| 35 |
#include <cstdint> |
| 36 |
|
| 37 |
#include "dict0dict.h" |
| 38 |
#include "rem0rec.h" |
| 39 |
#include "trx0undo.h" |
| 40 |
|
| 41 |
static inline ulint row_get_trx_id_offset(const dict_index_t *index, |
| 42 |
const ulint *offsets) { |
| 43 |
ulint pos; |
| 44 |
ulint offset; |
| 45 |
ulint len; |
| 46 |
|
| 47 |
ut_ad(index->is_clustered()); |
| 48 |
ut_ad(rec_offs_validate(nullptr, index, offsets)); |
| 49 |
|
| 50 |
pos = index->get_sys_col_pos(DATA_TRX_ID); |
| 51 |
|
| 52 |
offset = rec_get_nth_field_offs(index, offsets, pos, &len); |
| 53 |
|
| 54 |
ut_ad(len == DATA_TRX_ID_LEN); |
| 55 |
|
| 56 |
return (offset); |
| 57 |
} |
| 58 |
|
| 59 |
static inline trx_id_t row_get_rec_trx_id(const rec_t *rec, |
| 60 |
const dict_index_t *index, |
| 61 |
const ulint *offsets) { |
| 62 |
ulint offset; |
| 63 |
|
| 64 |
ut_ad(index->is_clustered()); |
| 65 |
ut_ad(rec_offs_validate(rec, index, offsets)); |
| 66 |
|
| 67 |
offset = index->trx_id_offset; |
| 68 |
|
| 69 |
if (!offset) { |
| 70 |
offset = row_get_trx_id_offset(index, offsets); |
| 71 |
} |
| 72 |
|
| 73 |
return (trx_read_trx_id(rec + offset)); |
| 74 |
} |
| 75 |
|
| 76 |
static inline roll_ptr_t row_get_rec_roll_ptr(const rec_t *rec, |
| 77 |
const dict_index_t *index, |
| 78 |
const ulint *offsets) { |
| 79 |
ulint offset; |
| 80 |
|
| 81 |
ut_ad(index->is_clustered()); |
| 82 |
ut_ad(rec_offs_validate(rec, index, offsets)); |
| 83 |
|
| 84 |
offset = index->trx_id_offset; |
| 85 |
|
| 86 |
if (!offset) { |
| 87 |
offset = row_get_trx_id_offset(index, offsets); |
| 88 |
} |
| 89 |
|
| 90 |
return (trx_read_roll_ptr(rec + offset + DATA_TRX_ID_LEN)); |
| 91 |
} |
| 92 |
|
| 93 |
static inline dtuple_t *row_build_index_entry(const dtuple_t *row, |
| 94 |
const row_ext_t *ext, |
| 95 |
const dict_index_t *index, |
| 96 |
mem_heap_t *heap) { |
| 97 |
dtuple_t *entry; |
| 98 |
|
| 99 |
ut_ad(dtuple_check_typed(row)); |
| 100 |
entry = row_build_index_entry_low(row, ext, index, heap, ROW_BUILD_NORMAL); |
| 101 |
ut_ad(!entry || dtuple_check_typed(entry)); |
| 102 |
return (entry); |
| 103 |
} |
| 104 |
|
| 105 |
/** Builds from a secondary index record a row reference with which we can |
| 106 |
search the clustered index record. */ |
| 107 |
static inline void row_build_row_ref_fast( |
| 108 |
dtuple_t *ref, /*!< in/out: typed data tuple where the |
| 109 |
reference is built */ |
| 110 |
const ulint *map, /*!< in: array of field numbers in rec |
| 111 |
telling how ref should be built from |
| 112 |
the fields of rec */ |
| 113 |
const rec_t *rec, /*!< in: record in the index; must be |
| 114 |
preserved while ref is used, as we do |
| 115 |
not copy field values to heap */ |
| 116 |
const ulint *offsets) /*!< in: array returned by rec_get_offsets() */ |
| 117 |
{ |
| 118 |
dfield_t *dfield; |
| 119 |
const byte *field; |
| 120 |
ulint len; |
| 121 |
ulint ref_len; |
| 122 |
ulint field_no; |
| 123 |
ulint i; |
| 124 |
|
| 125 |
ut_ad(rec_offs_validate(rec, nullptr, offsets)); |
| 126 |
ut_ad(!rec_offs_any_extern(offsets)); |
| 127 |
ref_len = dtuple_get_n_fields(ref); |
| 128 |
|
| 129 |
for (i = 0; i < ref_len; i++) { |
| 130 |
dfield = dtuple_get_nth_field(ref, i); |
| 131 |
|
| 132 |
field_no = *(map + i); |
| 133 |
|
| 134 |
if (field_no != ULINT_UNDEFINED) { |
| 135 |
field = rec_get_nth_field(nullptr, rec, offsets, field_no, &len); |
| 136 |
dfield_set_data(dfield, field, len); |
| 137 |
} |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
/** Parse the integer data from specified data, which could be |
| 142 |
DATA_INT, DATA_FLOAT or DATA_DOUBLE. If the value is less than 0 |
| 143 |
and the type is not unsigned then we reset the value to 0 |
| 144 |
@param[in] data data to read |
| 145 |
@param[in] len length of data |
| 146 |
@param[in] mtype mtype of data |
| 147 |
@param[in] unsigned_type if the data is unsigned |
| 148 |
@return the integer value from the data */ |
| 149 |
uint64_t row_parse_int(const byte *data, ulint len, ulint mtype, |
| 150 |
bool unsigned_type) { |
| 151 |
uint64_t value = 0; |
| 152 |
|
| 153 |
switch (mtype) { |
| 154 |
case DATA_INT: |
| 155 |
|
| 156 |
ut_a(len <= sizeof value); |
| 157 |
value = mach_read_int_type(data, len, unsigned_type); |
| 158 |
break; |
| 159 |
|
| 160 |
case DATA_FLOAT: |
| 161 |
|
| 162 |
ut_a(len == sizeof(float)); |
| 163 |
/* Cast float value to int64_t first, before casting to unsigned, |
| 164 |
to avoid UBSAN warnings. */ |
| 165 |
value = |
| 166 |
static_cast<uint64_t>(static_cast<int64_t>(mach_float_read(data))); |
| 167 |
break; |
| 168 |
|
| 169 |
case DATA_DOUBLE: { |
| 170 |
ut_a(len == sizeof(double)); |
| 171 |
double dblval = mach_double_read(data); |
| 172 |
value = std::signbit(dblval) ? static_cast<int64_t>(dblval) |
| 173 |
: static_cast<uint64_t>(dblval); |
| 174 |
break; |
| 175 |
} |
| 176 |
default: |
| 177 |
ut_error; |
| 178 |
} |
| 179 |
|
| 180 |
if (!unsigned_type && static_cast<int64_t>(value) < 0) { |
| 181 |
value = 0; |
| 182 |
} |
| 183 |
|
| 184 |
return (value); |
| 185 |
} |