MySQL 8.0.31
Source Code Documentation
row_iterator.h
Go to the documentation of this file.
1#ifndef SQL_ITERATORS_ROW_ITERATOR_H_
2#define SQL_ITERATORS_ROW_ITERATOR_H_
3
4/* Copyright (c) 2018, 2022, 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 also distributed 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 included with MySQL.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License, version 2.0, for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
25
26#include <assert.h>
27#include <string>
28
29class Item;
30class JOIN;
31class THD;
32struct TABLE;
33
34/**
35 Profiling data for an iterator, needed by 'EXPLAIN ANALYZE'.
36 Note that an iterator may be iterated over multiple times, e.g. if it is
37 the inner operand of a neste loop join. This is denoted 'loops'
38 below, and the metrics in this class are aggregated values for all loops.
39*/
41 public:
42 /** Time (in ms) spent fetching the first row. (Sum for all loops.)*/
43 virtual double GetFirstRowMs() const = 0;
44
45 /** Time (in ms) spent fetching the remaining rows. (Sum for all loops.)*/
46 virtual double GetLastRowMs() const = 0;
47
48 /** The number of loops (i.e number of iterator->Init() calls.*/
49 virtual uint64_t GetNumInitCalls() const = 0;
50
51 /** The number of rows fetched. (Sum for all loops.)*/
52 virtual uint64_t GetNumRows() const = 0;
53 virtual ~IteratorProfiler() = default;
54};
55
56/**
57 A context for reading through a single table using a chosen access method:
58 index read, scan, etc, use of cache, etc.. It is mostly meant as an interface,
59 but also contains some private member functions that are useful for many
60 implementations, such as error handling.
61
62 A RowIterator is a simple iterator; you initialize it, and then read one
63 record at a time until Read() returns EOF. A RowIterator can read from
64 other Iterators if you want to, e.g., SortingIterator, which takes in records
65 from another RowIterator and sorts them.
66
67 The abstraction is not completely tight. In particular, it still leaves some
68 specifics to TABLE, such as which columns to read (the read_set). This means
69 it would probably be hard as-is to e.g. sort a join of two tables.
70
71 Use by:
72@code
73 unique_ptr<RowIterator> iterator(new ...);
74 if (iterator->Init())
75 return true;
76 while (iterator->Read() == 0) {
77 ...
78 }
79@endcode
80 */
82 public:
83 // NOTE: Iterators should typically be instantiated using NewIterator,
84 // in sql/iterators/timing_iterator.h.
85 explicit RowIterator(THD *thd) : m_thd(thd) {}
86 virtual ~RowIterator() = default;
87
88 RowIterator(const RowIterator &) = delete;
89
90 // Default move ctor used by IndexRangeScanIterator.
91 RowIterator(RowIterator &&) = default;
92
93 /**
94 Initialize or reinitialize the iterator. You must always call Init()
95 before trying a Read() (but Init() does not imply Read()).
96
97 You can call Init() multiple times; subsequent calls will rewind the
98 iterator (or reposition it, depending on whether the iterator takes in
99 e.g. a TABLE_REF) and allow you to read the records anew.
100 */
101 virtual bool Init() = 0;
102
103 /**
104 Read a single row. The row data is not actually returned from the function;
105 it is put in the table's (or tables', in case of a join) record buffer, ie.,
106 table->records[0].
107
108 @retval
109 0 OK
110 @retval
111 -1 End of records
112 @retval
113 1 Error
114 */
115 virtual int Read() = 0;
116
117 /**
118 Mark the current row buffer as containing a NULL row or not, so that if you
119 read from it and the flag is true, you'll get only NULLs no matter what is
120 actually in the buffer (typically some old leftover row). This is used
121 for outer joins, when an iterator hasn't produced any rows and we need to
122 produce a NULL-complemented row. Init() or Read() won't necessarily
123 reset this flag, so if you ever set is to true, make sure to also set it
124 to false when needed.
125
126 Note that this can be called without Init() having been called first.
127 For example, NestedLoopIterator can hit EOF immediately on the outer
128 iterator, which means the inner iterator doesn't get an Init() call,
129 but will still forward SetNullRowFlag to both inner and outer iterators.
130
131 TODO: We shouldn't need this. See the comments on AggregateIterator for
132 a bit more discussion on abstracting out a row interface.
133 */
134 virtual void SetNullRowFlag(bool is_null_row) = 0;
135
136 // In certain queries, such as SELECT FOR UPDATE, UPDATE or DELETE queries,
137 // reading rows will automatically take locks on them. (This means that the
138 // set of locks taken will depend on whether e.g. the optimizer chose a table
139 // scan or used an index, due to InnoDB's row locking scheme with “gap locks”
140 // for B-trees instead of full predicate locks.)
141 //
142 // However, under some transaction isolation levels (READ COMMITTED or
143 // less strict), it is possible to release such locks if and only if the row
144 // failed a WHERE predicate, as only the returned rows are protected,
145 // not _which_ rows are returned. Thus, if Read() returned a row that you did
146 // not actually use, you should call UnlockRow() afterwards, which allows the
147 // storage engine to release the row lock in such situations.
148 //
149 // TableRowIterator has a default implementation of this; other iterators
150 // should usually either forward the call to their source iterator (if any)
151 // or just ignore it. The right behavior depends on the iterator.
152 virtual void UnlockRow() = 0;
153
154 /** Get profiling data for this iterator (for 'EXPLAIN ANALYZE').*/
155 virtual const IteratorProfiler *GetProfiler() const {
156 /**
157 Valid for TimingIterator, MaterializeIterator and
158 TemptableAggregateIterator only.
159 */
160 assert(false);
161 return nullptr;
162 }
163
164 /** @see TimingIterator .*/
165 virtual void SetOverrideProfiler([
166 [maybe_unused]] const IteratorProfiler *profiler) {
167 // Valid for TimingIterator only.
168 assert(false);
169 }
170
171 /**
172 Start performance schema batch mode, if supported (otherwise ignored).
173
174 PFS batch mode is a mitigation to reduce the overhead of performance schema,
175 typically applied at the innermost table of the entire join. If you start
176 it before scanning the table and then end it afterwards, the entire set
177 of handler calls will be timed only once, as a group, and the costs will
178 be distributed evenly out. This reduces timer overhead.
179
180 If you start PFS batch mode, you must also take care to end it at the
181 end of the scan, one way or the other. Do note that this is true even
182 if the query ends abruptly (LIMIT is reached, or an error happens).
183 The easiest workaround for this is to simply call EndPSIBatchModeIfStarted()
184 on the root iterator at the end of the scan. See the PFSBatchMode class for
185 a useful helper.
186
187 The rules for starting batch and ending mode are:
188
189 1. If you are an iterator with exactly one child (FilterIterator etc.),
190 forward any StartPSIBatchMode() calls to it.
191 2. If you drive an iterator (read rows from it using a for loop
192 or similar), use PFSBatchMode as described above.
193 3. If you have multiple children, ignore the call and do your own
194 handling of batch mode as appropriate. For materialization,
195 #2 would typically apply. For joins, it depends on the join type
196 (e.g., NestedLoopIterator applies batch mode only when scanning
197 the innermost table).
198
199 The upshot of this is that when scanning a single table, batch mode
200 will typically be activated for that table (since we call
201 StartPSIBatchMode() on the root iterator, and it will trickle all the way
202 down to the table iterator), but for a join, the call will be ignored
203 and the join iterator will activate batch mode by itself as needed.
204 */
205 virtual void StartPSIBatchMode() {}
206
207 /**
208 Ends performance schema batch mode, if started. It's always safe to
209 call this.
210
211 Iterators that have children (composite iterators) must forward the
212 EndPSIBatchModeIfStarted() call to every iterator they could conceivably
213 have called StartPSIBatchMode() on. This ensures that after such a call
214 to on the root iterator, all handlers are out of batch mode.
215 */
216 virtual void EndPSIBatchModeIfStarted() {}
217
218 /**
219 If this iterator is wrapping a different iterator (e.g. TimingIterator<T>)
220 and you need to down_cast<> to a specific iterator type, this allows getting
221 at the wrapped iterator.
222 */
223 virtual RowIterator *real_iterator() { return this; }
224 virtual const RowIterator *real_iterator() const { return this; }
225
226 protected:
227 THD *thd() const { return m_thd; }
228
229 private:
230 THD *const m_thd;
231};
232
234 public:
236
237 void UnlockRow() override;
238 void SetNullRowFlag(bool is_null_row) override;
239 void StartPSIBatchMode() override;
240 void EndPSIBatchModeIfStarted() override;
241
242 protected:
243 int HandleError(int error);
244 void PrintError(int error);
245 TABLE *table() const { return m_table; }
246
247 private:
249
251};
252
253#endif // SQL_ITERATORS_ROW_ITERATOR_H_
An iterator that switches between another iterator (typically a RefIterator or similar) and a TableSc...
Definition: ref_row_iterators.h:279
Base class that is used to represent any kind of expression in a relational query.
Definition: item.h:802
Profiling data for an iterator, needed by 'EXPLAIN ANALYZE'.
Definition: row_iterator.h:40
virtual uint64_t GetNumRows() const =0
The number of rows fetched.
virtual uint64_t GetNumInitCalls() const =0
The number of loops (i.e number of iterator->Init() calls.
virtual ~IteratorProfiler()=default
virtual double GetLastRowMs() const =0
Time (in ms) spent fetching the remaining rows.
virtual double GetFirstRowMs() const =0
Time (in ms) spent fetching the first row.
Definition: sql_optimizer.h:125
A context for reading through a single table using a chosen access method: index read,...
Definition: row_iterator.h:81
THD * thd() const
Definition: row_iterator.h:227
virtual const IteratorProfiler * GetProfiler() const
Get profiling data for this iterator (for 'EXPLAIN ANALYZE').
Definition: row_iterator.h:155
virtual void SetNullRowFlag(bool is_null_row)=0
Mark the current row buffer as containing a NULL row or not, so that if you read from it and the flag...
virtual void StartPSIBatchMode()
Start performance schema batch mode, if supported (otherwise ignored).
Definition: row_iterator.h:205
virtual void UnlockRow()=0
RowIterator(const RowIterator &)=delete
virtual ~RowIterator()=default
virtual const RowIterator * real_iterator() const
Definition: row_iterator.h:224
RowIterator(RowIterator &&)=default
virtual void EndPSIBatchModeIfStarted()
Ends performance schema batch mode, if started.
Definition: row_iterator.h:216
RowIterator(THD *thd)
Definition: row_iterator.h:85
THD *const m_thd
Definition: row_iterator.h:230
virtual RowIterator * real_iterator()
If this iterator is wrapping a different iterator (e.g.
Definition: row_iterator.h:223
virtual void SetOverrideProfiler([[maybe_unused]] const IteratorProfiler *profiler)
Definition: row_iterator.h:165
virtual int Read()=0
Read a single row.
virtual bool Init()=0
Initialize or reinitialize the iterator.
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_class.h:922
Definition: row_iterator.h:233
void UnlockRow() override
The default implementation of unlock-row method of RowIterator, used in all access methods except EQR...
Definition: basic_row_iterators.cc:139
int HandleError(int error)
Definition: basic_row_iterators.cc:149
void EndPSIBatchModeIfStarted() override
Ends performance schema batch mode, if started.
Definition: basic_row_iterators.cc:172
void PrintError(int error)
Definition: basic_row_iterators.cc:164
TABLE * table() const
Definition: row_iterator.h:245
TableRowIterator(THD *thd, TABLE *table)
Definition: row_iterator.h:235
void StartPSIBatchMode() override
Start performance schema batch mode, if supported (otherwise ignored).
Definition: basic_row_iterators.cc:168
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: basic_row_iterators.cc:141
TABLE *const m_table
Definition: row_iterator.h:248
Definition: table.h:1396