From 8b751f7f7d6107db7155344531fa8b43f152ecf9 Mon Sep 17 00:00:00 2001 From: casazhang Date: Mon, 13 Sep 2021 11:15:07 +0800 Subject: [PATCH] [bugfix] Derived condition pushdown rewrite ignores user variables. --- sql/parse_tree_items.cc | 2 ++ sql/sql_derived.cc | 1 + sql/sql_lex.h | 3 +++ 3 files changed, 6 insertions(+) diff --git a/sql/parse_tree_items.cc b/sql/parse_tree_items.cc index 4801c30..20b79ec 100644 --- a/sql/parse_tree_items.cc +++ b/sql/parse_tree_items.cc @@ -538,6 +538,7 @@ bool PTI_variable_aux_set_var::itemize(Parse_context *pc, Item **res) { return true; } lex->set_uncacheable(pc->select, UNCACHEABLE_RAND); + pc->select->has_user_vars = true; return lex->set_var_list.push_back(this); } @@ -550,6 +551,7 @@ bool PTI_user_variable::itemize(Parse_context *pc, Item **res) { return true; } lex->set_uncacheable(pc->select, UNCACHEABLE_RAND); + pc->select->has_user_vars = true; return false; } diff --git a/sql/sql_derived.cc b/sql/sql_derived.cc index 5e31d85..9e9d78f 100644 --- a/sql/sql_derived.cc +++ b/sql/sql_derived.cc @@ -833,6 +833,7 @@ bool TABLE_LIST::can_push_condition_to_derived(THD *thd) { OPTIMIZER_SWITCH_DERIVED_CONDITION_PUSHDOWN) && !unit->is_union() && !unit->first_query_block()->has_limit() && !is_inner_table_of_outer_join() && + !unit->first_query_block()->has_user_vars && !(common_table_expr() && common_table_expr()->references.size() >= 2); } diff --git a/sql/sql_lex.h b/sql/sql_lex.h index 564b373..a9d68c6 100644 --- a/sql/sql_lex.h +++ b/sql/sql_lex.h @@ -2138,6 +2138,9 @@ class Query_block { /// @note that using this means we modify resolved data during optimization uint hidden_items_from_optimization{0}; + /// Whether the query block has user_vars + bool has_user_vars{false}; + private: friend class Query_expression; friend class Condition_context; -- 1.8.3.1