diff --git a/sql/sql_opt_exec_shared.h b/sql/sql_opt_exec_shared.h index 483d482..f1a4c05 100644 --- a/sql/sql_opt_exec_shared.h +++ b/sql/sql_opt_exec_shared.h @@ -158,6 +158,16 @@ struct Index_lookup { } /** + Force on null value rejection, i.e the condition is '=' which is redundant + predicates and removed. + */ + void fix_null_rejecting() { + for (uint i = 0; i < key_parts; i++) { + null_rejecting |= (key_part_map)1 << i; + } + } + + /** Check if there are triggered/guarded conditions that might be 'switched off' by the subquery code when executing 'Full scan on NULL key' subqueries. diff --git a/sql/sql_optimizer.cc b/sql/sql_optimizer.cc index ff30bea..7e18ca1 100644 --- a/sql/sql_optimizer.cc +++ b/sql/sql_optimizer.cc @@ -8969,6 +8969,29 @@ static bool test_if_ref(THD *thd, Item_field *left_item, Item *right_item, } /** + @brief + Force on null value rejection + + @details + Set ref->null_rejection to avoid matching items when the error is NULL in + certain special circumstances, i.e. the equality predicate 'left_item = right_item' + is redundant due to a REF-access already being set up on the table. +*/ +static void set_null_rejecting(Item_field *item) { + assert(!item->depended_from); + + Field *field = item->field; + JOIN_TAB *join_tab = field->table->reginfo.join_tab; + + assert(join_tab); + // "ref_or_null" implements "x=y or x is null" + assert(!field->table->const_table && (join_tab->type() != JT_REF_OR_NULL)); + + Index_lookup *ref = &join_tab->ref(); + ref->fix_null_rejecting(); +} + +/** @brief Remove redundant predicates from condition, return the reduced condition. @@ -9095,6 +9118,7 @@ static bool reduce_cond_for_table(THD *thd, Item *cond, table_map null_extended, return true; } if (redundant) { + set_null_rejecting(down_cast(left_item)); return false; } if (right_item->type() == Item::FIELD_ITEM && @@ -9104,6 +9128,7 @@ static bool reduce_cond_for_table(THD *thd, Item *cond, table_map null_extended, return true; } if (redundant) { + set_null_rejecting(down_cast(right_item)); return false; } }