summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIgor Babaev <igor@askmonty.org>2017-02-15 22:41:45 -0800
committerIgor Babaev <igor@askmonty.org>2017-02-15 22:41:45 -0800
commit37925c6ccc61bd4d5af4956dbd877c69983c09f3 (patch)
treed565559a0e1b610bd0444c792a73971cd6b87ad5
parente688d8144411e38253b7ec4e13f67a989da8bb76 (diff)
downloadmariadb-git-37925c6ccc61bd4d5af4956dbd877c69983c09f3.tar.gz
Fixed bug mdev-9924.
Supported queries with window functions when GROUP BY could be optimized away.
-rw-r--r--mysql-test/r/win.result21
-rw-r--r--mysql-test/t/win.test16
-rw-r--r--sql/sql_select.cc5
3 files changed, 41 insertions, 1 deletions
diff --git a/mysql-test/r/win.result b/mysql-test/r/win.result
index e1d03739b3d..f0a9aea0419 100644
--- a/mysql-test/r/win.result
+++ b/mysql-test/r/win.result
@@ -3075,3 +3075,24 @@ select count(distinct s) from (select sum(d) as s from t group by a) Z where s >
count(distinct s)
1
drop table t;
+#
+# MDEV-9924: window function in query with group by optimized away
+#
+create table t1 (i int);
+insert into t1 values (2),(3),(1);
+select row_number() over () from t1 group by 1+2;
+row_number() over ()
+1
+select max(i), row_number() over () from t1 group by 1+2;
+max(i) row_number() over ()
+3 1
+select rank() over (order by max(i)) from t1 group by 1+2;
+rank() over (order by max(i))
+1
+select i, row_number() over () from t1 group by 1+2;
+i row_number() over ()
+2 1
+select i, rank() over (order by i) rnk from t1 group by 1+2;
+i rnk
+2 1
+drop table t1;
diff --git a/mysql-test/t/win.test b/mysql-test/t/win.test
index 0661daba7ef..ec083733f77 100644
--- a/mysql-test/t/win.test
+++ b/mysql-test/t/win.test
@@ -1793,3 +1793,19 @@ select count(distinct s) from (select sum(d) over(partition by a) as s from t) Z
select count(distinct s) from (select sum(d) as s from t group by a) Z where s > 0;
drop table t;
+
+--echo #
+--echo # MDEV-9924: window function in query with group by optimized away
+--echo #
+
+create table t1 (i int);
+insert into t1 values (2),(3),(1);
+
+select row_number() over () from t1 group by 1+2;
+select max(i), row_number() over () from t1 group by 1+2;
+select rank() over (order by max(i)) from t1 group by 1+2;
+
+select i, row_number() over () from t1 group by 1+2;
+select i, rank() over (order by i) rnk from t1 group by 1+2;
+
+drop table t1;
diff --git a/sql/sql_select.cc b/sql/sql_select.cc
index ea0f4891de1..802e0d27e88 100644
--- a/sql/sql_select.cc
+++ b/sql/sql_select.cc
@@ -2226,6 +2226,9 @@ bool JOIN::make_aggr_tables_info()
sort_and_group_aggr_tab= NULL;
+ if (group_optimized_away)
+ implicit_grouping= true;
+
bool implicit_grouping_with_window_funcs= implicit_grouping &&
select_lex->have_window_funcs();
@@ -2574,7 +2577,7 @@ bool JOIN::make_aggr_tables_info()
tmp_table_param.copy_field= tmp_table_param.copy_field_end=0;
first_record= sort_and_group=0;
- if (!group_optimized_away)
+ if (!group_optimized_away || implicit_grouping_with_window_funcs)
{
group= false;
}