Commit 85b63c1e authored by Sergey Petrunya's avatar Sergey Petrunya

MWL#17: Table elimination

Continue with addressing review feedback part two: 
- rename enum members
- add checking for out of memory errors on allocation
parent ef67ab4d
...@@ -111,11 +111,11 @@ class Module_dep : public Sql_alloc ...@@ -111,11 +111,11 @@ class Module_dep : public Sql_alloc
{ {
public: public:
enum { enum {
FD_EXPRESSION, MODULE_EXPRESSION,
FD_MULTI_EQUALITY, MODULE_MULTI_EQUALITY,
FD_UNIQUE_KEY, MODULE_UNIQUE_KEY,
FD_TABLE, MODULE_TABLE,
FD_OUTER_JOIN MODULE_OUTER_JOIN
} type; /* Type of the object */ } type; /* Type of the object */
/* /*
...@@ -156,7 +156,7 @@ public: ...@@ -156,7 +156,7 @@ public:
Key_module(Table_value *table_arg, uint keyno_arg, uint n_parts_arg) : Key_module(Table_value *table_arg, uint keyno_arg, uint n_parts_arg) :
table(table_arg), keyno(keyno_arg), next_table_key(NULL) table(table_arg), keyno(keyno_arg), next_table_key(NULL)
{ {
type= Module_dep::FD_UNIQUE_KEY; type= Module_dep::MODULE_UNIQUE_KEY;
unknown_args= n_parts_arg; unknown_args= n_parts_arg;
} }
Table_value *table; /* Table this key is from */ Table_value *table; /* Table this key is from */
...@@ -178,7 +178,7 @@ public: ...@@ -178,7 +178,7 @@ public:
Outer_join_module(TABLE_LIST *table_list_arg, uint n_children) : Outer_join_module(TABLE_LIST *table_list_arg, uint n_children) :
table_list(table_list_arg), parent(NULL) table_list(table_list_arg), parent(NULL)
{ {
type= Module_dep::FD_OUTER_JOIN; type= Module_dep::MODULE_OUTER_JOIN;
unknown_args= n_children; unknown_args= n_children;
} }
/* /*
...@@ -205,7 +205,7 @@ public: ...@@ -205,7 +205,7 @@ public:
class Table_elimination class Table_elimination
{ {
public: public:
Table_elimination(JOIN *join_arg) : join(join_arg) Table_elimination(JOIN *join_arg) : join(join_arg), n_outer_joins(0)
{ {
bzero(table_deps, sizeof(table_deps)); bzero(table_deps, sizeof(table_deps));
} }
...@@ -220,6 +220,7 @@ public: ...@@ -220,6 +220,7 @@ public:
/* Outer joins that are candidates for elimination */ /* Outer joins that are candidates for elimination */
List<Outer_join_module> oj_deps; List<Outer_join_module> oj_deps;
uint n_outer_joins;
/* Bitmap of how expressions depend on bits */ /* Bitmap of how expressions depend on bits */
MY_BITMAP expr_deps; MY_BITMAP expr_deps;
...@@ -630,22 +631,25 @@ void add_eq_dep(Table_elimination *te, Equality_module **eq_dep, ...@@ -630,22 +631,25 @@ void add_eq_dep(Table_elimination *te, Equality_module **eq_dep,
DBUG_ASSERT(eq_func); DBUG_ASSERT(eq_func);
/* Store possible eq field */ /* Store possible eq field */
(*eq_dep)->type= Module_dep::FD_EXPRESSION; //psergey-todo; (*eq_dep)->type= Module_dep::MODULE_EXPRESSION; //psergey-todo;
(*eq_dep)->field= get_field_value(te, field); (*eq_dep)->field= get_field_value(te, field);
(*eq_dep)->val= *value; (*eq_dep)->val= *value;
(*eq_dep)->level= and_level; (*eq_dep)->level= and_level;
(*eq_dep)++; (*eq_dep)++;
} }
/* /*
Get a Table_value object for the given table, creating it if necessary. Get a Table_value object for the given table, creating it if necessary.
*/ */
static Table_value *get_table_value(Table_elimination *te, TABLE *table) static Table_value *get_table_value(Table_elimination *te, TABLE *table)
{ {
Table_value *tbl_dep= new Table_value(table); Table_value *tbl_dep;
Key_module **key_list= &(tbl_dep->keys); if (!(tbl_dep= new Table_value(table)))
return NULL;
Key_module **key_list= &(tbl_dep->keys);
/* Add dependencies for unique keys */ /* Add dependencies for unique keys */
for (uint i=0; i < table->s->keys; i++) for (uint i=0; i < table->s->keys; i++)
{ {
...@@ -657,7 +661,7 @@ static Table_value *get_table_value(Table_elimination *te, TABLE *table) ...@@ -657,7 +661,7 @@ static Table_value *get_table_value(Table_elimination *te, TABLE *table)
key_list= &(key_dep->next_table_key); key_list= &(key_dep->next_table_key);
} }
} }
return te->table_deps[table->tablenr] = tbl_dep; return te->table_deps[table->tablenr]= tbl_dep;
} }
...@@ -672,7 +676,10 @@ static Field_value *get_field_value(Table_elimination *te, Field *field) ...@@ -672,7 +676,10 @@ static Field_value *get_field_value(Table_elimination *te, Field *field)
/* First, get the table*/ /* First, get the table*/
if (!(tbl_dep= te->table_deps[table->tablenr])) if (!(tbl_dep= te->table_deps[table->tablenr]))
tbl_dep= get_table_value(te, table); {
if (!(tbl_dep= get_table_value(te, table)))
return NULL;
}
/* Try finding the field in field list */ /* Try finding the field in field list */
Field_value **pfield= &(tbl_dep->fields); Field_value **pfield= &(tbl_dep->fields);
...@@ -702,10 +709,12 @@ static Field_value *get_field_value(Table_elimination *te, Field *field) ...@@ -702,10 +709,12 @@ static Field_value *get_field_value(Table_elimination *te, Field *field)
static static
Outer_join_module *get_outer_join_dep(Table_elimination *te, Outer_join_module *get_outer_join_dep(Table_elimination *te,
TABLE_LIST *outer_join, table_map deps_map) TABLE_LIST *outer_join,
table_map deps_map)
{ {
Outer_join_module *oj_dep; Outer_join_module *oj_dep;
oj_dep= new Outer_join_module(outer_join, my_count_bits(deps_map)); oj_dep= new Outer_join_module(outer_join, my_count_bits(deps_map));
te->n_outer_joins++;
/* /*
Collect a bitmap fo tables that we depend on, and also set parent pointer Collect a bitmap fo tables that we depend on, and also set parent pointer
...@@ -734,7 +743,8 @@ Outer_join_module *get_outer_join_dep(Table_elimination *te, ...@@ -734,7 +743,8 @@ Outer_join_module *get_outer_join_dep(Table_elimination *te,
} }
} }
DBUG_ASSERT(table); DBUG_ASSERT(table);
table_dep= get_table_value(te, table); if (!(table_dep= get_table_value(te, table)))
return NULL;
} }
/* /*
...@@ -781,7 +791,7 @@ Outer_join_module *get_outer_join_dep(Table_elimination *te, ...@@ -781,7 +791,7 @@ Outer_join_module *get_outer_join_dep(Table_elimination *te,
. .
*/ */
static void static bool
collect_funcdeps_for_join_list(Table_elimination *te, collect_funcdeps_for_join_list(Table_elimination *te,
List<TABLE_LIST> *join_list, List<TABLE_LIST> *join_list,
bool build_eq_deps, bool build_eq_deps,
...@@ -808,11 +818,12 @@ collect_funcdeps_for_join_list(Table_elimination *te, ...@@ -808,11 +818,12 @@ collect_funcdeps_for_join_list(Table_elimination *te,
eliminable= !(cur_map & outside_used_tables); eliminable= !(cur_map & outside_used_tables);
if (eliminable) if (eliminable)
*eliminable_tables |= cur_map; *eliminable_tables |= cur_map;
collect_funcdeps_for_join_list(te, &tbl->nested_join->join_list, if (collect_funcdeps_for_join_list(te, &tbl->nested_join->join_list,
eliminable || build_eq_deps, eliminable || build_eq_deps,
outside_used_tables, outside_used_tables,
eliminable_tables, eliminable_tables,
eq_dep); eq_dep))
return TRUE;
} }
else else
{ {
...@@ -830,13 +841,13 @@ collect_funcdeps_for_join_list(Table_elimination *te, ...@@ -830,13 +841,13 @@ collect_funcdeps_for_join_list(Table_elimination *te,
*eliminable_tables); *eliminable_tables);
} }
if (eliminable) if (eliminable && get_outer_join_dep(te, tbl, cur_map))
te->oj_deps.push_back(get_outer_join_dep(te, tbl, cur_map)); return TRUE;
tables_used_on_left |= tbl->on_expr->used_tables(); tables_used_on_left |= tbl->on_expr->used_tables();
} }
} }
return; return FALSE;
} }
...@@ -1053,16 +1064,18 @@ void eliminate_tables(JOIN *join) ...@@ -1053,16 +1064,18 @@ void eliminate_tables(JOIN *join)
DBUG_VOID_RETURN; DBUG_VOID_RETURN;
Equality_module *eq_deps_end= te.equality_deps; Equality_module *eq_deps_end= te.equality_deps;
table_map eliminable_tables= 0; table_map eliminable_tables= 0;
collect_funcdeps_for_join_list(&te, join->join_list, if (collect_funcdeps_for_join_list(&te, join->join_list,
FALSE, FALSE,
used_tables, used_tables,
&eliminable_tables, &eliminable_tables,
&eq_deps_end); &eq_deps_end))
DBUG_VOID_RETURN;
te.n_equality_deps= eq_deps_end - te.equality_deps; te.n_equality_deps= eq_deps_end - te.equality_deps;
Module_dep *bound_modules; Module_dep *bound_modules;
//Value_dep *bound_values; //Value_dep *bound_values;
setup_equality_deps(&te, &bound_modules); if (setup_equality_deps(&te, &bound_modules))
DBUG_VOID_RETURN;
run_elimination_wave(&te, bound_modules); run_elimination_wave(&te, bound_modules);
} }
...@@ -1108,7 +1121,7 @@ void run_elimination_wave(Table_elimination *te, Module_dep *bound_modules) ...@@ -1108,7 +1121,7 @@ void run_elimination_wave(Table_elimination *te, Module_dep *bound_modules)
{ {
switch (bound_modules->type) switch (bound_modules->type)
{ {
case Module_dep::FD_EXPRESSION: case Module_dep::MODULE_EXPRESSION:
{ {
/* It's a field=expr and we got to know the expr, so we know the field */ /* It's a field=expr and we got to know the expr, so we know the field */
Equality_module *eq_dep= (Equality_module*)bound_modules; Equality_module *eq_dep= (Equality_module*)bound_modules;
...@@ -1121,7 +1134,7 @@ void run_elimination_wave(Table_elimination *te, Module_dep *bound_modules) ...@@ -1121,7 +1134,7 @@ void run_elimination_wave(Table_elimination *te, Module_dep *bound_modules)
} }
break; break;
} }
case Module_dep::FD_UNIQUE_KEY: case Module_dep::MODULE_UNIQUE_KEY:
{ {
/* Unique key is known means the table is known */ /* Unique key is known means the table is known */
Table_value *table_dep=((Key_module*)bound_modules)->table; Table_value *table_dep=((Key_module*)bound_modules)->table;
...@@ -1134,13 +1147,13 @@ void run_elimination_wave(Table_elimination *te, Module_dep *bound_modules) ...@@ -1134,13 +1147,13 @@ void run_elimination_wave(Table_elimination *te, Module_dep *bound_modules)
} }
break; break;
} }
case Module_dep::FD_OUTER_JOIN: case Module_dep::MODULE_OUTER_JOIN:
{ {
Outer_join_module *outer_join_dep= (Outer_join_module*)bound_modules; Outer_join_module *outer_join_dep= (Outer_join_module*)bound_modules;
mark_as_eliminated(te->join, outer_join_dep->table_list); mark_as_eliminated(te->join, outer_join_dep->table_list);
break; break;
} }
case Module_dep::FD_MULTI_EQUALITY: case Module_dep::MODULE_MULTI_EQUALITY:
default: default:
DBUG_ASSERT(0); DBUG_ASSERT(0);
} }
......
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