From fb018850dc04f15234e3b1afdfb5911f28f9ecb9 Mon Sep 17 00:00:00 2001 From: Michael Widenius Date: Sun, 26 Jan 2014 21:48:23 +0100 Subject: [PATCH] Fixed that setup_natural_join_row_types can safely be called twice sql/item.h: Added cache for setup_natural_join_row_types sql/sql_base.cc: Cache old value of first_name_resolution_table for next call. (It's not safe to try to recalculate the value as the join structure may have been changed by the optimizer) --- sql/item.h | 2 ++ sql/sql_base.cc | 13 ++++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/sql/item.h b/sql/item.h index c4fe24bf482..20b7104bcda 100644 --- a/sql/item.h +++ b/sql/item.h @@ -332,6 +332,8 @@ struct Name_resolution_context: Sql_alloc */ TABLE_LIST *last_name_resolution_table; + /* Cache first_name_resolution_table in setup_natural_join_row_types */ + TABLE_LIST *natural_join_first_table; /* SELECT_LEX item belong to, in case of merged VIEW it can differ from SELECT_LEX where item was created, so we can't use table_list/field_list diff --git a/sql/sql_base.cc b/sql/sql_base.cc index 1dbb23226ef..723c4f2c50d 100644 --- a/sql/sql_base.cc +++ b/sql/sql_base.cc @@ -7925,6 +7925,11 @@ err: order, thus when we iterate over it, we are moving from the right to the left in the FROM clause. + NOTES + We can't run this many times as the first_name_resolution_table would + be different for subsequent runs when sub queries has been optimized + away. + RETURN TRUE Error FALSE OK @@ -7946,7 +7951,8 @@ static bool setup_natural_join_row_types(THD *thd, */ if (!context->select_lex->first_natural_join_processing) { - DBUG_PRINT("info", ("using cached store_top_level_join_columns")); + context->first_name_resolution_table= context->natural_join_first_table; + DBUG_PRINT("info", ("using cached setup_natural_join_row_types")); DBUG_RETURN(false); } context->select_lex->first_natural_join_processing= false; @@ -7989,6 +7995,11 @@ static bool setup_natural_join_row_types(THD *thd, DBUG_ASSERT(right_neighbor); context->first_name_resolution_table= right_neighbor->first_leaf_for_name_resolution(); + /* + This is only to ensure that first_name_resolution_table doesn't + change on re-execution + */ + context->natural_join_first_table= context->first_name_resolution_table; DBUG_RETURN (false); }