MySQL 8.4.2
Source Code Documentation
ref_row_iterators.h
Go to the documentation of this file.
1#ifndef SQL_ITERATORS_REF_ROW_ITERATORS_H_
2#define SQL_ITERATORS_REF_ROW_ITERATORS_H_
3
4/* Copyright (c) 2018, 2024, Oracle and/or its affiliates.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License, version 2.0,
8 as published by the Free Software Foundation.
9
10 This program is designed to work with certain software (including
11 but not limited to OpenSSL) that is licensed under separate terms,
12 as designated in a particular file or component or in included license
13 documentation. The authors of MySQL hereby grant you an additional
14 permission to link the program and your derivative works with the
15 separately licensed software that they have either included with
16 the program or referenced in the documentation.
17
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License, version 2.0, for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
26
27#include <sys/types.h>
28#include <memory>
29
30#include "my_alloc.h"
31#include "my_bitmap.h"
32#include "my_inttypes.h"
35#include "sql/sql_sort.h"
36
37class Item_func_match;
38class QEP_TAB;
39class THD;
40struct Index_lookup;
41struct TABLE;
42
43/**
44 For each record on the left side of a join (given in Init()), returns one or
45 more matching rows from the given table, i.e., WHERE column=<ref>.
46 */
47template <bool Reverse>
48class RefIterator final : public TableRowIterator {
49 public:
50 // "examined_rows", if not nullptr, is incremented for each successful Read().
52 double expected_rows, ha_rows *examined_rows)
54 m_ref(ref),
55 m_use_order(use_order),
56 m_expected_rows(expected_rows),
57 m_examined_rows(examined_rows) {}
58 ~RefIterator() override;
59
60 bool Init() override;
61 int Read() override;
62
63 private:
65 const bool m_use_order;
66 const double m_expected_rows;
70};
71
72/**
73 Like RefIterator, but after it's returned all its rows, will also search for
74 rows that match NULL, i.e., WHERE column=<ref> OR column IS NULL.
75 */
76class RefOrNullIterator final : public TableRowIterator {
77 public:
78 // "examined_rows", if not nullptr, is incremented for each successful Read().
79 RefOrNullIterator(THD *thd, TABLE *table, Index_lookup *ref, bool use_order,
80 double expected_rows, ha_rows *examined_rows);
81 ~RefOrNullIterator() override;
82
83 bool Init() override;
84 int Read() override;
85
86 private:
88 const bool m_use_order;
90 const double m_expected_rows;
93};
94
95/**
96 Like RefIterator, but used in situations where we're guaranteed to have
97 exactly zero or one rows for each reference (due to e.g. unique constraints).
98 It adds extra buffering to reduce the number of calls to the storage engine in
99 the case where many consecutive rows on the left side contain the same value.
100 */
101class EQRefIterator final : public TableRowIterator {
102 public:
103 // "examined_rows", if not nullptr, is incremented for each successful Read().
105 ha_rows *examined_rows);
106
107 bool Init() override;
108 int Read() override;
109 void UnlockRow() override;
110
111 // Performance schema batch mode on EQRefIterator does not make any sense,
112 // since it (by definition) can never scan more than one row. Normally,
113 // we should not get this (for nested loop joins, PFS batch mode is not
114 // enabled if the innermost iterator is an EQRefIterator); however,
115 // we cannot assert(false), since it could happen if we only have
116 // a single table. Thus, just ignore the call should it happen.
117 void StartPSIBatchMode() override {}
118
119 private:
123};
124
125/**
126 An iterator that reads from a table where only a single row is known to be
127 matching, no matter what's on the left side, i.e., WHERE column=<const>.
128 */
129class ConstIterator final : public TableRowIterator {
130 public:
131 // "examined_rows", if not nullptr, is incremented for each successful Read().
133 ha_rows *examined_rows);
134
135 bool Init() override;
136 int Read() override;
137
138 /**
139 Rows from const tables are read once but potentially used
140 multiple times during execution of a query.
141 Ensure such rows are never unlocked during query execution.
142 */
143 void UnlockRow() override {}
144
145 private:
149};
150
151/** An iterator that does a search through a full-text index. */
153 public:
154 // "examined_rows", if not nullptr, is incremented for each successful Read().
156 Item_func_match *ft_func, bool use_order,
157 bool use_limit, ha_rows *examined_rows);
158 ~FullTextSearchIterator() override;
159
160 bool Init() override;
161 int Read() override;
162
163 private:
166 const bool m_use_order;
167 const bool m_use_limit;
169};
170
171/*
172 This is for QS_DYNAMIC_RANGE, i.e., "Range checked for each
173 record". The trace for the range analysis below this point will
174 be printed with different ranges for every record to the left of
175 this table in the join; the range optimizer can either select any
176 RowIterator or a full table scan, and any Read() is just proxied
177 over to that.
178
179 Note in particular that this means the range optimizer will be
180 executed anew on every single call to Init(), and modify the
181 query plan accordingly! It is not clear whether this is an actual
182 win in a typical query.
183 */
185 public:
186 // "examined_rows", if not nullptr, is incremented for each successful Read().
188 ha_rows *examined_rows);
189 ~DynamicRangeIterator() override;
190
191 bool Init() override;
192 int Read() override;
193
194 private:
196
197 // All quicks are allocated on this MEM_ROOT, which is cleared out
198 // between every invocation of the range optimizer.
200
202
203 /**
204 Used by optimizer tracing to decide whether or not dynamic range
205 analysis of this select has been traced already. If optimizer
206 trace option DYNAMIC_RANGE is enabled, range analysis will be
207 traced with different ranges for every record to the left of this
208 table in the join. If disabled, range analysis will only be traced
209 for the first range.
210 */
212
214};
215
216/**
217 Read a table *assumed* to be included in execution of a pushed join.
218 This is the counterpart of RefIterator / EQRefIterator for child
219 tables in a pushed join. As the underlying handler interface for
220 pushed joins are the same for Ref / EQRef operations, we implement
221 both in the same PushedJoinRefIterator class.
222
223 In order to differentiate between a 'range' and 'single-row lookup'
224 in the DebugString(), the class takes a 'bool Unique' C'tor argument.
225 This also offers some optimizations in implementation of ::Read().
226
227 When the table access is performed as part of the pushed join,
228 all 'linked' child columns are prefetched together with the parent row.
229 The handler will then only format the row as required by MySQL and set
230 table status accordingly.
231
232 However, there may be situations where the prepared pushed join was not
233 executed as assumed. It is the responsibility of the handler to handle
234 these situation by letting @c ha_index_read_pushed() then effectively do a
235 plain old' index_read_map(..., HA_READ_KEY_EXACT);
236 */
238 public:
239 // "examined_rows", if not nullptr, is incremented for each successful Read().
241 bool use_order, bool is_unique, ha_rows *examined_rows);
242
243 bool Init() override;
244 int Read() override;
245
246 private:
248 const bool m_use_order;
249 const bool m_is_unique;
252};
253
254/**
255 An iterator that switches between another iterator (typically a RefIterator
256 or similar) and a TableScanIterator.
257
258 This is used when predicates have been pushed down into an IN subquery
259 and then created ref accesses, but said predicates should not be checked for
260 a NULL value (so we need to revert to table scans). See
261 QEP_TAB::access_path() for a more thorough explanation.
262 */
263class AlternativeIterator final : public RowIterator {
264 public:
265 // Takes ownership of "source", and is responsible for
266 // calling Init() on it, but does not hold the memory.
269 unique_ptr_destroy_only<RowIterator> table_scan_iterator,
271
272 bool Init() override;
273
274 int Read() override { return m_iterator->Read(); }
275
276 void SetNullRowFlag(bool is_null_row) override {
277 // Init() may not have been called yet, so just forward to both iterators.
278 m_source_iterator->SetNullRowFlag(is_null_row);
279 m_table_scan_iterator->SetNullRowFlag(is_null_row);
280 }
281
282 void UnlockRow() override { m_iterator->UnlockRow(); }
283
284 private:
285 // If any of these are false during Init(), we are having a NULL IN ( ... ),
286 // and need to fall back to table scan. Extracted from m_ref.
287 std::vector<bool *> m_applicable_cond_guards;
288
289 // Points to either m_source_iterator or m_table_scan_iterator,
290 // depending on the value of applicable_cond_guards. Set up during Init().
292
293 // Points to the last iterator that was Init()-ed. Used to reset the handler
294 // when switching from one iterator to the other.
296
297 // The iterator we are normally reading records from (a RefIterator or
298 // similar).
300
301 // Our fallback iterator (possibly wrapped in a TimingIterator).
303
304 // The underlying table.
306};
307
308#endif // SQL_ITERATORS_REF_ROW_ITERATORS_H_
Row iterators that scan a single table without reference to other tables or iterators.
An iterator that switches between another iterator (typically a RefIterator or similar) and a TableSc...
Definition: ref_row_iterators.h:263
AlternativeIterator(THD *thd, TABLE *table, unique_ptr_destroy_only< RowIterator > source, unique_ptr_destroy_only< RowIterator > table_scan_iterator, Index_lookup *ref)
Definition: ref_row_iterators.cc:769
std::vector< bool * > m_applicable_cond_guards
Definition: ref_row_iterators.h:287
void UnlockRow() override
Definition: ref_row_iterators.h:282
RowIterator * m_last_iterator_inited
Definition: ref_row_iterators.h:295
int Read() override
Read a single row.
Definition: ref_row_iterators.h:274
bool Init() override
Initialize or reinitialize the iterator.
Definition: ref_row_iterators.cc:786
void SetNullRowFlag(bool is_null_row) override
Mark the current row buffer as containing a NULL row or not, so that if you read from it and the flag...
Definition: ref_row_iterators.h:276
RowIterator * m_iterator
Definition: ref_row_iterators.h:291
TABLE *const m_table
Definition: ref_row_iterators.h:305
unique_ptr_destroy_only< RowIterator > m_source_iterator
Definition: ref_row_iterators.h:299
unique_ptr_destroy_only< RowIterator > m_table_scan_iterator
Definition: ref_row_iterators.h:302
An iterator that reads from a table where only a single row is known to be matching,...
Definition: ref_row_iterators.h:129
bool m_first_record_since_init
Definition: ref_row_iterators.h:147
void UnlockRow() override
Rows from const tables are read once but potentially used multiple times during execution of a query.
Definition: ref_row_iterators.h:143
ha_rows *const m_examined_rows
Definition: ref_row_iterators.h:148
Index_lookup *const m_ref
Definition: ref_row_iterators.h:146
ConstIterator(THD *thd, TABLE *table, Index_lookup *table_ref, ha_rows *examined_rows)
Definition: ref_row_iterators.cc:77
int Read() override
Read a constant table when there is at most one matching row, using an index lookup.
Definition: ref_row_iterators.cc:97
bool Init() override
Initialize or reinitialize the iterator.
Definition: ref_row_iterators.cc:83
Definition: ref_row_iterators.h:184
QEP_TAB * m_qep_tab
Definition: ref_row_iterators.h:195
bool Init() override
Initialize or reinitialize the iterator.
Definition: ref_row_iterators.cc:493
~DynamicRangeIterator() override
Definition: ref_row_iterators.cc:485
MEM_ROOT m_mem_root
Definition: ref_row_iterators.h:199
bool m_quick_traced_before
Used by optimizer tracing to decide whether or not dynamic range analysis of this select has been tra...
Definition: ref_row_iterators.h:211
unique_ptr_destroy_only< RowIterator > m_iterator
Definition: ref_row_iterators.h:201
int Read() override
Read a single row.
Definition: ref_row_iterators.cc:587
DynamicRangeIterator(THD *thd, TABLE *table, QEP_TAB *qep_tab, ha_rows *examined_rows)
Definition: ref_row_iterators.cc:476
ha_rows *const m_examined_rows
Definition: ref_row_iterators.h:213
Like RefIterator, but used in situations where we're guaranteed to have exactly zero or one rows for ...
Definition: ref_row_iterators.h:101
void UnlockRow() override
Since EQRefIterator may buffer a record, do not unlock it if it was not used in this invocation of EQ...
Definition: ref_row_iterators.cc:246
bool m_first_record_since_init
Definition: ref_row_iterators.h:121
int Read() override
Read row using unique key: eq_ref access method implementation.
Definition: ref_row_iterators.cc:163
bool Init() override
Read row using unique key: eq_ref access method implementation.
Definition: ref_row_iterators.cc:133
Index_lookup *const m_ref
Definition: ref_row_iterators.h:120
void StartPSIBatchMode() override
Start performance schema batch mode, if supported (otherwise ignored).
Definition: ref_row_iterators.h:117
ha_rows *const m_examined_rows
Definition: ref_row_iterators.h:122
EQRefIterator(THD *thd, TABLE *table, Index_lookup *ref, ha_rows *examined_rows)
Definition: ref_row_iterators.cc:110
An iterator that does a search through a full-text index.
Definition: ref_row_iterators.h:152
Index_lookup *const m_ref
Definition: ref_row_iterators.h:164
const bool m_use_limit
Definition: ref_row_iterators.h:167
const bool m_use_order
Definition: ref_row_iterators.h:166
bool Init() override
Initialize or reinitialize the iterator.
Definition: ref_row_iterators.cc:650
Item_func_match *const m_ft_func
Definition: ref_row_iterators.h:165
int Read() override
Read a single row.
Definition: ref_row_iterators.cc:673
ha_rows *const m_examined_rows
Definition: ref_row_iterators.h:168
FullTextSearchIterator(THD *thd, TABLE *table, Index_lookup *ref, Item_func_match *ft_func, bool use_order, bool use_limit, ha_rows *examined_rows)
Definition: ref_row_iterators.cc:595
~FullTextSearchIterator() override
Definition: ref_row_iterators.cc:643
Definition: item_func.h:3465
Read a table assumed to be included in execution of a pushed join.
Definition: ref_row_iterators.h:237
int Read() override
Read a single row.
Definition: ref_row_iterators.cc:276
Index_lookup *const m_ref
Definition: ref_row_iterators.h:247
PushedJoinRefIterator(THD *thd, TABLE *table, Index_lookup *ref, bool use_order, bool is_unique, ha_rows *examined_rows)
Definition: ref_row_iterators.cc:251
bool Init() override
Initialize or reinitialize the iterator.
Definition: ref_row_iterators.cc:261
ha_rows *const m_examined_rows
Definition: ref_row_iterators.h:251
const bool m_use_order
Definition: ref_row_iterators.h:248
const bool m_is_unique
Definition: ref_row_iterators.h:249
bool m_first_record_since_init
Definition: ref_row_iterators.h:250
Definition: sql_executor.h:254
For each record on the left side of a join (given in Init()), returns one or more matching rows from ...
Definition: ref_row_iterators.h:48
RefIterator(THD *thd, TABLE *table, Index_lookup *ref, bool use_order, double expected_rows, ha_rows *examined_rows)
Definition: ref_row_iterators.h:51
bool m_first_record_since_init
Definition: ref_row_iterators.h:68
ha_rows *const m_examined_rows
Definition: ref_row_iterators.h:67
Index_lookup *const m_ref
Definition: ref_row_iterators.h:64
bool m_is_mvi_unique_filter_enabled
Definition: ref_row_iterators.h:69
const bool m_use_order
Definition: ref_row_iterators.h:65
bool Init() override
Initialize or reinitialize the iterator.
Definition: ref_row_iterators.cc:338
const double m_expected_rows
Definition: ref_row_iterators.h:66
int Read() override
Read a single row.
~RefIterator() override
Like RefIterator, but after it's returned all its rows, will also search for rows that match NULL,...
Definition: ref_row_iterators.h:76
bool m_reading_first_row
Definition: ref_row_iterators.h:89
const double m_expected_rows
Definition: ref_row_iterators.h:90
int Read() override
Read a single row.
Definition: ref_row_iterators.cc:714
Index_lookup *const m_ref
Definition: ref_row_iterators.h:87
ha_rows *const m_examined_rows
Definition: ref_row_iterators.h:91
const bool m_use_order
Definition: ref_row_iterators.h:88
~RefOrNullIterator() override
Definition: ref_row_iterators.cc:762
bool Init() override
Initialize or reinitialize the iterator.
Definition: ref_row_iterators.cc:697
bool m_is_mvi_unique_filter_enabled
Definition: ref_row_iterators.h:92
RefOrNullIterator(THD *thd, TABLE *table, Index_lookup *ref, bool use_order, double expected_rows, ha_rows *examined_rows)
Reading of key with key reference and one part that may be NULL.
Definition: ref_row_iterators.cc:688
A context for reading through a single table using a chosen access method: index read,...
Definition: row_iterator.h:82
THD * thd() const
Definition: row_iterator.h:228
virtual void UnlockRow()=0
virtual int Read()=0
Read a single row.
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:36
Definition: row_iterator.h:234
TABLE * table() const
Definition: row_iterator.h:246
This file follows Google coding style, except for the name MEM_ROOT (which is kept for historical rea...
std::unique_ptr< T, Destroy_only< T > > unique_ptr_destroy_only
std::unique_ptr, but only destroying.
Definition: my_alloc.h:477
my_off_t ha_rows
Definition: my_base.h:1141
Some integer typedefs for easier portability.
static PFS_engine_table_share_proxy table
Definition: pfs.cc:61
PT & ref(PT *tp)
Definition: tablespace_impl.cc:359
repeated Source source
Definition: replication_asynchronous_connection_failover.proto:42
Structure used for index-based lookups.
Definition: sql_opt_exec_shared.h:67
The MEM_ROOT is a simple arena, where allocations are carved out of larger blocks.
Definition: my_alloc.h:83
Definition: table.h:1406