Skip to content

Commit

Permalink
Fixed bug mdev-9028.
Browse files Browse the repository at this point in the history
This patch is actually a complement for the fix of bug mdev-6892.
The procedure create_tmp_table() now must take into account
Item_direct_refs that wrap up constant fields of derived tables/views
that are used as inner tables in outer join operations.
  • Loading branch information
igorbabaev committed Feb 17, 2017
1 parent b70cd26 commit f49375f
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
23 changes: 23 additions & 0 deletions mysql-test/r/derived.result
Expand Up @@ -988,4 +988,27 @@ David Yes 210
Edward Yes 150
DROP TABLE example1463;
set sql_mode= @save_sql_mode;
#
# MDEV-9028: SELECT DISTINCT constant column of derived table
# used as the second operand of LEFT JOIN
#
create table t1 (id int, data varchar(255));
insert into t1 values (1,'yes'),(2,'yes');
select distinct t1.id, tt.id, tt.data
from t1
left join
(select t1.id, 'yes' as data from t1) as tt
on t1.id = tt.id;
id id data
1 1 yes
2 2 yes
select distinct t1.id, tt.id, tt.data
from t1
left join
(select t1.id, 'yes' as data from t1 where id > 1) as tt
on t1.id = tt.id;
id id data
2 2 yes
1 NULL NULL
drop table t1;
# end of 5.5
23 changes: 23 additions & 0 deletions mysql-test/t/derived.test
Expand Up @@ -842,4 +842,27 @@ SELECT Customer, Success, SUM(OrderSize)
DROP TABLE example1463;
set sql_mode= @save_sql_mode;

--echo #
--echo # MDEV-9028: SELECT DISTINCT constant column of derived table
--echo # used as the second operand of LEFT JOIN
--echo #

create table t1 (id int, data varchar(255));
insert into t1 values (1,'yes'),(2,'yes');

select distinct t1.id, tt.id, tt.data
from t1
left join
(select t1.id, 'yes' as data from t1) as tt
on t1.id = tt.id;

select distinct t1.id, tt.id, tt.data
from t1
left join
(select t1.id, 'yes' as data from t1 where id > 1) as tt
on t1.id = tt.id;

drop table t1;


--echo # end of 5.5
4 changes: 3 additions & 1 deletion sql/sql_select.cc
Expand Up @@ -14627,7 +14627,9 @@ static Field *create_tmp_field_from_item(THD *thd, Item *item, TABLE *table,
if (new_field)
new_field->init(table);

if (copy_func && item->real_item()->is_result_field())
if (copy_func &&
(item->is_result_field() ||
(item->real_item()->is_result_field())))
*((*copy_func)++) = item; // Save for copy_funcs
if (modify_item)
item->set_result_field(new_field);
Expand Down

0 comments on commit f49375f

Please sign in to comment.