diff options
author | Sergei Golubchik <sergii@pisem.net> | 2014-02-03 15:22:39 +0100 |
---|---|---|
committer | Sergei Golubchik <sergii@pisem.net> | 2014-02-03 15:22:39 +0100 |
commit | 72c20282db820b0b0818aea160a485bdca897eec (patch) | |
tree | 3089e022d958990fc0a405a38ba43ae00c87103c /sql/item.cc | |
parent | 5e1d5d9bc0bf9ea776bffe6c4914a84be920c0b2 (diff) | |
parent | 2acc01b3cfa27074f93016b893cda20fa0a3497f (diff) | |
download | mariadb-git-72c20282db820b0b0818aea160a485bdca897eec.tar.gz |
10.0-base merge
Diffstat (limited to 'sql/item.cc')
-rw-r--r-- | sql/item.cc | 54 |
1 files changed, 42 insertions, 12 deletions
diff --git a/sql/item.cc b/sql/item.cc index 5bc9d5816eb..1295902c1cd 100644 --- a/sql/item.cc +++ b/sql/item.cc @@ -4698,6 +4698,12 @@ bool is_outer_table(TABLE_LIST *table, SELECT_LEX *select) /** Resolve the name of an outer select column reference. + @param[in] thd current thread + @param[in,out] from_field found field reference or (Field*)not_found_field + @param[in,out] reference view column if this item was resolved to a + view column + + @description The method resolves the column reference represented by 'this' as a column present in outer selects that contain current select. @@ -4707,10 +4713,16 @@ bool is_outer_table(TABLE_LIST *table, SELECT_LEX *select) current select as dependent. The found reference of field should be provided in 'from_field'. - @param[in] thd current thread - @param[in,out] from_field found field reference or (Field*)not_found_field - @param[in,out] reference view column if this item was resolved to a - view column + The cache is critical for prepared statements of type: + + SELECT a FROM (SELECT a FROM test.t1) AS s1 NATURAL JOIN t2 AS s2; + + This is internally converted to a join similar to + + SELECT a FROM t1 AS s1,t2 AS s2 WHERE t2.a=t1.a; + + Without the cache, we would on re-prepare not know if 'a' did match + s1.a or s2.a. @note This is the inner loop of Item_field::fix_fields: @@ -4740,7 +4752,12 @@ Item_field::fix_outer_field(THD *thd, Field **from_field, Item **reference) enum_parsing_place place= NO_MATTER; bool field_found= (*from_field != not_found_field); bool upward_lookup= FALSE; + TABLE_LIST *table_list; + /* Calulate the TABLE_LIST for the table */ + table_list= (cached_table ? cached_table : + field_found && (*from_field) != view_ref_found ? + (*from_field)->table->pos_in_table_list : 0); /* If there are outer contexts (outer selects, but current select is not derived table or view) try to resolve this reference in the @@ -4759,6 +4776,15 @@ Item_field::fix_outer_field(THD *thd, Field **from_field, Item **reference) if (current_sel->master_unit()->first_select()->linkage != DERIVED_TABLE_TYPE) outer_context= context->outer_context; + + /* + This assert is to ensure we have an outer contex when *from_field + is set. + If this would not be the case, we would assert in mark_as_dependent + as last_checked_countex == context + */ + DBUG_ASSERT(outer_context || !*from_field || + *from_field == not_found_field); for (; outer_context; outer_context= outer_context->outer_context) @@ -4775,7 +4801,7 @@ Item_field::fix_outer_field(THD *thd, Field **from_field, Item **reference) to find_field_in_tables(). Only need to find appropriate context. */ if (field_found && outer_context->select_lex != - cached_table->select_lex) + table_list->select_lex) continue; /* In case of a view, find_field_in_tables() writes the pointer to @@ -4974,9 +5000,9 @@ Item_field::fix_outer_field(THD *thd, Field **from_field, Item **reference) if (last_checked_context->select_lex->having_fix_field) { Item_ref *rf; - rf= new Item_ref(context, - (cached_table->db[0] ? cached_table->db : 0), - (char*) cached_table->alias, (char*) field_name); + rf= new Item_ref(context, (*from_field)->table->s->db.str, + (*from_field)->table->alias.c_ptr(), + (char*) field_name); if (!rf) return -1; thd->change_item_tree(reference, rf); @@ -5047,6 +5073,7 @@ bool Item_field::fix_fields(THD *thd, Item **reference) if (!field) // If field is not checked { + TABLE_LIST *table_list; /* In case of view, find_field_in_tables() write pointer to view field expression to 'reference', i.e. it substitute that expression instead @@ -5132,11 +5159,14 @@ bool Item_field::fix_fields(THD *thd, Item **reference) else if (!from_field) goto error; - if (!outer_fixed && cached_table && cached_table->select_lex && + table_list= (cached_table ? cached_table : + from_field != view_ref_found ? + from_field->table->pos_in_table_list : 0); + if (!outer_fixed && table_list && table_list->select_lex && context->select_lex && - cached_table->select_lex != context->select_lex && - !context->select_lex->is_merged_child_of(cached_table->select_lex) && - is_outer_table(cached_table, context->select_lex)) + table_list->select_lex != context->select_lex && + !context->select_lex->is_merged_child_of(table_list->select_lex) && + is_outer_table(table_list, context->select_lex)) { int ret; if ((ret= fix_outer_field(thd, &from_field, reference)) < 0) |