MySQL 9.5.0
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, 2025, 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 private:
61 bool DoInit() override;
62 int DoRead() override;
63
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 private:
84 bool DoInit() override;
85 int DoRead() override;
86
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 void UnlockRow() override;
108
109 // Performance schema batch mode on EQRefIterator does not make any sense,
110 // since it (by definition) can never scan more than one row. Normally,
111 // we should not get this (for nested loop joins, PFS batch mode is not
112 // enabled if the innermost iterator is an EQRefIterator); however,
113 // we cannot assert(false), since it could happen if we only have
114 // a single table. Thus, just ignore the call should it happen.
115 void StartPSIBatchMode() override {}
116
117 private:
118 bool DoInit() override;
119 int DoRead() override;
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 /**
136 Rows from const tables are read once but potentially used
137 multiple times during execution of a query.
138 Ensure such rows are never unlocked during query execution.
139 */
140 void UnlockRow() override {}
141
142 private:
143 bool DoInit() override;
144 int DoRead() override;
145
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 private:
161 bool DoInit() override;
162 int DoRead() override;
163
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 private:
192 bool DoInit() override;
193 int DoRead() override;
194
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 private:
244 bool DoInit() override;
245 int DoRead() override;
246
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 void SetNullRowFlag(bool is_null_row) override {
273 // Init() may not have been called yet, so just forward to both iterators.
274 m_source_iterator->SetNullRowFlag(is_null_row);
275 m_table_scan_iterator->SetNullRowFlag(is_null_row);
276 }
277
278 void UnlockRow() override { m_iterator->UnlockRow(); }
279
280 private:
281 bool DoInit() override;
282 int DoRead() override { return m_iterator->Read(); }
283
284 // If any of these are false during Init(), we are having a NULL IN ( ... ),
285 // and need to fall back to table scan. Extracted from m_ref.
286 std::vector<bool *> m_applicable_cond_guards;
287
288 // Points to either m_source_iterator or m_table_scan_iterator,
289 // depending on the value of applicable_cond_guards. Set up during Init().
291
292 // Points to the last iterator that was Init()-ed. Used to reset the handler
293 // when switching from one iterator to the other.
295
296 // The iterator we are normally reading records from (a RefIterator or
297 // similar).
299
300 // Our fallback iterator (possibly wrapped in a TimingIterator).
302
303 // The underlying table.
305};
306
307#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
int DoRead() override
Definition: ref_row_iterators.h:282
std::vector< bool * > m_applicable_cond_guards
Definition: ref_row_iterators.h:286
void UnlockRow() override
Definition: ref_row_iterators.h:278
RowIterator * m_last_iterator_inited
Definition: ref_row_iterators.h:294
bool DoInit() override
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:272
RowIterator * m_iterator
Definition: ref_row_iterators.h:290
TABLE *const m_table
Definition: ref_row_iterators.h:304
unique_ptr_destroy_only< RowIterator > m_source_iterator
Definition: ref_row_iterators.h:298
unique_ptr_destroy_only< RowIterator > m_table_scan_iterator
Definition: ref_row_iterators.h:301
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
int DoRead() override
Read a constant table when there is at most one matching row, using an index lookup.
Definition: ref_row_iterators.cc:97
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:140
ha_rows *const m_examined_rows
Definition: ref_row_iterators.h:148
bool DoInit() override
Definition: ref_row_iterators.cc:83
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
Definition: ref_row_iterators.h:184
QEP_TAB * m_qep_tab
Definition: ref_row_iterators.h:195
bool DoInit() override
Definition: ref_row_iterators.cc:493
int DoRead() override
Definition: ref_row_iterators.cc:587
~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
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
bool DoInit() override
Read row using unique key: eq_ref access method implementation.
Definition: ref_row_iterators.cc:133
int DoRead() override
Read row using unique key: eq_ref access method implementation.
Definition: ref_row_iterators.cc:163
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:115
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
bool DoInit() override
Definition: ref_row_iterators.cc:650
const bool m_use_limit
Definition: ref_row_iterators.h:167
const bool m_use_order
Definition: ref_row_iterators.h:166
Item_func_match *const m_ft_func
Definition: ref_row_iterators.h:165
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
int DoRead() override
Definition: ref_row_iterators.cc:673
Definition: item_func.h:3601
Read a table assumed to be included in execution of a pushed join.
Definition: ref_row_iterators.h:237
Index_lookup *const m_ref
Definition: ref_row_iterators.h:247
bool DoInit() override
Definition: ref_row_iterators.cc:261
PushedJoinRefIterator(THD *thd, TABLE *table, Index_lookup *ref, bool use_order, bool is_unique, ha_rows *examined_rows)
Definition: ref_row_iterators.cc:251
ha_rows *const m_examined_rows
Definition: ref_row_iterators.h:251
int DoRead() override
Definition: ref_row_iterators.cc:276
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:256
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
int DoRead() override
const double m_expected_rows
Definition: ref_row_iterators.h:66
bool DoInit() override
Definition: ref_row_iterators.cc:338
~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
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 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
int DoRead() override
Definition: ref_row_iterators.cc:714
bool DoInit() override
Definition: ref_row_iterators.cc:697
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:255
virtual void UnlockRow()=0
int Read()
Read a single row.
Definition: row_iterator.h:124
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:267
TABLE * table() const
Definition: row_iterator.h:279
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:480
my_off_t ha_rows
Definition: my_base.h:1217
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:1435