1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
|
/* Copyright (c) 2000, 2025, Oracle and/or its affiliates.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is designed to work with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have either included with
the program or referenced in the documentation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License, version 2.0, for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
#include "sql/range_optimizer/reverse_index_range_scan.h"
#include <assert.h>
#include "my_base.h"
#include "my_dbug.h"
#include "sql/handler.h"
#include "sql/key.h"
#include "sql/sql_executor.h"
#include "sql/table.h"
ReverseIndexRangeScanIterator::ReverseIndexRangeScanIterator(
THD *thd, TABLE *table, ha_rows *examined_rows, double expected_rows,
uint index, MEM_ROOT *return_mem_root, uint mrr_flags,
Bounds_checked_array<QUICK_RANGE *> ranges, bool using_extended_key_parts)
: TableRowIterator(thd, table),
m_index{index},
m_expected_rows{expected_rows},
m_examined_rows{examined_rows},
mem_root{return_mem_root},
m_mrr_flags{mrr_flags},
ranges{ranges},
last_range{nullptr},
m_using_extended_key_parts{using_extended_key_parts} {
/*
Use default MRR implementation for reverse scans. No table engine
currently can do an MRR scan with output in reverse index order.
*/
m_mrr_flags |= HA_MRR_USE_DEFAULT_IMPL;
m_mrr_flags |= HA_MRR_SORTED; // 'sorted' as internals use index_last/_prev
for (QUICK_RANGE *r : ranges) {
if ((r->flag & EQ_RANGE) &&
table->key_info[m_index].key_length != r->max_length) {
r->flag &= ~EQ_RANGE;
}
}
key_part_info = table->key_info[m_index].key_part;
m_multi_valued_index = table->key_info[m_index].flags & HA_MULTI_VALUED_KEY;
}
ReverseIndexRangeScanIterator::~ReverseIndexRangeScanIterator() {
if (m_multi_valued_index && table()->file) {
table()->file->ha_extra(HA_EXTRA_DISABLE_UNIQUE_RECORD_FILTER);
}
}
bool ReverseIndexRangeScanIterator::Init() {
current_range_idx = ranges.size();
empty_record(table());
/*
Only attempt to allocate a record buffer the first time the handler is
initialized.
*/
const bool first_init = !table()->file->inited;
if (!inited) {
if (column_bitmap.bitmap == nullptr) {
/* Allocate a bitmap for used columns */
my_bitmap_map *bitmap =
(my_bitmap_map *)mem_root->Alloc(table()->s->column_bitmap_size);
if (bitmap == nullptr) {
return true;
}
bitmap_init(&column_bitmap, bitmap, table()->s->fields);
}
inited = true;
}
if (table()->file->inited) table()->file->ha_index_or_rnd_end();
last_range = nullptr;
if (InitIndexRangeScan(table(), table()->file, m_index, m_mrr_flags,
/*in_ror_merged_scan=*/false, &column_bitmap)) {
return true;
}
if (first_init && table()->file->inited) {
if (set_record_buffer(table(), m_expected_rows)) {
return true; /* purecov: inspected */
}
}
HANDLER_BUFFER empty_buf;
empty_buf.buffer = empty_buf.buffer_end = empty_buf.end_of_used_area =
nullptr;
RANGE_SEQ_IF seq_funcs = {quick_range_rev_seq_init, quick_range_seq_next,
nullptr};
if (int error = table()->file->multi_range_read_init(
&seq_funcs, this, ranges.size(), m_mrr_flags, &empty_buf);
error != 0) {
(void)report_handler_error(table(), error);
return true;
}
return false;
}
int ReverseIndexRangeScanIterator::Read() {
DBUG_TRACE;
/* The max key is handled as follows:
* - if there is NO_MAX_RANGE, start at the end and move backwards
* - if it is an EQ_RANGE (which means that max key covers the entire
* key) and the query does not use any hidden key fields that are
* not considered when the range optimzier sets EQ_RANGE (e.g. the
* primary key added by InnoDB), then go directly to the key and
* read through it (sorting backwards is same as sorting forwards).
* - if it is NEAR_MAX, go to the key or next, step back once, and
* move backwards
* - otherwise (not NEAR_MAX == include the key), go after the key,
* step back once, and move backwards
*/
for (;;) {
if (last_range != nullptr) { // Keep on reading from the same key.
int result = ((last_range->flag & EQ_RANGE && !m_using_extended_key_parts)
? table()->file->ha_index_next_same(
table()->record[0], last_range->min_key,
last_range->min_length)
: table()->file->ha_index_prev(table()->record[0]));
if (result == 0) {
if (cmp_prev(last_range) == 0) {
if (m_examined_rows != nullptr) {
++*m_examined_rows;
}
return 0;
}
} else {
if (int error_code = HandleError(result); error_code != -1) {
return error_code;
}
}
}
// EOF from this range, so read the next one.
if (current_range_idx == 0) {
return -1; // No more ranges.
}
last_range = ranges[--current_range_idx];
// Case where we can avoid descending scan, see comment above
const bool eqrange_all_keyparts =
(last_range->flag & EQ_RANGE) && !m_using_extended_key_parts;
/*
If we have pushed an index condition (ICP) and this quick select
will use ha_index_prev() to read data, we need to let the
handler know where to end the scan in order to avoid that the
ICP implementation continues to read past the range boundary.
*/
if (table()->file->pushed_idx_cond) {
if (!eqrange_all_keyparts) {
key_range min_range;
last_range->make_min_endpoint(&min_range);
if (min_range.length > 0)
table()->file->set_end_range(&min_range, handler::RANGE_SCAN_DESC);
else
table()->file->set_end_range(nullptr, handler::RANGE_SCAN_DESC);
} else {
/*
Will use ha_index_next_same() for reading records. In case we have
set the end range for an earlier range, this need to be cleared.
*/
table()->file->set_end_range(nullptr, handler::RANGE_SCAN_ASC);
}
}
if (last_range->flag & NO_MAX_RANGE) // Read last record
{
if (int result = table()->file->ha_index_last(table()->record[0]);
result != 0) {
/*
HA_ERR_END_OF_FILE is returned both when the table is empty and when
there are no qualifying records in the range (when using ICP).
Interpret this return value as "no qualifying rows in the range" to
avoid loss of records. If the error code truly meant "empty table"
the next iteration of the loop will exit.
*/
if (int error_code = HandleError(result); error_code != -1) {
return error_code;
}
last_range = nullptr; // Go to next range
continue;
}
if (cmp_prev(last_range) == 0) {
if (m_examined_rows != nullptr) {
++*m_examined_rows;
}
return 0;
}
last_range = nullptr; // No match; go to next range
continue;
}
int result;
if (eqrange_all_keyparts) {
result = table()->file->ha_index_read_map(
table()->record[0], last_range->max_key, last_range->max_keypart_map,
HA_READ_KEY_EXACT);
} else {
assert(last_range->flag & NEAR_MAX ||
(last_range->flag & EQ_RANGE && m_using_extended_key_parts) ||
range_reads_after_key(last_range));
result = table()->file->ha_index_read_map(
table()->record[0], last_range->max_key, last_range->max_keypart_map,
((last_range->flag & NEAR_MAX) ? HA_READ_BEFORE_KEY
: HA_READ_PREFIX_LAST_OR_PREV));
}
if (result != 0) {
if (int error_code = HandleError(result); error_code != -1) {
return error_code;
}
last_range = nullptr; // Not found, to next range
continue;
}
if (cmp_prev(last_range) == 0) {
if ((last_range->flag & (UNIQUE_RANGE | EQ_RANGE)) ==
(UNIQUE_RANGE | EQ_RANGE))
last_range = nullptr; // Stop searching
if (m_examined_rows != nullptr) {
++*m_examined_rows;
}
return 0; // Found key is in range
}
last_range = nullptr; // To next range
}
}
/*
true if this range will require using HA_READ_AFTER_KEY
See comment in Read() about this
*/
bool ReverseIndexRangeScanIterator::range_reads_after_key(
QUICK_RANGE *range_arg) {
return ((range_arg->flag & (NO_MAX_RANGE | NEAR_MAX)) ||
!(range_arg->flag & EQ_RANGE) ||
table()->key_info[m_index].key_length != range_arg->max_length)
? true
: false;
}
/*
Returns 0 if found key is inside range (found key >= range->min_key).
*/
int ReverseIndexRangeScanIterator::cmp_prev(QUICK_RANGE *range_arg) {
int cmp;
if (range_arg->flag & NO_MIN_RANGE) return 0; /* key can't be to small */
cmp = key_cmp(key_part_info, range_arg->min_key, range_arg->min_length,
/*is_reverse_multi_valued_index_scan*/ m_multi_valued_index);
if (cmp > 0 || (cmp == 0 && !(range_arg->flag & NEAR_MIN))) return 0;
return 1; // outside of range
}
// Pretty much the same as quick_range_seq_init(), just with a different class.
range_seq_t ReverseIndexRangeScanIterator::quick_range_rev_seq_init(
void *init_param, uint, uint) {
ReverseIndexRangeScanIterator *quick =
static_cast<ReverseIndexRangeScanIterator *>(init_param);
QUICK_RANGE **first = quick->ranges.begin();
QUICK_RANGE **last = quick->ranges.end();
quick->qr_traversal_ctx.first = first;
quick->qr_traversal_ctx.cur = first;
quick->qr_traversal_ctx.last = last;
return &quick->qr_traversal_ctx;
}
|