revision-id: d22ca57839047b051e92918e17da4acfade12636 (mariadb-10.1.41-106-gd22ca578390) parent(s): d930422e9e84aa1bc265963225dd29295203e9d3 author: Varun Gupta committer: Varun Gupta timestamp: 2019-12-03 19:15:51 +0530 message: MDEV-20922: Adding an order by changes the query results For Item_ref use real_item() when making the sort key. --- mysql-test/r/group_by.result | 16 ++++++++++++++++ mysql-test/t/group_by.test | 14 ++++++++++++++ sql/filesort.cc | 4 ++-- 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/mysql-test/r/group_by.result b/mysql-test/r/group_by.result index eb730a8c954..bde24afa6e3 100644 --- a/mysql-test/r/group_by.result +++ b/mysql-test/r/group_by.result @@ -2792,3 +2792,19 @@ SELECT 1 IN ( SELECT COUNT( DISTINCT f2 ) FROM t1 WHERE f1 <= 4 ); 1 IN ( SELECT COUNT( DISTINCT f2 ) FROM t1 WHERE f1 <= 4 ) 0 drop table t1; +# +# MDEV-20922: Adding an order by changes the query results +# +CREATE TABLE t1(a int, b int); +INSERT INTO t1 values (1, 100), (2, 200), (3, 100), (4, 200); +create view v1 as select a, b+1 as x from t1; +select x, count(distinct a) as y from v1 group by x order by y; +x y +101 2 +201 2 +select b+1 as x, count(distinct a) as y from t1 group by x order by y; +x y +101 2 +201 2 +drop view v1; +drop table t1; diff --git a/mysql-test/t/group_by.test b/mysql-test/t/group_by.test index 0401ad9780c..b3e12a93147 100644 --- a/mysql-test/t/group_by.test +++ b/mysql-test/t/group_by.test @@ -1915,3 +1915,17 @@ INSERT INTO t1 VALUES (0,'foo'),(1,'bar'); SELECT 1 IN ( SELECT COUNT( DISTINCT f2 ) FROM t1 WHERE f1 <= 4 ); drop table t1; +--echo # +--echo # MDEV-20922: Adding an order by changes the query results +--echo # + +CREATE TABLE t1(a int, b int); +INSERT INTO t1 values (1, 100), (2, 200), (3, 100), (4, 200); + +create view v1 as select a, b+1 as x from t1; + +select x, count(distinct a) as y from v1 group by x order by y; +select b+1 as x, count(distinct a) as y from t1 group by x order by y; + +drop view v1; +drop table t1; diff --git a/sql/filesort.cc b/sql/filesort.cc index bb3e73343ad..d461059a396 100644 --- a/sql/filesort.cc +++ b/sql/filesort.cc @@ -988,7 +988,7 @@ static void make_sortkey(Sort_param *param, } else { // Item - Item *item=sort_field->item; + Item *item=sort_field->item->real_item(); maybe_null= item->maybe_null; switch (sort_field->result_type) { case STRING_RESULT: @@ -1930,7 +1930,7 @@ sortlength(THD *thd, SORT_FIELD *sortorder, uint s_length, DBUG_ASSERT(0); break; } - if (sortorder->item->maybe_null) + if (sortorder->item->real_item()->maybe_null) length++; // Place for NULL marker } set_if_smaller(sortorder->length, thd->variables.max_sort_length);