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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486
|
/* 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 */
#include "sql/join_optimizer/access_path.h"
#include "my_base.h"
#include "sql/filesort.h"
#include "sql/item_cmpfunc.h"
#include "sql/item_func.h"
#include "sql/item_sum.h"
#include "sql/iterators/basic_row_iterators.h"
#include "sql/iterators/bka_iterator.h"
#include "sql/iterators/composite_iterators.h"
#include "sql/iterators/delete_rows_iterator.h"
#include "sql/iterators/hash_join_iterator.h"
#include "sql/iterators/ref_row_iterators.h"
#include "sql/iterators/sorting_iterator.h"
#include "sql/iterators/timing_iterator.h"
#include "sql/iterators/window_iterators.h"
#include "sql/join_optimizer/bit_utils.h"
#include "sql/join_optimizer/cost_model.h"
#include "sql/join_optimizer/estimate_selectivity.h"
#include "sql/join_optimizer/overflow_bitset.h"
#include "sql/join_optimizer/relational_expression.h"
#include "sql/join_optimizer/walk_access_paths.h"
#include "sql/mem_root_array.h"
#include "sql/range_optimizer/geometry_index_range_scan.h"
#include "sql/range_optimizer/group_index_skip_scan.h"
#include "sql/range_optimizer/group_index_skip_scan_plan.h"
#include "sql/range_optimizer/index_merge.h"
#include "sql/range_optimizer/index_range_scan.h"
#include "sql/range_optimizer/index_skip_scan.h"
#include "sql/range_optimizer/index_skip_scan_plan.h"
#include "sql/range_optimizer/range_optimizer.h"
#include "sql/range_optimizer/reverse_index_range_scan.h"
#include "sql/range_optimizer/rowid_ordered_retrieval.h"
#include "sql/sql_optimizer.h"
#include "sql/sql_update.h"
#include "sql/table.h"
#include <vector>
using pack_rows::TableCollection;
using std::vector;
AccessPath *NewSortAccessPath(THD *thd, AccessPath *child, Filesort *filesort,
ORDER *order, bool count_examined_rows) {
assert(child != nullptr);
assert(filesort != nullptr);
assert(order != nullptr);
AccessPath *path = new (thd->mem_root) AccessPath;
path->type = AccessPath::SORT;
path->count_examined_rows = count_examined_rows;
path->sort().child = child;
path->sort().filesort = filesort;
path->sort().order = order;
path->sort().remove_duplicates = filesort->m_remove_duplicates;
path->sort().unwrap_rollup = false;
path->sort().limit = filesort->limit;
path->sort().force_sort_rowids = !filesort->using_addon_fields();
if (filesort->using_addon_fields()) {
path->sort().tables_to_get_rowid_for = 0;
} else {
if (filesort->tables.size() == 1 &&
filesort->tables[0]->pos_in_table_list == nullptr) {
// This can happen if we sort a single temporary table
// which is not in the table list (e.g., one that was
// specifically created for us). Filesort has special-casing
// to always get the row ID in this case.
path->sort().tables_to_get_rowid_for = 0;
} else {
FindTablesToGetRowidFor(path);
}
}
return path;
}
AccessPath *NewDeleteRowsAccessPath(THD *thd, AccessPath *child,
table_map delete_tables,
table_map immediate_tables) {
assert(IsSubset(immediate_tables, delete_tables));
AccessPath *path = new (thd->mem_root) AccessPath;
path->type = AccessPath::DELETE_ROWS;
path->delete_rows().child = child;
path->delete_rows().tables_to_delete_from = delete_tables;
path->delete_rows().immediate_tables = immediate_tables;
return path;
}
AccessPath *NewUpdateRowsAccessPath(THD *thd, AccessPath *child,
table_map update_tables,
table_map immediate_tables) {
assert(IsSubset(immediate_tables, update_tables));
AccessPath *path = new (thd->mem_root) AccessPath;
path->type = AccessPath::UPDATE_ROWS;
path->update_rows().child = child;
path->update_rows().tables_to_update = update_tables;
path->update_rows().immediate_tables = immediate_tables;
return path;
}
static AccessPath *FindSingleAccessPathOfType(AccessPath *path,
AccessPath::Type type) {
AccessPath *found_path = nullptr;
auto func = [type, &found_path](AccessPath *subpath, const JOIN *) {
#ifdef NDEBUG
constexpr bool fast_exit = true;
#else
constexpr bool fast_exit = false;
#endif
if (subpath->type == type) {
assert(found_path == nullptr);
found_path = subpath;
// If not in debug mode, stop as soon as we find the first one.
if (fast_exit) {
return true;
}
}
return false;
};
// Our users generally want to stop at STREAM or MATERIALIZE nodes,
// since they are table-oriented and those nodes have their own tables.
WalkAccessPaths(path, /*join=*/nullptr,
WalkAccessPathPolicy::STOP_AT_MATERIALIZATION, func);
return found_path;
}
static RowIterator *FindSingleIteratorOfType(AccessPath *path,
AccessPath::Type type) {
AccessPath *found_path = FindSingleAccessPathOfType(path, type);
if (found_path == nullptr) {
return nullptr;
} else {
return found_path->iterator->real_iterator();
}
}
TABLE *GetBasicTable(const AccessPath *path) {
switch (path->type) {
// Basic access paths (those with no children, at least nominally).
case AccessPath::TABLE_SCAN:
return path->table_scan().table;
case AccessPath::INDEX_SCAN:
return path->index_scan().table;
case AccessPath::REF:
return path->ref().table;
case AccessPath::REF_OR_NULL:
return path->ref_or_null().table;
case AccessPath::EQ_REF:
return path->eq_ref().table;
case AccessPath::PUSHED_JOIN_REF:
return path->pushed_join_ref().table;
case AccessPath::FULL_TEXT_SEARCH:
return path->full_text_search().table;
case AccessPath::CONST_TABLE:
return path->const_table().table;
case AccessPath::MRR:
return path->mrr().table;
case AccessPath::FOLLOW_TAIL:
return path->follow_tail().table;
case AccessPath::INDEX_RANGE_SCAN:
return path->index_range_scan().used_key_part[0].field->table;
case AccessPath::INDEX_MERGE:
return path->index_merge().table;
case AccessPath::ROWID_INTERSECTION:
return path->rowid_intersection().table;
case AccessPath::ROWID_UNION:
return path->rowid_union().table;
case AccessPath::INDEX_SKIP_SCAN:
return path->index_skip_scan().table;
case AccessPath::GROUP_INDEX_SKIP_SCAN:
return path->group_index_skip_scan().table;
case AccessPath::DYNAMIC_INDEX_RANGE_SCAN:
return path->dynamic_index_range_scan().table;
// Basic access paths that don't correspond to a specific table.
case AccessPath::TABLE_VALUE_CONSTRUCTOR:
case AccessPath::FAKE_SINGLE_ROW:
case AccessPath::ZERO_ROWS:
case AccessPath::ZERO_ROWS_AGGREGATED:
case AccessPath::MATERIALIZED_TABLE_FUNCTION:
case AccessPath::UNQUALIFIED_COUNT:
// Note, some other AccessPaths may use its own temporary (derived) table.
// We intentionally do not return such TABLEs.
default:
return nullptr;
}
}
table_map GetUsedTableMap(const AccessPath *path, bool include_pruned_tables) {
table_map tmap = 0;
WalkTablesUnderAccessPath(
const_cast<AccessPath *>(path),
[&tmap](TABLE *table) {
if (table->pos_in_table_list == nullptr) {
// Materialization within a JOIN (e.g., for sorting). The table won't
// have a map, so the caller will need to find the table manually.
tmap |= RAND_TABLE_BIT;
} else {
tmap |= table->pos_in_table_list->map();
}
return false;
},
include_pruned_tables);
return tmap;
}
static Prealloced_array<TABLE *, 4> GetUsedTables(AccessPath *child,
bool include_pruned_tables) {
Prealloced_array<TABLE *, 4> tables{PSI_NOT_INSTRUMENTED};
WalkTablesUnderAccessPath(
child,
[&tables](TABLE *table) {
tables.push_back(table);
return false;
},
include_pruned_tables);
return tables;
}
Mem_root_array<TABLE *> CollectTables(THD *thd, AccessPath *root_path) {
Mem_root_array<TABLE *> tables(thd->mem_root);
WalkTablesUnderAccessPath(
root_path, [&tables](TABLE *table) { return tables.push_back(table); },
/*include_pruned_tables=*/true);
return tables;
}
/**
Get the tables that are accessed by EQ_REF and can be on the inner side of an
outer join. These need some extra care in AggregateIterator when handling
NULL-complemented rows, so that the cache in EQRefIterator is not disturbed by
AggregateIterator's switching between groups.
*/
static table_map GetNullableEqRefTables(const AccessPath *root_path) {
table_map tables = 0;
WalkAccessPaths(
root_path, /*join=*/nullptr,
WalkAccessPathPolicy::STOP_AT_MATERIALIZATION,
[&tables](const AccessPath *path, const JOIN *) {
if (path->type == AccessPath::EQ_REF) {
const auto ¶m = path->eq_ref();
if (param.table->is_nullable() && !param.ref->disable_cache) {
tables |= param.table->pos_in_table_list->map();
}
}
return false;
});
return tables;
}
// Mirrors QEP_TAB::pfs_batch_update(), with one addition:
// If there is more than one table, batch mode will be handled by the join
// iterators on the probe side, so joins will return false.
bool ShouldEnableBatchMode(AccessPath *path) {
switch (path->type) {
case AccessPath::TABLE_SCAN:
case AccessPath::INDEX_SCAN:
case AccessPath::REF:
case AccessPath::REF_OR_NULL:
case AccessPath::PUSHED_JOIN_REF:
case AccessPath::FULL_TEXT_SEARCH:
case AccessPath::DYNAMIC_INDEX_RANGE_SCAN:
return true;
case AccessPath::FILTER:
if (path->filter().condition->has_subquery()) {
return false;
} else {
return ShouldEnableBatchMode(path->filter().child);
}
case AccessPath::SORT:
return ShouldEnableBatchMode(path->sort().child);
case AccessPath::EQ_REF:
case AccessPath::CONST_TABLE:
// These can read only one row per scan, so batch mode will never be a
// win (fall through).
default:
// All others, in particular joins.
return false;
}
}
bool FinalizeMaterializedSubqueries(THD *thd, JOIN *join, AccessPath *path) {
if (path->type != AccessPath::FILTER ||
!path->filter().materialize_subqueries) {
return false;
}
return WalkItem(
path->filter().condition, enum_walk::POSTFIX, [thd, join](Item *item) {
if (!IsItemInSubSelect(item)) {
return false;
}
Item_in_subselect *item_subs = down_cast<Item_in_subselect *>(item);
Query_block *subquery_block = item_subs->unit->first_query_block();
if (!item_subs->subquery_allows_materialization(thd, subquery_block,
join->query_block)) {
return false;
}
if (item_subs->finalize_materialization_transform(
thd, subquery_block->join)) {
return true;
}
item_subs->create_iterators(thd);
return false;
});
}
namespace {
struct IteratorToBeCreated {
AccessPath *path;
JOIN *join;
bool eligible_for_batch_mode;
unique_ptr_destroy_only<RowIterator> *destination;
Bounds_checked_array<unique_ptr_destroy_only<RowIterator>> children;
void AllocChildren(MEM_ROOT *mem_root, int num_children) {
children =
Bounds_checked_array<unique_ptr_destroy_only<RowIterator>>::Alloc(
mem_root, num_children);
}
};
void SetupJobsForChildren(MEM_ROOT *mem_root, AccessPath *child, JOIN *join,
bool eligible_for_batch_mode,
IteratorToBeCreated *job,
Mem_root_array<IteratorToBeCreated> *todo) {
// Make jobs for the child, and we'll return to this job later.
job->AllocChildren(mem_root, 1);
todo->push_back(*job);
todo->push_back(
{child, join, eligible_for_batch_mode, &job->children[0], {}});
}
void SetupJobsForChildren(MEM_ROOT *mem_root, AccessPath *outer,
AccessPath *inner, JOIN *join,
bool inner_eligible_for_batch_mode,
IteratorToBeCreated *job,
Mem_root_array<IteratorToBeCreated> *todo) {
// Make jobs for the children, and we'll return to this job later.
// Note that we push the inner before the outer job, so that we get
// left created before right (invalidators in materialization access paths,
// used in the old join optimizer, depend on this).
job->AllocChildren(mem_root, 2);
todo->push_back(*job);
todo->push_back(
{inner, join, inner_eligible_for_batch_mode, &job->children[1], {}});
todo->push_back({outer, join, false, &job->children[0], {}});
}
} // namespace
unique_ptr_destroy_only<RowIterator> CreateIteratorFromAccessPath(
THD *thd, MEM_ROOT *mem_root, AccessPath *top_path, JOIN *top_join,
bool top_eligible_for_batch_mode) {
assert(IteratorsAreNeeded(thd, top_path));
unique_ptr_destroy_only<RowIterator> ret;
Mem_root_array<IteratorToBeCreated> todo(mem_root);
todo.push_back({top_path, top_join, top_eligible_for_batch_mode, &ret, {}});
// The access path trees can be pretty deep, and the stack frames can be big
// on certain compilers/setups, so instead of explicit recursion, we push jobs
// onto a MEM_ROOT-backed stack. This uses a little more RAM (the MEM_ROOT
// typically lives to the end of the query), but reduces the stack usage
// greatly.
//
// The general rule is that if an iterator requires any children, it will push
// jobs for their access paths at the end of the stack and then re-push
// itself. When the children are instantiated and we get back to the original
// iterator, we'll actually instantiate it. (We distinguish between the two
// cases on basis of whether job.children has been allocated or not; the child
// iterator's destination will point into this array. The child list needs
// to be allocated in a way that doesn't move around if the TODO job list
// is reallocated, which we do by means of allocating it directly on the
// MEM_ROOT.)
while (!todo.empty()) {
IteratorToBeCreated job = todo.back();
todo.pop_back();
AccessPath *path = job.path;
JOIN *join = job.join;
bool eligible_for_batch_mode = job.eligible_for_batch_mode;
if (job.join != nullptr) {
assert(!job.join->needs_finalize);
}
unique_ptr_destroy_only<RowIterator> iterator;
ha_rows *examined_rows = nullptr;
if (path->count_examined_rows && join != nullptr) {
examined_rows = &join->examined_rows;
}
switch (path->type) {
case AccessPath::TABLE_SCAN: {
const auto ¶m = path->table_scan();
iterator = NewIterator<TableScanIterator>(
thd, mem_root, param.table, path->num_output_rows(), examined_rows);
break;
}
case AccessPath::INDEX_SCAN: {
const auto ¶m = path->index_scan();
if (param.reverse) {
iterator = NewIterator<IndexScanIterator<true>>(
thd, mem_root, param.table, param.idx, param.use_order,
path->num_output_rows(), examined_rows);
} else {
iterator = NewIterator<IndexScanIterator<false>>(
thd, mem_root, param.table, param.idx, param.use_order,
path->num_output_rows(), examined_rows);
}
break;
}
case AccessPath::REF: {
const auto ¶m = path->ref();
if (param.reverse) {
iterator = NewIterator<RefIterator<true>>(
thd, mem_root, param.table, param.ref, param.use_order,
path->num_output_rows(), examined_rows);
} else {
iterator = NewIterator<RefIterator<false>>(
thd, mem_root, param.table, param.ref, param.use_order,
path->num_output_rows(), examined_rows);
}
break;
}
case AccessPath::REF_OR_NULL: {
const auto ¶m = path->ref_or_null();
iterator = NewIterator<RefOrNullIterator>(
thd, mem_root, param.table, param.ref, param.use_order,
path->num_output_rows(), examined_rows);
break;
}
case AccessPath::EQ_REF: {
const auto ¶m = path->eq_ref();
iterator = NewIterator<EQRefIterator>(thd, mem_root, param.table,
param.ref, examined_rows);
break;
}
case AccessPath::PUSHED_JOIN_REF: {
const auto ¶m = path->pushed_join_ref();
iterator = NewIterator<PushedJoinRefIterator>(
thd, mem_root, param.table, param.ref, param.use_order,
param.is_unique, examined_rows);
break;
}
case AccessPath::FULL_TEXT_SEARCH: {
const auto ¶m = path->full_text_search();
iterator = NewIterator<FullTextSearchIterator>(
thd, mem_root, param.table, param.ref, param.ft_func,
param.use_order, param.use_limit, examined_rows);
break;
}
case AccessPath::CONST_TABLE: {
const auto ¶m = path->const_table();
iterator = NewIterator<ConstIterator>(thd, mem_root, param.table,
param.ref, examined_rows);
break;
}
case AccessPath::MRR: {
const auto ¶m = path->mrr();
const auto &bka_param = param.bka_path->bka_join();
iterator = NewIterator<MultiRangeRowIterator>(
thd, mem_root, param.table, param.ref, param.mrr_flags,
bka_param.join_type,
GetUsedTables(bka_param.outer, /*include_pruned_tables=*/true),
bka_param.store_rowids, bka_param.tables_to_get_rowid_for);
break;
}
case AccessPath::FOLLOW_TAIL: {
const auto ¶m = path->follow_tail();
iterator = NewIterator<FollowTailIterator>(
thd, mem_root, param.table, path->num_output_rows(), examined_rows);
break;
}
case AccessPath::INDEX_RANGE_SCAN: {
const auto ¶m = path->index_range_scan();
TABLE *table = param.used_key_part[0].field->table;
if (param.geometry) {
iterator = NewIterator<GeometryIndexRangeScanIterator>(
thd, mem_root, table, examined_rows, path->num_output_rows(),
param.index, param.need_rows_in_rowid_order, param.reuse_handler,
mem_root, param.mrr_flags, param.mrr_buf_size,
Bounds_checked_array{param.ranges, param.num_ranges});
} else if (param.reverse) {
iterator = NewIterator<ReverseIndexRangeScanIterator>(
thd, mem_root, table, examined_rows, path->num_output_rows(),
param.index, mem_root, param.mrr_flags,
Bounds_checked_array{param.ranges, param.num_ranges},
param.using_extended_key_parts);
} else {
iterator = NewIterator<IndexRangeScanIterator>(
thd, mem_root, table, examined_rows, path->num_output_rows(),
param.index, param.need_rows_in_rowid_order, param.reuse_handler,
mem_root, param.mrr_flags, param.mrr_buf_size,
Bounds_checked_array{param.ranges, param.num_ranges});
}
break;
}
case AccessPath::INDEX_MERGE: {
const auto ¶m = path->index_merge();
unique_ptr_destroy_only<RowIterator> pk_quick_select;
if (job.children.is_null()) {
job.AllocChildren(mem_root, param.children->size());
todo.push_back(job);
for (size_t child_idx = 0; child_idx < param.children->size();
++child_idx) {
todo.push_back({(*param.children)[child_idx],
join,
/*eligible_for_batch_mode=*/false,
&job.children[child_idx],
{}});
}
continue;
}
Mem_root_array<unique_ptr_destroy_only<RowIterator>> children(mem_root);
children.reserve(param.children->size());
for (size_t child_idx = 0; child_idx < param.children->size();
++child_idx) {
AccessPath *range_scan = (*param.children)[child_idx];
if (param.allow_clustered_primary_key_scan &&
param.table->file->primary_key_is_clustered() &&
range_scan->index_range_scan().index ==
param.table->s->primary_key) {
assert(pk_quick_select == nullptr);
pk_quick_select = std::move(job.children[child_idx]);
} else {
children.push_back(std::move(job.children[child_idx]));
}
}
iterator = NewIterator<IndexMergeIterator>(
thd, mem_root, mem_root, param.table, std::move(pk_quick_select),
std::move(children));
break;
}
case AccessPath::ROWID_INTERSECTION: {
const auto ¶m = path->rowid_intersection();
if (job.children.is_null()) {
job.AllocChildren(mem_root, param.children->size() +
(param.cpk_child != nullptr ? 1 : 0));
todo.push_back(job);
for (size_t child_idx = 0; child_idx < param.children->size();
++child_idx) {
todo.push_back({(*param.children)[child_idx],
join,
/*eligible_for_batch_mode=*/false,
&job.children[child_idx],
{}});
}
if (param.cpk_child != nullptr) {
todo.push_back({param.cpk_child,
join,
/*eligible_for_batch_mode=*/false,
&job.children[param.children->size()],
{}});
}
continue;
}
// TODO(sgunders): Consider just sending in the array here,
// changing types in the constructor.
Mem_root_array<unique_ptr_destroy_only<RowIterator>> children(mem_root);
children.reserve(param.children->size());
for (size_t child_idx = 0; child_idx < param.children->size();
++child_idx) {
children.push_back(std::move(job.children[child_idx]));
}
unique_ptr_destroy_only<RowIterator> cpk_child;
if (param.cpk_child != nullptr) {
cpk_child = std::move(job.children[param.children->size()]);
}
iterator = NewIterator<RowIDIntersectionIterator>(
thd, mem_root, mem_root, param.table, param.retrieve_full_rows,
param.need_rows_in_rowid_order, std::move(children),
std::move(cpk_child));
break;
}
case AccessPath::ROWID_UNION: {
const auto ¶m = path->rowid_union();
if (job.children.is_null()) {
job.AllocChildren(mem_root, param.children->size());
todo.push_back(job);
for (size_t child_idx = 0; child_idx < param.children->size();
++child_idx) {
todo.push_back({(*param.children)[child_idx],
join,
/*eligible_for_batch_mode=*/false,
&job.children[child_idx],
{}});
}
continue;
}
// TODO(sgunders): Consider just sending in the array here,
// changing types in the constructor.
Mem_root_array<unique_ptr_destroy_only<RowIterator>> children(mem_root);
children.reserve(param.children->size());
for (unique_ptr_destroy_only<RowIterator> &child : job.children) {
children.push_back(std::move(child));
}
iterator = NewIterator<RowIDUnionIterator>(
thd, mem_root, mem_root, param.table, std::move(children));
break;
}
case AccessPath::INDEX_SKIP_SCAN: {
const IndexSkipScanParameters *param = path->index_skip_scan().param;
iterator = NewIterator<IndexSkipScanIterator>(
thd, mem_root, path->index_skip_scan().table, param->index_info,
path->index_skip_scan().index, param->eq_prefix_len,
param->eq_prefix_key_parts, param->eq_prefixes,
path->index_skip_scan().num_used_key_parts, mem_root,
param->has_aggregate_function, param->min_range_key,
param->max_range_key, param->min_search_key, param->max_search_key,
param->range_cond_flag, param->range_key_len);
break;
}
case AccessPath::GROUP_INDEX_SKIP_SCAN: {
const GroupIndexSkipScanParameters *param =
path->group_index_skip_scan().param;
iterator = NewIterator<GroupIndexSkipScanIterator>(
thd, mem_root, path->group_index_skip_scan().table,
¶m->min_functions, ¶m->max_functions,
param->have_agg_distinct, param->min_max_arg_part,
param->group_prefix_len, param->group_key_parts,
param->real_key_parts, param->max_used_key_length,
param->index_info, path->group_index_skip_scan().index,
param->key_infix_len, mem_root, param->is_index_scan,
¶m->prefix_ranges, ¶m->key_infix_ranges,
¶m->min_max_ranges);
break;
}
case AccessPath::DYNAMIC_INDEX_RANGE_SCAN: {
const auto ¶m = path->dynamic_index_range_scan();
iterator = NewIterator<DynamicRangeIterator>(
thd, mem_root, param.table, param.qep_tab, examined_rows);
break;
}
case AccessPath::TABLE_VALUE_CONSTRUCTOR: {
assert(join != nullptr);
Query_block *query_block = join->query_block;
iterator = NewIterator<TableValueConstructorIterator>(
thd, mem_root, examined_rows, *query_block->row_value_list,
query_block->join->fields);
break;
}
case AccessPath::FAKE_SINGLE_ROW:
iterator =
NewIterator<FakeSingleRowIterator>(thd, mem_root, examined_rows);
break;
case AccessPath::ZERO_ROWS: {
iterator = NewIterator<ZeroRowsIterator>(thd, mem_root,
CollectTables(thd, path));
break;
}
case AccessPath::ZERO_ROWS_AGGREGATED:
iterator = NewIterator<ZeroRowsAggregatedIterator>(thd, mem_root, join,
examined_rows);
break;
case AccessPath::MATERIALIZED_TABLE_FUNCTION: {
const auto ¶m = path->materialized_table_function();
if (job.children.is_null()) {
SetupJobsForChildren(mem_root, param.table_path, join,
eligible_for_batch_mode, &job, &todo);
continue;
}
iterator = NewIterator<MaterializedTableFunctionIterator>(
thd, mem_root, param.table_function, param.table,
std::move(job.children[0]));
break;
}
case AccessPath::UNQUALIFIED_COUNT:
iterator = NewIterator<UnqualifiedCountIterator>(thd, mem_root, join);
break;
case AccessPath::NESTED_LOOP_JOIN: {
const auto ¶m = path->nested_loop_join();
if (job.children.is_null()) {
SetupJobsForChildren(mem_root, param.outer, param.inner, join,
eligible_for_batch_mode, &job, &todo);
continue;
}
iterator = NewIterator<NestedLoopIterator>(
thd, mem_root, std::move(job.children[0]),
std::move(job.children[1]), param.join_type, param.pfs_batch_mode);
break;
}
case AccessPath::NESTED_LOOP_SEMIJOIN_WITH_DUPLICATE_REMOVAL: {
const auto ¶m = path->nested_loop_semijoin_with_duplicate_removal();
if (job.children.is_null()) {
SetupJobsForChildren(mem_root, param.outer, param.inner, join,
eligible_for_batch_mode, &job, &todo);
continue;
}
iterator = NewIterator<NestedLoopSemiJoinWithDuplicateRemovalIterator>(
thd, mem_root, std::move(job.children[0]),
std::move(job.children[1]), param.table, param.key, param.key_len);
break;
}
case AccessPath::BKA_JOIN: {
const auto ¶m = path->bka_join();
AccessPath *mrr_path =
FindSingleAccessPathOfType(param.inner, AccessPath::MRR);
if (job.children.is_null()) {
mrr_path->mrr().bka_path = path;
SetupJobsForChildren(mem_root, param.outer, param.inner, join,
/*inner_eligible_for_batch_mode=*/false, &job,
&todo);
continue;
}
MultiRangeRowIterator *mrr_iterator =
down_cast<MultiRangeRowIterator *>(
mrr_path->iterator->real_iterator());
iterator = NewIterator<BKAIterator>(
thd, mem_root, std::move(job.children[0]),
GetUsedTables(param.outer, /*include_pruned_tables=*/true),
std::move(job.children[1]), thd->variables.join_buff_size,
param.mrr_length_per_rec, param.rec_per_key, param.store_rowids,
param.tables_to_get_rowid_for, mrr_iterator, param.join_type);
break;
}
case AccessPath::HASH_JOIN: {
const auto ¶m = path->hash_join();
if (job.children.is_null()) {
SetupJobsForChildren(mem_root, param.outer, param.inner, join,
/*inner_eligible_for_batch_mode=*/true, &job,
&todo);
continue;
}
const JoinPredicate *join_predicate = param.join_predicate;
vector<HashJoinCondition> conditions;
for (Item_eq_base *cond : join_predicate->expr->equijoin_conditions) {
conditions.emplace_back(cond, thd->mem_root);
}
const bool probe_input_batch_mode =
eligible_for_batch_mode && ShouldEnableBatchMode(param.outer);
double estimated_build_rows = param.inner->num_output_rows();
if (param.inner->num_output_rows() < 0.0) {
// Not all access paths may propagate their costs properly.
// Choose a fairly safe estimate (it's better to be too large
// than too small).
estimated_build_rows = 1048576.0;
}
JoinType join_type{JoinType::INNER};
switch (join_predicate->expr->type) {
case RelationalExpression::INNER_JOIN:
case RelationalExpression::STRAIGHT_INNER_JOIN:
join_type = JoinType::INNER;
break;
case RelationalExpression::LEFT_JOIN:
join_type = JoinType::OUTER;
break;
case RelationalExpression::ANTIJOIN:
join_type = JoinType::ANTI;
break;
case RelationalExpression::SEMIJOIN:
join_type =
param.rewrite_semi_to_inner ? JoinType::INNER : JoinType::SEMI;
break;
case RelationalExpression::TABLE:
default:
assert(false);
}
// See if we can allow the hash table to keep its contents across Init()
// calls.
//
// The old optimizer will sometimes push join conditions referring
// to outer tables (in the same query block) down in under the hash
// operation, so without analysis of each filter and join condition, we
// cannot say for sure, and thus have to turn it off. But the hypergraph
// optimizer sets parameter_tables properly, so we're safe if we just
// check that.
//
// Regardless of optimizer, we can push outer references down in under
// the hash, but join->hash_table_generation will increase whenever we
// need to recompute the query block (in JOIN::clear_hash_tables()).
//
// TODO(sgunders): The old optimizer had a concept of _when_ to clear
// derived tables (invalidators), and this is somehow similar. If it
// becomes a performance issue, consider reintroducing them.
//
// TODO(sgunders): Should this perhaps be set as a flag on the access
// path instead of being computed here? We do make the same checks in
// the cost model, so perhaps it should set the flag as well.
uint64_t *hash_table_generation =
(thd->lex->using_hypergraph_optimizer() &&
path->parameter_tables == 0)
? &join->hash_table_generation
: nullptr;
iterator = NewIterator<HashJoinIterator>(
thd, mem_root, std::move(job.children[1]),
GetUsedTables(param.inner, /*include_pruned_tables=*/true),
estimated_build_rows, std::move(job.children[0]),
GetUsedTables(param.outer, /*include_pruned_tables=*/true),
param.store_rowids, param.tables_to_get_rowid_for,
thd->variables.join_buff_size, std::move(conditions),
param.allow_spill_to_disk, join_type,
join_predicate->expr->join_conditions, probe_input_batch_mode,
hash_table_generation);
break;
}
case AccessPath::FILTER: {
const auto ¶m = path->filter();
if (job.children.is_null()) {
SetupJobsForChildren(mem_root, param.child, join,
eligible_for_batch_mode, &job, &todo);
continue;
}
if (FinalizeMaterializedSubqueries(thd, join, path)) {
return nullptr;
}
iterator = NewIterator<FilterIterator>(
thd, mem_root, std::move(job.children[0]), param.condition);
break;
}
case AccessPath::SORT: {
const auto ¶m = path->sort();
if (job.children.is_null()) {
SetupJobsForChildren(mem_root, param.child, join,
eligible_for_batch_mode, &job, &todo);
continue;
}
ha_rows num_rows_estimate = param.child->num_output_rows() < 0.0
? HA_POS_ERROR
: lrint(param.child->num_output_rows());
Filesort *filesort = param.filesort;
iterator = NewIterator<SortingIterator>(
thd, mem_root, filesort, std::move(job.children[0]),
num_rows_estimate, param.tables_to_get_rowid_for, examined_rows);
if (filesort->m_remove_duplicates) {
filesort->tables[0]->duplicate_removal_iterator =
down_cast<SortingIterator *>(iterator->real_iterator());
} else {
filesort->tables[0]->sorting_iterator =
down_cast<SortingIterator *>(iterator->real_iterator());
}
break;
}
case AccessPath::AGGREGATE: {
const auto ¶m = path->aggregate();
if (job.children.is_null()) {
SetupJobsForChildren(mem_root, param.child, join,
eligible_for_batch_mode, &job, &todo);
continue;
}
Prealloced_array<TABLE *, 4> tables =
GetUsedTables(param.child, /*include_pruned_tables=*/true);
iterator = NewIterator<AggregateIterator>(
thd, mem_root, std::move(job.children[0]), join,
TableCollection(tables, /*store_rowids=*/false,
/*tables_to_get_rowid_for=*/0,
GetNullableEqRefTables(param.child)),
param.rollup);
break;
}
case AccessPath::TEMPTABLE_AGGREGATE: {
const auto ¶m = path->temptable_aggregate();
if (job.children.is_null()) {
job.AllocChildren(mem_root, 2);
todo.push_back(job);
todo.push_back({param.subquery_path,
join,
/*eligible_for_batch_mode=*/true,
&job.children[0],
{}});
todo.push_back({param.table_path,
join,
eligible_for_batch_mode,
&job.children[1],
{}});
continue;
}
iterator = unique_ptr_destroy_only<RowIterator>(
temptable_aggregate_iterator::CreateIterator(
thd, std::move(job.children[0]), param.temp_table_param,
param.table, std::move(job.children[1]), join,
param.ref_slice));
break;
}
case AccessPath::LIMIT_OFFSET: {
const auto ¶m = path->limit_offset();
if (job.children.is_null()) {
SetupJobsForChildren(mem_root, param.child, join,
eligible_for_batch_mode, &job, &todo);
continue;
}
ha_rows *send_records = nullptr;
if (param.send_records_override != nullptr) {
send_records = param.send_records_override;
} else if (join != nullptr) {
send_records = &join->send_records;
}
iterator = NewIterator<LimitOffsetIterator>(
thd, mem_root, std::move(job.children[0]), param.limit,
param.offset, param.count_all_rows, param.reject_multiple_rows,
send_records);
break;
}
case AccessPath::STREAM: {
const auto ¶m = path->stream();
if (job.children.is_null()) {
SetupJobsForChildren(mem_root, param.child, param.join,
eligible_for_batch_mode, &job, &todo);
continue;
}
iterator = NewIterator<StreamingIterator>(
thd, mem_root, std::move(job.children[0]), param.temp_table_param,
param.table, param.provide_rowid, param.join, param.ref_slice);
break;
}
case AccessPath::MATERIALIZE: {
// The table access path should be a single iterator, not a tree.
// (ALTERNATIVE counts as a single iterator in this regard.)
assert(
path->materialize().table_path->type == AccessPath::TABLE_SCAN ||
path->materialize().table_path->type == AccessPath::LIMIT_OFFSET ||
path->materialize().table_path->type == AccessPath::REF ||
path->materialize().table_path->type == AccessPath::REF_OR_NULL ||
path->materialize().table_path->type == AccessPath::EQ_REF ||
path->materialize().table_path->type == AccessPath::ALTERNATIVE ||
path->materialize().table_path->type == AccessPath::CONST_TABLE ||
path->materialize().table_path->type == AccessPath::INDEX_SCAN ||
path->materialize().table_path->type ==
AccessPath::INDEX_RANGE_SCAN ||
path->materialize().table_path->type ==
AccessPath::DYNAMIC_INDEX_RANGE_SCAN);
MaterializePathParameters *param = path->materialize().param;
if (job.children.is_null()) {
job.AllocChildren(mem_root, param->query_blocks.size() + 1);
todo.push_back(job);
todo.push_back({path->materialize().table_path,
join,
eligible_for_batch_mode,
&job.children[0],
{}});
for (size_t i = 0; i < param->query_blocks.size(); ++i) {
const MaterializePathParameters::QueryBlock &from =
param->query_blocks[i];
todo.push_back({from.subquery_path,
from.join,
/*eligible_for_batch_mode=*/true,
&job.children[i + 1],
{}});
}
continue;
}
unique_ptr_destroy_only<RowIterator> table_iterator =
std::move(job.children[0]);
Mem_root_array<materialize_iterator::QueryBlock> query_blocks(
thd->mem_root, param->query_blocks.size());
for (size_t i = 0; i < param->query_blocks.size(); ++i) {
const MaterializePathParameters::QueryBlock &from =
param->query_blocks[i];
materialize_iterator::QueryBlock &to = query_blocks[i];
to.subquery_iterator = std::move(job.children[i + 1]);
to.select_number = from.select_number;
to.join = from.join;
to.disable_deduplication_by_hash_field =
from.disable_deduplication_by_hash_field;
to.copy_items = from.copy_items;
to.temp_table_param = from.temp_table_param;
to.is_recursive_reference = from.is_recursive_reference;
to.m_first_distinct = from.m_first_distinct;
to.m_total_operands = from.m_total_operands;
to.m_operand_idx = from.m_operand_idx;
if (to.is_recursive_reference) {
// Find the recursive reference to ourselves; there should be
// exactly one, as per the standard.
RowIterator *recursive_reader = FindSingleIteratorOfType(
from.subquery_path, AccessPath::FOLLOW_TAIL);
if (recursive_reader == nullptr) {
// The recursive reference was optimized away, e.g. due to an
// impossible WHERE condition, so we're not a recursive
// reference after all.
to.is_recursive_reference = false;
} else {
to.recursive_reader =
down_cast<FollowTailIterator *>(recursive_reader);
}
}
}
JOIN *subjoin = param->ref_slice == -1 ? nullptr : query_blocks[0].join;
iterator = unique_ptr_destroy_only<RowIterator>(
materialize_iterator::CreateIterator(
thd, std::move(query_blocks), param, std::move(table_iterator),
subjoin));
break;
}
case AccessPath::MATERIALIZE_INFORMATION_SCHEMA_TABLE: {
const auto ¶m = path->materialize_information_schema_table();
if (job.children.is_null()) {
SetupJobsForChildren(mem_root, param.table_path, join,
eligible_for_batch_mode, &job, &todo);
continue;
}
iterator = NewIterator<MaterializeInformationSchemaTableIterator>(
thd, mem_root, std::move(job.children[0]), param.table_list,
param.condition);
break;
}
case AccessPath::APPEND: {
const auto ¶m = path->append();
if (job.children.is_null()) {
job.AllocChildren(mem_root, param.children->size());
todo.push_back(job);
for (size_t child_idx = 0; child_idx < param.children->size();
++child_idx) {
const AppendPathParameters &child_param =
(*param.children)[child_idx];
todo.push_back({child_param.path,
child_param.join,
/*eligible_for_batch_mode=*/true,
&job.children[child_idx],
{}});
}
continue;
}
// TODO(sgunders): Consider just sending in the array here,
// changing types in the constructor.
vector<unique_ptr_destroy_only<RowIterator>> children;
children.reserve(param.children->size());
for (unique_ptr_destroy_only<RowIterator> &child : job.children) {
children.push_back(std::move(child));
}
iterator =
NewIterator<AppendIterator>(thd, mem_root, std::move(children));
break;
}
case AccessPath::WINDOW: {
const auto ¶m = path->window();
if (job.children.is_null()) {
SetupJobsForChildren(mem_root, param.child, join,
eligible_for_batch_mode, &job, &todo);
continue;
}
if (param.needs_buffering) {
iterator = NewIterator<BufferingWindowIterator>(
thd, mem_root, std::move(job.children[0]), param.temp_table_param,
join, param.ref_slice);
} else {
iterator = NewIterator<WindowIterator>(
thd, mem_root, std::move(job.children[0]), param.temp_table_param,
join, param.ref_slice);
}
break;
}
case AccessPath::WEEDOUT: {
const auto ¶m = path->weedout();
if (job.children.is_null()) {
SetupJobsForChildren(mem_root, param.child, join,
eligible_for_batch_mode, &job, &todo);
continue;
}
iterator = NewIterator<WeedoutIterator>(
thd, mem_root, std::move(job.children[0]), param.weedout_table,
param.tables_to_get_rowid_for);
break;
}
case AccessPath::REMOVE_DUPLICATES: {
const auto ¶m = path->remove_duplicates();
if (job.children.is_null()) {
SetupJobsForChildren(mem_root, param.child, join,
eligible_for_batch_mode, &job, &todo);
continue;
}
iterator = NewIterator<RemoveDuplicatesIterator>(
thd, mem_root, std::move(job.children[0]), join, param.group_items,
param.group_items_size);
break;
}
case AccessPath::REMOVE_DUPLICATES_ON_INDEX: {
const auto ¶m = path->remove_duplicates_on_index();
if (job.children.is_null()) {
SetupJobsForChildren(mem_root, param.child, join,
eligible_for_batch_mode, &job, &todo);
continue;
}
iterator = NewIterator<RemoveDuplicatesOnIndexIterator>(
thd, mem_root, std::move(job.children[0]), param.table, param.key,
param.loosescan_key_len);
break;
}
case AccessPath::ALTERNATIVE: {
const auto ¶m = path->alternative();
if (job.children.is_null()) {
job.AllocChildren(mem_root, 2);
todo.push_back(job);
todo.push_back({param.child,
join,
eligible_for_batch_mode,
&job.children[0],
{}});
todo.push_back({param.table_scan_path,
join,
eligible_for_batch_mode,
&job.children[1],
{}});
continue;
}
iterator = NewIterator<AlternativeIterator>(
thd, mem_root, param.table_scan_path->table_scan().table,
std::move(job.children[0]), std::move(job.children[1]),
param.used_ref);
break;
}
case AccessPath::CACHE_INVALIDATOR: {
const auto ¶m = path->cache_invalidator();
if (job.children.is_null()) {
SetupJobsForChildren(mem_root, param.child, join,
eligible_for_batch_mode, &job, &todo);
continue;
}
iterator = NewIterator<CacheInvalidatorIterator>(
thd, mem_root, std::move(job.children[0]), param.name);
break;
}
case AccessPath::DELETE_ROWS: {
const auto ¶m = path->delete_rows();
if (job.children.is_null()) {
// Setting up tables for delete must be done before the child
// iterators are created, as some of the child iterators need to see
// the final read set when they are constructed, so doing it in
// DeleteRowsIterator's constructor or Init() is too late.
SetUpTablesForDelete(thd, join);
SetupJobsForChildren(mem_root, param.child, join,
eligible_for_batch_mode, &job, &todo);
continue;
}
iterator = NewIterator<DeleteRowsIterator>(
thd, mem_root, std::move(job.children[0]), join,
param.tables_to_delete_from, param.immediate_tables);
break;
}
case AccessPath::UPDATE_ROWS: {
const auto ¶m = path->update_rows();
if (job.children.is_null()) {
// Do the final setup for UPDATE before the child iterators are
// created.
if (FinalizeOptimizationForUpdate(join)) {
return nullptr;
}
SetupJobsForChildren(mem_root, param.child, join,
eligible_for_batch_mode, &job, &todo);
continue;
}
iterator = CreateUpdateRowsIterator(thd, mem_root, join,
std::move(job.children[0]));
break;
}
}
if (iterator == nullptr) {
return nullptr;
}
path->iterator = iterator.get();
*job.destination = std::move(iterator);
}
return ret;
}
void FindTablesToGetRowidFor(AccessPath *path) {
table_map handled_by_others = 0;
auto add_tables_handled_by_others = [path, &handled_by_others](
AccessPath *subpath, const JOIN *) {
if (path == subpath) return false; // Skip ourselves.
switch (subpath->type) {
case AccessPath::HASH_JOIN:
handled_by_others |=
GetUsedTableMap(subpath, /*include_pruned_tables=*/true);
FindTablesToGetRowidFor(subpath);
return true; // Don't double-traverse.
case AccessPath::BKA_JOIN:
handled_by_others |= GetUsedTableMap(subpath->bka_join().outer,
/*include_pruned_tables=*/true);
FindTablesToGetRowidFor(subpath);
return true; // Don't double-traverse.
case AccessPath::STREAM: {
subpath->stream().provide_rowid = true;
TABLE *table = subpath->stream().table;
if (table->pos_in_table_list == nullptr) {
// Don't need to set anything; see comment on the similar
// test in NewSortAccessPath().
} else {
handled_by_others |= table->pos_in_table_list->map();
}
// Doesn't really matter, we don't cross query blocks anyway.
return true;
}
default:
return false;
}
};
// We stop at MATERIALIZE and STREAM (they supply row IDs for us without
// having to ask the tables below).
switch (path->type) {
case AccessPath::HASH_JOIN:
WalkAccessPaths(path, /*join=*/nullptr,
WalkAccessPathPolicy::STOP_AT_MATERIALIZATION,
add_tables_handled_by_others);
path->hash_join().store_rowids = true;
path->hash_join().tables_to_get_rowid_for =
GetUsedTableMap(path, /*include_pruned_tables=*/true) &
~handled_by_others;
break;
case AccessPath::BKA_JOIN:
WalkAccessPaths(path->bka_join().outer, /*join=*/nullptr,
WalkAccessPathPolicy::STOP_AT_MATERIALIZATION,
add_tables_handled_by_others);
path->bka_join().store_rowids = true;
path->bka_join().tables_to_get_rowid_for =
GetUsedTableMap(path->bka_join().outer,
/*include_pruned_tables=*/true) &
~handled_by_others;
break;
case AccessPath::WEEDOUT:
WalkAccessPaths(path, /*join=*/nullptr,
WalkAccessPathPolicy::STOP_AT_MATERIALIZATION,
add_tables_handled_by_others);
path->weedout().tables_to_get_rowid_for =
GetUsedTableMap(path, /*include_pruned_tables=*/true) &
~handled_by_others;
break;
case AccessPath::SORT:
WalkAccessPaths(path, /*join=*/nullptr,
WalkAccessPathPolicy::STOP_AT_MATERIALIZATION,
add_tables_handled_by_others);
path->sort().tables_to_get_rowid_for =
GetUsedTableMap(path, /*include_pruned_tables=*/true) &
~handled_by_others;
break;
default:
my_abort();
}
}
// Move the join conditions that are left in path->filter_predicates into the
// hash join predicate of the given HASH_JOIN access path. Note that join
// conditions with subqueries are not moved. If the subqueries need to be
// materialized, then a filter access path is expected from the caller.
// So they will continue to stay as filters on top of the hash join.
//
// TODO(khatlen): It's a bit of a hack to widen the hash join condition like
// this after the plan has been found. It would be better if we found a way to
// encode the necessary information in the hypergraph itself. For example, when
// creating cycles in the hypergraph, we could add redundant complex hyperedges
// in addition to the simple cycle edges that we currently add.
static void MoveFilterPredicatesIntoHashJoinCondition(
THD *thd, AccessPath *path, const Mem_root_array<Predicate> &predicates,
int num_where_predicates) {
Mem_root_array<Item_eq_base *> equijoin_conditions(thd->mem_root);
Mem_root_array<Item *> join_conditions(thd->mem_root);
MutableOverflowBitset moved_predicates(thd->mem_root, predicates.size());
for (int filter_idx : BitsSetIn(path->filter_predicates)) {
if (filter_idx >= num_where_predicates) break;
const Predicate &predicate = predicates[filter_idx];
if (!predicate.was_join_condition) continue;
Item *condition = predicate.condition;
// Conditions with subqueries are not moved.
if (condition->has_subquery()) continue;
moved_predicates.SetBit(filter_idx);
if (condition->type() == Item::FUNC_ITEM &&
down_cast<Item_func *>(condition)
->contains_only_equi_join_condition()) {
equijoin_conditions.push_back(down_cast<Item_eq_base *>(condition));
} else {
join_conditions.push_back(condition);
}
}
if (equijoin_conditions.empty() && join_conditions.empty()) {
// No join conditions were found in the filter predicates.
return;
}
// Create a new JoinPredicate with all the conditions. We don't fully
// initialize it, since we're done planning and don't need most of the
// information any more. Just add enough to make EXPLAIN and
// CreateIteratorFromAccessPath() happy.
// TODO(khatlen): Maybe it's better to put directly into the access path those
// few parts of the join predicate that are needed, and leave the actual
// predicate and relational expression out.
auto ¶m = path->hash_join();
for (Item_eq_base *item : param.join_predicate->expr->equijoin_conditions) {
equijoin_conditions.push_back(item);
}
for (Item *item : param.join_predicate->expr->join_conditions) {
join_conditions.push_back(item);
}
RelationalExpression *expr = new (thd->mem_root) RelationalExpression(thd);
expr->type = param.join_predicate->expr->type;
expr->equijoin_conditions = std::move(equijoin_conditions);
expr->join_conditions = std::move(join_conditions);
JoinPredicate *join_predicate = new (thd->mem_root) JoinPredicate;
join_predicate->expr = expr;
param.join_predicate = join_predicate;
path->filter_predicates = OverflowBitset::Xor(
thd->mem_root, path->filter_predicates, std::move(moved_predicates));
}
Item *ConditionFromFilterPredicates(const Mem_root_array<Predicate> &predicates,
OverflowBitset mask,
int num_where_predicates) {
List<Item> items;
for (int pred_idx : BitsSetIn(mask)) {
if (pred_idx >= num_where_predicates) break;
items.push_back(predicates[pred_idx].condition);
}
return CreateConjunction(&items);
}
void ExpandSingleFilterAccessPath(THD *thd, AccessPath *path, const JOIN *join,
const Mem_root_array<Predicate> &predicates,
unsigned num_where_predicates) {
// Expand join filters for nested loop joins.
if (path->type == AccessPath::NESTED_LOOP_JOIN &&
!path->nested_loop_join().already_expanded_predicates &&
!(path->nested_loop_join().equijoin_predicates.empty() &&
path->nested_loop_join()
.join_predicate->expr->join_conditions.empty()) &&
path->nested_loop_join().inner->type != AccessPath::ZERO_ROWS) {
AccessPath *right_path = path->nested_loop_join().inner;
const RelationalExpression *expr =
path->nested_loop_join().join_predicate->expr;
// While we're collecting the join conditions, calculate cost and output
// rows (purely for display purposes). Note that this mirrors the
// calculation we are doing in CostingReceiver::ProposeNestedLoopJoin();
// we don't have space in the AccessPath to store it there.
double filter_cost = right_path->cost;
double filter_rows = right_path->num_output_rows();
List<Item> items;
for (size_t filter_idx :
BitsSetIn(path->nested_loop_join().equijoin_predicates)) {
Item *condition = expr->equijoin_conditions[filter_idx];
items.push_back(condition);
filter_cost +=
EstimateFilterCost(thd, filter_rows, condition, join->query_block)
.cost_if_not_materialized;
filter_rows *= EstimateSelectivity(thd, condition, /*trace=*/nullptr);
}
for (Item *condition : expr->join_conditions) {
items.push_back(condition);
filter_cost +=
EstimateFilterCost(thd, filter_rows, condition, join->query_block)
.cost_if_not_materialized;
filter_rows *= EstimateSelectivity(thd, condition, /*trace=*/nullptr);
}
assert(!items.is_empty());
AccessPath *filter_path = new (thd->mem_root) AccessPath;
filter_path->type = AccessPath::FILTER;
filter_path->filter().child = right_path;
// We don't bother trying to materialize subqueries in join conditions,
// since they should be very rare.
filter_path->filter().materialize_subqueries = false;
CopyBasicProperties(*right_path, filter_path);
filter_path->filter().condition = CreateConjunction(&items);
filter_path->cost = filter_cost;
filter_path->set_num_output_rows(filter_rows);
path->nested_loop_join().inner = filter_path;
// Since multiple root paths may have their filters expanded,
// and the same nested loop may be a subpath in several
// of them, we need to make sure we don't add the join predicates
// more than once, so mark them as done here.
path->nested_loop_join().already_expanded_predicates = true;
}
// If a hash join follows an edge that is part of a cycle in the hypergraph,
// there may be other applicable join predicates left in filter_predicates.
// Say we have {t1,t2} HJ {t3} along the t1.a=t3.a edge. If there is also a
// t2.b=t3.b edge, that predicate will be in filtered_predicates. In this
// case, it is desirable to have t1.a=t3.a AND t2.b=t3.b as the hash join
// predicate, and remove t2.b=t3.b from the filter predicates.
if (path->type == AccessPath::HASH_JOIN &&
path->hash_join().join_predicate->expr->join_predicate_first !=
path->hash_join().join_predicate->expr->join_predicate_last) {
MoveFilterPredicatesIntoHashJoinCondition(thd, path, predicates,
num_where_predicates);
}
// Expand filters _after_ the access path (these are much more common).
Item *condition = ConditionFromFilterPredicates(
predicates, path->filter_predicates, num_where_predicates);
if (condition == nullptr) {
return;
}
AccessPath *new_path = new (thd->mem_root) AccessPath(*path);
new_path->filter_predicates.Clear();
new_path->set_num_output_rows(path->num_output_rows_before_filter);
new_path->cost = path->cost_before_filter;
// We don't really know how much of init_cost comes from the filter,
// but we need to heed the invariant that cost >= init_cost
// also for the new (non-filter) path we're creating, even if it's
// just for display. Heuristically allocate as much as possible to
// the filter.
double filter_only_cost = path->cost - path->cost_before_filter;
new_path->init_cost = std::max(new_path->init_cost - filter_only_cost, 0.0);
new_path->init_once_cost =
std::max(new_path->init_once_cost - filter_only_cost, 0.0);
assert(new_path->cost >= new_path->init_cost);
assert(new_path->init_cost >= new_path->init_once_cost);
path->type = AccessPath::FILTER;
path->filter().condition = condition;
path->filter().child = new_path;
path->filter().materialize_subqueries = false;
// Clear filter_predicates, but keep applied_sargable_join_predicates.
MutableOverflowBitset applied_sargable_join_predicates =
path->applied_sargable_join_predicates().Clone(thd->mem_root);
applied_sargable_join_predicates.ClearBits(0, num_where_predicates);
path->filter_predicates = std::move(applied_sargable_join_predicates);
}
void ExpandFilterAccessPaths(THD *thd, AccessPath *path_arg, const JOIN *join,
const Mem_root_array<Predicate> &predicates,
unsigned num_where_predicates) {
WalkAccessPaths(path_arg, join, WalkAccessPathPolicy::ENTIRE_QUERY_BLOCK,
[thd, &predicates, num_where_predicates](
AccessPath *path, const JOIN *sub_join) {
ExpandSingleFilterAccessPath(
thd, path, sub_join, predicates, num_where_predicates);
return false;
});
}
table_map GetHashJoinTables(AccessPath *path) {
table_map tables = 0;
WalkAccessPaths(
path, /*join=*/nullptr, WalkAccessPathPolicy::STOP_AT_MATERIALIZATION,
[&tables](AccessPath *subpath, const JOIN *) {
if (subpath->type == AccessPath::HASH_JOIN) {
tables |= GetUsedTableMap(subpath, /*include_pruned_tables=*/true);
return true;
}
return false;
});
return tables;
}
|