diff --git a/mysql-test/r/subselect.result b/mysql-test/r/subselect.result index 71755aac52d..9f64281185f 100644 --- a/mysql-test/r/subselect.result +++ b/mysql-test/r/subselect.result @@ -1569,3 +1569,13 @@ INSERT INTO t2 VALUES (100, 200, 'C'); SELECT DISTINCT COLC FROM t1 WHERE COLA = (SELECT COLA FROM t2 WHERE COLB = 200 AND COLC ='C' LIMIT 1); COLC DROP TABLE t1, t2; +CREATE TABLE t1 (a int(1)); +INSERT INTO t1 VALUES (1),(1),(1),(1),(1),(2),(3),(4),(5); +SELECT DISTINCT (SELECT a) FROM t1 LIMIT 100; +(SELECT a) +1 +2 +3 +4 +5 +DROP TABLE t1; diff --git a/mysql-test/t/subselect.test b/mysql-test/t/subselect.test index e53679b444e..2879a4d7177 100644 --- a/mysql-test/t/subselect.test +++ b/mysql-test/t/subselect.test @@ -1009,3 +1009,8 @@ INSERT INTO t1 VALUES (1,1,'1A3240'), (1,2,'4W2365'); INSERT INTO t2 VALUES (100, 200, 'C'); SELECT DISTINCT COLC FROM t1 WHERE COLA = (SELECT COLA FROM t2 WHERE COLB = 200 AND COLC ='C' LIMIT 1); DROP TABLE t1, t2; + +CREATE TABLE t1 (a int(1)); +INSERT INTO t1 VALUES (1),(1),(1),(1),(1),(2),(3),(4),(5); +SELECT DISTINCT (SELECT a) FROM t1 LIMIT 100; +DROP TABLE t1; diff --git a/sql/item_subselect.cc b/sql/item_subselect.cc index 7b401b50d4c..123362b917b 100644 --- a/sql/item_subselect.cc +++ b/sql/item_subselect.cc @@ -166,6 +166,12 @@ bool Item_subselect::const_item() const return const_item_cache; } +Item *Item_subselect::get_tmp_table_item(THD *thd) +{ + if (!with_sum_func && !const_item()) + return new Item_field(result_field); + return copy_or_same(thd); +} void Item_subselect::update_used_tables() { diff --git a/sql/item_subselect.h b/sql/item_subselect.h index 8444dc7bf66..84f5de5a622 100644 --- a/sql/item_subselect.h +++ b/sql/item_subselect.h @@ -89,6 +89,7 @@ public: bool const_item() const; inline table_map get_used_tables_cache() { return used_tables_cache; } inline bool get_const_item_cache() { return const_item_cache; } + Item *get_tmp_table_item(THD *thd); void update_used_tables(); void print(String *str); bool change_engine(subselect_engine *eng)