Skip to content

Commit

Permalink
Fixed bug mdev-10660.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
igorbabaev committed Feb 3, 2017
1 parent bc12d99 commit 20aae56
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 12 deletions.
34 changes: 29 additions & 5 deletions mysql-test/r/win.result
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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;
26 changes: 22 additions & 4 deletions mysql-test/t/win.test
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
11 changes: 10 additions & 1 deletion sql/item_sum.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
}


Expand Down
23 changes: 22 additions & 1 deletion sql/item_sum.h
Original file line number Diff line number Diff line change
Expand Up @@ -409,10 +409,31 @@ class Item_sum :public Item_func_or_sum
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();
Expand Down
2 changes: 1 addition & 1 deletion sql/item_windowfunc.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ class Item_sum_row_number: public Item_sum_int
}
const char*func_name() const
{
return "row_number(";
return "row_number";
}

Item *get_copy(THD *thd, MEM_ROOT *mem_root)
Expand Down

0 comments on commit 20aae56

Please sign in to comment.