forked from alibaba/AliSQL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaccess_path.h
More file actions
1802 lines (1659 loc) · 64.8 KB
/
access_path.h
File metadata and controls
1802 lines (1659 loc) · 64.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (c) 2020, 2025, Oracle and/or its affiliates.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is designed to work with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have either included with
the program or referenced in the documentation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License, version 2.0, for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
#ifndef SQL_JOIN_OPTIMIZER_ACCESS_PATH_H
#define SQL_JOIN_OPTIMIZER_ACCESS_PATH_H
#include <assert.h>
#include <stdint.h>
#include <string>
#include <type_traits>
#include <vector>
#include "sql/iterators/row_iterator.h"
#include "sql/join_optimizer/interesting_orders_defs.h"
#include "sql/join_optimizer/materialize_path_parameters.h"
#include "sql/join_optimizer/node_map.h"
#include "sql/join_optimizer/overflow_bitset.h"
#include "sql/join_optimizer/relational_expression.h"
#include "sql/join_type.h"
#include "sql/mem_root_array.h"
#include "sql/sql_array.h"
#include "sql/sql_class.h"
#include "sql/table.h"
template <class T>
class Bounds_checked_array;
class Common_table_expr;
class Filesort;
class Item;
class Item_func_match;
class JOIN;
class KEY;
class QEP_TAB;
class QUICK_RANGE;
class SJ_TMP_TABLE;
class Table_function;
class Temp_table_param;
class Window;
struct AccessPath;
struct GroupIndexSkipScanParameters;
struct IndexSkipScanParameters;
struct Index_lookup;
struct KEY_PART;
struct ORDER;
struct POSITION;
struct RelationalExpression;
struct TABLE;
/**
A specification that two specific relational expressions
(e.g., two tables, or a table and a join between two other tables)
should be joined together. The actual join conditions, if any,
live inside the “expr” object, as does the join type etc.
*/
struct JoinPredicate {
RelationalExpression *expr;
double selectivity;
// If this join is made using a hash join, estimates the width
// of each row as stored in the hash table, in bytes.
size_t estimated_bytes_per_row;
// The set of (additional) functional dependencies that are active
// after this join predicate has been applied. E.g. if we're joining
// on t1.x = t2.x, there will be a bit for that functional dependency.
// We don't currently support more complex join conditions, but there's
// no conceptual reason why we couldn't, e.g. a join on a = b + c
// could give rise to the FD {b, c} → a and possibly even {a, b} → c
// or {a, c} → b.
//
// Used in the processing of interesting orders.
FunctionalDependencySet functional_dependencies;
// A less compact form of functional_dependencies, used during building
// (FunctionalDependencySet bitmaps are only available after all functional
// indexes have been collected and Build() has been called).
Mem_root_array<int> functional_dependencies_idx;
// If this is a suitable semijoin: Contains the grouping given by the
// join key. If the rows are in this grouping, then the join optimizer will
// consider deduplicating on it and inverting the join. -1 otherwise.
int ordering_idx_needed_for_semijoin_rewrite = -1;
// Same as ordering_idx_needed_for_semijoin_rewrite, but given to the
// RemoveDuplicatesIterator for doing the actual grouping. Allocated
// on the MEM_ROOT. Can be empty, in which case a LIMIT 1 would do.
Item **semijoin_group = nullptr;
int semijoin_group_size = 0;
};
/**
A filter of some sort that is not a join condition (those are stored
in JoinPredicate objects). AND conditions are typically split up into
multiple Predicates.
*/
struct Predicate {
Item *condition;
// condition->used_tables(), converted to a NodeMap.
hypergraph::NodeMap used_nodes;
// tables referred to by the condition, plus any tables whose values
// can null any of those tables. (Even when reordering outer joins,
// at least one of those tables will still be present on the
// left-hand side of the outer join, so this is sufficient.)
//
// As a special case, we allow setting RAND_TABLE_BIT, even though it
// is normally part of a table_map, not a NodeMap.
hypergraph::NodeMap total_eligibility_set;
double selectivity;
// Whether this predicate is a join condition after all; it was promoted
// to a WHERE predicate since it was part of a cycle (see the comment in
// AddCycleEdges()). If it is, it is usually ignored so that we don't
// double-apply join conditions -- but if the join in question was not
// applied (because the cycle was broken at this point), the predicate
// would come into play. This is normally registered on the join itself
// (see RelationalExpression::join_predicate_bitmap), but having the bit
// on the predicate itself is used to avoid trying to push it down as a
// sargable predicate.
bool was_join_condition = false;
// If this is a join condition that came from a multiple equality,
// and we have decided to create a mesh from that multiple equality,
// returns the index of it into the “multiple_equalities” array
// in MakeJoinHypergraph(). (You don't actually need the array to
// use this; it's just an opaque index to deduplicate between different
// predicates.) Otherwise, -1.
int source_multiple_equality_idx = -1;
// See the equivalent fields in JoinPredicate.
FunctionalDependencySet functional_dependencies;
Mem_root_array<int> functional_dependencies_idx;
// The list of all subqueries referred to in this predicate, if any.
// The optimizer uses this to add their materialized/non-materialized
// costs when evaluating filters.
Mem_root_array<ContainedSubquery> contained_subqueries;
};
struct AppendPathParameters {
AccessPath *path;
JOIN *join;
};
/// To indicate that a row estimate is not yet made.
constexpr double kUnknownRowCount = -1.0;
/**
Access paths are a query planning structure that correspond 1:1 to iterators,
in that an access path contains pretty much exactly the information
needed to instantiate given iterator, plus some information that is only
needed during planning, such as costs. (The new join optimizer will extend
this somewhat in the future. Some iterators also need the query block,
ie., JOIN object, they are part of, but that is implicitly available when
constructing the tree.)
AccessPath objects build on a variant, ie., they can hold an access path of
any type (table scan, filter, hash join, sort, etc.), although only one at the
same time. Currently, they contain 32 bytes of base information that is common
to any access path (type identifier, costs, etc.), and then up to 40 bytes
that is type-specific (e.g. for a table scan, the TABLE object). It would be
nice if we could squeeze it down to 64 and fit a cache line exactly, but it
does not seem to be easy without fairly large contortions.
We could have solved this by inheritance, but the fixed-size design makes it
possible to replace an access path when a better one is found, without
introducing a new allocation, which will be important when using them as a
planning structure.
*/
struct AccessPath {
// Most of the members are declared with initializers, but bit fields cannot
// be declared with initializers until C++20, so we need to define a
// constructor to initialize those fields.
// TODO(khatlen): When we move to C++20, add initializers to the bit fields
// too, and use the default constructor generated by the compiler.
AccessPath()
: count_examined_rows(false)
#ifndef NDEBUG
,
forced_by_dbug(false)
#endif
{
}
enum Type : uint8_t {
// Basic access paths (those with no children, at least nominally).
// NOTE: When adding more paths to this section, also update GetBasicTable()
// to handle them.
TABLE_SCAN,
INDEX_SCAN,
REF,
REF_OR_NULL,
EQ_REF,
PUSHED_JOIN_REF,
FULL_TEXT_SEARCH,
CONST_TABLE,
MRR,
FOLLOW_TAIL,
INDEX_RANGE_SCAN,
INDEX_MERGE,
ROWID_INTERSECTION,
ROWID_UNION,
INDEX_SKIP_SCAN,
GROUP_INDEX_SKIP_SCAN,
DYNAMIC_INDEX_RANGE_SCAN,
// Basic access paths that don't correspond to a specific table.
TABLE_VALUE_CONSTRUCTOR,
FAKE_SINGLE_ROW,
ZERO_ROWS,
ZERO_ROWS_AGGREGATED,
MATERIALIZED_TABLE_FUNCTION,
UNQUALIFIED_COUNT,
// Joins.
NESTED_LOOP_JOIN,
NESTED_LOOP_SEMIJOIN_WITH_DUPLICATE_REMOVAL,
BKA_JOIN,
HASH_JOIN,
// Composite access paths.
FILTER,
SORT,
AGGREGATE,
TEMPTABLE_AGGREGATE,
LIMIT_OFFSET,
STREAM,
MATERIALIZE,
MATERIALIZE_INFORMATION_SCHEMA_TABLE,
APPEND,
WINDOW,
WEEDOUT,
REMOVE_DUPLICATES,
REMOVE_DUPLICATES_ON_INDEX,
ALTERNATIVE,
CACHE_INVALIDATOR,
// Access paths that modify tables.
DELETE_ROWS,
UPDATE_ROWS,
} type;
/// A general enum to describe the safety of a given operation.
/// Currently we only use this to describe row IDs, but it can easily
/// be reused for safety of updating a table we're reading from
/// (the Halloween problem), or just generally unreproducible results
/// (e.g. a TABLESAMPLE changing due to external factors).
///
/// Less safe values have higher numerical values.
enum Safety : uint8_t {
/// The given operation is always safe on this access path.
SAFE = 0,
/// The given operation is safe if this access path is scanned once,
/// but not if it's scanned multiple times (e.g. used on the inner side
/// of a nested-loop join). A typical example of this is a derived table
/// or CTE that is rematerialized on each scan, so that references to
/// the old values (such as row IDs) are no longer valid.
SAFE_IF_SCANNED_ONCE = 1,
/// The given operation is unsafe on this access path, no matter how many
/// or few times it's scanned. Often, it may help to materialize it
/// (assuming the materialization itself doesn't use the operation
/// in question).
UNSAFE = 2
};
/// Whether it is safe to get row IDs (for sorting) from this access path.
Safety safe_for_rowid = SAFE;
/// Whether this access path counts as one that scans a base table,
/// and thus should be counted towards examined_rows. It can sometimes
/// seem a bit arbitrary which iterators count towards examined_rows
/// and which ones do not, so the only canonical reference is the tests.
bool count_examined_rows : 1;
#ifndef NDEBUG
/// Whether this access path is forced preferred over all others by means
/// of a SET DEBUG force_subplan_0x... statement.
bool forced_by_dbug : 1;
#endif
/// For UPDATE and DELETE statements: The node index of a table which can be
/// updated or deleted from immediately as the rows are read from the
/// iterator, if this path is only read from once. -1 if there is no such
/// table in this path.
///
/// Note that this is an index into CostingReceiver's array of nodes, and is
/// not necessarily equal to the table number within the query block given by
/// Table_ref::tableno().
///
/// The table, if any, is currently always the outermost table in the path.
///
/// It is possible to have plans where it would be safe to operate
/// "immediately" on more than one table. For example, if we do a merge join,
/// it is safe to perform immediate deletes on tables on the inner side of the
/// join, since both sides are read only once. (However, we currently do not
/// support merge joins.)
///
/// Another possibility is when the outer table of a nested loop join is
/// guaranteed to return at most one row (typically, a unique index lookup
/// aka. eq_ref). Then it's safe to delete immediately from both sides of the
/// nested loop join. But we don't to this yet.
///
/// Hash joins read both sides exactly once, However, with hash joins, the
/// scans on the inner tables are not positioned on the correct row when the
/// result of the join is returned, so the immediate delete logic will need to
/// be changed to reposition the underlying scans before doing the immediate
/// deletes. While this can be done, it makes the benefit of immediate deletes
/// less obvious for these tables, and it can also be a loss in some cases,
/// because we lose the deduplication provided by the Unique object used for
/// buffered deletes (the immediate deletes could end up spending time
/// repositioning to already deleted rows). So we currently don't attempt to
/// do immediate deletes from inner tables of hash joins either.
///
/// The outer table of a hash join can be deleted from immediately if the
/// inner table fits in memory. If the hash join spills to disk, though,
/// neither the rows of the outer table nor the rows of the inner table come
/// out in the order of the underlying scan, so it is not safe in general to
/// perform immediate deletes on the outer table of a hash join.
///
/// If support for immediate operations on multiple tables is added,
/// this member could be changed from a node index to a NodeMap.
int8_t immediate_update_delete_table{-1};
/// Which ordering the rows produced by this path follow, if any
/// (see interesting_orders.h). This is really a LogicalOrderings::StateIndex,
/// but we don't want to add a dependency on interesting_orders.h from
/// this file, so we use the base type instead of the typedef here.
int ordering_state = 0;
/// If an iterator has been instantiated for this access path, points to the
/// iterator. Used for constructing iterators that need to talk to each other
/// (e.g. for recursive CTEs, or BKA join), and also for locating timing
/// information in EXPLAIN ANALYZE queries.
RowIterator *iterator = nullptr;
/// Expected cost to read all of this access path once; -1.0 for unknown.
double cost{-1.0};
/// Expected cost to initialize this access path; ie., cost to read
/// k out of N rows would be init_cost + (k/N) * (cost - init_cost).
/// Note that EXPLAIN prints out cost of reading the _first_ row
/// because it is easier for the user and also easier to measure in
/// EXPLAIN ANALYZE, but it is easier to do calculations with a pure
/// initialization cost, so that is what we use in this member.
/// -1.0 for unknown.
double init_cost{-1.0};
/// Of init_cost, how much of the initialization needs only to be done
/// once per query block. (This is a cost, not a proportion.)
/// Ie., if the access path can reuse some its initialization work
/// if Init() is called multiple times, this member will be nonzero.
/// A typical example is a materialized table with rematerialize=false;
/// the second time Init() is called, it's a no-op. Most paths will have
/// init_once_cost = 0.0, ie., repeated scans will cost the same.
/// We do not intend to use this field to model cache effects.
///
/// This is currently not printed in EXPLAIN, only optimizer trace.
double init_once_cost{0.0};
/// Return the cost of scanning the given path for the second time
/// (or later) in the given query block. This is really the interesting
/// metric, not init_once_cost in itself, but since nearly all paths
/// have zero init_once_cost, storing that instead allows us to skip
/// a lot of repeated path->init_once_cost = path->init_cost calls
/// in the code.
double rescan_cost() const { return cost - init_once_cost; }
/// If no filter, identical to num_output_rows, cost, respectively.
/// init_cost is always the same (filters have zero initialization cost).
double num_output_rows_before_filter{kUnknownRowCount},
cost_before_filter{-1.0};
/// Bitmap of WHERE predicates that we are including on this access path,
/// referring to the “predicates” array internal to the join optimizer.
/// Since bit masks are much cheaper to deal with than creating Item
/// objects, and we don't invent new conditions during join optimization
/// (all of them are known when we begin optimization), we stick to
/// manipulating bit masks during optimization, saying which filters will be
/// applied at this node (a 1-bit means the filter will be applied here; if
/// there are multiple ones, they are ANDed together).
///
/// This is used during join optimization only; before iterators are
/// created, we will add FILTER access paths to represent these instead,
/// removing the dependency on the array. Said FILTER paths are by
/// convention created with materialize_subqueries = false, since the by far
/// most common case is that there are no subqueries in the predicate.
/// In other words, if you wish to represent a filter with
/// materialize_subqueries = true, you will need to make an explicit FILTER
/// node.
///
/// See also nested_loop_join().equijoin_predicates, which is for filters
/// being applied _before_ nested-loop joins, but is otherwise the same idea.
OverflowBitset filter_predicates{0};
/// Bitmap of sargable join predicates that have already been applied
/// in this access path by means of an index lookup (ref access),
/// again referring to “predicates”, and thus should not be counted again
/// for selectivity. Note that the filter may need to be applied
/// nevertheless (especially in case of type conversions); see
/// subsumed_sargable_join_predicates.
///
/// Since these refer to the same array as filter_predicates, they will
/// never overlap with filter_predicates, and so we can reuse the same
/// memory using an alias (a union would not be allowed, since OverflowBitset
/// is a class with non-trivial default constructor), even though the meaning
/// is entirely separate. If N = num_where_predicates in the hypergraph, then
/// bits 0..(N-1) belong to filter_predicates, and the rest to
/// applied_sargable_join_predicates.
OverflowBitset &applied_sargable_join_predicates() {
return filter_predicates;
}
const OverflowBitset &applied_sargable_join_predicates() const {
return filter_predicates;
}
/// Bitmap of WHERE predicates that touch tables we have joined in,
/// but that we could not apply yet (for instance because they reference
/// other tables, or because because we could not push them down into
/// the nullable side of outer joins). Used during planning only
/// (see filter_predicates).
OverflowBitset delayed_predicates{0};
/// Similar to applied_sargable_join_predicates, bitmap of sargable
/// join predicates that have been applied and will subsume the join
/// predicate entirely, ie., not only should the selectivity not be
/// double-counted, but the predicate itself is redundant and need not
/// be applied as a filter. (It is an error to have a bit set here but not
/// in applied_sargable_join_predicates.)
OverflowBitset &subsumed_sargable_join_predicates() {
return delayed_predicates;
}
const OverflowBitset &subsumed_sargable_join_predicates() const {
return delayed_predicates;
}
/// If nonzero, a bitmap of other tables whose joined-in rows must already be
/// loaded when rows from this access path are evaluated; that is, this
/// access path must be put on the inner side of a nested-loop join (or
/// multiple such joins) where the outer side includes all of the given
/// tables.
///
/// The most obvious case for this is dependent tables in LATERAL, but a more
/// common case is when we have pushed join conditions referring to those
/// tables; e.g., if this access path represents t1 and we have a condition
/// t1.x=t2.x that is pushed down into an index lookup (ref access), t2 will
/// be set in this bitmap. We can still join in other tables, deferring t2,
/// but the bit(s) will then propagate, and we cannot be on the right side of
/// a hash join until parameter_tables is zero again. (Also see
/// DisallowParameterizedJoinPath() for when we disallow such deferring,
/// as an optimization.)
///
/// As a special case, we allow setting RAND_TABLE_BIT, even though it
/// is normally part of a table_map, not a NodeMap. In this case, it specifies
/// that the access path is entirely noncachable, because it depends on
/// something nondeterministic or an outer reference, and thus can never be on
/// the right side of a hash join, ever.
hypergraph::NodeMap parameter_tables{0};
/// Auxiliary data used by a secondary storage engine while processing the
/// access path during optimization and execution. The secondary storage
/// engine is free to store any useful information in this member, for example
/// extra statistics or cost estimates. The data pointed to is fully owned by
/// the secondary storage engine, and it is the responsibility of the
/// secondary engine to manage the memory and make sure it is properly
/// destroyed.
void *secondary_engine_data{nullptr};
// Accessors for the union below.
auto &table_scan() {
assert(type == TABLE_SCAN);
return u.table_scan;
}
const auto &table_scan() const {
assert(type == TABLE_SCAN);
return u.table_scan;
}
auto &index_scan() {
assert(type == INDEX_SCAN);
return u.index_scan;
}
const auto &index_scan() const {
assert(type == INDEX_SCAN);
return u.index_scan;
}
auto &ref() {
assert(type == REF);
return u.ref;
}
const auto &ref() const {
assert(type == REF);
return u.ref;
}
auto &ref_or_null() {
assert(type == REF_OR_NULL);
return u.ref_or_null;
}
const auto &ref_or_null() const {
assert(type == REF_OR_NULL);
return u.ref_or_null;
}
auto &eq_ref() {
assert(type == EQ_REF);
return u.eq_ref;
}
const auto &eq_ref() const {
assert(type == EQ_REF);
return u.eq_ref;
}
auto &pushed_join_ref() {
assert(type == PUSHED_JOIN_REF);
return u.pushed_join_ref;
}
const auto &pushed_join_ref() const {
assert(type == PUSHED_JOIN_REF);
return u.pushed_join_ref;
}
auto &full_text_search() {
assert(type == FULL_TEXT_SEARCH);
return u.full_text_search;
}
const auto &full_text_search() const {
assert(type == FULL_TEXT_SEARCH);
return u.full_text_search;
}
auto &const_table() {
assert(type == CONST_TABLE);
return u.const_table;
}
const auto &const_table() const {
assert(type == CONST_TABLE);
return u.const_table;
}
auto &mrr() {
assert(type == MRR);
return u.mrr;
}
const auto &mrr() const {
assert(type == MRR);
return u.mrr;
}
auto &follow_tail() {
assert(type == FOLLOW_TAIL);
return u.follow_tail;
}
const auto &follow_tail() const {
assert(type == FOLLOW_TAIL);
return u.follow_tail;
}
auto &index_range_scan() {
assert(type == INDEX_RANGE_SCAN);
return u.index_range_scan;
}
const auto &index_range_scan() const {
assert(type == INDEX_RANGE_SCAN);
return u.index_range_scan;
}
auto &index_merge() {
assert(type == INDEX_MERGE);
return u.index_merge;
}
const auto &index_merge() const {
assert(type == INDEX_MERGE);
return u.index_merge;
}
auto &rowid_intersection() {
assert(type == ROWID_INTERSECTION);
return u.rowid_intersection;
}
const auto &rowid_intersection() const {
assert(type == ROWID_INTERSECTION);
return u.rowid_intersection;
}
auto &rowid_union() {
assert(type == ROWID_UNION);
return u.rowid_union;
}
const auto &rowid_union() const {
assert(type == ROWID_UNION);
return u.rowid_union;
}
auto &index_skip_scan() {
assert(type == INDEX_SKIP_SCAN);
return u.index_skip_scan;
}
const auto &index_skip_scan() const {
assert(type == INDEX_SKIP_SCAN);
return u.index_skip_scan;
}
auto &group_index_skip_scan() {
assert(type == GROUP_INDEX_SKIP_SCAN);
return u.group_index_skip_scan;
}
const auto &group_index_skip_scan() const {
assert(type == GROUP_INDEX_SKIP_SCAN);
return u.group_index_skip_scan;
}
auto &dynamic_index_range_scan() {
assert(type == DYNAMIC_INDEX_RANGE_SCAN);
return u.dynamic_index_range_scan;
}
const auto &dynamic_index_range_scan() const {
assert(type == DYNAMIC_INDEX_RANGE_SCAN);
return u.dynamic_index_range_scan;
}
auto &materialized_table_function() {
assert(type == MATERIALIZED_TABLE_FUNCTION);
return u.materialized_table_function;
}
const auto &materialized_table_function() const {
assert(type == MATERIALIZED_TABLE_FUNCTION);
return u.materialized_table_function;
}
auto &unqualified_count() {
assert(type == UNQUALIFIED_COUNT);
return u.unqualified_count;
}
const auto &unqualified_count() const {
assert(type == UNQUALIFIED_COUNT);
return u.unqualified_count;
}
auto &table_value_constructor() {
assert(type == TABLE_VALUE_CONSTRUCTOR);
return u.table_value_constructor;
}
const auto &table_value_constructor() const {
assert(type == TABLE_VALUE_CONSTRUCTOR);
return u.table_value_constructor;
}
auto &fake_single_row() {
assert(type == FAKE_SINGLE_ROW);
return u.fake_single_row;
}
const auto &fake_single_row() const {
assert(type == FAKE_SINGLE_ROW);
return u.fake_single_row;
}
auto &zero_rows() {
assert(type == ZERO_ROWS);
return u.zero_rows;
}
const auto &zero_rows() const {
assert(type == ZERO_ROWS);
return u.zero_rows;
}
auto &zero_rows_aggregated() {
assert(type == ZERO_ROWS_AGGREGATED);
return u.zero_rows_aggregated;
}
const auto &zero_rows_aggregated() const {
assert(type == ZERO_ROWS_AGGREGATED);
return u.zero_rows_aggregated;
}
auto &hash_join() {
assert(type == HASH_JOIN);
return u.hash_join;
}
const auto &hash_join() const {
assert(type == HASH_JOIN);
return u.hash_join;
}
auto &bka_join() {
assert(type == BKA_JOIN);
return u.bka_join;
}
const auto &bka_join() const {
assert(type == BKA_JOIN);
return u.bka_join;
}
auto &nested_loop_join() {
assert(type == NESTED_LOOP_JOIN);
return u.nested_loop_join;
}
const auto &nested_loop_join() const {
assert(type == NESTED_LOOP_JOIN);
return u.nested_loop_join;
}
auto &nested_loop_semijoin_with_duplicate_removal() {
assert(type == NESTED_LOOP_SEMIJOIN_WITH_DUPLICATE_REMOVAL);
return u.nested_loop_semijoin_with_duplicate_removal;
}
const auto &nested_loop_semijoin_with_duplicate_removal() const {
assert(type == NESTED_LOOP_SEMIJOIN_WITH_DUPLICATE_REMOVAL);
return u.nested_loop_semijoin_with_duplicate_removal;
}
auto &filter() {
assert(type == FILTER);
return u.filter;
}
const auto &filter() const {
assert(type == FILTER);
return u.filter;
}
auto &sort() {
assert(type == SORT);
return u.sort;
}
const auto &sort() const {
assert(type == SORT);
return u.sort;
}
auto &aggregate() {
assert(type == AGGREGATE);
return u.aggregate;
}
const auto &aggregate() const {
assert(type == AGGREGATE);
return u.aggregate;
}
auto &temptable_aggregate() {
assert(type == TEMPTABLE_AGGREGATE);
return u.temptable_aggregate;
}
const auto &temptable_aggregate() const {
assert(type == TEMPTABLE_AGGREGATE);
return u.temptable_aggregate;
}
auto &limit_offset() {
assert(type == LIMIT_OFFSET);
return u.limit_offset;
}
const auto &limit_offset() const {
assert(type == LIMIT_OFFSET);
return u.limit_offset;
}
auto &stream() {
assert(type == STREAM);
return u.stream;
}
const auto &stream() const {
assert(type == STREAM);
return u.stream;
}
auto &materialize() {
assert(type == MATERIALIZE);
return u.materialize;
}
const auto &materialize() const {
assert(type == MATERIALIZE);
return u.materialize;
}
auto &materialize_information_schema_table() {
assert(type == MATERIALIZE_INFORMATION_SCHEMA_TABLE);
return u.materialize_information_schema_table;
}
const auto &materialize_information_schema_table() const {
assert(type == MATERIALIZE_INFORMATION_SCHEMA_TABLE);
return u.materialize_information_schema_table;
}
auto &append() {
assert(type == APPEND);
return u.append;
}
const auto &append() const {
assert(type == APPEND);
return u.append;
}
auto &window() {
assert(type == WINDOW);
return u.window;
}
const auto &window() const {
assert(type == WINDOW);
return u.window;
}
auto &weedout() {
assert(type == WEEDOUT);
return u.weedout;
}
const auto &weedout() const {
assert(type == WEEDOUT);
return u.weedout;
}
auto &remove_duplicates() {
assert(type == REMOVE_DUPLICATES);
return u.remove_duplicates;
}
const auto &remove_duplicates() const {
assert(type == REMOVE_DUPLICATES);
return u.remove_duplicates;
}
auto &remove_duplicates_on_index() {
assert(type == REMOVE_DUPLICATES_ON_INDEX);
return u.remove_duplicates_on_index;
}
const auto &remove_duplicates_on_index() const {
assert(type == REMOVE_DUPLICATES_ON_INDEX);
return u.remove_duplicates_on_index;
}
auto &alternative() {
assert(type == ALTERNATIVE);
return u.alternative;
}
const auto &alternative() const {
assert(type == ALTERNATIVE);
return u.alternative;
}
auto &cache_invalidator() {
assert(type == CACHE_INVALIDATOR);
return u.cache_invalidator;
}
const auto &cache_invalidator() const {
assert(type == CACHE_INVALIDATOR);
return u.cache_invalidator;
}
auto &delete_rows() {
assert(type == DELETE_ROWS);
return u.delete_rows;
}
const auto &delete_rows() const {
assert(type == DELETE_ROWS);
return u.delete_rows;
}
auto &update_rows() {
assert(type == UPDATE_ROWS);
return u.update_rows;
}
const auto &update_rows() const {
assert(type == UPDATE_ROWS);
return u.update_rows;
}
double num_output_rows() const { return m_num_output_rows; }
void set_num_output_rows(double val) { m_num_output_rows = val; }
private:
/// Expected number of output rows, -1.0 for unknown.
double m_num_output_rows{kUnknownRowCount};
// We'd prefer if this could be an std::variant, but we don't have C++17 yet.
// It is private to force all access to be through the type-checking
// accessors.
//
// For information about the meaning of each value, see the corresponding
// row iterator constructors.
union {
struct {
TABLE *table;
} table_scan;
struct {
TABLE *table;
int idx;
bool use_order;
bool reverse;
} index_scan;
struct {
TABLE *table;
Index_lookup *ref;
bool use_order;
bool reverse;
} ref;
struct {
TABLE *table;
Index_lookup *ref;
bool use_order;
} ref_or_null;
struct {
TABLE *table;
Index_lookup *ref;
} eq_ref;
struct {
TABLE *table;
Index_lookup *ref;
bool use_order;
bool is_unique;
} pushed_join_ref;
struct {
TABLE *table;
Index_lookup *ref;
bool use_order;
bool use_limit;
Item_func_match *ft_func;
} full_text_search;
struct {
TABLE *table;
Index_lookup *ref;
} const_table;
struct {
TABLE *table;
Index_lookup *ref;
AccessPath *bka_path;
int mrr_flags;
bool keep_current_rowid;
} mrr;
struct {
TABLE *table;
} follow_tail;
struct {
// The key part(s) we are scanning on. Note that this may be an array.
// You can get the table we are working on by looking into
// used_key_parts[0].field->table (it is not stored directly, to avoid
// going over the AccessPath size limits).
KEY_PART *used_key_part;
// The actual ranges we are scanning over (originally derived from “key”).
// Not a Bounds_checked_array, to save 4 bytes on the length.
QUICK_RANGE **ranges;
unsigned num_ranges;
unsigned mrr_flags;
unsigned mrr_buf_size;
// Which index (in the TABLE) we are scanning over, and how many of its
// key parts we are using.
unsigned index;
unsigned num_used_key_parts;
// If true, the scan can return rows in rowid order.
bool can_be_used_for_ror : 1;
// If true, the scan _should_ return rows in rowid order.
// Should only be set if can_be_used_for_ror == true.
bool need_rows_in_rowid_order : 1;
// If true, this plan can be used for index merge scan.
bool can_be_used_for_imerge : 1;
// See row intersection for more details.
bool reuse_handler : 1;
// Whether we are scanning over a geometry key part.
bool geometry : 1;
// Whether we need a reverse scan. Only supported if geometry == false.
bool reverse : 1;
// For a reverse scan, if we are using extended key parts. It is needed,
// to set correct flags when retrieving records.
bool using_extended_key_parts : 1;
} index_range_scan;
struct {
TABLE *table;
bool forced_by_hint;
bool allow_clustered_primary_key_scan;
Mem_root_array<AccessPath *> *children;
} index_merge;
struct {
TABLE *table;
Mem_root_array<AccessPath *> *children;
// Clustered primary key scan, if any.
AccessPath *cpk_child;
bool forced_by_hint;
bool retrieve_full_rows;
bool need_rows_in_rowid_order;
// If true, the first child scan should reuse table->file instead of
// creating its own. This is true if the intersection is the topmost
// range scan, but _not_ if it's below a union. (The reasons for this
// are unknown.) It can also be negated by logic involving
// retrieve_full_rows and is_covering, again for unknown reasons.
//
// This is not only for performance; multi-table delete has a hidden
// dependency on this behavior when running against certain types of
// tables (e.g. MyISAM), as it assumes table->file is correctly positioned
// when deleting (and not all table types can transfer the position of one
// handler to another by using position()).
bool reuse_handler;
// true if no row retrieval phase is necessary.
bool is_covering;
} rowid_intersection;
struct {
TABLE *table;
Mem_root_array<AccessPath *> *children;
bool forced_by_hint;
} rowid_union;
struct {
TABLE *table;
unsigned index;
unsigned num_used_key_parts;
bool forced_by_hint;
// Large, and has nontrivial destructors, so split out into