MySQL 8.0.31
Source Code Documentation
composite_iterators.h
Go to the documentation of this file.
1#ifndef SQL_ITERATORS_COMPOSITE_ITERATORS_H_
2#define SQL_ITERATORS_COMPOSITE_ITERATORS_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/**
27 @file composite_iterators.h
28
29 A composite row iterator is one that takes in one or more existing iterators
30 and processes their rows in some interesting way. They are usually not bound
31 to a single table or similar, but are the inner (non-leaf) nodes of the
32 iterator execution tree. They consistently own their source iterator, although
33 not its memory (since we never allocate row iterators on the heap--usually on
34 a MEM_ROOT>). This means that in the end, you'll end up with a single root
35 iterator which then owns everything else recursively.
36
37 SortingIterator and the two window iterators are also composite iterators,
38 but are defined in their own files.
39 */
40
41#include <assert.h>
42#include <stdint.h>
43#include <stdio.h>
44#include <memory>
45#include <string>
46#include <utility>
47#include <vector>
48
49#include "my_alloc.h"
50#include "my_base.h"
51#include "my_inttypes.h"
52#include "my_table_map.h"
54#include "sql/join_type.h"
55#include "sql/mem_root_array.h"
56#include "sql/pack_rows.h"
57#include "sql/table.h"
58#include "sql_string.h"
59
60class Cached_item;
62class Item;
63class JOIN;
64class KEY;
67class SJ_TMP_TABLE;
68class THD;
69class Table_function;
71
72/**
73 An iterator that takes in a stream of rows and passes through only those that
74 meet some criteria (i.e., a condition evaluates to true). This is typically
75 used for WHERE/HAVING.
76 */
77class FilterIterator final : public RowIterator {
78 public:
80 Item *condition)
81 : RowIterator(thd), m_source(move(source)), m_condition(condition) {}
82
83 bool Init() override { return m_source->Init(); }
84
85 int Read() override;
86
87 void SetNullRowFlag(bool is_null_row) override {
88 m_source->SetNullRowFlag(is_null_row);
89 }
90
91 void StartPSIBatchMode() override { m_source->StartPSIBatchMode(); }
92 void EndPSIBatchModeIfStarted() override {
93 m_source->EndPSIBatchModeIfStarted();
94 }
95 void UnlockRow() override { m_source->UnlockRow(); }
96
97 private:
100};
101
102/**
103 Handles LIMIT and/or OFFSET; Init() eats the first "offset" rows, and Read()
104 stops as soon as it's seen "limit" rows (including any skipped by offset).
105 */
106class LimitOffsetIterator final : public RowIterator {
107 public:
108 /**
109 @param thd Thread context
110 @param source Row source
111 @param limit Maximum number of rows to read, including the ones skipped by
112 offset. Can be HA_POS_ERROR for no limit.
113 @param offset Number of initial rows to skip. Can be 0 for no offset.
114 @param count_all_rows If true, the query will run to completion to get
115 more accurate numbers for skipped_rows, so you will not get any
116 performance benefits of early end.
117 @param reject_multiple_rows True if a derived table transformed from a
118 scalar subquery needs a run-time cardinality check
119 @param skipped_rows If not nullptr, is incremented for each row skipped by
120 offset or limit.
121 */
123 ha_rows limit, ha_rows offset, bool count_all_rows,
124 bool reject_multiple_rows, ha_rows *skipped_rows)
125 : RowIterator(thd),
126 m_source(move(source)),
127 m_limit(limit),
128 m_offset(offset),
129 m_count_all_rows(count_all_rows),
130 m_reject_multiple_rows(reject_multiple_rows),
131 m_skipped_rows(skipped_rows) {
132 if (count_all_rows) {
133 assert(m_skipped_rows != nullptr);
134 }
135 }
136
137 bool Init() override;
138
139 int Read() override;
140
141 void SetNullRowFlag(bool is_null_row) override {
142 m_source->SetNullRowFlag(is_null_row);
143 }
144
145 void StartPSIBatchMode() override { m_source->StartPSIBatchMode(); }
146 void EndPSIBatchModeIfStarted() override {
147 m_source->EndPSIBatchModeIfStarted();
148 }
149 void UnlockRow() override { m_source->UnlockRow(); }
150
151 private:
153
154 // Note: The number of seen rows starts off at m_limit if we have OFFSET,
155 // which means we don't need separate LIMIT and OFFSET tests on the
156 // fast path of Read().
158
159 /**
160 Whether we have OFFSET rows that we still need to skip.
161 */
163
168};
169
170/**
171 Handles aggregation (typically used for GROUP BY) for the case where the rows
172 are already properly grouped coming in, ie., all rows that are supposed to be
173 part of the same group are adjacent in the input stream. (This could be
174 because they were sorted earlier, because we are scanning an index that
175 already gives us the rows in a group-compatible order, or because there is no
176 grouping.)
177
178 AggregateIterator needs to be able to save and restore rows; it doesn't know
179 when a group ends until it's seen the first row that is part of the _next_
180 group. When that happens, it needs to tuck away that next row, and then
181 restore the previous row so that the output row gets the correct grouped
182 values. A simple example, doing SELECT a, SUM(b) FROM t1 GROUP BY a:
183
184 t1.a t1.b SUM(b)
185 1 1 <-- first row, save it 1
186 1 2 3
187 1 3 6
188 2 1 <-- group changed, save row
189 [1 1] <-- restore first row, output 6
190 reset aggregate --> 0
191 [2 1] <-- restore new row, process it 1
192 2 10 11
193 <-- EOF, output 11
194
195 To save and restore rows like this, it uses the infrastructure from
196 pack_rows.h to pack and unpack all relevant rows into record[0] of every input
197 table. (Currently, there can only be one input table, but this may very well
198 change in the future.) It would be nice to have a more abstract concept of
199 sending a row around and taking copies of it if needed, as opposed to it
200 implicitly staying in the table's buffer. (This would also solve some
201 issues in EQRefIterator and when synthesizing NULL rows for outer joins.)
202 However, that's a large refactoring.
203 */
204class AggregateIterator final : public RowIterator {
205 public:
207 JOIN *join, pack_rows::TableCollection tables, bool rollup);
208
209 bool Init() override;
210 int Read() override;
211 void SetNullRowFlag(bool is_null_row) override {
212 m_source->SetNullRowFlag(is_null_row);
213 }
214
215 void StartPSIBatchMode() override { m_source->StartPSIBatchMode(); }
216 void EndPSIBatchModeIfStarted() override {
217 m_source->EndPSIBatchModeIfStarted();
218 }
219 void UnlockRow() override {
220 // Most likely, HAVING failed. Ideally, we'd like to backtrack and
221 // unlock all rows that went into this aggregate, but we can't do that,
222 // and we also can't unlock the _current_ row, since that belongs to a
223 // different group. Thus, do nothing.
224 }
225
226 private:
227 enum {
233
235
236 /**
237 The join we are part of. It would be nicer not to rely on this,
238 but we need a large number of members from there, like which
239 aggregate functions we have, the THD, temporary table parameters
240 and so on.
241 */
242 JOIN *m_join = nullptr;
243
244 /// Whether we have seen the last input row.
246
247 /**
248 Used to save NULL information in the specific case where we have
249 zero input rows.
250 */
252
253 /// Whether this is a rollup query.
254 const bool m_rollup;
255
256 /**
257 For rollup: The index of the first group item that did _not_ change when we
258 last switched groups. E.g., if we have group fields A,B,C,D and then switch
259 to group A,B,E,D, this value will become 1 (which means that we need
260 to output rollup rows for 2 -- A,B,E,NULL -- and then 1 -- A,B,NULL,NULL).
261 m_current_rollup_position will count down from the end until it becomes
262 less than this value.
263
264 If we do not have rollup, this value is perennially zero.
265 */
267
268 /**
269 If we are in state OUTPUTTING_ROLLUP_ROWS, where we are in the iteration.
270 This value will start at the index of the last group expression and then
271 count backwards down to and including m_last_unchanged_group_item_idx.
272 It is used to communicate to the rollup group items whether to turn
273 themselves into NULLs, and the sum items which of their sums to output.
274 */
276
277 /**
278 The list of tables we are reading from; they are the ones for which we need
279 to save and restore rows.
280 */
282
283 /// Packed version of the first row in the group we are currently processing.
285
286 /**
287 If applicable, packed version of the first row in the _next_ group. This is
288 used only in the LAST_ROW_STARTED_NEW_GROUP state; we just saw a row that
289 didn't belong to the current group, so we saved it here and went to output
290 a group. On the next Read() call, we need to process this deferred row
291 first of all.
292
293 Even when not in use, this string contains a buffer that is large enough to
294 pack a full row into, sans blobs. (If blobs are present,
295 StoreFromTableBuffers() will automatically allocate more space if needed.)
296 */
298
299 /**
300 The slice we're setting when returning rows. See the comment in the
301 constructor.
302 */
304
305 void SetRollupLevel(int level);
306};
307
308/**
309 A simple nested loop join, taking in two iterators (left/outer and
310 right/inner) and joining them together. This may, of course, scan the inner
311 iterator many times. It is currently the only form of join we have.
312
313 The iterator works as a state machine, where the state records whether we need
314 to read a new outer row or not, and whether we've seen any rows from the inner
315 iterator at all (if not, an outer join need to synthesize a new NULL row).
316
317 The iterator takes care of activating performance schema batch mode on the
318 right iterator if needed; this is typically only used if it is the innermost
319 table in the entire join (where the gains from turning on batch mode is the
320 largest, and the accuracy loss from turning it off are the least critical).
321 */
322class NestedLoopIterator final : public RowIterator {
323 public:
327 JoinType join_type, bool pfs_batch_mode)
328 : RowIterator(thd),
329 m_source_outer(move(source_outer)),
330 m_source_inner(move(source_inner)),
332 m_pfs_batch_mode(pfs_batch_mode) {
333 assert(m_source_outer != nullptr);
334 assert(m_source_inner != nullptr);
335
336 // Batch mode makes no sense for anti- or semijoins, since they should only
337 // be reading one row.
339 assert(!pfs_batch_mode);
340 }
341 }
342
343 bool Init() override;
344
345 int Read() override;
346
347 void SetNullRowFlag(bool is_null_row) override {
348 // TODO: write something here about why we can't do this lazily.
349 m_source_outer->SetNullRowFlag(is_null_row);
350 m_source_inner->SetNullRowFlag(is_null_row);
351 }
352
353 void EndPSIBatchModeIfStarted() override {
354 m_source_outer->EndPSIBatchModeIfStarted();
355 m_source_inner->EndPSIBatchModeIfStarted();
356 }
357
358 void UnlockRow() override {
359 // Since we don't know which condition that caused the row to be rejected,
360 // we can't know whether we could also unlock the outer row
361 // (it may still be used as parts of other joined rows).
363 m_source_inner->UnlockRow();
364 }
365 }
366
367 private:
368 enum {
374
378
379 /** Whether to use batch mode when scanning the inner iterator. */
381};
382
383/**
384 An iterator that helps invalidating caches. Every time a row passes through it
385 or it changes state in any other way, it increments its “generation” counter.
386 This allows MaterializeIterator to see whether any of its dependencies has
387 changed, and then force a rematerialization -- this is typically used for
388 LATERAL tables, where we're joining in a derived table that depends on
389 something earlier in the join.
390 */
392 public:
395 const std::string &name)
396 : RowIterator(thd),
397 m_source_iterator(move(source_iterator)),
398 m_name(name) {}
399
400 bool Init() override {
401 ++m_generation;
402 return m_source_iterator->Init();
403 }
404
405 int Read() override {
406 ++m_generation;
407 return m_source_iterator->Read();
408 }
409
410 void SetNullRowFlag(bool is_null_row) override {
411 ++m_generation;
412 m_source_iterator->SetNullRowFlag(is_null_row);
413 }
414
415 void UnlockRow() override { m_source_iterator->UnlockRow(); }
416
417 int64_t generation() const { return m_generation; }
418 std::string name() const { return m_name; }
419
420 private:
422 int64_t m_generation = 0;
423 std::string m_name;
424};
425
427/**
428 A query block to be materialized by MaterializeIterator.
429 (@see MaterializeIterator for details.)
430*/
432 /// The iterator to read the actual rows from.
434
435 /// Used only for optimizer trace.
437
438 /// The JOIN that this query block represents. Used for performance
439 /// schema batch mode: When materializing a query block that consists of
440 /// a single table, MaterializeIterator needs to set up schema batch mode,
441 /// since there is no nested loop iterator to do it. (This is similar to
442 /// what ExecuteIteratorQuery() needs to do at the top level.)
444
445 /// If true, unique constraint checking via hash key is disabled
446 /// when materializing this query block (ie., we simply avoid calling
447 /// check_unique_constraint() for each row). Used when materializing
448 /// UNION DISTINCT and UNION ALL parts into the same table.
449 /// We'd like to just use a unique constraint via unique index instead,
450 /// but there might be other indexes on the destination table
451 /// that we'd like to keep, and the implementation doesn't allow
452 /// disabling only one index.
453 ///
454 /// If you use this on a query block, doing_hash_deduplication()
455 /// must be true.
457
458 /// If set to false, the Field objects in the output row are
459 /// presumed already to be filled out. This is the case iff
460 /// there's a windowing iterator earlier in the chain.
462
463 /// The number of operands (i.e. blocks) involved in the set operation:
464 /// used for INTERSECT to determine if a value is present in all operands
466 /// The current operand (i.e. block) number, starting at zero. We use this
467 /// for INTERSECT and EXCEPT materialization operand.
469 /// Used for EXCEPT computation: the index of the first operand involved in
470 /// a N-ary except operation which has DISTINCT. This is significant for
471 /// calculating whether to set the counter to zero or just decrement it
472 /// when we see a right side operand.
474
475 /// If copy_items is true, used for copying the Field objects
476 /// into the temporary table row. Otherwise unused.
478
479 // Whether this query block is a recursive reference back to the
480 // output of the materialization.
482
483 // If is_recursive_reference is true, contains the FollowTailIterator
484 // in the query block (there can be at most one recursive reference
485 // in a join list, as per the SQL standard, so there should be exactly one).
486 // Used for informing the iterators about various shared state in the
487 // materialization (including coordinating rematerializations).
489};
490
491/**
492 Create an iterator that materializes a set of row into a temporary table
493 and sets up a (pre-existing) iterator to access that.
494 @see MaterializeIterator.
495
496 @param thd Thread handler.
497 @param query_blocks_to_materialize List of query blocks to materialize.
498 @param path_params MaterializePath settings.
499 @param table_iterator Iterator used for accessing the temporary table
500 after materialization.
501 @param join
502 When materializing within the same JOIN (e.g., into a temporary table
503 before sorting), as opposed to a derived table or a CTE, we may need
504 to change the slice on the join before returning rows from the result
505 table. If so, join and ref_slice would need to be set, and
506 query_blocks_to_materialize should contain only one member, with the same
507 join.
508 @return the iterator.
509*/
512 query_blocks_to_materialize,
513 const MaterializePathParameters *path_params,
515 JOIN *join);
516
517} // namespace materialize_iterator
518
520/**
521 Create an iterator that aggregates the output rows from another iterator
522 into a temporary table and then sets up a (pre-existing) iterator to
523 access the temporary table.
524 @see TemptableAggregateIterator.
525
526 @param thd Thread handler.
527 @param subquery_iterator input to aggregation.
528 @param temp_table_param temporary table settings.
529 @param table_iterator Iterator used for scanning the temporary table
530 after materialization.
531 @param table the temporary table.
532 @param join the JOIN in which we aggregate.
533 @param ref_slice the slice to set when accessing temporary table;
534 used if anything upstream wants to evaluate values based on its contents.
535 @return the iterator.
536*/
538 THD *thd, unique_ptr_destroy_only<RowIterator> subquery_iterator,
539 Temp_table_param *temp_table_param, TABLE *table,
541 int ref_slice);
542
543} // namespace temptable_aggregate_iterator
544
545/**
546 StreamingIterator is a minimal version of MaterializeIterator that does not
547 actually materialize; instead, every Read() just forwards the call to the
548 subquery iterator and does the required copying from one set of fields to
549 another.
550
551 It is used for when the optimizer would normally set up a materialization,
552 but you don't actually need one, ie. you don't want to read the rows multiple
553 times after writing them, and you don't want to access them by index (only
554 a single table scan). It also takes care of setting the NULL row flag
555 on the temporary table.
556 */
558 public:
559 /**
560 @param thd Thread handle.
561 @param subquery_iterator The iterator to read rows from.
562 @param temp_table_param Parameters for the temp table.
563 @param table The table we are streaming through. Will never actually
564 be written to, but its fields will be used.
565 @param provide_rowid If true, generate a row ID for each row we stream.
566 This is used if the parent needs row IDs for deduplication, in particular
567 weedout.
568 @param join See MaterializeIterator.
569 @param ref_slice See MaterializeIterator.
570 */
572 unique_ptr_destroy_only<RowIterator> subquery_iterator,
573 Temp_table_param *temp_table_param, TABLE *table,
574 bool provide_rowid, JOIN *join, int ref_slice);
575
576 bool Init() override;
577
578 int Read() override;
579
580 void StartPSIBatchMode() override {
581 m_subquery_iterator->StartPSIBatchMode();
582 }
583 void EndPSIBatchModeIfStarted() override {
584 m_subquery_iterator->EndPSIBatchModeIfStarted();
585 }
586 void UnlockRow() override { m_subquery_iterator->UnlockRow(); }
587
588 private:
592 JOIN *const m_join;
593 const int m_output_slice;
595
596 // Whether the iterator should generate and provide a row ID. Only true if the
597 // iterator is part of weedout, where the iterator will create a fake row ID
598 // to uniquely identify the rows it produces.
599 const bool m_provide_rowid;
600};
601
602/**
603 An iterator that wraps a Table_function (e.g. JSON_TABLE) and allows you to
604 iterate over the materialized temporary table. The table is materialized anew
605 for every Init().
606
607 TODO: Just wrapping it is probably not the optimal thing to do;
608 Table_function is highly oriented around materialization, but never caches.
609 Thus, perhaps we should rewrite Table_function to return a RowIterator
610 instead of going through a temporary table.
611 */
613 public:
615 THD *thd, Table_function *table_function, TABLE *table,
617
618 bool Init() override;
619 int Read() override { return m_table_iterator->Read(); }
620 void SetNullRowFlag(bool is_null_row) override {
621 m_table_iterator->SetNullRowFlag(is_null_row);
622 }
623
624 void StartPSIBatchMode() override { m_table_iterator->StartPSIBatchMode(); }
625 void EndPSIBatchModeIfStarted() override {
626 m_table_iterator->EndPSIBatchModeIfStarted();
627 }
628
629 // The temporary table is private to us, so there's no need to worry about
630 // locks to other transactions.
631 void UnlockRow() override {}
632
633 private:
635
637};
638
639/**
640 Like semijoin materialization, weedout works on the basic idea that a semijoin
641 is just like an inner join as we long as we can get rid of the duplicates
642 somehow. (This is advantageous, because inner joins can be reordered, whereas
643 semijoins generally can't.) However, unlike semijoin materialization, weedout
644 removes duplicates after the join, not before it. Consider something like
645
646 SELECT * FROM t1 WHERE a IN ( SELECT b FROM t2 );
647
648 Semijoin materialization solves this by materializing t2, with deduplication,
649 and then joining. Weedout joins t1 to t2 and then leaves only one output row
650 per t1 row. The disadvantage is that this potentially needs to discard more
651 rows; the (potential) advantage is that we deduplicate on t1 instead of t2.
652
653 Weedout, unlike materialization, works in a streaming fashion; rows are output
654 (or discarded) as they come in, with a temporary table used for recording the
655 row IDs we've seen before. (We need to deduplicate on t1's row IDs, not its
656 contents.) See SJ_TMP_TABLE for details about the table format.
657 */
658class WeedoutIterator final : public RowIterator {
659 public:
661 SJ_TMP_TABLE *sj, table_map tables_to_get_rowid_for);
662
663 bool Init() override;
664 int Read() override;
665
666 void SetNullRowFlag(bool is_null_row) override {
667 m_source->SetNullRowFlag(is_null_row);
668 }
669
670 void EndPSIBatchModeIfStarted() override {
671 m_source->EndPSIBatchModeIfStarted();
672 }
673 void UnlockRow() override { m_source->UnlockRow(); }
674
675 private:
679};
680
681/**
682 An iterator that removes consecutive rows that are the same according to
683 a set of items (typically the join key), so-called “loose scan”
684 (not to be confused with “loose index scan”, which is made by the
685 range optimizer). This is similar in spirit to WeedoutIterator above
686 (removing duplicates allows us to treat the semijoin as a normal join),
687 but is much cheaper if the data is already ordered/grouped correctly,
688 as the removal can happen before the join, and it does not need a
689 temporary table.
690 */
692 public:
695 JOIN *join, Item **group_items,
696 int group_items_size);
697
698 bool Init() override;
699 int Read() override;
700
701 void SetNullRowFlag(bool is_null_row) override {
702 m_source->SetNullRowFlag(is_null_row);
703 }
704
705 void StartPSIBatchMode() override { m_source->StartPSIBatchMode(); }
706 void EndPSIBatchModeIfStarted() override {
707 m_source->EndPSIBatchModeIfStarted();
708 }
709 void UnlockRow() override { m_source->UnlockRow(); }
710
711 private:
715};
716
717/**
718 Much like RemoveDuplicatesIterator, but works on the basis of a given index
719 (or more accurately, its keypart), not an arbitrary list of grouped fields.
720 This is only used in the non-hypergraph optimizer; the hypergraph optimizer
721 can deal with groupings that come from e.g. sorts.
722 */
724 public:
727 const TABLE *table, KEY *key, size_t key_len);
728
729 bool Init() override;
730 int Read() override;
731
732 void SetNullRowFlag(bool is_null_row) override {
733 m_source->SetNullRowFlag(is_null_row);
734 }
735
736 void StartPSIBatchMode() override { m_source->StartPSIBatchMode(); }
737 void EndPSIBatchModeIfStarted() override {
738 m_source->EndPSIBatchModeIfStarted();
739 }
740 void UnlockRow() override { m_source->UnlockRow(); }
741
742 private:
746 uchar *m_key_buf; // Owned by the THD's MEM_ROOT.
747 const size_t m_key_len;
749};
750
751/**
752 An iterator that is semantically equivalent to a semijoin NestedLoopIterator
753 immediately followed by a RemoveDuplicatesOnIndexIterator. It is used to
754 implement the “loose scan” strategy in queries with multiple tables on the
755 inside of a semijoin, like
756
757 ... FROM t1 WHERE ... IN ( SELECT ... FROM t2 JOIN t3 ... )
758
759 In this case, the query tree without this iterator would ostensibly look like
760
761 -> Nested loop join
762 -> Table scan on t1
763 -> Remove duplicates on t2_idx
764 -> Nested loop semijoin
765 -> Index scan on t2 using t2_idx
766 -> Filter (e.g. t3.a = t2.a)
767 -> Table scan on t3
768
769 (t3 will be marked as “first match” on t2 when implementing loose scan,
770 thus the semijoin.)
771
772 First note that we can't put the duplicate removal directly on t2 in this
773 case, as the first t2 row doesn't necessarily match anything in t3, so it
774 needs to be above. However, this is wasteful, because once we find a matching
775 t2/t3 pair, we should stop scanning t3 until we have a new t2.
776
777 NestedLoopSemiJoinWithDuplicateRemovalIterator solves the problem by doing
778 exactly this; it gets a row from the outer side, gets exactly one row from the
779 inner side, and then skips over rows from the outer side (_without_ scanning
780 the inner side) until its keypart changes.
781 */
783 : public RowIterator {
784 public:
787 unique_ptr_destroy_only<RowIterator> source_inner, const TABLE *table,
788 KEY *key, size_t key_len);
789
790 bool Init() override;
791
792 int Read() override;
793
794 void SetNullRowFlag(bool is_null_row) override {
795 m_source_outer->SetNullRowFlag(is_null_row);
796 m_source_inner->SetNullRowFlag(is_null_row);
797 }
798
799 void EndPSIBatchModeIfStarted() override {
800 m_source_outer->EndPSIBatchModeIfStarted();
801 m_source_inner->EndPSIBatchModeIfStarted();
802 }
803
804 void UnlockRow() override {
805 m_source_outer->UnlockRow();
806 m_source_inner->UnlockRow();
807 }
808
809 private:
812
815 uchar *m_key_buf; // Owned by the THD's MEM_ROOT.
816 const size_t m_key_len;
818};
819
820/**
821 MaterializeInformationSchemaTableIterator makes sure a given I_S temporary
822 table is materialized (filled out) before we try to scan it.
823 */
825 public:
828 TABLE_LIST *table_list, Item *condition);
829
830 bool Init() override;
831 int Read() override { return m_table_iterator->Read(); }
832
833 void SetNullRowFlag(bool is_null_row) override {
834 m_table_iterator->SetNullRowFlag(is_null_row);
835 }
836
837 void StartPSIBatchMode() override { m_table_iterator->StartPSIBatchMode(); }
838 void EndPSIBatchModeIfStarted() override {
839 m_table_iterator->EndPSIBatchModeIfStarted();
840 }
841
842 // The temporary table is private to us, so there's no need to worry about
843 // locks to other transactions.
844 void UnlockRow() override {}
845
846 private:
847 /// The iterator that reads from the materialized table.
851};
852
853/**
854 Takes in two or more iterators and output rows from them sequentially
855 (first all rows from the first one, the all from the second one, etc.).
856 Used for implementing UNION ALL, typically together with StreamingIterator.
857 */
858class AppendIterator final : public RowIterator {
859 public:
861 THD *thd,
863
864 bool Init() override;
865 int Read() override;
866
867 void StartPSIBatchMode() override;
868 void EndPSIBatchModeIfStarted() override;
869
870 void SetNullRowFlag(bool is_null_row) override;
871 void UnlockRow() override;
872
873 private:
874 std::vector<unique_ptr_destroy_only<RowIterator>> m_sub_iterators;
877};
878
879#endif // SQL_ITERATORS_COMPOSITE_ITERATORS_H_
Handles aggregation (typically used for GROUP BY) for the case where the rows are already properly gr...
Definition: composite_iterators.h:204
void UnlockRow() override
Definition: composite_iterators.h:219
int m_current_rollup_position
If we are in state OUTPUTTING_ROLLUP_ROWS, where we are in the iteration.
Definition: composite_iterators.h:275
@ LAST_ROW_STARTED_NEW_GROUP
Definition: composite_iterators.h:229
@ READING_FIRST_ROW
Definition: composite_iterators.h:228
@ OUTPUTTING_ROLLUP_ROWS
Definition: composite_iterators.h:230
@ DONE_OUTPUTTING_ROWS
Definition: composite_iterators.h:231
void StartPSIBatchMode() override
Start performance schema batch mode, if supported (otherwise ignored).
Definition: composite_iterators.h:215
int Read() override
Read a single row.
Definition: composite_iterators.cc:225
JOIN * m_join
The join we are part of.
Definition: composite_iterators.h:242
bool m_seen_eof
Whether we have seen the last input row.
Definition: composite_iterators.h:245
pack_rows::TableCollection m_tables
The list of tables we are reading from; they are the ones for which we need to save and restore rows.
Definition: composite_iterators.h:281
String m_first_row_this_group
Packed version of the first row in the group we are currently processing.
Definition: composite_iterators.h:284
String m_first_row_next_group
If applicable, packed version of the first row in the next group.
Definition: composite_iterators.h:297
table_map m_save_nullinfo
Used to save NULL information in the specific case where we have zero input rows.
Definition: composite_iterators.h:251
bool Init() override
Initialize or reinitialize the iterator.
Definition: composite_iterators.cc:188
unique_ptr_destroy_only< RowIterator > m_source
Definition: composite_iterators.h:234
int m_output_slice
The slice we're setting when returning rows.
Definition: composite_iterators.h:303
AggregateIterator(THD *thd, unique_ptr_destroy_only< RowIterator > source, JOIN *join, pack_rows::TableCollection tables, bool rollup)
Definition: composite_iterators.cc:175
void EndPSIBatchModeIfStarted() override
Ends performance schema batch mode, if started.
Definition: composite_iterators.h:216
const bool m_rollup
Whether this is a rollup query.
Definition: composite_iterators.h:254
enum AggregateIterator::@53 m_state
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: composite_iterators.h:211
void SetRollupLevel(int level)
Definition: composite_iterators.cc:435
int m_last_unchanged_group_item_idx
For rollup: The index of the first group item that did not change when we last switched groups.
Definition: composite_iterators.h:266
Takes in two or more iterators and output rows from them sequentially (first all rows from the first ...
Definition: composite_iterators.h:858
size_t m_current_iterator_index
Definition: composite_iterators.h:875
AppendIterator(THD *thd, std::vector< unique_ptr_destroy_only< RowIterator > > &&sub_iterators)
Definition: composite_iterators.cc:2207
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: composite_iterators.cc:2244
bool Init() override
Initialize or reinitialize the iterator.
Definition: composite_iterators.cc:2213
void EndPSIBatchModeIfStarted() override
Ends performance schema batch mode, if started.
Definition: composite_iterators.cc:2254
void UnlockRow() override
Definition: composite_iterators.cc:2262
std::vector< unique_ptr_destroy_only< RowIterator > > m_sub_iterators
Definition: composite_iterators.h:874
int Read() override
Read a single row.
Definition: composite_iterators.cc:2219
bool m_pfs_batch_mode_enabled
Definition: composite_iterators.h:876
void StartPSIBatchMode() override
Start performance schema batch mode, if supported (otherwise ignored).
Definition: composite_iterators.cc:2249
A wrapper class which provides array bounds checking.
Definition: sql_array.h:46
An iterator that helps invalidating caches.
Definition: composite_iterators.h:391
bool Init() override
Initialize or reinitialize the iterator.
Definition: composite_iterators.h:400
CacheInvalidatorIterator(THD *thd, unique_ptr_destroy_only< RowIterator > source_iterator, const std::string &name)
Definition: composite_iterators.h:393
std::string m_name
Definition: composite_iterators.h:423
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: composite_iterators.h:410
unique_ptr_destroy_only< RowIterator > m_source_iterator
Definition: composite_iterators.h:421
void UnlockRow() override
Definition: composite_iterators.h:415
int64_t m_generation
Definition: composite_iterators.h:422
int Read() override
Read a single row.
Definition: composite_iterators.h:405
std::string name() const
Definition: composite_iterators.h:418
int64_t generation() const
Definition: composite_iterators.h:417
This is used for segregating rows in groups (e.g.
Definition: item.h:6182
An iterator that takes in a stream of rows and passes through only those that meet some criteria (i....
Definition: composite_iterators.h:77
void EndPSIBatchModeIfStarted() override
Ends performance schema batch mode, if started.
Definition: composite_iterators.h:92
int Read() override
Read a single row.
Definition: composite_iterators.cc:74
void UnlockRow() override
Definition: composite_iterators.h:95
bool Init() override
Initialize or reinitialize the iterator.
Definition: composite_iterators.h:83
unique_ptr_destroy_only< RowIterator > m_source
Definition: composite_iterators.h:98
FilterIterator(THD *thd, unique_ptr_destroy_only< RowIterator > source, Item *condition)
Definition: composite_iterators.h:79
void StartPSIBatchMode() override
Start performance schema batch mode, if supported (otherwise ignored).
Definition: composite_iterators.h:91
Item * m_condition
Definition: composite_iterators.h:99
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: composite_iterators.h:87
FollowTailIterator is a special version of TableScanIterator that is used as part of WITH RECURSIVE q...
Definition: basic_row_iterators.h:438
Base class that is used to represent any kind of expression in a relational query.
Definition: item.h:802
Definition: sql_optimizer.h:125
Definition: key.h:112
Handles LIMIT and/or OFFSET; Init() eats the first "offset" rows, and Read() stops as soon as it's se...
Definition: composite_iterators.h:106
void UnlockRow() override
Definition: composite_iterators.h:149
LimitOffsetIterator(THD *thd, unique_ptr_destroy_only< RowIterator > source, ha_rows limit, ha_rows offset, bool count_all_rows, bool reject_multiple_rows, ha_rows *skipped_rows)
Definition: composite_iterators.h:122
bool Init() override
Initialize or reinitialize the iterator.
Definition: composite_iterators.cc:99
ha_rows m_seen_rows
Definition: composite_iterators.h:157
const bool m_count_all_rows
Definition: composite_iterators.h:165
ha_rows * m_skipped_rows
Definition: composite_iterators.h:167
void EndPSIBatchModeIfStarted() override
Ends performance schema batch mode, if started.
Definition: composite_iterators.h:146
const ha_rows m_limit
Definition: composite_iterators.h:164
const ha_rows m_offset
Definition: composite_iterators.h:164
bool m_needs_offset
Whether we have OFFSET rows that we still need to skip.
Definition: composite_iterators.h:162
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: composite_iterators.h:141
unique_ptr_destroy_only< RowIterator > m_source
Definition: composite_iterators.h:152
int Read() override
Read a single row.
Definition: composite_iterators.cc:113
const bool m_reject_multiple_rows
Definition: composite_iterators.h:166
void StartPSIBatchMode() override
Start performance schema batch mode, if supported (otherwise ignored).
Definition: composite_iterators.h:145
MaterializeInformationSchemaTableIterator makes sure a given I_S temporary table is materialized (fil...
Definition: composite_iterators.h:824
Item * m_condition
Definition: composite_iterators.h:850
TABLE_LIST * m_table_list
Definition: composite_iterators.h:849
void UnlockRow() override
Definition: composite_iterators.h:844
MaterializeInformationSchemaTableIterator(THD *thd, unique_ptr_destroy_only< RowIterator > table_iterator, TABLE_LIST *table_list, Item *condition)
Definition: composite_iterators.cc:2182
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: composite_iterators.h:833
void EndPSIBatchModeIfStarted() override
Ends performance schema batch mode, if started.
Definition: composite_iterators.h:838
int Read() override
Read a single row.
Definition: composite_iterators.h:831
void StartPSIBatchMode() override
Start performance schema batch mode, if supported (otherwise ignored).
Definition: composite_iterators.h:837
unique_ptr_destroy_only< RowIterator > m_table_iterator
The iterator that reads from the materialized table.
Definition: composite_iterators.h:848
bool Init() override
Initialize or reinitialize the iterator.
Definition: composite_iterators.cc:2190
An iterator that wraps a Table_function (e.g.
Definition: composite_iterators.h:612
Table_function * m_table_function
Definition: composite_iterators.h:636
unique_ptr_destroy_only< RowIterator > m_table_iterator
Definition: composite_iterators.h:634
void StartPSIBatchMode() override
Start performance schema batch mode, if supported (otherwise ignored).
Definition: composite_iterators.h:624
void UnlockRow() override
Definition: composite_iterators.h:631
int Read() override
Read a single row.
Definition: composite_iterators.h:619
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: composite_iterators.h:620
void EndPSIBatchModeIfStarted() override
Ends performance schema batch mode, if started.
Definition: composite_iterators.h:625
MaterializedTableFunctionIterator(THD *thd, Table_function *table_function, TABLE *table, unique_ptr_destroy_only< RowIterator > table_iterator)
Definition: composite_iterators.cc:1939
bool Init() override
Initialize or reinitialize the iterator.
Definition: composite_iterators.cc:1946
A typesafe replacement for DYNAMIC_ARRAY.
Definition: mem_root_array.h:425
A simple nested loop join, taking in two iterators (left/outer and right/inner) and joining them toge...
Definition: composite_iterators.h:322
bool Init() override
Initialize or reinitialize the iterator.
Definition: composite_iterators.cc:447
int Read() override
Read a single row.
Definition: composite_iterators.cc:458
@ END_OF_ROWS
Definition: composite_iterators.h:372
@ READING_INNER_ROWS
Definition: composite_iterators.h:371
@ NEEDS_OUTER_ROW
Definition: composite_iterators.h:369
@ READING_FIRST_INNER_ROW
Definition: composite_iterators.h:370
void UnlockRow() override
Definition: composite_iterators.h:358
const bool m_pfs_batch_mode
Whether to use batch mode when scanning the inner iterator.
Definition: composite_iterators.h:380
NestedLoopIterator(THD *thd, unique_ptr_destroy_only< RowIterator > source_outer, unique_ptr_destroy_only< RowIterator > source_inner, JoinType join_type, bool pfs_batch_mode)
Definition: composite_iterators.h:324
unique_ptr_destroy_only< RowIterator > const m_source_inner
Definition: composite_iterators.h:376
void EndPSIBatchModeIfStarted() override
Ends performance schema batch mode, if started.
Definition: composite_iterators.h:353
const JoinType m_join_type
Definition: composite_iterators.h:377
enum NestedLoopIterator::@54 m_state
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: composite_iterators.h:347
unique_ptr_destroy_only< RowIterator > const m_source_outer
Definition: composite_iterators.h:375
An iterator that is semantically equivalent to a semijoin NestedLoopIterator immediately followed by ...
Definition: composite_iterators.h:783
int Read() override
Read a single row.
Definition: composite_iterators.cc:2126
void UnlockRow() override
Definition: composite_iterators.h:804
KEY * m_key
Definition: composite_iterators.h:814
unique_ptr_destroy_only< RowIterator > const m_source_outer
Definition: composite_iterators.h:810
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: composite_iterators.h:794
const size_t m_key_len
Definition: composite_iterators.h:816
bool m_deduplicate_against_previous_row
Definition: composite_iterators.h:817
uchar * m_key_buf
Definition: composite_iterators.h:815
unique_ptr_destroy_only< RowIterator > const m_source_inner
Definition: composite_iterators.h:811
NestedLoopSemiJoinWithDuplicateRemovalIterator(THD *thd, unique_ptr_destroy_only< RowIterator > source_outer, unique_ptr_destroy_only< RowIterator > source_inner, const TABLE *table, KEY *key, size_t key_len)
Definition: composite_iterators.cc:2103
void EndPSIBatchModeIfStarted() override
Ends performance schema batch mode, if started.
Definition: composite_iterators.h:799
const TABLE * m_table_outer
Definition: composite_iterators.h:813
bool Init() override
Initialize or reinitialize the iterator.
Definition: composite_iterators.cc:2118
This class represents a query expression (one query block or several query blocks combined with UNION...
Definition: sql_lex.h:622
An iterator that removes consecutive rows that are the same according to a set of items (typically th...
Definition: composite_iterators.h:691
void StartPSIBatchMode() override
Start performance schema batch mode, if supported (otherwise ignored).
Definition: composite_iterators.h:705
void EndPSIBatchModeIfStarted() override
Ends performance schema batch mode, if started.
Definition: composite_iterators.h:706
void UnlockRow() override
Definition: composite_iterators.h:709
int Read() override
Read a single row.
Definition: composite_iterators.cc:2037
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: composite_iterators.h:701
bool m_first_row
Definition: composite_iterators.h:714
bool Init() override
Initialize or reinitialize the iterator.
Definition: composite_iterators.cc:2032
Bounds_checked_array< Cached_item * > m_caches
Definition: composite_iterators.h:713
RemoveDuplicatesIterator(THD *thd, unique_ptr_destroy_only< RowIterator > source, JOIN *join, Item **group_items, int group_items_size)
Definition: composite_iterators.cc:2020
unique_ptr_destroy_only< RowIterator > m_source
Definition: composite_iterators.h:712
Much like RemoveDuplicatesIterator, but works on the basis of a given index (or more accurately,...
Definition: composite_iterators.h:723
bool Init() override
Initialize or reinitialize the iterator.
Definition: composite_iterators.cc:2074
void UnlockRow() override
Definition: composite_iterators.h:740
uchar * m_key_buf
Definition: composite_iterators.h:746
bool m_first_row
Definition: composite_iterators.h:748
const TABLE * m_table
Definition: composite_iterators.h:744
void StartPSIBatchMode() override
Start performance schema batch mode, if supported (otherwise ignored).
Definition: composite_iterators.h:736
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: composite_iterators.h:732
const size_t m_key_len
Definition: composite_iterators.h:747
void EndPSIBatchModeIfStarted() override
Ends performance schema batch mode, if started.
Definition: composite_iterators.h:737
RemoveDuplicatesOnIndexIterator(THD *thd, unique_ptr_destroy_only< RowIterator > source, const TABLE *table, KEY *key, size_t key_len)
Definition: composite_iterators.cc:2064
KEY * m_key
Definition: composite_iterators.h:745
int Read() override
Read a single row.
Definition: composite_iterators.cc:2079
unique_ptr_destroy_only< RowIterator > m_source
Definition: composite_iterators.h:743
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
Definition: sql_executor.h:103
StreamingIterator is a minimal version of MaterializeIterator that does not actually materialize; ins...
Definition: composite_iterators.h:557
JOIN *const m_join
Definition: composite_iterators.h:592
void UnlockRow() override
Definition: composite_iterators.h:586
void StartPSIBatchMode() override
Start performance schema batch mode, if supported (otherwise ignored).
Definition: composite_iterators.h:580
int Read() override
Read a single row.
Definition: composite_iterators.cc:1532
StreamingIterator(THD *thd, unique_ptr_destroy_only< RowIterator > subquery_iterator, Temp_table_param *temp_table_param, TABLE *table, bool provide_rowid, JOIN *join, int ref_slice)
Definition: composite_iterators.cc:1480
Temp_table_param * m_temp_table_param
Definition: composite_iterators.h:590
const bool m_provide_rowid
Definition: composite_iterators.h:599
const int m_output_slice
Definition: composite_iterators.h:593
void EndPSIBatchModeIfStarted() override
Ends performance schema batch mode, if started.
Definition: composite_iterators.h:583
bool Init() override
Initialize or reinitialize the iterator.
Definition: composite_iterators.cc:1509
int m_input_slice
Definition: composite_iterators.h:594
unique_ptr_destroy_only< RowIterator > m_subquery_iterator
Definition: composite_iterators.h:589
ha_rows m_row_number
Definition: composite_iterators.h:591
Using this class is fraught with peril, and you need to be very careful when doing so.
Definition: sql_string.h:166
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
TABLE * table() const
Definition: row_iterator.h:245
Class representing a table function.
Definition: table_function.h:52
Object containing parameters used when creating and using temporary tables.
Definition: temp_table_param.h:94
Like semijoin materialization, weedout works on the basic idea that a semijoin is just like an inner ...
Definition: composite_iterators.h:658
unique_ptr_destroy_only< RowIterator > m_source
Definition: composite_iterators.h:676
void EndPSIBatchModeIfStarted() override
Ends performance schema batch mode, if started.
Definition: composite_iterators.h:670
WeedoutIterator(THD *thd, unique_ptr_destroy_only< RowIterator > source, SJ_TMP_TABLE *sj, table_map tables_to_get_rowid_for)
Definition: composite_iterators.cc:1959
int Read() override
Read a single row.
Definition: composite_iterators.cc:1989
const table_map m_tables_to_get_rowid_for
Definition: composite_iterators.h:678
SJ_TMP_TABLE * m_sj
Definition: composite_iterators.h:677
void UnlockRow() override
Definition: composite_iterators.h:673
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: composite_iterators.h:666
bool Init() override
Initialize or reinitialize the iterator.
Definition: composite_iterators.cc:1972
A structure that contains a list of tables for the hash join operation, and some pre-computed propert...
Definition: pack_rows.h:83
JoinType
Definition: join_type.h:27
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:488
This file includes constants used by all storage engines.
my_off_t ha_rows
Definition: my_base.h:1139
Some integer typedefs for easier portability.
unsigned long long int ulonglong
Definition: my_inttypes.h:55
unsigned char uchar
Definition: my_inttypes.h:51
uint64_t table_map
Definition: my_table_map.h:29
Definition: composite_iterators.h:426
RowIterator * CreateIterator(THD *thd, Mem_root_array< materialize_iterator::QueryBlock > query_blocks_to_materialize, const MaterializePathParameters *path_params, unique_ptr_destroy_only< RowIterator > table_iterator, JOIN *join)
Create an iterator that materializes a set of row into a temporary table and sets up a (pre-existing)...
Definition: composite_iterators.cc:1453
std::string join(Container cont, const std::string &delim)
join elements of an container into a string separated by a delimiter.
Definition: string.h:150
Definition: composite_iterators.h:519
RowIterator * CreateIterator(THD *thd, unique_ptr_destroy_only< RowIterator > subquery_iterator, Temp_table_param *temp_table_param, TABLE *table, unique_ptr_destroy_only< RowIterator > table_iterator, JOIN *join, int ref_slice)
Create an iterator that aggregates the output rows from another iterator into a temporary table and t...
Definition: composite_iterators.cc:1911
std::vector< T, ut::allocator< T > > vector
Specialization of vector which uses allocator.
Definition: ut0new.h:2855
Generic routines for packing rows (possibly from multiple tables at the same time) into strings,...
required string key
Definition: replication_asynchronous_connection_failover.proto:59
repeated Source source
Definition: replication_asynchronous_connection_failover.proto:41
join_type
Definition: sql_opt_exec_shared.h:177
Our own string classes, used pervasively throughout the executor.
Definition: materialize_path_parameters.h:39
Definition: table.h:2756
Definition: table.h:1396
A query block to be materialized by MaterializeIterator.
Definition: composite_iterators.h:431
int select_number
Used only for optimizer trace.
Definition: composite_iterators.h:436
ulonglong m_total_operands
The number of operands (i.e.
Definition: composite_iterators.h:465
JOIN * join
The JOIN that this query block represents.
Definition: composite_iterators.h:443
Temp_table_param * temp_table_param
If copy_items is true, used for copying the Field objects into the temporary table row.
Definition: composite_iterators.h:477
ulonglong m_operand_idx
The current operand (i.e.
Definition: composite_iterators.h:468
unique_ptr_destroy_only< RowIterator > subquery_iterator
The iterator to read the actual rows from.
Definition: composite_iterators.h:433
uint m_first_distinct
Used for EXCEPT computation: the index of the first operand involved in a N-ary except operation whic...
Definition: composite_iterators.h:473
FollowTailIterator * recursive_reader
Definition: composite_iterators.h:488
bool is_recursive_reference
Definition: composite_iterators.h:481
bool disable_deduplication_by_hash_field
If true, unique constraint checking via hash key is disabled when materializing this query block (ie....
Definition: composite_iterators.h:456
bool copy_items
If set to false, the Field objects in the output row are presumed already to be filled out.
Definition: composite_iterators.h:461
unsigned int uint
Definition: uca-dump.cc:29