Commit 1cb72d7e authored by unknown's avatar unknown

WL#2486 - natural and using join according to SQL:2003

- Corrected problem with N-way nested natural joins in PS mode.
- Code cleanup
- More asserts to check consistency of name resolution contexts
- Fixed potential memory leak of name resolution contexts


mysql-test/r/join.result:
  - Corrected problem with N-way nested natural joins in PS mode.
mysql-test/t/join.test:
  - Corrected problem with N-way nested natural joins in PS mode.
sql/item.h:
  - Fixed potential memory leak.
sql/sql_base.cc:
  - the local context of Item_fields that participate in TABLE_LIST::on_cond for
    natural joins is correctly set to the tables where the corresponding fields
    originate from.
  - removed unused variables
  - correct allocation of contexts
sql/sql_parse.cc:
  - correct allocation of contexts for JOIN ON conditions.
sql/table.cc:
  - added asserts to check the consistency of name resolution contexts
sql/table.h:
  - added asserts to check the consistency of name resolution contexts
parent bbf391cb
...@@ -121,6 +121,10 @@ id catid stateid countyid ...@@ -121,6 +121,10 @@ id catid stateid countyid
drop table t1, t2; drop table t1, t2;
create table t1 (a int primary key); create table t1 (a int primary key);
insert into t1 values(1),(2); insert into t1 values(1),(2);
select a from t1 as t1 left join t1 as t2 using (a) left join t1 as t3 using (a) left join t1 as t4 using (a) left join t1 as t5 using (a) left join t1 as t6 using (a) left join t1 as t7 using (a) left join t1 as t8 using (a) left join t1 as t9 using (a) left join t1 as t10 using (a) left join t1 as t11 using (a) left join t1 as t12 using (a) left join t1 as t13 using (a) left join t1 as t14 using (a) left join t1 as t15 using (a) left join t1 as t16 using (a) left join t1 as t17 using (a) left join t1 as t18 using (a) left join t1 as t19 using (a) left join t1 as t20 using (a) left join t1 as t21 using (a) left join t1 as t22 using (a) left join t1 as t23 using (a) left join t1 as t24 using (a) left join t1 as t25 using (a) left join t1 as t26 using (a) left join t1 as t27 using (a) left join t1 as t28 using (a) left join t1 as t29 using (a) left join t1 as t30 using (a) left join t1 as t31 using (a);
a
1
2
select a from t1 as t1 left join t1 as t2 using (a) left join t1 as t3 using (a) left join t1 as t4 using (a) left join t1 as t5 using (a) left join t1 as t6 using (a) left join t1 as t7 using (a) left join t1 as t8 using (a) left join t1 as t9 using (a) left join t1 as t10 using (a) left join t1 as t11 using (a) left join t1 as t12 using (a) left join t1 as t13 using (a) left join t1 as t14 using (a) left join t1 as t15 using (a) left join t1 as t16 using (a) left join t1 as t17 using (a) left join t1 as t18 using (a) left join t1 as t19 using (a) left join t1 as t20 using (a) left join t1 as t21 using (a) left join t1 as t22 using (a) left join t1 as t23 using (a) left join t1 as t24 using (a) left join t1 as t25 using (a) left join t1 as t26 using (a) left join t1 as t27 using (a) left join t1 as t28 using (a) left join t1 as t29 using (a) left join t1 as t30 using (a) left join t1 as t31 using (a) left join t1 as t32 using (a) left join t1 as t33 using (a) left join t1 as t34 using (a) left join t1 as t35 using (a) left join t1 as t36 using (a) left join t1 as t37 using (a) left join t1 as t38 using (a) left join t1 as t39 using (a) left join t1 as t40 using (a) left join t1 as t41 using (a) left join t1 as t42 using (a) left join t1 as t43 using (a) left join t1 as t44 using (a) left join t1 as t45 using (a) left join t1 as t46 using (a) left join t1 as t47 using (a) left join t1 as t48 using (a) left join t1 as t49 using (a) left join t1 as t50 using (a) left join t1 as t51 using (a) left join t1 as t52 using (a) left join t1 as t53 using (a) left join t1 as t54 using (a) left join t1 as t55 using (a) left join t1 as t56 using (a) left join t1 as t57 using (a) left join t1 as t58 using (a) left join t1 as t59 using (a) left join t1 as t60 using (a) left join t1 as t61 using (a) left join t1 as t62 using (a) left join t1 as t63 using (a) left join t1 as t64 using (a) left join t1 as t65 using (a); select a from t1 as t1 left join t1 as t2 using (a) left join t1 as t3 using (a) left join t1 as t4 using (a) left join t1 as t5 using (a) left join t1 as t6 using (a) left join t1 as t7 using (a) left join t1 as t8 using (a) left join t1 as t9 using (a) left join t1 as t10 using (a) left join t1 as t11 using (a) left join t1 as t12 using (a) left join t1 as t13 using (a) left join t1 as t14 using (a) left join t1 as t15 using (a) left join t1 as t16 using (a) left join t1 as t17 using (a) left join t1 as t18 using (a) left join t1 as t19 using (a) left join t1 as t20 using (a) left join t1 as t21 using (a) left join t1 as t22 using (a) left join t1 as t23 using (a) left join t1 as t24 using (a) left join t1 as t25 using (a) left join t1 as t26 using (a) left join t1 as t27 using (a) left join t1 as t28 using (a) left join t1 as t29 using (a) left join t1 as t30 using (a) left join t1 as t31 using (a) left join t1 as t32 using (a) left join t1 as t33 using (a) left join t1 as t34 using (a) left join t1 as t35 using (a) left join t1 as t36 using (a) left join t1 as t37 using (a) left join t1 as t38 using (a) left join t1 as t39 using (a) left join t1 as t40 using (a) left join t1 as t41 using (a) left join t1 as t42 using (a) left join t1 as t43 using (a) left join t1 as t44 using (a) left join t1 as t45 using (a) left join t1 as t46 using (a) left join t1 as t47 using (a) left join t1 as t48 using (a) left join t1 as t49 using (a) left join t1 as t50 using (a) left join t1 as t51 using (a) left join t1 as t52 using (a) left join t1 as t53 using (a) left join t1 as t54 using (a) left join t1 as t55 using (a) left join t1 as t56 using (a) left join t1 as t57 using (a) left join t1 as t58 using (a) left join t1 as t59 using (a) left join t1 as t60 using (a) left join t1 as t61 using (a) left join t1 as t62 using (a) left join t1 as t63 using (a) left join t1 as t64 using (a) left join t1 as t65 using (a);
ERROR HY000: Too many tables; MySQL can only use XX tables in a join ERROR HY000: Too many tables; MySQL can only use XX tables in a join
drop table t1; drop table t1;
......
...@@ -111,9 +111,7 @@ drop table t1, t2; ...@@ -111,9 +111,7 @@ drop table t1, t2;
create table t1 (a int primary key); create table t1 (a int primary key);
insert into t1 values(1),(2); insert into t1 values(1),(2);
# TODO: WL#2486 - the query fails in PS mode with error: select a from t1 as t1 left join t1 as t2 using (a) left join t1 as t3 using (a) left join t1 as t4 using (a) left join t1 as t5 using (a) left join t1 as t6 using (a) left join t1 as t7 using (a) left join t1 as t8 using (a) left join t1 as t9 using (a) left join t1 as t10 using (a) left join t1 as t11 using (a) left join t1 as t12 using (a) left join t1 as t13 using (a) left join t1 as t14 using (a) left join t1 as t15 using (a) left join t1 as t16 using (a) left join t1 as t17 using (a) left join t1 as t18 using (a) left join t1 as t19 using (a) left join t1 as t20 using (a) left join t1 as t21 using (a) left join t1 as t22 using (a) left join t1 as t23 using (a) left join t1 as t24 using (a) left join t1 as t25 using (a) left join t1 as t26 using (a) left join t1 as t27 using (a) left join t1 as t28 using (a) left join t1 as t29 using (a) left join t1 as t30 using (a) left join t1 as t31 using (a);
# Cross dependency found in OUTER JOIN; examine your ON conditions
#select a from t1 as t1 left join t1 as t2 using (a) left join t1 as t3 using (a) left join t1 as t4 using (a) left join t1 as t5 using (a) left join t1 as t6 using (a) left join t1 as t7 using (a) left join t1 as t8 using (a) left join t1 as t9 using (a) left join t1 as t10 using (a) left join t1 as t11 using (a) left join t1 as t12 using (a) left join t1 as t13 using (a) left join t1 as t14 using (a) left join t1 as t15 using (a) left join t1 as t16 using (a) left join t1 as t17 using (a) left join t1 as t18 using (a) left join t1 as t19 using (a) left join t1 as t20 using (a) left join t1 as t21 using (a) left join t1 as t22 using (a) left join t1 as t23 using (a) left join t1 as t24 using (a) left join t1 as t25 using (a) left join t1 as t26 using (a) left join t1 as t27 using (a) left join t1 as t28 using (a) left join t1 as t29 using (a) left join t1 as t30 using (a) left join t1 as t31 using (a);
--replace_result "31 tables" "XX tables" "61 tables" "XX tables" --replace_result "31 tables" "XX tables" "61 tables" "XX tables"
--error 1116 --error 1116
select a from t1 as t1 left join t1 as t2 using (a) left join t1 as t3 using (a) left join t1 as t4 using (a) left join t1 as t5 using (a) left join t1 as t6 using (a) left join t1 as t7 using (a) left join t1 as t8 using (a) left join t1 as t9 using (a) left join t1 as t10 using (a) left join t1 as t11 using (a) left join t1 as t12 using (a) left join t1 as t13 using (a) left join t1 as t14 using (a) left join t1 as t15 using (a) left join t1 as t16 using (a) left join t1 as t17 using (a) left join t1 as t18 using (a) left join t1 as t19 using (a) left join t1 as t20 using (a) left join t1 as t21 using (a) left join t1 as t22 using (a) left join t1 as t23 using (a) left join t1 as t24 using (a) left join t1 as t25 using (a) left join t1 as t26 using (a) left join t1 as t27 using (a) left join t1 as t28 using (a) left join t1 as t29 using (a) left join t1 as t30 using (a) left join t1 as t31 using (a) left join t1 as t32 using (a) left join t1 as t33 using (a) left join t1 as t34 using (a) left join t1 as t35 using (a) left join t1 as t36 using (a) left join t1 as t37 using (a) left join t1 as t38 using (a) left join t1 as t39 using (a) left join t1 as t40 using (a) left join t1 as t41 using (a) left join t1 as t42 using (a) left join t1 as t43 using (a) left join t1 as t44 using (a) left join t1 as t45 using (a) left join t1 as t46 using (a) left join t1 as t47 using (a) left join t1 as t48 using (a) left join t1 as t49 using (a) left join t1 as t50 using (a) left join t1 as t51 using (a) left join t1 as t52 using (a) left join t1 as t53 using (a) left join t1 as t54 using (a) left join t1 as t55 using (a) left join t1 as t56 using (a) left join t1 as t57 using (a) left join t1 as t58 using (a) left join t1 as t59 using (a) left join t1 as t60 using (a) left join t1 as t61 using (a) left join t1 as t62 using (a) left join t1 as t63 using (a) left join t1 as t64 using (a) left join t1 as t65 using (a); select a from t1 as t1 left join t1 as t2 using (a) left join t1 as t3 using (a) left join t1 as t4 using (a) left join t1 as t5 using (a) left join t1 as t6 using (a) left join t1 as t7 using (a) left join t1 as t8 using (a) left join t1 as t9 using (a) left join t1 as t10 using (a) left join t1 as t11 using (a) left join t1 as t12 using (a) left join t1 as t13 using (a) left join t1 as t14 using (a) left join t1 as t15 using (a) left join t1 as t16 using (a) left join t1 as t17 using (a) left join t1 as t18 using (a) left join t1 as t19 using (a) left join t1 as t20 using (a) left join t1 as t21 using (a) left join t1 as t22 using (a) left join t1 as t23 using (a) left join t1 as t24 using (a) left join t1 as t25 using (a) left join t1 as t26 using (a) left join t1 as t27 using (a) left join t1 as t28 using (a) left join t1 as t29 using (a) left join t1 as t30 using (a) left join t1 as t31 using (a) left join t1 as t32 using (a) left join t1 as t33 using (a) left join t1 as t34 using (a) left join t1 as t35 using (a) left join t1 as t36 using (a) left join t1 as t37 using (a) left join t1 as t38 using (a) left join t1 as t39 using (a) left join t1 as t40 using (a) left join t1 as t41 using (a) left join t1 as t42 using (a) left join t1 as t43 using (a) left join t1 as t44 using (a) left join t1 as t45 using (a) left join t1 as t46 using (a) left join t1 as t47 using (a) left join t1 as t48 using (a) left join t1 as t49 using (a) left join t1 as t50 using (a) left join t1 as t51 using (a) left join t1 as t52 using (a) left join t1 as t53 using (a) left join t1 as t54 using (a) left join t1 as t55 using (a) left join t1 as t56 using (a) left join t1 as t57 using (a) left join t1 as t58 using (a) left join t1 as t59 using (a) left join t1 as t60 using (a) left join t1 as t61 using (a) left join t1 as t62 using (a) left join t1 as t63 using (a) left join t1 as t64 using (a) left join t1 as t65 using (a);
......
...@@ -237,7 +237,7 @@ void view_error_processor(THD *thd, void *data); ...@@ -237,7 +237,7 @@ void view_error_processor(THD *thd, void *data);
structure before and after INSERT/CREATE and its SELECT to make correct structure before and after INSERT/CREATE and its SELECT to make correct
field name resolution. field name resolution.
*/ */
struct Name_resolution_context struct Name_resolution_context: Sql_alloc
{ {
/* /*
The name resolution context to search in when an Item cannot be The name resolution context to search in when an Item cannot be
......
...@@ -2688,6 +2688,7 @@ find_field_in_natural_join(THD *thd, TABLE_LIST *table_ref, const char *name, ...@@ -2688,6 +2688,7 @@ find_field_in_natural_join(THD *thd, TABLE_LIST *table_ref, const char *name,
{ {
/* This is a base table. */ /* This is a base table. */
DBUG_ASSERT(nj_col->view_field == NULL); DBUG_ASSERT(nj_col->view_field == NULL);
DBUG_ASSERT(nj_col->table_ref->table == nj_col->table_field->table);
found_field= nj_col->table_field; found_field= nj_col->table_field;
update_field_dependencies(thd, found_field, nj_col->table_ref->table); update_field_dependencies(thd, found_field, nj_col->table_ref->table);
} }
...@@ -3366,9 +3367,7 @@ static bool ...@@ -3366,9 +3367,7 @@ static bool
set_new_item_local_context(THD *thd, Item_ident *item, TABLE_LIST *table_ref) set_new_item_local_context(THD *thd, Item_ident *item, TABLE_LIST *table_ref)
{ {
Name_resolution_context *context; Name_resolution_context *context;
if (!(context= new Name_resolution_context))
if (!(context= (Name_resolution_context*)
thd->calloc(sizeof(Name_resolution_context))))
return TRUE; return TRUE;
context->init(); context->init();
context->first_name_resolution_table= context->first_name_resolution_table=
...@@ -3509,7 +3508,6 @@ mark_common_columns(THD *thd, TABLE_LIST *table_ref_1, TABLE_LIST *table_ref_2, ...@@ -3509,7 +3508,6 @@ mark_common_columns(THD *thd, TABLE_LIST *table_ref_1, TABLE_LIST *table_ref_2,
Field *field_1= nj_col_1->field(); Field *field_1= nj_col_1->field();
Field *field_2= nj_col_2->field(); Field *field_2= nj_col_2->field();
Item_ident *item_ident_1, *item_ident_2; Item_ident *item_ident_1, *item_ident_2;
Name_resolution_context *context_1, *context_2;
Item_func_eq *eq_cond; Item_func_eq *eq_cond;
DBUG_PRINT("info", ("new equi-join condition: %s.%s = %s.%s", DBUG_PRINT("info", ("new equi-join condition: %s.%s = %s.%s",
...@@ -3545,8 +3543,8 @@ mark_common_columns(THD *thd, TABLE_LIST *table_ref_1, TABLE_LIST *table_ref_2, ...@@ -3545,8 +3543,8 @@ mark_common_columns(THD *thd, TABLE_LIST *table_ref_1, TABLE_LIST *table_ref_2,
resolution of these items, and to enable proper name resolution of resolution of these items, and to enable proper name resolution of
the items during the execute phase of PS. the items during the execute phase of PS.
*/ */
if (set_new_item_local_context(thd, item_ident_1, table_ref_1) || if (set_new_item_local_context(thd, item_ident_1, nj_col_1->table_ref) ||
set_new_item_local_context(thd, item_ident_2, table_ref_2)) set_new_item_local_context(thd, item_ident_2, nj_col_2->table_ref))
goto err; goto err;
if (!(eq_cond= new Item_func_eq(item_ident_1, item_ident_2))) if (!(eq_cond= new Item_func_eq(item_ident_1, item_ident_2)))
...@@ -4336,7 +4334,6 @@ insert_fields(THD *thd, Name_resolution_context *context, const char *db_name, ...@@ -4336,7 +4334,6 @@ insert_fields(THD *thd, Name_resolution_context *context, const char *db_name,
continue; continue;
} }
bool view;
#ifndef NO_EMBEDDED_ACCESS_CHECKS #ifndef NO_EMBEDDED_ACCESS_CHECKS
/* Ensure that we have access rights to all fields to be inserted. */ /* Ensure that we have access rights to all fields to be inserted. */
if (!((table && (table->grant.privilege & SELECT_ACL) || if (!((table && (table->grant.privilege & SELECT_ACL) ||
......
...@@ -6415,8 +6415,7 @@ Name_resolution_context * ...@@ -6415,8 +6415,7 @@ Name_resolution_context *
make_join_on_context(THD *thd, TABLE_LIST *left_op, TABLE_LIST *right_op) make_join_on_context(THD *thd, TABLE_LIST *left_op, TABLE_LIST *right_op)
{ {
Name_resolution_context *on_context; Name_resolution_context *on_context;
if (!(on_context= (Name_resolution_context*) if (!(on_context= new Name_resolution_context))
thd->calloc(sizeof(Name_resolution_context))))
return NULL; return NULL;
on_context->init(); on_context->init();
on_context->first_name_resolution_table= on_context->first_name_resolution_table=
......
...@@ -2296,6 +2296,7 @@ Natural_join_column::Natural_join_column(Field_translator *field_param, ...@@ -2296,6 +2296,7 @@ Natural_join_column::Natural_join_column(Field_translator *field_param,
Natural_join_column::Natural_join_column(Field *field_param, Natural_join_column::Natural_join_column(Field *field_param,
TABLE_LIST *tab) TABLE_LIST *tab)
{ {
DBUG_ASSERT(tab->table == field_param->table);
table_field= field_param; table_field= field_param;
view_field= NULL; view_field= NULL;
table_ref= tab; table_ref= tab;
...@@ -2514,6 +2515,18 @@ void Field_iterator_natural_join::set(TABLE_LIST *table_ref) ...@@ -2514,6 +2515,18 @@ void Field_iterator_natural_join::set(TABLE_LIST *table_ref)
} }
void Field_iterator_natural_join::next()
{
cur_column_ref= (*column_ref_it)++;
DBUG_ASSERT(cur_column_ref ?
(cur_column_ref->table_field ?
cur_column_ref->table_ref->table ==
cur_column_ref->table_field->table :
TRUE) :
TRUE);
}
void Field_iterator_table_ref::set_field_iterator() void Field_iterator_table_ref::set_field_iterator()
{ {
DBUG_ENTER("Field_iterator_table_ref::set_field_iterator"); DBUG_ENTER("Field_iterator_table_ref::set_field_iterator");
...@@ -2660,18 +2673,31 @@ Field_iterator_table_ref::get_or_create_column_ref(THD *thd, bool *is_created) ...@@ -2660,18 +2673,31 @@ Field_iterator_table_ref::get_or_create_column_ref(THD *thd, bool *is_created)
*is_created= TRUE; *is_created= TRUE;
if (field_it == &table_field_it) if (field_it == &table_field_it)
return new Natural_join_column(table_field_it.field(), table_ref); {
if (field_it == &view_field_it) /* The field belongs to a stored table. */
return new Natural_join_column(view_field_it.field_translator(), Field *field= table_field_it.field();
table_ref); nj_col= new Natural_join_column(field, table_ref);
}
/* else if (field_it == &view_field_it)
This is NATURAL join, we already have created a column reference, {
so just return it. /* The field belongs to a merge view or information schema table. */
*/ Field_translator *translated_field= view_field_it.field_translator();
*is_created= FALSE; nj_col= new Natural_join_column(translated_field, table_ref);
nj_col= natural_join_it.column_ref(); }
DBUG_ASSERT(nj_col); else
{
/*
The field belongs to a NATURAL join, therefore the column reference was
already created via one of the two constructor calls above. In this case
we just return the already created column reference.
*/
*is_created= FALSE;
nj_col= natural_join_it.column_ref();
DBUG_ASSERT(nj_col);
}
DBUG_ASSERT(nj_col->table_field ?
nj_col->table_ref->table == nj_col->table_field->table :
TRUE);
return nj_col; return nj_col;
} }
......
...@@ -698,7 +698,7 @@ public: ...@@ -698,7 +698,7 @@ public:
Field_iterator_natural_join() :column_ref_it(NULL), cur_column_ref(NULL) {} Field_iterator_natural_join() :column_ref_it(NULL), cur_column_ref(NULL) {}
~Field_iterator_natural_join() { delete column_ref_it; } ~Field_iterator_natural_join() { delete column_ref_it; }
void set(TABLE_LIST *table); void set(TABLE_LIST *table);
void next() { cur_column_ref= (*column_ref_it)++; } void next();
bool end_of_fields() { return !cur_column_ref; } bool end_of_fields() { return !cur_column_ref; }
const char *name() { return cur_column_ref->name(); } const char *name() { return cur_column_ref->name(); }
Item *create_item(THD *thd) { return cur_column_ref->create_item(thd); } Item *create_item(THD *thd) { return cur_column_ref->create_item(thd); }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment