MySQL 8.0.29
Source Code Documentation
Go to the documentation of this file.
4/* Copyright (c) 2018, 2021, Oracle and/or its affiliates.
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.
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.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 GNU General Public License, version 2.0, for more details.
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 */
26#include <assert.h>
27#include <string>
28#include <vector>
30class Item;
31class JOIN;
32class THD;
33struct TABLE;
36 A context for reading through a single table using a chosen access method:
37 index read, scan, etc, use of cache, etc.. It is mostly meant as an interface,
38 but also contains some private member functions that are useful for many
39 implementations, such as error handling.
41 A RowIterator is a simple iterator; you initialize it, and then read one
42 record at a time until Read() returns EOF. A RowIterator can read from
43 other Iterators if you want to, e.g., SortingIterator, which takes in records
44 from another RowIterator and sorts them.
46 The abstraction is not completely tight. In particular, it still leaves some
47 specifics to TABLE, such as which columns to read (the read_set). This means
48 it would probably be hard as-is to e.g. sort a join of two tables.
50 Use by:
52 unique_ptr<RowIterator> iterator(new ...);
53 if (iterator->Init())
54 return true;
55 while (iterator->Read() == 0) {
56 ...
57 }
59 */
61 public:
62 // NOTE: Iterators should typically be instantiated using NewIterator,
63 // in sql/iterators/timing_iterator.h.
64 explicit RowIterator(THD *thd) : m_thd(thd) {}
65 virtual ~RowIterator() = default;
67 RowIterator(const RowIterator &) = delete;
69 // Default move ctor used by IndexRangeScanIterator.
70 RowIterator(RowIterator &&) = default;
72 /**
73 Initialize or reinitialize the iterator. You must always call Init()
74 before trying a Read() (but Init() does not imply Read()).
76 You can call Init() multiple times; subsequent calls will rewind the
77 iterator (or reposition it, depending on whether the iterator takes in
78 e.g. a TABLE_REF) and allow you to read the records anew.
79 */
80 virtual bool Init() = 0;
82 /**
83 Read a single row. The row data is not actually returned from the function;
84 it is put in the table's (or tables', in case of a join) record buffer, ie.,
85 table->records[0].
87 @retval
88 0 OK
89 @retval
90 -1 End of records
91 @retval
92 1 Error
93 */
94 virtual int Read() = 0;
96 /**
97 Mark the current row buffer as containing a NULL row or not, so that if you
98 read from it and the flag is true, you'll get only NULLs no matter what is
99 actually in the buffer (typically some old leftover row). This is used
100 for outer joins, when an iterator hasn't produced any rows and we need to
101 produce a NULL-complemented row. Init() or Read() won't necessarily
102 reset this flag, so if you ever set is to true, make sure to also set it
103 to false when needed.
105 Note that this can be called without Init() having been called first.
106 For example, NestedLoopIterator can hit EOF immediately on the outer
107 iterator, which means the inner iterator doesn't get an Init() call,
108 but will still forward SetNullRowFlag to both inner and outer iterators.
110 TODO: We shouldn't need this. See the comments on AggregateIterator for
111 a bit more discussion on abstracting out a row interface.
112 */
113 virtual void SetNullRowFlag(bool is_null_row) = 0;
115 // In certain queries, such as SELECT FOR UPDATE, UPDATE or DELETE queries,
116 // reading rows will automatically take locks on them. (This means that the
117 // set of locks taken will depend on whether e.g. the optimizer chose a table
118 // scan or used an index, due to InnoDB's row locking scheme with “gap locks”
119 // for B-trees instead of full predicate locks.)
120 //
121 // However, under some transaction isolation levels (READ COMMITTED or
122 // less strict), it is possible to release such locks if and only if the row
123 // failed a WHERE predicate, as only the returned rows are protected,
124 // not _which_ rows are returned. Thus, if Read() returned a row that you did
125 // not actually use, you should call UnlockRow() afterwards, which allows the
126 // storage engine to release the row lock in such situations.
127 //
128 // TableRowIterator has a default implementation of this; other iterators
129 // should usually either forward the call to their source iterator (if any)
130 // or just ignore it. The right behavior depends on the iterator.
131 virtual void UnlockRow() = 0;
133 virtual std::string TimingString() const {
134 // Valid for TimingIterator only.
135 assert(false);
136 return "";
137 }
139 /**
140 Start performance schema batch mode, if supported (otherwise ignored).
142 PFS batch mode is a mitigation to reduce the overhead of performance schema,
143 typically applied at the innermost table of the entire join. If you start
144 it before scanning the table and then end it afterwards, the entire set
145 of handler calls will be timed only once, as a group, and the costs will
146 be distributed evenly out. This reduces timer overhead.
148 If you start PFS batch mode, you must also take care to end it at the
149 end of the scan, one way or the other. Do note that this is true even
150 if the query ends abruptly (LIMIT is reached, or an error happens).
151 The easiest workaround for this is to simply call EndPSIBatchModeIfStarted()
152 on the root iterator at the end of the scan. See the PFSBatchMode class for
153 a useful helper.
155 The rules for starting batch and ending mode are:
157 1. If you are an iterator with exactly one child (FilterIterator etc.),
158 forward any StartPSIBatchMode() calls to it.
159 2. If you drive an iterator (read rows from it using a for loop
160 or similar), use PFSBatchMode as described above.
161 3. If you have multiple children, ignore the call and do your own
162 handling of batch mode as appropriate. For materialization,
163 #2 would typically apply. For joins, it depends on the join type
164 (e.g., NestedLoopIterator applies batch mode only when scanning
165 the innermost table).
167 The upshot of this is that when scanning a single table, batch mode
168 will typically be activated for that table (since we call
169 StartPSIBatchMode() on the root iterator, and it will trickle all the way
170 down to the table iterator), but for a join, the call will be ignored
171 and the join iterator will activate batch mode by itself as needed.
172 */
173 virtual void StartPSIBatchMode() {}
175 /**
176 Ends performance schema batch mode, if started. It's always safe to
177 call this.
179 Iterators that have children (composite iterators) must forward the
180 EndPSIBatchModeIfStarted() call to every iterator they could conceivably
181 have called StartPSIBatchMode() on. This ensures that after such a call
182 to on the root iterator, all handlers are out of batch mode.
183 */
184 virtual void EndPSIBatchModeIfStarted() {}
186 /**
187 If this iterator is wrapping a different iterator (e.g. TimingIterator<T>)
188 and you need to down_cast<> to a specific iterator type, this allows getting
189 at the wrapped iterator.
190 */
191 virtual RowIterator *real_iterator() { return this; }
192 virtual const RowIterator *real_iterator() const { return this; }
194 protected:
195 THD *thd() const { return m_thd; }
197 private:
198 THD *const m_thd;
202 public:
205 void UnlockRow() override;
206 void SetNullRowFlag(bool is_null_row) override;
207 void StartPSIBatchMode() override;
208 void EndPSIBatchModeIfStarted() override;
210 protected:
211 int HandleError(int error);
212 void PrintError(int error);
213 TABLE *table() const { return m_table; }
215 private:
An iterator that switches between another iterator (typically a RefIterator or similar) and a TableSc...
Definition: ref_row_iterators.h:281
Base class that is used to represent any kind of expression in a relational query.
Definition: item.h:802
Definition: sql_optimizer.h:125
A context for reading through a single table using a chosen access method: index read,...
Definition: row_iterator.h:60
THD * thd() const
Definition: row_iterator.h:195
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:173
virtual void UnlockRow()=0
RowIterator(const RowIterator &)=delete
virtual ~RowIterator()=default
virtual const RowIterator * real_iterator() const
Definition: row_iterator.h:192
RowIterator(RowIterator &&)=default
virtual void EndPSIBatchModeIfStarted()
Ends performance schema batch mode, if started.
Definition: row_iterator.h:184
RowIterator(THD *thd)
Definition: row_iterator.h:64
THD *const m_thd
Definition: row_iterator.h:198
virtual std::string TimingString() const
Definition: row_iterator.h:133
virtual RowIterator * real_iterator()
If this iterator is wrapping a different iterator (e.g.
Definition: row_iterator.h:191
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:945
Definition: row_iterator.h:201
void UnlockRow() override
The default implementation of unlock-row method of RowIterator, used in all access methods except EQR...
int HandleError(int error)
void EndPSIBatchModeIfStarted() override
Ends performance schema batch mode, if started.
void PrintError(int error)
TABLE * table() const
Definition: row_iterator.h:213
TableRowIterator(THD *thd, TABLE *table)
Definition: row_iterator.h:203
void StartPSIBatchMode() override
Start performance schema batch mode, if supported (otherwise ignored).
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...
TABLE *const m_table
Definition: row_iterator.h:216
Definition: table.h:1394