MySQL  8.0.27
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>
30 class Item;
31 class JOIN;
32 class THD;
33 struct TABLE;
35 /**
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:
51 @code
52  unique_ptr<RowIterator> iterator(new ...);
53  if (iterator->Init())
54  return true;
55  while (iterator->Read() == 0) {
56  ...
57  }
58 @endcode
59  */
60 class RowIterator {
61  public:
62  // NOTE: Iterators should typically be instantiated using NewIterator,
63  // in sql/timing_iterator.h.
65  virtual ~RowIterator() = default;
67  /**
68  Initialize or reinitialize the iterator. You must always call Init()
69  before trying a Read() (but Init() does not imply Read()).
71  You can call Init() multiple times; subsequent calls will rewind the
72  iterator (or reposition it, depending on whether the iterator takes in
73  e.g. a TABLE_REF) and allow you to read the records anew.
74  */
75  virtual bool Init() = 0;
77  /**
78  Read a single row. The row data is not actually returned from the function;
79  it is put in the table's (or tables', in case of a join) record buffer, ie.,
80  table->records[0].
82  @retval
83  0 OK
84  @retval
85  -1 End of records
86  @retval
87  1 Error
88  */
89  virtual int Read() = 0;
91  /**
92  Mark the current row buffer as containing a NULL row or not, so that if you
93  read from it and the flag is true, you'll get only NULLs no matter what is
94  actually in the buffer (typically some old leftover row). This is used
95  for outer joins, when an iterator hasn't produced any rows and we need to
96  produce a NULL-complemented row. Init() or Read() won't necessarily
97  reset this flag, so if you ever set is to true, make sure to also set it
98  to false when needed.
100  Note that this can be called without Init() having been called first.
101  For example, NestedLoopIterator can hit EOF immediately on the outer
102  iterator, which means the inner iterator doesn't get an Init() call,
103  but will still forward SetNullRowFlag to both inner and outer iterators.
105  TODO: We shouldn't need this. See the comments on AggregateIterator for
106  a bit more discussion on abstracting out a row interface.
107  */
108  virtual void SetNullRowFlag(bool is_null_row) = 0;
110  // In certain queries, such as SELECT FOR UPDATE, UPDATE or DELETE queries,
111  // reading rows will automatically take locks on them. (This means that the
112  // set of locks taken will depend on whether e.g. the optimizer chose a table
113  // scan or used an index, due to InnoDB's row locking scheme with “gap locks”
114  // for B-trees instead of full predicate locks.)
115  //
116  // However, under some transaction isolation levels (READ COMMITTED or
117  // less strict), it is possible to release such locks if and only if the row
118  // failed a WHERE predicate, as only the returned rows are protected,
119  // not _which_ rows are returned. Thus, if Read() returned a row that you did
120  // not actually use, you should call UnlockRow() afterwards, which allows the
121  // storage engine to release the row lock in such situations.
122  //
123  // TableRowIterator has a default implementation of this; other iterators
124  // should usually either forward the call to their source iterator (if any)
125  // or just ignore it. The right behavior depends on the iterator.
126  virtual void UnlockRow() = 0;
128  virtual std::string TimingString() const {
129  // Valid for TimingIterator only.
130  assert(false);
131  return "";
132  }
134  /**
135  Start performance schema batch mode, if supported (otherwise ignored).
137  PFS batch mode is a mitigation to reduce the overhead of performance schema,
138  typically applied at the innermost table of the entire join. If you start
139  it before scanning the table and then end it afterwards, the entire set
140  of handler calls will be timed only once, as a group, and the costs will
141  be distributed evenly out. This reduces timer overhead.
143  If you start PFS batch mode, you must also take care to end it at the
144  end of the scan, one way or the other. Do note that this is true even
145  if the query ends abruptly (LIMIT is reached, or an error happens).
146  The easiest workaround for this is to simply call EndPSIBatchModeIfStarted()
147  on the root iterator at the end of the scan. See the PFSBatchMode class for
148  a useful helper.
150  The rules for starting batch and ending mode are:
152  1. If you are an iterator with exactly one child (FilterIterator etc.),
153  forward any StartPSIBatchMode() calls to it.
154  2. If you drive an iterator (read rows from it using a for loop
155  or similar), use PFSBatchMode as described above.
156  3. If you have multiple children, ignore the call and do your own
157  handling of batch mode as appropriate. For materialization,
158  #2 would typically apply. For joins, it depends on the join type
159  (e.g., NestedLoopIterator applies batch mode only when scanning
160  the innermost table).
162  The upshot of this is that when scanning a single table, batch mode
163  will typically be activated for that table (since we call
164  StartPSIBatchMode() on the root iterator, and it will trickle all the way
165  down to the table iterator), but for a join, the call will be ignored
166  and the join iterator will activate batch mode by itself as needed.
167  */
168  virtual void StartPSIBatchMode() {}
170  /**
171  Ends performance schema batch mode, if started. It's always safe to
172  call this.
174  Iterators that have children (composite iterators) must forward the
175  EndPSIBatchModeIfStarted() call to every iterator they could conceivably
176  have called StartPSIBatchMode() on. This ensures that after such a call
177  to on the root iterator, all handlers are out of batch mode.
178  */
179  virtual void EndPSIBatchModeIfStarted() {}
181  /**
182  If this iterator is wrapping a different iterator (e.g. TimingIterator<T>)
183  and you need to down_cast<> to a specific iterator type, this allows getting
184  at the wrapped iterator.
185  */
186  virtual RowIterator *real_iterator() { return this; }
187  virtual const RowIterator *real_iterator() const { return this; }
189  protected:
190  THD *thd() const { return m_thd; }
192  private:
193  THD *const m_thd;
194 };
197  public:
200  void UnlockRow() override;
201  void SetNullRowFlag(bool is_null_row) override;
202  void StartPSIBatchMode() override;
203  void EndPSIBatchModeIfStarted() override;
205  protected:
206  int HandleError(int error);
207  void PrintError(int error);
208  TABLE *table() const { return m_table; }
210  private:
211  TABLE *const m_table;
213  friend class AlternativeIterator;
214 };
216 #endif // SQL_ROW_ITERATOR_H_
An iterator that switches between another iterator (typically a RefIterator or similar) and a TableSc...
Definition: ref_row_iterators.h:277
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:190
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:168
virtual RowIterator * real_iterator()
If this iterator is wrapping a different iterator (e.g.
Definition: row_iterator.h:186
virtual void UnlockRow()=0
virtual ~RowIterator()=default
virtual void EndPSIBatchModeIfStarted()
Ends performance schema batch mode, if started.
Definition: row_iterator.h:179
RowIterator(THD *thd)
Definition: row_iterator.h:64
THD *const m_thd
Definition: row_iterator.h:193
virtual std::string TimingString() const
Definition: row_iterator.h:128
virtual const RowIterator * real_iterator() const
Definition: row_iterator.h:187
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:821
Definition: row_iterator.h:196
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:208
TableRowIterator(THD *thd, TABLE *table)
Definition: row_iterator.h:198
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:211
Definition: table.h:1394