Commit a515802c authored by Igor Babaev's avatar Igor Babaev

Fixed LP bug #806097.

The value of THD::used tables should be re-evaluated after merges
of views and derived tables into the main query. 
Now it's done in the function SELECT_LEX::update_used_tables.
The re-evaluation of the 'used_table' bitmaps for the items
in HAVING, GROUP BY and ORDER BY clauses has been added as well.
 
parent 9e7495df
......@@ -900,3 +900,45 @@ id select_type table type possible_keys key key_len ref rows Extra
2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 2 Using where
DROP VIEW v1;
DROP TABLE t1,t2,t3;
#
# LP bug #806097: left join over a view + DISTINCT
#
CREATE TABLE t1 (a int, b int);
INSERT INTO t1 VALUES (252,6), (232,0), (174,232);
CREATE TABLE t2 (a int);
INSERT INTO t2 VALUES (232), (174);
CREATE TABLE t3 (c int);
INSERT INTO t3 VALUES (1), (2);
CREATE VIEW v1 AS SELECT t2.a FROM t3,t2;
SELECT v1.a FROM t1 LEFT JOIN v1 ON t1.b = 0;
a
NULL
232
174
232
174
NULL
SELECT DISTINCT t2.a FROM t1 LEFT JOIN (t3,t2) ON t1.b = 0;
a
NULL
232
174
EXPLAIN
SELECT DISTINCT t2.a FROM t1 LEFT JOIN (t3,t2) ON t1.b = 0;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 3 Using temporary
1 SIMPLE t3 ALL NULL NULL NULL NULL 2 Using where
1 SIMPLE t2 ALL NULL NULL NULL NULL 2
SELECT DISTINCT v1.a FROM t1 LEFT JOIN v1 ON t1.b = 0;
a
NULL
232
174
EXPLAIN
SELECT DISTINCT v1.a FROM t1 LEFT JOIN v1 ON t1.b = 0;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 3 Using temporary
1 SIMPLE t3 ALL NULL NULL NULL NULL 2 Using where
1 SIMPLE t2 ALL NULL NULL NULL NULL 2
DROP VIEW v1;
DROP TABLE t1,t2,t3;
......@@ -985,8 +985,8 @@ EXPLAIN EXTENDED SELECT 1 FROM
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 2 100.00
1 PRIMARY <derived2> ALL NULL NULL NULL NULL 4 100.00 Using join buffer (flat, BNL join)
2 DERIVED t1 ALL NULL NULL NULL NULL 2 100.00 Using temporary; Using filesort
2 DERIVED td ALL NULL NULL NULL NULL 2 100.00 Using join buffer (flat, BNL join)
2 DERIVED t1 ALL NULL NULL NULL NULL 2 100.00 Using temporary; Using filesort; Distinct
2 DERIVED td ALL NULL NULL NULL NULL 2 100.00 Distinct; Using join buffer (flat, BNL join)
Warnings:
Note 1003 select 1 AS `1` from (select distinct group_concat(`test`.`td`.`f1` separator ',') AS `GROUP_CONCAT(td.f1)` from `test`.`t1` join `test`.`t1` `td` group by `test`.`td`.`f1`) `d` join `test`.`t1`
SELECT 1 FROM
......
......@@ -503,3 +503,31 @@ SELECT * FROM v1 t
DROP VIEW v1;
DROP TABLE t1,t2,t3;
--echo #
--echo # LP bug #806097: left join over a view + DISTINCT
--echo #
CREATE TABLE t1 (a int, b int);
INSERT INTO t1 VALUES (252,6), (232,0), (174,232);
CREATE TABLE t2 (a int);
INSERT INTO t2 VALUES (232), (174);
CREATE TABLE t3 (c int);
INSERT INTO t3 VALUES (1), (2);
CREATE VIEW v1 AS SELECT t2.a FROM t3,t2;
SELECT v1.a FROM t1 LEFT JOIN v1 ON t1.b = 0;
SELECT DISTINCT t2.a FROM t1 LEFT JOIN (t3,t2) ON t1.b = 0;
EXPLAIN
SELECT DISTINCT t2.a FROM t1 LEFT JOIN (t3,t2) ON t1.b = 0;
SELECT DISTINCT v1.a FROM t1 LEFT JOIN v1 ON t1.b = 0;
EXPLAIN
SELECT DISTINCT v1.a FROM t1 LEFT JOIN v1 ON t1.b = 0;
DROP VIEW v1;
DROP TABLE t1,t2,t3;
......@@ -3501,6 +3501,33 @@ void SELECT_LEX::update_used_tables()
join->conds->update_used_tables();
join->conds->walk(&Item::eval_not_null_tables, 0, NULL);
}
if (join->having)
{
join->having->update_used_tables();
}
Item *item;
List_iterator_fast<Item> it(join->fields_list);
while ((item= it++))
{
item->update_used_tables();
join->thd->used_tables|= item->used_tables();
}
Item_outer_ref *ref;
List_iterator_fast<Item_outer_ref> ref_it(inner_refs_list);
while ((ref= ref_it++))
{
item= ref->outer_ref;
item->update_used_tables();
join->thd->used_tables|= item->used_tables();
}
for (ORDER *order= group_list.first; order; order= order->next)
(*order->item)->update_used_tables();
if (!master_unit()->is_union())
{
for (ORDER *order= order_list.first; order; order= order->next)
(*order->item)->update_used_tables();
}
}
......
......@@ -880,6 +880,8 @@ JOIN::optimize()
if (select_lex->first_cond_optimization)
{
if (!select_lex->outer_select())
thd->used_tables= 0;
//Do it only for the first execution
/* Merge all mergeable derived tables/views in this SELECT. */
if (select_lex->handle_derived(thd->lex, DT_MERGE))
......@@ -893,6 +895,8 @@ JOIN::optimize()
if (select_lex->first_cond_optimization)
{
if (!select_lex->outer_select())
thd->used_tables= 0;
/* dump_TABLE_LIST_graph(select_lex, select_lex->leaf_tables); */
if (convert_join_subqueries_to_semijoins(this))
DBUG_RETURN(1); /* purecov: inspected */
......
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