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
|
/* Copyright (c) 2015, 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/opt_hints.h"
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include "m_ctype.h"
#include "my_table_map.h"
#include "mysql/udf_registration_types.h"
#include "mysqld_error.h"
#include "sql/derror.h" // ER_THD
#include "sql/error_handler.h"
#include "sql/item.h"
#include "sql/item_subselect.h"
#include "sql/key.h"
#include "sql/mysqld.h" // table_alias_charset
#include "sql/nested_join.h"
#include "sql/parse_tree_hints.h"
#include "sql/set_var.h"
#include "sql/sql_class.h" // THD
#include "sql/sql_const.h"
#include "sql/sql_error.h" // Sql_condition
#include "sql/sql_optimizer.h" // JOIN class
#include "sql/sql_select.h"
#include "sql/table.h"
struct MEM_ROOT;
/**
Information about hints. Should be
synchronized with opt_hints_enum enum.
Note: Hint name depends on hint state. 'NO_' prefix is added
if appropriate hint state bit(see Opt_hints_map::hints) is not
set. Depending on 'switch_state_arg' argument in 'parse tree
object' constructors(see parse_tree_hints.[h,cc]) implementor
can control wishful form of the hint name.
*/
struct st_opt_hint_info opt_hint_info[] = {
{"BKA", true, true, false},
{"BNL", true, true, false},
{"ICP", true, true, false},
{"MRR", true, true, false},
{"NO_RANGE_OPTIMIZATION", true, true, false},
{"MAX_EXECUTION_TIME", false, false, false},
{"QB_NAME", false, false, false},
{"SEMIJOIN", false, false, false},
{"SUBQUERY", false, false, false},
{"MERGE", true, true, false},
{"JOIN_PREFIX", false, false, true},
{"JOIN_SUFFIX", false, false, true},
{"JOIN_ORDER", false, false, true},
{"JOIN_FIXED_ORDER", false, true, false},
{"INDEX_MERGE", false, false, false},
{"RESOURCE_GROUP", false, false, false},
{"SKIP_SCAN", false, false, false},
{"HASH_JOIN", true, true, false},
{"INDEX", false, false, false},
{"JOIN_INDEX", false, false, false},
{"GROUP_INDEX", false, false, false},
{"ORDER_INDEX", false, false, false},
{"DERIVED_CONDITION_PUSHDOWN", true, true, false},
{nullptr, false, false, false}};
/**
Prefix for system generated query block name.
Used in information warning in EXPLAIN oputput.
*/
const LEX_CSTRING sys_qb_prefix = {"select#", 7};
/*
Compare LEX_CSTRING objects.
@param s The 1st string
@param t The 2nd string
@param cs Pointer to character set
@return 0 if strings are equal
1 if s is greater
-1 if t is greater
*/
int cmp_lex_string(const LEX_CSTRING &s, const LEX_CSTRING &t,
const CHARSET_INFO *cs) {
return cs->coll->strnncollsp(cs, pointer_cast<const uchar *>(s.str), s.length,
pointer_cast<const uchar *>(t.str), t.length);
}
bool Opt_hints::get_switch(opt_hints_enum type_arg) const {
if (is_specified(type_arg)) return hints_map.switch_on(type_arg);
if (opt_hint_info[type_arg].check_upper_lvl)
return parent->get_switch(type_arg);
return false;
}
Opt_hints *Opt_hints::find_by_name(const LEX_CSTRING *name_arg,
const CHARSET_INFO *cs) const {
for (uint i = 0; i < child_array.size(); i++) {
const LEX_CSTRING *name = child_array[i]->get_print_name();
if (!cmp_lex_string(*name, *name_arg, cs)) return child_array[i];
}
return nullptr;
}
void Opt_hints::print(const THD *thd, String *str, enum_query_type query_type) {
for (uint i = 0; i < MAX_HINT_ENUM; i++) {
if (opt_hint_info[i].irregular_hint) continue;
opt_hints_enum hint = static_cast<opt_hints_enum>(i);
/*
If printing a normalized query, also unresolved hints will be printed.
(This is needed by query rewrite plugins which request
normalized form before resolving has been performed.)
*/
if (is_specified(hint) && !ignore_print(hint) &&
(is_resolved(hint) || query_type == QT_NORMALIZED_FORMAT)) {
append_hint_type(str, hint);
str->append(STRING_WITH_LEN("("));
append_name(thd, str);
if (!opt_hint_info[i].switch_hint)
get_complex_hints(hint)->append_args(thd, str);
str->append(STRING_WITH_LEN(") "));
}
}
print_irregular_hints(thd, str);
for (uint i = 0; i < child_array.size(); i++)
child_array[i]->print(thd, str, query_type);
}
void Opt_hints::append_hint_type(String *str, opt_hints_enum type) {
const char *hint_name = opt_hint_info[type].hint_name;
if (!hints_map.switch_on(type)) str->append(STRING_WITH_LEN("NO_"));
str->append(hint_name);
}
void Opt_hints::print_warn_unresolved(THD *thd) {
String hint_name_str, hint_type_str;
append_name(thd, &hint_name_str);
for (uint i = 0; i < MAX_HINT_ENUM; i++) {
if (is_specified(static_cast<opt_hints_enum>(i))) {
hint_type_str.length(0);
append_hint_type(&hint_type_str, static_cast<opt_hints_enum>(i));
push_warning_printf(
thd, Sql_condition::SL_WARNING, ER_UNRESOLVED_HINT_NAME,
ER_THD(thd, ER_UNRESOLVED_HINT_NAME), hint_name_str.c_ptr_safe(),
hint_type_str.c_ptr_safe());
get_parent()->set_unresolved(static_cast<opt_hints_enum>(i));
}
}
}
void Opt_hints::check_unresolved(THD *thd) {
if (!is_resolved(MAX_HINT_ENUM)) print_warn_unresolved(thd);
if (!is_all_resolved()) {
for (uint i = 0; i < child_array.size(); i++)
child_array[i]->check_unresolved(thd);
}
}
PT_hint *Opt_hints_global::get_complex_hints(opt_hints_enum type) {
if (type == MAX_EXEC_TIME_HINT_ENUM) return max_exec_time;
assert(0);
return nullptr;
}
void Opt_hints_global::print_irregular_hints(const THD *thd, String *str) {
if (sys_var_hint) sys_var_hint->print(thd, str);
}
Opt_hints_qb::Opt_hints_qb(Opt_hints *opt_hints_arg, MEM_ROOT *mem_root_arg,
uint select_number_arg)
: Opt_hints(nullptr, opt_hints_arg, mem_root_arg),
select_number(select_number_arg),
subquery_hint(nullptr),
semijoin_hint(nullptr),
join_order_hints(mem_root_arg),
join_order_hints_ignored(0) {
sys_name.str = buff;
sys_name.length =
snprintf(buff, sizeof(buff), "%s%x", sys_qb_prefix.str, select_number);
}
PT_hint *Opt_hints_qb::get_complex_hints(opt_hints_enum type) {
if (type == SEMIJOIN_HINT_ENUM) return semijoin_hint;
if (type == SUBQUERY_HINT_ENUM) return subquery_hint;
assert(0);
return nullptr;
}
Opt_hints_table *Opt_hints_qb::adjust_table_hints(Table_ref *tr) {
const LEX_CSTRING str = {tr->alias, strlen(tr->alias)};
Opt_hints_table *tab =
static_cast<Opt_hints_table *>(find_by_name(&str, table_alias_charset));
tr->opt_hints_qb = this;
if (!tab) // Tables not found
return nullptr;
tab->adjust_key_hints(tr);
return tab;
}
bool Opt_hints_qb::semijoin_enabled(const THD *thd) const {
if (subquery_hint) // SUBQUERY hint disables semi-join
return false;
if (semijoin_hint) {
// SEMIJOIN hint will always force semijoin regardless of optimizer_switch
if (semijoin_hint->switch_on()) return true;
// NO_SEMIJOIN hint. If strategy list is empty, do not use SEMIJOIN
if (semijoin_hint->get_args() == 0) return false;
// Fall through: NO_SEMIJOIN w/ strategies neither turns SEMIJOIN off nor on
}
return thd->optimizer_switch_flag(OPTIMIZER_SWITCH_SEMIJOIN);
}
uint Opt_hints_qb::sj_enabled_strategies(uint opt_switches) const {
// Hints override switches
if (semijoin_hint) {
const uint strategies = semijoin_hint->get_args();
if (semijoin_hint->switch_on()) // SEMIJOIN hint
return (strategies == 0) ? opt_switches : strategies;
// NO_SEMIJOIN hint. Hints and optimizer_switch both affect strategies
return ~strategies & opt_switches;
}
return opt_switches;
}
Subquery_strategy Opt_hints_qb::subquery_strategy() const {
if (subquery_hint)
return static_cast<Subquery_strategy>(subquery_hint->get_args());
return Subquery_strategy::UNSPECIFIED;
}
void Opt_hints_qb::print_irregular_hints(const THD *thd, String *str) {
/* Print join order hints */
for (uint i = 0; i < join_order_hints.size(); i++) {
if (join_order_hints_ignored & (1ULL << i)) continue;
const PT_qb_level_hint *hint = join_order_hints[i];
str->append(opt_hint_info[hint->type()].hint_name);
str->append(STRING_WITH_LEN("("));
append_name(thd, str);
str->append(STRING_WITH_LEN(" "));
hint->append_args(thd, str);
str->append(STRING_WITH_LEN(") "));
}
}
/**
Print warning about unresolved table for join order hints.
@param thd pointer to THD object
@param type hint type
@param hint_table table name
*/
static void print_join_order_warn(THD *thd, opt_hints_enum type,
const Hint_param_table *hint_table) {
String hint_name_str, hint_type_str;
hint_type_str.append(opt_hint_info[type].hint_name);
append_table_name(thd, &hint_name_str, &hint_table->opt_query_block,
&hint_table->table);
push_warning_printf(thd, Sql_condition::SL_WARNING, ER_UNRESOLVED_HINT_NAME,
ER_THD(thd, ER_UNRESOLVED_HINT_NAME),
hint_name_str.c_ptr_safe(), hint_type_str.c_ptr_safe());
}
/**
Function compares hint table name and Table_ref table name.
Query block name is taken into account.
@param hint_table hint table name
@param table pointer to Table_ref object
@return false if table names are equal, true otherwise.
*/
static bool compare_table_name(const Hint_param_table *hint_table,
const Table_ref *table) {
const LEX_CSTRING *hint_qb_name = &hint_table->opt_query_block;
const LEX_CSTRING *hint_table_name = &hint_table->table;
const LEX_CSTRING *table_qb_name =
table->opt_hints_qb ? table->opt_hints_qb->get_name() : nullptr;
const LEX_CSTRING table_name = {table->alias, strlen(table->alias)};
if (table_qb_name && table_qb_name->length > 0 && hint_qb_name->length > 0) {
if (cmp_lex_string(*hint_qb_name, *table_qb_name, system_charset_info))
return true;
}
if (cmp_lex_string(*hint_table_name, table_name, system_charset_info))
return true;
return false;
}
/**
Function returns dependencies used for updating table dependencies
depending on hint type.
@param type hint type
@param hint_tab_map hint table map
@param table_map table map
@return table dependencies.
*/
static table_map get_other_dep(opt_hints_enum type, table_map hint_tab_map,
table_map table_map) {
switch (type) {
case JOIN_PREFIX_HINT_ENUM:
if (hint_tab_map & table_map) // Hint table: No additional dependencies
return 0;
// Other tables: depend on all hint tables
return hint_tab_map;
case JOIN_SUFFIX_HINT_ENUM:
if (hint_tab_map & table_map) // Hint table: depends on all other tables
return ~hint_tab_map;
return 0;
case JOIN_ORDER_HINT_ENUM:
return 0; // No additional dependencies
default:
assert(0);
break;
}
return 0;
}
/**
Auxiliary class is used to save/restore table dependencies.
*/
class Join_order_hint_handler {
JOIN *join;
table_map *orig_dep_array; ///< Original table dependencies
public:
Join_order_hint_handler(JOIN *join_arg)
: join(join_arg), orig_dep_array(nullptr) {}
/**
Allocates and initializes orig_dep_array.
@return true if orig_dep_array is allocated, false otherwise.
*/
bool init() {
orig_dep_array =
(table_map *)join->thd->alloc(sizeof(table_map) * join->tables);
if (orig_dep_array == nullptr) return true;
for (uint i = 0; i < join->tables; i++) {
JOIN_TAB *tab = &join->join_tab[i];
orig_dep_array[i] = tab->dependent;
}
return false;
}
void no_restore_deps() { orig_dep_array = nullptr; }
/**
Restore original dependencies if necessary.
*/
~Join_order_hint_handler() {
if (orig_dep_array == nullptr) return;
for (uint i = 0; i < join->tables; i++) {
JOIN_TAB *tab = &join->join_tab[i];
tab->dependent = orig_dep_array[i];
}
}
};
/**
Function updates dependencies for nested joins. If table
specified in the hint belongs to nested join, we need
to update dependencies of all tables of the nested join
with the same dependency as for the hint table. It is also
necessary to update all tables of the nested joins this table
is part of.
@param join pointer to JOIN object
@param hint_tab pointer to JOIN_TAB object
@param hint_tab_map map of the tables, specified in the hint
*/
static void update_nested_join_deps(JOIN *join, const JOIN_TAB *hint_tab,
table_map hint_tab_map) {
const Table_ref *table = hint_tab->table_ref;
if (table->embedding) {
for (uint i = 0; i < join->tables; i++) {
JOIN_TAB *tab = &join->join_tab[i];
if (tab->table_ref->embedding) {
const NESTED_JOIN *const nested_join =
tab->table_ref->embedding->nested_join;
if (hint_tab->embedding_map & nested_join->nj_map)
tab->dependent |= (hint_tab_map & ~nested_join->used_tables);
}
}
}
}
/**
Function resolves hint tables, checks and sets table dependencies
according to the hint. If the hint is ignored due to circular table
dependencies, original dependencies are restored.
@param join pointer to JOIN object
@param hint_table_list hint table list
@param type hint type
@return false if hint is applied, true otherwise.
*/
static bool set_join_hint_deps(JOIN *join,
const Hint_param_table_list *hint_table_list,
opt_hints_enum type) {
/*
Make a copy of the original table dependencies.
If an error occurs when applying the hint dependencies,
the original dependencies will be restored by the destructor for this
object.
*/
Join_order_hint_handler hint_handler(join);
// Map of the tables, specified in the hint
table_map hint_tab_map = 0;
if (hint_handler.init()) return true;
for (const Hint_param_table *hint_table = hint_table_list->begin();
hint_table < hint_table_list->end(); hint_table++) {
bool hint_table_found = false;
for (uint i = 0; i < join->tables; i++) {
const Table_ref *table = join->join_tab[i].table_ref;
if (!compare_table_name(hint_table, table)) {
hint_table_found = true;
/*
Const tables are excluded from the process of dependency setting
since they are always first in the table order. Note that it
does not prevent the hint from being applied to the non-const
tables of the hint.
*/
if (join->const_table_map & table->map()) break;
JOIN_TAB *tab = &join->join_tab[i];
// Hint tables are always dependent on preceding tables
tab->dependent |= hint_tab_map;
update_nested_join_deps(join, tab, hint_tab_map);
hint_tab_map |= tab->table_ref->map();
break;
}
}
if (!hint_table_found) {
print_join_order_warn(join->thd, type, hint_table);
return true;
}
}
// Add dependencies that are related to non-hint tables
for (uint i = 0; i < join->tables; i++) {
JOIN_TAB *tab = &join->join_tab[i];
table_map dependent_tables =
get_other_dep(type, hint_tab_map, tab->table_ref->map());
update_nested_join_deps(join, tab, dependent_tables);
tab->dependent |= dependent_tables;
}
if (join->propagate_dependencies()) return true;
hint_handler.no_restore_deps();
return false;
}
void Opt_hints_qb::apply_join_order_hints(JOIN *join) {
for (uint hint_idx = 0; hint_idx < join_order_hints.size(); hint_idx++) {
PT_qb_level_hint *hint = join_order_hints[hint_idx];
Hint_param_table_list *hint_table_list = hint->get_table_list();
if (set_join_hint_deps(join, hint_table_list, hint->type()))
// Skip hint printing in EXPLAIN message.
join_order_hints_ignored |= 1ULL << hint_idx;
}
}
void Opt_hints_table::adjust_key_hints(Table_ref *tr) {
set_resolved();
if (child_array_ptr()->size() == 0) // No key level hints
{
get_parent()->incr_resolved_children();
return;
}
/*
Make sure that adjustment is done only once.
Table has already been processed if keyinfo_array is not empty.
*/
if (keyinfo_array.size()) return;
// Names of keys are not known for
// derived/internal temp/table_function tables.
if (!tr->is_base_table()) return;
TABLE *table = tr->table;
keyinfo_array.resize(table->s->keys, nullptr);
for (Opt_hints **hint = child_array_ptr()->begin();
hint < child_array_ptr()->end(); ++hint) {
KEY *key_info = table->key_info;
for (uint j = 0; j < table->s->keys; j++, key_info++) {
const LEX_CSTRING key_name = {key_info->name, strlen(key_info->name)};
if (!cmp_lex_string(*(*hint)->get_name(), key_name,
system_charset_info)) {
(*hint)->set_resolved();
keyinfo_array[j] = static_cast<Opt_hints_key *>(*hint);
incr_resolved_children();
set_compound_key_hint_map(*hint, j);
}
}
}
/*
Do not increase number of resolved tables
if there are unresolved key objects. It's
important for check_unresolved() function.
*/
if (is_all_resolved()) get_parent()->incr_resolved_children();
}
bool is_compound_hint(opt_hints_enum type_arg) {
return (
type_arg == INDEX_MERGE_HINT_ENUM || type_arg == SKIP_SCAN_HINT_ENUM ||
type_arg == INDEX_HINT_ENUM || type_arg == JOIN_INDEX_HINT_ENUM ||
type_arg == GROUP_INDEX_HINT_ENUM || type_arg == ORDER_INDEX_HINT_ENUM);
}
PT_hint *Opt_hints_table::get_complex_hints(opt_hints_enum type) {
assert(is_compound_hint(type));
return get_compound_key_hint(type)->get_pt_hint();
}
bool Opt_hints_table::is_hint_conflicting(Opt_hints_key *key_hint,
opt_hints_enum type) {
if ((key_hint == nullptr) && is_specified(type)) return true;
return (key_hint && key_hint->is_specified(type));
}
/**
Function updates key_to_use key map depending on index hint state.
@param keys_to_use key to use
@param available_keys_to_use available keys to use
@param type_arg hint type
*/
void Opt_hints_table::update_index_hint_map(Key_map *keys_to_use,
Key_map *available_keys_to_use,
opt_hints_enum type_arg) {
// Check if hint is resolved.
if (is_resolved(type_arg)) {
Key_map *keys_specified_in_hint =
get_compound_key_hint(type_arg)->get_key_map();
if (get_switch(type_arg)) {
// If the hint is on and no keys are specified in the hint,
// then set "keys_to_use" to all the available keys.
if (keys_specified_in_hint->is_clear_all())
keys_to_use->merge(*available_keys_to_use);
// If hint is on and there are keys specified in the hint, then add
// the specified keys to "keys_to_use" taking care of the disabled keys
// (available_keys_to_use).
else {
keys_to_use->merge(*keys_specified_in_hint);
keys_to_use->intersect(*available_keys_to_use);
}
} else {
// If hint is off and there are no keys specified in the hint, then
// we clear "keys_to_use".
if (keys_specified_in_hint->is_clear_all()) keys_to_use->clear_all();
// If hint is off and some keys are specified in the hint, then remove
// the specified keys from "keys_to_use.
else
keys_to_use->subtract(*keys_specified_in_hint);
}
}
}
/**
Function updates keys_in_use_for_query, keys_in_use_for_group_by,
keys_in_use_for_order_by depending on INDEX, JOIN_INDEX, GROUP_INDEX,
ORDER_INDEX hints.
@param thd pointer to THD object
@param tbl pointer to TABLE object
@return false if no index hint is specified, true otherwise.
*/
bool Opt_hints_table::update_index_hint_maps(THD *thd, TABLE *tbl) {
if (!is_resolved(INDEX_HINT_ENUM) && !is_resolved(JOIN_INDEX_HINT_ENUM) &&
!is_resolved(GROUP_INDEX_HINT_ENUM) &&
!is_resolved(ORDER_INDEX_HINT_ENUM))
return false; // No index hint is specified
Key_map usable_index_map(tbl->s->usable_indexes(thd));
tbl->keys_in_use_for_query = tbl->keys_in_use_for_group_by =
tbl->keys_in_use_for_order_by = usable_index_map;
bool force_index = is_force_index_hint(INDEX_HINT_ENUM);
tbl->force_index = (force_index || is_force_index_hint(JOIN_INDEX_HINT_ENUM));
tbl->force_index_group =
(force_index || is_force_index_hint(GROUP_INDEX_HINT_ENUM));
tbl->force_index_order =
(force_index || is_force_index_hint(ORDER_INDEX_HINT_ENUM));
if (tbl->force_index || tbl->force_index_group || tbl->force_index_order) {
tbl->keys_in_use_for_query.clear_all();
tbl->keys_in_use_for_group_by.clear_all();
tbl->keys_in_use_for_order_by.clear_all();
}
update_index_hint_map(&tbl->keys_in_use_for_query, &usable_index_map,
INDEX_HINT_ENUM);
update_index_hint_map(&tbl->keys_in_use_for_group_by, &usable_index_map,
INDEX_HINT_ENUM);
update_index_hint_map(&tbl->keys_in_use_for_order_by, &usable_index_map,
INDEX_HINT_ENUM);
update_index_hint_map(&tbl->keys_in_use_for_query, &usable_index_map,
JOIN_INDEX_HINT_ENUM);
update_index_hint_map(&tbl->keys_in_use_for_group_by, &usable_index_map,
GROUP_INDEX_HINT_ENUM);
update_index_hint_map(&tbl->keys_in_use_for_order_by, &usable_index_map,
ORDER_INDEX_HINT_ENUM);
/* Make sure "covering_keys" does not include indexes disabled with a hint */
Key_map covering_keys(tbl->keys_in_use_for_query);
covering_keys.merge(tbl->keys_in_use_for_group_by);
covering_keys.merge(tbl->keys_in_use_for_order_by);
tbl->covering_keys.intersect(covering_keys);
return true;
}
/**
Function checks if INDEX hint is conflicting with
already specified JOIN_INDEX, GROUP_INDEX, ORDER_INDEX
hints.
@param table_hint pointer to table hint
@param key_hint pointer to key hint
@return false if no conflict, true otherwise.
*/
bool Glob_index_key_hint::is_hint_conflicting(Opt_hints_table *table_hint,
Opt_hints_key *key_hint) {
return (table_hint->is_hint_conflicting(key_hint, JOIN_INDEX_HINT_ENUM) ||
table_hint->is_hint_conflicting(key_hint, GROUP_INDEX_HINT_ENUM) ||
table_hint->is_hint_conflicting(key_hint, ORDER_INDEX_HINT_ENUM));
}
/**
Function checks if JOIN_INDEX|GROUP_INDEX|ORDER_INDEX
hint is conflicting with already specified INDEX hint.
@param table_hint pointer to table hint
@param key_hint pointer to key hint
@return false if no conflict, true otherwise.
*/
bool Index_key_hint::is_hint_conflicting(Opt_hints_table *table_hint,
Opt_hints_key *key_hint) {
return table_hint->is_hint_conflicting(key_hint, INDEX_HINT_ENUM);
}
/**
Function prints hint using the info from set_var variable.
@param thd Thread handle
@param str Pointer to string object
@param var Pointer to set_var object
*/
static void print_hint_from_var(const THD *thd, String *str, set_var *var) {
str->append(STRING_WITH_LEN("SET_VAR("));
var->print_short(thd, str);
str->append(STRING_WITH_LEN(") "));
}
/**
Function prints hint as it is specified.
@param str Pointer to string object
@param sys_var_name Variable name
@param sys_var_value Variable value
*/
static void print_hint_specified(String *str, const std::string &sys_var_name,
Item *sys_var_value) {
str->append(STRING_WITH_LEN("SET_VAR("));
str->append(sys_var_name);
str->append(STRING_WITH_LEN("="));
char buff[STRING_BUFFER_USUAL_SIZE];
String str_buff(buff, sizeof(buff), system_charset_info), *str_res;
str_res = sys_var_value->val_str(&str_buff);
if (sys_var_value->result_type() == STRING_RESULT) {
str->append(STRING_WITH_LEN("'"));
str->append(str_res->ptr(), str_res->length());
str->append(STRING_WITH_LEN("'"));
} else if (sys_var_value->result_type() == INT_RESULT)
str->append(str_res->ptr(), str_res->length());
str->append(STRING_WITH_LEN(") "));
}
bool Sys_var_hint::add_var(THD *thd, const System_variable_tracker &var_tracker,
Item *sys_var_value) {
for (uint i = 0; i < var_list.size(); i++) {
const Hint_set_var *hint_var = var_list[i];
set_var *var = hint_var->var;
std::string existent_name{var->m_var_tracker.get_var_name()};
std::string new_name{var_tracker.get_var_name()};
/*
Issue a warning if system variable is already present in hint list.
*/
if (!cmp_lex_string(
LEX_CSTRING{existent_name.c_str(), existent_name.size()},
LEX_CSTRING{new_name.c_str(), new_name.size()},
system_charset_info)) {
String str;
print_hint_specified(&str, existent_name, sys_var_value);
push_warning_printf(
thd, Sql_condition::SL_WARNING, ER_WARN_CONFLICTING_HINT,
ER_THD(thd, ER_WARN_CONFLICTING_HINT), str.c_ptr_safe());
return false;
}
}
set_var *var =
new (thd->mem_root) set_var(OPT_SESSION, var_tracker, sys_var_value);
if (!var) return true;
Hint_set_var *hint_var = new (thd->mem_root) Hint_set_var(var);
if (!hint_var) return true;
return var_list.push_back(hint_var);
}
void Sys_var_hint::update_vars(THD *thd) {
// Skip SET_VAR hint applying on the slave.
if (thd->slave_thread) return;
Set_var_error_handler error_handler(false);
for (uint i = 0; i < var_list.size(); i++) {
thd->push_internal_handler(&error_handler);
Hint_set_var *hint_var = var_list[i];
set_var *var = hint_var->var;
if (!var->resolve(thd) && !var->check(thd)) {
auto f = [thd](const System_variable_tracker &, sys_var *v) -> Item * {
return v->copy_value(thd);
};
Item *save_value =
var->m_var_tracker.access_system_variable<Item *>(thd, f).value_or(
nullptr);
if (!var->update(thd)) hint_var->save_value = save_value;
}
thd->pop_internal_handler();
error_handler.reset_state();
}
}
void Sys_var_hint::restore_vars(THD *thd) {
Set_var_error_handler error_handler(true);
thd->push_internal_handler(&error_handler);
for (uint i = 0; i < var_list.size(); i++) {
Hint_set_var *hint_var = var_list[i];
set_var *var = hint_var->var;
if (hint_var->save_value) {
/* Restore original value for update */
std::swap(var->value, hint_var->save_value);
/*
There should be no error since original value is restored.
*/
#ifndef NDEBUG
assert(!var->check(thd));
assert(!var->update(thd));
#else
(void)var->check(thd);
(void)var->update(thd);
#endif
/* Restore hint value for further executions */
std::swap(var->value, hint_var->save_value);
}
}
thd->pop_internal_handler();
}
void Sys_var_hint::print(const THD *thd, String *str) {
for (uint i = 0; i < var_list.size(); i++) {
Hint_set_var *hint_var = var_list[i];
if (hint_var->save_value) print_hint_from_var(thd, str, hint_var->var);
}
}
/**
Function returns hint value depending on
the specified hint level. If hint is specified
on current level, current level hint value is
returned, otherwise parent level hint is checked.
@param hint Pointer to the hint object
@param parent_hint Pointer to the parent hint object,
should never be NULL
@param type_arg hint type
@param [out] ret_val hint value depending on
what hint level is used
@return true if hint is specified, false otherwise
*/
static bool get_hint_state(Opt_hints *hint, Opt_hints *parent_hint,
opt_hints_enum type_arg, bool *ret_val) {
assert(parent_hint);
if (opt_hint_info[type_arg].switch_hint) {
if (hint && hint->is_specified(type_arg)) {
*ret_val = hint->get_switch(type_arg);
return true;
}
if (opt_hint_info[type_arg].check_upper_lvl &&
parent_hint->is_specified(type_arg)) {
*ret_val = parent_hint->get_switch(type_arg);
return true;
}
} else {
if (hint && hint->is_specified(type_arg) &&
/*
This check is necessary because function idx_merge_key_enabled()
can operate only with resolved hint. For unresolved hint function,
idx_merge_key_enabled() always returns 'true' to emulate absence
of the hint.
*/
hint->is_resolved(type_arg)) {
*ret_val = hint->get_switch(type_arg);
return true;
}
}
return false;
}
bool hint_key_state(const THD *thd, const Table_ref *table, uint keyno,
opt_hints_enum type_arg, uint optimizer_switch) {
Opt_hints_table *table_hints = table->opt_hints_table;
/* Parent should always be initialized */
if (table_hints && keyno != MAX_KEY) {
Opt_hints_key *key_hints = table_hints->keyinfo_array.size() > 0
? table_hints->keyinfo_array[keyno]
: NULL;
bool ret_val = false;
if (get_hint_state(key_hints, table_hints, type_arg, &ret_val))
return ret_val;
}
return thd->optimizer_switch_flag(optimizer_switch);
}
bool hint_table_state(const THD *thd, const Table_ref *table_list,
opt_hints_enum type_arg, uint optimizer_switch) {
if (table_list->opt_hints_qb) {
bool ret_val = false;
if (get_hint_state(table_list->opt_hints_table, table_list->opt_hints_qb,
type_arg, &ret_val))
return ret_val;
}
return thd->optimizer_switch_flag(optimizer_switch);
}
void append_table_name(const THD *thd, String *str, const LEX_CSTRING *qb_name,
const LEX_CSTRING *table_name) {
/* Append table name */
append_identifier(thd, str, table_name->str, table_name->length);
/* Append QB name */
if (qb_name && qb_name->length > 0) {
str->append(STRING_WITH_LEN("@"));
append_identifier(thd, str, qb_name->str, qb_name->length);
}
}
bool compound_hint_key_enabled(const TABLE *table, uint keyno,
opt_hints_enum type_arg) {
Opt_hints_table *table_hints = table->pos_in_table_list->opt_hints_table;
if (table_hints && table_hints->is_resolved(type_arg)) {
if (table_hints->get_compound_key_hint(type_arg)->is_key_map_clear_all())
return table_hints->get_compound_key_hint(type_arg)
->get_pt_hint()
->switch_on();
return table_hints->get_compound_key_hint(type_arg)->is_set_key_map(
keyno) == table_hints->get_compound_key_hint(type_arg)
->get_pt_hint()
->switch_on();
}
return true;
}
bool idx_merge_hint_state(THD *thd, const TABLE *table,
bool *use_cheapest_index_merge) {
bool force_index_merge =
hint_table_state(thd, table->pos_in_table_list, INDEX_MERGE_HINT_ENUM, 0);
if (force_index_merge) {
assert(table->pos_in_table_list->opt_hints_table);
Opt_hints_table *table_hints = table->pos_in_table_list->opt_hints_table;
/*
If INDEX_MERGE hint is used without only specified index,
cheapest index merge should be used.
*/
*use_cheapest_index_merge = table_hints->index_merge.is_key_map_clear_all();
}
return force_index_merge;
}
|