forked from mysql/mysql-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathabstract_query_plan.cc
408 lines (363 loc) · 14.3 KB
/
abstract_query_plan.cc
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
/*
Copyright (c) 2010, 2021, 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 also distributed 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 included with MySQL.
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
*/
#include "sql/abstract_query_plan.h"
#include <stddef.h>
#include "my_alloc.h"
#include "my_base.h"
#include "my_dbug.h"
#include "my_inttypes.h"
#include "sql/handler.h"
#include "sql/item.h"
#include "sql/join_optimizer/access_path.h"
#include "sql/key.h"
#include "sql/range_optimizer/path_helpers.h"
#include "sql/range_optimizer/range_optimizer.h"
#include "sql/sql_const.h"
#include "sql/sql_executor.h" // QEP_TAB
#include "sql/sql_opt_exec_shared.h"
#include "sql/sql_optimizer.h" // JOIN
#include "sql/sql_select.h"
#include "sql/table.h"
#include "sql/thr_malloc.h"
namespace AQP {
Join_plan::Join_plan(const JOIN *join)
: m_qep_tabs(join->qep_tab),
m_access_count(join->tables),
m_table_accesses(nullptr) {
/*
This combination is assumed not to appear. If it does, code must
be written to handle it.
*/
assert(!m_qep_tabs[0].dynamic_range() || (m_qep_tabs[0].type() == JT_ALL) ||
(m_qep_tabs[0].range_scan() == nullptr));
// Discard trailing allocated, but unused, tables.
while (m_qep_tabs[m_access_count - 1].position() == nullptr) {
m_access_count--;
}
m_table_accesses = new (*THR_MALLOC) Table_access[m_access_count];
for (uint i = 0; i < m_access_count; i++) {
m_table_accesses[i].m_join_plan = this;
m_table_accesses[i].m_tab_no = i;
}
}
Join_plan::~Join_plan() {
destroy_array(m_table_accesses, m_access_count);
m_table_accesses = nullptr;
}
/** Get the QEP_TAB of the n'th table access operation.*/
const QEP_TAB *Join_plan::get_qep_tab(uint qep_tab_no) const {
assert(qep_tab_no < m_access_count);
return m_qep_tabs + qep_tab_no;
}
/**
Get the number of key values for this operation. It is an error
to call this method on an operation that is not an index lookup
operation.
*/
uint Table_access::get_no_of_key_fields() const {
assert(m_access_type == AT_PRIMARY_KEY || m_access_type == AT_UNIQUE_KEY ||
m_access_type == AT_MULTI_PRIMARY_KEY ||
m_access_type == AT_MULTI_UNIQUE_KEY ||
m_access_type == AT_ORDERED_INDEX_SCAN); // Used as 'range scan'
return get_qep_tab()->ref().key_parts;
}
/**
Get the field_no'th key values for this operation. It is an error
to call this method on an operation that is not an index lookup
operation.
*/
const Item *Table_access::get_key_field(uint field_no) const {
assert(field_no < get_no_of_key_fields());
return get_qep_tab()->ref().items[field_no];
}
/**
Get the field_no'th KEY_PART_INFO for this operation. It is an error
to call this method on an operation that is not an index lookup
operation.
*/
const KEY_PART_INFO *Table_access::get_key_part_info(uint field_no) const {
assert(field_no < get_no_of_key_fields());
const KEY *key = &get_qep_tab()->table()->key_info[get_qep_tab()->ref().key];
return &key->key_part[field_no];
}
/**
Get the table that this operation accesses.
*/
TABLE *Table_access::get_table() const { return get_qep_tab()->table(); }
/** Get the QEP_TAB object that corresponds to this operation.*/
const QEP_TAB *Table_access::get_qep_tab() const {
return m_join_plan->get_qep_tab(m_tab_no);
}
/** Get the Item_equal's set relevant for the specified 'Item_field' */
Item_equal *Table_access::get_item_equal(const Item_field *field_item) const {
assert(field_item->type() == Item::FIELD_ITEM);
COND_EQUAL *const cond_equal = get_qep_tab()->join()->cond_equal;
if (cond_equal != nullptr) {
return (field_item->item_equal != nullptr)
? field_item->item_equal
: field_item->find_item_equal(cond_equal);
}
return nullptr;
}
/**
Write an entry in the trace file about the contents of this object.
*/
void Table_access::dbug_print() const {
DBUG_PRINT("info", ("type:%d", get_qep_tab()->type()));
DBUG_PRINT("info", ("ref().key:%d", get_qep_tab()->ref().key));
DBUG_PRINT("info", ("ref().key_parts:%d", get_qep_tab()->ref().key_parts));
DBUG_PRINT("info", ("ref().key_length:%d", get_qep_tab()->ref().key_length));
DBUG_PRINT("info", ("order:%p", get_qep_tab()->join()->order.order));
DBUG_PRINT("info",
("skip_sort_order:%d", get_qep_tab()->join()->skip_sort_order));
DBUG_PRINT("info", ("simple_order:%d", get_qep_tab()->join()->simple_order));
DBUG_PRINT("info", ("group:%d", get_qep_tab()->join()->grouped));
DBUG_PRINT("info",
("group_list:%p", get_qep_tab()->join()->group_list.order));
DBUG_PRINT("info", ("simple_group:%d", get_qep_tab()->join()->simple_group));
DBUG_PRINT("info", ("group_optimized_away:%d",
get_qep_tab()->join()->group_optimized_away));
DBUG_PRINT("info", ("need_tmp_before_win:%d",
get_qep_tab()->join()->need_tmp_before_win));
DBUG_PRINT("info",
("select_distinct:%d", get_qep_tab()->join()->select_distinct));
DBUG_PRINT("info", ("dynamic_range:%d", (int)get_qep_tab()->dynamic_range()));
DBUG_PRINT("info", ("index:%d", get_qep_tab()->index()));
DBUG_PRINT("info", ("range_scan:%p", get_qep_tab()->range_scan()));
if (get_qep_tab()->range_scan()) {
DBUG_PRINT("info",
("range_scan->type():%d", get_qep_tab()->range_scan()->type));
}
}
/**
Compute the access type and index (if apliccable) of this operation .
*/
void Table_access::compute_type_and_index() const {
DBUG_ENTER("Table_access::compute_type_and_index");
const QEP_TAB *const qep_tab = get_qep_tab();
JOIN *const join = qep_tab->join();
/* Tables below 'const_tables' has been const'ified, or entirely
* optimized away due to 'impossible WHERE/ON'
*/
if (qep_tab < join->qep_tab + join->const_tables) {
DBUG_PRINT("info", ("Operation %d is const-optimized.", m_tab_no));
m_access_type = AT_FIXED;
DBUG_VOID_RETURN;
}
/*
Identify the type of access operation and the index to use (if any).
*/
switch (qep_tab->type()) {
case JT_EQ_REF:
m_index_no = qep_tab->ref().key;
if (m_index_no == static_cast<int>(qep_tab->table()->s->primary_key)) {
DBUG_PRINT("info", ("Operation %d is a primary key lookup.", m_tab_no));
m_access_type = AT_PRIMARY_KEY;
} else {
DBUG_PRINT("info",
("Operation %d is a unique index lookup.", m_tab_no));
m_access_type = AT_UNIQUE_KEY;
}
break;
case JT_REF: {
assert(qep_tab->ref().key >= 0);
assert((uint)qep_tab->ref().key < MAX_KEY);
m_index_no = qep_tab->ref().key;
/*
All parts of a key are specified for an unique index -> access is a key
lookup.
*/
const KEY *key_info = qep_tab->table()->s->key_info;
if (key_info[m_index_no].user_defined_key_parts ==
qep_tab->ref().key_parts &&
key_info[m_index_no].flags & HA_NOSAME) {
m_access_type =
(m_index_no == static_cast<int32>(qep_tab->table()->s->primary_key))
? AT_PRIMARY_KEY
: AT_UNIQUE_KEY;
DBUG_PRINT("info",
("Operation %d is an unique key referrence.", m_tab_no));
} else {
assert(qep_tab->ref().key_parts > 0);
assert(qep_tab->ref().key_parts <=
key_info[m_index_no].user_defined_key_parts);
m_access_type = AT_ORDERED_INDEX_SCAN;
DBUG_PRINT("info",
("Operation %d is an ordered index scan.", m_tab_no));
}
break;
}
case JT_INDEX_SCAN:
assert(qep_tab->index() < MAX_KEY);
m_index_no = qep_tab->index();
m_access_type = AT_ORDERED_INDEX_SCAN;
DBUG_PRINT("info", ("Operation %d is an ordered index scan.", m_tab_no));
break;
case JT_ALL:
case JT_RANGE:
case JT_INDEX_MERGE:
if (qep_tab->dynamic_range()) {
/*
It means that the decision on which access method to use
will be taken late (as rows from the preceding operation arrive).
This operation is therefor not pushable.
*/
DBUG_PRINT("info", ("Operation %d has 'dynamic range' -> not pushable",
m_tab_no));
m_access_type = AT_UNDECIDED;
m_index_no = -1;
} else {
if (qep_tab->range_scan() != nullptr) {
AccessPath *path = qep_tab->range_scan();
/** QUICK_SELECT results in execution of MRR (Multi Range Read).
* Depending on each range, it may require execution of
* either a PK-lookup or a range scan. To cover both of
* these we may need to prepare both a pushed lookup join
* and a pushed range scan. Currently we handle it as
* a range scan and convert e PK lookup to a (closed-) range
* whenever required.
**/
const KEY *key_info = qep_tab->table()->s->key_info;
DBUG_EXECUTE("info", dbug_dump(0, true, path););
// JT_INDEX_MERGE: We have a set of qualifying PKs as root of pushed
// joins
if (used_index(path) == MAX_KEY) {
m_index_no = qep_tab->table()->s->primary_key;
m_access_type =
AT_MULTI_PRIMARY_KEY; // Multiple PKs are produced by merge
}
// Else JT_RANGE: May be both exact PK and/or index scans when sorted
// index available
else if (used_index(path) == qep_tab->table()->s->primary_key) {
m_index_no = used_index(path);
if (key_info[m_index_no].algorithm == HA_KEY_ALG_HASH)
m_access_type = AT_MULTI_PRIMARY_KEY; // MRR w/ multiple PK's
else
m_access_type = AT_MULTI_MIXED; // MRR w/ both range and PKs
} else {
m_index_no = used_index(path);
if (key_info[m_index_no].algorithm == HA_KEY_ALG_HASH)
m_access_type =
AT_MULTI_UNIQUE_KEY; // MRR with multiple unique keys
else
m_access_type =
AT_MULTI_MIXED; // MRR w/ both range and unique keys
}
} else {
DBUG_PRINT("info", ("Operation %d is a table scan.", m_tab_no));
m_access_type = AT_TABLE_SCAN;
}
}
break;
case JT_REF_OR_NULL:
DBUG_PRINT("info",
("Operation %d is REF_OR_NULL. (REF + SCAN)", m_tab_no));
m_access_type = AT_UNDECIDED; // Is both a REF *and* a SCAN
break;
case JT_CONST:
case JT_SYSTEM:
default:
/*
Other join_types either cannot be pushed or the code analyze them is
not yet in place.
*/
DBUG_PRINT("info", ("Operation %d has join_type %d. -> Not pushable.",
m_tab_no, qep_tab->type()));
m_access_type = AT_OTHER;
m_index_no = -1;
m_other_access_reason = "This table access method can not be pushed.";
break;
}
DBUG_VOID_RETURN;
}
// Table_access::compute_type_and_index()
Table_access::Table_access()
: m_join_plan(nullptr),
m_tab_no(0),
m_access_type(AT_VOID),
m_other_access_reason(nullptr),
m_index_no(-1),
m_properties(0) {}
/**
Check if the results from this operation will joined with results
from the next operation using a join buffer (instead of plain nested loop).
@return True if using a join buffer.
*/
bool Table_access::uses_join_cache() const {
return get_qep_tab()->op_type == QEP_TAB::OT_BNL ||
get_qep_tab()->op_type == QEP_TAB::OT_BKA;
}
/**
Check if this table will be presorted to an intermediate record storage
before it is joined with its siblings.
*/
bool Table_access::filesort_before_join() const {
return (get_qep_tab()->filesort != nullptr);
}
Item *Table_access::get_condition() const { return get_qep_tab()->condition(); }
void Table_access::set_condition(Item *cond) {
const_cast<QEP_TAB *>(get_qep_tab())->set_condition(cond);
}
/**
* Returns the first/last table in the join-nest this table is a member of.
* As opposed to the nest info returned by the QEP_TAB interface, we
* enumerate the uppermost nest to range from [0..#tables-1] (not [-1,-1]).
*
* Similarly, the first_upper reference to this range is '0', instead of -1.
* Note, that first_upper of the uppermost nest is still negative.
*/
uint Table_access::get_first_inner() const {
const QEP_TAB *qep_tab = get_qep_tab();
if (qep_tab->first_inner() < 0) return 0;
return qep_tab->first_inner();
}
uint Table_access::get_last_inner() const {
const QEP_TAB *qep_tab = get_qep_tab();
if (qep_tab->last_inner() < 0) return m_join_plan->get_access_count() - 1;
return qep_tab->last_inner();
}
int Table_access::get_first_upper() const {
const QEP_TAB *qep_tab = get_qep_tab();
if (qep_tab->first_inner() > 0 && // Is an inner join_nest &&
qep_tab->first_upper() < 0) // upper indicate 'no-nest'
return 0; // Return 'first table'
return qep_tab->first_upper();
}
/**
* Returns the first/last table in a semi-join nest.
* Returns <0 if table is not part of a semi-join nest.
*/
int Table_access::get_first_sj_inner() const {
const QEP_TAB *qep_tab = get_qep_tab();
return qep_tab->first_sj_inner();
}
int Table_access::get_last_sj_inner() const {
const QEP_TAB *qep_tab = get_qep_tab();
return qep_tab->last_sj_inner();
}
bool Table_access::is_sj_firstmatch() const {
const QEP_TAB *qep_tab = get_qep_tab();
return (qep_tab->get_sj_strategy() == SJ_OPT_FIRST_MATCH);
}
bool Table_access::is_antijoin() const {
return get_table()->reginfo.not_exists_optimize;
}
} // namespace AQP
// namespace AQP