summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIgor Babaev <igor@askmonty.org>2017-02-03 15:50:25 -0800
committerIgor Babaev <igor@askmonty.org>2017-02-03 15:50:25 -0800
commit20aae56efa5aba90893e602ea87af2f14d161b10 (patch)
tree4bc7a24424484ece548b4c1220cc6becd36b7c47
parentbc12d993d7bc94a9533028a258afc7e4ceb21e92 (diff)
downloadmariadb-git-20aae56efa5aba90893e602ea87af2f14d161b10.tar.gz
Fixed bug mdev-10660.
The method Item_sum::print did not print opening '(' after the name of simple window functions (like rank, dense_rank etc). As a result the view definitions with such window functions were formed invalid in .frm files.
-rw-r--r--mysql-test/r/win.result34
-rw-r--r--mysql-test/t/win.test26
-rw-r--r--sql/item_sum.cc11
-rw-r--r--sql/item_sum.h23
-rw-r--r--sql/item_windowfunc.h2
5 files changed, 84 insertions, 12 deletions
diff --git a/mysql-test/r/win.result b/mysql-test/r/win.result
index fc215c2b793..2a4f6408377 100644
--- a/mysql-test/r/win.result
+++ b/mysql-test/r/win.result
@@ -456,7 +456,7 @@ pk, c,
row_number() over (partition by c order by pk
range between unbounded preceding and current row) as r
from t1;
-ERROR HY000: Window frame is not allowed with 'row_number('
+ERROR HY000: Window frame is not allowed with 'row_number'
select
pk, c,
rank() over w1 as r
@@ -2463,8 +2463,8 @@ drop table t1;
#
# MDEV-11594: window function over implicit grouping
#
-create table test.t1 (id int);
-insert into test.t1 values (1), (2), (3), (2);
+create table t1 (id int);
+insert into t1 values (1), (2), (3), (2);
select sum(id) over (order by sum(id)) from t1;
sum(id) over (order by sum(id))
1
@@ -2476,8 +2476,8 @@ drop table t1;
# MDEV-9923: integer constant in order by list
# of window specification
#
-create table test.t1 (id int);
-insert into test.t1 values (1), (2), (3), (2);
+create table t1 (id int);
+insert into t1 values (1), (2), (3), (2);
select rank() over (order by 1) from t1;
rank() over (order by 1)
1
@@ -2497,3 +2497,27 @@ rank() over (partition by id order by 2)
1
1
drop table t1;
+#
+# MDEV-10660: view using a simple window function
+#
+create table t1 (id int);
+insert into t1 values (1), (2), (3), (2);
+create view v1(id,rnk) as
+select id, rank() over (order by id) from t1;
+show create view v1;
+View Create View character_set_client collation_connection
+v1 CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v1` AS select `t1`.`id` AS `id`,rank() over ( order by `t1`.`id`) AS `rnk` from `t1` latin1 latin1_swedish_ci
+select id, rank() over (order by id) from t1;
+id rank() over (order by id)
+1 1
+2 2
+3 4
+2 2
+select * from v1;
+id rnk
+1 1
+2 2
+3 4
+2 2
+drop view v1;
+drop table t1;
diff --git a/mysql-test/t/win.test b/mysql-test/t/win.test
index 552be3484ef..15163f3a1d7 100644
--- a/mysql-test/t/win.test
+++ b/mysql-test/t/win.test
@@ -1495,8 +1495,8 @@ drop table t1;
--echo # MDEV-11594: window function over implicit grouping
--echo #
-create table test.t1 (id int);
-insert into test.t1 values (1), (2), (3), (2);
+create table t1 (id int);
+insert into t1 values (1), (2), (3), (2);
select sum(id) over (order by sum(id)) from t1;
@@ -1509,11 +1509,29 @@ drop table t1;
--echo # of window specification
--echo #
-create table test.t1 (id int);
-insert into test.t1 values (1), (2), (3), (2);
+create table t1 (id int);
+insert into t1 values (1), (2), (3), (2);
select rank() over (order by 1) from t1;
select rank() over (order by 2) from t1;
select rank() over (partition by id order by 2) from t1;
drop table t1;
+
+--echo #
+--echo # MDEV-10660: view using a simple window function
+--echo #
+
+create table t1 (id int);
+insert into t1 values (1), (2), (3), (2);
+
+create view v1(id,rnk) as
+ select id, rank() over (order by id) from t1;
+
+show create view v1;
+
+select id, rank() over (order by id) from t1;
+select * from v1;
+
+drop view v1;
+drop table t1;
diff --git a/sql/item_sum.cc b/sql/item_sum.cc
index 23f8638b724..2ca1be31ae1 100644
--- a/sql/item_sum.cc
+++ b/sql/item_sum.cc
@@ -471,6 +471,13 @@ void Item_sum::print(String *str, enum_query_type query_type)
/* orig_args is not filled with valid values until fix_fields() */
Item **pargs= fixed ? orig_args : args;
str->append(func_name());
+ /*
+ TODO:
+ The fact that func_name() may return a name with an extra '('
+ is really annoying. This shoud be fixed.
+ */
+ if (!is_aggr_sum_func())
+ str->append('(');
for (uint i=0 ; i < arg_count ; i++)
{
if (i)
@@ -594,7 +601,9 @@ Item *Item_sum::result_item(THD *thd, Field *field)
bool Item_sum::check_vcol_func_processor(void *arg)
{
- return mark_unsupported_function(func_name(), ")", arg, VCOL_IMPOSSIBLE);
+ return mark_unsupported_function(func_name(),
+ is_aggr_sum_func() ? ")" : "()",
+ arg, VCOL_IMPOSSIBLE);
}
diff --git a/sql/item_sum.h b/sql/item_sum.h
index 940726b5d8e..a838bd0bc10 100644
--- a/sql/item_sum.h
+++ b/sql/item_sum.h
@@ -409,10 +409,31 @@ public:
Item_sum(THD *thd, Item_sum *item);
enum Type type() const { return SUM_FUNC_ITEM; }
virtual enum Sumfunctype sum_func () const=0;
+ bool is_aggr_sum_func()
+ {
+ switch (sum_func()) {
+ case COUNT_FUNC:
+ case COUNT_DISTINCT_FUNC:
+ case SUM_FUNC:
+ case SUM_DISTINCT_FUNC:
+ case AVG_FUNC:
+ case AVG_DISTINCT_FUNC:
+ case MIN_FUNC:
+ case MAX_FUNC:
+ case STD_FUNC:
+ case VARIANCE_FUNC:
+ case SUM_BIT_FUNC:
+ case UDF_SUM_FUNC:
+ case GROUP_CONCAT_FUNC:
+ return true;
+ default:
+ return false;
+ }
+ }
/**
Resets the aggregate value to its default and aggregates the current
value of its attribute(s).
- */
+ */
inline bool reset_and_add()
{
aggregator_clear();
diff --git a/sql/item_windowfunc.h b/sql/item_windowfunc.h
index 15e5c9a7c5b..2beed69c0fa 100644
--- a/sql/item_windowfunc.h
+++ b/sql/item_windowfunc.h
@@ -127,7 +127,7 @@ public:
}
const char*func_name() const
{
- return "row_number(";
+ return "row_number";
}
Item *get_copy(THD *thd, MEM_ROOT *mem_root)