| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
#ifndef UNITTEST_GUNIT_OPTIMIZER_TEST_H |
| 25 |
#define UNITTEST_GUNIT_OPTIMIZER_TEST_H |
| 26 |
|
| 27 |
#include <assert.h> |
| 28 |
#include <gmock/gmock.h> |
| 29 |
#include <gtest/gtest.h> |
| 30 |
#include <string.h> |
| 31 |
#include <initializer_list> |
| 32 |
#include <memory> |
| 33 |
#include <string> |
| 34 |
#include <unordered_map> |
| 35 |
#include <vector> |
| 36 |
|
| 37 |
#include "lex_string.h" |
| 38 |
#include "mem_root_deque.h" |
| 39 |
#include "my_alloc.h" |
| 40 |
#include "my_sqlcommand.h" |
| 41 |
#include "my_table_map.h" |
| 42 |
#include "sql/field.h" |
| 43 |
#include "sql/handler.h" |
| 44 |
#include "sql/item.h" |
| 45 |
#include "sql/item_subselect.h" |
| 46 |
#include "sql/join_optimizer/bit_utils.h" |
| 47 |
#include "sql/nested_join.h" |
| 48 |
#include "sql/query_options.h" |
| 49 |
#include "sql/sql_class.h" |
| 50 |
#include "sql/sql_cmd.h" |
| 51 |
#include "sql/sql_const.h" |
| 52 |
#include "sql/sql_executor.h" |
| 53 |
#include "sql/sql_lex.h" |
| 54 |
#include "sql/sql_list.h" |
| 55 |
#include "sql/sql_opt_exec_shared.h" |
| 56 |
#include "sql/sql_optimizer.h" |
| 57 |
#include "sql/sql_select.h" |
| 58 |
#include "sql/table.h" |
| 59 |
#include "sql/visible_fields.h" |
| 60 |
#include "template_utils.h" |
| 61 |
#include "unittest/gunit/fake_table.h" |
| 62 |
#include "unittest/gunit/handler-t.h" |
| 63 |
#include "unittest/gunit/mock_field_long.h" |
| 64 |
#include "unittest/gunit/parsertest.h" |
| 65 |
#include "unittest/gunit/test_utils.h" |
| 66 |
|
| 67 |
inline Query_block *ParseAndResolve( |
| 68 |
const char *query, bool nullable, const Server_initializer &initializer, |
| 69 |
std::unordered_map<string, Fake_TABLE *> *fake_tables); |
| 70 |
inline void ResolveQueryBlock( |
| 71 |
THD *thd, Query_block *query_block, bool nullable, |
| 72 |
std::unordered_map<string, Fake_TABLE *> *fake_tables); |
| 73 |
inline void ResolveFieldToFakeTable( |
| 74 |
Item *item, const std::unordered_map<string, Fake_TABLE *> &fake_tables); |
| 75 |
inline void ResolveAllFieldsToFakeTable( |
| 76 |
const mem_root_deque<Table_ref *> &join_list, |
| 77 |
const std::unordered_map<string, Fake_TABLE *> &fake_tables); |
| 78 |
inline void SetJoinConditions(const mem_root_deque<Table_ref *> &join_list); |
| 79 |
|
| 80 |
inline void DestroyFakeTables( |
| 81 |
const std::unordered_map<string, Fake_TABLE *> &fake_tables) { |
| 82 |
for (const auto &[name, table] : fake_tables) { |
| 83 |
destroy(table); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
namespace optimizer_test { |
| 88 |
|
| 89 |
|
| 90 |
struct Table { |
| 91 |
std::string m_table_name; |
| 92 |
|
| 93 |
int m_plan_idx; |
| 94 |
|
| 95 |
|
| 96 |
table_map m_prefix_tables; |
| 97 |
|
| 98 |
Table(std::string table_name, int plan_idx, table_map prefix_tables) |
| 99 |
: m_table_name(table_name), |
| 100 |
m_plan_idx(plan_idx), |
| 101 |
m_prefix_tables(prefix_tables) {} |
| 102 |
}; |
| 103 |
|
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
class OptimizerTestBase : public ::testing::Test { |
| 108 |
public: |
| 109 |
void SetUp() override { |
| 110 |
m_initializer.SetUp(); |
| 111 |
m_thd = m_initializer.thd(); |
| 112 |
} |
| 113 |
void TearDown() override { |
| 114 |
ClearFakeTables(); |
| 115 |
m_initializer.TearDown(); |
| 116 |
} |
| 117 |
|
| 118 |
protected: |
| 119 |
Query_block *ParseAndResolve(const char *query, bool nullable) { |
| 120 |
return ::ParseAndResolve(query, nullable, m_initializer, &m_fake_tables); |
| 121 |
} |
| 122 |
void ClearFakeTables() { |
| 123 |
DestroyFakeTables(m_fake_tables); |
| 124 |
m_fake_tables.clear(); |
| 125 |
} |
| 126 |
handlerton *EnableSecondaryEngine(bool aggregation_is_unordered); |
| 127 |
void SetUpJoinTabs(Query_block *query_block, int num_tables, |
| 128 |
std::vector<optimizer_test::Table> tables); |
| 129 |
void SetUpQEPTabs(Query_block *query_block, int num_tables, |
| 130 |
std::vector<optimizer_test::Table> tables); |
| 131 |
|
| 132 |
Server_initializer m_initializer; |
| 133 |
THD *m_thd = nullptr; |
| 134 |
std::unordered_map<std::string, Fake_TABLE *> m_fake_tables; |
| 135 |
}; |
| 136 |
|
| 137 |
|
| 138 |
template <class T> |
| 139 |
class OptimizerTestWithParam : public OptimizerTestBase, |
| 140 |
public ::testing::WithParamInterface<T> {}; |
| 141 |
|
| 142 |
inline Query_block *ParseAndResolve( |
| 143 |
const char *query, bool nullable, const Server_initializer &initializer, |
| 144 |
std::unordered_map<string, Fake_TABLE *> *fake_tables) { |
| 145 |
Query_block *query_block = ::parse(&initializer, query, 0); |
| 146 |
ResolveQueryBlock(initializer.thd(), query_block, nullable, fake_tables); |
| 147 |
return query_block; |
| 148 |
} |
| 149 |
|
| 150 |
inline void ResolveQueryBlock( |
| 151 |
THD *thd, Query_block *query_block, bool nullable, |
| 152 |
std::unordered_map<string, Fake_TABLE *> *fake_tables) { |
| 153 |
Query_block *const saved_current_query_block = |
| 154 |
thd->lex->current_query_block(); |
| 155 |
thd->lex->set_current_query_block(query_block); |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
query_block->add_active_options(OPTION_NO_CONST_TABLES | |
| 160 |
OPTION_NO_SUBQUERY_DURING_OPTIMIZATION); |
| 161 |
|
| 162 |
|
| 163 |
int num_tables = 0; |
| 164 |
for (Table_ref *tl = query_block->get_table_list(); tl != nullptr; |
| 165 |
tl = tl->next_global) { |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
Fake_TABLE *&fake_table = (*fake_tables)[tl->alias]; |
| 170 |
if (fake_table == nullptr) { |
| 171 |
List<Field> fields; |
| 172 |
for (const char *field_name : {"x", "y", "z", "w"}) { |
| 173 |
fields.push_back(new (thd->mem_root) Mock_field_long( |
| 174 |
field_name, nullable, false)); |
| 175 |
} |
| 176 |
fake_table = new (thd->mem_root) Fake_TABLE(fields); |
| 177 |
} |
| 178 |
fake_table->alias = tl->alias; |
| 179 |
fake_table->pos_in_table_list = tl; |
| 180 |
fake_table->s->db = {tl->db, tl->db_length}; |
| 181 |
fake_table->s->table_name = {tl->table_name, tl->table_name_length}; |
| 182 |
tl->table = fake_table; |
| 183 |
tl->set_tableno(num_tables++); |
| 184 |
tl->set_updatable(); |
| 185 |
tl->grant.privilege = ~(Access_bitmask)0; |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
ResolveAllFieldsToFakeTable(query_block->m_table_nest, *fake_tables); |
| 190 |
|
| 191 |
|
| 192 |
if (query_block->where_cond() != nullptr) { |
| 193 |
WalkItem(query_block->where_cond(), enum_walk::POSTFIX, [&](Item *item) { |
| 194 |
if (item->type() == Item::SUBSELECT_ITEM && |
| 195 |
down_cast<Item_subselect *>(item)->substype() == |
| 196 |
Item_subselect::IN_SUBS) { |
| 197 |
Item_in_subselect *item_subselect = |
| 198 |
down_cast<Item_in_subselect *>(item); |
| 199 |
ResolveFieldToFakeTable(item_subselect->left_expr, *fake_tables); |
| 200 |
Query_block *child_query_block = |
| 201 |
item_subselect->unit->first_query_block(); |
| 202 |
ResolveAllFieldsToFakeTable(child_query_block->m_table_nest, |
| 203 |
*fake_tables); |
| 204 |
if (child_query_block->where_cond() != nullptr) { |
| 205 |
ResolveFieldToFakeTable(child_query_block->where_cond(), |
| 206 |
*fake_tables); |
| 207 |
} |
| 208 |
for (Item *field_item : child_query_block->fields) { |
| 209 |
ResolveFieldToFakeTable(field_item, *fake_tables); |
| 210 |
} |
| 211 |
return true; |
| 212 |
} else if (item->type() == Item::FIELD_ITEM) { |
| 213 |
ResolveFieldToFakeTable(item, *fake_tables); |
| 214 |
} |
| 215 |
return false; |
| 216 |
}); |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
for (Item *item : query_block->fields) { |
| 221 |
ResolveFieldToFakeTable(item, *fake_tables); |
| 222 |
} |
| 223 |
for (ORDER *cur_group = query_block->group_list.first; cur_group != nullptr; |
| 224 |
cur_group = cur_group->next) { |
| 225 |
ResolveFieldToFakeTable(*cur_group->item, *fake_tables); |
| 226 |
} |
| 227 |
for (ORDER *cur_group = query_block->order_list.first; cur_group != nullptr; |
| 228 |
cur_group = cur_group->next) { |
| 229 |
ResolveFieldToFakeTable(*cur_group->item, *fake_tables); |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
if (thd->lex->sql_command == SQLCOM_DELETE || |
| 234 |
thd->lex->sql_command == SQLCOM_UPDATE_MULTI) { |
| 235 |
assert(query_block->context.table_list == nullptr); |
| 236 |
assert(query_block->context.first_name_resolution_table == nullptr); |
| 237 |
query_block->context.table_list = |
| 238 |
query_block->context.first_name_resolution_table = |
| 239 |
query_block->get_table_list(); |
| 240 |
} |
| 241 |
|
| 242 |
query_block->prepare(thd, nullptr); |
| 243 |
|
| 244 |
|
| 245 |
switch (thd->lex->sql_command) { |
| 246 |
case SQLCOM_DELETE: |
| 247 |
case SQLCOM_DELETE_MULTI: |
| 248 |
for (Table_ref *tl = query_block->leaf_tables; tl != nullptr; |
| 249 |
tl = tl->next_leaf) { |
| 250 |
if (tl->updating) { |
| 251 |
tl->set_deleted(); |
| 252 |
} |
| 253 |
} |
| 254 |
break; |
| 255 |
case SQLCOM_UPDATE: |
| 256 |
case SQLCOM_UPDATE_MULTI: { |
| 257 |
table_map update_tables = 0; |
| 258 |
for (Item *item : query_block->visible_fields()) { |
| 259 |
update_tables |= item->used_tables(); |
| 260 |
} |
| 261 |
for (Table_ref *tl = query_block->leaf_tables; tl != nullptr; |
| 262 |
tl = tl->next_leaf) { |
| 263 |
if (Overlaps(tl->map(), update_tables)) { |
| 264 |
tl->set_updated(); |
| 265 |
} |
| 266 |
} |
| 267 |
} break; |
| 268 |
default: |
| 269 |
break; |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
query_block->join = new (thd->mem_root) JOIN(thd, query_block); |
| 274 |
query_block->join->where_cond = query_block->where_cond(); |
| 275 |
query_block->join->having_cond = query_block->having_cond(); |
| 276 |
query_block->join->fields = &query_block->fields; |
| 277 |
query_block->join->alloc_func_list(); |
| 278 |
SetJoinConditions(query_block->m_table_nest); |
| 279 |
count_field_types(query_block, &query_block->join->tmp_table_param, |
| 280 |
query_block->fields, false, |
| 281 |
false); |
| 282 |
|
| 283 |
if (query_block->select_limit != nullptr) { |
| 284 |
query_block->master_query_expression()->select_limit_cnt = |
| 285 |
query_block->select_limit->val_int(); |
| 286 |
} |
| 287 |
|
| 288 |
if (query_block->offset_limit != nullptr) { |
| 289 |
query_block->master_query_expression()->offset_limit_cnt = |
| 290 |
query_block->offset_limit->val_int(); |
| 291 |
} |
| 292 |
|
| 293 |
thd->lex->set_current_query_block(saved_current_query_block); |
| 294 |
} |
| 295 |
|
| 296 |
inline void ResolveFieldToFakeTable( |
| 297 |
Item *item_arg, |
| 298 |
const std::unordered_map<string, Fake_TABLE *> &fake_tables) { |
| 299 |
WalkItem(item_arg, enum_walk::POSTFIX, [&](Item *item) { |
| 300 |
if (item->type() == Item::FIELD_ITEM) { |
| 301 |
Item_field *item_field = down_cast<Item_field *>(item); |
| 302 |
Fake_TABLE *table = fake_tables.at(item_field->table_name); |
| 303 |
item_field->table_ref = table->pos_in_table_list; |
| 304 |
Field *field = nullptr; |
| 305 |
if (strcmp(item_field->field_name, "x") == 0) { |
| 306 |
field = table->field[0]; |
| 307 |
} else if (strcmp(item_field->field_name, "y") == 0) { |
| 308 |
field = table->field[1]; |
| 309 |
} else if (strcmp(item_field->field_name, "z") == 0) { |
| 310 |
field = table->field[2]; |
| 311 |
} else if (strcmp(item_field->field_name, "w") == 0) { |
| 312 |
field = table->field[3]; |
| 313 |
} else { |
| 314 |
assert(false); |
| 315 |
} |
| 316 |
item_field->field = field; |
| 317 |
item_field->set_nullable(field->is_nullable()); |
| 318 |
item_field->set_data_type(field->type()); |
| 319 |
item_field->collation.set(field->charset(), field->derivation(), |
| 320 |
field->repertoire()); |
| 321 |
} |
| 322 |
return false; |
| 323 |
}); |
| 324 |
} |
| 325 |
|
| 326 |
inline void ResolveAllFieldsToFakeTable( |
| 327 |
const mem_root_deque<Table_ref *> &join_list, |
| 328 |
const std::unordered_map<string, Fake_TABLE *> &fake_tables) { |
| 329 |
for (Table_ref *tl : join_list) { |
| 330 |
if (tl->join_cond() != nullptr) { |
| 331 |
ResolveFieldToFakeTable(tl->join_cond(), fake_tables); |
| 332 |
} |
| 333 |
if (tl->nested_join != nullptr) { |
| 334 |
ResolveAllFieldsToFakeTable(tl->nested_join->m_tables, fake_tables); |
| 335 |
} |
| 336 |
} |
| 337 |
} |
| 338 |
|
| 339 |
inline void SetJoinConditions(const mem_root_deque<Table_ref *> &join_list) { |
| 340 |
for (Table_ref *tl : join_list) { |
| 341 |
tl->set_join_cond_optim(tl->join_cond()); |
| 342 |
if (tl->nested_join != nullptr) { |
| 343 |
SetJoinConditions(tl->nested_join->m_tables); |
| 344 |
} |
| 345 |
} |
| 346 |
} |
| 347 |
|
| 348 |
inline handlerton *OptimizerTestBase::EnableSecondaryEngine( |
| 349 |
bool aggregation_is_unordered) { |
| 350 |
auto hton = new (m_thd->mem_root) Fake_handlerton; |
| 351 |
hton->flags = HTON_SUPPORTS_SECONDARY_ENGINE; |
| 352 |
if (aggregation_is_unordered) { |
| 353 |
hton->secondary_engine_flags = |
| 354 |
MakeSecondaryEngineFlags(SecondaryEngineFlag::SUPPORTS_HASH_JOIN, |
| 355 |
SecondaryEngineFlag::AGGREGATION_IS_UNORDERED); |
| 356 |
} else { |
| 357 |
hton->secondary_engine_flags = |
| 358 |
MakeSecondaryEngineFlags(SecondaryEngineFlag::SUPPORTS_HASH_JOIN); |
| 359 |
} |
| 360 |
hton->secondary_engine_modify_access_path_cost = nullptr; |
| 361 |
|
| 362 |
for (const auto &[name, table] : m_fake_tables) { |
| 363 |
table->file->ht = hton; |
| 364 |
static_cast<Fake_TABLE_SHARE *>(table->s)->set_secondary_engine(true); |
| 365 |
ON_CALL(table->mock_handler, table_type()) |
| 366 |
.WillByDefault(testing::Return("unit test")); |
| 367 |
} |
| 368 |
|
| 369 |
m_thd->lex->m_sql_cmd->use_secondary_storage_engine(hton); |
| 370 |
|
| 371 |
return hton; |
| 372 |
} |
| 373 |
|
| 374 |
inline void OptimizerTestBase::SetUpJoinTabs( |
| 375 |
Query_block *query_block, int num_tables, |
| 376 |
std::vector<optimizer_test::Table> tables) { |
| 377 |
|
| 378 |
|
| 379 |
JOIN *join = query_block->join; |
| 380 |
join->join_tab = m_thd->mem_root->ArrayAlloc<JOIN_TAB>(join->tables); |
| 381 |
for (int i = 0; i < num_tables; i++) { |
| 382 |
JOIN_TAB *join_tab = &join->join_tab[i]; |
| 383 |
join_tab->set_qs(new (m_thd->mem_root) QEP_shared); |
| 384 |
join_tab->set_idx(tables[i].m_plan_idx); |
| 385 |
join_tab->set_prefix_tables(tables[i].m_prefix_tables, |
| 386 |
TablesBetween(0, tables[i].m_plan_idx)); |
| 387 |
join_tab->set_table(m_fake_tables[tables[i].m_table_name]); |
| 388 |
join_tab->table_ref = |
| 389 |
m_fake_tables[tables[i].m_table_name]->pos_in_table_list; |
| 390 |
join_tab->set_join(join); |
| 391 |
join_tab->set_condition(join_tab->table_ref->join_cond_optim()); |
| 392 |
} |
| 393 |
} |
| 394 |
|
| 395 |
inline void OptimizerTestBase::SetUpQEPTabs( |
| 396 |
Query_block *query_block, int num_tables, |
| 397 |
std::vector<optimizer_test::Table> tables) { |
| 398 |
|
| 399 |
|
| 400 |
JOIN *join = query_block->join; |
| 401 |
join->qep_tab = m_thd->mem_root->ArrayAlloc<QEP_TAB>(join->tables); |
| 402 |
std::string tbl_name; |
| 403 |
for (int i = 0; i < num_tables; i++) { |
| 404 |
QEP_TAB *qep_tab = &join->qep_tab[i]; |
| 405 |
qep_tab->set_qs(new (m_thd->mem_root) QEP_shared); |
| 406 |
qep_tab->set_idx(tables[i].m_plan_idx); |
| 407 |
qep_tab->set_table(m_fake_tables[tables[i].m_table_name]); |
| 408 |
qep_tab->table_ref = |
| 409 |
m_fake_tables[tables[i].m_table_name]->pos_in_table_list; |
| 410 |
qep_tab->set_join(join); |
| 411 |
|
| 412 |
qep_tab->op_type = QEP_TAB::OT_BNL; |
| 413 |
qep_tab->set_type(JT_ALL); |
| 414 |
} |
| 415 |
} |
| 416 |
|
| 417 |
#endif |